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.27 2008/08/14 20:55:53 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/module.h>
56 #include <machine/limits.h>
57 #include <sys/select.h>
58 #include <sys/sysctl.h>
59 #include <sys/thread2.h>
61 #include <bus/usb/usb.h>
62 #include <bus/usb/usbhid.h>
63 #include <bus/usb/usbdi.h>
64 #include <bus/usb/usbdi_util.h>
65 #include <bus/usb/usb_quirks.h>
66 #include <bus/usb/hid.h>
69 #include <dev/misc/kbd/kbdreg.h>
71 #define UKBD_EMULATE_ATSCANCODE 1
73 #define DRIVER_NAME "ukbd"
75 #define delay(d) DELAY(d)
78 #define DPRINTF(x) if (ukbddebug) kprintf x
79 #define DPRINTFN(n,x) if (ukbddebug>(n)) kprintf x
81 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
82 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
83 &ukbddebug, 0, "ukbd debug level");
89 #define UPROTO_BOOT_KEYBOARD 1
95 #define MOD_CONTROL_L 0x01
96 #define MOD_CONTROL_R 0x10
97 #define MOD_SHIFT_L 0x02
98 #define MOD_SHIFT_R 0x20
99 #define MOD_ALT_L 0x04
100 #define MOD_ALT_R 0x40
101 #define MOD_WIN_L 0x08
102 #define MOD_WIN_R 0x80
104 u_int8_t keycode[NKEYCODE];
107 #define MAXKEYS (NMOD+2*NKEYCODE)
109 typedef struct ukbd_softc {
110 device_t sc_dev; /* base device */
113 #define UKBD_CHUNK 128 /* chunk size for read */
114 #define UKBD_BSIZE 1020 /* buffer size */
116 typedef void usbd_intr_t(usbd_xfer_handle, usbd_private_handle, usbd_status);
117 typedef void usbd_disco_t(void *);
119 static int ukbd_resume(device_t self);
120 static usbd_intr_t ukbd_intr;
121 static int ukbd_driver_load(module_t mod, int what, void *arg);
122 static int ukbd_default_term(keyboard_t *kbd);
124 static keyboard_t default_kbd;
126 static device_probe_t ukbd_match;
127 static device_attach_t ukbd_attach;
128 static device_detach_t ukbd_detach;
130 static devclass_t ukbd_devclass;
132 static kobj_method_t ukbd_methods[] = {
133 DEVMETHOD(device_probe, ukbd_match),
134 DEVMETHOD(device_attach, ukbd_attach),
135 DEVMETHOD(device_detach, ukbd_detach),
136 DEVMETHOD(device_resume, ukbd_resume),
140 static driver_t ukbd_driver = {
143 sizeof(struct ukbd_softc)
146 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
149 ukbd_match(device_t self)
151 struct usb_attach_arg *uaa = device_get_ivars(self);
153 keyboard_switch_t *sw;
155 int unit = device_get_unit(self);
157 sw = kbd_get_switch(DRIVER_NAME);
159 return (UMATCH_NONE);
161 arg[0] = (void *)uaa;
162 arg[1] = (void *)ukbd_intr;
163 if ((*sw->probe)(unit, (void *)arg, 0))
164 return (UMATCH_NONE);
166 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
170 ukbd_attach(device_t self)
172 struct ukbd_softc *sc = device_get_softc(self);
173 struct usb_attach_arg *uaa = device_get_ivars(self);
175 keyboard_switch_t *sw;
178 int unit = device_get_unit(self);
180 sw = kbd_get_switch(DRIVER_NAME);
186 arg[0] = (void *)uaa;
187 arg[1] = (void *)ukbd_intr;
189 if ((*sw->probe)(unit, (void *)arg, 0))
191 if ((*sw->init)(unit, &kbd, (void *)arg, 0))
195 #ifdef KBD_INSTALL_CDEV
200 (*sw->diag)(kbd, bootverbose);
206 ukbd_detach(device_t self)
211 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
212 device_get_unit(self)));
214 DPRINTF(("%s: keyboard not attached!?\n", device_get_nameunit(self)));
217 (*kbdsw[kbd->kb_index]->disable)(kbd);
219 #ifdef KBD_INSTALL_CDEV
220 if (kbd != &default_kbd) {
221 error = kbd_detach(kbd);
226 if (kbd == &default_kbd) {
227 ukbd_default_term(kbd);
229 error = (*kbdsw[kbd->kb_index]->term)(kbd);
234 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
240 ukbd_resume(device_t self)
244 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
245 device_get_unit(self)));
247 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
252 ukbd_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
254 keyboard_t *kbd = (keyboard_t *)addr;
256 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)status);
259 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
262 #define UKBD_DEFAULT 0
264 #define KEY_ERROR 0x01
267 #define KEY_RELEASE 0x400
268 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE)
271 #define SCAN_RELEASE 0x80
272 #define SCAN_PREFIX_E0 0x100
273 #define SCAN_PREFIX_E1 0x200
274 #define SCAN_PREFIX_CTL 0x400
275 #define SCAN_PREFIX_SHIFT 0x800
276 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL \
278 #define SCAN_CHAR(c) ((c) & 0x7f)
283 } ukbd_mods[NMOD] = {
284 { MOD_CONTROL_L, 0xe0 },
285 { MOD_CONTROL_R, 0xe4 },
286 { MOD_SHIFT_L, 0xe1 },
287 { MOD_SHIFT_R, 0xe5 },
294 #define NN 0 /* no translation */
296 * Translate USB keycodes to AT keyboard scancodes.
299 * FIXME: Mac USB keyboard generates:
300 * 0x53: keypad NumLock/Clear
307 static u_int8_t ukbd_trtab[256] = {
308 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
309 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
310 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
311 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
312 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
313 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
314 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
315 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
316 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
317 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
318 97, 100, 95, 69, 91, 55, 74, 78, /* 50 - 57 */
319 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
320 72, 73, 82, 83, 86, 107, NN, NN, /* 60 - 67 */
321 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
322 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
323 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7F */
324 NN, NN, NN, NN, NN, NN, NN, 115, /* 80 - 87 */
325 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */
326 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
327 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
328 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
329 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
330 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
331 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
332 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
333 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
334 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
335 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
336 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
337 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
338 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
339 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
342 typedef struct ukbd_state {
343 usbd_interface_handle ks_iface; /* interface */
344 usbd_pipe_handle ks_intrpipe; /* interrupt pipe */
345 struct usb_attach_arg *ks_uaa;
348 struct ukbd_data ks_ndata;
349 struct ukbd_data ks_odata;
350 u_long ks_ntime[NKEYCODE];
351 u_long ks_otime[NKEYCODE];
353 #define INPUTBUFSIZE (NMOD + 2*NKEYCODE)
354 u_int ks_input[INPUTBUFSIZE]; /* input buffer */
360 #define INTRENABLED (1 << 0)
361 #define DISCONNECTED (1 << 1)
363 struct callout ks_timeout;
365 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
366 int ks_flags; /* flags */
367 #define COMPOSE (1 << 0)
369 int ks_state; /* shift/lock key state */
370 int ks_accents; /* accent key index (> 0) */
371 u_int ks_composed_char; /* composed char code (> 0) */
372 #ifdef UKBD_EMULATE_ATSCANCODE
373 u_int ks_buffered_char[2];
377 /* keyboard driver declaration */
378 static int ukbd_configure(int flags);
379 static kbd_probe_t ukbd_probe;
380 static kbd_init_t ukbd_init;
381 static kbd_term_t ukbd_term;
382 static kbd_intr_t ukbd_interrupt;
383 static kbd_test_if_t ukbd_test_if;
384 static kbd_enable_t ukbd_enable;
385 static kbd_disable_t ukbd_disable;
386 static kbd_read_t ukbd_read;
387 static kbd_check_t ukbd_check;
388 static kbd_read_char_t ukbd_read_char;
389 static kbd_check_char_t ukbd_check_char;
390 static kbd_ioctl_t ukbd_ioctl;
391 static kbd_lock_t ukbd_lock;
392 static kbd_clear_state_t ukbd_clear_state;
393 static kbd_get_state_t ukbd_get_state;
394 static kbd_set_state_t ukbd_set_state;
395 static kbd_poll_mode_t ukbd_poll;
397 keyboard_switch_t ukbdsw = {
419 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
421 /* local functions */
422 static int ukbd_enable_intr(keyboard_t *kbd, int on,
424 static timeout_t ukbd_timeout;
426 static int ukbd_getc(ukbd_state_t *state, int wait);
427 static int probe_keyboard(struct usb_attach_arg *uaa, int flags);
428 static int init_keyboard(ukbd_state_t *state, int *type,
430 static void set_leds(ukbd_state_t *state, int leds);
431 static int set_typematic(keyboard_t *kbd, int code);
432 #ifdef UKBD_EMULATE_ATSCANCODE
433 static int keycode2scancode(int keycode, int shift, int up);
436 /* local variables */
438 /* the initial key map, accent map and fkey strings */
439 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
440 #define KBD_DFLT_KEYMAP
443 #include <dev/misc/kbd/kbdtables.h>
445 /* structures for the default keyboard */
446 static ukbd_state_t default_kbd_state;
447 static keymap_t default_keymap;
448 static accentmap_t default_accentmap;
449 static fkeytab_t default_fkeytab[NUM_FKEYS];
452 * The back door to the keyboard driver!
453 * This function is called by the console driver, via the kbdio module,
454 * to tickle keyboard drivers when the low-level console is being initialized.
455 * Almost nothing in the kernel has been initialied yet. Try to probe
456 * keyboards if possible.
457 * NOTE: because of the way the low-level conole is initialized, this routine
458 * may be called more than once!!
461 ukbd_configure(int flags)
468 struct usb_attach_arg *uaa;
471 device = devclass_get_device(ukbd_devclass, UKBD_DEFAULT);
474 uaa = (struct usb_attach_arg *)device_get_ivars(device);
478 /* probe the default keyboard */
479 arg[0] = (void *)uaa;
480 arg[1] = (void *)ukbd_intr;
482 if (ukbd_probe(UKBD_DEFAULT, arg, flags))
484 if (ukbd_init(UKBD_DEFAULT, &kbd, arg, flags))
487 /* return the number of found keyboards */
492 /* low-level functions */
494 /* detect a keyboard */
496 ukbd_probe(int unit, void *arg, int flags)
499 struct usb_attach_arg *uaa;
502 uaa = (struct usb_attach_arg *)data[0];
504 if (unit == UKBD_DEFAULT) {
505 if (KBD_IS_PROBED(&default_kbd))
508 if (probe_keyboard(uaa, flags))
514 * Reset and initialize the device. Note that unit 0 (UKBD_DEFAULT) is an
515 * always-connected device once it has been initially detected. We do not
516 * deregister it if the usb keyboard is unplugged to avoid losing the
517 * connection to the console. This feature also handles the USB bus reset
518 * which detaches and reattaches USB devices during boot.
521 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
529 void **data = (void **)arg;
530 struct usb_attach_arg *uaa = (struct usb_attach_arg *)data[0];
532 if (unit == UKBD_DEFAULT) {
533 *kbdp = kbd = &default_kbd;
534 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
536 state = &default_kbd_state;
537 keymap = &default_keymap;
538 accmap = &default_accentmap;
539 fkeymap = default_fkeytab;
541 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
542 } else if (*kbdp == NULL) {
543 *kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_INTWAIT | M_ZERO);
544 state = kmalloc(sizeof(*state), M_DEVBUF, M_INTWAIT);
545 keymap = kmalloc(sizeof(key_map), M_DEVBUF, M_INTWAIT);
546 accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_INTWAIT);
547 fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_INTWAIT);
548 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
549 if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
550 || (fkeymap == NULL)) {
552 kfree(state, M_DEVBUF);
554 kfree(keymap, M_DEVBUF);
556 kfree(accmap, M_DEVBUF);
558 kfree(fkeymap, M_DEVBUF);
559 kfree(kbd, M_DEVBUF);
562 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
566 state = (ukbd_state_t *)kbd->kb_data;
567 keymap = kbd->kb_keymap;
568 accmap = kbd->kb_accentmap;
569 fkeymap = kbd->kb_fkeytab;
570 fkeymap_size = kbd->kb_fkeytab_size;
573 if (!KBD_IS_PROBED(kbd)) {
574 if (KBD_IS_CONFIGURED(kbd)) {
575 kbd_reinit_struct(kbd, flags, KB_PRI_USB);
577 kbd_init_struct(kbd, DRIVER_NAME, KB_OTHER,
578 unit, flags, KB_PRI_USB,
581 bzero(state, sizeof(*state));
582 bcopy(&key_map, keymap, sizeof(key_map));
583 bcopy(&accent_map, accmap, sizeof(accent_map));
584 bcopy(fkey_tab, fkeymap,
585 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
586 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
587 kbd->kb_data = (void *)state;
589 if (probe_keyboard(uaa, flags))
592 KBD_FOUND_DEVICE(kbd);
593 ukbd_clear_state(kbd);
596 * If reattatching to an already open keyboard (e.g. console),
597 * try to restore the translation mode. Otherwise set the
598 * translation mode to, well, translation mode so we don't
601 if (!KBD_IS_CONFIGURED(kbd)) {
602 state->ks_mode = K_XLATE;
603 kbd->kb_savemode = state->ks_mode;
605 state->ks_mode = kbd->kb_savemode;
607 state->ks_iface = uaa->iface;
609 state->ks_ifstate = 0;
610 callout_init(&state->ks_timeout);
612 * FIXME: set the initial value for lock keys in ks_state
613 * according to the BIOS data?
617 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
618 if (KBD_HAS_DEVICE(kbd)
619 && init_keyboard((ukbd_state_t *)kbd->kb_data,
620 &kbd->kb_type, kbd->kb_flags))
622 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&(state->ks_state));
624 if (!KBD_IS_CONFIGURED(kbd)) {
625 if (kbd_register(kbd) < 0)
627 KBD_CONFIG_DONE(kbd);
629 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
630 if (ukbd_enable_intr(kbd, TRUE, (usbd_intr_t *)data[1]) == 0)
631 ukbd_timeout((void *)kbd);
638 ukbd_enable_intr(keyboard_t *kbd, int on, usbd_intr_t *func)
640 ukbd_state_t *state = (ukbd_state_t *)kbd->kb_data;
644 /* Set up interrupt pipe. */
645 if (state->ks_ifstate & INTRENABLED)
648 state->ks_ifstate |= INTRENABLED;
649 err = usbd_open_pipe_intr(state->ks_iface, state->ks_ep_addr,
650 USBD_SHORT_XFER_OK | USBD_CALLBACK_LAST,
651 &state->ks_intrpipe, kbd,
653 sizeof(state->ks_ndata), func,
654 USBD_DEFAULT_INTERVAL);
658 /* Disable interrupts. */
659 usbd_abort_pipe(state->ks_intrpipe);
660 usbd_close_pipe(state->ks_intrpipe);
662 state->ks_ifstate &= ~INTRENABLED;
668 /* finish using this keyboard */
670 ukbd_term(keyboard_t *kbd)
676 state = (ukbd_state_t *)kbd->kb_data;
677 DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state->ks_ifstate));
679 callout_stop(&state->ks_timeout);
681 if (state->ks_ifstate & INTRENABLED)
682 ukbd_enable_intr(kbd, FALSE, NULL);
683 if (state->ks_ifstate & INTRENABLED) {
685 DPRINTF(("ukbd_term: INTRENABLED!\n"));
689 error = kbd_unregister(kbd);
690 DPRINTF(("ukbd_term: kbd_unregister() %d\n", error));
693 if (kbd != &default_kbd) {
694 kfree(kbd->kb_keymap, M_DEVBUF);
695 kfree(kbd->kb_accentmap, M_DEVBUF);
696 kfree(kbd->kb_fkeytab, M_DEVBUF);
697 kfree(state, M_DEVBUF);
698 kfree(kbd, M_DEVBUF);
706 * Finish using the default keyboard. Shutdown the USB side of the keyboard
707 * but do not unregister it.
710 ukbd_default_term(keyboard_t *kbd)
716 state = (ukbd_state_t *)kbd->kb_data;
717 DPRINTF(("ukbd_default_term: ks_ifstate=0x%x\n", state->ks_ifstate));
719 callout_stop(&state->ks_timeout);
721 if (state->ks_ifstate & INTRENABLED)
722 ukbd_enable_intr(kbd, FALSE, NULL);
723 if (state->ks_ifstate & INTRENABLED) {
725 DPRINTF(("ukbd_term: INTRENABLED!\n"));
728 KBD_LOST_DEVICE(kbd);
735 /* keyboard interrupt routine */
738 ukbd_timeout(void *arg)
743 kbd = (keyboard_t *)arg;
744 state = (ukbd_state_t *)kbd->kb_data;
746 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)USBD_NORMAL_COMPLETION);
747 callout_reset(&state->ks_timeout, hz / 40, ukbd_timeout, arg);
752 ukbd_interrupt(keyboard_t *kbd, void *arg)
754 usbd_status status = (usbd_status)arg;
756 struct ukbd_data *ud;
763 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
764 if (status == USBD_CANCELLED)
767 state = (ukbd_state_t *)kbd->kb_data;
768 ud = &state->ks_ndata;
770 if (status != USBD_NORMAL_COMPLETION) {
771 DPRINTF(("ukbd_intr: status=%d\n", status));
772 if (status == USBD_STALLED)
773 usbd_clear_endpoint_stall_async(state->ks_intrpipe);
777 if (ud->keycode[0] == KEY_ERROR)
778 return 0; /* ignore */
781 now = (u_long)tv.tv_sec*1000 + (u_long)tv.tv_usec/1000;
784 if (state->ks_inputs < INPUTBUFSIZE) { \
785 state->ks_input[state->ks_inputtail] = (c); \
786 ++state->ks_inputs; \
787 state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \
791 omod = state->ks_odata.modifiers;
793 for (i = 0; i < NMOD; i++)
794 if (( mod & ukbd_mods[i].mask) !=
795 (omod & ukbd_mods[i].mask))
796 ADDKEY1(ukbd_mods[i].key |
797 (mod & ukbd_mods[i].mask
798 ? KEY_PRESS : KEY_RELEASE));
801 /* Check for released keys. */
802 for (i = 0; i < NKEYCODE; i++) {
803 key = state->ks_odata.keycode[i];
806 for (j = 0; j < NKEYCODE; j++) {
807 if (ud->keycode[j] == 0)
809 if (key == ud->keycode[j])
812 ADDKEY1(key | KEY_RELEASE);
817 /* Check for pressed keys. */
818 for (i = 0; i < NKEYCODE; i++) {
819 key = ud->keycode[i];
822 state->ks_ntime[i] = now + kbd->kb_delay1;
823 for (j = 0; j < NKEYCODE; j++) {
824 if (state->ks_odata.keycode[j] == 0)
826 if (key == state->ks_odata.keycode[j]) {
827 state->ks_ntime[i] = state->ks_otime[j];
828 if (state->ks_otime[j] > now)
830 state->ks_ntime[i] = now + kbd->kb_delay2;
834 ADDKEY1(key | KEY_PRESS);
839 state->ks_odata = *ud;
840 bcopy(state->ks_ntime, state->ks_otime, sizeof(state->ks_ntime));
841 if (state->ks_inputs <= 0)
845 for (i = state->ks_inputhead, j = 0; j < state->ks_inputs; ++j,
846 i = (i + 1)%INPUTBUFSIZE) {
847 c = state->ks_input[i];
848 DPRINTF(("0x%x (%d) %s\n", c, c,
849 (c & KEY_RELEASE) ? "released":"pressed"));
852 DPRINTF(("mod:0x%04x ", ud->modifiers));
853 for (i = 0; i < NKEYCODE; i++) {
855 DPRINTF(("%d ", ud->keycode[i]));
858 #endif /* USB_DEBUG */
860 if (state->ks_polling)
863 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
864 /* let the callback function to process the input */
865 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
866 kbd->kb_callback.kc_arg);
868 /* read and discard the input; no one is waiting for it */
870 c = ukbd_read_char(kbd, FALSE);
871 } while (c != NOKEY);
878 ukbd_getc(ukbd_state_t *state, int wait)
882 if (state->ks_polling) {
883 DPRINTFN(1,("ukbd_getc: polling\n"));
885 while (state->ks_inputs <= 0) {
886 usbd_dopoll(state->ks_iface);
893 if (state->ks_inputs <= 0) {
896 c = state->ks_input[state->ks_inputhead];
898 state->ks_inputhead = (state->ks_inputhead + 1)%INPUTBUFSIZE;
904 /* test the interface to the device */
906 ukbd_test_if(keyboard_t *kbd)
912 * Enable the access to the device; until this function is called,
913 * the client cannot read from the keyboard.
916 ukbd_enable(keyboard_t *kbd)
924 /* disallow the access to the device */
926 ukbd_disable(keyboard_t *kbd)
934 /* read one byte from the keyboard if it's allowed */
936 ukbd_read(keyboard_t *kbd, int wait)
940 #ifdef UKBD_EMULATE_ATSCANCODE
945 state = (ukbd_state_t *)kbd->kb_data;
946 #ifdef UKBD_EMULATE_ATSCANCODE
947 if (state->ks_buffered_char[0]) {
948 scancode = state->ks_buffered_char[0];
949 if (scancode & SCAN_PREFIX) {
950 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
951 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
953 state->ks_buffered_char[0] = state->ks_buffered_char[1];
954 state->ks_buffered_char[1] = 0;
958 #endif /* UKBD_EMULATE_ATSCANCODE */
960 usbcode = ukbd_getc(state, wait);
961 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd) || (usbcode == -1))
964 #ifdef UKBD_EMULATE_ATSCANCODE
965 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
969 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
970 usbcode & KEY_RELEASE);
971 if (scancode & SCAN_PREFIX) {
972 if (scancode & SCAN_PREFIX_CTL) {
973 state->ks_buffered_char[0] =
974 0x1d | (scancode & SCAN_RELEASE); /* Ctrl */
975 state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX;
976 } else if (scancode & SCAN_PREFIX_SHIFT) {
977 state->ks_buffered_char[0] =
978 0x2a | (scancode & SCAN_RELEASE); /* Shift */
979 state->ks_buffered_char[1] =
980 scancode & ~SCAN_PREFIX_SHIFT;
982 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
983 state->ks_buffered_char[1] = 0;
985 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
988 #else /* !UKBD_EMULATE_ATSCANCODE */
990 #endif /* UKBD_EMULATE_ATSCANCODE */
993 /* check if data is waiting */
995 ukbd_check(keyboard_t *kbd)
997 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd))
999 #ifdef UKBD_EMULATE_ATSCANCODE
1000 if (((ukbd_state_t *)kbd->kb_data)->ks_buffered_char[0])
1003 if (((ukbd_state_t *)kbd->kb_data)->ks_inputs > 0)
1008 /* read char from the keyboard */
1010 ukbd_read_char(keyboard_t *kbd, int wait)
1012 ukbd_state_t *state;
1016 #ifdef UKBD_EMULATE_ATSCANCODE
1020 state = (ukbd_state_t *)kbd->kb_data;
1022 /* do we have a composed char to return? */
1023 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
1024 action = state->ks_composed_char;
1025 state->ks_composed_char = 0;
1026 if (action > UCHAR_MAX)
1031 #ifdef UKBD_EMULATE_ATSCANCODE
1032 /* do we have a pending raw scan code? */
1033 if (state->ks_mode == K_RAW) {
1034 if (state->ks_buffered_char[0]) {
1035 scancode = state->ks_buffered_char[0];
1036 if (scancode & SCAN_PREFIX) {
1037 state->ks_buffered_char[0] =
1038 scancode & ~SCAN_PREFIX;
1039 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1041 state->ks_buffered_char[0] =
1042 state->ks_buffered_char[1];
1043 state->ks_buffered_char[1] = 0;
1048 #endif /* UKBD_EMULATE_ATSCANCODE */
1050 /* see if there is something in the keyboard port */
1052 usbcode = ukbd_getc(state, wait);
1057 #ifdef UKBD_EMULATE_ATSCANCODE
1058 /* USB key index -> key code -> AT scan code */
1059 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1063 /* return an AT scan code for the K_RAW mode */
1064 if (state->ks_mode == K_RAW) {
1065 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
1066 usbcode & KEY_RELEASE);
1067 if (scancode & SCAN_PREFIX) {
1068 if (scancode & SCAN_PREFIX_CTL) {
1069 state->ks_buffered_char[0] =
1070 0x1d | (scancode & SCAN_RELEASE);
1071 state->ks_buffered_char[1] =
1072 scancode & ~SCAN_PREFIX;
1073 } else if (scancode & SCAN_PREFIX_SHIFT) {
1074 state->ks_buffered_char[0] =
1075 0x2a | (scancode & SCAN_RELEASE);
1076 state->ks_buffered_char[1] =
1077 scancode & ~SCAN_PREFIX_SHIFT;
1079 state->ks_buffered_char[0] =
1080 scancode & ~SCAN_PREFIX;
1081 state->ks_buffered_char[1] = 0;
1083 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1087 #else /* !UKBD_EMULATE_ATSCANCODE */
1088 /* return the byte as is for the K_RAW mode */
1089 if (state->ks_mode == K_RAW)
1092 /* USB key index -> key code */
1093 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1096 #endif /* UKBD_EMULATE_ATSCANCODE */
1099 case 0x38: /* left alt (compose key) */
1100 if (usbcode & KEY_RELEASE) {
1101 if (state->ks_flags & COMPOSE) {
1102 state->ks_flags &= ~COMPOSE;
1103 if (state->ks_composed_char > UCHAR_MAX)
1104 state->ks_composed_char = 0;
1107 if (!(state->ks_flags & COMPOSE)) {
1108 state->ks_flags |= COMPOSE;
1109 state->ks_composed_char = 0;
1113 /* XXX: I don't like these... */
1114 case 0x5c: /* print screen */
1115 if (state->ks_flags & ALTS)
1116 keycode = 0x54; /* sysrq */
1118 case 0x68: /* pause/break */
1119 if (state->ks_flags & CTLS)
1120 keycode = 0x6c; /* break */
1124 /* return the key code in the K_CODE mode */
1125 if (usbcode & KEY_RELEASE)
1126 keycode |= SCAN_RELEASE;
1127 if (state->ks_mode == K_CODE)
1130 /* compose a character code */
1131 if (state->ks_flags & COMPOSE) {
1133 /* key pressed, process it */
1134 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
1135 state->ks_composed_char *= 10;
1136 state->ks_composed_char += keycode - 0x40;
1137 if (state->ks_composed_char > UCHAR_MAX)
1140 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
1141 state->ks_composed_char *= 10;
1142 state->ks_composed_char += keycode - 0x47;
1143 if (state->ks_composed_char > UCHAR_MAX)
1146 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
1147 state->ks_composed_char *= 10;
1148 state->ks_composed_char += keycode - 0x4E;
1149 if (state->ks_composed_char > UCHAR_MAX)
1152 case 0x52: /* keypad 0 */
1153 state->ks_composed_char *= 10;
1154 if (state->ks_composed_char > UCHAR_MAX)
1158 /* key released, no interest here */
1159 case SCAN_RELEASE | 0x47:
1160 case SCAN_RELEASE | 0x48:
1161 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1162 case SCAN_RELEASE | 0x4B:
1163 case SCAN_RELEASE | 0x4C:
1164 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1165 case SCAN_RELEASE | 0x4F:
1166 case SCAN_RELEASE | 0x50:
1167 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1168 case SCAN_RELEASE | 0x52: /* keypad 0 */
1171 case 0x38: /* left alt key */
1175 if (state->ks_composed_char > 0) {
1176 state->ks_flags &= ~COMPOSE;
1177 state->ks_composed_char = 0;
1184 /* keycode to key action */
1185 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1186 keycode & SCAN_RELEASE, &state->ks_state,
1187 &state->ks_accents);
1188 if (action == NOKEY)
1194 /* check if char is waiting */
1196 ukbd_check_char(keyboard_t *kbd)
1198 ukbd_state_t *state;
1200 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd))
1202 state = (ukbd_state_t *)kbd->kb_data;
1203 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
1205 return (ukbd_check(kbd));
1208 /* some useful control functions */
1210 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1212 /* trasnlate LED_XXX bits into the device specific bits */
1213 static u_char ledmap[8] = {
1214 0, 2, 1, 3, 4, 6, 5, 7,
1216 ukbd_state_t *state = kbd->kb_data;
1221 case KDGKBMODE: /* get keyboard mode */
1222 *(int *)arg = state->ks_mode;
1224 case KDSKBMODE: /* set keyboard mode */
1225 switch (*(int *)arg) {
1227 if (state->ks_mode != K_XLATE) {
1228 /* make lock key state and LED state match */
1229 state->ks_state &= ~LOCK_MASK;
1230 state->ks_state |= KBD_LED_VAL(kbd);
1235 if (state->ks_mode != *(int *)arg) {
1236 ukbd_clear_state(kbd);
1237 state->ks_mode = *(int *)arg;
1238 kbd->kb_savemode = state->ks_mode;
1247 case KDGETLED: /* get keyboard LED */
1248 *(int *)arg = KBD_LED_VAL(kbd);
1250 case KDSETLED: /* set keyboard LED */
1251 /* NOTE: lock key state in ks_state won't be changed */
1252 if (*(int *)arg & ~LOCK_MASK) {
1257 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1258 if (kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1264 if (KBD_HAS_DEVICE(kbd)) {
1265 set_leds(state, ledmap[i & LED_MASK]);
1266 /* XXX: error check? */
1268 KBD_LED_VAL(kbd) = *(int *)arg;
1271 case KDGKBSTATE: /* get lock key state */
1272 *(int *)arg = state->ks_state & LOCK_MASK;
1274 case KDSKBSTATE: /* set lock key state */
1275 if (*(int *)arg & ~LOCK_MASK) {
1279 state->ks_state &= ~LOCK_MASK;
1280 state->ks_state |= *(int *)arg;
1282 /* set LEDs and quit */
1283 return ukbd_ioctl(kbd, KDSETLED, arg);
1285 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
1287 if (!KBD_HAS_DEVICE(kbd))
1289 if (((int *)arg)[1] < 0)
1291 if (((int *)arg)[0] < 0)
1293 else if (((int *)arg)[0] == 0) /* fastest possible value */
1294 kbd->kb_delay1 = 200;
1296 kbd->kb_delay1 = ((int *)arg)[0];
1297 kbd->kb_delay2 = ((int *)arg)[1];
1300 case KDSETRAD: /* set keyboard repeat rate (old interface) */
1302 return set_typematic(kbd, *(int *)arg);
1304 case PIO_KEYMAP: /* set keyboard translation table */
1305 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1306 case PIO_DEADKEYMAP: /* set accent key translation table */
1307 state->ks_accents = 0;
1311 return genkbd_commonioctl(kbd, cmd, arg);
1315 ukbddebug = *(int *)arg;
1324 /* lock the access to the keyboard */
1326 ukbd_lock(keyboard_t *kbd, int lock)
1332 /* clear the internal state of the keyboard */
1334 ukbd_clear_state(keyboard_t *kbd)
1336 ukbd_state_t *state;
1338 state = (ukbd_state_t *)kbd->kb_data;
1339 state->ks_flags = 0;
1340 state->ks_polling = 0;
1341 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1342 state->ks_accents = 0;
1343 state->ks_composed_char = 0;
1344 #ifdef UKBD_EMULATE_ATSCANCODE
1345 state->ks_buffered_char[0] = 0;
1346 state->ks_buffered_char[1] = 0;
1348 bzero(&state->ks_ndata, sizeof(state->ks_ndata));
1349 bzero(&state->ks_odata, sizeof(state->ks_odata));
1350 bzero(&state->ks_ntime, sizeof(state->ks_ntime));
1351 bzero(&state->ks_otime, sizeof(state->ks_otime));
1354 /* save the internal state */
1356 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1359 return sizeof(ukbd_state_t);
1360 if (len < sizeof(ukbd_state_t))
1362 bcopy(kbd->kb_data, buf, sizeof(ukbd_state_t));
1366 /* set the internal state */
1368 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1370 if (len < sizeof(ukbd_state_t))
1372 bcopy(buf, kbd->kb_data, sizeof(ukbd_state_t));
1377 ukbd_poll(keyboard_t *kbd, int on)
1379 ukbd_state_t *state;
1380 usbd_device_handle dev;
1382 state = (ukbd_state_t *)kbd->kb_data;
1383 usbd_interface2device_handle(state->ks_iface, &dev);
1387 if (state->ks_polling == 0)
1388 usbd_set_polling(dev, on);
1389 ++state->ks_polling;
1391 --state->ks_polling;
1392 if (state->ks_polling == 0)
1393 usbd_set_polling(dev, on);
1399 /* local functions */
1402 probe_keyboard(struct usb_attach_arg *uaa, int flags)
1404 usb_interface_descriptor_t *id;
1406 if (!uaa->iface) /* we attach to ifaces only */
1409 /* Check that this is a keyboard that speaks the boot protocol. */
1410 id = usbd_get_interface_descriptor(uaa->iface);
1412 && id->bInterfaceClass == UICLASS_HID
1413 && id->bInterfaceSubClass == UISUBCLASS_BOOT
1414 && id->bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)
1415 return 0; /* found it */
1421 init_keyboard(ukbd_state_t *state, int *type, int flags)
1423 usb_endpoint_descriptor_t *ed;
1428 state->ks_ifstate |= DISCONNECTED;
1430 ed = usbd_interface2endpoint_descriptor(state->ks_iface, 0);
1432 kprintf("ukbd: could not read endpoint descriptor\n");
1436 DPRINTFN(10,("ukbd:init_keyboard: \
1437 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
1438 ed->bLength, ed->bDescriptorType,
1439 UE_GET_ADDR(ed->bEndpointAddress),
1440 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
1441 UE_GET_XFERTYPE(ed->bmAttributes),
1442 UGETW(ed->wMaxPacketSize), ed->bInterval));
1444 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
1445 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
1446 kprintf("ukbd: unexpected endpoint\n");
1450 if ((usbd_get_quirks(state->ks_uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
1451 err = usbd_set_protocol(state->ks_iface, 0);
1452 DPRINTFN(5, ("ukbd:init_keyboard: protocol set\n"));
1454 kprintf("ukbd: set protocol failed\n");
1458 /* Ignore if SETIDLE fails since it is not crucial. */
1459 usbd_set_idle(state->ks_iface, 0, 0);
1461 state->ks_ep_addr = ed->bEndpointAddress;
1462 state->ks_ifstate &= ~DISCONNECTED;
1468 set_leds(ukbd_state_t *state, int leds)
1470 u_int8_t res = leds;
1472 DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state, leds));
1474 usbd_set_report_async(state->ks_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
1478 set_typematic(keyboard_t *kbd, int code)
1480 static int delays[] = { 250, 500, 750, 1000 };
1481 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1482 68, 76, 84, 92, 100, 110, 118, 126,
1483 136, 152, 168, 184, 200, 220, 236, 252,
1484 272, 304, 336, 368, 400, 440, 472, 504 };
1488 kbd->kb_delay1 = delays[(code >> 5) & 3];
1489 kbd->kb_delay2 = rates[code & 0x1f];
1493 #ifdef UKBD_EMULATE_ATSCANCODE
1495 keycode2scancode(int keycode, int shift, int up)
1497 static int scan[] = {
1499 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1500 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1501 0x50, 0x51, 0x52, 0x53,
1502 0x46, /* XXX Pause/Break */
1508 if ((keycode >= 89) && (keycode < 89 + sizeof(scan)/sizeof(scan[0])))
1509 scancode = scan[keycode - 89] | SCAN_PREFIX_E0;
1511 if ((keycode == 104) && !(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))
1512 scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL;
1513 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R))
1514 scancode &= ~SCAN_PREFIX_SHIFT;
1515 return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS));
1517 #endif /* UKBD_EMULATE_ATSCANCODE */
1520 ukbd_driver_load(module_t mod, int what, void *arg)
1524 kbd_add_driver(&ukbd_kbd_driver);
1527 kbd_delete_driver(&ukbd_kbd_driver);
1530 return usbd_driver_load(mod, what, 0);