Home | History | Annotate | Line # | Download | only in bluetooth
bthidev.c revision 1.21.2.1
      1  1.21.2.1       riz /*	$NetBSD: bthidev.c,v 1.21.2.1 2012/04/03 17:31:19 riz 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.21.2.1       riz __KERNEL_RCSID(0, "$NetBSD: bthidev.c,v 1.21.2.1 2012/04/03 17:31:19 riz Exp $");
     36       1.1   gdamore 
     37       1.1   gdamore #include <sys/param.h>
     38      1.21    plunky #include <sys/condvar.h>
     39       1.1   gdamore #include <sys/conf.h>
     40       1.1   gdamore #include <sys/device.h>
     41       1.1   gdamore #include <sys/fcntl.h>
     42       1.1   gdamore #include <sys/kernel.h>
     43      1.21    plunky #include <sys/kthread.h>
     44       1.1   gdamore #include <sys/queue.h>
     45       1.1   gdamore #include <sys/malloc.h>
     46       1.1   gdamore #include <sys/mbuf.h>
     47      1.21    plunky #include <sys/mutex.h>
     48       1.1   gdamore #include <sys/proc.h>
     49      1.16    plunky #include <sys/socketvar.h>
     50       1.1   gdamore #include <sys/systm.h>
     51       1.1   gdamore 
     52       1.2      tron #include <prop/proplib.h>
     53       1.2      tron 
     54       1.1   gdamore #include <netbt/bluetooth.h>
     55       1.1   gdamore #include <netbt/l2cap.h>
     56       1.1   gdamore 
     57       1.1   gdamore #include <dev/usb/hid.h>
     58       1.1   gdamore #include <dev/bluetooth/btdev.h>
     59       1.1   gdamore #include <dev/bluetooth/bthid.h>
     60       1.1   gdamore #include <dev/bluetooth/bthidev.h>
     61       1.1   gdamore 
     62       1.1   gdamore #include "locators.h"
     63       1.1   gdamore 
     64       1.1   gdamore /*****************************************************************************
     65       1.1   gdamore  *
     66       1.1   gdamore  *	Bluetooth HID device
     67       1.1   gdamore  */
     68       1.1   gdamore 
     69       1.1   gdamore #define MAX_DESCRIPTOR_LEN	1024		/* sanity check */
     70       1.1   gdamore 
     71       1.1   gdamore /* bthidev softc */
     72       1.1   gdamore struct bthidev_softc {
     73       1.1   gdamore 	uint16_t		sc_state;
     74       1.1   gdamore 	uint16_t		sc_flags;
     75      1.12    plunky 	device_t		sc_dev;
     76       1.1   gdamore 
     77       1.1   gdamore 	bdaddr_t		sc_laddr;	/* local address */
     78       1.1   gdamore 	bdaddr_t		sc_raddr;	/* remote address */
     79      1.16    plunky 	struct sockopt		sc_mode;	/* link mode sockopt */
     80       1.1   gdamore 
     81       1.1   gdamore 	uint16_t		sc_ctlpsm;	/* control PSM */
     82       1.1   gdamore 	struct l2cap_channel	*sc_ctl;	/* control channel */
     83       1.1   gdamore 	struct l2cap_channel	*sc_ctl_l;	/* control listen */
     84       1.1   gdamore 
     85       1.1   gdamore 	uint16_t		sc_intpsm;	/* interrupt PSM */
     86       1.1   gdamore 	struct l2cap_channel	*sc_int;	/* interrupt channel */
     87       1.1   gdamore 	struct l2cap_channel	*sc_int_l;	/* interrupt listen */
     88       1.1   gdamore 
     89      1.21    plunky 	MBUFQ_HEAD()		sc_inq;		/* input queue */
     90      1.21    plunky 	kmutex_t		sc_lock;	/* input queue lock */
     91      1.21    plunky 	kcondvar_t		sc_cv;		/* input queue trigger */
     92      1.21    plunky 	lwp_t			*sc_lwp;	/* input queue processor */
     93      1.21    plunky 	int			sc_detach;
     94      1.21    plunky 
     95       1.1   gdamore 	LIST_HEAD(,bthidev)	sc_list;	/* child list */
     96       1.1   gdamore 
     97      1.11    plunky 	callout_t		sc_reconnect;
     98       1.1   gdamore 	int			sc_attempts;	/* connection attempts */
     99       1.1   gdamore };
    100       1.1   gdamore 
    101       1.2      tron /* sc_flags */
    102       1.2      tron #define BTHID_RECONNECT		(1 << 0)	/* reconnect on link loss */
    103       1.2      tron #define BTHID_CONNECTING	(1 << 1)	/* we are connecting */
    104       1.2      tron 
    105       1.1   gdamore /* device state */
    106       1.1   gdamore #define BTHID_CLOSED		0
    107       1.1   gdamore #define BTHID_WAIT_CTL		1
    108       1.1   gdamore #define BTHID_WAIT_INT		2
    109       1.1   gdamore #define BTHID_OPEN		3
    110       1.1   gdamore 
    111       1.1   gdamore #define	BTHID_RETRY_INTERVAL	5	/* seconds between connection attempts */
    112       1.1   gdamore 
    113       1.1   gdamore /* bthidev internals */
    114       1.1   gdamore static void bthidev_timeout(void *);
    115       1.1   gdamore static int  bthidev_listen(struct bthidev_softc *);
    116       1.1   gdamore static int  bthidev_connect(struct bthidev_softc *);
    117       1.1   gdamore static int  bthidev_output(struct bthidev *, uint8_t *, int);
    118       1.1   gdamore static void bthidev_null(struct bthidev *, uint8_t *, int);
    119      1.21    plunky static void bthidev_process(void *);
    120      1.21    plunky static void bthidev_process_one(struct bthidev_softc *, struct mbuf *);
    121       1.1   gdamore 
    122       1.1   gdamore /* autoconf(9) glue */
    123      1.17    cegger static int  bthidev_match(device_t, cfdata_t, void *);
    124      1.10    plunky static void bthidev_attach(device_t, device_t, void *);
    125      1.10    plunky static int  bthidev_detach(device_t, int);
    126       1.1   gdamore static int  bthidev_print(void *, const char *);
    127       1.1   gdamore 
    128      1.10    plunky CFATTACH_DECL_NEW(bthidev, sizeof(struct bthidev_softc),
    129       1.1   gdamore     bthidev_match, bthidev_attach, bthidev_detach, NULL);
    130       1.1   gdamore 
    131       1.1   gdamore /* bluetooth(9) protocol methods for L2CAP */
    132       1.1   gdamore static void  bthidev_connecting(void *);
    133       1.1   gdamore static void  bthidev_ctl_connected(void *);
    134       1.1   gdamore static void  bthidev_int_connected(void *);
    135       1.1   gdamore static void  bthidev_ctl_disconnected(void *, int);
    136       1.1   gdamore static void  bthidev_int_disconnected(void *, int);
    137       1.1   gdamore static void *bthidev_ctl_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    138       1.1   gdamore static void *bthidev_int_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    139       1.1   gdamore static void  bthidev_complete(void *, int);
    140       1.8    plunky static void  bthidev_linkmode(void *, int);
    141       1.1   gdamore static void  bthidev_input(void *, struct mbuf *);
    142       1.1   gdamore 
    143       1.1   gdamore static const struct btproto bthidev_ctl_proto = {
    144       1.1   gdamore 	bthidev_connecting,
    145       1.1   gdamore 	bthidev_ctl_connected,
    146       1.1   gdamore 	bthidev_ctl_disconnected,
    147       1.1   gdamore 	bthidev_ctl_newconn,
    148       1.1   gdamore 	bthidev_complete,
    149       1.8    plunky 	bthidev_linkmode,
    150       1.1   gdamore 	bthidev_input,
    151       1.1   gdamore };
    152       1.1   gdamore 
    153       1.1   gdamore static const struct btproto bthidev_int_proto = {
    154       1.1   gdamore 	bthidev_connecting,
    155       1.1   gdamore 	bthidev_int_connected,
    156       1.1   gdamore 	bthidev_int_disconnected,
    157       1.1   gdamore 	bthidev_int_newconn,
    158       1.1   gdamore 	bthidev_complete,
    159       1.8    plunky 	bthidev_linkmode,
    160       1.1   gdamore 	bthidev_input,
    161       1.1   gdamore };
    162       1.1   gdamore 
    163       1.1   gdamore /*****************************************************************************
    164       1.1   gdamore  *
    165       1.1   gdamore  *	bthidev autoconf(9) routines
    166       1.1   gdamore  */
    167       1.1   gdamore 
    168       1.1   gdamore static int
    169      1.17    cegger bthidev_match(device_t self, cfdata_t cfdata, void *aux)
    170       1.1   gdamore {
    171       1.2      tron 	prop_dictionary_t dict = aux;
    172       1.2      tron 	prop_object_t obj;
    173       1.1   gdamore 
    174       1.5    plunky 	obj = prop_dictionary_get(dict, BTDEVservice);
    175       1.5    plunky 	if (prop_string_equals_cstring(obj, "HID"))
    176       1.5    plunky 		return 1;
    177       1.5    plunky 
    178       1.5    plunky 	return 0;
    179       1.1   gdamore }
    180       1.1   gdamore 
    181       1.1   gdamore static void
    182      1.10    plunky bthidev_attach(device_t parent, device_t self, void *aux)
    183       1.1   gdamore {
    184      1.10    plunky 	struct bthidev_softc *sc = device_private(self);
    185       1.2      tron 	prop_dictionary_t dict = aux;
    186       1.2      tron 	prop_object_t obj;
    187      1.10    plunky 	device_t dev;
    188       1.1   gdamore 	struct bthidev_attach_args bha;
    189      1.10    plunky 	struct bthidev *hidev;
    190       1.1   gdamore 	struct hid_data *d;
    191       1.1   gdamore 	struct hid_item h;
    192       1.2      tron 	const void *desc;
    193       1.1   gdamore 	int locs[BTHIDBUSCF_NLOCS];
    194      1.15        ad 	int maxid, rep, dlen;
    195      1.19    plunky 	int vendor, product;
    196       1.1   gdamore 
    197       1.1   gdamore 	/*
    198       1.1   gdamore 	 * Init softc
    199       1.1   gdamore 	 */
    200      1.12    plunky 	sc->sc_dev = self;
    201       1.1   gdamore 	LIST_INIT(&sc->sc_list);
    202      1.21    plunky 	MBUFQ_INIT(&sc->sc_inq);
    203       1.9        ad 	callout_init(&sc->sc_reconnect, 0);
    204       1.1   gdamore 	callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc);
    205       1.1   gdamore 	sc->sc_state = BTHID_CLOSED;
    206       1.2      tron 	sc->sc_flags = BTHID_CONNECTING;
    207       1.2      tron 	sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL;
    208       1.2      tron 	sc->sc_intpsm = L2CAP_PSM_HID_INTR;
    209       1.1   gdamore 
    210      1.16    plunky 	sockopt_init(&sc->sc_mode, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
    211      1.21    plunky 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    212      1.21    plunky 	cv_init(&sc->sc_cv, device_xname(self));
    213      1.16    plunky 
    214       1.1   gdamore 	/*
    215       1.2      tron 	 * extract config from proplist
    216       1.1   gdamore 	 */
    217       1.3    plunky 	obj = prop_dictionary_get(dict, BTDEVladdr);
    218       1.2      tron 	bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
    219       1.2      tron 
    220       1.3    plunky 	obj = prop_dictionary_get(dict, BTDEVraddr);
    221       1.2      tron 	bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
    222       1.1   gdamore 
    223      1.19    plunky 	obj = prop_dictionary_get(dict, BTDEVvendor);
    224      1.19    plunky 	vendor = (int)prop_number_integer_value(obj);
    225      1.19    plunky 
    226      1.19    plunky 	obj = prop_dictionary_get(dict, BTDEVproduct);
    227      1.19    plunky 	product = (int)prop_number_integer_value(obj);
    228      1.19    plunky 
    229       1.8    plunky 	obj = prop_dictionary_get(dict, BTDEVmode);
    230       1.8    plunky 	if (prop_object_type(obj) == PROP_TYPE_STRING) {
    231       1.8    plunky 		if (prop_string_equals_cstring(obj, BTDEVauth))
    232      1.16    plunky 			sockopt_setint(&sc->sc_mode, L2CAP_LM_AUTH);
    233       1.8    plunky 		else if (prop_string_equals_cstring(obj, BTDEVencrypt))
    234      1.16    plunky 			sockopt_setint(&sc->sc_mode, L2CAP_LM_ENCRYPT);
    235       1.8    plunky 		else if (prop_string_equals_cstring(obj, BTDEVsecure))
    236      1.16    plunky 			sockopt_setint(&sc->sc_mode, L2CAP_LM_SECURE);
    237       1.8    plunky 		else  {
    238       1.8    plunky 			aprint_error(" unknown %s\n", BTDEVmode);
    239       1.8    plunky 			return;
    240       1.8    plunky 		}
    241       1.8    plunky 
    242       1.8    plunky 		aprint_verbose(" %s %s", BTDEVmode,
    243       1.8    plunky 					 prop_string_cstring_nocopy(obj));
    244       1.8    plunky 	}
    245       1.8    plunky 
    246       1.3    plunky 	obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
    247       1.3    plunky 	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
    248       1.2      tron 		sc->sc_ctlpsm = prop_number_integer_value(obj);
    249       1.2      tron 		if (L2CAP_PSM_INVALID(sc->sc_ctlpsm)) {
    250       1.3    plunky 			aprint_error(" invalid %s\n", BTHIDEVcontrolpsm);
    251       1.2      tron 			return;
    252       1.2      tron 		}
    253       1.2      tron 	}
    254       1.1   gdamore 
    255       1.3    plunky 	obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
    256       1.3    plunky 	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
    257       1.2      tron 		sc->sc_intpsm = prop_number_integer_value(obj);
    258       1.2      tron 		if (L2CAP_PSM_INVALID(sc->sc_intpsm)) {
    259       1.3    plunky 			aprint_error(" invalid %s\n", BTHIDEVinterruptpsm);
    260       1.2      tron 			return;
    261       1.2      tron 		}
    262       1.2      tron 	}
    263       1.2      tron 
    264       1.3    plunky 	obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
    265       1.3    plunky 	if (prop_object_type(obj) == PROP_TYPE_DATA) {
    266       1.2      tron 		dlen = prop_data_size(obj);
    267       1.2      tron 		desc = prop_data_data_nocopy(obj);
    268       1.2      tron 	} else {
    269       1.3    plunky 		aprint_error(" no %s\n", BTHIDEVdescriptor);
    270       1.1   gdamore 		return;
    271       1.1   gdamore 	}
    272       1.2      tron 
    273       1.3    plunky 	obj = prop_dictionary_get(dict, BTHIDEVreconnect);
    274       1.3    plunky 	if (prop_object_type(obj) == PROP_TYPE_BOOL
    275       1.2      tron 	    && !prop_bool_true(obj))
    276       1.2      tron 		sc->sc_flags |= BTHID_RECONNECT;
    277       1.1   gdamore 
    278       1.1   gdamore 	/*
    279       1.1   gdamore 	 * Parse the descriptor and attach child devices, one per report.
    280       1.1   gdamore 	 */
    281       1.1   gdamore 	maxid = -1;
    282       1.1   gdamore 	h.report_ID = 0;
    283       1.2      tron 	d = hid_start_parse(desc, dlen, hid_none);
    284       1.1   gdamore 	while (hid_get_item(d, &h)) {
    285       1.1   gdamore 		if (h.report_ID > maxid)
    286       1.1   gdamore 			maxid = h.report_ID;
    287       1.1   gdamore 	}
    288       1.1   gdamore 	hid_end_parse(d);
    289       1.1   gdamore 
    290       1.1   gdamore 	if (maxid < 0) {
    291       1.1   gdamore 		aprint_error(" no reports found\n");
    292       1.1   gdamore 		return;
    293       1.1   gdamore 	}
    294       1.1   gdamore 
    295       1.1   gdamore 	aprint_normal("\n");
    296       1.1   gdamore 
    297      1.21    plunky 	if (kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, bthidev_process,
    298      1.21    plunky 	    sc, &sc->sc_lwp, "%s", device_xname(self)) != 0) {
    299      1.21    plunky 		aprint_error_dev(self, "failed to create input thread\n");
    300      1.21    plunky 		return;
    301      1.21    plunky 	}
    302      1.21    plunky 
    303       1.1   gdamore 	for (rep = 0 ; rep <= maxid ; rep++) {
    304       1.2      tron 		if (hid_report_size(desc, dlen, hid_feature, rep) == 0
    305       1.2      tron 		    && hid_report_size(desc, dlen, hid_input, rep) == 0
    306       1.2      tron 		    && hid_report_size(desc, dlen, hid_output, rep) == 0)
    307       1.1   gdamore 			continue;
    308       1.1   gdamore 
    309      1.19    plunky 		bha.ba_vendor = vendor;
    310      1.19    plunky 		bha.ba_product = product;
    311       1.2      tron 		bha.ba_desc = desc;
    312       1.2      tron 		bha.ba_dlen = dlen;
    313       1.1   gdamore 		bha.ba_input = bthidev_null;
    314       1.1   gdamore 		bha.ba_feature = bthidev_null;
    315       1.1   gdamore 		bha.ba_output = bthidev_output;
    316       1.1   gdamore 		bha.ba_id = rep;
    317       1.1   gdamore 
    318       1.1   gdamore 		locs[BTHIDBUSCF_REPORTID] = rep;
    319       1.1   gdamore 
    320      1.10    plunky 		dev = config_found_sm_loc(self, "bthidbus",
    321       1.1   gdamore 					locs, &bha, bthidev_print, config_stdsubmatch);
    322       1.1   gdamore 		if (dev != NULL) {
    323      1.10    plunky 			hidev = device_private(dev);
    324      1.10    plunky 			hidev->sc_dev = dev;
    325      1.10    plunky 			hidev->sc_parent = self;
    326      1.10    plunky 			hidev->sc_id = rep;
    327      1.10    plunky 			hidev->sc_input = bha.ba_input;
    328      1.10    plunky 			hidev->sc_feature = bha.ba_feature;
    329      1.10    plunky 			LIST_INSERT_HEAD(&sc->sc_list, hidev, sc_next);
    330       1.1   gdamore 		}
    331       1.1   gdamore 	}
    332       1.1   gdamore 
    333  1.21.2.1       riz 	pmf_device_register(self, NULL, NULL);
    334  1.21.2.1       riz 
    335       1.1   gdamore 	/*
    336       1.1   gdamore 	 * start bluetooth connections
    337       1.1   gdamore 	 */
    338      1.15        ad 	mutex_enter(bt_lock);
    339       1.2      tron 	if ((sc->sc_flags & BTHID_RECONNECT) == 0)
    340       1.1   gdamore 		bthidev_listen(sc);
    341       1.1   gdamore 
    342       1.2      tron 	if (sc->sc_flags & BTHID_CONNECTING)
    343       1.1   gdamore 		bthidev_connect(sc);
    344      1.15        ad 	mutex_exit(bt_lock);
    345       1.1   gdamore }
    346       1.1   gdamore 
    347       1.1   gdamore static int
    348      1.10    plunky bthidev_detach(device_t self, int flags)
    349       1.1   gdamore {
    350      1.10    plunky 	struct bthidev_softc *sc = device_private(self);
    351      1.10    plunky 	struct bthidev *hidev;
    352       1.1   gdamore 
    353      1.15        ad 	mutex_enter(bt_lock);
    354       1.1   gdamore 	sc->sc_flags = 0;	/* disable reconnecting */
    355       1.1   gdamore 
    356       1.1   gdamore 	/* release interrupt listen */
    357       1.1   gdamore 	if (sc->sc_int_l != NULL) {
    358       1.1   gdamore 		l2cap_detach(&sc->sc_int_l);
    359       1.1   gdamore 		sc->sc_int_l = NULL;
    360       1.1   gdamore 	}
    361       1.1   gdamore 
    362       1.1   gdamore 	/* release control listen */
    363       1.1   gdamore 	if (sc->sc_ctl_l != NULL) {
    364       1.1   gdamore 		l2cap_detach(&sc->sc_ctl_l);
    365       1.1   gdamore 		sc->sc_ctl_l = NULL;
    366       1.1   gdamore 	}
    367       1.1   gdamore 
    368       1.1   gdamore 	/* close interrupt channel */
    369       1.1   gdamore 	if (sc->sc_int != NULL) {
    370       1.1   gdamore 		l2cap_disconnect(sc->sc_int, 0);
    371       1.1   gdamore 		l2cap_detach(&sc->sc_int);
    372       1.1   gdamore 		sc->sc_int = NULL;
    373       1.1   gdamore 	}
    374       1.1   gdamore 
    375       1.1   gdamore 	/* close control channel */
    376       1.1   gdamore 	if (sc->sc_ctl != NULL) {
    377       1.1   gdamore 		l2cap_disconnect(sc->sc_ctl, 0);
    378       1.1   gdamore 		l2cap_detach(&sc->sc_ctl);
    379       1.1   gdamore 		sc->sc_ctl = NULL;
    380       1.1   gdamore 	}
    381       1.1   gdamore 
    382      1.15        ad 	callout_halt(&sc->sc_reconnect, bt_lock);
    383      1.11    plunky 	callout_destroy(&sc->sc_reconnect);
    384      1.11    plunky 
    385      1.15        ad 	mutex_exit(bt_lock);
    386       1.1   gdamore 
    387  1.21.2.1       riz 	pmf_device_deregister(self);
    388  1.21.2.1       riz 
    389      1.21    plunky 	/* kill off the input processor */
    390      1.21    plunky 	if (sc->sc_lwp != NULL) {
    391      1.21    plunky 		mutex_enter(&sc->sc_lock);
    392      1.21    plunky 		sc->sc_detach = 1;
    393      1.21    plunky 		cv_signal(&sc->sc_cv);
    394      1.21    plunky 		mutex_exit(&sc->sc_lock);
    395      1.21    plunky 		kthread_join(sc->sc_lwp);
    396      1.21    plunky 		sc->sc_lwp = NULL;
    397      1.21    plunky 	}
    398      1.21    plunky 
    399       1.1   gdamore 	/* detach children */
    400      1.10    plunky 	while ((hidev = LIST_FIRST(&sc->sc_list)) != NULL) {
    401      1.10    plunky 		LIST_REMOVE(hidev, sc_next);
    402      1.10    plunky 		config_detach(hidev->sc_dev, flags);
    403       1.1   gdamore 	}
    404       1.1   gdamore 
    405      1.21    plunky 	MBUFQ_DRAIN(&sc->sc_inq);
    406      1.21    plunky 	cv_destroy(&sc->sc_cv);
    407      1.21    plunky 	mutex_destroy(&sc->sc_lock);
    408      1.16    plunky 	sockopt_destroy(&sc->sc_mode);
    409      1.16    plunky 
    410       1.1   gdamore 	return 0;
    411       1.1   gdamore }
    412       1.1   gdamore 
    413       1.1   gdamore /*
    414       1.1   gdamore  * bthidev config print
    415       1.1   gdamore  */
    416       1.1   gdamore static int
    417       1.1   gdamore bthidev_print(void *aux, const char *pnp)
    418       1.1   gdamore {
    419       1.1   gdamore 	struct bthidev_attach_args *ba = aux;
    420       1.1   gdamore 
    421       1.1   gdamore 	if (pnp != NULL)
    422       1.1   gdamore 		aprint_normal("%s:", pnp);
    423       1.1   gdamore 
    424       1.1   gdamore 	if (ba->ba_id > 0)
    425       1.1   gdamore 		aprint_normal(" reportid %d", ba->ba_id);
    426       1.1   gdamore 
    427       1.1   gdamore 	return UNCONF;
    428       1.1   gdamore }
    429       1.1   gdamore 
    430       1.1   gdamore /*****************************************************************************
    431       1.1   gdamore  *
    432       1.1   gdamore  *	bluetooth(4) HID attach/detach routines
    433       1.1   gdamore  */
    434       1.1   gdamore 
    435       1.1   gdamore /*
    436       1.4    plunky  * callouts are scheduled after connections have been lost, in order
    437       1.4    plunky  * to clean up and reconnect.
    438       1.1   gdamore  */
    439       1.1   gdamore static void
    440       1.1   gdamore bthidev_timeout(void *arg)
    441       1.1   gdamore {
    442       1.1   gdamore 	struct bthidev_softc *sc = arg;
    443       1.1   gdamore 
    444      1.15        ad 	mutex_enter(bt_lock);
    445       1.1   gdamore 	callout_ack(&sc->sc_reconnect);
    446       1.1   gdamore 
    447       1.1   gdamore 	switch (sc->sc_state) {
    448       1.1   gdamore 	case BTHID_CLOSED:
    449       1.4    plunky 		if (sc->sc_int != NULL) {
    450       1.4    plunky 			l2cap_disconnect(sc->sc_int, 0);
    451       1.4    plunky 			break;
    452       1.4    plunky 		}
    453       1.4    plunky 
    454       1.4    plunky 		if (sc->sc_ctl != NULL) {
    455       1.4    plunky 			l2cap_disconnect(sc->sc_ctl, 0);
    456       1.4    plunky 			break;
    457       1.4    plunky 		}
    458       1.4    plunky 
    459       1.4    plunky 		if (sc->sc_flags & BTHID_RECONNECT) {
    460       1.4    plunky 			sc->sc_flags |= BTHID_CONNECTING;
    461       1.4    plunky 			bthidev_connect(sc);
    462       1.4    plunky 			break;
    463       1.4    plunky 		}
    464       1.4    plunky 
    465       1.1   gdamore 		break;
    466       1.1   gdamore 
    467       1.1   gdamore 	case BTHID_WAIT_CTL:
    468       1.1   gdamore 		break;
    469       1.1   gdamore 
    470       1.1   gdamore 	case BTHID_WAIT_INT:
    471       1.1   gdamore 		break;
    472       1.1   gdamore 
    473       1.1   gdamore 	case BTHID_OPEN:
    474       1.1   gdamore 		break;
    475       1.1   gdamore 
    476       1.1   gdamore 	default:
    477       1.1   gdamore 		break;
    478       1.1   gdamore 	}
    479      1.15        ad 	mutex_exit(bt_lock);
    480       1.1   gdamore }
    481       1.1   gdamore 
    482       1.1   gdamore /*
    483       1.1   gdamore  * listen for our device
    484       1.1   gdamore  */
    485       1.1   gdamore static int
    486       1.1   gdamore bthidev_listen(struct bthidev_softc *sc)
    487       1.1   gdamore {
    488       1.1   gdamore 	struct sockaddr_bt sa;
    489       1.1   gdamore 	int err;
    490       1.1   gdamore 
    491       1.1   gdamore 	memset(&sa, 0, sizeof(sa));
    492       1.1   gdamore 	sa.bt_len = sizeof(sa);
    493       1.1   gdamore 	sa.bt_family = AF_BLUETOOTH;
    494       1.1   gdamore 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    495       1.1   gdamore 
    496       1.1   gdamore 	/*
    497       1.1   gdamore 	 * Listen on control PSM
    498       1.1   gdamore 	 */
    499       1.1   gdamore 	err = l2cap_attach(&sc->sc_ctl_l, &bthidev_ctl_proto, sc);
    500       1.1   gdamore 	if (err)
    501       1.1   gdamore 		return err;
    502       1.1   gdamore 
    503      1.16    plunky 	err = l2cap_setopt(sc->sc_ctl_l, &sc->sc_mode);
    504       1.8    plunky 	if (err)
    505       1.8    plunky 		return err;
    506       1.8    plunky 
    507       1.1   gdamore 	sa.bt_psm = sc->sc_ctlpsm;
    508       1.1   gdamore 	err = l2cap_bind(sc->sc_ctl_l, &sa);
    509       1.1   gdamore 	if (err)
    510       1.1   gdamore 		return err;
    511       1.1   gdamore 
    512       1.1   gdamore 	err = l2cap_listen(sc->sc_ctl_l);
    513       1.1   gdamore 	if (err)
    514       1.1   gdamore 		return err;
    515       1.1   gdamore 
    516       1.1   gdamore 	/*
    517       1.1   gdamore 	 * Listen on interrupt PSM
    518       1.1   gdamore 	 */
    519       1.1   gdamore 	err = l2cap_attach(&sc->sc_int_l, &bthidev_int_proto, sc);
    520       1.1   gdamore 	if (err)
    521       1.1   gdamore 		return err;
    522       1.1   gdamore 
    523      1.16    plunky 	err = l2cap_setopt(sc->sc_int_l, &sc->sc_mode);
    524       1.8    plunky 	if (err)
    525       1.8    plunky 		return err;
    526       1.8    plunky 
    527       1.1   gdamore 	sa.bt_psm = sc->sc_intpsm;
    528       1.1   gdamore 	err = l2cap_bind(sc->sc_int_l, &sa);
    529       1.1   gdamore 	if (err)
    530       1.1   gdamore 		return err;
    531       1.1   gdamore 
    532       1.1   gdamore 	err = l2cap_listen(sc->sc_int_l);
    533       1.1   gdamore 	if (err)
    534       1.1   gdamore 		return err;
    535       1.1   gdamore 
    536       1.1   gdamore 	sc->sc_state = BTHID_WAIT_CTL;
    537       1.1   gdamore 	return 0;
    538       1.1   gdamore }
    539       1.1   gdamore 
    540       1.1   gdamore /*
    541       1.1   gdamore  * start connecting to our device
    542       1.1   gdamore  */
    543       1.1   gdamore static int
    544       1.1   gdamore bthidev_connect(struct bthidev_softc *sc)
    545       1.1   gdamore {
    546       1.1   gdamore 	struct sockaddr_bt sa;
    547       1.1   gdamore 	int err;
    548       1.1   gdamore 
    549       1.1   gdamore 	if (sc->sc_attempts++ > 0)
    550      1.12    plunky 		aprint_verbose_dev(sc->sc_dev, "connect (#%d)\n", sc->sc_attempts);
    551       1.1   gdamore 
    552       1.1   gdamore 	memset(&sa, 0, sizeof(sa));
    553       1.1   gdamore 	sa.bt_len = sizeof(sa);
    554       1.1   gdamore 	sa.bt_family = AF_BLUETOOTH;
    555       1.1   gdamore 
    556       1.1   gdamore 	err = l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
    557       1.1   gdamore 	if (err) {
    558      1.12    plunky 		aprint_error_dev(sc->sc_dev, "l2cap_attach failed (%d)\n", err);
    559       1.1   gdamore 		return err;
    560       1.1   gdamore 	}
    561       1.1   gdamore 
    562      1.16    plunky 	err = l2cap_setopt(sc->sc_ctl, &sc->sc_mode);
    563       1.8    plunky 	if (err)
    564       1.8    plunky 		return err;
    565       1.8    plunky 
    566       1.1   gdamore 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    567       1.1   gdamore 	err = l2cap_bind(sc->sc_ctl, &sa);
    568       1.1   gdamore 	if (err) {
    569      1.12    plunky 		aprint_error_dev(sc->sc_dev, "l2cap_bind failed (%d)\n", err);
    570       1.1   gdamore 		return err;
    571       1.1   gdamore 	}
    572       1.1   gdamore 
    573       1.1   gdamore 	sa.bt_psm = sc->sc_ctlpsm;
    574       1.1   gdamore 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    575       1.1   gdamore 	err = l2cap_connect(sc->sc_ctl, &sa);
    576       1.1   gdamore 	if (err) {
    577      1.12    plunky 		aprint_error_dev(sc->sc_dev, "l2cap_connect failed (%d)\n", err);
    578       1.1   gdamore 		return err;
    579       1.1   gdamore 	}
    580       1.1   gdamore 
    581       1.1   gdamore 	sc->sc_state = BTHID_WAIT_CTL;
    582       1.1   gdamore 	return 0;
    583       1.1   gdamore }
    584       1.1   gdamore 
    585      1.21    plunky /*
    586      1.21    plunky  * The LWP which processes input reports, forwarding to child devices.
    587      1.21    plunky  * We are always either processing input reports, holding the lock, or
    588      1.21    plunky  * waiting for a signal on condvar.
    589      1.21    plunky  */
    590      1.21    plunky static void
    591      1.21    plunky bthidev_process(void *arg)
    592      1.21    plunky {
    593      1.21    plunky 	struct bthidev_softc *sc = arg;
    594      1.21    plunky 	struct mbuf *m;
    595      1.21    plunky 
    596      1.21    plunky 	mutex_enter(&sc->sc_lock);
    597      1.21    plunky 	while (sc->sc_detach == 0) {
    598      1.21    plunky 		MBUFQ_DEQUEUE(&sc->sc_inq, m);
    599      1.21    plunky 		if (m == NULL) {
    600      1.21    plunky 			cv_wait(&sc->sc_cv, &sc->sc_lock);
    601      1.21    plunky 			continue;
    602      1.21    plunky 		}
    603      1.21    plunky 
    604      1.21    plunky 		mutex_exit(&sc->sc_lock);
    605      1.21    plunky 		bthidev_process_one(sc, m);
    606      1.21    plunky 		m_freem(m);
    607      1.21    plunky 		mutex_enter(&sc->sc_lock);
    608      1.21    plunky 	}
    609      1.21    plunky 	mutex_exit(&sc->sc_lock);
    610      1.21    plunky 	kthread_exit(0);
    611      1.21    plunky }
    612      1.21    plunky 
    613      1.21    plunky static void
    614      1.21    plunky bthidev_process_one(struct bthidev_softc *sc, struct mbuf *m)
    615      1.21    plunky {
    616      1.21    plunky 	struct bthidev *hidev;
    617      1.21    plunky 	uint8_t *data;
    618      1.21    plunky 	int len;
    619      1.21    plunky 
    620      1.21    plunky 	if (sc->sc_state != BTHID_OPEN)
    621      1.21    plunky 		return;
    622      1.21    plunky 
    623      1.21    plunky 	if (m->m_pkthdr.len > m->m_len)
    624      1.21    plunky 		aprint_error_dev(sc->sc_dev, "truncating HID report\n");
    625      1.21    plunky 
    626      1.21    plunky 	len = m->m_len;
    627      1.21    plunky 	data = mtod(m, uint8_t *);
    628      1.21    plunky 
    629      1.21    plunky 	switch (BTHID_TYPE(data[0])) {
    630      1.21    plunky 	case BTHID_DATA:
    631      1.21    plunky 		/*
    632      1.21    plunky 		 * data[0] == type / parameter
    633      1.21    plunky 		 * data[1] == id
    634      1.21    plunky 		 * data[2..len] == report
    635      1.21    plunky 		 */
    636      1.21    plunky 		if (len < 3)
    637      1.21    plunky 			break;
    638      1.21    plunky 
    639      1.21    plunky 		LIST_FOREACH(hidev, &sc->sc_list, sc_next)
    640      1.21    plunky 			if (data[1] == hidev->sc_id)
    641      1.21    plunky 				break;
    642      1.21    plunky 
    643      1.21    plunky 		if (hidev == NULL) {
    644      1.21    plunky 			aprint_error_dev(sc->sc_dev,
    645      1.21    plunky 			    "report id %d, len = %d ignored\n", data[1], len - 2);
    646      1.21    plunky 
    647      1.21    plunky 			break;
    648      1.21    plunky 		}
    649      1.21    plunky 
    650      1.21    plunky 		switch (BTHID_DATA_PARAM(data[0])) {
    651      1.21    plunky 		case BTHID_DATA_INPUT:
    652      1.21    plunky 			(*hidev->sc_input)(hidev, data + 2, len - 2);
    653      1.21    plunky 			break;
    654      1.21    plunky 
    655      1.21    plunky 		case BTHID_DATA_FEATURE:
    656      1.21    plunky 			(*hidev->sc_feature)(hidev, data + 2, len - 2);
    657      1.21    plunky 			break;
    658      1.21    plunky 
    659      1.21    plunky 		default:
    660      1.21    plunky 			break;
    661      1.21    plunky 		}
    662      1.21    plunky 
    663      1.21    plunky 		break;
    664      1.21    plunky 
    665      1.21    plunky 	case BTHID_CONTROL:
    666      1.21    plunky 		if (len < 1)
    667      1.21    plunky 			break;
    668      1.21    plunky 
    669      1.21    plunky 		switch (BTHID_DATA_PARAM(data[0])) {
    670      1.21    plunky 		case BTHID_CONTROL_UNPLUG:
    671      1.21    plunky 			aprint_normal_dev(sc->sc_dev, "unplugged\n");
    672      1.21    plunky 
    673      1.21    plunky 			mutex_enter(bt_lock);
    674      1.21    plunky 			/* close interrupt channel */
    675      1.21    plunky 			if (sc->sc_int != NULL) {
    676      1.21    plunky 				l2cap_disconnect(sc->sc_int, 0);
    677      1.21    plunky 				l2cap_detach(&sc->sc_int);
    678      1.21    plunky 				sc->sc_int = NULL;
    679      1.21    plunky 			}
    680      1.21    plunky 
    681      1.21    plunky 			/* close control channel */
    682      1.21    plunky 			if (sc->sc_ctl != NULL) {
    683      1.21    plunky 				l2cap_disconnect(sc->sc_ctl, 0);
    684      1.21    plunky 				l2cap_detach(&sc->sc_ctl);
    685      1.21    plunky 				sc->sc_ctl = NULL;
    686      1.21    plunky 			}
    687      1.21    plunky 			mutex_exit(bt_lock);
    688      1.21    plunky 
    689      1.21    plunky 			break;
    690      1.21    plunky 
    691      1.21    plunky 		default:
    692      1.21    plunky 			break;
    693      1.21    plunky 		}
    694      1.21    plunky 
    695      1.21    plunky 		break;
    696      1.21    plunky 
    697      1.21    plunky 	default:
    698      1.21    plunky 		break;
    699      1.21    plunky 	}
    700      1.21    plunky }
    701      1.21    plunky 
    702       1.1   gdamore /*****************************************************************************
    703       1.1   gdamore  *
    704       1.1   gdamore  *	bluetooth(9) callback methods for L2CAP
    705       1.1   gdamore  *
    706       1.1   gdamore  *	All these are called from Bluetooth Protocol code, in a soft
    707       1.1   gdamore  *	interrupt context at IPL_SOFTNET.
    708       1.1   gdamore  */
    709       1.1   gdamore 
    710       1.1   gdamore static void
    711       1.7  christos bthidev_connecting(void *arg)
    712       1.1   gdamore {
    713       1.1   gdamore 
    714       1.1   gdamore 	/* dont care */
    715       1.1   gdamore }
    716       1.1   gdamore 
    717       1.1   gdamore static void
    718       1.1   gdamore bthidev_ctl_connected(void *arg)
    719       1.1   gdamore {
    720       1.1   gdamore 	struct sockaddr_bt sa;
    721       1.1   gdamore 	struct bthidev_softc *sc = arg;
    722       1.1   gdamore 	int err;
    723       1.1   gdamore 
    724       1.1   gdamore 	if (sc->sc_state != BTHID_WAIT_CTL)
    725       1.1   gdamore 		return;
    726       1.1   gdamore 
    727       1.1   gdamore 	KASSERT(sc->sc_ctl != NULL);
    728       1.1   gdamore 	KASSERT(sc->sc_int == NULL);
    729       1.1   gdamore 
    730       1.2      tron 	if (sc->sc_flags & BTHID_CONNECTING) {
    731       1.1   gdamore 		/* initiate connect on interrupt PSM */
    732       1.1   gdamore 		err = l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
    733       1.1   gdamore 		if (err)
    734       1.1   gdamore 			goto fail;
    735       1.1   gdamore 
    736      1.16    plunky 		err = l2cap_setopt(sc->sc_int, &sc->sc_mode);
    737       1.8    plunky 		if (err)
    738       1.8    plunky 			goto fail;
    739       1.8    plunky 
    740       1.1   gdamore 		memset(&sa, 0, sizeof(sa));
    741       1.1   gdamore 		sa.bt_len = sizeof(sa);
    742       1.1   gdamore 		sa.bt_family = AF_BLUETOOTH;
    743       1.1   gdamore 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    744       1.1   gdamore 
    745       1.1   gdamore 		err = l2cap_bind(sc->sc_int, &sa);
    746       1.1   gdamore 		if (err)
    747       1.1   gdamore 			goto fail;
    748       1.1   gdamore 
    749       1.1   gdamore 		sa.bt_psm = sc->sc_intpsm;
    750       1.1   gdamore 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    751       1.1   gdamore 		err = l2cap_connect(sc->sc_int, &sa);
    752       1.1   gdamore 		if (err)
    753       1.1   gdamore 			goto fail;
    754       1.1   gdamore 	}
    755       1.1   gdamore 
    756       1.1   gdamore 	sc->sc_state = BTHID_WAIT_INT;
    757       1.1   gdamore 	return;
    758       1.1   gdamore 
    759       1.1   gdamore fail:
    760       1.1   gdamore 	l2cap_detach(&sc->sc_ctl);
    761       1.1   gdamore 	sc->sc_ctl = NULL;
    762       1.3    plunky 
    763      1.12    plunky 	aprint_error_dev(sc->sc_dev, "connect failed (%d)\n", err);
    764       1.1   gdamore }
    765       1.1   gdamore 
    766       1.1   gdamore static void
    767       1.1   gdamore bthidev_int_connected(void *arg)
    768       1.1   gdamore {
    769       1.1   gdamore 	struct bthidev_softc *sc = arg;
    770       1.1   gdamore 
    771       1.1   gdamore 	if (sc->sc_state != BTHID_WAIT_INT)
    772       1.1   gdamore 		return;
    773       1.1   gdamore 
    774       1.1   gdamore 	KASSERT(sc->sc_ctl != NULL);
    775       1.1   gdamore 	KASSERT(sc->sc_int != NULL);
    776       1.1   gdamore 
    777       1.1   gdamore 	sc->sc_attempts = 0;
    778       1.2      tron 	sc->sc_flags &= ~BTHID_CONNECTING;
    779       1.1   gdamore 	sc->sc_state = BTHID_OPEN;
    780       1.1   gdamore 
    781      1.13    plunky 	aprint_normal_dev(sc->sc_dev, "connected\n");
    782       1.1   gdamore }
    783       1.1   gdamore 
    784       1.1   gdamore /*
    785       1.1   gdamore  * Disconnected
    786       1.1   gdamore  *
    787       1.1   gdamore  * Depending on our state, this could mean several things, but essentially
    788       1.2      tron  * we are lost. If both channels are closed, and we are marked to reconnect,
    789       1.2      tron  * schedule another try otherwise just give up. They will contact us.
    790       1.1   gdamore  */
    791       1.1   gdamore static void
    792       1.7  christos bthidev_ctl_disconnected(void *arg, int err)
    793       1.1   gdamore {
    794       1.1   gdamore 	struct bthidev_softc *sc = arg;
    795       1.1   gdamore 
    796       1.1   gdamore 	if (sc->sc_ctl != NULL) {
    797       1.1   gdamore 		l2cap_detach(&sc->sc_ctl);
    798       1.1   gdamore 		sc->sc_ctl = NULL;
    799       1.1   gdamore 	}
    800       1.1   gdamore 
    801       1.1   gdamore 	sc->sc_state = BTHID_CLOSED;
    802       1.1   gdamore 
    803       1.1   gdamore 	if (sc->sc_int == NULL) {
    804      1.13    plunky 		aprint_normal_dev(sc->sc_dev, "disconnected\n");
    805       1.2      tron 		sc->sc_flags &= ~BTHID_CONNECTING;
    806       1.1   gdamore 
    807       1.2      tron 		if (sc->sc_flags & BTHID_RECONNECT)
    808       1.1   gdamore 			callout_schedule(&sc->sc_reconnect,
    809       1.1   gdamore 					BTHID_RETRY_INTERVAL * hz);
    810       1.1   gdamore 		else
    811       1.1   gdamore 			sc->sc_state = BTHID_WAIT_CTL;
    812       1.2      tron 	} else {
    813       1.2      tron 		/*
    814       1.2      tron 		 * The interrupt channel should have been closed first,
    815       1.4    plunky 		 * but its potentially unsafe to detach that from here.
    816       1.4    plunky 		 * Give them a second to do the right thing or let the
    817       1.4    plunky 		 * callout handle it.
    818       1.2      tron 		 */
    819       1.4    plunky 		callout_schedule(&sc->sc_reconnect, hz);
    820       1.1   gdamore 	}
    821       1.1   gdamore }
    822       1.1   gdamore 
    823       1.1   gdamore static void
    824       1.7  christos bthidev_int_disconnected(void *arg, int err)
    825       1.1   gdamore {
    826       1.1   gdamore 	struct bthidev_softc *sc = arg;
    827       1.1   gdamore 
    828       1.1   gdamore 	if (sc->sc_int != NULL) {
    829       1.1   gdamore 		l2cap_detach(&sc->sc_int);
    830       1.1   gdamore 		sc->sc_int = NULL;
    831       1.1   gdamore 	}
    832       1.1   gdamore 
    833       1.1   gdamore 	sc->sc_state = BTHID_CLOSED;
    834       1.1   gdamore 
    835       1.1   gdamore 	if (sc->sc_ctl == NULL) {
    836      1.13    plunky 		aprint_normal_dev(sc->sc_dev, "disconnected\n");
    837       1.2      tron 		sc->sc_flags &= ~BTHID_CONNECTING;
    838       1.1   gdamore 
    839       1.2      tron 		if (sc->sc_flags & BTHID_RECONNECT)
    840       1.1   gdamore 			callout_schedule(&sc->sc_reconnect,
    841       1.1   gdamore 					BTHID_RETRY_INTERVAL * hz);
    842       1.1   gdamore 		else
    843       1.1   gdamore 			sc->sc_state = BTHID_WAIT_CTL;
    844       1.4    plunky 	} else {
    845       1.4    plunky 		/*
    846       1.4    plunky 		 * The control channel should be closing also, allow
    847       1.4    plunky 		 * them a chance to do that before we force it.
    848       1.4    plunky 		 */
    849       1.4    plunky 		callout_schedule(&sc->sc_reconnect, hz);
    850       1.1   gdamore 	}
    851       1.1   gdamore }
    852       1.1   gdamore 
    853       1.1   gdamore /*
    854       1.1   gdamore  * New Connections
    855       1.1   gdamore  *
    856       1.1   gdamore  * We give a new L2CAP handle back if this matches the BDADDR we are
    857       1.1   gdamore  * listening for and we are in the right state. bthidev_connected will
    858       1.1   gdamore  * be called when the connection is open, so nothing else to do here
    859       1.1   gdamore  */
    860       1.1   gdamore static void *
    861       1.7  christos bthidev_ctl_newconn(void *arg, struct sockaddr_bt *laddr,
    862       1.6  christos     struct sockaddr_bt *raddr)
    863       1.1   gdamore {
    864       1.1   gdamore 	struct bthidev_softc *sc = arg;
    865       1.1   gdamore 
    866      1.18    plunky 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
    867      1.18    plunky 		return NULL;
    868      1.18    plunky 
    869      1.18    plunky 	if ((sc->sc_flags & BTHID_CONNECTING)
    870       1.1   gdamore 	    || sc->sc_state != BTHID_WAIT_CTL
    871       1.1   gdamore 	    || sc->sc_ctl != NULL
    872      1.18    plunky 	    || sc->sc_int != NULL) {
    873      1.18    plunky 		aprint_verbose_dev(sc->sc_dev, "reject ctl newconn %s%s%s%s\n",
    874      1.18    plunky 		    (sc->sc_flags & BTHID_CONNECTING) ? " (CONNECTING)" : "",
    875      1.18    plunky 		    (sc->sc_state == BTHID_WAIT_CTL) ? " (WAITING)": "",
    876      1.18    plunky 		    (sc->sc_ctl != NULL) ? " (GOT CONTROL)" : "",
    877      1.18    plunky 		    (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
    878      1.18    plunky 
    879       1.1   gdamore 		return NULL;
    880      1.18    plunky 	}
    881       1.1   gdamore 
    882       1.1   gdamore 	l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
    883       1.1   gdamore 	return sc->sc_ctl;
    884       1.1   gdamore }
    885       1.1   gdamore 
    886       1.1   gdamore static void *
    887       1.7  christos bthidev_int_newconn(void *arg, struct sockaddr_bt *laddr,
    888       1.6  christos     struct sockaddr_bt *raddr)
    889       1.1   gdamore {
    890       1.1   gdamore 	struct bthidev_softc *sc = arg;
    891       1.1   gdamore 
    892      1.18    plunky 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
    893      1.18    plunky 		return NULL;
    894      1.18    plunky 
    895      1.18    plunky 	if ((sc->sc_flags & BTHID_CONNECTING)
    896       1.1   gdamore 	    || sc->sc_state != BTHID_WAIT_INT
    897       1.1   gdamore 	    || sc->sc_ctl == NULL
    898      1.18    plunky 	    || sc->sc_int != NULL) {
    899      1.18    plunky 		aprint_verbose_dev(sc->sc_dev, "reject int newconn %s%s%s%s\n",
    900      1.18    plunky 		    (sc->sc_flags & BTHID_CONNECTING) ? " (CONNECTING)" : "",
    901      1.18    plunky 		    (sc->sc_state == BTHID_WAIT_INT) ? " (WAITING)": "",
    902      1.18    plunky 		    (sc->sc_ctl == NULL) ? " (NO CONTROL)" : "",
    903      1.18    plunky 		    (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
    904      1.18    plunky 
    905       1.1   gdamore 		return NULL;
    906      1.18    plunky 	}
    907       1.1   gdamore 
    908       1.1   gdamore 	l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
    909       1.1   gdamore 	return sc->sc_int;
    910       1.1   gdamore }
    911       1.1   gdamore 
    912       1.1   gdamore static void
    913       1.7  christos bthidev_complete(void *arg, int count)
    914       1.1   gdamore {
    915       1.1   gdamore 
    916       1.1   gdamore 	/* dont care */
    917       1.1   gdamore }
    918       1.1   gdamore 
    919       1.8    plunky static void
    920       1.8    plunky bthidev_linkmode(void *arg, int new)
    921       1.8    plunky {
    922       1.8    plunky 	struct bthidev_softc *sc = arg;
    923      1.16    plunky 	int mode;
    924      1.16    plunky 
    925      1.16    plunky 	(void)sockopt_getint(&sc->sc_mode, &mode);
    926       1.8    plunky 
    927      1.16    plunky 	if ((mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
    928      1.12    plunky 		aprint_error_dev(sc->sc_dev, "auth failed\n");
    929      1.16    plunky 	else if ((mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
    930      1.12    plunky 		aprint_error_dev(sc->sc_dev, "encrypt off\n");
    931      1.16    plunky 	else if ((mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE))
    932      1.12    plunky 		aprint_error_dev(sc->sc_dev, "insecure\n");
    933       1.8    plunky 	else
    934       1.8    plunky 		return;
    935       1.8    plunky 
    936       1.8    plunky 	if (sc->sc_int != NULL)
    937       1.8    plunky 		l2cap_disconnect(sc->sc_int, 0);
    938       1.8    plunky 
    939       1.8    plunky 	if (sc->sc_ctl != NULL)
    940       1.8    plunky 		l2cap_disconnect(sc->sc_ctl, 0);
    941       1.8    plunky }
    942       1.8    plunky 
    943       1.1   gdamore /*
    944      1.21    plunky  * Receive reports from the protocol stack. Because this will be called
    945      1.21    plunky  * with bt_lock held, we queue the mbuf and process it with a kernel thread
    946       1.1   gdamore  */
    947       1.1   gdamore static void
    948       1.1   gdamore bthidev_input(void *arg, struct mbuf *m)
    949       1.1   gdamore {
    950       1.1   gdamore 	struct bthidev_softc *sc = arg;
    951       1.1   gdamore 
    952      1.21    plunky 	if (sc->sc_state != BTHID_OPEN) {
    953      1.21    plunky 		m_freem(m);
    954      1.21    plunky 		return;
    955       1.1   gdamore 	}
    956       1.1   gdamore 
    957      1.21    plunky 	mutex_enter(&sc->sc_lock);
    958      1.21    plunky 	MBUFQ_ENQUEUE(&sc->sc_inq, m);
    959      1.21    plunky 	cv_signal(&sc->sc_cv);
    960      1.21    plunky 	mutex_exit(&sc->sc_lock);
    961       1.1   gdamore }
    962       1.1   gdamore 
    963       1.1   gdamore /*****************************************************************************
    964       1.1   gdamore  *
    965       1.1   gdamore  *	IO routines
    966       1.1   gdamore  */
    967       1.1   gdamore 
    968       1.1   gdamore static void
    969      1.10    plunky bthidev_null(struct bthidev *hidev, uint8_t *report, int len)
    970       1.1   gdamore {
    971       1.1   gdamore 
    972       1.1   gdamore 	/*
    973       1.1   gdamore 	 * empty routine just in case the device
    974       1.1   gdamore 	 * provided no method to handle this report
    975       1.1   gdamore 	 */
    976       1.1   gdamore }
    977       1.1   gdamore 
    978       1.1   gdamore static int
    979      1.10    plunky bthidev_output(struct bthidev *hidev, uint8_t *report, int rlen)
    980       1.1   gdamore {
    981      1.10    plunky 	struct bthidev_softc *sc = device_private(hidev->sc_parent);
    982       1.1   gdamore 	struct mbuf *m;
    983      1.15        ad 	int err;
    984       1.1   gdamore 
    985       1.1   gdamore 	if (sc == NULL || sc->sc_state != BTHID_OPEN)
    986       1.1   gdamore 		return ENOTCONN;
    987       1.1   gdamore 
    988       1.1   gdamore 	KASSERT(sc->sc_ctl != NULL);
    989       1.1   gdamore 	KASSERT(sc->sc_int != NULL);
    990       1.1   gdamore 
    991       1.1   gdamore 	if (rlen == 0 || report == NULL)
    992       1.1   gdamore 		return 0;
    993       1.1   gdamore 
    994       1.1   gdamore 	if (rlen > MHLEN - 2) {
    995      1.12    plunky 		aprint_error_dev(sc->sc_dev,
    996      1.12    plunky 		    "output report too long (%d)!\n", rlen);
    997       1.1   gdamore 		return EMSGSIZE;
    998       1.1   gdamore 	}
    999       1.1   gdamore 
   1000       1.1   gdamore 	m = m_gethdr(M_DONTWAIT, MT_DATA);
   1001       1.1   gdamore 	if (m == NULL)
   1002       1.1   gdamore 		return ENOMEM;
   1003       1.1   gdamore 
   1004       1.1   gdamore 	/*
   1005       1.1   gdamore 	 * data[0] = type / parameter
   1006       1.1   gdamore 	 * data[1] = id
   1007       1.1   gdamore 	 * data[2..N] = report
   1008       1.1   gdamore 	 */
   1009       1.1   gdamore 	mtod(m, uint8_t *)[0] = (uint8_t)((BTHID_DATA << 4) | BTHID_DATA_OUTPUT);
   1010      1.10    plunky 	mtod(m, uint8_t *)[1] = hidev->sc_id;
   1011       1.1   gdamore 	memcpy(mtod(m, uint8_t *) + 2, report, rlen);
   1012       1.1   gdamore 	m->m_pkthdr.len = m->m_len = rlen + 2;
   1013       1.1   gdamore 
   1014      1.21    plunky 	mutex_enter(bt_lock);
   1015       1.1   gdamore 	err = l2cap_send(sc->sc_int, m);
   1016      1.21    plunky 	mutex_exit(bt_lock);
   1017       1.1   gdamore 
   1018       1.1   gdamore 	return err;
   1019       1.1   gdamore }
   1020