1 /* $OpenBSD: ukbd.c,v 1.61 2013/11/13 13:48:08 pirofti 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 u_char sc_dying; 136 137 struct hid_location sc_apple_fn; 138 139 void (*sc_munge)(void *, uint8_t *, u_int); 140 }; 141 142 void ukbd_cngetc(void *, u_int *, int *); 143 void ukbd_cnpollc(void *, int); 144 void ukbd_cnbell(void *, u_int, u_int, u_int); 145 146 const struct wskbd_consops ukbd_consops = { 147 ukbd_cngetc, 148 ukbd_cnpollc, 149 ukbd_cnbell, 150 }; 151 152 void ukbd_intr(struct uhidev *addr, void *ibuf, u_int len); 153 154 int ukbd_enable(void *, int); 155 void ukbd_set_leds(void *, int); 156 int ukbd_ioctl(void *, u_long, caddr_t, int, struct proc *); 157 158 const struct wskbd_accessops ukbd_accessops = { 159 ukbd_enable, 160 ukbd_set_leds, 161 ukbd_ioctl, 162 }; 163 164 int ukbd_match(struct device *, void *, void *); 165 void ukbd_attach(struct device *, struct device *, void *); 166 int ukbd_detach(struct device *, int); 167 int ukbd_activate(struct device *, int); 168 169 struct cfdriver ukbd_cd = { 170 NULL, "ukbd", DV_DULL 171 }; 172 173 const struct cfattach ukbd_ca = { 174 sizeof(struct ukbd_softc), 175 ukbd_match, 176 ukbd_attach, 177 ukbd_detach, 178 ukbd_activate, 179 }; 180 181 struct ukbd_translation { 182 uint8_t original; 183 uint8_t translation; 184 }; 185 186 #ifdef __loongson__ 187 void ukbd_gdium_munge(void *, uint8_t *, u_int); 188 #endif 189 void ukbd_apple_munge(void *, uint8_t *, u_int); 190 void ukbd_apple_iso_munge(void *, uint8_t *, u_int); 191 uint8_t ukbd_translate(const struct ukbd_translation *, size_t, uint8_t); 192 193 int 194 ukbd_match(struct device *parent, void *match, void *aux) 195 { 196 struct usb_attach_arg *uaa = aux; 197 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa; 198 int size; 199 void *desc; 200 201 uhidev_get_report_desc(uha->parent, &desc, &size); 202 if (!hid_is_collection(desc, size, uha->reportid, 203 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) 204 return (UMATCH_NONE); 205 206 return (UMATCH_IFACECLASS); 207 } 208 209 void 210 ukbd_attach(struct device *parent, struct device *self, void *aux) 211 { 212 struct ukbd_softc *sc = (struct ukbd_softc *)self; 213 struct hidkbd *kbd = &sc->sc_kbd; 214 struct usb_attach_arg *uaa = aux; 215 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa; 216 struct usb_hid_descriptor *hid; 217 u_int32_t qflags; 218 int dlen, repid; 219 void *desc; 220 kbd_t layout = (kbd_t)-1; 221 222 sc->sc_hdev.sc_intr = ukbd_intr; 223 sc->sc_hdev.sc_parent = uha->parent; 224 sc->sc_hdev.sc_report_id = uha->reportid; 225 226 uhidev_get_report_desc(uha->parent, &desc, &dlen); 227 repid = uha->reportid; 228 sc->sc_hdev.sc_isize = hid_report_size(desc, dlen, hid_input, repid); 229 sc->sc_hdev.sc_osize = hid_report_size(desc, dlen, hid_output, repid); 230 sc->sc_hdev.sc_fsize = hid_report_size(desc, dlen, hid_feature, repid); 231 232 qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags; 233 if (hidkbd_attach(self, kbd, 1, qflags, repid, desc, dlen) != 0) 234 return; 235 236 if (uha->uaa->vendor == USB_VENDOR_APPLE) { 237 int iso = 0; 238 239 if ((uha->uaa->product == USB_PRODUCT_APPLE_FOUNTAIN_ISO) || 240 (uha->uaa->product == USB_PRODUCT_APPLE_GEYSER_ISO)) 241 iso = 1; 242 243 if (hid_locate(desc, dlen, HID_USAGE2(HUP_APPLE, HUG_FN_KEY), 244 uha->reportid, hid_input, &sc->sc_apple_fn, &qflags)) { 245 if (qflags & HIO_VARIABLE) { 246 if (iso) 247 sc->sc_munge = ukbd_apple_iso_munge; 248 else 249 sc->sc_munge = ukbd_apple_munge; 250 } 251 } 252 } 253 254 if (uha->uaa->vendor == USB_VENDOR_TOPRE && 255 uha->uaa->product == USB_PRODUCT_TOPRE_HHKB) { 256 /* ignore country code on purpose */ 257 } else { 258 hid = usbd_get_hid_descriptor(uha->uaa->iface); 259 260 if (hid->bCountryCode <= HCC_MAX) 261 layout = ukbd_countrylayout[hid->bCountryCode]; 262 #ifdef DIAGNOSTIC 263 if (hid->bCountryCode != 0) 264 printf(", country code %d", hid->bCountryCode); 265 #endif 266 } 267 if (layout == (kbd_t)-1) { 268 #ifdef UKBD_LAYOUT 269 layout = UKBD_LAYOUT; 270 #else 271 layout = KB_US; 272 #endif 273 } 274 275 printf("\n"); 276 277 #ifdef __loongson__ 278 if (uha->uaa->vendor == USB_VENDOR_CYPRESS && 279 uha->uaa->product == USB_PRODUCT_CYPRESS_LPRDK) 280 sc->sc_munge = ukbd_gdium_munge; 281 #endif 282 283 if (kbd->sc_console_keyboard) { 284 extern struct wskbd_mapdata ukbd_keymapdata; 285 286 DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc)); 287 ukbd_keymapdata.layout = layout; 288 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata); 289 ukbd_enable(sc, 1); 290 } 291 292 /* Flash the leds; no real purpose, just shows we're alive. */ 293 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | 294 WSKBD_LED_CAPS | WSKBD_LED_COMPOSE); 295 usbd_delay_ms(uha->parent->sc_udev, 400); 296 ukbd_set_leds(sc, 0); 297 298 hidkbd_attach_wskbd(kbd, layout, &ukbd_accessops); 299 } 300 301 int 302 ukbd_activate(struct device *self, int act) 303 { 304 struct ukbd_softc *sc = (struct ukbd_softc *)self; 305 struct hidkbd *kbd = &sc->sc_kbd; 306 int rv = 0; 307 308 switch (act) { 309 case DVACT_DEACTIVATE: 310 if (kbd->sc_wskbddev != NULL) 311 rv = config_deactivate(kbd->sc_wskbddev); 312 sc->sc_dying = 1; 313 break; 314 } 315 return (rv); 316 } 317 318 int 319 ukbd_detach(struct device *self, int flags) 320 { 321 struct ukbd_softc *sc = (struct ukbd_softc *)self; 322 struct hidkbd *kbd = &sc->sc_kbd; 323 int rv; 324 325 rv = hidkbd_detach(kbd, flags); 326 327 /* The console keyboard does not get a disable call, so check pipe. */ 328 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) 329 uhidev_close(&sc->sc_hdev); 330 331 return (rv); 332 } 333 334 void 335 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len) 336 { 337 struct ukbd_softc *sc = (struct ukbd_softc *)addr; 338 struct hidkbd *kbd = &sc->sc_kbd; 339 340 if (kbd->sc_enabled != 0) { 341 if (sc->sc_munge != NULL) 342 (*sc->sc_munge)(sc, (uint8_t *)ibuf, len); 343 hidkbd_input(kbd, (uint8_t *)ibuf, len); 344 } 345 } 346 347 int 348 ukbd_enable(void *v, int on) 349 { 350 struct ukbd_softc *sc = v; 351 struct hidkbd *kbd = &sc->sc_kbd; 352 int rv; 353 354 if (on && sc->sc_dying) 355 return EIO; 356 357 if ((rv = hidkbd_enable(kbd, on)) != 0) 358 return rv; 359 360 if (on) { 361 return uhidev_open(&sc->sc_hdev); 362 } else { 363 uhidev_close(&sc->sc_hdev); 364 return 0; 365 } 366 } 367 368 void 369 ukbd_set_leds(void *v, int leds) 370 { 371 struct ukbd_softc *sc = v; 372 struct hidkbd *kbd = &sc->sc_kbd; 373 u_int8_t res; 374 375 if (sc->sc_dying) 376 return; 377 378 if (sc->sc_ledsize && hidkbd_set_leds(kbd, leds, &res) != 0) 379 uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, 380 &res, 1); 381 } 382 383 int 384 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p) 385 { 386 struct ukbd_softc *sc = v; 387 struct hidkbd *kbd = &sc->sc_kbd; 388 int rc; 389 390 switch (cmd) { 391 case WSKBDIO_GTYPE: 392 *(int *)data = WSKBD_TYPE_USB; 393 return (0); 394 case WSKBDIO_SETLEDS: 395 ukbd_set_leds(v, *(int *)data); 396 return (0); 397 default: 398 rc = uhidev_ioctl(&sc->sc_hdev, cmd, data, flag, p); 399 if (rc != -1) 400 return rc; 401 else 402 return hidkbd_ioctl(kbd, cmd, data, flag, p); 403 } 404 } 405 406 /* Console interface. */ 407 void 408 ukbd_cngetc(void *v, u_int *type, int *data) 409 { 410 struct ukbd_softc *sc = v; 411 struct hidkbd *kbd = &sc->sc_kbd; 412 413 DPRINTFN(0,("ukbd_cngetc: enter\n")); 414 kbd->sc_polling = 1; 415 while (kbd->sc_npollchar <= 0) 416 usbd_dopoll(sc->sc_hdev.sc_parent->sc_udev); 417 kbd->sc_polling = 0; 418 hidkbd_cngetc(kbd, type, data); 419 DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", *data)); 420 } 421 422 void 423 ukbd_cnpollc(void *v, int on) 424 { 425 struct ukbd_softc *sc = v; 426 427 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on)); 428 429 if (on) 430 sc->sc_spl = splusb(); 431 else 432 splx(sc->sc_spl); 433 usbd_set_polling(sc->sc_hdev.sc_parent->sc_udev, on); 434 } 435 436 void 437 ukbd_cnbell(void *v, u_int pitch, u_int period, u_int volume) 438 { 439 hidkbd_bell(pitch, period, volume, 1); 440 } 441 442 int 443 ukbd_cnattach(void) 444 { 445 446 /* 447 * XXX USB requires too many parts of the kernel to be running 448 * XXX in order to work, so we can't do much for the console 449 * XXX keyboard until autconfiguration has run its course. 450 */ 451 hidkbd_is_console = 1; 452 return (0); 453 } 454 455 uint8_t 456 ukbd_translate(const struct ukbd_translation *table, size_t tsize, 457 uint8_t keycode) 458 { 459 for (; tsize != 0; table++, tsize--) 460 if (table->original == keycode) 461 return table->translation; 462 return 0; 463 } 464 465 void 466 ukbd_apple_munge(void *vsc, uint8_t *ibuf, u_int ilen) 467 { 468 struct ukbd_softc *sc = vsc; 469 struct hidkbd *kbd = &sc->sc_kbd; 470 uint8_t *pos, *spos, *epos, xlat; 471 472 static const struct ukbd_translation apple_fn_trans[] = { 473 { 40, 73 }, /* return -> insert */ 474 { 42, 76 }, /* backspace -> delete */ 475 #ifdef notyet 476 { 58, 0 }, /* F1 -> screen brightness down */ 477 { 59, 0 }, /* F2 -> screen brightness up */ 478 { 60, 0 }, /* F3 */ 479 { 61, 0 }, /* F4 */ 480 { 62, 0 }, /* F5 -> keyboard backlight down */ 481 { 63, 0 }, /* F6 -> keyboard backlight up */ 482 { 64, 0 }, /* F7 -> audio back */ 483 { 65, 0 }, /* F8 -> audio pause/play */ 484 { 66, 0 }, /* F9 -> audio next */ 485 #endif 486 #ifdef __macppc__ 487 { 60, 127 }, /* F3 -> audio mute */ 488 { 61, 129 }, /* F4 -> audio lower */ 489 { 62, 128 }, /* F5 -> audio raise */ 490 #else 491 { 67, 127 }, /* F10 -> audio mute */ 492 { 68, 129 }, /* F11 -> audio lower */ 493 { 69, 128 }, /* F12 -> audio raise */ 494 #endif 495 { 79, 77 }, /* right -> end */ 496 { 80, 74 }, /* left -> home */ 497 { 81, 78 }, /* down -> page down */ 498 { 82, 75 } /* up -> page up */ 499 }; 500 501 if (!hid_get_data(ibuf, &sc->sc_apple_fn)) 502 return; 503 504 spos = ibuf + kbd->sc_keycodeloc.pos / 8; 505 epos = spos + kbd->sc_nkeycode; 506 507 for (pos = spos; pos != epos; pos++) { 508 xlat = ukbd_translate(apple_fn_trans, 509 nitems(apple_fn_trans), *pos); 510 if (xlat != 0) 511 *pos = xlat; 512 } 513 } 514 515 void 516 ukbd_apple_iso_munge(void *vsc, uint8_t *ibuf, u_int ilen) 517 { 518 struct ukbd_softc *sc = vsc; 519 struct hidkbd *kbd = &sc->sc_kbd; 520 uint8_t *pos, *spos, *epos, xlat; 521 522 static const struct ukbd_translation apple_iso_trans[] = { 523 { 53, 100 }, /* less -> grave */ 524 { 100, 53 }, 525 }; 526 527 spos = ibuf + kbd->sc_keycodeloc.pos / 8; 528 epos = spos + kbd->sc_nkeycode; 529 530 for (pos = spos; pos != epos; pos++) { 531 xlat = ukbd_translate(apple_iso_trans, 532 nitems(apple_iso_trans), *pos); 533 if (xlat != 0) 534 *pos = xlat; 535 } 536 537 ukbd_apple_munge(vsc, ibuf, ilen); 538 } 539 540 #ifdef __loongson__ 541 /* 542 * Software Fn- translation for Gdium Liberty keyboard. 543 */ 544 #define GDIUM_FN_CODE 0x82 545 void 546 ukbd_gdium_munge(void *vsc, uint8_t *ibuf, u_int ilen) 547 { 548 struct ukbd_softc *sc = vsc; 549 struct hidkbd *kbd = &sc->sc_kbd; 550 uint8_t *pos, *spos, *epos, xlat; 551 int fn; 552 553 static const struct ukbd_translation gdium_fn_trans[] = { 554 #ifdef notyet 555 { 58, 0 }, /* F1 -> toggle camera */ 556 { 59, 0 }, /* F2 -> toggle wireless */ 557 #endif 558 { 60, 127 }, /* F3 -> audio mute */ 559 { 61, 128 }, /* F4 -> audio raise */ 560 { 62, 129 }, /* F5 -> audio lower */ 561 #ifdef notyet 562 { 63, 0 }, /* F6 -> toggle ext. video */ 563 { 64, 0 }, /* F7 -> toggle mouse */ 564 { 65, 0 }, /* F8 -> brightness up */ 565 { 66, 0 }, /* F9 -> brightness down */ 566 { 67, 0 }, /* F10 -> suspend */ 567 { 68, 0 }, /* F11 -> user1 */ 568 { 69, 0 }, /* F12 -> user2 */ 569 { 70, 0 }, /* print screen -> sysrq */ 570 #endif 571 { 76, 71 }, /* delete -> scroll lock */ 572 { 81, 78 }, /* down -> page down */ 573 { 82, 75 } /* up -> page up */ 574 }; 575 576 spos = ibuf + kbd->sc_keycodeloc.pos / 8; 577 epos = spos + kbd->sc_nkeycode; 578 579 /* 580 * Check for Fn key being down and remove it from the report. 581 */ 582 583 fn = 0; 584 for (pos = spos; pos != epos; pos++) 585 if (*pos == GDIUM_FN_CODE) { 586 fn = 1; 587 *pos = 0; 588 break; 589 } 590 591 /* 592 * Rewrite keycodes on the fly to perform Fn-key translation. 593 * Keycodes without a translation are passed unaffected. 594 */ 595 596 if (fn != 0) 597 for (pos = spos; pos != epos; pos++) { 598 xlat = ukbd_translate(gdium_fn_trans, 599 nitems(gdium_fn_trans), *pos); 600 if (xlat != 0) 601 *pos = xlat; 602 } 603 604 } 605 #endif 606