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