1 /* $OpenBSD: hidkbd.c,v 1.6 2020/11/02 19:45:18 tobhe 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 and brightness 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 case 232: 409 case 233: 410 wskbd_input(kbd->sc_wskbddev, 411 key & RELEASE ? WSCONS_EVENT_KEY_UP : 412 WSCONS_EVENT_KEY_DOWN, key & CODEMASK); 413 break; 414 } 415 } 416 splx(s); 417 418 return; 419 } 420 #endif 421 422 s = spltty(); 423 for (i = 0; i < nkeys; i++) { 424 key = ibuf[i]; 425 wskbd_input(kbd->sc_wskbddev, 426 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN, 427 key&CODEMASK); 428 } 429 splx(s); 430 #undef ADDKEY 431 } 432 433 int 434 hidkbd_enable(struct hidkbd *kbd, int on) 435 { 436 if (kbd->sc_enabled == on) 437 return EBUSY; 438 439 kbd->sc_enabled = on; 440 return 0; 441 } 442 443 int 444 hidkbd_set_leds(struct hidkbd *kbd, int leds, uint8_t *report) 445 { 446 if (kbd->sc_leds == leds) 447 return 0; 448 449 kbd->sc_leds = leds; 450 451 /* 452 * This is not totally correct, since we did not check the 453 * report size from the descriptor but for keyboards it should 454 * just be a single byte with the relevant bits set. 455 */ 456 *report = 0; 457 if ((leds & WSKBD_LED_SCROLL) && kbd->sc_scroloc.size == 1) 458 *report |= 1 << kbd->sc_scroloc.pos; 459 if ((leds & WSKBD_LED_NUM) && kbd->sc_numloc.size == 1) 460 *report |= 1 << kbd->sc_numloc.pos; 461 if ((leds & WSKBD_LED_CAPS) && kbd->sc_capsloc.size == 1) 462 *report |= 1 << kbd->sc_capsloc.pos; 463 if ((leds & WSKBD_LED_COMPOSE) && kbd->sc_compose.size == 1) 464 *report |= 1 << kbd->sc_compose.pos; 465 466 return 1; 467 } 468 469 int 470 hidkbd_ioctl(struct hidkbd *kbd, u_long cmd, caddr_t data, int flag, 471 struct proc *p) 472 { 473 switch (cmd) { 474 case WSKBDIO_GETLEDS: 475 *(int *)data = kbd->sc_leds; 476 return (0); 477 case WSKBDIO_COMPLEXBELL: 478 #define d ((struct wskbd_bell_data *)data) 479 hidkbd_bell(d->pitch, d->period, d->volume, 0); 480 #undef d 481 return (0); 482 #ifdef WSDISPLAY_COMPAT_RAWKBD 483 case WSKBDIO_SETMODE: 484 DPRINTF(("hidkbd_ioctl: set raw = %d\n", *(int *)data)); 485 kbd->sc_rawkbd = *(int *)data == WSKBD_RAW; 486 return (0); 487 #endif 488 } 489 return (-1); 490 } 491 492 void 493 hidkbd_cngetc(struct hidkbd *kbd, u_int *type, int *data) 494 { 495 int c; 496 497 c = kbd->sc_pollchars[0]; 498 kbd->sc_npollchar--; 499 memmove(kbd->sc_pollchars, kbd->sc_pollchars+1, 500 kbd->sc_npollchar * sizeof(u_int16_t)); 501 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN; 502 *data = c & CODEMASK; 503 } 504 505 void 506 hidkbd_bell(u_int pitch, u_int period, u_int volume, int poll) 507 { 508 if (hidkbd_bell_fn != NULL) 509 (*hidkbd_bell_fn)(hidkbd_bell_fn_arg, pitch, period, 510 volume, poll); 511 } 512 513 void 514 hidkbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg) 515 { 516 if (hidkbd_bell_fn == NULL) { 517 hidkbd_bell_fn = fn; 518 hidkbd_bell_fn_arg = arg; 519 } 520 } 521 522 const char * 523 hidkbd_parse_desc(struct hidkbd *kbd, int id, void *desc, int dlen) 524 { 525 struct hid_data *d; 526 struct hid_item h; 527 unsigned int i, ivar = 0; 528 529 kbd->sc_nkeycode = 0; 530 531 d = hid_start_parse(desc, dlen, hid_input); 532 while (hid_get_item(d, &h)) { 533 if (h.kind != hid_input || (h.flags & HIO_CONST) || 534 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD || 535 h.report_ID != id) 536 continue; 537 if (h.flags & HIO_VARIABLE) 538 ivar++; 539 } 540 hid_end_parse(d); 541 542 if (ivar > MAXVARS) { 543 DPRINTF((": too many variable keys\n")); 544 ivar = MAXVARS; 545 } 546 547 kbd->sc_nvar = ivar; 548 kbd->sc_var = (struct hidkbd_variable *)mallocarray(ivar, 549 sizeof(struct hidkbd_variable), M_DEVBUF, M_NOWAIT); 550 551 if (!kbd->sc_var) 552 return NULL; 553 554 i = 0; 555 556 d = hid_start_parse(desc, dlen, hid_input); 557 while (hid_get_item(d, &h)) { 558 if (h.kind != hid_input || (h.flags & HIO_CONST) || 559 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD || 560 h.report_ID != id) 561 continue; 562 563 DPRINTF(("hidkbd: usage=0x%x flags=0x%x pos=%d size=%d cnt=%d", 564 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count)); 565 if (h.flags & HIO_VARIABLE) { 566 /* variable reports should be one bit each */ 567 if (h.loc.size != 1) { 568 DPRINTF((": bad variable size\n")); 569 continue; 570 } 571 572 /* variable report */ 573 if (i < MAXVARS) { 574 kbd->sc_var[i].loc = h.loc; 575 kbd->sc_var[i].mask = 1 << (i % 8); 576 kbd->sc_var[i].key = HID_GET_USAGE(h.usage); 577 i++; 578 } 579 } else { 580 /* keys array should be in bytes, on a byte boundary */ 581 if (h.loc.size != 8) { 582 DPRINTF((": key code size != 8\n")); 583 continue; 584 } 585 if (h.loc.pos % 8 != 0) { 586 DPRINTF((": array not on byte boundary")); 587 continue; 588 } 589 if (kbd->sc_nkeycode != 0) { 590 DPRINTF((": ignoring multiple arrays\n")); 591 continue; 592 } 593 kbd->sc_keycodeloc = h.loc; 594 if (h.loc.count > MAXKEYCODE) { 595 DPRINTF((": ignoring extra key codes")); 596 kbd->sc_nkeycode = MAXKEYCODE; 597 } else 598 kbd->sc_nkeycode = h.loc.count; 599 } 600 DPRINTF(("\n")); 601 } 602 hid_end_parse(d); 603 604 /* don't attach if no keys... */ 605 if (kbd->sc_nkeycode == 0 && ivar == 0) 606 return "no usable key codes array"; 607 608 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_NUM_LOCK), 609 id, hid_output, &kbd->sc_numloc, NULL); 610 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_CAPS_LOCK), 611 id, hid_output, &kbd->sc_capsloc, NULL); 612 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_SCROLL_LOCK), 613 id, hid_output, &kbd->sc_scroloc, NULL); 614 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_COMPOSE), 615 id, hid_output, &kbd->sc_compose, NULL); 616 617 return (NULL); 618 } 619