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