6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
37 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
40 #include "opt_compat.h"
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
63 #include <sys/sched.h>
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69 #include <dev/usb/usbhid.h>
71 #define USB_DEBUG_VAR ukbd_debug
72 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/quirk/usb_quirk.h>
76 #include <sys/ioccom.h>
77 #include <sys/filio.h>
81 #include <dev/kbd/kbdreg.h>
83 /* the initial key map, accent map and fkey strings */
84 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
85 #define KBD_DFLT_KEYMAP
89 /* the following file must be included after "ukbdmap.h" */
90 #include <dev/kbd/kbdtables.h>
93 static int ukbd_debug = 0;
94 static int ukbd_no_leds = 0;
95 static int ukbd_pollrate = 0;
97 static SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB keyboard");
98 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
99 &ukbd_debug, 0, "Debug level");
100 TUNABLE_INT("hw.usb.ukbd.debug", &ukbd_debug);
101 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW | CTLFLAG_TUN,
102 &ukbd_no_leds, 0, "Disables setting of keyboard leds");
103 TUNABLE_INT("hw.usb.ukbd.no_leds", &ukbd_no_leds);
104 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, pollrate, CTLFLAG_RW | CTLFLAG_TUN,
105 &ukbd_pollrate, 0, "Force this polling rate, 1-1000Hz");
106 TUNABLE_INT("hw.usb.ukbd.pollrate", &ukbd_pollrate);
109 #define UKBD_EMULATE_ATSCANCODE 1
110 #define UKBD_DRIVER_NAME "ukbd"
111 #define UKBD_NMOD 8 /* units */
112 #define UKBD_NKEYCODE 6 /* units */
113 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */
114 #define UKBD_IN_BUF_FULL (UKBD_IN_BUF_SIZE / 2) /* bytes */
115 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
116 #define UKBD_BUFFER_SIZE 64 /* bytes */
120 #define MOD_CONTROL_L 0x01
121 #define MOD_CONTROL_R 0x10
122 #define MOD_SHIFT_L 0x02
123 #define MOD_SHIFT_R 0x20
124 #define MOD_ALT_L 0x04
125 #define MOD_ALT_R 0x40
126 #define MOD_WIN_L 0x08
127 #define MOD_WIN_R 0x80
129 #define MOD_EJECT 0x0100
130 #define MOD_FN 0x0200
131 uint8_t keycode[UKBD_NKEYCODE];
143 accentmap_t sc_accmap;
144 fkeytab_t sc_fkeymap[UKBD_NFKEY];
145 struct hid_location sc_loc_apple_eject;
146 struct hid_location sc_loc_apple_fn;
147 struct hid_location sc_loc_ctrl_l;
148 struct hid_location sc_loc_ctrl_r;
149 struct hid_location sc_loc_shift_l;
150 struct hid_location sc_loc_shift_r;
151 struct hid_location sc_loc_alt_l;
152 struct hid_location sc_loc_alt_r;
153 struct hid_location sc_loc_win_l;
154 struct hid_location sc_loc_win_r;
155 struct hid_location sc_loc_events;
156 struct hid_location sc_loc_numlock;
157 struct hid_location sc_loc_capslock;
158 struct hid_location sc_loc_scrolllock;
159 struct usb_callout sc_callout;
160 struct ukbd_data sc_ndata;
161 struct ukbd_data sc_odata;
163 struct thread *sc_poll_thread;
164 struct usb_device *sc_udev;
165 struct usb_interface *sc_iface;
166 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
168 uint32_t sc_ntime[UKBD_NKEYCODE];
169 uint32_t sc_otime[UKBD_NKEYCODE];
170 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
172 uint32_t sc_composed_char; /* composed char code, if non-zero */
173 #ifdef UKBD_EMULATE_ATSCANCODE
174 uint32_t sc_buffered_char[2];
176 uint32_t sc_flags; /* flags */
177 #define UKBD_FLAG_COMPOSE 0x00000001
178 #define UKBD_FLAG_POLLING 0x00000002
179 #define UKBD_FLAG_SET_LEDS 0x00000004
180 #define UKBD_FLAG_ATTACHED 0x00000010
181 #define UKBD_FLAG_GONE 0x00000020
183 #define UKBD_FLAG_HID_MASK 0x003fffc0
184 #define UKBD_FLAG_APPLE_EJECT 0x00000040
185 #define UKBD_FLAG_APPLE_FN 0x00000080
186 #define UKBD_FLAG_APPLE_SWAP 0x00000100
187 #define UKBD_FLAG_TIMER_RUNNING 0x00000200
188 #define UKBD_FLAG_CTRL_L 0x00000400
189 #define UKBD_FLAG_CTRL_R 0x00000800
190 #define UKBD_FLAG_SHIFT_L 0x00001000
191 #define UKBD_FLAG_SHIFT_R 0x00002000
192 #define UKBD_FLAG_ALT_L 0x00004000
193 #define UKBD_FLAG_ALT_R 0x00008000
194 #define UKBD_FLAG_WIN_L 0x00010000
195 #define UKBD_FLAG_WIN_R 0x00020000
196 #define UKBD_FLAG_EVENTS 0x00040000
197 #define UKBD_FLAG_NUMLOCK 0x00080000
198 #define UKBD_FLAG_CAPSLOCK 0x00100000
199 #define UKBD_FLAG_SCROLLLOCK 0x00200000
201 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
202 int sc_state; /* shift/lock key state */
203 int sc_accents; /* accent key index (> 0) */
208 uint16_t sc_inputhead;
209 uint16_t sc_inputtail;
210 uint16_t sc_modifiers;
212 uint8_t sc_leds; /* store for async led requests */
213 uint8_t sc_iface_index;
215 uint8_t sc_id_apple_eject;
216 uint8_t sc_id_apple_fn;
217 uint8_t sc_id_ctrl_l;
218 uint8_t sc_id_ctrl_r;
219 uint8_t sc_id_shift_l;
220 uint8_t sc_id_shift_r;
226 uint8_t sc_id_numlock;
227 uint8_t sc_id_capslock;
228 uint8_t sc_id_scrolllock;
229 uint8_t sc_id_events;
232 uint8_t sc_buffer[UKBD_BUFFER_SIZE];
235 #define KEY_ERROR 0x01
238 #define KEY_RELEASE 0x400
239 #define KEY_INDEX(c) ((c) & 0xFF)
242 #define SCAN_RELEASE 0x80
243 #define SCAN_PREFIX_E0 0x100
244 #define SCAN_PREFIX_E1 0x200
245 #define SCAN_PREFIX_CTL 0x400
246 #define SCAN_PREFIX_SHIFT 0x800
247 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
248 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
249 #define SCAN_CHAR(c) ((c) & 0x7f)
251 #define UKBD_LOCK() mtx_lock(&Giant)
252 #define UKBD_UNLOCK() mtx_unlock(&Giant)
257 * Assert that the lock is held in all contexts
258 * where the code can be executed.
260 #define UKBD_LOCK_ASSERT() mtx_assert(&Giant, MA_OWNED)
263 * Assert that the lock is held in the contexts
264 * where it really has to be so.
266 #define UKBD_CTX_LOCK_ASSERT() \
268 if (!kdb_active && panicstr == NULL) \
269 mtx_assert(&Giant, MA_OWNED); \
273 #define UKBD_LOCK_ASSERT() (void)0
274 #define UKBD_CTX_LOCK_ASSERT() (void)0
282 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
283 {MOD_CONTROL_L, 0xe0},
284 {MOD_CONTROL_R, 0xe4},
293 #define NN 0 /* no translation */
295 * Translate USB keycodes to AT keyboard scancodes.
298 * FIXME: Mac USB keyboard generates:
299 * 0x53: keypad NumLock/Clear
306 static const uint8_t ukbd_trtab[256] = {
307 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
308 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
309 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
310 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
311 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
312 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
313 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
314 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
315 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
316 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
317 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
318 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
319 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
320 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
321 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
322 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
323 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */
324 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */
325 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
326 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
327 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
328 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
329 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
330 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
331 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
332 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
333 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
334 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
335 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
336 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
337 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
338 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
341 static const uint8_t ukbd_boot_desc[] = {
342 0x05, 0x01, 0x09, 0x06, 0xa1,
343 0x01, 0x05, 0x07, 0x19, 0xe0,
344 0x29, 0xe7, 0x15, 0x00, 0x25,
345 0x01, 0x75, 0x01, 0x95, 0x08,
346 0x81, 0x02, 0x95, 0x01, 0x75,
347 0x08, 0x81, 0x01, 0x95, 0x03,
348 0x75, 0x01, 0x05, 0x08, 0x19,
349 0x01, 0x29, 0x03, 0x91, 0x02,
350 0x95, 0x05, 0x75, 0x01, 0x91,
351 0x01, 0x95, 0x06, 0x75, 0x08,
352 0x15, 0x00, 0x26, 0xff, 0x00,
353 0x05, 0x07, 0x19, 0x00, 0x2a,
354 0xff, 0x00, 0x81, 0x00, 0xc0
358 static void ukbd_timeout(void *);
359 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
360 static int ukbd_set_typematic(keyboard_t *, int);
361 #ifdef UKBD_EMULATE_ATSCANCODE
362 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
364 static uint32_t ukbd_read_char(keyboard_t *, int);
365 static void ukbd_clear_state(keyboard_t *);
366 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
367 static int ukbd_enable(keyboard_t *);
368 static int ukbd_disable(keyboard_t *);
369 static void ukbd_interrupt(struct ukbd_softc *);
370 static void ukbd_event_keyinput(struct ukbd_softc *);
372 static device_probe_t ukbd_probe;
373 static device_attach_t ukbd_attach;
374 static device_detach_t ukbd_detach;
375 static device_resume_t ukbd_resume;
378 ukbd_any_key_pressed(struct ukbd_softc *sc)
383 for (j = i = 0; i < UKBD_NKEYCODE; i++)
384 j |= sc->sc_odata.keycode[i];
390 ukbd_start_timer(struct ukbd_softc *sc)
392 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
393 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
397 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
400 UKBD_CTX_LOCK_ASSERT();
402 DPRINTF("0x%02x (%d) %s\n", key, key,
403 (key & KEY_RELEASE) ? "released" : "pressed");
405 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
406 sc->sc_input[sc->sc_inputtail] = key;
408 ++(sc->sc_inputtail);
409 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
410 sc->sc_inputtail = 0;
413 DPRINTF("input buffer is full\n");
418 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
421 UKBD_CTX_LOCK_ASSERT();
422 KASSERT((sc->sc_flags & UKBD_FLAG_POLLING) != 0,
423 ("ukbd_do_poll called when not polling\n"));
424 DPRINTFN(2, "polling\n");
426 if (!kdb_active && !SCHEDULER_STOPPED()) {
428 * In this context the kernel is polling for input,
429 * but the USB subsystem works in normal interrupt-driven
430 * mode, so we just wait on the USB threads to do the job.
431 * Note that we currently hold the Giant, but it's also used
432 * as the transfer mtx, so we must release it while waiting.
434 while (sc->sc_inputs == 0) {
436 * Give USB threads a chance to run. Note that
437 * kern_yield performs DROP_GIANT + PICKUP_GIANT.
439 kern_yield(PRI_UNCHANGED);
446 while (sc->sc_inputs == 0) {
448 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
450 /* Delay-optimised support for repetition of keys */
451 if (ukbd_any_key_pressed(sc)) {
452 /* a key is pressed - need timekeeping */
455 /* 1 millisecond has passed */
467 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
471 UKBD_CTX_LOCK_ASSERT();
472 KASSERT((!kdb_active && !SCHEDULER_STOPPED())
473 || (sc->sc_flags & UKBD_FLAG_POLLING) != 0,
474 ("not polling in kdb or panic\n"));
476 if (sc->sc_inputs == 0) {
477 /* start transfer, if not already started */
478 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
481 if (sc->sc_flags & UKBD_FLAG_POLLING)
482 ukbd_do_poll(sc, wait);
484 if (sc->sc_inputs == 0) {
487 c = sc->sc_input[sc->sc_inputhead];
489 ++(sc->sc_inputhead);
490 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
491 sc->sc_inputhead = 0;
498 ukbd_interrupt(struct ukbd_softc *sc)
502 uint32_t now = sc->sc_time_ms;
508 UKBD_CTX_LOCK_ASSERT();
510 if (sc->sc_ndata.keycode[0] == KEY_ERROR)
513 n_mod = sc->sc_ndata.modifiers;
514 o_mod = sc->sc_odata.modifiers;
515 if (n_mod != o_mod) {
516 for (i = 0; i < UKBD_NMOD; i++) {
517 if ((n_mod & ukbd_mods[i].mask) !=
518 (o_mod & ukbd_mods[i].mask)) {
519 ukbd_put_key(sc, ukbd_mods[i].key |
520 ((n_mod & ukbd_mods[i].mask) ?
521 KEY_PRESS : KEY_RELEASE));
525 /* Check for released keys. */
526 for (i = 0; i < UKBD_NKEYCODE; i++) {
527 key = sc->sc_odata.keycode[i];
531 for (j = 0; j < UKBD_NKEYCODE; j++) {
532 if (sc->sc_ndata.keycode[j] == 0) {
535 if (key == sc->sc_ndata.keycode[j]) {
539 ukbd_put_key(sc, key | KEY_RELEASE);
543 /* Check for pressed keys. */
544 for (i = 0; i < UKBD_NKEYCODE; i++) {
545 key = sc->sc_ndata.keycode[i];
549 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
550 for (j = 0; j < UKBD_NKEYCODE; j++) {
551 if (sc->sc_odata.keycode[j] == 0) {
554 if (key == sc->sc_odata.keycode[j]) {
556 /* key is still pressed */
558 sc->sc_ntime[i] = sc->sc_otime[j];
559 dtime = (sc->sc_otime[j] - now);
561 if (!(dtime & 0x80000000)) {
562 /* time has not elapsed */
565 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
569 ukbd_put_key(sc, key | KEY_PRESS);
572 * If any other key is presently down, force its repeat to be
573 * well in the future (100s). This makes the last key to be
574 * pressed do the autorepeat.
576 for (j = 0; j != UKBD_NKEYCODE; j++) {
578 sc->sc_ntime[j] = now + (100 * 1000);
583 sc->sc_odata = sc->sc_ndata;
585 memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
587 ukbd_event_keyinput(sc);
591 ukbd_event_keyinput(struct ukbd_softc *sc)
595 UKBD_CTX_LOCK_ASSERT();
597 if ((sc->sc_flags & UKBD_FLAG_POLLING) != 0)
600 if (sc->sc_inputs == 0)
603 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
604 KBD_IS_BUSY(&sc->sc_kbd)) {
605 /* let the callback function process the input */
606 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
607 sc->sc_kbd.kb_callback.kc_arg);
609 /* read and discard the input, no one is waiting for it */
611 c = ukbd_read_char(&sc->sc_kbd, 0);
612 } while (c != NOKEY);
617 ukbd_timeout(void *arg)
619 struct ukbd_softc *sc = arg;
623 sc->sc_time_ms += 25; /* milliseconds */
627 /* Make sure any leftover key events gets read out */
628 ukbd_event_keyinput(sc);
630 if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
631 ukbd_start_timer(sc);
633 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
638 ukbd_apple_fn(uint8_t keycode) {
640 case 0x28: return 0x49; /* RETURN -> INSERT */
641 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
642 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
643 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
644 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
645 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
646 default: return keycode;
651 ukbd_apple_swap(uint8_t keycode) {
653 case 0x35: return 0x64;
654 case 0x64: return 0x35;
655 default: return keycode;
660 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
662 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
663 struct usb_page_cache *pc;
671 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
672 pc = usbd_xfer_get_frame(xfer, 0);
674 switch (USB_GET_STATE(xfer)) {
675 case USB_ST_TRANSFERRED:
676 DPRINTF("actlen=%d bytes\n", len);
679 DPRINTF("zero length data\n");
683 if (sc->sc_kbd_id != 0) {
684 /* check and remove HID ID byte */
685 usbd_copy_out(pc, 0, &id, 1);
689 DPRINTF("zero length data\n");
697 if (len > UKBD_BUFFER_SIZE)
698 len = UKBD_BUFFER_SIZE;
701 usbd_copy_out(pc, offset, sc->sc_buffer, len);
703 /* clear temporary storage */
704 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
706 /* scan through HID data */
707 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
708 (id == sc->sc_id_apple_eject)) {
709 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject))
710 sc->sc_modifiers |= MOD_EJECT;
712 sc->sc_modifiers &= ~MOD_EJECT;
714 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
715 (id == sc->sc_id_apple_fn)) {
716 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn))
717 sc->sc_modifiers |= MOD_FN;
719 sc->sc_modifiers &= ~MOD_FN;
721 if ((sc->sc_flags & UKBD_FLAG_CTRL_L) &&
722 (id == sc->sc_id_ctrl_l)) {
723 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_l))
724 sc-> sc_modifiers |= MOD_CONTROL_L;
726 sc-> sc_modifiers &= ~MOD_CONTROL_L;
728 if ((sc->sc_flags & UKBD_FLAG_CTRL_R) &&
729 (id == sc->sc_id_ctrl_r)) {
730 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_r))
731 sc->sc_modifiers |= MOD_CONTROL_R;
733 sc->sc_modifiers &= ~MOD_CONTROL_R;
735 if ((sc->sc_flags & UKBD_FLAG_SHIFT_L) &&
736 (id == sc->sc_id_shift_l)) {
737 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_l))
738 sc->sc_modifiers |= MOD_SHIFT_L;
740 sc->sc_modifiers &= ~MOD_SHIFT_L;
742 if ((sc->sc_flags & UKBD_FLAG_SHIFT_R) &&
743 (id == sc->sc_id_shift_r)) {
744 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_r))
745 sc->sc_modifiers |= MOD_SHIFT_R;
747 sc->sc_modifiers &= ~MOD_SHIFT_R;
749 if ((sc->sc_flags & UKBD_FLAG_ALT_L) &&
750 (id == sc->sc_id_alt_l)) {
751 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_l))
752 sc->sc_modifiers |= MOD_ALT_L;
754 sc->sc_modifiers &= ~MOD_ALT_L;
756 if ((sc->sc_flags & UKBD_FLAG_ALT_R) &&
757 (id == sc->sc_id_alt_r)) {
758 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_r))
759 sc->sc_modifiers |= MOD_ALT_R;
761 sc->sc_modifiers &= ~MOD_ALT_R;
763 if ((sc->sc_flags & UKBD_FLAG_WIN_L) &&
764 (id == sc->sc_id_win_l)) {
765 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_l))
766 sc->sc_modifiers |= MOD_WIN_L;
768 sc->sc_modifiers &= ~MOD_WIN_L;
770 if ((sc->sc_flags & UKBD_FLAG_WIN_R) &&
771 (id == sc->sc_id_win_r)) {
772 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_r))
773 sc->sc_modifiers |= MOD_WIN_R;
775 sc->sc_modifiers &= ~MOD_WIN_R;
778 sc->sc_ndata.modifiers = sc->sc_modifiers;
780 if ((sc->sc_flags & UKBD_FLAG_EVENTS) &&
781 (id == sc->sc_id_events)) {
782 i = sc->sc_loc_events.count;
783 if (i > UKBD_NKEYCODE)
788 sc->sc_ndata.keycode[i] =
789 hid_get_data(sc->sc_buffer + i, len - i,
795 DPRINTF("modifiers = 0x%04x\n", (int)sc->sc_modifiers);
796 for (i = 0; i < UKBD_NKEYCODE; i++) {
797 if (sc->sc_ndata.keycode[i]) {
798 DPRINTF("[%d] = 0x%02x\n",
799 (int)i, (int)sc->sc_ndata.keycode[i]);
803 if (sc->sc_modifiers & MOD_FN) {
804 for (i = 0; i < UKBD_NKEYCODE; i++) {
805 sc->sc_ndata.keycode[i] =
806 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
810 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
811 for (i = 0; i < UKBD_NKEYCODE; i++) {
812 sc->sc_ndata.keycode[i] =
813 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
819 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
820 if (ukbd_any_key_pressed(sc)) {
821 ukbd_start_timer(sc);
827 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
828 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
829 usbd_transfer_submit(xfer);
831 DPRINTF("input queue is full!\n");
836 DPRINTF("error=%s\n", usbd_errstr(error));
838 if (error != USB_ERR_CANCELLED) {
839 /* try to clear stall first */
840 usbd_xfer_set_stall(xfer);
848 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
850 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
851 struct usb_device_request req;
852 struct usb_page_cache *pc;
864 switch (USB_GET_STATE(xfer)) {
865 case USB_ST_TRANSFERRED:
867 if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS))
869 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
871 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
872 req.bRequest = UR_SET_REPORT;
873 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
874 req.wIndex[0] = sc->sc_iface_no;
878 memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE);
883 /* Assumption: All led bits must be in the same ID. */
885 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) {
886 if (sc->sc_leds & NLKED) {
887 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
888 &sc->sc_loc_numlock, 1);
890 id = sc->sc_id_numlock;
894 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) {
895 if (sc->sc_leds & SLKED) {
896 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
897 &sc->sc_loc_scrolllock, 1);
899 id = sc->sc_id_scrolllock;
903 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) {
904 if (sc->sc_leds & CLKED) {
905 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
906 &sc->sc_loc_capslock, 1);
908 id = sc->sc_id_capslock;
912 /* if no leds, nothing to do */
916 /* range check output report length */
917 len = sc->sc_led_size;
918 if (len > (UKBD_BUFFER_SIZE - 1))
919 len = (UKBD_BUFFER_SIZE - 1);
921 /* check if we need to prefix an ID byte */
922 sc->sc_buffer[0] = id;
924 pc = usbd_xfer_get_frame(xfer, 1);
927 usbd_copy_in(pc, 0, sc->sc_buffer, len);
929 usbd_copy_in(pc, 0, sc->sc_buffer + 1, len);
931 req.wLength[0] = len;
932 usbd_xfer_set_frame_len(xfer, 1, len);
934 DPRINTF("len=%d, id=%d\n", len, id);
936 /* setup control request last */
937 pc = usbd_xfer_get_frame(xfer, 0);
938 usbd_copy_in(pc, 0, &req, sizeof(req));
939 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
941 /* start data transfer */
942 usbd_xfer_set_frames(xfer, 2);
943 usbd_transfer_submit(xfer);
947 DPRINTFN(1, "error=%s\n", usbd_errstr(error));
952 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
955 .type = UE_INTERRUPT,
956 .endpoint = UE_ADDR_ANY,
957 .direction = UE_DIR_IN,
958 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
959 .bufsize = 0, /* use wMaxPacketSize */
960 .callback = &ukbd_intr_callback,
965 .endpoint = 0x00, /* Control pipe */
966 .direction = UE_DIR_ANY,
967 .bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE,
968 .callback = &ukbd_set_leds_callback,
969 .timeout = 1000, /* 1 second */
973 /* A match on these entries will load ukbd */
974 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = {
975 {USB_IFACE_CLASS(UICLASS_HID),
976 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
977 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),},
981 ukbd_probe(device_t dev)
983 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
984 struct usb_attach_arg *uaa = device_get_ivars(dev);
995 if (uaa->usb_mode != USB_MODE_HOST) {
999 if (uaa->info.bInterfaceClass != UICLASS_HID)
1002 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1005 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
1006 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD))
1007 return (BUS_PROBE_DEFAULT);
1009 error = usbd_req_get_hid_desc(uaa->device, NULL,
1010 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
1015 if (hid_is_keyboard(d_ptr, d_len)) {
1016 if (hid_is_mouse(d_ptr, d_len)) {
1018 * NOTE: We currently don't support USB mouse
1019 * and USB keyboard on the same USB endpoint.
1020 * Let "ums" driver win.
1024 error = BUS_PROBE_DEFAULT;
1029 free(d_ptr, M_TEMP);
1034 ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len)
1038 /* reset detected bits */
1039 sc->sc_flags &= ~UKBD_FLAG_HID_MASK;
1041 /* check if there is an ID byte */
1042 sc->sc_kbd_size = hid_report_size(ptr, len,
1043 hid_input, &sc->sc_kbd_id);
1045 /* investigate if this is an Apple Keyboard */
1046 if (hid_locate(ptr, len,
1047 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
1048 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
1049 &sc->sc_id_apple_eject)) {
1050 if (flags & HIO_VARIABLE)
1051 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
1052 UKBD_FLAG_APPLE_SWAP;
1053 DPRINTFN(1, "Found Apple eject-key\n");
1055 if (hid_locate(ptr, len,
1056 HID_USAGE2(0xFFFF, 0x0003),
1057 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
1058 &sc->sc_id_apple_fn)) {
1059 if (flags & HIO_VARIABLE)
1060 sc->sc_flags |= UKBD_FLAG_APPLE_FN;
1061 DPRINTFN(1, "Found Apple FN-key\n");
1063 /* figure out some keys */
1064 if (hid_locate(ptr, len,
1065 HID_USAGE2(HUP_KEYBOARD, 0xE0),
1066 hid_input, 0, &sc->sc_loc_ctrl_l, &flags,
1067 &sc->sc_id_ctrl_l)) {
1068 if (flags & HIO_VARIABLE)
1069 sc->sc_flags |= UKBD_FLAG_CTRL_L;
1070 DPRINTFN(1, "Found left control\n");
1072 if (hid_locate(ptr, len,
1073 HID_USAGE2(HUP_KEYBOARD, 0xE4),
1074 hid_input, 0, &sc->sc_loc_ctrl_r, &flags,
1075 &sc->sc_id_ctrl_r)) {
1076 if (flags & HIO_VARIABLE)
1077 sc->sc_flags |= UKBD_FLAG_CTRL_R;
1078 DPRINTFN(1, "Found right control\n");
1080 if (hid_locate(ptr, len,
1081 HID_USAGE2(HUP_KEYBOARD, 0xE1),
1082 hid_input, 0, &sc->sc_loc_shift_l, &flags,
1083 &sc->sc_id_shift_l)) {
1084 if (flags & HIO_VARIABLE)
1085 sc->sc_flags |= UKBD_FLAG_SHIFT_L;
1086 DPRINTFN(1, "Found left shift\n");
1088 if (hid_locate(ptr, len,
1089 HID_USAGE2(HUP_KEYBOARD, 0xE5),
1090 hid_input, 0, &sc->sc_loc_shift_r, &flags,
1091 &sc->sc_id_shift_r)) {
1092 if (flags & HIO_VARIABLE)
1093 sc->sc_flags |= UKBD_FLAG_SHIFT_R;
1094 DPRINTFN(1, "Found right shift\n");
1096 if (hid_locate(ptr, len,
1097 HID_USAGE2(HUP_KEYBOARD, 0xE2),
1098 hid_input, 0, &sc->sc_loc_alt_l, &flags,
1099 &sc->sc_id_alt_l)) {
1100 if (flags & HIO_VARIABLE)
1101 sc->sc_flags |= UKBD_FLAG_ALT_L;
1102 DPRINTFN(1, "Found left alt\n");
1104 if (hid_locate(ptr, len,
1105 HID_USAGE2(HUP_KEYBOARD, 0xE6),
1106 hid_input, 0, &sc->sc_loc_alt_r, &flags,
1107 &sc->sc_id_alt_r)) {
1108 if (flags & HIO_VARIABLE)
1109 sc->sc_flags |= UKBD_FLAG_ALT_R;
1110 DPRINTFN(1, "Found right alt\n");
1112 if (hid_locate(ptr, len,
1113 HID_USAGE2(HUP_KEYBOARD, 0xE3),
1114 hid_input, 0, &sc->sc_loc_win_l, &flags,
1115 &sc->sc_id_win_l)) {
1116 if (flags & HIO_VARIABLE)
1117 sc->sc_flags |= UKBD_FLAG_WIN_L;
1118 DPRINTFN(1, "Found left GUI\n");
1120 if (hid_locate(ptr, len,
1121 HID_USAGE2(HUP_KEYBOARD, 0xE7),
1122 hid_input, 0, &sc->sc_loc_win_r, &flags,
1123 &sc->sc_id_win_r)) {
1124 if (flags & HIO_VARIABLE)
1125 sc->sc_flags |= UKBD_FLAG_WIN_R;
1126 DPRINTFN(1, "Found right GUI\n");
1128 /* figure out event buffer */
1129 if (hid_locate(ptr, len,
1130 HID_USAGE2(HUP_KEYBOARD, 0x00),
1131 hid_input, 0, &sc->sc_loc_events, &flags,
1132 &sc->sc_id_events)) {
1133 if (flags & HIO_VARIABLE) {
1134 DPRINTFN(1, "Ignoring keyboard event control\n");
1136 sc->sc_flags |= UKBD_FLAG_EVENTS;
1137 DPRINTFN(1, "Found keyboard event array\n");
1141 /* figure out leds on keyboard */
1142 sc->sc_led_size = hid_report_size(ptr, len,
1145 if (hid_locate(ptr, len,
1146 HID_USAGE2(HUP_LEDS, 0x01),
1147 hid_output, 0, &sc->sc_loc_numlock, &flags,
1148 &sc->sc_id_numlock)) {
1149 if (flags & HIO_VARIABLE)
1150 sc->sc_flags |= UKBD_FLAG_NUMLOCK;
1151 DPRINTFN(1, "Found keyboard numlock\n");
1153 if (hid_locate(ptr, len,
1154 HID_USAGE2(HUP_LEDS, 0x02),
1155 hid_output, 0, &sc->sc_loc_capslock, &flags,
1156 &sc->sc_id_capslock)) {
1157 if (flags & HIO_VARIABLE)
1158 sc->sc_flags |= UKBD_FLAG_CAPSLOCK;
1159 DPRINTFN(1, "Found keyboard capslock\n");
1161 if (hid_locate(ptr, len,
1162 HID_USAGE2(HUP_LEDS, 0x03),
1163 hid_output, 0, &sc->sc_loc_scrolllock, &flags,
1164 &sc->sc_id_scrolllock)) {
1165 if (flags & HIO_VARIABLE)
1166 sc->sc_flags |= UKBD_FLAG_SCROLLLOCK;
1167 DPRINTFN(1, "Found keyboard scrolllock\n");
1172 ukbd_attach(device_t dev)
1174 struct ukbd_softc *sc = device_get_softc(dev);
1175 struct usb_attach_arg *uaa = device_get_ivars(dev);
1176 int unit = device_get_unit(dev);
1177 keyboard_t *kbd = &sc->sc_kbd;
1178 void *hid_ptr = NULL;
1187 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
1189 kbd->kb_data = (void *)sc;
1191 device_set_usb_desc(dev);
1193 sc->sc_udev = uaa->device;
1194 sc->sc_iface = uaa->iface;
1195 sc->sc_iface_index = uaa->info.bIfaceIndex;
1196 sc->sc_iface_no = uaa->info.bIfaceNum;
1197 sc->sc_mode = K_XLATE;
1199 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
1201 err = usbd_transfer_setup(uaa->device,
1202 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
1203 UKBD_N_TRANSFER, sc, &Giant);
1206 DPRINTF("error=%s\n", usbd_errstr(err));
1209 /* setup default keyboard maps */
1211 sc->sc_keymap = key_map;
1212 sc->sc_accmap = accent_map;
1213 for (n = 0; n < UKBD_NFKEY; n++) {
1214 sc->sc_fkeymap[n] = fkey_tab[n];
1217 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
1218 sc->sc_fkeymap, UKBD_NFKEY);
1220 KBD_FOUND_DEVICE(kbd);
1222 ukbd_clear_state(kbd);
1225 * FIXME: set the initial value for lock keys in "sc_state"
1226 * according to the BIOS data?
1228 KBD_PROBE_DONE(kbd);
1230 /* get HID descriptor */
1231 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
1232 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
1235 DPRINTF("Parsing HID descriptor of %d bytes\n",
1238 ukbd_parse_hid(sc, hid_ptr, hid_len);
1240 free(hid_ptr, M_TEMP);
1243 /* check if we should use the boot protocol */
1244 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) ||
1245 (err != 0) || (!(sc->sc_flags & UKBD_FLAG_EVENTS))) {
1247 DPRINTF("Forcing boot protocol\n");
1249 err = usbd_req_set_protocol(sc->sc_udev, NULL,
1250 sc->sc_iface_index, 0);
1253 DPRINTF("Set protocol error=%s (ignored)\n",
1257 ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc));
1260 /* ignore if SETIDLE fails, hence it is not crucial */
1261 usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
1263 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
1267 if (kbd_register(kbd) < 0) {
1270 KBD_CONFIG_DONE(kbd);
1274 #ifdef KBD_INSTALL_CDEV
1275 if (kbd_attach(kbd)) {
1279 sc->sc_flags |= UKBD_FLAG_ATTACHED;
1282 genkbd_diag(kbd, bootverbose);
1286 /* check for polling rate override */
1287 rate = ukbd_pollrate;
1294 /* set new polling interval in ms */
1295 usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT], rate);
1298 /* start the keyboard */
1299 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
1301 return (0); /* success */
1305 return (ENXIO); /* error */
1309 ukbd_detach(device_t dev)
1311 struct ukbd_softc *sc = device_get_softc(dev);
1318 sc->sc_flags |= UKBD_FLAG_GONE;
1320 usb_callout_stop(&sc->sc_callout);
1322 ukbd_disable(&sc->sc_kbd);
1324 #ifdef KBD_INSTALL_CDEV
1325 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1326 error = kbd_detach(&sc->sc_kbd);
1328 /* usb attach cannot return an error */
1329 device_printf(dev, "WARNING: kbd_detach() "
1330 "returned non-zero! (ignored)\n");
1334 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
1335 error = kbd_unregister(&sc->sc_kbd);
1337 /* usb attach cannot return an error */
1338 device_printf(dev, "WARNING: kbd_unregister() "
1339 "returned non-zero! (ignored)\n");
1342 sc->sc_kbd.kb_flags = 0;
1344 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1346 usb_callout_drain(&sc->sc_callout);
1348 DPRINTF("%s: disconnected\n",
1349 device_get_nameunit(dev));
1355 ukbd_resume(device_t dev)
1357 struct ukbd_softc *sc = device_get_softc(dev);
1361 ukbd_clear_state(&sc->sc_kbd);
1366 /* early keyboard probe, not supported */
1368 ukbd_configure(int flags)
1373 /* detect a keyboard, not used */
1375 ukbd__probe(int unit, void *arg, int flags)
1380 /* reset and initialize the device, not used */
1382 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1387 /* test the interface to the device, not used */
1389 ukbd_test_if(keyboard_t *kbd)
1394 /* finish using this keyboard, not used */
1396 ukbd_term(keyboard_t *kbd)
1401 /* keyboard interrupt routine, not used */
1403 ukbd_intr(keyboard_t *kbd, void *arg)
1408 /* lock the access to the keyboard, not used */
1410 ukbd_lock(keyboard_t *kbd, int lock)
1416 * Enable the access to the device; until this function is called,
1417 * the client cannot read from the keyboard.
1420 ukbd_enable(keyboard_t *kbd)
1430 /* disallow the access to the device */
1432 ukbd_disable(keyboard_t *kbd)
1436 KBD_DEACTIVATE(kbd);
1442 /* check if data is waiting */
1443 /* Currently unused. */
1445 ukbd_check(keyboard_t *kbd)
1447 struct ukbd_softc *sc = kbd->kb_data;
1449 UKBD_CTX_LOCK_ASSERT();
1451 if (!KBD_IS_ACTIVE(kbd))
1454 if (sc->sc_flags & UKBD_FLAG_POLLING)
1455 ukbd_do_poll(sc, 0);
1457 #ifdef UKBD_EMULATE_ATSCANCODE
1458 if (sc->sc_buffered_char[0]) {
1462 if (sc->sc_inputs > 0) {
1468 /* check if char is waiting */
1470 ukbd_check_char_locked(keyboard_t *kbd)
1472 struct ukbd_softc *sc = kbd->kb_data;
1474 UKBD_CTX_LOCK_ASSERT();
1476 if (!KBD_IS_ACTIVE(kbd))
1479 if ((sc->sc_composed_char > 0) &&
1480 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1483 return (ukbd_check(kbd));
1487 ukbd_check_char(keyboard_t *kbd)
1492 result = ukbd_check_char_locked(kbd);
1498 /* read one byte from the keyboard if it's allowed */
1499 /* Currently unused. */
1501 ukbd_read(keyboard_t *kbd, int wait)
1503 struct ukbd_softc *sc = kbd->kb_data;
1505 #ifdef UKBD_EMULATE_ATSCANCODE
1511 UKBD_CTX_LOCK_ASSERT();
1513 if (!KBD_IS_ACTIVE(kbd))
1516 #ifdef UKBD_EMULATE_ATSCANCODE
1517 if (sc->sc_buffered_char[0]) {
1518 scancode = sc->sc_buffered_char[0];
1519 if (scancode & SCAN_PREFIX) {
1520 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1521 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1523 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1524 sc->sc_buffered_char[1] = 0;
1527 #endif /* UKBD_EMULATE_ATSCANCODE */
1530 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1531 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1536 #ifdef UKBD_EMULATE_ATSCANCODE
1537 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1538 if (keycode == NN) {
1541 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1542 (usbcode & KEY_RELEASE)));
1543 #else /* !UKBD_EMULATE_ATSCANCODE */
1545 #endif /* UKBD_EMULATE_ATSCANCODE */
1548 /* read char from the keyboard */
1550 ukbd_read_char_locked(keyboard_t *kbd, int wait)
1552 struct ukbd_softc *sc = kbd->kb_data;
1556 #ifdef UKBD_EMULATE_ATSCANCODE
1560 UKBD_CTX_LOCK_ASSERT();
1562 if (!KBD_IS_ACTIVE(kbd))
1567 /* do we have a composed char to return ? */
1569 if ((sc->sc_composed_char > 0) &&
1570 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1572 action = sc->sc_composed_char;
1573 sc->sc_composed_char = 0;
1575 if (action > 0xFF) {
1580 #ifdef UKBD_EMULATE_ATSCANCODE
1582 /* do we have a pending raw scan code? */
1584 if (sc->sc_mode == K_RAW) {
1585 scancode = sc->sc_buffered_char[0];
1587 if (scancode & SCAN_PREFIX) {
1588 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1589 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1591 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1592 sc->sc_buffered_char[1] = 0;
1596 #endif /* UKBD_EMULATE_ATSCANCODE */
1598 /* see if there is something in the keyboard port */
1600 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1601 if (usbcode == -1) {
1606 #ifdef UKBD_EMULATE_ATSCANCODE
1607 /* USB key index -> key code -> AT scan code */
1608 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1609 if (keycode == NN) {
1612 /* return an AT scan code for the K_RAW mode */
1613 if (sc->sc_mode == K_RAW) {
1614 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1615 (usbcode & KEY_RELEASE)));
1617 #else /* !UKBD_EMULATE_ATSCANCODE */
1619 /* return the byte as is for the K_RAW mode */
1620 if (sc->sc_mode == K_RAW) {
1623 /* USB key index -> key code */
1624 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1625 if (keycode == NN) {
1628 #endif /* UKBD_EMULATE_ATSCANCODE */
1631 case 0x38: /* left alt (compose key) */
1632 if (usbcode & KEY_RELEASE) {
1633 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1634 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1636 if (sc->sc_composed_char > 0xFF) {
1637 sc->sc_composed_char = 0;
1641 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1642 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1643 sc->sc_composed_char = 0;
1647 /* XXX: I don't like these... */
1648 case 0x5c: /* print screen */
1649 if (sc->sc_flags & ALTS) {
1650 keycode = 0x54; /* sysrq */
1653 case 0x68: /* pause/break */
1654 if (sc->sc_flags & CTLS) {
1655 keycode = 0x6c; /* break */
1660 /* return the key code in the K_CODE mode */
1661 if (usbcode & KEY_RELEASE) {
1662 keycode |= SCAN_RELEASE;
1664 if (sc->sc_mode == K_CODE) {
1667 /* compose a character code */
1668 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1670 /* key pressed, process it */
1673 case 0x49: /* keypad 7,8,9 */
1674 sc->sc_composed_char *= 10;
1675 sc->sc_composed_char += keycode - 0x40;
1676 goto check_composed;
1680 case 0x4D: /* keypad 4,5,6 */
1681 sc->sc_composed_char *= 10;
1682 sc->sc_composed_char += keycode - 0x47;
1683 goto check_composed;
1687 case 0x51: /* keypad 1,2,3 */
1688 sc->sc_composed_char *= 10;
1689 sc->sc_composed_char += keycode - 0x4E;
1690 goto check_composed;
1692 case 0x52: /* keypad 0 */
1693 sc->sc_composed_char *= 10;
1694 goto check_composed;
1696 /* key released, no interest here */
1697 case SCAN_RELEASE | 0x47:
1698 case SCAN_RELEASE | 0x48:
1699 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1700 case SCAN_RELEASE | 0x4B:
1701 case SCAN_RELEASE | 0x4C:
1702 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1703 case SCAN_RELEASE | 0x4F:
1704 case SCAN_RELEASE | 0x50:
1705 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1706 case SCAN_RELEASE | 0x52: /* keypad 0 */
1709 case 0x38: /* left alt key */
1713 if (sc->sc_composed_char > 0) {
1714 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1715 sc->sc_composed_char = 0;
1721 /* keycode to key action */
1722 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1723 (keycode & SCAN_RELEASE),
1724 &sc->sc_state, &sc->sc_accents);
1725 if (action == NOKEY) {
1732 if (sc->sc_composed_char <= 0xFF) {
1739 /* Currently wait is always false. */
1741 ukbd_read_char(keyboard_t *kbd, int wait)
1746 keycode = ukbd_read_char_locked(kbd, wait);
1752 /* some useful control functions */
1754 ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
1756 struct ukbd_softc *sc = kbd->kb_data;
1758 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1759 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1767 case KDGKBMODE: /* get keyboard mode */
1768 *(int *)arg = sc->sc_mode;
1770 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1771 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1773 ival = IOCPARM_IVAL(arg);
1774 arg = (caddr_t)&ival;
1777 case KDSKBMODE: /* set keyboard mode */
1778 switch (*(int *)arg) {
1780 if (sc->sc_mode != K_XLATE) {
1781 /* make lock key state and LED state match */
1782 sc->sc_state &= ~LOCK_MASK;
1783 sc->sc_state |= KBD_LED_VAL(kbd);
1788 if (sc->sc_mode != *(int *)arg) {
1789 if ((sc->sc_flags & UKBD_FLAG_POLLING) == 0)
1790 ukbd_clear_state(kbd);
1791 sc->sc_mode = *(int *)arg;
1799 case KDGETLED: /* get keyboard LED */
1800 *(int *)arg = KBD_LED_VAL(kbd);
1802 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1803 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1805 ival = IOCPARM_IVAL(arg);
1806 arg = (caddr_t)&ival;
1809 case KDSETLED: /* set keyboard LED */
1810 /* NOTE: lock key state in "sc_state" won't be changed */
1811 if (*(int *)arg & ~LOCK_MASK)
1816 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1817 if (sc->sc_mode == K_XLATE &&
1818 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1824 if (KBD_HAS_DEVICE(kbd))
1825 ukbd_set_leds(sc, i);
1827 KBD_LED_VAL(kbd) = *(int *)arg;
1829 case KDGKBSTATE: /* get lock key state */
1830 *(int *)arg = sc->sc_state & LOCK_MASK;
1832 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1833 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1835 ival = IOCPARM_IVAL(arg);
1836 arg = (caddr_t)&ival;
1839 case KDSKBSTATE: /* set lock key state */
1840 if (*(int *)arg & ~LOCK_MASK) {
1843 sc->sc_state &= ~LOCK_MASK;
1844 sc->sc_state |= *(int *)arg;
1846 /* set LEDs and quit */
1847 return (ukbd_ioctl(kbd, KDSETLED, arg));
1849 case KDSETREPEAT: /* set keyboard repeat rate (new
1851 if (!KBD_HAS_DEVICE(kbd)) {
1854 if (((int *)arg)[1] < 0) {
1857 if (((int *)arg)[0] < 0) {
1860 if (((int *)arg)[0] < 200) /* fastest possible value */
1861 kbd->kb_delay1 = 200;
1863 kbd->kb_delay1 = ((int *)arg)[0];
1864 kbd->kb_delay2 = ((int *)arg)[1];
1867 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1868 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1870 ival = IOCPARM_IVAL(arg);
1871 arg = (caddr_t)&ival;
1874 case KDSETRAD: /* set keyboard repeat rate (old
1876 return (ukbd_set_typematic(kbd, *(int *)arg));
1878 case PIO_KEYMAP: /* set keyboard translation table */
1879 case OPIO_KEYMAP: /* set keyboard translation table
1881 case PIO_KEYMAPENT: /* set keyboard translation table
1883 case PIO_DEADKEYMAP: /* set accent key translation table */
1887 return (genkbd_commonioctl(kbd, cmd, arg));
1894 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1899 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
1900 * context where printf(9) can be called, which among other things
1901 * includes interrupt filters and threads with any kinds of locks
1902 * already held. For this reason it would be dangerous to acquire
1903 * the Giant here unconditionally. On the other hand we have to
1904 * have it to handle the ioctl.
1905 * So we make our best effort to auto-detect whether we can grab
1906 * the Giant or not. Blame syscons(4) for this.
1912 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
1913 return (EDEADLK); /* best I could come up with */
1917 result = ukbd_ioctl_locked(kbd, cmd, arg);
1924 /* clear the internal state of the keyboard */
1926 ukbd_clear_state(keyboard_t *kbd)
1928 struct ukbd_softc *sc = kbd->kb_data;
1930 UKBD_CTX_LOCK_ASSERT();
1932 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1933 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1935 sc->sc_composed_char = 0;
1936 #ifdef UKBD_EMULATE_ATSCANCODE
1937 sc->sc_buffered_char[0] = 0;
1938 sc->sc_buffered_char[1] = 0;
1940 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1941 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
1942 memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
1943 memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
1946 /* save the internal state, not used */
1948 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1950 return (len == 0) ? 1 : -1;
1953 /* set the internal state, not used */
1955 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1961 ukbd_poll(keyboard_t *kbd, int on)
1963 struct ukbd_softc *sc = kbd->kb_data;
1967 sc->sc_flags |= UKBD_FLAG_POLLING;
1968 sc->sc_poll_thread = curthread;
1970 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1971 ukbd_start_timer(sc); /* start timer */
1978 /* local functions */
1981 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1985 DPRINTF("leds=0x%02x\n", leds);
1988 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1990 /* start transfer, if not already started */
1992 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1996 ukbd_set_typematic(keyboard_t *kbd, int code)
1998 static const int delays[] = {250, 500, 750, 1000};
1999 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
2000 68, 76, 84, 92, 100, 110, 118, 126,
2001 136, 152, 168, 184, 200, 220, 236, 252,
2002 272, 304, 336, 368, 400, 440, 472, 504};
2007 kbd->kb_delay1 = delays[(code >> 5) & 3];
2008 kbd->kb_delay2 = rates[code & 0x1f];
2012 #ifdef UKBD_EMULATE_ATSCANCODE
2014 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
2016 static const int scan[] = {
2022 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
2032 0x151, /* PageDown */
2035 0x146, /* XXX Pause/Break */
2036 0x15b, /* Win_L(Super_L) */
2037 0x15c, /* Win_R(Super_R) */
2038 0x15d, /* Application(Menu) */
2040 /* SUN TYPE 6 USB KEYBOARD */
2041 0x168, /* Sun Type 6 Help */
2042 0x15e, /* Sun Type 6 Stop */
2044 0x15f, /* Sun Type 6 Again */
2045 0x160, /* Sun Type 6 Props */
2046 0x161, /* Sun Type 6 Undo */
2047 0x162, /* Sun Type 6 Front */
2048 0x163, /* Sun Type 6 Copy */
2049 0x164, /* Sun Type 6 Open */
2050 0x165, /* Sun Type 6 Paste */
2051 0x166, /* Sun Type 6 Find */
2052 0x167, /* Sun Type 6 Cut */
2053 0x125, /* Sun Type 6 Mute */
2055 0x11f, /* Sun Type 6 VolumeDown */
2056 0x11e, /* Sun Type 6 VolumeUp */
2057 0x120, /* Sun Type 6 PowerDown */
2059 /* Japanese 106/109 keyboard */
2060 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
2061 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
2062 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
2063 0x79, /* Keyboard Intl' 4 (Henkan) */
2064 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
2065 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
2068 if ((code >= 89) && (code < (int)(89 + (sizeof(scan) / sizeof(scan[0]))))) {
2069 code = scan[code - 89];
2072 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
2073 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
2075 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
2076 code &= ~SCAN_PREFIX_SHIFT;
2078 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
2080 if (code & SCAN_PREFIX) {
2081 if (code & SCAN_PREFIX_CTL) {
2083 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
2084 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
2085 } else if (code & SCAN_PREFIX_SHIFT) {
2087 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
2088 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
2090 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
2091 sc->sc_buffered_char[1] = 0;
2093 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
2099 #endif /* UKBD_EMULATE_ATSCANCODE */
2101 static keyboard_switch_t ukbdsw = {
2102 .probe = &ukbd__probe,
2106 .test_if = &ukbd_test_if,
2107 .enable = &ukbd_enable,
2108 .disable = &ukbd_disable,
2110 .check = &ukbd_check,
2111 .read_char = &ukbd_read_char,
2112 .check_char = &ukbd_check_char,
2113 .ioctl = &ukbd_ioctl,
2115 .clear_state = &ukbd_clear_state,
2116 .get_state = &ukbd_get_state,
2117 .set_state = &ukbd_set_state,
2118 .get_fkeystr = &genkbd_get_fkeystr,
2120 .diag = &genkbd_diag,
2123 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
2126 ukbd_driver_load(module_t mod, int what, void *arg)
2130 kbd_add_driver(&ukbd_kbd_driver);
2133 kbd_delete_driver(&ukbd_kbd_driver);
2139 static devclass_t ukbd_devclass;
2141 static device_method_t ukbd_methods[] = {
2142 DEVMETHOD(device_probe, ukbd_probe),
2143 DEVMETHOD(device_attach, ukbd_attach),
2144 DEVMETHOD(device_detach, ukbd_detach),
2145 DEVMETHOD(device_resume, ukbd_resume),
2150 static driver_t ukbd_driver = {
2152 .methods = ukbd_methods,
2153 .size = sizeof(struct ukbd_softc),
2156 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
2157 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
2158 MODULE_VERSION(ukbd, 1);