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.21 2007/06/28 13:55:13 hasso Exp $
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/ioccom.h>
54 #include <sys/module.h>
57 #include <machine/limits.h>
58 #include <sys/select.h>
59 #include <sys/vnode.h>
60 #include <sys/sysctl.h>
61 #include <sys/thread2.h>
63 #include <bus/usb/usb.h>
64 #include <bus/usb/usbhid.h>
65 #include <bus/usb/usbdi.h>
66 #include <bus/usb/usbdi_util.h>
67 #include <bus/usb/usbdevs.h>
68 #include <bus/usb/usb_quirks.h>
69 #include <bus/usb/hid.h>
72 #include <dev/misc/kbd/kbdreg.h>
74 #define UKBD_EMULATE_ATSCANCODE 1
76 #define DRIVER_NAME "ukbd"
78 #define delay(d) DELAY(d)
81 #define DPRINTF(x) if (ukbddebug) kprintf x
82 #define DPRINTFN(n,x) if (ukbddebug>(n)) kprintf x
84 SYSCTL_NODE(_hw_usb
, OID_AUTO
, ukbd
, CTLFLAG_RW
, 0, "USB ukbd");
85 SYSCTL_INT(_hw_usb_ukbd
, OID_AUTO
, debug
, CTLFLAG_RW
,
86 &ukbddebug
, 0, "ukbd debug level");
92 #define UPROTO_BOOT_KEYBOARD 1
98 #define MOD_CONTROL_L 0x01
99 #define MOD_CONTROL_R 0x10
100 #define MOD_SHIFT_L 0x02
101 #define MOD_SHIFT_R 0x20
102 #define MOD_ALT_L 0x04
103 #define MOD_ALT_R 0x40
104 #define MOD_WIN_L 0x08
105 #define MOD_WIN_R 0x80
107 u_int8_t keycode
[NKEYCODE
];
110 #define MAXKEYS (NMOD+2*NKEYCODE)
112 typedef struct ukbd_softc
{
113 device_t sc_dev
; /* base device */
116 #define UKBD_CHUNK 128 /* chunk size for read */
117 #define UKBD_BSIZE 1020 /* buffer size */
119 typedef void usbd_intr_t(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
120 typedef void usbd_disco_t(void *);
122 static int ukbd_resume(device_t self
);
123 static usbd_intr_t ukbd_intr
;
124 static int ukbd_driver_load(module_t mod
, int what
, void *arg
);
125 static int ukbd_default_term(keyboard_t
*kbd
);
127 static keyboard_t default_kbd
;
129 USB_DECLARE_DRIVER_INIT(ukbd
, DEVMETHOD(device_resume
, ukbd_resume
));
133 USB_MATCH_START(ukbd
, uaa
);
135 keyboard_switch_t
*sw
;
137 int unit
= device_get_unit(self
);
139 sw
= kbd_get_switch(DRIVER_NAME
);
141 return (UMATCH_NONE
);
143 arg
[0] = (void *)uaa
;
144 arg
[1] = (void *)ukbd_intr
;
145 if ((*sw
->probe
)(unit
, (void *)arg
, 0))
146 return (UMATCH_NONE
);
148 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO
);
153 USB_ATTACH_START(ukbd
, sc
, uaa
);
154 usbd_interface_handle iface
= uaa
->iface
;
155 usb_interface_descriptor_t
*id
;
158 keyboard_switch_t
*sw
;
161 int unit
= device_get_unit(self
);
163 sw
= kbd_get_switch(DRIVER_NAME
);
165 USB_ATTACH_ERROR_RETURN
;
167 id
= usbd_get_interface_descriptor(iface
);
168 usbd_devinfo(uaa
->device
, 0, devinfo
);
170 kprintf("%s: %s, iclass %d/%d\n", device_get_nameunit(sc
->sc_dev
),
171 devinfo
, id
->bInterfaceClass
, id
->bInterfaceSubClass
);
173 arg
[0] = (void *)uaa
;
174 arg
[1] = (void *)ukbd_intr
;
176 if ((*sw
->probe
)(unit
, (void *)arg
, 0))
177 USB_ATTACH_ERROR_RETURN
;
178 if ((*sw
->init
)(unit
, &kbd
, (void *)arg
, 0))
179 USB_ATTACH_ERROR_RETURN
;
182 #ifdef KBD_INSTALL_CDEV
184 USB_ATTACH_ERROR_RETURN
;
187 (*sw
->diag
)(kbd
, bootverbose
);
189 USB_ATTACH_SUCCESS_RETURN
;
193 ukbd_detach(device_t self
)
198 kbd
= kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME
,
199 device_get_unit(self
)));
201 DPRINTF(("%s: keyboard not attached!?\n", device_get_nameunit(self
)));
204 (*kbdsw
[kbd
->kb_index
]->disable
)(kbd
);
206 #ifdef KBD_INSTALL_CDEV
207 if (kbd
!= &default_kbd
) {
208 error
= kbd_detach(kbd
);
213 if (kbd
== &default_kbd
) {
214 ukbd_default_term(kbd
);
216 error
= (*kbdsw
[kbd
->kb_index
]->term
)(kbd
);
221 DPRINTF(("%s: disconnected\n", device_get_nameunit(self
)));
227 ukbd_resume(device_t self
)
231 kbd
= kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME
,
232 device_get_unit(self
)));
234 (*kbdsw
[kbd
->kb_index
]->clear_state
)(kbd
);
239 ukbd_intr(usbd_xfer_handle xfer
, usbd_private_handle addr
, usbd_status status
)
241 keyboard_t
*kbd
= (keyboard_t
*)addr
;
243 (*kbdsw
[kbd
->kb_index
]->intr
)(kbd
, (void *)status
);
246 DRIVER_MODULE(ukbd
, uhub
, ukbd_driver
, ukbd_devclass
, ukbd_driver_load
, 0);
249 #define UKBD_DEFAULT 0
251 #define KEY_ERROR 0x01
254 #define KEY_RELEASE 0x400
255 #define KEY_INDEX(c) ((c) & ~KEY_RELEASE)
258 #define SCAN_RELEASE 0x80
259 #define SCAN_PREFIX_E0 0x100
260 #define SCAN_PREFIX_E1 0x200
261 #define SCAN_PREFIX_CTL 0x400
262 #define SCAN_PREFIX_SHIFT 0x800
263 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL \
265 #define SCAN_CHAR(c) ((c) & 0x7f)
270 } ukbd_mods
[NMOD
] = {
271 { MOD_CONTROL_L
, 0xe0 },
272 { MOD_CONTROL_R
, 0xe4 },
273 { MOD_SHIFT_L
, 0xe1 },
274 { MOD_SHIFT_R
, 0xe5 },
281 #define NN 0 /* no translation */
283 * Translate USB keycodes to AT keyboard scancodes.
286 * FIXME: Mac USB keyboard generates:
287 * 0x53: keypad NumLock/Clear
294 static u_int8_t ukbd_trtab
[256] = {
295 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
296 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
297 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
298 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
299 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
300 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
301 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
302 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
303 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
304 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
305 97, 100, 95, 69, 91, 55, 74, 78, /* 50 - 57 */
306 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
307 72, 73, 82, 83, 86, 107, NN
, NN
, /* 60 - 67 */
308 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* 68 - 6F */
309 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* 70 - 77 */
310 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* 78 - 7F */
311 NN
, NN
, NN
, NN
, NN
, NN
, NN
, 115, /* 80 - 87 */
312 112, 125, 121, 123, NN
, NN
, NN
, NN
, /* 88 - 8F */
313 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* 90 - 97 */
314 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* 98 - 9F */
315 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* A0 - A7 */
316 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* A8 - AF */
317 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* B0 - B7 */
318 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* B8 - BF */
319 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* C0 - C7 */
320 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* C8 - CF */
321 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* D0 - D7 */
322 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* D8 - DF */
323 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
324 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* E8 - EF */
325 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* F0 - F7 */
326 NN
, NN
, NN
, NN
, NN
, NN
, NN
, NN
, /* F8 - FF */
329 typedef struct ukbd_state
{
330 usbd_interface_handle ks_iface
; /* interface */
331 usbd_pipe_handle ks_intrpipe
; /* interrupt pipe */
332 struct usb_attach_arg
*ks_uaa
;
335 struct ukbd_data ks_ndata
;
336 struct ukbd_data ks_odata
;
337 u_long ks_ntime
[NKEYCODE
];
338 u_long ks_otime
[NKEYCODE
];
340 #define INPUTBUFSIZE (NMOD + 2*NKEYCODE)
341 u_int ks_input
[INPUTBUFSIZE
]; /* input buffer */
347 #define INTRENABLED (1 << 0)
348 #define DISCONNECTED (1 << 1)
350 struct callout ks_timeout
;
352 int ks_mode
; /* input mode (K_XLATE,K_RAW,K_CODE) */
353 int ks_flags
; /* flags */
354 #define COMPOSE (1 << 0)
356 int ks_state
; /* shift/lock key state */
357 int ks_accents
; /* accent key index (> 0) */
358 u_int ks_composed_char
; /* composed char code (> 0) */
359 #ifdef UKBD_EMULATE_ATSCANCODE
360 u_int ks_buffered_char
[2];
364 /* keyboard driver declaration */
365 static int ukbd_configure(int flags
);
366 static kbd_probe_t ukbd_probe
;
367 static kbd_init_t ukbd_init
;
368 static kbd_term_t ukbd_term
;
369 static kbd_intr_t ukbd_interrupt
;
370 static kbd_test_if_t ukbd_test_if
;
371 static kbd_enable_t ukbd_enable
;
372 static kbd_disable_t ukbd_disable
;
373 static kbd_read_t ukbd_read
;
374 static kbd_check_t ukbd_check
;
375 static kbd_read_char_t ukbd_read_char
;
376 static kbd_check_char_t ukbd_check_char
;
377 static kbd_ioctl_t ukbd_ioctl
;
378 static kbd_lock_t ukbd_lock
;
379 static kbd_clear_state_t ukbd_clear_state
;
380 static kbd_get_state_t ukbd_get_state
;
381 static kbd_set_state_t ukbd_set_state
;
382 static kbd_poll_mode_t ukbd_poll
;
384 keyboard_switch_t ukbdsw
= {
406 KEYBOARD_DRIVER(ukbd
, ukbdsw
, ukbd_configure
);
408 /* local functions */
409 static int ukbd_enable_intr(keyboard_t
*kbd
, int on
,
411 static timeout_t ukbd_timeout
;
413 static int ukbd_getc(ukbd_state_t
*state
);
414 static int probe_keyboard(struct usb_attach_arg
*uaa
, int flags
);
415 static int init_keyboard(ukbd_state_t
*state
, int *type
,
417 static void set_leds(ukbd_state_t
*state
, int leds
);
418 static int set_typematic(keyboard_t
*kbd
, int code
);
419 #ifdef UKBD_EMULATE_ATSCANCODE
420 static int keycode2scancode(int keycode
, int shift
, int up
);
423 /* local variables */
425 /* the initial key map, accent map and fkey strings */
426 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
427 #define KBD_DFLT_KEYMAP
430 #include <dev/misc/kbd/kbdtables.h>
432 /* structures for the default keyboard */
433 static ukbd_state_t default_kbd_state
;
434 static keymap_t default_keymap
;
435 static accentmap_t default_accentmap
;
436 static fkeytab_t default_fkeytab
[NUM_FKEYS
];
439 * The back door to the keyboard driver!
440 * This function is called by the console driver, via the kbdio module,
441 * to tickle keyboard drivers when the low-level console is being initialized.
442 * Almost nothing in the kernel has been initialied yet. Try to probe
443 * keyboards if possible.
444 * NOTE: because of the way the low-level conole is initialized, this routine
445 * may be called more than once!!
448 ukbd_configure(int flags
)
455 struct usb_attach_arg
*uaa
;
458 device
= devclass_get_device(ukbd_devclass
, UKBD_DEFAULT
);
461 uaa
= (struct usb_attach_arg
*)device_get_ivars(device
);
465 /* probe the default keyboard */
466 arg
[0] = (void *)uaa
;
467 arg
[1] = (void *)ukbd_intr
;
469 if (ukbd_probe(UKBD_DEFAULT
, arg
, flags
))
471 if (ukbd_init(UKBD_DEFAULT
, &kbd
, arg
, flags
))
474 /* return the number of found keyboards */
479 /* low-level functions */
481 /* detect a keyboard */
483 ukbd_probe(int unit
, void *arg
, int flags
)
486 struct usb_attach_arg
*uaa
;
489 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
))
501 * Reset and initialize the device. Note that unit 0 (UKBD_DEFAULT) is an
502 * always-connected device once it has been initially detected. We do not
503 * deregister it if the usb keyboard is unplugged to avoid losing the
504 * connection to the console. This feature also handles the USB bus reset
505 * which detaches and reattaches USB devices during boot.
508 ukbd_init(int unit
, keyboard_t
**kbdp
, void *arg
, int flags
)
516 void **data
= (void **)arg
;
517 struct usb_attach_arg
*uaa
= (struct usb_attach_arg
*)data
[0];
519 if (unit
== UKBD_DEFAULT
) {
520 *kbdp
= kbd
= &default_kbd
;
521 if (KBD_IS_INITIALIZED(kbd
) && KBD_IS_CONFIGURED(kbd
))
523 state
= &default_kbd_state
;
524 keymap
= &default_keymap
;
525 accmap
= &default_accentmap
;
526 fkeymap
= default_fkeytab
;
528 sizeof(default_fkeytab
)/sizeof(default_fkeytab
[0]);
529 } else if (*kbdp
== NULL
) {
530 *kbdp
= kbd
= kmalloc(sizeof(*kbd
), M_DEVBUF
, M_INTWAIT
| M_ZERO
);
531 state
= kmalloc(sizeof(*state
), M_DEVBUF
, M_INTWAIT
);
532 keymap
= kmalloc(sizeof(key_map
), M_DEVBUF
, M_INTWAIT
);
533 accmap
= kmalloc(sizeof(accent_map
), M_DEVBUF
, M_INTWAIT
);
534 fkeymap
= kmalloc(sizeof(fkey_tab
), M_DEVBUF
, M_INTWAIT
);
535 fkeymap_size
= sizeof(fkey_tab
)/sizeof(fkey_tab
[0]);
536 if ((state
== NULL
) || (keymap
== NULL
) || (accmap
== NULL
)
537 || (fkeymap
== NULL
)) {
539 kfree(state
, M_DEVBUF
);
541 kfree(keymap
, M_DEVBUF
);
543 kfree(accmap
, M_DEVBUF
);
545 kfree(fkeymap
, M_DEVBUF
);
546 kfree(kbd
, M_DEVBUF
);
549 } else if (KBD_IS_INITIALIZED(*kbdp
) && KBD_IS_CONFIGURED(*kbdp
)) {
553 state
= (ukbd_state_t
*)kbd
->kb_data
;
554 keymap
= kbd
->kb_keymap
;
555 accmap
= kbd
->kb_accentmap
;
556 fkeymap
= kbd
->kb_fkeytab
;
557 fkeymap_size
= kbd
->kb_fkeytab_size
;
560 if (!KBD_IS_PROBED(kbd
)) {
561 if (KBD_IS_CONFIGURED(kbd
)) {
562 kbd_reinit_struct(kbd
, flags
, KB_PRI_USB
);
564 kbd_init_struct(kbd
, DRIVER_NAME
, KB_OTHER
,
565 unit
, flags
, KB_PRI_USB
,
568 bzero(state
, sizeof(*state
));
569 bcopy(&key_map
, keymap
, sizeof(key_map
));
570 bcopy(&accent_map
, accmap
, sizeof(accent_map
));
571 bcopy(fkey_tab
, fkeymap
,
572 imin(fkeymap_size
*sizeof(fkeymap
[0]), sizeof(fkey_tab
)));
573 kbd_set_maps(kbd
, keymap
, accmap
, fkeymap
, fkeymap_size
);
574 kbd
->kb_data
= (void *)state
;
576 if (probe_keyboard(uaa
, flags
))
579 KBD_FOUND_DEVICE(kbd
);
580 ukbd_clear_state(kbd
);
583 * If reattatching to an already open keyboard (e.g. console),
584 * try to restore the translation mode. Otherwise set the
585 * translation mode to, well, translation mode so we don't
588 if (!KBD_IS_CONFIGURED(kbd
)) {
589 state
->ks_mode
= K_XLATE
;
590 kbd
->kb_savemode
= state
->ks_mode
;
592 state
->ks_mode
= kbd
->kb_savemode
;
594 state
->ks_iface
= uaa
->iface
;
596 state
->ks_ifstate
= 0;
597 callout_init(&state
->ks_timeout
);
599 * FIXME: set the initial value for lock keys in ks_state
600 * according to the BIOS data?
604 if (!KBD_IS_INITIALIZED(kbd
) && !(flags
& KB_CONF_PROBE_ONLY
)) {
605 if (KBD_HAS_DEVICE(kbd
)
606 && init_keyboard((ukbd_state_t
*)kbd
->kb_data
,
607 &kbd
->kb_type
, kbd
->kb_flags
))
609 ukbd_ioctl(kbd
, KDSETLED
, (caddr_t
)&(state
->ks_state
));
611 if (!KBD_IS_CONFIGURED(kbd
)) {
612 if (kbd_register(kbd
) < 0)
614 KBD_CONFIG_DONE(kbd
);
616 if (!KBD_IS_INITIALIZED(kbd
) && !(flags
& KB_CONF_PROBE_ONLY
)) {
617 if (ukbd_enable_intr(kbd
, TRUE
, (usbd_intr_t
*)data
[1]) == 0)
618 ukbd_timeout((void *)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
)
663 state
= (ukbd_state_t
*)kbd
->kb_data
;
664 DPRINTF(("ukbd_term: ks_ifstate=0x%x\n", state
->ks_ifstate
));
666 callout_stop(&state
->ks_timeout
);
668 if (state
->ks_ifstate
& INTRENABLED
)
669 ukbd_enable_intr(kbd
, FALSE
, NULL
);
670 if (state
->ks_ifstate
& INTRENABLED
) {
672 DPRINTF(("ukbd_term: INTRENABLED!\n"));
676 error
= kbd_unregister(kbd
);
677 DPRINTF(("ukbd_term: kbd_unregister() %d\n", error
));
680 if (kbd
!= &default_kbd
) {
681 kfree(kbd
->kb_keymap
, M_DEVBUF
);
682 kfree(kbd
->kb_accentmap
, M_DEVBUF
);
683 kfree(kbd
->kb_fkeytab
, M_DEVBUF
);
684 kfree(state
, M_DEVBUF
);
685 kfree(kbd
, M_DEVBUF
);
693 * Finish using the default keyboard. Shutdown the USB side of the keyboard
694 * but do not unregister it.
697 ukbd_default_term(keyboard_t
*kbd
)
703 state
= (ukbd_state_t
*)kbd
->kb_data
;
704 DPRINTF(("ukbd_default_term: ks_ifstate=0x%x\n", state
->ks_ifstate
));
706 callout_stop(&state
->ks_timeout
);
708 if (state
->ks_ifstate
& INTRENABLED
)
709 ukbd_enable_intr(kbd
, FALSE
, NULL
);
710 if (state
->ks_ifstate
& INTRENABLED
) {
712 DPRINTF(("ukbd_term: INTRENABLED!\n"));
715 KBD_LOST_DEVICE(kbd
);
722 /* keyboard interrupt routine */
725 ukbd_timeout(void *arg
)
730 kbd
= (keyboard_t
*)arg
;
731 state
= (ukbd_state_t
*)kbd
->kb_data
;
733 (*kbdsw
[kbd
->kb_index
]->intr
)(kbd
, (void *)USBD_NORMAL_COMPLETION
);
734 callout_reset(&state
->ks_timeout
, hz
/ 40, ukbd_timeout
, arg
);
739 ukbd_interrupt(keyboard_t
*kbd
, void *arg
)
741 usbd_status status
= (usbd_status
)arg
;
743 struct ukbd_data
*ud
;
750 DPRINTFN(5, ("ukbd_intr: status=%d\n", status
));
751 if (status
== USBD_CANCELLED
)
754 state
= (ukbd_state_t
*)kbd
->kb_data
;
755 ud
= &state
->ks_ndata
;
757 if (status
!= USBD_NORMAL_COMPLETION
) {
758 DPRINTF(("ukbd_intr: status=%d\n", status
));
759 if (status
== USBD_STALLED
)
760 usbd_clear_endpoint_stall_async(state
->ks_intrpipe
);
764 if (ud
->keycode
[0] == KEY_ERROR
)
765 return 0; /* ignore */
768 now
= (u_long
)tv
.tv_sec
*1000 + (u_long
)tv
.tv_usec
/1000;
771 if (state->ks_inputs < INPUTBUFSIZE) { \
772 state->ks_input[state->ks_inputtail] = (c); \
773 ++state->ks_inputs; \
774 state->ks_inputtail = (state->ks_inputtail + 1)%INPUTBUFSIZE; \
778 omod
= state
->ks_odata
.modifiers
;
780 for (i
= 0; i
< NMOD
; i
++)
781 if (( mod
& ukbd_mods
[i
].mask
) !=
782 (omod
& ukbd_mods
[i
].mask
))
783 ADDKEY1(ukbd_mods
[i
].key
|
784 (mod
& ukbd_mods
[i
].mask
785 ? KEY_PRESS
: KEY_RELEASE
));
788 /* Check for released keys. */
789 for (i
= 0; i
< NKEYCODE
; i
++) {
790 key
= state
->ks_odata
.keycode
[i
];
793 for (j
= 0; j
< NKEYCODE
; j
++) {
794 if (ud
->keycode
[j
] == 0)
796 if (key
== ud
->keycode
[j
])
799 ADDKEY1(key
| KEY_RELEASE
);
804 /* Check for pressed keys. */
805 for (i
= 0; i
< NKEYCODE
; i
++) {
806 key
= ud
->keycode
[i
];
809 state
->ks_ntime
[i
] = now
+ kbd
->kb_delay1
;
810 for (j
= 0; j
< NKEYCODE
; j
++) {
811 if (state
->ks_odata
.keycode
[j
] == 0)
813 if (key
== state
->ks_odata
.keycode
[j
]) {
814 state
->ks_ntime
[i
] = state
->ks_otime
[j
];
815 if (state
->ks_otime
[j
] > now
)
817 state
->ks_ntime
[i
] = now
+ kbd
->kb_delay2
;
821 ADDKEY1(key
| KEY_PRESS
);
826 state
->ks_odata
= *ud
;
827 bcopy(state
->ks_ntime
, state
->ks_otime
, sizeof(state
->ks_ntime
));
828 if (state
->ks_inputs
<= 0)
832 for (i
= state
->ks_inputhead
, j
= 0; j
< state
->ks_inputs
; ++j
,
833 i
= (i
+ 1)%INPUTBUFSIZE
) {
834 c
= state
->ks_input
[i
];
835 DPRINTF(("0x%x (%d) %s\n", c
, c
,
836 (c
& KEY_RELEASE
) ? "released":"pressed"));
839 DPRINTF(("mod:0x%04x ", ud
->modifiers
));
840 for (i
= 0; i
< NKEYCODE
; i
++) {
842 DPRINTF(("%d ", ud
->keycode
[i
]));
845 #endif /* USB_DEBUG */
847 if (state
->ks_polling
)
850 if (KBD_IS_ACTIVE(kbd
) && KBD_IS_BUSY(kbd
)) {
851 /* let the callback function to process the input */
852 (*kbd
->kb_callback
.kc_func
)(kbd
, KBDIO_KEYINPUT
,
853 kbd
->kb_callback
.kc_arg
);
855 /* read and discard the input; no one is waiting for it */
857 c
= ukbd_read_char(kbd
, FALSE
);
858 } while (c
!= NOKEY
);
865 ukbd_getc(ukbd_state_t
*state
)
869 if (state
->ks_polling
) {
870 DPRINTFN(1,("ukbd_getc: polling\n"));
872 while (state
->ks_inputs
<= 0)
873 usbd_dopoll(state
->ks_iface
);
877 if (state
->ks_inputs
<= 0) {
880 c
= state
->ks_input
[state
->ks_inputhead
];
882 state
->ks_inputhead
= (state
->ks_inputhead
+ 1)%INPUTBUFSIZE
;
888 /* test the interface to the device */
890 ukbd_test_if(keyboard_t
*kbd
)
896 * Enable the access to the device; until this function is called,
897 * the client cannot read from the keyboard.
900 ukbd_enable(keyboard_t
*kbd
)
908 /* disallow the access to the device */
910 ukbd_disable(keyboard_t
*kbd
)
918 /* read one byte from the keyboard if it's allowed */
920 ukbd_read(keyboard_t
*kbd
, int wait
)
924 #ifdef UKBD_EMULATE_ATSCANCODE
929 state
= (ukbd_state_t
*)kbd
->kb_data
;
930 #ifdef UKBD_EMULATE_ATSCANCODE
931 if (state
->ks_buffered_char
[0]) {
932 scancode
= state
->ks_buffered_char
[0];
933 if (scancode
& SCAN_PREFIX
) {
934 state
->ks_buffered_char
[0] = scancode
& ~SCAN_PREFIX
;
935 return ((scancode
& SCAN_PREFIX_E0
) ? 0xe0 : 0xe1);
937 state
->ks_buffered_char
[0] = state
->ks_buffered_char
[1];
938 state
->ks_buffered_char
[1] = 0;
942 #endif /* UKBD_EMULATE_ATSCANCODE */
945 usbcode
= ukbd_getc(state
);
946 if (!KBD_IS_ACTIVE(kbd
) || !KBD_HAS_DEVICE(kbd
) || (usbcode
== -1))
949 #ifdef UKBD_EMULATE_ATSCANCODE
950 keycode
= ukbd_trtab
[KEY_INDEX(usbcode
)];
954 scancode
= keycode2scancode(keycode
, state
->ks_ndata
.modifiers
,
955 usbcode
& KEY_RELEASE
);
956 if (scancode
& SCAN_PREFIX
) {
957 if (scancode
& SCAN_PREFIX_CTL
) {
958 state
->ks_buffered_char
[0] =
959 0x1d | (scancode
& SCAN_RELEASE
); /* Ctrl */
960 state
->ks_buffered_char
[1] = scancode
& ~SCAN_PREFIX
;
961 } else if (scancode
& SCAN_PREFIX_SHIFT
) {
962 state
->ks_buffered_char
[0] =
963 0x2a | (scancode
& SCAN_RELEASE
); /* Shift */
964 state
->ks_buffered_char
[1] =
965 scancode
& ~SCAN_PREFIX_SHIFT
;
967 state
->ks_buffered_char
[0] = scancode
& ~SCAN_PREFIX
;
968 state
->ks_buffered_char
[1] = 0;
970 return ((scancode
& SCAN_PREFIX_E0
) ? 0xe0 : 0xe1);
973 #else /* !UKBD_EMULATE_ATSCANCODE */
975 #endif /* UKBD_EMULATE_ATSCANCODE */
978 /* check if data is waiting */
980 ukbd_check(keyboard_t
*kbd
)
982 if (!KBD_IS_ACTIVE(kbd
) || !KBD_HAS_DEVICE(kbd
))
984 #ifdef UKBD_EMULATE_ATSCANCODE
985 if (((ukbd_state_t
*)kbd
->kb_data
)->ks_buffered_char
[0])
988 if (((ukbd_state_t
*)kbd
->kb_data
)->ks_inputs
> 0)
993 /* read char from the keyboard */
995 ukbd_read_char(keyboard_t
*kbd
, int wait
)
1001 #ifdef UKBD_EMULATE_ATSCANCODE
1005 state
= (ukbd_state_t
*)kbd
->kb_data
;
1007 /* do we have a composed char to return? */
1008 if (!(state
->ks_flags
& COMPOSE
) && (state
->ks_composed_char
> 0)) {
1009 action
= state
->ks_composed_char
;
1010 state
->ks_composed_char
= 0;
1011 if (action
> UCHAR_MAX
)
1016 #ifdef UKBD_EMULATE_ATSCANCODE
1017 /* do we have a pending raw scan code? */
1018 if (state
->ks_mode
== K_RAW
) {
1019 if (state
->ks_buffered_char
[0]) {
1020 scancode
= state
->ks_buffered_char
[0];
1021 if (scancode
& SCAN_PREFIX
) {
1022 state
->ks_buffered_char
[0] =
1023 scancode
& ~SCAN_PREFIX
;
1024 return ((scancode
& SCAN_PREFIX_E0
) ? 0xe0 : 0xe1);
1026 state
->ks_buffered_char
[0] =
1027 state
->ks_buffered_char
[1];
1028 state
->ks_buffered_char
[1] = 0;
1033 #endif /* UKBD_EMULATE_ATSCANCODE */
1035 /* see if there is something in the keyboard port */
1037 usbcode
= ukbd_getc(state
);
1042 #ifdef UKBD_EMULATE_ATSCANCODE
1043 /* USB key index -> key code -> AT scan code */
1044 keycode
= ukbd_trtab
[KEY_INDEX(usbcode
)];
1048 /* return an AT scan code for the K_RAW mode */
1049 if (state
->ks_mode
== K_RAW
) {
1050 scancode
= keycode2scancode(keycode
, state
->ks_ndata
.modifiers
,
1051 usbcode
& KEY_RELEASE
);
1052 if (scancode
& SCAN_PREFIX
) {
1053 if (scancode
& SCAN_PREFIX_CTL
) {
1054 state
->ks_buffered_char
[0] =
1055 0x1d | (scancode
& SCAN_RELEASE
);
1056 state
->ks_buffered_char
[1] =
1057 scancode
& ~SCAN_PREFIX
;
1058 } else if (scancode
& SCAN_PREFIX_SHIFT
) {
1059 state
->ks_buffered_char
[0] =
1060 0x2a | (scancode
& SCAN_RELEASE
);
1061 state
->ks_buffered_char
[1] =
1062 scancode
& ~SCAN_PREFIX_SHIFT
;
1064 state
->ks_buffered_char
[0] =
1065 scancode
& ~SCAN_PREFIX
;
1066 state
->ks_buffered_char
[1] = 0;
1068 return ((scancode
& SCAN_PREFIX_E0
) ? 0xe0 : 0xe1);
1072 #else /* !UKBD_EMULATE_ATSCANCODE */
1073 /* return the byte as is for the K_RAW mode */
1074 if (state
->ks_mode
== K_RAW
)
1077 /* USB key index -> key code */
1078 keycode
= ukbd_trtab
[KEY_INDEX(usbcode
)];
1081 #endif /* UKBD_EMULATE_ATSCANCODE */
1084 case 0x38: /* left alt (compose key) */
1085 if (usbcode
& KEY_RELEASE
) {
1086 if (state
->ks_flags
& COMPOSE
) {
1087 state
->ks_flags
&= ~COMPOSE
;
1088 if (state
->ks_composed_char
> UCHAR_MAX
)
1089 state
->ks_composed_char
= 0;
1092 if (!(state
->ks_flags
& COMPOSE
)) {
1093 state
->ks_flags
|= COMPOSE
;
1094 state
->ks_composed_char
= 0;
1098 /* XXX: I don't like these... */
1099 case 0x5c: /* print screen */
1100 if (state
->ks_flags
& ALTS
)
1101 keycode
= 0x54; /* sysrq */
1103 case 0x68: /* pause/break */
1104 if (state
->ks_flags
& CTLS
)
1105 keycode
= 0x6c; /* break */
1109 /* return the key code in the K_CODE mode */
1110 if (usbcode
& KEY_RELEASE
)
1111 keycode
|= SCAN_RELEASE
;
1112 if (state
->ks_mode
== K_CODE
)
1115 /* compose a character code */
1116 if (state
->ks_flags
& COMPOSE
) {
1118 /* key pressed, process it */
1119 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
1120 state
->ks_composed_char
*= 10;
1121 state
->ks_composed_char
+= keycode
- 0x40;
1122 if (state
->ks_composed_char
> UCHAR_MAX
)
1125 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
1126 state
->ks_composed_char
*= 10;
1127 state
->ks_composed_char
+= keycode
- 0x47;
1128 if (state
->ks_composed_char
> UCHAR_MAX
)
1131 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
1132 state
->ks_composed_char
*= 10;
1133 state
->ks_composed_char
+= keycode
- 0x4E;
1134 if (state
->ks_composed_char
> UCHAR_MAX
)
1137 case 0x52: /* keypad 0 */
1138 state
->ks_composed_char
*= 10;
1139 if (state
->ks_composed_char
> UCHAR_MAX
)
1143 /* key released, no interest here */
1144 case SCAN_RELEASE
| 0x47:
1145 case SCAN_RELEASE
| 0x48:
1146 case SCAN_RELEASE
| 0x49: /* keypad 7,8,9 */
1147 case SCAN_RELEASE
| 0x4B:
1148 case SCAN_RELEASE
| 0x4C:
1149 case SCAN_RELEASE
| 0x4D: /* keypad 4,5,6 */
1150 case SCAN_RELEASE
| 0x4F:
1151 case SCAN_RELEASE
| 0x50:
1152 case SCAN_RELEASE
| 0x51: /* keypad 1,2,3 */
1153 case SCAN_RELEASE
| 0x52: /* keypad 0 */
1156 case 0x38: /* left alt key */
1160 if (state
->ks_composed_char
> 0) {
1161 state
->ks_flags
&= ~COMPOSE
;
1162 state
->ks_composed_char
= 0;
1169 /* keycode to key action */
1170 action
= genkbd_keyaction(kbd
, SCAN_CHAR(keycode
),
1171 keycode
& SCAN_RELEASE
, &state
->ks_state
,
1172 &state
->ks_accents
);
1173 if (action
== NOKEY
)
1179 /* check if char is waiting */
1181 ukbd_check_char(keyboard_t
*kbd
)
1183 ukbd_state_t
*state
;
1185 if (!KBD_IS_ACTIVE(kbd
) || !KBD_HAS_DEVICE(kbd
))
1187 state
= (ukbd_state_t
*)kbd
->kb_data
;
1188 if (!(state
->ks_flags
& COMPOSE
) && (state
->ks_composed_char
> 0))
1190 return (ukbd_check(kbd
));
1193 /* some useful control functions */
1195 ukbd_ioctl(keyboard_t
*kbd
, u_long cmd
, caddr_t arg
)
1197 /* trasnlate LED_XXX bits into the device specific bits */
1198 static u_char ledmap
[8] = {
1199 0, 2, 1, 3, 4, 6, 5, 7,
1201 ukbd_state_t
*state
= kbd
->kb_data
;
1206 case KDGKBMODE
: /* get keyboard mode */
1207 *(int *)arg
= state
->ks_mode
;
1209 case KDSKBMODE
: /* set keyboard mode */
1210 switch (*(int *)arg
) {
1212 if (state
->ks_mode
!= K_XLATE
) {
1213 /* make lock key state and LED state match */
1214 state
->ks_state
&= ~LOCK_MASK
;
1215 state
->ks_state
|= KBD_LED_VAL(kbd
);
1220 if (state
->ks_mode
!= *(int *)arg
) {
1221 ukbd_clear_state(kbd
);
1222 state
->ks_mode
= *(int *)arg
;
1223 kbd
->kb_savemode
= state
->ks_mode
;
1232 case KDGETLED
: /* get keyboard LED */
1233 *(int *)arg
= KBD_LED_VAL(kbd
);
1235 case KDSETLED
: /* set keyboard LED */
1236 /* NOTE: lock key state in ks_state won't be changed */
1237 if (*(int *)arg
& ~LOCK_MASK
) {
1242 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1243 if (kbd
->kb_keymap
->n_keys
> ALTGR_OFFSET
) {
1249 if (KBD_HAS_DEVICE(kbd
)) {
1250 set_leds(state
, ledmap
[i
& LED_MASK
]);
1251 /* XXX: error check? */
1253 KBD_LED_VAL(kbd
) = *(int *)arg
;
1256 case KDGKBSTATE
: /* get lock key state */
1257 *(int *)arg
= state
->ks_state
& LOCK_MASK
;
1259 case KDSKBSTATE
: /* set lock key state */
1260 if (*(int *)arg
& ~LOCK_MASK
) {
1264 state
->ks_state
&= ~LOCK_MASK
;
1265 state
->ks_state
|= *(int *)arg
;
1267 /* set LEDs and quit */
1268 return ukbd_ioctl(kbd
, KDSETLED
, arg
);
1270 case KDSETREPEAT
: /* set keyboard repeat rate (new interface) */
1272 if (!KBD_HAS_DEVICE(kbd
))
1274 if (((int *)arg
)[1] < 0)
1276 if (((int *)arg
)[0] < 0)
1278 else if (((int *)arg
)[0] == 0) /* fastest possible value */
1279 kbd
->kb_delay1
= 200;
1281 kbd
->kb_delay1
= ((int *)arg
)[0];
1282 kbd
->kb_delay2
= ((int *)arg
)[1];
1285 case KDSETRAD
: /* set keyboard repeat rate (old interface) */
1287 return set_typematic(kbd
, *(int *)arg
);
1289 case PIO_KEYMAP
: /* set keyboard translation table */
1290 case PIO_KEYMAPENT
: /* set keyboard translation table entry */
1291 case PIO_DEADKEYMAP
: /* set accent key translation table */
1292 state
->ks_accents
= 0;
1296 return genkbd_commonioctl(kbd
, cmd
, arg
);
1300 ukbddebug
= *(int *)arg
;
1309 /* lock the access to the keyboard */
1311 ukbd_lock(keyboard_t
*kbd
, int lock
)
1317 /* clear the internal state of the keyboard */
1319 ukbd_clear_state(keyboard_t
*kbd
)
1321 ukbd_state_t
*state
;
1323 state
= (ukbd_state_t
*)kbd
->kb_data
;
1324 state
->ks_flags
= 0;
1325 state
->ks_polling
= 0;
1326 state
->ks_state
&= LOCK_MASK
; /* preserve locking key state */
1327 state
->ks_accents
= 0;
1328 state
->ks_composed_char
= 0;
1329 #ifdef UKBD_EMULATE_ATSCANCODE
1330 state
->ks_buffered_char
[0] = 0;
1331 state
->ks_buffered_char
[1] = 0;
1333 bzero(&state
->ks_ndata
, sizeof(state
->ks_ndata
));
1334 bzero(&state
->ks_odata
, sizeof(state
->ks_odata
));
1335 bzero(&state
->ks_ntime
, sizeof(state
->ks_ntime
));
1336 bzero(&state
->ks_otime
, sizeof(state
->ks_otime
));
1339 /* save the internal state */
1341 ukbd_get_state(keyboard_t
*kbd
, void *buf
, size_t len
)
1344 return sizeof(ukbd_state_t
);
1345 if (len
< sizeof(ukbd_state_t
))
1347 bcopy(kbd
->kb_data
, buf
, sizeof(ukbd_state_t
));
1351 /* set the internal state */
1353 ukbd_set_state(keyboard_t
*kbd
, void *buf
, size_t len
)
1355 if (len
< sizeof(ukbd_state_t
))
1357 bcopy(buf
, kbd
->kb_data
, sizeof(ukbd_state_t
));
1362 ukbd_poll(keyboard_t
*kbd
, int on
)
1364 ukbd_state_t
*state
;
1365 usbd_device_handle dev
;
1367 state
= (ukbd_state_t
*)kbd
->kb_data
;
1368 usbd_interface2device_handle(state
->ks_iface
, &dev
);
1372 if (state
->ks_polling
== 0)
1373 usbd_set_polling(dev
, on
);
1374 ++state
->ks_polling
;
1376 --state
->ks_polling
;
1377 if (state
->ks_polling
== 0)
1378 usbd_set_polling(dev
, on
);
1384 /* local functions */
1387 probe_keyboard(struct usb_attach_arg
*uaa
, int flags
)
1389 usb_interface_descriptor_t
*id
;
1391 if (!uaa
->iface
) /* we attach to ifaces only */
1394 /* Check that this is a keyboard that speaks the boot protocol. */
1395 id
= usbd_get_interface_descriptor(uaa
->iface
);
1397 && id
->bInterfaceClass
== UICLASS_HID
1398 && id
->bInterfaceSubClass
== UISUBCLASS_BOOT
1399 && id
->bInterfaceProtocol
== UPROTO_BOOT_KEYBOARD
)
1400 return 0; /* found it */
1406 init_keyboard(ukbd_state_t
*state
, int *type
, int flags
)
1408 usb_endpoint_descriptor_t
*ed
;
1413 state
->ks_ifstate
|= DISCONNECTED
;
1415 ed
= usbd_interface2endpoint_descriptor(state
->ks_iface
, 0);
1417 kprintf("ukbd: could not read endpoint descriptor\n");
1421 DPRINTFN(10,("ukbd:init_keyboard: \
1422 bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
1423 ed
->bLength
, ed
->bDescriptorType
,
1424 UE_GET_ADDR(ed
->bEndpointAddress
),
1425 UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
? "in":"out",
1426 UE_GET_XFERTYPE(ed
->bmAttributes
),
1427 UGETW(ed
->wMaxPacketSize
), ed
->bInterval
));
1429 if (UE_GET_DIR(ed
->bEndpointAddress
) != UE_DIR_IN
||
1430 UE_GET_XFERTYPE(ed
->bmAttributes
) != UE_INTERRUPT
) {
1431 kprintf("ukbd: unexpected endpoint\n");
1435 if ((usbd_get_quirks(state
->ks_uaa
->device
)->uq_flags
& UQ_NO_SET_PROTO
) == 0) {
1436 err
= usbd_set_protocol(state
->ks_iface
, 0);
1437 DPRINTFN(5, ("ukbd:init_keyboard: protocol set\n"));
1439 kprintf("ukbd: set protocol failed\n");
1443 /* Ignore if SETIDLE fails since it is not crucial. */
1444 usbd_set_idle(state
->ks_iface
, 0, 0);
1446 state
->ks_ep_addr
= ed
->bEndpointAddress
;
1447 state
->ks_ifstate
&= ~DISCONNECTED
;
1453 set_leds(ukbd_state_t
*state
, int leds
)
1455 u_int8_t res
= leds
;
1457 DPRINTF(("ukbd:set_leds: state=%p leds=%d\n", state
, leds
));
1459 usbd_set_report_async(state
->ks_iface
, UHID_OUTPUT_REPORT
, 0, &res
, 1);
1463 set_typematic(keyboard_t
*kbd
, int code
)
1465 static int delays
[] = { 250, 500, 750, 1000 };
1466 static int rates
[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1467 68, 76, 84, 92, 100, 110, 118, 126,
1468 136, 152, 168, 184, 200, 220, 236, 252,
1469 272, 304, 336, 368, 400, 440, 472, 504 };
1473 kbd
->kb_delay1
= delays
[(code
>> 5) & 3];
1474 kbd
->kb_delay2
= rates
[code
& 0x1f];
1478 #ifdef UKBD_EMULATE_ATSCANCODE
1480 keycode2scancode(int keycode
, int shift
, int up
)
1482 static int scan
[] = {
1484 0x37 | SCAN_PREFIX_SHIFT
, /* PrintScreen */
1485 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1486 0x50, 0x51, 0x52, 0x53,
1487 0x46, /* XXX Pause/Break */
1493 if ((keycode
>= 89) && (keycode
< 89 + sizeof(scan
)/sizeof(scan
[0])))
1494 scancode
= scan
[keycode
- 89] | SCAN_PREFIX_E0
;
1496 if ((keycode
== 104) && !(shift
& (MOD_CONTROL_L
| MOD_CONTROL_R
)))
1497 scancode
= 0x45 | SCAN_PREFIX_E1
| SCAN_PREFIX_CTL
;
1498 if (shift
& (MOD_SHIFT_L
| MOD_SHIFT_R
))
1499 scancode
&= ~SCAN_PREFIX_SHIFT
;
1500 return (scancode
| (up
? SCAN_RELEASE
: SCAN_PRESS
));
1502 #endif /* UKBD_EMULATE_ATSCANCODE */
1505 ukbd_driver_load(module_t mod
, int what
, void *arg
)
1509 kbd_add_driver(&ukbd_kbd_driver
);
1512 kbd_delete_driver(&ukbd_kbd_driver
);
1515 return usbd_driver_load(mod
, what
, 0);