Home | History | Annotate | Line # | Download | only in usb
udl.c revision 1.11
      1 /*	$NetBSD: udl.c,v 1.11 2014/02/23 13:22:32 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009 FUKAUMI Naoki.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * Copyright (c) 2009 Marcus Glocker <mglocker (at) openbsd.org>
     30  *
     31  * Permission to use, copy, modify, and distribute this software for any
     32  * purpose with or without fee is hereby granted, provided that the above
     33  * copyright notice and this permission notice appear in all copies.
     34  *
     35  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     36  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     37  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     38  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     39  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     40  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     41  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     42  */
     43 
     44 /*
     45  * Driver for the ``DisplayLink DL-1x0 / DL-1x5'' graphic chips based
     46  * on the reversed engineered specifications of Florian Echtler
     47  * <floe at butterbrot dot org>:
     48  *
     49  * 	http://floe.butterbrot.org/displaylink/doku.php
     50  *
     51  * This driver was written by Marcus Glocker for OpenBSD and ported to
     52  * NetBSD by FUKAUMI Naoki with many modification.
     53  */
     54 
     55 #include <sys/cdefs.h>
     56 __KERNEL_RCSID(0, "$NetBSD: udl.c,v 1.11 2014/02/23 13:22:32 skrll Exp $");
     57 
     58 #include <sys/param.h>
     59 #include <sys/device.h>
     60 #include <sys/kernel.h>
     61 #include <sys/proc.h>
     62 #include <sys/systm.h>
     63 #include <sys/kmem.h>
     64 #include <uvm/uvm.h>
     65 
     66 #include <sys/bus.h>
     67 #include <sys/endian.h>
     68 
     69 #include <dev/usb/usb.h>
     70 #include <dev/usb/usbdi.h>
     71 #include <dev/usb/usbdivar.h>
     72 #include <dev/usb/usbdi_util.h>
     73 #include <dev/usb/usb_mem.h>
     74 #include <dev/usb/usbdevs.h>
     75 
     76 #include <dev/firmload.h>
     77 
     78 #include <dev/videomode/videomode.h>
     79 #include <dev/videomode/edidvar.h>
     80 
     81 #include <dev/wscons/wsconsio.h>
     82 #include <dev/wscons/wsdisplayvar.h>
     83 #include <dev/rasops/rasops.h>
     84 
     85 #include <dev/usb/udl.h>
     86 #ifdef notyet
     87 #include <dev/usb/udlio.h>
     88 #endif
     89 
     90 /*
     91  * Defines.
     92  */
     93 #ifdef UDL_DEBUG
     94 #define DPRINTF(x)	do { if (udl_debug) printf x; } while (0)
     95 #define DPRINTFN(n, x)	do { if (udl_debug >= (n)) printf x; } while (0)
     96 int udl_debug = 1;
     97 #else
     98 #define DPRINTF(x)	do {} while (0)
     99 #define DPRINTFN(n, x)	do {} while (0)
    100 #endif
    101 
    102 /*
    103  * Prototypes.
    104  */
    105 static int		udl_match(device_t, cfdata_t, void *);
    106 static void		udl_attach(device_t, device_t, void *);
    107 static int		udl_detach(device_t, int);
    108 
    109 static int		udl_ioctl(void *, void *, u_long, void *, int,
    110 			    struct lwp *);
    111 static paddr_t		udl_mmap(void *, void *, off_t, int);
    112 static int		udl_alloc_screen(void *, const struct wsscreen_descr *,
    113 			    void **, int *, int *, long *);
    114 static void		udl_free_screen(void *, void *);
    115 static int		udl_show_screen(void *, void *, int,
    116 			    void (*)(void *, int, int), void *);
    117 
    118 static void		udl_comp_load(struct udl_softc *);
    119 static void		udl_comp_unload(struct udl_softc *);
    120 static int		udl_fbmem_alloc(struct udl_softc *);
    121 static void		udl_fbmem_free(struct udl_softc *);
    122 static int		udl_cmdq_alloc(struct udl_softc *);
    123 static void		udl_cmdq_free(struct udl_softc *);
    124 static struct udl_cmdq *udl_cmdq_get(struct udl_softc *sc);
    125 static void		udl_cmdq_put(struct udl_softc *sc,
    126 			    struct udl_cmdq *cmdq);
    127 static void		udl_cmdq_flush(struct udl_softc *);
    128 
    129 static void		udl_cursor(void *, int, int, int);
    130 static void		udl_putchar(void *, int, int, u_int, long);
    131 static void		udl_copycols(void *, int, int, int, int);
    132 static void		udl_erasecols(void *, int, int, int, long);
    133 static void		udl_copyrows(void *, int, int, int);
    134 static void		udl_eraserows(void *, int, int, long);
    135 
    136 static void		udl_restore_char(struct rasops_info *);
    137 static void		udl_draw_char(struct rasops_info *, uint16_t *, u_int,
    138 			    int, int);
    139 static void		udl_copy_rect(struct udl_softc *, int, int, int, int,
    140 			    int, int);
    141 static void		udl_fill_rect(struct udl_softc *, uint16_t, int, int,
    142 			    int, int);
    143 #ifdef notyet
    144 static void		udl_draw_rect(struct udl_softc *,
    145 			    struct udl_ioctl_damage *);
    146 static void		udl_draw_rect_comp(struct udl_softc *,
    147 			    struct udl_ioctl_damage *);
    148 #endif
    149 
    150 static inline void	udl_copy_line(struct udl_softc *, int, int, int);
    151 static inline void	udl_fill_line(struct udl_softc *, uint16_t, int, int);
    152 static inline void	udl_draw_line(struct udl_softc *, uint16_t *, int,
    153 			    int);
    154 #ifdef notyet
    155 static inline void	udl_draw_line_comp(struct udl_softc *, uint16_t *, int,
    156 			    int);
    157 #endif
    158 
    159 static int		udl_cmd_send(struct udl_softc *);
    160 static void		udl_cmd_send_async(struct udl_softc *);
    161 static void		udl_cmd_send_async_cb(usbd_xfer_handle,
    162 			    usbd_private_handle, usbd_status);
    163 
    164 static int		udl_ctrl_msg(struct udl_softc *, uint8_t, uint8_t,
    165 			    uint16_t, uint16_t, uint8_t *, uint16_t);
    166 static int		udl_init(struct udl_softc *);
    167 static void		udl_read_edid(struct udl_softc *);
    168 static void		udl_set_address(struct udl_softc *, int, int, int,
    169 			    int);
    170 static void		udl_blank(struct udl_softc *, int);
    171 static uint16_t		udl_lfsr(uint16_t);
    172 static int		udl_set_resolution(struct udl_softc *,
    173 			    const struct videomode *);
    174 static const struct videomode *udl_videomode_lookup(const char *);
    175 
    176 static inline void
    177 udl_cmd_add_1(struct udl_softc *sc, uint8_t val)
    178 {
    179 
    180 	*sc->sc_cmd_buf++ = val;
    181 }
    182 
    183 static inline void
    184 udl_cmd_add_2(struct udl_softc *sc, uint16_t val)
    185 {
    186 
    187 	be16enc(sc->sc_cmd_buf, val);
    188 	sc->sc_cmd_buf += 2;
    189 }
    190 
    191 static inline void
    192 udl_cmd_add_3(struct udl_softc *sc, uint32_t val)
    193 {
    194 
    195 	udl_cmd_add_2(sc, val >> 8);
    196 	udl_cmd_add_1(sc, val);
    197 }
    198 
    199 static inline void
    200 udl_cmd_add_4(struct udl_softc *sc, uint32_t val)
    201 {
    202 
    203 	be32enc(sc->sc_cmd_buf, val);
    204 	sc->sc_cmd_buf += 4;
    205 }
    206 
    207 static inline void
    208 udl_cmd_add_buf(struct udl_softc *sc, uint16_t *buf, int width)
    209 {
    210 #if BYTE_ORDER == BIG_ENDIAN
    211 	memcpy(sc->sc_cmd_buf, buf, width * 2);
    212 	sc->sc_cmd_buf += width * 2;
    213 #else
    214 	uint16_t *endp;
    215 
    216 	endp = buf + width;
    217 
    218 	if (((uintptr_t)sc->sc_cmd_buf & 1) == 0) {
    219 		while (buf < endp) {
    220 			*(uint16_t *)sc->sc_cmd_buf = htobe16(*buf++);
    221 			sc->sc_cmd_buf += 2;
    222 		}
    223 	} else {
    224 		while (buf < endp) {
    225 			be16enc(sc->sc_cmd_buf, *buf++);
    226 			sc->sc_cmd_buf += 2;
    227 		}
    228 	}
    229 #endif
    230 }
    231 
    232 static inline void
    233 udl_reg_write_1(struct udl_softc *sc, uint8_t reg, uint8_t val)
    234 {
    235 
    236 	udl_cmd_add_4(sc, (UDL_BULK_SOC << 24) |
    237 	    (UDL_BULK_CMD_REG_WRITE_1 << 16) | (reg << 8) | val);
    238 }
    239 
    240 static inline void
    241 udl_reg_write_2(struct udl_softc *sc, uint8_t reg, uint16_t val)
    242 {
    243 
    244 	udl_reg_write_1(sc, reg++, val >> 8);
    245 	udl_reg_write_1(sc, reg, val);
    246 }
    247 
    248 static inline void
    249 udl_reg_write_3(struct udl_softc *sc, uint8_t reg, uint32_t val)
    250 {
    251 
    252 	udl_reg_write_1(sc, reg++, val >> 16);
    253 	udl_reg_write_1(sc, reg++, val >> 8);
    254 	udl_reg_write_1(sc, reg, val);
    255 }
    256 
    257 /* XXX */
    258 static int
    259 firmware_load(const char *dname, const char *iname, uint8_t **ucodep,
    260     size_t *sizep)
    261 {
    262 	firmware_handle_t fh;
    263 	int error;
    264 
    265 	if ((error = firmware_open(dname, iname, &fh)) != 0)
    266 		return error;
    267 	*sizep = firmware_get_size(fh);
    268 	if ((*ucodep = firmware_malloc(*sizep)) == NULL) {
    269 		firmware_close(fh);
    270 		return ENOMEM;
    271 	}
    272 	if ((error = firmware_read(fh, 0, *ucodep, *sizep)) != 0)
    273 		firmware_free(*ucodep, *sizep);
    274 	firmware_close(fh);
    275 
    276 	return error;
    277 }
    278 
    279 /*
    280  * Driver glue.
    281  */
    282 CFATTACH_DECL_NEW(udl, sizeof(struct udl_softc),
    283 	udl_match, udl_attach, udl_detach, NULL);
    284 
    285 /*
    286  * wsdisplay glue.
    287  */
    288 static struct wsdisplay_accessops udl_accessops = {
    289 	udl_ioctl,
    290 	udl_mmap,
    291 	udl_alloc_screen,
    292 	udl_free_screen,
    293 	udl_show_screen,
    294 	NULL,
    295 	NULL,
    296 	NULL,
    297 };
    298 
    299 /*
    300  * Matching devices.
    301  */
    302 static const struct usb_devno udl_devs[] = {
    303 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_GUC2020 },
    304 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LD220 },
    305 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LD190 },
    306 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_U70 },
    307 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_VCUD60 },
    308 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_CONV },
    309 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_DLDVI },
    310 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_USBRGB },
    311 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCDUSB7X },
    312 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCDUSB10X },
    313 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_VGA10 },
    314 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_WSDVI },
    315 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_EC008 },
    316 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_GXDVIU2 },
    317 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD4300U },
    318 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD8000U },
    319 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_HPDOCK },
    320 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_NL571 },
    321 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_M01061 },
    322 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_NBDOCK },
    323 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_SWDVI },
    324 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LUM70 },
    325 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD8000UD_DVI },
    326 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LDEWX015U },
    327 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LT1421WIDE },
    328 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_SD_U2VDH },
    329 	{ USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_UM7X0 }
    330 };
    331 
    332 static int
    333 udl_match(device_t parent, cfdata_t match, void *aux)
    334 {
    335 	struct usb_attach_arg *uaa = aux;
    336 
    337 	if (usb_lookup(udl_devs, uaa->vendor, uaa->product) != NULL)
    338 		return UMATCH_VENDOR_PRODUCT;
    339 
    340 	return UMATCH_NONE;
    341 }
    342 
    343 static void
    344 udl_attach(device_t parent, device_t self, void *aux)
    345 {
    346 	struct udl_softc *sc = device_private(self);
    347 	struct usb_attach_arg *uaa = aux;
    348 	struct wsemuldisplaydev_attach_args aa;
    349 	const struct videomode *vmp;
    350 	usbd_status error;
    351 	char *devinfop;
    352 
    353 	aprint_naive("\n");
    354 	aprint_normal("\n");
    355 
    356 	sc->sc_dev = self;
    357 	sc->sc_udev = uaa->device;
    358 
    359 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    360 	aprint_normal_dev(sc->sc_dev, "%s\n", devinfop);
    361 	usbd_devinfo_free(devinfop);
    362 
    363 	/*
    364 	 * Set device configuration descriptor number.
    365 	 */
    366 	error = usbd_set_config_no(sc->sc_udev, 1, 0);
    367 	if (error != USBD_NORMAL_COMPLETION) {
    368 		aprint_error_dev(self, "failed to set configuration"
    369 		    ", err=%s\n", usbd_errstr(error));
    370 		return;
    371 	}
    372 
    373 	/*
    374 	 * Create device handle to interface descriptor.
    375 	 */
    376 	error = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
    377 	if (error != USBD_NORMAL_COMPLETION)
    378 		return;
    379 
    380 	/*
    381 	 * Open bulk TX pipe.
    382 	 */
    383 	error = usbd_open_pipe(sc->sc_iface, 1, USBD_EXCLUSIVE_USE,
    384 	    &sc->sc_tx_pipeh);
    385 	if (error != USBD_NORMAL_COMPLETION)
    386 		return;
    387 
    388 	/*
    389 	 * Allocate bulk command queue.
    390 	 */
    391 #ifdef UDL_EVENT_COUNTERS
    392 	evcnt_attach_dynamic(&sc->sc_ev_cmdq_get, EVCNT_TYPE_MISC, NULL,
    393 	    device_xname(sc->sc_dev), "udl_cmdq_get");
    394 	evcnt_attach_dynamic(&sc->sc_ev_cmdq_put, EVCNT_TYPE_MISC, NULL,
    395 	    device_xname(sc->sc_dev), "udl_cmdq_put");
    396 	evcnt_attach_dynamic(&sc->sc_ev_cmdq_wait, EVCNT_TYPE_MISC, NULL,
    397 	    device_xname(sc->sc_dev), "udl_cmdq_wait");
    398 	evcnt_attach_dynamic(&sc->sc_ev_cmdq_timeout, EVCNT_TYPE_MISC, NULL,
    399 	    device_xname(sc->sc_dev), "udl_cmdq_timeout");
    400 #endif
    401 
    402 	if (udl_cmdq_alloc(sc) != 0)
    403 		return;
    404 
    405 	cv_init(&sc->sc_cv, device_xname(sc->sc_dev));
    406 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_TTY); /* XXX for tty_lock */
    407 
    408 	if ((sc->sc_cmd_cur = udl_cmdq_get(sc)) == NULL)
    409 		return;
    410 	UDL_CMD_BUFINIT(sc);
    411 
    412 	/*
    413 	 * Initialize chip.
    414 	 */
    415 	if (udl_init(sc) != 0)
    416 		return;
    417 
    418 	udl_read_edid(sc);
    419 
    420 	/*
    421 	 * Initialize resolution.
    422 	 */
    423 #ifndef UDL_VIDEOMODE
    424 	if (sc->sc_ei.edid_nmodes != 0 &&
    425 	    sc->sc_ei.edid_preferred_mode != NULL)
    426 		vmp = sc->sc_ei.edid_preferred_mode;
    427 	else
    428 #define UDL_VIDEOMODE	"640x480x60"
    429 #endif
    430 		vmp = udl_videomode_lookup(UDL_VIDEOMODE);
    431 
    432 	if (vmp == NULL)
    433 		return;
    434 
    435 	sc->sc_width = vmp->hdisplay;
    436 	sc->sc_height = vmp->vdisplay;
    437 	sc->sc_offscreen = sc->sc_height * 3 / 2;
    438 	sc->sc_depth = 16;
    439 
    440 	if (udl_set_resolution(sc, vmp) != 0)
    441 		return;
    442 
    443 	sc->sc_defaultscreen.name = "default";
    444 	sc->sc_screens[0] = &sc->sc_defaultscreen;
    445 	sc->sc_screenlist.nscreens = 1;
    446 	sc->sc_screenlist.screens = sc->sc_screens;
    447 
    448 	/*
    449 	 * Set initial wsdisplay emulation mode.
    450 	 */
    451 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    452 
    453 	/*
    454 	 * Attach wsdisplay.
    455 	 */
    456 	aa.console = 0;
    457 	aa.scrdata = &sc->sc_screenlist;
    458 	aa.accessops = &udl_accessops;
    459 	aa.accesscookie = sc;
    460 
    461 	sc->sc_wsdisplay =
    462 	    config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    463 
    464 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    465 }
    466 
    467 static int
    468 udl_detach(device_t self, int flags)
    469 {
    470 	struct udl_softc *sc = device_private(self);
    471 
    472 	/*
    473 	 * Close bulk TX pipe.
    474 	 */
    475 	if (sc->sc_tx_pipeh != NULL) {
    476 		usbd_abort_pipe(sc->sc_tx_pipeh);
    477 		usbd_close_pipe(sc->sc_tx_pipeh);
    478 	}
    479 
    480 	/*
    481 	 * Free command xfer buffers.
    482 	 */
    483 	udl_cmdq_flush(sc);
    484 	udl_cmdq_free(sc);
    485 
    486 	cv_destroy(&sc->sc_cv);
    487 	mutex_destroy(&sc->sc_mtx);
    488 
    489 	/*
    490 	 * Free Huffman table.
    491 	 */
    492 	udl_comp_unload(sc);
    493 
    494 	/*
    495 	 * Free framebuffer memory.
    496 	 */
    497 	udl_fbmem_free(sc);
    498 
    499 	/*
    500 	 * Detach wsdisplay.
    501 	 */
    502 	if (sc->sc_wsdisplay != NULL)
    503 		config_detach(sc->sc_wsdisplay, DETACH_FORCE);
    504 
    505 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    506 
    507 #ifdef UDL_EVENT_COUNTERS
    508 	evcnt_detach(&sc->sc_ev_cmdq_get);
    509 	evcnt_detach(&sc->sc_ev_cmdq_put);
    510 	evcnt_detach(&sc->sc_ev_cmdq_wait);
    511 	evcnt_detach(&sc->sc_ev_cmdq_timeout);
    512 #endif
    513 
    514 	return 0;
    515 }
    516 
    517 static int
    518 udl_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    519 {
    520 	struct udl_softc *sc = v;
    521 #ifdef notyet
    522 	struct udl_ioctl_damage *d;
    523 #endif
    524 	struct wsdisplay_fbinfo *wdf;
    525 	u_int mode;
    526 
    527 	switch (cmd) {
    528 	case WSDISPLAYIO_GTYPE:
    529 		*(u_int *)data = WSDISPLAY_TYPE_DL;
    530 		return 0;
    531 
    532 	case WSDISPLAYIO_GINFO:
    533 		wdf = (struct wsdisplay_fbinfo *)data;
    534 		wdf->height = sc->sc_height;
    535 		wdf->width = sc->sc_width;
    536 		wdf->depth = sc->sc_depth;
    537 		wdf->cmsize = 0;
    538 		return 0;
    539 
    540 	case WSDISPLAYIO_GVIDEO:
    541 		*(u_int *)data = sc->sc_blank;
    542 		return 0;
    543 
    544 	case WSDISPLAYIO_SVIDEO:
    545 		mode = *(u_int *)data;
    546 		if (mode == sc->sc_blank)
    547 			return 0;
    548 		switch (mode) {
    549 		case WSDISPLAYIO_VIDEO_OFF:
    550 			udl_blank(sc, 1);
    551 			break;
    552 		case WSDISPLAYIO_VIDEO_ON:
    553 			udl_blank(sc, 0);
    554 			break;
    555 		default:
    556 			return EINVAL;
    557 		}
    558 		udl_cmd_send_async(sc);
    559 		udl_cmdq_flush(sc);
    560 		sc->sc_blank = mode;
    561 		return 0;
    562 
    563 	case WSDISPLAYIO_SMODE:
    564 		mode = *(u_int *)data;
    565 		if (mode == sc->sc_mode)
    566 			return 0;
    567 		switch (mode) {
    568 		case WSDISPLAYIO_MODE_EMUL:
    569 			/* clear screen */
    570 			udl_fill_rect(sc, 0, 0, 0, sc->sc_width,
    571 			    sc->sc_height);
    572 			udl_cmd_send_async(sc);
    573 			udl_cmdq_flush(sc);
    574 			udl_comp_unload(sc);
    575 			break;
    576 		case WSDISPLAYIO_MODE_DUMBFB:
    577 			if (UDL_CMD_BUFSIZE(sc) > 0)
    578 				udl_cmd_send_async(sc);
    579 			udl_cmdq_flush(sc);
    580 			udl_comp_load(sc);
    581 			break;
    582 		default:
    583 			return EINVAL;
    584 		}
    585 		sc->sc_mode = mode;
    586 		return 0;
    587 
    588 	case WSDISPLAYIO_LINEBYTES:
    589 		*(u_int *)data = sc->sc_width * (sc->sc_depth / 8);
    590 		return 0;
    591 
    592 #ifdef notyet
    593 	/*
    594 	 * XXX
    595 	 * OpenBSD allows device specific ioctl()s and use this
    596 	 * UDLIO_DAMAGE for the damage extension ops of X servers.
    597 	 * Before blindly pulling such interfaces, probably we should
    598 	 * discuss how such devices should be handled which have
    599 	 * in-direct framebuffer memories that should be transfered
    600 	 * per updated rectangle regions via MI wscons APIs.
    601 	 */
    602 	case UDLIO_DAMAGE:
    603 		d = (struct udl_ioctl_damage *)data;
    604 		d->status = UDLIO_STATUS_OK;
    605 		if (sc->sc_flags & UDL_COMPRDY)
    606 			udl_draw_rect_comp(sc, d);
    607 		else
    608 			udl_draw_rect(sc, d);
    609 		return 0;
    610 #endif
    611 	}
    612 
    613 	return EPASSTHROUGH;
    614 }
    615 
    616 static paddr_t
    617 udl_mmap(void *v, void *vs, off_t off, int prot)
    618 {
    619 	struct udl_softc *sc = v;
    620 	vaddr_t vaddr;
    621 	paddr_t paddr;
    622 	bool rv __diagused;
    623 
    624 	if (off < 0 || off > roundup2(UDL_FBMEM_SIZE(sc), PAGE_SIZE))
    625 		return -1;
    626 
    627 	/* allocate framebuffer memory */
    628 	if (udl_fbmem_alloc(sc) != 0)
    629 		return -1;
    630 
    631 	vaddr = (vaddr_t)sc->sc_fbmem + off;
    632 	rv = pmap_extract(pmap_kernel(), vaddr, &paddr);
    633 	KASSERT(rv);
    634 	paddr += vaddr & PGOFSET;
    635 
    636 	/* XXX we need MI paddr_t -> mmap cookie API */
    637 #if defined(__alpha__)
    638 #define PTOMMAP(paddr)	alpha_btop((char *)paddr)
    639 #elif defined(__arm__)
    640 #define PTOMMAP(paddr)	arm_btop((u_long)paddr)
    641 #elif defined(__hppa__)
    642 #define PTOMMAP(paddr)	btop((u_long)paddr)
    643 #elif defined(__i386__) || defined(__x86_64__)
    644 #define PTOMMAP(paddr)	x86_btop(paddr)
    645 #elif defined(__m68k__)
    646 #define PTOMMAP(paddr)	m68k_btop((char *)paddr)
    647 #elif defined(__mips__)
    648 #define PTOMMAP(paddr)	mips_btop(paddr)
    649 #elif defined(__powerpc__)
    650 #define PTOMMAP(paddr)	(paddr)
    651 #elif defined(__sh__)
    652 #define PTOMMAP(paddr)	sh3_btop(paddr)
    653 #elif defined(__sparc__)
    654 #define PTOMMAP(paddr)	(paddr)
    655 #elif defined(__sparc64__)
    656 #define PTOMMAP(paddr)	atop(paddr)
    657 #elif defined(__vax__)
    658 #define PTOMMAP(paddr)	btop((u_int)paddr)
    659 #endif
    660 
    661 	return PTOMMAP(paddr);
    662 }
    663 
    664 static int
    665 udl_alloc_screen(void *v, const struct wsscreen_descr *type,
    666     void **cookiep, int *curxp, int *curyp, long *attrp)
    667 {
    668 	struct udl_softc *sc = v;
    669 
    670 	if (sc->sc_nscreens > 0)
    671 		return ENOMEM;
    672 
    673 	/*
    674 	 * Initialize rasops.
    675 	 */
    676 	sc->sc_ri.ri_depth = sc->sc_depth;
    677 	sc->sc_ri.ri_bits = NULL;
    678 	sc->sc_ri.ri_width = sc->sc_width;
    679 	sc->sc_ri.ri_height = sc->sc_height;
    680 	sc->sc_ri.ri_stride = sc->sc_width * (sc->sc_depth / 8);
    681 	sc->sc_ri.ri_hw = sc;
    682 	sc->sc_ri.ri_flg = 0;
    683 
    684 	if (sc->sc_depth == 16) {
    685 		sc->sc_ri.ri_rnum = 5;
    686 		sc->sc_ri.ri_gnum = 6;
    687 		sc->sc_ri.ri_bnum = 5;
    688 		sc->sc_ri.ri_rpos = 11;
    689 		sc->sc_ri.ri_gpos = 5;
    690 		sc->sc_ri.ri_bpos = 0;
    691 	}
    692 
    693 	rasops_init(&sc->sc_ri, sc->sc_height / 8, sc->sc_width / 8);
    694 
    695 	sc->sc_ri.ri_ops.cursor = udl_cursor;
    696 	sc->sc_ri.ri_ops.putchar = udl_putchar;
    697 	sc->sc_ri.ri_ops.copycols = udl_copycols;
    698 	sc->sc_ri.ri_ops.erasecols = udl_erasecols;
    699 	sc->sc_ri.ri_ops.copyrows = udl_copyrows;
    700 	sc->sc_ri.ri_ops.eraserows = udl_eraserows;
    701 
    702 	sc->sc_ri.ri_ops.allocattr(&sc->sc_ri, 0, 0, 0, attrp);
    703 
    704 	sc->sc_defaultscreen.ncols = sc->sc_ri.ri_cols;
    705 	sc->sc_defaultscreen.nrows = sc->sc_ri.ri_rows;
    706 	sc->sc_defaultscreen.textops = &sc->sc_ri.ri_ops;
    707 	sc->sc_defaultscreen.fontwidth = sc->sc_ri.ri_font->fontwidth;
    708 	sc->sc_defaultscreen.fontheight = sc->sc_ri.ri_font->fontheight;
    709 	sc->sc_defaultscreen.capabilities = sc->sc_ri.ri_caps;
    710 
    711 	*cookiep = &sc->sc_ri;
    712 	*curxp = 0;
    713 	*curyp = 0;
    714 
    715 	sc->sc_nscreens++;
    716 
    717 	return 0;
    718 }
    719 
    720 static void
    721 udl_free_screen(void *v, void *cookie)
    722 {
    723 	struct udl_softc *sc = v;
    724 
    725 	sc->sc_nscreens--;
    726 }
    727 
    728 static int
    729 udl_show_screen(void *v, void *cookie, int waitok,
    730     void (*cb)(void *, int, int), void *cbarg)
    731 {
    732 
    733 	return 0;
    734 }
    735 
    736 static inline void
    737 udl_cmd_add_decomptable(struct udl_softc *sc, uint8_t *buf, int len)
    738 {
    739 
    740 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_DECOMP);
    741 	udl_cmd_add_4(sc, 0x263871cd);	/* magic number */
    742 	udl_cmd_add_4(sc, 0x00000200);	/* 512 byte chunks */
    743 	memcpy(sc->sc_cmd_buf, buf, len);
    744 	sc->sc_cmd_buf += len;
    745 }
    746 
    747 static void
    748 udl_comp_load(struct udl_softc *sc)
    749 {
    750 	struct udl_huffman *h;
    751 	uint8_t *decomp;
    752 	size_t decomp_size;
    753 	int error, i;
    754 
    755 	if (!(sc->sc_flags & UDL_DECOMPRDY)) {
    756 		error = firmware_load("udl", "udl-decomp", &decomp,
    757 		    &decomp_size);
    758 		if (error != 0) {
    759 			aprint_error_dev(sc->sc_dev,
    760 			    "error %d, could not read decomp table %s!\n",
    761 			    error, "udl-decomp");
    762 			return;
    763 		}
    764 		udl_cmd_add_decomptable(sc, decomp, decomp_size);
    765 		firmware_free(decomp, decomp_size);
    766 		if (udl_cmd_send(sc) != 0)
    767 			return;
    768 		sc->sc_flags |= UDL_DECOMPRDY;
    769 	}
    770 
    771 	if (!(sc->sc_flags & UDL_COMPRDY)) {
    772 		error = firmware_load("udl", "udl-comp", &sc->sc_huffman,
    773 		    &sc->sc_huffman_size);
    774 		if (error != 0) {
    775 			aprint_error_dev(sc->sc_dev,
    776 			    "error %d, could not read huffman table %s!\n",
    777 			    error, "udl-comp");
    778 			return;
    779 		}
    780 		h = (struct udl_huffman *)sc->sc_huffman;
    781 		for (i = 0; i < UDL_HUFFMAN_RECORDS; i++)
    782 			h[i].bit_pattern = be32toh(h[i].bit_pattern);
    783 		sc->sc_huffman_base = sc->sc_huffman + UDL_HUFFMAN_BASE;
    784 		sc->sc_flags |= UDL_COMPRDY;
    785 	}
    786 }
    787 
    788 static void
    789 udl_comp_unload(struct udl_softc *sc)
    790 {
    791 
    792 	if (sc->sc_flags & UDL_COMPRDY) {
    793 		firmware_free(sc->sc_huffman, sc->sc_huffman_size);
    794 		sc->sc_huffman = NULL;
    795 		sc->sc_huffman_size = 0;
    796 		sc->sc_flags &= ~UDL_COMPRDY;
    797 	}
    798 }
    799 
    800 static int
    801 udl_fbmem_alloc(struct udl_softc *sc)
    802 {
    803 
    804 	if (sc->sc_fbmem == NULL) {
    805 		sc->sc_fbmem = kmem_alloc(UDL_FBMEM_SIZE(sc), KM_SLEEP);
    806 		if (sc->sc_fbmem == NULL)
    807 			return -1;
    808 	}
    809 
    810 	return 0;
    811 }
    812 
    813 static void
    814 udl_fbmem_free(struct udl_softc *sc)
    815 {
    816 
    817 	if (sc->sc_fbmem != NULL) {
    818 		kmem_free(sc->sc_fbmem, UDL_FBMEM_SIZE(sc));
    819 		sc->sc_fbmem = NULL;
    820 	}
    821 }
    822 
    823 static int
    824 udl_cmdq_alloc(struct udl_softc *sc)
    825 {
    826 	struct udl_cmdq *cmdq;
    827 	int i;
    828 
    829 	TAILQ_INIT(&sc->sc_freecmd);
    830 	TAILQ_INIT(&sc->sc_xfercmd);
    831 
    832 	for (i = 0; i < UDL_NCMDQ; i++) {
    833 		cmdq = &sc->sc_cmdq[i];
    834 
    835 		cmdq->cq_sc = sc;
    836 
    837 		cmdq->cq_xfer = usbd_alloc_xfer(sc->sc_udev);
    838 		if (cmdq->cq_xfer == NULL) {
    839 			aprint_error_dev(sc->sc_dev,
    840 			    "%s: can't allocate xfer handle!\n", __func__);
    841 			goto error;
    842 		}
    843 
    844 		cmdq->cq_buf =
    845 		    usbd_alloc_buffer(cmdq->cq_xfer, UDL_CMD_BUFFER_SIZE);
    846 		if (cmdq->cq_buf == NULL) {
    847 			aprint_error_dev(sc->sc_dev,
    848 			    "%s: can't allocate xfer buffer!\n", __func__);
    849 			goto error;
    850 		}
    851 
    852 		TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmdq, cq_chain);
    853 	}
    854 
    855 	return 0;
    856 
    857  error:
    858 	udl_cmdq_free(sc);
    859 	return -1;
    860 }
    861 
    862 static void
    863 udl_cmdq_free(struct udl_softc *sc)
    864 {
    865 	struct udl_cmdq *cmdq;
    866 	int i;
    867 
    868 	for (i = 0; i < UDL_NCMDQ; i++) {
    869 		cmdq = &sc->sc_cmdq[i];
    870 
    871 		if (cmdq->cq_xfer != NULL) {
    872 			usbd_free_xfer(cmdq->cq_xfer);
    873 			cmdq->cq_xfer = NULL;
    874 			cmdq->cq_buf = NULL;
    875 		}
    876 	}
    877 }
    878 
    879 static struct udl_cmdq *
    880 udl_cmdq_get(struct udl_softc *sc)
    881 {
    882 	struct udl_cmdq *cmdq;
    883 
    884 	cmdq = TAILQ_FIRST(&sc->sc_freecmd);
    885 	if (cmdq != NULL) {
    886 		TAILQ_REMOVE(&sc->sc_freecmd, cmdq, cq_chain);
    887 		UDL_EVCNT_INCR(&sc->sc_ev_cmdq_get);
    888 	}
    889 
    890 	return cmdq;
    891 }
    892 
    893 static void
    894 udl_cmdq_put(struct udl_softc *sc, struct udl_cmdq *cmdq)
    895 {
    896 
    897 	TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmdq, cq_chain);
    898 	UDL_EVCNT_INCR(&sc->sc_ev_cmdq_put);
    899 }
    900 
    901 static void
    902 udl_cmdq_flush(struct udl_softc *sc)
    903 {
    904 
    905 	mutex_enter(&sc->sc_mtx);
    906 	while (TAILQ_FIRST(&sc->sc_xfercmd) != NULL)
    907 		cv_wait(&sc->sc_cv, &sc->sc_mtx);
    908 	mutex_exit(&sc->sc_mtx);
    909 }
    910 
    911 static void
    912 udl_cursor(void *cookie, int on, int row, int col)
    913 {
    914 	struct rasops_info *ri = cookie;
    915 	struct udl_softc *sc = ri->ri_hw;
    916 	int x, y, width, height;
    917 
    918 	if (ri->ri_flg & RI_CURSOR)
    919 		udl_restore_char(ri);
    920 
    921 	ri->ri_crow = row;
    922 	ri->ri_ccol = col;
    923 
    924 	if (on != 0) {
    925 		ri->ri_flg |= RI_CURSOR;
    926 
    927 		x = col * ri->ri_font->fontwidth;
    928 		y = row * ri->ri_font->fontheight;
    929 		width = ri->ri_font->fontwidth;
    930 		height = ri->ri_font->fontheight;
    931 
    932 		/* save the last character block to off-screen */
    933 		udl_copy_rect(sc, x, y, 0, sc->sc_offscreen, width, height);
    934 
    935 		/* draw cursor */
    936 		udl_fill_rect(sc, 0xffff, x, y, width, 1);
    937 		udl_fill_rect(sc, 0xffff, x, y + 1, 1, height - 2);
    938 		udl_fill_rect(sc, 0xffff, x + width - 1, y + 1, 1, height - 2);
    939 		udl_fill_rect(sc, 0xffff, x, y + height - 1, width, 1);
    940 
    941 		udl_cmd_send_async(sc);
    942 	} else
    943 		ri->ri_flg &= ~RI_CURSOR;
    944 }
    945 
    946 static void
    947 udl_putchar(void *cookie, int row, int col, u_int uc, long attr)
    948 {
    949 	struct rasops_info *ri = cookie;
    950 	struct udl_softc *sc = ri->ri_hw;
    951 	uint16_t rgb16[2];
    952 	int fg, bg, underline, x, y, width, height;
    953 
    954 	rasops_unpack_attr(attr, &fg, &bg, &underline);
    955 	rgb16[1] = (uint16_t)ri->ri_devcmap[fg];
    956 	rgb16[0] = (uint16_t)ri->ri_devcmap[bg];
    957 
    958 	x = col * ri->ri_font->fontwidth;
    959 	y = row * ri->ri_font->fontheight;
    960 	width = ri->ri_font->fontwidth;
    961 	height = ri->ri_font->fontheight;
    962 
    963 	if (uc == ' ') {
    964 		/*
    965 		 * Writting a block for the space character instead rendering
    966 		 * it from font bits is more slim.
    967 		 */
    968 		udl_fill_rect(sc, rgb16[0], x, y, width, height);
    969 	} else {
    970 		/* render a character from font bits */
    971 		udl_draw_char(ri, rgb16, uc, x, y);
    972 	}
    973 
    974 	if (underline != 0)
    975 		udl_fill_rect(sc, rgb16[1], x, y + height - 1, width, 1);
    976 
    977 #if 0
    978 	udl_cmd_send_async(sc);
    979 #endif
    980 }
    981 
    982 static void
    983 udl_copycols(void *cookie, int row, int src, int dst, int num)
    984 {
    985 	struct rasops_info *ri = cookie;
    986 	struct udl_softc *sc = ri->ri_hw;
    987 	int sx, dx, y, width, height;
    988 
    989 	sx = src * ri->ri_font->fontwidth;
    990 	dx = dst * ri->ri_font->fontwidth;
    991 	y = row * ri->ri_font->fontheight;
    992 	width = num * ri->ri_font->fontwidth;
    993 	height = ri->ri_font->fontheight;
    994 
    995 	/* copy row block to off-screen first to fix overlay-copy problem */
    996 	udl_copy_rect(sc, sx, y, 0, sc->sc_offscreen, width, height);
    997 
    998 	/* copy row block back from off-screen now */
    999 	udl_copy_rect(sc, 0, sc->sc_offscreen, dx, y, width, height);
   1000 #if 0
   1001 	udl_cmd_send_async(sc);
   1002 #endif
   1003 }
   1004 
   1005 static void
   1006 udl_erasecols(void *cookie, int row, int col, int num, long attr)
   1007 {
   1008 	struct rasops_info *ri = cookie;
   1009 	struct udl_softc *sc = ri->ri_hw;
   1010 	uint16_t rgb16;
   1011 	int fg, bg, x, y, width, height;
   1012 
   1013 	rasops_unpack_attr(attr, &fg, &bg, NULL);
   1014 	rgb16 = (uint16_t)ri->ri_devcmap[bg];
   1015 
   1016 	x = col * ri->ri_font->fontwidth;
   1017 	y = row * ri->ri_font->fontheight;
   1018 	width = num * ri->ri_font->fontwidth;
   1019 	height = ri->ri_font->fontheight;
   1020 
   1021 	udl_fill_rect(sc, rgb16, x, y, width, height);
   1022 #if 0
   1023 	udl_cmd_send_async(sc);
   1024 #endif
   1025 }
   1026 
   1027 static void
   1028 udl_copyrows(void *cookie, int src, int dst, int num)
   1029 {
   1030 	struct rasops_info *ri = cookie;
   1031 	struct udl_softc *sc = ri->ri_hw;
   1032 	int sy, ey, dy, width, height;
   1033 
   1034 	width = ri->ri_emuwidth;
   1035 	height = ri->ri_font->fontheight;
   1036 
   1037 	if (dst < src) {
   1038 		sy = src * height;
   1039 		ey = (src + num) * height;
   1040 		dy = dst * height;
   1041 
   1042 		while (sy < ey) {
   1043 			udl_copy_rect(sc, 0, sy, 0, dy, width, height);
   1044 			sy += height;
   1045 			dy += height;
   1046 		}
   1047 	} else {
   1048 		sy = (src + num) * height;
   1049 		ey = src * height;
   1050 		dy = (dst + num) * height;
   1051 
   1052 		while (sy > ey) {
   1053 			sy -= height;
   1054 			dy -= height;
   1055 			udl_copy_rect(sc, 0, sy, 0, dy, width, height);
   1056 		}
   1057 	}
   1058 #if 0
   1059 	udl_cmd_send_async(sc);
   1060 #endif
   1061 }
   1062 
   1063 static void
   1064 udl_eraserows(void *cookie, int row, int num, long attr)
   1065 {
   1066 	struct rasops_info *ri = cookie;
   1067 	struct udl_softc *sc = ri->ri_hw;
   1068 	uint16_t rgb16;
   1069 	int fg, bg, y, width, height;
   1070 
   1071 	rasops_unpack_attr(attr, &fg, &bg, NULL);
   1072 	rgb16 = (uint16_t)ri->ri_devcmap[bg];
   1073 
   1074 	y = row * ri->ri_font->fontheight;
   1075 	width = ri->ri_emuwidth;
   1076 	height = num * ri->ri_font->fontheight;
   1077 
   1078 	udl_fill_rect(sc, rgb16, 0, y, width, height);
   1079 #if 0
   1080 	udl_cmd_send_async(sc);
   1081 #endif
   1082 }
   1083 
   1084 static void
   1085 udl_restore_char(struct rasops_info *ri)
   1086 {
   1087 	struct udl_softc *sc = ri->ri_hw;
   1088 	int x, y, width, height;
   1089 
   1090 	x = ri->ri_ccol * ri->ri_font->fontwidth;
   1091 	y = ri->ri_crow * ri->ri_font->fontheight;
   1092 	width = ri->ri_font->fontwidth;
   1093 	height = ri->ri_font->fontheight;
   1094 
   1095 	/* restore the last saved character from off-screen */
   1096 	udl_copy_rect(sc, 0, sc->sc_offscreen, x, y, width, height);
   1097 }
   1098 
   1099 static void
   1100 udl_draw_char(struct rasops_info *ri, uint16_t *rgb16, u_int uc, int x, int y)
   1101 {
   1102 	struct udl_softc *sc = ri->ri_hw;
   1103 	struct wsdisplay_font *font = ri->ri_font;
   1104 	uint32_t fontbits;
   1105 	uint16_t pixels[32];
   1106 	uint8_t *fontbase;
   1107 	int i, soff, eoff;
   1108 
   1109 	soff = y * sc->sc_width + x;
   1110 	eoff = (y + font->fontheight) * sc->sc_width + x;
   1111 	fontbase = (uint8_t *)font->data + (uc - font->firstchar) *
   1112 	    ri->ri_fontscale;
   1113 
   1114 	while (soff < eoff) {
   1115 		fontbits = 0;
   1116 		switch (font->stride) {
   1117 		case 4:
   1118 			fontbits |= fontbase[3];
   1119 			/* FALLTHROUGH */
   1120 		case 3:
   1121 			fontbits |= fontbase[2] << 8;
   1122 			/* FALLTHROUGH */
   1123 		case 2:
   1124 			fontbits |= fontbase[1] << 16;
   1125 			/* FALLTHROUGH */
   1126 		case 1:
   1127 			fontbits |= fontbase[0] << 24;
   1128 		}
   1129 		fontbase += font->stride;
   1130 
   1131 		for (i = 0; i < font->fontwidth; i++) {
   1132 			pixels[i] = rgb16[(fontbits >> 31) & 1];
   1133 			fontbits <<= 1;
   1134 		}
   1135 
   1136 		udl_draw_line(sc, pixels, soff, font->fontwidth);
   1137 		soff += sc->sc_width;
   1138 	}
   1139 }
   1140 
   1141 static void
   1142 udl_copy_rect(struct udl_softc *sc, int sx, int sy, int dx, int dy, int width,
   1143     int height)
   1144 {
   1145 	int sbase, soff, ebase, eoff, dbase, doff, width_cur;
   1146 
   1147 	sbase = sy * sc->sc_width;
   1148 	ebase = (sy + height) * sc->sc_width;
   1149 	dbase = dy * sc->sc_width;
   1150 
   1151 	while (width > 0) {
   1152 		soff = sbase + sx;
   1153 		eoff = ebase + sx;
   1154 		doff = dbase + dx;
   1155 
   1156 		if (width >= UDL_CMD_WIDTH_MAX)
   1157 			width_cur = UDL_CMD_WIDTH_MAX;
   1158 		else
   1159 			width_cur = width;
   1160 
   1161 		while (soff < eoff) {
   1162 			udl_copy_line(sc, soff, doff, width_cur);
   1163 			soff += sc->sc_width;
   1164 			doff += sc->sc_width;
   1165 		}
   1166 
   1167 		sx += width_cur;
   1168 		dx += width_cur;
   1169 		width -= width_cur;
   1170 	}
   1171 }
   1172 
   1173 static void
   1174 udl_fill_rect(struct udl_softc *sc, uint16_t rgb16, int x, int y, int width,
   1175     int height)
   1176 {
   1177 	int sbase, soff, ebase, eoff, width_cur;
   1178 
   1179 	sbase = y * sc->sc_width;
   1180 	ebase = (y + height) * sc->sc_width;
   1181 
   1182 	while (width > 0) {
   1183 		soff = sbase + x;
   1184 		eoff = ebase + x;
   1185 
   1186 		if (width >= UDL_CMD_WIDTH_MAX)
   1187 			width_cur = UDL_CMD_WIDTH_MAX;
   1188 		else
   1189 			width_cur = width;
   1190 
   1191 		while (soff < eoff) {
   1192 			udl_fill_line(sc, rgb16, soff, width_cur);
   1193 			soff += sc->sc_width;
   1194 		}
   1195 
   1196 		x += width_cur;
   1197 		width -= width_cur;
   1198 	}
   1199 }
   1200 
   1201 #ifdef notyet
   1202 static void
   1203 udl_draw_rect(struct udl_softc *sc, struct udl_ioctl_damage *d)
   1204 {
   1205 	int sbase, soff, ebase, eoff, x, y, width, width_cur, height;
   1206 
   1207 	x = d->x1;
   1208 	y = d->y1;
   1209 	width = d->x2 - d->x1;
   1210 	height = d->y2 - d->y1;
   1211 	sbase = y * sc->sc_width;
   1212 	ebase = (y + height) * sc->sc_width;
   1213 
   1214 	while (width > 0) {
   1215 		soff = sbase + x;
   1216 		eoff = ebase + x;
   1217 
   1218 		if (width >= UDL_CMD_WIDTH_MAX)
   1219 			width_cur = UDL_CMD_WIDTH_MAX;
   1220 		else
   1221 			width_cur = width;
   1222 
   1223 		while (soff < eoff) {
   1224 			udl_draw_line(sc, (uint16_t *)sc->sc_fbmem + soff,
   1225 			    soff, width_cur);
   1226 			soff += sc->sc_width;
   1227 		}
   1228 
   1229 		x += width_cur;
   1230 		width -= width_cur;
   1231 	}
   1232 
   1233 	udl_cmd_send_async(sc);
   1234 }
   1235 
   1236 static void
   1237 udl_draw_rect_comp(struct udl_softc *sc, struct udl_ioctl_damage *d)
   1238 {
   1239 	int soff, eoff, x, y, width, height;
   1240 
   1241 	x = d->x1;
   1242 	y = d->y1;
   1243 	width = d->x2 - d->x1;
   1244 	height = d->y2 - d->y1;
   1245 	soff = y * sc->sc_width + x;
   1246 	eoff = (y + height) * sc->sc_width + x;
   1247 
   1248 	udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
   1249 	sc->sc_cmd_cblen = 4;
   1250 
   1251 	while (soff < eoff) {
   1252 		udl_draw_line_comp(sc, (uint16_t *)sc->sc_fbmem + soff, soff,
   1253 		    width);
   1254 		soff += sc->sc_width;
   1255 	}
   1256 
   1257 	udl_cmd_send_async(sc);
   1258 }
   1259 #endif
   1260 
   1261 static inline void
   1262 udl_copy_line(struct udl_softc *sc, int soff, int doff, int width)
   1263 {
   1264 
   1265 	if (__predict_false((UDL_CMD_BUFSIZE(sc) + UDL_CMD_COPY_SIZE + 2) >
   1266 	    UDL_CMD_BUFFER_SIZE))
   1267 		udl_cmd_send_async(sc);
   1268 
   1269 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_COPY16);
   1270 	udl_cmd_add_4(sc, ((doff * 2) << 8) | (width & 0xff));
   1271 
   1272 	udl_cmd_add_3(sc, soff * 2);
   1273 }
   1274 
   1275 static inline void
   1276 udl_fill_line(struct udl_softc *sc, uint16_t rgb16, int off, int width)
   1277 {
   1278 
   1279 	if (__predict_false((UDL_CMD_BUFSIZE(sc) + UDL_CMD_FILL_SIZE + 2) >
   1280 	    UDL_CMD_BUFFER_SIZE))
   1281 		udl_cmd_send_async(sc);
   1282 
   1283 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_RLE16);
   1284 	udl_cmd_add_4(sc, ((off * 2) << 8) | (width & 0xff));
   1285 
   1286 	udl_cmd_add_1(sc, width);
   1287 	udl_cmd_add_2(sc, rgb16);
   1288 }
   1289 
   1290 static inline void
   1291 udl_draw_line(struct udl_softc *sc, uint16_t *buf, int off, int width)
   1292 {
   1293 
   1294 	if (__predict_false(
   1295 	    (UDL_CMD_BUFSIZE(sc) + UDL_CMD_DRAW_SIZE(width) + 2) >
   1296 	    UDL_CMD_BUFFER_SIZE))
   1297 		udl_cmd_send_async(sc);
   1298 
   1299 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_WRITE16);
   1300 	udl_cmd_add_4(sc, ((off * 2) << 8) | (width & 0xff));
   1301 
   1302 	udl_cmd_add_buf(sc, buf, width);
   1303 }
   1304 
   1305 #ifdef notyet
   1306 static inline int
   1307 udl_cmd_add_buf_comp(struct udl_softc *sc, uint16_t *buf, int width)
   1308 {
   1309 	struct udl_huffman *h;
   1310 	uint16_t *startp, *endp;
   1311 	uint32_t bit_pattern;
   1312 	uint16_t prev;
   1313 	int16_t diff;
   1314 	uint8_t bit_count, bit_pos, bit_rem, curlen;
   1315 
   1316 	startp = buf;
   1317 	if (width >= UDL_CMD_WIDTH_MAX)
   1318 		endp = buf + UDL_CMD_WIDTH_MAX;
   1319 	else
   1320 		endp = buf + width;
   1321 
   1322 	prev = bit_pos = *sc->sc_cmd_buf = 0;
   1323 	bit_rem = 8;
   1324 
   1325 	/*
   1326 	 * Generate a sub-block with maximal 256 pixels compressed data.
   1327 	 */
   1328 	while (buf < endp) {
   1329 		/* get difference between current and previous pixel */
   1330 		diff = *buf - prev;
   1331 
   1332 		/* get the huffman difference bit sequence */
   1333 		h = (struct udl_huffman *)sc->sc_huffman_base + diff;
   1334 		bit_count = h->bit_count;
   1335 		bit_pattern = h->bit_pattern;
   1336 
   1337 		curlen = (bit_pos + bit_count + 7) / 8;
   1338 		if (__predict_false((sc->sc_cmd_cblen + curlen + 1) >
   1339 		    UDL_CMD_COMP_BLOCK_SIZE))
   1340 			break;
   1341 
   1342 		/* generate one pixel compressed data */
   1343 		while (bit_count >= bit_rem) {
   1344 			*sc->sc_cmd_buf++ |=
   1345 			    (bit_pattern & ((1 << bit_rem) - 1)) << bit_pos;
   1346 			*sc->sc_cmd_buf = 0;
   1347 			sc->sc_cmd_cblen++;
   1348 			bit_count -= bit_rem;
   1349 			bit_pattern >>= bit_rem;
   1350 			bit_pos = 0;
   1351 			bit_rem = 8;
   1352 		}
   1353 
   1354 		if (bit_count > 0) {
   1355 			*sc->sc_cmd_buf |=
   1356 			    (bit_pattern & ((1 << bit_count) - 1)) << bit_pos;
   1357 			bit_pos += bit_count;
   1358 			bit_rem -= bit_count;
   1359 		}
   1360 
   1361 		prev = *buf++;
   1362 	}
   1363 
   1364 	/*
   1365  	 * If we have bits left in our last byte, round up to the next
   1366  	 * byte, so we don't overwrite them.
   1367  	 */
   1368 	if (bit_pos > 0) {
   1369 		sc->sc_cmd_buf++;
   1370 		sc->sc_cmd_cblen++;
   1371 	}
   1372 
   1373 	/* return how many pixels we have compressed */
   1374 	return buf - startp;
   1375 }
   1376 
   1377 static inline void
   1378 udl_draw_line_comp(struct udl_softc *sc, uint16_t *buf, int off, int width)
   1379 {
   1380 	uint8_t *widthp;
   1381 	int width_cur;
   1382 
   1383 	while (width > 0) {
   1384 		if (__predict_false(
   1385 		    (sc->sc_cmd_cblen + UDL_CMD_COMP_MIN_SIZE + 1) >
   1386 		    UDL_CMD_COMP_BLOCK_SIZE)) {
   1387 			if (UDL_CMD_BUFSIZE(sc) < UDL_CMD_COMP_THRESHOLD) {
   1388 				while (sc->sc_cmd_cblen <
   1389 				    UDL_CMD_COMP_BLOCK_SIZE) {
   1390 					*sc->sc_cmd_buf++ = 0;
   1391 					sc->sc_cmd_cblen++;
   1392 				}
   1393 			} else
   1394 				udl_cmd_send_async(sc);
   1395 			udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
   1396 			sc->sc_cmd_cblen = 4;
   1397 		}
   1398 
   1399 		udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) |
   1400 		    (UDL_BULK_CMD_FB_WRITE16 | UDL_BULK_CMD_FB_COMP));
   1401 		udl_cmd_add_4(sc, (off * 2) << 8);
   1402 
   1403 		widthp = sc->sc_cmd_buf - 1;
   1404 
   1405 		sc->sc_cmd_cblen += UDL_CMD_HEADER_SIZE;
   1406 
   1407 		width_cur = udl_cmd_add_buf_comp(sc, buf, width);
   1408 
   1409 		*widthp = width_cur;
   1410 		buf += width_cur;
   1411 		off += width_cur;
   1412 		width -= width_cur;
   1413 	}
   1414 }
   1415 #endif
   1416 
   1417 static int
   1418 udl_cmd_send(struct udl_softc *sc)
   1419 {
   1420 	struct udl_cmdq *cmdq;
   1421 	usbd_status error;
   1422 	uint32_t len;
   1423 
   1424 	cmdq = sc->sc_cmd_cur;
   1425 
   1426 	/* mark end of command stack */
   1427 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_EOC);
   1428 
   1429 	len = UDL_CMD_BUFSIZE(sc);
   1430 
   1431 	/* do xfer */
   1432 	error = usbd_bulk_transfer(cmdq->cq_xfer, sc->sc_tx_pipeh,
   1433 	    USBD_NO_COPY, USBD_NO_TIMEOUT, cmdq->cq_buf, &len, "udlcmds");
   1434 
   1435 	UDL_CMD_BUFINIT(sc);
   1436 
   1437 	if (error != USBD_NORMAL_COMPLETION) {
   1438 		aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__,
   1439 		    usbd_errstr(error));
   1440 		return -1;
   1441 	}
   1442 
   1443 	return 0;
   1444 }
   1445 
   1446 static void
   1447 udl_cmd_send_async(struct udl_softc *sc)
   1448 {
   1449 	struct udl_cmdq *cmdq;
   1450 	usbd_status error;
   1451 	uint32_t len;
   1452 
   1453 #if 1
   1454 	/*
   1455 	 * XXX
   1456 	 * All tty ops for wsemul are called with tty_lock spin mutex held,
   1457 	 * so we can't call cv_wait(9) here to acquire a free buffer.
   1458 	 * For now, all commands and data for wsemul ops are discarded
   1459 	 * if there is no free command buffer, and then screen text might
   1460 	 * be corrupted on large scroll ops etc.
   1461 	 *
   1462 	 * Probably we have to reorganize the giant tty_lock mutex, or
   1463 	 * change wsdisplay APIs (especially wsdisplaystart()) to return
   1464 	 * a number of actually handled characters as OpenBSD does, but
   1465 	 * the latter one requires whole API changes around rasops(9) etc.
   1466 	 */
   1467 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1468 		if (TAILQ_FIRST(&sc->sc_freecmd) == NULL) {
   1469 			UDL_CMD_BUFINIT(sc);
   1470 			return;
   1471 		}
   1472 	}
   1473 #endif
   1474 
   1475 	cmdq = sc->sc_cmd_cur;
   1476 
   1477 	/* mark end of command stack */
   1478 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_EOC);
   1479 
   1480 	len = UDL_CMD_BUFSIZE(sc);
   1481 
   1482 	/* do xfer */
   1483 	mutex_enter(&sc->sc_mtx);
   1484 	usbd_setup_xfer(cmdq->cq_xfer, sc->sc_tx_pipeh, cmdq, cmdq->cq_buf,
   1485 	    len, USBD_NO_COPY, USBD_NO_TIMEOUT, udl_cmd_send_async_cb);
   1486 	error = usbd_transfer(cmdq->cq_xfer);
   1487 	if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
   1488 		aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__,
   1489 		    usbd_errstr(error));
   1490 		mutex_exit(&sc->sc_mtx);
   1491 		goto end;
   1492 	}
   1493 
   1494 	TAILQ_INSERT_TAIL(&sc->sc_xfercmd, cmdq, cq_chain);
   1495 	cmdq = udl_cmdq_get(sc);
   1496 	mutex_exit(&sc->sc_mtx);
   1497 	while (cmdq == NULL) {
   1498 		int err;
   1499 		UDL_EVCNT_INCR(&sc->sc_ev_cmdq_wait);
   1500 		mutex_enter(&sc->sc_mtx);
   1501 		err = cv_timedwait(&sc->sc_cv, &sc->sc_mtx,
   1502 		    mstohz(100) /* XXX is this needed? */);
   1503 		if (err != 0) {
   1504 			DPRINTF(("%s: %s: cv timeout (error = %d)\n",
   1505 			    device_xname(sc->sc_dev), __func__, err));
   1506 			UDL_EVCNT_INCR(&sc->sc_ev_cmdq_timeout);
   1507 		}
   1508 		cmdq = udl_cmdq_get(sc);
   1509 		mutex_exit(&sc->sc_mtx);
   1510 	}
   1511 	sc->sc_cmd_cur = cmdq;
   1512  end:
   1513 	UDL_CMD_BUFINIT(sc);
   1514 }
   1515 
   1516 static void
   1517 udl_cmd_send_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
   1518     usbd_status status)
   1519 {
   1520 	struct udl_cmdq *cmdq = priv;
   1521 	struct udl_softc *sc = cmdq->cq_sc;
   1522 
   1523 	if (status != USBD_NORMAL_COMPLETION) {
   1524 		aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__,
   1525 		    usbd_errstr(status));
   1526 
   1527 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1528 			return;
   1529 		if (status == USBD_STALLED)
   1530 			usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
   1531 	}
   1532 
   1533 	mutex_enter(&sc->sc_mtx);
   1534 	TAILQ_REMOVE(&sc->sc_xfercmd, cmdq, cq_chain);
   1535 	udl_cmdq_put(sc, cmdq);
   1536 
   1537 	/* signal xfer op that sleeps for a free xfer buffer */
   1538 	cv_signal(&sc->sc_cv);
   1539 	mutex_exit(&sc->sc_mtx);
   1540 }
   1541 
   1542 static int
   1543 udl_ctrl_msg(struct udl_softc *sc, uint8_t rt, uint8_t r, uint16_t index,
   1544     uint16_t value, uint8_t *buf, uint16_t len)
   1545 {
   1546 	usb_device_request_t req;
   1547 	usbd_status error;
   1548 
   1549 	req.bmRequestType = rt;
   1550 	req.bRequest = r;
   1551 	USETW(req.wIndex, index);
   1552 	USETW(req.wValue, value);
   1553 	USETW(req.wLength, len);
   1554 
   1555 	error = usbd_do_request(sc->sc_udev, &req, buf);
   1556 	if (error != USBD_NORMAL_COMPLETION) {
   1557 		aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__,
   1558 		    usbd_errstr(error));
   1559 		return -1;
   1560 	}
   1561 
   1562 	return 0;
   1563 }
   1564 
   1565 static int
   1566 udl_init(struct udl_softc *sc)
   1567 {
   1568 	static uint8_t key[16] = {
   1569 	    0x57, 0xcd, 0xdc, 0xa7, 0x1c, 0x88, 0x5e, 0x15,
   1570 	    0x60, 0xfe, 0xc6, 0x97, 0x16, 0x3d, 0x47, 0xf2
   1571 	};
   1572 	uint8_t status[4], val;
   1573 
   1574 	if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_STATUS,
   1575 	    0x0000, 0x0000, status, sizeof(status)) != 0)
   1576 		return -1;
   1577 
   1578 	if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_1,
   1579 	    0xc484, 0x0000, &val, 1) != 0)
   1580 		return -1;
   1581 
   1582 	val = 1;
   1583 	if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_WRITE_1,
   1584 	    0xc41f, 0x0000, &val, 1) != 0)
   1585 		return -1;
   1586 
   1587 	if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_SET_KEY,
   1588 	    0x0000, 0x0000, key, sizeof(key)) != 0)
   1589 		return -1;
   1590 
   1591 	val = 0;
   1592 	if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_WRITE_1,
   1593 	    0xc40b, 0x0000, &val, 1) != 0)
   1594 		return -1;
   1595 
   1596 	return 0;
   1597 }
   1598 
   1599 static void
   1600 udl_read_edid(struct udl_softc *sc)
   1601 {
   1602 	uint8_t buf[64], edid[128];
   1603 	int offset;
   1604 
   1605 	memset(&sc->sc_ei, 0, sizeof(struct edid_info));
   1606 
   1607 	offset = 0;
   1608 	if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID,
   1609 	    0x00a1, (offset << 8), buf, 64) != 0)
   1610 		return;
   1611 	if (buf[0] != 0)
   1612 		return;
   1613 	memcpy(&edid[offset], &buf[1], 63);
   1614 	offset += 63;
   1615 
   1616 	if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID,
   1617 	    0x00a1, (offset << 8), buf, 64) != 0)
   1618 		return;
   1619 	if (buf[0] != 0)
   1620 		return;
   1621 	memcpy(&edid[offset], &buf[1], 63);
   1622 	offset += 63;
   1623 
   1624 	if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID,
   1625 	    0x00a1, (offset << 8), buf, 3) != 0)
   1626 		return;
   1627 	if (buf[0] != 0)
   1628 		return;
   1629 	memcpy(&edid[offset], &buf[1], 2);
   1630 
   1631 	if (edid_parse(edid, &sc->sc_ei) == 0) {
   1632 #ifdef UDL_DEBUG
   1633 		edid_print(&sc->sc_ei);
   1634 #endif
   1635 	}
   1636 }
   1637 
   1638 static void
   1639 udl_set_address(struct udl_softc *sc, int start16, int stride16, int start8,
   1640     int stride8)
   1641 {
   1642 	udl_reg_write_1(sc, UDL_REG_SYNC, 0x00);
   1643 	udl_reg_write_3(sc, UDL_REG_ADDR_START16, start16);
   1644 	udl_reg_write_3(sc, UDL_REG_ADDR_STRIDE16, stride16);
   1645 	udl_reg_write_3(sc, UDL_REG_ADDR_START8, start8);
   1646 	udl_reg_write_3(sc, UDL_REG_ADDR_STRIDE8, stride8);
   1647 	udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
   1648 }
   1649 
   1650 static void
   1651 udl_blank(struct udl_softc *sc, int blank)
   1652 {
   1653 
   1654 	if (blank != 0)
   1655 		udl_reg_write_1(sc, UDL_REG_BLANK, UDL_REG_BLANK_ON);
   1656 	else
   1657 		udl_reg_write_1(sc, UDL_REG_BLANK, UDL_REG_BLANK_OFF);
   1658 	udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
   1659 }
   1660 
   1661 static uint16_t
   1662 udl_lfsr(uint16_t count)
   1663 {
   1664 	uint16_t val = 0xffff;
   1665 
   1666 	while (count > 0) {
   1667 		val = (uint16_t)(val << 1) | ((uint16_t)(
   1668 		    (uint16_t)(val << 0) ^
   1669 		    (uint16_t)(val << 11) ^
   1670 		    (uint16_t)(val << 13) ^
   1671 		    (uint16_t)(val << 14)
   1672 		    ) >> 15);
   1673 		count--;
   1674 	}
   1675 
   1676 	return val;
   1677 }
   1678 
   1679 static int
   1680 udl_set_resolution(struct udl_softc *sc, const struct videomode *vmp)
   1681 {
   1682 	uint16_t val;
   1683 	int start16, stride16, start8, stride8;
   1684 
   1685 	/* set video memory offsets */
   1686 	start16 = 0;
   1687 	stride16 = sc->sc_width * 2;
   1688 	start8 = stride16 * sc->sc_height;
   1689 	stride8 = sc->sc_width;
   1690 	udl_set_address(sc, start16, stride16, start8, stride8);
   1691 
   1692 	/* write resolution values */
   1693 	udl_reg_write_1(sc, UDL_REG_SYNC, 0x00);
   1694 	udl_reg_write_1(sc, UDL_REG_COLORDEPTH, UDL_REG_COLORDEPTH_16);
   1695 	val = vmp->htotal - vmp->hsync_start;
   1696 	udl_reg_write_2(sc, UDL_REG_XDISPLAYSTART, udl_lfsr(val));
   1697 	val += vmp->hdisplay;
   1698 	udl_reg_write_2(sc, UDL_REG_XDISPLAYEND, udl_lfsr(val));
   1699 	val = vmp->vtotal - vmp->vsync_start;
   1700 	udl_reg_write_2(sc, UDL_REG_YDISPLAYSTART, udl_lfsr(val));
   1701 	val += vmp->vdisplay;
   1702 	udl_reg_write_2(sc, UDL_REG_YDISPLAYEND, udl_lfsr(val));
   1703 	val = vmp->htotal - 1;
   1704 	udl_reg_write_2(sc, UDL_REG_XENDCOUNT, udl_lfsr(val));
   1705 	val = vmp->hsync_end - vmp->hsync_start + 1;
   1706 	if (vmp->flags & VID_PHSYNC) {
   1707 		udl_reg_write_2(sc, UDL_REG_HSYNCSTART, udl_lfsr(1));
   1708 		udl_reg_write_2(sc, UDL_REG_HSYNCEND, udl_lfsr(val));
   1709 	} else {
   1710 		udl_reg_write_2(sc, UDL_REG_HSYNCSTART, udl_lfsr(val));
   1711 		udl_reg_write_2(sc, UDL_REG_HSYNCEND, udl_lfsr(1));
   1712 	}
   1713 	val = vmp->hdisplay;
   1714 	udl_reg_write_2(sc, UDL_REG_HPIXELS, val);
   1715 	val = vmp->vtotal;
   1716 	udl_reg_write_2(sc, UDL_REG_YENDCOUNT, udl_lfsr(val));
   1717 	val = vmp->vsync_end - vmp->vsync_start;
   1718 	if (vmp->flags & VID_PVSYNC) {
   1719 		udl_reg_write_2(sc, UDL_REG_VSYNCSTART, udl_lfsr(0));
   1720 		udl_reg_write_2(sc, UDL_REG_VSYNCEND, udl_lfsr(val));
   1721 	} else {
   1722 		udl_reg_write_2(sc, UDL_REG_VSYNCSTART, udl_lfsr(val));
   1723 		udl_reg_write_2(sc, UDL_REG_VSYNCEND, udl_lfsr(0));
   1724 	}
   1725 	val = vmp->vdisplay;
   1726 	udl_reg_write_2(sc, UDL_REG_VPIXELS, val);
   1727 	val = vmp->dot_clock / 5;
   1728 	udl_reg_write_2(sc, UDL_REG_PIXELCLOCK5KHZ, bswap16(val));
   1729 	udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
   1730 
   1731 	if (udl_cmd_send(sc) != 0)
   1732 		return -1;
   1733 
   1734 	/* clear screen */
   1735 	udl_fill_rect(sc, 0, 0, 0, sc->sc_width, sc->sc_height);
   1736 
   1737 	if (udl_cmd_send(sc) != 0)
   1738 		return -1;
   1739 
   1740 	/* show framebuffer content */
   1741 	udl_blank(sc, 0);
   1742 
   1743 	if (udl_cmd_send(sc) != 0)
   1744 		return -1;
   1745 
   1746 	sc->sc_blank = WSDISPLAYIO_VIDEO_ON;
   1747 
   1748 	return 0;
   1749 }
   1750 
   1751 static const struct videomode *
   1752 udl_videomode_lookup(const char *name)
   1753 {
   1754 	int i;
   1755 
   1756 	for (i = 0; i < videomode_count; i++)
   1757 		if (strcmp(name, videomode_list[i].name) == 0)
   1758 			return &videomode_list[i];
   1759 
   1760 	return NULL;
   1761 }
   1762