udl.c revision 1.11.6.6 1 /* $NetBSD: udl.c,v 1.11.6.6 2015/10/06 21:32:15 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.6 2015/10/06 21:32:15 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 usbd_close_pipe(sc->sc_tx_pipeh);
479 }
480
481 /*
482 * Free command xfer buffers.
483 */
484 udl_cmdq_flush(sc);
485 udl_cmdq_free(sc);
486
487 cv_destroy(&sc->sc_cv);
488 mutex_destroy(&sc->sc_mtx);
489
490 /*
491 * Free Huffman table.
492 */
493 udl_comp_unload(sc);
494
495 /*
496 * Free framebuffer memory.
497 */
498 udl_fbmem_free(sc);
499
500 /*
501 * Detach wsdisplay.
502 */
503 if (sc->sc_wsdisplay != NULL)
504 config_detach(sc->sc_wsdisplay, DETACH_FORCE);
505
506 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
507
508 #ifdef UDL_EVENT_COUNTERS
509 evcnt_detach(&sc->sc_ev_cmdq_get);
510 evcnt_detach(&sc->sc_ev_cmdq_put);
511 evcnt_detach(&sc->sc_ev_cmdq_wait);
512 evcnt_detach(&sc->sc_ev_cmdq_timeout);
513 #endif
514
515 return 0;
516 }
517
518 static int
519 udl_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
520 {
521 struct udl_softc *sc = v;
522 #ifdef notyet
523 struct udl_ioctl_damage *d;
524 #endif
525 struct wsdisplay_fbinfo *wdf;
526 u_int mode;
527
528 switch (cmd) {
529 case WSDISPLAYIO_GTYPE:
530 *(u_int *)data = WSDISPLAY_TYPE_DL;
531 return 0;
532
533 case WSDISPLAYIO_GINFO:
534 wdf = (struct wsdisplay_fbinfo *)data;
535 wdf->height = sc->sc_height;
536 wdf->width = sc->sc_width;
537 wdf->depth = sc->sc_depth;
538 wdf->cmsize = 0;
539 return 0;
540
541 case WSDISPLAYIO_GVIDEO:
542 *(u_int *)data = sc->sc_blank;
543 return 0;
544
545 case WSDISPLAYIO_SVIDEO:
546 mode = *(u_int *)data;
547 if (mode == sc->sc_blank)
548 return 0;
549 switch (mode) {
550 case WSDISPLAYIO_VIDEO_OFF:
551 udl_blank(sc, 1);
552 break;
553 case WSDISPLAYIO_VIDEO_ON:
554 udl_blank(sc, 0);
555 break;
556 default:
557 return EINVAL;
558 }
559 udl_cmd_send_async(sc);
560 udl_cmdq_flush(sc);
561 sc->sc_blank = mode;
562 return 0;
563
564 case WSDISPLAYIO_SMODE:
565 mode = *(u_int *)data;
566 if (mode == sc->sc_mode)
567 return 0;
568 switch (mode) {
569 case WSDISPLAYIO_MODE_EMUL:
570 /* clear screen */
571 udl_fill_rect(sc, 0, 0, 0, sc->sc_width,
572 sc->sc_height);
573 udl_cmd_send_async(sc);
574 udl_cmdq_flush(sc);
575 udl_comp_unload(sc);
576 break;
577 case WSDISPLAYIO_MODE_DUMBFB:
578 if (UDL_CMD_BUFSIZE(sc) > 0)
579 udl_cmd_send_async(sc);
580 udl_cmdq_flush(sc);
581 udl_comp_load(sc);
582 break;
583 default:
584 return EINVAL;
585 }
586 sc->sc_mode = mode;
587 return 0;
588
589 case WSDISPLAYIO_LINEBYTES:
590 *(u_int *)data = sc->sc_width * (sc->sc_depth / 8);
591 return 0;
592
593 #ifdef notyet
594 /*
595 * XXX
596 * OpenBSD allows device specific ioctl()s and use this
597 * UDLIO_DAMAGE for the damage extension ops of X servers.
598 * Before blindly pulling such interfaces, probably we should
599 * discuss how such devices should be handled which have
600 * in-direct framebuffer memories that should be transfered
601 * per updated rectangle regions via MI wscons APIs.
602 */
603 case UDLIO_DAMAGE:
604 d = (struct udl_ioctl_damage *)data;
605 d->status = UDLIO_STATUS_OK;
606 if (sc->sc_flags & UDL_COMPRDY)
607 udl_draw_rect_comp(sc, d);
608 else
609 udl_draw_rect(sc, d);
610 return 0;
611 #endif
612 }
613
614 return EPASSTHROUGH;
615 }
616
617 static paddr_t
618 udl_mmap(void *v, void *vs, off_t off, int prot)
619 {
620 struct udl_softc *sc = v;
621 vaddr_t vaddr;
622 paddr_t paddr;
623 bool rv __diagused;
624
625 if (off < 0 || off > roundup2(UDL_FBMEM_SIZE(sc), PAGE_SIZE))
626 return -1;
627
628 /* allocate framebuffer memory */
629 if (udl_fbmem_alloc(sc) != 0)
630 return -1;
631
632 vaddr = (vaddr_t)sc->sc_fbmem + off;
633 rv = pmap_extract(pmap_kernel(), vaddr, &paddr);
634 KASSERT(rv);
635 paddr += vaddr & PGOFSET;
636
637 /* XXX we need MI paddr_t -> mmap cookie API */
638 #if defined(__alpha__)
639 #define PTOMMAP(paddr) alpha_btop((char *)paddr)
640 #elif defined(__arm__)
641 #define PTOMMAP(paddr) arm_btop((u_long)paddr)
642 #elif defined(__hppa__)
643 #define PTOMMAP(paddr) btop((u_long)paddr)
644 #elif defined(__i386__) || defined(__x86_64__)
645 #define PTOMMAP(paddr) x86_btop(paddr)
646 #elif defined(__m68k__)
647 #define PTOMMAP(paddr) m68k_btop((char *)paddr)
648 #elif defined(__mips__)
649 #define PTOMMAP(paddr) mips_btop(paddr)
650 #elif defined(__powerpc__)
651 #define PTOMMAP(paddr) (paddr)
652 #elif defined(__sh__)
653 #define PTOMMAP(paddr) sh3_btop(paddr)
654 #elif defined(__sparc__)
655 #define PTOMMAP(paddr) (paddr)
656 #elif defined(__sparc64__)
657 #define PTOMMAP(paddr) atop(paddr)
658 #elif defined(__vax__)
659 #define PTOMMAP(paddr) btop((u_int)paddr)
660 #endif
661
662 return PTOMMAP(paddr);
663 }
664
665 static int
666 udl_alloc_screen(void *v, const struct wsscreen_descr *type,
667 void **cookiep, int *curxp, int *curyp, long *attrp)
668 {
669 struct udl_softc *sc = v;
670
671 if (sc->sc_nscreens > 0)
672 return ENOMEM;
673
674 /*
675 * Initialize rasops.
676 */
677 sc->sc_ri.ri_depth = sc->sc_depth;
678 sc->sc_ri.ri_bits = NULL;
679 sc->sc_ri.ri_width = sc->sc_width;
680 sc->sc_ri.ri_height = sc->sc_height;
681 sc->sc_ri.ri_stride = sc->sc_width * (sc->sc_depth / 8);
682 sc->sc_ri.ri_hw = sc;
683 sc->sc_ri.ri_flg = 0;
684
685 if (sc->sc_depth == 16) {
686 sc->sc_ri.ri_rnum = 5;
687 sc->sc_ri.ri_gnum = 6;
688 sc->sc_ri.ri_bnum = 5;
689 sc->sc_ri.ri_rpos = 11;
690 sc->sc_ri.ri_gpos = 5;
691 sc->sc_ri.ri_bpos = 0;
692 }
693
694 rasops_init(&sc->sc_ri, sc->sc_height / 8, sc->sc_width / 8);
695
696 sc->sc_ri.ri_ops.cursor = udl_cursor;
697 sc->sc_ri.ri_ops.putchar = udl_putchar;
698 sc->sc_ri.ri_ops.copycols = udl_copycols;
699 sc->sc_ri.ri_ops.erasecols = udl_erasecols;
700 sc->sc_ri.ri_ops.copyrows = udl_copyrows;
701 sc->sc_ri.ri_ops.eraserows = udl_eraserows;
702
703 sc->sc_ri.ri_ops.allocattr(&sc->sc_ri, 0, 0, 0, attrp);
704
705 sc->sc_defaultscreen.ncols = sc->sc_ri.ri_cols;
706 sc->sc_defaultscreen.nrows = sc->sc_ri.ri_rows;
707 sc->sc_defaultscreen.textops = &sc->sc_ri.ri_ops;
708 sc->sc_defaultscreen.fontwidth = sc->sc_ri.ri_font->fontwidth;
709 sc->sc_defaultscreen.fontheight = sc->sc_ri.ri_font->fontheight;
710 sc->sc_defaultscreen.capabilities = sc->sc_ri.ri_caps;
711
712 *cookiep = &sc->sc_ri;
713 *curxp = 0;
714 *curyp = 0;
715
716 sc->sc_nscreens++;
717
718 return 0;
719 }
720
721 static void
722 udl_free_screen(void *v, void *cookie)
723 {
724 struct udl_softc *sc = v;
725
726 sc->sc_nscreens--;
727 }
728
729 static int
730 udl_show_screen(void *v, void *cookie, int waitok,
731 void (*cb)(void *, int, int), void *cbarg)
732 {
733
734 return 0;
735 }
736
737 static inline void
738 udl_cmd_add_decomptable(struct udl_softc *sc, uint8_t *buf, int len)
739 {
740
741 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_DECOMP);
742 udl_cmd_add_4(sc, 0x263871cd); /* magic number */
743 udl_cmd_add_4(sc, 0x00000200); /* 512 byte chunks */
744 memcpy(sc->sc_cmd_buf, buf, len);
745 sc->sc_cmd_buf += len;
746 }
747
748 static void
749 udl_comp_load(struct udl_softc *sc)
750 {
751 struct udl_huffman *h;
752 uint8_t *decomp;
753 size_t decomp_size;
754 int error, i;
755
756 if (!(sc->sc_flags & UDL_DECOMPRDY)) {
757 error = firmware_load("udl", "udl-decomp", &decomp,
758 &decomp_size);
759 if (error != 0) {
760 aprint_error_dev(sc->sc_dev,
761 "error %d, could not read decomp table %s!\n",
762 error, "udl-decomp");
763 return;
764 }
765 udl_cmd_add_decomptable(sc, decomp, decomp_size);
766 firmware_free(decomp, decomp_size);
767 if (udl_cmd_send(sc) != 0)
768 return;
769 sc->sc_flags |= UDL_DECOMPRDY;
770 }
771
772 if (!(sc->sc_flags & UDL_COMPRDY)) {
773 error = firmware_load("udl", "udl-comp", &sc->sc_huffman,
774 &sc->sc_huffman_size);
775 if (error != 0) {
776 aprint_error_dev(sc->sc_dev,
777 "error %d, could not read huffman table %s!\n",
778 error, "udl-comp");
779 return;
780 }
781 h = (struct udl_huffman *)sc->sc_huffman;
782 for (i = 0; i < UDL_HUFFMAN_RECORDS; i++)
783 h[i].bit_pattern = be32toh(h[i].bit_pattern);
784 sc->sc_huffman_base = sc->sc_huffman + UDL_HUFFMAN_BASE;
785 sc->sc_flags |= UDL_COMPRDY;
786 }
787 }
788
789 static void
790 udl_comp_unload(struct udl_softc *sc)
791 {
792
793 if (sc->sc_flags & UDL_COMPRDY) {
794 firmware_free(sc->sc_huffman, sc->sc_huffman_size);
795 sc->sc_huffman = NULL;
796 sc->sc_huffman_size = 0;
797 sc->sc_flags &= ~UDL_COMPRDY;
798 }
799 }
800
801 static int
802 udl_fbmem_alloc(struct udl_softc *sc)
803 {
804
805 if (sc->sc_fbmem == NULL) {
806 sc->sc_fbmem = kmem_alloc(UDL_FBMEM_SIZE(sc), KM_SLEEP);
807 if (sc->sc_fbmem == NULL)
808 return -1;
809 }
810
811 return 0;
812 }
813
814 static void
815 udl_fbmem_free(struct udl_softc *sc)
816 {
817
818 if (sc->sc_fbmem != NULL) {
819 kmem_free(sc->sc_fbmem, UDL_FBMEM_SIZE(sc));
820 sc->sc_fbmem = NULL;
821 }
822 }
823
824 static int
825 udl_cmdq_alloc(struct udl_softc *sc)
826 {
827 struct udl_cmdq *cmdq;
828 int i;
829
830 TAILQ_INIT(&sc->sc_freecmd);
831 TAILQ_INIT(&sc->sc_xfercmd);
832
833 for (i = 0; i < UDL_NCMDQ; i++) {
834 cmdq = &sc->sc_cmdq[i];
835
836 cmdq->cq_sc = sc;
837
838 int err = usbd_create_xfer(sc->sc_tx_pipeh,
839 UDL_CMD_BUFFER_SIZE, 0, 0, &cmdq->cq_xfer);
840 if (err) {
841 aprint_error_dev(sc->sc_dev,
842 "%s: can't allocate xfer handle!\n", __func__);
843 goto error;
844 }
845
846 cmdq->cq_buf = usbd_get_buffer(cmdq->cq_xfer);
847
848 TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmdq, cq_chain);
849 }
850
851 return 0;
852
853 error:
854 udl_cmdq_free(sc);
855 return -1;
856 }
857
858 static void
859 udl_cmdq_free(struct udl_softc *sc)
860 {
861 struct udl_cmdq *cmdq;
862 int i;
863
864 for (i = 0; i < UDL_NCMDQ; i++) {
865 cmdq = &sc->sc_cmdq[i];
866
867 if (cmdq->cq_xfer != NULL) {
868 usbd_destroy_xfer(cmdq->cq_xfer);
869 cmdq->cq_xfer = NULL;
870 cmdq->cq_buf = NULL;
871 }
872 }
873 }
874
875 static struct udl_cmdq *
876 udl_cmdq_get(struct udl_softc *sc)
877 {
878 struct udl_cmdq *cmdq;
879
880 cmdq = TAILQ_FIRST(&sc->sc_freecmd);
881 if (cmdq != NULL) {
882 TAILQ_REMOVE(&sc->sc_freecmd, cmdq, cq_chain);
883 UDL_EVCNT_INCR(&sc->sc_ev_cmdq_get);
884 }
885
886 return cmdq;
887 }
888
889 static void
890 udl_cmdq_put(struct udl_softc *sc, struct udl_cmdq *cmdq)
891 {
892
893 TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmdq, cq_chain);
894 UDL_EVCNT_INCR(&sc->sc_ev_cmdq_put);
895 }
896
897 static void
898 udl_cmdq_flush(struct udl_softc *sc)
899 {
900
901 mutex_enter(&sc->sc_mtx);
902 while (TAILQ_FIRST(&sc->sc_xfercmd) != NULL)
903 cv_wait(&sc->sc_cv, &sc->sc_mtx);
904 mutex_exit(&sc->sc_mtx);
905 }
906
907 static void
908 udl_cursor(void *cookie, int on, int row, int col)
909 {
910 struct rasops_info *ri = cookie;
911 struct udl_softc *sc = ri->ri_hw;
912 int x, y, width, height;
913
914 if (ri->ri_flg & RI_CURSOR)
915 udl_restore_char(ri);
916
917 ri->ri_crow = row;
918 ri->ri_ccol = col;
919
920 if (on != 0) {
921 ri->ri_flg |= RI_CURSOR;
922
923 x = col * ri->ri_font->fontwidth;
924 y = row * ri->ri_font->fontheight;
925 width = ri->ri_font->fontwidth;
926 height = ri->ri_font->fontheight;
927
928 /* save the last character block to off-screen */
929 udl_copy_rect(sc, x, y, 0, sc->sc_offscreen, width, height);
930
931 /* draw cursor */
932 udl_fill_rect(sc, 0xffff, x, y, width, 1);
933 udl_fill_rect(sc, 0xffff, x, y + 1, 1, height - 2);
934 udl_fill_rect(sc, 0xffff, x + width - 1, y + 1, 1, height - 2);
935 udl_fill_rect(sc, 0xffff, x, y + height - 1, width, 1);
936
937 udl_cmd_send_async(sc);
938 } else
939 ri->ri_flg &= ~RI_CURSOR;
940 }
941
942 static void
943 udl_putchar(void *cookie, int row, int col, u_int uc, long attr)
944 {
945 struct rasops_info *ri = cookie;
946 struct udl_softc *sc = ri->ri_hw;
947 uint16_t rgb16[2];
948 int fg, bg, underline, x, y, width, height;
949
950 rasops_unpack_attr(attr, &fg, &bg, &underline);
951 rgb16[1] = (uint16_t)ri->ri_devcmap[fg];
952 rgb16[0] = (uint16_t)ri->ri_devcmap[bg];
953
954 x = col * ri->ri_font->fontwidth;
955 y = row * ri->ri_font->fontheight;
956 width = ri->ri_font->fontwidth;
957 height = ri->ri_font->fontheight;
958
959 if (uc == ' ') {
960 /*
961 * Writting a block for the space character instead rendering
962 * it from font bits is more slim.
963 */
964 udl_fill_rect(sc, rgb16[0], x, y, width, height);
965 } else {
966 /* render a character from font bits */
967 udl_draw_char(ri, rgb16, uc, x, y);
968 }
969
970 if (underline != 0)
971 udl_fill_rect(sc, rgb16[1], x, y + height - 1, width, 1);
972
973 #if 0
974 udl_cmd_send_async(sc);
975 #endif
976 }
977
978 static void
979 udl_copycols(void *cookie, int row, int src, int dst, int num)
980 {
981 struct rasops_info *ri = cookie;
982 struct udl_softc *sc = ri->ri_hw;
983 int sx, dx, y, width, height;
984
985 sx = src * ri->ri_font->fontwidth;
986 dx = dst * ri->ri_font->fontwidth;
987 y = row * ri->ri_font->fontheight;
988 width = num * ri->ri_font->fontwidth;
989 height = ri->ri_font->fontheight;
990
991 /* copy row block to off-screen first to fix overlay-copy problem */
992 udl_copy_rect(sc, sx, y, 0, sc->sc_offscreen, width, height);
993
994 /* copy row block back from off-screen now */
995 udl_copy_rect(sc, 0, sc->sc_offscreen, dx, y, width, height);
996 #if 0
997 udl_cmd_send_async(sc);
998 #endif
999 }
1000
1001 static void
1002 udl_erasecols(void *cookie, int row, int col, int num, long attr)
1003 {
1004 struct rasops_info *ri = cookie;
1005 struct udl_softc *sc = ri->ri_hw;
1006 uint16_t rgb16;
1007 int fg, bg, x, y, width, height;
1008
1009 rasops_unpack_attr(attr, &fg, &bg, NULL);
1010 rgb16 = (uint16_t)ri->ri_devcmap[bg];
1011
1012 x = col * ri->ri_font->fontwidth;
1013 y = row * ri->ri_font->fontheight;
1014 width = num * ri->ri_font->fontwidth;
1015 height = ri->ri_font->fontheight;
1016
1017 udl_fill_rect(sc, rgb16, x, y, width, height);
1018 #if 0
1019 udl_cmd_send_async(sc);
1020 #endif
1021 }
1022
1023 static void
1024 udl_copyrows(void *cookie, int src, int dst, int num)
1025 {
1026 struct rasops_info *ri = cookie;
1027 struct udl_softc *sc = ri->ri_hw;
1028 int sy, ey, dy, width, height;
1029
1030 width = ri->ri_emuwidth;
1031 height = ri->ri_font->fontheight;
1032
1033 if (dst < src) {
1034 sy = src * height;
1035 ey = (src + num) * height;
1036 dy = dst * height;
1037
1038 while (sy < ey) {
1039 udl_copy_rect(sc, 0, sy, 0, dy, width, height);
1040 sy += height;
1041 dy += height;
1042 }
1043 } else {
1044 sy = (src + num) * height;
1045 ey = src * height;
1046 dy = (dst + num) * height;
1047
1048 while (sy > ey) {
1049 sy -= height;
1050 dy -= height;
1051 udl_copy_rect(sc, 0, sy, 0, dy, width, height);
1052 }
1053 }
1054 #if 0
1055 udl_cmd_send_async(sc);
1056 #endif
1057 }
1058
1059 static void
1060 udl_eraserows(void *cookie, int row, int num, long attr)
1061 {
1062 struct rasops_info *ri = cookie;
1063 struct udl_softc *sc = ri->ri_hw;
1064 uint16_t rgb16;
1065 int fg, bg, y, width, height;
1066
1067 rasops_unpack_attr(attr, &fg, &bg, NULL);
1068 rgb16 = (uint16_t)ri->ri_devcmap[bg];
1069
1070 y = row * ri->ri_font->fontheight;
1071 width = ri->ri_emuwidth;
1072 height = num * ri->ri_font->fontheight;
1073
1074 udl_fill_rect(sc, rgb16, 0, y, width, height);
1075 #if 0
1076 udl_cmd_send_async(sc);
1077 #endif
1078 }
1079
1080 static void
1081 udl_restore_char(struct rasops_info *ri)
1082 {
1083 struct udl_softc *sc = ri->ri_hw;
1084 int x, y, width, height;
1085
1086 x = ri->ri_ccol * ri->ri_font->fontwidth;
1087 y = ri->ri_crow * ri->ri_font->fontheight;
1088 width = ri->ri_font->fontwidth;
1089 height = ri->ri_font->fontheight;
1090
1091 /* restore the last saved character from off-screen */
1092 udl_copy_rect(sc, 0, sc->sc_offscreen, x, y, width, height);
1093 }
1094
1095 static void
1096 udl_draw_char(struct rasops_info *ri, uint16_t *rgb16, u_int uc, int x, int y)
1097 {
1098 struct udl_softc *sc = ri->ri_hw;
1099 struct wsdisplay_font *font = ri->ri_font;
1100 uint32_t fontbits;
1101 uint16_t pixels[32];
1102 uint8_t *fontbase;
1103 int i, soff, eoff;
1104
1105 soff = y * sc->sc_width + x;
1106 eoff = (y + font->fontheight) * sc->sc_width + x;
1107 fontbase = (uint8_t *)font->data + (uc - font->firstchar) *
1108 ri->ri_fontscale;
1109
1110 while (soff < eoff) {
1111 fontbits = 0;
1112 switch (font->stride) {
1113 case 4:
1114 fontbits |= fontbase[3];
1115 /* FALLTHROUGH */
1116 case 3:
1117 fontbits |= fontbase[2] << 8;
1118 /* FALLTHROUGH */
1119 case 2:
1120 fontbits |= fontbase[1] << 16;
1121 /* FALLTHROUGH */
1122 case 1:
1123 fontbits |= fontbase[0] << 24;
1124 }
1125 fontbase += font->stride;
1126
1127 for (i = 0; i < font->fontwidth; i++) {
1128 pixels[i] = rgb16[(fontbits >> 31) & 1];
1129 fontbits <<= 1;
1130 }
1131
1132 udl_draw_line(sc, pixels, soff, font->fontwidth);
1133 soff += sc->sc_width;
1134 }
1135 }
1136
1137 static void
1138 udl_copy_rect(struct udl_softc *sc, int sx, int sy, int dx, int dy, int width,
1139 int height)
1140 {
1141 int sbase, soff, ebase, eoff, dbase, doff, width_cur;
1142
1143 sbase = sy * sc->sc_width;
1144 ebase = (sy + height) * sc->sc_width;
1145 dbase = dy * sc->sc_width;
1146
1147 while (width > 0) {
1148 soff = sbase + sx;
1149 eoff = ebase + sx;
1150 doff = dbase + dx;
1151
1152 if (width >= UDL_CMD_WIDTH_MAX)
1153 width_cur = UDL_CMD_WIDTH_MAX;
1154 else
1155 width_cur = width;
1156
1157 while (soff < eoff) {
1158 udl_copy_line(sc, soff, doff, width_cur);
1159 soff += sc->sc_width;
1160 doff += sc->sc_width;
1161 }
1162
1163 sx += width_cur;
1164 dx += width_cur;
1165 width -= width_cur;
1166 }
1167 }
1168
1169 static void
1170 udl_fill_rect(struct udl_softc *sc, uint16_t rgb16, int x, int y, int width,
1171 int height)
1172 {
1173 int sbase, soff, ebase, eoff, width_cur;
1174
1175 sbase = y * sc->sc_width;
1176 ebase = (y + height) * sc->sc_width;
1177
1178 while (width > 0) {
1179 soff = sbase + x;
1180 eoff = ebase + x;
1181
1182 if (width >= UDL_CMD_WIDTH_MAX)
1183 width_cur = UDL_CMD_WIDTH_MAX;
1184 else
1185 width_cur = width;
1186
1187 while (soff < eoff) {
1188 udl_fill_line(sc, rgb16, soff, width_cur);
1189 soff += sc->sc_width;
1190 }
1191
1192 x += width_cur;
1193 width -= width_cur;
1194 }
1195 }
1196
1197 #ifdef notyet
1198 static void
1199 udl_draw_rect(struct udl_softc *sc, struct udl_ioctl_damage *d)
1200 {
1201 int sbase, soff, ebase, eoff, x, y, width, width_cur, height;
1202
1203 x = d->x1;
1204 y = d->y1;
1205 width = d->x2 - d->x1;
1206 height = d->y2 - d->y1;
1207 sbase = y * sc->sc_width;
1208 ebase = (y + height) * sc->sc_width;
1209
1210 while (width > 0) {
1211 soff = sbase + x;
1212 eoff = ebase + x;
1213
1214 if (width >= UDL_CMD_WIDTH_MAX)
1215 width_cur = UDL_CMD_WIDTH_MAX;
1216 else
1217 width_cur = width;
1218
1219 while (soff < eoff) {
1220 udl_draw_line(sc, (uint16_t *)sc->sc_fbmem + soff,
1221 soff, width_cur);
1222 soff += sc->sc_width;
1223 }
1224
1225 x += width_cur;
1226 width -= width_cur;
1227 }
1228
1229 udl_cmd_send_async(sc);
1230 }
1231
1232 static void
1233 udl_draw_rect_comp(struct udl_softc *sc, struct udl_ioctl_damage *d)
1234 {
1235 int soff, eoff, x, y, width, height;
1236
1237 x = d->x1;
1238 y = d->y1;
1239 width = d->x2 - d->x1;
1240 height = d->y2 - d->y1;
1241 soff = y * sc->sc_width + x;
1242 eoff = (y + height) * sc->sc_width + x;
1243
1244 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
1245 sc->sc_cmd_cblen = 4;
1246
1247 while (soff < eoff) {
1248 udl_draw_line_comp(sc, (uint16_t *)sc->sc_fbmem + soff, soff,
1249 width);
1250 soff += sc->sc_width;
1251 }
1252
1253 udl_cmd_send_async(sc);
1254 }
1255 #endif
1256
1257 static inline void
1258 udl_copy_line(struct udl_softc *sc, int soff, int doff, int width)
1259 {
1260
1261 if (__predict_false((UDL_CMD_BUFSIZE(sc) + UDL_CMD_COPY_SIZE + 2) >
1262 UDL_CMD_BUFFER_SIZE))
1263 udl_cmd_send_async(sc);
1264
1265 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_COPY16);
1266 udl_cmd_add_4(sc, ((doff * 2) << 8) | (width & 0xff));
1267
1268 udl_cmd_add_3(sc, soff * 2);
1269 }
1270
1271 static inline void
1272 udl_fill_line(struct udl_softc *sc, uint16_t rgb16, int off, int width)
1273 {
1274
1275 if (__predict_false((UDL_CMD_BUFSIZE(sc) + UDL_CMD_FILL_SIZE + 2) >
1276 UDL_CMD_BUFFER_SIZE))
1277 udl_cmd_send_async(sc);
1278
1279 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_RLE16);
1280 udl_cmd_add_4(sc, ((off * 2) << 8) | (width & 0xff));
1281
1282 udl_cmd_add_1(sc, width);
1283 udl_cmd_add_2(sc, rgb16);
1284 }
1285
1286 static inline void
1287 udl_draw_line(struct udl_softc *sc, uint16_t *buf, int off, int width)
1288 {
1289
1290 if (__predict_false(
1291 (UDL_CMD_BUFSIZE(sc) + UDL_CMD_DRAW_SIZE(width) + 2) >
1292 UDL_CMD_BUFFER_SIZE))
1293 udl_cmd_send_async(sc);
1294
1295 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) | UDL_BULK_CMD_FB_WRITE16);
1296 udl_cmd_add_4(sc, ((off * 2) << 8) | (width & 0xff));
1297
1298 udl_cmd_add_buf(sc, buf, width);
1299 }
1300
1301 #ifdef notyet
1302 static inline int
1303 udl_cmd_add_buf_comp(struct udl_softc *sc, uint16_t *buf, int width)
1304 {
1305 struct udl_huffman *h;
1306 uint16_t *startp, *endp;
1307 uint32_t bit_pattern;
1308 uint16_t prev;
1309 int16_t diff;
1310 uint8_t bit_count, bit_pos, bit_rem, curlen;
1311
1312 startp = buf;
1313 if (width >= UDL_CMD_WIDTH_MAX)
1314 endp = buf + UDL_CMD_WIDTH_MAX;
1315 else
1316 endp = buf + width;
1317
1318 prev = bit_pos = *sc->sc_cmd_buf = 0;
1319 bit_rem = 8;
1320
1321 /*
1322 * Generate a sub-block with maximal 256 pixels compressed data.
1323 */
1324 while (buf < endp) {
1325 /* get difference between current and previous pixel */
1326 diff = *buf - prev;
1327
1328 /* get the huffman difference bit sequence */
1329 h = (struct udl_huffman *)sc->sc_huffman_base + diff;
1330 bit_count = h->bit_count;
1331 bit_pattern = h->bit_pattern;
1332
1333 curlen = (bit_pos + bit_count + 7) / 8;
1334 if (__predict_false((sc->sc_cmd_cblen + curlen + 1) >
1335 UDL_CMD_COMP_BLOCK_SIZE))
1336 break;
1337
1338 /* generate one pixel compressed data */
1339 while (bit_count >= bit_rem) {
1340 *sc->sc_cmd_buf++ |=
1341 (bit_pattern & ((1 << bit_rem) - 1)) << bit_pos;
1342 *sc->sc_cmd_buf = 0;
1343 sc->sc_cmd_cblen++;
1344 bit_count -= bit_rem;
1345 bit_pattern >>= bit_rem;
1346 bit_pos = 0;
1347 bit_rem = 8;
1348 }
1349
1350 if (bit_count > 0) {
1351 *sc->sc_cmd_buf |=
1352 (bit_pattern & ((1 << bit_count) - 1)) << bit_pos;
1353 bit_pos += bit_count;
1354 bit_rem -= bit_count;
1355 }
1356
1357 prev = *buf++;
1358 }
1359
1360 /*
1361 * If we have bits left in our last byte, round up to the next
1362 * byte, so we don't overwrite them.
1363 */
1364 if (bit_pos > 0) {
1365 sc->sc_cmd_buf++;
1366 sc->sc_cmd_cblen++;
1367 }
1368
1369 /* return how many pixels we have compressed */
1370 return buf - startp;
1371 }
1372
1373 static inline void
1374 udl_draw_line_comp(struct udl_softc *sc, uint16_t *buf, int off, int width)
1375 {
1376 uint8_t *widthp;
1377 int width_cur;
1378
1379 while (width > 0) {
1380 if (__predict_false(
1381 (sc->sc_cmd_cblen + UDL_CMD_COMP_MIN_SIZE + 1) >
1382 UDL_CMD_COMP_BLOCK_SIZE)) {
1383 if (UDL_CMD_BUFSIZE(sc) < UDL_CMD_COMP_THRESHOLD) {
1384 while (sc->sc_cmd_cblen <
1385 UDL_CMD_COMP_BLOCK_SIZE) {
1386 *sc->sc_cmd_buf++ = 0;
1387 sc->sc_cmd_cblen++;
1388 }
1389 } else
1390 udl_cmd_send_async(sc);
1391 udl_reg_write_1(sc, UDL_REG_SYNC, 0xff);
1392 sc->sc_cmd_cblen = 4;
1393 }
1394
1395 udl_cmd_add_2(sc, (UDL_BULK_SOC << 8) |
1396 (UDL_BULK_CMD_FB_WRITE16 | UDL_BULK_CMD_FB_COMP));
1397 udl_cmd_add_4(sc, (off * 2) << 8);
1398
1399 widthp = sc->sc_cmd_buf - 1;
1400
1401 sc->sc_cmd_cblen += UDL_CMD_HEADER_SIZE;
1402
1403 width_cur = udl_cmd_add_buf_comp(sc, buf, width);
1404
1405 *widthp = width_cur;
1406 buf += width_cur;
1407 off += width_cur;
1408 width -= width_cur;
1409 }
1410 }
1411 #endif
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, 0,
1429 USBD_NO_TIMEOUT, cmdq->cq_buf, &len);
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, cmdq, cmdq->cq_buf,
1481 len, 0, 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(struct usbd_xfer *xfer, void * 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 /* signal 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