1 /* $OpenBSD: ukbd.c,v 1.79 2020/08/23 11:08:02 mglocker 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 <machine/bus.h> 61 62 #include <dev/usb/usb.h> 63 #include <dev/usb/usbhid.h> 64 65 #include <dev/usb/usbdi.h> 66 #include <dev/usb/usbdivar.h> /* needs_reattach() */ 67 #include <dev/usb/usbdi_util.h> 68 #include <dev/usb/usbdevs.h> 69 #include <dev/usb/usb_quirks.h> 70 #include <dev/usb/uhidev.h> 71 #include <dev/usb/ukbdvar.h> 72 73 #include <dev/wscons/wsconsio.h> 74 #include <dev/wscons/wskbdvar.h> 75 #include <dev/wscons/wsksymdef.h> 76 #include <dev/wscons/wsksymvar.h> 77 78 #include <dev/hid/hidkbdsc.h> 79 80 #ifdef UKBD_DEBUG 81 #define DPRINTF(x) do { if (ukbddebug) printf x; } while (0) 82 #define DPRINTFN(n,x) do { if (ukbddebug>(n)) printf x; } while (0) 83 int ukbddebug = 0; 84 #else 85 #define DPRINTF(x) 86 #define DPRINTFN(n,x) 87 #endif 88 89 const kbd_t ukbd_countrylayout[1 + HCC_MAX] = { 90 (kbd_t)-1, 91 (kbd_t)-1, /* arabic */ 92 KB_BE, /* belgian */ 93 (kbd_t)-1, /* canadian bilingual */ 94 KB_CF, /* canadian french */ 95 (kbd_t)-1, /* czech */ 96 KB_DK, /* danish */ 97 (kbd_t)-1, /* finnish */ 98 KB_FR, /* french */ 99 KB_DE, /* german */ 100 (kbd_t)-1, /* greek */ 101 (kbd_t)-1, /* hebrew */ 102 KB_HU, /* hungary */ 103 (kbd_t)-1, /* international (iso) */ 104 KB_IT, /* italian */ 105 KB_JP, /* japanese (katakana) */ 106 (kbd_t)-1, /* korean */ 107 KB_LA, /* latin american */ 108 (kbd_t)-1, /* netherlands/dutch */ 109 KB_NO, /* norwegian */ 110 (kbd_t)-1, /* persian (farsi) */ 111 KB_PL, /* polish */ 112 KB_PT, /* portuguese */ 113 KB_RU, /* russian */ 114 (kbd_t)-1, /* slovakia */ 115 KB_ES, /* spanish */ 116 KB_SV, /* swedish */ 117 KB_SF, /* swiss french */ 118 KB_SG, /* swiss german */ 119 (kbd_t)-1, /* switzerland */ 120 (kbd_t)-1, /* taiwan */ 121 KB_TR, /* turkish Q */ 122 KB_UK, /* uk */ 123 KB_US, /* us */ 124 (kbd_t)-1, /* yugoslavia */ 125 (kbd_t)-1 /* turkish F */ 126 }; 127 128 struct ukbd_softc { 129 struct uhidev sc_hdev; 130 #define sc_ledsize sc_hdev.sc_osize 131 132 struct hidkbd sc_kbd; 133 int sc_spl; 134 struct hid_location sc_apple_fn; 135 void (*sc_munge)(void *, uint8_t *, u_int); 136 137 #ifdef DDB 138 struct timeout sc_ddb; /* for entering DDB */ 139 #endif 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 void ukbd_debugger(void *); 146 147 const struct wskbd_consops ukbd_consops = { 148 ukbd_cngetc, 149 ukbd_cnpollc, 150 ukbd_cnbell, 151 #ifdef DDB 152 ukbd_debugger, 153 #endif 154 }; 155 156 void ukbd_intr(struct uhidev *addr, void *ibuf, u_int len); 157 158 void ukbd_db_enter(void *); 159 int ukbd_enable(void *, int); 160 void ukbd_set_leds(void *, int); 161 int ukbd_ioctl(void *, u_long, caddr_t, int, struct proc *); 162 163 const struct wskbd_accessops ukbd_accessops = { 164 ukbd_enable, 165 ukbd_set_leds, 166 ukbd_ioctl, 167 }; 168 169 int ukbd_match(struct device *, void *, void *); 170 void ukbd_attach(struct device *, struct device *, void *); 171 int ukbd_detach(struct device *, int); 172 173 struct cfdriver ukbd_cd = { 174 NULL, "ukbd", DV_DULL 175 }; 176 177 const struct cfattach ukbd_ca = { 178 sizeof(struct ukbd_softc), ukbd_match, ukbd_attach, ukbd_detach 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_mba_munge(void *, uint8_t *, u_int); 191 void ukbd_apple_iso_munge(void *, uint8_t *, u_int); 192 void ukbd_apple_iso_mba_munge(void *, uint8_t *, u_int); 193 void ukbd_apple_translate(void *, uint8_t *, u_int, 194 const struct ukbd_translation *, u_int); 195 uint8_t ukbd_translate(const struct ukbd_translation *, size_t, uint8_t); 196 197 int 198 ukbd_match(struct device *parent, void *match, void *aux) 199 { 200 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux; 201 int size; 202 void *desc; 203 204 uhidev_get_report_desc(uha->parent, &desc, &size); 205 if (!hid_is_collection(desc, size, uha->reportid, 206 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) 207 return (UMATCH_NONE); 208 209 return (UMATCH_IFACECLASS); 210 } 211 212 void 213 ukbd_attach(struct device *parent, struct device *self, void *aux) 214 { 215 struct ukbd_softc *sc = (struct ukbd_softc *)self; 216 struct hidkbd *kbd = &sc->sc_kbd; 217 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux; 218 struct usb_hid_descriptor *hid; 219 u_int32_t quirks, qflags = 0; 220 int dlen, repid; 221 int console = 1; 222 void *desc; 223 kbd_t layout = (kbd_t)-1; 224 225 sc->sc_hdev.sc_intr = ukbd_intr; 226 sc->sc_hdev.sc_parent = uha->parent; 227 sc->sc_hdev.sc_udev = uha->uaa->device; 228 sc->sc_hdev.sc_report_id = uha->reportid; 229 230 usbd_set_idle(uha->parent->sc_udev, uha->parent->sc_ifaceno, 0, 0); 231 232 uhidev_get_report_desc(uha->parent, &desc, &dlen); 233 repid = uha->reportid; 234 sc->sc_hdev.sc_isize = hid_report_size(desc, dlen, hid_input, repid); 235 sc->sc_hdev.sc_osize = hid_report_size(desc, dlen, hid_output, repid); 236 sc->sc_hdev.sc_fsize = hid_report_size(desc, dlen, hid_feature, repid); 237 238 /* 239 * Since the HID-Proxy is always detected before any 240 * real keyboard, do not let it grab the console. 241 */ 242 if (uha->uaa->vendor == USB_VENDOR_APPLE && 243 uha->uaa->product == USB_PRODUCT_APPLE_BLUETOOTH_HCI) 244 console = 0; 245 246 quirks = usbd_get_quirks(sc->sc_hdev.sc_udev)->uq_flags; 247 if (quirks & UQ_SPUR_BUT_UP) 248 qflags |= HIDKBD_SPUR_BUT_UP; 249 250 if (hidkbd_attach(self, kbd, console, qflags, repid, desc, dlen) != 0) 251 return; 252 253 if (uha->uaa->vendor == USB_VENDOR_APPLE) { 254 if (hid_locate(desc, dlen, HID_USAGE2(HUP_APPLE, HUG_FN_KEY), 255 uha->reportid, hid_input, &sc->sc_apple_fn, &qflags)) { 256 if (qflags & HIO_VARIABLE) { 257 switch (uha->uaa->product) { 258 case USB_PRODUCT_APPLE_FOUNTAIN_ISO: 259 case USB_PRODUCT_APPLE_GEYSER_ISO: 260 case USB_PRODUCT_APPLE_WELLSPRING6_ISO: 261 case USB_PRODUCT_APPLE_WELLSPRING8_ISO: 262 sc->sc_munge = ukbd_apple_iso_munge; 263 break; 264 case USB_PRODUCT_APPLE_WELLSPRING_ISO: 265 case USB_PRODUCT_APPLE_WELLSPRING4_ISO: 266 case USB_PRODUCT_APPLE_WELLSPRING4A_ISO: 267 sc->sc_munge = ukbd_apple_iso_mba_munge; 268 break; 269 case USB_PRODUCT_APPLE_WELLSPRING_ANSI: 270 case USB_PRODUCT_APPLE_WELLSPRING_JIS: 271 case USB_PRODUCT_APPLE_WELLSPRING4_ANSI: 272 case USB_PRODUCT_APPLE_WELLSPRING4_JIS: 273 case USB_PRODUCT_APPLE_WELLSPRING4A_ANSI: 274 case USB_PRODUCT_APPLE_WELLSPRING4A_JIS: 275 sc->sc_munge = ukbd_apple_mba_munge; 276 break; 277 default: 278 sc->sc_munge = ukbd_apple_munge; 279 break; 280 } 281 } 282 } 283 } 284 285 if (uha->uaa->vendor == USB_VENDOR_TOPRE && 286 uha->uaa->product == USB_PRODUCT_TOPRE_HHKB) { 287 /* ignore country code on purpose */ 288 } else { 289 usb_interface_descriptor_t *id; 290 291 id = usbd_get_interface_descriptor(uha->uaa->iface); 292 hid = usbd_get_hid_descriptor(uha->uaa->device, id); 293 294 if (hid->bCountryCode <= HCC_MAX) 295 layout = ukbd_countrylayout[hid->bCountryCode]; 296 #ifdef DIAGNOSTIC 297 if (hid->bCountryCode != 0) 298 printf(", country code %d", hid->bCountryCode); 299 #endif 300 } 301 if (layout == (kbd_t)-1) { 302 #ifdef UKBD_LAYOUT 303 layout = UKBD_LAYOUT; 304 #else 305 layout = KB_US | KB_DEFAULT; 306 #endif 307 } 308 309 printf("\n"); 310 311 #ifdef __loongson__ 312 if (uha->uaa->vendor == USB_VENDOR_CYPRESS && 313 uha->uaa->product == USB_PRODUCT_CYPRESS_LPRDK) 314 sc->sc_munge = ukbd_gdium_munge; 315 #endif 316 317 if (kbd->sc_console_keyboard) { 318 extern struct wskbd_mapdata ukbd_keymapdata; 319 320 DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc)); 321 ukbd_keymapdata.layout = layout; 322 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata); 323 ukbd_enable(sc, 1); 324 } 325 326 /* Flash the leds; no real purpose, just shows we're alive. */ 327 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | 328 WSKBD_LED_CAPS | WSKBD_LED_COMPOSE); 329 usbd_delay_ms(sc->sc_hdev.sc_udev, 400); 330 ukbd_set_leds(sc, 0); 331 332 hidkbd_attach_wskbd(kbd, layout, &ukbd_accessops); 333 334 #ifdef DDB 335 timeout_set(&sc->sc_ddb, ukbd_db_enter, sc); 336 #endif 337 } 338 339 int 340 ukbd_detach(struct device *self, int flags) 341 { 342 struct ukbd_softc *sc = (struct ukbd_softc *)self; 343 struct hidkbd *kbd = &sc->sc_kbd; 344 int rv; 345 346 rv = hidkbd_detach(kbd, flags); 347 348 /* The console keyboard does not get a disable call, so check pipe. */ 349 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) 350 uhidev_close(&sc->sc_hdev); 351 352 return (rv); 353 } 354 355 void 356 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len) 357 { 358 struct ukbd_softc *sc = (struct ukbd_softc *)addr; 359 struct hidkbd *kbd = &sc->sc_kbd; 360 361 if (kbd->sc_enabled != 0) { 362 if (sc->sc_munge != NULL) 363 (*sc->sc_munge)(sc, (uint8_t *)ibuf, len); 364 hidkbd_input(kbd, (uint8_t *)ibuf, len); 365 } 366 } 367 368 int 369 ukbd_enable(void *v, int on) 370 { 371 struct ukbd_softc *sc = v; 372 struct hidkbd *kbd = &sc->sc_kbd; 373 int rv; 374 375 if (on && usbd_is_dying(sc->sc_hdev.sc_udev)) 376 return EIO; 377 378 if ((rv = hidkbd_enable(kbd, on)) != 0) 379 return rv; 380 381 if (on) { 382 return uhidev_open(&sc->sc_hdev); 383 } else { 384 uhidev_close(&sc->sc_hdev); 385 return 0; 386 } 387 } 388 389 void 390 ukbd_set_leds(void *v, int leds) 391 { 392 struct ukbd_softc *sc = v; 393 struct hidkbd *kbd = &sc->sc_kbd; 394 u_int8_t res; 395 396 if (usbd_is_dying(sc->sc_hdev.sc_udev)) 397 return; 398 399 if (sc->sc_ledsize && hidkbd_set_leds(kbd, leds, &res) != 0) 400 uhidev_set_report_async(sc->sc_hdev.sc_parent, 401 UHID_OUTPUT_REPORT, sc->sc_hdev.sc_report_id, &res, 1); 402 } 403 404 int 405 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p) 406 { 407 struct ukbd_softc *sc = v; 408 struct hidkbd *kbd = &sc->sc_kbd; 409 int rc; 410 411 switch (cmd) { 412 case WSKBDIO_GTYPE: 413 *(int *)data = WSKBD_TYPE_USB; 414 return (0); 415 case WSKBDIO_SETLEDS: 416 ukbd_set_leds(v, *(int *)data); 417 return (0); 418 default: 419 rc = uhidev_ioctl(&sc->sc_hdev, cmd, data, flag, p); 420 if (rc != -1) 421 return rc; 422 else 423 return hidkbd_ioctl(kbd, cmd, data, flag, p); 424 } 425 } 426 427 /* Console interface. */ 428 void 429 ukbd_cngetc(void *v, u_int *type, int *data) 430 { 431 struct ukbd_softc *sc = v; 432 struct hidkbd *kbd = &sc->sc_kbd; 433 434 DPRINTFN(0,("ukbd_cngetc: enter\n")); 435 kbd->sc_polling = 1; 436 while (kbd->sc_npollchar <= 0) 437 usbd_dopoll(sc->sc_hdev.sc_udev); 438 kbd->sc_polling = 0; 439 hidkbd_cngetc(kbd, type, data); 440 DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", *data)); 441 } 442 443 void 444 ukbd_cnpollc(void *v, int on) 445 { 446 struct ukbd_softc *sc = v; 447 448 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on)); 449 450 if (on) 451 sc->sc_spl = splusb(); 452 else 453 splx(sc->sc_spl); 454 usbd_set_polling(sc->sc_hdev.sc_udev, on); 455 } 456 457 void 458 ukbd_cnbell(void *v, u_int pitch, u_int period, u_int volume) 459 { 460 hidkbd_bell(pitch, period, volume, 1); 461 } 462 463 #ifdef DDB 464 void 465 ukbd_debugger(void *v) 466 { 467 struct ukbd_softc *sc = v; 468 469 /* 470 * For the console keyboard we can't deliver CTL-ALT-ESC 471 * from the interrupt routine. Doing so would start 472 * polling from inside the interrupt routine and that 473 * loses bigtime. 474 */ 475 timeout_add(&sc->sc_ddb, 1); 476 } 477 478 void 479 ukbd_db_enter(void *xsc) 480 { 481 db_enter(); 482 } 483 #endif 484 485 int 486 ukbd_cnattach(void) 487 { 488 struct ukbd_softc *sc; 489 int i; 490 491 /* 492 * XXX USB requires too many parts of the kernel to be running 493 * XXX in order to work, so we can't do much for the console 494 * XXX keyboard until autconfiguration has run its course. 495 */ 496 hidkbd_is_console = 1; 497 498 if (!cold) { 499 /* 500 * When switching console dynamically force all USB keyboards 501 * to re-attach and possibly became the 'console' keyboard. 502 */ 503 for (i = 0; i < ukbd_cd.cd_ndevs; i++) { 504 if ((sc = ukbd_cd.cd_devs[i]) != NULL) { 505 usb_needs_reattach(sc->sc_hdev.sc_udev); 506 break; 507 } 508 } 509 } 510 511 return (0); 512 } 513 514 uint8_t 515 ukbd_translate(const struct ukbd_translation *table, size_t tsize, 516 uint8_t keycode) 517 { 518 for (; tsize != 0; table++, tsize--) 519 if (table->original == keycode) 520 return table->translation; 521 return 0; 522 } 523 524 void 525 ukbd_apple_translate(void *vsc, uint8_t *ibuf, u_int ilen, 526 const struct ukbd_translation* trans, u_int tlen) 527 { 528 struct ukbd_softc *sc = vsc; 529 struct hidkbd *kbd = &sc->sc_kbd; 530 uint8_t *pos, *spos, *epos, xlat; 531 532 spos = ibuf + kbd->sc_keycodeloc.pos / 8; 533 epos = spos + kbd->sc_nkeycode; 534 535 for (pos = spos; pos != epos; pos++) { 536 xlat = ukbd_translate(trans, tlen, *pos); 537 if (xlat != 0) 538 *pos = xlat; 539 } 540 } 541 542 void 543 ukbd_apple_munge(void *vsc, uint8_t *ibuf, u_int ilen) 544 { 545 struct ukbd_softc *sc = vsc; 546 547 static const struct ukbd_translation apple_fn_trans[] = { 548 { 40, 73 }, /* return -> insert */ 549 { 42, 76 }, /* backspace -> delete */ 550 #ifdef notyet 551 { 58, 0 }, /* F1 -> screen brightness down */ 552 { 59, 0 }, /* F2 -> screen brightness up */ 553 { 60, 0 }, /* F3 */ 554 { 61, 0 }, /* F4 */ 555 { 62, 0 }, /* F5 -> keyboard backlight down */ 556 { 63, 0 }, /* F6 -> keyboard backlight up */ 557 { 64, 0 }, /* F7 -> audio back */ 558 { 65, 0 }, /* F8 -> audio pause/play */ 559 { 66, 0 }, /* F9 -> audio next */ 560 #endif 561 #ifdef __macppc__ 562 { 60, 127 }, /* F3 -> audio mute */ 563 { 61, 129 }, /* F4 -> audio lower */ 564 { 62, 128 }, /* F5 -> audio raise */ 565 #else 566 { 67, 127 }, /* F10 -> audio mute */ 567 { 68, 129 }, /* F11 -> audio lower */ 568 { 69, 128 }, /* F12 -> audio raise */ 569 #endif 570 { 79, 77 }, /* right -> end */ 571 { 80, 74 }, /* left -> home */ 572 { 81, 78 }, /* down -> page down */ 573 { 82, 75 } /* up -> page up */ 574 }; 575 576 if (!hid_get_data(ibuf, ilen, &sc->sc_apple_fn)) 577 return; 578 579 ukbd_apple_translate(vsc, ibuf, ilen, apple_fn_trans, 580 nitems(apple_fn_trans)); 581 } 582 583 void 584 ukbd_apple_mba_munge(void *vsc, uint8_t *ibuf, u_int ilen) 585 { 586 struct ukbd_softc *sc = vsc; 587 588 static const struct ukbd_translation apple_fn_trans[] = { 589 { 40, 73 }, /* return -> insert */ 590 { 42, 76 }, /* backspace -> delete */ 591 #ifdef notyet 592 { 58, 0 }, /* F1 -> screen brightness down */ 593 { 59, 0 }, /* F2 -> screen brightness up */ 594 { 60, 0 }, /* F3 */ 595 { 61, 0 }, /* F4 */ 596 { 62, 0 }, /* F5 */ 597 { 63, 0 }, /* F6 -> audio back */ 598 { 64, 0 }, /* F7 -> audio pause/play */ 599 { 65, 0 }, /* F8 -> audio next */ 600 #endif 601 { 66, 127 }, /* F9 -> audio mute */ 602 { 67, 129 }, /* F10 -> audio lower */ 603 { 68, 128 }, /* F11 -> audio raise */ 604 #ifdef notyet 605 { 69, 0 }, /* F12 -> eject */ 606 #endif 607 { 79, 77 }, /* right -> end */ 608 { 80, 74 }, /* left -> home */ 609 { 81, 78 }, /* down -> page down */ 610 { 82, 75 } /* up -> page up */ 611 }; 612 613 if (!hid_get_data(ibuf, ilen, &sc->sc_apple_fn)) 614 return; 615 616 ukbd_apple_translate(vsc, ibuf, ilen, apple_fn_trans, 617 nitems(apple_fn_trans)); 618 } 619 620 void 621 ukbd_apple_iso_munge(void *vsc, uint8_t *ibuf, u_int ilen) 622 { 623 static const struct ukbd_translation apple_iso_trans[] = { 624 { 53, 100 }, /* less -> grave */ 625 { 100, 53 }, 626 }; 627 628 ukbd_apple_translate(vsc, ibuf, ilen, apple_iso_trans, 629 nitems(apple_iso_trans)); 630 ukbd_apple_munge(vsc, ibuf, ilen); 631 } 632 633 void 634 ukbd_apple_iso_mba_munge(void *vsc, uint8_t *ibuf, u_int ilen) 635 { 636 static const struct ukbd_translation apple_iso_trans[] = { 637 { 53, 100 }, /* less -> grave */ 638 { 100, 53 }, 639 }; 640 641 ukbd_apple_translate(vsc, ibuf, ilen, apple_iso_trans, 642 nitems(apple_iso_trans)); 643 ukbd_apple_mba_munge(vsc, ibuf, ilen); 644 } 645 646 #ifdef __loongson__ 647 /* 648 * Software Fn- translation for Gdium Liberty keyboard. 649 */ 650 #define GDIUM_FN_CODE 0x82 651 void 652 ukbd_gdium_munge(void *vsc, uint8_t *ibuf, u_int ilen) 653 { 654 struct ukbd_softc *sc = vsc; 655 struct hidkbd *kbd = &sc->sc_kbd; 656 uint8_t *pos, *spos, *epos, xlat; 657 int fn; 658 659 static const struct ukbd_translation gdium_fn_trans[] = { 660 #ifdef notyet 661 { 58, 0 }, /* F1 -> toggle camera */ 662 { 59, 0 }, /* F2 -> toggle wireless */ 663 #endif 664 { 60, 127 }, /* F3 -> audio mute */ 665 { 61, 128 }, /* F4 -> audio raise */ 666 { 62, 129 }, /* F5 -> audio lower */ 667 #ifdef notyet 668 { 63, 0 }, /* F6 -> toggle ext. video */ 669 { 64, 0 }, /* F7 -> toggle mouse */ 670 { 65, 0 }, /* F8 -> brightness up */ 671 { 66, 0 }, /* F9 -> brightness down */ 672 { 67, 0 }, /* F10 -> suspend */ 673 { 68, 0 }, /* F11 -> user1 */ 674 { 69, 0 }, /* F12 -> user2 */ 675 { 70, 0 }, /* print screen -> sysrq */ 676 #endif 677 { 76, 71 }, /* delete -> scroll lock */ 678 { 81, 78 }, /* down -> page down */ 679 { 82, 75 } /* up -> page up */ 680 }; 681 682 spos = ibuf + kbd->sc_keycodeloc.pos / 8; 683 epos = spos + kbd->sc_nkeycode; 684 685 /* 686 * Check for Fn key being down and remove it from the report. 687 */ 688 689 fn = 0; 690 for (pos = spos; pos != epos; pos++) 691 if (*pos == GDIUM_FN_CODE) { 692 fn = 1; 693 *pos = 0; 694 break; 695 } 696 697 /* 698 * Rewrite keycodes on the fly to perform Fn-key translation. 699 * Keycodes without a translation are passed unaffected. 700 */ 701 702 if (fn != 0) 703 for (pos = spos; pos != epos; pos++) { 704 xlat = ukbd_translate(gdium_fn_trans, 705 nitems(gdium_fn_trans), *pos); 706 if (xlat != 0) 707 *pos = xlat; 708 } 709 710 } 711 #endif 712