1 /* $NetBSD: btkbd.c,v 1.17 2014/11/16 16:20:00 ozaki-r Exp $ */ 2 3 /* 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /*- 34 * Copyright (c) 2006 Itronix Inc. 35 * All rights reserved. 36 * 37 * Written by Iain Hibbert for Itronix Inc. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. The name of Itronix Inc. may not be used to endorse 48 * or promote products derived from this software without specific 49 * prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 53 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 54 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 55 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 58 * ON ANY THEORY OF LIABILITY, WHETHER IN 59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 61 * POSSIBILITY OF SUCH DAMAGE. 62 */ 63 64 /* 65 * based on dev/usb/ukbd.c 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: btkbd.c,v 1.17 2014/11/16 16:20:00 ozaki-r Exp $"); 70 71 #include <sys/param.h> 72 #include <sys/callout.h> 73 #include <sys/conf.h> 74 #include <sys/device.h> 75 #include <sys/kernel.h> 76 #include <sys/proc.h> 77 #include <sys/systm.h> 78 79 #include <netbt/bluetooth.h> 80 81 #include <dev/bluetooth/bthid.h> 82 #include <dev/bluetooth/bthidev.h> 83 84 #include <dev/usb/hid.h> 85 #include <dev/usb/usb.h> 86 #include <dev/usb/usbhid.h> 87 88 #include <dev/wscons/wsconsio.h> 89 #include <dev/wscons/wskbdvar.h> 90 #include <dev/wscons/wsksymdef.h> 91 #include <dev/wscons/wsksymvar.h> 92 93 #include "opt_wsdisplay_compat.h" 94 #include "opt_btkbd.h" 95 96 #define MAXKEYCODE 6 97 #define MAXMOD 8 /* max 32 */ 98 #define MAXKEYS (MAXMOD + (2 * MAXKEYCODE)) 99 100 struct btkbd_data { 101 uint32_t modifiers; 102 uint8_t keycode[MAXKEYCODE]; 103 }; 104 105 struct btkbd_mod { 106 uint32_t mask; 107 uint8_t key; 108 }; 109 110 struct btkbd_softc { 111 struct bthidev sc_hidev; /* device+ */ 112 device_t sc_wskbd; /* child */ 113 int sc_enabled; 114 115 int (*sc_output) /* output method */ 116 (struct bthidev *, uint8_t *, int); 117 118 /* stored data */ 119 struct btkbd_data sc_odata; 120 struct btkbd_data sc_ndata; 121 122 /* input reports */ 123 int sc_nmod; 124 struct hid_location sc_modloc[MAXMOD]; 125 struct btkbd_mod sc_mods[MAXMOD]; 126 127 int sc_nkeycode; 128 struct hid_location sc_keycodeloc; 129 130 /* output reports */ 131 struct hid_location sc_numloc; 132 struct hid_location sc_capsloc; 133 struct hid_location sc_scroloc; 134 int sc_leds; 135 136 #ifdef WSDISPLAY_COMPAT_RAWKBD 137 int sc_rawkbd; 138 #ifdef BTKBD_REPEAT 139 callout_t sc_repeat; 140 int sc_nrep; 141 char sc_rep[MAXKEYS]; 142 #endif 143 #endif 144 }; 145 146 /* autoconf(9) methods */ 147 static int btkbd_match(device_t, cfdata_t, void *); 148 static void btkbd_attach(device_t, device_t, void *); 149 static int btkbd_detach(device_t, int); 150 151 CFATTACH_DECL_NEW(btkbd, sizeof(struct btkbd_softc), 152 btkbd_match, btkbd_attach, btkbd_detach, NULL); 153 154 /* wskbd(4) accessops */ 155 static int btkbd_enable(void *, int); 156 static void btkbd_set_leds(void *, int); 157 static int btkbd_ioctl(void *, unsigned long, void *, int, struct lwp *); 158 159 static const struct wskbd_accessops btkbd_accessops = { 160 btkbd_enable, 161 btkbd_set_leds, 162 btkbd_ioctl 163 }; 164 165 /* wskbd(4) keymap data */ 166 extern const struct wscons_keydesc ukbd_keydesctab[]; 167 168 const struct wskbd_mapdata btkbd_keymapdata = { 169 ukbd_keydesctab, 170 #if defined(BTKBD_LAYOUT) 171 BTKBD_LAYOUT, 172 #elif defined(PCKBD_LAYOUT) 173 PCKBD_LAYOUT, 174 #else 175 KB_US, 176 #endif 177 }; 178 179 /* bthid methods */ 180 static void btkbd_input(struct bthidev *, uint8_t *, int); 181 182 /* internal prototypes */ 183 static const char *btkbd_parse_desc(struct btkbd_softc *, int, const void *, int); 184 185 #ifdef WSDISPLAY_COMPAT_RAWKBD 186 #ifdef BTKBD_REPEAT 187 static void btkbd_repeat(void *); 188 #endif 189 #endif 190 191 /***************************************************************************** 192 * 193 * btkbd autoconf(9) routines 194 */ 195 196 static int 197 btkbd_match(device_t self, cfdata_t cfdata, void *aux) 198 { 199 struct bthidev_attach_args *ba = aux; 200 201 if (hid_is_collection(ba->ba_desc, ba->ba_dlen, ba->ba_id, 202 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) 203 return 1; 204 205 return 0; 206 } 207 208 static void 209 btkbd_attach(device_t parent, device_t self, void *aux) 210 { 211 struct btkbd_softc *sc = device_private(self); 212 struct bthidev_attach_args *ba = aux; 213 struct wskbddev_attach_args wska; 214 const char *parserr; 215 216 sc->sc_output = ba->ba_output; 217 ba->ba_input = btkbd_input; 218 219 parserr = btkbd_parse_desc(sc, ba->ba_id, ba->ba_desc, ba->ba_dlen); 220 if (parserr != NULL) { 221 aprint_error("%s\n", parserr); 222 return; 223 } 224 225 aprint_normal("\n"); 226 227 #ifdef WSDISPLAY_COMPAT_RAWKBD 228 #ifdef BTKBD_REPEAT 229 callout_init(&sc->sc_repeat, 0); 230 callout_setfunc(&sc->sc_repeat, btkbd_repeat, sc); 231 #endif 232 #endif 233 234 wska.console = 0; 235 wska.keymap = &btkbd_keymapdata; 236 wska.accessops = &btkbd_accessops; 237 wska.accesscookie = sc; 238 239 sc->sc_wskbd = config_found(self, &wska, wskbddevprint); 240 241 pmf_device_register(self, NULL, NULL); 242 } 243 244 static int 245 btkbd_detach(device_t self, int flags) 246 { 247 struct btkbd_softc *sc = device_private(self); 248 int err = 0; 249 250 pmf_device_deregister(self); 251 252 #ifdef WSDISPLAY_COMPAT_RAWKBD 253 #ifdef BTKBD_REPEAT 254 callout_halt(&sc->sc_repeat, NULL); 255 callout_destroy(&sc->sc_repeat); 256 #endif 257 #endif 258 259 if (sc->sc_wskbd != NULL) { 260 err = config_detach(sc->sc_wskbd, flags); 261 sc->sc_wskbd = NULL; 262 } 263 264 return err; 265 } 266 267 static const char * 268 btkbd_parse_desc(struct btkbd_softc *sc, int id, const void *desc, int dlen) 269 { 270 struct hid_data *d; 271 struct hid_item h; 272 int imod; 273 274 imod = 0; 275 sc->sc_nkeycode = 0; 276 d = hid_start_parse(desc, dlen, hid_input); 277 while (hid_get_item(d, &h)) { 278 if (h.kind != hid_input || (h.flags & HIO_CONST) || 279 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD || 280 h.report_ID != id) 281 continue; 282 283 if (h.flags & HIO_VARIABLE) { 284 if (h.loc.size != 1) 285 return ("bad modifier size"); 286 287 /* Single item */ 288 if (imod < MAXMOD) { 289 sc->sc_modloc[imod] = h.loc; 290 sc->sc_mods[imod].mask = 1 << imod; 291 sc->sc_mods[imod].key = HID_GET_USAGE(h.usage); 292 imod++; 293 } else 294 return ("too many modifier keys"); 295 } else { 296 /* Array */ 297 if (h.loc.size != 8) 298 return ("key code size != 8"); 299 300 if (h.loc.count > MAXKEYCODE) 301 return ("too many key codes"); 302 303 if (h.loc.pos % 8 != 0) 304 return ("key codes not on byte boundary"); 305 306 if (sc->sc_nkeycode != 0) 307 return ("multiple key code arrays\n"); 308 309 sc->sc_keycodeloc = h.loc; 310 sc->sc_nkeycode = h.loc.count; 311 } 312 } 313 sc->sc_nmod = imod; 314 hid_end_parse(d); 315 316 hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK), 317 id, hid_output, &sc->sc_numloc, NULL); 318 319 hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK), 320 id, hid_output, &sc->sc_capsloc, NULL); 321 322 hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK), 323 id, hid_output, &sc->sc_scroloc, NULL); 324 325 return (NULL); 326 } 327 328 /***************************************************************************** 329 * 330 * wskbd(9) accessops 331 */ 332 333 static int 334 btkbd_enable(void *cookie, int on) 335 { 336 struct btkbd_softc *sc = cookie; 337 338 sc->sc_enabled = on; 339 return 0; 340 } 341 342 static void 343 btkbd_set_leds(void *cookie, int leds) 344 { 345 struct btkbd_softc *sc = cookie; 346 uint8_t report; 347 348 if (sc->sc_leds == leds) 349 return; 350 351 sc->sc_leds = leds; 352 353 /* 354 * This is not totally correct, since we did not check the 355 * report size from the descriptor but for keyboards it should 356 * just be a single byte with the relevant bits set. 357 */ 358 report = 0; 359 if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1) 360 report |= 1 << sc->sc_scroloc.pos; 361 362 if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1) 363 report |= 1 << sc->sc_numloc.pos; 364 365 if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1) 366 report |= 1 << sc->sc_capsloc.pos; 367 368 if (sc->sc_output) 369 (*sc->sc_output)(&sc->sc_hidev, &report, sizeof(report)); 370 } 371 372 static int 373 btkbd_ioctl(void *cookie, unsigned long cmd, void *data, int flag, 374 struct lwp *l) 375 { 376 struct btkbd_softc *sc = cookie; 377 378 switch (cmd) { 379 case WSKBDIO_GTYPE: 380 *(int *)data = WSKBD_TYPE_BLUETOOTH; 381 break; 382 383 case WSKBDIO_SETLEDS: 384 btkbd_set_leds(sc, *(int *)data); 385 break; 386 387 case WSKBDIO_GETLEDS: 388 *(int *)data = sc->sc_leds; 389 break; 390 391 #ifdef WSDISPLAY_COMPAT_RAWKBD 392 case WSKBDIO_SETMODE: 393 sc->sc_rawkbd = (*(int *)data == WSKBD_RAW); 394 #ifdef BTKBD_REPEAT 395 callout_stop(&sc->sc_repeat); 396 #endif 397 break; 398 #endif 399 400 default: 401 return EPASSTHROUGH; 402 } 403 404 return 0; 405 } 406 407 /***************************************************************************** 408 * 409 * btkbd input routine, called from our parent 410 */ 411 412 #ifdef WSDISPLAY_COMPAT_RAWKBD 413 #define NN 0 /* no translation */ 414 /* 415 * Translate USB keycodes to US keyboard XT scancodes. 416 * Scancodes >= 0x80 represent EXTENDED keycodes. 417 * 418 * See http://www.microsoft.com/HWDEV/TECH/input/Scancode.asp 419 */ 420 static const u_int8_t btkbd_trtab[256] = { 421 NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */ 422 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */ 423 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */ 424 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */ 425 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */ 426 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */ 427 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */ 428 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */ 429 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */ 430 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */ 431 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */ 432 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */ 433 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, NN, 0x59, /* 60 - 67 */ 434 0x5d, 0x5e, 0x5f, NN, NN, NN, NN, NN, /* 68 - 6f */ 435 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */ 436 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7f */ 437 NN, NN, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */ 438 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */ 439 NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */ 440 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */ 441 NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */ 442 NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */ 443 NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */ 444 NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */ 445 NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */ 446 NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */ 447 NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */ 448 NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */ 449 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */ 450 NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */ 451 NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */ 452 NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */ 453 }; 454 #endif 455 456 #define KEY_ERROR 0x01 457 #define PRESS 0x000 458 #define RELEASE 0x100 459 #define CODEMASK 0x0ff 460 #define ADDKEY(c) ibuf[nkeys++] = (c) 461 #define REP_DELAY1 400 462 #define REP_DELAYN 100 463 464 static void 465 btkbd_input(struct bthidev *hidev, uint8_t *data, int len) 466 { 467 struct btkbd_softc *sc = (struct btkbd_softc *)hidev; 468 struct btkbd_data *ud = &sc->sc_ndata; 469 uint16_t ibuf[MAXKEYS]; 470 uint32_t mod, omod; 471 int nkeys, i, j; 472 int key; 473 int s; 474 475 if (sc->sc_wskbd == NULL || sc->sc_enabled == 0) 476 return; 477 478 /* extract key modifiers */ 479 ud->modifiers = 0; 480 for (i = 0 ; i < sc->sc_nmod ; i++) 481 if (hid_get_data(data, &sc->sc_modloc[i])) 482 ud->modifiers |= sc->sc_mods[i].mask; 483 484 /* extract keycodes */ 485 memcpy(ud->keycode, data + (sc->sc_keycodeloc.pos / 8), 486 sc->sc_nkeycode); 487 488 if (ud->keycode[0] == KEY_ERROR) 489 return; /* ignore */ 490 491 nkeys = 0; 492 mod = ud->modifiers; 493 omod = sc->sc_odata.modifiers; 494 if (mod != omod) 495 for (i = 0 ; i < sc->sc_nmod ; i++) 496 if ((mod & sc->sc_mods[i].mask) != 497 (omod & sc->sc_mods[i].mask)) 498 ADDKEY(sc->sc_mods[i].key | 499 (mod & sc->sc_mods[i].mask 500 ? PRESS : RELEASE)); 501 502 if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) { 503 /* Check for released keys. */ 504 for (i = 0 ; i < sc->sc_nkeycode ; i++) { 505 key = sc->sc_odata.keycode[i]; 506 if (key == 0) 507 continue; 508 509 for (j = 0 ; j < sc->sc_nkeycode ; j++) 510 if (key == ud->keycode[j]) 511 goto rfound; 512 513 ADDKEY(key | RELEASE); 514 515 rfound: 516 ; 517 } 518 519 /* Check for pressed keys. */ 520 for (i = 0 ; i < sc->sc_nkeycode ; i++) { 521 key = ud->keycode[i]; 522 if (key == 0) 523 continue; 524 525 for (j = 0; j < sc->sc_nkeycode; j++) 526 if (key == sc->sc_odata.keycode[j]) 527 goto pfound; 528 529 ADDKEY(key | PRESS); 530 pfound: 531 ; 532 } 533 } 534 sc->sc_odata = *ud; 535 536 if (nkeys == 0) 537 return; 538 539 #ifdef WSDISPLAY_COMPAT_RAWKBD 540 if (sc->sc_rawkbd) { 541 u_char cbuf[MAXKEYS * 2]; 542 int c; 543 #ifdef BTKBD_REPEAT 544 int npress = 0; 545 #endif 546 547 for (i = j = 0 ; i < nkeys ; i++) { 548 key = ibuf[i]; 549 c = btkbd_trtab[key & CODEMASK]; 550 if (c == NN) 551 continue; 552 553 if (c & 0x80) 554 cbuf[j++] = 0xe0; 555 556 cbuf[j] = c & 0x7f; 557 if (key & RELEASE) 558 cbuf[j] |= 0x80; 559 #ifdef BTKBD_REPEAT 560 else { 561 /* remember pressed keys for autorepeat */ 562 if (c & 0x80) 563 sc->sc_rep[npress++] = 0xe0; 564 565 sc->sc_rep[npress++] = c & 0x7f; 566 } 567 #endif 568 569 j++; 570 } 571 572 s = spltty(); 573 wskbd_rawinput(sc->sc_wskbd, cbuf, j); 574 splx(s); 575 #ifdef BTKBD_REPEAT 576 callout_stop(&sc->sc_repeat); 577 if (npress != 0) { 578 sc->sc_nrep = npress; 579 callout_schedule(&sc->sc_repeat, hz * REP_DELAY1 / 1000); 580 } 581 #endif 582 return; 583 } 584 #endif 585 586 s = spltty(); 587 for (i = 0 ; i < nkeys ; i++) { 588 key = ibuf[i]; 589 wskbd_input(sc->sc_wskbd, 590 key & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN, 591 key & CODEMASK); 592 } 593 splx(s); 594 } 595 596 #ifdef WSDISPLAY_COMPAT_RAWKBD 597 #ifdef BTKBD_REPEAT 598 static void 599 btkbd_repeat(void *arg) 600 { 601 struct btkbd_softc *sc = arg; 602 int s; 603 604 s = spltty(); 605 wskbd_rawinput(sc->sc_wskbd, sc->sc_rep, sc->sc_nrep); 606 splx(s); 607 callout_schedule(&sc->sc_repeat, hz * REP_DELAYN / 1000); 608 } 609 #endif 610 #endif 611