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