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