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