Home | History | Annotate | Line # | Download | only in usb
ukbd.c revision 1.88
      1 /*      $NetBSD: ukbd.c,v 1.88 2005/05/03 18:37:46 jonathan 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 (lennart (at) augustsson.net) 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/devclass_docs/HID1_11.pdf
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.88 2005/05/03 18:37:46 jonathan Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/callout.h>
     50 #include <sys/kernel.h>
     51 #include <sys/device.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/tty.h>
     54 #include <sys/file.h>
     55 #include <sys/select.h>
     56 #include <sys/proc.h>
     57 #include <sys/vnode.h>
     58 #include <sys/poll.h>
     59 
     60 #include <dev/usb/usb.h>
     61 #include <dev/usb/usbhid.h>
     62 
     63 #include <dev/usb/usbdi.h>
     64 #include <dev/usb/usbdi_util.h>
     65 #include <dev/usb/usbdevs.h>
     66 #include <dev/usb/usb_quirks.h>
     67 #include <dev/usb/uhidev.h>
     68 #include <dev/usb/hid.h>
     69 #include <dev/usb/ukbdvar.h>
     70 
     71 #include <dev/wscons/wsconsio.h>
     72 #include <dev/wscons/wskbdvar.h>
     73 #include <dev/wscons/wsksymdef.h>
     74 #include <dev/wscons/wsksymvar.h>
     75 
     76 #include "opt_wsdisplay_compat.h"
     77 #include "opt_ddb.h"
     78 
     79 #ifdef UKBD_DEBUG
     80 #define DPRINTF(x)	if (ukbddebug) logprintf x
     81 #define DPRINTFN(n,x)	if (ukbddebug>(n)) logprintf x
     82 int	ukbddebug = 0;
     83 #else
     84 #define DPRINTF(x)
     85 #define DPRINTFN(n,x)
     86 #endif
     87 
     88 #define MAXKEYCODE 6
     89 #define MAXMOD 8		/* max 32 */
     90 
     91 struct ukbd_data {
     92 	u_int32_t	modifiers;
     93 	u_int8_t	keycode[MAXKEYCODE];
     94 };
     95 
     96 #define PRESS    0x000
     97 #define RELEASE  0x100
     98 #define CODEMASK 0x0ff
     99 
    100 #if defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD)
    101 #define NN 0			/* no translation */
    102 /*
    103  * Translate USB keycodes to US keyboard XT scancodes.
    104  * Scancodes >= 0x80 represent EXTENDED keycodes.
    105  *
    106  * See http://www.microsoft.com/HWDEV/TECH/input/Scancode.asp
    107  */
    108 Static const u_int8_t ukbd_trtab[256] = {
    109       NN,   NN,   NN,   NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
    110     0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
    111     0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
    112     0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
    113     0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
    114     0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
    115     0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
    116     0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
    117     0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */
    118     0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
    119     0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
    120     0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
    121     0x48, 0x49, 0x52, 0x53, 0x56, 0xdd,   NN, 0x59, /* 60 - 67 */
    122     0x5d, 0x5e, 0x5f,   NN,   NN,   NN,   NN,   NN, /* 68 - 6f */
    123       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 70 - 77 */
    124       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 78 - 7f */
    125       NN,   NN,   NN,   NN,   NN, 0x7e,   NN, 0x73, /* 80 - 87 */
    126     0x70, 0x7d, 0x79, 0x7b, 0x5c,   NN,   NN,   NN, /* 88 - 8f */
    127       NN,   NN, 0x78, 0x77, 0x76,   NN,   NN,   NN, /* 90 - 97 */
    128       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 98 - 9f */
    129       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a0 - a7 */
    130       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a8 - af */
    131       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b0 - b7 */
    132       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b8 - bf */
    133       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c0 - c7 */
    134       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c8 - cf */
    135       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d0 - d7 */
    136       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d8 - df */
    137     0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
    138       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* e8 - ef */
    139       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f0 - f7 */
    140       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f8 - ff */
    141 };
    142 #endif /* defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD) */
    143 
    144 #define KEY_ERROR 0x01
    145 
    146 #define MAXKEYS (MAXMOD+2*MAXKEYCODE)
    147 
    148 struct ukbd_softc {
    149 	struct uhidev sc_hdev;
    150 
    151 	struct ukbd_data sc_ndata;
    152 	struct ukbd_data sc_odata;
    153 	struct hid_location sc_modloc[MAXMOD];
    154 	u_int sc_nmod;
    155 	struct {
    156 		u_int32_t mask;
    157 		u_int8_t key;
    158 	} sc_mods[MAXMOD];
    159 
    160 	struct hid_location sc_keycodeloc;
    161 	u_int sc_nkeycode;
    162 
    163 	char sc_enabled;
    164 
    165 	int sc_console_keyboard;	/* we are the console keyboard */
    166 
    167 	char sc_debounce;		/* for quirk handling */
    168 	usb_callout_t sc_delay;		/* for quirk handling */
    169 	struct ukbd_data sc_data;	/* for quirk handling */
    170 
    171 	struct hid_location sc_numloc;
    172 	struct hid_location sc_capsloc;
    173 	struct hid_location sc_scroloc;
    174 	int sc_leds;
    175 #if defined(__NetBSD__)
    176 	struct device *sc_wskbddev;
    177 
    178 #if defined(WSDISPLAY_COMPAT_RAWKBD)
    179 	int sc_rawkbd;
    180 #if defined(UKBD_REPEAT)
    181 	usb_callout_t sc_rawrepeat_ch;
    182 #define REP_DELAY1 400
    183 #define REP_DELAYN 100
    184 	int sc_nrep;
    185 	char sc_rep[MAXKEYS];
    186 #endif /* defined(UKBD_REPEAT) */
    187 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
    188 
    189 	int sc_spl;
    190 	int sc_polling;
    191 	int sc_npollchar;
    192 	u_int16_t sc_pollchars[MAXKEYS];
    193 #endif /* defined(__NetBSD__) */
    194 
    195 	u_char sc_dying;
    196 };
    197 
    198 #ifdef UKBD_DEBUG
    199 #define UKBDTRACESIZE 64
    200 struct ukbdtraceinfo {
    201 	int unit;
    202 	struct timeval tv;
    203 	struct ukbd_data ud;
    204 };
    205 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
    206 int ukbdtraceindex = 0;
    207 int ukbdtrace = 0;
    208 void ukbdtracedump(void);
    209 void
    210 ukbdtracedump(void)
    211 {
    212 	int i;
    213 	for (i = 0; i < UKBDTRACESIZE; i++) {
    214 		struct ukbdtraceinfo *p =
    215 		    &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
    216 		printf("%lu.%06lu: mod=0x%02x key0=0x%02x key1=0x%02x "
    217 		       "key2=0x%02x key3=0x%02x\n",
    218 		       p->tv.tv_sec, p->tv.tv_usec,
    219 		       p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1],
    220 		       p->ud.keycode[2], p->ud.keycode[3]);
    221 	}
    222 }
    223 #endif
    224 
    225 #define	UKBDUNIT(dev)	(minor(dev))
    226 #define	UKBD_CHUNK	128	/* chunk size for read */
    227 #define	UKBD_BSIZE	1020	/* buffer size */
    228 
    229 Static int	ukbd_is_console;
    230 
    231 Static void	ukbd_cngetc(void *, u_int *, int *);
    232 Static void	ukbd_cnpollc(void *, int);
    233 
    234 #if defined(__NetBSD__)
    235 const struct wskbd_consops ukbd_consops = {
    236 	ukbd_cngetc,
    237 	ukbd_cnpollc,
    238 };
    239 #endif
    240 
    241 Static const char *ukbd_parse_desc(struct ukbd_softc *sc);
    242 
    243 Static void	ukbd_intr(struct uhidev *addr, void *ibuf, u_int len);
    244 Static void	ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud);
    245 Static void	ukbd_delayed_decode(void *addr);
    246 
    247 Static int	ukbd_enable(void *, int);
    248 Static void	ukbd_set_leds(void *, int);
    249 
    250 #if defined(__NetBSD__)
    251 Static int	ukbd_ioctl(void *, u_long, caddr_t, int, usb_proc_ptr );
    252 #if  defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
    253 Static void	ukbd_rawrepeat(void *v);
    254 #endif
    255 
    256 const struct wskbd_accessops ukbd_accessops = {
    257 	ukbd_enable,
    258 	ukbd_set_leds,
    259 	ukbd_ioctl,
    260 };
    261 
    262 extern const struct wscons_keydesc ukbd_keydesctab[];
    263 
    264 const struct wskbd_mapdata ukbd_keymapdata = {
    265 	ukbd_keydesctab,
    266 #if defined(UKBD_LAYOUT)
    267 	UKBD_LAYOUT,
    268 #elif defined(PCKBD_LAYOUT)
    269 	PCKBD_LAYOUT,
    270 #else
    271 	KB_US,
    272 #endif
    273 };
    274 #endif
    275 
    276 USB_DECLARE_DRIVER(ukbd);
    277 
    278 int
    279 ukbd_match(struct device *parent, struct cfdata *match, void *aux)
    280 {
    281 	struct uhidev_attach_arg *uha = aux;
    282 	int size;
    283 	void *desc;
    284 
    285 	uhidev_get_report_desc(uha->parent, &desc, &size);
    286 	if (!hid_is_collection(desc, size, uha->reportid,
    287 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
    288 		return (UMATCH_NONE);
    289 
    290 	return (UMATCH_IFACECLASS);
    291 }
    292 
    293 void
    294 ukbd_attach(struct device *parent, struct device *self, void *aux)
    295 {
    296 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
    297 	struct uhidev_attach_arg *uha = aux;
    298 	u_int32_t qflags;
    299 	const char *parseerr;
    300 #if defined(__NetBSD__)
    301 	struct wskbddev_attach_args a;
    302 #else
    303 	int i;
    304 #endif
    305 
    306 	sc->sc_hdev.sc_intr = ukbd_intr;
    307 	sc->sc_hdev.sc_parent = uha->parent;
    308 	sc->sc_hdev.sc_report_id = uha->reportid;
    309 
    310 	parseerr = ukbd_parse_desc(sc);
    311 	if (parseerr != NULL) {
    312 		printf("\n%s: attach failed, %s\n",
    313 		       sc->sc_hdev.sc_dev.dv_xname, parseerr);
    314 		USB_ATTACH_ERROR_RETURN;
    315 	}
    316 
    317 #ifdef DIAGNOSTIC
    318 	printf(": %d modifier keys, %d key codes", sc->sc_nmod,
    319 	       sc->sc_nkeycode);
    320 #endif
    321 	printf("\n");
    322 
    323 
    324 	qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
    325 	sc->sc_debounce = (qflags & UQ_SPUR_BUT_UP) != 0;
    326 
    327 	/*
    328 	 * Remember if we're the console keyboard.
    329 	 *
    330 	 * XXX This always picks the first keyboard on the
    331 	 * first USB bus, but what else can we really do?
    332 	 */
    333 	if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
    334 		/* Don't let any other keyboard have it. */
    335 		ukbd_is_console = 0;
    336 	}
    337 
    338 	if (sc->sc_console_keyboard) {
    339 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
    340 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
    341 		ukbd_enable(sc, 1);
    342 	}
    343 
    344 	a.console = sc->sc_console_keyboard;
    345 
    346 	a.keymap = &ukbd_keymapdata;
    347 
    348 	a.accessops = &ukbd_accessops;
    349 	a.accesscookie = sc;
    350 
    351 #ifdef UKBD_REPEAT
    352 	usb_callout_init(sc->sc_rawrepeat_ch);
    353 #endif
    354 
    355 	usb_callout_init(sc->sc_delay);
    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(uha->parent->sc_udev, 400);
    360 	ukbd_set_leds(sc, 0);
    361 
    362 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    363 
    364 	USB_ATTACH_SUCCESS_RETURN;
    365 }
    366 
    367 int
    368 ukbd_enable(void *v, int on)
    369 {
    370 	struct ukbd_softc *sc = v;
    371 
    372 	if (on && sc->sc_dying)
    373 		return (EIO);
    374 
    375 	/* Should only be called to change state */
    376 	if (sc->sc_enabled == on) {
    377 #ifdef DIAGNOSTIC
    378 		printf("ukbd_enable: %s: bad call on=%d\n",
    379 		       USBDEVNAME(sc->sc_hdev.sc_dev), on);
    380 #endif
    381 		return (EBUSY);
    382 	}
    383 
    384 	DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
    385 	sc->sc_enabled = on;
    386 	if (on) {
    387 		return (uhidev_open(&sc->sc_hdev));
    388 	} else {
    389 		uhidev_close(&sc->sc_hdev);
    390 		return (0);
    391 	}
    392 }
    393 
    394 int
    395 ukbd_activate(device_ptr_t self, enum devact act)
    396 {
    397 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
    398 	int rv = 0;
    399 
    400 	switch (act) {
    401 	case DVACT_ACTIVATE:
    402 		return (EOPNOTSUPP);
    403 
    404 	case DVACT_DEACTIVATE:
    405 		if (sc->sc_wskbddev != NULL)
    406 			rv = config_deactivate(sc->sc_wskbddev);
    407 		sc->sc_dying = 1;
    408 		break;
    409 	}
    410 	return (rv);
    411 }
    412 
    413 int
    414 ukbd_detach(struct device *self, int flags)
    415 {
    416 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
    417 	int rv = 0;
    418 
    419 	DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
    420 
    421 	if (sc->sc_console_keyboard) {
    422 #if 0
    423 		/*
    424 		 * XXX Should probably disconnect our consops,
    425 		 * XXX and either notify some other keyboard that
    426 		 * XXX it can now be the console, or if there aren't
    427 		 * XXX any more USB keyboards, set ukbd_is_console
    428 		 * XXX back to 1 so that the next USB keyboard attached
    429 		 * XXX to the system will get it.
    430 		 */
    431 		panic("ukbd_detach: console keyboard");
    432 #else
    433 		/*
    434 		 * Disconnect our consops and set ukbd_is_console
    435 		 * back to 1 so that the next USB keyboard attached
    436 		 * to the system will get it.
    437 		 * XXX Should notify some other keyboard that it can be
    438 		 * XXX console, if there are any other keyboards.
    439 		 */
    440 		printf("%s: was console keyboard\n",
    441 		       USBDEVNAME(sc->sc_hdev.sc_dev));
    442 		wskbd_cndetach();
    443 		ukbd_is_console = 1;
    444 #endif
    445 	}
    446 	/* No need to do reference counting of ukbd, wskbd has all the goo. */
    447 	if (sc->sc_wskbddev != NULL)
    448 		rv = config_detach(sc->sc_wskbddev, flags);
    449 
    450 	/* The console keyboard does not get a disable call, so check pipe. */
    451 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
    452 		uhidev_close(&sc->sc_hdev);
    453 
    454 	return (rv);
    455 }
    456 
    457 void
    458 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
    459 {
    460 	struct ukbd_softc *sc = (struct ukbd_softc *)addr;
    461 	struct ukbd_data *ud = &sc->sc_ndata;
    462 	int i;
    463 
    464 #ifdef UKBD_DEBUG
    465 	if (ukbddebug > 5) {
    466 		printf("ukbd_intr: data");
    467 		for (i = 0; i < len; i++)
    468 			printf(" 0x%02x", ((u_char *)ibuf)[i]);
    469 		printf("\n");
    470 	}
    471 #endif
    472 
    473 	ud->modifiers = 0;
    474 	for (i = 0; i < sc->sc_nmod; i++)
    475 		if (hid_get_data(ibuf, &sc->sc_modloc[i]))
    476 			ud->modifiers |= sc->sc_mods[i].mask;
    477 	memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8,
    478 	       sc->sc_nkeycode);
    479 
    480 	if (sc->sc_debounce && !sc->sc_polling) {
    481 		/*
    482 		 * Some keyboards have a peculiar quirk.  They sometimes
    483 		 * generate a key up followed by a key down for the same
    484 		 * key after about 10 ms.
    485 		 * We avoid this bug by holding off decoding for 20 ms.
    486 		 */
    487 		sc->sc_data = *ud;
    488 		usb_callout(sc->sc_delay, hz / 50, ukbd_delayed_decode, sc);
    489 #ifdef DDB
    490 	} else if (sc->sc_console_keyboard && !sc->sc_polling) {
    491 		/*
    492 		 * For the console keyboard we can't deliver CTL-ALT-ESC
    493 		 * from the interrupt routine.  Doing so would start
    494 		 * polling from inside the interrupt routine and that
    495 		 * loses bigtime.
    496 		 */
    497 		sc->sc_data = *ud;
    498 		usb_callout(sc->sc_delay, 1, ukbd_delayed_decode, sc);
    499 #endif
    500 	} else {
    501 		ukbd_decode(sc, ud);
    502 	}
    503 }
    504 
    505 void
    506 ukbd_delayed_decode(void *addr)
    507 {
    508 	struct ukbd_softc *sc = addr;
    509 
    510 	ukbd_decode(sc, &sc->sc_data);
    511 }
    512 
    513 void
    514 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
    515 {
    516 	int mod, omod;
    517 	u_int16_t ibuf[MAXKEYS];	/* chars events */
    518 	int s;
    519 	int nkeys, i, j;
    520 	int key;
    521 #define ADDKEY(c) ibuf[nkeys++] = (c)
    522 
    523 #ifdef UKBD_DEBUG
    524 	/*
    525 	 * Keep a trace of the last events.  Using printf changes the
    526 	 * timing, so this can be useful sometimes.
    527 	 */
    528 	if (ukbdtrace) {
    529 		struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
    530 		p->unit = sc->sc_hdev.sc_dev.dv_unit;
    531 		microtime(&p->tv);
    532 		p->ud = *ud;
    533 		if (++ukbdtraceindex >= UKBDTRACESIZE)
    534 			ukbdtraceindex = 0;
    535 	}
    536 	if (ukbddebug > 5) {
    537 		struct timeval tv;
    538 		microtime(&tv);
    539 		DPRINTF((" at %lu.%06lu  mod=0x%02x key0=0x%02x key1=0x%02x "
    540 			 "key2=0x%02x key3=0x%02x\n",
    541 			 tv.tv_sec, tv.tv_usec,
    542 			 ud->modifiers, ud->keycode[0], ud->keycode[1],
    543 			 ud->keycode[2], ud->keycode[3]));
    544 	}
    545 #endif
    546 
    547 	if (ud->keycode[0] == KEY_ERROR) {
    548 		DPRINTF(("ukbd_intr: KEY_ERROR\n"));
    549 		return;		/* ignore  */
    550 	}
    551 	nkeys = 0;
    552 	mod = ud->modifiers;
    553 	omod = sc->sc_odata.modifiers;
    554 	if (mod != omod)
    555 		for (i = 0; i < sc->sc_nmod; i++)
    556 			if (( mod & sc->sc_mods[i].mask) !=
    557 			    (omod & sc->sc_mods[i].mask))
    558 				ADDKEY(sc->sc_mods[i].key |
    559 				       (mod & sc->sc_mods[i].mask
    560 					  ? PRESS : RELEASE));
    561 	if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
    562 		/* Check for released keys. */
    563 		for (i = 0; i < sc->sc_nkeycode; i++) {
    564 			key = sc->sc_odata.keycode[i];
    565 			if (key == 0)
    566 				continue;
    567 			for (j = 0; j < sc->sc_nkeycode; j++)
    568 				if (key == ud->keycode[j])
    569 					goto rfound;
    570 			DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
    571 			ADDKEY(key | RELEASE);
    572 		rfound:
    573 			;
    574 		}
    575 
    576 		/* Check for pressed keys. */
    577 		for (i = 0; i < sc->sc_nkeycode; i++) {
    578 			key = ud->keycode[i];
    579 			if (key == 0)
    580 				continue;
    581 			for (j = 0; j < sc->sc_nkeycode; j++)
    582 				if (key == sc->sc_odata.keycode[j])
    583 					goto pfound;
    584 			DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
    585 			ADDKEY(key | PRESS);
    586 		pfound:
    587 			;
    588 		}
    589 	}
    590 	sc->sc_odata = *ud;
    591 
    592 	if (nkeys == 0)
    593 		return;
    594 
    595 	if (sc->sc_polling) {
    596 		DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
    597 		memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
    598 		sc->sc_npollchar = nkeys;
    599 		return;
    600 	}
    601 #ifdef WSDISPLAY_COMPAT_RAWKBD
    602 	if (sc->sc_rawkbd) {
    603 		u_char cbuf[MAXKEYS * 2];
    604 		int c;
    605 		int npress;
    606 
    607 		for (npress = i = j = 0; i < nkeys; i++) {
    608 			key = ibuf[i];
    609 			c = ukbd_trtab[key & CODEMASK];
    610 			if (c == NN)
    611 				continue;
    612 			if (c & 0x80)
    613 				cbuf[j++] = 0xe0;
    614 			cbuf[j] = c & 0x7f;
    615 			if (key & RELEASE)
    616 				cbuf[j] |= 0x80;
    617 #if defined(UKBD_REPEAT)
    618 			else {
    619 				/* remember pressed keys for autorepeat */
    620 				if (c & 0x80)
    621 					sc->sc_rep[npress++] = 0xe0;
    622 				sc->sc_rep[npress++] = c & 0x7f;
    623 			}
    624 #endif
    625 			DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
    626 				    c & 0x80 ? "0xe0 " : "",
    627 				    cbuf[j]));
    628 			j++;
    629 		}
    630 		s = spltty();
    631 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    632 		splx(s);
    633 #ifdef UKBD_REPEAT
    634 		usb_uncallout(sc->sc_rawrepeat_ch, ukbd_rawrepeat, sc);
    635 		if (npress != 0) {
    636 			sc->sc_nrep = npress;
    637 			usb_callout(sc->sc_rawrepeat_ch,
    638 			    hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
    639 		}
    640 #endif
    641 		return;
    642 	}
    643 #endif
    644 
    645 	s = spltty();
    646 	for (i = 0; i < nkeys; i++) {
    647 		key = ibuf[i];
    648 		wskbd_input(sc->sc_wskbddev,
    649 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
    650 		    key&CODEMASK);
    651 	}
    652 	splx(s);
    653 }
    654 
    655 void
    656 ukbd_set_leds(void *v, int leds)
    657 {
    658 	struct ukbd_softc *sc = v;
    659 	u_int8_t res;
    660 
    661 	DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n",
    662 		 sc, leds, sc->sc_leds));
    663 
    664 	if (sc->sc_dying)
    665 		return;
    666 
    667 	if (sc->sc_leds == leds)
    668 		return;
    669 	sc->sc_leds = leds;
    670 	res = 0;
    671 	/* XXX not really right */
    672 	if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
    673 		res |= 1 << sc->sc_scroloc.pos;
    674 	if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
    675 		res |= 1 << sc->sc_numloc.pos;
    676 	if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
    677 		res |= 1 << sc->sc_capsloc.pos;
    678 	uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1);
    679 }
    680 
    681 #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
    682 void
    683 ukbd_rawrepeat(void *v)
    684 {
    685 	struct ukbd_softc *sc = v;
    686 	int s;
    687 
    688 	s = spltty();
    689 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
    690 	splx(s);
    691 	usb_callout(sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
    692 	    ukbd_rawrepeat, sc);
    693 }
    694 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) */
    695 
    696 int
    697 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
    698 {
    699 	struct ukbd_softc *sc = v;
    700 
    701 	switch (cmd) {
    702 	case WSKBDIO_GTYPE:
    703 		*(int *)data = WSKBD_TYPE_USB;
    704 		return (0);
    705 	case WSKBDIO_SETLEDS:
    706 		ukbd_set_leds(v, *(int *)data);
    707 		return (0);
    708 	case WSKBDIO_GETLEDS:
    709 		*(int *)data = sc->sc_leds;
    710 		return (0);
    711 #if defined(WSDISPLAY_COMPAT_RAWKBD)
    712 	case WSKBDIO_SETMODE:
    713 		DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
    714 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    715 #if defined(UKBD_REPEAT)
    716 		usb_uncallout(sc->sc_rawrepeat_ch, ukbd_rawrepeat, sc);
    717 #endif
    718 		return (0);
    719 #endif
    720 	}
    721 	return (EPASSTHROUGH);
    722 }
    723 
    724 /*
    725  * This is a hack to work around some broken ports that don't call
    726  * cnpollc() before cngetc().
    727  */
    728 static int pollenter, warned;
    729 
    730 /* Console interface. */
    731 void
    732 ukbd_cngetc(void *v, u_int *type, int *data)
    733 {
    734 	struct ukbd_softc *sc = v;
    735 	int c;
    736 	int broken;
    737 
    738 	if (pollenter == 0) {
    739 		if (!warned) {
    740 			printf("\n"
    741 "This port is broken, it does not call cnpollc() before calling cngetc().\n"
    742 "This should be fixed, but it will work anyway (for now).\n");
    743 			warned = 1;
    744 		}
    745 		broken = 1;
    746 		ukbd_cnpollc(v, 1);
    747 	} else
    748 		broken = 0;
    749 
    750 	DPRINTFN(0,("ukbd_cngetc: enter\n"));
    751 	sc->sc_polling = 1;
    752 	while(sc->sc_npollchar <= 0)
    753 		usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface);
    754 	sc->sc_polling = 0;
    755 	c = sc->sc_pollchars[0];
    756 	sc->sc_npollchar--;
    757 	memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
    758 	       sc->sc_npollchar * sizeof(u_int16_t));
    759 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    760 	*data = c & CODEMASK;
    761 	DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
    762 	if (broken)
    763 		ukbd_cnpollc(v, 0);
    764 }
    765 
    766 void
    767 ukbd_cnpollc(void *v, int on)
    768 {
    769 	struct ukbd_softc *sc = v;
    770 	usbd_device_handle dev;
    771 
    772 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
    773 
    774 	usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev);
    775 	if (on) {
    776 		sc->sc_spl = splusb();
    777 		pollenter++;
    778 	} else {
    779 		splx(sc->sc_spl);
    780 		pollenter--;
    781 	}
    782 	usbd_set_polling(dev, on);
    783 }
    784 
    785 int
    786 ukbd_cnattach(void)
    787 {
    788 
    789 	/*
    790 	 * XXX USB requires too many parts of the kernel to be running
    791 	 * XXX in order to work, so we can't do much for the console
    792 	 * XXX keyboard until autconfiguration has run its course.
    793 	 */
    794 	ukbd_is_console = 1;
    795 	return (0);
    796 }
    797 
    798 const char *
    799 ukbd_parse_desc(struct ukbd_softc *sc)
    800 {
    801 	struct hid_data *d;
    802 	struct hid_item h;
    803 	int size;
    804 	void *desc;
    805 	int imod;
    806 
    807 	uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
    808 	imod = 0;
    809 	sc->sc_nkeycode = 0;
    810 	d = hid_start_parse(desc, size, hid_input);
    811 	while (hid_get_item(d, &h)) {
    812 		/*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n",
    813 		  h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/
    814 		if (h.kind != hid_input || (h.flags & HIO_CONST) ||
    815 		    HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
    816 		    h.report_ID != sc->sc_hdev.sc_report_id)
    817 			continue;
    818 		DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d "
    819 			 "cnt=%d\n", imod,
    820 			 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
    821 		if (h.flags & HIO_VARIABLE) {
    822 			if (h.loc.size != 1)
    823 				return ("bad modifier size");
    824 			/* Single item */
    825 			if (imod < MAXMOD) {
    826 				sc->sc_modloc[imod] = h.loc;
    827 				sc->sc_mods[imod].mask = 1 << imod;
    828 				sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
    829 				imod++;
    830 			} else
    831 				return ("too many modifier keys");
    832 		} else {
    833 			/* Array */
    834 			if (h.loc.size != 8)
    835 				return ("key code size != 8");
    836 			if (h.loc.count > MAXKEYCODE)
    837 				return ("too many key codes");
    838 			if (h.loc.pos % 8 != 0)
    839 				return ("key codes not on byte boundary");
    840 			if (sc->sc_nkeycode != 0)
    841 				return ("multiple key code arrays\n");
    842 			sc->sc_keycodeloc = h.loc;
    843 			sc->sc_nkeycode = h.loc.count;
    844 		}
    845 	}
    846 	sc->sc_nmod = imod;
    847 	hid_end_parse(d);
    848 
    849 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
    850 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL);
    851 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
    852 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL);
    853 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
    854 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL);
    855 
    856 	return (NULL);
    857 }
    858