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