1 /* $OpenBSD: hidkbd.c,v 1.15 2024/10/21 19:05:31 miod 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, 0xb7, 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 { 58, 233 }, /* F1 -> screen brightness down */
130 { 59, 232 }, /* F2 -> screen brightness up */
131 #ifdef notyet
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 { 60, 127 }, /* F3 -> audio mute */
142 { 61, 129 }, /* F4 -> audio lower */
143 { 62, 128 }, /* F5 -> audio raise */
144 { 63, 83 }, /* F6 -> num lock */
145 { 65, 234 }, /* F8 -> backlight toggle */
146 { 66, 236 }, /* F9 -> backlight lower */
147 { 67, 235 }, /* F10 -> backlight raise */
148 { 39, 84 }, /* keypad divide */
149 { 19, 85 }, /* keypad multiply */
150 { 51, 86 }, /* keypad subtract */
151 { 56, 87 }, /* keypad add */
152 { 13, 89 }, /* keypad 1 */
153 { 14, 90 }, /* keypad 2 */
154 { 15, 91 }, /* keypad 3 */
155 { 24, 92 }, /* keypad 4 */
156 { 12, 93 }, /* keypad 5 */
157 { 18, 94 }, /* keypad 6 */
158 { 36, 95 }, /* keypad 7 */
159 { 37, 96 }, /* keypad 8 */
160 { 38, 97 }, /* keypad 9 */
161 { 16, 98 }, /* keypad 0 */
162 { 55, 99 }, /* keypad decimal */
163 { 45, 103 }, /* keypad equal */
164 #else
165 { 63, 102 }, /* F6 -> sleep */
166 { 67, 127 }, /* F10 -> audio mute */
167 { 68, 129 }, /* F11 -> audio lower */
168 { 69, 128 }, /* F12 -> audio raise */
169 #endif
170 { 79, 77 }, /* right -> end */
171 { 80, 74 }, /* left -> home */
172 { 81, 78 }, /* down -> page down */
173 { 82, 75 } /* up -> page up */
174 };
175
176 static const struct hidkbd_translation apple_mba_trans[] = {
177 { 40, 73 }, /* return -> insert */
178 { 42, 76 }, /* backspace -> delete */
179 #ifdef notyet
180 { 58, 0 }, /* F1 -> screen brightness down */
181 { 59, 0 }, /* F2 -> screen brightness up */
182 { 60, 0 }, /* F3 */
183 { 61, 0 }, /* F4 */
184 { 62, 0 }, /* F5 */
185 { 63, 0 }, /* F6 -> audio back */
186 { 64, 0 }, /* F7 -> audio pause/play */
187 { 65, 0 }, /* F8 -> audio next */
188 #endif
189 { 66, 127 }, /* F9 -> audio mute */
190 { 67, 129 }, /* F10 -> audio lower */
191 { 68, 128 }, /* F11 -> audio raise */
192 #ifdef notyet
193 { 69, 0 }, /* F12 -> eject */
194 #endif
195 { 79, 77 }, /* right -> end */
196 { 80, 74 }, /* left -> home */
197 { 81, 78 }, /* down -> page down */
198 { 82, 75 } /* up -> page up */
199 };
200
201 static const struct hidkbd_translation apple_iso_trans[] = {
202 { 53, 100 }, /* less -> grave */
203 { 100, 53 }
204 };
205
206 #define KEY_ERROR 0x01
207
208 #ifdef HIDKBD_DEBUG
209 #define HIDKBDTRACESIZE 64
210 struct hidkbdtraceinfo {
211 int unit;
212 struct timeval tv;
213 struct hidkbd_data ud;
214 };
215 struct hidkbdtraceinfo hidkbdtracedata[HIDKBDTRACESIZE];
216 int hidkbdtraceindex = 0;
217 int hidkbdtrace = 0;
218 void hidkbdtracedump(void);
219 void
hidkbdtracedump(void)220 hidkbdtracedump(void)
221 {
222 int i;
223 for (i = 0; i < HIDKBDTRACESIZE; i++) {
224 struct hidkbdtraceinfo *p =
225 &hidkbdtracedata[(i+hidkbdtraceindex)%HIDKBDTRACESIZE];
226 printf("%lld.%06ld: key0=0x%02x key1=0x%02x "
227 "key2=0x%02x key3=0x%02x\n",
228 (long long)p->tv.tv_sec, p->tv.tv_usec,
229 p->ud.keycode[0], p->ud.keycode[1],
230 p->ud.keycode[2], p->ud.keycode[3]);
231 }
232 }
233 #endif
234
235 int hidkbd_is_console;
236
237 const char *hidkbd_parse_desc(struct hidkbd *, int, void *, int);
238
239 void (*hidkbd_bell_fn)(void *, u_int, u_int, u_int, int);
240 void *hidkbd_bell_fn_arg;
241
242 void hidkbd_decode(struct hidkbd *, struct hidkbd_data *);
243 void hidkbd_delayed_decode(void *addr);
244
245 extern const struct wscons_keydesc ukbd_keydesctab[];
246
247 struct wskbd_mapdata ukbd_keymapdata = {
248 ukbd_keydesctab
249 };
250
251 int
hidkbd_attach(struct device * self,struct hidkbd * kbd,int console,uint32_t qflags,int id,void * desc,int dlen)252 hidkbd_attach(struct device *self, struct hidkbd *kbd, int console,
253 uint32_t qflags, int id, void *desc, int dlen)
254 {
255 const char *parserr;
256
257 kbd->sc_var = NULL;
258
259 parserr = hidkbd_parse_desc(kbd, id, desc, dlen);
260 if (parserr != NULL) {
261 printf(": %s\n", parserr);
262 return ENXIO;
263 }
264
265 #ifdef DIAGNOSTIC
266 printf(": %d variable keys, %d key codes",
267 kbd->sc_nvar, kbd->sc_nkeycode);
268 #endif
269
270 kbd->sc_device = self;
271 kbd->sc_debounce = (qflags & HIDKBD_SPUR_BUT_UP) != 0;
272
273 /*
274 * Remember if we're the console keyboard.
275 *
276 * XXX This always picks the first (USB) keyboard to attach,
277 * but what else can we really do?
278 */
279 if (console) {
280 kbd->sc_console_keyboard = hidkbd_is_console;
281 /* Don't let any other keyboard have it. */
282 hidkbd_is_console = 0;
283 }
284
285 timeout_set(&kbd->sc_delay, hidkbd_delayed_decode, kbd);
286
287 return 0;
288 }
289
290 void
hidkbd_attach_wskbd(struct hidkbd * kbd,kbd_t layout,const struct wskbd_accessops * accessops)291 hidkbd_attach_wskbd(struct hidkbd *kbd, kbd_t layout,
292 const struct wskbd_accessops *accessops)
293 {
294 struct wskbddev_attach_args a;
295
296 ukbd_keymapdata.layout = layout;
297
298 a.console = kbd->sc_console_keyboard;
299 a.keymap = &ukbd_keymapdata;
300 a.accessops = accessops;
301 a.accesscookie = kbd->sc_device;
302 kbd->sc_wskbddev = config_found(kbd->sc_device, &a, wskbddevprint);
303 }
304
305 int
hidkbd_detach(struct hidkbd * kbd,int flags)306 hidkbd_detach(struct hidkbd *kbd, int flags)
307 {
308 int rv = 0;
309
310 DPRINTF(("hidkbd_detach: sc=%p flags=%d\n", kbd->sc_device, flags));
311
312 if (kbd->sc_console_keyboard) {
313 /*
314 * Disconnect our consops and set hidkbd_is_console
315 * back to 1 so that the next USB keyboard attached
316 * to the system will get it.
317 * XXX Should notify some other keyboard that it can be
318 * XXX console, if there are any other keyboards.
319 */
320 printf("%s: was console keyboard\n",
321 kbd->sc_device->dv_xname);
322 hidkbd_is_console = 1;
323 }
324 /* No need to do reference counting of hidkbd, wskbd has all the goo */
325 if (kbd->sc_wskbddev != NULL)
326 rv = config_detach(kbd->sc_wskbddev, flags);
327
328 if (kbd->sc_var != NULL)
329 free(kbd->sc_var, M_DEVBUF, 0);
330
331 return (rv);
332 }
333
334 uint8_t
hidkbd_translate(const struct hidkbd_translation * table,size_t tsize,uint8_t keycode)335 hidkbd_translate(const struct hidkbd_translation *table, size_t tsize,
336 uint8_t keycode)
337 {
338 for (; tsize != 0; table++, tsize--)
339 if (table->original == keycode)
340 return table->translation;
341 return 0;
342 }
343
344 void
hidkbd_apple_translate(void * vsc,uint8_t * ibuf,u_int ilen,const struct hidkbd_translation * trans,u_int tlen)345 hidkbd_apple_translate(void *vsc, uint8_t *ibuf, u_int ilen,
346 const struct hidkbd_translation* trans, u_int tlen)
347 {
348 struct hidkbd *kbd = vsc;
349 uint8_t *pos, *spos, *epos, xlat;
350
351 spos = ibuf + kbd->sc_keycodeloc.pos / 8;
352 epos = spos + kbd->sc_nkeycode;
353
354 for (pos = spos; pos != epos; pos++) {
355 xlat = hidkbd_translate(trans, tlen, *pos);
356 if (xlat != 0)
357 *pos = xlat;
358 }
359 }
360
361 void
hidkbd_apple_munge(void * vsc,uint8_t * ibuf,u_int ilen)362 hidkbd_apple_munge(void *vsc, uint8_t *ibuf, u_int ilen)
363 {
364 struct hidkbd *kbd = vsc;
365
366 if (!hid_get_data(ibuf, ilen, &kbd->sc_fn))
367 return;
368
369 hidkbd_apple_translate(vsc, ibuf, ilen, apple_fn_trans,
370 nitems(apple_fn_trans));
371 }
372
373 void
hidkbd_apple_tb_munge(void * vsc,uint8_t * ibuf,u_int ilen)374 hidkbd_apple_tb_munge(void *vsc, uint8_t *ibuf, u_int ilen)
375 {
376 struct hidkbd *kbd = vsc;
377
378 if (!hid_get_data(ibuf, ilen, &kbd->sc_fn))
379 return;
380
381 hidkbd_apple_munge(vsc, ibuf, ilen);
382
383 hidkbd_apple_translate(vsc, ibuf, ilen, apple_tb_trans,
384 nitems(apple_tb_trans));
385 }
386
387 void
hidkbd_apple_iso_munge(void * vsc,uint8_t * ibuf,u_int ilen)388 hidkbd_apple_iso_munge(void *vsc, uint8_t *ibuf, u_int ilen)
389 {
390 hidkbd_apple_translate(vsc, ibuf, ilen, apple_iso_trans,
391 nitems(apple_iso_trans));
392 hidkbd_apple_munge(vsc, ibuf, ilen);
393 }
394
395 void
hidkbd_apple_mba_munge(void * vsc,uint8_t * ibuf,u_int ilen)396 hidkbd_apple_mba_munge(void *vsc, uint8_t *ibuf, u_int ilen)
397 {
398 struct hidkbd *kbd = vsc;
399
400 if (!hid_get_data(ibuf, ilen, &kbd->sc_fn))
401 return;
402
403 hidkbd_apple_translate(vsc, ibuf, ilen, apple_mba_trans,
404 nitems(apple_mba_trans));
405 }
406
407 void
hidkbd_apple_iso_mba_munge(void * vsc,uint8_t * ibuf,u_int ilen)408 hidkbd_apple_iso_mba_munge(void *vsc, uint8_t *ibuf, u_int ilen)
409 {
410 hidkbd_apple_translate(vsc, ibuf, ilen, apple_iso_trans,
411 nitems(apple_iso_trans));
412 hidkbd_apple_mba_munge(vsc, ibuf, ilen);
413 }
414
415 void
hidkbd_input(struct hidkbd * kbd,uint8_t * data,u_int len)416 hidkbd_input(struct hidkbd *kbd, uint8_t *data, u_int len)
417 {
418 struct hidkbd_data *ud = &kbd->sc_ndata;
419 int i;
420
421 if (kbd->sc_munge != NULL)
422 (*kbd->sc_munge)(kbd, (uint8_t *)data, len);
423
424 #ifdef HIDKBD_DEBUG
425 if (hidkbddebug > 5) {
426 printf("hidkbd_input: data");
427 for (i = 0; i < len; i++)
428 printf(" 0x%02x", data[i]);
429 printf("\n");
430 }
431 #endif
432
433 /* extract variable keys */
434 for (i = 0; i < kbd->sc_nvar; i++)
435 ud->var[i] = (u_int8_t)hid_get_data(data, len,
436 &kbd->sc_var[i].loc);
437
438 /* extract keycodes */
439 if (kbd->sc_keycodeloc.pos / 8 + kbd->sc_nkeycode <= len)
440 memcpy(ud->keycode, data + kbd->sc_keycodeloc.pos / 8,
441 kbd->sc_nkeycode);
442 else
443 memset(ud->keycode, 0, kbd->sc_nkeycode);
444
445 if (kbd->sc_debounce && !kbd->sc_polling) {
446 /*
447 * Some keyboards have a peculiar quirk. They sometimes
448 * generate a key up followed by a key down for the same
449 * key after about 10 ms.
450 * We avoid this bug by holding off decoding for 20 ms.
451 */
452 kbd->sc_data = *ud;
453 timeout_add_msec(&kbd->sc_delay, 20);
454 } else {
455 hidkbd_decode(kbd, ud);
456 }
457 }
458
459 void
hidkbd_delayed_decode(void * addr)460 hidkbd_delayed_decode(void *addr)
461 {
462 struct hidkbd *kbd = addr;
463
464 hidkbd_decode(kbd, &kbd->sc_data);
465 }
466
467 void
hidkbd_decode(struct hidkbd * kbd,struct hidkbd_data * ud)468 hidkbd_decode(struct hidkbd *kbd, struct hidkbd_data *ud)
469 {
470 u_int16_t ibuf[MAXKEYS]; /* chars events */
471 int s;
472 int nkeys, i, j;
473 int key;
474 #define ADDKEY(c) ibuf[nkeys++] = (c)
475
476 #ifdef HIDKBD_DEBUG
477 /*
478 * Keep a trace of the last events. Using printf changes the
479 * timing, so this can be useful sometimes.
480 */
481 if (hidkbdtrace) {
482 struct hidkbdtraceinfo *p = &hidkbdtracedata[hidkbdtraceindex];
483 p->unit = kbd->sc_device->dv_unit;
484 microtime(&p->tv);
485 p->ud = *ud;
486 if (++hidkbdtraceindex >= HIDKBDTRACESIZE)
487 hidkbdtraceindex = 0;
488 }
489 if (hidkbddebug > 5) {
490 struct timeval tv;
491 microtime(&tv);
492 DPRINTF((" at %lld.%06ld key0=0x%02x key1=0x%02x "
493 "key2=0x%02x key3=0x%02x\n",
494 (long long)tv.tv_sec, tv.tv_usec,
495 ud->keycode[0], ud->keycode[1],
496 ud->keycode[2], ud->keycode[3]));
497 }
498 #endif
499
500 if (ud->keycode[0] == KEY_ERROR) {
501 DPRINTF(("hidkbd_input: KEY_ERROR\n"));
502 return; /* ignore */
503 }
504 nkeys = 0;
505
506 for (i = 0; i < kbd->sc_nvar; i++)
507 if ((kbd->sc_odata.var[i] & kbd->sc_var[i].mask) !=
508 (ud->var[i] & kbd->sc_var[i].mask)) {
509 ADDKEY(kbd->sc_var[i].key |
510 ((ud->var[i] & kbd->sc_var[i].mask) ?
511 PRESS : RELEASE));
512 }
513
514 if (memcmp(ud->keycode, kbd->sc_odata.keycode, kbd->sc_nkeycode) != 0) {
515 /* Check for released keys. */
516 for (i = 0; i < kbd->sc_nkeycode; i++) {
517 key = kbd->sc_odata.keycode[i];
518 if (key == 0)
519 continue;
520 for (j = 0; j < kbd->sc_nkeycode; j++)
521 if (key == ud->keycode[j])
522 goto rfound;
523 DPRINTFN(3,("hidkbd_decode: relse key=0x%02x\n", key));
524 ADDKEY(key | RELEASE);
525 rfound:
526 ;
527 }
528
529 /* Check for pressed keys. */
530 for (i = 0; i < kbd->sc_nkeycode; i++) {
531 key = ud->keycode[i];
532 if (key == 0)
533 continue;
534 for (j = 0; j < kbd->sc_nkeycode; j++)
535 if (key == kbd->sc_odata.keycode[j])
536 goto pfound;
537 DPRINTFN(2,("hidkbd_decode: press key=0x%02x\n", key));
538 ADDKEY(key | PRESS);
539 pfound:
540 ;
541 }
542 }
543 kbd->sc_odata = *ud;
544
545 if (nkeys == 0)
546 return;
547
548 if (kbd->sc_polling) {
549 DPRINTFN(1,("hidkbd_decode: pollchar = 0x%03x\n", ibuf[0]));
550 memcpy(kbd->sc_pollchars, ibuf, nkeys * sizeof(u_int16_t));
551 kbd->sc_npollchar = nkeys;
552 return;
553 }
554
555 if (kbd->sc_wskbddev == NULL)
556 return;
557
558 #ifdef WSDISPLAY_COMPAT_RAWKBD
559 if (kbd->sc_rawkbd) {
560 u_char cbuf[MAXKEYS * 2];
561 int c;
562
563 for (i = j = 0; i < nkeys; i++) {
564 key = ibuf[i];
565 c = hidkbd_trtab[key & CODEMASK];
566 if (c == NN)
567 continue;
568 if (c & 0x80)
569 cbuf[j++] = 0xe0;
570 cbuf[j] = c & 0x7f;
571 if (key & RELEASE)
572 cbuf[j] |= 0x80;
573 DPRINTFN(1,("hidkbd_decode: raw = %s0x%02x\n",
574 c & 0x80 ? "0xe0 " : "",
575 cbuf[j]));
576 j++;
577 }
578 s = spltty();
579 wskbd_rawinput(kbd->sc_wskbddev, cbuf, j);
580
581 /*
582 * Pass audio, brightness and sleep keys to wskbd_input anyway.
583 */
584 for (i = 0; i < nkeys; i++) {
585 key = ibuf[i];
586 switch (key & CODEMASK) {
587 case 102:
588 case 127:
589 case 128:
590 case 129:
591 case 232:
592 case 233:
593 case 234:
594 case 235:
595 case 236:
596 wskbd_input(kbd->sc_wskbddev,
597 key & RELEASE ? WSCONS_EVENT_KEY_UP :
598 WSCONS_EVENT_KEY_DOWN, key & CODEMASK);
599 break;
600 }
601 }
602 splx(s);
603
604 return;
605 }
606 #endif
607
608 s = spltty();
609 for (i = 0; i < nkeys; i++) {
610 key = ibuf[i];
611 wskbd_input(kbd->sc_wskbddev,
612 key&RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
613 key&CODEMASK);
614 }
615 splx(s);
616 #undef ADDKEY
617 }
618
619 int
hidkbd_enable(struct hidkbd * kbd,int on)620 hidkbd_enable(struct hidkbd *kbd, int on)
621 {
622 if (kbd->sc_enabled == on)
623 return EBUSY;
624
625 kbd->sc_enabled = on;
626 return 0;
627 }
628
629 int
hidkbd_set_leds(struct hidkbd * kbd,int leds,uint8_t * report)630 hidkbd_set_leds(struct hidkbd *kbd, int leds, uint8_t *report)
631 {
632 if (kbd->sc_leds == leds)
633 return 0;
634
635 kbd->sc_leds = leds;
636
637 /*
638 * This is not totally correct, since we did not check the
639 * report size from the descriptor but for keyboards it should
640 * just be a single byte with the relevant bits set.
641 */
642 *report = 0;
643 if ((leds & WSKBD_LED_SCROLL) && kbd->sc_scroloc.size == 1)
644 *report |= 1 << kbd->sc_scroloc.pos;
645 if ((leds & WSKBD_LED_NUM) && kbd->sc_numloc.size == 1)
646 *report |= 1 << kbd->sc_numloc.pos;
647 if ((leds & WSKBD_LED_CAPS) && kbd->sc_capsloc.size == 1)
648 *report |= 1 << kbd->sc_capsloc.pos;
649 if ((leds & WSKBD_LED_COMPOSE) && kbd->sc_compose.size == 1)
650 *report |= 1 << kbd->sc_compose.pos;
651
652 return 1;
653 }
654
655 int
hidkbd_ioctl(struct hidkbd * kbd,u_long cmd,caddr_t data,int flag,struct proc * p)656 hidkbd_ioctl(struct hidkbd *kbd, u_long cmd, caddr_t data, int flag,
657 struct proc *p)
658 {
659 switch (cmd) {
660 case WSKBDIO_GETLEDS:
661 *(int *)data = kbd->sc_leds;
662 return (0);
663 case WSKBDIO_COMPLEXBELL:
664 #define d ((struct wskbd_bell_data *)data)
665 hidkbd_bell(d->pitch, d->period, d->volume, 0);
666 #undef d
667 return (0);
668 #ifdef WSDISPLAY_COMPAT_RAWKBD
669 case WSKBDIO_SETMODE:
670 DPRINTF(("hidkbd_ioctl: set raw = %d\n", *(int *)data));
671 kbd->sc_rawkbd = *(int *)data == WSKBD_RAW;
672 return (0);
673 #endif
674 }
675 return (-1);
676 }
677
678 void
hidkbd_cngetc(struct hidkbd * kbd,u_int * type,int * data)679 hidkbd_cngetc(struct hidkbd *kbd, u_int *type, int *data)
680 {
681 int c;
682
683 c = kbd->sc_pollchars[0];
684 kbd->sc_npollchar--;
685 memmove(kbd->sc_pollchars, kbd->sc_pollchars+1,
686 kbd->sc_npollchar * sizeof(u_int16_t));
687 *type = c & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
688 *data = c & CODEMASK;
689 }
690
691 void
hidkbd_bell(u_int pitch,u_int period,u_int volume,int poll)692 hidkbd_bell(u_int pitch, u_int period, u_int volume, int poll)
693 {
694 if (hidkbd_bell_fn != NULL)
695 (*hidkbd_bell_fn)(hidkbd_bell_fn_arg, pitch, period,
696 volume, poll);
697 }
698
699 void
hidkbd_hookup_bell(void (* fn)(void *,u_int,u_int,u_int,int),void * arg)700 hidkbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
701 {
702 if (hidkbd_bell_fn == NULL) {
703 hidkbd_bell_fn = fn;
704 hidkbd_bell_fn_arg = arg;
705 }
706 }
707
708 const char *
hidkbd_parse_desc(struct hidkbd * kbd,int id,void * desc,int dlen)709 hidkbd_parse_desc(struct hidkbd *kbd, int id, void *desc, int dlen)
710 {
711 struct hid_data *d;
712 struct hid_item h;
713 unsigned int i, ivar = 0;
714
715 kbd->sc_nkeycode = 0;
716
717 d = hid_start_parse(desc, dlen, hid_input);
718 while (hid_get_item(d, &h)) {
719 if (h.kind != hid_input || (h.flags & HIO_CONST) ||
720 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
721 h.report_ID != id)
722 continue;
723 if (h.flags & HIO_VARIABLE)
724 ivar++;
725 }
726 hid_end_parse(d);
727
728 if (ivar > MAXVARS) {
729 DPRINTF((": too many variable keys\n"));
730 ivar = MAXVARS;
731 }
732
733 kbd->sc_nvar = ivar;
734 kbd->sc_var = (struct hidkbd_variable *)mallocarray(ivar,
735 sizeof(struct hidkbd_variable), M_DEVBUF, M_NOWAIT);
736
737 if (!kbd->sc_var)
738 return NULL;
739
740 i = 0;
741
742 d = hid_start_parse(desc, dlen, hid_input);
743 while (hid_get_item(d, &h)) {
744 if (h.kind != hid_input || (h.flags & HIO_CONST) ||
745 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
746 h.report_ID != id)
747 continue;
748
749 DPRINTF(("hidkbd: usage=0x%x flags=0x%x pos=%d size=%d cnt=%d",
750 h.usage, h.flags, h.loc.pos, h.loc.size, h.loc.count));
751 if (h.flags & HIO_VARIABLE) {
752 /* variable reports should be one bit each */
753 if (h.loc.size != 1) {
754 DPRINTF((": bad variable size\n"));
755 continue;
756 }
757
758 /* variable report */
759 if (i < MAXVARS) {
760 kbd->sc_var[i].loc = h.loc;
761 kbd->sc_var[i].mask = 1 << (i % 8);
762 kbd->sc_var[i].key = HID_GET_USAGE(h.usage);
763 i++;
764 }
765 } else {
766 /* keys array should be in bytes, on a byte boundary */
767 if (h.loc.size != 8) {
768 DPRINTF((": key code size != 8\n"));
769 continue;
770 }
771 if (h.loc.pos % 8 != 0) {
772 DPRINTF((": array not on byte boundary"));
773 continue;
774 }
775 if (kbd->sc_nkeycode != 0) {
776 DPRINTF((": ignoring multiple arrays\n"));
777 continue;
778 }
779 kbd->sc_keycodeloc = h.loc;
780 if (h.loc.count > MAXKEYCODE) {
781 DPRINTF((": ignoring extra key codes"));
782 kbd->sc_nkeycode = MAXKEYCODE;
783 } else
784 kbd->sc_nkeycode = h.loc.count;
785 }
786 DPRINTF(("\n"));
787 }
788 hid_end_parse(d);
789
790 /* don't attach if no keys... */
791 if (kbd->sc_nkeycode == 0 && ivar == 0)
792 return "no usable key codes array";
793
794 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_NUM_LOCK),
795 id, hid_output, &kbd->sc_numloc, NULL);
796 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_CAPS_LOCK),
797 id, hid_output, &kbd->sc_capsloc, NULL);
798 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_SCROLL_LOCK),
799 id, hid_output, &kbd->sc_scroloc, NULL);
800 hid_locate(desc, dlen, HID_USAGE2(HUP_LED, HUL_COMPOSE),
801 id, hid_output, &kbd->sc_compose, NULL);
802
803 return (NULL);
804 }
805