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