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.18 2007/06/06 18:49:28 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>
69 #include <sys/thread2.h>
71 #include <bus/usb/usb.h>
72 #include <bus/usb/usbhid.h>
73 #include <bus/usb/usbdi.h>
74 #include <bus/usb/usbdi_util.h>
75 #include <bus/usb/usbdevs.h>
76 #include <bus/usb/usb_quirks.h>
77 #include <bus/usb/hid.h>
80 #include <dev/misc/kbd/kbdreg.h>
82 #define UKBD_EMULATE_ATSCANCODE 1
84 #define DRIVER_NAME "ukbd"
86 #define delay(d) DELAY(d)
89 #define DPRINTF(x) if (ukbddebug) logprintf x
90 #define DPRINTFN(n,x) if (ukbddebug>(n)) logprintf x
92 SYSCTL_NODE(_hw_usb
, OID_AUTO
, ukbd
, CTLFLAG_RW
, 0, "USB ukbd");
93 SYSCTL_INT(_hw_usb_ukbd
, OID_AUTO
, debug
, CTLFLAG_RW
,
94 &ukbddebug
, 0, "ukbd debug level");
100 #define UPROTO_BOOT_KEYBOARD 1
106 #define MOD_CONTROL_L 0x01
107 #define MOD_CONTROL_R 0x10
108 #define MOD_SHIFT_L 0x02
109 #define MOD_SHIFT_R 0x20
110 #define MOD_ALT_L 0x04
111 #define MOD_ALT_R 0x40
112 #define MOD_WIN_L 0x08
113 #define MOD_WIN_R 0x80
115 u_int8_t keycode
[NKEYCODE
];
118 #define MAXKEYS (NMOD+2*NKEYCODE)
120 typedef struct ukbd_softc
{
121 device_t sc_dev
; /* base device */
124 #define UKBD_CHUNK 128 /* chunk size for read */
125 #define UKBD_BSIZE 1020 /* buffer size */
127 typedef void usbd_intr_t(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
128 typedef void usbd_disco_t(void *);
130 Static
int ukbd_resume(device_t self
);
131 Static usbd_intr_t ukbd_intr
;
132 Static
int ukbd_driver_load(module_t mod
, int what
, void *arg
);
133 Static
int ukbd_default_term(keyboard_t
*kbd
);
135 Static keyboard_t default_kbd
;
137 USB_DECLARE_DRIVER_INIT(ukbd
, DEVMETHOD(device_resume
, ukbd_resume
));
141 USB_MATCH_START(ukbd
, uaa
);
143 keyboard_switch_t
*sw
;
145 int unit
= device_get_unit(self
);
147 sw
= kbd_get_switch(DRIVER_NAME
);
149 return (UMATCH_NONE
);
151 arg
[0] = (void *)uaa
;
152 arg
[1] = (void *)ukbd_intr
;
153 if ((*sw
->probe
)(unit
, (void *)arg
, 0))
154 return (UMATCH_NONE
);
156 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO
);
161 USB_ATTACH_START(ukbd
, sc
, uaa
);
162 usbd_interface_handle iface
= uaa
->iface
;
163 usb_interface_descriptor_t
*id
;
166 keyboard_switch_t
*sw
;
169 int unit
= device_get_unit(self
);
171 sw
= kbd_get_switch(DRIVER_NAME
);
173 USB_ATTACH_ERROR_RETURN
;
175 id
= usbd_get_interface_descriptor(iface
);
176 usbd_devinfo(uaa
->device
, 0, devinfo
);
178 kprintf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc
->sc_dev
),
179 devinfo
, id
->bInterfaceClass
, id
->bInterfaceSubClass
);
181 arg
[0] = (void *)uaa
;
182 arg
[1] = (void *)ukbd_intr
;
184 if ((*sw
->probe
)(unit
, (void *)arg
, 0))
185 USB_ATTACH_ERROR_RETURN
;
186 if ((*sw
->init
)(unit
, &kbd
, (void *)arg
, 0))
187 USB_ATTACH_ERROR_RETURN
;
190 #ifdef KBD_INSTALL_CDEV
192 USB_ATTACH_ERROR_RETURN
;
195 (*sw
->diag
)(kbd
, bootverbose
);
197 USB_ATTACH_SUCCESS_RETURN
;
201 ukbd_detach(device_t self
)
206 kbd
= kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME
,
207 device_get_unit(self
)));
209 DPRINTF(("%s: keyboard not attached!?\n", USBDEVNAME(self
)));
212 (*kbdsw
[kbd
->kb_index
]->disable
)(kbd
);
214 #ifdef KBD_INSTALL_CDEV
215 if (kbd
!= &default_kbd
) {
216 error
= kbd_detach(kbd
);
221 if (kbd
== &default_kbd
) {
222 ukbd_default_term(kbd
);
224 error
= (*kbdsw
[kbd
->kb_index
]->term
)(kbd
);
229 DPRINTF(("%s: disconnected\n", USBDEVNAME(self
)));
235 ukbd_resume(device_t self
)
239 kbd
= kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME
,
240 device_get_unit(self
)));
242 (*kbdsw
[kbd
->kb_index
]->clear_state
)(kbd
);
247 ukbd_intr(usbd_xfer_handle xfer
, usbd_private_handle addr
, usbd_status status
)
249 keyboard_t
*kbd
= (keyboard_t
*)addr
;
251 (*kbdsw
[kbd
->kb_index
]->intr
)(kbd
, (void *)status
);
254 DRIVER_MODULE(ukbd
, uhub
, ukbd_driver
, ukbd_devclass
, ukbd_driver_load
, 0);
257 #define UKBD_DEFAULT 0
259 #define KEY_ERROR 0x01
262 #define KEY_RELEASE 0x400
263 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE)
266 #define SCAN_RELEASE 0x80
267 #define SCAN_PREFIX_E0 0x100
268 #define SCAN_PREFIX_E1 0x200
269 #define SCAN_PREFIX_CTL 0x400
270 #define SCAN_PREFIX_SHIFT 0x800
271 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL \
273 #define SCAN_CHAR(c) ((c) & 0x7f)
278 } ukbd_mods
[NMOD
] = {
279 { MOD_CONTROL_L
, 0xe0 },
280 { MOD_CONTROL_R
, 0xe4 },
281 { MOD_SHIFT_L
, 0xe1 },
282 { MOD_SHIFT_R
, 0xe5 },
289 #define NN 0 /* no translation */
291 * Translate USB keycodes to AT keyboard scancodes.
294 * FIXME: Mac USB keyboard generates:
295 * 0x53: keypad NumLock/Clear
302 Static u_int8_t ukbd_trtab
[256] = {
303 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
304 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
305 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
306 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
307 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
308 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
309 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
310 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
311 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
312 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
313 97, 100, 95, 69, 91, 55, 74, 78, /* 50 - 57 */
314 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
315 72, 73, 82, 83, 86, 107, NN
, NN
, /* 60 - 67 */
316 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* 68 - 6F */
317 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* 70 - 77 */
318 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* 78 - 7F */
319 NN
, NN
, NN
, NN
, NN
, NN
, NN
, 115, /* 80 - 87 */
320 112, 125, 121, 123, NN
, NN
, NN
, NN
, /* 88 - 8F */
321 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* 90 - 97 */
322 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* 98 - 9F */
323 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* A0 - A7 */
324 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* A8 - AF */
325 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* B0 - B7 */
326 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* B8 - BF */
327 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* C0 - C7 */
328 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* C8 - CF */
329 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* D0 - D7 */
330 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* D8 - DF */
331 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
332 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* E8 - EF */
333 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* F0 - F7 */
334 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* F8 - FF */
337 typedef struct ukbd_state
{
338 usbd_interface_handle ks_iface
; /* interface */
339 usbd_pipe_handle ks_intrpipe
; /* interrupt pipe */
340 struct usb_attach_arg
*ks_uaa
;
343 struct ukbd_data ks_ndata
;
344 struct ukbd_data ks_odata
;
345 u_long ks_ntime
[NKEYCODE
];
346 u_long ks_otime
[NKEYCODE
];
348 #define INPUTBUFSIZE (NMOD + 2*NKEYCODE)
349 u_int ks_input
[INPUTBUFSIZE
]; /* input buffer */
355 #define INTRENABLED (1 << 0)
356 #define DISCONNECTED (1 << 1)
358 struct callout ks_timeout
;
360 int ks_mode
; /* input mode (K_XLATE,K_RAW,K_CODE) */
361 int ks_flags
; /* flags */
362 #define COMPOSE (1 << 0)
364 int ks_state
; /* shift/lock key state */
365 int ks_accents
; /* accent key index (> 0) */
366 u_int ks_composed_char
; /* composed char code (> 0) */
367 #ifdef UKBD_EMULATE_ATSCANCODE
368 u_int ks_buffered_char
[2];
372 /* keyboard driver declaration */
373 Static
int ukbd_configure(int flags
);
374 Static kbd_probe_t ukbd_probe
;
375 Static kbd_init_t ukbd_init
;
376 Static kbd_term_t ukbd_term
;
377 Static kbd_intr_t ukbd_interrupt
;
378 Static kbd_test_if_t ukbd_test_if
;
379 Static kbd_enable_t ukbd_enable
;
380 Static kbd_disable_t ukbd_disable
;
381 Static kbd_read_t ukbd_read
;
382 Static kbd_check_t ukbd_check
;
383 Static kbd_read_char_t ukbd_read_char
;
384 Static kbd_check_char_t ukbd_check_char
;
385 Static kbd_ioctl_t ukbd_ioctl
;
386 Static kbd_lock_t ukbd_lock
;
387 Static kbd_clear_state_t ukbd_clear_state
;
388 Static kbd_get_state_t ukbd_get_state
;
389 Static kbd_set_state_t ukbd_set_state
;
390 Static kbd_poll_mode_t ukbd_poll
;
392 keyboard_switch_t ukbdsw
= {
414 KEYBOARD_DRIVER(ukbd
, ukbdsw
, ukbd_configure
);
416 /* local functions */
417 Static
int ukbd_enable_intr(keyboard_t
*kbd
, int on
,
419 Static timeout_t ukbd_timeout
;
421 Static
int ukbd_getc(ukbd_state_t
*state
);
422 Static
int probe_keyboard(struct usb_attach_arg
*uaa
, int flags
);
423 Static
int init_keyboard(ukbd_state_t
*state
, int *type
,
425 Static
void set_leds(ukbd_state_t
*state
, int leds
);
426 Static
int set_typematic(keyboard_t
*kbd
, int code
);
427 #ifdef UKBD_EMULATE_ATSCANCODE
428 Static
int keycode2scancode(int keycode
, int shift
, int up
);
431 /* local variables */
433 /* the initial key map, accent map and fkey strings */
434 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
435 #define KBD_DFLT_KEYMAP
438 #include <dev/misc/kbd/kbdtables.h>
440 /* structures for the default keyboard */
441 Static ukbd_state_t default_kbd_state
;
442 Static keymap_t default_keymap
;
443 Static accentmap_t default_accentmap
;
444 Static fkeytab_t default_fkeytab
[NUM_FKEYS
];
447 * The back door to the keyboard driver!
448 * This function is called by the console driver, via the kbdio module,
449 * to tickle keyboard drivers when the low-level console is being initialized.
450 * Almost nothing in the kernel has been initialied yet. Try to probe
451 * keyboards if possible.
452 * NOTE: because of the way the low-level conole is initialized, this routine
453 * may be called more than once!!
456 ukbd_configure(int flags
)
463 struct usb_attach_arg
*uaa
;
466 device
= devclass_get_device(ukbd_devclass
, UKBD_DEFAULT
);
469 uaa
= (struct usb_attach_arg
*)device_get_ivars(device
);
473 /* probe the default keyboard */
474 arg
[0] = (void *)uaa
;
475 arg
[1] = (void *)ukbd_intr
;
477 if (ukbd_probe(UKBD_DEFAULT
, arg
, flags
))
479 if (ukbd_init(UKBD_DEFAULT
, &kbd
, arg
, flags
))
482 /* return the number of found keyboards */
487 /* low-level functions */
489 /* detect a keyboard */
491 ukbd_probe(int unit
, void *arg
, int flags
)
494 struct usb_attach_arg
*uaa
;
497 uaa
= (struct usb_attach_arg
*)data
[0];
499 if (unit
== UKBD_DEFAULT
) {
500 if (KBD_IS_PROBED(&default_kbd
))
503 if (probe_keyboard(uaa
, flags
))
509 * Reset and initialize the device. Note that unit 0 (UKBD_DEFAULT) is an
510 * always-connected device once it has been initially detected. We do not
511 * deregister it if the usb keyboard is unplugged to avoid losing the
512 * connection to the console. This feature also handles the USB bus reset
513 * which detaches and reattaches USB devices during boot.
516 ukbd_init(int unit
, keyboard_t
**kbdp
, void *arg
, int flags
)
524 void **data
= (void **)arg
;
525 struct usb_attach_arg
*uaa
= (struct usb_attach_arg
*)data
[0];
527 if (unit
== UKBD_DEFAULT
) {
528 *kbdp
= kbd
= &default_kbd
;
529 if (KBD_IS_INITIALIZED(kbd
) && KBD_IS_CONFIGURED(kbd
))
531 state
= &default_kbd_state
;
532 keymap
= &default_keymap
;
533 accmap
= &default_accentmap
;
534 fkeymap
= default_fkeytab
;
536 sizeof(default_fkeytab
)/sizeof(default_fkeytab
[0]);
537 } else if (*kbdp
== NULL
) {
538 *kbdp
= kbd
= kmalloc(sizeof(*kbd
), M_DEVBUF
, M_INTWAIT
| M_ZERO
);
539 state
= kmalloc(sizeof(*state
), M_DEVBUF
, M_INTWAIT
);
540 keymap
= kmalloc(sizeof(key_map
), M_DEVBUF
, M_INTWAIT
);
541 accmap
= kmalloc(sizeof(accent_map
), M_DEVBUF
, M_INTWAIT
);
542 fkeymap
= kmalloc(sizeof(fkey_tab
), M_DEVBUF
, M_INTWAIT
);
543 fkeymap_size
= sizeof(fkey_tab
)/sizeof(fkey_tab
[0]);
544 if ((state
== NULL
) || (keymap
== NULL
) || (accmap
== NULL
)
545 || (fkeymap
== NULL
)) {
547 kfree(state
, M_DEVBUF
);
549 kfree(keymap
, M_DEVBUF
);
551 kfree(accmap
, M_DEVBUF
);
553 kfree(fkeymap
, M_DEVBUF
);
554 kfree(kbd
, M_DEVBUF
);
557 } else if (KBD_IS_INITIALIZED(*kbdp
) && KBD_IS_CONFIGURED(*kbdp
)) {
561 state
= (ukbd_state_t
*)kbd
->kb_data
;
562 keymap
= kbd
->kb_keymap
;
563 accmap
= kbd
->kb_accentmap
;
564 fkeymap
= kbd
->kb_fkeytab
;
565 fkeymap_size
= kbd
->kb_fkeytab_size
;
568 if (!KBD_IS_PROBED(kbd
)) {
569 if (KBD_IS_CONFIGURED(kbd
)) {
570 kbd_reinit_struct(kbd
, flags
, KB_PRI_USB
);
572 kbd_init_struct(kbd
, DRIVER_NAME
, KB_OTHER
,
573 unit
, flags
, KB_PRI_USB
,
576 bzero(state
, sizeof(*state
));
577 bcopy(&key_map
, keymap
, sizeof(key_map
));
578 bcopy(&accent_map
, accmap
, sizeof(accent_map
));
579 bcopy(fkey_tab
, fkeymap
,
580 imin(fkeymap_size
*sizeof(fkeymap
[0]), sizeof(fkey_tab
)));
581 kbd_set_maps(kbd
, keymap
, accmap
, fkeymap
, fkeymap_size
);
582 kbd
->kb_data
= (void *)state
;
584 if (probe_keyboard(uaa
, flags
))
587 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 if (!KBD_IS_CONFIGURED(kbd
)) {
597 state
->ks_mode
= K_XLATE
;
598 kbd
->kb_savemode
= state
->ks_mode
;
600 state
->ks_mode
= kbd
->kb_savemode
;
602 state
->ks_iface
= uaa
->iface
;
604 state
->ks_ifstate
= 0;
605 callout_init(&state
->ks_timeout
);
607 * FIXME: set the initial value for lock keys in ks_state
608 * according to the BIOS data?
612 if (!KBD_IS_INITIALIZED(kbd
) && !(flags
& KB_CONF_PROBE_ONLY
)) {
613 if (KBD_HAS_DEVICE(kbd
)
614 && init_keyboard((ukbd_state_t
*)kbd
->kb_data
,
615 &kbd
->kb_type
, kbd
->kb_flags
))
617 ukbd_ioctl(kbd
, KDSETLED
, (caddr_t
)&(state
->ks_state
));
619 if (!KBD_IS_CONFIGURED(kbd
)) {
620 if (kbd_register(kbd
) < 0)
622 KBD_CONFIG_DONE(kbd
);
624 if (!KBD_IS_INITIALIZED(kbd
) && !(flags
& KB_CONF_PROBE_ONLY
)) {
625 if (ukbd_enable_intr(kbd
, TRUE
, (usbd_intr_t
*)data
[1]) == 0)
626 ukbd_timeout((void *)kbd
);
633 ukbd_enable_intr(keyboard_t
*kbd
, int on
, usbd_intr_t
*func
)
635 ukbd_state_t
*state
= (ukbd_state_t
*)kbd
->kb_data
;
639 /* Set up interrupt pipe. */
640 if (state
->ks_ifstate
& INTRENABLED
)
643 state
->ks_ifstate
|= INTRENABLED
;
644 err
= usbd_open_pipe_intr(state
->ks_iface
, state
->ks_ep_addr
,
646 &state
->ks_intrpipe
, kbd
,
648 sizeof(state
->ks_ndata
), func
,
649 USBD_DEFAULT_INTERVAL
);
653 /* Disable interrupts. */
654 usbd_abort_pipe(state
->ks_intrpipe
);
655 usbd_close_pipe(state
->ks_intrpipe
);
657 state
->ks_ifstate
&= ~INTRENABLED
;
663 /* finish using this keyboard */
665 ukbd_term(keyboard_t
*kbd
)
671 state
= (ukbd_state_t
*)kbd
->kb_data
;
672 DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state
->ks_ifstate
));
674 callout_stop(&state
->ks_timeout
);
676 if (state
->ks_ifstate
& INTRENABLED
)
677 ukbd_enable_intr(kbd
, FALSE
, NULL
);
678 if (state
->ks_ifstate
& INTRENABLED
) {
680 DPRINTF(("ukbd_term: INTRENABLED!\n"));
684 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
);
701 * Finish using the default keyboard. Shutdown the USB side of the keyboard
702 * but do not unregister it.
705 ukbd_default_term(keyboard_t
*kbd
)
711 state
= (ukbd_state_t
*)kbd
->kb_data
;
712 DPRINTF(("ukbd_default_term: ks_ifstate=0x%x\n", state
->ks_ifstate
));
714 callout_stop(&state
->ks_timeout
);
716 if (state
->ks_ifstate
& INTRENABLED
)
717 ukbd_enable_intr(kbd
, FALSE
, NULL
);
718 if (state
->ks_ifstate
& INTRENABLED
) {
720 DPRINTF(("ukbd_term: INTRENABLED!\n"));
723 KBD_LOST_DEVICE(kbd
);
730 /* keyboard interrupt routine */
733 ukbd_timeout(void *arg
)
738 kbd
= (keyboard_t
*)arg
;
739 state
= (ukbd_state_t
*)kbd
->kb_data
;
741 (*kbdsw
[kbd
->kb_index
]->intr
)(kbd
, (void *)USBD_NORMAL_COMPLETION
);
742 callout_reset(&state
->ks_timeout
, hz
/ 40, ukbd_timeout
, arg
);
747 ukbd_interrupt(keyboard_t
*kbd
, void *arg
)
749 usbd_status status
= (usbd_status
)arg
;
751 struct ukbd_data
*ud
;
758 DPRINTFN(5, ("ukbd_intr: status=%d\n", status
));
759 if (status
== USBD_CANCELLED
)
762 state
= (ukbd_state_t
*)kbd
->kb_data
;
763 ud
= &state
->ks_ndata
;
765 if (status
!= USBD_NORMAL_COMPLETION
) {
766 DPRINTF(("ukbd_intr: status=%d\n", status
));
767 if (status
== USBD_STALLED
)
768 usbd_clear_endpoint_stall_async(state
->ks_intrpipe
);
772 if (ud
->keycode
[0] == KEY_ERROR
)
773 return 0; /* ignore */
776 now
= (u_long
)tv
.tv_sec
*1000 + (u_long
)tv
.tv_usec
/1000;
779 if (state->ks_inputs < INPUTBUFSIZE) { \
780 state->ks_input[state->ks_inputtail] = (c); \
781 ++state->ks_inputs; \
782 state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \
786 omod
= state
->ks_odata
.modifiers
;
788 for (i
= 0; i
< NMOD
; i
++)
789 if (( mod
& ukbd_mods
[i
].mask
) !=
790 (omod
& ukbd_mods
[i
].mask
))
791 ADDKEY1(ukbd_mods
[i
].key
|
792 (mod
& ukbd_mods
[i
].mask
793 ? KEY_PRESS
: KEY_RELEASE
));
796 /* Check for released keys. */
797 for (i
= 0; i
< NKEYCODE
; i
++) {
798 key
= state
->ks_odata
.keycode
[i
];
801 for (j
= 0; j
< NKEYCODE
; j
++) {
802 if (ud
->keycode
[j
] == 0)
804 if (key
== ud
->keycode
[j
])
807 ADDKEY1(key
| KEY_RELEASE
);
812 /* Check for pressed keys. */
813 for (i
= 0; i
< NKEYCODE
; i
++) {
814 key
= ud
->keycode
[i
];
817 state
->ks_ntime
[i
] = now
+ kbd
->kb_delay1
;
818 for (j
= 0; j
< NKEYCODE
; j
++) {
819 if (state
->ks_odata
.keycode
[j
] == 0)
821 if (key
== state
->ks_odata
.keycode
[j
]) {
822 state
->ks_ntime
[i
] = state
->ks_otime
[j
];
823 if (state
->ks_otime
[j
] > now
)
825 state
->ks_ntime
[i
] = now
+ kbd
->kb_delay2
;
829 ADDKEY1(key
| KEY_PRESS
);
834 state
->ks_odata
= *ud
;
835 bcopy(state
->ks_ntime
, state
->ks_otime
, sizeof(state
->ks_ntime
));
836 if (state
->ks_inputs
<= 0)
840 for (i
= state
->ks_inputhead
, j
= 0; j
< state
->ks_inputs
; ++j
,
841 i
= (i
+ 1)%INPUTBUFSIZE
) {
842 c
= state
->ks_input
[i
];
843 DPRINTF(("0x%x (%d) %s\n", c
, c
,
844 (c
& KEY_RELEASE
) ? "released":"pressed"));
847 DPRINTF(("mod:0x%04x ", ud
->modifiers
));
848 for (i
= 0; i
< NKEYCODE
; i
++) {
850 DPRINTF(("%d ", ud
->keycode
[i
]));
853 #endif /* USB_DEBUG */
855 if (state
->ks_polling
)
858 if (KBD_IS_ACTIVE(kbd
) && KBD_IS_BUSY(kbd
)) {
859 /* let the callback function to process the input */
860 (*kbd
->kb_callback
.kc_func
)(kbd
, KBDIO_KEYINPUT
,
861 kbd
->kb_callback
.kc_arg
);
863 /* read and discard the input; no one is waiting for it */
865 c
= ukbd_read_char(kbd
, FALSE
);
866 } while (c
!= NOKEY
);
873 ukbd_getc(ukbd_state_t
*state
)
877 if (state
->ks_polling
) {
878 DPRINTFN(1,("ukbd_getc: polling\n"));
880 while (state
->ks_inputs
<= 0)
881 usbd_dopoll(state
->ks_iface
);
885 if (state
->ks_inputs
<= 0) {
888 c
= state
->ks_input
[state
->ks_inputhead
];
890 state
->ks_inputhead
= (state
->ks_inputhead
+ 1)%INPUTBUFSIZE
;
896 /* test the interface to the device */
898 ukbd_test_if(keyboard_t
*kbd
)
904 * Enable the access to the device; until this function is called,
905 * the client cannot read from the keyboard.
908 ukbd_enable(keyboard_t
*kbd
)
916 /* disallow the access to the device */
918 ukbd_disable(keyboard_t
*kbd
)
926 /* read one byte from the keyboard if it's allowed */
928 ukbd_read(keyboard_t
*kbd
, int wait
)
932 #ifdef UKBD_EMULATE_ATSCANCODE
937 state
= (ukbd_state_t
*)kbd
->kb_data
;
938 #ifdef UKBD_EMULATE_ATSCANCODE
939 if (state
->ks_buffered_char
[0]) {
940 scancode
= state
->ks_buffered_char
[0];
941 if (scancode
& SCAN_PREFIX
) {
942 state
->ks_buffered_char
[0] = scancode
& ~SCAN_PREFIX
;
943 return ((scancode
& SCAN_PREFIX_E0
) ? 0xe0 : 0xe1);
945 state
->ks_buffered_char
[0] = state
->ks_buffered_char
[1];
946 state
->ks_buffered_char
[1] = 0;
950 #endif /* UKBD_EMULATE_ATSCANCODE */
953 usbcode
= ukbd_getc(state
);
954 if (!KBD_IS_ACTIVE(kbd
) || !KBD_HAS_DEVICE(kbd
) || (usbcode
== -1))
957 #ifdef UKBD_EMULATE_ATSCANCODE
958 keycode
= ukbd_trtab
[KEY_INDEX(usbcode
)];
962 scancode
= keycode2scancode(keycode
, state
->ks_ndata
.modifiers
,
963 usbcode
& KEY_RELEASE
);
964 if (scancode
& SCAN_PREFIX
) {
965 if (scancode
& SCAN_PREFIX_CTL
) {
966 state
->ks_buffered_char
[0] =
967 0x1d | (scancode
& SCAN_RELEASE
); /* Ctrl */
968 state
->ks_buffered_char
[1] = scancode
& ~SCAN_PREFIX
;
969 } else if (scancode
& SCAN_PREFIX_SHIFT
) {
970 state
->ks_buffered_char
[0] =
971 0x2a | (scancode
& SCAN_RELEASE
); /* Shift */
972 state
->ks_buffered_char
[1] =
973 scancode
& ~SCAN_PREFIX_SHIFT
;
975 state
->ks_buffered_char
[0] = scancode
& ~SCAN_PREFIX
;
976 state
->ks_buffered_char
[1] = 0;
978 return ((scancode
& SCAN_PREFIX_E0
) ? 0xe0 : 0xe1);
981 #else /* !UKBD_EMULATE_ATSCANCODE */
983 #endif /* UKBD_EMULATE_ATSCANCODE */
986 /* check if data is waiting */
988 ukbd_check(keyboard_t
*kbd
)
990 if (!KBD_IS_ACTIVE(kbd
) || !KBD_HAS_DEVICE(kbd
))
992 #ifdef UKBD_EMULATE_ATSCANCODE
993 if (((ukbd_state_t
*)kbd
->kb_data
)->ks_buffered_char
[0])
996 if (((ukbd_state_t
*)kbd
->kb_data
)->ks_inputs
> 0)
1001 /* read char from the keyboard */
1003 ukbd_read_char(keyboard_t
*kbd
, int wait
)
1005 ukbd_state_t
*state
;
1009 #ifdef UKBD_EMULATE_ATSCANCODE
1013 state
= (ukbd_state_t
*)kbd
->kb_data
;
1015 /* do we have a composed char to return? */
1016 if (!(state
->ks_flags
& COMPOSE
) && (state
->ks_composed_char
> 0)) {
1017 action
= state
->ks_composed_char
;
1018 state
->ks_composed_char
= 0;
1019 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
);
1050 #ifdef UKBD_EMULATE_ATSCANCODE
1051 /* USB key index -> key code -> AT scan code */
1052 keycode
= ukbd_trtab
[KEY_INDEX(usbcode
)];
1056 /* return an AT scan code for the K_RAW mode */
1057 if (state
->ks_mode
== K_RAW
) {
1058 scancode
= keycode2scancode(keycode
, state
->ks_ndata
.modifiers
,
1059 usbcode
& KEY_RELEASE
);
1060 if (scancode
& SCAN_PREFIX
) {
1061 if (scancode
& SCAN_PREFIX_CTL
) {
1062 state
->ks_buffered_char
[0] =
1063 0x1d | (scancode
& SCAN_RELEASE
);
1064 state
->ks_buffered_char
[1] =
1065 scancode
& ~SCAN_PREFIX
;
1066 } else if (scancode
& SCAN_PREFIX_SHIFT
) {
1067 state
->ks_buffered_char
[0] =
1068 0x2a | (scancode
& SCAN_RELEASE
);
1069 state
->ks_buffered_char
[1] =
1070 scancode
& ~SCAN_PREFIX_SHIFT
;
1072 state
->ks_buffered_char
[0] =
1073 scancode
& ~SCAN_PREFIX
;
1074 state
->ks_buffered_char
[1] = 0;
1076 return ((scancode
& SCAN_PREFIX_E0
) ? 0xe0 : 0xe1);
1080 #else /* !UKBD_EMULATE_ATSCANCODE */
1081 /* return the byte as is for the K_RAW mode */
1082 if (state
->ks_mode
== K_RAW
)
1085 /* USB key index -> key code */
1086 keycode
= ukbd_trtab
[KEY_INDEX(usbcode
)];
1089 #endif /* UKBD_EMULATE_ATSCANCODE */
1092 case 0x38: /* left alt (compose key) */
1093 if (usbcode
& KEY_RELEASE
) {
1094 if (state
->ks_flags
& COMPOSE
) {
1095 state
->ks_flags
&= ~COMPOSE
;
1096 if (state
->ks_composed_char
> UCHAR_MAX
)
1097 state
->ks_composed_char
= 0;
1100 if (!(state
->ks_flags
& COMPOSE
)) {
1101 state
->ks_flags
|= COMPOSE
;
1102 state
->ks_composed_char
= 0;
1106 /* XXX: I don't like these... */
1107 case 0x5c: /* print screen */
1108 if (state
->ks_flags
& ALTS
)
1109 keycode
= 0x54; /* sysrq */
1111 case 0x68: /* pause/break */
1112 if (state
->ks_flags
& CTLS
)
1113 keycode
= 0x6c; /* break */
1117 /* return the key code in the K_CODE mode */
1118 if (usbcode
& KEY_RELEASE
)
1119 keycode
|= SCAN_RELEASE
;
1120 if (state
->ks_mode
== K_CODE
)
1123 /* compose a character code */
1124 if (state
->ks_flags
& COMPOSE
) {
1126 /* key pressed, process it */
1127 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
1128 state
->ks_composed_char
*= 10;
1129 state
->ks_composed_char
+= keycode
- 0x40;
1130 if (state
->ks_composed_char
> UCHAR_MAX
)
1133 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
1134 state
->ks_composed_char
*= 10;
1135 state
->ks_composed_char
+= keycode
- 0x47;
1136 if (state
->ks_composed_char
> UCHAR_MAX
)
1139 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
1140 state
->ks_composed_char
*= 10;
1141 state
->ks_composed_char
+= keycode
- 0x4E;
1142 if (state
->ks_composed_char
> UCHAR_MAX
)
1145 case 0x52: /* keypad 0 */
1146 state
->ks_composed_char
*= 10;
1147 if (state
->ks_composed_char
> UCHAR_MAX
)
1151 /* key released, no interest here */
1152 case SCAN_RELEASE
| 0x47:
1153 case SCAN_RELEASE
| 0x48:
1154 case SCAN_RELEASE
| 0x49: /* keypad 7,8,9 */
1155 case SCAN_RELEASE
| 0x4B:
1156 case SCAN_RELEASE
| 0x4C:
1157 case SCAN_RELEASE
| 0x4D: /* keypad 4,5,6 */
1158 case SCAN_RELEASE
| 0x4F:
1159 case SCAN_RELEASE
| 0x50:
1160 case SCAN_RELEASE
| 0x51: /* keypad 1,2,3 */
1161 case SCAN_RELEASE
| 0x52: /* keypad 0 */
1164 case 0x38: /* left alt key */
1168 if (state
->ks_composed_char
> 0) {
1169 state
->ks_flags
&= ~COMPOSE
;
1170 state
->ks_composed_char
= 0;
1177 /* keycode to key action */
1178 action
= genkbd_keyaction(kbd
, SCAN_CHAR(keycode
),
1179 keycode
& SCAN_RELEASE
, &state
->ks_state
,
1180 &state
->ks_accents
);
1181 if (action
== NOKEY
)
1187 /* check if char is waiting */
1189 ukbd_check_char(keyboard_t
*kbd
)
1191 ukbd_state_t
*state
;
1193 if (!KBD_IS_ACTIVE(kbd
) || !KBD_HAS_DEVICE(kbd
))
1195 state
= (ukbd_state_t
*)kbd
->kb_data
;
1196 if (!(state
->ks_flags
& COMPOSE
) && (state
->ks_composed_char
> 0))
1198 return (ukbd_check(kbd
));
1201 /* some useful control functions */
1203 ukbd_ioctl(keyboard_t
*kbd
, u_long cmd
, caddr_t arg
)
1205 /* trasnlate LED_XXX bits into the device specific bits */
1206 static u_char ledmap
[8] = {
1207 0, 2, 1, 3, 4, 6, 5, 7,
1209 ukbd_state_t
*state
= kbd
->kb_data
;
1214 case KDGKBMODE
: /* get keyboard mode */
1215 *(int *)arg
= state
->ks_mode
;
1217 case KDSKBMODE
: /* set keyboard mode */
1218 switch (*(int *)arg
) {
1220 if (state
->ks_mode
!= K_XLATE
) {
1221 /* make lock key state and LED state match */
1222 state
->ks_state
&= ~LOCK_MASK
;
1223 state
->ks_state
|= KBD_LED_VAL(kbd
);
1228 if (state
->ks_mode
!= *(int *)arg
) {
1229 ukbd_clear_state(kbd
);
1230 state
->ks_mode
= *(int *)arg
;
1231 kbd
->kb_savemode
= state
->ks_mode
;
1240 case KDGETLED
: /* get keyboard LED */
1241 *(int *)arg
= KBD_LED_VAL(kbd
);
1243 case KDSETLED
: /* set keyboard LED */
1244 /* NOTE: lock key state in ks_state won't be changed */
1245 if (*(int *)arg
& ~LOCK_MASK
) {
1250 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1251 if (kbd
->kb_keymap
->n_keys
> ALTGR_OFFSET
) {
1257 if (KBD_HAS_DEVICE(kbd
)) {
1258 set_leds(state
, ledmap
[i
& LED_MASK
]);
1259 /* XXX: error check? */
1261 KBD_LED_VAL(kbd
) = *(int *)arg
;
1264 case KDGKBSTATE
: /* get lock key state */
1265 *(int *)arg
= state
->ks_state
& LOCK_MASK
;
1267 case KDSKBSTATE
: /* set lock key state */
1268 if (*(int *)arg
& ~LOCK_MASK
) {
1272 state
->ks_state
&= ~LOCK_MASK
;
1273 state
->ks_state
|= *(int *)arg
;
1275 /* set LEDs and quit */
1276 return ukbd_ioctl(kbd
, KDSETLED
, arg
);
1278 case KDSETREPEAT
: /* set keyboard repeat rate (new interface) */
1280 if (!KBD_HAS_DEVICE(kbd
))
1282 if (((int *)arg
)[1] < 0)
1284 if (((int *)arg
)[0] < 0)
1286 else if (((int *)arg
)[0] == 0) /* fastest possible value */
1287 kbd
->kb_delay1
= 200;
1289 kbd
->kb_delay1
= ((int *)arg
)[0];
1290 kbd
->kb_delay2
= ((int *)arg
)[1];
1293 case KDSETRAD
: /* set keyboard repeat rate (old interface) */
1295 return set_typematic(kbd
, *(int *)arg
);
1297 case PIO_KEYMAP
: /* set keyboard translation table */
1298 case PIO_KEYMAPENT
: /* set keyboard translation table entry */
1299 case PIO_DEADKEYMAP
: /* set accent key translation table */
1300 state
->ks_accents
= 0;
1304 return genkbd_commonioctl(kbd
, cmd
, arg
);
1308 ukbddebug
= *(int *)arg
;
1317 /* lock the access to the keyboard */
1319 ukbd_lock(keyboard_t
*kbd
, int lock
)
1325 /* clear the internal state of the keyboard */
1327 ukbd_clear_state(keyboard_t
*kbd
)
1329 ukbd_state_t
*state
;
1331 state
= (ukbd_state_t
*)kbd
->kb_data
;
1332 state
->ks_flags
= 0;
1333 state
->ks_polling
= 0;
1334 state
->ks_state
&= LOCK_MASK
; /* preserve locking key state */
1335 state
->ks_accents
= 0;
1336 state
->ks_composed_char
= 0;
1337 #ifdef UKBD_EMULATE_ATSCANCODE
1338 state
->ks_buffered_char
[0] = 0;
1339 state
->ks_buffered_char
[1] = 0;
1341 bzero(&state
->ks_ndata
, sizeof(state
->ks_ndata
));
1342 bzero(&state
->ks_odata
, sizeof(state
->ks_odata
));
1343 bzero(&state
->ks_ntime
, sizeof(state
->ks_ntime
));
1344 bzero(&state
->ks_otime
, sizeof(state
->ks_otime
));
1347 /* save the internal state */
1349 ukbd_get_state(keyboard_t
*kbd
, void *buf
, size_t len
)
1352 return sizeof(ukbd_state_t
);
1353 if (len
< sizeof(ukbd_state_t
))
1355 bcopy(kbd
->kb_data
, buf
, sizeof(ukbd_state_t
));
1359 /* set the internal state */
1361 ukbd_set_state(keyboard_t
*kbd
, void *buf
, size_t len
)
1363 if (len
< sizeof(ukbd_state_t
))
1365 bcopy(buf
, kbd
->kb_data
, sizeof(ukbd_state_t
));
1370 ukbd_poll(keyboard_t
*kbd
, int on
)
1372 ukbd_state_t
*state
;
1373 usbd_device_handle dev
;
1375 state
= (ukbd_state_t
*)kbd
->kb_data
;
1376 usbd_interface2device_handle(state
->ks_iface
, &dev
);
1380 if (state
->ks_polling
== 0)
1381 usbd_set_polling(dev
, on
);
1382 ++state
->ks_polling
;
1384 --state
->ks_polling
;
1385 if (state
->ks_polling
== 0)
1386 usbd_set_polling(dev
, on
);
1392 /* local functions */
1395 probe_keyboard(struct usb_attach_arg
*uaa
, int flags
)
1397 usb_interface_descriptor_t
*id
;
1399 if (!uaa
->iface
) /* we attach to ifaces only */
1402 /* Check that this is a keyboard that speaks the boot protocol. */
1403 id
= usbd_get_interface_descriptor(uaa
->iface
);
1405 && id
->bInterfaceClass
== UICLASS_HID
1406 && id
->bInterfaceSubClass
== UISUBCLASS_BOOT
1407 && id
->bInterfaceProtocol
== UPROTO_BOOT_KEYBOARD
)
1408 return 0; /* found it */
1414 init_keyboard(ukbd_state_t
*state
, int *type
, int flags
)
1416 usb_endpoint_descriptor_t
*ed
;
1421 state
->ks_ifstate
|= DISCONNECTED
;
1423 ed
= usbd_interface2endpoint_descriptor(state
->ks_iface
, 0);
1425 kprintf("ukbd: could not read endpoint descriptor\n");
1429 DPRINTFN(10,("ukbd:init_keyboard: \
1430 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
1431 ed
->bLength
, ed
->bDescriptorType
,
1432 UE_GET_ADDR(ed
->bEndpointAddress
),
1433 UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
? "in":"out",
1434 UE_GET_XFERTYPE(ed
->bmAttributes
),
1435 UGETW(ed
->wMaxPacketSize
), ed
->bInterval
));
1437 if (UE_GET_DIR(ed
->bEndpointAddress
) != UE_DIR_IN
||
1438 UE_GET_XFERTYPE(ed
->bmAttributes
) != UE_INTERRUPT
) {
1439 kprintf("ukbd: unexpected endpoint\n");
1443 if ((usbd_get_quirks(state
->ks_uaa
->device
)->uq_flags
& UQ_NO_SET_PROTO
) == 0) {
1444 err
= usbd_set_protocol(state
->ks_iface
, 0);
1445 DPRINTFN(5, ("ukbd:init_keyboard: protocol set\n"));
1447 kprintf("ukbd: set protocol failed\n");
1451 /* Ignore if SETIDLE fails since it is not crucial. */
1452 usbd_set_idle(state
->ks_iface
, 0, 0);
1454 state
->ks_ep_addr
= ed
->bEndpointAddress
;
1455 state
->ks_ifstate
&= ~DISCONNECTED
;
1461 set_leds(ukbd_state_t
*state
, int leds
)
1463 u_int8_t res
= leds
;
1465 DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state
, leds
));
1467 usbd_set_report_async(state
->ks_iface
, UHID_OUTPUT_REPORT
, 0, &res
, 1);
1471 set_typematic(keyboard_t
*kbd
, int code
)
1473 static int delays
[] = { 250, 500, 750, 1000 };
1474 static int rates
[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1475 68, 76, 84, 92, 100, 110, 118, 126,
1476 136, 152, 168, 184, 200, 220, 236, 252,
1477 272, 304, 336, 368, 400, 440, 472, 504 };
1481 kbd
->kb_delay1
= delays
[(code
>> 5) & 3];
1482 kbd
->kb_delay2
= rates
[code
& 0x1f];
1486 #ifdef UKBD_EMULATE_ATSCANCODE
1488 keycode2scancode(int keycode
, int shift
, int up
)
1490 static int scan
[] = {
1492 0x37 | SCAN_PREFIX_SHIFT
, /* PrintScreen */
1493 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1494 0x50, 0x51, 0x52, 0x53,
1495 0x46, /* XXX Pause/Break */
1501 if ((keycode
>= 89) && (keycode
< 89 + sizeof(scan
)/sizeof(scan
[0])))
1502 scancode
= scan
[keycode
- 89] | SCAN_PREFIX_E0
;
1504 if ((keycode
== 104) && !(shift
& (MOD_CONTROL_L
| MOD_CONTROL_R
)))
1505 scancode
= 0x45 | SCAN_PREFIX_E1
| SCAN_PREFIX_CTL
;
1506 if (shift
& (MOD_SHIFT_L
| MOD_SHIFT_R
))
1507 scancode
&= ~SCAN_PREFIX_SHIFT
;
1508 return (scancode
| (up
? SCAN_RELEASE
: SCAN_PRESS
));
1510 #endif /* UKBD_EMULATE_ATSCANCODE */
1513 ukbd_driver_load(module_t mod
, int what
, void *arg
)
1517 kbd_add_driver(&ukbd_kbd_driver
);
1520 kbd_delete_driver(&ukbd_kbd_driver
);
1523 return usbd_driver_load(mod
, what
, 0);