1 /* $NetBSD: udl.c,v 1.7 2012/12/27 16:42:32 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2009 FUKAUMI Naoki. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * Copyright (c) 2009 Marcus Glocker <mglocker@openbsd.org> 30 * 31 * Permission to use, copy, modify, and distribute this software for any 32 * purpose with or without fee is hereby granted, provided that the above 33 * copyright notice and this permission notice appear in all copies. 34 * 35 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 36 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 37 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 38 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 39 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 40 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 41 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 42 */ 43 44 /* 45 * Driver for the ``DisplayLink DL-1x0 / DL-1x5'' graphic chips based 46 * on the reversed engineered specifications of Florian Echtler 47 * <floe at butterbrot dot org>: 48 * 49 * http://floe.butterbrot.org/displaylink/doku.php 50 * 51 * This driver was written by Marcus Glocker for OpenBSD and ported to 52 * NetBSD by FUKAUMI Naoki with many modification. 53 */ 54 55 #include <sys/cdefs.h> 56 __KERNEL_RCSID(0, "$NetBSD: udl.c,v 1.7 2012/12/27 16:42:32 skrll Exp $"); 57 58 #include <sys/param.h> 59 #include <sys/device.h> 60 #include <sys/kernel.h> 61 #include <sys/proc.h> 62 #include <sys/systm.h> 63 #include <sys/kmem.h> 64 #include <uvm/uvm.h> 65 66 #include <sys/bus.h> 67 #include <sys/endian.h> 68 69 #include <dev/usb/usb.h> 70 #include <dev/usb/usbdi.h> 71 #include <dev/usb/usbdivar.h> 72 #include <dev/usb/usbdi_util.h> 73 #include <dev/usb/usb_mem.h> 74 #include <dev/usb/usbdevs.h> 75 76 #include <dev/firmload.h> 77 78 #include <dev/videomode/videomode.h> 79 #include <dev/videomode/edidvar.h> 80 81 #include <dev/wscons/wsconsio.h> 82 #include <dev/wscons/wsdisplayvar.h> 83 #include <dev/rasops/rasops.h> 84 85 #include <dev/usb/udl.h> 86 #ifdef notyet 87 #include <dev/usb/udlio.h> 88 #endif 89 90 /* 91 * Defines. 92 */ 93 #ifdef UDL_DEBUG 94 #define DPRINTF(x) do { if (udl_debug) printf x; } while (0) 95 #define DPRINTFN(n, x) do { if (udl_debug >= (n)) printf x; } while (0) 96 int udl_debug = 1; 97 #else 98 #define DPRINTF(x) do {} while (0) 99 #define DPRINTFN(n, x) do {} while (0) 100 #endif 101 102 /* 103 * Prototypes. 104 */ 105 static int udl_match(device_t, cfdata_t, void *); 106 static void udl_attach(device_t, device_t, void *); 107 static int udl_detach(device_t, int); 108 109 static int udl_ioctl(void *, void *, u_long, void *, int, 110 struct lwp *); 111 static paddr_t udl_mmap(void *, void *, off_t, int); 112 static int udl_alloc_screen(void *, const struct wsscreen_descr *, 113 void **, int *, int *, long *); 114 static void udl_free_screen(void *, void *); 115 static int udl_show_screen(void *, void *, int, 116 void (*)(void *, int, int), void *); 117 118 static void udl_comp_load(struct udl_softc *); 119 static void udl_comp_unload(struct udl_softc *); 120 static int udl_fbmem_alloc(struct udl_softc *); 121 static void udl_fbmem_free(struct udl_softc *); 122 static int udl_cmdq_alloc(struct udl_softc *); 123 static void udl_cmdq_free(struct udl_softc *); 124 static struct udl_cmdq *udl_cmdq_get(struct udl_softc *sc); 125 static void udl_cmdq_put(struct udl_softc *sc, 126 struct udl_cmdq *cmdq); 127 static void udl_cmdq_flush(struct udl_softc *); 128 129 static void udl_cursor(void *, int, int, int); 130 static void udl_putchar(void *, int, int, u_int, long); 131 static void udl_copycols(void *, int, int, int, int); 132 static void udl_erasecols(void *, int, int, int, long); 133 static void udl_copyrows(void *, int, int, int); 134 static void udl_eraserows(void *, int, int, long); 135 136 static void udl_restore_char(struct rasops_info *); 137 static void udl_draw_char(struct rasops_info *, uint16_t *, u_int, 138 int, int); 139 static void udl_copy_rect(struct udl_softc *, int, int, int, int, 140 int, int); 141 static void udl_fill_rect(struct udl_softc *, uint16_t, int, int, 142 int, int); 143 #ifdef notyet 144 static void udl_draw_rect(struct udl_softc *, 145 struct udl_ioctl_damage *); 146 static void udl_draw_rect_comp(struct udl_softc *, 147 struct udl_ioctl_damage *); 148 #endif 149 150 static inline void udl_copy_line(struct udl_softc *, int, int, int); 151 static inline void udl_fill_line(struct udl_softc *, uint16_t, int, int); 152 static inline void udl_draw_line(struct udl_softc *, uint16_t *, int, 153 int); 154 static inline void udl_draw_line_comp(struct udl_softc *, uint16_t *, int, 155 int); 156 157 static int udl_cmd_send(struct udl_softc *); 158 static void udl_cmd_send_async(struct udl_softc *); 159 static void udl_cmd_send_async_cb(usbd_xfer_handle, 160 usbd_private_handle, usbd_status); 161 162 static int udl_ctrl_msg(struct udl_softc *, uint8_t, uint8_t, 163 uint16_t, uint16_t, uint8_t *, uint16_t); 164 static int udl_init(struct udl_softc *); 165 static void udl_read_edid(struct udl_softc *); 166 static void udl_set_address(struct udl_softc *, int, int, int, 167 int); 168 static void udl_blank(struct udl_softc *, int); 169 static uint16_t udl_lfsr(uint16_t); 170 static int udl_set_resolution(struct udl_softc *, 171 const struct videomode *); 172 static const struct videomode *udl_videomode_lookup(const char *); 173 174 static inline void 175 udl_cmd_add_1(struct udl_softc *sc, uint8_t val) 176 { 177 178 *sc->sc_cmd_buf++ = val; 179 } 180 181 static inline void 182 udl_cmd_add_2(struct udl_softc *sc, uint16_t val) 183 { 184 185 be16enc(sc->sc_cmd_buf, val); 186 sc->sc_cmd_buf += 2; 187 } 188 189 static inline void 190 udl_cmd_add_3(struct udl_softc *sc, uint32_t val) 191 { 192 193 udl_cmd_add_2(sc, val >> 8); 194 udl_cmd_add_1(sc, val); 195 } 196 197 static inline void 198 udl_cmd_add_4(struct udl_softc *sc, uint32_t val) 199 { 200 201 be32enc(sc->sc_cmd_buf, val); 202 sc->sc_cmd_buf += 4; 203 } 204 205 static inline void 206 udl_cmd_add_buf(struct udl_softc *sc, uint16_t *buf, int width) 207 { 208 #if BYTE_ORDER == BIG_ENDIAN 209 memcpy(sc->sc_cmd_buf, buf, width * 2); 210 sc->sc_cmd_buf += width * 2; 211 #else 212 uint16_t *endp; 213 214 endp = buf + width; 215 216 if (((uintptr_t)sc->sc_cmd_buf & 1) == 0) { 217 while (buf < endp) { 218 *(uint16_t *)sc->sc_cmd_buf = htobe16(*buf++); 219 sc->sc_cmd_buf += 2; 220 } 221 } else { 222 while (buf < endp) { 223 be16enc(sc->sc_cmd_buf, *buf++); 224 sc->sc_cmd_buf += 2; 225 } 226 } 227 #endif 228 } 229 230 static inline void 231 udl_reg_write_1(struct udl_softc *sc, uint8_t reg, uint8_t val) 232 { 233 234 udl_cmd_add_4(sc, (UDL_BULK_SOC << 24) | 235 (UDL_BULK_CMD_REG_WRITE_1 << 16) | (reg << 8) | val); 236 } 237 238 static inline void 239 udl_reg_write_2(struct udl_softc *sc, uint8_t reg, uint16_t val) 240 { 241 242 udl_reg_write_1(sc, reg++, val >> 8); 243 udl_reg_write_1(sc, reg, val); 244 } 245 246 static inline void 247 udl_reg_write_3(struct udl_softc *sc, uint8_t reg, uint32_t val) 248 { 249 250 udl_reg_write_1(sc, reg++, val >> 16); 251 udl_reg_write_1(sc, reg++, val >> 8); 252 udl_reg_write_1(sc, reg, val); 253 } 254 255 /* XXX */ 256 static int 257 firmware_load(const char *dname, const char *iname, uint8_t **ucodep, 258 size_t *sizep) 259 { 260 firmware_handle_t fh; 261 int error; 262 263 if ((error = firmware_open(dname, iname, &fh)) != 0) 264 return error; 265 *sizep = firmware_get_size(fh); 266 if ((*ucodep = firmware_malloc(*sizep)) == NULL) { 267 firmware_close(fh); 268 return ENOMEM; 269 } 270 if ((error = firmware_read(fh, 0, *ucodep, *sizep)) != 0) 271 firmware_free(*ucodep, *sizep); 272 firmware_close(fh); 273 274 return error; 275 } 276 277 /* 278 * Driver glue. 279 */ 280 CFATTACH_DECL_NEW(udl, sizeof(struct udl_softc), 281 udl_match, udl_attach, udl_detach, NULL); 282 283 /* 284 * wsdisplay glue. 285 */ 286 static struct wsdisplay_accessops udl_accessops = { 287 udl_ioctl, 288 udl_mmap, 289 udl_alloc_screen, 290 udl_free_screen, 291 udl_show_screen, 292 NULL, 293 NULL, 294 NULL, 295 }; 296 297 /* 298 * Matching devices. 299 */ 300 static const struct usb_devno udl_devs[] = { 301 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_GUC2020 }, 302 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LD220 }, 303 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LD190 }, 304 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_U70 }, 305 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_VCUD60 }, 306 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_CONV }, 307 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_DLDVI }, 308 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_USBRGB }, 309 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCDUSB7X }, 310 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCDUSB10X }, 311 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_VGA10 }, 312 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_WSDVI }, 313 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_EC008 }, 314 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_GXDVIU2 }, 315 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD4300U }, 316 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD8000U }, 317 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_HPDOCK }, 318 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_NL571 }, 319 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_M01061 }, 320 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_NBDOCK }, 321 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_SWDVI }, 322 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LUM70 }, 323 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LCD8000UD_DVI }, 324 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LDEWX015U }, 325 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_LT1421WIDE }, 326 { USB_VENDOR_DISPLAYLINK, USB_PRODUCT_DISPLAYLINK_UM7X0 } 327 }; 328 329 static int 330 udl_match(device_t parent, cfdata_t match, void *aux) 331 { 332 struct usb_attach_arg *uaa = aux; 333 334 if (usb_lookup(udl_devs, uaa->vendor, uaa->product) != NULL) 335 return UMATCH_VENDOR_PRODUCT; 336 337 return UMATCH_NONE; 338 } 339 340 static void 341 udl_attach(device_t parent, device_t self, void *aux) 342 { 343 struct udl_softc *sc = device_private(self); 344 struct usb_attach_arg *uaa = aux; 345 struct wsemuldisplaydev_attach_args aa; 346 const struct videomode *vmp; 347 usbd_status error; 348 char *devinfop; 349 350 aprint_naive("\n"); 351 aprint_normal("\n"); 352 353 sc->sc_dev = self; 354 sc->sc_udev = uaa->device; 355 356 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0); 357 aprint_normal_dev(sc->sc_dev, "%s\n", devinfop); 358 usbd_devinfo_free(devinfop); 359 360 /* 361 * Set device configuration descriptor number. 362 */ 363 error = usbd_set_config_no(sc->sc_udev, 1, 0); 364 if (error != USBD_NORMAL_COMPLETION) { 365 aprint_error_dev(self, "failed to set configuration" 366 ", err=%s\n", usbd_errstr(error)); 367 return; 368 } 369 370 /* 371 * Create device handle to interface descriptor. 372 */ 373 error = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface); 374 if (error != USBD_NORMAL_COMPLETION) 375 return; 376 377 /* 378 * Open bulk TX pipe. 379 */ 380 error = usbd_open_pipe(sc->sc_iface, 1, USBD_EXCLUSIVE_USE, 381 &sc->sc_tx_pipeh); 382 if (error != USBD_NORMAL_COMPLETION) 383 return; 384 385 /* 386 * Allocate bulk command queue. 387 */ 388 #ifdef UDL_EVENT_COUNTERS 389 evcnt_attach_dynamic(&sc->sc_ev_cmdq_get, EVCNT_TYPE_MISC, NULL, 390 device_xname(sc->sc_dev), "udl_cmdq_get"); 391 evcnt_attach_dynamic(&sc->sc_ev_cmdq_put, EVCNT_TYPE_MISC, NULL, 392 device_xname(sc->sc_dev), "udl_cmdq_put"); 393 evcnt_attach_dynamic(&sc->sc_ev_cmdq_wait, EVCNT_TYPE_MISC, NULL, 394 device_xname(sc->sc_dev), "udl_cmdq_wait"); 395 evcnt_attach_dynamic(&sc->sc_ev_cmdq_timeout, EVCNT_TYPE_MISC, NULL, 396 device_xname(sc->sc_dev), "udl_cmdq_timeout"); 397 #endif 398 399 if (udl_cmdq_alloc(sc) != 0) 400 return; 401 402 cv_init(&sc->sc_cv, device_xname(sc->sc_dev)); 403 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_TTY); /* XXX for tty_lock */ 404 405 if ((sc->sc_cmd_cur = udl_cmdq_get(sc)) == NULL) 406 return; 407 UDL_CMD_BUFINIT(sc); 408 409 /* 410 * Initialize chip. 411 */ 412 if (udl_init(sc) != 0) 413 return; 414 415 udl_read_edid(sc); 416 417 /* 418 * Initialize resolution. 419 */ 420 #ifndef UDL_VIDEOMODE 421 if (sc->sc_ei.edid_nmodes != 0 && 422 sc->sc_ei.edid_preferred_mode != NULL) 423 vmp = sc->sc_ei.edid_preferred_mode; 424 else 425 #define UDL_VIDEOMODE "640x480x60" 426 #endif 427 vmp = udl_videomode_lookup(UDL_VIDEOMODE); 428 429 if (vmp == NULL) 430 return; 431 432 sc->sc_width = vmp->hdisplay; 433 sc->sc_height = vmp->vdisplay; 434 sc->sc_offscreen = sc->sc_height * 3 / 2; 435 sc->sc_depth = 16; 436 437 if (udl_set_resolution(sc, vmp) != 0) 438 return; 439 440 sc->sc_defaultscreen.name = "default"; 441 sc->sc_screens[0] = &sc->sc_defaultscreen; 442 sc->sc_screenlist.nscreens = 1; 443 sc->sc_screenlist.screens = sc->sc_screens; 444 445 /* 446 * Set initial wsdisplay emulation mode. 447 */ 448 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 449 450 /* 451 * Attach wsdisplay. 452 */ 453 aa.console = 0; 454 aa.scrdata = &sc->sc_screenlist; 455 aa.accessops = &udl_accessops; 456 aa.accesscookie = sc; 457 458 sc->sc_wsdisplay = 459 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint); 460 461 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 462 } 463 464 static int 465 udl_detach(device_t self, int flags) 466 { 467 struct udl_softc *sc = device_private(self); 468 469 /* 470 * Close bulk TX pipe. 471 */ 472 if (sc->sc_tx_pipeh != NULL) { 473 usbd_abort_pipe(sc->sc_tx_pipeh); 474 usbd_close_pipe(sc->sc_tx_pipeh); 475 } 476 477 /* 478 * Free command xfer buffers. 479 */ 480 udl_cmdq_flush(sc); 481 udl_cmdq_free(sc); 482 483 cv_destroy(&sc->sc_cv); 484 mutex_destroy(&sc->sc_mtx); 485 486 /* 487 * Free Huffman table. 488 */ 489 udl_comp_unload(sc); 490 491 /* 492 * Free framebuffer memory. 493 */ 494 udl_fbmem_free(sc); 495 496 /* 497 * Detach wsdisplay. 498 */ 499 if (sc->sc_wsdisplay != NULL) 500 config_detach(sc->sc_wsdisplay, DETACH_FORCE); 501 502 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 503 504 #ifdef UDL_EVENT_COUNTERS 505 evcnt_detach(&sc->sc_ev_cmdq_get); 506 evcnt_detach(&sc->sc_ev_cmdq_put); 507 evcnt_detach(&sc->sc_ev_cmdq_wait); 508 evcnt_detach(&sc->sc_ev_cmdq_timeout); 509 #endif 510 511 return 0; 512 } 513 514 static int 515 udl_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l) 516 { 517 struct udl_softc *sc = v; 518 #ifdef notyet 519 struct udl_ioctl_damage *d; 520 #endif 521 struct wsdisplay_fbinfo *wdf; 522 u_int mode; 523 524 switch (cmd) { 525 case WSDISPLAYIO_GTYPE: 526 *(u_int *)data = WSDISPLAY_TYPE_DL; 527 return 0; 528 529 case WSDISPLAYIO_GINFO: 530 wdf = (struct wsdisplay_fbinfo *)data; 531 wdf->height = sc->sc_height; 532 wdf->width = sc->sc_width; 533 wdf->depth = sc->sc_depth; 534 wdf->cmsize = 0; 535 return 0; 536 537 case WSDISPLAYIO_GVIDEO: 538 *(u_int *)data = sc->sc_blank; 539 return 0; 540 541 case WSDISPLAYIO_SVIDEO: 542 mode = *(u_int *)data; 543 if (mode == sc->sc_blank) 544 return 0; 545 switch (mode) { 546 case WSDISPLAYIO_VIDEO_OFF: 547 udl_blank(sc, 1); 548 break; 549 case WSDISPLAYIO_VIDEO_ON: 550 udl_blank(sc, 0); 551 break; 552 default: 553 return EINVAL; 554 } 555 udl_cmd_send_async(sc); 556 udl_cmdq_flush(sc); 557 sc->sc_blank = mode; 558 return 0; 559 560 case WSDISPLAYIO_SMODE: 561 mode = *(u_int *)data; 562 if (mode == sc->sc_mode) 563 return 0; 564 switch (mode) { 565 case WSDISPLAYIO_MODE_EMUL: 566 /* clear screen */ 567 udl_fill_rect(sc, 0, 0, 0, sc->sc_width, 568 sc->sc_height); 569 udl_cmd_send_async(sc); 570 udl_cmdq_flush(sc); 571 udl_comp_unload(sc); 572 break; 573 case WSDISPLAYIO_MODE_DUMBFB: 574 if (UDL_CMD_BUFSIZE(sc) > 0) 575 udl_cmd_send_async(sc); 576 udl_cmdq_flush(sc); 577 udl_comp_load(sc); 578 break; 579 default: 580 return EINVAL; 581 } 582 sc->sc_mode = mode; 583 return 0; 584 585 case WSDISPLAYIO_LINEBYTES: 586 *(u_int *)data = sc->sc_width * (sc->sc_depth / 8); 587 return 0; 588 589 #ifdef notyet 590 /* 591 * XXX 592 * OpenBSD allows device specific ioctl()s and use this 593 * UDLIO_DAMAGE for the damage extension ops of X servers. 594 * Before blindly pulling such interfaces, probably we should 595 * discuss how such devices should be handled which have 596 * in-direct framebuffer memories that should be transfered 597 * per updated rectangle regions via MI wscons APIs. 598 */ 599 case UDLIO_DAMAGE: 600 d = (struct udl_ioctl_damage *)data; 601 d->status = UDLIO_STATUS_OK; 602 if (sc->sc_flags & UDL_COMPRDY) 603 udl_draw_rect_comp(sc, d); 604 else 605 udl_draw_rect(sc, d); 606 return 0; 607 #endif 608 } 609 610 return EPASSTHROUGH; 611 } 612 613 static paddr_t 614 udl_mmap(void *v, void *vs, off_t off, int prot) 615 { 616 struct udl_softc *sc = v; 617 vaddr_t vaddr; 618 paddr_t paddr; 619 bool rv; 620 621 if (off < 0 || off > roundup2(UDL_FBMEM_SIZE(sc), PAGE_SIZE)) 622 return -1; 623 624 /* allocate framebuffer memory */ 625 if (udl_fbmem_alloc(sc) != 0) 626 return -1; 627 628 vaddr = (vaddr_t)sc->sc_fbmem + off; 629 rv = pmap_extract(pmap_kernel(), vaddr, &paddr); 630 KASSERT(rv); 631 paddr += vaddr & PGOFSET; 632 633 /* XXX we need MI paddr_t -> mmap cookie API */ 634 #if defined(__alpha__) 635 #define PTOMMAP(paddr) alpha_btop((char *)paddr) 636 #elif defined(__arm__) 637 #define PTOMMAP(paddr) arm_btop((u_long)paddr) 638 #elif defined(__hppa__) 639 #define PTOMMAP(paddr) btop((u_long)paddr) 640 #elif defined(__i386__) || defined(__x86_64__) 641 #define PTOMMAP(paddr) x86_btop(paddr) 642 #elif defined(__m68k__) 643 #define PTOMMAP(paddr) m68k_btop((char *)paddr) 644 #elif defined(__mips__) 645 #define PTOMMAP(paddr) mips_btop(paddr) 646 #elif defined(__powerpc__) 647 #define PTOMMAP(paddr) (paddr) 648 #elif defined(__sh__) 649 #define PTOMMAP(paddr) sh3_btop(paddr) 650 #elif defined(__sparc__) 651 #define PTOMMAP(paddr) (paddr) 652 #elif defined(__sparc64__) 653 #define PTOMMAP(paddr) atop(paddr) 654 #elif defined(__vax__) 655 #define PTOMMAP(paddr) btop((u_int)paddr) 656 #endif 657 658 return PTOMMAP(paddr); 659 } 660 661 static int 662 udl_alloc_screen(void *v, const struct wsscreen_descr *type, 663 void **cookiep, int *curxp, int *curyp, long *attrp) 664 { 665 struct udl_softc *sc = v; 666 667 if (sc->sc_nscreens > 0) 668 return ENOMEM; 669 670 /* 671 * Initialize rasops. 672 */ 673 sc->sc_ri.ri_depth = sc->sc_depth; 674 sc->sc_ri.ri_bits = NULL; 675 sc->sc_ri.ri_width = sc->sc_width; 676 sc->sc_ri.ri_height = sc->sc_height; 677 sc->sc_ri.ri_stride = sc->sc_width * (sc->sc_depth / 8); 678 sc->sc_ri.ri_hw = sc; 679 sc->sc_ri.ri_flg = 0; 680 681 if (sc->sc_depth == 16) { 682 sc->sc_ri.ri_rnum = 5; 683 sc->sc_ri.ri_gnum = 6; 684 sc->sc_ri.ri_bnum = 5; 685 sc->sc_ri.ri_rpos = 11; 686 sc->sc_ri.ri_gpos = 5; 687 sc->sc_ri.ri_bpos = 0; 688 } 689 690 rasops_init(&sc->sc_ri, sc->sc_height / 8, sc->sc_width / 8); 691 692 sc->sc_ri.ri_ops.cursor = udl_cursor; 693 sc->sc_ri.ri_ops.putchar = udl_putchar; 694 sc->sc_ri.ri_ops.copycols = udl_copycols; 695 sc->sc_ri.ri_ops.erasecols = udl_erasecols; 696 sc->sc_ri.ri_ops.copyrows = udl_copyrows; 697 sc->sc_ri.ri_ops.eraserows = udl_eraserows; 698 699 sc->sc_ri.ri_ops.allocattr(&sc->sc_ri, 0, 0, 0, attrp); 700 701 sc->sc_defaultscreen.ncols = sc->sc_ri.ri_cols; 702 sc->sc_defaultscreen.nrows = sc->sc_ri.ri_rows; 703 sc->sc_defaultscreen.textops = &sc->sc_ri.ri_ops; 704 sc->sc_defaultscreen.fontwidth = sc->sc_ri.ri_font->fontwidth; 705 sc->sc_defaultscreen.fontheight = sc->sc_ri.ri_font->fontheight; 706 sc->sc_defaultscreen.capabilities = sc->sc_ri.ri_caps; 707 708 *cookiep = &sc->sc_ri; 709 *curxp = 0; 710 *curyp = 0; 711 712 sc->sc_nscreens++; 713 714 return 0; 715 } 716 717 static void 718 udl_free_screen(void *v, void *cookie) 719 { 720 struct udl_softc *sc = v; 721 722 sc->sc_nscreens--; 723 } 724 725 static int 726 udl_show_screen(void *v, void *cookie, int waitok, 727 void (*cb)(void *, int, int), void *cbarg) 728 { 729 730 return 0; 731 } 732 733 static inline void 734 udl_cmd_add_decomptable(struct udl_softc *sc, uint8_t *buf, int len) 735 { 736 737 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_DECOMP); 738 udl_cmd_add_4(sc, 0x263871cd); /* magic number */ 739 udl_cmd_add_4(sc, 0x00000200); /* 512 byte chunks */ 740 memcpy(sc->sc_cmd_buf, buf, len); 741 sc->sc_cmd_buf += len; 742 } 743 744 static void 745 udl_comp_load(struct udl_softc *sc) 746 { 747 struct udl_huffman *h; 748 uint8_t *decomp; 749 size_t decomp_size; 750 int error, i; 751 752 if (!(sc->sc_flags & UDL_DECOMPRDY)) { 753 error = firmware_load("udl", "udl-decomp", &decomp, 754 &decomp_size); 755 if (error != 0) { 756 aprint_error_dev(sc->sc_dev, 757 "error %d, could not read decomp table %s!\n", 758 error, "udl-decomp"); 759 return; 760 } 761 udl_cmd_add_decomptable(sc, decomp, decomp_size); 762 firmware_free(decomp, decomp_size); 763 if (udl_cmd_send(sc) != 0) 764 return; 765 sc->sc_flags |= UDL_DECOMPRDY; 766 } 767 768 if (!(sc->sc_flags & UDL_COMPRDY)) { 769 error = firmware_load("udl", "udl-comp", &sc->sc_huffman, 770 &sc->sc_huffman_size); 771 if (error != 0) { 772 aprint_error_dev(sc->sc_dev, 773 "error %d, could not read huffman table %s!\n", 774 error, "udl-comp"); 775 return; 776 } 777 h = (struct udl_huffman *)sc->sc_huffman; 778 for (i = 0; i < UDL_HUFFMAN_RECORDS; i++) 779 h[i].bit_pattern = be32toh(h[i].bit_pattern); 780 sc->sc_huffman_base = sc->sc_huffman + UDL_HUFFMAN_BASE; 781 sc->sc_flags |= UDL_COMPRDY; 782 } 783 } 784 785 static void 786 udl_comp_unload(struct udl_softc *sc) 787 { 788 789 if (sc->sc_flags & UDL_COMPRDY) { 790 firmware_free(sc->sc_huffman, sc->sc_huffman_size); 791 sc->sc_huffman = NULL; 792 sc->sc_huffman_size = 0; 793 sc->sc_flags &= ~UDL_COMPRDY; 794 } 795 } 796 797 static int 798 udl_fbmem_alloc(struct udl_softc *sc) 799 { 800 801 if (sc->sc_fbmem == NULL) { 802 sc->sc_fbmem = kmem_alloc(UDL_FBMEM_SIZE(sc), KM_SLEEP); 803 if (sc->sc_fbmem == NULL) 804 return -1; 805 } 806 807 return 0; 808 } 809 810 static void 811 udl_fbmem_free(struct udl_softc *sc) 812 { 813 814 if (sc->sc_fbmem != NULL) { 815 kmem_free(sc->sc_fbmem, UDL_FBMEM_SIZE(sc)); 816 sc->sc_fbmem = NULL; 817 } 818 } 819 820 static int 821 udl_cmdq_alloc(struct udl_softc *sc) 822 { 823 struct udl_cmdq *cmdq; 824 int i; 825 826 TAILQ_INIT(&sc->sc_freecmd); 827 TAILQ_INIT(&sc->sc_xfercmd); 828 829 for (i = 0; i < UDL_NCMDQ; i++) { 830 cmdq = &sc->sc_cmdq[i]; 831 832 cmdq->cq_sc = sc; 833 834 cmdq->cq_xfer = usbd_alloc_xfer(sc->sc_udev); 835 if (cmdq->cq_xfer == NULL) { 836 aprint_error_dev(sc->sc_dev, 837 "%s: can't allocate xfer handle!\n", __func__); 838 goto error; 839 } 840 841 cmdq->cq_buf = 842 usbd_alloc_buffer(cmdq->cq_xfer, UDL_CMD_BUFFER_SIZE); 843 if (cmdq->cq_buf == NULL) { 844 aprint_error_dev(sc->sc_dev, 845 "%s: can't allocate xfer buffer!\n", __func__); 846 goto error; 847 } 848 849 TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmdq, cq_chain); 850 } 851 852 return 0; 853 854 error: 855 udl_cmdq_free(sc); 856 return -1; 857 } 858 859 static void 860 udl_cmdq_free(struct udl_softc *sc) 861 { 862 struct udl_cmdq *cmdq; 863 int i; 864 865 for (i = 0; i < UDL_NCMDQ; i++) { 866 cmdq = &sc->sc_cmdq[i]; 867 868 if (cmdq->cq_xfer != NULL) { 869 usbd_free_xfer(cmdq->cq_xfer); 870 cmdq->cq_xfer = NULL; 871 cmdq->cq_buf = NULL; 872 } 873 } 874 } 875 876 static struct udl_cmdq * 877 udl_cmdq_get(struct udl_softc *sc) 878 { 879 struct udl_cmdq *cmdq; 880 881 cmdq = TAILQ_FIRST(&sc->sc_freecmd); 882 if (cmdq != NULL) { 883 TAILQ_REMOVE(&sc->sc_freecmd, cmdq, cq_chain); 884 UDL_EVCNT_INCR(&sc->sc_ev_cmdq_get); 885 } 886 887 return cmdq; 888 } 889 890 static void 891 udl_cmdq_put(struct udl_softc *sc, struct udl_cmdq *cmdq) 892 { 893 894 TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmdq, cq_chain); 895 UDL_EVCNT_INCR(&sc->sc_ev_cmdq_put); 896 } 897 898 static void 899 udl_cmdq_flush(struct udl_softc *sc) 900 { 901 902 mutex_enter(&sc->sc_mtx); 903 while (TAILQ_FIRST(&sc->sc_xfercmd) != NULL) 904 cv_wait(&sc->sc_cv, &sc->sc_mtx); 905 mutex_exit(&sc->sc_mtx); 906 } 907 908 static void 909 udl_cursor(void *cookie, int on, int row, int col) 910 { 911 struct rasops_info *ri = cookie; 912 struct udl_softc *sc = ri->ri_hw; 913 int x, y, width, height; 914 915 if (ri->ri_flg & RI_CURSOR) 916 udl_restore_char(ri); 917 918 ri->ri_crow = row; 919 ri->ri_ccol = col; 920 921 if (on != 0) { 922 ri->ri_flg |= RI_CURSOR; 923 924 x = col * ri->ri_font->fontwidth; 925 y = row * ri->ri_font->fontheight; 926 width = ri->ri_font->fontwidth; 927 height = ri->ri_font->fontheight; 928 929 /* save the last character block to off-screen */ 930 udl_copy_rect(sc, x, y, 0, sc->sc_offscreen, width, height); 931 932 /* draw cursor */ 933 udl_fill_rect(sc, 0xffff, x, y, width, 1); 934 udl_fill_rect(sc, 0xffff, x, y + 1, 1, height - 2); 935 udl_fill_rect(sc, 0xffff, x + width - 1, y + 1, 1, height - 2); 936 udl_fill_rect(sc, 0xffff, x, y + height - 1, width, 1); 937 938 udl_cmd_send_async(sc); 939 } else 940 ri->ri_flg &= ~RI_CURSOR; 941 } 942 943 static void 944 udl_putchar(void *cookie, int row, int col, u_int uc, long attr) 945 { 946 struct rasops_info *ri = cookie; 947 struct udl_softc *sc = ri->ri_hw; 948 uint16_t rgb16[2]; 949 int fg, bg, underline, x, y, width, height; 950 951 rasops_unpack_attr(attr, &fg, &bg, &underline); 952 rgb16[1] = (uint16_t)ri->ri_devcmap[fg]; 953 rgb16[0] = (uint16_t)ri->ri_devcmap[bg]; 954 955 x = col * ri->ri_font->fontwidth; 956 y = row * ri->ri_font->fontheight; 957 width = ri->ri_font->fontwidth; 958 height = ri->ri_font->fontheight; 959 960 if (uc == ' ') { 961 /* 962 * Writting a block for the space character instead rendering 963 * it from font bits is more slim. 964 */ 965 udl_fill_rect(sc, rgb16[0], x, y, width, height); 966 } else { 967 /* render a character from font bits */ 968 udl_draw_char(ri, rgb16, uc, x, y); 969 } 970 971 if (underline != 0) 972 udl_fill_rect(sc, rgb16[1], x, y + height - 1, width, 1); 973 974 #if 0 975 udl_cmd_send_async(sc); 976 #endif 977 } 978 979 static void 980 udl_copycols(void *cookie, int row, int src, int dst, int num) 981 { 982 struct rasops_info *ri = cookie; 983 struct udl_softc *sc = ri->ri_hw; 984 int sx, dx, y, width, height; 985 986 sx = src * ri->ri_font->fontwidth; 987 dx = dst * ri->ri_font->fontwidth; 988 y = row * ri->ri_font->fontheight; 989 width = num * ri->ri_font->fontwidth; 990 height = ri->ri_font->fontheight; 991 992 /* copy row block to off-screen first to fix overlay-copy problem */ 993 udl_copy_rect(sc, sx, y, 0, sc->sc_offscreen, width, height); 994 995 /* copy row block back from off-screen now */ 996 udl_copy_rect(sc, 0, sc->sc_offscreen, dx, y, width, height); 997 #if 0 998 udl_cmd_send_async(sc); 999 #endif 1000 } 1001 1002 static void 1003 udl_erasecols(void *cookie, int row, int col, int num, long attr) 1004 { 1005 struct rasops_info *ri = cookie; 1006 struct udl_softc *sc = ri->ri_hw; 1007 uint16_t rgb16; 1008 int fg, bg, x, y, width, height; 1009 1010 rasops_unpack_attr(attr, &fg, &bg, NULL); 1011 rgb16 = (uint16_t)ri->ri_devcmap[bg]; 1012 1013 x = col * ri->ri_font->fontwidth; 1014 y = row * ri->ri_font->fontheight; 1015 width = num * ri->ri_font->fontwidth; 1016 height = ri->ri_font->fontheight; 1017 1018 udl_fill_rect(sc, rgb16, x, y, width, height); 1019 #if 0 1020 udl_cmd_send_async(sc); 1021 #endif 1022 } 1023 1024 static void 1025 udl_copyrows(void *cookie, int src, int dst, int num) 1026 { 1027 struct rasops_info *ri = cookie; 1028 struct udl_softc *sc = ri->ri_hw; 1029 int sy, ey, dy, width, height; 1030 1031 width = ri->ri_emuwidth; 1032 height = ri->ri_font->fontheight; 1033 1034 if (dst < src) { 1035 sy = src * height; 1036 ey = (src + num) * height; 1037 dy = dst * height; 1038 1039 while (sy < ey) { 1040 udl_copy_rect(sc, 0, sy, 0, dy, width, height); 1041 sy += height; 1042 dy += height; 1043 } 1044 } else { 1045 sy = (src + num) * height; 1046 ey = src * height; 1047 dy = (dst + num) * height; 1048 1049 while (sy > ey) { 1050 sy -= height; 1051 dy -= height; 1052 udl_copy_rect(sc, 0, sy, 0, dy, width, height); 1053 } 1054 } 1055 #if 0 1056 udl_cmd_send_async(sc); 1057 #endif 1058 } 1059 1060 static void 1061 udl_eraserows(void *cookie, int row, int num, long attr) 1062 { 1063 struct rasops_info *ri = cookie; 1064 struct udl_softc *sc = ri->ri_hw; 1065 uint16_t rgb16; 1066 int fg, bg, y, width, height; 1067 1068 rasops_unpack_attr(attr, &fg, &bg, NULL); 1069 rgb16 = (uint16_t)ri->ri_devcmap[bg]; 1070 1071 y = row * ri->ri_font->fontheight; 1072 width = ri->ri_emuwidth; 1073 height = num * ri->ri_font->fontheight; 1074 1075 udl_fill_rect(sc, rgb16, 0, y, width, height); 1076 #if 0 1077 udl_cmd_send_async(sc); 1078 #endif 1079 } 1080 1081 static void 1082 udl_restore_char(struct rasops_info *ri) 1083 { 1084 struct udl_softc *sc = ri->ri_hw; 1085 int x, y, width, height; 1086 1087 x = ri->ri_ccol * ri->ri_font->fontwidth; 1088 y = ri->ri_crow * ri->ri_font->fontheight; 1089 width = ri->ri_font->fontwidth; 1090 height = ri->ri_font->fontheight; 1091 1092 /* restore the last saved character from off-screen */ 1093 udl_copy_rect(sc, 0, sc->sc_offscreen, x, y, width, height); 1094 } 1095 1096 static void 1097 udl_draw_char(struct rasops_info *ri, uint16_t *rgb16, u_int uc, int x, int y) 1098 { 1099 struct udl_softc *sc = ri->ri_hw; 1100 struct wsdisplay_font *font = ri->ri_font; 1101 uint32_t fontbits; 1102 uint16_t pixels[32]; 1103 uint8_t *fontbase; 1104 int i, soff, eoff; 1105 1106 soff = y * sc->sc_width + x; 1107 eoff = (y + font->fontheight) * sc->sc_width + x; 1108 fontbase = (uint8_t *)font->data + (uc - font->firstchar) * 1109 ri->ri_fontscale; 1110 1111 while (soff < eoff) { 1112 fontbits = 0; 1113 switch (font->stride) { 1114 case 4: 1115 fontbits |= fontbase[3]; 1116 /* FALLTHROUGH */ 1117 case 3: 1118 fontbits |= fontbase[2] << 8; 1119 /* FALLTHROUGH */ 1120 case 2: 1121 fontbits |= fontbase[1] << 16; 1122 /* FALLTHROUGH */ 1123 case 1: 1124 fontbits |= fontbase[0] << 24; 1125 } 1126 fontbase += font->stride; 1127 1128 for (i = 0; i < font->fontwidth; i++) { 1129 pixels[i] = rgb16[(fontbits >> 31) & 1]; 1130 fontbits <<= 1; 1131 } 1132 1133 udl_draw_line(sc, pixels, soff, font->fontwidth); 1134 soff += sc->sc_width; 1135 } 1136 } 1137 1138 static void 1139 udl_copy_rect(struct udl_softc *sc, int sx, int sy, int dx, int dy, int width, 1140 int height) 1141 { 1142 int sbase, soff, ebase, eoff, dbase, doff, width_cur; 1143 1144 sbase = sy * sc->sc_width; 1145 ebase = (sy + height) * sc->sc_width; 1146 dbase = dy * sc->sc_width; 1147 1148 while (width > 0) { 1149 soff = sbase + sx; 1150 eoff = ebase + sx; 1151 doff = dbase + dx; 1152 1153 if (width >= UDL_CMD_WIDTH_MAX) 1154 width_cur = UDL_CMD_WIDTH_MAX; 1155 else 1156 width_cur = width; 1157 1158 while (soff < eoff) { 1159 udl_copy_line(sc, soff, doff, width_cur); 1160 soff += sc->sc_width; 1161 doff += sc->sc_width; 1162 } 1163 1164 sx += width_cur; 1165 dx += width_cur; 1166 width -= width_cur; 1167 } 1168 } 1169 1170 static void 1171 udl_fill_rect(struct udl_softc *sc, uint16_t rgb16, int x, int y, int width, 1172 int height) 1173 { 1174 int sbase, soff, ebase, eoff, width_cur; 1175 1176 sbase = y * sc->sc_width; 1177 ebase = (y + height) * sc->sc_width; 1178 1179 while (width > 0) { 1180 soff = sbase + x; 1181 eoff = ebase + x; 1182 1183 if (width >= UDL_CMD_WIDTH_MAX) 1184 width_cur = UDL_CMD_WIDTH_MAX; 1185 else 1186 width_cur = width; 1187 1188 while (soff < eoff) { 1189 udl_fill_line(sc, rgb16, soff, width_cur); 1190 soff += sc->sc_width; 1191 } 1192 1193 x += width_cur; 1194 width -= width_cur; 1195 } 1196 } 1197 1198 #ifdef notyet 1199 static void 1200 udl_draw_rect(struct udl_softc *sc, struct udl_ioctl_damage *d) 1201 { 1202 int sbase, soff, ebase, eoff, x, y, width, width_cur, height; 1203 1204 x = d->x1; 1205 y = d->y1; 1206 width = d->x2 - d->x1; 1207 height = d->y2 - d->y1; 1208 sbase = y * sc->sc_width; 1209 ebase = (y + height) * sc->sc_width; 1210 1211 while (width > 0) { 1212 soff = sbase + x; 1213 eoff = ebase + x; 1214 1215 if (width >= UDL_CMD_WIDTH_MAX) 1216 width_cur = UDL_CMD_WIDTH_MAX; 1217 else 1218 width_cur = width; 1219 1220 while (soff < eoff) { 1221 udl_draw_line(sc, (uint16_t *)sc->sc_fbmem + soff, 1222 soff, width_cur); 1223 soff += sc->sc_width; 1224 } 1225 1226 x += width_cur; 1227 width -= width_cur; 1228 } 1229 1230 udl_cmd_send_async(sc); 1231 } 1232 1233 static void 1234 udl_draw_rect_comp(struct udl_softc *sc, struct udl_ioctl_damage *d) 1235 { 1236 int soff, eoff, x, y, width, height; 1237 1238 x = d->x1; 1239 y = d->y1; 1240 width = d->x2 - d->x1; 1241 height = d->y2 - d->y1; 1242 soff = y * sc->sc_width + x; 1243 eoff = (y + height) * sc->sc_width + x; 1244 1245 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff); 1246 sc->sc_cmd_cblen = 4; 1247 1248 while (soff < eoff) { 1249 udl_draw_line_comp(sc, (uint16_t *)sc->sc_fbmem + soff, soff, 1250 width); 1251 soff += sc->sc_width; 1252 } 1253 1254 udl_cmd_send_async(sc); 1255 } 1256 #endif 1257 1258 static inline void 1259 udl_copy_line(struct udl_softc *sc, int soff, int doff, int width) 1260 { 1261 1262 if (__predict_false((UDL_CMD_BUFSIZE(sc) + UDL_CMD_COPY_SIZE + 2) > 1263 UDL_CMD_BUFFER_SIZE)) 1264 udl_cmd_send_async(sc); 1265 1266 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_COPY16); 1267 udl_cmd_add_4(sc, ((doff * 2) << 8) | (width & 0xff)); 1268 1269 udl_cmd_add_3(sc, soff * 2); 1270 } 1271 1272 static inline void 1273 udl_fill_line(struct udl_softc *sc, uint16_t rgb16, int off, int width) 1274 { 1275 1276 if (__predict_false((UDL_CMD_BUFSIZE(sc) + UDL_CMD_FILL_SIZE + 2) > 1277 UDL_CMD_BUFFER_SIZE)) 1278 udl_cmd_send_async(sc); 1279 1280 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_RLE16); 1281 udl_cmd_add_4(sc, ((off * 2) << 8) | (width & 0xff)); 1282 1283 udl_cmd_add_1(sc, width); 1284 udl_cmd_add_2(sc, rgb16); 1285 } 1286 1287 static inline void 1288 udl_draw_line(struct udl_softc *sc, uint16_t *buf, int off, int width) 1289 { 1290 1291 if (__predict_false( 1292 (UDL_CMD_BUFSIZE(sc) + UDL_CMD_DRAW_SIZE(width) + 2) > 1293 UDL_CMD_BUFFER_SIZE)) 1294 udl_cmd_send_async(sc); 1295 1296 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_WRITE16); 1297 udl_cmd_add_4(sc, ((off * 2) << 8) | (width & 0xff)); 1298 1299 udl_cmd_add_buf(sc, buf, width); 1300 } 1301 1302 static inline int 1303 udl_cmd_add_buf_comp(struct udl_softc *sc, uint16_t *buf, int width) 1304 { 1305 struct udl_huffman *h; 1306 uint16_t *startp, *endp; 1307 uint32_t bit_pattern; 1308 uint16_t prev; 1309 int16_t diff; 1310 uint8_t bit_count, bit_pos, bit_rem, curlen; 1311 1312 startp = buf; 1313 if (width >= UDL_CMD_WIDTH_MAX) 1314 endp = buf + UDL_CMD_WIDTH_MAX; 1315 else 1316 endp = buf + width; 1317 1318 prev = bit_pos = *sc->sc_cmd_buf = 0; 1319 bit_rem = 8; 1320 1321 /* 1322 * Generate a sub-block with maximal 256 pixels compressed data. 1323 */ 1324 while (buf < endp) { 1325 /* get difference between current and previous pixel */ 1326 diff = *buf - prev; 1327 1328 /* get the huffman difference bit sequence */ 1329 h = (struct udl_huffman *)sc->sc_huffman_base + diff; 1330 bit_count = h->bit_count; 1331 bit_pattern = h->bit_pattern; 1332 1333 curlen = (bit_pos + bit_count + 7) / 8; 1334 if (__predict_false((sc->sc_cmd_cblen + curlen + 1) > 1335 UDL_CMD_COMP_BLOCK_SIZE)) 1336 break; 1337 1338 /* generate one pixel compressed data */ 1339 while (bit_count >= bit_rem) { 1340 *sc->sc_cmd_buf++ |= 1341 (bit_pattern & ((1 << bit_rem) - 1)) << bit_pos; 1342 *sc->sc_cmd_buf = 0; 1343 sc->sc_cmd_cblen++; 1344 bit_count -= bit_rem; 1345 bit_pattern >>= bit_rem; 1346 bit_pos = 0; 1347 bit_rem = 8; 1348 } 1349 1350 if (bit_count > 0) { 1351 *sc->sc_cmd_buf |= 1352 (bit_pattern & ((1 << bit_count) - 1)) << bit_pos; 1353 bit_pos += bit_count; 1354 bit_rem -= bit_count; 1355 } 1356 1357 prev = *buf++; 1358 } 1359 1360 /* 1361 * If we have bits left in our last byte, round up to the next 1362 * byte, so we don't overwrite them. 1363 */ 1364 if (bit_pos > 0) { 1365 sc->sc_cmd_buf++; 1366 sc->sc_cmd_cblen++; 1367 } 1368 1369 /* return how many pixels we have compressed */ 1370 return buf - startp; 1371 } 1372 1373 static inline void 1374 udl_draw_line_comp(struct udl_softc *sc, uint16_t *buf, int off, int width) 1375 { 1376 uint8_t *widthp; 1377 int width_cur; 1378 1379 while (width > 0) { 1380 if (__predict_false( 1381 (sc->sc_cmd_cblen + UDL_CMD_COMP_MIN_SIZE + 1) > 1382 UDL_CMD_COMP_BLOCK_SIZE)) { 1383 if (UDL_CMD_BUFSIZE(sc) < UDL_CMD_COMP_THRESHOLD) { 1384 while (sc->sc_cmd_cblen < 1385 UDL_CMD_COMP_BLOCK_SIZE) { 1386 *sc->sc_cmd_buf++ = 0; 1387 sc->sc_cmd_cblen++; 1388 } 1389 } else 1390 udl_cmd_send_async(sc); 1391 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff); 1392 sc->sc_cmd_cblen = 4; 1393 } 1394 1395 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | 1396 (UDL_BULK_CMD_FB_WRITE16 | UDL_BULK_CMD_FB_COMP)); 1397 udl_cmd_add_4(sc, (off * 2) << 8); 1398 1399 widthp = sc->sc_cmd_buf - 1; 1400 1401 sc->sc_cmd_cblen += UDL_CMD_HEADER_SIZE; 1402 1403 width_cur = udl_cmd_add_buf_comp(sc, buf, width); 1404 1405 *widthp = width_cur; 1406 buf += width_cur; 1407 off += width_cur; 1408 width -= width_cur; 1409 } 1410 } 1411 1412 static int 1413 udl_cmd_send(struct udl_softc *sc) 1414 { 1415 struct udl_cmdq *cmdq; 1416 usbd_status error; 1417 uint32_t len; 1418 1419 cmdq = sc->sc_cmd_cur; 1420 1421 /* mark end of command stack */ 1422 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_EOC); 1423 1424 len = UDL_CMD_BUFSIZE(sc); 1425 1426 /* do xfer */ 1427 error = usbd_bulk_transfer(cmdq->cq_xfer, sc->sc_tx_pipeh, 1428 USBD_NO_COPY, USBD_NO_TIMEOUT, cmdq->cq_buf, &len, "udlcmds"); 1429 1430 UDL_CMD_BUFINIT(sc); 1431 1432 if (error != USBD_NORMAL_COMPLETION) { 1433 aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__, 1434 usbd_errstr(error)); 1435 return -1; 1436 } 1437 1438 return 0; 1439 } 1440 1441 static void 1442 udl_cmd_send_async(struct udl_softc *sc) 1443 { 1444 struct udl_cmdq *cmdq; 1445 usbd_status error; 1446 uint32_t len; 1447 1448 #if 1 1449 /* 1450 * XXX 1451 * All tty ops for wsemul are called with tty_lock spin mutex held, 1452 * so we can't call cv_wait(9) here to acquire a free buffer. 1453 * For now, all commands and data for wsemul ops are discarded 1454 * if there is no free command buffer, and then screen text might 1455 * be corrupted on large scroll ops etc. 1456 * 1457 * Probably we have to reorganize the giant tty_lock mutex, or 1458 * change wsdisplay APIs (especially wsdisplaystart()) to return 1459 * a number of actually handled characters as OpenBSD does, but 1460 * the latter one requires whole API changes around rasops(9) etc. 1461 */ 1462 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 1463 if (TAILQ_FIRST(&sc->sc_freecmd) == NULL) { 1464 UDL_CMD_BUFINIT(sc); 1465 return; 1466 } 1467 } 1468 #endif 1469 1470 cmdq = sc->sc_cmd_cur; 1471 1472 /* mark end of command stack */ 1473 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_EOC); 1474 1475 len = UDL_CMD_BUFSIZE(sc); 1476 1477 /* do xfer */ 1478 mutex_enter(&sc->sc_mtx); 1479 usbd_setup_xfer(cmdq->cq_xfer, sc->sc_tx_pipeh, cmdq, cmdq->cq_buf, 1480 len, USBD_NO_COPY, USBD_NO_TIMEOUT, udl_cmd_send_async_cb); 1481 error = usbd_transfer(cmdq->cq_xfer); 1482 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) { 1483 aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__, 1484 usbd_errstr(error)); 1485 mutex_exit(&sc->sc_mtx); 1486 goto end; 1487 } 1488 1489 TAILQ_INSERT_TAIL(&sc->sc_xfercmd, cmdq, cq_chain); 1490 cmdq = udl_cmdq_get(sc); 1491 mutex_exit(&sc->sc_mtx); 1492 while (cmdq == NULL) { 1493 int err; 1494 UDL_EVCNT_INCR(&sc->sc_ev_cmdq_wait); 1495 mutex_enter(&sc->sc_mtx); 1496 err = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, 1497 mstohz(100) /* XXX is this needed? */); 1498 if (err != 0) { 1499 DPRINTF(("%s: %s: cv timeout (error = %d)\n", 1500 device_xname(sc->sc_dev), __func__, err)); 1501 UDL_EVCNT_INCR(&sc->sc_ev_cmdq_timeout); 1502 } 1503 cmdq = udl_cmdq_get(sc); 1504 mutex_exit(&sc->sc_mtx); 1505 } 1506 sc->sc_cmd_cur = cmdq; 1507 end: 1508 UDL_CMD_BUFINIT(sc); 1509 } 1510 1511 static void 1512 udl_cmd_send_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 1513 usbd_status status) 1514 { 1515 struct udl_cmdq *cmdq = priv; 1516 struct udl_softc *sc = cmdq->cq_sc; 1517 1518 if (status != USBD_NORMAL_COMPLETION) { 1519 aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__, 1520 usbd_errstr(status)); 1521 1522 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1523 return; 1524 if (status == USBD_STALLED) 1525 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh); 1526 } 1527 1528 mutex_enter(&sc->sc_mtx); 1529 TAILQ_REMOVE(&sc->sc_xfercmd, cmdq, cq_chain); 1530 udl_cmdq_put(sc, cmdq); 1531 1532 /* wakeup xfer op that sleeps for a free xfer buffer */ 1533 cv_signal(&sc->sc_cv); 1534 mutex_exit(&sc->sc_mtx); 1535 } 1536 1537 static int 1538 udl_ctrl_msg(struct udl_softc *sc, uint8_t rt, uint8_t r, uint16_t index, 1539 uint16_t value, uint8_t *buf, uint16_t len) 1540 { 1541 usb_device_request_t req; 1542 usbd_status error; 1543 1544 req.bmRequestType = rt; 1545 req.bRequest = r; 1546 USETW(req.wIndex, index); 1547 USETW(req.wValue, value); 1548 USETW(req.wLength, len); 1549 1550 error = usbd_do_request(sc->sc_udev, &req, buf); 1551 if (error != USBD_NORMAL_COMPLETION) { 1552 aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__, 1553 usbd_errstr(error)); 1554 return -1; 1555 } 1556 1557 return 0; 1558 } 1559 1560 static int 1561 udl_init(struct udl_softc *sc) 1562 { 1563 static uint8_t key[16] = { 1564 0x57, 0xcd, 0xdc, 0xa7, 0x1c, 0x88, 0x5e, 0x15, 1565 0x60, 0xfe, 0xc6, 0x97, 0x16, 0x3d, 0x47, 0xf2 1566 }; 1567 uint8_t status[4], val; 1568 1569 if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_STATUS, 1570 0x0000, 0x0000, status, sizeof(status)) != 0) 1571 return -1; 1572 1573 if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_1, 1574 0xc484, 0x0000, &val, 1) != 0) 1575 return -1; 1576 1577 val = 1; 1578 if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_WRITE_1, 1579 0xc41f, 0x0000, &val, 1) != 0) 1580 return -1; 1581 1582 if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_SET_KEY, 1583 0x0000, 0x0000, key, sizeof(key)) != 0) 1584 return -1; 1585 1586 val = 0; 1587 if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_WRITE_1, 1588 0xc40b, 0x0000, &val, 1) != 0) 1589 return -1; 1590 1591 return 0; 1592 } 1593 1594 static void 1595 udl_read_edid(struct udl_softc *sc) 1596 { 1597 uint8_t buf[64], edid[128]; 1598 int offset; 1599 1600 memset(&sc->sc_ei, 0, sizeof(struct edid_info)); 1601 1602 offset = 0; 1603 if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID, 1604 0x00a1, (offset << 8), buf, 64) != 0) 1605 return; 1606 if (buf[0] != 0) 1607 return; 1608 memcpy(&edid[offset], &buf[1], 63); 1609 offset += 63; 1610 1611 if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID, 1612 0x00a1, (offset << 8), buf, 64) != 0) 1613 return; 1614 if (buf[0] != 0) 1615 return; 1616 memcpy(&edid[offset], &buf[1], 63); 1617 offset += 63; 1618 1619 if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID, 1620 0x00a1, (offset << 8), buf, 3) != 0) 1621 return; 1622 if (buf[0] != 0) 1623 return; 1624 memcpy(&edid[offset], &buf[1], 2); 1625 1626 if (edid_parse(edid, &sc->sc_ei) == 0) { 1627 #ifdef UDL_DEBUG 1628 edid_print(&sc->sc_ei); 1629 #endif 1630 } 1631 } 1632 1633 static void 1634 udl_set_address(struct udl_softc *sc, int start16, int stride16, int start8, 1635 int stride8) 1636 { 1637 udl_reg_write_1(sc, UDL_REG_SYNC, 0x00); 1638 udl_reg_write_3(sc, UDL_REG_ADDR_START16, start16); 1639 udl_reg_write_3(sc, UDL_REG_ADDR_STRIDE16, stride16); 1640 udl_reg_write_3(sc, UDL_REG_ADDR_START8, start8); 1641 udl_reg_write_3(sc, UDL_REG_ADDR_STRIDE8, stride8); 1642 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff); 1643 } 1644 1645 static void 1646 udl_blank(struct udl_softc *sc, int blank) 1647 { 1648 1649 if (blank != 0) 1650 udl_reg_write_1(sc, UDL_REG_BLANK, UDL_REG_BLANK_ON); 1651 else 1652 udl_reg_write_1(sc, UDL_REG_BLANK, UDL_REG_BLANK_OFF); 1653 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff); 1654 } 1655 1656 static uint16_t 1657 udl_lfsr(uint16_t count) 1658 { 1659 uint16_t val = 0xffff; 1660 1661 while (count > 0) { 1662 val = (uint16_t)(val << 1) | ((uint16_t)( 1663 (uint16_t)(val << 0) ^ 1664 (uint16_t)(val << 11) ^ 1665 (uint16_t)(val << 13) ^ 1666 (uint16_t)(val << 14) 1667 ) >> 15); 1668 count--; 1669 } 1670 1671 return val; 1672 } 1673 1674 static int 1675 udl_set_resolution(struct udl_softc *sc, const struct videomode *vmp) 1676 { 1677 uint16_t val; 1678 int start16, stride16, start8, stride8; 1679 1680 /* set video memory offsets */ 1681 start16 = 0; 1682 stride16 = sc->sc_width * 2; 1683 start8 = stride16 * sc->sc_height; 1684 stride8 = sc->sc_width; 1685 udl_set_address(sc, start16, stride16, start8, stride8); 1686 1687 /* write resolution values */ 1688 udl_reg_write_1(sc, UDL_REG_SYNC, 0x00); 1689 udl_reg_write_1(sc, UDL_REG_COLORDEPTH, UDL_REG_COLORDEPTH_16); 1690 val = vmp->htotal - vmp->hsync_start; 1691 udl_reg_write_2(sc, UDL_REG_XDISPLAYSTART, udl_lfsr(val)); 1692 val += vmp->hdisplay; 1693 udl_reg_write_2(sc, UDL_REG_XDISPLAYEND, udl_lfsr(val)); 1694 val = vmp->vtotal - vmp->vsync_start; 1695 udl_reg_write_2(sc, UDL_REG_YDISPLAYSTART, udl_lfsr(val)); 1696 val += vmp->vdisplay; 1697 udl_reg_write_2(sc, UDL_REG_YDISPLAYEND, udl_lfsr(val)); 1698 val = vmp->htotal - 1; 1699 udl_reg_write_2(sc, UDL_REG_XENDCOUNT, udl_lfsr(val)); 1700 val = vmp->hsync_end - vmp->hsync_start + 1; 1701 if (vmp->flags & VID_PHSYNC) { 1702 udl_reg_write_2(sc, UDL_REG_HSYNCSTART, udl_lfsr(1)); 1703 udl_reg_write_2(sc, UDL_REG_HSYNCEND, udl_lfsr(val)); 1704 } else { 1705 udl_reg_write_2(sc, UDL_REG_HSYNCSTART, udl_lfsr(val)); 1706 udl_reg_write_2(sc, UDL_REG_HSYNCEND, udl_lfsr(1)); 1707 } 1708 val = vmp->hdisplay; 1709 udl_reg_write_2(sc, UDL_REG_HPIXELS, val); 1710 val = vmp->vtotal; 1711 udl_reg_write_2(sc, UDL_REG_YENDCOUNT, udl_lfsr(val)); 1712 val = vmp->vsync_end - vmp->vsync_start; 1713 if (vmp->flags & VID_PVSYNC) { 1714 udl_reg_write_2(sc, UDL_REG_VSYNCSTART, udl_lfsr(0)); 1715 udl_reg_write_2(sc, UDL_REG_VSYNCEND, udl_lfsr(val)); 1716 } else { 1717 udl_reg_write_2(sc, UDL_REG_VSYNCSTART, udl_lfsr(val)); 1718 udl_reg_write_2(sc, UDL_REG_VSYNCEND, udl_lfsr(0)); 1719 } 1720 val = vmp->vdisplay; 1721 udl_reg_write_2(sc, UDL_REG_VPIXELS, val); 1722 val = vmp->dot_clock / 5; 1723 udl_reg_write_2(sc, UDL_REG_PIXELCLOCK5KHZ, bswap16(val)); 1724 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff); 1725 1726 if (udl_cmd_send(sc) != 0) 1727 return -1; 1728 1729 /* clear screen */ 1730 udl_fill_rect(sc, 0, 0, 0, sc->sc_width, sc->sc_height); 1731 1732 if (udl_cmd_send(sc) != 0) 1733 return -1; 1734 1735 /* show framebuffer content */ 1736 udl_blank(sc, 0); 1737 1738 if (udl_cmd_send(sc) != 0) 1739 return -1; 1740 1741 sc->sc_blank = WSDISPLAYIO_VIDEO_ON; 1742 1743 return 0; 1744 } 1745 1746 static const struct videomode * 1747 udl_videomode_lookup(const char *name) 1748 { 1749 int i; 1750 1751 for (i = 0; i < videomode_count; i++) 1752 if (strcmp(name, videomode_list[i].name) == 0) 1753 return &videomode_list[i]; 1754 1755 return NULL; 1756 } 1757