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