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 offset = sc->sc_loc_key[0].count; 706 if (offset < 0 || offset > len) 707 offset = len; 708 while (offset--) { 709 uint32_t key = 710 hid_get_data(sc->sc_buffer + offset, len - offset, 711 &sc->sc_loc_key[i]); 712 if (modifiers & MOD_FN) 713 key = ukbd_apple_fn(key); 714 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) 715 key = ukbd_apple_swap(key); 716 if (key == KEY_NONE || key == KEY_ERROR || key >= UKBD_NKEYCODE) 717 continue; 718 /* set key in bitmap */ 719 sc->sc_ndata.bitmap[key / 64] |= 1ULL << (key % 64); 720 } 721 } else if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_key[i])) { 722 uint32_t key = i; 723 724 if (modifiers & MOD_FN) 725 key = ukbd_apple_fn(key); 726 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) 727 key = ukbd_apple_swap(key); 728 if (key == KEY_NONE || key == KEY_ERROR || key >= UKBD_NKEYCODE) 729 continue; 730 /* set key in bitmap */ 731 sc->sc_ndata.bitmap[key / 64] |= 1ULL << (key % 64); 732 } 733 } 734 #ifdef USB_DEBUG 735 DPRINTF("modifiers = 0x%04x\n", modifiers); 736 for (i = 0; i != UKBD_NKEYCODE; i++) { 737 const uint64_t valid = sc->sc_ndata.bitmap[i / 64]; 738 const uint64_t mask = 1ULL << (i % 64); 739 740 if (valid & mask) 741 DPRINTF("Key 0x%02x pressed\n", i); 742 } 743 #endif 744 ukbd_interrupt(sc); 745 746 case USB_ST_SETUP: 747 tr_setup: 748 if (sc->sc_inputs < UKBD_IN_BUF_FULL) { 749 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 750 usbd_transfer_submit(xfer); 751 } else { 752 DPRINTF("input queue is full!\n"); 753 } 754 break; 755 756 default: /* Error */ 757 DPRINTF("error=%s\n", usbd_errstr(error)); 758 759 if (error != USB_ERR_CANCELLED) { 760 /* try to clear stall first */ 761 usbd_xfer_set_stall(xfer); 762 goto tr_setup; 763 } 764 break; 765 } 766 } 767 768 static void 769 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error) 770 { 771 struct ukbd_softc *sc = usbd_xfer_softc(xfer); 772 struct usb_device_request req; 773 struct usb_page_cache *pc; 774 uint8_t id; 775 uint8_t any; 776 int len; 777 778 UKBD_LOCK_ASSERT(); 779 780 #ifdef USB_DEBUG 781 if (ukbd_no_leds) 782 return; 783 #endif 784 785 switch (USB_GET_STATE(xfer)) { 786 case USB_ST_TRANSFERRED: 787 case USB_ST_SETUP: 788 if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS)) 789 break; 790 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS; 791 792 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 793 req.bRequest = UR_SET_REPORT; 794 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0); 795 req.wIndex[0] = sc->sc_iface_no; 796 req.wIndex[1] = 0; 797 req.wLength[1] = 0; 798 799 memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE); 800 801 id = 0; 802 any = 0; 803 804 /* Assumption: All led bits must be in the same ID. */ 805 806 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) { 807 if (sc->sc_leds & NLKED) { 808 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1, 809 &sc->sc_loc_numlock, 1); 810 } 811 id = sc->sc_id_numlock; 812 any = 1; 813 } 814 815 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) { 816 if (sc->sc_leds & SLKED) { 817 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1, 818 &sc->sc_loc_scrolllock, 1); 819 } 820 id = sc->sc_id_scrolllock; 821 any = 1; 822 } 823 824 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) { 825 if (sc->sc_leds & CLKED) { 826 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1, 827 &sc->sc_loc_capslock, 1); 828 } 829 id = sc->sc_id_capslock; 830 any = 1; 831 } 832 833 /* if no leds, nothing to do */ 834 if (!any) 835 break; 836 837 #ifdef EVDEV_SUPPORT 838 if (sc->sc_evdev != NULL) 839 evdev_push_leds(sc->sc_evdev, sc->sc_leds); 840 #endif 841 842 /* range check output report length */ 843 len = sc->sc_led_size; 844 if (len > (UKBD_BUFFER_SIZE - 1)) 845 len = (UKBD_BUFFER_SIZE - 1); 846 847 /* check if we need to prefix an ID byte */ 848 sc->sc_buffer[0] = id; 849 850 pc = usbd_xfer_get_frame(xfer, 1); 851 if (id != 0) { 852 len++; 853 usbd_copy_in(pc, 0, sc->sc_buffer, len); 854 } else { 855 usbd_copy_in(pc, 0, sc->sc_buffer + 1, len); 856 } 857 req.wLength[0] = len; 858 usbd_xfer_set_frame_len(xfer, 1, len); 859 860 DPRINTF("len=%d, id=%d\n", len, id); 861 862 /* setup control request last */ 863 pc = usbd_xfer_get_frame(xfer, 0); 864 usbd_copy_in(pc, 0, &req, sizeof(req)); 865 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 866 867 /* start data transfer */ 868 usbd_xfer_set_frames(xfer, 2); 869 usbd_transfer_submit(xfer); 870 break; 871 872 default: /* Error */ 873 DPRINTFN(1, "error=%s\n", usbd_errstr(error)); 874 break; 875 } 876 } 877 878 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = { 879 880 [UKBD_INTR_DT_0] = { 881 .type = UE_INTERRUPT, 882 .endpoint = UE_ADDR_ANY, 883 .direction = UE_DIR_IN, 884 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 885 .bufsize = 0, /* use wMaxPacketSize */ 886 .callback = &ukbd_intr_callback, 887 }, 888 889 [UKBD_INTR_DT_1] = { 890 .type = UE_INTERRUPT, 891 .endpoint = UE_ADDR_ANY, 892 .direction = UE_DIR_IN, 893 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 894 .bufsize = 0, /* use wMaxPacketSize */ 895 .callback = &ukbd_intr_callback, 896 }, 897 898 [UKBD_CTRL_LED] = { 899 .type = UE_CONTROL, 900 .endpoint = 0x00, /* Control pipe */ 901 .direction = UE_DIR_ANY, 902 .bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE, 903 .callback = &ukbd_set_leds_callback, 904 .timeout = 1000, /* 1 second */ 905 }, 906 }; 907 908 /* A match on these entries will load ukbd */ 909 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = { 910 {USB_IFACE_CLASS(UICLASS_HID), 911 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT), 912 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),}, 913 }; 914 915 static int 916 ukbd_probe(device_t dev) 917 { 918 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME); 919 struct usb_attach_arg *uaa = device_get_ivars(dev); 920 void *d_ptr; 921 int error; 922 uint16_t d_len; 923 924 UKBD_LOCK_ASSERT(); 925 DPRINTFN(11, "\n"); 926 927 if (sw == NULL) { 928 return (ENXIO); 929 } 930 if (uaa->usb_mode != USB_MODE_HOST) { 931 return (ENXIO); 932 } 933 934 if (uaa->info.bInterfaceClass != UICLASS_HID) 935 return (ENXIO); 936 937 if (usb_test_quirk(uaa, UQ_KBD_IGNORE)) 938 return (ENXIO); 939 940 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 941 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) 942 return (BUS_PROBE_DEFAULT); 943 944 error = usbd_req_get_hid_desc(uaa->device, NULL, 945 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); 946 947 if (error) 948 return (ENXIO); 949 950 if (hid_is_keyboard(d_ptr, d_len)) { 951 if (hid_is_mouse(d_ptr, d_len)) { 952 /* 953 * NOTE: We currently don't support USB mouse 954 * and USB keyboard on the same USB endpoint. 955 * Let "ums" driver win. 956 */ 957 error = ENXIO; 958 } else { 959 error = BUS_PROBE_DEFAULT; 960 } 961 } else { 962 error = ENXIO; 963 } 964 free(d_ptr, M_TEMP); 965 return (error); 966 } 967 968 static void 969 ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len) 970 { 971 uint32_t flags; 972 uint32_t key; 973 974 /* reset detected bits */ 975 sc->sc_flags &= ~UKBD_FLAG_HID_MASK; 976 977 /* reset detected keys */ 978 memset(sc->sc_loc_key_valid, 0, sizeof(sc->sc_loc_key_valid)); 979 980 /* check if there is an ID byte */ 981 sc->sc_kbd_size = hid_report_size(ptr, len, 982 hid_input, &sc->sc_kbd_id); 983 984 /* investigate if this is an Apple Keyboard */ 985 if (hid_locate(ptr, len, 986 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT), 987 hid_input, 0, &sc->sc_loc_apple_eject, &flags, 988 &sc->sc_id_apple_eject)) { 989 if (flags & HIO_VARIABLE) 990 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT | 991 UKBD_FLAG_APPLE_SWAP; 992 DPRINTFN(1, "Found Apple eject-key\n"); 993 } 994 if (hid_locate(ptr, len, 995 HID_USAGE2(0xFFFF, 0x0003), 996 hid_input, 0, &sc->sc_loc_apple_fn, &flags, 997 &sc->sc_id_apple_fn)) { 998 if (flags & HIO_VARIABLE) 999 sc->sc_flags |= UKBD_FLAG_APPLE_FN; 1000 DPRINTFN(1, "Found Apple FN-key\n"); 1001 } 1002 1003 /* figure out event buffer */ 1004 if (hid_locate(ptr, len, 1005 HID_USAGE2(HUP_KEYBOARD, 0x00), 1006 hid_input, 0, &sc->sc_loc_key[0], &flags, 1007 &sc->sc_id_loc_key[0])) { 1008 if (flags & HIO_VARIABLE) { 1009 DPRINTFN(1, "Ignoring keyboard event control\n"); 1010 } else { 1011 sc->sc_loc_key_valid[0] |= 1; 1012 DPRINTFN(1, "Found keyboard event array\n"); 1013 } 1014 } 1015 1016 /* figure out the keys */ 1017 for (key = 1; key != UKBD_NKEYCODE; key++) { 1018 if (hid_locate(ptr, len, 1019 HID_USAGE2(HUP_KEYBOARD, key), 1020 hid_input, 0, &sc->sc_loc_key[key], &flags, 1021 &sc->sc_id_loc_key[key])) { 1022 if (flags & HIO_VARIABLE) { 1023 sc->sc_loc_key_valid[key / 64] |= 1024 1ULL << (key % 64); 1025 DPRINTFN(1, "Found key 0x%02x\n", key); 1026 } 1027 } 1028 } 1029 1030 /* figure out leds on keyboard */ 1031 sc->sc_led_size = hid_report_size(ptr, len, 1032 hid_output, NULL); 1033 1034 if (hid_locate(ptr, len, 1035 HID_USAGE2(HUP_LEDS, 0x01), 1036 hid_output, 0, &sc->sc_loc_numlock, &flags, 1037 &sc->sc_id_numlock)) { 1038 if (flags & HIO_VARIABLE) 1039 sc->sc_flags |= UKBD_FLAG_NUMLOCK; 1040 DPRINTFN(1, "Found keyboard numlock\n"); 1041 } 1042 if (hid_locate(ptr, len, 1043 HID_USAGE2(HUP_LEDS, 0x02), 1044 hid_output, 0, &sc->sc_loc_capslock, &flags, 1045 &sc->sc_id_capslock)) { 1046 if (flags & HIO_VARIABLE) 1047 sc->sc_flags |= UKBD_FLAG_CAPSLOCK; 1048 DPRINTFN(1, "Found keyboard capslock\n"); 1049 } 1050 if (hid_locate(ptr, len, 1051 HID_USAGE2(HUP_LEDS, 0x03), 1052 hid_output, 0, &sc->sc_loc_scrolllock, &flags, 1053 &sc->sc_id_scrolllock)) { 1054 if (flags & HIO_VARIABLE) 1055 sc->sc_flags |= UKBD_FLAG_SCROLLLOCK; 1056 DPRINTFN(1, "Found keyboard scrolllock\n"); 1057 } 1058 } 1059 1060 static int 1061 ukbd_attach(device_t dev) 1062 { 1063 struct ukbd_softc *sc = device_get_softc(dev); 1064 struct usb_attach_arg *uaa = device_get_ivars(dev); 1065 int unit = device_get_unit(dev); 1066 keyboard_t *kbd = &sc->sc_kbd; 1067 void *hid_ptr = NULL; 1068 usb_error_t err; 1069 uint16_t n; 1070 uint16_t hid_len; 1071 #ifdef EVDEV_SUPPORT 1072 struct evdev_dev *evdev; 1073 int i; 1074 #endif 1075 #ifdef USB_DEBUG 1076 int rate; 1077 #endif 1078 UKBD_LOCK_ASSERT(); 1079 1080 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); 1081 1082 kbd->kb_data = (void *)sc; 1083 1084 device_set_usb_desc(dev); 1085 1086 sc->sc_udev = uaa->device; 1087 sc->sc_iface = uaa->iface; 1088 sc->sc_iface_index = uaa->info.bIfaceIndex; 1089 sc->sc_iface_no = uaa->info.bIfaceNum; 1090 sc->sc_mode = K_XLATE; 1091 1092 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0); 1093 1094 #ifdef UKBD_NO_POLLING 1095 err = usbd_transfer_setup(uaa->device, 1096 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config, 1097 UKBD_N_TRANSFER, sc, &Giant); 1098 #else 1099 /* 1100 * Setup the UKBD USB transfers one by one, so they are memory 1101 * independent which allows for handling panics triggered by 1102 * the keyboard driver itself, typically via CTRL+ALT+ESC 1103 * sequences. Or if the USB keyboard driver was processing a 1104 * key at the moment of panic. 1105 */ 1106 for (n = 0; n != UKBD_N_TRANSFER; n++) { 1107 err = usbd_transfer_setup(uaa->device, 1108 &uaa->info.bIfaceIndex, sc->sc_xfer + n, ukbd_config + n, 1109 1, sc, &Giant); 1110 if (err) 1111 break; 1112 } 1113 #endif 1114 1115 if (err) { 1116 DPRINTF("error=%s\n", usbd_errstr(err)); 1117 goto detach; 1118 } 1119 /* setup default keyboard maps */ 1120 1121 sc->sc_keymap = key_map; 1122 sc->sc_accmap = accent_map; 1123 for (n = 0; n < UKBD_NFKEY; n++) { 1124 sc->sc_fkeymap[n] = fkey_tab[n]; 1125 } 1126 1127 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, 1128 sc->sc_fkeymap, UKBD_NFKEY); 1129 1130 KBD_FOUND_DEVICE(kbd); 1131 1132 ukbd_clear_state(kbd); 1133 1134 /* 1135 * FIXME: set the initial value for lock keys in "sc_state" 1136 * according to the BIOS data? 1137 */ 1138 KBD_PROBE_DONE(kbd); 1139 1140 /* get HID descriptor */ 1141 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr, 1142 &hid_len, M_TEMP, uaa->info.bIfaceIndex); 1143 1144 if (err == 0) { 1145 DPRINTF("Parsing HID descriptor of %d bytes\n", 1146 (int)hid_len); 1147 1148 ukbd_parse_hid(sc, hid_ptr, hid_len); 1149 1150 free(hid_ptr, M_TEMP); 1151 } 1152 1153 /* check if we should use the boot protocol */ 1154 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) || 1155 (err != 0) || ukbd_any_key_valid(sc) == false) { 1156 1157 DPRINTF("Forcing boot protocol\n"); 1158 1159 err = usbd_req_set_protocol(sc->sc_udev, NULL, 1160 sc->sc_iface_index, 0); 1161 1162 if (err != 0) { 1163 DPRINTF("Set protocol error=%s (ignored)\n", 1164 usbd_errstr(err)); 1165 } 1166 1167 ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc)); 1168 } 1169 1170 /* ignore if SETIDLE fails, hence it is not crucial */ 1171 usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0); 1172 1173 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state); 1174 1175 KBD_INIT_DONE(kbd); 1176 1177 if (kbd_register(kbd) < 0) { 1178 goto detach; 1179 } 1180 KBD_CONFIG_DONE(kbd); 1181 1182 ukbd_enable(kbd); 1183 1184 #ifdef KBD_INSTALL_CDEV 1185 if (kbd_attach(kbd)) { 1186 goto detach; 1187 } 1188 #endif 1189 1190 #ifdef EVDEV_SUPPORT 1191 evdev = evdev_alloc(); 1192 evdev_set_name(evdev, device_get_desc(dev)); 1193 evdev_set_phys(evdev, device_get_nameunit(dev)); 1194 evdev_set_id(evdev, BUS_USB, uaa->info.idVendor, 1195 uaa->info.idProduct, 0); 1196 evdev_set_serial(evdev, usb_get_serial(uaa->device)); 1197 evdev_set_methods(evdev, kbd, &ukbd_evdev_methods); 1198 evdev_support_event(evdev, EV_SYN); 1199 evdev_support_event(evdev, EV_KEY); 1200 if (sc->sc_flags & (UKBD_FLAG_NUMLOCK | UKBD_FLAG_CAPSLOCK | 1201 UKBD_FLAG_SCROLLLOCK)) 1202 evdev_support_event(evdev, EV_LED); 1203 evdev_support_event(evdev, EV_REP); 1204 1205 for (i = 0x00; i <= 0xFF; i++) 1206 evdev_support_key(evdev, evdev_hid2key(i)); 1207 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) 1208 evdev_support_led(evdev, LED_NUML); 1209 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) 1210 evdev_support_led(evdev, LED_CAPSL); 1211 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) 1212 evdev_support_led(evdev, LED_SCROLLL); 1213 1214 if (evdev_register_mtx(evdev, &Giant)) 1215 evdev_free(evdev); 1216 else 1217 sc->sc_evdev = evdev; 1218 #endif 1219 1220 sc->sc_flags |= UKBD_FLAG_ATTACHED; 1221 1222 if (bootverbose) { 1223 kbdd_diag(kbd, bootverbose); 1224 } 1225 1226 #ifdef USB_DEBUG 1227 /* check for polling rate override */ 1228 rate = ukbd_pollrate; 1229 if (rate > 0) { 1230 if (rate > 1000) 1231 rate = 1; 1232 else 1233 rate = 1000 / rate; 1234 1235 /* set new polling interval in ms */ 1236 usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT_0], rate); 1237 usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT_1], rate); 1238 } 1239 #endif 1240 /* start the keyboard */ 1241 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]); 1242 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]); 1243 1244 return (0); /* success */ 1245 1246 detach: 1247 ukbd_detach(dev); 1248 return (ENXIO); /* error */ 1249 } 1250 1251 static int 1252 ukbd_detach(device_t dev) 1253 { 1254 struct ukbd_softc *sc = device_get_softc(dev); 1255 int error; 1256 1257 UKBD_LOCK_ASSERT(); 1258 1259 DPRINTF("\n"); 1260 1261 sc->sc_flags |= UKBD_FLAG_GONE; 1262 1263 usb_callout_stop(&sc->sc_callout); 1264 1265 /* kill any stuck keys */ 1266 if (sc->sc_flags & UKBD_FLAG_ATTACHED) { 1267 /* stop receiving events from the USB keyboard */ 1268 usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_0]); 1269 usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_1]); 1270 1271 /* release all leftover keys, if any */ 1272 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); 1273 1274 /* process releasing of all keys */ 1275 ukbd_interrupt(sc); 1276 } 1277 1278 ukbd_disable(&sc->sc_kbd); 1279 1280 #ifdef KBD_INSTALL_CDEV 1281 if (sc->sc_flags & UKBD_FLAG_ATTACHED) { 1282 error = kbd_detach(&sc->sc_kbd); 1283 if (error) { 1284 /* usb attach cannot return an error */ 1285 device_printf(dev, "WARNING: kbd_detach() " 1286 "returned non-zero! (ignored)\n"); 1287 } 1288 } 1289 #endif 1290 1291 #ifdef EVDEV_SUPPORT 1292 evdev_free(sc->sc_evdev); 1293 #endif 1294 1295 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { 1296 error = kbd_unregister(&sc->sc_kbd); 1297 if (error) { 1298 /* usb attach cannot return an error */ 1299 device_printf(dev, "WARNING: kbd_unregister() " 1300 "returned non-zero! (ignored)\n"); 1301 } 1302 } 1303 sc->sc_kbd.kb_flags = 0; 1304 1305 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER); 1306 1307 usb_callout_drain(&sc->sc_callout); 1308 1309 DPRINTF("%s: disconnected\n", 1310 device_get_nameunit(dev)); 1311 1312 return (0); 1313 } 1314 1315 static int 1316 ukbd_resume(device_t dev) 1317 { 1318 struct ukbd_softc *sc = device_get_softc(dev); 1319 1320 UKBD_LOCK_ASSERT(); 1321 1322 ukbd_clear_state(&sc->sc_kbd); 1323 1324 return (0); 1325 } 1326 1327 #ifdef EVDEV_SUPPORT 1328 static void 1329 ukbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 1330 int32_t value) 1331 { 1332 keyboard_t *kbd = evdev_get_softc(evdev); 1333 1334 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && 1335 (type == EV_LED || type == EV_REP)) { 1336 mtx_lock(&Giant); 1337 kbd_ev_event(kbd, type, code, value); 1338 mtx_unlock(&Giant); 1339 } 1340 } 1341 #endif 1342 1343 /* early keyboard probe, not supported */ 1344 static int 1345 ukbd_configure(int flags) 1346 { 1347 return (0); 1348 } 1349 1350 /* detect a keyboard, not used */ 1351 static int 1352 ukbd__probe(int unit, void *arg, int flags) 1353 { 1354 return (ENXIO); 1355 } 1356 1357 /* reset and initialize the device, not used */ 1358 static int 1359 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 1360 { 1361 return (ENXIO); 1362 } 1363 1364 /* test the interface to the device, not used */ 1365 static int 1366 ukbd_test_if(keyboard_t *kbd) 1367 { 1368 return (0); 1369 } 1370 1371 /* finish using this keyboard, not used */ 1372 static int 1373 ukbd_term(keyboard_t *kbd) 1374 { 1375 return (ENXIO); 1376 } 1377 1378 /* keyboard interrupt routine, not used */ 1379 static int 1380 ukbd_intr(keyboard_t *kbd, void *arg) 1381 { 1382 return (0); 1383 } 1384 1385 /* lock the access to the keyboard, not used */ 1386 static int 1387 ukbd_lock(keyboard_t *kbd, int lock) 1388 { 1389 return (1); 1390 } 1391 1392 /* 1393 * Enable the access to the device; until this function is called, 1394 * the client cannot read from the keyboard. 1395 */ 1396 static int 1397 ukbd_enable(keyboard_t *kbd) 1398 { 1399 1400 UKBD_LOCK(); 1401 KBD_ACTIVATE(kbd); 1402 UKBD_UNLOCK(); 1403 1404 return (0); 1405 } 1406 1407 /* disallow the access to the device */ 1408 static int 1409 ukbd_disable(keyboard_t *kbd) 1410 { 1411 1412 UKBD_LOCK(); 1413 KBD_DEACTIVATE(kbd); 1414 UKBD_UNLOCK(); 1415 1416 return (0); 1417 } 1418 1419 /* check if data is waiting */ 1420 /* Currently unused. */ 1421 static int 1422 ukbd_check(keyboard_t *kbd) 1423 { 1424 struct ukbd_softc *sc = kbd->kb_data; 1425 1426 UKBD_LOCK_ASSERT(); 1427 1428 if (!KBD_IS_ACTIVE(kbd)) 1429 return (0); 1430 1431 if (sc->sc_flags & UKBD_FLAG_POLLING) 1432 ukbd_do_poll(sc, 0); 1433 1434 #ifdef UKBD_EMULATE_ATSCANCODE 1435 if (sc->sc_buffered_char[0]) { 1436 return (1); 1437 } 1438 #endif 1439 if (sc->sc_inputs > 0) { 1440 return (1); 1441 } 1442 return (0); 1443 } 1444 1445 /* check if char is waiting */ 1446 static int 1447 ukbd_check_char_locked(keyboard_t *kbd) 1448 { 1449 struct ukbd_softc *sc = kbd->kb_data; 1450 1451 UKBD_LOCK_ASSERT(); 1452 1453 if (!KBD_IS_ACTIVE(kbd)) 1454 return (0); 1455 1456 if ((sc->sc_composed_char > 0) && 1457 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { 1458 return (1); 1459 } 1460 return (ukbd_check(kbd)); 1461 } 1462 1463 static int 1464 ukbd_check_char(keyboard_t *kbd) 1465 { 1466 int result; 1467 1468 UKBD_LOCK(); 1469 result = ukbd_check_char_locked(kbd); 1470 UKBD_UNLOCK(); 1471 1472 return (result); 1473 } 1474 1475 /* read one byte from the keyboard if it's allowed */ 1476 /* Currently unused. */ 1477 static int 1478 ukbd_read(keyboard_t *kbd, int wait) 1479 { 1480 struct ukbd_softc *sc = kbd->kb_data; 1481 int32_t usbcode; 1482 #ifdef UKBD_EMULATE_ATSCANCODE 1483 uint32_t keycode; 1484 uint32_t scancode; 1485 1486 #endif 1487 1488 UKBD_LOCK_ASSERT(); 1489 1490 if (!KBD_IS_ACTIVE(kbd)) 1491 return (-1); 1492 1493 #ifdef UKBD_EMULATE_ATSCANCODE 1494 if (sc->sc_buffered_char[0]) { 1495 scancode = sc->sc_buffered_char[0]; 1496 if (scancode & SCAN_PREFIX) { 1497 sc->sc_buffered_char[0] &= ~SCAN_PREFIX; 1498 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1499 } 1500 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1501 sc->sc_buffered_char[1] = 0; 1502 return (scancode); 1503 } 1504 #endif /* UKBD_EMULATE_ATSCANCODE */ 1505 1506 /* XXX */ 1507 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1508 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) 1509 return (-1); 1510 1511 ++(kbd->kb_count); 1512 1513 #ifdef UKBD_EMULATE_ATSCANCODE 1514 keycode = ukbd_atkeycode(usbcode, sc->sc_ndata.bitmap); 1515 if (keycode == NN) { 1516 return -1; 1517 } 1518 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.bitmap, 1519 (usbcode & KEY_RELEASE))); 1520 #else /* !UKBD_EMULATE_ATSCANCODE */ 1521 return (usbcode); 1522 #endif /* UKBD_EMULATE_ATSCANCODE */ 1523 } 1524 1525 /* read char from the keyboard */ 1526 static uint32_t 1527 ukbd_read_char_locked(keyboard_t *kbd, int wait) 1528 { 1529 struct ukbd_softc *sc = kbd->kb_data; 1530 uint32_t action; 1531 uint32_t keycode; 1532 int32_t usbcode; 1533 #ifdef UKBD_EMULATE_ATSCANCODE 1534 uint32_t scancode; 1535 #endif 1536 1537 UKBD_LOCK_ASSERT(); 1538 1539 if (!KBD_IS_ACTIVE(kbd)) 1540 return (NOKEY); 1541 1542 next_code: 1543 1544 /* do we have a composed char to return ? */ 1545 1546 if ((sc->sc_composed_char > 0) && 1547 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) { 1548 1549 action = sc->sc_composed_char; 1550 sc->sc_composed_char = 0; 1551 1552 if (action > 0xFF) { 1553 goto errkey; 1554 } 1555 goto done; 1556 } 1557 #ifdef UKBD_EMULATE_ATSCANCODE 1558 1559 /* do we have a pending raw scan code? */ 1560 1561 if (sc->sc_mode == K_RAW) { 1562 scancode = sc->sc_buffered_char[0]; 1563 if (scancode) { 1564 if (scancode & SCAN_PREFIX) { 1565 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX); 1566 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1567 } 1568 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1569 sc->sc_buffered_char[1] = 0; 1570 return (scancode); 1571 } 1572 } 1573 #endif /* UKBD_EMULATE_ATSCANCODE */ 1574 1575 /* see if there is something in the keyboard port */ 1576 /* XXX */ 1577 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1578 if (usbcode == -1) { 1579 return (NOKEY); 1580 } 1581 ++kbd->kb_count; 1582 1583 #ifdef UKBD_EMULATE_ATSCANCODE 1584 /* USB key index -> key code -> AT scan code */ 1585 keycode = ukbd_atkeycode(usbcode, sc->sc_ndata.bitmap); 1586 if (keycode == NN) { 1587 return (NOKEY); 1588 } 1589 /* return an AT scan code for the K_RAW mode */ 1590 if (sc->sc_mode == K_RAW) { 1591 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.bitmap, 1592 (usbcode & KEY_RELEASE))); 1593 } 1594 #else /* !UKBD_EMULATE_ATSCANCODE */ 1595 1596 /* return the byte as is for the K_RAW mode */ 1597 if (sc->sc_mode == K_RAW) { 1598 return (usbcode); 1599 } 1600 /* USB key index -> key code */ 1601 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 1602 if (keycode == NN) { 1603 return (NOKEY); 1604 } 1605 #endif /* UKBD_EMULATE_ATSCANCODE */ 1606 1607 switch (keycode) { 1608 case 0x38: /* left alt (compose key) */ 1609 if (usbcode & KEY_RELEASE) { 1610 if (sc->sc_flags & UKBD_FLAG_COMPOSE) { 1611 sc->sc_flags &= ~UKBD_FLAG_COMPOSE; 1612 1613 if (sc->sc_composed_char > 0xFF) { 1614 sc->sc_composed_char = 0; 1615 } 1616 } 1617 } else { 1618 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) { 1619 sc->sc_flags |= UKBD_FLAG_COMPOSE; 1620 sc->sc_composed_char = 0; 1621 } 1622 } 1623 break; 1624 } 1625 1626 /* return the key code in the K_CODE mode */ 1627 if (usbcode & KEY_RELEASE) { 1628 keycode |= SCAN_RELEASE; 1629 } 1630 if (sc->sc_mode == K_CODE) { 1631 return (keycode); 1632 } 1633 /* compose a character code */ 1634 if (sc->sc_flags & UKBD_FLAG_COMPOSE) { 1635 switch (keycode) { 1636 /* key pressed, process it */ 1637 case 0x47: 1638 case 0x48: 1639 case 0x49: /* keypad 7,8,9 */ 1640 sc->sc_composed_char *= 10; 1641 sc->sc_composed_char += keycode - 0x40; 1642 goto check_composed; 1643 1644 case 0x4B: 1645 case 0x4C: 1646 case 0x4D: /* keypad 4,5,6 */ 1647 sc->sc_composed_char *= 10; 1648 sc->sc_composed_char += keycode - 0x47; 1649 goto check_composed; 1650 1651 case 0x4F: 1652 case 0x50: 1653 case 0x51: /* keypad 1,2,3 */ 1654 sc->sc_composed_char *= 10; 1655 sc->sc_composed_char += keycode - 0x4E; 1656 goto check_composed; 1657 1658 case 0x52: /* keypad 0 */ 1659 sc->sc_composed_char *= 10; 1660 goto check_composed; 1661 1662 /* key released, no interest here */ 1663 case SCAN_RELEASE | 0x47: 1664 case SCAN_RELEASE | 0x48: 1665 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */ 1666 case SCAN_RELEASE | 0x4B: 1667 case SCAN_RELEASE | 0x4C: 1668 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */ 1669 case SCAN_RELEASE | 0x4F: 1670 case SCAN_RELEASE | 0x50: 1671 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */ 1672 case SCAN_RELEASE | 0x52: /* keypad 0 */ 1673 goto next_code; 1674 1675 case 0x38: /* left alt key */ 1676 break; 1677 1678 default: 1679 if (sc->sc_composed_char > 0) { 1680 sc->sc_flags &= ~UKBD_FLAG_COMPOSE; 1681 sc->sc_composed_char = 0; 1682 goto errkey; 1683 } 1684 break; 1685 } 1686 } 1687 /* keycode to key action */ 1688 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), 1689 (keycode & SCAN_RELEASE), 1690 &sc->sc_state, &sc->sc_accents); 1691 if (action == NOKEY) { 1692 goto next_code; 1693 } 1694 done: 1695 return (action); 1696 1697 check_composed: 1698 if (sc->sc_composed_char <= 0xFF) { 1699 goto next_code; 1700 } 1701 errkey: 1702 return (ERRKEY); 1703 } 1704 1705 /* Currently wait is always false. */ 1706 static uint32_t 1707 ukbd_read_char(keyboard_t *kbd, int wait) 1708 { 1709 uint32_t keycode; 1710 1711 UKBD_LOCK(); 1712 keycode = ukbd_read_char_locked(kbd, wait); 1713 UKBD_UNLOCK(); 1714 1715 return (keycode); 1716 } 1717 1718 /* some useful control functions */ 1719 static int 1720 ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) 1721 { 1722 struct ukbd_softc *sc = kbd->kb_data; 1723 int i; 1724 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1725 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1726 int ival; 1727 1728 #endif 1729 1730 UKBD_LOCK_ASSERT(); 1731 1732 switch (cmd) { 1733 case KDGKBMODE: /* get keyboard mode */ 1734 *(int *)arg = sc->sc_mode; 1735 break; 1736 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1737 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1738 case _IO('K', 7): 1739 ival = IOCPARM_IVAL(arg); 1740 arg = (caddr_t)&ival; 1741 /* FALLTHROUGH */ 1742 #endif 1743 case KDSKBMODE: /* set keyboard mode */ 1744 switch (*(int *)arg) { 1745 case K_XLATE: 1746 if (sc->sc_mode != K_XLATE) { 1747 /* make lock key state and LED state match */ 1748 sc->sc_state &= ~LOCK_MASK; 1749 sc->sc_state |= KBD_LED_VAL(kbd); 1750 } 1751 /* FALLTHROUGH */ 1752 case K_RAW: 1753 case K_CODE: 1754 if (sc->sc_mode != *(int *)arg) { 1755 if ((sc->sc_flags & UKBD_FLAG_POLLING) == 0) 1756 ukbd_clear_state(kbd); 1757 sc->sc_mode = *(int *)arg; 1758 } 1759 break; 1760 default: 1761 return (EINVAL); 1762 } 1763 break; 1764 1765 case KDGETLED: /* get keyboard LED */ 1766 *(int *)arg = KBD_LED_VAL(kbd); 1767 break; 1768 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1769 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1770 case _IO('K', 66): 1771 ival = IOCPARM_IVAL(arg); 1772 arg = (caddr_t)&ival; 1773 /* FALLTHROUGH */ 1774 #endif 1775 case KDSETLED: /* set keyboard LED */ 1776 /* NOTE: lock key state in "sc_state" won't be changed */ 1777 if (*(int *)arg & ~LOCK_MASK) 1778 return (EINVAL); 1779 1780 i = *(int *)arg; 1781 1782 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 1783 if (sc->sc_mode == K_XLATE && 1784 kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 1785 if (i & ALKED) 1786 i |= CLKED; 1787 else 1788 i &= ~CLKED; 1789 } 1790 if (KBD_HAS_DEVICE(kbd)) 1791 ukbd_set_leds(sc, i); 1792 1793 KBD_LED_VAL(kbd) = *(int *)arg; 1794 break; 1795 case KDGKBSTATE: /* get lock key state */ 1796 *(int *)arg = sc->sc_state & LOCK_MASK; 1797 break; 1798 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1799 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1800 case _IO('K', 20): 1801 ival = IOCPARM_IVAL(arg); 1802 arg = (caddr_t)&ival; 1803 /* FALLTHROUGH */ 1804 #endif 1805 case KDSKBSTATE: /* set lock key state */ 1806 if (*(int *)arg & ~LOCK_MASK) { 1807 return (EINVAL); 1808 } 1809 sc->sc_state &= ~LOCK_MASK; 1810 sc->sc_state |= *(int *)arg; 1811 1812 /* set LEDs and quit */ 1813 return (ukbd_ioctl(kbd, KDSETLED, arg)); 1814 1815 case KDSETREPEAT: /* set keyboard repeat rate (new 1816 * interface) */ 1817 if (!KBD_HAS_DEVICE(kbd)) { 1818 return (0); 1819 } 1820 /* 1821 * Convert negative, zero and tiny args to the same limits 1822 * as atkbd. We could support delays of 1 msec, but 1823 * anything much shorter than the shortest atkbd value 1824 * of 250.34 is almost unusable as well as incompatible. 1825 */ 1826 kbd->kb_delay1 = imax(((int *)arg)[0], 250); 1827 kbd->kb_delay2 = imax(((int *)arg)[1], 34); 1828 #ifdef EVDEV_SUPPORT 1829 if (sc->sc_evdev != NULL) 1830 evdev_push_repeats(sc->sc_evdev, kbd); 1831 #endif 1832 return (0); 1833 1834 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1835 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1836 case _IO('K', 67): 1837 ival = IOCPARM_IVAL(arg); 1838 arg = (caddr_t)&ival; 1839 /* FALLTHROUGH */ 1840 #endif 1841 case KDSETRAD: /* set keyboard repeat rate (old 1842 * interface) */ 1843 return (ukbd_set_typematic(kbd, *(int *)arg)); 1844 1845 case PIO_KEYMAP: /* set keyboard translation table */ 1846 case OPIO_KEYMAP: /* set keyboard translation table 1847 * (compat) */ 1848 case PIO_KEYMAPENT: /* set keyboard translation table 1849 * entry */ 1850 case PIO_DEADKEYMAP: /* set accent key translation table */ 1851 sc->sc_accents = 0; 1852 /* FALLTHROUGH */ 1853 default: 1854 return (genkbd_commonioctl(kbd, cmd, arg)); 1855 } 1856 1857 return (0); 1858 } 1859 1860 static int 1861 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 1862 { 1863 int result; 1864 1865 /* 1866 * XXX Check if someone is calling us from a critical section: 1867 */ 1868 if (curthread->td_critnest != 0) 1869 return (EDEADLK); 1870 1871 /* 1872 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any 1873 * context where printf(9) can be called, which among other things 1874 * includes interrupt filters and threads with any kinds of locks 1875 * already held. For this reason it would be dangerous to acquire 1876 * the Giant here unconditionally. On the other hand we have to 1877 * have it to handle the ioctl. 1878 * So we make our best effort to auto-detect whether we can grab 1879 * the Giant or not. Blame syscons(4) for this. 1880 */ 1881 switch (cmd) { 1882 case KDGKBSTATE: 1883 case KDSKBSTATE: 1884 case KDSETLED: 1885 if (!mtx_owned(&Giant) && !USB_IN_POLLING_MODE_FUNC()) 1886 return (EDEADLK); /* best I could come up with */ 1887 /* FALLTHROUGH */ 1888 default: 1889 UKBD_LOCK(); 1890 result = ukbd_ioctl_locked(kbd, cmd, arg); 1891 UKBD_UNLOCK(); 1892 return (result); 1893 } 1894 } 1895 1896 1897 /* clear the internal state of the keyboard */ 1898 static void 1899 ukbd_clear_state(keyboard_t *kbd) 1900 { 1901 struct ukbd_softc *sc = kbd->kb_data; 1902 1903 UKBD_LOCK_ASSERT(); 1904 1905 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING); 1906 sc->sc_state &= LOCK_MASK; /* preserve locking key state */ 1907 sc->sc_accents = 0; 1908 sc->sc_composed_char = 0; 1909 #ifdef UKBD_EMULATE_ATSCANCODE 1910 sc->sc_buffered_char[0] = 0; 1911 sc->sc_buffered_char[1] = 0; 1912 #endif 1913 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata)); 1914 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata)); 1915 sc->sc_repeat_time = 0; 1916 sc->sc_repeat_key = 0; 1917 } 1918 1919 /* save the internal state, not used */ 1920 static int 1921 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len) 1922 { 1923 return (len == 0) ? 1 : -1; 1924 } 1925 1926 /* set the internal state, not used */ 1927 static int 1928 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len) 1929 { 1930 return (EINVAL); 1931 } 1932 1933 static int 1934 ukbd_poll(keyboard_t *kbd, int on) 1935 { 1936 struct ukbd_softc *sc = kbd->kb_data; 1937 1938 UKBD_LOCK(); 1939 /* 1940 * Keep a reference count on polling to allow recursive 1941 * cngrab() during a panic for example. 1942 */ 1943 if (on) 1944 sc->sc_polling++; 1945 else if (sc->sc_polling > 0) 1946 sc->sc_polling--; 1947 1948 if (sc->sc_polling != 0) { 1949 sc->sc_flags |= UKBD_FLAG_POLLING; 1950 sc->sc_poll_thread = curthread; 1951 } else { 1952 sc->sc_flags &= ~UKBD_FLAG_POLLING; 1953 sc->sc_delay = 0; 1954 } 1955 UKBD_UNLOCK(); 1956 1957 return (0); 1958 } 1959 1960 /* local functions */ 1961 1962 static void 1963 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds) 1964 { 1965 1966 UKBD_LOCK_ASSERT(); 1967 DPRINTF("leds=0x%02x\n", leds); 1968 1969 sc->sc_leds = leds; 1970 sc->sc_flags |= UKBD_FLAG_SET_LEDS; 1971 1972 /* start transfer, if not already started */ 1973 1974 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]); 1975 } 1976 1977 static int 1978 ukbd_set_typematic(keyboard_t *kbd, int code) 1979 { 1980 #ifdef EVDEV_SUPPORT 1981 struct ukbd_softc *sc = kbd->kb_data; 1982 #endif 1983 static const int delays[] = {250, 500, 750, 1000}; 1984 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63, 1985 68, 76, 84, 92, 100, 110, 118, 126, 1986 136, 152, 168, 184, 200, 220, 236, 252, 1987 272, 304, 336, 368, 400, 440, 472, 504}; 1988 1989 if (code & ~0x7f) { 1990 return (EINVAL); 1991 } 1992 kbd->kb_delay1 = delays[(code >> 5) & 3]; 1993 kbd->kb_delay2 = rates[code & 0x1f]; 1994 #ifdef EVDEV_SUPPORT 1995 if (sc->sc_evdev != NULL) 1996 evdev_push_repeats(sc->sc_evdev, kbd); 1997 #endif 1998 return (0); 1999 } 2000 2001 #ifdef UKBD_EMULATE_ATSCANCODE 2002 static uint32_t 2003 ukbd_atkeycode(int usbcode, const uint64_t *bitmap) 2004 { 2005 uint32_t keycode; 2006 2007 keycode = ukbd_trtab[KEY_INDEX(usbcode)]; 2008 2009 /* 2010 * Translate Alt-PrintScreen to SysRq. 2011 * 2012 * Some or all AT keyboards connected through USB have already 2013 * mapped Alted PrintScreens to an unusual usbcode (0x8a). 2014 * ukbd_trtab translates this to 0x7e, and key2scan() would 2015 * translate that to 0x79 (Intl' 4). Assume that if we have 2016 * an Alted 0x7e here then it actually is an Alted PrintScreen. 2017 * 2018 * The usual usbcode for all PrintScreens is 0x46. ukbd_trtab 2019 * translates this to 0x5c, so the Alt check to classify 0x5c 2020 * is routine. 2021 */ 2022 if ((keycode == 0x5c || keycode == 0x7e) && 2023 (UKBD_KEY_PRESSED(bitmap, 0xe2 /* ALT-L */) || 2024 UKBD_KEY_PRESSED(bitmap, 0xe6 /* ALT-R */))) 2025 return (0x54); 2026 return (keycode); 2027 } 2028 2029 static int 2030 ukbd_key2scan(struct ukbd_softc *sc, int code, const uint64_t *bitmap, int up) 2031 { 2032 static const int scan[] = { 2033 /* 89 */ 2034 0x11c, /* Enter */ 2035 /* 90-99 */ 2036 0x11d, /* Ctrl-R */ 2037 0x135, /* Divide */ 2038 0x137, /* PrintScreen */ 2039 0x138, /* Alt-R */ 2040 0x147, /* Home */ 2041 0x148, /* Up */ 2042 0x149, /* PageUp */ 2043 0x14b, /* Left */ 2044 0x14d, /* Right */ 2045 0x14f, /* End */ 2046 /* 100-109 */ 2047 0x150, /* Down */ 2048 0x151, /* PageDown */ 2049 0x152, /* Insert */ 2050 0x153, /* Delete */ 2051 0x146, /* Pause/Break */ 2052 0x15b, /* Win_L(Super_L) */ 2053 0x15c, /* Win_R(Super_R) */ 2054 0x15d, /* Application(Menu) */ 2055 2056 /* SUN TYPE 6 USB KEYBOARD */ 2057 0x168, /* Sun Type 6 Help */ 2058 0x15e, /* Sun Type 6 Stop */ 2059 /* 110 - 119 */ 2060 0x15f, /* Sun Type 6 Again */ 2061 0x160, /* Sun Type 6 Props */ 2062 0x161, /* Sun Type 6 Undo */ 2063 0x162, /* Sun Type 6 Front */ 2064 0x163, /* Sun Type 6 Copy */ 2065 0x164, /* Sun Type 6 Open */ 2066 0x165, /* Sun Type 6 Paste */ 2067 0x166, /* Sun Type 6 Find */ 2068 0x167, /* Sun Type 6 Cut */ 2069 0x125, /* Sun Type 6 Mute */ 2070 /* 120 - 130 */ 2071 0x11f, /* Sun Type 6 VolumeDown */ 2072 0x11e, /* Sun Type 6 VolumeUp */ 2073 0x120, /* Sun Type 6 PowerDown */ 2074 2075 /* Japanese 106/109 keyboard */ 2076 0x73, /* Keyboard Intl' 1 (backslash / underscore) */ 2077 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */ 2078 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */ 2079 0x79, /* Keyboard Intl' 4 (Henkan) */ 2080 0x7b, /* Keyboard Intl' 5 (Muhenkan) */ 2081 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */ 2082 0x71, /* Apple Keyboard JIS (Kana) */ 2083 0x72, /* Apple Keyboard JIS (Eisu) */ 2084 }; 2085 2086 if ((code >= 89) && (code < (int)(89 + nitems(scan)))) { 2087 code = scan[code - 89]; 2088 } 2089 /* PrintScreen */ 2090 if (code == 0x137 && (!( 2091 UKBD_KEY_PRESSED(bitmap, 0xe0 /* CTRL-L */) || 2092 UKBD_KEY_PRESSED(bitmap, 0xe4 /* CTRL-R */) || 2093 UKBD_KEY_PRESSED(bitmap, 0xe1 /* SHIFT-L */) || 2094 UKBD_KEY_PRESSED(bitmap, 0xe5 /* SHIFT-R */)))) { 2095 code |= SCAN_PREFIX_SHIFT; 2096 } 2097 /* Pause/Break */ 2098 if ((code == 0x146) && (!( 2099 UKBD_KEY_PRESSED(bitmap, 0xe0 /* CTRL-L */) || 2100 UKBD_KEY_PRESSED(bitmap, 0xe4 /* CTRL-R */)))) { 2101 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL); 2102 } 2103 code |= (up ? SCAN_RELEASE : SCAN_PRESS); 2104 2105 if (code & SCAN_PREFIX) { 2106 if (code & SCAN_PREFIX_CTL) { 2107 /* Ctrl */ 2108 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE)); 2109 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX); 2110 } else if (code & SCAN_PREFIX_SHIFT) { 2111 /* Shift */ 2112 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE)); 2113 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT); 2114 } else { 2115 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX); 2116 sc->sc_buffered_char[1] = 0; 2117 } 2118 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 2119 } 2120 return (code); 2121 2122 } 2123 2124 #endif /* UKBD_EMULATE_ATSCANCODE */ 2125 2126 static keyboard_switch_t ukbdsw = { 2127 .probe = &ukbd__probe, 2128 .init = &ukbd_init, 2129 .term = &ukbd_term, 2130 .intr = &ukbd_intr, 2131 .test_if = &ukbd_test_if, 2132 .enable = &ukbd_enable, 2133 .disable = &ukbd_disable, 2134 .read = &ukbd_read, 2135 .check = &ukbd_check, 2136 .read_char = &ukbd_read_char, 2137 .check_char = &ukbd_check_char, 2138 .ioctl = &ukbd_ioctl, 2139 .lock = &ukbd_lock, 2140 .clear_state = &ukbd_clear_state, 2141 .get_state = &ukbd_get_state, 2142 .set_state = &ukbd_set_state, 2143 .poll = &ukbd_poll, 2144 }; 2145 2146 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure); 2147 2148 static int 2149 ukbd_driver_load(module_t mod, int what, void *arg) 2150 { 2151 switch (what) { 2152 case MOD_LOAD: 2153 kbd_add_driver(&ukbd_kbd_driver); 2154 break; 2155 case MOD_UNLOAD: 2156 kbd_delete_driver(&ukbd_kbd_driver); 2157 break; 2158 } 2159 return (0); 2160 } 2161 2162 static devclass_t ukbd_devclass; 2163 2164 static device_method_t ukbd_methods[] = { 2165 DEVMETHOD(device_probe, ukbd_probe), 2166 DEVMETHOD(device_attach, ukbd_attach), 2167 DEVMETHOD(device_detach, ukbd_detach), 2168 DEVMETHOD(device_resume, ukbd_resume), 2169 2170 DEVMETHOD_END 2171 }; 2172 2173 static driver_t ukbd_driver = { 2174 .name = "ukbd", 2175 .methods = ukbd_methods, 2176 .size = sizeof(struct ukbd_softc), 2177 }; 2178 2179 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0); 2180 MODULE_DEPEND(ukbd, usb, 1, 1, 1); 2181 #ifdef EVDEV_SUPPORT 2182 MODULE_DEPEND(ukbd, evdev, 1, 1, 1); 2183 #endif 2184 MODULE_VERSION(ukbd, 1); 2185 USB_PNP_HOST_INFO(ukbd_devs); 2186