ukbd.c revision 1.57 1 /* $NetBSD: ukbd.c,v 1.57 2000/03/23 07:01:46 thorpej 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 (augustss (at) carlstedt.se) 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 __P((void *, u_int *, int *));
210 static void ukbd_cnpollc __P((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 __P((usbd_xfer_handle, usbd_private_handle,
220 usbd_status));
221
222 static int ukbd_enable __P((void *, int));
223 static void ukbd_set_leds __P((void *, int));
224
225 #if defined(__NetBSD__)
226 static int ukbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
227 #ifdef WSDISPLAY_COMPAT_RAWKBD
228 static void ukbd_rawrepeat __P((void *v));
229 #endif
230
231 const struct wskbd_accessops ukbd_accessops = {
232 ukbd_enable,
233 ukbd_set_leds,
234 ukbd_ioctl,
235 };
236
237 extern const struct wscons_keydesc ukbd_keydesctab[];
238
239 const struct wskbd_mapdata ukbd_keymapdata = {
240 ukbd_keydesctab,
241 KB_US,
242 };
243 #endif
244
245 USB_DECLARE_DRIVER(ukbd);
246
247 USB_MATCH(ukbd)
248 {
249 USB_MATCH_START(ukbd, uaa);
250 usb_interface_descriptor_t *id;
251
252 /* Check that this is a keyboard that speaks the boot protocol. */
253 if (uaa->iface == NULL)
254 return (UMATCH_NONE);
255 id = usbd_get_interface_descriptor(uaa->iface);
256 if (id == NULL ||
257 id->bInterfaceClass != UICLASS_HID ||
258 id->bInterfaceSubClass != UISUBCLASS_BOOT ||
259 id->bInterfaceProtocol != UIPROTO_BOOT_KEYBOARD)
260 return (UMATCH_NONE);
261 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
262 }
263
264 USB_ATTACH(ukbd)
265 {
266 USB_ATTACH_START(ukbd, sc, uaa);
267 usbd_interface_handle iface = uaa->iface;
268 usb_interface_descriptor_t *id;
269 usb_endpoint_descriptor_t *ed;
270 usbd_status err;
271 char devinfo[1024];
272 #if defined(__NetBSD__)
273 struct wskbddev_attach_args a;
274 #else
275 int i;
276 #endif
277
278 sc->sc_udev = uaa->device;
279 sc->sc_iface = iface;
280 id = usbd_get_interface_descriptor(iface);
281 usbd_devinfo(uaa->device, 0, devinfo);
282 USB_ATTACH_SETUP;
283 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
284 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
285
286 ed = usbd_interface2endpoint_descriptor(iface, 0);
287 if (ed == NULL) {
288 printf("%s: could not read endpoint descriptor\n",
289 USBDEVNAME(sc->sc_dev));
290 USB_ATTACH_ERROR_RETURN;
291 }
292
293 DPRINTFN(10,("ukbd_attach: bLength=%d bDescriptorType=%d "
294 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
295 " bInterval=%d\n",
296 ed->bLength, ed->bDescriptorType,
297 ed->bEndpointAddress & UE_ADDR,
298 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
299 ed->bmAttributes & UE_XFERTYPE,
300 UGETW(ed->wMaxPacketSize), ed->bInterval));
301
302 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
303 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
304 printf("%s: unexpected endpoint\n",
305 USBDEVNAME(sc->sc_dev));
306 USB_ATTACH_ERROR_RETURN;
307 }
308
309 if ((usbd_get_quirks(uaa->device)->uq_flags & UQ_NO_SET_PROTO) == 0) {
310 err = usbd_set_protocol(iface, 0);
311 DPRINTFN(5, ("ukbd_attach: protocol set\n"));
312 if (err) {
313 printf("%s: set protocol failed\n",
314 USBDEVNAME(sc->sc_dev));
315 USB_ATTACH_ERROR_RETURN;
316 }
317 }
318
319 /* Ignore if SETIDLE fails since it is not crucial. */
320 usbd_set_idle(iface, 0, 0);
321
322 sc->sc_ep_addr = ed->bEndpointAddress;
323
324 /*
325 * Remember if we're the console keyboard.
326 *
327 * XXX This always picks the first keyboard on the
328 * first USB bus, but what else can we really do?
329 */
330 if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
331 /* Don't let any other keyboard have it. */
332 ukbd_is_console = 0;
333 }
334
335 if (sc->sc_console_keyboard) {
336 DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
337 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
338 ukbd_enable(sc, 1);
339 }
340
341 a.console = sc->sc_console_keyboard;
342
343 a.keymap = &ukbd_keymapdata;
344
345 a.accessops = &ukbd_accessops;
346 a.accesscookie = sc;
347
348 callout_init(&sc->sc_rawrepeat_ch);
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(uaa->device, 400);
353 ukbd_set_leds(sc, 0);
354
355 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
356
357 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
358 USBDEV(sc->sc_dev));
359
360 USB_ATTACH_SUCCESS_RETURN;
361 }
362
363 int
364 ukbd_enable(v, on)
365 void *v;
366 int on;
367 {
368 struct ukbd_softc *sc = v;
369 usbd_status err;
370
371 if (on && sc->sc_dying)
372 return (EIO);
373
374 /* Should only be called to change state */
375 if (sc->sc_enabled == on) {
376 #ifdef DIAGNOSTIC
377 printf("ukbd_enable: %s: bad call on=%d\n",
378 USBDEVNAME(sc->sc_dev), on);
379 #endif
380 return (EBUSY);
381 }
382
383 DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
384 if (on) {
385 /* Set up interrupt pipe. */
386 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
387 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
388 &sc->sc_ndata, sizeof(sc->sc_ndata), ukbd_intr,
389 USBD_DEFAULT_INTERVAL);
390 if (err)
391 return (EIO);
392 } else {
393 /* Disable interrupts. */
394 usbd_abort_pipe(sc->sc_intrpipe);
395 usbd_close_pipe(sc->sc_intrpipe);
396 }
397 sc->sc_enabled = on;
398
399 return (0);
400 }
401
402 int
403 ukbd_activate(self, act)
404 device_ptr_t self;
405 enum devact act;
406 {
407 struct ukbd_softc *sc = (struct ukbd_softc *)self;
408 int rv = 0;
409
410 switch (act) {
411 case DVACT_ACTIVATE:
412 return (EOPNOTSUPP);
413 break;
414
415 case DVACT_DEACTIVATE:
416 if (sc->sc_wskbddev != NULL)
417 rv = config_deactivate(sc->sc_wskbddev);
418 sc->sc_dying = 1;
419 break;
420 }
421 return (rv);
422 }
423
424 USB_DETACH(ukbd)
425 {
426 USB_DETACH_START(ukbd, sc);
427 int rv = 0;
428
429 DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
430
431 if (sc->sc_console_keyboard) {
432 #if 0
433 /*
434 * XXX Should probably disconnect our consops,
435 * XXX and either notify some other keyboard that
436 * XXX it can now be the console, or if there aren't
437 * XXX any more USB keyboards, set ukbd_is_console
438 * XXX back to 1 so that the next USB keyboard attached
439 * XXX to the system will get it.
440 */
441 panic("ukbd_detach: console keyboard");
442 #else
443 /*
444 * Disconnect our consops and set ukbd_is_console
445 * back to 1 so that the next USB keyboard attached
446 * to the system will get it.
447 * XXX Should notify some other keyboard that it can be
448 * XXX console, if there are any other keyboards.
449 */
450 printf("%s: was console keyboard\n", USBDEVNAME(sc->sc_dev));
451 wskbd_cndetach();
452 ukbd_is_console = 1;
453 #endif
454 }
455 /* No need to do reference counting of ukbd, wskbd has all the goo. */
456 if (sc->sc_wskbddev != NULL)
457 rv = config_detach(sc->sc_wskbddev, flags);
458
459 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
460 USBDEV(sc->sc_dev));
461
462 return (rv);
463 }
464
465 void
466 ukbd_intr(xfer, addr, status)
467 usbd_xfer_handle xfer;
468 usbd_private_handle addr;
469 usbd_status status;
470 {
471 struct ukbd_softc *sc = addr;
472 struct ukbd_data *ud = &sc->sc_ndata;
473 int mod, omod;
474 u_int16_t ibuf[MAXKEYS]; /* chars events */
475 int s;
476 int nkeys, i, j;
477 int key;
478 #define ADDKEY(c) ibuf[nkeys++] = (c)
479
480 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
481 if (status == USBD_CANCELLED)
482 return;
483
484 if (status) {
485 DPRINTF(("ukbd_intr: status=%d\n", status));
486 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
487 return;
488 }
489
490 DPRINTFN(5, (" mod=0x%02x key0=0x%02x key1=0x%02x\n",
491 ud->modifiers, ud->keycode[0], ud->keycode[1]));
492
493 if (ud->keycode[0] == KEY_ERROR) {
494 DPRINTF(("ukbd_intr: KEY_ERROR\n"));
495 return; /* ignore */
496 }
497 nkeys = 0;
498 mod = ud->modifiers;
499 omod = sc->sc_odata.modifiers;
500 if (mod != omod)
501 for (i = 0; i < NMOD; i++)
502 if (( mod & ukbd_mods[i].mask) !=
503 (omod & ukbd_mods[i].mask))
504 ADDKEY(ukbd_mods[i].key |
505 (mod & ukbd_mods[i].mask
506 ? PRESS : RELEASE));
507 if (memcmp(ud->keycode, sc->sc_odata.keycode, NKEYCODE) != 0) {
508 /* Check for released keys. */
509 for (i = 0; i < NKEYCODE; i++) {
510 key = sc->sc_odata.keycode[i];
511 if (key == 0)
512 continue;
513 for (j = 0; j < NKEYCODE; j++)
514 if (key == ud->keycode[j])
515 goto rfound;
516 ADDKEY(key | RELEASE);
517 rfound:
518 ;
519 }
520
521 /* Check for pressed keys. */
522 for (i = 0; i < NKEYCODE; i++) {
523 key = ud->keycode[i];
524 if (key == 0)
525 continue;
526 for (j = 0; j < NKEYCODE; j++)
527 if (key == sc->sc_odata.keycode[j])
528 goto pfound;
529 DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
530 ADDKEY(key | PRESS);
531 pfound:
532 ;
533 }
534 }
535 sc->sc_odata = *ud;
536
537 if (nkeys == 0)
538 return;
539
540 if (sc->sc_polling) {
541 DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
542 memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
543 sc->sc_npollchar = nkeys;
544 return;
545 }
546 #ifdef WSDISPLAY_COMPAT_RAWKBD
547 if (sc->sc_rawkbd) {
548 char cbuf[MAXKEYS * 2];
549 int c;
550 int npress;
551
552 for (npress = i = j = 0; i < nkeys; i++) {
553 key = ibuf[i];
554 c = ukbd_trtab[key & CODEMASK];
555 if (c == NN)
556 continue;
557 if (c & 0x80)
558 cbuf[j++] = 0xe0;
559 cbuf[j] = c & 0x7f;
560 if (key & RELEASE)
561 cbuf[j] |= 0x80;
562 else {
563 /* remember pressed keys for autorepeat */
564 if (c & 0x80)
565 sc->sc_rep[npress++] = 0xe0;
566 sc->sc_rep[npress++] = c & 0x7f;
567 }
568 DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
569 c & 0x80 ? "0xe0 " : "",
570 cbuf[j]));
571 j++;
572 }
573 s = spltty();
574 wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
575 splx(s);
576 callout_stop(&sc->sc_rawrepeat_ch);
577 if (npress != 0) {
578 sc->sc_nrep = npress;
579 callout_reset(&sc->sc_rawrepeat_ch,
580 hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
581 }
582 return;
583 }
584 #endif
585
586 s = spltty();
587 for (i = 0; i < nkeys; i++) {
588 key = ibuf[i];
589 wskbd_input(sc->sc_wskbddev,
590 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
591 key&CODEMASK);
592 }
593 splx(s);
594 }
595
596 void
597 ukbd_set_leds(v, leds)
598 void *v;
599 int leds;
600 {
601 struct ukbd_softc *sc = v;
602 u_int8_t res;
603
604 DPRINTF(("ukbd_set_leds: sc=%p leds=%d\n", sc, leds));
605
606 if (sc->sc_dying)
607 return;
608
609 sc->sc_leds = leds;
610 res = 0;
611 if (leds & WSKBD_LED_SCROLL)
612 res |= SCROLL_LOCK;
613 if (leds & WSKBD_LED_NUM)
614 res |= NUM_LOCK;
615 if (leds & WSKBD_LED_CAPS)
616 res |= CAPS_LOCK;
617 res |= leds & 0xf8;
618 usbd_set_report_async(sc->sc_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
619 }
620
621 #ifdef WSDISPLAY_COMPAT_RAWKBD
622 void
623 ukbd_rawrepeat(v)
624 void *v;
625 {
626 struct ukbd_softc *sc = v;
627 int s;
628
629 s = spltty();
630 wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
631 splx(s);
632 callout_reset(&sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
633 ukbd_rawrepeat, sc);
634 }
635 #endif
636
637 int
638 ukbd_ioctl(v, cmd, data, flag, p)
639 void *v;
640 u_long cmd;
641 caddr_t data;
642 int flag;
643 struct proc *p;
644 {
645 struct ukbd_softc *sc = v;
646
647 switch (cmd) {
648 case WSKBDIO_GTYPE:
649 *(int *)data = WSKBD_TYPE_USB;
650 return (0);
651 case WSKBDIO_SETLEDS:
652 ukbd_set_leds(v, *(int *)data);
653 return (0);
654 case WSKBDIO_GETLEDS:
655 *(int *)data = sc->sc_leds;
656 return (0);
657 #ifdef WSDISPLAY_COMPAT_RAWKBD
658 case WSKBDIO_SETMODE:
659 DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
660 sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
661 callout_stop(&sc->sc_rawrepeat_ch);
662 return (0);
663 #endif
664 }
665 return (-1);
666 }
667
668 /* Console interface. */
669 void
670 ukbd_cngetc(v, type, data)
671 void *v;
672 u_int *type;
673 int *data;
674 {
675 struct ukbd_softc *sc = v;
676 int s;
677 int c;
678
679 DPRINTFN(0,("ukbd_cngetc: enter\n"));
680 s = splusb();
681 sc->sc_polling = 1;
682 while(sc->sc_npollchar <= 0)
683 usbd_dopoll(sc->sc_iface);
684 sc->sc_polling = 0;
685 c = sc->sc_pollchars[0];
686 sc->sc_npollchar--;
687 memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
688 sc->sc_npollchar * sizeof(u_int16_t));
689 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
690 *data = c & CODEMASK;
691 splx(s);
692 DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
693 }
694
695 void
696 ukbd_cnpollc(v, on)
697 void *v;
698 int on;
699 {
700 struct ukbd_softc *sc = v;
701 usbd_device_handle dev;
702
703 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
704
705 (void)usbd_interface2device_handle(sc->sc_iface,&dev);
706 usbd_set_polling(dev, on);
707 }
708
709 int
710 ukbd_cnattach()
711 {
712
713 /*
714 * XXX USB requires too many parts of the kernel to be running
715 * XXX in order to work, so we can't do much for the console
716 * XXX keyboard until autconfiguration has run its course.
717 */
718 ukbd_is_console = 1;
719 return (0);
720 }
721