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