6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
44 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
47 #include "opt_compat.h"
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usb_mfunc.h>
53 #include <dev/usb/usb_error.h>
54 #include <dev/usb/usbhid.h>
56 #define USB_DEBUG_VAR ukbd_debug
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_util.h>
60 #include <dev/usb/usb_debug.h>
61 #include <dev/usb/usb_busdma.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_transfer.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_dynamic.h>
66 #include <dev/usb/usb_hid.h>
68 #include <dev/usb/quirk/usb_quirk.h>
70 #include <sys/ioccom.h>
71 #include <sys/filio.h>
75 #include <dev/kbd/kbdreg.h>
77 /* the initial key map, accent map and fkey strings */
78 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
79 #define KBD_DFLT_KEYMAP
83 /* the following file must be included after "ukbdmap.h" */
84 #include <dev/kbd/kbdtables.h>
87 static int ukbd_debug = 0;
89 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
90 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
91 &ukbd_debug, 0, "Debug level");
94 #define UPROTO_BOOT_KEYBOARD 1
96 #define UKBD_EMULATE_ATSCANCODE 1
97 #define UKBD_DRIVER_NAME "ukbd"
98 #define UKBD_NMOD 8 /* units */
99 #define UKBD_NKEYCODE 6 /* units */
100 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */
101 #define UKBD_IN_BUF_FULL (UKBD_IN_BUF_SIZE / 2) /* bytes */
102 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
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 uint8_t keycode[UKBD_NKEYCODE];
128 accentmap_t sc_accmap;
129 fkeytab_t sc_fkeymap[UKBD_NFKEY];
130 struct usb2_callout sc_callout;
131 struct ukbd_data sc_ndata;
132 struct ukbd_data sc_odata;
134 struct usb2_device *sc_udev;
135 struct usb2_interface *sc_iface;
136 struct usb2_xfer *sc_xfer[UKBD_N_TRANSFER];
138 uint32_t sc_ntime[UKBD_NKEYCODE];
139 uint32_t sc_otime[UKBD_NKEYCODE];
140 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
142 uint32_t sc_composed_char; /* composed char code, if non-zero */
143 #ifdef UKBD_EMULATE_ATSCANCODE
144 uint32_t sc_buffered_char[2];
146 uint32_t sc_flags; /* flags */
147 #define UKBD_FLAG_COMPOSE 0x0001
148 #define UKBD_FLAG_POLLING 0x0002
149 #define UKBD_FLAG_SET_LEDS 0x0004
150 #define UKBD_FLAG_INTR_STALL 0x0008
151 #define UKBD_FLAG_ATTACHED 0x0010
152 #define UKBD_FLAG_GONE 0x0020
154 int32_t sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
155 int32_t sc_state; /* shift/lock key state */
156 int32_t sc_accents; /* accent key index (> 0) */
159 uint16_t sc_inputhead;
160 uint16_t sc_inputtail;
162 uint8_t sc_leds; /* store for async led requests */
163 uint8_t sc_iface_index;
167 #define KEY_ERROR 0x01
170 #define KEY_RELEASE 0x400
171 #define KEY_INDEX(c) ((c) & 0xFF)
174 #define SCAN_RELEASE 0x80
175 #define SCAN_PREFIX_E0 0x100
176 #define SCAN_PREFIX_E1 0x200
177 #define SCAN_PREFIX_CTL 0x400
178 #define SCAN_PREFIX_SHIFT 0x800
179 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
180 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
181 #define SCAN_CHAR(c) ((c) & 0x7f)
187 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
188 {MOD_CONTROL_L, 0xe0},
189 {MOD_CONTROL_R, 0xe4},
198 #define NN 0 /* no translation */
200 * Translate USB keycodes to AT keyboard scancodes.
203 * FIXME: Mac USB keyboard generates:
204 * 0x53: keypad NumLock/Clear
211 static const uint8_t ukbd_trtab[256] = {
212 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
213 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
214 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
215 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
216 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
217 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
218 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
219 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
220 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
221 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
222 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
223 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
224 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
225 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
226 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
227 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
228 121, 120, NN, NN, NN, NN, NN, 115, /* 80 - 87 */
229 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */
230 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
231 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
232 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
233 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
234 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
235 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
236 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
237 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
238 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
239 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
240 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
241 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
242 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
243 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
247 static void ukbd_timeout(void *);
248 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
249 static int ukbd_set_typematic(keyboard_t *, int);
250 #ifdef UKBD_EMULATE_ATSCANCODE
251 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
253 static uint32_t ukbd_read_char(keyboard_t *, int);
254 static void ukbd_clear_state(keyboard_t *);
255 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
256 static int ukbd_enable(keyboard_t *);
257 static int ukbd_disable(keyboard_t *);
258 static void ukbd_interrupt(struct ukbd_softc *);
260 static device_probe_t ukbd_probe;
261 static device_attach_t ukbd_attach;
262 static device_detach_t ukbd_detach;
263 static device_resume_t ukbd_resume;
266 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
268 mtx_assert(&Giant, MA_OWNED);
270 DPRINTF("0x%02x (%d) %s\n", key, key,
271 (key & KEY_RELEASE) ? "released" : "pressed");
273 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
274 sc->sc_input[sc->sc_inputtail] = key;
276 ++(sc->sc_inputtail);
277 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
278 sc->sc_inputtail = 0;
281 DPRINTF("input buffer is full\n");
286 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
290 mtx_assert(&Giant, MA_OWNED);
292 if (sc->sc_inputs == 0) {
293 /* start transfer, if not already started */
294 usb2_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
296 if (sc->sc_flags & UKBD_FLAG_POLLING) {
297 DPRINTFN(2, "polling\n");
299 while (sc->sc_inputs == 0) {
301 usb2_do_poll(sc->sc_xfer, UKBD_N_TRANSFER);
303 DELAY(1000); /* delay 1 ms */
307 /* support repetition of keys: */
316 if (sc->sc_inputs == 0) {
319 c = sc->sc_input[sc->sc_inputhead];
321 ++(sc->sc_inputhead);
322 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
323 sc->sc_inputhead = 0;
330 ukbd_interrupt(struct ukbd_softc *sc)
334 uint32_t now = sc->sc_time_ms;
341 if (sc->sc_ndata.keycode[0] == KEY_ERROR) {
344 n_mod = sc->sc_ndata.modifiers;
345 o_mod = sc->sc_odata.modifiers;
346 if (n_mod != o_mod) {
347 for (i = 0; i < UKBD_NMOD; i++) {
348 if ((n_mod & ukbd_mods[i].mask) !=
349 (o_mod & ukbd_mods[i].mask)) {
350 ukbd_put_key(sc, ukbd_mods[i].key |
351 ((n_mod & ukbd_mods[i].mask) ?
352 KEY_PRESS : KEY_RELEASE));
356 /* Check for released keys. */
357 for (i = 0; i < UKBD_NKEYCODE; i++) {
358 key = sc->sc_odata.keycode[i];
362 for (j = 0; j < UKBD_NKEYCODE; j++) {
363 if (sc->sc_ndata.keycode[j] == 0) {
366 if (key == sc->sc_ndata.keycode[j]) {
370 ukbd_put_key(sc, key | KEY_RELEASE);
374 /* Check for pressed keys. */
375 for (i = 0; i < UKBD_NKEYCODE; i++) {
376 key = sc->sc_ndata.keycode[i];
380 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
381 for (j = 0; j < UKBD_NKEYCODE; j++) {
382 if (sc->sc_odata.keycode[j] == 0) {
385 if (key == sc->sc_odata.keycode[j]) {
387 /* key is still pressed */
389 sc->sc_ntime[i] = sc->sc_otime[j];
390 dtime = (sc->sc_otime[j] - now);
392 if (!(dtime & 0x80000000)) {
393 /* time has not elapsed */
396 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
400 ukbd_put_key(sc, key | KEY_PRESS);
403 * If any other key is presently down, force its repeat to be
404 * well in the future (100s). This makes the last key to be
405 * pressed do the autorepeat.
407 for (j = 0; j != UKBD_NKEYCODE; j++) {
409 sc->sc_ntime[j] = now + (100 * 1000);
414 sc->sc_odata = sc->sc_ndata;
416 bcopy(sc->sc_ntime, sc->sc_otime, sizeof(sc->sc_otime));
418 if (sc->sc_inputs == 0) {
421 if (sc->sc_flags & UKBD_FLAG_POLLING) {
424 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
425 KBD_IS_BUSY(&sc->sc_kbd)) {
426 /* let the callback function process the input */
427 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
428 sc->sc_kbd.kb_callback.kc_arg);
430 /* read and discard the input, no one is waiting for it */
432 c = ukbd_read_char(&sc->sc_kbd, 0);
433 } while (c != NOKEY);
440 ukbd_timeout(void *arg)
442 struct ukbd_softc *sc = arg;
444 mtx_assert(&Giant, MA_OWNED);
446 if (!(sc->sc_flags & UKBD_FLAG_POLLING)) {
447 sc->sc_time_ms += 25; /* milliseconds */
451 usb2_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
455 ukbd_clear_stall_callback(struct usb2_xfer *xfer)
457 struct ukbd_softc *sc = xfer->priv_sc;
458 struct usb2_xfer *xfer_other = sc->sc_xfer[UKBD_INTR_DT];
460 if (usb2_clear_stall_callback(xfer, xfer_other)) {
461 DPRINTF("stall cleared\n");
462 sc->sc_flags &= ~UKBD_FLAG_INTR_STALL;
463 usb2_transfer_start(xfer_other);
468 ukbd_intr_callback(struct usb2_xfer *xfer)
470 struct ukbd_softc *sc = xfer->priv_sc;
471 uint16_t len = xfer->actlen;
474 switch (USB_GET_STATE(xfer)) {
475 case USB_ST_TRANSFERRED:
476 DPRINTF("actlen=%d bytes\n", len);
478 if (len > sizeof(sc->sc_ndata)) {
479 len = sizeof(sc->sc_ndata);
482 bzero(&sc->sc_ndata, sizeof(sc->sc_ndata));
483 usb2_copy_out(xfer->frbuffers, 0, &sc->sc_ndata, len);
485 if (sc->sc_ndata.modifiers) {
486 DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers);
488 for (i = 0; i < UKBD_NKEYCODE; i++) {
489 if (sc->sc_ndata.keycode[i]) {
490 DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]);
493 #endif /* USB_DEBUG */
497 if (sc->sc_flags & UKBD_FLAG_INTR_STALL) {
498 usb2_transfer_start(sc->sc_xfer[UKBD_INTR_CS]);
501 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
502 xfer->frlengths[0] = xfer->max_data_length;
503 usb2_start_hardware(xfer);
505 DPRINTF("input queue is full!\n");
510 DPRINTF("error=%s\n", usb2_errstr(xfer->error));
512 if (xfer->error != USB_ERR_CANCELLED) {
513 /* try to clear stall first */
514 sc->sc_flags |= UKBD_FLAG_INTR_STALL;
515 usb2_transfer_start(sc->sc_xfer[UKBD_INTR_CS]);
522 ukbd_set_leds_callback(struct usb2_xfer *xfer)
524 struct usb2_device_request req;
526 struct ukbd_softc *sc = xfer->priv_sc;
528 switch (USB_GET_STATE(xfer)) {
529 case USB_ST_TRANSFERRED:
531 if (sc->sc_flags & UKBD_FLAG_SET_LEDS) {
532 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
534 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
535 req.bRequest = UR_SET_REPORT;
536 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
537 req.wIndex[0] = sc->sc_iface_no;
539 USETW(req.wLength, 1);
541 buf[0] = sc->sc_leds;
543 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
544 usb2_copy_in(xfer->frbuffers + 1, 0, buf, sizeof(buf));
546 xfer->frlengths[0] = sizeof(req);
547 xfer->frlengths[1] = sizeof(buf);
549 usb2_start_hardware(xfer);
554 DPRINTFN(0, "error=%s\n", usb2_errstr(xfer->error));
559 static const struct usb2_config ukbd_config[UKBD_N_TRANSFER] = {
562 .type = UE_INTERRUPT,
563 .endpoint = UE_ADDR_ANY,
564 .direction = UE_DIR_IN,
565 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
566 .bufsize = 0, /* use wMaxPacketSize */
567 .callback = &ukbd_intr_callback,
572 .endpoint = 0x00, /* Control pipe */
573 .direction = UE_DIR_ANY,
574 .bufsize = sizeof(struct usb2_device_request),
575 .callback = &ukbd_clear_stall_callback,
576 .timeout = 1000, /* 1 second */
577 .interval = 50, /* 50ms */
582 .endpoint = 0x00, /* Control pipe */
583 .direction = UE_DIR_ANY,
584 .bufsize = sizeof(struct usb2_device_request) + 1,
585 .callback = &ukbd_set_leds_callback,
586 .timeout = 1000, /* 1 second */
591 ukbd_probe(device_t dev)
593 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
594 struct usb2_attach_arg *uaa = device_get_ivars(dev);
601 if (uaa->usb_mode != USB_MODE_HOST) {
604 /* check that the keyboard speaks the boot protocol: */
605 if ((uaa->info.bInterfaceClass == UICLASS_HID)
606 && (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT)
607 && (uaa->info.bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)) {
608 if (usb2_test_quirk(uaa, UQ_KBD_IGNORE))
617 ukbd_attach(device_t dev)
619 struct ukbd_softc *sc = device_get_softc(dev);
620 struct usb2_attach_arg *uaa = device_get_ivars(dev);
621 int32_t unit = device_get_unit(dev);
622 keyboard_t *kbd = &sc->sc_kbd;
626 mtx_assert(&Giant, MA_OWNED);
628 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
630 kbd->kb_data = (void *)sc;
632 device_set_usb2_desc(dev);
634 sc->sc_udev = uaa->device;
635 sc->sc_iface = uaa->iface;
636 sc->sc_iface_index = uaa->info.bIfaceIndex;
637 sc->sc_iface_no = uaa->info.bIfaceNum;
638 sc->sc_mode = K_XLATE;
639 sc->sc_iface = uaa->iface;
641 usb2_callout_init_mtx(&sc->sc_callout, &Giant, 0);
643 err = usb2_transfer_setup(uaa->device,
644 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
645 UKBD_N_TRANSFER, sc, &Giant);
648 DPRINTF("error=%s\n", usb2_errstr(err));
651 /* setup default keyboard maps */
653 sc->sc_keymap = key_map;
654 sc->sc_accmap = accent_map;
655 for (n = 0; n < UKBD_NFKEY; n++) {
656 sc->sc_fkeymap[n] = fkey_tab[n];
659 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
660 sc->sc_fkeymap, UKBD_NFKEY);
662 KBD_FOUND_DEVICE(kbd);
664 ukbd_clear_state(kbd);
667 * FIXME: set the initial value for lock keys in "sc_state"
668 * according to the BIOS data?
672 /* ignore if SETIDLE fails, hence it is not crucial */
673 err = usb2_req_set_idle(sc->sc_udev, &Giant, sc->sc_iface_index, 0, 0);
675 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
679 if (kbd_register(kbd) < 0) {
682 KBD_CONFIG_DONE(kbd);
686 #ifdef KBD_INSTALL_CDEV
687 if (kbd_attach(kbd)) {
691 sc->sc_flags |= UKBD_FLAG_ATTACHED;
694 genkbd_diag(kbd, bootverbose);
696 /* lock keyboard mutex */
700 /* start the keyboard */
702 usb2_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
704 /* start the timer */
708 return (0); /* success */
712 return (ENXIO); /* error */
716 ukbd_detach(device_t dev)
718 struct ukbd_softc *sc = device_get_softc(dev);
721 mtx_assert(&Giant, MA_OWNED);
725 if (sc->sc_flags & UKBD_FLAG_POLLING) {
726 panic("cannot detach polled keyboard!\n");
728 sc->sc_flags |= UKBD_FLAG_GONE;
730 usb2_callout_stop(&sc->sc_callout);
732 ukbd_disable(&sc->sc_kbd);
734 #ifdef KBD_INSTALL_CDEV
735 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
736 error = kbd_detach(&sc->sc_kbd);
738 /* usb attach cannot return an error */
739 device_printf(dev, "WARNING: kbd_detach() "
740 "returned non-zero! (ignored)\n");
744 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
745 error = kbd_unregister(&sc->sc_kbd);
747 /* usb attach cannot return an error */
748 device_printf(dev, "WARNING: kbd_unregister() "
749 "returned non-zero! (ignored)\n");
752 sc->sc_kbd.kb_flags = 0;
754 usb2_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
756 usb2_callout_drain(&sc->sc_callout);
758 DPRINTF("%s: disconnected\n",
759 device_get_nameunit(dev));
765 ukbd_resume(device_t dev)
767 struct ukbd_softc *sc = device_get_softc(dev);
769 mtx_assert(&Giant, MA_OWNED);
771 ukbd_clear_state(&sc->sc_kbd);
776 /* early keyboard probe, not supported */
778 ukbd_configure(int flags)
783 /* detect a keyboard, not used */
785 ukbd__probe(int unit, void *arg, int flags)
787 mtx_assert(&Giant, MA_OWNED);
791 /* reset and initialize the device, not used */
793 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
795 mtx_assert(&Giant, MA_OWNED);
799 /* test the interface to the device, not used */
801 ukbd_test_if(keyboard_t *kbd)
803 mtx_assert(&Giant, MA_OWNED);
807 /* finish using this keyboard, not used */
809 ukbd_term(keyboard_t *kbd)
811 mtx_assert(&Giant, MA_OWNED);
815 /* keyboard interrupt routine, not used */
817 ukbd_intr(keyboard_t *kbd, void *arg)
819 mtx_assert(&Giant, MA_OWNED);
823 /* lock the access to the keyboard, not used */
825 ukbd_lock(keyboard_t *kbd, int lock)
827 mtx_assert(&Giant, MA_OWNED);
832 * Enable the access to the device; until this function is called,
833 * the client cannot read from the keyboard.
836 ukbd_enable(keyboard_t *kbd)
838 mtx_assert(&Giant, MA_OWNED);
843 /* disallow the access to the device */
845 ukbd_disable(keyboard_t *kbd)
847 mtx_assert(&Giant, MA_OWNED);
852 /* check if data is waiting */
854 ukbd_check(keyboard_t *kbd)
856 struct ukbd_softc *sc = kbd->kb_data;
858 if (!mtx_owned(&Giant)) {
859 return (0); /* XXX */
861 mtx_assert(&Giant, MA_OWNED);
863 if (!KBD_IS_ACTIVE(kbd)) {
866 #ifdef UKBD_EMULATE_ATSCANCODE
867 if (sc->sc_buffered_char[0]) {
871 if (sc->sc_inputs > 0) {
877 /* check if char is waiting */
879 ukbd_check_char(keyboard_t *kbd)
881 struct ukbd_softc *sc = kbd->kb_data;
883 if (!mtx_owned(&Giant)) {
884 return (0); /* XXX */
886 mtx_assert(&Giant, MA_OWNED);
888 if (!KBD_IS_ACTIVE(kbd)) {
891 if ((sc->sc_composed_char > 0) &&
892 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
895 return (ukbd_check(kbd));
899 /* read one byte from the keyboard if it's allowed */
901 ukbd_read(keyboard_t *kbd, int wait)
903 struct ukbd_softc *sc = kbd->kb_data;
906 #ifdef UKBD_EMULATE_ATSCANCODE
912 if (!mtx_owned(&Giant)) {
915 mtx_assert(&Giant, MA_OWNED);
917 #ifdef UKBD_EMULATE_ATSCANCODE
918 if (sc->sc_buffered_char[0]) {
919 scancode = sc->sc_buffered_char[0];
920 if (scancode & SCAN_PREFIX) {
921 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
922 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
924 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
925 sc->sc_buffered_char[1] = 0;
928 #endif /* UKBD_EMULATE_ATSCANCODE */
931 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
932 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) {
937 #ifdef UKBD_EMULATE_ATSCANCODE
938 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
942 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
943 (usbcode & KEY_RELEASE)));
944 #else /* !UKBD_EMULATE_ATSCANCODE */
946 #endif /* UKBD_EMULATE_ATSCANCODE */
949 /* read char from the keyboard */
951 ukbd_read_char(keyboard_t *kbd, int wait)
953 struct ukbd_softc *sc = kbd->kb_data;
958 #ifdef UKBD_EMULATE_ATSCANCODE
962 if (!mtx_owned(&Giant)) {
963 return (NOKEY); /* XXX */
965 mtx_assert(&Giant, MA_OWNED);
969 /* do we have a composed char to return ? */
971 if ((sc->sc_composed_char > 0) &&
972 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
974 action = sc->sc_composed_char;
975 sc->sc_composed_char = 0;
982 #ifdef UKBD_EMULATE_ATSCANCODE
984 /* do we have a pending raw scan code? */
986 if (sc->sc_mode == K_RAW) {
987 scancode = sc->sc_buffered_char[0];
989 if (scancode & SCAN_PREFIX) {
990 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
991 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
993 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
994 sc->sc_buffered_char[1] = 0;
998 #endif /* UKBD_EMULATE_ATSCANCODE */
1000 /* see if there is something in the keyboard port */
1002 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1003 if (usbcode == -1) {
1008 #ifdef UKBD_EMULATE_ATSCANCODE
1009 /* USB key index -> key code -> AT scan code */
1010 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1011 if (keycode == NN) {
1014 /* return an AT scan code for the K_RAW mode */
1015 if (sc->sc_mode == K_RAW) {
1016 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1017 (usbcode & KEY_RELEASE)));
1019 #else /* !UKBD_EMULATE_ATSCANCODE */
1021 /* return the byte as is for the K_RAW mode */
1022 if (sc->sc_mode == K_RAW) {
1025 /* USB key index -> key code */
1026 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1027 if (keycode == NN) {
1030 #endif /* UKBD_EMULATE_ATSCANCODE */
1033 case 0x38: /* left alt (compose key) */
1034 if (usbcode & KEY_RELEASE) {
1035 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1036 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1038 if (sc->sc_composed_char > 0xFF) {
1039 sc->sc_composed_char = 0;
1043 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1044 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1045 sc->sc_composed_char = 0;
1049 /* XXX: I don't like these... */
1050 case 0x5c: /* print screen */
1051 if (sc->sc_flags & ALTS) {
1052 keycode = 0x54; /* sysrq */
1055 case 0x68: /* pause/break */
1056 if (sc->sc_flags & CTLS) {
1057 keycode = 0x6c; /* break */
1062 /* return the key code in the K_CODE mode */
1063 if (usbcode & KEY_RELEASE) {
1064 keycode |= SCAN_RELEASE;
1066 if (sc->sc_mode == K_CODE) {
1069 /* compose a character code */
1070 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1072 /* key pressed, process it */
1075 case 0x49: /* keypad 7,8,9 */
1076 sc->sc_composed_char *= 10;
1077 sc->sc_composed_char += keycode - 0x40;
1078 goto check_composed;
1082 case 0x4D: /* keypad 4,5,6 */
1083 sc->sc_composed_char *= 10;
1084 sc->sc_composed_char += keycode - 0x47;
1085 goto check_composed;
1089 case 0x51: /* keypad 1,2,3 */
1090 sc->sc_composed_char *= 10;
1091 sc->sc_composed_char += keycode - 0x4E;
1092 goto check_composed;
1094 case 0x52: /* keypad 0 */
1095 sc->sc_composed_char *= 10;
1096 goto check_composed;
1098 /* key released, no interest here */
1099 case SCAN_RELEASE | 0x47:
1100 case SCAN_RELEASE | 0x48:
1101 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1102 case SCAN_RELEASE | 0x4B:
1103 case SCAN_RELEASE | 0x4C:
1104 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1105 case SCAN_RELEASE | 0x4F:
1106 case SCAN_RELEASE | 0x50:
1107 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1108 case SCAN_RELEASE | 0x52: /* keypad 0 */
1111 case 0x38: /* left alt key */
1115 if (sc->sc_composed_char > 0) {
1116 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1117 sc->sc_composed_char = 0;
1123 /* keycode to key action */
1124 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1125 (keycode & SCAN_RELEASE),
1126 &sc->sc_state, &sc->sc_accents);
1127 if (action == NOKEY) {
1134 if (sc->sc_composed_char <= 0xFF) {
1141 /* some useful control functions */
1143 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1145 /* translate LED_XXX bits into the device specific bits */
1146 static const uint8_t ledmap[8] = {
1147 0, 2, 1, 3, 4, 6, 5, 7,
1149 struct ukbd_softc *sc = kbd->kb_data;
1152 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1153 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1157 if (!mtx_owned(&Giant)) {
1159 * XXX big problem: If scroll lock is pressed and "printf()"
1160 * is called, the CPU will get here, to un-scroll lock the
1161 * keyboard. But if "printf()" acquires the "Giant" lock,
1162 * there will be a locking order reversal problem, so the
1163 * keyboard system must get out of "Giant" first, before the
1164 * CPU can proceed here ...
1168 mtx_assert(&Giant, MA_OWNED);
1171 case KDGKBMODE: /* get keyboard mode */
1172 *(int *)arg = sc->sc_mode;
1174 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1175 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1177 ival = IOCPARM_IVAL(arg);
1178 arg = (caddr_t)&ival;
1181 case KDSKBMODE: /* set keyboard mode */
1182 switch (*(int *)arg) {
1184 if (sc->sc_mode != K_XLATE) {
1185 /* make lock key state and LED state match */
1186 sc->sc_state &= ~LOCK_MASK;
1187 sc->sc_state |= KBD_LED_VAL(kbd);
1192 if (sc->sc_mode != *(int *)arg) {
1193 ukbd_clear_state(kbd);
1194 sc->sc_mode = *(int *)arg;
1202 case KDGETLED: /* get keyboard LED */
1203 *(int *)arg = KBD_LED_VAL(kbd);
1205 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1206 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1208 ival = IOCPARM_IVAL(arg);
1209 arg = (caddr_t)&ival;
1212 case KDSETLED: /* set keyboard LED */
1213 /* NOTE: lock key state in "sc_state" won't be changed */
1214 if (*(int *)arg & ~LOCK_MASK) {
1218 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1219 if (sc->sc_mode == K_XLATE &&
1220 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1226 if (KBD_HAS_DEVICE(kbd)) {
1227 ukbd_set_leds(sc, ledmap[i & LED_MASK]);
1229 KBD_LED_VAL(kbd) = *(int *)arg;
1231 case KDGKBSTATE: /* get lock key state */
1232 *(int *)arg = sc->sc_state & LOCK_MASK;
1234 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1235 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1237 ival = IOCPARM_IVAL(arg);
1238 arg = (caddr_t)&ival;
1241 case KDSKBSTATE: /* set lock key state */
1242 if (*(int *)arg & ~LOCK_MASK) {
1245 sc->sc_state &= ~LOCK_MASK;
1246 sc->sc_state |= *(int *)arg;
1248 /* set LEDs and quit */
1249 return (ukbd_ioctl(kbd, KDSETLED, arg));
1251 case KDSETREPEAT: /* set keyboard repeat rate (new
1253 if (!KBD_HAS_DEVICE(kbd)) {
1256 if (((int *)arg)[1] < 0) {
1259 if (((int *)arg)[0] < 0) {
1262 if (((int *)arg)[0] < 200) /* fastest possible value */
1263 kbd->kb_delay1 = 200;
1265 kbd->kb_delay1 = ((int *)arg)[0];
1266 kbd->kb_delay2 = ((int *)arg)[1];
1269 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1270 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1272 ival = IOCPARM_IVAL(arg);
1273 arg = (caddr_t)&ival;
1276 case KDSETRAD: /* set keyboard repeat rate (old
1278 return (ukbd_set_typematic(kbd, *(int *)arg));
1280 case PIO_KEYMAP: /* set keyboard translation table */
1281 case PIO_KEYMAPENT: /* set keyboard translation table
1283 case PIO_DEADKEYMAP: /* set accent key translation table */
1287 return (genkbd_commonioctl(kbd, cmd, arg));
1293 /* clear the internal state of the keyboard */
1295 ukbd_clear_state(keyboard_t *kbd)
1297 struct ukbd_softc *sc = kbd->kb_data;
1299 if (!mtx_owned(&Giant)) {
1302 mtx_assert(&Giant, MA_OWNED);
1304 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1305 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1307 sc->sc_composed_char = 0;
1308 #ifdef UKBD_EMULATE_ATSCANCODE
1309 sc->sc_buffered_char[0] = 0;
1310 sc->sc_buffered_char[1] = 0;
1312 bzero(&sc->sc_ndata, sizeof(sc->sc_ndata));
1313 bzero(&sc->sc_odata, sizeof(sc->sc_odata));
1314 bzero(&sc->sc_ntime, sizeof(sc->sc_ntime));
1315 bzero(&sc->sc_otime, sizeof(sc->sc_otime));
1318 /* save the internal state, not used */
1320 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1322 mtx_assert(&Giant, MA_OWNED);
1323 return (len == 0) ? 1 : -1;
1326 /* set the internal state, not used */
1328 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1330 mtx_assert(&Giant, MA_OWNED);
1335 ukbd_poll(keyboard_t *kbd, int on)
1337 struct ukbd_softc *sc = kbd->kb_data;
1339 if (!mtx_owned(&Giant)) {
1340 return (0); /* XXX */
1342 mtx_assert(&Giant, MA_OWNED);
1345 sc->sc_flags |= UKBD_FLAG_POLLING;
1347 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1352 /* local functions */
1355 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1357 DPRINTF("leds=0x%02x\n", leds);
1360 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1362 /* start transfer, if not already started */
1364 usb2_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1368 ukbd_set_typematic(keyboard_t *kbd, int code)
1370 static const int delays[] = {250, 500, 750, 1000};
1371 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1372 68, 76, 84, 92, 100, 110, 118, 126,
1373 136, 152, 168, 184, 200, 220, 236, 252,
1374 272, 304, 336, 368, 400, 440, 472, 504};
1379 kbd->kb_delay1 = delays[(code >> 5) & 3];
1380 kbd->kb_delay2 = rates[code & 0x1f];
1384 #ifdef UKBD_EMULATE_ATSCANCODE
1386 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1388 static const int scan[] = {
1390 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1391 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1392 0x50, 0x51, 0x52, 0x53,
1393 0x46, /* XXX Pause/Break */
1395 /* SUN TYPE 6 USB KEYBOARD */
1396 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
1397 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e,
1401 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
1402 code = scan[code - 89] | SCAN_PREFIX_E0;
1405 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
1406 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
1408 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
1409 code &= ~SCAN_PREFIX_SHIFT;
1411 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1413 if (code & SCAN_PREFIX) {
1414 if (code & SCAN_PREFIX_CTL) {
1416 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
1417 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
1418 } else if (code & SCAN_PREFIX_SHIFT) {
1420 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1421 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1423 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1424 sc->sc_buffered_char[1] = 0;
1426 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1432 #endif /* UKBD_EMULATE_ATSCANCODE */
1434 keyboard_switch_t ukbdsw = {
1435 .probe = &ukbd__probe,
1439 .test_if = &ukbd_test_if,
1440 .enable = &ukbd_enable,
1441 .disable = &ukbd_disable,
1443 .check = &ukbd_check,
1444 .read_char = &ukbd_read_char,
1445 .check_char = &ukbd_check_char,
1446 .ioctl = &ukbd_ioctl,
1448 .clear_state = &ukbd_clear_state,
1449 .get_state = &ukbd_get_state,
1450 .set_state = &ukbd_set_state,
1451 .get_fkeystr = &genkbd_get_fkeystr,
1453 .diag = &genkbd_diag,
1456 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
1459 ukbd_driver_load(module_t mod, int what, void *arg)
1463 kbd_add_driver(&ukbd_kbd_driver);
1466 kbd_delete_driver(&ukbd_kbd_driver);
1472 static devclass_t ukbd_devclass;
1474 static device_method_t ukbd_methods[] = {
1475 DEVMETHOD(device_probe, ukbd_probe),
1476 DEVMETHOD(device_attach, ukbd_attach),
1477 DEVMETHOD(device_detach, ukbd_detach),
1478 DEVMETHOD(device_resume, ukbd_resume),
1482 static driver_t ukbd_driver = {
1484 .methods = ukbd_methods,
1485 .size = sizeof(struct ukbd_softc),
1488 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
1489 MODULE_DEPEND(ukbd, usb, 1, 1, 1);