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