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