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