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