Home | History | Annotate | Line # | Download | only in usb
ukbd.c revision 1.125
      1 /*      $NetBSD: ukbd.c,v 1.125 2013/01/05 01:30:17 christos 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.125 2013/01/05 01:30:17 christos Exp $");
     39 
     40 #ifdef _KERNEL_OPT
     41 #include "opt_ukbd.h"
     42 #include "opt_ukbd_layout.h"
     43 #include "opt_wsdisplay_compat.h"
     44 #include "opt_ddb.h"
     45 #endif /* _KERNEL_OPT */
     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/file.h>
     54 #include <sys/select.h>
     55 #include <sys/proc.h>
     56 #include <sys/vnode.h>
     57 #include <sys/poll.h>
     58 
     59 #include <dev/usb/usb.h>
     60 #include <dev/usb/usbhid.h>
     61 
     62 #include <dev/usb/usbdi.h>
     63 #include <dev/usb/usbdi_util.h>
     64 #include <dev/usb/usbdivar.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 #ifdef UKBD_DEBUG
     77 #define DPRINTF(x)	if (ukbddebug) printf x
     78 #define DPRINTFN(n,x)	if (ukbddebug>(n)) printf x
     79 int	ukbddebug = 0;
     80 #else
     81 #define DPRINTF(x)
     82 #define DPRINTFN(n,x)
     83 #endif
     84 
     85 #define MAXKEYCODE 6
     86 #define MAXMOD 8		/* max 32 */
     87 
     88 struct ukbd_data {
     89 	u_int32_t	modifiers;
     90 	u_int8_t	keycode[MAXKEYCODE];
     91 };
     92 
     93 #define PRESS    0x000
     94 #define RELEASE  0x100
     95 #define CODEMASK 0x0ff
     96 
     97 struct ukbd_keycodetrans {
     98 	u_int16_t	from;
     99 	u_int16_t	to;
    100 };
    101 
    102 #define IS_PMF	0x8000
    103 
    104 Static const struct ukbd_keycodetrans trtab_apple_fn[] = {
    105 	{ 0x0c, 0x5d },	/* i -> KP 5 */
    106 	{ 0x0d, 0x59 },	/* j -> KP 1 */
    107 	{ 0x0e, 0x5a },	/* k -> KP 2 */
    108 	{ 0x0f, 0x5b },	/* l -> KP 3 */
    109 	{ 0x10, 0x62 },	/* m -> KP 0 */
    110 	{ 0x12, 0x5e },	/* o -> KP 6 */
    111 	{ 0x13, 0x55 },	/* o -> KP * */
    112 	{ 0x18, 0x5c },	/* u -> KP 4 */
    113 	{ 0x0c, 0x5d },	/* i -> KP 5 */
    114 	{ 0x2a, 0x4c },	/* Backspace -> Delete */
    115 	{ 0x28, 0x49 },	/* Return -> Insert */
    116 	{ 0x24, 0x5f }, /* 7 -> KP 7 */
    117 	{ 0x25, 0x60 }, /* 8 -> KP 8 */
    118 	{ 0x26, 0x61 }, /* 9 -> KP 9 */
    119 	{ 0x27, 0x54 }, /* 0 -> KP / */
    120 	{ 0x2d, 0x67 }, /* - -> KP = */
    121 	{ 0x33, 0x56 },	/* ; -> KP - */
    122 	{ 0x37, 0x63 },	/* . -> KP . */
    123 	{ 0x38, 0x57 },	/* / -> KP + */
    124 	{ 0x3a, 0xd1 },	/* F1..F12 mapped to reserved codes 0xd1..0xdc */
    125 	{ 0x3b, 0xd2 },
    126 	{ 0x3c, 0xd3 },
    127 	{ 0x3d, 0xd4 },
    128 	{ 0x3e, 0xd5 },
    129 	{ 0x3f, 0xd6 },
    130 	{ 0x40, 0xd7 },
    131 	{ 0x41, 0xd8 },
    132 	{ 0x42, 0xd9 },
    133 	{ 0x43, 0xda },
    134 	{ 0x44, 0xdb },
    135 	{ 0x45, 0xdc },
    136 	{ 0x4f, 0x4d },	/* Right -> End */
    137 	{ 0x50, 0x4a },	/* Left -> Home */
    138 	{ 0x51, 0x4e },	/* Down -> PageDown */
    139 	{ 0x52, 0x4b },	/* Up -> PageUp */
    140 	{ 0x00, 0x00 }
    141 };
    142 
    143 Static const struct ukbd_keycodetrans trtab_apple_iso[] = {
    144 	{ 0x35, 0x64 },	/* swap the key above tab with key right of shift */
    145 	{ 0x64, 0x35 },
    146 	{ 0x31, 0x32 },	/* key left of return is Europe1, not "\|" */
    147 	{ 0x00, 0x00 }
    148 };
    149 
    150 #ifdef GDIUM_KEYBOARD_HACK
    151 Static const struct ukbd_keycodetrans trtab_gdium_fn[] = {
    152 #ifdef notyet
    153 	{ 58, 0 },	/* F1 -> toggle camera */
    154 	{ 59, 0 },	/* F2 -> toggle wireless */
    155 #endif
    156 	{ 60, IS_PMF | PMFE_AUDIO_VOLUME_TOGGLE },
    157 	{ 61, IS_PMF | PMFE_AUDIO_VOLUME_UP },
    158 	{ 62, IS_PMF | PMFE_AUDIO_VOLUME_DOWN },
    159 #ifdef notyet
    160 	{ 63, 0 },	/* F6 -> toggle ext. video */
    161 	{ 64, 0 },	/* F7 -> toggle mouse */
    162 #endif
    163 	{ 65, IS_PMF | PMFE_DISPLAY_BRIGHTNESS_UP },
    164 	{ 66, IS_PMF | PMFE_DISPLAY_BRIGHTNESS_DOWN },
    165 #ifdef notyet
    166 	{ 67, 0 },	/* F10 -> suspend */
    167 	{ 68, 0 },	/* F11 -> user1 */
    168 	{ 69, 0 },	/* F12 -> user2 */
    169 	{ 70, 0 },	/* print screen -> sysrq */
    170 #endif
    171 	{ 76, 71 },	/* delete -> scroll lock */
    172 	{ 81, 78 },	/* down -> page down */
    173 	{ 82, 75 },	/* up -> page up */
    174 	{  0, 0 }
    175 };
    176 #endif
    177 
    178 Static const struct ukbd_keycodetrans trtab_generic[] = {
    179 	{ 0x7f, IS_PMF | PMFE_AUDIO_VOLUME_TOGGLE },
    180 	{ 0x80, IS_PMF | PMFE_AUDIO_VOLUME_UP },
    181 	{ 0x81, IS_PMF | PMFE_AUDIO_VOLUME_DOWN },
    182 	{ 0x00, 0x00 }
    183 };
    184 
    185 #if defined(WSDISPLAY_COMPAT_RAWKBD)
    186 #define NN 0			/* no translation */
    187 /*
    188  * Translate USB keycodes to US keyboard XT scancodes.
    189  * Scancodes >= 0x80 represent EXTENDED keycodes.
    190  *
    191  * See http://www.microsoft.com/whdc/archive/scancode.mspx
    192  *
    193  * Note: a real pckbd(4) has more complexity in its
    194  * protocol for some keys than this translation implements.
    195  * For example, some keys generate Fake ShiftL events (e0 2a)
    196  * before the actual key sequence.
    197  */
    198 Static const u_int8_t ukbd_trtab[256] = {
    199       NN,   NN,   NN,   NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
    200     0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
    201     0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
    202     0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
    203     0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
    204     0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
    205     0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
    206     0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
    207     0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xb7, 0x46, /* 40 - 47 */
    208     0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
    209     0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
    210     0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
    211     0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, 0xdf, 0x59, /* 60 - 67 */
    212     0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,   NN, /* 68 - 6f */
    213       NN,   NN,   NN,   NN, 0x84, 0x85, 0x87, 0x88, /* 70 - 77 */
    214     0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,   NN, /* 78 - 7f */
    215       NN,   NN,   NN,   NN,   NN, 0x7e,   NN, 0x73, /* 80 - 87 */
    216     0x70, 0x7d, 0x79, 0x7b, 0x5c,   NN,   NN,   NN, /* 88 - 8f */
    217       NN,   NN, 0x78, 0x77, 0x76,   NN,   NN,   NN, /* 90 - 97 */
    218       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 98 - 9f */
    219       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a0 - a7 */
    220       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a8 - af */
    221       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b0 - b7 */
    222       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b8 - bf */
    223       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c0 - c7 */
    224       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c8 - cf */
    225       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d0 - d7 */
    226       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d8 - df */
    227     0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
    228       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* e8 - ef */
    229       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f0 - f7 */
    230       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f8 - ff */
    231 };
    232 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
    233 
    234 #define KEY_ERROR 0x01
    235 
    236 #define MAXKEYS (MAXMOD+2*MAXKEYCODE)
    237 
    238 struct ukbd_softc {
    239 	struct uhidev sc_hdev;
    240 
    241 	struct ukbd_data sc_ndata;
    242 	struct ukbd_data sc_odata;
    243 	struct hid_location sc_modloc[MAXMOD];
    244 	u_int sc_nmod;
    245 	struct {
    246 		u_int32_t mask;
    247 		u_int8_t key;
    248 	} sc_mods[MAXMOD];
    249 
    250 	struct hid_location sc_keycodeloc;
    251 	u_int sc_nkeycode;
    252 
    253 	u_int sc_flags;			/* flags */
    254 #define FLAG_ENABLED		0x0001
    255 #define FLAG_POLLING		0x0002
    256 #define FLAG_DEBOUNCE		0x0004	/* for quirk handling */
    257 #define FLAG_APPLE_FIX_ISO	0x0008
    258 #define FLAG_APPLE_FN		0x0010
    259 #define FLAG_GDIUM_FN		0x0020
    260 #define FLAG_FN_PRESSED		0x0100	/* FN key is held down */
    261 #define FLAG_FN_ALT		0x0200	/* Last Alt key was FN-Alt = AltGr */
    262 
    263 	int sc_console_keyboard;	/* we are the console keyboard */
    264 
    265 	struct callout sc_delay;	/* for quirk handling */
    266 	struct ukbd_data sc_data;	/* for quirk handling */
    267 
    268 	struct hid_location sc_apple_fn;
    269 	struct hid_location sc_numloc;
    270 	struct hid_location sc_capsloc;
    271 	struct hid_location sc_scroloc;
    272 	struct hid_location sc_compose;
    273 	int sc_leds;
    274 	device_t sc_wskbddev;
    275 
    276 #if defined(WSDISPLAY_COMPAT_RAWKBD)
    277 	int sc_rawkbd;
    278 #if defined(UKBD_REPEAT)
    279 	struct callout sc_rawrepeat_ch;
    280 #define REP_DELAY1 400
    281 #define REP_DELAYN 100
    282 	int sc_nrep;
    283 	char sc_rep[MAXKEYS];
    284 #endif /* defined(UKBD_REPEAT) */
    285 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
    286 
    287 	int sc_spl;
    288 	int sc_npollchar;
    289 	u_int16_t sc_pollchars[MAXKEYS];
    290 
    291 	u_char sc_dying;
    292 };
    293 
    294 #ifdef UKBD_DEBUG
    295 #define UKBDTRACESIZE 64
    296 struct ukbdtraceinfo {
    297 	int unit;
    298 	struct timeval tv;
    299 	struct ukbd_data ud;
    300 };
    301 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
    302 int ukbdtraceindex = 0;
    303 int ukbdtrace = 0;
    304 void ukbdtracedump(void);
    305 void
    306 ukbdtracedump(void)
    307 {
    308 	int i;
    309 	for (i = 0; i < UKBDTRACESIZE; i++) {
    310 		struct ukbdtraceinfo *p =
    311 		    &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
    312 		printf("%"PRIu64".%06"PRIu64": mod=0x%02x key0=0x%02x key1=0x%02x "
    313 		       "key2=0x%02x key3=0x%02x\n",
    314 		       p->tv.tv_sec, (uint64_t)p->tv.tv_usec,
    315 		       p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1],
    316 		       p->ud.keycode[2], p->ud.keycode[3]);
    317 	}
    318 }
    319 #endif
    320 
    321 #define	UKBDUNIT(dev)	(minor(dev))
    322 #define	UKBD_CHUNK	128	/* chunk size for read */
    323 #define	UKBD_BSIZE	1020	/* buffer size */
    324 
    325 Static int	ukbd_is_console;
    326 
    327 Static void	ukbd_cngetc(void *, u_int *, int *);
    328 Static void	ukbd_cnpollc(void *, int);
    329 
    330 const struct wskbd_consops ukbd_consops = {
    331 	.getc =  ukbd_cngetc,
    332 	.pollc = ukbd_cnpollc,
    333 	.bell =  NULL,
    334 };
    335 
    336 Static const char *ukbd_parse_desc(struct ukbd_softc *sc);
    337 
    338 Static void	ukbd_intr(struct uhidev *addr, void *ibuf, u_int len);
    339 Static void	ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud);
    340 Static void	ukbd_delayed_decode(void *addr);
    341 
    342 Static int	ukbd_enable(void *, int);
    343 Static void	ukbd_set_leds(void *, int);
    344 
    345 Static int	ukbd_ioctl(void *, u_long, void *, int, struct lwp *);
    346 #if  defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
    347 Static void	ukbd_rawrepeat(void *v);
    348 #endif
    349 
    350 const struct wskbd_accessops ukbd_accessops = {
    351 	ukbd_enable,
    352 	ukbd_set_leds,
    353 	ukbd_ioctl,
    354 };
    355 
    356 extern const struct wscons_keydesc ukbd_keydesctab[];
    357 
    358 const struct wskbd_mapdata ukbd_keymapdata = {
    359 	ukbd_keydesctab,
    360 #if defined(UKBD_LAYOUT)
    361 	UKBD_LAYOUT,
    362 #elif defined(PCKBD_LAYOUT)
    363 	PCKBD_LAYOUT,
    364 #else
    365 	KB_US,
    366 #endif
    367 };
    368 
    369 static int ukbd_match(device_t, cfdata_t, void *);
    370 static void ukbd_attach(device_t, device_t, void *);
    371 static int ukbd_detach(device_t, int);
    372 static int ukbd_activate(device_t, enum devact);
    373 static void ukbd_childdet(device_t, device_t);
    374 
    375 extern struct cfdriver ukbd_cd;
    376 
    377 CFATTACH_DECL2_NEW(ukbd, sizeof(struct ukbd_softc), ukbd_match, ukbd_attach,
    378     ukbd_detach, ukbd_activate, NULL, ukbd_childdet);
    379 
    380 int
    381 ukbd_match(device_t parent, cfdata_t match, void *aux)
    382 {
    383 	struct uhidev_attach_arg *uha = aux;
    384 	int size;
    385 	void *desc;
    386 
    387 	uhidev_get_report_desc(uha->parent, &desc, &size);
    388 	if (!hid_is_collection(desc, size, uha->reportid,
    389 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
    390 		return (UMATCH_NONE);
    391 
    392 	return (UMATCH_IFACECLASS);
    393 }
    394 
    395 void
    396 ukbd_attach(device_t parent, device_t self, void *aux)
    397 {
    398 	struct ukbd_softc *sc = device_private(self);
    399 	struct uhidev_attach_arg *uha = aux;
    400 	u_int32_t qflags;
    401 	const char *parseerr;
    402 	struct wskbddev_attach_args a;
    403 
    404 	sc->sc_hdev.sc_dev = self;
    405 	sc->sc_hdev.sc_intr = ukbd_intr;
    406 	sc->sc_hdev.sc_parent = uha->parent;
    407 	sc->sc_hdev.sc_report_id = uha->reportid;
    408 	sc->sc_flags = 0;
    409 
    410 	if (!pmf_device_register(self, NULL, NULL)) {
    411 		aprint_normal("\n");
    412 		aprint_error_dev(self, "couldn't establish power handler\n");
    413 	}
    414 
    415 	parseerr = ukbd_parse_desc(sc);
    416 	if (parseerr != NULL) {
    417 		aprint_normal("\n");
    418 		aprint_error_dev(self, "attach failed, %s\n", parseerr);
    419 		return;
    420 	}
    421 
    422 	/* Quirks */
    423 	qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
    424 	if (qflags & UQ_SPUR_BUT_UP)
    425 		sc->sc_flags |= FLAG_DEBOUNCE;
    426 	if (qflags & UQ_APPLE_ISO)
    427 		sc->sc_flags |= FLAG_APPLE_FIX_ISO;
    428 
    429 #ifdef GDIUM_KEYBOARD_HACK
    430 	if (uha->uaa->vendor == USB_VENDOR_CYPRESS &&
    431 	    uha->uaa->product == USB_PRODUCT_CYPRESS_LPRDK)
    432 		sc->sc_flags = FLAG_GDIUM_FN;
    433 #endif
    434 
    435 #ifdef DIAGNOSTIC
    436 	aprint_normal(": %d modifier keys, %d key codes", sc->sc_nmod,
    437 	       sc->sc_nkeycode);
    438 	if (sc->sc_flags & FLAG_APPLE_FN)
    439 		aprint_normal(", apple fn key");
    440 	if (sc->sc_flags & FLAG_APPLE_FIX_ISO)
    441 		aprint_normal(", fix apple iso");
    442 	if (sc->sc_flags & FLAG_GDIUM_FN)
    443 		aprint_normal(", Gdium fn key");
    444 #endif
    445 	aprint_normal("\n");
    446 
    447 	/*
    448 	 * Remember if we're the console keyboard.
    449 	 *
    450 	 * XXX This always picks the first keyboard on the
    451 	 * first USB bus, but what else can we really do?
    452 	 */
    453 	if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
    454 		/* Don't let any other keyboard have it. */
    455 		ukbd_is_console = 0;
    456 	}
    457 
    458 	if (sc->sc_console_keyboard) {
    459 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
    460 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
    461 		ukbd_enable(sc, 1);
    462 	}
    463 
    464 	a.console = sc->sc_console_keyboard;
    465 
    466 	a.keymap = &ukbd_keymapdata;
    467 
    468 	a.accessops = &ukbd_accessops;
    469 	a.accesscookie = sc;
    470 
    471 #ifdef UKBD_REPEAT
    472 	callout_init(&sc->sc_rawrepeat_ch, 0);
    473 #endif
    474 
    475 	callout_init(&sc->sc_delay, 0);
    476 
    477 	/* Flash the leds; no real purpose, just shows we're alive. */
    478 	ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS
    479 			| WSKBD_LED_COMPOSE);
    480 	usbd_delay_ms(uha->parent->sc_udev, 400);
    481 	ukbd_set_leds(sc, 0);
    482 
    483 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    484 
    485 	return;
    486 }
    487 
    488 int
    489 ukbd_enable(void *v, int on)
    490 {
    491 	struct ukbd_softc *sc = v;
    492 
    493 	if (on && sc->sc_dying)
    494 		return (EIO);
    495 
    496 	/* Should only be called to change state */
    497 	if ((sc->sc_flags & FLAG_ENABLED) != 0 && on != 0) {
    498 #ifdef DIAGNOSTIC
    499 		printf("ukbd_enable: %s: bad call on=%d\n",
    500 		       device_xname(sc->sc_hdev.sc_dev), on);
    501 #endif
    502 		return (EBUSY);
    503 	}
    504 
    505 	DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
    506 	if (on) {
    507 		sc->sc_flags |= FLAG_ENABLED;
    508 		return (uhidev_open(&sc->sc_hdev));
    509 	} else {
    510 		sc->sc_flags &= ~FLAG_ENABLED;
    511 		uhidev_close(&sc->sc_hdev);
    512 		return (0);
    513 	}
    514 }
    515 
    516 
    517 static void
    518 ukbd_childdet(device_t self, device_t child)
    519 {
    520 	struct ukbd_softc *sc = device_private(self);
    521 
    522 	KASSERT(sc->sc_wskbddev == child);
    523 	sc->sc_wskbddev = NULL;
    524 }
    525 
    526 int
    527 ukbd_activate(device_t self, enum devact act)
    528 {
    529 	struct ukbd_softc *sc = device_private(self);
    530 
    531 	switch (act) {
    532 	case DVACT_DEACTIVATE:
    533 		sc->sc_dying = 1;
    534 		return 0;
    535 	default:
    536 		return EOPNOTSUPP;
    537 	}
    538 }
    539 
    540 int
    541 ukbd_detach(device_t self, int flags)
    542 {
    543 	struct ukbd_softc *sc = device_private(self);
    544 	int rv = 0;
    545 
    546 	DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
    547 
    548 	pmf_device_deregister(self);
    549 
    550 	if (sc->sc_console_keyboard) {
    551 #if 0
    552 		/*
    553 		 * XXX Should probably disconnect our consops,
    554 		 * XXX and either notify some other keyboard that
    555 		 * XXX it can now be the console, or if there aren't
    556 		 * XXX any more USB keyboards, set ukbd_is_console
    557 		 * XXX back to 1 so that the next USB keyboard attached
    558 		 * XXX to the system will get it.
    559 		 */
    560 		panic("ukbd_detach: console keyboard");
    561 #else
    562 		/*
    563 		 * Disconnect our consops and set ukbd_is_console
    564 		 * back to 1 so that the next USB keyboard attached
    565 		 * to the system will get it.
    566 		 * XXX Should notify some other keyboard that it can be
    567 		 * XXX console, if there are any other keyboards.
    568 		 */
    569 		printf("%s: was console keyboard\n",
    570 		       device_xname(sc->sc_hdev.sc_dev));
    571 		wskbd_cndetach();
    572 		ukbd_is_console = 1;
    573 #endif
    574 	}
    575 	/* No need to do reference counting of ukbd, wskbd has all the goo. */
    576 	if (sc->sc_wskbddev != NULL)
    577 		rv = config_detach(sc->sc_wskbddev, flags);
    578 
    579 	/* The console keyboard does not get a disable call, so check pipe. */
    580 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
    581 		uhidev_close(&sc->sc_hdev);
    582 
    583 	return (rv);
    584 }
    585 
    586 static void
    587 ukbd_translate_keycodes(struct ukbd_softc *sc, struct ukbd_data *ud,
    588     const struct ukbd_keycodetrans *tab)
    589 {
    590 	const struct ukbd_keycodetrans *tp;
    591 	int i;
    592 	u_int8_t key;
    593 
    594 	for (i = 0; i < sc->sc_nkeycode; i++) {
    595 		key = ud->keycode[i];
    596 		if (key)
    597 			for (tp = tab; tp->from; tp++)
    598 				if (tp->from == key) {
    599 					if (tp->to & IS_PMF) {
    600 						pmf_event_inject(
    601 						    sc->sc_hdev.sc_dev,
    602 						    tp->to & 0xff);
    603 						ud->keycode[i] = 0;
    604 					} else
    605 						ud->keycode[i] = tp->to;
    606 					break;
    607 				}
    608 	}
    609 }
    610 
    611 static u_int16_t
    612 ukbd_translate_modifier(struct ukbd_softc *sc, u_int16_t key)
    613 {
    614 	if ((sc->sc_flags & FLAG_APPLE_FN) && (key & CODEMASK) == 0x00e2) {
    615 		if ((key & ~CODEMASK) == PRESS) {
    616 			if (sc->sc_flags & FLAG_FN_PRESSED) {
    617 				/* pressed FN-Alt, translate to AltGr */
    618 				key = 0x00e6 | PRESS;
    619 				sc->sc_flags |= FLAG_FN_ALT;
    620 			}
    621 		} else {
    622 			if (sc->sc_flags & FLAG_FN_ALT) {
    623 				/* released Alt, which was treated as FN-Alt */
    624 				key = 0x00e6 | RELEASE;
    625 				sc->sc_flags &= ~FLAG_FN_ALT;
    626 			}
    627 		}
    628 	}
    629 	return key;
    630 }
    631 
    632 void
    633 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
    634 {
    635 	struct ukbd_softc *sc = (struct ukbd_softc *)addr;
    636 	struct ukbd_data *ud = &sc->sc_ndata;
    637 	int i;
    638 
    639 #ifdef UKBD_DEBUG
    640 	if (ukbddebug > 5) {
    641 		printf("ukbd_intr: data");
    642 		for (i = 0; i < len; i++)
    643 			printf(" 0x%02x", ((u_char *)ibuf)[i]);
    644 		printf("\n");
    645 	}
    646 #endif
    647 
    648 	ud->modifiers = 0;
    649 	for (i = 0; i < sc->sc_nmod; i++)
    650 		if (hid_get_data(ibuf, &sc->sc_modloc[i]))
    651 			ud->modifiers |= sc->sc_mods[i].mask;
    652 	memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8,
    653 	       sc->sc_nkeycode);
    654 
    655 	if (sc->sc_flags & FLAG_APPLE_FN) {
    656 		if (hid_get_data(ibuf, &sc->sc_apple_fn)) {
    657 			sc->sc_flags |= FLAG_FN_PRESSED;
    658 			ukbd_translate_keycodes(sc, ud, trtab_apple_fn);
    659 		}
    660 		else
    661 			sc->sc_flags &= ~FLAG_FN_PRESSED;
    662 	}
    663 
    664 #ifdef GDIUM_KEYBOARD_HACK
    665 	if (sc->sc_flags & FLAG_GDIUM_FN) {
    666 		if (sc->sc_flags & FLAG_FN_PRESSED) {
    667 			ukbd_translate_keycodes(sc, ud, trtab_gdium_fn);
    668 		}
    669 	}
    670 #endif
    671 
    672 	ukbd_translate_keycodes(sc, ud, trtab_generic);
    673 
    674 	if ((sc->sc_flags & FLAG_DEBOUNCE) && !(sc->sc_flags & FLAG_POLLING)) {
    675 		/*
    676 		 * Some keyboards have a peculiar quirk.  They sometimes
    677 		 * generate a key up followed by a key down for the same
    678 		 * key after about 10 ms.
    679 		 * We avoid this bug by holding off decoding for 20 ms.
    680 		 */
    681 		sc->sc_data = *ud;
    682 		callout_reset(&sc->sc_delay, hz / 50, ukbd_delayed_decode, sc);
    683 #ifdef DDB
    684 	} else if (sc->sc_console_keyboard && !(sc->sc_flags & FLAG_POLLING)) {
    685 		/*
    686 		 * For the console keyboard we can't deliver CTL-ALT-ESC
    687 		 * from the interrupt routine.  Doing so would start
    688 		 * polling from inside the interrupt routine and that
    689 		 * loses bigtime.
    690 		 */
    691 		sc->sc_data = *ud;
    692 		callout_reset(&sc->sc_delay, 1, ukbd_delayed_decode, sc);
    693 #endif
    694 	} else {
    695 		ukbd_decode(sc, ud);
    696 	}
    697 }
    698 
    699 void
    700 ukbd_delayed_decode(void *addr)
    701 {
    702 	struct ukbd_softc *sc = addr;
    703 
    704 	ukbd_decode(sc, &sc->sc_data);
    705 }
    706 
    707 void
    708 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
    709 {
    710 	int mod, omod;
    711 	u_int16_t ibuf[MAXKEYS];	/* chars events */
    712 	int s;
    713 	int nkeys, i, j;
    714 	int key;
    715 #define ADDKEY(c) ibuf[nkeys++] = (c)
    716 
    717 #ifdef UKBD_DEBUG
    718 	/*
    719 	 * Keep a trace of the last events.  Using printf changes the
    720 	 * timing, so this can be useful sometimes.
    721 	 */
    722 	if (ukbdtrace) {
    723 		struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
    724 		p->unit = device_unit(sc->sc_hdev.sc_dev);
    725 		microtime(&p->tv);
    726 		p->ud = *ud;
    727 		if (++ukbdtraceindex >= UKBDTRACESIZE)
    728 			ukbdtraceindex = 0;
    729 	}
    730 	if (ukbddebug > 5) {
    731 		struct timeval tv;
    732 		microtime(&tv);
    733 		DPRINTF((" at %"PRIu64".%06"PRIu64"  mod=0x%02x key0=0x%02x key1=0x%02x "
    734 			 "key2=0x%02x key3=0x%02x\n",
    735 			 tv.tv_sec, (uint64_t)tv.tv_usec,
    736 			 ud->modifiers, ud->keycode[0], ud->keycode[1],
    737 			 ud->keycode[2], ud->keycode[3]));
    738 	}
    739 #endif
    740 
    741 	if (ud->keycode[0] == KEY_ERROR) {
    742 		DPRINTF(("ukbd_intr: KEY_ERROR\n"));
    743 		return;		/* ignore  */
    744 	}
    745 
    746 	if (sc->sc_flags & FLAG_APPLE_FIX_ISO)
    747 		ukbd_translate_keycodes(sc, ud, trtab_apple_iso);
    748 
    749 	nkeys = 0;
    750 	mod = ud->modifiers;
    751 	omod = sc->sc_odata.modifiers;
    752 	if (mod != omod)
    753 		for (i = 0; i < sc->sc_nmod; i++)
    754 			if (( mod & sc->sc_mods[i].mask) !=
    755 			    (omod & sc->sc_mods[i].mask)) {
    756 				key = sc->sc_mods[i].key |
    757 				    ((mod & sc->sc_mods[i].mask) ?
    758 				    PRESS : RELEASE);
    759 				ADDKEY(ukbd_translate_modifier(sc, key));
    760 			}
    761 	if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
    762 		/* Check for released keys. */
    763 		for (i = 0; i < sc->sc_nkeycode; i++) {
    764 			key = sc->sc_odata.keycode[i];
    765 			if (key == 0)
    766 				continue;
    767 			for (j = 0; j < sc->sc_nkeycode; j++)
    768 				if (key == ud->keycode[j])
    769 					goto rfound;
    770 			DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
    771 #ifdef GDIUM_KEYBOARD_HACK
    772 			if (sc->sc_flags & FLAG_GDIUM_FN) {
    773 				if (key == 0x82) {
    774 					sc->sc_flags &= ~FLAG_FN_PRESSED;
    775 					goto rfound;
    776 				}
    777 			}
    778 #endif
    779 			ADDKEY(key | RELEASE);
    780 		rfound:
    781 			;
    782 		}
    783 
    784 		/* Check for pressed keys. */
    785 		for (i = 0; i < sc->sc_nkeycode; i++) {
    786 			key = ud->keycode[i];
    787 			if (key == 0)
    788 				continue;
    789 			for (j = 0; j < sc->sc_nkeycode; j++)
    790 				if (key == sc->sc_odata.keycode[j])
    791 					goto pfound;
    792 			DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
    793 #ifdef GDIUM_KEYBOARD_HACK
    794 			if (sc->sc_flags & FLAG_GDIUM_FN) {
    795 				if (key == 0x82) {
    796 					sc->sc_flags |= FLAG_FN_PRESSED;
    797 					goto pfound;
    798 				}
    799 			}
    800 #endif
    801 			ADDKEY(key | PRESS);
    802 		pfound:
    803 			;
    804 		}
    805 	}
    806 	sc->sc_odata = *ud;
    807 
    808 	if (nkeys == 0)
    809 		return;
    810 
    811 	if (sc->sc_flags & FLAG_POLLING) {
    812 		DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
    813 		memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
    814 		sc->sc_npollchar = nkeys;
    815 		return;
    816 	}
    817 #ifdef WSDISPLAY_COMPAT_RAWKBD
    818 	if (sc->sc_rawkbd) {
    819 		u_char cbuf[MAXKEYS * 2];
    820 		int c;
    821 		int npress;
    822 
    823 		for (npress = i = j = 0; i < nkeys; i++) {
    824 			key = ibuf[i];
    825 			c = ukbd_trtab[key & CODEMASK];
    826 			if (c == NN)
    827 				continue;
    828 			if (c == 0x7f) {
    829 				/* pause key */
    830 				cbuf[j++] = 0xe1;
    831 				cbuf[j++] = 0x1d;
    832 				cbuf[j-1] |= (key & RELEASE) ? 0x80 : 0;
    833 				cbuf[j] = 0x45;
    834 			} else {
    835 				if (c & 0x80)
    836 					cbuf[j++] = 0xe0;
    837 				cbuf[j] = c & 0x7f;
    838 			}
    839 			if (key & RELEASE)
    840 				cbuf[j] |= 0x80;
    841 #if defined(UKBD_REPEAT)
    842 			else {
    843 				/* remember pressed keys for autorepeat */
    844 				if (c & 0x80)
    845 					sc->sc_rep[npress++] = 0xe0;
    846 				sc->sc_rep[npress++] = c & 0x7f;
    847 			}
    848 #endif
    849 			DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
    850 				    c & 0x80 ? "0xe0 " : "",
    851 				    cbuf[j]));
    852 			j++;
    853 		}
    854 		s = spltty();
    855 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    856 		splx(s);
    857 #ifdef UKBD_REPEAT
    858 		callout_stop(&sc->sc_rawrepeat_ch);
    859 		if (npress != 0) {
    860 			sc->sc_nrep = npress;
    861 			callout_reset(&sc->sc_rawrepeat_ch,
    862 			    hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
    863 		}
    864 #endif
    865 		return;
    866 	}
    867 #endif
    868 
    869 	s = spltty();
    870 	for (i = 0; i < nkeys; i++) {
    871 		key = ibuf[i];
    872 		wskbd_input(sc->sc_wskbddev,
    873 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
    874 		    key&CODEMASK);
    875 	}
    876 	splx(s);
    877 }
    878 
    879 void
    880 ukbd_set_leds(void *v, int leds)
    881 {
    882 	struct ukbd_softc *sc = v;
    883 	u_int8_t res;
    884 
    885 	DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n",
    886 		 sc, leds, sc->sc_leds));
    887 
    888 	if (sc->sc_dying)
    889 		return;
    890 
    891 	if (sc->sc_leds == leds)
    892 		return;
    893 	sc->sc_leds = leds;
    894 	res = 0;
    895 	/* XXX not really right */
    896 	if ((leds & WSKBD_LED_COMPOSE) && sc->sc_compose.size == 1)
    897 		res |= 1 << sc->sc_compose.pos;
    898 	if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
    899 		res |= 1 << sc->sc_scroloc.pos;
    900 	if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
    901 		res |= 1 << sc->sc_numloc.pos;
    902 	if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
    903 		res |= 1 << sc->sc_capsloc.pos;
    904 	uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1);
    905 }
    906 
    907 #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
    908 void
    909 ukbd_rawrepeat(void *v)
    910 {
    911 	struct ukbd_softc *sc = v;
    912 	int s;
    913 
    914 	s = spltty();
    915 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
    916 	splx(s);
    917 	callout_reset(&sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
    918 	    ukbd_rawrepeat, sc);
    919 }
    920 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) */
    921 
    922 int
    923 ukbd_ioctl(void *v, u_long cmd, void *data, int flag,
    924     struct lwp *l)
    925 {
    926 	struct ukbd_softc *sc = v;
    927 
    928 	switch (cmd) {
    929 	case WSKBDIO_GTYPE:
    930 		*(int *)data = WSKBD_TYPE_USB;
    931 		return (0);
    932 	case WSKBDIO_SETLEDS:
    933 		ukbd_set_leds(v, *(int *)data);
    934 		return (0);
    935 	case WSKBDIO_GETLEDS:
    936 		*(int *)data = sc->sc_leds;
    937 		return (0);
    938 #if defined(WSDISPLAY_COMPAT_RAWKBD)
    939 	case WSKBDIO_SETMODE:
    940 		DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
    941 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    942 #if defined(UKBD_REPEAT)
    943 		callout_stop(&sc->sc_rawrepeat_ch);
    944 #endif
    945 		return (0);
    946 #endif
    947 	}
    948 	return (EPASSTHROUGH);
    949 }
    950 
    951 /*
    952  * This is a hack to work around some broken ports that don't call
    953  * cnpollc() before cngetc().
    954  */
    955 static int pollenter, warned;
    956 
    957 /* Console interface. */
    958 void
    959 ukbd_cngetc(void *v, u_int *type, int *data)
    960 {
    961 	struct ukbd_softc *sc = v;
    962 	int c;
    963 	int broken;
    964 
    965 	if (pollenter == 0) {
    966 		if (!warned) {
    967 			printf("\n"
    968 "This port is broken, it does not call cnpollc() before calling cngetc().\n"
    969 "This should be fixed, but it will work anyway (for now).\n");
    970 			warned = 1;
    971 		}
    972 		broken = 1;
    973 		ukbd_cnpollc(v, 1);
    974 	} else
    975 		broken = 0;
    976 
    977 	DPRINTFN(0,("ukbd_cngetc: enter\n"));
    978 	sc->sc_flags |= FLAG_POLLING;
    979 	while(sc->sc_npollchar <= 0)
    980 		usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface);
    981 	sc->sc_flags &= ~FLAG_POLLING;
    982 	c = sc->sc_pollchars[0];
    983 	sc->sc_npollchar--;
    984 	memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
    985 	       sc->sc_npollchar * sizeof(u_int16_t));
    986 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    987 	*data = c & CODEMASK;
    988 	DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
    989 	if (broken)
    990 		ukbd_cnpollc(v, 0);
    991 }
    992 
    993 void
    994 ukbd_cnpollc(void *v, int on)
    995 {
    996 	struct ukbd_softc *sc = v;
    997 	usbd_device_handle dev;
    998 
    999 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
   1000 
   1001 	usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev);
   1002 	if (on) {
   1003 		sc->sc_spl = splusb();
   1004 		pollenter++;
   1005 	} else {
   1006 		splx(sc->sc_spl);
   1007 		pollenter--;
   1008 	}
   1009 	usbd_set_polling(dev, on);
   1010 }
   1011 
   1012 int
   1013 ukbd_cnattach(void)
   1014 {
   1015 
   1016 	/*
   1017 	 * XXX USB requires too many parts of the kernel to be running
   1018 	 * XXX in order to work, so we can't do much for the console
   1019 	 * XXX keyboard until autconfiguration has run its course.
   1020 	 */
   1021 	ukbd_is_console = 1;
   1022 	return (0);
   1023 }
   1024 
   1025 const char *
   1026 ukbd_parse_desc(struct ukbd_softc *sc)
   1027 {
   1028 	struct hid_data *d;
   1029 	struct hid_item h;
   1030 	int size;
   1031 	void *desc;
   1032 	int imod;
   1033 
   1034 	uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
   1035 	imod = 0;
   1036 	sc->sc_nkeycode = 0;
   1037 	d = hid_start_parse(desc, size, hid_input);
   1038 	while (hid_get_item(d, &h)) {
   1039 		/*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n",
   1040 		  h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/
   1041 
   1042 		/* Check for special Apple notebook FN key */
   1043 		if (HID_GET_USAGE_PAGE(h.usage) == 0x00ff &&
   1044 		    HID_GET_USAGE(h.usage) == 0x0003 &&
   1045 		    h.kind == hid_input && (h.flags & HIO_VARIABLE)) {
   1046 			sc->sc_flags |= FLAG_APPLE_FN;
   1047 			sc->sc_apple_fn = h.loc;
   1048 		}
   1049 
   1050 		if (h.kind != hid_input || (h.flags & HIO_CONST) ||
   1051 		    HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
   1052 		    h.report_ID != sc->sc_hdev.sc_report_id)
   1053 			continue;
   1054 		DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d "
   1055 			 "cnt=%d\n", imod,
   1056 			 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
   1057 		if (h.flags & HIO_VARIABLE) {
   1058 			if (h.loc.size != 1)
   1059 				return ("bad modifier size");
   1060 			/* Single item */
   1061 			if (imod < MAXMOD) {
   1062 				sc->sc_modloc[imod] = h.loc;
   1063 				sc->sc_mods[imod].mask = 1 << imod;
   1064 				sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
   1065 				imod++;
   1066 			} else
   1067 				return ("too many modifier keys");
   1068 		} else {
   1069 			/* Array */
   1070 			if (h.loc.size != 8)
   1071 				return ("key code size != 8");
   1072 			if (h.loc.count > MAXKEYCODE)
   1073 				h.loc.count = MAXKEYCODE;
   1074 			if (h.loc.pos % 8 != 0)
   1075 				return ("key codes not on byte boundary");
   1076 			if (sc->sc_nkeycode != 0)
   1077 				return ("multiple key code arrays\n");
   1078 			sc->sc_keycodeloc = h.loc;
   1079 			sc->sc_nkeycode = h.loc.count;
   1080 		}
   1081 	}
   1082 	sc->sc_nmod = imod;
   1083 	hid_end_parse(d);
   1084 
   1085 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
   1086 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL);
   1087 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
   1088 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL);
   1089 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
   1090 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL);
   1091 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_COMPOSE),
   1092 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_compose, NULL);
   1093 
   1094 	return (NULL);
   1095 }
   1096