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