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