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