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