Home | History | Annotate | Line # | Download | only in usb
udl.c revision 1.21
      1 /*	$NetBSD: udl.c,v 1.21 2018/04/01 04:35:05 ryo 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.21 2018/04/01 04:35:05 ryo 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(__aarch64__)
    677 #define PTOMMAP(paddr)	aarch64_btop((char *)paddr)
    678 #elif defined(__alpha__)
    679 #define PTOMMAP(paddr)	alpha_btop((char *)paddr)
    680 #elif defined(__arm__)
    681 #define PTOMMAP(paddr)	arm_btop((u_long)paddr)
    682 #elif defined(__hppa__)
    683 #define PTOMMAP(paddr)	btop((u_long)paddr)
    684 #elif defined(__i386__) || defined(__x86_64__)
    685 #define PTOMMAP(paddr)	x86_btop(paddr)
    686 #elif defined(__m68k__)
    687 #define PTOMMAP(paddr)	m68k_btop((char *)paddr)
    688 #elif defined(__mips__)
    689 #define PTOMMAP(paddr)	mips_btop(paddr)
    690 #elif defined(__powerpc__)
    691 #define PTOMMAP(paddr)	(paddr)
    692 #elif defined(__sh__)
    693 #define PTOMMAP(paddr)	sh3_btop(paddr)
    694 #elif defined(__sparc__)
    695 #define PTOMMAP(paddr)	(paddr)
    696 #elif defined(__sparc64__)
    697 #define PTOMMAP(paddr)	atop(paddr)
    698 #elif defined(__vax__)
    699 #define PTOMMAP(paddr)	btop((u_int)paddr)
    700 #endif
    701 
    702 	return PTOMMAP(paddr);
    703 }
    704 
    705 static int
    706 udl_alloc_screen(void *v, const struct wsscreen_descr *type,
    707     void **cookiep, int *curxp, int *curyp, long *attrp)
    708 {
    709 	struct udl_softc *sc = v;
    710 
    711 	if (sc->sc_nscreens > 0)
    712 		return ENOMEM;
    713 
    714 	/*
    715 	 * Initialize rasops.
    716 	 */
    717 	sc->sc_ri.ri_depth = sc->sc_depth;
    718 	sc->sc_ri.ri_bits = NULL;
    719 	sc->sc_ri.ri_width = sc->sc_width;
    720 	sc->sc_ri.ri_height = sc->sc_height;
    721 	sc->sc_ri.ri_stride = sc->sc_width * (sc->sc_depth / 8);
    722 	sc->sc_ri.ri_hw = sc;
    723 	sc->sc_ri.ri_flg = 0;
    724 
    725 	if (sc->sc_depth == 16) {
    726 		sc->sc_ri.ri_rnum = 5;
    727 		sc->sc_ri.ri_gnum = 6;
    728 		sc->sc_ri.ri_bnum = 5;
    729 		sc->sc_ri.ri_rpos = 11;
    730 		sc->sc_ri.ri_gpos = 5;
    731 		sc->sc_ri.ri_bpos = 0;
    732 	}
    733 
    734 	rasops_init(&sc->sc_ri, sc->sc_height / 8, sc->sc_width / 8);
    735 
    736 	sc->sc_ri.ri_ops.cursor = udl_cursor;
    737 	sc->sc_ri.ri_ops.putchar = udl_putchar;
    738 	sc->sc_ri.ri_ops.copycols = udl_copycols;
    739 	sc->sc_ri.ri_ops.erasecols = udl_erasecols;
    740 	sc->sc_ri.ri_ops.copyrows = udl_copyrows;
    741 	sc->sc_ri.ri_ops.eraserows = udl_eraserows;
    742 
    743 	sc->sc_ri.ri_ops.allocattr(&sc->sc_ri, 0, 0, 0, attrp);
    744 
    745 	sc->sc_defaultscreen.ncols = sc->sc_ri.ri_cols;
    746 	sc->sc_defaultscreen.nrows = sc->sc_ri.ri_rows;
    747 	sc->sc_defaultscreen.textops = &sc->sc_ri.ri_ops;
    748 	sc->sc_defaultscreen.fontwidth = sc->sc_ri.ri_font->fontwidth;
    749 	sc->sc_defaultscreen.fontheight = sc->sc_ri.ri_font->fontheight;
    750 	sc->sc_defaultscreen.capabilities = sc->sc_ri.ri_caps;
    751 
    752 	*cookiep = &sc->sc_ri;
    753 	*curxp = 0;
    754 	*curyp = 0;
    755 
    756 	sc->sc_nscreens++;
    757 
    758 	return 0;
    759 }
    760 
    761 static void
    762 udl_free_screen(void *v, void *cookie)
    763 {
    764 	struct udl_softc *sc = v;
    765 
    766 	sc->sc_nscreens--;
    767 }
    768 
    769 static int
    770 udl_show_screen(void *v, void *cookie, int waitok,
    771     void (*cb)(void *, int, int), void *cbarg)
    772 {
    773 
    774 	return 0;
    775 }
    776 
    777 static inline void
    778 udl_cmd_add_decomptable(struct udl_softc *sc, uint8_t *buf, int len)
    779 {
    780 
    781 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_DECOMP);
    782 	udl_cmd_add_4(sc, 0x263871cd);	/* magic number */
    783 	udl_cmd_add_4(sc, 0x00000200);	/* 512 byte chunks */
    784 	memcpy(sc->sc_cmd_buf, buf, len);
    785 	sc->sc_cmd_buf += len;
    786 }
    787 
    788 static void
    789 udl_comp_load(struct udl_softc *sc)
    790 {
    791 	struct udl_huffman *h;
    792 	uint8_t *decomp;
    793 	size_t decomp_size;
    794 	int error, i;
    795 
    796 	if (!(sc->sc_flags & UDL_DECOMPRDY)) {
    797 		error = firmware_load("udl", "udl-decomp", &decomp,
    798 		    &decomp_size);
    799 		if (error != 0) {
    800 			aprint_error_dev(sc->sc_dev,
    801 			    "error %d, could not read decomp table %s!\n",
    802 			    error, "udl-decomp");
    803 			return;
    804 		}
    805 		udl_cmd_add_decomptable(sc, decomp, decomp_size);
    806 		firmware_free(decomp, decomp_size);
    807 		if (udl_cmd_send(sc) != 0)
    808 			return;
    809 		sc->sc_flags |= UDL_DECOMPRDY;
    810 	}
    811 
    812 	if (!(sc->sc_flags & UDL_COMPRDY)) {
    813 		error = firmware_load("udl", "udl-comp", &sc->sc_huffman,
    814 		    &sc->sc_huffman_size);
    815 		if (error != 0) {
    816 			aprint_error_dev(sc->sc_dev,
    817 			    "error %d, could not read huffman table %s!\n",
    818 			    error, "udl-comp");
    819 			return;
    820 		}
    821 		h = (struct udl_huffman *)sc->sc_huffman;
    822 		for (i = 0; i < UDL_HUFFMAN_RECORDS; i++)
    823 			h[i].bit_pattern = be32toh(h[i].bit_pattern);
    824 		sc->sc_huffman_base = sc->sc_huffman + UDL_HUFFMAN_BASE;
    825 		sc->sc_flags |= UDL_COMPRDY;
    826 	}
    827 }
    828 
    829 static void
    830 udl_comp_unload(struct udl_softc *sc)
    831 {
    832 
    833 	if (sc->sc_flags & UDL_COMPRDY) {
    834 		firmware_free(sc->sc_huffman, sc->sc_huffman_size);
    835 		sc->sc_huffman = NULL;
    836 		sc->sc_huffman_size = 0;
    837 		sc->sc_flags &= ~UDL_COMPRDY;
    838 	}
    839 }
    840 
    841 static int
    842 udl_fbmem_alloc(struct udl_softc *sc)
    843 {
    844 
    845 	mutex_enter(&sc->sc_thread_mtx);
    846 	if (sc->sc_fbmem == NULL)
    847 		sc->sc_fbmem = kmem_zalloc(UDL_FBMEM_SIZE(sc), KM_SLEEP);
    848 	if (sc->sc_fbmem_prev == NULL)
    849 		sc->sc_fbmem_prev = kmem_zalloc(UDL_FBMEM_SIZE(sc), KM_SLEEP);
    850 	mutex_exit(&sc->sc_thread_mtx);
    851 
    852 	return 0;
    853 }
    854 
    855 static void
    856 udl_fbmem_free(struct udl_softc *sc)
    857 {
    858 
    859 	mutex_enter(&sc->sc_thread_mtx);
    860 	if (sc->sc_fbmem != NULL) {
    861 		kmem_free(sc->sc_fbmem, UDL_FBMEM_SIZE(sc));
    862 		sc->sc_fbmem = NULL;
    863 	}
    864 	if (sc->sc_fbmem_prev != NULL) {
    865 		kmem_free(sc->sc_fbmem_prev, UDL_FBMEM_SIZE(sc));
    866 		sc->sc_fbmem_prev = NULL;
    867 	}
    868 	mutex_exit(&sc->sc_thread_mtx);
    869 }
    870 
    871 static int
    872 udl_cmdq_alloc(struct udl_softc *sc)
    873 {
    874 	struct udl_cmdq *cmdq;
    875 	int i;
    876 
    877 	TAILQ_INIT(&sc->sc_freecmd);
    878 	TAILQ_INIT(&sc->sc_xfercmd);
    879 
    880 	for (i = 0; i < UDL_NCMDQ; i++) {
    881 		cmdq = &sc->sc_cmdq[i];
    882 
    883 		cmdq->cq_sc = sc;
    884 
    885 		int err = usbd_create_xfer(sc->sc_tx_pipeh,
    886 		    UDL_CMD_BUFFER_SIZE, 0, 0, &cmdq->cq_xfer);
    887 		if (err) {
    888 			aprint_error_dev(sc->sc_dev,
    889 			    "%s: can't allocate xfer handle!\n", __func__);
    890 			goto error;
    891 		}
    892 
    893 		cmdq->cq_buf = usbd_get_buffer(cmdq->cq_xfer);
    894 
    895 		TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmdq, cq_chain);
    896 	}
    897 
    898 	return 0;
    899 
    900  error:
    901 	udl_cmdq_free(sc);
    902 	return -1;
    903 }
    904 
    905 static void
    906 udl_cmdq_free(struct udl_softc *sc)
    907 {
    908 	struct udl_cmdq *cmdq;
    909 	int i;
    910 
    911 	for (i = 0; i < UDL_NCMDQ; i++) {
    912 		cmdq = &sc->sc_cmdq[i];
    913 
    914 		if (cmdq->cq_xfer != NULL) {
    915 			usbd_destroy_xfer(cmdq->cq_xfer);
    916 			cmdq->cq_xfer = NULL;
    917 			cmdq->cq_buf = NULL;
    918 		}
    919 	}
    920 }
    921 
    922 static struct udl_cmdq *
    923 udl_cmdq_get(struct udl_softc *sc)
    924 {
    925 	struct udl_cmdq *cmdq;
    926 
    927 	cmdq = TAILQ_FIRST(&sc->sc_freecmd);
    928 	if (cmdq != NULL) {
    929 		TAILQ_REMOVE(&sc->sc_freecmd, cmdq, cq_chain);
    930 		UDL_EVCNT_INCR(&sc->sc_ev_cmdq_get);
    931 	}
    932 
    933 	return cmdq;
    934 }
    935 
    936 static void
    937 udl_cmdq_put(struct udl_softc *sc, struct udl_cmdq *cmdq)
    938 {
    939 
    940 	TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmdq, cq_chain);
    941 	UDL_EVCNT_INCR(&sc->sc_ev_cmdq_put);
    942 }
    943 
    944 static void
    945 udl_cmdq_flush(struct udl_softc *sc)
    946 {
    947 
    948 	mutex_enter(&sc->sc_mtx);
    949 	while (TAILQ_FIRST(&sc->sc_xfercmd) != NULL)
    950 		cv_wait(&sc->sc_cv, &sc->sc_mtx);
    951 	mutex_exit(&sc->sc_mtx);
    952 }
    953 
    954 static void
    955 udl_cursor(void *cookie, int on, int row, int col)
    956 {
    957 	struct rasops_info *ri = cookie;
    958 	struct udl_softc *sc = ri->ri_hw;
    959 	int x, y, width, height;
    960 
    961 	if (ri->ri_flg & RI_CURSOR)
    962 		udl_restore_char(ri);
    963 
    964 	ri->ri_crow = row;
    965 	ri->ri_ccol = col;
    966 
    967 	if (on != 0) {
    968 		ri->ri_flg |= RI_CURSOR;
    969 
    970 		x = col * ri->ri_font->fontwidth;
    971 		y = row * ri->ri_font->fontheight;
    972 		width = ri->ri_font->fontwidth;
    973 		height = ri->ri_font->fontheight;
    974 
    975 		/* save the last character block to off-screen */
    976 		udl_copy_rect(sc, x, y, 0, sc->sc_offscreen, width, height);
    977 
    978 		/* draw cursor */
    979 		udl_fill_rect(sc, 0xffff, x, y, width, 1);
    980 		udl_fill_rect(sc, 0xffff, x, y + 1, 1, height - 2);
    981 		udl_fill_rect(sc, 0xffff, x + width - 1, y + 1, 1, height - 2);
    982 		udl_fill_rect(sc, 0xffff, x, y + height - 1, width, 1);
    983 
    984 		udl_cmd_send_async(sc);
    985 	} else
    986 		ri->ri_flg &= ~RI_CURSOR;
    987 }
    988 
    989 static void
    990 udl_putchar(void *cookie, int row, int col, u_int uc, long attr)
    991 {
    992 	struct rasops_info *ri = cookie;
    993 	struct udl_softc *sc = ri->ri_hw;
    994 	uint16_t rgb16[2];
    995 	int fg, bg, underline, x, y, width, height;
    996 
    997 	rasops_unpack_attr(attr, &fg, &bg, &underline);
    998 	rgb16[1] = (uint16_t)ri->ri_devcmap[fg];
    999 	rgb16[0] = (uint16_t)ri->ri_devcmap[bg];
   1000 
   1001 	x = col * ri->ri_font->fontwidth;
   1002 	y = row * ri->ri_font->fontheight;
   1003 	width = ri->ri_font->fontwidth;
   1004 	height = ri->ri_font->fontheight;
   1005 
   1006 	if (uc == ' ') {
   1007 		/*
   1008 		 * Writting a block for the space character instead rendering
   1009 		 * it from font bits is more slim.
   1010 		 */
   1011 		udl_fill_rect(sc, rgb16[0], x, y, width, height);
   1012 	} else {
   1013 		/* render a character from font bits */
   1014 		udl_draw_char(ri, rgb16, uc, x, y);
   1015 	}
   1016 
   1017 	if (underline != 0)
   1018 		udl_fill_rect(sc, rgb16[1], x, y + height - 1, width, 1);
   1019 
   1020 #if 0
   1021 	udl_cmd_send_async(sc);
   1022 #endif
   1023 }
   1024 
   1025 static void
   1026 udl_copycols(void *cookie, int row, int src, int dst, int num)
   1027 {
   1028 	struct rasops_info *ri = cookie;
   1029 	struct udl_softc *sc = ri->ri_hw;
   1030 	int sx, dx, y, width, height;
   1031 
   1032 	sx = src * ri->ri_font->fontwidth;
   1033 	dx = dst * ri->ri_font->fontwidth;
   1034 	y = row * ri->ri_font->fontheight;
   1035 	width = num * ri->ri_font->fontwidth;
   1036 	height = ri->ri_font->fontheight;
   1037 
   1038 	/* copy row block to off-screen first to fix overlay-copy problem */
   1039 	udl_copy_rect(sc, sx, y, 0, sc->sc_offscreen, width, height);
   1040 
   1041 	/* copy row block back from off-screen now */
   1042 	udl_copy_rect(sc, 0, sc->sc_offscreen, dx, y, width, height);
   1043 #if 0
   1044 	udl_cmd_send_async(sc);
   1045 #endif
   1046 }
   1047 
   1048 static void
   1049 udl_erasecols(void *cookie, int row, int col, int num, long attr)
   1050 {
   1051 	struct rasops_info *ri = cookie;
   1052 	struct udl_softc *sc = ri->ri_hw;
   1053 	uint16_t rgb16;
   1054 	int fg, bg, x, y, width, height;
   1055 
   1056 	rasops_unpack_attr(attr, &fg, &bg, NULL);
   1057 	rgb16 = (uint16_t)ri->ri_devcmap[bg];
   1058 
   1059 	x = col * ri->ri_font->fontwidth;
   1060 	y = row * ri->ri_font->fontheight;
   1061 	width = num * ri->ri_font->fontwidth;
   1062 	height = ri->ri_font->fontheight;
   1063 
   1064 	udl_fill_rect(sc, rgb16, x, y, width, height);
   1065 #if 0
   1066 	udl_cmd_send_async(sc);
   1067 #endif
   1068 }
   1069 
   1070 static void
   1071 udl_copyrows(void *cookie, int src, int dst, int num)
   1072 {
   1073 	struct rasops_info *ri = cookie;
   1074 	struct udl_softc *sc = ri->ri_hw;
   1075 	int sy, ey, dy, width, height;
   1076 
   1077 	width = ri->ri_emuwidth;
   1078 	height = ri->ri_font->fontheight;
   1079 
   1080 	if (dst < src) {
   1081 		sy = src * height;
   1082 		ey = (src + num) * height;
   1083 		dy = dst * height;
   1084 
   1085 		while (sy < ey) {
   1086 			udl_copy_rect(sc, 0, sy, 0, dy, width, height);
   1087 			sy += height;
   1088 			dy += height;
   1089 		}
   1090 	} else {
   1091 		sy = (src + num) * height;
   1092 		ey = src * height;
   1093 		dy = (dst + num) * height;
   1094 
   1095 		while (sy > ey) {
   1096 			sy -= height;
   1097 			dy -= height;
   1098 			udl_copy_rect(sc, 0, sy, 0, dy, width, height);
   1099 		}
   1100 	}
   1101 #if 0
   1102 	udl_cmd_send_async(sc);
   1103 #endif
   1104 }
   1105 
   1106 static void
   1107 udl_eraserows(void *cookie, int row, int num, long attr)
   1108 {
   1109 	struct rasops_info *ri = cookie;
   1110 	struct udl_softc *sc = ri->ri_hw;
   1111 	uint16_t rgb16;
   1112 	int fg, bg, y, width, height;
   1113 
   1114 	rasops_unpack_attr(attr, &fg, &bg, NULL);
   1115 	rgb16 = (uint16_t)ri->ri_devcmap[bg];
   1116 
   1117 	y = row * ri->ri_font->fontheight;
   1118 	width = ri->ri_emuwidth;
   1119 	height = num * ri->ri_font->fontheight;
   1120 
   1121 	udl_fill_rect(sc, rgb16, 0, y, width, height);
   1122 #if 0
   1123 	udl_cmd_send_async(sc);
   1124 #endif
   1125 }
   1126 
   1127 static void
   1128 udl_restore_char(struct rasops_info *ri)
   1129 {
   1130 	struct udl_softc *sc = ri->ri_hw;
   1131 	int x, y, width, height;
   1132 
   1133 	x = ri->ri_ccol * ri->ri_font->fontwidth;
   1134 	y = ri->ri_crow * ri->ri_font->fontheight;
   1135 	width = ri->ri_font->fontwidth;
   1136 	height = ri->ri_font->fontheight;
   1137 
   1138 	/* restore the last saved character from off-screen */
   1139 	udl_copy_rect(sc, 0, sc->sc_offscreen, x, y, width, height);
   1140 }
   1141 
   1142 static void
   1143 udl_draw_char(struct rasops_info *ri, uint16_t *rgb16, u_int uc, int x, int y)
   1144 {
   1145 	struct udl_softc *sc = ri->ri_hw;
   1146 	struct wsdisplay_font *font = ri->ri_font;
   1147 	uint32_t fontbits;
   1148 	uint16_t pixels[32];
   1149 	uint8_t *fontbase;
   1150 	int i, soff, eoff;
   1151 
   1152 	soff = y * sc->sc_width + x;
   1153 	eoff = (y + font->fontheight) * sc->sc_width + x;
   1154 	fontbase = (uint8_t *)font->data + (uc - font->firstchar) *
   1155 	    ri->ri_fontscale;
   1156 
   1157 	while (soff < eoff) {
   1158 		fontbits = 0;
   1159 		switch (font->stride) {
   1160 		case 4:
   1161 			fontbits |= fontbase[3];
   1162 			/* FALLTHROUGH */
   1163 		case 3:
   1164 			fontbits |= fontbase[2] << 8;
   1165 			/* FALLTHROUGH */
   1166 		case 2:
   1167 			fontbits |= fontbase[1] << 16;
   1168 			/* FALLTHROUGH */
   1169 		case 1:
   1170 			fontbits |= fontbase[0] << 24;
   1171 		}
   1172 		fontbase += font->stride;
   1173 
   1174 		for (i = 0; i < font->fontwidth; i++) {
   1175 			pixels[i] = rgb16[(fontbits >> 31) & 1];
   1176 			fontbits <<= 1;
   1177 		}
   1178 
   1179 		udl_draw_line(sc, pixels, soff, font->fontwidth);
   1180 		soff += sc->sc_width;
   1181 	}
   1182 }
   1183 
   1184 static void
   1185 udl_copy_rect(struct udl_softc *sc, int sx, int sy, int dx, int dy, int width,
   1186     int height)
   1187 {
   1188 	int sbase, soff, ebase, eoff, dbase, doff, width_cur;
   1189 
   1190 	sbase = sy * sc->sc_width;
   1191 	ebase = (sy + height) * sc->sc_width;
   1192 	dbase = dy * sc->sc_width;
   1193 
   1194 	while (width > 0) {
   1195 		soff = sbase + sx;
   1196 		eoff = ebase + sx;
   1197 		doff = dbase + dx;
   1198 
   1199 		if (width >= UDL_CMD_WIDTH_MAX)
   1200 			width_cur = UDL_CMD_WIDTH_MAX;
   1201 		else
   1202 			width_cur = width;
   1203 
   1204 		while (soff < eoff) {
   1205 			udl_copy_line(sc, soff, doff, width_cur);
   1206 			soff += sc->sc_width;
   1207 			doff += sc->sc_width;
   1208 		}
   1209 
   1210 		sx += width_cur;
   1211 		dx += width_cur;
   1212 		width -= width_cur;
   1213 	}
   1214 }
   1215 
   1216 static void
   1217 udl_fill_rect(struct udl_softc *sc, uint16_t rgb16, int x, int y, int width,
   1218     int height)
   1219 {
   1220 	int sbase, soff, ebase, eoff, width_cur;
   1221 
   1222 	sbase = y * sc->sc_width;
   1223 	ebase = (y + height) * sc->sc_width;
   1224 
   1225 	while (width > 0) {
   1226 		soff = sbase + x;
   1227 		eoff = ebase + x;
   1228 
   1229 		if (width >= UDL_CMD_WIDTH_MAX)
   1230 			width_cur = UDL_CMD_WIDTH_MAX;
   1231 		else
   1232 			width_cur = width;
   1233 
   1234 		while (soff < eoff) {
   1235 			udl_fill_line(sc, rgb16, soff, width_cur);
   1236 			soff += sc->sc_width;
   1237 		}
   1238 
   1239 		x += width_cur;
   1240 		width -= width_cur;
   1241 	}
   1242 }
   1243 
   1244 #ifdef notyet
   1245 static void
   1246 udl_draw_rect(struct udl_softc *sc, struct udl_ioctl_damage *d)
   1247 {
   1248 	int sbase, soff, ebase, eoff, x, y, width, width_cur, height;
   1249 
   1250 	x = d->x1;
   1251 	y = d->y1;
   1252 	width = d->x2 - d->x1;
   1253 	height = d->y2 - d->y1;
   1254 	sbase = y * sc->sc_width;
   1255 	ebase = (y + height) * sc->sc_width;
   1256 
   1257 	while (width > 0) {
   1258 		soff = sbase + x;
   1259 		eoff = ebase + x;
   1260 
   1261 		if (width >= UDL_CMD_WIDTH_MAX)
   1262 			width_cur = UDL_CMD_WIDTH_MAX;
   1263 		else
   1264 			width_cur = width;
   1265 
   1266 		while (soff < eoff) {
   1267 			udl_draw_line(sc, (uint16_t *)sc->sc_fbmem + soff,
   1268 			    soff, width_cur);
   1269 			soff += sc->sc_width;
   1270 		}
   1271 
   1272 		x += width_cur;
   1273 		width -= width_cur;
   1274 	}
   1275 
   1276 	udl_cmd_send_async(sc);
   1277 }
   1278 
   1279 static void
   1280 udl_draw_rect_comp(struct udl_softc *sc, struct udl_ioctl_damage *d)
   1281 {
   1282 	int soff, eoff, x, y, width, height;
   1283 
   1284 	x = d->x1;
   1285 	y = d->y1;
   1286 	width = d->x2 - d->x1;
   1287 	height = d->y2 - d->y1;
   1288 	soff = y * sc->sc_width + x;
   1289 	eoff = (y + height) * sc->sc_width + x;
   1290 
   1291 	udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
   1292 	sc->sc_cmd_cblen = 4;
   1293 
   1294 	while (soff < eoff) {
   1295 		udl_draw_line_comp(sc, (uint16_t *)sc->sc_fbmem + soff, soff,
   1296 		    width);
   1297 		soff += sc->sc_width;
   1298 	}
   1299 
   1300 	udl_cmd_send_async(sc);
   1301 }
   1302 #endif
   1303 
   1304 static inline void
   1305 udl_copy_line(struct udl_softc *sc, int soff, int doff, int width)
   1306 {
   1307 
   1308 	if (__predict_false((UDL_CMD_BUFSIZE(sc) + UDL_CMD_COPY_SIZE + 2) >
   1309 	    UDL_CMD_BUFFER_SIZE))
   1310 		udl_cmd_send_async(sc);
   1311 
   1312 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_COPY16);
   1313 	udl_cmd_add_4(sc, ((doff * 2) << 8) | (width & 0xff));
   1314 
   1315 	udl_cmd_add_3(sc, soff * 2);
   1316 }
   1317 
   1318 static inline void
   1319 udl_fill_line(struct udl_softc *sc, uint16_t rgb16, int off, int width)
   1320 {
   1321 
   1322 	if (__predict_false((UDL_CMD_BUFSIZE(sc) + UDL_CMD_FILL_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_RLE16);
   1327 	udl_cmd_add_4(sc, ((off * 2) << 8) | (width & 0xff));
   1328 
   1329 	udl_cmd_add_1(sc, width);
   1330 	udl_cmd_add_2(sc, rgb16);
   1331 }
   1332 
   1333 static inline void
   1334 udl_draw_line(struct udl_softc *sc, uint16_t *buf, int off, int width)
   1335 {
   1336 
   1337 	if (__predict_false(
   1338 	    (UDL_CMD_BUFSIZE(sc) + UDL_CMD_DRAW_SIZE(width) + 2) >
   1339 	    UDL_CMD_BUFFER_SIZE))
   1340 		udl_cmd_send_async(sc);
   1341 
   1342 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_WRITE16);
   1343 	udl_cmd_add_4(sc, ((off * 2) << 8) | (width & 0xff));
   1344 
   1345 	udl_cmd_add_buf(sc, buf, width);
   1346 }
   1347 
   1348 #ifdef notyet
   1349 static inline int
   1350 udl_cmd_add_buf_comp(struct udl_softc *sc, uint16_t *buf, int width)
   1351 {
   1352 	struct udl_huffman *h;
   1353 	uint16_t *startp, *endp;
   1354 	uint32_t bit_pattern;
   1355 	uint16_t prev;
   1356 	int16_t diff;
   1357 	uint8_t bit_count, bit_pos, bit_rem, curlen;
   1358 
   1359 	startp = buf;
   1360 	if (width >= UDL_CMD_WIDTH_MAX)
   1361 		endp = buf + UDL_CMD_WIDTH_MAX;
   1362 	else
   1363 		endp = buf + width;
   1364 
   1365 	prev = bit_pos = *sc->sc_cmd_buf = 0;
   1366 	bit_rem = 8;
   1367 
   1368 	/*
   1369 	 * Generate a sub-block with maximal 256 pixels compressed data.
   1370 	 */
   1371 	while (buf < endp) {
   1372 		/* get difference between current and previous pixel */
   1373 		diff = *buf - prev;
   1374 
   1375 		/* get the huffman difference bit sequence */
   1376 		h = (struct udl_huffman *)sc->sc_huffman_base + diff;
   1377 		bit_count = h->bit_count;
   1378 		bit_pattern = h->bit_pattern;
   1379 
   1380 		curlen = (bit_pos + bit_count + 7) / 8;
   1381 		if (__predict_false((sc->sc_cmd_cblen + curlen + 1) >
   1382 		    UDL_CMD_COMP_BLOCK_SIZE))
   1383 			break;
   1384 
   1385 		/* generate one pixel compressed data */
   1386 		while (bit_count >= bit_rem) {
   1387 			*sc->sc_cmd_buf++ |=
   1388 			    (bit_pattern & ((1 << bit_rem) - 1)) << bit_pos;
   1389 			*sc->sc_cmd_buf = 0;
   1390 			sc->sc_cmd_cblen++;
   1391 			bit_count -= bit_rem;
   1392 			bit_pattern >>= bit_rem;
   1393 			bit_pos = 0;
   1394 			bit_rem = 8;
   1395 		}
   1396 
   1397 		if (bit_count > 0) {
   1398 			*sc->sc_cmd_buf |=
   1399 			    (bit_pattern & ((1 << bit_count) - 1)) << bit_pos;
   1400 			bit_pos += bit_count;
   1401 			bit_rem -= bit_count;
   1402 		}
   1403 
   1404 		prev = *buf++;
   1405 	}
   1406 
   1407 	/*
   1408 	 * If we have bits left in our last byte, round up to the next
   1409 	 * byte, so we don't overwrite them.
   1410 	 */
   1411 	if (bit_pos > 0) {
   1412 		sc->sc_cmd_buf++;
   1413 		sc->sc_cmd_cblen++;
   1414 	}
   1415 
   1416 	/* return how many pixels we have compressed */
   1417 	return buf - startp;
   1418 }
   1419 
   1420 static inline void
   1421 udl_draw_line_comp(struct udl_softc *sc, uint16_t *buf, int off, int width)
   1422 {
   1423 	uint8_t *widthp;
   1424 	int width_cur;
   1425 
   1426 	while (width > 0) {
   1427 		if (__predict_false(
   1428 		    (sc->sc_cmd_cblen + UDL_CMD_COMP_MIN_SIZE + 1) >
   1429 		    UDL_CMD_COMP_BLOCK_SIZE)) {
   1430 			if (UDL_CMD_BUFSIZE(sc) < UDL_CMD_COMP_THRESHOLD) {
   1431 				while (sc->sc_cmd_cblen <
   1432 				    UDL_CMD_COMP_BLOCK_SIZE) {
   1433 					*sc->sc_cmd_buf++ = 0;
   1434 					sc->sc_cmd_cblen++;
   1435 				}
   1436 			} else
   1437 				udl_cmd_send_async(sc);
   1438 			udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
   1439 			sc->sc_cmd_cblen = 4;
   1440 		}
   1441 
   1442 		udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) |
   1443 		    (UDL_BULK_CMD_FB_WRITE16 | UDL_BULK_CMD_FB_COMP));
   1444 		udl_cmd_add_4(sc, (off * 2) << 8);
   1445 
   1446 		widthp = sc->sc_cmd_buf - 1;
   1447 
   1448 		sc->sc_cmd_cblen += UDL_CMD_HEADER_SIZE;
   1449 
   1450 		width_cur = udl_cmd_add_buf_comp(sc, buf, width);
   1451 
   1452 		*widthp = width_cur;
   1453 		buf += width_cur;
   1454 		off += width_cur;
   1455 		width -= width_cur;
   1456 	}
   1457 }
   1458 #endif
   1459 
   1460 static int
   1461 udl_cmd_send(struct udl_softc *sc)
   1462 {
   1463 	struct udl_cmdq *cmdq;
   1464 	usbd_status error;
   1465 	uint32_t len;
   1466 
   1467 	cmdq = sc->sc_cmd_cur;
   1468 
   1469 	/* mark end of command stack */
   1470 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_EOC);
   1471 
   1472 	len = UDL_CMD_BUFSIZE(sc);
   1473 
   1474 	/* do xfer */
   1475 	error = usbd_bulk_transfer(cmdq->cq_xfer, sc->sc_tx_pipeh, 0,
   1476 	    USBD_NO_TIMEOUT, cmdq->cq_buf, &len);
   1477 
   1478 	UDL_CMD_BUFINIT(sc);
   1479 
   1480 	if (error != USBD_NORMAL_COMPLETION) {
   1481 		aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__,
   1482 		    usbd_errstr(error));
   1483 		return -1;
   1484 	}
   1485 
   1486 	return 0;
   1487 }
   1488 
   1489 static void
   1490 udl_cmd_send_async(struct udl_softc *sc)
   1491 {
   1492 	struct udl_cmdq *cmdq;
   1493 	usbd_status error;
   1494 	uint32_t len;
   1495 
   1496 #if 1
   1497 	/*
   1498 	 * XXX
   1499 	 * All tty ops for wsemul are called with tty_lock spin mutex held,
   1500 	 * so we can't call cv_wait(9) here to acquire a free buffer.
   1501 	 * For now, all commands and data for wsemul ops are discarded
   1502 	 * if there is no free command buffer, and then screen text might
   1503 	 * be corrupted on large scroll ops etc.
   1504 	 *
   1505 	 * Probably we have to reorganize the giant tty_lock mutex, or
   1506 	 * change wsdisplay APIs (especially wsdisplaystart()) to return
   1507 	 * a number of actually handled characters as OpenBSD does, but
   1508 	 * the latter one requires whole API changes around rasops(9) etc.
   1509 	 */
   1510 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1511 		if (TAILQ_FIRST(&sc->sc_freecmd) == NULL) {
   1512 			UDL_CMD_BUFINIT(sc);
   1513 			return;
   1514 		}
   1515 	}
   1516 #endif
   1517 
   1518 	cmdq = sc->sc_cmd_cur;
   1519 
   1520 	/* mark end of command stack */
   1521 	udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_EOC);
   1522 
   1523 	len = UDL_CMD_BUFSIZE(sc);
   1524 
   1525 	/* do xfer */
   1526 	mutex_enter(&sc->sc_mtx);
   1527 	usbd_setup_xfer(cmdq->cq_xfer, cmdq, cmdq->cq_buf,
   1528 	    len, 0, USBD_NO_TIMEOUT, udl_cmd_send_async_cb);
   1529 	error = usbd_transfer(cmdq->cq_xfer);
   1530 	if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
   1531 		aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__,
   1532 		    usbd_errstr(error));
   1533 		mutex_exit(&sc->sc_mtx);
   1534 		goto end;
   1535 	}
   1536 
   1537 	TAILQ_INSERT_TAIL(&sc->sc_xfercmd, cmdq, cq_chain);
   1538 	cmdq = udl_cmdq_get(sc);
   1539 	mutex_exit(&sc->sc_mtx);
   1540 	while (cmdq == NULL) {
   1541 		int err;
   1542 		UDL_EVCNT_INCR(&sc->sc_ev_cmdq_wait);
   1543 		mutex_enter(&sc->sc_mtx);
   1544 		err = cv_timedwait(&sc->sc_cv, &sc->sc_mtx,
   1545 		    mstohz(100) /* XXX is this needed? */);
   1546 		if (err != 0) {
   1547 			DPRINTF(("%s: %s: cv timeout (error = %d)\n",
   1548 			    device_xname(sc->sc_dev), __func__, err));
   1549 			UDL_EVCNT_INCR(&sc->sc_ev_cmdq_timeout);
   1550 		}
   1551 		cmdq = udl_cmdq_get(sc);
   1552 		mutex_exit(&sc->sc_mtx);
   1553 	}
   1554 	sc->sc_cmd_cur = cmdq;
   1555  end:
   1556 	UDL_CMD_BUFINIT(sc);
   1557 }
   1558 
   1559 static void
   1560 udl_cmd_send_async_cb(struct usbd_xfer *xfer, void * priv,
   1561     usbd_status status)
   1562 {
   1563 	struct udl_cmdq *cmdq = priv;
   1564 	struct udl_softc *sc = cmdq->cq_sc;
   1565 
   1566 	if (status != USBD_NORMAL_COMPLETION) {
   1567 		aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__,
   1568 		    usbd_errstr(status));
   1569 
   1570 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1571 			return;
   1572 		if (status == USBD_STALLED)
   1573 			usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
   1574 	}
   1575 
   1576 	mutex_enter(&sc->sc_mtx);
   1577 	TAILQ_REMOVE(&sc->sc_xfercmd, cmdq, cq_chain);
   1578 	udl_cmdq_put(sc, cmdq);
   1579 
   1580 	/* signal xfer op that sleeps for a free xfer buffer */
   1581 	cv_signal(&sc->sc_cv);
   1582 	mutex_exit(&sc->sc_mtx);
   1583 }
   1584 
   1585 static int
   1586 udl_ctrl_msg(struct udl_softc *sc, uint8_t rt, uint8_t r, uint16_t index,
   1587     uint16_t value, uint8_t *buf, uint16_t len)
   1588 {
   1589 	usb_device_request_t req;
   1590 	usbd_status error;
   1591 
   1592 	req.bmRequestType = rt;
   1593 	req.bRequest = r;
   1594 	USETW(req.wIndex, index);
   1595 	USETW(req.wValue, value);
   1596 	USETW(req.wLength, len);
   1597 
   1598 	error = usbd_do_request(sc->sc_udev, &req, buf);
   1599 	if (error != USBD_NORMAL_COMPLETION) {
   1600 		aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__,
   1601 		    usbd_errstr(error));
   1602 		return -1;
   1603 	}
   1604 
   1605 	return 0;
   1606 }
   1607 
   1608 static int
   1609 udl_init(struct udl_softc *sc)
   1610 {
   1611 	static uint8_t key[16] = {
   1612 	    0x57, 0xcd, 0xdc, 0xa7, 0x1c, 0x88, 0x5e, 0x15,
   1613 	    0x60, 0xfe, 0xc6, 0x97, 0x16, 0x3d, 0x47, 0xf2
   1614 	};
   1615 	uint8_t status[4], val;
   1616 
   1617 	if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_STATUS,
   1618 	    0x0000, 0x0000, status, sizeof(status)) != 0)
   1619 		return -1;
   1620 
   1621 	if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_1,
   1622 	    0xc484, 0x0000, &val, 1) != 0)
   1623 		return -1;
   1624 
   1625 	val = 1;
   1626 	if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_WRITE_1,
   1627 	    0xc41f, 0x0000, &val, 1) != 0)
   1628 		return -1;
   1629 
   1630 	if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_SET_KEY,
   1631 	    0x0000, 0x0000, key, sizeof(key)) != 0)
   1632 		return -1;
   1633 
   1634 	val = 0;
   1635 	if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_WRITE_1,
   1636 	    0xc40b, 0x0000, &val, 1) != 0)
   1637 		return -1;
   1638 
   1639 	return 0;
   1640 }
   1641 
   1642 static void
   1643 udl_read_edid(struct udl_softc *sc)
   1644 {
   1645 	uint8_t buf[64], edid[128];
   1646 	int offset;
   1647 
   1648 	memset(&sc->sc_ei, 0, sizeof(struct edid_info));
   1649 
   1650 	offset = 0;
   1651 	if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID,
   1652 	    0x00a1, (offset << 8), buf, 64) != 0)
   1653 		return;
   1654 	if (buf[0] != 0)
   1655 		return;
   1656 	memcpy(&edid[offset], &buf[1], 63);
   1657 	offset += 63;
   1658 
   1659 	if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID,
   1660 	    0x00a1, (offset << 8), buf, 64) != 0)
   1661 		return;
   1662 	if (buf[0] != 0)
   1663 		return;
   1664 	memcpy(&edid[offset], &buf[1], 63);
   1665 	offset += 63;
   1666 
   1667 	if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID,
   1668 	    0x00a1, (offset << 8), buf, 3) != 0)
   1669 		return;
   1670 	if (buf[0] != 0)
   1671 		return;
   1672 	memcpy(&edid[offset], &buf[1], 2);
   1673 
   1674 	if (edid_parse(edid, &sc->sc_ei) == 0) {
   1675 #ifdef UDL_DEBUG
   1676 		edid_print(&sc->sc_ei);
   1677 #endif
   1678 	}
   1679 }
   1680 
   1681 static void
   1682 udl_set_address(struct udl_softc *sc, int start16, int stride16, int start8,
   1683     int stride8)
   1684 {
   1685 	udl_reg_write_1(sc, UDL_REG_SYNC, 0x00);
   1686 	udl_reg_write_3(sc, UDL_REG_ADDR_START16, start16);
   1687 	udl_reg_write_3(sc, UDL_REG_ADDR_STRIDE16, stride16);
   1688 	udl_reg_write_3(sc, UDL_REG_ADDR_START8, start8);
   1689 	udl_reg_write_3(sc, UDL_REG_ADDR_STRIDE8, stride8);
   1690 	udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
   1691 }
   1692 
   1693 static void
   1694 udl_blank(struct udl_softc *sc, int blank)
   1695 {
   1696 
   1697 	if (blank != 0)
   1698 		udl_reg_write_1(sc, UDL_REG_BLANK, UDL_REG_BLANK_ON);
   1699 	else
   1700 		udl_reg_write_1(sc, UDL_REG_BLANK, UDL_REG_BLANK_OFF);
   1701 	udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
   1702 }
   1703 
   1704 static uint16_t
   1705 udl_lfsr(uint16_t count)
   1706 {
   1707 	uint16_t val = 0xffff;
   1708 
   1709 	while (count > 0) {
   1710 		val = (uint16_t)(val << 1) | ((uint16_t)(
   1711 		    (uint16_t)(val << 0) ^
   1712 		    (uint16_t)(val << 11) ^
   1713 		    (uint16_t)(val << 13) ^
   1714 		    (uint16_t)(val << 14)
   1715 		    ) >> 15);
   1716 		count--;
   1717 	}
   1718 
   1719 	return val;
   1720 }
   1721 
   1722 static int
   1723 udl_set_resolution(struct udl_softc *sc, const struct videomode *vmp)
   1724 {
   1725 	uint16_t val;
   1726 	int start16, stride16, start8, stride8;
   1727 
   1728 	/* set video memory offsets */
   1729 	start16 = 0;
   1730 	stride16 = sc->sc_width * 2;
   1731 	start8 = stride16 * sc->sc_height;
   1732 	stride8 = sc->sc_width;
   1733 	udl_set_address(sc, start16, stride16, start8, stride8);
   1734 
   1735 	/* write resolution values */
   1736 	udl_reg_write_1(sc, UDL_REG_SYNC, 0x00);
   1737 	udl_reg_write_1(sc, UDL_REG_COLORDEPTH, UDL_REG_COLORDEPTH_16);
   1738 	val = vmp->htotal - vmp->hsync_start;
   1739 	udl_reg_write_2(sc, UDL_REG_XDISPLAYSTART, udl_lfsr(val));
   1740 	val += vmp->hdisplay;
   1741 	udl_reg_write_2(sc, UDL_REG_XDISPLAYEND, udl_lfsr(val));
   1742 	val = vmp->vtotal - vmp->vsync_start;
   1743 	udl_reg_write_2(sc, UDL_REG_YDISPLAYSTART, udl_lfsr(val));
   1744 	val += vmp->vdisplay;
   1745 	udl_reg_write_2(sc, UDL_REG_YDISPLAYEND, udl_lfsr(val));
   1746 	val = vmp->htotal - 1;
   1747 	udl_reg_write_2(sc, UDL_REG_XENDCOUNT, udl_lfsr(val));
   1748 	val = vmp->hsync_end - vmp->hsync_start + 1;
   1749 	if (vmp->flags & VID_PHSYNC) {
   1750 		udl_reg_write_2(sc, UDL_REG_HSYNCSTART, udl_lfsr(1));
   1751 		udl_reg_write_2(sc, UDL_REG_HSYNCEND, udl_lfsr(val));
   1752 	} else {
   1753 		udl_reg_write_2(sc, UDL_REG_HSYNCSTART, udl_lfsr(val));
   1754 		udl_reg_write_2(sc, UDL_REG_HSYNCEND, udl_lfsr(1));
   1755 	}
   1756 	val = vmp->hdisplay;
   1757 	udl_reg_write_2(sc, UDL_REG_HPIXELS, val);
   1758 	val = vmp->vtotal;
   1759 	udl_reg_write_2(sc, UDL_REG_YENDCOUNT, udl_lfsr(val));
   1760 	val = vmp->vsync_end - vmp->vsync_start;
   1761 	if (vmp->flags & VID_PVSYNC) {
   1762 		udl_reg_write_2(sc, UDL_REG_VSYNCSTART, udl_lfsr(0));
   1763 		udl_reg_write_2(sc, UDL_REG_VSYNCEND, udl_lfsr(val));
   1764 	} else {
   1765 		udl_reg_write_2(sc, UDL_REG_VSYNCSTART, udl_lfsr(val));
   1766 		udl_reg_write_2(sc, UDL_REG_VSYNCEND, udl_lfsr(0));
   1767 	}
   1768 	val = vmp->vdisplay;
   1769 	udl_reg_write_2(sc, UDL_REG_VPIXELS, val);
   1770 	val = vmp->dot_clock / 5;
   1771 	udl_reg_write_2(sc, UDL_REG_PIXELCLOCK5KHZ, bswap16(val));
   1772 	udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
   1773 
   1774 	if (udl_cmd_send(sc) != 0)
   1775 		return -1;
   1776 
   1777 	/* clear screen */
   1778 	udl_fill_rect(sc, 0, 0, 0, sc->sc_width, sc->sc_height);
   1779 
   1780 	if (udl_cmd_send(sc) != 0)
   1781 		return -1;
   1782 
   1783 	/* show framebuffer content */
   1784 	udl_blank(sc, 0);
   1785 
   1786 	if (udl_cmd_send(sc) != 0)
   1787 		return -1;
   1788 
   1789 	sc->sc_blank = WSDISPLAYIO_VIDEO_ON;
   1790 
   1791 	return 0;
   1792 }
   1793 
   1794 static const struct videomode *
   1795 udl_videomode_lookup(const char *name)
   1796 {
   1797 	int i;
   1798 
   1799 	for (i = 0; i < videomode_count; i++)
   1800 		if (strcmp(name, videomode_list[i].name) == 0)
   1801 			return &videomode_list[i];
   1802 
   1803 	return NULL;
   1804 }
   1805 
   1806 static void
   1807 udl_update_thread(void *v)
   1808 {
   1809 	struct udl_softc *sc = v;
   1810 	int stride;
   1811 #ifdef notyet
   1812 	bool update = false;
   1813 	int linecount, x, y;
   1814 	uint16_t *fb, *fbcopy;
   1815 	uint8_t *curfb;
   1816 #else
   1817 	uint16_t *fb;
   1818 	int offs;
   1819 #endif
   1820 
   1821 	mutex_enter(&sc->sc_thread_mtx);
   1822 
   1823 	for (;;) {
   1824 		stride = min(sc->sc_width, UDL_CMD_WIDTH_MAX - 8);
   1825 		if (sc->sc_dying == true) {
   1826 			mutex_exit(&sc->sc_thread_mtx);
   1827 			kthread_exit(0);
   1828 		}
   1829 
   1830 		if (sc->sc_thread_stop == true || sc->sc_fbmem == NULL)
   1831 			goto thread_wait;
   1832 
   1833 #ifdef notyet
   1834 		curfb = kmem_zalloc(UDL_FBMEM_SIZE(sc), KM_SLEEP);
   1835 		memcpy(curfb, sc->sc_fbmem, sc->sc_height * sc->sc_width * 2);
   1836 		fb = (uint16_t *)curfb;
   1837 		fbcopy = (uint16_t *)sc->sc_fbmem_prev;
   1838 		for (y = 0; y < sc->sc_height; y++) {
   1839 			linecount = 0;
   1840 			update = false;
   1841 			for (x = 0; x < sc->sc_width; x++) {
   1842 				if (linecount >= stride) {
   1843 					udl_draw_line(sc, &fb[y * sc->sc_width
   1844 					    + x - linecount], y * sc->sc_width
   1845 					    + x - linecount, linecount);
   1846 					linecount = 0;
   1847 					update = false;
   1848 				}
   1849 				if (fb[y * sc->sc_width + x] ^ fbcopy[y *
   1850 				    sc->sc_width + x]) {
   1851 					update = true;
   1852 					linecount ++;
   1853 				} else if (update == true) {
   1854 					udl_draw_line(sc, &fb[y * sc->sc_width
   1855 					    + x - linecount], y * sc->sc_width
   1856 					    + x - linecount, linecount);
   1857 					linecount = 0;
   1858 					update = false;
   1859 				}
   1860 			}
   1861 			if (linecount) {
   1862 				udl_draw_line(sc, &fb[y * sc->sc_width + x -
   1863 				    linecount], y * sc->sc_width  + x -
   1864 				    linecount, linecount);
   1865 			}
   1866 		}
   1867 		memcpy(sc->sc_fbmem_prev, curfb, sc->sc_height * sc->sc_width
   1868 		    * 2);
   1869 		kmem_free(curfb, UDL_FBMEM_SIZE(sc));
   1870 #else
   1871 		fb = (uint16_t *)sc->sc_fbmem;
   1872 		for (offs = 0; offs < sc->sc_height * sc->sc_width; offs += stride)
   1873 			udl_draw_line(sc, &fb[offs], offs, stride);
   1874 
   1875 #endif
   1876 
   1877 		kpause("udlslp", false, (40 * hz)/1000 + 1, &sc->sc_thread_mtx);
   1878 		continue;
   1879 
   1880 thread_wait:
   1881 		cv_wait(&sc->sc_thread_cv, &sc->sc_thread_mtx);
   1882 	}
   1883 }
   1884 
   1885 static inline void
   1886 udl_startstop(struct udl_softc *sc, bool stop)
   1887 {
   1888 	mutex_enter(&sc->sc_thread_mtx);
   1889 	sc->sc_thread_stop = stop;
   1890 	if (!stop)
   1891 		cv_broadcast(&sc->sc_thread_cv);
   1892 	mutex_exit(&sc->sc_thread_mtx);
   1893 }
   1894