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