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