ukbd.c revision 1.68 1 /* $NetBSD: ukbd.c,v 1.68 2001/10/24 15:31:06 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 (at) 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/devclass/hid1_1.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 UKBD_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 const 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 const 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, 43, 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, 86, 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 char sc_debounce; /* for quirk handling */
183 struct callout sc_delay; /* for quirk handling */
184 struct ukbd_data sc_data; /* for quirk handling */
185
186 int sc_leds;
187 #if defined(__NetBSD__)
188 struct callout sc_rawrepeat_ch;
189
190 struct device *sc_wskbddev;
191 #if defined(WSDISPLAY_COMPAT_RAWKBD)
192 #define REP_DELAY1 400
193 #define REP_DELAYN 100
194 int sc_rawkbd;
195 int sc_nrep;
196 char sc_rep[MAXKEYS];
197 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
198
199 int sc_polling;
200 int sc_npollchar;
201 u_int16_t sc_pollchars[MAXKEYS];
202 #endif /* defined(__NetBSD__) */
203
204 u_char sc_dying;
205 };
206
207 #ifdef UKBD_DEBUG
208 #define UKBDTRACESIZE 64
209 struct ukbdtraceinfo {
210 int unit;
211 struct timeval tv;
212 struct ukbd_data ud;
213 };
214 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
215 int ukbdtraceindex = 0;
216 int ukbdtrace = 0;
217 void ukbdtracedump(void);
218 void
219 ukbdtracedump(void)
220 {
221 int i;
222 for (i = 0; i < UKBDTRACESIZE; i++) {
223 struct ukbdtraceinfo *p =
224 &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
225 printf("%lu.%06lu: mod=0x%02x key0=0x%02x key1=0x%02x "
226 "key2=0x%02x key3=0x%02x\n",
227 p->tv.tv_sec, p->tv.tv_usec,
228 p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1],
229 p->ud.keycode[2], p->ud.keycode[3]);
230 }
231 }
232 #endif
233
234 #define UKBDUNIT(dev) (minor(dev))
235 #define UKBD_CHUNK 128 /* chunk size for read */
236 #define UKBD_BSIZE 1020 /* buffer size */
237
238 Static int ukbd_is_console;
239
240 Static void ukbd_cngetc(void *, u_int *, int *);
241 Static void ukbd_cnpollc(void *, int);
242
243 #if defined(__NetBSD__)
244 const struct wskbd_consops ukbd_consops = {
245 ukbd_cngetc,
246 ukbd_cnpollc,
247 };
248 #endif
249
250 Static void ukbd_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
251 Static void ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud);
252 Static void ukbd_delayed_decode(void *addr);
253
254 Static int ukbd_enable(void *, int);
255 Static void ukbd_set_leds(void *, int);
256
257 #if defined(__NetBSD__)
258 Static int ukbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
259 #ifdef WSDISPLAY_COMPAT_RAWKBD
260 Static void ukbd_rawrepeat(void *v);
261 #endif
262
263 const struct wskbd_accessops ukbd_accessops = {
264 ukbd_enable,
265 ukbd_set_leds,
266 ukbd_ioctl,
267 };
268
269 extern const struct wscons_keydesc ukbd_keydesctab[];
270
271 const struct wskbd_mapdata ukbd_keymapdata = {
272 ukbd_keydesctab,
273 #ifdef UKBD_LAYOUT
274 UKBD_LAYOUT,
275 #else
276 KB_US,
277 #endif
278 };
279 #endif
280
281 USB_DECLARE_DRIVER(ukbd);
282
283 USB_MATCH(ukbd)
284 {
285 USB_MATCH_START(ukbd, uaa);
286 usb_interface_descriptor_t *id;
287
288 /* Check that this is a keyboard that speaks the boot protocol. */
289 if (uaa->iface == NULL)
290 return (UMATCH_NONE);
291 id = usbd_get_interface_descriptor(uaa->iface);
292 if (id == NULL ||
293 id->bInterfaceClass != UICLASS_HID ||
294 id->bInterfaceSubClass != UISUBCLASS_BOOT ||
295 id->bInterfaceProtocol != UIPROTO_BOOT_KEYBOARD)
296 return (UMATCH_NONE);
297 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
298 }
299
300 USB_ATTACH(ukbd)
301 {
302 USB_ATTACH_START(ukbd, sc, uaa);
303 usbd_interface_handle iface = uaa->iface;
304 usb_interface_descriptor_t *id;
305 usb_endpoint_descriptor_t *ed;
306 usbd_status err;
307 u_int32_t qflags;
308 char devinfo[1024];
309 #if defined(__NetBSD__)
310 struct wskbddev_attach_args a;
311 #else
312 int i;
313 #endif
314
315 sc->sc_udev = uaa->device;
316 sc->sc_iface = iface;
317 id = usbd_get_interface_descriptor(iface);
318 usbd_devinfo(uaa->device, 0, devinfo);
319 USB_ATTACH_SETUP;
320 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
321 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
322
323 ed = usbd_interface2endpoint_descriptor(iface, 0);
324 if (ed == NULL) {
325 printf("%s: could not read endpoint descriptor\n",
326 USBDEVNAME(sc->sc_dev));
327 USB_ATTACH_ERROR_RETURN;
328 }
329
330 DPRINTFN(10,("ukbd_attach: bLength=%d bDescriptorType=%d "
331 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
332 " bInterval=%d\n",
333 ed->bLength, ed->bDescriptorType,
334 ed->bEndpointAddress & UE_ADDR,
335 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
336 ed->bmAttributes & UE_XFERTYPE,
337 UGETW(ed->wMaxPacketSize), ed->bInterval));
338
339 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
340 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
341 printf("%s: unexpected endpoint\n",
342 USBDEVNAME(sc->sc_dev));
343 USB_ATTACH_ERROR_RETURN;
344 }
345
346 qflags = usbd_get_quirks(uaa->device)->uq_flags;
347 if ((qflags & UQ_NO_SET_PROTO) == 0) {
348 err = usbd_set_protocol(iface, 0);
349 DPRINTFN(5, ("ukbd_attach: protocol set\n"));
350 if (err) {
351 printf("%s: set protocol failed\n",
352 USBDEVNAME(sc->sc_dev));
353 USB_ATTACH_ERROR_RETURN;
354 }
355 }
356 sc->sc_debounce = (qflags & UQ_SPUR_BUT_UP) != 0;
357
358 /* Ignore if SETIDLE fails since it is not crucial. */
359 (void)usbd_set_idle(iface, 0, 0);
360
361 sc->sc_ep_addr = ed->bEndpointAddress;
362
363 /*
364 * Remember if we're the console keyboard.
365 *
366 * XXX This always picks the first keyboard on the
367 * first USB bus, but what else can we really do?
368 */
369 if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
370 /* Don't let any other keyboard have it. */
371 ukbd_is_console = 0;
372 }
373
374 if (sc->sc_console_keyboard) {
375 DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
376 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
377 ukbd_enable(sc, 1);
378 }
379
380 a.console = sc->sc_console_keyboard;
381
382 a.keymap = &ukbd_keymapdata;
383
384 a.accessops = &ukbd_accessops;
385 a.accesscookie = sc;
386
387 callout_init(&sc->sc_rawrepeat_ch);
388
389 callout_init(&sc->sc_delay);
390
391 /* Flash the leds; no real purpose, just shows we're alive. */
392 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
393 usbd_delay_ms(uaa->device, 400);
394 ukbd_set_leds(sc, 0);
395
396 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
397 USBDEV(sc->sc_dev));
398
399 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
400
401 USB_ATTACH_SUCCESS_RETURN;
402 }
403
404 int
405 ukbd_enable(void *v, int on)
406 {
407 struct ukbd_softc *sc = v;
408 usbd_status err;
409
410 if (on && sc->sc_dying)
411 return (EIO);
412
413 /* Should only be called to change state */
414 if (sc->sc_enabled == on) {
415 #ifdef DIAGNOSTIC
416 printf("ukbd_enable: %s: bad call on=%d\n",
417 USBDEVNAME(sc->sc_dev), on);
418 #endif
419 return (EBUSY);
420 }
421
422 DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
423 if (on) {
424 /* Set up interrupt pipe. */
425 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
426 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
427 &sc->sc_ndata, sizeof(sc->sc_ndata), ukbd_intr,
428 USBD_DEFAULT_INTERVAL);
429 if (err)
430 return (EIO);
431 } else {
432 /* Disable interrupts. */
433 usbd_abort_pipe(sc->sc_intrpipe);
434 usbd_close_pipe(sc->sc_intrpipe);
435 sc->sc_intrpipe = NULL;
436 }
437 sc->sc_enabled = on;
438
439 return (0);
440 }
441
442 int
443 ukbd_activate(device_ptr_t self, enum devact act)
444 {
445 struct ukbd_softc *sc = (struct ukbd_softc *)self;
446 int rv = 0;
447
448 switch (act) {
449 case DVACT_ACTIVATE:
450 return (EOPNOTSUPP);
451 break;
452
453 case DVACT_DEACTIVATE:
454 if (sc->sc_wskbddev != NULL)
455 rv = config_deactivate(sc->sc_wskbddev);
456 sc->sc_dying = 1;
457 break;
458 }
459 return (rv);
460 }
461
462 USB_DETACH(ukbd)
463 {
464 USB_DETACH_START(ukbd, sc);
465 int rv = 0;
466
467 DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
468
469 if (sc->sc_console_keyboard) {
470 #if 0
471 /*
472 * XXX Should probably disconnect our consops,
473 * XXX and either notify some other keyboard that
474 * XXX it can now be the console, or if there aren't
475 * XXX any more USB keyboards, set ukbd_is_console
476 * XXX back to 1 so that the next USB keyboard attached
477 * XXX to the system will get it.
478 */
479 panic("ukbd_detach: console keyboard");
480 #else
481 /*
482 * Disconnect our consops and set ukbd_is_console
483 * back to 1 so that the next USB keyboard attached
484 * to the system will get it.
485 * XXX Should notify some other keyboard that it can be
486 * XXX console, if there are any other keyboards.
487 */
488 printf("%s: was console keyboard\n", USBDEVNAME(sc->sc_dev));
489 wskbd_cndetach();
490 ukbd_is_console = 1;
491 #endif
492 }
493 /* No need to do reference counting of ukbd, wskbd has all the goo. */
494 if (sc->sc_wskbddev != NULL)
495 rv = config_detach(sc->sc_wskbddev, flags);
496
497 /* The console keyboard does not get a disable call, so check pipe. */
498 if (sc->sc_intrpipe != NULL) {
499 usbd_abort_pipe(sc->sc_intrpipe);
500 usbd_close_pipe(sc->sc_intrpipe);
501 sc->sc_intrpipe = NULL;
502 }
503
504 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
505 USBDEV(sc->sc_dev));
506
507 return (rv);
508 }
509
510 void
511 ukbd_intr(xfer, addr, status)
512 usbd_xfer_handle xfer;
513 usbd_private_handle addr;
514 usbd_status status;
515 {
516 struct ukbd_softc *sc = addr;
517 struct ukbd_data *ud = &sc->sc_ndata;
518
519 DPRINTFN(5, ("ukbd_intr: status=%d\n", status));
520 if (status == USBD_CANCELLED)
521 return;
522
523 if (status) {
524 DPRINTF(("ukbd_intr: status=%d\n", status));
525 if (status == USBD_STALLED)
526 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
527 return;
528 }
529
530 if (sc->sc_debounce) {
531 /*
532 * Some keyboards have a peculiar quirk. They sometimes
533 * generate a key up followed by a key down for the same
534 * key after about 10 ms.
535 * We avoid this bug by holding off decoding for 20 ms.
536 */
537 sc->sc_data = *ud;
538 callout_reset(&sc->sc_delay, hz / 50, ukbd_delayed_decode, sc);
539 } else {
540 ukbd_decode(sc, ud);
541 }
542 }
543
544 void
545 ukbd_delayed_decode(void *addr)
546 {
547 struct ukbd_softc *sc = addr;
548
549 ukbd_decode(sc, &sc->sc_data);
550 }
551
552 void
553 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
554 {
555 int mod, omod;
556 u_int16_t ibuf[MAXKEYS]; /* chars events */
557 int s;
558 int nkeys, i, j;
559 int key;
560 #define ADDKEY(c) ibuf[nkeys++] = (c)
561
562 #ifdef UKBD_DEBUG
563 /*
564 * Keep a trace of the last events. Using printf changes the
565 * timing, so this can be useful sometimes.
566 */
567 if (ukbdtrace) {
568 struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
569 p->unit = sc->sc_dev.dv_unit;
570 microtime(&p->tv);
571 p->ud = *ud;
572 if (++ukbdtraceindex >= UKBDTRACESIZE)
573 ukbdtraceindex = 0;
574 }
575 if (ukbddebug > 5) {
576 struct timeval tv;
577 microtime(&tv);
578 DPRINTF((" at %lu.%06lu mod=0x%02x key0=0x%02x key1=0x%02x "
579 "key2=0x%02x key3=0x%02x\n",
580 tv.tv_sec, tv.tv_usec,
581 ud->modifiers, ud->keycode[0], ud->keycode[1],
582 ud->keycode[2], ud->keycode[3]));
583 }
584 #endif
585
586 if (ud->keycode[0] == KEY_ERROR) {
587 DPRINTF(("ukbd_intr: KEY_ERROR\n"));
588 return; /* ignore */
589 }
590 nkeys = 0;
591 mod = ud->modifiers;
592 omod = sc->sc_odata.modifiers;
593 if (mod != omod)
594 for (i = 0; i < NMOD; i++)
595 if (( mod & ukbd_mods[i].mask) !=
596 (omod & ukbd_mods[i].mask))
597 ADDKEY(ukbd_mods[i].key |
598 (mod & ukbd_mods[i].mask
599 ? PRESS : RELEASE));
600 if (memcmp(ud->keycode, sc->sc_odata.keycode, NKEYCODE) != 0) {
601 /* Check for released keys. */
602 for (i = 0; i < NKEYCODE; i++) {
603 key = sc->sc_odata.keycode[i];
604 if (key == 0)
605 continue;
606 for (j = 0; j < NKEYCODE; j++)
607 if (key == ud->keycode[j])
608 goto rfound;
609 DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
610 ADDKEY(key | RELEASE);
611 rfound:
612 ;
613 }
614
615 /* Check for pressed keys. */
616 for (i = 0; i < NKEYCODE; i++) {
617 key = ud->keycode[i];
618 if (key == 0)
619 continue;
620 for (j = 0; j < NKEYCODE; j++)
621 if (key == sc->sc_odata.keycode[j])
622 goto pfound;
623 DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
624 ADDKEY(key | PRESS);
625 pfound:
626 ;
627 }
628 }
629 sc->sc_odata = *ud;
630
631 if (nkeys == 0)
632 return;
633
634 if (sc->sc_polling) {
635 DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
636 memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
637 sc->sc_npollchar = nkeys;
638 return;
639 }
640 #ifdef WSDISPLAY_COMPAT_RAWKBD
641 if (sc->sc_rawkbd) {
642 char cbuf[MAXKEYS * 2];
643 int c;
644 int npress;
645
646 for (npress = i = j = 0; i < nkeys; i++) {
647 key = ibuf[i];
648 c = ukbd_trtab[key & CODEMASK];
649 if (c == NN)
650 continue;
651 if (c & 0x80)
652 cbuf[j++] = 0xe0;
653 cbuf[j] = c & 0x7f;
654 if (key & RELEASE)
655 cbuf[j] |= 0x80;
656 else {
657 /* remember pressed keys for autorepeat */
658 if (c & 0x80)
659 sc->sc_rep[npress++] = 0xe0;
660 sc->sc_rep[npress++] = c & 0x7f;
661 }
662 DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
663 c & 0x80 ? "0xe0 " : "",
664 cbuf[j]));
665 j++;
666 }
667 s = spltty();
668 wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
669 splx(s);
670 callout_stop(&sc->sc_rawrepeat_ch);
671 if (npress != 0) {
672 sc->sc_nrep = npress;
673 callout_reset(&sc->sc_rawrepeat_ch,
674 hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
675 }
676 return;
677 }
678 #endif
679
680 s = spltty();
681 for (i = 0; i < nkeys; i++) {
682 key = ibuf[i];
683 wskbd_input(sc->sc_wskbddev,
684 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
685 key&CODEMASK);
686 }
687 splx(s);
688 }
689
690 void
691 ukbd_set_leds(void *v, int leds)
692 {
693 struct ukbd_softc *sc = v;
694 u_int8_t res;
695
696 DPRINTF(("ukbd_set_leds: sc=%p leds=%d\n", sc, leds));
697
698 if (sc->sc_dying)
699 return;
700
701 sc->sc_leds = leds;
702 res = 0;
703 if (leds & WSKBD_LED_SCROLL)
704 res |= SCROLL_LOCK;
705 if (leds & WSKBD_LED_NUM)
706 res |= NUM_LOCK;
707 if (leds & WSKBD_LED_CAPS)
708 res |= CAPS_LOCK;
709 res |= leds & 0xf8;
710 usbd_set_report_async(sc->sc_iface, UHID_OUTPUT_REPORT, 0, &res, 1);
711 }
712
713 #ifdef WSDISPLAY_COMPAT_RAWKBD
714 void
715 ukbd_rawrepeat(void *v)
716 {
717 struct ukbd_softc *sc = v;
718 int s;
719
720 s = spltty();
721 wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
722 splx(s);
723 callout_reset(&sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
724 ukbd_rawrepeat, sc);
725 }
726 #endif
727
728 int
729 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
730 {
731 struct ukbd_softc *sc = v;
732
733 switch (cmd) {
734 case WSKBDIO_GTYPE:
735 *(int *)data = WSKBD_TYPE_USB;
736 return (0);
737 case WSKBDIO_SETLEDS:
738 ukbd_set_leds(v, *(int *)data);
739 return (0);
740 case WSKBDIO_GETLEDS:
741 *(int *)data = sc->sc_leds;
742 return (0);
743 #ifdef WSDISPLAY_COMPAT_RAWKBD
744 case WSKBDIO_SETMODE:
745 DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
746 sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
747 callout_stop(&sc->sc_rawrepeat_ch);
748 return (0);
749 #endif
750 }
751 return (-1);
752 }
753
754 /* Console interface. */
755 void
756 ukbd_cngetc(void *v, u_int *type, int *data)
757 {
758 struct ukbd_softc *sc = v;
759 int s;
760 int c;
761
762 DPRINTFN(0,("ukbd_cngetc: enter\n"));
763 s = splusb();
764 sc->sc_polling = 1;
765 while(sc->sc_npollchar <= 0)
766 usbd_dopoll(sc->sc_iface);
767 sc->sc_polling = 0;
768 c = sc->sc_pollchars[0];
769 sc->sc_npollchar--;
770 memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
771 sc->sc_npollchar * sizeof(u_int16_t));
772 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
773 *data = c & CODEMASK;
774 splx(s);
775 DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
776 }
777
778 void
779 ukbd_cnpollc(void *v, int on)
780 {
781 struct ukbd_softc *sc = v;
782 usbd_device_handle dev;
783
784 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
785
786 (void)usbd_interface2device_handle(sc->sc_iface,&dev);
787 usbd_set_polling(dev, on);
788 }
789
790 int
791 ukbd_cnattach(void)
792 {
793
794 /*
795 * XXX USB requires too many parts of the kernel to be running
796 * XXX in order to work, so we can't do much for the console
797 * XXX keyboard until autconfiguration has run its course.
798 */
799 ukbd_is_console = 1;
800 return (0);
801 }
802