xref: /openbsd/sys/dev/hid/hidkbd.c (revision 3cab2bb3)
1 /*	$OpenBSD: hidkbd.c,v 1.5 2017/05/30 07:40:24 mpi Exp $	*/
2 /*      $NetBSD: ukbd.c,v 1.85 2003/03/11 16:44:00 augustss Exp $        */
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/timeout.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/ioctl.h>
44 #include <sys/malloc.h>
45 
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wskbdvar.h>
48 #include <dev/wscons/wsksymdef.h>
49 #include <dev/wscons/wsksymvar.h>
50 
51 #include <dev/hid/hid.h>
52 #include <dev/hid/hidkbdsc.h>
53 
54 #ifdef HIDKBD_DEBUG
55 #define DPRINTF(x)	do { if (hidkbddebug) printf x; } while (0)
56 #define DPRINTFN(n,x)	do { if (hidkbddebug>(n)) printf x; } while (0)
57 int	hidkbddebug = 0;
58 #else
59 #define DPRINTF(x)
60 #define DPRINTFN(n,x)
61 #endif
62 
63 #define PRESS    0x000
64 #define RELEASE  0x100
65 #define CODEMASK 0x0ff
66 
67 #if defined(WSDISPLAY_COMPAT_RAWKBD)
68 #define NN 0			/* no translation */
69 /*
70  * Translate USB keycodes to US keyboard XT scancodes.
71  * Scancodes >= 0x80 represent EXTENDED keycodes.
72  *
73  * See http://www.microsoft.com/whdc/archive/Scancode.mspx
74  */
75 const u_int8_t hidkbd_trtab[256] = {
76       NN,   NN,   NN,   NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
77     0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
78     0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
79     0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
80     0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
81     0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
82     0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
83     0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
84     0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */
85     0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
86     0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
87     0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
88     0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, 0xde, 0x59, /* 60 - 67 */
89     0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, /* 68 - 6f */
90     0x6c, 0x6d, 0x6e, 0x76, 0x97,   NN, 0x93, 0x95, /* 70 - 77 */
91     0x91, 0x92, 0x94, 0x9a, 0x96, 0x98, 0x99, 0xa0, /* 78 - 7f */
92     0xb0, 0xae,   NN,   NN,   NN, 0x7e,   NN, 0x73, /* 80 - 87 */
93     0x70, 0x7d, 0x79, 0x7b, 0x5c,   NN,   NN,   NN, /* 88 - 8f */
94       NN,   NN, 0x78, 0x77, 0x76,   NN,   NN,   NN, /* 90 - 97 */
95       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 98 - 9f */
96       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a0 - a7 */
97       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a8 - af */
98       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b0 - b7 */
99       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b8 - bf */
100       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c0 - c7 */
101       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c8 - cf */
102       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d0 - d7 */
103       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d8 - df */
104     0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
105       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* e8 - ef */
106       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f0 - f7 */
107       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f8 - ff */
108 };
109 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
110 
111 #define KEY_ERROR 0x01
112 
113 #ifdef HIDKBD_DEBUG
114 #define HIDKBDTRACESIZE 64
115 struct hidkbdtraceinfo {
116 	int unit;
117 	struct timeval tv;
118 	struct hidkbd_data ud;
119 };
120 struct hidkbdtraceinfo hidkbdtracedata[HIDKBDTRACESIZE];
121 int hidkbdtraceindex = 0;
122 int hidkbdtrace = 0;
123 void hidkbdtracedump(void);
124 void
125 hidkbdtracedump(void)
126 {
127 	int i;
128 	for (i = 0; i < HIDKBDTRACESIZE; i++) {
129 		struct hidkbdtraceinfo *p =
130 		    &hidkbdtracedata[(i+hidkbdtraceindex)%HIDKBDTRACESIZE];
131 		printf("%lld.%06ld: key0=0x%02x key1=0x%02x "
132 		       "key2=0x%02x key3=0x%02x\n",
133 		       (long long)p->tv.tv_sec, p->tv.tv_usec,
134 		       p->ud.keycode[0], p->ud.keycode[1],
135 		       p->ud.keycode[2], p->ud.keycode[3]);
136 	}
137 }
138 #endif
139 
140 int	hidkbd_is_console;
141 
142 const char *hidkbd_parse_desc(struct hidkbd *, int, void *, int);
143 
144 void	(*hidkbd_bell_fn)(void *, u_int, u_int, u_int, int);
145 void	*hidkbd_bell_fn_arg;
146 
147 void	hidkbd_decode(struct hidkbd *, struct hidkbd_data *);
148 void	hidkbd_delayed_decode(void *addr);
149 
150 extern const struct wscons_keydesc ukbd_keydesctab[];
151 
152 struct wskbd_mapdata ukbd_keymapdata = {
153 	ukbd_keydesctab
154 };
155 
156 int
157 hidkbd_attach(struct device *self, struct hidkbd *kbd, int console,
158     uint32_t qflags, int id, void *desc, int dlen)
159 {
160 	const char *parserr;
161 
162 	kbd->sc_var = NULL;
163 
164 	parserr = hidkbd_parse_desc(kbd, id, desc, dlen);
165 	if (parserr != NULL) {
166 		printf(": %s\n", parserr);
167 		return ENXIO;
168 	}
169 
170 #ifdef DIAGNOSTIC
171 	printf(": %d variable keys, %d key codes",
172 	    kbd->sc_nvar, kbd->sc_nkeycode);
173 #endif
174 
175 	kbd->sc_device = self;
176 	kbd->sc_debounce = (qflags & HIDKBD_SPUR_BUT_UP) != 0;
177 
178 	/*
179 	 * Remember if we're the console keyboard.
180 	 *
181 	 * XXX This always picks the first (USB) keyboard to attach,
182 	 * but what else can we really do?
183 	 */
184 	if (console) {
185 		kbd->sc_console_keyboard = hidkbd_is_console;
186 		/* Don't let any other keyboard have it. */
187 		hidkbd_is_console = 0;
188 	}
189 
190 	timeout_set(&kbd->sc_delay, hidkbd_delayed_decode, kbd);
191 
192 	return 0;
193 }
194 
195 void
196 hidkbd_attach_wskbd(struct hidkbd *kbd, kbd_t layout,
197     const struct wskbd_accessops *accessops)
198 {
199 	struct wskbddev_attach_args a;
200 
201 	ukbd_keymapdata.layout = layout;
202 
203 	a.console = kbd->sc_console_keyboard;
204 	a.keymap = &ukbd_keymapdata;
205 	a.accessops = accessops;
206 	a.accesscookie = kbd->sc_device;
207 	kbd->sc_wskbddev = config_found(kbd->sc_device, &a, wskbddevprint);
208 }
209 
210 int
211 hidkbd_detach(struct hidkbd *kbd, int flags)
212 {
213 	int rv = 0;
214 
215 	DPRINTF(("hidkbd_detach: sc=%p flags=%d\n", kbd->sc_device, flags));
216 
217 	if (kbd->sc_console_keyboard) {
218 		/*
219 		 * Disconnect our consops and set hidkbd_is_console
220 		 * back to 1 so that the next USB keyboard attached
221 		 * to the system will get it.
222 		 * XXX Should notify some other keyboard that it can be
223 		 * XXX console, if there are any other keyboards.
224 		 */
225 		printf("%s: was console keyboard\n",
226 		       kbd->sc_device->dv_xname);
227 		hidkbd_is_console = 1;
228 	}
229 	/* No need to do reference counting of hidkbd, wskbd has all the goo */
230 	if (kbd->sc_wskbddev != NULL)
231 		rv = config_detach(kbd->sc_wskbddev, flags);
232 
233 	if (kbd->sc_var != NULL)
234 		free(kbd->sc_var, M_DEVBUF, 0);
235 
236 	return (rv);
237 }
238 
239 void
240 hidkbd_input(struct hidkbd *kbd, uint8_t *data, u_int len)
241 {
242 	struct hidkbd_data *ud = &kbd->sc_ndata;
243 	int i;
244 
245 #ifdef HIDKBD_DEBUG
246 	if (hidkbddebug > 5) {
247 		printf("hidkbd_input: data");
248 		for (i = 0; i < len; i++)
249 			printf(" 0x%02x", data[i]);
250 		printf("\n");
251 	}
252 #endif
253 
254 	/* extract variable keys */
255 	for (i = 0; i < kbd->sc_nvar; i++)
256 		ud->var[i] = (u_int8_t)hid_get_data(data, len,
257 		    &kbd->sc_var[i].loc);
258 
259 	/* extract keycodes */
260 	memcpy(ud->keycode, data + kbd->sc_keycodeloc.pos / 8,
261 	    kbd->sc_nkeycode);
262 
263 	if (kbd->sc_debounce && !kbd->sc_polling) {
264 		/*
265 		 * Some keyboards have a peculiar quirk.  They sometimes
266 		 * generate a key up followed by a key down for the same
267 		 * key after about 10 ms.
268 		 * We avoid this bug by holding off decoding for 20 ms.
269 		 */
270 		kbd->sc_data = *ud;
271 		timeout_add_msec(&kbd->sc_delay, 20);
272 	} else {
273 		hidkbd_decode(kbd, ud);
274 	}
275 }
276 
277 void
278 hidkbd_delayed_decode(void *addr)
279 {
280 	struct hidkbd *kbd = addr;
281 
282 	hidkbd_decode(kbd, &kbd->sc_data);
283 }
284 
285 void
286 hidkbd_decode(struct hidkbd *kbd, struct hidkbd_data *ud)
287 {
288 	u_int16_t ibuf[MAXKEYS];	/* chars events */
289 	int s;
290 	int nkeys, i, j;
291 	int key;
292 #define ADDKEY(c) ibuf[nkeys++] = (c)
293 
294 #ifdef HIDKBD_DEBUG
295 	/*
296 	 * Keep a trace of the last events.  Using printf changes the
297 	 * timing, so this can be useful sometimes.
298 	 */
299 	if (hidkbdtrace) {
300 		struct hidkbdtraceinfo *p = &hidkbdtracedata[hidkbdtraceindex];
301 		p->unit = kbd->sc_device->dv_unit;
302 		microtime(&p->tv);
303 		p->ud = *ud;
304 		if (++hidkbdtraceindex >= HIDKBDTRACESIZE)
305 			hidkbdtraceindex = 0;
306 	}
307 	if (hidkbddebug > 5) {
308 		struct timeval tv;
309 		microtime(&tv);
310 		DPRINTF((" at %lld.%06ld key0=0x%02x key1=0x%02x "
311 			 "key2=0x%02x key3=0x%02x\n",
312 			 (long long)tv.tv_sec, tv.tv_usec,
313 			 ud->keycode[0], ud->keycode[1],
314 			 ud->keycode[2], ud->keycode[3]));
315 	}
316 #endif
317 
318 	if (ud->keycode[0] == KEY_ERROR) {
319 		DPRINTF(("hidkbd_input: KEY_ERROR\n"));
320 		return;		/* ignore  */
321 	}
322 	nkeys = 0;
323 
324 	for (i = 0; i < kbd->sc_nvar; i++)
325 		if ((kbd->sc_odata.var[i] & kbd->sc_var[i].mask) !=
326 		    (ud->var[i] & kbd->sc_var[i].mask)) {
327 			ADDKEY(kbd->sc_var[i].key |
328 			    ((ud->var[i] & kbd->sc_var[i].mask) ?
329 			    PRESS : RELEASE));
330 		}
331 
332 	if (memcmp(ud->keycode, kbd->sc_odata.keycode, kbd->sc_nkeycode) != 0) {
333 		/* Check for released keys. */
334 		for (i = 0; i < kbd->sc_nkeycode; i++) {
335 			key = kbd->sc_odata.keycode[i];
336 			if (key == 0)
337 				continue;
338 			for (j = 0; j < kbd->sc_nkeycode; j++)
339 				if (key == ud->keycode[j])
340 					goto rfound;
341 			DPRINTFN(3,("hidkbd_decode: relse key=0x%02x\n", key));
342 			ADDKEY(key | RELEASE);
343 		rfound:
344 			;
345 		}
346 
347 		/* Check for pressed keys. */
348 		for (i = 0; i < kbd->sc_nkeycode; i++) {
349 			key = ud->keycode[i];
350 			if (key == 0)
351 				continue;
352 			for (j = 0; j < kbd->sc_nkeycode; j++)
353 				if (key == kbd->sc_odata.keycode[j])
354 					goto pfound;
355 			DPRINTFN(2,("hidkbd_decode: press key=0x%02x\n", key));
356 			ADDKEY(key | PRESS);
357 		pfound:
358 			;
359 		}
360 	}
361 	kbd->sc_odata = *ud;
362 
363 	if (nkeys == 0)
364 		return;
365 
366 	if (kbd->sc_polling) {
367 		DPRINTFN(1,("hidkbd_decode: pollchar = 0x%03x\n", ibuf[0]));
368 		memcpy(kbd->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
369 		kbd->sc_npollchar = nkeys;
370 		return;
371 	}
372 
373 	if (kbd->sc_wskbddev == NULL)
374 		return;
375 
376 #ifdef WSDISPLAY_COMPAT_RAWKBD
377 	if (kbd->sc_rawkbd) {
378 		u_char cbuf[MAXKEYS * 2];
379 		int c;
380 
381 		for (i = j = 0; i < nkeys; i++) {
382 			key = ibuf[i];
383 			c = hidkbd_trtab[key & CODEMASK];
384 			if (c == NN)
385 				continue;
386 			if (c & 0x80)
387 				cbuf[j++] = 0xe0;
388 			cbuf[j] = c & 0x7f;
389 			if (key & RELEASE)
390 				cbuf[j] |= 0x80;
391 			DPRINTFN(1,("hidkbd_decode: raw = %s0x%02x\n",
392 				    c & 0x80 ? "0xe0 " : "",
393 				    cbuf[j]));
394 			j++;
395 		}
396 		s = spltty();
397 		wskbd_rawinput(kbd->sc_wskbddev, cbuf, j);
398 
399 		/*
400 		 * Pass audio keys to wskbd_input anyway.
401 		 */
402 		for (i = 0; i < nkeys; i++) {
403 			key = ibuf[i];
404 			switch (key & CODEMASK) {
405 			case 127:
406 			case 128:
407 			case 129:
408 				wskbd_input(kbd->sc_wskbddev,
409 				    key & RELEASE ?  WSCONS_EVENT_KEY_UP :
410 				      WSCONS_EVENT_KEY_DOWN, key & CODEMASK);
411 				break;
412 			}
413 		}
414 		splx(s);
415 
416 		return;
417 	}
418 #endif
419 
420 	s = spltty();
421 	for (i = 0; i < nkeys; i++) {
422 		key = ibuf[i];
423 		wskbd_input(kbd->sc_wskbddev,
424 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
425 		    key&CODEMASK);
426 	}
427 	splx(s);
428 #undef	ADDKEY
429 }
430 
431 int
432 hidkbd_enable(struct hidkbd *kbd, int on)
433 {
434 	if (kbd->sc_enabled == on)
435 		return EBUSY;
436 
437 	kbd->sc_enabled = on;
438 	return 0;
439 }
440 
441 int
442 hidkbd_set_leds(struct hidkbd *kbd, int leds, uint8_t *report)
443 {
444 	if (kbd->sc_leds == leds)
445 		return 0;
446 
447 	kbd->sc_leds = leds;
448 
449 	/*
450 	 * This is not totally correct, since we did not check the
451 	 * report size from the descriptor but for keyboards it should
452 	 * just be a single byte with the relevant bits set.
453 	 */
454 	*report = 0;
455 	if ((leds & WSKBD_LED_SCROLL) && kbd->sc_scroloc.size == 1)
456 		*report |= 1 << kbd->sc_scroloc.pos;
457 	if ((leds & WSKBD_LED_NUM) && kbd->sc_numloc.size == 1)
458 		*report |= 1 << kbd->sc_numloc.pos;
459 	if ((leds & WSKBD_LED_CAPS) && kbd->sc_capsloc.size == 1)
460 		*report |= 1 << kbd->sc_capsloc.pos;
461 	if ((leds & WSKBD_LED_COMPOSE) && kbd->sc_compose.size == 1)
462 		*report |= 1 << kbd->sc_compose.pos;
463 
464 	return 1;
465 }
466 
467 int
468 hidkbd_ioctl(struct hidkbd *kbd, u_long cmd, caddr_t data, int flag,
469    struct proc *p)
470 {
471 	switch (cmd) {
472 	case WSKBDIO_GETLEDS:
473 		*(int *)data = kbd->sc_leds;
474 		return (0);
475 	case WSKBDIO_COMPLEXBELL:
476 #define d ((struct wskbd_bell_data *)data)
477 		hidkbd_bell(d->pitch, d->period, d->volume, 0);
478 #undef d
479 		return (0);
480 #ifdef WSDISPLAY_COMPAT_RAWKBD
481 	case WSKBDIO_SETMODE:
482 		DPRINTF(("hidkbd_ioctl: set raw = %d\n", *(int *)data));
483 		kbd->sc_rawkbd = *(int *)data == WSKBD_RAW;
484 		return (0);
485 #endif
486 	}
487 	return (-1);
488 }
489 
490 void
491 hidkbd_cngetc(struct hidkbd *kbd, u_int *type, int *data)
492 {
493 	int c;
494 
495 	c = kbd->sc_pollchars[0];
496 	kbd->sc_npollchar--;
497 	memmove(kbd->sc_pollchars, kbd->sc_pollchars+1,
498 	       kbd->sc_npollchar * sizeof(u_int16_t));
499 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
500 	*data = c & CODEMASK;
501 }
502 
503 void
504 hidkbd_bell(u_int pitch, u_int period, u_int volume, int poll)
505 {
506 	if (hidkbd_bell_fn != NULL)
507 		(*hidkbd_bell_fn)(hidkbd_bell_fn_arg, pitch, period,
508 		    volume, poll);
509 }
510 
511 void
512 hidkbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
513 {
514 	if (hidkbd_bell_fn == NULL) {
515 		hidkbd_bell_fn = fn;
516 		hidkbd_bell_fn_arg = arg;
517 	}
518 }
519 
520 const char *
521 hidkbd_parse_desc(struct hidkbd *kbd, int id, void *desc, int dlen)
522 {
523 	struct hid_data *d;
524 	struct hid_item h;
525 	unsigned int i, ivar = 0;
526 
527 	kbd->sc_nkeycode = 0;
528 
529 	d = hid_start_parse(desc, dlen, hid_input);
530 	while (hid_get_item(d, &h)) {
531 		if (h.kind != hid_input || (h.flags & HIO_CONST) ||
532 		    HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
533 		    h.report_ID != id)
534 			continue;
535 		if (h.flags & HIO_VARIABLE)
536 			ivar++;
537 	}
538 	hid_end_parse(d);
539 
540 	if (ivar > MAXVARS) {
541 		DPRINTF((": too many variable keys\n"));
542 		ivar = MAXVARS;
543 	}
544 
545 	kbd->sc_nvar = ivar;
546 	kbd->sc_var = (struct hidkbd_variable *)mallocarray(ivar,
547 			sizeof(struct hidkbd_variable), M_DEVBUF, M_NOWAIT);
548 
549 	if (!kbd->sc_var)
550 		return NULL;
551 
552 	i = 0;
553 
554 	d = hid_start_parse(desc, dlen, hid_input);
555 	while (hid_get_item(d, &h)) {
556 		if (h.kind != hid_input || (h.flags & HIO_CONST) ||
557 		    HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
558 		    h.report_ID != id)
559 			continue;
560 
561 		DPRINTF(("hidkbd: usage=0x%x flags=0x%x pos=%d size=%d cnt=%d",
562 			 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
563 		if (h.flags & HIO_VARIABLE) {
564 			/* variable reports should be one bit each */
565 			if (h.loc.size != 1) {
566 				DPRINTF((": bad variable size\n"));
567 				continue;
568 			}
569 
570 			/* variable report */
571 			if (i < MAXVARS) {
572 				kbd->sc_var[i].loc = h.loc;
573 				kbd->sc_var[i].mask = 1 << (i % 8);
574 				kbd->sc_var[i].key = HID_GET_USAGE(h.usage);
575 				i++;
576 			}
577 		} else {
578 			/* keys array should be in bytes, on a byte boundary */
579 			if (h.loc.size != 8) {
580 				DPRINTF((": key code size != 8\n"));
581 				continue;
582 			}
583 			if (h.loc.pos % 8 != 0) {
584 				DPRINTF((": array not on byte boundary"));
585 				continue;
586 			}
587 			if (kbd->sc_nkeycode != 0) {
588 				DPRINTF((": ignoring multiple arrays\n"));
589 				continue;
590 			}
591 			kbd->sc_keycodeloc = h.loc;
592 			if (h.loc.count > MAXKEYCODE) {
593 				DPRINTF((": ignoring extra key codes"));
594 				kbd->sc_nkeycode = MAXKEYCODE;
595 			} else
596 				kbd->sc_nkeycode = h.loc.count;
597 		}
598 		DPRINTF(("\n"));
599 	}
600 	hid_end_parse(d);
601 
602 	/* don't attach if no keys... */
603 	if (kbd->sc_nkeycode == 0 && ivar == 0)
604 		return "no usable key codes array";
605 
606 	hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_NUM_LOCK),
607 	    id, hid_output, &kbd->sc_numloc, NULL);
608 	hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_CAPS_LOCK),
609 	    id, hid_output, &kbd->sc_capsloc, NULL);
610 	hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_SCROLL_LOCK),
611 	    id, hid_output, &kbd->sc_scroloc, NULL);
612 	hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_COMPOSE),
613 	    id, hid_output, &kbd->sc_compose, NULL);
614 
615 	return (NULL);
616 }
617