btkbd.c revision 1.6 1 /* $NetBSD: btkbd.c,v 1.6 2007/03/04 06:01:45 christos 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.6 2007/03/04 06:01:45 christos 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 struct device *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(struct device *, struct cfdata *, void *);
118 static void btkbd_attach(struct device *, struct device *, void *);
119 static int btkbd_detach(struct device *, int);
120
121 CFATTACH_DECL(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(struct device *self, struct cfdata *cfdata,
168 void *aux)
169 {
170 struct bthidev_attach_args *ba = aux;
171
172 if (hid_is_collection(ba->ba_desc, ba->ba_dlen, ba->ba_id,
173 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
174 return 1;
175
176 return 0;
177 }
178
179 static void
180 btkbd_attach(struct device *parent, struct device *self, void *aux)
181 {
182 struct btkbd_softc *sc = (struct btkbd_softc *)self;
183 struct bthidev_attach_args *ba = aux;
184 struct wskbddev_attach_args wska;
185 const char *parserr;
186
187 sc->sc_output = ba->ba_output;
188 ba->ba_input = btkbd_input;
189
190 parserr = btkbd_parse_desc(sc, ba->ba_id, ba->ba_desc, ba->ba_dlen);
191 if (parserr != NULL) {
192 aprint_error("%s\n", parserr);
193 return;
194 }
195
196 aprint_normal("\n");
197
198 #ifdef WSDISPLAY_COMPAT_RAWKBD
199 #ifdef BTKBD_REPEAT
200 callout_init(&sc->sc_repeat);
201 callout_setfunc(&sc->sc_repeat, btkbd_repeat, sc);
202 #endif
203 #endif
204
205 wska.console = 0;
206 wska.keymap = &btkbd_keymapdata;
207 wska.accessops = &btkbd_accessops;
208 wska.accesscookie = sc;
209
210 sc->sc_wskbd = config_found((struct device *)sc, &wska, wskbddevprint);
211 }
212
213 static int
214 btkbd_detach(struct device *self, int flags)
215 {
216 struct btkbd_softc *sc = (struct btkbd_softc *)self;
217 int err = 0;
218
219 #ifdef WSDISPLAY_COMPAT_RAWKBD
220 #ifdef BTKBD_REPEAT
221 callout_stop(&sc->sc_repeat);
222 KASSERT(!callout_invoking(&sc->sc_repeat));
223 #endif
224 #endif
225
226 if (sc->sc_wskbd != NULL) {
227 err = config_detach(sc->sc_wskbd, flags);
228 sc->sc_wskbd = NULL;
229 }
230
231 return err;
232 }
233
234 static const char *
235 btkbd_parse_desc(struct btkbd_softc *sc, int id, const void *desc, int dlen)
236 {
237 struct hid_data *d;
238 struct hid_item h;
239 int imod;
240
241 imod = 0;
242 sc->sc_nkeycode = 0;
243 d = hid_start_parse(desc, dlen, hid_input);
244 while (hid_get_item(d, &h)) {
245 if (h.kind != hid_input || (h.flags & HIO_CONST) ||
246 HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
247 h.report_ID != id)
248 continue;
249
250 if (h.flags & HIO_VARIABLE) {
251 if (h.loc.size != 1)
252 return ("bad modifier size");
253
254 /* Single item */
255 if (imod < MAXMOD) {
256 sc->sc_modloc[imod] = h.loc;
257 sc->sc_mods[imod].mask = 1 << imod;
258 sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
259 imod++;
260 } else
261 return ("too many modifier keys");
262 } else {
263 /* Array */
264 if (h.loc.size != 8)
265 return ("key code size != 8");
266
267 if (h.loc.count > MAXKEYCODE)
268 return ("too many key codes");
269
270 if (h.loc.pos % 8 != 0)
271 return ("key codes not on byte boundary");
272
273 if (sc->sc_nkeycode != 0)
274 return ("multiple key code arrays\n");
275
276 sc->sc_keycodeloc = h.loc;
277 sc->sc_nkeycode = h.loc.count;
278 }
279 }
280 sc->sc_nmod = imod;
281 hid_end_parse(d);
282
283 hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
284 id, hid_output, &sc->sc_numloc, NULL);
285
286 hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
287 id, hid_output, &sc->sc_capsloc, NULL);
288
289 hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
290 id, hid_output, &sc->sc_scroloc, NULL);
291
292 return (NULL);
293 }
294
295 /*****************************************************************************
296 *
297 * wskbd(9) accessops
298 */
299
300 static int
301 btkbd_enable(void *self, int on)
302 {
303 struct btkbd_softc *sc = (struct btkbd_softc *)self;
304
305 sc->sc_enabled = on;
306 return 0;
307 }
308
309 static void
310 btkbd_set_leds(void *self, int leds)
311 {
312 struct btkbd_softc *sc = (struct btkbd_softc *)self;
313 uint8_t report;
314
315 if (sc->sc_leds == leds)
316 return;
317
318 sc->sc_leds = leds;
319
320 /*
321 * This is not totally correct, since we did not check the
322 * report size from the descriptor but for keyboards it should
323 * just be a single byte with the relevant bits set.
324 */
325 report = 0;
326 if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
327 report |= 1 << sc->sc_scroloc.pos;
328
329 if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
330 report |= 1 << sc->sc_numloc.pos;
331
332 if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
333 report |= 1 << sc->sc_capsloc.pos;
334
335 if (sc->sc_output)
336 (*sc->sc_output)(&sc->sc_hidev, &report, sizeof(report));
337 }
338
339 static int
340 btkbd_ioctl(void *self, unsigned long cmd, void *data, int flag,
341 struct lwp *l)
342 {
343 struct btkbd_softc *sc = (struct btkbd_softc *)self;
344
345 switch (cmd) {
346 case WSKBDIO_GTYPE:
347 *(int *)data = WSKBD_TYPE_BLUETOOTH;
348 break;
349
350 case WSKBDIO_SETLEDS:
351 btkbd_set_leds(sc, *(int *)data);
352 break;
353
354 case WSKBDIO_GETLEDS:
355 *(int *)data = sc->sc_leds;
356 break;
357
358 #ifdef WSDISPLAY_COMPAT_RAWKBD
359 case WSKBDIO_SETMODE:
360 sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
361 #ifdef BTKBD_REPEAT
362 callout_stop(&sc->sc_repeat);
363 #endif
364 break;
365 #endif
366
367 default:
368 return EPASSTHROUGH;
369 }
370
371 return 0;
372 }
373
374 /*****************************************************************************
375 *
376 * btkbd input routine, called from our parent
377 */
378
379 #ifdef WSDISPLAY_COMPAT_RAWKBD
380 #define NN 0 /* no translation */
381 /*
382 * Translate USB keycodes to US keyboard XT scancodes.
383 * Scancodes >= 0x80 represent EXTENDED keycodes.
384 *
385 * See http://www.microsoft.com/HWDEV/TECH/input/Scancode.asp
386 */
387 static const u_int8_t btkbd_trtab[256] = {
388 NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
389 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
390 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
391 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
392 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
393 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
394 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
395 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
396 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */
397 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
398 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
399 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
400 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, NN, 0x59, /* 60 - 67 */
401 0x5d, 0x5e, 0x5f, NN, NN, NN, NN, NN, /* 68 - 6f */
402 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
403 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7f */
404 NN, NN, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */
405 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */
406 NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */
407 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */
408 NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */
409 NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */
410 NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */
411 NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */
412 NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */
413 NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */
414 NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */
415 NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */
416 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
417 NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */
418 NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */
419 NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */
420 };
421 #endif
422
423 #define KEY_ERROR 0x01
424 #define PRESS 0x000
425 #define RELEASE 0x100
426 #define CODEMASK 0x0ff
427 #define ADDKEY(c) ibuf[nkeys++] = (c)
428 #define REP_DELAY1 400
429 #define REP_DELAYN 100
430
431 static void
432 btkbd_input(struct bthidev *self, uint8_t *data, int len)
433 {
434 struct btkbd_softc *sc = (struct btkbd_softc *)self;
435 struct btkbd_data *ud = &sc->sc_ndata;
436 uint16_t ibuf[MAXKEYS];
437 uint32_t mod, omod;
438 int nkeys, i, j;
439 int key;
440 int s;
441
442 if (sc->sc_wskbd == NULL || sc->sc_enabled == 0)
443 return;
444
445 /* extract key modifiers */
446 ud->modifiers = 0;
447 for (i = 0 ; i < sc->sc_nmod ; i++)
448 if (hid_get_data(data, &sc->sc_modloc[i]))
449 ud->modifiers |= sc->sc_mods[i].mask;
450
451 /* extract keycodes */
452 memcpy(ud->keycode, data + (sc->sc_keycodeloc.pos / 8),
453 sc->sc_nkeycode);
454
455 if (ud->keycode[0] == KEY_ERROR)
456 return; /* ignore */
457
458 nkeys = 0;
459 mod = ud->modifiers;
460 omod = sc->sc_odata.modifiers;
461 if (mod != omod)
462 for (i = 0 ; i < sc->sc_nmod ; i++)
463 if ((mod & sc->sc_mods[i].mask) !=
464 (omod & sc->sc_mods[i].mask))
465 ADDKEY(sc->sc_mods[i].key |
466 (mod & sc->sc_mods[i].mask
467 ? PRESS : RELEASE));
468
469 if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
470 /* Check for released keys. */
471 for (i = 0 ; i < sc->sc_nkeycode ; i++) {
472 key = sc->sc_odata.keycode[i];
473 if (key == 0)
474 continue;
475
476 for (j = 0 ; j < sc->sc_nkeycode ; j++)
477 if (key == ud->keycode[j])
478 goto rfound;
479
480 ADDKEY(key | RELEASE);
481
482 rfound:
483 ;
484 }
485
486 /* Check for pressed keys. */
487 for (i = 0 ; i < sc->sc_nkeycode ; i++) {
488 key = ud->keycode[i];
489 if (key == 0)
490 continue;
491
492 for (j = 0; j < sc->sc_nkeycode; j++)
493 if (key == sc->sc_odata.keycode[j])
494 goto pfound;
495
496 ADDKEY(key | PRESS);
497 pfound:
498 ;
499 }
500 }
501 sc->sc_odata = *ud;
502
503 if (nkeys == 0)
504 return;
505
506 #ifdef WSDISPLAY_COMPAT_RAWKBD
507 if (sc->sc_rawkbd) {
508 u_char cbuf[MAXKEYS * 2];
509 int c;
510 int npress;
511
512 for (npress = i = j = 0 ; i < nkeys ; i++) {
513 key = ibuf[i];
514 c = btkbd_trtab[key & CODEMASK];
515 if (c == NN)
516 continue;
517
518 if (c & 0x80)
519 cbuf[j++] = 0xe0;
520
521 cbuf[j] = c & 0x7f;
522 if (key & RELEASE)
523 cbuf[j] |= 0x80;
524 #ifdef BTKBD_REPEAT
525 else {
526 /* remember pressed keys for autorepeat */
527 if (c & 0x80)
528 sc->sc_rep[npress++] = 0xe0;
529
530 sc->sc_rep[npress++] = c & 0x7f;
531 }
532 #endif
533
534 j++;
535 }
536
537 s = spltty();
538 wskbd_rawinput(sc->sc_wskbd, cbuf, j);
539 splx(s);
540 #ifdef BTKBD_REPEAT
541 callout_stop(&sc->sc_repeat);
542 if (npress != 0) {
543 sc->sc_nrep = npress;
544 callout_schedule(&sc->sc_repeat, hz * REP_DELAY1 / 1000);
545 }
546 #endif
547 return;
548 }
549 #endif
550
551 s = spltty();
552 for (i = 0 ; i < nkeys ; i++) {
553 key = ibuf[i];
554 wskbd_input(sc->sc_wskbd,
555 key & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
556 key & CODEMASK);
557 }
558 splx(s);
559 }
560
561 #ifdef WSDISPLAY_COMPAT_RAWKBD
562 #ifdef BTKBD_REPEAT
563 static void
564 btkbd_repeat(void *arg)
565 {
566 struct btkbd_softc *sc = arg;
567 int s;
568
569 s = spltty();
570 wskbd_rawinput(sc->sc_wskbd, sc->sc_rep, sc->sc_nrep);
571 splx(s);
572 callout_schedule(&sc->sc_repeat, hz * REP_DELAYN / 1000);
573 }
574 #endif
575 #endif
576