Home | History | Annotate | Line # | Download | only in bluetooth
bthidev.c revision 1.7.4.1
      1 /*	$NetBSD: bthidev.c,v 1.7.4.1 2007/05/07 10:55:25 yamt 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.7.4.1 2007/05/07 10:55:25 yamt 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 	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 	struct callout		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(struct device *, struct cfdata *, void *);
    113 static void bthidev_attach(struct device *, struct device *, void *);
    114 static int  bthidev_detach(struct device *, int);
    115 static int  bthidev_print(void *, const char *);
    116 
    117 CFATTACH_DECL(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(struct device *self, struct cfdata *cfdata,
    159     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(struct device *parent, struct device *self, void *aux)
    173 {
    174 	struct bthidev_softc *sc = (struct bthidev_softc *)self;
    175 	prop_dictionary_t dict = aux;
    176 	prop_object_t obj;
    177 	struct bthidev_attach_args bha;
    178 	struct bthidev *dev;
    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 	LIST_INIT(&sc->sc_list);
    189 	callout_init(&sc->sc_reconnect);
    190 	callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc);
    191 	sc->sc_state = BTHID_CLOSED;
    192 	sc->sc_flags = BTHID_CONNECTING;
    193 	sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL;
    194 	sc->sc_intpsm = L2CAP_PSM_HID_INTR;
    195 
    196 	/*
    197 	 * extract config from proplist
    198 	 */
    199 	obj = prop_dictionary_get(dict, BTDEVladdr);
    200 	bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
    201 
    202 	obj = prop_dictionary_get(dict, BTDEVraddr);
    203 	bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
    204 
    205 	obj = prop_dictionary_get(dict, BTDEVmode);
    206 	if (prop_object_type(obj) == PROP_TYPE_STRING) {
    207 		if (prop_string_equals_cstring(obj, BTDEVauth))
    208 			sc->sc_mode = L2CAP_LM_AUTH;
    209 		else if (prop_string_equals_cstring(obj, BTDEVencrypt))
    210 			sc->sc_mode = L2CAP_LM_ENCRYPT;
    211 		else if (prop_string_equals_cstring(obj, BTDEVsecure))
    212 			sc->sc_mode = L2CAP_LM_SECURE;
    213 		else  {
    214 			aprint_error(" unknown %s\n", BTDEVmode);
    215 			return;
    216 		}
    217 
    218 		aprint_verbose(" %s %s", BTDEVmode,
    219 					 prop_string_cstring_nocopy(obj));
    220 	}
    221 
    222 	obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
    223 	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
    224 		sc->sc_ctlpsm = prop_number_integer_value(obj);
    225 		if (L2CAP_PSM_INVALID(sc->sc_ctlpsm)) {
    226 			aprint_error(" invalid %s\n", BTHIDEVcontrolpsm);
    227 			return;
    228 		}
    229 	}
    230 
    231 	obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
    232 	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
    233 		sc->sc_intpsm = prop_number_integer_value(obj);
    234 		if (L2CAP_PSM_INVALID(sc->sc_intpsm)) {
    235 			aprint_error(" invalid %s\n", BTHIDEVinterruptpsm);
    236 			return;
    237 		}
    238 	}
    239 
    240 	obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
    241 	if (prop_object_type(obj) == PROP_TYPE_DATA) {
    242 		dlen = prop_data_size(obj);
    243 		desc = prop_data_data_nocopy(obj);
    244 	} else {
    245 		aprint_error(" no %s\n", BTHIDEVdescriptor);
    246 		return;
    247 	}
    248 
    249 	obj = prop_dictionary_get(dict, BTHIDEVreconnect);
    250 	if (prop_object_type(obj) == PROP_TYPE_BOOL
    251 	    && !prop_bool_true(obj))
    252 		sc->sc_flags |= BTHID_RECONNECT;
    253 
    254 	/*
    255 	 * Parse the descriptor and attach child devices, one per report.
    256 	 */
    257 	maxid = -1;
    258 	h.report_ID = 0;
    259 	d = hid_start_parse(desc, dlen, hid_none);
    260 	while (hid_get_item(d, &h)) {
    261 		if (h.report_ID > maxid)
    262 			maxid = h.report_ID;
    263 	}
    264 	hid_end_parse(d);
    265 
    266 	if (maxid < 0) {
    267 		aprint_error(" no reports found\n");
    268 		return;
    269 	}
    270 
    271 	aprint_normal("\n");
    272 
    273 	for (rep = 0 ; rep <= maxid ; rep++) {
    274 		if (hid_report_size(desc, dlen, hid_feature, rep) == 0
    275 		    && hid_report_size(desc, dlen, hid_input, rep) == 0
    276 		    && hid_report_size(desc, dlen, hid_output, rep) == 0)
    277 			continue;
    278 
    279 		bha.ba_desc = desc;
    280 		bha.ba_dlen = dlen;
    281 		bha.ba_input = bthidev_null;
    282 		bha.ba_feature = bthidev_null;
    283 		bha.ba_output = bthidev_output;
    284 		bha.ba_id = rep;
    285 
    286 		locs[BTHIDBUSCF_REPORTID] = rep;
    287 
    288 		dev = (struct bthidev *)config_found_sm_loc((struct device *)sc, "bthidbus",
    289 					locs, &bha, bthidev_print, config_stdsubmatch);
    290 		if (dev != NULL) {
    291 			dev->sc_parent = (struct device *)sc;
    292 			dev->sc_id = rep;
    293 			dev->sc_input = bha.ba_input;
    294 			dev->sc_feature = bha.ba_feature;
    295 			LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
    296 		}
    297 	}
    298 
    299 	/*
    300 	 * start bluetooth connections
    301 	 */
    302 	s = splsoftnet();
    303 	if ((sc->sc_flags & BTHID_RECONNECT) == 0)
    304 		bthidev_listen(sc);
    305 
    306 	if (sc->sc_flags & BTHID_CONNECTING)
    307 		bthidev_connect(sc);
    308 	splx(s);
    309 }
    310 
    311 static int
    312 bthidev_detach(struct device *self, int flags)
    313 {
    314 	struct bthidev_softc *sc = (struct bthidev_softc *)self;
    315 	struct bthidev *dev;
    316 	int s;
    317 
    318 	s = splsoftnet();
    319 	sc->sc_flags = 0;	/* disable reconnecting */
    320 
    321 	/* release interrupt listen */
    322 	if (sc->sc_int_l != NULL) {
    323 		l2cap_detach(&sc->sc_int_l);
    324 		sc->sc_int_l = NULL;
    325 	}
    326 
    327 	/* release control listen */
    328 	if (sc->sc_ctl_l != NULL) {
    329 		l2cap_detach(&sc->sc_ctl_l);
    330 		sc->sc_ctl_l = NULL;
    331 	}
    332 
    333 	/* close interrupt channel */
    334 	if (sc->sc_int != NULL) {
    335 		l2cap_disconnect(sc->sc_int, 0);
    336 		l2cap_detach(&sc->sc_int);
    337 		sc->sc_int = NULL;
    338 	}
    339 
    340 	/* close control channel */
    341 	if (sc->sc_ctl != NULL) {
    342 		l2cap_disconnect(sc->sc_ctl, 0);
    343 		l2cap_detach(&sc->sc_ctl);
    344 		sc->sc_ctl = NULL;
    345 	}
    346 
    347 	/* remove callout */
    348 	sc->sc_state = BTHID_DETACHING;
    349 	callout_stop(&sc->sc_reconnect);
    350 	if (callout_invoking(&sc->sc_reconnect))
    351 		tsleep(sc, PWAIT, "bthidetach", 0);
    352 
    353 	splx(s);
    354 
    355 	/* detach children */
    356 	while ((dev = LIST_FIRST(&sc->sc_list)) != NULL) {
    357 		LIST_REMOVE(dev, sc_next);
    358 		config_detach((struct device *)dev, flags);
    359 	}
    360 
    361 	return 0;
    362 }
    363 
    364 /*
    365  * bthidev config print
    366  */
    367 static int
    368 bthidev_print(void *aux, const char *pnp)
    369 {
    370 	struct bthidev_attach_args *ba = aux;
    371 
    372 	if (pnp != NULL)
    373 		aprint_normal("%s:", pnp);
    374 
    375 	if (ba->ba_id > 0)
    376 		aprint_normal(" reportid %d", ba->ba_id);
    377 
    378 	return UNCONF;
    379 }
    380 
    381 /*****************************************************************************
    382  *
    383  *	bluetooth(4) HID attach/detach routines
    384  */
    385 
    386 /*
    387  * callouts are scheduled after connections have been lost, in order
    388  * to clean up and reconnect.
    389  */
    390 static void
    391 bthidev_timeout(void *arg)
    392 {
    393 	struct bthidev_softc *sc = arg;
    394 	int s;
    395 
    396 	s = splsoftnet();
    397 	callout_ack(&sc->sc_reconnect);
    398 
    399 	switch (sc->sc_state) {
    400 	case BTHID_CLOSED:
    401 		if (sc->sc_int != NULL) {
    402 			l2cap_disconnect(sc->sc_int, 0);
    403 			break;
    404 		}
    405 
    406 		if (sc->sc_ctl != NULL) {
    407 			l2cap_disconnect(sc->sc_ctl, 0);
    408 			break;
    409 		}
    410 
    411 		if (sc->sc_flags & BTHID_RECONNECT) {
    412 			sc->sc_flags |= BTHID_CONNECTING;
    413 			bthidev_connect(sc);
    414 			break;
    415 		}
    416 
    417 		break;
    418 
    419 	case BTHID_WAIT_CTL:
    420 		break;
    421 
    422 	case BTHID_WAIT_INT:
    423 		break;
    424 
    425 	case BTHID_OPEN:
    426 		break;
    427 
    428 	case BTHID_DETACHING:
    429 		wakeup(sc);
    430 		break;
    431 
    432 	default:
    433 		break;
    434 	}
    435 	splx(s);
    436 }
    437 
    438 /*
    439  * listen for our device
    440  */
    441 static int
    442 bthidev_listen(struct bthidev_softc *sc)
    443 {
    444 	struct sockaddr_bt sa;
    445 	int err;
    446 
    447 	memset(&sa, 0, sizeof(sa));
    448 	sa.bt_len = sizeof(sa);
    449 	sa.bt_family = AF_BLUETOOTH;
    450 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    451 
    452 	/*
    453 	 * Listen on control PSM
    454 	 */
    455 	err = l2cap_attach(&sc->sc_ctl_l, &bthidev_ctl_proto, sc);
    456 	if (err)
    457 		return err;
    458 
    459 	err = l2cap_setopt(sc->sc_ctl_l, SO_L2CAP_LM, &sc->sc_mode);
    460 	if (err)
    461 		return err;
    462 
    463 	sa.bt_psm = sc->sc_ctlpsm;
    464 	err = l2cap_bind(sc->sc_ctl_l, &sa);
    465 	if (err)
    466 		return err;
    467 
    468 	err = l2cap_listen(sc->sc_ctl_l);
    469 	if (err)
    470 		return err;
    471 
    472 	/*
    473 	 * Listen on interrupt PSM
    474 	 */
    475 	err = l2cap_attach(&sc->sc_int_l, &bthidev_int_proto, sc);
    476 	if (err)
    477 		return err;
    478 
    479 	err = l2cap_setopt(sc->sc_int_l, SO_L2CAP_LM, &sc->sc_mode);
    480 	if (err)
    481 		return err;
    482 
    483 	sa.bt_psm = sc->sc_intpsm;
    484 	err = l2cap_bind(sc->sc_int_l, &sa);
    485 	if (err)
    486 		return err;
    487 
    488 	err = l2cap_listen(sc->sc_int_l);
    489 	if (err)
    490 		return err;
    491 
    492 	sc->sc_state = BTHID_WAIT_CTL;
    493 	return 0;
    494 }
    495 
    496 /*
    497  * start connecting to our device
    498  */
    499 static int
    500 bthidev_connect(struct bthidev_softc *sc)
    501 {
    502 	struct sockaddr_bt sa;
    503 	int err;
    504 
    505 	if (sc->sc_attempts++ > 0)
    506 		printf("%s: connect (#%d)\n",
    507 			device_xname((struct device *)sc), 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 		printf("%s: l2cap_attach failed (%d)\n",
    516 			device_xname((struct device *)sc), err);
    517 		return err;
    518 	}
    519 
    520 	err = l2cap_setopt(sc->sc_ctl, SO_L2CAP_LM, &sc->sc_mode);
    521 	if (err)
    522 		return err;
    523 
    524 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    525 	err = l2cap_bind(sc->sc_ctl, &sa);
    526 	if (err) {
    527 		printf("%s: l2cap_bind failed (%d)\n",
    528 			device_xname((struct device *)sc), 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 		printf("%s: l2cap_connect failed (%d)\n",
    537 			device_xname((struct device *)sc), 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",
    607 		device_xname((struct device *)sc), 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 	printf("%s: connected\n", device_xname((struct device *)sc));
    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 		printf("%s: disconnected\n",
    649 			device_xname((struct device *)sc));
    650 		sc->sc_flags &= ~BTHID_CONNECTING;
    651 
    652 		if (sc->sc_flags & BTHID_RECONNECT)
    653 			callout_schedule(&sc->sc_reconnect,
    654 					BTHID_RETRY_INTERVAL * hz);
    655 		else
    656 			sc->sc_state = BTHID_WAIT_CTL;
    657 	} else {
    658 		/*
    659 		 * The interrupt channel should have been closed first,
    660 		 * but its potentially unsafe to detach that from here.
    661 		 * Give them a second to do the right thing or let the
    662 		 * callout handle it.
    663 		 */
    664 		callout_schedule(&sc->sc_reconnect, hz);
    665 	}
    666 }
    667 
    668 static void
    669 bthidev_int_disconnected(void *arg, int err)
    670 {
    671 	struct bthidev_softc *sc = arg;
    672 
    673 	if (sc->sc_int != NULL) {
    674 		l2cap_detach(&sc->sc_int);
    675 		sc->sc_int = NULL;
    676 	}
    677 
    678 	sc->sc_state = BTHID_CLOSED;
    679 
    680 	if (sc->sc_ctl == NULL) {
    681 		printf("%s: disconnected\n",
    682 			device_xname((struct device *)sc));
    683 		sc->sc_flags &= ~BTHID_CONNECTING;
    684 
    685 		if (sc->sc_flags & BTHID_RECONNECT)
    686 			callout_schedule(&sc->sc_reconnect,
    687 					BTHID_RETRY_INTERVAL * hz);
    688 		else
    689 			sc->sc_state = BTHID_WAIT_CTL;
    690 	} else {
    691 		/*
    692 		 * The control channel should be closing also, allow
    693 		 * them a chance to do that before we force it.
    694 		 */
    695 		callout_schedule(&sc->sc_reconnect, hz);
    696 	}
    697 }
    698 
    699 /*
    700  * New Connections
    701  *
    702  * We give a new L2CAP handle back if this matches the BDADDR we are
    703  * listening for and we are in the right state. bthidev_connected will
    704  * be called when the connection is open, so nothing else to do here
    705  */
    706 static void *
    707 bthidev_ctl_newconn(void *arg, struct sockaddr_bt *laddr,
    708     struct sockaddr_bt *raddr)
    709 {
    710 	struct bthidev_softc *sc = arg;
    711 
    712 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
    713 	    || (sc->sc_flags & BTHID_CONNECTING)
    714 	    || sc->sc_state != BTHID_WAIT_CTL
    715 	    || sc->sc_ctl != NULL
    716 	    || sc->sc_int != NULL)
    717 		return NULL;
    718 
    719 	l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
    720 	return sc->sc_ctl;
    721 }
    722 
    723 static void *
    724 bthidev_int_newconn(void *arg, struct sockaddr_bt *laddr,
    725     struct sockaddr_bt *raddr)
    726 {
    727 	struct bthidev_softc *sc = arg;
    728 
    729 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
    730 	    || (sc->sc_flags & BTHID_CONNECTING)
    731 	    || sc->sc_state != BTHID_WAIT_INT
    732 	    || sc->sc_ctl == NULL
    733 	    || sc->sc_int != NULL)
    734 		return NULL;
    735 
    736 	l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
    737 	return sc->sc_int;
    738 }
    739 
    740 static void
    741 bthidev_complete(void *arg, int count)
    742 {
    743 
    744 	/* dont care */
    745 }
    746 
    747 static void
    748 bthidev_linkmode(void *arg, int new)
    749 {
    750 	struct bthidev_softc *sc = arg;
    751 
    752 	if ((sc->sc_mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
    753 		printf("%s: auth failed\n", device_xname((struct device *)sc));
    754 	else if ((sc->sc_mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
    755 		printf("%s: encrypt off\n", device_xname((struct device *)sc));
    756 	else if ((sc->sc_mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE))
    757 		printf("%s: insecure\n", device_xname((struct device *)sc));
    758 	else
    759 		return;
    760 
    761 	if (sc->sc_int != NULL)
    762 		l2cap_disconnect(sc->sc_int, 0);
    763 
    764 	if (sc->sc_ctl != NULL)
    765 		l2cap_disconnect(sc->sc_ctl, 0);
    766 }
    767 
    768 /*
    769  * Receive reports from the protocol stack.
    770  */
    771 static void
    772 bthidev_input(void *arg, struct mbuf *m)
    773 {
    774 	struct bthidev_softc *sc = arg;
    775 	struct bthidev *dev;
    776 	uint8_t *data;
    777 	int len;
    778 
    779 	if (sc->sc_state != BTHID_OPEN)
    780 		goto release;
    781 
    782 	if (m->m_pkthdr.len > m->m_len)
    783 		printf("%s: truncating HID report\n",
    784 			device_xname((struct device *)sc));
    785 
    786 	len = m->m_len;
    787 	data = mtod(m, uint8_t *);
    788 
    789 	if (BTHID_TYPE(data[0]) == BTHID_DATA) {
    790 		/*
    791 		 * data[0] == type / parameter
    792 		 * data[1] == id
    793 		 * data[2..len] == report
    794 		 */
    795 		if (len < 3)
    796 			goto release;
    797 
    798 		LIST_FOREACH(dev, &sc->sc_list, sc_next) {
    799 			if (data[1] == dev->sc_id) {
    800 				switch (BTHID_DATA_PARAM(data[0])) {
    801 				case BTHID_DATA_INPUT:
    802 					(*dev->sc_input)(dev, data + 2, len - 2);
    803 					break;
    804 
    805 				case BTHID_DATA_FEATURE:
    806 					(*dev->sc_feature)(dev, data + 2, len - 2);
    807 					break;
    808 
    809 				default:
    810 					break;
    811 				}
    812 
    813 				goto release;
    814 			}
    815 		}
    816 		printf("%s: report id %d, len = %d ignored\n",
    817 			device_xname((struct device *)sc), data[1], len - 2);
    818 
    819 		goto release;
    820 	}
    821 
    822 	if (BTHID_TYPE(data[0]) == BTHID_CONTROL) {
    823 		if (len < 1)
    824 			goto release;
    825 
    826 		if (BTHID_DATA_PARAM(data[0]) == BTHID_CONTROL_UNPLUG) {
    827 			printf("%s: unplugged\n",
    828 				device_xname((struct device *)sc));
    829 
    830 			/* close interrupt channel */
    831 			if (sc->sc_int != NULL) {
    832 				l2cap_disconnect(sc->sc_int, 0);
    833 				l2cap_detach(&sc->sc_int);
    834 				sc->sc_int = NULL;
    835 			}
    836 
    837 			/* close control channel */
    838 			if (sc->sc_ctl != NULL) {
    839 				l2cap_disconnect(sc->sc_ctl, 0);
    840 				l2cap_detach(&sc->sc_ctl);
    841 				sc->sc_ctl = NULL;
    842 			}
    843 		}
    844 
    845 		goto release;
    846 	}
    847 
    848 release:
    849 	m_freem(m);
    850 }
    851 
    852 /*****************************************************************************
    853  *
    854  *	IO routines
    855  */
    856 
    857 static void
    858 bthidev_null(struct bthidev *dev, uint8_t *report,
    859     int len)
    860 {
    861 
    862 	/*
    863 	 * empty routine just in case the device
    864 	 * provided no method to handle this report
    865 	 */
    866 }
    867 
    868 static int
    869 bthidev_output(struct bthidev *dev, uint8_t *report, int rlen)
    870 {
    871 	struct bthidev_softc *sc = (struct bthidev_softc *)dev->sc_parent;
    872 	struct mbuf *m;
    873 	int s, err;
    874 
    875 	if (sc == NULL || sc->sc_state != BTHID_OPEN)
    876 		return ENOTCONN;
    877 
    878 	KASSERT(sc->sc_ctl != NULL);
    879 	KASSERT(sc->sc_int != NULL);
    880 
    881 	if (rlen == 0 || report == NULL)
    882 		return 0;
    883 
    884 	if (rlen > MHLEN - 2) {
    885 		printf("%s: output report too long (%d)!\n",
    886 				device_xname((struct device *)sc), rlen);
    887 
    888 		return EMSGSIZE;
    889 	}
    890 
    891 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    892 	if (m == NULL)
    893 		return ENOMEM;
    894 
    895 	/*
    896 	 * data[0] = type / parameter
    897 	 * data[1] = id
    898 	 * data[2..N] = report
    899 	 */
    900 	mtod(m, uint8_t *)[0] = (uint8_t)((BTHID_DATA << 4) | BTHID_DATA_OUTPUT);
    901 	mtod(m, uint8_t *)[1] = dev->sc_id;
    902 	memcpy(mtod(m, uint8_t *) + 2, report, rlen);
    903 	m->m_pkthdr.len = m->m_len = rlen + 2;
    904 
    905 	s = splsoftnet();
    906 	err = l2cap_send(sc->sc_int, m);
    907 	splx(s);
    908 
    909 	return err;
    910 }
    911