1 /* $OpenBSD: udl.c,v 1.85 2016/11/06 12:58:01 mpi Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Marcus Glocker <mglocker@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * Driver for the ``DisplayLink DL-120 / DL-160'' graphic chips based 21 * on the reversed engineered specifications of Florian Echtler 22 * <floe@butterbrot.org>: 23 * 24 * http://floe.butterbrot.org/displaylink/doku.php 25 * 26 * This driver has been inspired by the cfxga(4) driver because we have 27 * to deal with similar challenges, like no direct access to the video 28 * memory. 29 */ 30 31 #include <sys/param.h> 32 #include <sys/device.h> 33 #include <sys/kernel.h> 34 #include <sys/malloc.h> 35 #include <sys/systm.h> 36 37 #include <uvm/uvm_extern.h> 38 39 #include <dev/usb/usb.h> 40 #include <dev/usb/usbdi.h> 41 #include <dev/usb/usbdi_util.h> 42 #include <dev/usb/usbdevs.h> 43 44 #include <dev/wscons/wsconsio.h> 45 #include <dev/wscons/wsdisplayvar.h> 46 #include <dev/rasops/rasops.h> 47 48 #include <dev/videomode/videomode.h> 49 #include <dev/videomode/edidvar.h> 50 51 #include <dev/usb/udl.h> 52 #include <dev/usb/udlio.h> 53 54 /* 55 * Defines. 56 */ 57 #if 0 58 #define UDL_DEBUG 59 #endif 60 #ifdef UDL_DEBUG 61 int udl_debug = 1; 62 #define DPRINTF(l, x...) do { if ((l) <= udl_debug) printf(x); } while (0) 63 #else 64 #define DPRINTF(l, x...) 65 #endif 66 67 #define DN(sc) ((sc)->sc_dev.dv_xname) 68 #define FUNC __func__ 69 70 /* 71 * Prototypes. 72 */ 73 int udl_match(struct device *, void *, void *); 74 void udl_attach(struct device *, struct device *, void *); 75 void udl_attach_hook(struct device *); 76 int udl_detach(struct device *, int); 77 int udl_activate(struct device *, int); 78 79 int udl_ioctl(void *, u_long, caddr_t, int, struct proc *); 80 paddr_t udl_mmap(void *, off_t, int); 81 int udl_alloc_screen(void *, const struct wsscreen_descr *, 82 void **, int *, int *, long *); 83 void udl_free_screen(void *, void *); 84 int udl_show_screen(void *, void *, int, 85 void (*)(void *, int, int), void *); 86 int udl_load_font(void *, void *, struct wsdisplay_font *); 87 int udl_list_font(void *, struct wsdisplay_font *); 88 void udl_burner(void *, u_int, u_int); 89 90 int udl_copycols(void *, int, int, int, int); 91 int udl_copyrows(void *, int, int, int); 92 int udl_erasecols(void *, int, int, int, long); 93 int udl_eraserows(void *, int, int, long); 94 int udl_putchar(void *, int, int, u_int, long); 95 int udl_do_cursor(struct rasops_info *); 96 int udl_draw_char(struct udl_softc *, uint16_t, uint16_t, u_int, 97 uint32_t, uint32_t); 98 int udl_damage(struct udl_softc *, uint8_t *, 99 uint32_t, uint32_t, uint32_t, uint32_t); 100 int udl_draw_image(struct udl_softc *, uint8_t *, 101 uint32_t, uint32_t, uint32_t, uint32_t); 102 103 usbd_status udl_ctrl_msg(struct udl_softc *, uint8_t, uint8_t, 104 uint16_t, uint16_t, uint8_t *, size_t); 105 usbd_status udl_poll(struct udl_softc *, uint32_t *); 106 usbd_status udl_read_1(struct udl_softc *, uint16_t, uint8_t *); 107 usbd_status udl_write_1(struct udl_softc *, uint16_t, uint8_t); 108 usbd_status udl_read_edid(struct udl_softc *, uint8_t *); 109 uint8_t udl_lookup_mode(uint16_t, uint16_t, uint8_t, uint16_t, 110 uint32_t); 111 int udl_select_chip(struct udl_softc *); 112 usbd_status udl_set_enc_key(struct udl_softc *, uint8_t *, uint8_t); 113 usbd_status udl_set_decomp_table(struct udl_softc *, uint8_t *, uint16_t); 114 115 int udl_load_huffman(struct udl_softc *); 116 void udl_free_huffman(struct udl_softc *); 117 int udl_fbmem_alloc(struct udl_softc *); 118 void udl_fbmem_free(struct udl_softc *); 119 usbd_status udl_cmd_alloc_xfer(struct udl_softc *); 120 void udl_cmd_free_xfer(struct udl_softc *); 121 int udl_cmd_alloc_buf(struct udl_softc *); 122 void udl_cmd_free_buf(struct udl_softc *); 123 void udl_cmd_insert_int_1(struct udl_softc *, uint8_t); 124 void udl_cmd_insert_int_2(struct udl_softc *, uint16_t); 125 void udl_cmd_insert_int_3(struct udl_softc *, uint32_t); 126 void udl_cmd_insert_int_4(struct udl_softc *, uint32_t); 127 void udl_cmd_insert_buf(struct udl_softc *, uint8_t *, uint32_t); 128 int udl_cmd_insert_buf_comp(struct udl_softc *, uint8_t *, 129 uint32_t); 130 int udl_cmd_insert_head_comp(struct udl_softc *, uint32_t); 131 int udl_cmd_insert_check(struct udl_softc *, int); 132 void udl_cmd_set_xfer_type(struct udl_softc *, int); 133 void udl_cmd_save_offset(struct udl_softc *); 134 void udl_cmd_restore_offset(struct udl_softc *); 135 void udl_cmd_write_reg_1(struct udl_softc *, uint8_t, uint8_t); 136 void udl_cmd_write_reg_3(struct udl_softc *, uint8_t, uint32_t); 137 usbd_status udl_cmd_send(struct udl_softc *); 138 usbd_status udl_cmd_send_async(struct udl_softc *); 139 void udl_cmd_send_async_cb(struct usbd_xfer *, void *, usbd_status); 140 141 usbd_status udl_init_chip(struct udl_softc *); 142 void udl_init_fb_offsets(struct udl_softc *, uint32_t, uint32_t, 143 uint32_t, uint32_t); 144 usbd_status udl_init_resolution(struct udl_softc *); 145 usbd_status udl_clear_screen(struct udl_softc *); 146 void udl_select_mode(struct udl_softc *); 147 int udl_fb_buf_write(struct udl_softc *, uint8_t *, uint32_t, 148 uint32_t, uint16_t); 149 int udl_fb_block_write(struct udl_softc *, uint16_t, uint32_t, 150 uint32_t, uint32_t, uint32_t); 151 int udl_fb_line_write(struct udl_softc *, uint16_t, uint32_t, 152 uint32_t, uint32_t); 153 int udl_fb_off_write(struct udl_softc *, uint16_t, uint32_t, 154 uint16_t); 155 int udl_fb_block_copy(struct udl_softc *, uint32_t, uint32_t, 156 uint32_t, uint32_t, uint32_t, uint32_t); 157 int udl_fb_line_copy(struct udl_softc *, uint32_t, uint32_t, 158 uint32_t, uint32_t, uint32_t); 159 int udl_fb_off_copy(struct udl_softc *, uint32_t, uint32_t, 160 uint16_t); 161 int udl_fb_buf_write_comp(struct udl_softc *, uint8_t *, uint32_t, 162 uint32_t, uint16_t); 163 int udl_fb_block_write_comp(struct udl_softc *, uint16_t, uint32_t, 164 uint32_t, uint32_t, uint32_t); 165 int udl_fb_line_write_comp(struct udl_softc *, uint16_t, uint32_t, 166 uint32_t, uint32_t); 167 int udl_fb_off_write_comp(struct udl_softc *, uint16_t, uint32_t, 168 uint16_t); 169 int udl_fb_block_copy_comp(struct udl_softc *, uint32_t, uint32_t, 170 uint32_t, uint32_t, uint32_t, uint32_t); 171 int udl_fb_line_copy_comp(struct udl_softc *, uint32_t, uint32_t, 172 uint32_t, uint32_t, uint32_t); 173 int udl_fb_off_copy_comp(struct udl_softc *, uint32_t, uint32_t, 174 uint16_t); 175 #ifdef UDL_DEBUG 176 void udl_hexdump(void *, int, int); 177 usbd_status udl_init_test(struct udl_softc *); 178 #endif 179 180 /* 181 * Driver glue. 182 */ 183 struct cfdriver udl_cd = { 184 NULL, "udl", DV_DULL 185 }; 186 187 const struct cfattach udl_ca = { 188 sizeof(struct udl_softc), 189 udl_match, 190 udl_attach, 191 udl_detach, 192 udl_activate 193 }; 194 195 /* 196 * wsdisplay glue. 197 */ 198 struct wsscreen_descr udl_stdscreen = { 199 "std", /* name */ 200 0, 0, /* ncols, nrows */ 201 NULL, /* textops */ 202 0, 0, /* fontwidth, fontheight */ 203 WSSCREEN_WSCOLORS /* capabilities */ 204 }; 205 206 const struct wsscreen_descr *udl_scrlist[] = { 207 &udl_stdscreen 208 }; 209 210 struct wsscreen_list udl_screenlist = { 211 sizeof(udl_scrlist) / sizeof(struct wsscreen_descr *), udl_scrlist 212 }; 213 214 struct wsdisplay_accessops udl_accessops = { 215 .ioctl = udl_ioctl, 216 .mmap = udl_mmap, 217 .alloc_screen = udl_alloc_screen, 218 .free_screen = udl_free_screen, 219 .show_screen = udl_show_screen, 220 .load_font = udl_load_font, 221 .list_font = udl_list_font, 222 .burn_screen = udl_burner 223 }; 224 225 /* 226 * Matching devices. 227 */ 228 struct udl_type { 229 struct usb_devno udl_dev; 230 uint16_t udl_chip; 231 }; 232 233 static const struct udl_type udl_devs[] = { 234 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD4300U }, DL120 }, 235 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD8000U }, DL120 }, 236 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_GUC2020 }, DL160 }, 237 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LD220 }, DL165 }, 238 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_VCUD60 }, DL160 }, 239 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_DLDVI }, DL160 }, 240 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_VGA10 }, DL120 }, 241 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_WSDVI }, DLUNK }, 242 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_EC008 }, DL160 }, 243 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_HPDOCK }, DL160 }, 244 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_NL571 }, DL160 }, 245 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_M01061 }, DL195 }, 246 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_NBDOCK }, DL165 }, 247 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_SWDVI }, DLUNK }, 248 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_UM7X0 }, DL120 }, 249 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_CONV }, DL160 }, 250 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LUM70 }, DL125 }, 251 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_POLARIS2 }, DLUNK }, 252 { { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LT1421 }, DLUNK } 253 }; 254 #define udl_lookup(v, p) ((struct udl_type *)usb_lookup(udl_devs, v, p)) 255 256 int 257 udl_match(struct device *parent, void *match, void *aux) 258 { 259 struct usb_attach_arg *uaa = aux; 260 261 if (uaa->iface == NULL || uaa->configno != 1) 262 return (UMATCH_NONE); 263 264 if (udl_lookup(uaa->vendor, uaa->product) != NULL) 265 return (UMATCH_VENDOR_PRODUCT); 266 267 return (UMATCH_NONE); 268 } 269 270 void 271 udl_attach(struct device *parent, struct device *self, void *aux) 272 { 273 struct udl_softc *sc = (struct udl_softc *)self; 274 struct usb_attach_arg *uaa = aux; 275 struct wsemuldisplaydev_attach_args aa; 276 usbd_status error; 277 int err, i; 278 279 sc->sc_udev = uaa->device; 280 sc->sc_chip = udl_lookup(uaa->vendor, uaa->product)->udl_chip; 281 sc->sc_width = 0; 282 sc->sc_height = 0; 283 sc->sc_depth = 16; 284 sc->sc_cur_mode = MAX_DL_MODES; 285 286 /* 287 * Override chip if requested. 288 */ 289 if ((sc->sc_dev.dv_cfdata->cf_flags & 0xff00) > 0) { 290 i = ((sc->sc_dev.dv_cfdata->cf_flags & 0xff00) >> 8) - 1; 291 if (i <= DLMAX) { 292 sc->sc_chip = i; 293 printf("%s: %s: cf_flags (0x%04x) forced chip to %d\n", 294 DN(sc), FUNC, 295 sc->sc_dev.dv_cfdata->cf_flags, i); 296 } 297 } 298 299 /* 300 * The product might have more than one chip 301 */ 302 if (sc->sc_chip == DLUNK) 303 if (udl_select_chip(sc)) 304 return; 305 306 307 /* 308 * Create device handle to interface descriptor. 309 */ 310 error = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface); 311 if (error != USBD_NORMAL_COMPLETION) 312 return; 313 314 /* 315 * Allocate bulk command xfer. 316 */ 317 error = udl_cmd_alloc_xfer(sc); 318 if (error != USBD_NORMAL_COMPLETION) 319 return; 320 321 /* 322 * Allocate command buffer. 323 */ 324 err = udl_cmd_alloc_buf(sc); 325 if (err != 0) 326 return; 327 328 /* 329 * Open bulk TX pipe. 330 */ 331 error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE, 332 &sc->sc_tx_pipeh); 333 if (error != USBD_NORMAL_COMPLETION) 334 return; 335 336 /* 337 * Device initialization is done per synchronous xfers. 338 */ 339 udl_cmd_set_xfer_type(sc, UDL_CMD_XFER_SYNC); 340 341 /* 342 * Initialize chip. 343 */ 344 error = udl_init_chip(sc); 345 if (error != USBD_NORMAL_COMPLETION) 346 return; 347 348 /* 349 * Select edid mode. 350 */ 351 udl_select_mode(sc); 352 353 /* 354 * Override mode if requested. 355 */ 356 if ((sc->sc_dev.dv_cfdata->cf_flags & 0xff) > 0) { 357 i = (sc->sc_dev.dv_cfdata->cf_flags & 0xff) - 1; 358 359 if (i < MAX_DL_MODES) { 360 if (udl_modes[i].chip <= sc->sc_chip) { 361 sc->sc_width = udl_modes[i].hdisplay; 362 sc->sc_height = udl_modes[i].vdisplay; 363 printf("%s: %s: cf_flags (0x%04x) ", 364 DN(sc), FUNC, 365 sc->sc_dev.dv_cfdata->cf_flags); 366 printf("forced mode to %d\n", i); 367 sc->sc_cur_mode = i; 368 } 369 } 370 } 371 372 error = udl_init_resolution(sc); 373 if (error != USBD_NORMAL_COMPLETION) 374 return; 375 376 /* 377 * Attach wsdisplay. 378 */ 379 aa.console = 0; 380 aa.scrdata = &udl_screenlist; 381 aa.accessops = &udl_accessops; 382 aa.accesscookie = sc; 383 aa.defaultscreens = 0; 384 385 sc->sc_wsdisplay = config_found(self, &aa, wsemuldisplaydevprint); 386 387 /* 388 * Load Huffman table. 389 */ 390 config_mountroot(self, udl_attach_hook); 391 } 392 393 void 394 udl_attach_hook(struct device *self) 395 { 396 struct udl_softc *sc = (struct udl_softc *)self; 397 398 if (udl_load_huffman(sc) != 0) { 399 /* compression not possible */ 400 printf("%s: run in uncompressed mode\n", DN(sc)); 401 sc->udl_fb_buf_write = udl_fb_buf_write; 402 sc->udl_fb_block_write = udl_fb_block_write; 403 sc->udl_fb_line_write = udl_fb_line_write; 404 sc->udl_fb_off_write = udl_fb_off_write; 405 sc->udl_fb_block_copy = udl_fb_block_copy; 406 sc->udl_fb_line_copy = udl_fb_line_copy; 407 sc->udl_fb_off_copy = udl_fb_off_copy; 408 } else { 409 /* compression possible */ 410 sc->udl_fb_buf_write = udl_fb_buf_write_comp; 411 sc->udl_fb_block_write = udl_fb_block_write_comp; 412 sc->udl_fb_line_write = udl_fb_line_write_comp; 413 sc->udl_fb_off_write = udl_fb_off_write_comp; 414 sc->udl_fb_block_copy = udl_fb_block_copy_comp; 415 sc->udl_fb_line_copy = udl_fb_line_copy_comp; 416 sc->udl_fb_off_copy = udl_fb_off_copy_comp; 417 } 418 #ifdef UDL_DEBUG 419 if (udl_debug >= 4) 420 udl_init_test(sc); 421 #endif 422 /* 423 * From this point on we do asynchronous xfers. 424 */ 425 udl_cmd_set_xfer_type(sc, UDL_CMD_XFER_ASYNC); 426 427 /* 428 * Set initial wsdisplay emulation mode. 429 */ 430 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 431 } 432 433 int 434 udl_detach(struct device *self, int flags) 435 { 436 struct udl_softc *sc = (struct udl_softc *)self; 437 438 /* 439 * Close bulk TX pipe. 440 */ 441 if (sc->sc_tx_pipeh != NULL) { 442 usbd_abort_pipe(sc->sc_tx_pipeh); 443 usbd_close_pipe(sc->sc_tx_pipeh); 444 } 445 446 /* 447 * Free command buffer. 448 */ 449 udl_cmd_free_buf(sc); 450 451 /* 452 * Free command xfer. 453 */ 454 udl_cmd_free_xfer(sc); 455 456 /* 457 * Free Huffman table. 458 */ 459 udl_free_huffman(sc); 460 461 /* 462 * Free framebuffer memory. 463 */ 464 udl_fbmem_free(sc); 465 466 /* 467 * Detach wsdisplay. 468 */ 469 if (sc->sc_wsdisplay != NULL) 470 config_detach(sc->sc_wsdisplay, DETACH_FORCE); 471 472 return (0); 473 } 474 475 int 476 udl_activate(struct device *self, int act) 477 { 478 struct udl_softc *sc = (struct udl_softc *)self; 479 int rv; 480 481 switch (act) { 482 case DVACT_DEACTIVATE: 483 usbd_deactivate(sc->sc_udev); 484 break; 485 } 486 rv = config_activate_children(self, act); 487 return (rv); 488 } 489 490 /* ---------- */ 491 492 int 493 udl_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p) 494 { 495 struct udl_softc *sc; 496 struct wsdisplay_fbinfo *wdf; 497 struct udl_ioctl_damage *d; 498 int r, error, mode; 499 500 sc = v; 501 502 DPRINTF(1, "%s: %s: ('%c', %d, %d)\n", 503 DN(sc), FUNC, IOCGROUP(cmd), cmd & 0xff, IOCPARM_LEN(cmd)); 504 505 switch (cmd) { 506 case WSDISPLAYIO_GTYPE: 507 *(u_int *)data = WSDISPLAY_TYPE_DL; 508 break; 509 case WSDISPLAYIO_GINFO: 510 wdf = (struct wsdisplay_fbinfo *)data; 511 wdf->height = sc->sc_height; 512 wdf->width = sc->sc_width; 513 wdf->depth = sc->sc_depth; 514 wdf->cmsize = 0; /* XXX fill up colormap size */ 515 break; 516 case WSDISPLAYIO_SMODE: 517 mode = *(u_int *)data; 518 if (mode == sc->sc_mode) 519 break; 520 switch (mode) { 521 case WSDISPLAYIO_MODE_EMUL: 522 /* clear screen */ 523 (void)udl_clear_screen(sc); 524 break; 525 case WSDISPLAYIO_MODE_DUMBFB: 526 /* TODO */ 527 break; 528 default: 529 return (EINVAL); 530 } 531 sc->sc_mode = mode; 532 break; 533 case WSDISPLAYIO_LINEBYTES: 534 *(u_int *)data = sc->sc_width * (sc->sc_depth / 8); 535 break; 536 case WSDISPLAYIO_SVIDEO: 537 case WSDISPLAYIO_GVIDEO: 538 /* handled for us by wscons */ 539 break; 540 case UDLIO_DAMAGE: 541 d = (struct udl_ioctl_damage *)data; 542 d->status = UDLIO_STATUS_OK; 543 r = udl_damage(sc, sc->sc_fbmem, d->x1, d->x2, d->y1, d->y2); 544 if (r != 0) { 545 error = tsleep(sc, 0, "udlio", hz / 100); 546 if (error) { 547 d->status = UDLIO_STATUS_FAILED; 548 } else { 549 r = udl_damage(sc, sc->sc_fbmem, d->x1, d->x2, 550 d->y1, d->y2); 551 if (r != 0) 552 d->status = UDLIO_STATUS_FAILED; 553 } 554 } 555 break; 556 default: 557 return (-1); 558 } 559 560 return (0); 561 } 562 563 paddr_t 564 udl_mmap(void *v, off_t off, int prot) 565 { 566 struct udl_softc *sc; 567 caddr_t p; 568 paddr_t pa; 569 570 sc = v; 571 572 DPRINTF(1, "%s: %s\n", DN(sc), FUNC); 573 574 /* allocate framebuffer memory */ 575 if (udl_fbmem_alloc(sc) == -1) 576 return (-1); 577 578 /* return memory address to userland process */ 579 p = sc->sc_fbmem + off; 580 if (pmap_extract(pmap_kernel(), (vaddr_t)p, &pa) == FALSE) { 581 printf("udl_mmap: invalid page\n"); 582 udl_fbmem_free(sc); 583 return (-1); 584 } 585 return (pa); 586 } 587 588 int 589 udl_alloc_screen(void *v, const struct wsscreen_descr *type, 590 void **cookiep, int *curxp, int *curyp, long *attrp) 591 { 592 struct udl_softc *sc = v; 593 struct wsdisplay_font *font; 594 595 DPRINTF(1, "%s: %s\n", DN(sc), FUNC); 596 597 if (sc->sc_nscreens > 0) 598 return (ENOMEM); 599 600 /* 601 * Initialize rasops. 602 */ 603 sc->sc_ri.ri_depth = sc->sc_depth; 604 sc->sc_ri.ri_bits = NULL; 605 sc->sc_ri.ri_width = sc->sc_width; 606 sc->sc_ri.ri_height = sc->sc_height; 607 sc->sc_ri.ri_stride = sc->sc_width * sc->sc_height / 8; 608 sc->sc_ri.ri_hw = (void *)sc; 609 sc->sc_ri.ri_flg = 0; 610 611 /* swap B and R at 16 bpp */ 612 if (sc->sc_depth == 16) { 613 sc->sc_ri.ri_rnum = 5; 614 sc->sc_ri.ri_rpos = 11; 615 sc->sc_ri.ri_gnum = 6; 616 sc->sc_ri.ri_gpos = 5; 617 sc->sc_ri.ri_bnum = 5; 618 sc->sc_ri.ri_bpos = 0; 619 } 620 621 rasops_init(&sc->sc_ri, 100, 200); 622 623 sc->sc_ri.ri_ops.copycols = udl_copycols; 624 sc->sc_ri.ri_ops.copyrows = udl_copyrows; 625 sc->sc_ri.ri_ops.erasecols = udl_erasecols; 626 sc->sc_ri.ri_ops.eraserows = udl_eraserows; 627 sc->sc_ri.ri_ops.putchar = udl_putchar; 628 sc->sc_ri.ri_do_cursor = udl_do_cursor; 629 630 sc->sc_ri.ri_ops.alloc_attr(&sc->sc_ri, 0, 0, 0, attrp); 631 632 udl_stdscreen.nrows = sc->sc_ri.ri_rows; 633 udl_stdscreen.ncols = sc->sc_ri.ri_cols; 634 udl_stdscreen.textops = &sc->sc_ri.ri_ops; 635 udl_stdscreen.fontwidth = sc->sc_ri.ri_font->fontwidth; 636 udl_stdscreen.fontheight = sc->sc_ri.ri_font->fontheight; 637 udl_stdscreen.capabilities = sc->sc_ri.ri_caps; 638 639 *cookiep = &sc->sc_ri; 640 *curxp = 0; 641 *curyp = 0; 642 643 /* allocate character backing store */ 644 sc->sc_cbs = mallocarray(sc->sc_ri.ri_rows, sc->sc_ri.ri_cols * 645 sizeof(*sc->sc_cbs), M_DEVBUF, M_NOWAIT|M_ZERO); 646 if (sc->sc_cbs == NULL) { 647 printf("%s: can't allocate mem for character backing store!\n", 648 DN(sc)); 649 return (ENOMEM); 650 } 651 652 sc->sc_nscreens++; 653 654 font = sc->sc_ri.ri_font; 655 DPRINTF(1, "%s: %s: using font %s (%dx%d)\n", 656 DN(sc), FUNC, font->name, sc->sc_ri.ri_cols, sc->sc_ri.ri_rows); 657 658 return (0); 659 } 660 661 void 662 udl_free_screen(void *v, void *cookie) 663 { 664 struct udl_softc *sc; 665 666 sc = v; 667 668 DPRINTF(1, "%s: %s\n", DN(sc), FUNC); 669 670 /* free character backing store */ 671 if (sc->sc_cbs != NULL) 672 free(sc->sc_cbs, M_DEVBUF, 0); 673 674 sc->sc_nscreens--; 675 } 676 677 int 678 udl_show_screen(void *v, void *cookie, int waitok, 679 void (*cb)(void *, int, int), void *cbarg) 680 { 681 struct udl_softc *sc; 682 683 sc = v; 684 685 DPRINTF(1, "%s: %s\n", DN(sc), FUNC); 686 687 return (0); 688 } 689 690 int 691 udl_load_font(void *v, void *emulcookie, struct wsdisplay_font *font) 692 { 693 struct udl_softc *sc = v; 694 struct rasops_info *ri = &sc->sc_ri; 695 696 return rasops_load_font(ri, emulcookie, font); 697 } 698 699 int 700 udl_list_font(void *v, struct wsdisplay_font *font) 701 { 702 struct udl_softc *sc = v; 703 struct rasops_info *ri = &sc->sc_ri; 704 705 return rasops_list_font(ri, font); 706 } 707 708 void 709 udl_burner(void *v, u_int on, u_int flags) 710 { 711 struct udl_softc *sc; 712 713 sc = v; 714 715 DPRINTF(1, "%s: %s: screen %s\n", DN(sc), FUNC, on ? "ON" : "OFF"); 716 717 if (on) 718 udl_cmd_write_reg_1(sc, UDL_REG_SCREEN, UDL_REG_SCREEN_ON); 719 else 720 udl_cmd_write_reg_1(sc, UDL_REG_SCREEN, UDL_REG_SCREEN_OFF); 721 722 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0xff); 723 724 (void)udl_cmd_send_async(sc); 725 } 726 727 /* ---------- */ 728 729 int 730 udl_copycols(void *cookie, int row, int src, int dst, int num) 731 { 732 struct rasops_info *ri = cookie; 733 struct udl_softc *sc; 734 int sx, sy, dx, dy, cx, cy, r; 735 usbd_status error; 736 737 sc = ri->ri_hw; 738 739 DPRINTF(2, "%s: %s: row=%d, src=%d, dst=%d, num=%d\n", 740 DN(sc), FUNC, row, src, dst, num); 741 742 udl_cmd_save_offset(sc); 743 744 sx = src * ri->ri_font->fontwidth; 745 sy = row * ri->ri_font->fontheight; 746 dx = dst * ri->ri_font->fontwidth; 747 dy = row * ri->ri_font->fontheight; 748 cx = num * ri->ri_font->fontwidth; 749 cy = ri->ri_font->fontheight; 750 751 /* copy row block to off-screen first to fix overlay-copy problem */ 752 r = (sc->udl_fb_block_copy) 753 (sc, sx, sy, 0, sc->sc_ri.ri_emuheight, cx, cy); 754 if (r != 0) 755 goto fail; 756 757 /* copy row block back from off-screen now */ 758 r = (sc->udl_fb_block_copy) 759 (sc, 0, sc->sc_ri.ri_emuheight, dx, dy, cx, cy); 760 if (r != 0) 761 goto fail; 762 763 error = udl_cmd_send_async(sc); 764 if (error != USBD_NORMAL_COMPLETION) { 765 fail: 766 udl_cmd_restore_offset(sc); 767 return (EAGAIN); 768 } 769 770 /* update character backing store */ 771 bcopy(sc->sc_cbs + ((row * sc->sc_ri.ri_cols) + src), 772 sc->sc_cbs + ((row * sc->sc_ri.ri_cols) + dst), 773 num * sizeof(*sc->sc_cbs)); 774 775 return (0); 776 } 777 778 int 779 udl_copyrows(void *cookie, int src, int dst, int num) 780 { 781 struct rasops_info *ri = cookie; 782 struct udl_softc *sc; 783 int sy, dy, cx, cy, r; 784 usbd_status error; 785 786 sc = ri->ri_hw; 787 788 DPRINTF(2, "%s: %s: src=%d, dst=%d, num=%d\n", 789 DN(sc), FUNC, src, dst, num); 790 791 udl_cmd_save_offset(sc); 792 793 sy = src * sc->sc_ri.ri_font->fontheight; 794 dy = dst * sc->sc_ri.ri_font->fontheight; 795 cx = sc->sc_ri.ri_emuwidth; 796 cy = num * sc->sc_ri.ri_font->fontheight; 797 798 /* copy row block to off-screen first to fix overlay-copy problem */ 799 r = (sc->udl_fb_block_copy) 800 (sc, 0, sy, 0, sc->sc_ri.ri_emuheight, cx, cy); 801 if (r != 0) 802 goto fail; 803 804 /* copy row block back from off-screen now */ 805 r = (sc->udl_fb_block_copy) 806 (sc, 0, sc->sc_ri.ri_emuheight, 0, dy, cx, cy); 807 if (r != 0) 808 goto fail; 809 810 error = udl_cmd_send_async(sc); 811 if (error != USBD_NORMAL_COMPLETION) { 812 fail: 813 udl_cmd_restore_offset(sc); 814 return (EAGAIN); 815 } 816 817 /* update character backing store */ 818 bcopy(sc->sc_cbs + (src * sc->sc_ri.ri_cols), 819 sc->sc_cbs + (dst * sc->sc_ri.ri_cols), 820 (num * sc->sc_ri.ri_cols) * sizeof(*sc->sc_cbs)); 821 822 return (0); 823 } 824 825 int 826 udl_erasecols(void *cookie, int row, int col, int num, long attr) 827 { 828 struct rasops_info *ri = cookie; 829 struct udl_softc *sc = ri->ri_hw; 830 uint16_t bgc; 831 int fg, bg; 832 int x, y, cx, cy, r; 833 usbd_status error; 834 835 sc = ri->ri_hw; 836 837 DPRINTF(2, "%s: %s: row=%d, col=%d, num=%d\n", 838 DN(sc), FUNC, row, col, num); 839 840 udl_cmd_save_offset(sc); 841 842 sc->sc_ri.ri_ops.unpack_attr(cookie, attr, &fg, &bg, NULL); 843 bgc = (uint16_t)sc->sc_ri.ri_devcmap[bg]; 844 845 x = col * sc->sc_ri.ri_font->fontwidth; 846 y = row * sc->sc_ri.ri_font->fontheight; 847 cx = num * sc->sc_ri.ri_font->fontwidth; 848 cy = sc->sc_ri.ri_font->fontheight; 849 850 r = (sc->udl_fb_block_write)(sc, bgc, x, y, cx, cy); 851 if (r != 0) 852 goto fail; 853 854 error = udl_cmd_send_async(sc); 855 if (error != USBD_NORMAL_COMPLETION) { 856 fail: 857 udl_cmd_restore_offset(sc); 858 return (EAGAIN); 859 } 860 861 /* update character backing store */ 862 bzero(sc->sc_cbs + ((row * sc->sc_ri.ri_cols) + col), 863 num * sizeof(*sc->sc_cbs)); 864 865 return (0); 866 } 867 868 int 869 udl_eraserows(void *cookie, int row, int num, long attr) 870 { 871 struct rasops_info *ri = cookie; 872 struct udl_softc *sc; 873 uint16_t bgc; 874 int fg, bg; 875 int x, y, cx, cy, r; 876 usbd_status error; 877 878 sc = ri->ri_hw; 879 880 DPRINTF(2, "%s: %s: row=%d, num=%d\n", DN(sc), FUNC, row, num); 881 882 udl_cmd_save_offset(sc); 883 884 sc->sc_ri.ri_ops.unpack_attr(cookie, attr, &fg, &bg, NULL); 885 bgc = (uint16_t)sc->sc_ri.ri_devcmap[bg]; 886 887 x = 0; 888 y = row * sc->sc_ri.ri_font->fontheight; 889 cx = sc->sc_ri.ri_emuwidth; 890 cy = num * sc->sc_ri.ri_font->fontheight; 891 892 r = (sc->udl_fb_block_write)(sc, bgc, x, y, cx, cy); 893 if (r != 0) 894 goto fail; 895 896 error = udl_cmd_send_async(sc); 897 if (error != USBD_NORMAL_COMPLETION) { 898 fail: 899 udl_cmd_restore_offset(sc); 900 return (EAGAIN); 901 } 902 903 /* update character backing store */ 904 bzero(sc->sc_cbs + (row * sc->sc_ri.ri_cols), 905 (num * sc->sc_ri.ri_cols) * sizeof(*sc->sc_cbs)); 906 907 return (0); 908 } 909 910 int 911 udl_putchar(void *cookie, int row, int col, u_int uc, long attr) 912 { 913 struct rasops_info *ri = cookie; 914 struct udl_softc *sc = ri->ri_hw; 915 int r; 916 uint16_t fgc, bgc; 917 uint32_t x, y, fg, bg; 918 919 DPRINTF(4, "%s: %s\n", DN(sc), FUNC); 920 921 udl_cmd_save_offset(sc); 922 923 sc->sc_ri.ri_ops.unpack_attr(cookie, attr, &fg, &bg, NULL); 924 fgc = (uint16_t)sc->sc_ri.ri_devcmap[fg]; 925 bgc = (uint16_t)sc->sc_ri.ri_devcmap[bg]; 926 927 x = col * ri->ri_font->fontwidth; 928 y = row * ri->ri_font->fontheight; 929 930 if (uc == ' ') { 931 /* 932 * Writting a block for the space character instead rendering 933 * it from font bits is more slim. 934 */ 935 r = (sc->udl_fb_block_write)(sc, bgc, x, y, 936 ri->ri_font->fontwidth, ri->ri_font->fontheight); 937 if (r != 0) 938 goto fail; 939 } else { 940 /* render a character from font bits */ 941 r = udl_draw_char(sc, fgc, bgc, uc, x, y); 942 if (r != 0) 943 goto fail; 944 } 945 946 /* 947 * We don't call udl_cmd_send_async() here, since sending each 948 * character by itself gets the performance down bad. Instead the 949 * character will be buffered until another rasops function flush 950 * the buffer. 951 */ 952 953 /* update character backing store */ 954 sc->sc_cbs[(row * sc->sc_ri.ri_cols) + col] = uc; 955 956 return (0); 957 958 fail: 959 udl_cmd_restore_offset(sc); 960 return (EAGAIN); 961 } 962 963 int 964 udl_do_cursor(struct rasops_info *ri) 965 { 966 struct udl_softc *sc = ri->ri_hw; 967 int r, pos; 968 uint32_t x, y; 969 uint8_t save_cursor; 970 usbd_status error; 971 972 DPRINTF(2, "%s: %s: ccol=%d, crow=%d\n", 973 DN(sc), FUNC, ri->ri_ccol, ri->ri_crow); 974 975 udl_cmd_save_offset(sc); 976 save_cursor = sc->sc_cursor_on; 977 978 x = ri->ri_ccol * ri->ri_font->fontwidth; 979 y = ri->ri_crow * ri->ri_font->fontheight; 980 981 if (sc->sc_cursor_on == 0) { 982 /* save the last character block to off-screen */ 983 r = (sc->udl_fb_block_copy)(sc, x, y, 0, sc->sc_ri.ri_emuheight, 984 ri->ri_font->fontwidth, ri->ri_font->fontheight); 985 if (r != 0) 986 goto fail; 987 988 /* draw cursor */ 989 pos = (ri->ri_crow * sc->sc_ri.ri_cols) + ri->ri_ccol; 990 if (sc->sc_cbs[pos] == 0 || sc->sc_cbs[pos] == ' ') { 991 r = (sc->udl_fb_block_write)(sc, 0xffff, x, y, 992 ri->ri_font->fontwidth, ri->ri_font->fontheight); 993 } else { 994 r = udl_draw_char(sc, 0x0000, 0xffff, sc->sc_cbs[pos], 995 x, y); 996 } 997 if (r != 0) 998 goto fail; 999 1000 sc->sc_cursor_on = 1; 1001 } else { 1002 /* restore the last saved character from off-screen */ 1003 r = (sc->udl_fb_block_copy)(sc, 0, sc->sc_ri.ri_emuheight, x, y, 1004 ri->ri_font->fontwidth, ri->ri_font->fontheight); 1005 if (r != 0) 1006 goto fail; 1007 1008 sc->sc_cursor_on = 0; 1009 } 1010 1011 error = udl_cmd_send_async(sc); 1012 if (error != USBD_NORMAL_COMPLETION) { 1013 fail: 1014 udl_cmd_restore_offset(sc); 1015 sc->sc_cursor_on = save_cursor; 1016 return (EAGAIN); 1017 } 1018 1019 return (0); 1020 } 1021 1022 int 1023 udl_draw_char(struct udl_softc *sc, uint16_t fg, uint16_t bg, u_int uc, 1024 uint32_t x, uint32_t y) 1025 { 1026 int i, j, ly, r; 1027 uint8_t *fontchar; 1028 uint8_t buf[UDL_CMD_MAX_DATA_SIZE]; 1029 uint16_t *line, lrgb16, fontbits, luc; 1030 struct wsdisplay_font *font = sc->sc_ri.ri_font; 1031 1032 fontchar = (uint8_t *)(font->data + (uc - font->firstchar) * 1033 sc->sc_ri.ri_fontscale); 1034 1035 ly = y; 1036 for (i = 0; i < font->fontheight; i++) { 1037 if (font->fontwidth > 8) { 1038 fontbits = betoh16(*(uint16_t *)fontchar); 1039 } else { 1040 fontbits = *fontchar; 1041 fontbits = fontbits << 8; 1042 } 1043 line = (uint16_t *)buf; 1044 1045 for (j = 15; j > (15 - font->fontwidth); j--) { 1046 luc = 1 << j; 1047 if (fontbits & luc) 1048 lrgb16 = htobe16(fg); 1049 else 1050 lrgb16 = htobe16(bg); 1051 bcopy(&lrgb16, line, 2); 1052 line++; 1053 } 1054 r = (sc->udl_fb_buf_write)(sc, buf, x, ly, font->fontwidth); 1055 if (r != 0) 1056 return (r); 1057 ly++; 1058 1059 fontchar += font->stride; 1060 } 1061 1062 return (0); 1063 } 1064 1065 int 1066 udl_damage(struct udl_softc *sc, uint8_t *image, 1067 uint32_t x1, uint32_t x2, uint32_t y1, uint32_t y2) 1068 { 1069 int r; 1070 int x, y, width, height; 1071 usbd_status error; 1072 1073 udl_cmd_save_offset(sc); 1074 1075 x = x1; 1076 y = y1; 1077 width = x2 - x1; 1078 height = y2 - y1; 1079 1080 r = udl_draw_image(sc, image, x, y, width, height); 1081 if (r != 0) 1082 goto fail; 1083 1084 error = udl_cmd_send_async(sc); 1085 if (error != USBD_NORMAL_COMPLETION) { 1086 fail: 1087 udl_cmd_restore_offset(sc); 1088 return (EAGAIN); 1089 } 1090 1091 return (0); 1092 } 1093 1094 int 1095 udl_draw_image(struct udl_softc *sc, uint8_t *image, 1096 uint32_t x, uint32_t y, uint32_t width, uint32_t height) 1097 { 1098 int i, j, r; 1099 int width_cur, x_cur; 1100 uint8_t buf[UDL_CMD_MAX_DATA_SIZE]; 1101 uint16_t *image16, lrgb16; 1102 uint32_t off, block; 1103 1104 for (i = 0; i < height; i++) { 1105 off = ((y * sc->sc_width) + x) * 2; 1106 x_cur = x; 1107 width_cur = width; 1108 1109 while (width_cur) { 1110 if (width_cur > UDL_CMD_MAX_PIXEL_COUNT) 1111 block = UDL_CMD_MAX_PIXEL_COUNT; 1112 else 1113 block = width_cur; 1114 1115 /* fix RGB ordering */ 1116 image16 = (uint16_t *)(image + off); 1117 for (j = 0; j < (block * 2); j += 2) { 1118 lrgb16 = htobe16(*image16); 1119 bcopy(&lrgb16, buf + j, 2); 1120 image16++; 1121 } 1122 1123 r = (sc->udl_fb_buf_write)(sc, buf, x_cur, y, block); 1124 if (r != 0) 1125 return (r); 1126 1127 off += block * 2; 1128 x_cur += block; 1129 width_cur -= block; 1130 } 1131 y++; 1132 } 1133 1134 return (0); 1135 } 1136 1137 /* ---------- */ 1138 1139 usbd_status 1140 udl_ctrl_msg(struct udl_softc *sc, uint8_t rt, uint8_t r, 1141 uint16_t index, uint16_t value, uint8_t *buf, size_t len) 1142 { 1143 usb_device_request_t req; 1144 usbd_status error; 1145 1146 req.bmRequestType = rt; 1147 req.bRequest = r; 1148 USETW(req.wIndex, index); 1149 USETW(req.wValue, value); 1150 USETW(req.wLength, len); 1151 1152 error = usbd_do_request(sc->sc_udev, &req, buf); 1153 if (error != USBD_NORMAL_COMPLETION) { 1154 printf("%s: %s: %s!\n", DN(sc), FUNC, usbd_errstr(error)); 1155 return (error); 1156 } 1157 1158 return (USBD_NORMAL_COMPLETION); 1159 } 1160 1161 usbd_status 1162 udl_poll(struct udl_softc *sc, uint32_t *buf) 1163 { 1164 uint8_t lbuf[4]; 1165 usbd_status error; 1166 1167 error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, 1168 UDL_CTRL_CMD_POLL, 0x0000, 0x0000, lbuf, 4); 1169 if (error != USBD_NORMAL_COMPLETION) { 1170 printf("%s: %s: %s!\n", DN(sc), FUNC, usbd_errstr(error)); 1171 return (error); 1172 } 1173 *buf = *(uint32_t *)lbuf; 1174 1175 return (USBD_NORMAL_COMPLETION); 1176 } 1177 1178 usbd_status 1179 udl_read_1(struct udl_softc *sc, uint16_t addr, uint8_t *buf) 1180 { 1181 uint8_t lbuf[1]; 1182 usbd_status error; 1183 1184 error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, 1185 UDL_CTRL_CMD_READ_1, addr, 0x0000, lbuf, 1); 1186 if (error != USBD_NORMAL_COMPLETION) { 1187 printf("%s: %s: %s!\n", DN(sc), FUNC, usbd_errstr(error)); 1188 return (error); 1189 } 1190 *buf = *(uint8_t *)lbuf; 1191 1192 return (USBD_NORMAL_COMPLETION); 1193 } 1194 1195 usbd_status 1196 udl_write_1(struct udl_softc *sc, uint16_t addr, uint8_t buf) 1197 { 1198 usbd_status error; 1199 1200 error = udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, 1201 UDL_CTRL_CMD_WRITE_1, addr, 0x0000, &buf, 1); 1202 if (error != USBD_NORMAL_COMPLETION) { 1203 printf("%s: %s: %s!\n", DN(sc), FUNC, usbd_errstr(error)); 1204 return (error); 1205 } 1206 1207 return (USBD_NORMAL_COMPLETION); 1208 } 1209 1210 usbd_status 1211 udl_read_edid(struct udl_softc *sc, uint8_t *buf) 1212 { 1213 uint8_t lbuf[64]; 1214 uint16_t offset; 1215 usbd_status error; 1216 1217 offset = 0; 1218 1219 error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, 1220 UDL_CTRL_CMD_READ_EDID, 0x00a1, (offset << 8), lbuf, 64); 1221 if (error != USBD_NORMAL_COMPLETION) 1222 goto fail; 1223 bcopy(lbuf + 1, buf + offset, 63); 1224 offset += 63; 1225 1226 error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, 1227 UDL_CTRL_CMD_READ_EDID, 0x00a1, (offset << 8), lbuf, 64); 1228 if (error != USBD_NORMAL_COMPLETION) 1229 goto fail; 1230 bcopy(lbuf + 1, buf + offset, 63); 1231 offset += 63; 1232 1233 error = udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, 1234 UDL_CTRL_CMD_READ_EDID, 0x00a1, (offset << 8), lbuf, 3); 1235 if (error != USBD_NORMAL_COMPLETION) 1236 goto fail; 1237 bcopy(lbuf + 1, buf + offset, 2); 1238 1239 return (USBD_NORMAL_COMPLETION); 1240 fail: 1241 printf("%s: %s: %s!\n", DN(sc), FUNC, usbd_errstr(error)); 1242 return (error); 1243 } 1244 1245 uint8_t 1246 udl_lookup_mode(uint16_t hdisplay, uint16_t vdisplay, uint8_t hz, 1247 uint16_t chip, uint32_t clock) 1248 { 1249 uint8_t idx = 0; 1250 1251 /* 1252 * Check first if we have a matching mode with pixelclock 1253 */ 1254 while (idx < MAX_DL_MODES) { 1255 if ((udl_modes[idx].hdisplay == hdisplay) && 1256 (udl_modes[idx].vdisplay == vdisplay) && 1257 (udl_modes[idx].clock == clock) && 1258 (udl_modes[idx].chip <= chip)) { 1259 return(idx); 1260 } 1261 idx++; 1262 } 1263 1264 /* 1265 * If not, check for matching mode with update frequency 1266 */ 1267 idx = 0; 1268 while (idx < MAX_DL_MODES) { 1269 if ((udl_modes[idx].hdisplay == hdisplay) && 1270 (udl_modes[idx].vdisplay == vdisplay) && 1271 (udl_modes[idx].hz == hz) && 1272 (udl_modes[idx].chip <= chip)) { 1273 return(idx); 1274 } 1275 idx++; 1276 } 1277 1278 return(idx); 1279 } 1280 1281 int 1282 udl_select_chip(struct udl_softc *sc) 1283 { 1284 char serialnum[USB_MAX_STRING_LEN]; 1285 usb_device_descriptor_t *dd; 1286 usb_string_descriptor_t us; 1287 usbd_status error; 1288 int len, i, n; 1289 char *s; 1290 uint16_t c; 1291 1292 sc->sc_chip = DL120; 1293 1294 dd = usbd_get_device_descriptor(sc->sc_udev); 1295 1296 if ((UGETW(dd->idVendor) == USB_VENDOR_DISPLAYLINK) && 1297 (UGETW(dd->idProduct) == USB_PRODUCT_DISPLAYLINK_WSDVI)) { 1298 1299 /* 1300 * WS Tech DVI is DL120 or DL160. All deviced uses the 1301 * same revision (0.04) so iSerialNumber must be used 1302 * to determin which chip it is. 1303 */ 1304 1305 bzero(serialnum, sizeof serialnum); 1306 error = usbd_get_string_desc(sc->sc_udev, dd->iSerialNumber, 1307 0, &us, &len); 1308 if (error != USBD_NORMAL_COMPLETION) 1309 return (1); 1310 1311 s = &serialnum[0]; 1312 n = len / 2 - 1; 1313 for (i = 0; i < n && i < USB_MAX_STRING_LEN; i++) { 1314 c = UGETW(us.bString[i]); 1315 /* Convert from Unicode, handle buggy strings. */ 1316 if ((c & 0xff00) == 0) 1317 *s++ = c; 1318 else if ((c & 0x00ff) == 0) 1319 *s++ = c >> 8; 1320 else 1321 *s++ = '?'; 1322 } 1323 *s++ = 0; 1324 1325 if (strlen(serialnum) > 7) 1326 if (strncmp(serialnum, "0198-13", 7) == 0) 1327 sc->sc_chip = DL160; 1328 1329 DPRINTF(1, "%s: %s: iSerialNumber (%s) used to select chip (%d)\n", 1330 DN(sc), FUNC, serialnum, sc->sc_chip); 1331 1332 } 1333 1334 if ((UGETW(dd->idVendor) == USB_VENDOR_DISPLAYLINK) && 1335 (UGETW(dd->idProduct) == USB_PRODUCT_DISPLAYLINK_SWDVI)) { 1336 1337 /* 1338 * SUNWEIT DVI is DL160, DL125, DL165 or DL195. Major revision 1339 * can be used to differ between DL1x0 and DL1x5. Minor to 1340 * differ between DL1x5. iSerialNumber seems not to be uniqe. 1341 */ 1342 1343 sc->sc_chip = DL160; 1344 1345 if (UGETW(dd->bcdDevice) >= 0x100) { 1346 sc->sc_chip = DL165; 1347 if (UGETW(dd->bcdDevice) == 0x104) 1348 sc->sc_chip = DL195; 1349 if (UGETW(dd->bcdDevice) == 0x108) 1350 sc->sc_chip = DL125; 1351 } 1352 1353 DPRINTF(1, "%s: %s: bcdDevice (%02x) used to select chip (%d)\n", 1354 DN(sc), FUNC, UGETW(dd->bcdDevice), sc->sc_chip); 1355 1356 } 1357 1358 return (0); 1359 } 1360 1361 usbd_status 1362 udl_set_enc_key(struct udl_softc *sc, uint8_t *buf, uint8_t len) 1363 { 1364 usbd_status error; 1365 1366 error = udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, 1367 UDL_CTRL_CMD_SET_KEY, 0x0000, 0x0000, buf, len); 1368 if (error != USBD_NORMAL_COMPLETION) { 1369 printf("%s: %s: %s!\n", DN(sc), FUNC, usbd_errstr(error)); 1370 return (error); 1371 } 1372 1373 return (USBD_NORMAL_COMPLETION); 1374 } 1375 1376 usbd_status 1377 udl_set_decomp_table(struct udl_softc *sc, uint8_t *buf, uint16_t len) 1378 { 1379 int err; 1380 1381 udl_cmd_insert_int_1(sc, UDL_BULK_SOC); 1382 udl_cmd_insert_int_1(sc, UDL_BULK_CMD_DECOMP); 1383 udl_cmd_insert_int_4(sc, 0x263871cd); /* magic number */ 1384 udl_cmd_insert_int_4(sc, 0x00000200); /* 512 byte chunks */ 1385 udl_cmd_insert_buf(sc, buf, len); 1386 1387 err = udl_cmd_send(sc); 1388 if (err != 0) 1389 return (USBD_INVAL); 1390 1391 return (USBD_NORMAL_COMPLETION); 1392 } 1393 1394 /* ---------- */ 1395 1396 int 1397 udl_load_huffman(struct udl_softc *sc) 1398 { 1399 const char *name = "udl_huffman"; 1400 int error; 1401 1402 if (sc->sc_huffman == NULL) { 1403 error = loadfirmware(name, &sc->sc_huffman, 1404 &sc->sc_huffman_size); 1405 if (error != 0) { 1406 printf("%s: error %d, could not read huffman table " 1407 "%s!\n", DN(sc), error, name); 1408 return (EIO); 1409 } 1410 } 1411 1412 DPRINTF(1, "%s: huffman table %s allocated\n", DN(sc), name); 1413 1414 return (0); 1415 } 1416 1417 void 1418 udl_free_huffman(struct udl_softc *sc) 1419 { 1420 if (sc->sc_huffman != NULL) { 1421 free(sc->sc_huffman, M_DEVBUF, 0); 1422 sc->sc_huffman = NULL; 1423 sc->sc_huffman_size = 0; 1424 DPRINTF(1, "%s: huffman table freed\n", DN(sc)); 1425 } 1426 } 1427 1428 int 1429 udl_fbmem_alloc(struct udl_softc *sc) 1430 { 1431 int size; 1432 1433 size = (sc->sc_width * sc->sc_height) * (sc->sc_depth / 8); 1434 size = round_page(size); 1435 1436 if (sc->sc_fbmem == NULL) { 1437 sc->sc_fbmem = malloc(size, M_DEVBUF, M_NOWAIT|M_ZERO); 1438 if (sc->sc_fbmem == NULL) 1439 return (-1); 1440 } 1441 1442 return (0); 1443 } 1444 1445 void 1446 udl_fbmem_free(struct udl_softc *sc) 1447 { 1448 if (sc->sc_fbmem != NULL) { 1449 free(sc->sc_fbmem, M_DEVBUF, 0); 1450 sc->sc_fbmem = NULL; 1451 } 1452 } 1453 1454 usbd_status 1455 udl_cmd_alloc_xfer(struct udl_softc *sc) 1456 { 1457 int i; 1458 1459 for (i = 0; i < UDL_CMD_XFER_COUNT; i++) { 1460 struct udl_cmd_xfer *cx = &sc->sc_cmd_xfer[i]; 1461 1462 cx->sc = sc; 1463 1464 cx->xfer = usbd_alloc_xfer(sc->sc_udev); 1465 if (cx->xfer == NULL) { 1466 printf("%s: %s: can't allocate xfer handle!\n", 1467 DN(sc), FUNC); 1468 return (USBD_NOMEM); 1469 } 1470 1471 cx->buf = usbd_alloc_buffer(cx->xfer, UDL_CMD_MAX_XFER_SIZE); 1472 if (cx->buf == NULL) { 1473 printf("%s: %s: can't allocate xfer buffer!\n", 1474 DN(sc), FUNC); 1475 return (USBD_NOMEM); 1476 } 1477 } 1478 1479 return (USBD_NORMAL_COMPLETION); 1480 } 1481 1482 void 1483 udl_cmd_free_xfer(struct udl_softc *sc) 1484 { 1485 int i; 1486 1487 for (i = 0; i < UDL_CMD_XFER_COUNT; i++) { 1488 struct udl_cmd_xfer *cx = &sc->sc_cmd_xfer[i]; 1489 1490 if (cx->xfer != NULL) { 1491 usbd_free_xfer(cx->xfer); 1492 cx->xfer = NULL; 1493 } 1494 } 1495 } 1496 1497 int 1498 udl_cmd_alloc_buf(struct udl_softc *sc) 1499 { 1500 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1501 1502 cb->buf = malloc(UDL_CMD_MAX_XFER_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO); 1503 if (cb->buf == NULL) { 1504 printf("%s: %s: can't allocate buffer!\n", 1505 DN(sc), FUNC); 1506 return (ENOMEM); 1507 } 1508 cb->off = 0; 1509 cb->compblock = 0; 1510 1511 return (0); 1512 } 1513 1514 void 1515 udl_cmd_free_buf(struct udl_softc *sc) 1516 { 1517 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1518 1519 if (cb->buf != NULL) { 1520 free(cb->buf, M_DEVBUF, 0); 1521 cb->buf = NULL; 1522 } 1523 cb->off = 0; 1524 } 1525 1526 void 1527 udl_cmd_insert_int_1(struct udl_softc *sc, uint8_t value) 1528 { 1529 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1530 1531 cb->buf[cb->off] = value; 1532 1533 cb->off += 1; 1534 } 1535 1536 void 1537 udl_cmd_insert_int_2(struct udl_softc *sc, uint16_t value) 1538 { 1539 uint16_t lvalue; 1540 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1541 1542 lvalue = htobe16(value); 1543 bcopy(&lvalue, cb->buf + cb->off, 2); 1544 1545 cb->off += 2; 1546 } 1547 1548 void 1549 udl_cmd_insert_int_3(struct udl_softc *sc, uint32_t value) 1550 { 1551 uint32_t lvalue; 1552 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1553 #if BYTE_ORDER == BIG_ENDIAN 1554 lvalue = htobe32(value) << 8; 1555 #else 1556 lvalue = htobe32(value) >> 8; 1557 #endif 1558 bcopy(&lvalue, cb->buf + cb->off, 3); 1559 1560 cb->off += 3; 1561 } 1562 1563 void 1564 udl_cmd_insert_int_4(struct udl_softc *sc, uint32_t value) 1565 { 1566 uint32_t lvalue; 1567 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1568 1569 lvalue = htobe32(value); 1570 bcopy(&lvalue, cb->buf + cb->off, 4); 1571 1572 cb->off += 4; 1573 } 1574 1575 void 1576 udl_cmd_insert_buf(struct udl_softc *sc, uint8_t *buf, uint32_t len) 1577 { 1578 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1579 1580 bcopy(buf, cb->buf + cb->off, len); 1581 1582 cb->off += len; 1583 } 1584 1585 int 1586 udl_cmd_insert_buf_comp(struct udl_softc *sc, uint8_t *buf, uint32_t len) 1587 { 1588 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1589 struct udl_huffman *h; 1590 uint8_t bit_pos; 1591 uint16_t *pixels, prev; 1592 int16_t diff; 1593 uint32_t bit_count, bit_pattern, bit_cur; 1594 int i, j, bytes, eob, padding, next; 1595 1596 pixels = (uint16_t *)buf; 1597 bit_pos = bytes = eob = padding = 0; 1598 1599 /* 1600 * If the header doesn't fit into the 512 byte main-block anymore, 1601 * skip the header and finish up the main-block. We return zero 1602 * to signal our caller that the header has been skipped. 1603 */ 1604 if (cb->compblock >= UDL_CB_RESTART_SIZE) { 1605 cb->off -= UDL_CMD_WRITE_HEAD_SIZE; 1606 cb->compblock -= UDL_CMD_WRITE_HEAD_SIZE; 1607 eob = 1; 1608 } 1609 1610 /* 1611 * Generate a sub-block with maximal 256 pixels compressed data. 1612 */ 1613 for (i = 0; i < len / 2 && eob == 0; i++) { 1614 /* get difference between current and previous pixel */ 1615 if (i > 0) 1616 prev = betoh16(pixels[i - 1]); 1617 else 1618 prev = 0; 1619 1620 /* get the huffman difference bit sequence */ 1621 diff = betoh16(pixels[i]) - prev; 1622 h = (struct udl_huffman *)(sc->sc_huffman + UDL_HUFFMAN_BASE); 1623 h += diff; 1624 bit_count = h->bit_count; 1625 bit_pattern = betoh32(h->bit_pattern); 1626 1627 1628 /* we are near the end of the main-block, so quit loop */ 1629 if (bit_count % 8 == 0) 1630 next = bit_count / 8; 1631 else 1632 next = (bit_count / 8) + 1; 1633 1634 if (cb->compblock + next >= UDL_CB_BODY_SIZE) { 1635 eob = 1; 1636 break; 1637 } 1638 1639 /* generate one pixel compressed data */ 1640 for (j = 0; j < bit_count; j++) { 1641 if (bit_pos == 0) 1642 cb->buf[cb->off] = 0; 1643 bit_cur = (bit_pattern >> j) & 1; 1644 cb->buf[cb->off] |= (bit_cur << bit_pos); 1645 bit_pos++; 1646 1647 if (bit_pos == 8) { 1648 bit_pos = 0; 1649 cb->off++; 1650 cb->compblock++; 1651 } 1652 } 1653 bytes += 2; 1654 } 1655 1656 /* 1657 * If we have bits left in our last byte, round up to the next 1658 * byte, so we don't overwrite them. 1659 */ 1660 if (bit_pos != 0) { 1661 cb->off++; 1662 cb->compblock++; 1663 } 1664 1665 /* 1666 * Finish up a 512 byte main-block. The leftover space gets 1667 * padded to zero. Finally terminate the block by writting the 1668 * 0xff-into-UDL_REG_SYNC-register sequence. 1669 */ 1670 if (eob == 1) { 1671 padding = (UDL_CB_BODY_SIZE - cb->compblock); 1672 for (i = 0; i < padding; i++) { 1673 cb->buf[cb->off] = 0; 1674 cb->off++; 1675 cb->compblock++; 1676 } 1677 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0xff); 1678 cb->compblock = 0; 1679 } 1680 1681 /* return how many bytes we have compressed */ 1682 return (bytes); 1683 } 1684 1685 int 1686 udl_cmd_insert_head_comp(struct udl_softc *sc, uint32_t len) 1687 { 1688 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1689 int i, padding; 1690 1691 if (cb->compblock > UDL_CB_BODY_SIZE) { 1692 cb->off -= UDL_CMD_COPY_HEAD_SIZE; 1693 cb->compblock -= UDL_CMD_COPY_HEAD_SIZE; 1694 1695 padding = (UDL_CB_BODY_SIZE - cb->compblock); 1696 for (i = 0; i < padding; i++) { 1697 cb->buf[cb->off] = 0; 1698 cb->off++; 1699 cb->compblock++; 1700 } 1701 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0xff); 1702 cb->compblock = 0; 1703 return (0); 1704 } 1705 1706 return (len); 1707 } 1708 1709 int 1710 udl_cmd_insert_check(struct udl_softc *sc, int len) 1711 { 1712 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1713 int total; 1714 usbd_status error; 1715 1716 total = cb->off + len; 1717 1718 if (total > UDL_CMD_MAX_XFER_SIZE) { 1719 /* command buffer is almost full, try to flush it */ 1720 if (cb->xfer_type == UDL_CMD_XFER_ASYNC) 1721 error = udl_cmd_send_async(sc); 1722 else 1723 error = udl_cmd_send(sc); 1724 if (error != USBD_NORMAL_COMPLETION) { 1725 DPRINTF(1, "%s: %s: can't flush full command buffer\n", 1726 DN(sc), FUNC); 1727 return (EAGAIN); 1728 } 1729 } 1730 1731 return (0); 1732 } 1733 1734 void 1735 udl_cmd_set_xfer_type(struct udl_softc *sc, int xfer_type) 1736 { 1737 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1738 1739 cb->xfer_type = xfer_type; 1740 } 1741 1742 void 1743 udl_cmd_save_offset(struct udl_softc *sc) 1744 { 1745 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1746 1747 cb->off_save = cb->off; 1748 cb->compblock_save = cb->compblock; 1749 } 1750 1751 void 1752 udl_cmd_restore_offset(struct udl_softc *sc) 1753 { 1754 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1755 1756 cb->off = cb->off_save; 1757 cb->compblock = cb->compblock_save; 1758 } 1759 1760 void 1761 udl_cmd_write_reg_1(struct udl_softc *sc, uint8_t reg, uint8_t val) 1762 { 1763 udl_cmd_insert_int_1(sc, UDL_BULK_SOC); 1764 udl_cmd_insert_int_1(sc, UDL_BULK_CMD_REG_WRITE_1); 1765 udl_cmd_insert_int_1(sc, reg); 1766 udl_cmd_insert_int_1(sc, val); 1767 } 1768 1769 void 1770 udl_cmd_write_reg_3(struct udl_softc *sc, uint8_t reg, uint32_t val) 1771 { 1772 udl_cmd_write_reg_1(sc, reg + 0, (val >> 16) & 0xff); 1773 udl_cmd_write_reg_1(sc, reg + 1, (val >> 8) & 0xff); 1774 udl_cmd_write_reg_1(sc, reg + 2, (val >> 0) & 0xff); 1775 } 1776 1777 usbd_status 1778 udl_cmd_send(struct udl_softc *sc) 1779 { 1780 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1781 struct udl_cmd_xfer *cx = &sc->sc_cmd_xfer[0]; 1782 int len; 1783 usbd_status error; 1784 1785 /* mark end of command stack */ 1786 udl_cmd_insert_int_1(sc, UDL_BULK_SOC); 1787 udl_cmd_insert_int_1(sc, UDL_BULK_CMD_EOC); 1788 1789 bcopy(cb->buf, cx->buf, cb->off); 1790 1791 len = cb->off; 1792 usbd_setup_xfer(cx->xfer, sc->sc_tx_pipeh, 0, cx->buf, len, 1793 USBD_NO_COPY | USBD_SHORT_XFER_OK | USBD_SYNCHRONOUS, 1000, NULL); 1794 error = usbd_transfer(cx->xfer); 1795 if (error != USBD_NORMAL_COMPLETION) { 1796 printf("%s: %s: %s!\n", DN(sc), FUNC, usbd_errstr(error)); 1797 /* we clear our buffer now to avoid growing out of bounds */ 1798 goto fail; 1799 } 1800 DPRINTF(1, "%s: %s: sent %d of %d bytes\n", 1801 DN(sc), FUNC, len, cb->off); 1802 fail: 1803 cb->off = 0; 1804 cb->compblock = 0; 1805 1806 return (error); 1807 } 1808 1809 usbd_status 1810 udl_cmd_send_async(struct udl_softc *sc) 1811 { 1812 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 1813 struct udl_cmd_xfer *cx; 1814 usbd_status error; 1815 int i, s; 1816 1817 /* check if command xfer queue is full */ 1818 if (sc->sc_cmd_xfer_cnt == UDL_CMD_XFER_COUNT) 1819 return (USBD_IN_USE); 1820 1821 s = splusb(); /* no callbacks please until accounting is done */ 1822 1823 /* find a free command xfer buffer */ 1824 for (i = 0; i < UDL_CMD_XFER_COUNT; i++) { 1825 if (sc->sc_cmd_xfer[i].busy == 0) 1826 break; 1827 } 1828 if (i == UDL_CMD_XFER_COUNT) { 1829 /* this shouldn't happen */ 1830 splx(s); 1831 return (USBD_IN_USE); 1832 } 1833 cx = &sc->sc_cmd_xfer[i]; 1834 1835 /* mark end of command stack */ 1836 udl_cmd_insert_int_1(sc, UDL_BULK_SOC); 1837 udl_cmd_insert_int_1(sc, UDL_BULK_CMD_EOC); 1838 1839 /* copy command buffer to xfer buffer */ 1840 bcopy(cb->buf, cx->buf, cb->off); 1841 1842 /* do xfer */ 1843 usbd_setup_xfer(cx->xfer, sc->sc_tx_pipeh, cx, cx->buf, cb->off, 1844 USBD_NO_COPY, 1000, udl_cmd_send_async_cb); 1845 error = usbd_transfer(cx->xfer); 1846 if (error != 0 && error != USBD_IN_PROGRESS) { 1847 printf("%s: %s: %s!\n", DN(sc), FUNC, usbd_errstr(error)); 1848 splx(s); 1849 return (error); 1850 } 1851 DPRINTF(2, "%s: %s: sending %d bytes from buffer no. %d\n", 1852 DN(sc), FUNC, cb->off, i); 1853 1854 /* free command buffer, lock xfer buffer */ 1855 cb->off = 0; 1856 cb->compblock = 0; 1857 cx->busy = 1; 1858 sc->sc_cmd_xfer_cnt++; 1859 1860 splx(s); 1861 1862 return (USBD_NORMAL_COMPLETION); 1863 } 1864 1865 void 1866 udl_cmd_send_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status) 1867 { 1868 struct udl_cmd_xfer *cx = priv; 1869 struct udl_softc *sc = cx->sc; 1870 int len; 1871 1872 if (status != USBD_NORMAL_COMPLETION) { 1873 printf("%s: %s: %s!\n", DN(sc), FUNC, usbd_errstr(status)); 1874 1875 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1876 return; 1877 if (status == USBD_STALLED) 1878 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh); 1879 goto skip; 1880 } 1881 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 1882 1883 DPRINTF(3, "%s: %s: sent %d bytes\n", DN(sc), FUNC, len); 1884 skip: 1885 /* free xfer buffer */ 1886 cx->busy = 0; 1887 sc->sc_cmd_xfer_cnt--; 1888 1889 /* wakeup UDLIO_DAMAGE if it sleeps for a free xfer buffer */ 1890 wakeup(sc); 1891 } 1892 1893 /* ---------- */ 1894 1895 usbd_status 1896 udl_init_chip(struct udl_softc *sc) 1897 { 1898 uint8_t ui8; 1899 uint32_t ui32; 1900 usbd_status error; 1901 1902 error = udl_poll(sc, &ui32); 1903 if (error != USBD_NORMAL_COMPLETION) 1904 return (error); 1905 DPRINTF(1, "%s: %s: poll=0x%08x\n", DN(sc), FUNC, ui32); 1906 1907 /* Some products may use later chip too */ 1908 switch (ui32 & 0xff) { 1909 case 0xf1: /* DL1x5 */ 1910 switch (sc->sc_chip) { 1911 case DL120: 1912 sc->sc_chip = DL125; 1913 break; 1914 case DL160: 1915 sc->sc_chip = DL165; 1916 break; 1917 } 1918 break; 1919 } 1920 DPRINTF(1, "%s: %s: chip %d\n", DN(sc), FUNC, sc->sc_chip); 1921 1922 error = udl_read_1(sc, 0xc484, &ui8); 1923 if (error != USBD_NORMAL_COMPLETION) 1924 return (error); 1925 DPRINTF(1, "%s: %s: read 0x%02x from 0xc484\n", DN(sc), FUNC, ui8); 1926 1927 error = udl_write_1(sc, 0xc41f, 0x01); 1928 if (error != USBD_NORMAL_COMPLETION) 1929 return (error); 1930 DPRINTF(1, "%s: %s: write 0x01 to 0xc41f\n", DN(sc), FUNC); 1931 1932 error = udl_read_edid(sc, sc->sc_edid); 1933 if (error != USBD_NORMAL_COMPLETION) 1934 return (error); 1935 DPRINTF(1, "%s: %s: read EDID\n", DN(sc), FUNC); 1936 1937 error = udl_set_enc_key(sc, udl_null_key_1, sizeof(udl_null_key_1)); 1938 if (error != USBD_NORMAL_COMPLETION) 1939 return (error); 1940 DPRINTF(1, "%s: %s: set encryption key\n", DN(sc), FUNC); 1941 1942 error = udl_write_1(sc, 0xc40b, 0x00); 1943 if (error != USBD_NORMAL_COMPLETION) 1944 return (error); 1945 DPRINTF(1, "%s: %s: write 0x00 to 0xc40b\n", DN(sc), FUNC, ui8); 1946 1947 error = udl_set_decomp_table(sc, udl_decomp_table, 1948 sizeof(udl_decomp_table)); 1949 if (error != USBD_NORMAL_COMPLETION) 1950 return (error); 1951 DPRINTF(1, "%s: %s: set decompression table\n", DN(sc), FUNC); 1952 1953 return (USBD_NORMAL_COMPLETION); 1954 } 1955 1956 void 1957 udl_init_fb_offsets(struct udl_softc *sc, uint32_t start16, uint32_t stride16, 1958 uint32_t start8, uint32_t stride8) 1959 { 1960 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0x00); 1961 udl_cmd_write_reg_3(sc, UDL_REG_ADDR_START16, start16); 1962 udl_cmd_write_reg_3(sc, UDL_REG_ADDR_STRIDE16, stride16); 1963 udl_cmd_write_reg_3(sc, UDL_REG_ADDR_START8, start8); 1964 udl_cmd_write_reg_3(sc, UDL_REG_ADDR_STRIDE8, stride8); 1965 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0xff); 1966 } 1967 1968 usbd_status 1969 udl_init_resolution(struct udl_softc *sc) 1970 { 1971 int i; 1972 usbd_status error; 1973 uint8_t *buf = udl_modes[sc->sc_cur_mode].mode; 1974 1975 /* write resolution values and set video memory offsets */ 1976 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0x00); 1977 for (i = 0; i < UDL_MODE_SIZE; i++) 1978 udl_cmd_write_reg_1(sc, i, buf[i]); 1979 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0xff); 1980 1981 udl_init_fb_offsets(sc, 0x000000, 0x000a00, 0x555555, 0x000500); 1982 error = udl_cmd_send(sc); 1983 if (error != USBD_NORMAL_COMPLETION) 1984 return (error); 1985 1986 /* clear screen */ 1987 error = udl_clear_screen(sc); 1988 if (error != USBD_NORMAL_COMPLETION) 1989 return (error); 1990 1991 /* show framebuffer content */ 1992 udl_cmd_write_reg_1(sc, UDL_REG_SCREEN, UDL_REG_SCREEN_ON); 1993 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0xff); 1994 error = udl_cmd_send(sc); 1995 if (error != USBD_NORMAL_COMPLETION) 1996 return (error); 1997 1998 return (USBD_NORMAL_COMPLETION); 1999 } 2000 2001 usbd_status 2002 udl_clear_screen(struct udl_softc *sc) 2003 { 2004 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 2005 usbd_status error; 2006 2007 /* clear screen */ 2008 udl_fb_block_write(sc, 0x0000, 0, 0, sc->sc_width, sc->sc_height); 2009 if (cb->xfer_type == UDL_CMD_XFER_ASYNC) 2010 error = udl_cmd_send_async(sc); 2011 else 2012 error = udl_cmd_send(sc); 2013 if (error != USBD_NORMAL_COMPLETION) 2014 return (error); 2015 2016 return (USBD_NORMAL_COMPLETION); 2017 } 2018 2019 void 2020 udl_select_mode(struct udl_softc *sc) 2021 { 2022 struct udl_mode mode; 2023 int index = MAX_DL_MODES, i; 2024 2025 /* try to get the preferred mode from EDID */ 2026 edid_parse(sc->sc_edid, &sc->sc_edid_info); 2027 #ifdef UDL_DEBUG 2028 edid_print(&sc->sc_edid_info); 2029 #endif 2030 if (sc->sc_edid_info.edid_preferred_mode != NULL) { 2031 mode.hz = 2032 (sc->sc_edid_info.edid_preferred_mode->dot_clock * 1000) / 2033 (sc->sc_edid_info.edid_preferred_mode->htotal * 2034 sc->sc_edid_info.edid_preferred_mode->vtotal); 2035 mode.clock = 2036 sc->sc_edid_info.edid_preferred_mode->dot_clock / 10; 2037 mode.hdisplay = 2038 sc->sc_edid_info.edid_preferred_mode->hdisplay; 2039 mode.vdisplay = 2040 sc->sc_edid_info.edid_preferred_mode->vdisplay; 2041 index = udl_lookup_mode(mode.hdisplay, mode.vdisplay, mode.hz, 2042 sc->sc_chip, mode.clock); 2043 sc->sc_cur_mode = index; 2044 } else { 2045 DPRINTF(1, "%s: %s: no preferred mode found!\n", DN(sc), FUNC); 2046 } 2047 2048 if (index == MAX_DL_MODES) { 2049 DPRINTF(1, "%s: %s: no mode line found for %dx%d @ %dHz!\n", 2050 DN(sc), FUNC, mode.hdisplay, mode.vdisplay, mode.hz); 2051 2052 i = 0; 2053 while (i < sc->sc_edid_info.edid_nmodes) { 2054 mode.hz = 2055 (sc->sc_edid_info.edid_modes[i].dot_clock * 1000) / 2056 (sc->sc_edid_info.edid_modes[i].htotal * 2057 sc->sc_edid_info.edid_modes[i].vtotal); 2058 mode.clock = 2059 sc->sc_edid_info.edid_modes[i].dot_clock / 10; 2060 mode.hdisplay = 2061 sc->sc_edid_info.edid_modes[i].hdisplay; 2062 mode.vdisplay = 2063 sc->sc_edid_info.edid_modes[i].vdisplay; 2064 index = udl_lookup_mode(mode.hdisplay, mode.vdisplay, 2065 mode.hz, sc->sc_chip, mode.clock); 2066 if (index < MAX_DL_MODES) 2067 if ((sc->sc_cur_mode == MAX_DL_MODES) || 2068 (index > sc->sc_cur_mode)) 2069 sc->sc_cur_mode = index; 2070 i++; 2071 } 2072 } 2073 2074 /* 2075 * If no mode found use default. 2076 */ 2077 if (sc->sc_cur_mode == MAX_DL_MODES) 2078 sc->sc_cur_mode = udl_lookup_mode(800, 600, 60, sc->sc_chip, 0); 2079 2080 mode = udl_modes[sc->sc_cur_mode]; 2081 sc->sc_width = mode.hdisplay; 2082 sc->sc_height = mode.vdisplay; 2083 2084 /* 2085 * We always use 16bit color depth for now. 2086 */ 2087 sc->sc_depth = 16; 2088 2089 DPRINTF(1, "%s: %s: %dx%d @ %dHz\n", 2090 DN(sc), FUNC, mode.hdisplay, mode.vdisplay, mode.hz); 2091 } 2092 2093 int 2094 udl_fb_buf_write(struct udl_softc *sc, uint8_t *buf, uint32_t x, 2095 uint32_t y, uint16_t width) 2096 { 2097 uint16_t lwidth; 2098 uint32_t off; 2099 int r; 2100 2101 r = udl_cmd_insert_check(sc, UDL_CMD_WRITE_MAX_SIZE); 2102 if (r != 0) 2103 return (r); 2104 2105 off = ((y * sc->sc_width) + x) * 2; 2106 lwidth = width * 2; 2107 2108 udl_cmd_insert_int_1(sc, UDL_BULK_SOC); 2109 udl_cmd_insert_int_1(sc, UDL_BULK_CMD_FB_WRITE | UDL_BULK_CMD_FB_WORD); 2110 udl_cmd_insert_int_3(sc, off); 2111 udl_cmd_insert_int_1(sc, width >= UDL_CMD_MAX_PIXEL_COUNT ? 0 : width); 2112 2113 udl_cmd_insert_buf(sc, buf, lwidth); 2114 2115 return (0); 2116 } 2117 2118 int 2119 udl_fb_block_write(struct udl_softc *sc, uint16_t rgb16, uint32_t x, 2120 uint32_t y, uint32_t width, uint32_t height) 2121 { 2122 uint32_t i; 2123 int r; 2124 2125 for (i = 0; i < height; i++) { 2126 r = udl_fb_line_write(sc, rgb16, x, y + i, width); 2127 if (r != 0) 2128 return (r); 2129 } 2130 2131 return (0); 2132 } 2133 2134 int 2135 udl_fb_line_write(struct udl_softc *sc, uint16_t rgb16, uint32_t x, 2136 uint32_t y, uint32_t width) 2137 { 2138 uint32_t off, block; 2139 int r; 2140 2141 off = (y * sc->sc_width) + x; 2142 2143 while (width) { 2144 if (width > UDL_CMD_MAX_PIXEL_COUNT) 2145 block = UDL_CMD_MAX_PIXEL_COUNT; 2146 else 2147 block = width; 2148 2149 r = udl_fb_off_write(sc, rgb16, off, block); 2150 if (r != 0) 2151 return (r); 2152 2153 off += block; 2154 width -= block; 2155 } 2156 2157 return (0); 2158 } 2159 2160 int 2161 udl_fb_off_write(struct udl_softc *sc, uint16_t rgb16, uint32_t off, 2162 uint16_t width) 2163 { 2164 uint8_t buf[UDL_CMD_MAX_DATA_SIZE]; 2165 uint16_t lwidth, lrgb16; 2166 uint32_t loff; 2167 int i, r; 2168 2169 r = udl_cmd_insert_check(sc, UDL_CMD_WRITE_MAX_SIZE); 2170 if (r != 0) 2171 return (r); 2172 2173 loff = off * 2; 2174 lwidth = width * 2; 2175 2176 udl_cmd_insert_int_1(sc, UDL_BULK_SOC); 2177 udl_cmd_insert_int_1(sc, UDL_BULK_CMD_FB_WRITE | UDL_BULK_CMD_FB_WORD); 2178 udl_cmd_insert_int_3(sc, loff); 2179 udl_cmd_insert_int_1(sc, width >= UDL_CMD_MAX_PIXEL_COUNT ? 0 : width); 2180 2181 for (i = 0; i < lwidth; i += 2) { 2182 lrgb16 = htobe16(rgb16); 2183 bcopy(&lrgb16, buf + i, 2); 2184 } 2185 2186 udl_cmd_insert_buf(sc, buf, lwidth); 2187 2188 return (0); 2189 } 2190 2191 int 2192 udl_fb_block_copy(struct udl_softc *sc, uint32_t src_x, uint32_t src_y, 2193 uint32_t dst_x, uint32_t dst_y, uint32_t width, uint32_t height) 2194 { 2195 int i, r; 2196 2197 for (i = 0; i < height; i++) { 2198 r = udl_fb_line_copy(sc, src_x, src_y + i, dst_x, dst_y + i, 2199 width); 2200 if (r != 0) 2201 return (r); 2202 } 2203 2204 return (0); 2205 } 2206 2207 2208 int 2209 udl_fb_line_copy(struct udl_softc *sc, uint32_t src_x, uint32_t src_y, 2210 uint32_t dst_x, uint32_t dst_y, uint32_t width) 2211 { 2212 uint32_t src_off, dst_off, block; 2213 int r; 2214 2215 src_off = (src_y * sc->sc_width) + src_x; 2216 dst_off = (dst_y * sc->sc_width) + dst_x; 2217 2218 while (width) { 2219 if (width > UDL_CMD_MAX_PIXEL_COUNT) 2220 block = UDL_CMD_MAX_PIXEL_COUNT; 2221 else 2222 block = width; 2223 2224 r = udl_fb_off_copy(sc, src_off, dst_off, block); 2225 if (r != 0) 2226 return (r); 2227 2228 src_off += block; 2229 dst_off += block; 2230 width -= block; 2231 } 2232 2233 return (0); 2234 } 2235 2236 int 2237 udl_fb_off_copy(struct udl_softc *sc, uint32_t src_off, uint32_t dst_off, 2238 uint16_t width) 2239 { 2240 uint32_t ldst_off, lsrc_off; 2241 int r; 2242 2243 r = udl_cmd_insert_check(sc, UDL_CMD_COPY_MAX_SIZE); 2244 if (r != 0) 2245 return (r); 2246 2247 ldst_off = dst_off * 2; 2248 lsrc_off = src_off * 2; 2249 2250 udl_cmd_insert_int_1(sc, UDL_BULK_SOC); 2251 udl_cmd_insert_int_1(sc, UDL_BULK_CMD_FB_COPY | UDL_BULK_CMD_FB_WORD); 2252 udl_cmd_insert_int_3(sc, ldst_off); 2253 udl_cmd_insert_int_1(sc, width >= UDL_CMD_MAX_PIXEL_COUNT ? 0 : width); 2254 udl_cmd_insert_int_3(sc, lsrc_off); 2255 2256 return (0); 2257 } 2258 2259 int 2260 udl_fb_buf_write_comp(struct udl_softc *sc, uint8_t *buf, uint32_t x, 2261 uint32_t y, uint16_t width) 2262 { 2263 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 2264 uint8_t *count; 2265 uint16_t lwidth; 2266 uint32_t off; 2267 int r, sent; 2268 2269 r = udl_cmd_insert_check(sc, UDL_CMD_WRITE_MAX_SIZE); 2270 if (r != 0) 2271 return (r); 2272 2273 off = ((y * sc->sc_width) + x) * 2; 2274 lwidth = width * 2; 2275 2276 /* 2277 * A new compressed stream needs the 0xff-into-UDL_REG_SYNC-register 2278 * sequence always as first command. 2279 */ 2280 if (cb->off == 0) 2281 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0xff); 2282 2283 r = sent = 0; 2284 while (sent < lwidth) { 2285 udl_cmd_insert_int_1(sc, UDL_BULK_SOC); 2286 udl_cmd_insert_int_1(sc, 2287 UDL_BULK_CMD_FB_WRITE | 2288 UDL_BULK_CMD_FB_WORD | 2289 UDL_BULK_CMD_FB_COMP); 2290 udl_cmd_insert_int_3(sc, off + sent); 2291 udl_cmd_insert_int_1(sc, 2292 width >= UDL_CMD_MAX_PIXEL_COUNT ? 0 : width); 2293 cb->compblock += UDL_CMD_WRITE_HEAD_SIZE; 2294 2295 count = &cb->buf[cb->off - 1]; 2296 r = udl_cmd_insert_buf_comp(sc, buf + sent, lwidth - sent); 2297 if (r > 0 && r != (lwidth - sent)) { 2298 *count = r / 2; 2299 width -= r / 2; 2300 } 2301 sent += r; 2302 } 2303 2304 return (0); 2305 } 2306 2307 int 2308 udl_fb_block_write_comp(struct udl_softc *sc, uint16_t rgb16, uint32_t x, 2309 uint32_t y, uint32_t width, uint32_t height) 2310 { 2311 uint32_t i; 2312 int r; 2313 2314 for (i = 0; i < height; i++) { 2315 r = udl_fb_line_write_comp(sc, rgb16, x, y + i, width); 2316 if (r != 0) 2317 return (r); 2318 } 2319 2320 return (0); 2321 } 2322 2323 int 2324 udl_fb_line_write_comp(struct udl_softc *sc, uint16_t rgb16, uint32_t x, 2325 uint32_t y, uint32_t width) 2326 { 2327 uint32_t off, block; 2328 int r; 2329 2330 off = (y * sc->sc_width) + x; 2331 2332 while (width) { 2333 if (width > UDL_CMD_MAX_PIXEL_COUNT) 2334 block = UDL_CMD_MAX_PIXEL_COUNT; 2335 else 2336 block = width; 2337 2338 r = udl_fb_off_write_comp(sc, rgb16, off, block); 2339 if (r != 0) 2340 return (r); 2341 2342 off += block; 2343 width -= block; 2344 } 2345 2346 return (0); 2347 } 2348 2349 int 2350 udl_fb_off_write_comp(struct udl_softc *sc, uint16_t rgb16, uint32_t off, 2351 uint16_t width) 2352 { 2353 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 2354 uint8_t buf[UDL_CMD_MAX_DATA_SIZE]; 2355 uint8_t *count; 2356 uint16_t lwidth, lrgb16; 2357 uint32_t loff; 2358 int i, r, sent; 2359 2360 r = udl_cmd_insert_check(sc, UDL_CMD_WRITE_MAX_SIZE); 2361 if (r != 0) 2362 return (r); 2363 2364 loff = off * 2; 2365 lwidth = width * 2; 2366 2367 for (i = 0; i < lwidth; i += 2) { 2368 lrgb16 = htobe16(rgb16); 2369 bcopy(&lrgb16, buf + i, 2); 2370 } 2371 2372 /* 2373 * A new compressed stream needs the 0xff-into-UDL_REG_SYNC-register 2374 * sequence always as first command. 2375 */ 2376 if (cb->off == 0) 2377 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0xff); 2378 2379 r = sent = 0; 2380 while (sent < lwidth) { 2381 udl_cmd_insert_int_1(sc, UDL_BULK_SOC); 2382 udl_cmd_insert_int_1(sc, 2383 UDL_BULK_CMD_FB_WRITE | 2384 UDL_BULK_CMD_FB_WORD | 2385 UDL_BULK_CMD_FB_COMP); 2386 udl_cmd_insert_int_3(sc, loff + sent); 2387 udl_cmd_insert_int_1(sc, 2388 width >= UDL_CMD_MAX_PIXEL_COUNT ? 0 : width); 2389 cb->compblock += UDL_CMD_WRITE_HEAD_SIZE; 2390 2391 count = &cb->buf[cb->off - 1]; 2392 r = udl_cmd_insert_buf_comp(sc, buf + sent, lwidth - sent); 2393 if (r > 0 && r != (lwidth - sent)) { 2394 *count = r / 2; 2395 width -= r / 2; 2396 } 2397 sent += r; 2398 } 2399 2400 return (0); 2401 } 2402 2403 int 2404 udl_fb_block_copy_comp(struct udl_softc *sc, uint32_t src_x, uint32_t src_y, 2405 uint32_t dst_x, uint32_t dst_y, uint32_t width, uint32_t height) 2406 { 2407 int i, r; 2408 2409 for (i = 0; i < height; i++) { 2410 r = udl_fb_line_copy_comp(sc, src_x, src_y + i, 2411 dst_x, dst_y + i, width); 2412 if (r != 0) 2413 return (r); 2414 } 2415 2416 return (0); 2417 } 2418 2419 int 2420 udl_fb_line_copy_comp(struct udl_softc *sc, uint32_t src_x, uint32_t src_y, 2421 uint32_t dst_x, uint32_t dst_y, uint32_t width) 2422 { 2423 uint32_t src_off, dst_off, block; 2424 int r; 2425 2426 src_off = (src_y * sc->sc_width) + src_x; 2427 dst_off = (dst_y * sc->sc_width) + dst_x; 2428 2429 while (width) { 2430 if (width > UDL_CMD_MAX_PIXEL_COUNT) 2431 block = UDL_CMD_MAX_PIXEL_COUNT; 2432 else 2433 block = width; 2434 2435 r = udl_fb_off_copy_comp(sc, src_off, dst_off, block); 2436 if (r != 0) 2437 return (r); 2438 2439 src_off += block; 2440 dst_off += block; 2441 width -= block; 2442 } 2443 2444 return (0); 2445 } 2446 2447 int 2448 udl_fb_off_copy_comp(struct udl_softc *sc, uint32_t src_off, uint32_t dst_off, 2449 uint16_t width) 2450 { 2451 struct udl_cmd_buf *cb = &sc->sc_cmd_buf; 2452 uint32_t ldst_off, lsrc_off; 2453 int r; 2454 2455 r = udl_cmd_insert_check(sc, UDL_CMD_COPY_MAX_SIZE); 2456 if (r != 0) 2457 return (r); 2458 2459 ldst_off = dst_off * 2; 2460 lsrc_off = src_off * 2; 2461 2462 /* 2463 * A new compressed stream needs the 0xff-into-UDL_REG_SYNC-register 2464 * sequence always as first command. 2465 */ 2466 if (cb->off == 0) 2467 udl_cmd_write_reg_1(sc, UDL_REG_SYNC, 0xff); 2468 2469 r = 0; 2470 while (r < 1) { 2471 udl_cmd_insert_int_1(sc, UDL_BULK_SOC); 2472 udl_cmd_insert_int_1(sc, 2473 UDL_BULK_CMD_FB_COPY | UDL_BULK_CMD_FB_WORD); 2474 udl_cmd_insert_int_3(sc, ldst_off); 2475 udl_cmd_insert_int_1(sc, 2476 width >= UDL_CMD_MAX_PIXEL_COUNT ? 0 : width); 2477 udl_cmd_insert_int_3(sc, lsrc_off); 2478 cb->compblock += UDL_CMD_COPY_HEAD_SIZE; 2479 2480 r = udl_cmd_insert_head_comp(sc, UDL_CMD_COPY_HEAD_SIZE); 2481 } 2482 2483 return (0); 2484 } 2485 2486 /* ---------- */ 2487 #ifdef UDL_DEBUG 2488 void 2489 udl_hexdump(void *buf, int len, int quiet) 2490 { 2491 int i; 2492 2493 for (i = 0; i < len; i++) { 2494 if (quiet == 0) { 2495 if (i % 16 == 0) 2496 printf("%s%5i:", i ? "\n" : "", i); 2497 if (i % 4 == 0) 2498 printf(" "); 2499 } 2500 printf("%02x", (int)*((u_char *)buf + i)); 2501 } 2502 printf("\n"); 2503 } 2504 2505 usbd_status 2506 udl_init_test(struct udl_softc *sc) 2507 { 2508 int i, j, parts, loops; 2509 uint16_t color; 2510 uint16_t rgb24[3] = { 0xf800, 0x07e0, 0x001f }; 2511 2512 loops = (sc->sc_width * sc->sc_height) / UDL_CMD_MAX_PIXEL_COUNT; 2513 parts = loops / 3; 2514 color = rgb24[0]; 2515 2516 j = 1; 2517 for (i = 0; i < loops; i++) { 2518 if (i == parts) { 2519 color = rgb24[j]; 2520 parts += parts; 2521 j++; 2522 } 2523 (sc->udl_fb_off_write)(sc, color, i * UDL_CMD_MAX_PIXEL_COUNT, 2524 UDL_CMD_MAX_PIXEL_COUNT); 2525 } 2526 (void)udl_cmd_send(sc); 2527 2528 return (USBD_NORMAL_COMPLETION); 2529 } 2530 #endif 2531