1 /* $NetBSD: udl.c,v 1.6 2011/11/20 12:29:33 nonaka 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.6 2011/11/20 12:29:33 nonaka 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 return; 366 367 /* 368 * Create device handle to interface descriptor. 369 */ 370 error = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface); 371 if (error != USBD_NORMAL_COMPLETION) 372 return; 373 374 /* 375 * Open bulk TX pipe. 376 */ 377 error = usbd_open_pipe(sc->sc_iface, 1, USBD_EXCLUSIVE_USE, 378 &sc->sc_tx_pipeh); 379 if (error != USBD_NORMAL_COMPLETION) 380 return; 381 382 /* 383 * Allocate bulk command queue. 384 */ 385 #ifdef UDL_EVENT_COUNTERS 386 evcnt_attach_dynamic(&sc->sc_ev_cmdq_get, EVCNT_TYPE_MISC, NULL, 387 device_xname(sc->sc_dev), "udl_cmdq_get"); 388 evcnt_attach_dynamic(&sc->sc_ev_cmdq_put, EVCNT_TYPE_MISC, NULL, 389 device_xname(sc->sc_dev), "udl_cmdq_put"); 390 evcnt_attach_dynamic(&sc->sc_ev_cmdq_wait, EVCNT_TYPE_MISC, NULL, 391 device_xname(sc->sc_dev), "udl_cmdq_wait"); 392 evcnt_attach_dynamic(&sc->sc_ev_cmdq_timeout, EVCNT_TYPE_MISC, NULL, 393 device_xname(sc->sc_dev), "udl_cmdq_timeout"); 394 #endif 395 396 if (udl_cmdq_alloc(sc) != 0) 397 return; 398 399 cv_init(&sc->sc_cv, device_xname(sc->sc_dev)); 400 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_TTY); /* XXX for tty_lock */ 401 402 if ((sc->sc_cmd_cur = udl_cmdq_get(sc)) == NULL) 403 return; 404 UDL_CMD_BUFINIT(sc); 405 406 /* 407 * Initialize chip. 408 */ 409 if (udl_init(sc) != 0) 410 return; 411 412 udl_read_edid(sc); 413 414 /* 415 * Initialize resolution. 416 */ 417 #ifndef UDL_VIDEOMODE 418 if (sc->sc_ei.edid_nmodes != 0 && 419 sc->sc_ei.edid_preferred_mode != NULL) 420 vmp = sc->sc_ei.edid_preferred_mode; 421 else 422 #define UDL_VIDEOMODE "640x480x60" 423 #endif 424 vmp = udl_videomode_lookup(UDL_VIDEOMODE); 425 426 if (vmp == NULL) 427 return; 428 429 sc->sc_width = vmp->hdisplay; 430 sc->sc_height = vmp->vdisplay; 431 sc->sc_offscreen = sc->sc_height * 3 / 2; 432 sc->sc_depth = 16; 433 434 if (udl_set_resolution(sc, vmp) != 0) 435 return; 436 437 sc->sc_defaultscreen.name = "default"; 438 sc->sc_screens[0] = &sc->sc_defaultscreen; 439 sc->sc_screenlist.nscreens = 1; 440 sc->sc_screenlist.screens = sc->sc_screens; 441 442 /* 443 * Set initial wsdisplay emulation mode. 444 */ 445 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 446 447 /* 448 * Attach wsdisplay. 449 */ 450 aa.console = 0; 451 aa.scrdata = &sc->sc_screenlist; 452 aa.accessops = &udl_accessops; 453 aa.accesscookie = sc; 454 455 sc->sc_wsdisplay = 456 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint); 457 458 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 459 } 460 461 static int 462 udl_detach(device_t self, int flags) 463 { 464 struct udl_softc *sc = device_private(self); 465 466 /* 467 * Close bulk TX pipe. 468 */ 469 if (sc->sc_tx_pipeh != NULL) { 470 usbd_abort_pipe(sc->sc_tx_pipeh); 471 usbd_close_pipe(sc->sc_tx_pipeh); 472 } 473 474 /* 475 * Free command xfer buffers. 476 */ 477 udl_cmdq_flush(sc); 478 udl_cmdq_free(sc); 479 480 cv_destroy(&sc->sc_cv); 481 mutex_destroy(&sc->sc_mtx); 482 483 /* 484 * Free Huffman table. 485 */ 486 udl_comp_unload(sc); 487 488 /* 489 * Free framebuffer memory. 490 */ 491 udl_fbmem_free(sc); 492 493 /* 494 * Detach wsdisplay. 495 */ 496 if (sc->sc_wsdisplay != NULL) 497 config_detach(sc->sc_wsdisplay, DETACH_FORCE); 498 499 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 500 501 #ifdef UDL_EVENT_COUNTERS 502 evcnt_detach(&sc->sc_ev_cmdq_get); 503 evcnt_detach(&sc->sc_ev_cmdq_put); 504 evcnt_detach(&sc->sc_ev_cmdq_wait); 505 evcnt_detach(&sc->sc_ev_cmdq_timeout); 506 #endif 507 508 return 0; 509 } 510 511 static int 512 udl_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l) 513 { 514 struct udl_softc *sc = v; 515 #ifdef notyet 516 struct udl_ioctl_damage *d; 517 #endif 518 struct wsdisplay_fbinfo *wdf; 519 u_int mode; 520 521 switch (cmd) { 522 case WSDISPLAYIO_GTYPE: 523 *(u_int *)data = WSDISPLAY_TYPE_DL; 524 return 0; 525 526 case WSDISPLAYIO_GINFO: 527 wdf = (struct wsdisplay_fbinfo *)data; 528 wdf->height = sc->sc_height; 529 wdf->width = sc->sc_width; 530 wdf->depth = sc->sc_depth; 531 wdf->cmsize = 0; 532 return 0; 533 534 case WSDISPLAYIO_GVIDEO: 535 *(u_int *)data = sc->sc_blank; 536 return 0; 537 538 case WSDISPLAYIO_SVIDEO: 539 mode = *(u_int *)data; 540 if (mode == sc->sc_blank) 541 return 0; 542 switch (mode) { 543 case WSDISPLAYIO_VIDEO_OFF: 544 udl_blank(sc, 1); 545 break; 546 case WSDISPLAYIO_VIDEO_ON: 547 udl_blank(sc, 0); 548 break; 549 default: 550 return EINVAL; 551 } 552 udl_cmd_send_async(sc); 553 udl_cmdq_flush(sc); 554 sc->sc_blank = mode; 555 return 0; 556 557 case WSDISPLAYIO_SMODE: 558 mode = *(u_int *)data; 559 if (mode == sc->sc_mode) 560 return 0; 561 switch (mode) { 562 case WSDISPLAYIO_MODE_EMUL: 563 /* clear screen */ 564 udl_fill_rect(sc, 0, 0, 0, sc->sc_width, 565 sc->sc_height); 566 udl_cmd_send_async(sc); 567 udl_cmdq_flush(sc); 568 udl_comp_unload(sc); 569 break; 570 case WSDISPLAYIO_MODE_DUMBFB: 571 if (UDL_CMD_BUFSIZE(sc) > 0) 572 udl_cmd_send_async(sc); 573 udl_cmdq_flush(sc); 574 udl_comp_load(sc); 575 break; 576 default: 577 return EINVAL; 578 } 579 sc->sc_mode = mode; 580 return 0; 581 582 case WSDISPLAYIO_LINEBYTES: 583 *(u_int *)data = sc->sc_width * (sc->sc_depth / 8); 584 return 0; 585 586 #ifdef notyet 587 /* 588 * XXX 589 * OpenBSD allows device specific ioctl()s and use this 590 * UDLIO_DAMAGE for the damage extension ops of X servers. 591 * Before blindly pulling such interfaces, probably we should 592 * discuss how such devices should be handled which have 593 * in-direct framebuffer memories that should be transfered 594 * per updated rectangle regions via MI wscons APIs. 595 */ 596 case UDLIO_DAMAGE: 597 d = (struct udl_ioctl_damage *)data; 598 d->status = UDLIO_STATUS_OK; 599 if (sc->sc_flags & UDL_COMPRDY) 600 udl_draw_rect_comp(sc, d); 601 else 602 udl_draw_rect(sc, d); 603 return 0; 604 #endif 605 } 606 607 return EPASSTHROUGH; 608 } 609 610 static paddr_t 611 udl_mmap(void *v, void *vs, off_t off, int prot) 612 { 613 struct udl_softc *sc = v; 614 vaddr_t vaddr; 615 paddr_t paddr; 616 bool rv; 617 618 if (off < 0 || off > roundup2(UDL_FBMEM_SIZE(sc), PAGE_SIZE)) 619 return -1; 620 621 /* allocate framebuffer memory */ 622 if (udl_fbmem_alloc(sc) != 0) 623 return -1; 624 625 vaddr = (vaddr_t)sc->sc_fbmem + off; 626 rv = pmap_extract(pmap_kernel(), vaddr, &paddr); 627 KASSERT(rv); 628 paddr += vaddr & PGOFSET; 629 630 /* XXX we need MI paddr_t -> mmap cookie API */ 631 #if defined(__alpha__) 632 #define PTOMMAP(paddr) alpha_btop((char *)paddr) 633 #elif defined(__arm__) 634 #define PTOMMAP(paddr) arm_btop((u_long)paddr) 635 #elif defined(__hppa__) 636 #define PTOMMAP(paddr) btop((u_long)paddr) 637 #elif defined(__i386__) || defined(__x86_64__) 638 #define PTOMMAP(paddr) x86_btop(paddr) 639 #elif defined(__m68k__) 640 #define PTOMMAP(paddr) m68k_btop((char *)paddr) 641 #elif defined(__mips__) 642 #define PTOMMAP(paddr) mips_btop(paddr) 643 #elif defined(__powerpc__) 644 #define PTOMMAP(paddr) (paddr) 645 #elif defined(__sh__) 646 #define PTOMMAP(paddr) sh3_btop(paddr) 647 #elif defined(__sparc__) 648 #define PTOMMAP(paddr) (paddr) 649 #elif defined(__sparc64__) 650 #define PTOMMAP(paddr) atop(paddr) 651 #elif defined(__vax__) 652 #define PTOMMAP(paddr) btop((u_int)paddr) 653 #endif 654 655 return PTOMMAP(paddr); 656 } 657 658 static int 659 udl_alloc_screen(void *v, const struct wsscreen_descr *type, 660 void **cookiep, int *curxp, int *curyp, long *attrp) 661 { 662 struct udl_softc *sc = v; 663 664 if (sc->sc_nscreens > 0) 665 return ENOMEM; 666 667 /* 668 * Initialize rasops. 669 */ 670 sc->sc_ri.ri_depth = sc->sc_depth; 671 sc->sc_ri.ri_bits = NULL; 672 sc->sc_ri.ri_width = sc->sc_width; 673 sc->sc_ri.ri_height = sc->sc_height; 674 sc->sc_ri.ri_stride = sc->sc_width * (sc->sc_depth / 8); 675 sc->sc_ri.ri_hw = sc; 676 sc->sc_ri.ri_flg = 0; 677 678 if (sc->sc_depth == 16) { 679 sc->sc_ri.ri_rnum = 5; 680 sc->sc_ri.ri_gnum = 6; 681 sc->sc_ri.ri_bnum = 5; 682 sc->sc_ri.ri_rpos = 11; 683 sc->sc_ri.ri_gpos = 5; 684 sc->sc_ri.ri_bpos = 0; 685 } 686 687 rasops_init(&sc->sc_ri, sc->sc_height / 8, sc->sc_width / 8); 688 689 sc->sc_ri.ri_ops.cursor = udl_cursor; 690 sc->sc_ri.ri_ops.putchar = udl_putchar; 691 sc->sc_ri.ri_ops.copycols = udl_copycols; 692 sc->sc_ri.ri_ops.erasecols = udl_erasecols; 693 sc->sc_ri.ri_ops.copyrows = udl_copyrows; 694 sc->sc_ri.ri_ops.eraserows = udl_eraserows; 695 696 sc->sc_ri.ri_ops.allocattr(&sc->sc_ri, 0, 0, 0, attrp); 697 698 sc->sc_defaultscreen.ncols = sc->sc_ri.ri_cols; 699 sc->sc_defaultscreen.nrows = sc->sc_ri.ri_rows; 700 sc->sc_defaultscreen.textops = &sc->sc_ri.ri_ops; 701 sc->sc_defaultscreen.fontwidth = sc->sc_ri.ri_font->fontwidth; 702 sc->sc_defaultscreen.fontheight = sc->sc_ri.ri_font->fontheight; 703 sc->sc_defaultscreen.capabilities = sc->sc_ri.ri_caps; 704 705 *cookiep = &sc->sc_ri; 706 *curxp = 0; 707 *curyp = 0; 708 709 sc->sc_nscreens++; 710 711 return 0; 712 } 713 714 static void 715 udl_free_screen(void *v, void *cookie) 716 { 717 struct udl_softc *sc = v; 718 719 sc->sc_nscreens--; 720 } 721 722 static int 723 udl_show_screen(void *v, void *cookie, int waitok, 724 void (*cb)(void *, int, int), void *cbarg) 725 { 726 727 return 0; 728 } 729 730 static inline void 731 udl_cmd_add_decomptable(struct udl_softc *sc, uint8_t *buf, int len) 732 { 733 734 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_DECOMP); 735 udl_cmd_add_4(sc, 0x263871cd); /* magic number */ 736 udl_cmd_add_4(sc, 0x00000200); /* 512 byte chunks */ 737 memcpy(sc->sc_cmd_buf, buf, len); 738 sc->sc_cmd_buf += len; 739 } 740 741 static void 742 udl_comp_load(struct udl_softc *sc) 743 { 744 struct udl_huffman *h; 745 uint8_t *decomp; 746 size_t decomp_size; 747 int error, i; 748 749 if (!(sc->sc_flags & UDL_DECOMPRDY)) { 750 error = firmware_load("udl", "udl-decomp", &decomp, 751 &decomp_size); 752 if (error != 0) { 753 aprint_error_dev(sc->sc_dev, 754 "error %d, could not read decomp table %s!\n", 755 error, "udl-decomp"); 756 return; 757 } 758 udl_cmd_add_decomptable(sc, decomp, decomp_size); 759 firmware_free(decomp, decomp_size); 760 if (udl_cmd_send(sc) != 0) 761 return; 762 sc->sc_flags |= UDL_DECOMPRDY; 763 } 764 765 if (!(sc->sc_flags & UDL_COMPRDY)) { 766 error = firmware_load("udl", "udl-comp", &sc->sc_huffman, 767 &sc->sc_huffman_size); 768 if (error != 0) { 769 aprint_error_dev(sc->sc_dev, 770 "error %d, could not read huffman table %s!\n", 771 error, "udl-comp"); 772 return; 773 } 774 h = (struct udl_huffman *)sc->sc_huffman; 775 for (i = 0; i < UDL_HUFFMAN_RECORDS; i++) 776 h[i].bit_pattern = be32toh(h[i].bit_pattern); 777 sc->sc_huffman_base = sc->sc_huffman + UDL_HUFFMAN_BASE; 778 sc->sc_flags |= UDL_COMPRDY; 779 } 780 } 781 782 static void 783 udl_comp_unload(struct udl_softc *sc) 784 { 785 786 if (sc->sc_flags & UDL_COMPRDY) { 787 firmware_free(sc->sc_huffman, sc->sc_huffman_size); 788 sc->sc_huffman = NULL; 789 sc->sc_huffman_size = 0; 790 sc->sc_flags &= ~UDL_COMPRDY; 791 } 792 } 793 794 static int 795 udl_fbmem_alloc(struct udl_softc *sc) 796 { 797 798 if (sc->sc_fbmem == NULL) { 799 sc->sc_fbmem = kmem_alloc(UDL_FBMEM_SIZE(sc), KM_SLEEP); 800 if (sc->sc_fbmem == NULL) 801 return -1; 802 } 803 804 return 0; 805 } 806 807 static void 808 udl_fbmem_free(struct udl_softc *sc) 809 { 810 811 if (sc->sc_fbmem != NULL) { 812 kmem_free(sc->sc_fbmem, UDL_FBMEM_SIZE(sc)); 813 sc->sc_fbmem = NULL; 814 } 815 } 816 817 static int 818 udl_cmdq_alloc(struct udl_softc *sc) 819 { 820 struct udl_cmdq *cmdq; 821 int i; 822 823 TAILQ_INIT(&sc->sc_freecmd); 824 TAILQ_INIT(&sc->sc_xfercmd); 825 826 for (i = 0; i < UDL_NCMDQ; i++) { 827 cmdq = &sc->sc_cmdq[i]; 828 829 cmdq->cq_sc = sc; 830 831 cmdq->cq_xfer = usbd_alloc_xfer(sc->sc_udev); 832 if (cmdq->cq_xfer == NULL) { 833 aprint_error_dev(sc->sc_dev, 834 "%s: can't allocate xfer handle!\n", __func__); 835 goto error; 836 } 837 838 cmdq->cq_buf = 839 usbd_alloc_buffer(cmdq->cq_xfer, UDL_CMD_BUFFER_SIZE); 840 if (cmdq->cq_buf == NULL) { 841 aprint_error_dev(sc->sc_dev, 842 "%s: can't allocate xfer buffer!\n", __func__); 843 goto error; 844 } 845 846 TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmdq, cq_chain); 847 } 848 849 return 0; 850 851 error: 852 udl_cmdq_free(sc); 853 return -1; 854 } 855 856 static void 857 udl_cmdq_free(struct udl_softc *sc) 858 { 859 struct udl_cmdq *cmdq; 860 int i; 861 862 for (i = 0; i < UDL_NCMDQ; i++) { 863 cmdq = &sc->sc_cmdq[i]; 864 865 if (cmdq->cq_xfer != NULL) { 866 usbd_free_xfer(cmdq->cq_xfer); 867 cmdq->cq_xfer = NULL; 868 cmdq->cq_buf = NULL; 869 } 870 } 871 } 872 873 static struct udl_cmdq * 874 udl_cmdq_get(struct udl_softc *sc) 875 { 876 struct udl_cmdq *cmdq; 877 878 cmdq = TAILQ_FIRST(&sc->sc_freecmd); 879 if (cmdq != NULL) { 880 TAILQ_REMOVE(&sc->sc_freecmd, cmdq, cq_chain); 881 UDL_EVCNT_INCR(&sc->sc_ev_cmdq_get); 882 } 883 884 return cmdq; 885 } 886 887 static void 888 udl_cmdq_put(struct udl_softc *sc, struct udl_cmdq *cmdq) 889 { 890 891 TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmdq, cq_chain); 892 UDL_EVCNT_INCR(&sc->sc_ev_cmdq_put); 893 } 894 895 static void 896 udl_cmdq_flush(struct udl_softc *sc) 897 { 898 899 mutex_enter(&sc->sc_mtx); 900 while (TAILQ_FIRST(&sc->sc_xfercmd) != NULL) 901 cv_wait(&sc->sc_cv, &sc->sc_mtx); 902 mutex_exit(&sc->sc_mtx); 903 } 904 905 static void 906 udl_cursor(void *cookie, int on, int row, int col) 907 { 908 struct rasops_info *ri = cookie; 909 struct udl_softc *sc = ri->ri_hw; 910 int x, y, width, height; 911 912 if (ri->ri_flg & RI_CURSOR) 913 udl_restore_char(ri); 914 915 ri->ri_crow = row; 916 ri->ri_ccol = col; 917 918 if (on != 0) { 919 ri->ri_flg |= RI_CURSOR; 920 921 x = col * ri->ri_font->fontwidth; 922 y = row * ri->ri_font->fontheight; 923 width = ri->ri_font->fontwidth; 924 height = ri->ri_font->fontheight; 925 926 /* save the last character block to off-screen */ 927 udl_copy_rect(sc, x, y, 0, sc->sc_offscreen, width, height); 928 929 /* draw cursor */ 930 udl_fill_rect(sc, 0xffff, x, y, width, 1); 931 udl_fill_rect(sc, 0xffff, x, y + 1, 1, height - 2); 932 udl_fill_rect(sc, 0xffff, x + width - 1, y + 1, 1, height - 2); 933 udl_fill_rect(sc, 0xffff, x, y + height - 1, width, 1); 934 935 udl_cmd_send_async(sc); 936 } else 937 ri->ri_flg &= ~RI_CURSOR; 938 } 939 940 static void 941 udl_putchar(void *cookie, int row, int col, u_int uc, long attr) 942 { 943 struct rasops_info *ri = cookie; 944 struct udl_softc *sc = ri->ri_hw; 945 uint16_t rgb16[2]; 946 int fg, bg, underline, x, y, width, height; 947 948 rasops_unpack_attr(attr, &fg, &bg, &underline); 949 rgb16[1] = (uint16_t)ri->ri_devcmap[fg]; 950 rgb16[0] = (uint16_t)ri->ri_devcmap[bg]; 951 952 x = col * ri->ri_font->fontwidth; 953 y = row * ri->ri_font->fontheight; 954 width = ri->ri_font->fontwidth; 955 height = ri->ri_font->fontheight; 956 957 if (uc == ' ') { 958 /* 959 * Writting a block for the space character instead rendering 960 * it from font bits is more slim. 961 */ 962 udl_fill_rect(sc, rgb16[0], x, y, width, height); 963 } else { 964 /* render a character from font bits */ 965 udl_draw_char(ri, rgb16, uc, x, y); 966 } 967 968 if (underline != 0) 969 udl_fill_rect(sc, rgb16[1], x, y + height - 1, width, 1); 970 971 #if 0 972 udl_cmd_send_async(sc); 973 #endif 974 } 975 976 static void 977 udl_copycols(void *cookie, int row, int src, int dst, int num) 978 { 979 struct rasops_info *ri = cookie; 980 struct udl_softc *sc = ri->ri_hw; 981 int sx, dx, y, width, height; 982 983 sx = src * ri->ri_font->fontwidth; 984 dx = dst * ri->ri_font->fontwidth; 985 y = row * ri->ri_font->fontheight; 986 width = num * ri->ri_font->fontwidth; 987 height = ri->ri_font->fontheight; 988 989 /* copy row block to off-screen first to fix overlay-copy problem */ 990 udl_copy_rect(sc, sx, y, 0, sc->sc_offscreen, width, height); 991 992 /* copy row block back from off-screen now */ 993 udl_copy_rect(sc, 0, sc->sc_offscreen, dx, y, width, height); 994 #if 0 995 udl_cmd_send_async(sc); 996 #endif 997 } 998 999 static void 1000 udl_erasecols(void *cookie, int row, int col, int num, long attr) 1001 { 1002 struct rasops_info *ri = cookie; 1003 struct udl_softc *sc = ri->ri_hw; 1004 uint16_t rgb16; 1005 int fg, bg, x, y, width, height; 1006 1007 rasops_unpack_attr(attr, &fg, &bg, NULL); 1008 rgb16 = (uint16_t)ri->ri_devcmap[bg]; 1009 1010 x = col * ri->ri_font->fontwidth; 1011 y = row * ri->ri_font->fontheight; 1012 width = num * ri->ri_font->fontwidth; 1013 height = ri->ri_font->fontheight; 1014 1015 udl_fill_rect(sc, rgb16, x, y, width, height); 1016 #if 0 1017 udl_cmd_send_async(sc); 1018 #endif 1019 } 1020 1021 static void 1022 udl_copyrows(void *cookie, int src, int dst, int num) 1023 { 1024 struct rasops_info *ri = cookie; 1025 struct udl_softc *sc = ri->ri_hw; 1026 int sy, ey, dy, width, height; 1027 1028 width = ri->ri_emuwidth; 1029 height = ri->ri_font->fontheight; 1030 1031 if (dst < src) { 1032 sy = src * height; 1033 ey = (src + num) * height; 1034 dy = dst * height; 1035 1036 while (sy < ey) { 1037 udl_copy_rect(sc, 0, sy, 0, dy, width, height); 1038 sy += height; 1039 dy += height; 1040 } 1041 } else { 1042 sy = (src + num) * height; 1043 ey = src * height; 1044 dy = (dst + num) * height; 1045 1046 while (sy > ey) { 1047 sy -= height; 1048 dy -= height; 1049 udl_copy_rect(sc, 0, sy, 0, dy, width, height); 1050 } 1051 } 1052 #if 0 1053 udl_cmd_send_async(sc); 1054 #endif 1055 } 1056 1057 static void 1058 udl_eraserows(void *cookie, int row, int num, long attr) 1059 { 1060 struct rasops_info *ri = cookie; 1061 struct udl_softc *sc = ri->ri_hw; 1062 uint16_t rgb16; 1063 int fg, bg, y, width, height; 1064 1065 rasops_unpack_attr(attr, &fg, &bg, NULL); 1066 rgb16 = (uint16_t)ri->ri_devcmap[bg]; 1067 1068 y = row * ri->ri_font->fontheight; 1069 width = ri->ri_emuwidth; 1070 height = num * ri->ri_font->fontheight; 1071 1072 udl_fill_rect(sc, rgb16, 0, y, width, height); 1073 #if 0 1074 udl_cmd_send_async(sc); 1075 #endif 1076 } 1077 1078 static void 1079 udl_restore_char(struct rasops_info *ri) 1080 { 1081 struct udl_softc *sc = ri->ri_hw; 1082 int x, y, width, height; 1083 1084 x = ri->ri_ccol * ri->ri_font->fontwidth; 1085 y = ri->ri_crow * ri->ri_font->fontheight; 1086 width = ri->ri_font->fontwidth; 1087 height = ri->ri_font->fontheight; 1088 1089 /* restore the last saved character from off-screen */ 1090 udl_copy_rect(sc, 0, sc->sc_offscreen, x, y, width, height); 1091 } 1092 1093 static void 1094 udl_draw_char(struct rasops_info *ri, uint16_t *rgb16, u_int uc, int x, int y) 1095 { 1096 struct udl_softc *sc = ri->ri_hw; 1097 struct wsdisplay_font *font = ri->ri_font; 1098 uint32_t fontbits; 1099 uint16_t pixels[32]; 1100 uint8_t *fontbase; 1101 int i, soff, eoff; 1102 1103 soff = y * sc->sc_width + x; 1104 eoff = (y + font->fontheight) * sc->sc_width + x; 1105 fontbase = (uint8_t *)font->data + (uc - font->firstchar) * 1106 ri->ri_fontscale; 1107 1108 while (soff < eoff) { 1109 fontbits = 0; 1110 switch (font->stride) { 1111 case 4: 1112 fontbits |= fontbase[3]; 1113 /* FALLTHROUGH */ 1114 case 3: 1115 fontbits |= fontbase[2] << 8; 1116 /* FALLTHROUGH */ 1117 case 2: 1118 fontbits |= fontbase[1] << 16; 1119 /* FALLTHROUGH */ 1120 case 1: 1121 fontbits |= fontbase[0] << 24; 1122 } 1123 fontbase += font->stride; 1124 1125 for (i = 0; i < font->fontwidth; i++) { 1126 pixels[i] = rgb16[(fontbits >> 31) & 1]; 1127 fontbits <<= 1; 1128 } 1129 1130 udl_draw_line(sc, pixels, soff, font->fontwidth); 1131 soff += sc->sc_width; 1132 } 1133 } 1134 1135 static void 1136 udl_copy_rect(struct udl_softc *sc, int sx, int sy, int dx, int dy, int width, 1137 int height) 1138 { 1139 int sbase, soff, ebase, eoff, dbase, doff, width_cur; 1140 1141 sbase = sy * sc->sc_width; 1142 ebase = (sy + height) * sc->sc_width; 1143 dbase = dy * sc->sc_width; 1144 1145 while (width > 0) { 1146 soff = sbase + sx; 1147 eoff = ebase + sx; 1148 doff = dbase + dx; 1149 1150 if (width >= UDL_CMD_WIDTH_MAX) 1151 width_cur = UDL_CMD_WIDTH_MAX; 1152 else 1153 width_cur = width; 1154 1155 while (soff < eoff) { 1156 udl_copy_line(sc, soff, doff, width_cur); 1157 soff += sc->sc_width; 1158 doff += sc->sc_width; 1159 } 1160 1161 sx += width_cur; 1162 dx += width_cur; 1163 width -= width_cur; 1164 } 1165 } 1166 1167 static void 1168 udl_fill_rect(struct udl_softc *sc, uint16_t rgb16, int x, int y, int width, 1169 int height) 1170 { 1171 int sbase, soff, ebase, eoff, width_cur; 1172 1173 sbase = y * sc->sc_width; 1174 ebase = (y + height) * sc->sc_width; 1175 1176 while (width > 0) { 1177 soff = sbase + x; 1178 eoff = ebase + x; 1179 1180 if (width >= UDL_CMD_WIDTH_MAX) 1181 width_cur = UDL_CMD_WIDTH_MAX; 1182 else 1183 width_cur = width; 1184 1185 while (soff < eoff) { 1186 udl_fill_line(sc, rgb16, soff, width_cur); 1187 soff += sc->sc_width; 1188 } 1189 1190 x += width_cur; 1191 width -= width_cur; 1192 } 1193 } 1194 1195 #ifdef notyet 1196 static void 1197 udl_draw_rect(struct udl_softc *sc, struct udl_ioctl_damage *d) 1198 { 1199 int sbase, soff, ebase, eoff, x, y, width, width_cur, height; 1200 1201 x = d->x1; 1202 y = d->y1; 1203 width = d->x2 - d->x1; 1204 height = d->y2 - d->y1; 1205 sbase = y * sc->sc_width; 1206 ebase = (y + height) * sc->sc_width; 1207 1208 while (width > 0) { 1209 soff = sbase + x; 1210 eoff = ebase + x; 1211 1212 if (width >= UDL_CMD_WIDTH_MAX) 1213 width_cur = UDL_CMD_WIDTH_MAX; 1214 else 1215 width_cur = width; 1216 1217 while (soff < eoff) { 1218 udl_draw_line(sc, (uint16_t *)sc->sc_fbmem + soff, 1219 soff, width_cur); 1220 soff += sc->sc_width; 1221 } 1222 1223 x += width_cur; 1224 width -= width_cur; 1225 } 1226 1227 udl_cmd_send_async(sc); 1228 } 1229 1230 static void 1231 udl_draw_rect_comp(struct udl_softc *sc, struct udl_ioctl_damage *d) 1232 { 1233 int soff, eoff, x, y, width, height; 1234 1235 x = d->x1; 1236 y = d->y1; 1237 width = d->x2 - d->x1; 1238 height = d->y2 - d->y1; 1239 soff = y * sc->sc_width + x; 1240 eoff = (y + height) * sc->sc_width + x; 1241 1242 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff); 1243 sc->sc_cmd_cblen = 4; 1244 1245 while (soff < eoff) { 1246 udl_draw_line_comp(sc, (uint16_t *)sc->sc_fbmem + soff, soff, 1247 width); 1248 soff += sc->sc_width; 1249 } 1250 1251 udl_cmd_send_async(sc); 1252 } 1253 #endif 1254 1255 static inline void 1256 udl_copy_line(struct udl_softc *sc, int soff, int doff, int width) 1257 { 1258 1259 if (__predict_false((UDL_CMD_BUFSIZE(sc) + UDL_CMD_COPY_SIZE + 2) > 1260 UDL_CMD_BUFFER_SIZE)) 1261 udl_cmd_send_async(sc); 1262 1263 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_COPY16); 1264 udl_cmd_add_4(sc, ((doff * 2) << 8) | (width & 0xff)); 1265 1266 udl_cmd_add_3(sc, soff * 2); 1267 } 1268 1269 static inline void 1270 udl_fill_line(struct udl_softc *sc, uint16_t rgb16, int off, int width) 1271 { 1272 1273 if (__predict_false((UDL_CMD_BUFSIZE(sc) + UDL_CMD_FILL_SIZE + 2) > 1274 UDL_CMD_BUFFER_SIZE)) 1275 udl_cmd_send_async(sc); 1276 1277 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_RLE16); 1278 udl_cmd_add_4(sc, ((off * 2) << 8) | (width & 0xff)); 1279 1280 udl_cmd_add_1(sc, width); 1281 udl_cmd_add_2(sc, rgb16); 1282 } 1283 1284 static inline void 1285 udl_draw_line(struct udl_softc *sc, uint16_t *buf, int off, int width) 1286 { 1287 1288 if (__predict_false( 1289 (UDL_CMD_BUFSIZE(sc) + UDL_CMD_DRAW_SIZE(width) + 2) > 1290 UDL_CMD_BUFFER_SIZE)) 1291 udl_cmd_send_async(sc); 1292 1293 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_WRITE16); 1294 udl_cmd_add_4(sc, ((off * 2) << 8) | (width & 0xff)); 1295 1296 udl_cmd_add_buf(sc, buf, width); 1297 } 1298 1299 static inline int 1300 udl_cmd_add_buf_comp(struct udl_softc *sc, uint16_t *buf, int width) 1301 { 1302 struct udl_huffman *h; 1303 uint16_t *startp, *endp; 1304 uint32_t bit_pattern; 1305 uint16_t prev; 1306 int16_t diff; 1307 uint8_t bit_count, bit_pos, bit_rem, curlen; 1308 1309 startp = buf; 1310 if (width >= UDL_CMD_WIDTH_MAX) 1311 endp = buf + UDL_CMD_WIDTH_MAX; 1312 else 1313 endp = buf + width; 1314 1315 prev = bit_pos = *sc->sc_cmd_buf = 0; 1316 bit_rem = 8; 1317 1318 /* 1319 * Generate a sub-block with maximal 256 pixels compressed data. 1320 */ 1321 while (buf < endp) { 1322 /* get difference between current and previous pixel */ 1323 diff = *buf - prev; 1324 1325 /* get the huffman difference bit sequence */ 1326 h = (struct udl_huffman *)sc->sc_huffman_base + diff; 1327 bit_count = h->bit_count; 1328 bit_pattern = h->bit_pattern; 1329 1330 curlen = (bit_pos + bit_count + 7) / 8; 1331 if (__predict_false((sc->sc_cmd_cblen + curlen + 1) > 1332 UDL_CMD_COMP_BLOCK_SIZE)) 1333 break; 1334 1335 /* generate one pixel compressed data */ 1336 while (bit_count >= bit_rem) { 1337 *sc->sc_cmd_buf++ |= 1338 (bit_pattern & ((1 << bit_rem) - 1)) << bit_pos; 1339 *sc->sc_cmd_buf = 0; 1340 sc->sc_cmd_cblen++; 1341 bit_count -= bit_rem; 1342 bit_pattern >>= bit_rem; 1343 bit_pos = 0; 1344 bit_rem = 8; 1345 } 1346 1347 if (bit_count > 0) { 1348 *sc->sc_cmd_buf |= 1349 (bit_pattern & ((1 << bit_count) - 1)) << bit_pos; 1350 bit_pos += bit_count; 1351 bit_rem -= bit_count; 1352 } 1353 1354 prev = *buf++; 1355 } 1356 1357 /* 1358 * If we have bits left in our last byte, round up to the next 1359 * byte, so we don't overwrite them. 1360 */ 1361 if (bit_pos > 0) { 1362 sc->sc_cmd_buf++; 1363 sc->sc_cmd_cblen++; 1364 } 1365 1366 /* return how many pixels we have compressed */ 1367 return buf - startp; 1368 } 1369 1370 static inline void 1371 udl_draw_line_comp(struct udl_softc *sc, uint16_t *buf, int off, int width) 1372 { 1373 uint8_t *widthp; 1374 int width_cur; 1375 1376 while (width > 0) { 1377 if (__predict_false( 1378 (sc->sc_cmd_cblen + UDL_CMD_COMP_MIN_SIZE + 1) > 1379 UDL_CMD_COMP_BLOCK_SIZE)) { 1380 if (UDL_CMD_BUFSIZE(sc) < UDL_CMD_COMP_THRESHOLD) { 1381 while (sc->sc_cmd_cblen < 1382 UDL_CMD_COMP_BLOCK_SIZE) { 1383 *sc->sc_cmd_buf++ = 0; 1384 sc->sc_cmd_cblen++; 1385 } 1386 } else 1387 udl_cmd_send_async(sc); 1388 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff); 1389 sc->sc_cmd_cblen = 4; 1390 } 1391 1392 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | 1393 (UDL_BULK_CMD_FB_WRITE16 | UDL_BULK_CMD_FB_COMP)); 1394 udl_cmd_add_4(sc, (off * 2) << 8); 1395 1396 widthp = sc->sc_cmd_buf - 1; 1397 1398 sc->sc_cmd_cblen += UDL_CMD_HEADER_SIZE; 1399 1400 width_cur = udl_cmd_add_buf_comp(sc, buf, width); 1401 1402 *widthp = width_cur; 1403 buf += width_cur; 1404 off += width_cur; 1405 width -= width_cur; 1406 } 1407 } 1408 1409 static int 1410 udl_cmd_send(struct udl_softc *sc) 1411 { 1412 struct udl_cmdq *cmdq; 1413 usbd_status error; 1414 uint32_t len; 1415 1416 cmdq = sc->sc_cmd_cur; 1417 1418 /* mark end of command stack */ 1419 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_EOC); 1420 1421 len = UDL_CMD_BUFSIZE(sc); 1422 1423 /* do xfer */ 1424 error = usbd_bulk_transfer(cmdq->cq_xfer, sc->sc_tx_pipeh, 1425 USBD_NO_COPY, USBD_NO_TIMEOUT, cmdq->cq_buf, &len, "udlcmds"); 1426 1427 UDL_CMD_BUFINIT(sc); 1428 1429 if (error != USBD_NORMAL_COMPLETION) { 1430 aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__, 1431 usbd_errstr(error)); 1432 return -1; 1433 } 1434 1435 return 0; 1436 } 1437 1438 static void 1439 udl_cmd_send_async(struct udl_softc *sc) 1440 { 1441 struct udl_cmdq *cmdq; 1442 usbd_status error; 1443 uint32_t len; 1444 1445 #if 1 1446 /* 1447 * XXX 1448 * All tty ops for wsemul are called with tty_lock spin mutex held, 1449 * so we can't call cv_wait(9) here to acquire a free buffer. 1450 * For now, all commands and data for wsemul ops are discarded 1451 * if there is no free command buffer, and then screen text might 1452 * be corrupted on large scroll ops etc. 1453 * 1454 * Probably we have to reorganize the giant tty_lock mutex, or 1455 * change wsdisplay APIs (especially wsdisplaystart()) to return 1456 * a number of actually handled characters as OpenBSD does, but 1457 * the latter one requires whole API changes around rasops(9) etc. 1458 */ 1459 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 1460 if (TAILQ_FIRST(&sc->sc_freecmd) == NULL) { 1461 UDL_CMD_BUFINIT(sc); 1462 return; 1463 } 1464 } 1465 #endif 1466 1467 cmdq = sc->sc_cmd_cur; 1468 1469 /* mark end of command stack */ 1470 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_EOC); 1471 1472 len = UDL_CMD_BUFSIZE(sc); 1473 1474 /* do xfer */ 1475 mutex_enter(&sc->sc_mtx); 1476 usbd_setup_xfer(cmdq->cq_xfer, sc->sc_tx_pipeh, cmdq, cmdq->cq_buf, 1477 len, USBD_NO_COPY, USBD_NO_TIMEOUT, udl_cmd_send_async_cb); 1478 error = usbd_transfer(cmdq->cq_xfer); 1479 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) { 1480 aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__, 1481 usbd_errstr(error)); 1482 mutex_exit(&sc->sc_mtx); 1483 goto end; 1484 } 1485 1486 TAILQ_INSERT_TAIL(&sc->sc_xfercmd, cmdq, cq_chain); 1487 cmdq = udl_cmdq_get(sc); 1488 mutex_exit(&sc->sc_mtx); 1489 while (cmdq == NULL) { 1490 int err; 1491 UDL_EVCNT_INCR(&sc->sc_ev_cmdq_wait); 1492 mutex_enter(&sc->sc_mtx); 1493 err = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, 1494 mstohz(100) /* XXX is this needed? */); 1495 if (err != 0) { 1496 DPRINTF(("%s: %s: cv timeout (error = %d)\n", 1497 device_xname(sc->sc_dev), __func__, err)); 1498 UDL_EVCNT_INCR(&sc->sc_ev_cmdq_timeout); 1499 } 1500 cmdq = udl_cmdq_get(sc); 1501 mutex_exit(&sc->sc_mtx); 1502 } 1503 sc->sc_cmd_cur = cmdq; 1504 end: 1505 UDL_CMD_BUFINIT(sc); 1506 } 1507 1508 static void 1509 udl_cmd_send_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 1510 usbd_status status) 1511 { 1512 struct udl_cmdq *cmdq = priv; 1513 struct udl_softc *sc = cmdq->cq_sc; 1514 1515 if (status != USBD_NORMAL_COMPLETION) { 1516 aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__, 1517 usbd_errstr(status)); 1518 1519 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1520 return; 1521 if (status == USBD_STALLED) 1522 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh); 1523 } 1524 1525 mutex_enter(&sc->sc_mtx); 1526 TAILQ_REMOVE(&sc->sc_xfercmd, cmdq, cq_chain); 1527 udl_cmdq_put(sc, cmdq); 1528 1529 /* wakeup xfer op that sleeps for a free xfer buffer */ 1530 cv_signal(&sc->sc_cv); 1531 mutex_exit(&sc->sc_mtx); 1532 } 1533 1534 static int 1535 udl_ctrl_msg(struct udl_softc *sc, uint8_t rt, uint8_t r, uint16_t index, 1536 uint16_t value, uint8_t *buf, uint16_t len) 1537 { 1538 usb_device_request_t req; 1539 usbd_status error; 1540 1541 req.bmRequestType = rt; 1542 req.bRequest = r; 1543 USETW(req.wIndex, index); 1544 USETW(req.wValue, value); 1545 USETW(req.wLength, len); 1546 1547 error = usbd_do_request(sc->sc_udev, &req, buf); 1548 if (error != USBD_NORMAL_COMPLETION) { 1549 aprint_error_dev(sc->sc_dev, "%s: %s!\n", __func__, 1550 usbd_errstr(error)); 1551 return -1; 1552 } 1553 1554 return 0; 1555 } 1556 1557 static int 1558 udl_init(struct udl_softc *sc) 1559 { 1560 static uint8_t key[16] = { 1561 0x57, 0xcd, 0xdc, 0xa7, 0x1c, 0x88, 0x5e, 0x15, 1562 0x60, 0xfe, 0xc6, 0x97, 0x16, 0x3d, 0x47, 0xf2 1563 }; 1564 uint8_t status[4], val; 1565 1566 if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_STATUS, 1567 0x0000, 0x0000, status, sizeof(status)) != 0) 1568 return -1; 1569 1570 if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_1, 1571 0xc484, 0x0000, &val, 1) != 0) 1572 return -1; 1573 1574 val = 1; 1575 if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_WRITE_1, 1576 0xc41f, 0x0000, &val, 1) != 0) 1577 return -1; 1578 1579 if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_SET_KEY, 1580 0x0000, 0x0000, key, sizeof(key)) != 0) 1581 return -1; 1582 1583 val = 0; 1584 if (udl_ctrl_msg(sc, UT_WRITE_VENDOR_DEVICE, UDL_CTRL_CMD_WRITE_1, 1585 0xc40b, 0x0000, &val, 1) != 0) 1586 return -1; 1587 1588 return 0; 1589 } 1590 1591 static void 1592 udl_read_edid(struct udl_softc *sc) 1593 { 1594 uint8_t buf[64], edid[128]; 1595 int offset; 1596 1597 memset(&sc->sc_ei, 0, sizeof(struct edid_info)); 1598 1599 offset = 0; 1600 if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID, 1601 0x00a1, (offset << 8), buf, 64) != 0) 1602 return; 1603 if (buf[0] != 0) 1604 return; 1605 memcpy(&edid[offset], &buf[1], 63); 1606 offset += 63; 1607 1608 if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID, 1609 0x00a1, (offset << 8), buf, 64) != 0) 1610 return; 1611 if (buf[0] != 0) 1612 return; 1613 memcpy(&edid[offset], &buf[1], 63); 1614 offset += 63; 1615 1616 if (udl_ctrl_msg(sc, UT_READ_VENDOR_DEVICE, UDL_CTRL_CMD_READ_EDID, 1617 0x00a1, (offset << 8), buf, 3) != 0) 1618 return; 1619 if (buf[0] != 0) 1620 return; 1621 memcpy(&edid[offset], &buf[1], 2); 1622 1623 if (edid_parse(edid, &sc->sc_ei) == 0) { 1624 #ifdef UDL_DEBUG 1625 edid_print(&sc->sc_ei); 1626 #endif 1627 } 1628 } 1629 1630 static void 1631 udl_set_address(struct udl_softc *sc, int start16, int stride16, int start8, 1632 int stride8) 1633 { 1634 udl_reg_write_1(sc, UDL_REG_SYNC, 0x00); 1635 udl_reg_write_3(sc, UDL_REG_ADDR_START16, start16); 1636 udl_reg_write_3(sc, UDL_REG_ADDR_STRIDE16, stride16); 1637 udl_reg_write_3(sc, UDL_REG_ADDR_START8, start8); 1638 udl_reg_write_3(sc, UDL_REG_ADDR_STRIDE8, stride8); 1639 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff); 1640 } 1641 1642 static void 1643 udl_blank(struct udl_softc *sc, int blank) 1644 { 1645 1646 if (blank != 0) 1647 udl_reg_write_1(sc, UDL_REG_BLANK, UDL_REG_BLANK_ON); 1648 else 1649 udl_reg_write_1(sc, UDL_REG_BLANK, UDL_REG_BLANK_OFF); 1650 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff); 1651 } 1652 1653 static uint16_t 1654 udl_lfsr(uint16_t count) 1655 { 1656 uint16_t val = 0xffff; 1657 1658 while (count > 0) { 1659 val = (uint16_t)(val << 1) | ((uint16_t)( 1660 (uint16_t)(val << 0) ^ 1661 (uint16_t)(val << 11) ^ 1662 (uint16_t)(val << 13) ^ 1663 (uint16_t)(val << 14) 1664 ) >> 15); 1665 count--; 1666 } 1667 1668 return val; 1669 } 1670 1671 static int 1672 udl_set_resolution(struct udl_softc *sc, const struct videomode *vmp) 1673 { 1674 uint16_t val; 1675 int start16, stride16, start8, stride8; 1676 1677 /* set video memory offsets */ 1678 start16 = 0; 1679 stride16 = sc->sc_width * 2; 1680 start8 = stride16 * sc->sc_height; 1681 stride8 = sc->sc_width; 1682 udl_set_address(sc, start16, stride16, start8, stride8); 1683 1684 /* write resolution values */ 1685 udl_reg_write_1(sc, UDL_REG_SYNC, 0x00); 1686 udl_reg_write_1(sc, UDL_REG_COLORDEPTH, UDL_REG_COLORDEPTH_16); 1687 val = vmp->htotal - vmp->hsync_start; 1688 udl_reg_write_2(sc, UDL_REG_XDISPLAYSTART, udl_lfsr(val)); 1689 val += vmp->hdisplay; 1690 udl_reg_write_2(sc, UDL_REG_XDISPLAYEND, udl_lfsr(val)); 1691 val = vmp->vtotal - vmp->vsync_start; 1692 udl_reg_write_2(sc, UDL_REG_YDISPLAYSTART, udl_lfsr(val)); 1693 val += vmp->vdisplay; 1694 udl_reg_write_2(sc, UDL_REG_YDISPLAYEND, udl_lfsr(val)); 1695 val = vmp->htotal - 1; 1696 udl_reg_write_2(sc, UDL_REG_XENDCOUNT, udl_lfsr(val)); 1697 val = vmp->hsync_end - vmp->hsync_start + 1; 1698 if (vmp->flags & VID_PHSYNC) { 1699 udl_reg_write_2(sc, UDL_REG_HSYNCSTART, udl_lfsr(1)); 1700 udl_reg_write_2(sc, UDL_REG_HSYNCEND, udl_lfsr(val)); 1701 } else { 1702 udl_reg_write_2(sc, UDL_REG_HSYNCSTART, udl_lfsr(val)); 1703 udl_reg_write_2(sc, UDL_REG_HSYNCEND, udl_lfsr(1)); 1704 } 1705 val = vmp->hdisplay; 1706 udl_reg_write_2(sc, UDL_REG_HPIXELS, val); 1707 val = vmp->vtotal; 1708 udl_reg_write_2(sc, UDL_REG_YENDCOUNT, udl_lfsr(val)); 1709 val = vmp->vsync_end - vmp->vsync_start; 1710 if (vmp->flags & VID_PVSYNC) { 1711 udl_reg_write_2(sc, UDL_REG_VSYNCSTART, udl_lfsr(0)); 1712 udl_reg_write_2(sc, UDL_REG_VSYNCEND, udl_lfsr(val)); 1713 } else { 1714 udl_reg_write_2(sc, UDL_REG_VSYNCSTART, udl_lfsr(val)); 1715 udl_reg_write_2(sc, UDL_REG_VSYNCEND, udl_lfsr(0)); 1716 } 1717 val = vmp->vdisplay; 1718 udl_reg_write_2(sc, UDL_REG_VPIXELS, val); 1719 val = vmp->dot_clock / 5; 1720 udl_reg_write_2(sc, UDL_REG_PIXELCLOCK5KHZ, bswap16(val)); 1721 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff); 1722 1723 if (udl_cmd_send(sc) != 0) 1724 return -1; 1725 1726 /* clear screen */ 1727 udl_fill_rect(sc, 0, 0, 0, sc->sc_width, sc->sc_height); 1728 1729 if (udl_cmd_send(sc) != 0) 1730 return -1; 1731 1732 /* show framebuffer content */ 1733 udl_blank(sc, 0); 1734 1735 if (udl_cmd_send(sc) != 0) 1736 return -1; 1737 1738 sc->sc_blank = WSDISPLAYIO_VIDEO_ON; 1739 1740 return 0; 1741 } 1742 1743 static const struct videomode * 1744 udl_videomode_lookup(const char *name) 1745 { 1746 int i; 1747 1748 for (i = 0; i < videomode_count; i++) 1749 if (strcmp(name, videomode_list[i].name) == 0) 1750 return &videomode_list[i]; 1751 1752 return NULL; 1753 } 1754