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 <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/linker_set.h>
60 #include <sys/module.h>
62 #include <sys/mutex.h>
63 #include <sys/condvar.h>
64 #include <sys/sysctl.h>
66 #include <sys/unistd.h>
67 #include <sys/callout.h>
68 #include <sys/malloc.h>
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/usbhid.h>
77 #define USB_DEBUG_VAR ukbd_debug
78 #include <dev/usb/usb_debug.h>
80 #include <dev/usb/quirk/usb_quirk.h>
82 #include <sys/ioccom.h>
83 #include <sys/filio.h>
87 #include <dev/kbd/kbdreg.h>
89 /* the initial key map, accent map and fkey strings */
90 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
91 #define KBD_DFLT_KEYMAP
95 /* the following file must be included after "ukbdmap.h" */
96 #include <dev/kbd/kbdtables.h>
99 static int ukbd_debug = 0;
100 static int ukbd_no_leds = 0;
102 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
103 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
104 &ukbd_debug, 0, "Debug level");
105 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW,
106 &ukbd_no_leds, 0, "Disables setting of keyboard leds");
110 #define UPROTO_BOOT_KEYBOARD 1
112 #define UKBD_EMULATE_ATSCANCODE 1
113 #define UKBD_DRIVER_NAME "ukbd"
114 #define UKBD_NMOD 8 /* units */
115 #define UKBD_NKEYCODE 6 /* units */
116 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */
117 #define UKBD_IN_BUF_FULL (UKBD_IN_BUF_SIZE / 2) /* bytes */
118 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
122 #define MOD_CONTROL_L 0x01
123 #define MOD_CONTROL_R 0x10
124 #define MOD_SHIFT_L 0x02
125 #define MOD_SHIFT_R 0x20
126 #define MOD_ALT_L 0x04
127 #define MOD_ALT_R 0x40
128 #define MOD_WIN_L 0x08
129 #define MOD_WIN_R 0x80
131 uint8_t keycode[UKBD_NKEYCODE];
144 accentmap_t sc_accmap;
145 fkeytab_t sc_fkeymap[UKBD_NFKEY];
146 struct hid_location sc_loc_apple_eject;
147 struct hid_location sc_loc_apple_fn;
148 struct usb_callout sc_callout;
149 struct ukbd_data sc_ndata;
150 struct ukbd_data sc_odata;
152 struct usb_device *sc_udev;
153 struct usb_interface *sc_iface;
154 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
156 uint32_t sc_ntime[UKBD_NKEYCODE];
157 uint32_t sc_otime[UKBD_NKEYCODE];
158 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
160 uint32_t sc_composed_char; /* composed char code, if non-zero */
161 #ifdef UKBD_EMULATE_ATSCANCODE
162 uint32_t sc_buffered_char[2];
164 uint32_t sc_flags; /* flags */
165 #define UKBD_FLAG_COMPOSE 0x0001
166 #define UKBD_FLAG_POLLING 0x0002
167 #define UKBD_FLAG_SET_LEDS 0x0004
168 #define UKBD_FLAG_ATTACHED 0x0010
169 #define UKBD_FLAG_GONE 0x0020
170 #define UKBD_FLAG_APPLE_EJECT 0x0040
171 #define UKBD_FLAG_APPLE_FN 0x0080
172 #define UKBD_FLAG_APPLE_SWAP 0x0100
173 #define UKBD_FLAG_TIMER_RUNNING 0x0200
175 int32_t sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
176 int32_t sc_state; /* shift/lock key state */
177 int32_t sc_accents; /* accent key index (> 0) */
180 uint16_t sc_inputhead;
181 uint16_t sc_inputtail;
183 uint8_t sc_leds; /* store for async led requests */
184 uint8_t sc_iface_index;
190 #define KEY_ERROR 0x01
193 #define KEY_RELEASE 0x400
194 #define KEY_INDEX(c) ((c) & 0xFF)
197 #define SCAN_RELEASE 0x80
198 #define SCAN_PREFIX_E0 0x100
199 #define SCAN_PREFIX_E1 0x200
200 #define SCAN_PREFIX_CTL 0x400
201 #define SCAN_PREFIX_SHIFT 0x800
202 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
203 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
204 #define SCAN_CHAR(c) ((c) & 0x7f)
210 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
211 {MOD_CONTROL_L, 0xe0},
212 {MOD_CONTROL_R, 0xe4},
221 #define NN 0 /* no translation */
223 * Translate USB keycodes to AT keyboard scancodes.
226 * FIXME: Mac USB keyboard generates:
227 * 0x53: keypad NumLock/Clear
234 static const uint8_t ukbd_trtab[256] = {
235 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
236 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
237 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
238 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
239 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
240 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
241 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
242 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
243 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
244 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
245 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
246 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
247 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
248 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
249 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
250 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
251 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */
252 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */
253 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
254 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
255 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
256 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
257 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
258 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
259 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
260 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
261 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
262 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
263 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
264 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
265 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
266 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
270 static void ukbd_timeout(void *);
271 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
272 static int ukbd_set_typematic(keyboard_t *, int);
273 #ifdef UKBD_EMULATE_ATSCANCODE
274 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
276 static uint32_t ukbd_read_char(keyboard_t *, int);
277 static void ukbd_clear_state(keyboard_t *);
278 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
279 static int ukbd_enable(keyboard_t *);
280 static int ukbd_disable(keyboard_t *);
281 static void ukbd_interrupt(struct ukbd_softc *);
283 static device_probe_t ukbd_probe;
284 static device_attach_t ukbd_attach;
285 static device_detach_t ukbd_detach;
286 static device_resume_t ukbd_resume;
289 ukbd_any_key_pressed(struct ukbd_softc *sc)
294 for (j = i = 0; i < UKBD_NKEYCODE; i++)
295 j |= sc->sc_odata.keycode[i];
301 ukbd_start_timer(struct ukbd_softc *sc)
303 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
304 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
308 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
310 mtx_assert(&Giant, MA_OWNED);
312 DPRINTF("0x%02x (%d) %s\n", key, key,
313 (key & KEY_RELEASE) ? "released" : "pressed");
315 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
316 sc->sc_input[sc->sc_inputtail] = key;
318 ++(sc->sc_inputtail);
319 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
320 sc->sc_inputtail = 0;
323 DPRINTF("input buffer is full\n");
328 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
330 DPRINTFN(2, "polling\n");
333 return; /* Only poll if KDB is active */
335 while (sc->sc_inputs == 0) {
337 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
339 /* Delay-optimised support for repetition of keys */
341 if (ukbd_any_key_pressed(sc)) {
342 /* a key is pressed - need timekeeping */
345 /* 1 millisecond has passed */
357 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
361 mtx_assert(&Giant, MA_OWNED);
363 if (sc->sc_inputs == 0) {
364 /* start transfer, if not already started */
365 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
367 if (sc->sc_flags & UKBD_FLAG_POLLING) {
368 ukbd_do_poll(sc, wait);
370 if (sc->sc_inputs == 0) {
373 c = sc->sc_input[sc->sc_inputhead];
375 ++(sc->sc_inputhead);
376 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
377 sc->sc_inputhead = 0;
384 ukbd_interrupt(struct ukbd_softc *sc)
388 uint32_t now = sc->sc_time_ms;
395 if (sc->sc_ndata.keycode[0] == KEY_ERROR) {
398 n_mod = sc->sc_ndata.modifiers;
399 o_mod = sc->sc_odata.modifiers;
400 if (n_mod != o_mod) {
401 for (i = 0; i < UKBD_NMOD; i++) {
402 if ((n_mod & ukbd_mods[i].mask) !=
403 (o_mod & ukbd_mods[i].mask)) {
404 ukbd_put_key(sc, ukbd_mods[i].key |
405 ((n_mod & ukbd_mods[i].mask) ?
406 KEY_PRESS : KEY_RELEASE));
410 /* Check for released keys. */
411 for (i = 0; i < UKBD_NKEYCODE; i++) {
412 key = sc->sc_odata.keycode[i];
416 for (j = 0; j < UKBD_NKEYCODE; j++) {
417 if (sc->sc_ndata.keycode[j] == 0) {
420 if (key == sc->sc_ndata.keycode[j]) {
424 ukbd_put_key(sc, key | KEY_RELEASE);
428 /* Check for pressed keys. */
429 for (i = 0; i < UKBD_NKEYCODE; i++) {
430 key = sc->sc_ndata.keycode[i];
434 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
435 for (j = 0; j < UKBD_NKEYCODE; j++) {
436 if (sc->sc_odata.keycode[j] == 0) {
439 if (key == sc->sc_odata.keycode[j]) {
441 /* key is still pressed */
443 sc->sc_ntime[i] = sc->sc_otime[j];
444 dtime = (sc->sc_otime[j] - now);
446 if (!(dtime & 0x80000000)) {
447 /* time has not elapsed */
450 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
454 ukbd_put_key(sc, key | KEY_PRESS);
457 * If any other key is presently down, force its repeat to be
458 * well in the future (100s). This makes the last key to be
459 * pressed do the autorepeat.
461 for (j = 0; j != UKBD_NKEYCODE; j++) {
463 sc->sc_ntime[j] = now + (100 * 1000);
468 sc->sc_odata = sc->sc_ndata;
470 bcopy(sc->sc_ntime, sc->sc_otime, sizeof(sc->sc_otime));
472 if (sc->sc_inputs == 0) {
475 if (sc->sc_flags & UKBD_FLAG_POLLING) {
478 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
479 KBD_IS_BUSY(&sc->sc_kbd)) {
480 /* let the callback function process the input */
481 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
482 sc->sc_kbd.kb_callback.kc_arg);
484 /* read and discard the input, no one is waiting for it */
486 c = ukbd_read_char(&sc->sc_kbd, 0);
487 } while (c != NOKEY);
494 ukbd_timeout(void *arg)
496 struct ukbd_softc *sc = arg;
498 mtx_assert(&Giant, MA_OWNED);
500 if (!(sc->sc_flags & UKBD_FLAG_POLLING)) {
501 sc->sc_time_ms += 25; /* milliseconds */
505 if (ukbd_any_key_pressed(sc)) {
506 ukbd_start_timer(sc);
508 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
513 ukbd_apple_fn(uint8_t keycode) {
515 case 0x28: return 0x49; /* RETURN -> INSERT */
516 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
517 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
518 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
519 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
520 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
521 default: return keycode;
526 ukbd_apple_swap(uint8_t keycode) {
528 case 0x35: return 0x64;
529 case 0x64: return 0x35;
530 default: return keycode;
535 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
537 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
538 struct usb_page_cache *pc;
546 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
547 pc = usbd_xfer_get_frame(xfer, 0);
549 switch (USB_GET_STATE(xfer)) {
550 case USB_ST_TRANSFERRED:
551 DPRINTF("actlen=%d bytes\n", len);
554 DPRINTF("zero length data\n");
558 if (sc->sc_kbd_id != 0) {
559 /* check and remove HID ID byte */
560 usbd_copy_out(pc, 0, &id, 1);
561 if (id != sc->sc_kbd_id) {
562 DPRINTF("wrong HID ID\n");
571 if (len > sizeof(sc->sc_ndata)) {
572 len = sizeof(sc->sc_ndata);
576 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
577 usbd_copy_out(pc, offset, &sc->sc_ndata, len);
579 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
580 hid_get_data((uint8_t *)&sc->sc_ndata,
581 len, &sc->sc_loc_apple_eject))
586 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
587 hid_get_data((uint8_t *)&sc->sc_ndata,
588 len, &sc->sc_loc_apple_fn))
593 DPRINTF("apple_eject=%u apple_fn=%u\n",
594 apple_eject, apple_fn);
596 if (sc->sc_ndata.modifiers) {
597 DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers);
599 for (i = 0; i < UKBD_NKEYCODE; i++) {
600 if (sc->sc_ndata.keycode[i]) {
601 DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]);
604 #endif /* USB_DEBUG */
607 for (i = 0; i < UKBD_NKEYCODE; i++) {
608 sc->sc_ndata.keycode[i] =
609 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
613 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
614 for (i = 0; i < UKBD_NKEYCODE; i++) {
615 sc->sc_ndata.keycode[i] =
616 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
622 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
623 if (ukbd_any_key_pressed(sc)) {
624 ukbd_start_timer(sc);
630 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
631 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
632 usbd_transfer_submit(xfer);
634 DPRINTF("input queue is full!\n");
639 DPRINTF("error=%s\n", usbd_errstr(error));
641 if (error != USB_ERR_CANCELLED) {
642 /* try to clear stall first */
643 usbd_xfer_set_stall(xfer);
651 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
653 struct usb_device_request req;
654 struct usb_page_cache *pc;
656 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
663 switch (USB_GET_STATE(xfer)) {
664 case USB_ST_TRANSFERRED:
666 if (sc->sc_flags & UKBD_FLAG_SET_LEDS) {
667 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
669 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
670 req.bRequest = UR_SET_REPORT;
671 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
672 req.wIndex[0] = sc->sc_iface_no;
676 /* check if we need to prefix an ID byte */
677 if (sc->sc_led_id != 0) {
679 buf[0] = sc->sc_led_id;
680 buf[1] = sc->sc_leds;
683 buf[0] = sc->sc_leds;
687 pc = usbd_xfer_get_frame(xfer, 0);
688 usbd_copy_in(pc, 0, &req, sizeof(req));
689 pc = usbd_xfer_get_frame(xfer, 1);
690 usbd_copy_in(pc, 0, buf, sizeof(buf));
692 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
693 usbd_xfer_set_frame_len(xfer, 1, req.wLength[0]);
694 usbd_xfer_set_frames(xfer, 2);
695 usbd_transfer_submit(xfer);
700 DPRINTFN(0, "error=%s\n", usbd_errstr(error));
705 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
708 .type = UE_INTERRUPT,
709 .endpoint = UE_ADDR_ANY,
710 .direction = UE_DIR_IN,
711 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
712 .bufsize = 0, /* use wMaxPacketSize */
713 .callback = &ukbd_intr_callback,
718 .endpoint = 0x00, /* Control pipe */
719 .direction = UE_DIR_ANY,
720 .bufsize = sizeof(struct usb_device_request) + 8,
721 .callback = &ukbd_set_leds_callback,
722 .timeout = 1000, /* 1 second */
727 ukbd_probe(device_t dev)
729 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
730 struct usb_attach_arg *uaa = device_get_ivars(dev);
740 if (uaa->usb_mode != USB_MODE_HOST) {
744 if (uaa->info.bInterfaceClass != UICLASS_HID)
747 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
748 (uaa->info.bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)) {
749 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
755 error = usbd_req_get_hid_desc(uaa->device, NULL,
756 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
762 * NOTE: we currently don't support USB mouse and USB keyboard
763 * on the same USB endpoint.
765 if (hid_is_collection(d_ptr, d_len,
766 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
767 /* most likely a mouse */
769 } else if (hid_is_collection(d_ptr, d_len,
770 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
771 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
783 ukbd_attach(device_t dev)
785 struct ukbd_softc *sc = device_get_softc(dev);
786 struct usb_attach_arg *uaa = device_get_ivars(dev);
787 int32_t unit = device_get_unit(dev);
788 keyboard_t *kbd = &sc->sc_kbd;
789 void *hid_ptr = NULL;
795 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
797 kbd->kb_data = (void *)sc;
799 device_set_usb_desc(dev);
801 sc->sc_udev = uaa->device;
802 sc->sc_iface = uaa->iface;
803 sc->sc_iface_index = uaa->info.bIfaceIndex;
804 sc->sc_iface_no = uaa->info.bIfaceNum;
805 sc->sc_mode = K_XLATE;
807 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
809 err = usbd_transfer_setup(uaa->device,
810 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
811 UKBD_N_TRANSFER, sc, &Giant);
814 DPRINTF("error=%s\n", usbd_errstr(err));
817 /* setup default keyboard maps */
819 sc->sc_keymap = key_map;
820 sc->sc_accmap = accent_map;
821 for (n = 0; n < UKBD_NFKEY; n++) {
822 sc->sc_fkeymap[n] = fkey_tab[n];
825 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
826 sc->sc_fkeymap, UKBD_NFKEY);
828 KBD_FOUND_DEVICE(kbd);
830 ukbd_clear_state(kbd);
833 * FIXME: set the initial value for lock keys in "sc_state"
834 * according to the BIOS data?
838 /* figure out if there is an ID byte in the data */
839 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
840 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
844 /* investigate if this is an Apple Keyboard */
845 if (hid_locate(hid_ptr, hid_len,
846 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
847 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
849 if (flags & HIO_VARIABLE)
850 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
851 UKBD_FLAG_APPLE_SWAP;
852 if (hid_locate(hid_ptr, hid_len,
853 HID_USAGE2(0xFFFF, 0x0003),
854 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
856 if (flags & HIO_VARIABLE)
857 sc->sc_flags |= UKBD_FLAG_APPLE_FN |
858 UKBD_FLAG_APPLE_SWAP;
859 if (temp_id != sc->sc_kbd_id) {
860 DPRINTF("HID IDs mismatch\n");
865 * Assume the first HID ID contains the
868 hid_report_size(hid_ptr, hid_len,
869 hid_input, &sc->sc_kbd_id);
872 /* investigate if we need an ID-byte for the leds */
873 hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id);
875 free(hid_ptr, M_TEMP);
878 /* ignore if SETIDLE fails, hence it is not crucial */
879 err = usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
881 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
885 if (kbd_register(kbd) < 0) {
888 KBD_CONFIG_DONE(kbd);
892 #ifdef KBD_INSTALL_CDEV
893 if (kbd_attach(kbd)) {
897 sc->sc_flags |= UKBD_FLAG_ATTACHED;
900 genkbd_diag(kbd, bootverbose);
902 /* lock keyboard mutex */
906 /* start the keyboard */
908 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
911 return (0); /* success */
915 return (ENXIO); /* error */
919 ukbd_detach(device_t dev)
921 struct ukbd_softc *sc = device_get_softc(dev);
926 if (sc->sc_flags & UKBD_FLAG_POLLING) {
927 panic("cannot detach polled keyboard!\n");
929 sc->sc_flags |= UKBD_FLAG_GONE;
931 usb_callout_stop(&sc->sc_callout);
933 ukbd_disable(&sc->sc_kbd);
935 #ifdef KBD_INSTALL_CDEV
936 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
937 error = kbd_detach(&sc->sc_kbd);
939 /* usb attach cannot return an error */
940 device_printf(dev, "WARNING: kbd_detach() "
941 "returned non-zero! (ignored)\n");
945 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
946 error = kbd_unregister(&sc->sc_kbd);
948 /* usb attach cannot return an error */
949 device_printf(dev, "WARNING: kbd_unregister() "
950 "returned non-zero! (ignored)\n");
953 sc->sc_kbd.kb_flags = 0;
955 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
957 usb_callout_drain(&sc->sc_callout);
959 DPRINTF("%s: disconnected\n",
960 device_get_nameunit(dev));
966 ukbd_resume(device_t dev)
968 struct ukbd_softc *sc = device_get_softc(dev);
970 ukbd_clear_state(&sc->sc_kbd);
975 /* early keyboard probe, not supported */
977 ukbd_configure(int flags)
982 /* detect a keyboard, not used */
984 ukbd__probe(int unit, void *arg, int flags)
989 /* reset and initialize the device, not used */
991 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
996 /* test the interface to the device, not used */
998 ukbd_test_if(keyboard_t *kbd)
1003 /* finish using this keyboard, not used */
1005 ukbd_term(keyboard_t *kbd)
1010 /* keyboard interrupt routine, not used */
1012 ukbd_intr(keyboard_t *kbd, void *arg)
1017 /* lock the access to the keyboard, not used */
1019 ukbd_lock(keyboard_t *kbd, int lock)
1025 * Enable the access to the device; until this function is called,
1026 * the client cannot read from the keyboard.
1029 ukbd_enable(keyboard_t *kbd)
1031 if (!mtx_owned(&Giant)) {
1035 retval = ukbd_enable(kbd);
1043 /* disallow the access to the device */
1045 ukbd_disable(keyboard_t *kbd)
1047 if (!mtx_owned(&Giant)) {
1051 retval = ukbd_disable(kbd);
1055 KBD_DEACTIVATE(kbd);
1059 /* check if data is waiting */
1061 ukbd_check(keyboard_t *kbd)
1063 struct ukbd_softc *sc = kbd->kb_data;
1065 if (!KBD_IS_ACTIVE(kbd))
1068 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1069 if (!mtx_owned(&Giant)) {
1073 retval = ukbd_check(kbd);
1077 ukbd_do_poll(sc, 0);
1079 /* XXX the keyboard layer requires Giant */
1080 if (!mtx_owned(&Giant))
1084 #ifdef UKBD_EMULATE_ATSCANCODE
1085 if (sc->sc_buffered_char[0]) {
1089 if (sc->sc_inputs > 0) {
1095 /* check if char is waiting */
1097 ukbd_check_char(keyboard_t *kbd)
1099 struct ukbd_softc *sc = kbd->kb_data;
1101 if (!KBD_IS_ACTIVE(kbd))
1104 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1105 if (!mtx_owned(&Giant)) {
1109 retval = ukbd_check_char(kbd);
1114 /* XXX the keyboard layer requires Giant */
1115 if (!mtx_owned(&Giant))
1119 if ((sc->sc_composed_char > 0) &&
1120 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1123 return (ukbd_check(kbd));
1127 /* read one byte from the keyboard if it's allowed */
1129 ukbd_read(keyboard_t *kbd, int wait)
1131 struct ukbd_softc *sc = kbd->kb_data;
1134 #ifdef UKBD_EMULATE_ATSCANCODE
1139 if (!KBD_IS_ACTIVE(kbd))
1142 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1143 if (!mtx_owned(&Giant)) {
1147 retval = ukbd_read(kbd, wait);
1152 /* XXX the keyboard layer requires Giant */
1153 if (!mtx_owned(&Giant))
1157 #ifdef UKBD_EMULATE_ATSCANCODE
1158 if (sc->sc_buffered_char[0]) {
1159 scancode = sc->sc_buffered_char[0];
1160 if (scancode & SCAN_PREFIX) {
1161 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1162 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1164 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1165 sc->sc_buffered_char[1] = 0;
1168 #endif /* UKBD_EMULATE_ATSCANCODE */
1171 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1172 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1177 #ifdef UKBD_EMULATE_ATSCANCODE
1178 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1179 if (keycode == NN) {
1182 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1183 (usbcode & KEY_RELEASE)));
1184 #else /* !UKBD_EMULATE_ATSCANCODE */
1186 #endif /* UKBD_EMULATE_ATSCANCODE */
1189 /* read char from the keyboard */
1191 ukbd_read_char(keyboard_t *kbd, int wait)
1193 struct ukbd_softc *sc = kbd->kb_data;
1198 #ifdef UKBD_EMULATE_ATSCANCODE
1203 if (!KBD_IS_ACTIVE(kbd))
1206 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1207 if (!mtx_owned(&Giant)) {
1211 retval = ukbd_read_char(kbd, wait);
1216 /* XXX the keyboard layer requires Giant */
1217 if (!mtx_owned(&Giant))
1223 /* do we have a composed char to return ? */
1225 if ((sc->sc_composed_char > 0) &&
1226 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1228 action = sc->sc_composed_char;
1229 sc->sc_composed_char = 0;
1231 if (action > 0xFF) {
1236 #ifdef UKBD_EMULATE_ATSCANCODE
1238 /* do we have a pending raw scan code? */
1240 if (sc->sc_mode == K_RAW) {
1241 scancode = sc->sc_buffered_char[0];
1243 if (scancode & SCAN_PREFIX) {
1244 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1245 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1247 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1248 sc->sc_buffered_char[1] = 0;
1252 #endif /* UKBD_EMULATE_ATSCANCODE */
1254 /* see if there is something in the keyboard port */
1256 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1257 if (usbcode == -1) {
1262 #ifdef UKBD_EMULATE_ATSCANCODE
1263 /* USB key index -> key code -> AT scan code */
1264 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1265 if (keycode == NN) {
1268 /* return an AT scan code for the K_RAW mode */
1269 if (sc->sc_mode == K_RAW) {
1270 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1271 (usbcode & KEY_RELEASE)));
1273 #else /* !UKBD_EMULATE_ATSCANCODE */
1275 /* return the byte as is for the K_RAW mode */
1276 if (sc->sc_mode == K_RAW) {
1279 /* USB key index -> key code */
1280 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1281 if (keycode == NN) {
1284 #endif /* UKBD_EMULATE_ATSCANCODE */
1287 case 0x38: /* left alt (compose key) */
1288 if (usbcode & KEY_RELEASE) {
1289 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1290 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1292 if (sc->sc_composed_char > 0xFF) {
1293 sc->sc_composed_char = 0;
1297 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1298 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1299 sc->sc_composed_char = 0;
1303 /* XXX: I don't like these... */
1304 case 0x5c: /* print screen */
1305 if (sc->sc_flags & ALTS) {
1306 keycode = 0x54; /* sysrq */
1309 case 0x68: /* pause/break */
1310 if (sc->sc_flags & CTLS) {
1311 keycode = 0x6c; /* break */
1316 /* return the key code in the K_CODE mode */
1317 if (usbcode & KEY_RELEASE) {
1318 keycode |= SCAN_RELEASE;
1320 if (sc->sc_mode == K_CODE) {
1323 /* compose a character code */
1324 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1326 /* key pressed, process it */
1329 case 0x49: /* keypad 7,8,9 */
1330 sc->sc_composed_char *= 10;
1331 sc->sc_composed_char += keycode - 0x40;
1332 goto check_composed;
1336 case 0x4D: /* keypad 4,5,6 */
1337 sc->sc_composed_char *= 10;
1338 sc->sc_composed_char += keycode - 0x47;
1339 goto check_composed;
1343 case 0x51: /* keypad 1,2,3 */
1344 sc->sc_composed_char *= 10;
1345 sc->sc_composed_char += keycode - 0x4E;
1346 goto check_composed;
1348 case 0x52: /* keypad 0 */
1349 sc->sc_composed_char *= 10;
1350 goto check_composed;
1352 /* key released, no interest here */
1353 case SCAN_RELEASE | 0x47:
1354 case SCAN_RELEASE | 0x48:
1355 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1356 case SCAN_RELEASE | 0x4B:
1357 case SCAN_RELEASE | 0x4C:
1358 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1359 case SCAN_RELEASE | 0x4F:
1360 case SCAN_RELEASE | 0x50:
1361 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1362 case SCAN_RELEASE | 0x52: /* keypad 0 */
1365 case 0x38: /* left alt key */
1369 if (sc->sc_composed_char > 0) {
1370 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1371 sc->sc_composed_char = 0;
1377 /* keycode to key action */
1378 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1379 (keycode & SCAN_RELEASE),
1380 &sc->sc_state, &sc->sc_accents);
1381 if (action == NOKEY) {
1388 if (sc->sc_composed_char <= 0xFF) {
1395 /* some useful control functions */
1397 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1399 /* translate LED_XXX bits into the device specific bits */
1400 static const uint8_t ledmap[8] = {
1401 0, 2, 1, 3, 4, 6, 5, 7,
1403 struct ukbd_softc *sc = kbd->kb_data;
1406 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1407 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1411 if (!mtx_owned(&Giant)) {
1413 * XXX big problem: If scroll lock is pressed and "printf()"
1414 * is called, the CPU will get here, to un-scroll lock the
1415 * keyboard. But if "printf()" acquires the "Giant" lock,
1416 * there will be a locking order reversal problem, so the
1417 * keyboard system must get out of "Giant" first, before the
1418 * CPU can proceed here ...
1424 case KDGKBMODE: /* get keyboard mode */
1425 *(int *)arg = sc->sc_mode;
1427 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1428 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1430 ival = IOCPARM_IVAL(arg);
1431 arg = (caddr_t)&ival;
1434 case KDSKBMODE: /* set keyboard mode */
1435 switch (*(int *)arg) {
1437 if (sc->sc_mode != K_XLATE) {
1438 /* make lock key state and LED state match */
1439 sc->sc_state &= ~LOCK_MASK;
1440 sc->sc_state |= KBD_LED_VAL(kbd);
1445 if (sc->sc_mode != *(int *)arg) {
1446 ukbd_clear_state(kbd);
1447 sc->sc_mode = *(int *)arg;
1455 case KDGETLED: /* get keyboard LED */
1456 *(int *)arg = KBD_LED_VAL(kbd);
1458 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1459 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1461 ival = IOCPARM_IVAL(arg);
1462 arg = (caddr_t)&ival;
1465 case KDSETLED: /* set keyboard LED */
1466 /* NOTE: lock key state in "sc_state" won't be changed */
1467 if (*(int *)arg & ~LOCK_MASK) {
1471 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1472 if (sc->sc_mode == K_XLATE &&
1473 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1479 if (KBD_HAS_DEVICE(kbd)) {
1480 ukbd_set_leds(sc, ledmap[i & LED_MASK]);
1482 KBD_LED_VAL(kbd) = *(int *)arg;
1484 case KDGKBSTATE: /* get lock key state */
1485 *(int *)arg = sc->sc_state & LOCK_MASK;
1487 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1488 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1490 ival = IOCPARM_IVAL(arg);
1491 arg = (caddr_t)&ival;
1494 case KDSKBSTATE: /* set lock key state */
1495 if (*(int *)arg & ~LOCK_MASK) {
1498 sc->sc_state &= ~LOCK_MASK;
1499 sc->sc_state |= *(int *)arg;
1501 /* set LEDs and quit */
1502 return (ukbd_ioctl(kbd, KDSETLED, arg));
1504 case KDSETREPEAT: /* set keyboard repeat rate (new
1506 if (!KBD_HAS_DEVICE(kbd)) {
1509 if (((int *)arg)[1] < 0) {
1512 if (((int *)arg)[0] < 0) {
1515 if (((int *)arg)[0] < 200) /* fastest possible value */
1516 kbd->kb_delay1 = 200;
1518 kbd->kb_delay1 = ((int *)arg)[0];
1519 kbd->kb_delay2 = ((int *)arg)[1];
1522 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1523 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1525 ival = IOCPARM_IVAL(arg);
1526 arg = (caddr_t)&ival;
1529 case KDSETRAD: /* set keyboard repeat rate (old
1531 return (ukbd_set_typematic(kbd, *(int *)arg));
1533 case PIO_KEYMAP: /* set keyboard translation table */
1534 case PIO_KEYMAPENT: /* set keyboard translation table
1536 case PIO_DEADKEYMAP: /* set accent key translation table */
1540 return (genkbd_commonioctl(kbd, cmd, arg));
1546 /* clear the internal state of the keyboard */
1548 ukbd_clear_state(keyboard_t *kbd)
1550 struct ukbd_softc *sc = kbd->kb_data;
1552 if (!mtx_owned(&Giant)) {
1556 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1557 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1559 sc->sc_composed_char = 0;
1560 #ifdef UKBD_EMULATE_ATSCANCODE
1561 sc->sc_buffered_char[0] = 0;
1562 sc->sc_buffered_char[1] = 0;
1564 bzero(&sc->sc_ndata, sizeof(sc->sc_ndata));
1565 bzero(&sc->sc_odata, sizeof(sc->sc_odata));
1566 bzero(&sc->sc_ntime, sizeof(sc->sc_ntime));
1567 bzero(&sc->sc_otime, sizeof(sc->sc_otime));
1570 /* save the internal state, not used */
1572 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1574 return (len == 0) ? 1 : -1;
1577 /* set the internal state, not used */
1579 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1585 ukbd_poll(keyboard_t *kbd, int on)
1587 struct ukbd_softc *sc = kbd->kb_data;
1589 if (!mtx_owned(&Giant)) {
1593 retval = ukbd_poll(kbd, on);
1599 sc->sc_flags |= UKBD_FLAG_POLLING;
1601 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1606 /* local functions */
1609 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1611 DPRINTF("leds=0x%02x\n", leds);
1614 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1616 /* start transfer, if not already started */
1618 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1622 ukbd_set_typematic(keyboard_t *kbd, int code)
1624 static const int delays[] = {250, 500, 750, 1000};
1625 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1626 68, 76, 84, 92, 100, 110, 118, 126,
1627 136, 152, 168, 184, 200, 220, 236, 252,
1628 272, 304, 336, 368, 400, 440, 472, 504};
1633 kbd->kb_delay1 = delays[(code >> 5) & 3];
1634 kbd->kb_delay2 = rates[code & 0x1f];
1638 #ifdef UKBD_EMULATE_ATSCANCODE
1640 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1642 static const int scan[] = {
1648 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1658 0x151, /* PageDown */
1661 0x146, /* XXX Pause/Break */
1662 0x15b, /* Win_L(Super_L) */
1663 0x15c, /* Win_R(Super_R) */
1664 0x15d, /* Application(Menu) */
1666 /* SUN TYPE 6 USB KEYBOARD */
1667 0x168, /* Sun Type 6 Help */
1668 0x15e, /* Sun Type 6 Stop */
1670 0x15f, /* Sun Type 6 Again */
1671 0x160, /* Sun Type 6 Props */
1672 0x161, /* Sun Type 6 Undo */
1673 0x162, /* Sun Type 6 Front */
1674 0x163, /* Sun Type 6 Copy */
1675 0x164, /* Sun Type 6 Open */
1676 0x165, /* Sun Type 6 Paste */
1677 0x166, /* Sun Type 6 Find */
1678 0x167, /* Sun Type 6 Cut */
1679 0x125, /* Sun Type 6 Mute */
1681 0x11f, /* Sun Type 6 VolumeDown */
1682 0x11e, /* Sun Type 6 VolumeUp */
1683 0x120, /* Sun Type 6 PowerDown */
1685 /* Japanese 106/109 keyboard */
1686 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
1687 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
1688 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
1689 0x79, /* Keyboard Intl' 4 (Henkan) */
1690 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
1691 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
1694 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
1695 code = scan[code - 89];
1698 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
1699 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
1701 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
1702 code &= ~SCAN_PREFIX_SHIFT;
1704 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1706 if (code & SCAN_PREFIX) {
1707 if (code & SCAN_PREFIX_CTL) {
1709 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
1710 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
1711 } else if (code & SCAN_PREFIX_SHIFT) {
1713 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1714 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1716 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1717 sc->sc_buffered_char[1] = 0;
1719 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1725 #endif /* UKBD_EMULATE_ATSCANCODE */
1727 static keyboard_switch_t ukbdsw = {
1728 .probe = &ukbd__probe,
1732 .test_if = &ukbd_test_if,
1733 .enable = &ukbd_enable,
1734 .disable = &ukbd_disable,
1736 .check = &ukbd_check,
1737 .read_char = &ukbd_read_char,
1738 .check_char = &ukbd_check_char,
1739 .ioctl = &ukbd_ioctl,
1741 .clear_state = &ukbd_clear_state,
1742 .get_state = &ukbd_get_state,
1743 .set_state = &ukbd_set_state,
1744 .get_fkeystr = &genkbd_get_fkeystr,
1746 .diag = &genkbd_diag,
1749 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
1752 ukbd_driver_load(module_t mod, int what, void *arg)
1756 kbd_add_driver(&ukbd_kbd_driver);
1759 kbd_delete_driver(&ukbd_kbd_driver);
1765 static devclass_t ukbd_devclass;
1767 static device_method_t ukbd_methods[] = {
1768 DEVMETHOD(device_probe, ukbd_probe),
1769 DEVMETHOD(device_attach, ukbd_attach),
1770 DEVMETHOD(device_detach, ukbd_detach),
1771 DEVMETHOD(device_resume, ukbd_resume),
1775 static driver_t ukbd_driver = {
1777 .methods = ukbd_methods,
1778 .size = sizeof(struct ukbd_softc),
1781 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
1782 MODULE_DEPEND(ukbd, usb, 1, 1, 1);