5 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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.
38 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
44 #include "opt_evdev.h"
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>
54 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
66 #include <sys/epoch.h>
67 #include <sys/taskqueue.h>
68 #include <sys/bitstring.h>
70 #include <machine/atomic.h>
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>
79 #include <dev/evdev/input.h>
80 #include <dev/evdev/evdev.h>
83 #include <sys/ioccom.h>
84 #include <sys/filio.h>
87 #include <dev/kbd/kbdreg.h>
89 /* the initial key map, accent map and fkey strings */
90 #if defined(HKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
91 #define KBD_DFLT_KEYMAP
95 /* the following file must be included after "ukbdmap.h" */
96 #include <dev/kbd/kbdtables.h>
99 static int hkbd_debug = 0;
100 static int hkbd_no_leds = 0;
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");
109 #define INPUT_EPOCH global_epoch_preempt
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); \
123 #define MOD_EJECT 0x01
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;
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);
153 struct thread *sc_poll_thread;
155 struct evdev_dev *sc_evdev;
158 sbintime_t sc_co_basetime;
160 uint32_t sc_repeat_time;
161 uint32_t sc_input[HKBD_IN_BUF_SIZE]; /* input buffer */
163 uint32_t sc_composed_char; /* composed char code, if non-zero */
164 #ifdef HKBD_EMULATE_ATSCANCODE
165 uint32_t sc_buffered_char[2];
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
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
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 */
188 uint32_t sc_inputhead;
189 uint32_t sc_inputtail;
191 uint8_t sc_iface_index;
193 uint8_t sc_id_apple_eject;
194 uint8_t sc_id_apple_fn;
195 uint8_t sc_id_loc_key[HKBD_NKEYCODE];
198 uint8_t sc_repeat_key;
200 uint8_t sc_buffer[HKBD_BUFFER_SIZE];
203 #define KEY_NONE 0x00
204 #define KEY_ERROR 0x01
207 #define KEY_RELEASE 0x400
208 #define KEY_INDEX(c) ((c) & 0xFF)
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)
220 #define HKBD_LOCK(sc) do { \
221 if (!HID_IN_POLLING_MODE()) \
222 mtx_lock(&(sc)->sc_mtx); \
224 #define HKBD_UNLOCK(sc) do { \
225 if (!HID_IN_POLLING_MODE()) \
226 mtx_unlock(&(sc)->sc_mtx); \
228 #define HKBD_LOCK_ASSERT(sc) do { \
229 if (!HID_IN_POLLING_MODE()) \
230 mtx_assert(&(sc)->sc_mtx, MA_OWNED); \
232 #define SYSCONS_LOCK() do { \
233 if (!HID_IN_POLLING_MODE()) \
236 #define SYSCONS_UNLOCK() do { \
237 if (!HID_IN_POLLING_MODE()) \
238 mtx_unlock(&Giant); \
240 #define SYSCONS_LOCK_ASSERT() do { \
241 if (!HID_IN_POLLING_MODE()) \
242 mtx_assert(&Giant, MA_OWNED); \
245 #define NN 0 /* no translation */
247 * Translate USB keycodes to AT keyboard scancodes.
250 * FIXME: Mac USB keyboard generates:
251 * 0x53: keypad NumLock/Clear
258 * USB Apple Keyboard JIS generates:
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 */
297 static const uint8_t hkbd_boot_desc[] = { HID_KBD_BOOTPROTO_DESCR() };
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);
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 *);
314 static task_fn_t hkbd_event_keyinput;
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;
322 static evdev_event_t hkbd_ev_event;
324 static const struct evdev_methods hkbd_evdev_methods = {
325 .ev_event = hkbd_ev_event,
330 hkbd_any_key_pressed(struct hkbd_softc *sc)
334 bit_ffs(sc->sc_odata, HKBD_NKEYCODE, &result);
335 return (result != -1);
339 hkbd_any_key_valid(struct hkbd_softc *sc)
343 bit_ffs(sc->sc_loc_key_valid, HKBD_NKEYCODE, &result);
344 return (result != -1);
348 hkbd_is_modifier_key(uint32_t key)
351 return (key >= MOD_MIN && key <= MOD_MAX);
355 hkbd_start_timer(struct hkbd_softc *sc)
357 sbintime_t delay, now, prec;
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;
365 /* compute timeout */
366 delay = SBT_1MS * sc->sc_delay;
367 sc->sc_co_basetime += delay;
369 /* check if we are running behind */
370 if (sc->sc_co_basetime < now)
371 sc->sc_co_basetime = now;
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);
381 hkbd_put_key(struct hkbd_softc *sc, uint32_t key)
385 HKBD_LOCK_ASSERT(sc);
387 DPRINTF("0x%02x (%d) %s\n", key, key,
388 (key & KEY_RELEASE) ? "released" : "pressed");
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));
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);
401 DPRINTF("input buffer is full\n");
406 hkbd_do_poll(struct hkbd_softc *sc, uint8_t wait)
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");
414 if (!HID_IN_POLLING_MODE()) {
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.
422 while (sc->sc_inputhead ==
423 atomic_load_acq_32(&sc->sc_inputtail)) {
425 * Give USB threads a chance to run. Note that
426 * kern_yield performs DROP_GIANT + PICKUP_GIANT.
428 kern_yield(PRI_UNCHANGED);
435 while (sc->sc_inputhead == sc->sc_inputtail) {
436 hidbus_intr_poll(sc->sc_dev);
438 /* Delay-optimised support for repetition of keys */
439 if (hkbd_any_key_pressed(sc)) {
440 /* a key is pressed - need timekeeping */
443 /* 1 millisecond has passed */
455 hkbd_get_key(struct hkbd_softc *sc, uint8_t wait)
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"));
465 if (sc->sc_flags & HKBD_FLAG_POLLING)
466 hkbd_do_poll(sc, wait);
468 head = sc->sc_inputhead;
469 if (head == atomic_load_acq_32(&sc->sc_inputtail)) {
472 c = sc->sc_input[head];
473 head = (head + 1) % HKBD_IN_BUF_SIZE;
474 atomic_store_rel_32(&sc->sc_inputhead, head);
480 hkbd_interrupt(struct hkbd_softc *sc)
482 const uint32_t now = sc->sc_time_ms;
485 HKBD_LOCK_ASSERT(sc);
488 * Check for key changes, the order is:
490 * 2. Modifier keys up
491 * 3. Modifier keys down
492 * 4. Regular keys down
494 * This allows devices which send events changing the state of
495 * both a modifier key and a regular key, to be correctly
497 bit_foreach(sc->sc_odata, HKBD_NKEYCODE, key) {
498 if (hkbd_is_modifier_key(key) || bit_test(sc->sc_ndata, key))
500 hkbd_put_key(sc, key | KEY_RELEASE);
502 /* clear repeating key, if any */
503 if (sc->sc_repeat_key == key)
504 sc->sc_repeat_key = 0;
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))
515 hkbd_put_key(sc, key | KEY_PRESS);
517 sc->sc_co_basetime = sbinuptime();
518 sc->sc_delay = sc->sc_kbd.kb_delay1;
519 hkbd_start_timer(sc);
521 /* set repeat time for last key */
522 sc->sc_repeat_time = now + sc->sc_kbd.kb_delay1;
523 sc->sc_repeat_key = key;
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));
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);
534 /* check if time has elapsed */
536 hkbd_put_key(sc, sc->sc_repeat_key | KEY_PRESS);
537 sc->sc_repeat_time = now + sc->sc_kbd.kb_delay2;
542 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL)
543 evdev_sync(sc->sc_evdev);
546 /* wakeup keyboard system */
547 if (!HID_IN_POLLING_MODE())
548 taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task);
552 hkbd_event_keyinput(void *context, int pending)
554 struct hkbd_softc *sc = context;
557 SYSCONS_LOCK_ASSERT();
559 if ((sc->sc_flags & HKBD_FLAG_POLLING) != 0)
562 if (sc->sc_inputhead == atomic_load_acq_32(&sc->sc_inputtail))
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);
571 /* read and discard the input, no one is waiting for it */
573 c = hkbd_read_char(&sc->sc_kbd, 0);
574 } while (c != NOKEY);
579 hkbd_timeout(void *arg)
581 struct hkbd_softc *sc = arg;
583 struct epoch_tracker et;
586 HKBD_LOCK_ASSERT(sc);
588 sc->sc_time_ms += sc->sc_delay;
592 epoch_enter_preempt(INPUT_EPOCH, &et);
596 epoch_exit_preempt(INPUT_EPOCH, &et);
599 /* Make sure any leftover key events gets read out */
600 taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task);
602 if (hkbd_any_key_pressed(sc) ||
603 atomic_load_acq_32(&sc->sc_inputhead) != sc->sc_inputtail) {
604 hkbd_start_timer(sc);
609 hkbd_apple_fn(uint32_t 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;
623 hkbd_apple_swap(uint32_t keycode)
626 case 0x35: return 0x64;
627 case 0x64: return 0x35;
628 default: return keycode;
633 hkbd_intr_callback(void *context, void *data, hid_size_t len)
635 struct hkbd_softc *sc = context;
641 HKBD_LOCK_ASSERT(sc);
643 DPRINTF("actlen=%d bytes\n", len);
646 DPRINTF("zero length data\n");
650 if (sc->sc_kbd_id != 0) {
651 /* check and remove HID ID byte */
656 DPRINTF("zero length data\n");
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));
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);
671 /* clear modifiers */
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;
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))
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 */
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--) {
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));
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)
713 /* set key in bitmap */
714 bit_set(sc->sc_ndata, key);
715 bit_set(sc->sc_ndata0, key);
717 } else if (hid_get_data(buf, len, &sc->sc_loc_key[i])) {
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)
726 /* set key in bitmap */
727 bit_set(sc->sc_ndata, key);
731 DPRINTF("modifiers = 0x%04x\n", modifiers);
732 bit_foreach(sc->sc_ndata, HKBD_NKEYCODE, i)
733 DPRINTF("Key 0x%02x pressed\n", i);
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) },
744 hkbd_probe(device_t dev)
746 keyboard_switch_t *sw = kbd_get_switch(HKBD_DRIVER_NAME);
755 error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hkbd_devs);
759 hidbus_set_desc(dev, "Keyboard");
761 return (BUS_PROBE_DEFAULT);
765 hkbd_parse_hid(struct hkbd_softc *sc, const uint8_t *ptr, uint32_t len,
772 /* reset detected bits */
773 sc->sc_flags &= ~HKBD_FLAG_HID_MASK;
775 /* reset detected keys */
776 memset(sc->sc_loc_key_valid, 0, bitstr_size(HKBD_NKEYCODE));
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);
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");
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");
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");
809 bit_set(sc->sc_loc_key_valid, 0);
810 DPRINTFN(1, "Found keyboard event array\n");
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);
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");
836 if (hidbus_locate(ptr, len,
837 HID_USAGE2(HUP_LEDS, 0x02),
838 hid_output, tlc_index, 0, &sc->sc_loc_capslock, &flags,
840 if ((sc->sc_flags & HKBD_FLAG_NUMLOCK) == 0)
842 if (flags & HIO_VARIABLE && sc->sc_id_leds == id)
843 sc->sc_flags |= HKBD_FLAG_CAPSLOCK;
844 DPRINTFN(1, "Found keyboard capslock\n");
846 if (hidbus_locate(ptr, len,
847 HID_USAGE2(HUP_LEDS, 0x03),
848 hid_output, tlc_index, 0, &sc->sc_loc_scrolllock, &flags,
850 if ((sc->sc_flags & (HKBD_FLAG_NUMLOCK | HKBD_FLAG_CAPSLOCK))
853 if (flags & HIO_VARIABLE && sc->sc_id_leds == id)
854 sc->sc_flags |= HKBD_FLAG_SCROLLLOCK;
855 DPRINTFN(1, "Found keyboard scrolllock\n");
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);
865 hkbd_attach(device_t dev)
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;
875 uint8_t tlc_index = hidbus_get_index(dev);
877 struct evdev_dev *evdev;
882 SYSCONS_LOCK_ASSERT();
884 kbd_init_struct(kbd, HKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
886 kbd->kb_data = (void *)sc;
888 sc->sc_mode = K_XLATE;
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);
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);
900 /* setup default keyboard maps */
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];
908 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
909 sc->sc_fkeymap, HKBD_NFKEY);
911 KBD_FOUND_DEVICE(kbd);
913 hkbd_clear_state(kbd);
916 * FIXME: set the initial value for lock keys in "sc_state"
917 * according to the BIOS data?
921 /* get HID descriptor */
922 err = hid_get_report_descr(dev, &hid_ptr, &hid_len);
925 DPRINTF("Parsing HID descriptor of %d bytes\n",
928 hkbd_parse_hid(sc, hid_ptr, hid_len, tlc_index);
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");
936 err = hid_set_protocol(dev, 0);
939 DPRINTF("Set protocol error=%d (ignored)\n", err);
942 hkbd_parse_hid(sc, hkbd_boot_desc, sizeof(hkbd_boot_desc), 0);
945 /* ignore if SETIDLE fails, hence it is not crucial */
946 hid_set_idle(dev, 0, 0);
948 hkbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
952 if (kbd_register(kbd) < 0) {
955 KBD_CONFIG_DONE(kbd);
959 #ifdef KBD_INSTALL_CDEV
960 if (kbd_attach(kbd)) {
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,
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);
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);
990 if (evdev_register(evdev))
993 sc->sc_evdev = evdev;
996 sc->sc_flags |= HKBD_FLAG_ATTACHED;
999 kbdd_diag(kbd, bootverbose);
1002 /* start the keyboard */
1003 hidbus_intr_start(dev);
1005 return (0); /* success */
1009 return (ENXIO); /* error */
1013 hkbd_detach(device_t dev)
1015 struct hkbd_softc *sc = device_get_softc(dev);
1016 #ifdef EVDEV_SUPPORT
1017 struct epoch_tracker et;
1021 SYSCONS_LOCK_ASSERT();
1025 sc->sc_flags |= HKBD_FLAG_GONE;
1028 callout_stop(&sc->sc_callout);
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);
1036 /* release all leftover keys, if any */
1037 memset(&sc->sc_ndata, 0, bitstr_size(HKBD_NKEYCODE));
1039 /* process releasing of all keys */
1041 #ifdef EVDEV_SUPPORT
1042 epoch_enter_preempt(INPUT_EPOCH, &et);
1045 #ifdef EVDEV_SUPPORT
1046 epoch_exit_preempt(INPUT_EPOCH, &et);
1049 taskqueue_drain(taskqueue_swi_giant, &sc->sc_task);
1052 mtx_destroy(&sc->sc_mtx);
1053 hkbd_disable(&sc->sc_kbd);
1055 #ifdef KBD_INSTALL_CDEV
1056 if (sc->sc_flags & HKBD_FLAG_ATTACHED) {
1057 error = kbd_detach(&sc->sc_kbd);
1059 /* usb attach cannot return an error */
1060 device_printf(dev, "WARNING: kbd_detach() "
1061 "returned non-zero! (ignored)\n");
1066 #ifdef EVDEV_SUPPORT
1067 evdev_free(sc->sc_evdev);
1070 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
1071 error = kbd_unregister(&sc->sc_kbd);
1073 /* usb attach cannot return an error */
1074 device_printf(dev, "WARNING: kbd_unregister() "
1075 "returned non-zero! (ignored)\n");
1078 sc->sc_kbd.kb_flags = 0;
1080 DPRINTF("%s: disconnected\n",
1081 device_get_nameunit(dev));
1087 hkbd_resume(device_t dev)
1089 struct hkbd_softc *sc = device_get_softc(dev);
1091 SYSCONS_LOCK_ASSERT();
1093 hkbd_clear_state(&sc->sc_kbd);
1098 #ifdef EVDEV_SUPPORT
1100 hkbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
1103 keyboard_t *kbd = evdev_get_softc(evdev);
1105 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
1106 (type == EV_LED || type == EV_REP)) {
1108 kbd_ev_event(kbd, type, code, value);
1114 /* early keyboard probe, not supported */
1116 hkbd_configure(int flags)
1121 /* detect a keyboard, not used */
1123 hkbd__probe(int unit, void *arg, int flags)
1128 /* reset and initialize the device, not used */
1130 hkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1135 /* test the interface to the device, not used */
1137 hkbd_test_if(keyboard_t *kbd)
1142 /* finish using this keyboard, not used */
1144 hkbd_term(keyboard_t *kbd)
1149 /* keyboard interrupt routine, not used */
1151 hkbd_intr(keyboard_t *kbd, void *arg)
1156 /* lock the access to the keyboard, not used */
1158 hkbd_lock(keyboard_t *kbd, int lock)
1164 * Enable the access to the device; until this function is called,
1165 * the client cannot read from the keyboard.
1168 hkbd_enable(keyboard_t *kbd)
1178 /* disallow the access to the device */
1180 hkbd_disable(keyboard_t *kbd)
1184 KBD_DEACTIVATE(kbd);
1190 /* check if data is waiting */
1191 /* Currently unused. */
1193 hkbd_check(keyboard_t *kbd)
1195 struct hkbd_softc *sc = kbd->kb_data;
1197 SYSCONS_LOCK_ASSERT();
1199 if (!KBD_IS_ACTIVE(kbd))
1202 if (sc->sc_flags & HKBD_FLAG_POLLING)
1203 hkbd_do_poll(sc, 0);
1205 #ifdef HKBD_EMULATE_ATSCANCODE
1206 if (sc->sc_buffered_char[0]) {
1210 if (sc->sc_inputhead != atomic_load_acq_32(&sc->sc_inputtail)) {
1216 /* check if char is waiting */
1218 hkbd_check_char_locked(keyboard_t *kbd)
1220 struct hkbd_softc *sc = kbd->kb_data;
1222 SYSCONS_LOCK_ASSERT();
1224 if (!KBD_IS_ACTIVE(kbd))
1227 if ((sc->sc_composed_char > 0) &&
1228 (!(sc->sc_flags & HKBD_FLAG_COMPOSE))) {
1231 return (hkbd_check(kbd));
1235 hkbd_check_char(keyboard_t *kbd)
1240 result = hkbd_check_char_locked(kbd);
1246 /* read one byte from the keyboard if it's allowed */
1247 /* Currently unused. */
1249 hkbd_read(keyboard_t *kbd, int wait)
1251 struct hkbd_softc *sc = kbd->kb_data;
1253 #ifdef HKBD_EMULATE_ATSCANCODE
1259 SYSCONS_LOCK_ASSERT();
1261 if (!KBD_IS_ACTIVE(kbd))
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);
1271 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1272 sc->sc_buffered_char[1] = 0;
1275 #endif /* HKBD_EMULATE_ATSCANCODE */
1278 usbcode = hkbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1279 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1284 #ifdef HKBD_EMULATE_ATSCANCODE
1285 keycode = hkbd_atkeycode(usbcode, sc->sc_ndata);
1286 if (keycode == NN) {
1289 return (hkbd_key2scan(sc, keycode, sc->sc_ndata,
1290 (usbcode & KEY_RELEASE)));
1291 #else /* !HKBD_EMULATE_ATSCANCODE */
1293 #endif /* HKBD_EMULATE_ATSCANCODE */
1296 /* read char from the keyboard */
1298 hkbd_read_char_locked(keyboard_t *kbd, int wait)
1300 struct hkbd_softc *sc = kbd->kb_data;
1304 #ifdef HKBD_EMULATE_ATSCANCODE
1308 SYSCONS_LOCK_ASSERT();
1310 if (!KBD_IS_ACTIVE(kbd))
1315 /* do we have a composed char to return ? */
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;
1322 if (action > 0xFF) {
1327 #ifdef HKBD_EMULATE_ATSCANCODE
1329 /* do we have a pending raw scan code? */
1331 if (sc->sc_mode == K_RAW) {
1332 scancode = sc->sc_buffered_char[0];
1334 if (scancode & SCAN_PREFIX) {
1335 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1336 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1338 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1339 sc->sc_buffered_char[1] = 0;
1343 #endif /* HKBD_EMULATE_ATSCANCODE */
1345 /* see if there is something in the keyboard port */
1347 usbcode = hkbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1348 if (usbcode == -1) {
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) {
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)));
1364 #else /* !HKBD_EMULATE_ATSCANCODE */
1366 /* return the byte as is for the K_RAW mode */
1367 if (sc->sc_mode == K_RAW) {
1370 /* USB key index -> key code */
1371 keycode = hkbd_trtab[KEY_INDEX(usbcode)];
1372 if (keycode == NN) {
1375 #endif /* HKBD_EMULATE_ATSCANCODE */
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;
1383 if (sc->sc_composed_char > 0xFF) {
1384 sc->sc_composed_char = 0;
1388 if (!(sc->sc_flags & HKBD_FLAG_COMPOSE)) {
1389 sc->sc_flags |= HKBD_FLAG_COMPOSE;
1390 sc->sc_composed_char = 0;
1396 /* return the key code in the K_CODE mode */
1397 if (usbcode & KEY_RELEASE) {
1398 keycode |= SCAN_RELEASE;
1400 if (sc->sc_mode == K_CODE) {
1403 /* compose a character code */
1404 if (sc->sc_flags & HKBD_FLAG_COMPOSE) {
1406 /* key pressed, process it */
1409 case 0x49: /* keypad 7,8,9 */
1410 sc->sc_composed_char *= 10;
1411 sc->sc_composed_char += keycode - 0x40;
1412 goto check_composed;
1416 case 0x4D: /* keypad 4,5,6 */
1417 sc->sc_composed_char *= 10;
1418 sc->sc_composed_char += keycode - 0x47;
1419 goto check_composed;
1423 case 0x51: /* keypad 1,2,3 */
1424 sc->sc_composed_char *= 10;
1425 sc->sc_composed_char += keycode - 0x4E;
1426 goto check_composed;
1428 case 0x52: /* keypad 0 */
1429 sc->sc_composed_char *= 10;
1430 goto check_composed;
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 */
1445 case 0x38: /* left alt key */
1449 if (sc->sc_composed_char > 0) {
1450 sc->sc_flags &= ~HKBD_FLAG_COMPOSE;
1451 sc->sc_composed_char = 0;
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) {
1468 if (sc->sc_composed_char <= 0xFF) {
1475 /* Currently wait is always false. */
1477 hkbd_read_char(keyboard_t *kbd, int wait)
1482 keycode = hkbd_read_char_locked(kbd, wait);
1488 /* some useful control functions */
1490 hkbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
1492 struct hkbd_softc *sc = kbd->kb_data;
1493 #ifdef EVDEV_SUPPORT
1494 struct epoch_tracker et;
1498 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1499 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1504 SYSCONS_LOCK_ASSERT();
1507 case KDGKBMODE: /* get keyboard mode */
1508 *(int *)arg = sc->sc_mode;
1510 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1511 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1513 ival = IOCPARM_IVAL(arg);
1514 arg = (caddr_t)&ival;
1517 case KDSKBMODE: /* set keyboard mode */
1518 switch (*(int *)arg) {
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);
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;
1539 case KDGETLED: /* get keyboard LED */
1540 *(int *)arg = KBD_LED_VAL(kbd);
1542 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1543 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1545 ival = IOCPARM_IVAL(arg);
1546 arg = (caddr_t)&ival;
1549 case KDSETLED: /* set keyboard LED */
1550 /* NOTE: lock key state in "sc_state" won't be changed */
1551 if (*(int *)arg & ~LOCK_MASK)
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) {
1564 if (KBD_HAS_DEVICE(kbd)) {
1565 error = hkbd_set_leds(sc, i);
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);
1577 KBD_LED_VAL(kbd) = *(int *)arg;
1580 case KDGKBSTATE: /* get lock key state */
1581 *(int *)arg = sc->sc_state & LOCK_MASK;
1583 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1584 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1586 ival = IOCPARM_IVAL(arg);
1587 arg = (caddr_t)&ival;
1590 case KDSKBSTATE: /* set lock key state */
1591 if (*(int *)arg & ~LOCK_MASK) {
1594 sc->sc_state &= ~LOCK_MASK;
1595 sc->sc_state |= *(int *)arg;
1597 /* set LEDs and quit */
1598 return (hkbd_ioctl_locked(kbd, KDSETLED, arg));
1600 case KDSETREPEAT: /* set keyboard repeat rate (new
1602 if (!KBD_HAS_DEVICE(kbd)) {
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.
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);
1622 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1623 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1625 ival = IOCPARM_IVAL(arg);
1626 arg = (caddr_t)&ival;
1629 case KDSETRAD: /* set keyboard repeat rate (old
1631 return (hkbd_set_typematic(kbd, *(int *)arg));
1633 case PIO_KEYMAP: /* set keyboard translation table */
1634 case OPIO_KEYMAP: /* set keyboard translation table
1636 case PIO_KEYMAPENT: /* set keyboard translation table
1638 case PIO_DEADKEYMAP: /* set accent key translation table */
1642 return (genkbd_commonioctl(kbd, cmd, arg));
1649 hkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1654 * XXX Check if someone is calling us from a critical section:
1656 if (curthread->td_critnest != 0)
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.
1673 if (!mtx_owned(&Giant) && !HID_IN_POLLING_MODE())
1674 return (EDEADLK); /* best I could come up with */
1678 result = hkbd_ioctl_locked(kbd, cmd, arg);
1684 /* clear the internal state of the keyboard */
1686 hkbd_clear_state(keyboard_t *kbd)
1688 struct hkbd_softc *sc = kbd->kb_data;
1690 SYSCONS_LOCK_ASSERT();
1692 sc->sc_flags &= ~(HKBD_FLAG_COMPOSE | HKBD_FLAG_POLLING);
1693 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
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;
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;
1708 /* save the internal state, not used */
1710 hkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1712 return (len == 0) ? 1 : -1;
1715 /* set the internal state, not used */
1717 hkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1723 hkbd_poll(keyboard_t *kbd, int on)
1725 struct hkbd_softc *sc = kbd->kb_data;
1729 * Keep a reference count on polling to allow recursive
1730 * cngrab() during a panic for example.
1734 else if (sc->sc_polling > 0)
1737 if (sc->sc_polling != 0) {
1738 sc->sc_flags |= HKBD_FLAG_POLLING;
1739 sc->sc_poll_thread = curthread;
1741 sc->sc_flags &= ~HKBD_FLAG_POLLING;
1749 /* local functions */
1752 hkbd_set_leds(struct hkbd_softc *sc, uint8_t leds)
1760 SYSCONS_LOCK_ASSERT();
1761 DPRINTF("leds=0x%02x\n", leds);
1768 memset(sc->sc_buffer, 0, HKBD_BUFFER_SIZE);
1770 id = sc->sc_id_leds;
1773 /* Assumption: All led bits must be in the same ID. */
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);
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);
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);
1793 /* if no leds, nothing to do */
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);
1802 /* check if we need to prefix an ID byte */
1805 sc->sc_buffer[0] = id;
1806 buf = sc->sc_buffer;
1808 buf = sc->sc_buffer + 1;
1811 DPRINTF("len=%d, id=%d\n", len, id);
1813 /* start data transfer */
1815 error = hid_write(sc->sc_dev, buf, len);
1822 hkbd_set_typematic(keyboard_t *kbd, int code)
1824 #ifdef EVDEV_SUPPORT
1825 struct hkbd_softc *sc = kbd->kb_data;
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};
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);
1845 #ifdef HKBD_EMULATE_ATSCANCODE
1847 hkbd_atkeycode(int usbcode, const bitstr_t *bitmap)
1851 keycode = hkbd_trtab[KEY_INDEX(usbcode)];
1854 * Translate Alt-PrintScreen to SysRq.
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.
1862 * The usual usbcode for all PrintScreens is 0x46. hkbd_trtab
1863 * translates this to 0x5c, so the Alt check to classify 0x5c
1866 if ((keycode == 0x5c || keycode == 0x7e) &&
1867 (HKBD_KEY_PRESSED(bitmap, 0xe2 /* ALT-L */) ||
1868 HKBD_KEY_PRESSED(bitmap, 0xe6 /* ALT-R */)))
1874 hkbd_key2scan(struct hkbd_softc *sc, int code, const bitstr_t *bitmap, int up)
1876 static const int scan[] = {
1882 0x137, /* PrintScreen */
1892 0x151, /* PageDown */
1895 0x146, /* Pause/Break */
1896 0x15b, /* Win_L(Super_L) */
1897 0x15c, /* Win_R(Super_R) */
1898 0x15d, /* Application(Menu) */
1900 /* SUN TYPE 6 USB KEYBOARD */
1901 0x168, /* Sun Type 6 Help */
1902 0x15e, /* Sun Type 6 Stop */
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 */
1915 0x11f, /* Sun Type 6 VolumeDown */
1916 0x11e, /* Sun Type 6 VolumeUp */
1917 0x120, /* Sun Type 6 PowerDown */
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) */
1930 if ((code >= 89) && (code < (int)(89 + nitems(scan)))) {
1931 code = scan[code - 89];
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;
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);
1947 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1949 if (code & SCAN_PREFIX) {
1950 if (code & SCAN_PREFIX_CTL) {
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) {
1956 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1957 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1959 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1960 sc->sc_buffered_char[1] = 0;
1962 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1968 #endif /* HKBD_EMULATE_ATSCANCODE */
1970 static keyboard_switch_t hkbdsw = {
1971 .probe = &hkbd__probe,
1975 .test_if = &hkbd_test_if,
1976 .enable = &hkbd_enable,
1977 .disable = &hkbd_disable,
1979 .check = &hkbd_check,
1980 .read_char = &hkbd_read_char,
1981 .check_char = &hkbd_check_char,
1982 .ioctl = &hkbd_ioctl,
1984 .clear_state = &hkbd_clear_state,
1985 .get_state = &hkbd_get_state,
1986 .set_state = &hkbd_set_state,
1990 KEYBOARD_DRIVER(hkbd, hkbdsw, hkbd_configure);
1993 hkbd_driver_load(module_t mod, int what, void *arg)
1997 kbd_add_driver(&hkbd_kbd_driver);
2000 kbd_delete_driver(&hkbd_kbd_driver);
2006 static devclass_t hkbd_devclass;
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),
2017 static driver_t hkbd_driver = {
2019 .methods = hkbd_methods,
2020 .size = sizeof(struct hkbd_softc),
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);
2029 MODULE_VERSION(hkbd, 1);
2030 HID_PNP_INFO(hkbd_devs);