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