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