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