1 #include <sys/cdefs.h> 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause 4 * 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 * 33 */ 34 35 /* 36 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 37 */ 38 39 #include "opt_hid.h" 40 #include "opt_kbd.h" 41 #include "opt_hkbd.h" 42 #include "opt_evdev.h" 43 44 #include <sys/stdint.h> 45 #include <sys/stddef.h> 46 #include <sys/param.h> 47 #include <sys/queue.h> 48 #include <sys/types.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/bus.h> 52 #include <sys/module.h> 53 #include <sys/lock.h> 54 #include <sys/mutex.h> 55 #include <sys/condvar.h> 56 #include <sys/sysctl.h> 57 #include <sys/sx.h> 58 #include <sys/unistd.h> 59 #include <sys/callout.h> 60 #include <sys/malloc.h> 61 #include <sys/priv.h> 62 #include <sys/proc.h> 63 #include <sys/kdb.h> 64 #include <sys/epoch.h> 65 #include <sys/taskqueue.h> 66 #include <sys/bitstring.h> 67 68 #include <machine/atomic.h> 69 70 #define HID_DEBUG_VAR hkbd_debug 71 #include <dev/hid/hid.h> 72 #include <dev/hid/hidbus.h> 73 #include <dev/hid/hidquirk.h> 74 #include <dev/hid/hidrdesc.h> 75 76 #include "usbdevs.h" 77 78 #ifdef EVDEV_SUPPORT 79 #include <dev/evdev/input.h> 80 #include <dev/evdev/evdev.h> 81 #endif 82 83 #include <sys/ioccom.h> 84 #include <sys/filio.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(HKBD_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 HID_DEBUG 99 static int hkbd_debug = 0; 100 #endif 101 static int hkbd_no_leds = 0; 102 static int hkbd_apple_fn_mode = 0; 103 104 static SYSCTL_NODE(_hw_hid, OID_AUTO, hkbd, CTLFLAG_RW, 0, "USB keyboard"); 105 #ifdef HID_DEBUG 106 SYSCTL_INT(_hw_hid_hkbd, OID_AUTO, debug, CTLFLAG_RWTUN, 107 &hkbd_debug, 0, "Debug level"); 108 #endif 109 SYSCTL_INT(_hw_hid_hkbd, OID_AUTO, no_leds, CTLFLAG_RWTUN, 110 &hkbd_no_leds, 0, "Disables setting of keyboard leds"); 111 SYSCTL_INT(_hw_hid_hkbd, OID_AUTO, apple_fn_mode, CTLFLAG_RWTUN, 112 &hkbd_apple_fn_mode, 0, "0 = Fn + F1..12 -> media, 1 = F1..F12 -> media"); 113 114 #define INPUT_EPOCH global_epoch_preempt 115 116 #define HKBD_EMULATE_ATSCANCODE 1 117 #define HKBD_DRIVER_NAME "hkbd" 118 #define HKBD_NKEYCODE 256 /* units */ 119 #define HKBD_IN_BUF_SIZE (4 * HKBD_NKEYCODE) /* scancodes */ 120 #define HKBD_IN_BUF_FULL ((HKBD_IN_BUF_SIZE / 2) - 1) /* scancodes */ 121 #define HKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */ 122 #define HKBD_BUFFER_SIZE 64 /* bytes */ 123 #define HKBD_KEY_PRESSED(map, key) ({ \ 124 CTASSERT((key) >= 0 && (key) < HKBD_NKEYCODE); \ 125 bit_test(map, key); \ 126 }) 127 128 #define MOD_EJECT 0x01 129 #define MOD_FN 0x02 130 131 #define MOD_MIN 0xe0 132 #define MOD_MAX 0xe7 133 134 /* check evdev_usb_scancodes[] for names */ 135 #define APPLE_FN_KEY 0xff 136 #define APPLE_EJECT_KEY 0xec 137 138 struct hkbd_softc { 139 device_t sc_dev; 140 141 keyboard_t sc_kbd; 142 keymap_t sc_keymap; 143 accentmap_t sc_accmap; 144 fkeytab_t sc_fkeymap[HKBD_NFKEY]; 145 bitstr_t bit_decl(sc_loc_key_valid, HKBD_NKEYCODE); 146 struct hid_location sc_loc_apple_eject; 147 struct hid_location sc_loc_apple_fn; 148 struct hid_location sc_loc_key[HKBD_NKEYCODE]; 149 struct hid_location sc_loc_numlock; 150 struct hid_location sc_loc_capslock; 151 struct hid_location sc_loc_scrolllock; 152 struct mtx sc_mtx; 153 struct task sc_task; 154 struct callout sc_callout; 155 /* All reported keycodes */ 156 bitstr_t bit_decl(sc_ndata, HKBD_NKEYCODE); 157 bitstr_t bit_decl(sc_odata, HKBD_NKEYCODE); 158 /* Keycodes reported in array fields only */ 159 bitstr_t bit_decl(sc_ndata0, HKBD_NKEYCODE); 160 bitstr_t bit_decl(sc_odata0, HKBD_NKEYCODE); 161 162 struct thread *sc_poll_thread; 163 #ifdef EVDEV_SUPPORT 164 struct evdev_dev *sc_evdev; 165 #endif 166 167 sbintime_t sc_co_basetime; 168 int sc_delay; 169 uint32_t sc_repeat_time; 170 uint32_t sc_input[HKBD_IN_BUF_SIZE]; /* input buffer */ 171 uint32_t sc_time_ms; 172 uint32_t sc_composed_char; /* composed char code, if non-zero */ 173 #ifdef HKBD_EMULATE_ATSCANCODE 174 uint32_t sc_buffered_char[2]; 175 #endif 176 uint32_t sc_flags; /* flags */ 177 #define HKBD_FLAG_COMPOSE 0x00000001 178 #define HKBD_FLAG_POLLING 0x00000002 179 #define HKBD_FLAG_ATTACHED 0x00000010 180 #define HKBD_FLAG_GONE 0x00000020 181 182 #define HKBD_FLAG_HID_MASK 0x003fffc0 183 #define HKBD_FLAG_APPLE_EJECT 0x00000040 184 #define HKBD_FLAG_APPLE_FN 0x00000080 185 #define HKBD_FLAG_APPLE_SWAP 0x00000100 186 #define HKBD_FLAG_NUMLOCK 0x00080000 187 #define HKBD_FLAG_CAPSLOCK 0x00100000 188 #define HKBD_FLAG_SCROLLLOCK 0x00200000 189 190 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ 191 int sc_state; /* shift/lock key state */ 192 int sc_accents; /* accent key index (> 0) */ 193 int sc_polling; /* polling recursion count */ 194 int sc_led_size; 195 int sc_kbd_size; 196 197 uint32_t sc_inputhead; 198 uint32_t sc_inputtail; 199 200 uint8_t sc_iface_index; 201 uint8_t sc_iface_no; 202 uint8_t sc_id_apple_eject; 203 uint8_t sc_id_apple_fn; 204 uint8_t sc_id_loc_key[HKBD_NKEYCODE]; 205 uint8_t sc_id_leds; 206 uint8_t sc_kbd_id; 207 uint8_t sc_repeat_key; 208 209 uint8_t sc_buffer[HKBD_BUFFER_SIZE]; 210 }; 211 212 #define KEY_NONE 0x00 213 #define KEY_ERROR 0x01 214 215 #define KEY_PRESS 0 216 #define KEY_RELEASE 0x400 217 #define KEY_INDEX(c) ((c) & 0xFF) 218 219 #define SCAN_PRESS 0 220 #define SCAN_RELEASE 0x80 221 #define SCAN_PREFIX_E0 0x100 222 #define SCAN_PREFIX_E1 0x200 223 #define SCAN_PREFIX_CTL 0x400 224 #define SCAN_PREFIX_SHIFT 0x800 225 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \ 226 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT) 227 #define SCAN_CHAR(c) ((c) & 0x7f) 228 229 #define HKBD_LOCK(sc) do { \ 230 if (!HID_IN_POLLING_MODE()) \ 231 mtx_lock(&(sc)->sc_mtx); \ 232 } while (0) 233 #define HKBD_UNLOCK(sc) do { \ 234 if (!HID_IN_POLLING_MODE()) \ 235 mtx_unlock(&(sc)->sc_mtx); \ 236 } while (0) 237 #define HKBD_LOCK_ASSERT(sc) do { \ 238 if (!HID_IN_POLLING_MODE()) \ 239 mtx_assert(&(sc)->sc_mtx, MA_OWNED); \ 240 } while (0) 241 #define SYSCONS_LOCK() do { \ 242 if (!HID_IN_POLLING_MODE()) \ 243 mtx_lock(&Giant); \ 244 } while (0) 245 #define SYSCONS_UNLOCK() do { \ 246 if (!HID_IN_POLLING_MODE()) \ 247 mtx_unlock(&Giant); \ 248 } while (0) 249 #define SYSCONS_LOCK_ASSERT() do { \ 250 if (!HID_IN_POLLING_MODE()) \ 251 mtx_assert(&Giant, MA_OWNED); \ 252 } while (0) 253 254 #define NN 0 /* no translation */ 255 /* 256 * Translate USB keycodes to AT keyboard scancodes. 257 */ 258 /* 259 * FIXME: Mac USB keyboard generates: 260 * 0x53: keypad NumLock/Clear 261 * 0x66: Power 262 * 0x67: keypad = 263 * 0x68: F13 264 * 0x69: F14 265 * 0x6a: F15 266 * 267 * USB Apple Keyboard JIS generates: 268 * 0x90: Kana 269 * 0x91: Eisu 270 */ 271 static const uint8_t hkbd_trtab[256] = { 272 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */ 273 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */ 274 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */ 275 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */ 276 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */ 277 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */ 278 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */ 279 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */ 280 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */ 281 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */ 282 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */ 283 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */ 284 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */ 285 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */ 286 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */ 287 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */ 288 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */ 289 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */ 290 129, 130, NN, NN, NN, NN, NN, NN, /* 90 - 97 */ 291 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */ 292 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */ 293 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */ 294 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */ 295 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */ 296 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */ 297 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */ 298 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */ 299 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */ 300 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */ 301 NN, NN, NN, NN, 254, NN, NN, NN, /* E8 - EF */ 302 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */ 303 NN, NN, NN, NN, NN, NN, NN, 255, /* F8 - FF */ 304 }; 305 306 static const uint8_t hkbd_boot_desc[] = { HID_KBD_BOOTPROTO_DESCR() }; 307 308 /* prototypes */ 309 static void hkbd_timeout(void *); 310 static int hkbd_set_leds(struct hkbd_softc *, uint8_t); 311 static int hkbd_set_typematic(keyboard_t *, int); 312 #ifdef HKBD_EMULATE_ATSCANCODE 313 static uint32_t hkbd_atkeycode(int, const bitstr_t *); 314 static int hkbd_key2scan(struct hkbd_softc *, int, const bitstr_t *, int); 315 #endif 316 static uint32_t hkbd_read_char(keyboard_t *, int); 317 static void hkbd_clear_state(keyboard_t *); 318 static int hkbd_ioctl(keyboard_t *, u_long, caddr_t); 319 static int hkbd_enable(keyboard_t *); 320 static int hkbd_disable(keyboard_t *); 321 static void hkbd_interrupt(struct hkbd_softc *); 322 323 static task_fn_t hkbd_event_keyinput; 324 325 static device_probe_t hkbd_probe; 326 static device_attach_t hkbd_attach; 327 static device_detach_t hkbd_detach; 328 static device_resume_t hkbd_resume; 329 330 #ifdef EVDEV_SUPPORT 331 static evdev_event_t hkbd_ev_event; 332 333 static const struct evdev_methods hkbd_evdev_methods = { 334 .ev_event = hkbd_ev_event, 335 }; 336 #endif 337 338 static bool 339 hkbd_any_key_pressed(struct hkbd_softc *sc) 340 { 341 int result; 342 343 bit_ffs(sc->sc_odata, HKBD_NKEYCODE, &result); 344 return (result != -1); 345 } 346 347 static bool 348 hkbd_any_key_valid(struct hkbd_softc *sc) 349 { 350 int result; 351 352 bit_ffs(sc->sc_loc_key_valid, HKBD_NKEYCODE, &result); 353 return (result != -1); 354 } 355 356 static bool 357 hkbd_is_modifier_key(uint32_t key) 358 { 359 360 return (key >= MOD_MIN && key <= MOD_MAX); 361 } 362 363 static void 364 hkbd_start_timer(struct hkbd_softc *sc) 365 { 366 sbintime_t delay, now, prec; 367 368 now = sbinuptime(); 369 370 /* check if initial delay passed and fallback to key repeat delay */ 371 if (sc->sc_delay == 0) 372 sc->sc_delay = sc->sc_kbd.kb_delay2; 373 374 /* compute timeout */ 375 delay = SBT_1MS * sc->sc_delay; 376 sc->sc_co_basetime += delay; 377 378 /* check if we are running behind */ 379 if (sc->sc_co_basetime < now) 380 sc->sc_co_basetime = now; 381 382 /* This is rarely called, so prefer precision to efficiency. */ 383 prec = qmin(delay >> 7, SBT_1MS * 10); 384 if (!HID_IN_POLLING_MODE()) 385 callout_reset_sbt(&sc->sc_callout, sc->sc_co_basetime, prec, 386 hkbd_timeout, sc, C_ABSOLUTE); 387 } 388 389 static void 390 hkbd_put_key(struct hkbd_softc *sc, uint32_t key) 391 { 392 uint32_t tail; 393 394 HKBD_LOCK_ASSERT(sc); 395 396 DPRINTF("0x%02x (%d) %s\n", key, key, 397 (key & KEY_RELEASE) ? "released" : "pressed"); 398 399 #ifdef EVDEV_SUPPORT 400 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) 401 evdev_push_event(sc->sc_evdev, EV_KEY, 402 evdev_hid2key(KEY_INDEX(key)), !(key & KEY_RELEASE)); 403 if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev)) 404 return; 405 #endif 406 407 tail = (sc->sc_inputtail + 1) % HKBD_IN_BUF_SIZE; 408 if (tail != atomic_load_acq_32(&sc->sc_inputhead)) { 409 sc->sc_input[sc->sc_inputtail] = key; 410 atomic_store_rel_32(&sc->sc_inputtail, tail); 411 } else { 412 DPRINTF("input buffer is full\n"); 413 } 414 } 415 416 static void 417 hkbd_do_poll(struct hkbd_softc *sc, uint8_t wait) 418 { 419 420 SYSCONS_LOCK_ASSERT(); 421 KASSERT((sc->sc_flags & HKBD_FLAG_POLLING) != 0, 422 ("hkbd_do_poll called when not polling\n")); 423 DPRINTFN(2, "polling\n"); 424 425 if (!HID_IN_POLLING_MODE()) { 426 /* 427 * In this context the kernel is polling for input, 428 * but the USB subsystem works in normal interrupt-driven 429 * mode, so we just wait on the USB threads to do the job. 430 * Note that we currently hold the Giant, but it's also used 431 * as the transfer mtx, so we must release it while waiting. 432 */ 433 while (sc->sc_inputhead == 434 atomic_load_acq_32(&sc->sc_inputtail)) { 435 /* 436 * Give USB threads a chance to run. Note that 437 * kern_yield performs DROP_GIANT + PICKUP_GIANT. 438 */ 439 kern_yield(PRI_UNCHANGED); 440 if (!wait) 441 break; 442 } 443 return; 444 } 445 446 while (sc->sc_inputhead == sc->sc_inputtail) { 447 hid_intr_poll(sc->sc_dev); 448 449 /* Delay-optimised support for repetition of keys */ 450 if (hkbd_any_key_pressed(sc)) { 451 /* a key is pressed - need timekeeping */ 452 DELAY(1000); 453 454 /* 1 millisecond has passed */ 455 sc->sc_time_ms += 1; 456 } 457 458 hkbd_interrupt(sc); 459 460 if (!wait) 461 break; 462 } 463 } 464 465 static int32_t 466 hkbd_get_key(struct hkbd_softc *sc, uint8_t wait) 467 { 468 uint32_t head; 469 int32_t c; 470 471 SYSCONS_LOCK_ASSERT(); 472 KASSERT(!HID_IN_POLLING_MODE() || 473 (sc->sc_flags & HKBD_FLAG_POLLING) != 0, 474 ("not polling in kdb or panic\n")); 475 476 if (sc->sc_flags & HKBD_FLAG_POLLING) 477 hkbd_do_poll(sc, wait); 478 479 head = sc->sc_inputhead; 480 if (head == atomic_load_acq_32(&sc->sc_inputtail)) { 481 c = -1; 482 } else { 483 c = sc->sc_input[head]; 484 head = (head + 1) % HKBD_IN_BUF_SIZE; 485 atomic_store_rel_32(&sc->sc_inputhead, head); 486 } 487 return (c); 488 } 489 490 static void 491 hkbd_interrupt(struct hkbd_softc *sc) 492 { 493 const uint32_t now = sc->sc_time_ms; 494 unsigned key; 495 496 HKBD_LOCK_ASSERT(sc); 497 498 /* 499 * Check for key changes, the order is: 500 * 1. Regular keys up 501 * 2. Modifier keys up 502 * 3. Modifier keys down 503 * 4. Regular keys down 504 * 505 * This allows devices which send events changing the state of 506 * both a modifier key and a regular key, to be correctly 507 * translated. */ 508 bit_foreach(sc->sc_odata, HKBD_NKEYCODE, key) { 509 if (hkbd_is_modifier_key(key) || bit_test(sc->sc_ndata, key)) 510 continue; 511 hkbd_put_key(sc, key | KEY_RELEASE); 512 513 /* clear repeating key, if any */ 514 if (sc->sc_repeat_key == key) 515 sc->sc_repeat_key = 0; 516 } 517 bit_foreach_at(sc->sc_odata, MOD_MIN, MOD_MAX + 1, key) 518 if (!bit_test(sc->sc_ndata, key)) 519 hkbd_put_key(sc, key | KEY_RELEASE); 520 bit_foreach_at(sc->sc_ndata, MOD_MIN, MOD_MAX + 1, key) 521 if (!bit_test(sc->sc_odata, key)) 522 hkbd_put_key(sc, key | KEY_PRESS); 523 bit_foreach(sc->sc_ndata, HKBD_NKEYCODE, key) { 524 if (hkbd_is_modifier_key(key) || bit_test(sc->sc_odata, key)) 525 continue; 526 hkbd_put_key(sc, key | KEY_PRESS); 527 528 if (key != APPLE_FN_KEY) { 529 sc->sc_co_basetime = sbinuptime(); 530 sc->sc_delay = sc->sc_kbd.kb_delay1; 531 hkbd_start_timer(sc); 532 /* set repeat time for last key */ 533 sc->sc_repeat_time = now + sc->sc_kbd.kb_delay1; 534 sc->sc_repeat_key = key; 535 } 536 } 537 538 /* synchronize old data with new data */ 539 memcpy(sc->sc_odata0, sc->sc_ndata0, bitstr_size(HKBD_NKEYCODE)); 540 memcpy(sc->sc_odata, sc->sc_ndata, bitstr_size(HKBD_NKEYCODE)); 541 542 /* check if last key is still pressed */ 543 if (sc->sc_repeat_key != 0) { 544 const int32_t dtime = (sc->sc_repeat_time - now); 545 546 /* check if time has elapsed */ 547 if (dtime <= 0) { 548 hkbd_put_key(sc, sc->sc_repeat_key | KEY_PRESS); 549 sc->sc_repeat_time = now + sc->sc_kbd.kb_delay2; 550 } 551 } 552 553 #ifdef EVDEV_SUPPORT 554 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) 555 evdev_sync(sc->sc_evdev); 556 if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev)) 557 return; 558 #endif 559 560 /* wakeup keyboard system */ 561 if (!HID_IN_POLLING_MODE()) 562 taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task); 563 } 564 565 static void 566 hkbd_event_keyinput(void *context, int pending) 567 { 568 struct hkbd_softc *sc = context; 569 int c; 570 571 SYSCONS_LOCK_ASSERT(); 572 573 if ((sc->sc_flags & HKBD_FLAG_POLLING) != 0) 574 return; 575 576 if (sc->sc_inputhead == atomic_load_acq_32(&sc->sc_inputtail)) 577 return; 578 579 if (KBD_IS_ACTIVE(&sc->sc_kbd) && 580 KBD_IS_BUSY(&sc->sc_kbd)) { 581 /* let the callback function process the input */ 582 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, 583 sc->sc_kbd.kb_callback.kc_arg); 584 } else { 585 /* read and discard the input, no one is waiting for it */ 586 do { 587 c = hkbd_read_char(&sc->sc_kbd, 0); 588 } while (c != NOKEY); 589 } 590 } 591 592 static void 593 hkbd_timeout(void *arg) 594 { 595 struct hkbd_softc *sc = arg; 596 #ifdef EVDEV_SUPPORT 597 struct epoch_tracker et; 598 #endif 599 600 HKBD_LOCK_ASSERT(sc); 601 602 sc->sc_time_ms += sc->sc_delay; 603 sc->sc_delay = 0; 604 605 #ifdef EVDEV_SUPPORT 606 epoch_enter_preempt(INPUT_EPOCH, &et); 607 #endif 608 hkbd_interrupt(sc); 609 #ifdef EVDEV_SUPPORT 610 epoch_exit_preempt(INPUT_EPOCH, &et); 611 #endif 612 613 /* Make sure any leftover key events gets read out */ 614 taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task); 615 616 if (hkbd_any_key_pressed(sc) || 617 atomic_load_acq_32(&sc->sc_inputhead) != sc->sc_inputtail) { 618 hkbd_start_timer(sc); 619 } 620 } 621 622 static uint32_t 623 hkbd_apple_fn(uint32_t keycode) 624 { 625 switch (keycode) { 626 case 0x0b: return 0x50; /* H -> LEFT ARROW */ 627 case 0x0d: return 0x51; /* J -> DOWN ARROW */ 628 case 0x0e: return 0x52; /* K -> UP ARROW */ 629 case 0x0f: return 0x4f; /* L -> RIGHT ARROW */ 630 case 0x36: return 0x4a; /* COMMA -> HOME */ 631 case 0x37: return 0x4d; /* DOT -> END */ 632 case 0x18: return 0x4b; /* U -> PGUP */ 633 case 0x07: return 0x4e; /* D -> PGDN */ 634 case 0x16: return 0x47; /* S -> SCROLLLOCK */ 635 case 0x13: return 0x46; /* P -> SYSRQ/PRTSC */ 636 case 0x28: return 0x49; /* RETURN -> INSERT */ 637 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */ 638 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */ 639 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */ 640 case 0x52: return 0x4b; /* UP ARROW -> PGUP */ 641 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */ 642 default: return keycode; 643 } 644 } 645 646 /* separate so the sysctl doesn't butcher non-fn keys */ 647 static uint32_t 648 hkbd_apple_fn_media(uint32_t keycode) 649 { 650 switch (keycode) { 651 case 0x3a: return 0xc0; /* F1 -> BRIGHTNESS DOWN */ 652 case 0x3b: return 0xc1; /* F2 -> BRIGHTNESS UP */ 653 case 0x3c: return 0xc2; /* F3 -> SCALE (MISSION CTRL)*/ 654 case 0x3d: return 0xc3; /* F4 -> DASHBOARD (LAUNCHPAD) */ 655 case 0x3e: return 0xc4; /* F5 -> KBD BACKLIGHT DOWN */ 656 case 0x3f: return 0xc5; /* F6 -> KBD BACKLIGHT UP */ 657 case 0x40: return 0xea; /* F7 -> MEDIA PREV */ 658 case 0x41: return 0xe8; /* F8 -> PLAY/PAUSE */ 659 case 0x42: return 0xeb; /* F9 -> MEDIA NEXT */ 660 case 0x43: return 0xef; /* F10 -> MUTE */ 661 case 0x44: return 0xee; /* F11 -> VOLUME DOWN */ 662 case 0x45: return 0xed; /* F12 -> VOLUME UP */ 663 default: return keycode; 664 } 665 } 666 667 static uint32_t 668 hkbd_apple_swap(uint32_t keycode) 669 { 670 switch (keycode) { 671 case 0x35: return 0x64; 672 case 0x64: return 0x35; 673 default: return keycode; 674 } 675 } 676 677 static void 678 hkbd_intr_callback(void *context, void *data, hid_size_t len) 679 { 680 struct hkbd_softc *sc = context; 681 uint8_t *buf = data; 682 uint32_t i; 683 uint8_t id = 0; 684 uint8_t modifiers; 685 686 HKBD_LOCK_ASSERT(sc); 687 688 DPRINTF("actlen=%d bytes\n", len); 689 690 if (len == 0) { 691 DPRINTF("zero length data\n"); 692 return; 693 } 694 695 if (sc->sc_kbd_id != 0) { 696 /* check and remove HID ID byte */ 697 id = buf[0]; 698 buf++; 699 len--; 700 if (len == 0) { 701 DPRINTF("zero length data\n"); 702 return; 703 } 704 } 705 706 /* clear temporary storage */ 707 if (bit_test(sc->sc_loc_key_valid, 0) && id == sc->sc_id_loc_key[0]) { 708 bit_foreach(sc->sc_ndata0, HKBD_NKEYCODE, i) 709 bit_clear(sc->sc_ndata, i); 710 memset(&sc->sc_ndata0, 0, bitstr_size(HKBD_NKEYCODE)); 711 } 712 bit_foreach(sc->sc_ndata, HKBD_NKEYCODE, i) 713 if (id == sc->sc_id_loc_key[i]) 714 bit_clear(sc->sc_ndata, i); 715 716 /* clear modifiers */ 717 modifiers = 0; 718 719 /* scan through HID data and expose magic apple keys */ 720 if ((sc->sc_flags & HKBD_FLAG_APPLE_EJECT) && 721 (id == sc->sc_id_apple_eject)) { 722 if (hid_get_data(buf, len, &sc->sc_loc_apple_eject)) { 723 bit_set(sc->sc_ndata, APPLE_EJECT_KEY); 724 modifiers |= MOD_EJECT; 725 } else { 726 bit_clear(sc->sc_ndata, APPLE_EJECT_KEY); 727 } 728 } 729 if ((sc->sc_flags & HKBD_FLAG_APPLE_FN) && 730 (id == sc->sc_id_apple_fn)) { 731 if (hid_get_data(buf, len, &sc->sc_loc_apple_fn)) { 732 bit_set(sc->sc_ndata, APPLE_FN_KEY); 733 modifiers |= MOD_FN; 734 } else { 735 bit_clear(sc->sc_ndata, APPLE_FN_KEY); 736 } 737 } 738 739 int apply_apple_fn_media = (modifiers & MOD_FN) ? 1 : 0; 740 if (hkbd_apple_fn_mode) /* toggle from sysctl value */ 741 apply_apple_fn_media = !apply_apple_fn_media; 742 743 bit_foreach(sc->sc_loc_key_valid, HKBD_NKEYCODE, i) { 744 if (id != sc->sc_id_loc_key[i]) { 745 continue; /* invalid HID ID */ 746 } else if (i == 0) { 747 struct hid_location tmp_loc = sc->sc_loc_key[0]; 748 /* range check array size */ 749 if (tmp_loc.count > HKBD_NKEYCODE) 750 tmp_loc.count = HKBD_NKEYCODE; 751 while (tmp_loc.count--) { 752 uint32_t key = 753 hid_get_udata(buf, len, &tmp_loc); 754 /* advance to next location */ 755 tmp_loc.pos += tmp_loc.size; 756 if (key == KEY_ERROR) { 757 DPRINTF("KEY_ERROR\n"); 758 memcpy(sc->sc_ndata0, sc->sc_odata0, 759 bitstr_size(HKBD_NKEYCODE)); 760 memcpy(sc->sc_ndata, sc->sc_odata, 761 bitstr_size(HKBD_NKEYCODE)); 762 return; /* ignore */ 763 } 764 if (modifiers & MOD_FN) 765 key = hkbd_apple_fn(key); 766 if (apply_apple_fn_media) 767 key = hkbd_apple_fn_media(key); 768 if (sc->sc_flags & HKBD_FLAG_APPLE_SWAP) 769 key = hkbd_apple_swap(key); 770 if (key == KEY_NONE || key >= HKBD_NKEYCODE) 771 continue; 772 /* set key in bitmap */ 773 bit_set(sc->sc_ndata, key); 774 bit_set(sc->sc_ndata0, key); 775 } 776 } else if (hid_get_data(buf, len, &sc->sc_loc_key[i])) { 777 uint32_t key = i; 778 779 if (modifiers & MOD_FN) 780 key = hkbd_apple_fn(key); 781 if (apply_apple_fn_media) 782 key = hkbd_apple_fn_media(key); 783 if (sc->sc_flags & HKBD_FLAG_APPLE_SWAP) 784 key = hkbd_apple_swap(key); 785 if (key == KEY_NONE || key == KEY_ERROR || key >= HKBD_NKEYCODE) 786 continue; 787 /* set key in bitmap */ 788 bit_set(sc->sc_ndata, key); 789 } 790 } 791 #ifdef HID_DEBUG 792 DPRINTF("modifiers = 0x%04x\n", modifiers); 793 bit_foreach(sc->sc_ndata, HKBD_NKEYCODE, i) 794 DPRINTF("Key 0x%02x pressed\n", i); 795 #endif 796 hkbd_interrupt(sc); 797 } 798 799 /* A match on these entries will load ukbd */ 800 static const struct hid_device_id __used hkbd_devs[] = { 801 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_KEYBOARD) }, 802 }; 803 804 static int 805 hkbd_probe(device_t dev) 806 { 807 keyboard_switch_t *sw = kbd_get_switch(HKBD_DRIVER_NAME); 808 int error; 809 810 DPRINTFN(11, "\n"); 811 812 if (sw == NULL) { 813 return (ENXIO); 814 } 815 816 error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hkbd_devs); 817 if (error != 0) 818 return (error); 819 820 hidbus_set_desc(dev, "Keyboard"); 821 822 return (BUS_PROBE_DEFAULT); 823 } 824 825 static void 826 hkbd_parse_hid(struct hkbd_softc *sc, const uint8_t *ptr, uint32_t len, 827 uint8_t tlc_index) 828 { 829 uint32_t flags; 830 uint32_t key; 831 uint8_t id; 832 833 /* reset detected bits */ 834 sc->sc_flags &= ~HKBD_FLAG_HID_MASK; 835 836 /* reset detected keys */ 837 memset(sc->sc_loc_key_valid, 0, bitstr_size(HKBD_NKEYCODE)); 838 839 /* check if there is an ID byte */ 840 sc->sc_kbd_size = hid_report_size_max(ptr, len, 841 hid_input, &sc->sc_kbd_id); 842 843 const struct hid_device_info *hw = hid_get_device_info(sc->sc_dev); 844 845 /* investigate if this is an Apple Keyboard */ 846 if (hw->idVendor == USB_VENDOR_APPLE) { /* belt & braces! */ 847 if (hidbus_locate(ptr, len, 848 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT), 849 hid_input, tlc_index, 0, &sc->sc_loc_apple_eject, &flags, 850 &sc->sc_id_apple_eject, NULL)) { 851 if (flags & HIO_VARIABLE) 852 sc->sc_flags |= HKBD_FLAG_APPLE_EJECT | 853 HKBD_FLAG_APPLE_SWAP; 854 DPRINTFN(1, "Found Apple eject-key\n"); 855 } 856 /* 857 * check the same vendor pages that linux does to find the one 858 * apple uses for the function key. 859 */ 860 static const uint16_t apple_pages[] = { 861 HUP_APPLE, /* HID_UP_CUSTOM in linux */ 862 HUP_MICROSOFT, /* HID_UP_MSVENDOR in linux */ 863 HUP_HP, /* HID_UP_HPVENDOR2 in linux */ 864 0xFFFF /* Original FreeBSD check (Remove?) */ 865 }; 866 for (int i = 0; i < (int)nitems(apple_pages); i++) { 867 if (hidbus_locate(ptr, len, 868 HID_USAGE2(apple_pages[i], 0x0003), 869 hid_input, tlc_index, 0, &sc->sc_loc_apple_fn, &flags, 870 &sc->sc_id_apple_fn, NULL)) { 871 if (flags & HIO_VARIABLE) 872 sc->sc_flags |= HKBD_FLAG_APPLE_FN; 873 DPRINTFN(1, "Found Apple FN-key on page 0x%04x\n", 874 apple_pages[i]); 875 break; 876 } 877 } 878 } 879 880 /* figure out event buffer */ 881 if (hidbus_locate(ptr, len, 882 HID_USAGE2(HUP_KEYBOARD, 0x00), 883 hid_input, tlc_index, 0, &sc->sc_loc_key[0], &flags, 884 &sc->sc_id_loc_key[0], NULL)) { 885 if (flags & HIO_VARIABLE) { 886 DPRINTFN(1, "Ignoring keyboard event control\n"); 887 } else { 888 bit_set(sc->sc_loc_key_valid, 0); 889 DPRINTFN(1, "Found keyboard event array\n"); 890 } 891 } 892 893 /* figure out the keys */ 894 for (key = 1; key != HKBD_NKEYCODE; key++) { 895 if (hidbus_locate(ptr, len, 896 HID_USAGE2(HUP_KEYBOARD, key), 897 hid_input, tlc_index, 0, &sc->sc_loc_key[key], &flags, 898 &sc->sc_id_loc_key[key], NULL)) { 899 if (flags & HIO_VARIABLE) { 900 bit_set(sc->sc_loc_key_valid, key); 901 DPRINTFN(1, "Found key 0x%02x\n", key); 902 } 903 } 904 } 905 906 /* figure out leds on keyboard */ 907 if (hidbus_locate(ptr, len, 908 HID_USAGE2(HUP_LEDS, 0x01), 909 hid_output, tlc_index, 0, &sc->sc_loc_numlock, &flags, 910 &sc->sc_id_leds, NULL)) { 911 if (flags & HIO_VARIABLE) 912 sc->sc_flags |= HKBD_FLAG_NUMLOCK; 913 DPRINTFN(1, "Found keyboard numlock\n"); 914 } 915 if (hidbus_locate(ptr, len, 916 HID_USAGE2(HUP_LEDS, 0x02), 917 hid_output, tlc_index, 0, &sc->sc_loc_capslock, &flags, 918 &id, NULL)) { 919 if ((sc->sc_flags & HKBD_FLAG_NUMLOCK) == 0) 920 sc->sc_id_leds = id; 921 if (flags & HIO_VARIABLE && sc->sc_id_leds == id) 922 sc->sc_flags |= HKBD_FLAG_CAPSLOCK; 923 DPRINTFN(1, "Found keyboard capslock\n"); 924 } 925 if (hidbus_locate(ptr, len, 926 HID_USAGE2(HUP_LEDS, 0x03), 927 hid_output, tlc_index, 0, &sc->sc_loc_scrolllock, &flags, 928 &id, NULL)) { 929 if ((sc->sc_flags & (HKBD_FLAG_NUMLOCK | HKBD_FLAG_CAPSLOCK)) 930 == 0) 931 sc->sc_id_leds = id; 932 if (flags & HIO_VARIABLE && sc->sc_id_leds == id) 933 sc->sc_flags |= HKBD_FLAG_SCROLLLOCK; 934 DPRINTFN(1, "Found keyboard scrolllock\n"); 935 } 936 937 if ((sc->sc_flags & (HKBD_FLAG_NUMLOCK | HKBD_FLAG_CAPSLOCK | 938 HKBD_FLAG_SCROLLLOCK)) != 0) 939 sc->sc_led_size = hid_report_size(ptr, len, 940 hid_output, sc->sc_id_leds); 941 } 942 943 static int 944 hkbd_attach(device_t dev) 945 { 946 struct hkbd_softc *sc = device_get_softc(dev); 947 const struct hid_device_info *hw = hid_get_device_info(dev); 948 int unit = device_get_unit(dev); 949 keyboard_t *kbd = &sc->sc_kbd; 950 void *hid_ptr = NULL; 951 int err; 952 uint16_t n; 953 hid_size_t hid_len; 954 uint8_t tlc_index = hidbus_get_index(dev); 955 #ifdef EVDEV_SUPPORT 956 struct evdev_dev *evdev; 957 int i; 958 #endif 959 960 sc->sc_dev = dev; 961 SYSCONS_LOCK_ASSERT(); 962 963 kbd_init_struct(kbd, HKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); 964 965 kbd->kb_data = (void *)sc; 966 967 sc->sc_mode = K_XLATE; 968 969 mtx_init(&sc->sc_mtx, "hkbd lock", NULL, MTX_DEF); 970 TASK_INIT(&sc->sc_task, 0, hkbd_event_keyinput, sc); 971 callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0); 972 973 hidbus_set_intr(dev, hkbd_intr_callback, sc); 974 /* interrupt handler will be called with hkbd mutex taken */ 975 hidbus_set_lock(dev, &sc->sc_mtx); 976 /* interrupt handler can be called during panic */ 977 hidbus_set_flags(dev, hidbus_get_flags(dev) | HIDBUS_FLAG_CAN_POLL); 978 979 /* setup default keyboard maps */ 980 981 sc->sc_keymap = key_map; 982 sc->sc_accmap = accent_map; 983 for (n = 0; n < HKBD_NFKEY; n++) { 984 sc->sc_fkeymap[n] = fkey_tab[n]; 985 } 986 987 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, 988 sc->sc_fkeymap, HKBD_NFKEY); 989 990 KBD_FOUND_DEVICE(kbd); 991 992 hkbd_clear_state(kbd); 993 994 /* 995 * FIXME: set the initial value for lock keys in "sc_state" 996 * according to the BIOS data? 997 */ 998 KBD_PROBE_DONE(kbd); 999 1000 /* get HID descriptor */ 1001 err = hid_get_report_descr(dev, &hid_ptr, &hid_len); 1002 1003 if (err == 0) { 1004 DPRINTF("Parsing HID descriptor of %d bytes\n", 1005 (int)hid_len); 1006 1007 hkbd_parse_hid(sc, hid_ptr, hid_len, tlc_index); 1008 } 1009 1010 /* check if we should use the boot protocol */ 1011 if (hid_test_quirk(hw, HQ_KBD_BOOTPROTO) || 1012 (err != 0) || hkbd_any_key_valid(sc) == false) { 1013 DPRINTF("Forcing boot protocol\n"); 1014 1015 err = hid_set_protocol(dev, 0); 1016 1017 if (err != 0) { 1018 DPRINTF("Set protocol error=%d (ignored)\n", err); 1019 } 1020 1021 hkbd_parse_hid(sc, hkbd_boot_desc, sizeof(hkbd_boot_desc), 0); 1022 } 1023 1024 /* ignore if SETIDLE fails, hence it is not crucial */ 1025 hid_set_idle(dev, 0, 0); 1026 1027 hkbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state); 1028 1029 KBD_INIT_DONE(kbd); 1030 1031 if (kbd_register(kbd) < 0) { 1032 goto detach; 1033 } 1034 KBD_CONFIG_DONE(kbd); 1035 1036 hkbd_enable(kbd); 1037 1038 #ifdef KBD_INSTALL_CDEV 1039 if (kbd_attach(kbd)) { 1040 goto detach; 1041 } 1042 #endif 1043 1044 #ifdef EVDEV_SUPPORT 1045 evdev = evdev_alloc(); 1046 evdev_set_name(evdev, device_get_desc(dev)); 1047 evdev_set_phys(evdev, device_get_nameunit(dev)); 1048 evdev_set_id(evdev, hw->idBus, hw->idVendor, hw->idProduct, 1049 hw->idVersion); 1050 evdev_set_serial(evdev, hw->serial); 1051 evdev_set_methods(evdev, kbd, &hkbd_evdev_methods); 1052 evdev_set_flag(evdev, EVDEV_FLAG_EXT_EPOCH); /* hidbus child */ 1053 evdev_support_event(evdev, EV_SYN); 1054 evdev_support_event(evdev, EV_KEY); 1055 if (sc->sc_flags & (HKBD_FLAG_NUMLOCK | HKBD_FLAG_CAPSLOCK | 1056 HKBD_FLAG_SCROLLLOCK)) 1057 evdev_support_event(evdev, EV_LED); 1058 evdev_support_event(evdev, EV_REP); 1059 1060 for (i = 0x00; i <= 0xFF; i++) 1061 evdev_support_key(evdev, evdev_hid2key(i)); 1062 if (sc->sc_flags & HKBD_FLAG_NUMLOCK) 1063 evdev_support_led(evdev, LED_NUML); 1064 if (sc->sc_flags & HKBD_FLAG_CAPSLOCK) 1065 evdev_support_led(evdev, LED_CAPSL); 1066 if (sc->sc_flags & HKBD_FLAG_SCROLLLOCK) 1067 evdev_support_led(evdev, LED_SCROLLL); 1068 1069 if (evdev_register(evdev)) 1070 evdev_free(evdev); 1071 else 1072 sc->sc_evdev = evdev; 1073 #endif 1074 1075 sc->sc_flags |= HKBD_FLAG_ATTACHED; 1076 1077 if (bootverbose) { 1078 kbdd_diag(kbd, bootverbose); 1079 } 1080 1081 /* start the keyboard */ 1082 hid_intr_start(dev); 1083 1084 return (0); /* success */ 1085 1086 detach: 1087 hkbd_detach(dev); 1088 return (ENXIO); /* error */ 1089 } 1090 1091 static int 1092 hkbd_detach(device_t dev) 1093 { 1094 struct hkbd_softc *sc = device_get_softc(dev); 1095 #ifdef EVDEV_SUPPORT 1096 struct epoch_tracker et; 1097 #endif 1098 int error; 1099 1100 SYSCONS_LOCK_ASSERT(); 1101 1102 DPRINTF("\n"); 1103 1104 sc->sc_flags |= HKBD_FLAG_GONE; 1105 1106 HKBD_LOCK(sc); 1107 callout_stop(&sc->sc_callout); 1108 HKBD_UNLOCK(sc); 1109 1110 /* kill any stuck keys */ 1111 if (sc->sc_flags & HKBD_FLAG_ATTACHED) { 1112 /* stop receiving events from the USB keyboard */ 1113 hid_intr_stop(dev); 1114 1115 /* release all leftover keys, if any */ 1116 memset(&sc->sc_ndata, 0, bitstr_size(HKBD_NKEYCODE)); 1117 1118 /* process releasing of all keys */ 1119 HKBD_LOCK(sc); 1120 #ifdef EVDEV_SUPPORT 1121 epoch_enter_preempt(INPUT_EPOCH, &et); 1122 #endif 1123 hkbd_interrupt(sc); 1124 #ifdef EVDEV_SUPPORT 1125 epoch_exit_preempt(INPUT_EPOCH, &et); 1126 #endif 1127 HKBD_UNLOCK(sc); 1128 taskqueue_drain(taskqueue_swi_giant, &sc->sc_task); 1129 } 1130 1131 mtx_destroy(&sc->sc_mtx); 1132 hkbd_disable(&sc->sc_kbd); 1133 1134 #ifdef KBD_INSTALL_CDEV 1135 if (sc->sc_flags & HKBD_FLAG_ATTACHED) { 1136 error = kbd_detach(&sc->sc_kbd); 1137 if (error) { 1138 /* usb attach cannot return an error */ 1139 device_printf(dev, "WARNING: kbd_detach() " 1140 "returned non-zero! (ignored)\n"); 1141 } 1142 } 1143 #endif 1144 1145 #ifdef EVDEV_SUPPORT 1146 evdev_free(sc->sc_evdev); 1147 #endif 1148 1149 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { 1150 error = kbd_unregister(&sc->sc_kbd); 1151 if (error) { 1152 /* usb attach cannot return an error */ 1153 device_printf(dev, "WARNING: kbd_unregister() " 1154 "returned non-zero! (ignored)\n"); 1155 } 1156 } 1157 sc->sc_kbd.kb_flags = 0; 1158 1159 DPRINTF("%s: disconnected\n", 1160 device_get_nameunit(dev)); 1161 1162 return (0); 1163 } 1164 1165 static int 1166 hkbd_resume(device_t dev) 1167 { 1168 struct hkbd_softc *sc = device_get_softc(dev); 1169 1170 SYSCONS_LOCK_ASSERT(); 1171 1172 hkbd_clear_state(&sc->sc_kbd); 1173 1174 return (0); 1175 } 1176 1177 #ifdef EVDEV_SUPPORT 1178 static void 1179 hkbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 1180 int32_t value) 1181 { 1182 keyboard_t *kbd = evdev_get_softc(evdev); 1183 1184 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && 1185 (type == EV_LED || type == EV_REP)) { 1186 mtx_lock(&Giant); 1187 kbd_ev_event(kbd, type, code, value); 1188 mtx_unlock(&Giant); 1189 } 1190 } 1191 #endif 1192 1193 /* early keyboard probe, not supported */ 1194 static int 1195 hkbd_configure(int flags) 1196 { 1197 return (0); 1198 } 1199 1200 /* detect a keyboard, not used */ 1201 static int 1202 hkbd__probe(int unit, void *arg, int flags) 1203 { 1204 return (ENXIO); 1205 } 1206 1207 /* reset and initialize the device, not used */ 1208 static int 1209 hkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 1210 { 1211 return (ENXIO); 1212 } 1213 1214 /* test the interface to the device, not used */ 1215 static int 1216 hkbd_test_if(keyboard_t *kbd) 1217 { 1218 return (0); 1219 } 1220 1221 /* finish using this keyboard, not used */ 1222 static int 1223 hkbd_term(keyboard_t *kbd) 1224 { 1225 return (ENXIO); 1226 } 1227 1228 /* keyboard interrupt routine, not used */ 1229 static int 1230 hkbd_intr(keyboard_t *kbd, void *arg) 1231 { 1232 return (0); 1233 } 1234 1235 /* lock the access to the keyboard, not used */ 1236 static int 1237 hkbd_lock(keyboard_t *kbd, int lock) 1238 { 1239 return (1); 1240 } 1241 1242 /* 1243 * Enable the access to the device; until this function is called, 1244 * the client cannot read from the keyboard. 1245 */ 1246 static int 1247 hkbd_enable(keyboard_t *kbd) 1248 { 1249 1250 SYSCONS_LOCK(); 1251 KBD_ACTIVATE(kbd); 1252 SYSCONS_UNLOCK(); 1253 1254 return (0); 1255 } 1256 1257 /* disallow the access to the device */ 1258 static int 1259 hkbd_disable(keyboard_t *kbd) 1260 { 1261 1262 SYSCONS_LOCK(); 1263 KBD_DEACTIVATE(kbd); 1264 SYSCONS_UNLOCK(); 1265 1266 return (0); 1267 } 1268 1269 /* check if data is waiting */ 1270 /* Currently unused. */ 1271 static int 1272 hkbd_check(keyboard_t *kbd) 1273 { 1274 struct hkbd_softc *sc = kbd->kb_data; 1275 1276 SYSCONS_LOCK_ASSERT(); 1277 1278 if (!KBD_IS_ACTIVE(kbd)) 1279 return (0); 1280 1281 if (sc->sc_flags & HKBD_FLAG_POLLING) 1282 hkbd_do_poll(sc, 0); 1283 1284 #ifdef HKBD_EMULATE_ATSCANCODE 1285 if (sc->sc_buffered_char[0]) { 1286 return (1); 1287 } 1288 #endif 1289 if (sc->sc_inputhead != atomic_load_acq_32(&sc->sc_inputtail)) { 1290 return (1); 1291 } 1292 return (0); 1293 } 1294 1295 /* check if char is waiting */ 1296 static int 1297 hkbd_check_char_locked(keyboard_t *kbd) 1298 { 1299 struct hkbd_softc *sc = kbd->kb_data; 1300 1301 SYSCONS_LOCK_ASSERT(); 1302 1303 if (!KBD_IS_ACTIVE(kbd)) 1304 return (0); 1305 1306 if ((sc->sc_composed_char > 0) && 1307 (!(sc->sc_flags & HKBD_FLAG_COMPOSE))) { 1308 return (1); 1309 } 1310 return (hkbd_check(kbd)); 1311 } 1312 1313 static int 1314 hkbd_check_char(keyboard_t *kbd) 1315 { 1316 int result; 1317 1318 SYSCONS_LOCK(); 1319 result = hkbd_check_char_locked(kbd); 1320 SYSCONS_UNLOCK(); 1321 1322 return (result); 1323 } 1324 1325 /* read one byte from the keyboard if it's allowed */ 1326 /* Currently unused. */ 1327 static int 1328 hkbd_read(keyboard_t *kbd, int wait) 1329 { 1330 struct hkbd_softc *sc = kbd->kb_data; 1331 int32_t usbcode; 1332 #ifdef HKBD_EMULATE_ATSCANCODE 1333 uint32_t keycode; 1334 uint32_t scancode; 1335 1336 #endif 1337 1338 SYSCONS_LOCK_ASSERT(); 1339 1340 if (!KBD_IS_ACTIVE(kbd)) 1341 return (-1); 1342 1343 #ifdef HKBD_EMULATE_ATSCANCODE 1344 if (sc->sc_buffered_char[0]) { 1345 scancode = sc->sc_buffered_char[0]; 1346 if (scancode & SCAN_PREFIX) { 1347 sc->sc_buffered_char[0] &= ~SCAN_PREFIX; 1348 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1349 } 1350 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1351 sc->sc_buffered_char[1] = 0; 1352 return (scancode); 1353 } 1354 #endif /* HKBD_EMULATE_ATSCANCODE */ 1355 1356 /* XXX */ 1357 usbcode = hkbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1358 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) 1359 return (-1); 1360 1361 ++(kbd->kb_count); 1362 1363 #ifdef HKBD_EMULATE_ATSCANCODE 1364 keycode = hkbd_atkeycode(usbcode, sc->sc_ndata); 1365 if (keycode == NN) { 1366 return -1; 1367 } 1368 return (hkbd_key2scan(sc, keycode, sc->sc_ndata, 1369 (usbcode & KEY_RELEASE))); 1370 #else /* !HKBD_EMULATE_ATSCANCODE */ 1371 return (usbcode); 1372 #endif /* HKBD_EMULATE_ATSCANCODE */ 1373 } 1374 1375 /* read char from the keyboard */ 1376 static uint32_t 1377 hkbd_read_char_locked(keyboard_t *kbd, int wait) 1378 { 1379 struct hkbd_softc *sc = kbd->kb_data; 1380 uint32_t action; 1381 uint32_t keycode; 1382 int32_t usbcode; 1383 #ifdef HKBD_EMULATE_ATSCANCODE 1384 uint32_t scancode; 1385 #endif 1386 1387 SYSCONS_LOCK_ASSERT(); 1388 1389 if (!KBD_IS_ACTIVE(kbd)) 1390 return (NOKEY); 1391 1392 next_code: 1393 1394 /* do we have a composed char to return ? */ 1395 1396 if ((sc->sc_composed_char > 0) && 1397 (!(sc->sc_flags & HKBD_FLAG_COMPOSE))) { 1398 action = sc->sc_composed_char; 1399 sc->sc_composed_char = 0; 1400 1401 if (action > 0xFF) { 1402 goto errkey; 1403 } 1404 goto done; 1405 } 1406 #ifdef HKBD_EMULATE_ATSCANCODE 1407 1408 /* do we have a pending raw scan code? */ 1409 1410 if (sc->sc_mode == K_RAW) { 1411 scancode = sc->sc_buffered_char[0]; 1412 if (scancode) { 1413 if (scancode & SCAN_PREFIX) { 1414 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX); 1415 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 1416 } 1417 sc->sc_buffered_char[0] = sc->sc_buffered_char[1]; 1418 sc->sc_buffered_char[1] = 0; 1419 return (scancode); 1420 } 1421 } 1422 #endif /* HKBD_EMULATE_ATSCANCODE */ 1423 1424 /* see if there is something in the keyboard port */ 1425 /* XXX */ 1426 usbcode = hkbd_get_key(sc, (wait == FALSE) ? 0 : 1); 1427 if (usbcode == -1) { 1428 return (NOKEY); 1429 } 1430 ++kbd->kb_count; 1431 1432 #ifdef HKBD_EMULATE_ATSCANCODE 1433 /* USB key index -> key code -> AT scan code */ 1434 keycode = hkbd_atkeycode(usbcode, sc->sc_ndata); 1435 if (keycode == NN) { 1436 return (NOKEY); 1437 } 1438 /* return an AT scan code for the K_RAW mode */ 1439 if (sc->sc_mode == K_RAW) { 1440 return (hkbd_key2scan(sc, keycode, sc->sc_ndata, 1441 (usbcode & KEY_RELEASE))); 1442 } 1443 #else /* !HKBD_EMULATE_ATSCANCODE */ 1444 1445 /* return the byte as is for the K_RAW mode */ 1446 if (sc->sc_mode == K_RAW) { 1447 return (usbcode); 1448 } 1449 /* USB key index -> key code */ 1450 keycode = hkbd_trtab[KEY_INDEX(usbcode)]; 1451 if (keycode == NN) { 1452 return (NOKEY); 1453 } 1454 #endif /* HKBD_EMULATE_ATSCANCODE */ 1455 1456 switch (keycode) { 1457 case 0x38: /* left alt (compose key) */ 1458 if (usbcode & KEY_RELEASE) { 1459 if (sc->sc_flags & HKBD_FLAG_COMPOSE) { 1460 sc->sc_flags &= ~HKBD_FLAG_COMPOSE; 1461 1462 if (sc->sc_composed_char > 0xFF) { 1463 sc->sc_composed_char = 0; 1464 } 1465 } 1466 } else { 1467 if (!(sc->sc_flags & HKBD_FLAG_COMPOSE)) { 1468 sc->sc_flags |= HKBD_FLAG_COMPOSE; 1469 sc->sc_composed_char = 0; 1470 } 1471 } 1472 break; 1473 } 1474 1475 /* return the key code in the K_CODE mode */ 1476 if (usbcode & KEY_RELEASE) { 1477 keycode |= SCAN_RELEASE; 1478 } 1479 if (sc->sc_mode == K_CODE) { 1480 return (keycode); 1481 } 1482 /* compose a character code */ 1483 if (sc->sc_flags & HKBD_FLAG_COMPOSE) { 1484 switch (keycode) { 1485 /* key pressed, process it */ 1486 case 0x47: 1487 case 0x48: 1488 case 0x49: /* keypad 7,8,9 */ 1489 sc->sc_composed_char *= 10; 1490 sc->sc_composed_char += keycode - 0x40; 1491 goto check_composed; 1492 1493 case 0x4B: 1494 case 0x4C: 1495 case 0x4D: /* keypad 4,5,6 */ 1496 sc->sc_composed_char *= 10; 1497 sc->sc_composed_char += keycode - 0x47; 1498 goto check_composed; 1499 1500 case 0x4F: 1501 case 0x50: 1502 case 0x51: /* keypad 1,2,3 */ 1503 sc->sc_composed_char *= 10; 1504 sc->sc_composed_char += keycode - 0x4E; 1505 goto check_composed; 1506 1507 case 0x52: /* keypad 0 */ 1508 sc->sc_composed_char *= 10; 1509 goto check_composed; 1510 1511 /* key released, no interest here */ 1512 case SCAN_RELEASE | 0x47: 1513 case SCAN_RELEASE | 0x48: 1514 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */ 1515 case SCAN_RELEASE | 0x4B: 1516 case SCAN_RELEASE | 0x4C: 1517 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */ 1518 case SCAN_RELEASE | 0x4F: 1519 case SCAN_RELEASE | 0x50: 1520 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */ 1521 case SCAN_RELEASE | 0x52: /* keypad 0 */ 1522 goto next_code; 1523 1524 case 0x38: /* left alt key */ 1525 break; 1526 1527 default: 1528 if (sc->sc_composed_char > 0) { 1529 sc->sc_flags &= ~HKBD_FLAG_COMPOSE; 1530 sc->sc_composed_char = 0; 1531 goto errkey; 1532 } 1533 break; 1534 } 1535 } 1536 /* keycode to key action */ 1537 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), 1538 (keycode & SCAN_RELEASE), 1539 &sc->sc_state, &sc->sc_accents); 1540 if (action == NOKEY) { 1541 goto next_code; 1542 } 1543 done: 1544 return (action); 1545 1546 check_composed: 1547 if (sc->sc_composed_char <= 0xFF) { 1548 goto next_code; 1549 } 1550 errkey: 1551 return (ERRKEY); 1552 } 1553 1554 /* Currently wait is always false. */ 1555 static uint32_t 1556 hkbd_read_char(keyboard_t *kbd, int wait) 1557 { 1558 uint32_t keycode; 1559 1560 SYSCONS_LOCK(); 1561 keycode = hkbd_read_char_locked(kbd, wait); 1562 SYSCONS_UNLOCK(); 1563 1564 return (keycode); 1565 } 1566 1567 /* some useful control functions */ 1568 static int 1569 hkbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) 1570 { 1571 struct hkbd_softc *sc = kbd->kb_data; 1572 #ifdef EVDEV_SUPPORT 1573 struct epoch_tracker et; 1574 #endif 1575 int error; 1576 int i; 1577 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1578 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1579 int ival; 1580 1581 #endif 1582 1583 SYSCONS_LOCK_ASSERT(); 1584 1585 switch (cmd) { 1586 case KDGKBMODE: /* get keyboard mode */ 1587 *(int *)arg = sc->sc_mode; 1588 break; 1589 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1590 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1591 case _IO('K', 7): 1592 ival = IOCPARM_IVAL(arg); 1593 arg = (caddr_t)&ival; 1594 /* FALLTHROUGH */ 1595 #endif 1596 case KDSKBMODE: /* set keyboard mode */ 1597 switch (*(int *)arg) { 1598 case K_XLATE: 1599 if (sc->sc_mode != K_XLATE) { 1600 /* make lock key state and LED state match */ 1601 sc->sc_state &= ~LOCK_MASK; 1602 sc->sc_state |= KBD_LED_VAL(kbd); 1603 } 1604 /* FALLTHROUGH */ 1605 case K_RAW: 1606 case K_CODE: 1607 if (sc->sc_mode != *(int *)arg) { 1608 if ((sc->sc_flags & HKBD_FLAG_POLLING) == 0) 1609 hkbd_clear_state(kbd); 1610 sc->sc_mode = *(int *)arg; 1611 } 1612 break; 1613 default: 1614 return (EINVAL); 1615 } 1616 break; 1617 1618 case KDGETLED: /* get keyboard LED */ 1619 *(int *)arg = KBD_LED_VAL(kbd); 1620 break; 1621 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1622 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1623 case _IO('K', 66): 1624 ival = IOCPARM_IVAL(arg); 1625 arg = (caddr_t)&ival; 1626 /* FALLTHROUGH */ 1627 #endif 1628 case KDSETLED: /* set keyboard LED */ 1629 /* NOTE: lock key state in "sc_state" won't be changed */ 1630 if (*(int *)arg & ~LOCK_MASK) 1631 return (EINVAL); 1632 1633 i = *(int *)arg; 1634 1635 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 1636 if (sc->sc_mode == K_XLATE && 1637 kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 1638 if (i & ALKED) 1639 i |= CLKED; 1640 else 1641 i &= ~CLKED; 1642 } 1643 if (KBD_HAS_DEVICE(kbd)) { 1644 error = hkbd_set_leds(sc, i); 1645 if (error) 1646 return (error); 1647 } 1648 #ifdef EVDEV_SUPPORT 1649 if (sc->sc_evdev != NULL && !HID_IN_POLLING_MODE()) { 1650 epoch_enter_preempt(INPUT_EPOCH, &et); 1651 evdev_push_leds(sc->sc_evdev, i); 1652 epoch_exit_preempt(INPUT_EPOCH, &et); 1653 } 1654 #endif 1655 1656 KBD_LED_VAL(kbd) = *(int *)arg; 1657 break; 1658 1659 case KDGKBSTATE: /* get lock key state */ 1660 *(int *)arg = sc->sc_state & LOCK_MASK; 1661 break; 1662 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1663 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1664 case _IO('K', 20): 1665 ival = IOCPARM_IVAL(arg); 1666 arg = (caddr_t)&ival; 1667 /* FALLTHROUGH */ 1668 #endif 1669 case KDSKBSTATE: /* set lock key state */ 1670 if (*(int *)arg & ~LOCK_MASK) { 1671 return (EINVAL); 1672 } 1673 sc->sc_state &= ~LOCK_MASK; 1674 sc->sc_state |= *(int *)arg; 1675 1676 /* 1677 * Attempt to set the keyboard LEDs; ignore the return value 1678 * intentionally. Note: Some hypervisors/emulators (e.g., QEMU, 1679 * Parallels—at least as of the time of writing) may fail when 1680 * setting LEDs. This can prevent kbdmux from attaching the 1681 * keyboard, which in turn may block the console from accessing 1682 * it. 1683 */ 1684 (void)hkbd_ioctl_locked(kbd, KDSETLED, arg); 1685 return (0); 1686 1687 case KDSETREPEAT: /* set keyboard repeat rate (new 1688 * interface) */ 1689 if (!KBD_HAS_DEVICE(kbd)) { 1690 return (0); 1691 } 1692 /* 1693 * Convert negative, zero and tiny args to the same limits 1694 * as atkbd. We could support delays of 1 msec, but 1695 * anything much shorter than the shortest atkbd value 1696 * of 250.34 is almost unusable as well as incompatible. 1697 */ 1698 kbd->kb_delay1 = imax(((int *)arg)[0], 250); 1699 kbd->kb_delay2 = imax(((int *)arg)[1], 34); 1700 #ifdef EVDEV_SUPPORT 1701 if (sc->sc_evdev != NULL && !HID_IN_POLLING_MODE()) { 1702 epoch_enter_preempt(INPUT_EPOCH, &et); 1703 evdev_push_repeats(sc->sc_evdev, kbd); 1704 epoch_exit_preempt(INPUT_EPOCH, &et); 1705 } 1706 #endif 1707 return (0); 1708 1709 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1710 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1711 case _IO('K', 67): 1712 ival = IOCPARM_IVAL(arg); 1713 arg = (caddr_t)&ival; 1714 /* FALLTHROUGH */ 1715 #endif 1716 case KDSETRAD: /* set keyboard repeat rate (old 1717 * interface) */ 1718 return (hkbd_set_typematic(kbd, *(int *)arg)); 1719 1720 case PIO_KEYMAP: /* set keyboard translation table */ 1721 case PIO_KEYMAPENT: /* set keyboard translation table 1722 * entry */ 1723 case PIO_DEADKEYMAP: /* set accent key translation table */ 1724 #ifdef COMPAT_FREEBSD13 1725 case OPIO_KEYMAP: /* set keyboard translation table 1726 * (compat) */ 1727 case OPIO_DEADKEYMAP: /* set accent key translation table 1728 * (compat) */ 1729 #endif /* COMPAT_FREEBSD13 */ 1730 sc->sc_accents = 0; 1731 /* FALLTHROUGH */ 1732 default: 1733 return (genkbd_commonioctl(kbd, cmd, arg)); 1734 } 1735 1736 return (0); 1737 } 1738 1739 static int 1740 hkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 1741 { 1742 int result; 1743 1744 /* 1745 * XXX Check if someone is calling us from a critical section: 1746 */ 1747 if (curthread->td_critnest != 0) 1748 return (EDEADLK); 1749 1750 /* 1751 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any 1752 * context where printf(9) can be called, which among other things 1753 * includes interrupt filters and threads with any kinds of locks 1754 * already held. For this reason it would be dangerous to acquire 1755 * the Giant here unconditionally. On the other hand we have to 1756 * have it to handle the ioctl. 1757 * So we make our best effort to auto-detect whether we can grab 1758 * the Giant or not. Blame syscons(4) for this. 1759 */ 1760 switch (cmd) { 1761 case KDGKBSTATE: 1762 case KDSKBSTATE: 1763 case KDSETLED: 1764 if (!mtx_owned(&Giant) && !HID_IN_POLLING_MODE()) 1765 return (EDEADLK); /* best I could come up with */ 1766 /* FALLTHROUGH */ 1767 default: 1768 SYSCONS_LOCK(); 1769 result = hkbd_ioctl_locked(kbd, cmd, arg); 1770 SYSCONS_UNLOCK(); 1771 return (result); 1772 } 1773 } 1774 1775 /* clear the internal state of the keyboard */ 1776 static void 1777 hkbd_clear_state(keyboard_t *kbd) 1778 { 1779 struct hkbd_softc *sc = kbd->kb_data; 1780 1781 SYSCONS_LOCK_ASSERT(); 1782 1783 sc->sc_flags &= ~(HKBD_FLAG_COMPOSE | HKBD_FLAG_POLLING); 1784 sc->sc_state &= LOCK_MASK; /* preserve locking key state */ 1785 sc->sc_accents = 0; 1786 sc->sc_composed_char = 0; 1787 #ifdef HKBD_EMULATE_ATSCANCODE 1788 sc->sc_buffered_char[0] = 0; 1789 sc->sc_buffered_char[1] = 0; 1790 #endif 1791 memset(&sc->sc_ndata, 0, bitstr_size(HKBD_NKEYCODE)); 1792 memset(&sc->sc_odata, 0, bitstr_size(HKBD_NKEYCODE)); 1793 memset(&sc->sc_ndata0, 0, bitstr_size(HKBD_NKEYCODE)); 1794 memset(&sc->sc_odata0, 0, bitstr_size(HKBD_NKEYCODE)); 1795 sc->sc_repeat_time = 0; 1796 sc->sc_repeat_key = 0; 1797 } 1798 1799 /* save the internal state, not used */ 1800 static int 1801 hkbd_get_state(keyboard_t *kbd, void *buf, size_t len) 1802 { 1803 return (len == 0) ? 1 : -1; 1804 } 1805 1806 /* set the internal state, not used */ 1807 static int 1808 hkbd_set_state(keyboard_t *kbd, void *buf, size_t len) 1809 { 1810 return (EINVAL); 1811 } 1812 1813 static int 1814 hkbd_poll(keyboard_t *kbd, int on) 1815 { 1816 struct hkbd_softc *sc = kbd->kb_data; 1817 1818 SYSCONS_LOCK(); 1819 /* 1820 * Keep a reference count on polling to allow recursive 1821 * cngrab() during a panic for example. 1822 */ 1823 if (on) 1824 sc->sc_polling++; 1825 else if (sc->sc_polling > 0) 1826 sc->sc_polling--; 1827 1828 if (sc->sc_polling != 0) { 1829 sc->sc_flags |= HKBD_FLAG_POLLING; 1830 sc->sc_poll_thread = curthread; 1831 } else { 1832 sc->sc_flags &= ~HKBD_FLAG_POLLING; 1833 sc->sc_delay = 0; 1834 } 1835 SYSCONS_UNLOCK(); 1836 1837 return (0); 1838 } 1839 1840 /* local functions */ 1841 1842 static int 1843 hkbd_set_leds(struct hkbd_softc *sc, uint8_t leds) 1844 { 1845 uint8_t id; 1846 uint8_t any; 1847 uint8_t *buf; 1848 int len; 1849 int error; 1850 1851 SYSCONS_LOCK_ASSERT(); 1852 DPRINTF("leds=0x%02x\n", leds); 1853 1854 if (hkbd_no_leds) 1855 return (0); 1856 1857 memset(sc->sc_buffer, 0, HKBD_BUFFER_SIZE); 1858 1859 id = sc->sc_id_leds; 1860 any = 0; 1861 1862 /* Assumption: All led bits must be in the same ID. */ 1863 1864 if (sc->sc_flags & HKBD_FLAG_NUMLOCK) { 1865 hid_put_udata(sc->sc_buffer + 1, HKBD_BUFFER_SIZE - 1, 1866 &sc->sc_loc_numlock, leds & NLKED ? 1 : 0); 1867 any = 1; 1868 } 1869 1870 if (sc->sc_flags & HKBD_FLAG_SCROLLLOCK) { 1871 hid_put_udata(sc->sc_buffer + 1, HKBD_BUFFER_SIZE - 1, 1872 &sc->sc_loc_scrolllock, leds & SLKED ? 1 : 0); 1873 any = 1; 1874 } 1875 1876 if (sc->sc_flags & HKBD_FLAG_CAPSLOCK) { 1877 hid_put_udata(sc->sc_buffer + 1, HKBD_BUFFER_SIZE - 1, 1878 &sc->sc_loc_capslock, leds & CLKED ? 1 : 0); 1879 any = 1; 1880 } 1881 1882 /* if no leds, nothing to do */ 1883 if (!any) 1884 return (0); 1885 1886 /* range check output report length */ 1887 len = sc->sc_led_size; 1888 if (len > (HKBD_BUFFER_SIZE - 1)) 1889 len = (HKBD_BUFFER_SIZE - 1); 1890 1891 /* check if we need to prefix an ID byte */ 1892 1893 if (id != 0) { 1894 sc->sc_buffer[0] = id; 1895 buf = sc->sc_buffer; 1896 } else { 1897 buf = sc->sc_buffer + 1; 1898 } 1899 1900 DPRINTF("len=%d, id=%d\n", len, id); 1901 1902 /* start data transfer */ 1903 SYSCONS_UNLOCK(); 1904 error = hid_write(sc->sc_dev, buf, len); 1905 SYSCONS_LOCK(); 1906 DPRINTF("error %d", error); 1907 1908 return (error); 1909 } 1910 1911 static int 1912 hkbd_set_typematic(keyboard_t *kbd, int code) 1913 { 1914 #ifdef EVDEV_SUPPORT 1915 struct hkbd_softc *sc = kbd->kb_data; 1916 #endif 1917 if (code & ~0x7f) { 1918 return (EINVAL); 1919 } 1920 kbd->kb_delay1 = kbdelays[(code >> 5) & 3]; 1921 kbd->kb_delay2 = kbrates[code & 0x1f]; 1922 #ifdef EVDEV_SUPPORT 1923 if (sc->sc_evdev != NULL) 1924 evdev_push_repeats(sc->sc_evdev, kbd); 1925 #endif 1926 return (0); 1927 } 1928 1929 #ifdef HKBD_EMULATE_ATSCANCODE 1930 static uint32_t 1931 hkbd_atkeycode(int usbcode, const bitstr_t *bitmap) 1932 { 1933 uint32_t keycode; 1934 1935 keycode = hkbd_trtab[KEY_INDEX(usbcode)]; 1936 1937 /* 1938 * Translate Alt-PrintScreen to SysRq. 1939 * 1940 * Some or all AT keyboards connected through USB have already 1941 * mapped Alted PrintScreens to an unusual usbcode (0x8a). 1942 * hkbd_trtab translates this to 0x7e, and key2scan() would 1943 * translate that to 0x79 (Intl' 4). Assume that if we have 1944 * an Alted 0x7e here then it actually is an Alted PrintScreen. 1945 * 1946 * The usual usbcode for all PrintScreens is 0x46. hkbd_trtab 1947 * translates this to 0x5c, so the Alt check to classify 0x5c 1948 * is routine. 1949 */ 1950 if ((keycode == 0x5c || keycode == 0x7e) && 1951 (HKBD_KEY_PRESSED(bitmap, 0xe2 /* ALT-L */) || 1952 HKBD_KEY_PRESSED(bitmap, 0xe6 /* ALT-R */))) 1953 return (0x54); 1954 return (keycode); 1955 } 1956 1957 static int 1958 hkbd_key2scan(struct hkbd_softc *sc, int code, const bitstr_t *bitmap, int up) 1959 { 1960 static const int scan[] = { 1961 /* 89 */ 1962 0x11c, /* Enter */ 1963 /* 90-99 */ 1964 0x11d, /* Ctrl-R */ 1965 0x135, /* Divide */ 1966 0x137, /* PrintScreen */ 1967 0x138, /* Alt-R */ 1968 0x147, /* Home */ 1969 0x148, /* Up */ 1970 0x149, /* PageUp */ 1971 0x14b, /* Left */ 1972 0x14d, /* Right */ 1973 0x14f, /* End */ 1974 /* 100-109 */ 1975 0x150, /* Down */ 1976 0x151, /* PageDown */ 1977 0x152, /* Insert */ 1978 0x153, /* Delete */ 1979 0x146, /* Pause/Break */ 1980 0x15b, /* Win_L(Super_L) */ 1981 0x15c, /* Win_R(Super_R) */ 1982 0x15d, /* Application(Menu) */ 1983 1984 /* SUN TYPE 6 USB KEYBOARD */ 1985 0x168, /* Sun Type 6 Help */ 1986 0x15e, /* Sun Type 6 Stop */ 1987 /* 110 - 119 */ 1988 0x15f, /* Sun Type 6 Again */ 1989 0x160, /* Sun Type 6 Props */ 1990 0x161, /* Sun Type 6 Undo */ 1991 0x162, /* Sun Type 6 Front */ 1992 0x163, /* Sun Type 6 Copy */ 1993 0x164, /* Sun Type 6 Open */ 1994 0x165, /* Sun Type 6 Paste */ 1995 0x166, /* Sun Type 6 Find */ 1996 0x167, /* Sun Type 6 Cut */ 1997 0x125, /* Sun Type 6 Mute */ 1998 /* 120 - 130 */ 1999 0x11f, /* Sun Type 6 VolumeDown */ 2000 0x11e, /* Sun Type 6 VolumeUp */ 2001 0x120, /* Sun Type 6 PowerDown */ 2002 2003 /* Japanese 106/109 keyboard */ 2004 0x73, /* Keyboard Intl' 1 (backslash / underscore) */ 2005 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */ 2006 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */ 2007 0x79, /* Keyboard Intl' 4 (Henkan) */ 2008 0x7b, /* Keyboard Intl' 5 (Muhenkan) */ 2009 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */ 2010 0x71, /* Apple Keyboard JIS (Kana) */ 2011 0x72, /* Apple Keyboard JIS (Eisu) */ 2012 }; 2013 2014 if ((code >= 89) && (code < (int)(89 + nitems(scan)))) { 2015 code = scan[code - 89]; 2016 } 2017 /* PrintScreen */ 2018 if (code == 0x137 && (!( 2019 HKBD_KEY_PRESSED(bitmap, 0xe0 /* CTRL-L */) || 2020 HKBD_KEY_PRESSED(bitmap, 0xe4 /* CTRL-R */) || 2021 HKBD_KEY_PRESSED(bitmap, 0xe1 /* SHIFT-L */) || 2022 HKBD_KEY_PRESSED(bitmap, 0xe5 /* SHIFT-R */)))) { 2023 code |= SCAN_PREFIX_SHIFT; 2024 } 2025 /* Pause/Break */ 2026 if ((code == 0x146) && (!( 2027 HKBD_KEY_PRESSED(bitmap, 0xe0 /* CTRL-L */) || 2028 HKBD_KEY_PRESSED(bitmap, 0xe4 /* CTRL-R */)))) { 2029 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL); 2030 } 2031 code |= (up ? SCAN_RELEASE : SCAN_PRESS); 2032 2033 if (code & SCAN_PREFIX) { 2034 if (code & SCAN_PREFIX_CTL) { 2035 /* Ctrl */ 2036 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE)); 2037 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX); 2038 } else if (code & SCAN_PREFIX_SHIFT) { 2039 /* Shift */ 2040 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE)); 2041 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT); 2042 } else { 2043 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX); 2044 sc->sc_buffered_char[1] = 0; 2045 } 2046 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1); 2047 } 2048 return (code); 2049 2050 } 2051 2052 #endif /* HKBD_EMULATE_ATSCANCODE */ 2053 2054 static keyboard_switch_t hkbdsw = { 2055 .probe = &hkbd__probe, 2056 .init = &hkbd_init, 2057 .term = &hkbd_term, 2058 .intr = &hkbd_intr, 2059 .test_if = &hkbd_test_if, 2060 .enable = &hkbd_enable, 2061 .disable = &hkbd_disable, 2062 .read = &hkbd_read, 2063 .check = &hkbd_check, 2064 .read_char = &hkbd_read_char, 2065 .check_char = &hkbd_check_char, 2066 .ioctl = &hkbd_ioctl, 2067 .lock = &hkbd_lock, 2068 .clear_state = &hkbd_clear_state, 2069 .get_state = &hkbd_get_state, 2070 .set_state = &hkbd_set_state, 2071 .poll = &hkbd_poll, 2072 }; 2073 2074 KEYBOARD_DRIVER(hkbd, hkbdsw, hkbd_configure); 2075 2076 static int 2077 hkbd_driver_load(module_t mod, int what, void *arg) 2078 { 2079 switch (what) { 2080 case MOD_LOAD: 2081 kbd_add_driver(&hkbd_kbd_driver); 2082 break; 2083 case MOD_UNLOAD: 2084 kbd_delete_driver(&hkbd_kbd_driver); 2085 break; 2086 } 2087 return (0); 2088 } 2089 2090 static device_method_t hkbd_methods[] = { 2091 DEVMETHOD(device_probe, hkbd_probe), 2092 DEVMETHOD(device_attach, hkbd_attach), 2093 DEVMETHOD(device_detach, hkbd_detach), 2094 DEVMETHOD(device_resume, hkbd_resume), 2095 2096 DEVMETHOD_END 2097 }; 2098 2099 static driver_t hkbd_driver = { 2100 .name = "hkbd", 2101 .methods = hkbd_methods, 2102 .size = sizeof(struct hkbd_softc), 2103 }; 2104 2105 DRIVER_MODULE(hkbd, hidbus, hkbd_driver, hkbd_driver_load, NULL); 2106 MODULE_DEPEND(hkbd, hid, 1, 1, 1); 2107 MODULE_DEPEND(hkbd, hidbus, 1, 1, 1); 2108 #ifdef EVDEV_SUPPORT 2109 MODULE_DEPEND(hkbd, evdev, 1, 1, 1); 2110 #endif 2111 MODULE_VERSION(hkbd, 1); 2112 HID_PNP_INFO(hkbd_devs); 2113