xref: /freebsd/sys/dev/usb/input/ukbd.c (revision 841c0c7ec75bef3c9920cd811270f9f84791ee04)
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3 
4 
5 /*-
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
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.
21  *
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.
33  *
34  */
35 
36 /*
37  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
38  */
39 
40 #include "opt_compat.h"
41 #include "opt_kbd.h"
42 #include "opt_ukbd.h"
43 
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/bus.h>
52 #include <sys/linker_set.h>
53 #include <sys/module.h>
54 #include <sys/lock.h>
55 #include <sys/mutex.h>
56 #include <sys/condvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/sx.h>
59 #include <sys/unistd.h>
60 #include <sys/callout.h>
61 #include <sys/malloc.h>
62 #include <sys/priv.h>
63 #include <sys/kdb.h>
64 
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68 #include <dev/usb/usbhid.h>
69 
70 #define	USB_DEBUG_VAR ukbd_debug
71 #include <dev/usb/usb_debug.h>
72 
73 #include <dev/usb/quirk/usb_quirk.h>
74 
75 #include <sys/ioccom.h>
76 #include <sys/filio.h>
77 #include <sys/tty.h>
78 #include <sys/kbio.h>
79 
80 #include <dev/kbd/kbdreg.h>
81 
82 /* the initial key map, accent map and fkey strings */
83 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
84 #define	KBD_DFLT_KEYMAP
85 #include "ukbdmap.h"
86 #endif
87 
88 /* the following file must be included after "ukbdmap.h" */
89 #include <dev/kbd/kbdtables.h>
90 
91 #if USB_DEBUG
92 static int ukbd_debug = 0;
93 static int ukbd_no_leds = 0;
94 
95 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
96 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
97     &ukbd_debug, 0, "Debug level");
98 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW,
99     &ukbd_no_leds, 0, "Disables setting of keyboard leds");
100 
101 TUNABLE_INT("hw.usb.ukbd.debug", &ukbd_debug);
102 TUNABLE_INT("hw.usb.ukbd.no_leds", &ukbd_no_leds);
103 #endif
104 
105 #define	UPROTO_BOOT_KEYBOARD 1
106 
107 #define	UKBD_EMULATE_ATSCANCODE	       1
108 #define	UKBD_DRIVER_NAME          "ukbd"
109 #define	UKBD_NMOD                     8	/* units */
110 #define	UKBD_NKEYCODE                 6	/* units */
111 #define	UKBD_IN_BUF_SIZE  (2*(UKBD_NMOD + (2*UKBD_NKEYCODE)))	/* bytes */
112 #define	UKBD_IN_BUF_FULL  (UKBD_IN_BUF_SIZE / 2)	/* bytes */
113 #define	UKBD_NFKEY        (sizeof(fkey_tab)/sizeof(fkey_tab[0]))	/* units */
114 
115 struct ukbd_data {
116 	uint8_t	modifiers;
117 #define	MOD_CONTROL_L	0x01
118 #define	MOD_CONTROL_R	0x10
119 #define	MOD_SHIFT_L	0x02
120 #define	MOD_SHIFT_R	0x20
121 #define	MOD_ALT_L	0x04
122 #define	MOD_ALT_R	0x40
123 #define	MOD_WIN_L	0x08
124 #define	MOD_WIN_R	0x80
125 	uint8_t	reserved;
126 	uint8_t	keycode[UKBD_NKEYCODE];
127 	uint8_t exten[8];
128 };
129 
130 enum {
131 	UKBD_INTR_DT,
132 	UKBD_CTRL_LED,
133 	UKBD_N_TRANSFER,
134 };
135 
136 struct ukbd_softc {
137 	keyboard_t sc_kbd;
138 	keymap_t sc_keymap;
139 	accentmap_t sc_accmap;
140 	fkeytab_t sc_fkeymap[UKBD_NFKEY];
141 	struct hid_location sc_loc_apple_eject;
142 	struct hid_location sc_loc_apple_fn;
143 	struct usb_callout sc_callout;
144 	struct ukbd_data sc_ndata;
145 	struct ukbd_data sc_odata;
146 
147 	struct thread *sc_poll_thread;
148 	struct usb_device *sc_udev;
149 	struct usb_interface *sc_iface;
150 	struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
151 
152 	uint32_t sc_ntime[UKBD_NKEYCODE];
153 	uint32_t sc_otime[UKBD_NKEYCODE];
154 	uint32_t sc_input[UKBD_IN_BUF_SIZE];	/* input buffer */
155 	uint32_t sc_time_ms;
156 	uint32_t sc_composed_char;	/* composed char code, if non-zero */
157 #ifdef UKBD_EMULATE_ATSCANCODE
158 	uint32_t sc_buffered_char[2];
159 #endif
160 	uint32_t sc_flags;		/* flags */
161 #define	UKBD_FLAG_COMPOSE	0x0001
162 #define	UKBD_FLAG_POLLING	0x0002
163 #define	UKBD_FLAG_SET_LEDS	0x0004
164 #define	UKBD_FLAG_ATTACHED	0x0010
165 #define	UKBD_FLAG_GONE		0x0020
166 #define	UKBD_FLAG_APPLE_EJECT	0x0040
167 #define	UKBD_FLAG_APPLE_FN	0x0080
168 #define	UKBD_FLAG_APPLE_SWAP	0x0100
169 #define	UKBD_FLAG_TIMER_RUNNING	0x0200
170 
171 	int	sc_mode;		/* input mode (K_XLATE,K_RAW,K_CODE) */
172 	int	sc_state;		/* shift/lock key state */
173 	int	sc_accents;		/* accent key index (> 0) */
174 	int	sc_poll_tick_last;
175 
176 	uint16_t sc_inputs;
177 	uint16_t sc_inputhead;
178 	uint16_t sc_inputtail;
179 
180 	uint8_t	sc_leds;		/* store for async led requests */
181 	uint8_t	sc_iface_index;
182 	uint8_t	sc_iface_no;
183 	uint8_t sc_kbd_id;
184 	uint8_t sc_led_id;
185 	uint8_t sc_poll_detected;
186 };
187 
188 #define	KEY_ERROR	  0x01
189 
190 #define	KEY_PRESS	  0
191 #define	KEY_RELEASE	  0x400
192 #define	KEY_INDEX(c)	  ((c) & 0xFF)
193 
194 #define	SCAN_PRESS	  0
195 #define	SCAN_RELEASE	  0x80
196 #define	SCAN_PREFIX_E0	  0x100
197 #define	SCAN_PREFIX_E1	  0x200
198 #define	SCAN_PREFIX_CTL	  0x400
199 #define	SCAN_PREFIX_SHIFT 0x800
200 #define	SCAN_PREFIX	(SCAN_PREFIX_E0  | SCAN_PREFIX_E1 | \
201 			 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
202 #define	SCAN_CHAR(c)	((c) & 0x7f)
203 
204 struct ukbd_mods {
205 	uint32_t mask, key;
206 };
207 
208 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
209 	{MOD_CONTROL_L, 0xe0},
210 	{MOD_CONTROL_R, 0xe4},
211 	{MOD_SHIFT_L, 0xe1},
212 	{MOD_SHIFT_R, 0xe5},
213 	{MOD_ALT_L, 0xe2},
214 	{MOD_ALT_R, 0xe6},
215 	{MOD_WIN_L, 0xe3},
216 	{MOD_WIN_R, 0xe7},
217 };
218 
219 #define	NN 0				/* no translation */
220 /*
221  * Translate USB keycodes to AT keyboard scancodes.
222  */
223 /*
224  * FIXME: Mac USB keyboard generates:
225  * 0x53: keypad NumLock/Clear
226  * 0x66: Power
227  * 0x67: keypad =
228  * 0x68: F13
229  * 0x69: F14
230  * 0x6a: F15
231  */
232 static const uint8_t ukbd_trtab[256] = {
233 	0, 0, 0, 0, 30, 48, 46, 32,	/* 00 - 07 */
234 	18, 33, 34, 35, 23, 36, 37, 38,	/* 08 - 0F */
235 	50, 49, 24, 25, 16, 19, 31, 20,	/* 10 - 17 */
236 	22, 47, 17, 45, 21, 44, 2, 3,	/* 18 - 1F */
237 	4, 5, 6, 7, 8, 9, 10, 11,	/* 20 - 27 */
238 	28, 1, 14, 15, 57, 12, 13, 26,	/* 28 - 2F */
239 	27, 43, 43, 39, 40, 41, 51, 52,	/* 30 - 37 */
240 	53, 58, 59, 60, 61, 62, 63, 64,	/* 38 - 3F */
241 	65, 66, 67, 68, 87, 88, 92, 70,	/* 40 - 47 */
242 	104, 102, 94, 96, 103, 99, 101, 98,	/* 48 - 4F */
243 	97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
244 	89, 79, 80, 81, 75, 76, 77, 71,	/* 58 - 5F */
245 	72, 73, 82, 83, 86, 107, 122, NN,	/* 60 - 67 */
246 	NN, NN, NN, NN, NN, NN, NN, NN,	/* 68 - 6F */
247 	NN, NN, NN, NN, 115, 108, 111, 113,	/* 70 - 77 */
248 	109, 110, 112, 118, 114, 116, 117, 119,	/* 78 - 7F */
249 	121, 120, NN, NN, NN, NN, NN, 123,	/* 80 - 87 */
250 	124, 125, 126, 127, 128, NN, NN, NN,	/* 88 - 8F */
251 	NN, NN, NN, NN, NN, NN, NN, NN,	/* 90 - 97 */
252 	NN, NN, NN, NN, NN, NN, NN, NN,	/* 98 - 9F */
253 	NN, NN, NN, NN, NN, NN, NN, NN,	/* A0 - A7 */
254 	NN, NN, NN, NN, NN, NN, NN, NN,	/* A8 - AF */
255 	NN, NN, NN, NN, NN, NN, NN, NN,	/* B0 - B7 */
256 	NN, NN, NN, NN, NN, NN, NN, NN,	/* B8 - BF */
257 	NN, NN, NN, NN, NN, NN, NN, NN,	/* C0 - C7 */
258 	NN, NN, NN, NN, NN, NN, NN, NN,	/* C8 - CF */
259 	NN, NN, NN, NN, NN, NN, NN, NN,	/* D0 - D7 */
260 	NN, NN, NN, NN, NN, NN, NN, NN,	/* D8 - DF */
261 	29, 42, 56, 105, 90, 54, 93, 106,	/* E0 - E7 */
262 	NN, NN, NN, NN, NN, NN, NN, NN,	/* E8 - EF */
263 	NN, NN, NN, NN, NN, NN, NN, NN,	/* F0 - F7 */
264 	NN, NN, NN, NN, NN, NN, NN, NN,	/* F8 - FF */
265 };
266 
267 /* prototypes */
268 static void	ukbd_timeout(void *);
269 static void	ukbd_set_leds(struct ukbd_softc *, uint8_t);
270 static int	ukbd_set_typematic(keyboard_t *, int);
271 #ifdef UKBD_EMULATE_ATSCANCODE
272 static int	ukbd_key2scan(struct ukbd_softc *, int, int, int);
273 #endif
274 static uint32_t	ukbd_read_char(keyboard_t *, int);
275 static void	ukbd_clear_state(keyboard_t *);
276 static int	ukbd_ioctl(keyboard_t *, u_long, caddr_t);
277 static int	ukbd_enable(keyboard_t *);
278 static int	ukbd_disable(keyboard_t *);
279 static void	ukbd_interrupt(struct ukbd_softc *);
280 static int	ukbd_is_polling(struct ukbd_softc *);
281 static int	ukbd_polls_other_thread(struct ukbd_softc *);
282 static void	ukbd_event_keyinput(struct ukbd_softc *);
283 
284 static device_probe_t ukbd_probe;
285 static device_attach_t ukbd_attach;
286 static device_detach_t ukbd_detach;
287 static device_resume_t ukbd_resume;
288 
289 static uint8_t
290 ukbd_any_key_pressed(struct ukbd_softc *sc)
291 {
292 	uint8_t i;
293 	uint8_t j;
294 
295 	for (j = i = 0; i < UKBD_NKEYCODE; i++)
296 		j |= sc->sc_odata.keycode[i];
297 
298 	return (j ? 1 : 0);
299 }
300 
301 static void
302 ukbd_start_timer(struct ukbd_softc *sc)
303 {
304 	sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
305 	usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
306 }
307 
308 static void
309 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
310 {
311 	mtx_assert(&Giant, MA_OWNED);
312 
313 	DPRINTF("0x%02x (%d) %s\n", key, key,
314 	    (key & KEY_RELEASE) ? "released" : "pressed");
315 
316 	if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
317 		sc->sc_input[sc->sc_inputtail] = key;
318 		++(sc->sc_inputs);
319 		++(sc->sc_inputtail);
320 		if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
321 			sc->sc_inputtail = 0;
322 		}
323 	} else {
324 		DPRINTF("input buffer is full\n");
325 	}
326 }
327 
328 static void
329 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
330 {
331 	DPRINTFN(2, "polling\n");
332 
333 	/* update stats about last polling event */
334 	sc->sc_poll_tick_last = ticks;
335 	sc->sc_poll_detected = 1;
336 
337 	if (kdb_active == 0) {
338 		while (sc->sc_inputs == 0) {
339 			/* make sure the USB code gets a chance to run */
340 			pause("UKBD", 1);
341 
342 			/* check if we should wait */
343 			if (!wait)
344 				break;
345 		}
346 		return;		/* Only poll if KDB is active */
347 	}
348 
349 	while (sc->sc_inputs == 0) {
350 
351 		usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
352 
353 		/* Delay-optimised support for repetition of keys */
354 
355 		if (ukbd_any_key_pressed(sc)) {
356 			/* a key is pressed - need timekeeping */
357 			DELAY(1000);
358 
359 			/* 1 millisecond has passed */
360 			sc->sc_time_ms += 1;
361 		}
362 
363 		ukbd_interrupt(sc);
364 
365 		if (!wait)
366 			break;
367 	}
368 }
369 
370 static int32_t
371 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
372 {
373 	int32_t c;
374 
375 	mtx_assert(&Giant, MA_OWNED);
376 
377 	if (sc->sc_inputs == 0) {
378 		/* start transfer, if not already started */
379 		usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
380 	}
381 
382 	if (ukbd_polls_other_thread(sc))
383 		return (-1);
384 
385 	if (sc->sc_flags & UKBD_FLAG_POLLING)
386 		ukbd_do_poll(sc, wait);
387 
388 	if (sc->sc_inputs == 0) {
389 		c = -1;
390 	} else {
391 		c = sc->sc_input[sc->sc_inputhead];
392 		--(sc->sc_inputs);
393 		++(sc->sc_inputhead);
394 		if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
395 			sc->sc_inputhead = 0;
396 		}
397 	}
398 	return (c);
399 }
400 
401 static void
402 ukbd_interrupt(struct ukbd_softc *sc)
403 {
404 	uint32_t n_mod;
405 	uint32_t o_mod;
406 	uint32_t now = sc->sc_time_ms;
407 	uint32_t dtime;
408 	uint8_t key;
409 	uint8_t i;
410 	uint8_t j;
411 
412 	if (sc->sc_ndata.keycode[0] == KEY_ERROR)
413 		return;
414 
415 	n_mod = sc->sc_ndata.modifiers;
416 	o_mod = sc->sc_odata.modifiers;
417 	if (n_mod != o_mod) {
418 		for (i = 0; i < UKBD_NMOD; i++) {
419 			if ((n_mod & ukbd_mods[i].mask) !=
420 			    (o_mod & ukbd_mods[i].mask)) {
421 				ukbd_put_key(sc, ukbd_mods[i].key |
422 				    ((n_mod & ukbd_mods[i].mask) ?
423 				    KEY_PRESS : KEY_RELEASE));
424 			}
425 		}
426 	}
427 	/* Check for released keys. */
428 	for (i = 0; i < UKBD_NKEYCODE; i++) {
429 		key = sc->sc_odata.keycode[i];
430 		if (key == 0) {
431 			continue;
432 		}
433 		for (j = 0; j < UKBD_NKEYCODE; j++) {
434 			if (sc->sc_ndata.keycode[j] == 0) {
435 				continue;
436 			}
437 			if (key == sc->sc_ndata.keycode[j]) {
438 				goto rfound;
439 			}
440 		}
441 		ukbd_put_key(sc, key | KEY_RELEASE);
442 rfound:	;
443 	}
444 
445 	/* Check for pressed keys. */
446 	for (i = 0; i < UKBD_NKEYCODE; i++) {
447 		key = sc->sc_ndata.keycode[i];
448 		if (key == 0) {
449 			continue;
450 		}
451 		sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
452 		for (j = 0; j < UKBD_NKEYCODE; j++) {
453 			if (sc->sc_odata.keycode[j] == 0) {
454 				continue;
455 			}
456 			if (key == sc->sc_odata.keycode[j]) {
457 
458 				/* key is still pressed */
459 
460 				sc->sc_ntime[i] = sc->sc_otime[j];
461 				dtime = (sc->sc_otime[j] - now);
462 
463 				if (!(dtime & 0x80000000)) {
464 					/* time has not elapsed */
465 					goto pfound;
466 				}
467 				sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
468 				break;
469 			}
470 		}
471 		ukbd_put_key(sc, key | KEY_PRESS);
472 
473 		/*
474                  * If any other key is presently down, force its repeat to be
475                  * well in the future (100s).  This makes the last key to be
476                  * pressed do the autorepeat.
477                  */
478 		for (j = 0; j != UKBD_NKEYCODE; j++) {
479 			if (j != i)
480 				sc->sc_ntime[j] = now + (100 * 1000);
481 		}
482 pfound:	;
483 	}
484 
485 	sc->sc_odata = sc->sc_ndata;
486 
487 	memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
488 
489 	ukbd_event_keyinput(sc);
490 }
491 
492 static void
493 ukbd_event_keyinput(struct ukbd_softc *sc)
494 {
495 	int c;
496 
497 	if (ukbd_is_polling(sc))
498 		return;
499 
500 	if (sc->sc_inputs == 0)
501 		return;
502 
503 	if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
504 	    KBD_IS_BUSY(&sc->sc_kbd)) {
505 		/* let the callback function process the input */
506 		(sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
507 		    sc->sc_kbd.kb_callback.kc_arg);
508 	} else {
509 		/* read and discard the input, no one is waiting for it */
510 		do {
511 			c = ukbd_read_char(&sc->sc_kbd, 0);
512 		} while (c != NOKEY);
513 	}
514 }
515 
516 static void
517 ukbd_timeout(void *arg)
518 {
519 	struct ukbd_softc *sc = arg;
520 
521 	mtx_assert(&Giant, MA_OWNED);
522 
523 	sc->sc_time_ms += 25;	/* milliseconds */
524 
525 	ukbd_interrupt(sc);
526 
527 	/* Make sure any leftover key events gets read out */
528 	ukbd_event_keyinput(sc);
529 
530 	if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
531 		ukbd_start_timer(sc);
532 	} else {
533 		sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
534 	}
535 }
536 
537 static uint8_t
538 ukbd_apple_fn(uint8_t keycode) {
539 	switch (keycode) {
540 	case 0x28: return 0x49; /* RETURN -> INSERT */
541 	case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
542 	case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
543 	case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
544 	case 0x52: return 0x4b; /* UP ARROW -> PGUP */
545 	case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
546 	default: return keycode;
547 	}
548 }
549 
550 static uint8_t
551 ukbd_apple_swap(uint8_t keycode) {
552 	switch (keycode) {
553 	case 0x35: return 0x64;
554 	case 0x64: return 0x35;
555 	default: return keycode;
556 	}
557 }
558 
559 static void
560 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
561 {
562 	struct ukbd_softc *sc = usbd_xfer_softc(xfer);
563 	struct usb_page_cache *pc;
564 	uint8_t i;
565 	uint8_t offset;
566 	uint8_t id;
567 	uint8_t apple_fn;
568 	uint8_t apple_eject;
569 	int len;
570 
571 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
572 	pc = usbd_xfer_get_frame(xfer, 0);
573 
574 	switch (USB_GET_STATE(xfer)) {
575 	case USB_ST_TRANSFERRED:
576 		DPRINTF("actlen=%d bytes\n", len);
577 
578 		if (len == 0) {
579 			DPRINTF("zero length data\n");
580 			goto tr_setup;
581 		}
582 
583 		if (sc->sc_kbd_id != 0) {
584 			/* check and remove HID ID byte */
585 			usbd_copy_out(pc, 0, &id, 1);
586 			if (id != sc->sc_kbd_id) {
587 				DPRINTF("wrong HID ID\n");
588 				goto tr_setup;
589 			}
590 			offset = 1;
591 			len--;
592 		} else {
593 			offset = 0;
594 		}
595 
596 		if (len > sizeof(sc->sc_ndata)) {
597 			len = sizeof(sc->sc_ndata);
598 		}
599 
600 		if (len) {
601 			memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
602 			usbd_copy_out(pc, offset, &sc->sc_ndata, len);
603 
604 			if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
605 			    hid_get_data((uint8_t *)&sc->sc_ndata,
606 				len, &sc->sc_loc_apple_eject))
607 				apple_eject = 1;
608 			else
609 				apple_eject = 0;
610 
611 			if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
612 			    hid_get_data((uint8_t *)&sc->sc_ndata,
613 				len, &sc->sc_loc_apple_fn))
614 				apple_fn = 1;
615 			else
616 				apple_fn = 0;
617 #if USB_DEBUG
618 			DPRINTF("apple_eject=%u apple_fn=%u\n",
619 			    apple_eject, apple_fn);
620 
621 			if (sc->sc_ndata.modifiers) {
622 				DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers);
623 			}
624 			for (i = 0; i < UKBD_NKEYCODE; i++) {
625 				if (sc->sc_ndata.keycode[i]) {
626 					DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]);
627 				}
628 			}
629 #endif					/* USB_DEBUG */
630 
631 			if (apple_fn) {
632 				for (i = 0; i < UKBD_NKEYCODE; i++) {
633 					sc->sc_ndata.keycode[i] =
634 					    ukbd_apple_fn(sc->sc_ndata.keycode[i]);
635 				}
636 			}
637 
638 			if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
639 				for (i = 0; i < UKBD_NKEYCODE; i++) {
640 					sc->sc_ndata.keycode[i] =
641 					    ukbd_apple_swap(sc->sc_ndata.keycode[i]);
642 				}
643 			}
644 
645 			ukbd_interrupt(sc);
646 
647 			if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
648 				if (ukbd_any_key_pressed(sc)) {
649 					ukbd_start_timer(sc);
650 				}
651 			}
652 		}
653 	case USB_ST_SETUP:
654 tr_setup:
655 		if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
656 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
657 			usbd_transfer_submit(xfer);
658 		} else {
659 			DPRINTF("input queue is full!\n");
660 		}
661 		break;
662 
663 	default:			/* Error */
664 		DPRINTF("error=%s\n", usbd_errstr(error));
665 
666 		if (error != USB_ERR_CANCELLED) {
667 			/* try to clear stall first */
668 			usbd_xfer_set_stall(xfer);
669 			goto tr_setup;
670 		}
671 		break;
672 	}
673 }
674 
675 static void
676 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
677 {
678 	struct usb_device_request req;
679 	struct usb_page_cache *pc;
680 	uint8_t buf[2];
681 	struct ukbd_softc *sc = usbd_xfer_softc(xfer);
682 
683 #if USB_DEBUG
684 	if (ukbd_no_leds)
685 		return;
686 #endif
687 
688 	switch (USB_GET_STATE(xfer)) {
689 	case USB_ST_TRANSFERRED:
690 	case USB_ST_SETUP:
691 		if (sc->sc_flags & UKBD_FLAG_SET_LEDS) {
692 			sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
693 
694 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
695 			req.bRequest = UR_SET_REPORT;
696 			USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
697 			req.wIndex[0] = sc->sc_iface_no;
698 			req.wIndex[1] = 0;
699 			req.wLength[1] = 0;
700 
701 			/* check if we need to prefix an ID byte */
702 			if (sc->sc_led_id != 0) {
703 				req.wLength[0] = 2;
704 				buf[0] = sc->sc_led_id;
705 				buf[1] = sc->sc_leds;
706 			} else {
707 				req.wLength[0] = 1;
708 				buf[0] = sc->sc_leds;
709 				buf[1] = 0;
710 			}
711 
712 			pc = usbd_xfer_get_frame(xfer, 0);
713 			usbd_copy_in(pc, 0, &req, sizeof(req));
714 			pc = usbd_xfer_get_frame(xfer, 1);
715 			usbd_copy_in(pc, 0, buf, sizeof(buf));
716 
717 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
718 			usbd_xfer_set_frame_len(xfer, 1, req.wLength[0]);
719 			usbd_xfer_set_frames(xfer, 2);
720 			usbd_transfer_submit(xfer);
721 		}
722 		break;
723 
724 	default:			/* Error */
725 		DPRINTFN(0, "error=%s\n", usbd_errstr(error));
726 		break;
727 	}
728 }
729 
730 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
731 
732 	[UKBD_INTR_DT] = {
733 		.type = UE_INTERRUPT,
734 		.endpoint = UE_ADDR_ANY,
735 		.direction = UE_DIR_IN,
736 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
737 		.bufsize = 0,	/* use wMaxPacketSize */
738 		.callback = &ukbd_intr_callback,
739 	},
740 
741 	[UKBD_CTRL_LED] = {
742 		.type = UE_CONTROL,
743 		.endpoint = 0x00,	/* Control pipe */
744 		.direction = UE_DIR_ANY,
745 		.bufsize = sizeof(struct usb_device_request) + 8,
746 		.callback = &ukbd_set_leds_callback,
747 		.timeout = 1000,	/* 1 second */
748 	},
749 };
750 
751 static int
752 ukbd_probe(device_t dev)
753 {
754 	keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
755 	struct usb_attach_arg *uaa = device_get_ivars(dev);
756 	void *d_ptr;
757 	int error;
758 	uint16_t d_len;
759 
760 	DPRINTFN(11, "\n");
761 
762 	if (sw == NULL) {
763 		return (ENXIO);
764 	}
765 	if (uaa->usb_mode != USB_MODE_HOST) {
766 		return (ENXIO);
767 	}
768 
769 	if (uaa->info.bInterfaceClass != UICLASS_HID)
770 		return (ENXIO);
771 
772 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
773 	    (uaa->info.bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)) {
774 		if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
775 			return (ENXIO);
776 		else
777 			return (BUS_PROBE_GENERIC);
778 	}
779 
780 	error = usbd_req_get_hid_desc(uaa->device, NULL,
781 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
782 
783 	if (error)
784 		return (ENXIO);
785 
786 	/*
787 	 * NOTE: we currently don't support USB mouse and USB keyboard
788 	 * on the same USB endpoint.
789 	 */
790 	if (hid_is_collection(d_ptr, d_len,
791 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
792 		/* most likely a mouse */
793 		error = ENXIO;
794 	} else if (hid_is_collection(d_ptr, d_len,
795 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
796 		if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
797 			error = ENXIO;
798 		else
799 			error = BUS_PROBE_GENERIC;
800 	} else
801 		error = ENXIO;
802 
803 	free(d_ptr, M_TEMP);
804 	return (error);
805 }
806 
807 static int
808 ukbd_attach(device_t dev)
809 {
810 	struct ukbd_softc *sc = device_get_softc(dev);
811 	struct usb_attach_arg *uaa = device_get_ivars(dev);
812 	int32_t unit = device_get_unit(dev);
813 	keyboard_t *kbd = &sc->sc_kbd;
814 	void *hid_ptr = NULL;
815 	usb_error_t err;
816 	uint32_t flags;
817 	uint16_t n;
818 	uint16_t hid_len;
819 
820 	kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
821 
822 	kbd->kb_data = (void *)sc;
823 
824 	device_set_usb_desc(dev);
825 
826 	sc->sc_udev = uaa->device;
827 	sc->sc_iface = uaa->iface;
828 	sc->sc_iface_index = uaa->info.bIfaceIndex;
829 	sc->sc_iface_no = uaa->info.bIfaceNum;
830 	sc->sc_mode = K_XLATE;
831 
832 	usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
833 
834 	err = usbd_transfer_setup(uaa->device,
835 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
836 	    UKBD_N_TRANSFER, sc, &Giant);
837 
838 	if (err) {
839 		DPRINTF("error=%s\n", usbd_errstr(err));
840 		goto detach;
841 	}
842 	/* setup default keyboard maps */
843 
844 	sc->sc_keymap = key_map;
845 	sc->sc_accmap = accent_map;
846 	for (n = 0; n < UKBD_NFKEY; n++) {
847 		sc->sc_fkeymap[n] = fkey_tab[n];
848 	}
849 
850 	kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
851 	    sc->sc_fkeymap, UKBD_NFKEY);
852 
853 	KBD_FOUND_DEVICE(kbd);
854 
855 	ukbd_clear_state(kbd);
856 
857 	/*
858 	 * FIXME: set the initial value for lock keys in "sc_state"
859 	 * according to the BIOS data?
860 	 */
861 	KBD_PROBE_DONE(kbd);
862 
863 	/*
864 	 * Set boot protocol if we need the quirk.
865 	 */
866 	if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO)) {
867 		err = usbd_req_set_protocol(sc->sc_udev, NULL,
868 			sc->sc_iface_index, 0);
869 		if (err != USB_ERR_NORMAL_COMPLETION) {
870 			DPRINTF("set protocol error=%s\n", usbd_errstr(err));
871 			goto detach;
872 		}
873 	}
874 
875 	/* figure out if there is an ID byte in the data */
876 	err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
877 	    &hid_len, M_TEMP, uaa->info.bIfaceIndex);
878 	if (err == 0) {
879 		uint8_t temp_id;
880 
881 		/* investigate if this is an Apple Keyboard */
882 		if (hid_locate(hid_ptr, hid_len,
883 		    HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
884 		    hid_input, 0, &sc->sc_loc_apple_eject, &flags,
885 		    &sc->sc_kbd_id)) {
886 			if (flags & HIO_VARIABLE)
887 				sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
888 				    UKBD_FLAG_APPLE_SWAP;
889 			if (hid_locate(hid_ptr, hid_len,
890 			    HID_USAGE2(0xFFFF, 0x0003),
891 			    hid_input, 0, &sc->sc_loc_apple_fn, &flags,
892 			    &temp_id)) {
893 				if (flags & HIO_VARIABLE)
894 					sc->sc_flags |= UKBD_FLAG_APPLE_FN |
895 					    UKBD_FLAG_APPLE_SWAP;
896 				if (temp_id != sc->sc_kbd_id) {
897 					DPRINTF("HID IDs mismatch\n");
898 				}
899 			}
900 		} else {
901 			/*
902 			 * Assume the first HID ID contains the
903 			 * keyboard data
904 			 */
905 			hid_report_size(hid_ptr, hid_len,
906 			    hid_input, &sc->sc_kbd_id);
907 		}
908 
909 		/* investigate if we need an ID-byte for the leds */
910 		hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id);
911 
912 		free(hid_ptr, M_TEMP);
913 	}
914 
915 	/* ignore if SETIDLE fails, hence it is not crucial */
916 	err = usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
917 
918 	mtx_lock(&Giant);
919 
920 	ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
921 
922 	KBD_INIT_DONE(kbd);
923 
924 	mtx_unlock(&Giant);
925 
926 	if (kbd_register(kbd) < 0) {
927 		goto detach;
928 	}
929 	KBD_CONFIG_DONE(kbd);
930 
931 	ukbd_enable(kbd);
932 
933 #ifdef KBD_INSTALL_CDEV
934 	if (kbd_attach(kbd)) {
935 		goto detach;
936 	}
937 #endif
938 	sc->sc_flags |= UKBD_FLAG_ATTACHED;
939 
940 	if (bootverbose) {
941 		genkbd_diag(kbd, bootverbose);
942 	}
943 	/* lock keyboard mutex */
944 
945 	mtx_lock(&Giant);
946 
947 	/* start the keyboard */
948 
949 	usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
950 
951 	mtx_unlock(&Giant);
952 	return (0);			/* success */
953 
954 detach:
955 	ukbd_detach(dev);
956 	return (ENXIO);			/* error */
957 }
958 
959 static int
960 ukbd_detach(device_t dev)
961 {
962 	struct ukbd_softc *sc = device_get_softc(dev);
963 	int error;
964 
965 	DPRINTF("\n");
966 
967 	mtx_lock(&Giant);
968 
969 	sc->sc_flags |= UKBD_FLAG_GONE;
970 
971 	usb_callout_stop(&sc->sc_callout);
972 
973 	ukbd_disable(&sc->sc_kbd);
974 
975 #ifdef KBD_INSTALL_CDEV
976 	if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
977 		error = kbd_detach(&sc->sc_kbd);
978 		if (error) {
979 			/* usb attach cannot return an error */
980 			device_printf(dev, "WARNING: kbd_detach() "
981 			    "returned non-zero! (ignored)\n");
982 		}
983 	}
984 #endif
985 	if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
986 		error = kbd_unregister(&sc->sc_kbd);
987 		if (error) {
988 			/* usb attach cannot return an error */
989 			device_printf(dev, "WARNING: kbd_unregister() "
990 			    "returned non-zero! (ignored)\n");
991 		}
992 	}
993 	sc->sc_kbd.kb_flags = 0;
994 
995 	mtx_unlock(&Giant);
996 
997 	usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
998 
999 	usb_callout_drain(&sc->sc_callout);
1000 
1001 	DPRINTF("%s: disconnected\n",
1002 	    device_get_nameunit(dev));
1003 
1004 	return (0);
1005 }
1006 
1007 static int
1008 ukbd_resume(device_t dev)
1009 {
1010 	struct ukbd_softc *sc = device_get_softc(dev);
1011 
1012 	mtx_lock(&Giant);
1013 
1014 	ukbd_clear_state(&sc->sc_kbd);
1015 
1016 	mtx_unlock(&Giant);
1017 
1018 	return (0);
1019 }
1020 
1021 /* early keyboard probe, not supported */
1022 static int
1023 ukbd_configure(int flags)
1024 {
1025 	return (0);
1026 }
1027 
1028 /* detect a keyboard, not used */
1029 static int
1030 ukbd__probe(int unit, void *arg, int flags)
1031 {
1032 	return (ENXIO);
1033 }
1034 
1035 /* reset and initialize the device, not used */
1036 static int
1037 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1038 {
1039 	return (ENXIO);
1040 }
1041 
1042 /* test the interface to the device, not used */
1043 static int
1044 ukbd_test_if(keyboard_t *kbd)
1045 {
1046 	return (0);
1047 }
1048 
1049 /* finish using this keyboard, not used */
1050 static int
1051 ukbd_term(keyboard_t *kbd)
1052 {
1053 	return (ENXIO);
1054 }
1055 
1056 /* keyboard interrupt routine, not used */
1057 static int
1058 ukbd_intr(keyboard_t *kbd, void *arg)
1059 {
1060 	return (0);
1061 }
1062 
1063 /* lock the access to the keyboard, not used */
1064 static int
1065 ukbd_lock(keyboard_t *kbd, int lock)
1066 {
1067 	return (1);
1068 }
1069 
1070 /*
1071  * Enable the access to the device; until this function is called,
1072  * the client cannot read from the keyboard.
1073  */
1074 static int
1075 ukbd_enable(keyboard_t *kbd)
1076 {
1077 	if (!mtx_owned(&Giant)) {
1078 		/* XXX cludge */
1079 		int retval;
1080 		mtx_lock(&Giant);
1081 		retval = ukbd_enable(kbd);
1082 		mtx_unlock(&Giant);
1083 		return (retval);
1084 	}
1085 	KBD_ACTIVATE(kbd);
1086 	return (0);
1087 }
1088 
1089 /* disallow the access to the device */
1090 static int
1091 ukbd_disable(keyboard_t *kbd)
1092 {
1093 	if (!mtx_owned(&Giant)) {
1094 		/* XXX cludge */
1095 		int retval;
1096 		mtx_lock(&Giant);
1097 		retval = ukbd_disable(kbd);
1098 		mtx_unlock(&Giant);
1099 		return (retval);
1100 	}
1101 	KBD_DEACTIVATE(kbd);
1102 	return (0);
1103 }
1104 
1105 /* check if data is waiting */
1106 static int
1107 ukbd_check(keyboard_t *kbd)
1108 {
1109 	struct ukbd_softc *sc = kbd->kb_data;
1110 
1111 	if (!KBD_IS_ACTIVE(kbd))
1112 		return (0);
1113 
1114 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1115 		if (!mtx_owned(&Giant)) {
1116 			/* XXX cludge */
1117 			int retval;
1118 			mtx_lock(&Giant);
1119 			retval = ukbd_check(kbd);
1120 			mtx_unlock(&Giant);
1121 			return (retval);
1122 		}
1123 	} else {
1124 		/* XXX the keyboard layer requires Giant */
1125 		if (!mtx_owned(&Giant))
1126 			return (0);
1127 	}
1128 
1129 	/* check if key belongs to this thread */
1130 	if (ukbd_polls_other_thread(sc))
1131 		return (0);
1132 
1133 	if (sc->sc_flags & UKBD_FLAG_POLLING)
1134 		ukbd_do_poll(sc, 0);
1135 
1136 #ifdef UKBD_EMULATE_ATSCANCODE
1137 	if (sc->sc_buffered_char[0]) {
1138 		return (1);
1139 	}
1140 #endif
1141 	if (sc->sc_inputs > 0) {
1142 		return (1);
1143 	}
1144 	return (0);
1145 }
1146 
1147 /* check if char is waiting */
1148 static int
1149 ukbd_check_char(keyboard_t *kbd)
1150 {
1151 	struct ukbd_softc *sc = kbd->kb_data;
1152 
1153 	if (!KBD_IS_ACTIVE(kbd))
1154 		return (0);
1155 
1156 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1157 		if (!mtx_owned(&Giant)) {
1158 			/* XXX cludge */
1159 			int retval;
1160 			mtx_lock(&Giant);
1161 			retval = ukbd_check_char(kbd);
1162 			mtx_unlock(&Giant);
1163 			return (retval);
1164 		}
1165 	} else {
1166 		/* XXX the keyboard layer requires Giant */
1167 		if (!mtx_owned(&Giant))
1168 			return (0);
1169 	}
1170 
1171 	/* check if key belongs to this thread */
1172 	if (ukbd_polls_other_thread(sc))
1173 		return (0);
1174 
1175 	if ((sc->sc_composed_char > 0) &&
1176 	    (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1177 		return (1);
1178 	}
1179 	return (ukbd_check(kbd));
1180 }
1181 
1182 
1183 /* read one byte from the keyboard if it's allowed */
1184 static int
1185 ukbd_read(keyboard_t *kbd, int wait)
1186 {
1187 	struct ukbd_softc *sc = kbd->kb_data;
1188 	int32_t usbcode;
1189 
1190 #ifdef UKBD_EMULATE_ATSCANCODE
1191 	uint32_t keycode;
1192 	uint32_t scancode;
1193 
1194 #endif
1195 	if (!KBD_IS_ACTIVE(kbd))
1196 		return (-1);
1197 
1198 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1199 		if (!mtx_owned(&Giant)) {
1200 			/* XXX cludge */
1201 			int retval;
1202 			mtx_lock(&Giant);
1203 			retval = ukbd_read(kbd, wait);
1204 			mtx_unlock(&Giant);
1205 			return (retval);
1206 		}
1207 	} else {
1208 		/* XXX the keyboard layer requires Giant */
1209 		if (!mtx_owned(&Giant))
1210 			return (-1);
1211 	}
1212 
1213 	/* check if key belongs to this thread */
1214 	if (ukbd_polls_other_thread(sc))
1215 		return (-1);
1216 
1217 #ifdef UKBD_EMULATE_ATSCANCODE
1218 	if (sc->sc_buffered_char[0]) {
1219 		scancode = sc->sc_buffered_char[0];
1220 		if (scancode & SCAN_PREFIX) {
1221 			sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1222 			return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1223 		}
1224 		sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1225 		sc->sc_buffered_char[1] = 0;
1226 		return (scancode);
1227 	}
1228 #endif					/* UKBD_EMULATE_ATSCANCODE */
1229 
1230 	/* XXX */
1231 	usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1232 	if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1233 		return (-1);
1234 
1235 	++(kbd->kb_count);
1236 
1237 #ifdef UKBD_EMULATE_ATSCANCODE
1238 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1239 	if (keycode == NN) {
1240 		return -1;
1241 	}
1242 	return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1243 	    (usbcode & KEY_RELEASE)));
1244 #else					/* !UKBD_EMULATE_ATSCANCODE */
1245 	return (usbcode);
1246 #endif					/* UKBD_EMULATE_ATSCANCODE */
1247 }
1248 
1249 /* read char from the keyboard */
1250 static uint32_t
1251 ukbd_read_char(keyboard_t *kbd, int wait)
1252 {
1253 	struct ukbd_softc *sc = kbd->kb_data;
1254 	uint32_t action;
1255 	uint32_t keycode;
1256 	int32_t usbcode;
1257 
1258 #ifdef UKBD_EMULATE_ATSCANCODE
1259 	uint32_t scancode;
1260 
1261 #endif
1262 
1263 	if (!KBD_IS_ACTIVE(kbd))
1264 		return (NOKEY);
1265 
1266 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1267 		if (!mtx_owned(&Giant)) {
1268 			/* XXX cludge */
1269 			int retval;
1270 			mtx_lock(&Giant);
1271 			retval = ukbd_read_char(kbd, wait);
1272 			mtx_unlock(&Giant);
1273 			return (retval);
1274 		}
1275 	} else {
1276 		/* XXX the keyboard layer requires Giant */
1277 		if (!mtx_owned(&Giant))
1278 			return (NOKEY);
1279 	}
1280 
1281 	/* check if key belongs to this thread */
1282 	if (ukbd_polls_other_thread(sc))
1283 		return (NOKEY);
1284 
1285 next_code:
1286 
1287 	/* do we have a composed char to return ? */
1288 
1289 	if ((sc->sc_composed_char > 0) &&
1290 	    (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1291 
1292 		action = sc->sc_composed_char;
1293 		sc->sc_composed_char = 0;
1294 
1295 		if (action > 0xFF) {
1296 			goto errkey;
1297 		}
1298 		goto done;
1299 	}
1300 #ifdef UKBD_EMULATE_ATSCANCODE
1301 
1302 	/* do we have a pending raw scan code? */
1303 
1304 	if (sc->sc_mode == K_RAW) {
1305 		scancode = sc->sc_buffered_char[0];
1306 		if (scancode) {
1307 			if (scancode & SCAN_PREFIX) {
1308 				sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1309 				return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1310 			}
1311 			sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1312 			sc->sc_buffered_char[1] = 0;
1313 			return (scancode);
1314 		}
1315 	}
1316 #endif					/* UKBD_EMULATE_ATSCANCODE */
1317 
1318 	/* see if there is something in the keyboard port */
1319 	/* XXX */
1320 	usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1321 	if (usbcode == -1) {
1322 		return (NOKEY);
1323 	}
1324 	++kbd->kb_count;
1325 
1326 #ifdef UKBD_EMULATE_ATSCANCODE
1327 	/* USB key index -> key code -> AT scan code */
1328 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1329 	if (keycode == NN) {
1330 		return (NOKEY);
1331 	}
1332 	/* return an AT scan code for the K_RAW mode */
1333 	if (sc->sc_mode == K_RAW) {
1334 		return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1335 		    (usbcode & KEY_RELEASE)));
1336 	}
1337 #else					/* !UKBD_EMULATE_ATSCANCODE */
1338 
1339 	/* return the byte as is for the K_RAW mode */
1340 	if (sc->sc_mode == K_RAW) {
1341 		return (usbcode);
1342 	}
1343 	/* USB key index -> key code */
1344 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1345 	if (keycode == NN) {
1346 		return (NOKEY);
1347 	}
1348 #endif					/* UKBD_EMULATE_ATSCANCODE */
1349 
1350 	switch (keycode) {
1351 	case 0x38:			/* left alt (compose key) */
1352 		if (usbcode & KEY_RELEASE) {
1353 			if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1354 				sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1355 
1356 				if (sc->sc_composed_char > 0xFF) {
1357 					sc->sc_composed_char = 0;
1358 				}
1359 			}
1360 		} else {
1361 			if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1362 				sc->sc_flags |= UKBD_FLAG_COMPOSE;
1363 				sc->sc_composed_char = 0;
1364 			}
1365 		}
1366 		break;
1367 		/* XXX: I don't like these... */
1368 	case 0x5c:			/* print screen */
1369 		if (sc->sc_flags & ALTS) {
1370 			keycode = 0x54;	/* sysrq */
1371 		}
1372 		break;
1373 	case 0x68:			/* pause/break */
1374 		if (sc->sc_flags & CTLS) {
1375 			keycode = 0x6c;	/* break */
1376 		}
1377 		break;
1378 	}
1379 
1380 	/* return the key code in the K_CODE mode */
1381 	if (usbcode & KEY_RELEASE) {
1382 		keycode |= SCAN_RELEASE;
1383 	}
1384 	if (sc->sc_mode == K_CODE) {
1385 		return (keycode);
1386 	}
1387 	/* compose a character code */
1388 	if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1389 		switch (keycode) {
1390 			/* key pressed, process it */
1391 		case 0x47:
1392 		case 0x48:
1393 		case 0x49:		/* keypad 7,8,9 */
1394 			sc->sc_composed_char *= 10;
1395 			sc->sc_composed_char += keycode - 0x40;
1396 			goto check_composed;
1397 
1398 		case 0x4B:
1399 		case 0x4C:
1400 		case 0x4D:		/* keypad 4,5,6 */
1401 			sc->sc_composed_char *= 10;
1402 			sc->sc_composed_char += keycode - 0x47;
1403 			goto check_composed;
1404 
1405 		case 0x4F:
1406 		case 0x50:
1407 		case 0x51:		/* keypad 1,2,3 */
1408 			sc->sc_composed_char *= 10;
1409 			sc->sc_composed_char += keycode - 0x4E;
1410 			goto check_composed;
1411 
1412 		case 0x52:		/* keypad 0 */
1413 			sc->sc_composed_char *= 10;
1414 			goto check_composed;
1415 
1416 			/* key released, no interest here */
1417 		case SCAN_RELEASE | 0x47:
1418 		case SCAN_RELEASE | 0x48:
1419 		case SCAN_RELEASE | 0x49:	/* keypad 7,8,9 */
1420 		case SCAN_RELEASE | 0x4B:
1421 		case SCAN_RELEASE | 0x4C:
1422 		case SCAN_RELEASE | 0x4D:	/* keypad 4,5,6 */
1423 		case SCAN_RELEASE | 0x4F:
1424 		case SCAN_RELEASE | 0x50:
1425 		case SCAN_RELEASE | 0x51:	/* keypad 1,2,3 */
1426 		case SCAN_RELEASE | 0x52:	/* keypad 0 */
1427 			goto next_code;
1428 
1429 		case 0x38:		/* left alt key */
1430 			break;
1431 
1432 		default:
1433 			if (sc->sc_composed_char > 0) {
1434 				sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1435 				sc->sc_composed_char = 0;
1436 				goto errkey;
1437 			}
1438 			break;
1439 		}
1440 	}
1441 	/* keycode to key action */
1442 	action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1443 	    (keycode & SCAN_RELEASE),
1444 	    &sc->sc_state, &sc->sc_accents);
1445 	if (action == NOKEY) {
1446 		goto next_code;
1447 	}
1448 done:
1449 	return (action);
1450 
1451 check_composed:
1452 	if (sc->sc_composed_char <= 0xFF) {
1453 		goto next_code;
1454 	}
1455 errkey:
1456 	return (ERRKEY);
1457 }
1458 
1459 /* some useful control functions */
1460 static int
1461 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1462 {
1463 	/* translate LED_XXX bits into the device specific bits */
1464 	static const uint8_t ledmap[8] = {
1465 		0, 2, 1, 3, 4, 6, 5, 7,
1466 	};
1467 	struct ukbd_softc *sc = kbd->kb_data;
1468 	int i;
1469 
1470 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1471     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1472 	int ival;
1473 
1474 #endif
1475 	if (!mtx_owned(&Giant)) {
1476 		/*
1477 		 * XXX big problem: If scroll lock is pressed and "printf()"
1478 		 * is called, the CPU will get here, to un-scroll lock the
1479 		 * keyboard. But if "printf()" acquires the "Giant" lock,
1480 		 * there will be a locking order reversal problem, so the
1481 		 * keyboard system must get out of "Giant" first, before the
1482 		 * CPU can proceed here ...
1483 		 */
1484 		switch (cmd) {
1485 		case KDGKBMODE:
1486 		case KDSKBMODE:
1487 			/* workaround for Geli */
1488 			mtx_lock(&Giant);
1489 			i = ukbd_ioctl(kbd, cmd, arg);
1490 			mtx_unlock(&Giant);
1491 			return (i);
1492 		default:
1493 			return (EINVAL);
1494 		}
1495 	}
1496 
1497 	switch (cmd) {
1498 	case KDGKBMODE:		/* get keyboard mode */
1499 		*(int *)arg = sc->sc_mode;
1500 		break;
1501 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1502     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1503 	case _IO('K', 7):
1504 		ival = IOCPARM_IVAL(arg);
1505 		arg = (caddr_t)&ival;
1506 		/* FALLTHROUGH */
1507 #endif
1508 	case KDSKBMODE:		/* set keyboard mode */
1509 		switch (*(int *)arg) {
1510 		case K_XLATE:
1511 			if (sc->sc_mode != K_XLATE) {
1512 				/* make lock key state and LED state match */
1513 				sc->sc_state &= ~LOCK_MASK;
1514 				sc->sc_state |= KBD_LED_VAL(kbd);
1515 			}
1516 			/* FALLTHROUGH */
1517 		case K_RAW:
1518 		case K_CODE:
1519 			if (sc->sc_mode != *(int *)arg) {
1520 				if (ukbd_is_polling(sc) == 0)
1521 					ukbd_clear_state(kbd);
1522 				sc->sc_mode = *(int *)arg;
1523 			}
1524 			break;
1525 		default:
1526 			return (EINVAL);
1527 		}
1528 		break;
1529 
1530 	case KDGETLED:			/* get keyboard LED */
1531 		*(int *)arg = KBD_LED_VAL(kbd);
1532 		break;
1533 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1534     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1535 	case _IO('K', 66):
1536 		ival = IOCPARM_IVAL(arg);
1537 		arg = (caddr_t)&ival;
1538 		/* FALLTHROUGH */
1539 #endif
1540 	case KDSETLED:			/* set keyboard LED */
1541 		/* NOTE: lock key state in "sc_state" won't be changed */
1542 		if (*(int *)arg & ~LOCK_MASK) {
1543 			return (EINVAL);
1544 		}
1545 		i = *(int *)arg;
1546 		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1547 		if (sc->sc_mode == K_XLATE &&
1548 		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1549 			if (i & ALKED)
1550 				i |= CLKED;
1551 			else
1552 				i &= ~CLKED;
1553 		}
1554 		if (KBD_HAS_DEVICE(kbd)) {
1555 			ukbd_set_leds(sc, ledmap[i & LED_MASK]);
1556 		}
1557 		KBD_LED_VAL(kbd) = *(int *)arg;
1558 		break;
1559 	case KDGKBSTATE:		/* get lock key state */
1560 		*(int *)arg = sc->sc_state & LOCK_MASK;
1561 		break;
1562 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1563     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1564 	case _IO('K', 20):
1565 		ival = IOCPARM_IVAL(arg);
1566 		arg = (caddr_t)&ival;
1567 		/* FALLTHROUGH */
1568 #endif
1569 	case KDSKBSTATE:		/* set lock key state */
1570 		if (*(int *)arg & ~LOCK_MASK) {
1571 			return (EINVAL);
1572 		}
1573 		sc->sc_state &= ~LOCK_MASK;
1574 		sc->sc_state |= *(int *)arg;
1575 
1576 		/* set LEDs and quit */
1577 		return (ukbd_ioctl(kbd, KDSETLED, arg));
1578 
1579 	case KDSETREPEAT:		/* set keyboard repeat rate (new
1580 					 * interface) */
1581 		if (!KBD_HAS_DEVICE(kbd)) {
1582 			return (0);
1583 		}
1584 		if (((int *)arg)[1] < 0) {
1585 			return (EINVAL);
1586 		}
1587 		if (((int *)arg)[0] < 0) {
1588 			return (EINVAL);
1589 		}
1590 		if (((int *)arg)[0] < 200)	/* fastest possible value */
1591 			kbd->kb_delay1 = 200;
1592 		else
1593 			kbd->kb_delay1 = ((int *)arg)[0];
1594 		kbd->kb_delay2 = ((int *)arg)[1];
1595 		return (0);
1596 
1597 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1598     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1599 	case _IO('K', 67):
1600 		ival = IOCPARM_IVAL(arg);
1601 		arg = (caddr_t)&ival;
1602 		/* FALLTHROUGH */
1603 #endif
1604 	case KDSETRAD:			/* set keyboard repeat rate (old
1605 					 * interface) */
1606 		return (ukbd_set_typematic(kbd, *(int *)arg));
1607 
1608 	case PIO_KEYMAP:		/* set keyboard translation table */
1609 	case PIO_KEYMAPENT:		/* set keyboard translation table
1610 					 * entry */
1611 	case PIO_DEADKEYMAP:		/* set accent key translation table */
1612 		sc->sc_accents = 0;
1613 		/* FALLTHROUGH */
1614 	default:
1615 		return (genkbd_commonioctl(kbd, cmd, arg));
1616 	}
1617 
1618 	return (0);
1619 }
1620 
1621 /* clear the internal state of the keyboard */
1622 static void
1623 ukbd_clear_state(keyboard_t *kbd)
1624 {
1625 	struct ukbd_softc *sc = kbd->kb_data;
1626 
1627 	if (!mtx_owned(&Giant)) {
1628 		/* XXX cludge */
1629 		mtx_lock(&Giant);
1630 		ukbd_clear_state(kbd);
1631 		mtx_unlock(&Giant);
1632 		return;
1633 	}
1634 
1635 	sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1636 	sc->sc_state &= LOCK_MASK;	/* preserve locking key state */
1637 	sc->sc_accents = 0;
1638 	sc->sc_composed_char = 0;
1639 #ifdef UKBD_EMULATE_ATSCANCODE
1640 	sc->sc_buffered_char[0] = 0;
1641 	sc->sc_buffered_char[1] = 0;
1642 #endif
1643 	memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1644 	memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
1645 	memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
1646 	memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
1647 }
1648 
1649 /* save the internal state, not used */
1650 static int
1651 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1652 {
1653 	return (len == 0) ? 1 : -1;
1654 }
1655 
1656 /* set the internal state, not used */
1657 static int
1658 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1659 {
1660 	return (EINVAL);
1661 }
1662 
1663 static int
1664 ukbd_is_polling(struct ukbd_softc *sc)
1665 {
1666 	int delta;
1667 
1668 	if (sc->sc_flags & UKBD_FLAG_POLLING)
1669 		return (1);	/* polling */
1670 
1671 	delta = ticks - sc->sc_poll_tick_last;
1672 	if ((delta < 0) || (delta >= hz)) {
1673 		sc->sc_poll_detected = 0;
1674 		return (0);		/* not polling */
1675 	}
1676 
1677 	return (sc->sc_poll_detected);
1678 }
1679 
1680 static int
1681 ukbd_polls_other_thread(struct ukbd_softc *sc)
1682 {
1683 	return (ukbd_is_polling(sc) &&
1684 	    (sc->sc_poll_thread != curthread));
1685 }
1686 
1687 static int
1688 ukbd_poll(keyboard_t *kbd, int on)
1689 {
1690 	struct ukbd_softc *sc = kbd->kb_data;
1691 
1692 	if (!mtx_owned(&Giant)) {
1693 		/* XXX cludge */
1694 		int retval;
1695 		mtx_lock(&Giant);
1696 		retval = ukbd_poll(kbd, on);
1697 		mtx_unlock(&Giant);
1698 		return (retval);
1699 	}
1700 
1701 	if (on) {
1702 		sc->sc_flags |= UKBD_FLAG_POLLING;
1703 		sc->sc_poll_thread = curthread;
1704 	} else {
1705 		sc->sc_flags &= ~UKBD_FLAG_POLLING;
1706 		ukbd_start_timer(sc);	/* start timer */
1707 	}
1708 	return (0);
1709 }
1710 
1711 /* local functions */
1712 
1713 static void
1714 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1715 {
1716 	DPRINTF("leds=0x%02x\n", leds);
1717 
1718 	sc->sc_leds = leds;
1719 	sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1720 
1721 	/* start transfer, if not already started */
1722 
1723 	usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1724 }
1725 
1726 static int
1727 ukbd_set_typematic(keyboard_t *kbd, int code)
1728 {
1729 	static const int delays[] = {250, 500, 750, 1000};
1730 	static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1731 		68, 76, 84, 92, 100, 110, 118, 126,
1732 		136, 152, 168, 184, 200, 220, 236, 252,
1733 	272, 304, 336, 368, 400, 440, 472, 504};
1734 
1735 	if (code & ~0x7f) {
1736 		return (EINVAL);
1737 	}
1738 	kbd->kb_delay1 = delays[(code >> 5) & 3];
1739 	kbd->kb_delay2 = rates[code & 0x1f];
1740 	return (0);
1741 }
1742 
1743 #ifdef UKBD_EMULATE_ATSCANCODE
1744 static int
1745 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1746 {
1747 	static const int scan[] = {
1748 		/* 89 */
1749 		0x11c,	/* Enter */
1750 		/* 90-99 */
1751 		0x11d,	/* Ctrl-R */
1752 		0x135,	/* Divide */
1753 		0x137 | SCAN_PREFIX_SHIFT,	/* PrintScreen */
1754 		0x138,	/* Alt-R */
1755 		0x147,	/* Home */
1756 		0x148,	/* Up */
1757 		0x149,	/* PageUp */
1758 		0x14b,	/* Left */
1759 		0x14d,	/* Right */
1760 		0x14f,	/* End */
1761 		/* 100-109 */
1762 		0x150,	/* Down */
1763 		0x151,	/* PageDown */
1764 		0x152,	/* Insert */
1765 		0x153,	/* Delete */
1766 		0x146,	/* XXX Pause/Break */
1767 		0x15b,	/* Win_L(Super_L) */
1768 		0x15c,	/* Win_R(Super_R) */
1769 		0x15d,	/* Application(Menu) */
1770 
1771 		/* SUN TYPE 6 USB KEYBOARD */
1772 		0x168,	/* Sun Type 6 Help */
1773 		0x15e,	/* Sun Type 6 Stop */
1774 		/* 110 - 119 */
1775 		0x15f,	/* Sun Type 6 Again */
1776 		0x160,	/* Sun Type 6 Props */
1777 		0x161,	/* Sun Type 6 Undo */
1778 		0x162,	/* Sun Type 6 Front */
1779 		0x163,	/* Sun Type 6 Copy */
1780 		0x164,	/* Sun Type 6 Open */
1781 		0x165,	/* Sun Type 6 Paste */
1782 		0x166,	/* Sun Type 6 Find */
1783 		0x167,	/* Sun Type 6 Cut */
1784 		0x125,	/* Sun Type 6 Mute */
1785 		/* 120 - 128 */
1786 		0x11f,	/* Sun Type 6 VolumeDown */
1787 		0x11e,	/* Sun Type 6 VolumeUp */
1788 		0x120,	/* Sun Type 6 PowerDown */
1789 
1790 		/* Japanese 106/109 keyboard */
1791 		0x73,	/* Keyboard Intl' 1 (backslash / underscore) */
1792 		0x70,	/* Keyboard Intl' 2 (Katakana / Hiragana) */
1793 		0x7d,	/* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
1794 		0x79,	/* Keyboard Intl' 4 (Henkan) */
1795 		0x7b,	/* Keyboard Intl' 5 (Muhenkan) */
1796 		0x5c,	/* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
1797 	};
1798 
1799 	if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
1800 		code = scan[code - 89];
1801 	}
1802 	/* Pause/Break */
1803 	if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
1804 		code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
1805 	}
1806 	if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
1807 		code &= ~SCAN_PREFIX_SHIFT;
1808 	}
1809 	code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1810 
1811 	if (code & SCAN_PREFIX) {
1812 		if (code & SCAN_PREFIX_CTL) {
1813 			/* Ctrl */
1814 			sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
1815 			sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
1816 		} else if (code & SCAN_PREFIX_SHIFT) {
1817 			/* Shift */
1818 			sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1819 			sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1820 		} else {
1821 			sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1822 			sc->sc_buffered_char[1] = 0;
1823 		}
1824 		return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1825 	}
1826 	return (code);
1827 
1828 }
1829 
1830 #endif					/* UKBD_EMULATE_ATSCANCODE */
1831 
1832 static keyboard_switch_t ukbdsw = {
1833 	.probe = &ukbd__probe,
1834 	.init = &ukbd_init,
1835 	.term = &ukbd_term,
1836 	.intr = &ukbd_intr,
1837 	.test_if = &ukbd_test_if,
1838 	.enable = &ukbd_enable,
1839 	.disable = &ukbd_disable,
1840 	.read = &ukbd_read,
1841 	.check = &ukbd_check,
1842 	.read_char = &ukbd_read_char,
1843 	.check_char = &ukbd_check_char,
1844 	.ioctl = &ukbd_ioctl,
1845 	.lock = &ukbd_lock,
1846 	.clear_state = &ukbd_clear_state,
1847 	.get_state = &ukbd_get_state,
1848 	.set_state = &ukbd_set_state,
1849 	.get_fkeystr = &genkbd_get_fkeystr,
1850 	.poll = &ukbd_poll,
1851 	.diag = &genkbd_diag,
1852 };
1853 
1854 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
1855 
1856 static int
1857 ukbd_driver_load(module_t mod, int what, void *arg)
1858 {
1859 	switch (what) {
1860 	case MOD_LOAD:
1861 		kbd_add_driver(&ukbd_kbd_driver);
1862 		break;
1863 	case MOD_UNLOAD:
1864 		kbd_delete_driver(&ukbd_kbd_driver);
1865 		break;
1866 	}
1867 	return (0);
1868 }
1869 
1870 static devclass_t ukbd_devclass;
1871 
1872 static device_method_t ukbd_methods[] = {
1873 	DEVMETHOD(device_probe, ukbd_probe),
1874 	DEVMETHOD(device_attach, ukbd_attach),
1875 	DEVMETHOD(device_detach, ukbd_detach),
1876 	DEVMETHOD(device_resume, ukbd_resume),
1877 	{0, 0}
1878 };
1879 
1880 static driver_t ukbd_driver = {
1881 	.name = "ukbd",
1882 	.methods = ukbd_methods,
1883 	.size = sizeof(struct ukbd_softc),
1884 };
1885 
1886 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
1887 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
1888