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];
135 accentmap_t sc_accmap;
137 fkeytab_t sc_fkeymap[UKBD_NFKEY];
138 struct hid_location sc_loc_apple_eject;
139 struct hid_location sc_loc_apple_fn;
140 struct hid_location sc_loc_ctrl_l;
141 struct hid_location sc_loc_ctrl_r;
142 struct hid_location sc_loc_shift_l;
143 struct hid_location sc_loc_shift_r;
144 struct hid_location sc_loc_alt_l;
145 struct hid_location sc_loc_alt_r;
146 struct hid_location sc_loc_win_l;
147 struct hid_location sc_loc_win_r;
148 struct hid_location sc_loc_events;
149 struct hid_location sc_loc_numlock;
150 struct hid_location sc_loc_capslock;
151 struct hid_location sc_loc_scrolllock;
152 struct usb_callout sc_callout;
153 struct ukbd_data sc_ndata;
154 struct ukbd_data sc_odata;
156 struct thread *sc_poll_thread;
157 struct usb_device *sc_udev;
158 struct usb_interface *sc_iface;
159 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
161 uint32_t sc_ntime[UKBD_NKEYCODE];
162 uint32_t sc_otime[UKBD_NKEYCODE];
163 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
165 uint32_t sc_composed_char; /* composed char code, if non-zero */
166 #ifdef UKBD_EMULATE_ATSCANCODE
167 uint32_t sc_buffered_char[2];
169 uint32_t sc_flags; /* flags */
170 #define UKBD_FLAG_COMPOSE 0x00000001
171 #define UKBD_FLAG_POLLING 0x00000002
172 #define UKBD_FLAG_SET_LEDS 0x00000004
173 #define UKBD_FLAG_ATTACHED 0x00000010
174 #define UKBD_FLAG_GONE 0x00000020
176 #define UKBD_FLAG_HID_MASK 0x003fffc0
177 #define UKBD_FLAG_APPLE_EJECT 0x00000040
178 #define UKBD_FLAG_APPLE_FN 0x00000080
179 #define UKBD_FLAG_APPLE_SWAP 0x00000100
180 #define UKBD_FLAG_TIMER_RUNNING 0x00000200
181 #define UKBD_FLAG_CTRL_L 0x00000400
182 #define UKBD_FLAG_CTRL_R 0x00000800
183 #define UKBD_FLAG_SHIFT_L 0x00001000
184 #define UKBD_FLAG_SHIFT_R 0x00002000
185 #define UKBD_FLAG_ALT_L 0x00004000
186 #define UKBD_FLAG_ALT_R 0x00008000
187 #define UKBD_FLAG_WIN_L 0x00010000
188 #define UKBD_FLAG_WIN_R 0x00020000
189 #define UKBD_FLAG_EVENTS 0x00040000
190 #define UKBD_FLAG_NUMLOCK 0x00080000
191 #define UKBD_FLAG_CAPSLOCK 0x00100000
192 #define UKBD_FLAG_SCROLLLOCK 0x00200000
194 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
195 int sc_state; /* shift/lock key state */
196 int sc_accents; /* accent key index (> 0) */
201 uint16_t sc_inputhead;
202 uint16_t sc_inputtail;
203 uint16_t sc_modifiers;
205 uint8_t sc_leds; /* store for async led requests */
206 uint8_t sc_iface_index;
208 uint8_t sc_id_apple_eject;
209 uint8_t sc_id_apple_fn;
210 uint8_t sc_id_ctrl_l;
211 uint8_t sc_id_ctrl_r;
212 uint8_t sc_id_shift_l;
213 uint8_t sc_id_shift_r;
219 uint8_t sc_id_numlock;
220 uint8_t sc_id_capslock;
221 uint8_t sc_id_scrolllock;
222 uint8_t sc_id_events;
225 uint8_t sc_buffer[UKBD_BUFFER_SIZE];
228 #define KEY_ERROR 0x01
231 #define KEY_RELEASE 0x400
232 #define KEY_INDEX(c) ((c) & 0xFF)
235 #define SCAN_RELEASE 0x80
236 #define SCAN_PREFIX_E0 0x100
237 #define SCAN_PREFIX_E1 0x200
238 #define SCAN_PREFIX_CTL 0x400
239 #define SCAN_PREFIX_SHIFT 0x800
240 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
241 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
242 #define SCAN_CHAR(c) ((c) & 0x7f)
244 #define UKBD_LOCK(sc) lockmgr(&(sc)->sc_lock, LK_EXCLUSIVE)
245 #define UKBD_UNLOCK(sc) lockmgr(&(sc)->sc_lock, LK_RELEASE)
250 * Assert that the lock is held in all contexts
251 * where the code can be executed.
253 #define UKBD_LOCK_ASSERT()
256 * Assert that the lock is held in the contexts
257 * where it really has to be so.
259 #define UKBD_CTX_LOCK_ASSERT()
262 #define UKBD_LOCK_ASSERT() (void)0
263 #define UKBD_CTX_LOCK_ASSERT() (void)0
271 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
272 {MOD_CONTROL_L, 0xe0},
273 {MOD_CONTROL_R, 0xe4},
282 #define NN 0 /* no translation */
284 * Translate USB keycodes to AT keyboard scancodes.
287 * FIXME: Mac USB keyboard generates:
288 * 0x53: keypad NumLock/Clear
295 static const uint8_t ukbd_trtab[256] = {
296 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
297 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
298 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
299 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
300 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
301 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
302 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
303 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
304 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
305 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
306 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
307 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
308 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
309 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
310 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
311 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
312 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */
313 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */
314 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
315 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
316 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
317 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
318 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
319 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
320 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
321 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
322 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
323 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
324 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
325 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
326 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
327 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
330 static const uint8_t ukbd_boot_desc[] = {
331 0x05, 0x01, 0x09, 0x06, 0xa1,
332 0x01, 0x05, 0x07, 0x19, 0xe0,
333 0x29, 0xe7, 0x15, 0x00, 0x25,
334 0x01, 0x75, 0x01, 0x95, 0x08,
335 0x81, 0x02, 0x95, 0x01, 0x75,
336 0x08, 0x81, 0x01, 0x95, 0x03,
337 0x75, 0x01, 0x05, 0x08, 0x19,
338 0x01, 0x29, 0x03, 0x91, 0x02,
339 0x95, 0x05, 0x75, 0x01, 0x91,
340 0x01, 0x95, 0x06, 0x75, 0x08,
341 0x15, 0x00, 0x26, 0xff, 0x00,
342 0x05, 0x07, 0x19, 0x00, 0x2a,
343 0xff, 0x00, 0x81, 0x00, 0xc0
347 static void ukbd_timeout(void *);
348 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
349 static int ukbd_set_typematic(keyboard_t *, int);
350 #ifdef UKBD_EMULATE_ATSCANCODE
351 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
353 static uint32_t ukbd_read_char(keyboard_t *, int);
354 static void ukbd_clear_state(keyboard_t *);
355 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
356 static int ukbd_enable(keyboard_t *);
357 static int ukbd_disable(keyboard_t *);
358 static void ukbd_interrupt(struct ukbd_softc *);
359 static void ukbd_event_keyinput(struct ukbd_softc *);
361 static device_probe_t ukbd_probe;
362 static device_attach_t ukbd_attach;
363 static device_detach_t ukbd_detach;
364 static device_resume_t ukbd_resume;
367 ukbd_any_key_pressed(struct ukbd_softc *sc)
372 for (j = i = 0; i < UKBD_NKEYCODE; i++)
373 j |= sc->sc_odata.keycode[i];
379 ukbd_start_timer(struct ukbd_softc *sc)
381 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
382 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
386 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
389 UKBD_CTX_LOCK_ASSERT();
391 DPRINTF("0x%02x (%d) %s\n", key, key,
392 (key & KEY_RELEASE) ? "released" : "pressed");
394 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
395 sc->sc_input[sc->sc_inputtail] = key;
397 ++(sc->sc_inputtail);
398 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
399 sc->sc_inputtail = 0;
402 DPRINTF("input buffer is full\n");
407 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
410 UKBD_CTX_LOCK_ASSERT();
411 KASSERT((sc->sc_flags & UKBD_FLAG_POLLING) != 0,
412 ("ukbd_do_poll called when not polling\n"));
413 DPRINTFN(2, "polling\n");
415 if (!kdb_active && !SCHEDULER_STOPPED()) {
418 * In this context the kernel is polling for input,
419 * but the USB subsystem works in normal interrupt-driven
420 * mode, so we just wait on the USB threads to do the job.
421 * Note that we currently hold the Giant, but it's also used
422 * as the transfer mtx, so we must release it while waiting.
424 while (sc->sc_inputs == 0) {
426 * Give USB threads a chance to run. Note that
427 * kern_yield performs DROP_GIANT + PICKUP_GIANT.
438 while (sc->sc_inputs == 0) {
440 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
442 /* Delay-optimised support for repetition of keys */
443 if (ukbd_any_key_pressed(sc)) {
444 /* a key is pressed - need timekeeping */
447 /* 1 millisecond has passed */
459 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
463 UKBD_CTX_LOCK_ASSERT();
465 KASSERT((!kdb_active && !SCHEDULER_STOPPED())
466 || (sc->sc_flags & UKBD_FLAG_POLLING) != 0,
467 ("not polling in kdb or panic\n"));
470 if (sc->sc_inputs == 0) {
471 /* start transfer, if not already started */
472 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
475 if (sc->sc_flags & UKBD_FLAG_POLLING)
476 ukbd_do_poll(sc, wait);
478 if (sc->sc_inputs == 0) {
481 c = sc->sc_input[sc->sc_inputhead];
483 ++(sc->sc_inputhead);
484 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
485 sc->sc_inputhead = 0;
492 ukbd_interrupt(struct ukbd_softc *sc)
496 uint32_t now = sc->sc_time_ms;
502 UKBD_CTX_LOCK_ASSERT();
504 if (sc->sc_ndata.keycode[0] == KEY_ERROR)
507 n_mod = sc->sc_ndata.modifiers;
508 o_mod = sc->sc_odata.modifiers;
509 if (n_mod != o_mod) {
510 for (i = 0; i < UKBD_NMOD; i++) {
511 if ((n_mod & ukbd_mods[i].mask) !=
512 (o_mod & ukbd_mods[i].mask)) {
513 ukbd_put_key(sc, ukbd_mods[i].key |
514 ((n_mod & ukbd_mods[i].mask) ?
515 KEY_PRESS : KEY_RELEASE));
519 /* Check for released keys. */
520 for (i = 0; i < UKBD_NKEYCODE; i++) {
521 key = sc->sc_odata.keycode[i];
525 for (j = 0; j < UKBD_NKEYCODE; j++) {
526 if (sc->sc_ndata.keycode[j] == 0) {
529 if (key == sc->sc_ndata.keycode[j]) {
533 ukbd_put_key(sc, key | KEY_RELEASE);
537 /* Check for pressed keys. */
538 for (i = 0; i < UKBD_NKEYCODE; i++) {
539 key = sc->sc_ndata.keycode[i];
543 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
544 for (j = 0; j < UKBD_NKEYCODE; j++) {
545 if (sc->sc_odata.keycode[j] == 0) {
548 if (key == sc->sc_odata.keycode[j]) {
550 /* key is still pressed */
552 sc->sc_ntime[i] = sc->sc_otime[j];
553 dtime = (sc->sc_otime[j] - now);
555 if (!(dtime & 0x80000000)) {
556 /* time has not elapsed */
559 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
563 ukbd_put_key(sc, key | KEY_PRESS);
566 * If any other key is presently down, force its repeat to be
567 * well in the future (100s). This makes the last key to be
568 * pressed do the autorepeat.
570 for (j = 0; j != UKBD_NKEYCODE; j++) {
572 sc->sc_ntime[j] = now + (100 * 1000);
577 sc->sc_odata = sc->sc_ndata;
579 memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
581 ukbd_event_keyinput(sc);
585 ukbd_event_keyinput(struct ukbd_softc *sc)
589 UKBD_CTX_LOCK_ASSERT();
591 if ((sc->sc_flags & UKBD_FLAG_POLLING) != 0)
594 if (sc->sc_inputs == 0)
597 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
598 KBD_IS_BUSY(&sc->sc_kbd)) {
599 /* let the callback function process the input */
600 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
601 sc->sc_kbd.kb_callback.kc_arg);
603 /* read and discard the input, no one is waiting for it */
605 c = ukbd_read_char(&sc->sc_kbd, 0);
606 } while (c != NOKEY);
611 ukbd_timeout(void *arg)
613 struct ukbd_softc *sc = arg;
617 sc->sc_time_ms += 25; /* milliseconds */
621 /* Make sure any leftover key events gets read out */
622 ukbd_event_keyinput(sc);
624 if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
625 ukbd_start_timer(sc);
627 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
632 ukbd_apple_fn(uint8_t keycode) {
634 case 0x28: return 0x49; /* RETURN -> INSERT */
635 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
636 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
637 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
638 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
639 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
640 default: return keycode;
645 ukbd_apple_swap(uint8_t keycode) {
647 case 0x35: return 0x64;
648 case 0x64: return 0x35;
649 default: return keycode;
654 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
656 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
657 struct usb_page_cache *pc;
665 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
666 pc = usbd_xfer_get_frame(xfer, 0);
668 switch (USB_GET_STATE(xfer)) {
669 case USB_ST_TRANSFERRED:
670 DPRINTF("actlen=%d bytes\n", len);
673 DPRINTF("zero length data\n");
677 if (sc->sc_kbd_id != 0) {
678 /* check and remove HID ID byte */
679 usbd_copy_out(pc, 0, &id, 1);
683 DPRINTF("zero length data\n");
691 if (len > UKBD_BUFFER_SIZE)
692 len = UKBD_BUFFER_SIZE;
695 usbd_copy_out(pc, offset, sc->sc_buffer, len);
697 /* clear temporary storage */
698 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
700 /* scan through HID data */
701 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
702 (id == sc->sc_id_apple_eject)) {
703 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject))
704 sc->sc_modifiers |= MOD_EJECT;
706 sc->sc_modifiers &= ~MOD_EJECT;
708 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
709 (id == sc->sc_id_apple_fn)) {
710 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn))
711 sc->sc_modifiers |= MOD_FN;
713 sc->sc_modifiers &= ~MOD_FN;
715 if ((sc->sc_flags & UKBD_FLAG_CTRL_L) &&
716 (id == sc->sc_id_ctrl_l)) {
717 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_l))
718 sc-> sc_modifiers |= MOD_CONTROL_L;
720 sc-> sc_modifiers &= ~MOD_CONTROL_L;
722 if ((sc->sc_flags & UKBD_FLAG_CTRL_R) &&
723 (id == sc->sc_id_ctrl_r)) {
724 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_r))
725 sc->sc_modifiers |= MOD_CONTROL_R;
727 sc->sc_modifiers &= ~MOD_CONTROL_R;
729 if ((sc->sc_flags & UKBD_FLAG_SHIFT_L) &&
730 (id == sc->sc_id_shift_l)) {
731 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_l))
732 sc->sc_modifiers |= MOD_SHIFT_L;
734 sc->sc_modifiers &= ~MOD_SHIFT_L;
736 if ((sc->sc_flags & UKBD_FLAG_SHIFT_R) &&
737 (id == sc->sc_id_shift_r)) {
738 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_r))
739 sc->sc_modifiers |= MOD_SHIFT_R;
741 sc->sc_modifiers &= ~MOD_SHIFT_R;
743 if ((sc->sc_flags & UKBD_FLAG_ALT_L) &&
744 (id == sc->sc_id_alt_l)) {
745 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_l))
746 sc->sc_modifiers |= MOD_ALT_L;
748 sc->sc_modifiers &= ~MOD_ALT_L;
750 if ((sc->sc_flags & UKBD_FLAG_ALT_R) &&
751 (id == sc->sc_id_alt_r)) {
752 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_r))
753 sc->sc_modifiers |= MOD_ALT_R;
755 sc->sc_modifiers &= ~MOD_ALT_R;
757 if ((sc->sc_flags & UKBD_FLAG_WIN_L) &&
758 (id == sc->sc_id_win_l)) {
759 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_l))
760 sc->sc_modifiers |= MOD_WIN_L;
762 sc->sc_modifiers &= ~MOD_WIN_L;
764 if ((sc->sc_flags & UKBD_FLAG_WIN_R) &&
765 (id == sc->sc_id_win_r)) {
766 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_r))
767 sc->sc_modifiers |= MOD_WIN_R;
769 sc->sc_modifiers &= ~MOD_WIN_R;
772 sc->sc_ndata.modifiers = sc->sc_modifiers;
774 if ((sc->sc_flags & UKBD_FLAG_EVENTS) &&
775 (id == sc->sc_id_events)) {
776 i = sc->sc_loc_events.count;
777 if (i > UKBD_NKEYCODE)
782 sc->sc_ndata.keycode[i] =
783 hid_get_data(sc->sc_buffer + i, len - i,
789 DPRINTF("modifiers = 0x%04x\n", (int)sc->sc_modifiers);
790 for (i = 0; i < UKBD_NKEYCODE; i++) {
791 if (sc->sc_ndata.keycode[i]) {
792 DPRINTF("[%d] = 0x%02x\n",
793 (int)i, (int)sc->sc_ndata.keycode[i]);
797 if (sc->sc_modifiers & MOD_FN) {
798 for (i = 0; i < UKBD_NKEYCODE; i++) {
799 sc->sc_ndata.keycode[i] =
800 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
804 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
805 for (i = 0; i < UKBD_NKEYCODE; i++) {
806 sc->sc_ndata.keycode[i] =
807 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
813 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
814 if (ukbd_any_key_pressed(sc)) {
815 ukbd_start_timer(sc);
821 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
822 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
823 usbd_transfer_submit(xfer);
825 DPRINTF("input queue is full!\n");
830 DPRINTF("error=%s\n", usbd_errstr(error));
832 if (error != USB_ERR_CANCELLED) {
833 /* try to clear stall first */
834 usbd_xfer_set_stall(xfer);
842 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
844 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
845 struct usb_device_request req;
846 struct usb_page_cache *pc;
858 switch (USB_GET_STATE(xfer)) {
859 case USB_ST_TRANSFERRED:
861 if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS))
863 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
865 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
866 req.bRequest = UR_SET_REPORT;
867 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
868 req.wIndex[0] = sc->sc_iface_no;
872 memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE);
877 /* Assumption: All led bits must be in the same ID. */
879 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) {
880 if (sc->sc_leds & NLKED) {
881 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
882 &sc->sc_loc_numlock, 1);
884 id = sc->sc_id_numlock;
888 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) {
889 if (sc->sc_leds & SLKED) {
890 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
891 &sc->sc_loc_scrolllock, 1);
893 id = sc->sc_id_scrolllock;
897 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) {
898 if (sc->sc_leds & CLKED) {
899 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
900 &sc->sc_loc_capslock, 1);
902 id = sc->sc_id_capslock;
906 /* if no leds, nothing to do */
910 /* range check output report length */
911 len = sc->sc_led_size;
912 if (len > (UKBD_BUFFER_SIZE - 1))
913 len = (UKBD_BUFFER_SIZE - 1);
915 /* check if we need to prefix an ID byte */
916 sc->sc_buffer[0] = id;
918 pc = usbd_xfer_get_frame(xfer, 1);
921 usbd_copy_in(pc, 0, sc->sc_buffer, len);
923 usbd_copy_in(pc, 0, sc->sc_buffer + 1, len);
925 req.wLength[0] = len;
926 usbd_xfer_set_frame_len(xfer, 1, len);
928 DPRINTF("len=%d, id=%d\n", len, id);
930 /* setup control request last */
931 pc = usbd_xfer_get_frame(xfer, 0);
932 usbd_copy_in(pc, 0, &req, sizeof(req));
933 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
935 /* start data transfer */
936 usbd_xfer_set_frames(xfer, 2);
937 usbd_transfer_submit(xfer);
941 DPRINTFN(1, "error=%s\n", usbd_errstr(error));
946 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
949 .type = UE_INTERRUPT,
950 .endpoint = UE_ADDR_ANY,
951 .direction = UE_DIR_IN,
952 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
953 .bufsize = 0, /* use wMaxPacketSize */
954 .callback = &ukbd_intr_callback,
959 .endpoint = 0x00, /* Control pipe */
960 .direction = UE_DIR_ANY,
961 .bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE,
962 .callback = &ukbd_set_leds_callback,
963 .timeout = 1000, /* 1 second */
967 /* A match on these entries will load ukbd */
968 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = {
969 {USB_IFACE_CLASS(UICLASS_HID),
970 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
971 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),},
975 ukbd_probe(device_t dev)
977 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
978 struct usb_attach_arg *uaa = device_get_ivars(dev);
988 if (uaa->usb_mode != USB_MODE_HOST) {
992 if (uaa->info.bInterfaceClass != UICLASS_HID)
995 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
996 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) {
997 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1000 return (BUS_PROBE_DEFAULT);
1003 error = usbd_req_get_hid_desc(uaa->device, NULL,
1004 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
1010 * NOTE: we currently don't support USB mouse and USB keyboard
1011 * on the same USB endpoint.
1013 if (hid_is_collection(d_ptr, d_len,
1014 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
1015 /* most likely a mouse */
1017 } else if (hid_is_collection(d_ptr, d_len,
1018 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
1019 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1022 error = BUS_PROBE_DEFAULT;
1026 kfree(d_ptr, M_TEMP);
1031 ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len)
1035 /* reset detected bits */
1036 sc->sc_flags &= ~UKBD_FLAG_HID_MASK;
1038 /* check if there is an ID byte */
1039 sc->sc_kbd_size = hid_report_size(ptr, len,
1040 hid_input, &sc->sc_kbd_id);
1042 /* investigate if this is an Apple Keyboard */
1043 if (hid_locate(ptr, len,
1044 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
1045 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
1046 &sc->sc_id_apple_eject)) {
1047 if (flags & HIO_VARIABLE)
1048 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
1049 UKBD_FLAG_APPLE_SWAP;
1050 DPRINTFN(1, "Found Apple eject-key\n");
1052 if (hid_locate(ptr, len,
1053 HID_USAGE2(0xFFFF, 0x0003),
1054 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
1055 &sc->sc_id_apple_fn)) {
1056 if (flags & HIO_VARIABLE)
1057 sc->sc_flags |= UKBD_FLAG_APPLE_FN;
1058 DPRINTFN(1, "Found Apple FN-key\n");
1060 /* figure out some keys */
1061 if (hid_locate(ptr, len,
1062 HID_USAGE2(HUP_KEYBOARD, 0xE0),
1063 hid_input, 0, &sc->sc_loc_ctrl_l, &flags,
1064 &sc->sc_id_ctrl_l)) {
1065 if (flags & HIO_VARIABLE)
1066 sc->sc_flags |= UKBD_FLAG_CTRL_L;
1067 DPRINTFN(1, "Found left control\n");
1069 if (hid_locate(ptr, len,
1070 HID_USAGE2(HUP_KEYBOARD, 0xE4),
1071 hid_input, 0, &sc->sc_loc_ctrl_r, &flags,
1072 &sc->sc_id_ctrl_r)) {
1073 if (flags & HIO_VARIABLE)
1074 sc->sc_flags |= UKBD_FLAG_CTRL_R;
1075 DPRINTFN(1, "Found right control\n");
1077 if (hid_locate(ptr, len,
1078 HID_USAGE2(HUP_KEYBOARD, 0xE1),
1079 hid_input, 0, &sc->sc_loc_shift_l, &flags,
1080 &sc->sc_id_shift_l)) {
1081 if (flags & HIO_VARIABLE)
1082 sc->sc_flags |= UKBD_FLAG_SHIFT_L;
1083 DPRINTFN(1, "Found left shift\n");
1085 if (hid_locate(ptr, len,
1086 HID_USAGE2(HUP_KEYBOARD, 0xE5),
1087 hid_input, 0, &sc->sc_loc_shift_r, &flags,
1088 &sc->sc_id_shift_r)) {
1089 if (flags & HIO_VARIABLE)
1090 sc->sc_flags |= UKBD_FLAG_SHIFT_R;
1091 DPRINTFN(1, "Found right shift\n");
1093 if (hid_locate(ptr, len,
1094 HID_USAGE2(HUP_KEYBOARD, 0xE2),
1095 hid_input, 0, &sc->sc_loc_alt_l, &flags,
1096 &sc->sc_id_alt_l)) {
1097 if (flags & HIO_VARIABLE)
1098 sc->sc_flags |= UKBD_FLAG_ALT_L;
1099 DPRINTFN(1, "Found left alt\n");
1101 if (hid_locate(ptr, len,
1102 HID_USAGE2(HUP_KEYBOARD, 0xE6),
1103 hid_input, 0, &sc->sc_loc_alt_r, &flags,
1104 &sc->sc_id_alt_r)) {
1105 if (flags & HIO_VARIABLE)
1106 sc->sc_flags |= UKBD_FLAG_ALT_R;
1107 DPRINTFN(1, "Found right alt\n");
1109 if (hid_locate(ptr, len,
1110 HID_USAGE2(HUP_KEYBOARD, 0xE3),
1111 hid_input, 0, &sc->sc_loc_win_l, &flags,
1112 &sc->sc_id_win_l)) {
1113 if (flags & HIO_VARIABLE)
1114 sc->sc_flags |= UKBD_FLAG_WIN_L;
1115 DPRINTFN(1, "Found left GUI\n");
1117 if (hid_locate(ptr, len,
1118 HID_USAGE2(HUP_KEYBOARD, 0xE7),
1119 hid_input, 0, &sc->sc_loc_win_r, &flags,
1120 &sc->sc_id_win_r)) {
1121 if (flags & HIO_VARIABLE)
1122 sc->sc_flags |= UKBD_FLAG_WIN_R;
1123 DPRINTFN(1, "Found right GUI\n");
1125 /* figure out event buffer */
1126 if (hid_locate(ptr, len,
1127 HID_USAGE2(HUP_KEYBOARD, 0x00),
1128 hid_input, 0, &sc->sc_loc_events, &flags,
1129 &sc->sc_id_events)) {
1130 sc->sc_flags |= UKBD_FLAG_EVENTS;
1131 DPRINTFN(1, "Found keyboard events\n");
1134 /* figure out leds on keyboard */
1135 sc->sc_led_size = hid_report_size(ptr, len,
1138 if (hid_locate(ptr, len,
1139 HID_USAGE2(HUP_LEDS, 0x01),
1140 hid_output, 0, &sc->sc_loc_numlock, &flags,
1141 &sc->sc_id_numlock)) {
1142 if (flags & HIO_VARIABLE)
1143 sc->sc_flags |= UKBD_FLAG_NUMLOCK;
1144 DPRINTFN(1, "Found keyboard numlock\n");
1146 if (hid_locate(ptr, len,
1147 HID_USAGE2(HUP_LEDS, 0x02),
1148 hid_output, 0, &sc->sc_loc_capslock, &flags,
1149 &sc->sc_id_capslock)) {
1150 if (flags & HIO_VARIABLE)
1151 sc->sc_flags |= UKBD_FLAG_CAPSLOCK;
1152 DPRINTFN(1, "Found keyboard capslock\n");
1154 if (hid_locate(ptr, len,
1155 HID_USAGE2(HUP_LEDS, 0x03),
1156 hid_output, 0, &sc->sc_loc_scrolllock, &flags,
1157 &sc->sc_id_scrolllock)) {
1158 if (flags & HIO_VARIABLE)
1159 sc->sc_flags |= UKBD_FLAG_SCROLLLOCK;
1160 DPRINTFN(1, "Found keyboard scrolllock\n");
1165 ukbd_attach(device_t dev)
1167 struct ukbd_softc *sc = device_get_softc(dev);
1168 struct usb_attach_arg *uaa = device_get_ivars(dev);
1169 int32_t unit = device_get_unit(dev);
1170 keyboard_t *kbd = &sc->sc_kbd;
1171 void *hid_ptr = NULL;
1176 lockinit(&sc->sc_lock, "ukbd", 0, 0);
1177 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER,
1178 unit, 0, KB_PRI_USB, 0, 0);
1180 kbd->kb_data = (void *)sc;
1182 device_set_usb_desc(dev);
1184 sc->sc_udev = uaa->device;
1185 sc->sc_iface = uaa->iface;
1186 sc->sc_iface_index = uaa->info.bIfaceIndex;
1187 sc->sc_iface_no = uaa->info.bIfaceNum;
1188 sc->sc_mode = K_XLATE;
1190 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_lock, 0);
1192 err = usbd_transfer_setup(uaa->device,
1193 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
1194 UKBD_N_TRANSFER, sc, &sc->sc_lock);
1197 DPRINTF("error=%s\n", usbd_errstr(err));
1200 /* setup default keyboard maps */
1202 sc->sc_keymap = key_map;
1203 sc->sc_accmap = accent_map;
1204 for (n = 0; n < UKBD_NFKEY; n++) {
1205 sc->sc_fkeymap[n] = fkey_tab[n];
1208 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
1209 sc->sc_fkeymap, UKBD_NFKEY);
1211 KBD_FOUND_DEVICE(kbd);
1213 ukbd_clear_state(kbd);
1216 * FIXME: set the initial value for lock keys in "sc_state"
1217 * according to the BIOS data?
1219 KBD_PROBE_DONE(kbd);
1221 /* get HID descriptor */
1222 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
1223 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
1226 DPRINTF("Parsing HID descriptor of %d bytes\n",
1229 ukbd_parse_hid(sc, hid_ptr, hid_len);
1231 kfree(hid_ptr, M_TEMP);
1234 /* check if we should use the boot protocol */
1235 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) ||
1236 (err != 0) || (!(sc->sc_flags & UKBD_FLAG_EVENTS))) {
1238 DPRINTF("Forcing boot protocol\n");
1240 err = usbd_req_set_protocol(sc->sc_udev, NULL,
1241 sc->sc_iface_index, 0);
1244 DPRINTF("Set protocol error=%s (ignored)\n",
1248 ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc));
1251 /* ignore if SETIDLE fails, hence it is not crucial */
1252 usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
1254 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
1258 if (kbd_register(kbd) < 0) {
1261 KBD_CONFIG_DONE(kbd);
1265 #ifdef KBD_INSTALL_CDEV
1266 if (kbd_attach(kbd)) {
1270 sc->sc_flags |= UKBD_FLAG_ATTACHED;
1273 genkbd_diag(kbd, bootverbose);
1276 /* start the keyboard */
1278 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
1281 return (0); /* success */
1285 return (ENXIO); /* error */
1289 ukbd_detach(device_t dev)
1291 struct ukbd_softc *sc = device_get_softc(dev);
1296 sc->sc_flags |= UKBD_FLAG_GONE;
1298 usb_callout_stop(&sc->sc_callout);
1300 ukbd_disable(&sc->sc_kbd);
1302 #ifdef KBD_INSTALL_CDEV
1303 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1304 error = kbd_detach(&sc->sc_kbd);
1306 /* usb attach cannot return an error */
1307 device_printf(dev, "WARNING: kbd_detach() "
1308 "returned non-zero! (ignored)\n");
1312 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
1313 error = kbd_unregister(&sc->sc_kbd);
1315 /* usb attach cannot return an error */
1316 device_printf(dev, "WARNING: kbd_unregister() "
1317 "returned non-zero! (ignored)\n");
1320 sc->sc_kbd.kb_flags = 0;
1322 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1324 usb_callout_drain(&sc->sc_callout);
1326 DPRINTF("%s: disconnected\n",
1327 device_get_nameunit(dev));
1333 ukbd_resume(device_t dev)
1335 struct ukbd_softc *sc = device_get_softc(dev);
1337 ukbd_clear_state(&sc->sc_kbd);
1342 /* early keyboard probe, not supported */
1344 ukbd_configure(int flags)
1349 /* detect a keyboard, not used */
1351 ukbd__probe(int unit, void *arg, int flags)
1356 /* reset and initialize the device, not used */
1358 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1363 /* test the interface to the device, not used */
1365 ukbd_test_if(keyboard_t *kbd)
1370 /* finish using this keyboard, not used */
1372 ukbd_term(keyboard_t *kbd)
1377 /* keyboard interrupt routine, not used */
1379 ukbd_intr(keyboard_t *kbd, void *arg)
1384 /* lock the access to the keyboard, not used */
1386 ukbd_lock(keyboard_t *kbd, int lock)
1392 * Enable the access to the device; until this function is called,
1393 * the client cannot read from the keyboard.
1396 ukbd_enable(keyboard_t *kbd)
1398 struct ukbd_softc *sc = kbd->kb_data;
1407 /* disallow the access to the device */
1409 ukbd_disable(keyboard_t *kbd)
1411 struct ukbd_softc *sc = kbd->kb_data;
1414 KBD_DEACTIVATE(kbd);
1420 /* check if data is waiting */
1421 /* Currently unused. */
1423 ukbd_check(keyboard_t *kbd)
1425 struct ukbd_softc *sc = kbd->kb_data;
1427 if (!KBD_IS_ACTIVE(kbd))
1430 if (sc->sc_flags & UKBD_FLAG_POLLING)
1431 ukbd_do_poll(sc, 0);
1433 #ifdef UKBD_EMULATE_ATSCANCODE
1434 if (sc->sc_buffered_char[0]) {
1438 if (sc->sc_inputs > 0) {
1444 /* check if char is waiting */
1446 ukbd_check_char_locked(keyboard_t *kbd)
1448 struct ukbd_softc *sc = kbd->kb_data;
1450 if (!KBD_IS_ACTIVE(kbd))
1453 if ((sc->sc_composed_char > 0) &&
1454 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1457 return (ukbd_check(kbd));
1461 ukbd_check_char(keyboard_t *kbd)
1465 struct ukbd_softc *sc = kbd->kb_data;
1469 result = ukbd_check_char_locked(kbd);
1477 /* read one byte from the keyboard if it's allowed */
1478 /* Currently unused. */
1480 ukbd_read(keyboard_t *kbd, int wait)
1482 struct ukbd_softc *sc = kbd->kb_data;
1484 #ifdef UKBD_EMULATE_ATSCANCODE
1490 if (!KBD_IS_ACTIVE(kbd))
1493 #ifdef UKBD_EMULATE_ATSCANCODE
1494 if (sc->sc_buffered_char[0]) {
1495 scancode = sc->sc_buffered_char[0];
1496 if (scancode & SCAN_PREFIX) {
1497 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1498 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1500 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1501 sc->sc_buffered_char[1] = 0;
1504 #endif /* UKBD_EMULATE_ATSCANCODE */
1507 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1508 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1513 #ifdef UKBD_EMULATE_ATSCANCODE
1514 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1515 if (keycode == NN) {
1518 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1519 (usbcode & KEY_RELEASE)));
1520 #else /* !UKBD_EMULATE_ATSCANCODE */
1522 #endif /* UKBD_EMULATE_ATSCANCODE */
1525 /* read char from the keyboard */
1527 ukbd_read_char_locked(keyboard_t *kbd, int wait)
1529 struct ukbd_softc *sc = kbd->kb_data;
1533 #ifdef UKBD_EMULATE_ATSCANCODE
1537 if (!KBD_IS_ACTIVE(kbd))
1542 /* do we have a composed char to return ? */
1544 if ((sc->sc_composed_char > 0) &&
1545 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1547 action = sc->sc_composed_char;
1548 sc->sc_composed_char = 0;
1550 if (action > 0xFF) {
1555 #ifdef UKBD_EMULATE_ATSCANCODE
1557 /* do we have a pending raw scan code? */
1559 if (sc->sc_mode == K_RAW) {
1560 scancode = sc->sc_buffered_char[0];
1562 if (scancode & SCAN_PREFIX) {
1563 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1564 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1566 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1567 sc->sc_buffered_char[1] = 0;
1571 #endif /* UKBD_EMULATE_ATSCANCODE */
1573 /* see if there is something in the keyboard port */
1575 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1576 if (usbcode == -1) {
1581 #ifdef UKBD_EMULATE_ATSCANCODE
1582 /* USB key index -> key code -> AT scan code */
1583 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1584 if (keycode == NN) {
1587 /* return an AT scan code for the K_RAW mode */
1588 if (sc->sc_mode == K_RAW) {
1589 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1590 (usbcode & KEY_RELEASE)));
1592 #else /* !UKBD_EMULATE_ATSCANCODE */
1594 /* return the byte as is for the K_RAW mode */
1595 if (sc->sc_mode == K_RAW) {
1598 /* USB key index -> key code */
1599 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1600 if (keycode == NN) {
1603 #endif /* UKBD_EMULATE_ATSCANCODE */
1606 case 0x38: /* left alt (compose key) */
1607 if (usbcode & KEY_RELEASE) {
1608 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1609 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1611 if (sc->sc_composed_char > 0xFF) {
1612 sc->sc_composed_char = 0;
1616 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1617 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1618 sc->sc_composed_char = 0;
1622 /* XXX: I don't like these... */
1623 case 0x5c: /* print screen */
1624 if (sc->sc_flags & ALTS) {
1625 keycode = 0x54; /* sysrq */
1628 case 0x68: /* pause/break */
1629 if (sc->sc_flags & CTLS) {
1630 keycode = 0x6c; /* break */
1635 /* return the key code in the K_CODE mode */
1636 if (usbcode & KEY_RELEASE) {
1637 keycode |= SCAN_RELEASE;
1639 if (sc->sc_mode == K_CODE) {
1642 /* compose a character code */
1643 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1645 /* key pressed, process it */
1648 case 0x49: /* keypad 7,8,9 */
1649 sc->sc_composed_char *= 10;
1650 sc->sc_composed_char += keycode - 0x40;
1651 goto check_composed;
1655 case 0x4D: /* keypad 4,5,6 */
1656 sc->sc_composed_char *= 10;
1657 sc->sc_composed_char += keycode - 0x47;
1658 goto check_composed;
1662 case 0x51: /* keypad 1,2,3 */
1663 sc->sc_composed_char *= 10;
1664 sc->sc_composed_char += keycode - 0x4E;
1665 goto check_composed;
1667 case 0x52: /* keypad 0 */
1668 sc->sc_composed_char *= 10;
1669 goto check_composed;
1671 /* key released, no interest here */
1672 case SCAN_RELEASE | 0x47:
1673 case SCAN_RELEASE | 0x48:
1674 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1675 case SCAN_RELEASE | 0x4B:
1676 case SCAN_RELEASE | 0x4C:
1677 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1678 case SCAN_RELEASE | 0x4F:
1679 case SCAN_RELEASE | 0x50:
1680 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1681 case SCAN_RELEASE | 0x52: /* keypad 0 */
1684 case 0x38: /* left alt key */
1688 if (sc->sc_composed_char > 0) {
1689 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1690 sc->sc_composed_char = 0;
1696 /* keycode to key action */
1697 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1698 (keycode & SCAN_RELEASE),
1699 &sc->sc_state, &sc->sc_accents);
1700 if (action == NOKEY) {
1707 if (sc->sc_composed_char <= 0xFF) {
1714 /* Currently wait is always false. */
1716 ukbd_read_char(keyboard_t *kbd, int wait)
1720 struct ukbd_softc *sc = kbd->kb_data;
1724 keycode = ukbd_read_char_locked(kbd, wait);
1732 /* some useful control functions */
1734 ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
1736 struct ukbd_softc *sc = kbd->kb_data;
1739 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1740 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1747 case KDGKBMODE: /* get keyboard mode */
1748 *(int *)arg = sc->sc_mode;
1751 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1752 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1754 ival = IOCPARM_IVAL(arg);
1755 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);
1785 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1786 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1788 ival = IOCPARM_IVAL(arg);
1789 arg = (caddr_t)&ival;
1793 case KDSETLED: /* set keyboard LED */
1794 /* NOTE: lock key state in "sc_state" won't be changed */
1795 if (*(int *)arg & ~LOCK_MASK)
1800 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1801 if (sc->sc_mode == K_XLATE &&
1802 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1808 if (KBD_HAS_DEVICE(kbd))
1809 ukbd_set_leds(sc, i);
1811 KBD_LED_VAL(kbd) = *(int *)arg;
1813 case KDGKBSTATE: /* get lock key state */
1814 *(int *)arg = sc->sc_state & LOCK_MASK;
1817 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1818 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1820 ival = IOCPARM_IVAL(arg);
1821 arg = (caddr_t)&ival;
1825 case KDSKBSTATE: /* set lock key state */
1826 if (*(int *)arg & ~LOCK_MASK) {
1829 sc->sc_state &= ~LOCK_MASK;
1830 sc->sc_state |= *(int *)arg;
1832 /* set LEDs and quit */
1833 return (ukbd_ioctl(kbd, KDSETLED, arg));
1835 case KDSETREPEAT: /* set keyboard repeat rate (new
1837 if (!KBD_HAS_DEVICE(kbd)) {
1840 if (((int *)arg)[1] < 0) {
1843 if (((int *)arg)[0] < 0) {
1846 if (((int *)arg)[0] < 200) /* fastest possible value */
1847 kbd->kb_delay1 = 200;
1849 kbd->kb_delay1 = ((int *)arg)[0];
1850 kbd->kb_delay2 = ((int *)arg)[1];
1854 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1855 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1857 ival = IOCPARM_IVAL(arg);
1858 arg = (caddr_t)&ival;
1862 case KDSETRAD: /* set keyboard repeat rate (old
1864 return (ukbd_set_typematic(kbd, *(int *)arg));
1866 case PIO_KEYMAP: /* set keyboard translation table */
1867 case PIO_KEYMAPENT: /* set keyboard translation table
1869 case PIO_DEADKEYMAP: /* set accent key translation table */
1873 return (genkbd_commonioctl(kbd, cmd, arg));
1880 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1883 struct ukbd_softc *sc = kbd->kb_data;
1886 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
1887 * context where printf(9) can be called, which among other things
1888 * includes interrupt filters and threads with any kinds of locks
1889 * already held. For this reason it would be dangerous to acquire
1890 * the Giant here unconditionally. On the other hand we have to
1891 * have it to handle the ioctl.
1892 * So we make our best effort to auto-detect whether we can grab
1893 * the Giant or not. Blame syscons(4) for this.
1899 return (EDEADLK); /* best I could come up with */
1903 result = ukbd_ioctl_locked(kbd, cmd, arg);
1910 /* clear the internal state of the keyboard */
1912 ukbd_clear_state(keyboard_t *kbd)
1914 struct ukbd_softc *sc = kbd->kb_data;
1916 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1917 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1919 sc->sc_composed_char = 0;
1920 #ifdef UKBD_EMULATE_ATSCANCODE
1921 sc->sc_buffered_char[0] = 0;
1922 sc->sc_buffered_char[1] = 0;
1924 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1925 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
1926 memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
1927 memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
1930 /* save the internal state, not used */
1932 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1934 return (len == 0) ? 1 : -1;
1937 /* set the internal state, not used */
1939 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1945 ukbd_poll(keyboard_t *kbd, int on)
1947 struct ukbd_softc *sc = kbd->kb_data;
1951 sc->sc_flags |= UKBD_FLAG_POLLING;
1952 sc->sc_poll_thread = curthread;
1954 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1955 ukbd_start_timer(sc); /* start timer */
1962 /* local functions */
1965 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1968 DPRINTF("leds=0x%02x\n", leds);
1971 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1973 /* start transfer, if not already started */
1975 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1979 ukbd_set_typematic(keyboard_t *kbd, int code)
1981 static const int delays[] = {250, 500, 750, 1000};
1982 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1983 68, 76, 84, 92, 100, 110, 118, 126,
1984 136, 152, 168, 184, 200, 220, 236, 252,
1985 272, 304, 336, 368, 400, 440, 472, 504};
1990 kbd->kb_delay1 = delays[(code >> 5) & 3];
1991 kbd->kb_delay2 = rates[code & 0x1f];
1995 #ifdef UKBD_EMULATE_ATSCANCODE
1997 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1999 static const int scan[] = {
2005 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
2015 0x151, /* PageDown */
2018 0x146, /* XXX Pause/Break */
2019 0x15b, /* Win_L(Super_L) */
2020 0x15c, /* Win_R(Super_R) */
2021 0x15d, /* Application(Menu) */
2023 /* SUN TYPE 6 USB KEYBOARD */
2024 0x168, /* Sun Type 6 Help */
2025 0x15e, /* Sun Type 6 Stop */
2027 0x15f, /* Sun Type 6 Again */
2028 0x160, /* Sun Type 6 Props */
2029 0x161, /* Sun Type 6 Undo */
2030 0x162, /* Sun Type 6 Front */
2031 0x163, /* Sun Type 6 Copy */
2032 0x164, /* Sun Type 6 Open */
2033 0x165, /* Sun Type 6 Paste */
2034 0x166, /* Sun Type 6 Find */
2035 0x167, /* Sun Type 6 Cut */
2036 0x125, /* Sun Type 6 Mute */
2038 0x11f, /* Sun Type 6 VolumeDown */
2039 0x11e, /* Sun Type 6 VolumeUp */
2040 0x120, /* Sun Type 6 PowerDown */
2042 /* Japanese 106/109 keyboard */
2043 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
2044 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
2045 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
2046 0x79, /* Keyboard Intl' 4 (Henkan) */
2047 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
2048 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
2051 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
2052 code = scan[code - 89];
2055 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
2056 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
2058 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
2059 code &= ~SCAN_PREFIX_SHIFT;
2061 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
2063 if (code & SCAN_PREFIX) {
2064 if (code & SCAN_PREFIX_CTL) {
2066 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
2067 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
2068 } else if (code & SCAN_PREFIX_SHIFT) {
2070 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
2071 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
2073 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
2074 sc->sc_buffered_char[1] = 0;
2076 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
2082 #endif /* UKBD_EMULATE_ATSCANCODE */
2084 static keyboard_switch_t ukbdsw = {
2085 .probe = &ukbd__probe,
2089 .test_if = &ukbd_test_if,
2090 .enable = &ukbd_enable,
2091 .disable = &ukbd_disable,
2093 .check = &ukbd_check,
2094 .read_char = &ukbd_read_char,
2095 .check_char = &ukbd_check_char,
2096 .ioctl = &ukbd_ioctl,
2098 .clear_state = &ukbd_clear_state,
2099 .get_state = &ukbd_get_state,
2100 .set_state = &ukbd_set_state,
2101 .get_fkeystr = &genkbd_get_fkeystr,
2103 .diag = &genkbd_diag,
2106 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
2109 ukbd_driver_load(module_t mod, int what, void *arg)
2113 kbd_add_driver(&ukbd_kbd_driver);
2116 kbd_delete_driver(&ukbd_kbd_driver);
2122 static devclass_t ukbd_devclass;
2124 static device_method_t ukbd_methods[] = {
2125 DEVMETHOD(device_probe, ukbd_probe),
2126 DEVMETHOD(device_attach, ukbd_attach),
2127 DEVMETHOD(device_detach, ukbd_detach),
2128 DEVMETHOD(device_resume, ukbd_resume),
2132 static driver_t ukbd_driver = {
2134 .methods = ukbd_methods,
2135 .size = sizeof(struct ukbd_softc),
2138 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, NULL);
2139 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
2140 MODULE_VERSION(ukbd, 1);