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;
96 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
97 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
98 &ukbd_debug, 0, "Debug level");
99 TUNABLE_INT("hw.usb.ukbd.debug", &ukbd_debug);
100 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW | CTLFLAG_TUN,
101 &ukbd_no_leds, 0, "Disables setting of keyboard leds");
102 TUNABLE_INT("hw.usb.ukbd.no_leds", &ukbd_no_leds);
105 #define UKBD_EMULATE_ATSCANCODE 1
106 #define UKBD_DRIVER_NAME "ukbd"
107 #define UKBD_NMOD 8 /* units */
108 #define UKBD_NKEYCODE 6 /* units */
109 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */
110 #define UKBD_IN_BUF_FULL ((UKBD_IN_BUF_SIZE / 2) - 1) /* bytes */
111 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
112 #define UKBD_BUFFER_SIZE 64 /* bytes */
116 #define MOD_CONTROL_L 0x01
117 #define MOD_CONTROL_R 0x10
118 #define MOD_SHIFT_L 0x02
119 #define MOD_SHIFT_R 0x20
120 #define MOD_ALT_L 0x04
121 #define MOD_ALT_R 0x40
122 #define MOD_WIN_L 0x08
123 #define MOD_WIN_R 0x80
125 #define MOD_EJECT 0x0100
126 #define MOD_FN 0x0200
127 uint8_t keycode[UKBD_NKEYCODE];
140 accentmap_t sc_accmap;
141 fkeytab_t sc_fkeymap[UKBD_NFKEY];
142 struct hid_location sc_loc_apple_eject;
143 struct hid_location sc_loc_apple_fn;
144 struct hid_location sc_loc_ctrl_l;
145 struct hid_location sc_loc_ctrl_r;
146 struct hid_location sc_loc_shift_l;
147 struct hid_location sc_loc_shift_r;
148 struct hid_location sc_loc_alt_l;
149 struct hid_location sc_loc_alt_r;
150 struct hid_location sc_loc_win_l;
151 struct hid_location sc_loc_win_r;
152 struct hid_location sc_loc_events;
153 struct hid_location sc_loc_numlock;
154 struct hid_location sc_loc_capslock;
155 struct hid_location sc_loc_scrolllock;
156 struct usb_callout sc_callout;
157 struct ukbd_data sc_ndata;
158 struct ukbd_data sc_odata;
160 struct thread *sc_poll_thread;
161 struct usb_device *sc_udev;
162 struct usb_interface *sc_iface;
163 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
165 uint32_t sc_ntime[UKBD_NKEYCODE];
166 uint32_t sc_otime[UKBD_NKEYCODE];
167 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
169 uint32_t sc_composed_char; /* composed char code, if non-zero */
170 #ifdef UKBD_EMULATE_ATSCANCODE
171 uint32_t sc_buffered_char[2];
173 uint32_t sc_flags; /* flags */
174 #define UKBD_FLAG_COMPOSE 0x00000001
175 #define UKBD_FLAG_POLLING 0x00000002
176 #define UKBD_FLAG_SET_LEDS 0x00000004
177 #define UKBD_FLAG_ATTACHED 0x00000010
178 #define UKBD_FLAG_GONE 0x00000020
180 #define UKBD_FLAG_HID_MASK 0x003fffc0
181 #define UKBD_FLAG_APPLE_EJECT 0x00000040
182 #define UKBD_FLAG_APPLE_FN 0x00000080
183 #define UKBD_FLAG_APPLE_SWAP 0x00000100
184 #define UKBD_FLAG_TIMER_RUNNING 0x00000200
185 #define UKBD_FLAG_CTRL_L 0x00000400
186 #define UKBD_FLAG_CTRL_R 0x00000800
187 #define UKBD_FLAG_SHIFT_L 0x00001000
188 #define UKBD_FLAG_SHIFT_R 0x00002000
189 #define UKBD_FLAG_ALT_L 0x00004000
190 #define UKBD_FLAG_ALT_R 0x00008000
191 #define UKBD_FLAG_WIN_L 0x00010000
192 #define UKBD_FLAG_WIN_R 0x00020000
193 #define UKBD_FLAG_EVENTS 0x00040000
194 #define UKBD_FLAG_NUMLOCK 0x00080000
195 #define UKBD_FLAG_CAPSLOCK 0x00100000
196 #define UKBD_FLAG_SCROLLLOCK 0x00200000
198 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
199 int sc_state; /* shift/lock key state */
200 int sc_accents; /* accent key index (> 0) */
201 int sc_poll_tick_last;
202 int sc_polling; /* polling recursion count */
207 uint16_t sc_inputhead;
208 uint16_t sc_inputtail;
209 uint16_t sc_modifiers;
211 uint8_t sc_leds; /* store for async led requests */
212 uint8_t sc_iface_index;
214 uint8_t sc_id_apple_eject;
215 uint8_t sc_id_apple_fn;
216 uint8_t sc_id_ctrl_l;
217 uint8_t sc_id_ctrl_r;
218 uint8_t sc_id_shift_l;
219 uint8_t sc_id_shift_r;
225 uint8_t sc_id_numlock;
226 uint8_t sc_id_capslock;
227 uint8_t sc_id_scrolllock;
228 uint8_t sc_id_events;
231 uint8_t sc_poll_detected;
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)
255 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
256 {MOD_CONTROL_L, 0xe0},
257 {MOD_CONTROL_R, 0xe4},
266 #define NN 0 /* no translation */
268 * Translate USB keycodes to AT keyboard scancodes.
271 * FIXME: Mac USB keyboard generates:
272 * 0x53: keypad NumLock/Clear
279 * USB Apple Keyboard JIS generates:
283 static const uint8_t ukbd_trtab[256] = {
284 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
285 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
286 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
287 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
288 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
289 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
290 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
291 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
292 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
293 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
294 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
295 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
296 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
297 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
298 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
299 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
300 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */
301 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */
302 129, 130, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
303 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
304 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
305 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
306 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
307 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
308 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
309 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
310 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
311 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
312 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
313 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
314 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
315 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
318 static const uint8_t ukbd_boot_desc[] = {
319 0x05, 0x01, 0x09, 0x06, 0xa1,
320 0x01, 0x05, 0x07, 0x19, 0xe0,
321 0x29, 0xe7, 0x15, 0x00, 0x25,
322 0x01, 0x75, 0x01, 0x95, 0x08,
323 0x81, 0x02, 0x95, 0x01, 0x75,
324 0x08, 0x81, 0x01, 0x95, 0x03,
325 0x75, 0x01, 0x05, 0x08, 0x19,
326 0x01, 0x29, 0x03, 0x91, 0x02,
327 0x95, 0x05, 0x75, 0x01, 0x91,
328 0x01, 0x95, 0x06, 0x75, 0x08,
329 0x15, 0x00, 0x26, 0xff, 0x00,
330 0x05, 0x07, 0x19, 0x00, 0x2a,
331 0xff, 0x00, 0x81, 0x00, 0xc0
335 static void ukbd_timeout(void *);
336 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
337 static int ukbd_set_typematic(keyboard_t *, int);
338 #ifdef UKBD_EMULATE_ATSCANCODE
339 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
341 static uint32_t ukbd_read_char(keyboard_t *, int);
342 static void ukbd_clear_state(keyboard_t *);
343 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
344 static int ukbd_enable(keyboard_t *);
345 static int ukbd_disable(keyboard_t *);
346 static void ukbd_interrupt(struct ukbd_softc *);
347 static int ukbd_is_polling(struct ukbd_softc *);
348 static int ukbd_polls_other_thread(struct ukbd_softc *);
349 static void ukbd_event_keyinput(struct ukbd_softc *);
351 static device_probe_t ukbd_probe;
352 static device_attach_t ukbd_attach;
353 static device_detach_t ukbd_detach;
354 static device_resume_t ukbd_resume;
357 ukbd_any_key_pressed(struct ukbd_softc *sc)
362 for (j = i = 0; i < UKBD_NKEYCODE; i++)
363 j |= sc->sc_odata.keycode[i];
369 ukbd_start_timer(struct ukbd_softc *sc)
371 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
372 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
376 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
378 mtx_assert(&Giant, MA_OWNED);
380 DPRINTF("0x%02x (%d) %s\n", key, key,
381 (key & KEY_RELEASE) ? "released" : "pressed");
383 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
384 sc->sc_input[sc->sc_inputtail] = key;
386 ++(sc->sc_inputtail);
387 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
388 sc->sc_inputtail = 0;
391 DPRINTF("input buffer is full\n");
398 struct thread *td = curthread;
405 /* get current priority */
406 old_prio = td->td_base_pri;
408 /* set new priority */
409 sched_prio(td, td->td_user_pri);
411 /* cause a task switch */
412 mi_switch(SW_INVOL | SWT_RELINQUISH, NULL);
414 /* restore priority */
415 sched_prio(td, old_prio);
423 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
425 DPRINTFN(2, "polling\n");
427 /* update stats about last polling event */
428 sc->sc_poll_tick_last = ticks;
429 sc->sc_poll_detected = 1;
431 if (kdb_active == 0) {
432 while (sc->sc_inputs == 0) {
434 /* give USB threads a chance to run */
437 /* check if we should wait */
441 return; /* Only poll if KDB is active */
444 while (sc->sc_inputs == 0) {
446 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
448 /* Delay-optimised support for repetition of keys */
450 if (ukbd_any_key_pressed(sc)) {
451 /* a key is pressed - need timekeeping */
454 /* 1 millisecond has passed */
466 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
470 mtx_assert(&Giant, MA_OWNED);
472 if (sc->sc_inputs == 0 &&
473 (sc->sc_flags & UKBD_FLAG_GONE) == 0) {
474 /* start transfer, if not already started */
475 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]);
476 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]);
479 if (ukbd_polls_other_thread(sc))
482 if (sc->sc_flags & UKBD_FLAG_POLLING)
483 ukbd_do_poll(sc, wait);
485 if (sc->sc_inputs == 0) {
488 c = sc->sc_input[sc->sc_inputhead];
490 ++(sc->sc_inputhead);
491 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
492 sc->sc_inputhead = 0;
499 ukbd_interrupt(struct ukbd_softc *sc)
503 uint32_t now = sc->sc_time_ms;
509 if (sc->sc_ndata.keycode[0] == KEY_ERROR)
512 n_mod = sc->sc_ndata.modifiers;
513 o_mod = sc->sc_odata.modifiers;
514 if (n_mod != o_mod) {
515 for (i = 0; i < UKBD_NMOD; i++) {
516 if ((n_mod & ukbd_mods[i].mask) !=
517 (o_mod & ukbd_mods[i].mask)) {
518 ukbd_put_key(sc, ukbd_mods[i].key |
519 ((n_mod & ukbd_mods[i].mask) ?
520 KEY_PRESS : KEY_RELEASE));
524 /* Check for released keys. */
525 for (i = 0; i < UKBD_NKEYCODE; i++) {
526 key = sc->sc_odata.keycode[i];
530 for (j = 0; j < UKBD_NKEYCODE; j++) {
531 if (sc->sc_ndata.keycode[j] == 0) {
534 if (key == sc->sc_ndata.keycode[j]) {
538 ukbd_put_key(sc, key | KEY_RELEASE);
542 /* Check for pressed keys. */
543 for (i = 0; i < UKBD_NKEYCODE; i++) {
544 key = sc->sc_ndata.keycode[i];
548 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
549 for (j = 0; j < UKBD_NKEYCODE; j++) {
550 if (sc->sc_odata.keycode[j] == 0) {
553 if (key == sc->sc_odata.keycode[j]) {
555 /* key is still pressed */
557 sc->sc_ntime[i] = sc->sc_otime[j];
558 dtime = (sc->sc_otime[j] - now);
560 if (!(dtime & 0x80000000)) {
561 /* time has not elapsed */
564 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
568 ukbd_put_key(sc, key | KEY_PRESS);
571 * If any other key is presently down, force its repeat to be
572 * well in the future (100s). This makes the last key to be
573 * pressed do the autorepeat.
575 for (j = 0; j != UKBD_NKEYCODE; j++) {
577 sc->sc_ntime[j] = now + (100 * 1000);
582 sc->sc_odata = sc->sc_ndata;
584 memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
586 ukbd_event_keyinput(sc);
590 ukbd_event_keyinput(struct ukbd_softc *sc)
594 if (ukbd_is_polling(sc))
597 if (sc->sc_inputs == 0)
600 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
601 KBD_IS_BUSY(&sc->sc_kbd)) {
602 /* let the callback function process the input */
603 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
604 sc->sc_kbd.kb_callback.kc_arg);
606 /* read and discard the input, no one is waiting for it */
608 c = ukbd_read_char(&sc->sc_kbd, 0);
609 } while (c != NOKEY);
614 ukbd_timeout(void *arg)
616 struct ukbd_softc *sc = arg;
618 mtx_assert(&Giant, MA_OWNED);
620 sc->sc_time_ms += 25; /* milliseconds */
624 /* Make sure any leftover key events gets read out */
625 ukbd_event_keyinput(sc);
627 if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
628 ukbd_start_timer(sc);
630 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
635 ukbd_apple_fn(uint8_t keycode) {
637 case 0x28: return 0x49; /* RETURN -> INSERT */
638 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
639 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
640 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
641 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
642 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
643 default: return keycode;
648 ukbd_apple_swap(uint8_t keycode) {
650 case 0x35: return 0x64;
651 case 0x64: return 0x35;
652 default: return keycode;
657 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
659 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
660 struct usb_page_cache *pc;
666 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
667 pc = usbd_xfer_get_frame(xfer, 0);
669 switch (USB_GET_STATE(xfer)) {
670 case USB_ST_TRANSFERRED:
671 DPRINTF("actlen=%d bytes\n", len);
674 DPRINTF("zero length data\n");
678 if (sc->sc_kbd_id != 0) {
679 /* check and remove HID ID byte */
680 usbd_copy_out(pc, 0, &id, 1);
684 DPRINTF("zero length data\n");
692 if (len > UKBD_BUFFER_SIZE)
693 len = UKBD_BUFFER_SIZE;
696 usbd_copy_out(pc, offset, sc->sc_buffer, len);
698 /* clear temporary storage */
699 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
701 /* scan through HID data */
702 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
703 (id == sc->sc_id_apple_eject)) {
704 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject))
705 sc->sc_modifiers |= MOD_EJECT;
707 sc->sc_modifiers &= ~MOD_EJECT;
709 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
710 (id == sc->sc_id_apple_fn)) {
711 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn))
712 sc->sc_modifiers |= MOD_FN;
714 sc->sc_modifiers &= ~MOD_FN;
716 if ((sc->sc_flags & UKBD_FLAG_CTRL_L) &&
717 (id == sc->sc_id_ctrl_l)) {
718 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_l))
719 sc-> sc_modifiers |= MOD_CONTROL_L;
721 sc-> sc_modifiers &= ~MOD_CONTROL_L;
723 if ((sc->sc_flags & UKBD_FLAG_CTRL_R) &&
724 (id == sc->sc_id_ctrl_r)) {
725 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_r))
726 sc->sc_modifiers |= MOD_CONTROL_R;
728 sc->sc_modifiers &= ~MOD_CONTROL_R;
730 if ((sc->sc_flags & UKBD_FLAG_SHIFT_L) &&
731 (id == sc->sc_id_shift_l)) {
732 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_l))
733 sc->sc_modifiers |= MOD_SHIFT_L;
735 sc->sc_modifiers &= ~MOD_SHIFT_L;
737 if ((sc->sc_flags & UKBD_FLAG_SHIFT_R) &&
738 (id == sc->sc_id_shift_r)) {
739 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_r))
740 sc->sc_modifiers |= MOD_SHIFT_R;
742 sc->sc_modifiers &= ~MOD_SHIFT_R;
744 if ((sc->sc_flags & UKBD_FLAG_ALT_L) &&
745 (id == sc->sc_id_alt_l)) {
746 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_l))
747 sc->sc_modifiers |= MOD_ALT_L;
749 sc->sc_modifiers &= ~MOD_ALT_L;
751 if ((sc->sc_flags & UKBD_FLAG_ALT_R) &&
752 (id == sc->sc_id_alt_r)) {
753 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_r))
754 sc->sc_modifiers |= MOD_ALT_R;
756 sc->sc_modifiers &= ~MOD_ALT_R;
758 if ((sc->sc_flags & UKBD_FLAG_WIN_L) &&
759 (id == sc->sc_id_win_l)) {
760 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_l))
761 sc->sc_modifiers |= MOD_WIN_L;
763 sc->sc_modifiers &= ~MOD_WIN_L;
765 if ((sc->sc_flags & UKBD_FLAG_WIN_R) &&
766 (id == sc->sc_id_win_r)) {
767 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_r))
768 sc->sc_modifiers |= MOD_WIN_R;
770 sc->sc_modifiers &= ~MOD_WIN_R;
773 sc->sc_ndata.modifiers = sc->sc_modifiers;
775 if ((sc->sc_flags & UKBD_FLAG_EVENTS) &&
776 (id == sc->sc_id_events)) {
777 i = sc->sc_loc_events.count;
778 if (i > UKBD_NKEYCODE)
783 sc->sc_ndata.keycode[i] =
784 hid_get_data(sc->sc_buffer + i, len - i,
790 DPRINTF("modifiers = 0x%04x\n", (int)sc->sc_modifiers);
791 for (i = 0; i < UKBD_NKEYCODE; i++) {
792 if (sc->sc_ndata.keycode[i]) {
793 DPRINTF("[%d] = 0x%02x\n",
794 (int)i, (int)sc->sc_ndata.keycode[i]);
798 if (sc->sc_modifiers & MOD_FN) {
799 for (i = 0; i < UKBD_NKEYCODE; i++) {
800 sc->sc_ndata.keycode[i] =
801 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
805 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
806 for (i = 0; i < UKBD_NKEYCODE; i++) {
807 sc->sc_ndata.keycode[i] =
808 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
814 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
815 if (ukbd_any_key_pressed(sc)) {
816 ukbd_start_timer(sc);
822 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
823 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
824 usbd_transfer_submit(xfer);
826 DPRINTF("input queue is full!\n");
831 DPRINTF("error=%s\n", usbd_errstr(error));
833 if (error != USB_ERR_CANCELLED) {
834 /* try to clear stall first */
835 usbd_xfer_set_stall(xfer);
843 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
845 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
846 struct usb_device_request req;
847 struct usb_page_cache *pc;
857 switch (USB_GET_STATE(xfer)) {
858 case USB_ST_TRANSFERRED:
860 if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS))
862 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
864 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
865 req.bRequest = UR_SET_REPORT;
866 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
867 req.wIndex[0] = sc->sc_iface_no;
871 memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE);
876 /* Assumption: All led bits must be in the same ID. */
878 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) {
879 if (sc->sc_leds & NLKED) {
880 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
881 &sc->sc_loc_numlock, 1);
883 id = sc->sc_id_numlock;
887 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) {
888 if (sc->sc_leds & SLKED) {
889 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
890 &sc->sc_loc_scrolllock, 1);
892 id = sc->sc_id_scrolllock;
896 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) {
897 if (sc->sc_leds & CLKED) {
898 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
899 &sc->sc_loc_capslock, 1);
901 id = sc->sc_id_capslock;
905 /* if no leds, nothing to do */
909 /* range check output report length */
910 len = sc->sc_led_size;
911 if (len > (UKBD_BUFFER_SIZE - 1))
912 len = (UKBD_BUFFER_SIZE - 1);
914 /* check if we need to prefix an ID byte */
915 sc->sc_buffer[0] = id;
917 pc = usbd_xfer_get_frame(xfer, 1);
920 usbd_copy_in(pc, 0, sc->sc_buffer, len);
922 usbd_copy_in(pc, 0, sc->sc_buffer + 1, len);
924 req.wLength[0] = len;
925 usbd_xfer_set_frame_len(xfer, 1, len);
927 DPRINTF("len=%d, id=%d\n", len, id);
929 /* setup control request last */
930 pc = usbd_xfer_get_frame(xfer, 0);
931 usbd_copy_in(pc, 0, &req, sizeof(req));
932 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
934 /* start data transfer */
935 usbd_xfer_set_frames(xfer, 2);
936 usbd_transfer_submit(xfer);
940 DPRINTFN(1, "error=%s\n", usbd_errstr(error));
945 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
948 .type = UE_INTERRUPT,
949 .endpoint = UE_ADDR_ANY,
950 .direction = UE_DIR_IN,
951 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
952 .bufsize = 0, /* use wMaxPacketSize */
953 .callback = &ukbd_intr_callback,
957 .type = UE_INTERRUPT,
958 .endpoint = UE_ADDR_ANY,
959 .direction = UE_DIR_IN,
960 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
961 .bufsize = 0, /* use wMaxPacketSize */
962 .callback = &ukbd_intr_callback,
967 .endpoint = 0x00, /* Control pipe */
968 .direction = UE_DIR_ANY,
969 .bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE,
970 .callback = &ukbd_set_leds_callback,
971 .timeout = 1000, /* 1 second */
975 /* A match on these entries will load ukbd */
976 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = {
977 {USB_IFACE_CLASS(UICLASS_HID),
978 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
979 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),},
983 ukbd_probe(device_t dev)
985 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
986 struct usb_attach_arg *uaa = device_get_ivars(dev);
996 if (uaa->usb_mode != USB_MODE_HOST) {
1000 if (uaa->info.bInterfaceClass != UICLASS_HID)
1003 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
1004 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) {
1005 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1008 return (BUS_PROBE_DEFAULT);
1011 error = usbd_req_get_hid_desc(uaa->device, NULL,
1012 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
1018 * NOTE: we currently don't support USB mouse and USB keyboard
1019 * on the same USB endpoint.
1021 if (hid_is_collection(d_ptr, d_len,
1022 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
1023 /* most likely a mouse */
1025 } else if (hid_is_collection(d_ptr, d_len,
1026 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
1027 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1030 error = BUS_PROBE_DEFAULT;
1034 free(d_ptr, M_TEMP);
1039 ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len)
1043 /* reset detected bits */
1044 sc->sc_flags &= ~UKBD_FLAG_HID_MASK;
1046 /* check if there is an ID byte */
1047 sc->sc_kbd_size = hid_report_size(ptr, len,
1048 hid_input, &sc->sc_kbd_id);
1050 /* investigate if this is an Apple Keyboard */
1051 if (hid_locate(ptr, len,
1052 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
1053 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
1054 &sc->sc_id_apple_eject)) {
1055 if (flags & HIO_VARIABLE)
1056 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
1057 UKBD_FLAG_APPLE_SWAP;
1058 DPRINTFN(1, "Found Apple eject-key\n");
1060 if (hid_locate(ptr, len,
1061 HID_USAGE2(0xFFFF, 0x0003),
1062 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
1063 &sc->sc_id_apple_fn)) {
1064 if (flags & HIO_VARIABLE)
1065 sc->sc_flags |= UKBD_FLAG_APPLE_FN;
1066 DPRINTFN(1, "Found Apple FN-key\n");
1068 /* figure out some keys */
1069 if (hid_locate(ptr, len,
1070 HID_USAGE2(HUP_KEYBOARD, 0xE0),
1071 hid_input, 0, &sc->sc_loc_ctrl_l, &flags,
1072 &sc->sc_id_ctrl_l)) {
1073 if (flags & HIO_VARIABLE)
1074 sc->sc_flags |= UKBD_FLAG_CTRL_L;
1075 DPRINTFN(1, "Found left control\n");
1077 if (hid_locate(ptr, len,
1078 HID_USAGE2(HUP_KEYBOARD, 0xE4),
1079 hid_input, 0, &sc->sc_loc_ctrl_r, &flags,
1080 &sc->sc_id_ctrl_r)) {
1081 if (flags & HIO_VARIABLE)
1082 sc->sc_flags |= UKBD_FLAG_CTRL_R;
1083 DPRINTFN(1, "Found right control\n");
1085 if (hid_locate(ptr, len,
1086 HID_USAGE2(HUP_KEYBOARD, 0xE1),
1087 hid_input, 0, &sc->sc_loc_shift_l, &flags,
1088 &sc->sc_id_shift_l)) {
1089 if (flags & HIO_VARIABLE)
1090 sc->sc_flags |= UKBD_FLAG_SHIFT_L;
1091 DPRINTFN(1, "Found left shift\n");
1093 if (hid_locate(ptr, len,
1094 HID_USAGE2(HUP_KEYBOARD, 0xE5),
1095 hid_input, 0, &sc->sc_loc_shift_r, &flags,
1096 &sc->sc_id_shift_r)) {
1097 if (flags & HIO_VARIABLE)
1098 sc->sc_flags |= UKBD_FLAG_SHIFT_R;
1099 DPRINTFN(1, "Found right shift\n");
1101 if (hid_locate(ptr, len,
1102 HID_USAGE2(HUP_KEYBOARD, 0xE2),
1103 hid_input, 0, &sc->sc_loc_alt_l, &flags,
1104 &sc->sc_id_alt_l)) {
1105 if (flags & HIO_VARIABLE)
1106 sc->sc_flags |= UKBD_FLAG_ALT_L;
1107 DPRINTFN(1, "Found left alt\n");
1109 if (hid_locate(ptr, len,
1110 HID_USAGE2(HUP_KEYBOARD, 0xE6),
1111 hid_input, 0, &sc->sc_loc_alt_r, &flags,
1112 &sc->sc_id_alt_r)) {
1113 if (flags & HIO_VARIABLE)
1114 sc->sc_flags |= UKBD_FLAG_ALT_R;
1115 DPRINTFN(1, "Found right alt\n");
1117 if (hid_locate(ptr, len,
1118 HID_USAGE2(HUP_KEYBOARD, 0xE3),
1119 hid_input, 0, &sc->sc_loc_win_l, &flags,
1120 &sc->sc_id_win_l)) {
1121 if (flags & HIO_VARIABLE)
1122 sc->sc_flags |= UKBD_FLAG_WIN_L;
1123 DPRINTFN(1, "Found left GUI\n");
1125 if (hid_locate(ptr, len,
1126 HID_USAGE2(HUP_KEYBOARD, 0xE7),
1127 hid_input, 0, &sc->sc_loc_win_r, &flags,
1128 &sc->sc_id_win_r)) {
1129 if (flags & HIO_VARIABLE)
1130 sc->sc_flags |= UKBD_FLAG_WIN_R;
1131 DPRINTFN(1, "Found right GUI\n");
1133 /* figure out event buffer */
1134 if (hid_locate(ptr, len,
1135 HID_USAGE2(HUP_KEYBOARD, 0x00),
1136 hid_input, 0, &sc->sc_loc_events, &flags,
1137 &sc->sc_id_events)) {
1138 if (flags & HIO_VARIABLE) {
1139 DPRINTFN(1, "Ignoring keyboard event control\n");
1141 sc->sc_flags |= UKBD_FLAG_EVENTS;
1142 DPRINTFN(1, "Found keyboard event array\n");
1146 /* figure out leds on keyboard */
1147 sc->sc_led_size = hid_report_size(ptr, len,
1150 if (hid_locate(ptr, len,
1151 HID_USAGE2(HUP_LEDS, 0x01),
1152 hid_output, 0, &sc->sc_loc_numlock, &flags,
1153 &sc->sc_id_numlock)) {
1154 if (flags & HIO_VARIABLE)
1155 sc->sc_flags |= UKBD_FLAG_NUMLOCK;
1156 DPRINTFN(1, "Found keyboard numlock\n");
1158 if (hid_locate(ptr, len,
1159 HID_USAGE2(HUP_LEDS, 0x02),
1160 hid_output, 0, &sc->sc_loc_capslock, &flags,
1161 &sc->sc_id_capslock)) {
1162 if (flags & HIO_VARIABLE)
1163 sc->sc_flags |= UKBD_FLAG_CAPSLOCK;
1164 DPRINTFN(1, "Found keyboard capslock\n");
1166 if (hid_locate(ptr, len,
1167 HID_USAGE2(HUP_LEDS, 0x03),
1168 hid_output, 0, &sc->sc_loc_scrolllock, &flags,
1169 &sc->sc_id_scrolllock)) {
1170 if (flags & HIO_VARIABLE)
1171 sc->sc_flags |= UKBD_FLAG_SCROLLLOCK;
1172 DPRINTFN(1, "Found keyboard scrolllock\n");
1177 ukbd_attach(device_t dev)
1179 struct ukbd_softc *sc = device_get_softc(dev);
1180 struct usb_attach_arg *uaa = device_get_ivars(dev);
1181 int32_t unit = device_get_unit(dev);
1182 keyboard_t *kbd = &sc->sc_kbd;
1183 void *hid_ptr = NULL;
1188 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
1190 kbd->kb_data = (void *)sc;
1192 device_set_usb_desc(dev);
1194 sc->sc_udev = uaa->device;
1195 sc->sc_iface = uaa->iface;
1196 sc->sc_iface_index = uaa->info.bIfaceIndex;
1197 sc->sc_iface_no = uaa->info.bIfaceNum;
1198 sc->sc_mode = K_XLATE;
1200 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
1202 #ifdef UKBD_NO_POLLING
1203 err = usbd_transfer_setup(uaa->device,
1204 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
1205 UKBD_N_TRANSFER, sc, &Giant);
1208 * Setup the UKBD USB transfers one by one, so they are memory
1209 * independent which allows for handling panics triggered by
1210 * the keyboard driver itself, typically via CTRL+ALT+ESC
1211 * sequences. Or if the USB keyboard driver was processing a
1212 * key at the moment of panic.
1214 for (n = 0; n != UKBD_N_TRANSFER; n++) {
1215 err = usbd_transfer_setup(uaa->device,
1216 &uaa->info.bIfaceIndex, sc->sc_xfer + n, ukbd_config + n,
1224 DPRINTF("error=%s\n", usbd_errstr(err));
1227 /* setup default keyboard maps */
1229 sc->sc_keymap = key_map;
1230 sc->sc_accmap = accent_map;
1231 for (n = 0; n < UKBD_NFKEY; n++) {
1232 sc->sc_fkeymap[n] = fkey_tab[n];
1235 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
1236 sc->sc_fkeymap, UKBD_NFKEY);
1238 KBD_FOUND_DEVICE(kbd);
1240 ukbd_clear_state(kbd);
1243 * FIXME: set the initial value for lock keys in "sc_state"
1244 * according to the BIOS data?
1246 KBD_PROBE_DONE(kbd);
1248 /* get HID descriptor */
1249 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
1250 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
1253 DPRINTF("Parsing HID descriptor of %d bytes\n",
1256 ukbd_parse_hid(sc, hid_ptr, hid_len);
1258 free(hid_ptr, M_TEMP);
1261 /* check if we should use the boot protocol */
1262 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) ||
1263 (err != 0) || (!(sc->sc_flags & UKBD_FLAG_EVENTS))) {
1265 DPRINTF("Forcing boot protocol\n");
1267 err = usbd_req_set_protocol(sc->sc_udev, NULL,
1268 sc->sc_iface_index, 0);
1271 DPRINTF("Set protocol error=%s (ignored)\n",
1275 ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc));
1278 /* ignore if SETIDLE fails, hence it is not crucial */
1279 usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
1283 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
1289 if (kbd_register(kbd) < 0) {
1292 KBD_CONFIG_DONE(kbd);
1296 #ifdef KBD_INSTALL_CDEV
1297 if (kbd_attach(kbd)) {
1301 sc->sc_flags |= UKBD_FLAG_ATTACHED;
1304 genkbd_diag(kbd, bootverbose);
1306 /* lock keyboard mutex */
1310 /* start the keyboard */
1312 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]);
1313 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]);
1316 return (0); /* success */
1320 return (ENXIO); /* error */
1324 ukbd_detach(device_t dev)
1326 struct ukbd_softc *sc = device_get_softc(dev);
1333 sc->sc_flags |= UKBD_FLAG_GONE;
1335 usb_callout_stop(&sc->sc_callout);
1337 /* kill any stuck keys */
1338 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1339 /* stop receiving events from the USB keyboard */
1340 usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_0]);
1341 usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_1]);
1343 /* release all leftover keys, if any */
1344 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1346 /* process releasing of all keys */
1350 ukbd_disable(&sc->sc_kbd);
1352 #ifdef KBD_INSTALL_CDEV
1353 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1354 error = kbd_detach(&sc->sc_kbd);
1356 /* usb attach cannot return an error */
1357 device_printf(dev, "WARNING: kbd_detach() "
1358 "returned non-zero! (ignored)\n");
1362 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
1363 error = kbd_unregister(&sc->sc_kbd);
1365 /* usb attach cannot return an error */
1366 device_printf(dev, "WARNING: kbd_unregister() "
1367 "returned non-zero! (ignored)\n");
1370 sc->sc_kbd.kb_flags = 0;
1374 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1376 usb_callout_drain(&sc->sc_callout);
1378 DPRINTF("%s: disconnected\n",
1379 device_get_nameunit(dev));
1385 ukbd_resume(device_t dev)
1387 struct ukbd_softc *sc = device_get_softc(dev);
1391 ukbd_clear_state(&sc->sc_kbd);
1398 /* early keyboard probe, not supported */
1400 ukbd_configure(int flags)
1405 /* detect a keyboard, not used */
1407 ukbd__probe(int unit, void *arg, int flags)
1412 /* reset and initialize the device, not used */
1414 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1419 /* test the interface to the device, not used */
1421 ukbd_test_if(keyboard_t *kbd)
1426 /* finish using this keyboard, not used */
1428 ukbd_term(keyboard_t *kbd)
1433 /* keyboard interrupt routine, not used */
1435 ukbd_intr(keyboard_t *kbd, void *arg)
1440 /* lock the access to the keyboard, not used */
1442 ukbd_lock(keyboard_t *kbd, int lock)
1448 * Enable the access to the device; until this function is called,
1449 * the client cannot read from the keyboard.
1452 ukbd_enable(keyboard_t *kbd)
1454 if (!mtx_owned(&Giant)) {
1458 retval = ukbd_enable(kbd);
1466 /* disallow the access to the device */
1468 ukbd_disable(keyboard_t *kbd)
1470 if (!mtx_owned(&Giant)) {
1474 retval = ukbd_disable(kbd);
1478 KBD_DEACTIVATE(kbd);
1482 /* check if data is waiting */
1484 ukbd_check(keyboard_t *kbd)
1486 struct ukbd_softc *sc = kbd->kb_data;
1488 if (!KBD_IS_ACTIVE(kbd))
1491 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1492 if (!mtx_owned(&Giant)) {
1496 retval = ukbd_check(kbd);
1501 /* XXX the keyboard layer requires Giant */
1502 if (!mtx_owned(&Giant))
1506 /* check if key belongs to this thread */
1507 if (ukbd_polls_other_thread(sc))
1510 if (sc->sc_flags & UKBD_FLAG_POLLING)
1511 ukbd_do_poll(sc, 0);
1513 #ifdef UKBD_EMULATE_ATSCANCODE
1514 if (sc->sc_buffered_char[0]) {
1518 if (sc->sc_inputs > 0) {
1524 /* check if char is waiting */
1526 ukbd_check_char(keyboard_t *kbd)
1528 struct ukbd_softc *sc = kbd->kb_data;
1530 if (!KBD_IS_ACTIVE(kbd))
1533 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1534 if (!mtx_owned(&Giant)) {
1538 retval = ukbd_check_char(kbd);
1543 /* XXX the keyboard layer requires Giant */
1544 if (!mtx_owned(&Giant))
1548 /* check if key belongs to this thread */
1549 if (ukbd_polls_other_thread(sc))
1552 if ((sc->sc_composed_char > 0) &&
1553 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1556 return (ukbd_check(kbd));
1560 /* read one byte from the keyboard if it's allowed */
1562 ukbd_read(keyboard_t *kbd, int wait)
1564 struct ukbd_softc *sc = kbd->kb_data;
1567 #ifdef UKBD_EMULATE_ATSCANCODE
1572 if (!KBD_IS_ACTIVE(kbd))
1575 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1576 if (!mtx_owned(&Giant)) {
1580 retval = ukbd_read(kbd, wait);
1585 /* XXX the keyboard layer requires Giant */
1586 if (!mtx_owned(&Giant))
1590 /* check if key belongs to this thread */
1591 if (ukbd_polls_other_thread(sc))
1594 #ifdef UKBD_EMULATE_ATSCANCODE
1595 if (sc->sc_buffered_char[0]) {
1596 scancode = sc->sc_buffered_char[0];
1597 if (scancode & SCAN_PREFIX) {
1598 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1599 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1601 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1602 sc->sc_buffered_char[1] = 0;
1605 #endif /* UKBD_EMULATE_ATSCANCODE */
1608 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1609 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1614 #ifdef UKBD_EMULATE_ATSCANCODE
1615 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1616 if (keycode == NN) {
1619 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1620 (usbcode & KEY_RELEASE)));
1621 #else /* !UKBD_EMULATE_ATSCANCODE */
1623 #endif /* UKBD_EMULATE_ATSCANCODE */
1626 /* read char from the keyboard */
1628 ukbd_read_char(keyboard_t *kbd, int wait)
1630 struct ukbd_softc *sc = kbd->kb_data;
1635 #ifdef UKBD_EMULATE_ATSCANCODE
1640 if (!KBD_IS_ACTIVE(kbd))
1643 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1644 if (!mtx_owned(&Giant)) {
1648 retval = ukbd_read_char(kbd, wait);
1653 /* XXX the keyboard layer requires Giant */
1654 if (!mtx_owned(&Giant))
1658 /* check if key belongs to this thread */
1659 if (ukbd_polls_other_thread(sc))
1664 /* do we have a composed char to return ? */
1666 if ((sc->sc_composed_char > 0) &&
1667 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1669 action = sc->sc_composed_char;
1670 sc->sc_composed_char = 0;
1672 if (action > 0xFF) {
1677 #ifdef UKBD_EMULATE_ATSCANCODE
1679 /* do we have a pending raw scan code? */
1681 if (sc->sc_mode == K_RAW) {
1682 scancode = sc->sc_buffered_char[0];
1684 if (scancode & SCAN_PREFIX) {
1685 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1686 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1688 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1689 sc->sc_buffered_char[1] = 0;
1693 #endif /* UKBD_EMULATE_ATSCANCODE */
1695 /* see if there is something in the keyboard port */
1697 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1698 if (usbcode == -1) {
1703 #ifdef UKBD_EMULATE_ATSCANCODE
1704 /* USB key index -> key code -> AT scan code */
1705 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1706 if (keycode == NN) {
1709 /* return an AT scan code for the K_RAW mode */
1710 if (sc->sc_mode == K_RAW) {
1711 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1712 (usbcode & KEY_RELEASE)));
1714 #else /* !UKBD_EMULATE_ATSCANCODE */
1716 /* return the byte as is for the K_RAW mode */
1717 if (sc->sc_mode == K_RAW) {
1720 /* USB key index -> key code */
1721 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1722 if (keycode == NN) {
1725 #endif /* UKBD_EMULATE_ATSCANCODE */
1728 case 0x38: /* left alt (compose key) */
1729 if (usbcode & KEY_RELEASE) {
1730 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1731 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1733 if (sc->sc_composed_char > 0xFF) {
1734 sc->sc_composed_char = 0;
1738 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1739 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1740 sc->sc_composed_char = 0;
1744 /* XXX: I don't like these... */
1745 case 0x5c: /* print screen */
1746 if (sc->sc_flags & ALTS) {
1747 keycode = 0x54; /* sysrq */
1750 case 0x68: /* pause/break */
1751 if (sc->sc_flags & CTLS) {
1752 keycode = 0x6c; /* break */
1757 /* return the key code in the K_CODE mode */
1758 if (usbcode & KEY_RELEASE) {
1759 keycode |= SCAN_RELEASE;
1761 if (sc->sc_mode == K_CODE) {
1764 /* compose a character code */
1765 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1767 /* key pressed, process it */
1770 case 0x49: /* keypad 7,8,9 */
1771 sc->sc_composed_char *= 10;
1772 sc->sc_composed_char += keycode - 0x40;
1773 goto check_composed;
1777 case 0x4D: /* keypad 4,5,6 */
1778 sc->sc_composed_char *= 10;
1779 sc->sc_composed_char += keycode - 0x47;
1780 goto check_composed;
1784 case 0x51: /* keypad 1,2,3 */
1785 sc->sc_composed_char *= 10;
1786 sc->sc_composed_char += keycode - 0x4E;
1787 goto check_composed;
1789 case 0x52: /* keypad 0 */
1790 sc->sc_composed_char *= 10;
1791 goto check_composed;
1793 /* key released, no interest here */
1794 case SCAN_RELEASE | 0x47:
1795 case SCAN_RELEASE | 0x48:
1796 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1797 case SCAN_RELEASE | 0x4B:
1798 case SCAN_RELEASE | 0x4C:
1799 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1800 case SCAN_RELEASE | 0x4F:
1801 case SCAN_RELEASE | 0x50:
1802 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1803 case SCAN_RELEASE | 0x52: /* keypad 0 */
1806 case 0x38: /* left alt key */
1810 if (sc->sc_composed_char > 0) {
1811 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1812 sc->sc_composed_char = 0;
1818 /* keycode to key action */
1819 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1820 (keycode & SCAN_RELEASE),
1821 &sc->sc_state, &sc->sc_accents);
1822 if (action == NOKEY) {
1829 if (sc->sc_composed_char <= 0xFF) {
1836 /* some useful control functions */
1838 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1840 struct ukbd_softc *sc = kbd->kb_data;
1843 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1844 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1848 if (!mtx_owned(&Giant)) {
1850 * XXX big problem: If scroll lock is pressed and "printf()"
1851 * is called, the CPU will get here, to un-scroll lock the
1852 * keyboard. But if "printf()" acquires the "Giant" lock,
1853 * there will be a locking order reversal problem, so the
1854 * keyboard system must get out of "Giant" first, before the
1855 * CPU can proceed here ...
1860 /* workaround for Geli */
1862 i = ukbd_ioctl(kbd, cmd, arg);
1871 case KDGKBMODE: /* get keyboard mode */
1872 *(int *)arg = sc->sc_mode;
1874 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1875 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1877 ival = IOCPARM_IVAL(arg);
1878 arg = (caddr_t)&ival;
1881 case KDSKBMODE: /* set keyboard mode */
1882 switch (*(int *)arg) {
1884 if (sc->sc_mode != K_XLATE) {
1885 /* make lock key state and LED state match */
1886 sc->sc_state &= ~LOCK_MASK;
1887 sc->sc_state |= KBD_LED_VAL(kbd);
1892 if (sc->sc_mode != *(int *)arg) {
1893 if (ukbd_is_polling(sc) == 0)
1894 ukbd_clear_state(kbd);
1895 sc->sc_mode = *(int *)arg;
1903 case KDGETLED: /* get keyboard LED */
1904 *(int *)arg = KBD_LED_VAL(kbd);
1906 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1907 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1909 ival = IOCPARM_IVAL(arg);
1910 arg = (caddr_t)&ival;
1913 case KDSETLED: /* set keyboard LED */
1914 /* NOTE: lock key state in "sc_state" won't be changed */
1915 if (*(int *)arg & ~LOCK_MASK)
1920 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1921 if (sc->sc_mode == K_XLATE &&
1922 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1928 if (KBD_HAS_DEVICE(kbd))
1929 ukbd_set_leds(sc, i);
1931 KBD_LED_VAL(kbd) = *(int *)arg;
1933 case KDGKBSTATE: /* get lock key state */
1934 *(int *)arg = sc->sc_state & LOCK_MASK;
1936 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1937 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1939 ival = IOCPARM_IVAL(arg);
1940 arg = (caddr_t)&ival;
1943 case KDSKBSTATE: /* set lock key state */
1944 if (*(int *)arg & ~LOCK_MASK) {
1947 sc->sc_state &= ~LOCK_MASK;
1948 sc->sc_state |= *(int *)arg;
1950 /* set LEDs and quit */
1951 return (ukbd_ioctl(kbd, KDSETLED, arg));
1953 case KDSETREPEAT: /* set keyboard repeat rate (new
1955 if (!KBD_HAS_DEVICE(kbd)) {
1958 if (((int *)arg)[1] < 0) {
1961 if (((int *)arg)[0] < 0) {
1964 if (((int *)arg)[0] < 200) /* fastest possible value */
1965 kbd->kb_delay1 = 200;
1967 kbd->kb_delay1 = ((int *)arg)[0];
1968 kbd->kb_delay2 = ((int *)arg)[1];
1971 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1972 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1974 ival = IOCPARM_IVAL(arg);
1975 arg = (caddr_t)&ival;
1978 case KDSETRAD: /* set keyboard repeat rate (old
1980 return (ukbd_set_typematic(kbd, *(int *)arg));
1982 case PIO_KEYMAP: /* set keyboard translation table */
1983 case PIO_KEYMAPENT: /* set keyboard translation table
1985 case PIO_DEADKEYMAP: /* set accent key translation table */
1989 return (genkbd_commonioctl(kbd, cmd, arg));
1995 /* clear the internal state of the keyboard */
1997 ukbd_clear_state(keyboard_t *kbd)
1999 struct ukbd_softc *sc = kbd->kb_data;
2001 if (!mtx_owned(&Giant)) {
2004 ukbd_clear_state(kbd);
2009 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
2010 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
2012 sc->sc_composed_char = 0;
2013 #ifdef UKBD_EMULATE_ATSCANCODE
2014 sc->sc_buffered_char[0] = 0;
2015 sc->sc_buffered_char[1] = 0;
2017 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
2018 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
2019 memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
2020 memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
2023 /* save the internal state, not used */
2025 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
2027 return (len == 0) ? 1 : -1;
2030 /* set the internal state, not used */
2032 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
2038 ukbd_is_polling(struct ukbd_softc *sc)
2042 if (sc->sc_flags & UKBD_FLAG_POLLING)
2043 return (1); /* polling */
2045 delta = ticks - sc->sc_poll_tick_last;
2046 if ((delta < 0) || (delta >= hz)) {
2047 sc->sc_poll_detected = 0;
2048 return (0); /* not polling */
2051 return (sc->sc_poll_detected);
2055 ukbd_polls_other_thread(struct ukbd_softc *sc)
2057 return (ukbd_is_polling(sc) &&
2058 (sc->sc_poll_thread != curthread));
2062 ukbd_poll(keyboard_t *kbd, int on)
2064 struct ukbd_softc *sc = kbd->kb_data;
2066 if (!mtx_owned(&Giant)) {
2070 retval = ukbd_poll(kbd, on);
2076 * Keep a reference count on polling to allow recursive
2077 * cngrab() during a panic for example.
2081 else if (sc->sc_polling > 0)
2084 if (sc->sc_polling != 0) {
2085 sc->sc_flags |= UKBD_FLAG_POLLING;
2086 sc->sc_poll_thread = curthread;
2088 sc->sc_flags &= ~UKBD_FLAG_POLLING;
2089 ukbd_start_timer(sc); /* start timer */
2094 /* local functions */
2097 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
2099 DPRINTF("leds=0x%02x\n", leds);
2102 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
2104 /* start transfer, if not already started */
2106 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
2110 ukbd_set_typematic(keyboard_t *kbd, int code)
2112 static const int delays[] = {250, 500, 750, 1000};
2113 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
2114 68, 76, 84, 92, 100, 110, 118, 126,
2115 136, 152, 168, 184, 200, 220, 236, 252,
2116 272, 304, 336, 368, 400, 440, 472, 504};
2121 kbd->kb_delay1 = delays[(code >> 5) & 3];
2122 kbd->kb_delay2 = rates[code & 0x1f];
2126 #ifdef UKBD_EMULATE_ATSCANCODE
2128 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
2130 static const int scan[] = {
2136 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
2146 0x151, /* PageDown */
2149 0x146, /* XXX Pause/Break */
2150 0x15b, /* Win_L(Super_L) */
2151 0x15c, /* Win_R(Super_R) */
2152 0x15d, /* Application(Menu) */
2154 /* SUN TYPE 6 USB KEYBOARD */
2155 0x168, /* Sun Type 6 Help */
2156 0x15e, /* Sun Type 6 Stop */
2158 0x15f, /* Sun Type 6 Again */
2159 0x160, /* Sun Type 6 Props */
2160 0x161, /* Sun Type 6 Undo */
2161 0x162, /* Sun Type 6 Front */
2162 0x163, /* Sun Type 6 Copy */
2163 0x164, /* Sun Type 6 Open */
2164 0x165, /* Sun Type 6 Paste */
2165 0x166, /* Sun Type 6 Find */
2166 0x167, /* Sun Type 6 Cut */
2167 0x125, /* Sun Type 6 Mute */
2169 0x11f, /* Sun Type 6 VolumeDown */
2170 0x11e, /* Sun Type 6 VolumeUp */
2171 0x120, /* Sun Type 6 PowerDown */
2173 /* Japanese 106/109 keyboard */
2174 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
2175 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
2176 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
2177 0x79, /* Keyboard Intl' 4 (Henkan) */
2178 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
2179 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
2180 0x71, /* Apple Keyboard JIS (Kana) */
2181 0x72, /* Apple Keyboard JIS (Eisu) */
2184 if ((code >= 89) && (code < (int)(89 + (sizeof(scan) / sizeof(scan[0]))))) {
2185 code = scan[code - 89];
2188 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
2189 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
2191 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
2192 code &= ~SCAN_PREFIX_SHIFT;
2194 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
2196 if (code & SCAN_PREFIX) {
2197 if (code & SCAN_PREFIX_CTL) {
2199 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
2200 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
2201 } else if (code & SCAN_PREFIX_SHIFT) {
2203 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
2204 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
2206 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
2207 sc->sc_buffered_char[1] = 0;
2209 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
2215 #endif /* UKBD_EMULATE_ATSCANCODE */
2217 static keyboard_switch_t ukbdsw = {
2218 .probe = &ukbd__probe,
2222 .test_if = &ukbd_test_if,
2223 .enable = &ukbd_enable,
2224 .disable = &ukbd_disable,
2226 .check = &ukbd_check,
2227 .read_char = &ukbd_read_char,
2228 .check_char = &ukbd_check_char,
2229 .ioctl = &ukbd_ioctl,
2231 .clear_state = &ukbd_clear_state,
2232 .get_state = &ukbd_get_state,
2233 .set_state = &ukbd_set_state,
2234 .get_fkeystr = &genkbd_get_fkeystr,
2236 .diag = &genkbd_diag,
2239 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
2242 ukbd_driver_load(module_t mod, int what, void *arg)
2246 kbd_add_driver(&ukbd_kbd_driver);
2249 kbd_delete_driver(&ukbd_kbd_driver);
2255 static devclass_t ukbd_devclass;
2257 static device_method_t ukbd_methods[] = {
2258 DEVMETHOD(device_probe, ukbd_probe),
2259 DEVMETHOD(device_attach, ukbd_attach),
2260 DEVMETHOD(device_detach, ukbd_detach),
2261 DEVMETHOD(device_resume, ukbd_resume),
2265 static driver_t ukbd_driver = {
2267 .methods = ukbd_methods,
2268 .size = sizeof(struct ukbd_softc),
2271 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
2272 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
2273 MODULE_VERSION(ukbd, 1);