Home | History | Annotate | Line # | Download | only in usb
ukbd.c revision 1.67
      1 /*      $NetBSD: ukbd.c,v 1.67 2001/09/28 23:42:17 augustss Exp $        */
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * HID spec: http://www.usb.org/developers/data/devclass/hid1_1.pdf
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/callout.h>
     47 #include <sys/kernel.h>
     48 #include <sys/device.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/tty.h>
     51 #include <sys/file.h>
     52 #include <sys/select.h>
     53 #include <sys/proc.h>
     54 #include <sys/vnode.h>
     55 #include <sys/poll.h>
     56 
     57 #include <dev/usb/usb.h>
     58 #include <dev/usb/usbhid.h>
     59 
     60 #include <dev/usb/usbdi.h>
     61 #include <dev/usb/usbdi_util.h>
     62 #include <dev/usb/usbdevs.h>
     63 #include <dev/usb/usb_quirks.h>
     64 #include <dev/usb/hid.h>
     65 #include <dev/usb/ukbdvar.h>
     66 
     67 #include <dev/wscons/wsconsio.h>
     68 #include <dev/wscons/wskbdvar.h>
     69 #include <dev/wscons/wsksymdef.h>
     70 #include <dev/wscons/wsksymvar.h>
     71 
     72 #include "opt_wsdisplay_compat.h"
     73 
     74 #ifdef UKBD_DEBUG
     75 #define DPRINTF(x)	if (ukbddebug) logprintf x
     76 #define DPRINTFN(n,x)	if (ukbddebug>(n)) logprintf x
     77 int	ukbddebug = 0;
     78 #else
     79 #define DPRINTF(x)
     80 #define DPRINTFN(n,x)
     81 #endif
     82 
     83 #define NKEYCODE 6
     84 
     85 #define NUM_LOCK 0x01
     86 #define CAPS_LOCK 0x02
     87 #define SCROLL_LOCK 0x04
     88 
     89 struct ukbd_data {
     90 	u_int8_t	modifiers;
     91 #define MOD_CONTROL_L	0x01
     92 #define MOD_CONTROL_R	0x10
     93 #define MOD_SHIFT_L	0x02
     94 #define MOD_SHIFT_R	0x20
     95 #define MOD_ALT_L	0x04
     96 #define MOD_ALT_R	0x40
     97 #define MOD_WIN_L	0x08
     98 #define MOD_WIN_R	0x80
     99 	u_int8_t	reserved;
    100 	u_int8_t	keycode[NKEYCODE];
    101 };
    102 
    103 #define PRESS    0x000
    104 #define RELEASE  0x100
    105 #define CODEMASK 0x0ff
    106 
    107 /* Translate USB bitmap to USB keycode. */
    108 #define NMOD 8
    109 Static const struct {
    110 	int mask, key;
    111 } ukbd_mods[NMOD] = {
    112 	{ MOD_CONTROL_L, 224 },
    113 	{ MOD_CONTROL_R, 228 },
    114 	{ MOD_SHIFT_L,   225 },
    115 	{ MOD_SHIFT_R,   229 },
    116 	{ MOD_ALT_L,     226 },
    117 	{ MOD_ALT_R,     230 },
    118 	{ MOD_WIN_L,     227 },
    119 	{ MOD_WIN_R,     231 },
    120 };
    121 
    122 #if defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD)
    123 #define NN 0			/* no translation */
    124 /*
    125  * Translate USB keycodes to US keyboard XT scancodes.
    126  * Scancodes >= 128 represent EXTENDED keycodes.
    127  */
    128 Static const u_int8_t ukbd_trtab[256] = {
    129 	  NN,  NN,  NN,  NN,  30,  48,  46,  32, /* 00 - 07 */
    130 	  18,  33,  34,  35,  23,  36,  37,  38, /* 08 - 0F */
    131 	  50,  49,  24,  25,  16,  19,  31,  20, /* 10 - 17 */
    132 	  22,  47,  17,  45,  21,  44,   2,   3, /* 18 - 1F */
    133 	   4,   5,   6,   7,   8,   9,  10,  11, /* 20 - 27 */
    134 	  28,   1,  14,  15,  57,  12,  13,  26, /* 28 - 2F */
    135 	  27,  43,  43,  39,  40,  41,  51,  52, /* 30 - 37 */
    136 	  53,  58,  59,  60,  61,  62,  63,  64, /* 38 - 3F */
    137 	  65,  66,  67,  68,  87,  88, 170,  70, /* 40 - 47 */
    138 	 127, 210, 199, 201, 211, 207, 209, 205, /* 48 - 4F */
    139 	 203, 208, 200,  69, 181,  55,  74,  78, /* 50 - 57 */
    140 	 156,  79,  80,  81,  75,  76,  77,  71, /* 58 - 5F */
    141           72,  73,  82,  83,  86, 221,  NN,  NN, /* 60 - 67 */
    142           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 68 - 6F */
    143           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 70 - 77 */
    144           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 78 - 7F */
    145           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 80 - 87 */
    146           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 88 - 8F */
    147           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 90 - 97 */
    148           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 98 - 9F */
    149           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* A0 - A7 */
    150           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* A8 - AF */
    151           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* B0 - B7 */
    152           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* B8 - BF */
    153           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* C0 - C7 */
    154           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* C8 - CF */
    155           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* D0 - D7 */
    156           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* D8 - DF */
    157           29,  42,  56, 219,  157, 54,  184,220, /* E0 - E7 */
    158           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* E8 - EF */
    159           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* F0 - F7 */
    160           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* F8 - FF */
    161 };
    162 #endif /* defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD) */
    163 
    164 #define KEY_ERROR 0x01
    165 
    166 #define MAXKEYS (NMOD+2*NKEYCODE)
    167 
    168 struct ukbd_softc {
    169 	USBBASEDEVICE	sc_dev;		/* base device */
    170 	usbd_device_handle sc_udev;
    171 	usbd_interface_handle sc_iface;	/* interface */
    172 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
    173 	int sc_ep_addr;
    174 
    175 	struct ukbd_data sc_ndata;
    176 	struct ukbd_data sc_odata;
    177 
    178 	char sc_enabled;
    179 
    180 	int sc_console_keyboard;	/* we are the console keyboard */
    181 
    182 	char sc_debounce;		/* for quirk handling */
    183 	struct callout sc_delay;	/* for quirk handling */
    184 	struct ukbd_data sc_data;	/* for quirk handling */
    185 
    186 	int sc_leds;
    187 #if defined(__NetBSD__)
    188 	struct callout sc_rawrepeat_ch;
    189 
    190 	struct device *sc_wskbddev;
    191 #if defined(WSDISPLAY_COMPAT_RAWKBD)
    192 #define REP_DELAY1 400
    193 #define REP_DELAYN 100
    194 	int sc_rawkbd;
    195 	int sc_nrep;
    196 	char sc_rep[MAXKEYS];
    197 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
    198 
    199 	int sc_polling;
    200 	int sc_npollchar;
    201 	u_int16_t sc_pollchars[MAXKEYS];
    202 #endif /* defined(__NetBSD__) */
    203 
    204 	u_char sc_dying;
    205 };
    206 
    207 #ifdef UKBD_DEBUG
    208 #define UKBDTRACESIZE 64
    209 struct ukbdtraceinfo {
    210 	int unit;
    211 	struct timeval tv;
    212 	struct ukbd_data ud;
    213 };
    214 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
    215 int ukbdtraceindex = 0;
    216 int ukbdtrace = 0;
    217 void ukbdtracedump(void);
    218 void
    219 ukbdtracedump(void)
    220 {
    221 	int i;
    222 	for (i = 0; i < UKBDTRACESIZE; i++) {
    223 		struct ukbdtraceinfo *p =
    224 		    &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
    225 		printf("%lu.%06lu: mod=0x%02x key0=0x%02x key1=0x%02x "
    226 		       "key2=0x%02x key3=0x%02x\n",
    227 		       p->tv.tv_sec, p->tv.tv_usec,
    228 		       p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1],
    229 		       p->ud.keycode[2], p->ud.keycode[3]);
    230 	}
    231 }
    232 #endif
    233 
    234 #define	UKBDUNIT(dev)	(minor(dev))
    235 #define	UKBD_CHUNK	128	/* chunk size for read */
    236 #define	UKBD_BSIZE	1020	/* buffer size */
    237 
    238 Static int	ukbd_is_console;
    239 
    240 Static void	ukbd_cngetc(void *, u_int *, int *);
    241 Static void	ukbd_cnpollc(void *, int);
    242 
    243 #if defined(__NetBSD__)
    244 const struct wskbd_consops ukbd_consops = {
    245 	ukbd_cngetc,
    246 	ukbd_cnpollc,
    247 };
    248 #endif
    249 
    250 Static void	ukbd_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    251 Static void	ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud);
    252 Static void	ukbd_delayed_decode(void *addr);
    253 
    254 Static int	ukbd_enable(void *, int);
    255 Static void	ukbd_set_leds(void *, int);
    256 
    257 #if defined(__NetBSD__)
    258 Static int	ukbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
    259 #ifdef WSDISPLAY_COMPAT_RAWKBD
    260 Static void	ukbd_rawrepeat(void *v);
    261 #endif
    262 
    263 const struct wskbd_accessops ukbd_accessops = {
    264 	ukbd_enable,
    265 	ukbd_set_leds,
    266 	ukbd_ioctl,
    267 };
    268 
    269 extern const struct wscons_keydesc ukbd_keydesctab[];
    270 
    271 const struct wskbd_mapdata ukbd_keymapdata = {
    272 	ukbd_keydesctab,
    273 #ifdef UKBD_LAYOUT
    274 	UKBD_LAYOUT,
    275 #else
    276 	KB_US,
    277 #endif
    278 };
    279 #endif
    280 
    281 USB_DECLARE_DRIVER(ukbd);
    282 
    283 USB_MATCH(ukbd)
    284 {
    285 	USB_MATCH_START(ukbd, uaa);
    286 	usb_interface_descriptor_t *id;
    287 
    288 	/* Check that this is a keyboard that speaks the boot protocol. */
    289 	if (uaa->iface == NULL)
    290 		return (UMATCH_NONE);
    291 	id = usbd_get_interface_descriptor(uaa->iface);
    292 	if (id == NULL ||
    293 	    id->bInterfaceClass != UICLASS_HID ||
    294 	    id->bInterfaceSubClass != UISUBCLASS_BOOT ||
    295 	    id->bInterfaceProtocol != UIPROTO_BOOT_KEYBOARD)
    296 		return (UMATCH_NONE);
    297 	return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    298 }
    299 
    300 USB_ATTACH(ukbd)
    301 {
    302 	USB_ATTACH_START(ukbd, sc, uaa);
    303 	usbd_interface_handle iface = uaa->iface;
    304 	usb_interface_descriptor_t *id;
    305 	usb_endpoint_descriptor_t *ed;
    306 	usbd_status err;
    307 	u_int32_t qflags;
    308 	char devinfo[1024];
    309 #if defined(__NetBSD__)
    310 	struct wskbddev_attach_args a;
    311 #else
    312 	int i;
    313 #endif
    314 
    315 	sc->sc_udev = uaa->device;
    316 	sc->sc_iface = iface;
    317 	id = usbd_get_interface_descriptor(iface);
    318 	usbd_devinfo(uaa->device, 0, devinfo);
    319 	USB_ATTACH_SETUP;
    320 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    321 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    322 
    323 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    324 	if (ed == NULL) {
    325 		printf("%s: could not read endpoint descriptor\n",
    326 		       USBDEVNAME(sc->sc_dev));
    327 		USB_ATTACH_ERROR_RETURN;
    328 	}
    329 
    330 	DPRINTFN(10,("ukbd_attach: bLength=%d bDescriptorType=%d "
    331 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    332 		     " bInterval=%d\n",
    333 		     ed->bLength, ed->bDescriptorType,
    334 		     ed->bEndpointAddress & UE_ADDR,
    335 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    336 		     ed->bmAttributes & UE_XFERTYPE,
    337 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    338 
    339 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    340 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    341 		printf("%s: unexpected endpoint\n",
    342 		       USBDEVNAME(sc->sc_dev));
    343 		USB_ATTACH_ERROR_RETURN;
    344 	}
    345 
    346 	qflags = usbd_get_quirks(uaa->device)->uq_flags;
    347 	if ((qflags & UQ_NO_SET_PROTO) == 0) {
    348 		err = usbd_set_protocol(iface, 0);
    349 		DPRINTFN(5, ("ukbd_attach: protocol set\n"));
    350 		if (err) {
    351 			printf("%s: set protocol failed\n",
    352 			    USBDEVNAME(sc->sc_dev));
    353 			USB_ATTACH_ERROR_RETURN;
    354 		}
    355 	}
    356 	sc->sc_debounce = (qflags & UQ_SPUR_BUT_UP) != 0;
    357 
    358 	/* Ignore if SETIDLE fails since it is not crucial. */
    359 	(void)usbd_set_idle(iface, 0, 0);
    360 
    361 	sc->sc_ep_addr = ed->bEndpointAddress;
    362 
    363 	/*
    364 	 * Remember if we're the console keyboard.
    365 	 *
    366 	 * XXX This always picks the first keyboard on the
    367 	 * first USB bus, but what else can we really do?
    368 	 */
    369 	if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
    370 		/* Don't let any other keyboard have it. */
    371 		ukbd_is_console = 0;
    372 	}
    373 
    374 	if (sc->sc_console_keyboard) {
    375 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
    376 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
    377 		ukbd_enable(sc, 1);
    378 	}
    379 
    380 	a.console = sc->sc_console_keyboard;
    381 
    382 	a.keymap = &ukbd_keymapdata;
    383 
    384 	a.accessops = &ukbd_accessops;
    385 	a.accesscookie = sc;
    386 
    387 	callout_init(&sc->sc_rawrepeat_ch);
    388 
    389 	callout_init(&sc->sc_delay);
    390 
    391 	/* Flash the leds; no real purpose, just shows we're alive. */
    392 	ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
    393 	usbd_delay_ms(uaa->device, 400);
    394 	ukbd_set_leds(sc, 0);
    395 
    396 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    397 			   USBDEV(sc->sc_dev));
    398 
    399 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    400 
    401 	USB_ATTACH_SUCCESS_RETURN;
    402 }
    403 
    404 int
    405 ukbd_enable(void *v, int on)
    406 {
    407 	struct ukbd_softc *sc = v;
    408 	usbd_status err;
    409 
    410 	if (on && sc->sc_dying)
    411 		return (EIO);
    412 
    413 	/* Should only be called to change state */
    414 	if (sc->sc_enabled == on) {
    415 #ifdef DIAGNOSTIC
    416 		printf("ukbd_enable: %s: bad call on=%d\n",
    417 		       USBDEVNAME(sc->sc_dev), on);
    418 #endif
    419 		return (EBUSY);
    420 	}
    421 
    422 	DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
    423 	if (on) {
    424 		/* Set up interrupt pipe. */
    425 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    426 			  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
    427 			  &sc->sc_ndata, sizeof(sc->sc_ndata), ukbd_intr,
    428 			  USBD_DEFAULT_INTERVAL);
    429 		if (err)
    430 			return (EIO);
    431 	} else {
    432 		/* Disable interrupts. */
    433 		usbd_abort_pipe(sc->sc_intrpipe);
    434 		usbd_close_pipe(sc->sc_intrpipe);
    435 	}
    436 	sc->sc_enabled = on;
    437 
    438 	return (0);
    439 }
    440 
    441 int
    442 ukbd_activate(device_ptr_t self, enum devact act)
    443 {
    444 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
    445 	int rv = 0;
    446 
    447 	switch (act) {
    448 	case DVACT_ACTIVATE:
    449 		return (EOPNOTSUPP);
    450 		break;
    451 
    452 	case DVACT_DEACTIVATE:
    453 		if (sc->sc_wskbddev != NULL)
    454 			rv = config_deactivate(sc->sc_wskbddev);
    455 		sc->sc_dying = 1;
    456 		break;
    457 	}
    458 	return (rv);
    459 }
    460 
    461 USB_DETACH(ukbd)
    462 {
    463 	USB_DETACH_START(ukbd, sc);
    464 	int rv = 0;
    465 
    466 	DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
    467 
    468 	if (sc->sc_console_keyboard) {
    469 #if 0
    470 		/*
    471 		 * XXX Should probably disconnect our consops,
    472 		 * XXX and either notify some other keyboard that
    473 		 * XXX it can now be the console, or if there aren't
    474 		 * XXX any more USB keyboards, set ukbd_is_console
    475 		 * XXX back to 1 so that the next USB keyboard attached
    476 		 * XXX to the system will get it.
    477 		 */
    478 		panic("ukbd_detach: console keyboard");
    479 #else
    480 		/*
    481 		 * Disconnect our consops and set ukbd_is_console
    482 		 * back to 1 so that the next USB keyboard attached
    483 		 * to the system will get it.
    484 		 * XXX Should notify some other keyboard that it can be
    485 		 * XXX console, if there are any other keyboards.
    486 		 */
    487 		printf("%s: was console keyboard\n", USBDEVNAME(sc->sc_dev));
    488 		wskbd_cndetach();
    489 		ukbd_is_console = 1;
    490 #endif
    491 	}
    492 	/* No need to do reference counting of ukbd, wskbd has all the goo. */
    493 	if (sc->sc_wskbddev != NULL)
    494 		rv = config_detach(sc->sc_wskbddev, flags);
    495 
    496 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    497 			   USBDEV(sc->sc_dev));
    498 
    499 	return (rv);
    500 }
    501 
    502 void
    503 ukbd_intr(xfer, addr, status)
    504 	usbd_xfer_handle xfer;
    505 	usbd_private_handle addr;
    506 	usbd_status status;
    507 {
    508 	struct ukbd_softc *sc = addr;
    509 	struct ukbd_data *ud = &sc->sc_ndata;
    510 
    511 	DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
    512 	if (status == USBD_CANCELLED)
    513 		return;
    514 
    515 	if (status) {
    516 		DPRINTF(("ukbd_intr: status=%d\n", status));
    517 		if (status == USBD_STALLED)
    518 			usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    519 		return;
    520 	}
    521 
    522 	if (sc->sc_debounce) {
    523 		/*
    524 		 * Some keyboards have a peculiar quirk.  They sometimes
    525 		 * generate a key up followed by a key down for the same
    526 		 * key after about 10 ms.
    527 		 * We avoid this bug by holding off decoding for 20 ms.
    528 		 */
    529 		sc->sc_data = *ud;
    530 		callout_reset(&sc->sc_delay, hz / 50, ukbd_delayed_decode, sc);
    531 	} else {
    532 		ukbd_decode(sc, ud);
    533 	}
    534 }
    535 
    536 void
    537 ukbd_delayed_decode(void *addr)
    538 {
    539 	struct ukbd_softc *sc = addr;
    540 
    541 	ukbd_decode(sc, &sc->sc_data);
    542 }
    543 
    544 void
    545 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
    546 {
    547 	int mod, omod;
    548 	u_int16_t ibuf[MAXKEYS];	/* chars events */
    549 	int s;
    550 	int nkeys, i, j;
    551 	int key;
    552 #define ADDKEY(c) ibuf[nkeys++] = (c)
    553 
    554 #ifdef UKBD_DEBUG
    555 	/*
    556 	 * Keep a trace of the last events.  Using printf changes the
    557 	 * timing, so this can be useful sometimes.
    558 	 */
    559 	if (ukbdtrace) {
    560 		struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
    561 		p->unit = sc->sc_dev.dv_unit;
    562 		microtime(&p->tv);
    563 		p->ud = *ud;
    564 		if (++ukbdtraceindex >= UKBDTRACESIZE)
    565 			ukbdtraceindex = 0;
    566 	}
    567 	if (ukbddebug > 5) {
    568 		struct timeval tv;
    569 		microtime(&tv);
    570 		DPRINTF((" at %lu.%06lu  mod=0x%02x key0=0x%02x key1=0x%02x "
    571 			 "key2=0x%02x key3=0x%02x\n",
    572 			 tv.tv_sec, tv.tv_usec,
    573 			 ud->modifiers, ud->keycode[0], ud->keycode[1],
    574 			 ud->keycode[2], ud->keycode[3]));
    575 	}
    576 #endif
    577 
    578 	if (ud->keycode[0] == KEY_ERROR) {
    579 		DPRINTF(("ukbd_intr: KEY_ERROR\n"));
    580 		return;		/* ignore  */
    581 	}
    582 	nkeys = 0;
    583 	mod = ud->modifiers;
    584 	omod = sc->sc_odata.modifiers;
    585 	if (mod != omod)
    586 		for (i = 0; i < NMOD; i++)
    587 			if (( mod & ukbd_mods[i].mask) !=
    588 			    (omod & ukbd_mods[i].mask))
    589 				ADDKEY(ukbd_mods[i].key |
    590 				       (mod & ukbd_mods[i].mask
    591 					  ? PRESS : RELEASE));
    592 	if (memcmp(ud->keycode, sc->sc_odata.keycode, NKEYCODE) != 0) {
    593 		/* Check for released keys. */
    594 		for (i = 0; i < NKEYCODE; i++) {
    595 			key = sc->sc_odata.keycode[i];
    596 			if (key == 0)
    597 				continue;
    598 			for (j = 0; j < NKEYCODE; j++)
    599 				if (key == ud->keycode[j])
    600 					goto rfound;
    601 			DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
    602 			ADDKEY(key | RELEASE);
    603 		rfound:
    604 			;
    605 		}
    606 
    607 		/* Check for pressed keys. */
    608 		for (i = 0; i < NKEYCODE; i++) {
    609 			key = ud->keycode[i];
    610 			if (key == 0)
    611 				continue;
    612 			for (j = 0; j < NKEYCODE; j++)
    613 				if (key == sc->sc_odata.keycode[j])
    614 					goto pfound;
    615 			DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
    616 			ADDKEY(key | PRESS);
    617 		pfound:
    618 			;
    619 		}
    620 	}
    621 	sc->sc_odata = *ud;
    622 
    623 	if (nkeys == 0)
    624 		return;
    625 
    626 	if (sc->sc_polling) {
    627 		DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
    628 		memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
    629 		sc->sc_npollchar = nkeys;
    630 		return;
    631 	}
    632 #ifdef WSDISPLAY_COMPAT_RAWKBD
    633 	if (sc->sc_rawkbd) {
    634 		char cbuf[MAXKEYS * 2];
    635 		int c;
    636 		int npress;
    637 
    638 		for (npress = i = j = 0; i < nkeys; i++) {
    639 			key = ibuf[i];
    640 			c = ukbd_trtab[key & CODEMASK];
    641 			if (c == NN)
    642 				continue;
    643 			if (c & 0x80)
    644 				cbuf[j++] = 0xe0;
    645 			cbuf[j] = c & 0x7f;
    646 			if (key & RELEASE)
    647 				cbuf[j] |= 0x80;
    648 			else {
    649 				/* remember pressed keys for autorepeat */
    650 				if (c & 0x80)
    651 					sc->sc_rep[npress++] = 0xe0;
    652 				sc->sc_rep[npress++] = c & 0x7f;
    653 			}
    654 			DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
    655 				    c & 0x80 ? "0xe0 " : "",
    656 				    cbuf[j]));
    657 			j++;
    658 		}
    659 		s = spltty();
    660 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    661 		splx(s);
    662 		callout_stop(&sc->sc_rawrepeat_ch);
    663 		if (npress != 0) {
    664 			sc->sc_nrep = npress;
    665 			callout_reset(&sc->sc_rawrepeat_ch,
    666 			    hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
    667 		}
    668 		return;
    669 	}
    670 #endif
    671 
    672 	s = spltty();
    673 	for (i = 0; i < nkeys; i++) {
    674 		key = ibuf[i];
    675 		wskbd_input(sc->sc_wskbddev,
    676 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
    677 		    key&CODEMASK);
    678 	}
    679 	splx(s);
    680 }
    681 
    682 void
    683 ukbd_set_leds(void *v, int leds)
    684 {
    685 	struct ukbd_softc *sc = v;
    686 	u_int8_t res;
    687 
    688 	DPRINTF(("ukbd_set_leds: sc=%p leds=%d\n", sc, leds));
    689 
    690 	if (sc->sc_dying)
    691 		return;
    692 
    693 	sc->sc_leds = leds;
    694 	res = 0;
    695 	if (leds & WSKBD_LED_SCROLL)
    696 		res |= SCROLL_LOCK;
    697 	if (leds & WSKBD_LED_NUM)
    698 		res |= NUM_LOCK;
    699 	if (leds & WSKBD_LED_CAPS)
    700 		res |= CAPS_LOCK;
    701 	res |= leds & 0xf8;
    702 	usbd_set_report_async(sc->sc_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
    703 }
    704 
    705 #ifdef WSDISPLAY_COMPAT_RAWKBD
    706 void
    707 ukbd_rawrepeat(void *v)
    708 {
    709 	struct ukbd_softc *sc = v;
    710 	int s;
    711 
    712 	s = spltty();
    713 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
    714 	splx(s);
    715 	callout_reset(&sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
    716 	    ukbd_rawrepeat, sc);
    717 }
    718 #endif
    719 
    720 int
    721 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    722 {
    723 	struct ukbd_softc *sc = v;
    724 
    725 	switch (cmd) {
    726 	case WSKBDIO_GTYPE:
    727 		*(int *)data = WSKBD_TYPE_USB;
    728 		return (0);
    729 	case WSKBDIO_SETLEDS:
    730 		ukbd_set_leds(v, *(int *)data);
    731 		return (0);
    732 	case WSKBDIO_GETLEDS:
    733 		*(int *)data = sc->sc_leds;
    734 		return (0);
    735 #ifdef WSDISPLAY_COMPAT_RAWKBD
    736 	case WSKBDIO_SETMODE:
    737 		DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
    738 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    739 		callout_stop(&sc->sc_rawrepeat_ch);
    740 		return (0);
    741 #endif
    742 	}
    743 	return (-1);
    744 }
    745 
    746 /* Console interface. */
    747 void
    748 ukbd_cngetc(void *v, u_int *type, int *data)
    749 {
    750 	struct ukbd_softc *sc = v;
    751 	int s;
    752 	int c;
    753 
    754 	DPRINTFN(0,("ukbd_cngetc: enter\n"));
    755 	s = splusb();
    756 	sc->sc_polling = 1;
    757 	while(sc->sc_npollchar <= 0)
    758 		usbd_dopoll(sc->sc_iface);
    759 	sc->sc_polling = 0;
    760 	c = sc->sc_pollchars[0];
    761 	sc->sc_npollchar--;
    762 	memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
    763 	       sc->sc_npollchar * sizeof(u_int16_t));
    764 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    765 	*data = c & CODEMASK;
    766 	splx(s);
    767 	DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
    768 }
    769 
    770 void
    771 ukbd_cnpollc(void *v, int on)
    772 {
    773 	struct ukbd_softc *sc = v;
    774 	usbd_device_handle dev;
    775 
    776 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
    777 
    778 	(void)usbd_interface2device_handle(sc->sc_iface,&dev);
    779 	usbd_set_polling(dev, on);
    780 }
    781 
    782 int
    783 ukbd_cnattach(void)
    784 {
    785 
    786 	/*
    787 	 * XXX USB requires too many parts of the kernel to be running
    788 	 * XXX in order to work, so we can't do much for the console
    789 	 * XXX keyboard until autconfiguration has run its course.
    790 	 */
    791 	ukbd_is_console = 1;
    792 	return (0);
    793 }
    794