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.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
44 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
47 #include "opt_compat.h"
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/linker_set.h>
60 #include <sys/module.h>
62 #include <sys/mutex.h>
63 #include <sys/condvar.h>
64 #include <sys/sysctl.h>
66 #include <sys/unistd.h>
67 #include <sys/callout.h>
68 #include <sys/malloc.h>
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/usbhid.h>
77 #define USB_DEBUG_VAR ukbd_debug
78 #include <dev/usb/usb_debug.h>
80 #include <dev/usb/quirk/usb_quirk.h>
82 #include <sys/ioccom.h>
83 #include <sys/filio.h>
87 #include <dev/kbd/kbdreg.h>
89 /* the initial key map, accent map and fkey strings */
90 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
91 #define KBD_DFLT_KEYMAP
95 /* the following file must be included after "ukbdmap.h" */
96 #include <dev/kbd/kbdtables.h>
99 static int ukbd_debug = 0;
100 static int ukbd_no_leds = 0;
102 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
103 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
104 &ukbd_debug, 0, "Debug level");
105 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW,
106 &ukbd_no_leds, 0, "Disables setting of keyboard leds");
108 TUNABLE_INT("hw.usb.ukbd.debug", &ukbd_debug);
109 TUNABLE_INT("hw.usb.ukbd.no_leds", &ukbd_no_leds);
112 #define UKBD_EMULATE_ATSCANCODE 1
113 #define UKBD_DRIVER_NAME "ukbd"
114 #define UKBD_NMOD 8 /* units */
115 #define UKBD_NKEYCODE 6 /* units */
116 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */
117 #define UKBD_IN_BUF_FULL (UKBD_IN_BUF_SIZE / 2) /* bytes */
118 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
122 #define MOD_CONTROL_L 0x01
123 #define MOD_CONTROL_R 0x10
124 #define MOD_SHIFT_L 0x02
125 #define MOD_SHIFT_R 0x20
126 #define MOD_ALT_L 0x04
127 #define MOD_ALT_R 0x40
128 #define MOD_WIN_L 0x08
129 #define MOD_WIN_R 0x80
131 uint8_t keycode[UKBD_NKEYCODE];
144 accentmap_t sc_accmap;
145 fkeytab_t sc_fkeymap[UKBD_NFKEY];
146 struct hid_location sc_loc_apple_eject;
147 struct hid_location sc_loc_apple_fn;
148 struct usb_callout sc_callout;
149 struct ukbd_data sc_ndata;
150 struct ukbd_data sc_odata;
152 struct thread *sc_poll_thread;
153 struct usb_device *sc_udev;
154 struct usb_interface *sc_iface;
155 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
157 uint32_t sc_ntime[UKBD_NKEYCODE];
158 uint32_t sc_otime[UKBD_NKEYCODE];
159 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
161 uint32_t sc_composed_char; /* composed char code, if non-zero */
162 #ifdef UKBD_EMULATE_ATSCANCODE
163 uint32_t sc_buffered_char[2];
165 uint32_t sc_flags; /* flags */
166 #define UKBD_FLAG_COMPOSE 0x0001
167 #define UKBD_FLAG_POLLING 0x0002
168 #define UKBD_FLAG_SET_LEDS 0x0004
169 #define UKBD_FLAG_ATTACHED 0x0010
170 #define UKBD_FLAG_GONE 0x0020
171 #define UKBD_FLAG_APPLE_EJECT 0x0040
172 #define UKBD_FLAG_APPLE_FN 0x0080
173 #define UKBD_FLAG_APPLE_SWAP 0x0100
174 #define UKBD_FLAG_TIMER_RUNNING 0x0200
176 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
177 int sc_state; /* shift/lock key state */
178 int sc_accents; /* accent key index (> 0) */
179 int sc_poll_tick_last;
182 uint16_t sc_inputhead;
183 uint16_t sc_inputtail;
185 uint8_t sc_leds; /* store for async led requests */
186 uint8_t sc_iface_index;
190 uint8_t sc_poll_detected;
193 #define KEY_ERROR 0x01
196 #define KEY_RELEASE 0x400
197 #define KEY_INDEX(c) ((c) & 0xFF)
200 #define SCAN_RELEASE 0x80
201 #define SCAN_PREFIX_E0 0x100
202 #define SCAN_PREFIX_E1 0x200
203 #define SCAN_PREFIX_CTL 0x400
204 #define SCAN_PREFIX_SHIFT 0x800
205 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
206 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
207 #define SCAN_CHAR(c) ((c) & 0x7f)
213 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
214 {MOD_CONTROL_L, 0xe0},
215 {MOD_CONTROL_R, 0xe4},
224 #define NN 0 /* no translation */
226 * Translate USB keycodes to AT keyboard scancodes.
229 * FIXME: Mac USB keyboard generates:
230 * 0x53: keypad NumLock/Clear
237 static const uint8_t ukbd_trtab[256] = {
238 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
239 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
240 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
241 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
242 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
243 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
244 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
245 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
246 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
247 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
248 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
249 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
250 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
251 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
252 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
253 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
254 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */
255 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */
256 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
257 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
258 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
259 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
260 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
261 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
262 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
263 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
264 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
265 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
266 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
267 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
268 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
269 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
273 static void ukbd_timeout(void *);
274 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
275 static int ukbd_set_typematic(keyboard_t *, int);
276 #ifdef UKBD_EMULATE_ATSCANCODE
277 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
279 static uint32_t ukbd_read_char(keyboard_t *, int);
280 static void ukbd_clear_state(keyboard_t *);
281 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
282 static int ukbd_enable(keyboard_t *);
283 static int ukbd_disable(keyboard_t *);
284 static void ukbd_interrupt(struct ukbd_softc *);
285 static int ukbd_is_polling(struct ukbd_softc *);
286 static int ukbd_polls_other_thread(struct ukbd_softc *);
287 static void ukbd_event_keyinput(struct ukbd_softc *);
289 static device_probe_t ukbd_probe;
290 static device_attach_t ukbd_attach;
291 static device_detach_t ukbd_detach;
292 static device_resume_t ukbd_resume;
295 ukbd_any_key_pressed(struct ukbd_softc *sc)
300 for (j = i = 0; i < UKBD_NKEYCODE; i++)
301 j |= sc->sc_odata.keycode[i];
307 ukbd_start_timer(struct ukbd_softc *sc)
309 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
310 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
314 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
316 mtx_assert(&Giant, MA_OWNED);
318 DPRINTF("0x%02x (%d) %s\n", key, key,
319 (key & KEY_RELEASE) ? "released" : "pressed");
321 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
322 sc->sc_input[sc->sc_inputtail] = key;
324 ++(sc->sc_inputtail);
325 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
326 sc->sc_inputtail = 0;
329 DPRINTF("input buffer is full\n");
334 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
336 DPRINTFN(2, "polling\n");
338 /* update stats about last polling event */
339 sc->sc_poll_tick_last = ticks;
340 sc->sc_poll_detected = 1;
342 if (kdb_active == 0) {
343 while (sc->sc_inputs == 0) {
344 /* make sure the USB code gets a chance to run */
347 /* check if we should wait */
351 return; /* Only poll if KDB is active */
354 while (sc->sc_inputs == 0) {
356 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
358 /* Delay-optimised support for repetition of keys */
360 if (ukbd_any_key_pressed(sc)) {
361 /* a key is pressed - need timekeeping */
364 /* 1 millisecond has passed */
376 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
380 mtx_assert(&Giant, MA_OWNED);
382 if (sc->sc_inputs == 0) {
383 /* start transfer, if not already started */
384 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
387 if (ukbd_polls_other_thread(sc))
390 if (sc->sc_flags & UKBD_FLAG_POLLING)
391 ukbd_do_poll(sc, wait);
393 if (sc->sc_inputs == 0) {
396 c = sc->sc_input[sc->sc_inputhead];
398 ++(sc->sc_inputhead);
399 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
400 sc->sc_inputhead = 0;
407 ukbd_interrupt(struct ukbd_softc *sc)
411 uint32_t now = sc->sc_time_ms;
417 if (sc->sc_ndata.keycode[0] == KEY_ERROR)
420 n_mod = sc->sc_ndata.modifiers;
421 o_mod = sc->sc_odata.modifiers;
422 if (n_mod != o_mod) {
423 for (i = 0; i < UKBD_NMOD; i++) {
424 if ((n_mod & ukbd_mods[i].mask) !=
425 (o_mod & ukbd_mods[i].mask)) {
426 ukbd_put_key(sc, ukbd_mods[i].key |
427 ((n_mod & ukbd_mods[i].mask) ?
428 KEY_PRESS : KEY_RELEASE));
432 /* Check for released keys. */
433 for (i = 0; i < UKBD_NKEYCODE; i++) {
434 key = sc->sc_odata.keycode[i];
438 for (j = 0; j < UKBD_NKEYCODE; j++) {
439 if (sc->sc_ndata.keycode[j] == 0) {
442 if (key == sc->sc_ndata.keycode[j]) {
446 ukbd_put_key(sc, key | KEY_RELEASE);
450 /* Check for pressed keys. */
451 for (i = 0; i < UKBD_NKEYCODE; i++) {
452 key = sc->sc_ndata.keycode[i];
456 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
457 for (j = 0; j < UKBD_NKEYCODE; j++) {
458 if (sc->sc_odata.keycode[j] == 0) {
461 if (key == sc->sc_odata.keycode[j]) {
463 /* key is still pressed */
465 sc->sc_ntime[i] = sc->sc_otime[j];
466 dtime = (sc->sc_otime[j] - now);
468 if (!(dtime & 0x80000000)) {
469 /* time has not elapsed */
472 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
476 ukbd_put_key(sc, key | KEY_PRESS);
479 * If any other key is presently down, force its repeat to be
480 * well in the future (100s). This makes the last key to be
481 * pressed do the autorepeat.
483 for (j = 0; j != UKBD_NKEYCODE; j++) {
485 sc->sc_ntime[j] = now + (100 * 1000);
490 sc->sc_odata = sc->sc_ndata;
492 memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
494 ukbd_event_keyinput(sc);
498 ukbd_event_keyinput(struct ukbd_softc *sc)
502 if (ukbd_is_polling(sc))
505 if (sc->sc_inputs == 0)
508 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
509 KBD_IS_BUSY(&sc->sc_kbd)) {
510 /* let the callback function process the input */
511 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
512 sc->sc_kbd.kb_callback.kc_arg);
514 /* read and discard the input, no one is waiting for it */
516 c = ukbd_read_char(&sc->sc_kbd, 0);
517 } while (c != NOKEY);
522 ukbd_timeout(void *arg)
524 struct ukbd_softc *sc = arg;
526 mtx_assert(&Giant, MA_OWNED);
528 sc->sc_time_ms += 25; /* milliseconds */
532 /* Make sure any leftover key events gets read out */
533 ukbd_event_keyinput(sc);
535 if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
536 ukbd_start_timer(sc);
538 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
543 ukbd_apple_fn(uint8_t keycode) {
545 case 0x28: return 0x49; /* RETURN -> INSERT */
546 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
547 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
548 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
549 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
550 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
551 default: return keycode;
556 ukbd_apple_swap(uint8_t keycode) {
558 case 0x35: return 0x64;
559 case 0x64: return 0x35;
560 default: return keycode;
565 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
567 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
568 struct usb_page_cache *pc;
576 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
577 pc = usbd_xfer_get_frame(xfer, 0);
579 switch (USB_GET_STATE(xfer)) {
580 case USB_ST_TRANSFERRED:
581 DPRINTF("actlen=%d bytes\n", len);
584 DPRINTF("zero length data\n");
588 if (sc->sc_kbd_id != 0) {
589 /* check and remove HID ID byte */
590 usbd_copy_out(pc, 0, &id, 1);
591 if (id != sc->sc_kbd_id) {
592 DPRINTF("wrong HID ID\n");
601 if (len > sizeof(sc->sc_ndata)) {
602 len = sizeof(sc->sc_ndata);
606 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
607 usbd_copy_out(pc, offset, &sc->sc_ndata, len);
609 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
610 hid_get_data((uint8_t *)&sc->sc_ndata,
611 len, &sc->sc_loc_apple_eject))
616 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
617 hid_get_data((uint8_t *)&sc->sc_ndata,
618 len, &sc->sc_loc_apple_fn))
623 DPRINTF("apple_eject=%u apple_fn=%u\n",
624 apple_eject, apple_fn);
626 if (sc->sc_ndata.modifiers) {
627 DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers);
629 for (i = 0; i < UKBD_NKEYCODE; i++) {
630 if (sc->sc_ndata.keycode[i]) {
631 DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]);
634 #endif /* USB_DEBUG */
637 for (i = 0; i < UKBD_NKEYCODE; i++) {
638 sc->sc_ndata.keycode[i] =
639 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
643 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
644 for (i = 0; i < UKBD_NKEYCODE; i++) {
645 sc->sc_ndata.keycode[i] =
646 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
652 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
653 if (ukbd_any_key_pressed(sc)) {
654 ukbd_start_timer(sc);
660 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
661 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
662 usbd_transfer_submit(xfer);
664 DPRINTF("input queue is full!\n");
669 DPRINTF("error=%s\n", usbd_errstr(error));
671 if (error != USB_ERR_CANCELLED) {
672 /* try to clear stall first */
673 usbd_xfer_set_stall(xfer);
681 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
683 struct usb_device_request req;
684 struct usb_page_cache *pc;
686 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
693 switch (USB_GET_STATE(xfer)) {
694 case USB_ST_TRANSFERRED:
696 if (sc->sc_flags & UKBD_FLAG_SET_LEDS) {
697 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
699 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
700 req.bRequest = UR_SET_REPORT;
701 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
702 req.wIndex[0] = sc->sc_iface_no;
706 /* check if we need to prefix an ID byte */
707 if (sc->sc_led_id != 0) {
709 buf[0] = sc->sc_led_id;
710 buf[1] = sc->sc_leds;
713 buf[0] = sc->sc_leds;
717 pc = usbd_xfer_get_frame(xfer, 0);
718 usbd_copy_in(pc, 0, &req, sizeof(req));
719 pc = usbd_xfer_get_frame(xfer, 1);
720 usbd_copy_in(pc, 0, buf, sizeof(buf));
722 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
723 usbd_xfer_set_frame_len(xfer, 1, req.wLength[0]);
724 usbd_xfer_set_frames(xfer, 2);
725 usbd_transfer_submit(xfer);
730 DPRINTFN(1, "error=%s\n", usbd_errstr(error));
735 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
738 .type = UE_INTERRUPT,
739 .endpoint = UE_ADDR_ANY,
740 .direction = UE_DIR_IN,
741 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
742 .bufsize = 0, /* use wMaxPacketSize */
743 .callback = &ukbd_intr_callback,
748 .endpoint = 0x00, /* Control pipe */
749 .direction = UE_DIR_ANY,
750 .bufsize = sizeof(struct usb_device_request) + 8,
751 .callback = &ukbd_set_leds_callback,
752 .timeout = 1000, /* 1 second */
757 ukbd_probe(device_t dev)
759 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
760 struct usb_attach_arg *uaa = device_get_ivars(dev);
770 if (uaa->usb_mode != USB_MODE_HOST) {
774 if (uaa->info.bInterfaceClass != UICLASS_HID)
777 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
778 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) {
779 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
782 return (BUS_PROBE_GENERIC);
785 error = usbd_req_get_hid_desc(uaa->device, NULL,
786 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
792 * NOTE: we currently don't support USB mouse and USB keyboard
793 * on the same USB endpoint.
795 if (hid_is_collection(d_ptr, d_len,
796 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
797 /* most likely a mouse */
799 } else if (hid_is_collection(d_ptr, d_len,
800 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
801 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
804 error = BUS_PROBE_GENERIC;
813 ukbd_attach(device_t dev)
815 struct ukbd_softc *sc = device_get_softc(dev);
816 struct usb_attach_arg *uaa = device_get_ivars(dev);
817 int32_t unit = device_get_unit(dev);
818 keyboard_t *kbd = &sc->sc_kbd;
819 void *hid_ptr = NULL;
825 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
827 kbd->kb_data = (void *)sc;
829 device_set_usb_desc(dev);
831 sc->sc_udev = uaa->device;
832 sc->sc_iface = uaa->iface;
833 sc->sc_iface_index = uaa->info.bIfaceIndex;
834 sc->sc_iface_no = uaa->info.bIfaceNum;
835 sc->sc_mode = K_XLATE;
837 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
839 err = usbd_transfer_setup(uaa->device,
840 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
841 UKBD_N_TRANSFER, sc, &Giant);
844 DPRINTF("error=%s\n", usbd_errstr(err));
847 /* setup default keyboard maps */
849 sc->sc_keymap = key_map;
850 sc->sc_accmap = accent_map;
851 for (n = 0; n < UKBD_NFKEY; n++) {
852 sc->sc_fkeymap[n] = fkey_tab[n];
855 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
856 sc->sc_fkeymap, UKBD_NFKEY);
858 KBD_FOUND_DEVICE(kbd);
860 ukbd_clear_state(kbd);
863 * FIXME: set the initial value for lock keys in "sc_state"
864 * according to the BIOS data?
869 * Set boot protocol if we need the quirk.
871 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO)) {
872 err = usbd_req_set_protocol(sc->sc_udev, NULL,
873 sc->sc_iface_index, 0);
874 if (err != USB_ERR_NORMAL_COMPLETION) {
875 DPRINTF("set protocol error=%s\n", usbd_errstr(err));
880 /* figure out if there is an ID byte in the data */
881 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
882 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
884 uint8_t apple_keys = 0;
887 /* investigate if this is an Apple Keyboard */
888 if (hid_locate(hid_ptr, hid_len,
889 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
890 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
892 if (flags & HIO_VARIABLE)
893 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
894 UKBD_FLAG_APPLE_SWAP;
895 DPRINTFN(1, "Found Apple eject-key\n");
897 sc->sc_kbd_id = temp_id;
899 if (hid_locate(hid_ptr, hid_len,
900 HID_USAGE2(0xFFFF, 0x0003),
901 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
903 if (flags & HIO_VARIABLE)
904 sc->sc_flags |= UKBD_FLAG_APPLE_FN;
905 DPRINTFN(1, "Found Apple FN-key\n");
907 sc->sc_kbd_id = temp_id;
909 if (apple_keys == 0) {
911 * Assume the first HID ID contains the
914 hid_report_size(hid_ptr, hid_len,
915 hid_input, &sc->sc_kbd_id);
918 /* investigate if we need an ID-byte for the leds */
919 hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id);
921 free(hid_ptr, M_TEMP);
924 /* ignore if SETIDLE fails, hence it is not crucial */
925 err = usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
929 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
935 if (kbd_register(kbd) < 0) {
938 KBD_CONFIG_DONE(kbd);
942 #ifdef KBD_INSTALL_CDEV
943 if (kbd_attach(kbd)) {
947 sc->sc_flags |= UKBD_FLAG_ATTACHED;
950 genkbd_diag(kbd, bootverbose);
952 /* lock keyboard mutex */
956 /* start the keyboard */
958 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
961 return (0); /* success */
965 return (ENXIO); /* error */
969 ukbd_detach(device_t dev)
971 struct ukbd_softc *sc = device_get_softc(dev);
978 sc->sc_flags |= UKBD_FLAG_GONE;
980 usb_callout_stop(&sc->sc_callout);
982 ukbd_disable(&sc->sc_kbd);
984 #ifdef KBD_INSTALL_CDEV
985 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
986 error = kbd_detach(&sc->sc_kbd);
988 /* usb attach cannot return an error */
989 device_printf(dev, "WARNING: kbd_detach() "
990 "returned non-zero! (ignored)\n");
994 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
995 error = kbd_unregister(&sc->sc_kbd);
997 /* usb attach cannot return an error */
998 device_printf(dev, "WARNING: kbd_unregister() "
999 "returned non-zero! (ignored)\n");
1002 sc->sc_kbd.kb_flags = 0;
1006 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1008 usb_callout_drain(&sc->sc_callout);
1010 DPRINTF("%s: disconnected\n",
1011 device_get_nameunit(dev));
1017 ukbd_resume(device_t dev)
1019 struct ukbd_softc *sc = device_get_softc(dev);
1023 ukbd_clear_state(&sc->sc_kbd);
1030 /* early keyboard probe, not supported */
1032 ukbd_configure(int flags)
1037 /* detect a keyboard, not used */
1039 ukbd__probe(int unit, void *arg, int flags)
1044 /* reset and initialize the device, not used */
1046 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1051 /* test the interface to the device, not used */
1053 ukbd_test_if(keyboard_t *kbd)
1058 /* finish using this keyboard, not used */
1060 ukbd_term(keyboard_t *kbd)
1065 /* keyboard interrupt routine, not used */
1067 ukbd_intr(keyboard_t *kbd, void *arg)
1072 /* lock the access to the keyboard, not used */
1074 ukbd_lock(keyboard_t *kbd, int lock)
1080 * Enable the access to the device; until this function is called,
1081 * the client cannot read from the keyboard.
1084 ukbd_enable(keyboard_t *kbd)
1086 if (!mtx_owned(&Giant)) {
1090 retval = ukbd_enable(kbd);
1098 /* disallow the access to the device */
1100 ukbd_disable(keyboard_t *kbd)
1102 if (!mtx_owned(&Giant)) {
1106 retval = ukbd_disable(kbd);
1110 KBD_DEACTIVATE(kbd);
1114 /* check if data is waiting */
1116 ukbd_check(keyboard_t *kbd)
1118 struct ukbd_softc *sc = kbd->kb_data;
1120 if (!KBD_IS_ACTIVE(kbd))
1123 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1124 if (!mtx_owned(&Giant)) {
1128 retval = ukbd_check(kbd);
1133 /* XXX the keyboard layer requires Giant */
1134 if (!mtx_owned(&Giant))
1138 /* check if key belongs to this thread */
1139 if (ukbd_polls_other_thread(sc))
1142 if (sc->sc_flags & UKBD_FLAG_POLLING)
1143 ukbd_do_poll(sc, 0);
1145 #ifdef UKBD_EMULATE_ATSCANCODE
1146 if (sc->sc_buffered_char[0]) {
1150 if (sc->sc_inputs > 0) {
1156 /* check if char is waiting */
1158 ukbd_check_char(keyboard_t *kbd)
1160 struct ukbd_softc *sc = kbd->kb_data;
1162 if (!KBD_IS_ACTIVE(kbd))
1165 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1166 if (!mtx_owned(&Giant)) {
1170 retval = ukbd_check_char(kbd);
1175 /* XXX the keyboard layer requires Giant */
1176 if (!mtx_owned(&Giant))
1180 /* check if key belongs to this thread */
1181 if (ukbd_polls_other_thread(sc))
1184 if ((sc->sc_composed_char > 0) &&
1185 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1188 return (ukbd_check(kbd));
1192 /* read one byte from the keyboard if it's allowed */
1194 ukbd_read(keyboard_t *kbd, int wait)
1196 struct ukbd_softc *sc = kbd->kb_data;
1199 #ifdef UKBD_EMULATE_ATSCANCODE
1204 if (!KBD_IS_ACTIVE(kbd))
1207 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1208 if (!mtx_owned(&Giant)) {
1212 retval = ukbd_read(kbd, wait);
1217 /* XXX the keyboard layer requires Giant */
1218 if (!mtx_owned(&Giant))
1222 /* check if key belongs to this thread */
1223 if (ukbd_polls_other_thread(sc))
1226 #ifdef UKBD_EMULATE_ATSCANCODE
1227 if (sc->sc_buffered_char[0]) {
1228 scancode = sc->sc_buffered_char[0];
1229 if (scancode & SCAN_PREFIX) {
1230 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1231 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1233 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1234 sc->sc_buffered_char[1] = 0;
1237 #endif /* UKBD_EMULATE_ATSCANCODE */
1240 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1241 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1246 #ifdef UKBD_EMULATE_ATSCANCODE
1247 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1248 if (keycode == NN) {
1251 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1252 (usbcode & KEY_RELEASE)));
1253 #else /* !UKBD_EMULATE_ATSCANCODE */
1255 #endif /* UKBD_EMULATE_ATSCANCODE */
1258 /* read char from the keyboard */
1260 ukbd_read_char(keyboard_t *kbd, int wait)
1262 struct ukbd_softc *sc = kbd->kb_data;
1267 #ifdef UKBD_EMULATE_ATSCANCODE
1272 if (!KBD_IS_ACTIVE(kbd))
1275 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1276 if (!mtx_owned(&Giant)) {
1280 retval = ukbd_read_char(kbd, wait);
1285 /* XXX the keyboard layer requires Giant */
1286 if (!mtx_owned(&Giant))
1290 /* check if key belongs to this thread */
1291 if (ukbd_polls_other_thread(sc))
1296 /* do we have a composed char to return ? */
1298 if ((sc->sc_composed_char > 0) &&
1299 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1301 action = sc->sc_composed_char;
1302 sc->sc_composed_char = 0;
1304 if (action > 0xFF) {
1309 #ifdef UKBD_EMULATE_ATSCANCODE
1311 /* do we have a pending raw scan code? */
1313 if (sc->sc_mode == K_RAW) {
1314 scancode = sc->sc_buffered_char[0];
1316 if (scancode & SCAN_PREFIX) {
1317 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1318 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1320 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1321 sc->sc_buffered_char[1] = 0;
1325 #endif /* UKBD_EMULATE_ATSCANCODE */
1327 /* see if there is something in the keyboard port */
1329 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1330 if (usbcode == -1) {
1335 #ifdef UKBD_EMULATE_ATSCANCODE
1336 /* USB key index -> key code -> AT scan code */
1337 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1338 if (keycode == NN) {
1341 /* return an AT scan code for the K_RAW mode */
1342 if (sc->sc_mode == K_RAW) {
1343 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1344 (usbcode & KEY_RELEASE)));
1346 #else /* !UKBD_EMULATE_ATSCANCODE */
1348 /* return the byte as is for the K_RAW mode */
1349 if (sc->sc_mode == K_RAW) {
1352 /* USB key index -> key code */
1353 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1354 if (keycode == NN) {
1357 #endif /* UKBD_EMULATE_ATSCANCODE */
1360 case 0x38: /* left alt (compose key) */
1361 if (usbcode & KEY_RELEASE) {
1362 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1363 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1365 if (sc->sc_composed_char > 0xFF) {
1366 sc->sc_composed_char = 0;
1370 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1371 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1372 sc->sc_composed_char = 0;
1376 /* XXX: I don't like these... */
1377 case 0x5c: /* print screen */
1378 if (sc->sc_flags & ALTS) {
1379 keycode = 0x54; /* sysrq */
1382 case 0x68: /* pause/break */
1383 if (sc->sc_flags & CTLS) {
1384 keycode = 0x6c; /* break */
1389 /* return the key code in the K_CODE mode */
1390 if (usbcode & KEY_RELEASE) {
1391 keycode |= SCAN_RELEASE;
1393 if (sc->sc_mode == K_CODE) {
1396 /* compose a character code */
1397 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1399 /* key pressed, process it */
1402 case 0x49: /* keypad 7,8,9 */
1403 sc->sc_composed_char *= 10;
1404 sc->sc_composed_char += keycode - 0x40;
1405 goto check_composed;
1409 case 0x4D: /* keypad 4,5,6 */
1410 sc->sc_composed_char *= 10;
1411 sc->sc_composed_char += keycode - 0x47;
1412 goto check_composed;
1416 case 0x51: /* keypad 1,2,3 */
1417 sc->sc_composed_char *= 10;
1418 sc->sc_composed_char += keycode - 0x4E;
1419 goto check_composed;
1421 case 0x52: /* keypad 0 */
1422 sc->sc_composed_char *= 10;
1423 goto check_composed;
1425 /* key released, no interest here */
1426 case SCAN_RELEASE | 0x47:
1427 case SCAN_RELEASE | 0x48:
1428 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1429 case SCAN_RELEASE | 0x4B:
1430 case SCAN_RELEASE | 0x4C:
1431 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1432 case SCAN_RELEASE | 0x4F:
1433 case SCAN_RELEASE | 0x50:
1434 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1435 case SCAN_RELEASE | 0x52: /* keypad 0 */
1438 case 0x38: /* left alt key */
1442 if (sc->sc_composed_char > 0) {
1443 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1444 sc->sc_composed_char = 0;
1450 /* keycode to key action */
1451 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1452 (keycode & SCAN_RELEASE),
1453 &sc->sc_state, &sc->sc_accents);
1454 if (action == NOKEY) {
1461 if (sc->sc_composed_char <= 0xFF) {
1468 /* some useful control functions */
1470 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1472 /* translate LED_XXX bits into the device specific bits */
1473 static const uint8_t ledmap[8] = {
1474 0, 2, 1, 3, 4, 6, 5, 7,
1476 struct ukbd_softc *sc = kbd->kb_data;
1479 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1480 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1484 if (!mtx_owned(&Giant)) {
1486 * XXX big problem: If scroll lock is pressed and "printf()"
1487 * is called, the CPU will get here, to un-scroll lock the
1488 * keyboard. But if "printf()" acquires the "Giant" lock,
1489 * there will be a locking order reversal problem, so the
1490 * keyboard system must get out of "Giant" first, before the
1491 * CPU can proceed here ...
1496 /* workaround for Geli */
1498 i = ukbd_ioctl(kbd, cmd, arg);
1507 case KDGKBMODE: /* get keyboard mode */
1508 *(int *)arg = sc->sc_mode;
1510 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1511 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1513 ival = IOCPARM_IVAL(arg);
1514 arg = (caddr_t)&ival;
1517 case KDSKBMODE: /* set keyboard mode */
1518 switch (*(int *)arg) {
1520 if (sc->sc_mode != K_XLATE) {
1521 /* make lock key state and LED state match */
1522 sc->sc_state &= ~LOCK_MASK;
1523 sc->sc_state |= KBD_LED_VAL(kbd);
1528 if (sc->sc_mode != *(int *)arg) {
1529 if (ukbd_is_polling(sc) == 0)
1530 ukbd_clear_state(kbd);
1531 sc->sc_mode = *(int *)arg;
1539 case KDGETLED: /* get keyboard LED */
1540 *(int *)arg = KBD_LED_VAL(kbd);
1542 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1543 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1545 ival = IOCPARM_IVAL(arg);
1546 arg = (caddr_t)&ival;
1549 case KDSETLED: /* set keyboard LED */
1550 /* NOTE: lock key state in "sc_state" won't be changed */
1551 if (*(int *)arg & ~LOCK_MASK) {
1555 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1556 if (sc->sc_mode == K_XLATE &&
1557 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1563 if (KBD_HAS_DEVICE(kbd)) {
1564 ukbd_set_leds(sc, ledmap[i & LED_MASK]);
1566 KBD_LED_VAL(kbd) = *(int *)arg;
1568 case KDGKBSTATE: /* get lock key state */
1569 *(int *)arg = sc->sc_state & LOCK_MASK;
1571 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1572 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1574 ival = IOCPARM_IVAL(arg);
1575 arg = (caddr_t)&ival;
1578 case KDSKBSTATE: /* set lock key state */
1579 if (*(int *)arg & ~LOCK_MASK) {
1582 sc->sc_state &= ~LOCK_MASK;
1583 sc->sc_state |= *(int *)arg;
1585 /* set LEDs and quit */
1586 return (ukbd_ioctl(kbd, KDSETLED, arg));
1588 case KDSETREPEAT: /* set keyboard repeat rate (new
1590 if (!KBD_HAS_DEVICE(kbd)) {
1593 if (((int *)arg)[1] < 0) {
1596 if (((int *)arg)[0] < 0) {
1599 if (((int *)arg)[0] < 200) /* fastest possible value */
1600 kbd->kb_delay1 = 200;
1602 kbd->kb_delay1 = ((int *)arg)[0];
1603 kbd->kb_delay2 = ((int *)arg)[1];
1606 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1607 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1609 ival = IOCPARM_IVAL(arg);
1610 arg = (caddr_t)&ival;
1613 case KDSETRAD: /* set keyboard repeat rate (old
1615 return (ukbd_set_typematic(kbd, *(int *)arg));
1617 case PIO_KEYMAP: /* set keyboard translation table */
1618 case PIO_KEYMAPENT: /* set keyboard translation table
1620 case PIO_DEADKEYMAP: /* set accent key translation table */
1624 return (genkbd_commonioctl(kbd, cmd, arg));
1630 /* clear the internal state of the keyboard */
1632 ukbd_clear_state(keyboard_t *kbd)
1634 struct ukbd_softc *sc = kbd->kb_data;
1636 if (!mtx_owned(&Giant)) {
1639 ukbd_clear_state(kbd);
1644 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1645 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1647 sc->sc_composed_char = 0;
1648 #ifdef UKBD_EMULATE_ATSCANCODE
1649 sc->sc_buffered_char[0] = 0;
1650 sc->sc_buffered_char[1] = 0;
1652 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1653 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
1654 memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
1655 memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
1658 /* save the internal state, not used */
1660 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1662 return (len == 0) ? 1 : -1;
1665 /* set the internal state, not used */
1667 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1673 ukbd_is_polling(struct ukbd_softc *sc)
1677 if (sc->sc_flags & UKBD_FLAG_POLLING)
1678 return (1); /* polling */
1680 delta = ticks - sc->sc_poll_tick_last;
1681 if ((delta < 0) || (delta >= hz)) {
1682 sc->sc_poll_detected = 0;
1683 return (0); /* not polling */
1686 return (sc->sc_poll_detected);
1690 ukbd_polls_other_thread(struct ukbd_softc *sc)
1692 return (ukbd_is_polling(sc) &&
1693 (sc->sc_poll_thread != curthread));
1697 ukbd_poll(keyboard_t *kbd, int on)
1699 struct ukbd_softc *sc = kbd->kb_data;
1701 if (!mtx_owned(&Giant)) {
1705 retval = ukbd_poll(kbd, on);
1711 sc->sc_flags |= UKBD_FLAG_POLLING;
1712 sc->sc_poll_thread = curthread;
1714 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1715 ukbd_start_timer(sc); /* start timer */
1720 /* local functions */
1723 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1725 DPRINTF("leds=0x%02x\n", leds);
1728 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1730 /* start transfer, if not already started */
1732 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1736 ukbd_set_typematic(keyboard_t *kbd, int code)
1738 static const int delays[] = {250, 500, 750, 1000};
1739 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1740 68, 76, 84, 92, 100, 110, 118, 126,
1741 136, 152, 168, 184, 200, 220, 236, 252,
1742 272, 304, 336, 368, 400, 440, 472, 504};
1747 kbd->kb_delay1 = delays[(code >> 5) & 3];
1748 kbd->kb_delay2 = rates[code & 0x1f];
1752 #ifdef UKBD_EMULATE_ATSCANCODE
1754 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1756 static const int scan[] = {
1762 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1772 0x151, /* PageDown */
1775 0x146, /* XXX Pause/Break */
1776 0x15b, /* Win_L(Super_L) */
1777 0x15c, /* Win_R(Super_R) */
1778 0x15d, /* Application(Menu) */
1780 /* SUN TYPE 6 USB KEYBOARD */
1781 0x168, /* Sun Type 6 Help */
1782 0x15e, /* Sun Type 6 Stop */
1784 0x15f, /* Sun Type 6 Again */
1785 0x160, /* Sun Type 6 Props */
1786 0x161, /* Sun Type 6 Undo */
1787 0x162, /* Sun Type 6 Front */
1788 0x163, /* Sun Type 6 Copy */
1789 0x164, /* Sun Type 6 Open */
1790 0x165, /* Sun Type 6 Paste */
1791 0x166, /* Sun Type 6 Find */
1792 0x167, /* Sun Type 6 Cut */
1793 0x125, /* Sun Type 6 Mute */
1795 0x11f, /* Sun Type 6 VolumeDown */
1796 0x11e, /* Sun Type 6 VolumeUp */
1797 0x120, /* Sun Type 6 PowerDown */
1799 /* Japanese 106/109 keyboard */
1800 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
1801 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
1802 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
1803 0x79, /* Keyboard Intl' 4 (Henkan) */
1804 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
1805 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
1808 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
1809 code = scan[code - 89];
1812 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
1813 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
1815 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
1816 code &= ~SCAN_PREFIX_SHIFT;
1818 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1820 if (code & SCAN_PREFIX) {
1821 if (code & SCAN_PREFIX_CTL) {
1823 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
1824 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
1825 } else if (code & SCAN_PREFIX_SHIFT) {
1827 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1828 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1830 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1831 sc->sc_buffered_char[1] = 0;
1833 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1839 #endif /* UKBD_EMULATE_ATSCANCODE */
1841 static keyboard_switch_t ukbdsw = {
1842 .probe = &ukbd__probe,
1846 .test_if = &ukbd_test_if,
1847 .enable = &ukbd_enable,
1848 .disable = &ukbd_disable,
1850 .check = &ukbd_check,
1851 .read_char = &ukbd_read_char,
1852 .check_char = &ukbd_check_char,
1853 .ioctl = &ukbd_ioctl,
1855 .clear_state = &ukbd_clear_state,
1856 .get_state = &ukbd_get_state,
1857 .set_state = &ukbd_set_state,
1858 .get_fkeystr = &genkbd_get_fkeystr,
1860 .diag = &genkbd_diag,
1863 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
1866 ukbd_driver_load(module_t mod, int what, void *arg)
1870 kbd_add_driver(&ukbd_kbd_driver);
1873 kbd_delete_driver(&ukbd_kbd_driver);
1879 static devclass_t ukbd_devclass;
1881 static device_method_t ukbd_methods[] = {
1882 DEVMETHOD(device_probe, ukbd_probe),
1883 DEVMETHOD(device_attach, ukbd_attach),
1884 DEVMETHOD(device_detach, ukbd_detach),
1885 DEVMETHOD(device_resume, ukbd_resume),
1889 static driver_t ukbd_driver = {
1891 .methods = ukbd_methods,
1892 .size = sizeof(struct ukbd_softc),
1895 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
1896 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
1897 MODULE_VERSION(ukbd, 1);