btkbd.c revision 1.3.4.2 1 /* $NetBSD: btkbd.c,v 1.3.4.2 2006/09/09 02:49:44 rpaulo 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.3.4.2 2006/09/09 02:49:44 rpaulo 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, caddr_t, 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, 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(struct device *parent, struct device *self, void *aux)
180 {
181 struct btkbd_softc *sc = (struct btkbd_softc *)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);
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((struct device *)sc, &wska, wskbddevprint);
210 }
211
212 static int
213 btkbd_detach(struct device *self, int flags)
214 {
215 struct btkbd_softc *sc = (struct btkbd_softc *)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 = (struct btkbd_softc *)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 = (struct btkbd_softc *)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, caddr_t data, int flag, struct lwp *l)
340 {
341 struct btkbd_softc *sc = (struct btkbd_softc *)self;
342
343 switch (cmd) {
344 case WSKBDIO_GTYPE:
345 *(int *)data = WSKBD_TYPE_BLUETOOTH;
346 break;
347
348 case WSKBDIO_SETLEDS:
349 btkbd_set_leds(sc, *(int *)data);
350 break;
351
352 case WSKBDIO_GETLEDS:
353 *(int *)data = sc->sc_leds;
354 break;
355
356 #ifdef WSDISPLAY_COMPAT_RAWKBD
357 case WSKBDIO_SETMODE:
358 sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
359 #ifdef BTKBD_REPEAT
360 callout_stop(&sc->sc_repeat);
361 #endif
362 break;
363 #endif
364
365 default:
366 return EPASSTHROUGH;
367 }
368
369 return 0;
370 }
371
372 /*****************************************************************************
373 *
374 * btkbd input routine, called from our parent
375 */
376
377 #ifdef WSDISPLAY_COMPAT_RAWKBD
378 #define NN 0 /* no translation */
379 /*
380 * Translate USB keycodes to US keyboard XT scancodes.
381 * Scancodes >= 0x80 represent EXTENDED keycodes.
382 *
383 * See http://www.microsoft.com/HWDEV/TECH/input/Scancode.asp
384 */
385 static const u_int8_t btkbd_trtab[256] = {
386 NN, NN, NN, NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
387 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
388 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
389 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
390 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
391 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
392 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
393 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
394 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */
395 0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
396 0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
397 0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
398 0x48, 0x49, 0x52, 0x53, 0x56, 0xdd, NN, 0x59, /* 60 - 67 */
399 0x5d, 0x5e, 0x5f, NN, NN, NN, NN, NN, /* 68 - 6f */
400 NN, NN, NN, NN, NN, NN, NN, NN, /* 70 - 77 */
401 NN, NN, NN, NN, NN, NN, NN, NN, /* 78 - 7f */
402 NN, NN, NN, NN, NN, 0x7e, NN, 0x73, /* 80 - 87 */
403 0x70, 0x7d, 0x79, 0x7b, 0x5c, NN, NN, NN, /* 88 - 8f */
404 NN, NN, 0x78, 0x77, 0x76, NN, NN, NN, /* 90 - 97 */
405 NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9f */
406 NN, NN, NN, NN, NN, NN, NN, NN, /* a0 - a7 */
407 NN, NN, NN, NN, NN, NN, NN, NN, /* a8 - af */
408 NN, NN, NN, NN, NN, NN, NN, NN, /* b0 - b7 */
409 NN, NN, NN, NN, NN, NN, NN, NN, /* b8 - bf */
410 NN, NN, NN, NN, NN, NN, NN, NN, /* c0 - c7 */
411 NN, NN, NN, NN, NN, NN, NN, NN, /* c8 - cf */
412 NN, NN, NN, NN, NN, NN, NN, NN, /* d0 - d7 */
413 NN, NN, NN, NN, NN, NN, NN, NN, /* d8 - df */
414 0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
415 NN, NN, NN, NN, NN, NN, NN, NN, /* e8 - ef */
416 NN, NN, NN, NN, NN, NN, NN, NN, /* f0 - f7 */
417 NN, NN, NN, NN, NN, NN, NN, NN, /* f8 - ff */
418 };
419 #endif
420
421 #define KEY_ERROR 0x01
422 #define PRESS 0x000
423 #define RELEASE 0x100
424 #define CODEMASK 0x0ff
425 #define ADDKEY(c) ibuf[nkeys++] = (c)
426 #define REP_DELAY1 400
427 #define REP_DELAYN 100
428
429 static void
430 btkbd_input(struct bthidev *self, uint8_t *data, int len)
431 {
432 struct btkbd_softc *sc = (struct btkbd_softc *)self;
433 struct btkbd_data *ud = &sc->sc_ndata;
434 uint16_t ibuf[MAXKEYS];
435 uint32_t mod, omod;
436 int nkeys, i, j;
437 int key;
438 int s;
439
440 if (sc->sc_wskbd == NULL || sc->sc_enabled == 0)
441 return;
442
443 /* extract key modifiers */
444 ud->modifiers = 0;
445 for (i = 0 ; i < sc->sc_nmod ; i++)
446 if (hid_get_data(data, &sc->sc_modloc[i]))
447 ud->modifiers |= sc->sc_mods[i].mask;
448
449 /* extract keycodes */
450 memcpy(ud->keycode, data + (sc->sc_keycodeloc.pos / 8),
451 sc->sc_nkeycode);
452
453 if (ud->keycode[0] == KEY_ERROR)
454 return; /* ignore */
455
456 nkeys = 0;
457 mod = ud->modifiers;
458 omod = sc->sc_odata.modifiers;
459 if (mod != omod)
460 for (i = 0 ; i < sc->sc_nmod ; i++)
461 if ((mod & sc->sc_mods[i].mask) !=
462 (omod & sc->sc_mods[i].mask))
463 ADDKEY(sc->sc_mods[i].key |
464 (mod & sc->sc_mods[i].mask
465 ? PRESS : RELEASE));
466
467 if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
468 /* Check for released keys. */
469 for (i = 0 ; i < sc->sc_nkeycode ; i++) {
470 key = sc->sc_odata.keycode[i];
471 if (key == 0)
472 continue;
473
474 for (j = 0 ; j < sc->sc_nkeycode ; j++)
475 if (key == ud->keycode[j])
476 goto rfound;
477
478 ADDKEY(key | RELEASE);
479
480 rfound:
481 ;
482 }
483
484 /* Check for pressed keys. */
485 for (i = 0 ; i < sc->sc_nkeycode ; i++) {
486 key = ud->keycode[i];
487 if (key == 0)
488 continue;
489
490 for (j = 0; j < sc->sc_nkeycode; j++)
491 if (key == sc->sc_odata.keycode[j])
492 goto pfound;
493
494 ADDKEY(key | PRESS);
495 pfound:
496 ;
497 }
498 }
499 sc->sc_odata = *ud;
500
501 if (nkeys == 0)
502 return;
503
504 #ifdef WSDISPLAY_COMPAT_RAWKBD
505 if (sc->sc_rawkbd) {
506 u_char cbuf[MAXKEYS * 2];
507 int c;
508 int npress;
509
510 for (npress = i = j = 0 ; i < nkeys ; i++) {
511 key = ibuf[i];
512 c = btkbd_trtab[key & CODEMASK];
513 if (c == NN)
514 continue;
515
516 if (c & 0x80)
517 cbuf[j++] = 0xe0;
518
519 cbuf[j] = c & 0x7f;
520 if (key & RELEASE)
521 cbuf[j] |= 0x80;
522 #ifdef BTKBD_REPEAT
523 else {
524 /* remember pressed keys for autorepeat */
525 if (c & 0x80)
526 sc->sc_rep[npress++] = 0xe0;
527
528 sc->sc_rep[npress++] = c & 0x7f;
529 }
530 #endif
531
532 j++;
533 }
534
535 s = spltty();
536 wskbd_rawinput(sc->sc_wskbd, cbuf, j);
537 splx(s);
538 #ifdef BTKBD_REPEAT
539 callout_stop(&sc->sc_repeat);
540 if (npress != 0) {
541 sc->sc_nrep = npress;
542 callout_schedule(&sc->sc_repeat, hz * REP_DELAY1 / 1000);
543 }
544 #endif
545 return;
546 }
547 #endif
548
549 s = spltty();
550 for (i = 0 ; i < nkeys ; i++) {
551 key = ibuf[i];
552 wskbd_input(sc->sc_wskbd,
553 key & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
554 key & CODEMASK);
555 }
556 splx(s);
557 }
558
559 #ifdef WSDISPLAY_COMPAT_RAWKBD
560 #ifdef BTKBD_REPEAT
561 static void
562 btkbd_repeat(void *arg)
563 {
564 struct btkbd_softc *sc = arg;
565 int s;
566
567 s = spltty();
568 wskbd_rawinput(sc->sc_wskbd, sc->sc_rep, sc->sc_nrep);
569 splx(s);
570 callout_schedule(&sc->sc_repeat, hz * REP_DELAYN / 1000);
571 }
572 #endif
573 #endif
574