Home | History | Annotate | Line # | Download | only in usb
ukbd.c revision 1.32
      1 /*      $NetBSD: ukbd.c,v 1.32 1999/05/14 19:38:44 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 #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 #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 	char sc_disconnected;		/* device is gone */
    193 
    194 	int sc_console_keyboard;	/* we are the console keyboard */
    195 
    196 	int sc_leds;
    197 #if defined(__NetBSD__)
    198 	struct device *sc_wskbddev;
    199 #if defined(WSDISPLAY_COMPAT_RAWKBD)
    200 #define REP_DELAY1 400
    201 #define REP_DELAYN 100
    202 	int sc_rawkbd;
    203 	int sc_nrep;
    204 	char sc_rep[MAXKEYS];
    205 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
    206 
    207 	int sc_polling;
    208 	int sc_npollchar;
    209 	u_int16_t sc_pollchars[MAXKEYS];
    210 #endif /* defined(__NetBSD__) */
    211 };
    212 
    213 #define	UKBDUNIT(dev)	(minor(dev))
    214 #define	UKBD_CHUNK	128	/* chunk size for read */
    215 #define	UKBD_BSIZE	1020	/* buffer size */
    216 
    217 int	ukbd_is_console;
    218 
    219 void	ukbd_cngetc __P((void *, u_int *, int *));
    220 void	ukbd_cnpollc __P((void *, int));
    221 
    222 #if defined(__NetBSD__)
    223 const struct wskbd_consops ukbd_consops = {
    224 	ukbd_cngetc,
    225 	ukbd_cnpollc,
    226 };
    227 #endif
    228 
    229 void	ukbd_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
    230 void	ukbd_disco __P((void *));
    231 
    232 int	ukbd_enable __P((void *, int));
    233 void	ukbd_set_leds __P((void *, int));
    234 
    235 #if defined(__NetBSD__)
    236 int	ukbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    237 void	ukbd_rawrepeat __P((void *v));
    238 
    239 const struct wskbd_accessops ukbd_accessops = {
    240 	ukbd_enable,
    241 	ukbd_set_leds,
    242 	ukbd_ioctl,
    243 };
    244 
    245 extern const struct wscons_keydesc ukbd_keydesctab[];
    246 
    247 const struct wskbd_mapdata ukbd_keymapdata = {
    248 	ukbd_keydesctab,
    249 	KB_US,
    250 };
    251 #endif
    252 
    253 USB_DECLARE_DRIVER(ukbd);
    254 
    255 USB_MATCH(ukbd)
    256 {
    257 	USB_MATCH_START(ukbd, uaa);
    258 	usb_interface_descriptor_t *id;
    259 
    260 	/* Check that this is a keyboard that speaks the boot protocol. */
    261 	if (!uaa->iface)
    262 		return (UMATCH_NONE);
    263 	id = usbd_get_interface_descriptor(uaa->iface);
    264 	if (!id ||
    265 	    id->bInterfaceClass != UCLASS_HID ||
    266 	    id->bInterfaceSubClass != USUBCLASS_BOOT ||
    267 	    id->bInterfaceProtocol != UPROTO_BOOT_KEYBOARD)
    268 		return (UMATCH_NONE);
    269 	return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    270 }
    271 
    272 USB_ATTACH(ukbd)
    273 {
    274 	USB_ATTACH_START(ukbd, sc, uaa);
    275 	usbd_interface_handle iface = uaa->iface;
    276 	usb_interface_descriptor_t *id;
    277 	usb_endpoint_descriptor_t *ed;
    278 	usbd_status r;
    279 	char devinfo[1024];
    280 #if defined(__NetBSD__)
    281 	struct wskbddev_attach_args a;
    282 #else
    283 	int i;
    284 #endif
    285 
    286 	sc->sc_disconnected = 1;
    287 	sc->sc_iface = iface;
    288 	id = usbd_get_interface_descriptor(iface);
    289 	usbd_devinfo(uaa->device, 0, devinfo);
    290 	USB_ATTACH_SETUP;
    291 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    292 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    293 
    294 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    295 	if (!ed) {
    296 		printf("%s: could not read endpoint descriptor\n",
    297 		       USBDEVNAME(sc->sc_dev));
    298 		USB_ATTACH_ERROR_RETURN;
    299 	}
    300 
    301 	DPRINTFN(10,("ukbd_attach: bLength=%d bDescriptorType=%d "
    302 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    303 		     " bInterval=%d\n",
    304 		     ed->bLength, ed->bDescriptorType,
    305 		     ed->bEndpointAddress & UE_ADDR,
    306 		     ed->bEndpointAddress & UE_IN ? "in" : "out",
    307 		     ed->bmAttributes & UE_XFERTYPE,
    308 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    309 
    310 	if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
    311 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    312 		printf("%s: unexpected endpoint\n",
    313 		       USBDEVNAME(sc->sc_dev));
    314 		USB_ATTACH_ERROR_RETURN;
    315 	}
    316 
    317 	if ((usbd_get_quirks(uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
    318 		r = usbd_set_protocol(iface, 0);
    319 		DPRINTFN(5, ("ukbd_attach: protocol set\n"));
    320 		if (r != USBD_NORMAL_COMPLETION) {
    321 			printf("%s: set protocol failed\n",
    322 			       USBDEVNAME(sc->sc_dev));
    323 			USB_ATTACH_ERROR_RETURN;
    324 		}
    325 	}
    326 
    327 	/* Ignore if SETIDLE fails since it is not crucial. */
    328 	usbd_set_idle(iface, 0, 0);
    329 
    330 	sc->sc_ep_addr = ed->bEndpointAddress;
    331 	sc->sc_disconnected = 0;
    332 
    333 	/*
    334 	 * Remember if we're the console keyboard.
    335 	 *
    336 	 * XXX This always picks the first keyboard on the
    337 	 * first USB bus, but what else can we really do?
    338 	 */
    339 	if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
    340 		/* Don't let any other keyboard have it. */
    341 		ukbd_is_console = 0;
    342 	}
    343 
    344 #if defined(__NetBSD__)
    345 	if (sc->sc_console_keyboard) {
    346 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
    347 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
    348 	}
    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 either notify some other keyboard that
    416 		 * XXX it can now be the console, or if there aren't
    417 		 * XXX any more USB keyboards, set ukbd_is_console
    418 		 * XXX back to 1 so that the next USB keyboard attached
    419 		 * XXX to the system will get it.
    420 		 */
    421 		panic("ukbd_disco: console keyboard");
    422 	}
    423 }
    424 
    425 int
    426 ukbd_enable(v, on)
    427 	void *v;
    428 	int on;
    429 {
    430 	struct ukbd_softc *sc = v;
    431 	usbd_status r;
    432 
    433 	if (on) {
    434 		/* Set up interrupt pipe. */
    435 		if (sc->sc_enabled)
    436 			return (EBUSY);
    437 
    438 		sc->sc_enabled = 1;
    439 		r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    440 					USBD_SHORT_XFER_OK,
    441 					&sc->sc_intrpipe, sc, &sc->sc_ndata,
    442 					sizeof(sc->sc_ndata), ukbd_intr);
    443 		if (r != USBD_NORMAL_COMPLETION)
    444 			return (EIO);
    445 		usbd_set_disco(sc->sc_intrpipe, ukbd_disco, sc);
    446 	} else {
    447 		/* Disable interrupts. */
    448 		usbd_abort_pipe(sc->sc_intrpipe);
    449 		usbd_close_pipe(sc->sc_intrpipe);
    450 
    451 		sc->sc_enabled = 0;
    452 	}
    453 
    454 	return (0);
    455 }
    456 
    457 void
    458 ukbd_intr(reqh, addr, status)
    459 	usbd_request_handle reqh;
    460 	usbd_private_handle addr;
    461 	usbd_status status;
    462 {
    463 	struct ukbd_softc *sc = addr;
    464 	struct ukbd_data *ud = &sc->sc_ndata;
    465 	int mod, omod;
    466 	u_int16_t ibuf[MAXKEYS];	/* chars events */
    467 	int s;
    468 	int nkeys, i, j;
    469 	int key;
    470 #define ADDKEY(c) ibuf[nkeys++] = (c)
    471 
    472 	DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
    473 	if (status == USBD_CANCELLED)
    474 		return;
    475 
    476 	if (status != USBD_NORMAL_COMPLETION) {
    477 		DPRINTF(("ukbd_intr: status=%d\n", status));
    478 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    479 		return;
    480 	}
    481 
    482 	DPRINTFN(5, ("          mod=0x%02x key0=0x%02x key1=0x%02x\n",
    483 		     ud->modifiers, ud->keycode[0], ud->keycode[1]));
    484 
    485 	if (ud->keycode[0] == KEY_ERROR)
    486 		return;		/* ignore  */
    487 	nkeys = 0;
    488 	mod = ud->modifiers;
    489 	omod = sc->sc_odata.modifiers;
    490 	if (mod != omod)
    491 		for (i = 0; i < NMOD; i++)
    492 			if (( mod & ukbd_mods[i].mask) !=
    493 			    (omod & ukbd_mods[i].mask))
    494 				ADDKEY(ukbd_mods[i].key |
    495 				       (mod & ukbd_mods[i].mask
    496 					  ? PRESS : RELEASE));
    497 	if (memcmp(ud->keycode, sc->sc_odata.keycode, NKEYCODE) != 0) {
    498 		/* Check for released keys. */
    499 		for (i = 0; i < NKEYCODE; i++) {
    500 			key = sc->sc_odata.keycode[i];
    501 			if (key == 0)
    502 				continue;
    503 			for (j = 0; j < NKEYCODE; j++)
    504 				if (key == ud->keycode[j])
    505 					goto rfound;
    506 			ADDKEY(key | RELEASE);
    507 		rfound:
    508 			;
    509 		}
    510 
    511 		/* Check for pressed keys. */
    512 		for (i = 0; i < NKEYCODE; i++) {
    513 			key = ud->keycode[i];
    514 			if (key == 0)
    515 				continue;
    516 			for (j = 0; j < NKEYCODE; j++)
    517 				if (key == sc->sc_odata.keycode[j])
    518 					goto pfound;
    519 			DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
    520 			ADDKEY(key | PRESS);
    521 		pfound:
    522 			;
    523 		}
    524 	}
    525 	sc->sc_odata = *ud;
    526 
    527 	if (nkeys == 0)
    528 		return;
    529 
    530 #if defined(__NetBSD__)
    531 	if (sc->sc_polling) {
    532 		DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
    533 		memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
    534 		sc->sc_npollchar = nkeys;
    535 		return;
    536 	}
    537 #ifdef WSDISPLAY_COMPAT_RAWKBD
    538 	if (sc->sc_rawkbd) {
    539 		char cbuf[MAXKEYS * 2];
    540 		int c;
    541 		int npress;
    542 
    543 		for (npress = i = j = 0; i < nkeys; i++) {
    544 			key = ibuf[i];
    545 			c = ukbd_trtab[key & CODEMASK];
    546 			if (c == NN)
    547 				continue;
    548 			if (c & 0x80)
    549 				cbuf[j++] = 0xe0;
    550 			cbuf[j] = c & 0x7f;
    551 			if (key & RELEASE)
    552 				cbuf[j] |= 0x80;
    553 			else {
    554 				/* remember pressed keys for autorepeat */
    555 				if (c & 0x80)
    556 					sc->sc_rep[npress++] = 0xe0;
    557 				sc->sc_rep[npress++] = c & 0x7f;
    558 			}
    559 			DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
    560 				    c & 0x80 ? "0xe0 " : "",
    561 				    cbuf[j]));
    562 			j++;
    563 		}
    564 		s = spltty();
    565 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    566 		splx(s);
    567 		untimeout(ukbd_rawrepeat, sc);
    568 		if (npress != 0) {
    569 			sc->sc_nrep = npress;
    570 			timeout(ukbd_rawrepeat, sc, hz * REP_DELAY1 / 1000);
    571 		}
    572 		return;
    573 	}
    574 #endif
    575 
    576 	s = spltty();
    577 	for (i = 0; i < nkeys; i++) {
    578 		key = ibuf[i];
    579 		wskbd_input(sc->sc_wskbddev,
    580 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
    581 		    key&CODEMASK);
    582 	}
    583 	splx(s);
    584 
    585 #elif defined(__FreeBSD__)
    586 	/* XXX shouldn't the keys be used? */
    587 	for (i = 0; i < nkeys; i++) {
    588 		c = ibuf[i];
    589 		printf("%c (%d) %s\n",
    590 		       ((c&CODEMASK) < 32 || (c&CODEMASK) > 126 ? '.' :
    591 			(c&CODEMASK)), c,
    592 		       (c&RELEASE? "released":"pressed"));
    593 		if (ud->modifiers)
    594 			printf("0x%04x\n", ud->modifiers);
    595                 for (i = 0; i < NKEYCODE; i++)
    596 			if (ud->keycode[i])
    597 				printf("%d ", ud->keycode[i]);
    598 		printf("\n");
    599 	}
    600 #endif
    601 }
    602 
    603 void
    604 ukbd_set_leds(v, leds)
    605 	void *v;
    606 	int leds;
    607 {
    608 	struct ukbd_softc *sc = v;
    609 	u_int8_t res;
    610 
    611 	DPRINTF(("ukbd_set_leds: sc=%p leds=%d\n", sc, leds));
    612 
    613 	sc->sc_leds = leds;
    614 #if defined(__NetBSD__)
    615 	res = 0;
    616 	if (leds & WSKBD_LED_SCROLL)
    617 		res |= SCROLL_LOCK;
    618 	if (leds & WSKBD_LED_NUM)
    619 		res |= NUM_LOCK;
    620 	if (leds & WSKBD_LED_CAPS)
    621 		res |= CAPS_LOCK;
    622 #elif defined(__FreeBSD__)
    623 	res = leds;
    624 #endif
    625 	usbd_set_report_async(sc->sc_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
    626 }
    627 
    628 #if defined(__NetBSD__)
    629 
    630 #ifdef WSDISPLAY_COMPAT_RAWKBD
    631 void
    632 ukbd_rawrepeat(v)
    633 	void *v;
    634 {
    635 	struct ukbd_softc *sc = v;
    636 	int s;
    637 
    638 	s = spltty();
    639 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
    640 	splx(s);
    641 	timeout(ukbd_rawrepeat, sc, hz * REP_DELAYN / 1000);
    642 }
    643 #endif
    644 
    645 int
    646 ukbd_ioctl(v, cmd, data, flag, p)
    647 	void *v;
    648 	u_long cmd;
    649 	caddr_t data;
    650 	int flag;
    651 	struct proc *p;
    652 {
    653 	struct ukbd_softc *sc = v;
    654 
    655 	switch (cmd) {
    656 	case WSKBDIO_GTYPE:
    657 		*(int *)data = WSKBD_TYPE_USB;
    658 		return (0);
    659 	case WSKBDIO_SETLEDS:
    660 		ukbd_set_leds(v, *(int *)data);
    661 		return (0);
    662 	case WSKBDIO_GETLEDS:
    663 		*(int *)data = sc->sc_leds;
    664 		return (0);
    665 #ifdef WSDISPLAY_COMPAT_RAWKBD
    666 	case WSKBDIO_SETMODE:
    667 		DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
    668 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    669 		untimeout(ukbd_rawrepeat, sc);
    670 		return (0);
    671 #endif
    672 	}
    673 	return (-1);
    674 }
    675 
    676 /* Console interface. */
    677 void
    678 ukbd_cngetc(v, type, data)
    679 	void *v;
    680 	u_int *type;
    681 	int *data;
    682 {
    683 	struct ukbd_softc *sc = v;
    684 	usbd_lock_token s;
    685 	int c;
    686 
    687 	DPRINTFN(-1,("ukbd_cngetc: enter\n"));
    688 	s = usbd_lock();
    689 	sc->sc_polling = 1;
    690 	while(sc->sc_npollchar <= 0)
    691 		usbd_dopoll(sc->sc_iface);
    692 	sc->sc_polling = 0;
    693 	c = sc->sc_pollchars[0];
    694 	sc->sc_npollchar--;
    695 	memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
    696 	       sc->sc_npollchar * sizeof(u_int16_t));
    697 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    698 	*data = c & CODEMASK;
    699 	usbd_unlock(s);
    700 	DPRINTFN(-1,("ukbd_cngetc: return 0x%02x\n", c));
    701 }
    702 
    703 void
    704 ukbd_cnpollc(v, on)
    705 	void *v;
    706         int on;
    707 {
    708 	struct ukbd_softc *sc = v;
    709 
    710 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
    711 
    712 	usbd_set_polling(sc->sc_iface, on);
    713 }
    714 
    715 int
    716 ukbd_cnattach()
    717 {
    718 
    719 	/*
    720 	 * XXX USB requires too many parts of the kernel to be running
    721 	 * XXX in order to work, so we can't do much for the console
    722 	 * XXX keyboard until autconfiguration has run its course.
    723 	 */
    724 	ukbd_is_console = 1;
    725 	return (0);
    726 }
    727 
    728 #endif /* NetBSD */
    729 
    730 #if defined(__FreeBSD__)
    731 DRIVER_MODULE(ukbd, usb, ukbd_driver, ukbd_devclass, usbd_driver_load, 0);
    732 #endif
    733