xref: /netbsd-src/sys/dev/usb/ukbd.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*      $NetBSD: ukbd.c,v 1.60 2000/06/01 14:29:00 augustss 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/usbhid10.pdf
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/callout.h>
47 #include <sys/kernel.h>
48 #include <sys/device.h>
49 #include <sys/ioctl.h>
50 #include <sys/tty.h>
51 #include <sys/file.h>
52 #include <sys/select.h>
53 #include <sys/proc.h>
54 #include <sys/vnode.h>
55 #include <sys/poll.h>
56 
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbhid.h>
59 
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
62 #include <dev/usb/usbdevs.h>
63 #include <dev/usb/usb_quirks.h>
64 #include <dev/usb/hid.h>
65 #include <dev/usb/ukbdvar.h>
66 
67 #include <dev/wscons/wsconsio.h>
68 #include <dev/wscons/wskbdvar.h>
69 #include <dev/wscons/wsksymdef.h>
70 #include <dev/wscons/wsksymvar.h>
71 
72 #include "opt_wsdisplay_compat.h"
73 
74 #ifdef USB_DEBUG
75 #define DPRINTF(x)	if (ukbddebug) logprintf x
76 #define DPRINTFN(n,x)	if (ukbddebug>(n)) logprintf x
77 int	ukbddebug = 0;
78 #else
79 #define DPRINTF(x)
80 #define DPRINTFN(n,x)
81 #endif
82 
83 #define NKEYCODE 6
84 
85 #define NUM_LOCK 0x01
86 #define CAPS_LOCK 0x02
87 #define SCROLL_LOCK 0x04
88 
89 struct ukbd_data {
90 	u_int8_t	modifiers;
91 #define MOD_CONTROL_L	0x01
92 #define MOD_CONTROL_R	0x10
93 #define MOD_SHIFT_L	0x02
94 #define MOD_SHIFT_R	0x20
95 #define MOD_ALT_L	0x04
96 #define MOD_ALT_R	0x40
97 #define MOD_WIN_L	0x08
98 #define MOD_WIN_R	0x80
99 	u_int8_t	reserved;
100 	u_int8_t	keycode[NKEYCODE];
101 };
102 
103 #define PRESS    0x000
104 #define RELEASE  0x100
105 #define CODEMASK 0x0ff
106 
107 /* Translate USB bitmap to USB keycode. */
108 #define NMOD 8
109 Static struct {
110 	int mask, key;
111 } ukbd_mods[NMOD] = {
112 	{ MOD_CONTROL_L, 224 },
113 	{ MOD_CONTROL_R, 228 },
114 	{ MOD_SHIFT_L,   225 },
115 	{ MOD_SHIFT_R,   229 },
116 	{ MOD_ALT_L,     226 },
117 	{ MOD_ALT_R,     230 },
118 	{ MOD_WIN_L,     227 },
119 	{ MOD_WIN_R,     231 },
120 };
121 
122 #if defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD)
123 #define NN 0			/* no translation */
124 /*
125  * Translate USB keycodes to US keyboard XT scancodes.
126  * Scancodes >= 128 represent EXTENDED keycodes.
127  */
128 Static u_int8_t ukbd_trtab[256] = {
129 	  NN,  NN,  NN,  NN,  30,  48,  46,  32, /* 00 - 07 */
130 	  18,  33,  34,  35,  23,  36,  37,  38, /* 08 - 0F */
131 	  50,  49,  24,  25,  16,  19,  31,  20, /* 10 - 17 */
132 	  22,  47,  17,  45,  21,  44,   2,   3, /* 18 - 1F */
133 	   4,   5,   6,   7,   8,   9,  10,  11, /* 20 - 27 */
134 	  28,   1,  14,  15,  57,  12,  13,  26, /* 28 - 2F */
135 	  27,  43,  NN,  39,  40,  41,  51,  52, /* 30 - 37 */
136 	  53,  58,  59,  60,  61,  62,  63,  64, /* 38 - 3F */
137 	  65,  66,  67,  68,  87,  88, 170,  70, /* 40 - 47 */
138 	 127, 210, 199, 201, 211, 207, 209, 205, /* 48 - 4F */
139 	 203, 208, 200,  69, 181,  55,  74,  78, /* 50 - 57 */
140 	 156,  79,  80,  81,  75,  76,  77,  71, /* 58 - 5F */
141           72,  73,  82,  83,  NN, 221,  NN,  NN, /* 60 - 67 */
142           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 68 - 6F */
143           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 70 - 77 */
144           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 78 - 7F */
145           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 80 - 87 */
146           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 88 - 8F */
147           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 90 - 97 */
148           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* 98 - 9F */
149           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* A0 - A7 */
150           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* A8 - AF */
151           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* B0 - B7 */
152           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* B8 - BF */
153           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* C0 - C7 */
154           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* C8 - CF */
155           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* D0 - D7 */
156           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* D8 - DF */
157           29,  42,  56, 219,  157, 54,  184,220, /* E0 - E7 */
158           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* E8 - EF */
159           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* F0 - F7 */
160           NN,  NN,  NN,  NN,  NN,  NN,  NN,  NN, /* F8 - FF */
161 };
162 #endif /* defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD) */
163 
164 #define KEY_ERROR 0x01
165 
166 #define MAXKEYS (NMOD+2*NKEYCODE)
167 
168 struct ukbd_softc {
169 	USBBASEDEVICE	sc_dev;		/* base device */
170 	usbd_device_handle sc_udev;
171 	usbd_interface_handle sc_iface;	/* interface */
172 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
173 	int sc_ep_addr;
174 
175 	struct ukbd_data sc_ndata;
176 	struct ukbd_data sc_odata;
177 
178 	char sc_enabled;
179 
180 	int sc_console_keyboard;	/* we are the console keyboard */
181 
182 	int sc_leds;
183 #if defined(__NetBSD__)
184 	struct callout sc_rawrepeat_ch;
185 
186 	struct device *sc_wskbddev;
187 #if defined(WSDISPLAY_COMPAT_RAWKBD)
188 #define REP_DELAY1 400
189 #define REP_DELAYN 100
190 	int sc_rawkbd;
191 	int sc_nrep;
192 	char sc_rep[MAXKEYS];
193 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
194 
195 	int sc_polling;
196 	int sc_npollchar;
197 	u_int16_t sc_pollchars[MAXKEYS];
198 #endif /* defined(__NetBSD__) */
199 
200 	u_char sc_dying;
201 };
202 
203 #define	UKBDUNIT(dev)	(minor(dev))
204 #define	UKBD_CHUNK	128	/* chunk size for read */
205 #define	UKBD_BSIZE	1020	/* buffer size */
206 
207 Static int	ukbd_is_console;
208 
209 Static void	ukbd_cngetc(void *, u_int *, int *);
210 Static void	ukbd_cnpollc(void *, int);
211 
212 #if defined(__NetBSD__)
213 const struct wskbd_consops ukbd_consops = {
214 	ukbd_cngetc,
215 	ukbd_cnpollc,
216 };
217 #endif
218 
219 Static void	ukbd_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
220 
221 Static int	ukbd_enable(void *, int);
222 Static void	ukbd_set_leds(void *, int);
223 
224 #if defined(__NetBSD__)
225 Static int	ukbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
226 #ifdef WSDISPLAY_COMPAT_RAWKBD
227 Static void	ukbd_rawrepeat(void *v);
228 #endif
229 
230 const struct wskbd_accessops ukbd_accessops = {
231 	ukbd_enable,
232 	ukbd_set_leds,
233 	ukbd_ioctl,
234 };
235 
236 extern const struct wscons_keydesc ukbd_keydesctab[];
237 
238 const struct wskbd_mapdata ukbd_keymapdata = {
239 	ukbd_keydesctab,
240 	KB_US,
241 };
242 #endif
243 
244 USB_DECLARE_DRIVER(ukbd);
245 
246 USB_MATCH(ukbd)
247 {
248 	USB_MATCH_START(ukbd, uaa);
249 	usb_interface_descriptor_t *id;
250 
251 	/* Check that this is a keyboard that speaks the boot protocol. */
252 	if (uaa->iface == NULL)
253 		return (UMATCH_NONE);
254 	id = usbd_get_interface_descriptor(uaa->iface);
255 	if (id == NULL ||
256 	    id->bInterfaceClass != UICLASS_HID ||
257 	    id->bInterfaceSubClass != UISUBCLASS_BOOT ||
258 	    id->bInterfaceProtocol != UIPROTO_BOOT_KEYBOARD)
259 		return (UMATCH_NONE);
260 	return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
261 }
262 
263 USB_ATTACH(ukbd)
264 {
265 	USB_ATTACH_START(ukbd, sc, uaa);
266 	usbd_interface_handle iface = uaa->iface;
267 	usb_interface_descriptor_t *id;
268 	usb_endpoint_descriptor_t *ed;
269 	usbd_status err;
270 	char devinfo[1024];
271 #if defined(__NetBSD__)
272 	struct wskbddev_attach_args a;
273 #else
274 	int i;
275 #endif
276 
277 	sc->sc_udev = uaa->device;
278 	sc->sc_iface = iface;
279 	id = usbd_get_interface_descriptor(iface);
280 	usbd_devinfo(uaa->device, 0, devinfo);
281 	USB_ATTACH_SETUP;
282 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
283 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
284 
285 	ed = usbd_interface2endpoint_descriptor(iface, 0);
286 	if (ed == NULL) {
287 		printf("%s: could not read endpoint descriptor\n",
288 		       USBDEVNAME(sc->sc_dev));
289 		USB_ATTACH_ERROR_RETURN;
290 	}
291 
292 	DPRINTFN(10,("ukbd_attach: bLength=%d bDescriptorType=%d "
293 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
294 		     " bInterval=%d\n",
295 		     ed->bLength, ed->bDescriptorType,
296 		     ed->bEndpointAddress & UE_ADDR,
297 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
298 		     ed->bmAttributes & UE_XFERTYPE,
299 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
300 
301 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
302 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
303 		printf("%s: unexpected endpoint\n",
304 		       USBDEVNAME(sc->sc_dev));
305 		USB_ATTACH_ERROR_RETURN;
306 	}
307 
308 	if ((usbd_get_quirks(uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
309 		err = usbd_set_protocol(iface, 0);
310 		DPRINTFN(5, ("ukbd_attach: protocol set\n"));
311 		if (err) {
312 			printf("%s: set protocol failed\n",
313 			    USBDEVNAME(sc->sc_dev));
314 			USB_ATTACH_ERROR_RETURN;
315 		}
316 	}
317 
318 	/* Ignore if SETIDLE fails since it is not crucial. */
319 	usbd_set_idle(iface, 0, 0);
320 
321 	sc->sc_ep_addr = ed->bEndpointAddress;
322 
323 	/*
324 	 * Remember if we're the console keyboard.
325 	 *
326 	 * XXX This always picks the first keyboard on the
327 	 * first USB bus, but what else can we really do?
328 	 */
329 	if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
330 		/* Don't let any other keyboard have it. */
331 		ukbd_is_console = 0;
332 	}
333 
334 	if (sc->sc_console_keyboard) {
335 		DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
336 		wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
337 		ukbd_enable(sc, 1);
338 	}
339 
340 	a.console = sc->sc_console_keyboard;
341 
342 	a.keymap = &ukbd_keymapdata;
343 
344 	a.accessops = &ukbd_accessops;
345 	a.accesscookie = sc;
346 
347 	callout_init(&sc->sc_rawrepeat_ch);
348 
349 	/* Flash the leds; no real purpose, just shows we're alive. */
350 	ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
351 	usbd_delay_ms(uaa->device, 400);
352 	ukbd_set_leds(sc, 0);
353 
354 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
355 
356 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
357 			   USBDEV(sc->sc_dev));
358 
359 	USB_ATTACH_SUCCESS_RETURN;
360 }
361 
362 int
363 ukbd_enable(void *v, int on)
364 {
365 	struct ukbd_softc *sc = v;
366 	usbd_status err;
367 
368 	if (on && sc->sc_dying)
369 		return (EIO);
370 
371 	/* Should only be called to change state */
372 	if (sc->sc_enabled == on) {
373 #ifdef DIAGNOSTIC
374 		printf("ukbd_enable: %s: bad call on=%d\n",
375 		       USBDEVNAME(sc->sc_dev), on);
376 #endif
377 		return (EBUSY);
378 	}
379 
380 	DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
381 	if (on) {
382 		/* Set up interrupt pipe. */
383 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
384 			  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
385 			  &sc->sc_ndata, sizeof(sc->sc_ndata), ukbd_intr,
386 			  USBD_DEFAULT_INTERVAL);
387 		if (err)
388 			return (EIO);
389 	} else {
390 		/* Disable interrupts. */
391 		usbd_abort_pipe(sc->sc_intrpipe);
392 		usbd_close_pipe(sc->sc_intrpipe);
393 	}
394 	sc->sc_enabled = on;
395 
396 	return (0);
397 }
398 
399 int
400 ukbd_activate(device_ptr_t self, enum devact act)
401 {
402 	struct ukbd_softc *sc = (struct ukbd_softc *)self;
403 	int rv = 0;
404 
405 	switch (act) {
406 	case DVACT_ACTIVATE:
407 		return (EOPNOTSUPP);
408 		break;
409 
410 	case DVACT_DEACTIVATE:
411 		if (sc->sc_wskbddev != NULL)
412 			rv = config_deactivate(sc->sc_wskbddev);
413 		sc->sc_dying = 1;
414 		break;
415 	}
416 	return (rv);
417 }
418 
419 USB_DETACH(ukbd)
420 {
421 	USB_DETACH_START(ukbd, sc);
422 	int rv = 0;
423 
424 	DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
425 
426 	if (sc->sc_console_keyboard) {
427 #if 0
428 		/*
429 		 * XXX Should probably disconnect our consops,
430 		 * XXX and either notify some other keyboard that
431 		 * XXX it can now be the console, or if there aren't
432 		 * XXX any more USB keyboards, set ukbd_is_console
433 		 * XXX back to 1 so that the next USB keyboard attached
434 		 * XXX to the system will get it.
435 		 */
436 		panic("ukbd_detach: console keyboard");
437 #else
438 		/*
439 		 * Disconnect our consops and set ukbd_is_console
440 		 * back to 1 so that the next USB keyboard attached
441 		 * to the system will get it.
442 		 * XXX Should notify some other keyboard that it can be
443 		 * XXX console, if there are any other keyboards.
444 		 */
445 		printf("%s: was console keyboard\n", USBDEVNAME(sc->sc_dev));
446 		wskbd_cndetach();
447 		ukbd_is_console = 1;
448 #endif
449 	}
450 	/* No need to do reference counting of ukbd, wskbd has all the goo. */
451 	if (sc->sc_wskbddev != NULL)
452 		rv = config_detach(sc->sc_wskbddev, flags);
453 
454 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
455 			   USBDEV(sc->sc_dev));
456 
457 	return (rv);
458 }
459 
460 void
461 ukbd_intr(xfer, addr, status)
462 	usbd_xfer_handle xfer;
463 	usbd_private_handle addr;
464 	usbd_status status;
465 {
466 	struct ukbd_softc *sc = addr;
467 	struct ukbd_data *ud = &sc->sc_ndata;
468 	int mod, omod;
469 	u_int16_t ibuf[MAXKEYS];	/* chars events */
470 	int s;
471 	int nkeys, i, j;
472 	int key;
473 #define ADDKEY(c) ibuf[nkeys++] = (c)
474 
475 	DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
476 	if (status == USBD_CANCELLED)
477 		return;
478 
479 	if (status) {
480 		DPRINTF(("ukbd_intr: status=%d\n", status));
481 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
482 		return;
483 	}
484 
485 	DPRINTFN(5, ("          mod=0x%02x key0=0x%02x key1=0x%02x\n",
486 		     ud->modifiers, ud->keycode[0], ud->keycode[1]));
487 
488 	if (ud->keycode[0] == KEY_ERROR) {
489 		DPRINTF(("ukbd_intr: KEY_ERROR\n"));
490 		return;		/* ignore  */
491 	}
492 	nkeys = 0;
493 	mod = ud->modifiers;
494 	omod = sc->sc_odata.modifiers;
495 	if (mod != omod)
496 		for (i = 0; i < NMOD; i++)
497 			if (( mod & ukbd_mods[i].mask) !=
498 			    (omod & ukbd_mods[i].mask))
499 				ADDKEY(ukbd_mods[i].key |
500 				       (mod & ukbd_mods[i].mask
501 					  ? PRESS : RELEASE));
502 	if (memcmp(ud->keycode, sc->sc_odata.keycode, NKEYCODE) != 0) {
503 		/* Check for released keys. */
504 		for (i = 0; i < NKEYCODE; i++) {
505 			key = sc->sc_odata.keycode[i];
506 			if (key == 0)
507 				continue;
508 			for (j = 0; j < NKEYCODE; j++)
509 				if (key == ud->keycode[j])
510 					goto rfound;
511 			ADDKEY(key | RELEASE);
512 		rfound:
513 			;
514 		}
515 
516 		/* Check for pressed keys. */
517 		for (i = 0; i < NKEYCODE; i++) {
518 			key = ud->keycode[i];
519 			if (key == 0)
520 				continue;
521 			for (j = 0; j < NKEYCODE; j++)
522 				if (key == sc->sc_odata.keycode[j])
523 					goto pfound;
524 			DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
525 			ADDKEY(key | PRESS);
526 		pfound:
527 			;
528 		}
529 	}
530 	sc->sc_odata = *ud;
531 
532 	if (nkeys == 0)
533 		return;
534 
535 	if (sc->sc_polling) {
536 		DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
537 		memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
538 		sc->sc_npollchar = nkeys;
539 		return;
540 	}
541 #ifdef WSDISPLAY_COMPAT_RAWKBD
542 	if (sc->sc_rawkbd) {
543 		char cbuf[MAXKEYS * 2];
544 		int c;
545 		int npress;
546 
547 		for (npress = i = j = 0; i < nkeys; i++) {
548 			key = ibuf[i];
549 			c = ukbd_trtab[key & CODEMASK];
550 			if (c == NN)
551 				continue;
552 			if (c & 0x80)
553 				cbuf[j++] = 0xe0;
554 			cbuf[j] = c & 0x7f;
555 			if (key & RELEASE)
556 				cbuf[j] |= 0x80;
557 			else {
558 				/* remember pressed keys for autorepeat */
559 				if (c & 0x80)
560 					sc->sc_rep[npress++] = 0xe0;
561 				sc->sc_rep[npress++] = c & 0x7f;
562 			}
563 			DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
564 				    c & 0x80 ? "0xe0 " : "",
565 				    cbuf[j]));
566 			j++;
567 		}
568 		s = spltty();
569 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
570 		splx(s);
571 		callout_stop(&sc->sc_rawrepeat_ch);
572 		if (npress != 0) {
573 			sc->sc_nrep = npress;
574 			callout_reset(&sc->sc_rawrepeat_ch,
575 			    hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
576 		}
577 		return;
578 	}
579 #endif
580 
581 	s = spltty();
582 	for (i = 0; i < nkeys; i++) {
583 		key = ibuf[i];
584 		wskbd_input(sc->sc_wskbddev,
585 		    key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
586 		    key&CODEMASK);
587 	}
588 	splx(s);
589 }
590 
591 void
592 ukbd_set_leds(void *v, int leds)
593 {
594 	struct ukbd_softc *sc = v;
595 	u_int8_t res;
596 
597 	DPRINTF(("ukbd_set_leds: sc=%p leds=%d\n", sc, leds));
598 
599 	if (sc->sc_dying)
600 		return;
601 
602 	sc->sc_leds = leds;
603 	res = 0;
604 	if (leds & WSKBD_LED_SCROLL)
605 		res |= SCROLL_LOCK;
606 	if (leds & WSKBD_LED_NUM)
607 		res |= NUM_LOCK;
608 	if (leds & WSKBD_LED_CAPS)
609 		res |= CAPS_LOCK;
610 	res |= leds & 0xf8;
611 	usbd_set_report_async(sc->sc_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
612 }
613 
614 #ifdef WSDISPLAY_COMPAT_RAWKBD
615 void
616 ukbd_rawrepeat(void *v)
617 {
618 	struct ukbd_softc *sc = v;
619 	int s;
620 
621 	s = spltty();
622 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
623 	splx(s);
624 	callout_reset(&sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
625 	    ukbd_rawrepeat, sc);
626 }
627 #endif
628 
629 int
630 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
631 {
632 	struct ukbd_softc *sc = v;
633 
634 	switch (cmd) {
635 	case WSKBDIO_GTYPE:
636 		*(int *)data = WSKBD_TYPE_USB;
637 		return (0);
638 	case WSKBDIO_SETLEDS:
639 		ukbd_set_leds(v, *(int *)data);
640 		return (0);
641 	case WSKBDIO_GETLEDS:
642 		*(int *)data = sc->sc_leds;
643 		return (0);
644 #ifdef WSDISPLAY_COMPAT_RAWKBD
645 	case WSKBDIO_SETMODE:
646 		DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
647 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
648 		callout_stop(&sc->sc_rawrepeat_ch);
649 		return (0);
650 #endif
651 	}
652 	return (-1);
653 }
654 
655 /* Console interface. */
656 void
657 ukbd_cngetc(void *v, u_int *type, int *data)
658 {
659 	struct ukbd_softc *sc = v;
660 	int s;
661 	int c;
662 
663 	DPRINTFN(0,("ukbd_cngetc: enter\n"));
664 	s = splusb();
665 	sc->sc_polling = 1;
666 	while(sc->sc_npollchar <= 0)
667 		usbd_dopoll(sc->sc_iface);
668 	sc->sc_polling = 0;
669 	c = sc->sc_pollchars[0];
670 	sc->sc_npollchar--;
671 	memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
672 	       sc->sc_npollchar * sizeof(u_int16_t));
673 	*type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
674 	*data = c & CODEMASK;
675 	splx(s);
676 	DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
677 }
678 
679 void
680 ukbd_cnpollc(void *v, int on)
681 {
682 	struct ukbd_softc *sc = v;
683 	usbd_device_handle dev;
684 
685 	DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
686 
687 	(void)usbd_interface2device_handle(sc->sc_iface,&dev);
688 	usbd_set_polling(dev, on);
689 }
690 
691 int
692 ukbd_cnattach(void)
693 {
694 
695 	/*
696 	 * XXX USB requires too many parts of the kernel to be running
697 	 * XXX in order to work, so we can't do much for the console
698 	 * XXX keyboard until autconfiguration has run its course.
699 	 */
700 	ukbd_is_console = 1;
701 	return (0);
702 }
703