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