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 hid_location sc_loc_apple_eject;
131 struct hid_location sc_loc_apple_fn;
132 struct usb2_callout sc_callout;
133 struct ukbd_data sc_ndata;
134 struct ukbd_data sc_odata;
136 struct usb2_device *sc_udev;
137 struct usb2_interface *sc_iface;
138 struct usb2_xfer *sc_xfer[UKBD_N_TRANSFER];
140 uint32_t sc_ntime[UKBD_NKEYCODE];
141 uint32_t sc_otime[UKBD_NKEYCODE];
142 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
144 uint32_t sc_composed_char; /* composed char code, if non-zero */
145 #ifdef UKBD_EMULATE_ATSCANCODE
146 uint32_t sc_buffered_char[2];
148 uint32_t sc_flags; /* flags */
149 #define UKBD_FLAG_COMPOSE 0x0001
150 #define UKBD_FLAG_POLLING 0x0002
151 #define UKBD_FLAG_SET_LEDS 0x0004
152 #define UKBD_FLAG_ATTACHED 0x0010
153 #define UKBD_FLAG_GONE 0x0020
154 #define UKBD_FLAG_APPLE_EJECT 0x0040
155 #define UKBD_FLAG_APPLE_FN 0x0080
156 #define UKBD_FLAG_APPLE_SWAP 0x0100
158 int32_t sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
159 int32_t sc_state; /* shift/lock key state */
160 int32_t sc_accents; /* accent key index (> 0) */
163 uint16_t sc_inputhead;
164 uint16_t sc_inputtail;
166 uint8_t sc_leds; /* store for async led requests */
167 uint8_t sc_iface_index;
173 #define KEY_ERROR 0x01
176 #define KEY_RELEASE 0x400
177 #define KEY_INDEX(c) ((c) & 0xFF)
180 #define SCAN_RELEASE 0x80
181 #define SCAN_PREFIX_E0 0x100
182 #define SCAN_PREFIX_E1 0x200
183 #define SCAN_PREFIX_CTL 0x400
184 #define SCAN_PREFIX_SHIFT 0x800
185 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
186 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
187 #define SCAN_CHAR(c) ((c) & 0x7f)
193 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
194 {MOD_CONTROL_L, 0xe0},
195 {MOD_CONTROL_R, 0xe4},
204 #define NN 0 /* no translation */
206 * Translate USB keycodes to AT keyboard scancodes.
209 * FIXME: Mac USB keyboard generates:
210 * 0x53: keypad NumLock/Clear
217 static const uint8_t ukbd_trtab[256] = {
218 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
219 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
220 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
221 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
222 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
223 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
224 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
225 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
226 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
227 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
228 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
229 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
230 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
231 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
232 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
233 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
234 121, 120, NN, NN, NN, NN, NN, 115, /* 80 - 87 */
235 112, 125, 121, 123, NN, NN, NN, NN, /* 88 - 8F */
236 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
237 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
238 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
239 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
240 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
241 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
242 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
243 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
244 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
245 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
246 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
247 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
248 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
249 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
253 static void ukbd_timeout(void *);
254 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
255 static int ukbd_set_typematic(keyboard_t *, int);
256 #ifdef UKBD_EMULATE_ATSCANCODE
257 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
259 static uint32_t ukbd_read_char(keyboard_t *, int);
260 static void ukbd_clear_state(keyboard_t *);
261 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
262 static int ukbd_enable(keyboard_t *);
263 static int ukbd_disable(keyboard_t *);
264 static void ukbd_interrupt(struct ukbd_softc *);
266 static device_probe_t ukbd_probe;
267 static device_attach_t ukbd_attach;
268 static device_detach_t ukbd_detach;
269 static device_resume_t ukbd_resume;
272 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
274 mtx_assert(&Giant, MA_OWNED);
276 DPRINTF("0x%02x (%d) %s\n", key, key,
277 (key & KEY_RELEASE) ? "released" : "pressed");
279 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
280 sc->sc_input[sc->sc_inputtail] = key;
282 ++(sc->sc_inputtail);
283 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
284 sc->sc_inputtail = 0;
287 DPRINTF("input buffer is full\n");
292 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
296 mtx_assert(&Giant, MA_OWNED);
298 if (sc->sc_inputs == 0) {
299 /* start transfer, if not already started */
300 usb2_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
302 if (sc->sc_flags & UKBD_FLAG_POLLING) {
303 DPRINTFN(2, "polling\n");
305 while (sc->sc_inputs == 0) {
307 usb2_do_poll(sc->sc_xfer, UKBD_N_TRANSFER);
309 DELAY(1000); /* delay 1 ms */
313 /* support repetition of keys: */
322 if (sc->sc_inputs == 0) {
325 c = sc->sc_input[sc->sc_inputhead];
327 ++(sc->sc_inputhead);
328 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
329 sc->sc_inputhead = 0;
336 ukbd_interrupt(struct ukbd_softc *sc)
340 uint32_t now = sc->sc_time_ms;
347 if (sc->sc_ndata.keycode[0] == KEY_ERROR) {
350 n_mod = sc->sc_ndata.modifiers;
351 o_mod = sc->sc_odata.modifiers;
352 if (n_mod != o_mod) {
353 for (i = 0; i < UKBD_NMOD; i++) {
354 if ((n_mod & ukbd_mods[i].mask) !=
355 (o_mod & ukbd_mods[i].mask)) {
356 ukbd_put_key(sc, ukbd_mods[i].key |
357 ((n_mod & ukbd_mods[i].mask) ?
358 KEY_PRESS : KEY_RELEASE));
362 /* Check for released keys. */
363 for (i = 0; i < UKBD_NKEYCODE; i++) {
364 key = sc->sc_odata.keycode[i];
368 for (j = 0; j < UKBD_NKEYCODE; j++) {
369 if (sc->sc_ndata.keycode[j] == 0) {
372 if (key == sc->sc_ndata.keycode[j]) {
376 ukbd_put_key(sc, key | KEY_RELEASE);
380 /* Check for pressed keys. */
381 for (i = 0; i < UKBD_NKEYCODE; i++) {
382 key = sc->sc_ndata.keycode[i];
386 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
387 for (j = 0; j < UKBD_NKEYCODE; j++) {
388 if (sc->sc_odata.keycode[j] == 0) {
391 if (key == sc->sc_odata.keycode[j]) {
393 /* key is still pressed */
395 sc->sc_ntime[i] = sc->sc_otime[j];
396 dtime = (sc->sc_otime[j] - now);
398 if (!(dtime & 0x80000000)) {
399 /* time has not elapsed */
402 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
406 ukbd_put_key(sc, key | KEY_PRESS);
409 * If any other key is presently down, force its repeat to be
410 * well in the future (100s). This makes the last key to be
411 * pressed do the autorepeat.
413 for (j = 0; j != UKBD_NKEYCODE; j++) {
415 sc->sc_ntime[j] = now + (100 * 1000);
420 sc->sc_odata = sc->sc_ndata;
422 bcopy(sc->sc_ntime, sc->sc_otime, sizeof(sc->sc_otime));
424 if (sc->sc_inputs == 0) {
427 if (sc->sc_flags & UKBD_FLAG_POLLING) {
430 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
431 KBD_IS_BUSY(&sc->sc_kbd)) {
432 /* let the callback function process the input */
433 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
434 sc->sc_kbd.kb_callback.kc_arg);
436 /* read and discard the input, no one is waiting for it */
438 c = ukbd_read_char(&sc->sc_kbd, 0);
439 } while (c != NOKEY);
446 ukbd_timeout(void *arg)
448 struct ukbd_softc *sc = arg;
450 mtx_assert(&Giant, MA_OWNED);
452 if (!(sc->sc_flags & UKBD_FLAG_POLLING)) {
453 sc->sc_time_ms += 25; /* milliseconds */
457 usb2_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
461 ukbd_apple_fn(uint8_t keycode) {
463 case 0x28: return 0x49; /* RETURN -> INSERT */
464 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
465 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
466 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
467 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
468 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
469 default: return keycode;
474 ukbd_apple_swap(uint8_t keycode) {
476 case 0x35: return 0x64;
477 case 0x64: return 0x35;
478 default: return keycode;
483 ukbd_intr_callback(struct usb2_xfer *xfer)
485 struct ukbd_softc *sc = xfer->priv_sc;
486 uint16_t len = xfer->actlen;
493 switch (USB_GET_STATE(xfer)) {
494 case USB_ST_TRANSFERRED:
495 DPRINTF("actlen=%d bytes\n", len);
498 DPRINTF("zero length data\n");
502 if (sc->sc_kbd_id != 0) {
503 /* check and remove HID ID byte */
504 usb2_copy_out(xfer->frbuffers, 0, &id, 1);
505 if (id != sc->sc_kbd_id) {
506 DPRINTF("wrong HID ID\n");
515 if (len > sizeof(sc->sc_ndata)) {
516 len = sizeof(sc->sc_ndata);
520 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
521 usb2_copy_out(xfer->frbuffers, offset,
524 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
525 hid_get_data((uint8_t *)&sc->sc_ndata,
526 len, &sc->sc_loc_apple_eject))
531 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
532 hid_get_data((uint8_t *)&sc->sc_ndata,
533 len, &sc->sc_loc_apple_fn))
538 DPRINTF("apple_eject=%u apple_fn=%u\n",
539 apple_eject, apple_fn);
541 if (sc->sc_ndata.modifiers) {
542 DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers);
544 for (i = 0; i < UKBD_NKEYCODE; i++) {
545 if (sc->sc_ndata.keycode[i]) {
546 DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]);
549 #endif /* USB_DEBUG */
552 for (i = 0; i < UKBD_NKEYCODE; i++) {
553 sc->sc_ndata.keycode[i] =
554 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
558 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
559 for (i = 0; i < UKBD_NKEYCODE; i++) {
560 sc->sc_ndata.keycode[i] =
561 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
569 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
570 xfer->frlengths[0] = xfer->max_data_length;
571 usb2_start_hardware(xfer);
573 DPRINTF("input queue is full!\n");
578 DPRINTF("error=%s\n", usb2_errstr(xfer->error));
580 if (xfer->error != USB_ERR_CANCELLED) {
581 /* try to clear stall first */
582 xfer->flags.stall_pipe = 1;
590 ukbd_set_leds_callback(struct usb2_xfer *xfer)
592 struct usb2_device_request req;
594 struct ukbd_softc *sc = xfer->priv_sc;
596 switch (USB_GET_STATE(xfer)) {
597 case USB_ST_TRANSFERRED:
599 if (sc->sc_flags & UKBD_FLAG_SET_LEDS) {
600 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
602 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
603 req.bRequest = UR_SET_REPORT;
604 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
605 req.wIndex[0] = sc->sc_iface_no;
609 /* check if we need to prefix an ID byte */
610 if (sc->sc_led_id != 0) {
612 buf[0] = sc->sc_led_id;
613 buf[1] = sc->sc_leds;
616 buf[0] = sc->sc_leds;
620 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
621 usb2_copy_in(xfer->frbuffers + 1, 0, buf, sizeof(buf));
623 xfer->frlengths[0] = sizeof(req);
624 xfer->frlengths[1] = req.wLength[0];
626 usb2_start_hardware(xfer);
631 DPRINTFN(0, "error=%s\n", usb2_errstr(xfer->error));
636 static const struct usb2_config ukbd_config[UKBD_N_TRANSFER] = {
639 .type = UE_INTERRUPT,
640 .endpoint = UE_ADDR_ANY,
641 .direction = UE_DIR_IN,
642 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
643 .bufsize = 0, /* use wMaxPacketSize */
644 .callback = &ukbd_intr_callback,
649 .endpoint = 0x00, /* Control pipe */
650 .direction = UE_DIR_ANY,
651 .bufsize = sizeof(struct usb2_device_request) + 8,
652 .callback = &ukbd_set_leds_callback,
653 .timeout = 1000, /* 1 second */
658 ukbd_probe(device_t dev)
660 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
661 struct usb2_attach_arg *uaa = device_get_ivars(dev);
668 if (uaa->usb_mode != USB_MODE_HOST) {
671 /* check that the keyboard speaks the boot protocol: */
672 if ((uaa->info.bInterfaceClass == UICLASS_HID)
673 && (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT)
674 && (uaa->info.bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)) {
675 if (usb2_test_quirk(uaa, UQ_KBD_IGNORE))
684 ukbd_attach(device_t dev)
686 struct ukbd_softc *sc = device_get_softc(dev);
687 struct usb2_attach_arg *uaa = device_get_ivars(dev);
688 int32_t unit = device_get_unit(dev);
689 keyboard_t *kbd = &sc->sc_kbd;
690 void *hid_ptr = NULL;
696 mtx_assert(&Giant, MA_OWNED);
698 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
700 kbd->kb_data = (void *)sc;
702 device_set_usb2_desc(dev);
704 sc->sc_udev = uaa->device;
705 sc->sc_iface = uaa->iface;
706 sc->sc_iface_index = uaa->info.bIfaceIndex;
707 sc->sc_iface_no = uaa->info.bIfaceNum;
708 sc->sc_mode = K_XLATE;
709 sc->sc_iface = uaa->iface;
711 usb2_callout_init_mtx(&sc->sc_callout, &Giant, 0);
713 err = usb2_transfer_setup(uaa->device,
714 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
715 UKBD_N_TRANSFER, sc, &Giant);
718 DPRINTF("error=%s\n", usb2_errstr(err));
721 /* setup default keyboard maps */
723 sc->sc_keymap = key_map;
724 sc->sc_accmap = accent_map;
725 for (n = 0; n < UKBD_NFKEY; n++) {
726 sc->sc_fkeymap[n] = fkey_tab[n];
729 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
730 sc->sc_fkeymap, UKBD_NFKEY);
732 KBD_FOUND_DEVICE(kbd);
734 ukbd_clear_state(kbd);
737 * FIXME: set the initial value for lock keys in "sc_state"
738 * according to the BIOS data?
742 /* figure out if there is an ID byte in the data */
743 err = usb2_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
744 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
748 /* investigate if this is an Apple Keyboard */
749 if (hid_locate(hid_ptr, hid_len,
750 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
751 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
753 if (flags & HIO_VARIABLE)
754 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
755 UKBD_FLAG_APPLE_SWAP;
756 if (hid_locate(hid_ptr, hid_len,
757 HID_USAGE2(0xFFFF, 0x0003),
758 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
760 if (flags & HIO_VARIABLE)
761 sc->sc_flags |= UKBD_FLAG_APPLE_FN |
762 UKBD_FLAG_APPLE_SWAP;
763 if (temp_id != sc->sc_kbd_id) {
764 DPRINTF("HID IDs mismatch\n");
769 * Assume the first HID ID contains the
772 hid_report_size(hid_ptr, hid_len,
773 hid_input, &sc->sc_kbd_id);
776 /* investigate if we need an ID-byte for the leds */
777 hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id);
779 free(hid_ptr, M_TEMP);
782 /* ignore if SETIDLE fails, hence it is not crucial */
783 err = usb2_req_set_idle(sc->sc_udev, &Giant, sc->sc_iface_index, 0, 0);
785 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
789 if (kbd_register(kbd) < 0) {
792 KBD_CONFIG_DONE(kbd);
796 #ifdef KBD_INSTALL_CDEV
797 if (kbd_attach(kbd)) {
801 sc->sc_flags |= UKBD_FLAG_ATTACHED;
804 genkbd_diag(kbd, bootverbose);
806 /* lock keyboard mutex */
810 /* start the keyboard */
812 usb2_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
814 /* start the timer */
818 return (0); /* success */
822 return (ENXIO); /* error */
826 ukbd_detach(device_t dev)
828 struct ukbd_softc *sc = device_get_softc(dev);
831 mtx_assert(&Giant, MA_OWNED);
835 if (sc->sc_flags & UKBD_FLAG_POLLING) {
836 panic("cannot detach polled keyboard!\n");
838 sc->sc_flags |= UKBD_FLAG_GONE;
840 usb2_callout_stop(&sc->sc_callout);
842 ukbd_disable(&sc->sc_kbd);
844 #ifdef KBD_INSTALL_CDEV
845 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
846 error = kbd_detach(&sc->sc_kbd);
848 /* usb attach cannot return an error */
849 device_printf(dev, "WARNING: kbd_detach() "
850 "returned non-zero! (ignored)\n");
854 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
855 error = kbd_unregister(&sc->sc_kbd);
857 /* usb attach cannot return an error */
858 device_printf(dev, "WARNING: kbd_unregister() "
859 "returned non-zero! (ignored)\n");
862 sc->sc_kbd.kb_flags = 0;
864 usb2_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
866 usb2_callout_drain(&sc->sc_callout);
868 DPRINTF("%s: disconnected\n",
869 device_get_nameunit(dev));
875 ukbd_resume(device_t dev)
877 struct ukbd_softc *sc = device_get_softc(dev);
879 mtx_assert(&Giant, MA_OWNED);
881 ukbd_clear_state(&sc->sc_kbd);
886 /* early keyboard probe, not supported */
888 ukbd_configure(int flags)
893 /* detect a keyboard, not used */
895 ukbd__probe(int unit, void *arg, int flags)
897 mtx_assert(&Giant, MA_OWNED);
901 /* reset and initialize the device, not used */
903 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
905 mtx_assert(&Giant, MA_OWNED);
909 /* test the interface to the device, not used */
911 ukbd_test_if(keyboard_t *kbd)
913 mtx_assert(&Giant, MA_OWNED);
917 /* finish using this keyboard, not used */
919 ukbd_term(keyboard_t *kbd)
921 mtx_assert(&Giant, MA_OWNED);
925 /* keyboard interrupt routine, not used */
927 ukbd_intr(keyboard_t *kbd, void *arg)
929 mtx_assert(&Giant, MA_OWNED);
933 /* lock the access to the keyboard, not used */
935 ukbd_lock(keyboard_t *kbd, int lock)
937 mtx_assert(&Giant, MA_OWNED);
942 * Enable the access to the device; until this function is called,
943 * the client cannot read from the keyboard.
946 ukbd_enable(keyboard_t *kbd)
948 mtx_assert(&Giant, MA_OWNED);
953 /* disallow the access to the device */
955 ukbd_disable(keyboard_t *kbd)
957 mtx_assert(&Giant, MA_OWNED);
962 /* check if data is waiting */
964 ukbd_check(keyboard_t *kbd)
966 struct ukbd_softc *sc = kbd->kb_data;
968 if (!mtx_owned(&Giant)) {
969 return (0); /* XXX */
971 mtx_assert(&Giant, MA_OWNED);
973 if (!KBD_IS_ACTIVE(kbd)) {
976 #ifdef UKBD_EMULATE_ATSCANCODE
977 if (sc->sc_buffered_char[0]) {
981 if (sc->sc_inputs > 0) {
987 /* check if char is waiting */
989 ukbd_check_char(keyboard_t *kbd)
991 struct ukbd_softc *sc = kbd->kb_data;
993 if (!mtx_owned(&Giant)) {
994 return (0); /* XXX */
996 mtx_assert(&Giant, MA_OWNED);
998 if (!KBD_IS_ACTIVE(kbd)) {
1001 if ((sc->sc_composed_char > 0) &&
1002 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1005 return (ukbd_check(kbd));
1009 /* read one byte from the keyboard if it's allowed */
1011 ukbd_read(keyboard_t *kbd, int wait)
1013 struct ukbd_softc *sc = kbd->kb_data;
1016 #ifdef UKBD_EMULATE_ATSCANCODE
1022 if (!mtx_owned(&Giant)) {
1023 return -1; /* XXX */
1025 mtx_assert(&Giant, MA_OWNED);
1027 #ifdef UKBD_EMULATE_ATSCANCODE
1028 if (sc->sc_buffered_char[0]) {
1029 scancode = sc->sc_buffered_char[0];
1030 if (scancode & SCAN_PREFIX) {
1031 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1032 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1034 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1035 sc->sc_buffered_char[1] = 0;
1038 #endif /* UKBD_EMULATE_ATSCANCODE */
1041 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1042 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) {
1047 #ifdef UKBD_EMULATE_ATSCANCODE
1048 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1049 if (keycode == NN) {
1052 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1053 (usbcode & KEY_RELEASE)));
1054 #else /* !UKBD_EMULATE_ATSCANCODE */
1056 #endif /* UKBD_EMULATE_ATSCANCODE */
1059 /* read char from the keyboard */
1061 ukbd_read_char(keyboard_t *kbd, int wait)
1063 struct ukbd_softc *sc = kbd->kb_data;
1068 #ifdef UKBD_EMULATE_ATSCANCODE
1072 if (!mtx_owned(&Giant)) {
1073 return (NOKEY); /* XXX */
1075 mtx_assert(&Giant, MA_OWNED);
1079 /* do we have a composed char to return ? */
1081 if ((sc->sc_composed_char > 0) &&
1082 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1084 action = sc->sc_composed_char;
1085 sc->sc_composed_char = 0;
1087 if (action > 0xFF) {
1092 #ifdef UKBD_EMULATE_ATSCANCODE
1094 /* do we have a pending raw scan code? */
1096 if (sc->sc_mode == K_RAW) {
1097 scancode = sc->sc_buffered_char[0];
1099 if (scancode & SCAN_PREFIX) {
1100 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1101 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1103 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1104 sc->sc_buffered_char[1] = 0;
1108 #endif /* UKBD_EMULATE_ATSCANCODE */
1110 /* see if there is something in the keyboard port */
1112 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1113 if (usbcode == -1) {
1118 #ifdef UKBD_EMULATE_ATSCANCODE
1119 /* USB key index -> key code -> AT scan code */
1120 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1121 if (keycode == NN) {
1124 /* return an AT scan code for the K_RAW mode */
1125 if (sc->sc_mode == K_RAW) {
1126 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1127 (usbcode & KEY_RELEASE)));
1129 #else /* !UKBD_EMULATE_ATSCANCODE */
1131 /* return the byte as is for the K_RAW mode */
1132 if (sc->sc_mode == K_RAW) {
1135 /* USB key index -> key code */
1136 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1137 if (keycode == NN) {
1140 #endif /* UKBD_EMULATE_ATSCANCODE */
1143 case 0x38: /* left alt (compose key) */
1144 if (usbcode & KEY_RELEASE) {
1145 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1146 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1148 if (sc->sc_composed_char > 0xFF) {
1149 sc->sc_composed_char = 0;
1153 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1154 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1155 sc->sc_composed_char = 0;
1159 /* XXX: I don't like these... */
1160 case 0x5c: /* print screen */
1161 if (sc->sc_flags & ALTS) {
1162 keycode = 0x54; /* sysrq */
1165 case 0x68: /* pause/break */
1166 if (sc->sc_flags & CTLS) {
1167 keycode = 0x6c; /* break */
1172 /* return the key code in the K_CODE mode */
1173 if (usbcode & KEY_RELEASE) {
1174 keycode |= SCAN_RELEASE;
1176 if (sc->sc_mode == K_CODE) {
1179 /* compose a character code */
1180 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1182 /* key pressed, process it */
1185 case 0x49: /* keypad 7,8,9 */
1186 sc->sc_composed_char *= 10;
1187 sc->sc_composed_char += keycode - 0x40;
1188 goto check_composed;
1192 case 0x4D: /* keypad 4,5,6 */
1193 sc->sc_composed_char *= 10;
1194 sc->sc_composed_char += keycode - 0x47;
1195 goto check_composed;
1199 case 0x51: /* keypad 1,2,3 */
1200 sc->sc_composed_char *= 10;
1201 sc->sc_composed_char += keycode - 0x4E;
1202 goto check_composed;
1204 case 0x52: /* keypad 0 */
1205 sc->sc_composed_char *= 10;
1206 goto check_composed;
1208 /* key released, no interest here */
1209 case SCAN_RELEASE | 0x47:
1210 case SCAN_RELEASE | 0x48:
1211 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1212 case SCAN_RELEASE | 0x4B:
1213 case SCAN_RELEASE | 0x4C:
1214 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1215 case SCAN_RELEASE | 0x4F:
1216 case SCAN_RELEASE | 0x50:
1217 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1218 case SCAN_RELEASE | 0x52: /* keypad 0 */
1221 case 0x38: /* left alt key */
1225 if (sc->sc_composed_char > 0) {
1226 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1227 sc->sc_composed_char = 0;
1233 /* keycode to key action */
1234 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1235 (keycode & SCAN_RELEASE),
1236 &sc->sc_state, &sc->sc_accents);
1237 if (action == NOKEY) {
1244 if (sc->sc_composed_char <= 0xFF) {
1251 /* some useful control functions */
1253 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1255 /* translate LED_XXX bits into the device specific bits */
1256 static const uint8_t ledmap[8] = {
1257 0, 2, 1, 3, 4, 6, 5, 7,
1259 struct ukbd_softc *sc = kbd->kb_data;
1262 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1263 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1267 if (!mtx_owned(&Giant)) {
1269 * XXX big problem: If scroll lock is pressed and "printf()"
1270 * is called, the CPU will get here, to un-scroll lock the
1271 * keyboard. But if "printf()" acquires the "Giant" lock,
1272 * there will be a locking order reversal problem, so the
1273 * keyboard system must get out of "Giant" first, before the
1274 * CPU can proceed here ...
1278 mtx_assert(&Giant, MA_OWNED);
1281 case KDGKBMODE: /* get keyboard mode */
1282 *(int *)arg = sc->sc_mode;
1284 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1285 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1287 ival = IOCPARM_IVAL(arg);
1288 arg = (caddr_t)&ival;
1291 case KDSKBMODE: /* set keyboard mode */
1292 switch (*(int *)arg) {
1294 if (sc->sc_mode != K_XLATE) {
1295 /* make lock key state and LED state match */
1296 sc->sc_state &= ~LOCK_MASK;
1297 sc->sc_state |= KBD_LED_VAL(kbd);
1302 if (sc->sc_mode != *(int *)arg) {
1303 ukbd_clear_state(kbd);
1304 sc->sc_mode = *(int *)arg;
1312 case KDGETLED: /* get keyboard LED */
1313 *(int *)arg = KBD_LED_VAL(kbd);
1315 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1316 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1318 ival = IOCPARM_IVAL(arg);
1319 arg = (caddr_t)&ival;
1322 case KDSETLED: /* set keyboard LED */
1323 /* NOTE: lock key state in "sc_state" won't be changed */
1324 if (*(int *)arg & ~LOCK_MASK) {
1328 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1329 if (sc->sc_mode == K_XLATE &&
1330 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1336 if (KBD_HAS_DEVICE(kbd)) {
1337 ukbd_set_leds(sc, ledmap[i & LED_MASK]);
1339 KBD_LED_VAL(kbd) = *(int *)arg;
1341 case KDGKBSTATE: /* get lock key state */
1342 *(int *)arg = sc->sc_state & LOCK_MASK;
1344 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1345 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1347 ival = IOCPARM_IVAL(arg);
1348 arg = (caddr_t)&ival;
1351 case KDSKBSTATE: /* set lock key state */
1352 if (*(int *)arg & ~LOCK_MASK) {
1355 sc->sc_state &= ~LOCK_MASK;
1356 sc->sc_state |= *(int *)arg;
1358 /* set LEDs and quit */
1359 return (ukbd_ioctl(kbd, KDSETLED, arg));
1361 case KDSETREPEAT: /* set keyboard repeat rate (new
1363 if (!KBD_HAS_DEVICE(kbd)) {
1366 if (((int *)arg)[1] < 0) {
1369 if (((int *)arg)[0] < 0) {
1372 if (((int *)arg)[0] < 200) /* fastest possible value */
1373 kbd->kb_delay1 = 200;
1375 kbd->kb_delay1 = ((int *)arg)[0];
1376 kbd->kb_delay2 = ((int *)arg)[1];
1379 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1380 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1382 ival = IOCPARM_IVAL(arg);
1383 arg = (caddr_t)&ival;
1386 case KDSETRAD: /* set keyboard repeat rate (old
1388 return (ukbd_set_typematic(kbd, *(int *)arg));
1390 case PIO_KEYMAP: /* set keyboard translation table */
1391 case PIO_KEYMAPENT: /* set keyboard translation table
1393 case PIO_DEADKEYMAP: /* set accent key translation table */
1397 return (genkbd_commonioctl(kbd, cmd, arg));
1403 /* clear the internal state of the keyboard */
1405 ukbd_clear_state(keyboard_t *kbd)
1407 struct ukbd_softc *sc = kbd->kb_data;
1409 if (!mtx_owned(&Giant)) {
1412 mtx_assert(&Giant, MA_OWNED);
1414 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1415 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1417 sc->sc_composed_char = 0;
1418 #ifdef UKBD_EMULATE_ATSCANCODE
1419 sc->sc_buffered_char[0] = 0;
1420 sc->sc_buffered_char[1] = 0;
1422 bzero(&sc->sc_ndata, sizeof(sc->sc_ndata));
1423 bzero(&sc->sc_odata, sizeof(sc->sc_odata));
1424 bzero(&sc->sc_ntime, sizeof(sc->sc_ntime));
1425 bzero(&sc->sc_otime, sizeof(sc->sc_otime));
1428 /* save the internal state, not used */
1430 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1432 mtx_assert(&Giant, MA_OWNED);
1433 return (len == 0) ? 1 : -1;
1436 /* set the internal state, not used */
1438 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1440 mtx_assert(&Giant, MA_OWNED);
1445 ukbd_poll(keyboard_t *kbd, int on)
1447 struct ukbd_softc *sc = kbd->kb_data;
1449 if (!mtx_owned(&Giant)) {
1450 return (0); /* XXX */
1452 mtx_assert(&Giant, MA_OWNED);
1455 sc->sc_flags |= UKBD_FLAG_POLLING;
1457 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1462 /* local functions */
1465 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1467 DPRINTF("leds=0x%02x\n", leds);
1470 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1472 /* start transfer, if not already started */
1474 usb2_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1478 ukbd_set_typematic(keyboard_t *kbd, int code)
1480 static const int delays[] = {250, 500, 750, 1000};
1481 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1482 68, 76, 84, 92, 100, 110, 118, 126,
1483 136, 152, 168, 184, 200, 220, 236, 252,
1484 272, 304, 336, 368, 400, 440, 472, 504};
1489 kbd->kb_delay1 = delays[(code >> 5) & 3];
1490 kbd->kb_delay2 = rates[code & 0x1f];
1494 #ifdef UKBD_EMULATE_ATSCANCODE
1496 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1498 static const int scan[] = {
1500 0x37 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1501 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1502 0x50, 0x51, 0x52, 0x53,
1503 0x46, /* XXX Pause/Break */
1505 /* SUN TYPE 6 USB KEYBOARD */
1506 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
1507 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e,
1511 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
1512 code = scan[code - 89] | SCAN_PREFIX_E0;
1515 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
1516 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
1518 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
1519 code &= ~SCAN_PREFIX_SHIFT;
1521 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1523 if (code & SCAN_PREFIX) {
1524 if (code & SCAN_PREFIX_CTL) {
1526 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
1527 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
1528 } else if (code & SCAN_PREFIX_SHIFT) {
1530 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1531 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1533 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1534 sc->sc_buffered_char[1] = 0;
1536 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1542 #endif /* UKBD_EMULATE_ATSCANCODE */
1544 keyboard_switch_t ukbdsw = {
1545 .probe = &ukbd__probe,
1549 .test_if = &ukbd_test_if,
1550 .enable = &ukbd_enable,
1551 .disable = &ukbd_disable,
1553 .check = &ukbd_check,
1554 .read_char = &ukbd_read_char,
1555 .check_char = &ukbd_check_char,
1556 .ioctl = &ukbd_ioctl,
1558 .clear_state = &ukbd_clear_state,
1559 .get_state = &ukbd_get_state,
1560 .set_state = &ukbd_set_state,
1561 .get_fkeystr = &genkbd_get_fkeystr,
1563 .diag = &genkbd_diag,
1566 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
1569 ukbd_driver_load(module_t mod, int what, void *arg)
1573 kbd_add_driver(&ukbd_kbd_driver);
1576 kbd_delete_driver(&ukbd_kbd_driver);
1582 static devclass_t ukbd_devclass;
1584 static device_method_t ukbd_methods[] = {
1585 DEVMETHOD(device_probe, ukbd_probe),
1586 DEVMETHOD(device_attach, ukbd_attach),
1587 DEVMETHOD(device_detach, ukbd_detach),
1588 DEVMETHOD(device_resume, ukbd_resume),
1592 static driver_t ukbd_driver = {
1594 .methods = ukbd_methods,
1595 .size = sizeof(struct ukbd_softc),
1598 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
1599 MODULE_DEPEND(ukbd, usb, 1, 1, 1);