ukbd.c revision 1.145 1 /* $NetBSD: ukbd.c,v 1.145 2020/03/14 02:35:33 christos 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.145 2020/03/14 02:35:33 christos Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_ddb.h"
42 #include "opt_ukbd.h"
43 #include "opt_ukbd_layout.h"
44 #include "opt_usb.h"
45 #include "opt_wsdisplay_compat.h"
46 #endif /* _KERNEL_OPT */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/callout.h>
51 #include <sys/kernel.h>
52 #include <sys/device.h>
53 #include <sys/ioctl.h>
54 #include <sys/file.h>
55 #include <sys/select.h>
56 #include <sys/proc.h>
57 #include <sys/vnode.h>
58 #include <sys/poll.h>
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbhid.h>
62
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdi_util.h>
65 #include <dev/usb/usbdivar.h>
66 #include <dev/usb/usbdevs.h>
67 #include <dev/usb/usb_quirks.h>
68 #include <dev/usb/uhidev.h>
69 #include <dev/usb/ukbdvar.h>
70 #include <dev/hid/hid.h>
71
72 #include <dev/wscons/wsconsio.h>
73 #include <dev/wscons/wskbdvar.h>
74 #include <dev/wscons/wsksymdef.h>
75 #include <dev/wscons/wsksymvar.h>
76
77 #ifdef UKBD_DEBUG
78 #define DPRINTF(x) if (ukbddebug) printf x
79 #define DPRINTFN(n,x) if (ukbddebug>(n)) printf x
80 int ukbddebug = 0;
81 #else
82 #define DPRINTF(x)
83 #define DPRINTFN(n,x)
84 #endif
85
86 #define MAXKEYCODE 32
87 #define MAXKEYS 256
88
89 struct ukbd_data {
90 uint8_t keys[MAXKEYS/NBBY];
91 };
92
93 #define PRESS 0x000
94 #define RELEASE 0x100
95 #define CODEMASK 0x0ff
96
97 struct ukbd_keycodetrans {
98 uint16_t from;
99 uint16_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, IS_PMF | PMFE_DISPLAY_BRIGHTNESS_DOWN },
125 { 0x3b, IS_PMF | PMFE_DISPLAY_BRIGHTNESS_UP },
126 { 0x3c, IS_PMF | PMFE_AUDIO_VOLUME_TOGGLE },
127 { 0x3d, IS_PMF | PMFE_AUDIO_VOLUME_DOWN },
128 { 0x3e, IS_PMF | PMFE_AUDIO_VOLUME_UP },
129 { 0x3f, 0xd6 }, /* num lock */
130 { 0x40, 0xd7 },
131 { 0x41, 0xd8 },
132 { 0x42, 0xd9 }, /* kbd light down */
133 { 0x43, 0xda }, /* kbd light up */
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 Static const struct ukbd_keycodetrans trtab_generic[] = {
179 { 0x7f, IS_PMF | PMFE_AUDIO_VOLUME_TOGGLE },
180 { 0x80, IS_PMF | PMFE_AUDIO_VOLUME_UP },
181 { 0x81, IS_PMF | PMFE_AUDIO_VOLUME_DOWN },
182 { 0x00, 0x00 }
183 };
184
185 #if defined(WSDISPLAY_COMPAT_RAWKBD)
186 #define NN 0 /* no translation */
187 /*
188 * Translate USB keycodes to US keyboard XT scancodes.
189 * Scancodes >= 0x80 represent EXTENDED keycodes.
190 *
191 * See http://www.microsoft.com/whdc/archive/scancode.mspx
192 *
193 * Note: a real pckbd(4) has more complexity in its
194 * protocol for some keys than this translation implements.
195 * For example, some keys generate Fake ShiftL events (e0 2a)
196 * before the actual key sequence.
197 */
198 Static const uint8_t ukbd_trtab[256] = {
199 NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
200 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
201 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
202 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
203 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
204 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
205 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
206 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
207 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xb7, 0x46, /* 40 - 47 */
208 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
209 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
210 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
211 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, 0xdf, 0x59, /* 60 - 67 */
212 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, NN, /* 68 - 6f */
213 NN, NN, NN, NN, 0x84, 0x85, 0x87, 0x88, /* 70 - 77 */
214 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, NN, /* 78 - 7f */
215 NN, NN, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */
216 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */
217 NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */
218 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */
219 NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */
220 NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */
221 NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */
222 NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */
223 NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */
224 NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */
225 NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */
226 NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */
227 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
228 NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */
229 NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */
230 NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */
231 };
232 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
233
234 #define KEY_ERROR 0x01
235
236 struct ukbd_softc {
237 struct uhidev sc_hdev;
238
239 struct ukbd_data sc_ndata;
240 struct ukbd_data sc_odata;
241 struct hid_location sc_keyloc[MAXKEYS];
242 uint8_t sc_keyuse[MAXKEYS];
243 u_int sc_nkeyloc;
244
245 struct hid_location sc_keycodeloc;
246 u_int sc_nkeycode;
247
248 u_int sc_flags; /* flags */
249 #define FLAG_ENABLED 0x0001
250 #define FLAG_POLLING 0x0002
251 #define FLAG_DEBOUNCE 0x0004 /* for quirk handling */
252 #define FLAG_APPLE_FIX_ISO 0x0008
253 #define FLAG_APPLE_FN 0x0010
254 #define FLAG_GDIUM_FN 0x0020
255 #define FLAG_FN_PRESSED 0x0100 /* FN key is held down */
256 #define FLAG_FN_ALT 0x0200 /* Last Alt key was FN-Alt = AltGr */
257
258 int sc_console_keyboard; /* we are the console keyboard */
259
260 struct callout sc_delay; /* for quirk handling */
261 struct ukbd_data sc_data; /* for quirk handling */
262
263 struct hid_location sc_apple_fn;
264 struct hid_location sc_numloc;
265 struct hid_location sc_capsloc;
266 struct hid_location sc_scroloc;
267 struct hid_location sc_compose;
268 int sc_leds;
269 struct usb_task sc_ledtask;
270 device_t sc_wskbddev;
271
272 #if defined(WSDISPLAY_COMPAT_RAWKBD)
273 int sc_rawkbd;
274 #if defined(UKBD_REPEAT)
275 struct callout sc_rawrepeat_ch;
276 #define REP_DELAY1 400
277 #define REP_DELAYN 100
278 int sc_nrep;
279 char sc_rep[MAXKEYS];
280 #endif /* defined(UKBD_REPEAT) */
281 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
282
283 int sc_spl;
284 int sc_npollchar;
285 uint16_t sc_pollchars[MAXKEYS];
286
287 u_char sc_dying;
288 };
289
290 #ifdef UKBD_DEBUG
291 #define UKBDTRACESIZE 64
292 struct ukbdtraceinfo {
293 int unit;
294 struct timeval tv;
295 struct ukbd_data ud;
296 };
297 struct ukbdtraceinfo ukbdtracedata[UKBDTRACESIZE];
298 int ukbdtraceindex = 0;
299 int ukbdtrace = 0;
300 void ukbdtracedump(void);
301 void
302 ukbdtracedump(void)
303 {
304 size_t i, j;
305 for (i = 0; i < UKBDTRACESIZE; i++) {
306 struct ukbdtraceinfo *p =
307 &ukbdtracedata[(i+ukbdtraceindex)%UKBDTRACESIZE];
308 printf("%"PRIu64".%06"PRIu64":", p->tv.tv_sec,
309 (uint64_t)p->tv.tv_usec);
310 for (j = 0; j < MAXKEYS; j++) {
311 if (isset(p->ud.keys, j))
312 printf(" %zu", j);
313 }
314 printf(".\n");
315 }
316 }
317 #endif
318
319 #define UKBDUNIT(dev) (minor(dev))
320 #define UKBD_CHUNK 128 /* chunk size for read */
321 #define UKBD_BSIZE 1020 /* buffer size */
322
323 Static int ukbd_is_console;
324
325 Static void ukbd_cngetc(void *, u_int *, int *);
326 Static void ukbd_cnpollc(void *, int);
327
328 const struct wskbd_consops ukbd_consops = {
329 .getc = ukbd_cngetc,
330 .pollc = ukbd_cnpollc,
331 .bell = NULL,
332 };
333
334 Static const char *ukbd_parse_desc(struct ukbd_softc *);
335
336 Static void ukbd_intr(struct uhidev *, void *, u_int);
337 Static void ukbd_decode(struct ukbd_softc *, struct ukbd_data *);
338 Static void ukbd_delayed_decode(void *);
339
340 Static int ukbd_enable(void *, int);
341 Static void ukbd_set_leds(void *, int);
342 Static void ukbd_set_leds_task(void *);
343
344 Static int ukbd_ioctl(void *, u_long, void *, int, struct lwp *);
345 #if defined(WSDISPLAY_COMPAT_RAWKBD) && defined(UKBD_REPEAT)
346 Static void ukbd_rawrepeat(void *v);
347 #endif
348
349 const struct wskbd_accessops ukbd_accessops = {
350 ukbd_enable,
351 ukbd_set_leds,
352 ukbd_ioctl,
353 };
354
355 extern const struct wscons_keydesc hidkbd_keydesctab[];
356
357 const struct wskbd_mapdata ukbd_keymapdata = {
358 hidkbd_keydesctab,
359 #if defined(UKBD_LAYOUT)
360 UKBD_LAYOUT,
361 #elif defined(PCKBD_LAYOUT)
362 PCKBD_LAYOUT,
363 #else
364 KB_US,
365 #endif
366 };
367
368 static int ukbd_match(device_t, cfdata_t, void *);
369 static void ukbd_attach(device_t, device_t, void *);
370 static int ukbd_detach(device_t, int);
371 static int ukbd_activate(device_t, enum devact);
372 static void ukbd_childdet(device_t, device_t);
373
374
375
376 CFATTACH_DECL2_NEW(ukbd, sizeof(struct ukbd_softc), ukbd_match, ukbd_attach,
377 ukbd_detach, ukbd_activate, NULL, ukbd_childdet);
378
379 int
380 ukbd_match(device_t parent, cfdata_t match, void *aux)
381 {
382 struct uhidev_attach_arg *uha = aux;
383 int size;
384 void *desc;
385
386 uhidev_get_report_desc(uha->parent, &desc, &size);
387 if (!hid_is_collection(desc, size, uha->reportid,
388 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
389 return UMATCH_NONE;
390
391 return UMATCH_IFACECLASS;
392 }
393
394 void
395 ukbd_attach(device_t parent, device_t self, void *aux)
396 {
397 struct ukbd_softc *sc = device_private(self);
398 struct uhidev_attach_arg *uha = aux;
399 uint32_t qflags;
400 const char *parseerr;
401 struct wskbddev_attach_args a;
402
403 sc->sc_hdev.sc_dev = self;
404 sc->sc_hdev.sc_intr = ukbd_intr;
405 sc->sc_hdev.sc_parent = uha->parent;
406 sc->sc_hdev.sc_report_id = uha->reportid;
407 sc->sc_flags = 0;
408
409 aprint_naive("\n");
410
411 if (!pmf_device_register(self, NULL, NULL)) {
412 aprint_normal("\n");
413 aprint_error_dev(self, "couldn't establish power handler\n");
414 }
415
416 parseerr = ukbd_parse_desc(sc);
417 if (parseerr != NULL) {
418 aprint_normal("\n");
419 aprint_error_dev(self, "attach failed, %s\n", parseerr);
420 return;
421 }
422
423 /* Quirks */
424 qflags = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
425 if (qflags & UQ_SPUR_BUT_UP)
426 sc->sc_flags |= FLAG_DEBOUNCE;
427 if (qflags & UQ_APPLE_ISO)
428 sc->sc_flags |= FLAG_APPLE_FIX_ISO;
429
430 #ifdef GDIUM_KEYBOARD_HACK
431 if (uha->uiaa->uiaa_vendor == USB_VENDOR_CYPRESS &&
432 uha->uiaa->uiaa_product == USB_PRODUCT_CYPRESS_LPRDK)
433 sc->sc_flags = FLAG_GDIUM_FN;
434 #endif
435
436 #ifdef DIAGNOSTIC
437 aprint_normal(": %d Variable keys, %d Array codes", sc->sc_nkeyloc,
438 sc->sc_nkeycode);
439 if (sc->sc_flags & FLAG_APPLE_FN)
440 aprint_normal(", apple fn key");
441 if (sc->sc_flags & FLAG_APPLE_FIX_ISO)
442 aprint_normal(", fix apple iso");
443 if (sc->sc_flags & FLAG_GDIUM_FN)
444 aprint_normal(", Gdium fn key");
445 #endif
446 aprint_normal("\n");
447
448 /*
449 * Remember if we're the console keyboard.
450 *
451 * XXX This always picks the first keyboard on the
452 * first USB bus, but what else can we really do?
453 */
454 if ((sc->sc_console_keyboard = ukbd_is_console) != 0) {
455 /* Don't let any other keyboard have it. */
456 ukbd_is_console = 0;
457 }
458
459 if (sc->sc_console_keyboard) {
460 DPRINTF(("%s: console keyboard sc=%p\n", __func__, sc));
461 wskbd_cnattach(&ukbd_consops, sc, &ukbd_keymapdata);
462 ukbd_enable(sc, 1);
463 }
464
465 a.console = sc->sc_console_keyboard;
466
467 a.keymap = &ukbd_keymapdata;
468
469 a.accessops = &ukbd_accessops;
470 a.accesscookie = sc;
471
472 #ifdef UKBD_REPEAT
473 callout_init(&sc->sc_rawrepeat_ch, 0);
474 #endif
475
476 callout_init(&sc->sc_delay, 0);
477
478 usb_init_task(&sc->sc_ledtask, ukbd_set_leds_task, sc, 0);
479
480 /* Flash the leds; no real purpose, just shows we're alive. */
481 ukbd_set_leds(sc, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS
482 | WSKBD_LED_COMPOSE);
483 usbd_delay_ms(uha->parent->sc_udev, 400);
484 ukbd_set_leds(sc, 0);
485
486 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
487
488 return;
489 }
490
491 int
492 ukbd_enable(void *v, int on)
493 {
494 struct ukbd_softc *sc = v;
495
496 if (on && sc->sc_dying)
497 return EIO;
498
499 /* Should only be called to change state */
500 if ((sc->sc_flags & FLAG_ENABLED) != 0 && on != 0) {
501 #ifdef DIAGNOSTIC
502 aprint_error_dev(sc->sc_hdev.sc_dev, "bad call on=%d\n", on);
503 #endif
504 return EBUSY;
505 }
506
507 DPRINTF(("%s: sc=%p on=%d\n", __func__, sc, on));
508 if (on) {
509 sc->sc_flags |= FLAG_ENABLED;
510 return uhidev_open(&sc->sc_hdev);
511 } else {
512 sc->sc_flags &= ~FLAG_ENABLED;
513 uhidev_close(&sc->sc_hdev);
514 return 0;
515 }
516 }
517
518
519 static void
520 ukbd_childdet(device_t self, device_t child)
521 {
522 struct ukbd_softc *sc = device_private(self);
523
524 KASSERT(sc->sc_wskbddev == child);
525 sc->sc_wskbddev = NULL;
526 }
527
528 int
529 ukbd_activate(device_t self, enum devact act)
530 {
531 struct ukbd_softc *sc = device_private(self);
532
533 switch (act) {
534 case DVACT_DEACTIVATE:
535 sc->sc_dying = 1;
536 return 0;
537 default:
538 return EOPNOTSUPP;
539 }
540 }
541
542 int
543 ukbd_detach(device_t self, int flags)
544 {
545 struct ukbd_softc *sc = device_private(self);
546 int rv = 0;
547
548 DPRINTF(("%s: sc=%p flags=%d\n", __func__, sc, flags));
549
550 pmf_device_deregister(self);
551
552 if (sc->sc_console_keyboard) {
553 #if 0
554 /*
555 * XXX Should probably disconnect our consops,
556 * XXX and either notify some other keyboard that
557 * XXX it can now be the console, or if there aren't
558 * XXX any more USB keyboards, set ukbd_is_console
559 * XXX back to 1 so that the next USB keyboard attached
560 * XXX to the system will get it.
561 */
562 panic("ukbd_detach: console keyboard");
563 #else
564 /*
565 * Disconnect our consops and set ukbd_is_console
566 * back to 1 so that the next USB keyboard attached
567 * to the system will get it.
568 * XXX Should notify some other keyboard that it can be
569 * XXX console, if there are any other keyboards.
570 */
571 printf("%s: was console keyboard\n",
572 device_xname(sc->sc_hdev.sc_dev));
573 wskbd_cndetach();
574 ukbd_is_console = 1;
575 #endif
576 }
577 /* No need to do reference counting of ukbd, wskbd has all the goo. */
578 if (sc->sc_wskbddev != NULL)
579 rv = config_detach(sc->sc_wskbddev, flags);
580
581 /* The console keyboard does not get a disable call, so check pipe. */
582 if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
583 uhidev_close(&sc->sc_hdev);
584
585 return rv;
586 }
587
588 static void
589 ukbd_translate_keycodes(struct ukbd_softc *sc, struct ukbd_data *ud,
590 const struct ukbd_keycodetrans *tab)
591 {
592 const struct ukbd_keycodetrans *tp;
593 struct ukbd_data oud;
594 int i;
595
596 oud = *ud;
597
598 for (i = 4; i < MAXKEYS; i++) {
599 if (isset(oud.keys, i))
600 for (tp = tab; tp->from; tp++)
601 if (tp->from == i) {
602 if (tp->to & IS_PMF) {
603 pmf_event_inject(
604 sc->sc_hdev.sc_dev,
605 tp->to & 0xff);
606 } else
607 setbit(ud->keys, tp->to);
608 clrbit(ud->keys, i);
609 break;
610 }
611 }
612 }
613
614 static uint16_t
615 ukbd_translate_modifier(struct ukbd_softc *sc, uint16_t key)
616 {
617 if ((sc->sc_flags & FLAG_APPLE_FN) && (key & CODEMASK) == 0x00e2) {
618 if ((key & ~CODEMASK) == PRESS) {
619 if (sc->sc_flags & FLAG_FN_PRESSED) {
620 /* pressed FN-Alt, translate to AltGr */
621 key = 0x00e6 | PRESS;
622 sc->sc_flags |= FLAG_FN_ALT;
623 }
624 } else {
625 if (sc->sc_flags & FLAG_FN_ALT) {
626 /* released Alt, which was treated as FN-Alt */
627 key = 0x00e6 | RELEASE;
628 sc->sc_flags &= ~FLAG_FN_ALT;
629 }
630 }
631 }
632 return key;
633 }
634
635 void
636 ukbd_intr(struct uhidev *addr, void *ibuf, u_int len)
637 {
638 struct ukbd_softc *sc = (struct ukbd_softc *)addr;
639 struct ukbd_data *ud = &sc->sc_ndata;
640 int i;
641
642 #ifdef UKBD_DEBUG
643 if (ukbddebug > 5) {
644 printf("ukbd_intr: data");
645 for (i = 0; i < len; i++)
646 printf(" 0x%02x", ((u_char *)ibuf)[i]);
647 printf("\n");
648 }
649 #endif
650
651 memset(ud->keys, 0, sizeof(ud->keys));
652
653 for (i = 0; i < sc->sc_nkeyloc; i++)
654 if (hid_get_data(ibuf, &sc->sc_keyloc[i]))
655 setbit(ud->keys, sc->sc_keyuse[i]);
656
657 const uint8_t * const scancode = (char *)ibuf + sc->sc_keycodeloc.pos / 8;
658 const uint16_t Keyboard_NoEvent = 0x0000;
659 for (i = 0; i < sc->sc_nkeycode; i++) {
660 if (scancode[i] != Keyboard_NoEvent)
661 setbit(ud->keys, scancode[i]);
662 }
663
664 if (sc->sc_flags & FLAG_APPLE_FN) {
665 if (hid_get_data(ibuf, &sc->sc_apple_fn)) {
666 sc->sc_flags |= FLAG_FN_PRESSED;
667 ukbd_translate_keycodes(sc, ud, trtab_apple_fn);
668 }
669 else
670 sc->sc_flags &= ~FLAG_FN_PRESSED;
671 }
672
673 #ifdef GDIUM_KEYBOARD_HACK
674 if (sc->sc_flags & FLAG_GDIUM_FN) {
675 if (sc->sc_flags & FLAG_FN_PRESSED) {
676 ukbd_translate_keycodes(sc, ud, trtab_gdium_fn);
677 }
678 }
679 #endif
680
681 ukbd_translate_keycodes(sc, ud, trtab_generic);
682
683 if ((sc->sc_flags & FLAG_DEBOUNCE) && !(sc->sc_flags & FLAG_POLLING)) {
684 /*
685 * Some keyboards have a peculiar quirk. They sometimes
686 * generate a key up followed by a key down for the same
687 * key after about 10 ms.
688 * We avoid this bug by holding off decoding for 20 ms.
689 */
690 sc->sc_data = *ud;
691 callout_reset(&sc->sc_delay, hz / 50, ukbd_delayed_decode, sc);
692 #ifdef DDB
693 } else if (sc->sc_console_keyboard && !(sc->sc_flags & FLAG_POLLING)) {
694 /*
695 * For the console keyboard we can't deliver CTL-ALT-ESC
696 * from the interrupt routine. Doing so would start
697 * polling from inside the interrupt routine and that
698 * loses bigtime.
699 */
700 sc->sc_data = *ud;
701 callout_reset(&sc->sc_delay, 1, ukbd_delayed_decode, sc);
702 #endif
703 } else {
704 ukbd_decode(sc, ud);
705 }
706 }
707
708 void
709 ukbd_delayed_decode(void *addr)
710 {
711 struct ukbd_softc *sc = addr;
712
713 ukbd_decode(sc, &sc->sc_data);
714 }
715
716 void
717 ukbd_decode(struct ukbd_softc *sc, struct ukbd_data *ud)
718 {
719 uint16_t ibuf[MAXKEYS]; /* chars events */
720 int s;
721 int nkeys, i;
722 #ifdef WSDISPLAY_COMPAT_RAWKBD
723 int j;
724 #endif
725 int key;
726 #define ADDKEY(c) do { \
727 KASSERT(nkeys < MAXKEYS); \
728 ibuf[nkeys++] = (c); \
729 } while (0)
730
731 #ifdef UKBD_DEBUG
732 /*
733 * Keep a trace of the last events. Using printf changes the
734 * timing, so this can be useful sometimes.
735 */
736 if (ukbdtrace) {
737 struct ukbdtraceinfo *p = &ukbdtracedata[ukbdtraceindex];
738 p->unit = device_unit(sc->sc_hdev.sc_dev);
739 microtime(&p->tv);
740 p->ud = *ud;
741 if (++ukbdtraceindex >= UKBDTRACESIZE)
742 ukbdtraceindex = 0;
743 }
744 if (ukbddebug > 5) {
745 struct timeval tv;
746 microtime(&tv);
747 DPRINTF((" at %"PRIu64".%06"PRIu64":", tv.tv_sec,
748 (uint64_t)tv.tv_usec));
749 for (size_t k = 0; k < MAXKEYS; k++) {
750 if (isset(ud->keys, k))
751 DPRINTF((" %zu", k));
752 }
753 DPRINTF((".\n"));
754 }
755 #endif
756
757 if (isset(ud->keys, KEY_ERROR)) {
758 DPRINTF(("%s: KEY_ERROR\n", __func__));
759 return; /* ignore */
760 }
761
762 if (sc->sc_flags & FLAG_APPLE_FIX_ISO)
763 ukbd_translate_keycodes(sc, ud, trtab_apple_iso);
764
765 nkeys = 0;
766 for (i = 0; i < MAXKEYS; i++) {
767 #ifdef GDIUM_KEYBOARD_HACK
768 if (sc->sc_flags & FLAG_GDIUM_FN && i == 0x82) {
769 if (isset(ud->keys, i))
770 sc->sc_flags |= FLAG_FN_PRESSED;
771 else
772 sc->sc_flags &= ~FLAG_FN_PRESSED;
773 }
774 #endif
775 if (isset(ud->keys, i) != isset(sc->sc_odata.keys, i)) {
776 key = i | ((isset(ud->keys, i) ? PRESS : RELEASE));
777 ADDKEY(ukbd_translate_modifier(sc, key));
778 }
779 }
780 sc->sc_odata = *ud;
781
782 if (nkeys == 0)
783 return;
784
785 if (sc->sc_flags & FLAG_POLLING) {
786 DPRINTFN(1,("%s: pollchar = 0x%03x\n", __func__, ibuf[0]));
787 memcpy(sc->sc_pollchars, ibuf, nkeys * sizeof(uint16_t));
788 sc->sc_npollchar = nkeys;
789 return;
790 }
791 #ifdef WSDISPLAY_COMPAT_RAWKBD
792 if (sc->sc_rawkbd) {
793 u_char cbuf[MAXKEYS * 2];
794 int c;
795 #if defined(UKBD_REPEAT)
796 int npress = 0;
797 #endif
798
799 for (i = j = 0; i < nkeys; i++) {
800 key = ibuf[i];
801 c = ukbd_trtab[key & CODEMASK];
802 if (c == NN)
803 continue;
804 if (c == 0x7f) {
805 /* pause key */
806 cbuf[j++] = 0xe1;
807 cbuf[j++] = 0x1d;
808 cbuf[j-1] |= (key & RELEASE) ? 0x80 : 0;
809 cbuf[j] = 0x45;
810 } else {
811 if (c & 0x80)
812 cbuf[j++] = 0xe0;
813 cbuf[j] = c & 0x7f;
814 }
815 if (key & RELEASE)
816 cbuf[j] |= 0x80;
817 #if defined(UKBD_REPEAT)
818 else {
819 /* remember pressed keys for autorepeat */
820 if (c & 0x80)
821 sc->sc_rep[npress++] = 0xe0;
822 sc->sc_rep[npress++] = c & 0x7f;
823 }
824 #endif
825 DPRINTFN(1,("%s: raw = %s0x%02x\n", __func__,
826 c & 0x80 ? "0xe0 " : "",
827 cbuf[j]));
828 j++;
829 }
830 s = spltty();
831 wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
832 splx(s);
833 #ifdef UKBD_REPEAT
834 callout_stop(&sc->sc_rawrepeat_ch);
835 if (npress != 0) {
836 sc->sc_nrep = npress;
837 callout_reset(&sc->sc_rawrepeat_ch,
838 hz * REP_DELAY1 / 1000, ukbd_rawrepeat, sc);
839 }
840 #endif
841 return;
842 }
843 #endif
844
845 s = spltty();
846 for (i = 0; i < nkeys; i++) {
847 key = ibuf[i];
848 wskbd_input(sc->sc_wskbddev,
849 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
850 key&CODEMASK);
851 }
852 splx(s);
853 }
854
855 void
856 ukbd_set_leds(void *v, int leds)
857 {
858 struct ukbd_softc *sc = v;
859 struct usbd_device *udev = sc->sc_hdev.sc_parent->sc_udev;
860
861 DPRINTF(("%s: sc=%p leds=%d, sc_leds=%d\n", __func__,
862 sc, leds, sc->sc_leds));
863
864 if (sc->sc_dying)
865 return;
866
867 if (sc->sc_leds == leds)
868 return;
869
870 sc->sc_leds = leds;
871 usb_add_task(udev, &sc->sc_ledtask, USB_TASKQ_DRIVER);
872 }
873
874 void
875 ukbd_set_leds_task(void *v)
876 {
877 struct ukbd_softc *sc = v;
878 int leds = sc->sc_leds;
879 uint8_t res = 0;
880
881 /* XXX not really right */
882 if ((leds & WSKBD_LED_COMPOSE) && sc->sc_compose.size == 1)
883 res |= 1 << sc->sc_compose.pos;
884 if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
885 res |= 1 << sc->sc_scroloc.pos;
886 if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
887 res |= 1 << sc->sc_numloc.pos;
888 if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
889 res |= 1 << sc->sc_capsloc.pos;
890
891 uhidev_set_report(&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(("%s: set raw = %d\n", __func__, *(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,("%s: enter\n", __func__));
965 sc->sc_flags |= FLAG_POLLING;
966 if (sc->sc_npollchar <= 0)
967 usbd_dopoll(sc->sc_hdev.sc_parent->sc_iface);
968 sc->sc_flags &= ~FLAG_POLLING;
969 if (sc->sc_npollchar > 0) {
970 c = sc->sc_pollchars[0];
971 sc->sc_npollchar--;
972 memmove(sc->sc_pollchars, sc->sc_pollchars+1,
973 sc->sc_npollchar * sizeof(uint16_t));
974 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
975 *data = c & CODEMASK;
976 DPRINTFN(0,("%s: return 0x%02x\n", __func__, c));
977 } else {
978 *type = 0;
979 *data = 0;
980 }
981 if (broken)
982 ukbd_cnpollc(v, 0);
983 }
984
985 void
986 ukbd_cnpollc(void *v, int on)
987 {
988 struct ukbd_softc *sc = v;
989 struct usbd_device *dev;
990
991 DPRINTFN(2,("%s: sc=%p on=%d\n", __func__, v, on));
992
993 usbd_interface2device_handle(sc->sc_hdev.sc_parent->sc_iface, &dev);
994 if (on) {
995 sc->sc_spl = splusb();
996 pollenter++;
997 } else {
998 splx(sc->sc_spl);
999 pollenter--;
1000 }
1001 usbd_set_polling(dev, on);
1002 }
1003
1004 int
1005 ukbd_cnattach(void)
1006 {
1007
1008 /*
1009 * XXX USB requires too many parts of the kernel to be running
1010 * XXX in order to work, so we can't do much for the console
1011 * XXX keyboard until autconfiguration has run its course.
1012 */
1013 ukbd_is_console = 1;
1014 return 0;
1015 }
1016
1017 const char *
1018 ukbd_parse_desc(struct ukbd_softc *sc)
1019 {
1020 struct hid_data *d;
1021 struct hid_item h;
1022 int size;
1023 void *desc;
1024 int ikey;
1025
1026 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
1027 ikey = 0;
1028 sc->sc_nkeycode = 0;
1029 d = hid_start_parse(desc, size, hid_input);
1030 while (hid_get_item(d, &h)) {
1031 /*printf("ukbd: id=%d kind=%d usage=%#x flags=%#x pos=%d size=%d cnt=%d\n",
1032 h.report_ID, h.kind, h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count);*/
1033
1034 /* Check for special Apple notebook FN key */
1035 if (HID_GET_USAGE_PAGE(h.usage) == 0x00ff &&
1036 HID_GET_USAGE(h.usage) == 0x0003 &&
1037 h.kind == hid_input && (h.flags & HIO_VARIABLE)) {
1038 sc->sc_flags |= FLAG_APPLE_FN;
1039 sc->sc_apple_fn = h.loc;
1040 }
1041
1042 if (h.kind != hid_input || (h.flags & HIO_CONST) ||
1043 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
1044 h.report_ID != sc->sc_hdev.sc_report_id)
1045 continue;
1046 DPRINTF(("%s: ikey=%d usage=%#x flags=%#x pos=%d size=%d "
1047 "cnt=%d\n", __func__, ikey, h.usage, h.flags, h.loc.pos,
1048 h.loc.size, h.loc.count));
1049 if (h.flags & HIO_VARIABLE) {
1050 if (h.loc.size != 1) {
1051 hid_end_parse(d);
1052 return "bad modifier size";
1053 }
1054 /* Single item */
1055 if (ikey < MAXKEYS) {
1056 sc->sc_keyloc[ikey] = h.loc;
1057 sc->sc_keyuse[ikey] = HID_GET_USAGE(h.usage);
1058 ikey++;
1059 } else {
1060 hid_end_parse(d);
1061 return "too many Variable keys";
1062 }
1063 } else {
1064 /* Array */
1065 if (h.loc.size != 8) {
1066 hid_end_parse(d);
1067 return "key code size != 8";
1068 }
1069 if (h.loc.count > MAXKEYCODE)
1070 h.loc.count = MAXKEYCODE;
1071 if (h.loc.pos % 8 != 0) {
1072 hid_end_parse(d);
1073 return "key codes not on byte boundary";
1074 }
1075 if (sc->sc_nkeycode != 0) {
1076 hid_end_parse(d);
1077 return "multiple key code arrays";
1078 }
1079 sc->sc_keycodeloc = h.loc;
1080 sc->sc_nkeycode = h.loc.count;
1081 }
1082 }
1083 sc->sc_nkeyloc = ikey;
1084 hid_end_parse(d);
1085
1086 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
1087 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_numloc, NULL);
1088 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
1089 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_capsloc, NULL);
1090 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
1091 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_scroloc, NULL);
1092 hid_locate(desc, size, HID_USAGE2(HUP_LEDS, HUD_LED_COMPOSE),
1093 sc->sc_hdev.sc_report_id, hid_output, &sc->sc_compose, NULL);
1094
1095 return NULL;
1096 }
1097