1 /* $OpenBSD: udl.c,v 1.81 2014/12/09 07:05:06 doug Exp $ */
5 * Copyright (c) 2015 Hans Petter Selasky <hselasky@freebsd.org>
6 * Copyright (c) 2009 Marcus Glocker <mglocker@openbsd.org>
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.
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.
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>:
26 * http://floe.butterbrot.org/displaylink/doku.php
29 #include <sys/param.h>
31 #include <sys/callout.h>
33 #include <sys/kernel.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>
43 #include <dev/fb/fbreg.h>
44 #include <dev/syscons/syscons.h>
46 #include <dev/videomode/videomode.h>
47 #include <dev/videomode/edidvar.h>
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
54 #include <dev/usb/video/udl.h>
60 #define USB_DEBUG_VAR udl_debug
61 #include <dev/usb/usb_debug.h>
63 static SYSCTL_NODE(_hw_usb, OID_AUTO, udl, CTLFLAG_RW, 0, "USB UDL");
66 static int udl_debug = 0;
68 SYSCTL_INT(_hw_usb_udl, OID_AUTO, debug, CTLFLAG_RWTUN,
69 &udl_debug, 0, "Debug level");
72 #define UDL_FPS_MAX 60
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");
79 static struct mtx udl_buffer_mtx;
80 static struct udl_buffer_head udl_buffer_head;
82 MALLOC_DEFINE(M_USB_DL, "USB", "USB DisplayLink");
87 static usb_callback_t udl_bulk_write_callback;
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;
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);
109 static const struct usb_config udl_config[UDL_N_TRANSFER] = {
110 [UDL_BULK_WRITE_0] = {
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 */
120 [UDL_BULK_WRITE_1] = {
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 */
135 static devclass_t udl_devclass;
137 static device_method_t udl_methods[] = {
138 DEVMETHOD(device_probe, udl_probe),
139 DEVMETHOD(device_attach, udl_attach),
140 DEVMETHOD(device_detach, udl_detach),
141 DEVMETHOD(fb_getinfo, udl_fb_getinfo),
145 static driver_t udl_driver = {
147 .methods = udl_methods,
148 .size = sizeof(struct udl_softc),
151 DRIVER_MODULE(udl, uhub, udl_driver, udl_devclass, NULL, NULL);
152 MODULE_DEPEND(udl, usb, 1, 1, 1);
153 MODULE_DEPEND(udl, fbd, 1, 1, 1);
154 MODULE_DEPEND(udl, videomode, 1, 1, 1);
155 MODULE_VERSION(udl, 1);
160 static const STRUCT_USB_HOST_ID udl_devs[] = {
161 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD4300U, DL120)},
162 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD8000U, DL120)},
163 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_GUC2020, DL160)},
164 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LD220, DL165)},
165 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_VCUD60, DL160)},
166 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_DLDVI, DL160)},
167 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_VGA10, DL120)},
168 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_WSDVI, DLUNK)},
169 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_EC008, DL160)},
170 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_HPDOCK, DL160)},
171 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_NL571, DL160)},
172 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_M01061, DL195)},
173 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_NBDOCK, DL165)},
174 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_SWDVI, DLUNK)},
175 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_UM7X0, DL120)},
176 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_CONV, DL160)},
177 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_PLUGABLE, DL160)},
178 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LUM70, DL125)},
179 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_POLARIS2, DLUNK)},
180 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LT1421, DLUNK)},
181 {USB_VPI(USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_ITEC, DL165)},
185 udl_buffer_init(void *arg)
187 mtx_init(&udl_buffer_mtx, "USB", "UDL", MTX_DEF);
188 TAILQ_INIT(&udl_buffer_head);
190 SYSINIT(udl_buffer_init, SI_SUB_LOCK, SI_ORDER_FIRST, udl_buffer_init, NULL);
192 CTASSERT(sizeof(struct udl_buffer) < PAGE_SIZE);
195 udl_buffer_alloc(uint32_t size)
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);
205 mtx_unlock(&udl_buffer_mtx);
207 uint8_t *ptr = ((uint8_t *)buf) - size;
208 /* wipe and recycle buffer */
209 memset(ptr, 0, size);
210 /* return buffer pointer */
213 /* allocate new buffer */
214 return (malloc(size + sizeof(*buf), M_USB_DL, M_WAITOK | M_ZERO));
218 udl_buffer_free(void *_buf, uint32_t size)
220 struct udl_buffer *buf;
222 /* check for NULL pointer */
225 /* compute pointer to recycle list */
226 buf = (struct udl_buffer *)(((uint8_t *)_buf) + size);
229 * Memory mapped buffers should never be freed.
230 * Put display buffer into a recycle list.
232 mtx_lock(&udl_buffer_mtx);
234 TAILQ_INSERT_TAIL(&udl_buffer_head, buf, entry);
235 mtx_unlock(&udl_buffer_mtx);
239 udl_get_fb_size(struct udl_softc *sc)
241 unsigned i = sc->sc_cur_mode;
243 return ((uint32_t)udl_modes[i].hdisplay *
244 (uint32_t)udl_modes[i].vdisplay * 2);
248 udl_get_fb_width(struct udl_softc *sc)
250 unsigned i = sc->sc_cur_mode;
252 return (udl_modes[i].hdisplay);
256 udl_get_fb_height(struct udl_softc *sc)
258 unsigned i = sc->sc_cur_mode;
260 return (udl_modes[i].vdisplay);
264 udl_get_fb_hz(struct udl_softc *sc)
266 unsigned i = sc->sc_cur_mode;
268 return (udl_modes[i].hz);
272 udl_callout(void *arg)
274 struct udl_softc *sc = arg;
275 const uint32_t max = udl_get_fb_size(sc);
278 if (sc->sc_power_save == 0) {
281 /* figure out number of frames per second */
282 if (fps < UDL_FPS_MIN)
284 else if (fps > UDL_FPS_MAX)
287 if (sc->sc_sync_off >= max)
289 usbd_transfer_start(sc->sc_xfer[UDL_BULK_WRITE_0]);
290 usbd_transfer_start(sc->sc_xfer[UDL_BULK_WRITE_1]);
294 callout_reset(&sc->sc_callout, hz / fps, &udl_callout, sc);
298 udl_probe(device_t dev)
300 struct usb_attach_arg *uaa = device_get_ivars(dev);
302 if (uaa->usb_mode != USB_MODE_HOST)
304 if (uaa->info.bConfigIndex != 0)
306 if (uaa->info.bIfaceIndex != 0)
309 return (usbd_lookup_id_by_uaa(udl_devs, sizeof(udl_devs), uaa));
313 udl_attach(device_t dev)
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);
322 device_set_usb_desc(dev);
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;
329 error = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex,
330 sc->sc_xfer, udl_config, UDL_N_TRANSFER, sc, &sc->sc_mtx);
333 DPRINTF("usbd_transfer_setup error=%s\n", usbd_errstr(error));
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]);
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);
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;
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");
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");
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;
362 * The product might have more than one chip
364 if (sc->sc_chip == DLUNK)
365 udl_select_chip(sc, uaa);
367 for (i = 0; i != UDL_CMD_MAX_BUFFERS; i++) {
368 struct udl_cmd_buf *cb = &sc->sc_cmd_buf_temp[i];
370 TAILQ_INSERT_TAIL(&sc->sc_cmd_buf_free, cb, entry);
376 error = udl_init_chip(sc);
377 if (error != USB_ERR_NORMAL_COMPLETION)
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");
389 /* Export current mode */
390 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "mode",
391 CTLFLAG_RD, &sc->sc_cur_mode, 0, "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);
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));
406 udl_init_resolution(sc);
408 /* Allocate frame buffer */
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;
427 sc->sc_fbdev = device_add_child(dev, "fbd", -1);
428 if (sc->sc_fbdev == NULL)
430 if (device_probe_and_attach(sc->sc_fbdev) != 0)
442 udl_detach(device_t dev)
444 struct udl_softc *sc = device_get_softc(dev);
446 if (sc->sc_fbdev != NULL) {
452 device_delete_child(dev, bdev);
456 callout_stop(&sc->sc_callout);
459 usbd_transfer_unsetup(sc->sc_xfer, UDL_N_TRANSFER);
461 callout_drain(&sc->sc_callout);
463 mtx_destroy(&sc->sc_mtx);
464 cv_destroy(&sc->sc_cv);
466 /* put main framebuffer into a recycle list, if any */
467 udl_buffer_free(sc->sc_fb_addr, sc->sc_fb_size);
469 /* free shadow framebuffer memory, if any */
470 free(sc->sc_fb_copy, M_USB_DL);
475 static struct fb_info *
476 udl_fb_getinfo(device_t dev)
478 struct udl_softc *sc = device_get_softc(dev);
480 return (&sc->sc_fb_info);
484 udl_fb_setblankmode(void *arg, int mode)
486 struct udl_softc *sc = arg;
490 udl_power_save(sc, 1, M_WAITOK);
492 case V_DISPLAY_BLANK:
493 udl_power_save(sc, 1, M_WAITOK);
494 if (sc->sc_fb_addr != 0) {
495 const uint32_t max = udl_get_fb_size(sc);
497 memset((void *)sc->sc_fb_addr, 0, max);
500 case V_DISPLAY_STAND_BY:
501 case V_DISPLAY_SUSPEND:
502 udl_power_save(sc, 0, M_WAITOK);
508 static struct udl_cmd_buf *
509 udl_cmd_buf_alloc_locked(struct udl_softc *sc, int flags)
511 struct udl_cmd_buf *cb;
513 while ((cb = TAILQ_FIRST(&sc->sc_cmd_buf_free)) == NULL) {
514 if (flags != M_WAITOK)
516 cv_wait(&sc->sc_cv, &sc->sc_mtx);
519 TAILQ_REMOVE(&sc->sc_cmd_buf_free, cb, entry);
525 static struct udl_cmd_buf *
526 udl_cmd_buf_alloc(struct udl_softc *sc, int flags)
528 struct udl_cmd_buf *cb;
531 cb = udl_cmd_buf_alloc_locked(sc, flags);
537 udl_cmd_buf_send(struct udl_softc *sc, struct udl_cmd_buf *cb)
541 TAILQ_INSERT_TAIL(&sc->sc_cmd_buf_free, cb, entry);
543 /* mark end of command stack */
544 udl_cmd_insert_int_1(cb, UDL_BULK_SOC);
545 udl_cmd_insert_int_1(cb, UDL_BULK_CMD_EOC);
547 TAILQ_INSERT_TAIL(&sc->sc_cmd_buf_pending, cb, entry);
548 usbd_transfer_start(sc->sc_xfer[UDL_BULK_WRITE_0]);
549 usbd_transfer_start(sc->sc_xfer[UDL_BULK_WRITE_1]);
554 static struct udl_cmd_buf *
555 udl_fb_synchronize_locked(struct udl_softc *sc)
557 const uint32_t max = udl_get_fb_size(sc);
559 /* check if framebuffer is not ready */
560 if (sc->sc_fb_addr == NULL ||
561 sc->sc_fb_copy == NULL)
564 while (sc->sc_sync_off < max) {
565 uint32_t delta = max - sc->sc_sync_off;
567 if (delta > UDL_CMD_MAX_PIXEL_COUNT * 2)
568 delta = UDL_CMD_MAX_PIXEL_COUNT * 2;
569 if (bcmp(sc->sc_fb_addr + sc->sc_sync_off, sc->sc_fb_copy + sc->sc_sync_off, delta) != 0) {
570 struct udl_cmd_buf *cb;
572 cb = udl_cmd_buf_alloc_locked(sc, M_NOWAIT);
575 memcpy(sc->sc_fb_copy + sc->sc_sync_off,
576 sc->sc_fb_addr + sc->sc_sync_off, delta);
577 udl_cmd_insert_int_1(cb, UDL_BULK_SOC);
578 udl_cmd_insert_int_1(cb, UDL_BULK_CMD_FB_WRITE | UDL_BULK_CMD_FB_WORD);
579 udl_cmd_insert_int_3(cb, sc->sc_sync_off);
580 udl_cmd_insert_int_1(cb, delta / 2);
581 udl_cmd_insert_buf_le16(cb, sc->sc_fb_copy + sc->sc_sync_off, delta);
582 sc->sc_sync_off += delta;
585 sc->sc_sync_off += delta;
593 udl_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
595 struct udl_softc *sc = usbd_xfer_softc(xfer);
596 struct udl_cmd_head *phead = usbd_xfer_get_priv(xfer);
597 struct udl_cmd_buf *cb;
600 switch (USB_GET_STATE(xfer)) {
601 case USB_ST_TRANSFERRED:
602 TAILQ_CONCAT(&sc->sc_cmd_buf_free, phead, entry);
605 for (i = 0; i != UDL_CMD_MAX_FRAMES; i++) {
606 cb = TAILQ_FIRST(&sc->sc_cmd_buf_pending);
608 cb = udl_fb_synchronize_locked(sc);
612 TAILQ_REMOVE(&sc->sc_cmd_buf_pending, cb, entry);
614 TAILQ_INSERT_TAIL(phead, cb, entry);
615 usbd_xfer_set_frame_data(xfer, i, cb->buf, cb->off);
618 usbd_xfer_set_frames(xfer, i);
619 usbd_transfer_submit(xfer);
623 TAILQ_CONCAT(&sc->sc_cmd_buf_free, phead, entry);
624 if (error != USB_ERR_CANCELLED) {
625 /* try clear stall first */
626 usbd_xfer_set_stall(xfer);
631 /* wakeup any waiters */
632 cv_signal(&sc->sc_cv);
636 udl_power_save(struct udl_softc *sc, int on, int flags)
638 struct udl_cmd_buf *cb;
641 cb = udl_cmd_buf_alloc(sc, flags);
645 DPRINTF("screen %s\n", on ? "ON" : "OFF");
647 sc->sc_power_save = on ? 0 : 1;
650 udl_cmd_write_reg_1(cb, UDL_REG_SCREEN, UDL_REG_SCREEN_ON);
652 udl_cmd_write_reg_1(cb, UDL_REG_SCREEN, UDL_REG_SCREEN_OFF);
654 udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0xff);
655 udl_cmd_buf_send(sc, cb);
660 udl_ctrl_msg(struct udl_softc *sc, uint8_t rt, uint8_t r,
661 uint16_t index, uint16_t value, uint8_t *buf, size_t len)
663 usb_device_request_t req;
666 req.bmRequestType = rt;
668 USETW(req.wIndex, index);
669 USETW(req.wValue, value);
670 USETW(req.wLength, len);
672 error = usbd_do_request_flags(sc->sc_udev, NULL,
673 &req, buf, 0, NULL, USB_DEFAULT_TIMEOUT);
675 DPRINTF("%s\n", usbd_errstr(error));
681 udl_poll(struct udl_softc *sc, uint32_t *buf)
686 error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE,
687 UDL_CTRL_CMD_POLL, 0x0000, 0x0000, (uint8_t *)&lbuf, sizeof(lbuf));
688 if (error == USB_ERR_NORMAL_COMPLETION)
689 *buf = le32toh(lbuf);
694 udl_read_1(struct udl_softc *sc, uint16_t addr, uint8_t *buf)
699 error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE,
700 UDL_CTRL_CMD_READ_1, addr, 0x0000, lbuf, 1);
701 if (error == USB_ERR_NORMAL_COMPLETION)
702 *buf = *(uint8_t *)lbuf;
707 udl_write_1(struct udl_softc *sc, uint16_t addr, uint8_t buf)
711 error = udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE,
712 UDL_CTRL_CMD_WRITE_1, addr, 0x0000, &buf, 1);
717 udl_read_edid(struct udl_softc *sc, uint8_t *buf)
725 error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE,
726 UDL_CTRL_CMD_READ_EDID, 0x00a1, (offset << 8), lbuf, 64);
727 if (error != USB_ERR_NORMAL_COMPLETION)
729 bcopy(lbuf + 1, buf + offset, 63);
732 error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE,
733 UDL_CTRL_CMD_READ_EDID, 0x00a1, (offset << 8), lbuf, 64);
734 if (error != USB_ERR_NORMAL_COMPLETION)
736 bcopy(lbuf + 1, buf + offset, 63);
739 error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE,
740 UDL_CTRL_CMD_READ_EDID, 0x00a1, (offset << 8), lbuf, 3);
741 if (error != USB_ERR_NORMAL_COMPLETION)
743 bcopy(lbuf + 1, buf + offset, 2);
749 udl_lookup_mode(uint16_t hdisplay, uint16_t vdisplay, uint8_t hz,
750 uint16_t chip, uint32_t clock)
755 * Check first if we have a matching mode with pixelclock
757 for (idx = 0; idx != UDL_MAX_MODES; idx++) {
758 if ((udl_modes[idx].hdisplay == hdisplay) &&
759 (udl_modes[idx].vdisplay == vdisplay) &&
760 (udl_modes[idx].clock == clock) &&
761 (udl_modes[idx].chip <= chip)) {
767 * If not, check for matching mode with update frequency
769 for (idx = 0; idx != UDL_MAX_MODES; idx++) {
770 if ((udl_modes[idx].hdisplay == hdisplay) &&
771 (udl_modes[idx].vdisplay == vdisplay) &&
772 (udl_modes[idx].hz == hz) &&
773 (udl_modes[idx].chip <= chip)) {
781 udl_select_chip(struct udl_softc *sc, struct usb_attach_arg *uaa)
785 pserial = usb_get_serial(uaa->device);
789 if ((uaa->info.idVendor == USB_VENDOR_DISPLAYLINK) &&
790 (uaa->info.idProduct == USB_PRODUCT_DISPLAYLINK_WSDVI)) {
793 * WS Tech DVI is DL120 or DL160. All deviced uses the
794 * same revision (0.04) so iSerialNumber must be used
795 * to determin which chip it is.
798 if (strlen(pserial) > 7) {
799 if (strncmp(pserial, "0198-13", 7) == 0)
802 DPRINTF("iSerialNumber (%s) used to select chip (%d)\n",
803 pserial, sc->sc_chip);
805 if ((uaa->info.idVendor == USB_VENDOR_DISPLAYLINK) &&
806 (uaa->info.idProduct == USB_PRODUCT_DISPLAYLINK_SWDVI)) {
809 * SUNWEIT DVI is DL160, DL125, DL165 or DL195. Major revision
810 * can be used to differ between DL1x0 and DL1x5. Minor to
811 * differ between DL1x5. iSerialNumber seems not to be uniqe.
816 if (uaa->info.bcdDevice >= 0x100) {
818 if (uaa->info.bcdDevice == 0x104)
820 if (uaa->info.bcdDevice == 0x108)
823 DPRINTF("bcdDevice (%02x) used to select chip (%d)\n",
824 uaa->info.bcdDevice, sc->sc_chip);
829 udl_set_enc_key(struct udl_softc *sc, uint8_t *buf, uint8_t len)
833 error = udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE,
834 UDL_CTRL_CMD_SET_KEY, 0x0000, 0x0000, buf, len);
839 udl_fbmem_alloc(struct udl_softc *sc)
843 size = udl_get_fb_size(sc);
844 size = round_page(size);
845 /* check for zero size */
849 * It is assumed that allocations above PAGE_SIZE bytes will
850 * be PAGE_SIZE aligned for use with mmap()
852 sc->sc_fb_addr = udl_buffer_alloc(size);
853 sc->sc_fb_copy = malloc(size, M_USB_DL, M_WAITOK | M_ZERO);
854 sc->sc_fb_size = size;
858 udl_cmd_insert_int_1(struct udl_cmd_buf *cb, uint8_t value)
861 cb->buf[cb->off] = value;
867 udl_cmd_insert_int_2(struct udl_cmd_buf *cb, uint16_t value)
871 lvalue = htobe16(value);
872 bcopy(&lvalue, cb->buf + cb->off, 2);
880 udl_cmd_insert_int_3(struct udl_cmd_buf *cb, uint32_t value)
884 #if BYTE_ORDER == BIG_ENDIAN
885 lvalue = htobe32(value) << 8;
887 lvalue = htobe32(value) >> 8;
889 bcopy(&lvalue, cb->buf + cb->off, 3);
896 udl_cmd_insert_int_4(struct udl_cmd_buf *cb, uint32_t value)
900 lvalue = htobe32(value);
901 bcopy(&lvalue, cb->buf + cb->off, 4);
909 udl_cmd_insert_buf_le16(struct udl_cmd_buf *cb, const uint8_t *buf, uint32_t len)
913 for (x = 0; x != len; x += 2) {
914 /* byte swap from little endian to big endian */
915 cb->buf[cb->off + x + 0] = buf[x + 1];
916 cb->buf[cb->off + x + 1] = buf[x + 0];
922 udl_cmd_write_reg_1(struct udl_cmd_buf *cb, uint8_t reg, uint8_t val)
925 udl_cmd_insert_int_1(cb, UDL_BULK_SOC);
926 udl_cmd_insert_int_1(cb, UDL_BULK_CMD_REG_WRITE_1);
927 udl_cmd_insert_int_1(cb, reg);
928 udl_cmd_insert_int_1(cb, val);
932 udl_cmd_write_reg_3(struct udl_cmd_buf *cb, uint8_t reg, uint32_t val)
935 udl_cmd_write_reg_1(cb, reg + 0, (val >> 16) & 0xff);
936 udl_cmd_write_reg_1(cb, reg + 1, (val >> 8) & 0xff);
937 udl_cmd_write_reg_1(cb, reg + 2, (val >> 0) & 0xff);
941 udl_init_chip(struct udl_softc *sc)
947 error = udl_poll(sc, &ui32);
948 if (error != USB_ERR_NORMAL_COMPLETION)
950 DPRINTF("poll=0x%08x\n", ui32);
952 /* Some products may use later chip too */
953 switch (ui32 & 0xff) {
954 case 0xf1: /* DL1x5 */
955 switch (sc->sc_chip) {
965 DPRINTF("chip 0x%04x\n", sc->sc_chip);
967 error = udl_read_1(sc, 0xc484, &ui8);
968 if (error != USB_ERR_NORMAL_COMPLETION)
970 DPRINTF("read 0x%02x from 0xc484\n", ui8);
972 error = udl_write_1(sc, 0xc41f, 0x01);
973 if (error != USB_ERR_NORMAL_COMPLETION)
975 DPRINTF("write 0x01 to 0xc41f\n");
977 error = udl_read_edid(sc, sc->sc_edid);
978 if (error != USB_ERR_NORMAL_COMPLETION)
980 DPRINTF("read EDID\n");
982 error = udl_set_enc_key(sc, __DECONST(void *, udl_null_key_1),
983 sizeof(udl_null_key_1));
984 if (error != USB_ERR_NORMAL_COMPLETION)
986 DPRINTF("set encryption key\n");
988 error = udl_write_1(sc, 0xc40b, 0x00);
989 if (error != USB_ERR_NORMAL_COMPLETION)
991 DPRINTF("write 0x00 to 0xc40b\n");
993 return (USB_ERR_NORMAL_COMPLETION);
997 udl_init_fb_offsets(struct udl_cmd_buf *cb, uint32_t start16, uint32_t stride16,
998 uint32_t start8, uint32_t stride8)
1000 udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0x00);
1001 udl_cmd_write_reg_3(cb, UDL_REG_ADDR_START16, start16);
1002 udl_cmd_write_reg_3(cb, UDL_REG_ADDR_STRIDE16, stride16);
1003 udl_cmd_write_reg_3(cb, UDL_REG_ADDR_START8, start8);
1004 udl_cmd_write_reg_3(cb, UDL_REG_ADDR_STRIDE8, stride8);
1005 udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0xff);
1009 udl_init_resolution(struct udl_softc *sc)
1011 const uint32_t max = udl_get_fb_size(sc);
1012 const uint8_t *buf = udl_modes[sc->sc_cur_mode].mode;
1013 struct udl_cmd_buf *cb;
1018 /* get new buffer */
1019 cb = udl_cmd_buf_alloc(sc, M_WAITOK);
1023 /* write resolution values and set video memory offsets */
1024 udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0x00);
1025 for (i = 0; i < UDL_MODE_SIZE; i++)
1026 udl_cmd_write_reg_1(cb, i, buf[i]);
1027 udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0xff);
1029 udl_init_fb_offsets(cb, 0x000000, 0x000a00, 0x555555, 0x000500);
1030 udl_cmd_buf_send(sc, cb);
1032 /* fill screen with black color */
1033 for (i = 0; i < max; i += delta) {
1034 static const uint8_t udl_black[UDL_CMD_MAX_PIXEL_COUNT * 2] __aligned(4);
1037 if (delta > UDL_CMD_MAX_PIXEL_COUNT * 2)
1038 delta = UDL_CMD_MAX_PIXEL_COUNT * 2;
1040 error = udl_cmd_write_buf_le16(sc, udl_black, i, delta / 2, M_WAITOK);
1042 error = udl_cmd_buf_copy_le16(sc, 0, i, delta / 2, M_WAITOK);
1047 /* get new buffer */
1048 cb = udl_cmd_buf_alloc(sc, M_WAITOK);
1052 /* show framebuffer content */
1053 udl_cmd_write_reg_1(cb, UDL_REG_SCREEN, UDL_REG_SCREEN_ON);
1054 udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0xff);
1055 udl_cmd_buf_send(sc, cb);
1060 udl_select_mode(struct udl_softc *sc)
1062 struct udl_mode mode;
1063 int index = UDL_MAX_MODES;
1066 /* try to get the preferred mode from EDID */
1067 edid_parse(sc->sc_edid, &sc->sc_edid_info);
1069 edid_print(&sc->sc_edid_info);
1071 if (sc->sc_edid_info.edid_preferred_mode != NULL) {
1073 (sc->sc_edid_info.edid_preferred_mode->dot_clock * 1000) /
1074 (sc->sc_edid_info.edid_preferred_mode->htotal *
1075 sc->sc_edid_info.edid_preferred_mode->vtotal);
1077 sc->sc_edid_info.edid_preferred_mode->dot_clock / 10;
1079 sc->sc_edid_info.edid_preferred_mode->hdisplay;
1081 sc->sc_edid_info.edid_preferred_mode->vdisplay;
1082 index = udl_lookup_mode(mode.hdisplay, mode.vdisplay, mode.hz,
1083 sc->sc_chip, mode.clock);
1084 sc->sc_cur_mode = index;
1086 DPRINTF("no preferred mode found!\n");
1089 if (index == UDL_MAX_MODES) {
1090 DPRINTF("no mode line found\n");
1093 while (i < sc->sc_edid_info.edid_nmodes) {
1095 (sc->sc_edid_info.edid_modes[i].dot_clock * 1000) /
1096 (sc->sc_edid_info.edid_modes[i].htotal *
1097 sc->sc_edid_info.edid_modes[i].vtotal);
1099 sc->sc_edid_info.edid_modes[i].dot_clock / 10;
1101 sc->sc_edid_info.edid_modes[i].hdisplay;
1103 sc->sc_edid_info.edid_modes[i].vdisplay;
1104 index = udl_lookup_mode(mode.hdisplay, mode.vdisplay,
1105 mode.hz, sc->sc_chip, mode.clock);
1106 if (index < UDL_MAX_MODES)
1107 if ((sc->sc_cur_mode == UDL_MAX_MODES) ||
1108 (index > sc->sc_cur_mode))
1109 sc->sc_cur_mode = index;
1114 * If no mode found use default.
1116 if (sc->sc_cur_mode == UDL_MAX_MODES)
1117 sc->sc_cur_mode = udl_lookup_mode(800, 600, 60, sc->sc_chip, 0);
1121 udl_cmd_write_buf_le16(struct udl_softc *sc, const uint8_t *buf, uint32_t off,
1122 uint8_t pixels, int flags)
1124 struct udl_cmd_buf *cb;
1126 cb = udl_cmd_buf_alloc(sc, flags);
1130 udl_cmd_insert_int_1(cb, UDL_BULK_SOC);
1131 udl_cmd_insert_int_1(cb, UDL_BULK_CMD_FB_WRITE | UDL_BULK_CMD_FB_WORD);
1132 udl_cmd_insert_int_3(cb, off);
1133 udl_cmd_insert_int_1(cb, pixels);
1134 udl_cmd_insert_buf_le16(cb, buf, 2 * pixels);
1135 udl_cmd_buf_send(sc, cb);
1141 udl_cmd_buf_copy_le16(struct udl_softc *sc, uint32_t src, uint32_t dst,
1142 uint8_t pixels, int flags)
1144 struct udl_cmd_buf *cb;
1146 cb = udl_cmd_buf_alloc(sc, flags);
1150 udl_cmd_insert_int_1(cb, UDL_BULK_SOC);
1151 udl_cmd_insert_int_1(cb, UDL_BULK_CMD_FB_COPY | UDL_BULK_CMD_FB_WORD);
1152 udl_cmd_insert_int_3(cb, dst);
1153 udl_cmd_insert_int_1(cb, pixels);
1154 udl_cmd_insert_int_3(cb, src);
1155 udl_cmd_buf_send(sc, cb);