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.8 2004/03/15 02:27:57 dillon 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 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
58 #include <sys/limits.h>
60 #include <machine/limits.h>
62 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
63 #include <sys/selinfo.h>
65 #include <sys/select.h>
67 #include <sys/vnode.h>
68 #include <sys/sysctl.h>
70 #include <bus/usb/usb.h>
71 #include <bus/usb/usbhid.h>
72 #include <bus/usb/usbdi.h>
73 #include <bus/usb/usbdi_util.h>
74 #include <bus/usb/usbdevs.h>
75 #include <bus/usb/usb_quirks.h>
76 #include <bus/usb/hid.h>
79 #include <dev/misc/kbd/kbdreg.h>
81 #define UKBD_EMULATE_ATSCANCODE 1
83 #define DRIVER_NAME "ukbd"
85 #define delay(d) DELAY(d)
88 #define DPRINTF(x) if (ukbddebug) logprintf x
89 #define DPRINTFN(n,x) if (ukbddebug>(n)) logprintf x
91 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
92 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
93 &ukbddebug, 0, "ukbd debug level");
99 #define UPROTO_BOOT_KEYBOARD 1
105 #define MOD_CONTROL_L 0x01
106 #define MOD_CONTROL_R 0x10
107 #define MOD_SHIFT_L 0x02
108 #define MOD_SHIFT_R 0x20
109 #define MOD_ALT_L 0x04
110 #define MOD_ALT_R 0x40
111 #define MOD_WIN_L 0x08
112 #define MOD_WIN_R 0x80
114 u_int8_t keycode[NKEYCODE];
117 #define MAXKEYS (NMOD+2*NKEYCODE)
119 typedef struct ukbd_softc {
120 device_t sc_dev; /* base device */
123 #define UKBD_CHUNK 128 /* chunk size for read */
124 #define UKBD_BSIZE 1020 /* buffer size */
126 typedef void usbd_intr_t(usbd_xfer_handle, usbd_private_handle, usbd_status);
127 typedef void usbd_disco_t(void *);
129 Static int ukbd_resume(device_t self);
130 Static usbd_intr_t ukbd_intr;
131 Static int ukbd_driver_load(module_t mod, int what, void *arg);
133 USB_DECLARE_DRIVER_INIT(ukbd, DEVMETHOD(device_resume, ukbd_resume));
137 USB_MATCH_START(ukbd, uaa);
139 keyboard_switch_t *sw;
141 int unit = device_get_unit(self);
143 sw = kbd_get_switch(DRIVER_NAME);
145 return (UMATCH_NONE);
147 arg[0] = (void *)uaa;
148 arg[1] = (void *)ukbd_intr;
149 if ((*sw->probe)(unit, (void *)arg, 0))
150 return (UMATCH_NONE);
152 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
157 USB_ATTACH_START(ukbd, sc, uaa);
158 usbd_interface_handle iface = uaa->iface;
159 usb_interface_descriptor_t *id;
162 keyboard_switch_t *sw;
165 int unit = device_get_unit(self);
167 sw = kbd_get_switch(DRIVER_NAME);
169 USB_ATTACH_ERROR_RETURN;
171 id = usbd_get_interface_descriptor(iface);
172 usbd_devinfo(uaa->device, 0, devinfo);
174 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
175 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
177 arg[0] = (void *)uaa;
178 arg[1] = (void *)ukbd_intr;
180 if ((*sw->probe)(unit, (void *)arg, 0))
181 USB_ATTACH_ERROR_RETURN;
182 if ((*sw->init)(unit, &kbd, (void *)arg, 0))
183 USB_ATTACH_ERROR_RETURN;
186 #ifdef KBD_INSTALL_CDEV
188 USB_ATTACH_ERROR_RETURN;
191 (*sw->diag)(kbd, bootverbose);
193 USB_ATTACH_SUCCESS_RETURN;
197 ukbd_detach(device_t self)
202 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
203 device_get_unit(self)));
205 DPRINTF(("%s: keyboard not attached!?\n", USBDEVNAME(self)));
208 (*kbdsw[kbd->kb_index]->disable)(kbd);
210 #ifdef KBD_INSTALL_CDEV
211 error = kbd_detach(kbd);
215 error = (*kbdsw[kbd->kb_index]->term)(kbd);
219 DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
225 ukbd_resume(device_t self)
229 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
230 device_get_unit(self)));
232 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
237 ukbd_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
239 keyboard_t *kbd = (keyboard_t *)addr;
241 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)status);
244 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
247 #define UKBD_DEFAULT 0
249 #define KEY_ERROR 0x01
252 #define KEY_RELEASE 0x400
253 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE)
256 #define SCAN_RELEASE 0x80
257 #define SCAN_PREFIX_E0 0x100
258 #define SCAN_PREFIX_E1 0x200
259 #define SCAN_PREFIX_CTL 0x400
260 #define SCAN_PREFIX_SHIFT 0x800
261 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL \
263 #define SCAN_CHAR(c) ((c) & 0x7f)
268 } ukbd_mods[NMOD] = {
269 { MOD_CONTROL_L, 0xe0 },
270 { MOD_CONTROL_R, 0xe4 },
271 { MOD_SHIFT_L, 0xe1 },
272 { MOD_SHIFT_R, 0xe5 },
279 #define NN 0 /* no translation */
281 * Translate USB keycodes to AT keyboard scancodes.
284 * FIXME: Mac USB keyboard generates:
285 * 0x53: keypad NumLock/Clear
292 Static u_int8_t ukbd_trtab[256] = {
293 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
294 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
295 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
296 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
297 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
298 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
299 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
300 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
301 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
302 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
303 97, 100, 95, 69, 91, 55, 74, 78, /* 50 - 57 */
304 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
305 72, 73, 82, 83, 86, 107, NN, NN, /* 60 - 67 */
306 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
307 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
308 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7F */
309 NN, NN, NN, NN, NN, NN, NN, 115, /* 80 - 87 */
310 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */
311 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
312 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
313 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
314 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
315 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
316 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
317 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
318 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
319 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
320 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
321 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
322 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
323 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
324 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
327 typedef struct ukbd_state {
328 usbd_interface_handle ks_iface; /* interface */
329 usbd_pipe_handle ks_intrpipe; /* interrupt pipe */
330 struct usb_attach_arg *ks_uaa;
333 struct ukbd_data ks_ndata;
334 struct ukbd_data ks_odata;
335 u_long ks_ntime[NKEYCODE];
336 u_long ks_otime[NKEYCODE];
338 #define INPUTBUFSIZE (NMOD + 2*NKEYCODE)
339 u_int ks_input[INPUTBUFSIZE]; /* input buffer */
345 #define INTRENABLED (1 << 0)
346 #define DISCONNECTED (1 << 1)
348 struct callout_handle ks_timeout_handle;
350 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
351 int ks_flags; /* flags */
352 #define COMPOSE (1 << 0)
354 int ks_state; /* shift/lock key state */
355 int ks_accents; /* accent key index (> 0) */
356 u_int ks_composed_char; /* composed char code (> 0) */
357 #ifdef UKBD_EMULATE_ATSCANCODE
358 u_int ks_buffered_char[2];
362 /* keyboard driver declaration */
363 Static int ukbd_configure(int flags);
364 Static kbd_probe_t ukbd_probe;
365 Static kbd_init_t ukbd_init;
366 Static kbd_term_t ukbd_term;
367 Static kbd_intr_t ukbd_interrupt;
368 Static kbd_test_if_t ukbd_test_if;
369 Static kbd_enable_t ukbd_enable;
370 Static kbd_disable_t ukbd_disable;
371 Static kbd_read_t ukbd_read;
372 Static kbd_check_t ukbd_check;
373 Static kbd_read_char_t ukbd_read_char;
374 Static kbd_check_char_t ukbd_check_char;
375 Static kbd_ioctl_t ukbd_ioctl;
376 Static kbd_lock_t ukbd_lock;
377 Static kbd_clear_state_t ukbd_clear_state;
378 Static kbd_get_state_t ukbd_get_state;
379 Static kbd_set_state_t ukbd_set_state;
380 Static kbd_poll_mode_t ukbd_poll;
382 keyboard_switch_t ukbdsw = {
404 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
406 /* local functions */
407 Static int ukbd_enable_intr(keyboard_t *kbd, int on,
409 Static timeout_t ukbd_timeout;
411 Static int ukbd_getc(ukbd_state_t *state);
412 Static int probe_keyboard(struct usb_attach_arg *uaa, int flags);
413 Static int init_keyboard(ukbd_state_t *state, int *type,
415 Static void set_leds(ukbd_state_t *state, int leds);
416 Static int set_typematic(keyboard_t *kbd, int code);
417 #ifdef UKBD_EMULATE_ATSCANCODE
418 Static int keycode2scancode(int keycode, int shift, int up);
421 /* local variables */
423 /* the initial key map, accent map and fkey strings */
424 #ifdef UKBD_DFLT_KEYMAP
425 #define KBD_DFLT_KEYMAP
428 #include <dev/misc/kbd/kbdtables.h>
430 /* structures for the default keyboard */
431 Static keyboard_t default_kbd;
432 Static ukbd_state_t default_kbd_state;
433 Static keymap_t default_keymap;
434 Static accentmap_t default_accentmap;
435 Static fkeytab_t default_fkeytab[NUM_FKEYS];
438 * The back door to the keyboard driver!
439 * This function is called by the console driver, via the kbdio module,
440 * to tickle keyboard drivers when the low-level console is being initialized.
441 * Almost nothing in the kernel has been initialied yet. Try to probe
442 * keyboards if possible.
443 * NOTE: because of the way the low-level conole is initialized, this routine
444 * may be called more than once!!
447 ukbd_configure(int flags)
454 struct usb_attach_arg *uaa;
457 device = devclass_get_device(ukbd_devclass, UKBD_DEFAULT);
460 uaa = (struct usb_attach_arg *)device_get_ivars(device);
464 /* probe the default keyboard */
465 arg[0] = (void *)uaa;
466 arg[1] = (void *)ukbd_intr;
468 if (ukbd_probe(UKBD_DEFAULT, arg, flags))
470 if (ukbd_init(UKBD_DEFAULT, &kbd, arg, flags))
473 /* return the number of found keyboards */
478 /* low-level functions */
480 /* detect a keyboard */
482 ukbd_probe(int unit, void *arg, int flags)
485 struct usb_attach_arg *uaa;
488 uaa = (struct usb_attach_arg *)data[0];
491 if (unit == UKBD_DEFAULT) {
492 if (KBD_IS_PROBED(&default_kbd))
495 if (probe_keyboard(uaa, flags))
500 /* reset and initialize the device */
502 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
510 void **data = (void **)arg;
511 struct usb_attach_arg *uaa = (struct usb_attach_arg *)data[0];
514 if (unit == UKBD_DEFAULT) {
515 *kbdp = kbd = &default_kbd;
516 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
518 state = &default_kbd_state;
519 keymap = &default_keymap;
520 accmap = &default_accentmap;
521 fkeymap = default_fkeytab;
523 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
524 } else if (*kbdp == NULL) {
525 *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_INTWAIT | M_ZERO);
526 state = malloc(sizeof(*state), M_DEVBUF, M_INTWAIT);
527 keymap = malloc(sizeof(key_map), M_DEVBUF, M_INTWAIT);
528 accmap = malloc(sizeof(accent_map), M_DEVBUF, M_INTWAIT);
529 fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_INTWAIT);
530 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
531 if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
532 || (fkeymap == NULL)) {
534 free(state, M_DEVBUF);
536 free(keymap, M_DEVBUF);
538 free(accmap, M_DEVBUF);
540 free(fkeymap, M_DEVBUF);
544 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
548 state = (ukbd_state_t *)kbd->kb_data;
549 keymap = kbd->kb_keymap;
550 accmap = kbd->kb_accentmap;
551 fkeymap = kbd->kb_fkeytab;
552 fkeymap_size = kbd->kb_fkeytab_size;
555 if (!KBD_IS_PROBED(kbd)) {
556 kbd_init_struct(kbd, DRIVER_NAME, KB_OTHER, unit, flags, 0, 0);
557 bzero(state, sizeof(*state));
558 bcopy(&key_map, keymap, sizeof(key_map));
559 bcopy(&accent_map, accmap, sizeof(accent_map));
560 bcopy(fkey_tab, fkeymap,
561 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
562 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
563 kbd->kb_data = (void *)state;
565 if (probe_keyboard(uaa, flags))
568 KBD_FOUND_DEVICE(kbd);
569 ukbd_clear_state(kbd);
570 state->ks_mode = K_XLATE;
571 state->ks_iface = uaa->iface;
573 state->ks_ifstate = 0;
574 callout_handle_init(&state->ks_timeout_handle);
576 * FIXME: set the initial value for lock keys in ks_state
577 * according to the BIOS data?
581 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
582 if (KBD_HAS_DEVICE(kbd)
583 && init_keyboard((ukbd_state_t *)kbd->kb_data,
584 &kbd->kb_type, kbd->kb_flags))
586 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&(state->ks_state));
589 if (!KBD_IS_CONFIGURED(kbd)) {
590 if (kbd_register(kbd) < 0)
592 if (ukbd_enable_intr(kbd, TRUE, (usbd_intr_t *)data[1]) == 0)
593 ukbd_timeout((void *)kbd);
594 KBD_CONFIG_DONE(kbd);
601 ukbd_enable_intr(keyboard_t *kbd, int on, usbd_intr_t *func)
603 ukbd_state_t *state = (ukbd_state_t *)kbd->kb_data;
607 /* Set up interrupt pipe. */
608 if (state->ks_ifstate & INTRENABLED)
611 state->ks_ifstate |= INTRENABLED;
612 err = usbd_open_pipe_intr(state->ks_iface, state->ks_ep_addr,
614 &state->ks_intrpipe, kbd,
616 sizeof(state->ks_ndata), func,
617 USBD_DEFAULT_INTERVAL);
621 /* Disable interrupts. */
622 usbd_abort_pipe(state->ks_intrpipe);
623 usbd_close_pipe(state->ks_intrpipe);
625 state->ks_ifstate &= ~INTRENABLED;
631 /* finish using this keyboard */
633 ukbd_term(keyboard_t *kbd)
641 state = (ukbd_state_t *)kbd->kb_data;
642 DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state->ks_ifstate));
644 untimeout(ukbd_timeout, (void *)kbd, state->ks_timeout_handle);
645 callout_handle_init(&state->ks_timeout_handle);
647 if (state->ks_ifstate & INTRENABLED)
648 ukbd_enable_intr(kbd, FALSE, NULL);
649 if (state->ks_ifstate & INTRENABLED) {
651 DPRINTF(("ukbd_term: INTRENABLED!\n"));
655 error = kbd_unregister(kbd);
656 DPRINTF(("ukbd_term: kbd_unregister() %d\n", error));
659 if (kbd != &default_kbd) {
660 free(kbd->kb_keymap, M_DEVBUF);
661 free(kbd->kb_accentmap, M_DEVBUF);
662 free(kbd->kb_fkeytab, M_DEVBUF);
663 free(state, M_DEVBUF);
673 /* keyboard interrupt routine */
676 ukbd_timeout(void *arg)
682 kbd = (keyboard_t *)arg;
683 state = (ukbd_state_t *)kbd->kb_data;
685 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)USBD_NORMAL_COMPLETION);
686 state->ks_timeout_handle = timeout(ukbd_timeout, arg, hz/40);
691 ukbd_interrupt(keyboard_t *kbd, void *arg)
693 usbd_status status = (usbd_status)arg;
695 struct ukbd_data *ud;
702 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
703 if (status == USBD_CANCELLED)
706 state = (ukbd_state_t *)kbd->kb_data;
707 ud = &state->ks_ndata;
709 if (status != USBD_NORMAL_COMPLETION) {
710 DPRINTF(("ukbd_intr: status=%d\n", status));
711 if (status == USBD_STALLED)
712 usbd_clear_endpoint_stall_async(state->ks_intrpipe);
716 if (ud->keycode[0] == KEY_ERROR)
717 return 0; /* ignore */
720 now = (u_long)tv.tv_sec*1000 + (u_long)tv.tv_usec/1000;
723 if (state->ks_inputs < INPUTBUFSIZE) { \
724 state->ks_input[state->ks_inputtail] = (c); \
725 ++state->ks_inputs; \
726 state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \
730 omod = state->ks_odata.modifiers;
732 for (i = 0; i < NMOD; i++)
733 if (( mod & ukbd_mods[i].mask) !=
734 (omod & ukbd_mods[i].mask))
735 ADDKEY1(ukbd_mods[i].key |
736 (mod & ukbd_mods[i].mask
737 ? KEY_PRESS : KEY_RELEASE));
740 /* Check for released keys. */
741 for (i = 0; i < NKEYCODE; i++) {
742 key = state->ks_odata.keycode[i];
745 for (j = 0; j < NKEYCODE; j++) {
746 if (ud->keycode[j] == 0)
748 if (key == ud->keycode[j])
751 ADDKEY1(key | KEY_RELEASE);
756 /* Check for pressed keys. */
757 for (i = 0; i < NKEYCODE; i++) {
758 key = ud->keycode[i];
761 state->ks_ntime[i] = now + kbd->kb_delay1;
762 for (j = 0; j < NKEYCODE; j++) {
763 if (state->ks_odata.keycode[j] == 0)
765 if (key == state->ks_odata.keycode[j]) {
766 state->ks_ntime[i] = state->ks_otime[j];
767 if (state->ks_otime[j] > now)
769 state->ks_ntime[i] = now + kbd->kb_delay2;
773 ADDKEY1(key | KEY_PRESS);
778 state->ks_odata = *ud;
779 bcopy(state->ks_ntime, state->ks_otime, sizeof(state->ks_ntime));
780 if (state->ks_inputs <= 0)
784 for (i = state->ks_inputhead, j = 0; j < state->ks_inputs; ++j,
785 i = (i + 1)%INPUTBUFSIZE) {
786 c = state->ks_input[i];
787 DPRINTF(("0x%x (%d) %s\n", c, c,
788 (c & KEY_RELEASE) ? "released":"pressed"));
791 DPRINTF(("mod:0x%04x ", ud->modifiers));
792 for (i = 0; i < NKEYCODE; i++) {
794 DPRINTF(("%d ", ud->keycode[i]));
797 #endif /* USB_DEBUG */
799 if (state->ks_polling)
802 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
803 /* let the callback function to process the input */
804 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
805 kbd->kb_callback.kc_arg);
807 /* read and discard the input; no one is waiting for it */
809 c = ukbd_read_char(kbd, FALSE);
810 } while (c != NOKEY);
817 ukbd_getc(ukbd_state_t *state)
822 if (state->ks_polling) {
823 DPRINTFN(1,("ukbd_getc: polling\n"));
825 while (state->ks_inputs <= 0)
826 usbd_dopoll(state->ks_iface);
830 if (state->ks_inputs <= 0) {
833 c = state->ks_input[state->ks_inputhead];
835 state->ks_inputhead = (state->ks_inputhead + 1)%INPUTBUFSIZE;
841 /* test the interface to the device */
843 ukbd_test_if(keyboard_t *kbd)
849 * Enable the access to the device; until this function is called,
850 * the client cannot read from the keyboard.
853 ukbd_enable(keyboard_t *kbd)
863 /* disallow the access to the device */
865 ukbd_disable(keyboard_t *kbd)
875 /* read one byte from the keyboard if it's allowed */
877 ukbd_read(keyboard_t *kbd, int wait)
881 #ifdef UKBD_EMULATE_ATSCANCODE
886 state = (ukbd_state_t *)kbd->kb_data;
887 #ifdef UKBD_EMULATE_ATSCANCODE
888 if (state->ks_buffered_char[0]) {
889 scancode = state->ks_buffered_char[0];
890 if (scancode & SCAN_PREFIX) {
891 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
892 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
894 state->ks_buffered_char[0] = state->ks_buffered_char[1];
895 state->ks_buffered_char[1] = 0;
899 #endif /* UKBD_EMULATE_ATSCANCODE */
902 usbcode = ukbd_getc(state);
903 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
906 #ifdef UKBD_EMULATE_ATSCANCODE
907 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
911 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
912 usbcode & KEY_RELEASE);
913 if (scancode & SCAN_PREFIX) {
914 if (scancode & SCAN_PREFIX_CTL) {
915 state->ks_buffered_char[0] =
916 0x1d | (scancode & SCAN_RELEASE); /* Ctrl */
917 state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX;
918 } else if (scancode & SCAN_PREFIX_SHIFT) {
919 state->ks_buffered_char[0] =
920 0x2a | (scancode & SCAN_RELEASE); /* Shift */
921 state->ks_buffered_char[1] =
922 scancode & ~SCAN_PREFIX_SHIFT;
924 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
925 state->ks_buffered_char[1] = 0;
927 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
930 #else /* !UKBD_EMULATE_ATSCANCODE */
932 #endif /* UKBD_EMULATE_ATSCANCODE */
935 /* check if data is waiting */
937 ukbd_check(keyboard_t *kbd)
939 if (!KBD_IS_ACTIVE(kbd))
941 #ifdef UKBD_EMULATE_ATSCANCODE
942 if (((ukbd_state_t *)kbd->kb_data)->ks_buffered_char[0])
945 if (((ukbd_state_t *)kbd->kb_data)->ks_inputs > 0)
950 /* read char from the keyboard */
952 ukbd_read_char(keyboard_t *kbd, int wait)
958 #ifdef UKBD_EMULATE_ATSCANCODE
962 state = (ukbd_state_t *)kbd->kb_data;
964 /* do we have a composed char to return? */
965 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
966 action = state->ks_composed_char;
967 state->ks_composed_char = 0;
968 if (action > UCHAR_MAX)
973 #ifdef UKBD_EMULATE_ATSCANCODE
974 /* do we have a pending raw scan code? */
975 if (state->ks_mode == K_RAW) {
976 if (state->ks_buffered_char[0]) {
977 scancode = state->ks_buffered_char[0];
978 if (scancode & SCAN_PREFIX) {
979 state->ks_buffered_char[0] =
980 scancode & ~SCAN_PREFIX;
981 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
983 state->ks_buffered_char[0] =
984 state->ks_buffered_char[1];
985 state->ks_buffered_char[1] = 0;
990 #endif /* UKBD_EMULATE_ATSCANCODE */
992 /* see if there is something in the keyboard port */
994 usbcode = ukbd_getc(state);
999 #ifdef UKBD_EMULATE_ATSCANCODE
1000 /* USB key index -> key code -> AT scan code */
1001 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1005 /* return an AT scan code for the K_RAW mode */
1006 if (state->ks_mode == K_RAW) {
1007 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
1008 usbcode & KEY_RELEASE);
1009 if (scancode & SCAN_PREFIX) {
1010 if (scancode & SCAN_PREFIX_CTL) {
1011 state->ks_buffered_char[0] =
1012 0x1d | (scancode & SCAN_RELEASE);
1013 state->ks_buffered_char[1] =
1014 scancode & ~SCAN_PREFIX;
1015 } else if (scancode & SCAN_PREFIX_SHIFT) {
1016 state->ks_buffered_char[0] =
1017 0x2a | (scancode & SCAN_RELEASE);
1018 state->ks_buffered_char[1] =
1019 scancode & ~SCAN_PREFIX_SHIFT;
1021 state->ks_buffered_char[0] =
1022 scancode & ~SCAN_PREFIX;
1023 state->ks_buffered_char[1] = 0;
1025 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1029 #else /* !UKBD_EMULATE_ATSCANCODE */
1030 /* return the byte as is for the K_RAW mode */
1031 if (state->ks_mode == K_RAW)
1034 /* USB key index -> key code */
1035 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1038 #endif /* UKBD_EMULATE_ATSCANCODE */
1041 case 0x38: /* left alt (compose key) */
1042 if (usbcode & KEY_RELEASE) {
1043 if (state->ks_flags & COMPOSE) {
1044 state->ks_flags &= ~COMPOSE;
1045 if (state->ks_composed_char > UCHAR_MAX)
1046 state->ks_composed_char = 0;
1049 if (!(state->ks_flags & COMPOSE)) {
1050 state->ks_flags |= COMPOSE;
1051 state->ks_composed_char = 0;
1055 /* XXX: I don't like these... */
1056 case 0x5c: /* print screen */
1057 if (state->ks_flags & ALTS)
1058 keycode = 0x54; /* sysrq */
1060 case 0x68: /* pause/break */
1061 if (state->ks_flags & CTLS)
1062 keycode = 0x6c; /* break */
1066 /* return the key code in the K_CODE mode */
1067 if (usbcode & KEY_RELEASE)
1068 keycode |= SCAN_RELEASE;
1069 if (state->ks_mode == K_CODE)
1072 /* compose a character code */
1073 if (state->ks_flags & COMPOSE) {
1075 /* key pressed, process it */
1076 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
1077 state->ks_composed_char *= 10;
1078 state->ks_composed_char += keycode - 0x40;
1079 if (state->ks_composed_char > UCHAR_MAX)
1082 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
1083 state->ks_composed_char *= 10;
1084 state->ks_composed_char += keycode - 0x47;
1085 if (state->ks_composed_char > UCHAR_MAX)
1088 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
1089 state->ks_composed_char *= 10;
1090 state->ks_composed_char += keycode - 0x4E;
1091 if (state->ks_composed_char > UCHAR_MAX)
1094 case 0x52: /* keypad 0 */
1095 state->ks_composed_char *= 10;
1096 if (state->ks_composed_char > UCHAR_MAX)
1100 /* key released, no interest here */
1101 case SCAN_RELEASE | 0x47:
1102 case SCAN_RELEASE | 0x48:
1103 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1104 case SCAN_RELEASE | 0x4B:
1105 case SCAN_RELEASE | 0x4C:
1106 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1107 case SCAN_RELEASE | 0x4F:
1108 case SCAN_RELEASE | 0x50:
1109 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1110 case SCAN_RELEASE | 0x52: /* keypad 0 */
1113 case 0x38: /* left alt key */
1117 if (state->ks_composed_char > 0) {
1118 state->ks_flags &= ~COMPOSE;
1119 state->ks_composed_char = 0;
1126 /* keycode to key action */
1127 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1128 keycode & SCAN_RELEASE, &state->ks_state,
1129 &state->ks_accents);
1130 if (action == NOKEY)
1136 /* check if char is waiting */
1138 ukbd_check_char(keyboard_t *kbd)
1140 ukbd_state_t *state;
1142 if (!KBD_IS_ACTIVE(kbd))
1144 state = (ukbd_state_t *)kbd->kb_data;
1145 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
1147 if (state->ks_inputs > 0)
1152 /* some useful control functions */
1154 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1156 /* trasnlate LED_XXX bits into the device specific bits */
1157 static u_char ledmap[8] = {
1158 0, 2, 1, 3, 4, 6, 5, 7,
1160 ukbd_state_t *state = kbd->kb_data;
1167 case KDGKBMODE: /* get keyboard mode */
1168 *(int *)arg = state->ks_mode;
1170 case KDSKBMODE: /* set keyboard mode */
1171 switch (*(int *)arg) {
1173 if (state->ks_mode != K_XLATE) {
1174 /* make lock key state and LED state match */
1175 state->ks_state &= ~LOCK_MASK;
1176 state->ks_state |= KBD_LED_VAL(kbd);
1181 if (state->ks_mode != *(int *)arg) {
1182 ukbd_clear_state(kbd);
1183 state->ks_mode = *(int *)arg;
1192 case KDGETLED: /* get keyboard LED */
1193 *(int *)arg = KBD_LED_VAL(kbd);
1195 case KDSETLED: /* set keyboard LED */
1196 /* NOTE: lock key state in ks_state won't be changed */
1197 if (*(int *)arg & ~LOCK_MASK) {
1202 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1203 if (kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1209 if (KBD_HAS_DEVICE(kbd)) {
1210 set_leds(state, ledmap[i & LED_MASK]);
1211 /* XXX: error check? */
1213 KBD_LED_VAL(kbd) = *(int *)arg;
1216 case KDGKBSTATE: /* get lock key state */
1217 *(int *)arg = state->ks_state & LOCK_MASK;
1219 case KDSKBSTATE: /* set lock key state */
1220 if (*(int *)arg & ~LOCK_MASK) {
1224 state->ks_state &= ~LOCK_MASK;
1225 state->ks_state |= *(int *)arg;
1227 /* set LEDs and quit */
1228 return ukbd_ioctl(kbd, KDSETLED, arg);
1230 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
1232 if (!KBD_HAS_DEVICE(kbd))
1234 if (((int *)arg)[1] < 0)
1236 if (((int *)arg)[0] < 0)
1238 else if (((int *)arg)[0] == 0) /* fastest possible value */
1239 kbd->kb_delay1 = 200;
1241 kbd->kb_delay1 = ((int *)arg)[0];
1242 kbd->kb_delay2 = ((int *)arg)[1];
1245 case KDSETRAD: /* set keyboard repeat rate (old interface) */
1247 return set_typematic(kbd, *(int *)arg);
1249 case PIO_KEYMAP: /* set keyboard translation table */
1250 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1251 case PIO_DEADKEYMAP: /* set accent key translation table */
1252 state->ks_accents = 0;
1256 return genkbd_commonioctl(kbd, cmd, arg);
1260 ukbddebug = *(int *)arg;
1269 /* lock the access to the keyboard */
1271 ukbd_lock(keyboard_t *kbd, int lock)
1277 /* clear the internal state of the keyboard */
1279 ukbd_clear_state(keyboard_t *kbd)
1281 ukbd_state_t *state;
1283 state = (ukbd_state_t *)kbd->kb_data;
1284 state->ks_flags = 0;
1285 state->ks_polling = 0;
1286 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1287 state->ks_accents = 0;
1288 state->ks_composed_char = 0;
1289 #ifdef UKBD_EMULATE_ATSCANCODE
1290 state->ks_buffered_char[0] = 0;
1291 state->ks_buffered_char[1] = 0;
1293 bzero(&state->ks_ndata, sizeof(state->ks_ndata));
1294 bzero(&state->ks_odata, sizeof(state->ks_odata));
1295 bzero(&state->ks_ntime, sizeof(state->ks_ntime));
1296 bzero(&state->ks_otime, sizeof(state->ks_otime));
1299 /* save the internal state */
1301 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1304 return sizeof(ukbd_state_t);
1305 if (len < sizeof(ukbd_state_t))
1307 bcopy(kbd->kb_data, buf, sizeof(ukbd_state_t));
1311 /* set the internal state */
1313 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1315 if (len < sizeof(ukbd_state_t))
1317 bcopy(buf, kbd->kb_data, sizeof(ukbd_state_t));
1322 ukbd_poll(keyboard_t *kbd, int on)
1324 ukbd_state_t *state;
1325 usbd_device_handle dev;
1328 state = (ukbd_state_t *)kbd->kb_data;
1329 usbd_interface2device_handle(state->ks_iface, &dev);
1333 if (state->ks_polling == 0)
1334 usbd_set_polling(dev, on);
1335 ++state->ks_polling;
1337 --state->ks_polling;
1338 if (state->ks_polling == 0)
1339 usbd_set_polling(dev, on);
1345 /* local functions */
1348 probe_keyboard(struct usb_attach_arg *uaa, int flags)
1350 usb_interface_descriptor_t *id;
1352 if (!uaa->iface) /* we attach to ifaces only */
1355 /* Check that this is a keyboard that speaks the boot protocol. */
1356 id = usbd_get_interface_descriptor(uaa->iface);
1358 && id->bInterfaceClass == UICLASS_HID
1359 && id->bInterfaceSubClass == UISUBCLASS_BOOT
1360 && id->bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)
1361 return 0; /* found it */
1367 init_keyboard(ukbd_state_t *state, int *type, int flags)
1369 usb_endpoint_descriptor_t *ed;
1374 state->ks_ifstate |= DISCONNECTED;
1376 ed = usbd_interface2endpoint_descriptor(state->ks_iface, 0);
1378 printf("ukbd: could not read endpoint descriptor\n");
1382 DPRINTFN(10,("ukbd:init_keyboard: \
1383 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
1384 ed->bLength, ed->bDescriptorType,
1385 UE_GET_ADDR(ed->bEndpointAddress),
1386 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
1387 UE_GET_XFERTYPE(ed->bmAttributes),
1388 UGETW(ed->wMaxPacketSize), ed->bInterval));
1390 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
1391 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
1392 printf("ukbd: unexpected endpoint\n");
1396 if ((usbd_get_quirks(state->ks_uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
1397 err = usbd_set_protocol(state->ks_iface, 0);
1398 DPRINTFN(5, ("ukbd:init_keyboard: protocol set\n"));
1400 printf("ukbd: set protocol failed\n");
1404 /* Ignore if SETIDLE fails since it is not crucial. */
1405 usbd_set_idle(state->ks_iface, 0, 0);
1407 state->ks_ep_addr = ed->bEndpointAddress;
1408 state->ks_ifstate &= ~DISCONNECTED;
1414 set_leds(ukbd_state_t *state, int leds)
1416 u_int8_t res = leds;
1418 DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state, leds));
1420 usbd_set_report_async(state->ks_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
1424 set_typematic(keyboard_t *kbd, int code)
1426 static int delays[] = { 250, 500, 750, 1000 };
1427 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1428 68, 76, 84, 92, 100, 110, 118, 126,
1429 136, 152, 168, 184, 200, 220, 236, 252,
1430 272, 304, 336, 368, 400, 440, 472, 504 };
1434 kbd->kb_delay1 = delays[(code >> 5) & 3];
1435 kbd->kb_delay2 = rates[code & 0x1f];
1439 #ifdef UKBD_EMULATE_ATSCANCODE
1441 keycode2scancode(int keycode, int shift, int up)
1443 static int scan[] = {
1445 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1446 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1447 0x50, 0x51, 0x52, 0x53,
1448 0x46, /* XXX Pause/Break */
1454 if ((keycode >= 89) && (keycode < 89 + sizeof(scan)/sizeof(scan[0])))
1455 scancode = scan[keycode - 89] | SCAN_PREFIX_E0;
1457 if ((keycode == 104) && !(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))
1458 scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL;
1459 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R))
1460 scancode &= ~SCAN_PREFIX_SHIFT;
1461 return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS));
1463 #endif /* UKBD_EMULATE_ATSCANCODE */
1466 ukbd_driver_load(module_t mod, int what, void *arg)
1470 kbd_add_driver(&ukbd_kbd_driver);
1473 kbd_delete_driver(&ukbd_kbd_driver);
1476 return usbd_driver_load(mod, what, 0);