Home | History | Annotate | Line # | Download | only in bluetooth
bthidev.c revision 1.3
      1 /*	$NetBSD: bthidev.c,v 1.3 2006/09/10 15:45:56 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.3 2006/09/10 15:45:56 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 /* btdev identity matcher */
    120 static int bthidev_identify(struct btdev *, prop_dictionary_t);
    121 
    122 /* bluetooth(9) protocol methods for L2CAP */
    123 static void  bthidev_connecting(void *);
    124 static void  bthidev_ctl_connected(void *);
    125 static void  bthidev_int_connected(void *);
    126 static void  bthidev_ctl_disconnected(void *, int);
    127 static void  bthidev_int_disconnected(void *, int);
    128 static void *bthidev_ctl_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    129 static void *bthidev_int_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    130 static void  bthidev_complete(void *, int);
    131 static void  bthidev_input(void *, struct mbuf *);
    132 
    133 static const struct btproto bthidev_ctl_proto = {
    134 	bthidev_connecting,
    135 	bthidev_ctl_connected,
    136 	bthidev_ctl_disconnected,
    137 	bthidev_ctl_newconn,
    138 	bthidev_complete,
    139 	bthidev_input,
    140 };
    141 
    142 static const struct btproto bthidev_int_proto = {
    143 	bthidev_connecting,
    144 	bthidev_int_connected,
    145 	bthidev_int_disconnected,
    146 	bthidev_int_newconn,
    147 	bthidev_complete,
    148 	bthidev_input,
    149 };
    150 
    151 /*****************************************************************************
    152  *
    153  *	bthidev autoconf(9) routines
    154  */
    155 
    156 static int
    157 bthidev_match(struct device *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, BTDEVtype);
    163 	return prop_string_equals_cstring(obj, "bthidev");
    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 	sc->sc_btdev.sc_identify = bthidev_identify;
    192 
    193 	/*
    194 	 * extract config from proplist
    195 	 */
    196 	obj = prop_dictionary_get(dict, BTDEVladdr);
    197 	bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
    198 
    199 	obj = prop_dictionary_get(dict, BTDEVraddr);
    200 	bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
    201 
    202 	obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
    203 	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
    204 		sc->sc_ctlpsm = prop_number_integer_value(obj);
    205 		if (L2CAP_PSM_INVALID(sc->sc_ctlpsm)) {
    206 			aprint_error(" invalid %s\n", BTHIDEVcontrolpsm);
    207 			return;
    208 		}
    209 	}
    210 
    211 	obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
    212 	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
    213 		sc->sc_intpsm = prop_number_integer_value(obj);
    214 		if (L2CAP_PSM_INVALID(sc->sc_intpsm)) {
    215 			aprint_error(" invalid %s\n", BTHIDEVinterruptpsm);
    216 			return;
    217 		}
    218 	}
    219 
    220 	obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
    221 	if (prop_object_type(obj) == PROP_TYPE_DATA) {
    222 		dlen = prop_data_size(obj);
    223 		desc = prop_data_data_nocopy(obj);
    224 	} else {
    225 		aprint_error(" no %s\n", BTHIDEVdescriptor);
    226 		return;
    227 	}
    228 
    229 	obj = prop_dictionary_get(dict, BTHIDEVreconnect);
    230 	if (prop_object_type(obj) == PROP_TYPE_BOOL
    231 	    && !prop_bool_true(obj))
    232 		sc->sc_flags |= BTHID_RECONNECT;
    233 
    234 	/*
    235 	 * Parse the descriptor and attach child devices, one per report.
    236 	 */
    237 	maxid = -1;
    238 	h.report_ID = 0;
    239 	d = hid_start_parse(desc, dlen, hid_none);
    240 	while (hid_get_item(d, &h)) {
    241 		if (h.report_ID > maxid)
    242 			maxid = h.report_ID;
    243 	}
    244 	hid_end_parse(d);
    245 
    246 	if (maxid < 0) {
    247 		aprint_error(" no reports found\n");
    248 		return;
    249 	}
    250 
    251 	aprint_normal("\n");
    252 
    253 	for (rep = 0 ; rep <= maxid ; rep++) {
    254 		if (hid_report_size(desc, dlen, hid_feature, rep) == 0
    255 		    && hid_report_size(desc, dlen, hid_input, rep) == 0
    256 		    && hid_report_size(desc, dlen, hid_output, rep) == 0)
    257 			continue;
    258 
    259 		bha.ba_desc = desc;
    260 		bha.ba_dlen = dlen;
    261 		bha.ba_input = bthidev_null;
    262 		bha.ba_feature = bthidev_null;
    263 		bha.ba_output = bthidev_output;
    264 		bha.ba_id = rep;
    265 
    266 		locs[BTHIDBUSCF_REPORTID] = rep;
    267 
    268 		dev = (struct bthidev *)config_found_sm_loc((struct device *)sc, "bthidbus",
    269 					locs, &bha, bthidev_print, config_stdsubmatch);
    270 		if (dev != NULL) {
    271 			dev->sc_parent = (struct device *)sc;
    272 			dev->sc_id = rep;
    273 			dev->sc_input = bha.ba_input;
    274 			dev->sc_feature = bha.ba_feature;
    275 			LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
    276 		}
    277 	}
    278 
    279 	/*
    280 	 * start bluetooth connections
    281 	 */
    282 	s = splsoftnet();
    283 	if ((sc->sc_flags & BTHID_RECONNECT) == 0)
    284 		bthidev_listen(sc);
    285 
    286 	if (sc->sc_flags & BTHID_CONNECTING)
    287 		bthidev_connect(sc);
    288 	splx(s);
    289 }
    290 
    291 static int
    292 bthidev_detach(struct device *self, int flags)
    293 {
    294 	struct bthidev_softc *sc = (struct bthidev_softc *)self;
    295 	struct bthidev *dev;
    296 	int s;
    297 
    298 	s = splsoftnet();
    299 	sc->sc_flags = 0;	/* disable reconnecting */
    300 
    301 	/* release interrupt listen */
    302 	if (sc->sc_int_l != NULL) {
    303 		l2cap_detach(&sc->sc_int_l);
    304 		sc->sc_int_l = NULL;
    305 	}
    306 
    307 	/* release control listen */
    308 	if (sc->sc_ctl_l != NULL) {
    309 		l2cap_detach(&sc->sc_ctl_l);
    310 		sc->sc_ctl_l = NULL;
    311 	}
    312 
    313 	/* close interrupt channel */
    314 	if (sc->sc_int != NULL) {
    315 		l2cap_disconnect(sc->sc_int, 0);
    316 		l2cap_detach(&sc->sc_int);
    317 		sc->sc_int = NULL;
    318 	}
    319 
    320 	/* close control channel */
    321 	if (sc->sc_ctl != NULL) {
    322 		l2cap_disconnect(sc->sc_ctl, 0);
    323 		l2cap_detach(&sc->sc_ctl);
    324 		sc->sc_ctl = NULL;
    325 	}
    326 
    327 	/* remove callout */
    328 	sc->sc_state = BTHID_DETACHING;
    329 	callout_stop(&sc->sc_reconnect);
    330 	if (callout_invoking(&sc->sc_reconnect))
    331 		tsleep(sc, PWAIT, "bthidetach", 0);
    332 
    333 	splx(s);
    334 
    335 	/* detach children */
    336 	while ((dev = LIST_FIRST(&sc->sc_list)) != NULL) {
    337 		LIST_REMOVE(dev, sc_next);
    338 		config_detach((struct device *)dev, flags);
    339 	}
    340 
    341 	return 0;
    342 }
    343 
    344 /*
    345  * bthidev config print
    346  */
    347 static int
    348 bthidev_print(void *aux, const char *pnp)
    349 {
    350 	struct bthidev_attach_args *ba = aux;
    351 
    352 	if (pnp != NULL)
    353 		aprint_normal("%s:", pnp);
    354 
    355 	if (ba->ba_id > 0)
    356 		aprint_normal(" reportid %d", ba->ba_id);
    357 
    358 	return UNCONF;
    359 }
    360 
    361 /*
    362  * btdev identity matcher
    363  */
    364 static int
    365 bthidev_identify(struct btdev *dev, prop_dictionary_t dict)
    366 {
    367 	struct bthidev_softc *sc = (struct bthidev_softc *)dev;
    368 	prop_object_t obj;
    369 
    370 	obj = prop_dictionary_get(dict, BTDEVtype);
    371 	if (!prop_string_equals_cstring(obj, "bthidev"))
    372 		return 0;
    373 
    374 	obj = prop_dictionary_get(dict, BTDEVladdr);
    375 	if (!prop_data_equals_data(obj, &sc->sc_laddr, sizeof(bdaddr_t)))
    376 		return 0;
    377 
    378 	obj = prop_dictionary_get(dict, BTDEVraddr);
    379 	if (!prop_data_equals_data(obj, &sc->sc_raddr, sizeof(bdaddr_t)))
    380 		return 0;
    381 
    382 	obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
    383 	if (!prop_number_equals_integer(obj, sc->sc_ctlpsm))
    384 		return 0;
    385 
    386 	obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
    387 	if (!prop_number_equals_integer(obj, sc->sc_intpsm))
    388 		return 0;
    389 
    390 	return 1;
    391 }
    392 
    393 /*****************************************************************************
    394  *
    395  *	bluetooth(4) HID attach/detach routines
    396  */
    397 
    398 /*
    399  * callouts are scheduled to initiate outgoing connections
    400  * after the connection has been lost.
    401  */
    402 static void
    403 bthidev_timeout(void *arg)
    404 {
    405 	struct bthidev_softc *sc = arg;
    406 	int s, err;
    407 
    408 	s = splsoftnet();
    409 	callout_ack(&sc->sc_reconnect);
    410 
    411 	switch (sc->sc_state) {
    412 	case BTHID_CLOSED:
    413 		sc->sc_flags |= BTHID_CONNECTING;
    414 		err = bthidev_connect(sc);
    415 		if (err)
    416 			printf("%s: connect failed (%d)\n",
    417 				device_xname((struct device *)sc), err);
    418 		break;
    419 
    420 	case BTHID_WAIT_CTL:
    421 		break;
    422 
    423 	case BTHID_WAIT_INT:
    424 		break;
    425 
    426 	case BTHID_OPEN:
    427 		break;
    428 
    429 	case BTHID_DETACHING:
    430 		wakeup(sc);
    431 		break;
    432 
    433 	default:
    434 		break;
    435 	}
    436 	splx(s);
    437 }
    438 
    439 /*
    440  * listen for our device
    441  */
    442 static int
    443 bthidev_listen(struct bthidev_softc *sc)
    444 {
    445 	struct sockaddr_bt sa;
    446 	int err;
    447 
    448 	memset(&sa, 0, sizeof(sa));
    449 	sa.bt_len = sizeof(sa);
    450 	sa.bt_family = AF_BLUETOOTH;
    451 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    452 
    453 	/*
    454 	 * Listen on control PSM
    455 	 */
    456 	err = l2cap_attach(&sc->sc_ctl_l, &bthidev_ctl_proto, sc);
    457 	if (err)
    458 		return err;
    459 
    460 	sa.bt_psm = sc->sc_ctlpsm;
    461 	err = l2cap_bind(sc->sc_ctl_l, &sa);
    462 	if (err)
    463 		return err;
    464 
    465 	err = l2cap_listen(sc->sc_ctl_l);
    466 	if (err)
    467 		return err;
    468 
    469 	/*
    470 	 * Listen on interrupt PSM
    471 	 */
    472 	err = l2cap_attach(&sc->sc_int_l, &bthidev_int_proto, sc);
    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 		printf("%s: connect (#%d)\n",
    500 			device_xname((struct device *)sc), sc->sc_attempts);
    501 
    502 	memset(&sa, 0, sizeof(sa));
    503 	sa.bt_len = sizeof(sa);
    504 	sa.bt_family = AF_BLUETOOTH;
    505 
    506 	err = l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
    507 	if (err) {
    508 		printf("%s: l2cap_attach failed (%d)\n",
    509 			device_xname((struct device *)sc), err);
    510 		return err;
    511 	}
    512 
    513 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    514 	err = l2cap_bind(sc->sc_ctl, &sa);
    515 	if (err) {
    516 		printf("%s: l2cap_bind failed (%d)\n",
    517 			device_xname((struct device *)sc), err);
    518 		return err;
    519 	}
    520 
    521 	sa.bt_psm = sc->sc_ctlpsm;
    522 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    523 	err = l2cap_connect(sc->sc_ctl, &sa);
    524 	if (err) {
    525 		printf("%s: l2cap_connect failed (%d)\n",
    526 			device_xname((struct device *)sc), 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 		memset(&sa, 0, sizeof(sa));
    569 		sa.bt_len = sizeof(sa);
    570 		sa.bt_family = AF_BLUETOOTH;
    571 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    572 
    573 		err = l2cap_bind(sc->sc_int, &sa);
    574 		if (err)
    575 			goto fail;
    576 
    577 		sa.bt_psm = sc->sc_intpsm;
    578 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    579 		err = l2cap_connect(sc->sc_int, &sa);
    580 		if (err)
    581 			goto fail;
    582 	}
    583 
    584 	sc->sc_state = BTHID_WAIT_INT;
    585 	return;
    586 
    587 fail:
    588 	l2cap_detach(&sc->sc_ctl);
    589 	sc->sc_ctl = NULL;
    590 
    591 	printf("%s: connect failed (%d)\n",
    592 		device_xname((struct device *)sc), err);
    593 }
    594 
    595 static void
    596 bthidev_int_connected(void *arg)
    597 {
    598 	struct bthidev_softc *sc = arg;
    599 
    600 	if (sc->sc_state != BTHID_WAIT_INT)
    601 		return;
    602 
    603 	KASSERT(sc->sc_ctl != NULL);
    604 	KASSERT(sc->sc_int != NULL);
    605 
    606 	sc->sc_attempts = 0;
    607 	sc->sc_flags &= ~BTHID_CONNECTING;
    608 	sc->sc_state = BTHID_OPEN;
    609 
    610 	printf("%s: connected\n", device_xname((struct device *)sc));
    611 }
    612 
    613 /*
    614  * Disconnected
    615  *
    616  * Depending on our state, this could mean several things, but essentially
    617  * we are lost. If both channels are closed, and we are marked to reconnect,
    618  * schedule another try otherwise just give up. They will contact us.
    619  */
    620 static void
    621 bthidev_ctl_disconnected(void *arg, int err)
    622 {
    623 	struct bthidev_softc *sc = arg;
    624 
    625 	if (sc->sc_ctl != NULL) {
    626 		l2cap_detach(&sc->sc_ctl);
    627 		sc->sc_ctl = NULL;
    628 	}
    629 
    630 	sc->sc_state = BTHID_CLOSED;
    631 
    632 	if (sc->sc_int == NULL) {
    633 		printf("%s: disconnected\n",
    634 			device_xname((struct device *)sc));
    635 		sc->sc_flags &= ~BTHID_CONNECTING;
    636 
    637 		if (sc->sc_flags & BTHID_RECONNECT)
    638 			callout_schedule(&sc->sc_reconnect,
    639 					BTHID_RETRY_INTERVAL * hz);
    640 		else
    641 			sc->sc_state = BTHID_WAIT_CTL;
    642 	} else {
    643 		/*
    644 		 * The interrupt channel should have been closed first,
    645 		 * so if its still up then kick it along..
    646 		 */
    647 		l2cap_disconnect(sc->sc_int, 0);
    648 	}
    649 }
    650 
    651 static void
    652 bthidev_int_disconnected(void *arg, int err)
    653 {
    654 	struct bthidev_softc *sc = arg;
    655 
    656 	if (sc->sc_int != NULL) {
    657 		l2cap_detach(&sc->sc_int);
    658 		sc->sc_int = NULL;
    659 	}
    660 
    661 	sc->sc_state = BTHID_CLOSED;
    662 
    663 	if (sc->sc_ctl == NULL) {
    664 		printf("%s: disconnected\n",
    665 			device_xname((struct device *)sc));
    666 		sc->sc_flags &= ~BTHID_CONNECTING;
    667 
    668 		if (sc->sc_flags & BTHID_RECONNECT)
    669 			callout_schedule(&sc->sc_reconnect,
    670 					BTHID_RETRY_INTERVAL * hz);
    671 		else
    672 			sc->sc_state = BTHID_WAIT_CTL;
    673 	}
    674 }
    675 
    676 /*
    677  * New Connections
    678  *
    679  * We give a new L2CAP handle back if this matches the BDADDR we are
    680  * listening for and we are in the right state. bthidev_connected will
    681  * be called when the connection is open, so nothing else to do here
    682  */
    683 static void *
    684 bthidev_ctl_newconn(void *arg, struct sockaddr_bt *laddr, struct sockaddr_bt *raddr)
    685 {
    686 	struct bthidev_softc *sc = arg;
    687 
    688 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
    689 	    || (sc->sc_flags & BTHID_CONNECTING)
    690 	    || sc->sc_state != BTHID_WAIT_CTL
    691 	    || sc->sc_ctl != NULL
    692 	    || sc->sc_int != NULL)
    693 		return NULL;
    694 
    695 	l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
    696 	return sc->sc_ctl;
    697 }
    698 
    699 static void *
    700 bthidev_int_newconn(void *arg, struct sockaddr_bt *laddr, struct sockaddr_bt *raddr)
    701 {
    702 	struct bthidev_softc *sc = arg;
    703 
    704 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
    705 	    || (sc->sc_flags & BTHID_CONNECTING)
    706 	    || sc->sc_state != BTHID_WAIT_INT
    707 	    || sc->sc_ctl == NULL
    708 	    || sc->sc_int != NULL)
    709 		return NULL;
    710 
    711 	l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
    712 	return sc->sc_int;
    713 }
    714 
    715 static void
    716 bthidev_complete(void *arg, int count)
    717 {
    718 
    719 	/* dont care */
    720 }
    721 
    722 /*
    723  * Receive reports from the protocol stack.
    724  */
    725 static void
    726 bthidev_input(void *arg, struct mbuf *m)
    727 {
    728 	struct bthidev_softc *sc = arg;
    729 	struct bthidev *dev;
    730 	uint8_t *data;
    731 	int len;
    732 
    733 	if (sc->sc_state != BTHID_OPEN)
    734 		goto release;
    735 
    736 	if (m->m_pkthdr.len > m->m_len)
    737 		printf("%s: truncating HID report\n",
    738 			device_xname((struct device *)sc));
    739 
    740 	len = m->m_len;
    741 	data = mtod(m, uint8_t *);
    742 
    743 	if (BTHID_TYPE(data[0]) == BTHID_DATA) {
    744 		/*
    745 		 * data[0] == type / parameter
    746 		 * data[1] == id
    747 		 * data[2..len] == report
    748 		 */
    749 		if (len < 3)
    750 			goto release;
    751 
    752 		LIST_FOREACH(dev, &sc->sc_list, sc_next) {
    753 			if (data[1] == dev->sc_id) {
    754 				switch (BTHID_DATA_PARAM(data[0])) {
    755 				case BTHID_DATA_INPUT:
    756 					(*dev->sc_input)(dev, data + 2, len - 2);
    757 					break;
    758 
    759 				case BTHID_DATA_FEATURE:
    760 					(*dev->sc_feature)(dev, data + 2, len - 2);
    761 					break;
    762 
    763 				default:
    764 					break;
    765 				}
    766 
    767 				goto release;
    768 			}
    769 		}
    770 		printf("%s: report id %d, len = %d ignored\n",
    771 			device_xname((struct device *)sc), data[1], len - 2);
    772 
    773 		goto release;
    774 	}
    775 
    776 	if (BTHID_TYPE(data[0]) == BTHID_CONTROL) {
    777 		if (len < 1)
    778 			goto release;
    779 
    780 		if (BTHID_DATA_PARAM(data[0]) == BTHID_CONTROL_UNPLUG) {
    781 			printf("%s: unplugged\n",
    782 				device_xname((struct device *)sc));
    783 
    784 			/* close interrupt channel */
    785 			if (sc->sc_int != NULL) {
    786 				l2cap_disconnect(sc->sc_int, 0);
    787 				l2cap_detach(&sc->sc_int);
    788 				sc->sc_int = NULL;
    789 			}
    790 
    791 			/* close control channel */
    792 			if (sc->sc_ctl != NULL) {
    793 				l2cap_disconnect(sc->sc_ctl, 0);
    794 				l2cap_detach(&sc->sc_ctl);
    795 				sc->sc_ctl = NULL;
    796 			}
    797 		}
    798 
    799 		goto release;
    800 	}
    801 
    802 release:
    803 	m_freem(m);
    804 }
    805 
    806 /*****************************************************************************
    807  *
    808  *	IO routines
    809  */
    810 
    811 static void
    812 bthidev_null(struct bthidev *dev, uint8_t *report, int len)
    813 {
    814 
    815 	/*
    816 	 * empty routine just in case the device
    817 	 * provided no method to handle this report
    818 	 */
    819 }
    820 
    821 static int
    822 bthidev_output(struct bthidev *dev, uint8_t *report, int rlen)
    823 {
    824 	struct bthidev_softc *sc = (struct bthidev_softc *)dev->sc_parent;
    825 	struct mbuf *m;
    826 	int s, err;
    827 
    828 	if (sc == NULL || sc->sc_state != BTHID_OPEN)
    829 		return ENOTCONN;
    830 
    831 	KASSERT(sc->sc_ctl != NULL);
    832 	KASSERT(sc->sc_int != NULL);
    833 
    834 	if (rlen == 0 || report == NULL)
    835 		return 0;
    836 
    837 	if (rlen > MHLEN - 2) {
    838 		printf("%s: output report too long (%d)!\n",
    839 				device_xname((struct device *)sc), rlen);
    840 
    841 		return EMSGSIZE;
    842 	}
    843 
    844 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    845 	if (m == NULL)
    846 		return ENOMEM;
    847 
    848 	/*
    849 	 * data[0] = type / parameter
    850 	 * data[1] = id
    851 	 * data[2..N] = report
    852 	 */
    853 	mtod(m, uint8_t *)[0] = (uint8_t)((BTHID_DATA << 4) | BTHID_DATA_OUTPUT);
    854 	mtod(m, uint8_t *)[1] = dev->sc_id;
    855 	memcpy(mtod(m, uint8_t *) + 2, report, rlen);
    856 	m->m_pkthdr.len = m->m_len = rlen + 2;
    857 
    858 	s = splsoftnet();
    859 	err = l2cap_send(sc->sc_int, m);
    860 	splx(s);
    861 
    862 	return err;
    863 }
    864