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