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