2 * $FreeBSD: src/sys/dev/usb/ukbd.c,v 1.45 2003/10/04 21:41:01 joe Exp $
3 * $DragonFly: src/sys/dev/usbmisc/ukbd/ukbd.c,v 1.25 2007/11/05 19:09:43 hasso Exp $
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/ioccom.h>
54 #include <sys/module.h>
57 #include <machine/limits.h>
58 #include <sys/select.h>
59 #include <sys/sysctl.h>
60 #include <sys/thread2.h>
62 #include <bus/usb/usb.h>
63 #include <bus/usb/usbhid.h>
64 #include <bus/usb/usbdi.h>
65 #include <bus/usb/usbdi_util.h>
66 #include <bus/usb/usb_quirks.h>
67 #include <bus/usb/hid.h>
70 #include <dev/misc/kbd/kbdreg.h>
72 #define UKBD_EMULATE_ATSCANCODE 1
74 #define DRIVER_NAME "ukbd"
76 #define delay(d) DELAY(d)
79 #define DPRINTF(x) if (ukbddebug) kprintf x
80 #define DPRINTFN(n,x) if (ukbddebug>(n)) kprintf x
82 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
83 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
84 &ukbddebug, 0, "ukbd debug level");
90 #define UPROTO_BOOT_KEYBOARD 1
96 #define MOD_CONTROL_L 0x01
97 #define MOD_CONTROL_R 0x10
98 #define MOD_SHIFT_L 0x02
99 #define MOD_SHIFT_R 0x20
100 #define MOD_ALT_L 0x04
101 #define MOD_ALT_R 0x40
102 #define MOD_WIN_L 0x08
103 #define MOD_WIN_R 0x80
105 u_int8_t keycode[NKEYCODE];
108 #define MAXKEYS (NMOD+2*NKEYCODE)
110 typedef struct ukbd_softc {
111 device_t sc_dev; /* base device */
114 #define UKBD_CHUNK 128 /* chunk size for read */
115 #define UKBD_BSIZE 1020 /* buffer size */
117 typedef void usbd_intr_t(usbd_xfer_handle, usbd_private_handle, usbd_status);
118 typedef void usbd_disco_t(void *);
120 static int ukbd_resume(device_t self);
121 static usbd_intr_t ukbd_intr;
122 static int ukbd_driver_load(module_t mod, int what, void *arg);
123 static int ukbd_default_term(keyboard_t *kbd);
125 static keyboard_t default_kbd;
127 static device_probe_t ukbd_match;
128 static device_attach_t ukbd_attach;
129 static device_detach_t ukbd_detach;
131 static devclass_t ukbd_devclass;
133 static kobj_method_t ukbd_methods[] = {
134 DEVMETHOD(device_probe, ukbd_match),
135 DEVMETHOD(device_attach, ukbd_attach),
136 DEVMETHOD(device_detach, ukbd_detach),
137 DEVMETHOD(device_resume, ukbd_resume),
141 static driver_t ukbd_driver = {
144 sizeof(struct ukbd_softc)
147 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
150 ukbd_match(device_t self)
152 struct usb_attach_arg *uaa = device_get_ivars(self);
154 keyboard_switch_t *sw;
156 int unit = device_get_unit(self);
158 sw = kbd_get_switch(DRIVER_NAME);
160 return (UMATCH_NONE);
162 arg[0] = (void *)uaa;
163 arg[1] = (void *)ukbd_intr;
164 if ((*sw->probe)(unit, (void *)arg, 0))
165 return (UMATCH_NONE);
167 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
171 ukbd_attach(device_t self)
173 struct ukbd_softc *sc = device_get_softc(self);
174 struct usb_attach_arg *uaa = device_get_ivars(self);
175 usbd_interface_handle iface = uaa->iface;
176 usb_interface_descriptor_t *id;
179 keyboard_switch_t *sw;
182 int unit = device_get_unit(self);
184 sw = kbd_get_switch(DRIVER_NAME);
188 id = usbd_get_interface_descriptor(iface);
189 usbd_devinfo(uaa->device, 0, devinfo);
191 device_set_desc_copy(self, devinfo);
192 kprintf("%s: %s, iclass %d/%d\n", device_get_nameunit(sc->sc_dev),
193 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
195 arg[0] = (void *)uaa;
196 arg[1] = (void *)ukbd_intr;
198 if ((*sw->probe)(unit, (void *)arg, 0))
200 if ((*sw->init)(unit, &kbd, (void *)arg, 0))
204 #ifdef KBD_INSTALL_CDEV
209 (*sw->diag)(kbd, bootverbose);
215 ukbd_detach(device_t self)
220 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
221 device_get_unit(self)));
223 DPRINTF(("%s: keyboard not attached!?\n", device_get_nameunit(self)));
226 (*kbdsw[kbd->kb_index]->disable)(kbd);
228 #ifdef KBD_INSTALL_CDEV
229 if (kbd != &default_kbd) {
230 error = kbd_detach(kbd);
235 if (kbd == &default_kbd) {
236 ukbd_default_term(kbd);
238 error = (*kbdsw[kbd->kb_index]->term)(kbd);
243 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
249 ukbd_resume(device_t self)
253 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
254 device_get_unit(self)));
256 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
261 ukbd_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
263 keyboard_t *kbd = (keyboard_t *)addr;
265 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)status);
268 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
271 #define UKBD_DEFAULT 0
273 #define KEY_ERROR 0x01
276 #define KEY_RELEASE 0x400
277 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE)
280 #define SCAN_RELEASE 0x80
281 #define SCAN_PREFIX_E0 0x100
282 #define SCAN_PREFIX_E1 0x200
283 #define SCAN_PREFIX_CTL 0x400
284 #define SCAN_PREFIX_SHIFT 0x800
285 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL \
287 #define SCAN_CHAR(c) ((c) & 0x7f)
292 } ukbd_mods[NMOD] = {
293 { MOD_CONTROL_L, 0xe0 },
294 { MOD_CONTROL_R, 0xe4 },
295 { MOD_SHIFT_L, 0xe1 },
296 { MOD_SHIFT_R, 0xe5 },
303 #define NN 0 /* no translation */
305 * Translate USB keycodes to AT keyboard scancodes.
308 * FIXME: Mac USB keyboard generates:
309 * 0x53: keypad NumLock/Clear
316 static u_int8_t ukbd_trtab[256] = {
317 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
318 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
319 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
320 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
321 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
322 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
323 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
324 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
325 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
326 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
327 97, 100, 95, 69, 91, 55, 74, 78, /* 50 - 57 */
328 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
329 72, 73, 82, 83, 86, 107, NN, NN, /* 60 - 67 */
330 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
331 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
332 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7F */
333 NN, NN, NN, NN, NN, NN, NN, 115, /* 80 - 87 */
334 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */
335 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
336 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
337 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
338 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
339 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
340 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
341 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
342 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
343 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
344 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
345 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
346 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
347 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
348 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
351 typedef struct ukbd_state {
352 usbd_interface_handle ks_iface; /* interface */
353 usbd_pipe_handle ks_intrpipe; /* interrupt pipe */
354 struct usb_attach_arg *ks_uaa;
357 struct ukbd_data ks_ndata;
358 struct ukbd_data ks_odata;
359 u_long ks_ntime[NKEYCODE];
360 u_long ks_otime[NKEYCODE];
362 #define INPUTBUFSIZE (NMOD + 2*NKEYCODE)
363 u_int ks_input[INPUTBUFSIZE]; /* input buffer */
369 #define INTRENABLED (1 << 0)
370 #define DISCONNECTED (1 << 1)
372 struct callout ks_timeout;
374 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
375 int ks_flags; /* flags */
376 #define COMPOSE (1 << 0)
378 int ks_state; /* shift/lock key state */
379 int ks_accents; /* accent key index (> 0) */
380 u_int ks_composed_char; /* composed char code (> 0) */
381 #ifdef UKBD_EMULATE_ATSCANCODE
382 u_int ks_buffered_char[2];
386 /* keyboard driver declaration */
387 static int ukbd_configure(int flags);
388 static kbd_probe_t ukbd_probe;
389 static kbd_init_t ukbd_init;
390 static kbd_term_t ukbd_term;
391 static kbd_intr_t ukbd_interrupt;
392 static kbd_test_if_t ukbd_test_if;
393 static kbd_enable_t ukbd_enable;
394 static kbd_disable_t ukbd_disable;
395 static kbd_read_t ukbd_read;
396 static kbd_check_t ukbd_check;
397 static kbd_read_char_t ukbd_read_char;
398 static kbd_check_char_t ukbd_check_char;
399 static kbd_ioctl_t ukbd_ioctl;
400 static kbd_lock_t ukbd_lock;
401 static kbd_clear_state_t ukbd_clear_state;
402 static kbd_get_state_t ukbd_get_state;
403 static kbd_set_state_t ukbd_set_state;
404 static kbd_poll_mode_t ukbd_poll;
406 keyboard_switch_t ukbdsw = {
428 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
430 /* local functions */
431 static int ukbd_enable_intr(keyboard_t *kbd, int on,
433 static timeout_t ukbd_timeout;
435 static int ukbd_getc(ukbd_state_t *state);
436 static int probe_keyboard(struct usb_attach_arg *uaa, int flags);
437 static int init_keyboard(ukbd_state_t *state, int *type,
439 static void set_leds(ukbd_state_t *state, int leds);
440 static int set_typematic(keyboard_t *kbd, int code);
441 #ifdef UKBD_EMULATE_ATSCANCODE
442 static int keycode2scancode(int keycode, int shift, int up);
445 /* local variables */
447 /* the initial key map, accent map and fkey strings */
448 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
449 #define KBD_DFLT_KEYMAP
452 #include <dev/misc/kbd/kbdtables.h>
454 /* structures for the default keyboard */
455 static ukbd_state_t default_kbd_state;
456 static keymap_t default_keymap;
457 static accentmap_t default_accentmap;
458 static fkeytab_t default_fkeytab[NUM_FKEYS];
461 * The back door to the keyboard driver!
462 * This function is called by the console driver, via the kbdio module,
463 * to tickle keyboard drivers when the low-level console is being initialized.
464 * Almost nothing in the kernel has been initialied yet. Try to probe
465 * keyboards if possible.
466 * NOTE: because of the way the low-level conole is initialized, this routine
467 * may be called more than once!!
470 ukbd_configure(int flags)
477 struct usb_attach_arg *uaa;
480 device = devclass_get_device(ukbd_devclass, UKBD_DEFAULT);
483 uaa = (struct usb_attach_arg *)device_get_ivars(device);
487 /* probe the default keyboard */
488 arg[0] = (void *)uaa;
489 arg[1] = (void *)ukbd_intr;
491 if (ukbd_probe(UKBD_DEFAULT, arg, flags))
493 if (ukbd_init(UKBD_DEFAULT, &kbd, arg, flags))
496 /* return the number of found keyboards */
501 /* low-level functions */
503 /* detect a keyboard */
505 ukbd_probe(int unit, void *arg, int flags)
508 struct usb_attach_arg *uaa;
511 uaa = (struct usb_attach_arg *)data[0];
513 if (unit == UKBD_DEFAULT) {
514 if (KBD_IS_PROBED(&default_kbd))
517 if (probe_keyboard(uaa, flags))
523 * Reset and initialize the device. Note that unit 0 (UKBD_DEFAULT) is an
524 * always-connected device once it has been initially detected. We do not
525 * deregister it if the usb keyboard is unplugged to avoid losing the
526 * connection to the console. This feature also handles the USB bus reset
527 * which detaches and reattaches USB devices during boot.
530 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
538 void **data = (void **)arg;
539 struct usb_attach_arg *uaa = (struct usb_attach_arg *)data[0];
541 if (unit == UKBD_DEFAULT) {
542 *kbdp = kbd = &default_kbd;
543 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
545 state = &default_kbd_state;
546 keymap = &default_keymap;
547 accmap = &default_accentmap;
548 fkeymap = default_fkeytab;
550 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
551 } else if (*kbdp == NULL) {
552 *kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_INTWAIT | M_ZERO);
553 state = kmalloc(sizeof(*state), M_DEVBUF, M_INTWAIT);
554 keymap = kmalloc(sizeof(key_map), M_DEVBUF, M_INTWAIT);
555 accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_INTWAIT);
556 fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_INTWAIT);
557 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
558 if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
559 || (fkeymap == NULL)) {
561 kfree(state, M_DEVBUF);
563 kfree(keymap, M_DEVBUF);
565 kfree(accmap, M_DEVBUF);
567 kfree(fkeymap, M_DEVBUF);
568 kfree(kbd, M_DEVBUF);
571 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
575 state = (ukbd_state_t *)kbd->kb_data;
576 keymap = kbd->kb_keymap;
577 accmap = kbd->kb_accentmap;
578 fkeymap = kbd->kb_fkeytab;
579 fkeymap_size = kbd->kb_fkeytab_size;
582 if (!KBD_IS_PROBED(kbd)) {
583 if (KBD_IS_CONFIGURED(kbd)) {
584 kbd_reinit_struct(kbd, flags, KB_PRI_USB);
586 kbd_init_struct(kbd, DRIVER_NAME, KB_OTHER,
587 unit, flags, KB_PRI_USB,
590 bzero(state, sizeof(*state));
591 bcopy(&key_map, keymap, sizeof(key_map));
592 bcopy(&accent_map, accmap, sizeof(accent_map));
593 bcopy(fkey_tab, fkeymap,
594 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
595 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
596 kbd->kb_data = (void *)state;
598 if (probe_keyboard(uaa, flags))
601 KBD_FOUND_DEVICE(kbd);
602 ukbd_clear_state(kbd);
605 * If reattatching to an already open keyboard (e.g. console),
606 * try to restore the translation mode. Otherwise set the
607 * translation mode to, well, translation mode so we don't
610 if (!KBD_IS_CONFIGURED(kbd)) {
611 state->ks_mode = K_XLATE;
612 kbd->kb_savemode = state->ks_mode;
614 state->ks_mode = kbd->kb_savemode;
616 state->ks_iface = uaa->iface;
618 state->ks_ifstate = 0;
619 callout_init(&state->ks_timeout);
621 * FIXME: set the initial value for lock keys in ks_state
622 * according to the BIOS data?
626 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
627 if (KBD_HAS_DEVICE(kbd)
628 && init_keyboard((ukbd_state_t *)kbd->kb_data,
629 &kbd->kb_type, kbd->kb_flags))
631 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&(state->ks_state));
633 if (!KBD_IS_CONFIGURED(kbd)) {
634 if (kbd_register(kbd) < 0)
636 KBD_CONFIG_DONE(kbd);
638 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
639 if (ukbd_enable_intr(kbd, TRUE, (usbd_intr_t *)data[1]) == 0)
640 ukbd_timeout((void *)kbd);
647 ukbd_enable_intr(keyboard_t *kbd, int on, usbd_intr_t *func)
649 ukbd_state_t *state = (ukbd_state_t *)kbd->kb_data;
653 /* Set up interrupt pipe. */
654 if (state->ks_ifstate & INTRENABLED)
657 state->ks_ifstate |= INTRENABLED;
658 err = usbd_open_pipe_intr(state->ks_iface, state->ks_ep_addr,
660 &state->ks_intrpipe, kbd,
662 sizeof(state->ks_ndata), func,
663 USBD_DEFAULT_INTERVAL);
667 /* Disable interrupts. */
668 usbd_abort_pipe(state->ks_intrpipe);
669 usbd_close_pipe(state->ks_intrpipe);
671 state->ks_ifstate &= ~INTRENABLED;
677 /* finish using this keyboard */
679 ukbd_term(keyboard_t *kbd)
685 state = (ukbd_state_t *)kbd->kb_data;
686 DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state->ks_ifstate));
688 callout_stop(&state->ks_timeout);
690 if (state->ks_ifstate & INTRENABLED)
691 ukbd_enable_intr(kbd, FALSE, NULL);
692 if (state->ks_ifstate & INTRENABLED) {
694 DPRINTF(("ukbd_term: INTRENABLED!\n"));
698 error = kbd_unregister(kbd);
699 DPRINTF(("ukbd_term: kbd_unregister() %d\n", error));
702 if (kbd != &default_kbd) {
703 kfree(kbd->kb_keymap, M_DEVBUF);
704 kfree(kbd->kb_accentmap, M_DEVBUF);
705 kfree(kbd->kb_fkeytab, M_DEVBUF);
706 kfree(state, M_DEVBUF);
707 kfree(kbd, M_DEVBUF);
715 * Finish using the default keyboard. Shutdown the USB side of the keyboard
716 * but do not unregister it.
719 ukbd_default_term(keyboard_t *kbd)
725 state = (ukbd_state_t *)kbd->kb_data;
726 DPRINTF(("ukbd_default_term: ks_ifstate=0x%x\n", state->ks_ifstate));
728 callout_stop(&state->ks_timeout);
730 if (state->ks_ifstate & INTRENABLED)
731 ukbd_enable_intr(kbd, FALSE, NULL);
732 if (state->ks_ifstate & INTRENABLED) {
734 DPRINTF(("ukbd_term: INTRENABLED!\n"));
737 KBD_LOST_DEVICE(kbd);
744 /* keyboard interrupt routine */
747 ukbd_timeout(void *arg)
752 kbd = (keyboard_t *)arg;
753 state = (ukbd_state_t *)kbd->kb_data;
755 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)USBD_NORMAL_COMPLETION);
756 callout_reset(&state->ks_timeout, hz / 40, ukbd_timeout, arg);
761 ukbd_interrupt(keyboard_t *kbd, void *arg)
763 usbd_status status = (usbd_status)arg;
765 struct ukbd_data *ud;
772 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
773 if (status == USBD_CANCELLED)
776 state = (ukbd_state_t *)kbd->kb_data;
777 ud = &state->ks_ndata;
779 if (status != USBD_NORMAL_COMPLETION) {
780 DPRINTF(("ukbd_intr: status=%d\n", status));
781 if (status == USBD_STALLED)
782 usbd_clear_endpoint_stall_async(state->ks_intrpipe);
786 if (ud->keycode[0] == KEY_ERROR)
787 return 0; /* ignore */
790 now = (u_long)tv.tv_sec*1000 + (u_long)tv.tv_usec/1000;
793 if (state->ks_inputs < INPUTBUFSIZE) { \
794 state->ks_input[state->ks_inputtail] = (c); \
795 ++state->ks_inputs; \
796 state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \
800 omod = state->ks_odata.modifiers;
802 for (i = 0; i < NMOD; i++)
803 if (( mod & ukbd_mods[i].mask) !=
804 (omod & ukbd_mods[i].mask))
805 ADDKEY1(ukbd_mods[i].key |
806 (mod & ukbd_mods[i].mask
807 ? KEY_PRESS : KEY_RELEASE));
810 /* Check for released keys. */
811 for (i = 0; i < NKEYCODE; i++) {
812 key = state->ks_odata.keycode[i];
815 for (j = 0; j < NKEYCODE; j++) {
816 if (ud->keycode[j] == 0)
818 if (key == ud->keycode[j])
821 ADDKEY1(key | KEY_RELEASE);
826 /* Check for pressed keys. */
827 for (i = 0; i < NKEYCODE; i++) {
828 key = ud->keycode[i];
831 state->ks_ntime[i] = now + kbd->kb_delay1;
832 for (j = 0; j < NKEYCODE; j++) {
833 if (state->ks_odata.keycode[j] == 0)
835 if (key == state->ks_odata.keycode[j]) {
836 state->ks_ntime[i] = state->ks_otime[j];
837 if (state->ks_otime[j] > now)
839 state->ks_ntime[i] = now + kbd->kb_delay2;
843 ADDKEY1(key | KEY_PRESS);
848 state->ks_odata = *ud;
849 bcopy(state->ks_ntime, state->ks_otime, sizeof(state->ks_ntime));
850 if (state->ks_inputs <= 0)
854 for (i = state->ks_inputhead, j = 0; j < state->ks_inputs; ++j,
855 i = (i + 1)%INPUTBUFSIZE) {
856 c = state->ks_input[i];
857 DPRINTF(("0x%x (%d) %s\n", c, c,
858 (c & KEY_RELEASE) ? "released":"pressed"));
861 DPRINTF(("mod:0x%04x ", ud->modifiers));
862 for (i = 0; i < NKEYCODE; i++) {
864 DPRINTF(("%d ", ud->keycode[i]));
867 #endif /* USB_DEBUG */
869 if (state->ks_polling)
872 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
873 /* let the callback function to process the input */
874 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
875 kbd->kb_callback.kc_arg);
877 /* read and discard the input; no one is waiting for it */
879 c = ukbd_read_char(kbd, FALSE);
880 } while (c != NOKEY);
887 ukbd_getc(ukbd_state_t *state)
891 if (state->ks_polling) {
892 DPRINTFN(1,("ukbd_getc: polling\n"));
894 while (state->ks_inputs <= 0)
895 usbd_dopoll(state->ks_iface);
899 if (state->ks_inputs <= 0) {
902 c = state->ks_input[state->ks_inputhead];
904 state->ks_inputhead = (state->ks_inputhead + 1)%INPUTBUFSIZE;
910 /* test the interface to the device */
912 ukbd_test_if(keyboard_t *kbd)
918 * Enable the access to the device; until this function is called,
919 * the client cannot read from the keyboard.
922 ukbd_enable(keyboard_t *kbd)
930 /* disallow the access to the device */
932 ukbd_disable(keyboard_t *kbd)
940 /* read one byte from the keyboard if it's allowed */
942 ukbd_read(keyboard_t *kbd, int wait)
946 #ifdef UKBD_EMULATE_ATSCANCODE
951 state = (ukbd_state_t *)kbd->kb_data;
952 #ifdef UKBD_EMULATE_ATSCANCODE
953 if (state->ks_buffered_char[0]) {
954 scancode = state->ks_buffered_char[0];
955 if (scancode & SCAN_PREFIX) {
956 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
957 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
959 state->ks_buffered_char[0] = state->ks_buffered_char[1];
960 state->ks_buffered_char[1] = 0;
964 #endif /* UKBD_EMULATE_ATSCANCODE */
967 usbcode = ukbd_getc(state);
968 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd) || (usbcode == -1))
971 #ifdef UKBD_EMULATE_ATSCANCODE
972 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
976 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
977 usbcode & KEY_RELEASE);
978 if (scancode & SCAN_PREFIX) {
979 if (scancode & SCAN_PREFIX_CTL) {
980 state->ks_buffered_char[0] =
981 0x1d | (scancode & SCAN_RELEASE); /* Ctrl */
982 state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX;
983 } else if (scancode & SCAN_PREFIX_SHIFT) {
984 state->ks_buffered_char[0] =
985 0x2a | (scancode & SCAN_RELEASE); /* Shift */
986 state->ks_buffered_char[1] =
987 scancode & ~SCAN_PREFIX_SHIFT;
989 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
990 state->ks_buffered_char[1] = 0;
992 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
995 #else /* !UKBD_EMULATE_ATSCANCODE */
997 #endif /* UKBD_EMULATE_ATSCANCODE */
1000 /* check if data is waiting */
1002 ukbd_check(keyboard_t *kbd)
1004 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd))
1006 #ifdef UKBD_EMULATE_ATSCANCODE
1007 if (((ukbd_state_t *)kbd->kb_data)->ks_buffered_char[0])
1010 if (((ukbd_state_t *)kbd->kb_data)->ks_inputs > 0)
1015 /* read char from the keyboard */
1017 ukbd_read_char(keyboard_t *kbd, int wait)
1019 ukbd_state_t *state;
1023 #ifdef UKBD_EMULATE_ATSCANCODE
1027 state = (ukbd_state_t *)kbd->kb_data;
1029 /* do we have a composed char to return? */
1030 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
1031 action = state->ks_composed_char;
1032 state->ks_composed_char = 0;
1033 if (action > UCHAR_MAX)
1038 #ifdef UKBD_EMULATE_ATSCANCODE
1039 /* do we have a pending raw scan code? */
1040 if (state->ks_mode == K_RAW) {
1041 if (state->ks_buffered_char[0]) {
1042 scancode = state->ks_buffered_char[0];
1043 if (scancode & SCAN_PREFIX) {
1044 state->ks_buffered_char[0] =
1045 scancode & ~SCAN_PREFIX;
1046 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1048 state->ks_buffered_char[0] =
1049 state->ks_buffered_char[1];
1050 state->ks_buffered_char[1] = 0;
1055 #endif /* UKBD_EMULATE_ATSCANCODE */
1057 /* see if there is something in the keyboard port */
1059 usbcode = ukbd_getc(state);
1064 #ifdef UKBD_EMULATE_ATSCANCODE
1065 /* USB key index -> key code -> AT scan code */
1066 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1070 /* return an AT scan code for the K_RAW mode */
1071 if (state->ks_mode == K_RAW) {
1072 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
1073 usbcode & KEY_RELEASE);
1074 if (scancode & SCAN_PREFIX) {
1075 if (scancode & SCAN_PREFIX_CTL) {
1076 state->ks_buffered_char[0] =
1077 0x1d | (scancode & SCAN_RELEASE);
1078 state->ks_buffered_char[1] =
1079 scancode & ~SCAN_PREFIX;
1080 } else if (scancode & SCAN_PREFIX_SHIFT) {
1081 state->ks_buffered_char[0] =
1082 0x2a | (scancode & SCAN_RELEASE);
1083 state->ks_buffered_char[1] =
1084 scancode & ~SCAN_PREFIX_SHIFT;
1086 state->ks_buffered_char[0] =
1087 scancode & ~SCAN_PREFIX;
1088 state->ks_buffered_char[1] = 0;
1090 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1094 #else /* !UKBD_EMULATE_ATSCANCODE */
1095 /* return the byte as is for the K_RAW mode */
1096 if (state->ks_mode == K_RAW)
1099 /* USB key index -> key code */
1100 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1103 #endif /* UKBD_EMULATE_ATSCANCODE */
1106 case 0x38: /* left alt (compose key) */
1107 if (usbcode & KEY_RELEASE) {
1108 if (state->ks_flags & COMPOSE) {
1109 state->ks_flags &= ~COMPOSE;
1110 if (state->ks_composed_char > UCHAR_MAX)
1111 state->ks_composed_char = 0;
1114 if (!(state->ks_flags & COMPOSE)) {
1115 state->ks_flags |= COMPOSE;
1116 state->ks_composed_char = 0;
1120 /* XXX: I don't like these... */
1121 case 0x5c: /* print screen */
1122 if (state->ks_flags & ALTS)
1123 keycode = 0x54; /* sysrq */
1125 case 0x68: /* pause/break */
1126 if (state->ks_flags & CTLS)
1127 keycode = 0x6c; /* break */
1131 /* return the key code in the K_CODE mode */
1132 if (usbcode & KEY_RELEASE)
1133 keycode |= SCAN_RELEASE;
1134 if (state->ks_mode == K_CODE)
1137 /* compose a character code */
1138 if (state->ks_flags & COMPOSE) {
1140 /* key pressed, process it */
1141 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
1142 state->ks_composed_char *= 10;
1143 state->ks_composed_char += keycode - 0x40;
1144 if (state->ks_composed_char > UCHAR_MAX)
1147 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
1148 state->ks_composed_char *= 10;
1149 state->ks_composed_char += keycode - 0x47;
1150 if (state->ks_composed_char > UCHAR_MAX)
1153 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
1154 state->ks_composed_char *= 10;
1155 state->ks_composed_char += keycode - 0x4E;
1156 if (state->ks_composed_char > UCHAR_MAX)
1159 case 0x52: /* keypad 0 */
1160 state->ks_composed_char *= 10;
1161 if (state->ks_composed_char > UCHAR_MAX)
1165 /* key released, no interest here */
1166 case SCAN_RELEASE | 0x47:
1167 case SCAN_RELEASE | 0x48:
1168 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1169 case SCAN_RELEASE | 0x4B:
1170 case SCAN_RELEASE | 0x4C:
1171 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1172 case SCAN_RELEASE | 0x4F:
1173 case SCAN_RELEASE | 0x50:
1174 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1175 case SCAN_RELEASE | 0x52: /* keypad 0 */
1178 case 0x38: /* left alt key */
1182 if (state->ks_composed_char > 0) {
1183 state->ks_flags &= ~COMPOSE;
1184 state->ks_composed_char = 0;
1191 /* keycode to key action */
1192 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1193 keycode & SCAN_RELEASE, &state->ks_state,
1194 &state->ks_accents);
1195 if (action == NOKEY)
1201 /* check if char is waiting */
1203 ukbd_check_char(keyboard_t *kbd)
1205 ukbd_state_t *state;
1207 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd))
1209 state = (ukbd_state_t *)kbd->kb_data;
1210 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
1212 return (ukbd_check(kbd));
1215 /* some useful control functions */
1217 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1219 /* trasnlate LED_XXX bits into the device specific bits */
1220 static u_char ledmap[8] = {
1221 0, 2, 1, 3, 4, 6, 5, 7,
1223 ukbd_state_t *state = kbd->kb_data;
1228 case KDGKBMODE: /* get keyboard mode */
1229 *(int *)arg = state->ks_mode;
1231 case KDSKBMODE: /* set keyboard mode */
1232 switch (*(int *)arg) {
1234 if (state->ks_mode != K_XLATE) {
1235 /* make lock key state and LED state match */
1236 state->ks_state &= ~LOCK_MASK;
1237 state->ks_state |= KBD_LED_VAL(kbd);
1242 if (state->ks_mode != *(int *)arg) {
1243 ukbd_clear_state(kbd);
1244 state->ks_mode = *(int *)arg;
1245 kbd->kb_savemode = state->ks_mode;
1254 case KDGETLED: /* get keyboard LED */
1255 *(int *)arg = KBD_LED_VAL(kbd);
1257 case KDSETLED: /* set keyboard LED */
1258 /* NOTE: lock key state in ks_state won't be changed */
1259 if (*(int *)arg & ~LOCK_MASK) {
1264 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1265 if (kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1271 if (KBD_HAS_DEVICE(kbd)) {
1272 set_leds(state, ledmap[i & LED_MASK]);
1273 /* XXX: error check? */
1275 KBD_LED_VAL(kbd) = *(int *)arg;
1278 case KDGKBSTATE: /* get lock key state */
1279 *(int *)arg = state->ks_state & LOCK_MASK;
1281 case KDSKBSTATE: /* set lock key state */
1282 if (*(int *)arg & ~LOCK_MASK) {
1286 state->ks_state &= ~LOCK_MASK;
1287 state->ks_state |= *(int *)arg;
1289 /* set LEDs and quit */
1290 return ukbd_ioctl(kbd, KDSETLED, arg);
1292 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
1294 if (!KBD_HAS_DEVICE(kbd))
1296 if (((int *)arg)[1] < 0)
1298 if (((int *)arg)[0] < 0)
1300 else if (((int *)arg)[0] == 0) /* fastest possible value */
1301 kbd->kb_delay1 = 200;
1303 kbd->kb_delay1 = ((int *)arg)[0];
1304 kbd->kb_delay2 = ((int *)arg)[1];
1307 case KDSETRAD: /* set keyboard repeat rate (old interface) */
1309 return set_typematic(kbd, *(int *)arg);
1311 case PIO_KEYMAP: /* set keyboard translation table */
1312 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1313 case PIO_DEADKEYMAP: /* set accent key translation table */
1314 state->ks_accents = 0;
1318 return genkbd_commonioctl(kbd, cmd, arg);
1322 ukbddebug = *(int *)arg;
1331 /* lock the access to the keyboard */
1333 ukbd_lock(keyboard_t *kbd, int lock)
1339 /* clear the internal state of the keyboard */
1341 ukbd_clear_state(keyboard_t *kbd)
1343 ukbd_state_t *state;
1345 state = (ukbd_state_t *)kbd->kb_data;
1346 state->ks_flags = 0;
1347 state->ks_polling = 0;
1348 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1349 state->ks_accents = 0;
1350 state->ks_composed_char = 0;
1351 #ifdef UKBD_EMULATE_ATSCANCODE
1352 state->ks_buffered_char[0] = 0;
1353 state->ks_buffered_char[1] = 0;
1355 bzero(&state->ks_ndata, sizeof(state->ks_ndata));
1356 bzero(&state->ks_odata, sizeof(state->ks_odata));
1357 bzero(&state->ks_ntime, sizeof(state->ks_ntime));
1358 bzero(&state->ks_otime, sizeof(state->ks_otime));
1361 /* save the internal state */
1363 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1366 return sizeof(ukbd_state_t);
1367 if (len < sizeof(ukbd_state_t))
1369 bcopy(kbd->kb_data, buf, sizeof(ukbd_state_t));
1373 /* set the internal state */
1375 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1377 if (len < sizeof(ukbd_state_t))
1379 bcopy(buf, kbd->kb_data, sizeof(ukbd_state_t));
1384 ukbd_poll(keyboard_t *kbd, int on)
1386 ukbd_state_t *state;
1387 usbd_device_handle dev;
1389 state = (ukbd_state_t *)kbd->kb_data;
1390 usbd_interface2device_handle(state->ks_iface, &dev);
1394 if (state->ks_polling == 0)
1395 usbd_set_polling(dev, on);
1396 ++state->ks_polling;
1398 --state->ks_polling;
1399 if (state->ks_polling == 0)
1400 usbd_set_polling(dev, on);
1406 /* local functions */
1409 probe_keyboard(struct usb_attach_arg *uaa, int flags)
1411 usb_interface_descriptor_t *id;
1413 if (!uaa->iface) /* we attach to ifaces only */
1416 /* Check that this is a keyboard that speaks the boot protocol. */
1417 id = usbd_get_interface_descriptor(uaa->iface);
1419 && id->bInterfaceClass == UICLASS_HID
1420 && id->bInterfaceSubClass == UISUBCLASS_BOOT
1421 && id->bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)
1422 return 0; /* found it */
1428 init_keyboard(ukbd_state_t *state, int *type, int flags)
1430 usb_endpoint_descriptor_t *ed;
1435 state->ks_ifstate |= DISCONNECTED;
1437 ed = usbd_interface2endpoint_descriptor(state->ks_iface, 0);
1439 kprintf("ukbd: could not read endpoint descriptor\n");
1443 DPRINTFN(10,("ukbd:init_keyboard: \
1444 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
1445 ed->bLength, ed->bDescriptorType,
1446 UE_GET_ADDR(ed->bEndpointAddress),
1447 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
1448 UE_GET_XFERTYPE(ed->bmAttributes),
1449 UGETW(ed->wMaxPacketSize), ed->bInterval));
1451 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
1452 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
1453 kprintf("ukbd: unexpected endpoint\n");
1457 if ((usbd_get_quirks(state->ks_uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
1458 err = usbd_set_protocol(state->ks_iface, 0);
1459 DPRINTFN(5, ("ukbd:init_keyboard: protocol set\n"));
1461 kprintf("ukbd: set protocol failed\n");
1465 /* Ignore if SETIDLE fails since it is not crucial. */
1466 usbd_set_idle(state->ks_iface, 0, 0);
1468 state->ks_ep_addr = ed->bEndpointAddress;
1469 state->ks_ifstate &= ~DISCONNECTED;
1475 set_leds(ukbd_state_t *state, int leds)
1477 u_int8_t res = leds;
1479 DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state, leds));
1481 usbd_set_report_async(state->ks_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
1485 set_typematic(keyboard_t *kbd, int code)
1487 static int delays[] = { 250, 500, 750, 1000 };
1488 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1489 68, 76, 84, 92, 100, 110, 118, 126,
1490 136, 152, 168, 184, 200, 220, 236, 252,
1491 272, 304, 336, 368, 400, 440, 472, 504 };
1495 kbd->kb_delay1 = delays[(code >> 5) & 3];
1496 kbd->kb_delay2 = rates[code & 0x1f];
1500 #ifdef UKBD_EMULATE_ATSCANCODE
1502 keycode2scancode(int keycode, int shift, int up)
1504 static int scan[] = {
1506 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1507 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1508 0x50, 0x51, 0x52, 0x53,
1509 0x46, /* XXX Pause/Break */
1515 if ((keycode >= 89) && (keycode < 89 + sizeof(scan)/sizeof(scan[0])))
1516 scancode = scan[keycode - 89] | SCAN_PREFIX_E0;
1518 if ((keycode == 104) && !(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))
1519 scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL;
1520 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R))
1521 scancode &= ~SCAN_PREFIX_SHIFT;
1522 return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS));
1524 #endif /* UKBD_EMULATE_ATSCANCODE */
1527 ukbd_driver_load(module_t mod, int what, void *arg)
1531 kbd_add_driver(&ukbd_kbd_driver);
1534 kbd_delete_driver(&ukbd_kbd_driver);
1537 return usbd_driver_load(mod, what, 0);