1 /* $NetBSD: ukbd.c,v 1.117 2012/03/11 01:06:07 mrg 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 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.117 2012/03/11 01:06:07 mrg Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/callout.h> 43 #include <sys/kernel.h> 44 #include <sys/device.h> 45 #include <sys/ioctl.h> 46 #include <sys/file.h> 47 #include <sys/select.h> 48 #include <sys/proc.h> 49 #include <sys/vnode.h> 50 #include <sys/poll.h> 51 52 #include <dev/usb/usb.h> 53 #include <dev/usb/usbhid.h> 54 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdi_util.h> 57 #include <dev/usb/usbdivar.h> 58 #include <dev/usb/usbdevs.h> 59 #include <dev/usb/usb_quirks.h> 60 #include <dev/usb/uhidev.h> 61 #include <dev/usb/hid.h> 62 #include <dev/usb/ukbdvar.h> 63 64 #include <dev/wscons/wsconsio.h> 65 #include <dev/wscons/wskbdvar.h> 66 #include <dev/wscons/wsksymdef.h> 67 #include <dev/wscons/wsksymvar.h> 68 69 #ifdef _KERNEL_OPT 70 #include "opt_ukbd.h" 71 #include "opt_ukbd_layout.h" 72 #include "opt_wsdisplay_compat.h" 73 #include "opt_ddb.h" 74 #endif /* _KERNEL_OPT */ 75 76 #ifdef UKBD_DEBUG 77 #define DPRINTF(x) if (ukbddebug) printf x 78 #define DPRINTFN(n,x) if (ukbddebug>(n)) printf x 79 int ukbddebug = 0; 80 #else 81 #define DPRINTF(x) 82 #define DPRINTFN(n,x) 83 #endif 84 85 #define MAXKEYCODE 6 86 #define MAXMOD 8 /* max 32 */ 87 88 struct ukbd_data { 89 u_int32_t modifiers; 90 u_int8_t keycode[MAXKEYCODE]; 91 }; 92 93 #define PRESS 0x000 94 #define RELEASE 0x100 95 #define CODEMASK 0x0ff 96 97 struct ukbd_keycodetrans { 98 u_int16_t from; 99 u_int16_t to; 100 }; 101 102 #define IS_PMF 0x8000 103 104 Static const struct ukbd_keycodetrans trtab_apple_fn[] = { 105 { 0x0c, 0x5d }, /* i -> KP 5 */ 106 { 0x0d, 0x59 }, /* j -> KP 1 */ 107 { 0x0e, 0x5a }, /* k -> KP 2 */ 108 { 0x0f, 0x5b }, /* l -> KP 3 */ 109 { 0x10, 0x62 }, /* m -> KP 0 */ 110 { 0x12, 0x5e }, /* o -> KP 6 */ 111 { 0x13, 0x55 }, /* o -> KP * */ 112 { 0x18, 0x5c }, /* u -> KP 4 */ 113 { 0x0c, 0x5d }, /* i -> KP 5 */ 114 { 0x2a, 0x4c }, /* Backspace -> Delete */ 115 { 0x28, 0x49 }, /* Return -> Insert */ 116 { 0x24, 0x5f }, /* 7 -> KP 7 */ 117 { 0x25, 0x60 }, /* 8 -> KP 8 */ 118 { 0x26, 0x61 }, /* 9 -> KP 9 */ 119 { 0x27, 0x54 }, /* 0 -> KP / */ 120 { 0x2d, 0x67 }, /* - -> KP = */ 121 { 0x33, 0x56 }, /* ; -> KP - */ 122 { 0x37, 0x63 }, /* . -> KP . */ 123 { 0x38, 0x57 }, /* / -> KP + */ 124 { 0x3a, 0xd1 }, /* F1..F12 mapped to reserved codes 0xd1..0xdc */ 125 { 0x3b, 0xd2 }, 126 { 0x3c, 0xd3 }, 127 { 0x3d, 0xd4 }, 128 { 0x3e, 0xd5 }, 129 { 0x3f, 0xd6 }, 130 { 0x40, 0xd7 }, 131 { 0x41, 0xd8 }, 132 { 0x42, 0xd9 }, 133 { 0x43, 0xda }, 134 { 0x44, 0xdb }, 135 { 0x45, 0xdc }, 136 { 0x4f, 0x4d }, /* Right -> End */ 137 { 0x50, 0x4a }, /* Left -> Home */ 138 { 0x51, 0x4e }, /* Down -> PageDown */ 139 { 0x52, 0x4b }, /* Up -> PageUp */ 140 { 0x00, 0x00 } 141 }; 142 143 Static const struct ukbd_keycodetrans trtab_apple_iso[] = { 144 { 0x35, 0x64 }, /* swap the key above tab with key right of shift */ 145 { 0x64, 0x35 }, 146 { 0x31, 0x32 }, /* key left of return is Europe1, not "\|" */ 147 { 0x00, 0x00 } 148 }; 149 150 #ifdef GDIUM_KEYBOARD_HACK 151 Static const struct ukbd_keycodetrans trtab_gdium_fn[] = { 152 #ifdef notyet 153 { 58, 0 }, /* F1 -> toggle camera */ 154 { 59, 0 }, /* F2 -> toggle wireless */ 155 #endif 156 { 60, IS_PMF | PMFE_AUDIO_VOLUME_TOGGLE }, 157 { 61, IS_PMF | PMFE_AUDIO_VOLUME_UP }, 158 { 62, IS_PMF | PMFE_AUDIO_VOLUME_DOWN }, 159 #ifdef notyet 160 { 63, 0 }, /* F6 -> toggle ext. video */ 161 { 64, 0 }, /* F7 -> toggle mouse */ 162 #endif 163 { 65, IS_PMF | PMFE_DISPLAY_BRIGHTNESS_UP }, 164 { 66, IS_PMF | PMFE_DISPLAY_BRIGHTNESS_DOWN }, 165 #ifdef notyet 166 { 67, 0 }, /* F10 -> suspend */ 167 { 68, 0 }, /* F11 -> user1 */ 168 { 69, 0 }, /* F12 -> user2 */ 169 { 70, 0 }, /* print screen -> sysrq */ 170 #endif 171 { 76, 71 }, /* delete -> scroll lock */ 172 { 81, 78 }, /* down -> page down */ 173 { 82, 75 } /* up -> page up */ 174 }; 175 #endif 176 177 #if defined(WSDISPLAY_COMPAT_RAWKBD) 178 #define NN 0 /* no translation */ 179 /* 180 * Translate USB keycodes to US keyboard XT scancodes. 181 * Scancodes >= 0x80 represent EXTENDED keycodes. 182 * 183 * See http://www.microsoft.com/whdc/archive/scancode.mspx 184 * 185 * Note: a real pckbd(4) has more complexity in its 186 * protocol for some keys than this translation implements. 187 * For example, some keys generate Fake ShiftL events (e0 2a) 188 * before the actual key sequence. 189 */ 190 Static const u_int8_t ukbd_trtab[256] = { 191 NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */ 192 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */ 193 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */ 194 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */ 195 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */ 196 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */ 197 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */ 198 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */ 199 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xb7, 0x46, /* 40 - 47 */ 200 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */ 201 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */ 202 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */ 203 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, NN, 0x59, /* 60 - 67 */ 204 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, NN, /* 68 - 6f */ 205 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */ 206 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7f */ 207 NN, NN, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */ 208 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */ 209 NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */ 210 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */ 211 NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */ 212 NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */ 213 NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */ 214 NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */ 215 NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */ 216 NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */ 217 NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */ 218 NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */ 219 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */ 220 NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */ 221 NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */ 222 NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */ 223 }; 224 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */ 225 226 #define KEY_ERROR 0x01 227 228 #define MAXKEYS (MAXMOD+2*MAXKEYCODE) 229 230 struct ukbd_softc { 231 struct uhidev sc_hdev; 232 233 struct ukbd_data sc_ndata; 234 struct ukbd_data sc_odata; 235 struct hid_location sc_modloc[MAXMOD]; 236 u_int sc_nmod; 237 struct { 238 u_int32_t mask; 239 u_int8_t key; 240 } sc_mods[MAXMOD]; 241 242 struct hid_location sc_keycodeloc; 243 u_int sc_nkeycode; 244 245 u_int sc_flags; /* flags */ 246 #define FLAG_ENABLED 0x0001 247 #define FLAG_POLLING 0x0002 248 #define FLAG_DEBOUNCE 0x0004 /* for quirk handling */ 249 #define FLAG_APPLE_FIX_ISO 0x0008 250 #define FLAG_APPLE_FN 0x0010 251 #define FLAG_GDIUM_FN 0x0020 252 #define FLAG_FN_PRESSED 0x0100 /* FN key is held down */ 253 #define FLAG_FN_ALT 0x0200 /* Last Alt key was FN-Alt = AltGr */ 254 255 int sc_console_keyboard; /* we are the console keyboard */ 256 257 struct callout sc_delay; /* for quirk handling */ 258 struct ukbd_data sc_data; /* for quirk handling */ 259 260 struct hid_location sc_apple_fn; 261 struct hid_location sc_numloc; 262 struct hid_location sc_capsloc; 263 struct hid_location sc_scroloc; 264 int sc_leds; 265 device_t sc_wskbddev; 266 267 #if defined(WSDISPLAY_COMPAT_RAWKBD) 268 int sc_rawkbd; 269 #if defined(UKBD_REPEAT) 270 struct callout sc_rawrepeat_ch; 271 #define REP_DELAY1 400 272 #define REP_DELAYN 100 273 int sc_nrep; 274 char sc_rep[MAXKEYS]; 275 #endif /* defined(UKBD_REPEAT) */ 276 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */ 277 278 int sc_spl; 279 int sc_npollchar; 280 u_int16_t sc_pollchars[MAXKEYS]; 281 282 u_char sc_dying; 283 }; 284 285 #ifdef UKBD_DEBUG 286 #define UKBDTRACESIZE 64 287 struct ukbdtraceinfo { 288 int unit; 289 struct timeval tv; 290 struct ukbd_data ud; 291 }; 292 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE]; 293 int ukbdtraceindex = 0; 294 int ukbdtrace = 0; 295 void ukbdtracedump(void); 296 void 297 ukbdtracedump(void) 298 { 299 int i; 300 for (i = 0; i < UKBDTRACESIZE; i++) { 301 struct ukbdtraceinfo *p = 302 &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE]; 303 printf("%"PRIu64".%06"PRIu64": mod=0x%02x key0=0x%02x key1=0x%02x " 304 "key2=0x%02x key3=0x%02x\n", 305 p->tv.tv_sec, (uint64_t)p->tv.tv_usec, 306 p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1], 307 p->ud.keycode[2], p->ud.keycode[3]); 308 } 309 } 310 #endif 311 312 #define UKBDUNIT(dev) (minor(dev)) 313 #define UKBD_CHUNK 128 /* chunk size for read */ 314 #define UKBD_BSIZE 1020 /* buffer size */ 315 316 Static int ukbd_is_console; 317 318 Static void ukbd_cngetc(void *, u_int *, int *); 319 Static void ukbd_cnpollc(void *, int); 320 321 const struct wskbd_consops ukbd_consops = { 322 ukbd_cngetc, 323 ukbd_cnpollc, 324 NULL, /* bell */ 325 }; 326 327 Static const char *ukbd_parse_desc(struct ukbd_softc *sc); 328 329 Static void ukbd_intr(struct uhidev *addr, void *ibuf, u_int len); 330 Static void ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud); 331 Static void ukbd_delayed_decode(void *addr); 332 333 Static int ukbd_enable(void *, int); 334 Static void ukbd_set_leds(void *, int); 335 336 Static int ukbd_ioctl(void *, u_long, void *, int, struct lwp *); 337 #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) 338 Static void ukbd_rawrepeat(void *v); 339 #endif 340 341 const struct wskbd_accessops ukbd_accessops = { 342 ukbd_enable, 343 ukbd_set_leds, 344 ukbd_ioctl, 345 }; 346 347 extern const struct wscons_keydesc ukbd_keydesctab[]; 348 349 const struct wskbd_mapdata ukbd_keymapdata = { 350 ukbd_keydesctab, 351 #if defined(UKBD_LAYOUT) 352 UKBD_LAYOUT, 353 #elif defined(PCKBD_LAYOUT) 354 PCKBD_LAYOUT, 355 #else 356 KB_US, 357 #endif 358 }; 359 360 static int ukbd_match(device_t, cfdata_t, void *); 361 static void ukbd_attach(device_t, device_t, void *); 362 static int ukbd_detach(device_t, int); 363 static int ukbd_activate(device_t, enum devact); 364 static void ukbd_childdet(device_t, device_t); 365 366 extern struct cfdriver ukbd_cd; 367 368 CFATTACH_DECL2_NEW(ukbd, sizeof(struct ukbd_softc), ukbd_match, ukbd_attach, 369 ukbd_detach, ukbd_activate, NULL, ukbd_childdet); 370 371 int 372 ukbd_match(device_t parent, cfdata_t match, void *aux) 373 { 374 struct uhidev_attach_arg *uha = aux; 375 int size; 376 void *desc; 377 378 uhidev_get_report_desc(uha->parent, &desc, &size); 379 if (!hid_is_collection(desc, size, uha->reportid, 380 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) 381 return (UMATCH_NONE); 382 383 return (UMATCH_IFACECLASS); 384 } 385 386 void 387 ukbd_attach(device_t parent, device_t self, void *aux) 388 { 389 struct ukbd_softc *sc = device_private(self); 390 struct uhidev_attach_arg *uha = aux; 391 u_int32_t qflags; 392 const char *parseerr; 393 struct wskbddev_attach_args a; 394 395 sc->sc_hdev.sc_dev = self; 396 sc->sc_hdev.sc_intr = ukbd_intr; 397 sc->sc_hdev.sc_parent = uha->parent; 398 sc->sc_hdev.sc_report_id = uha->reportid; 399 sc->sc_flags = 0; 400 401 if (!pmf_device_register(self, NULL, NULL)) { 402 aprint_normal("\n"); 403 aprint_error_dev(self, "couldn't establish power handler\n"); 404 } 405 406 parseerr = ukbd_parse_desc(sc); 407 if (parseerr != NULL) { 408 aprint_normal("\n"); 409 aprint_error_dev(self, "attach failed, %s\n", parseerr); 410 return; 411 } 412 413 /* Quirks */ 414 qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags; 415 if (qflags & UQ_SPUR_BUT_UP) 416 sc->sc_flags |= FLAG_DEBOUNCE; 417 if (qflags & UQ_APPLE_ISO) 418 sc->sc_flags |= FLAG_APPLE_FIX_ISO; 419 420 #ifdef GDIUM_KEYBOARD_HACK 421 if (uha->uaa->vendor == USB_VENDOR_CYPRESS && 422 uha->uaa->product == USB_PRODUCT_CYPRESS_LPRDK) 423 sc->sc_flags = FLAG_GDIUM_FN; 424 #endif 425 426 #ifdef DIAGNOSTIC 427 aprint_normal(": %d modifier keys, %d key codes", sc->sc_nmod, 428 sc->sc_nkeycode); 429 if (sc->sc_flags & FLAG_APPLE_FN) 430 aprint_normal(", apple fn key"); 431 if (sc->sc_flags & FLAG_APPLE_FIX_ISO) 432 aprint_normal(", fix apple iso"); 433 if (sc->sc_flags & FLAG_GDIUM_FN) 434 aprint_normal(", Gdium fn key"); 435 #endif 436 aprint_normal("\n"); 437 438 /* 439 * Remember if we're the console keyboard. 440 * 441 * XXX This always picks the first keyboard on the 442 * first USB bus, but what else can we really do? 443 */ 444 if ((sc->sc_console_keyboard = ukbd_is_console) != 0) { 445 /* Don't let any other keyboard have it. */ 446 ukbd_is_console = 0; 447 } 448 449 if (sc->sc_console_keyboard) { 450 DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc)); 451 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata); 452 ukbd_enable(sc, 1); 453 } 454 455 a.console = sc->sc_console_keyboard; 456 457 a.keymap = &ukbd_keymapdata; 458 459 a.accessops = &ukbd_accessops; 460 a.accesscookie = sc; 461 462 #ifdef UKBD_REPEAT 463 callout_init(&sc->sc_rawrepeat_ch, 0); 464 #endif 465 466 callout_init(&sc->sc_delay, 0); 467 468 /* Flash the leds; no real purpose, just shows we're alive. */ 469 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS); 470 usbd_delay_ms(uha->parent->sc_udev, 400); 471 ukbd_set_leds(sc, 0); 472 473 sc->sc_wskbddev = config_found(self, &a, wskbddevprint); 474 475 return; 476 } 477 478 int 479 ukbd_enable(void *v, int on) 480 { 481 struct ukbd_softc *sc = v; 482 483 if (on && sc->sc_dying) 484 return (EIO); 485 486 /* Should only be called to change state */ 487 if ((sc->sc_flags & FLAG_ENABLED) != 0 && on != 0) { 488 #ifdef DIAGNOSTIC 489 printf("ukbd_enable: %s: bad call on=%d\n", 490 device_xname(sc->sc_hdev.sc_dev), on); 491 #endif 492 return (EBUSY); 493 } 494 495 DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on)); 496 if (on) { 497 sc->sc_flags |= FLAG_ENABLED; 498 return (uhidev_open(&sc->sc_hdev)); 499 } else { 500 sc->sc_flags &= ~FLAG_ENABLED; 501 uhidev_close(&sc->sc_hdev); 502 return (0); 503 } 504 } 505 506 507 static void 508 ukbd_childdet(device_t self, device_t child) 509 { 510 struct ukbd_softc *sc = device_private(self); 511 512 KASSERT(sc->sc_wskbddev == child); 513 sc->sc_wskbddev = NULL; 514 } 515 516 int 517 ukbd_activate(device_t self, enum devact act) 518 { 519 struct ukbd_softc *sc = device_private(self); 520 521 switch (act) { 522 case DVACT_DEACTIVATE: 523 sc->sc_dying = 1; 524 return 0; 525 default: 526 return EOPNOTSUPP; 527 } 528 } 529 530 int 531 ukbd_detach(device_t self, int flags) 532 { 533 struct ukbd_softc *sc = device_private(self); 534 int rv = 0; 535 536 DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags)); 537 538 pmf_device_deregister(self); 539 540 if (sc->sc_console_keyboard) { 541 #if 0 542 /* 543 * XXX Should probably disconnect our consops, 544 * XXX and either notify some other keyboard that 545 * XXX it can now be the console, or if there aren't 546 * XXX any more USB keyboards, set ukbd_is_console 547 * XXX back to 1 so that the next USB keyboard attached 548 * XXX to the system will get it. 549 */ 550 panic("ukbd_detach: console keyboard"); 551 #else 552 /* 553 * Disconnect our consops and set ukbd_is_console 554 * back to 1 so that the next USB keyboard attached 555 * to the system will get it. 556 * XXX Should notify some other keyboard that it can be 557 * XXX console, if there are any other keyboards. 558 */ 559 printf("%s: was console keyboard\n", 560 device_xname(sc->sc_hdev.sc_dev)); 561 wskbd_cndetach(); 562 ukbd_is_console = 1; 563 #endif 564 } 565 /* No need to do reference counting of ukbd, wskbd has all the goo. */ 566 if (sc->sc_wskbddev != NULL) 567 rv = config_detach(sc->sc_wskbddev, flags); 568 569 /* The console keyboard does not get a disable call, so check pipe. */ 570 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) 571 uhidev_close(&sc->sc_hdev); 572 573 return (rv); 574 } 575 576 static void 577 ukbd_translate_keycodes(struct ukbd_softc *sc, struct ukbd_data *ud, 578 const struct ukbd_keycodetrans *tab) 579 { 580 const struct ukbd_keycodetrans *tp; 581 int i; 582 u_int8_t key; 583 584 for (i = 0; i < sc->sc_nkeycode; i++) { 585 key = ud->keycode[i]; 586 if (key) 587 for (tp = tab; tp->from; tp++) 588 if (tp->from == key) { 589 if (tp->to & IS_PMF) { 590 pmf_event_inject( 591 sc->sc_hdev.sc_dev, 592 tp->to & 0xff); 593 ud->keycode[i] = 0; 594 } else 595 ud->keycode[i] = tp->to; 596 break; 597 } 598 } 599 } 600 601 static u_int16_t 602 ukbd_translate_modifier(struct ukbd_softc *sc, u_int16_t key) 603 { 604 if ((sc->sc_flags & FLAG_APPLE_FN) && (key & CODEMASK) == 0x00e2) { 605 if ((key & ~CODEMASK) == PRESS) { 606 if (sc->sc_flags & FLAG_FN_PRESSED) { 607 /* pressed FN-Alt, translate to AltGr */ 608 key = 0x00e6 | PRESS; 609 sc->sc_flags |= FLAG_FN_ALT; 610 } 611 } else { 612 if (sc->sc_flags & FLAG_FN_ALT) { 613 /* released Alt, which was treated as FN-Alt */ 614 key = 0x00e6 | RELEASE; 615 sc->sc_flags &= ~FLAG_FN_ALT; 616 } 617 } 618 } 619 return key; 620 } 621 622 void 623 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len) 624 { 625 struct ukbd_softc *sc = (struct ukbd_softc *)addr; 626 struct ukbd_data *ud = &sc->sc_ndata; 627 int i; 628 629 #ifdef UKBD_DEBUG 630 if (ukbddebug > 5) { 631 printf("ukbd_intr: data"); 632 for (i = 0; i < len; i++) 633 printf(" 0x%02x", ((u_char *)ibuf)[i]); 634 printf("\n"); 635 } 636 #endif 637 638 ud->modifiers = 0; 639 for (i = 0; i < sc->sc_nmod; i++) 640 if (hid_get_data(ibuf, &sc->sc_modloc[i])) 641 ud->modifiers |= sc->sc_mods[i].mask; 642 memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8, 643 sc->sc_nkeycode); 644 645 if (sc->sc_flags & FLAG_APPLE_FN) { 646 if (hid_get_data(ibuf, &sc->sc_apple_fn)) { 647 sc->sc_flags |= FLAG_FN_PRESSED; 648 ukbd_translate_keycodes(sc, ud, trtab_apple_fn); 649 } 650 else 651 sc->sc_flags &= ~FLAG_FN_PRESSED; 652 } 653 654 #ifdef GDIUM_KEYBOARD_HACK 655 if (sc->sc_flags & FLAG_GDIUM_FN) { 656 if (sc->sc_flags & FLAG_FN_PRESSED) { 657 ukbd_translate_keycodes(sc, ud, trtab_gdium_fn); 658 } 659 } 660 #endif 661 662 if ((sc->sc_flags & FLAG_DEBOUNCE) && !(sc->sc_flags & FLAG_POLLING)) { 663 /* 664 * Some keyboards have a peculiar quirk. They sometimes 665 * generate a key up followed by a key down for the same 666 * key after about 10 ms. 667 * We avoid this bug by holding off decoding for 20 ms. 668 */ 669 sc->sc_data = *ud; 670 callout_reset(&sc->sc_delay, hz / 50, ukbd_delayed_decode, sc); 671 #ifdef DDB 672 } else if (sc->sc_console_keyboard && !(sc->sc_flags & FLAG_POLLING)) { 673 /* 674 * For the console keyboard we can't deliver CTL-ALT-ESC 675 * from the interrupt routine. Doing so would start 676 * polling from inside the interrupt routine and that 677 * loses bigtime. 678 */ 679 sc->sc_data = *ud; 680 callout_reset(&sc->sc_delay, 1, ukbd_delayed_decode, sc); 681 #endif 682 } else { 683 ukbd_decode(sc, ud); 684 } 685 } 686 687 void 688 ukbd_delayed_decode(void *addr) 689 { 690 struct ukbd_softc *sc = addr; 691 692 ukbd_decode(sc, &sc->sc_data); 693 } 694 695 void 696 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud) 697 { 698 int mod, omod; 699 u_int16_t ibuf[MAXKEYS]; /* chars events */ 700 int s; 701 int nkeys, i, j; 702 int key; 703 #define ADDKEY(c) ibuf[nkeys++] = (c) 704 705 #ifdef UKBD_DEBUG 706 /* 707 * Keep a trace of the last events. Using printf changes the 708 * timing, so this can be useful sometimes. 709 */ 710 if (ukbdtrace) { 711 struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex]; 712 p->unit = device_unit(sc->sc_hdev.sc_dev); 713 microtime(&p->tv); 714 p->ud = *ud; 715 if (++ukbdtraceindex >= UKBDTRACESIZE) 716 ukbdtraceindex = 0; 717 } 718 if (ukbddebug > 5) { 719 struct timeval tv; 720 microtime(&tv); 721 DPRINTF((" at %"PRIu64".%06"PRIu64" mod=0x%02x key0=0x%02x key1=0x%02x " 722 "key2=0x%02x key3=0x%02x\n", 723 tv.tv_sec, (uint64_t)tv.tv_usec, 724 ud->modifiers, ud->keycode[0], ud->keycode[1], 725 ud->keycode[2], ud->keycode[3])); 726 } 727 #endif 728 729 if (ud->keycode[0] == KEY_ERROR) { 730 DPRINTF(("ukbd_intr: KEY_ERROR\n")); 731 return; /* ignore */ 732 } 733 734 if (sc->sc_flags & FLAG_APPLE_FIX_ISO) 735 ukbd_translate_keycodes(sc, ud, trtab_apple_iso); 736 737 nkeys = 0; 738 mod = ud->modifiers; 739 omod = sc->sc_odata.modifiers; 740 if (mod != omod) 741 for (i = 0; i < sc->sc_nmod; i++) 742 if (( mod & sc->sc_mods[i].mask) != 743 (omod & sc->sc_mods[i].mask)) { 744 key = sc->sc_mods[i].key | 745 ((mod & sc->sc_mods[i].mask) ? 746 PRESS : RELEASE); 747 ADDKEY(ukbd_translate_modifier(sc, key)); 748 } 749 if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) { 750 /* Check for released keys. */ 751 for (i = 0; i < sc->sc_nkeycode; i++) { 752 key = sc->sc_odata.keycode[i]; 753 if (key == 0) 754 continue; 755 for (j = 0; j < sc->sc_nkeycode; j++) 756 if (key == ud->keycode[j]) 757 goto rfound; 758 DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key)); 759 #ifdef GDIUM_KEYBOARD_HACK 760 if (sc->sc_flags & FLAG_GDIUM_FN) { 761 if (key == 0x82) { 762 sc->sc_flags &= ~FLAG_FN_PRESSED; 763 goto rfound; 764 } 765 } 766 #endif 767 ADDKEY(key | RELEASE); 768 rfound: 769 ; 770 } 771 772 /* Check for pressed keys. */ 773 for (i = 0; i < sc->sc_nkeycode; i++) { 774 key = ud->keycode[i]; 775 if (key == 0) 776 continue; 777 for (j = 0; j < sc->sc_nkeycode; j++) 778 if (key == sc->sc_odata.keycode[j]) 779 goto pfound; 780 DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key)); 781 #ifdef GDIUM_KEYBOARD_HACK 782 if (sc->sc_flags & FLAG_GDIUM_FN) { 783 if (key == 0x82) { 784 sc->sc_flags |= FLAG_FN_PRESSED; 785 goto pfound; 786 } 787 } 788 #endif 789 ADDKEY(key | PRESS); 790 pfound: 791 ; 792 } 793 } 794 sc->sc_odata = *ud; 795 796 if (nkeys == 0) 797 return; 798 799 if (sc->sc_flags & FLAG_POLLING) { 800 DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0])); 801 memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t)); 802 sc->sc_npollchar = nkeys; 803 return; 804 } 805 #ifdef WSDISPLAY_COMPAT_RAWKBD 806 if (sc->sc_rawkbd) { 807 u_char cbuf[MAXKEYS * 2]; 808 int c; 809 int npress; 810 811 for (npress = i = j = 0; i < nkeys; i++) { 812 key = ibuf[i]; 813 c = ukbd_trtab[key & CODEMASK]; 814 if (c == NN) 815 continue; 816 if (c == 0x7f) { 817 /* pause key */ 818 cbuf[j++] = 0xe1; 819 cbuf[j++] = 0x1d; 820 cbuf[j-1] |= (key & RELEASE) ? 0x80 : 0; 821 cbuf[j] = 0x45; 822 } else { 823 if (c & 0x80) 824 cbuf[j++] = 0xe0; 825 cbuf[j] = c & 0x7f; 826 } 827 if (key & RELEASE) 828 cbuf[j] |= 0x80; 829 #if defined(UKBD_REPEAT) 830 else { 831 /* remember pressed keys for autorepeat */ 832 if (c & 0x80) 833 sc->sc_rep[npress++] = 0xe0; 834 sc->sc_rep[npress++] = c & 0x7f; 835 } 836 #endif 837 DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n", 838 c & 0x80 ? "0xe0 " : "", 839 cbuf[j])); 840 j++; 841 } 842 s = spltty(); 843 wskbd_rawinput(sc->sc_wskbddev, cbuf, j); 844 splx(s); 845 #ifdef UKBD_REPEAT 846 callout_stop(&sc->sc_rawrepeat_ch); 847 if (npress != 0) { 848 sc->sc_nrep = npress; 849 callout_reset(&sc->sc_rawrepeat_ch, 850 hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc); 851 } 852 #endif 853 return; 854 } 855 #endif 856 857 s = spltty(); 858 for (i = 0; i < nkeys; i++) { 859 key = ibuf[i]; 860 wskbd_input(sc->sc_wskbddev, 861 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN, 862 key&CODEMASK); 863 } 864 splx(s); 865 } 866 867 void 868 ukbd_set_leds(void *v, int leds) 869 { 870 struct ukbd_softc *sc = v; 871 u_int8_t res; 872 873 DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n", 874 sc, leds, sc->sc_leds)); 875 876 if (sc->sc_dying) 877 return; 878 879 if (sc->sc_leds == leds) 880 return; 881 sc->sc_leds = leds; 882 res = 0; 883 /* XXX not really right */ 884 if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1) 885 res |= 1 << sc->sc_scroloc.pos; 886 if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1) 887 res |= 1 << sc->sc_numloc.pos; 888 if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1) 889 res |= 1 << sc->sc_capsloc.pos; 890 uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1); 891 } 892 893 #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) 894 void 895 ukbd_rawrepeat(void *v) 896 { 897 struct ukbd_softc *sc = v; 898 int s; 899 900 s = spltty(); 901 wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep); 902 splx(s); 903 callout_reset(&sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000, 904 ukbd_rawrepeat, sc); 905 } 906 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) */ 907 908 int 909 ukbd_ioctl(void *v, u_long cmd, void *data, int flag, 910 struct lwp *l) 911 { 912 struct ukbd_softc *sc = v; 913 914 switch (cmd) { 915 case WSKBDIO_GTYPE: 916 *(int *)data = WSKBD_TYPE_USB; 917 return (0); 918 case WSKBDIO_SETLEDS: 919 ukbd_set_leds(v, *(int *)data); 920 return (0); 921 case WSKBDIO_GETLEDS: 922 *(int *)data = sc->sc_leds; 923 return (0); 924 #if defined(WSDISPLAY_COMPAT_RAWKBD) 925 case WSKBDIO_SETMODE: 926 DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data)); 927 sc->sc_rawkbd = *(int *)data == WSKBD_RAW; 928 #if defined(UKBD_REPEAT) 929 callout_stop(&sc->sc_rawrepeat_ch); 930 #endif 931 return (0); 932 #endif 933 } 934 return (EPASSTHROUGH); 935 } 936 937 /* 938 * This is a hack to work around some broken ports that don't call 939 * cnpollc() before cngetc(). 940 */ 941 static int pollenter, warned; 942 943 /* Console interface. */ 944 void 945 ukbd_cngetc(void *v, u_int *type, int *data) 946 { 947 struct ukbd_softc *sc = v; 948 int c; 949 int broken; 950 951 if (pollenter == 0) { 952 if (!warned) { 953 printf("\n" 954 "This port is broken, it does not call cnpollc() before calling cngetc().\n" 955 "This should be fixed, but it will work anyway (for now).\n"); 956 warned = 1; 957 } 958 broken = 1; 959 ukbd_cnpollc(v, 1); 960 } else 961 broken = 0; 962 963 DPRINTFN(0,("ukbd_cngetc: enter\n")); 964 sc->sc_flags |= FLAG_POLLING; 965 while(sc->sc_npollchar <= 0) 966 usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface); 967 sc->sc_flags &= ~FLAG_POLLING; 968 c = sc->sc_pollchars[0]; 969 sc->sc_npollchar--; 970 memcpy(sc->sc_pollchars, sc->sc_pollchars+1, 971 sc->sc_npollchar * sizeof(u_int16_t)); 972 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN; 973 *data = c & CODEMASK; 974 DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c)); 975 if (broken) 976 ukbd_cnpollc(v, 0); 977 } 978 979 void 980 ukbd_cnpollc(void *v, int on) 981 { 982 struct ukbd_softc *sc = v; 983 usbd_device_handle dev; 984 985 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on)); 986 987 usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev); 988 if (on) { 989 sc->sc_spl = splusb(); 990 pollenter++; 991 } else { 992 splx(sc->sc_spl); 993 pollenter--; 994 } 995 usbd_set_polling(dev, on); 996 } 997 998 int 999 ukbd_cnattach(void) 1000 { 1001 1002 /* 1003 * XXX USB requires too many parts of the kernel to be running 1004 * XXX in order to work, so we can't do much for the console 1005 * XXX keyboard until autconfiguration has run its course. 1006 */ 1007 ukbd_is_console = 1; 1008 return (0); 1009 } 1010 1011 const char * 1012 ukbd_parse_desc(struct ukbd_softc *sc) 1013 { 1014 struct hid_data *d; 1015 struct hid_item h; 1016 int size; 1017 void *desc; 1018 int imod; 1019 1020 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size); 1021 imod = 0; 1022 sc->sc_nkeycode = 0; 1023 d = hid_start_parse(desc, size, hid_input); 1024 while (hid_get_item(d, &h)) { 1025 /*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n", 1026 h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/ 1027 1028 /* Check for special Apple notebook FN key */ 1029 if (HID_GET_USAGE_PAGE(h.usage) == 0x00ff && 1030 HID_GET_USAGE(h.usage) == 0x0003 && 1031 h.kind == hid_input && (h.flags & HIO_VARIABLE)) { 1032 sc->sc_flags |= FLAG_APPLE_FN; 1033 sc->sc_apple_fn = h.loc; 1034 } 1035 1036 if (h.kind != hid_input || (h.flags & HIO_CONST) || 1037 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD || 1038 h.report_ID != sc->sc_hdev.sc_report_id) 1039 continue; 1040 DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d " 1041 "cnt=%d\n", imod, 1042 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count)); 1043 if (h.flags & HIO_VARIABLE) { 1044 if (h.loc.size != 1) 1045 return ("bad modifier size"); 1046 /* Single item */ 1047 if (imod < MAXMOD) { 1048 sc->sc_modloc[imod] = h.loc; 1049 sc->sc_mods[imod].mask = 1 << imod; 1050 sc->sc_mods[imod].key = HID_GET_USAGE(h.usage); 1051 imod++; 1052 } else 1053 return ("too many modifier keys"); 1054 } else { 1055 /* Array */ 1056 if (h.loc.size != 8) 1057 return ("key code size != 8"); 1058 if (h.loc.count > MAXKEYCODE) 1059 h.loc.count = MAXKEYCODE; 1060 if (h.loc.pos % 8 != 0) 1061 return ("key codes not on byte boundary"); 1062 if (sc->sc_nkeycode != 0) 1063 return ("multiple key code arrays\n"); 1064 sc->sc_keycodeloc = h.loc; 1065 sc->sc_nkeycode = h.loc.count; 1066 } 1067 } 1068 sc->sc_nmod = imod; 1069 hid_end_parse(d); 1070 1071 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK), 1072 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL); 1073 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK), 1074 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL); 1075 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK), 1076 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL); 1077 1078 return (NULL); 1079 } 1080