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