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