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