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