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