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