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