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