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