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