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