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