2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Lennart Augustsson (lennart@augustsson.net) at
7 * Carlstedt Research & Technology.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
46 #include "opt_compat.h"
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 <sys/limits.h>
58 #include <sys/selinfo.h>
59 #include <sys/sysctl.h>
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbhid.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usb_quirks.h>
68 #include <dev/usb/hid.h>
71 #include <dev/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) printf x
81 #define DPRINTFN(n,x) if (ukbddebug>(n)) printf 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 usbd_intr_t ukbd_intr;
122 static int ukbd_driver_load(module_t mod, int what, void *arg);
124 static device_probe_t ukbd_match;
125 static device_attach_t ukbd_attach;
126 static device_detach_t ukbd_detach;
127 static device_resume_t ukbd_resume;
129 static device_method_t ukbd_methods[] = {
130 /* Device interface */
131 DEVMETHOD(device_probe, ukbd_match),
132 DEVMETHOD(device_attach, ukbd_attach),
133 DEVMETHOD(device_detach, ukbd_detach),
134 DEVMETHOD(device_resume, ukbd_resume),
139 static driver_t ukbd_driver = {
142 sizeof(struct ukbd_softc)
145 static devclass_t ukbd_devclass;
147 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
148 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
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 if (usbd_get_quirks(uaa->device)->uq_flags & UQ_KBD_IGNORE)
169 return (UMATCH_NONE);
171 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
175 ukbd_attach(device_t self)
177 struct ukbd_softc *sc = device_get_softc(self);
178 struct usb_attach_arg *uaa = device_get_ivars(self);
179 usbd_interface_handle iface = uaa->iface;
180 usb_interface_descriptor_t *id;
182 keyboard_switch_t *sw;
185 int unit = device_get_unit(self);
188 sw = kbd_get_switch(DRIVER_NAME);
192 id = usbd_get_interface_descriptor(iface);
194 arg[0] = (void *)uaa;
195 arg[1] = (void *)ukbd_intr;
197 if ((*sw->probe)(unit, (void *)arg, 0))
199 if ((*sw->init)(unit, &kbd, (void *)arg, 0))
203 #ifdef KBD_INSTALL_CDEV
208 (*sw->diag)(kbd, bootverbose);
214 ukbd_detach(device_t self)
219 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
220 device_get_unit(self)));
222 DPRINTF(("%s: keyboard not attached!?\n", device_get_nameunit(self)));
225 (*kbdsw[kbd->kb_index]->disable)(kbd);
227 #ifdef KBD_INSTALL_CDEV
228 error = kbd_detach(kbd);
232 error = (*kbdsw[kbd->kb_index]->term)(kbd);
236 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
242 ukbd_resume(device_t self)
246 kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
247 device_get_unit(self)));
249 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
254 ukbd_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
256 keyboard_t *kbd = (keyboard_t *)addr;
258 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)status);
261 #define UKBD_DEFAULT 0
263 #define KEY_ERROR 0x01
266 #define KEY_RELEASE 0x400
267 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE)
270 #define SCAN_RELEASE 0x80
271 #define SCAN_PREFIX_E0 0x100
272 #define SCAN_PREFIX_E1 0x200
273 #define SCAN_PREFIX_CTL 0x400
274 #define SCAN_PREFIX_SHIFT 0x800
275 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL \
277 #define SCAN_CHAR(c) ((c) & 0x7f)
282 } ukbd_mods[NMOD] = {
283 { MOD_CONTROL_L, 0xe0 },
284 { MOD_CONTROL_R, 0xe4 },
285 { MOD_SHIFT_L, 0xe1 },
286 { MOD_SHIFT_R, 0xe5 },
293 #define NN 0 /* no translation */
295 * Translate USB keycodes to AT keyboard scancodes.
298 * FIXME: Mac USB keyboard generates:
299 * 0x53: keypad NumLock/Clear
306 static u_int8_t ukbd_trtab[256] = {
307 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
308 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
309 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
310 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
311 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
312 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
313 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
314 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
315 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
316 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
317 97, 100, 95, 69, 91, 55, 74, 78, /* 50 - 57 */
318 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
319 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
320 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
321 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
322 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
323 121, 120, NN, NN, NN, NN, NN, 115, /* 80 - 87 */
324 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */
325 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
326 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
327 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
328 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
329 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
330 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
331 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
332 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
333 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
334 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
335 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
336 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
337 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
338 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
341 typedef struct ukbd_state {
342 usbd_interface_handle ks_iface; /* interface */
343 usbd_pipe_handle ks_intrpipe; /* interrupt pipe */
344 struct usb_attach_arg *ks_uaa;
347 struct ukbd_data ks_ndata;
348 struct ukbd_data ks_odata;
349 u_long ks_ntime[NKEYCODE];
350 u_long ks_otime[NKEYCODE];
352 #define INPUTBUFSIZE (NMOD + 2*NKEYCODE)
353 u_int ks_input[INPUTBUFSIZE]; /* input buffer */
359 #define INTRENABLED (1 << 0)
360 #define DISCONNECTED (1 << 1)
362 struct callout ks_timeout_handle;
364 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
365 int ks_flags; /* flags */
366 #define COMPOSE (1 << 0)
368 int ks_state; /* shift/lock key state */
369 int ks_accents; /* accent key index (> 0) */
370 u_int ks_composed_char; /* composed char code (> 0) */
371 #ifdef UKBD_EMULATE_ATSCANCODE
372 u_int ks_buffered_char[2];
373 u_int8_t ks_leds; /* store for async led requests */
377 /* keyboard driver declaration */
378 static int ukbd_configure(int flags);
379 static kbd_probe_t ukbd_probe;
380 static kbd_init_t ukbd_init;
381 static kbd_term_t ukbd_term;
382 static kbd_intr_t ukbd_interrupt;
383 static kbd_test_if_t ukbd_test_if;
384 static kbd_enable_t ukbd_enable;
385 static kbd_disable_t ukbd_disable;
386 static kbd_read_t ukbd_read;
387 static kbd_check_t ukbd_check;
388 static kbd_read_char_t ukbd_read_char;
389 static kbd_check_char_t ukbd_check_char;
390 static kbd_ioctl_t ukbd_ioctl;
391 static kbd_lock_t ukbd_lock;
392 static kbd_clear_state_t ukbd_clear_state;
393 static kbd_get_state_t ukbd_get_state;
394 static kbd_set_state_t ukbd_set_state;
395 static kbd_poll_mode_t ukbd_poll;
397 keyboard_switch_t ukbdsw = {
419 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
421 /* local functions */
422 static int ukbd_enable_intr(keyboard_t *kbd, int on,
424 static void ukbd_timeout(void *arg);
426 static int ukbd_getc(ukbd_state_t *state, int wait);
427 static int probe_keyboard(struct usb_attach_arg *uaa, int flags);
428 static int init_keyboard(ukbd_state_t *state, int *type,
430 static void set_leds(ukbd_state_t *state, int leds);
431 static int set_typematic(keyboard_t *kbd, int code);
432 #ifdef UKBD_EMULATE_ATSCANCODE
433 static int keycode2scancode(int keycode, int shift, int up);
436 /* local variables */
438 /* the initial key map, accent map and fkey strings */
439 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
440 #define KBD_DFLT_KEYMAP
443 #include <dev/kbd/kbdtables.h>
445 /* structures for the default keyboard */
446 static keyboard_t default_kbd;
447 static ukbd_state_t default_kbd_state;
448 static keymap_t default_keymap;
449 static accentmap_t default_accentmap;
450 static fkeytab_t default_fkeytab[NUM_FKEYS];
453 * The back door to the keyboard driver!
454 * This function is called by the console driver, via the kbdio module,
455 * to tickle keyboard drivers when the low-level console is being initialized.
456 * Almost nothing in the kernel has been initialied yet. Try to probe
457 * keyboards if possible.
458 * NOTE: because of the way the low-level conole is initialized, this routine
459 * may be called more than once!!
462 ukbd_configure(int flags)
469 struct usb_attach_arg *uaa;
472 device = devclass_get_device(ukbd_devclass, UKBD_DEFAULT);
475 uaa = (struct usb_attach_arg *)device_get_ivars(device);
479 /* probe the default keyboard */
480 arg[0] = (void *)uaa;
481 arg[1] = (void *)ukbd_intr;
483 if (ukbd_probe(UKBD_DEFAULT, arg, flags))
485 if (ukbd_init(UKBD_DEFAULT, &kbd, arg, flags))
488 /* return the number of found keyboards */
493 /* low-level functions */
495 /* detect a keyboard */
497 ukbd_probe(int unit, void *arg, int flags)
500 struct usb_attach_arg *uaa;
503 uaa = (struct usb_attach_arg *)data[0];
506 if (unit == UKBD_DEFAULT) {
507 if (KBD_IS_PROBED(&default_kbd))
510 if (probe_keyboard(uaa, flags))
515 /* reset and initialize the device */
517 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
525 void **data = (void **)arg;
526 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))
533 state = &default_kbd_state;
534 keymap = &default_keymap;
535 accmap = &default_accentmap;
536 fkeymap = default_fkeytab;
538 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
539 } else if (*kbdp == NULL) {
540 *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT);
543 bzero(kbd, sizeof(*kbd));
544 state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT);
545 keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
546 accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
547 fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
548 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
549 if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
550 || (fkeymap == NULL)) {
552 free(state, M_DEVBUF);
554 free(keymap, M_DEVBUF);
556 free(accmap, M_DEVBUF);
558 free(fkeymap, M_DEVBUF);
562 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
566 state = (ukbd_state_t *)kbd->kb_data;
567 keymap = kbd->kb_keymap;
568 accmap = kbd->kb_accentmap;
569 fkeymap = kbd->kb_fkeytab;
570 fkeymap_size = kbd->kb_fkeytab_size;
573 if (!KBD_IS_PROBED(kbd)) {
574 kbd_init_struct(kbd, DRIVER_NAME, KB_OTHER, unit, flags, 0, 0);
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);
587 ukbd_clear_state(kbd);
588 state->ks_mode = K_XLATE;
589 state->ks_iface = uaa->iface;
591 state->ks_ifstate = 0;
592 callout_init(&state->ks_timeout_handle, 0);
594 * FIXME: set the initial value for lock keys in ks_state
595 * according to the BIOS data?
599 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
600 if (KBD_HAS_DEVICE(kbd)
601 && init_keyboard((ukbd_state_t *)kbd->kb_data,
602 &kbd->kb_type, kbd->kb_flags)) {
604 /* XXX: Missing free()'s */
607 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&(state->ks_state));
610 if (!KBD_IS_CONFIGURED(kbd)) {
611 if (kbd_register(kbd) < 0) {
613 /* XXX: Missing free()'s */
616 if (ukbd_enable_intr(kbd, TRUE, (usbd_intr_t *)data[1]) == 0)
617 ukbd_timeout((void *)kbd);
618 KBD_CONFIG_DONE(kbd);
625 ukbd_enable_intr(keyboard_t *kbd, int on, usbd_intr_t *func)
627 ukbd_state_t *state = (ukbd_state_t *)kbd->kb_data;
631 /* Set up interrupt pipe. */
632 if (state->ks_ifstate & INTRENABLED)
635 state->ks_ifstate |= INTRENABLED;
636 err = usbd_open_pipe_intr(state->ks_iface, state->ks_ep_addr,
638 &state->ks_intrpipe, kbd,
640 sizeof(state->ks_ndata), func,
641 USBD_DEFAULT_INTERVAL);
645 /* Disable interrupts. */
646 usbd_abort_pipe(state->ks_intrpipe);
647 usbd_close_pipe(state->ks_intrpipe);
649 state->ks_ifstate &= ~INTRENABLED;
655 /* finish using this keyboard */
657 ukbd_term(keyboard_t *kbd)
665 state = (ukbd_state_t *)kbd->kb_data;
666 DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state->ks_ifstate));
668 callout_stop(&state->ks_timeout_handle);
670 if (state->ks_ifstate & INTRENABLED)
671 ukbd_enable_intr(kbd, FALSE, NULL);
672 if (state->ks_ifstate & INTRENABLED) {
674 DPRINTF(("ukbd_term: INTRENABLED!\n"));
678 error = kbd_unregister(kbd);
679 DPRINTF(("ukbd_term: kbd_unregister() %d\n", error));
682 if (kbd != &default_kbd) {
683 free(kbd->kb_keymap, M_DEVBUF);
684 free(kbd->kb_accentmap, M_DEVBUF);
685 free(kbd->kb_fkeytab, M_DEVBUF);
686 free(state, M_DEVBUF);
696 /* keyboard interrupt routine */
699 ukbd_timeout(void *arg)
705 kbd = (keyboard_t *)arg;
706 state = (ukbd_state_t *)kbd->kb_data;
708 (*kbdsw[kbd->kb_index]->intr)(kbd, (void *)USBD_NORMAL_COMPLETION);
709 callout_reset(&state->ks_timeout_handle, hz / 40, ukbd_timeout, arg);
714 ukbd_interrupt(keyboard_t *kbd, void *arg)
716 usbd_status status = (usbd_status)arg;
718 struct ukbd_data *ud;
725 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
726 if (status == USBD_CANCELLED)
729 state = (ukbd_state_t *)kbd->kb_data;
730 ud = &state->ks_ndata;
732 if (status != USBD_NORMAL_COMPLETION) {
733 DPRINTF(("ukbd_intr: status=%d\n", status));
734 if (status == USBD_STALLED)
735 usbd_clear_endpoint_stall_async(state->ks_intrpipe);
739 if (ud->keycode[0] == KEY_ERROR)
740 return 0; /* ignore */
743 now = (u_long)tv.tv_sec*1000 + (u_long)tv.tv_usec/1000;
746 if (state->ks_inputs < INPUTBUFSIZE) { \
747 state->ks_input[state->ks_inputtail] = (c); \
748 ++state->ks_inputs; \
749 state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \
753 omod = state->ks_odata.modifiers;
755 for (i = 0; i < NMOD; i++)
756 if (( mod & ukbd_mods[i].mask) !=
757 (omod & ukbd_mods[i].mask))
758 ADDKEY1(ukbd_mods[i].key |
759 (mod & ukbd_mods[i].mask
760 ? KEY_PRESS : KEY_RELEASE));
763 /* Check for released keys. */
764 for (i = 0; i < NKEYCODE; i++) {
765 key = state->ks_odata.keycode[i];
768 for (j = 0; j < NKEYCODE; j++) {
769 if (ud->keycode[j] == 0)
771 if (key == ud->keycode[j])
774 ADDKEY1(key | KEY_RELEASE);
779 /* Check for pressed keys. */
780 for (i = 0; i < NKEYCODE; i++) {
781 key = ud->keycode[i];
784 state->ks_ntime[i] = now + kbd->kb_delay1;
785 for (j = 0; j < NKEYCODE; j++) {
786 if (state->ks_odata.keycode[j] == 0)
788 if (key == state->ks_odata.keycode[j]) {
789 state->ks_ntime[i] = state->ks_otime[j];
790 if (state->ks_otime[j] > now)
792 state->ks_ntime[i] = now + kbd->kb_delay2;
796 ADDKEY1(key | KEY_PRESS);
798 * If any other key is presently down, force its repeat to be
799 * well in the future (100s). This makes the last key to be
800 * pressed do the autorepeat.
802 for (j = 0; j < NKEYCODE; j++) {
804 state->ks_ntime[j] = now + 100 * 1000;
810 state->ks_odata = *ud;
811 bcopy(state->ks_ntime, state->ks_otime, sizeof(state->ks_ntime));
812 if (state->ks_inputs <= 0)
816 for (i = state->ks_inputhead, j = 0; j < state->ks_inputs; ++j,
817 i = (i + 1)%INPUTBUFSIZE) {
818 c = state->ks_input[i];
819 DPRINTF(("0x%x (%d) %s\n", c, c,
820 (c & KEY_RELEASE) ? "released":"pressed"));
823 DPRINTF(("mod:0x%04x ", ud->modifiers));
824 for (i = 0; i < NKEYCODE; i++) {
826 DPRINTF(("%d ", ud->keycode[i]));
829 #endif /* USB_DEBUG */
831 if (state->ks_polling)
834 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
835 /* let the callback function to process the input */
836 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
837 kbd->kb_callback.kc_arg);
839 /* read and discard the input; no one is waiting for it */
841 c = ukbd_read_char(kbd, FALSE);
842 } while (c != NOKEY);
849 ukbd_getc(ukbd_state_t *state, int wait)
854 if (state->ks_polling) {
855 DPRINTFN(1,("ukbd_getc: polling\n"));
857 while (state->ks_inputs <= 0) {
858 usbd_dopoll(state->ks_iface);
865 if (state->ks_inputs <= 0) {
868 c = state->ks_input[state->ks_inputhead];
870 state->ks_inputhead = (state->ks_inputhead + 1)%INPUTBUFSIZE;
876 /* test the interface to the device */
878 ukbd_test_if(keyboard_t *kbd)
884 * Enable the access to the device; until this function is called,
885 * the client cannot read from the keyboard.
888 ukbd_enable(keyboard_t *kbd)
898 /* disallow the access to the device */
900 ukbd_disable(keyboard_t *kbd)
910 /* read one byte from the keyboard if it's allowed */
912 ukbd_read(keyboard_t *kbd, int wait)
916 #ifdef UKBD_EMULATE_ATSCANCODE
921 state = (ukbd_state_t *)kbd->kb_data;
922 #ifdef UKBD_EMULATE_ATSCANCODE
923 if (state->ks_buffered_char[0]) {
924 scancode = state->ks_buffered_char[0];
925 if (scancode & SCAN_PREFIX) {
926 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
927 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
929 state->ks_buffered_char[0] = state->ks_buffered_char[1];
930 state->ks_buffered_char[1] = 0;
934 #endif /* UKBD_EMULATE_ATSCANCODE */
937 usbcode = ukbd_getc(state, wait);
938 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
941 #ifdef UKBD_EMULATE_ATSCANCODE
942 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
946 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
947 usbcode & KEY_RELEASE);
948 if (scancode & SCAN_PREFIX) {
949 if (scancode & SCAN_PREFIX_CTL) {
950 state->ks_buffered_char[0] =
951 0x1d | (scancode & SCAN_RELEASE); /* Ctrl */
952 state->ks_buffered_char[1] = scancode & ~SCAN_PREFIX;
953 } else if (scancode & SCAN_PREFIX_SHIFT) {
954 state->ks_buffered_char[0] =
955 0x2a | (scancode & SCAN_RELEASE); /* Shift */
956 state->ks_buffered_char[1] =
957 scancode & ~SCAN_PREFIX_SHIFT;
959 state->ks_buffered_char[0] = scancode & ~SCAN_PREFIX;
960 state->ks_buffered_char[1] = 0;
962 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
965 #else /* !UKBD_EMULATE_ATSCANCODE */
967 #endif /* UKBD_EMULATE_ATSCANCODE */
970 /* check if data is waiting */
972 ukbd_check(keyboard_t *kbd)
974 if (!KBD_IS_ACTIVE(kbd))
976 #ifdef UKBD_EMULATE_ATSCANCODE
977 if (((ukbd_state_t *)kbd->kb_data)->ks_buffered_char[0])
980 if (((ukbd_state_t *)kbd->kb_data)->ks_inputs > 0)
985 /* read char from the keyboard */
987 ukbd_read_char(keyboard_t *kbd, int wait)
993 #ifdef UKBD_EMULATE_ATSCANCODE
997 state = (ukbd_state_t *)kbd->kb_data;
999 /* do we have a composed char to return? */
1000 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
1001 action = state->ks_composed_char;
1002 state->ks_composed_char = 0;
1003 if (action > UCHAR_MAX)
1008 #ifdef UKBD_EMULATE_ATSCANCODE
1009 /* do we have a pending raw scan code? */
1010 if (state->ks_mode == K_RAW) {
1011 if (state->ks_buffered_char[0]) {
1012 scancode = state->ks_buffered_char[0];
1013 if (scancode & SCAN_PREFIX) {
1014 state->ks_buffered_char[0] =
1015 scancode & ~SCAN_PREFIX;
1016 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1018 state->ks_buffered_char[0] =
1019 state->ks_buffered_char[1];
1020 state->ks_buffered_char[1] = 0;
1025 #endif /* UKBD_EMULATE_ATSCANCODE */
1027 /* see if there is something in the keyboard port */
1029 usbcode = ukbd_getc(state, wait);
1034 #ifdef UKBD_EMULATE_ATSCANCODE
1035 /* USB key index -> key code -> AT scan code */
1036 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1040 /* return an AT scan code for the K_RAW mode */
1041 if (state->ks_mode == K_RAW) {
1042 scancode = keycode2scancode(keycode, state->ks_ndata.modifiers,
1043 usbcode & KEY_RELEASE);
1044 if (scancode & SCAN_PREFIX) {
1045 if (scancode & SCAN_PREFIX_CTL) {
1046 state->ks_buffered_char[0] =
1047 0x1d | (scancode & SCAN_RELEASE);
1048 state->ks_buffered_char[1] =
1049 scancode & ~SCAN_PREFIX;
1050 } else if (scancode & SCAN_PREFIX_SHIFT) {
1051 state->ks_buffered_char[0] =
1052 0x2a | (scancode & SCAN_RELEASE);
1053 state->ks_buffered_char[1] =
1054 scancode & ~SCAN_PREFIX_SHIFT;
1056 state->ks_buffered_char[0] =
1057 scancode & ~SCAN_PREFIX;
1058 state->ks_buffered_char[1] = 0;
1060 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1064 #else /* !UKBD_EMULATE_ATSCANCODE */
1065 /* return the byte as is for the K_RAW mode */
1066 if (state->ks_mode == K_RAW)
1069 /* USB key index -> key code */
1070 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1073 #endif /* UKBD_EMULATE_ATSCANCODE */
1076 case 0x38: /* left alt (compose key) */
1077 if (usbcode & KEY_RELEASE) {
1078 if (state->ks_flags & COMPOSE) {
1079 state->ks_flags &= ~COMPOSE;
1080 if (state->ks_composed_char > UCHAR_MAX)
1081 state->ks_composed_char = 0;
1084 if (!(state->ks_flags & COMPOSE)) {
1085 state->ks_flags |= COMPOSE;
1086 state->ks_composed_char = 0;
1090 /* XXX: I don't like these... */
1091 case 0x5c: /* print screen */
1092 if (state->ks_flags & ALTS)
1093 keycode = 0x54; /* sysrq */
1095 case 0x68: /* pause/break */
1096 if (state->ks_flags & CTLS)
1097 keycode = 0x6c; /* break */
1101 /* return the key code in the K_CODE mode */
1102 if (usbcode & KEY_RELEASE)
1103 keycode |= SCAN_RELEASE;
1104 if (state->ks_mode == K_CODE)
1107 /* compose a character code */
1108 if (state->ks_flags & COMPOSE) {
1110 /* key pressed, process it */
1111 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
1112 state->ks_composed_char *= 10;
1113 state->ks_composed_char += keycode - 0x40;
1114 if (state->ks_composed_char > UCHAR_MAX)
1117 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
1118 state->ks_composed_char *= 10;
1119 state->ks_composed_char += keycode - 0x47;
1120 if (state->ks_composed_char > UCHAR_MAX)
1123 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
1124 state->ks_composed_char *= 10;
1125 state->ks_composed_char += keycode - 0x4E;
1126 if (state->ks_composed_char > UCHAR_MAX)
1129 case 0x52: /* keypad 0 */
1130 state->ks_composed_char *= 10;
1131 if (state->ks_composed_char > UCHAR_MAX)
1135 /* key released, no interest here */
1136 case SCAN_RELEASE | 0x47:
1137 case SCAN_RELEASE | 0x48:
1138 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1139 case SCAN_RELEASE | 0x4B:
1140 case SCAN_RELEASE | 0x4C:
1141 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1142 case SCAN_RELEASE | 0x4F:
1143 case SCAN_RELEASE | 0x50:
1144 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1145 case SCAN_RELEASE | 0x52: /* keypad 0 */
1148 case 0x38: /* left alt key */
1152 if (state->ks_composed_char > 0) {
1153 state->ks_flags &= ~COMPOSE;
1154 state->ks_composed_char = 0;
1161 /* keycode to key action */
1162 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1163 keycode & SCAN_RELEASE, &state->ks_state,
1164 &state->ks_accents);
1165 if (action == NOKEY)
1171 /* check if char is waiting */
1173 ukbd_check_char(keyboard_t *kbd)
1175 ukbd_state_t *state;
1177 if (!KBD_IS_ACTIVE(kbd))
1179 state = (ukbd_state_t *)kbd->kb_data;
1180 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
1182 return ukbd_check(kbd);
1185 /* some useful control functions */
1187 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1189 /* trasnlate LED_XXX bits into the device specific bits */
1190 static u_char ledmap[8] = {
1191 0, 2, 1, 3, 4, 6, 5, 7,
1193 ukbd_state_t *state = kbd->kb_data;
1196 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1197 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1204 case KDGKBMODE: /* get keyboard mode */
1205 *(int *)arg = state->ks_mode;
1207 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1208 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1210 ival = IOCPARM_IVAL(arg);
1211 arg = (caddr_t)&ival;
1214 case KDSKBMODE: /* set keyboard mode */
1215 switch (*(int *)arg) {
1217 if (state->ks_mode != K_XLATE) {
1218 /* make lock key state and LED state match */
1219 state->ks_state &= ~LOCK_MASK;
1220 state->ks_state |= KBD_LED_VAL(kbd);
1225 if (state->ks_mode != *(int *)arg) {
1226 ukbd_clear_state(kbd);
1227 state->ks_mode = *(int *)arg;
1236 case KDGETLED: /* get keyboard LED */
1237 *(int *)arg = KBD_LED_VAL(kbd);
1239 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1240 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1242 ival = IOCPARM_IVAL(arg);
1243 arg = (caddr_t)&ival;
1246 case KDSETLED: /* set keyboard LED */
1247 /* NOTE: lock key state in ks_state won't be changed */
1248 if (*(int *)arg & ~LOCK_MASK) {
1253 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1254 if (state->ks_mode == K_XLATE &&
1255 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1261 if (KBD_HAS_DEVICE(kbd)) {
1262 set_leds(state, ledmap[i & LED_MASK]);
1263 /* XXX: error check? */
1265 KBD_LED_VAL(kbd) = *(int *)arg;
1268 case KDGKBSTATE: /* get lock key state */
1269 *(int *)arg = state->ks_state & LOCK_MASK;
1271 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1272 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1274 ival = IOCPARM_IVAL(arg);
1275 arg = (caddr_t)&ival;
1278 case KDSKBSTATE: /* set lock key state */
1279 if (*(int *)arg & ~LOCK_MASK) {
1283 state->ks_state &= ~LOCK_MASK;
1284 state->ks_state |= *(int *)arg;
1286 /* set LEDs and quit */
1287 return ukbd_ioctl(kbd, KDSETLED, arg);
1289 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
1291 if (!KBD_HAS_DEVICE(kbd))
1293 if (((int *)arg)[1] < 0)
1295 if (((int *)arg)[0] < 0)
1297 else if (((int *)arg)[0] == 0) /* fastest possible value */
1298 kbd->kb_delay1 = 200;
1300 kbd->kb_delay1 = ((int *)arg)[0];
1301 kbd->kb_delay2 = ((int *)arg)[1];
1304 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1305 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1307 ival = IOCPARM_IVAL(arg);
1308 arg = (caddr_t)&ival;
1311 case KDSETRAD: /* set keyboard repeat rate (old interface) */
1313 return set_typematic(kbd, *(int *)arg);
1315 case PIO_KEYMAP: /* set keyboard translation table */
1316 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1317 case PIO_DEADKEYMAP: /* set accent key translation table */
1318 state->ks_accents = 0;
1322 return genkbd_commonioctl(kbd, cmd, arg);
1326 ukbddebug = *(int *)arg;
1335 /* lock the access to the keyboard */
1337 ukbd_lock(keyboard_t *kbd, int lock)
1343 /* clear the internal state of the keyboard */
1345 ukbd_clear_state(keyboard_t *kbd)
1347 ukbd_state_t *state;
1349 state = (ukbd_state_t *)kbd->kb_data;
1350 state->ks_flags = 0;
1351 state->ks_polling = 0;
1352 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1353 state->ks_accents = 0;
1354 state->ks_composed_char = 0;
1355 #ifdef UKBD_EMULATE_ATSCANCODE
1356 state->ks_buffered_char[0] = 0;
1357 state->ks_buffered_char[1] = 0;
1359 bzero(&state->ks_ndata, sizeof(state->ks_ndata));
1360 bzero(&state->ks_odata, sizeof(state->ks_odata));
1361 bzero(&state->ks_ntime, sizeof(state->ks_ntime));
1362 bzero(&state->ks_otime, sizeof(state->ks_otime));
1365 /* save the internal state */
1367 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1370 return sizeof(ukbd_state_t);
1371 if (len < sizeof(ukbd_state_t))
1373 bcopy(kbd->kb_data, buf, sizeof(ukbd_state_t));
1377 /* set the internal state */
1379 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1381 if (len < sizeof(ukbd_state_t))
1383 bcopy(buf, kbd->kb_data, sizeof(ukbd_state_t));
1388 ukbd_poll(keyboard_t *kbd, int on)
1390 ukbd_state_t *state;
1391 usbd_device_handle dev;
1394 state = (ukbd_state_t *)kbd->kb_data;
1395 usbd_interface2device_handle(state->ks_iface, &dev);
1399 ++state->ks_polling;
1400 if (state->ks_polling == 1)
1401 usbd_set_polling(dev, on);
1403 --state->ks_polling;
1404 if (state->ks_polling == 0)
1405 usbd_set_polling(dev, on);
1411 /* local functions */
1414 probe_keyboard(struct usb_attach_arg *uaa, int flags)
1416 usb_interface_descriptor_t *id;
1418 if (!uaa->iface) /* we attach to ifaces only */
1421 /* Check that this is a keyboard that speaks the boot protocol. */
1422 id = usbd_get_interface_descriptor(uaa->iface);
1424 && id->bInterfaceClass == UICLASS_HID
1425 && id->bInterfaceSubClass == UISUBCLASS_BOOT
1426 && id->bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)
1427 return 0; /* found it */
1433 init_keyboard(ukbd_state_t *state, int *type, int flags)
1435 usb_endpoint_descriptor_t *ed;
1439 state->ks_ifstate |= DISCONNECTED;
1441 ed = usbd_interface2endpoint_descriptor(state->ks_iface, 0);
1443 printf("ukbd: could not read endpoint descriptor\n");
1447 DPRINTFN(10,("ukbd:init_keyboard: \
1448 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
1449 ed->bLength, ed->bDescriptorType,
1450 UE_GET_ADDR(ed->bEndpointAddress),
1451 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
1452 UE_GET_XFERTYPE(ed->bmAttributes),
1453 UGETW(ed->wMaxPacketSize), ed->bInterval));
1455 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
1456 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
1457 printf("ukbd: unexpected endpoint\n");
1461 /* Ignore if SETIDLE fails since it is not crucial. */
1462 usbd_set_idle(state->ks_iface, 0, 0);
1464 state->ks_ep_addr = ed->bEndpointAddress;
1465 state->ks_ifstate &= ~DISCONNECTED;
1471 set_leds(ukbd_state_t *state, int leds)
1474 DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state, leds));
1475 state->ks_leds = leds;
1476 usbd_set_report_async(state->ks_iface, UHID_OUTPUT_REPORT, 0,
1477 &state->ks_leds, 1);
1481 set_typematic(keyboard_t *kbd, int code)
1483 static int delays[] = { 250, 500, 750, 1000 };
1484 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1485 68, 76, 84, 92, 100, 110, 118, 126,
1486 136, 152, 168, 184, 200, 220, 236, 252,
1487 272, 304, 336, 368, 400, 440, 472, 504 };
1491 kbd->kb_delay1 = delays[(code >> 5) & 3];
1492 kbd->kb_delay2 = rates[code & 0x1f];
1496 #ifdef UKBD_EMULATE_ATSCANCODE
1498 keycode2scancode(int keycode, int shift, int up)
1500 static int scan[] = {
1502 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1503 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1504 0x50, 0x51, 0x52, 0x53,
1505 0x46, /* XXX Pause/Break */
1507 /* SUN TYPE 6 USB KEYBOARD */
1508 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
1509 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e,
1515 if ((keycode >= 89) && (keycode < 89 + sizeof(scan)/sizeof(scan[0])))
1516 scancode = scan[keycode - 89] | SCAN_PREFIX_E0;
1518 if ((keycode == 104) && !(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))
1519 scancode = 0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL;
1520 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R))
1521 scancode &= ~SCAN_PREFIX_SHIFT;
1522 return (scancode | (up ? SCAN_RELEASE : SCAN_PRESS));
1524 #endif /* UKBD_EMULATE_ATSCANCODE */
1527 ukbd_driver_load(module_t mod, int what, void *arg)
1531 kbd_add_driver(&ukbd_kbd_driver);
1534 kbd_delete_driver(&ukbd_kbd_driver);
1537 return usbd_driver_load(mod, what, 0);