1 /* $OpenBSD: hidkbd.c,v 1.9 2023/07/09 08:02:13 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 static const struct hidkbd_translation apple_tb_trans[] = { 112 { 30, 58 }, /* 1 -> F1 */ 113 { 31, 59 }, /* 2 -> F2 */ 114 { 32, 60 }, /* 3 -> F3 */ 115 { 33, 61 }, /* 4 -> F4 */ 116 { 34, 62 }, /* 5 -> F5 */ 117 { 35, 63 }, /* 6 -> F6 */ 118 { 36, 64 }, /* 7 -> F7 */ 119 { 37, 65 }, /* 8 -> F8 */ 120 { 38, 66 }, /* 9 -> F9 */ 121 { 39, 67 }, /* 0 -> F10 */ 122 { 45, 68 }, /* - -> F11 */ 123 { 46, 69 } /* = -> F12 */ 124 }; 125 126 static const struct hidkbd_translation apple_fn_trans[] = { 127 { 40, 73 }, /* return -> insert */ 128 { 42, 76 }, /* backspace -> delete */ 129 #ifdef notyet 130 { 58, 0 }, /* F1 -> screen brightness down */ 131 { 59, 0 }, /* F2 -> screen brightness up */ 132 { 60, 0 }, /* F3 */ 133 { 61, 0 }, /* F4 */ 134 { 62, 0 }, /* F5 -> keyboard backlight down */ 135 { 63, 0 }, /* F6 -> keyboard backlight up */ 136 { 64, 0 }, /* F7 -> audio back */ 137 { 65, 0 }, /* F8 -> audio pause/play */ 138 { 66, 0 }, /* F9 -> audio next */ 139 #endif 140 #ifdef __macppc__ 141 { 58, 233 }, /* F1 -> screen brightness down */ 142 { 59, 232 }, /* F2 -> screen brightness up */ 143 { 60, 127 }, /* F3 -> audio mute */ 144 { 61, 129 }, /* F4 -> audio lower */ 145 { 62, 128 }, /* F5 -> audio raise */ 146 #else 147 { 63, 102 }, /* F6 -> sleep */ 148 { 67, 127 }, /* F10 -> audio mute */ 149 { 68, 129 }, /* F11 -> audio lower */ 150 { 69, 128 }, /* F12 -> audio raise */ 151 #endif 152 { 79, 77 }, /* right -> end */ 153 { 80, 74 }, /* left -> home */ 154 { 81, 78 }, /* down -> page down */ 155 { 82, 75 } /* up -> page up */ 156 }; 157 158 static const struct hidkbd_translation apple_mba_trans[] = { 159 { 40, 73 }, /* return -> insert */ 160 { 42, 76 }, /* backspace -> delete */ 161 #ifdef notyet 162 { 58, 0 }, /* F1 -> screen brightness down */ 163 { 59, 0 }, /* F2 -> screen brightness up */ 164 { 60, 0 }, /* F3 */ 165 { 61, 0 }, /* F4 */ 166 { 62, 0 }, /* F5 */ 167 { 63, 0 }, /* F6 -> audio back */ 168 { 64, 0 }, /* F7 -> audio pause/play */ 169 { 65, 0 }, /* F8 -> audio next */ 170 #endif 171 { 66, 127 }, /* F9 -> audio mute */ 172 { 67, 129 }, /* F10 -> audio lower */ 173 { 68, 128 }, /* F11 -> audio raise */ 174 #ifdef notyet 175 { 69, 0 }, /* F12 -> eject */ 176 #endif 177 { 79, 77 }, /* right -> end */ 178 { 80, 74 }, /* left -> home */ 179 { 81, 78 }, /* down -> page down */ 180 { 82, 75 } /* up -> page up */ 181 }; 182 183 static const struct hidkbd_translation apple_iso_trans[] = { 184 { 53, 100 }, /* less -> grave */ 185 { 100, 53 } 186 }; 187 188 #define KEY_ERROR 0x01 189 190 #ifdef HIDKBD_DEBUG 191 #define HIDKBDTRACESIZE 64 192 struct hidkbdtraceinfo { 193 int unit; 194 struct timeval tv; 195 struct hidkbd_data ud; 196 }; 197 struct hidkbdtraceinfo hidkbdtracedata[HIDKBDTRACESIZE]; 198 int hidkbdtraceindex = 0; 199 int hidkbdtrace = 0; 200 void hidkbdtracedump(void); 201 void 202 hidkbdtracedump(void) 203 { 204 int i; 205 for (i = 0; i < HIDKBDTRACESIZE; i++) { 206 struct hidkbdtraceinfo *p = 207 &hidkbdtracedata[(i+hidkbdtraceindex)%HIDKBDTRACESIZE]; 208 printf("%lld.%06ld: key0=0x%02x key1=0x%02x " 209 "key2=0x%02x key3=0x%02x\n", 210 (long long)p->tv.tv_sec, p->tv.tv_usec, 211 p->ud.keycode[0], p->ud.keycode[1], 212 p->ud.keycode[2], p->ud.keycode[3]); 213 } 214 } 215 #endif 216 217 int hidkbd_is_console; 218 219 const char *hidkbd_parse_desc(struct hidkbd *, int, void *, int); 220 221 void (*hidkbd_bell_fn)(void *, u_int, u_int, u_int, int); 222 void *hidkbd_bell_fn_arg; 223 224 void hidkbd_decode(struct hidkbd *, struct hidkbd_data *); 225 void hidkbd_delayed_decode(void *addr); 226 227 extern const struct wscons_keydesc ukbd_keydesctab[]; 228 229 struct wskbd_mapdata ukbd_keymapdata = { 230 ukbd_keydesctab 231 }; 232 233 int 234 hidkbd_attach(struct device *self, struct hidkbd *kbd, int console, 235 uint32_t qflags, int id, void *desc, int dlen) 236 { 237 const char *parserr; 238 239 kbd->sc_var = NULL; 240 241 parserr = hidkbd_parse_desc(kbd, id, desc, dlen); 242 if (parserr != NULL) { 243 printf(": %s\n", parserr); 244 return ENXIO; 245 } 246 247 #ifdef DIAGNOSTIC 248 printf(": %d variable keys, %d key codes", 249 kbd->sc_nvar, kbd->sc_nkeycode); 250 #endif 251 252 kbd->sc_device = self; 253 kbd->sc_debounce = (qflags & HIDKBD_SPUR_BUT_UP) != 0; 254 255 /* 256 * Remember if we're the console keyboard. 257 * 258 * XXX This always picks the first (USB) keyboard to attach, 259 * but what else can we really do? 260 */ 261 if (console) { 262 kbd->sc_console_keyboard = hidkbd_is_console; 263 /* Don't let any other keyboard have it. */ 264 hidkbd_is_console = 0; 265 } 266 267 timeout_set(&kbd->sc_delay, hidkbd_delayed_decode, kbd); 268 269 return 0; 270 } 271 272 void 273 hidkbd_attach_wskbd(struct hidkbd *kbd, kbd_t layout, 274 const struct wskbd_accessops *accessops) 275 { 276 struct wskbddev_attach_args a; 277 278 ukbd_keymapdata.layout = layout; 279 280 a.console = kbd->sc_console_keyboard; 281 a.keymap = &ukbd_keymapdata; 282 a.accessops = accessops; 283 a.accesscookie = kbd->sc_device; 284 kbd->sc_wskbddev = config_found(kbd->sc_device, &a, wskbddevprint); 285 } 286 287 int 288 hidkbd_detach(struct hidkbd *kbd, int flags) 289 { 290 int rv = 0; 291 292 DPRINTF(("hidkbd_detach: sc=%p flags=%d\n", kbd->sc_device, flags)); 293 294 if (kbd->sc_console_keyboard) { 295 /* 296 * Disconnect our consops and set hidkbd_is_console 297 * back to 1 so that the next USB keyboard attached 298 * to the system will get it. 299 * XXX Should notify some other keyboard that it can be 300 * XXX console, if there are any other keyboards. 301 */ 302 printf("%s: was console keyboard\n", 303 kbd->sc_device->dv_xname); 304 hidkbd_is_console = 1; 305 } 306 /* No need to do reference counting of hidkbd, wskbd has all the goo */ 307 if (kbd->sc_wskbddev != NULL) 308 rv = config_detach(kbd->sc_wskbddev, flags); 309 310 if (kbd->sc_var != NULL) 311 free(kbd->sc_var, M_DEVBUF, 0); 312 313 return (rv); 314 } 315 316 uint8_t 317 hidkbd_translate(const struct hidkbd_translation *table, size_t tsize, 318 uint8_t keycode) 319 { 320 for (; tsize != 0; table++, tsize--) 321 if (table->original == keycode) 322 return table->translation; 323 return 0; 324 } 325 326 void 327 hidkbd_apple_translate(void *vsc, uint8_t *ibuf, u_int ilen, 328 const struct hidkbd_translation* trans, u_int tlen) 329 { 330 struct hidkbd *kbd = vsc; 331 uint8_t *pos, *spos, *epos, xlat; 332 333 spos = ibuf + kbd->sc_keycodeloc.pos / 8; 334 epos = spos + kbd->sc_nkeycode; 335 336 for (pos = spos; pos != epos; pos++) { 337 xlat = hidkbd_translate(trans, tlen, *pos); 338 if (xlat != 0) 339 *pos = xlat; 340 } 341 } 342 343 void 344 hidkbd_apple_munge(void *vsc, uint8_t *ibuf, u_int ilen) 345 { 346 struct hidkbd *kbd = vsc; 347 348 if (!hid_get_data(ibuf, ilen, &kbd->sc_fn)) 349 return; 350 351 hidkbd_apple_translate(vsc, ibuf, ilen, apple_fn_trans, 352 nitems(apple_fn_trans)); 353 } 354 355 void 356 hidkbd_apple_tb_munge(void *vsc, uint8_t *ibuf, u_int ilen) 357 { 358 struct hidkbd *kbd = vsc; 359 360 if (!hid_get_data(ibuf, ilen, &kbd->sc_fn)) 361 return; 362 363 hidkbd_apple_munge(vsc, ibuf, ilen); 364 365 hidkbd_apple_translate(vsc, ibuf, ilen, apple_tb_trans, 366 nitems(apple_tb_trans)); 367 } 368 369 void 370 hidkbd_apple_iso_munge(void *vsc, uint8_t *ibuf, u_int ilen) 371 { 372 hidkbd_apple_translate(vsc, ibuf, ilen, apple_iso_trans, 373 nitems(apple_iso_trans)); 374 hidkbd_apple_munge(vsc, ibuf, ilen); 375 } 376 377 void 378 hidkbd_apple_mba_munge(void *vsc, uint8_t *ibuf, u_int ilen) 379 { 380 struct hidkbd *kbd = vsc; 381 382 if (!hid_get_data(ibuf, ilen, &kbd->sc_fn)) 383 return; 384 385 hidkbd_apple_translate(vsc, ibuf, ilen, apple_mba_trans, 386 nitems(apple_mba_trans)); 387 } 388 389 void 390 hidkbd_apple_iso_mba_munge(void *vsc, uint8_t *ibuf, u_int ilen) 391 { 392 hidkbd_apple_translate(vsc, ibuf, ilen, apple_iso_trans, 393 nitems(apple_iso_trans)); 394 hidkbd_apple_mba_munge(vsc, ibuf, ilen); 395 } 396 397 void 398 hidkbd_input(struct hidkbd *kbd, uint8_t *data, u_int len) 399 { 400 struct hidkbd_data *ud = &kbd->sc_ndata; 401 int i; 402 403 if (kbd->sc_munge != NULL) 404 (*kbd->sc_munge)(kbd, (uint8_t *)data, len); 405 406 #ifdef HIDKBD_DEBUG 407 if (hidkbddebug > 5) { 408 printf("hidkbd_input: data"); 409 for (i = 0; i < len; i++) 410 printf(" 0x%02x", data[i]); 411 printf("\n"); 412 } 413 #endif 414 415 /* extract variable keys */ 416 for (i = 0; i < kbd->sc_nvar; i++) 417 ud->var[i] = (u_int8_t)hid_get_data(data, len, 418 &kbd->sc_var[i].loc); 419 420 /* extract keycodes */ 421 memcpy(ud->keycode, data + kbd->sc_keycodeloc.pos / 8, 422 kbd->sc_nkeycode); 423 424 if (kbd->sc_debounce && !kbd->sc_polling) { 425 /* 426 * Some keyboards have a peculiar quirk. They sometimes 427 * generate a key up followed by a key down for the same 428 * key after about 10 ms. 429 * We avoid this bug by holding off decoding for 20 ms. 430 */ 431 kbd->sc_data = *ud; 432 timeout_add_msec(&kbd->sc_delay, 20); 433 } else { 434 hidkbd_decode(kbd, ud); 435 } 436 } 437 438 void 439 hidkbd_delayed_decode(void *addr) 440 { 441 struct hidkbd *kbd = addr; 442 443 hidkbd_decode(kbd, &kbd->sc_data); 444 } 445 446 void 447 hidkbd_decode(struct hidkbd *kbd, struct hidkbd_data *ud) 448 { 449 u_int16_t ibuf[MAXKEYS]; /* chars events */ 450 int s; 451 int nkeys, i, j; 452 int key; 453 #define ADDKEY(c) ibuf[nkeys++] = (c) 454 455 #ifdef HIDKBD_DEBUG 456 /* 457 * Keep a trace of the last events. Using printf changes the 458 * timing, so this can be useful sometimes. 459 */ 460 if (hidkbdtrace) { 461 struct hidkbdtraceinfo *p = &hidkbdtracedata[hidkbdtraceindex]; 462 p->unit = kbd->sc_device->dv_unit; 463 microtime(&p->tv); 464 p->ud = *ud; 465 if (++hidkbdtraceindex >= HIDKBDTRACESIZE) 466 hidkbdtraceindex = 0; 467 } 468 if (hidkbddebug > 5) { 469 struct timeval tv; 470 microtime(&tv); 471 DPRINTF((" at %lld.%06ld key0=0x%02x key1=0x%02x " 472 "key2=0x%02x key3=0x%02x\n", 473 (long long)tv.tv_sec, tv.tv_usec, 474 ud->keycode[0], ud->keycode[1], 475 ud->keycode[2], ud->keycode[3])); 476 } 477 #endif 478 479 if (ud->keycode[0] == KEY_ERROR) { 480 DPRINTF(("hidkbd_input: KEY_ERROR\n")); 481 return; /* ignore */ 482 } 483 nkeys = 0; 484 485 for (i = 0; i < kbd->sc_nvar; i++) 486 if ((kbd->sc_odata.var[i] & kbd->sc_var[i].mask) != 487 (ud->var[i] & kbd->sc_var[i].mask)) { 488 ADDKEY(kbd->sc_var[i].key | 489 ((ud->var[i] & kbd->sc_var[i].mask) ? 490 PRESS : RELEASE)); 491 } 492 493 if (memcmp(ud->keycode, kbd->sc_odata.keycode, kbd->sc_nkeycode) != 0) { 494 /* Check for released keys. */ 495 for (i = 0; i < kbd->sc_nkeycode; i++) { 496 key = kbd->sc_odata.keycode[i]; 497 if (key == 0) 498 continue; 499 for (j = 0; j < kbd->sc_nkeycode; j++) 500 if (key == ud->keycode[j]) 501 goto rfound; 502 DPRINTFN(3,("hidkbd_decode: relse key=0x%02x\n", key)); 503 ADDKEY(key | RELEASE); 504 rfound: 505 ; 506 } 507 508 /* Check for pressed keys. */ 509 for (i = 0; i < kbd->sc_nkeycode; i++) { 510 key = ud->keycode[i]; 511 if (key == 0) 512 continue; 513 for (j = 0; j < kbd->sc_nkeycode; j++) 514 if (key == kbd->sc_odata.keycode[j]) 515 goto pfound; 516 DPRINTFN(2,("hidkbd_decode: press key=0x%02x\n", key)); 517 ADDKEY(key | PRESS); 518 pfound: 519 ; 520 } 521 } 522 kbd->sc_odata = *ud; 523 524 if (nkeys == 0) 525 return; 526 527 if (kbd->sc_polling) { 528 DPRINTFN(1,("hidkbd_decode: pollchar = 0x%03x\n", ibuf[0])); 529 memcpy(kbd->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t)); 530 kbd->sc_npollchar = nkeys; 531 return; 532 } 533 534 if (kbd->sc_wskbddev == NULL) 535 return; 536 537 #ifdef WSDISPLAY_COMPAT_RAWKBD 538 if (kbd->sc_rawkbd) { 539 u_char cbuf[MAXKEYS * 2]; 540 int c; 541 542 for (i = j = 0; i < nkeys; i++) { 543 key = ibuf[i]; 544 c = hidkbd_trtab[key & CODEMASK]; 545 if (c == NN) 546 continue; 547 if (c & 0x80) 548 cbuf[j++] = 0xe0; 549 cbuf[j] = c & 0x7f; 550 if (key & RELEASE) 551 cbuf[j] |= 0x80; 552 DPRINTFN(1,("hidkbd_decode: raw = %s0x%02x\n", 553 c & 0x80 ? "0xe0 " : "", 554 cbuf[j])); 555 j++; 556 } 557 s = spltty(); 558 wskbd_rawinput(kbd->sc_wskbddev, cbuf, j); 559 560 /* 561 * Pass audio, brightness and sleep keys to wskbd_input anyway. 562 */ 563 for (i = 0; i < nkeys; i++) { 564 key = ibuf[i]; 565 switch (key & CODEMASK) { 566 case 102: 567 case 127: 568 case 128: 569 case 129: 570 case 232: 571 case 233: 572 wskbd_input(kbd->sc_wskbddev, 573 key & RELEASE ? WSCONS_EVENT_KEY_UP : 574 WSCONS_EVENT_KEY_DOWN, key & CODEMASK); 575 break; 576 } 577 } 578 splx(s); 579 580 return; 581 } 582 #endif 583 584 s = spltty(); 585 for (i = 0; i < nkeys; i++) { 586 key = ibuf[i]; 587 wskbd_input(kbd->sc_wskbddev, 588 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN, 589 key&CODEMASK); 590 } 591 splx(s); 592 #undef ADDKEY 593 } 594 595 int 596 hidkbd_enable(struct hidkbd *kbd, int on) 597 { 598 if (kbd->sc_enabled == on) 599 return EBUSY; 600 601 kbd->sc_enabled = on; 602 return 0; 603 } 604 605 int 606 hidkbd_set_leds(struct hidkbd *kbd, int leds, uint8_t *report) 607 { 608 if (kbd->sc_leds == leds) 609 return 0; 610 611 kbd->sc_leds = leds; 612 613 /* 614 * This is not totally correct, since we did not check the 615 * report size from the descriptor but for keyboards it should 616 * just be a single byte with the relevant bits set. 617 */ 618 *report = 0; 619 if ((leds & WSKBD_LED_SCROLL) && kbd->sc_scroloc.size == 1) 620 *report |= 1 << kbd->sc_scroloc.pos; 621 if ((leds & WSKBD_LED_NUM) && kbd->sc_numloc.size == 1) 622 *report |= 1 << kbd->sc_numloc.pos; 623 if ((leds & WSKBD_LED_CAPS) && kbd->sc_capsloc.size == 1) 624 *report |= 1 << kbd->sc_capsloc.pos; 625 if ((leds & WSKBD_LED_COMPOSE) && kbd->sc_compose.size == 1) 626 *report |= 1 << kbd->sc_compose.pos; 627 628 return 1; 629 } 630 631 int 632 hidkbd_ioctl(struct hidkbd *kbd, u_long cmd, caddr_t data, int flag, 633 struct proc *p) 634 { 635 switch (cmd) { 636 case WSKBDIO_GETLEDS: 637 *(int *)data = kbd->sc_leds; 638 return (0); 639 case WSKBDIO_COMPLEXBELL: 640 #define d ((struct wskbd_bell_data *)data) 641 hidkbd_bell(d->pitch, d->period, d->volume, 0); 642 #undef d 643 return (0); 644 #ifdef WSDISPLAY_COMPAT_RAWKBD 645 case WSKBDIO_SETMODE: 646 DPRINTF(("hidkbd_ioctl: set raw = %d\n", *(int *)data)); 647 kbd->sc_rawkbd = *(int *)data == WSKBD_RAW; 648 return (0); 649 #endif 650 } 651 return (-1); 652 } 653 654 void 655 hidkbd_cngetc(struct hidkbd *kbd, u_int *type, int *data) 656 { 657 int c; 658 659 c = kbd->sc_pollchars[0]; 660 kbd->sc_npollchar--; 661 memmove(kbd->sc_pollchars, kbd->sc_pollchars+1, 662 kbd->sc_npollchar * sizeof(u_int16_t)); 663 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN; 664 *data = c & CODEMASK; 665 } 666 667 void 668 hidkbd_bell(u_int pitch, u_int period, u_int volume, int poll) 669 { 670 if (hidkbd_bell_fn != NULL) 671 (*hidkbd_bell_fn)(hidkbd_bell_fn_arg, pitch, period, 672 volume, poll); 673 } 674 675 void 676 hidkbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg) 677 { 678 if (hidkbd_bell_fn == NULL) { 679 hidkbd_bell_fn = fn; 680 hidkbd_bell_fn_arg = arg; 681 } 682 } 683 684 const char * 685 hidkbd_parse_desc(struct hidkbd *kbd, int id, void *desc, int dlen) 686 { 687 struct hid_data *d; 688 struct hid_item h; 689 unsigned int i, ivar = 0; 690 691 kbd->sc_nkeycode = 0; 692 693 d = hid_start_parse(desc, dlen, hid_input); 694 while (hid_get_item(d, &h)) { 695 if (h.kind != hid_input || (h.flags & HIO_CONST) || 696 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD || 697 h.report_ID != id) 698 continue; 699 if (h.flags & HIO_VARIABLE) 700 ivar++; 701 } 702 hid_end_parse(d); 703 704 if (ivar > MAXVARS) { 705 DPRINTF((": too many variable keys\n")); 706 ivar = MAXVARS; 707 } 708 709 kbd->sc_nvar = ivar; 710 kbd->sc_var = (struct hidkbd_variable *)mallocarray(ivar, 711 sizeof(struct hidkbd_variable), M_DEVBUF, M_NOWAIT); 712 713 if (!kbd->sc_var) 714 return NULL; 715 716 i = 0; 717 718 d = hid_start_parse(desc, dlen, hid_input); 719 while (hid_get_item(d, &h)) { 720 if (h.kind != hid_input || (h.flags & HIO_CONST) || 721 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD || 722 h.report_ID != id) 723 continue; 724 725 DPRINTF(("hidkbd: usage=0x%x flags=0x%x pos=%d size=%d cnt=%d", 726 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count)); 727 if (h.flags & HIO_VARIABLE) { 728 /* variable reports should be one bit each */ 729 if (h.loc.size != 1) { 730 DPRINTF((": bad variable size\n")); 731 continue; 732 } 733 734 /* variable report */ 735 if (i < MAXVARS) { 736 kbd->sc_var[i].loc = h.loc; 737 kbd->sc_var[i].mask = 1 << (i % 8); 738 kbd->sc_var[i].key = HID_GET_USAGE(h.usage); 739 i++; 740 } 741 } else { 742 /* keys array should be in bytes, on a byte boundary */ 743 if (h.loc.size != 8) { 744 DPRINTF((": key code size != 8\n")); 745 continue; 746 } 747 if (h.loc.pos % 8 != 0) { 748 DPRINTF((": array not on byte boundary")); 749 continue; 750 } 751 if (kbd->sc_nkeycode != 0) { 752 DPRINTF((": ignoring multiple arrays\n")); 753 continue; 754 } 755 kbd->sc_keycodeloc = h.loc; 756 if (h.loc.count > MAXKEYCODE) { 757 DPRINTF((": ignoring extra key codes")); 758 kbd->sc_nkeycode = MAXKEYCODE; 759 } else 760 kbd->sc_nkeycode = h.loc.count; 761 } 762 DPRINTF(("\n")); 763 } 764 hid_end_parse(d); 765 766 /* don't attach if no keys... */ 767 if (kbd->sc_nkeycode == 0 && ivar == 0) 768 return "no usable key codes array"; 769 770 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_NUM_LOCK), 771 id, hid_output, &kbd->sc_numloc, NULL); 772 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_CAPS_LOCK), 773 id, hid_output, &kbd->sc_capsloc, NULL); 774 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_SCROLL_LOCK), 775 id, hid_output, &kbd->sc_scroloc, NULL); 776 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_COMPOSE), 777 id, hid_output, &kbd->sc_compose, NULL); 778 779 return (NULL); 780 } 781