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