xref: /freebsd/sys/dev/usb/video/udl.c (revision 26a222dc0c048fc071b548eadad7b80405a1b126)
1 /*	$OpenBSD: udl.c,v 1.81 2014/12/09 07:05:06 doug Exp $ */
2 /*	$FreeBSD$ */
3 
4 /*-
5  * Copyright (c) 2015 Hans Petter Selasky <hselasky@freebsd.org>
6  * Copyright (c) 2009 Marcus Glocker <mglocker@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 /*
22  * Driver for the "DisplayLink DL-120 / DL-160" graphic chips based on
23  * the reversed engineered specifications of Florian Echtler
24  * <floe@butterbrot.org>:
25  *
26  * 	http://floe.butterbrot.org/displaylink/doku.php
27  */
28 
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/callout.h>
32 #include <sys/conf.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/condvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 #include <sys/consio.h>
41 #include <sys/fbio.h>
42 
43 #include <dev/fb/fbreg.h>
44 #include <dev/syscons/syscons.h>
45 
46 #include <dev/videomode/videomode.h>
47 #include <dev/videomode/edidvar.h>
48 
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include "usbdevs.h"
53 
54 #include <dev/usb/video/udl.h>
55 
56 #include "fb_if.h"
57 
58 #undef DPRINTF
59 #undef DPRINTFN
60 #define	USB_DEBUG_VAR udl_debug
61 #include <dev/usb/usb_debug.h>
62 
63 static	SYSCTL_NODE(_hw_usb, OID_AUTO, udl, CTLFLAG_RW, 0, "USB UDL");
64 
65 #ifdef USB_DEBUG
66 static int udl_debug = 0;
67 
68 SYSCTL_INT(_hw_usb_udl, OID_AUTO, debug, CTLFLAG_RWTUN,
69     &udl_debug, 0, "Debug level");
70 #endif
71 
72 #define	UDL_FPS_MAX	60
73 #define	UDL_FPS_MIN	1
74 
75 static int udl_fps = 25;
76 SYSCTL_INT(_hw_usb_udl, OID_AUTO, fps, CTLFLAG_RWTUN,
77     &udl_fps, 0, "Frames Per Second, 1-60");
78 
79 /*
80  * Prototypes.
81  */
82 static usb_callback_t udl_bulk_write_callback;
83 
84 static device_probe_t udl_probe;
85 static device_attach_t udl_attach;
86 static device_detach_t udl_detach;
87 static fb_getinfo_t udl_fb_getinfo;
88 static fb_setblankmode_t udl_fb_setblankmode;
89 
90 static void udl_select_chip(struct udl_softc *, struct usb_attach_arg *);
91 static int udl_init_chip(struct udl_softc *);
92 static void udl_select_mode(struct udl_softc *);
93 static int udl_init_resolution(struct udl_softc *);
94 static void udl_fbmem_alloc(struct udl_softc *);
95 static int udl_cmd_write_buf_le16(struct udl_softc *, const uint8_t *, uint32_t, uint8_t, int);
96 static int udl_cmd_buf_copy_le16(struct udl_softc *, uint32_t, uint32_t, uint8_t, int);
97 static void udl_cmd_insert_int_1(struct udl_cmd_buf *, uint8_t);
98 static void udl_cmd_insert_int_3(struct udl_cmd_buf *, uint32_t);
99 static void udl_cmd_insert_buf_le16(struct udl_cmd_buf *, const uint8_t *, uint32_t);
100 static void udl_cmd_write_reg_1(struct udl_cmd_buf *, uint8_t, uint8_t);
101 static void udl_cmd_write_reg_3(struct udl_cmd_buf *, uint8_t, uint32_t);
102 static int udl_power_save(struct udl_softc *, int, int);
103 
104 static const struct usb_config udl_config[UDL_N_TRANSFER] = {
105 	[UDL_BULK_WRITE_0] = {
106 		.type = UE_BULK,
107 		.endpoint = UE_ADDR_ANY,
108 		.direction = UE_DIR_TX,
109 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
110 		.bufsize = UDL_CMD_MAX_DATA_SIZE * UDL_CMD_MAX_FRAMES,
111 		.callback = &udl_bulk_write_callback,
112 		.frames = UDL_CMD_MAX_FRAMES,
113 		.timeout = 5000,	/* 5 seconds */
114 	},
115 	[UDL_BULK_WRITE_1] = {
116 		.type = UE_BULK,
117 		.endpoint = UE_ADDR_ANY,
118 		.direction = UE_DIR_TX,
119 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
120 		.bufsize = UDL_CMD_MAX_DATA_SIZE * UDL_CMD_MAX_FRAMES,
121 		.callback = &udl_bulk_write_callback,
122 		.frames = UDL_CMD_MAX_FRAMES,
123 		.timeout = 5000,	/* 5 seconds */
124 	},
125 };
126 
127 /*
128  * Driver glue.
129  */
130 static devclass_t udl_devclass;
131 
132 static device_method_t udl_methods[] = {
133 	DEVMETHOD(device_probe, udl_probe),
134 	DEVMETHOD(device_attach, udl_attach),
135 	DEVMETHOD(device_detach, udl_detach),
136 	DEVMETHOD(fb_getinfo, udl_fb_getinfo),
137 	DEVMETHOD_END
138 };
139 
140 static driver_t udl_driver = {
141 	.name = "udl",
142 	.methods = udl_methods,
143 	.size = sizeof(struct udl_softc),
144 };
145 
146 DRIVER_MODULE(udl, uhub, udl_driver, udl_devclass, NULL, NULL);
147 MODULE_DEPEND(udl, usb, 1, 1, 1);
148 MODULE_DEPEND(udl, fbd, 1, 1, 1);
149 MODULE_DEPEND(udl, videomode, 1, 1, 1);
150 MODULE_VERSION(udl, 1);
151 
152 /*
153  * Matching devices.
154  */
155 static const STRUCT_USB_HOST_ID udl_devs[] = {
156 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD4300U, DL120)},
157 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD8000U, DL120)},
158 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_GUC2020, DL160)},
159 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LD220, DL165)},
160 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_VCUD60, DL160)},
161 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_DLDVI, DL160)},
162 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_VGA10, DL120)},
163 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_WSDVI, DLUNK)},
164 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_EC008, DL160)},
165 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_HPDOCK, DL160)},
166 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_NL571, DL160)},
167 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_M01061, DL195)},
168 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_NBDOCK, DL165)},
169 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_SWDVI, DLUNK)},
170 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_UM7X0, DL120)},
171 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_CONV, DL160)},
172 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_PLUGABLE, DL160)},
173 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LUM70, DL125)},
174 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_POLARIS2, DLUNK)},
175 	{USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LT1421, DLUNK)}
176 };
177 
178 static uint32_t
179 udl_get_fb_size(struct udl_softc *sc)
180 {
181 	unsigned i = sc->sc_cur_mode;
182 
183 	return ((uint32_t)udl_modes[i].hdisplay *
184 	    (uint32_t)udl_modes[i].vdisplay * 2);
185 }
186 
187 static uint32_t
188 udl_get_fb_width(struct udl_softc *sc)
189 {
190 	unsigned i = sc->sc_cur_mode;
191 
192 	return (udl_modes[i].hdisplay);
193 }
194 
195 static uint32_t
196 udl_get_fb_height(struct udl_softc *sc)
197 {
198 	unsigned i = sc->sc_cur_mode;
199 
200 	return (udl_modes[i].vdisplay);
201 }
202 
203 static uint32_t
204 udl_get_fb_hz(struct udl_softc *sc)
205 {
206 	unsigned i = sc->sc_cur_mode;
207 
208 	return (udl_modes[i].hz);
209 }
210 
211 static void
212 udl_callout(void *arg)
213 {
214 	struct udl_softc *sc = arg;
215 	const uint32_t max = udl_get_fb_size(sc);
216 	int fps;
217 
218 	if (sc->sc_power_save == 0) {
219 		fps = udl_fps;
220 
221 	  	/* figure out number of frames per second */
222 		if (fps < UDL_FPS_MIN)
223 			fps = UDL_FPS_MIN;
224 		else if (fps > UDL_FPS_MAX)
225 			fps = UDL_FPS_MAX;
226 
227 		if (sc->sc_sync_off >= max)
228 			sc->sc_sync_off = 0;
229 		usbd_transfer_start(sc->sc_xfer[UDL_BULK_WRITE_0]);
230 		usbd_transfer_start(sc->sc_xfer[UDL_BULK_WRITE_1]);
231 	} else {
232 		fps = 1;
233 	}
234 	callout_reset(&sc->sc_callout, hz / fps, &udl_callout, sc);
235 }
236 
237 static int
238 udl_probe(device_t dev)
239 {
240 	struct usb_attach_arg *uaa = device_get_ivars(dev);
241 
242 	if (uaa->usb_mode != USB_MODE_HOST)
243 		return (ENXIO);
244 	if (uaa->info.bConfigIndex != 0)
245 		return (ENXIO);
246 	if (uaa->info.bIfaceIndex != 0)
247 		return (ENXIO);
248 
249 	return (usbd_lookup_id_by_uaa(udl_devs, sizeof(udl_devs), uaa));
250 }
251 
252 static int
253 udl_attach(device_t dev)
254 {
255 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
256 	struct sysctl_oid *tree = device_get_sysctl_tree(dev);
257 	struct udl_softc *sc = device_get_softc(dev);
258 	struct usb_attach_arg *uaa = device_get_ivars(dev);
259 	int error;
260 	int i;
261 
262 	device_set_usb_desc(dev);
263 
264 	mtx_init(&sc->sc_mtx, "UDL lock", NULL, MTX_DEF);
265 	cv_init(&sc->sc_cv, "UDLCV");
266 	callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
267 	sc->sc_udev = uaa->device;
268 
269 	error = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex,
270 	    sc->sc_xfer, udl_config, UDL_N_TRANSFER, sc, &sc->sc_mtx);
271 
272 	if (error) {
273 		DPRINTF("usbd_transfer_setup error=%s\n", usbd_errstr(error));
274 		goto detach;
275 	}
276 	usbd_xfer_set_priv(sc->sc_xfer[UDL_BULK_WRITE_0], &sc->sc_xfer_head[0]);
277 	usbd_xfer_set_priv(sc->sc_xfer[UDL_BULK_WRITE_1], &sc->sc_xfer_head[1]);
278 
279 	TAILQ_INIT(&sc->sc_xfer_head[0]);
280 	TAILQ_INIT(&sc->sc_xfer_head[1]);
281 	TAILQ_INIT(&sc->sc_cmd_buf_free);
282 	TAILQ_INIT(&sc->sc_cmd_buf_pending);
283 
284 	sc->sc_def_chip = -1;
285 	sc->sc_chip = USB_GET_DRIVER_INFO(uaa);
286 	sc->sc_def_mode = -1;
287 	sc->sc_cur_mode = UDL_MAX_MODES;
288 
289 	/* Allow chip ID to be overwritten */
290 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "chipid_force",
291 	    CTLFLAG_RWTUN, &sc->sc_def_chip, 0, "chip ID");
292 
293 	/* Export current chip ID */
294 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "chipid",
295 	    CTLFLAG_RD, &sc->sc_chip, 0, "chip ID");
296 
297 	if (sc->sc_def_chip > -1 && sc->sc_def_chip <= DLMAX) {
298 		device_printf(dev, "Forcing chip ID to 0x%04x\n", sc->sc_def_chip);
299 		sc->sc_chip = sc->sc_def_chip;
300 	}
301 	/*
302 	 * The product might have more than one chip
303 	 */
304 	if (sc->sc_chip == DLUNK)
305 		udl_select_chip(sc, uaa);
306 
307 	for (i = 0; i != UDL_CMD_MAX_BUFFERS; i++) {
308 		struct udl_cmd_buf *cb = &sc->sc_cmd_buf_temp[i];
309 
310 		TAILQ_INSERT_TAIL(&sc->sc_cmd_buf_free, cb, entry);
311 	}
312 
313 	/*
314 	 * Initialize chip.
315 	 */
316 	error = udl_init_chip(sc);
317 	if (error != USB_ERR_NORMAL_COMPLETION)
318 		goto detach;
319 
320 	/*
321 	 * Select edid mode.
322 	 */
323 	udl_select_mode(sc);
324 
325 	/* Allow default mode to be overwritten */
326 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "mode_force",
327 	    CTLFLAG_RWTUN, &sc->sc_def_mode, 0, "mode");
328 
329 	/* Export current mode */
330 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "mode",
331 	    CTLFLAG_RD, &sc->sc_cur_mode, 0, "mode");
332 
333 	i = sc->sc_def_mode;
334 	if (i > -1 && i < UDL_MAX_MODES) {
335 		if (udl_modes[i].chip <= sc->sc_chip) {
336 			device_printf(dev, "Forcing mode to %d\n", i);
337 			sc->sc_cur_mode = i;
338 		}
339 	}
340 	/* Printout current mode */
341 	device_printf(dev, "Mode selected %dx%d @ %dHz\n",
342 	    (int)udl_get_fb_width(sc),
343 	    (int)udl_get_fb_height(sc),
344 	    (int)udl_get_fb_hz(sc));
345 
346 	udl_init_resolution(sc);
347 
348 	/* Allocate frame buffer */
349 	udl_fbmem_alloc(sc);
350 
351 	UDL_LOCK(sc);
352 	udl_callout(sc);
353 	UDL_UNLOCK(sc);
354 
355 	sc->sc_fb_info.fb_name = device_get_nameunit(dev);
356 	sc->sc_fb_info.fb_size = sc->sc_fb_size;
357 	sc->sc_fb_info.fb_bpp = 16;
358 	sc->sc_fb_info.fb_depth = 16;
359 	sc->sc_fb_info.fb_width = udl_get_fb_width(sc);
360 	sc->sc_fb_info.fb_height = udl_get_fb_height(sc);
361 	sc->sc_fb_info.fb_stride = sc->sc_fb_info.fb_width * 2;
362 	sc->sc_fb_info.fb_pbase = 0;
363 	sc->sc_fb_info.fb_vbase = (uintptr_t)sc->sc_fb_addr;
364 	sc->sc_fb_info.fb_priv = sc;
365 	sc->sc_fb_info.setblankmode = &udl_fb_setblankmode;
366 
367 	sc->sc_fbdev = device_add_child(dev, "fbd", -1);
368 	if (sc->sc_fbdev == NULL)
369 		goto detach;
370 	if (device_probe_and_attach(sc->sc_fbdev) != 0)
371 		goto detach;
372 
373 	return (0);
374 
375 detach:
376 	udl_detach(dev);
377 
378 	return (ENXIO);
379 }
380 
381 static int
382 udl_detach(device_t dev)
383 {
384 	struct udl_softc *sc = device_get_softc(dev);
385 
386 	if (sc->sc_fbdev != NULL) {
387 		device_t bdev;
388 
389 		bdev = sc->sc_fbdev;
390 		sc->sc_fbdev = NULL;
391 		device_detach(bdev);
392 		device_delete_child(dev, bdev);
393 	}
394 	UDL_LOCK(sc);
395 	sc->sc_gone = 1;
396 	callout_stop(&sc->sc_callout);
397 	UDL_UNLOCK(sc);
398 
399 	usbd_transfer_unsetup(sc->sc_xfer, UDL_N_TRANSFER);
400 
401 	callout_drain(&sc->sc_callout);
402 
403 	mtx_destroy(&sc->sc_mtx);
404 	cv_destroy(&sc->sc_cv);
405 
406 	/*
407 	 * Free framebuffer memory, if any.
408 	 */
409 	free(sc->sc_fb_addr, M_DEVBUF);
410 	free(sc->sc_fb_copy, M_DEVBUF);
411 
412 	return (0);
413 }
414 
415 static struct fb_info *
416 udl_fb_getinfo(device_t dev)
417 {
418 	struct udl_softc *sc = device_get_softc(dev);
419 
420 	return (&sc->sc_fb_info);
421 }
422 
423 static int
424 udl_fb_setblankmode(void *arg, int mode)
425 {
426 	struct udl_softc *sc = arg;
427 
428 	switch (mode) {
429 	case V_DISPLAY_ON:
430 		udl_power_save(sc, 1, M_WAITOK);
431 		break;
432 	case V_DISPLAY_BLANK:
433 		udl_power_save(sc, 1, M_WAITOK);
434 		if (sc->sc_fb_addr != 0) {
435 			const uint32_t max = udl_get_fb_size(sc);
436 
437 			memset((void *)sc->sc_fb_addr, 0, max);
438 		}
439 		break;
440 	case V_DISPLAY_STAND_BY:
441 	case V_DISPLAY_SUSPEND:
442 		udl_power_save(sc, 0, M_WAITOK);
443 		break;
444 	}
445 	return (0);
446 }
447 
448 static struct udl_cmd_buf *
449 udl_cmd_buf_alloc_locked(struct udl_softc *sc, int flags)
450 {
451 	struct udl_cmd_buf *cb;
452 
453 	while ((cb = TAILQ_FIRST(&sc->sc_cmd_buf_free)) == NULL) {
454 		if (flags != M_WAITOK)
455 			break;
456 		cv_wait(&sc->sc_cv, &sc->sc_mtx);
457 	}
458 	if (cb != NULL) {
459 		TAILQ_REMOVE(&sc->sc_cmd_buf_free, cb, entry);
460 		cb->off = 0;
461 	}
462 	return (cb);
463 }
464 
465 static struct udl_cmd_buf *
466 udl_cmd_buf_alloc(struct udl_softc *sc, int flags)
467 {
468 	struct udl_cmd_buf *cb;
469 
470 	UDL_LOCK(sc);
471 	cb = udl_cmd_buf_alloc_locked(sc, flags);
472 	UDL_UNLOCK(sc);
473 	return (cb);
474 }
475 
476 static void
477 udl_cmd_buf_send(struct udl_softc *sc, struct udl_cmd_buf *cb)
478 {
479 	UDL_LOCK(sc);
480 	if (sc->sc_gone) {
481 		TAILQ_INSERT_TAIL(&sc->sc_cmd_buf_free, cb, entry);
482 	} else {
483 		/* mark end of command stack */
484 		udl_cmd_insert_int_1(cb, UDL_BULK_SOC);
485 		udl_cmd_insert_int_1(cb, UDL_BULK_CMD_EOC);
486 
487 		TAILQ_INSERT_TAIL(&sc->sc_cmd_buf_pending, cb, entry);
488 		usbd_transfer_start(sc->sc_xfer[UDL_BULK_WRITE_0]);
489 		usbd_transfer_start(sc->sc_xfer[UDL_BULK_WRITE_1]);
490 	}
491 	UDL_UNLOCK(sc);
492 }
493 
494 static struct udl_cmd_buf *
495 udl_fb_synchronize_locked(struct udl_softc *sc)
496 {
497 	const uint32_t max = udl_get_fb_size(sc);
498 
499 	/* check if framebuffer is not ready */
500 	if (sc->sc_fb_addr == NULL ||
501 	    sc->sc_fb_copy == NULL)
502 		return (NULL);
503 
504 	while (sc->sc_sync_off < max) {
505 		uint32_t delta = max - sc->sc_sync_off;
506 
507 		if (delta > UDL_CMD_MAX_PIXEL_COUNT * 2)
508 			delta = UDL_CMD_MAX_PIXEL_COUNT * 2;
509 		if (bcmp(sc->sc_fb_addr + sc->sc_sync_off, sc->sc_fb_copy + sc->sc_sync_off, delta) != 0) {
510 			struct udl_cmd_buf *cb;
511 
512 			cb = udl_cmd_buf_alloc_locked(sc, M_NOWAIT);
513 			if (cb == NULL)
514 				goto done;
515 			memcpy(sc->sc_fb_copy + sc->sc_sync_off,
516 			    sc->sc_fb_addr + sc->sc_sync_off, delta);
517 			udl_cmd_insert_int_1(cb, UDL_BULK_SOC);
518 			udl_cmd_insert_int_1(cb, UDL_BULK_CMD_FB_WRITE | UDL_BULK_CMD_FB_WORD);
519 			udl_cmd_insert_int_3(cb, sc->sc_sync_off);
520 			udl_cmd_insert_int_1(cb, delta / 2);
521 			udl_cmd_insert_buf_le16(cb, sc->sc_fb_copy + sc->sc_sync_off, delta);
522 			sc->sc_sync_off += delta;
523 			return (cb);
524 		} else {
525 			sc->sc_sync_off += delta;
526 		}
527 	}
528 done:
529 	return (NULL);
530 }
531 
532 static void
533 udl_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
534 {
535 	struct udl_softc *sc = usbd_xfer_softc(xfer);
536 	struct udl_cmd_head *phead = usbd_xfer_get_priv(xfer);
537 	struct udl_cmd_buf *cb;
538 	unsigned i;
539 
540 	switch (USB_GET_STATE(xfer)) {
541 	case USB_ST_TRANSFERRED:
542 		TAILQ_CONCAT(&sc->sc_cmd_buf_free, phead, entry);
543 	case USB_ST_SETUP:
544 tr_setup:
545 		for (i = 0; i != UDL_CMD_MAX_FRAMES; i++) {
546 			cb = TAILQ_FIRST(&sc->sc_cmd_buf_pending);
547 			if (cb == NULL) {
548 				cb = udl_fb_synchronize_locked(sc);
549 				if (cb == NULL)
550 					break;
551 			} else {
552 				TAILQ_REMOVE(&sc->sc_cmd_buf_pending, cb, entry);
553 			}
554 			TAILQ_INSERT_TAIL(phead, cb, entry);
555 			usbd_xfer_set_frame_data(xfer, i, cb->buf, cb->off);
556 		}
557 		if (i != 0) {
558 			usbd_xfer_set_frames(xfer, i);
559 			usbd_transfer_submit(xfer);
560 		}
561 		break;
562 	default:
563 		TAILQ_CONCAT(&sc->sc_cmd_buf_free, phead, entry);
564 		if (error != USB_ERR_CANCELLED) {
565 			/* try clear stall first */
566 			usbd_xfer_set_stall(xfer);
567 			goto tr_setup;
568 		}
569 		break;
570 	}
571 	/* wakeup any waiters */
572 	cv_signal(&sc->sc_cv);
573 }
574 
575 static int
576 udl_power_save(struct udl_softc *sc, int on, int flags)
577 {
578 	struct udl_cmd_buf *cb;
579 
580 	/* get new buffer */
581 	cb = udl_cmd_buf_alloc(sc, flags);
582 	if (cb == NULL)
583 		return (EAGAIN);
584 
585 	DPRINTF("screen %s\n", on ? "ON" : "OFF");
586 
587 	sc->sc_power_save = on ? 0 : 1;
588 
589 	if (on)
590 		udl_cmd_write_reg_1(cb, UDL_REG_SCREEN, UDL_REG_SCREEN_ON);
591 	else
592 		udl_cmd_write_reg_1(cb, UDL_REG_SCREEN, UDL_REG_SCREEN_OFF);
593 
594 	udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0xff);
595 	udl_cmd_buf_send(sc, cb);
596 	return (0);
597 }
598 
599 static int
600 udl_ctrl_msg(struct udl_softc *sc, uint8_t rt, uint8_t r,
601     uint16_t index, uint16_t value, uint8_t *buf, size_t len)
602 {
603 	usb_device_request_t req;
604 	int error;
605 
606 	req.bmRequestType = rt;
607 	req.bRequest = r;
608 	USETW(req.wIndex, index);
609 	USETW(req.wValue, value);
610 	USETW(req.wLength, len);
611 
612 	error = usbd_do_request_flags(sc->sc_udev, NULL,
613 	    &req, buf, 0, NULL, USB_DEFAULT_TIMEOUT);
614 
615 	DPRINTF("%s\n", usbd_errstr(error));
616 
617 	return (error);
618 }
619 
620 static int
621 udl_poll(struct udl_softc *sc, uint32_t *buf)
622 {
623 	uint32_t lbuf;
624 	int error;
625 
626 	error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE,
627 	    UDL_CTRL_CMD_POLL, 0x0000, 0x0000, (uint8_t *)&lbuf, sizeof(lbuf));
628 	if (error == USB_ERR_NORMAL_COMPLETION)
629 		*buf = le32toh(lbuf);
630 	return (error);
631 }
632 
633 static int
634 udl_read_1(struct udl_softc *sc, uint16_t addr, uint8_t *buf)
635 {
636 	uint8_t lbuf[1];
637 	int error;
638 
639 	error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE,
640 	    UDL_CTRL_CMD_READ_1, addr, 0x0000, lbuf, 1);
641 	if (error == USB_ERR_NORMAL_COMPLETION)
642 		*buf = *(uint8_t *)lbuf;
643 	return (error);
644 }
645 
646 static int
647 udl_write_1(struct udl_softc *sc, uint16_t addr, uint8_t buf)
648 {
649 	int error;
650 
651 	error = udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE,
652 	    UDL_CTRL_CMD_WRITE_1, addr, 0x0000, &buf, 1);
653 	return (error);
654 }
655 
656 static int
657 udl_read_edid(struct udl_softc *sc, uint8_t *buf)
658 {
659 	uint8_t lbuf[64];
660 	uint16_t offset;
661 	int error;
662 
663 	offset = 0;
664 
665 	error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE,
666 	    UDL_CTRL_CMD_READ_EDID, 0x00a1, (offset << 8), lbuf, 64);
667 	if (error != USB_ERR_NORMAL_COMPLETION)
668 		goto fail;
669 	bcopy(lbuf + 1, buf + offset, 63);
670 	offset += 63;
671 
672 	error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE,
673 	    UDL_CTRL_CMD_READ_EDID, 0x00a1, (offset << 8), lbuf, 64);
674 	if (error != USB_ERR_NORMAL_COMPLETION)
675 		goto fail;
676 	bcopy(lbuf + 1, buf + offset, 63);
677 	offset += 63;
678 
679 	error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE,
680 	    UDL_CTRL_CMD_READ_EDID, 0x00a1, (offset << 8), lbuf, 3);
681 	if (error != USB_ERR_NORMAL_COMPLETION)
682 		goto fail;
683 	bcopy(lbuf + 1, buf + offset, 2);
684 fail:
685 	return (error);
686 }
687 
688 static uint8_t
689 udl_lookup_mode(uint16_t hdisplay, uint16_t vdisplay, uint8_t hz,
690     uint16_t chip, uint32_t clock)
691 {
692 	uint8_t idx;
693 
694 	/*
695 	 * Check first if we have a matching mode with pixelclock
696 	 */
697 	for (idx = 0; idx != UDL_MAX_MODES; idx++) {
698 		if ((udl_modes[idx].hdisplay == hdisplay) &&
699 		    (udl_modes[idx].vdisplay == vdisplay) &&
700 		    (udl_modes[idx].clock == clock) &&
701 		    (udl_modes[idx].chip <= chip)) {
702 			return (idx);
703 		}
704 	}
705 
706 	/*
707 	 * If not, check for matching mode with update frequency
708 	 */
709 	for (idx = 0; idx != UDL_MAX_MODES; idx++) {
710 		if ((udl_modes[idx].hdisplay == hdisplay) &&
711 		    (udl_modes[idx].vdisplay == vdisplay) &&
712 		    (udl_modes[idx].hz == hz) &&
713 		    (udl_modes[idx].chip <= chip)) {
714 			return (idx);
715 		}
716 	}
717 	return (idx);
718 }
719 
720 static void
721 udl_select_chip(struct udl_softc *sc, struct usb_attach_arg *uaa)
722 {
723 	const char *pserial;
724 
725 	pserial = usb_get_serial(uaa->device);
726 
727 	sc->sc_chip = DL120;
728 
729 	if ((uaa->info.idVendor == USB_VENDOR_DISPLAYLINK) &&
730 	    (uaa->info.idProduct == USB_PRODUCT_DISPLAYLINK_WSDVI)) {
731 
732 		/*
733 		 * WS Tech DVI is DL120 or DL160. All deviced uses the
734 		 * same revision (0.04) so iSerialNumber must be used
735 		 * to determin which chip it is.
736 		 */
737 
738 		if (strlen(pserial) > 7) {
739 			if (strncmp(pserial, "0198-13", 7) == 0)
740 				sc->sc_chip = DL160;
741 		}
742 		DPRINTF("iSerialNumber (%s) used to select chip (%d)\n",
743 		    pserial, sc->sc_chip);
744 	}
745 	if ((uaa->info.idVendor == USB_VENDOR_DISPLAYLINK) &&
746 	    (uaa->info.idProduct == USB_PRODUCT_DISPLAYLINK_SWDVI)) {
747 
748 		/*
749 		 * SUNWEIT DVI is DL160, DL125, DL165 or DL195. Major revision
750 		 * can be used to differ between DL1x0 and DL1x5. Minor to
751 		 * differ between DL1x5. iSerialNumber seems not to be uniqe.
752 		 */
753 
754 		sc->sc_chip = DL160;
755 
756 		if (uaa->info.bcdDevice >= 0x100) {
757 			sc->sc_chip = DL165;
758 			if (uaa->info.bcdDevice == 0x104)
759 				sc->sc_chip = DL195;
760 			if (uaa->info.bcdDevice == 0x108)
761 				sc->sc_chip = DL125;
762 		}
763 		DPRINTF("bcdDevice (%02x) used to select chip (%d)\n",
764 		    uaa->info.bcdDevice, sc->sc_chip);
765 	}
766 }
767 
768 static int
769 udl_set_enc_key(struct udl_softc *sc, uint8_t *buf, uint8_t len)
770 {
771 	int error;
772 
773 	error = udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE,
774 	    UDL_CTRL_CMD_SET_KEY, 0x0000, 0x0000, buf, len);
775 	return (error);
776 }
777 
778 static void
779 udl_fbmem_alloc(struct udl_softc *sc)
780 {
781 	uint32_t size;
782 
783 	size = udl_get_fb_size(sc);
784 	size = round_page(size);
785 
786 	/*
787 	 * It is assumed that allocations above PAGE_SIZE bytes will
788 	 * be PAGE_SIZE aligned for use with mmap()
789 	 */
790 	sc->sc_fb_addr = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
791 	sc->sc_fb_copy = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
792 	sc->sc_fb_size = size;
793 }
794 
795 static void
796 udl_cmd_insert_int_1(struct udl_cmd_buf *cb, uint8_t value)
797 {
798 
799 	cb->buf[cb->off] = value;
800 	cb->off += 1;
801 }
802 
803 #if 0
804 static void
805 udl_cmd_insert_int_2(struct udl_cmd_buf *cb, uint16_t value)
806 {
807 	uint16_t lvalue;
808 
809 	lvalue = htobe16(value);
810 	bcopy(&lvalue, cb->buf + cb->off, 2);
811 
812 	cb->off += 2;
813 }
814 
815 #endif
816 
817 static void
818 udl_cmd_insert_int_3(struct udl_cmd_buf *cb, uint32_t value)
819 {
820 	uint32_t lvalue;
821 
822 #if BYTE_ORDER == BIG_ENDIAN
823 	lvalue = htobe32(value) << 8;
824 #else
825 	lvalue = htobe32(value) >> 8;
826 #endif
827 	bcopy(&lvalue, cb->buf + cb->off, 3);
828 
829 	cb->off += 3;
830 }
831 
832 #if 0
833 static void
834 udl_cmd_insert_int_4(struct udl_cmd_buf *cb, uint32_t value)
835 {
836 	uint32_t lvalue;
837 
838 	lvalue = htobe32(value);
839 	bcopy(&lvalue, cb->buf + cb->off, 4);
840 
841 	cb->off += 4;
842 }
843 
844 #endif
845 
846 static void
847 udl_cmd_insert_buf_le16(struct udl_cmd_buf *cb, const uint8_t *buf, uint32_t len)
848 {
849 	uint32_t x;
850 
851 	for (x = 0; x != len; x += 2) {
852 		/* byte swap from little endian to big endian */
853 		cb->buf[cb->off + x + 0] = buf[x + 1];
854 		cb->buf[cb->off + x + 1] = buf[x + 0];
855 	}
856 	cb->off += len;
857 }
858 
859 static void
860 udl_cmd_write_reg_1(struct udl_cmd_buf *cb, uint8_t reg, uint8_t val)
861 {
862 
863 	udl_cmd_insert_int_1(cb, UDL_BULK_SOC);
864 	udl_cmd_insert_int_1(cb, UDL_BULK_CMD_REG_WRITE_1);
865 	udl_cmd_insert_int_1(cb, reg);
866 	udl_cmd_insert_int_1(cb, val);
867 }
868 
869 static void
870 udl_cmd_write_reg_3(struct udl_cmd_buf *cb, uint8_t reg, uint32_t val)
871 {
872 
873 	udl_cmd_write_reg_1(cb, reg + 0, (val >> 16) & 0xff);
874 	udl_cmd_write_reg_1(cb, reg + 1, (val >> 8) & 0xff);
875 	udl_cmd_write_reg_1(cb, reg + 2, (val >> 0) & 0xff);
876 }
877 
878 static int
879 udl_init_chip(struct udl_softc *sc)
880 {
881 	uint32_t ui32;
882 	uint8_t ui8;
883 	int error;
884 
885 	error = udl_poll(sc, &ui32);
886 	if (error != USB_ERR_NORMAL_COMPLETION)
887 		return (error);
888 	DPRINTF("poll=0x%08x\n", ui32);
889 
890 	/* Some products may use later chip too */
891 	switch (ui32 & 0xff) {
892 	case 0xf1:			/* DL1x5 */
893 		switch (sc->sc_chip) {
894 		case DL120:
895 			sc->sc_chip = DL125;
896 			break;
897 		case DL160:
898 			sc->sc_chip = DL165;
899 			break;
900 		}
901 		break;
902 	}
903 	DPRINTF("chip 0x%04x\n", sc->sc_chip);
904 
905 	error = udl_read_1(sc, 0xc484, &ui8);
906 	if (error != USB_ERR_NORMAL_COMPLETION)
907 		return (error);
908 	DPRINTF("read 0x%02x from 0xc484\n", ui8);
909 
910 	error = udl_write_1(sc, 0xc41f, 0x01);
911 	if (error != USB_ERR_NORMAL_COMPLETION)
912 		return (error);
913 	DPRINTF("write 0x01 to 0xc41f\n");
914 
915 	error = udl_read_edid(sc, sc->sc_edid);
916 	if (error != USB_ERR_NORMAL_COMPLETION)
917 		return (error);
918 	DPRINTF("read EDID\n");
919 
920 	error = udl_set_enc_key(sc, __DECONST(void *, udl_null_key_1),
921 	    sizeof(udl_null_key_1));
922 	if (error != USB_ERR_NORMAL_COMPLETION)
923 		return (error);
924 	DPRINTF("set encryption key\n");
925 
926 	error = udl_write_1(sc, 0xc40b, 0x00);
927 	if (error != USB_ERR_NORMAL_COMPLETION)
928 		return (error);
929 	DPRINTF("write 0x00 to 0xc40b\n");
930 
931 	return (USB_ERR_NORMAL_COMPLETION);
932 }
933 
934 static void
935 udl_init_fb_offsets(struct udl_cmd_buf *cb, uint32_t start16, uint32_t stride16,
936     uint32_t start8, uint32_t stride8)
937 {
938 	udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0x00);
939 	udl_cmd_write_reg_3(cb, UDL_REG_ADDR_START16, start16);
940 	udl_cmd_write_reg_3(cb, UDL_REG_ADDR_STRIDE16, stride16);
941 	udl_cmd_write_reg_3(cb, UDL_REG_ADDR_START8, start8);
942 	udl_cmd_write_reg_3(cb, UDL_REG_ADDR_STRIDE8, stride8);
943 	udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0xff);
944 }
945 
946 static int
947 udl_init_resolution(struct udl_softc *sc)
948 {
949 	const uint32_t max = udl_get_fb_size(sc);
950 	const uint8_t *buf = udl_modes[sc->sc_cur_mode].mode;
951 	struct udl_cmd_buf *cb;
952 	uint32_t delta;
953 	uint32_t i;
954 	int error;
955 
956 	/* get new buffer */
957 	cb = udl_cmd_buf_alloc(sc, M_WAITOK);
958 	if (cb == NULL)
959 		return (EAGAIN);
960 
961 	/* write resolution values and set video memory offsets */
962 	udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0x00);
963 	for (i = 0; i < UDL_MODE_SIZE; i++)
964 		udl_cmd_write_reg_1(cb, i, buf[i]);
965 	udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0xff);
966 
967 	udl_init_fb_offsets(cb, 0x000000, 0x000a00, 0x555555, 0x000500);
968 	udl_cmd_buf_send(sc, cb);
969 
970 	/* fill screen with black color */
971 	for (i = 0; i < max; i += delta) {
972 		static const uint8_t udl_black[UDL_CMD_MAX_PIXEL_COUNT * 2] __aligned(4);
973 
974 		delta = max - i;
975 		if (delta > UDL_CMD_MAX_PIXEL_COUNT * 2)
976 			delta = UDL_CMD_MAX_PIXEL_COUNT * 2;
977 		if (i == 0)
978 			error = udl_cmd_write_buf_le16(sc, udl_black, i, delta / 2, M_WAITOK);
979 		else
980 			error = udl_cmd_buf_copy_le16(sc, 0, i, delta / 2, M_WAITOK);
981 		if (error)
982 			return (error);
983 	}
984 
985 	/* get new buffer */
986 	cb = udl_cmd_buf_alloc(sc, M_WAITOK);
987 	if (cb == NULL)
988 		return (EAGAIN);
989 
990 	/* show framebuffer content */
991 	udl_cmd_write_reg_1(cb, UDL_REG_SCREEN, UDL_REG_SCREEN_ON);
992 	udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0xff);
993 	udl_cmd_buf_send(sc, cb);
994 	return (0);
995 }
996 
997 static void
998 udl_select_mode(struct udl_softc *sc)
999 {
1000 	struct udl_mode mode;
1001 	int index = UDL_MAX_MODES;
1002 	int i;
1003 
1004 	/* try to get the preferred mode from EDID */
1005 	edid_parse(sc->sc_edid, &sc->sc_edid_info);
1006 #ifdef USB_DEBUG
1007 	edid_print(&sc->sc_edid_info);
1008 #endif
1009 	if (sc->sc_edid_info.edid_preferred_mode != NULL) {
1010 		mode.hz =
1011 		    (sc->sc_edid_info.edid_preferred_mode->dot_clock * 1000) /
1012 		    (sc->sc_edid_info.edid_preferred_mode->htotal *
1013 		    sc->sc_edid_info.edid_preferred_mode->vtotal);
1014 		mode.clock =
1015 		    sc->sc_edid_info.edid_preferred_mode->dot_clock / 10;
1016 		mode.hdisplay =
1017 		    sc->sc_edid_info.edid_preferred_mode->hdisplay;
1018 		mode.vdisplay =
1019 		    sc->sc_edid_info.edid_preferred_mode->vdisplay;
1020 		index = udl_lookup_mode(mode.hdisplay, mode.vdisplay, mode.hz,
1021 		    sc->sc_chip, mode.clock);
1022 		sc->sc_cur_mode = index;
1023 	} else {
1024 		DPRINTF("no preferred mode found!\n");
1025 	}
1026 
1027 	if (index == UDL_MAX_MODES) {
1028 		DPRINTF("no mode line found for %dx%d @ %dHz!\n",
1029 		    mode.hdisplay, mode.vdisplay, mode.hz);
1030 
1031 		i = 0;
1032 		while (i < sc->sc_edid_info.edid_nmodes) {
1033 			mode.hz =
1034 			    (sc->sc_edid_info.edid_modes[i].dot_clock * 1000) /
1035 			    (sc->sc_edid_info.edid_modes[i].htotal *
1036 			    sc->sc_edid_info.edid_modes[i].vtotal);
1037 			mode.clock =
1038 			    sc->sc_edid_info.edid_modes[i].dot_clock / 10;
1039 			mode.hdisplay =
1040 			    sc->sc_edid_info.edid_modes[i].hdisplay;
1041 			mode.vdisplay =
1042 			    sc->sc_edid_info.edid_modes[i].vdisplay;
1043 			index = udl_lookup_mode(mode.hdisplay, mode.vdisplay,
1044 			    mode.hz, sc->sc_chip, mode.clock);
1045 			if (index < UDL_MAX_MODES)
1046 				if ((sc->sc_cur_mode == UDL_MAX_MODES) ||
1047 				    (index > sc->sc_cur_mode))
1048 					sc->sc_cur_mode = index;
1049 			i++;
1050 		}
1051 	}
1052 	/*
1053 	 * If no mode found use default.
1054 	 */
1055 	if (sc->sc_cur_mode == UDL_MAX_MODES)
1056 		sc->sc_cur_mode = udl_lookup_mode(800, 600, 60, sc->sc_chip, 0);
1057 }
1058 
1059 static int
1060 udl_cmd_write_buf_le16(struct udl_softc *sc, const uint8_t *buf, uint32_t off,
1061     uint8_t pixels, int flags)
1062 {
1063 	struct udl_cmd_buf *cb;
1064 
1065 	cb = udl_cmd_buf_alloc(sc, flags);
1066 	if (cb == NULL)
1067 		return (EAGAIN);
1068 
1069 	udl_cmd_insert_int_1(cb, UDL_BULK_SOC);
1070 	udl_cmd_insert_int_1(cb, UDL_BULK_CMD_FB_WRITE | UDL_BULK_CMD_FB_WORD);
1071 	udl_cmd_insert_int_3(cb, off);
1072 	udl_cmd_insert_int_1(cb, pixels);
1073 	udl_cmd_insert_buf_le16(cb, buf, 2 * pixels);
1074 	udl_cmd_buf_send(sc, cb);
1075 
1076 	return (0);
1077 }
1078 
1079 static int
1080 udl_cmd_buf_copy_le16(struct udl_softc *sc, uint32_t src, uint32_t dst,
1081     uint8_t pixels, int flags)
1082 {
1083 	struct udl_cmd_buf *cb;
1084 
1085 	cb = udl_cmd_buf_alloc(sc, flags);
1086 	if (cb == NULL)
1087 		return (EAGAIN);
1088 
1089 	udl_cmd_insert_int_1(cb, UDL_BULK_SOC);
1090 	udl_cmd_insert_int_1(cb, UDL_BULK_CMD_FB_COPY | UDL_BULK_CMD_FB_WORD);
1091 	udl_cmd_insert_int_3(cb, dst);
1092 	udl_cmd_insert_int_1(cb, pixels);
1093 	udl_cmd_insert_int_3(cb, src);
1094 	udl_cmd_buf_send(sc, cb);
1095 
1096 	return (0);
1097 }
1098