ukbd.c revision 1.86 1 /* $NetBSD: ukbd.c,v 1.86 2005/04/29 17:02: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/devclass_docs/HID1_11.pdf
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.86 2005/04/29 17:02:06 augustss Exp $");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/callout.h>
50 #include <sys/kernel.h>
51 #include <sys/device.h>
52 #include <sys/ioctl.h>
53 #include <sys/tty.h>
54 #include <sys/file.h>
55 #include <sys/select.h>
56 #include <sys/proc.h>
57 #include <sys/vnode.h>
58 #include <sys/poll.h>
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbhid.h>
62
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdi_util.h>
65 #include <dev/usb/usbdevs.h>
66 #include <dev/usb/usb_quirks.h>
67 #include <dev/usb/uhidev.h>
68 #include <dev/usb/hid.h>
69 #include <dev/usb/ukbdvar.h>
70
71 #include <dev/wscons/wsconsio.h>
72 #include <dev/wscons/wskbdvar.h>
73 #include <dev/wscons/wsksymdef.h>
74 #include <dev/wscons/wsksymvar.h>
75
76 #include "opt_wsdisplay_compat.h"
77 #include "opt_ddb.h"
78
79 #ifdef UKBD_DEBUG
80 #define DPRINTF(x) if (ukbddebug) logprintf x
81 #define DPRINTFN(n,x) if (ukbddebug>(n)) logprintf x
82 int ukbddebug = 0;
83 #else
84 #define DPRINTF(x)
85 #define DPRINTFN(n,x)
86 #endif
87
88 #define MAXKEYCODE 6
89 #define MAXMOD 8 /* max 32 */
90
91 struct ukbd_data {
92 u_int32_t modifiers;
93 u_int8_t keycode[MAXKEYCODE];
94 };
95
96 #define PRESS 0x000
97 #define RELEASE 0x100
98 #define CODEMASK 0x0ff
99
100 #if defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD)
101 #define NN 0 /* no translation */
102 /*
103 * Translate USB keycodes to US keyboard XT scancodes.
104 * Scancodes >= 0x80 represent EXTENDED keycodes.
105 *
106 * See http://www.microsoft.com/HWDEV/TECH/input/Scancode.asp
107 */
108 Static const u_int8_t ukbd_trtab[256] = {
109 NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
110 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
111 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
112 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
113 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
114 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
115 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
116 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
117 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */
118 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
119 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
120 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
121 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, NN, 0x59, /* 60 - 67 */
122 0x5d, 0x5e, 0x5f, NN, NN, NN, NN, NN, /* 68 - 6f */
123 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
124 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7f */
125 NN, NN, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */
126 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */
127 NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */
128 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */
129 NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */
130 NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */
131 NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */
132 NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */
133 NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */
134 NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */
135 NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */
136 NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */
137 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
138 NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */
139 NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */
140 NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */
141 };
142 #endif /* defined(__NetBSD__) && defined(WSDISPLAY_COMPAT_RAWKBD) */
143
144 #define KEY_ERROR 0x01
145
146 #define MAXKEYS (MAXMOD+2*MAXKEYCODE)
147
148 struct ukbd_softc {
149 struct uhidev sc_hdev;
150
151 struct ukbd_data sc_ndata;
152 struct ukbd_data sc_odata;
153 struct hid_location sc_modloc[MAXMOD];
154 u_int sc_nmod;
155 struct {
156 u_int32_t mask;
157 u_int8_t key;
158 } sc_mods[MAXMOD];
159
160 struct hid_location sc_keycodeloc;
161 u_int sc_nkeycode;
162
163 char sc_enabled;
164
165 int sc_console_keyboard; /* we are the console keyboard */
166
167 char sc_debounce; /* for quirk handling */
168 usb_callout_t sc_delay; /* for quirk handling */
169 struct ukbd_data sc_data; /* for quirk handling */
170
171 struct hid_location sc_numloc;
172 struct hid_location sc_capsloc;
173 struct hid_location sc_scroloc;
174 int sc_leds;
175 #if defined(__NetBSD__)
176 usb_callout_t sc_rawrepeat_ch;
177
178 struct device *sc_wskbddev;
179 #if defined(WSDISPLAY_COMPAT_RAWKBD)
180 #define REP_DELAY1 400
181 #define REP_DELAYN 100
182 int sc_rawkbd;
183 int sc_nrep;
184 char sc_rep[MAXKEYS];
185 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
186
187 int sc_spl;
188 int sc_polling;
189 int sc_npollchar;
190 u_int16_t sc_pollchars[MAXKEYS];
191 #endif /* defined(__NetBSD__) */
192
193 u_char sc_dying;
194 };
195
196 #ifdef UKBD_DEBUG
197 #define UKBDTRACESIZE 64
198 struct ukbdtraceinfo {
199 int unit;
200 struct timeval tv;
201 struct ukbd_data ud;
202 };
203 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
204 int ukbdtraceindex = 0;
205 int ukbdtrace = 0;
206 void ukbdtracedump(void);
207 void
208 ukbdtracedump(void)
209 {
210 int i;
211 for (i = 0; i < UKBDTRACESIZE; i++) {
212 struct ukbdtraceinfo *p =
213 &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
214 printf("%lu.%06lu: mod=0x%02x key0=0x%02x key1=0x%02x "
215 "key2=0x%02x key3=0x%02x\n",
216 p->tv.tv_sec, p->tv.tv_usec,
217 p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1],
218 p->ud.keycode[2], p->ud.keycode[3]);
219 }
220 }
221 #endif
222
223 #define UKBDUNIT(dev) (minor(dev))
224 #define UKBD_CHUNK 128 /* chunk size for read */
225 #define UKBD_BSIZE 1020 /* buffer size */
226
227 Static int ukbd_is_console;
228
229 Static void ukbd_cngetc(void *, u_int *, int *);
230 Static void ukbd_cnpollc(void *, int);
231
232 #if defined(__NetBSD__)
233 const struct wskbd_consops ukbd_consops = {
234 ukbd_cngetc,
235 ukbd_cnpollc,
236 };
237 #endif
238
239 Static const char *ukbd_parse_desc(struct ukbd_softc *sc);
240
241 Static void ukbd_intr(struct uhidev *addr, void *ibuf, u_int len);
242 Static void ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud);
243 Static void ukbd_delayed_decode(void *addr);
244
245 Static int ukbd_enable(void *, int);
246 Static void ukbd_set_leds(void *, int);
247
248 #if defined(__NetBSD__)
249 Static int ukbd_ioctl(void *, u_long, caddr_t, int, usb_proc_ptr );
250 #ifdef WSDISPLAY_COMPAT_RAWKBD
251 Static void ukbd_rawrepeat(void *v);
252 #endif
253
254 const struct wskbd_accessops ukbd_accessops = {
255 ukbd_enable,
256 ukbd_set_leds,
257 ukbd_ioctl,
258 };
259
260 extern const struct wscons_keydesc ukbd_keydesctab[];
261
262 const struct wskbd_mapdata ukbd_keymapdata = {
263 ukbd_keydesctab,
264 #if defined(UKBD_LAYOUT)
265 UKBD_LAYOUT,
266 #elif defined(PCKBD_LAYOUT)
267 PCKBD_LAYOUT,
268 #else
269 KB_US,
270 #endif
271 };
272 #endif
273
274 USB_DECLARE_DRIVER(ukbd);
275
276 int
277 ukbd_match(struct device *parent, struct cfdata *match, void *aux)
278 {
279 struct uhidev_attach_arg *uha = aux;
280 int size;
281 void *desc;
282
283 uhidev_get_report_desc(uha->parent, &desc, &size);
284 if (!hid_is_collection(desc, size, uha->reportid,
285 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
286 return (UMATCH_NONE);
287
288 return (UMATCH_IFACECLASS);
289 }
290
291 void
292 ukbd_attach(struct device *parent, struct device *self, void *aux)
293 {
294 struct ukbd_softc *sc = (struct ukbd_softc *)self;
295 struct uhidev_attach_arg *uha = aux;
296 u_int32_t qflags;
297 const char *parseerr;
298 #if defined(__NetBSD__)
299 struct wskbddev_attach_args a;
300 #else
301 int i;
302 #endif
303
304 sc->sc_hdev.sc_intr = ukbd_intr;
305 sc->sc_hdev.sc_parent = uha->parent;
306 sc->sc_hdev.sc_report_id = uha->reportid;
307
308 parseerr = ukbd_parse_desc(sc);
309 if (parseerr != NULL) {
310 printf("\n%s: attach failed, %s\n",
311 sc->sc_hdev.sc_dev.dv_xname, parseerr);
312 USB_ATTACH_ERROR_RETURN;
313 }
314
315 #ifdef DIAGNOSTIC
316 printf(": %d modifier keys, %d key codes", sc->sc_nmod,
317 sc->sc_nkeycode);
318 #endif
319 printf("\n");
320
321
322 qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
323 sc->sc_debounce = (qflags & UQ_SPUR_BUT_UP) != 0;
324
325 /*
326 * Remember if we're the console keyboard.
327 *
328 * XXX This always picks the first keyboard on the
329 * first USB bus, but what else can we really do?
330 */
331 if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
332 /* Don't let any other keyboard have it. */
333 ukbd_is_console = 0;
334 }
335
336 if (sc->sc_console_keyboard) {
337 DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
338 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
339 ukbd_enable(sc, 1);
340 }
341
342 a.console = sc->sc_console_keyboard;
343
344 a.keymap = &ukbd_keymapdata;
345
346 a.accessops = &ukbd_accessops;
347 a.accesscookie = sc;
348
349 usb_callout_init(sc->sc_rawrepeat_ch);
350
351 usb_callout_init(sc->sc_delay);
352
353 /* Flash the leds; no real purpose, just shows we're alive. */
354 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
355 usbd_delay_ms(uha->parent->sc_udev, 400);
356 ukbd_set_leds(sc, 0);
357
358 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
359
360 USB_ATTACH_SUCCESS_RETURN;
361 }
362
363 int
364 ukbd_enable(void *v, int on)
365 {
366 struct ukbd_softc *sc = v;
367
368 if (on && sc->sc_dying)
369 return (EIO);
370
371 /* Should only be called to change state */
372 if (sc->sc_enabled == on) {
373 #ifdef DIAGNOSTIC
374 printf("ukbd_enable: %s: bad call on=%d\n",
375 USBDEVNAME(sc->sc_hdev.sc_dev), on);
376 #endif
377 return (EBUSY);
378 }
379
380 DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
381 sc->sc_enabled = on;
382 if (on) {
383 return (uhidev_open(&sc->sc_hdev));
384 } else {
385 uhidev_close(&sc->sc_hdev);
386 return (0);
387 }
388 }
389
390 int
391 ukbd_activate(device_ptr_t self, enum devact act)
392 {
393 struct ukbd_softc *sc = (struct ukbd_softc *)self;
394 int rv = 0;
395
396 switch (act) {
397 case DVACT_ACTIVATE:
398 return (EOPNOTSUPP);
399
400 case DVACT_DEACTIVATE:
401 if (sc->sc_wskbddev != NULL)
402 rv = config_deactivate(sc->sc_wskbddev);
403 sc->sc_dying = 1;
404 break;
405 }
406 return (rv);
407 }
408
409 int
410 ukbd_detach(struct device *self, int flags)
411 {
412 struct ukbd_softc *sc = (struct ukbd_softc *)self;
413 int rv = 0;
414
415 DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
416
417 if (sc->sc_console_keyboard) {
418 #if 0
419 /*
420 * XXX Should probably disconnect our consops,
421 * XXX and either notify some other keyboard that
422 * XXX it can now be the console, or if there aren't
423 * XXX any more USB keyboards, set ukbd_is_console
424 * XXX back to 1 so that the next USB keyboard attached
425 * XXX to the system will get it.
426 */
427 panic("ukbd_detach: console keyboard");
428 #else
429 /*
430 * Disconnect our consops and set ukbd_is_console
431 * back to 1 so that the next USB keyboard attached
432 * to the system will get it.
433 * XXX Should notify some other keyboard that it can be
434 * XXX console, if there are any other keyboards.
435 */
436 printf("%s: was console keyboard\n",
437 USBDEVNAME(sc->sc_hdev.sc_dev));
438 wskbd_cndetach();
439 ukbd_is_console = 1;
440 #endif
441 }
442 /* No need to do reference counting of ukbd, wskbd has all the goo. */
443 if (sc->sc_wskbddev != NULL)
444 rv = config_detach(sc->sc_wskbddev, flags);
445
446 /* The console keyboard does not get a disable call, so check pipe. */
447 if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
448 uhidev_close(&sc->sc_hdev);
449
450 return (rv);
451 }
452
453 void
454 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
455 {
456 struct ukbd_softc *sc = (struct ukbd_softc *)addr;
457 struct ukbd_data *ud = &sc->sc_ndata;
458 int i;
459
460 #ifdef UKBD_DEBUG
461 if (ukbddebug > 5) {
462 printf("ukbd_intr: data");
463 for (i = 0; i < len; i++)
464 printf(" 0x%02x", ((u_char *)ibuf)[i]);
465 printf("\n");
466 }
467 #endif
468
469 ud->modifiers = 0;
470 for (i = 0; i < sc->sc_nmod; i++)
471 if (hid_get_data(ibuf, &sc->sc_modloc[i]))
472 ud->modifiers |= sc->sc_mods[i].mask;
473 memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8,
474 sc->sc_nkeycode);
475
476 if (sc->sc_debounce && !sc->sc_polling) {
477 /*
478 * Some keyboards have a peculiar quirk. They sometimes
479 * generate a key up followed by a key down for the same
480 * key after about 10 ms.
481 * We avoid this bug by holding off decoding for 20 ms.
482 */
483 sc->sc_data = *ud;
484 usb_callout(sc->sc_delay, hz / 50, ukbd_delayed_decode, sc);
485 #ifdef DDB
486 } else if (sc->sc_console_keyboard && !sc->sc_polling) {
487 /*
488 * For the console keyboard we can't deliver CTL-ALT-ESC
489 * from the interrupt routine. Doing so would start
490 * polling from inside the interrupt routine and that
491 * loses bigtime.
492 */
493 sc->sc_data = *ud;
494 usb_callout(sc->sc_delay, 1, ukbd_delayed_decode, sc);
495 #endif
496 } else {
497 ukbd_decode(sc, ud);
498 }
499 }
500
501 void
502 ukbd_delayed_decode(void *addr)
503 {
504 struct ukbd_softc *sc = addr;
505
506 ukbd_decode(sc, &sc->sc_data);
507 }
508
509 void
510 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
511 {
512 int mod, omod;
513 u_int16_t ibuf[MAXKEYS]; /* chars events */
514 int s;
515 int nkeys, i, j;
516 int key;
517 #define ADDKEY(c) ibuf[nkeys++] = (c)
518
519 #ifdef UKBD_DEBUG
520 /*
521 * Keep a trace of the last events. Using printf changes the
522 * timing, so this can be useful sometimes.
523 */
524 if (ukbdtrace) {
525 struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
526 p->unit = sc->sc_hdev.sc_dev.dv_unit;
527 microtime(&p->tv);
528 p->ud = *ud;
529 if (++ukbdtraceindex >= UKBDTRACESIZE)
530 ukbdtraceindex = 0;
531 }
532 if (ukbddebug > 5) {
533 struct timeval tv;
534 microtime(&tv);
535 DPRINTF((" at %lu.%06lu mod=0x%02x key0=0x%02x key1=0x%02x "
536 "key2=0x%02x key3=0x%02x\n",
537 tv.tv_sec, tv.tv_usec,
538 ud->modifiers, ud->keycode[0], ud->keycode[1],
539 ud->keycode[2], ud->keycode[3]));
540 }
541 #endif
542
543 if (ud->keycode[0] == KEY_ERROR) {
544 DPRINTF(("ukbd_intr: KEY_ERROR\n"));
545 return; /* ignore */
546 }
547 nkeys = 0;
548 mod = ud->modifiers;
549 omod = sc->sc_odata.modifiers;
550 if (mod != omod)
551 for (i = 0; i < sc->sc_nmod; i++)
552 if (( mod & sc->sc_mods[i].mask) !=
553 (omod & sc->sc_mods[i].mask))
554 ADDKEY(sc->sc_mods[i].key |
555 (mod & sc->sc_mods[i].mask
556 ? PRESS : RELEASE));
557 if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
558 /* Check for released keys. */
559 for (i = 0; i < sc->sc_nkeycode; i++) {
560 key = sc->sc_odata.keycode[i];
561 if (key == 0)
562 continue;
563 for (j = 0; j < sc->sc_nkeycode; j++)
564 if (key == ud->keycode[j])
565 goto rfound;
566 DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
567 ADDKEY(key | RELEASE);
568 rfound:
569 ;
570 }
571
572 /* Check for pressed keys. */
573 for (i = 0; i < sc->sc_nkeycode; i++) {
574 key = ud->keycode[i];
575 if (key == 0)
576 continue;
577 for (j = 0; j < sc->sc_nkeycode; j++)
578 if (key == sc->sc_odata.keycode[j])
579 goto pfound;
580 DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
581 ADDKEY(key | PRESS);
582 pfound:
583 ;
584 }
585 }
586 sc->sc_odata = *ud;
587
588 if (nkeys == 0)
589 return;
590
591 if (sc->sc_polling) {
592 DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
593 memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
594 sc->sc_npollchar = nkeys;
595 return;
596 }
597 #ifdef WSDISPLAY_COMPAT_RAWKBD
598 if (sc->sc_rawkbd) {
599 u_char cbuf[MAXKEYS * 2];
600 int c;
601 int npress;
602
603 for (npress = i = j = 0; i < nkeys; i++) {
604 key = ibuf[i];
605 c = ukbd_trtab[key & CODEMASK];
606 if (c == NN)
607 continue;
608 if (c & 0x80)
609 cbuf[j++] = 0xe0;
610 cbuf[j] = c & 0x7f;
611 if (key & RELEASE)
612 cbuf[j] |= 0x80;
613 else {
614 /* remember pressed keys for autorepeat */
615 if (c & 0x80)
616 sc->sc_rep[npress++] = 0xe0;
617 sc->sc_rep[npress++] = c & 0x7f;
618 }
619 DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
620 c & 0x80 ? "0xe0 " : "",
621 cbuf[j]));
622 j++;
623 }
624 s = spltty();
625 wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
626 splx(s);
627 usb_uncallout(sc->sc_rawrepeat_ch, ukbd_rawrepeat, sc);
628 if (npress != 0) {
629 sc->sc_nrep = npress;
630 usb_callout(sc->sc_rawrepeat_ch,
631 hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
632 }
633 return;
634 }
635 #endif
636
637 s = spltty();
638 for (i = 0; i < nkeys; i++) {
639 key = ibuf[i];
640 wskbd_input(sc->sc_wskbddev,
641 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
642 key&CODEMASK);
643 }
644 splx(s);
645 }
646
647 void
648 ukbd_set_leds(void *v, int leds)
649 {
650 struct ukbd_softc *sc = v;
651 u_int8_t res;
652
653 DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n",
654 sc, leds, sc->sc_leds));
655
656 if (sc->sc_dying)
657 return;
658
659 if (sc->sc_leds == leds)
660 return;
661 sc->sc_leds = leds;
662 res = 0;
663 /* XXX not really right */
664 if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
665 res |= 1 << sc->sc_scroloc.pos;
666 if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
667 res |= 1 << sc->sc_numloc.pos;
668 if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
669 res |= 1 << sc->sc_capsloc.pos;
670 uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1);
671 }
672
673 #ifdef WSDISPLAY_COMPAT_RAWKBD
674 void
675 ukbd_rawrepeat(void *v)
676 {
677 struct ukbd_softc *sc = v;
678 int s;
679
680 s = spltty();
681 wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
682 splx(s);
683 usb_callout(sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
684 ukbd_rawrepeat, sc);
685 }
686 #endif
687
688 int
689 ukbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
690 {
691 struct ukbd_softc *sc = v;
692
693 switch (cmd) {
694 case WSKBDIO_GTYPE:
695 *(int *)data = WSKBD_TYPE_USB;
696 return (0);
697 case WSKBDIO_SETLEDS:
698 ukbd_set_leds(v, *(int *)data);
699 return (0);
700 case WSKBDIO_GETLEDS:
701 *(int *)data = sc->sc_leds;
702 return (0);
703 #ifdef WSDISPLAY_COMPAT_RAWKBD
704 case WSKBDIO_SETMODE:
705 DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
706 sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
707 usb_uncallout(sc->sc_rawrepeat_ch, ukbd_rawrepeat, sc);
708 return (0);
709 #endif
710 }
711 return (EPASSTHROUGH);
712 }
713
714 /*
715 * This is a hack to work around some broken ports that don't call
716 * cnpollc() before cngetc().
717 */
718 static int pollenter, warned;
719
720 /* Console interface. */
721 void
722 ukbd_cngetc(void *v, u_int *type, int *data)
723 {
724 struct ukbd_softc *sc = v;
725 int c;
726 int broken;
727
728 if (pollenter == 0) {
729 if (!warned) {
730 printf("\n"
731 "This port is broken, it does not call cnpollc() before calling cngetc().\n"
732 "This should be fixed, but it will work anyway (for now).\n");
733 warned = 1;
734 }
735 broken = 1;
736 ukbd_cnpollc(v, 1);
737 } else
738 broken = 0;
739
740 DPRINTFN(0,("ukbd_cngetc: enter\n"));
741 sc->sc_polling = 1;
742 while(sc->sc_npollchar <= 0)
743 usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface);
744 sc->sc_polling = 0;
745 c = sc->sc_pollchars[0];
746 sc->sc_npollchar--;
747 memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
748 sc->sc_npollchar * sizeof(u_int16_t));
749 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
750 *data = c & CODEMASK;
751 DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
752 if (broken)
753 ukbd_cnpollc(v, 0);
754 }
755
756 void
757 ukbd_cnpollc(void *v, int on)
758 {
759 struct ukbd_softc *sc = v;
760 usbd_device_handle dev;
761
762 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
763
764 usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev);
765 if (on) {
766 sc->sc_spl = splusb();
767 pollenter++;
768 } else {
769 splx(sc->sc_spl);
770 pollenter--;
771 }
772 usbd_set_polling(dev, on);
773 }
774
775 int
776 ukbd_cnattach(void)
777 {
778
779 /*
780 * XXX USB requires too many parts of the kernel to be running
781 * XXX in order to work, so we can't do much for the console
782 * XXX keyboard until autconfiguration has run its course.
783 */
784 ukbd_is_console = 1;
785 return (0);
786 }
787
788 const char *
789 ukbd_parse_desc(struct ukbd_softc *sc)
790 {
791 struct hid_data *d;
792 struct hid_item h;
793 int size;
794 void *desc;
795 int imod;
796
797 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
798 imod = 0;
799 sc->sc_nkeycode = 0;
800 d = hid_start_parse(desc, size, hid_input);
801 while (hid_get_item(d, &h)) {
802 /*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n",
803 h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/
804 if (h.kind != hid_input || (h.flags & HIO_CONST) ||
805 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
806 h.report_ID != sc->sc_hdev.sc_report_id)
807 continue;
808 DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d "
809 "cnt=%d\n", imod,
810 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
811 if (h.flags & HIO_VARIABLE) {
812 if (h.loc.size != 1)
813 return ("bad modifier size");
814 /* Single item */
815 if (imod < MAXMOD) {
816 sc->sc_modloc[imod] = h.loc;
817 sc->sc_mods[imod].mask = 1 << imod;
818 sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
819 imod++;
820 } else
821 return ("too many modifier keys");
822 } else {
823 /* Array */
824 if (h.loc.size != 8)
825 return ("key code size != 8");
826 if (h.loc.count > MAXKEYCODE)
827 return ("too many key codes");
828 if (h.loc.pos % 8 != 0)
829 return ("key codes not on byte boundary");
830 if (sc->sc_nkeycode != 0)
831 return ("multiple key code arrays\n");
832 sc->sc_keycodeloc = h.loc;
833 sc->sc_nkeycode = h.loc.count;
834 }
835 }
836 sc->sc_nmod = imod;
837 hid_end_parse(d);
838
839 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
840 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL);
841 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
842 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL);
843 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
844 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL);
845
846 return (NULL);
847 }
848