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 static SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
97 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
98 &ukbd_debug, 0, "Debug level");
99 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW,
100 &ukbd_no_leds, 0, "Disables setting of keyboard leds");
102 TUNABLE_INT("hw.usb.ukbd.debug", &ukbd_debug);
103 TUNABLE_INT("hw.usb.ukbd.no_leds", &ukbd_no_leds);
106 #define UKBD_EMULATE_ATSCANCODE 1
107 #define UKBD_DRIVER_NAME "ukbd"
108 #define UKBD_NMOD 8 /* units */
109 #define UKBD_NKEYCODE 6 /* units */
110 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */
111 #define UKBD_IN_BUF_FULL (UKBD_IN_BUF_SIZE / 2) /* bytes */
112 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
113 #define UKBD_BUFFER_SIZE 64 /* bytes */
117 #define MOD_CONTROL_L 0x01
118 #define MOD_CONTROL_R 0x10
119 #define MOD_SHIFT_L 0x02
120 #define MOD_SHIFT_R 0x20
121 #define MOD_ALT_L 0x04
122 #define MOD_ALT_R 0x40
123 #define MOD_WIN_L 0x08
124 #define MOD_WIN_R 0x80
126 #define MOD_EJECT 0x0100
127 #define MOD_FN 0x0200
128 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) */
205 uint16_t sc_inputhead;
206 uint16_t sc_inputtail;
207 uint16_t sc_modifiers;
209 uint8_t sc_leds; /* store for async led requests */
210 uint8_t sc_iface_index;
212 uint8_t sc_id_apple_eject;
213 uint8_t sc_id_apple_fn;
214 uint8_t sc_id_ctrl_l;
215 uint8_t sc_id_ctrl_r;
216 uint8_t sc_id_shift_l;
217 uint8_t sc_id_shift_r;
223 uint8_t sc_id_numlock;
224 uint8_t sc_id_capslock;
225 uint8_t sc_id_scrolllock;
226 uint8_t sc_id_events;
229 uint8_t sc_buffer[UKBD_BUFFER_SIZE];
232 #define KEY_ERROR 0x01
235 #define KEY_RELEASE 0x400
236 #define KEY_INDEX(c) ((c) & 0xFF)
239 #define SCAN_RELEASE 0x80
240 #define SCAN_PREFIX_E0 0x100
241 #define SCAN_PREFIX_E1 0x200
242 #define SCAN_PREFIX_CTL 0x400
243 #define SCAN_PREFIX_SHIFT 0x800
244 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
245 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
246 #define SCAN_CHAR(c) ((c) & 0x7f)
248 #define UKBD_LOCK() mtx_lock(&Giant)
249 #define UKBD_UNLOCK() mtx_unlock(&Giant)
254 * Assert that the lock is held in all contexts
255 * where the code can be executed.
257 #define UKBD_LOCK_ASSERT() mtx_assert(&Giant, MA_OWNED)
260 * Assert that the lock is held in the contexts
261 * where it really has to be so.
263 #define UKBD_CTX_LOCK_ASSERT() \
265 if (!kdb_active && panicstr == NULL) \
266 mtx_assert(&Giant, MA_OWNED); \
270 #define UKBD_LOCK_ASSERT() (void)0
271 #define UKBD_CTX_LOCK_ASSERT() (void)0
279 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
280 {MOD_CONTROL_L, 0xe0},
281 {MOD_CONTROL_R, 0xe4},
290 #define NN 0 /* no translation */
292 * Translate USB keycodes to AT keyboard scancodes.
295 * FIXME: Mac USB keyboard generates:
296 * 0x53: keypad NumLock/Clear
303 static const uint8_t ukbd_trtab[256] = {
304 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
305 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
306 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
307 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
308 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
309 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
310 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
311 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
312 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
313 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
314 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
315 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
316 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
317 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
318 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
319 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
320 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */
321 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */
322 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
323 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
324 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
325 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
326 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
327 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
328 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
329 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
330 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
331 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
332 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
333 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
334 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
335 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
338 static const uint8_t ukbd_boot_desc[] = {
339 0x05, 0x01, 0x09, 0x06, 0xa1,
340 0x01, 0x05, 0x07, 0x19, 0xe0,
341 0x29, 0xe7, 0x15, 0x00, 0x25,
342 0x01, 0x75, 0x01, 0x95, 0x08,
343 0x81, 0x02, 0x95, 0x01, 0x75,
344 0x08, 0x81, 0x01, 0x95, 0x03,
345 0x75, 0x01, 0x05, 0x08, 0x19,
346 0x01, 0x29, 0x03, 0x91, 0x02,
347 0x95, 0x05, 0x75, 0x01, 0x91,
348 0x01, 0x95, 0x06, 0x75, 0x08,
349 0x15, 0x00, 0x26, 0xff, 0x00,
350 0x05, 0x07, 0x19, 0x00, 0x2a,
351 0xff, 0x00, 0x81, 0x00, 0xc0
355 static void ukbd_timeout(void *);
356 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
357 static int ukbd_set_typematic(keyboard_t *, int);
358 #ifdef UKBD_EMULATE_ATSCANCODE
359 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
361 static uint32_t ukbd_read_char(keyboard_t *, int);
362 static void ukbd_clear_state(keyboard_t *);
363 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
364 static int ukbd_enable(keyboard_t *);
365 static int ukbd_disable(keyboard_t *);
366 static void ukbd_interrupt(struct ukbd_softc *);
367 static void ukbd_event_keyinput(struct ukbd_softc *);
369 static device_probe_t ukbd_probe;
370 static device_attach_t ukbd_attach;
371 static device_detach_t ukbd_detach;
372 static device_resume_t ukbd_resume;
375 ukbd_any_key_pressed(struct ukbd_softc *sc)
380 for (j = i = 0; i < UKBD_NKEYCODE; i++)
381 j |= sc->sc_odata.keycode[i];
387 ukbd_start_timer(struct ukbd_softc *sc)
389 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
390 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
394 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
397 UKBD_CTX_LOCK_ASSERT();
399 DPRINTF("0x%02x (%d) %s\n", key, key,
400 (key & KEY_RELEASE) ? "released" : "pressed");
402 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
403 sc->sc_input[sc->sc_inputtail] = key;
405 ++(sc->sc_inputtail);
406 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
407 sc->sc_inputtail = 0;
410 DPRINTF("input buffer is full\n");
415 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
418 UKBD_CTX_LOCK_ASSERT();
419 KASSERT((sc->sc_flags & UKBD_FLAG_POLLING) != 0,
420 ("ukbd_do_poll called when not polling\n"));
421 DPRINTFN(2, "polling\n");
423 if (!kdb_active && !SCHEDULER_STOPPED()) {
425 * In this context the kernel is polling for input,
426 * but the USB subsystem works in normal interrupt-driven
427 * mode, so we just wait on the USB threads to do the job.
428 * Note that we currently hold the Giant, but it's also used
429 * as the transfer mtx, so we must release it while waiting.
431 while (sc->sc_inputs == 0) {
433 * Give USB threads a chance to run. Note that
434 * kern_yield performs DROP_GIANT + PICKUP_GIANT.
436 kern_yield(PRI_UNCHANGED);
443 while (sc->sc_inputs == 0) {
445 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
447 /* Delay-optimised support for repetition of keys */
448 if (ukbd_any_key_pressed(sc)) {
449 /* a key is pressed - need timekeeping */
452 /* 1 millisecond has passed */
464 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
468 UKBD_CTX_LOCK_ASSERT();
469 KASSERT((!kdb_active && !SCHEDULER_STOPPED())
470 || (sc->sc_flags & UKBD_FLAG_POLLING) != 0,
471 ("not polling in kdb or panic\n"));
473 if (sc->sc_inputs == 0) {
474 /* start transfer, if not already started */
475 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
478 if (sc->sc_flags & UKBD_FLAG_POLLING)
479 ukbd_do_poll(sc, wait);
481 if (sc->sc_inputs == 0) {
484 c = sc->sc_input[sc->sc_inputhead];
486 ++(sc->sc_inputhead);
487 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
488 sc->sc_inputhead = 0;
495 ukbd_interrupt(struct ukbd_softc *sc)
499 uint32_t now = sc->sc_time_ms;
505 UKBD_CTX_LOCK_ASSERT();
507 if (sc->sc_ndata.keycode[0] == KEY_ERROR)
510 n_mod = sc->sc_ndata.modifiers;
511 o_mod = sc->sc_odata.modifiers;
512 if (n_mod != o_mod) {
513 for (i = 0; i < UKBD_NMOD; i++) {
514 if ((n_mod & ukbd_mods[i].mask) !=
515 (o_mod & ukbd_mods[i].mask)) {
516 ukbd_put_key(sc, ukbd_mods[i].key |
517 ((n_mod & ukbd_mods[i].mask) ?
518 KEY_PRESS : KEY_RELEASE));
522 /* Check for released keys. */
523 for (i = 0; i < UKBD_NKEYCODE; i++) {
524 key = sc->sc_odata.keycode[i];
528 for (j = 0; j < UKBD_NKEYCODE; j++) {
529 if (sc->sc_ndata.keycode[j] == 0) {
532 if (key == sc->sc_ndata.keycode[j]) {
536 ukbd_put_key(sc, key | KEY_RELEASE);
540 /* Check for pressed keys. */
541 for (i = 0; i < UKBD_NKEYCODE; i++) {
542 key = sc->sc_ndata.keycode[i];
546 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
547 for (j = 0; j < UKBD_NKEYCODE; j++) {
548 if (sc->sc_odata.keycode[j] == 0) {
551 if (key == sc->sc_odata.keycode[j]) {
553 /* key is still pressed */
555 sc->sc_ntime[i] = sc->sc_otime[j];
556 dtime = (sc->sc_otime[j] - now);
558 if (!(dtime & 0x80000000)) {
559 /* time has not elapsed */
562 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
566 ukbd_put_key(sc, key | KEY_PRESS);
569 * If any other key is presently down, force its repeat to be
570 * well in the future (100s). This makes the last key to be
571 * pressed do the autorepeat.
573 for (j = 0; j != UKBD_NKEYCODE; j++) {
575 sc->sc_ntime[j] = now + (100 * 1000);
580 sc->sc_odata = sc->sc_ndata;
582 memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
584 ukbd_event_keyinput(sc);
588 ukbd_event_keyinput(struct ukbd_softc *sc)
592 UKBD_CTX_LOCK_ASSERT();
594 if ((sc->sc_flags & UKBD_FLAG_POLLING) != 0)
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;
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;
668 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
669 pc = usbd_xfer_get_frame(xfer, 0);
671 switch (USB_GET_STATE(xfer)) {
672 case USB_ST_TRANSFERRED:
673 DPRINTF("actlen=%d bytes\n", len);
676 DPRINTF("zero length data\n");
680 if (sc->sc_kbd_id != 0) {
681 /* check and remove HID ID byte */
682 usbd_copy_out(pc, 0, &id, 1);
686 DPRINTF("zero length data\n");
694 if (len > UKBD_BUFFER_SIZE)
695 len = UKBD_BUFFER_SIZE;
698 usbd_copy_out(pc, offset, sc->sc_buffer, len);
700 /* clear temporary storage */
701 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
703 /* scan through HID data */
704 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
705 (id == sc->sc_id_apple_eject)) {
706 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject))
707 sc->sc_modifiers |= MOD_EJECT;
709 sc->sc_modifiers &= ~MOD_EJECT;
711 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
712 (id == sc->sc_id_apple_fn)) {
713 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn))
714 sc->sc_modifiers |= MOD_FN;
716 sc->sc_modifiers &= ~MOD_FN;
718 if ((sc->sc_flags & UKBD_FLAG_CTRL_L) &&
719 (id == sc->sc_id_ctrl_l)) {
720 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_l))
721 sc-> sc_modifiers |= MOD_CONTROL_L;
723 sc-> sc_modifiers &= ~MOD_CONTROL_L;
725 if ((sc->sc_flags & UKBD_FLAG_CTRL_R) &&
726 (id == sc->sc_id_ctrl_r)) {
727 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_r))
728 sc->sc_modifiers |= MOD_CONTROL_R;
730 sc->sc_modifiers &= ~MOD_CONTROL_R;
732 if ((sc->sc_flags & UKBD_FLAG_SHIFT_L) &&
733 (id == sc->sc_id_shift_l)) {
734 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_l))
735 sc->sc_modifiers |= MOD_SHIFT_L;
737 sc->sc_modifiers &= ~MOD_SHIFT_L;
739 if ((sc->sc_flags & UKBD_FLAG_SHIFT_R) &&
740 (id == sc->sc_id_shift_r)) {
741 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_r))
742 sc->sc_modifiers |= MOD_SHIFT_R;
744 sc->sc_modifiers &= ~MOD_SHIFT_R;
746 if ((sc->sc_flags & UKBD_FLAG_ALT_L) &&
747 (id == sc->sc_id_alt_l)) {
748 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_l))
749 sc->sc_modifiers |= MOD_ALT_L;
751 sc->sc_modifiers &= ~MOD_ALT_L;
753 if ((sc->sc_flags & UKBD_FLAG_ALT_R) &&
754 (id == sc->sc_id_alt_r)) {
755 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_r))
756 sc->sc_modifiers |= MOD_ALT_R;
758 sc->sc_modifiers &= ~MOD_ALT_R;
760 if ((sc->sc_flags & UKBD_FLAG_WIN_L) &&
761 (id == sc->sc_id_win_l)) {
762 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_l))
763 sc->sc_modifiers |= MOD_WIN_L;
765 sc->sc_modifiers &= ~MOD_WIN_L;
767 if ((sc->sc_flags & UKBD_FLAG_WIN_R) &&
768 (id == sc->sc_id_win_r)) {
769 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_r))
770 sc->sc_modifiers |= MOD_WIN_R;
772 sc->sc_modifiers &= ~MOD_WIN_R;
775 sc->sc_ndata.modifiers = sc->sc_modifiers;
777 if ((sc->sc_flags & UKBD_FLAG_EVENTS) &&
778 (id == sc->sc_id_events)) {
779 i = sc->sc_loc_events.count;
780 if (i > UKBD_NKEYCODE)
785 sc->sc_ndata.keycode[i] =
786 hid_get_data(sc->sc_buffer + i, len - i,
792 DPRINTF("modifiers = 0x%04x\n", (int)sc->sc_modifiers);
793 for (i = 0; i < UKBD_NKEYCODE; i++) {
794 if (sc->sc_ndata.keycode[i]) {
795 DPRINTF("[%d] = 0x%02x\n",
796 (int)i, (int)sc->sc_ndata.keycode[i]);
800 if (sc->sc_modifiers & MOD_FN) {
801 for (i = 0; i < UKBD_NKEYCODE; i++) {
802 sc->sc_ndata.keycode[i] =
803 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
807 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
808 for (i = 0; i < UKBD_NKEYCODE; i++) {
809 sc->sc_ndata.keycode[i] =
810 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
816 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
817 if (ukbd_any_key_pressed(sc)) {
818 ukbd_start_timer(sc);
824 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
825 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
826 usbd_transfer_submit(xfer);
828 DPRINTF("input queue is full!\n");
833 DPRINTF("error=%s\n", usbd_errstr(error));
835 if (error != USB_ERR_CANCELLED) {
836 /* try to clear stall first */
837 usbd_xfer_set_stall(xfer);
845 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
847 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
848 struct usb_device_request req;
849 struct usb_page_cache *pc;
861 switch (USB_GET_STATE(xfer)) {
862 case USB_ST_TRANSFERRED:
864 if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS))
866 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
868 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
869 req.bRequest = UR_SET_REPORT;
870 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
871 req.wIndex[0] = sc->sc_iface_no;
875 memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE);
880 /* Assumption: All led bits must be in the same ID. */
882 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) {
883 if (sc->sc_leds & NLKED) {
884 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
885 &sc->sc_loc_numlock, 1);
887 id = sc->sc_id_numlock;
891 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) {
892 if (sc->sc_leds & SLKED) {
893 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
894 &sc->sc_loc_scrolllock, 1);
896 id = sc->sc_id_scrolllock;
900 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) {
901 if (sc->sc_leds & CLKED) {
902 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
903 &sc->sc_loc_capslock, 1);
905 id = sc->sc_id_capslock;
909 /* if no leds, nothing to do */
913 /* range check output report length */
914 len = sc->sc_led_size;
915 if (len > (UKBD_BUFFER_SIZE - 1))
916 len = (UKBD_BUFFER_SIZE - 1);
918 /* check if we need to prefix an ID byte */
919 sc->sc_buffer[0] = id;
921 pc = usbd_xfer_get_frame(xfer, 1);
924 usbd_copy_in(pc, 0, sc->sc_buffer, len);
926 usbd_copy_in(pc, 0, sc->sc_buffer + 1, len);
928 req.wLength[0] = len;
929 usbd_xfer_set_frame_len(xfer, 1, len);
931 DPRINTF("len=%d, id=%d\n", len, id);
933 /* setup control request last */
934 pc = usbd_xfer_get_frame(xfer, 0);
935 usbd_copy_in(pc, 0, &req, sizeof(req));
936 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
938 /* start data transfer */
939 usbd_xfer_set_frames(xfer, 2);
940 usbd_transfer_submit(xfer);
944 DPRINTFN(1, "error=%s\n", usbd_errstr(error));
949 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
952 .type = UE_INTERRUPT,
953 .endpoint = UE_ADDR_ANY,
954 .direction = UE_DIR_IN,
955 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
956 .bufsize = 0, /* use wMaxPacketSize */
957 .callback = &ukbd_intr_callback,
962 .endpoint = 0x00, /* Control pipe */
963 .direction = UE_DIR_ANY,
964 .bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE,
965 .callback = &ukbd_set_leds_callback,
966 .timeout = 1000, /* 1 second */
970 /* A match on these entries will load ukbd */
971 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = {
972 {USB_IFACE_CLASS(UICLASS_HID),
973 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
974 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),},
978 ukbd_probe(device_t dev)
980 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
981 struct usb_attach_arg *uaa = device_get_ivars(dev);
992 if (uaa->usb_mode != USB_MODE_HOST) {
996 if (uaa->info.bInterfaceClass != UICLASS_HID)
999 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
1000 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) {
1001 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1004 return (BUS_PROBE_DEFAULT);
1007 error = usbd_req_get_hid_desc(uaa->device, NULL,
1008 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
1014 * NOTE: we currently don't support USB mouse and USB keyboard
1015 * on the same USB endpoint.
1017 if (hid_is_collection(d_ptr, d_len,
1018 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
1019 /* most likely a mouse */
1021 } else if (hid_is_collection(d_ptr, d_len,
1022 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
1023 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1026 error = BUS_PROBE_DEFAULT;
1030 free(d_ptr, M_TEMP);
1035 ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len)
1039 /* reset detected bits */
1040 sc->sc_flags &= ~UKBD_FLAG_HID_MASK;
1042 /* check if there is an ID byte */
1043 sc->sc_kbd_size = hid_report_size(ptr, len,
1044 hid_input, &sc->sc_kbd_id);
1046 /* investigate if this is an Apple Keyboard */
1047 if (hid_locate(ptr, len,
1048 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
1049 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
1050 &sc->sc_id_apple_eject)) {
1051 if (flags & HIO_VARIABLE)
1052 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
1053 UKBD_FLAG_APPLE_SWAP;
1054 DPRINTFN(1, "Found Apple eject-key\n");
1056 if (hid_locate(ptr, len,
1057 HID_USAGE2(0xFFFF, 0x0003),
1058 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
1059 &sc->sc_id_apple_fn)) {
1060 if (flags & HIO_VARIABLE)
1061 sc->sc_flags |= UKBD_FLAG_APPLE_FN;
1062 DPRINTFN(1, "Found Apple FN-key\n");
1064 /* figure out some keys */
1065 if (hid_locate(ptr, len,
1066 HID_USAGE2(HUP_KEYBOARD, 0xE0),
1067 hid_input, 0, &sc->sc_loc_ctrl_l, &flags,
1068 &sc->sc_id_ctrl_l)) {
1069 if (flags & HIO_VARIABLE)
1070 sc->sc_flags |= UKBD_FLAG_CTRL_L;
1071 DPRINTFN(1, "Found left control\n");
1073 if (hid_locate(ptr, len,
1074 HID_USAGE2(HUP_KEYBOARD, 0xE4),
1075 hid_input, 0, &sc->sc_loc_ctrl_r, &flags,
1076 &sc->sc_id_ctrl_r)) {
1077 if (flags & HIO_VARIABLE)
1078 sc->sc_flags |= UKBD_FLAG_CTRL_R;
1079 DPRINTFN(1, "Found right control\n");
1081 if (hid_locate(ptr, len,
1082 HID_USAGE2(HUP_KEYBOARD, 0xE1),
1083 hid_input, 0, &sc->sc_loc_shift_l, &flags,
1084 &sc->sc_id_shift_l)) {
1085 if (flags & HIO_VARIABLE)
1086 sc->sc_flags |= UKBD_FLAG_SHIFT_L;
1087 DPRINTFN(1, "Found left shift\n");
1089 if (hid_locate(ptr, len,
1090 HID_USAGE2(HUP_KEYBOARD, 0xE5),
1091 hid_input, 0, &sc->sc_loc_shift_r, &flags,
1092 &sc->sc_id_shift_r)) {
1093 if (flags & HIO_VARIABLE)
1094 sc->sc_flags |= UKBD_FLAG_SHIFT_R;
1095 DPRINTFN(1, "Found right shift\n");
1097 if (hid_locate(ptr, len,
1098 HID_USAGE2(HUP_KEYBOARD, 0xE2),
1099 hid_input, 0, &sc->sc_loc_alt_l, &flags,
1100 &sc->sc_id_alt_l)) {
1101 if (flags & HIO_VARIABLE)
1102 sc->sc_flags |= UKBD_FLAG_ALT_L;
1103 DPRINTFN(1, "Found left alt\n");
1105 if (hid_locate(ptr, len,
1106 HID_USAGE2(HUP_KEYBOARD, 0xE6),
1107 hid_input, 0, &sc->sc_loc_alt_r, &flags,
1108 &sc->sc_id_alt_r)) {
1109 if (flags & HIO_VARIABLE)
1110 sc->sc_flags |= UKBD_FLAG_ALT_R;
1111 DPRINTFN(1, "Found right alt\n");
1113 if (hid_locate(ptr, len,
1114 HID_USAGE2(HUP_KEYBOARD, 0xE3),
1115 hid_input, 0, &sc->sc_loc_win_l, &flags,
1116 &sc->sc_id_win_l)) {
1117 if (flags & HIO_VARIABLE)
1118 sc->sc_flags |= UKBD_FLAG_WIN_L;
1119 DPRINTFN(1, "Found left GUI\n");
1121 if (hid_locate(ptr, len,
1122 HID_USAGE2(HUP_KEYBOARD, 0xE7),
1123 hid_input, 0, &sc->sc_loc_win_r, &flags,
1124 &sc->sc_id_win_r)) {
1125 if (flags & HIO_VARIABLE)
1126 sc->sc_flags |= UKBD_FLAG_WIN_R;
1127 DPRINTFN(1, "Found right GUI\n");
1129 /* figure out event buffer */
1130 if (hid_locate(ptr, len,
1131 HID_USAGE2(HUP_KEYBOARD, 0x00),
1132 hid_input, 0, &sc->sc_loc_events, &flags,
1133 &sc->sc_id_events)) {
1134 sc->sc_flags |= UKBD_FLAG_EVENTS;
1135 DPRINTFN(1, "Found keyboard events\n");
1138 /* figure out leds on keyboard */
1139 sc->sc_led_size = hid_report_size(ptr, len,
1142 if (hid_locate(ptr, len,
1143 HID_USAGE2(HUP_LEDS, 0x01),
1144 hid_output, 0, &sc->sc_loc_numlock, &flags,
1145 &sc->sc_id_numlock)) {
1146 if (flags & HIO_VARIABLE)
1147 sc->sc_flags |= UKBD_FLAG_NUMLOCK;
1148 DPRINTFN(1, "Found keyboard numlock\n");
1150 if (hid_locate(ptr, len,
1151 HID_USAGE2(HUP_LEDS, 0x02),
1152 hid_output, 0, &sc->sc_loc_capslock, &flags,
1153 &sc->sc_id_capslock)) {
1154 if (flags & HIO_VARIABLE)
1155 sc->sc_flags |= UKBD_FLAG_CAPSLOCK;
1156 DPRINTFN(1, "Found keyboard capslock\n");
1158 if (hid_locate(ptr, len,
1159 HID_USAGE2(HUP_LEDS, 0x03),
1160 hid_output, 0, &sc->sc_loc_scrolllock, &flags,
1161 &sc->sc_id_scrolllock)) {
1162 if (flags & HIO_VARIABLE)
1163 sc->sc_flags |= UKBD_FLAG_SCROLLLOCK;
1164 DPRINTFN(1, "Found keyboard scrolllock\n");
1169 ukbd_attach(device_t dev)
1171 struct ukbd_softc *sc = device_get_softc(dev);
1172 struct usb_attach_arg *uaa = device_get_ivars(dev);
1173 int32_t unit = device_get_unit(dev);
1174 keyboard_t *kbd = &sc->sc_kbd;
1175 void *hid_ptr = NULL;
1182 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
1184 kbd->kb_data = (void *)sc;
1186 device_set_usb_desc(dev);
1188 sc->sc_udev = uaa->device;
1189 sc->sc_iface = uaa->iface;
1190 sc->sc_iface_index = uaa->info.bIfaceIndex;
1191 sc->sc_iface_no = uaa->info.bIfaceNum;
1192 sc->sc_mode = K_XLATE;
1194 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
1196 err = usbd_transfer_setup(uaa->device,
1197 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
1198 UKBD_N_TRANSFER, sc, &Giant);
1201 DPRINTF("error=%s\n", usbd_errstr(err));
1204 /* setup default keyboard maps */
1206 sc->sc_keymap = key_map;
1207 sc->sc_accmap = accent_map;
1208 for (n = 0; n < UKBD_NFKEY; n++) {
1209 sc->sc_fkeymap[n] = fkey_tab[n];
1212 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
1213 sc->sc_fkeymap, UKBD_NFKEY);
1215 KBD_FOUND_DEVICE(kbd);
1217 ukbd_clear_state(kbd);
1220 * FIXME: set the initial value for lock keys in "sc_state"
1221 * according to the BIOS data?
1223 KBD_PROBE_DONE(kbd);
1225 /* get HID descriptor */
1226 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
1227 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
1230 DPRINTF("Parsing HID descriptor of %d bytes\n",
1233 ukbd_parse_hid(sc, hid_ptr, hid_len);
1235 free(hid_ptr, M_TEMP);
1238 /* check if we should use the boot protocol */
1239 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) ||
1240 (err != 0) || (!(sc->sc_flags & UKBD_FLAG_EVENTS))) {
1242 DPRINTF("Forcing boot protocol\n");
1244 err = usbd_req_set_protocol(sc->sc_udev, NULL,
1245 sc->sc_iface_index, 0);
1248 DPRINTF("Set protocol error=%s (ignored)\n",
1252 ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc));
1255 /* ignore if SETIDLE fails, hence it is not crucial */
1256 usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
1258 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
1262 if (kbd_register(kbd) < 0) {
1265 KBD_CONFIG_DONE(kbd);
1269 #ifdef KBD_INSTALL_CDEV
1270 if (kbd_attach(kbd)) {
1274 sc->sc_flags |= UKBD_FLAG_ATTACHED;
1277 genkbd_diag(kbd, bootverbose);
1280 /* start the keyboard */
1281 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
1283 return (0); /* success */
1287 return (ENXIO); /* error */
1291 ukbd_detach(device_t dev)
1293 struct ukbd_softc *sc = device_get_softc(dev);
1300 sc->sc_flags |= UKBD_FLAG_GONE;
1302 usb_callout_stop(&sc->sc_callout);
1304 ukbd_disable(&sc->sc_kbd);
1306 #ifdef KBD_INSTALL_CDEV
1307 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1308 error = kbd_detach(&sc->sc_kbd);
1310 /* usb attach cannot return an error */
1311 device_printf(dev, "WARNING: kbd_detach() "
1312 "returned non-zero! (ignored)\n");
1316 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
1317 error = kbd_unregister(&sc->sc_kbd);
1319 /* usb attach cannot return an error */
1320 device_printf(dev, "WARNING: kbd_unregister() "
1321 "returned non-zero! (ignored)\n");
1324 sc->sc_kbd.kb_flags = 0;
1326 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1328 usb_callout_drain(&sc->sc_callout);
1330 DPRINTF("%s: disconnected\n",
1331 device_get_nameunit(dev));
1337 ukbd_resume(device_t dev)
1339 struct ukbd_softc *sc = device_get_softc(dev);
1343 ukbd_clear_state(&sc->sc_kbd);
1348 /* early keyboard probe, not supported */
1350 ukbd_configure(int flags)
1355 /* detect a keyboard, not used */
1357 ukbd__probe(int unit, void *arg, int flags)
1362 /* reset and initialize the device, not used */
1364 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1369 /* test the interface to the device, not used */
1371 ukbd_test_if(keyboard_t *kbd)
1376 /* finish using this keyboard, not used */
1378 ukbd_term(keyboard_t *kbd)
1383 /* keyboard interrupt routine, not used */
1385 ukbd_intr(keyboard_t *kbd, void *arg)
1390 /* lock the access to the keyboard, not used */
1392 ukbd_lock(keyboard_t *kbd, int lock)
1398 * Enable the access to the device; until this function is called,
1399 * the client cannot read from the keyboard.
1402 ukbd_enable(keyboard_t *kbd)
1412 /* disallow the access to the device */
1414 ukbd_disable(keyboard_t *kbd)
1418 KBD_DEACTIVATE(kbd);
1424 /* check if data is waiting */
1425 /* Currently unused. */
1427 ukbd_check(keyboard_t *kbd)
1429 struct ukbd_softc *sc = kbd->kb_data;
1431 UKBD_CTX_LOCK_ASSERT();
1433 if (!KBD_IS_ACTIVE(kbd))
1436 if (sc->sc_flags & UKBD_FLAG_POLLING)
1437 ukbd_do_poll(sc, 0);
1439 #ifdef UKBD_EMULATE_ATSCANCODE
1440 if (sc->sc_buffered_char[0]) {
1444 if (sc->sc_inputs > 0) {
1450 /* check if char is waiting */
1452 ukbd_check_char_locked(keyboard_t *kbd)
1454 struct ukbd_softc *sc = kbd->kb_data;
1456 UKBD_CTX_LOCK_ASSERT();
1458 if (!KBD_IS_ACTIVE(kbd))
1461 if ((sc->sc_composed_char > 0) &&
1462 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1465 return (ukbd_check(kbd));
1469 ukbd_check_char(keyboard_t *kbd)
1474 result = ukbd_check_char_locked(kbd);
1480 /* read one byte from the keyboard if it's allowed */
1481 /* Currently unused. */
1483 ukbd_read(keyboard_t *kbd, int wait)
1485 struct ukbd_softc *sc = kbd->kb_data;
1487 #ifdef UKBD_EMULATE_ATSCANCODE
1493 UKBD_CTX_LOCK_ASSERT();
1495 if (!KBD_IS_ACTIVE(kbd))
1498 #ifdef UKBD_EMULATE_ATSCANCODE
1499 if (sc->sc_buffered_char[0]) {
1500 scancode = sc->sc_buffered_char[0];
1501 if (scancode & SCAN_PREFIX) {
1502 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1503 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1505 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1506 sc->sc_buffered_char[1] = 0;
1509 #endif /* UKBD_EMULATE_ATSCANCODE */
1512 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1513 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1518 #ifdef UKBD_EMULATE_ATSCANCODE
1519 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1520 if (keycode == NN) {
1523 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1524 (usbcode & KEY_RELEASE)));
1525 #else /* !UKBD_EMULATE_ATSCANCODE */
1527 #endif /* UKBD_EMULATE_ATSCANCODE */
1530 /* read char from the keyboard */
1532 ukbd_read_char_locked(keyboard_t *kbd, int wait)
1534 struct ukbd_softc *sc = kbd->kb_data;
1538 #ifdef UKBD_EMULATE_ATSCANCODE
1542 UKBD_CTX_LOCK_ASSERT();
1544 if (!KBD_IS_ACTIVE(kbd))
1549 /* do we have a composed char to return ? */
1551 if ((sc->sc_composed_char > 0) &&
1552 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1554 action = sc->sc_composed_char;
1555 sc->sc_composed_char = 0;
1557 if (action > 0xFF) {
1562 #ifdef UKBD_EMULATE_ATSCANCODE
1564 /* do we have a pending raw scan code? */
1566 if (sc->sc_mode == K_RAW) {
1567 scancode = sc->sc_buffered_char[0];
1569 if (scancode & SCAN_PREFIX) {
1570 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1571 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1573 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1574 sc->sc_buffered_char[1] = 0;
1578 #endif /* UKBD_EMULATE_ATSCANCODE */
1580 /* see if there is something in the keyboard port */
1582 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1583 if (usbcode == -1) {
1588 #ifdef UKBD_EMULATE_ATSCANCODE
1589 /* USB key index -> key code -> AT scan code */
1590 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1591 if (keycode == NN) {
1594 /* return an AT scan code for the K_RAW mode */
1595 if (sc->sc_mode == K_RAW) {
1596 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1597 (usbcode & KEY_RELEASE)));
1599 #else /* !UKBD_EMULATE_ATSCANCODE */
1601 /* return the byte as is for the K_RAW mode */
1602 if (sc->sc_mode == K_RAW) {
1605 /* USB key index -> key code */
1606 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1607 if (keycode == NN) {
1610 #endif /* UKBD_EMULATE_ATSCANCODE */
1613 case 0x38: /* left alt (compose key) */
1614 if (usbcode & KEY_RELEASE) {
1615 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1616 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1618 if (sc->sc_composed_char > 0xFF) {
1619 sc->sc_composed_char = 0;
1623 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1624 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1625 sc->sc_composed_char = 0;
1629 /* XXX: I don't like these... */
1630 case 0x5c: /* print screen */
1631 if (sc->sc_flags & ALTS) {
1632 keycode = 0x54; /* sysrq */
1635 case 0x68: /* pause/break */
1636 if (sc->sc_flags & CTLS) {
1637 keycode = 0x6c; /* break */
1642 /* return the key code in the K_CODE mode */
1643 if (usbcode & KEY_RELEASE) {
1644 keycode |= SCAN_RELEASE;
1646 if (sc->sc_mode == K_CODE) {
1649 /* compose a character code */
1650 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1652 /* key pressed, process it */
1655 case 0x49: /* keypad 7,8,9 */
1656 sc->sc_composed_char *= 10;
1657 sc->sc_composed_char += keycode - 0x40;
1658 goto check_composed;
1662 case 0x4D: /* keypad 4,5,6 */
1663 sc->sc_composed_char *= 10;
1664 sc->sc_composed_char += keycode - 0x47;
1665 goto check_composed;
1669 case 0x51: /* keypad 1,2,3 */
1670 sc->sc_composed_char *= 10;
1671 sc->sc_composed_char += keycode - 0x4E;
1672 goto check_composed;
1674 case 0x52: /* keypad 0 */
1675 sc->sc_composed_char *= 10;
1676 goto check_composed;
1678 /* key released, no interest here */
1679 case SCAN_RELEASE | 0x47:
1680 case SCAN_RELEASE | 0x48:
1681 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1682 case SCAN_RELEASE | 0x4B:
1683 case SCAN_RELEASE | 0x4C:
1684 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1685 case SCAN_RELEASE | 0x4F:
1686 case SCAN_RELEASE | 0x50:
1687 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1688 case SCAN_RELEASE | 0x52: /* keypad 0 */
1691 case 0x38: /* left alt key */
1695 if (sc->sc_composed_char > 0) {
1696 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1697 sc->sc_composed_char = 0;
1703 /* keycode to key action */
1704 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1705 (keycode & SCAN_RELEASE),
1706 &sc->sc_state, &sc->sc_accents);
1707 if (action == NOKEY) {
1714 if (sc->sc_composed_char <= 0xFF) {
1721 /* Currently wait is always false. */
1723 ukbd_read_char(keyboard_t *kbd, int wait)
1728 keycode = ukbd_read_char_locked(kbd, wait);
1734 /* some useful control functions */
1736 ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
1738 struct ukbd_softc *sc = kbd->kb_data;
1740 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1741 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1749 case KDGKBMODE: /* get keyboard mode */
1750 *(int *)arg = sc->sc_mode;
1752 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1753 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1755 ival = IOCPARM_IVAL(arg);
1756 arg = (caddr_t)&ival;
1759 case KDSKBMODE: /* set keyboard mode */
1760 switch (*(int *)arg) {
1762 if (sc->sc_mode != K_XLATE) {
1763 /* make lock key state and LED state match */
1764 sc->sc_state &= ~LOCK_MASK;
1765 sc->sc_state |= KBD_LED_VAL(kbd);
1770 if (sc->sc_mode != *(int *)arg) {
1771 if ((sc->sc_flags & UKBD_FLAG_POLLING) == 0)
1772 ukbd_clear_state(kbd);
1773 sc->sc_mode = *(int *)arg;
1781 case KDGETLED: /* get keyboard LED */
1782 *(int *)arg = KBD_LED_VAL(kbd);
1784 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1785 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1787 ival = IOCPARM_IVAL(arg);
1788 arg = (caddr_t)&ival;
1791 case KDSETLED: /* set keyboard LED */
1792 /* NOTE: lock key state in "sc_state" won't be changed */
1793 if (*(int *)arg & ~LOCK_MASK)
1798 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1799 if (sc->sc_mode == K_XLATE &&
1800 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1806 if (KBD_HAS_DEVICE(kbd))
1807 ukbd_set_leds(sc, i);
1809 KBD_LED_VAL(kbd) = *(int *)arg;
1811 case KDGKBSTATE: /* get lock key state */
1812 *(int *)arg = sc->sc_state & LOCK_MASK;
1814 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1815 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1817 ival = IOCPARM_IVAL(arg);
1818 arg = (caddr_t)&ival;
1821 case KDSKBSTATE: /* set lock key state */
1822 if (*(int *)arg & ~LOCK_MASK) {
1825 sc->sc_state &= ~LOCK_MASK;
1826 sc->sc_state |= *(int *)arg;
1828 /* set LEDs and quit */
1829 return (ukbd_ioctl(kbd, KDSETLED, arg));
1831 case KDSETREPEAT: /* set keyboard repeat rate (new
1833 if (!KBD_HAS_DEVICE(kbd)) {
1836 if (((int *)arg)[1] < 0) {
1839 if (((int *)arg)[0] < 0) {
1842 if (((int *)arg)[0] < 200) /* fastest possible value */
1843 kbd->kb_delay1 = 200;
1845 kbd->kb_delay1 = ((int *)arg)[0];
1846 kbd->kb_delay2 = ((int *)arg)[1];
1849 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1850 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1852 ival = IOCPARM_IVAL(arg);
1853 arg = (caddr_t)&ival;
1856 case KDSETRAD: /* set keyboard repeat rate (old
1858 return (ukbd_set_typematic(kbd, *(int *)arg));
1860 case PIO_KEYMAP: /* set keyboard translation table */
1861 case OPIO_KEYMAP: /* set keyboard translation table
1863 case PIO_KEYMAPENT: /* set keyboard translation table
1865 case PIO_DEADKEYMAP: /* set accent key translation table */
1869 return (genkbd_commonioctl(kbd, cmd, arg));
1876 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1881 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
1882 * context where printf(9) can be called, which among other things
1883 * includes interrupt filters and threads with any kinds of locks
1884 * already held. For this reason it would be dangerous to acquire
1885 * the Giant here unconditionally. On the other hand we have to
1886 * have it to handle the ioctl.
1887 * So we make our best effort to auto-detect whether we can grab
1888 * the Giant or not. Blame syscons(4) for this.
1894 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
1895 return (EDEADLK); /* best I could come up with */
1899 result = ukbd_ioctl_locked(kbd, cmd, arg);
1906 /* clear the internal state of the keyboard */
1908 ukbd_clear_state(keyboard_t *kbd)
1910 struct ukbd_softc *sc = kbd->kb_data;
1912 UKBD_CTX_LOCK_ASSERT();
1914 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1915 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1917 sc->sc_composed_char = 0;
1918 #ifdef UKBD_EMULATE_ATSCANCODE
1919 sc->sc_buffered_char[0] = 0;
1920 sc->sc_buffered_char[1] = 0;
1922 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1923 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
1924 memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
1925 memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
1928 /* save the internal state, not used */
1930 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1932 return (len == 0) ? 1 : -1;
1935 /* set the internal state, not used */
1937 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1943 ukbd_poll(keyboard_t *kbd, int on)
1945 struct ukbd_softc *sc = kbd->kb_data;
1949 sc->sc_flags |= UKBD_FLAG_POLLING;
1950 sc->sc_poll_thread = curthread;
1952 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1953 ukbd_start_timer(sc); /* start timer */
1960 /* local functions */
1963 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1967 DPRINTF("leds=0x%02x\n", leds);
1970 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1972 /* start transfer, if not already started */
1974 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1978 ukbd_set_typematic(keyboard_t *kbd, int code)
1980 static const int delays[] = {250, 500, 750, 1000};
1981 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1982 68, 76, 84, 92, 100, 110, 118, 126,
1983 136, 152, 168, 184, 200, 220, 236, 252,
1984 272, 304, 336, 368, 400, 440, 472, 504};
1989 kbd->kb_delay1 = delays[(code >> 5) & 3];
1990 kbd->kb_delay2 = rates[code & 0x1f];
1994 #ifdef UKBD_EMULATE_ATSCANCODE
1996 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1998 static const int scan[] = {
2004 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
2014 0x151, /* PageDown */
2017 0x146, /* XXX Pause/Break */
2018 0x15b, /* Win_L(Super_L) */
2019 0x15c, /* Win_R(Super_R) */
2020 0x15d, /* Application(Menu) */
2022 /* SUN TYPE 6 USB KEYBOARD */
2023 0x168, /* Sun Type 6 Help */
2024 0x15e, /* Sun Type 6 Stop */
2026 0x15f, /* Sun Type 6 Again */
2027 0x160, /* Sun Type 6 Props */
2028 0x161, /* Sun Type 6 Undo */
2029 0x162, /* Sun Type 6 Front */
2030 0x163, /* Sun Type 6 Copy */
2031 0x164, /* Sun Type 6 Open */
2032 0x165, /* Sun Type 6 Paste */
2033 0x166, /* Sun Type 6 Find */
2034 0x167, /* Sun Type 6 Cut */
2035 0x125, /* Sun Type 6 Mute */
2037 0x11f, /* Sun Type 6 VolumeDown */
2038 0x11e, /* Sun Type 6 VolumeUp */
2039 0x120, /* Sun Type 6 PowerDown */
2041 /* Japanese 106/109 keyboard */
2042 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
2043 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
2044 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
2045 0x79, /* Keyboard Intl' 4 (Henkan) */
2046 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
2047 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
2050 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
2051 code = scan[code - 89];
2054 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
2055 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
2057 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
2058 code &= ~SCAN_PREFIX_SHIFT;
2060 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
2062 if (code & SCAN_PREFIX) {
2063 if (code & SCAN_PREFIX_CTL) {
2065 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
2066 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
2067 } else if (code & SCAN_PREFIX_SHIFT) {
2069 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
2070 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
2072 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
2073 sc->sc_buffered_char[1] = 0;
2075 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
2081 #endif /* UKBD_EMULATE_ATSCANCODE */
2083 static keyboard_switch_t ukbdsw = {
2084 .probe = &ukbd__probe,
2088 .test_if = &ukbd_test_if,
2089 .enable = &ukbd_enable,
2090 .disable = &ukbd_disable,
2092 .check = &ukbd_check,
2093 .read_char = &ukbd_read_char,
2094 .check_char = &ukbd_check_char,
2095 .ioctl = &ukbd_ioctl,
2097 .clear_state = &ukbd_clear_state,
2098 .get_state = &ukbd_get_state,
2099 .set_state = &ukbd_set_state,
2100 .get_fkeystr = &genkbd_get_fkeystr,
2102 .diag = &genkbd_diag,
2105 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
2108 ukbd_driver_load(module_t mod, int what, void *arg)
2112 kbd_add_driver(&ukbd_kbd_driver);
2115 kbd_delete_driver(&ukbd_kbd_driver);
2121 static devclass_t ukbd_devclass;
2123 static device_method_t ukbd_methods[] = {
2124 DEVMETHOD(device_probe, ukbd_probe),
2125 DEVMETHOD(device_attach, ukbd_attach),
2126 DEVMETHOD(device_detach, ukbd_detach),
2127 DEVMETHOD(device_resume, ukbd_resume),
2131 static driver_t ukbd_driver = {
2133 .methods = ukbd_methods,
2134 .size = sizeof(struct ukbd_softc),
2137 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
2138 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
2139 MODULE_VERSION(ukbd, 1);