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.24 2007/08/07 10:42:40 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/usbdevs.h>
67 #include <bus/usb/usb_quirks.h>
68 #include <bus/usb/hid.h>
71 #include <dev/misc/kbd/kbdreg.h>
73 #define UKBD_EMULATE_ATSCANCODE 1
75 #define DRIVER_NAME "ukbd"
77 #define delay(d) DELAY(d)
80 #define DPRINTF(x) if (ukbddebug) kprintf x
81 #define DPRINTFN(n,x) if (ukbddebug>(n)) kprintf x
83 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
84 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
85 &ukbddebug, 0, "ukbd debug level");
91 #define UPROTO_BOOT_KEYBOARD 1
97 #define MOD_CONTROL_L 0x01
98 #define MOD_CONTROL_R 0x10
99 #define MOD_SHIFT_L 0x02
100 #define MOD_SHIFT_R 0x20
101 #define MOD_ALT_L 0x04
102 #define MOD_ALT_R 0x40
103 #define MOD_WIN_L 0x08
104 #define MOD_WIN_R 0x80
106 u_int8_t keycode[NKEYCODE];
109 #define MAXKEYS (NMOD+2*NKEYCODE)
111 typedef struct ukbd_softc {
112 device_t sc_dev; /* base device */
115 #define UKBD_CHUNK 128 /* chunk size for read */
116 #define UKBD_BSIZE 1020 /* buffer size */
118 typedef void usbd_intr_t(usbd_xfer_handle, usbd_private_handle, usbd_status);
119 typedef void usbd_disco_t(void *);
121 static int ukbd_resume(device_t self);
122 static usbd_intr_t ukbd_intr;
123 static int ukbd_driver_load(module_t mod, int what, void *arg);
124 static int ukbd_default_term(keyboard_t *kbd);
126 static keyboard_t default_kbd;
128 static device_probe_t ukbd_match;
129 static device_attach_t ukbd_attach;
130 static device_detach_t ukbd_detach;
132 static devclass_t ukbd_devclass;
134 static kobj_method_t ukbd_methods[] = {
135 DEVMETHOD(device_probe, ukbd_match),
136 DEVMETHOD(device_attach, ukbd_attach),
137 DEVMETHOD(device_detach, ukbd_detach),
138 DEVMETHOD(device_resume, ukbd_resume),
142 static driver_t ukbd_driver = {
145 sizeof(struct ukbd_softc)
148 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
151 ukbd_match(device_t self)
153 struct usb_attach_arg *uaa = device_get_ivars(self);
155 keyboard_switch_t *sw;
157 int unit = device_get_unit(self);
159 sw = kbd_get_switch(DRIVER_NAME);
161 return (UMATCH_NONE);
163 arg[0] = (void *)uaa;
164 arg[1] = (void *)ukbd_intr;
165 if ((*sw->probe)(unit, (void *)arg, 0))
166 return (UMATCH_NONE);
168 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
172 ukbd_attach(device_t self)
174 struct ukbd_softc *sc = device_get_softc(self);
175 struct usb_attach_arg *uaa = device_get_ivars(self);
176 usbd_interface_handle iface = uaa->iface;
177 usb_interface_descriptor_t *id;
180 keyboard_switch_t *sw;
183 int unit = device_get_unit(self);
185 sw = kbd_get_switch(DRIVER_NAME);
189 id = usbd_get_interface_descriptor(iface);
190 usbd_devinfo(uaa->device, 0, devinfo);
192 device_set_desc_copy(self, devinfo);
193 kprintf("%s: %s, iclass %d/%d\n", device_get_nameunit(sc->sc_dev),
194 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
196 arg[0] = (void *)uaa;
197 arg[1] = (void *)ukbd_intr;
199 if ((*sw->probe)(unit, (void *)arg, 0))
201 if ((*sw->init)(unit, &kbd, (void *)arg, 0))
205 #ifdef KBD_INSTALL_CDEV
210 (*sw->diag)(kbd, bootverbose);
216 ukbd_detach(device_t self)
221 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
222 device_get_unit(self)));
224 DPRINTF(("%s: keyboard not attached!?\n", device_get_nameunit(self)));
227 (*kbdsw[kbd->kb_index]->disable)(kbd);
229 #ifdef KBD_INSTALL_CDEV
230 if (kbd != &default_kbd) {
231 error = kbd_detach(kbd);
236 if (kbd == &default_kbd) {
237 ukbd_default_term(kbd);
239 error = (*kbdsw[kbd->kb_index]->term)(kbd);
244 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
250 ukbd_resume(device_t self)
254 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
255 device_get_unit(self)));
257 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
262 ukbd_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
264 keyboard_t *kbd = (keyboard_t *)addr;
266 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)status);
269 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
272 #define UKBD_DEFAULT 0
274 #define KEY_ERROR 0x01
277 #define KEY_RELEASE 0x400
278 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE)
281 #define SCAN_RELEASE 0x80
282 #define SCAN_PREFIX_E0 0x100
283 #define SCAN_PREFIX_E1 0x200
284 #define SCAN_PREFIX_CTL 0x400
285 #define SCAN_PREFIX_SHIFT 0x800
286 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL \
288 #define SCAN_CHAR(c) ((c) & 0x7f)
293 } ukbd_mods[NMOD] = {
294 { MOD_CONTROL_L, 0xe0 },
295 { MOD_CONTROL_R, 0xe4 },
296 { MOD_SHIFT_L, 0xe1 },
297 { MOD_SHIFT_R, 0xe5 },
304 #define NN 0 /* no translation */
306 * Translate USB keycodes to AT keyboard scancodes.
309 * FIXME: Mac USB keyboard generates:
310 * 0x53: keypad NumLock/Clear
317 static u_int8_t ukbd_trtab[256] = {
318 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
319 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
320 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
321 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
322 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
323 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
324 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
325 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
326 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
327 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
328 97, 100, 95, 69, 91, 55, 74, 78, /* 50 - 57 */
329 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
330 72, 73, 82, 83, 86, 107, NN, NN, /* 60 - 67 */
331 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
332 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
333 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7F */
334 NN, NN, NN, NN, NN, NN, NN, 115, /* 80 - 87 */
335 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */
336 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
337 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
338 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
339 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
340 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
341 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
342 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
343 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
344 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
345 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
346 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
347 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
348 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
349 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
352 typedef struct ukbd_state {
353 usbd_interface_handle ks_iface; /* interface */
354 usbd_pipe_handle ks_intrpipe; /* interrupt pipe */
355 struct usb_attach_arg *ks_uaa;
358 struct ukbd_data ks_ndata;
359 struct ukbd_data ks_odata;
360 u_long ks_ntime[NKEYCODE];
361 u_long ks_otime[NKEYCODE];
363 #define INPUTBUFSIZE (NMOD + 2*NKEYCODE)
364 u_int ks_input[INPUTBUFSIZE]; /* input buffer */
370 #define INTRENABLED (1 << 0)
371 #define DISCONNECTED (1 << 1)
373 struct callout ks_timeout;
375 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
376 int ks_flags; /* flags */
377 #define COMPOSE (1 << 0)
379 int ks_state; /* shift/lock key state */
380 int ks_accents; /* accent key index (> 0) */
381 u_int ks_composed_char; /* composed char code (> 0) */
382 #ifdef UKBD_EMULATE_ATSCANCODE
383 u_int ks_buffered_char[2];
387 /* keyboard driver declaration */
388 static int ukbd_configure(int flags);
389 static kbd_probe_t ukbd_probe;
390 static kbd_init_t ukbd_init;
391 static kbd_term_t ukbd_term;
392 static kbd_intr_t ukbd_interrupt;
393 static kbd_test_if_t ukbd_test_if;
394 static kbd_enable_t ukbd_enable;
395 static kbd_disable_t ukbd_disable;
396 static kbd_read_t ukbd_read;
397 static kbd_check_t ukbd_check;
398 static kbd_read_char_t ukbd_read_char;
399 static kbd_check_char_t ukbd_check_char;
400 static kbd_ioctl_t ukbd_ioctl;
401 static kbd_lock_t ukbd_lock;
402 static kbd_clear_state_t ukbd_clear_state;
403 static kbd_get_state_t ukbd_get_state;
404 static kbd_set_state_t ukbd_set_state;
405 static kbd_poll_mode_t ukbd_poll;
407 keyboard_switch_t ukbdsw = {
429 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
431 /* local functions */
432 static int ukbd_enable_intr(keyboard_t *kbd, int on,
434 static timeout_t ukbd_timeout;
436 static int ukbd_getc(ukbd_state_t *state);
437 static int probe_keyboard(struct usb_attach_arg *uaa, int flags);
438 static int init_keyboard(ukbd_state_t *state, int *type,
440 static void set_leds(ukbd_state_t *state, int leds);
441 static int set_typematic(keyboard_t *kbd, int code);
442 #ifdef UKBD_EMULATE_ATSCANCODE
443 static int keycode2scancode(int keycode, int shift, int up);
446 /* local variables */
448 /* the initial key map, accent map and fkey strings */
449 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
450 #define KBD_DFLT_KEYMAP
453 #include <dev/misc/kbd/kbdtables.h>
455 /* structures for the default keyboard */
456 static ukbd_state_t default_kbd_state;
457 static keymap_t default_keymap;
458 static accentmap_t default_accentmap;
459 static fkeytab_t default_fkeytab[NUM_FKEYS];
462 * The back door to the keyboard driver!
463 * This function is called by the console driver, via the kbdio module,
464 * to tickle keyboard drivers when the low-level console is being initialized.
465 * Almost nothing in the kernel has been initialied yet. Try to probe
466 * keyboards if possible.
467 * NOTE: because of the way the low-level conole is initialized, this routine
468 * may be called more than once!!
471 ukbd_configure(int flags)
478 struct usb_attach_arg *uaa;
481 device = devclass_get_device(ukbd_devclass, UKBD_DEFAULT);
484 uaa = (struct usb_attach_arg *)device_get_ivars(device);
488 /* probe the default keyboard */
489 arg[0] = (void *)uaa;
490 arg[1] = (void *)ukbd_intr;
492 if (ukbd_probe(UKBD_DEFAULT, arg, flags))
494 if (ukbd_init(UKBD_DEFAULT, &kbd, arg, flags))
497 /* return the number of found keyboards */
502 /* low-level functions */
504 /* detect a keyboard */
506 ukbd_probe(int unit, void *arg, int flags)
509 struct usb_attach_arg *uaa;
512 uaa = (struct usb_attach_arg *)data[0];
514 if (unit == UKBD_DEFAULT) {
515 if (KBD_IS_PROBED(&default_kbd))
518 if (probe_keyboard(uaa, flags))
524 * Reset and initialize the device. Note that unit 0 (UKBD_DEFAULT) is an
525 * always-connected device once it has been initially detected. We do not
526 * deregister it if the usb keyboard is unplugged to avoid losing the
527 * connection to the console. This feature also handles the USB bus reset
528 * which detaches and reattaches USB devices during boot.
531 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
539 void **data = (void **)arg;
540 struct usb_attach_arg *uaa = (struct usb_attach_arg *)data[0];
542 if (unit == UKBD_DEFAULT) {
543 *kbdp = kbd = &default_kbd;
544 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
546 state = &default_kbd_state;
547 keymap = &default_keymap;
548 accmap = &default_accentmap;
549 fkeymap = default_fkeytab;
551 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
552 } else if (*kbdp == NULL) {
553 *kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_INTWAIT | M_ZERO);
554 state = kmalloc(sizeof(*state), M_DEVBUF, M_INTWAIT);
555 keymap = kmalloc(sizeof(key_map), M_DEVBUF, M_INTWAIT);
556 accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_INTWAIT);
557 fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_INTWAIT);
558 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
559 if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
560 || (fkeymap == NULL)) {
562 kfree(state, M_DEVBUF);
564 kfree(keymap, M_DEVBUF);
566 kfree(accmap, M_DEVBUF);
568 kfree(fkeymap, M_DEVBUF);
569 kfree(kbd, M_DEVBUF);
572 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
576 state = (ukbd_state_t *)kbd->kb_data;
577 keymap = kbd->kb_keymap;
578 accmap = kbd->kb_accentmap;
579 fkeymap = kbd->kb_fkeytab;
580 fkeymap_size = kbd->kb_fkeytab_size;
583 if (!KBD_IS_PROBED(kbd)) {
584 if (KBD_IS_CONFIGURED(kbd)) {
585 kbd_reinit_struct(kbd, flags, KB_PRI_USB);
587 kbd_init_struct(kbd, DRIVER_NAME, KB_OTHER,
588 unit, flags, KB_PRI_USB,
591 bzero(state, sizeof(*state));
592 bcopy(&key_map, keymap, sizeof(key_map));
593 bcopy(&accent_map, accmap, sizeof(accent_map));
594 bcopy(fkey_tab, fkeymap,
595 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
596 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
597 kbd->kb_data = (void *)state;
599 if (probe_keyboard(uaa, flags))
602 KBD_FOUND_DEVICE(kbd);
603 ukbd_clear_state(kbd);
606 * If reattatching to an already open keyboard (e.g. console),
607 * try to restore the translation mode. Otherwise set the
608 * translation mode to, well, translation mode so we don't
611 if (!KBD_IS_CONFIGURED(kbd)) {
612 state->ks_mode = K_XLATE;
613 kbd->kb_savemode = state->ks_mode;
615 state->ks_mode = kbd->kb_savemode;
617 state->ks_iface = uaa->iface;
619 state->ks_ifstate = 0;
620 callout_init(&state->ks_timeout);
622 * FIXME: set the initial value for lock keys in ks_state
623 * according to the BIOS data?
627 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
628 if (KBD_HAS_DEVICE(kbd)
629 && init_keyboard((ukbd_state_t *)kbd->kb_data,
630 &kbd->kb_type, kbd->kb_flags))
632 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&(state->ks_state));
634 if (!KBD_IS_CONFIGURED(kbd)) {
635 if (kbd_register(kbd) < 0)
637 KBD_CONFIG_DONE(kbd);
639 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
640 if (ukbd_enable_intr(kbd, TRUE, (usbd_intr_t *)data[1]) == 0)
641 ukbd_timeout((void *)kbd);
648 ukbd_enable_intr(keyboard_t *kbd, int on, usbd_intr_t *func)
650 ukbd_state_t *state = (ukbd_state_t *)kbd->kb_data;
654 /* Set up interrupt pipe. */
655 if (state->ks_ifstate & INTRENABLED)
658 state->ks_ifstate |= INTRENABLED;
659 err = usbd_open_pipe_intr(state->ks_iface, state->ks_ep_addr,
661 &state->ks_intrpipe, kbd,
663 sizeof(state->ks_ndata), func,
664 USBD_DEFAULT_INTERVAL);
668 /* Disable interrupts. */
669 usbd_abort_pipe(state->ks_intrpipe);
670 usbd_close_pipe(state->ks_intrpipe);
672 state->ks_ifstate &= ~INTRENABLED;
678 /* finish using this keyboard */
680 ukbd_term(keyboard_t *kbd)
686 state = (ukbd_state_t *)kbd->kb_data;
687 DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state->ks_ifstate));
689 callout_stop(&state->ks_timeout);
691 if (state->ks_ifstate & INTRENABLED)
692 ukbd_enable_intr(kbd, FALSE, NULL);
693 if (state->ks_ifstate & INTRENABLED) {
695 DPRINTF(("ukbd_term: INTRENABLED!\n"));
699 error = kbd_unregister(kbd);
700 DPRINTF(("ukbd_term: kbd_unregister() %d\n", error));
703 if (kbd != &default_kbd) {
704 kfree(kbd->kb_keymap, M_DEVBUF);
705 kfree(kbd->kb_accentmap, M_DEVBUF);
706 kfree(kbd->kb_fkeytab, M_DEVBUF);
707 kfree(state, M_DEVBUF);
708 kfree(kbd, M_DEVBUF);
716 * Finish using the default keyboard. Shutdown the USB side of the keyboard
717 * but do not unregister it.
720 ukbd_default_term(keyboard_t *kbd)
726 state = (ukbd_state_t *)kbd->kb_data;
727 DPRINTF(("ukbd_default_term: ks_ifstate=0x%x\n", state->ks_ifstate));
729 callout_stop(&state->ks_timeout);
731 if (state->ks_ifstate & INTRENABLED)
732 ukbd_enable_intr(kbd, FALSE, NULL);
733 if (state->ks_ifstate & INTRENABLED) {
735 DPRINTF(("ukbd_term: INTRENABLED!\n"));
738 KBD_LOST_DEVICE(kbd);
745 /* keyboard interrupt routine */
748 ukbd_timeout(void *arg)
753 kbd = (keyboard_t *)arg;
754 state = (ukbd_state_t *)kbd->kb_data;
756 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)USBD_NORMAL_COMPLETION);
757 callout_reset(&state->ks_timeout, hz / 40, ukbd_timeout, arg);
762 ukbd_interrupt(keyboard_t *kbd, void *arg)
764 usbd_status status = (usbd_status)arg;
766 struct ukbd_data *ud;
773 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
774 if (status == USBD_CANCELLED)
777 state = (ukbd_state_t *)kbd->kb_data;
778 ud = &state->ks_ndata;
780 if (status != USBD_NORMAL_COMPLETION) {
781 DPRINTF(("ukbd_intr: status=%d\n", status));
782 if (status == USBD_STALLED)
783 usbd_clear_endpoint_stall_async(state->ks_intrpipe);
787 if (ud->keycode[0] == KEY_ERROR)
788 return 0; /* ignore */
791 now = (u_long)tv.tv_sec*1000 + (u_long)tv.tv_usec/1000;
794 if (state->ks_inputs < INPUTBUFSIZE) { \
795 state->ks_input[state->ks_inputtail] = (c); \
796 ++state->ks_inputs; \
797 state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \
801 omod = state->ks_odata.modifiers;
803 for (i = 0; i < NMOD; i++)
804 if (( mod & ukbd_mods[i].mask) !=
805 (omod & ukbd_mods[i].mask))
806 ADDKEY1(ukbd_mods[i].key |
807 (mod & ukbd_mods[i].mask
808 ? KEY_PRESS : KEY_RELEASE));
811 /* Check for released keys. */
812 for (i = 0; i < NKEYCODE; i++) {
813 key = state->ks_odata.keycode[i];
816 for (j = 0; j < NKEYCODE; j++) {
817 if (ud->keycode[j] == 0)
819 if (key == ud->keycode[j])
822 ADDKEY1(key | KEY_RELEASE);
827 /* Check for pressed keys. */
828 for (i = 0; i < NKEYCODE; i++) {
829 key = ud->keycode[i];
832 state->ks_ntime[i] = now + kbd->kb_delay1;
833 for (j = 0; j < NKEYCODE; j++) {
834 if (state->ks_odata.keycode[j] == 0)
836 if (key == state->ks_odata.keycode[j]) {
837 state->ks_ntime[i] = state->ks_otime[j];
838 if (state->ks_otime[j] > now)
840 state->ks_ntime[i] = now + kbd->kb_delay2;
844 ADDKEY1(key | KEY_PRESS);
849 state->ks_odata = *ud;
850 bcopy(state->ks_ntime, state->ks_otime, sizeof(state->ks_ntime));
851 if (state->ks_inputs <= 0)
855 for (i = state->ks_inputhead, j = 0; j < state->ks_inputs; ++j,
856 i = (i + 1)%INPUTBUFSIZE) {
857 c = state->ks_input[i];
858 DPRINTF(("0x%x (%d) %s\n", c, c,
859 (c & KEY_RELEASE) ? "released":"pressed"));
862 DPRINTF(("mod:0x%04x ", ud->modifiers));
863 for (i = 0; i < NKEYCODE; i++) {
865 DPRINTF(("%d ", ud->keycode[i]));
868 #endif /* USB_DEBUG */
870 if (state->ks_polling)
873 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
874 /* let the callback function to process the input */
875 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
876 kbd->kb_callback.kc_arg);
878 /* read and discard the input; no one is waiting for it */
880 c = ukbd_read_char(kbd, FALSE);
881 } while (c != NOKEY);
888 ukbd_getc(ukbd_state_t *state)
892 if (state->ks_polling) {
893 DPRINTFN(1,("ukbd_getc: polling\n"));
895 while (state->ks_inputs <= 0)
896 usbd_dopoll(state->ks_iface);
900 if (state->ks_inputs <= 0) {
903 c = state->ks_input[state->ks_inputhead];
905 state->ks_inputhead = (state->ks_inputhead + 1)%INPUTBUFSIZE;
911 /* test the interface to the device */
913 ukbd_test_if(keyboard_t *kbd)
919 * Enable the access to the device; until this function is called,
920 * the client cannot read from the keyboard.
923 ukbd_enable(keyboard_t *kbd)
931 /* disallow the access to the device */
933 ukbd_disable(keyboard_t *kbd)
941 /* read one byte from the keyboard if it's allowed */
943 ukbd_read(keyboard_t *kbd, int wait)
947 #ifdef UKBD_EMULATE_ATSCANCODE
952 state = (ukbd_state_t *)kbd->kb_data;
953 #ifdef UKBD_EMULATE_ATSCANCODE
954 if (state->ks_buffered_char[0]) {
955 scancode = state->ks_buffered_char[0];
956 if (scancode & SCAN_PREFIX) {
957 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
958 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
960 state->ks_buffered_char[0] = state->ks_buffered_char[1];
961 state->ks_buffered_char[1] = 0;
965 #endif /* UKBD_EMULATE_ATSCANCODE */
968 usbcode = ukbd_getc(state);
969 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd) || (usbcode == -1))
972 #ifdef UKBD_EMULATE_ATSCANCODE
973 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
977 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
978 usbcode & KEY_RELEASE);
979 if (scancode & SCAN_PREFIX) {
980 if (scancode & SCAN_PREFIX_CTL) {
981 state->ks_buffered_char[0] =
982 0x1d | (scancode & SCAN_RELEASE); /* Ctrl */
983 state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX;
984 } else if (scancode & SCAN_PREFIX_SHIFT) {
985 state->ks_buffered_char[0] =
986 0x2a | (scancode & SCAN_RELEASE); /* Shift */
987 state->ks_buffered_char[1] =
988 scancode & ~SCAN_PREFIX_SHIFT;
990 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
991 state->ks_buffered_char[1] = 0;
993 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
996 #else /* !UKBD_EMULATE_ATSCANCODE */
998 #endif /* UKBD_EMULATE_ATSCANCODE */
1001 /* check if data is waiting */
1003 ukbd_check(keyboard_t *kbd)
1005 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd))
1007 #ifdef UKBD_EMULATE_ATSCANCODE
1008 if (((ukbd_state_t *)kbd->kb_data)->ks_buffered_char[0])
1011 if (((ukbd_state_t *)kbd->kb_data)->ks_inputs > 0)
1016 /* read char from the keyboard */
1018 ukbd_read_char(keyboard_t *kbd, int wait)
1020 ukbd_state_t *state;
1024 #ifdef UKBD_EMULATE_ATSCANCODE
1028 state = (ukbd_state_t *)kbd->kb_data;
1030 /* do we have a composed char to return? */
1031 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
1032 action = state->ks_composed_char;
1033 state->ks_composed_char = 0;
1034 if (action > UCHAR_MAX)
1039 #ifdef UKBD_EMULATE_ATSCANCODE
1040 /* do we have a pending raw scan code? */
1041 if (state->ks_mode == K_RAW) {
1042 if (state->ks_buffered_char[0]) {
1043 scancode = state->ks_buffered_char[0];
1044 if (scancode & SCAN_PREFIX) {
1045 state->ks_buffered_char[0] =
1046 scancode & ~SCAN_PREFIX;
1047 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1049 state->ks_buffered_char[0] =
1050 state->ks_buffered_char[1];
1051 state->ks_buffered_char[1] = 0;
1056 #endif /* UKBD_EMULATE_ATSCANCODE */
1058 /* see if there is something in the keyboard port */
1060 usbcode = ukbd_getc(state);
1065 #ifdef UKBD_EMULATE_ATSCANCODE
1066 /* USB key index -> key code -> AT scan code */
1067 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1071 /* return an AT scan code for the K_RAW mode */
1072 if (state->ks_mode == K_RAW) {
1073 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
1074 usbcode & KEY_RELEASE);
1075 if (scancode & SCAN_PREFIX) {
1076 if (scancode & SCAN_PREFIX_CTL) {
1077 state->ks_buffered_char[0] =
1078 0x1d | (scancode & SCAN_RELEASE);
1079 state->ks_buffered_char[1] =
1080 scancode & ~SCAN_PREFIX;
1081 } else if (scancode & SCAN_PREFIX_SHIFT) {
1082 state->ks_buffered_char[0] =
1083 0x2a | (scancode & SCAN_RELEASE);
1084 state->ks_buffered_char[1] =
1085 scancode & ~SCAN_PREFIX_SHIFT;
1087 state->ks_buffered_char[0] =
1088 scancode & ~SCAN_PREFIX;
1089 state->ks_buffered_char[1] = 0;
1091 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1095 #else /* !UKBD_EMULATE_ATSCANCODE */
1096 /* return the byte as is for the K_RAW mode */
1097 if (state->ks_mode == K_RAW)
1100 /* USB key index -> key code */
1101 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1104 #endif /* UKBD_EMULATE_ATSCANCODE */
1107 case 0x38: /* left alt (compose key) */
1108 if (usbcode & KEY_RELEASE) {
1109 if (state->ks_flags & COMPOSE) {
1110 state->ks_flags &= ~COMPOSE;
1111 if (state->ks_composed_char > UCHAR_MAX)
1112 state->ks_composed_char = 0;
1115 if (!(state->ks_flags & COMPOSE)) {
1116 state->ks_flags |= COMPOSE;
1117 state->ks_composed_char = 0;
1121 /* XXX: I don't like these... */
1122 case 0x5c: /* print screen */
1123 if (state->ks_flags & ALTS)
1124 keycode = 0x54; /* sysrq */
1126 case 0x68: /* pause/break */
1127 if (state->ks_flags & CTLS)
1128 keycode = 0x6c; /* break */
1132 /* return the key code in the K_CODE mode */
1133 if (usbcode & KEY_RELEASE)
1134 keycode |= SCAN_RELEASE;
1135 if (state->ks_mode == K_CODE)
1138 /* compose a character code */
1139 if (state->ks_flags & COMPOSE) {
1141 /* key pressed, process it */
1142 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
1143 state->ks_composed_char *= 10;
1144 state->ks_composed_char += keycode - 0x40;
1145 if (state->ks_composed_char > UCHAR_MAX)
1148 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
1149 state->ks_composed_char *= 10;
1150 state->ks_composed_char += keycode - 0x47;
1151 if (state->ks_composed_char > UCHAR_MAX)
1154 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
1155 state->ks_composed_char *= 10;
1156 state->ks_composed_char += keycode - 0x4E;
1157 if (state->ks_composed_char > UCHAR_MAX)
1160 case 0x52: /* keypad 0 */
1161 state->ks_composed_char *= 10;
1162 if (state->ks_composed_char > UCHAR_MAX)
1166 /* key released, no interest here */
1167 case SCAN_RELEASE | 0x47:
1168 case SCAN_RELEASE | 0x48:
1169 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1170 case SCAN_RELEASE | 0x4B:
1171 case SCAN_RELEASE | 0x4C:
1172 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1173 case SCAN_RELEASE | 0x4F:
1174 case SCAN_RELEASE | 0x50:
1175 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1176 case SCAN_RELEASE | 0x52: /* keypad 0 */
1179 case 0x38: /* left alt key */
1183 if (state->ks_composed_char > 0) {
1184 state->ks_flags &= ~COMPOSE;
1185 state->ks_composed_char = 0;
1192 /* keycode to key action */
1193 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1194 keycode & SCAN_RELEASE, &state->ks_state,
1195 &state->ks_accents);
1196 if (action == NOKEY)
1202 /* check if char is waiting */
1204 ukbd_check_char(keyboard_t *kbd)
1206 ukbd_state_t *state;
1208 if (!KBD_IS_ACTIVE(kbd) || !KBD_HAS_DEVICE(kbd))
1210 state = (ukbd_state_t *)kbd->kb_data;
1211 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
1213 return (ukbd_check(kbd));
1216 /* some useful control functions */
1218 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1220 /* trasnlate LED_XXX bits into the device specific bits */
1221 static u_char ledmap[8] = {
1222 0, 2, 1, 3, 4, 6, 5, 7,
1224 ukbd_state_t *state = kbd->kb_data;
1229 case KDGKBMODE: /* get keyboard mode */
1230 *(int *)arg = state->ks_mode;
1232 case KDSKBMODE: /* set keyboard mode */
1233 switch (*(int *)arg) {
1235 if (state->ks_mode != K_XLATE) {
1236 /* make lock key state and LED state match */
1237 state->ks_state &= ~LOCK_MASK;
1238 state->ks_state |= KBD_LED_VAL(kbd);
1243 if (state->ks_mode != *(int *)arg) {
1244 ukbd_clear_state(kbd);
1245 state->ks_mode = *(int *)arg;
1246 kbd->kb_savemode = state->ks_mode;
1255 case KDGETLED: /* get keyboard LED */
1256 *(int *)arg = KBD_LED_VAL(kbd);
1258 case KDSETLED: /* set keyboard LED */
1259 /* NOTE: lock key state in ks_state won't be changed */
1260 if (*(int *)arg & ~LOCK_MASK) {
1265 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1266 if (kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1272 if (KBD_HAS_DEVICE(kbd)) {
1273 set_leds(state, ledmap[i & LED_MASK]);
1274 /* XXX: error check? */
1276 KBD_LED_VAL(kbd) = *(int *)arg;
1279 case KDGKBSTATE: /* get lock key state */
1280 *(int *)arg = state->ks_state & LOCK_MASK;
1282 case KDSKBSTATE: /* set lock key state */
1283 if (*(int *)arg & ~LOCK_MASK) {
1287 state->ks_state &= ~LOCK_MASK;
1288 state->ks_state |= *(int *)arg;
1290 /* set LEDs and quit */
1291 return ukbd_ioctl(kbd, KDSETLED, arg);
1293 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
1295 if (!KBD_HAS_DEVICE(kbd))
1297 if (((int *)arg)[1] < 0)
1299 if (((int *)arg)[0] < 0)
1301 else if (((int *)arg)[0] == 0) /* fastest possible value */
1302 kbd->kb_delay1 = 200;
1304 kbd->kb_delay1 = ((int *)arg)[0];
1305 kbd->kb_delay2 = ((int *)arg)[1];
1308 case KDSETRAD: /* set keyboard repeat rate (old interface) */
1310 return set_typematic(kbd, *(int *)arg);
1312 case PIO_KEYMAP: /* set keyboard translation table */
1313 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1314 case PIO_DEADKEYMAP: /* set accent key translation table */
1315 state->ks_accents = 0;
1319 return genkbd_commonioctl(kbd, cmd, arg);
1323 ukbddebug = *(int *)arg;
1332 /* lock the access to the keyboard */
1334 ukbd_lock(keyboard_t *kbd, int lock)
1340 /* clear the internal state of the keyboard */
1342 ukbd_clear_state(keyboard_t *kbd)
1344 ukbd_state_t *state;
1346 state = (ukbd_state_t *)kbd->kb_data;
1347 state->ks_flags = 0;
1348 state->ks_polling = 0;
1349 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1350 state->ks_accents = 0;
1351 state->ks_composed_char = 0;
1352 #ifdef UKBD_EMULATE_ATSCANCODE
1353 state->ks_buffered_char[0] = 0;
1354 state->ks_buffered_char[1] = 0;
1356 bzero(&state->ks_ndata, sizeof(state->ks_ndata));
1357 bzero(&state->ks_odata, sizeof(state->ks_odata));
1358 bzero(&state->ks_ntime, sizeof(state->ks_ntime));
1359 bzero(&state->ks_otime, sizeof(state->ks_otime));
1362 /* save the internal state */
1364 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1367 return sizeof(ukbd_state_t);
1368 if (len < sizeof(ukbd_state_t))
1370 bcopy(kbd->kb_data, buf, sizeof(ukbd_state_t));
1374 /* set the internal state */
1376 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1378 if (len < sizeof(ukbd_state_t))
1380 bcopy(buf, kbd->kb_data, sizeof(ukbd_state_t));
1385 ukbd_poll(keyboard_t *kbd, int on)
1387 ukbd_state_t *state;
1388 usbd_device_handle dev;
1390 state = (ukbd_state_t *)kbd->kb_data;
1391 usbd_interface2device_handle(state->ks_iface, &dev);
1395 if (state->ks_polling == 0)
1396 usbd_set_polling(dev, on);
1397 ++state->ks_polling;
1399 --state->ks_polling;
1400 if (state->ks_polling == 0)
1401 usbd_set_polling(dev, on);
1407 /* local functions */
1410 probe_keyboard(struct usb_attach_arg *uaa, int flags)
1412 usb_interface_descriptor_t *id;
1414 if (!uaa->iface) /* we attach to ifaces only */
1417 /* Check that this is a keyboard that speaks the boot protocol. */
1418 id = usbd_get_interface_descriptor(uaa->iface);
1420 && id->bInterfaceClass == UICLASS_HID
1421 && id->bInterfaceSubClass == UISUBCLASS_BOOT
1422 && id->bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)
1423 return 0; /* found it */
1429 init_keyboard(ukbd_state_t *state, int *type, int flags)
1431 usb_endpoint_descriptor_t *ed;
1436 state->ks_ifstate |= DISCONNECTED;
1438 ed = usbd_interface2endpoint_descriptor(state->ks_iface, 0);
1440 kprintf("ukbd: could not read endpoint descriptor\n");
1444 DPRINTFN(10,("ukbd:init_keyboard: \
1445 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
1446 ed->bLength, ed->bDescriptorType,
1447 UE_GET_ADDR(ed->bEndpointAddress),
1448 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
1449 UE_GET_XFERTYPE(ed->bmAttributes),
1450 UGETW(ed->wMaxPacketSize), ed->bInterval));
1452 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
1453 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
1454 kprintf("ukbd: unexpected endpoint\n");
1458 if ((usbd_get_quirks(state->ks_uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
1459 err = usbd_set_protocol(state->ks_iface, 0);
1460 DPRINTFN(5, ("ukbd:init_keyboard: protocol set\n"));
1462 kprintf("ukbd: set protocol failed\n");
1466 /* Ignore if SETIDLE fails since it is not crucial. */
1467 usbd_set_idle(state->ks_iface, 0, 0);
1469 state->ks_ep_addr = ed->bEndpointAddress;
1470 state->ks_ifstate &= ~DISCONNECTED;
1476 set_leds(ukbd_state_t *state, int leds)
1478 u_int8_t res = leds;
1480 DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state, leds));
1482 usbd_set_report_async(state->ks_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
1486 set_typematic(keyboard_t *kbd, int code)
1488 static int delays[] = { 250, 500, 750, 1000 };
1489 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1490 68, 76, 84, 92, 100, 110, 118, 126,
1491 136, 152, 168, 184, 200, 220, 236, 252,
1492 272, 304, 336, 368, 400, 440, 472, 504 };
1496 kbd->kb_delay1 = delays[(code >> 5) & 3];
1497 kbd->kb_delay2 = rates[code & 0x1f];
1501 #ifdef UKBD_EMULATE_ATSCANCODE
1503 keycode2scancode(int keycode, int shift, int up)
1505 static int scan[] = {
1507 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1508 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1509 0x50, 0x51, 0x52, 0x53,
1510 0x46, /* XXX Pause/Break */
1516 if ((keycode >= 89) && (keycode < 89 + sizeof(scan)/sizeof(scan[0])))
1517 scancode = scan[keycode - 89] | SCAN_PREFIX_E0;
1519 if ((keycode == 104) && !(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))
1520 scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL;
1521 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R))
1522 scancode &= ~SCAN_PREFIX_SHIFT;
1523 return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS));
1525 #endif /* UKBD_EMULATE_ATSCANCODE */
1528 ukbd_driver_load(module_t mod, int what, void *arg)
1532 kbd_add_driver(&ukbd_kbd_driver);
1535 kbd_delete_driver(&ukbd_kbd_driver);
1538 return usbd_driver_load(mod, what, 0);