1 #include <sys/cdefs.h> 2 __FBSDID("$FreeBSD$"); 3 4 5 /*- 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Lennart Augustsson (lennart@augustsson.net) at 11 * Carlstedt Research & Technology. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 * 34 */ 35 36 /* 37 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 38 */ 39 40 #include "opt_compat.h" 41 #include "opt_kbd.h" 42 #include "opt_ukbd.h" 43 44 #include <sys/stdint.h> 45 #include <sys/stddef.h> 46 #include <sys/param.h> 47 #include <sys/queue.h> 48 #include <sys/types.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/bus.h> 52 #include <sys/linker_set.h> 53 #include <sys/module.h> 54 #include <sys/lock.h> 55 #include <sys/mutex.h> 56 #include <sys/condvar.h> 57 #include <sys/sysctl.h> 58 #include <sys/sx.h> 59 #include <sys/unistd.h> 60 #include <sys/callout.h> 61 #include <sys/malloc.h> 62 #include <sys/priv.h> 63 #include <sys/kdb.h> 64 65 #include <dev/usb/usb.h> 66 #include <dev/usb/usbdi.h> 67 #include <dev/usb/usbdi_util.h> 68 #include <dev/usb/usbhid.h> 69 70 #define USB_DEBUG_VAR ukbd_debug 71 #include <dev/usb/usb_debug.h> 72 73 #include <dev/usb/quirk/usb_quirk.h> 74 75 #include <sys/ioccom.h> 76 #include <sys/filio.h> 77 #include <sys/tty.h> 78 #include <sys/kbio.h> 79 80 #include <dev/kbd/kbdreg.h> 81 82 /* the initial key map, accent map and fkey strings */ 83 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE) 84 #define KBD_DFLT_KEYMAP 85 #include "ukbdmap.h" 86 #endif 87 88 /* the following file must be included after "ukbdmap.h" */ 89 #include <dev/kbd/kbdtables.h> 90 91 #if USB_DEBUG 92 static int ukbd_debug = 0; 93 static int ukbd_no_leds = 0; 94 95 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd"); 96 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW, 97 &ukbd_debug, 0, "Debug level"); 98 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW, 99 &ukbd_no_leds, 0, "Disables setting of keyboard leds"); 100 101 TUNABLE_INT("hw.usb.ukbd.debug", &ukbd_debug); 102 TUNABLE_INT("hw.usb.ukbd.no_leds", &ukbd_no_leds); 103 #endif 104 105 #define UPROTO_BOOT_KEYBOARD 1 106 107 #define UKBD_EMULATE_ATSCANCODE 1 108 #define UKBD_DRIVER_NAME "ukbd" 109 #define UKBD_NMOD 8 /* units */ 110 #define UKBD_NKEYCODE 6 /* units */ 111 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */ 112 #define UKBD_IN_BUF_FULL (UKBD_IN_BUF_SIZE / 2) /* bytes */ 113 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */ 114 115 struct ukbd_data { 116 uint8_t modifiers; 117 #define MOD_CONTROL_L 0x01 118 #define MOD_CONTROL_R 0x10 119 #define MOD_SHIFT_L 0x02 120 #define MOD_SHIFT_R 0x20 121 #define MOD_ALT_L 0x04 122 #define MOD_ALT_R 0x40 123 #define MOD_WIN_L 0x08 124 #define MOD_WIN_R 0x80 125 uint8_t reserved; 126 uint8_t keycode[UKBD_NKEYCODE]; 127 uint8_t exten[8]; 128 }; 129 130 enum { 131 UKBD_INTR_DT, 132 UKBD_CTRL_LED, 133 UKBD_N_TRANSFER, 134 }; 135 136 struct ukbd_softc { 137 keyboard_t sc_kbd; 138 keymap_t sc_keymap; 139 accentmap_t sc_accmap; 140 fkeytab_t sc_fkeymap[UKBD_NFKEY]; 141 struct hid_location sc_loc_apple_eject; 142 struct hid_location sc_loc_apple_fn; 143 struct usb_callout sc_callout; 144 struct ukbd_data sc_ndata; 145 struct ukbd_data sc_odata; 146 147 struct thread *sc_poll_thread; 148 struct usb_device *sc_udev; 149 struct usb_interface *sc_iface; 150 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER]; 151 152 uint32_t sc_ntime[UKBD_NKEYCODE]; 153 uint32_t sc_otime[UKBD_NKEYCODE]; 154 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */ 155 uint32_t sc_time_ms; 156 uint32_t sc_composed_char; /* composed char code, if non-zero */ 157 #ifdef UKBD_EMULATE_ATSCANCODE 158 uint32_t sc_buffered_char[2]; 159 #endif 160 uint32_t sc_flags; /* flags */ 161 #define UKBD_FLAG_COMPOSE 0x0001 162 #define UKBD_FLAG_POLLING 0x0002 163 #define UKBD_FLAG_SET_LEDS 0x0004 164 #define UKBD_FLAG_ATTACHED 0x0010 165 #define UKBD_FLAG_GONE 0x0020 166 #define UKBD_FLAG_APPLE_EJECT 0x0040 167 #define UKBD_FLAG_APPLE_FN 0x0080 168 #define UKBD_FLAG_APPLE_SWAP 0x0100 169 #define UKBD_FLAG_TIMER_RUNNING 0x0200 170 171 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ 172 int sc_state; /* shift/lock key state */ 173 int sc_accents; /* accent key index (> 0) */ 174 int sc_poll_tick_last; 175 176 uint16_t sc_inputs; 177 uint16_t sc_inputhead; 178 uint16_t sc_inputtail; 179 180 uint8_t sc_leds; /* store for async led requests */ 181 uint8_t sc_iface_index; 182 uint8_t sc_iface_no; 183 uint8_t sc_kbd_id; 184 uint8_t sc_led_id; 185 uint8_t sc_poll_detected; 186 }; 187 188 #define KEY_ERROR 0x01 189 190 #define KEY_PRESS 0 191 #define KEY_RELEASE 0x400 192 #define KEY_INDEX(c) ((c) & 0xFF) 193 194 #define SCAN_PRESS 0 195 #define SCAN_RELEASE 0x80 196 #define SCAN_PREFIX_E0 0x100 197 #define SCAN_PREFIX_E1 0x200 198 #define SCAN_PREFIX_CTL 0x400 199 #define SCAN_PREFIX_SHIFT 0x800 200 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \ 201 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT) 202 #define SCAN_CHAR(c) ((c) & 0x7f) 203 204 struct ukbd_mods { 205 uint32_t mask, key; 206 }; 207 208 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = { 209 {MOD_CONTROL_L, 0xe0}, 210 {MOD_CONTROL_R, 0xe4}, 211 {MOD_SHIFT_L, 0xe1}, 212 {MOD_SHIFT_R, 0xe5}, 213 {MOD_ALT_L, 0xe2}, 214 {MOD_ALT_R, 0xe6}, 215 {MOD_WIN_L, 0xe3}, 216 {MOD_WIN_R, 0xe7}, 217 }; 218 219 #define NN 0 /* no translation */ 220 /* 221 * Translate USB keycodes to AT keyboard scancodes. 222 */ 223 /* 224 * FIXME: Mac USB keyboard generates: 225 * 0x53: keypad NumLock/Clear 226 * 0x66: Power 227 * 0x67: keypad = 228 * 0x68: F13 229 * 0x69: F14 230 * 0x6a: F15 231 */ 232 static const uint8_t ukbd_trtab[256] = { 233 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */ 234 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */ 235 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */ 236 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */ 237 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */ 238 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */ 239 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */ 240 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */ 241 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */ 242 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */ 243 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */ 244 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */ 245 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */ 246 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */ 247 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */ 248 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */ 249 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */ 250 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */ 251 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */ 252 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */ 253 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */ 254 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */ 255 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */ 256 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */ 257 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */ 258 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */ 259 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */ 260 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */ 261 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */ 262 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */ 263 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */ 264 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */ 265 }; 266 267 /* prototypes */ 268 static void ukbd_timeout(void *); 269 static void ukbd_set_leds(struct ukbd_softc *, uint8_t); 270 static int ukbd_set_typematic(keyboard_t *, int); 271 #ifdef UKBD_EMULATE_ATSCANCODE 272 static int ukbd_key2scan(struct ukbd_softc *, int, int, int); 273 #endif 274 static uint32_t ukbd_read_char(keyboard_t *, int); 275 static void ukbd_clear_state(keyboard_t *); 276 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t); 277 static int ukbd_enable(keyboard_t *); 278 static int ukbd_disable(keyboard_t *); 279 static void ukbd_interrupt(struct ukbd_softc *); 280 static int ukbd_is_polling(struct ukbd_softc *); 281 static int ukbd_polls_other_thread(struct ukbd_softc *); 282 static void ukbd_event_keyinput(struct ukbd_softc *); 283 284 static device_probe_t ukbd_probe; 285 static device_attach_t ukbd_attach; 286 static device_detach_t ukbd_detach; 287 static device_resume_t ukbd_resume; 288 289 static uint8_t 290 ukbd_any_key_pressed(struct ukbd_softc *sc) 291 { 292 uint8_t i; 293 uint8_t j; 294 295 for (j = i = 0; i < UKBD_NKEYCODE; i++) 296 j |= sc->sc_odata.keycode[i]; 297 298 return (j ? 1 : 0); 299 } 300 301 static void 302 ukbd_start_timer(struct ukbd_softc *sc) 303 { 304 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING; 305 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc); 306 } 307 308 static void 309 ukbd_put_key(struct ukbd_softc *sc, uint32_t key) 310 { 311 mtx_assert(&Giant, MA_OWNED); 312 313 DPRINTF("0x%02x (%d) %s\n", key, key, 314 (key & KEY_RELEASE) ? "released" : "pressed"); 315 316 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) { 317 sc->sc_input[sc->sc_inputtail] = key; 318 ++(sc->sc_inputs); 319 ++(sc->sc_inputtail); 320 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) { 321 sc->sc_inputtail = 0; 322 } 323 } else { 324 DPRINTF("input buffer is full\n"); 325 } 326 } 327 328 static void 329 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait) 330 { 331 DPRINTFN(2, "polling\n"); 332 333 /* update stats about last polling event */ 334 sc->sc_poll_tick_last = ticks; 335 sc->sc_poll_detected = 1; 336 337 if (kdb_active == 0) { 338 while (sc->sc_inputs == 0) { 339 /* make sure the USB code gets a chance to run */ 340 pause("UKBD", 1); 341 342 /* check if we should wait */ 343 if (!wait) 344 break; 345 } 346 return; /* Only poll if KDB is active */ 347 } 348 349 while (sc->sc_inputs == 0) { 350 351 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER); 352 353 /* Delay-optimised support for repetition of keys */ 354 355 if (ukbd_any_key_pressed(sc)) { 356 /* a key is pressed - need timekeeping */ 357 DELAY(1000); 358 359 /* 1 millisecond has passed */ 360 sc->sc_time_ms += 1; 361 } 362 363 ukbd_interrupt(sc); 364 365 if (!wait) 366 break; 367 } 368 } 369 370 static int32_t 371 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait) 372 { 373 int32_t c; 374 375 mtx_assert(&Giant, MA_OWNED); 376 377 if (sc->sc_inputs == 0) { 378 /* start transfer, if not already started */ 379 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]); 380 } 381 382 if (ukbd_polls_other_thread(sc)) 383 return (-1); 384 385 if (sc->sc_flags & UKBD_FLAG_POLLING) 386 ukbd_do_poll(sc, wait); 387 388 if (sc->sc_inputs == 0) { 389 c = -1; 390 } else { 391 c = sc->sc_input[sc->sc_inputhead]; 392 --(sc->sc_inputs); 393 ++(sc->sc_inputhead); 394 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) { 395 sc->sc_inputhead = 0; 396 } 397 } 398 return (c); 399 } 400 401 static void 402 ukbd_interrupt(struct ukbd_softc *sc) 403 { 404 uint32_t n_mod; 405 uint32_t o_mod; 406 uint32_t now = sc->sc_time_ms; 407 uint32_t dtime; 408 uint8_t key; 409 uint8_t i; 410 uint8_t j; 411 412 if (sc->sc_ndata.keycode[0] == KEY_ERROR) 413 return; 414 415 n_mod = sc->sc_ndata.modifiers; 416 o_mod = sc->sc_odata.modifiers; 417 if (n_mod != o_mod) { 418 for (i = 0; i < UKBD_NMOD; i++) { 419 if ((n_mod & ukbd_mods[i].mask) != 420 (o_mod & ukbd_mods[i].mask)) { 421 ukbd_put_key(sc, ukbd_mods[i].key | 422 ((n_mod & ukbd_mods[i].mask) ? 423 KEY_PRESS : KEY_RELEASE)); 424 } 425 } 426 } 427 /* Check for released keys. */ 428 for (i = 0; i < UKBD_NKEYCODE; i++) { 429 key = sc->sc_odata.keycode[i]; 430 if (key == 0) { 431 continue; 432 } 433 for (j = 0; j < UKBD_NKEYCODE; j++) { 434 if (sc->sc_ndata.keycode[j] == 0) { 435 continue; 436 } 437 if (key == sc->sc_ndata.keycode[j]) { 438 goto rfound; 439 } 440 } 441 ukbd_put_key(sc, key | KEY_RELEASE); 442 rfound: ; 443 } 444 445 /* Check for pressed keys. */ 446 for (i = 0; i < UKBD_NKEYCODE; i++) { 447 key = sc->sc_ndata.keycode[i]; 448 if (key == 0) { 449 continue; 450 } 451 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1; 452 for (j = 0; j < UKBD_NKEYCODE; j++) { 453 if (sc->sc_odata.keycode[j] == 0) { 454 continue; 455 } 456 if (key == sc->sc_odata.keycode[j]) { 457 458 /* key is still pressed */ 459 460 sc->sc_ntime[i] = sc->sc_otime[j]; 461 dtime = (sc->sc_otime[j] - now); 462 463 if (!(dtime & 0x80000000)) { 464 /* time has not elapsed */ 465 goto pfound; 466 } 467 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2; 468 break; 469 } 470 } 471 ukbd_put_key(sc, key | KEY_PRESS); 472 473 /* 474 * If any other key is presently down, force its repeat to be 475 * well in the future (100s). This makes the last key to be 476 * pressed do the autorepeat. 477 */ 478 for (j = 0; j != UKBD_NKEYCODE; j++) { 479 if (j != i) 480 sc->sc_ntime[j] = now + (100 * 1000); 481 } 482 pfound: ; 483 } 484 485 sc->sc_odata = sc->sc_ndata; 486 487 memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime)); 488 489 ukbd_event_keyinput(sc); 490 } 491 492 static void 493 ukbd_event_keyinput(struct ukbd_softc *sc) 494 { 495 int c; 496 497 if (ukbd_is_polling(sc)) 498 return; 499 500 if (sc->sc_inputs == 0) 501 return; 502 503 if (KBD_IS_ACTIVE(&sc->sc_kbd) && 504 KBD_IS_BUSY(&sc->sc_kbd)) { 505 /* let the callback function process the input */ 506 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, 507 sc->sc_kbd.kb_callback.kc_arg); 508 } else { 509 /* read and discard the input, no one is waiting for it */ 510 do { 511 c = ukbd_read_char(&sc->sc_kbd, 0); 512 } while (c != NOKEY); 513 } 514 } 515 516 static void 517 ukbd_timeout(void *arg) 518 { 519 struct ukbd_softc *sc = arg; 520 521 mtx_assert(&Giant, MA_OWNED); 522 523 sc->sc_time_ms += 25; /* milliseconds */ 524 525 ukbd_interrupt(sc); 526 527 /* Make sure any leftover key events gets read out */ 528 ukbd_event_keyinput(sc); 529 530 if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) { 531 ukbd_start_timer(sc); 532 } else { 533 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING; 534 } 535 } 536 537 static uint8_t 538 ukbd_apple_fn(uint8_t keycode) { 539 switch (keycode) { 540 case 0x28: return 0x49; /* RETURN -> INSERT */ 541 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */ 542 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */ 543 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */ 544 case 0x52: return 0x4b; /* UP ARROW -> PGUP */ 545 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */ 546 default: return keycode; 547 } 548 } 549 550 static uint8_t 551 ukbd_apple_swap(uint8_t keycode) { 552 switch (keycode) { 553 case 0x35: return 0x64; 554 case 0x64: return 0x35; 555 default: return keycode; 556 } 557 } 558 559 static void 560 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error) 561 { 562 struct ukbd_softc *sc = usbd_xfer_softc(xfer); 563 struct usb_page_cache *pc; 564 uint8_t i; 565 uint8_t offset; 566 uint8_t id; 567 uint8_t apple_fn; 568 uint8_t apple_eject; 569 int len; 570 571 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 572 pc = usbd_xfer_get_frame(xfer, 0); 573 574 switch (USB_GET_STATE(xfer)) { 575 case USB_ST_TRANSFERRED: 576 DPRINTF("actlen=%d bytes\n", len); 577 578 if (len == 0) { 579 DPRINTF("zero length data\n"); 580 goto tr_setup; 581 } 582 583 if (sc->sc_kbd_id != 0) { 584 /* check and remove HID ID byte */ 585 usbd_copy_out(pc, 0, &id, 1); 586 if (id != sc->sc_kbd_id) { 587 DPRINTF("wrong HID ID\n"); 588 goto tr_setup; 589 } 590 offset = 1; 591 len--; 592 } else { 593 offset = 0; 594 } 595 596 if (len > sizeof(sc->sc_ndata)) { 597 len = sizeof(sc->sc_ndata); 598 } 599 600 if (len) { 601 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); 602 usbd_copy_out(pc, offset, &sc->sc_ndata, len); 603 604 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) && 605 hid_get_data((uint8_t *)&sc->sc_ndata, 606 len, &sc->sc_loc_apple_eject)) 607 apple_eject = 1; 608 else 609 apple_eject = 0; 610 611 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) && 612 hid_get_data((uint8_t *)&sc->sc_ndata, 613 len, &sc->sc_loc_apple_fn)) 614 apple_fn = 1; 615 else 616 apple_fn = 0; 617 #if USB_DEBUG 618 DPRINTF("apple_eject=%u apple_fn=%u\n", 619 apple_eject, apple_fn); 620 621 if (sc->sc_ndata.modifiers) { 622 DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers); 623 } 624 for (i = 0; i < UKBD_NKEYCODE; i++) { 625 if (sc->sc_ndata.keycode[i]) { 626 DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]); 627 } 628 } 629 #endif /* USB_DEBUG */ 630 631 if (apple_fn) { 632 for (i = 0; i < UKBD_NKEYCODE; i++) { 633 sc->sc_ndata.keycode[i] = 634 ukbd_apple_fn(sc->sc_ndata.keycode[i]); 635 } 636 } 637 638 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) { 639 for (i = 0; i < UKBD_NKEYCODE; i++) { 640 sc->sc_ndata.keycode[i] = 641 ukbd_apple_swap(sc->sc_ndata.keycode[i]); 642 } 643 } 644 645 ukbd_interrupt(sc); 646 647 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) { 648 if (ukbd_any_key_pressed(sc)) { 649 ukbd_start_timer(sc); 650 } 651 } 652 } 653 case USB_ST_SETUP: 654 tr_setup: 655 if (sc->sc_inputs < UKBD_IN_BUF_FULL) { 656 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 657 usbd_transfer_submit(xfer); 658 } else { 659 DPRINTF("input queue is full!\n"); 660 } 661 break; 662 663 default: /* Error */ 664 DPRINTF("error=%s\n", usbd_errstr(error)); 665 666 if (error != USB_ERR_CANCELLED) { 667 /* try to clear stall first */ 668 usbd_xfer_set_stall(xfer); 669 goto tr_setup; 670 } 671 break; 672 } 673 } 674 675 static void 676 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error) 677 { 678 struct usb_device_request req; 679 struct usb_page_cache *pc; 680 uint8_t buf[2]; 681 struct ukbd_softc *sc = usbd_xfer_softc(xfer); 682 683 #if USB_DEBUG 684 if (ukbd_no_leds) 685 return; 686 #endif 687 688 switch (USB_GET_STATE(xfer)) { 689 case USB_ST_TRANSFERRED: 690 case USB_ST_SETUP: 691 if (sc->sc_flags & UKBD_FLAG_SET_LEDS) { 692 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS; 693 694 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 695 req.bRequest = UR_SET_REPORT; 696 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0); 697 req.wIndex[0] = sc->sc_iface_no; 698 req.wIndex[1] = 0; 699 req.wLength[1] = 0; 700 701 /* check if we need to prefix an ID byte */ 702 if (sc->sc_led_id != 0) { 703 req.wLength[0] = 2; 704 buf[0] = sc->sc_led_id; 705 buf[1] = sc->sc_leds; 706 } else { 707 req.wLength[0] = 1; 708 buf[0] = sc->sc_leds; 709 buf[1] = 0; 710 } 711 712 pc = usbd_xfer_get_frame(xfer, 0); 713 usbd_copy_in(pc, 0, &req, sizeof(req)); 714 pc = usbd_xfer_get_frame(xfer, 1); 715 usbd_copy_in(pc, 0, buf, sizeof(buf)); 716 717 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 718 usbd_xfer_set_frame_len(xfer, 1, req.wLength[0]); 719 usbd_xfer_set_frames(xfer, 2); 720 usbd_transfer_submit(xfer); 721 } 722 break; 723 724 default: /* Error */ 725 DPRINTFN(0, "error=%s\n", usbd_errstr(error)); 726 break; 727 } 728 } 729 730 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = { 731 732 [UKBD_INTR_DT] = { 733 .type = UE_INTERRUPT, 734 .endpoint = UE_ADDR_ANY, 735 .direction = UE_DIR_IN, 736 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 737 .bufsize = 0, /* use wMaxPacketSize */ 738 .callback = &ukbd_intr_callback, 739 }, 740 741 [UKBD_CTRL_LED] = { 742 .type = UE_CONTROL, 743 .endpoint = 0x00, /* Control pipe */ 744 .direction = UE_DIR_ANY, 745 .bufsize = sizeof(struct usb_device_request) + 8, 746 .callback = &ukbd_set_leds_callback, 747 .timeout = 1000, /* 1 second */ 748 }, 749 }; 750 751 static int 752 ukbd_probe(device_t dev) 753 { 754 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME); 755 struct usb_attach_arg *uaa = device_get_ivars(dev); 756 void *d_ptr; 757 int error; 758 uint16_t d_len; 759 760 DPRINTFN(11, "\n"); 761 762 if (sw == NULL) { 763 return (ENXIO); 764 } 765 if (uaa->usb_mode != USB_MODE_HOST) { 766 return (ENXIO); 767 } 768 769 if (uaa->info.bInterfaceClass != UICLASS_HID) 770 return (ENXIO); 771 772 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 773 (uaa->info.bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)) { 774 if (usb_test_quirk(uaa, UQ_KBD_IGNORE)) 775 return (ENXIO); 776 else 777 return (BUS_PROBE_GENERIC); 778 } 779 780 error = usbd_req_get_hid_desc(uaa->device, NULL, 781 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); 782 783 if (error) 784 return (ENXIO); 785 786 /* 787 * NOTE: we currently don't support USB mouse and USB keyboard 788 * on the same USB endpoint. 789 */ 790 if (hid_is_collection(d_ptr, d_len, 791 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) { 792 /* most likely a mouse */ 793 error = ENXIO; 794 } else if (hid_is_collection(d_ptr, d_len, 795 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) { 796 if (usb_test_quirk(uaa, UQ_KBD_IGNORE)) 797 error = ENXIO; 798 else 799 error = BUS_PROBE_GENERIC; 800 } else 801 error = ENXIO; 802 803 free(d_ptr, M_TEMP); 804 return (error); 805 } 806 807 static int 808 ukbd_attach(device_t dev) 809 { 810 struct ukbd_softc *sc = device_get_softc(dev); 811 struct usb_attach_arg *uaa = device_get_ivars(dev); 812 int32_t unit = device_get_unit(dev); 813 keyboard_t *kbd = &sc->sc_kbd; 814 void *hid_ptr = NULL; 815 usb_error_t err; 816 uint32_t flags; 817 uint16_t n; 818 uint16_t hid_len; 819 820 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); 821 822 kbd->kb_data = (void *)sc; 823 824 device_set_usb_desc(dev); 825 826 sc->sc_udev = uaa->device; 827 sc->sc_iface = uaa->iface; 828 sc->sc_iface_index = uaa->info.bIfaceIndex; 829 sc->sc_iface_no = uaa->info.bIfaceNum; 830 sc->sc_mode = K_XLATE; 831 832 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0); 833 834 err = usbd_transfer_setup(uaa->device, 835 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config, 836 UKBD_N_TRANSFER, sc, &Giant); 837 838 if (err) { 839 DPRINTF("error=%s\n", usbd_errstr(err)); 840 goto detach; 841 } 842 /* setup default keyboard maps */ 843 844 sc->sc_keymap = key_map; 845 sc->sc_accmap = accent_map; 846 for (n = 0; n < UKBD_NFKEY; n++) { 847 sc->sc_fkeymap[n] = fkey_tab[n]; 848 } 849 850 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, 851 sc->sc_fkeymap, UKBD_NFKEY); 852 853 KBD_FOUND_DEVICE(kbd); 854 855 ukbd_clear_state(kbd); 856 857 /* 858 * FIXME: set the initial value for lock keys in "sc_state" 859 * according to the BIOS data? 860 */ 861 KBD_PROBE_DONE(kbd); 862 863 /* 864 * Set boot protocol if we need the quirk. 865 */ 866 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO)) { 867 err = usbd_req_set_protocol(sc->sc_udev, NULL, 868 sc->sc_iface_index, 0); 869 if (err != USB_ERR_NORMAL_COMPLETION) { 870 DPRINTF("set protocol error=%s\n", usbd_errstr(err)); 871 goto detach; 872 } 873 } 874 875 /* figure out if there is an ID byte in the data */ 876 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr, 877 &hid_len, M_TEMP, uaa->info.bIfaceIndex); 878 if (err == 0) { 879 uint8_t apple_keys = 0; 880 uint8_t temp_id; 881 882 /* investigate if this is an Apple Keyboard */ 883 if (hid_locate(hid_ptr, hid_len, 884 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT), 885 hid_input, 0, &sc->sc_loc_apple_eject, &flags, 886 &temp_id)) { 887 if (flags & HIO_VARIABLE) 888 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT | 889 UKBD_FLAG_APPLE_SWAP; 890 DPRINTFN(1, "Found Apple eject-key\n"); 891 apple_keys = 1; 892 sc->sc_kbd_id = temp_id; 893 } 894 if (hid_locate(hid_ptr, hid_len, 895 HID_USAGE2(0xFFFF, 0x0003), 896 hid_input, 0, &sc->sc_loc_apple_fn, &flags, 897 &temp_id)) { 898 if (flags & HIO_VARIABLE) 899 sc->sc_flags |= UKBD_FLAG_APPLE_FN; 900 DPRINTFN(1, "Found Apple FN-key\n"); 901 apple_keys = 1; 902 sc->sc_kbd_id = temp_id; 903 } 904 if (apple_keys == 0) { 905 /* 906 * Assume the first HID ID contains the 907 * keyboard data 908 */ 909 hid_report_size(hid_ptr, hid_len, 910 hid_input, &sc->sc_kbd_id); 911 } 912 913 /* investigate if we need an ID-byte for the leds */ 914 hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id); 915 916 free(hid_ptr, M_TEMP); 917 } 918 919 /* ignore if SETIDLE fails, hence it is not crucial */ 920 err = usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0); 921 922 mtx_lock(&Giant); 923 924 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state); 925 926 KBD_INIT_DONE(kbd); 927 928 mtx_unlock(&Giant); 929 930 if (kbd_register(kbd) < 0) { 931 goto detach; 932 } 933 KBD_CONFIG_DONE(kbd); 934 935 ukbd_enable(kbd); 936 937 #ifdef KBD_INSTALL_CDEV 938 if (kbd_attach(kbd)) { 939 goto detach; 940 } 941 #endif 942 sc->sc_flags |= UKBD_FLAG_ATTACHED; 943 944 if (bootverbose) { 945 genkbd_diag(kbd, bootverbose); 946 } 947 /* lock keyboard mutex */ 948 949 mtx_lock(&Giant); 950 951 /* start the keyboard */ 952 953 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]); 954 955 mtx_unlock(&Giant); 956 return (0); /* success */ 957 958 detach: 959 ukbd_detach(dev); 960 return (ENXIO); /* error */ 961 } 962 963 static int 964 ukbd_detach(device_t dev) 965 { 966 struct ukbd_softc *sc = device_get_softc(dev); 967 int error; 968 969 DPRINTF("\n"); 970 971 mtx_lock(&Giant); 972 973 sc->sc_flags |= UKBD_FLAG_GONE; 974 975 usb_callout_stop(&sc->sc_callout); 976 977 ukbd_disable(&sc->sc_kbd); 978 979 #ifdef KBD_INSTALL_CDEV 980 if (sc->sc_flags & UKBD_FLAG_ATTACHED) { 981 error = kbd_detach(&sc->sc_kbd); 982 if (error) { 983 /* usb attach cannot return an error */ 984 device_printf(dev, "WARNING: kbd_detach() " 985 "returned non-zero! (ignored)\n"); 986 } 987 } 988 #endif 989 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { 990 error = kbd_unregister(&sc->sc_kbd); 991 if (error) { 992 /* usb attach cannot return an error */ 993 device_printf(dev, "WARNING: kbd_unregister() " 994 "returned non-zero! (ignored)\n"); 995 } 996 } 997 sc->sc_kbd.kb_flags = 0; 998 999 mtx_unlock(&Giant); 1000 1001 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER); 1002 1003 usb_callout_drain(&sc->sc_callout); 1004 1005 DPRINTF("%s: disconnected\n", 1006 device_get_nameunit(dev)); 1007 1008 return (0); 1009 } 1010 1011 static int 1012 ukbd_resume(device_t dev) 1013 { 1014 struct ukbd_softc *sc = device_get_softc(dev); 1015 1016 mtx_lock(&Giant); 1017 1018 ukbd_clear_state(&sc->sc_kbd); 1019 1020 mtx_unlock(&Giant); 1021 1022 return (0); 1023 } 1024 1025 /* early keyboard probe, not supported */ 1026 static int 1027 ukbd_configure(int flags) 1028 { 1029 return (0); 1030 } 1031 1032 /* detect a keyboard, not used */ 1033 static int 1034 ukbd__probe(int unit, void *arg, int flags) 1035 { 1036 return (ENXIO); 1037 } 1038 1039 /* reset and initialize the device, not used */ 1040 static int 1041 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 1042 { 1043 return (ENXIO); 1044 } 1045 1046 /* test the interface to the device, not used */ 1047 static int 1048 ukbd_test_if(keyboard_t *kbd) 1049 { 1050 return (0); 1051 } 1052 1053 /* finish using this keyboard, not used */ 1054 static int 1055 ukbd_term(keyboard_t *kbd) 1056 { 1057 return (ENXIO); 1058 } 1059 1060 /* keyboard interrupt routine, not used */ 1061 static int 1062 ukbd_intr(keyboard_t *kbd, void *arg) 1063 { 1064 return (0); 1065 } 1066 1067 /* lock the access to the keyboard, not used */ 1068 static int 1069 ukbd_lock(keyboard_t *kbd, int lock) 1070 { 1071 return (1); 1072 } 1073 1074 /* 1075 * Enable the access to the device; until this function is called, 1076 * the client cannot read from the keyboard. 1077 */ 1078 static int 1079 ukbd_enable(keyboard_t *kbd) 1080 { 1081 if (!mtx_owned(&Giant)) { 1082 /* XXX cludge */ 1083 int retval; 1084 mtx_lock(&Giant); 1085 retval = ukbd_enable(kbd); 1086 mtx_unlock(&Giant); 1087 return (retval); 1088 } 1089 KBD_ACTIVATE(kbd); 1090 return (0); 1091 } 1092 1093 /* disallow the access to the device */ 1094 static int 1095 ukbd_disable(keyboard_t *kbd) 1096 { 1097 if (!mtx_owned(&Giant)) { 1098 /* XXX cludge */ 1099 int retval; 1100 mtx_lock(&Giant); 1101 retval = ukbd_disable(kbd); 1102 mtx_unlock(&Giant); 1103 return (retval); 1104 } 1105 KBD_DEACTIVATE(kbd); 1106 return (0); 1107 } 1108 1109 /* check if data is waiting */ 1110 static int 1111 ukbd_check(keyboard_t *kbd) 1112 { 1113 struct ukbd_softc *sc = kbd->kb_data; 1114 1115 if (!KBD_IS_ACTIVE(kbd)) 1116 return (0); 1117 1118 if (sc->sc_flags & UKBD_FLAG_POLLING) { 1119 if (!mtx_owned(&Giant)) { 1120 /* XXX cludge */ 1121 int retval; 1122 mtx_lock(&Giant); 1123 retval = ukbd_check(kbd); 1124 mtx_unlock(&Giant); 1125 return (retval); 1126 } 1127 } else { 1128 /* XXX the keyboard layer requires Giant */ 1129 if (!mtx_owned(&Giant)) 1130 return (0); 1131 } 1132 1133 /* check if key belongs to this thread */ 1134 if (ukbd_polls_other_thread(sc)) 1135 return (0); 1136 1137 if (sc->sc_flags & UKBD_FLAG_POLLING) 1138 ukbd_do_poll(sc, 0); 1139 1140 #ifdef UKBD_EMULATE_ATSCANCODE 1141 if (sc->sc_buffered_char[0]) { 1142 return (1); 1143 } 1144 #endif 1145 if (sc->sc_inputs > 0) { 1146 return (1); 1147 } 1148 return (0); 1149 } 1150 1151 /* check if char is waiting */ 1152 static int 1153 ukbd_check_char(keyboard_t *kbd) 1154 { 1155 struct ukbd_softc *sc = kbd->kb_data; 1156 1157 if (!KBD_IS_ACTIVE(kbd)) 1158 return (0); 1159 1160 if (sc->sc_flags & UKBD_FLAG_POLLING) { 1161 if (!mtx_owned(&Giant)) { 1162 /* XXX cludge */ 1163 int retval; 1164 mtx_lock(&Giant); 1165 retval = ukbd_check_char(kbd); 1166 mtx_unlock(&Giant); 1167 return (retval); 1168 } 1169 } else { 1170 /* XXX the keyboard layer requires Giant */ 1171 if (!mtx_owned(&Giant)) 1172 return (0); 1173 } 1174 1175 /* check if key belongs to this thread */ 1176 if (ukbd_polls_other_thread(sc)) 1177 return (0); 1178 1179 if ((sc->sc_composed_char > 0) && 1180 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { 1181 return (1); 1182 } 1183 return (ukbd_check(kbd)); 1184 } 1185 1186 1187 /* read one byte from the keyboard if it's allowed */ 1188 static int 1189 ukbd_read(keyboard_t *kbd, int wait) 1190 { 1191 struct ukbd_softc *sc = kbd->kb_data; 1192 int32_t usbcode; 1193 1194 #ifdef UKBD_EMULATE_ATSCANCODE 1195 uint32_t keycode; 1196 uint32_t scancode; 1197 1198 #endif 1199 if (!KBD_IS_ACTIVE(kbd)) 1200 return (-1); 1201 1202 if (sc->sc_flags & UKBD_FLAG_POLLING) { 1203 if (!mtx_owned(&Giant)) { 1204 /* XXX cludge */ 1205 int retval; 1206 mtx_lock(&Giant); 1207 retval = ukbd_read(kbd, wait); 1208 mtx_unlock(&Giant); 1209 return (retval); 1210 } 1211 } else { 1212 /* XXX the keyboard layer requires Giant */ 1213 if (!mtx_owned(&Giant)) 1214 return (-1); 1215 } 1216 1217 /* check if key belongs to this thread */ 1218 if (ukbd_polls_other_thread(sc)) 1219 return (-1); 1220 1221 #ifdef UKBD_EMULATE_ATSCANCODE 1222 if (sc->sc_buffered_char[0]) { 1223 scancode = sc->sc_buffered_char[0]; 1224 if (scancode & SCAN_PREFIX) { 1225 sc->sc_buffered_char[0] &= ~SCAN_PREFIX; 1226 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1227 } 1228 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1229 sc->sc_buffered_char[1] = 0; 1230 return (scancode); 1231 } 1232 #endif /* UKBD_EMULATE_ATSCANCODE */ 1233 1234 /* XXX */ 1235 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1236 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) 1237 return (-1); 1238 1239 ++(kbd->kb_count); 1240 1241 #ifdef UKBD_EMULATE_ATSCANCODE 1242 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1243 if (keycode == NN) { 1244 return -1; 1245 } 1246 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers, 1247 (usbcode & KEY_RELEASE))); 1248 #else /* !UKBD_EMULATE_ATSCANCODE */ 1249 return (usbcode); 1250 #endif /* UKBD_EMULATE_ATSCANCODE */ 1251 } 1252 1253 /* read char from the keyboard */ 1254 static uint32_t 1255 ukbd_read_char(keyboard_t *kbd, int wait) 1256 { 1257 struct ukbd_softc *sc = kbd->kb_data; 1258 uint32_t action; 1259 uint32_t keycode; 1260 int32_t usbcode; 1261 1262 #ifdef UKBD_EMULATE_ATSCANCODE 1263 uint32_t scancode; 1264 1265 #endif 1266 1267 if (!KBD_IS_ACTIVE(kbd)) 1268 return (NOKEY); 1269 1270 if (sc->sc_flags & UKBD_FLAG_POLLING) { 1271 if (!mtx_owned(&Giant)) { 1272 /* XXX cludge */ 1273 int retval; 1274 mtx_lock(&Giant); 1275 retval = ukbd_read_char(kbd, wait); 1276 mtx_unlock(&Giant); 1277 return (retval); 1278 } 1279 } else { 1280 /* XXX the keyboard layer requires Giant */ 1281 if (!mtx_owned(&Giant)) 1282 return (NOKEY); 1283 } 1284 1285 /* check if key belongs to this thread */ 1286 if (ukbd_polls_other_thread(sc)) 1287 return (NOKEY); 1288 1289 next_code: 1290 1291 /* do we have a composed char to return ? */ 1292 1293 if ((sc->sc_composed_char > 0) && 1294 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { 1295 1296 action = sc->sc_composed_char; 1297 sc->sc_composed_char = 0; 1298 1299 if (action > 0xFF) { 1300 goto errkey; 1301 } 1302 goto done; 1303 } 1304 #ifdef UKBD_EMULATE_ATSCANCODE 1305 1306 /* do we have a pending raw scan code? */ 1307 1308 if (sc->sc_mode == K_RAW) { 1309 scancode = sc->sc_buffered_char[0]; 1310 if (scancode) { 1311 if (scancode & SCAN_PREFIX) { 1312 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX); 1313 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1314 } 1315 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1316 sc->sc_buffered_char[1] = 0; 1317 return (scancode); 1318 } 1319 } 1320 #endif /* UKBD_EMULATE_ATSCANCODE */ 1321 1322 /* see if there is something in the keyboard port */ 1323 /* XXX */ 1324 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1325 if (usbcode == -1) { 1326 return (NOKEY); 1327 } 1328 ++kbd->kb_count; 1329 1330 #ifdef UKBD_EMULATE_ATSCANCODE 1331 /* USB key index -> key code -> AT scan code */ 1332 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1333 if (keycode == NN) { 1334 return (NOKEY); 1335 } 1336 /* return an AT scan code for the K_RAW mode */ 1337 if (sc->sc_mode == K_RAW) { 1338 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers, 1339 (usbcode & KEY_RELEASE))); 1340 } 1341 #else /* !UKBD_EMULATE_ATSCANCODE */ 1342 1343 /* return the byte as is for the K_RAW mode */ 1344 if (sc->sc_mode == K_RAW) { 1345 return (usbcode); 1346 } 1347 /* USB key index -> key code */ 1348 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1349 if (keycode == NN) { 1350 return (NOKEY); 1351 } 1352 #endif /* UKBD_EMULATE_ATSCANCODE */ 1353 1354 switch (keycode) { 1355 case 0x38: /* left alt (compose key) */ 1356 if (usbcode & KEY_RELEASE) { 1357 if (sc->sc_flags & UKBD_FLAG_COMPOSE) { 1358 sc->sc_flags &= ~UKBD_FLAG_COMPOSE; 1359 1360 if (sc->sc_composed_char > 0xFF) { 1361 sc->sc_composed_char = 0; 1362 } 1363 } 1364 } else { 1365 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) { 1366 sc->sc_flags |= UKBD_FLAG_COMPOSE; 1367 sc->sc_composed_char = 0; 1368 } 1369 } 1370 break; 1371 /* XXX: I don't like these... */ 1372 case 0x5c: /* print screen */ 1373 if (sc->sc_flags & ALTS) { 1374 keycode = 0x54; /* sysrq */ 1375 } 1376 break; 1377 case 0x68: /* pause/break */ 1378 if (sc->sc_flags & CTLS) { 1379 keycode = 0x6c; /* break */ 1380 } 1381 break; 1382 } 1383 1384 /* return the key code in the K_CODE mode */ 1385 if (usbcode & KEY_RELEASE) { 1386 keycode |= SCAN_RELEASE; 1387 } 1388 if (sc->sc_mode == K_CODE) { 1389 return (keycode); 1390 } 1391 /* compose a character code */ 1392 if (sc->sc_flags & UKBD_FLAG_COMPOSE) { 1393 switch (keycode) { 1394 /* key pressed, process it */ 1395 case 0x47: 1396 case 0x48: 1397 case 0x49: /* keypad 7,8,9 */ 1398 sc->sc_composed_char *= 10; 1399 sc->sc_composed_char += keycode - 0x40; 1400 goto check_composed; 1401 1402 case 0x4B: 1403 case 0x4C: 1404 case 0x4D: /* keypad 4,5,6 */ 1405 sc->sc_composed_char *= 10; 1406 sc->sc_composed_char += keycode - 0x47; 1407 goto check_composed; 1408 1409 case 0x4F: 1410 case 0x50: 1411 case 0x51: /* keypad 1,2,3 */ 1412 sc->sc_composed_char *= 10; 1413 sc->sc_composed_char += keycode - 0x4E; 1414 goto check_composed; 1415 1416 case 0x52: /* keypad 0 */ 1417 sc->sc_composed_char *= 10; 1418 goto check_composed; 1419 1420 /* key released, no interest here */ 1421 case SCAN_RELEASE | 0x47: 1422 case SCAN_RELEASE | 0x48: 1423 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */ 1424 case SCAN_RELEASE | 0x4B: 1425 case SCAN_RELEASE | 0x4C: 1426 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */ 1427 case SCAN_RELEASE | 0x4F: 1428 case SCAN_RELEASE | 0x50: 1429 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */ 1430 case SCAN_RELEASE | 0x52: /* keypad 0 */ 1431 goto next_code; 1432 1433 case 0x38: /* left alt key */ 1434 break; 1435 1436 default: 1437 if (sc->sc_composed_char > 0) { 1438 sc->sc_flags &= ~UKBD_FLAG_COMPOSE; 1439 sc->sc_composed_char = 0; 1440 goto errkey; 1441 } 1442 break; 1443 } 1444 } 1445 /* keycode to key action */ 1446 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), 1447 (keycode & SCAN_RELEASE), 1448 &sc->sc_state, &sc->sc_accents); 1449 if (action == NOKEY) { 1450 goto next_code; 1451 } 1452 done: 1453 return (action); 1454 1455 check_composed: 1456 if (sc->sc_composed_char <= 0xFF) { 1457 goto next_code; 1458 } 1459 errkey: 1460 return (ERRKEY); 1461 } 1462 1463 /* some useful control functions */ 1464 static int 1465 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 1466 { 1467 /* translate LED_XXX bits into the device specific bits */ 1468 static const uint8_t ledmap[8] = { 1469 0, 2, 1, 3, 4, 6, 5, 7, 1470 }; 1471 struct ukbd_softc *sc = kbd->kb_data; 1472 int i; 1473 1474 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1475 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1476 int ival; 1477 1478 #endif 1479 if (!mtx_owned(&Giant)) { 1480 /* 1481 * XXX big problem: If scroll lock is pressed and "printf()" 1482 * is called, the CPU will get here, to un-scroll lock the 1483 * keyboard. But if "printf()" acquires the "Giant" lock, 1484 * there will be a locking order reversal problem, so the 1485 * keyboard system must get out of "Giant" first, before the 1486 * CPU can proceed here ... 1487 */ 1488 switch (cmd) { 1489 case KDGKBMODE: 1490 case KDSKBMODE: 1491 /* workaround for Geli */ 1492 mtx_lock(&Giant); 1493 i = ukbd_ioctl(kbd, cmd, arg); 1494 mtx_unlock(&Giant); 1495 return (i); 1496 default: 1497 return (EINVAL); 1498 } 1499 } 1500 1501 switch (cmd) { 1502 case KDGKBMODE: /* get keyboard mode */ 1503 *(int *)arg = sc->sc_mode; 1504 break; 1505 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1506 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1507 case _IO('K', 7): 1508 ival = IOCPARM_IVAL(arg); 1509 arg = (caddr_t)&ival; 1510 /* FALLTHROUGH */ 1511 #endif 1512 case KDSKBMODE: /* set keyboard mode */ 1513 switch (*(int *)arg) { 1514 case K_XLATE: 1515 if (sc->sc_mode != K_XLATE) { 1516 /* make lock key state and LED state match */ 1517 sc->sc_state &= ~LOCK_MASK; 1518 sc->sc_state |= KBD_LED_VAL(kbd); 1519 } 1520 /* FALLTHROUGH */ 1521 case K_RAW: 1522 case K_CODE: 1523 if (sc->sc_mode != *(int *)arg) { 1524 if (ukbd_is_polling(sc) == 0) 1525 ukbd_clear_state(kbd); 1526 sc->sc_mode = *(int *)arg; 1527 } 1528 break; 1529 default: 1530 return (EINVAL); 1531 } 1532 break; 1533 1534 case KDGETLED: /* get keyboard LED */ 1535 *(int *)arg = KBD_LED_VAL(kbd); 1536 break; 1537 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1538 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1539 case _IO('K', 66): 1540 ival = IOCPARM_IVAL(arg); 1541 arg = (caddr_t)&ival; 1542 /* FALLTHROUGH */ 1543 #endif 1544 case KDSETLED: /* set keyboard LED */ 1545 /* NOTE: lock key state in "sc_state" won't be changed */ 1546 if (*(int *)arg & ~LOCK_MASK) { 1547 return (EINVAL); 1548 } 1549 i = *(int *)arg; 1550 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 1551 if (sc->sc_mode == K_XLATE && 1552 kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 1553 if (i & ALKED) 1554 i |= CLKED; 1555 else 1556 i &= ~CLKED; 1557 } 1558 if (KBD_HAS_DEVICE(kbd)) { 1559 ukbd_set_leds(sc, ledmap[i & LED_MASK]); 1560 } 1561 KBD_LED_VAL(kbd) = *(int *)arg; 1562 break; 1563 case KDGKBSTATE: /* get lock key state */ 1564 *(int *)arg = sc->sc_state & LOCK_MASK; 1565 break; 1566 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1567 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1568 case _IO('K', 20): 1569 ival = IOCPARM_IVAL(arg); 1570 arg = (caddr_t)&ival; 1571 /* FALLTHROUGH */ 1572 #endif 1573 case KDSKBSTATE: /* set lock key state */ 1574 if (*(int *)arg & ~LOCK_MASK) { 1575 return (EINVAL); 1576 } 1577 sc->sc_state &= ~LOCK_MASK; 1578 sc->sc_state |= *(int *)arg; 1579 1580 /* set LEDs and quit */ 1581 return (ukbd_ioctl(kbd, KDSETLED, arg)); 1582 1583 case KDSETREPEAT: /* set keyboard repeat rate (new 1584 * interface) */ 1585 if (!KBD_HAS_DEVICE(kbd)) { 1586 return (0); 1587 } 1588 if (((int *)arg)[1] < 0) { 1589 return (EINVAL); 1590 } 1591 if (((int *)arg)[0] < 0) { 1592 return (EINVAL); 1593 } 1594 if (((int *)arg)[0] < 200) /* fastest possible value */ 1595 kbd->kb_delay1 = 200; 1596 else 1597 kbd->kb_delay1 = ((int *)arg)[0]; 1598 kbd->kb_delay2 = ((int *)arg)[1]; 1599 return (0); 1600 1601 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1602 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1603 case _IO('K', 67): 1604 ival = IOCPARM_IVAL(arg); 1605 arg = (caddr_t)&ival; 1606 /* FALLTHROUGH */ 1607 #endif 1608 case KDSETRAD: /* set keyboard repeat rate (old 1609 * interface) */ 1610 return (ukbd_set_typematic(kbd, *(int *)arg)); 1611 1612 case PIO_KEYMAP: /* set keyboard translation table */ 1613 case PIO_KEYMAPENT: /* set keyboard translation table 1614 * entry */ 1615 case PIO_DEADKEYMAP: /* set accent key translation table */ 1616 sc->sc_accents = 0; 1617 /* FALLTHROUGH */ 1618 default: 1619 return (genkbd_commonioctl(kbd, cmd, arg)); 1620 } 1621 1622 return (0); 1623 } 1624 1625 /* clear the internal state of the keyboard */ 1626 static void 1627 ukbd_clear_state(keyboard_t *kbd) 1628 { 1629 struct ukbd_softc *sc = kbd->kb_data; 1630 1631 if (!mtx_owned(&Giant)) { 1632 /* XXX cludge */ 1633 mtx_lock(&Giant); 1634 ukbd_clear_state(kbd); 1635 mtx_unlock(&Giant); 1636 return; 1637 } 1638 1639 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING); 1640 sc->sc_state &= LOCK_MASK; /* preserve locking key state */ 1641 sc->sc_accents = 0; 1642 sc->sc_composed_char = 0; 1643 #ifdef UKBD_EMULATE_ATSCANCODE 1644 sc->sc_buffered_char[0] = 0; 1645 sc->sc_buffered_char[1] = 0; 1646 #endif 1647 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); 1648 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata)); 1649 memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime)); 1650 memset(&sc->sc_otime, 0, sizeof(sc->sc_otime)); 1651 } 1652 1653 /* save the internal state, not used */ 1654 static int 1655 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len) 1656 { 1657 return (len == 0) ? 1 : -1; 1658 } 1659 1660 /* set the internal state, not used */ 1661 static int 1662 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len) 1663 { 1664 return (EINVAL); 1665 } 1666 1667 static int 1668 ukbd_is_polling(struct ukbd_softc *sc) 1669 { 1670 int delta; 1671 1672 if (sc->sc_flags & UKBD_FLAG_POLLING) 1673 return (1); /* polling */ 1674 1675 delta = ticks - sc->sc_poll_tick_last; 1676 if ((delta < 0) || (delta >= hz)) { 1677 sc->sc_poll_detected = 0; 1678 return (0); /* not polling */ 1679 } 1680 1681 return (sc->sc_poll_detected); 1682 } 1683 1684 static int 1685 ukbd_polls_other_thread(struct ukbd_softc *sc) 1686 { 1687 return (ukbd_is_polling(sc) && 1688 (sc->sc_poll_thread != curthread)); 1689 } 1690 1691 static int 1692 ukbd_poll(keyboard_t *kbd, int on) 1693 { 1694 struct ukbd_softc *sc = kbd->kb_data; 1695 1696 if (!mtx_owned(&Giant)) { 1697 /* XXX cludge */ 1698 int retval; 1699 mtx_lock(&Giant); 1700 retval = ukbd_poll(kbd, on); 1701 mtx_unlock(&Giant); 1702 return (retval); 1703 } 1704 1705 if (on) { 1706 sc->sc_flags |= UKBD_FLAG_POLLING; 1707 sc->sc_poll_thread = curthread; 1708 } else { 1709 sc->sc_flags &= ~UKBD_FLAG_POLLING; 1710 ukbd_start_timer(sc); /* start timer */ 1711 } 1712 return (0); 1713 } 1714 1715 /* local functions */ 1716 1717 static void 1718 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds) 1719 { 1720 DPRINTF("leds=0x%02x\n", leds); 1721 1722 sc->sc_leds = leds; 1723 sc->sc_flags |= UKBD_FLAG_SET_LEDS; 1724 1725 /* start transfer, if not already started */ 1726 1727 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]); 1728 } 1729 1730 static int 1731 ukbd_set_typematic(keyboard_t *kbd, int code) 1732 { 1733 static const int delays[] = {250, 500, 750, 1000}; 1734 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63, 1735 68, 76, 84, 92, 100, 110, 118, 126, 1736 136, 152, 168, 184, 200, 220, 236, 252, 1737 272, 304, 336, 368, 400, 440, 472, 504}; 1738 1739 if (code & ~0x7f) { 1740 return (EINVAL); 1741 } 1742 kbd->kb_delay1 = delays[(code >> 5) & 3]; 1743 kbd->kb_delay2 = rates[code & 0x1f]; 1744 return (0); 1745 } 1746 1747 #ifdef UKBD_EMULATE_ATSCANCODE 1748 static int 1749 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up) 1750 { 1751 static const int scan[] = { 1752 /* 89 */ 1753 0x11c, /* Enter */ 1754 /* 90-99 */ 1755 0x11d, /* Ctrl-R */ 1756 0x135, /* Divide */ 1757 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */ 1758 0x138, /* Alt-R */ 1759 0x147, /* Home */ 1760 0x148, /* Up */ 1761 0x149, /* PageUp */ 1762 0x14b, /* Left */ 1763 0x14d, /* Right */ 1764 0x14f, /* End */ 1765 /* 100-109 */ 1766 0x150, /* Down */ 1767 0x151, /* PageDown */ 1768 0x152, /* Insert */ 1769 0x153, /* Delete */ 1770 0x146, /* XXX Pause/Break */ 1771 0x15b, /* Win_L(Super_L) */ 1772 0x15c, /* Win_R(Super_R) */ 1773 0x15d, /* Application(Menu) */ 1774 1775 /* SUN TYPE 6 USB KEYBOARD */ 1776 0x168, /* Sun Type 6 Help */ 1777 0x15e, /* Sun Type 6 Stop */ 1778 /* 110 - 119 */ 1779 0x15f, /* Sun Type 6 Again */ 1780 0x160, /* Sun Type 6 Props */ 1781 0x161, /* Sun Type 6 Undo */ 1782 0x162, /* Sun Type 6 Front */ 1783 0x163, /* Sun Type 6 Copy */ 1784 0x164, /* Sun Type 6 Open */ 1785 0x165, /* Sun Type 6 Paste */ 1786 0x166, /* Sun Type 6 Find */ 1787 0x167, /* Sun Type 6 Cut */ 1788 0x125, /* Sun Type 6 Mute */ 1789 /* 120 - 128 */ 1790 0x11f, /* Sun Type 6 VolumeDown */ 1791 0x11e, /* Sun Type 6 VolumeUp */ 1792 0x120, /* Sun Type 6 PowerDown */ 1793 1794 /* Japanese 106/109 keyboard */ 1795 0x73, /* Keyboard Intl' 1 (backslash / underscore) */ 1796 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */ 1797 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */ 1798 0x79, /* Keyboard Intl' 4 (Henkan) */ 1799 0x7b, /* Keyboard Intl' 5 (Muhenkan) */ 1800 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */ 1801 }; 1802 1803 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) { 1804 code = scan[code - 89]; 1805 } 1806 /* Pause/Break */ 1807 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) { 1808 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL); 1809 } 1810 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) { 1811 code &= ~SCAN_PREFIX_SHIFT; 1812 } 1813 code |= (up ? SCAN_RELEASE : SCAN_PRESS); 1814 1815 if (code & SCAN_PREFIX) { 1816 if (code & SCAN_PREFIX_CTL) { 1817 /* Ctrl */ 1818 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE)); 1819 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX); 1820 } else if (code & SCAN_PREFIX_SHIFT) { 1821 /* Shift */ 1822 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE)); 1823 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT); 1824 } else { 1825 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX); 1826 sc->sc_buffered_char[1] = 0; 1827 } 1828 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1829 } 1830 return (code); 1831 1832 } 1833 1834 #endif /* UKBD_EMULATE_ATSCANCODE */ 1835 1836 static keyboard_switch_t ukbdsw = { 1837 .probe = &ukbd__probe, 1838 .init = &ukbd_init, 1839 .term = &ukbd_term, 1840 .intr = &ukbd_intr, 1841 .test_if = &ukbd_test_if, 1842 .enable = &ukbd_enable, 1843 .disable = &ukbd_disable, 1844 .read = &ukbd_read, 1845 .check = &ukbd_check, 1846 .read_char = &ukbd_read_char, 1847 .check_char = &ukbd_check_char, 1848 .ioctl = &ukbd_ioctl, 1849 .lock = &ukbd_lock, 1850 .clear_state = &ukbd_clear_state, 1851 .get_state = &ukbd_get_state, 1852 .set_state = &ukbd_set_state, 1853 .get_fkeystr = &genkbd_get_fkeystr, 1854 .poll = &ukbd_poll, 1855 .diag = &genkbd_diag, 1856 }; 1857 1858 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure); 1859 1860 static int 1861 ukbd_driver_load(module_t mod, int what, void *arg) 1862 { 1863 switch (what) { 1864 case MOD_LOAD: 1865 kbd_add_driver(&ukbd_kbd_driver); 1866 break; 1867 case MOD_UNLOAD: 1868 kbd_delete_driver(&ukbd_kbd_driver); 1869 break; 1870 } 1871 return (0); 1872 } 1873 1874 static devclass_t ukbd_devclass; 1875 1876 static device_method_t ukbd_methods[] = { 1877 DEVMETHOD(device_probe, ukbd_probe), 1878 DEVMETHOD(device_attach, ukbd_attach), 1879 DEVMETHOD(device_detach, ukbd_detach), 1880 DEVMETHOD(device_resume, ukbd_resume), 1881 {0, 0} 1882 }; 1883 1884 static driver_t ukbd_driver = { 1885 .name = "ukbd", 1886 .methods = ukbd_methods, 1887 .size = sizeof(struct ukbd_softc), 1888 }; 1889 1890 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0); 1891 MODULE_DEPEND(ukbd, usb, 1, 1, 1); 1892