btkbd.c revision 1.16 1 /* $NetBSD: btkbd.c,v 1.16 2013/09/12 12:20:01 martin 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 * Copyright (c) 2006 Itronix Inc.
35 * All rights reserved.
36 *
37 * Written by Iain Hibbert for Itronix Inc.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. The name of Itronix Inc. may not be used to endorse
48 * or promote products derived from this software without specific
49 * prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
55 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58 * ON ANY THEORY OF LIABILITY, WHETHER IN
59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
62 */
63
64 /*
65 * based on dev/usb/ukbd.c
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: btkbd.c,v 1.16 2013/09/12 12:20:01 martin Exp $");
70
71 #include <sys/param.h>
72 #include <sys/callout.h>
73 #include <sys/conf.h>
74 #include <sys/device.h>
75 #include <sys/kernel.h>
76 #include <sys/proc.h>
77 #include <sys/systm.h>
78
79 #include <netbt/bluetooth.h>
80
81 #include <dev/bluetooth/bthid.h>
82 #include <dev/bluetooth/bthidev.h>
83
84 #include <dev/usb/hid.h>
85 #include <dev/usb/usb.h>
86 #include <dev/usb/usbhid.h>
87
88 #include <dev/wscons/wsconsio.h>
89 #include <dev/wscons/wskbdvar.h>
90 #include <dev/wscons/wsksymdef.h>
91 #include <dev/wscons/wsksymvar.h>
92
93 #include "opt_wsdisplay_compat.h"
94 #include "opt_btkbd.h"
95
96 #define MAXKEYCODE 6
97 #define MAXMOD 8 /* max 32 */
98 #define MAXKEYS (MAXMOD + (2 * MAXKEYCODE))
99
100 struct btkbd_data {
101 uint32_t modifiers;
102 uint8_t keycode[MAXKEYCODE];
103 };
104
105 struct btkbd_mod {
106 uint32_t mask;
107 uint8_t key;
108 };
109
110 struct btkbd_softc {
111 struct bthidev sc_hidev; /* device+ */
112 device_t sc_wskbd; /* child */
113 int sc_enabled;
114
115 int (*sc_output) /* output method */
116 (struct bthidev *, uint8_t *, int);
117
118 /* stored data */
119 struct btkbd_data sc_odata;
120 struct btkbd_data sc_ndata;
121
122 /* input reports */
123 int sc_nmod;
124 struct hid_location sc_modloc[MAXMOD];
125 struct btkbd_mod sc_mods[MAXMOD];
126
127 int sc_nkeycode;
128 struct hid_location sc_keycodeloc;
129
130 /* output reports */
131 struct hid_location sc_numloc;
132 struct hid_location sc_capsloc;
133 struct hid_location sc_scroloc;
134 int sc_leds;
135
136 #ifdef WSDISPLAY_COMPAT_RAWKBD
137 int sc_rawkbd;
138 #ifdef BTKBD_REPEAT
139 callout_t sc_repeat;
140 int sc_nrep;
141 char sc_rep[MAXKEYS];
142 #endif
143 #endif
144 };
145
146 /* autoconf(9) methods */
147 static int btkbd_match(device_t, cfdata_t, void *);
148 static void btkbd_attach(device_t, device_t, void *);
149 static int btkbd_detach(device_t, int);
150
151 CFATTACH_DECL_NEW(btkbd, sizeof(struct btkbd_softc),
152 btkbd_match, btkbd_attach, btkbd_detach, NULL);
153
154 /* wskbd(4) accessops */
155 static int btkbd_enable(void *, int);
156 static void btkbd_set_leds(void *, int);
157 static int btkbd_ioctl(void *, unsigned long, void *, int, struct lwp *);
158
159 static const struct wskbd_accessops btkbd_accessops = {
160 btkbd_enable,
161 btkbd_set_leds,
162 btkbd_ioctl
163 };
164
165 /* wskbd(4) keymap data */
166 extern const struct wscons_keydesc ukbd_keydesctab[];
167
168 const struct wskbd_mapdata btkbd_keymapdata = {
169 ukbd_keydesctab,
170 #if defined(BTKBD_LAYOUT)
171 BTKBD_LAYOUT,
172 #elif defined(PCKBD_LAYOUT)
173 PCKBD_LAYOUT,
174 #else
175 KB_US,
176 #endif
177 };
178
179 /* bthid methods */
180 static void btkbd_input(struct bthidev *, uint8_t *, int);
181
182 /* internal prototypes */
183 static const char *btkbd_parse_desc(struct btkbd_softc *, int, const void *, int);
184
185 #ifdef WSDISPLAY_COMPAT_RAWKBD
186 #ifdef BTKBD_REPEAT
187 static void btkbd_repeat(void *);
188 #endif
189 #endif
190
191 /*****************************************************************************
192 *
193 * btkbd autoconf(9) routines
194 */
195
196 static int
197 btkbd_match(device_t self, cfdata_t cfdata, void *aux)
198 {
199 struct bthidev_attach_args *ba = aux;
200
201 if (hid_is_collection(ba->ba_desc, ba->ba_dlen, ba->ba_id,
202 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
203 return 1;
204
205 return 0;
206 }
207
208 static void
209 btkbd_attach(device_t parent, device_t self, void *aux)
210 {
211 struct btkbd_softc *sc = device_private(self);
212 struct bthidev_attach_args *ba = aux;
213 struct wskbddev_attach_args wska;
214 const char *parserr;
215
216 sc->sc_output = ba->ba_output;
217 ba->ba_input = btkbd_input;
218
219 parserr = btkbd_parse_desc(sc, ba->ba_id, ba->ba_desc, ba->ba_dlen);
220 if (parserr != NULL) {
221 aprint_error("%s\n", parserr);
222 return;
223 }
224
225 aprint_normal("\n");
226
227 #ifdef WSDISPLAY_COMPAT_RAWKBD
228 #ifdef BTKBD_REPEAT
229 callout_init(&sc->sc_repeat, 0);
230 callout_setfunc(&sc->sc_repeat, btkbd_repeat, sc);
231 #endif
232 #endif
233
234 wska.console = 0;
235 wska.keymap = &btkbd_keymapdata;
236 wska.accessops = &btkbd_accessops;
237 wska.accesscookie = sc;
238
239 sc->sc_wskbd = config_found(self, &wska, wskbddevprint);
240
241 pmf_device_register(self, NULL, NULL);
242 }
243
244 static int
245 btkbd_detach(device_t self, int flags)
246 {
247 struct btkbd_softc *sc = device_private(self);
248 int err = 0;
249
250 pmf_device_deregister(self);
251
252 #ifdef WSDISPLAY_COMPAT_RAWKBD
253 #ifdef BTKBD_REPEAT
254 callout_stop(&sc->sc_repeat);
255 KASSERT(!callout_invoking(&sc->sc_repeat));
256 callout_destroy(&sc->sc_repeat);
257 #endif
258 #endif
259
260 if (sc->sc_wskbd != NULL) {
261 err = config_detach(sc->sc_wskbd, flags);
262 sc->sc_wskbd = NULL;
263 }
264
265 return err;
266 }
267
268 static const char *
269 btkbd_parse_desc(struct btkbd_softc *sc, int id, const void *desc, int dlen)
270 {
271 struct hid_data *d;
272 struct hid_item h;
273 int imod;
274
275 imod = 0;
276 sc->sc_nkeycode = 0;
277 d = hid_start_parse(desc, dlen, hid_input);
278 while (hid_get_item(d, &h)) {
279 if (h.kind != hid_input || (h.flags & HIO_CONST) ||
280 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
281 h.report_ID != id)
282 continue;
283
284 if (h.flags & HIO_VARIABLE) {
285 if (h.loc.size != 1)
286 return ("bad modifier size");
287
288 /* Single item */
289 if (imod < MAXMOD) {
290 sc->sc_modloc[imod] = h.loc;
291 sc->sc_mods[imod].mask = 1 << imod;
292 sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
293 imod++;
294 } else
295 return ("too many modifier keys");
296 } else {
297 /* Array */
298 if (h.loc.size != 8)
299 return ("key code size != 8");
300
301 if (h.loc.count > MAXKEYCODE)
302 return ("too many key codes");
303
304 if (h.loc.pos % 8 != 0)
305 return ("key codes not on byte boundary");
306
307 if (sc->sc_nkeycode != 0)
308 return ("multiple key code arrays\n");
309
310 sc->sc_keycodeloc = h.loc;
311 sc->sc_nkeycode = h.loc.count;
312 }
313 }
314 sc->sc_nmod = imod;
315 hid_end_parse(d);
316
317 hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
318 id, hid_output, &sc->sc_numloc, NULL);
319
320 hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
321 id, hid_output, &sc->sc_capsloc, NULL);
322
323 hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
324 id, hid_output, &sc->sc_scroloc, NULL);
325
326 return (NULL);
327 }
328
329 /*****************************************************************************
330 *
331 * wskbd(9) accessops
332 */
333
334 static int
335 btkbd_enable(void *cookie, int on)
336 {
337 struct btkbd_softc *sc = cookie;
338
339 sc->sc_enabled = on;
340 return 0;
341 }
342
343 static void
344 btkbd_set_leds(void *cookie, int leds)
345 {
346 struct btkbd_softc *sc = cookie;
347 uint8_t report;
348
349 if (sc->sc_leds == leds)
350 return;
351
352 sc->sc_leds = leds;
353
354 /*
355 * This is not totally correct, since we did not check the
356 * report size from the descriptor but for keyboards it should
357 * just be a single byte with the relevant bits set.
358 */
359 report = 0;
360 if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
361 report |= 1 << sc->sc_scroloc.pos;
362
363 if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
364 report |= 1 << sc->sc_numloc.pos;
365
366 if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
367 report |= 1 << sc->sc_capsloc.pos;
368
369 if (sc->sc_output)
370 (*sc->sc_output)(&sc->sc_hidev, &report, sizeof(report));
371 }
372
373 static int
374 btkbd_ioctl(void *cookie, unsigned long cmd, void *data, int flag,
375 struct lwp *l)
376 {
377 struct btkbd_softc *sc = cookie;
378
379 switch (cmd) {
380 case WSKBDIO_GTYPE:
381 *(int *)data = WSKBD_TYPE_BLUETOOTH;
382 break;
383
384 case WSKBDIO_SETLEDS:
385 btkbd_set_leds(sc, *(int *)data);
386 break;
387
388 case WSKBDIO_GETLEDS:
389 *(int *)data = sc->sc_leds;
390 break;
391
392 #ifdef WSDISPLAY_COMPAT_RAWKBD
393 case WSKBDIO_SETMODE:
394 sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
395 #ifdef BTKBD_REPEAT
396 callout_stop(&sc->sc_repeat);
397 #endif
398 break;
399 #endif
400
401 default:
402 return EPASSTHROUGH;
403 }
404
405 return 0;
406 }
407
408 /*****************************************************************************
409 *
410 * btkbd input routine, called from our parent
411 */
412
413 #ifdef WSDISPLAY_COMPAT_RAWKBD
414 #define NN 0 /* no translation */
415 /*
416 * Translate USB keycodes to US keyboard XT scancodes.
417 * Scancodes >= 0x80 represent EXTENDED keycodes.
418 *
419 * See http://www.microsoft.com/HWDEV/TECH/input/Scancode.asp
420 */
421 static const u_int8_t btkbd_trtab[256] = {
422 NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
423 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
424 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
425 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
426 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
427 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
428 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
429 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
430 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */
431 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
432 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
433 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
434 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, NN, 0x59, /* 60 - 67 */
435 0x5d, 0x5e, 0x5f, NN, NN, NN, NN, NN, /* 68 - 6f */
436 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
437 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7f */
438 NN, NN, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */
439 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */
440 NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */
441 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */
442 NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */
443 NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */
444 NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */
445 NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */
446 NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */
447 NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */
448 NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */
449 NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */
450 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
451 NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */
452 NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */
453 NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */
454 };
455 #endif
456
457 #define KEY_ERROR 0x01
458 #define PRESS 0x000
459 #define RELEASE 0x100
460 #define CODEMASK 0x0ff
461 #define ADDKEY(c) ibuf[nkeys++] = (c)
462 #define REP_DELAY1 400
463 #define REP_DELAYN 100
464
465 static void
466 btkbd_input(struct bthidev *hidev, uint8_t *data, int len)
467 {
468 struct btkbd_softc *sc = (struct btkbd_softc *)hidev;
469 struct btkbd_data *ud = &sc->sc_ndata;
470 uint16_t ibuf[MAXKEYS];
471 uint32_t mod, omod;
472 int nkeys, i, j;
473 int key;
474 int s;
475
476 if (sc->sc_wskbd == NULL || sc->sc_enabled == 0)
477 return;
478
479 /* extract key modifiers */
480 ud->modifiers = 0;
481 for (i = 0 ; i < sc->sc_nmod ; i++)
482 if (hid_get_data(data, &sc->sc_modloc[i]))
483 ud->modifiers |= sc->sc_mods[i].mask;
484
485 /* extract keycodes */
486 memcpy(ud->keycode, data + (sc->sc_keycodeloc.pos / 8),
487 sc->sc_nkeycode);
488
489 if (ud->keycode[0] == KEY_ERROR)
490 return; /* ignore */
491
492 nkeys = 0;
493 mod = ud->modifiers;
494 omod = sc->sc_odata.modifiers;
495 if (mod != omod)
496 for (i = 0 ; i < sc->sc_nmod ; i++)
497 if ((mod & sc->sc_mods[i].mask) !=
498 (omod & sc->sc_mods[i].mask))
499 ADDKEY(sc->sc_mods[i].key |
500 (mod & sc->sc_mods[i].mask
501 ? PRESS : RELEASE));
502
503 if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
504 /* Check for released keys. */
505 for (i = 0 ; i < sc->sc_nkeycode ; i++) {
506 key = sc->sc_odata.keycode[i];
507 if (key == 0)
508 continue;
509
510 for (j = 0 ; j < sc->sc_nkeycode ; j++)
511 if (key == ud->keycode[j])
512 goto rfound;
513
514 ADDKEY(key | RELEASE);
515
516 rfound:
517 ;
518 }
519
520 /* Check for pressed keys. */
521 for (i = 0 ; i < sc->sc_nkeycode ; i++) {
522 key = ud->keycode[i];
523 if (key == 0)
524 continue;
525
526 for (j = 0; j < sc->sc_nkeycode; j++)
527 if (key == sc->sc_odata.keycode[j])
528 goto pfound;
529
530 ADDKEY(key | PRESS);
531 pfound:
532 ;
533 }
534 }
535 sc->sc_odata = *ud;
536
537 if (nkeys == 0)
538 return;
539
540 #ifdef WSDISPLAY_COMPAT_RAWKBD
541 if (sc->sc_rawkbd) {
542 u_char cbuf[MAXKEYS * 2];
543 int c;
544 #ifdef BTKBD_REPEAT
545 int npress = 0;
546 #endif
547
548 for (i = j = 0 ; i < nkeys ; i++) {
549 key = ibuf[i];
550 c = btkbd_trtab[key & CODEMASK];
551 if (c == NN)
552 continue;
553
554 if (c & 0x80)
555 cbuf[j++] = 0xe0;
556
557 cbuf[j] = c & 0x7f;
558 if (key & RELEASE)
559 cbuf[j] |= 0x80;
560 #ifdef BTKBD_REPEAT
561 else {
562 /* remember pressed keys for autorepeat */
563 if (c & 0x80)
564 sc->sc_rep[npress++] = 0xe0;
565
566 sc->sc_rep[npress++] = c & 0x7f;
567 }
568 #endif
569
570 j++;
571 }
572
573 s = spltty();
574 wskbd_rawinput(sc->sc_wskbd, cbuf, j);
575 splx(s);
576 #ifdef BTKBD_REPEAT
577 callout_stop(&sc->sc_repeat);
578 if (npress != 0) {
579 sc->sc_nrep = npress;
580 callout_schedule(&sc->sc_repeat, hz * REP_DELAY1 / 1000);
581 }
582 #endif
583 return;
584 }
585 #endif
586
587 s = spltty();
588 for (i = 0 ; i < nkeys ; i++) {
589 key = ibuf[i];
590 wskbd_input(sc->sc_wskbd,
591 key & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
592 key & CODEMASK);
593 }
594 splx(s);
595 }
596
597 #ifdef WSDISPLAY_COMPAT_RAWKBD
598 #ifdef BTKBD_REPEAT
599 static void
600 btkbd_repeat(void *arg)
601 {
602 struct btkbd_softc *sc = arg;
603 int s;
604
605 s = spltty();
606 wskbd_rawinput(sc->sc_wskbd, sc->sc_rep, sc->sc_nrep);
607 splx(s);
608 callout_schedule(&sc->sc_repeat, hz * REP_DELAYN / 1000);
609 }
610 #endif
611 #endif
612