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