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