xref: /openbsd-src/sys/dev/hid/hidkbd.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: hidkbd.c,v 1.1 2016/01/08 15:54:13 jcs 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: mod=0x%02x 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.modifiers, 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 #if 0
219 		/*
220 		 * XXX Should probably disconnect our consops,
221 		 * XXX and either notify some other keyboard that
222 		 * XXX it can now be the console, or if there aren't
223 		 * XXX any more USB keyboards, set hidkbd_is_console
224 		 * XXX back to 1 so that the next USB keyboard attached
225 		 * XXX to the system will get it.
226 		 */
227 		panic("hidkbd_detach: console keyboard");
228 #else
229 		/*
230 		 * Disconnect our consops and set hidkbd_is_console
231 		 * back to 1 so that the next USB keyboard attached
232 		 * to the system will get it.
233 		 * XXX Should notify some other keyboard that it can be
234 		 * XXX console, if there are any other keyboards.
235 		 */
236 		printf("%s: was console keyboard\n",
237 		       kbd->sc_device->dv_xname);
238 		wskbd_cndetach();
239 		hidkbd_is_console = 1;
240 #endif
241 	}
242 	/* No need to do reference counting of hidkbd, wskbd has all the goo */
243 	if (kbd->sc_wskbddev != NULL)
244 		rv = config_detach(kbd->sc_wskbddev, flags);
245 
246 	if (kbd->sc_var != NULL)
247 		free(kbd->sc_var, M_DEVBUF, 0);
248 
249 	return (rv);
250 }
251 
252 void
253 hidkbd_input(struct hidkbd *kbd, uint8_t *data, u_int len)
254 {
255 	struct hidkbd_data *ud = &kbd->sc_ndata;
256 	int i;
257 
258 #ifdef HIDKBD_DEBUG
259 	if (hidkbddebug > 5) {
260 		printf("hidkbd_input: data");
261 		for (i = 0; i < len; i++)
262 			printf(" 0x%02x", data[i]);
263 		printf("\n");
264 	}
265 #endif
266 
267 	/* extract variable keys */
268 	for (i = 0; i < kbd->sc_nvar; i++)
269 		ud->var[i] = (u_int8_t)hid_get_data(data, len,
270 		    &kbd->sc_var[i].loc);
271 
272 	/* extract keycodes */
273 	memcpy(ud->keycode, data + kbd->sc_keycodeloc.pos / 8,
274 	    kbd->sc_nkeycode);
275 
276 	if (kbd->sc_debounce && !kbd->sc_polling) {
277 		/*
278 		 * Some keyboards have a peculiar quirk.  They sometimes
279 		 * generate a key up followed by a key down for the same
280 		 * key after about 10 ms.
281 		 * We avoid this bug by holding off decoding for 20 ms.
282 		 */
283 		kbd->sc_data = *ud;
284 		timeout_add_msec(&kbd->sc_delay, 20);
285 #ifdef DDB
286 	} else if (kbd->sc_console_keyboard && !kbd->sc_polling) {
287 		/*
288 		 * For the console keyboard we can't deliver CTL-ALT-ESC
289 		 * from the interrupt routine.  Doing so would start
290 		 * polling from inside the interrupt routine and that
291 		 * loses bigtime.
292 		 */
293 		/* if (!timeout_pending(&kbd->sc_delay)) */ {
294 			kbd->sc_data = *ud;
295 			timeout_add(&kbd->sc_delay, 1);
296 		}
297 #endif
298 	} else {
299 		hidkbd_decode(kbd, ud);
300 	}
301 }
302 
303 void
304 hidkbd_delayed_decode(void *addr)
305 {
306 	struct hidkbd *kbd = addr;
307 
308 	hidkbd_decode(kbd, &kbd->sc_data);
309 }
310 
311 void
312 hidkbd_decode(struct hidkbd *kbd, struct hidkbd_data *ud)
313 {
314 	u_int16_t ibuf[MAXKEYS];	/* chars events */
315 	int s;
316 	int nkeys, i, j;
317 	int key;
318 #define ADDKEY(c) ibuf[nkeys++] = (c)
319 
320 #ifdef HIDKBD_DEBUG
321 	/*
322 	 * Keep a trace of the last events.  Using printf changes the
323 	 * timing, so this can be useful sometimes.
324 	 */
325 	if (hidkbdtrace) {
326 		struct hidkbdtraceinfo *p = &hidkbdtracedata[hidkbdtraceindex];
327 		p->unit = kbd->sc_device->dv_unit;
328 		microtime(&p->tv);
329 		p->ud = *ud;
330 		if (++hidkbdtraceindex >= HIDKBDTRACESIZE)
331 			hidkbdtraceindex = 0;
332 	}
333 	if (hidkbddebug > 5) {
334 		struct timeval tv;
335 		microtime(&tv);
336 		DPRINTF((" at %lld.%06ld  mod=0x%02x key0=0x%02x key1=0x%02x "
337 			 "key2=0x%02x key3=0x%02x\n",
338 			 (long long)tv.tv_sec, tv.tv_usec,
339 			 ud->modifiers, ud->keycode[0], ud->keycode[1],
340 			 ud->keycode[2], ud->keycode[3]));
341 	}
342 #endif
343 
344 	if (ud->keycode[0] == KEY_ERROR) {
345 		DPRINTF(("hidkbd_input: KEY_ERROR\n"));
346 		return;		/* ignore  */
347 	}
348 	nkeys = 0;
349 
350 	for (i = 0; i < kbd->sc_nvar; i++)
351 		if ((kbd->sc_odata.var[i] & kbd->sc_var[i].mask) !=
352 		    (ud->var[i] & kbd->sc_var[i].mask)) {
353 			ADDKEY(kbd->sc_var[i].key |
354 			    ((ud->var[i] & kbd->sc_var[i].mask) ?
355 			    PRESS : RELEASE));
356 		}
357 
358 	if (memcmp(ud->keycode, kbd->sc_odata.keycode, kbd->sc_nkeycode) != 0) {
359 		/* Check for released keys. */
360 		for (i = 0; i < kbd->sc_nkeycode; i++) {
361 			key = kbd->sc_odata.keycode[i];
362 			if (key == 0)
363 				continue;
364 			for (j = 0; j < kbd->sc_nkeycode; j++)
365 				if (key == ud->keycode[j])
366 					goto rfound;
367 			DPRINTFN(3,("hidkbd_decode: relse key=0x%02x\n", key));
368 			ADDKEY(key | RELEASE);
369 		rfound:
370 			;
371 		}
372 
373 		/* Check for pressed keys. */
374 		for (i = 0; i < kbd->sc_nkeycode; i++) {
375 			key = ud->keycode[i];
376 			if (key == 0)
377 				continue;
378 			for (j = 0; j < kbd->sc_nkeycode; j++)
379 				if (key == kbd->sc_odata.keycode[j])
380 					goto pfound;
381 			DPRINTFN(2,("hidkbd_decode: press key=0x%02x\n", key));
382 			ADDKEY(key | PRESS);
383 		pfound:
384 			;
385 		}
386 	}
387 	kbd->sc_odata = *ud;
388 
389 	if (nkeys == 0)
390 		return;
391 
392 	if (kbd->sc_polling) {
393 		DPRINTFN(1,("hidkbd_decode: pollchar = 0x%03x\n", ibuf[0]));
394 		memcpy(kbd->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
395 		kbd->sc_npollchar = nkeys;
396 		return;
397 	}
398 
399 	if (kbd->sc_wskbddev == NULL)
400 		return;
401 
402 #ifdef WSDISPLAY_COMPAT_RAWKBD
403 	if (kbd->sc_rawkbd) {
404 		u_char cbuf[MAXKEYS * 2];
405 		int c;
406 
407 		for (i = j = 0; i < nkeys; i++) {
408 			key = ibuf[i];
409 			c = hidkbd_trtab[key & CODEMASK];
410 			if (c == NN)
411 				continue;
412 			if (c & 0x80)
413 				cbuf[j++] = 0xe0;
414 			cbuf[j] = c & 0x7f;
415 			if (key & RELEASE)
416 				cbuf[j] |= 0x80;
417 			DPRINTFN(1,("hidkbd_decode: raw = %s0x%02x\n",
418 				    c & 0x80 ? "0xe0 " : "",
419 				    cbuf[j]));
420 			j++;
421 		}
422 		s = spltty();
423 		wskbd_rawinput(kbd->sc_wskbddev, cbuf, j);
424 
425 		/*
426 		 * Pass audio keys to wskbd_input anyway.
427 		 */
428 		for (i = 0; i < nkeys; i++) {
429 			key = ibuf[i];
430 			switch (key & CODEMASK) {
431 			case 127:
432 			case 128:
433 			case 129:
434 				wskbd_input(kbd->sc_wskbddev,
435 				    key & RELEASE ?  WSCONS_EVENT_KEY_UP :
436 				      WSCONS_EVENT_KEY_DOWN, key & CODEMASK);
437 				break;
438 			}
439 		}
440 		splx(s);
441 
442 		return;
443 	}
444 #endif
445 
446 	s = spltty();
447 	for (i = 0; i < nkeys; i++) {
448 		key = ibuf[i];
449 		wskbd_input(kbd->sc_wskbddev,
450 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
451 		    key&CODEMASK);
452 	}
453 	splx(s);
454 #undef	ADDKEY
455 }
456 
457 int
458 hidkbd_enable(struct hidkbd *kbd, int on)
459 {
460 	if (kbd->sc_enabled == on)
461 		return EBUSY;
462 
463 	kbd->sc_enabled = on;
464 	return 0;
465 }
466 
467 int
468 hidkbd_set_leds(struct hidkbd *kbd, int leds, uint8_t *report)
469 {
470 	if (kbd->sc_leds == leds)
471 		return 0;
472 
473 	kbd->sc_leds = leds;
474 
475 	/*
476 	 * This is not totally correct, since we did not check the
477 	 * report size from the descriptor but for keyboards it should
478 	 * just be a single byte with the relevant bits set.
479 	 */
480 	*report = 0;
481 	if ((leds & WSKBD_LED_SCROLL) && kbd->sc_scroloc.size == 1)
482 		*report |= 1 << kbd->sc_scroloc.pos;
483 	if ((leds & WSKBD_LED_NUM) && kbd->sc_numloc.size == 1)
484 		*report |= 1 << kbd->sc_numloc.pos;
485 	if ((leds & WSKBD_LED_CAPS) && kbd->sc_capsloc.size == 1)
486 		*report |= 1 << kbd->sc_capsloc.pos;
487 	if ((leds & WSKBD_LED_COMPOSE) && kbd->sc_compose.size == 1)
488 		*report |= 1 << kbd->sc_compose.pos;
489 
490 	return 1;
491 }
492 
493 int
494 hidkbd_ioctl(struct hidkbd *kbd, u_long cmd, caddr_t data, int flag,
495    struct proc *p)
496 {
497 	switch (cmd) {
498 	case WSKBDIO_GETLEDS:
499 		*(int *)data = kbd->sc_leds;
500 		return (0);
501 	case WSKBDIO_COMPLEXBELL:
502 #define d ((struct wskbd_bell_data *)data)
503 		hidkbd_bell(d->pitch, d->period, d->volume, 0);
504 #undef d
505 		return (0);
506 #ifdef WSDISPLAY_COMPAT_RAWKBD
507 	case WSKBDIO_SETMODE:
508 		DPRINTF(("hidkbd_ioctl: set raw = %d\n", *(int *)data));
509 		kbd->sc_rawkbd = *(int *)data == WSKBD_RAW;
510 		return (0);
511 #endif
512 	}
513 	return (-1);
514 }
515 
516 void
517 hidkbd_cngetc(struct hidkbd *kbd, u_int *type, int *data)
518 {
519 	int c;
520 
521 	c = kbd->sc_pollchars[0];
522 	kbd->sc_npollchar--;
523 	memcpy(kbd->sc_pollchars, kbd->sc_pollchars+1,
524 	       kbd->sc_npollchar * sizeof(u_int16_t));
525 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
526 	*data = c & CODEMASK;
527 }
528 
529 void
530 hidkbd_bell(u_int pitch, u_int period, u_int volume, int poll)
531 {
532 	if (hidkbd_bell_fn != NULL)
533 		(*hidkbd_bell_fn)(hidkbd_bell_fn_arg, pitch, period,
534 		    volume, poll);
535 }
536 
537 void
538 hidkbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
539 {
540 	if (hidkbd_bell_fn == NULL) {
541 		hidkbd_bell_fn = fn;
542 		hidkbd_bell_fn_arg = arg;
543 	}
544 }
545 
546 const char *
547 hidkbd_parse_desc(struct hidkbd *kbd, int id, void *desc, int dlen)
548 {
549 	struct hid_data *d;
550 	struct hid_item h;
551 	unsigned int i, ivar = 0;
552 
553 	kbd->sc_nkeycode = 0;
554 
555 	d = hid_start_parse(desc, dlen, hid_input);
556 	while (hid_get_item(d, &h)) {
557 		if (h.kind != hid_input || (h.flags & HIO_CONST) ||
558 		    HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
559 		    h.report_ID != id)
560 			continue;
561 		if (h.flags & HIO_VARIABLE)
562 			ivar++;
563 	}
564 	hid_end_parse(d);
565 
566 	if (ivar > MAXVARS) {
567 		DPRINTF((": too many variable keys\n"));
568 		ivar = MAXVARS;
569 	}
570 
571 	kbd->sc_nvar = ivar;
572 	kbd->sc_var = (struct hidkbd_variable *)mallocarray(ivar,
573 			sizeof(struct hidkbd_variable), M_DEVBUF, M_NOWAIT);
574 
575 	if (!kbd->sc_var)
576 		return NULL;
577 
578 	i = 0;
579 
580 	d = hid_start_parse(desc, dlen, hid_input);
581 	while (hid_get_item(d, &h)) {
582 		if (h.kind != hid_input || (h.flags & HIO_CONST) ||
583 		    HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
584 		    h.report_ID != id)
585 			continue;
586 
587 		DPRINTF(("hidkbd: usage=0x%x flags=0x%x pos=%d size=%d cnt=%d",
588 			 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
589 		if (h.flags & HIO_VARIABLE) {
590 			/* variable reports should be one bit each */
591 			if (h.loc.size != 1) {
592 				DPRINTF((": bad variable size\n"));
593 				continue;
594 			}
595 
596 			/* variable report */
597 			if (i < MAXVARS) {
598 				kbd->sc_var[i].loc = h.loc;
599 				kbd->sc_var[i].mask = 1 << (i % 8);
600 				kbd->sc_var[i].key = HID_GET_USAGE(h.usage);
601 				i++;
602 			}
603 		} else {
604 			/* keys array should be in bytes, on a byte boundary */
605 			if (h.loc.size != 8) {
606 				DPRINTF((": key code size != 8\n"));
607 				continue;
608 			}
609 			if (h.loc.pos % 8 != 0) {
610 				DPRINTF((": array not on byte boundary"));
611 				continue;
612 			}
613 			if (kbd->sc_nkeycode != 0) {
614 				DPRINTF((": ignoring multiple arrays\n"));
615 				continue;
616 			}
617 			kbd->sc_keycodeloc = h.loc;
618 			if (h.loc.count > MAXKEYCODE) {
619 				DPRINTF((": ignoring extra key codes"));
620 				kbd->sc_nkeycode = MAXKEYCODE;
621 			} else
622 				kbd->sc_nkeycode = h.loc.count;
623 		}
624 		DPRINTF(("\n"));
625 	}
626 	hid_end_parse(d);
627 
628 	/* don't attach if no keys... */
629 	if (kbd->sc_nkeycode == 0 && ivar == 0)
630 		return "no usable key codes array";
631 
632 	hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_NUM_LOCK),
633 	    id, hid_output, &kbd->sc_numloc, NULL);
634 	hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_CAPS_LOCK),
635 	    id, hid_output, &kbd->sc_capsloc, NULL);
636 	hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_SCROLL_LOCK),
637 	    id, hid_output, &kbd->sc_scroloc, NULL);
638 	hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_COMPOSE),
639 	    id, hid_output, &kbd->sc_compose, NULL);
640 
641 	return (NULL);
642 }
643