ukbd.c revision 1.116 1 /* $NetBSD: ukbd.c,v 1.116 2012/02/24 06:48:26 mrg 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.116 2012/02/24 06:48:26 mrg 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(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(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 device_t sc_wskbddev;
265
266 #if defined(WSDISPLAY_COMPAT_RAWKBD)
267 int sc_rawkbd;
268 #if defined(UKBD_REPEAT)
269 struct callout sc_rawrepeat_ch;
270 #define REP_DELAY1 400
271 #define REP_DELAYN 100
272 int sc_nrep;
273 char sc_rep[MAXKEYS];
274 #endif /* defined(UKBD_REPEAT) */
275 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
276
277 int sc_spl;
278 int sc_npollchar;
279 u_int16_t sc_pollchars[MAXKEYS];
280
281 u_char sc_dying;
282 };
283
284 #ifdef UKBD_DEBUG
285 #define UKBDTRACESIZE 64
286 struct ukbdtraceinfo {
287 int unit;
288 struct timeval tv;
289 struct ukbd_data ud;
290 };
291 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
292 int ukbdtraceindex = 0;
293 int ukbdtrace = 0;
294 void ukbdtracedump(void);
295 void
296 ukbdtracedump(void)
297 {
298 int i;
299 for (i = 0; i < UKBDTRACESIZE; i++) {
300 struct ukbdtraceinfo *p =
301 &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
302 printf("%"PRIu64".%06"PRIu64": mod=0x%02x key0=0x%02x key1=0x%02x "
303 "key2=0x%02x key3=0x%02x\n",
304 p->tv.tv_sec, (uint64_t)p->tv.tv_usec,
305 p->ud.modifiers, p->ud.keycode[0], p->ud.keycode[1],
306 p->ud.keycode[2], p->ud.keycode[3]);
307 }
308 }
309 #endif
310
311 #define UKBDUNIT(dev) (minor(dev))
312 #define UKBD_CHUNK 128 /* chunk size for read */
313 #define UKBD_BSIZE 1020 /* buffer size */
314
315 Static int ukbd_is_console;
316
317 Static void ukbd_cngetc(void *, u_int *, int *);
318 Static void ukbd_cnpollc(void *, int);
319
320 const struct wskbd_consops ukbd_consops = {
321 ukbd_cngetc,
322 ukbd_cnpollc,
323 NULL, /* bell */
324 };
325
326 Static const char *ukbd_parse_desc(struct ukbd_softc *sc);
327
328 Static void ukbd_intr(struct uhidev *addr, void *ibuf, u_int len);
329 Static void ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud);
330 Static void ukbd_delayed_decode(void *addr);
331
332 Static int ukbd_enable(void *, int);
333 Static void ukbd_set_leds(void *, int);
334
335 Static int ukbd_ioctl(void *, u_long, void *, int, struct lwp *);
336 #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
337 Static void ukbd_rawrepeat(void *v);
338 #endif
339
340 const struct wskbd_accessops ukbd_accessops = {
341 ukbd_enable,
342 ukbd_set_leds,
343 ukbd_ioctl,
344 };
345
346 extern const struct wscons_keydesc ukbd_keydesctab[];
347
348 const struct wskbd_mapdata ukbd_keymapdata = {
349 ukbd_keydesctab,
350 #if defined(UKBD_LAYOUT)
351 UKBD_LAYOUT,
352 #elif defined(PCKBD_LAYOUT)
353 PCKBD_LAYOUT,
354 #else
355 KB_US,
356 #endif
357 };
358
359 static int ukbd_match(device_t, cfdata_t, void *);
360 static void ukbd_attach(device_t, device_t, void *);
361 static int ukbd_detach(device_t, int);
362 static int ukbd_activate(device_t, enum devact);
363 static void ukbd_childdet(device_t, device_t);
364
365 extern struct cfdriver ukbd_cd;
366
367 CFATTACH_DECL2_NEW(ukbd, sizeof(struct ukbd_softc), ukbd_match, ukbd_attach,
368 ukbd_detach, ukbd_activate, NULL, ukbd_childdet);
369
370 int
371 ukbd_match(device_t parent, cfdata_t match, void *aux)
372 {
373 struct uhidev_attach_arg *uha = aux;
374 int size;
375 void *desc;
376
377 uhidev_get_report_desc(uha->parent, &desc, &size);
378 if (!hid_is_collection(desc, size, uha->reportid,
379 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
380 return (UMATCH_NONE);
381
382 return (UMATCH_IFACECLASS);
383 }
384
385 void
386 ukbd_attach(device_t parent, device_t self, void *aux)
387 {
388 struct ukbd_softc *sc = device_private(self);
389 struct uhidev_attach_arg *uha = aux;
390 u_int32_t qflags;
391 const char *parseerr;
392 struct wskbddev_attach_args a;
393
394 sc->sc_hdev.sc_dev = self;
395 sc->sc_hdev.sc_intr = ukbd_intr;
396 sc->sc_hdev.sc_parent = uha->parent;
397 sc->sc_hdev.sc_report_id = uha->reportid;
398 sc->sc_flags = 0;
399
400 if (!pmf_device_register(self, NULL, NULL)) {
401 aprint_normal("\n");
402 aprint_error_dev(self, "couldn't establish power handler\n");
403 }
404
405 parseerr = ukbd_parse_desc(sc);
406 if (parseerr != NULL) {
407 aprint_normal("\n");
408 aprint_error_dev(self, "attach failed, %s\n", parseerr);
409 return;
410 }
411
412 /* Quirks */
413 qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
414 if (qflags & UQ_SPUR_BUT_UP)
415 sc->sc_flags |= FLAG_DEBOUNCE;
416 if (qflags & UQ_APPLE_ISO)
417 sc->sc_flags |= FLAG_APPLE_FIX_ISO;
418
419 #ifdef GDIUM_KEYBOARD_HACK
420 if (uha->uaa->vendor == USB_VENDOR_CYPRESS &&
421 uha->uaa->product == USB_PRODUCT_CYPRESS_LPRDK)
422 sc->sc_flags = FLAG_GDIUM_FN;
423 #endif
424
425 #ifdef DIAGNOSTIC
426 aprint_normal(": %d modifier keys, %d key codes", sc->sc_nmod,
427 sc->sc_nkeycode);
428 if (sc->sc_flags & FLAG_APPLE_FN)
429 aprint_normal(", apple fn key");
430 if (sc->sc_flags & FLAG_APPLE_FIX_ISO)
431 aprint_normal(", fix apple iso");
432 if (sc->sc_flags & FLAG_GDIUM_FN)
433 aprint_normal(", Gdium fn key");
434 #endif
435 aprint_normal("\n");
436
437 /*
438 * Remember if we're the console keyboard.
439 *
440 * XXX This always picks the first keyboard on the
441 * first USB bus, but what else can we really do?
442 */
443 if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
444 /* Don't let any other keyboard have it. */
445 ukbd_is_console = 0;
446 }
447
448 if (sc->sc_console_keyboard) {
449 DPRINTF(("ukbd_attach: console keyboard sc=%p\n", sc));
450 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
451 ukbd_enable(sc, 1);
452 }
453
454 a.console = sc->sc_console_keyboard;
455
456 a.keymap = &ukbd_keymapdata;
457
458 a.accessops = &ukbd_accessops;
459 a.accesscookie = sc;
460
461 #ifdef UKBD_REPEAT
462 callout_init(&sc->sc_rawrepeat_ch, 0);
463 #endif
464
465 callout_init(&sc->sc_delay, 0);
466
467 /* Flash the leds; no real purpose, just shows we're alive. */
468 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
469 usbd_delay_ms(uha->parent->sc_udev, 400);
470 ukbd_set_leds(sc, 0);
471
472 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
473
474 return;
475 }
476
477 int
478 ukbd_enable(void *v, int on)
479 {
480 struct ukbd_softc *sc = v;
481
482 if (on && sc->sc_dying)
483 return (EIO);
484
485 /* Should only be called to change state */
486 if ((sc->sc_flags & FLAG_ENABLED) != 0 && on != 0) {
487 #ifdef DIAGNOSTIC
488 printf("ukbd_enable: %s: bad call on=%d\n",
489 device_xname(sc->sc_hdev.sc_dev), on);
490 #endif
491 return (EBUSY);
492 }
493
494 DPRINTF(("ukbd_enable: sc=%p on=%d\n", sc, on));
495 if (on) {
496 sc->sc_flags |= FLAG_ENABLED;
497 return (uhidev_open(&sc->sc_hdev));
498 } else {
499 sc->sc_flags &= ~FLAG_ENABLED;
500 uhidev_close(&sc->sc_hdev);
501 return (0);
502 }
503 }
504
505
506 static void
507 ukbd_childdet(device_t self, device_t child)
508 {
509 struct ukbd_softc *sc = device_private(self);
510
511 KASSERT(sc->sc_wskbddev == child);
512 sc->sc_wskbddev = NULL;
513 }
514
515 int
516 ukbd_activate(device_t self, enum devact act)
517 {
518 struct ukbd_softc *sc = device_private(self);
519
520 switch (act) {
521 case DVACT_DEACTIVATE:
522 sc->sc_dying = 1;
523 return 0;
524 default:
525 return EOPNOTSUPP;
526 }
527 }
528
529 int
530 ukbd_detach(device_t self, int flags)
531 {
532 struct ukbd_softc *sc = device_private(self);
533 int rv = 0;
534
535 DPRINTF(("ukbd_detach: sc=%p flags=%d\n", sc, flags));
536
537 pmf_device_deregister(self);
538
539 if (sc->sc_console_keyboard) {
540 #if 0
541 /*
542 * XXX Should probably disconnect our consops,
543 * XXX and either notify some other keyboard that
544 * XXX it can now be the console, or if there aren't
545 * XXX any more USB keyboards, set ukbd_is_console
546 * XXX back to 1 so that the next USB keyboard attached
547 * XXX to the system will get it.
548 */
549 panic("ukbd_detach: console keyboard");
550 #else
551 /*
552 * Disconnect our consops and set ukbd_is_console
553 * back to 1 so that the next USB keyboard attached
554 * to the system will get it.
555 * XXX Should notify some other keyboard that it can be
556 * XXX console, if there are any other keyboards.
557 */
558 printf("%s: was console keyboard\n",
559 device_xname(sc->sc_hdev.sc_dev));
560 wskbd_cndetach();
561 ukbd_is_console = 1;
562 #endif
563 }
564 /* No need to do reference counting of ukbd, wskbd has all the goo. */
565 if (sc->sc_wskbddev != NULL)
566 rv = config_detach(sc->sc_wskbddev, flags);
567
568 /* The console keyboard does not get a disable call, so check pipe. */
569 if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
570 uhidev_close(&sc->sc_hdev);
571
572 return (rv);
573 }
574
575 static void
576 ukbd_translate_keycodes(struct ukbd_softc *sc, struct ukbd_data *ud,
577 const struct ukbd_keycodetrans *tab)
578 {
579 const struct ukbd_keycodetrans *tp;
580 int i;
581 u_int8_t key;
582
583 for (i = 0; i < sc->sc_nkeycode; i++) {
584 key = ud->keycode[i];
585 if (key)
586 for (tp = tab; tp->from; tp++)
587 if (tp->from == key) {
588 if (tp->to & IS_PMF) {
589 pmf_event_inject(
590 sc->sc_hdev.sc_dev,
591 tp->to & 0xff);
592 ud->keycode[i] = 0;
593 } else
594 ud->keycode[i] = tp->to;
595 break;
596 }
597 }
598 }
599
600 static u_int16_t
601 ukbd_translate_modifier(struct ukbd_softc *sc, u_int16_t key)
602 {
603 if ((sc->sc_flags & FLAG_APPLE_FN) && (key & CODEMASK) == 0x00e2) {
604 if ((key & ~CODEMASK) == PRESS) {
605 if (sc->sc_flags & FLAG_FN_PRESSED) {
606 /* pressed FN-Alt, translate to AltGr */
607 key = 0x00e6 | PRESS;
608 sc->sc_flags |= FLAG_FN_ALT;
609 }
610 } else {
611 if (sc->sc_flags & FLAG_FN_ALT) {
612 /* released Alt, which was treated as FN-Alt */
613 key = 0x00e6 | RELEASE;
614 sc->sc_flags &= ~FLAG_FN_ALT;
615 }
616 }
617 }
618 return key;
619 }
620
621 void
622 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
623 {
624 struct ukbd_softc *sc = (struct ukbd_softc *)addr;
625 struct ukbd_data *ud = &sc->sc_ndata;
626 int i;
627
628 #ifdef UKBD_DEBUG
629 if (ukbddebug > 5) {
630 printf("ukbd_intr: data");
631 for (i = 0; i < len; i++)
632 printf(" 0x%02x", ((u_char *)ibuf)[i]);
633 printf("\n");
634 }
635 #endif
636
637 ud->modifiers = 0;
638 for (i = 0; i < sc->sc_nmod; i++)
639 if (hid_get_data(ibuf, &sc->sc_modloc[i]))
640 ud->modifiers |= sc->sc_mods[i].mask;
641 memcpy(ud->keycode, (char *)ibuf + sc->sc_keycodeloc.pos / 8,
642 sc->sc_nkeycode);
643
644 if (sc->sc_flags & FLAG_APPLE_FN) {
645 if (hid_get_data(ibuf, &sc->sc_apple_fn)) {
646 sc->sc_flags |= FLAG_FN_PRESSED;
647 ukbd_translate_keycodes(sc, ud, trtab_apple_fn);
648 }
649 else
650 sc->sc_flags &= ~FLAG_FN_PRESSED;
651 }
652
653 #ifdef GDIUM_KEYBOARD_HACK
654 if (sc->sc_flags & FLAG_GDIUM_FN) {
655 if (sc->sc_flags & FLAG_FN_PRESSED) {
656 ukbd_translate_keycodes(sc, ud, trtab_gdium_fn);
657 }
658 }
659 #endif
660
661 if ((sc->sc_flags & FLAG_DEBOUNCE) && !(sc->sc_flags & FLAG_POLLING)) {
662 /*
663 * Some keyboards have a peculiar quirk. They sometimes
664 * generate a key up followed by a key down for the same
665 * key after about 10 ms.
666 * We avoid this bug by holding off decoding for 20 ms.
667 */
668 sc->sc_data = *ud;
669 callout_reset(&sc->sc_delay, hz / 50, ukbd_delayed_decode, sc);
670 #ifdef DDB
671 } else if (sc->sc_console_keyboard && !(sc->sc_flags & FLAG_POLLING)) {
672 /*
673 * For the console keyboard we can't deliver CTL-ALT-ESC
674 * from the interrupt routine. Doing so would start
675 * polling from inside the interrupt routine and that
676 * loses bigtime.
677 */
678 sc->sc_data = *ud;
679 callout_reset(&sc->sc_delay, 1, ukbd_delayed_decode, sc);
680 #endif
681 } else {
682 ukbd_decode(sc, ud);
683 }
684 }
685
686 void
687 ukbd_delayed_decode(void *addr)
688 {
689 struct ukbd_softc *sc = addr;
690
691 ukbd_decode(sc, &sc->sc_data);
692 }
693
694 void
695 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
696 {
697 int mod, omod;
698 u_int16_t ibuf[MAXKEYS]; /* chars events */
699 int s;
700 int nkeys, i, j;
701 int key;
702 #define ADDKEY(c) ibuf[nkeys++] = (c)
703
704 #ifdef UKBD_DEBUG
705 /*
706 * Keep a trace of the last events. Using printf changes the
707 * timing, so this can be useful sometimes.
708 */
709 if (ukbdtrace) {
710 struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
711 p->unit = device_unit(sc->sc_hdev.sc_dev);
712 microtime(&p->tv);
713 p->ud = *ud;
714 if (++ukbdtraceindex >= UKBDTRACESIZE)
715 ukbdtraceindex = 0;
716 }
717 if (ukbddebug > 5) {
718 struct timeval tv;
719 microtime(&tv);
720 DPRINTF((" at %"PRIu64".%06"PRIu64" mod=0x%02x key0=0x%02x key1=0x%02x "
721 "key2=0x%02x key3=0x%02x\n",
722 tv.tv_sec, (uint64_t)tv.tv_usec,
723 ud->modifiers, ud->keycode[0], ud->keycode[1],
724 ud->keycode[2], ud->keycode[3]));
725 }
726 #endif
727
728 if (ud->keycode[0] == KEY_ERROR) {
729 DPRINTF(("ukbd_intr: KEY_ERROR\n"));
730 return; /* ignore */
731 }
732
733 if (sc->sc_flags & FLAG_APPLE_FIX_ISO)
734 ukbd_translate_keycodes(sc, ud, trtab_apple_iso);
735
736 nkeys = 0;
737 mod = ud->modifiers;
738 omod = sc->sc_odata.modifiers;
739 if (mod != omod)
740 for (i = 0; i < sc->sc_nmod; i++)
741 if (( mod & sc->sc_mods[i].mask) !=
742 (omod & sc->sc_mods[i].mask)) {
743 key = sc->sc_mods[i].key |
744 ((mod & sc->sc_mods[i].mask) ?
745 PRESS : RELEASE);
746 ADDKEY(ukbd_translate_modifier(sc, key));
747 }
748 if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
749 /* Check for released keys. */
750 for (i = 0; i < sc->sc_nkeycode; i++) {
751 key = sc->sc_odata.keycode[i];
752 if (key == 0)
753 continue;
754 for (j = 0; j < sc->sc_nkeycode; j++)
755 if (key == ud->keycode[j])
756 goto rfound;
757 DPRINTFN(3,("ukbd_intr: relse key=0x%02x\n", key));
758 #ifdef GDIUM_KEYBOARD_HACK
759 if (sc->sc_flags & FLAG_GDIUM_FN) {
760 if (key == 0x82) {
761 sc->sc_flags &= ~FLAG_FN_PRESSED;
762 goto rfound;
763 }
764 }
765 #endif
766 ADDKEY(key | RELEASE);
767 rfound:
768 ;
769 }
770
771 /* Check for pressed keys. */
772 for (i = 0; i < sc->sc_nkeycode; i++) {
773 key = ud->keycode[i];
774 if (key == 0)
775 continue;
776 for (j = 0; j < sc->sc_nkeycode; j++)
777 if (key == sc->sc_odata.keycode[j])
778 goto pfound;
779 DPRINTFN(2,("ukbd_intr: press key=0x%02x\n", key));
780 #ifdef GDIUM_KEYBOARD_HACK
781 if (sc->sc_flags & FLAG_GDIUM_FN) {
782 if (key == 0x82) {
783 sc->sc_flags |= FLAG_FN_PRESSED;
784 goto pfound;
785 }
786 }
787 #endif
788 ADDKEY(key | PRESS);
789 pfound:
790 ;
791 }
792 }
793 sc->sc_odata = *ud;
794
795 if (nkeys == 0)
796 return;
797
798 if (sc->sc_flags & FLAG_POLLING) {
799 DPRINTFN(1,("ukbd_intr: pollchar = 0x%03x\n", ibuf[0]));
800 memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
801 sc->sc_npollchar = nkeys;
802 return;
803 }
804 #ifdef WSDISPLAY_COMPAT_RAWKBD
805 if (sc->sc_rawkbd) {
806 u_char cbuf[MAXKEYS * 2];
807 int c;
808 int npress;
809
810 for (npress = i = j = 0; i < nkeys; i++) {
811 key = ibuf[i];
812 c = ukbd_trtab[key & CODEMASK];
813 if (c == NN)
814 continue;
815 if (c == 0x7f) {
816 /* pause key */
817 cbuf[j++] = 0xe1;
818 cbuf[j++] = 0x1d;
819 cbuf[j-1] |= (key & RELEASE) ? 0x80 : 0;
820 cbuf[j] = 0x45;
821 } else {
822 if (c & 0x80)
823 cbuf[j++] = 0xe0;
824 cbuf[j] = c & 0x7f;
825 }
826 if (key & RELEASE)
827 cbuf[j] |= 0x80;
828 #if defined(UKBD_REPEAT)
829 else {
830 /* remember pressed keys for autorepeat */
831 if (c & 0x80)
832 sc->sc_rep[npress++] = 0xe0;
833 sc->sc_rep[npress++] = c & 0x7f;
834 }
835 #endif
836 DPRINTFN(1,("ukbd_intr: raw = %s0x%02x\n",
837 c & 0x80 ? "0xe0 " : "",
838 cbuf[j]));
839 j++;
840 }
841 s = spltty();
842 wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
843 splx(s);
844 #ifdef UKBD_REPEAT
845 callout_stop(&sc->sc_rawrepeat_ch);
846 if (npress != 0) {
847 sc->sc_nrep = npress;
848 callout_reset(&sc->sc_rawrepeat_ch,
849 hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
850 }
851 #endif
852 return;
853 }
854 #endif
855
856 s = spltty();
857 for (i = 0; i < nkeys; i++) {
858 key = ibuf[i];
859 wskbd_input(sc->sc_wskbddev,
860 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
861 key&CODEMASK);
862 }
863 splx(s);
864 }
865
866 void
867 ukbd_set_leds(void *v, int leds)
868 {
869 struct ukbd_softc *sc = v;
870 u_int8_t res;
871
872 DPRINTF(("ukbd_set_leds: sc=%p leds=%d, sc_leds=%d\n",
873 sc, leds, sc->sc_leds));
874
875 if (sc->sc_dying)
876 return;
877
878 if (sc->sc_leds == leds)
879 return;
880 sc->sc_leds = leds;
881 res = 0;
882 /* XXX not really right */
883 if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
884 res |= 1 << sc->sc_scroloc.pos;
885 if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
886 res |= 1 << sc->sc_numloc.pos;
887 if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
888 res |= 1 << sc->sc_capsloc.pos;
889 uhidev_set_report_async(&sc->sc_hdev, UHID_OUTPUT_REPORT, &res, 1);
890 }
891
892 #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
893 void
894 ukbd_rawrepeat(void *v)
895 {
896 struct ukbd_softc *sc = v;
897 int s;
898
899 s = spltty();
900 wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
901 splx(s);
902 callout_reset(&sc->sc_rawrepeat_ch, hz * REP_DELAYN / 1000,
903 ukbd_rawrepeat, sc);
904 }
905 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT) */
906
907 int
908 ukbd_ioctl(void *v, u_long cmd, void *data, int flag,
909 struct lwp *l)
910 {
911 struct ukbd_softc *sc = v;
912
913 switch (cmd) {
914 case WSKBDIO_GTYPE:
915 *(int *)data = WSKBD_TYPE_USB;
916 return (0);
917 case WSKBDIO_SETLEDS:
918 ukbd_set_leds(v, *(int *)data);
919 return (0);
920 case WSKBDIO_GETLEDS:
921 *(int *)data = sc->sc_leds;
922 return (0);
923 #if defined(WSDISPLAY_COMPAT_RAWKBD)
924 case WSKBDIO_SETMODE:
925 DPRINTF(("ukbd_ioctl: set raw = %d\n", *(int *)data));
926 sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
927 #if defined(UKBD_REPEAT)
928 callout_stop(&sc->sc_rawrepeat_ch);
929 #endif
930 return (0);
931 #endif
932 }
933 return (EPASSTHROUGH);
934 }
935
936 /*
937 * This is a hack to work around some broken ports that don't call
938 * cnpollc() before cngetc().
939 */
940 static int pollenter, warned;
941
942 /* Console interface. */
943 void
944 ukbd_cngetc(void *v, u_int *type, int *data)
945 {
946 struct ukbd_softc *sc = v;
947 int c;
948 int broken;
949
950 if (pollenter == 0) {
951 if (!warned) {
952 printf("\n"
953 "This port is broken, it does not call cnpollc() before calling cngetc().\n"
954 "This should be fixed, but it will work anyway (for now).\n");
955 warned = 1;
956 }
957 broken = 1;
958 ukbd_cnpollc(v, 1);
959 } else
960 broken = 0;
961
962 DPRINTFN(0,("ukbd_cngetc: enter\n"));
963 sc->sc_flags |= FLAG_POLLING;
964 while(sc->sc_npollchar <= 0)
965 usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface);
966 sc->sc_flags &= ~FLAG_POLLING;
967 c = sc->sc_pollchars[0];
968 sc->sc_npollchar--;
969 memcpy(sc->sc_pollchars, sc->sc_pollchars+1,
970 sc->sc_npollchar * sizeof(u_int16_t));
971 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
972 *data = c & CODEMASK;
973 DPRINTFN(0,("ukbd_cngetc: return 0x%02x\n", c));
974 if (broken)
975 ukbd_cnpollc(v, 0);
976 }
977
978 void
979 ukbd_cnpollc(void *v, int on)
980 {
981 struct ukbd_softc *sc = v;
982 usbd_device_handle dev;
983
984 DPRINTFN(2,("ukbd_cnpollc: sc=%p on=%d\n", v, on));
985
986 usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev);
987 if (on) {
988 sc->sc_spl = splusb();
989 pollenter++;
990 } else {
991 splx(sc->sc_spl);
992 pollenter--;
993 }
994 usbd_set_polling(dev, on);
995 }
996
997 int
998 ukbd_cnattach(void)
999 {
1000
1001 /*
1002 * XXX USB requires too many parts of the kernel to be running
1003 * XXX in order to work, so we can't do much for the console
1004 * XXX keyboard until autconfiguration has run its course.
1005 */
1006 ukbd_is_console = 1;
1007 return (0);
1008 }
1009
1010 const char *
1011 ukbd_parse_desc(struct ukbd_softc *sc)
1012 {
1013 struct hid_data *d;
1014 struct hid_item h;
1015 int size;
1016 void *desc;
1017 int imod;
1018
1019 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
1020 imod = 0;
1021 sc->sc_nkeycode = 0;
1022 d = hid_start_parse(desc, size, hid_input);
1023 while (hid_get_item(d, &h)) {
1024 /*printf("ukbd: id=%d kind=%d usage=0x%x flags=0x%x pos=%d size=%d cnt=%d\n",
1025 h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/
1026
1027 /* Check for special Apple notebook FN key */
1028 if (HID_GET_USAGE_PAGE(h.usage) == 0x00ff &&
1029 HID_GET_USAGE(h.usage) == 0x0003 &&
1030 h.kind == hid_input && (h.flags & HIO_VARIABLE)) {
1031 sc->sc_flags |= FLAG_APPLE_FN;
1032 sc->sc_apple_fn = h.loc;
1033 }
1034
1035 if (h.kind != hid_input || (h.flags & HIO_CONST) ||
1036 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
1037 h.report_ID != sc->sc_hdev.sc_report_id)
1038 continue;
1039 DPRINTF(("ukbd: imod=%d usage=0x%x flags=0x%x pos=%d size=%d "
1040 "cnt=%d\n", imod,
1041 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
1042 if (h.flags & HIO_VARIABLE) {
1043 if (h.loc.size != 1)
1044 return ("bad modifier size");
1045 /* Single item */
1046 if (imod < MAXMOD) {
1047 sc->sc_modloc[imod] = h.loc;
1048 sc->sc_mods[imod].mask = 1 << imod;
1049 sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
1050 imod++;
1051 } else
1052 return ("too many modifier keys");
1053 } else {
1054 /* Array */
1055 if (h.loc.size != 8)
1056 return ("key code size != 8");
1057 if (h.loc.count > MAXKEYCODE)
1058 h.loc.count = MAXKEYCODE;
1059 if (h.loc.pos % 8 != 0)
1060 return ("key codes not on byte boundary");
1061 if (sc->sc_nkeycode != 0)
1062 return ("multiple key code arrays\n");
1063 sc->sc_keycodeloc = h.loc;
1064 sc->sc_nkeycode = h.loc.count;
1065 }
1066 }
1067 sc->sc_nmod = imod;
1068 hid_end_parse(d);
1069
1070 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
1071 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL);
1072 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
1073 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL);
1074 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
1075 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL);
1076
1077 return (NULL);
1078 }
1079