Home | History | Annotate | Line # | Download | only in bluetooth
bthidev.c revision 1.13.14.3
      1  1.13.14.1       mjf /*	$NetBSD: bthidev.c,v 1.13.14.3 2008/09/28 10:40:19 mjf Exp $	*/
      2        1.1   gdamore 
      3        1.1   gdamore /*-
      4        1.1   gdamore  * Copyright (c) 2006 Itronix Inc.
      5        1.1   gdamore  * All rights reserved.
      6        1.1   gdamore  *
      7        1.1   gdamore  * Written by Iain Hibbert for Itronix Inc.
      8        1.1   gdamore  *
      9        1.1   gdamore  * Redistribution and use in source and binary forms, with or without
     10        1.1   gdamore  * modification, are permitted provided that the following conditions
     11        1.1   gdamore  * are met:
     12        1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     13        1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     14        1.1   gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1   gdamore  *    notice, this list of conditions and the following disclaimer in the
     16        1.1   gdamore  *    documentation and/or other materials provided with the distribution.
     17        1.1   gdamore  * 3. The name of Itronix Inc. may not be used to endorse
     18        1.1   gdamore  *    or promote products derived from this software without specific
     19        1.1   gdamore  *    prior written permission.
     20        1.1   gdamore  *
     21        1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22        1.1   gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23        1.1   gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24        1.1   gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25        1.1   gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26        1.1   gdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27        1.1   gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28        1.1   gdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29        1.1   gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30        1.1   gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31        1.1   gdamore  * POSSIBILITY OF SUCH DAMAGE.
     32        1.1   gdamore  */
     33        1.1   gdamore 
     34        1.1   gdamore #include <sys/cdefs.h>
     35  1.13.14.1       mjf __KERNEL_RCSID(0, "$NetBSD: bthidev.c,v 1.13.14.3 2008/09/28 10:40:19 mjf Exp $");
     36        1.1   gdamore 
     37        1.1   gdamore #include <sys/param.h>
     38        1.1   gdamore #include <sys/conf.h>
     39        1.1   gdamore #include <sys/device.h>
     40        1.1   gdamore #include <sys/fcntl.h>
     41        1.1   gdamore #include <sys/kernel.h>
     42        1.1   gdamore #include <sys/queue.h>
     43        1.1   gdamore #include <sys/malloc.h>
     44        1.1   gdamore #include <sys/mbuf.h>
     45        1.1   gdamore #include <sys/proc.h>
     46  1.13.14.3       mjf #include <sys/socketvar.h>
     47        1.1   gdamore #include <sys/systm.h>
     48        1.1   gdamore 
     49        1.2      tron #include <prop/proplib.h>
     50        1.2      tron 
     51        1.1   gdamore #include <netbt/bluetooth.h>
     52        1.1   gdamore #include <netbt/l2cap.h>
     53        1.1   gdamore 
     54        1.1   gdamore #include <dev/usb/hid.h>
     55        1.1   gdamore #include <dev/bluetooth/btdev.h>
     56        1.1   gdamore #include <dev/bluetooth/bthid.h>
     57        1.1   gdamore #include <dev/bluetooth/bthidev.h>
     58        1.1   gdamore 
     59        1.1   gdamore #include "locators.h"
     60        1.1   gdamore 
     61        1.1   gdamore /*****************************************************************************
     62        1.1   gdamore  *
     63        1.1   gdamore  *	Bluetooth HID device
     64        1.1   gdamore  */
     65        1.1   gdamore 
     66        1.1   gdamore #define MAX_DESCRIPTOR_LEN	1024		/* sanity check */
     67        1.1   gdamore 
     68        1.1   gdamore /* bthidev softc */
     69        1.1   gdamore struct bthidev_softc {
     70        1.1   gdamore 	uint16_t		sc_state;
     71        1.1   gdamore 	uint16_t		sc_flags;
     72       1.12    plunky 	device_t		sc_dev;
     73        1.1   gdamore 
     74        1.1   gdamore 	bdaddr_t		sc_laddr;	/* local address */
     75        1.1   gdamore 	bdaddr_t		sc_raddr;	/* remote address */
     76  1.13.14.3       mjf 	struct sockopt		sc_mode;	/* link mode sockopt */
     77        1.1   gdamore 
     78        1.1   gdamore 	uint16_t		sc_ctlpsm;	/* control PSM */
     79        1.1   gdamore 	struct l2cap_channel	*sc_ctl;	/* control channel */
     80        1.1   gdamore 	struct l2cap_channel	*sc_ctl_l;	/* control listen */
     81        1.1   gdamore 
     82        1.1   gdamore 	uint16_t		sc_intpsm;	/* interrupt PSM */
     83        1.1   gdamore 	struct l2cap_channel	*sc_int;	/* interrupt channel */
     84        1.1   gdamore 	struct l2cap_channel	*sc_int_l;	/* interrupt listen */
     85        1.1   gdamore 
     86        1.1   gdamore 	LIST_HEAD(,bthidev)	sc_list;	/* child list */
     87        1.1   gdamore 
     88       1.11    plunky 	callout_t		sc_reconnect;
     89        1.1   gdamore 	int			sc_attempts;	/* connection attempts */
     90        1.1   gdamore };
     91        1.1   gdamore 
     92        1.2      tron /* sc_flags */
     93        1.2      tron #define BTHID_RECONNECT		(1 << 0)	/* reconnect on link loss */
     94        1.2      tron #define BTHID_CONNECTING	(1 << 1)	/* we are connecting */
     95        1.2      tron 
     96        1.1   gdamore /* device state */
     97        1.1   gdamore #define BTHID_CLOSED		0
     98        1.1   gdamore #define BTHID_WAIT_CTL		1
     99        1.1   gdamore #define BTHID_WAIT_INT		2
    100        1.1   gdamore #define BTHID_OPEN		3
    101        1.1   gdamore 
    102        1.1   gdamore #define	BTHID_RETRY_INTERVAL	5	/* seconds between connection attempts */
    103        1.1   gdamore 
    104        1.1   gdamore /* bthidev internals */
    105        1.1   gdamore static void bthidev_timeout(void *);
    106        1.1   gdamore static int  bthidev_listen(struct bthidev_softc *);
    107        1.1   gdamore static int  bthidev_connect(struct bthidev_softc *);
    108        1.1   gdamore static int  bthidev_output(struct bthidev *, uint8_t *, int);
    109        1.1   gdamore static void bthidev_null(struct bthidev *, uint8_t *, int);
    110        1.1   gdamore 
    111        1.1   gdamore /* autoconf(9) glue */
    112       1.10    plunky static int  bthidev_match(device_t, struct cfdata *, void *);
    113       1.10    plunky static void bthidev_attach(device_t, device_t, void *);
    114       1.10    plunky static int  bthidev_detach(device_t, int);
    115        1.1   gdamore static int  bthidev_print(void *, const char *);
    116        1.1   gdamore 
    117       1.10    plunky CFATTACH_DECL_NEW(bthidev, sizeof(struct bthidev_softc),
    118        1.1   gdamore     bthidev_match, bthidev_attach, bthidev_detach, NULL);
    119        1.1   gdamore 
    120        1.1   gdamore /* bluetooth(9) protocol methods for L2CAP */
    121        1.1   gdamore static void  bthidev_connecting(void *);
    122        1.1   gdamore static void  bthidev_ctl_connected(void *);
    123        1.1   gdamore static void  bthidev_int_connected(void *);
    124        1.1   gdamore static void  bthidev_ctl_disconnected(void *, int);
    125        1.1   gdamore static void  bthidev_int_disconnected(void *, int);
    126        1.1   gdamore static void *bthidev_ctl_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    127        1.1   gdamore static void *bthidev_int_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    128        1.1   gdamore static void  bthidev_complete(void *, int);
    129        1.8    plunky static void  bthidev_linkmode(void *, int);
    130        1.1   gdamore static void  bthidev_input(void *, struct mbuf *);
    131        1.1   gdamore 
    132        1.1   gdamore static const struct btproto bthidev_ctl_proto = {
    133        1.1   gdamore 	bthidev_connecting,
    134        1.1   gdamore 	bthidev_ctl_connected,
    135        1.1   gdamore 	bthidev_ctl_disconnected,
    136        1.1   gdamore 	bthidev_ctl_newconn,
    137        1.1   gdamore 	bthidev_complete,
    138        1.8    plunky 	bthidev_linkmode,
    139        1.1   gdamore 	bthidev_input,
    140        1.1   gdamore };
    141        1.1   gdamore 
    142        1.1   gdamore static const struct btproto bthidev_int_proto = {
    143        1.1   gdamore 	bthidev_connecting,
    144        1.1   gdamore 	bthidev_int_connected,
    145        1.1   gdamore 	bthidev_int_disconnected,
    146        1.1   gdamore 	bthidev_int_newconn,
    147        1.1   gdamore 	bthidev_complete,
    148        1.8    plunky 	bthidev_linkmode,
    149        1.1   gdamore 	bthidev_input,
    150        1.1   gdamore };
    151        1.1   gdamore 
    152        1.1   gdamore /*****************************************************************************
    153        1.1   gdamore  *
    154        1.1   gdamore  *	bthidev autoconf(9) routines
    155        1.1   gdamore  */
    156        1.1   gdamore 
    157        1.1   gdamore static int
    158       1.10    plunky bthidev_match(device_t self, struct cfdata *cfdata, void *aux)
    159        1.1   gdamore {
    160        1.2      tron 	prop_dictionary_t dict = aux;
    161        1.2      tron 	prop_object_t obj;
    162        1.1   gdamore 
    163        1.5    plunky 	obj = prop_dictionary_get(dict, BTDEVservice);
    164        1.5    plunky 	if (prop_string_equals_cstring(obj, "HID"))
    165        1.5    plunky 		return 1;
    166        1.5    plunky 
    167        1.5    plunky 	return 0;
    168        1.1   gdamore }
    169        1.1   gdamore 
    170        1.1   gdamore static void
    171       1.10    plunky bthidev_attach(device_t parent, device_t self, void *aux)
    172        1.1   gdamore {
    173       1.10    plunky 	struct bthidev_softc *sc = device_private(self);
    174        1.2      tron 	prop_dictionary_t dict = aux;
    175        1.2      tron 	prop_object_t obj;
    176       1.10    plunky 	device_t dev;
    177        1.1   gdamore 	struct bthidev_attach_args bha;
    178       1.10    plunky 	struct bthidev *hidev;
    179        1.1   gdamore 	struct hid_data *d;
    180        1.1   gdamore 	struct hid_item h;
    181        1.2      tron 	const void *desc;
    182        1.1   gdamore 	int locs[BTHIDBUSCF_NLOCS];
    183  1.13.14.2       mjf 	int maxid, rep, dlen;
    184        1.1   gdamore 
    185        1.1   gdamore 	/*
    186        1.1   gdamore 	 * Init softc
    187        1.1   gdamore 	 */
    188       1.12    plunky 	sc->sc_dev = self;
    189        1.1   gdamore 	LIST_INIT(&sc->sc_list);
    190        1.9        ad 	callout_init(&sc->sc_reconnect, 0);
    191        1.1   gdamore 	callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc);
    192        1.1   gdamore 	sc->sc_state = BTHID_CLOSED;
    193        1.2      tron 	sc->sc_flags = BTHID_CONNECTING;
    194        1.2      tron 	sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL;
    195        1.2      tron 	sc->sc_intpsm = L2CAP_PSM_HID_INTR;
    196        1.1   gdamore 
    197  1.13.14.3       mjf 	sockopt_init(&sc->sc_mode, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
    198  1.13.14.3       mjf 
    199        1.1   gdamore 	/*
    200        1.2      tron 	 * extract config from proplist
    201        1.1   gdamore 	 */
    202        1.3    plunky 	obj = prop_dictionary_get(dict, BTDEVladdr);
    203        1.2      tron 	bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
    204        1.2      tron 
    205        1.3    plunky 	obj = prop_dictionary_get(dict, BTDEVraddr);
    206        1.2      tron 	bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
    207        1.1   gdamore 
    208        1.8    plunky 	obj = prop_dictionary_get(dict, BTDEVmode);
    209        1.8    plunky 	if (prop_object_type(obj) == PROP_TYPE_STRING) {
    210        1.8    plunky 		if (prop_string_equals_cstring(obj, BTDEVauth))
    211  1.13.14.3       mjf 			sockopt_setint(&sc->sc_mode, L2CAP_LM_AUTH);
    212        1.8    plunky 		else if (prop_string_equals_cstring(obj, BTDEVencrypt))
    213  1.13.14.3       mjf 			sockopt_setint(&sc->sc_mode, L2CAP_LM_ENCRYPT);
    214        1.8    plunky 		else if (prop_string_equals_cstring(obj, BTDEVsecure))
    215  1.13.14.3       mjf 			sockopt_setint(&sc->sc_mode, L2CAP_LM_SECURE);
    216        1.8    plunky 		else  {
    217        1.8    plunky 			aprint_error(" unknown %s\n", BTDEVmode);
    218        1.8    plunky 			return;
    219        1.8    plunky 		}
    220        1.8    plunky 
    221        1.8    plunky 		aprint_verbose(" %s %s", BTDEVmode,
    222        1.8    plunky 					 prop_string_cstring_nocopy(obj));
    223        1.8    plunky 	}
    224        1.8    plunky 
    225        1.3    plunky 	obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
    226        1.3    plunky 	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
    227        1.2      tron 		sc->sc_ctlpsm = prop_number_integer_value(obj);
    228        1.2      tron 		if (L2CAP_PSM_INVALID(sc->sc_ctlpsm)) {
    229        1.3    plunky 			aprint_error(" invalid %s\n", BTHIDEVcontrolpsm);
    230        1.2      tron 			return;
    231        1.2      tron 		}
    232        1.2      tron 	}
    233        1.1   gdamore 
    234        1.3    plunky 	obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
    235        1.3    plunky 	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
    236        1.2      tron 		sc->sc_intpsm = prop_number_integer_value(obj);
    237        1.2      tron 		if (L2CAP_PSM_INVALID(sc->sc_intpsm)) {
    238        1.3    plunky 			aprint_error(" invalid %s\n", BTHIDEVinterruptpsm);
    239        1.2      tron 			return;
    240        1.2      tron 		}
    241        1.2      tron 	}
    242        1.2      tron 
    243        1.3    plunky 	obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
    244        1.3    plunky 	if (prop_object_type(obj) == PROP_TYPE_DATA) {
    245        1.2      tron 		dlen = prop_data_size(obj);
    246        1.2      tron 		desc = prop_data_data_nocopy(obj);
    247        1.2      tron 	} else {
    248        1.3    plunky 		aprint_error(" no %s\n", BTHIDEVdescriptor);
    249        1.1   gdamore 		return;
    250        1.1   gdamore 	}
    251        1.2      tron 
    252        1.3    plunky 	obj = prop_dictionary_get(dict, BTHIDEVreconnect);
    253        1.3    plunky 	if (prop_object_type(obj) == PROP_TYPE_BOOL
    254        1.2      tron 	    && !prop_bool_true(obj))
    255        1.2      tron 		sc->sc_flags |= BTHID_RECONNECT;
    256        1.1   gdamore 
    257        1.1   gdamore 	/*
    258        1.1   gdamore 	 * Parse the descriptor and attach child devices, one per report.
    259        1.1   gdamore 	 */
    260        1.1   gdamore 	maxid = -1;
    261        1.1   gdamore 	h.report_ID = 0;
    262        1.2      tron 	d = hid_start_parse(desc, dlen, hid_none);
    263        1.1   gdamore 	while (hid_get_item(d, &h)) {
    264        1.1   gdamore 		if (h.report_ID > maxid)
    265        1.1   gdamore 			maxid = h.report_ID;
    266        1.1   gdamore 	}
    267        1.1   gdamore 	hid_end_parse(d);
    268        1.1   gdamore 
    269        1.1   gdamore 	if (maxid < 0) {
    270        1.1   gdamore 		aprint_error(" no reports found\n");
    271        1.1   gdamore 		return;
    272        1.1   gdamore 	}
    273        1.1   gdamore 
    274        1.1   gdamore 	aprint_normal("\n");
    275        1.1   gdamore 
    276        1.1   gdamore 	for (rep = 0 ; rep <= maxid ; rep++) {
    277        1.2      tron 		if (hid_report_size(desc, dlen, hid_feature, rep) == 0
    278        1.2      tron 		    && hid_report_size(desc, dlen, hid_input, rep) == 0
    279        1.2      tron 		    && hid_report_size(desc, dlen, hid_output, rep) == 0)
    280        1.1   gdamore 			continue;
    281        1.1   gdamore 
    282        1.2      tron 		bha.ba_desc = desc;
    283        1.2      tron 		bha.ba_dlen = dlen;
    284        1.1   gdamore 		bha.ba_input = bthidev_null;
    285        1.1   gdamore 		bha.ba_feature = bthidev_null;
    286        1.1   gdamore 		bha.ba_output = bthidev_output;
    287        1.1   gdamore 		bha.ba_id = rep;
    288        1.1   gdamore 
    289        1.1   gdamore 		locs[BTHIDBUSCF_REPORTID] = rep;
    290        1.1   gdamore 
    291       1.10    plunky 		dev = config_found_sm_loc(self, "bthidbus",
    292        1.1   gdamore 					locs, &bha, bthidev_print, config_stdsubmatch);
    293        1.1   gdamore 		if (dev != NULL) {
    294       1.10    plunky 			hidev = device_private(dev);
    295       1.10    plunky 			hidev->sc_dev = dev;
    296       1.10    plunky 			hidev->sc_parent = self;
    297       1.10    plunky 			hidev->sc_id = rep;
    298       1.10    plunky 			hidev->sc_input = bha.ba_input;
    299       1.10    plunky 			hidev->sc_feature = bha.ba_feature;
    300       1.10    plunky 			LIST_INSERT_HEAD(&sc->sc_list, hidev, sc_next);
    301        1.1   gdamore 		}
    302        1.1   gdamore 	}
    303        1.1   gdamore 
    304        1.1   gdamore 	/*
    305        1.1   gdamore 	 * start bluetooth connections
    306        1.1   gdamore 	 */
    307  1.13.14.2       mjf 	mutex_enter(bt_lock);
    308        1.2      tron 	if ((sc->sc_flags & BTHID_RECONNECT) == 0)
    309        1.1   gdamore 		bthidev_listen(sc);
    310        1.1   gdamore 
    311        1.2      tron 	if (sc->sc_flags & BTHID_CONNECTING)
    312        1.1   gdamore 		bthidev_connect(sc);
    313  1.13.14.2       mjf 	mutex_exit(bt_lock);
    314        1.1   gdamore }
    315        1.1   gdamore 
    316        1.1   gdamore static int
    317       1.10    plunky bthidev_detach(device_t self, int flags)
    318        1.1   gdamore {
    319       1.10    plunky 	struct bthidev_softc *sc = device_private(self);
    320       1.10    plunky 	struct bthidev *hidev;
    321        1.1   gdamore 
    322  1.13.14.2       mjf 	mutex_enter(bt_lock);
    323        1.1   gdamore 	sc->sc_flags = 0;	/* disable reconnecting */
    324        1.1   gdamore 
    325        1.1   gdamore 	/* release interrupt listen */
    326        1.1   gdamore 	if (sc->sc_int_l != NULL) {
    327        1.1   gdamore 		l2cap_detach(&sc->sc_int_l);
    328        1.1   gdamore 		sc->sc_int_l = NULL;
    329        1.1   gdamore 	}
    330        1.1   gdamore 
    331        1.1   gdamore 	/* release control listen */
    332        1.1   gdamore 	if (sc->sc_ctl_l != NULL) {
    333        1.1   gdamore 		l2cap_detach(&sc->sc_ctl_l);
    334        1.1   gdamore 		sc->sc_ctl_l = NULL;
    335        1.1   gdamore 	}
    336        1.1   gdamore 
    337        1.1   gdamore 	/* close interrupt channel */
    338        1.1   gdamore 	if (sc->sc_int != NULL) {
    339        1.1   gdamore 		l2cap_disconnect(sc->sc_int, 0);
    340        1.1   gdamore 		l2cap_detach(&sc->sc_int);
    341        1.1   gdamore 		sc->sc_int = NULL;
    342        1.1   gdamore 	}
    343        1.1   gdamore 
    344        1.1   gdamore 	/* close control channel */
    345        1.1   gdamore 	if (sc->sc_ctl != NULL) {
    346        1.1   gdamore 		l2cap_disconnect(sc->sc_ctl, 0);
    347        1.1   gdamore 		l2cap_detach(&sc->sc_ctl);
    348        1.1   gdamore 		sc->sc_ctl = NULL;
    349        1.1   gdamore 	}
    350        1.1   gdamore 
    351  1.13.14.2       mjf 	callout_halt(&sc->sc_reconnect, bt_lock);
    352       1.11    plunky 	callout_destroy(&sc->sc_reconnect);
    353       1.11    plunky 
    354  1.13.14.2       mjf 	mutex_exit(bt_lock);
    355        1.1   gdamore 
    356        1.1   gdamore 	/* detach children */
    357       1.10    plunky 	while ((hidev = LIST_FIRST(&sc->sc_list)) != NULL) {
    358       1.10    plunky 		LIST_REMOVE(hidev, sc_next);
    359       1.10    plunky 		config_detach(hidev->sc_dev, flags);
    360        1.1   gdamore 	}
    361        1.1   gdamore 
    362  1.13.14.3       mjf 	sockopt_destroy(&sc->sc_mode);
    363  1.13.14.3       mjf 
    364        1.1   gdamore 	return 0;
    365        1.1   gdamore }
    366        1.1   gdamore 
    367        1.1   gdamore /*
    368        1.1   gdamore  * bthidev config print
    369        1.1   gdamore  */
    370        1.1   gdamore static int
    371        1.1   gdamore bthidev_print(void *aux, const char *pnp)
    372        1.1   gdamore {
    373        1.1   gdamore 	struct bthidev_attach_args *ba = aux;
    374        1.1   gdamore 
    375        1.1   gdamore 	if (pnp != NULL)
    376        1.1   gdamore 		aprint_normal("%s:", pnp);
    377        1.1   gdamore 
    378        1.1   gdamore 	if (ba->ba_id > 0)
    379        1.1   gdamore 		aprint_normal(" reportid %d", ba->ba_id);
    380        1.1   gdamore 
    381        1.1   gdamore 	return UNCONF;
    382        1.1   gdamore }
    383        1.1   gdamore 
    384        1.1   gdamore /*****************************************************************************
    385        1.1   gdamore  *
    386        1.1   gdamore  *	bluetooth(4) HID attach/detach routines
    387        1.1   gdamore  */
    388        1.1   gdamore 
    389        1.1   gdamore /*
    390        1.4    plunky  * callouts are scheduled after connections have been lost, in order
    391        1.4    plunky  * to clean up and reconnect.
    392        1.1   gdamore  */
    393        1.1   gdamore static void
    394        1.1   gdamore bthidev_timeout(void *arg)
    395        1.1   gdamore {
    396        1.1   gdamore 	struct bthidev_softc *sc = arg;
    397        1.1   gdamore 
    398  1.13.14.2       mjf 	mutex_enter(bt_lock);
    399        1.1   gdamore 	callout_ack(&sc->sc_reconnect);
    400        1.1   gdamore 
    401        1.1   gdamore 	switch (sc->sc_state) {
    402        1.1   gdamore 	case BTHID_CLOSED:
    403        1.4    plunky 		if (sc->sc_int != NULL) {
    404        1.4    plunky 			l2cap_disconnect(sc->sc_int, 0);
    405        1.4    plunky 			break;
    406        1.4    plunky 		}
    407        1.4    plunky 
    408        1.4    plunky 		if (sc->sc_ctl != NULL) {
    409        1.4    plunky 			l2cap_disconnect(sc->sc_ctl, 0);
    410        1.4    plunky 			break;
    411        1.4    plunky 		}
    412        1.4    plunky 
    413        1.4    plunky 		if (sc->sc_flags & BTHID_RECONNECT) {
    414        1.4    plunky 			sc->sc_flags |= BTHID_CONNECTING;
    415        1.4    plunky 			bthidev_connect(sc);
    416        1.4    plunky 			break;
    417        1.4    plunky 		}
    418        1.4    plunky 
    419        1.1   gdamore 		break;
    420        1.1   gdamore 
    421        1.1   gdamore 	case BTHID_WAIT_CTL:
    422        1.1   gdamore 		break;
    423        1.1   gdamore 
    424        1.1   gdamore 	case BTHID_WAIT_INT:
    425        1.1   gdamore 		break;
    426        1.1   gdamore 
    427        1.1   gdamore 	case BTHID_OPEN:
    428        1.1   gdamore 		break;
    429        1.1   gdamore 
    430        1.1   gdamore 	default:
    431        1.1   gdamore 		break;
    432        1.1   gdamore 	}
    433  1.13.14.2       mjf 	mutex_exit(bt_lock);
    434        1.1   gdamore }
    435        1.1   gdamore 
    436        1.1   gdamore /*
    437        1.1   gdamore  * listen for our device
    438        1.1   gdamore  */
    439        1.1   gdamore static int
    440        1.1   gdamore bthidev_listen(struct bthidev_softc *sc)
    441        1.1   gdamore {
    442        1.1   gdamore 	struct sockaddr_bt sa;
    443        1.1   gdamore 	int err;
    444        1.1   gdamore 
    445        1.1   gdamore 	memset(&sa, 0, sizeof(sa));
    446        1.1   gdamore 	sa.bt_len = sizeof(sa);
    447        1.1   gdamore 	sa.bt_family = AF_BLUETOOTH;
    448        1.1   gdamore 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    449        1.1   gdamore 
    450        1.1   gdamore 	/*
    451        1.1   gdamore 	 * Listen on control PSM
    452        1.1   gdamore 	 */
    453        1.1   gdamore 	err = l2cap_attach(&sc->sc_ctl_l, &bthidev_ctl_proto, sc);
    454        1.1   gdamore 	if (err)
    455        1.1   gdamore 		return err;
    456        1.1   gdamore 
    457  1.13.14.3       mjf 	err = l2cap_setopt(sc->sc_ctl_l, &sc->sc_mode);
    458        1.8    plunky 	if (err)
    459        1.8    plunky 		return err;
    460        1.8    plunky 
    461        1.1   gdamore 	sa.bt_psm = sc->sc_ctlpsm;
    462        1.1   gdamore 	err = l2cap_bind(sc->sc_ctl_l, &sa);
    463        1.1   gdamore 	if (err)
    464        1.1   gdamore 		return err;
    465        1.1   gdamore 
    466        1.1   gdamore 	err = l2cap_listen(sc->sc_ctl_l);
    467        1.1   gdamore 	if (err)
    468        1.1   gdamore 		return err;
    469        1.1   gdamore 
    470        1.1   gdamore 	/*
    471        1.1   gdamore 	 * Listen on interrupt PSM
    472        1.1   gdamore 	 */
    473        1.1   gdamore 	err = l2cap_attach(&sc->sc_int_l, &bthidev_int_proto, sc);
    474        1.1   gdamore 	if (err)
    475        1.1   gdamore 		return err;
    476        1.1   gdamore 
    477  1.13.14.3       mjf 	err = l2cap_setopt(sc->sc_int_l, &sc->sc_mode);
    478        1.8    plunky 	if (err)
    479        1.8    plunky 		return err;
    480        1.8    plunky 
    481        1.1   gdamore 	sa.bt_psm = sc->sc_intpsm;
    482        1.1   gdamore 	err = l2cap_bind(sc->sc_int_l, &sa);
    483        1.1   gdamore 	if (err)
    484        1.1   gdamore 		return err;
    485        1.1   gdamore 
    486        1.1   gdamore 	err = l2cap_listen(sc->sc_int_l);
    487        1.1   gdamore 	if (err)
    488        1.1   gdamore 		return err;
    489        1.1   gdamore 
    490        1.1   gdamore 	sc->sc_state = BTHID_WAIT_CTL;
    491        1.1   gdamore 	return 0;
    492        1.1   gdamore }
    493        1.1   gdamore 
    494        1.1   gdamore /*
    495        1.1   gdamore  * start connecting to our device
    496        1.1   gdamore  */
    497        1.1   gdamore static int
    498        1.1   gdamore bthidev_connect(struct bthidev_softc *sc)
    499        1.1   gdamore {
    500        1.1   gdamore 	struct sockaddr_bt sa;
    501        1.1   gdamore 	int err;
    502        1.1   gdamore 
    503        1.1   gdamore 	if (sc->sc_attempts++ > 0)
    504       1.12    plunky 		aprint_verbose_dev(sc->sc_dev, "connect (#%d)\n", sc->sc_attempts);
    505        1.1   gdamore 
    506        1.1   gdamore 	memset(&sa, 0, sizeof(sa));
    507        1.1   gdamore 	sa.bt_len = sizeof(sa);
    508        1.1   gdamore 	sa.bt_family = AF_BLUETOOTH;
    509        1.1   gdamore 
    510        1.1   gdamore 	err = l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
    511        1.1   gdamore 	if (err) {
    512       1.12    plunky 		aprint_error_dev(sc->sc_dev, "l2cap_attach failed (%d)\n", err);
    513        1.1   gdamore 		return err;
    514        1.1   gdamore 	}
    515        1.1   gdamore 
    516  1.13.14.3       mjf 	err = l2cap_setopt(sc->sc_ctl, &sc->sc_mode);
    517        1.8    plunky 	if (err)
    518        1.8    plunky 		return err;
    519        1.8    plunky 
    520        1.1   gdamore 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    521        1.1   gdamore 	err = l2cap_bind(sc->sc_ctl, &sa);
    522        1.1   gdamore 	if (err) {
    523       1.12    plunky 		aprint_error_dev(sc->sc_dev, "l2cap_bind failed (%d)\n", err);
    524        1.1   gdamore 		return err;
    525        1.1   gdamore 	}
    526        1.1   gdamore 
    527        1.1   gdamore 	sa.bt_psm = sc->sc_ctlpsm;
    528        1.1   gdamore 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    529        1.1   gdamore 	err = l2cap_connect(sc->sc_ctl, &sa);
    530        1.1   gdamore 	if (err) {
    531       1.12    plunky 		aprint_error_dev(sc->sc_dev, "l2cap_connect failed (%d)\n", err);
    532        1.1   gdamore 		return err;
    533        1.1   gdamore 	}
    534        1.1   gdamore 
    535        1.1   gdamore 	sc->sc_state = BTHID_WAIT_CTL;
    536        1.1   gdamore 	return 0;
    537        1.1   gdamore }
    538        1.1   gdamore 
    539        1.1   gdamore /*****************************************************************************
    540        1.1   gdamore  *
    541        1.1   gdamore  *	bluetooth(9) callback methods for L2CAP
    542        1.1   gdamore  *
    543        1.1   gdamore  *	All these are called from Bluetooth Protocol code, in a soft
    544        1.1   gdamore  *	interrupt context at IPL_SOFTNET.
    545        1.1   gdamore  */
    546        1.1   gdamore 
    547        1.1   gdamore static void
    548        1.7  christos bthidev_connecting(void *arg)
    549        1.1   gdamore {
    550        1.1   gdamore 
    551        1.1   gdamore 	/* dont care */
    552        1.1   gdamore }
    553        1.1   gdamore 
    554        1.1   gdamore static void
    555        1.1   gdamore bthidev_ctl_connected(void *arg)
    556        1.1   gdamore {
    557        1.1   gdamore 	struct sockaddr_bt sa;
    558        1.1   gdamore 	struct bthidev_softc *sc = arg;
    559        1.1   gdamore 	int err;
    560        1.1   gdamore 
    561        1.1   gdamore 	if (sc->sc_state != BTHID_WAIT_CTL)
    562        1.1   gdamore 		return;
    563        1.1   gdamore 
    564        1.1   gdamore 	KASSERT(sc->sc_ctl != NULL);
    565        1.1   gdamore 	KASSERT(sc->sc_int == NULL);
    566        1.1   gdamore 
    567        1.2      tron 	if (sc->sc_flags & BTHID_CONNECTING) {
    568        1.1   gdamore 		/* initiate connect on interrupt PSM */
    569        1.1   gdamore 		err = l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
    570        1.1   gdamore 		if (err)
    571        1.1   gdamore 			goto fail;
    572        1.1   gdamore 
    573  1.13.14.3       mjf 		err = l2cap_setopt(sc->sc_int, &sc->sc_mode);
    574        1.8    plunky 		if (err)
    575        1.8    plunky 			goto fail;
    576        1.8    plunky 
    577        1.1   gdamore 		memset(&sa, 0, sizeof(sa));
    578        1.1   gdamore 		sa.bt_len = sizeof(sa);
    579        1.1   gdamore 		sa.bt_family = AF_BLUETOOTH;
    580        1.1   gdamore 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    581        1.1   gdamore 
    582        1.1   gdamore 		err = l2cap_bind(sc->sc_int, &sa);
    583        1.1   gdamore 		if (err)
    584        1.1   gdamore 			goto fail;
    585        1.1   gdamore 
    586        1.1   gdamore 		sa.bt_psm = sc->sc_intpsm;
    587        1.1   gdamore 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    588        1.1   gdamore 		err = l2cap_connect(sc->sc_int, &sa);
    589        1.1   gdamore 		if (err)
    590        1.1   gdamore 			goto fail;
    591        1.1   gdamore 	}
    592        1.1   gdamore 
    593        1.1   gdamore 	sc->sc_state = BTHID_WAIT_INT;
    594        1.1   gdamore 	return;
    595        1.1   gdamore 
    596        1.1   gdamore fail:
    597        1.1   gdamore 	l2cap_detach(&sc->sc_ctl);
    598        1.1   gdamore 	sc->sc_ctl = NULL;
    599        1.3    plunky 
    600       1.12    plunky 	aprint_error_dev(sc->sc_dev, "connect failed (%d)\n", err);
    601        1.1   gdamore }
    602        1.1   gdamore 
    603        1.1   gdamore static void
    604        1.1   gdamore bthidev_int_connected(void *arg)
    605        1.1   gdamore {
    606        1.1   gdamore 	struct bthidev_softc *sc = arg;
    607        1.1   gdamore 
    608        1.1   gdamore 	if (sc->sc_state != BTHID_WAIT_INT)
    609        1.1   gdamore 		return;
    610        1.1   gdamore 
    611        1.1   gdamore 	KASSERT(sc->sc_ctl != NULL);
    612        1.1   gdamore 	KASSERT(sc->sc_int != NULL);
    613        1.1   gdamore 
    614        1.1   gdamore 	sc->sc_attempts = 0;
    615        1.2      tron 	sc->sc_flags &= ~BTHID_CONNECTING;
    616        1.1   gdamore 	sc->sc_state = BTHID_OPEN;
    617        1.1   gdamore 
    618       1.13    plunky 	aprint_normal_dev(sc->sc_dev, "connected\n");
    619        1.1   gdamore }
    620        1.1   gdamore 
    621        1.1   gdamore /*
    622        1.1   gdamore  * Disconnected
    623        1.1   gdamore  *
    624        1.1   gdamore  * Depending on our state, this could mean several things, but essentially
    625        1.2      tron  * we are lost. If both channels are closed, and we are marked to reconnect,
    626        1.2      tron  * schedule another try otherwise just give up. They will contact us.
    627        1.1   gdamore  */
    628        1.1   gdamore static void
    629        1.7  christos bthidev_ctl_disconnected(void *arg, int err)
    630        1.1   gdamore {
    631        1.1   gdamore 	struct bthidev_softc *sc = arg;
    632        1.1   gdamore 
    633        1.1   gdamore 	if (sc->sc_ctl != NULL) {
    634        1.1   gdamore 		l2cap_detach(&sc->sc_ctl);
    635        1.1   gdamore 		sc->sc_ctl = NULL;
    636        1.1   gdamore 	}
    637        1.1   gdamore 
    638        1.1   gdamore 	sc->sc_state = BTHID_CLOSED;
    639        1.1   gdamore 
    640        1.1   gdamore 	if (sc->sc_int == NULL) {
    641       1.13    plunky 		aprint_normal_dev(sc->sc_dev, "disconnected\n");
    642        1.2      tron 		sc->sc_flags &= ~BTHID_CONNECTING;
    643        1.1   gdamore 
    644        1.2      tron 		if (sc->sc_flags & BTHID_RECONNECT)
    645        1.1   gdamore 			callout_schedule(&sc->sc_reconnect,
    646        1.1   gdamore 					BTHID_RETRY_INTERVAL * hz);
    647        1.1   gdamore 		else
    648        1.1   gdamore 			sc->sc_state = BTHID_WAIT_CTL;
    649        1.2      tron 	} else {
    650        1.2      tron 		/*
    651        1.2      tron 		 * The interrupt channel should have been closed first,
    652        1.4    plunky 		 * but its potentially unsafe to detach that from here.
    653        1.4    plunky 		 * Give them a second to do the right thing or let the
    654        1.4    plunky 		 * callout handle it.
    655        1.2      tron 		 */
    656        1.4    plunky 		callout_schedule(&sc->sc_reconnect, hz);
    657        1.1   gdamore 	}
    658        1.1   gdamore }
    659        1.1   gdamore 
    660        1.1   gdamore static void
    661        1.7  christos bthidev_int_disconnected(void *arg, int err)
    662        1.1   gdamore {
    663        1.1   gdamore 	struct bthidev_softc *sc = arg;
    664        1.1   gdamore 
    665        1.1   gdamore 	if (sc->sc_int != NULL) {
    666        1.1   gdamore 		l2cap_detach(&sc->sc_int);
    667        1.1   gdamore 		sc->sc_int = NULL;
    668        1.1   gdamore 	}
    669        1.1   gdamore 
    670        1.1   gdamore 	sc->sc_state = BTHID_CLOSED;
    671        1.1   gdamore 
    672        1.1   gdamore 	if (sc->sc_ctl == NULL) {
    673       1.13    plunky 		aprint_normal_dev(sc->sc_dev, "disconnected\n");
    674        1.2      tron 		sc->sc_flags &= ~BTHID_CONNECTING;
    675        1.1   gdamore 
    676        1.2      tron 		if (sc->sc_flags & BTHID_RECONNECT)
    677        1.1   gdamore 			callout_schedule(&sc->sc_reconnect,
    678        1.1   gdamore 					BTHID_RETRY_INTERVAL * hz);
    679        1.1   gdamore 		else
    680        1.1   gdamore 			sc->sc_state = BTHID_WAIT_CTL;
    681        1.4    plunky 	} else {
    682        1.4    plunky 		/*
    683        1.4    plunky 		 * The control channel should be closing also, allow
    684        1.4    plunky 		 * them a chance to do that before we force it.
    685        1.4    plunky 		 */
    686        1.4    plunky 		callout_schedule(&sc->sc_reconnect, hz);
    687        1.1   gdamore 	}
    688        1.1   gdamore }
    689        1.1   gdamore 
    690        1.1   gdamore /*
    691        1.1   gdamore  * New Connections
    692        1.1   gdamore  *
    693        1.1   gdamore  * We give a new L2CAP handle back if this matches the BDADDR we are
    694        1.1   gdamore  * listening for and we are in the right state. bthidev_connected will
    695        1.1   gdamore  * be called when the connection is open, so nothing else to do here
    696        1.1   gdamore  */
    697        1.1   gdamore static void *
    698        1.7  christos bthidev_ctl_newconn(void *arg, struct sockaddr_bt *laddr,
    699        1.6  christos     struct sockaddr_bt *raddr)
    700        1.1   gdamore {
    701        1.1   gdamore 	struct bthidev_softc *sc = arg;
    702        1.1   gdamore 
    703        1.1   gdamore 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
    704        1.2      tron 	    || (sc->sc_flags & BTHID_CONNECTING)
    705        1.1   gdamore 	    || sc->sc_state != BTHID_WAIT_CTL
    706        1.1   gdamore 	    || sc->sc_ctl != NULL
    707        1.1   gdamore 	    || sc->sc_int != NULL)
    708        1.1   gdamore 		return NULL;
    709        1.1   gdamore 
    710        1.1   gdamore 	l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
    711        1.1   gdamore 	return sc->sc_ctl;
    712        1.1   gdamore }
    713        1.1   gdamore 
    714        1.1   gdamore static void *
    715        1.7  christos bthidev_int_newconn(void *arg, struct sockaddr_bt *laddr,
    716        1.6  christos     struct sockaddr_bt *raddr)
    717        1.1   gdamore {
    718        1.1   gdamore 	struct bthidev_softc *sc = arg;
    719        1.1   gdamore 
    720        1.1   gdamore 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
    721        1.2      tron 	    || (sc->sc_flags & BTHID_CONNECTING)
    722        1.1   gdamore 	    || sc->sc_state != BTHID_WAIT_INT
    723        1.1   gdamore 	    || sc->sc_ctl == NULL
    724        1.1   gdamore 	    || sc->sc_int != NULL)
    725        1.1   gdamore 		return NULL;
    726        1.1   gdamore 
    727        1.1   gdamore 	l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
    728        1.1   gdamore 	return sc->sc_int;
    729        1.1   gdamore }
    730        1.1   gdamore 
    731        1.1   gdamore static void
    732        1.7  christos bthidev_complete(void *arg, int count)
    733        1.1   gdamore {
    734        1.1   gdamore 
    735        1.1   gdamore 	/* dont care */
    736        1.1   gdamore }
    737        1.1   gdamore 
    738        1.8    plunky static void
    739        1.8    plunky bthidev_linkmode(void *arg, int new)
    740        1.8    plunky {
    741        1.8    plunky 	struct bthidev_softc *sc = arg;
    742  1.13.14.3       mjf 	int mode;
    743  1.13.14.3       mjf 
    744  1.13.14.3       mjf 	(void)sockopt_getint(&sc->sc_mode, &mode);
    745        1.8    plunky 
    746  1.13.14.3       mjf 	if ((mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
    747       1.12    plunky 		aprint_error_dev(sc->sc_dev, "auth failed\n");
    748  1.13.14.3       mjf 	else if ((mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
    749       1.12    plunky 		aprint_error_dev(sc->sc_dev, "encrypt off\n");
    750  1.13.14.3       mjf 	else if ((mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE))
    751       1.12    plunky 		aprint_error_dev(sc->sc_dev, "insecure\n");
    752        1.8    plunky 	else
    753        1.8    plunky 		return;
    754        1.8    plunky 
    755        1.8    plunky 	if (sc->sc_int != NULL)
    756        1.8    plunky 		l2cap_disconnect(sc->sc_int, 0);
    757        1.8    plunky 
    758        1.8    plunky 	if (sc->sc_ctl != NULL)
    759        1.8    plunky 		l2cap_disconnect(sc->sc_ctl, 0);
    760        1.8    plunky }
    761        1.8    plunky 
    762        1.1   gdamore /*
    763        1.1   gdamore  * Receive reports from the protocol stack.
    764        1.1   gdamore  */
    765        1.1   gdamore static void
    766        1.1   gdamore bthidev_input(void *arg, struct mbuf *m)
    767        1.1   gdamore {
    768        1.1   gdamore 	struct bthidev_softc *sc = arg;
    769       1.10    plunky 	struct bthidev *hidev;
    770        1.1   gdamore 	uint8_t *data;
    771        1.1   gdamore 	int len;
    772        1.1   gdamore 
    773        1.1   gdamore 	if (sc->sc_state != BTHID_OPEN)
    774        1.1   gdamore 		goto release;
    775        1.1   gdamore 
    776        1.1   gdamore 	if (m->m_pkthdr.len > m->m_len)
    777       1.12    plunky 		aprint_error_dev(sc->sc_dev, "truncating HID report\n");
    778        1.1   gdamore 
    779        1.1   gdamore 	len = m->m_len;
    780        1.1   gdamore 	data = mtod(m, uint8_t *);
    781        1.1   gdamore 
    782        1.1   gdamore 	if (BTHID_TYPE(data[0]) == BTHID_DATA) {
    783        1.1   gdamore 		/*
    784        1.1   gdamore 		 * data[0] == type / parameter
    785        1.1   gdamore 		 * data[1] == id
    786        1.1   gdamore 		 * data[2..len] == report
    787        1.1   gdamore 		 */
    788        1.1   gdamore 		if (len < 3)
    789        1.1   gdamore 			goto release;
    790        1.1   gdamore 
    791       1.10    plunky 		LIST_FOREACH(hidev, &sc->sc_list, sc_next) {
    792       1.10    plunky 			if (data[1] == hidev->sc_id) {
    793        1.1   gdamore 				switch (BTHID_DATA_PARAM(data[0])) {
    794        1.1   gdamore 				case BTHID_DATA_INPUT:
    795       1.10    plunky 					(*hidev->sc_input)(hidev, data + 2, len - 2);
    796        1.1   gdamore 					break;
    797        1.1   gdamore 
    798        1.1   gdamore 				case BTHID_DATA_FEATURE:
    799       1.10    plunky 					(*hidev->sc_feature)(hidev, data + 2, len - 2);
    800        1.1   gdamore 					break;
    801        1.1   gdamore 
    802        1.1   gdamore 				default:
    803        1.1   gdamore 					break;
    804        1.1   gdamore 				}
    805        1.1   gdamore 
    806        1.1   gdamore 				goto release;
    807        1.1   gdamore 			}
    808        1.1   gdamore 		}
    809       1.12    plunky 		aprint_error_dev(sc->sc_dev, "report id %d, len = %d ignored\n",
    810       1.12    plunky 		    data[1], len - 2);
    811        1.1   gdamore 
    812        1.1   gdamore 		goto release;
    813        1.1   gdamore 	}
    814        1.1   gdamore 
    815        1.1   gdamore 	if (BTHID_TYPE(data[0]) == BTHID_CONTROL) {
    816        1.1   gdamore 		if (len < 1)
    817        1.1   gdamore 			goto release;
    818        1.1   gdamore 
    819        1.1   gdamore 		if (BTHID_DATA_PARAM(data[0]) == BTHID_CONTROL_UNPLUG) {
    820       1.12    plunky 			aprint_normal_dev(sc->sc_dev, "unplugged\n");
    821        1.1   gdamore 
    822        1.1   gdamore 			/* close interrupt channel */
    823        1.1   gdamore 			if (sc->sc_int != NULL) {
    824        1.1   gdamore 				l2cap_disconnect(sc->sc_int, 0);
    825        1.1   gdamore 				l2cap_detach(&sc->sc_int);
    826        1.1   gdamore 				sc->sc_int = NULL;
    827        1.1   gdamore 			}
    828        1.1   gdamore 
    829        1.1   gdamore 			/* close control channel */
    830        1.1   gdamore 			if (sc->sc_ctl != NULL) {
    831        1.1   gdamore 				l2cap_disconnect(sc->sc_ctl, 0);
    832        1.1   gdamore 				l2cap_detach(&sc->sc_ctl);
    833        1.1   gdamore 				sc->sc_ctl = NULL;
    834        1.1   gdamore 			}
    835        1.1   gdamore 		}
    836        1.1   gdamore 
    837        1.1   gdamore 		goto release;
    838        1.1   gdamore 	}
    839        1.1   gdamore 
    840        1.1   gdamore release:
    841        1.1   gdamore 	m_freem(m);
    842        1.1   gdamore }
    843        1.1   gdamore 
    844        1.1   gdamore /*****************************************************************************
    845        1.1   gdamore  *
    846        1.1   gdamore  *	IO routines
    847        1.1   gdamore  */
    848        1.1   gdamore 
    849        1.1   gdamore static void
    850       1.10    plunky bthidev_null(struct bthidev *hidev, uint8_t *report, int len)
    851        1.1   gdamore {
    852        1.1   gdamore 
    853        1.1   gdamore 	/*
    854        1.1   gdamore 	 * empty routine just in case the device
    855        1.1   gdamore 	 * provided no method to handle this report
    856        1.1   gdamore 	 */
    857        1.1   gdamore }
    858        1.1   gdamore 
    859        1.1   gdamore static int
    860       1.10    plunky bthidev_output(struct bthidev *hidev, uint8_t *report, int rlen)
    861        1.1   gdamore {
    862       1.10    plunky 	struct bthidev_softc *sc = device_private(hidev->sc_parent);
    863        1.1   gdamore 	struct mbuf *m;
    864  1.13.14.2       mjf 	int err;
    865        1.1   gdamore 
    866        1.1   gdamore 	if (sc == NULL || sc->sc_state != BTHID_OPEN)
    867        1.1   gdamore 		return ENOTCONN;
    868        1.1   gdamore 
    869        1.1   gdamore 	KASSERT(sc->sc_ctl != NULL);
    870        1.1   gdamore 	KASSERT(sc->sc_int != NULL);
    871        1.1   gdamore 
    872        1.1   gdamore 	if (rlen == 0 || report == NULL)
    873        1.1   gdamore 		return 0;
    874        1.1   gdamore 
    875        1.1   gdamore 	if (rlen > MHLEN - 2) {
    876       1.12    plunky 		aprint_error_dev(sc->sc_dev,
    877       1.12    plunky 		    "output report too long (%d)!\n", rlen);
    878        1.1   gdamore 		return EMSGSIZE;
    879        1.1   gdamore 	}
    880        1.1   gdamore 
    881        1.1   gdamore 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    882        1.1   gdamore 	if (m == NULL)
    883        1.1   gdamore 		return ENOMEM;
    884        1.1   gdamore 
    885        1.1   gdamore 	/*
    886        1.1   gdamore 	 * data[0] = type / parameter
    887        1.1   gdamore 	 * data[1] = id
    888        1.1   gdamore 	 * data[2..N] = report
    889        1.1   gdamore 	 */
    890        1.1   gdamore 	mtod(m, uint8_t *)[0] = (uint8_t)((BTHID_DATA << 4) | BTHID_DATA_OUTPUT);
    891       1.10    plunky 	mtod(m, uint8_t *)[1] = hidev->sc_id;
    892        1.1   gdamore 	memcpy(mtod(m, uint8_t *) + 2, report, rlen);
    893        1.1   gdamore 	m->m_pkthdr.len = m->m_len = rlen + 2;
    894        1.1   gdamore 
    895  1.13.14.2       mjf 	mutex_enter(bt_lock);
    896        1.1   gdamore 	err = l2cap_send(sc->sc_int, m);
    897  1.13.14.2       mjf 	mutex_exit(bt_lock);
    898        1.1   gdamore 
    899        1.1   gdamore 	return err;
    900        1.1   gdamore }
    901