ukbd.c revision 1.119 1 /* $NetBSD: ukbd.c,v 1.119 2012/04/22 14:13:32 khorben 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.119 2012/04/22 14:13:32 khorben Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/callout.h>
43 #include <sys/kernel.h>
44 #include <sys/device.h>
45 #include <sys/ioctl.h>
46 #include <sys/file.h>
47 #include <sys/select.h>
48 #include <sys/proc.h>
49 #include <sys/vnode.h>
50 #include <sys/poll.h>
51
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbhid.h>
54
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbdivar.h>
58 #include <dev/usb/usbdevs.h>
59 #include <dev/usb/usb_quirks.h>
60 #include <dev/usb/uhidev.h>
61 #include <dev/usb/hid.h>
62 #include <dev/usb/ukbdvar.h>
63
64 #include <dev/wscons/wsconsio.h>
65 #include <dev/wscons/wskbdvar.h>
66 #include <dev/wscons/wsksymdef.h>
67 #include <dev/wscons/wsksymvar.h>
68
69 #ifdef _KERNEL_OPT
70 #include "opt_ukbd.h"
71 #include "opt_ukbd_layout.h"
72 #include "opt_wsdisplay_compat.h"
73 #include "opt_ddb.h"
74 #endif /* _KERNEL_OPT */
75
76 #ifdef UKBD_DEBUG
77 #define DPRINTF(x) if (ukbddebug) printf x
78 #define DPRINTFN(n,x) if (ukbddebug>(n)) printf x
79 int ukbddebug = 0;
80 #else
81 #define DPRINTF(x)
82 #define DPRINTFN(n,x)
83 #endif
84
85 #define MAXKEYCODE 6
86 #define MAXMOD 8 /* max 32 */
87
88 struct ukbd_data {
89 u_int32_t modifiers;
90 u_int8_t keycode[MAXKEYCODE];
91 };
92
93 #define PRESS 0x000
94 #define RELEASE 0x100
95 #define CODEMASK 0x0ff
96
97 struct ukbd_keycodetrans {
98 u_int16_t from;
99 u_int16_t to;
100 };
101
102 #define IS_PMF 0x8000
103
104 Static const struct ukbd_keycodetrans trtab_apple_fn[] = {
105 { 0x0c, 0x5d }, /* i -> KP 5 */
106 { 0x0d, 0x59 }, /* j -> KP 1 */
107 { 0x0e, 0x5a }, /* k -> KP 2 */
108 { 0x0f, 0x5b }, /* l -> KP 3 */
109 { 0x10, 0x62 }, /* m -> KP 0 */
110 { 0x12, 0x5e }, /* o -> KP 6 */
111 { 0x13, 0x55 }, /* o -> KP * */
112 { 0x18, 0x5c }, /* u -> KP 4 */
113 { 0x0c, 0x5d }, /* i -> KP 5 */
114 { 0x2a, 0x4c }, /* Backspace -> Delete */
115 { 0x28, 0x49 }, /* Return -> Insert */
116 { 0x24, 0x5f }, /* 7 -> KP 7 */
117 { 0x25, 0x60 }, /* 8 -> KP 8 */
118 { 0x26, 0x61 }, /* 9 -> KP 9 */
119 { 0x27, 0x54 }, /* 0 -> KP / */
120 { 0x2d, 0x67 }, /* - -> KP = */
121 { 0x33, 0x56 }, /* ; -> KP - */
122 { 0x37, 0x63 }, /* . -> KP . */
123 { 0x38, 0x57 }, /* / -> KP + */
124 { 0x3a, 0xd1 }, /* F1..F12 mapped to reserved codes 0xd1..0xdc */
125 { 0x3b, 0xd2 },
126 { 0x3c, 0xd3 },
127 { 0x3d, 0xd4 },
128 { 0x3e, 0xd5 },
129 { 0x3f, 0xd6 },
130 { 0x40, 0xd7 },
131 { 0x41, 0xd8 },
132 { 0x42, 0xd9 },
133 { 0x43, 0xda },
134 { 0x44, 0xdb },
135 { 0x45, 0xdc },
136 { 0x4f, 0x4d }, /* Right -> End */
137 { 0x50, 0x4a }, /* Left -> Home */
138 { 0x51, 0x4e }, /* Down -> PageDown */
139 { 0x52, 0x4b }, /* Up -> PageUp */
140 { 0x00, 0x00 }
141 };
142
143 Static const struct ukbd_keycodetrans trtab_apple_iso[] = {
144 { 0x35, 0x64 }, /* swap the key above tab with key right of shift */
145 { 0x64, 0x35 },
146 { 0x31, 0x32 }, /* key left of return is Europe1, not "\|" */
147 { 0x00, 0x00 }
148 };
149
150 #ifdef GDIUM_KEYBOARD_HACK
151 Static const struct ukbd_keycodetrans trtab_gdium_fn[] = {
152 #ifdef notyet
153 { 58, 0 }, /* F1 -> toggle camera */
154 { 59, 0 }, /* F2 -> toggle wireless */
155 #endif
156 { 60, IS_PMF | PMFE_AUDIO_VOLUME_TOGGLE },
157 { 61, IS_PMF | PMFE_AUDIO_VOLUME_UP },
158 { 62, IS_PMF | PMFE_AUDIO_VOLUME_DOWN },
159 #ifdef notyet
160 { 63, 0 }, /* F6 -> toggle ext. video */
161 { 64, 0 }, /* F7 -> toggle mouse */
162 #endif
163 { 65, IS_PMF | PMFE_DISPLAY_BRIGHTNESS_UP },
164 { 66, IS_PMF | PMFE_DISPLAY_BRIGHTNESS_DOWN },
165 #ifdef notyet
166 { 67, 0 }, /* F10 -> suspend */
167 { 68, 0 }, /* F11 -> user1 */
168 { 69, 0 }, /* F12 -> user2 */
169 { 70, 0 }, /* print screen -> sysrq */
170 #endif
171 { 76, 71 }, /* delete -> scroll lock */
172 { 81, 78 }, /* down -> page down */
173 { 82, 75 }, /* up -> page up */
174 { 0, 0 }
175 };
176 #endif
177
178 #if defined(WSDISPLAY_COMPAT_RAWKBD)
179 #define NN 0 /* no translation */
180 /*
181 * Translate USB keycodes to US keyboard XT scancodes.
182 * Scancodes >= 0x80 represent EXTENDED keycodes.
183 *
184 * See http://www.microsoft.com/whdc/archive/scancode.mspx
185 *
186 * Note: a real pckbd(4) has more complexity in its
187 * protocol for some keys than this translation implements.
188 * For example, some keys generate Fake ShiftL events (e0 2a)
189 * before the actual key sequence.
190 */
191 Static const u_int8_t ukbd_trtab[256] = {
192 NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
193 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
194 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
195 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
196 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
197 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
198 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
199 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
200 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xb7, 0x46, /* 40 - 47 */
201 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
202 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
203 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
204 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, NN, 0x59, /* 60 - 67 */
205 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, NN, /* 68 - 6f */
206 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
207 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7f */
208 NN, NN, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */
209 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */
210 NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */
211 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */
212 NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */
213 NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */
214 NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */
215 NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */
216 NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */
217 NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */
218 NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */
219 NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */
220 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
221 NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */
222 NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */
223 NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */
224 };
225 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
226
227 #define KEY_ERROR 0x01
228
229 #define MAXKEYS (MAXMOD+2*MAXKEYCODE)
230
231 struct ukbd_softc {
232 struct uhidev sc_hdev;
233
234 struct ukbd_data sc_ndata;
235 struct ukbd_data sc_odata;
236 struct hid_location sc_modloc[MAXMOD];
237 u_int sc_nmod;
238 struct {
239 u_int32_t mask;
240 u_int8_t key;
241 } sc_mods[MAXMOD];
242
243 struct hid_location sc_keycodeloc;
244 u_int sc_nkeycode;
245
246 u_int sc_flags; /* flags */
247 #define FLAG_ENABLED 0x0001
248 #define FLAG_POLLING 0x0002
249 #define FLAG_DEBOUNCE 0x0004 /* for quirk handling */
250 #define FLAG_APPLE_FIX_ISO 0x0008
251 #define FLAG_APPLE_FN 0x0010
252 #define FLAG_GDIUM_FN 0x0020
253 #define FLAG_FN_PRESSED 0x0100 /* FN key is held down */
254 #define FLAG_FN_ALT 0x0200 /* Last Alt key was FN-Alt = AltGr */
255
256 int sc_console_keyboard; /* we are the console keyboard */
257
258 struct callout sc_delay; /* for quirk handling */
259 struct ukbd_data sc_data; /* for quirk handling */
260
261 struct hid_location sc_apple_fn;
262 struct hid_location sc_numloc;
263 struct hid_location sc_capsloc;
264 struct hid_location sc_scroloc;
265 int sc_leds;
266 device_t sc_wskbddev;
267
268 #if defined(WSDISPLAY_COMPAT_RAWKBD)
269 int sc_rawkbd;
270 #if defined(UKBD_REPEAT)
271 struct callout sc_rawrepeat_ch;
272 #define REP_DELAY1 400
273 #define REP_DELAYN 100
274 int sc_nrep;
275 char sc_rep[MAXKEYS];
276 #endif /* defined(UKBD_REPEAT) */
277 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
278
279 int sc_spl;
280 int sc_npollchar;
281 u_int16_t sc_pollchars[MAXKEYS];
282
283 u_char sc_dying;
284 };
285
286 #ifdef UKBD_DEBUG
287 #define UKBDTRACESIZE 64
288 struct ukbdtraceinfo {
289 int unit;
290 struct timeval tv;
291 struct ukbd_data ud;
292 };
293 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
294 int ukbdtraceindex = 0;
295 int ukbdtrace = 0;
296 void ukbdtracedump(void);
297 void
298 ukbdtracedump(void)
299 {
300 int i;
301 for (i = 0; i < UKBDTRACESIZE; i++) {
302 struct ukbdtraceinfo *p =
303 &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
304 printf("%"PRIu64".%06"PRIu64": mod=0x%02x key0=0x%02x key1=0x%02x "
305 "key2=0x%02x key3=0x%02x\n",
306 p->tv.tv_sec, (uint64_t)p->tv.tv_usec,
307 p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1],
308 p->ud.keycode[2], p->ud.keycode[3]);
309 }
310 }
311 #endif
312
313 #define UKBDUNIT(dev) (minor(dev))
314 #define UKBD_CHUNK 128 /* chunk size for read */
315 #define UKBD_BSIZE 1020 /* buffer size */
316
317 Static int ukbd_is_console;
318
319 Static void ukbd_cngetc(void *, u_int *, int *);
320 Static void ukbd_cnpollc(void *, int);
321
322 const struct wskbd_consops ukbd_consops = {
323 ukbd_cngetc,
324 ukbd_cnpollc,
325 NULL, /* bell */
326 };
327
328 Static const char *ukbd_parse_desc(struct ukbd_softc *sc);
329
330 Static void ukbd_intr(struct uhidev *addr, void *ibuf, u_int len);
331 Static void ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud);
332 Static void ukbd_delayed_decode(void *addr);
333
334 Static int ukbd_enable(void *, int);
335 Static void ukbd_set_leds(void *, int);
336
337 Static int ukbd_ioctl(void *, u_long, void *, int, struct lwp *);
338 #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
339 Static void ukbd_rawrepeat(void *v);
340 #endif
341
342 const struct wskbd_accessops ukbd_accessops = {
343 ukbd_enable,
344 ukbd_set_leds,
345 ukbd_ioctl,
346 };
347
348 extern const struct wscons_keydesc ukbd_keydesctab[];
349
350 const struct wskbd_mapdata ukbd_keymapdata = {
351 ukbd_keydesctab,
352 #if defined(UKBD_LAYOUT)
353 UKBD_LAYOUT,
354 #elif defined(PCKBD_LAYOUT)
355 PCKBD_LAYOUT,
356 #else
357 KB_US,
358 #endif
359 };
360
361 static int ukbd_match(device_t, cfdata_t, void *);
362 static void ukbd_attach(device_t, device_t, void *);
363 static int ukbd_detach(device_t, int);
364 static int ukbd_activate(device_t, enum devact);
365 static void ukbd_childdet(device_t, device_t);
366
367 extern struct cfdriver ukbd_cd;
368
369 CFATTACH_DECL2_NEW(ukbd, sizeof(struct ukbd_softc), ukbd_match, ukbd_attach,
370 ukbd_detach, ukbd_activate, NULL, ukbd_childdet);
371
372 int
373 ukbd_match(device_t parent, cfdata_t match, void *aux)
374 {
375 struct uhidev_attach_arg *uha = aux;
376 int size;
377 void *desc;
378
379 uhidev_get_report_desc(uha->parent, &desc, &size);
380 if (!hid_is_collection(desc, size, uha->reportid,
381 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
382 return (UMATCH_NONE);
383
384 return (UMATCH_IFACECLASS);
385 }
386
387 void
388 ukbd_attach(device_t parent, device_t self, void *aux)
389 {
390 struct ukbd_softc *sc = device_private(self);
391 struct uhidev_attach_arg *uha = aux;
392 u_int32_t qflags;
393 const char *parseerr;
394 struct wskbddev_attach_args a;
395
396 sc->sc_hdev.sc_dev = self;
397 sc->sc_hdev.sc_intr = ukbd_intr;
398 sc->sc_hdev.sc_parent = uha->parent;
399 sc->sc_hdev.sc_report_id = uha->reportid;
400 sc->sc_flags = 0;
401
402 if (!pmf_device_register(self, NULL, NULL)) {
403 aprint_normal("\n");
404 aprint_error_dev(self, "couldn't establish power handler\n");
405 }
406
407 parseerr = ukbd_parse_desc(sc);
408 if (parseerr != NULL) {
409 aprint_normal("\n");
410 aprint_error_dev(self, "attach failed, %s\n", parseerr);
411 return;
412 }
413
414 /* Quirks */
415 qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
416 if (qflags & UQ_SPUR_BUT_UP)
417 sc->sc_flags |= FLAG_DEBOUNCE;
418 if (qflags & UQ_APPLE_ISO)
419 sc->sc_flags |= FLAG_APPLE_FIX_ISO;
420
421 #ifdef GDIUM_KEYBOARD_HACK
422 if (uha->uaa->vendor == USB_VENDOR_CYPRESS &&
423 uha->uaa->product == USB_PRODUCT_CYPRESS_LPRDK)
424 sc->sc_flags = FLAG_GDIUM_FN;
425 #endif
426
427 #ifdef DIAGNOSTIC
428 aprint_normal(": %d modifier keys, %d key codes", sc->sc_nmod,
429 sc->sc_nkeycode);
430 if (sc->sc_flags & FLAG_APPLE_FN)
431 aprint_normal(", apple fn key");
432 if (sc->sc_flags & FLAG_APPLE_FIX_ISO)
433 aprint_normal(", fix apple iso");
434 if (sc->sc_flags & FLAG_GDIUM_FN)
435 aprint_normal(", Gdium fn key");
436 #endif
437 aprint_normal("\n");
438
439 /*
440 * Remember if we're the console keyboard.
441 *
442 * XXX This always picks the first keyboard on the
443 * first USB bus, but what else can we really do?
444 */
445 if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
446 /* Don't let any other keyboard have it. */
447 ukbd_is_console = 0;
448 }
449
450 if (sc->sc_console_keyboard) {
451 DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
452 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
453 ukbd_enable(sc, 1);
454 }
455
456 a.console = sc->sc_console_keyboard;
457
458 a.keymap = &ukbd_keymapdata;
459
460 a.accessops = &ukbd_accessops;
461 a.accesscookie = sc;
462
463 #ifdef UKBD_REPEAT
464 callout_init(&sc->sc_rawrepeat_ch, 0);
465 #endif
466
467 callout_init(&sc->sc_delay, 0);
468
469 /* Flash the leds; no real purpose, just shows we're alive. */
470 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
471 usbd_delay_ms(uha->parent->sc_udev, 400);
472 ukbd_set_leds(sc, 0);
473
474 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
475
476 return;
477 }
478
479 int
480 ukbd_enable(void *v, int on)
481 {
482 struct ukbd_softc *sc = v;
483
484 if (on && sc->sc_dying)
485 return (EIO);
486
487 /* Should only be called to change state */
488 if ((sc->sc_flags & FLAG_ENABLED) != 0 && on != 0) {
489 #ifdef DIAGNOSTIC
490 printf("ukbd_enable: %s: bad call on=%d\n",
491 device_xname(sc->sc_hdev.sc_dev), on);
492 #endif
493 return (EBUSY);
494 }
495
496 DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
497 if (on) {
498 sc->sc_flags |= FLAG_ENABLED;
499 return (uhidev_open(&sc->sc_hdev));
500 } else {
501 sc->sc_flags &= ~FLAG_ENABLED;
502 uhidev_close(&sc->sc_hdev);
503 return (0);
504 }
505 }
506
507
508 static void
509 ukbd_childdet(device_t self, device_t child)
510 {
511 struct ukbd_softc *sc = device_private(self);
512
513 KASSERT(sc->sc_wskbddev == child);
514 sc->sc_wskbddev = NULL;
515 }
516
517 int
518 ukbd_activate(device_t self, enum devact act)
519 {
520 struct ukbd_softc *sc = device_private(self);
521
522 switch (act) {
523 case DVACT_DEACTIVATE:
524 sc->sc_dying = 1;
525 return 0;
526 default:
527 return EOPNOTSUPP;
528 }
529 }
530
531 int
532 ukbd_detach(device_t self, int flags)
533 {
534 struct ukbd_softc *sc = device_private(self);
535 int rv = 0;
536
537 DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
538
539 pmf_device_deregister(self);
540
541 if (sc->sc_console_keyboard) {
542 #if 0
543 /*
544 * XXX Should probably disconnect our consops,
545 * XXX and either notify some other keyboard that
546 * XXX it can now be the console, or if there aren't
547 * XXX any more USB keyboards, set ukbd_is_console
548 * XXX back to 1 so that the next USB keyboard attached
549 * XXX to the system will get it.
550 */
551 panic("ukbd_detach: console keyboard");
552 #else
553 /*
554 * Disconnect our consops and set ukbd_is_console
555 * back to 1 so that the next USB keyboard attached
556 * to the system will get it.
557 * XXX Should notify some other keyboard that it can be
558 * XXX console, if there are any other keyboards.
559 */
560 printf("%s: was console keyboard\n",
561 device_xname(sc->sc_hdev.sc_dev));
562 wskbd_cndetach();
563 ukbd_is_console = 1;
564 #endif
565 }
566 /* No need to do reference counting of ukbd, wskbd has all the goo. */
567 if (sc->sc_wskbddev != NULL)
568 rv = config_detach(sc->sc_wskbddev, flags);
569
570 /* The console keyboard does not get a disable call, so check pipe. */
571 if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
572 uhidev_close(&sc->sc_hdev);
573
574 return (rv);
575 }
576
577 static void
578 ukbd_translate_keycodes(struct ukbd_softc *sc, struct ukbd_data *ud,
579 const struct ukbd_keycodetrans *tab)
580 {
581 const struct ukbd_keycodetrans *tp;
582 int i;
583 u_int8_t key;
584
585 for (i = 0; i < sc->sc_nkeycode; i++) {
586 key = ud->keycode[i];
587 if (key)
588 for (tp = tab; tp->from; tp++)
589 if (tp->from == key) {
590 if (tp->to & IS_PMF) {
591 pmf_event_inject(
592 sc->sc_hdev.sc_dev,
593 tp->to & 0xff);
594 ud->keycode[i] = 0;
595 } else
596 ud->keycode[i] = tp->to;
597 break;
598 }
599 }
600 }
601
602 static u_int16_t
603 ukbd_translate_modifier(struct ukbd_softc *sc, u_int16_t key)
604 {
605 if ((sc->sc_flags & FLAG_APPLE_FN) && (key & CODEMASK) == 0x00e2) {
606 if ((key & ~CODEMASK) == PRESS) {
607 if (sc->sc_flags & FLAG_FN_PRESSED) {
608 /* pressed FN-Alt, translate to AltGr */
609 key = 0x00e6 | PRESS;
610 sc->sc_flags |= FLAG_FN_ALT;
611 }
612 } else {
613 if (sc->sc_flags & FLAG_FN_ALT) {
614 /* released Alt, which was treated as FN-Alt */
615 key = 0x00e6 | RELEASE;
616 sc->sc_flags &= ~FLAG_FN_ALT;
617 }
618 }
619 }
620 return key;
621 }
622
623 void
624 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
625 {
626 struct ukbd_softc *sc = (struct ukbd_softc *)addr;
627 struct ukbd_data *ud = &sc->sc_ndata;
628 int i;
629
630 #ifdef UKBD_DEBUG
631 if (ukbddebug > 5) {
632 printf("ukbd_intr: data");
633 for (i = 0; i < len; i++)
634 printf(" 0x%02x", ((u_char *)ibuf)[i]);
635 printf("\n");
636 }
637 #endif
638
639 ud->modifiers = 0;
640 for (i = 0; i < sc->sc_nmod; i++)
641 if (hid_get_data(ibuf, &sc->sc_modloc[i]))
642 ud->modifiers |= sc->sc_mods[i].mask;
643 memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8,
644 sc->sc_nkeycode);
645
646 if (sc->sc_flags & FLAG_APPLE_FN) {
647 if (hid_get_data(ibuf, &sc->sc_apple_fn)) {
648 sc->sc_flags |= FLAG_FN_PRESSED;
649 ukbd_translate_keycodes(sc, ud, trtab_apple_fn);
650 }
651 else
652 sc->sc_flags &= ~FLAG_FN_PRESSED;
653 }
654
655 #ifdef GDIUM_KEYBOARD_HACK
656 if (sc->sc_flags & FLAG_GDIUM_FN) {
657 if (sc->sc_flags & FLAG_FN_PRESSED) {
658 ukbd_translate_keycodes(sc, ud, trtab_gdium_fn);
659 }
660 }
661 #endif
662
663 if ((sc->sc_flags & FLAG_DEBOUNCE) && !(sc->sc_flags & FLAG_POLLING)) {
664 /*
665 * Some keyboards have a peculiar quirk. They sometimes
666 * generate a key up followed by a key down for the same
667 * key after about 10 ms.
668 * We avoid this bug by holding off decoding for 20 ms.
669 */
670 sc->sc_data = *ud;
671 callout_reset(&sc->sc_delay, hz / 50, ukbd_delayed_decode, sc);
672 #ifdef DDB
673 } else if (sc->sc_console_keyboard && !(sc->sc_flags & FLAG_POLLING)) {
674 /*
675 * For the console keyboard we can't deliver CTL-ALT-ESC
676 * from the interrupt routine. Doing so would start
677 * polling from inside the interrupt routine and that
678 * loses bigtime.
679 */
680 sc->sc_data = *ud;
681 callout_reset(&sc->sc_delay, 1, ukbd_delayed_decode, sc);
682 #endif
683 } else {
684 ukbd_decode(sc, ud);
685 }
686 }
687
688 void
689 ukbd_delayed_decode(void *addr)
690 {
691 struct ukbd_softc *sc = addr;
692
693 ukbd_decode(sc, &sc->sc_data);
694 }
695
696 void
697 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
698 {
699 int mod, omod;
700 u_int16_t ibuf[MAXKEYS]; /* chars events */
701 int s;
702 int nkeys, i, j;
703 int key;
704 #define ADDKEY(c) ibuf[nkeys++] = (c)
705
706 #ifdef UKBD_DEBUG
707 /*
708 * Keep a trace of the last events. Using printf changes the
709 * timing, so this can be useful sometimes.
710 */
711 if (ukbdtrace) {
712 struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
713 p->unit = device_unit(sc->sc_hdev.sc_dev);
714 microtime(&p->tv);
715 p->ud = *ud;
716 if (++ukbdtraceindex >= UKBDTRACESIZE)
717 ukbdtraceindex = 0;
718 }
719 if (ukbddebug > 5) {
720 struct timeval tv;
721 microtime(&tv);
722 DPRINTF((" at %"PRIu64".%06"PRIu64" mod=0x%02x key0=0x%02x key1=0x%02x "
723 "key2=0x%02x key3=0x%02x\n",
724 tv.tv_sec, (uint64_t)tv.tv_usec,
725 ud->modifiers, ud->keycode[0], ud->keycode[1],
726 ud->keycode[2], ud->keycode[3]));
727 }
728 #endif
729
730 if (ud->keycode[0] == KEY_ERROR) {
731 DPRINTF(("ukbd_intr: KEY_ERROR\n"));
732 return; /* ignore */
733 }
734
735 if (sc->sc_flags & FLAG_APPLE_FIX_ISO)
736 ukbd_translate_keycodes(sc, ud, trtab_apple_iso);
737
738 nkeys = 0;
739 mod = ud->modifiers;
740 omod = sc->sc_odata.modifiers;
741 if (mod != omod)
742 for (i = 0; i < sc->sc_nmod; i++)
743 if (( mod & sc->sc_mods[i].mask) !=
744 (omod & sc->sc_mods[i].mask)) {
745 key = sc->sc_mods[i].key |
746 ((mod & sc->sc_mods[i].mask) ?
747 PRESS : RELEASE);
748 ADDKEY(ukbd_translate_modifier(sc, key));
749 }
750 if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
751 /* Check for released keys. */
752 for (i = 0; i < sc->sc_nkeycode; i++) {
753 key = sc->sc_odata.keycode[i];
754 if (key == 0)
755 continue;
756 for (j = 0; j < sc->sc_nkeycode; j++)
757 if (key == ud->keycode[j])
758 goto rfound;
759 DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
760 #ifdef GDIUM_KEYBOARD_HACK
761 if (sc->sc_flags & FLAG_GDIUM_FN) {
762 if (key == 0x82) {
763 sc->sc_flags &= ~FLAG_FN_PRESSED;
764 goto rfound;
765 }
766 }
767 #endif
768 ADDKEY(key | RELEASE);
769 rfound:
770 ;
771 }
772
773 /* Check for pressed keys. */
774 for (i = 0; i < sc->sc_nkeycode; i++) {
775 key = ud->keycode[i];
776 if (key == 0)
777 continue;
778 for (j = 0; j < sc->sc_nkeycode; j++)
779 if (key == sc->sc_odata.keycode[j])
780 goto pfound;
781 DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
782 #ifdef GDIUM_KEYBOARD_HACK
783 if (sc->sc_flags & FLAG_GDIUM_FN) {
784 if (key == 0x82) {
785 sc->sc_flags |= FLAG_FN_PRESSED;
786 goto pfound;
787 }
788 }
789 #endif
790 ADDKEY(key | PRESS);
791 pfound:
792 ;
793 }
794 }
795 sc->sc_odata = *ud;
796
797 if (nkeys == 0)
798 return;
799
800 if (sc->sc_flags & FLAG_POLLING) {
801 DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
802 memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
803 sc->sc_npollchar = nkeys;
804 return;
805 }
806 #ifdef WSDISPLAY_COMPAT_RAWKBD
807 if (sc->sc_rawkbd) {
808 u_char cbuf[MAXKEYS * 2];
809 int c;
810 int npress;
811
812 for (npress = i = j = 0; i < nkeys; i++) {
813 key = ibuf[i];
814 c = ukbd_trtab[key & CODEMASK];
815 if (c == NN)
816 continue;
817 if (c == 0x7f) {
818 /* pause key */
819 cbuf[j++] = 0xe1;
820 cbuf[j++] = 0x1d;
821 cbuf[j-1] |= (key & RELEASE) ? 0x80 : 0;
822 cbuf[j] = 0x45;
823 } else {
824 if (c & 0x80)
825 cbuf[j++] = 0xe0;
826 cbuf[j] = c & 0x7f;
827 }
828 if (key & RELEASE)
829 cbuf[j] |= 0x80;
830 #if defined(UKBD_REPEAT)
831 else {
832 /* remember pressed keys for autorepeat */
833 if (c & 0x80)
834 sc->sc_rep[npress++] = 0xe0;
835 sc->sc_rep[npress++] = c & 0x7f;
836 }
837 #endif
838 DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
839 c & 0x80 ? "0xe0 " : "",
840 cbuf[j]));
841 j++;
842 }
843 s = spltty();
844 wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
845 splx(s);
846 #ifdef UKBD_REPEAT
847 callout_stop(&sc->sc_rawrepeat_ch);
848 if (npress != 0) {
849 sc->sc_nrep = npress;
850 callout_reset(&sc->sc_rawrepeat_ch,
851 hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
852 }
853 #endif
854 return;
855 }
856 #endif
857
858 s = spltty();
859 for (i = 0; i < nkeys; i++) {
860 key = ibuf[i];
861 wskbd_input(sc->sc_wskbddev,
862 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
863 key&CODEMASK);
864 }
865 splx(s);
866 }
867
868 void
869 ukbd_set_leds(void *v, int leds)
870 {
871 struct ukbd_softc *sc = v;
872 u_int8_t res;
873
874 DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n",
875 sc, leds, sc->sc_leds));
876
877 if (sc->sc_dying)
878 return;
879
880 if (sc->sc_leds == leds)
881 return;
882 sc->sc_leds = leds;
883 res = 0;
884 /* XXX not really right */
885 if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
886 res |= 1 << sc->sc_scroloc.pos;
887 if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
888 res |= 1 << sc->sc_numloc.pos;
889 if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
890 res |= 1 << sc->sc_capsloc.pos;
891 uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1);
892 }
893
894 #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
895 void
896 ukbd_rawrepeat(void *v)
897 {
898 struct ukbd_softc *sc = v;
899 int s;
900
901 s = spltty();
902 wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
903 splx(s);
904 callout_reset(&sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
905 ukbd_rawrepeat, sc);
906 }
907 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) */
908
909 int
910 ukbd_ioctl(void *v, u_long cmd, void *data, int flag,
911 struct lwp *l)
912 {
913 struct ukbd_softc *sc = v;
914
915 switch (cmd) {
916 case WSKBDIO_GTYPE:
917 *(int *)data = WSKBD_TYPE_USB;
918 return (0);
919 case WSKBDIO_SETLEDS:
920 ukbd_set_leds(v, *(int *)data);
921 return (0);
922 case WSKBDIO_GETLEDS:
923 *(int *)data = sc->sc_leds;
924 return (0);
925 #if defined(WSDISPLAY_COMPAT_RAWKBD)
926 case WSKBDIO_SETMODE:
927 DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
928 sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
929 #if defined(UKBD_REPEAT)
930 callout_stop(&sc->sc_rawrepeat_ch);
931 #endif
932 return (0);
933 #endif
934 }
935 return (EPASSTHROUGH);
936 }
937
938 /*
939 * This is a hack to work around some broken ports that don't call
940 * cnpollc() before cngetc().
941 */
942 static int pollenter, warned;
943
944 /* Console interface. */
945 void
946 ukbd_cngetc(void *v, u_int *type, int *data)
947 {
948 struct ukbd_softc *sc = v;
949 int c;
950 int broken;
951
952 if (pollenter == 0) {
953 if (!warned) {
954 printf("\n"
955 "This port is broken, it does not call cnpollc() before calling cngetc().\n"
956 "This should be fixed, but it will work anyway (for now).\n");
957 warned = 1;
958 }
959 broken = 1;
960 ukbd_cnpollc(v, 1);
961 } else
962 broken = 0;
963
964 DPRINTFN(0,("ukbd_cngetc: enter\n"));
965 sc->sc_flags |= FLAG_POLLING;
966 while(sc->sc_npollchar <= 0)
967 usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface);
968 sc->sc_flags &= ~FLAG_POLLING;
969 c = sc->sc_pollchars[0];
970 sc->sc_npollchar--;
971 memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
972 sc->sc_npollchar * sizeof(u_int16_t));
973 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
974 *data = c & CODEMASK;
975 DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
976 if (broken)
977 ukbd_cnpollc(v, 0);
978 }
979
980 void
981 ukbd_cnpollc(void *v, int on)
982 {
983 struct ukbd_softc *sc = v;
984 usbd_device_handle dev;
985
986 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
987
988 usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev);
989 if (on) {
990 sc->sc_spl = splusb();
991 pollenter++;
992 } else {
993 splx(sc->sc_spl);
994 pollenter--;
995 }
996 usbd_set_polling(dev, on);
997 }
998
999 int
1000 ukbd_cnattach(void)
1001 {
1002
1003 /*
1004 * XXX USB requires too many parts of the kernel to be running
1005 * XXX in order to work, so we can't do much for the console
1006 * XXX keyboard until autconfiguration has run its course.
1007 */
1008 ukbd_is_console = 1;
1009 return (0);
1010 }
1011
1012 const char *
1013 ukbd_parse_desc(struct ukbd_softc *sc)
1014 {
1015 struct hid_data *d;
1016 struct hid_item h;
1017 int size;
1018 void *desc;
1019 int imod;
1020
1021 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
1022 imod = 0;
1023 sc->sc_nkeycode = 0;
1024 d = hid_start_parse(desc, size, hid_input);
1025 while (hid_get_item(d, &h)) {
1026 /*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n",
1027 h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/
1028
1029 /* Check for special Apple notebook FN key */
1030 if (HID_GET_USAGE_PAGE(h.usage) == 0x00ff &&
1031 HID_GET_USAGE(h.usage) == 0x0003 &&
1032 h.kind == hid_input && (h.flags & HIO_VARIABLE)) {
1033 sc->sc_flags |= FLAG_APPLE_FN;
1034 sc->sc_apple_fn = h.loc;
1035 }
1036
1037 if (h.kind != hid_input || (h.flags & HIO_CONST) ||
1038 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
1039 h.report_ID != sc->sc_hdev.sc_report_id)
1040 continue;
1041 DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d "
1042 "cnt=%d\n", imod,
1043 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
1044 if (h.flags & HIO_VARIABLE) {
1045 if (h.loc.size != 1)
1046 return ("bad modifier size");
1047 /* Single item */
1048 if (imod < MAXMOD) {
1049 sc->sc_modloc[imod] = h.loc;
1050 sc->sc_mods[imod].mask = 1 << imod;
1051 sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
1052 imod++;
1053 } else
1054 return ("too many modifier keys");
1055 } else {
1056 /* Array */
1057 if (h.loc.size != 8)
1058 return ("key code size != 8");
1059 if (h.loc.count > MAXKEYCODE)
1060 h.loc.count = MAXKEYCODE;
1061 if (h.loc.pos % 8 != 0)
1062 return ("key codes not on byte boundary");
1063 if (sc->sc_nkeycode != 0)
1064 return ("multiple key code arrays\n");
1065 sc->sc_keycodeloc = h.loc;
1066 sc->sc_nkeycode = h.loc.count;
1067 }
1068 }
1069 sc->sc_nmod = imod;
1070 hid_end_parse(d);
1071
1072 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
1073 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL);
1074 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
1075 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL);
1076 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
1077 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL);
1078
1079 return (NULL);
1080 }
1081