2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Lennart Augustsson (lennart@augustsson.net) at
7 * Carlstedt Research & Technology.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
33 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
36 #include "opt_compat.h"
40 #include <sys/stdint.h>
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/module.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
56 #include <sys/sched.h>
57 #include <sys/thread2.h>
59 #include <bus/u4b/usb.h>
60 #include <bus/u4b/usbdi.h>
61 #include <bus/u4b/usbdi_util.h>
62 #include <bus/u4b/usbhid.h>
64 #define USB_DEBUG_VAR ukbd_debug
65 #include <bus/u4b/usb_debug.h>
67 #include <bus/u4b/quirk/usb_quirk.h>
69 #include <sys/ioccom.h>
70 #include <sys/filio.h>
74 #include <dev/misc/kbd/kbdreg.h>
76 /* the initial key map, accent map and fkey strings */
77 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
78 #define KBD_DFLT_KEYMAP
82 /* the following file must be included after "ukbdmap.h" */
83 #include <dev/misc/kbd/kbdtables.h>
86 static int ukbd_debug = 0;
87 static int ukbd_no_leds = 0;
89 static SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
90 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
91 &ukbd_debug, 0, "Debug level");
92 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW,
93 &ukbd_no_leds, 0, "Disables setting of keyboard leds");
95 TUNABLE_INT("hw.usb.ukbd.debug", &ukbd_debug);
96 TUNABLE_INT("hw.usb.ukbd.no_leds", &ukbd_no_leds);
99 #define UKBD_EMULATE_ATSCANCODE 1
100 #define UKBD_DRIVER_NAME "ukbd"
101 #define UKBD_NMOD 8 /* units */
102 #define UKBD_NKEYCODE 6 /* units */
103 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */
104 #define UKBD_IN_BUF_FULL (UKBD_IN_BUF_SIZE / 2) /* bytes */
105 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
106 #define UKBD_BUFFER_SIZE 64 /* bytes */
110 #define MOD_CONTROL_L 0x01
111 #define MOD_CONTROL_R 0x10
112 #define MOD_SHIFT_L 0x02
113 #define MOD_SHIFT_R 0x20
114 #define MOD_ALT_L 0x04
115 #define MOD_ALT_R 0x40
116 #define MOD_WIN_L 0x08
117 #define MOD_WIN_R 0x80
119 #define MOD_EJECT 0x0100
120 #define MOD_FN 0x0200
121 uint8_t keycode[UKBD_NKEYCODE];
134 accentmap_t sc_accmap;
136 fkeytab_t sc_fkeymap[UKBD_NFKEY];
137 struct hid_location sc_loc_apple_eject;
138 struct hid_location sc_loc_apple_fn;
139 struct hid_location sc_loc_ctrl_l;
140 struct hid_location sc_loc_ctrl_r;
141 struct hid_location sc_loc_shift_l;
142 struct hid_location sc_loc_shift_r;
143 struct hid_location sc_loc_alt_l;
144 struct hid_location sc_loc_alt_r;
145 struct hid_location sc_loc_win_l;
146 struct hid_location sc_loc_win_r;
147 struct hid_location sc_loc_events;
148 struct hid_location sc_loc_numlock;
149 struct hid_location sc_loc_capslock;
150 struct hid_location sc_loc_scrolllock;
151 struct usb_callout sc_callout;
152 struct ukbd_data sc_ndata;
153 struct ukbd_data sc_odata;
155 struct thread *sc_poll_thread;
156 struct usb_device *sc_udev;
157 struct usb_interface *sc_iface;
158 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
160 uint32_t sc_ntime[UKBD_NKEYCODE];
161 uint32_t sc_otime[UKBD_NKEYCODE];
162 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
164 uint32_t sc_composed_char; /* composed char code, if non-zero */
165 #ifdef UKBD_EMULATE_ATSCANCODE
166 uint32_t sc_buffered_char[2];
168 uint32_t sc_flags; /* flags */
169 #define UKBD_FLAG_COMPOSE 0x00000001
170 #define UKBD_FLAG_POLLING 0x00000002
171 #define UKBD_FLAG_SET_LEDS 0x00000004
172 #define UKBD_FLAG_ATTACHED 0x00000010
173 #define UKBD_FLAG_GONE 0x00000020
175 #define UKBD_FLAG_HID_MASK 0x003fffc0
176 #define UKBD_FLAG_APPLE_EJECT 0x00000040
177 #define UKBD_FLAG_APPLE_FN 0x00000080
178 #define UKBD_FLAG_APPLE_SWAP 0x00000100
179 #define UKBD_FLAG_TIMER_RUNNING 0x00000200
180 #define UKBD_FLAG_CTRL_L 0x00000400
181 #define UKBD_FLAG_CTRL_R 0x00000800
182 #define UKBD_FLAG_SHIFT_L 0x00001000
183 #define UKBD_FLAG_SHIFT_R 0x00002000
184 #define UKBD_FLAG_ALT_L 0x00004000
185 #define UKBD_FLAG_ALT_R 0x00008000
186 #define UKBD_FLAG_WIN_L 0x00010000
187 #define UKBD_FLAG_WIN_R 0x00020000
188 #define UKBD_FLAG_EVENTS 0x00040000
189 #define UKBD_FLAG_NUMLOCK 0x00080000
190 #define UKBD_FLAG_CAPSLOCK 0x00100000
191 #define UKBD_FLAG_SCROLLLOCK 0x00200000
193 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
194 int sc_state; /* shift/lock key state */
195 int sc_accents; /* accent key index (> 0) */
200 uint16_t sc_inputhead;
201 uint16_t sc_inputtail;
202 uint16_t sc_modifiers;
204 uint8_t sc_leds; /* store for async led requests */
205 uint8_t sc_iface_index;
207 uint8_t sc_id_apple_eject;
208 uint8_t sc_id_apple_fn;
209 uint8_t sc_id_ctrl_l;
210 uint8_t sc_id_ctrl_r;
211 uint8_t sc_id_shift_l;
212 uint8_t sc_id_shift_r;
218 uint8_t sc_id_numlock;
219 uint8_t sc_id_capslock;
220 uint8_t sc_id_scrolllock;
221 uint8_t sc_id_events;
224 uint8_t sc_buffer[UKBD_BUFFER_SIZE];
227 #define KEY_ERROR 0x01
230 #define KEY_RELEASE 0x400
231 #define KEY_INDEX(c) ((c) & 0xFF)
234 #define SCAN_RELEASE 0x80
235 #define SCAN_PREFIX_E0 0x100
236 #define SCAN_PREFIX_E1 0x200
237 #define SCAN_PREFIX_CTL 0x400
238 #define SCAN_PREFIX_SHIFT 0x800
239 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
240 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
241 #define SCAN_CHAR(c) ((c) & 0x7f)
243 #define UKBD_LOCK(sc) lockmgr(&(sc)->sc_kbd.kb_lock, LK_EXCLUSIVE)
244 #define UKBD_UNLOCK(sc) lockmgr(&(sc)->sc_kbd.kb_lock, LK_RELEASE)
249 * Assert that the lock is held in all contexts
250 * where the code can be executed.
252 #define UKBD_LOCK_ASSERT()
255 * Assert that the lock is held in the contexts
256 * where it really has to be so.
258 #define UKBD_CTX_LOCK_ASSERT()
261 #define UKBD_LOCK_ASSERT() (void)0
262 #define UKBD_CTX_LOCK_ASSERT() (void)0
270 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
271 {MOD_CONTROL_L, 0xe0},
272 {MOD_CONTROL_R, 0xe4},
281 #define NN 0 /* no translation */
283 * Translate USB keycodes to AT keyboard scancodes.
286 * FIXME: Mac USB keyboard generates:
287 * 0x53: keypad NumLock/Clear
294 static const uint8_t ukbd_trtab[256] = {
295 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
296 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
297 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
298 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
299 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
300 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
301 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
302 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
303 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
304 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
305 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
306 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
307 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
308 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
309 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
310 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
311 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */
312 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */
313 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
314 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
315 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
316 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
317 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
318 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
319 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
320 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
321 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
322 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
323 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
324 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
325 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
326 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
329 static const uint8_t ukbd_boot_desc[] = {
330 0x05, 0x01, 0x09, 0x06, 0xa1,
331 0x01, 0x05, 0x07, 0x19, 0xe0,
332 0x29, 0xe7, 0x15, 0x00, 0x25,
333 0x01, 0x75, 0x01, 0x95, 0x08,
334 0x81, 0x02, 0x95, 0x01, 0x75,
335 0x08, 0x81, 0x01, 0x95, 0x03,
336 0x75, 0x01, 0x05, 0x08, 0x19,
337 0x01, 0x29, 0x03, 0x91, 0x02,
338 0x95, 0x05, 0x75, 0x01, 0x91,
339 0x01, 0x95, 0x06, 0x75, 0x08,
340 0x15, 0x00, 0x26, 0xff, 0x00,
341 0x05, 0x07, 0x19, 0x00, 0x2a,
342 0xff, 0x00, 0x81, 0x00, 0xc0
346 static void ukbd_timeout(void *);
347 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
348 static int ukbd_set_typematic(keyboard_t *, int);
349 #ifdef UKBD_EMULATE_ATSCANCODE
350 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
352 static uint32_t ukbd_read_char(keyboard_t *, int);
353 static void ukbd_clear_state(keyboard_t *);
354 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
355 static int ukbd_enable(keyboard_t *);
356 static int ukbd_disable(keyboard_t *);
357 static void ukbd_interrupt(struct ukbd_softc *);
358 static void ukbd_event_keyinput(struct ukbd_softc *);
360 static device_probe_t ukbd_probe;
361 static device_attach_t ukbd_attach;
362 static device_detach_t ukbd_detach;
363 static device_resume_t ukbd_resume;
366 ukbd_any_key_pressed(struct ukbd_softc *sc)
371 for (j = i = 0; i < UKBD_NKEYCODE; i++)
372 j |= sc->sc_odata.keycode[i];
378 ukbd_start_timer(struct ukbd_softc *sc)
380 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
381 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
385 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
388 UKBD_CTX_LOCK_ASSERT();
390 DPRINTF("0x%02x (%d) %s\n", key, key,
391 (key & KEY_RELEASE) ? "released" : "pressed");
393 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
394 sc->sc_input[sc->sc_inputtail] = key;
396 ++(sc->sc_inputtail);
397 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
398 sc->sc_inputtail = 0;
401 DPRINTF("input buffer is full\n");
406 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
409 UKBD_CTX_LOCK_ASSERT();
410 KASSERT((sc->sc_flags & UKBD_FLAG_POLLING) != 0,
411 ("ukbd_do_poll called when not polling\n"));
412 DPRINTFN(2, "polling\n");
414 if (!kdb_active && !SCHEDULER_STOPPED()) {
416 * In this context the kernel is polling for input,
417 * but the USB subsystem works in normal interrupt-driven
418 * mode, so we just wait on the USB threads to do the job.
419 * Note that we currently hold the Giant, but it's also used
420 * as the transfer mtx, so we must release it while waiting.
422 while (sc->sc_inputs == 0) {
424 * Give USB threads a chance to run. Note that
425 * kern_yield performs DROP_GIANT + PICKUP_GIANT.
435 while (sc->sc_inputs == 0) {
437 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
439 /* Delay-optimised support for repetition of keys */
440 if (ukbd_any_key_pressed(sc)) {
441 /* a key is pressed - need timekeeping */
444 /* 1 millisecond has passed */
456 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
460 UKBD_CTX_LOCK_ASSERT();
462 KASSERT((!kdb_active && !SCHEDULER_STOPPED())
463 || (sc->sc_flags & UKBD_FLAG_POLLING) != 0,
464 ("not polling in kdb or panic\n"));
467 if (sc->sc_inputs == 0) {
468 /* start transfer, if not already started */
469 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
472 if (sc->sc_flags & UKBD_FLAG_POLLING)
473 ukbd_do_poll(sc, wait);
475 if (sc->sc_inputs == 0) {
478 c = sc->sc_input[sc->sc_inputhead];
480 ++(sc->sc_inputhead);
481 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
482 sc->sc_inputhead = 0;
489 ukbd_interrupt(struct ukbd_softc *sc)
493 uint32_t now = sc->sc_time_ms;
499 UKBD_CTX_LOCK_ASSERT();
501 if (sc->sc_ndata.keycode[0] == KEY_ERROR)
504 n_mod = sc->sc_ndata.modifiers;
505 o_mod = sc->sc_odata.modifiers;
506 if (n_mod != o_mod) {
507 for (i = 0; i < UKBD_NMOD; i++) {
508 if ((n_mod & ukbd_mods[i].mask) !=
509 (o_mod & ukbd_mods[i].mask)) {
510 ukbd_put_key(sc, ukbd_mods[i].key |
511 ((n_mod & ukbd_mods[i].mask) ?
512 KEY_PRESS : KEY_RELEASE));
516 /* Check for released keys. */
517 for (i = 0; i < UKBD_NKEYCODE; i++) {
518 key = sc->sc_odata.keycode[i];
522 for (j = 0; j < UKBD_NKEYCODE; j++) {
523 if (sc->sc_ndata.keycode[j] == 0) {
526 if (key == sc->sc_ndata.keycode[j]) {
530 ukbd_put_key(sc, key | KEY_RELEASE);
534 /* Check for pressed keys. */
535 for (i = 0; i < UKBD_NKEYCODE; i++) {
536 key = sc->sc_ndata.keycode[i];
540 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
541 for (j = 0; j < UKBD_NKEYCODE; j++) {
542 if (sc->sc_odata.keycode[j] == 0) {
545 if (key == sc->sc_odata.keycode[j]) {
547 /* key is still pressed */
549 sc->sc_ntime[i] = sc->sc_otime[j];
550 dtime = (sc->sc_otime[j] - now);
552 if (!(dtime & 0x80000000)) {
553 /* time has not elapsed */
556 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
560 ukbd_put_key(sc, key | KEY_PRESS);
563 * If any other key is presently down, force its repeat to be
564 * well in the future (100s). This makes the last key to be
565 * pressed do the autorepeat.
567 for (j = 0; j != UKBD_NKEYCODE; j++) {
569 sc->sc_ntime[j] = now + (100 * 1000);
574 sc->sc_odata = sc->sc_ndata;
576 memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
578 ukbd_event_keyinput(sc);
582 ukbd_event_keyinput(struct ukbd_softc *sc)
586 UKBD_CTX_LOCK_ASSERT();
588 if ((sc->sc_flags & UKBD_FLAG_POLLING) != 0)
591 if (sc->sc_inputs == 0)
594 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
595 KBD_IS_BUSY(&sc->sc_kbd)) {
596 /* let the callback function process the input */
597 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
598 sc->sc_kbd.kb_callback.kc_arg);
600 /* read and discard the input, no one is waiting for it */
602 c = ukbd_read_char(&sc->sc_kbd, 0);
603 } while (c != NOKEY);
608 ukbd_timeout(void *arg)
610 struct ukbd_softc *sc = arg;
614 sc->sc_time_ms += 25; /* milliseconds */
618 /* Make sure any leftover key events gets read out */
619 ukbd_event_keyinput(sc);
621 if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
622 ukbd_start_timer(sc);
624 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
629 ukbd_apple_fn(uint8_t keycode) {
631 case 0x28: return 0x49; /* RETURN -> INSERT */
632 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
633 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
634 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
635 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
636 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
637 default: return keycode;
642 ukbd_apple_swap(uint8_t keycode) {
644 case 0x35: return 0x64;
645 case 0x64: return 0x35;
646 default: return keycode;
651 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
653 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
654 struct usb_page_cache *pc;
662 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
663 pc = usbd_xfer_get_frame(xfer, 0);
665 switch (USB_GET_STATE(xfer)) {
666 case USB_ST_TRANSFERRED:
667 DPRINTF("actlen=%d bytes\n", len);
670 DPRINTF("zero length data\n");
674 if (sc->sc_kbd_id != 0) {
675 /* check and remove HID ID byte */
676 usbd_copy_out(pc, 0, &id, 1);
680 DPRINTF("zero length data\n");
688 if (len > UKBD_BUFFER_SIZE)
689 len = UKBD_BUFFER_SIZE;
692 usbd_copy_out(pc, offset, sc->sc_buffer, len);
694 /* clear temporary storage */
695 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
697 /* scan through HID data */
698 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
699 (id == sc->sc_id_apple_eject)) {
700 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject))
701 sc->sc_modifiers |= MOD_EJECT;
703 sc->sc_modifiers &= ~MOD_EJECT;
705 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
706 (id == sc->sc_id_apple_fn)) {
707 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn))
708 sc->sc_modifiers |= MOD_FN;
710 sc->sc_modifiers &= ~MOD_FN;
712 if ((sc->sc_flags & UKBD_FLAG_CTRL_L) &&
713 (id == sc->sc_id_ctrl_l)) {
714 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_l))
715 sc-> sc_modifiers |= MOD_CONTROL_L;
717 sc-> sc_modifiers &= ~MOD_CONTROL_L;
719 if ((sc->sc_flags & UKBD_FLAG_CTRL_R) &&
720 (id == sc->sc_id_ctrl_r)) {
721 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_r))
722 sc->sc_modifiers |= MOD_CONTROL_R;
724 sc->sc_modifiers &= ~MOD_CONTROL_R;
726 if ((sc->sc_flags & UKBD_FLAG_SHIFT_L) &&
727 (id == sc->sc_id_shift_l)) {
728 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_l))
729 sc->sc_modifiers |= MOD_SHIFT_L;
731 sc->sc_modifiers &= ~MOD_SHIFT_L;
733 if ((sc->sc_flags & UKBD_FLAG_SHIFT_R) &&
734 (id == sc->sc_id_shift_r)) {
735 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_r))
736 sc->sc_modifiers |= MOD_SHIFT_R;
738 sc->sc_modifiers &= ~MOD_SHIFT_R;
740 if ((sc->sc_flags & UKBD_FLAG_ALT_L) &&
741 (id == sc->sc_id_alt_l)) {
742 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_l))
743 sc->sc_modifiers |= MOD_ALT_L;
745 sc->sc_modifiers &= ~MOD_ALT_L;
747 if ((sc->sc_flags & UKBD_FLAG_ALT_R) &&
748 (id == sc->sc_id_alt_r)) {
749 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_r))
750 sc->sc_modifiers |= MOD_ALT_R;
752 sc->sc_modifiers &= ~MOD_ALT_R;
754 if ((sc->sc_flags & UKBD_FLAG_WIN_L) &&
755 (id == sc->sc_id_win_l)) {
756 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_l))
757 sc->sc_modifiers |= MOD_WIN_L;
759 sc->sc_modifiers &= ~MOD_WIN_L;
761 if ((sc->sc_flags & UKBD_FLAG_WIN_R) &&
762 (id == sc->sc_id_win_r)) {
763 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_r))
764 sc->sc_modifiers |= MOD_WIN_R;
766 sc->sc_modifiers &= ~MOD_WIN_R;
769 sc->sc_ndata.modifiers = sc->sc_modifiers;
771 if ((sc->sc_flags & UKBD_FLAG_EVENTS) &&
772 (id == sc->sc_id_events)) {
773 i = sc->sc_loc_events.count;
774 if (i > UKBD_NKEYCODE)
779 sc->sc_ndata.keycode[i] =
780 hid_get_data(sc->sc_buffer + i, len - i,
786 DPRINTF("modifiers = 0x%04x\n", (int)sc->sc_modifiers);
787 for (i = 0; i < UKBD_NKEYCODE; i++) {
788 if (sc->sc_ndata.keycode[i]) {
789 DPRINTF("[%d] = 0x%02x\n",
790 (int)i, (int)sc->sc_ndata.keycode[i]);
794 if (sc->sc_modifiers & MOD_FN) {
795 for (i = 0; i < UKBD_NKEYCODE; i++) {
796 sc->sc_ndata.keycode[i] =
797 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
801 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
802 for (i = 0; i < UKBD_NKEYCODE; i++) {
803 sc->sc_ndata.keycode[i] =
804 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
810 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
811 if (ukbd_any_key_pressed(sc)) {
812 ukbd_start_timer(sc);
818 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
819 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
820 usbd_transfer_submit(xfer);
822 DPRINTF("input queue is full!\n");
827 DPRINTF("error=%s\n", usbd_errstr(error));
829 if (error != USB_ERR_CANCELLED) {
830 /* try to clear stall first */
831 usbd_xfer_set_stall(xfer);
839 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
841 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
842 struct usb_device_request req;
843 struct usb_page_cache *pc;
855 switch (USB_GET_STATE(xfer)) {
856 case USB_ST_TRANSFERRED:
858 if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS))
860 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
862 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
863 req.bRequest = UR_SET_REPORT;
864 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
865 req.wIndex[0] = sc->sc_iface_no;
869 memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE);
874 /* Assumption: All led bits must be in the same ID. */
876 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) {
877 if (sc->sc_leds & NLKED) {
878 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
879 &sc->sc_loc_numlock, 1);
881 id = sc->sc_id_numlock;
885 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) {
886 if (sc->sc_leds & SLKED) {
887 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
888 &sc->sc_loc_scrolllock, 1);
890 id = sc->sc_id_scrolllock;
894 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) {
895 if (sc->sc_leds & CLKED) {
896 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
897 &sc->sc_loc_capslock, 1);
899 id = sc->sc_id_capslock;
903 /* if no leds, nothing to do */
907 /* range check output report length */
908 len = sc->sc_led_size;
909 if (len > (UKBD_BUFFER_SIZE - 1))
910 len = (UKBD_BUFFER_SIZE - 1);
912 /* check if we need to prefix an ID byte */
913 sc->sc_buffer[0] = id;
915 pc = usbd_xfer_get_frame(xfer, 1);
918 usbd_copy_in(pc, 0, sc->sc_buffer, len);
920 usbd_copy_in(pc, 0, sc->sc_buffer + 1, len);
922 req.wLength[0] = len;
923 usbd_xfer_set_frame_len(xfer, 1, len);
925 DPRINTF("len=%d, id=%d\n", len, id);
927 /* setup control request last */
928 pc = usbd_xfer_get_frame(xfer, 0);
929 usbd_copy_in(pc, 0, &req, sizeof(req));
930 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
932 /* start data transfer */
933 usbd_xfer_set_frames(xfer, 2);
934 usbd_transfer_submit(xfer);
938 DPRINTFN(1, "error=%s\n", usbd_errstr(error));
943 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
946 .type = UE_INTERRUPT,
947 .endpoint = UE_ADDR_ANY,
948 .direction = UE_DIR_IN,
949 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
950 .bufsize = 0, /* use wMaxPacketSize */
951 .callback = &ukbd_intr_callback,
956 .endpoint = 0x00, /* Control pipe */
957 .direction = UE_DIR_ANY,
958 .bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE,
959 .callback = &ukbd_set_leds_callback,
960 .timeout = 1000, /* 1 second */
964 /* A match on these entries will load ukbd */
965 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = {
966 {USB_IFACE_CLASS(UICLASS_HID),
967 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
968 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),},
972 ukbd_probe(device_t dev)
974 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
975 struct usb_attach_arg *uaa = device_get_ivars(dev);
985 if (uaa->usb_mode != USB_MODE_HOST) {
989 if (uaa->info.bInterfaceClass != UICLASS_HID)
992 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
993 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) {
994 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
997 return (BUS_PROBE_DEFAULT);
1000 error = usbd_req_get_hid_desc(uaa->device, NULL,
1001 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
1007 * NOTE: we currently don't support USB mouse and USB keyboard
1008 * on the same USB endpoint.
1010 if (hid_is_collection(d_ptr, d_len,
1011 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
1012 /* most likely a mouse */
1014 } else if (hid_is_collection(d_ptr, d_len,
1015 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
1016 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1019 error = BUS_PROBE_DEFAULT;
1023 kfree(d_ptr, M_TEMP);
1028 ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len)
1032 /* reset detected bits */
1033 sc->sc_flags &= ~UKBD_FLAG_HID_MASK;
1035 /* check if there is an ID byte */
1036 sc->sc_kbd_size = hid_report_size(ptr, len,
1037 hid_input, &sc->sc_kbd_id);
1039 /* investigate if this is an Apple Keyboard */
1040 if (hid_locate(ptr, len,
1041 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
1042 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
1043 &sc->sc_id_apple_eject)) {
1044 if (flags & HIO_VARIABLE)
1045 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
1046 UKBD_FLAG_APPLE_SWAP;
1047 DPRINTFN(1, "Found Apple eject-key\n");
1049 if (hid_locate(ptr, len,
1050 HID_USAGE2(0xFFFF, 0x0003),
1051 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
1052 &sc->sc_id_apple_fn)) {
1053 if (flags & HIO_VARIABLE)
1054 sc->sc_flags |= UKBD_FLAG_APPLE_FN;
1055 DPRINTFN(1, "Found Apple FN-key\n");
1057 /* figure out some keys */
1058 if (hid_locate(ptr, len,
1059 HID_USAGE2(HUP_KEYBOARD, 0xE0),
1060 hid_input, 0, &sc->sc_loc_ctrl_l, &flags,
1061 &sc->sc_id_ctrl_l)) {
1062 if (flags & HIO_VARIABLE)
1063 sc->sc_flags |= UKBD_FLAG_CTRL_L;
1064 DPRINTFN(1, "Found left control\n");
1066 if (hid_locate(ptr, len,
1067 HID_USAGE2(HUP_KEYBOARD, 0xE4),
1068 hid_input, 0, &sc->sc_loc_ctrl_r, &flags,
1069 &sc->sc_id_ctrl_r)) {
1070 if (flags & HIO_VARIABLE)
1071 sc->sc_flags |= UKBD_FLAG_CTRL_R;
1072 DPRINTFN(1, "Found right control\n");
1074 if (hid_locate(ptr, len,
1075 HID_USAGE2(HUP_KEYBOARD, 0xE1),
1076 hid_input, 0, &sc->sc_loc_shift_l, &flags,
1077 &sc->sc_id_shift_l)) {
1078 if (flags & HIO_VARIABLE)
1079 sc->sc_flags |= UKBD_FLAG_SHIFT_L;
1080 DPRINTFN(1, "Found left shift\n");
1082 if (hid_locate(ptr, len,
1083 HID_USAGE2(HUP_KEYBOARD, 0xE5),
1084 hid_input, 0, &sc->sc_loc_shift_r, &flags,
1085 &sc->sc_id_shift_r)) {
1086 if (flags & HIO_VARIABLE)
1087 sc->sc_flags |= UKBD_FLAG_SHIFT_R;
1088 DPRINTFN(1, "Found right shift\n");
1090 if (hid_locate(ptr, len,
1091 HID_USAGE2(HUP_KEYBOARD, 0xE2),
1092 hid_input, 0, &sc->sc_loc_alt_l, &flags,
1093 &sc->sc_id_alt_l)) {
1094 if (flags & HIO_VARIABLE)
1095 sc->sc_flags |= UKBD_FLAG_ALT_L;
1096 DPRINTFN(1, "Found left alt\n");
1098 if (hid_locate(ptr, len,
1099 HID_USAGE2(HUP_KEYBOARD, 0xE6),
1100 hid_input, 0, &sc->sc_loc_alt_r, &flags,
1101 &sc->sc_id_alt_r)) {
1102 if (flags & HIO_VARIABLE)
1103 sc->sc_flags |= UKBD_FLAG_ALT_R;
1104 DPRINTFN(1, "Found right alt\n");
1106 if (hid_locate(ptr, len,
1107 HID_USAGE2(HUP_KEYBOARD, 0xE3),
1108 hid_input, 0, &sc->sc_loc_win_l, &flags,
1109 &sc->sc_id_win_l)) {
1110 if (flags & HIO_VARIABLE)
1111 sc->sc_flags |= UKBD_FLAG_WIN_L;
1112 DPRINTFN(1, "Found left GUI\n");
1114 if (hid_locate(ptr, len,
1115 HID_USAGE2(HUP_KEYBOARD, 0xE7),
1116 hid_input, 0, &sc->sc_loc_win_r, &flags,
1117 &sc->sc_id_win_r)) {
1118 if (flags & HIO_VARIABLE)
1119 sc->sc_flags |= UKBD_FLAG_WIN_R;
1120 DPRINTFN(1, "Found right GUI\n");
1122 /* figure out event buffer */
1123 if (hid_locate(ptr, len,
1124 HID_USAGE2(HUP_KEYBOARD, 0x00),
1125 hid_input, 0, &sc->sc_loc_events, &flags,
1126 &sc->sc_id_events)) {
1127 sc->sc_flags |= UKBD_FLAG_EVENTS;
1128 DPRINTFN(1, "Found keyboard events\n");
1131 /* figure out leds on keyboard */
1132 sc->sc_led_size = hid_report_size(ptr, len,
1135 if (hid_locate(ptr, len,
1136 HID_USAGE2(HUP_LEDS, 0x01),
1137 hid_output, 0, &sc->sc_loc_numlock, &flags,
1138 &sc->sc_id_numlock)) {
1139 if (flags & HIO_VARIABLE)
1140 sc->sc_flags |= UKBD_FLAG_NUMLOCK;
1141 DPRINTFN(1, "Found keyboard numlock\n");
1143 if (hid_locate(ptr, len,
1144 HID_USAGE2(HUP_LEDS, 0x02),
1145 hid_output, 0, &sc->sc_loc_capslock, &flags,
1146 &sc->sc_id_capslock)) {
1147 if (flags & HIO_VARIABLE)
1148 sc->sc_flags |= UKBD_FLAG_CAPSLOCK;
1149 DPRINTFN(1, "Found keyboard capslock\n");
1151 if (hid_locate(ptr, len,
1152 HID_USAGE2(HUP_LEDS, 0x03),
1153 hid_output, 0, &sc->sc_loc_scrolllock, &flags,
1154 &sc->sc_id_scrolllock)) {
1155 if (flags & HIO_VARIABLE)
1156 sc->sc_flags |= UKBD_FLAG_SCROLLLOCK;
1157 DPRINTFN(1, "Found keyboard scrolllock\n");
1162 ukbd_attach(device_t dev)
1164 struct ukbd_softc *sc = device_get_softc(dev);
1165 struct usb_attach_arg *uaa = device_get_ivars(dev);
1166 int32_t unit = device_get_unit(dev);
1167 keyboard_t *kbd = &sc->sc_kbd;
1168 void *hid_ptr = NULL;
1173 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER,
1174 unit, 0, KB_PRI_USB, 0, 0);
1176 kbd->kb_data = (void *)sc;
1178 device_set_usb_desc(dev);
1180 sc->sc_udev = uaa->device;
1181 sc->sc_iface = uaa->iface;
1182 sc->sc_iface_index = uaa->info.bIfaceIndex;
1183 sc->sc_iface_no = uaa->info.bIfaceNum;
1184 sc->sc_mode = K_XLATE;
1186 usb_callout_init_mtx(&sc->sc_callout, &kbd->kb_lock, 0);
1188 err = usbd_transfer_setup(uaa->device,
1189 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
1190 UKBD_N_TRANSFER, sc, &sc->sc_kbd.kb_lock);
1193 DPRINTF("error=%s\n", usbd_errstr(err));
1196 /* setup default keyboard maps */
1198 sc->sc_keymap = key_map;
1199 sc->sc_accmap = accent_map;
1200 for (n = 0; n < UKBD_NFKEY; n++) {
1201 sc->sc_fkeymap[n] = fkey_tab[n];
1204 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
1205 sc->sc_fkeymap, UKBD_NFKEY);
1207 KBD_FOUND_DEVICE(kbd);
1209 ukbd_clear_state(kbd);
1212 * FIXME: set the initial value for lock keys in "sc_state"
1213 * according to the BIOS data?
1215 KBD_PROBE_DONE(kbd);
1217 /* get HID descriptor */
1218 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
1219 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
1222 DPRINTF("Parsing HID descriptor of %d bytes\n",
1225 ukbd_parse_hid(sc, hid_ptr, hid_len);
1227 kfree(hid_ptr, M_TEMP);
1230 /* check if we should use the boot protocol */
1231 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) ||
1232 (err != 0) || (!(sc->sc_flags & UKBD_FLAG_EVENTS))) {
1234 DPRINTF("Forcing boot protocol\n");
1236 err = usbd_req_set_protocol(sc->sc_udev, NULL,
1237 sc->sc_iface_index, 0);
1240 DPRINTF("Set protocol error=%s (ignored)\n",
1244 ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc));
1247 /* ignore if SETIDLE fails, hence it is not crucial */
1248 usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
1250 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
1254 if (kbd_register(kbd) < 0) {
1257 KBD_CONFIG_DONE(kbd);
1261 #ifdef KBD_INSTALL_CDEV
1262 if (kbd_attach(kbd)) {
1266 sc->sc_flags |= UKBD_FLAG_ATTACHED;
1269 genkbd_diag(kbd, bootverbose);
1272 /* start the keyboard */
1274 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
1277 return (0); /* success */
1281 return (ENXIO); /* error */
1285 ukbd_detach(device_t dev)
1287 struct ukbd_softc *sc = device_get_softc(dev);
1293 sc->sc_flags |= UKBD_FLAG_GONE;
1295 usb_callout_stop(&sc->sc_callout);
1297 ukbd_disable(&sc->sc_kbd);
1300 * XXX make sure this is in the correct place here,
1301 * it was taken from below the second if()
1303 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1304 usb_callout_drain(&sc->sc_callout);
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)) {
1318 * kbd_unregister requires kb_lock to be held
1319 * but lockuninits it then
1322 error = kbd_unregister(&sc->sc_kbd);
1324 /* usb attach cannot return an error */
1325 device_printf(dev, "WARNING: kbd_unregister() "
1326 "returned non-zero! (ignored)\n");
1329 sc->sc_kbd.kb_flags = 0;
1334 DPRINTF("%s: disconnected\n",
1335 device_get_nameunit(dev));
1341 ukbd_resume(device_t dev)
1343 struct ukbd_softc *sc = device_get_softc(dev);
1345 ukbd_clear_state(&sc->sc_kbd);
1350 /* early keyboard probe, not supported */
1352 ukbd_configure(int flags)
1357 /* detect a keyboard, not used */
1359 ukbd__probe(int unit, void *arg, int flags)
1364 /* reset and initialize the device, not used */
1366 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1371 /* test the interface to the device, not used */
1373 ukbd_test_if(keyboard_t *kbd)
1378 /* finish using this keyboard, not used */
1380 ukbd_term(keyboard_t *kbd)
1385 /* keyboard interrupt routine, not used */
1387 ukbd_intr(keyboard_t *kbd, void *arg)
1392 /* lock the access to the keyboard, not used */
1394 ukbd_lock(keyboard_t *kbd, int lock)
1400 * Enable the access to the device; until this function is called,
1401 * the client cannot read from the keyboard.
1404 ukbd_enable(keyboard_t *kbd)
1413 /* disallow the access to the device */
1415 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 if (!KBD_IS_ACTIVE(kbd))
1434 if (sc->sc_flags & UKBD_FLAG_POLLING)
1435 ukbd_do_poll(sc, 0);
1437 #ifdef UKBD_EMULATE_ATSCANCODE
1438 if (sc->sc_buffered_char[0]) {
1442 if (sc->sc_inputs > 0) {
1448 /* check if char is waiting */
1450 ukbd_check_char_locked(keyboard_t *kbd)
1452 struct ukbd_softc *sc = kbd->kb_data;
1454 if (!KBD_IS_ACTIVE(kbd))
1457 if ((sc->sc_composed_char > 0) &&
1458 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1461 return (ukbd_check(kbd));
1465 ukbd_check_char(keyboard_t *kbd)
1469 struct ukbd_softc *sc = kbd->kb_data;
1473 result = ukbd_check_char_locked(kbd);
1482 /* read one byte from the keyboard if it's allowed */
1483 /* Currently unused. */
1485 ukbd_read(keyboard_t *kbd, int wait)
1487 struct ukbd_softc *sc = kbd->kb_data;
1489 #ifdef UKBD_EMULATE_ATSCANCODE
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 if (!KBD_IS_ACTIVE(kbd))
1547 /* do we have a composed char to return ? */
1549 if ((sc->sc_composed_char > 0) &&
1550 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1552 action = sc->sc_composed_char;
1553 sc->sc_composed_char = 0;
1555 if (action > 0xFF) {
1560 #ifdef UKBD_EMULATE_ATSCANCODE
1562 /* do we have a pending raw scan code? */
1564 if (sc->sc_mode == K_RAW) {
1565 scancode = sc->sc_buffered_char[0];
1567 if (scancode & SCAN_PREFIX) {
1568 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1569 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1571 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1572 sc->sc_buffered_char[1] = 0;
1576 #endif /* UKBD_EMULATE_ATSCANCODE */
1578 /* see if there is something in the keyboard port */
1580 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1581 if (usbcode == -1) {
1586 #ifdef UKBD_EMULATE_ATSCANCODE
1587 /* USB key index -> key code -> AT scan code */
1588 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1589 if (keycode == NN) {
1592 /* return an AT scan code for the K_RAW mode */
1593 if (sc->sc_mode == K_RAW) {
1594 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1595 (usbcode & KEY_RELEASE)));
1597 #else /* !UKBD_EMULATE_ATSCANCODE */
1599 /* return the byte as is for the K_RAW mode */
1600 if (sc->sc_mode == K_RAW) {
1603 /* USB key index -> key code */
1604 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1605 if (keycode == NN) {
1608 #endif /* UKBD_EMULATE_ATSCANCODE */
1611 case 0x38: /* left alt (compose key) */
1612 if (usbcode & KEY_RELEASE) {
1613 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1614 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1616 if (sc->sc_composed_char > 0xFF) {
1617 sc->sc_composed_char = 0;
1621 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1622 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1623 sc->sc_composed_char = 0;
1627 /* XXX: I don't like these... */
1628 case 0x5c: /* print screen */
1629 if (sc->sc_flags & ALTS) {
1630 keycode = 0x54; /* sysrq */
1633 case 0x68: /* pause/break */
1634 if (sc->sc_flags & CTLS) {
1635 keycode = 0x6c; /* break */
1640 /* return the key code in the K_CODE mode */
1641 if (usbcode & KEY_RELEASE) {
1642 keycode |= SCAN_RELEASE;
1644 if (sc->sc_mode == K_CODE) {
1647 /* compose a character code */
1648 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1650 /* key pressed, process it */
1653 case 0x49: /* keypad 7,8,9 */
1654 sc->sc_composed_char *= 10;
1655 sc->sc_composed_char += keycode - 0x40;
1656 goto check_composed;
1660 case 0x4D: /* keypad 4,5,6 */
1661 sc->sc_composed_char *= 10;
1662 sc->sc_composed_char += keycode - 0x47;
1663 goto check_composed;
1667 case 0x51: /* keypad 1,2,3 */
1668 sc->sc_composed_char *= 10;
1669 sc->sc_composed_char += keycode - 0x4E;
1670 goto check_composed;
1672 case 0x52: /* keypad 0 */
1673 sc->sc_composed_char *= 10;
1674 goto check_composed;
1676 /* key released, no interest here */
1677 case SCAN_RELEASE | 0x47:
1678 case SCAN_RELEASE | 0x48:
1679 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1680 case SCAN_RELEASE | 0x4B:
1681 case SCAN_RELEASE | 0x4C:
1682 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1683 case SCAN_RELEASE | 0x4F:
1684 case SCAN_RELEASE | 0x50:
1685 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1686 case SCAN_RELEASE | 0x52: /* keypad 0 */
1689 case 0x38: /* left alt key */
1693 if (sc->sc_composed_char > 0) {
1694 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1695 sc->sc_composed_char = 0;
1701 /* keycode to key action */
1702 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1703 (keycode & SCAN_RELEASE),
1704 &sc->sc_state, &sc->sc_accents);
1705 if (action == NOKEY) {
1712 if (sc->sc_composed_char <= 0xFF) {
1719 /* Currently wait is always false. */
1721 ukbd_read_char(keyboard_t *kbd, int wait)
1725 struct ukbd_softc *sc = kbd->kb_data;
1729 keycode = ukbd_read_char_locked(kbd, wait);
1737 /* some useful control functions */
1739 ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
1741 struct ukbd_softc *sc = kbd->kb_data;
1744 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1745 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1752 case KDGKBMODE: /* get keyboard mode */
1753 *(int *)arg = sc->sc_mode;
1756 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1757 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1759 ival = IOCPARM_IVAL(arg);
1760 arg = (caddr_t)&ival;
1764 case KDSKBMODE: /* set keyboard mode */
1765 switch (*(int *)arg) {
1767 if (sc->sc_mode != K_XLATE) {
1768 /* make lock key state and LED state match */
1769 sc->sc_state &= ~LOCK_MASK;
1770 sc->sc_state |= KBD_LED_VAL(kbd);
1775 if (sc->sc_mode != *(int *)arg) {
1776 if ((sc->sc_flags & UKBD_FLAG_POLLING) == 0)
1777 ukbd_clear_state(kbd);
1778 sc->sc_mode = *(int *)arg;
1786 case KDGETLED: /* get keyboard LED */
1787 *(int *)arg = KBD_LED_VAL(kbd);
1790 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1791 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1793 ival = IOCPARM_IVAL(arg);
1794 arg = (caddr_t)&ival;
1798 case KDSETLED: /* set keyboard LED */
1799 /* NOTE: lock key state in "sc_state" won't be changed */
1800 if (*(int *)arg & ~LOCK_MASK)
1805 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1806 if (sc->sc_mode == K_XLATE &&
1807 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1813 if (KBD_HAS_DEVICE(kbd))
1814 ukbd_set_leds(sc, i);
1816 KBD_LED_VAL(kbd) = *(int *)arg;
1818 case KDGKBSTATE: /* get lock key state */
1819 *(int *)arg = sc->sc_state & LOCK_MASK;
1822 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1823 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1825 ival = IOCPARM_IVAL(arg);
1826 arg = (caddr_t)&ival;
1830 case KDSKBSTATE: /* set lock key state */
1831 if (*(int *)arg & ~LOCK_MASK) {
1834 sc->sc_state &= ~LOCK_MASK;
1835 sc->sc_state |= *(int *)arg;
1837 /* set LEDs and quit */
1838 return (ukbd_ioctl(kbd, KDSETLED, arg));
1840 case KDSETREPEAT: /* set keyboard repeat rate (new
1842 if (!KBD_HAS_DEVICE(kbd)) {
1845 if (((int *)arg)[1] < 0) {
1848 if (((int *)arg)[0] < 0) {
1851 if (((int *)arg)[0] < 200) /* fastest possible value */
1852 kbd->kb_delay1 = 200;
1854 kbd->kb_delay1 = ((int *)arg)[0];
1855 kbd->kb_delay2 = ((int *)arg)[1];
1859 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1860 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1862 ival = IOCPARM_IVAL(arg);
1863 arg = (caddr_t)&ival;
1867 case KDSETRAD: /* set keyboard repeat rate (old
1869 return (ukbd_set_typematic(kbd, *(int *)arg));
1871 case PIO_KEYMAP: /* set keyboard translation table */
1872 case PIO_KEYMAPENT: /* set keyboard translation table
1874 case PIO_DEADKEYMAP: /* set accent key translation table */
1878 return (genkbd_commonioctl(kbd, cmd, arg));
1885 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1888 struct ukbd_softc *sc = kbd->kb_data;
1891 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
1892 * context where printf(9) can be called, which among other things
1893 * includes interrupt filters and threads with any kinds of locks
1894 * already held. For this reason it would be dangerous to acquire
1895 * the Giant here unconditionally. On the other hand we have to
1896 * have it to handle the ioctl.
1897 * So we make our best effort to auto-detect whether we can grab
1898 * the Giant or not. Blame syscons(4) for this.
1904 if(!lockowned(&kbd->kb_lock)) {
1905 return (EDEADLK); /* best I could come up with */
1910 result = ukbd_ioctl_locked(kbd, cmd, arg);
1917 /* clear the internal state of the keyboard */
1919 ukbd_clear_state(keyboard_t *kbd)
1921 struct ukbd_softc *sc = kbd->kb_data;
1923 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1924 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1926 sc->sc_composed_char = 0;
1927 #ifdef UKBD_EMULATE_ATSCANCODE
1928 sc->sc_buffered_char[0] = 0;
1929 sc->sc_buffered_char[1] = 0;
1931 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1932 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
1933 memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
1934 memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
1937 /* save the internal state, not used */
1939 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1941 return (len == 0) ? 1 : -1;
1944 /* set the internal state, not used */
1946 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1952 ukbd_poll(keyboard_t *kbd, int on)
1954 struct ukbd_softc *sc = kbd->kb_data;
1958 sc->sc_flags |= UKBD_FLAG_POLLING;
1959 sc->sc_poll_thread = curthread;
1961 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1962 ukbd_start_timer(sc); /* start timer */
1969 /* local functions */
1972 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1975 DPRINTF("leds=0x%02x\n", leds);
1978 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1980 /* start transfer, if not already started */
1982 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1986 ukbd_set_typematic(keyboard_t *kbd, int code)
1988 static const int delays[] = {250, 500, 750, 1000};
1989 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1990 68, 76, 84, 92, 100, 110, 118, 126,
1991 136, 152, 168, 184, 200, 220, 236, 252,
1992 272, 304, 336, 368, 400, 440, 472, 504};
1997 kbd->kb_delay1 = delays[(code >> 5) & 3];
1998 kbd->kb_delay2 = rates[code & 0x1f];
2002 #ifdef UKBD_EMULATE_ATSCANCODE
2004 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
2006 static const int scan[] = {
2012 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
2022 0x151, /* PageDown */
2025 0x146, /* XXX Pause/Break */
2026 0x15b, /* Win_L(Super_L) */
2027 0x15c, /* Win_R(Super_R) */
2028 0x15d, /* Application(Menu) */
2030 /* SUN TYPE 6 USB KEYBOARD */
2031 0x168, /* Sun Type 6 Help */
2032 0x15e, /* Sun Type 6 Stop */
2034 0x15f, /* Sun Type 6 Again */
2035 0x160, /* Sun Type 6 Props */
2036 0x161, /* Sun Type 6 Undo */
2037 0x162, /* Sun Type 6 Front */
2038 0x163, /* Sun Type 6 Copy */
2039 0x164, /* Sun Type 6 Open */
2040 0x165, /* Sun Type 6 Paste */
2041 0x166, /* Sun Type 6 Find */
2042 0x167, /* Sun Type 6 Cut */
2043 0x125, /* Sun Type 6 Mute */
2045 0x11f, /* Sun Type 6 VolumeDown */
2046 0x11e, /* Sun Type 6 VolumeUp */
2047 0x120, /* Sun Type 6 PowerDown */
2049 /* Japanese 106/109 keyboard */
2050 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
2051 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
2052 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
2053 0x79, /* Keyboard Intl' 4 (Henkan) */
2054 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
2055 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
2058 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
2059 code = scan[code - 89];
2062 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
2063 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
2065 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
2066 code &= ~SCAN_PREFIX_SHIFT;
2068 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
2070 if (code & SCAN_PREFIX) {
2071 if (code & SCAN_PREFIX_CTL) {
2073 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
2074 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
2075 } else if (code & SCAN_PREFIX_SHIFT) {
2077 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
2078 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
2080 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
2081 sc->sc_buffered_char[1] = 0;
2083 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
2089 #endif /* UKBD_EMULATE_ATSCANCODE */
2091 static keyboard_switch_t ukbdsw = {
2092 .probe = &ukbd__probe,
2096 .test_if = &ukbd_test_if,
2097 .enable = &ukbd_enable,
2098 .disable = &ukbd_disable,
2100 .check = &ukbd_check,
2101 .read_char = &ukbd_read_char,
2102 .check_char = &ukbd_check_char,
2103 .ioctl = &ukbd_ioctl,
2105 .clear_state = &ukbd_clear_state,
2106 .get_state = &ukbd_get_state,
2107 .set_state = &ukbd_set_state,
2108 .get_fkeystr = &genkbd_get_fkeystr,
2110 .diag = &genkbd_diag,
2113 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
2116 ukbd_driver_load(module_t mod, int what, void *arg)
2120 kbd_add_driver(&ukbd_kbd_driver);
2123 kbd_delete_driver(&ukbd_kbd_driver);
2129 static devclass_t ukbd_devclass;
2131 static device_method_t ukbd_methods[] = {
2132 DEVMETHOD(device_probe, ukbd_probe),
2133 DEVMETHOD(device_attach, ukbd_attach),
2134 DEVMETHOD(device_detach, ukbd_detach),
2135 DEVMETHOD(device_resume, ukbd_resume),
2139 static driver_t ukbd_driver = {
2141 .methods = ukbd_methods,
2142 .size = sizeof(struct ukbd_softc),
2145 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, NULL);
2146 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
2147 MODULE_VERSION(ukbd, 1);