Home | History | Annotate | Line # | Download | only in usb
ukbd.c revision 1.55
      1 /*      $NetBSD: ukbd.c,v 1.55 2000/02/02 13:18:46 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 (augustss (at) carlstedt.se) 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/usbhid10.pdf
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/device.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/tty.h>
     50 #include <sys/file.h>
     51 #include <sys/select.h>
     52 #include <sys/proc.h>
     53 #include <sys/vnode.h>
     54 #include <sys/poll.h>
     55 
     56 #include <dev/usb/usb.h>
     57 #include <dev/usb/usbhid.h>
     58 
     59 #include <dev/usb/usbdi.h>
     60 #include <dev/usb/usbdi_util.h>
     61 #include <dev/usb/usbdevs.h>
     62 #include <dev/usb/usb_quirks.h>
     63 #include <dev/usb/hid.h>
     64 #include <dev/usb/ukbdvar.h>
     65 
     66 #include <dev/wscons/wsconsio.h>
     67 #include <dev/wscons/wskbdvar.h>
     68 #include <dev/wscons/wsksymdef.h>
     69 #include <dev/wscons/wsksymvar.h>
     70 
     71 #include "opt_wsdisplay_compat.h"
     72 
     73 #ifdef USB_DEBUG
     74 #define DPRINTF(x)	if (ukbddebug) logprintf x
     75 #define DPRINTFN(n,x)	if (ukbddebug>(n)) logprintf x
     76 int	ukbddebug = 0;
     77 #else
     78 #define DPRINTF(x)
     79 #define DPRINTFN(n,x)
     80 #endif
     81 
     82 #define UPROTO_BOOT_KEYBOARD 1
     83 
     84 #define NKEYCODE 6
     85 
     86 #define NUM_LOCK 0x01
     87 #define CAPS_LOCK 0x02
     88 #define SCROLL_LOCK 0x04
     89 
     90 struct ukbd_data {
     91 	u_int8_t	modifiers;
     92 #define MOD_CONTROL_L	0x01
     93 #define MOD_CONTROL_R	0x10
     94 #define MOD_SHIFT_L	0x02
     95 #define MOD_SHIFT_R	0x20
     96 #define MOD_ALT_L	0x04
     97 #define MOD_ALT_R	0x40
     98 #define MOD_WIN_L	0x08
     99 #define MOD_WIN_R	0x80
    100 	u_int8_t	reserved;
    101 	u_int8_t	keycode[NKEYCODE];
    102 };
    103 
    104 #define PRESS    0x000
    105 #define RELEASE  0x100
    106 #define CODEMASK 0x0ff
    107 
    108 /* Translate USB bitmap to USB keycode. */
    109 #define NMOD 8
    110 static struct {
    111 	int mask, key;
    112 } ukbd_mods[NMOD] = {
    113 	{ MOD_CONTROL_L, 224 },
    114 	{ MOD_CONTROL_R, 228 },
    115 	{ MOD_SHIFT_L,   225 },
    116 	{ MOD_SHIFT_R,   229 },
    117 	{ MOD_ALT_L,     226 },
    118 	{ MOD_ALT_R,     230 },
    119 	{ MOD_WIN_L,     227 },
    120 	{ MOD_WIN_R,     231 },
    121 };
    122 
    123 #if defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD)
    124 #define NN 0			/* no translation */
    125 /*
    126  * Translate USB keycodes to US keyboard XT scancodes.
    127  * Scancodes >= 128 represent EXTENDED keycodes.
    128  */
    129 static u_int8_t ukbd_trtab[256] = {
    130 	  NN,  NN,  NN,  NN,  30,  48,  46,  32, /* 00 - 07 */
    131 	  18,  33,  34,  35,  23,  36,  37,  38, /* 08 - 0F */
    132 	  50,  49,  24,  25,  16,  19,  31,  20, /* 10 - 17 */
    133 	  22,  47,  17,  45,  21,  44,   2,   3, /* 18 - 1F */
    134 	   4,   5,   6,   7,   8,   9,  10,  11, /* 20 - 27 */
    135 	  28,   1,  14,  15,  57,  12,  13,  26, /* 28 - 2F */
    136 	  27,  43,  NN,  39,  40,  41,  51,  52, /* 30 - 37 */
    137 	  53,  58,  59,  60,  61,  62,  63,  64, /* 38 - 3F */
    138 	  65,  66,  67,  68,  87,  88, 170,  70, /* 40 - 47 */
    139 	 127, 210, 199, 201, 211, 207, 209, 205, /* 48 - 4F */
    140 	 203, 208, 200,  69, 181,  55,  74,  78, /* 50 - 57 */
    141 	 156,  79,  80,  81,  75,  76,  77,  71, /* 58 - 5F */
    142           72,  73,  82,  83,  NN, 221,  NN,  NN, /* 60 - 67 */
    143           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 68 - 6F */
    144           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 70 - 77 */
    145           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 78 - 7F */
    146           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 80 - 87 */
    147           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 88 - 8F */
    148           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 90 - 97 */
    149           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 98 - 9F */
    150           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* A0 - A7 */
    151           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* A8 - AF */
    152           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* B0 - B7 */
    153           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* B8 - BF */
    154           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* C0 - C7 */
    155           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* C8 - CF */
    156           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* D0 - D7 */
    157           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* D8 - DF */
    158           29,  42,  56, 219,  157, 54,  184,220, /* E0 - E7 */
    159           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* E8 - EF */
    160           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* F0 - F7 */
    161           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* F8 - FF */
    162 };
    163 #endif /* defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD) */
    164 
    165 #define KEY_ERROR 0x01
    166 
    167 #define MAXKEYS (NMOD+2*NKEYCODE)
    168 
    169 struct ukbd_softc {
    170 	USBBASEDEVICE	sc_dev;		/* base device */
    171 	usbd_device_handle sc_udev;
    172 	usbd_interface_handle sc_iface;	/* interface */
    173 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
    174 	int sc_ep_addr;
    175 
    176 	struct ukbd_data sc_ndata;
    177 	struct ukbd_data sc_odata;
    178 
    179 	char sc_enabled;
    180 
    181 	int sc_console_keyboard;	/* we are the console keyboard */
    182 
    183 	int sc_leds;
    184 #if defined(__NetBSD__)
    185 	struct device *sc_wskbddev;
    186 #if defined(WSDISPLAY_COMPAT_RAWKBD)
    187 #define REP_DELAY1 400
    188 #define REP_DELAYN 100
    189 	int sc_rawkbd;
    190 	int sc_nrep;
    191 	char sc_rep[MAXKEYS];
    192 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
    193 
    194 	int sc_polling;
    195 	int sc_npollchar;
    196 	u_int16_t sc_pollchars[MAXKEYS];
    197 #endif /* defined(__NetBSD__) */
    198 
    199 	u_char sc_dying;
    200 };
    201 
    202 #define	UKBDUNIT(dev)	(minor(dev))
    203 #define	UKBD_CHUNK	128	/* chunk size for read */
    204 #define	UKBD_BSIZE	1020	/* buffer size */
    205 
    206 static int	ukbd_is_console;
    207 
    208 static void	ukbd_cngetc __P((void *, u_int *, int *));
    209 static void	ukbd_cnpollc __P((void *, int));
    210 
    211 #if defined(__NetBSD__)
    212 const struct wskbd_consops ukbd_consops = {
    213 	ukbd_cngetc,
    214 	ukbd_cnpollc,
    215 };
    216 #endif
    217 
    218 static void	ukbd_intr __P((usbd_xfer_handle, usbd_private_handle,
    219 			       usbd_status));
    220 
    221 static int	ukbd_enable __P((void *, int));
    222 static void	ukbd_set_leds __P((void *, int));
    223 
    224 #if defined(__NetBSD__)
    225 static int	ukbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    226 #ifdef WSDISPLAY_COMPAT_RAWKBD
    227 static void	ukbd_rawrepeat __P((void *v));
    228 #endif
    229 
    230 const struct wskbd_accessops ukbd_accessops = {
    231 	ukbd_enable,
    232 	ukbd_set_leds,
    233 	ukbd_ioctl,
    234 };
    235 
    236 extern const struct wscons_keydesc ukbd_keydesctab[];
    237 
    238 const struct wskbd_mapdata ukbd_keymapdata = {
    239 	ukbd_keydesctab,
    240 	KB_US,
    241 };
    242 #endif
    243 
    244 USB_DECLARE_DRIVER(ukbd);
    245 
    246 USB_MATCH(ukbd)
    247 {
    248 	USB_MATCH_START(ukbd, uaa);
    249 	usb_interface_descriptor_t *id;
    250 
    251 	/* Check that this is a keyboard that speaks the boot protocol. */
    252 	if (uaa->iface == NULL)
    253 		return (UMATCH_NONE);
    254 	id = usbd_get_interface_descriptor(uaa->iface);
    255 	if (id == NULL ||
    256 	    id->bInterfaceClass != UCLASS_HID ||
    257 	    id->bInterfaceSubClass != USUBCLASS_BOOT ||
    258 	    id->bInterfaceProtocol != UPROTO_BOOT_KEYBOARD)
    259 		return (UMATCH_NONE);
    260 	return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    261 }
    262 
    263 USB_ATTACH(ukbd)
    264 {
    265 	USB_ATTACH_START(ukbd, sc, uaa);
    266 	usbd_interface_handle iface = uaa->iface;
    267 	usb_interface_descriptor_t *id;
    268 	usb_endpoint_descriptor_t *ed;
    269 	usbd_status err;
    270 	char devinfo[1024];
    271 #if defined(__NetBSD__)
    272 	struct wskbddev_attach_args a;
    273 #else
    274 	int i;
    275 #endif
    276 
    277 	sc->sc_udev = uaa->device;
    278 	sc->sc_iface = iface;
    279 	id = usbd_get_interface_descriptor(iface);
    280 	usbd_devinfo(uaa->device, 0, devinfo);
    281 	USB_ATTACH_SETUP;
    282 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    283 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    284 
    285 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    286 	if (ed == NULL) {
    287 		printf("%s: could not read endpoint descriptor\n",
    288 		       USBDEVNAME(sc->sc_dev));
    289 		USB_ATTACH_ERROR_RETURN;
    290 	}
    291 
    292 	DPRINTFN(10,("ukbd_attach: bLength=%d bDescriptorType=%d "
    293 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    294 		     " bInterval=%d\n",
    295 		     ed->bLength, ed->bDescriptorType,
    296 		     ed->bEndpointAddress & UE_ADDR,
    297 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    298 		     ed->bmAttributes & UE_XFERTYPE,
    299 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    300 
    301 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    302 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    303 		printf("%s: unexpected endpoint\n",
    304 		       USBDEVNAME(sc->sc_dev));
    305 		USB_ATTACH_ERROR_RETURN;
    306 	}
    307 
    308 	if ((usbd_get_quirks(uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
    309 		err = usbd_set_protocol(iface, 0);
    310 		DPRINTFN(5, ("ukbd_attach: protocol set\n"));
    311 		if (err) {
    312 			printf("%s: set protocol failed\n",
    313 			    USBDEVNAME(sc->sc_dev));
    314 			USB_ATTACH_ERROR_RETURN;
    315 		}
    316 	}
    317 
    318 	/* Ignore if SETIDLE fails since it is not crucial. */
    319 	usbd_set_idle(iface, 0, 0);
    320 
    321 	sc->sc_ep_addr = ed->bEndpointAddress;
    322 
    323 	/*
    324 	 * Remember if we're the console keyboard.
    325 	 *
    326 	 * XXX This always picks the first keyboard on the
    327 	 * first USB bus, but what else can we really do?
    328 	 */
    329 	if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
    330 		/* Don't let any other keyboard have it. */
    331 		ukbd_is_console = 0;
    332 	}
    333 
    334 	if (sc->sc_console_keyboard) {
    335 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
    336 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
    337 		ukbd_enable(sc, 1);
    338 	}
    339 
    340 	a.console = sc->sc_console_keyboard;
    341 
    342 	a.keymap = &ukbd_keymapdata;
    343 
    344 	a.accessops = &ukbd_accessops;
    345 	a.accesscookie = sc;
    346 
    347 	/* Flash the leds; no real purpose, just shows we're alive. */
    348 	ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
    349 	usbd_delay_ms(uaa->device, 400);
    350 	ukbd_set_leds(sc, 0);
    351 
    352 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    353 
    354 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    355 			   USBDEV(sc->sc_dev));
    356 
    357 	USB_ATTACH_SUCCESS_RETURN;
    358 }
    359 
    360 int
    361 ukbd_enable(v, on)
    362 	void *v;
    363 	int on;
    364 {
    365 	struct ukbd_softc *sc = v;
    366 	usbd_status err;
    367 
    368 	if (on && sc->sc_dying)
    369 		return (EIO);
    370 
    371 	/* Should only be called to change state */
    372 	if (sc->sc_enabled == on) {
    373 #ifdef DIAGNOSTIC
    374 		printf("ukbd_enable: %s: bad call on=%d\n",
    375 		       USBDEVNAME(sc->sc_dev), on);
    376 #endif
    377 		return (EBUSY);
    378 	}
    379 
    380 	DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
    381 	if (on) {
    382 		/* Set up interrupt pipe. */
    383 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    384 			  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
    385 			  &sc->sc_ndata, sizeof(sc->sc_ndata), ukbd_intr,
    386 			  USBD_DEFAULT_INTERVAL);
    387 		if (err)
    388 			return (EIO);
    389 	} else {
    390 		/* Disable interrupts. */
    391 		usbd_abort_pipe(sc->sc_intrpipe);
    392 		usbd_close_pipe(sc->sc_intrpipe);
    393 	}
    394 	sc->sc_enabled = on;
    395 
    396 	return (0);
    397 }
    398 
    399 int
    400 ukbd_activate(self, act)
    401 	device_ptr_t self;
    402 	enum devact act;
    403 {
    404 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
    405 	int rv = 0;
    406 
    407 	switch (act) {
    408 	case DVACT_ACTIVATE:
    409 		return (EOPNOTSUPP);
    410 		break;
    411 
    412 	case DVACT_DEACTIVATE:
    413 		if (sc->sc_wskbddev != NULL)
    414 			rv = config_deactivate(sc->sc_wskbddev);
    415 		sc->sc_dying = 1;
    416 		break;
    417 	}
    418 	return (rv);
    419 }
    420 
    421 USB_DETACH(ukbd)
    422 {
    423 	USB_DETACH_START(ukbd, sc);
    424 	int rv = 0;
    425 
    426 	DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
    427 
    428 	if (sc->sc_console_keyboard) {
    429 #if 0
    430 		/*
    431 		 * XXX Should probably disconnect our consops,
    432 		 * XXX and either notify some other keyboard that
    433 		 * XXX it can now be the console, or if there aren't
    434 		 * XXX any more USB keyboards, set ukbd_is_console
    435 		 * XXX back to 1 so that the next USB keyboard attached
    436 		 * XXX to the system will get it.
    437 		 */
    438 		panic("ukbd_detach: console keyboard");
    439 #else
    440 		/*
    441 		 * Disconnect our consops and set ukbd_is_console
    442 		 * back to 1 so that the next USB keyboard attached
    443 		 * to the system will get it.
    444 		 * XXX Should notify some other keyboard that it can be
    445 		 * XXX console, if there are any other keyboards.
    446 		 */
    447 		printf("%s: was console keyboard\n", USBDEVNAME(sc->sc_dev));
    448 		wskbd_cndetach();
    449 		ukbd_is_console = 1;
    450 #endif
    451 	}
    452 	/* No need to do reference counting of ukbd, wskbd has all the goo. */
    453 	if (sc->sc_wskbddev != NULL)
    454 		rv = config_detach(sc->sc_wskbddev, flags);
    455 
    456 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    457 			   USBDEV(sc->sc_dev));
    458 
    459 	return (rv);
    460 }
    461 
    462 void
    463 ukbd_intr(xfer, addr, status)
    464 	usbd_xfer_handle xfer;
    465 	usbd_private_handle addr;
    466 	usbd_status status;
    467 {
    468 	struct ukbd_softc *sc = addr;
    469 	struct ukbd_data *ud = &sc->sc_ndata;
    470 	int mod, omod;
    471 	u_int16_t ibuf[MAXKEYS];	/* chars events */
    472 	int s;
    473 	int nkeys, i, j;
    474 	int key;
    475 #define ADDKEY(c) ibuf[nkeys++] = (c)
    476 
    477 	DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
    478 	if (status == USBD_CANCELLED)
    479 		return;
    480 
    481 	if (status) {
    482 		DPRINTF(("ukbd_intr: status=%d\n", status));
    483 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    484 		return;
    485 	}
    486 
    487 	DPRINTFN(5, ("          mod=0x%02x key0=0x%02x key1=0x%02x\n",
    488 		     ud->modifiers, ud->keycode[0], ud->keycode[1]));
    489 
    490 	if (ud->keycode[0] == KEY_ERROR) {
    491 		DPRINTF(("ukbd_intr: KEY_ERROR\n"));
    492 		return;		/* ignore  */
    493 	}
    494 	nkeys = 0;
    495 	mod = ud->modifiers;
    496 	omod = sc->sc_odata.modifiers;
    497 	if (mod != omod)
    498 		for (i = 0; i < NMOD; i++)
    499 			if (( mod & ukbd_mods[i].mask) !=
    500 			    (omod & ukbd_mods[i].mask))
    501 				ADDKEY(ukbd_mods[i].key |
    502 				       (mod & ukbd_mods[i].mask
    503 					  ? PRESS : RELEASE));
    504 	if (memcmp(ud->keycode, sc->sc_odata.keycode, NKEYCODE) != 0) {
    505 		/* Check for released keys. */
    506 		for (i = 0; i < NKEYCODE; i++) {
    507 			key = sc->sc_odata.keycode[i];
    508 			if (key == 0)
    509 				continue;
    510 			for (j = 0; j < NKEYCODE; j++)
    511 				if (key == ud->keycode[j])
    512 					goto rfound;
    513 			ADDKEY(key | RELEASE);
    514 		rfound:
    515 			;
    516 		}
    517 
    518 		/* Check for pressed keys. */
    519 		for (i = 0; i < NKEYCODE; i++) {
    520 			key = ud->keycode[i];
    521 			if (key == 0)
    522 				continue;
    523 			for (j = 0; j < NKEYCODE; j++)
    524 				if (key == sc->sc_odata.keycode[j])
    525 					goto pfound;
    526 			DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
    527 			ADDKEY(key | PRESS);
    528 		pfound:
    529 			;
    530 		}
    531 	}
    532 	sc->sc_odata = *ud;
    533 
    534 	if (nkeys == 0)
    535 		return;
    536 
    537 	if (sc->sc_polling) {
    538 		DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
    539 		memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
    540 		sc->sc_npollchar = nkeys;
    541 		return;
    542 	}
    543 #ifdef WSDISPLAY_COMPAT_RAWKBD
    544 	if (sc->sc_rawkbd) {
    545 		char cbuf[MAXKEYS * 2];
    546 		int c;
    547 		int npress;
    548 
    549 		for (npress = i = j = 0; i < nkeys; i++) {
    550 			key = ibuf[i];
    551 			c = ukbd_trtab[key & CODEMASK];
    552 			if (c == NN)
    553 				continue;
    554 			if (c & 0x80)
    555 				cbuf[j++] = 0xe0;
    556 			cbuf[j] = c & 0x7f;
    557 			if (key & RELEASE)
    558 				cbuf[j] |= 0x80;
    559 			else {
    560 				/* remember pressed keys for autorepeat */
    561 				if (c & 0x80)
    562 					sc->sc_rep[npress++] = 0xe0;
    563 				sc->sc_rep[npress++] = c & 0x7f;
    564 			}
    565 			DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
    566 				    c & 0x80 ? "0xe0 " : "",
    567 				    cbuf[j]));
    568 			j++;
    569 		}
    570 		s = spltty();
    571 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    572 		splx(s);
    573 		untimeout(ukbd_rawrepeat, sc);
    574 		if (npress != 0) {
    575 			sc->sc_nrep = npress;
    576 			timeout(ukbd_rawrepeat, sc, hz * REP_DELAY1 / 1000);
    577 		}
    578 		return;
    579 	}
    580 #endif
    581 
    582 	s = spltty();
    583 	for (i = 0; i < nkeys; i++) {
    584 		key = ibuf[i];
    585 		wskbd_input(sc->sc_wskbddev,
    586 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
    587 		    key&CODEMASK);
    588 	}
    589 	splx(s);
    590 }
    591 
    592 void
    593 ukbd_set_leds(v, leds)
    594 	void *v;
    595 	int leds;
    596 {
    597 	struct ukbd_softc *sc = v;
    598 	u_int8_t res;
    599 
    600 	DPRINTF(("ukbd_set_leds: sc=%p leds=%d\n", sc, leds));
    601 
    602 	if (sc->sc_dying)
    603 		return;
    604 
    605 	sc->sc_leds = leds;
    606 	res = 0;
    607 	if (leds & WSKBD_LED_SCROLL)
    608 		res |= SCROLL_LOCK;
    609 	if (leds & WSKBD_LED_NUM)
    610 		res |= NUM_LOCK;
    611 	if (leds & WSKBD_LED_CAPS)
    612 		res |= CAPS_LOCK;
    613 	res |= leds & 0xf8;
    614 	usbd_set_report_async(sc->sc_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
    615 }
    616 
    617 #ifdef WSDISPLAY_COMPAT_RAWKBD
    618 void
    619 ukbd_rawrepeat(v)
    620 	void *v;
    621 {
    622 	struct ukbd_softc *sc = v;
    623 	int s;
    624 
    625 	s = spltty();
    626 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
    627 	splx(s);
    628 	timeout(ukbd_rawrepeat, sc, hz * REP_DELAYN / 1000);
    629 }
    630 #endif
    631 
    632 int
    633 ukbd_ioctl(v, cmd, data, flag, p)
    634 	void *v;
    635 	u_long cmd;
    636 	caddr_t data;
    637 	int flag;
    638 	struct proc *p;
    639 {
    640 	struct ukbd_softc *sc = v;
    641 
    642 	switch (cmd) {
    643 	case WSKBDIO_GTYPE:
    644 		*(int *)data = WSKBD_TYPE_USB;
    645 		return (0);
    646 	case WSKBDIO_SETLEDS:
    647 		ukbd_set_leds(v, *(int *)data);
    648 		return (0);
    649 	case WSKBDIO_GETLEDS:
    650 		*(int *)data = sc->sc_leds;
    651 		return (0);
    652 #ifdef WSDISPLAY_COMPAT_RAWKBD
    653 	case WSKBDIO_SETMODE:
    654 		DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
    655 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    656 		untimeout(ukbd_rawrepeat, sc);
    657 		return (0);
    658 #endif
    659 	}
    660 	return (-1);
    661 }
    662 
    663 /* Console interface. */
    664 void
    665 ukbd_cngetc(v, type, data)
    666 	void *v;
    667 	u_int *type;
    668 	int *data;
    669 {
    670 	struct ukbd_softc *sc = v;
    671 	int s;
    672 	int c;
    673 
    674 	DPRINTFN(0,("ukbd_cngetc: enter\n"));
    675 	s = splusb();
    676 	sc->sc_polling = 1;
    677 	while(sc->sc_npollchar <= 0)
    678 		usbd_dopoll(sc->sc_iface);
    679 	sc->sc_polling = 0;
    680 	c = sc->sc_pollchars[0];
    681 	sc->sc_npollchar--;
    682 	memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
    683 	       sc->sc_npollchar * sizeof(u_int16_t));
    684 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    685 	*data = c & CODEMASK;
    686 	splx(s);
    687 	DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
    688 }
    689 
    690 void
    691 ukbd_cnpollc(v, on)
    692 	void *v;
    693         int on;
    694 {
    695 	struct ukbd_softc *sc = v;
    696 	usbd_device_handle dev;
    697 
    698 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
    699 
    700 	(void)usbd_interface2device_handle(sc->sc_iface,&dev);
    701 	usbd_set_polling(dev, on);
    702 }
    703 
    704 int
    705 ukbd_cnattach()
    706 {
    707 
    708 	/*
    709 	 * XXX USB requires too many parts of the kernel to be running
    710 	 * XXX in order to work, so we can't do much for the console
    711 	 * XXX keyboard until autconfiguration has run its course.
    712 	 */
    713 	ukbd_is_console = 1;
    714 	return (0);
    715 }
    716