xref: /netbsd-src/sys/dev/bluetooth/bthidev.c (revision 1921cb5602567cfc8401cc953be22fe9d74238f2)
1 /*	$NetBSD: bthidev.c,v 1.5 2006/10/04 19:23:59 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 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: bthidev.c,v 1.5 2006/10/04 19:23:59 plunky Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/conf.h>
39 #include <sys/device.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/proc.h>
46 #include <sys/systm.h>
47 
48 #include <prop/proplib.h>
49 
50 #include <netbt/bluetooth.h>
51 #include <netbt/l2cap.h>
52 
53 #include <dev/usb/hid.h>
54 #include <dev/bluetooth/btdev.h>
55 #include <dev/bluetooth/bthid.h>
56 #include <dev/bluetooth/bthidev.h>
57 
58 #include "locators.h"
59 
60 /*****************************************************************************
61  *
62  *	Bluetooth HID device
63  */
64 
65 #define MAX_DESCRIPTOR_LEN	1024		/* sanity check */
66 
67 /* bthidev softc */
68 struct bthidev_softc {
69 	struct btdev		sc_btdev;
70 	uint16_t		sc_state;
71 	uint16_t		sc_flags;
72 
73 	bdaddr_t		sc_laddr;	/* local address */
74 	bdaddr_t		sc_raddr;	/* remote address */
75 
76 	uint16_t		sc_ctlpsm;	/* control PSM */
77 	struct l2cap_channel	*sc_ctl;	/* control channel */
78 	struct l2cap_channel	*sc_ctl_l;	/* control listen */
79 
80 	uint16_t		sc_intpsm;	/* interrupt PSM */
81 	struct l2cap_channel	*sc_int;	/* interrupt channel */
82 	struct l2cap_channel	*sc_int_l;	/* interrupt listen */
83 
84 	LIST_HEAD(,bthidev)	sc_list;	/* child list */
85 
86 	struct callout		sc_reconnect;
87 	int			sc_attempts;	/* connection attempts */
88 };
89 
90 /* sc_flags */
91 #define BTHID_RECONNECT		(1 << 0)	/* reconnect on link loss */
92 #define BTHID_CONNECTING	(1 << 1)	/* we are connecting */
93 
94 /* device state */
95 #define BTHID_CLOSED		0
96 #define BTHID_WAIT_CTL		1
97 #define BTHID_WAIT_INT		2
98 #define BTHID_OPEN		3
99 #define BTHID_DETACHING		4
100 
101 #define	BTHID_RETRY_INTERVAL	5	/* seconds between connection attempts */
102 
103 /* bthidev internals */
104 static void bthidev_timeout(void *);
105 static int  bthidev_listen(struct bthidev_softc *);
106 static int  bthidev_connect(struct bthidev_softc *);
107 static int  bthidev_output(struct bthidev *, uint8_t *, int);
108 static void bthidev_null(struct bthidev *, uint8_t *, int);
109 
110 /* autoconf(9) glue */
111 static int  bthidev_match(struct device *, struct cfdata *, void *);
112 static void bthidev_attach(struct device *, struct device *, void *);
113 static int  bthidev_detach(struct device *, int);
114 static int  bthidev_print(void *, const char *);
115 
116 CFATTACH_DECL(bthidev, sizeof(struct bthidev_softc),
117     bthidev_match, bthidev_attach, bthidev_detach, NULL);
118 
119 /* bluetooth(9) protocol methods for L2CAP */
120 static void  bthidev_connecting(void *);
121 static void  bthidev_ctl_connected(void *);
122 static void  bthidev_int_connected(void *);
123 static void  bthidev_ctl_disconnected(void *, int);
124 static void  bthidev_int_disconnected(void *, int);
125 static void *bthidev_ctl_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
126 static void *bthidev_int_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
127 static void  bthidev_complete(void *, int);
128 static void  bthidev_input(void *, struct mbuf *);
129 
130 static const struct btproto bthidev_ctl_proto = {
131 	bthidev_connecting,
132 	bthidev_ctl_connected,
133 	bthidev_ctl_disconnected,
134 	bthidev_ctl_newconn,
135 	bthidev_complete,
136 	bthidev_input,
137 };
138 
139 static const struct btproto bthidev_int_proto = {
140 	bthidev_connecting,
141 	bthidev_int_connected,
142 	bthidev_int_disconnected,
143 	bthidev_int_newconn,
144 	bthidev_complete,
145 	bthidev_input,
146 };
147 
148 /*****************************************************************************
149  *
150  *	bthidev autoconf(9) routines
151  */
152 
153 static int
154 bthidev_match(struct device *self, struct cfdata *cfdata, void *aux)
155 {
156 	prop_dictionary_t dict = aux;
157 	prop_object_t obj;
158 
159 	obj = prop_dictionary_get(dict, BTDEVservice);
160 	if (prop_string_equals_cstring(obj, "HID"))
161 		return 1;
162 
163 	return 0;
164 }
165 
166 static void
167 bthidev_attach(struct device *parent, struct device *self, void *aux)
168 {
169 	struct bthidev_softc *sc = (struct bthidev_softc *)self;
170 	prop_dictionary_t dict = aux;
171 	prop_object_t obj;
172 	struct bthidev_attach_args bha;
173 	struct bthidev *dev;
174 	struct hid_data *d;
175 	struct hid_item h;
176 	const void *desc;
177 	int locs[BTHIDBUSCF_NLOCS];
178 	int maxid, rep, s, dlen;
179 
180 	/*
181 	 * Init softc
182 	 */
183 	LIST_INIT(&sc->sc_list);
184 	callout_init(&sc->sc_reconnect);
185 	callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc);
186 	sc->sc_state = BTHID_CLOSED;
187 	sc->sc_flags = BTHID_CONNECTING;
188 	sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL;
189 	sc->sc_intpsm = L2CAP_PSM_HID_INTR;
190 
191 	/*
192 	 * extract config from proplist
193 	 */
194 	obj = prop_dictionary_get(dict, BTDEVladdr);
195 	bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
196 
197 	obj = prop_dictionary_get(dict, BTDEVraddr);
198 	bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
199 
200 	obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
201 	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
202 		sc->sc_ctlpsm = prop_number_integer_value(obj);
203 		if (L2CAP_PSM_INVALID(sc->sc_ctlpsm)) {
204 			aprint_error(" invalid %s\n", BTHIDEVcontrolpsm);
205 			return;
206 		}
207 	}
208 
209 	obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
210 	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
211 		sc->sc_intpsm = prop_number_integer_value(obj);
212 		if (L2CAP_PSM_INVALID(sc->sc_intpsm)) {
213 			aprint_error(" invalid %s\n", BTHIDEVinterruptpsm);
214 			return;
215 		}
216 	}
217 
218 	obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
219 	if (prop_object_type(obj) == PROP_TYPE_DATA) {
220 		dlen = prop_data_size(obj);
221 		desc = prop_data_data_nocopy(obj);
222 	} else {
223 		aprint_error(" no %s\n", BTHIDEVdescriptor);
224 		return;
225 	}
226 
227 	obj = prop_dictionary_get(dict, BTHIDEVreconnect);
228 	if (prop_object_type(obj) == PROP_TYPE_BOOL
229 	    && !prop_bool_true(obj))
230 		sc->sc_flags |= BTHID_RECONNECT;
231 
232 	/*
233 	 * Parse the descriptor and attach child devices, one per report.
234 	 */
235 	maxid = -1;
236 	h.report_ID = 0;
237 	d = hid_start_parse(desc, dlen, hid_none);
238 	while (hid_get_item(d, &h)) {
239 		if (h.report_ID > maxid)
240 			maxid = h.report_ID;
241 	}
242 	hid_end_parse(d);
243 
244 	if (maxid < 0) {
245 		aprint_error(" no reports found\n");
246 		return;
247 	}
248 
249 	aprint_normal("\n");
250 
251 	for (rep = 0 ; rep <= maxid ; rep++) {
252 		if (hid_report_size(desc, dlen, hid_feature, rep) == 0
253 		    && hid_report_size(desc, dlen, hid_input, rep) == 0
254 		    && hid_report_size(desc, dlen, hid_output, rep) == 0)
255 			continue;
256 
257 		bha.ba_desc = desc;
258 		bha.ba_dlen = dlen;
259 		bha.ba_input = bthidev_null;
260 		bha.ba_feature = bthidev_null;
261 		bha.ba_output = bthidev_output;
262 		bha.ba_id = rep;
263 
264 		locs[BTHIDBUSCF_REPORTID] = rep;
265 
266 		dev = (struct bthidev *)config_found_sm_loc((struct device *)sc, "bthidbus",
267 					locs, &bha, bthidev_print, config_stdsubmatch);
268 		if (dev != NULL) {
269 			dev->sc_parent = (struct device *)sc;
270 			dev->sc_id = rep;
271 			dev->sc_input = bha.ba_input;
272 			dev->sc_feature = bha.ba_feature;
273 			LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
274 		}
275 	}
276 
277 	/*
278 	 * start bluetooth connections
279 	 */
280 	s = splsoftnet();
281 	if ((sc->sc_flags & BTHID_RECONNECT) == 0)
282 		bthidev_listen(sc);
283 
284 	if (sc->sc_flags & BTHID_CONNECTING)
285 		bthidev_connect(sc);
286 	splx(s);
287 }
288 
289 static int
290 bthidev_detach(struct device *self, int flags)
291 {
292 	struct bthidev_softc *sc = (struct bthidev_softc *)self;
293 	struct bthidev *dev;
294 	int s;
295 
296 	s = splsoftnet();
297 	sc->sc_flags = 0;	/* disable reconnecting */
298 
299 	/* release interrupt listen */
300 	if (sc->sc_int_l != NULL) {
301 		l2cap_detach(&sc->sc_int_l);
302 		sc->sc_int_l = NULL;
303 	}
304 
305 	/* release control listen */
306 	if (sc->sc_ctl_l != NULL) {
307 		l2cap_detach(&sc->sc_ctl_l);
308 		sc->sc_ctl_l = NULL;
309 	}
310 
311 	/* close interrupt channel */
312 	if (sc->sc_int != NULL) {
313 		l2cap_disconnect(sc->sc_int, 0);
314 		l2cap_detach(&sc->sc_int);
315 		sc->sc_int = NULL;
316 	}
317 
318 	/* close control channel */
319 	if (sc->sc_ctl != NULL) {
320 		l2cap_disconnect(sc->sc_ctl, 0);
321 		l2cap_detach(&sc->sc_ctl);
322 		sc->sc_ctl = NULL;
323 	}
324 
325 	/* remove callout */
326 	sc->sc_state = BTHID_DETACHING;
327 	callout_stop(&sc->sc_reconnect);
328 	if (callout_invoking(&sc->sc_reconnect))
329 		tsleep(sc, PWAIT, "bthidetach", 0);
330 
331 	splx(s);
332 
333 	/* detach children */
334 	while ((dev = LIST_FIRST(&sc->sc_list)) != NULL) {
335 		LIST_REMOVE(dev, sc_next);
336 		config_detach((struct device *)dev, flags);
337 	}
338 
339 	return 0;
340 }
341 
342 /*
343  * bthidev config print
344  */
345 static int
346 bthidev_print(void *aux, const char *pnp)
347 {
348 	struct bthidev_attach_args *ba = aux;
349 
350 	if (pnp != NULL)
351 		aprint_normal("%s:", pnp);
352 
353 	if (ba->ba_id > 0)
354 		aprint_normal(" reportid %d", ba->ba_id);
355 
356 	return UNCONF;
357 }
358 
359 /*****************************************************************************
360  *
361  *	bluetooth(4) HID attach/detach routines
362  */
363 
364 /*
365  * callouts are scheduled after connections have been lost, in order
366  * to clean up and reconnect.
367  */
368 static void
369 bthidev_timeout(void *arg)
370 {
371 	struct bthidev_softc *sc = arg;
372 	int s;
373 
374 	s = splsoftnet();
375 	callout_ack(&sc->sc_reconnect);
376 
377 	switch (sc->sc_state) {
378 	case BTHID_CLOSED:
379 		if (sc->sc_int != NULL) {
380 			l2cap_disconnect(sc->sc_int, 0);
381 			break;
382 		}
383 
384 		if (sc->sc_ctl != NULL) {
385 			l2cap_disconnect(sc->sc_ctl, 0);
386 			break;
387 		}
388 
389 		if (sc->sc_flags & BTHID_RECONNECT) {
390 			sc->sc_flags |= BTHID_CONNECTING;
391 			bthidev_connect(sc);
392 			break;
393 		}
394 
395 		break;
396 
397 	case BTHID_WAIT_CTL:
398 		break;
399 
400 	case BTHID_WAIT_INT:
401 		break;
402 
403 	case BTHID_OPEN:
404 		break;
405 
406 	case BTHID_DETACHING:
407 		wakeup(sc);
408 		break;
409 
410 	default:
411 		break;
412 	}
413 	splx(s);
414 }
415 
416 /*
417  * listen for our device
418  */
419 static int
420 bthidev_listen(struct bthidev_softc *sc)
421 {
422 	struct sockaddr_bt sa;
423 	int err;
424 
425 	memset(&sa, 0, sizeof(sa));
426 	sa.bt_len = sizeof(sa);
427 	sa.bt_family = AF_BLUETOOTH;
428 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
429 
430 	/*
431 	 * Listen on control PSM
432 	 */
433 	err = l2cap_attach(&sc->sc_ctl_l, &bthidev_ctl_proto, sc);
434 	if (err)
435 		return err;
436 
437 	sa.bt_psm = sc->sc_ctlpsm;
438 	err = l2cap_bind(sc->sc_ctl_l, &sa);
439 	if (err)
440 		return err;
441 
442 	err = l2cap_listen(sc->sc_ctl_l);
443 	if (err)
444 		return err;
445 
446 	/*
447 	 * Listen on interrupt PSM
448 	 */
449 	err = l2cap_attach(&sc->sc_int_l, &bthidev_int_proto, sc);
450 	if (err)
451 		return err;
452 
453 	sa.bt_psm = sc->sc_intpsm;
454 	err = l2cap_bind(sc->sc_int_l, &sa);
455 	if (err)
456 		return err;
457 
458 	err = l2cap_listen(sc->sc_int_l);
459 	if (err)
460 		return err;
461 
462 	sc->sc_state = BTHID_WAIT_CTL;
463 	return 0;
464 }
465 
466 /*
467  * start connecting to our device
468  */
469 static int
470 bthidev_connect(struct bthidev_softc *sc)
471 {
472 	struct sockaddr_bt sa;
473 	int err;
474 
475 	if (sc->sc_attempts++ > 0)
476 		printf("%s: connect (#%d)\n",
477 			device_xname((struct device *)sc), sc->sc_attempts);
478 
479 	memset(&sa, 0, sizeof(sa));
480 	sa.bt_len = sizeof(sa);
481 	sa.bt_family = AF_BLUETOOTH;
482 
483 	err = l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
484 	if (err) {
485 		printf("%s: l2cap_attach failed (%d)\n",
486 			device_xname((struct device *)sc), err);
487 		return err;
488 	}
489 
490 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
491 	err = l2cap_bind(sc->sc_ctl, &sa);
492 	if (err) {
493 		printf("%s: l2cap_bind failed (%d)\n",
494 			device_xname((struct device *)sc), err);
495 		return err;
496 	}
497 
498 	sa.bt_psm = sc->sc_ctlpsm;
499 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
500 	err = l2cap_connect(sc->sc_ctl, &sa);
501 	if (err) {
502 		printf("%s: l2cap_connect failed (%d)\n",
503 			device_xname((struct device *)sc), err);
504 		return err;
505 	}
506 
507 	sc->sc_state = BTHID_WAIT_CTL;
508 	return 0;
509 }
510 
511 /*****************************************************************************
512  *
513  *	bluetooth(9) callback methods for L2CAP
514  *
515  *	All these are called from Bluetooth Protocol code, in a soft
516  *	interrupt context at IPL_SOFTNET.
517  */
518 
519 static void
520 bthidev_connecting(void *arg)
521 {
522 
523 	/* dont care */
524 }
525 
526 static void
527 bthidev_ctl_connected(void *arg)
528 {
529 	struct sockaddr_bt sa;
530 	struct bthidev_softc *sc = arg;
531 	int err;
532 
533 	if (sc->sc_state != BTHID_WAIT_CTL)
534 		return;
535 
536 	KASSERT(sc->sc_ctl != NULL);
537 	KASSERT(sc->sc_int == NULL);
538 
539 	if (sc->sc_flags & BTHID_CONNECTING) {
540 		/* initiate connect on interrupt PSM */
541 		err = l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
542 		if (err)
543 			goto fail;
544 
545 		memset(&sa, 0, sizeof(sa));
546 		sa.bt_len = sizeof(sa);
547 		sa.bt_family = AF_BLUETOOTH;
548 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
549 
550 		err = l2cap_bind(sc->sc_int, &sa);
551 		if (err)
552 			goto fail;
553 
554 		sa.bt_psm = sc->sc_intpsm;
555 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
556 		err = l2cap_connect(sc->sc_int, &sa);
557 		if (err)
558 			goto fail;
559 	}
560 
561 	sc->sc_state = BTHID_WAIT_INT;
562 	return;
563 
564 fail:
565 	l2cap_detach(&sc->sc_ctl);
566 	sc->sc_ctl = NULL;
567 
568 	printf("%s: connect failed (%d)\n",
569 		device_xname((struct device *)sc), err);
570 }
571 
572 static void
573 bthidev_int_connected(void *arg)
574 {
575 	struct bthidev_softc *sc = arg;
576 
577 	if (sc->sc_state != BTHID_WAIT_INT)
578 		return;
579 
580 	KASSERT(sc->sc_ctl != NULL);
581 	KASSERT(sc->sc_int != NULL);
582 
583 	sc->sc_attempts = 0;
584 	sc->sc_flags &= ~BTHID_CONNECTING;
585 	sc->sc_state = BTHID_OPEN;
586 
587 	printf("%s: connected\n", device_xname((struct device *)sc));
588 }
589 
590 /*
591  * Disconnected
592  *
593  * Depending on our state, this could mean several things, but essentially
594  * we are lost. If both channels are closed, and we are marked to reconnect,
595  * schedule another try otherwise just give up. They will contact us.
596  */
597 static void
598 bthidev_ctl_disconnected(void *arg, int err)
599 {
600 	struct bthidev_softc *sc = arg;
601 
602 	if (sc->sc_ctl != NULL) {
603 		l2cap_detach(&sc->sc_ctl);
604 		sc->sc_ctl = NULL;
605 	}
606 
607 	sc->sc_state = BTHID_CLOSED;
608 
609 	if (sc->sc_int == NULL) {
610 		printf("%s: disconnected\n",
611 			device_xname((struct device *)sc));
612 		sc->sc_flags &= ~BTHID_CONNECTING;
613 
614 		if (sc->sc_flags & BTHID_RECONNECT)
615 			callout_schedule(&sc->sc_reconnect,
616 					BTHID_RETRY_INTERVAL * hz);
617 		else
618 			sc->sc_state = BTHID_WAIT_CTL;
619 	} else {
620 		/*
621 		 * The interrupt channel should have been closed first,
622 		 * but its potentially unsafe to detach that from here.
623 		 * Give them a second to do the right thing or let the
624 		 * callout handle it.
625 		 */
626 		callout_schedule(&sc->sc_reconnect, hz);
627 	}
628 }
629 
630 static void
631 bthidev_int_disconnected(void *arg, int err)
632 {
633 	struct bthidev_softc *sc = arg;
634 
635 	if (sc->sc_int != NULL) {
636 		l2cap_detach(&sc->sc_int);
637 		sc->sc_int = NULL;
638 	}
639 
640 	sc->sc_state = BTHID_CLOSED;
641 
642 	if (sc->sc_ctl == NULL) {
643 		printf("%s: disconnected\n",
644 			device_xname((struct device *)sc));
645 		sc->sc_flags &= ~BTHID_CONNECTING;
646 
647 		if (sc->sc_flags & BTHID_RECONNECT)
648 			callout_schedule(&sc->sc_reconnect,
649 					BTHID_RETRY_INTERVAL * hz);
650 		else
651 			sc->sc_state = BTHID_WAIT_CTL;
652 	} else {
653 		/*
654 		 * The control channel should be closing also, allow
655 		 * them a chance to do that before we force it.
656 		 */
657 		callout_schedule(&sc->sc_reconnect, hz);
658 	}
659 }
660 
661 /*
662  * New Connections
663  *
664  * We give a new L2CAP handle back if this matches the BDADDR we are
665  * listening for and we are in the right state. bthidev_connected will
666  * be called when the connection is open, so nothing else to do here
667  */
668 static void *
669 bthidev_ctl_newconn(void *arg, struct sockaddr_bt *laddr, struct sockaddr_bt *raddr)
670 {
671 	struct bthidev_softc *sc = arg;
672 
673 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
674 	    || (sc->sc_flags & BTHID_CONNECTING)
675 	    || sc->sc_state != BTHID_WAIT_CTL
676 	    || sc->sc_ctl != NULL
677 	    || sc->sc_int != NULL)
678 		return NULL;
679 
680 	l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
681 	return sc->sc_ctl;
682 }
683 
684 static void *
685 bthidev_int_newconn(void *arg, struct sockaddr_bt *laddr, struct sockaddr_bt *raddr)
686 {
687 	struct bthidev_softc *sc = arg;
688 
689 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
690 	    || (sc->sc_flags & BTHID_CONNECTING)
691 	    || sc->sc_state != BTHID_WAIT_INT
692 	    || sc->sc_ctl == NULL
693 	    || sc->sc_int != NULL)
694 		return NULL;
695 
696 	l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
697 	return sc->sc_int;
698 }
699 
700 static void
701 bthidev_complete(void *arg, int count)
702 {
703 
704 	/* dont care */
705 }
706 
707 /*
708  * Receive reports from the protocol stack.
709  */
710 static void
711 bthidev_input(void *arg, struct mbuf *m)
712 {
713 	struct bthidev_softc *sc = arg;
714 	struct bthidev *dev;
715 	uint8_t *data;
716 	int len;
717 
718 	if (sc->sc_state != BTHID_OPEN)
719 		goto release;
720 
721 	if (m->m_pkthdr.len > m->m_len)
722 		printf("%s: truncating HID report\n",
723 			device_xname((struct device *)sc));
724 
725 	len = m->m_len;
726 	data = mtod(m, uint8_t *);
727 
728 	if (BTHID_TYPE(data[0]) == BTHID_DATA) {
729 		/*
730 		 * data[0] == type / parameter
731 		 * data[1] == id
732 		 * data[2..len] == report
733 		 */
734 		if (len < 3)
735 			goto release;
736 
737 		LIST_FOREACH(dev, &sc->sc_list, sc_next) {
738 			if (data[1] == dev->sc_id) {
739 				switch (BTHID_DATA_PARAM(data[0])) {
740 				case BTHID_DATA_INPUT:
741 					(*dev->sc_input)(dev, data + 2, len - 2);
742 					break;
743 
744 				case BTHID_DATA_FEATURE:
745 					(*dev->sc_feature)(dev, data + 2, len - 2);
746 					break;
747 
748 				default:
749 					break;
750 				}
751 
752 				goto release;
753 			}
754 		}
755 		printf("%s: report id %d, len = %d ignored\n",
756 			device_xname((struct device *)sc), data[1], len - 2);
757 
758 		goto release;
759 	}
760 
761 	if (BTHID_TYPE(data[0]) == BTHID_CONTROL) {
762 		if (len < 1)
763 			goto release;
764 
765 		if (BTHID_DATA_PARAM(data[0]) == BTHID_CONTROL_UNPLUG) {
766 			printf("%s: unplugged\n",
767 				device_xname((struct device *)sc));
768 
769 			/* close interrupt channel */
770 			if (sc->sc_int != NULL) {
771 				l2cap_disconnect(sc->sc_int, 0);
772 				l2cap_detach(&sc->sc_int);
773 				sc->sc_int = NULL;
774 			}
775 
776 			/* close control channel */
777 			if (sc->sc_ctl != NULL) {
778 				l2cap_disconnect(sc->sc_ctl, 0);
779 				l2cap_detach(&sc->sc_ctl);
780 				sc->sc_ctl = NULL;
781 			}
782 		}
783 
784 		goto release;
785 	}
786 
787 release:
788 	m_freem(m);
789 }
790 
791 /*****************************************************************************
792  *
793  *	IO routines
794  */
795 
796 static void
797 bthidev_null(struct bthidev *dev, uint8_t *report, int len)
798 {
799 
800 	/*
801 	 * empty routine just in case the device
802 	 * provided no method to handle this report
803 	 */
804 }
805 
806 static int
807 bthidev_output(struct bthidev *dev, uint8_t *report, int rlen)
808 {
809 	struct bthidev_softc *sc = (struct bthidev_softc *)dev->sc_parent;
810 	struct mbuf *m;
811 	int s, err;
812 
813 	if (sc == NULL || sc->sc_state != BTHID_OPEN)
814 		return ENOTCONN;
815 
816 	KASSERT(sc->sc_ctl != NULL);
817 	KASSERT(sc->sc_int != NULL);
818 
819 	if (rlen == 0 || report == NULL)
820 		return 0;
821 
822 	if (rlen > MHLEN - 2) {
823 		printf("%s: output report too long (%d)!\n",
824 				device_xname((struct device *)sc), rlen);
825 
826 		return EMSGSIZE;
827 	}
828 
829 	m = m_gethdr(M_DONTWAIT, MT_DATA);
830 	if (m == NULL)
831 		return ENOMEM;
832 
833 	/*
834 	 * data[0] = type / parameter
835 	 * data[1] = id
836 	 * data[2..N] = report
837 	 */
838 	mtod(m, uint8_t *)[0] = (uint8_t)((BTHID_DATA << 4) | BTHID_DATA_OUTPUT);
839 	mtod(m, uint8_t *)[1] = dev->sc_id;
840 	memcpy(mtod(m, uint8_t *) + 2, report, rlen);
841 	m->m_pkthdr.len = m->m_len = rlen + 2;
842 
843 	s = splsoftnet();
844 	err = l2cap_send(sc->sc_int, m);
845 	splx(s);
846 
847 	return err;
848 }
849