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