Home | History | Annotate | Line # | Download | only in bluetooth
bthidev.c revision 1.19.12.1
      1  1.19.12.1       mrg /*	$NetBSD: bthidev.c,v 1.19.12.1 2012/02/18 07:34:09 mrg 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.19.12.1       mrg __KERNEL_RCSID(0, "$NetBSD: bthidev.c,v 1.19.12.1 2012/02/18 07:34:09 mrg Exp $");
     36        1.1   gdamore 
     37        1.1   gdamore #include <sys/param.h>
     38  1.19.12.1       mrg #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.19.12.1       mrg #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.19.12.1       mrg #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.19.12.1       mrg 	MBUFQ_HEAD()		sc_inq;		/* input queue */
     90  1.19.12.1       mrg 	kmutex_t		sc_lock;	/* input queue lock */
     91  1.19.12.1       mrg 	kcondvar_t		sc_cv;		/* input queue trigger */
     92  1.19.12.1       mrg 	lwp_t			*sc_lwp;	/* input queue processor */
     93  1.19.12.1       mrg 	int			sc_detach;
     94  1.19.12.1       mrg 
     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.19.12.1       mrg static void bthidev_process(void *);
    120  1.19.12.1       mrg 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.19.12.1       mrg 	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.19.12.1       mrg 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    212  1.19.12.1       mrg 	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.19.12.1       mrg 	if (kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, bthidev_process,
    298  1.19.12.1       mrg 	    sc, &sc->sc_lwp, "%s", device_xname(self)) != 0) {
    299  1.19.12.1       mrg 		aprint_error_dev(self, "failed to create input thread\n");
    300  1.19.12.1       mrg 		return;
    301  1.19.12.1       mrg 	}
    302  1.19.12.1       mrg 
    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.1   gdamore 	/*
    334        1.1   gdamore 	 * start bluetooth connections
    335        1.1   gdamore 	 */
    336       1.15        ad 	mutex_enter(bt_lock);
    337        1.2      tron 	if ((sc->sc_flags & BTHID_RECONNECT) == 0)
    338        1.1   gdamore 		bthidev_listen(sc);
    339        1.1   gdamore 
    340        1.2      tron 	if (sc->sc_flags & BTHID_CONNECTING)
    341        1.1   gdamore 		bthidev_connect(sc);
    342       1.15        ad 	mutex_exit(bt_lock);
    343        1.1   gdamore }
    344        1.1   gdamore 
    345        1.1   gdamore static int
    346       1.10    plunky bthidev_detach(device_t self, int flags)
    347        1.1   gdamore {
    348       1.10    plunky 	struct bthidev_softc *sc = device_private(self);
    349       1.10    plunky 	struct bthidev *hidev;
    350        1.1   gdamore 
    351       1.15        ad 	mutex_enter(bt_lock);
    352        1.1   gdamore 	sc->sc_flags = 0;	/* disable reconnecting */
    353        1.1   gdamore 
    354        1.1   gdamore 	/* release interrupt listen */
    355        1.1   gdamore 	if (sc->sc_int_l != NULL) {
    356        1.1   gdamore 		l2cap_detach(&sc->sc_int_l);
    357        1.1   gdamore 		sc->sc_int_l = NULL;
    358        1.1   gdamore 	}
    359        1.1   gdamore 
    360        1.1   gdamore 	/* release control listen */
    361        1.1   gdamore 	if (sc->sc_ctl_l != NULL) {
    362        1.1   gdamore 		l2cap_detach(&sc->sc_ctl_l);
    363        1.1   gdamore 		sc->sc_ctl_l = NULL;
    364        1.1   gdamore 	}
    365        1.1   gdamore 
    366        1.1   gdamore 	/* close interrupt channel */
    367        1.1   gdamore 	if (sc->sc_int != NULL) {
    368        1.1   gdamore 		l2cap_disconnect(sc->sc_int, 0);
    369        1.1   gdamore 		l2cap_detach(&sc->sc_int);
    370        1.1   gdamore 		sc->sc_int = NULL;
    371        1.1   gdamore 	}
    372        1.1   gdamore 
    373        1.1   gdamore 	/* close control channel */
    374        1.1   gdamore 	if (sc->sc_ctl != NULL) {
    375        1.1   gdamore 		l2cap_disconnect(sc->sc_ctl, 0);
    376        1.1   gdamore 		l2cap_detach(&sc->sc_ctl);
    377        1.1   gdamore 		sc->sc_ctl = NULL;
    378        1.1   gdamore 	}
    379        1.1   gdamore 
    380       1.15        ad 	callout_halt(&sc->sc_reconnect, bt_lock);
    381       1.11    plunky 	callout_destroy(&sc->sc_reconnect);
    382       1.11    plunky 
    383       1.15        ad 	mutex_exit(bt_lock);
    384        1.1   gdamore 
    385  1.19.12.1       mrg 	/* kill off the input processor */
    386  1.19.12.1       mrg 	if (sc->sc_lwp != NULL) {
    387  1.19.12.1       mrg 		mutex_enter(&sc->sc_lock);
    388  1.19.12.1       mrg 		sc->sc_detach = 1;
    389  1.19.12.1       mrg 		cv_signal(&sc->sc_cv);
    390  1.19.12.1       mrg 		mutex_exit(&sc->sc_lock);
    391  1.19.12.1       mrg 		kthread_join(sc->sc_lwp);
    392  1.19.12.1       mrg 		sc->sc_lwp = NULL;
    393  1.19.12.1       mrg 	}
    394  1.19.12.1       mrg 
    395        1.1   gdamore 	/* detach children */
    396       1.10    plunky 	while ((hidev = LIST_FIRST(&sc->sc_list)) != NULL) {
    397       1.10    plunky 		LIST_REMOVE(hidev, sc_next);
    398       1.10    plunky 		config_detach(hidev->sc_dev, flags);
    399        1.1   gdamore 	}
    400        1.1   gdamore 
    401  1.19.12.1       mrg 	MBUFQ_DRAIN(&sc->sc_inq);
    402  1.19.12.1       mrg 	cv_destroy(&sc->sc_cv);
    403  1.19.12.1       mrg 	mutex_destroy(&sc->sc_lock);
    404       1.16    plunky 	sockopt_destroy(&sc->sc_mode);
    405       1.16    plunky 
    406        1.1   gdamore 	return 0;
    407        1.1   gdamore }
    408        1.1   gdamore 
    409        1.1   gdamore /*
    410        1.1   gdamore  * bthidev config print
    411        1.1   gdamore  */
    412        1.1   gdamore static int
    413        1.1   gdamore bthidev_print(void *aux, const char *pnp)
    414        1.1   gdamore {
    415        1.1   gdamore 	struct bthidev_attach_args *ba = aux;
    416        1.1   gdamore 
    417        1.1   gdamore 	if (pnp != NULL)
    418        1.1   gdamore 		aprint_normal("%s:", pnp);
    419        1.1   gdamore 
    420        1.1   gdamore 	if (ba->ba_id > 0)
    421        1.1   gdamore 		aprint_normal(" reportid %d", ba->ba_id);
    422        1.1   gdamore 
    423        1.1   gdamore 	return UNCONF;
    424        1.1   gdamore }
    425        1.1   gdamore 
    426        1.1   gdamore /*****************************************************************************
    427        1.1   gdamore  *
    428        1.1   gdamore  *	bluetooth(4) HID attach/detach routines
    429        1.1   gdamore  */
    430        1.1   gdamore 
    431        1.1   gdamore /*
    432        1.4    plunky  * callouts are scheduled after connections have been lost, in order
    433        1.4    plunky  * to clean up and reconnect.
    434        1.1   gdamore  */
    435        1.1   gdamore static void
    436        1.1   gdamore bthidev_timeout(void *arg)
    437        1.1   gdamore {
    438        1.1   gdamore 	struct bthidev_softc *sc = arg;
    439        1.1   gdamore 
    440       1.15        ad 	mutex_enter(bt_lock);
    441        1.1   gdamore 	callout_ack(&sc->sc_reconnect);
    442        1.1   gdamore 
    443        1.1   gdamore 	switch (sc->sc_state) {
    444        1.1   gdamore 	case BTHID_CLOSED:
    445        1.4    plunky 		if (sc->sc_int != NULL) {
    446        1.4    plunky 			l2cap_disconnect(sc->sc_int, 0);
    447        1.4    plunky 			break;
    448        1.4    plunky 		}
    449        1.4    plunky 
    450        1.4    plunky 		if (sc->sc_ctl != NULL) {
    451        1.4    plunky 			l2cap_disconnect(sc->sc_ctl, 0);
    452        1.4    plunky 			break;
    453        1.4    plunky 		}
    454        1.4    plunky 
    455        1.4    plunky 		if (sc->sc_flags & BTHID_RECONNECT) {
    456        1.4    plunky 			sc->sc_flags |= BTHID_CONNECTING;
    457        1.4    plunky 			bthidev_connect(sc);
    458        1.4    plunky 			break;
    459        1.4    plunky 		}
    460        1.4    plunky 
    461        1.1   gdamore 		break;
    462        1.1   gdamore 
    463        1.1   gdamore 	case BTHID_WAIT_CTL:
    464        1.1   gdamore 		break;
    465        1.1   gdamore 
    466        1.1   gdamore 	case BTHID_WAIT_INT:
    467        1.1   gdamore 		break;
    468        1.1   gdamore 
    469        1.1   gdamore 	case BTHID_OPEN:
    470        1.1   gdamore 		break;
    471        1.1   gdamore 
    472        1.1   gdamore 	default:
    473        1.1   gdamore 		break;
    474        1.1   gdamore 	}
    475       1.15        ad 	mutex_exit(bt_lock);
    476        1.1   gdamore }
    477        1.1   gdamore 
    478        1.1   gdamore /*
    479        1.1   gdamore  * listen for our device
    480        1.1   gdamore  */
    481        1.1   gdamore static int
    482        1.1   gdamore bthidev_listen(struct bthidev_softc *sc)
    483        1.1   gdamore {
    484        1.1   gdamore 	struct sockaddr_bt sa;
    485        1.1   gdamore 	int err;
    486        1.1   gdamore 
    487        1.1   gdamore 	memset(&sa, 0, sizeof(sa));
    488        1.1   gdamore 	sa.bt_len = sizeof(sa);
    489        1.1   gdamore 	sa.bt_family = AF_BLUETOOTH;
    490        1.1   gdamore 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    491        1.1   gdamore 
    492        1.1   gdamore 	/*
    493        1.1   gdamore 	 * Listen on control PSM
    494        1.1   gdamore 	 */
    495        1.1   gdamore 	err = l2cap_attach(&sc->sc_ctl_l, &bthidev_ctl_proto, sc);
    496        1.1   gdamore 	if (err)
    497        1.1   gdamore 		return err;
    498        1.1   gdamore 
    499       1.16    plunky 	err = l2cap_setopt(sc->sc_ctl_l, &sc->sc_mode);
    500        1.8    plunky 	if (err)
    501        1.8    plunky 		return err;
    502        1.8    plunky 
    503        1.1   gdamore 	sa.bt_psm = sc->sc_ctlpsm;
    504        1.1   gdamore 	err = l2cap_bind(sc->sc_ctl_l, &sa);
    505        1.1   gdamore 	if (err)
    506        1.1   gdamore 		return err;
    507        1.1   gdamore 
    508        1.1   gdamore 	err = l2cap_listen(sc->sc_ctl_l);
    509        1.1   gdamore 	if (err)
    510        1.1   gdamore 		return err;
    511        1.1   gdamore 
    512        1.1   gdamore 	/*
    513        1.1   gdamore 	 * Listen on interrupt PSM
    514        1.1   gdamore 	 */
    515        1.1   gdamore 	err = l2cap_attach(&sc->sc_int_l, &bthidev_int_proto, sc);
    516        1.1   gdamore 	if (err)
    517        1.1   gdamore 		return err;
    518        1.1   gdamore 
    519       1.16    plunky 	err = l2cap_setopt(sc->sc_int_l, &sc->sc_mode);
    520        1.8    plunky 	if (err)
    521        1.8    plunky 		return err;
    522        1.8    plunky 
    523        1.1   gdamore 	sa.bt_psm = sc->sc_intpsm;
    524        1.1   gdamore 	err = l2cap_bind(sc->sc_int_l, &sa);
    525        1.1   gdamore 	if (err)
    526        1.1   gdamore 		return err;
    527        1.1   gdamore 
    528        1.1   gdamore 	err = l2cap_listen(sc->sc_int_l);
    529        1.1   gdamore 	if (err)
    530        1.1   gdamore 		return err;
    531        1.1   gdamore 
    532        1.1   gdamore 	sc->sc_state = BTHID_WAIT_CTL;
    533        1.1   gdamore 	return 0;
    534        1.1   gdamore }
    535        1.1   gdamore 
    536        1.1   gdamore /*
    537        1.1   gdamore  * start connecting to our device
    538        1.1   gdamore  */
    539        1.1   gdamore static int
    540        1.1   gdamore bthidev_connect(struct bthidev_softc *sc)
    541        1.1   gdamore {
    542        1.1   gdamore 	struct sockaddr_bt sa;
    543        1.1   gdamore 	int err;
    544        1.1   gdamore 
    545        1.1   gdamore 	if (sc->sc_attempts++ > 0)
    546       1.12    plunky 		aprint_verbose_dev(sc->sc_dev, "connect (#%d)\n", sc->sc_attempts);
    547        1.1   gdamore 
    548        1.1   gdamore 	memset(&sa, 0, sizeof(sa));
    549        1.1   gdamore 	sa.bt_len = sizeof(sa);
    550        1.1   gdamore 	sa.bt_family = AF_BLUETOOTH;
    551        1.1   gdamore 
    552        1.1   gdamore 	err = l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
    553        1.1   gdamore 	if (err) {
    554       1.12    plunky 		aprint_error_dev(sc->sc_dev, "l2cap_attach failed (%d)\n", err);
    555        1.1   gdamore 		return err;
    556        1.1   gdamore 	}
    557        1.1   gdamore 
    558       1.16    plunky 	err = l2cap_setopt(sc->sc_ctl, &sc->sc_mode);
    559        1.8    plunky 	if (err)
    560        1.8    plunky 		return err;
    561        1.8    plunky 
    562        1.1   gdamore 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    563        1.1   gdamore 	err = l2cap_bind(sc->sc_ctl, &sa);
    564        1.1   gdamore 	if (err) {
    565       1.12    plunky 		aprint_error_dev(sc->sc_dev, "l2cap_bind failed (%d)\n", err);
    566        1.1   gdamore 		return err;
    567        1.1   gdamore 	}
    568        1.1   gdamore 
    569        1.1   gdamore 	sa.bt_psm = sc->sc_ctlpsm;
    570        1.1   gdamore 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    571        1.1   gdamore 	err = l2cap_connect(sc->sc_ctl, &sa);
    572        1.1   gdamore 	if (err) {
    573       1.12    plunky 		aprint_error_dev(sc->sc_dev, "l2cap_connect failed (%d)\n", err);
    574        1.1   gdamore 		return err;
    575        1.1   gdamore 	}
    576        1.1   gdamore 
    577        1.1   gdamore 	sc->sc_state = BTHID_WAIT_CTL;
    578        1.1   gdamore 	return 0;
    579        1.1   gdamore }
    580        1.1   gdamore 
    581  1.19.12.1       mrg /*
    582  1.19.12.1       mrg  * The LWP which processes input reports, forwarding to child devices.
    583  1.19.12.1       mrg  * We are always either processing input reports, holding the lock, or
    584  1.19.12.1       mrg  * waiting for a signal on condvar.
    585  1.19.12.1       mrg  */
    586  1.19.12.1       mrg static void
    587  1.19.12.1       mrg bthidev_process(void *arg)
    588  1.19.12.1       mrg {
    589  1.19.12.1       mrg 	struct bthidev_softc *sc = arg;
    590  1.19.12.1       mrg 	struct mbuf *m;
    591  1.19.12.1       mrg 
    592  1.19.12.1       mrg 	mutex_enter(&sc->sc_lock);
    593  1.19.12.1       mrg 	while (sc->sc_detach == 0) {
    594  1.19.12.1       mrg 		MBUFQ_DEQUEUE(&sc->sc_inq, m);
    595  1.19.12.1       mrg 		if (m == NULL) {
    596  1.19.12.1       mrg 			cv_wait(&sc->sc_cv, &sc->sc_lock);
    597  1.19.12.1       mrg 			continue;
    598  1.19.12.1       mrg 		}
    599  1.19.12.1       mrg 
    600  1.19.12.1       mrg 		mutex_exit(&sc->sc_lock);
    601  1.19.12.1       mrg 		bthidev_process_one(sc, m);
    602  1.19.12.1       mrg 		m_freem(m);
    603  1.19.12.1       mrg 		mutex_enter(&sc->sc_lock);
    604  1.19.12.1       mrg 	}
    605  1.19.12.1       mrg 	mutex_exit(&sc->sc_lock);
    606  1.19.12.1       mrg 	kthread_exit(0);
    607  1.19.12.1       mrg }
    608  1.19.12.1       mrg 
    609  1.19.12.1       mrg static void
    610  1.19.12.1       mrg bthidev_process_one(struct bthidev_softc *sc, struct mbuf *m)
    611  1.19.12.1       mrg {
    612  1.19.12.1       mrg 	struct bthidev *hidev;
    613  1.19.12.1       mrg 	uint8_t *data;
    614  1.19.12.1       mrg 	int len;
    615  1.19.12.1       mrg 
    616  1.19.12.1       mrg 	if (sc->sc_state != BTHID_OPEN)
    617  1.19.12.1       mrg 		return;
    618  1.19.12.1       mrg 
    619  1.19.12.1       mrg 	if (m->m_pkthdr.len > m->m_len)
    620  1.19.12.1       mrg 		aprint_error_dev(sc->sc_dev, "truncating HID report\n");
    621  1.19.12.1       mrg 
    622  1.19.12.1       mrg 	len = m->m_len;
    623  1.19.12.1       mrg 	data = mtod(m, uint8_t *);
    624  1.19.12.1       mrg 
    625  1.19.12.1       mrg 	switch (BTHID_TYPE(data[0])) {
    626  1.19.12.1       mrg 	case BTHID_DATA:
    627  1.19.12.1       mrg 		/*
    628  1.19.12.1       mrg 		 * data[0] == type / parameter
    629  1.19.12.1       mrg 		 * data[1] == id
    630  1.19.12.1       mrg 		 * data[2..len] == report
    631  1.19.12.1       mrg 		 */
    632  1.19.12.1       mrg 		if (len < 3)
    633  1.19.12.1       mrg 			break;
    634  1.19.12.1       mrg 
    635  1.19.12.1       mrg 		LIST_FOREACH(hidev, &sc->sc_list, sc_next)
    636  1.19.12.1       mrg 			if (data[1] == hidev->sc_id)
    637  1.19.12.1       mrg 				break;
    638  1.19.12.1       mrg 
    639  1.19.12.1       mrg 		if (hidev == NULL) {
    640  1.19.12.1       mrg 			aprint_error_dev(sc->sc_dev,
    641  1.19.12.1       mrg 			    "report id %d, len = %d ignored\n", data[1], len - 2);
    642  1.19.12.1       mrg 
    643  1.19.12.1       mrg 			break;
    644  1.19.12.1       mrg 		}
    645  1.19.12.1       mrg 
    646  1.19.12.1       mrg 		switch (BTHID_DATA_PARAM(data[0])) {
    647  1.19.12.1       mrg 		case BTHID_DATA_INPUT:
    648  1.19.12.1       mrg 			(*hidev->sc_input)(hidev, data + 2, len - 2);
    649  1.19.12.1       mrg 			break;
    650  1.19.12.1       mrg 
    651  1.19.12.1       mrg 		case BTHID_DATA_FEATURE:
    652  1.19.12.1       mrg 			(*hidev->sc_feature)(hidev, data + 2, len - 2);
    653  1.19.12.1       mrg 			break;
    654  1.19.12.1       mrg 
    655  1.19.12.1       mrg 		default:
    656  1.19.12.1       mrg 			break;
    657  1.19.12.1       mrg 		}
    658  1.19.12.1       mrg 
    659  1.19.12.1       mrg 		break;
    660  1.19.12.1       mrg 
    661  1.19.12.1       mrg 	case BTHID_CONTROL:
    662  1.19.12.1       mrg 		if (len < 1)
    663  1.19.12.1       mrg 			break;
    664  1.19.12.1       mrg 
    665  1.19.12.1       mrg 		switch (BTHID_DATA_PARAM(data[0])) {
    666  1.19.12.1       mrg 		case BTHID_CONTROL_UNPLUG:
    667  1.19.12.1       mrg 			aprint_normal_dev(sc->sc_dev, "unplugged\n");
    668  1.19.12.1       mrg 
    669  1.19.12.1       mrg 			mutex_enter(bt_lock);
    670  1.19.12.1       mrg 			/* close interrupt channel */
    671  1.19.12.1       mrg 			if (sc->sc_int != NULL) {
    672  1.19.12.1       mrg 				l2cap_disconnect(sc->sc_int, 0);
    673  1.19.12.1       mrg 				l2cap_detach(&sc->sc_int);
    674  1.19.12.1       mrg 				sc->sc_int = NULL;
    675  1.19.12.1       mrg 			}
    676  1.19.12.1       mrg 
    677  1.19.12.1       mrg 			/* close control channel */
    678  1.19.12.1       mrg 			if (sc->sc_ctl != NULL) {
    679  1.19.12.1       mrg 				l2cap_disconnect(sc->sc_ctl, 0);
    680  1.19.12.1       mrg 				l2cap_detach(&sc->sc_ctl);
    681  1.19.12.1       mrg 				sc->sc_ctl = NULL;
    682  1.19.12.1       mrg 			}
    683  1.19.12.1       mrg 			mutex_exit(bt_lock);
    684  1.19.12.1       mrg 
    685  1.19.12.1       mrg 			break;
    686  1.19.12.1       mrg 
    687  1.19.12.1       mrg 		default:
    688  1.19.12.1       mrg 			break;
    689  1.19.12.1       mrg 		}
    690  1.19.12.1       mrg 
    691  1.19.12.1       mrg 		break;
    692  1.19.12.1       mrg 
    693  1.19.12.1       mrg 	default:
    694  1.19.12.1       mrg 		break;
    695  1.19.12.1       mrg 	}
    696  1.19.12.1       mrg }
    697  1.19.12.1       mrg 
    698        1.1   gdamore /*****************************************************************************
    699        1.1   gdamore  *
    700        1.1   gdamore  *	bluetooth(9) callback methods for L2CAP
    701        1.1   gdamore  *
    702        1.1   gdamore  *	All these are called from Bluetooth Protocol code, in a soft
    703        1.1   gdamore  *	interrupt context at IPL_SOFTNET.
    704        1.1   gdamore  */
    705        1.1   gdamore 
    706        1.1   gdamore static void
    707        1.7  christos bthidev_connecting(void *arg)
    708        1.1   gdamore {
    709        1.1   gdamore 
    710        1.1   gdamore 	/* dont care */
    711        1.1   gdamore }
    712        1.1   gdamore 
    713        1.1   gdamore static void
    714        1.1   gdamore bthidev_ctl_connected(void *arg)
    715        1.1   gdamore {
    716        1.1   gdamore 	struct sockaddr_bt sa;
    717        1.1   gdamore 	struct bthidev_softc *sc = arg;
    718        1.1   gdamore 	int err;
    719        1.1   gdamore 
    720        1.1   gdamore 	if (sc->sc_state != BTHID_WAIT_CTL)
    721        1.1   gdamore 		return;
    722        1.1   gdamore 
    723        1.1   gdamore 	KASSERT(sc->sc_ctl != NULL);
    724        1.1   gdamore 	KASSERT(sc->sc_int == NULL);
    725        1.1   gdamore 
    726        1.2      tron 	if (sc->sc_flags & BTHID_CONNECTING) {
    727        1.1   gdamore 		/* initiate connect on interrupt PSM */
    728        1.1   gdamore 		err = l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
    729        1.1   gdamore 		if (err)
    730        1.1   gdamore 			goto fail;
    731        1.1   gdamore 
    732       1.16    plunky 		err = l2cap_setopt(sc->sc_int, &sc->sc_mode);
    733        1.8    plunky 		if (err)
    734        1.8    plunky 			goto fail;
    735        1.8    plunky 
    736        1.1   gdamore 		memset(&sa, 0, sizeof(sa));
    737        1.1   gdamore 		sa.bt_len = sizeof(sa);
    738        1.1   gdamore 		sa.bt_family = AF_BLUETOOTH;
    739        1.1   gdamore 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    740        1.1   gdamore 
    741        1.1   gdamore 		err = l2cap_bind(sc->sc_int, &sa);
    742        1.1   gdamore 		if (err)
    743        1.1   gdamore 			goto fail;
    744        1.1   gdamore 
    745        1.1   gdamore 		sa.bt_psm = sc->sc_intpsm;
    746        1.1   gdamore 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    747        1.1   gdamore 		err = l2cap_connect(sc->sc_int, &sa);
    748        1.1   gdamore 		if (err)
    749        1.1   gdamore 			goto fail;
    750        1.1   gdamore 	}
    751        1.1   gdamore 
    752        1.1   gdamore 	sc->sc_state = BTHID_WAIT_INT;
    753        1.1   gdamore 	return;
    754        1.1   gdamore 
    755        1.1   gdamore fail:
    756        1.1   gdamore 	l2cap_detach(&sc->sc_ctl);
    757        1.1   gdamore 	sc->sc_ctl = NULL;
    758        1.3    plunky 
    759       1.12    plunky 	aprint_error_dev(sc->sc_dev, "connect failed (%d)\n", err);
    760        1.1   gdamore }
    761        1.1   gdamore 
    762        1.1   gdamore static void
    763        1.1   gdamore bthidev_int_connected(void *arg)
    764        1.1   gdamore {
    765        1.1   gdamore 	struct bthidev_softc *sc = arg;
    766        1.1   gdamore 
    767        1.1   gdamore 	if (sc->sc_state != BTHID_WAIT_INT)
    768        1.1   gdamore 		return;
    769        1.1   gdamore 
    770        1.1   gdamore 	KASSERT(sc->sc_ctl != NULL);
    771        1.1   gdamore 	KASSERT(sc->sc_int != NULL);
    772        1.1   gdamore 
    773        1.1   gdamore 	sc->sc_attempts = 0;
    774        1.2      tron 	sc->sc_flags &= ~BTHID_CONNECTING;
    775        1.1   gdamore 	sc->sc_state = BTHID_OPEN;
    776        1.1   gdamore 
    777       1.13    plunky 	aprint_normal_dev(sc->sc_dev, "connected\n");
    778        1.1   gdamore }
    779        1.1   gdamore 
    780        1.1   gdamore /*
    781        1.1   gdamore  * Disconnected
    782        1.1   gdamore  *
    783        1.1   gdamore  * Depending on our state, this could mean several things, but essentially
    784        1.2      tron  * we are lost. If both channels are closed, and we are marked to reconnect,
    785        1.2      tron  * schedule another try otherwise just give up. They will contact us.
    786        1.1   gdamore  */
    787        1.1   gdamore static void
    788        1.7  christos bthidev_ctl_disconnected(void *arg, int err)
    789        1.1   gdamore {
    790        1.1   gdamore 	struct bthidev_softc *sc = arg;
    791        1.1   gdamore 
    792        1.1   gdamore 	if (sc->sc_ctl != NULL) {
    793        1.1   gdamore 		l2cap_detach(&sc->sc_ctl);
    794        1.1   gdamore 		sc->sc_ctl = NULL;
    795        1.1   gdamore 	}
    796        1.1   gdamore 
    797        1.1   gdamore 	sc->sc_state = BTHID_CLOSED;
    798        1.1   gdamore 
    799        1.1   gdamore 	if (sc->sc_int == NULL) {
    800       1.13    plunky 		aprint_normal_dev(sc->sc_dev, "disconnected\n");
    801        1.2      tron 		sc->sc_flags &= ~BTHID_CONNECTING;
    802        1.1   gdamore 
    803        1.2      tron 		if (sc->sc_flags & BTHID_RECONNECT)
    804        1.1   gdamore 			callout_schedule(&sc->sc_reconnect,
    805        1.1   gdamore 					BTHID_RETRY_INTERVAL * hz);
    806        1.1   gdamore 		else
    807        1.1   gdamore 			sc->sc_state = BTHID_WAIT_CTL;
    808        1.2      tron 	} else {
    809        1.2      tron 		/*
    810        1.2      tron 		 * The interrupt channel should have been closed first,
    811        1.4    plunky 		 * but its potentially unsafe to detach that from here.
    812        1.4    plunky 		 * Give them a second to do the right thing or let the
    813        1.4    plunky 		 * callout handle it.
    814        1.2      tron 		 */
    815        1.4    plunky 		callout_schedule(&sc->sc_reconnect, hz);
    816        1.1   gdamore 	}
    817        1.1   gdamore }
    818        1.1   gdamore 
    819        1.1   gdamore static void
    820        1.7  christos bthidev_int_disconnected(void *arg, int err)
    821        1.1   gdamore {
    822        1.1   gdamore 	struct bthidev_softc *sc = arg;
    823        1.1   gdamore 
    824        1.1   gdamore 	if (sc->sc_int != NULL) {
    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 	sc->sc_state = BTHID_CLOSED;
    830        1.1   gdamore 
    831        1.1   gdamore 	if (sc->sc_ctl == NULL) {
    832       1.13    plunky 		aprint_normal_dev(sc->sc_dev, "disconnected\n");
    833        1.2      tron 		sc->sc_flags &= ~BTHID_CONNECTING;
    834        1.1   gdamore 
    835        1.2      tron 		if (sc->sc_flags & BTHID_RECONNECT)
    836        1.1   gdamore 			callout_schedule(&sc->sc_reconnect,
    837        1.1   gdamore 					BTHID_RETRY_INTERVAL * hz);
    838        1.1   gdamore 		else
    839        1.1   gdamore 			sc->sc_state = BTHID_WAIT_CTL;
    840        1.4    plunky 	} else {
    841        1.4    plunky 		/*
    842        1.4    plunky 		 * The control channel should be closing also, allow
    843        1.4    plunky 		 * them a chance to do that before we force it.
    844        1.4    plunky 		 */
    845        1.4    plunky 		callout_schedule(&sc->sc_reconnect, hz);
    846        1.1   gdamore 	}
    847        1.1   gdamore }
    848        1.1   gdamore 
    849        1.1   gdamore /*
    850        1.1   gdamore  * New Connections
    851        1.1   gdamore  *
    852        1.1   gdamore  * We give a new L2CAP handle back if this matches the BDADDR we are
    853        1.1   gdamore  * listening for and we are in the right state. bthidev_connected will
    854        1.1   gdamore  * be called when the connection is open, so nothing else to do here
    855        1.1   gdamore  */
    856        1.1   gdamore static void *
    857        1.7  christos bthidev_ctl_newconn(void *arg, struct sockaddr_bt *laddr,
    858        1.6  christos     struct sockaddr_bt *raddr)
    859        1.1   gdamore {
    860        1.1   gdamore 	struct bthidev_softc *sc = arg;
    861        1.1   gdamore 
    862       1.18    plunky 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
    863       1.18    plunky 		return NULL;
    864       1.18    plunky 
    865       1.18    plunky 	if ((sc->sc_flags & BTHID_CONNECTING)
    866        1.1   gdamore 	    || sc->sc_state != BTHID_WAIT_CTL
    867        1.1   gdamore 	    || sc->sc_ctl != NULL
    868       1.18    plunky 	    || sc->sc_int != NULL) {
    869       1.18    plunky 		aprint_verbose_dev(sc->sc_dev, "reject ctl newconn %s%s%s%s\n",
    870       1.18    plunky 		    (sc->sc_flags & BTHID_CONNECTING) ? " (CONNECTING)" : "",
    871       1.18    plunky 		    (sc->sc_state == BTHID_WAIT_CTL) ? " (WAITING)": "",
    872       1.18    plunky 		    (sc->sc_ctl != NULL) ? " (GOT CONTROL)" : "",
    873       1.18    plunky 		    (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
    874       1.18    plunky 
    875        1.1   gdamore 		return NULL;
    876       1.18    plunky 	}
    877        1.1   gdamore 
    878        1.1   gdamore 	l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
    879        1.1   gdamore 	return sc->sc_ctl;
    880        1.1   gdamore }
    881        1.1   gdamore 
    882        1.1   gdamore static void *
    883        1.7  christos bthidev_int_newconn(void *arg, struct sockaddr_bt *laddr,
    884        1.6  christos     struct sockaddr_bt *raddr)
    885        1.1   gdamore {
    886        1.1   gdamore 	struct bthidev_softc *sc = arg;
    887        1.1   gdamore 
    888       1.18    plunky 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
    889       1.18    plunky 		return NULL;
    890       1.18    plunky 
    891       1.18    plunky 	if ((sc->sc_flags & BTHID_CONNECTING)
    892        1.1   gdamore 	    || sc->sc_state != BTHID_WAIT_INT
    893        1.1   gdamore 	    || sc->sc_ctl == NULL
    894       1.18    plunky 	    || sc->sc_int != NULL) {
    895       1.18    plunky 		aprint_verbose_dev(sc->sc_dev, "reject int newconn %s%s%s%s\n",
    896       1.18    plunky 		    (sc->sc_flags & BTHID_CONNECTING) ? " (CONNECTING)" : "",
    897       1.18    plunky 		    (sc->sc_state == BTHID_WAIT_INT) ? " (WAITING)": "",
    898       1.18    plunky 		    (sc->sc_ctl == NULL) ? " (NO CONTROL)" : "",
    899       1.18    plunky 		    (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
    900       1.18    plunky 
    901        1.1   gdamore 		return NULL;
    902       1.18    plunky 	}
    903        1.1   gdamore 
    904        1.1   gdamore 	l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
    905        1.1   gdamore 	return sc->sc_int;
    906        1.1   gdamore }
    907        1.1   gdamore 
    908        1.1   gdamore static void
    909        1.7  christos bthidev_complete(void *arg, int count)
    910        1.1   gdamore {
    911        1.1   gdamore 
    912        1.1   gdamore 	/* dont care */
    913        1.1   gdamore }
    914        1.1   gdamore 
    915        1.8    plunky static void
    916        1.8    plunky bthidev_linkmode(void *arg, int new)
    917        1.8    plunky {
    918        1.8    plunky 	struct bthidev_softc *sc = arg;
    919       1.16    plunky 	int mode;
    920       1.16    plunky 
    921       1.16    plunky 	(void)sockopt_getint(&sc->sc_mode, &mode);
    922        1.8    plunky 
    923       1.16    plunky 	if ((mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
    924       1.12    plunky 		aprint_error_dev(sc->sc_dev, "auth failed\n");
    925       1.16    plunky 	else if ((mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
    926       1.12    plunky 		aprint_error_dev(sc->sc_dev, "encrypt off\n");
    927       1.16    plunky 	else if ((mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE))
    928       1.12    plunky 		aprint_error_dev(sc->sc_dev, "insecure\n");
    929        1.8    plunky 	else
    930        1.8    plunky 		return;
    931        1.8    plunky 
    932        1.8    plunky 	if (sc->sc_int != NULL)
    933        1.8    plunky 		l2cap_disconnect(sc->sc_int, 0);
    934        1.8    plunky 
    935        1.8    plunky 	if (sc->sc_ctl != NULL)
    936        1.8    plunky 		l2cap_disconnect(sc->sc_ctl, 0);
    937        1.8    plunky }
    938        1.8    plunky 
    939        1.1   gdamore /*
    940  1.19.12.1       mrg  * Receive reports from the protocol stack. Because this will be called
    941  1.19.12.1       mrg  * with bt_lock held, we queue the mbuf and process it with a kernel thread
    942        1.1   gdamore  */
    943        1.1   gdamore static void
    944        1.1   gdamore bthidev_input(void *arg, struct mbuf *m)
    945        1.1   gdamore {
    946        1.1   gdamore 	struct bthidev_softc *sc = arg;
    947        1.1   gdamore 
    948  1.19.12.1       mrg 	if (sc->sc_state != BTHID_OPEN) {
    949  1.19.12.1       mrg 		m_freem(m);
    950  1.19.12.1       mrg 		return;
    951        1.1   gdamore 	}
    952        1.1   gdamore 
    953  1.19.12.1       mrg 	mutex_enter(&sc->sc_lock);
    954  1.19.12.1       mrg 	MBUFQ_ENQUEUE(&sc->sc_inq, m);
    955  1.19.12.1       mrg 	cv_signal(&sc->sc_cv);
    956  1.19.12.1       mrg 	mutex_exit(&sc->sc_lock);
    957        1.1   gdamore }
    958        1.1   gdamore 
    959        1.1   gdamore /*****************************************************************************
    960        1.1   gdamore  *
    961        1.1   gdamore  *	IO routines
    962        1.1   gdamore  */
    963        1.1   gdamore 
    964        1.1   gdamore static void
    965       1.10    plunky bthidev_null(struct bthidev *hidev, uint8_t *report, int len)
    966        1.1   gdamore {
    967        1.1   gdamore 
    968        1.1   gdamore 	/*
    969        1.1   gdamore 	 * empty routine just in case the device
    970        1.1   gdamore 	 * provided no method to handle this report
    971        1.1   gdamore 	 */
    972        1.1   gdamore }
    973        1.1   gdamore 
    974        1.1   gdamore static int
    975       1.10    plunky bthidev_output(struct bthidev *hidev, uint8_t *report, int rlen)
    976        1.1   gdamore {
    977       1.10    plunky 	struct bthidev_softc *sc = device_private(hidev->sc_parent);
    978        1.1   gdamore 	struct mbuf *m;
    979       1.15        ad 	int err;
    980        1.1   gdamore 
    981        1.1   gdamore 	if (sc == NULL || sc->sc_state != BTHID_OPEN)
    982        1.1   gdamore 		return ENOTCONN;
    983        1.1   gdamore 
    984        1.1   gdamore 	KASSERT(sc->sc_ctl != NULL);
    985        1.1   gdamore 	KASSERT(sc->sc_int != NULL);
    986        1.1   gdamore 
    987        1.1   gdamore 	if (rlen == 0 || report == NULL)
    988        1.1   gdamore 		return 0;
    989        1.1   gdamore 
    990        1.1   gdamore 	if (rlen > MHLEN - 2) {
    991       1.12    plunky 		aprint_error_dev(sc->sc_dev,
    992       1.12    plunky 		    "output report too long (%d)!\n", rlen);
    993        1.1   gdamore 		return EMSGSIZE;
    994        1.1   gdamore 	}
    995        1.1   gdamore 
    996        1.1   gdamore 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    997        1.1   gdamore 	if (m == NULL)
    998        1.1   gdamore 		return ENOMEM;
    999        1.1   gdamore 
   1000        1.1   gdamore 	/*
   1001        1.1   gdamore 	 * data[0] = type / parameter
   1002        1.1   gdamore 	 * data[1] = id
   1003        1.1   gdamore 	 * data[2..N] = report
   1004        1.1   gdamore 	 */
   1005        1.1   gdamore 	mtod(m, uint8_t *)[0] = (uint8_t)((BTHID_DATA << 4) | BTHID_DATA_OUTPUT);
   1006       1.10    plunky 	mtod(m, uint8_t *)[1] = hidev->sc_id;
   1007        1.1   gdamore 	memcpy(mtod(m, uint8_t *) + 2, report, rlen);
   1008        1.1   gdamore 	m->m_pkthdr.len = m->m_len = rlen + 2;
   1009        1.1   gdamore 
   1010       1.15        ad 	mutex_enter(bt_lock);
   1011        1.1   gdamore 	err = l2cap_send(sc->sc_int, m);
   1012       1.15        ad 	mutex_exit(bt_lock);
   1013        1.1   gdamore 
   1014        1.1   gdamore 	return err;
   1015        1.1   gdamore }
   1016