Line data Source code
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 0 : 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 0 : kbd->sc_var = NULL;
163 :
164 0 : parserr = hidkbd_parse_desc(kbd, id, desc, dlen);
165 0 : if (parserr != NULL) {
166 0 : printf(": %s\n", parserr);
167 0 : return ENXIO;
168 : }
169 :
170 : #ifdef DIAGNOSTIC
171 0 : printf(": %d variable keys, %d key codes",
172 0 : kbd->sc_nvar, kbd->sc_nkeycode);
173 : #endif
174 :
175 0 : kbd->sc_device = self;
176 0 : 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 0 : if (console) {
185 0 : kbd->sc_console_keyboard = hidkbd_is_console;
186 : /* Don't let any other keyboard have it. */
187 0 : hidkbd_is_console = 0;
188 0 : }
189 :
190 0 : timeout_set(&kbd->sc_delay, hidkbd_delayed_decode, kbd);
191 :
192 0 : return 0;
193 0 : }
194 :
195 : void
196 0 : hidkbd_attach_wskbd(struct hidkbd *kbd, kbd_t layout,
197 : const struct wskbd_accessops *accessops)
198 : {
199 0 : struct wskbddev_attach_args a;
200 :
201 0 : ukbd_keymapdata.layout = layout;
202 :
203 0 : a.console = kbd->sc_console_keyboard;
204 0 : a.keymap = &ukbd_keymapdata;
205 0 : a.accessops = accessops;
206 0 : a.accesscookie = kbd->sc_device;
207 0 : kbd->sc_wskbddev = config_found(kbd->sc_device, &a, wskbddevprint);
208 0 : }
209 :
210 : int
211 0 : 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 0 : 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 0 : printf("%s: was console keyboard\n",
226 0 : kbd->sc_device->dv_xname);
227 0 : hidkbd_is_console = 1;
228 0 : }
229 : /* No need to do reference counting of hidkbd, wskbd has all the goo */
230 0 : if (kbd->sc_wskbddev != NULL)
231 0 : rv = config_detach(kbd->sc_wskbddev, flags);
232 :
233 0 : if (kbd->sc_var != NULL)
234 0 : free(kbd->sc_var, M_DEVBUF, 0);
235 :
236 0 : return (rv);
237 : }
238 :
239 : void
240 0 : hidkbd_input(struct hidkbd *kbd, uint8_t *data, u_int len)
241 : {
242 0 : 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 0 : for (i = 0; i < kbd->sc_nvar; i++)
256 0 : ud->var[i] = (u_int8_t)hid_get_data(data, len,
257 0 : &kbd->sc_var[i].loc);
258 :
259 : /* extract keycodes */
260 0 : memcpy(ud->keycode, data + kbd->sc_keycodeloc.pos / 8,
261 : kbd->sc_nkeycode);
262 :
263 0 : 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 0 : kbd->sc_data = *ud;
271 0 : timeout_add_msec(&kbd->sc_delay, 20);
272 0 : } else {
273 0 : hidkbd_decode(kbd, ud);
274 : }
275 0 : }
276 :
277 : void
278 0 : hidkbd_delayed_decode(void *addr)
279 : {
280 0 : struct hidkbd *kbd = addr;
281 :
282 0 : hidkbd_decode(kbd, &kbd->sc_data);
283 0 : }
284 :
285 : void
286 0 : hidkbd_decode(struct hidkbd *kbd, struct hidkbd_data *ud)
287 : {
288 0 : 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 0 : if (ud->keycode[0] == KEY_ERROR) {
319 : DPRINTF(("hidkbd_input: KEY_ERROR\n"));
320 0 : return; /* ignore */
321 : }
322 : nkeys = 0;
323 :
324 0 : for (i = 0; i < kbd->sc_nvar; i++)
325 0 : if ((kbd->sc_odata.var[i] & kbd->sc_var[i].mask) !=
326 0 : (ud->var[i] & kbd->sc_var[i].mask)) {
327 0 : ADDKEY(kbd->sc_var[i].key |
328 : ((ud->var[i] & kbd->sc_var[i].mask) ?
329 : PRESS : RELEASE));
330 0 : }
331 :
332 0 : if (memcmp(ud->keycode, kbd->sc_odata.keycode, kbd->sc_nkeycode) != 0) {
333 : /* Check for released keys. */
334 0 : for (i = 0; i < kbd->sc_nkeycode; i++) {
335 0 : key = kbd->sc_odata.keycode[i];
336 0 : if (key == 0)
337 : continue;
338 0 : for (j = 0; j < kbd->sc_nkeycode; j++)
339 0 : if (key == ud->keycode[j])
340 : goto rfound;
341 : DPRINTFN(3,("hidkbd_decode: relse key=0x%02x\n", key));
342 0 : ADDKEY(key | RELEASE);
343 : rfound:
344 : ;
345 : }
346 :
347 : /* Check for pressed keys. */
348 0 : for (i = 0; i < kbd->sc_nkeycode; i++) {
349 0 : key = ud->keycode[i];
350 0 : if (key == 0)
351 : continue;
352 0 : for (j = 0; j < kbd->sc_nkeycode; j++)
353 0 : if (key == kbd->sc_odata.keycode[j])
354 : goto pfound;
355 : DPRINTFN(2,("hidkbd_decode: press key=0x%02x\n", key));
356 0 : ADDKEY(key | PRESS);
357 : pfound:
358 : ;
359 : }
360 : }
361 0 : kbd->sc_odata = *ud;
362 :
363 0 : if (nkeys == 0)
364 0 : return;
365 :
366 0 : if (kbd->sc_polling) {
367 : DPRINTFN(1,("hidkbd_decode: pollchar = 0x%03x\n", ibuf[0]));
368 0 : memcpy(kbd->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
369 0 : kbd->sc_npollchar = nkeys;
370 0 : return;
371 : }
372 :
373 0 : if (kbd->sc_wskbddev == NULL)
374 0 : return;
375 :
376 : #ifdef WSDISPLAY_COMPAT_RAWKBD
377 0 : if (kbd->sc_rawkbd) {
378 0 : u_char cbuf[MAXKEYS * 2];
379 : int c;
380 :
381 0 : for (i = j = 0; i < nkeys; i++) {
382 0 : key = ibuf[i];
383 0 : c = hidkbd_trtab[key & CODEMASK];
384 0 : if (c == NN)
385 : continue;
386 0 : if (c & 0x80)
387 0 : cbuf[j++] = 0xe0;
388 0 : cbuf[j] = c & 0x7f;
389 0 : if (key & RELEASE)
390 0 : cbuf[j] |= 0x80;
391 : DPRINTFN(1,("hidkbd_decode: raw = %s0x%02x\n",
392 : c & 0x80 ? "0xe0 " : "",
393 : cbuf[j]));
394 0 : j++;
395 0 : }
396 0 : s = spltty();
397 0 : wskbd_rawinput(kbd->sc_wskbddev, cbuf, j);
398 :
399 : /*
400 : * Pass audio keys to wskbd_input anyway.
401 : */
402 0 : for (i = 0; i < nkeys; i++) {
403 0 : key = ibuf[i];
404 0 : switch (key & CODEMASK) {
405 : case 127:
406 : case 128:
407 : case 129:
408 0 : wskbd_input(kbd->sc_wskbddev,
409 0 : key & RELEASE ? WSCONS_EVENT_KEY_UP :
410 : WSCONS_EVENT_KEY_DOWN, key & CODEMASK);
411 0 : break;
412 : }
413 : }
414 0 : splx(s);
415 :
416 : return;
417 0 : }
418 : #endif
419 :
420 0 : s = spltty();
421 0 : for (i = 0; i < nkeys; i++) {
422 0 : key = ibuf[i];
423 0 : wskbd_input(kbd->sc_wskbddev,
424 0 : key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
425 0 : key&CODEMASK);
426 : }
427 0 : splx(s);
428 : #undef ADDKEY
429 0 : }
430 :
431 : int
432 0 : hidkbd_enable(struct hidkbd *kbd, int on)
433 : {
434 0 : if (kbd->sc_enabled == on)
435 0 : return EBUSY;
436 :
437 0 : kbd->sc_enabled = on;
438 0 : return 0;
439 0 : }
440 :
441 : int
442 0 : hidkbd_set_leds(struct hidkbd *kbd, int leds, uint8_t *report)
443 : {
444 0 : if (kbd->sc_leds == leds)
445 0 : return 0;
446 :
447 0 : 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 0 : *report = 0;
455 0 : if ((leds & WSKBD_LED_SCROLL) && kbd->sc_scroloc.size == 1)
456 0 : *report |= 1 << kbd->sc_scroloc.pos;
457 0 : if ((leds & WSKBD_LED_NUM) && kbd->sc_numloc.size == 1)
458 0 : *report |= 1 << kbd->sc_numloc.pos;
459 0 : if ((leds & WSKBD_LED_CAPS) && kbd->sc_capsloc.size == 1)
460 0 : *report |= 1 << kbd->sc_capsloc.pos;
461 0 : if ((leds & WSKBD_LED_COMPOSE) && kbd->sc_compose.size == 1)
462 0 : *report |= 1 << kbd->sc_compose.pos;
463 :
464 0 : return 1;
465 0 : }
466 :
467 : int
468 0 : hidkbd_ioctl(struct hidkbd *kbd, u_long cmd, caddr_t data, int flag,
469 : struct proc *p)
470 : {
471 0 : switch (cmd) {
472 : case WSKBDIO_GETLEDS:
473 0 : *(int *)data = kbd->sc_leds;
474 0 : return (0);
475 : case WSKBDIO_COMPLEXBELL:
476 : #define d ((struct wskbd_bell_data *)data)
477 0 : hidkbd_bell(d->pitch, d->period, d->volume, 0);
478 : #undef d
479 0 : return (0);
480 : #ifdef WSDISPLAY_COMPAT_RAWKBD
481 : case WSKBDIO_SETMODE:
482 : DPRINTF(("hidkbd_ioctl: set raw = %d\n", *(int *)data));
483 0 : kbd->sc_rawkbd = *(int *)data == WSKBD_RAW;
484 0 : return (0);
485 : #endif
486 : }
487 0 : return (-1);
488 0 : }
489 :
490 : void
491 0 : hidkbd_cngetc(struct hidkbd *kbd, u_int *type, int *data)
492 : {
493 : int c;
494 :
495 0 : c = kbd->sc_pollchars[0];
496 0 : kbd->sc_npollchar--;
497 0 : memmove(kbd->sc_pollchars, kbd->sc_pollchars+1,
498 : kbd->sc_npollchar * sizeof(u_int16_t));
499 0 : *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
500 0 : *data = c & CODEMASK;
501 0 : }
502 :
503 : void
504 0 : hidkbd_bell(u_int pitch, u_int period, u_int volume, int poll)
505 : {
506 0 : if (hidkbd_bell_fn != NULL)
507 0 : (*hidkbd_bell_fn)(hidkbd_bell_fn_arg, pitch, period,
508 : volume, poll);
509 0 : }
510 :
511 : void
512 0 : hidkbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
513 : {
514 0 : if (hidkbd_bell_fn == NULL) {
515 0 : hidkbd_bell_fn = fn;
516 0 : hidkbd_bell_fn_arg = arg;
517 0 : }
518 0 : }
519 :
520 : const char *
521 0 : hidkbd_parse_desc(struct hidkbd *kbd, int id, void *desc, int dlen)
522 : {
523 : struct hid_data *d;
524 0 : struct hid_item h;
525 : unsigned int i, ivar = 0;
526 :
527 0 : kbd->sc_nkeycode = 0;
528 :
529 0 : d = hid_start_parse(desc, dlen, hid_input);
530 0 : while (hid_get_item(d, &h)) {
531 0 : if (h.kind != hid_input || (h.flags & HIO_CONST) ||
532 0 : HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
533 0 : h.report_ID != id)
534 0 : continue;
535 0 : if (h.flags & HIO_VARIABLE)
536 0 : ivar++;
537 : }
538 0 : hid_end_parse(d);
539 :
540 0 : if (ivar > MAXVARS) {
541 : DPRINTF((": too many variable keys\n"));
542 : ivar = MAXVARS;
543 0 : }
544 :
545 0 : kbd->sc_nvar = ivar;
546 0 : kbd->sc_var = (struct hidkbd_variable *)mallocarray(ivar,
547 : sizeof(struct hidkbd_variable), M_DEVBUF, M_NOWAIT);
548 :
549 0 : if (!kbd->sc_var)
550 0 : return NULL;
551 :
552 : i = 0;
553 :
554 0 : d = hid_start_parse(desc, dlen, hid_input);
555 0 : while (hid_get_item(d, &h)) {
556 0 : if (h.kind != hid_input || (h.flags & HIO_CONST) ||
557 0 : HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
558 0 : h.report_ID != id)
559 0 : 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 0 : if (h.flags & HIO_VARIABLE) {
564 : /* variable reports should be one bit each */
565 0 : if (h.loc.size != 1) {
566 : DPRINTF((": bad variable size\n"));
567 0 : continue;
568 : }
569 :
570 : /* variable report */
571 0 : if (i < MAXVARS) {
572 0 : kbd->sc_var[i].loc = h.loc;
573 0 : kbd->sc_var[i].mask = 1 << (i % 8);
574 0 : kbd->sc_var[i].key = HID_GET_USAGE(h.usage);
575 0 : i++;
576 0 : }
577 : } else {
578 : /* keys array should be in bytes, on a byte boundary */
579 0 : if (h.loc.size != 8) {
580 : DPRINTF((": key code size != 8\n"));
581 0 : continue;
582 : }
583 0 : if (h.loc.pos % 8 != 0) {
584 : DPRINTF((": array not on byte boundary"));
585 0 : continue;
586 : }
587 0 : if (kbd->sc_nkeycode != 0) {
588 : DPRINTF((": ignoring multiple arrays\n"));
589 0 : continue;
590 : }
591 0 : kbd->sc_keycodeloc = h.loc;
592 0 : if (h.loc.count > MAXKEYCODE) {
593 : DPRINTF((": ignoring extra key codes"));
594 0 : kbd->sc_nkeycode = MAXKEYCODE;
595 0 : } else
596 0 : kbd->sc_nkeycode = h.loc.count;
597 : }
598 : DPRINTF(("\n"));
599 : }
600 0 : hid_end_parse(d);
601 :
602 : /* don't attach if no keys... */
603 0 : if (kbd->sc_nkeycode == 0 && ivar == 0)
604 0 : return "no usable key codes array";
605 :
606 0 : hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_NUM_LOCK),
607 0 : id, hid_output, &kbd->sc_numloc, NULL);
608 0 : hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_CAPS_LOCK),
609 0 : id, hid_output, &kbd->sc_capsloc, NULL);
610 0 : hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_SCROLL_LOCK),
611 0 : id, hid_output, &kbd->sc_scroloc, NULL);
612 0 : hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_COMPOSE),
613 0 : id, hid_output, &kbd->sc_compose, NULL);
614 :
615 0 : return (NULL);
616 0 : }
|