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