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);
123 static keyboard_t default_kbd;
125 static device_probe_t ukbd_match;
126 static device_attach_t ukbd_attach;
127 static device_detach_t ukbd_detach;
129 static devclass_t ukbd_devclass;
131 static kobj_method_t ukbd_methods[] = {
132 DEVMETHOD(device_probe, ukbd_match),
133 DEVMETHOD(device_attach, ukbd_attach),
134 DEVMETHOD(device_detach, ukbd_detach),
135 DEVMETHOD(device_resume, ukbd_resume),
139 static driver_t ukbd_driver = {
142 sizeof(struct ukbd_softc)
145 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
148 ukbd_match(device_t self)
150 struct usb_attach_arg *uaa = device_get_ivars(self);
152 keyboard_switch_t *sw;
154 int unit = device_get_unit(self);
156 sw = kbd_get_switch(DRIVER_NAME);
158 return (UMATCH_NONE);
160 arg[0] = (void *)uaa;
161 arg[1] = (void *)ukbd_intr;
162 if ((*sw->probe)(unit, (void *)arg, 0))
163 return (UMATCH_NONE);
165 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
169 ukbd_attach(device_t self)
171 struct ukbd_softc *sc = device_get_softc(self);
172 struct usb_attach_arg *uaa = device_get_ivars(self);
174 keyboard_switch_t *sw;
177 int unit = device_get_unit(self);
181 sw = kbd_get_switch(DRIVER_NAME);
185 arg[0] = (void *)uaa;
186 arg[1] = (void *)ukbd_intr;
188 if ((*sw->probe)(unit, (void *)arg, 0))
190 if ((*sw->init)(unit, &kbd, (void *)arg, 0))
194 #ifdef KBD_INSTALL_CDEV
199 (*sw->diag)(kbd, bootverbose);
205 ukbd_detach(device_t self)
210 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
211 device_get_unit(self)));
213 DPRINTF(("%s: keyboard not attached!?\n", device_get_nameunit(self)));
218 #ifdef KBD_INSTALL_CDEV
219 error = kbd_detach(kbd);
223 error = kbd_term(kbd);
227 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
233 ukbd_resume(device_t self)
237 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
238 device_get_unit(self)));
240 kbd_clear_state(kbd);
245 ukbd_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
247 keyboard_t *kbd = (keyboard_t *)addr;
249 kbd_intr(kbd, (void *)status);
252 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
255 #define UKBD_DEFAULT 0
257 #define KEY_ERROR 0x01
260 #define KEY_RELEASE 0x400
261 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE)
264 #define SCAN_RELEASE 0x80
265 #define SCAN_PREFIX_E0 0x100
266 #define SCAN_PREFIX_E1 0x200
267 #define SCAN_PREFIX_CTL 0x400
268 #define SCAN_PREFIX_SHIFT 0x800
269 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL \
271 #define SCAN_CHAR(c) ((c) & 0x7f)
276 } ukbd_mods[NMOD] = {
277 { MOD_CONTROL_L, 0xe0 },
278 { MOD_CONTROL_R, 0xe4 },
279 { MOD_SHIFT_L, 0xe1 },
280 { MOD_SHIFT_R, 0xe5 },
287 #define NN 0 /* no translation */
289 * Translate USB keycodes to AT keyboard scancodes.
292 * FIXME: Mac USB keyboard generates:
293 * 0x53: keypad NumLock/Clear
300 static u_int8_t ukbd_trtab[256] = {
301 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
302 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
303 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
304 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
305 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
306 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
307 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
308 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
309 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
310 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
311 97, 100, 95, 69, 91, 55, 74, 78, /* 50 - 57 */
312 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
313 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
314 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
315 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
316 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
317 121, 120, NN, NN, NN, NN, NN, 115, /* 80 - 87 */
318 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */
319 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
320 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
321 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
322 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
323 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
324 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
325 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
326 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
327 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
328 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
329 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
330 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
331 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
332 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
335 typedef struct ukbd_state {
336 usbd_interface_handle ks_iface; /* interface */
337 usbd_pipe_handle ks_intrpipe; /* interrupt pipe */
338 struct usb_attach_arg *ks_uaa;
341 struct ukbd_data ks_ndata;
342 struct ukbd_data ks_odata;
343 u_long ks_ntime[NKEYCODE];
344 u_long ks_otime[NKEYCODE];
346 #define INPUTBUFSIZE (NMOD + 2*NKEYCODE)
347 u_int ks_input[INPUTBUFSIZE]; /* input buffer */
353 #define INTRENABLED (1 << 0)
354 #define DISCONNECTED (1 << 1)
356 struct callout ks_timeout;
358 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
359 int ks_flags; /* flags */
360 #define COMPOSE (1 << 0)
362 int ks_state; /* shift/lock key state */
363 int ks_accents; /* accent key index (> 0) */
364 u_int ks_composed_char; /* composed char code (> 0) */
365 #ifdef UKBD_EMULATE_ATSCANCODE
366 u_int ks_buffered_char[2];
370 /* keyboard driver declaration */
371 static int ukbd_configure(int flags);
372 static kbd_probe_t ukbd_probe;
373 static kbd_init_t ukbd_init;
374 static kbd_term_t ukbd_term;
375 static kbd_intr_t ukbd_interrupt;
376 static kbd_test_if_t ukbd_test_if;
377 static kbd_enable_t ukbd_enable;
378 static kbd_disable_t ukbd_disable;
379 static kbd_read_t ukbd_read;
380 static kbd_check_t ukbd_check;
381 static kbd_read_char_t ukbd_read_char;
382 static kbd_check_char_t ukbd_check_char;
383 static kbd_ioctl_t ukbd_ioctl;
384 static kbd_lock_t ukbd_lock;
385 static kbd_clear_state_t ukbd_clear_state;
386 static kbd_get_state_t ukbd_get_state;
387 static kbd_set_state_t ukbd_set_state;
388 static kbd_poll_mode_t ukbd_poll;
390 keyboard_switch_t ukbdsw = {
412 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
414 /* local functions */
415 static int ukbd_enable_intr(keyboard_t *kbd, int on,
417 static timeout_t ukbd_timeout;
419 static int ukbd_getc(ukbd_state_t *state, int wait);
420 static int probe_keyboard(struct usb_attach_arg *uaa, int flags);
421 static int init_keyboard(ukbd_state_t *state, int *type,
423 static void set_leds(ukbd_state_t *state, int leds);
424 static int set_typematic(keyboard_t *kbd, int code);
425 #ifdef UKBD_EMULATE_ATSCANCODE
426 static int keycode2scancode(int keycode, int shift, int up);
429 /* local variables */
431 /* the initial key map, accent map and fkey strings */
432 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
433 #define KBD_DFLT_KEYMAP
436 #include <dev/misc/kbd/kbdtables.h>
438 /* structures for the default keyboard */
439 static ukbd_state_t default_kbd_state;
440 static keymap_t default_keymap;
441 static accentmap_t default_accentmap;
442 static fkeytab_t default_fkeytab[NUM_FKEYS];
445 * The back door to the keyboard driver!
446 * This function is called by the console driver, via the kbdio module,
447 * to tickle keyboard drivers when the low-level console is being initialized.
448 * Almost nothing in the kernel has been initialied yet. Try to probe
449 * keyboards if possible.
450 * NOTE: because of the way the low-level conole is initialized, this routine
451 * may be called more than once!!
454 ukbd_configure(int flags)
461 struct usb_attach_arg *uaa;
464 device = devclass_get_device(ukbd_devclass, UKBD_DEFAULT);
467 uaa = (struct usb_attach_arg *)device_get_ivars(device);
471 /* probe the default keyboard */
472 arg[0] = (void *)uaa;
473 arg[1] = (void *)ukbd_intr;
475 if (ukbd_probe(UKBD_DEFAULT, arg, flags))
477 if (ukbd_init(UKBD_DEFAULT, &kbd, arg, flags))
480 /* return the number of found keyboards */
485 /* low-level functions */
487 /* detect a keyboard */
489 ukbd_probe(int unit, void *arg, int flags)
492 struct usb_attach_arg *uaa;
495 uaa = (struct usb_attach_arg *)data[0];
497 if (unit == UKBD_DEFAULT) {
498 if (KBD_IS_PROBED(&default_kbd))
501 if (probe_keyboard(uaa, flags))
507 * Reset and initialize the device. Note that unit 0 (UKBD_DEFAULT) is an
508 * always-connected device once it has been initially detected. We do not
509 * deregister it if the usb keyboard is unplugged to avoid losing the
510 * connection to the console. This feature also handles the USB bus reset
511 * which detaches and reattaches USB devices during boot.
514 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
522 void **data = (void **)arg;
523 struct usb_attach_arg *uaa = (struct usb_attach_arg *)data[0];
525 if (unit == UKBD_DEFAULT) {
526 *kbdp = kbd = &default_kbd;
527 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
529 state = &default_kbd_state;
530 keymap = &default_keymap;
531 accmap = &default_accentmap;
532 fkeymap = default_fkeytab;
534 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
535 } else if (*kbdp == NULL) {
536 *kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_INTWAIT | M_ZERO);
537 state = kmalloc(sizeof(*state), M_DEVBUF, M_INTWAIT);
538 keymap = kmalloc(sizeof(key_map), M_DEVBUF, M_INTWAIT);
539 accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_INTWAIT);
540 fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_INTWAIT);
541 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
542 if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
543 || (fkeymap == NULL)) {
545 kfree(state, M_DEVBUF);
547 kfree(keymap, M_DEVBUF);
549 kfree(accmap, M_DEVBUF);
551 kfree(fkeymap, M_DEVBUF);
552 kfree(kbd, M_DEVBUF);
555 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
559 state = (ukbd_state_t *)kbd->kb_data;
560 keymap = kbd->kb_keymap;
561 accmap = kbd->kb_accentmap;
562 fkeymap = kbd->kb_fkeytab;
563 fkeymap_size = kbd->kb_fkeytab_size;
566 if (!KBD_IS_PROBED(kbd)) {
567 kbd_init_struct(kbd, DRIVER_NAME, KB_OTHER,
568 unit, flags, KB_PRI_USB,
570 bzero(state, sizeof(*state));
571 bcopy(&key_map, keymap, sizeof(key_map));
572 bcopy(&accent_map, accmap, sizeof(accent_map));
573 bcopy(fkey_tab, fkeymap,
574 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
575 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
576 kbd->kb_data = (void *)state;
578 if (probe_keyboard(uaa, flags))
581 KBD_FOUND_DEVICE(kbd);
582 ukbd_clear_state(kbd);
585 * If reattatching to an already open keyboard (e.g. console),
586 * try to restore the translation mode. Otherwise set the
587 * translation mode to, well, translation mode so we don't
590 state->ks_mode = K_XLATE;
591 state->ks_iface = uaa->iface;
593 state->ks_ifstate = 0;
594 callout_init(&state->ks_timeout);
596 * FIXME: set the initial value for lock keys in ks_state
597 * according to the BIOS data?
601 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
602 if (KBD_HAS_DEVICE(kbd)
603 && init_keyboard((ukbd_state_t *)kbd->kb_data,
604 &kbd->kb_type, kbd->kb_flags))
606 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&(state->ks_state));
608 if (!KBD_IS_CONFIGURED(kbd)) {
609 if (kbd_register(kbd) < 0) {
611 /* XXX: Missing free()'s */
614 if (ukbd_enable_intr(kbd, TRUE, (usbd_intr_t *)data[1]) == 0)
615 ukbd_timeout((void *)kbd);
616 KBD_CONFIG_DONE(kbd);
622 ukbd_enable_intr(keyboard_t *kbd, int on, usbd_intr_t *func)
624 ukbd_state_t *state = (ukbd_state_t *)kbd->kb_data;
628 /* Set up interrupt pipe. */
629 if (state->ks_ifstate & INTRENABLED)
632 state->ks_ifstate |= INTRENABLED;
633 err = usbd_open_pipe_intr(state->ks_iface, state->ks_ep_addr,
634 USBD_SHORT_XFER_OK | USBD_CALLBACK_LAST,
635 &state->ks_intrpipe, kbd,
637 sizeof(state->ks_ndata), func,
638 USBD_DEFAULT_INTERVAL);
642 /* Disable interrupts. */
643 usbd_abort_pipe(state->ks_intrpipe);
644 usbd_close_pipe(state->ks_intrpipe);
646 state->ks_ifstate &= ~INTRENABLED;
652 /* finish using this keyboard */
654 ukbd_term(keyboard_t *kbd)
660 state = (ukbd_state_t *)kbd->kb_data;
661 DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state->ks_ifstate));
663 callout_stop(&state->ks_timeout);
665 if (state->ks_ifstate & INTRENABLED)
666 ukbd_enable_intr(kbd, FALSE, NULL);
667 if (state->ks_ifstate & INTRENABLED) {
669 DPRINTF(("ukbd_term: INTRENABLED!\n"));
673 error = kbd_unregister(kbd);
674 DPRINTF(("ukbd_term: kbd_unregister() %d\n", error));
677 if (kbd != &default_kbd) {
678 kfree(kbd->kb_keymap, M_DEVBUF);
679 kfree(kbd->kb_accentmap, M_DEVBUF);
680 kfree(kbd->kb_fkeytab, M_DEVBUF);
681 kfree(state, M_DEVBUF);
682 kfree(kbd, M_DEVBUF);
689 /* keyboard interrupt routine */
692 ukbd_timeout(void *arg)
697 kbd = (keyboard_t *)arg;
698 state = (ukbd_state_t *)kbd->kb_data;
700 kbd_intr(kbd, (void *)USBD_NORMAL_COMPLETION);
701 callout_reset(&state->ks_timeout, hz / 40, ukbd_timeout, arg);
706 ukbd_interrupt(keyboard_t *kbd, void *arg)
708 usbd_status status = (usbd_status)arg;
710 struct ukbd_data *ud;
717 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
718 if (status == USBD_CANCELLED)
721 state = (ukbd_state_t *)kbd->kb_data;
722 ud = &state->ks_ndata;
724 if (status != USBD_NORMAL_COMPLETION) {
725 DPRINTF(("ukbd_intr: status=%d\n", status));
726 if (status == USBD_STALLED)
727 usbd_clear_endpoint_stall_async(state->ks_intrpipe);
731 if (ud->keycode[0] == KEY_ERROR)
732 return 0; /* ignore */
735 now = (u_long)tv.tv_sec*1000 + (u_long)tv.tv_usec/1000;
738 if (state->ks_inputs < INPUTBUFSIZE) { \
739 state->ks_input[state->ks_inputtail] = (c); \
740 ++state->ks_inputs; \
741 state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \
745 omod = state->ks_odata.modifiers;
747 for (i = 0; i < NMOD; i++)
748 if (( mod & ukbd_mods[i].mask) !=
749 (omod & ukbd_mods[i].mask))
750 ADDKEY1(ukbd_mods[i].key |
751 (mod & ukbd_mods[i].mask
752 ? KEY_PRESS : KEY_RELEASE));
755 /* Check for released keys. */
756 for (i = 0; i < NKEYCODE; i++) {
757 key = state->ks_odata.keycode[i];
760 for (j = 0; j < NKEYCODE; j++) {
761 if (ud->keycode[j] == 0)
763 if (key == ud->keycode[j])
766 ADDKEY1(key | KEY_RELEASE);
771 /* Check for pressed keys. */
772 for (i = 0; i < NKEYCODE; i++) {
773 key = ud->keycode[i];
776 state->ks_ntime[i] = now + kbd->kb_delay1;
777 for (j = 0; j < NKEYCODE; j++) {
778 if (state->ks_odata.keycode[j] == 0)
780 if (key == state->ks_odata.keycode[j]) {
781 state->ks_ntime[i] = state->ks_otime[j];
782 if (state->ks_otime[j] > now)
784 state->ks_ntime[i] = now + kbd->kb_delay2;
788 ADDKEY1(key | KEY_PRESS);
790 * If any other key is presently down, force its repeat to be
791 * well in the future (100s). This makes the last key to be
792 * pressed do the autorepeat.
794 for (j = 0; j < NKEYCODE; j++) {
796 state->ks_ntime[j] = now + 100 * 1000;
802 state->ks_odata = *ud;
803 bcopy(state->ks_ntime, state->ks_otime, sizeof(state->ks_ntime));
804 if (state->ks_inputs <= 0)
808 for (i = state->ks_inputhead, j = 0; j < state->ks_inputs; ++j,
809 i = (i + 1)%INPUTBUFSIZE) {
810 c = state->ks_input[i];
811 DPRINTF(("0x%x (%d) %s\n", c, c,
812 (c & KEY_RELEASE) ? "released":"pressed"));
815 DPRINTF(("mod:0x%04x ", ud->modifiers));
816 for (i = 0; i < NKEYCODE; i++) {
818 DPRINTF(("%d ", ud->keycode[i]));
821 #endif /* USB_DEBUG */
823 if (state->ks_polling)
826 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
827 /* let the callback function to process the input */
828 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
829 kbd->kb_callback.kc_arg);
831 /* read and discard the input; no one is waiting for it */
833 c = ukbd_read_char(kbd, FALSE);
834 } while (c != NOKEY);
841 ukbd_getc(ukbd_state_t *state, int wait)
845 if (state->ks_polling) {
846 DPRINTFN(1,("ukbd_getc: polling\n"));
848 while (state->ks_inputs <= 0) {
849 usbd_dopoll(state->ks_iface);
856 if (state->ks_inputs <= 0) {
859 c = state->ks_input[state->ks_inputhead];
861 state->ks_inputhead = (state->ks_inputhead + 1)%INPUTBUFSIZE;
867 /* test the interface to the device */
869 ukbd_test_if(keyboard_t *kbd)
875 * Enable the access to the device; until this function is called,
876 * the client cannot read from the keyboard.
879 ukbd_enable(keyboard_t *kbd)
887 /* disallow the access to the device */
889 ukbd_disable(keyboard_t *kbd)
897 /* read one byte from the keyboard if it's allowed */
899 ukbd_read(keyboard_t *kbd, int wait)
903 #ifdef UKBD_EMULATE_ATSCANCODE
908 state = (ukbd_state_t *)kbd->kb_data;
909 #ifdef UKBD_EMULATE_ATSCANCODE
910 if (state->ks_buffered_char[0]) {
911 scancode = state->ks_buffered_char[0];
912 if (scancode & SCAN_PREFIX) {
913 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
914 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
916 state->ks_buffered_char[0] = state->ks_buffered_char[1];
917 state->ks_buffered_char[1] = 0;
921 #endif /* UKBD_EMULATE_ATSCANCODE */
923 usbcode = ukbd_getc(state, wait);
924 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
927 #ifdef UKBD_EMULATE_ATSCANCODE
928 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
932 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
933 usbcode & KEY_RELEASE);
934 if (scancode & SCAN_PREFIX) {
935 if (scancode & SCAN_PREFIX_CTL) {
936 state->ks_buffered_char[0] =
937 0x1d | (scancode & SCAN_RELEASE); /* Ctrl */
938 state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX;
939 } else if (scancode & SCAN_PREFIX_SHIFT) {
940 state->ks_buffered_char[0] =
941 0x2a | (scancode & SCAN_RELEASE); /* Shift */
942 state->ks_buffered_char[1] =
943 scancode & ~SCAN_PREFIX_SHIFT;
945 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
946 state->ks_buffered_char[1] = 0;
948 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
951 #else /* !UKBD_EMULATE_ATSCANCODE */
953 #endif /* UKBD_EMULATE_ATSCANCODE */
956 /* check if data is waiting */
958 ukbd_check(keyboard_t *kbd)
960 if (!KBD_IS_ACTIVE(kbd))
962 #ifdef UKBD_EMULATE_ATSCANCODE
963 if (((ukbd_state_t *)kbd->kb_data)->ks_buffered_char[0])
966 if (((ukbd_state_t *)kbd->kb_data)->ks_inputs > 0)
971 /* read char from the keyboard */
973 ukbd_read_char(keyboard_t *kbd, int wait)
979 #ifdef UKBD_EMULATE_ATSCANCODE
983 state = (ukbd_state_t *)kbd->kb_data;
985 /* do we have a composed char to return? */
986 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
987 action = state->ks_composed_char;
988 state->ks_composed_char = 0;
989 if (action > UCHAR_MAX)
994 #ifdef UKBD_EMULATE_ATSCANCODE
995 /* do we have a pending raw scan code? */
996 if (state->ks_mode == K_RAW) {
997 if (state->ks_buffered_char[0]) {
998 scancode = state->ks_buffered_char[0];
999 if (scancode & SCAN_PREFIX) {
1000 state->ks_buffered_char[0] =
1001 scancode & ~SCAN_PREFIX;
1002 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1004 state->ks_buffered_char[0] =
1005 state->ks_buffered_char[1];
1006 state->ks_buffered_char[1] = 0;
1011 #endif /* UKBD_EMULATE_ATSCANCODE */
1013 /* see if there is something in the keyboard port */
1015 usbcode = ukbd_getc(state, wait);
1020 #ifdef UKBD_EMULATE_ATSCANCODE
1021 /* USB key index -> key code -> AT scan code */
1022 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1026 /* return an AT scan code for the K_RAW mode */
1027 if (state->ks_mode == K_RAW) {
1028 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
1029 usbcode & KEY_RELEASE);
1030 if (scancode & SCAN_PREFIX) {
1031 if (scancode & SCAN_PREFIX_CTL) {
1032 state->ks_buffered_char[0] =
1033 0x1d | (scancode & SCAN_RELEASE);
1034 state->ks_buffered_char[1] =
1035 scancode & ~SCAN_PREFIX;
1036 } else if (scancode & SCAN_PREFIX_SHIFT) {
1037 state->ks_buffered_char[0] =
1038 0x2a | (scancode & SCAN_RELEASE);
1039 state->ks_buffered_char[1] =
1040 scancode & ~SCAN_PREFIX_SHIFT;
1042 state->ks_buffered_char[0] =
1043 scancode & ~SCAN_PREFIX;
1044 state->ks_buffered_char[1] = 0;
1046 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1050 #else /* !UKBD_EMULATE_ATSCANCODE */
1051 /* return the byte as is for the K_RAW mode */
1052 if (state->ks_mode == K_RAW)
1055 /* USB key index -> key code */
1056 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1059 #endif /* UKBD_EMULATE_ATSCANCODE */
1062 case 0x38: /* left alt (compose key) */
1063 if (usbcode & KEY_RELEASE) {
1064 if (state->ks_flags & COMPOSE) {
1065 state->ks_flags &= ~COMPOSE;
1066 if (state->ks_composed_char > UCHAR_MAX)
1067 state->ks_composed_char = 0;
1070 if (!(state->ks_flags & COMPOSE)) {
1071 state->ks_flags |= COMPOSE;
1072 state->ks_composed_char = 0;
1076 /* XXX: I don't like these... */
1077 case 0x5c: /* print screen */
1078 if (state->ks_flags & ALTS)
1079 keycode = 0x54; /* sysrq */
1081 case 0x68: /* pause/break */
1082 if (state->ks_flags & CTLS)
1083 keycode = 0x6c; /* break */
1087 /* return the key code in the K_CODE mode */
1088 if (usbcode & KEY_RELEASE)
1089 keycode |= SCAN_RELEASE;
1090 if (state->ks_mode == K_CODE)
1093 /* compose a character code */
1094 if (state->ks_flags & COMPOSE) {
1096 /* key pressed, process it */
1097 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
1098 state->ks_composed_char *= 10;
1099 state->ks_composed_char += keycode - 0x40;
1100 if (state->ks_composed_char > UCHAR_MAX)
1103 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
1104 state->ks_composed_char *= 10;
1105 state->ks_composed_char += keycode - 0x47;
1106 if (state->ks_composed_char > UCHAR_MAX)
1109 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
1110 state->ks_composed_char *= 10;
1111 state->ks_composed_char += keycode - 0x4E;
1112 if (state->ks_composed_char > UCHAR_MAX)
1115 case 0x52: /* keypad 0 */
1116 state->ks_composed_char *= 10;
1117 if (state->ks_composed_char > UCHAR_MAX)
1121 /* key released, no interest here */
1122 case SCAN_RELEASE | 0x47:
1123 case SCAN_RELEASE | 0x48:
1124 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1125 case SCAN_RELEASE | 0x4B:
1126 case SCAN_RELEASE | 0x4C:
1127 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1128 case SCAN_RELEASE | 0x4F:
1129 case SCAN_RELEASE | 0x50:
1130 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1131 case SCAN_RELEASE | 0x52: /* keypad 0 */
1134 case 0x38: /* left alt key */
1138 if (state->ks_composed_char > 0) {
1139 state->ks_flags &= ~COMPOSE;
1140 state->ks_composed_char = 0;
1147 /* keycode to key action */
1148 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1149 keycode & SCAN_RELEASE, &state->ks_state,
1150 &state->ks_accents);
1151 if (action == NOKEY)
1157 /* check if char is waiting */
1159 ukbd_check_char(keyboard_t *kbd)
1161 ukbd_state_t *state;
1163 if (!KBD_IS_ACTIVE(kbd))
1165 state = (ukbd_state_t *)kbd->kb_data;
1166 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
1168 return (ukbd_check(kbd));
1171 /* some useful control functions */
1173 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1175 /* trasnlate LED_XXX bits into the device specific bits */
1176 static u_char ledmap[8] = {
1177 0, 2, 1, 3, 4, 6, 5, 7,
1179 ukbd_state_t *state = kbd->kb_data;
1184 case KDGKBMODE: /* get keyboard mode */
1185 *(int *)arg = state->ks_mode;
1187 case KDSKBMODE: /* set keyboard mode */
1188 switch (*(int *)arg) {
1190 if (state->ks_mode != K_XLATE) {
1191 /* make lock key state and LED state match */
1192 state->ks_state &= ~LOCK_MASK;
1193 state->ks_state |= KBD_LED_VAL(kbd);
1198 if (state->ks_mode != *(int *)arg) {
1199 ukbd_clear_state(kbd);
1200 state->ks_mode = *(int *)arg;
1201 kbd->kb_savemode = state->ks_mode;
1210 case KDGETLED: /* get keyboard LED */
1211 *(int *)arg = KBD_LED_VAL(kbd);
1213 case KDSETLED: /* set keyboard LED */
1214 /* NOTE: lock key state in ks_state won't be changed */
1215 if (*(int *)arg & ~LOCK_MASK) {
1220 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1221 if (state->ks_mode == K_XLATE &&
1222 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1228 if (KBD_HAS_DEVICE(kbd)) {
1229 set_leds(state, ledmap[i & LED_MASK]);
1230 /* XXX: error check? */
1232 KBD_LED_VAL(kbd) = *(int *)arg;
1235 case KDGKBSTATE: /* get lock key state */
1236 *(int *)arg = state->ks_state & LOCK_MASK;
1238 case KDSKBSTATE: /* set lock key state */
1239 if (*(int *)arg & ~LOCK_MASK) {
1243 state->ks_state &= ~LOCK_MASK;
1244 state->ks_state |= *(int *)arg;
1246 /* set LEDs and quit */
1247 return ukbd_ioctl(kbd, KDSETLED, arg);
1249 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
1251 if (!KBD_HAS_DEVICE(kbd))
1253 if (((int *)arg)[1] < 0)
1255 if (((int *)arg)[0] < 0)
1257 else if (((int *)arg)[0] == 0) /* fastest possible value */
1258 kbd->kb_delay1 = 200;
1260 kbd->kb_delay1 = ((int *)arg)[0];
1261 kbd->kb_delay2 = ((int *)arg)[1];
1264 case KDSETRAD: /* set keyboard repeat rate (old interface) */
1266 return set_typematic(kbd, *(int *)arg);
1268 case PIO_KEYMAP: /* set keyboard translation table */
1269 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1270 case PIO_DEADKEYMAP: /* set accent key translation table */
1271 state->ks_accents = 0;
1275 return genkbd_commonioctl(kbd, cmd, arg);
1279 ukbddebug = *(int *)arg;
1288 /* lock the access to the keyboard */
1290 ukbd_lock(keyboard_t *kbd, int lock)
1296 /* clear the internal state of the keyboard */
1298 ukbd_clear_state(keyboard_t *kbd)
1300 ukbd_state_t *state;
1302 state = (ukbd_state_t *)kbd->kb_data;
1303 state->ks_flags = 0;
1304 state->ks_polling = 0;
1305 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1306 state->ks_accents = 0;
1307 state->ks_composed_char = 0;
1308 #ifdef UKBD_EMULATE_ATSCANCODE
1309 state->ks_buffered_char[0] = 0;
1310 state->ks_buffered_char[1] = 0;
1312 bzero(&state->ks_ndata, sizeof(state->ks_ndata));
1313 bzero(&state->ks_odata, sizeof(state->ks_odata));
1314 bzero(&state->ks_ntime, sizeof(state->ks_ntime));
1315 bzero(&state->ks_otime, sizeof(state->ks_otime));
1318 /* save the internal state */
1320 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1323 return sizeof(ukbd_state_t);
1324 if (len < sizeof(ukbd_state_t))
1326 bcopy(kbd->kb_data, buf, sizeof(ukbd_state_t));
1330 /* set the internal state */
1332 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1334 if (len < sizeof(ukbd_state_t))
1336 bcopy(buf, kbd->kb_data, sizeof(ukbd_state_t));
1341 ukbd_poll(keyboard_t *kbd, int on)
1343 ukbd_state_t *state;
1344 usbd_device_handle dev;
1346 state = (ukbd_state_t *)kbd->kb_data;
1347 usbd_interface2device_handle(state->ks_iface, &dev);
1351 ++state->ks_polling;
1352 if (state->ks_polling == 1)
1353 usbd_set_polling(dev, on);
1355 --state->ks_polling;
1356 if (state->ks_polling == 0)
1357 usbd_set_polling(dev, on);
1363 /* local functions */
1366 probe_keyboard(struct usb_attach_arg *uaa, int flags)
1368 usb_interface_descriptor_t *id;
1370 if (!uaa->iface) /* we attach to ifaces only */
1373 /* Check that this is a keyboard that speaks the boot protocol. */
1374 id = usbd_get_interface_descriptor(uaa->iface);
1376 && id->bInterfaceClass == UICLASS_HID
1377 && id->bInterfaceSubClass == UISUBCLASS_BOOT
1378 && id->bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)
1379 return 0; /* found it */
1385 init_keyboard(ukbd_state_t *state, int *type, int flags)
1387 usb_endpoint_descriptor_t *ed;
1392 state->ks_ifstate |= DISCONNECTED;
1394 ed = usbd_interface2endpoint_descriptor(state->ks_iface, 0);
1396 kprintf("ukbd: could not read endpoint descriptor\n");
1400 DPRINTFN(10,("ukbd:init_keyboard: \
1401 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
1402 ed->bLength, ed->bDescriptorType,
1403 UE_GET_ADDR(ed->bEndpointAddress),
1404 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
1405 UE_GET_XFERTYPE(ed->bmAttributes),
1406 UGETW(ed->wMaxPacketSize), ed->bInterval));
1408 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
1409 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
1410 kprintf("ukbd: unexpected endpoint\n");
1414 if ((usbd_get_quirks(state->ks_uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
1415 err = usbd_set_protocol(state->ks_iface, 0);
1416 DPRINTFN(5, ("ukbd:init_keyboard: protocol set\n"));
1418 kprintf("ukbd: set protocol failed\n");
1422 /* Ignore if SETIDLE fails since it is not crucial. */
1423 usbd_set_idle(state->ks_iface, 0, 0);
1425 state->ks_ep_addr = ed->bEndpointAddress;
1426 state->ks_ifstate &= ~DISCONNECTED;
1432 set_leds(ukbd_state_t *state, int leds)
1434 u_int8_t res = leds;
1436 DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state, leds));
1438 usbd_set_report_async(state->ks_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
1442 set_typematic(keyboard_t *kbd, int code)
1444 static int delays[] = { 250, 500, 750, 1000 };
1445 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1446 68, 76, 84, 92, 100, 110, 118, 126,
1447 136, 152, 168, 184, 200, 220, 236, 252,
1448 272, 304, 336, 368, 400, 440, 472, 504 };
1452 kbd->kb_delay1 = delays[(code >> 5) & 3];
1453 kbd->kb_delay2 = rates[code & 0x1f];
1457 #ifdef UKBD_EMULATE_ATSCANCODE
1459 keycode2scancode(int keycode, int shift, int up)
1461 static int scan[] = {
1463 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1464 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1465 0x50, 0x51, 0x52, 0x53,
1466 0x46, /* XXX Pause/Break */
1472 if ((keycode >= 89) && (keycode < 89 + sizeof(scan)/sizeof(scan[0])))
1473 scancode = scan[keycode - 89] | SCAN_PREFIX_E0;
1475 if ((keycode == 104) && !(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))
1476 scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL;
1477 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R))
1478 scancode &= ~SCAN_PREFIX_SHIFT;
1479 return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS));
1481 #endif /* UKBD_EMULATE_ATSCANCODE */
1484 ukbd_driver_load(module_t mod, int what, void *arg)
1488 kbd_add_driver(&ukbd_kbd_driver);
1491 kbd_delete_driver(&ukbd_kbd_driver);
1494 return usbd_driver_load(mod, what, 0);