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