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