sbin/hammer2/cmd_debug.c: Clear errno
[dragonfly.git] / sys / bus / u4b / input / ukbd.c
blobb40ad2c104f0c081a5291439f772d874355214c1
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD: head/sys/dev/usb/input/ukbd.c 262972 2014-03-10 08:52:30Z hselasky $");
5 /*-
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * All rights reserved.
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
15 * are met:
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.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
37 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
40 #include "opt_kbd.h"
41 #include "opt_ukbd.h"
42 #include "opt_evdev.h"
44 #include <sys/stdint.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/types.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/bus.h>
51 #include <sys/module.h>
52 #include <sys/lock.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
58 #include <sys/caps.h>
59 #include <sys/proc.h>
60 #include <sys/sched.h>
61 #include <sys/thread2.h>
63 #include <bus/u4b/usb.h>
64 #include <bus/u4b/usbdi.h>
65 #include <bus/u4b/usbdi_util.h>
66 #include <bus/u4b/usbhid.h>
68 #define USB_DEBUG_VAR ukbd_debug
69 #include <bus/u4b/usb_debug.h>
71 #include <bus/u4b/quirk/usb_quirk.h>
73 #ifdef EVDEV_SUPPORT
74 #include <dev/misc/evdev/input.h>
75 #include <dev/misc/evdev/evdev.h>
76 #endif
78 #include <sys/filio.h>
79 #include <sys/tty.h>
80 #include <sys/kbio.h>
82 #include <dev/misc/kbd/kbdreg.h>
84 /* the initial key map, accent map and fkey strings */
85 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
86 #define KBD_DFLT_KEYMAP
87 #include "ukbdmap.h"
88 #endif
90 /* the following file must be included after "ukbdmap.h" */
91 #include <dev/misc/kbd/kbdtables.h>
93 #ifdef USB_DEBUG
94 static int ukbd_debug = 0;
95 static int ukbd_no_leds = 0;
96 static int ukbd_pollrate = 0;
98 static SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB keyboard");
99 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
100 &ukbd_debug, 0, "Debug level");
101 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW,
102 &ukbd_no_leds, 0, "Disables setting of keyboard leds");
103 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, pollrate, CTLFLAG_RW,
104 &ukbd_pollrate, 0, "Force this polling rate, 1-1000Hz");
106 TUNABLE_INT("hw.usb.ukbd.debug", &ukbd_debug);
107 TUNABLE_INT("hw.usb.ukbd.no_leds", &ukbd_no_leds);
108 TUNABLE_INT("hw.usb.ukbd.pollrate", &ukbd_pollrate);
109 #endif
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 */
118 #define UKBD_BUFFER_SIZE 64 /* bytes */
120 struct ukbd_data {
121 uint16_t modifiers;
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
130 /* internal */
131 #define MOD_EJECT 0x0100
132 #define MOD_FN 0x0200
133 uint8_t keycode[UKBD_NKEYCODE];
136 enum {
137 UKBD_INTR_DT,
138 UKBD_CTRL_LED,
139 UKBD_N_TRANSFER,
142 struct ukbd_softc {
143 device_t sc_dev;
144 keyboard_t sc_kbd;
145 keymap_t sc_keymap;
146 accentmap_t sc_accmap;
147 fkeytab_t sc_fkeymap[UKBD_NFKEY];
148 struct hid_location sc_loc_apple_eject;
149 struct hid_location sc_loc_apple_fn;
150 struct hid_location sc_loc_ctrl_l;
151 struct hid_location sc_loc_ctrl_r;
152 struct hid_location sc_loc_shift_l;
153 struct hid_location sc_loc_shift_r;
154 struct hid_location sc_loc_alt_l;
155 struct hid_location sc_loc_alt_r;
156 struct hid_location sc_loc_win_l;
157 struct hid_location sc_loc_win_r;
158 struct hid_location sc_loc_events;
159 struct hid_location sc_loc_numlock;
160 struct hid_location sc_loc_capslock;
161 struct hid_location sc_loc_scrolllock;
162 struct usb_callout sc_callout;
163 struct ukbd_data sc_ndata;
164 struct ukbd_data sc_odata;
166 struct thread *sc_poll_thread;
167 struct usb_device *sc_udev;
168 struct usb_interface *sc_iface;
169 struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
170 #ifdef EVDEV_SUPPORT
171 struct evdev_dev *sc_evdev;
172 #endif
174 uint32_t sc_ntime[UKBD_NKEYCODE];
175 uint32_t sc_otime[UKBD_NKEYCODE];
176 uint32_t sc_input[UKBD_IN_BUF_SIZE]; /* input buffer */
177 uint32_t sc_time_ms;
178 uint32_t sc_composed_char; /* composed char code, if non-zero */
179 #ifdef UKBD_EMULATE_ATSCANCODE
180 uint32_t sc_buffered_char[2];
181 #endif
182 uint32_t sc_flags; /* flags */
183 #define UKBD_FLAG_COMPOSE 0x00000001
184 #define UKBD_FLAG_POLLING 0x00000002
185 #define UKBD_FLAG_SET_LEDS 0x00000004
186 #define UKBD_FLAG_ATTACHED 0x00000010
187 #define UKBD_FLAG_GONE 0x00000020
189 #define UKBD_FLAG_HID_MASK 0x003fffc0
190 #define UKBD_FLAG_APPLE_EJECT 0x00000040
191 #define UKBD_FLAG_APPLE_FN 0x00000080
192 #define UKBD_FLAG_APPLE_SWAP 0x00000100
193 #define UKBD_FLAG_TIMER_RUNNING 0x00000200
194 #define UKBD_FLAG_CTRL_L 0x00000400
195 #define UKBD_FLAG_CTRL_R 0x00000800
196 #define UKBD_FLAG_SHIFT_L 0x00001000
197 #define UKBD_FLAG_SHIFT_R 0x00002000
198 #define UKBD_FLAG_ALT_L 0x00004000
199 #define UKBD_FLAG_ALT_R 0x00008000
200 #define UKBD_FLAG_WIN_L 0x00010000
201 #define UKBD_FLAG_WIN_R 0x00020000
202 #define UKBD_FLAG_EVENTS 0x00040000
203 #define UKBD_FLAG_NUMLOCK 0x00080000
204 #define UKBD_FLAG_CAPSLOCK 0x00100000
205 #define UKBD_FLAG_SCROLLLOCK 0x00200000
207 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
208 int sc_state; /* shift/lock key state */
209 int sc_accents; /* accent key index (> 0) */
210 int sc_led_size;
211 int sc_kbd_size;
213 uint16_t sc_inputs;
214 uint16_t sc_inputhead;
215 uint16_t sc_inputtail;
216 uint16_t sc_modifiers;
218 uint8_t sc_leds; /* store for async led requests */
219 uint8_t sc_iface_index;
220 uint8_t sc_iface_no;
221 uint8_t sc_id_apple_eject;
222 uint8_t sc_id_apple_fn;
223 uint8_t sc_id_ctrl_l;
224 uint8_t sc_id_ctrl_r;
225 uint8_t sc_id_shift_l;
226 uint8_t sc_id_shift_r;
227 uint8_t sc_id_alt_l;
228 uint8_t sc_id_alt_r;
229 uint8_t sc_id_win_l;
230 uint8_t sc_id_win_r;
231 uint8_t sc_id_event;
232 uint8_t sc_id_numlock;
233 uint8_t sc_id_capslock;
234 uint8_t sc_id_scrolllock;
235 uint8_t sc_id_events;
236 uint8_t sc_kbd_id;
238 uint8_t sc_buffer[UKBD_BUFFER_SIZE];
241 #define KEY_ERROR 0x01
243 #define KEY_PRESS 0
244 #define KEY_RELEASE 0x400
245 #define KEY_INDEX(c) ((c) & 0xFF)
247 #define SCAN_PRESS 0
248 #define SCAN_RELEASE 0x80
249 #define SCAN_PREFIX_E0 0x100
250 #define SCAN_PREFIX_E1 0x200
251 #define SCAN_PREFIX_CTL 0x400
252 #define SCAN_PREFIX_SHIFT 0x800
253 #define SCAN_PREFIX (SCAN_PREFIX_E0 | SCAN_PREFIX_E1 | \
254 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
255 #define SCAN_CHAR(c) ((c) & 0x7f)
257 #define UKBD_LOCK(sc) lockmgr(&(sc)->sc_kbd.kb_lock, LK_EXCLUSIVE)
258 #define UKBD_UNLOCK(sc) lockmgr(&(sc)->sc_kbd.kb_lock, LK_RELEASE)
260 #ifdef INVARIANTS
263 * Assert that the lock is held in all contexts
264 * where the code can be executed.
266 #define UKBD_LOCK_ASSERT()
269 * Assert that the lock is held in the contexts
270 * where it really has to be so.
272 #define UKBD_CTX_LOCK_ASSERT()
273 #else
275 #define UKBD_LOCK_ASSERT() (void)0
276 #define UKBD_CTX_LOCK_ASSERT() (void)0
278 #endif
280 struct ukbd_mods {
281 uint32_t mask, key;
284 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
285 {MOD_CONTROL_L, 0xe0},
286 {MOD_CONTROL_R, 0xe4},
287 {MOD_SHIFT_L, 0xe1},
288 {MOD_SHIFT_R, 0xe5},
289 {MOD_ALT_L, 0xe2},
290 {MOD_ALT_R, 0xe6},
291 {MOD_WIN_L, 0xe3},
292 {MOD_WIN_R, 0xe7},
295 #define NN 0 /* no translation */
297 * Translate USB keycodes to AT keyboard scancodes.
300 * FIXME: Mac USB keyboard generates:
301 * 0x53: keypad NumLock/Clear
302 * 0x66: Power
303 * 0x67: keypad =
304 * 0x68: F13
305 * 0x69: F14
306 * 0x6a: F15
308 static const uint8_t ukbd_trtab[256] = {
309 0, 0, 0, 0, 30, 48, 46, 32, /* 00 - 07 */
310 18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
311 50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
312 22, 47, 17, 45, 21, 44, 2, 3, /* 18 - 1F */
313 4, 5, 6, 7, 8, 9, 10, 11, /* 20 - 27 */
314 28, 1, 14, 15, 57, 12, 13, 26, /* 28 - 2F */
315 27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
316 53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
317 65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
318 104, 102, 94, 96, 103, 99, 101, 98, /* 48 - 4F */
319 97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
320 89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
321 72, 73, 82, 83, 86, 107, 122, NN, /* 60 - 67 */
322 NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
323 NN, NN, NN, NN, 115, 108, 111, 113, /* 70 - 77 */
324 109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
325 121, 120, NN, NN, NN, NN, NN, 123, /* 80 - 87 */
326 124, 125, 126, 127, 128, NN, NN, NN, /* 88 - 8F */
327 NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
328 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
329 NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
330 NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
331 NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
332 NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
333 NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
334 NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
335 NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
336 NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
337 29, 42, 56, 105, 90, 54, 93, 106, /* E0 - E7 */
338 NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
339 NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
340 NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
343 static const uint8_t ukbd_boot_desc[] = {
344 0x05, 0x01, 0x09, 0x06, 0xa1,
345 0x01, 0x05, 0x07, 0x19, 0xe0,
346 0x29, 0xe7, 0x15, 0x00, 0x25,
347 0x01, 0x75, 0x01, 0x95, 0x08,
348 0x81, 0x02, 0x95, 0x01, 0x75,
349 0x08, 0x81, 0x01, 0x95, 0x03,
350 0x75, 0x01, 0x05, 0x08, 0x19,
351 0x01, 0x29, 0x03, 0x91, 0x02,
352 0x95, 0x05, 0x75, 0x01, 0x91,
353 0x01, 0x95, 0x06, 0x75, 0x08,
354 0x15, 0x00, 0x26, 0xff, 0x00,
355 0x05, 0x07, 0x19, 0x00, 0x2a,
356 0xff, 0x00, 0x81, 0x00, 0xc0
359 /* prototypes */
360 static void ukbd_timeout(void *);
361 static void ukbd_set_leds(struct ukbd_softc *, uint8_t);
362 #ifdef UKBD_EMULATE_ATSCANCODE
363 static int ukbd_key2scan(struct ukbd_softc *, int, int, int);
364 #endif
365 static uint32_t ukbd_read_char(keyboard_t *, int);
366 static void ukbd_clear_state(keyboard_t *);
367 static int ukbd_ioctl(keyboard_t *, u_long, caddr_t);
368 static int ukbd_enable(keyboard_t *);
369 static int ukbd_disable(keyboard_t *);
370 static void ukbd_interrupt(struct ukbd_softc *);
371 static void ukbd_event_keyinput(struct ukbd_softc *);
373 static device_probe_t ukbd_probe;
374 static device_attach_t ukbd_attach;
375 static device_detach_t ukbd_detach;
376 static device_resume_t ukbd_resume;
378 #ifdef EVDEV_SUPPORT
379 static const struct evdev_methods ukbd_evdev_methods = {
380 .ev_event = evdev_ev_kbd_event,
382 #endif
384 static uint8_t
385 ukbd_any_key_pressed(struct ukbd_softc *sc)
387 uint8_t i;
388 uint8_t j;
390 for (j = i = 0; i < UKBD_NKEYCODE; i++)
391 j |= sc->sc_odata.keycode[i];
393 return (j ? 1 : 0);
396 static void
397 ukbd_start_timer(struct ukbd_softc *sc)
399 sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
400 usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
403 static void
404 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
407 UKBD_CTX_LOCK_ASSERT();
409 DPRINTF("0x%02x (%d) %s\n", key, key,
410 (key & KEY_RELEASE) ? "released" : "pressed");
412 #ifdef EVDEV_SUPPORT
413 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) {
414 evdev_push_event(sc->sc_evdev, EV_KEY,
415 evdev_hid2key(KEY_INDEX(key)), !(key & KEY_RELEASE));
416 evdev_sync(sc->sc_evdev);
418 #endif
420 if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
421 sc->sc_input[sc->sc_inputtail] = key;
422 ++(sc->sc_inputs);
423 ++(sc->sc_inputtail);
424 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
425 sc->sc_inputtail = 0;
427 } else {
428 DPRINTF("input buffer is full\n");
432 static void
433 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
436 UKBD_CTX_LOCK_ASSERT();
437 KASSERT((sc->sc_flags & UKBD_FLAG_POLLING) != 0,
438 ("ukbd_do_poll called when not polling\n"));
439 DPRINTFN(2, "polling\n");
440 #if 0 /* XXX */
441 if (!kdb_active && !SCHEDULER_STOPPED()) {
443 * In this context the kernel is polling for input,
444 * but the USB subsystem works in normal interrupt-driven
445 * mode, so we just wait on the USB threads to do the job.
446 * Note that we currently hold the Giant, but it's also used
447 * as the transfer mtx, so we must release it while waiting.
449 while (sc->sc_inputs == 0) {
451 * Give USB threads a chance to run. Note that
452 * kern_yield performs DROP_GIANT + PICKUP_GIANT.
454 lwkt_yield();
455 if (!wait)
456 break;
458 return;
460 #endif
462 while (sc->sc_inputs == 0) {
464 usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
466 /* Delay-optimised support for repetition of keys */
467 if (ukbd_any_key_pressed(sc)) {
468 /* a key is pressed - need timekeeping */
469 DELAY(1000);
471 /* 1 millisecond has passed */
472 sc->sc_time_ms += 1;
475 ukbd_interrupt(sc);
477 if (!wait)
478 break;
482 static int32_t
483 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
485 int32_t c;
487 UKBD_CTX_LOCK_ASSERT();
488 #if 0
489 KASSERT((!kdb_active && !SCHEDULER_STOPPED())
490 || (sc->sc_flags & UKBD_FLAG_POLLING) != 0,
491 ("not polling in kdb or panic\n"));
492 #endif
494 if (sc->sc_inputs == 0 &&
495 (sc->sc_flags & UKBD_FLAG_GONE) == 0) {
496 /* start transfer, if not already started */
497 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
500 if (sc->sc_flags & UKBD_FLAG_POLLING)
501 ukbd_do_poll(sc, wait);
503 if (sc->sc_inputs == 0) {
504 c = -1;
505 } else {
506 c = sc->sc_input[sc->sc_inputhead];
507 --(sc->sc_inputs);
508 ++(sc->sc_inputhead);
509 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
510 sc->sc_inputhead = 0;
513 return (c);
516 static void
517 ukbd_interrupt(struct ukbd_softc *sc)
519 uint32_t n_mod;
520 uint32_t o_mod;
521 uint32_t now = sc->sc_time_ms;
522 uint32_t dtime;
523 uint8_t key;
524 uint8_t i;
525 uint8_t j;
527 UKBD_CTX_LOCK_ASSERT();
529 if (sc->sc_ndata.keycode[0] == KEY_ERROR)
530 return;
532 n_mod = sc->sc_ndata.modifiers;
533 o_mod = sc->sc_odata.modifiers;
534 if (n_mod != o_mod) {
535 for (i = 0; i < UKBD_NMOD; i++) {
536 if ((n_mod & ukbd_mods[i].mask) !=
537 (o_mod & ukbd_mods[i].mask)) {
538 ukbd_put_key(sc, ukbd_mods[i].key |
539 ((n_mod & ukbd_mods[i].mask) ?
540 KEY_PRESS : KEY_RELEASE));
544 /* Check for released keys. */
545 for (i = 0; i < UKBD_NKEYCODE; i++) {
546 key = sc->sc_odata.keycode[i];
547 if (key == 0) {
548 continue;
550 for (j = 0; j < UKBD_NKEYCODE; j++) {
551 if (sc->sc_ndata.keycode[j] == 0) {
552 continue;
554 if (key == sc->sc_ndata.keycode[j]) {
555 goto rfound;
558 ukbd_put_key(sc, key | KEY_RELEASE);
559 rfound: ;
562 /* Check for pressed keys. */
563 for (i = 0; i < UKBD_NKEYCODE; i++) {
564 key = sc->sc_ndata.keycode[i];
565 if (key == 0) {
566 continue;
568 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
569 for (j = 0; j < UKBD_NKEYCODE; j++) {
570 if (sc->sc_odata.keycode[j] == 0) {
571 continue;
573 if (key == sc->sc_odata.keycode[j]) {
575 /* key is still pressed */
577 sc->sc_ntime[i] = sc->sc_otime[j];
578 dtime = (sc->sc_otime[j] - now);
580 if (!(dtime & 0x80000000)) {
581 /* time has not elapsed */
582 goto pfound;
584 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
585 break;
588 ukbd_put_key(sc, key | KEY_PRESS);
591 * If any other key is presently down, force its repeat to be
592 * well in the future (100s). This makes the last key to be
593 * pressed do the autorepeat.
595 for (j = 0; j != UKBD_NKEYCODE; j++) {
596 if (j != i)
597 sc->sc_ntime[j] = now + (100 * 1000);
599 pfound: ;
602 sc->sc_odata = sc->sc_ndata;
604 memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
606 ukbd_event_keyinput(sc);
609 static void
610 ukbd_event_keyinput(struct ukbd_softc *sc)
612 int c;
614 UKBD_CTX_LOCK_ASSERT();
616 if ((sc->sc_flags & UKBD_FLAG_POLLING) != 0)
617 return;
619 if (sc->sc_inputs == 0)
620 return;
622 if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
623 KBD_IS_BUSY(&sc->sc_kbd)) {
624 /* let the callback function process the input */
625 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
626 sc->sc_kbd.kb_callback.kc_arg);
627 } else {
628 /* read and discard the input, no one is waiting for it */
629 do {
630 c = ukbd_read_char(&sc->sc_kbd, 0);
631 } while (c != NOKEY);
635 static void
636 ukbd_timeout(void *arg)
638 struct ukbd_softc *sc = arg;
640 UKBD_LOCK_ASSERT();
642 sc->sc_time_ms += 25; /* milliseconds */
644 ukbd_interrupt(sc);
646 /* Make sure any leftover key events gets read out */
647 ukbd_event_keyinput(sc);
649 if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
650 ukbd_start_timer(sc);
651 } else {
652 sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
656 static uint8_t
657 ukbd_apple_fn(uint8_t keycode) {
658 switch (keycode) {
659 case 0x28: return 0x49; /* RETURN -> INSERT */
660 case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
661 case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
662 case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
663 case 0x52: return 0x4b; /* UP ARROW -> PGUP */
664 case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
665 default: return keycode;
669 static uint8_t
670 ukbd_apple_swap(uint8_t keycode) {
671 switch (keycode) {
672 case 0x35: return 0x64;
673 case 0x64: return 0x35;
674 default: return keycode;
678 static void
679 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
681 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
682 struct usb_page_cache *pc;
683 uint8_t i;
684 uint8_t offset;
685 uint8_t id;
686 int len;
688 UKBD_LOCK_ASSERT();
690 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
691 pc = usbd_xfer_get_frame(xfer, 0);
693 switch (USB_GET_STATE(xfer)) {
694 case USB_ST_TRANSFERRED:
695 DPRINTF("actlen=%d bytes\n", len);
697 if (len == 0) {
698 DPRINTF("zero length data\n");
699 goto tr_setup;
702 if (sc->sc_kbd_id != 0) {
703 /* check and remove HID ID byte */
704 usbd_copy_out(pc, 0, &id, 1);
705 offset = 1;
706 len--;
707 if (len == 0) {
708 DPRINTF("zero length data\n");
709 goto tr_setup;
711 } else {
712 offset = 0;
713 id = 0;
716 if (len > UKBD_BUFFER_SIZE)
717 len = UKBD_BUFFER_SIZE;
719 /* get data */
720 usbd_copy_out(pc, offset, sc->sc_buffer, len);
722 /* clear temporary storage */
723 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
725 /* scan through HID data */
726 if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
727 (id == sc->sc_id_apple_eject)) {
728 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject))
729 sc->sc_modifiers |= MOD_EJECT;
730 else
731 sc->sc_modifiers &= ~MOD_EJECT;
733 if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
734 (id == sc->sc_id_apple_fn)) {
735 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn))
736 sc->sc_modifiers |= MOD_FN;
737 else
738 sc->sc_modifiers &= ~MOD_FN;
740 if ((sc->sc_flags & UKBD_FLAG_CTRL_L) &&
741 (id == sc->sc_id_ctrl_l)) {
742 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_l))
743 sc-> sc_modifiers |= MOD_CONTROL_L;
744 else
745 sc-> sc_modifiers &= ~MOD_CONTROL_L;
747 if ((sc->sc_flags & UKBD_FLAG_CTRL_R) &&
748 (id == sc->sc_id_ctrl_r)) {
749 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_r))
750 sc->sc_modifiers |= MOD_CONTROL_R;
751 else
752 sc->sc_modifiers &= ~MOD_CONTROL_R;
754 if ((sc->sc_flags & UKBD_FLAG_SHIFT_L) &&
755 (id == sc->sc_id_shift_l)) {
756 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_l))
757 sc->sc_modifiers |= MOD_SHIFT_L;
758 else
759 sc->sc_modifiers &= ~MOD_SHIFT_L;
761 if ((sc->sc_flags & UKBD_FLAG_SHIFT_R) &&
762 (id == sc->sc_id_shift_r)) {
763 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_r))
764 sc->sc_modifiers |= MOD_SHIFT_R;
765 else
766 sc->sc_modifiers &= ~MOD_SHIFT_R;
768 if ((sc->sc_flags & UKBD_FLAG_ALT_L) &&
769 (id == sc->sc_id_alt_l)) {
770 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_l))
771 sc->sc_modifiers |= MOD_ALT_L;
772 else
773 sc->sc_modifiers &= ~MOD_ALT_L;
775 if ((sc->sc_flags & UKBD_FLAG_ALT_R) &&
776 (id == sc->sc_id_alt_r)) {
777 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_r))
778 sc->sc_modifiers |= MOD_ALT_R;
779 else
780 sc->sc_modifiers &= ~MOD_ALT_R;
782 if ((sc->sc_flags & UKBD_FLAG_WIN_L) &&
783 (id == sc->sc_id_win_l)) {
784 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_l))
785 sc->sc_modifiers |= MOD_WIN_L;
786 else
787 sc->sc_modifiers &= ~MOD_WIN_L;
789 if ((sc->sc_flags & UKBD_FLAG_WIN_R) &&
790 (id == sc->sc_id_win_r)) {
791 if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_r))
792 sc->sc_modifiers |= MOD_WIN_R;
793 else
794 sc->sc_modifiers &= ~MOD_WIN_R;
797 sc->sc_ndata.modifiers = sc->sc_modifiers;
799 if ((sc->sc_flags & UKBD_FLAG_EVENTS) &&
800 (id == sc->sc_id_events)) {
801 i = sc->sc_loc_events.count;
802 if (i > UKBD_NKEYCODE)
803 i = UKBD_NKEYCODE;
804 if (i > len)
805 i = len;
806 while (i--) {
807 sc->sc_ndata.keycode[i] =
808 hid_get_data(sc->sc_buffer + i, len - i,
809 &sc->sc_loc_events);
813 #ifdef USB_DEBUG
814 DPRINTF("modifiers = 0x%04x\n", (int)sc->sc_modifiers);
815 for (i = 0; i < UKBD_NKEYCODE; i++) {
816 if (sc->sc_ndata.keycode[i]) {
817 DPRINTF("[%d] = 0x%02x\n",
818 (int)i, (int)sc->sc_ndata.keycode[i]);
821 #endif
822 if (sc->sc_modifiers & MOD_FN) {
823 for (i = 0; i < UKBD_NKEYCODE; i++) {
824 sc->sc_ndata.keycode[i] =
825 ukbd_apple_fn(sc->sc_ndata.keycode[i]);
829 if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
830 for (i = 0; i < UKBD_NKEYCODE; i++) {
831 sc->sc_ndata.keycode[i] =
832 ukbd_apple_swap(sc->sc_ndata.keycode[i]);
836 ukbd_interrupt(sc);
838 if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
839 if (ukbd_any_key_pressed(sc)) {
840 ukbd_start_timer(sc);
844 case USB_ST_SETUP:
845 tr_setup:
846 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
847 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
848 usbd_transfer_submit(xfer);
849 } else {
850 DPRINTF("input queue is full!\n");
852 break;
854 default: /* Error */
855 DPRINTF("error=%s\n", usbd_errstr(error));
857 if (error != USB_ERR_CANCELLED) {
858 /* try to clear stall first */
859 usbd_xfer_set_stall(xfer);
860 goto tr_setup;
862 break;
866 static void
867 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
869 struct ukbd_softc *sc = usbd_xfer_softc(xfer);
870 struct usb_device_request req;
871 struct usb_page_cache *pc;
872 uint8_t id;
873 uint8_t any;
874 int len;
876 UKBD_LOCK_ASSERT();
878 #ifdef USB_DEBUG
879 if (ukbd_no_leds)
880 return;
881 #endif
883 switch (USB_GET_STATE(xfer)) {
884 case USB_ST_TRANSFERRED:
885 case USB_ST_SETUP:
886 if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS))
887 break;
888 sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
890 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
891 req.bRequest = UR_SET_REPORT;
892 USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
893 req.wIndex[0] = sc->sc_iface_no;
894 req.wIndex[1] = 0;
895 req.wLength[1] = 0;
897 memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE);
899 id = 0;
900 any = 0;
902 /* Assumption: All led bits must be in the same ID. */
904 if (sc->sc_flags & UKBD_FLAG_NUMLOCK) {
905 if (sc->sc_leds & NLKED) {
906 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
907 &sc->sc_loc_numlock, 1);
909 id = sc->sc_id_numlock;
910 any = 1;
913 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) {
914 if (sc->sc_leds & SLKED) {
915 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
916 &sc->sc_loc_scrolllock, 1);
918 id = sc->sc_id_scrolllock;
919 any = 1;
922 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) {
923 if (sc->sc_leds & CLKED) {
924 hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
925 &sc->sc_loc_capslock, 1);
927 id = sc->sc_id_capslock;
928 any = 1;
931 /* if no leds, nothing to do */
932 if (!any)
933 break;
935 #ifdef EVDEV_SUPPORT
936 if (sc->sc_evdev != NULL)
937 evdev_push_leds(sc->sc_evdev, sc->sc_leds);
938 #endif
940 /* range check output report length */
941 len = sc->sc_led_size;
942 if (len > (UKBD_BUFFER_SIZE - 1))
943 len = (UKBD_BUFFER_SIZE - 1);
945 /* check if we need to prefix an ID byte */
946 sc->sc_buffer[0] = id;
948 pc = usbd_xfer_get_frame(xfer, 1);
949 if (id != 0) {
950 len++;
951 usbd_copy_in(pc, 0, sc->sc_buffer, len);
952 } else {
953 usbd_copy_in(pc, 0, sc->sc_buffer + 1, len);
955 req.wLength[0] = len;
956 usbd_xfer_set_frame_len(xfer, 1, len);
958 DPRINTF("len=%d, id=%d\n", len, id);
960 /* setup control request last */
961 pc = usbd_xfer_get_frame(xfer, 0);
962 usbd_copy_in(pc, 0, &req, sizeof(req));
963 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
965 /* start data transfer */
966 usbd_xfer_set_frames(xfer, 2);
967 usbd_transfer_submit(xfer);
968 break;
970 default: /* Error */
971 DPRINTFN(1, "error=%s\n", usbd_errstr(error));
972 break;
976 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
978 [UKBD_INTR_DT] = {
979 .type = UE_INTERRUPT,
980 .endpoint = UE_ADDR_ANY,
981 .direction = UE_DIR_IN,
982 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
983 .bufsize = 0, /* use wMaxPacketSize */
984 .callback = &ukbd_intr_callback,
987 [UKBD_CTRL_LED] = {
988 .type = UE_CONTROL,
989 .endpoint = 0x00, /* Control pipe */
990 .direction = UE_DIR_ANY,
991 .bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE,
992 .callback = &ukbd_set_leds_callback,
993 .timeout = 1000, /* 1 second */
997 /* A match on these entries will load ukbd */
998 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = {
999 {USB_IFACE_CLASS(UICLASS_HID),
1000 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
1001 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),},
1004 static int
1005 ukbd_probe(device_t dev)
1007 keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
1008 struct usb_attach_arg *uaa = device_get_ivars(dev);
1009 void *d_ptr;
1010 int error;
1011 uint16_t d_len;
1013 UKBD_LOCK_ASSERT();
1014 DPRINTFN(11, "\n");
1016 if (sw == NULL) {
1017 return (ENXIO);
1019 if (uaa->usb_mode != USB_MODE_HOST) {
1020 return (ENXIO);
1023 if (uaa->info.bInterfaceClass != UICLASS_HID)
1024 return (ENXIO);
1026 if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1027 return (ENXIO);
1029 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
1030 (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD))
1031 return (BUS_PROBE_DEFAULT);
1033 error = usbd_req_get_hid_desc(uaa->device, NULL,
1034 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
1036 if (error)
1037 return (ENXIO);
1039 if (hid_is_keyboard(d_ptr, d_len)) {
1040 if (hid_is_mouse(d_ptr, d_len)) {
1042 * NOTE: We currently don't support USB mouse
1043 * and USB keyboard on the same USB endpoint.
1044 * Let "ums" driver win.
1046 error = ENXIO;
1047 } else {
1048 error = BUS_PROBE_DEFAULT;
1050 } else {
1051 error = ENXIO;
1053 kfree(d_ptr, M_TEMP);
1054 return (error);
1057 static void
1058 ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len)
1060 uint32_t flags;
1062 /* reset detected bits */
1063 sc->sc_flags &= ~UKBD_FLAG_HID_MASK;
1065 /* check if there is an ID byte */
1066 sc->sc_kbd_size = hid_report_size(ptr, len,
1067 hid_input, &sc->sc_kbd_id);
1069 /* investigate if this is an Apple Keyboard */
1070 if (hid_locate(ptr, len,
1071 HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
1072 hid_input, 0, &sc->sc_loc_apple_eject, &flags,
1073 &sc->sc_id_apple_eject)) {
1074 if (flags & HIO_VARIABLE)
1075 sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
1076 UKBD_FLAG_APPLE_SWAP;
1077 DPRINTFN(1, "Found Apple eject-key\n");
1079 if (hid_locate(ptr, len,
1080 HID_USAGE2(0xFFFF, 0x0003),
1081 hid_input, 0, &sc->sc_loc_apple_fn, &flags,
1082 &sc->sc_id_apple_fn)) {
1083 if (flags & HIO_VARIABLE)
1084 sc->sc_flags |= UKBD_FLAG_APPLE_FN;
1085 DPRINTFN(1, "Found Apple FN-key\n");
1087 /* figure out some keys */
1088 if (hid_locate(ptr, len,
1089 HID_USAGE2(HUP_KEYBOARD, 0xE0),
1090 hid_input, 0, &sc->sc_loc_ctrl_l, &flags,
1091 &sc->sc_id_ctrl_l)) {
1092 if (flags & HIO_VARIABLE)
1093 sc->sc_flags |= UKBD_FLAG_CTRL_L;
1094 DPRINTFN(1, "Found left control\n");
1096 if (hid_locate(ptr, len,
1097 HID_USAGE2(HUP_KEYBOARD, 0xE4),
1098 hid_input, 0, &sc->sc_loc_ctrl_r, &flags,
1099 &sc->sc_id_ctrl_r)) {
1100 if (flags & HIO_VARIABLE)
1101 sc->sc_flags |= UKBD_FLAG_CTRL_R;
1102 DPRINTFN(1, "Found right control\n");
1104 if (hid_locate(ptr, len,
1105 HID_USAGE2(HUP_KEYBOARD, 0xE1),
1106 hid_input, 0, &sc->sc_loc_shift_l, &flags,
1107 &sc->sc_id_shift_l)) {
1108 if (flags & HIO_VARIABLE)
1109 sc->sc_flags |= UKBD_FLAG_SHIFT_L;
1110 DPRINTFN(1, "Found left shift\n");
1112 if (hid_locate(ptr, len,
1113 HID_USAGE2(HUP_KEYBOARD, 0xE5),
1114 hid_input, 0, &sc->sc_loc_shift_r, &flags,
1115 &sc->sc_id_shift_r)) {
1116 if (flags & HIO_VARIABLE)
1117 sc->sc_flags |= UKBD_FLAG_SHIFT_R;
1118 DPRINTFN(1, "Found right shift\n");
1120 if (hid_locate(ptr, len,
1121 HID_USAGE2(HUP_KEYBOARD, 0xE2),
1122 hid_input, 0, &sc->sc_loc_alt_l, &flags,
1123 &sc->sc_id_alt_l)) {
1124 if (flags & HIO_VARIABLE)
1125 sc->sc_flags |= UKBD_FLAG_ALT_L;
1126 DPRINTFN(1, "Found left alt\n");
1128 if (hid_locate(ptr, len,
1129 HID_USAGE2(HUP_KEYBOARD, 0xE6),
1130 hid_input, 0, &sc->sc_loc_alt_r, &flags,
1131 &sc->sc_id_alt_r)) {
1132 if (flags & HIO_VARIABLE)
1133 sc->sc_flags |= UKBD_FLAG_ALT_R;
1134 DPRINTFN(1, "Found right alt\n");
1136 if (hid_locate(ptr, len,
1137 HID_USAGE2(HUP_KEYBOARD, 0xE3),
1138 hid_input, 0, &sc->sc_loc_win_l, &flags,
1139 &sc->sc_id_win_l)) {
1140 if (flags & HIO_VARIABLE)
1141 sc->sc_flags |= UKBD_FLAG_WIN_L;
1142 DPRINTFN(1, "Found left GUI\n");
1144 if (hid_locate(ptr, len,
1145 HID_USAGE2(HUP_KEYBOARD, 0xE7),
1146 hid_input, 0, &sc->sc_loc_win_r, &flags,
1147 &sc->sc_id_win_r)) {
1148 if (flags & HIO_VARIABLE)
1149 sc->sc_flags |= UKBD_FLAG_WIN_R;
1150 DPRINTFN(1, "Found right GUI\n");
1152 /* figure out event buffer */
1153 if (hid_locate(ptr, len,
1154 HID_USAGE2(HUP_KEYBOARD, 0x00),
1155 hid_input, 0, &sc->sc_loc_events, &flags,
1156 &sc->sc_id_events)) {
1157 if (flags & HIO_VARIABLE) {
1158 DPRINTFN(1, "Ignoring keyboard event control\n");
1159 } else {
1160 sc->sc_flags |= UKBD_FLAG_EVENTS;
1161 DPRINTFN(1, "Found keyboard event array\n");
1165 /* figure out leds on keyboard */
1166 sc->sc_led_size = hid_report_size(ptr, len,
1167 hid_output, NULL);
1169 if (hid_locate(ptr, len,
1170 HID_USAGE2(HUP_LEDS, 0x01),
1171 hid_output, 0, &sc->sc_loc_numlock, &flags,
1172 &sc->sc_id_numlock)) {
1173 if (flags & HIO_VARIABLE)
1174 sc->sc_flags |= UKBD_FLAG_NUMLOCK;
1175 DPRINTFN(1, "Found keyboard numlock\n");
1177 if (hid_locate(ptr, len,
1178 HID_USAGE2(HUP_LEDS, 0x02),
1179 hid_output, 0, &sc->sc_loc_capslock, &flags,
1180 &sc->sc_id_capslock)) {
1181 if (flags & HIO_VARIABLE)
1182 sc->sc_flags |= UKBD_FLAG_CAPSLOCK;
1183 DPRINTFN(1, "Found keyboard capslock\n");
1185 if (hid_locate(ptr, len,
1186 HID_USAGE2(HUP_LEDS, 0x03),
1187 hid_output, 0, &sc->sc_loc_scrolllock, &flags,
1188 &sc->sc_id_scrolllock)) {
1189 if (flags & HIO_VARIABLE)
1190 sc->sc_flags |= UKBD_FLAG_SCROLLLOCK;
1191 DPRINTFN(1, "Found keyboard scrolllock\n");
1195 static int
1196 ukbd_attach(device_t dev)
1198 struct ukbd_softc *sc = device_get_softc(dev);
1199 struct usb_attach_arg *uaa = device_get_ivars(dev);
1200 int32_t unit = device_get_unit(dev);
1201 keyboard_t *kbd = &sc->sc_kbd;
1202 void *hid_ptr = NULL;
1203 usb_error_t err;
1204 uint16_t n;
1205 uint16_t hid_len;
1206 #ifdef EVDEV_SUPPORT
1207 struct evdev_dev *evdev;
1208 int i;
1209 #endif
1210 #ifdef USB_DEBUG
1211 int rate;
1212 #endif
1213 UKBD_LOCK_ASSERT();
1215 kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER,
1216 unit, 0, KB_PRI_USB, 0, 0);
1218 kbd->kb_data = (void *)sc;
1220 device_set_usb_desc(dev);
1222 sc->sc_udev = uaa->device;
1223 sc->sc_iface = uaa->iface;
1224 sc->sc_iface_index = uaa->info.bIfaceIndex;
1225 sc->sc_iface_no = uaa->info.bIfaceNum;
1226 sc->sc_mode = K_XLATE;
1228 usb_callout_init_mtx(&sc->sc_callout, &kbd->kb_lock, 0);
1230 err = usbd_transfer_setup(uaa->device,
1231 &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
1232 UKBD_N_TRANSFER, sc, &sc->sc_kbd.kb_lock);
1234 if (err) {
1235 DPRINTF("error=%s\n", usbd_errstr(err));
1236 goto detach;
1238 /* setup default keyboard maps */
1240 sc->sc_keymap = key_map;
1241 sc->sc_accmap = accent_map;
1242 for (n = 0; n < UKBD_NFKEY; n++) {
1243 sc->sc_fkeymap[n] = fkey_tab[n];
1246 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
1247 sc->sc_fkeymap, UKBD_NFKEY);
1249 KBD_FOUND_DEVICE(kbd);
1251 ukbd_clear_state(kbd);
1254 * FIXME: set the initial value for lock keys in "sc_state"
1255 * according to the BIOS data?
1257 KBD_PROBE_DONE(kbd);
1259 /* get HID descriptor */
1260 err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
1261 &hid_len, M_TEMP, uaa->info.bIfaceIndex);
1263 if (err == 0) {
1264 DPRINTF("Parsing HID descriptor of %d bytes\n",
1265 (int)hid_len);
1267 ukbd_parse_hid(sc, hid_ptr, hid_len);
1269 kfree(hid_ptr, M_TEMP);
1272 /* check if we should use the boot protocol */
1273 if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) ||
1274 (err != 0) || (!(sc->sc_flags & UKBD_FLAG_EVENTS))) {
1276 DPRINTF("Forcing boot protocol\n");
1278 err = usbd_req_set_protocol(sc->sc_udev, NULL,
1279 sc->sc_iface_index, 0);
1281 if (err != 0) {
1282 DPRINTF("Set protocol error=%s (ignored)\n",
1283 usbd_errstr(err));
1286 ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc));
1289 /* ignore if SETIDLE fails, hence it is not crucial */
1290 usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
1292 ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
1294 KBD_INIT_DONE(kbd);
1296 if (kbd_register(kbd) < 0) {
1297 goto detach;
1299 KBD_CONFIG_DONE(kbd);
1301 ukbd_enable(kbd);
1303 #ifdef KBD_INSTALL_CDEV
1304 if (kbd_attach(kbd)) {
1305 goto detach;
1307 #endif
1309 #ifdef EVDEV_SUPPORT
1310 evdev = evdev_alloc();
1311 evdev_set_name(evdev, device_get_desc(dev));
1312 evdev_set_phys(evdev, device_get_nameunit(dev));
1313 evdev_set_id(evdev, BUS_USB, uaa->info.idVendor,
1314 uaa->info.idProduct, 0);
1315 evdev_set_serial(evdev, usb_get_serial(uaa->device));
1316 evdev_set_methods(evdev, kbd, &ukbd_evdev_methods);
1317 evdev_support_event(evdev, EV_SYN);
1318 evdev_support_event(evdev, EV_KEY);
1319 if (sc->sc_flags & (UKBD_FLAG_NUMLOCK | UKBD_FLAG_CAPSLOCK |
1320 UKBD_FLAG_SCROLLLOCK))
1321 evdev_support_event(evdev, EV_LED);
1322 evdev_support_event(evdev, EV_REP);
1324 for (i = 0x00; i <= 0xFF; i++)
1325 evdev_support_key(evdev, evdev_hid2key(i));
1326 if (sc->sc_flags & UKBD_FLAG_NUMLOCK)
1327 evdev_support_led(evdev, LED_NUML);
1328 if (sc->sc_flags & UKBD_FLAG_CAPSLOCK)
1329 evdev_support_led(evdev, LED_CAPSL);
1330 if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK)
1331 evdev_support_led(evdev, LED_SCROLLL);
1333 if (evdev_register(evdev))
1334 evdev_free(evdev);
1335 else
1336 sc->sc_evdev = evdev;
1337 #endif
1339 sc->sc_flags |= UKBD_FLAG_ATTACHED;
1341 if (bootverbose) {
1342 genkbd_diag(kbd, bootverbose);
1345 #ifdef USB_DEBUG
1346 /* check for polling rate override */
1347 rate = ukbd_pollrate;
1348 if (rate > 0) {
1349 if (rate > 1000)
1350 rate = 1;
1351 else
1352 rate = 1000 / rate;
1354 /* set new polling interval in ms */
1355 usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT], rate);
1357 #endif
1358 /* start the keyboard */
1359 /* XXX mp locking added */
1360 UKBD_LOCK(sc);
1361 usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
1362 UKBD_UNLOCK(sc);
1364 return (0); /* success */
1365 detach:
1366 ukbd_detach(dev);
1367 return (ENXIO); /* error */
1370 static int
1371 ukbd_detach(device_t dev)
1373 struct ukbd_softc *sc = device_get_softc(dev);
1374 int error;
1376 UKBD_LOCK_ASSERT();
1378 DPRINTF("\n");
1380 crit_enter();
1381 sc->sc_flags |= UKBD_FLAG_GONE;
1383 usb_callout_stop(&sc->sc_callout);
1385 /* kill any stuck keys */
1386 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1387 /* stop receiving events from the USB keyboard */
1388 usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT]);
1390 /* release all leftover keys, if any */
1391 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1393 /* process releasing of all keys */
1394 ukbd_interrupt(sc);
1397 ukbd_disable(&sc->sc_kbd);
1400 * XXX make sure this is in the correct place here,
1401 * it was taken from below the second if()
1403 usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1404 usb_callout_drain(&sc->sc_callout);
1406 #ifdef KBD_INSTALL_CDEV
1407 if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1408 error = kbd_detach(&sc->sc_kbd);
1409 if (error) {
1410 /* usb attach cannot return an error */
1411 device_printf(dev, "WARNING: kbd_detach() "
1412 "returned non-zero! (ignored)\n");
1415 #endif
1417 #ifdef EVDEV_SUPPORT
1418 evdev_free(sc->sc_evdev);
1419 #endif
1421 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
1423 * kbd_unregister requires kb_lock to be held
1424 * but lockuninits it then
1426 UKBD_LOCK(sc);
1427 error = kbd_unregister(&sc->sc_kbd);
1428 if (error) {
1429 /* usb attach cannot return an error */
1430 device_printf(dev, "WARNING: kbd_unregister() "
1431 "returned non-zero! (ignored)\n");
1434 sc->sc_kbd.kb_flags = 0;
1436 crit_exit();
1438 DPRINTF("%s: disconnected\n",
1439 device_get_nameunit(dev));
1441 return (0);
1444 static int
1445 ukbd_resume(device_t dev)
1447 struct ukbd_softc *sc = device_get_softc(dev);
1449 UKBD_LOCK_ASSERT();
1451 ukbd_clear_state(&sc->sc_kbd);
1453 return (0);
1456 /* early keyboard probe, not supported */
1457 static int
1458 ukbd_configure(int flags)
1460 return (0);
1463 /* detect a keyboard, not used */
1464 static int
1465 ukbd__probe(int unit, void *arg, int flags)
1467 return (ENXIO);
1470 /* reset and initialize the device, not used */
1471 static int
1472 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1474 return (ENXIO);
1477 /* test the interface to the device, not used */
1478 static int
1479 ukbd_test_if(keyboard_t *kbd)
1481 return (0);
1484 /* finish using this keyboard, not used */
1485 static int
1486 ukbd_term(keyboard_t *kbd)
1488 return (ENXIO);
1491 /* keyboard interrupt routine, not used */
1492 static int
1493 ukbd_intr(keyboard_t *kbd, void *arg)
1495 return (0);
1498 /* lock the access to the keyboard, not used */
1499 static int
1500 ukbd_lock(keyboard_t *kbd, int lock)
1502 return (1);
1506 * Enable the access to the device; until this function is called,
1507 * the client cannot read from the keyboard.
1509 static int
1510 ukbd_enable(keyboard_t *kbd)
1512 crit_enter();
1513 KBD_ACTIVATE(kbd);
1514 crit_exit();
1516 return (0);
1519 /* disallow the access to the device */
1520 static int
1521 ukbd_disable(keyboard_t *kbd)
1523 crit_enter();
1524 KBD_DEACTIVATE(kbd);
1525 crit_exit();
1527 return (0);
1530 /* check if data is waiting */
1531 /* Currently unused. */
1532 static int
1533 ukbd_check(keyboard_t *kbd)
1535 struct ukbd_softc *sc = kbd->kb_data;
1537 UKBD_CTX_LOCK_ASSERT();
1539 if (!KBD_IS_ACTIVE(kbd))
1540 return (0);
1542 if (sc->sc_flags & UKBD_FLAG_POLLING)
1543 ukbd_do_poll(sc, 0);
1545 #ifdef UKBD_EMULATE_ATSCANCODE
1546 if (sc->sc_buffered_char[0]) {
1547 return (1);
1549 #endif
1550 if (sc->sc_inputs > 0) {
1551 return (1);
1553 return (0);
1556 /* check if char is waiting */
1557 static int
1558 ukbd_check_char_locked(keyboard_t *kbd)
1560 struct ukbd_softc *sc = kbd->kb_data;
1562 UKBD_CTX_LOCK_ASSERT();
1564 if (!KBD_IS_ACTIVE(kbd))
1565 return (0);
1567 if ((sc->sc_composed_char > 0) &&
1568 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1569 return (1);
1571 return (ukbd_check(kbd));
1574 static int
1575 ukbd_check_char(keyboard_t *kbd)
1577 int result;
1578 #if 0
1579 struct ukbd_softc *sc = kbd->kb_data;
1581 UKBD_LOCK(sc);
1582 #endif
1583 result = ukbd_check_char_locked(kbd);
1584 #if 0
1585 UKBD_UNLOCK(sc);
1586 #endif
1588 return (result);
1591 /* read one byte from the keyboard if it's allowed */
1592 /* Currently unused. */
1593 static int
1594 ukbd_read(keyboard_t *kbd, int wait)
1596 struct ukbd_softc *sc = kbd->kb_data;
1597 int32_t usbcode;
1598 #ifdef UKBD_EMULATE_ATSCANCODE
1599 uint32_t keycode;
1600 uint32_t scancode;
1602 #endif
1604 UKBD_CTX_LOCK_ASSERT();
1606 if (!KBD_IS_ACTIVE(kbd))
1607 return (-1);
1609 #ifdef UKBD_EMULATE_ATSCANCODE
1610 if (sc->sc_buffered_char[0]) {
1611 scancode = sc->sc_buffered_char[0];
1612 if (scancode & SCAN_PREFIX) {
1613 sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1614 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1616 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1617 sc->sc_buffered_char[1] = 0;
1618 return (scancode);
1620 #endif /* UKBD_EMULATE_ATSCANCODE */
1622 /* XXX */
1623 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1624 if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1625 return (-1);
1627 ++(kbd->kb_count);
1629 #ifdef UKBD_EMULATE_ATSCANCODE
1630 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1631 if (keycode == NN) {
1632 return -1;
1634 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1635 (usbcode & KEY_RELEASE)));
1636 #else /* !UKBD_EMULATE_ATSCANCODE */
1637 return (usbcode);
1638 #endif /* UKBD_EMULATE_ATSCANCODE */
1641 /* read char from the keyboard */
1642 static uint32_t
1643 ukbd_read_char_locked(keyboard_t *kbd, int wait)
1645 struct ukbd_softc *sc = kbd->kb_data;
1646 uint32_t action;
1647 uint32_t keycode;
1648 int32_t usbcode;
1649 #ifdef UKBD_EMULATE_ATSCANCODE
1650 uint32_t scancode;
1651 #endif
1653 UKBD_CTX_LOCK_ASSERT();
1655 if (!KBD_IS_ACTIVE(kbd))
1656 return (NOKEY);
1658 next_code:
1660 /* do we have a composed char to return ? */
1662 if ((sc->sc_composed_char > 0) &&
1663 (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1665 action = sc->sc_composed_char;
1666 sc->sc_composed_char = 0;
1668 if (action > 0xFF) {
1669 goto errkey;
1671 goto done;
1673 #ifdef UKBD_EMULATE_ATSCANCODE
1675 /* do we have a pending raw scan code? */
1677 if (sc->sc_mode == K_RAW) {
1678 scancode = sc->sc_buffered_char[0];
1679 if (scancode) {
1680 if (scancode & SCAN_PREFIX) {
1681 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1682 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1684 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1685 sc->sc_buffered_char[1] = 0;
1686 return (scancode);
1689 #endif /* UKBD_EMULATE_ATSCANCODE */
1691 /* see if there is something in the keyboard port */
1692 /* XXX */
1693 usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1694 if (usbcode == -1) {
1695 return (NOKEY);
1697 ++kbd->kb_count;
1699 #ifdef UKBD_EMULATE_ATSCANCODE
1700 /* USB key index -> key code -> AT scan code */
1701 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1702 if (keycode == NN) {
1703 return (NOKEY);
1705 /* return an AT scan code for the K_RAW mode */
1706 if (sc->sc_mode == K_RAW) {
1707 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1708 (usbcode & KEY_RELEASE)));
1710 #else /* !UKBD_EMULATE_ATSCANCODE */
1712 /* return the byte as is for the K_RAW mode */
1713 if (sc->sc_mode == K_RAW) {
1714 return (usbcode);
1716 /* USB key index -> key code */
1717 keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1718 if (keycode == NN) {
1719 return (NOKEY);
1721 #endif /* UKBD_EMULATE_ATSCANCODE */
1723 switch (keycode) {
1724 case 0x38: /* left alt (compose key) */
1725 if (usbcode & KEY_RELEASE) {
1726 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1727 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1729 if (sc->sc_composed_char > 0xFF) {
1730 sc->sc_composed_char = 0;
1733 } else {
1734 if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1735 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1736 sc->sc_composed_char = 0;
1739 break;
1740 /* XXX: I don't like these... */
1741 case 0x5c: /* print screen */
1742 if (sc->sc_flags & ALTS) {
1743 keycode = 0x54; /* sysrq */
1745 break;
1746 case 0x68: /* pause/break */
1747 if (sc->sc_flags & CTLS) {
1748 keycode = 0x6c; /* break */
1750 break;
1753 /* return the key code in the K_CODE mode */
1754 if (usbcode & KEY_RELEASE) {
1755 keycode |= SCAN_RELEASE;
1757 if (sc->sc_mode == K_CODE) {
1758 return (keycode);
1760 /* compose a character code */
1761 if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1762 switch (keycode) {
1763 /* key pressed, process it */
1764 case 0x47:
1765 case 0x48:
1766 case 0x49: /* keypad 7,8,9 */
1767 sc->sc_composed_char *= 10;
1768 sc->sc_composed_char += keycode - 0x40;
1769 goto check_composed;
1771 case 0x4B:
1772 case 0x4C:
1773 case 0x4D: /* keypad 4,5,6 */
1774 sc->sc_composed_char *= 10;
1775 sc->sc_composed_char += keycode - 0x47;
1776 goto check_composed;
1778 case 0x4F:
1779 case 0x50:
1780 case 0x51: /* keypad 1,2,3 */
1781 sc->sc_composed_char *= 10;
1782 sc->sc_composed_char += keycode - 0x4E;
1783 goto check_composed;
1785 case 0x52: /* keypad 0 */
1786 sc->sc_composed_char *= 10;
1787 goto check_composed;
1789 /* key released, no interest here */
1790 case SCAN_RELEASE | 0x47:
1791 case SCAN_RELEASE | 0x48:
1792 case SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
1793 case SCAN_RELEASE | 0x4B:
1794 case SCAN_RELEASE | 0x4C:
1795 case SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
1796 case SCAN_RELEASE | 0x4F:
1797 case SCAN_RELEASE | 0x50:
1798 case SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
1799 case SCAN_RELEASE | 0x52: /* keypad 0 */
1800 goto next_code;
1802 case 0x38: /* left alt key */
1803 break;
1805 default:
1806 if (sc->sc_composed_char > 0) {
1807 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1808 sc->sc_composed_char = 0;
1809 goto errkey;
1811 break;
1814 /* keycode to key action */
1815 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1816 (keycode & SCAN_RELEASE),
1817 &sc->sc_state, &sc->sc_accents);
1818 if (action == NOKEY) {
1819 goto next_code;
1821 done:
1822 return (action);
1824 check_composed:
1825 if (sc->sc_composed_char <= 0xFF) {
1826 goto next_code;
1828 errkey:
1829 return (ERRKEY);
1832 /* Currently wait is always false. */
1833 static uint32_t
1834 ukbd_read_char(keyboard_t *kbd, int wait)
1836 uint32_t keycode;
1837 #if 0
1838 struct ukbd_softc *sc = kbd->kb_data;
1840 UKBD_LOCK(sc);
1841 #endif
1842 keycode = ukbd_read_char_locked(kbd, wait);
1843 #if 0
1844 UKBD_UNLOCK(sc);
1845 #endif
1847 return (keycode);
1850 /* some useful control functions */
1851 static int
1852 ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
1854 struct ukbd_softc *sc = kbd->kb_data;
1855 int i;
1857 switch (cmd) {
1858 case KDGKBMODE: /* get keyboard mode */
1859 *(int *)arg = sc->sc_mode;
1860 break;
1861 case KDSKBMODE: /* set keyboard mode */
1862 switch (*(int *)arg) {
1863 case K_XLATE:
1864 if (sc->sc_mode != K_XLATE) {
1865 /* make lock key state and LED state match */
1866 sc->sc_state &= ~LOCK_MASK;
1867 sc->sc_state |= KBD_LED_VAL(kbd);
1869 /* FALLTHROUGH */
1870 case K_RAW:
1871 case K_CODE:
1872 if (sc->sc_mode != *(int *)arg) {
1873 if ((sc->sc_flags & UKBD_FLAG_POLLING) == 0)
1874 ukbd_clear_state(kbd);
1875 sc->sc_mode = *(int *)arg;
1877 break;
1878 default:
1879 return (EINVAL);
1881 break;
1883 case KDGETLED: /* get keyboard LED */
1884 *(int *)arg = KBD_LED_VAL(kbd);
1885 break;
1886 case KDSETLED: /* set keyboard LED */
1887 /* NOTE: lock key state in "sc_state" won't be changed */
1888 if (*(int *)arg & ~LOCK_MASK)
1889 return (EINVAL);
1891 i = *(int *)arg;
1893 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1894 if (sc->sc_mode == K_XLATE &&
1895 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1896 if (i & ALKED)
1897 i |= CLKED;
1898 else
1899 i &= ~CLKED;
1901 if (KBD_HAS_DEVICE(kbd))
1902 ukbd_set_leds(sc, i);
1904 KBD_LED_VAL(kbd) = *(int *)arg;
1905 break;
1906 case KDGKBSTATE: /* get lock key state */
1907 *(int *)arg = sc->sc_state & LOCK_MASK;
1908 break;
1909 case KDSKBSTATE: /* set lock key state */
1910 if (*(int *)arg & ~LOCK_MASK) {
1911 return (EINVAL);
1913 sc->sc_state &= ~LOCK_MASK;
1914 sc->sc_state |= *(int *)arg;
1916 /* set LEDs and quit */
1917 return (ukbd_ioctl(kbd, KDSETLED, arg));
1919 case KDSETREPEAT: /* set keyboard repeat rate (new
1920 * interface) */
1921 if (!KBD_HAS_DEVICE(kbd)) {
1922 return (0);
1924 if (((int *)arg)[1] < 0) {
1925 return (EINVAL);
1927 if (((int *)arg)[0] < 0) {
1928 return (EINVAL);
1930 if (((int *)arg)[0] < 200) /* fastest possible value */
1931 kbd->kb_delay1 = 200;
1932 else
1933 kbd->kb_delay1 = ((int *)arg)[0];
1934 kbd->kb_delay2 = ((int *)arg)[1];
1935 #ifdef EVDEV_SUPPORT
1936 if (sc->sc_evdev != NULL)
1937 evdev_push_repeats(sc->sc_evdev, kbd);
1938 #endif
1939 return (0);
1941 case PIO_KEYMAP: /* set keyboard translation table */
1942 case PIO_KEYMAPENT: /* set keyboard translation table
1943 * entry */
1944 case PIO_DEADKEYMAP: /* set accent key translation table */
1945 sc->sc_accents = 0;
1946 /* FALLTHROUGH */
1947 default:
1948 return (genkbd_commonioctl(kbd, cmd, arg));
1951 return (0);
1954 static int
1955 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1957 int result;
1958 struct ukbd_softc *sc = kbd->kb_data;
1961 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
1962 * context where printf(9) can be called, which among other things
1963 * includes interrupt filters and threads with any kinds of locks
1964 * already held. For this reason it would be dangerous to acquire
1965 * the Giant here unconditionally. On the other hand we have to
1966 * have it to handle the ioctl.
1967 * So we make our best effort to auto-detect whether we can grab
1968 * the Giant or not. Blame syscons(4) for this.
1970 switch (cmd) {
1971 case KDGKBSTATE:
1972 case KDSKBSTATE:
1973 case KDSETLED:
1974 if(!lockowned(&kbd->kb_lock)) {
1975 return (EDEADLK); /* best I could come up with */
1977 /* FALLTHROUGH */
1978 default:
1979 UKBD_LOCK(sc);
1980 result = ukbd_ioctl_locked(kbd, cmd, arg);
1981 UKBD_UNLOCK(sc);
1982 return (result);
1987 /* clear the internal state of the keyboard */
1988 static void
1989 ukbd_clear_state(keyboard_t *kbd)
1991 struct ukbd_softc *sc = kbd->kb_data;
1993 UKBD_CTX_LOCK_ASSERT();
1995 sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1996 sc->sc_state &= LOCK_MASK; /* preserve locking key state */
1997 sc->sc_accents = 0;
1998 sc->sc_composed_char = 0;
1999 #ifdef UKBD_EMULATE_ATSCANCODE
2000 sc->sc_buffered_char[0] = 0;
2001 sc->sc_buffered_char[1] = 0;
2002 #endif
2003 memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
2004 memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
2005 memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
2006 memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
2009 /* save the internal state, not used */
2010 static int
2011 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
2013 return (len == 0) ? 1 : -1;
2016 /* set the internal state, not used */
2017 static int
2018 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
2020 return (EINVAL);
2023 static int
2024 ukbd_poll(keyboard_t *kbd, int on)
2026 struct ukbd_softc *sc = kbd->kb_data;
2028 UKBD_LOCK(sc);
2029 if (on) {
2030 sc->sc_flags |= UKBD_FLAG_POLLING;
2031 sc->sc_poll_thread = curthread;
2032 } else {
2033 sc->sc_flags &= ~UKBD_FLAG_POLLING;
2034 ukbd_start_timer(sc); /* start timer */
2036 UKBD_UNLOCK(sc);
2038 return (0);
2041 /* local functions */
2043 static void
2044 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
2046 UKBD_LOCK_ASSERT();
2047 DPRINTF("leds=0x%02x\n", leds);
2049 sc->sc_leds = leds;
2050 sc->sc_flags |= UKBD_FLAG_SET_LEDS;
2052 /* start transfer, if not already started */
2054 usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
2057 #ifdef UKBD_EMULATE_ATSCANCODE
2058 static int
2059 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
2061 static const int scan[] = {
2062 /* 89 */
2063 0x11c, /* Enter */
2064 /* 90-99 */
2065 0x11d, /* Ctrl-R */
2066 0x135, /* Divide */
2067 0x137 | SCAN_PREFIX_SHIFT, /* PrintScreen */
2068 0x138, /* Alt-R */
2069 0x147, /* Home */
2070 0x148, /* Up */
2071 0x149, /* PageUp */
2072 0x14b, /* Left */
2073 0x14d, /* Right */
2074 0x14f, /* End */
2075 /* 100-109 */
2076 0x150, /* Down */
2077 0x151, /* PageDown */
2078 0x152, /* Insert */
2079 0x153, /* Delete */
2080 0x146, /* XXX Pause/Break */
2081 0x15b, /* Win_L(Super_L) */
2082 0x15c, /* Win_R(Super_R) */
2083 0x15d, /* Application(Menu) */
2085 /* SUN TYPE 6 USB KEYBOARD */
2086 0x168, /* Sun Type 6 Help */
2087 0x15e, /* Sun Type 6 Stop */
2088 /* 110 - 119 */
2089 0x15f, /* Sun Type 6 Again */
2090 0x160, /* Sun Type 6 Props */
2091 0x161, /* Sun Type 6 Undo */
2092 0x162, /* Sun Type 6 Front */
2093 0x163, /* Sun Type 6 Copy */
2094 0x164, /* Sun Type 6 Open */
2095 0x165, /* Sun Type 6 Paste */
2096 0x166, /* Sun Type 6 Find */
2097 0x167, /* Sun Type 6 Cut */
2098 0x125, /* Sun Type 6 Mute */
2099 /* 120 - 128 */
2100 0x11f, /* Sun Type 6 VolumeDown */
2101 0x11e, /* Sun Type 6 VolumeUp */
2102 0x120, /* Sun Type 6 PowerDown */
2104 /* Japanese 106/109 keyboard */
2105 0x73, /* Keyboard Intl' 1 (backslash / underscore) */
2106 0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
2107 0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
2108 0x79, /* Keyboard Intl' 4 (Henkan) */
2109 0x7b, /* Keyboard Intl' 5 (Muhenkan) */
2110 0x5c, /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
2113 if ((code >= 89) && (code < (int)(89 + NELEM(scan)))) {
2114 code = scan[code - 89];
2116 /* Pause/Break */
2117 if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
2118 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
2120 if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
2121 code &= ~SCAN_PREFIX_SHIFT;
2123 code |= (up ? SCAN_RELEASE : SCAN_PRESS);
2125 if (code & SCAN_PREFIX) {
2126 if (code & SCAN_PREFIX_CTL) {
2127 /* Ctrl */
2128 sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
2129 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
2130 } else if (code & SCAN_PREFIX_SHIFT) {
2131 /* Shift */
2132 sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
2133 sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
2134 } else {
2135 sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
2136 sc->sc_buffered_char[1] = 0;
2138 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
2140 return (code);
2144 #endif /* UKBD_EMULATE_ATSCANCODE */
2146 static keyboard_switch_t ukbdsw = {
2147 .probe = &ukbd__probe,
2148 .init = &ukbd_init,
2149 .term = &ukbd_term,
2150 .intr = &ukbd_intr,
2151 .test_if = &ukbd_test_if,
2152 .enable = &ukbd_enable,
2153 .disable = &ukbd_disable,
2154 .read = &ukbd_read,
2155 .check = &ukbd_check,
2156 .read_char = &ukbd_read_char,
2157 .check_char = &ukbd_check_char,
2158 .ioctl = &ukbd_ioctl,
2159 .lock = &ukbd_lock,
2160 .clear_state = &ukbd_clear_state,
2161 .get_state = &ukbd_get_state,
2162 .set_state = &ukbd_set_state,
2163 .get_fkeystr = &genkbd_get_fkeystr,
2164 .poll = &ukbd_poll,
2165 .diag = &genkbd_diag,
2168 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
2170 static int
2171 ukbd_driver_load(module_t mod, int what, void *arg)
2173 switch (what) {
2174 case MOD_LOAD:
2175 kbd_add_driver(&ukbd_kbd_driver);
2176 break;
2177 case MOD_UNLOAD:
2178 kbd_delete_driver(&ukbd_kbd_driver);
2179 break;
2181 return (0);
2184 static devclass_t ukbd_devclass;
2186 static device_method_t ukbd_methods[] = {
2187 DEVMETHOD(device_probe, ukbd_probe),
2188 DEVMETHOD(device_attach, ukbd_attach),
2189 DEVMETHOD(device_detach, ukbd_detach),
2190 DEVMETHOD(device_resume, ukbd_resume),
2191 DEVMETHOD_END
2194 static driver_t ukbd_driver = {
2195 .name = "ukbd",
2196 .methods = ukbd_methods,
2197 .size = sizeof(struct ukbd_softc),
2200 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, NULL);
2201 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
2202 #ifdef EVDEV_SUPPORT
2203 MODULE_DEPEND(ukbd, evdev, 1, 1, 1);
2204 #endif
2205 MODULE_VERSION(ukbd, 1);