FreeBSD/Linux Kernel Cross Reference
sys/dev/hid/hidkbd.c
1 /* $OpenBSD: hidkbd.c,v 1.8 2022/11/09 10:05:18 robert Exp $ */
2 /* $NetBSD: ukbd.c,v 1.85 2003/03/11 16:44:00 augustss Exp $ */
3
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/timeout.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/ioctl.h>
44 #include <sys/malloc.h>
45
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wskbdvar.h>
48 #include <dev/wscons/wsksymdef.h>
49 #include <dev/wscons/wsksymvar.h>
50
51 #include <dev/hid/hid.h>
52 #include <dev/hid/hidkbdsc.h>
53
54 #ifdef HIDKBD_DEBUG
55 #define DPRINTF(x) do { if (hidkbddebug) printf x; } while (0)
56 #define DPRINTFN(n,x) do { if (hidkbddebug>(n)) printf x; } while (0)
57 int hidkbddebug = 0;
58 #else
59 #define DPRINTF(x)
60 #define DPRINTFN(n,x)
61 #endif
62
63 #define PRESS 0x000
64 #define RELEASE 0x100
65 #define CODEMASK 0x0ff
66
67 #if defined(WSDISPLAY_COMPAT_RAWKBD)
68 #define NN 0 /* no translation */
69 /*
70 * Translate USB keycodes to US keyboard XT scancodes.
71 * Scancodes >= 0x80 represent EXTENDED keycodes.
72 *
73 * See http://www.microsoft.com/whdc/archive/Scancode.mspx
74 */
75 const u_int8_t hidkbd_trtab[256] = {
76 NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
77 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
78 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
79 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
80 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
81 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
82 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
83 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
84 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */
85 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
86 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
87 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
88 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, 0xde, 0x59, /* 60 - 67 */
89 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, /* 68 - 6f */
90 0x6c, 0x6d, 0x6e, 0x76, 0x97, NN, 0x93, 0x95, /* 70 - 77 */
91 0x91, 0x92, 0x94, 0x9a, 0x96, 0x98, 0x99, 0xa0, /* 78 - 7f */
92 0xb0, 0xae, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */
93 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */
94 NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */
95 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */
96 NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */
97 NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */
98 NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */
99 NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */
100 NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */
101 NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */
102 NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */
103 NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */
104 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
105 NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */
106 NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */
107 NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */
108 };
109 #endif /* defined(WSDISPLAY_COMPAT_RAWKBD) */
110
111 static const struct hidkbd_translation apple_tb_trans[] = {
112 { 30, 58 }, /* 1 -> F1 */
113 { 31, 59 }, /* 2 -> F2 */
114 { 32, 60 }, /* 3 -> F3 */
115 { 33, 61 }, /* 4 -> F4 */
116 { 34, 62 }, /* 5 -> F5 */
117 { 35, 63 }, /* 6 -> F6 */
118 { 36, 64 }, /* 7 -> F7 */
119 { 37, 65 }, /* 8 -> F8 */
120 { 38, 66 }, /* 9 -> F9 */
121 { 39, 67 }, /* 0 -> F10 */
122 { 45, 68 }, /* - -> F11 */
123 { 46, 69 } /* = -> F12 */
124 };
125
126 static const struct hidkbd_translation apple_fn_trans[] = {
127 { 40, 73 }, /* return -> insert */
128 { 42, 76 }, /* backspace -> delete */
129 #ifdef notyet
130 { 58, 0 }, /* F1 -> screen brightness down */
131 { 59, 0 }, /* F2 -> screen brightness up */
132 { 60, 0 }, /* F3 */
133 { 61, 0 }, /* F4 */
134 { 62, 0 }, /* F5 -> keyboard backlight down */
135 { 63, 0 }, /* F6 -> keyboard backlight up */
136 { 64, 0 }, /* F7 -> audio back */
137 { 65, 0 }, /* F8 -> audio pause/play */
138 { 66, 0 }, /* F9 -> audio next */
139 #endif
140 #ifdef __macppc__
141 { 58, 233 }, /* F1 -> screen brightness down */
142 { 59, 232 }, /* F2 -> screen brightness up */
143 { 60, 127 }, /* F3 -> audio mute */
144 { 61, 129 }, /* F4 -> audio lower */
145 { 62, 128 }, /* F5 -> audio raise */
146 #else
147 { 67, 127 }, /* F10 -> audio mute */
148 { 68, 129 }, /* F11 -> audio lower */
149 { 69, 128 }, /* F12 -> audio raise */
150 #endif
151 { 79, 77 }, /* right -> end */
152 { 80, 74 }, /* left -> home */
153 { 81, 78 }, /* down -> page down */
154 { 82, 75 } /* up -> page up */
155 };
156
157 static const struct hidkbd_translation apple_mba_trans[] = {
158 { 40, 73 }, /* return -> insert */
159 { 42, 76 }, /* backspace -> delete */
160 #ifdef notyet
161 { 58, 0 }, /* F1 -> screen brightness down */
162 { 59, 0 }, /* F2 -> screen brightness up */
163 { 60, 0 }, /* F3 */
164 { 61, 0 }, /* F4 */
165 { 62, 0 }, /* F5 */
166 { 63, 0 }, /* F6 -> audio back */
167 { 64, 0 }, /* F7 -> audio pause/play */
168 { 65, 0 }, /* F8 -> audio next */
169 #endif
170 { 66, 127 }, /* F9 -> audio mute */
171 { 67, 129 }, /* F10 -> audio lower */
172 { 68, 128 }, /* F11 -> audio raise */
173 #ifdef notyet
174 { 69, 0 }, /* F12 -> eject */
175 #endif
176 { 79, 77 }, /* right -> end */
177 { 80, 74 }, /* left -> home */
178 { 81, 78 }, /* down -> page down */
179 { 82, 75 } /* up -> page up */
180 };
181
182 static const struct hidkbd_translation apple_iso_trans[] = {
183 { 53, 100 }, /* less -> grave */
184 { 100, 53 }
185 };
186
187 #define KEY_ERROR 0x01
188
189 #ifdef HIDKBD_DEBUG
190 #define HIDKBDTRACESIZE 64
191 struct hidkbdtraceinfo {
192 int unit;
193 struct timeval tv;
194 struct hidkbd_data ud;
195 };
196 struct hidkbdtraceinfo hidkbdtracedata[HIDKBDTRACESIZE];
197 int hidkbdtraceindex = 0;
198 int hidkbdtrace = 0;
199 void hidkbdtracedump(void);
200 void
201 hidkbdtracedump(void)
202 {
203 int i;
204 for (i = 0; i < HIDKBDTRACESIZE; i++) {
205 struct hidkbdtraceinfo *p =
206 &hidkbdtracedata[(i+hidkbdtraceindex)%HIDKBDTRACESIZE];
207 printf("%lld.%06ld: key0=0x%02x key1=0x%02x "
208 "key2=0x%02x key3=0x%02x\n",
209 (long long)p->tv.tv_sec, p->tv.tv_usec,
210 p->ud.keycode[0], p->ud.keycode[1],
211 p->ud.keycode[2], p->ud.keycode[3]);
212 }
213 }
214 #endif
215
216 int hidkbd_is_console;
217
218 const char *hidkbd_parse_desc(struct hidkbd *, int, void *, int);
219
220 void (*hidkbd_bell_fn)(void *, u_int, u_int, u_int, int);
221 void *hidkbd_bell_fn_arg;
222
223 void hidkbd_decode(struct hidkbd *, struct hidkbd_data *);
224 void hidkbd_delayed_decode(void *addr);
225
226 extern const struct wscons_keydesc ukbd_keydesctab[];
227
228 struct wskbd_mapdata ukbd_keymapdata = {
229 ukbd_keydesctab
230 };
231
232 int
233 hidkbd_attach(struct device *self, struct hidkbd *kbd, int console,
234 uint32_t qflags, int id, void *desc, int dlen)
235 {
236 const char *parserr;
237
238 kbd->sc_var = NULL;
239
240 parserr = hidkbd_parse_desc(kbd, id, desc, dlen);
241 if (parserr != NULL) {
242 printf(": %s\n", parserr);
243 return ENXIO;
244 }
245
246 #ifdef DIAGNOSTIC
247 printf(": %d variable keys, %d key codes",
248 kbd->sc_nvar, kbd->sc_nkeycode);
249 #endif
250
251 kbd->sc_device = self;
252 kbd->sc_debounce = (qflags & HIDKBD_SPUR_BUT_UP) != 0;
253
254 /*
255 * Remember if we're the console keyboard.
256 *
257 * XXX This always picks the first (USB) keyboard to attach,
258 * but what else can we really do?
259 */
260 if (console) {
261 kbd->sc_console_keyboard = hidkbd_is_console;
262 /* Don't let any other keyboard have it. */
263 hidkbd_is_console = 0;
264 }
265
266 timeout_set(&kbd->sc_delay, hidkbd_delayed_decode, kbd);
267
268 return 0;
269 }
270
271 void
272 hidkbd_attach_wskbd(struct hidkbd *kbd, kbd_t layout,
273 const struct wskbd_accessops *accessops)
274 {
275 struct wskbddev_attach_args a;
276
277 ukbd_keymapdata.layout = layout;
278
279 a.console = kbd->sc_console_keyboard;
280 a.keymap = &ukbd_keymapdata;
281 a.accessops = accessops;
282 a.accesscookie = kbd->sc_device;
283 kbd->sc_wskbddev = config_found(kbd->sc_device, &a, wskbddevprint);
284 }
285
286 int
287 hidkbd_detach(struct hidkbd *kbd, int flags)
288 {
289 int rv = 0;
290
291 DPRINTF(("hidkbd_detach: sc=%p flags=%d\n", kbd->sc_device, flags));
292
293 if (kbd->sc_console_keyboard) {
294 /*
295 * Disconnect our consops and set hidkbd_is_console
296 * back to 1 so that the next USB keyboard attached
297 * to the system will get it.
298 * XXX Should notify some other keyboard that it can be
299 * XXX console, if there are any other keyboards.
300 */
301 printf("%s: was console keyboard\n",
302 kbd->sc_device->dv_xname);
303 hidkbd_is_console = 1;
304 }
305 /* No need to do reference counting of hidkbd, wskbd has all the goo */
306 if (kbd->sc_wskbddev != NULL)
307 rv = config_detach(kbd->sc_wskbddev, flags);
308
309 if (kbd->sc_var != NULL)
310 free(kbd->sc_var, M_DEVBUF, 0);
311
312 return (rv);
313 }
314
315 uint8_t
316 hidkbd_translate(const struct hidkbd_translation *table, size_t tsize,
317 uint8_t keycode)
318 {
319 for (; tsize != 0; table++, tsize--)
320 if (table->original == keycode)
321 return table->translation;
322 return 0;
323 }
324
325 void
326 hidkbd_apple_translate(void *vsc, uint8_t *ibuf, u_int ilen,
327 const struct hidkbd_translation* trans, u_int tlen)
328 {
329 struct hidkbd *kbd = vsc;
330 uint8_t *pos, *spos, *epos, xlat;
331
332 spos = ibuf + kbd->sc_keycodeloc.pos / 8;
333 epos = spos + kbd->sc_nkeycode;
334
335 for (pos = spos; pos != epos; pos++) {
336 xlat = hidkbd_translate(trans, tlen, *pos);
337 if (xlat != 0)
338 *pos = xlat;
339 }
340 }
341
342 void
343 hidkbd_apple_munge(void *vsc, uint8_t *ibuf, u_int ilen)
344 {
345 struct hidkbd *kbd = vsc;
346
347 if (!hid_get_data(ibuf, ilen, &kbd->sc_fn))
348 return;
349
350 hidkbd_apple_translate(vsc, ibuf, ilen, apple_fn_trans,
351 nitems(apple_fn_trans));
352 }
353
354 void
355 hidkbd_apple_tb_munge(void *vsc, uint8_t *ibuf, u_int ilen)
356 {
357 struct hidkbd *kbd = vsc;
358
359 if (!hid_get_data(ibuf, ilen, &kbd->sc_fn))
360 return;
361
362 hidkbd_apple_munge(vsc, ibuf, ilen);
363
364 hidkbd_apple_translate(vsc, ibuf, ilen, apple_tb_trans,
365 nitems(apple_tb_trans));
366 }
367
368 void
369 hidkbd_apple_iso_munge(void *vsc, uint8_t *ibuf, u_int ilen)
370 {
371 hidkbd_apple_translate(vsc, ibuf, ilen, apple_iso_trans,
372 nitems(apple_iso_trans));
373 hidkbd_apple_munge(vsc, ibuf, ilen);
374 }
375
376 void
377 hidkbd_apple_mba_munge(void *vsc, uint8_t *ibuf, u_int ilen)
378 {
379 struct hidkbd *kbd = vsc;
380
381 if (!hid_get_data(ibuf, ilen, &kbd->sc_fn))
382 return;
383
384 hidkbd_apple_translate(vsc, ibuf, ilen, apple_mba_trans,
385 nitems(apple_mba_trans));
386 }
387
388 void
389 hidkbd_apple_iso_mba_munge(void *vsc, uint8_t *ibuf, u_int ilen)
390 {
391 hidkbd_apple_translate(vsc, ibuf, ilen, apple_iso_trans,
392 nitems(apple_iso_trans));
393 hidkbd_apple_mba_munge(vsc, ibuf, ilen);
394 }
395
396 void
397 hidkbd_input(struct hidkbd *kbd, uint8_t *data, u_int len)
398 {
399 struct hidkbd_data *ud = &kbd->sc_ndata;
400 int i;
401
402 if (kbd->sc_munge != NULL)
403 (*kbd->sc_munge)(kbd, (uint8_t *)data, len);
404
405 #ifdef HIDKBD_DEBUG
406 if (hidkbddebug > 5) {
407 printf("hidkbd_input: data");
408 for (i = 0; i < len; i++)
409 printf(" 0x%02x", data[i]);
410 printf("\n");
411 }
412 #endif
413
414 /* extract variable keys */
415 for (i = 0; i < kbd->sc_nvar; i++)
416 ud->var[i] = (u_int8_t)hid_get_data(data, len,
417 &kbd->sc_var[i].loc);
418
419 /* extract keycodes */
420 memcpy(ud->keycode, data + kbd->sc_keycodeloc.pos / 8,
421 kbd->sc_nkeycode);
422
423 if (kbd->sc_debounce && !kbd->sc_polling) {
424 /*
425 * Some keyboards have a peculiar quirk. They sometimes
426 * generate a key up followed by a key down for the same
427 * key after about 10 ms.
428 * We avoid this bug by holding off decoding for 20 ms.
429 */
430 kbd->sc_data = *ud;
431 timeout_add_msec(&kbd->sc_delay, 20);
432 } else {
433 hidkbd_decode(kbd, ud);
434 }
435 }
436
437 void
438 hidkbd_delayed_decode(void *addr)
439 {
440 struct hidkbd *kbd = addr;
441
442 hidkbd_decode(kbd, &kbd->sc_data);
443 }
444
445 void
446 hidkbd_decode(struct hidkbd *kbd, struct hidkbd_data *ud)
447 {
448 u_int16_t ibuf[MAXKEYS]; /* chars events */
449 int s;
450 int nkeys, i, j;
451 int key;
452 #define ADDKEY(c) ibuf[nkeys++] = (c)
453
454 #ifdef HIDKBD_DEBUG
455 /*
456 * Keep a trace of the last events. Using printf changes the
457 * timing, so this can be useful sometimes.
458 */
459 if (hidkbdtrace) {
460 struct hidkbdtraceinfo *p = &hidkbdtracedata[hidkbdtraceindex];
461 p->unit = kbd->sc_device->dv_unit;
462 microtime(&p->tv);
463 p->ud = *ud;
464 if (++hidkbdtraceindex >= HIDKBDTRACESIZE)
465 hidkbdtraceindex = 0;
466 }
467 if (hidkbddebug > 5) {
468 struct timeval tv;
469 microtime(&tv);
470 DPRINTF((" at %lld.%06ld key0=0x%02x key1=0x%02x "
471 "key2=0x%02x key3=0x%02x\n",
472 (long long)tv.tv_sec, tv.tv_usec,
473 ud->keycode[0], ud->keycode[1],
474 ud->keycode[2], ud->keycode[3]));
475 }
476 #endif
477
478 if (ud->keycode[0] == KEY_ERROR) {
479 DPRINTF(("hidkbd_input: KEY_ERROR\n"));
480 return; /* ignore */
481 }
482 nkeys = 0;
483
484 for (i = 0; i < kbd->sc_nvar; i++)
485 if ((kbd->sc_odata.var[i] & kbd->sc_var[i].mask) !=
486 (ud->var[i] & kbd->sc_var[i].mask)) {
487 ADDKEY(kbd->sc_var[i].key |
488 ((ud->var[i] & kbd->sc_var[i].mask) ?
489 PRESS : RELEASE));
490 }
491
492 if (memcmp(ud->keycode, kbd->sc_odata.keycode, kbd->sc_nkeycode) != 0) {
493 /* Check for released keys. */
494 for (i = 0; i < kbd->sc_nkeycode; i++) {
495 key = kbd->sc_odata.keycode[i];
496 if (key == 0)
497 continue;
498 for (j = 0; j < kbd->sc_nkeycode; j++)
499 if (key == ud->keycode[j])
500 goto rfound;
501 DPRINTFN(3,("hidkbd_decode: relse key=0x%02x\n", key));
502 ADDKEY(key | RELEASE);
503 rfound:
504 ;
505 }
506
507 /* Check for pressed keys. */
508 for (i = 0; i < kbd->sc_nkeycode; i++) {
509 key = ud->keycode[i];
510 if (key == 0)
511 continue;
512 for (j = 0; j < kbd->sc_nkeycode; j++)
513 if (key == kbd->sc_odata.keycode[j])
514 goto pfound;
515 DPRINTFN(2,("hidkbd_decode: press key=0x%02x\n", key));
516 ADDKEY(key | PRESS);
517 pfound:
518 ;
519 }
520 }
521 kbd->sc_odata = *ud;
522
523 if (nkeys == 0)
524 return;
525
526 if (kbd->sc_polling) {
527 DPRINTFN(1,("hidkbd_decode: pollchar = 0x%03x\n", ibuf[0]));
528 memcpy(kbd->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
529 kbd->sc_npollchar = nkeys;
530 return;
531 }
532
533 if (kbd->sc_wskbddev == NULL)
534 return;
535
536 #ifdef WSDISPLAY_COMPAT_RAWKBD
537 if (kbd->sc_rawkbd) {
538 u_char cbuf[MAXKEYS * 2];
539 int c;
540
541 for (i = j = 0; i < nkeys; i++) {
542 key = ibuf[i];
543 c = hidkbd_trtab[key & CODEMASK];
544 if (c == NN)
545 continue;
546 if (c & 0x80)
547 cbuf[j++] = 0xe0;
548 cbuf[j] = c & 0x7f;
549 if (key & RELEASE)
550 cbuf[j] |= 0x80;
551 DPRINTFN(1,("hidkbd_decode: raw = %s0x%02x\n",
552 c & 0x80 ? "0xe0 " : "",
553 cbuf[j]));
554 j++;
555 }
556 s = spltty();
557 wskbd_rawinput(kbd->sc_wskbddev, cbuf, j);
558
559 /*
560 * Pass audio and brightness keys to wskbd_input anyway.
561 */
562 for (i = 0; i < nkeys; i++) {
563 key = ibuf[i];
564 switch (key & CODEMASK) {
565 case 127:
566 case 128:
567 case 129:
568 case 232:
569 case 233:
570 wskbd_input(kbd->sc_wskbddev,
571 key & RELEASE ? WSCONS_EVENT_KEY_UP :
572 WSCONS_EVENT_KEY_DOWN, key & CODEMASK);
573 break;
574 }
575 }
576 splx(s);
577
578 return;
579 }
580 #endif
581
582 s = spltty();
583 for (i = 0; i < nkeys; i++) {
584 key = ibuf[i];
585 wskbd_input(kbd->sc_wskbddev,
586 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
587 key&CODEMASK);
588 }
589 splx(s);
590 #undef ADDKEY
591 }
592
593 int
594 hidkbd_enable(struct hidkbd *kbd, int on)
595 {
596 if (kbd->sc_enabled == on)
597 return EBUSY;
598
599 kbd->sc_enabled = on;
600 return 0;
601 }
602
603 int
604 hidkbd_set_leds(struct hidkbd *kbd, int leds, uint8_t *report)
605 {
606 if (kbd->sc_leds == leds)
607 return 0;
608
609 kbd->sc_leds = leds;
610
611 /*
612 * This is not totally correct, since we did not check the
613 * report size from the descriptor but for keyboards it should
614 * just be a single byte with the relevant bits set.
615 */
616 *report = 0;
617 if ((leds & WSKBD_LED_SCROLL) && kbd->sc_scroloc.size == 1)
618 *report |= 1 << kbd->sc_scroloc.pos;
619 if ((leds & WSKBD_LED_NUM) && kbd->sc_numloc.size == 1)
620 *report |= 1 << kbd->sc_numloc.pos;
621 if ((leds & WSKBD_LED_CAPS) && kbd->sc_capsloc.size == 1)
622 *report |= 1 << kbd->sc_capsloc.pos;
623 if ((leds & WSKBD_LED_COMPOSE) && kbd->sc_compose.size == 1)
624 *report |= 1 << kbd->sc_compose.pos;
625
626 return 1;
627 }
628
629 int
630 hidkbd_ioctl(struct hidkbd *kbd, u_long cmd, caddr_t data, int flag,
631 struct proc *p)
632 {
633 switch (cmd) {
634 case WSKBDIO_GETLEDS:
635 *(int *)data = kbd->sc_leds;
636 return (0);
637 case WSKBDIO_COMPLEXBELL:
638 #define d ((struct wskbd_bell_data *)data)
639 hidkbd_bell(d->pitch, d->period, d->volume, 0);
640 #undef d
641 return (0);
642 #ifdef WSDISPLAY_COMPAT_RAWKBD
643 case WSKBDIO_SETMODE:
644 DPRINTF(("hidkbd_ioctl: set raw = %d\n", *(int *)data));
645 kbd->sc_rawkbd = *(int *)data == WSKBD_RAW;
646 return (0);
647 #endif
648 }
649 return (-1);
650 }
651
652 void
653 hidkbd_cngetc(struct hidkbd *kbd, u_int *type, int *data)
654 {
655 int c;
656
657 c = kbd->sc_pollchars[0];
658 kbd->sc_npollchar--;
659 memmove(kbd->sc_pollchars, kbd->sc_pollchars+1,
660 kbd->sc_npollchar * sizeof(u_int16_t));
661 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
662 *data = c & CODEMASK;
663 }
664
665 void
666 hidkbd_bell(u_int pitch, u_int period, u_int volume, int poll)
667 {
668 if (hidkbd_bell_fn != NULL)
669 (*hidkbd_bell_fn)(hidkbd_bell_fn_arg, pitch, period,
670 volume, poll);
671 }
672
673 void
674 hidkbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
675 {
676 if (hidkbd_bell_fn == NULL) {
677 hidkbd_bell_fn = fn;
678 hidkbd_bell_fn_arg = arg;
679 }
680 }
681
682 const char *
683 hidkbd_parse_desc(struct hidkbd *kbd, int id, void *desc, int dlen)
684 {
685 struct hid_data *d;
686 struct hid_item h;
687 unsigned int i, ivar = 0;
688
689 kbd->sc_nkeycode = 0;
690
691 d = hid_start_parse(desc, dlen, hid_input);
692 while (hid_get_item(d, &h)) {
693 if (h.kind != hid_input || (h.flags & HIO_CONST) ||
694 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
695 h.report_ID != id)
696 continue;
697 if (h.flags & HIO_VARIABLE)
698 ivar++;
699 }
700 hid_end_parse(d);
701
702 if (ivar > MAXVARS) {
703 DPRINTF((": too many variable keys\n"));
704 ivar = MAXVARS;
705 }
706
707 kbd->sc_nvar = ivar;
708 kbd->sc_var = (struct hidkbd_variable *)mallocarray(ivar,
709 sizeof(struct hidkbd_variable), M_DEVBUF, M_NOWAIT);
710
711 if (!kbd->sc_var)
712 return NULL;
713
714 i = 0;
715
716 d = hid_start_parse(desc, dlen, hid_input);
717 while (hid_get_item(d, &h)) {
718 if (h.kind != hid_input || (h.flags & HIO_CONST) ||
719 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
720 h.report_ID != id)
721 continue;
722
723 DPRINTF(("hidkbd: usage=0x%x flags=0x%x pos=%d size=%d cnt=%d",
724 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
725 if (h.flags & HIO_VARIABLE) {
726 /* variable reports should be one bit each */
727 if (h.loc.size != 1) {
728 DPRINTF((": bad variable size\n"));
729 continue;
730 }
731
732 /* variable report */
733 if (i < MAXVARS) {
734 kbd->sc_var[i].loc = h.loc;
735 kbd->sc_var[i].mask = 1 << (i % 8);
736 kbd->sc_var[i].key = HID_GET_USAGE(h.usage);
737 i++;
738 }
739 } else {
740 /* keys array should be in bytes, on a byte boundary */
741 if (h.loc.size != 8) {
742 DPRINTF((": key code size != 8\n"));
743 continue;
744 }
745 if (h.loc.pos % 8 != 0) {
746 DPRINTF((": array not on byte boundary"));
747 continue;
748 }
749 if (kbd->sc_nkeycode != 0) {
750 DPRINTF((": ignoring multiple arrays\n"));
751 continue;
752 }
753 kbd->sc_keycodeloc = h.loc;
754 if (h.loc.count > MAXKEYCODE) {
755 DPRINTF((": ignoring extra key codes"));
756 kbd->sc_nkeycode = MAXKEYCODE;
757 } else
758 kbd->sc_nkeycode = h.loc.count;
759 }
760 DPRINTF(("\n"));
761 }
762 hid_end_parse(d);
763
764 /* don't attach if no keys... */
765 if (kbd->sc_nkeycode == 0 && ivar == 0)
766 return "no usable key codes array";
767
768 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_NUM_LOCK),
769 id, hid_output, &kbd->sc_numloc, NULL);
770 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_CAPS_LOCK),
771 id, hid_output, &kbd->sc_capsloc, NULL);
772 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_SCROLL_LOCK),
773 id, hid_output, &kbd->sc_scroloc, NULL);
774 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_COMPOSE),
775 id, hid_output, &kbd->sc_compose, NULL);
776
777 return (NULL);
778 }
Cache object: 57a4d299fdd455df8d2f057689689902
|