6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
37 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
40 #include "opt_compat.h"
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/linker_set.h>
53 #include <sys/module.h>
55 #include <sys/mutex.h>
56 #include <sys/condvar.h>
57 #include <sys/sysctl.h>
59 #include <sys/unistd.h>
60 #include <sys/callout.h>
61 #include <sys/malloc.h>
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68 #include <dev/usb/usbhid.h>
70 #define USB_DEBUG_VAR ukbd_debug
71 #include <dev/usb/usb_debug.h>
73 #include <dev/usb/quirk/usb_quirk.h>
75 #include <sys/ioccom.h>
76 #include <sys/filio.h>
80 #include <dev/kbd/kbdreg.h>
82 /* the initial key map, accent map and fkey strings */
83 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
84 #define KBD_DFLT_KEYMAP
88 /* the following file must be included after "ukbdmap.h" */
89 #include <dev/kbd/kbdtables.h>
92 static int ukbd_debug = 0;
93 static int ukbd_no_leds = 0;
95 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
96 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
97 &ukbd_debug, 0, "Debug level");
98 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW,
99 &ukbd_no_leds, 0, "Disables setting of keyboard leds");
101 TUNABLE_INT("hw.usb.ukbd.debug", &ukbd_debug);
102 TUNABLE_INT("hw.usb.ukbd.no_leds", &ukbd_no_leds);
105 #define UKBD_EMULATE_ATSCANCODE 1
106 #define UKBD_DRIVER_NAME "ukbd"
107 #define UKBD_NMOD 8 /* units */
108 #define UKBD_NKEYCODE 6 /* units */
109 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */
110 #define UKBD_IN_BUF_FULL (UKBD_IN_BUF_SIZE / 2) /* bytes */
111 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
115 #define MOD_CONTROL_L 0x01
116 #define MOD_CONTROL_R 0x10
117 #define MOD_SHIFT_L 0x02
118 #define MOD_SHIFT_R 0x20
119 #define MOD_ALT_L 0x04
120 #define MOD_ALT_R 0x40
121 #define MOD_WIN_L 0x08
122 #define MOD_WIN_R 0x80
124 uint8_t keycode[UKBD_NKEYCODE];
137 accentmap_t sc_accmap;
138 fkeytab_t sc_fkeymap[UKBD_NFKEY];
139 struct hid_location sc_loc_apple_eject;
140 struct hid_location sc_loc_apple_fn;
141 struct usb_callout sc_callout;
142 struct ukbd_data sc_ndata;
143 struct ukbd_data sc_odata;
145 struct thread *sc_poll_thread;
146 struct usb_device *sc_udev;
147 struct usb_interface *sc_iface;
148 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
150 uint32_t sc_ntime[UKBD_NKEYCODE];
151 uint32_t sc_otime[UKBD_NKEYCODE];
152 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
154 uint32_t sc_composed_char; /* composed char code, if non-zero */
155 #ifdef UKBD_EMULATE_ATSCANCODE
156 uint32_t sc_buffered_char[2];
158 uint32_t sc_flags; /* flags */
159 #define UKBD_FLAG_COMPOSE 0x0001
160 #define UKBD_FLAG_POLLING 0x0002
161 #define UKBD_FLAG_SET_LEDS 0x0004
162 #define UKBD_FLAG_ATTACHED 0x0010
163 #define UKBD_FLAG_GONE 0x0020
164 #define UKBD_FLAG_APPLE_EJECT 0x0040
165 #define UKBD_FLAG_APPLE_FN 0x0080
166 #define UKBD_FLAG_APPLE_SWAP 0x0100
167 #define UKBD_FLAG_TIMER_RUNNING 0x0200
169 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
170 int sc_state; /* shift/lock key state */
171 int sc_accents; /* accent key index (> 0) */
172 int sc_poll_tick_last;
175 uint16_t sc_inputhead;
176 uint16_t sc_inputtail;
178 uint8_t sc_leds; /* store for async led requests */
179 uint8_t sc_iface_index;
183 uint8_t sc_poll_detected;
186 #define KEY_ERROR 0x01
189 #define KEY_RELEASE 0x400
190 #define KEY_INDEX(c) ((c) & 0xFF)
193 #define SCAN_RELEASE 0x80
194 #define SCAN_PREFIX_E0 0x100
195 #define SCAN_PREFIX_E1 0x200
196 #define SCAN_PREFIX_CTL 0x400
197 #define SCAN_PREFIX_SHIFT 0x800
198 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
199 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
200 #define SCAN_CHAR(c) ((c) & 0x7f)
206 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
207 {MOD_CONTROL_L, 0xe0},
208 {MOD_CONTROL_R, 0xe4},
217 #define NN 0 /* no translation */
219 * Translate USB keycodes to AT keyboard scancodes.
222 * FIXME: Mac USB keyboard generates:
223 * 0x53: keypad NumLock/Clear
230 static const uint8_t ukbd_trtab[256] = {
231 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
232 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
233 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
234 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
235 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
236 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
237 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
238 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
239 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
240 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
241 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
242 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
243 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
244 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
245 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
246 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
247 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */
248 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */
249 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
250 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
251 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
252 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
253 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
254 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
255 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
256 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
257 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
258 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
259 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
260 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
261 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
262 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
266 static void ukbd_timeout(void *);
267 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
268 static int ukbd_set_typematic(keyboard_t *, int);
269 #ifdef UKBD_EMULATE_ATSCANCODE
270 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
272 static uint32_t ukbd_read_char(keyboard_t *, int);
273 static void ukbd_clear_state(keyboard_t *);
274 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
275 static int ukbd_enable(keyboard_t *);
276 static int ukbd_disable(keyboard_t *);
277 static void ukbd_interrupt(struct ukbd_softc *);
278 static int ukbd_is_polling(struct ukbd_softc *);
279 static int ukbd_polls_other_thread(struct ukbd_softc *);
280 static void ukbd_event_keyinput(struct ukbd_softc *);
282 static device_probe_t ukbd_probe;
283 static device_attach_t ukbd_attach;
284 static device_detach_t ukbd_detach;
285 static device_resume_t ukbd_resume;
288 ukbd_any_key_pressed(struct ukbd_softc *sc)
293 for (j = i = 0; i < UKBD_NKEYCODE; i++)
294 j |= sc->sc_odata.keycode[i];
300 ukbd_start_timer(struct ukbd_softc *sc)
302 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
303 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
307 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
309 mtx_assert(&Giant, MA_OWNED);
311 DPRINTF("0x%02x (%d) %s\n", key, key,
312 (key & KEY_RELEASE) ? "released" : "pressed");
314 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
315 sc->sc_input[sc->sc_inputtail] = key;
317 ++(sc->sc_inputtail);
318 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
319 sc->sc_inputtail = 0;
322 DPRINTF("input buffer is full\n");
327 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
329 DPRINTFN(2, "polling\n");
331 /* update stats about last polling event */
332 sc->sc_poll_tick_last = ticks;
333 sc->sc_poll_detected = 1;
335 if (kdb_active == 0) {
336 while (sc->sc_inputs == 0) {
337 /* make sure the USB code gets a chance to run */
340 /* check if we should wait */
344 return; /* Only poll if KDB is active */
347 while (sc->sc_inputs == 0) {
349 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
351 /* Delay-optimised support for repetition of keys */
353 if (ukbd_any_key_pressed(sc)) {
354 /* a key is pressed - need timekeeping */
357 /* 1 millisecond has passed */
369 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
373 mtx_assert(&Giant, MA_OWNED);
375 if (sc->sc_inputs == 0) {
376 /* start transfer, if not already started */
377 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
380 if (ukbd_polls_other_thread(sc))
383 if (sc->sc_flags & UKBD_FLAG_POLLING)
384 ukbd_do_poll(sc, wait);
386 if (sc->sc_inputs == 0) {
389 c = sc->sc_input[sc->sc_inputhead];
391 ++(sc->sc_inputhead);
392 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
393 sc->sc_inputhead = 0;
400 ukbd_interrupt(struct ukbd_softc *sc)
404 uint32_t now = sc->sc_time_ms;
410 if (sc->sc_ndata.keycode[0] == KEY_ERROR)
413 n_mod = sc->sc_ndata.modifiers;
414 o_mod = sc->sc_odata.modifiers;
415 if (n_mod != o_mod) {
416 for (i = 0; i < UKBD_NMOD; i++) {
417 if ((n_mod & ukbd_mods[i].mask) !=
418 (o_mod & ukbd_mods[i].mask)) {
419 ukbd_put_key(sc, ukbd_mods[i].key |
420 ((n_mod & ukbd_mods[i].mask) ?
421 KEY_PRESS : KEY_RELEASE));
425 /* Check for released keys. */
426 for (i = 0; i < UKBD_NKEYCODE; i++) {
427 key = sc->sc_odata.keycode[i];
431 for (j = 0; j < UKBD_NKEYCODE; j++) {
432 if (sc->sc_ndata.keycode[j] == 0) {
435 if (key == sc->sc_ndata.keycode[j]) {
439 ukbd_put_key(sc, key | KEY_RELEASE);
443 /* Check for pressed keys. */
444 for (i = 0; i < UKBD_NKEYCODE; i++) {
445 key = sc->sc_ndata.keycode[i];
449 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
450 for (j = 0; j < UKBD_NKEYCODE; j++) {
451 if (sc->sc_odata.keycode[j] == 0) {
454 if (key == sc->sc_odata.keycode[j]) {
456 /* key is still pressed */
458 sc->sc_ntime[i] = sc->sc_otime[j];
459 dtime = (sc->sc_otime[j] - now);
461 if (!(dtime & 0x80000000)) {
462 /* time has not elapsed */
465 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
469 ukbd_put_key(sc, key | KEY_PRESS);
472 * If any other key is presently down, force its repeat to be
473 * well in the future (100s). This makes the last key to be
474 * pressed do the autorepeat.
476 for (j = 0; j != UKBD_NKEYCODE; j++) {
478 sc->sc_ntime[j] = now + (100 * 1000);
483 sc->sc_odata = sc->sc_ndata;
485 memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
487 ukbd_event_keyinput(sc);
491 ukbd_event_keyinput(struct ukbd_softc *sc)
495 if (ukbd_is_polling(sc))
498 if (sc->sc_inputs == 0)
501 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
502 KBD_IS_BUSY(&sc->sc_kbd)) {
503 /* let the callback function process the input */
504 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
505 sc->sc_kbd.kb_callback.kc_arg);
507 /* read and discard the input, no one is waiting for it */
509 c = ukbd_read_char(&sc->sc_kbd, 0);
510 } while (c != NOKEY);
515 ukbd_timeout(void *arg)
517 struct ukbd_softc *sc = arg;
519 mtx_assert(&Giant, MA_OWNED);
521 sc->sc_time_ms += 25; /* milliseconds */
525 /* Make sure any leftover key events gets read out */
526 ukbd_event_keyinput(sc);
528 if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
529 ukbd_start_timer(sc);
531 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
536 ukbd_apple_fn(uint8_t keycode) {
538 case 0x28: return 0x49; /* RETURN -> INSERT */
539 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
540 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
541 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
542 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
543 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
544 default: return keycode;
549 ukbd_apple_swap(uint8_t keycode) {
551 case 0x35: return 0x64;
552 case 0x64: return 0x35;
553 default: return keycode;
558 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
560 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
561 struct usb_page_cache *pc;
569 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
570 pc = usbd_xfer_get_frame(xfer, 0);
572 switch (USB_GET_STATE(xfer)) {
573 case USB_ST_TRANSFERRED:
574 DPRINTF("actlen=%d bytes\n", len);
577 DPRINTF("zero length data\n");
581 if (sc->sc_kbd_id != 0) {
582 /* check and remove HID ID byte */
583 usbd_copy_out(pc, 0, &id, 1);
584 if (id != sc->sc_kbd_id) {
585 DPRINTF("wrong HID ID\n");
594 if (len > sizeof(sc->sc_ndata)) {
595 len = sizeof(sc->sc_ndata);
599 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
600 usbd_copy_out(pc, offset, &sc->sc_ndata, len);
602 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
603 hid_get_data((uint8_t *)&sc->sc_ndata,
604 len, &sc->sc_loc_apple_eject))
609 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
610 hid_get_data((uint8_t *)&sc->sc_ndata,
611 len, &sc->sc_loc_apple_fn))
616 DPRINTF("apple_eject=%u apple_fn=%u\n",
617 apple_eject, apple_fn);
619 if (sc->sc_ndata.modifiers) {
620 DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers);
622 for (i = 0; i < UKBD_NKEYCODE; i++) {
623 if (sc->sc_ndata.keycode[i]) {
624 DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]);
627 #endif /* USB_DEBUG */
630 for (i = 0; i < UKBD_NKEYCODE; i++) {
631 sc->sc_ndata.keycode[i] =
632 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
636 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
637 for (i = 0; i < UKBD_NKEYCODE; i++) {
638 sc->sc_ndata.keycode[i] =
639 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
645 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
646 if (ukbd_any_key_pressed(sc)) {
647 ukbd_start_timer(sc);
653 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
654 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
655 usbd_transfer_submit(xfer);
657 DPRINTF("input queue is full!\n");
662 DPRINTF("error=%s\n", usbd_errstr(error));
664 if (error != USB_ERR_CANCELLED) {
665 /* try to clear stall first */
666 usbd_xfer_set_stall(xfer);
674 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
676 struct usb_device_request req;
677 struct usb_page_cache *pc;
679 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
686 switch (USB_GET_STATE(xfer)) {
687 case USB_ST_TRANSFERRED:
689 if (sc->sc_flags & UKBD_FLAG_SET_LEDS) {
690 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
692 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
693 req.bRequest = UR_SET_REPORT;
694 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
695 req.wIndex[0] = sc->sc_iface_no;
699 /* check if we need to prefix an ID byte */
700 if (sc->sc_led_id != 0) {
702 buf[0] = sc->sc_led_id;
703 buf[1] = sc->sc_leds;
706 buf[0] = sc->sc_leds;
710 pc = usbd_xfer_get_frame(xfer, 0);
711 usbd_copy_in(pc, 0, &req, sizeof(req));
712 pc = usbd_xfer_get_frame(xfer, 1);
713 usbd_copy_in(pc, 0, buf, sizeof(buf));
715 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
716 usbd_xfer_set_frame_len(xfer, 1, req.wLength[0]);
717 usbd_xfer_set_frames(xfer, 2);
718 usbd_transfer_submit(xfer);
723 DPRINTFN(0, "error=%s\n", usbd_errstr(error));
728 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
731 .type = UE_INTERRUPT,
732 .endpoint = UE_ADDR_ANY,
733 .direction = UE_DIR_IN,
734 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
735 .bufsize = 0, /* use wMaxPacketSize */
736 .callback = &ukbd_intr_callback,
741 .endpoint = 0x00, /* Control pipe */
742 .direction = UE_DIR_ANY,
743 .bufsize = sizeof(struct usb_device_request) + 8,
744 .callback = &ukbd_set_leds_callback,
745 .timeout = 1000, /* 1 second */
750 ukbd_probe(device_t dev)
752 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
753 struct usb_attach_arg *uaa = device_get_ivars(dev);
763 if (uaa->usb_mode != USB_MODE_HOST) {
767 if (uaa->info.bInterfaceClass != UICLASS_HID)
770 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
771 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) {
772 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
775 return (BUS_PROBE_GENERIC);
778 error = usbd_req_get_hid_desc(uaa->device, NULL,
779 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
785 * NOTE: we currently don't support USB mouse and USB keyboard
786 * on the same USB endpoint.
788 if (hid_is_collection(d_ptr, d_len,
789 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
790 /* most likely a mouse */
792 } else if (hid_is_collection(d_ptr, d_len,
793 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
794 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
797 error = BUS_PROBE_GENERIC;
806 ukbd_attach(device_t dev)
808 struct ukbd_softc *sc = device_get_softc(dev);
809 struct usb_attach_arg *uaa = device_get_ivars(dev);
810 int32_t unit = device_get_unit(dev);
811 keyboard_t *kbd = &sc->sc_kbd;
812 void *hid_ptr = NULL;
818 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
820 kbd->kb_data = (void *)sc;
822 device_set_usb_desc(dev);
824 sc->sc_udev = uaa->device;
825 sc->sc_iface = uaa->iface;
826 sc->sc_iface_index = uaa->info.bIfaceIndex;
827 sc->sc_iface_no = uaa->info.bIfaceNum;
828 sc->sc_mode = K_XLATE;
830 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
832 err = usbd_transfer_setup(uaa->device,
833 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
834 UKBD_N_TRANSFER, sc, &Giant);
837 DPRINTF("error=%s\n", usbd_errstr(err));
840 /* setup default keyboard maps */
842 sc->sc_keymap = key_map;
843 sc->sc_accmap = accent_map;
844 for (n = 0; n < UKBD_NFKEY; n++) {
845 sc->sc_fkeymap[n] = fkey_tab[n];
848 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
849 sc->sc_fkeymap, UKBD_NFKEY);
851 KBD_FOUND_DEVICE(kbd);
853 ukbd_clear_state(kbd);
856 * FIXME: set the initial value for lock keys in "sc_state"
857 * according to the BIOS data?
862 * Set boot protocol if we need the quirk.
864 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO)) {
865 err = usbd_req_set_protocol(sc->sc_udev, NULL,
866 sc->sc_iface_index, 0);
867 if (err != USB_ERR_NORMAL_COMPLETION) {
868 DPRINTF("set protocol error=%s\n", usbd_errstr(err));
873 /* figure out if there is an ID byte in the data */
874 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
875 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
877 uint8_t apple_keys = 0;
880 /* investigate if this is an Apple Keyboard */
881 if (hid_locate(hid_ptr, hid_len,
882 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
883 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
885 if (flags & HIO_VARIABLE)
886 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
887 UKBD_FLAG_APPLE_SWAP;
888 DPRINTFN(1, "Found Apple eject-key\n");
890 sc->sc_kbd_id = temp_id;
892 if (hid_locate(hid_ptr, hid_len,
893 HID_USAGE2(0xFFFF, 0x0003),
894 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
896 if (flags & HIO_VARIABLE)
897 sc->sc_flags |= UKBD_FLAG_APPLE_FN;
898 DPRINTFN(1, "Found Apple FN-key\n");
900 sc->sc_kbd_id = temp_id;
902 if (apple_keys == 0) {
904 * Assume the first HID ID contains the
907 hid_report_size(hid_ptr, hid_len,
908 hid_input, &sc->sc_kbd_id);
911 /* investigate if we need an ID-byte for the leds */
912 hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id);
914 free(hid_ptr, M_TEMP);
917 /* ignore if SETIDLE fails, hence it is not crucial */
918 err = usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
922 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
928 if (kbd_register(kbd) < 0) {
931 KBD_CONFIG_DONE(kbd);
935 #ifdef KBD_INSTALL_CDEV
936 if (kbd_attach(kbd)) {
940 sc->sc_flags |= UKBD_FLAG_ATTACHED;
943 genkbd_diag(kbd, bootverbose);
945 /* lock keyboard mutex */
949 /* start the keyboard */
951 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
954 return (0); /* success */
958 return (ENXIO); /* error */
962 ukbd_detach(device_t dev)
964 struct ukbd_softc *sc = device_get_softc(dev);
971 sc->sc_flags |= UKBD_FLAG_GONE;
973 usb_callout_stop(&sc->sc_callout);
975 ukbd_disable(&sc->sc_kbd);
977 #ifdef KBD_INSTALL_CDEV
978 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
979 error = kbd_detach(&sc->sc_kbd);
981 /* usb attach cannot return an error */
982 device_printf(dev, "WARNING: kbd_detach() "
983 "returned non-zero! (ignored)\n");
987 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
988 error = kbd_unregister(&sc->sc_kbd);
990 /* usb attach cannot return an error */
991 device_printf(dev, "WARNING: kbd_unregister() "
992 "returned non-zero! (ignored)\n");
995 sc->sc_kbd.kb_flags = 0;
999 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1001 usb_callout_drain(&sc->sc_callout);
1003 DPRINTF("%s: disconnected\n",
1004 device_get_nameunit(dev));
1010 ukbd_resume(device_t dev)
1012 struct ukbd_softc *sc = device_get_softc(dev);
1016 ukbd_clear_state(&sc->sc_kbd);
1023 /* early keyboard probe, not supported */
1025 ukbd_configure(int flags)
1030 /* detect a keyboard, not used */
1032 ukbd__probe(int unit, void *arg, int flags)
1037 /* reset and initialize the device, not used */
1039 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1044 /* test the interface to the device, not used */
1046 ukbd_test_if(keyboard_t *kbd)
1051 /* finish using this keyboard, not used */
1053 ukbd_term(keyboard_t *kbd)
1058 /* keyboard interrupt routine, not used */
1060 ukbd_intr(keyboard_t *kbd, void *arg)
1065 /* lock the access to the keyboard, not used */
1067 ukbd_lock(keyboard_t *kbd, int lock)
1073 * Enable the access to the device; until this function is called,
1074 * the client cannot read from the keyboard.
1077 ukbd_enable(keyboard_t *kbd)
1079 if (!mtx_owned(&Giant)) {
1083 retval = ukbd_enable(kbd);
1091 /* disallow the access to the device */
1093 ukbd_disable(keyboard_t *kbd)
1095 if (!mtx_owned(&Giant)) {
1099 retval = ukbd_disable(kbd);
1103 KBD_DEACTIVATE(kbd);
1107 /* check if data is waiting */
1109 ukbd_check(keyboard_t *kbd)
1111 struct ukbd_softc *sc = kbd->kb_data;
1113 if (!KBD_IS_ACTIVE(kbd))
1116 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1117 if (!mtx_owned(&Giant)) {
1121 retval = ukbd_check(kbd);
1126 /* XXX the keyboard layer requires Giant */
1127 if (!mtx_owned(&Giant))
1131 /* check if key belongs to this thread */
1132 if (ukbd_polls_other_thread(sc))
1135 if (sc->sc_flags & UKBD_FLAG_POLLING)
1136 ukbd_do_poll(sc, 0);
1138 #ifdef UKBD_EMULATE_ATSCANCODE
1139 if (sc->sc_buffered_char[0]) {
1143 if (sc->sc_inputs > 0) {
1149 /* check if char is waiting */
1151 ukbd_check_char(keyboard_t *kbd)
1153 struct ukbd_softc *sc = kbd->kb_data;
1155 if (!KBD_IS_ACTIVE(kbd))
1158 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1159 if (!mtx_owned(&Giant)) {
1163 retval = ukbd_check_char(kbd);
1168 /* XXX the keyboard layer requires Giant */
1169 if (!mtx_owned(&Giant))
1173 /* check if key belongs to this thread */
1174 if (ukbd_polls_other_thread(sc))
1177 if ((sc->sc_composed_char > 0) &&
1178 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1181 return (ukbd_check(kbd));
1185 /* read one byte from the keyboard if it's allowed */
1187 ukbd_read(keyboard_t *kbd, int wait)
1189 struct ukbd_softc *sc = kbd->kb_data;
1192 #ifdef UKBD_EMULATE_ATSCANCODE
1197 if (!KBD_IS_ACTIVE(kbd))
1200 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1201 if (!mtx_owned(&Giant)) {
1205 retval = ukbd_read(kbd, wait);
1210 /* XXX the keyboard layer requires Giant */
1211 if (!mtx_owned(&Giant))
1215 /* check if key belongs to this thread */
1216 if (ukbd_polls_other_thread(sc))
1219 #ifdef UKBD_EMULATE_ATSCANCODE
1220 if (sc->sc_buffered_char[0]) {
1221 scancode = sc->sc_buffered_char[0];
1222 if (scancode & SCAN_PREFIX) {
1223 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1224 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1226 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1227 sc->sc_buffered_char[1] = 0;
1230 #endif /* UKBD_EMULATE_ATSCANCODE */
1233 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1234 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1239 #ifdef UKBD_EMULATE_ATSCANCODE
1240 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1241 if (keycode == NN) {
1244 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1245 (usbcode & KEY_RELEASE)));
1246 #else /* !UKBD_EMULATE_ATSCANCODE */
1248 #endif /* UKBD_EMULATE_ATSCANCODE */
1251 /* read char from the keyboard */
1253 ukbd_read_char(keyboard_t *kbd, int wait)
1255 struct ukbd_softc *sc = kbd->kb_data;
1260 #ifdef UKBD_EMULATE_ATSCANCODE
1265 if (!KBD_IS_ACTIVE(kbd))
1268 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1269 if (!mtx_owned(&Giant)) {
1273 retval = ukbd_read_char(kbd, wait);
1278 /* XXX the keyboard layer requires Giant */
1279 if (!mtx_owned(&Giant))
1283 /* check if key belongs to this thread */
1284 if (ukbd_polls_other_thread(sc))
1289 /* do we have a composed char to return ? */
1291 if ((sc->sc_composed_char > 0) &&
1292 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1294 action = sc->sc_composed_char;
1295 sc->sc_composed_char = 0;
1297 if (action > 0xFF) {
1302 #ifdef UKBD_EMULATE_ATSCANCODE
1304 /* do we have a pending raw scan code? */
1306 if (sc->sc_mode == K_RAW) {
1307 scancode = sc->sc_buffered_char[0];
1309 if (scancode & SCAN_PREFIX) {
1310 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1311 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1313 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1314 sc->sc_buffered_char[1] = 0;
1318 #endif /* UKBD_EMULATE_ATSCANCODE */
1320 /* see if there is something in the keyboard port */
1322 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1323 if (usbcode == -1) {
1328 #ifdef UKBD_EMULATE_ATSCANCODE
1329 /* USB key index -> key code -> AT scan code */
1330 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1331 if (keycode == NN) {
1334 /* return an AT scan code for the K_RAW mode */
1335 if (sc->sc_mode == K_RAW) {
1336 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1337 (usbcode & KEY_RELEASE)));
1339 #else /* !UKBD_EMULATE_ATSCANCODE */
1341 /* return the byte as is for the K_RAW mode */
1342 if (sc->sc_mode == K_RAW) {
1345 /* USB key index -> key code */
1346 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1347 if (keycode == NN) {
1350 #endif /* UKBD_EMULATE_ATSCANCODE */
1353 case 0x38: /* left alt (compose key) */
1354 if (usbcode & KEY_RELEASE) {
1355 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1356 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1358 if (sc->sc_composed_char > 0xFF) {
1359 sc->sc_composed_char = 0;
1363 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1364 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1365 sc->sc_composed_char = 0;
1369 /* XXX: I don't like these... */
1370 case 0x5c: /* print screen */
1371 if (sc->sc_flags & ALTS) {
1372 keycode = 0x54; /* sysrq */
1375 case 0x68: /* pause/break */
1376 if (sc->sc_flags & CTLS) {
1377 keycode = 0x6c; /* break */
1382 /* return the key code in the K_CODE mode */
1383 if (usbcode & KEY_RELEASE) {
1384 keycode |= SCAN_RELEASE;
1386 if (sc->sc_mode == K_CODE) {
1389 /* compose a character code */
1390 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1392 /* key pressed, process it */
1395 case 0x49: /* keypad 7,8,9 */
1396 sc->sc_composed_char *= 10;
1397 sc->sc_composed_char += keycode - 0x40;
1398 goto check_composed;
1402 case 0x4D: /* keypad 4,5,6 */
1403 sc->sc_composed_char *= 10;
1404 sc->sc_composed_char += keycode - 0x47;
1405 goto check_composed;
1409 case 0x51: /* keypad 1,2,3 */
1410 sc->sc_composed_char *= 10;
1411 sc->sc_composed_char += keycode - 0x4E;
1412 goto check_composed;
1414 case 0x52: /* keypad 0 */
1415 sc->sc_composed_char *= 10;
1416 goto check_composed;
1418 /* key released, no interest here */
1419 case SCAN_RELEASE | 0x47:
1420 case SCAN_RELEASE | 0x48:
1421 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1422 case SCAN_RELEASE | 0x4B:
1423 case SCAN_RELEASE | 0x4C:
1424 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1425 case SCAN_RELEASE | 0x4F:
1426 case SCAN_RELEASE | 0x50:
1427 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1428 case SCAN_RELEASE | 0x52: /* keypad 0 */
1431 case 0x38: /* left alt key */
1435 if (sc->sc_composed_char > 0) {
1436 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1437 sc->sc_composed_char = 0;
1443 /* keycode to key action */
1444 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1445 (keycode & SCAN_RELEASE),
1446 &sc->sc_state, &sc->sc_accents);
1447 if (action == NOKEY) {
1454 if (sc->sc_composed_char <= 0xFF) {
1461 /* some useful control functions */
1463 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1465 /* translate LED_XXX bits into the device specific bits */
1466 static const uint8_t ledmap[8] = {
1467 0, 2, 1, 3, 4, 6, 5, 7,
1469 struct ukbd_softc *sc = kbd->kb_data;
1472 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1473 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1477 if (!mtx_owned(&Giant)) {
1479 * XXX big problem: If scroll lock is pressed and "printf()"
1480 * is called, the CPU will get here, to un-scroll lock the
1481 * keyboard. But if "printf()" acquires the "Giant" lock,
1482 * there will be a locking order reversal problem, so the
1483 * keyboard system must get out of "Giant" first, before the
1484 * CPU can proceed here ...
1489 /* workaround for Geli */
1491 i = ukbd_ioctl(kbd, cmd, arg);
1500 case KDGKBMODE: /* get keyboard mode */
1501 *(int *)arg = sc->sc_mode;
1503 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1504 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1506 ival = IOCPARM_IVAL(arg);
1507 arg = (caddr_t)&ival;
1510 case KDSKBMODE: /* set keyboard mode */
1511 switch (*(int *)arg) {
1513 if (sc->sc_mode != K_XLATE) {
1514 /* make lock key state and LED state match */
1515 sc->sc_state &= ~LOCK_MASK;
1516 sc->sc_state |= KBD_LED_VAL(kbd);
1521 if (sc->sc_mode != *(int *)arg) {
1522 if (ukbd_is_polling(sc) == 0)
1523 ukbd_clear_state(kbd);
1524 sc->sc_mode = *(int *)arg;
1532 case KDGETLED: /* get keyboard LED */
1533 *(int *)arg = KBD_LED_VAL(kbd);
1535 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1536 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1538 ival = IOCPARM_IVAL(arg);
1539 arg = (caddr_t)&ival;
1542 case KDSETLED: /* set keyboard LED */
1543 /* NOTE: lock key state in "sc_state" won't be changed */
1544 if (*(int *)arg & ~LOCK_MASK) {
1548 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1549 if (sc->sc_mode == K_XLATE &&
1550 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1556 if (KBD_HAS_DEVICE(kbd)) {
1557 ukbd_set_leds(sc, ledmap[i & LED_MASK]);
1559 KBD_LED_VAL(kbd) = *(int *)arg;
1561 case KDGKBSTATE: /* get lock key state */
1562 *(int *)arg = sc->sc_state & LOCK_MASK;
1564 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1565 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1567 ival = IOCPARM_IVAL(arg);
1568 arg = (caddr_t)&ival;
1571 case KDSKBSTATE: /* set lock key state */
1572 if (*(int *)arg & ~LOCK_MASK) {
1575 sc->sc_state &= ~LOCK_MASK;
1576 sc->sc_state |= *(int *)arg;
1578 /* set LEDs and quit */
1579 return (ukbd_ioctl(kbd, KDSETLED, arg));
1581 case KDSETREPEAT: /* set keyboard repeat rate (new
1583 if (!KBD_HAS_DEVICE(kbd)) {
1586 if (((int *)arg)[1] < 0) {
1589 if (((int *)arg)[0] < 0) {
1592 if (((int *)arg)[0] < 200) /* fastest possible value */
1593 kbd->kb_delay1 = 200;
1595 kbd->kb_delay1 = ((int *)arg)[0];
1596 kbd->kb_delay2 = ((int *)arg)[1];
1599 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1600 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1602 ival = IOCPARM_IVAL(arg);
1603 arg = (caddr_t)&ival;
1606 case KDSETRAD: /* set keyboard repeat rate (old
1608 return (ukbd_set_typematic(kbd, *(int *)arg));
1610 case PIO_KEYMAP: /* set keyboard translation table */
1611 case PIO_KEYMAPENT: /* set keyboard translation table
1613 case PIO_DEADKEYMAP: /* set accent key translation table */
1617 return (genkbd_commonioctl(kbd, cmd, arg));
1623 /* clear the internal state of the keyboard */
1625 ukbd_clear_state(keyboard_t *kbd)
1627 struct ukbd_softc *sc = kbd->kb_data;
1629 if (!mtx_owned(&Giant)) {
1632 ukbd_clear_state(kbd);
1637 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1638 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1640 sc->sc_composed_char = 0;
1641 #ifdef UKBD_EMULATE_ATSCANCODE
1642 sc->sc_buffered_char[0] = 0;
1643 sc->sc_buffered_char[1] = 0;
1645 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1646 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
1647 memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
1648 memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
1651 /* save the internal state, not used */
1653 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1655 return (len == 0) ? 1 : -1;
1658 /* set the internal state, not used */
1660 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1666 ukbd_is_polling(struct ukbd_softc *sc)
1670 if (sc->sc_flags & UKBD_FLAG_POLLING)
1671 return (1); /* polling */
1673 delta = ticks - sc->sc_poll_tick_last;
1674 if ((delta < 0) || (delta >= hz)) {
1675 sc->sc_poll_detected = 0;
1676 return (0); /* not polling */
1679 return (sc->sc_poll_detected);
1683 ukbd_polls_other_thread(struct ukbd_softc *sc)
1685 return (ukbd_is_polling(sc) &&
1686 (sc->sc_poll_thread != curthread));
1690 ukbd_poll(keyboard_t *kbd, int on)
1692 struct ukbd_softc *sc = kbd->kb_data;
1694 if (!mtx_owned(&Giant)) {
1698 retval = ukbd_poll(kbd, on);
1704 sc->sc_flags |= UKBD_FLAG_POLLING;
1705 sc->sc_poll_thread = curthread;
1707 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1708 ukbd_start_timer(sc); /* start timer */
1713 /* local functions */
1716 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1718 DPRINTF("leds=0x%02x\n", leds);
1721 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1723 /* start transfer, if not already started */
1725 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1729 ukbd_set_typematic(keyboard_t *kbd, int code)
1731 static const int delays[] = {250, 500, 750, 1000};
1732 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1733 68, 76, 84, 92, 100, 110, 118, 126,
1734 136, 152, 168, 184, 200, 220, 236, 252,
1735 272, 304, 336, 368, 400, 440, 472, 504};
1740 kbd->kb_delay1 = delays[(code >> 5) & 3];
1741 kbd->kb_delay2 = rates[code & 0x1f];
1745 #ifdef UKBD_EMULATE_ATSCANCODE
1747 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1749 static const int scan[] = {
1755 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1765 0x151, /* PageDown */
1768 0x146, /* XXX Pause/Break */
1769 0x15b, /* Win_L(Super_L) */
1770 0x15c, /* Win_R(Super_R) */
1771 0x15d, /* Application(Menu) */
1773 /* SUN TYPE 6 USB KEYBOARD */
1774 0x168, /* Sun Type 6 Help */
1775 0x15e, /* Sun Type 6 Stop */
1777 0x15f, /* Sun Type 6 Again */
1778 0x160, /* Sun Type 6 Props */
1779 0x161, /* Sun Type 6 Undo */
1780 0x162, /* Sun Type 6 Front */
1781 0x163, /* Sun Type 6 Copy */
1782 0x164, /* Sun Type 6 Open */
1783 0x165, /* Sun Type 6 Paste */
1784 0x166, /* Sun Type 6 Find */
1785 0x167, /* Sun Type 6 Cut */
1786 0x125, /* Sun Type 6 Mute */
1788 0x11f, /* Sun Type 6 VolumeDown */
1789 0x11e, /* Sun Type 6 VolumeUp */
1790 0x120, /* Sun Type 6 PowerDown */
1792 /* Japanese 106/109 keyboard */
1793 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
1794 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
1795 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
1796 0x79, /* Keyboard Intl' 4 (Henkan) */
1797 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
1798 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
1801 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
1802 code = scan[code - 89];
1805 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
1806 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
1808 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
1809 code &= ~SCAN_PREFIX_SHIFT;
1811 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1813 if (code & SCAN_PREFIX) {
1814 if (code & SCAN_PREFIX_CTL) {
1816 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
1817 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
1818 } else if (code & SCAN_PREFIX_SHIFT) {
1820 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1821 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1823 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1824 sc->sc_buffered_char[1] = 0;
1826 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1832 #endif /* UKBD_EMULATE_ATSCANCODE */
1834 static keyboard_switch_t ukbdsw = {
1835 .probe = &ukbd__probe,
1839 .test_if = &ukbd_test_if,
1840 .enable = &ukbd_enable,
1841 .disable = &ukbd_disable,
1843 .check = &ukbd_check,
1844 .read_char = &ukbd_read_char,
1845 .check_char = &ukbd_check_char,
1846 .ioctl = &ukbd_ioctl,
1848 .clear_state = &ukbd_clear_state,
1849 .get_state = &ukbd_get_state,
1850 .set_state = &ukbd_set_state,
1851 .get_fkeystr = &genkbd_get_fkeystr,
1853 .diag = &genkbd_diag,
1856 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
1859 ukbd_driver_load(module_t mod, int what, void *arg)
1863 kbd_add_driver(&ukbd_kbd_driver);
1866 kbd_delete_driver(&ukbd_kbd_driver);
1872 static devclass_t ukbd_devclass;
1874 static device_method_t ukbd_methods[] = {
1875 DEVMETHOD(device_probe, ukbd_probe),
1876 DEVMETHOD(device_attach, ukbd_attach),
1877 DEVMETHOD(device_detach, ukbd_detach),
1878 DEVMETHOD(device_resume, ukbd_resume),
1882 static driver_t ukbd_driver = {
1884 .methods = ukbd_methods,
1885 .size = sizeof(struct ukbd_softc),
1888 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
1889 MODULE_DEPEND(ukbd, usb, 1, 1, 1);