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