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 $
9 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * All rights reserved.
12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Lennart Augustsson (lennart@augustsson.net) at
14 * Carlstedt Research & Technology.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the NetBSD
27 * Foundation, Inc. and its contributors.
28 * 4. Neither the name of The NetBSD Foundation nor the names of its
29 * contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
33 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
34 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
36 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
46 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
48 * NOTE: All locks are handled by the kbd wrappers.
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/module.h>
60 #include <machine/limits.h>
61 #include <sys/select.h>
62 #include <sys/sysctl.h>
63 #include <sys/thread2.h>
65 #include <bus/usb/usb.h>
66 #include <bus/usb/usbhid.h>
67 #include <bus/usb/usbdi.h>
68 #include <bus/usb/usbdi_util.h>
69 #include <bus/usb/usb_quirks.h>
70 #include <bus/usb/hid.h>
73 #include <dev/misc/kbd/kbdreg.h>
75 #define UKBD_EMULATE_ATSCANCODE 1
77 #define DRIVER_NAME "ukbd"
79 #define delay(d) DELAY(d)
82 #define DPRINTF(x) if (ukbddebug) kprintf x
83 #define DPRINTFN(n,x) if (ukbddebug>(n)) kprintf x
85 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
86 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
87 &ukbddebug, 0, "ukbd debug level");
93 #define UPROTO_BOOT_KEYBOARD 1
99 #define MOD_CONTROL_L 0x01
100 #define MOD_CONTROL_R 0x10
101 #define MOD_SHIFT_L 0x02
102 #define MOD_SHIFT_R 0x20
103 #define MOD_ALT_L 0x04
104 #define MOD_ALT_R 0x40
105 #define MOD_WIN_L 0x08
106 #define MOD_WIN_R 0x80
108 u_int8_t keycode[NKEYCODE];
111 #define MAXKEYS (NMOD+2*NKEYCODE)
113 typedef struct ukbd_softc {
114 device_t sc_dev; /* base device */
117 #define UKBD_CHUNK 128 /* chunk size for read */
118 #define UKBD_BSIZE 1020 /* buffer size */
120 typedef void usbd_intr_t(usbd_xfer_handle, usbd_private_handle, usbd_status);
121 typedef void usbd_disco_t(void *);
123 static int ukbd_resume(device_t self);
124 static usbd_intr_t ukbd_intr;
125 static int ukbd_driver_load(module_t mod, int what, void *arg);
127 static keyboard_t default_kbd;
129 static device_probe_t ukbd_match;
130 static device_attach_t ukbd_attach;
131 static device_detach_t ukbd_detach;
133 static devclass_t ukbd_devclass;
135 static kobj_method_t ukbd_methods[] = {
136 DEVMETHOD(device_probe, ukbd_match),
137 DEVMETHOD(device_attach, ukbd_attach),
138 DEVMETHOD(device_detach, ukbd_detach),
139 DEVMETHOD(device_resume, ukbd_resume),
143 static driver_t ukbd_driver = {
146 sizeof(struct ukbd_softc)
149 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
152 ukbd_match(device_t self)
154 struct usb_attach_arg *uaa = device_get_ivars(self);
156 keyboard_switch_t *sw;
158 int unit = device_get_unit(self);
160 sw = kbd_get_switch(DRIVER_NAME);
162 return (UMATCH_NONE);
164 arg[0] = (void *)uaa;
165 arg[1] = (void *)ukbd_intr;
166 if ((*sw->probe)(unit, (void *)arg, 0))
167 return (UMATCH_NONE);
169 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
173 ukbd_attach(device_t self)
175 struct ukbd_softc *sc = device_get_softc(self);
176 struct usb_attach_arg *uaa = device_get_ivars(self);
178 keyboard_switch_t *sw;
181 int unit = device_get_unit(self);
185 sw = kbd_get_switch(DRIVER_NAME);
189 arg[0] = (void *)uaa;
190 arg[1] = (void *)ukbd_intr;
192 if ((*sw->probe)(unit, (void *)arg, 0))
194 if ((*sw->init)(unit, &kbd, (void *)arg, 0))
198 #ifdef KBD_INSTALL_CDEV
203 (*sw->diag)(kbd, bootverbose);
208 ukbd_detach(device_t self)
213 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
214 device_get_unit(self)));
216 DPRINTF(("%s: keyboard not attached!?\n", device_get_nameunit(self)));
221 #ifdef KBD_INSTALL_CDEV
222 error = kbd_detach(kbd);
226 error = kbd_term(kbd);
230 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
236 ukbd_resume(device_t self)
240 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
241 device_get_unit(self)));
243 kbd_clear_state(kbd);
249 ukbd_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
251 keyboard_t *kbd = (keyboard_t *)addr;
253 kbd_intr(kbd, (void *)status);
256 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
259 #define UKBD_DEFAULT 0
261 #define KEY_ERROR 0x01
264 #define KEY_RELEASE 0x400
265 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE)
268 #define SCAN_RELEASE 0x80
269 #define SCAN_PREFIX_E0 0x100
270 #define SCAN_PREFIX_E1 0x200
271 #define SCAN_PREFIX_CTL 0x400
272 #define SCAN_PREFIX_SHIFT 0x800
273 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL \
275 #define SCAN_CHAR(c) ((c) & 0x7f)
280 } ukbd_mods[NMOD] = {
281 { MOD_CONTROL_L, 0xe0 },
282 { MOD_CONTROL_R, 0xe4 },
283 { MOD_SHIFT_L, 0xe1 },
284 { MOD_SHIFT_R, 0xe5 },
291 #define NN 0 /* no translation */
293 * Translate USB keycodes to AT keyboard scancodes.
296 * FIXME: Mac USB keyboard generates:
297 * 0x53: keypad NumLock/Clear
304 static u_int8_t ukbd_trtab[256] = {
305 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
306 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
307 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
308 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
309 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
310 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
311 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
312 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
313 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
314 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
315 97, 100, 95, 69, 91, 55, 74, 78, /* 50 - 57 */
316 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
317 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
318 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
319 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
320 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
321 121, 120, NN, NN, NN, NN, NN, 115, /* 80 - 87 */
322 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */
323 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
324 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
325 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
326 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
327 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
328 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
329 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
330 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
331 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
332 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
333 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
334 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
335 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
336 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
339 typedef struct ukbd_state {
340 usbd_interface_handle ks_iface; /* interface */
341 usbd_pipe_handle ks_intrpipe; /* interrupt pipe */
342 struct usb_attach_arg *ks_uaa;
345 struct ukbd_data ks_ndata;
346 struct ukbd_data ks_odata;
347 u_long ks_ntime[NKEYCODE];
348 u_long ks_otime[NKEYCODE];
350 #define INPUTBUFSIZE (NMOD + 2*NKEYCODE)
351 u_int ks_input[INPUTBUFSIZE]; /* input buffer */
357 #define INTRENABLED (1 << 0)
358 #define DISCONNECTED (1 << 1)
360 struct callout ks_timeout;
362 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
363 int ks_flags; /* flags */
364 #define COMPOSE (1 << 0)
366 int ks_state; /* shift/lock key state */
367 int ks_accents; /* accent key index (> 0) */
368 u_int ks_composed_char; /* composed char code (> 0) */
369 #ifdef UKBD_EMULATE_ATSCANCODE
370 u_int ks_buffered_char[2];
374 /* keyboard driver declaration */
375 static int ukbd_configure(int flags);
376 static kbd_probe_t ukbd_probe;
377 static kbd_init_t ukbd_init;
378 static kbd_term_t ukbd_term;
379 static kbd_intr_t ukbd_interrupt;
380 static kbd_test_if_t ukbd_test_if;
381 static kbd_enable_t ukbd_enable;
382 static kbd_disable_t ukbd_disable;
383 static kbd_read_t ukbd_read;
384 static kbd_check_t ukbd_check;
385 static kbd_read_char_t ukbd_read_char;
386 static kbd_check_char_t ukbd_check_char;
387 static kbd_ioctl_t ukbd_ioctl;
388 static kbd_lock_t ukbd_lock;
389 static kbd_clear_state_t ukbd_clear_state;
390 static kbd_get_state_t ukbd_get_state;
391 static kbd_set_state_t ukbd_set_state;
392 static kbd_poll_mode_t ukbd_poll;
394 keyboard_switch_t ukbdsw = {
416 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
418 /* local functions */
419 static int ukbd_enable_intr(keyboard_t *kbd, int on,
421 static timeout_t ukbd_timeout;
423 static int ukbd_getc(ukbd_state_t *state, int wait);
424 static int probe_keyboard(struct usb_attach_arg *uaa, int flags);
425 static int init_keyboard(ukbd_state_t *state, int *type,
427 static void set_leds(ukbd_state_t *state, int leds);
428 static int set_typematic(keyboard_t *kbd, int code);
429 #ifdef UKBD_EMULATE_ATSCANCODE
430 static int keycode2scancode(int keycode, int shift, int up);
433 /* local variables */
435 /* the initial key map, accent map and fkey strings */
436 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
437 #define KBD_DFLT_KEYMAP
440 #include <dev/misc/kbd/kbdtables.h>
442 /* structures for the default keyboard */
443 static ukbd_state_t default_kbd_state;
444 static keymap_t default_keymap;
445 static accentmap_t default_accentmap;
446 static fkeytab_t default_fkeytab[NUM_FKEYS];
449 * The back door to the keyboard driver!
450 * This function is called by the console driver, via the kbdio module,
451 * to tickle keyboard drivers when the low-level console is being initialized.
452 * Almost nothing in the kernel has been initialied yet. Try to probe
453 * keyboards if possible.
454 * NOTE: because of the way the low-level conole is initialized, this routine
455 * may be called more than once!!
458 ukbd_configure(int flags)
465 struct usb_attach_arg *uaa;
468 device = devclass_get_device(ukbd_devclass, UKBD_DEFAULT);
471 uaa = (struct usb_attach_arg *)device_get_ivars(device);
475 /* probe the default keyboard */
476 arg[0] = (void *)uaa;
477 arg[1] = (void *)ukbd_intr;
479 if (ukbd_probe(UKBD_DEFAULT, arg, flags))
481 if (ukbd_init(UKBD_DEFAULT, &kbd, arg, flags))
484 /* return the number of found keyboards */
489 /* low-level functions */
491 /* detect a keyboard */
493 ukbd_probe(int unit, void *arg, int flags)
496 struct usb_attach_arg *uaa;
499 uaa = (struct usb_attach_arg *)data[0];
501 if (unit == UKBD_DEFAULT) {
502 if (KBD_IS_PROBED(&default_kbd))
505 if (probe_keyboard(uaa, flags))
511 * Reset and initialize the device. Note that unit 0 (UKBD_DEFAULT) is an
512 * always-connected device once it has been initially detected. We do not
513 * deregister it if the usb keyboard is unplugged to avoid losing the
514 * connection to the console. This feature also handles the USB bus reset
515 * which detaches and reattaches USB devices during boot.
518 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
526 void **data = (void **)arg;
527 struct usb_attach_arg *uaa = (struct usb_attach_arg *)data[0];
529 if (unit == UKBD_DEFAULT) {
530 *kbdp = kbd = &default_kbd;
531 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd)) {
534 state = &default_kbd_state;
535 keymap = &default_keymap;
536 accmap = &default_accentmap;
537 fkeymap = default_fkeytab;
539 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
540 } else if (*kbdp == NULL) {
541 *kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_INTWAIT | M_ZERO);
542 state = kmalloc(sizeof(*state), M_DEVBUF, M_INTWAIT);
543 keymap = kmalloc(sizeof(key_map), M_DEVBUF, M_INTWAIT);
544 accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_INTWAIT);
545 fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_INTWAIT);
546 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
547 if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
548 || (fkeymap == NULL)) {
550 kfree(state, M_DEVBUF);
552 kfree(keymap, M_DEVBUF);
554 kfree(accmap, M_DEVBUF);
556 kfree(fkeymap, M_DEVBUF);
557 kfree(kbd, M_DEVBUF);
560 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
564 state = (ukbd_state_t *)kbd->kb_data;
565 keymap = kbd->kb_keymap;
566 accmap = kbd->kb_accentmap;
567 fkeymap = kbd->kb_fkeytab;
568 fkeymap_size = kbd->kb_fkeytab_size;
571 if (!KBD_IS_PROBED(kbd)) {
572 kbd_init_struct(kbd, DRIVER_NAME, KB_OTHER,
573 unit, flags, KB_PRI_USB,
575 bzero(state, sizeof(*state));
576 bcopy(&key_map, keymap, sizeof(key_map));
577 bcopy(&accent_map, accmap, sizeof(accent_map));
578 bcopy(fkey_tab, fkeymap,
579 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
580 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
581 kbd->kb_data = (void *)state;
583 if (probe_keyboard(uaa, flags)) {
586 KBD_FOUND_DEVICE(kbd);
588 ukbd_clear_state(kbd);
591 * If reattatching to an already open keyboard (e.g. console),
592 * try to restore the translation mode. Otherwise set the
593 * translation mode to, well, translation mode so we don't
596 state->ks_mode = K_XLATE;
597 state->ks_iface = uaa->iface;
599 state->ks_ifstate = 0;
600 callout_init_mp(&state->ks_timeout);
602 * FIXME: set the initial value for lock keys in ks_state
603 * according to the BIOS data?
607 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
608 if (KBD_HAS_DEVICE(kbd)
609 && init_keyboard((ukbd_state_t *)kbd->kb_data,
610 &kbd->kb_type, kbd->kb_flags)) {
613 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&(state->ks_state));
615 if (!KBD_IS_CONFIGURED(kbd)) {
616 if (kbd_register(kbd) < 0) {
618 /* XXX: Missing free()'s */
621 if (ukbd_enable_intr(kbd, TRUE, (usbd_intr_t *)data[1]) == 0)
622 ukbd_timeout((void *)kbd);
623 KBD_CONFIG_DONE(kbd);
630 ukbd_enable_intr(keyboard_t *kbd, int on, usbd_intr_t *func)
632 ukbd_state_t *state = (ukbd_state_t *)kbd->kb_data;
636 /* Set up interrupt pipe. */
637 if (state->ks_ifstate & INTRENABLED) {
641 state->ks_ifstate |= INTRENABLED;
642 err = usbd_open_pipe_intr(state->ks_iface, state->ks_ep_addr,
643 USBD_SHORT_XFER_OK | USBD_CALLBACK_LAST,
644 &state->ks_intrpipe, kbd,
646 sizeof(state->ks_ndata), func,
647 USBD_DEFAULT_INTERVAL);
652 /* Disable interrupts. */
653 usbd_abort_pipe(state->ks_intrpipe);
654 usbd_close_pipe(state->ks_intrpipe);
656 state->ks_ifstate &= ~INTRENABLED;
662 /* finish using this keyboard */
664 ukbd_term(keyboard_t *kbd)
670 state = (ukbd_state_t *)kbd->kb_data;
671 DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state->ks_ifstate));
673 callout_stop(&state->ks_timeout);
675 if (state->ks_ifstate & INTRENABLED)
676 ukbd_enable_intr(kbd, FALSE, NULL);
677 if (state->ks_ifstate & INTRENABLED) {
679 DPRINTF(("ukbd_term: INTRENABLED!\n"));
683 error = kbd_unregister(kbd);
685 DPRINTF(("ukbd_term: kbd_unregister() %d\n", error));
688 if (kbd != &default_kbd) {
689 kfree(kbd->kb_keymap, M_DEVBUF);
690 kfree(kbd->kb_accentmap, M_DEVBUF);
691 kfree(kbd->kb_fkeytab, M_DEVBUF);
692 kfree(state, M_DEVBUF);
693 kfree(kbd, M_DEVBUF);
700 /* keyboard interrupt routine */
703 ukbd_timeout(void *arg)
708 kbd = (keyboard_t *)arg;
709 state = (ukbd_state_t *)kbd->kb_data;
711 kbd_intr(kbd, (void *)USBD_NORMAL_COMPLETION);
712 callout_reset(&state->ks_timeout, hz / 40, ukbd_timeout, arg);
717 ukbd_interrupt(keyboard_t *kbd, void *arg)
719 usbd_status status = (usbd_status)arg;
721 struct ukbd_data *ud;
728 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
729 if (status == USBD_CANCELLED)
732 state = (ukbd_state_t *)kbd->kb_data;
733 ud = &state->ks_ndata;
735 if (status != USBD_NORMAL_COMPLETION) {
736 DPRINTF(("ukbd_intr: status=%d\n", status));
737 if (status == USBD_STALLED)
738 usbd_clear_endpoint_stall_async(state->ks_intrpipe);
742 if (ud->keycode[0] == KEY_ERROR) {
743 return 0; /* ignore */
747 now = (u_long)tv.tv_sec*1000 + (u_long)tv.tv_usec/1000;
750 if (state->ks_inputs < INPUTBUFSIZE) { \
751 state->ks_input[state->ks_inputtail] = (c); \
752 ++state->ks_inputs; \
753 state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \
757 omod = state->ks_odata.modifiers;
759 for (i = 0; i < NMOD; i++)
760 if (( mod & ukbd_mods[i].mask) !=
761 (omod & ukbd_mods[i].mask))
762 ADDKEY1(ukbd_mods[i].key |
763 (mod & ukbd_mods[i].mask
764 ? KEY_PRESS : KEY_RELEASE));
767 /* Check for released keys. */
768 for (i = 0; i < NKEYCODE; i++) {
769 key = state->ks_odata.keycode[i];
772 for (j = 0; j < NKEYCODE; j++) {
773 if (ud->keycode[j] == 0)
775 if (key == ud->keycode[j])
778 ADDKEY1(key | KEY_RELEASE);
783 /* Check for pressed keys. */
784 for (i = 0; i < NKEYCODE; i++) {
785 key = ud->keycode[i];
788 state->ks_ntime[i] = now + kbd->kb_delay1;
789 for (j = 0; j < NKEYCODE; j++) {
790 if (state->ks_odata.keycode[j] == 0)
792 if (key == state->ks_odata.keycode[j]) {
793 state->ks_ntime[i] = state->ks_otime[j];
794 if (state->ks_otime[j] > now)
796 state->ks_ntime[i] = now + kbd->kb_delay2;
800 ADDKEY1(key | KEY_PRESS);
802 * If any other key is presently down, force its repeat to be
803 * well in the future (100s). This makes the last key to be
804 * pressed do the autorepeat.
806 for (j = 0; j < NKEYCODE; j++) {
808 state->ks_ntime[j] = now + 100 * 1000;
814 state->ks_odata = *ud;
815 bcopy(state->ks_ntime, state->ks_otime, sizeof(state->ks_ntime));
816 if (state->ks_inputs <= 0) {
821 for (i = state->ks_inputhead, j = 0; j < state->ks_inputs; ++j,
822 i = (i + 1)%INPUTBUFSIZE) {
823 c = state->ks_input[i];
824 DPRINTF(("0x%x (%d) %s\n", c, c,
825 (c & KEY_RELEASE) ? "released":"pressed"));
828 DPRINTF(("mod:0x%04x ", ud->modifiers));
829 for (i = 0; i < NKEYCODE; i++) {
831 DPRINTF(("%d ", ud->keycode[i]));
834 #endif /* USB_DEBUG */
836 if (state->ks_polling) {
840 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
841 /* let the callback function to process the input */
842 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
843 kbd->kb_callback.kc_arg);
845 /* read and discard the input; no one is waiting for it */
847 c = ukbd_read_char(kbd, FALSE);
848 } while (c != NOKEY);
855 ukbd_getc(ukbd_state_t *state, int wait)
859 if (state->ks_polling) {
860 DPRINTFN(1,("ukbd_getc: polling\n"));
862 while (state->ks_inputs <= 0) {
863 usbd_dopoll(state->ks_iface);
870 if (state->ks_inputs <= 0) {
873 c = state->ks_input[state->ks_inputhead];
875 state->ks_inputhead = (state->ks_inputhead + 1)%INPUTBUFSIZE;
882 /* test the interface to the device */
884 ukbd_test_if(keyboard_t *kbd)
890 * Enable the access to the device; until this function is called,
891 * the client cannot read from the keyboard.
894 ukbd_enable(keyboard_t *kbd)
902 /* disallow the access to the device */
904 ukbd_disable(keyboard_t *kbd)
912 /* read one byte from the keyboard if it's allowed */
914 ukbd_read(keyboard_t *kbd, int wait)
918 #ifdef UKBD_EMULATE_ATSCANCODE
923 state = (ukbd_state_t *)kbd->kb_data;
924 #ifdef UKBD_EMULATE_ATSCANCODE
925 if (state->ks_buffered_char[0]) {
926 scancode = state->ks_buffered_char[0];
927 if (scancode & SCAN_PREFIX) {
928 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
929 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
931 state->ks_buffered_char[0] = state->ks_buffered_char[1];
932 state->ks_buffered_char[1] = 0;
936 #endif /* UKBD_EMULATE_ATSCANCODE */
938 usbcode = ukbd_getc(state, wait);
939 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) {
943 #ifdef UKBD_EMULATE_ATSCANCODE
944 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
949 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
950 usbcode & KEY_RELEASE);
951 if (scancode & SCAN_PREFIX) {
952 if (scancode & SCAN_PREFIX_CTL) {
953 state->ks_buffered_char[0] =
954 0x1d | (scancode & SCAN_RELEASE); /* Ctrl */
955 state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX;
956 } else if (scancode & SCAN_PREFIX_SHIFT) {
957 state->ks_buffered_char[0] =
958 0x2a | (scancode & SCAN_RELEASE); /* Shift */
959 state->ks_buffered_char[1] =
960 scancode & ~SCAN_PREFIX_SHIFT;
962 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
963 state->ks_buffered_char[1] = 0;
965 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
968 #else /* !UKBD_EMULATE_ATSCANCODE */
970 #endif /* UKBD_EMULATE_ATSCANCODE */
973 /* check if data is waiting */
975 ukbd_check(keyboard_t *kbd)
979 if (!KBD_IS_ACTIVE(kbd)) {
982 state = (ukbd_state_t *)kbd->kb_data;
983 if (state->ks_polling) {
985 usbd_dopoll(state->ks_iface);
988 #ifdef UKBD_EMULATE_ATSCANCODE
989 if (((ukbd_state_t *)kbd->kb_data)->ks_buffered_char[0]) {
993 if (((ukbd_state_t *)kbd->kb_data)->ks_inputs > 0) {
1000 /* read char from the keyboard */
1002 ukbd_read_char(keyboard_t *kbd, int wait)
1004 ukbd_state_t *state;
1008 #ifdef UKBD_EMULATE_ATSCANCODE
1012 state = (ukbd_state_t *)kbd->kb_data;
1014 /* do we have a composed char to return? */
1015 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
1016 action = state->ks_composed_char;
1017 state->ks_composed_char = 0;
1018 if (action > UCHAR_MAX) {
1024 #ifdef UKBD_EMULATE_ATSCANCODE
1025 /* do we have a pending raw scan code? */
1026 if (state->ks_mode == K_RAW) {
1027 if (state->ks_buffered_char[0]) {
1028 scancode = state->ks_buffered_char[0];
1029 if (scancode & SCAN_PREFIX) {
1030 state->ks_buffered_char[0] =
1031 scancode & ~SCAN_PREFIX;
1032 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1034 state->ks_buffered_char[0] =
1035 state->ks_buffered_char[1];
1036 state->ks_buffered_char[1] = 0;
1041 #endif /* UKBD_EMULATE_ATSCANCODE */
1043 /* see if there is something in the keyboard port */
1045 usbcode = ukbd_getc(state, wait);
1046 if (usbcode == -1) {
1051 #ifdef UKBD_EMULATE_ATSCANCODE
1052 /* USB key index -> key code -> AT scan code */
1053 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1054 if (keycode == NN) {
1058 /* return an AT scan code for the K_RAW mode */
1059 if (state->ks_mode == K_RAW) {
1060 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
1061 usbcode & KEY_RELEASE);
1062 if (scancode & SCAN_PREFIX) {
1063 if (scancode & SCAN_PREFIX_CTL) {
1064 state->ks_buffered_char[0] =
1065 0x1d | (scancode & SCAN_RELEASE);
1066 state->ks_buffered_char[1] =
1067 scancode & ~SCAN_PREFIX;
1068 } else if (scancode & SCAN_PREFIX_SHIFT) {
1069 state->ks_buffered_char[0] =
1070 0x2a | (scancode & SCAN_RELEASE);
1071 state->ks_buffered_char[1] =
1072 scancode & ~SCAN_PREFIX_SHIFT;
1074 state->ks_buffered_char[0] =
1075 scancode & ~SCAN_PREFIX;
1076 state->ks_buffered_char[1] = 0;
1078 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1082 #else /* !UKBD_EMULATE_ATSCANCODE */
1083 /* return the byte as is for the K_RAW mode */
1084 if (state->ks_mode == K_RAW) {
1088 /* USB key index -> key code */
1089 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1090 if (keycode == NN) {
1093 #endif /* UKBD_EMULATE_ATSCANCODE */
1096 case 0x38: /* left alt (compose key) */
1097 if (usbcode & KEY_RELEASE) {
1098 if (state->ks_flags & COMPOSE) {
1099 state->ks_flags &= ~COMPOSE;
1100 if (state->ks_composed_char > UCHAR_MAX)
1101 state->ks_composed_char = 0;
1104 if (!(state->ks_flags & COMPOSE)) {
1105 state->ks_flags |= COMPOSE;
1106 state->ks_composed_char = 0;
1110 /* XXX: I don't like these... */
1111 case 0x5c: /* print screen */
1112 if (state->ks_flags & ALTS)
1113 keycode = 0x54; /* sysrq */
1115 case 0x68: /* pause/break */
1116 if (state->ks_flags & CTLS)
1117 keycode = 0x6c; /* break */
1121 /* return the key code in the K_CODE mode */
1122 if (usbcode & KEY_RELEASE)
1123 keycode |= SCAN_RELEASE;
1124 if (state->ks_mode == K_CODE) {
1128 /* compose a character code */
1129 if (state->ks_flags & COMPOSE) {
1131 /* key pressed, process it */
1132 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
1133 state->ks_composed_char *= 10;
1134 state->ks_composed_char += keycode - 0x40;
1135 if (state->ks_composed_char > UCHAR_MAX) {
1139 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
1140 state->ks_composed_char *= 10;
1141 state->ks_composed_char += keycode - 0x47;
1142 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) {
1153 case 0x52: /* keypad 0 */
1154 state->ks_composed_char *= 10;
1155 if (state->ks_composed_char > UCHAR_MAX) {
1160 /* key released, no interest here */
1161 case SCAN_RELEASE | 0x47:
1162 case SCAN_RELEASE | 0x48:
1163 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1164 case SCAN_RELEASE | 0x4B:
1165 case SCAN_RELEASE | 0x4C:
1166 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1167 case SCAN_RELEASE | 0x4F:
1168 case SCAN_RELEASE | 0x50:
1169 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1170 case SCAN_RELEASE | 0x52: /* keypad 0 */
1173 case 0x38: /* left alt key */
1177 if (state->ks_composed_char > 0) {
1178 state->ks_flags &= ~COMPOSE;
1179 state->ks_composed_char = 0;
1186 /* keycode to key action */
1187 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1188 keycode & SCAN_RELEASE, &state->ks_state,
1189 &state->ks_accents);
1190 if (action == NOKEY) {
1198 /* check if char is waiting */
1200 ukbd_check_char(keyboard_t *kbd)
1202 ukbd_state_t *state;
1205 if (!KBD_IS_ACTIVE(kbd)) {
1208 state = (ukbd_state_t *)kbd->kb_data;
1209 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
1212 ret = (ukbd_check(kbd));
1217 /* some useful control functions */
1219 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1221 /* trasnlate LED_XXX bits into the device specific bits */
1222 static u_char ledmap[8] = {
1223 0, 2, 1, 3, 4, 6, 5, 7,
1225 ukbd_state_t *state = kbd->kb_data;
1230 case KDGKBMODE: /* get keyboard mode */
1231 *(int *)arg = state->ks_mode;
1233 case KDSKBMODE: /* set keyboard mode */
1234 switch (*(int *)arg) {
1236 if (state->ks_mode != K_XLATE) {
1237 /* make lock key state and LED state match */
1238 state->ks_state &= ~LOCK_MASK;
1239 state->ks_state |= KBD_LED_VAL(kbd);
1244 if (state->ks_mode != *(int *)arg) {
1245 ukbd_clear_state(kbd);
1246 state->ks_mode = *(int *)arg;
1247 kbd->kb_savemode = state->ks_mode;
1256 case KDGETLED: /* get keyboard LED */
1257 *(int *)arg = KBD_LED_VAL(kbd);
1259 case KDSETLED: /* set keyboard LED */
1260 /* NOTE: lock key state in ks_state won't be changed */
1261 if (*(int *)arg & ~LOCK_MASK) {
1266 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1267 if (state->ks_mode == K_XLATE &&
1268 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1274 if (KBD_HAS_DEVICE(kbd)) {
1275 set_leds(state, ledmap[i & LED_MASK]);
1276 /* XXX: error check? */
1278 KBD_LED_VAL(kbd) = *(int *)arg;
1281 case KDGKBSTATE: /* get lock key state */
1282 *(int *)arg = state->ks_state & LOCK_MASK;
1284 case KDSKBSTATE: /* set lock key state */
1285 if (*(int *)arg & ~LOCK_MASK) {
1289 state->ks_state &= ~LOCK_MASK;
1290 state->ks_state |= *(int *)arg;
1292 /* set LEDs and quit */
1293 return ukbd_ioctl(kbd, KDSETLED, arg);
1295 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
1297 if (!KBD_HAS_DEVICE(kbd)) {
1300 if (((int *)arg)[1] < 0) {
1303 if (((int *)arg)[0] < 0) {
1306 else if (((int *)arg)[0] == 0) /* fastest possible value */
1307 kbd->kb_delay1 = 200;
1309 kbd->kb_delay1 = ((int *)arg)[0];
1310 kbd->kb_delay2 = ((int *)arg)[1];
1313 case KDSETRAD: /* set keyboard repeat rate (old interface) */
1315 return set_typematic(kbd, *(int *)arg);
1317 case PIO_KEYMAP: /* set keyboard translation table */
1318 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1319 case PIO_DEADKEYMAP: /* set accent key translation table */
1320 state->ks_accents = 0;
1324 return genkbd_commonioctl(kbd, cmd, arg);
1328 ukbddebug = *(int *)arg;
1337 /* lock the access to the keyboard */
1339 ukbd_lock(keyboard_t *kbd, int lock)
1345 /* clear the internal state of the keyboard */
1347 ukbd_clear_state(keyboard_t *kbd)
1349 ukbd_state_t *state;
1351 state = (ukbd_state_t *)kbd->kb_data;
1352 state->ks_flags = 0;
1353 state->ks_polling = 0;
1354 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1355 state->ks_accents = 0;
1356 state->ks_composed_char = 0;
1357 #ifdef UKBD_EMULATE_ATSCANCODE
1358 state->ks_buffered_char[0] = 0;
1359 state->ks_buffered_char[1] = 0;
1361 bzero(&state->ks_ndata, sizeof(state->ks_ndata));
1362 bzero(&state->ks_odata, sizeof(state->ks_odata));
1363 bzero(&state->ks_ntime, sizeof(state->ks_ntime));
1364 bzero(&state->ks_otime, sizeof(state->ks_otime));
1367 /* save the internal state */
1369 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1372 return sizeof(ukbd_state_t);
1373 if (len < sizeof(ukbd_state_t))
1375 bcopy(kbd->kb_data, buf, sizeof(ukbd_state_t));
1379 /* set the internal state */
1381 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1383 if (len < sizeof(ukbd_state_t))
1385 bcopy(buf, kbd->kb_data, sizeof(ukbd_state_t));
1390 ukbd_poll(keyboard_t *kbd, int on)
1392 ukbd_state_t *state;
1393 usbd_device_handle dev;
1395 state = (ukbd_state_t *)kbd->kb_data;
1396 usbd_interface2device_handle(state->ks_iface, &dev);
1400 ++state->ks_polling;
1401 if (state->ks_polling == 1)
1402 usbd_set_polling(dev, on);
1404 --state->ks_polling;
1405 if (state->ks_polling == 0)
1406 usbd_set_polling(dev, on);
1412 /* local functions */
1415 probe_keyboard(struct usb_attach_arg *uaa, int flags)
1417 usb_interface_descriptor_t *id;
1419 if (!uaa->iface) /* we attach to ifaces only */
1422 /* Check that this is a keyboard that speaks the boot protocol. */
1423 id = usbd_get_interface_descriptor(uaa->iface);
1425 && id->bInterfaceClass == UICLASS_HID
1426 && id->bInterfaceSubClass == UISUBCLASS_BOOT
1427 && id->bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)
1428 return 0; /* found it */
1434 init_keyboard(ukbd_state_t *state, int *type, int flags)
1436 usb_endpoint_descriptor_t *ed;
1441 state->ks_ifstate |= DISCONNECTED;
1443 ed = usbd_interface2endpoint_descriptor(state->ks_iface, 0);
1445 kprintf("ukbd: could not read endpoint descriptor\n");
1449 DPRINTFN(10,("ukbd:init_keyboard: \
1450 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
1451 ed->bLength, ed->bDescriptorType,
1452 UE_GET_ADDR(ed->bEndpointAddress),
1453 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
1454 UE_GET_XFERTYPE(ed->bmAttributes),
1455 UGETW(ed->wMaxPacketSize), ed->bInterval));
1457 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
1458 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
1459 kprintf("ukbd: unexpected endpoint\n");
1463 if ((usbd_get_quirks(state->ks_uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
1464 err = usbd_set_protocol(state->ks_iface, 0);
1465 DPRINTFN(5, ("ukbd:init_keyboard: protocol set\n"));
1467 kprintf("ukbd: set protocol failed\n");
1471 /* Ignore if SETIDLE fails since it is not crucial. */
1472 usbd_set_idle(state->ks_iface, 0, 0);
1474 state->ks_ep_addr = ed->bEndpointAddress;
1475 state->ks_ifstate &= ~DISCONNECTED;
1481 set_leds(ukbd_state_t *state, int leds)
1483 u_int8_t res = leds;
1485 DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state, leds));
1487 usbd_set_report_async(state->ks_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
1491 set_typematic(keyboard_t *kbd, int code)
1493 static int delays[] = { 250, 500, 750, 1000 };
1494 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1495 68, 76, 84, 92, 100, 110, 118, 126,
1496 136, 152, 168, 184, 200, 220, 236, 252,
1497 272, 304, 336, 368, 400, 440, 472, 504 };
1501 kbd->kb_delay1 = delays[(code >> 5) & 3];
1502 kbd->kb_delay2 = rates[code & 0x1f];
1506 #ifdef UKBD_EMULATE_ATSCANCODE
1508 keycode2scancode(int keycode, int shift, int up)
1510 static int scan[] = {
1512 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1513 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1514 0x50, 0x51, 0x52, 0x53,
1515 0x46, /* XXX Pause/Break */
1521 if ((keycode >= 89) && (keycode < 89 + sizeof(scan)/sizeof(scan[0])))
1522 scancode = scan[keycode - 89] | SCAN_PREFIX_E0;
1524 if ((keycode == 104) && !(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))
1525 scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL;
1526 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R))
1527 scancode &= ~SCAN_PREFIX_SHIFT;
1528 return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS));
1530 #endif /* UKBD_EMULATE_ATSCANCODE */
1533 ukbd_driver_load(module_t mod, int what, void *arg)
1537 kbd_add_driver(&ukbd_kbd_driver);
1540 kbd_delete_driver(&ukbd_kbd_driver);
1543 return usbd_driver_load(mod, what, 0);