Home | History | Annotate | Line # | Download | only in netbt
hci_link.c revision 1.1.2.6
      1  1.1.2.6  yamt /*	$NetBSD: hci_link.c,v 1.1.2.6 2007/11/15 11:45:04 yamt Exp $	*/
      2  1.1.2.2  yamt 
      3  1.1.2.2  yamt /*-
      4  1.1.2.2  yamt  * Copyright (c) 2005 Iain Hibbert.
      5  1.1.2.2  yamt  * Copyright (c) 2006 Itronix Inc.
      6  1.1.2.2  yamt  * All rights reserved.
      7  1.1.2.2  yamt  *
      8  1.1.2.2  yamt  * Redistribution and use in source and binary forms, with or without
      9  1.1.2.2  yamt  * modification, are permitted provided that the following conditions
     10  1.1.2.2  yamt  * are met:
     11  1.1.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     12  1.1.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     13  1.1.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     15  1.1.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     16  1.1.2.2  yamt  * 3. The name of Itronix Inc. may not be used to endorse
     17  1.1.2.2  yamt  *    or promote products derived from this software without specific
     18  1.1.2.2  yamt  *    prior written permission.
     19  1.1.2.2  yamt  *
     20  1.1.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     21  1.1.2.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1.2.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1.2.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     24  1.1.2.2  yamt  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     25  1.1.2.2  yamt  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     26  1.1.2.2  yamt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     27  1.1.2.2  yamt  * ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1.2.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1.2.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1.2.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1.2.2  yamt  */
     32  1.1.2.2  yamt 
     33  1.1.2.2  yamt #include <sys/cdefs.h>
     34  1.1.2.6  yamt __KERNEL_RCSID(0, "$NetBSD: hci_link.c,v 1.1.2.6 2007/11/15 11:45:04 yamt Exp $");
     35  1.1.2.2  yamt 
     36  1.1.2.2  yamt #include <sys/param.h>
     37  1.1.2.2  yamt #include <sys/kernel.h>
     38  1.1.2.2  yamt #include <sys/malloc.h>
     39  1.1.2.2  yamt #include <sys/mbuf.h>
     40  1.1.2.2  yamt #include <sys/proc.h>
     41  1.1.2.2  yamt #include <sys/queue.h>
     42  1.1.2.2  yamt #include <sys/systm.h>
     43  1.1.2.2  yamt 
     44  1.1.2.2  yamt #include <netbt/bluetooth.h>
     45  1.1.2.2  yamt #include <netbt/hci.h>
     46  1.1.2.2  yamt #include <netbt/l2cap.h>
     47  1.1.2.3  yamt #include <netbt/sco.h>
     48  1.1.2.2  yamt 
     49  1.1.2.2  yamt /*******************************************************************************
     50  1.1.2.2  yamt  *
     51  1.1.2.2  yamt  *	HCI ACL Connections
     52  1.1.2.2  yamt  */
     53  1.1.2.2  yamt 
     54  1.1.2.2  yamt /*
     55  1.1.2.2  yamt  * Automatically expire unused ACL connections after this number of
     56  1.1.2.2  yamt  * seconds (if zero, do not expire unused connections) [sysctl]
     57  1.1.2.2  yamt  */
     58  1.1.2.2  yamt int hci_acl_expiry = 10;	/* seconds */
     59  1.1.2.2  yamt 
     60  1.1.2.2  yamt /*
     61  1.1.2.2  yamt  * hci_acl_open(unit, bdaddr)
     62  1.1.2.2  yamt  *
     63  1.1.2.2  yamt  * open ACL connection to remote bdaddr. Only one ACL connection is permitted
     64  1.1.2.2  yamt  * between any two Bluetooth devices, so we look for an existing one before
     65  1.1.2.2  yamt  * trying to start a new one.
     66  1.1.2.2  yamt  */
     67  1.1.2.2  yamt struct hci_link *
     68  1.1.2.2  yamt hci_acl_open(struct hci_unit *unit, bdaddr_t *bdaddr)
     69  1.1.2.2  yamt {
     70  1.1.2.2  yamt 	struct hci_link *link;
     71  1.1.2.3  yamt 	struct hci_memo *memo;
     72  1.1.2.2  yamt 	hci_create_con_cp cp;
     73  1.1.2.2  yamt 	int err;
     74  1.1.2.2  yamt 
     75  1.1.2.4  yamt 	KASSERT(unit != NULL);
     76  1.1.2.4  yamt 	KASSERT(bdaddr != NULL);
     77  1.1.2.2  yamt 
     78  1.1.2.2  yamt 	link = hci_link_lookup_bdaddr(unit, bdaddr, HCI_LINK_ACL);
     79  1.1.2.2  yamt 	if (link == NULL) {
     80  1.1.2.2  yamt 		link = hci_link_alloc(unit);
     81  1.1.2.2  yamt 		if (link == NULL)
     82  1.1.2.2  yamt 			return NULL;
     83  1.1.2.2  yamt 
     84  1.1.2.2  yamt 		link->hl_type = HCI_LINK_ACL;
     85  1.1.2.2  yamt 		bdaddr_copy(&link->hl_bdaddr, bdaddr);
     86  1.1.2.2  yamt 	}
     87  1.1.2.2  yamt 
     88  1.1.2.2  yamt 	switch(link->hl_state) {
     89  1.1.2.2  yamt 	case HCI_LINK_CLOSED:
     90  1.1.2.2  yamt 		/*
     91  1.1.2.2  yamt 		 * open connection to remote device
     92  1.1.2.2  yamt 		 */
     93  1.1.2.2  yamt 		memset(&cp, 0, sizeof(cp));
     94  1.1.2.2  yamt 		bdaddr_copy(&cp.bdaddr, bdaddr);
     95  1.1.2.2  yamt 		cp.pkt_type = htole16(unit->hci_packet_type);
     96  1.1.2.3  yamt 
     97  1.1.2.3  yamt 		memo = hci_memo_find(unit, bdaddr);
     98  1.1.2.3  yamt 		if (memo != NULL) {
     99  1.1.2.5  yamt 			cp.page_scan_rep_mode = memo->page_scan_rep_mode;
    100  1.1.2.5  yamt 			cp.page_scan_mode = memo->page_scan_mode;
    101  1.1.2.5  yamt 			cp.clock_offset = memo->clock_offset;
    102  1.1.2.3  yamt 		}
    103  1.1.2.3  yamt 
    104  1.1.2.2  yamt 		if (unit->hci_link_policy & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH)
    105  1.1.2.2  yamt 			cp.accept_role_switch = 1;
    106  1.1.2.2  yamt 
    107  1.1.2.2  yamt 		err = hci_send_cmd(unit, HCI_CMD_CREATE_CON, &cp, sizeof(cp));
    108  1.1.2.2  yamt 		if (err) {
    109  1.1.2.2  yamt 			hci_link_free(link, err);
    110  1.1.2.2  yamt 			return NULL;
    111  1.1.2.2  yamt 		}
    112  1.1.2.2  yamt 
    113  1.1.2.2  yamt 		link->hl_state = HCI_LINK_WAIT_CONNECT;
    114  1.1.2.2  yamt 		break;
    115  1.1.2.2  yamt 
    116  1.1.2.2  yamt 	case HCI_LINK_WAIT_CONNECT:
    117  1.1.2.4  yamt 	case HCI_LINK_WAIT_AUTH:
    118  1.1.2.4  yamt 	case HCI_LINK_WAIT_ENCRYPT:
    119  1.1.2.4  yamt 	case HCI_LINK_WAIT_SECURE:
    120  1.1.2.2  yamt 		/*
    121  1.1.2.2  yamt 		 * somebody else already trying to connect, we just
    122  1.1.2.2  yamt 		 * sit on the bench with them..
    123  1.1.2.2  yamt 		 */
    124  1.1.2.2  yamt 		break;
    125  1.1.2.2  yamt 
    126  1.1.2.2  yamt 	case HCI_LINK_OPEN:
    127  1.1.2.2  yamt 		/*
    128  1.1.2.2  yamt 		 * If already open, halt any expiry timeouts. We dont need
    129  1.1.2.2  yamt 		 * to care about already invoking timeouts since refcnt >0
    130  1.1.2.2  yamt 		 * will keep the link alive.
    131  1.1.2.2  yamt 		 */
    132  1.1.2.2  yamt 		callout_stop(&link->hl_expire);
    133  1.1.2.2  yamt 		break;
    134  1.1.2.2  yamt 
    135  1.1.2.2  yamt 	default:
    136  1.1.2.2  yamt 		UNKNOWN(link->hl_state);
    137  1.1.2.2  yamt 		return NULL;
    138  1.1.2.2  yamt 	}
    139  1.1.2.2  yamt 
    140  1.1.2.2  yamt 	/* open */
    141  1.1.2.2  yamt 	link->hl_refcnt++;
    142  1.1.2.2  yamt 
    143  1.1.2.2  yamt 	return link;
    144  1.1.2.2  yamt }
    145  1.1.2.2  yamt 
    146  1.1.2.2  yamt /*
    147  1.1.2.2  yamt  * Close ACL connection. When there are no more references to this link,
    148  1.1.2.2  yamt  * we can either close it down or schedule a delayed closedown.
    149  1.1.2.2  yamt  */
    150  1.1.2.2  yamt void
    151  1.1.2.2  yamt hci_acl_close(struct hci_link *link, int err)
    152  1.1.2.2  yamt {
    153  1.1.2.2  yamt 
    154  1.1.2.4  yamt 	KASSERT(link != NULL);
    155  1.1.2.2  yamt 
    156  1.1.2.2  yamt 	if (--link->hl_refcnt == 0) {
    157  1.1.2.2  yamt 		if (link->hl_state == HCI_LINK_CLOSED)
    158  1.1.2.2  yamt 			hci_link_free(link, err);
    159  1.1.2.2  yamt 		else if (hci_acl_expiry > 0)
    160  1.1.2.2  yamt 			callout_schedule(&link->hl_expire, hci_acl_expiry * hz);
    161  1.1.2.2  yamt 	}
    162  1.1.2.2  yamt }
    163  1.1.2.2  yamt 
    164  1.1.2.2  yamt /*
    165  1.1.2.3  yamt  * Incoming ACL connection.
    166  1.1.2.3  yamt  *
    167  1.1.2.3  yamt  * For now, we accept all connections but it would be better to check
    168  1.1.2.3  yamt  * the L2CAP listen list and only accept when there is a listener
    169  1.1.2.3  yamt  * available.
    170  1.1.2.3  yamt  *
    171  1.1.2.3  yamt  * There should not be a link to the same bdaddr already, we check
    172  1.1.2.3  yamt  * anyway though its left unhandled for now.
    173  1.1.2.2  yamt  */
    174  1.1.2.2  yamt struct hci_link *
    175  1.1.2.2  yamt hci_acl_newconn(struct hci_unit *unit, bdaddr_t *bdaddr)
    176  1.1.2.2  yamt {
    177  1.1.2.2  yamt 	struct hci_link *link;
    178  1.1.2.2  yamt 
    179  1.1.2.3  yamt 	link = hci_link_lookup_bdaddr(unit, bdaddr, HCI_LINK_ACL);
    180  1.1.2.3  yamt 	if (link != NULL)
    181  1.1.2.3  yamt 		return NULL;
    182  1.1.2.3  yamt 
    183  1.1.2.2  yamt 	link = hci_link_alloc(unit);
    184  1.1.2.2  yamt 	if (link != NULL) {
    185  1.1.2.2  yamt 		link->hl_state = HCI_LINK_WAIT_CONNECT;
    186  1.1.2.2  yamt 		link->hl_type = HCI_LINK_ACL;
    187  1.1.2.2  yamt 		bdaddr_copy(&link->hl_bdaddr, bdaddr);
    188  1.1.2.2  yamt 
    189  1.1.2.2  yamt 		if (hci_acl_expiry > 0)
    190  1.1.2.2  yamt 			callout_schedule(&link->hl_expire, hci_acl_expiry * hz);
    191  1.1.2.2  yamt 	}
    192  1.1.2.2  yamt 
    193  1.1.2.2  yamt 	return link;
    194  1.1.2.2  yamt }
    195  1.1.2.2  yamt 
    196  1.1.2.2  yamt void
    197  1.1.2.2  yamt hci_acl_timeout(void *arg)
    198  1.1.2.2  yamt {
    199  1.1.2.2  yamt 	struct hci_link *link = arg;
    200  1.1.2.2  yamt 	hci_discon_cp cp;
    201  1.1.2.2  yamt 	int s, err;
    202  1.1.2.2  yamt 
    203  1.1.2.2  yamt 	s = splsoftnet();
    204  1.1.2.2  yamt 	callout_ack(&link->hl_expire);
    205  1.1.2.2  yamt 
    206  1.1.2.2  yamt 	if (link->hl_refcnt > 0)
    207  1.1.2.2  yamt 		goto out;
    208  1.1.2.2  yamt 
    209  1.1.2.2  yamt 	DPRINTF("link #%d expired\n", link->hl_handle);
    210  1.1.2.2  yamt 
    211  1.1.2.2  yamt 	switch (link->hl_state) {
    212  1.1.2.2  yamt 	case HCI_LINK_CLOSED:
    213  1.1.2.2  yamt 	case HCI_LINK_WAIT_CONNECT:
    214  1.1.2.2  yamt 		hci_link_free(link, ECONNRESET);
    215  1.1.2.2  yamt 		break;
    216  1.1.2.2  yamt 
    217  1.1.2.4  yamt 	case HCI_LINK_WAIT_AUTH:
    218  1.1.2.4  yamt 	case HCI_LINK_WAIT_ENCRYPT:
    219  1.1.2.4  yamt 	case HCI_LINK_WAIT_SECURE:
    220  1.1.2.2  yamt 	case HCI_LINK_OPEN:
    221  1.1.2.2  yamt 		cp.con_handle = htole16(link->hl_handle);
    222  1.1.2.2  yamt 		cp.reason = 0x13; /* "Remote User Terminated Connection" */
    223  1.1.2.2  yamt 
    224  1.1.2.2  yamt 		err = hci_send_cmd(link->hl_unit, HCI_CMD_DISCONNECT,
    225  1.1.2.2  yamt 					&cp, sizeof(cp));
    226  1.1.2.2  yamt 
    227  1.1.2.3  yamt 		if (err) {
    228  1.1.2.2  yamt 			DPRINTF("error %d sending HCI_CMD_DISCONNECT\n",
    229  1.1.2.3  yamt 			    err);
    230  1.1.2.3  yamt 		}
    231  1.1.2.2  yamt 
    232  1.1.2.2  yamt 		break;
    233  1.1.2.2  yamt 
    234  1.1.2.2  yamt 	default:
    235  1.1.2.2  yamt 		UNKNOWN(link->hl_state);
    236  1.1.2.2  yamt 		break;
    237  1.1.2.2  yamt 	}
    238  1.1.2.2  yamt 
    239  1.1.2.2  yamt out:
    240  1.1.2.2  yamt 	splx(s);
    241  1.1.2.2  yamt }
    242  1.1.2.2  yamt 
    243  1.1.2.2  yamt /*
    244  1.1.2.4  yamt  * Initiate any Link Mode change requests.
    245  1.1.2.4  yamt  */
    246  1.1.2.4  yamt int
    247  1.1.2.4  yamt hci_acl_setmode(struct hci_link *link)
    248  1.1.2.4  yamt {
    249  1.1.2.4  yamt 	int err;
    250  1.1.2.4  yamt 
    251  1.1.2.4  yamt 	KASSERT(link != NULL);
    252  1.1.2.4  yamt 	KASSERT(link->hl_unit != NULL);
    253  1.1.2.4  yamt 
    254  1.1.2.4  yamt 	if (link->hl_state != HCI_LINK_OPEN)
    255  1.1.2.4  yamt 		return EINPROGRESS;
    256  1.1.2.4  yamt 
    257  1.1.2.4  yamt 	if ((link->hl_flags & HCI_LINK_AUTH_REQ)
    258  1.1.2.4  yamt 	    && !(link->hl_flags & HCI_LINK_AUTH)) {
    259  1.1.2.4  yamt 		hci_auth_req_cp cp;
    260  1.1.2.4  yamt 
    261  1.1.2.4  yamt 		DPRINTF("requesting auth for handle #%d\n",
    262  1.1.2.4  yamt 			link->hl_handle);
    263  1.1.2.4  yamt 
    264  1.1.2.4  yamt 		link->hl_state = HCI_LINK_WAIT_AUTH;
    265  1.1.2.4  yamt 		cp.con_handle = htole16(link->hl_handle);
    266  1.1.2.4  yamt 		err = hci_send_cmd(link->hl_unit, HCI_CMD_AUTH_REQ,
    267  1.1.2.4  yamt 				   &cp, sizeof(cp));
    268  1.1.2.4  yamt 
    269  1.1.2.4  yamt 		return (err == 0 ? EINPROGRESS : err);
    270  1.1.2.4  yamt 	}
    271  1.1.2.4  yamt 
    272  1.1.2.4  yamt 	if ((link->hl_flags & HCI_LINK_ENCRYPT_REQ)
    273  1.1.2.4  yamt 	    && !(link->hl_flags & HCI_LINK_ENCRYPT)) {
    274  1.1.2.4  yamt 		hci_set_con_encryption_cp cp;
    275  1.1.2.4  yamt 
    276  1.1.2.4  yamt 		/* XXX we should check features for encryption capability */
    277  1.1.2.4  yamt 
    278  1.1.2.4  yamt 		DPRINTF("requesting encryption for handle #%d\n",
    279  1.1.2.4  yamt 			link->hl_handle);
    280  1.1.2.4  yamt 
    281  1.1.2.4  yamt 		link->hl_state = HCI_LINK_WAIT_ENCRYPT;
    282  1.1.2.4  yamt 		cp.con_handle = htole16(link->hl_handle);
    283  1.1.2.4  yamt 		cp.encryption_enable = 0x01;
    284  1.1.2.4  yamt 
    285  1.1.2.4  yamt 		err = hci_send_cmd(link->hl_unit, HCI_CMD_SET_CON_ENCRYPTION,
    286  1.1.2.4  yamt 				   &cp, sizeof(cp));
    287  1.1.2.4  yamt 
    288  1.1.2.4  yamt 		return (err == 0 ? EINPROGRESS : err);
    289  1.1.2.4  yamt 	}
    290  1.1.2.4  yamt 
    291  1.1.2.4  yamt 	if ((link->hl_flags & HCI_LINK_SECURE_REQ)) {
    292  1.1.2.4  yamt 		hci_change_con_link_key_cp cp;
    293  1.1.2.4  yamt 
    294  1.1.2.4  yamt 		/* always change link key for SECURE requests */
    295  1.1.2.4  yamt 		link->hl_flags &= ~HCI_LINK_SECURE;
    296  1.1.2.4  yamt 
    297  1.1.2.4  yamt 		DPRINTF("changing link key for handle #%d\n",
    298  1.1.2.4  yamt 			link->hl_handle);
    299  1.1.2.4  yamt 
    300  1.1.2.4  yamt 		link->hl_state = HCI_LINK_WAIT_SECURE;
    301  1.1.2.4  yamt 		cp.con_handle = htole16(link->hl_handle);
    302  1.1.2.4  yamt 
    303  1.1.2.4  yamt 		err = hci_send_cmd(link->hl_unit, HCI_CMD_CHANGE_CON_LINK_KEY,
    304  1.1.2.4  yamt 				   &cp, sizeof(cp));
    305  1.1.2.4  yamt 
    306  1.1.2.4  yamt 		return (err == 0 ? EINPROGRESS : err);
    307  1.1.2.4  yamt 	}
    308  1.1.2.4  yamt 
    309  1.1.2.4  yamt 	return 0;
    310  1.1.2.4  yamt }
    311  1.1.2.4  yamt 
    312  1.1.2.4  yamt /*
    313  1.1.2.4  yamt  * Link Mode changed.
    314  1.1.2.4  yamt  *
    315  1.1.2.4  yamt  * This is called from event handlers when the mode change
    316  1.1.2.4  yamt  * is complete. We notify upstream and restart the link.
    317  1.1.2.4  yamt  */
    318  1.1.2.4  yamt void
    319  1.1.2.4  yamt hci_acl_linkmode(struct hci_link *link)
    320  1.1.2.4  yamt {
    321  1.1.2.4  yamt 	struct l2cap_channel *chan, *next;
    322  1.1.2.4  yamt 	int err, mode = 0;
    323  1.1.2.4  yamt 
    324  1.1.2.4  yamt 	DPRINTF("handle #%d, auth %s, encrypt %s, secure %s\n",
    325  1.1.2.4  yamt 		link->hl_handle,
    326  1.1.2.4  yamt 		(link->hl_flags & HCI_LINK_AUTH ? "on" : "off"),
    327  1.1.2.4  yamt 		(link->hl_flags & HCI_LINK_ENCRYPT ? "on" : "off"),
    328  1.1.2.4  yamt 		(link->hl_flags & HCI_LINK_SECURE ? "on" : "off"));
    329  1.1.2.4  yamt 
    330  1.1.2.4  yamt 	if (link->hl_flags & HCI_LINK_AUTH)
    331  1.1.2.4  yamt 		mode |= L2CAP_LM_AUTH;
    332  1.1.2.4  yamt 
    333  1.1.2.4  yamt 	if (link->hl_flags & HCI_LINK_ENCRYPT)
    334  1.1.2.4  yamt 		mode |= L2CAP_LM_ENCRYPT;
    335  1.1.2.4  yamt 
    336  1.1.2.4  yamt 	if (link->hl_flags & HCI_LINK_SECURE)
    337  1.1.2.4  yamt 		mode |= L2CAP_LM_SECURE;
    338  1.1.2.4  yamt 
    339  1.1.2.4  yamt 	/*
    340  1.1.2.4  yamt 	 * The link state will only be OPEN here if the mode change
    341  1.1.2.4  yamt 	 * was successful. So, we can proceed with L2CAP connections,
    342  1.1.2.4  yamt 	 * or notify already establshed channels, to allow any that
    343  1.1.2.4  yamt 	 * are dissatisfied to disconnect before we restart.
    344  1.1.2.4  yamt 	 */
    345  1.1.2.4  yamt 	next = LIST_FIRST(&l2cap_active_list);
    346  1.1.2.4  yamt 	while ((chan = next) != NULL) {
    347  1.1.2.4  yamt 		next = LIST_NEXT(chan, lc_ncid);
    348  1.1.2.4  yamt 
    349  1.1.2.4  yamt 		if (chan->lc_link != link)
    350  1.1.2.4  yamt 			continue;
    351  1.1.2.4  yamt 
    352  1.1.2.4  yamt 		switch(chan->lc_state) {
    353  1.1.2.4  yamt 		case L2CAP_WAIT_SEND_CONNECT_REQ: /* we are connecting */
    354  1.1.2.4  yamt 			if ((mode & chan->lc_mode) != chan->lc_mode) {
    355  1.1.2.4  yamt 				l2cap_close(chan, ECONNABORTED);
    356  1.1.2.4  yamt 				break;
    357  1.1.2.4  yamt 			}
    358  1.1.2.4  yamt 
    359  1.1.2.4  yamt 			chan->lc_state = L2CAP_WAIT_RECV_CONNECT_RSP;
    360  1.1.2.4  yamt 			err = l2cap_send_connect_req(chan);
    361  1.1.2.4  yamt 			if (err) {
    362  1.1.2.4  yamt 				l2cap_close(chan, err);
    363  1.1.2.4  yamt 				break;
    364  1.1.2.4  yamt 			}
    365  1.1.2.4  yamt 			break;
    366  1.1.2.4  yamt 
    367  1.1.2.4  yamt 		case L2CAP_WAIT_SEND_CONNECT_RSP: /* they are connecting */
    368  1.1.2.4  yamt 			if ((mode & chan->lc_mode) != chan->lc_mode) {
    369  1.1.2.4  yamt 				l2cap_send_connect_rsp(link, chan->lc_ident,
    370  1.1.2.4  yamt 							0, chan->lc_rcid,
    371  1.1.2.4  yamt 							L2CAP_SECURITY_BLOCK);
    372  1.1.2.4  yamt 
    373  1.1.2.4  yamt 				l2cap_close(chan, ECONNABORTED);
    374  1.1.2.4  yamt 				break;
    375  1.1.2.4  yamt 			}
    376  1.1.2.4  yamt 
    377  1.1.2.4  yamt 			l2cap_send_connect_rsp(link, chan->lc_ident,
    378  1.1.2.4  yamt 						chan->lc_lcid, chan->lc_rcid,
    379  1.1.2.4  yamt 						L2CAP_SUCCESS);
    380  1.1.2.4  yamt 
    381  1.1.2.4  yamt 			chan->lc_state = L2CAP_WAIT_CONFIG;
    382  1.1.2.4  yamt 			chan->lc_flags |= (L2CAP_WAIT_CONFIG_RSP | L2CAP_WAIT_CONFIG_REQ);
    383  1.1.2.4  yamt 			err = l2cap_send_config_req(chan);
    384  1.1.2.4  yamt 			if (err) {
    385  1.1.2.4  yamt 				l2cap_close(chan, err);
    386  1.1.2.4  yamt 				break;
    387  1.1.2.4  yamt 			}
    388  1.1.2.4  yamt 			break;
    389  1.1.2.4  yamt 
    390  1.1.2.4  yamt 		case L2CAP_WAIT_RECV_CONNECT_RSP:
    391  1.1.2.4  yamt 		case L2CAP_WAIT_CONFIG:
    392  1.1.2.4  yamt 		case L2CAP_OPEN: /* already established */
    393  1.1.2.4  yamt 			(*chan->lc_proto->linkmode)(chan->lc_upper, mode);
    394  1.1.2.4  yamt 			break;
    395  1.1.2.4  yamt 
    396  1.1.2.4  yamt 		default:
    397  1.1.2.4  yamt 			break;
    398  1.1.2.4  yamt 		}
    399  1.1.2.4  yamt 	}
    400  1.1.2.4  yamt 
    401  1.1.2.4  yamt 	link->hl_state = HCI_LINK_OPEN;
    402  1.1.2.4  yamt 	hci_acl_start(link);
    403  1.1.2.4  yamt }
    404  1.1.2.4  yamt 
    405  1.1.2.4  yamt /*
    406  1.1.2.2  yamt  * Receive ACL Data
    407  1.1.2.2  yamt  *
    408  1.1.2.2  yamt  * we accumulate packet fragments on the hci_link structure
    409  1.1.2.2  yamt  * until a full L2CAP frame is ready, then send it on.
    410  1.1.2.2  yamt  */
    411  1.1.2.2  yamt void
    412  1.1.2.2  yamt hci_acl_recv(struct mbuf *m, struct hci_unit *unit)
    413  1.1.2.2  yamt {
    414  1.1.2.2  yamt 	struct hci_link *link;
    415  1.1.2.2  yamt 	hci_acldata_hdr_t hdr;
    416  1.1.2.2  yamt 	uint16_t handle, want;
    417  1.1.2.2  yamt 	int pb, got;
    418  1.1.2.2  yamt 
    419  1.1.2.4  yamt 	KASSERT(m != NULL);
    420  1.1.2.4  yamt 	KASSERT(unit != NULL);
    421  1.1.2.2  yamt 
    422  1.1.2.2  yamt 	KASSERT(m->m_pkthdr.len >= sizeof(hdr));
    423  1.1.2.2  yamt 	m_copydata(m, 0, sizeof(hdr), &hdr);
    424  1.1.2.2  yamt 	m_adj(m, sizeof(hdr));
    425  1.1.2.2  yamt 
    426  1.1.2.2  yamt #ifdef DIAGNOSTIC
    427  1.1.2.2  yamt 	if (hdr.type != HCI_ACL_DATA_PKT) {
    428  1.1.2.6  yamt 		aprint_error_dev(unit->hci_dev, "bad ACL packet type\n");
    429  1.1.2.2  yamt 		goto bad;
    430  1.1.2.2  yamt 	}
    431  1.1.2.2  yamt 
    432  1.1.2.2  yamt 	if (m->m_pkthdr.len != le16toh(hdr.length)) {
    433  1.1.2.6  yamt 		aprint_error_dev(unit->hci_dev,
    434  1.1.2.6  yamt 		    "bad ACL packet length (%d != %d)\n",
    435  1.1.2.6  yamt 		    m->m_pkthdr.len, le16toh(hdr.length));
    436  1.1.2.2  yamt 		goto bad;
    437  1.1.2.2  yamt 	}
    438  1.1.2.2  yamt #endif
    439  1.1.2.2  yamt 
    440  1.1.2.2  yamt 	hdr.length = le16toh(hdr.length);
    441  1.1.2.2  yamt 	hdr.con_handle = le16toh(hdr.con_handle);
    442  1.1.2.2  yamt 	handle = HCI_CON_HANDLE(hdr.con_handle);
    443  1.1.2.2  yamt 	pb = HCI_PB_FLAG(hdr.con_handle);
    444  1.1.2.2  yamt 
    445  1.1.2.2  yamt 	link = hci_link_lookup_handle(unit, handle);
    446  1.1.2.2  yamt 	if (link == NULL) {
    447  1.1.2.2  yamt 		hci_discon_cp cp;
    448  1.1.2.2  yamt 
    449  1.1.2.2  yamt 		DPRINTF("%s: dumping packet for unknown handle #%d\n",
    450  1.1.2.6  yamt 			device_xname(unit->hci_dev), handle);
    451  1.1.2.2  yamt 
    452  1.1.2.2  yamt 		/*
    453  1.1.2.2  yamt 		 * There is no way to find out what this connection handle is
    454  1.1.2.2  yamt 		 * for, just get rid of it. This may happen, if a USB dongle
    455  1.1.2.2  yamt 		 * is plugged into a self powered hub and does not reset when
    456  1.1.2.2  yamt 		 * the system is shut down.
    457  1.1.2.2  yamt 		 */
    458  1.1.2.2  yamt 		cp.con_handle = htole16(handle);
    459  1.1.2.2  yamt 		cp.reason = 0x13; /* "Remote User Terminated Connection" */
    460  1.1.2.2  yamt 		hci_send_cmd(unit, HCI_CMD_DISCONNECT, &cp, sizeof(cp));
    461  1.1.2.2  yamt 		goto bad;
    462  1.1.2.2  yamt 	}
    463  1.1.2.2  yamt 
    464  1.1.2.2  yamt 	switch (pb) {
    465  1.1.2.2  yamt 	case HCI_PACKET_START:
    466  1.1.2.2  yamt 		if (link->hl_rxp != NULL)
    467  1.1.2.6  yamt 			aprint_error_dev(unit->hci_dev,
    468  1.1.2.6  yamt 			    "dropped incomplete ACL packet\n");
    469  1.1.2.2  yamt 
    470  1.1.2.2  yamt 		if (m->m_pkthdr.len < sizeof(l2cap_hdr_t)) {
    471  1.1.2.6  yamt 			aprint_error_dev(unit->hci_dev, "short ACL packet\n");
    472  1.1.2.2  yamt 			goto bad;
    473  1.1.2.2  yamt 		}
    474  1.1.2.2  yamt 
    475  1.1.2.2  yamt 		link->hl_rxp = m;
    476  1.1.2.2  yamt 		got = m->m_pkthdr.len;
    477  1.1.2.2  yamt 		break;
    478  1.1.2.2  yamt 
    479  1.1.2.2  yamt 	case HCI_PACKET_FRAGMENT:
    480  1.1.2.2  yamt 		if (link->hl_rxp == NULL) {
    481  1.1.2.6  yamt 			aprint_error_dev(unit->hci_dev,
    482  1.1.2.6  yamt 			    "unexpected packet fragment\n");
    483  1.1.2.2  yamt 
    484  1.1.2.2  yamt 			goto bad;
    485  1.1.2.2  yamt 		}
    486  1.1.2.2  yamt 
    487  1.1.2.2  yamt 		got = m->m_pkthdr.len + link->hl_rxp->m_pkthdr.len;
    488  1.1.2.2  yamt 		m_cat(link->hl_rxp, m);
    489  1.1.2.2  yamt 		m = link->hl_rxp;
    490  1.1.2.2  yamt 		m->m_pkthdr.len = got;
    491  1.1.2.2  yamt 		break;
    492  1.1.2.2  yamt 
    493  1.1.2.2  yamt 	default:
    494  1.1.2.6  yamt 		aprint_error_dev(unit->hci_dev, "unknown packet type\n");
    495  1.1.2.2  yamt 		goto bad;
    496  1.1.2.2  yamt 	}
    497  1.1.2.2  yamt 
    498  1.1.2.2  yamt 	m_copydata(m, 0, sizeof(want), &want);
    499  1.1.2.2  yamt 	want = le16toh(want) + sizeof(l2cap_hdr_t) - got;
    500  1.1.2.2  yamt 
    501  1.1.2.2  yamt 	if (want > 0)
    502  1.1.2.2  yamt 		return;
    503  1.1.2.2  yamt 
    504  1.1.2.2  yamt 	link->hl_rxp = NULL;
    505  1.1.2.2  yamt 
    506  1.1.2.2  yamt 	if (want == 0) {
    507  1.1.2.2  yamt 		l2cap_recv_frame(m, link);
    508  1.1.2.2  yamt 		return;
    509  1.1.2.2  yamt 	}
    510  1.1.2.2  yamt 
    511  1.1.2.2  yamt bad:
    512  1.1.2.2  yamt 	m_freem(m);
    513  1.1.2.2  yamt }
    514  1.1.2.2  yamt 
    515  1.1.2.2  yamt /*
    516  1.1.2.2  yamt  * Send ACL data on link
    517  1.1.2.2  yamt  *
    518  1.1.2.2  yamt  * We must fragment packets into chunks of less than unit->hci_max_acl_size and
    519  1.1.2.2  yamt  * prepend a relevant ACL header to each fragment. We keep a PDU structure
    520  1.1.2.2  yamt  * attached to the link, so that completed fragments can be marked off and
    521  1.1.2.2  yamt  * more data requested from above once the PDU is sent.
    522  1.1.2.2  yamt  */
    523  1.1.2.2  yamt int
    524  1.1.2.2  yamt hci_acl_send(struct mbuf *m, struct hci_link *link,
    525  1.1.2.2  yamt 		struct l2cap_channel *chan)
    526  1.1.2.2  yamt {
    527  1.1.2.2  yamt 	struct l2cap_pdu *pdu;
    528  1.1.2.2  yamt 	struct mbuf *n = NULL;
    529  1.1.2.2  yamt 	int plen, mlen, num = 0;
    530  1.1.2.2  yamt 
    531  1.1.2.4  yamt 	KASSERT(link != NULL);
    532  1.1.2.4  yamt 	KASSERT(m != NULL);
    533  1.1.2.2  yamt 	KASSERT(m->m_flags & M_PKTHDR);
    534  1.1.2.2  yamt 	KASSERT(m->m_pkthdr.len > 0);
    535  1.1.2.2  yamt 
    536  1.1.2.2  yamt 	if (link->hl_state == HCI_LINK_CLOSED) {
    537  1.1.2.2  yamt 		m_freem(m);
    538  1.1.2.2  yamt 		return ENETDOWN;
    539  1.1.2.2  yamt 	}
    540  1.1.2.2  yamt 
    541  1.1.2.2  yamt 	pdu = pool_get(&l2cap_pdu_pool, PR_NOWAIT);
    542  1.1.2.2  yamt 	if (pdu == NULL)
    543  1.1.2.2  yamt 		goto nomem;
    544  1.1.2.2  yamt 
    545  1.1.2.2  yamt 	pdu->lp_chan = chan;
    546  1.1.2.2  yamt 	pdu->lp_pending = 0;
    547  1.1.2.2  yamt 	MBUFQ_INIT(&pdu->lp_data);
    548  1.1.2.2  yamt 
    549  1.1.2.2  yamt 	plen = m->m_pkthdr.len;
    550  1.1.2.2  yamt 	mlen = link->hl_unit->hci_max_acl_size;
    551  1.1.2.2  yamt 
    552  1.1.2.2  yamt 	DPRINTFN(5, "%s: handle #%d, plen = %d, max = %d\n",
    553  1.1.2.6  yamt 		device_xname(link->hl_unit->hci_dev), link->hl_handle, plen, mlen);
    554  1.1.2.2  yamt 
    555  1.1.2.2  yamt 	while (plen > 0) {
    556  1.1.2.2  yamt 		if (plen > mlen) {
    557  1.1.2.2  yamt 			n = m_split(m, mlen, M_DONTWAIT);
    558  1.1.2.2  yamt 			if (n == NULL)
    559  1.1.2.2  yamt 				goto nomem;
    560  1.1.2.2  yamt 		} else {
    561  1.1.2.2  yamt 			mlen = plen;
    562  1.1.2.2  yamt 		}
    563  1.1.2.2  yamt 
    564  1.1.2.2  yamt 		if (num++ == 0)
    565  1.1.2.2  yamt 			m->m_flags |= M_PROTO1;	/* tag first fragment */
    566  1.1.2.2  yamt 
    567  1.1.2.2  yamt 		DPRINTFN(10, "chunk of %d (plen = %d) bytes\n", mlen, plen);
    568  1.1.2.2  yamt 		MBUFQ_ENQUEUE(&pdu->lp_data, m);
    569  1.1.2.2  yamt 		m = n;
    570  1.1.2.2  yamt 		plen -= mlen;
    571  1.1.2.2  yamt 	}
    572  1.1.2.2  yamt 
    573  1.1.2.2  yamt 	TAILQ_INSERT_TAIL(&link->hl_txq, pdu, lp_next);
    574  1.1.2.2  yamt 	link->hl_txqlen += num;
    575  1.1.2.2  yamt 
    576  1.1.2.2  yamt 	hci_acl_start(link);
    577  1.1.2.2  yamt 
    578  1.1.2.2  yamt 	return 0;
    579  1.1.2.2  yamt 
    580  1.1.2.2  yamt nomem:
    581  1.1.2.2  yamt 	if (m) m_freem(m);
    582  1.1.2.2  yamt 	if (pdu) {
    583  1.1.2.2  yamt 		MBUFQ_DRAIN(&pdu->lp_data);
    584  1.1.2.2  yamt 		pool_put(&l2cap_pdu_pool, pdu);
    585  1.1.2.2  yamt 	}
    586  1.1.2.2  yamt 
    587  1.1.2.2  yamt 	return ENOMEM;
    588  1.1.2.2  yamt }
    589  1.1.2.2  yamt 
    590  1.1.2.2  yamt /*
    591  1.1.2.2  yamt  * Start sending ACL data on link.
    592  1.1.2.2  yamt  *
    593  1.1.2.4  yamt  *	This is called when the queue may need restarting: as new data
    594  1.1.2.4  yamt  * is queued, after link mode changes have completed, or when device
    595  1.1.2.4  yamt  * buffers have cleared.
    596  1.1.2.4  yamt  *
    597  1.1.2.2  yamt  *	We may use all the available packet slots. The reason that we add
    598  1.1.2.2  yamt  * the ACL encapsulation here rather than in hci_acl_send() is that L2CAP
    599  1.1.2.2  yamt  * signal packets may be queued before the handle is given to us..
    600  1.1.2.2  yamt  */
    601  1.1.2.2  yamt void
    602  1.1.2.2  yamt hci_acl_start(struct hci_link *link)
    603  1.1.2.2  yamt {
    604  1.1.2.2  yamt 	struct hci_unit *unit;
    605  1.1.2.2  yamt 	hci_acldata_hdr_t *hdr;
    606  1.1.2.2  yamt 	struct l2cap_pdu *pdu;
    607  1.1.2.2  yamt 	struct mbuf *m;
    608  1.1.2.2  yamt 	uint16_t handle;
    609  1.1.2.2  yamt 
    610  1.1.2.4  yamt 	KASSERT(link != NULL);
    611  1.1.2.2  yamt 
    612  1.1.2.2  yamt 	unit = link->hl_unit;
    613  1.1.2.4  yamt 	KASSERT(unit != NULL);
    614  1.1.2.2  yamt 
    615  1.1.2.2  yamt 	/* this is mainly to block ourselves (below) */
    616  1.1.2.2  yamt 	if (link->hl_state != HCI_LINK_OPEN)
    617  1.1.2.2  yamt 		return;
    618  1.1.2.2  yamt 
    619  1.1.2.2  yamt 	if (link->hl_txqlen == 0 || unit->hci_num_acl_pkts == 0)
    620  1.1.2.2  yamt 		return;
    621  1.1.2.2  yamt 
    622  1.1.2.2  yamt 	/* find first PDU with data to send */
    623  1.1.2.2  yamt 	pdu = TAILQ_FIRST(&link->hl_txq);
    624  1.1.2.2  yamt 	for (;;) {
    625  1.1.2.2  yamt 		if (pdu == NULL)
    626  1.1.2.2  yamt 			return;
    627  1.1.2.2  yamt 
    628  1.1.2.2  yamt 		if (MBUFQ_FIRST(&pdu->lp_data) != NULL)
    629  1.1.2.2  yamt 			break;
    630  1.1.2.2  yamt 
    631  1.1.2.2  yamt 		pdu = TAILQ_NEXT(pdu, lp_next);
    632  1.1.2.2  yamt 	}
    633  1.1.2.2  yamt 
    634  1.1.2.2  yamt 	while (unit->hci_num_acl_pkts > 0) {
    635  1.1.2.2  yamt 		MBUFQ_DEQUEUE(&pdu->lp_data, m);
    636  1.1.2.2  yamt 		KASSERT(m != NULL);
    637  1.1.2.2  yamt 
    638  1.1.2.2  yamt 		if (m->m_flags & M_PROTO1)
    639  1.1.2.2  yamt 			handle = HCI_MK_CON_HANDLE(link->hl_handle,
    640  1.1.2.2  yamt 						HCI_PACKET_START, 0);
    641  1.1.2.2  yamt 		else
    642  1.1.2.2  yamt 			handle = HCI_MK_CON_HANDLE(link->hl_handle,
    643  1.1.2.2  yamt 						HCI_PACKET_FRAGMENT, 0);
    644  1.1.2.2  yamt 
    645  1.1.2.2  yamt 		M_PREPEND(m, sizeof(*hdr), M_DONTWAIT);
    646  1.1.2.2  yamt 		if (m == NULL)
    647  1.1.2.2  yamt 			break;
    648  1.1.2.2  yamt 
    649  1.1.2.2  yamt 		hdr = mtod(m, hci_acldata_hdr_t *);
    650  1.1.2.2  yamt 		hdr->type = HCI_ACL_DATA_PKT;
    651  1.1.2.2  yamt 		hdr->con_handle = htole16(handle);
    652  1.1.2.2  yamt 		hdr->length = htole16(m->m_pkthdr.len - sizeof(*hdr));
    653  1.1.2.2  yamt 
    654  1.1.2.2  yamt 		link->hl_txqlen--;
    655  1.1.2.2  yamt 		pdu->lp_pending++;
    656  1.1.2.2  yamt 
    657  1.1.2.2  yamt 		hci_output_acl(unit, m);
    658  1.1.2.2  yamt 
    659  1.1.2.2  yamt 		if (MBUFQ_FIRST(&pdu->lp_data) == NULL) {
    660  1.1.2.2  yamt 			if (pdu->lp_chan) {
    661  1.1.2.2  yamt 				/*
    662  1.1.2.2  yamt 				 * This should enable streaming of PDUs - when
    663  1.1.2.2  yamt 				 * we have placed all the fragments on the acl
    664  1.1.2.2  yamt 				 * output queue, we trigger the L2CAP layer to
    665  1.1.2.2  yamt 				 * send us down one more. Use a false state so
    666  1.1.2.2  yamt 				 * we dont run into ourselves coming back from
    667  1.1.2.2  yamt 				 * the future..
    668  1.1.2.2  yamt 				 */
    669  1.1.2.2  yamt 				link->hl_state = HCI_LINK_BLOCK;
    670  1.1.2.2  yamt 				l2cap_start(pdu->lp_chan);
    671  1.1.2.2  yamt 				link->hl_state = HCI_LINK_OPEN;
    672  1.1.2.2  yamt 			}
    673  1.1.2.2  yamt 
    674  1.1.2.2  yamt 			pdu = TAILQ_NEXT(pdu, lp_next);
    675  1.1.2.2  yamt 			if (pdu == NULL)
    676  1.1.2.2  yamt 				break;
    677  1.1.2.2  yamt 		}
    678  1.1.2.2  yamt 	}
    679  1.1.2.2  yamt 
    680  1.1.2.2  yamt 	/*
    681  1.1.2.2  yamt 	 * We had our turn now, move to the back of the queue to let
    682  1.1.2.2  yamt 	 * other links have a go at the output buffers..
    683  1.1.2.2  yamt 	 */
    684  1.1.2.2  yamt 	if (TAILQ_NEXT(link, hl_next)) {
    685  1.1.2.2  yamt 		TAILQ_REMOVE(&unit->hci_links, link, hl_next);
    686  1.1.2.2  yamt 		TAILQ_INSERT_TAIL(&unit->hci_links, link, hl_next);
    687  1.1.2.2  yamt 	}
    688  1.1.2.2  yamt }
    689  1.1.2.2  yamt 
    690  1.1.2.2  yamt /*
    691  1.1.2.2  yamt  * Confirm ACL packets cleared from Controller buffers. We scan our PDU
    692  1.1.2.2  yamt  * list to clear pending fragments and signal upstream for more data
    693  1.1.2.2  yamt  * when a PDU is complete.
    694  1.1.2.2  yamt  */
    695  1.1.2.2  yamt void
    696  1.1.2.2  yamt hci_acl_complete(struct hci_link *link, int num)
    697  1.1.2.2  yamt {
    698  1.1.2.2  yamt 	struct l2cap_pdu *pdu;
    699  1.1.2.2  yamt 	struct l2cap_channel *chan;
    700  1.1.2.2  yamt 
    701  1.1.2.2  yamt 	DPRINTFN(5, "handle #%d (%d)\n", link->hl_handle, num);
    702  1.1.2.2  yamt 
    703  1.1.2.2  yamt 	while (num > 0) {
    704  1.1.2.2  yamt 		pdu = TAILQ_FIRST(&link->hl_txq);
    705  1.1.2.2  yamt 		if (pdu == NULL) {
    706  1.1.2.6  yamt 			aprint_error_dev(link->hl_unit->hci_dev,
    707  1.1.2.6  yamt 			    "%d packets completed on handle #%x but none pending!\n",
    708  1.1.2.6  yamt 			    num, link->hl_handle);
    709  1.1.2.6  yamt 
    710  1.1.2.2  yamt 			return;
    711  1.1.2.2  yamt 		}
    712  1.1.2.2  yamt 
    713  1.1.2.2  yamt 		if (num >= pdu->lp_pending) {
    714  1.1.2.2  yamt 			num -= pdu->lp_pending;
    715  1.1.2.2  yamt 			pdu->lp_pending = 0;
    716  1.1.2.2  yamt 
    717  1.1.2.2  yamt 			if (MBUFQ_FIRST(&pdu->lp_data) == NULL) {
    718  1.1.2.2  yamt 				TAILQ_REMOVE(&link->hl_txq, pdu, lp_next);
    719  1.1.2.2  yamt 				chan = pdu->lp_chan;
    720  1.1.2.2  yamt 				if (chan != NULL) {
    721  1.1.2.2  yamt 					chan->lc_pending--;
    722  1.1.2.2  yamt 					(*chan->lc_proto->complete)
    723  1.1.2.2  yamt 							(chan->lc_upper, 1);
    724  1.1.2.2  yamt 
    725  1.1.2.2  yamt 					if (chan->lc_pending == 0)
    726  1.1.2.2  yamt 						l2cap_start(chan);
    727  1.1.2.2  yamt 				}
    728  1.1.2.2  yamt 
    729  1.1.2.2  yamt 				pool_put(&l2cap_pdu_pool, pdu);
    730  1.1.2.2  yamt 			}
    731  1.1.2.2  yamt 		} else {
    732  1.1.2.2  yamt 			pdu->lp_pending -= num;
    733  1.1.2.2  yamt 			num = 0;
    734  1.1.2.2  yamt 		}
    735  1.1.2.2  yamt 	}
    736  1.1.2.2  yamt }
    737  1.1.2.2  yamt 
    738  1.1.2.2  yamt /*******************************************************************************
    739  1.1.2.2  yamt  *
    740  1.1.2.2  yamt  *	HCI SCO Connections
    741  1.1.2.2  yamt  */
    742  1.1.2.2  yamt 
    743  1.1.2.2  yamt /*
    744  1.1.2.3  yamt  * Incoming SCO Connection. We check the list for anybody willing
    745  1.1.2.3  yamt  * to take it.
    746  1.1.2.2  yamt  */
    747  1.1.2.2  yamt struct hci_link *
    748  1.1.2.2  yamt hci_sco_newconn(struct hci_unit *unit, bdaddr_t *bdaddr)
    749  1.1.2.2  yamt {
    750  1.1.2.3  yamt 	struct sockaddr_bt laddr, raddr;
    751  1.1.2.3  yamt 	struct sco_pcb *pcb, *new;
    752  1.1.2.3  yamt 	struct hci_link *sco, *acl;
    753  1.1.2.3  yamt 
    754  1.1.2.3  yamt 	memset(&laddr, 0, sizeof(laddr));
    755  1.1.2.3  yamt 	laddr.bt_len = sizeof(laddr);
    756  1.1.2.3  yamt 	laddr.bt_family = AF_BLUETOOTH;
    757  1.1.2.3  yamt 	bdaddr_copy(&laddr.bt_bdaddr, &unit->hci_bdaddr);
    758  1.1.2.3  yamt 
    759  1.1.2.3  yamt 	memset(&raddr, 0, sizeof(raddr));
    760  1.1.2.3  yamt 	raddr.bt_len = sizeof(raddr);
    761  1.1.2.3  yamt 	raddr.bt_family = AF_BLUETOOTH;
    762  1.1.2.3  yamt 	bdaddr_copy(&raddr.bt_bdaddr, bdaddr);
    763  1.1.2.3  yamt 
    764  1.1.2.3  yamt 	/*
    765  1.1.2.3  yamt 	 * There should already be an ACL link up and running before
    766  1.1.2.3  yamt 	 * the controller sends us SCO connection requests, but you
    767  1.1.2.3  yamt 	 * never know..
    768  1.1.2.3  yamt 	 */
    769  1.1.2.3  yamt 	acl = hci_link_lookup_bdaddr(unit, bdaddr, HCI_LINK_ACL);
    770  1.1.2.3  yamt 	if (acl == NULL || acl->hl_state != HCI_LINK_OPEN)
    771  1.1.2.3  yamt 		return NULL;
    772  1.1.2.3  yamt 
    773  1.1.2.3  yamt 	LIST_FOREACH(pcb, &sco_pcb, sp_next) {
    774  1.1.2.3  yamt 		if ((pcb->sp_flags & SP_LISTENING) == 0)
    775  1.1.2.3  yamt 			continue;
    776  1.1.2.3  yamt 
    777  1.1.2.3  yamt 		new = (*pcb->sp_proto->newconn)(pcb->sp_upper, &laddr, &raddr);
    778  1.1.2.3  yamt 		if (new == NULL)
    779  1.1.2.3  yamt 			continue;
    780  1.1.2.3  yamt 
    781  1.1.2.3  yamt 		/*
    782  1.1.2.3  yamt 		 * Ok, got new pcb so we can start a new link and fill
    783  1.1.2.3  yamt 		 * in all the details.
    784  1.1.2.3  yamt 		 */
    785  1.1.2.3  yamt 		bdaddr_copy(&new->sp_laddr, &unit->hci_bdaddr);
    786  1.1.2.3  yamt 		bdaddr_copy(&new->sp_raddr, bdaddr);
    787  1.1.2.3  yamt 
    788  1.1.2.3  yamt 		sco = hci_link_alloc(unit);
    789  1.1.2.3  yamt 		if (sco == NULL) {
    790  1.1.2.3  yamt 			sco_detach(&new);
    791  1.1.2.3  yamt 			return NULL;
    792  1.1.2.3  yamt 		}
    793  1.1.2.3  yamt 
    794  1.1.2.3  yamt 		sco->hl_type = HCI_LINK_SCO;
    795  1.1.2.3  yamt 		bdaddr_copy(&sco->hl_bdaddr, bdaddr);
    796  1.1.2.3  yamt 
    797  1.1.2.3  yamt 		sco->hl_link = hci_acl_open(unit, bdaddr);
    798  1.1.2.3  yamt 		KASSERT(sco->hl_link == acl);
    799  1.1.2.3  yamt 
    800  1.1.2.3  yamt 		sco->hl_sco = new;
    801  1.1.2.3  yamt 		new->sp_link = sco;
    802  1.1.2.3  yamt 
    803  1.1.2.3  yamt 		new->sp_mtu = unit->hci_max_sco_size;
    804  1.1.2.3  yamt 		return sco;
    805  1.1.2.3  yamt 	}
    806  1.1.2.2  yamt 
    807  1.1.2.2  yamt 	return NULL;
    808  1.1.2.2  yamt }
    809  1.1.2.2  yamt 
    810  1.1.2.2  yamt /*
    811  1.1.2.2  yamt  * receive SCO packet, we only need to strip the header and send
    812  1.1.2.2  yamt  * it to the right handler
    813  1.1.2.2  yamt  */
    814  1.1.2.2  yamt void
    815  1.1.2.2  yamt hci_sco_recv(struct mbuf *m, struct hci_unit *unit)
    816  1.1.2.2  yamt {
    817  1.1.2.2  yamt 	struct hci_link *link;
    818  1.1.2.2  yamt 	hci_scodata_hdr_t hdr;
    819  1.1.2.2  yamt 	uint16_t handle;
    820  1.1.2.2  yamt 
    821  1.1.2.4  yamt 	KASSERT(m != NULL);
    822  1.1.2.4  yamt 	KASSERT(unit != NULL);
    823  1.1.2.2  yamt 
    824  1.1.2.2  yamt 	KASSERT(m->m_pkthdr.len >= sizeof(hdr));
    825  1.1.2.2  yamt 	m_copydata(m, 0, sizeof(hdr), &hdr);
    826  1.1.2.2  yamt 	m_adj(m, sizeof(hdr));
    827  1.1.2.2  yamt 
    828  1.1.2.2  yamt #ifdef DIAGNOSTIC
    829  1.1.2.2  yamt 	if (hdr.type != HCI_SCO_DATA_PKT) {
    830  1.1.2.6  yamt 		aprint_error_dev(unit->hci_dev, "bad SCO packet type\n");
    831  1.1.2.2  yamt 		goto bad;
    832  1.1.2.2  yamt 	}
    833  1.1.2.2  yamt 
    834  1.1.2.2  yamt 	if (m->m_pkthdr.len != hdr.length) {
    835  1.1.2.6  yamt 		aprint_error_dev(unit->hci_dev,
    836  1.1.2.6  yamt 		    "bad SCO packet length (%d != %d)\n",
    837  1.1.2.6  yamt 		    m->m_pkthdr.len, hdr.length);
    838  1.1.2.6  yamt 
    839  1.1.2.2  yamt 		goto bad;
    840  1.1.2.2  yamt 	}
    841  1.1.2.2  yamt #endif
    842  1.1.2.2  yamt 
    843  1.1.2.2  yamt 	hdr.con_handle = le16toh(hdr.con_handle);
    844  1.1.2.2  yamt 	handle = HCI_CON_HANDLE(hdr.con_handle);
    845  1.1.2.2  yamt 
    846  1.1.2.2  yamt 	link = hci_link_lookup_handle(unit, handle);
    847  1.1.2.2  yamt 	if (link == NULL || link->hl_type == HCI_LINK_ACL) {
    848  1.1.2.2  yamt 		DPRINTF("%s: dumping packet for unknown handle #%d\n",
    849  1.1.2.6  yamt 			device_xname(unit->hci_dev), handle);
    850  1.1.2.2  yamt 
    851  1.1.2.2  yamt 		goto bad;
    852  1.1.2.2  yamt 	}
    853  1.1.2.2  yamt 
    854  1.1.2.2  yamt 	(*link->hl_sco->sp_proto->input)(link->hl_sco->sp_upper, m);
    855  1.1.2.2  yamt 	return;
    856  1.1.2.2  yamt 
    857  1.1.2.2  yamt bad:
    858  1.1.2.2  yamt 	m_freem(m);
    859  1.1.2.2  yamt }
    860  1.1.2.2  yamt 
    861  1.1.2.2  yamt void
    862  1.1.2.2  yamt hci_sco_start(struct hci_link *link)
    863  1.1.2.2  yamt {
    864  1.1.2.2  yamt }
    865  1.1.2.2  yamt 
    866  1.1.2.2  yamt /*
    867  1.1.2.2  yamt  * SCO packets have completed at the controller, so we can
    868  1.1.2.2  yamt  * signal up to free the buffer space.
    869  1.1.2.2  yamt  */
    870  1.1.2.2  yamt void
    871  1.1.2.2  yamt hci_sco_complete(struct hci_link *link, int num)
    872  1.1.2.2  yamt {
    873  1.1.2.2  yamt 
    874  1.1.2.2  yamt 	DPRINTFN(5, "handle #%d (num=%d)\n", link->hl_handle, num);
    875  1.1.2.2  yamt 	link->hl_sco->sp_pending--;
    876  1.1.2.2  yamt 	(*link->hl_sco->sp_proto->complete)(link->hl_sco->sp_upper, num);
    877  1.1.2.2  yamt }
    878  1.1.2.2  yamt 
    879  1.1.2.2  yamt /*******************************************************************************
    880  1.1.2.2  yamt  *
    881  1.1.2.2  yamt  *	Generic HCI Connection alloc/free/lookup etc
    882  1.1.2.2  yamt  */
    883  1.1.2.2  yamt 
    884  1.1.2.2  yamt struct hci_link *
    885  1.1.2.2  yamt hci_link_alloc(struct hci_unit *unit)
    886  1.1.2.2  yamt {
    887  1.1.2.2  yamt 	struct hci_link *link;
    888  1.1.2.2  yamt 
    889  1.1.2.4  yamt 	KASSERT(unit != NULL);
    890  1.1.2.2  yamt 
    891  1.1.2.2  yamt 	link = malloc(sizeof(struct hci_link), M_BLUETOOTH, M_NOWAIT | M_ZERO);
    892  1.1.2.2  yamt 	if (link == NULL)
    893  1.1.2.2  yamt 		return NULL;
    894  1.1.2.2  yamt 
    895  1.1.2.2  yamt 	link->hl_unit = unit;
    896  1.1.2.2  yamt 	link->hl_state = HCI_LINK_CLOSED;
    897  1.1.2.2  yamt 
    898  1.1.2.2  yamt 	/* init ACL portion */
    899  1.1.2.4  yamt 	callout_init(&link->hl_expire, 0);
    900  1.1.2.2  yamt 	callout_setfunc(&link->hl_expire, hci_acl_timeout, link);
    901  1.1.2.2  yamt 
    902  1.1.2.2  yamt 	TAILQ_INIT(&link->hl_txq);	/* outgoing packets */
    903  1.1.2.2  yamt 	TAILQ_INIT(&link->hl_reqs);	/* request queue */
    904  1.1.2.2  yamt 
    905  1.1.2.2  yamt 	link->hl_mtu = L2CAP_MTU_DEFAULT;		/* L2CAP signal mtu */
    906  1.1.2.2  yamt 	link->hl_flush = L2CAP_FLUSH_TIMO_DEFAULT;	/* flush timeout */
    907  1.1.2.2  yamt 
    908  1.1.2.2  yamt 	/* init SCO portion */
    909  1.1.2.2  yamt 	MBUFQ_INIT(&link->hl_data);
    910  1.1.2.2  yamt 
    911  1.1.2.2  yamt 	/* attach to unit */
    912  1.1.2.2  yamt 	TAILQ_INSERT_HEAD(&unit->hci_links, link, hl_next);
    913  1.1.2.2  yamt 	return link;
    914  1.1.2.2  yamt }
    915  1.1.2.2  yamt 
    916  1.1.2.2  yamt void
    917  1.1.2.2  yamt hci_link_free(struct hci_link *link, int err)
    918  1.1.2.2  yamt {
    919  1.1.2.2  yamt 	struct l2cap_req *req;
    920  1.1.2.2  yamt 	struct l2cap_pdu *pdu;
    921  1.1.2.3  yamt 	struct l2cap_channel *chan, *next;
    922  1.1.2.2  yamt 
    923  1.1.2.4  yamt 	KASSERT(link != NULL);
    924  1.1.2.2  yamt 
    925  1.1.2.2  yamt 	DPRINTF("#%d, type = %d, state = %d, refcnt = %d\n",
    926  1.1.2.2  yamt 		link->hl_handle, link->hl_type,
    927  1.1.2.2  yamt 		link->hl_state, link->hl_refcnt);
    928  1.1.2.2  yamt 
    929  1.1.2.2  yamt 	/* ACL reference count */
    930  1.1.2.2  yamt 	if (link->hl_refcnt > 0) {
    931  1.1.2.3  yamt 		next = LIST_FIRST(&l2cap_active_list);
    932  1.1.2.3  yamt 		while ((chan = next) != NULL) {
    933  1.1.2.3  yamt 			next = LIST_NEXT(chan, lc_ncid);
    934  1.1.2.2  yamt 			if (chan->lc_link == link)
    935  1.1.2.2  yamt 				l2cap_close(chan, err);
    936  1.1.2.2  yamt 		}
    937  1.1.2.2  yamt 	}
    938  1.1.2.2  yamt 	KASSERT(link->hl_refcnt == 0);
    939  1.1.2.2  yamt 
    940  1.1.2.2  yamt 	/* ACL L2CAP requests.. */
    941  1.1.2.2  yamt 	while ((req = TAILQ_FIRST(&link->hl_reqs)) != NULL)
    942  1.1.2.2  yamt 		l2cap_request_free(req);
    943  1.1.2.2  yamt 
    944  1.1.2.2  yamt 	KASSERT(TAILQ_EMPTY(&link->hl_reqs));
    945  1.1.2.2  yamt 
    946  1.1.2.2  yamt 	/* ACL outgoing data queue */
    947  1.1.2.2  yamt 	while ((pdu = TAILQ_FIRST(&link->hl_txq)) != NULL) {
    948  1.1.2.2  yamt 		TAILQ_REMOVE(&link->hl_txq, pdu, lp_next);
    949  1.1.2.2  yamt 		MBUFQ_DRAIN(&pdu->lp_data);
    950  1.1.2.2  yamt 		if (pdu->lp_pending)
    951  1.1.2.2  yamt 			link->hl_unit->hci_num_acl_pkts += pdu->lp_pending;
    952  1.1.2.2  yamt 
    953  1.1.2.2  yamt 		pool_put(&l2cap_pdu_pool, pdu);
    954  1.1.2.2  yamt 	}
    955  1.1.2.2  yamt 
    956  1.1.2.2  yamt 	KASSERT(TAILQ_EMPTY(&link->hl_txq));
    957  1.1.2.2  yamt 
    958  1.1.2.2  yamt 	/* ACL incoming data packet */
    959  1.1.2.2  yamt 	if (link->hl_rxp != NULL) {
    960  1.1.2.2  yamt 		m_freem(link->hl_rxp);
    961  1.1.2.2  yamt 		link->hl_rxp = NULL;
    962  1.1.2.2  yamt 	}
    963  1.1.2.2  yamt 
    964  1.1.2.2  yamt 	/* SCO master ACL link */
    965  1.1.2.2  yamt 	if (link->hl_link != NULL) {
    966  1.1.2.2  yamt 		hci_acl_close(link->hl_link, err);
    967  1.1.2.2  yamt 		link->hl_link = NULL;
    968  1.1.2.2  yamt 	}
    969  1.1.2.2  yamt 
    970  1.1.2.2  yamt 	/* SCO pcb */
    971  1.1.2.2  yamt 	if (link->hl_sco != NULL) {
    972  1.1.2.2  yamt 		struct sco_pcb *pcb;
    973  1.1.2.2  yamt 
    974  1.1.2.2  yamt 		pcb = link->hl_sco;
    975  1.1.2.2  yamt 		pcb->sp_link = NULL;
    976  1.1.2.2  yamt 		link->hl_sco = NULL;
    977  1.1.2.2  yamt 		(*pcb->sp_proto->disconnected)(pcb->sp_upper, err);
    978  1.1.2.2  yamt 	}
    979  1.1.2.2  yamt 
    980  1.1.2.2  yamt 	/* flush any SCO data */
    981  1.1.2.2  yamt 	MBUFQ_DRAIN(&link->hl_data);
    982  1.1.2.2  yamt 
    983  1.1.2.2  yamt 	/*
    984  1.1.2.2  yamt 	 * Halt the callout - if its already running we cannot free the
    985  1.1.2.2  yamt 	 * link structure but the timeout function will call us back in
    986  1.1.2.2  yamt 	 * any case.
    987  1.1.2.2  yamt 	 */
    988  1.1.2.2  yamt 	link->hl_state = HCI_LINK_CLOSED;
    989  1.1.2.2  yamt 	callout_stop(&link->hl_expire);
    990  1.1.2.2  yamt 	if (callout_invoking(&link->hl_expire))
    991  1.1.2.2  yamt 		return;
    992  1.1.2.2  yamt 
    993  1.1.2.6  yamt 	callout_destroy(&link->hl_expire);
    994  1.1.2.6  yamt 
    995  1.1.2.5  yamt 	/*
    996  1.1.2.5  yamt 	 * If we made a note of clock offset, keep it in a memo
    997  1.1.2.5  yamt 	 * to facilitate reconnections to this device
    998  1.1.2.5  yamt 	 */
    999  1.1.2.5  yamt 	if (link->hl_clock != 0) {
   1000  1.1.2.5  yamt 		struct hci_memo *memo;
   1001  1.1.2.5  yamt 
   1002  1.1.2.5  yamt 		memo = hci_memo_new(link->hl_unit, &link->hl_bdaddr);
   1003  1.1.2.5  yamt 		if (memo != NULL)
   1004  1.1.2.5  yamt 			memo->clock_offset = link->hl_clock;
   1005  1.1.2.5  yamt 	}
   1006  1.1.2.5  yamt 
   1007  1.1.2.2  yamt 	TAILQ_REMOVE(&link->hl_unit->hci_links, link, hl_next);
   1008  1.1.2.2  yamt 	free(link, M_BLUETOOTH);
   1009  1.1.2.2  yamt }
   1010  1.1.2.2  yamt 
   1011  1.1.2.2  yamt /*
   1012  1.1.2.2  yamt  * Lookup HCI link by address and type. Note that for SCO links there may
   1013  1.1.2.2  yamt  * be more than one link per address, so we only return links with no
   1014  1.1.2.2  yamt  * handle (ie new links)
   1015  1.1.2.2  yamt  */
   1016  1.1.2.2  yamt struct hci_link *
   1017  1.1.2.2  yamt hci_link_lookup_bdaddr(struct hci_unit *unit, bdaddr_t *bdaddr, uint16_t type)
   1018  1.1.2.2  yamt {
   1019  1.1.2.2  yamt 	struct hci_link *link;
   1020  1.1.2.2  yamt 
   1021  1.1.2.4  yamt 	KASSERT(unit != NULL);
   1022  1.1.2.4  yamt 	KASSERT(bdaddr != NULL);
   1023  1.1.2.2  yamt 
   1024  1.1.2.2  yamt 	TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
   1025  1.1.2.2  yamt 		if (link->hl_type != type)
   1026  1.1.2.2  yamt 			continue;
   1027  1.1.2.2  yamt 
   1028  1.1.2.2  yamt 		if (type == HCI_LINK_SCO && link->hl_handle != 0)
   1029  1.1.2.2  yamt 			continue;
   1030  1.1.2.2  yamt 
   1031  1.1.2.2  yamt 		if (bdaddr_same(&link->hl_bdaddr, bdaddr))
   1032  1.1.2.2  yamt 			break;
   1033  1.1.2.2  yamt 	}
   1034  1.1.2.2  yamt 
   1035  1.1.2.2  yamt 	return link;
   1036  1.1.2.2  yamt }
   1037  1.1.2.2  yamt 
   1038  1.1.2.2  yamt struct hci_link *
   1039  1.1.2.2  yamt hci_link_lookup_handle(struct hci_unit *unit, uint16_t handle)
   1040  1.1.2.2  yamt {
   1041  1.1.2.2  yamt 	struct hci_link *link;
   1042  1.1.2.2  yamt 
   1043  1.1.2.4  yamt 	KASSERT(unit != NULL);
   1044  1.1.2.2  yamt 
   1045  1.1.2.2  yamt 	TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
   1046  1.1.2.2  yamt 		if (handle == link->hl_handle)
   1047  1.1.2.2  yamt 			break;
   1048  1.1.2.2  yamt 	}
   1049  1.1.2.2  yamt 
   1050  1.1.2.2  yamt 	return link;
   1051  1.1.2.2  yamt }
   1052