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