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