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