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