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