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