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