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