xref: /openbsd-src/sys/dev/usb/ukbd.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: ukbd.c,v 1.46 2009/07/31 11:01:48 blambert Exp $	*/
2 /*      $NetBSD: ukbd.c,v 1.85 2003/03/11 16:44:00 augustss Exp $        */
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/timeout.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/ioctl.h>
44 #include <sys/tty.h>
45 #include <sys/file.h>
46 #include <sys/selinfo.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/poll.h>
50 
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbhid.h>
53 
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56 #include <dev/usb/usbdevs.h>
57 #include <dev/usb/usb_quirks.h>
58 #include <dev/usb/uhidev.h>
59 #include <dev/usb/hid.h>
60 #include <dev/usb/ukbdvar.h>
61 
62 #include <dev/wscons/wsconsio.h>
63 #include <dev/wscons/wskbdvar.h>
64 #include <dev/wscons/wsksymdef.h>
65 #include <dev/wscons/wsksymvar.h>
66 
67 #ifdef UKBD_DEBUG
68 #define DPRINTF(x)	do { if (ukbddebug) printf x; } while (0)
69 #define DPRINTFN(n,x)	do { if (ukbddebug>(n)) printf x; } while (0)
70 int	ukbddebug = 0;
71 #else
72 #define DPRINTF(x)
73 #define DPRINTFN(n,x)
74 #endif
75 
76 #define MAXKEYCODE 6
77 #define MAXMOD 8		/* max 32 */
78 
79 struct ukbd_data {
80 	u_int32_t	modifiers;
81 	u_int8_t	keycode[MAXKEYCODE];
82 };
83 
84 #define PRESS    0x000
85 #define RELEASE  0x100
86 #define CODEMASK 0x0ff
87 
88 #if defined(WSDISPLAY_COMPAT_RAWKBD)
89 #define NN 0			/* no translation */
90 /*
91  * Translate USB keycodes to US keyboard XT scancodes.
92  * Scancodes >= 0x80 represent EXTENDED keycodes.
93  *
94  * See http://www.microsoft.com/whdc/device/input/Scancode.mspx
95  */
96 const u_int8_t ukbd_trtab[256] = {
97       NN,   NN,   NN,   NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
98     0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
99     0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
100     0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
101     0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
102     0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
103     0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
104     0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
105     0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */
106     0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
107     0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
108     0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
109     0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, 0x84, 0x59, /* 60 - 67 */
110     0x5d, 0x5e, 0x5f,   NN,   NN,   NN,   NN,   NN, /* 68 - 6f */
111       NN,   NN,   NN,   NN, 0x97,   NN, 0x93, 0x95, /* 70 - 77 */
112     0x91, 0x92, 0x94, 0x9a, 0x96, 0x98, 0x99, 0xa0, /* 78 - 7f */
113     0xb0, 0xae,   NN,   NN,   NN, 0x7e,   NN, 0x73, /* 80 - 87 */
114     0x70, 0x7d, 0x79, 0x7b, 0x5c,   NN,   NN,   NN, /* 88 - 8f */
115       NN,   NN, 0x78, 0x77, 0x76,   NN,   NN,   NN, /* 90 - 97 */
116       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 98 - 9f */
117       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a0 - a7 */
118       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a8 - af */
119       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b0 - b7 */
120       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b8 - bf */
121       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c0 - c7 */
122       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c8 - cf */
123       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d0 - d7 */
124       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d8 - df */
125     0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
126       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* e8 - ef */
127       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f0 - f7 */
128       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f8 - ff */
129 };
130 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
131 
132 const kbd_t ukbd_countrylayout[1 + HCC_MAX] = {
133 	(kbd_t)-1,
134 	(kbd_t)-1,	/* arabic */
135 	KB_BE,		/* belgian */
136 	(kbd_t)-1,	/* canadian bilingual */
137 	KB_CF,		/* canadian french */
138 	(kbd_t)-1,	/* czech */
139 	KB_DK,		/* danish */
140 	(kbd_t)-1,	/* finnish */
141 	KB_FR,		/* french */
142 	KB_DE,		/* german */
143 	(kbd_t)-1,	/* greek */
144 	(kbd_t)-1,	/* hebrew */
145 	KB_HU,		/* hungary */
146 	(kbd_t)-1,	/* international (iso) */
147 	KB_IT,		/* italian */
148 	KB_JP,		/* japanese (katakana) */
149 	(kbd_t)-1,	/* korean */
150 	KB_LA,		/* latin american */
151 	(kbd_t)-1,	/* netherlands/dutch */
152 	KB_NO,		/* norwegian */
153 	(kbd_t)-1,	/* persian (farsi) */
154 	KB_PL,		/* polish */
155 	KB_PT,		/* portuguese */
156 	KB_RU,		/* russian */
157 	(kbd_t)-1,	/* slovakia */
158 	KB_ES,		/* spanish */
159 	KB_SV,		/* swedish */
160 	KB_SF,		/* swiss french */
161 	KB_SG,		/* swiss german */
162 	(kbd_t)-1,	/* switzerland */
163 	(kbd_t)-1,	/* taiwan */
164 	KB_TR,		/* turkish Q */
165 	KB_UK,		/* uk */
166 	KB_US,		/* us */
167 	(kbd_t)-1,	/* yugoslavia */
168 	(kbd_t)-1	/* turkish F */
169 };
170 
171 #define KEY_ERROR 0x01
172 
173 #define MAXKEYS (MAXMOD+2*MAXKEYCODE)
174 
175 struct ukbd_softc {
176 	struct uhidev sc_hdev;
177 
178 	struct ukbd_data sc_ndata;
179 	struct ukbd_data sc_odata;
180 	struct hid_location sc_modloc[MAXMOD];
181 	u_int sc_nmod;
182 	struct {
183 		u_int32_t mask;
184 		u_int8_t key;
185 	} sc_mods[MAXMOD];
186 
187 	struct hid_location sc_keycodeloc;
188 	u_int sc_nkeycode;
189 
190 	char sc_enabled;
191 
192 	int sc_console_keyboard;	/* we are the console keyboard */
193 
194 	char sc_debounce;		/* for quirk handling */
195 	struct timeout sc_delay;		/* for quirk handling */
196 	struct ukbd_data sc_data;	/* for quirk handling */
197 
198 	struct hid_location sc_numloc;
199 	struct hid_location sc_capsloc;
200 	struct hid_location sc_scroloc;
201 	int sc_leds;
202 
203 	struct timeout sc_rawrepeat_ch;
204 
205 	struct device *sc_wskbddev;
206 #if defined(WSDISPLAY_COMPAT_RAWKBD)
207 #define REP_DELAY1 400
208 #define REP_DELAYN 100
209 	int sc_rawkbd;
210 	int sc_nrep;
211 	char sc_rep[MAXKEYS];
212 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
213 
214 	int sc_spl;
215 	int sc_polling;
216 	int sc_npollchar;
217 	u_int16_t sc_pollchars[MAXKEYS];
218 
219 	u_char sc_dying;
220 };
221 
222 #ifdef UKBD_DEBUG
223 #define UKBDTRACESIZE 64
224 struct ukbdtraceinfo {
225 	int unit;
226 	struct timeval tv;
227 	struct ukbd_data ud;
228 };
229 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
230 int ukbdtraceindex = 0;
231 int ukbdtrace = 0;
232 void ukbdtracedump(void);
233 void
234 ukbdtracedump(void)
235 {
236 	int i;
237 	for (i = 0; i < UKBDTRACESIZE; i++) {
238 		struct ukbdtraceinfo *p =
239 		    &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
240 		printf("%lu.%06lu: mod=0x%02x key0=0x%02x key1=0x%02x "
241 		       "key2=0x%02x key3=0x%02x\n",
242 		       p->tv.tv_sec, p->tv.tv_usec,
243 		       p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1],
244 		       p->ud.keycode[2], p->ud.keycode[3]);
245 	}
246 }
247 #endif
248 
249 #define	UKBDUNIT(dev)	(minor(dev))
250 #define	UKBD_CHUNK	128	/* chunk size for read */
251 #define	UKBD_BSIZE	1020	/* buffer size */
252 
253 int	ukbd_is_console;
254 
255 void	ukbd_cngetc(void *, u_int *, int *);
256 void	ukbd_cnpollc(void *, int);
257 void	ukbd_cnbell(void *, u_int, u_int, u_int);
258 
259 const struct wskbd_consops ukbd_consops = {
260 	ukbd_cngetc,
261 	ukbd_cnpollc,
262 	ukbd_cnbell,
263 };
264 
265 const char *ukbd_parse_desc(struct ukbd_softc *sc);
266 
267 void	(*ukbd_bell_fn)(void *, u_int, u_int, u_int, int);
268 void	*ukbd_bell_fn_arg;
269 
270 void	ukbd_bell(u_int, u_int, u_int, int);
271 
272 void	ukbd_intr(struct uhidev *addr, void *ibuf, u_int len);
273 void	ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud);
274 void	ukbd_delayed_decode(void *addr);
275 
276 int	ukbd_enable(void *, int);
277 void	ukbd_set_leds(void *, int);
278 
279 int	ukbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
280 #ifdef WSDISPLAY_COMPAT_RAWKBD
281 void	ukbd_rawrepeat(void *v);
282 #endif
283 
284 const struct wskbd_accessops ukbd_accessops = {
285 	ukbd_enable,
286 	ukbd_set_leds,
287 	ukbd_ioctl,
288 };
289 
290 extern const struct wscons_keydesc ukbd_keydesctab[];
291 
292 struct wskbd_mapdata ukbd_keymapdata = {
293 	ukbd_keydesctab
294 };
295 
296 int ukbd_match(struct device *, void *, void *);
297 void ukbd_attach(struct device *, struct device *, void *);
298 int ukbd_detach(struct device *, int);
299 int ukbd_activate(struct device *, enum devact);
300 
301 struct cfdriver ukbd_cd = {
302 	NULL, "ukbd", DV_DULL
303 };
304 
305 const struct cfattach ukbd_ca = {
306 	sizeof(struct ukbd_softc),
307 	ukbd_match,
308 	ukbd_attach,
309 	ukbd_detach,
310 	ukbd_activate,
311 };
312 
313 int
314 ukbd_match(struct device *parent, void *match, void *aux)
315 {
316 	struct usb_attach_arg *uaa = aux;
317 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
318 	int size;
319 	void *desc;
320 
321 	uhidev_get_report_desc(uha->parent, &desc, &size);
322 	if (!hid_is_collection(desc, size, uha->reportid,
323 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
324 		return (UMATCH_NONE);
325 
326 	return (UMATCH_IFACECLASS);
327 }
328 
329 void
330 ukbd_attach(struct device *parent, struct device *self, void *aux)
331 {
332 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
333 	struct usb_attach_arg *uaa = aux;
334 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
335 	usb_hid_descriptor_t *hid;
336 	u_int32_t qflags;
337 	const char *parseerr;
338 	kbd_t layout = (kbd_t)-1;
339 	struct wskbddev_attach_args a;
340 
341 	sc->sc_hdev.sc_intr = ukbd_intr;
342 	sc->sc_hdev.sc_parent = uha->parent;
343 	sc->sc_hdev.sc_report_id = uha->reportid;
344 
345 	parseerr = ukbd_parse_desc(sc);
346 	if (parseerr != NULL) {
347 		printf("\n%s: attach failed, %s\n",
348 		       sc->sc_hdev.sc_dev.dv_xname, parseerr);
349 		return;
350 	}
351 
352 	hid = usbd_get_hid_descriptor(uha->uaa->iface);
353 
354 #ifdef DIAGNOSTIC
355 	printf(": %d modifier keys, %d key codes",
356 	    sc->sc_nmod, sc->sc_nkeycode);
357 #endif
358 
359 	qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
360 	sc->sc_debounce = (qflags & UQ_SPUR_BUT_UP) != 0;
361 
362 	/*
363 	 * Remember if we're the console keyboard.
364 	 *
365 	 * XXX This always picks the first keyboard on the
366 	 * first USB bus, but what else can we really do?
367 	 */
368 	if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
369 		/* Don't let any other keyboard have it. */
370 		ukbd_is_console = 0;
371 	}
372 
373 	if (uha->uaa->vendor == USB_VENDOR_TOPRE &&
374 	    uha->uaa->product == USB_PRODUCT_TOPRE_HHKB) {
375 		/* ignore country code on purpose */
376 	} else {
377 		if (hid->bCountryCode <= HCC_MAX)
378 			layout = ukbd_countrylayout[hid->bCountryCode];
379 #ifdef DIAGNOSTIC
380 		if (hid->bCountryCode != 0)
381 			printf(", country code %d", hid->bCountryCode);
382 #endif
383 	}
384 	if (layout == (kbd_t)-1) {
385 #ifdef UKBD_LAYOUT
386 		layout = UKBD_LAYOUT;
387 #else
388 		layout = KB_US;
389 #endif
390 	}
391 	ukbd_keymapdata.layout = layout;
392 
393 	printf("\n");
394 
395 	if (sc->sc_console_keyboard) {
396 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
397 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
398 		ukbd_enable(sc, 1);
399 	}
400 
401 	a.console = sc->sc_console_keyboard;
402 
403 	a.keymap = &ukbd_keymapdata;
404 
405 	a.accessops = &ukbd_accessops;
406 	a.accesscookie = sc;
407 
408 #ifdef WSDISPLAY_COMPAT_RAWKBD
409 	timeout_set(&sc->sc_rawrepeat_ch, ukbd_rawrepeat, sc);
410 #endif
411 	timeout_set(&sc->sc_delay, ukbd_delayed_decode, sc);
412 
413 	/* Flash the leds; no real purpose, just shows we're alive. */
414 	ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
415 	usbd_delay_ms(uha->parent->sc_udev, 400);
416 	ukbd_set_leds(sc, 0);
417 
418 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
419 }
420 
421 int
422 ukbd_enable(void *v, int on)
423 {
424 	struct ukbd_softc *sc = v;
425 
426 	if (on && sc->sc_dying)
427 		return (EIO);
428 
429 	/* Should only be called to change state */
430 	if (sc->sc_enabled == on) {
431 		DPRINTF(("ukbd_enable: %s: bad call on=%d\n",
432 			 sc->sc_hdev.sc_dev.dv_xname, on));
433 		return (EBUSY);
434 	}
435 
436 	DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
437 	sc->sc_enabled = on;
438 	if (on) {
439 		return (uhidev_open(&sc->sc_hdev));
440 	} else {
441 		uhidev_close(&sc->sc_hdev);
442 		return (0);
443 	}
444 }
445 
446 int
447 ukbd_activate(struct device *self, enum devact act)
448 {
449 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
450 	int rv = 0;
451 
452 	switch (act) {
453 	case DVACT_ACTIVATE:
454 		break;
455 
456 	case DVACT_DEACTIVATE:
457 		if (sc->sc_wskbddev != NULL)
458 			rv = config_deactivate(sc->sc_wskbddev);
459 		sc->sc_dying = 1;
460 		break;
461 	}
462 	return (rv);
463 }
464 
465 int
466 ukbd_detach(struct device *self, int flags)
467 {
468 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
469 	int rv = 0;
470 
471 	DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
472 
473 	if (sc->sc_console_keyboard) {
474 #if 0
475 		/*
476 		 * XXX Should probably disconnect our consops,
477 		 * XXX and either notify some other keyboard that
478 		 * XXX it can now be the console, or if there aren't
479 		 * XXX any more USB keyboards, set ukbd_is_console
480 		 * XXX back to 1 so that the next USB keyboard attached
481 		 * XXX to the system will get it.
482 		 */
483 		panic("ukbd_detach: console keyboard");
484 #else
485 		/*
486 		 * Disconnect our consops and set ukbd_is_console
487 		 * back to 1 so that the next USB keyboard attached
488 		 * to the system will get it.
489 		 * XXX Should notify some other keyboard that it can be
490 		 * XXX console, if there are any other keyboards.
491 		 */
492 		printf("%s: was console keyboard\n",
493 		       sc->sc_hdev.sc_dev.dv_xname);
494 		wskbd_cndetach();
495 		ukbd_is_console = 1;
496 #endif
497 	}
498 	/* No need to do reference counting of ukbd, wskbd has all the goo. */
499 	if (sc->sc_wskbddev != NULL)
500 		rv = config_detach(sc->sc_wskbddev, flags);
501 
502 	/* The console keyboard does not get a disable call, so check pipe. */
503 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
504 		uhidev_close(&sc->sc_hdev);
505 
506 	return (rv);
507 }
508 
509 void
510 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
511 {
512 	struct ukbd_softc *sc = (struct ukbd_softc *)addr;
513 	struct ukbd_data *ud = &sc->sc_ndata;
514 	int i;
515 
516 #ifdef UKBD_DEBUG
517 	if (ukbddebug > 5) {
518 		printf("ukbd_intr: data");
519 		for (i = 0; i < len; i++)
520 			printf(" 0x%02x", ((u_char *)ibuf)[i]);
521 		printf("\n");
522 	}
523 #endif
524 
525 	ud->modifiers = 0;
526 	for (i = 0; i < sc->sc_nmod; i++)
527 		if (hid_get_data(ibuf, &sc->sc_modloc[i]))
528 			ud->modifiers |= sc->sc_mods[i].mask;
529 	memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8,
530 	       sc->sc_nkeycode);
531 
532 	if (sc->sc_debounce && !sc->sc_polling) {
533 		/*
534 		 * Some keyboards have a peculiar quirk.  They sometimes
535 		 * generate a key up followed by a key down for the same
536 		 * key after about 10 ms.
537 		 * We avoid this bug by holding off decoding for 20 ms.
538 		 */
539 		sc->sc_data = *ud;
540 		timeout_add_msec(&sc->sc_delay, 20);
541 #ifdef DDB
542 	} else if (sc->sc_console_keyboard && !sc->sc_polling) {
543 		/*
544 		 * For the console keyboard we can't deliver CTL-ALT-ESC
545 		 * from the interrupt routine.  Doing so would start
546 		 * polling from inside the interrupt routine and that
547 		 * loses bigtime.
548 		 */
549 		sc->sc_data = *ud;
550 		timeout_add(&sc->sc_delay, 1);
551 #endif
552 	} else {
553 		ukbd_decode(sc, ud);
554 	}
555 }
556 
557 void
558 ukbd_delayed_decode(void *addr)
559 {
560 	struct ukbd_softc *sc = addr;
561 
562 	ukbd_decode(sc, &sc->sc_data);
563 }
564 
565 void
566 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
567 {
568 	int mod, omod;
569 	u_int16_t ibuf[MAXKEYS];	/* chars events */
570 	int s;
571 	int nkeys, i, j;
572 	int key;
573 #define ADDKEY(c) ibuf[nkeys++] = (c)
574 
575 #ifdef UKBD_DEBUG
576 	/*
577 	 * Keep a trace of the last events.  Using printf changes the
578 	 * timing, so this can be useful sometimes.
579 	 */
580 	if (ukbdtrace) {
581 		struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
582 		p->unit = sc->sc_hdev.sc_dev.dv_unit;
583 		microtime(&p->tv);
584 		p->ud = *ud;
585 		if (++ukbdtraceindex >= UKBDTRACESIZE)
586 			ukbdtraceindex = 0;
587 	}
588 	if (ukbddebug > 5) {
589 		struct timeval tv;
590 		microtime(&tv);
591 		DPRINTF((" at %lu.%06lu  mod=0x%02x key0=0x%02x key1=0x%02x "
592 			 "key2=0x%02x key3=0x%02x\n",
593 			 tv.tv_sec, tv.tv_usec,
594 			 ud->modifiers, ud->keycode[0], ud->keycode[1],
595 			 ud->keycode[2], ud->keycode[3]));
596 	}
597 #endif
598 
599 	if (ud->keycode[0] == KEY_ERROR) {
600 		DPRINTF(("ukbd_intr: KEY_ERROR\n"));
601 		return;		/* ignore  */
602 	}
603 	nkeys = 0;
604 	mod = ud->modifiers;
605 	omod = sc->sc_odata.modifiers;
606 	if (mod != omod)
607 		for (i = 0; i < sc->sc_nmod; i++)
608 			if (( mod & sc->sc_mods[i].mask) !=
609 			    (omod & sc->sc_mods[i].mask))
610 				ADDKEY(sc->sc_mods[i].key |
611 				       (mod & sc->sc_mods[i].mask
612 					  ? PRESS : RELEASE));
613 	if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
614 		/* Check for released keys. */
615 		for (i = 0; i < sc->sc_nkeycode; i++) {
616 			key = sc->sc_odata.keycode[i];
617 			if (key == 0)
618 				continue;
619 			for (j = 0; j < sc->sc_nkeycode; j++)
620 				if (key == ud->keycode[j])
621 					goto rfound;
622 			DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
623 			ADDKEY(key | RELEASE);
624 		rfound:
625 			;
626 		}
627 
628 		/* Check for pressed keys. */
629 		for (i = 0; i < sc->sc_nkeycode; i++) {
630 			key = ud->keycode[i];
631 			if (key == 0)
632 				continue;
633 			for (j = 0; j < sc->sc_nkeycode; j++)
634 				if (key == sc->sc_odata.keycode[j])
635 					goto pfound;
636 			DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
637 			ADDKEY(key | PRESS);
638 		pfound:
639 			;
640 		}
641 	}
642 	sc->sc_odata = *ud;
643 
644 	if (nkeys == 0)
645 		return;
646 
647 	if (sc->sc_polling) {
648 		DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
649 		memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
650 		sc->sc_npollchar = nkeys;
651 		return;
652 	}
653 #ifdef WSDISPLAY_COMPAT_RAWKBD
654 	if (sc->sc_rawkbd) {
655 		u_char cbuf[MAXKEYS * 2];
656 		int c;
657 		int npress;
658 
659 		for (npress = i = j = 0; i < nkeys; i++) {
660 			key = ibuf[i];
661 			c = ukbd_trtab[key & CODEMASK];
662 			if (c == NN)
663 				continue;
664 			if (c & 0x80)
665 				cbuf[j++] = 0xe0;
666 			cbuf[j] = c & 0x7f;
667 			if (key & RELEASE)
668 				cbuf[j] |= 0x80;
669 			else {
670 				/* remember pressed keys for autorepeat */
671 				if (c & 0x80)
672 					sc->sc_rep[npress++] = 0xe0;
673 				sc->sc_rep[npress++] = c & 0x7f;
674 			}
675 			DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
676 				    c & 0x80 ? "0xe0 " : "",
677 				    cbuf[j]));
678 			j++;
679 		}
680 		s = spltty();
681 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
682 		splx(s);
683 		if (npress != 0) {
684 			sc->sc_nrep = npress;
685 			timeout_add_msec(&sc->sc_rawrepeat_ch, REP_DELAY1);
686 		} else
687 			timeout_del(&sc->sc_rawrepeat_ch);
688 		return;
689 	}
690 #endif
691 
692 	s = spltty();
693 	for (i = 0; i < nkeys; i++) {
694 		key = ibuf[i];
695 		wskbd_input(sc->sc_wskbddev,
696 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
697 		    key&CODEMASK);
698 	}
699 	splx(s);
700 }
701 
702 void
703 ukbd_set_leds(void *v, int leds)
704 {
705 	struct ukbd_softc *sc = v;
706 	u_int8_t res;
707 
708 	DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n",
709 		 sc, leds, sc->sc_leds));
710 
711 	if (sc->sc_dying)
712 		return;
713 
714 	if (sc->sc_leds == leds)
715 		return;
716 	sc->sc_leds = leds;
717 	res = 0;
718 	/* XXX not really right */
719 	if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
720 		res |= 1 << sc->sc_scroloc.pos;
721 	if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
722 		res |= 1 << sc->sc_numloc.pos;
723 	if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
724 		res |= 1 << sc->sc_capsloc.pos;
725 	uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1);
726 }
727 
728 #ifdef WSDISPLAY_COMPAT_RAWKBD
729 void
730 ukbd_rawrepeat(void *v)
731 {
732 	struct ukbd_softc *sc = v;
733 	int s;
734 
735 	s = spltty();
736 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
737 	splx(s);
738 	timeout_add_msec(&sc->sc_rawrepeat_ch, REP_DELAYN);
739 }
740 #endif
741 
742 int
743 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
744 {
745 	struct ukbd_softc *sc = v;
746 
747 	switch (cmd) {
748 	case WSKBDIO_GTYPE:
749 		*(int *)data = WSKBD_TYPE_USB;
750 		return (0);
751 	case WSKBDIO_SETLEDS:
752 		ukbd_set_leds(v, *(int *)data);
753 		return (0);
754 	case WSKBDIO_GETLEDS:
755 		*(int *)data = sc->sc_leds;
756 		return (0);
757 	case WSKBDIO_COMPLEXBELL:
758 #define d ((struct wskbd_bell_data *)data)
759 		ukbd_bell(d->pitch, d->period, d->volume, 0);
760 #undef d
761 		return (0);
762 #ifdef WSDISPLAY_COMPAT_RAWKBD
763 	case WSKBDIO_SETMODE:
764 		DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
765 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
766 		timeout_del(&sc->sc_rawrepeat_ch);
767 		return (0);
768 #endif
769 	}
770 	return (-1);
771 }
772 
773 void
774 ukbd_bell(u_int pitch, u_int period, u_int volume, int poll)
775 {
776 	if (ukbd_bell_fn != NULL)
777 		(*ukbd_bell_fn)(ukbd_bell_fn_arg, pitch, period,
778 		    volume, poll);
779 }
780 
781 void
782 ukbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
783 {
784 	if (ukbd_bell_fn == NULL) {
785 		ukbd_bell_fn = fn;
786 		ukbd_bell_fn_arg = arg;
787 	}
788 }
789 
790 /* Console interface. */
791 void
792 ukbd_cngetc(void *v, u_int *type, int *data)
793 {
794 	struct ukbd_softc *sc = v;
795 	int c;
796 
797 	DPRINTFN(0,("ukbd_cngetc: enter\n"));
798 	sc->sc_polling = 1;
799 	while(sc->sc_npollchar <= 0)
800 		usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface);
801 	sc->sc_polling = 0;
802 	c = sc->sc_pollchars[0];
803 	sc->sc_npollchar--;
804 	memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
805 	       sc->sc_npollchar * sizeof(u_int16_t));
806 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
807 	*data = c & CODEMASK;
808 	DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
809 }
810 
811 void
812 ukbd_cnpollc(void *v, int on)
813 {
814 	struct ukbd_softc *sc = v;
815 	usbd_device_handle dev;
816 
817 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
818 
819 	usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev);
820 	if (on)
821 		sc->sc_spl = splusb();
822 	else
823 		splx(sc->sc_spl);
824 	usbd_set_polling(dev, on);
825 }
826 
827 void
828 ukbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
829 {
830 	ukbd_bell(pitch, period, volume, 1);
831 }
832 
833 int
834 ukbd_cnattach(void)
835 {
836 
837 	/*
838 	 * XXX USB requires too many parts of the kernel to be running
839 	 * XXX in order to work, so we can't do much for the console
840 	 * XXX keyboard until autconfiguration has run its course.
841 	 */
842 	ukbd_is_console = 1;
843 	return (0);
844 }
845 
846 const char *
847 ukbd_parse_desc(struct ukbd_softc *sc)
848 {
849 	struct hid_data *d;
850 	struct hid_item h;
851 	int size;
852 	void *desc;
853 	int imod;
854 
855 	uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
856 	imod = 0;
857 	sc->sc_nkeycode = 0;
858 	d = hid_start_parse(desc, size, hid_input);
859 	while (hid_get_item(d, &h)) {
860 		/*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n",
861 		  h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/
862 		if (h.kind != hid_input || (h.flags & HIO_CONST) ||
863 		    HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
864 		    h.report_ID != sc->sc_hdev.sc_report_id)
865 			continue;
866 		DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d "
867 			 "cnt=%d\n", imod,
868 			 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
869 		if (h.flags & HIO_VARIABLE) {
870 			if (h.loc.size != 1)
871 				return ("bad modifier size");
872 			/* Single item */
873 			if (imod < MAXMOD) {
874 				sc->sc_modloc[imod] = h.loc;
875 				sc->sc_mods[imod].mask = 1 << imod;
876 				sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
877 				imod++;
878 			} else
879 				return ("too many modifier keys");
880 		} else {
881 			/* Array */
882 			if (h.loc.size != 8)
883 				return ("key code size != 8");
884 			if (h.loc.count > MAXKEYCODE)
885 				return ("too many key codes");
886 			if (h.loc.pos % 8 != 0)
887 				return ("key codes not on byte boundary");
888 			if (sc->sc_nkeycode != 0)
889 				return ("multiple key code arrays\n");
890 			sc->sc_keycodeloc = h.loc;
891 			sc->sc_nkeycode = h.loc.count;
892 		}
893 	}
894 	sc->sc_nmod = imod;
895 	hid_end_parse(d);
896 
897 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
898 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL);
899 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
900 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL);
901 	hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
902 		   sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL);
903 
904 	return (NULL);
905 }
906