ukbd.c revision 1.34 1 /* $NetBSD: ukbd.c,v 1.34 1999/06/11 19:05:13 wrstuden 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 ukbd_enable(sc, 1);
349 }
350
351 a.console = sc->sc_console_keyboard;
352
353 a.keymap = &ukbd_keymapdata;
354
355 a.accessops = &ukbd_accessops;
356 a.accesscookie = sc;
357
358 /* Flash the leds; no real purpose, just shows we're alive. */
359 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
360 usbd_delay_ms(uaa->device, 400);
361 ukbd_set_leds(sc, 0);
362
363 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
364
365 #elif defined(__FreeBSD__)
366 /* XXX why waste CPU in delay() ? */
367 /* It's alive! IT'S ALIVE! Do a little song and dance. */
368 ukbd_set_leds(sc, NUM_LOCK);
369 delay(15000);
370 ukbd_set_leds(sc, CAPS_LOCK);
371 delay(20000);
372 ukbd_set_leds(sc, SCROLL_LOCK);
373 delay(30000);
374 ukbd_set_leds(sc, CAPS_LOCK);
375 delay(50000);
376 ukbd_set_leds(sc, NUM_LOCK);
377
378 ukbd_enable(sc, 1);
379 #endif
380
381 USB_ATTACH_SUCCESS_RETURN;
382 }
383
384 #if defined(__FreeBSD__)
385 int
386 ukbd_detach(device_t self)
387 {
388 struct ukbd_softc *sc = device_get_softc(self);
389 char *devinfo = (char *) device_get_desc(self);
390
391 if (sc->sc_enabled)
392 return (ENXIO);
393
394 if (devinfo) {
395 device_set_desc(self, NULL);
396 free(devinfo, M_USB);
397 }
398
399 return (0);
400 }
401 #endif
402
403 void
404 ukbd_disco(p)
405 void *p;
406 {
407 struct ukbd_softc *sc = p;
408
409 DPRINTF(("ukbd_disco: sc=%p\n", sc));
410 usbd_abort_pipe(sc->sc_intrpipe);
411 sc->sc_disconnected = 1;
412
413 if (sc->sc_console_keyboard) {
414 /*
415 * XXX Should probably disconnect our consops,
416 * XXX and either notify some other keyboard that
417 * XXX it can now be the console, or if there aren't
418 * XXX any more USB keyboards, set ukbd_is_console
419 * XXX back to 1 so that the next USB keyboard attached
420 * XXX to the system will get it.
421 */
422 panic("ukbd_disco: console keyboard");
423 }
424 }
425
426 int
427 ukbd_enable(v, on)
428 void *v;
429 int on;
430 {
431 struct ukbd_softc *sc = v;
432 usbd_status r;
433
434 DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
435 if (on) {
436 /* Set up interrupt pipe. */
437 if (sc->sc_enabled)
438 return (EBUSY);
439
440 sc->sc_enabled = 1;
441 r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
442 USBD_SHORT_XFER_OK,
443 &sc->sc_intrpipe, sc, &sc->sc_ndata,
444 sizeof(sc->sc_ndata), ukbd_intr);
445 if (r != USBD_NORMAL_COMPLETION)
446 return (EIO);
447 usbd_set_disco(sc->sc_intrpipe, ukbd_disco, sc);
448 } else {
449 /* Disable interrupts. */
450 usbd_abort_pipe(sc->sc_intrpipe);
451 usbd_close_pipe(sc->sc_intrpipe);
452
453 sc->sc_enabled = 0;
454 }
455
456 return (0);
457 }
458
459 void
460 ukbd_intr(reqh, addr, status)
461 usbd_request_handle reqh;
462 usbd_private_handle addr;
463 usbd_status status;
464 {
465 struct ukbd_softc *sc = addr;
466 struct ukbd_data *ud = &sc->sc_ndata;
467 int mod, omod;
468 u_int16_t ibuf[MAXKEYS]; /* chars events */
469 int s;
470 int nkeys, i, j;
471 int key;
472 #define ADDKEY(c) ibuf[nkeys++] = (c)
473
474 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
475 if (status == USBD_CANCELLED)
476 return;
477
478 if (status != USBD_NORMAL_COMPLETION) {
479 DPRINTF(("ukbd_intr: status=%d\n", status));
480 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
481 return;
482 }
483
484 DPRINTFN(5, (" mod=0x%02x key0=0x%02x key1=0x%02x\n",
485 ud->modifiers, ud->keycode[0], ud->keycode[1]));
486
487 if (ud->keycode[0] == KEY_ERROR)
488 return; /* ignore */
489 nkeys = 0;
490 mod = ud->modifiers;
491 omod = sc->sc_odata.modifiers;
492 if (mod != omod)
493 for (i = 0; i < NMOD; i++)
494 if (( mod & ukbd_mods[i].mask) !=
495 (omod & ukbd_mods[i].mask))
496 ADDKEY(ukbd_mods[i].key |
497 (mod & ukbd_mods[i].mask
498 ? PRESS : RELEASE));
499 if (memcmp(ud->keycode, sc->sc_odata.keycode, NKEYCODE) != 0) {
500 /* Check for released keys. */
501 for (i = 0; i < NKEYCODE; i++) {
502 key = sc->sc_odata.keycode[i];
503 if (key == 0)
504 continue;
505 for (j = 0; j < NKEYCODE; j++)
506 if (key == ud->keycode[j])
507 goto rfound;
508 ADDKEY(key | RELEASE);
509 rfound:
510 ;
511 }
512
513 /* Check for pressed keys. */
514 for (i = 0; i < NKEYCODE; i++) {
515 key = ud->keycode[i];
516 if (key == 0)
517 continue;
518 for (j = 0; j < NKEYCODE; j++)
519 if (key == sc->sc_odata.keycode[j])
520 goto pfound;
521 DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
522 ADDKEY(key | PRESS);
523 pfound:
524 ;
525 }
526 }
527 sc->sc_odata = *ud;
528
529 if (nkeys == 0)
530 return;
531
532 #if defined(__NetBSD__)
533 if (sc->sc_polling) {
534 DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
535 memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
536 sc->sc_npollchar = nkeys;
537 return;
538 }
539 #ifdef WSDISPLAY_COMPAT_RAWKBD
540 if (sc->sc_rawkbd) {
541 char cbuf[MAXKEYS * 2];
542 int c;
543 int npress;
544
545 for (npress = i = j = 0; i < nkeys; i++) {
546 key = ibuf[i];
547 c = ukbd_trtab[key & CODEMASK];
548 if (c == NN)
549 continue;
550 if (c & 0x80)
551 cbuf[j++] = 0xe0;
552 cbuf[j] = c & 0x7f;
553 if (key & RELEASE)
554 cbuf[j] |= 0x80;
555 else {
556 /* remember pressed keys for autorepeat */
557 if (c & 0x80)
558 sc->sc_rep[npress++] = 0xe0;
559 sc->sc_rep[npress++] = c & 0x7f;
560 }
561 DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
562 c & 0x80 ? "0xe0 " : "",
563 cbuf[j]));
564 j++;
565 }
566 s = spltty();
567 wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
568 splx(s);
569 untimeout(ukbd_rawrepeat, sc);
570 if (npress != 0) {
571 sc->sc_nrep = npress;
572 timeout(ukbd_rawrepeat, sc, hz * REP_DELAY1 / 1000);
573 }
574 return;
575 }
576 #endif
577
578 s = spltty();
579 for (i = 0; i < nkeys; i++) {
580 key = ibuf[i];
581 wskbd_input(sc->sc_wskbddev,
582 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
583 key&CODEMASK);
584 }
585 splx(s);
586
587 #elif defined(__FreeBSD__)
588 /* XXX shouldn't the keys be used? */
589 for (i = 0; i < nkeys; i++) {
590 c = ibuf[i];
591 printf("%c (%d) %s\n",
592 ((c&CODEMASK) < 32 || (c&CODEMASK) > 126 ? '.' :
593 (c&CODEMASK)), c,
594 (c&RELEASE? "released":"pressed"));
595 if (ud->modifiers)
596 printf("0x%04x\n", ud->modifiers);
597 for (i = 0; i < NKEYCODE; i++)
598 if (ud->keycode[i])
599 printf("%d ", ud->keycode[i]);
600 printf("\n");
601 }
602 #endif
603 }
604
605 void
606 ukbd_set_leds(v, leds)
607 void *v;
608 int leds;
609 {
610 struct ukbd_softc *sc = v;
611 u_int8_t res;
612
613 DPRINTF(("ukbd_set_leds: sc=%p leds=%d\n", sc, leds));
614
615 sc->sc_leds = leds;
616 #if defined(__NetBSD__)
617 res = 0;
618 if (leds & WSKBD_LED_SCROLL)
619 res |= SCROLL_LOCK;
620 if (leds & WSKBD_LED_NUM)
621 res |= NUM_LOCK;
622 if (leds & WSKBD_LED_CAPS)
623 res |= CAPS_LOCK;
624 #elif defined(__FreeBSD__)
625 res = leds;
626 #endif
627 res |= leds & 0xf8;
628 usbd_set_report_async(sc->sc_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
629 }
630
631 #if defined(__NetBSD__)
632
633 #ifdef WSDISPLAY_COMPAT_RAWKBD
634 void
635 ukbd_rawrepeat(v)
636 void *v;
637 {
638 struct ukbd_softc *sc = v;
639 int s;
640
641 s = spltty();
642 wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
643 splx(s);
644 timeout(ukbd_rawrepeat, sc, hz * REP_DELAYN / 1000);
645 }
646 #endif
647
648 int
649 ukbd_ioctl(v, cmd, data, flag, p)
650 void *v;
651 u_long cmd;
652 caddr_t data;
653 int flag;
654 struct proc *p;
655 {
656 struct ukbd_softc *sc = v;
657
658 switch (cmd) {
659 case WSKBDIO_GTYPE:
660 *(int *)data = WSKBD_TYPE_USB;
661 return (0);
662 case WSKBDIO_SETLEDS:
663 ukbd_set_leds(v, *(int *)data);
664 return (0);
665 case WSKBDIO_GETLEDS:
666 *(int *)data = sc->sc_leds;
667 return (0);
668 #ifdef WSDISPLAY_COMPAT_RAWKBD
669 case WSKBDIO_SETMODE:
670 DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
671 sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
672 untimeout(ukbd_rawrepeat, sc);
673 return (0);
674 #endif
675 }
676 return (-1);
677 }
678
679 /* Console interface. */
680 void
681 ukbd_cngetc(v, type, data)
682 void *v;
683 u_int *type;
684 int *data;
685 {
686 struct ukbd_softc *sc = v;
687 usbd_lock_token s;
688 int c;
689
690 DPRINTFN(-1,("ukbd_cngetc: enter\n"));
691 s = usbd_lock();
692 sc->sc_polling = 1;
693 while(sc->sc_npollchar <= 0)
694 usbd_dopoll(sc->sc_iface);
695 sc->sc_polling = 0;
696 c = sc->sc_pollchars[0];
697 sc->sc_npollchar--;
698 memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
699 sc->sc_npollchar * sizeof(u_int16_t));
700 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
701 *data = c & CODEMASK;
702 usbd_unlock(s);
703 DPRINTFN(-1,("ukbd_cngetc: return 0x%02x\n", c));
704 }
705
706 void
707 ukbd_cnpollc(v, on)
708 void *v;
709 int on;
710 {
711 struct ukbd_softc *sc = v;
712
713 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
714
715 usbd_set_polling(sc->sc_iface, on);
716 }
717
718 int
719 ukbd_cnattach()
720 {
721
722 /*
723 * XXX USB requires too many parts of the kernel to be running
724 * XXX in order to work, so we can't do much for the console
725 * XXX keyboard until autconfiguration has run its course.
726 */
727 ukbd_is_console = 1;
728 return (0);
729 }
730
731 #endif /* NetBSD */
732
733 #if defined(__FreeBSD__)
734 DRIVER_MODULE(ukbd, usb, ukbd_driver, ukbd_devclass, usbd_driver_load, 0);
735 #endif
736