1 /* $OpenBSD: ukbd.c,v 1.67 2014/05/12 09:50:44 mpi Exp $ */ 2 /* $NetBSD: ukbd.c,v 1.85 2003/03/11 16:44:00 augustss Exp $ */ 3 4 /* 5 * Copyright (c) 2010 Miodrag Vallat. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 /* 20 * Copyright (c) 1998 The NetBSD Foundation, Inc. 21 * All rights reserved. 22 * 23 * This code is derived from software contributed to The NetBSD Foundation 24 * by Lennart Augustsson (lennart@augustsson.net) at 25 * Carlstedt Research & Technology. 26 * 27 * Redistribution and use in source and binary forms, with or without 28 * modification, are permitted provided that the following conditions 29 * are met: 30 * 1. Redistributions of source code must retain the above copyright 31 * notice, this list of conditions and the following disclaimer. 32 * 2. Redistributions in binary form must reproduce the above copyright 33 * notice, this list of conditions and the following disclaimer in the 34 * documentation and/or other materials provided with the distribution. 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 37 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 38 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 40 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 43 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 44 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 46 * POSSIBILITY OF SUCH DAMAGE. 47 */ 48 49 /* 50 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 51 */ 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/timeout.h> 56 #include <sys/kernel.h> 57 #include <sys/device.h> 58 #include <sys/ioctl.h> 59 60 #include <dev/usb/usb.h> 61 #include <dev/usb/usbhid.h> 62 63 #include <dev/usb/usbdi.h> 64 #include <dev/usb/usbdi_util.h> 65 #include <dev/usb/usbdevs.h> 66 #include <dev/usb/usb_quirks.h> 67 #include <dev/usb/uhidev.h> 68 #include <dev/usb/hid.h> 69 #include <dev/usb/ukbdvar.h> 70 71 #include <dev/wscons/wsconsio.h> 72 #include <dev/wscons/wskbdvar.h> 73 #include <dev/wscons/wsksymdef.h> 74 #include <dev/wscons/wsksymvar.h> 75 76 #include <dev/usb/hidkbdsc.h> 77 #include <dev/usb/hidkbdvar.h> 78 79 #ifdef UKBD_DEBUG 80 #define DPRINTF(x) do { if (ukbddebug) printf x; } while (0) 81 #define DPRINTFN(n,x) do { if (ukbddebug>(n)) printf x; } while (0) 82 int ukbddebug = 0; 83 #else 84 #define DPRINTF(x) 85 #define DPRINTFN(n,x) 86 #endif 87 88 const kbd_t ukbd_countrylayout[1 + HCC_MAX] = { 89 (kbd_t)-1, 90 (kbd_t)-1, /* arabic */ 91 KB_BE, /* belgian */ 92 (kbd_t)-1, /* canadian bilingual */ 93 KB_CF, /* canadian french */ 94 (kbd_t)-1, /* czech */ 95 KB_DK, /* danish */ 96 (kbd_t)-1, /* finnish */ 97 KB_FR, /* french */ 98 KB_DE, /* german */ 99 (kbd_t)-1, /* greek */ 100 (kbd_t)-1, /* hebrew */ 101 KB_HU, /* hungary */ 102 (kbd_t)-1, /* international (iso) */ 103 KB_IT, /* italian */ 104 KB_JP, /* japanese (katakana) */ 105 (kbd_t)-1, /* korean */ 106 KB_LA, /* latin american */ 107 (kbd_t)-1, /* netherlands/dutch */ 108 KB_NO, /* norwegian */ 109 (kbd_t)-1, /* persian (farsi) */ 110 KB_PL, /* polish */ 111 KB_PT, /* portuguese */ 112 KB_RU, /* russian */ 113 (kbd_t)-1, /* slovakia */ 114 KB_ES, /* spanish */ 115 KB_SV, /* swedish */ 116 KB_SF, /* swiss french */ 117 KB_SG, /* swiss german */ 118 (kbd_t)-1, /* switzerland */ 119 (kbd_t)-1, /* taiwan */ 120 KB_TR, /* turkish Q */ 121 KB_UK, /* uk */ 122 KB_US, /* us */ 123 (kbd_t)-1, /* yugoslavia */ 124 (kbd_t)-1 /* turkish F */ 125 }; 126 127 struct ukbd_softc { 128 struct uhidev sc_hdev; 129 #define sc_ledsize sc_hdev.sc_osize 130 131 struct hidkbd sc_kbd; 132 133 int sc_spl; 134 135 struct hid_location sc_apple_fn; 136 137 void (*sc_munge)(void *, uint8_t *, u_int); 138 }; 139 140 void ukbd_cngetc(void *, u_int *, int *); 141 void ukbd_cnpollc(void *, int); 142 void ukbd_cnbell(void *, u_int, u_int, u_int); 143 144 const struct wskbd_consops ukbd_consops = { 145 ukbd_cngetc, 146 ukbd_cnpollc, 147 ukbd_cnbell, 148 }; 149 150 void ukbd_intr(struct uhidev *addr, void *ibuf, u_int len); 151 152 int ukbd_enable(void *, int); 153 void ukbd_set_leds(void *, int); 154 int ukbd_ioctl(void *, u_long, caddr_t, int, struct proc *); 155 156 const struct wskbd_accessops ukbd_accessops = { 157 ukbd_enable, 158 ukbd_set_leds, 159 ukbd_ioctl, 160 }; 161 162 int ukbd_match(struct device *, void *, void *); 163 void ukbd_attach(struct device *, struct device *, void *); 164 int ukbd_detach(struct device *, int); 165 int ukbd_activate(struct device *, int); 166 167 struct cfdriver ukbd_cd = { 168 NULL, "ukbd", DV_DULL 169 }; 170 171 const struct cfattach ukbd_ca = { 172 sizeof(struct ukbd_softc), 173 ukbd_match, 174 ukbd_attach, 175 ukbd_detach, 176 ukbd_activate, 177 }; 178 179 struct ukbd_translation { 180 uint8_t original; 181 uint8_t translation; 182 }; 183 184 #ifdef __loongson__ 185 void ukbd_gdium_munge(void *, uint8_t *, u_int); 186 #endif 187 void ukbd_apple_munge(void *, uint8_t *, u_int); 188 void ukbd_apple_iso_munge(void *, uint8_t *, u_int); 189 uint8_t ukbd_translate(const struct ukbd_translation *, size_t, uint8_t); 190 191 int 192 ukbd_match(struct device *parent, void *match, void *aux) 193 { 194 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux; 195 int size; 196 void *desc; 197 198 uhidev_get_report_desc(uha->parent, &desc, &size); 199 if (!hid_is_collection(desc, size, uha->reportid, 200 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) 201 return (UMATCH_NONE); 202 203 return (UMATCH_IFACECLASS); 204 } 205 206 void 207 ukbd_attach(struct device *parent, struct device *self, void *aux) 208 { 209 struct ukbd_softc *sc = (struct ukbd_softc *)self; 210 struct hidkbd *kbd = &sc->sc_kbd; 211 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux; 212 struct usb_hid_descriptor *hid; 213 u_int32_t qflags; 214 int dlen, repid; 215 int console = 1; 216 void *desc; 217 kbd_t layout = (kbd_t)-1; 218 219 sc->sc_hdev.sc_intr = ukbd_intr; 220 sc->sc_hdev.sc_parent = uha->parent; 221 sc->sc_hdev.sc_udev = uha->uaa->device; 222 sc->sc_hdev.sc_report_id = uha->reportid; 223 224 uhidev_get_report_desc(uha->parent, &desc, &dlen); 225 repid = uha->reportid; 226 sc->sc_hdev.sc_isize = hid_report_size(desc, dlen, hid_input, repid); 227 sc->sc_hdev.sc_osize = hid_report_size(desc, dlen, hid_output, repid); 228 sc->sc_hdev.sc_fsize = hid_report_size(desc, dlen, hid_feature, repid); 229 230 /* 231 * Since the HID-Proxy is always detected before any 232 * real keyboard, do not let it grab the console. 233 */ 234 if (uha->uaa->vendor == USB_VENDOR_APPLE && 235 uha->uaa->product == USB_PRODUCT_APPLE_BLUETOOTH_HCI) 236 console = 0; 237 238 qflags = usbd_get_quirks(sc->sc_hdev.sc_udev)->uq_flags; 239 if (hidkbd_attach(self, kbd, console, qflags, repid, desc, dlen) != 0) 240 return; 241 242 if (uha->uaa->vendor == USB_VENDOR_APPLE) { 243 int iso = 0; 244 245 if ((uha->uaa->product == USB_PRODUCT_APPLE_FOUNTAIN_ISO) || 246 (uha->uaa->product == USB_PRODUCT_APPLE_GEYSER_ISO)) 247 iso = 1; 248 249 if (hid_locate(desc, dlen, HID_USAGE2(HUP_APPLE, HUG_FN_KEY), 250 uha->reportid, hid_input, &sc->sc_apple_fn, &qflags)) { 251 if (qflags & HIO_VARIABLE) { 252 if (iso) 253 sc->sc_munge = ukbd_apple_iso_munge; 254 else 255 sc->sc_munge = ukbd_apple_munge; 256 } 257 } 258 } 259 260 if (uha->uaa->vendor == USB_VENDOR_TOPRE && 261 uha->uaa->product == USB_PRODUCT_TOPRE_HHKB) { 262 /* ignore country code on purpose */ 263 } else { 264 usb_interface_descriptor_t *id; 265 266 id = usbd_get_interface_descriptor(uha->uaa->iface); 267 hid = usbd_get_hid_descriptor(uha->uaa->device, id); 268 269 if (hid->bCountryCode <= HCC_MAX) 270 layout = ukbd_countrylayout[hid->bCountryCode]; 271 #ifdef DIAGNOSTIC 272 if (hid->bCountryCode != 0) 273 printf(", country code %d", hid->bCountryCode); 274 #endif 275 } 276 if (layout == (kbd_t)-1) { 277 #ifdef UKBD_LAYOUT 278 layout = UKBD_LAYOUT; 279 #else 280 layout = KB_US | KB_DEFAULT; 281 #endif 282 } 283 284 printf("\n"); 285 286 #ifdef __loongson__ 287 if (uha->uaa->vendor == USB_VENDOR_CYPRESS && 288 uha->uaa->product == USB_PRODUCT_CYPRESS_LPRDK) 289 sc->sc_munge = ukbd_gdium_munge; 290 #endif 291 292 if (kbd->sc_console_keyboard) { 293 extern struct wskbd_mapdata ukbd_keymapdata; 294 295 DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc)); 296 ukbd_keymapdata.layout = layout; 297 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata); 298 ukbd_enable(sc, 1); 299 } 300 301 /* Flash the leds; no real purpose, just shows we're alive. */ 302 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | 303 WSKBD_LED_CAPS | WSKBD_LED_COMPOSE); 304 usbd_delay_ms(sc->sc_hdev.sc_udev, 400); 305 ukbd_set_leds(sc, 0); 306 307 hidkbd_attach_wskbd(kbd, layout, &ukbd_accessops); 308 } 309 310 int 311 ukbd_activate(struct device *self, int act) 312 { 313 struct ukbd_softc *sc = (struct ukbd_softc *)self; 314 struct hidkbd *kbd = &sc->sc_kbd; 315 int rv = 0; 316 317 switch (act) { 318 case DVACT_DEACTIVATE: 319 if (kbd->sc_wskbddev != NULL) 320 rv = config_deactivate(kbd->sc_wskbddev); 321 break; 322 } 323 return (rv); 324 } 325 326 int 327 ukbd_detach(struct device *self, int flags) 328 { 329 struct ukbd_softc *sc = (struct ukbd_softc *)self; 330 struct hidkbd *kbd = &sc->sc_kbd; 331 int rv; 332 333 rv = hidkbd_detach(kbd, flags); 334 335 /* The console keyboard does not get a disable call, so check pipe. */ 336 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) 337 uhidev_close(&sc->sc_hdev); 338 339 return (rv); 340 } 341 342 void 343 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len) 344 { 345 struct ukbd_softc *sc = (struct ukbd_softc *)addr; 346 struct hidkbd *kbd = &sc->sc_kbd; 347 348 if (kbd->sc_enabled != 0) { 349 if (sc->sc_munge != NULL) 350 (*sc->sc_munge)(sc, (uint8_t *)ibuf, len); 351 hidkbd_input(kbd, (uint8_t *)ibuf, len); 352 } 353 } 354 355 int 356 ukbd_enable(void *v, int on) 357 { 358 struct ukbd_softc *sc = v; 359 struct hidkbd *kbd = &sc->sc_kbd; 360 int rv; 361 362 if (on && usbd_is_dying(sc->sc_hdev.sc_udev)) 363 return EIO; 364 365 if ((rv = hidkbd_enable(kbd, on)) != 0) 366 return rv; 367 368 if (on) { 369 return uhidev_open(&sc->sc_hdev); 370 } else { 371 uhidev_close(&sc->sc_hdev); 372 return 0; 373 } 374 } 375 376 void 377 ukbd_set_leds(void *v, int leds) 378 { 379 struct ukbd_softc *sc = v; 380 struct hidkbd *kbd = &sc->sc_kbd; 381 u_int8_t res; 382 383 if (usbd_is_dying(sc->sc_hdev.sc_udev)) 384 return; 385 386 if (sc->sc_ledsize && hidkbd_set_leds(kbd, leds, &res) != 0) 387 uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, 388 sc->sc_hdev.sc_report_id, &res, 1); 389 } 390 391 int 392 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p) 393 { 394 struct ukbd_softc *sc = v; 395 struct hidkbd *kbd = &sc->sc_kbd; 396 int rc; 397 398 switch (cmd) { 399 case WSKBDIO_GTYPE: 400 *(int *)data = WSKBD_TYPE_USB; 401 return (0); 402 case WSKBDIO_SETLEDS: 403 ukbd_set_leds(v, *(int *)data); 404 return (0); 405 default: 406 rc = uhidev_ioctl(&sc->sc_hdev, cmd, data, flag, p); 407 if (rc != -1) 408 return rc; 409 else 410 return hidkbd_ioctl(kbd, cmd, data, flag, p); 411 } 412 } 413 414 /* Console interface. */ 415 void 416 ukbd_cngetc(void *v, u_int *type, int *data) 417 { 418 struct ukbd_softc *sc = v; 419 struct hidkbd *kbd = &sc->sc_kbd; 420 421 DPRINTFN(0,("ukbd_cngetc: enter\n")); 422 kbd->sc_polling = 1; 423 while (kbd->sc_npollchar <= 0) 424 usbd_dopoll(sc->sc_hdev.sc_udev); 425 kbd->sc_polling = 0; 426 hidkbd_cngetc(kbd, type, data); 427 DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", *data)); 428 } 429 430 void 431 ukbd_cnpollc(void *v, int on) 432 { 433 struct ukbd_softc *sc = v; 434 435 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on)); 436 437 if (on) 438 sc->sc_spl = splusb(); 439 else 440 splx(sc->sc_spl); 441 usbd_set_polling(sc->sc_hdev.sc_udev, on); 442 } 443 444 void 445 ukbd_cnbell(void *v, u_int pitch, u_int period, u_int volume) 446 { 447 hidkbd_bell(pitch, period, volume, 1); 448 } 449 450 int 451 ukbd_cnattach(void) 452 { 453 454 /* 455 * XXX USB requires too many parts of the kernel to be running 456 * XXX in order to work, so we can't do much for the console 457 * XXX keyboard until autconfiguration has run its course. 458 */ 459 hidkbd_is_console = 1; 460 return (0); 461 } 462 463 uint8_t 464 ukbd_translate(const struct ukbd_translation *table, size_t tsize, 465 uint8_t keycode) 466 { 467 for (; tsize != 0; table++, tsize--) 468 if (table->original == keycode) 469 return table->translation; 470 return 0; 471 } 472 473 void 474 ukbd_apple_munge(void *vsc, uint8_t *ibuf, u_int ilen) 475 { 476 struct ukbd_softc *sc = vsc; 477 struct hidkbd *kbd = &sc->sc_kbd; 478 uint8_t *pos, *spos, *epos, xlat; 479 480 static const struct ukbd_translation apple_fn_trans[] = { 481 { 40, 73 }, /* return -> insert */ 482 { 42, 76 }, /* backspace -> delete */ 483 #ifdef notyet 484 { 58, 0 }, /* F1 -> screen brightness down */ 485 { 59, 0 }, /* F2 -> screen brightness up */ 486 { 60, 0 }, /* F3 */ 487 { 61, 0 }, /* F4 */ 488 { 62, 0 }, /* F5 -> keyboard backlight down */ 489 { 63, 0 }, /* F6 -> keyboard backlight up */ 490 { 64, 0 }, /* F7 -> audio back */ 491 { 65, 0 }, /* F8 -> audio pause/play */ 492 { 66, 0 }, /* F9 -> audio next */ 493 #endif 494 #ifdef __macppc__ 495 { 60, 127 }, /* F3 -> audio mute */ 496 { 61, 129 }, /* F4 -> audio lower */ 497 { 62, 128 }, /* F5 -> audio raise */ 498 #else 499 { 67, 127 }, /* F10 -> audio mute */ 500 { 68, 129 }, /* F11 -> audio lower */ 501 { 69, 128 }, /* F12 -> audio raise */ 502 #endif 503 { 79, 77 }, /* right -> end */ 504 { 80, 74 }, /* left -> home */ 505 { 81, 78 }, /* down -> page down */ 506 { 82, 75 } /* up -> page up */ 507 }; 508 509 if (!hid_get_data(ibuf, ilen, &sc->sc_apple_fn)) 510 return; 511 512 spos = ibuf + kbd->sc_keycodeloc.pos / 8; 513 epos = spos + kbd->sc_nkeycode; 514 515 for (pos = spos; pos != epos; pos++) { 516 xlat = ukbd_translate(apple_fn_trans, 517 nitems(apple_fn_trans), *pos); 518 if (xlat != 0) 519 *pos = xlat; 520 } 521 } 522 523 void 524 ukbd_apple_iso_munge(void *vsc, uint8_t *ibuf, u_int ilen) 525 { 526 struct ukbd_softc *sc = vsc; 527 struct hidkbd *kbd = &sc->sc_kbd; 528 uint8_t *pos, *spos, *epos, xlat; 529 530 static const struct ukbd_translation apple_iso_trans[] = { 531 { 53, 100 }, /* less -> grave */ 532 { 100, 53 }, 533 }; 534 535 spos = ibuf + kbd->sc_keycodeloc.pos / 8; 536 epos = spos + kbd->sc_nkeycode; 537 538 for (pos = spos; pos != epos; pos++) { 539 xlat = ukbd_translate(apple_iso_trans, 540 nitems(apple_iso_trans), *pos); 541 if (xlat != 0) 542 *pos = xlat; 543 } 544 545 ukbd_apple_munge(vsc, ibuf, ilen); 546 } 547 548 #ifdef __loongson__ 549 /* 550 * Software Fn- translation for Gdium Liberty keyboard. 551 */ 552 #define GDIUM_FN_CODE 0x82 553 void 554 ukbd_gdium_munge(void *vsc, uint8_t *ibuf, u_int ilen) 555 { 556 struct ukbd_softc *sc = vsc; 557 struct hidkbd *kbd = &sc->sc_kbd; 558 uint8_t *pos, *spos, *epos, xlat; 559 int fn; 560 561 static const struct ukbd_translation gdium_fn_trans[] = { 562 #ifdef notyet 563 { 58, 0 }, /* F1 -> toggle camera */ 564 { 59, 0 }, /* F2 -> toggle wireless */ 565 #endif 566 { 60, 127 }, /* F3 -> audio mute */ 567 { 61, 128 }, /* F4 -> audio raise */ 568 { 62, 129 }, /* F5 -> audio lower */ 569 #ifdef notyet 570 { 63, 0 }, /* F6 -> toggle ext. video */ 571 { 64, 0 }, /* F7 -> toggle mouse */ 572 { 65, 0 }, /* F8 -> brightness up */ 573 { 66, 0 }, /* F9 -> brightness down */ 574 { 67, 0 }, /* F10 -> suspend */ 575 { 68, 0 }, /* F11 -> user1 */ 576 { 69, 0 }, /* F12 -> user2 */ 577 { 70, 0 }, /* print screen -> sysrq */ 578 #endif 579 { 76, 71 }, /* delete -> scroll lock */ 580 { 81, 78 }, /* down -> page down */ 581 { 82, 75 } /* up -> page up */ 582 }; 583 584 spos = ibuf + kbd->sc_keycodeloc.pos / 8; 585 epos = spos + kbd->sc_nkeycode; 586 587 /* 588 * Check for Fn key being down and remove it from the report. 589 */ 590 591 fn = 0; 592 for (pos = spos; pos != epos; pos++) 593 if (*pos == GDIUM_FN_CODE) { 594 fn = 1; 595 *pos = 0; 596 break; 597 } 598 599 /* 600 * Rewrite keycodes on the fly to perform Fn-key translation. 601 * Keycodes without a translation are passed unaffected. 602 */ 603 604 if (fn != 0) 605 for (pos = spos; pos != epos; pos++) { 606 xlat = ukbd_translate(gdium_fn_trans, 607 nitems(gdium_fn_trans), *pos); 608 if (xlat != 0) 609 *pos = xlat; 610 } 611 612 } 613 #endif 614