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