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()) {
417 * In this context the kernel is polling for input,
418 * but the USB subsystem works in normal interrupt-driven
419 * mode, so we just wait on the USB threads to do the job.
420 * Note that we currently hold the Giant, but it's also used
421 * as the transfer mtx, so we must release it while waiting.
423 while (sc->sc_inputs == 0) {
425 * Give USB threads a chance to run. Note that
426 * kern_yield performs DROP_GIANT + PICKUP_GIANT.
437 while (sc->sc_inputs == 0) {
439 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
441 /* Delay-optimised support for repetition of keys */
442 if (ukbd_any_key_pressed(sc)) {
443 /* a key is pressed - need timekeeping */
446 /* 1 millisecond has passed */
458 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
462 UKBD_CTX_LOCK_ASSERT();
464 KASSERT((!kdb_active && !SCHEDULER_STOPPED())
465 || (sc->sc_flags & UKBD_FLAG_POLLING) != 0,
466 ("not polling in kdb or panic\n"));
469 if (sc->sc_inputs == 0) {
470 /* start transfer, if not already started */
471 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
474 if (sc->sc_flags & UKBD_FLAG_POLLING)
475 ukbd_do_poll(sc, wait);
477 if (sc->sc_inputs == 0) {
480 c = sc->sc_input[sc->sc_inputhead];
482 ++(sc->sc_inputhead);
483 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
484 sc->sc_inputhead = 0;
491 ukbd_interrupt(struct ukbd_softc *sc)
495 uint32_t now = sc->sc_time_ms;
501 UKBD_CTX_LOCK_ASSERT();
503 if (sc->sc_ndata.keycode[0] == KEY_ERROR)
506 n_mod = sc->sc_ndata.modifiers;
507 o_mod = sc->sc_odata.modifiers;
508 if (n_mod != o_mod) {
509 for (i = 0; i < UKBD_NMOD; i++) {
510 if ((n_mod & ukbd_mods[i].mask) !=
511 (o_mod & ukbd_mods[i].mask)) {
512 ukbd_put_key(sc, ukbd_mods[i].key |
513 ((n_mod & ukbd_mods[i].mask) ?
514 KEY_PRESS : KEY_RELEASE));
518 /* Check for released keys. */
519 for (i = 0; i < UKBD_NKEYCODE; i++) {
520 key = sc->sc_odata.keycode[i];
524 for (j = 0; j < UKBD_NKEYCODE; j++) {
525 if (sc->sc_ndata.keycode[j] == 0) {
528 if (key == sc->sc_ndata.keycode[j]) {
532 ukbd_put_key(sc, key | KEY_RELEASE);
536 /* Check for pressed keys. */
537 for (i = 0; i < UKBD_NKEYCODE; i++) {
538 key = sc->sc_ndata.keycode[i];
542 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
543 for (j = 0; j < UKBD_NKEYCODE; j++) {
544 if (sc->sc_odata.keycode[j] == 0) {
547 if (key == sc->sc_odata.keycode[j]) {
549 /* key is still pressed */
551 sc->sc_ntime[i] = sc->sc_otime[j];
552 dtime = (sc->sc_otime[j] - now);
554 if (!(dtime & 0x80000000)) {
555 /* time has not elapsed */
558 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
562 ukbd_put_key(sc, key | KEY_PRESS);
565 * If any other key is presently down, force its repeat to be
566 * well in the future (100s). This makes the last key to be
567 * pressed do the autorepeat.
569 for (j = 0; j != UKBD_NKEYCODE; j++) {
571 sc->sc_ntime[j] = now + (100 * 1000);
576 sc->sc_odata = sc->sc_ndata;
578 memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
580 ukbd_event_keyinput(sc);
584 ukbd_event_keyinput(struct ukbd_softc *sc)
588 UKBD_CTX_LOCK_ASSERT();
590 if ((sc->sc_flags & UKBD_FLAG_POLLING) != 0)
593 if (sc->sc_inputs == 0)
596 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
597 KBD_IS_BUSY(&sc->sc_kbd)) {
598 /* let the callback function process the input */
599 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
600 sc->sc_kbd.kb_callback.kc_arg);
602 /* read and discard the input, no one is waiting for it */
604 c = ukbd_read_char(&sc->sc_kbd, 0);
605 } while (c != NOKEY);
610 ukbd_timeout(void *arg)
612 struct ukbd_softc *sc = arg;
616 sc->sc_time_ms += 25; /* milliseconds */
620 /* Make sure any leftover key events gets read out */
621 ukbd_event_keyinput(sc);
623 if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
624 ukbd_start_timer(sc);
626 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
631 ukbd_apple_fn(uint8_t keycode) {
633 case 0x28: return 0x49; /* RETURN -> INSERT */
634 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
635 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
636 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
637 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
638 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
639 default: return keycode;
644 ukbd_apple_swap(uint8_t keycode) {
646 case 0x35: return 0x64;
647 case 0x64: return 0x35;
648 default: return keycode;
653 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
655 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
656 struct usb_page_cache *pc;
664 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
665 pc = usbd_xfer_get_frame(xfer, 0);
667 switch (USB_GET_STATE(xfer)) {
668 case USB_ST_TRANSFERRED:
669 DPRINTF("actlen=%d bytes\n", len);
672 DPRINTF("zero length data\n");
676 if (sc->sc_kbd_id != 0) {
677 /* check and remove HID ID byte */
678 usbd_copy_out(pc, 0, &id, 1);
682 DPRINTF("zero length data\n");
690 if (len > UKBD_BUFFER_SIZE)
691 len = UKBD_BUFFER_SIZE;
694 usbd_copy_out(pc, offset, sc->sc_buffer, len);
696 /* clear temporary storage */
697 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
699 /* scan through HID data */
700 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
701 (id == sc->sc_id_apple_eject)) {
702 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject))
703 sc->sc_modifiers |= MOD_EJECT;
705 sc->sc_modifiers &= ~MOD_EJECT;
707 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
708 (id == sc->sc_id_apple_fn)) {
709 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn))
710 sc->sc_modifiers |= MOD_FN;
712 sc->sc_modifiers &= ~MOD_FN;
714 if ((sc->sc_flags & UKBD_FLAG_CTRL_L) &&
715 (id == sc->sc_id_ctrl_l)) {
716 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_l))
717 sc-> sc_modifiers |= MOD_CONTROL_L;
719 sc-> sc_modifiers &= ~MOD_CONTROL_L;
721 if ((sc->sc_flags & UKBD_FLAG_CTRL_R) &&
722 (id == sc->sc_id_ctrl_r)) {
723 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_r))
724 sc->sc_modifiers |= MOD_CONTROL_R;
726 sc->sc_modifiers &= ~MOD_CONTROL_R;
728 if ((sc->sc_flags & UKBD_FLAG_SHIFT_L) &&
729 (id == sc->sc_id_shift_l)) {
730 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_l))
731 sc->sc_modifiers |= MOD_SHIFT_L;
733 sc->sc_modifiers &= ~MOD_SHIFT_L;
735 if ((sc->sc_flags & UKBD_FLAG_SHIFT_R) &&
736 (id == sc->sc_id_shift_r)) {
737 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_r))
738 sc->sc_modifiers |= MOD_SHIFT_R;
740 sc->sc_modifiers &= ~MOD_SHIFT_R;
742 if ((sc->sc_flags & UKBD_FLAG_ALT_L) &&
743 (id == sc->sc_id_alt_l)) {
744 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_l))
745 sc->sc_modifiers |= MOD_ALT_L;
747 sc->sc_modifiers &= ~MOD_ALT_L;
749 if ((sc->sc_flags & UKBD_FLAG_ALT_R) &&
750 (id == sc->sc_id_alt_r)) {
751 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_r))
752 sc->sc_modifiers |= MOD_ALT_R;
754 sc->sc_modifiers &= ~MOD_ALT_R;
756 if ((sc->sc_flags & UKBD_FLAG_WIN_L) &&
757 (id == sc->sc_id_win_l)) {
758 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_l))
759 sc->sc_modifiers |= MOD_WIN_L;
761 sc->sc_modifiers &= ~MOD_WIN_L;
763 if ((sc->sc_flags & UKBD_FLAG_WIN_R) &&
764 (id == sc->sc_id_win_r)) {
765 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_r))
766 sc->sc_modifiers |= MOD_WIN_R;
768 sc->sc_modifiers &= ~MOD_WIN_R;
771 sc->sc_ndata.modifiers = sc->sc_modifiers;
773 if ((sc->sc_flags & UKBD_FLAG_EVENTS) &&
774 (id == sc->sc_id_events)) {
775 i = sc->sc_loc_events.count;
776 if (i > UKBD_NKEYCODE)
781 sc->sc_ndata.keycode[i] =
782 hid_get_data(sc->sc_buffer + i, len - i,
788 DPRINTF("modifiers = 0x%04x\n", (int)sc->sc_modifiers);
789 for (i = 0; i < UKBD_NKEYCODE; i++) {
790 if (sc->sc_ndata.keycode[i]) {
791 DPRINTF("[%d] = 0x%02x\n",
792 (int)i, (int)sc->sc_ndata.keycode[i]);
796 if (sc->sc_modifiers & MOD_FN) {
797 for (i = 0; i < UKBD_NKEYCODE; i++) {
798 sc->sc_ndata.keycode[i] =
799 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
803 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
804 for (i = 0; i < UKBD_NKEYCODE; i++) {
805 sc->sc_ndata.keycode[i] =
806 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
812 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
813 if (ukbd_any_key_pressed(sc)) {
814 ukbd_start_timer(sc);
820 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
821 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
822 usbd_transfer_submit(xfer);
824 DPRINTF("input queue is full!\n");
829 DPRINTF("error=%s\n", usbd_errstr(error));
831 if (error != USB_ERR_CANCELLED) {
832 /* try to clear stall first */
833 usbd_xfer_set_stall(xfer);
841 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
843 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
844 struct usb_device_request req;
845 struct usb_page_cache *pc;
857 switch (USB_GET_STATE(xfer)) {
858 case USB_ST_TRANSFERRED:
860 if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS))
862 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
864 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
865 req.bRequest = UR_SET_REPORT;
866 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
867 req.wIndex[0] = sc->sc_iface_no;
871 memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE);
876 /* Assumption: All led bits must be in the same ID. */
878 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) {
879 if (sc->sc_leds & NLKED) {
880 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
881 &sc->sc_loc_numlock, 1);
883 id = sc->sc_id_numlock;
887 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) {
888 if (sc->sc_leds & SLKED) {
889 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
890 &sc->sc_loc_scrolllock, 1);
892 id = sc->sc_id_scrolllock;
896 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) {
897 if (sc->sc_leds & CLKED) {
898 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
899 &sc->sc_loc_capslock, 1);
901 id = sc->sc_id_capslock;
905 /* if no leds, nothing to do */
909 /* range check output report length */
910 len = sc->sc_led_size;
911 if (len > (UKBD_BUFFER_SIZE - 1))
912 len = (UKBD_BUFFER_SIZE - 1);
914 /* check if we need to prefix an ID byte */
915 sc->sc_buffer[0] = id;
917 pc = usbd_xfer_get_frame(xfer, 1);
920 usbd_copy_in(pc, 0, sc->sc_buffer, len);
922 usbd_copy_in(pc, 0, sc->sc_buffer + 1, len);
924 req.wLength[0] = len;
925 usbd_xfer_set_frame_len(xfer, 1, len);
927 DPRINTF("len=%d, id=%d\n", len, id);
929 /* setup control request last */
930 pc = usbd_xfer_get_frame(xfer, 0);
931 usbd_copy_in(pc, 0, &req, sizeof(req));
932 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
934 /* start data transfer */
935 usbd_xfer_set_frames(xfer, 2);
936 usbd_transfer_submit(xfer);
940 DPRINTFN(1, "error=%s\n", usbd_errstr(error));
945 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
948 .type = UE_INTERRUPT,
949 .endpoint = UE_ADDR_ANY,
950 .direction = UE_DIR_IN,
951 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
952 .bufsize = 0, /* use wMaxPacketSize */
953 .callback = &ukbd_intr_callback,
958 .endpoint = 0x00, /* Control pipe */
959 .direction = UE_DIR_ANY,
960 .bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE,
961 .callback = &ukbd_set_leds_callback,
962 .timeout = 1000, /* 1 second */
966 /* A match on these entries will load ukbd */
967 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = {
968 {USB_IFACE_CLASS(UICLASS_HID),
969 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
970 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),},
974 ukbd_probe(device_t dev)
976 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
977 struct usb_attach_arg *uaa = device_get_ivars(dev);
987 if (uaa->usb_mode != USB_MODE_HOST) {
991 if (uaa->info.bInterfaceClass != UICLASS_HID)
994 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
995 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) {
996 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
999 return (BUS_PROBE_DEFAULT);
1002 error = usbd_req_get_hid_desc(uaa->device, NULL,
1003 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
1009 * NOTE: we currently don't support USB mouse and USB keyboard
1010 * on the same USB endpoint.
1012 if (hid_is_collection(d_ptr, d_len,
1013 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
1014 /* most likely a mouse */
1016 } else if (hid_is_collection(d_ptr, d_len,
1017 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
1018 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1021 error = BUS_PROBE_DEFAULT;
1025 kfree(d_ptr, M_TEMP);
1030 ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len)
1034 /* reset detected bits */
1035 sc->sc_flags &= ~UKBD_FLAG_HID_MASK;
1037 /* check if there is an ID byte */
1038 sc->sc_kbd_size = hid_report_size(ptr, len,
1039 hid_input, &sc->sc_kbd_id);
1041 /* investigate if this is an Apple Keyboard */
1042 if (hid_locate(ptr, len,
1043 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
1044 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
1045 &sc->sc_id_apple_eject)) {
1046 if (flags & HIO_VARIABLE)
1047 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
1048 UKBD_FLAG_APPLE_SWAP;
1049 DPRINTFN(1, "Found Apple eject-key\n");
1051 if (hid_locate(ptr, len,
1052 HID_USAGE2(0xFFFF, 0x0003),
1053 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
1054 &sc->sc_id_apple_fn)) {
1055 if (flags & HIO_VARIABLE)
1056 sc->sc_flags |= UKBD_FLAG_APPLE_FN;
1057 DPRINTFN(1, "Found Apple FN-key\n");
1059 /* figure out some keys */
1060 if (hid_locate(ptr, len,
1061 HID_USAGE2(HUP_KEYBOARD, 0xE0),
1062 hid_input, 0, &sc->sc_loc_ctrl_l, &flags,
1063 &sc->sc_id_ctrl_l)) {
1064 if (flags & HIO_VARIABLE)
1065 sc->sc_flags |= UKBD_FLAG_CTRL_L;
1066 DPRINTFN(1, "Found left control\n");
1068 if (hid_locate(ptr, len,
1069 HID_USAGE2(HUP_KEYBOARD, 0xE4),
1070 hid_input, 0, &sc->sc_loc_ctrl_r, &flags,
1071 &sc->sc_id_ctrl_r)) {
1072 if (flags & HIO_VARIABLE)
1073 sc->sc_flags |= UKBD_FLAG_CTRL_R;
1074 DPRINTFN(1, "Found right control\n");
1076 if (hid_locate(ptr, len,
1077 HID_USAGE2(HUP_KEYBOARD, 0xE1),
1078 hid_input, 0, &sc->sc_loc_shift_l, &flags,
1079 &sc->sc_id_shift_l)) {
1080 if (flags & HIO_VARIABLE)
1081 sc->sc_flags |= UKBD_FLAG_SHIFT_L;
1082 DPRINTFN(1, "Found left shift\n");
1084 if (hid_locate(ptr, len,
1085 HID_USAGE2(HUP_KEYBOARD, 0xE5),
1086 hid_input, 0, &sc->sc_loc_shift_r, &flags,
1087 &sc->sc_id_shift_r)) {
1088 if (flags & HIO_VARIABLE)
1089 sc->sc_flags |= UKBD_FLAG_SHIFT_R;
1090 DPRINTFN(1, "Found right shift\n");
1092 if (hid_locate(ptr, len,
1093 HID_USAGE2(HUP_KEYBOARD, 0xE2),
1094 hid_input, 0, &sc->sc_loc_alt_l, &flags,
1095 &sc->sc_id_alt_l)) {
1096 if (flags & HIO_VARIABLE)
1097 sc->sc_flags |= UKBD_FLAG_ALT_L;
1098 DPRINTFN(1, "Found left alt\n");
1100 if (hid_locate(ptr, len,
1101 HID_USAGE2(HUP_KEYBOARD, 0xE6),
1102 hid_input, 0, &sc->sc_loc_alt_r, &flags,
1103 &sc->sc_id_alt_r)) {
1104 if (flags & HIO_VARIABLE)
1105 sc->sc_flags |= UKBD_FLAG_ALT_R;
1106 DPRINTFN(1, "Found right alt\n");
1108 if (hid_locate(ptr, len,
1109 HID_USAGE2(HUP_KEYBOARD, 0xE3),
1110 hid_input, 0, &sc->sc_loc_win_l, &flags,
1111 &sc->sc_id_win_l)) {
1112 if (flags & HIO_VARIABLE)
1113 sc->sc_flags |= UKBD_FLAG_WIN_L;
1114 DPRINTFN(1, "Found left GUI\n");
1116 if (hid_locate(ptr, len,
1117 HID_USAGE2(HUP_KEYBOARD, 0xE7),
1118 hid_input, 0, &sc->sc_loc_win_r, &flags,
1119 &sc->sc_id_win_r)) {
1120 if (flags & HIO_VARIABLE)
1121 sc->sc_flags |= UKBD_FLAG_WIN_R;
1122 DPRINTFN(1, "Found right GUI\n");
1124 /* figure out event buffer */
1125 if (hid_locate(ptr, len,
1126 HID_USAGE2(HUP_KEYBOARD, 0x00),
1127 hid_input, 0, &sc->sc_loc_events, &flags,
1128 &sc->sc_id_events)) {
1129 sc->sc_flags |= UKBD_FLAG_EVENTS;
1130 DPRINTFN(1, "Found keyboard events\n");
1133 /* figure out leds on keyboard */
1134 sc->sc_led_size = hid_report_size(ptr, len,
1137 if (hid_locate(ptr, len,
1138 HID_USAGE2(HUP_LEDS, 0x01),
1139 hid_output, 0, &sc->sc_loc_numlock, &flags,
1140 &sc->sc_id_numlock)) {
1141 if (flags & HIO_VARIABLE)
1142 sc->sc_flags |= UKBD_FLAG_NUMLOCK;
1143 DPRINTFN(1, "Found keyboard numlock\n");
1145 if (hid_locate(ptr, len,
1146 HID_USAGE2(HUP_LEDS, 0x02),
1147 hid_output, 0, &sc->sc_loc_capslock, &flags,
1148 &sc->sc_id_capslock)) {
1149 if (flags & HIO_VARIABLE)
1150 sc->sc_flags |= UKBD_FLAG_CAPSLOCK;
1151 DPRINTFN(1, "Found keyboard capslock\n");
1153 if (hid_locate(ptr, len,
1154 HID_USAGE2(HUP_LEDS, 0x03),
1155 hid_output, 0, &sc->sc_loc_scrolllock, &flags,
1156 &sc->sc_id_scrolllock)) {
1157 if (flags & HIO_VARIABLE)
1158 sc->sc_flags |= UKBD_FLAG_SCROLLLOCK;
1159 DPRINTFN(1, "Found keyboard scrolllock\n");
1164 ukbd_attach(device_t dev)
1166 struct ukbd_softc *sc = device_get_softc(dev);
1167 struct usb_attach_arg *uaa = device_get_ivars(dev);
1168 int32_t unit = device_get_unit(dev);
1169 keyboard_t *kbd = &sc->sc_kbd;
1170 void *hid_ptr = NULL;
1175 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER,
1176 unit, 0, KB_PRI_USB, 0, 0);
1178 kbd->kb_data = (void *)sc;
1180 device_set_usb_desc(dev);
1182 sc->sc_udev = uaa->device;
1183 sc->sc_iface = uaa->iface;
1184 sc->sc_iface_index = uaa->info.bIfaceIndex;
1185 sc->sc_iface_no = uaa->info.bIfaceNum;
1186 sc->sc_mode = K_XLATE;
1188 usb_callout_init_mtx(&sc->sc_callout, &kbd->kb_lock, 0);
1190 err = usbd_transfer_setup(uaa->device,
1191 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
1192 UKBD_N_TRANSFER, sc, &sc->sc_kbd.kb_lock);
1195 DPRINTF("error=%s\n", usbd_errstr(err));
1198 /* setup default keyboard maps */
1200 sc->sc_keymap = key_map;
1201 sc->sc_accmap = accent_map;
1202 for (n = 0; n < UKBD_NFKEY; n++) {
1203 sc->sc_fkeymap[n] = fkey_tab[n];
1206 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
1207 sc->sc_fkeymap, UKBD_NFKEY);
1209 KBD_FOUND_DEVICE(kbd);
1211 ukbd_clear_state(kbd);
1214 * FIXME: set the initial value for lock keys in "sc_state"
1215 * according to the BIOS data?
1217 KBD_PROBE_DONE(kbd);
1219 /* get HID descriptor */
1220 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
1221 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
1224 DPRINTF("Parsing HID descriptor of %d bytes\n",
1227 ukbd_parse_hid(sc, hid_ptr, hid_len);
1229 kfree(hid_ptr, M_TEMP);
1232 /* check if we should use the boot protocol */
1233 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) ||
1234 (err != 0) || (!(sc->sc_flags & UKBD_FLAG_EVENTS))) {
1236 DPRINTF("Forcing boot protocol\n");
1238 err = usbd_req_set_protocol(sc->sc_udev, NULL,
1239 sc->sc_iface_index, 0);
1242 DPRINTF("Set protocol error=%s (ignored)\n",
1246 ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc));
1249 /* ignore if SETIDLE fails, hence it is not crucial */
1250 usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
1252 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
1256 if (kbd_register(kbd) < 0) {
1259 KBD_CONFIG_DONE(kbd);
1263 #ifdef KBD_INSTALL_CDEV
1264 if (kbd_attach(kbd)) {
1268 sc->sc_flags |= UKBD_FLAG_ATTACHED;
1271 genkbd_diag(kbd, bootverbose);
1274 /* start the keyboard */
1276 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
1279 return (0); /* success */
1283 return (ENXIO); /* error */
1287 ukbd_detach(device_t dev)
1289 struct ukbd_softc *sc = device_get_softc(dev);
1295 sc->sc_flags |= UKBD_FLAG_GONE;
1297 usb_callout_stop(&sc->sc_callout);
1299 ukbd_disable(&sc->sc_kbd);
1302 * XXX make sure this is in the correct place here,
1303 * it was taken from below the second if()
1305 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1306 usb_callout_drain(&sc->sc_callout);
1308 #ifdef KBD_INSTALL_CDEV
1309 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1310 error = kbd_detach(&sc->sc_kbd);
1312 /* usb attach cannot return an error */
1313 device_printf(dev, "WARNING: kbd_detach() "
1314 "returned non-zero! (ignored)\n");
1318 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
1320 * kbd_unregister requires kb_lock to be held
1321 * but lockuninits it then
1324 error = kbd_unregister(&sc->sc_kbd);
1326 /* usb attach cannot return an error */
1327 device_printf(dev, "WARNING: kbd_unregister() "
1328 "returned non-zero! (ignored)\n");
1331 sc->sc_kbd.kb_flags = 0;
1336 DPRINTF("%s: disconnected\n",
1337 device_get_nameunit(dev));
1343 ukbd_resume(device_t dev)
1345 struct ukbd_softc *sc = device_get_softc(dev);
1347 ukbd_clear_state(&sc->sc_kbd);
1352 /* early keyboard probe, not supported */
1354 ukbd_configure(int flags)
1359 /* detect a keyboard, not used */
1361 ukbd__probe(int unit, void *arg, int flags)
1366 /* reset and initialize the device, not used */
1368 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1373 /* test the interface to the device, not used */
1375 ukbd_test_if(keyboard_t *kbd)
1380 /* finish using this keyboard, not used */
1382 ukbd_term(keyboard_t *kbd)
1387 /* keyboard interrupt routine, not used */
1389 ukbd_intr(keyboard_t *kbd, void *arg)
1394 /* lock the access to the keyboard, not used */
1396 ukbd_lock(keyboard_t *kbd, int lock)
1402 * Enable the access to the device; until this function is called,
1403 * the client cannot read from the keyboard.
1406 ukbd_enable(keyboard_t *kbd)
1415 /* disallow the access to the device */
1417 ukbd_disable(keyboard_t *kbd)
1420 KBD_DEACTIVATE(kbd);
1426 /* check if data is waiting */
1427 /* Currently unused. */
1429 ukbd_check(keyboard_t *kbd)
1431 struct ukbd_softc *sc = kbd->kb_data;
1433 if (!KBD_IS_ACTIVE(kbd))
1436 if (sc->sc_flags & UKBD_FLAG_POLLING)
1437 ukbd_do_poll(sc, 0);
1439 #ifdef UKBD_EMULATE_ATSCANCODE
1440 if (sc->sc_buffered_char[0]) {
1444 if (sc->sc_inputs > 0) {
1450 /* check if char is waiting */
1452 ukbd_check_char_locked(keyboard_t *kbd)
1454 struct ukbd_softc *sc = kbd->kb_data;
1456 if (!KBD_IS_ACTIVE(kbd))
1459 if ((sc->sc_composed_char > 0) &&
1460 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1463 return (ukbd_check(kbd));
1467 ukbd_check_char(keyboard_t *kbd)
1471 struct ukbd_softc *sc = kbd->kb_data;
1475 result = ukbd_check_char_locked(kbd);
1484 /* read one byte from the keyboard if it's allowed */
1485 /* Currently unused. */
1487 ukbd_read(keyboard_t *kbd, int wait)
1489 struct ukbd_softc *sc = kbd->kb_data;
1491 #ifdef UKBD_EMULATE_ATSCANCODE
1497 if (!KBD_IS_ACTIVE(kbd))
1500 #ifdef UKBD_EMULATE_ATSCANCODE
1501 if (sc->sc_buffered_char[0]) {
1502 scancode = sc->sc_buffered_char[0];
1503 if (scancode & SCAN_PREFIX) {
1504 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1505 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1507 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1508 sc->sc_buffered_char[1] = 0;
1511 #endif /* UKBD_EMULATE_ATSCANCODE */
1514 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1515 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1520 #ifdef UKBD_EMULATE_ATSCANCODE
1521 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1522 if (keycode == NN) {
1525 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1526 (usbcode & KEY_RELEASE)));
1527 #else /* !UKBD_EMULATE_ATSCANCODE */
1529 #endif /* UKBD_EMULATE_ATSCANCODE */
1532 /* read char from the keyboard */
1534 ukbd_read_char_locked(keyboard_t *kbd, int wait)
1536 struct ukbd_softc *sc = kbd->kb_data;
1540 #ifdef UKBD_EMULATE_ATSCANCODE
1544 if (!KBD_IS_ACTIVE(kbd))
1549 /* do we have a composed char to return ? */
1551 if ((sc->sc_composed_char > 0) &&
1552 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1554 action = sc->sc_composed_char;
1555 sc->sc_composed_char = 0;
1557 if (action > 0xFF) {
1562 #ifdef UKBD_EMULATE_ATSCANCODE
1564 /* do we have a pending raw scan code? */
1566 if (sc->sc_mode == K_RAW) {
1567 scancode = sc->sc_buffered_char[0];
1569 if (scancode & SCAN_PREFIX) {
1570 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1571 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1573 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1574 sc->sc_buffered_char[1] = 0;
1578 #endif /* UKBD_EMULATE_ATSCANCODE */
1580 /* see if there is something in the keyboard port */
1582 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1583 if (usbcode == -1) {
1588 #ifdef UKBD_EMULATE_ATSCANCODE
1589 /* USB key index -> key code -> AT scan code */
1590 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1591 if (keycode == NN) {
1594 /* return an AT scan code for the K_RAW mode */
1595 if (sc->sc_mode == K_RAW) {
1596 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1597 (usbcode & KEY_RELEASE)));
1599 #else /* !UKBD_EMULATE_ATSCANCODE */
1601 /* return the byte as is for the K_RAW mode */
1602 if (sc->sc_mode == K_RAW) {
1605 /* USB key index -> key code */
1606 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1607 if (keycode == NN) {
1610 #endif /* UKBD_EMULATE_ATSCANCODE */
1613 case 0x38: /* left alt (compose key) */
1614 if (usbcode & KEY_RELEASE) {
1615 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1616 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1618 if (sc->sc_composed_char > 0xFF) {
1619 sc->sc_composed_char = 0;
1623 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1624 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1625 sc->sc_composed_char = 0;
1629 /* XXX: I don't like these... */
1630 case 0x5c: /* print screen */
1631 if (sc->sc_flags & ALTS) {
1632 keycode = 0x54; /* sysrq */
1635 case 0x68: /* pause/break */
1636 if (sc->sc_flags & CTLS) {
1637 keycode = 0x6c; /* break */
1642 /* return the key code in the K_CODE mode */
1643 if (usbcode & KEY_RELEASE) {
1644 keycode |= SCAN_RELEASE;
1646 if (sc->sc_mode == K_CODE) {
1649 /* compose a character code */
1650 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1652 /* key pressed, process it */
1655 case 0x49: /* keypad 7,8,9 */
1656 sc->sc_composed_char *= 10;
1657 sc->sc_composed_char += keycode - 0x40;
1658 goto check_composed;
1662 case 0x4D: /* keypad 4,5,6 */
1663 sc->sc_composed_char *= 10;
1664 sc->sc_composed_char += keycode - 0x47;
1665 goto check_composed;
1669 case 0x51: /* keypad 1,2,3 */
1670 sc->sc_composed_char *= 10;
1671 sc->sc_composed_char += keycode - 0x4E;
1672 goto check_composed;
1674 case 0x52: /* keypad 0 */
1675 sc->sc_composed_char *= 10;
1676 goto check_composed;
1678 /* key released, no interest here */
1679 case SCAN_RELEASE | 0x47:
1680 case SCAN_RELEASE | 0x48:
1681 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1682 case SCAN_RELEASE | 0x4B:
1683 case SCAN_RELEASE | 0x4C:
1684 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1685 case SCAN_RELEASE | 0x4F:
1686 case SCAN_RELEASE | 0x50:
1687 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1688 case SCAN_RELEASE | 0x52: /* keypad 0 */
1691 case 0x38: /* left alt key */
1695 if (sc->sc_composed_char > 0) {
1696 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1697 sc->sc_composed_char = 0;
1703 /* keycode to key action */
1704 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1705 (keycode & SCAN_RELEASE),
1706 &sc->sc_state, &sc->sc_accents);
1707 if (action == NOKEY) {
1714 if (sc->sc_composed_char <= 0xFF) {
1721 /* Currently wait is always false. */
1723 ukbd_read_char(keyboard_t *kbd, int wait)
1727 struct ukbd_softc *sc = kbd->kb_data;
1731 keycode = ukbd_read_char_locked(kbd, wait);
1739 /* some useful control functions */
1741 ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
1743 struct ukbd_softc *sc = kbd->kb_data;
1746 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1747 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1754 case KDGKBMODE: /* get keyboard mode */
1755 *(int *)arg = sc->sc_mode;
1758 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1759 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1761 ival = IOCPARM_IVAL(arg);
1762 arg = (caddr_t)&ival;
1766 case KDSKBMODE: /* set keyboard mode */
1767 switch (*(int *)arg) {
1769 if (sc->sc_mode != K_XLATE) {
1770 /* make lock key state and LED state match */
1771 sc->sc_state &= ~LOCK_MASK;
1772 sc->sc_state |= KBD_LED_VAL(kbd);
1777 if (sc->sc_mode != *(int *)arg) {
1778 if ((sc->sc_flags & UKBD_FLAG_POLLING) == 0)
1779 ukbd_clear_state(kbd);
1780 sc->sc_mode = *(int *)arg;
1788 case KDGETLED: /* get keyboard LED */
1789 *(int *)arg = KBD_LED_VAL(kbd);
1792 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1793 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1795 ival = IOCPARM_IVAL(arg);
1796 arg = (caddr_t)&ival;
1800 case KDSETLED: /* set keyboard LED */
1801 /* NOTE: lock key state in "sc_state" won't be changed */
1802 if (*(int *)arg & ~LOCK_MASK)
1807 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1808 if (sc->sc_mode == K_XLATE &&
1809 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1815 if (KBD_HAS_DEVICE(kbd))
1816 ukbd_set_leds(sc, i);
1818 KBD_LED_VAL(kbd) = *(int *)arg;
1820 case KDGKBSTATE: /* get lock key state */
1821 *(int *)arg = sc->sc_state & LOCK_MASK;
1824 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1825 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1827 ival = IOCPARM_IVAL(arg);
1828 arg = (caddr_t)&ival;
1832 case KDSKBSTATE: /* set lock key state */
1833 if (*(int *)arg & ~LOCK_MASK) {
1836 sc->sc_state &= ~LOCK_MASK;
1837 sc->sc_state |= *(int *)arg;
1839 /* set LEDs and quit */
1840 return (ukbd_ioctl(kbd, KDSETLED, arg));
1842 case KDSETREPEAT: /* set keyboard repeat rate (new
1844 if (!KBD_HAS_DEVICE(kbd)) {
1847 if (((int *)arg)[1] < 0) {
1850 if (((int *)arg)[0] < 0) {
1853 if (((int *)arg)[0] < 200) /* fastest possible value */
1854 kbd->kb_delay1 = 200;
1856 kbd->kb_delay1 = ((int *)arg)[0];
1857 kbd->kb_delay2 = ((int *)arg)[1];
1861 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1862 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1864 ival = IOCPARM_IVAL(arg);
1865 arg = (caddr_t)&ival;
1869 case KDSETRAD: /* set keyboard repeat rate (old
1871 return (ukbd_set_typematic(kbd, *(int *)arg));
1873 case PIO_KEYMAP: /* set keyboard translation table */
1874 case PIO_KEYMAPENT: /* set keyboard translation table
1876 case PIO_DEADKEYMAP: /* set accent key translation table */
1880 return (genkbd_commonioctl(kbd, cmd, arg));
1887 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1890 struct ukbd_softc *sc = kbd->kb_data;
1893 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
1894 * context where printf(9) can be called, which among other things
1895 * includes interrupt filters and threads with any kinds of locks
1896 * already held. For this reason it would be dangerous to acquire
1897 * the Giant here unconditionally. On the other hand we have to
1898 * have it to handle the ioctl.
1899 * So we make our best effort to auto-detect whether we can grab
1900 * the Giant or not. Blame syscons(4) for this.
1906 if(!lockowned(&kbd->kb_lock)) {
1907 return (EDEADLK); /* best I could come up with */
1912 result = ukbd_ioctl_locked(kbd, cmd, arg);
1919 /* clear the internal state of the keyboard */
1921 ukbd_clear_state(keyboard_t *kbd)
1923 struct ukbd_softc *sc = kbd->kb_data;
1925 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1926 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1928 sc->sc_composed_char = 0;
1929 #ifdef UKBD_EMULATE_ATSCANCODE
1930 sc->sc_buffered_char[0] = 0;
1931 sc->sc_buffered_char[1] = 0;
1933 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1934 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
1935 memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
1936 memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
1939 /* save the internal state, not used */
1941 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1943 return (len == 0) ? 1 : -1;
1946 /* set the internal state, not used */
1948 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1954 ukbd_poll(keyboard_t *kbd, int on)
1956 struct ukbd_softc *sc = kbd->kb_data;
1960 sc->sc_flags |= UKBD_FLAG_POLLING;
1961 sc->sc_poll_thread = curthread;
1963 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1964 ukbd_start_timer(sc); /* start timer */
1971 /* local functions */
1974 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1977 DPRINTF("leds=0x%02x\n", leds);
1980 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1982 /* start transfer, if not already started */
1984 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1988 ukbd_set_typematic(keyboard_t *kbd, int code)
1990 static const int delays[] = {250, 500, 750, 1000};
1991 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1992 68, 76, 84, 92, 100, 110, 118, 126,
1993 136, 152, 168, 184, 200, 220, 236, 252,
1994 272, 304, 336, 368, 400, 440, 472, 504};
1999 kbd->kb_delay1 = delays[(code >> 5) & 3];
2000 kbd->kb_delay2 = rates[code & 0x1f];
2004 #ifdef UKBD_EMULATE_ATSCANCODE
2006 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
2008 static const int scan[] = {
2014 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
2024 0x151, /* PageDown */
2027 0x146, /* XXX Pause/Break */
2028 0x15b, /* Win_L(Super_L) */
2029 0x15c, /* Win_R(Super_R) */
2030 0x15d, /* Application(Menu) */
2032 /* SUN TYPE 6 USB KEYBOARD */
2033 0x168, /* Sun Type 6 Help */
2034 0x15e, /* Sun Type 6 Stop */
2036 0x15f, /* Sun Type 6 Again */
2037 0x160, /* Sun Type 6 Props */
2038 0x161, /* Sun Type 6 Undo */
2039 0x162, /* Sun Type 6 Front */
2040 0x163, /* Sun Type 6 Copy */
2041 0x164, /* Sun Type 6 Open */
2042 0x165, /* Sun Type 6 Paste */
2043 0x166, /* Sun Type 6 Find */
2044 0x167, /* Sun Type 6 Cut */
2045 0x125, /* Sun Type 6 Mute */
2047 0x11f, /* Sun Type 6 VolumeDown */
2048 0x11e, /* Sun Type 6 VolumeUp */
2049 0x120, /* Sun Type 6 PowerDown */
2051 /* Japanese 106/109 keyboard */
2052 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
2053 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
2054 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
2055 0x79, /* Keyboard Intl' 4 (Henkan) */
2056 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
2057 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
2060 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
2061 code = scan[code - 89];
2064 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
2065 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
2067 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
2068 code &= ~SCAN_PREFIX_SHIFT;
2070 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
2072 if (code & SCAN_PREFIX) {
2073 if (code & SCAN_PREFIX_CTL) {
2075 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
2076 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
2077 } else if (code & SCAN_PREFIX_SHIFT) {
2079 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
2080 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
2082 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
2083 sc->sc_buffered_char[1] = 0;
2085 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
2091 #endif /* UKBD_EMULATE_ATSCANCODE */
2093 static keyboard_switch_t ukbdsw = {
2094 .probe = &ukbd__probe,
2098 .test_if = &ukbd_test_if,
2099 .enable = &ukbd_enable,
2100 .disable = &ukbd_disable,
2102 .check = &ukbd_check,
2103 .read_char = &ukbd_read_char,
2104 .check_char = &ukbd_check_char,
2105 .ioctl = &ukbd_ioctl,
2107 .clear_state = &ukbd_clear_state,
2108 .get_state = &ukbd_get_state,
2109 .set_state = &ukbd_set_state,
2110 .get_fkeystr = &genkbd_get_fkeystr,
2112 .diag = &genkbd_diag,
2115 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
2118 ukbd_driver_load(module_t mod, int what, void *arg)
2122 kbd_add_driver(&ukbd_kbd_driver);
2125 kbd_delete_driver(&ukbd_kbd_driver);
2131 static devclass_t ukbd_devclass;
2133 static device_method_t ukbd_methods[] = {
2134 DEVMETHOD(device_probe, ukbd_probe),
2135 DEVMETHOD(device_attach, ukbd_attach),
2136 DEVMETHOD(device_detach, ukbd_detach),
2137 DEVMETHOD(device_resume, ukbd_resume),
2141 static driver_t ukbd_driver = {
2143 .methods = ukbd_methods,
2144 .size = sizeof(struct ukbd_softc),
2147 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, NULL);
2148 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
2149 MODULE_VERSION(ukbd, 1);