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