1 /* $OpenBSD: udl.c,v 1.81 2014/12/09 07:05:06 doug Exp $ */ 2 /* $FreeBSD$ */ 3 4 /*- 5 * Copyright (c) 2015 Hans Petter Selasky <hselasky@freebsd.org> 6 * Copyright (c) 2009 Marcus Glocker <mglocker@openbsd.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 /* 22 * Driver for the "DisplayLink DL-120 / DL-160" graphic chips based on 23 * the reversed engineered specifications of Florian Echtler 24 * <floe@butterbrot.org>: 25 * 26 * http://floe.butterbrot.org/displaylink/doku.php 27 */ 28 29 #include <sys/param.h> 30 #include <sys/bus.h> 31 #include <sys/callout.h> 32 #include <sys/conf.h> 33 #include <sys/kernel.h> 34 #include <sys/lock.h> 35 #include <sys/module.h> 36 #include <sys/mutex.h> 37 #include <sys/condvar.h> 38 #include <sys/sysctl.h> 39 #include <sys/systm.h> 40 #include <sys/consio.h> 41 #include <sys/fbio.h> 42 43 #include <dev/fb/fbreg.h> 44 #include <dev/syscons/syscons.h> 45 46 #include <dev/videomode/videomode.h> 47 #include <dev/videomode/edidvar.h> 48 49 #include <dev/usb/usb.h> 50 #include <dev/usb/usbdi.h> 51 #include <dev/usb/usbdi_util.h> 52 #include "usbdevs.h" 53 54 #include <dev/usb/video/udl.h> 55 56 #include "fb_if.h" 57 58 #undef DPRINTF 59 #undef DPRINTFN 60 #define USB_DEBUG_VAR udl_debug 61 #include <dev/usb/usb_debug.h> 62 63 static SYSCTL_NODE(_hw_usb, OID_AUTO, udl, CTLFLAG_RW, 0, "USB UDL"); 64 65 #ifdef USB_DEBUG 66 static int udl_debug = 0; 67 68 SYSCTL_INT(_hw_usb_udl, OID_AUTO, debug, CTLFLAG_RWTUN, 69 &udl_debug, 0, "Debug level"); 70 #endif 71 72 #define UDL_FPS_MAX 60 73 #define UDL_FPS_MIN 1 74 75 static int udl_fps = 25; 76 SYSCTL_INT(_hw_usb_udl, OID_AUTO, fps, CTLFLAG_RWTUN, 77 &udl_fps, 0, "Frames Per Second, 1-60"); 78 79 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 devclass_t udl_devclass; 136 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), 142 DEVMETHOD_END 143 }; 144 145 static driver_t udl_driver = { 146 .name = "udl", 147 .methods = udl_methods, 148 .size = sizeof(struct udl_softc), 149 }; 150 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); 156 157 /* 158 * Matching devices. 159 */ 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)}, 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 if (sc->sc_fbdev != NULL) { 447 device_t bdev; 448 449 bdev = sc->sc_fbdev; 450 sc->sc_fbdev = NULL; 451 device_detach(bdev); 452 device_delete_child(dev, bdev); 453 } 454 UDL_LOCK(sc); 455 sc->sc_gone = 1; 456 callout_stop(&sc->sc_callout); 457 UDL_UNLOCK(sc); 458 459 usbd_transfer_unsetup(sc->sc_xfer, UDL_N_TRANSFER); 460 461 callout_drain(&sc->sc_callout); 462 463 mtx_destroy(&sc->sc_mtx); 464 cv_destroy(&sc->sc_cv); 465 466 /* put main framebuffer into a recycle list, if any */ 467 udl_buffer_free(sc->sc_fb_addr, sc->sc_fb_size); 468 469 /* free shadow framebuffer memory, if any */ 470 free(sc->sc_fb_copy, M_USB_DL); 471 472 return (0); 473 } 474 475 static struct fb_info * 476 udl_fb_getinfo(device_t dev) 477 { 478 struct udl_softc *sc = device_get_softc(dev); 479 480 return (&sc->sc_fb_info); 481 } 482 483 static int 484 udl_fb_setblankmode(void *arg, int mode) 485 { 486 struct udl_softc *sc = arg; 487 488 switch (mode) { 489 case V_DISPLAY_ON: 490 udl_power_save(sc, 1, M_WAITOK); 491 break; 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); 496 497 memset((void *)sc->sc_fb_addr, 0, max); 498 } 499 break; 500 case V_DISPLAY_STAND_BY: 501 case V_DISPLAY_SUSPEND: 502 udl_power_save(sc, 0, M_WAITOK); 503 break; 504 } 505 return (0); 506 } 507 508 static struct udl_cmd_buf * 509 udl_cmd_buf_alloc_locked(struct udl_softc *sc, int flags) 510 { 511 struct udl_cmd_buf *cb; 512 513 while ((cb = TAILQ_FIRST(&sc->sc_cmd_buf_free)) == NULL) { 514 if (flags != M_WAITOK) 515 break; 516 cv_wait(&sc->sc_cv, &sc->sc_mtx); 517 } 518 if (cb != NULL) { 519 TAILQ_REMOVE(&sc->sc_cmd_buf_free, cb, entry); 520 cb->off = 0; 521 } 522 return (cb); 523 } 524 525 static struct udl_cmd_buf * 526 udl_cmd_buf_alloc(struct udl_softc *sc, int flags) 527 { 528 struct udl_cmd_buf *cb; 529 530 UDL_LOCK(sc); 531 cb = udl_cmd_buf_alloc_locked(sc, flags); 532 UDL_UNLOCK(sc); 533 return (cb); 534 } 535 536 static void 537 udl_cmd_buf_send(struct udl_softc *sc, struct udl_cmd_buf *cb) 538 { 539 UDL_LOCK(sc); 540 if (sc->sc_gone) { 541 TAILQ_INSERT_TAIL(&sc->sc_cmd_buf_free, cb, entry); 542 } else { 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); 546 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]); 550 } 551 UDL_UNLOCK(sc); 552 } 553 554 static struct udl_cmd_buf * 555 udl_fb_synchronize_locked(struct udl_softc *sc) 556 { 557 const uint32_t max = udl_get_fb_size(sc); 558 559 /* check if framebuffer is not ready */ 560 if (sc->sc_fb_addr == NULL || 561 sc->sc_fb_copy == NULL) 562 return (NULL); 563 564 while (sc->sc_sync_off < max) { 565 uint32_t delta = max - sc->sc_sync_off; 566 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; 571 572 cb = udl_cmd_buf_alloc_locked(sc, M_NOWAIT); 573 if (cb == NULL) 574 goto done; 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; 583 return (cb); 584 } else { 585 sc->sc_sync_off += delta; 586 } 587 } 588 done: 589 return (NULL); 590 } 591 592 static void 593 udl_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 594 { 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; 598 unsigned i; 599 600 switch (USB_GET_STATE(xfer)) { 601 case USB_ST_TRANSFERRED: 602 TAILQ_CONCAT(&sc->sc_cmd_buf_free, phead, entry); 603 case USB_ST_SETUP: 604 tr_setup: 605 for (i = 0; i != UDL_CMD_MAX_FRAMES; i++) { 606 cb = TAILQ_FIRST(&sc->sc_cmd_buf_pending); 607 if (cb == NULL) { 608 cb = udl_fb_synchronize_locked(sc); 609 if (cb == NULL) 610 break; 611 } else { 612 TAILQ_REMOVE(&sc->sc_cmd_buf_pending, cb, entry); 613 } 614 TAILQ_INSERT_TAIL(phead, cb, entry); 615 usbd_xfer_set_frame_data(xfer, i, cb->buf, cb->off); 616 } 617 if (i != 0) { 618 usbd_xfer_set_frames(xfer, i); 619 usbd_transfer_submit(xfer); 620 } 621 break; 622 default: 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); 627 goto tr_setup; 628 } 629 break; 630 } 631 /* wakeup any waiters */ 632 cv_signal(&sc->sc_cv); 633 } 634 635 static int 636 udl_power_save(struct udl_softc *sc, int on, int flags) 637 { 638 struct udl_cmd_buf *cb; 639 640 /* get new buffer */ 641 cb = udl_cmd_buf_alloc(sc, flags); 642 if (cb == NULL) 643 return (EAGAIN); 644 645 DPRINTF("screen %s\n", on ? "ON" : "OFF"); 646 647 sc->sc_power_save = on ? 0 : 1; 648 649 if (on) 650 udl_cmd_write_reg_1(cb, UDL_REG_SCREEN, UDL_REG_SCREEN_ON); 651 else 652 udl_cmd_write_reg_1(cb, UDL_REG_SCREEN, UDL_REG_SCREEN_OFF); 653 654 udl_cmd_write_reg_1(cb, UDL_REG_SYNC, 0xff); 655 udl_cmd_buf_send(sc, cb); 656 return (0); 657 } 658 659 static int 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) 662 { 663 usb_device_request_t req; 664 int error; 665 666 req.bmRequestType = rt; 667 req.bRequest = r; 668 USETW(req.wIndex, index); 669 USETW(req.wValue, value); 670 USETW(req.wLength, len); 671 672 error = usbd_do_request_flags(sc->sc_udev, NULL, 673 &req, buf, 0, NULL, USB_DEFAULT_TIMEOUT); 674 675 DPRINTF("%s\n", usbd_errstr(error)); 676 677 return (error); 678 } 679 680 static int 681 udl_poll(struct udl_softc *sc, uint32_t *buf) 682 { 683 uint32_t lbuf; 684 int error; 685 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); 690 return (error); 691 } 692 693 static int 694 udl_read_1(struct udl_softc *sc, uint16_t addr, uint8_t *buf) 695 { 696 uint8_t lbuf[1]; 697 int error; 698 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; 703 return (error); 704 } 705 706 static int 707 udl_write_1(struct udl_softc *sc, uint16_t addr, uint8_t buf) 708 { 709 int error; 710 711 error = udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, 712 UDL_CTRL_CMD_WRITE_1, addr, 0x0000, &buf, 1); 713 return (error); 714 } 715 716 static int 717 udl_read_edid(struct udl_softc *sc, uint8_t *buf) 718 { 719 uint8_t lbuf[64]; 720 uint16_t offset; 721 int error; 722 723 offset = 0; 724 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) 728 goto fail; 729 bcopy(lbuf + 1, buf + offset, 63); 730 offset += 63; 731 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) 735 goto fail; 736 bcopy(lbuf + 1, buf + offset, 63); 737 offset += 63; 738 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) 742 goto fail; 743 bcopy(lbuf + 1, buf + offset, 2); 744 fail: 745 return (error); 746 } 747 748 static uint8_t 749 udl_lookup_mode(uint16_t hdisplay, uint16_t vdisplay, uint8_t hz, 750 uint16_t chip, uint32_t clock) 751 { 752 uint8_t idx; 753 754 /* 755 * Check first if we have a matching mode with pixelclock 756 */ 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)) { 762 return (idx); 763 } 764 } 765 766 /* 767 * If not, check for matching mode with update frequency 768 */ 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)) { 774 return (idx); 775 } 776 } 777 return (idx); 778 } 779 780 static void 781 udl_select_chip(struct udl_softc *sc, struct usb_attach_arg *uaa) 782 { 783 const char *pserial; 784 785 pserial = usb_get_serial(uaa->device); 786 787 sc->sc_chip = DL120; 788 789 if ((uaa->info.idVendor == USB_VENDOR_DISPLAYLINK) && 790 (uaa->info.idProduct == USB_PRODUCT_DISPLAYLINK_WSDVI)) { 791 792 /* 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. 796 */ 797 798 if (strlen(pserial) > 7) { 799 if (strncmp(pserial, "0198-13", 7) == 0) 800 sc->sc_chip = DL160; 801 } 802 DPRINTF("iSerialNumber (%s) used to select chip (%d)\n", 803 pserial, sc->sc_chip); 804 } 805 if ((uaa->info.idVendor == USB_VENDOR_DISPLAYLINK) && 806 (uaa->info.idProduct == USB_PRODUCT_DISPLAYLINK_SWDVI)) { 807 808 /* 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. 812 */ 813 814 sc->sc_chip = DL160; 815 816 if (uaa->info.bcdDevice >= 0x100) { 817 sc->sc_chip = DL165; 818 if (uaa->info.bcdDevice == 0x104) 819 sc->sc_chip = DL195; 820 if (uaa->info.bcdDevice == 0x108) 821 sc->sc_chip = DL125; 822 } 823 DPRINTF("bcdDevice (%02x) used to select chip (%d)\n", 824 uaa->info.bcdDevice, sc->sc_chip); 825 } 826 } 827 828 static int 829 udl_set_enc_key(struct udl_softc *sc, uint8_t *buf, uint8_t len) 830 { 831 int error; 832 833 error = udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, 834 UDL_CTRL_CMD_SET_KEY, 0x0000, 0x0000, buf, len); 835 return (error); 836 } 837 838 static void 839 udl_fbmem_alloc(struct udl_softc *sc) 840 { 841 uint32_t size; 842 843 size = udl_get_fb_size(sc); 844 size = round_page(size); 845 /* check for zero size */ 846 if (size == 0) 847 size = PAGE_SIZE; 848 /* 849 * It is assumed that allocations above PAGE_SIZE bytes will 850 * be PAGE_SIZE aligned for use with mmap() 851 */ 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; 855 } 856 857 static void 858 udl_cmd_insert_int_1(struct udl_cmd_buf *cb, uint8_t value) 859 { 860 861 cb->buf[cb->off] = value; 862 cb->off += 1; 863 } 864 865 #if 0 866 static void 867 udl_cmd_insert_int_2(struct udl_cmd_buf *cb, uint16_t value) 868 { 869 uint16_t lvalue; 870 871 lvalue = htobe16(value); 872 bcopy(&lvalue, cb->buf + cb->off, 2); 873 874 cb->off += 2; 875 } 876 877 #endif 878 879 static void 880 udl_cmd_insert_int_3(struct udl_cmd_buf *cb, uint32_t value) 881 { 882 uint32_t lvalue; 883 884 #if BYTE_ORDER == BIG_ENDIAN 885 lvalue = htobe32(value) << 8; 886 #else 887 lvalue = htobe32(value) >> 8; 888 #endif 889 bcopy(&lvalue, cb->buf + cb->off, 3); 890 891 cb->off += 3; 892 } 893 894 #if 0 895 static void 896 udl_cmd_insert_int_4(struct udl_cmd_buf *cb, uint32_t value) 897 { 898 uint32_t lvalue; 899 900 lvalue = htobe32(value); 901 bcopy(&lvalue, cb->buf + cb->off, 4); 902 903 cb->off += 4; 904 } 905 906 #endif 907 908 static void 909 udl_cmd_insert_buf_le16(struct udl_cmd_buf *cb, const uint8_t *buf, uint32_t len) 910 { 911 uint32_t x; 912 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]; 917 } 918 cb->off += len; 919 } 920 921 static void 922 udl_cmd_write_reg_1(struct udl_cmd_buf *cb, uint8_t reg, uint8_t val) 923 { 924 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); 929 } 930 931 static void 932 udl_cmd_write_reg_3(struct udl_cmd_buf *cb, uint8_t reg, uint32_t val) 933 { 934 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); 938 } 939 940 static int 941 udl_init_chip(struct udl_softc *sc) 942 { 943 uint32_t ui32; 944 uint8_t ui8; 945 int error; 946 947 error = udl_poll(sc, &ui32); 948 if (error != USB_ERR_NORMAL_COMPLETION) 949 return (error); 950 DPRINTF("poll=0x%08x\n", ui32); 951 952 /* Some products may use later chip too */ 953 switch (ui32 & 0xff) { 954 case 0xf1: /* DL1x5 */ 955 switch (sc->sc_chip) { 956 case DL120: 957 sc->sc_chip = DL125; 958 break; 959 case DL160: 960 sc->sc_chip = DL165; 961 break; 962 } 963 break; 964 } 965 DPRINTF("chip 0x%04x\n", sc->sc_chip); 966 967 error = udl_read_1(sc, 0xc484, &ui8); 968 if (error != USB_ERR_NORMAL_COMPLETION) 969 return (error); 970 DPRINTF("read 0x%02x from 0xc484\n", ui8); 971 972 error = udl_write_1(sc, 0xc41f, 0x01); 973 if (error != USB_ERR_NORMAL_COMPLETION) 974 return (error); 975 DPRINTF("write 0x01 to 0xc41f\n"); 976 977 error = udl_read_edid(sc, sc->sc_edid); 978 if (error != USB_ERR_NORMAL_COMPLETION) 979 return (error); 980 DPRINTF("read EDID\n"); 981 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) 985 return (error); 986 DPRINTF("set encryption key\n"); 987 988 error = udl_write_1(sc, 0xc40b, 0x00); 989 if (error != USB_ERR_NORMAL_COMPLETION) 990 return (error); 991 DPRINTF("write 0x00 to 0xc40b\n"); 992 993 return (USB_ERR_NORMAL_COMPLETION); 994 } 995 996 static void 997 udl_init_fb_offsets(struct udl_cmd_buf *cb, uint32_t start16, uint32_t stride16, 998 uint32_t start8, uint32_t stride8) 999 { 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); 1006 } 1007 1008 static int 1009 udl_init_resolution(struct udl_softc *sc) 1010 { 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; 1014 uint32_t delta; 1015 uint32_t i; 1016 int error; 1017 1018 /* get new buffer */ 1019 cb = udl_cmd_buf_alloc(sc, M_WAITOK); 1020 if (cb == NULL) 1021 return (EAGAIN); 1022 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); 1028 1029 udl_init_fb_offsets(cb, 0x000000, 0x000a00, 0x555555, 0x000500); 1030 udl_cmd_buf_send(sc, cb); 1031 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); 1035 1036 delta = max - i; 1037 if (delta > UDL_CMD_MAX_PIXEL_COUNT * 2) 1038 delta = UDL_CMD_MAX_PIXEL_COUNT * 2; 1039 if (i == 0) 1040 error = udl_cmd_write_buf_le16(sc, udl_black, i, delta / 2, M_WAITOK); 1041 else 1042 error = udl_cmd_buf_copy_le16(sc, 0, i, delta / 2, M_WAITOK); 1043 if (error) 1044 return (error); 1045 } 1046 1047 /* get new buffer */ 1048 cb = udl_cmd_buf_alloc(sc, M_WAITOK); 1049 if (cb == NULL) 1050 return (EAGAIN); 1051 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); 1056 return (0); 1057 } 1058 1059 static void 1060 udl_select_mode(struct udl_softc *sc) 1061 { 1062 struct udl_mode mode; 1063 int index = UDL_MAX_MODES; 1064 int i; 1065 1066 /* try to get the preferred mode from EDID */ 1067 edid_parse(sc->sc_edid, &sc->sc_edid_info); 1068 #ifdef USB_DEBUG 1069 edid_print(&sc->sc_edid_info); 1070 #endif 1071 if (sc->sc_edid_info.edid_preferred_mode != NULL) { 1072 mode.hz = 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); 1076 mode.clock = 1077 sc->sc_edid_info.edid_preferred_mode->dot_clock / 10; 1078 mode.hdisplay = 1079 sc->sc_edid_info.edid_preferred_mode->hdisplay; 1080 mode.vdisplay = 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; 1085 } else { 1086 DPRINTF("no preferred mode found!\n"); 1087 } 1088 1089 if (index == UDL_MAX_MODES) { 1090 DPRINTF("no mode line found\n"); 1091 1092 i = 0; 1093 while (i < sc->sc_edid_info.edid_nmodes) { 1094 mode.hz = 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); 1098 mode.clock = 1099 sc->sc_edid_info.edid_modes[i].dot_clock / 10; 1100 mode.hdisplay = 1101 sc->sc_edid_info.edid_modes[i].hdisplay; 1102 mode.vdisplay = 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; 1110 i++; 1111 } 1112 } 1113 /* 1114 * If no mode found use default. 1115 */ 1116 if (sc->sc_cur_mode == UDL_MAX_MODES) 1117 sc->sc_cur_mode = udl_lookup_mode(800, 600, 60, sc->sc_chip, 0); 1118 } 1119 1120 static int 1121 udl_cmd_write_buf_le16(struct udl_softc *sc, const uint8_t *buf, uint32_t off, 1122 uint8_t pixels, int flags) 1123 { 1124 struct udl_cmd_buf *cb; 1125 1126 cb = udl_cmd_buf_alloc(sc, flags); 1127 if (cb == NULL) 1128 return (EAGAIN); 1129 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); 1136 1137 return (0); 1138 } 1139 1140 static int 1141 udl_cmd_buf_copy_le16(struct udl_softc *sc, uint32_t src, uint32_t dst, 1142 uint8_t pixels, int flags) 1143 { 1144 struct udl_cmd_buf *cb; 1145 1146 cb = udl_cmd_buf_alloc(sc, flags); 1147 if (cb == NULL) 1148 return (EAGAIN); 1149 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); 1156 1157 return (0); 1158 } 1159