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>
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
74 #include <dev/usb/usbhid.h>
76 #define USB_DEBUG_VAR ukbd_debug
77 #include <dev/usb/usb_debug.h>
79 #include <dev/usb/quirk/usb_quirk.h>
81 #include <sys/ioccom.h>
82 #include <sys/filio.h>
86 #include <dev/kbd/kbdreg.h>
88 /* the initial key map, accent map and fkey strings */
89 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
90 #define KBD_DFLT_KEYMAP
94 /* the following file must be included after "ukbdmap.h" */
95 #include <dev/kbd/kbdtables.h>
98 static int ukbd_debug = 0;
99 static int ukbd_no_leds = 0;
101 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
102 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
103 &ukbd_debug, 0, "Debug level");
104 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW,
105 &ukbd_no_leds, 0, "Disables setting of keyboard leds");
109 #define UPROTO_BOOT_KEYBOARD 1
111 #define UKBD_EMULATE_ATSCANCODE 1
112 #define UKBD_DRIVER_NAME "ukbd"
113 #define UKBD_NMOD 8 /* units */
114 #define UKBD_NKEYCODE 6 /* units */
115 #define UKBD_IN_BUF_SIZE (2*(UKBD_NMOD + (2*UKBD_NKEYCODE))) /* bytes */
116 #define UKBD_IN_BUF_FULL (UKBD_IN_BUF_SIZE / 2) /* bytes */
117 #define UKBD_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
121 #define MOD_CONTROL_L 0x01
122 #define MOD_CONTROL_R 0x10
123 #define MOD_SHIFT_L 0x02
124 #define MOD_SHIFT_R 0x20
125 #define MOD_ALT_L 0x04
126 #define MOD_ALT_R 0x40
127 #define MOD_WIN_L 0x08
128 #define MOD_WIN_R 0x80
130 uint8_t keycode[UKBD_NKEYCODE];
143 accentmap_t sc_accmap;
144 fkeytab_t sc_fkeymap[UKBD_NFKEY];
145 struct hid_location sc_loc_apple_eject;
146 struct hid_location sc_loc_apple_fn;
147 struct usb_callout sc_callout;
148 struct ukbd_data sc_ndata;
149 struct ukbd_data sc_odata;
151 struct usb_device *sc_udev;
152 struct usb_interface *sc_iface;
153 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
155 uint32_t sc_ntime[UKBD_NKEYCODE];
156 uint32_t sc_otime[UKBD_NKEYCODE];
157 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
159 uint32_t sc_composed_char; /* composed char code, if non-zero */
160 #ifdef UKBD_EMULATE_ATSCANCODE
161 uint32_t sc_buffered_char[2];
163 uint32_t sc_flags; /* flags */
164 #define UKBD_FLAG_COMPOSE 0x0001
165 #define UKBD_FLAG_POLLING 0x0002
166 #define UKBD_FLAG_SET_LEDS 0x0004
167 #define UKBD_FLAG_ATTACHED 0x0010
168 #define UKBD_FLAG_GONE 0x0020
169 #define UKBD_FLAG_APPLE_EJECT 0x0040
170 #define UKBD_FLAG_APPLE_FN 0x0080
171 #define UKBD_FLAG_APPLE_SWAP 0x0100
172 #define UKBD_FLAG_TIMER_RUNNING 0x0200
174 int32_t sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
175 int32_t sc_state; /* shift/lock key state */
176 int32_t sc_accents; /* accent key index (> 0) */
179 uint16_t sc_inputhead;
180 uint16_t sc_inputtail;
182 uint8_t sc_leds; /* store for async led requests */
183 uint8_t sc_iface_index;
189 #define KEY_ERROR 0x01
192 #define KEY_RELEASE 0x400
193 #define KEY_INDEX(c) ((c) & 0xFF)
196 #define SCAN_RELEASE 0x80
197 #define SCAN_PREFIX_E0 0x100
198 #define SCAN_PREFIX_E1 0x200
199 #define SCAN_PREFIX_CTL 0x400
200 #define SCAN_PREFIX_SHIFT 0x800
201 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
202 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
203 #define SCAN_CHAR(c) ((c) & 0x7f)
209 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
210 {MOD_CONTROL_L, 0xe0},
211 {MOD_CONTROL_R, 0xe4},
220 #define NN 0 /* no translation */
222 * Translate USB keycodes to AT keyboard scancodes.
225 * FIXME: Mac USB keyboard generates:
226 * 0x53: keypad NumLock/Clear
233 static const uint8_t ukbd_trtab[256] = {
234 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
235 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
236 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
237 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
238 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
239 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
240 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
241 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
242 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
243 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
244 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
245 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
246 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
247 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
248 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
249 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
250 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */
251 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */
252 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
253 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
254 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
255 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
256 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
257 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
258 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
259 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
260 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
261 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
262 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
263 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
264 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
265 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
269 static void ukbd_timeout(void *);
270 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
271 static int ukbd_set_typematic(keyboard_t *, int);
272 #ifdef UKBD_EMULATE_ATSCANCODE
273 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
275 static uint32_t ukbd_read_char(keyboard_t *, int);
276 static void ukbd_clear_state(keyboard_t *);
277 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
278 static int ukbd_enable(keyboard_t *);
279 static int ukbd_disable(keyboard_t *);
280 static void ukbd_interrupt(struct ukbd_softc *);
282 static device_probe_t ukbd_probe;
283 static device_attach_t ukbd_attach;
284 static device_detach_t ukbd_detach;
285 static device_resume_t ukbd_resume;
288 ukbd_any_key_pressed(struct ukbd_softc *sc)
293 for (j = i = 0; i < UKBD_NKEYCODE; i++)
294 j |= sc->sc_odata.keycode[i];
300 ukbd_start_timer(struct ukbd_softc *sc)
302 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
303 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
307 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
309 mtx_assert(&Giant, MA_OWNED);
311 DPRINTF("0x%02x (%d) %s\n", key, key,
312 (key & KEY_RELEASE) ? "released" : "pressed");
314 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
315 sc->sc_input[sc->sc_inputtail] = key;
317 ++(sc->sc_inputtail);
318 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
319 sc->sc_inputtail = 0;
322 DPRINTF("input buffer is full\n");
327 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
329 DPRINTFN(2, "polling\n");
331 while (sc->sc_inputs == 0) {
333 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
335 /* Delay-optimised support for repetition of keys */
337 if (ukbd_any_key_pressed(sc)) {
338 /* a key is pressed - need timekeeping */
341 /* 1 millisecond has passed */
353 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
357 mtx_assert(&Giant, MA_OWNED);
359 if (sc->sc_inputs == 0) {
360 /* start transfer, if not already started */
361 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
363 if (sc->sc_flags & UKBD_FLAG_POLLING) {
364 ukbd_do_poll(sc, wait);
366 if (sc->sc_inputs == 0) {
369 c = sc->sc_input[sc->sc_inputhead];
371 ++(sc->sc_inputhead);
372 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
373 sc->sc_inputhead = 0;
380 ukbd_interrupt(struct ukbd_softc *sc)
384 uint32_t now = sc->sc_time_ms;
391 if (sc->sc_ndata.keycode[0] == KEY_ERROR) {
394 n_mod = sc->sc_ndata.modifiers;
395 o_mod = sc->sc_odata.modifiers;
396 if (n_mod != o_mod) {
397 for (i = 0; i < UKBD_NMOD; i++) {
398 if ((n_mod & ukbd_mods[i].mask) !=
399 (o_mod & ukbd_mods[i].mask)) {
400 ukbd_put_key(sc, ukbd_mods[i].key |
401 ((n_mod & ukbd_mods[i].mask) ?
402 KEY_PRESS : KEY_RELEASE));
406 /* Check for released keys. */
407 for (i = 0; i < UKBD_NKEYCODE; i++) {
408 key = sc->sc_odata.keycode[i];
412 for (j = 0; j < UKBD_NKEYCODE; j++) {
413 if (sc->sc_ndata.keycode[j] == 0) {
416 if (key == sc->sc_ndata.keycode[j]) {
420 ukbd_put_key(sc, key | KEY_RELEASE);
424 /* Check for pressed keys. */
425 for (i = 0; i < UKBD_NKEYCODE; i++) {
426 key = sc->sc_ndata.keycode[i];
430 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
431 for (j = 0; j < UKBD_NKEYCODE; j++) {
432 if (sc->sc_odata.keycode[j] == 0) {
435 if (key == sc->sc_odata.keycode[j]) {
437 /* key is still pressed */
439 sc->sc_ntime[i] = sc->sc_otime[j];
440 dtime = (sc->sc_otime[j] - now);
442 if (!(dtime & 0x80000000)) {
443 /* time has not elapsed */
446 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
450 ukbd_put_key(sc, key | KEY_PRESS);
453 * If any other key is presently down, force its repeat to be
454 * well in the future (100s). This makes the last key to be
455 * pressed do the autorepeat.
457 for (j = 0; j != UKBD_NKEYCODE; j++) {
459 sc->sc_ntime[j] = now + (100 * 1000);
464 sc->sc_odata = sc->sc_ndata;
466 bcopy(sc->sc_ntime, sc->sc_otime, sizeof(sc->sc_otime));
468 if (sc->sc_inputs == 0) {
471 if (sc->sc_flags & UKBD_FLAG_POLLING) {
474 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
475 KBD_IS_BUSY(&sc->sc_kbd)) {
476 /* let the callback function process the input */
477 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
478 sc->sc_kbd.kb_callback.kc_arg);
480 /* read and discard the input, no one is waiting for it */
482 c = ukbd_read_char(&sc->sc_kbd, 0);
483 } while (c != NOKEY);
490 ukbd_timeout(void *arg)
492 struct ukbd_softc *sc = arg;
494 mtx_assert(&Giant, MA_OWNED);
496 if (!(sc->sc_flags & UKBD_FLAG_POLLING)) {
497 sc->sc_time_ms += 25; /* milliseconds */
501 if (ukbd_any_key_pressed(sc)) {
502 ukbd_start_timer(sc);
504 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
509 ukbd_apple_fn(uint8_t keycode) {
511 case 0x28: return 0x49; /* RETURN -> INSERT */
512 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
513 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
514 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
515 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
516 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
517 default: return keycode;
522 ukbd_apple_swap(uint8_t keycode) {
524 case 0x35: return 0x64;
525 case 0x64: return 0x35;
526 default: return keycode;
531 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
533 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
534 struct usb_page_cache *pc;
542 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
543 pc = usbd_xfer_get_frame(xfer, 0);
545 switch (USB_GET_STATE(xfer)) {
546 case USB_ST_TRANSFERRED:
547 DPRINTF("actlen=%d bytes\n", len);
550 DPRINTF("zero length data\n");
554 if (sc->sc_kbd_id != 0) {
555 /* check and remove HID ID byte */
556 usbd_copy_out(pc, 0, &id, 1);
557 if (id != sc->sc_kbd_id) {
558 DPRINTF("wrong HID ID\n");
567 if (len > sizeof(sc->sc_ndata)) {
568 len = sizeof(sc->sc_ndata);
572 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
573 usbd_copy_out(pc, offset, &sc->sc_ndata, len);
575 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
576 hid_get_data((uint8_t *)&sc->sc_ndata,
577 len, &sc->sc_loc_apple_eject))
582 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
583 hid_get_data((uint8_t *)&sc->sc_ndata,
584 len, &sc->sc_loc_apple_fn))
589 DPRINTF("apple_eject=%u apple_fn=%u\n",
590 apple_eject, apple_fn);
592 if (sc->sc_ndata.modifiers) {
593 DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers);
595 for (i = 0; i < UKBD_NKEYCODE; i++) {
596 if (sc->sc_ndata.keycode[i]) {
597 DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]);
600 #endif /* USB_DEBUG */
603 for (i = 0; i < UKBD_NKEYCODE; i++) {
604 sc->sc_ndata.keycode[i] =
605 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
609 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
610 for (i = 0; i < UKBD_NKEYCODE; i++) {
611 sc->sc_ndata.keycode[i] =
612 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
618 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
619 if (ukbd_any_key_pressed(sc)) {
620 ukbd_start_timer(sc);
626 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
627 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
628 usbd_transfer_submit(xfer);
630 DPRINTF("input queue is full!\n");
635 DPRINTF("error=%s\n", usbd_errstr(error));
637 if (error != USB_ERR_CANCELLED) {
638 /* try to clear stall first */
639 usbd_xfer_set_stall(xfer);
647 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
649 struct usb_device_request req;
650 struct usb_page_cache *pc;
652 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
659 switch (USB_GET_STATE(xfer)) {
660 case USB_ST_TRANSFERRED:
662 if (sc->sc_flags & UKBD_FLAG_SET_LEDS) {
663 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
665 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
666 req.bRequest = UR_SET_REPORT;
667 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
668 req.wIndex[0] = sc->sc_iface_no;
672 /* check if we need to prefix an ID byte */
673 if (sc->sc_led_id != 0) {
675 buf[0] = sc->sc_led_id;
676 buf[1] = sc->sc_leds;
679 buf[0] = sc->sc_leds;
683 pc = usbd_xfer_get_frame(xfer, 0);
684 usbd_copy_in(pc, 0, &req, sizeof(req));
685 pc = usbd_xfer_get_frame(xfer, 1);
686 usbd_copy_in(pc, 0, buf, sizeof(buf));
688 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
689 usbd_xfer_set_frame_len(xfer, 1, req.wLength[0]);
690 usbd_xfer_set_frames(xfer, 2);
691 usbd_transfer_submit(xfer);
696 DPRINTFN(0, "error=%s\n", usbd_errstr(error));
701 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
704 .type = UE_INTERRUPT,
705 .endpoint = UE_ADDR_ANY,
706 .direction = UE_DIR_IN,
707 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
708 .bufsize = 0, /* use wMaxPacketSize */
709 .callback = &ukbd_intr_callback,
714 .endpoint = 0x00, /* Control pipe */
715 .direction = UE_DIR_ANY,
716 .bufsize = sizeof(struct usb_device_request) + 8,
717 .callback = &ukbd_set_leds_callback,
718 .timeout = 1000, /* 1 second */
723 ukbd_probe(device_t dev)
725 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
726 struct usb_attach_arg *uaa = device_get_ivars(dev);
736 if (uaa->usb_mode != USB_MODE_HOST) {
740 if (uaa->info.bInterfaceClass != UICLASS_HID)
743 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
744 (uaa->info.bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)) {
745 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
751 error = usbd_req_get_hid_desc(uaa->device, NULL,
752 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
758 * NOTE: we currently don't support USB mouse and USB keyboard
759 * on the same USB endpoint.
761 if (hid_is_collection(d_ptr, d_len,
762 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
763 /* most likely a mouse */
765 } else if (hid_is_collection(d_ptr, d_len,
766 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
767 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
779 ukbd_attach(device_t dev)
781 struct ukbd_softc *sc = device_get_softc(dev);
782 struct usb_attach_arg *uaa = device_get_ivars(dev);
783 int32_t unit = device_get_unit(dev);
784 keyboard_t *kbd = &sc->sc_kbd;
785 void *hid_ptr = NULL;
791 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
793 kbd->kb_data = (void *)sc;
795 device_set_usb_desc(dev);
797 sc->sc_udev = uaa->device;
798 sc->sc_iface = uaa->iface;
799 sc->sc_iface_index = uaa->info.bIfaceIndex;
800 sc->sc_iface_no = uaa->info.bIfaceNum;
801 sc->sc_mode = K_XLATE;
803 usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
805 err = usbd_transfer_setup(uaa->device,
806 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
807 UKBD_N_TRANSFER, sc, &Giant);
810 DPRINTF("error=%s\n", usbd_errstr(err));
813 /* setup default keyboard maps */
815 sc->sc_keymap = key_map;
816 sc->sc_accmap = accent_map;
817 for (n = 0; n < UKBD_NFKEY; n++) {
818 sc->sc_fkeymap[n] = fkey_tab[n];
821 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
822 sc->sc_fkeymap, UKBD_NFKEY);
824 KBD_FOUND_DEVICE(kbd);
826 ukbd_clear_state(kbd);
829 * FIXME: set the initial value for lock keys in "sc_state"
830 * according to the BIOS data?
834 /* figure out if there is an ID byte in the data */
835 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
836 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
840 /* investigate if this is an Apple Keyboard */
841 if (hid_locate(hid_ptr, hid_len,
842 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
843 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
845 if (flags & HIO_VARIABLE)
846 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
847 UKBD_FLAG_APPLE_SWAP;
848 if (hid_locate(hid_ptr, hid_len,
849 HID_USAGE2(0xFFFF, 0x0003),
850 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
852 if (flags & HIO_VARIABLE)
853 sc->sc_flags |= UKBD_FLAG_APPLE_FN |
854 UKBD_FLAG_APPLE_SWAP;
855 if (temp_id != sc->sc_kbd_id) {
856 DPRINTF("HID IDs mismatch\n");
861 * Assume the first HID ID contains the
864 hid_report_size(hid_ptr, hid_len,
865 hid_input, &sc->sc_kbd_id);
868 /* investigate if we need an ID-byte for the leds */
869 hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id);
871 free(hid_ptr, M_TEMP);
874 /* ignore if SETIDLE fails, hence it is not crucial */
875 err = usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
877 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
881 if (kbd_register(kbd) < 0) {
884 KBD_CONFIG_DONE(kbd);
888 #ifdef KBD_INSTALL_CDEV
889 if (kbd_attach(kbd)) {
893 sc->sc_flags |= UKBD_FLAG_ATTACHED;
896 genkbd_diag(kbd, bootverbose);
898 /* lock keyboard mutex */
902 /* start the keyboard */
904 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
907 return (0); /* success */
911 return (ENXIO); /* error */
915 ukbd_detach(device_t dev)
917 struct ukbd_softc *sc = device_get_softc(dev);
922 if (sc->sc_flags & UKBD_FLAG_POLLING) {
923 panic("cannot detach polled keyboard!\n");
925 sc->sc_flags |= UKBD_FLAG_GONE;
927 usb_callout_stop(&sc->sc_callout);
929 ukbd_disable(&sc->sc_kbd);
931 #ifdef KBD_INSTALL_CDEV
932 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
933 error = kbd_detach(&sc->sc_kbd);
935 /* usb attach cannot return an error */
936 device_printf(dev, "WARNING: kbd_detach() "
937 "returned non-zero! (ignored)\n");
941 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
942 error = kbd_unregister(&sc->sc_kbd);
944 /* usb attach cannot return an error */
945 device_printf(dev, "WARNING: kbd_unregister() "
946 "returned non-zero! (ignored)\n");
949 sc->sc_kbd.kb_flags = 0;
951 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
953 usb_callout_drain(&sc->sc_callout);
955 DPRINTF("%s: disconnected\n",
956 device_get_nameunit(dev));
962 ukbd_resume(device_t dev)
964 struct ukbd_softc *sc = device_get_softc(dev);
966 ukbd_clear_state(&sc->sc_kbd);
971 /* early keyboard probe, not supported */
973 ukbd_configure(int flags)
978 /* detect a keyboard, not used */
980 ukbd__probe(int unit, void *arg, int flags)
985 /* reset and initialize the device, not used */
987 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
992 /* test the interface to the device, not used */
994 ukbd_test_if(keyboard_t *kbd)
999 /* finish using this keyboard, not used */
1001 ukbd_term(keyboard_t *kbd)
1006 /* keyboard interrupt routine, not used */
1008 ukbd_intr(keyboard_t *kbd, void *arg)
1013 /* lock the access to the keyboard, not used */
1015 ukbd_lock(keyboard_t *kbd, int lock)
1021 * Enable the access to the device; until this function is called,
1022 * the client cannot read from the keyboard.
1025 ukbd_enable(keyboard_t *kbd)
1027 if (!mtx_owned(&Giant)) {
1031 retval = ukbd_enable(kbd);
1039 /* disallow the access to the device */
1041 ukbd_disable(keyboard_t *kbd)
1043 if (!mtx_owned(&Giant)) {
1047 retval = ukbd_disable(kbd);
1051 KBD_DEACTIVATE(kbd);
1055 /* check if data is waiting */
1057 ukbd_check(keyboard_t *kbd)
1059 struct ukbd_softc *sc = kbd->kb_data;
1061 if (!KBD_IS_ACTIVE(kbd))
1064 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1065 if (!mtx_owned(&Giant)) {
1069 retval = ukbd_check(kbd);
1073 ukbd_do_poll(sc, 0);
1075 /* XXX the keyboard layer requires Giant */
1076 if (!mtx_owned(&Giant))
1080 #ifdef UKBD_EMULATE_ATSCANCODE
1081 if (sc->sc_buffered_char[0]) {
1085 if (sc->sc_inputs > 0) {
1091 /* check if char is waiting */
1093 ukbd_check_char(keyboard_t *kbd)
1095 struct ukbd_softc *sc = kbd->kb_data;
1097 if (!KBD_IS_ACTIVE(kbd))
1100 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1101 if (!mtx_owned(&Giant)) {
1105 retval = ukbd_check_char(kbd);
1110 /* XXX the keyboard layer requires Giant */
1111 if (!mtx_owned(&Giant))
1115 if ((sc->sc_composed_char > 0) &&
1116 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1119 return (ukbd_check(kbd));
1123 /* read one byte from the keyboard if it's allowed */
1125 ukbd_read(keyboard_t *kbd, int wait)
1127 struct ukbd_softc *sc = kbd->kb_data;
1130 #ifdef UKBD_EMULATE_ATSCANCODE
1135 if (!KBD_IS_ACTIVE(kbd))
1138 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1139 if (!mtx_owned(&Giant)) {
1143 retval = ukbd_read(kbd, wait);
1148 /* XXX the keyboard layer requires Giant */
1149 if (!mtx_owned(&Giant))
1153 #ifdef UKBD_EMULATE_ATSCANCODE
1154 if (sc->sc_buffered_char[0]) {
1155 scancode = sc->sc_buffered_char[0];
1156 if (scancode & SCAN_PREFIX) {
1157 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1158 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1160 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1161 sc->sc_buffered_char[1] = 0;
1164 #endif /* UKBD_EMULATE_ATSCANCODE */
1167 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1168 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1173 #ifdef UKBD_EMULATE_ATSCANCODE
1174 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1175 if (keycode == NN) {
1178 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1179 (usbcode & KEY_RELEASE)));
1180 #else /* !UKBD_EMULATE_ATSCANCODE */
1182 #endif /* UKBD_EMULATE_ATSCANCODE */
1185 /* read char from the keyboard */
1187 ukbd_read_char(keyboard_t *kbd, int wait)
1189 struct ukbd_softc *sc = kbd->kb_data;
1194 #ifdef UKBD_EMULATE_ATSCANCODE
1199 if (!KBD_IS_ACTIVE(kbd))
1202 if (sc->sc_flags & UKBD_FLAG_POLLING) {
1203 if (!mtx_owned(&Giant)) {
1207 retval = ukbd_read_char(kbd, wait);
1212 /* XXX the keyboard layer requires Giant */
1213 if (!mtx_owned(&Giant))
1219 /* do we have a composed char to return ? */
1221 if ((sc->sc_composed_char > 0) &&
1222 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1224 action = sc->sc_composed_char;
1225 sc->sc_composed_char = 0;
1227 if (action > 0xFF) {
1232 #ifdef UKBD_EMULATE_ATSCANCODE
1234 /* do we have a pending raw scan code? */
1236 if (sc->sc_mode == K_RAW) {
1237 scancode = sc->sc_buffered_char[0];
1239 if (scancode & SCAN_PREFIX) {
1240 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1241 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1243 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1244 sc->sc_buffered_char[1] = 0;
1248 #endif /* UKBD_EMULATE_ATSCANCODE */
1250 /* see if there is something in the keyboard port */
1252 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1253 if (usbcode == -1) {
1258 #ifdef UKBD_EMULATE_ATSCANCODE
1259 /* USB key index -> key code -> AT scan code */
1260 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1261 if (keycode == NN) {
1264 /* return an AT scan code for the K_RAW mode */
1265 if (sc->sc_mode == K_RAW) {
1266 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1267 (usbcode & KEY_RELEASE)));
1269 #else /* !UKBD_EMULATE_ATSCANCODE */
1271 /* return the byte as is for the K_RAW mode */
1272 if (sc->sc_mode == K_RAW) {
1275 /* USB key index -> key code */
1276 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1277 if (keycode == NN) {
1280 #endif /* UKBD_EMULATE_ATSCANCODE */
1283 case 0x38: /* left alt (compose key) */
1284 if (usbcode & KEY_RELEASE) {
1285 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1286 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1288 if (sc->sc_composed_char > 0xFF) {
1289 sc->sc_composed_char = 0;
1293 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1294 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1295 sc->sc_composed_char = 0;
1299 /* XXX: I don't like these... */
1300 case 0x5c: /* print screen */
1301 if (sc->sc_flags & ALTS) {
1302 keycode = 0x54; /* sysrq */
1305 case 0x68: /* pause/break */
1306 if (sc->sc_flags & CTLS) {
1307 keycode = 0x6c; /* break */
1312 /* return the key code in the K_CODE mode */
1313 if (usbcode & KEY_RELEASE) {
1314 keycode |= SCAN_RELEASE;
1316 if (sc->sc_mode == K_CODE) {
1319 /* compose a character code */
1320 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1322 /* key pressed, process it */
1325 case 0x49: /* keypad 7,8,9 */
1326 sc->sc_composed_char *= 10;
1327 sc->sc_composed_char += keycode - 0x40;
1328 goto check_composed;
1332 case 0x4D: /* keypad 4,5,6 */
1333 sc->sc_composed_char *= 10;
1334 sc->sc_composed_char += keycode - 0x47;
1335 goto check_composed;
1339 case 0x51: /* keypad 1,2,3 */
1340 sc->sc_composed_char *= 10;
1341 sc->sc_composed_char += keycode - 0x4E;
1342 goto check_composed;
1344 case 0x52: /* keypad 0 */
1345 sc->sc_composed_char *= 10;
1346 goto check_composed;
1348 /* key released, no interest here */
1349 case SCAN_RELEASE | 0x47:
1350 case SCAN_RELEASE | 0x48:
1351 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1352 case SCAN_RELEASE | 0x4B:
1353 case SCAN_RELEASE | 0x4C:
1354 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1355 case SCAN_RELEASE | 0x4F:
1356 case SCAN_RELEASE | 0x50:
1357 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1358 case SCAN_RELEASE | 0x52: /* keypad 0 */
1361 case 0x38: /* left alt key */
1365 if (sc->sc_composed_char > 0) {
1366 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1367 sc->sc_composed_char = 0;
1373 /* keycode to key action */
1374 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1375 (keycode & SCAN_RELEASE),
1376 &sc->sc_state, &sc->sc_accents);
1377 if (action == NOKEY) {
1384 if (sc->sc_composed_char <= 0xFF) {
1391 /* some useful control functions */
1393 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1395 /* translate LED_XXX bits into the device specific bits */
1396 static const uint8_t ledmap[8] = {
1397 0, 2, 1, 3, 4, 6, 5, 7,
1399 struct ukbd_softc *sc = kbd->kb_data;
1402 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1403 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1407 if (!mtx_owned(&Giant)) {
1409 * XXX big problem: If scroll lock is pressed and "printf()"
1410 * is called, the CPU will get here, to un-scroll lock the
1411 * keyboard. But if "printf()" acquires the "Giant" lock,
1412 * there will be a locking order reversal problem, so the
1413 * keyboard system must get out of "Giant" first, before the
1414 * CPU can proceed here ...
1420 case KDGKBMODE: /* get keyboard mode */
1421 *(int *)arg = sc->sc_mode;
1423 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1424 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1426 ival = IOCPARM_IVAL(arg);
1427 arg = (caddr_t)&ival;
1430 case KDSKBMODE: /* set keyboard mode */
1431 switch (*(int *)arg) {
1433 if (sc->sc_mode != K_XLATE) {
1434 /* make lock key state and LED state match */
1435 sc->sc_state &= ~LOCK_MASK;
1436 sc->sc_state |= KBD_LED_VAL(kbd);
1441 if (sc->sc_mode != *(int *)arg) {
1442 ukbd_clear_state(kbd);
1443 sc->sc_mode = *(int *)arg;
1451 case KDGETLED: /* get keyboard LED */
1452 *(int *)arg = KBD_LED_VAL(kbd);
1454 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1455 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1457 ival = IOCPARM_IVAL(arg);
1458 arg = (caddr_t)&ival;
1461 case KDSETLED: /* set keyboard LED */
1462 /* NOTE: lock key state in "sc_state" won't be changed */
1463 if (*(int *)arg & ~LOCK_MASK) {
1467 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1468 if (sc->sc_mode == K_XLATE &&
1469 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1475 if (KBD_HAS_DEVICE(kbd)) {
1476 ukbd_set_leds(sc, ledmap[i & LED_MASK]);
1478 KBD_LED_VAL(kbd) = *(int *)arg;
1480 case KDGKBSTATE: /* get lock key state */
1481 *(int *)arg = sc->sc_state & LOCK_MASK;
1483 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1484 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1486 ival = IOCPARM_IVAL(arg);
1487 arg = (caddr_t)&ival;
1490 case KDSKBSTATE: /* set lock key state */
1491 if (*(int *)arg & ~LOCK_MASK) {
1494 sc->sc_state &= ~LOCK_MASK;
1495 sc->sc_state |= *(int *)arg;
1497 /* set LEDs and quit */
1498 return (ukbd_ioctl(kbd, KDSETLED, arg));
1500 case KDSETREPEAT: /* set keyboard repeat rate (new
1502 if (!KBD_HAS_DEVICE(kbd)) {
1505 if (((int *)arg)[1] < 0) {
1508 if (((int *)arg)[0] < 0) {
1511 if (((int *)arg)[0] < 200) /* fastest possible value */
1512 kbd->kb_delay1 = 200;
1514 kbd->kb_delay1 = ((int *)arg)[0];
1515 kbd->kb_delay2 = ((int *)arg)[1];
1518 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1519 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1521 ival = IOCPARM_IVAL(arg);
1522 arg = (caddr_t)&ival;
1525 case KDSETRAD: /* set keyboard repeat rate (old
1527 return (ukbd_set_typematic(kbd, *(int *)arg));
1529 case PIO_KEYMAP: /* set keyboard translation table */
1530 case PIO_KEYMAPENT: /* set keyboard translation table
1532 case PIO_DEADKEYMAP: /* set accent key translation table */
1536 return (genkbd_commonioctl(kbd, cmd, arg));
1542 /* clear the internal state of the keyboard */
1544 ukbd_clear_state(keyboard_t *kbd)
1546 struct ukbd_softc *sc = kbd->kb_data;
1548 if (!mtx_owned(&Giant)) {
1552 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1553 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1555 sc->sc_composed_char = 0;
1556 #ifdef UKBD_EMULATE_ATSCANCODE
1557 sc->sc_buffered_char[0] = 0;
1558 sc->sc_buffered_char[1] = 0;
1560 bzero(&sc->sc_ndata, sizeof(sc->sc_ndata));
1561 bzero(&sc->sc_odata, sizeof(sc->sc_odata));
1562 bzero(&sc->sc_ntime, sizeof(sc->sc_ntime));
1563 bzero(&sc->sc_otime, sizeof(sc->sc_otime));
1566 /* save the internal state, not used */
1568 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1570 return (len == 0) ? 1 : -1;
1573 /* set the internal state, not used */
1575 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1581 ukbd_poll(keyboard_t *kbd, int on)
1583 struct ukbd_softc *sc = kbd->kb_data;
1585 if (!mtx_owned(&Giant)) {
1589 retval = ukbd_poll(kbd, on);
1595 sc->sc_flags |= UKBD_FLAG_POLLING;
1597 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1602 /* local functions */
1605 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1607 DPRINTF("leds=0x%02x\n", leds);
1610 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1612 /* start transfer, if not already started */
1614 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1618 ukbd_set_typematic(keyboard_t *kbd, int code)
1620 static const int delays[] = {250, 500, 750, 1000};
1621 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1622 68, 76, 84, 92, 100, 110, 118, 126,
1623 136, 152, 168, 184, 200, 220, 236, 252,
1624 272, 304, 336, 368, 400, 440, 472, 504};
1629 kbd->kb_delay1 = delays[(code >> 5) & 3];
1630 kbd->kb_delay2 = rates[code & 0x1f];
1634 #ifdef UKBD_EMULATE_ATSCANCODE
1636 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1638 static const int scan[] = {
1644 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
1654 0x151, /* PageDown */
1657 0x146, /* XXX Pause/Break */
1658 0x15b, /* Win_L(Super_L) */
1659 0x15c, /* Win_R(Super_R) */
1660 0x15d, /* Application(Menu) */
1662 /* SUN TYPE 6 USB KEYBOARD */
1663 0x168, /* Sun Type 6 Help */
1664 0x15e, /* Sun Type 6 Stop */
1666 0x15f, /* Sun Type 6 Again */
1667 0x160, /* Sun Type 6 Props */
1668 0x161, /* Sun Type 6 Undo */
1669 0x162, /* Sun Type 6 Front */
1670 0x163, /* Sun Type 6 Copy */
1671 0x164, /* Sun Type 6 Open */
1672 0x165, /* Sun Type 6 Paste */
1673 0x166, /* Sun Type 6 Find */
1674 0x167, /* Sun Type 6 Cut */
1675 0x125, /* Sun Type 6 Mute */
1677 0x11f, /* Sun Type 6 VolumeDown */
1678 0x11e, /* Sun Type 6 VolumeUp */
1679 0x120, /* Sun Type 6 PowerDown */
1681 /* Japanese 106/109 keyboard */
1682 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
1683 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
1684 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
1685 0x79, /* Keyboard Intl' 4 (Henkan) */
1686 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
1687 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
1690 if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
1691 code = scan[code - 89];
1694 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
1695 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
1697 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
1698 code &= ~SCAN_PREFIX_SHIFT;
1700 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1702 if (code & SCAN_PREFIX) {
1703 if (code & SCAN_PREFIX_CTL) {
1705 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
1706 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
1707 } else if (code & SCAN_PREFIX_SHIFT) {
1709 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1710 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1712 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1713 sc->sc_buffered_char[1] = 0;
1715 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1721 #endif /* UKBD_EMULATE_ATSCANCODE */
1723 static keyboard_switch_t ukbdsw = {
1724 .probe = &ukbd__probe,
1728 .test_if = &ukbd_test_if,
1729 .enable = &ukbd_enable,
1730 .disable = &ukbd_disable,
1732 .check = &ukbd_check,
1733 .read_char = &ukbd_read_char,
1734 .check_char = &ukbd_check_char,
1735 .ioctl = &ukbd_ioctl,
1737 .clear_state = &ukbd_clear_state,
1738 .get_state = &ukbd_get_state,
1739 .set_state = &ukbd_set_state,
1740 .get_fkeystr = &genkbd_get_fkeystr,
1742 .diag = &genkbd_diag,
1745 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
1748 ukbd_driver_load(module_t mod, int what, void *arg)
1752 kbd_add_driver(&ukbd_kbd_driver);
1755 kbd_delete_driver(&ukbd_kbd_driver);
1761 static devclass_t ukbd_devclass;
1763 static device_method_t ukbd_methods[] = {
1764 DEVMETHOD(device_probe, ukbd_probe),
1765 DEVMETHOD(device_attach, ukbd_attach),
1766 DEVMETHOD(device_detach, ukbd_detach),
1767 DEVMETHOD(device_resume, ukbd_resume),
1771 static driver_t ukbd_driver = {
1773 .methods = ukbd_methods,
1774 .size = sizeof(struct ukbd_softc),
1777 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
1778 MODULE_DEPEND(ukbd, usb, 1, 1, 1);