Home | History | Annotate | Line # | Download | only in netbt
hci_event.c revision 1.7.8.2
      1  1.7.8.2  plunky /*	$NetBSD: hci_event.c,v 1.7.8.2 2007/07/19 20:48:52 plunky Exp $	*/
      2  1.7.8.2  plunky 
      3  1.7.8.2  plunky /*-
      4  1.7.8.2  plunky  * Copyright (c) 2005 Iain Hibbert.
      5  1.7.8.2  plunky  * Copyright (c) 2006 Itronix Inc.
      6  1.7.8.2  plunky  * All rights reserved.
      7  1.7.8.2  plunky  *
      8  1.7.8.2  plunky  * Redistribution and use in source and binary forms, with or without
      9  1.7.8.2  plunky  * modification, are permitted provided that the following conditions
     10  1.7.8.2  plunky  * are met:
     11  1.7.8.2  plunky  * 1. Redistributions of source code must retain the above copyright
     12  1.7.8.2  plunky  *    notice, this list of conditions and the following disclaimer.
     13  1.7.8.2  plunky  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.7.8.2  plunky  *    notice, this list of conditions and the following disclaimer in the
     15  1.7.8.2  plunky  *    documentation and/or other materials provided with the distribution.
     16  1.7.8.2  plunky  * 3. The name of Itronix Inc. may not be used to endorse
     17  1.7.8.2  plunky  *    or promote products derived from this software without specific
     18  1.7.8.2  plunky  *    prior written permission.
     19  1.7.8.2  plunky  *
     20  1.7.8.2  plunky  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     21  1.7.8.2  plunky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.7.8.2  plunky  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.7.8.2  plunky  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     24  1.7.8.2  plunky  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     25  1.7.8.2  plunky  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     26  1.7.8.2  plunky  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     27  1.7.8.2  plunky  * ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.7.8.2  plunky  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.7.8.2  plunky  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.7.8.2  plunky  * POSSIBILITY OF SUCH DAMAGE.
     31  1.7.8.2  plunky  */
     32  1.7.8.2  plunky 
     33  1.7.8.2  plunky #include <sys/cdefs.h>
     34  1.7.8.2  plunky __KERNEL_RCSID(0, "$NetBSD: hci_event.c,v 1.7.8.2 2007/07/19 20:48:52 plunky Exp $");
     35  1.7.8.2  plunky 
     36  1.7.8.2  plunky #include <sys/param.h>
     37  1.7.8.2  plunky #include <sys/kernel.h>
     38  1.7.8.2  plunky #include <sys/malloc.h>
     39  1.7.8.2  plunky #include <sys/mbuf.h>
     40  1.7.8.2  plunky #include <sys/proc.h>
     41  1.7.8.2  plunky #include <sys/systm.h>
     42  1.7.8.2  plunky 
     43  1.7.8.2  plunky #include <netbt/bluetooth.h>
     44  1.7.8.2  plunky #include <netbt/hci.h>
     45  1.7.8.2  plunky #include <netbt/sco.h>
     46  1.7.8.2  plunky 
     47  1.7.8.2  plunky static void hci_event_inquiry_result(struct hci_unit *, struct mbuf *);
     48  1.7.8.2  plunky static void hci_event_command_status(struct hci_unit *, struct mbuf *);
     49  1.7.8.2  plunky static void hci_event_command_compl(struct hci_unit *, struct mbuf *);
     50  1.7.8.2  plunky static void hci_event_con_compl(struct hci_unit *, struct mbuf *);
     51  1.7.8.2  plunky static void hci_event_discon_compl(struct hci_unit *, struct mbuf *);
     52  1.7.8.2  plunky static void hci_event_con_req(struct hci_unit *, struct mbuf *);
     53  1.7.8.2  plunky static void hci_event_num_compl_pkts(struct hci_unit *, struct mbuf *);
     54  1.7.8.2  plunky static void hci_event_auth_compl(struct hci_unit *, struct mbuf *);
     55  1.7.8.2  plunky static void hci_event_encryption_change(struct hci_unit *, struct mbuf *);
     56  1.7.8.2  plunky static void hci_event_change_con_link_key_compl(struct hci_unit *, struct mbuf *);
     57  1.7.8.2  plunky static void hci_cmd_read_bdaddr(struct hci_unit *, struct mbuf *);
     58  1.7.8.2  plunky static void hci_cmd_read_buffer_size(struct hci_unit *, struct mbuf *);
     59  1.7.8.2  plunky static void hci_cmd_read_local_features(struct hci_unit *, struct mbuf *);
     60  1.7.8.2  plunky static void hci_cmd_reset(struct hci_unit *, struct mbuf *);
     61  1.7.8.2  plunky 
     62  1.7.8.2  plunky #ifdef BLUETOOTH_DEBUG
     63  1.7.8.2  plunky int bluetooth_debug;
     64  1.7.8.2  plunky 
     65  1.7.8.2  plunky static const char *hci_eventnames[] = {
     66  1.7.8.2  plunky /* 0x00 */ "NULL",
     67  1.7.8.2  plunky /* 0x01 */ "INQUIRY COMPLETE",
     68  1.7.8.2  plunky /* 0x02 */ "INQUIRY RESULT",
     69  1.7.8.2  plunky /* 0x03 */ "CONN COMPLETE",
     70  1.7.8.2  plunky /* 0x04 */ "CONN REQ",
     71  1.7.8.2  plunky /* 0x05 */ "DISCONN COMPLETE",
     72  1.7.8.2  plunky /* 0x06 */ "AUTH COMPLETE",
     73  1.7.8.2  plunky /* 0x07 */ "REMOTE NAME REQ COMPLETE",
     74  1.7.8.2  plunky /* 0x08 */ "ENCRYPTION CHANGE",
     75  1.7.8.2  plunky /* 0x09 */ "CHANGE CONN LINK KEY COMPLETE",
     76  1.7.8.2  plunky /* 0x0a */ "MASTER LINK KEY COMPLETE",
     77  1.7.8.2  plunky /* 0x0b */ "READ REMOTE FEATURES COMPLETE",
     78  1.7.8.2  plunky /* 0x0c */ "READ REMOTE VERSION INFO COMPLETE",
     79  1.7.8.2  plunky /* 0x0d */ "QoS SETUP COMPLETE",
     80  1.7.8.2  plunky /* 0x0e */ "COMMAND COMPLETE",
     81  1.7.8.2  plunky /* 0x0f */ "COMMAND STATUS",
     82  1.7.8.2  plunky /* 0x10 */ "HARDWARE ERROR",
     83  1.7.8.2  plunky /* 0x11 */ "FLUSH OCCUR",
     84  1.7.8.2  plunky /* 0x12 */ "ROLE CHANGE",
     85  1.7.8.2  plunky /* 0x13 */ "NUM COMPLETED PACKETS",
     86  1.7.8.2  plunky /* 0x14 */ "MODE CHANGE",
     87  1.7.8.2  plunky /* 0x15 */ "RETURN LINK KEYS",
     88  1.7.8.2  plunky /* 0x16 */ "PIN CODE REQ",
     89  1.7.8.2  plunky /* 0x17 */ "LINK KEY REQ",
     90  1.7.8.2  plunky /* 0x18 */ "LINK KEY NOTIFICATION",
     91  1.7.8.2  plunky /* 0x19 */ "LOOPBACK COMMAND",
     92  1.7.8.2  plunky /* 0x1a */ "DATA BUFFER OVERFLOW",
     93  1.7.8.2  plunky /* 0x1b */ "MAX SLOT CHANGE",
     94  1.7.8.2  plunky /* 0x1c */ "READ CLOCK OFFSET COMPLETE",
     95  1.7.8.2  plunky /* 0x1d */ "CONN PKT TYPE CHANGED",
     96  1.7.8.2  plunky /* 0x1e */ "QOS VIOLATION",
     97  1.7.8.2  plunky /* 0x1f */ "PAGE SCAN MODE CHANGE",
     98  1.7.8.2  plunky /* 0x20 */ "PAGE SCAN REP MODE CHANGE",
     99  1.7.8.2  plunky /* 0x21 */ "FLOW SPECIFICATION COMPLETE",
    100  1.7.8.2  plunky /* 0x22 */ "RSSI RESULT",
    101  1.7.8.2  plunky /* 0x23 */ "READ REMOTE EXT FEATURES"
    102  1.7.8.2  plunky };
    103  1.7.8.2  plunky 
    104  1.7.8.2  plunky static const char *
    105  1.7.8.2  plunky hci_eventstr(unsigned int event)
    106  1.7.8.2  plunky {
    107  1.7.8.2  plunky 
    108  1.7.8.2  plunky 	if (event < (sizeof(hci_eventnames) / sizeof(*hci_eventnames)))
    109  1.7.8.2  plunky 		return hci_eventnames[event];
    110  1.7.8.2  plunky 
    111  1.7.8.2  plunky 	switch (event) {
    112  1.7.8.2  plunky 	case HCI_EVENT_SCO_CON_COMPL:	/* 0x2c */
    113  1.7.8.2  plunky 		return "SCO CON COMPLETE";
    114  1.7.8.2  plunky 
    115  1.7.8.2  plunky 	case HCI_EVENT_SCO_CON_CHANGED:	/* 0x2d */
    116  1.7.8.2  plunky 		return "SCO CON CHANGED";
    117  1.7.8.2  plunky 
    118  1.7.8.2  plunky 	case HCI_EVENT_BT_LOGO:		/* 0xfe */
    119  1.7.8.2  plunky 		return "BT_LOGO";
    120  1.7.8.2  plunky 
    121  1.7.8.2  plunky 	case HCI_EVENT_VENDOR:		/* 0xff */
    122  1.7.8.2  plunky 		return "VENDOR";
    123  1.7.8.2  plunky 	}
    124  1.7.8.2  plunky 
    125  1.7.8.2  plunky 	return "UNRECOGNISED";
    126  1.7.8.2  plunky }
    127  1.7.8.2  plunky #endif	/* BLUETOOTH_DEBUG */
    128  1.7.8.2  plunky 
    129  1.7.8.2  plunky /*
    130  1.7.8.2  plunky  * process HCI Events
    131  1.7.8.2  plunky  *
    132  1.7.8.2  plunky  * We will free the mbuf at the end, no need for any sub
    133  1.7.8.2  plunky  * functions to handle that. We kind of assume that the
    134  1.7.8.2  plunky  * device sends us valid events.
    135  1.7.8.2  plunky  */
    136  1.7.8.2  plunky void
    137  1.7.8.2  plunky hci_event(struct mbuf *m, struct hci_unit *unit)
    138  1.7.8.2  plunky {
    139  1.7.8.2  plunky 	hci_event_hdr_t hdr;
    140  1.7.8.2  plunky 
    141  1.7.8.2  plunky 	KASSERT(m->m_flags & M_PKTHDR);
    142  1.7.8.2  plunky 
    143  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(hdr));
    144  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(hdr), &hdr);
    145  1.7.8.2  plunky 	m_adj(m, sizeof(hdr));
    146  1.7.8.2  plunky 
    147  1.7.8.2  plunky 	KASSERT(hdr.type == HCI_EVENT_PKT);
    148  1.7.8.2  plunky 
    149  1.7.8.2  plunky 	DPRINTFN(1, "(%s) event %s\n", unit->hci_devname, hci_eventstr(hdr.event));
    150  1.7.8.2  plunky 
    151  1.7.8.2  plunky 	switch(hdr.event) {
    152  1.7.8.2  plunky 	case HCI_EVENT_COMMAND_STATUS:
    153  1.7.8.2  plunky 		hci_event_command_status(unit, m);
    154  1.7.8.2  plunky 		break;
    155  1.7.8.2  plunky 
    156  1.7.8.2  plunky 	case HCI_EVENT_COMMAND_COMPL:
    157  1.7.8.2  plunky 		hci_event_command_compl(unit, m);
    158  1.7.8.2  plunky 		break;
    159  1.7.8.2  plunky 
    160  1.7.8.2  plunky 	case HCI_EVENT_NUM_COMPL_PKTS:
    161  1.7.8.2  plunky 		hci_event_num_compl_pkts(unit, m);
    162  1.7.8.2  plunky 		break;
    163  1.7.8.2  plunky 
    164  1.7.8.2  plunky 	case HCI_EVENT_INQUIRY_RESULT:
    165  1.7.8.2  plunky 		hci_event_inquiry_result(unit, m);
    166  1.7.8.2  plunky 		break;
    167  1.7.8.2  plunky 
    168  1.7.8.2  plunky 	case HCI_EVENT_CON_COMPL:
    169  1.7.8.2  plunky 		hci_event_con_compl(unit, m);
    170  1.7.8.2  plunky 		break;
    171  1.7.8.2  plunky 
    172  1.7.8.2  plunky 	case HCI_EVENT_DISCON_COMPL:
    173  1.7.8.2  plunky 		hci_event_discon_compl(unit, m);
    174  1.7.8.2  plunky 		break;
    175  1.7.8.2  plunky 
    176  1.7.8.2  plunky 	case HCI_EVENT_CON_REQ:
    177  1.7.8.2  plunky 		hci_event_con_req(unit, m);
    178  1.7.8.2  plunky 		break;
    179  1.7.8.2  plunky 
    180  1.7.8.2  plunky 	case HCI_EVENT_AUTH_COMPL:
    181  1.7.8.2  plunky 		hci_event_auth_compl(unit, m);
    182  1.7.8.2  plunky 		break;
    183  1.7.8.2  plunky 
    184  1.7.8.2  plunky 	case HCI_EVENT_ENCRYPTION_CHANGE:
    185  1.7.8.2  plunky 		hci_event_encryption_change(unit, m);
    186  1.7.8.2  plunky 		break;
    187  1.7.8.2  plunky 
    188  1.7.8.2  plunky 	case HCI_EVENT_CHANGE_CON_LINK_KEY_COMPL:
    189  1.7.8.2  plunky 		hci_event_change_con_link_key_compl(unit, m);
    190  1.7.8.2  plunky 		break;
    191  1.7.8.2  plunky 
    192  1.7.8.2  plunky 	case HCI_EVENT_SCO_CON_COMPL:
    193  1.7.8.2  plunky 	case HCI_EVENT_INQUIRY_COMPL:
    194  1.7.8.2  plunky 	case HCI_EVENT_REMOTE_NAME_REQ_COMPL:
    195  1.7.8.2  plunky 	case HCI_EVENT_MASTER_LINK_KEY_COMPL:
    196  1.7.8.2  plunky 	case HCI_EVENT_READ_REMOTE_FEATURES_COMPL:
    197  1.7.8.2  plunky 	case HCI_EVENT_READ_REMOTE_VER_INFO_COMPL:
    198  1.7.8.2  plunky 	case HCI_EVENT_QOS_SETUP_COMPL:
    199  1.7.8.2  plunky 	case HCI_EVENT_HARDWARE_ERROR:
    200  1.7.8.2  plunky 	case HCI_EVENT_FLUSH_OCCUR:
    201  1.7.8.2  plunky 	case HCI_EVENT_ROLE_CHANGE:
    202  1.7.8.2  plunky 	case HCI_EVENT_MODE_CHANGE:
    203  1.7.8.2  plunky 	case HCI_EVENT_RETURN_LINK_KEYS:
    204  1.7.8.2  plunky 	case HCI_EVENT_PIN_CODE_REQ:
    205  1.7.8.2  plunky 	case HCI_EVENT_LINK_KEY_REQ:
    206  1.7.8.2  plunky 	case HCI_EVENT_LINK_KEY_NOTIFICATION:
    207  1.7.8.2  plunky 	case HCI_EVENT_LOOPBACK_COMMAND:
    208  1.7.8.2  plunky 	case HCI_EVENT_DATA_BUFFER_OVERFLOW:
    209  1.7.8.2  plunky 	case HCI_EVENT_MAX_SLOT_CHANGE:
    210  1.7.8.2  plunky 	case HCI_EVENT_READ_CLOCK_OFFSET_COMPL:
    211  1.7.8.2  plunky 	case HCI_EVENT_CON_PKT_TYPE_CHANGED:
    212  1.7.8.2  plunky 	case HCI_EVENT_QOS_VIOLATION:
    213  1.7.8.2  plunky 	case HCI_EVENT_PAGE_SCAN_MODE_CHANGE:
    214  1.7.8.2  plunky 	case HCI_EVENT_PAGE_SCAN_REP_MODE_CHANGE:
    215  1.7.8.2  plunky 	case HCI_EVENT_FLOW_SPECIFICATION_COMPL:
    216  1.7.8.2  plunky 	case HCI_EVENT_RSSI_RESULT:
    217  1.7.8.2  plunky 	case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES:
    218  1.7.8.2  plunky 	case HCI_EVENT_SCO_CON_CHANGED:
    219  1.7.8.2  plunky 	case HCI_EVENT_BT_LOGO:
    220  1.7.8.2  plunky 	case HCI_EVENT_VENDOR:
    221  1.7.8.2  plunky 		break;
    222  1.7.8.2  plunky 
    223  1.7.8.2  plunky 	default:
    224  1.7.8.2  plunky 		UNKNOWN(hdr.event);
    225  1.7.8.2  plunky 		break;
    226  1.7.8.2  plunky 	}
    227  1.7.8.2  plunky 
    228  1.7.8.2  plunky 	m_freem(m);
    229  1.7.8.2  plunky }
    230  1.7.8.2  plunky 
    231  1.7.8.2  plunky /*
    232  1.7.8.2  plunky  * Command Status
    233  1.7.8.2  plunky  *
    234  1.7.8.2  plunky  * Update our record of num_cmd_pkts then post-process any pending commands
    235  1.7.8.2  plunky  * and optionally restart cmd output on the unit.
    236  1.7.8.2  plunky  */
    237  1.7.8.2  plunky static void
    238  1.7.8.2  plunky hci_event_command_status(struct hci_unit *unit, struct mbuf *m)
    239  1.7.8.2  plunky {
    240  1.7.8.2  plunky 	hci_command_status_ep ep;
    241  1.7.8.2  plunky 
    242  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(ep));
    243  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(ep), &ep);
    244  1.7.8.2  plunky 	m_adj(m, sizeof(ep));
    245  1.7.8.2  plunky 
    246  1.7.8.2  plunky 	DPRINTFN(1, "(%s) opcode (%03x|%04x) status = 0x%x num_cmd_pkts = %d\n",
    247  1.7.8.2  plunky 		unit->hci_devname,
    248  1.7.8.2  plunky 		HCI_OGF(le16toh(ep.opcode)), HCI_OCF(le16toh(ep.opcode)),
    249  1.7.8.2  plunky 		ep.status,
    250  1.7.8.2  plunky 		ep.num_cmd_pkts);
    251  1.7.8.2  plunky 
    252  1.7.8.2  plunky 	unit->hci_num_cmd_pkts = ep.num_cmd_pkts;
    253  1.7.8.2  plunky 
    254  1.7.8.2  plunky 	/*
    255  1.7.8.2  plunky 	 * post processing of pending commands
    256  1.7.8.2  plunky 	 */
    257  1.7.8.2  plunky 	switch(le16toh(ep.opcode)) {
    258  1.7.8.2  plunky 	default:
    259  1.7.8.2  plunky 		break;
    260  1.7.8.2  plunky 	}
    261  1.7.8.2  plunky 
    262  1.7.8.2  plunky 	while (unit->hci_num_cmd_pkts > 0 && MBUFQ_FIRST(&unit->hci_cmdwait)) {
    263  1.7.8.2  plunky 		MBUFQ_DEQUEUE(&unit->hci_cmdwait, m);
    264  1.7.8.2  plunky 		hci_output_cmd(unit, m);
    265  1.7.8.2  plunky 	}
    266  1.7.8.2  plunky }
    267  1.7.8.2  plunky 
    268  1.7.8.2  plunky /*
    269  1.7.8.2  plunky  * Command Complete
    270  1.7.8.2  plunky  *
    271  1.7.8.2  plunky  * Update our record of num_cmd_pkts then handle the completed command,
    272  1.7.8.2  plunky  * and optionally restart cmd output on the unit.
    273  1.7.8.2  plunky  */
    274  1.7.8.2  plunky static void
    275  1.7.8.2  plunky hci_event_command_compl(struct hci_unit *unit, struct mbuf *m)
    276  1.7.8.2  plunky {
    277  1.7.8.2  plunky 	hci_command_compl_ep ep;
    278  1.7.8.2  plunky 
    279  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(ep));
    280  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(ep), &ep);
    281  1.7.8.2  plunky 	m_adj(m, sizeof(ep));
    282  1.7.8.2  plunky 
    283  1.7.8.2  plunky 	DPRINTFN(1, "(%s) opcode (%03x|%04x) num_cmd_pkts = %d\n",
    284  1.7.8.2  plunky 		unit->hci_devname,
    285  1.7.8.2  plunky 		HCI_OGF(le16toh(ep.opcode)), HCI_OCF(le16toh(ep.opcode)),
    286  1.7.8.2  plunky 		ep.num_cmd_pkts);
    287  1.7.8.2  plunky 
    288  1.7.8.2  plunky 	unit->hci_num_cmd_pkts = ep.num_cmd_pkts;
    289  1.7.8.2  plunky 
    290  1.7.8.2  plunky 	/*
    291  1.7.8.2  plunky 	 * post processing of completed commands
    292  1.7.8.2  plunky 	 */
    293  1.7.8.2  plunky 	switch(le16toh(ep.opcode)) {
    294  1.7.8.2  plunky 	case HCI_CMD_READ_BDADDR:
    295  1.7.8.2  plunky 		hci_cmd_read_bdaddr(unit, m);
    296  1.7.8.2  plunky 		break;
    297  1.7.8.2  plunky 
    298  1.7.8.2  plunky 	case HCI_CMD_READ_BUFFER_SIZE:
    299  1.7.8.2  plunky 		hci_cmd_read_buffer_size(unit, m);
    300  1.7.8.2  plunky 		break;
    301  1.7.8.2  plunky 
    302  1.7.8.2  plunky 	case HCI_CMD_READ_LOCAL_FEATURES:
    303  1.7.8.2  plunky 		hci_cmd_read_local_features(unit, m);
    304  1.7.8.2  plunky 		break;
    305  1.7.8.2  plunky 
    306  1.7.8.2  plunky 	case HCI_CMD_RESET:
    307  1.7.8.2  plunky 		hci_cmd_reset(unit, m);
    308  1.7.8.2  plunky 		break;
    309  1.7.8.2  plunky 
    310  1.7.8.2  plunky 	default:
    311  1.7.8.2  plunky 		break;
    312  1.7.8.2  plunky 	}
    313  1.7.8.2  plunky 
    314  1.7.8.2  plunky 	while (unit->hci_num_cmd_pkts > 0 && MBUFQ_FIRST(&unit->hci_cmdwait)) {
    315  1.7.8.2  plunky 		MBUFQ_DEQUEUE(&unit->hci_cmdwait, m);
    316  1.7.8.2  plunky 		hci_output_cmd(unit, m);
    317  1.7.8.2  plunky 	}
    318  1.7.8.2  plunky }
    319  1.7.8.2  plunky 
    320  1.7.8.2  plunky /*
    321  1.7.8.2  plunky  * Number of Completed Packets
    322  1.7.8.2  plunky  *
    323  1.7.8.2  plunky  * This is sent periodically by the Controller telling us how many
    324  1.7.8.2  plunky  * buffers are now freed up and which handle was using them. From
    325  1.7.8.2  plunky  * this we determine which type of buffer it was and add the qty
    326  1.7.8.2  plunky  * back into the relevant packet counter, then restart output on
    327  1.7.8.2  plunky  * links that have halted.
    328  1.7.8.2  plunky  */
    329  1.7.8.2  plunky static void
    330  1.7.8.2  plunky hci_event_num_compl_pkts(struct hci_unit *unit, struct mbuf *m)
    331  1.7.8.2  plunky {
    332  1.7.8.2  plunky 	hci_num_compl_pkts_ep ep;
    333  1.7.8.2  plunky 	struct hci_link *link, *next;
    334  1.7.8.2  plunky 	uint16_t handle, num;
    335  1.7.8.2  plunky 	int num_acl = 0, num_sco = 0;
    336  1.7.8.2  plunky 
    337  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(ep));
    338  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(ep), &ep);
    339  1.7.8.2  plunky 	m_adj(m, sizeof(ep));
    340  1.7.8.2  plunky 
    341  1.7.8.2  plunky 	while (ep.num_con_handles--) {
    342  1.7.8.2  plunky 		m_copydata(m, 0, sizeof(handle), &handle);
    343  1.7.8.2  plunky 		m_adj(m, sizeof(handle));
    344  1.7.8.2  plunky 		handle = le16toh(handle);
    345  1.7.8.2  plunky 
    346  1.7.8.2  plunky 		m_copydata(m, 0, sizeof(num), &num);
    347  1.7.8.2  plunky 		m_adj(m, sizeof(num));
    348  1.7.8.2  plunky 		num = le16toh(num);
    349  1.7.8.2  plunky 
    350  1.7.8.2  plunky 		link = hci_link_lookup_handle(unit, handle);
    351  1.7.8.2  plunky 		if (link) {
    352  1.7.8.2  plunky 			if (link->hl_type == HCI_LINK_ACL) {
    353  1.7.8.2  plunky 				num_acl += num;
    354  1.7.8.2  plunky 				hci_acl_complete(link, num);
    355  1.7.8.2  plunky 			} else {
    356  1.7.8.2  plunky 				num_sco += num;
    357  1.7.8.2  plunky 				hci_sco_complete(link, num);
    358  1.7.8.2  plunky 			}
    359  1.7.8.2  plunky 		} else {
    360  1.7.8.2  plunky 			/* XXX need to issue Read_Buffer_Size or Reset? */
    361  1.7.8.2  plunky 			printf("%s: unknown handle %d! "
    362  1.7.8.2  plunky 				"(losing track of %d packet buffer%s)\n",
    363  1.7.8.2  plunky 				unit->hci_devname, handle,
    364  1.7.8.2  plunky 				num, (num == 1 ? "" : "s"));
    365  1.7.8.2  plunky 		}
    366  1.7.8.2  plunky 	}
    367  1.7.8.2  plunky 
    368  1.7.8.2  plunky 	/*
    369  1.7.8.2  plunky 	 * Move up any queued packets. When a link has sent data, it will move
    370  1.7.8.2  plunky 	 * to the back of the queue - technically then if a link had something
    371  1.7.8.2  plunky 	 * to send and there were still buffers available it could get started
    372  1.7.8.2  plunky 	 * twice but it seemed more important to to handle higher loads fairly
    373  1.7.8.2  plunky 	 * than worry about wasting cycles when we are not busy.
    374  1.7.8.2  plunky 	 */
    375  1.7.8.2  plunky 
    376  1.7.8.2  plunky 	unit->hci_num_acl_pkts += num_acl;
    377  1.7.8.2  plunky 	unit->hci_num_sco_pkts += num_sco;
    378  1.7.8.2  plunky 
    379  1.7.8.2  plunky 	link = TAILQ_FIRST(&unit->hci_links);
    380  1.7.8.2  plunky 	while (link && (unit->hci_num_acl_pkts > 0 || unit->hci_num_sco_pkts > 0)) {
    381  1.7.8.2  plunky 		next = TAILQ_NEXT(link, hl_next);
    382  1.7.8.2  plunky 
    383  1.7.8.2  plunky 		if (link->hl_type == HCI_LINK_ACL) {
    384  1.7.8.2  plunky 			if (unit->hci_num_acl_pkts > 0 && link->hl_txqlen > 0)
    385  1.7.8.2  plunky 				hci_acl_start(link);
    386  1.7.8.2  plunky 		} else {
    387  1.7.8.2  plunky 			if (unit->hci_num_sco_pkts > 0 && link->hl_txqlen > 0)
    388  1.7.8.2  plunky 				hci_sco_start(link);
    389  1.7.8.2  plunky 		}
    390  1.7.8.2  plunky 
    391  1.7.8.2  plunky 		link = next;
    392  1.7.8.2  plunky 	}
    393  1.7.8.2  plunky }
    394  1.7.8.2  plunky 
    395  1.7.8.2  plunky /*
    396  1.7.8.2  plunky  * Inquiry Result
    397  1.7.8.2  plunky  *
    398  1.7.8.2  plunky  * keep a note of devices seen, so we know which unit to use
    399  1.7.8.2  plunky  * on outgoing connections
    400  1.7.8.2  plunky  */
    401  1.7.8.2  plunky static void
    402  1.7.8.2  plunky hci_event_inquiry_result(struct hci_unit *unit, struct mbuf *m)
    403  1.7.8.2  plunky {
    404  1.7.8.2  plunky 	hci_inquiry_result_ep ep;
    405  1.7.8.2  plunky 	struct hci_memo *memo;
    406  1.7.8.2  plunky 	bdaddr_t bdaddr;
    407  1.7.8.2  plunky 
    408  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(ep));
    409  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(ep), &ep);
    410  1.7.8.2  plunky 	m_adj(m, sizeof(ep));
    411  1.7.8.2  plunky 
    412  1.7.8.2  plunky 	DPRINTFN(1, "%d response%s\n", ep.num_responses,
    413  1.7.8.2  plunky 				(ep.num_responses == 1 ? "" : "s"));
    414  1.7.8.2  plunky 
    415  1.7.8.2  plunky 	while(ep.num_responses--) {
    416  1.7.8.2  plunky 		m_copydata(m, 0, sizeof(bdaddr_t), &bdaddr);
    417  1.7.8.2  plunky 
    418  1.7.8.2  plunky 		DPRINTFN(1, "bdaddr %02x:%02x:%02x:%02x:%02x:%02x\n",
    419  1.7.8.2  plunky 			bdaddr.b[5], bdaddr.b[4], bdaddr.b[3],
    420  1.7.8.2  plunky 			bdaddr.b[2], bdaddr.b[1], bdaddr.b[0]);
    421  1.7.8.2  plunky 
    422  1.7.8.2  plunky 		memo = hci_memo_find(unit, &bdaddr);
    423  1.7.8.2  plunky 		if (memo == NULL) {
    424  1.7.8.2  plunky 			memo = malloc(sizeof(struct hci_memo),
    425  1.7.8.2  plunky 				M_BLUETOOTH, M_NOWAIT | M_ZERO);
    426  1.7.8.2  plunky 			if (memo == NULL) {
    427  1.7.8.2  plunky 				DPRINTFN(0, "out of memo memory!\n");
    428  1.7.8.2  plunky 				break;
    429  1.7.8.2  plunky 			}
    430  1.7.8.2  plunky 
    431  1.7.8.2  plunky 			LIST_INSERT_HEAD(&unit->hci_memos, memo, next);
    432  1.7.8.2  plunky 		}
    433  1.7.8.2  plunky 
    434  1.7.8.2  plunky 		microtime(&memo->time);
    435  1.7.8.2  plunky 		m_copydata(m, 0, sizeof(hci_inquiry_response), &memo->response);
    436  1.7.8.2  plunky 		m_adj(m, sizeof(hci_inquiry_response));
    437  1.7.8.2  plunky 
    438  1.7.8.2  plunky 		memo->response.clock_offset =
    439  1.7.8.2  plunky 		    le16toh(memo->response.clock_offset);
    440  1.7.8.2  plunky 	}
    441  1.7.8.2  plunky }
    442  1.7.8.2  plunky 
    443  1.7.8.2  plunky /*
    444  1.7.8.2  plunky  * Connection Complete
    445  1.7.8.2  plunky  *
    446  1.7.8.2  plunky  * Sent to us when a connection is made. If there is no link
    447  1.7.8.2  plunky  * structure already allocated for this, we must have changed
    448  1.7.8.2  plunky  * our mind, so just disconnect.
    449  1.7.8.2  plunky  */
    450  1.7.8.2  plunky static void
    451  1.7.8.2  plunky hci_event_con_compl(struct hci_unit *unit, struct mbuf *m)
    452  1.7.8.2  plunky {
    453  1.7.8.2  plunky 	hci_con_compl_ep ep;
    454  1.7.8.2  plunky 	hci_write_link_policy_settings_cp cp;
    455  1.7.8.2  plunky 	struct hci_link *link;
    456  1.7.8.2  plunky 	int err;
    457  1.7.8.2  plunky 
    458  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(ep));
    459  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(ep), &ep);
    460  1.7.8.2  plunky 	m_adj(m, sizeof(ep));
    461  1.7.8.2  plunky 
    462  1.7.8.2  plunky 	DPRINTFN(1, "(%s) %s connection complete for "
    463  1.7.8.2  plunky 		"%02x:%02x:%02x:%02x:%02x:%02x status %#x\n",
    464  1.7.8.2  plunky 		unit->hci_devname,
    465  1.7.8.2  plunky 		(ep.link_type == HCI_LINK_ACL ? "ACL" : "SCO"),
    466  1.7.8.2  plunky 		ep.bdaddr.b[5], ep.bdaddr.b[4], ep.bdaddr.b[3],
    467  1.7.8.2  plunky 		ep.bdaddr.b[2], ep.bdaddr.b[1], ep.bdaddr.b[0],
    468  1.7.8.2  plunky 		ep.status);
    469  1.7.8.2  plunky 
    470  1.7.8.2  plunky 	link = hci_link_lookup_bdaddr(unit, &ep.bdaddr, ep.link_type);
    471  1.7.8.2  plunky 
    472  1.7.8.2  plunky 	if (ep.status) {
    473  1.7.8.2  plunky 		if (link != NULL) {
    474  1.7.8.2  plunky 			switch (ep.status) {
    475  1.7.8.2  plunky 			case 0x04: /* "Page Timeout" */
    476  1.7.8.2  plunky 				err = EHOSTDOWN;
    477  1.7.8.2  plunky 				break;
    478  1.7.8.2  plunky 
    479  1.7.8.2  plunky 			case 0x08: /* "Connection Timed Out" */
    480  1.7.8.2  plunky 				err = ETIMEDOUT;
    481  1.7.8.2  plunky 				break;
    482  1.7.8.2  plunky 
    483  1.7.8.2  plunky 			case 0x16: /* "Connection Terminated by Local Host" */
    484  1.7.8.2  plunky 				err = 0;
    485  1.7.8.2  plunky 				break;
    486  1.7.8.2  plunky 
    487  1.7.8.2  plunky 			default:
    488  1.7.8.2  plunky 				err = ECONNREFUSED;
    489  1.7.8.2  plunky 				break;
    490  1.7.8.2  plunky 			}
    491  1.7.8.2  plunky 
    492  1.7.8.2  plunky 			hci_link_free(link, err);
    493  1.7.8.2  plunky 		}
    494  1.7.8.2  plunky 
    495  1.7.8.2  plunky 		return;
    496  1.7.8.2  plunky 	}
    497  1.7.8.2  plunky 
    498  1.7.8.2  plunky 	if (link == NULL) {
    499  1.7.8.2  plunky 		hci_discon_cp dp;
    500  1.7.8.2  plunky 
    501  1.7.8.2  plunky 		dp.con_handle = ep.con_handle;
    502  1.7.8.2  plunky 		dp.reason = 0x13; /* "Remote User Terminated Connection" */
    503  1.7.8.2  plunky 
    504  1.7.8.2  plunky 		hci_send_cmd(unit, HCI_CMD_DISCONNECT, &dp, sizeof(dp));
    505  1.7.8.2  plunky 		return;
    506  1.7.8.2  plunky 	}
    507  1.7.8.2  plunky 
    508  1.7.8.2  plunky 	/* XXX could check auth_enable here */
    509  1.7.8.2  plunky 
    510  1.7.8.2  plunky 	if (ep.encryption_mode)
    511  1.7.8.2  plunky 		link->hl_flags |= (HCI_LINK_AUTH | HCI_LINK_ENCRYPT);
    512  1.7.8.2  plunky 
    513  1.7.8.2  plunky 	link->hl_state = HCI_LINK_OPEN;
    514  1.7.8.2  plunky 	link->hl_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
    515  1.7.8.2  plunky 
    516  1.7.8.2  plunky 	if (ep.link_type == HCI_LINK_ACL) {
    517  1.7.8.2  plunky 		cp.con_handle = ep.con_handle;
    518  1.7.8.2  plunky 		cp.settings = htole16(unit->hci_link_policy);
    519  1.7.8.2  plunky 		err = hci_send_cmd(unit, HCI_CMD_WRITE_LINK_POLICY_SETTINGS,
    520  1.7.8.2  plunky 						&cp, sizeof(cp));
    521  1.7.8.2  plunky 		if (err)
    522  1.7.8.2  plunky 			printf("%s: Warning, could not write link policy\n",
    523  1.7.8.2  plunky 				unit->hci_devname);
    524  1.7.8.2  plunky 
    525  1.7.8.2  plunky 		err = hci_acl_setmode(link);
    526  1.7.8.2  plunky 		if (err == EINPROGRESS)
    527  1.7.8.2  plunky 			return;
    528  1.7.8.2  plunky 
    529  1.7.8.2  plunky 		hci_acl_linkmode(link);
    530  1.7.8.2  plunky 	} else {
    531  1.7.8.2  plunky 		(*link->hl_sco->sp_proto->connected)(link->hl_sco->sp_upper);
    532  1.7.8.2  plunky 	}
    533  1.7.8.2  plunky }
    534  1.7.8.2  plunky 
    535  1.7.8.2  plunky /*
    536  1.7.8.2  plunky  * Disconnection Complete
    537  1.7.8.2  plunky  *
    538  1.7.8.2  plunky  * This is sent in response to a disconnection request, but also if
    539  1.7.8.2  plunky  * the remote device goes out of range.
    540  1.7.8.2  plunky  */
    541  1.7.8.2  plunky static void
    542  1.7.8.2  plunky hci_event_discon_compl(struct hci_unit *unit, struct mbuf *m)
    543  1.7.8.2  plunky {
    544  1.7.8.2  plunky 	hci_discon_compl_ep ep;
    545  1.7.8.2  plunky 	struct hci_link *link;
    546  1.7.8.2  plunky 
    547  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(ep));
    548  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(ep), &ep);
    549  1.7.8.2  plunky 	m_adj(m, sizeof(ep));
    550  1.7.8.2  plunky 
    551  1.7.8.2  plunky 	ep.con_handle = le16toh(ep.con_handle);
    552  1.7.8.2  plunky 
    553  1.7.8.2  plunky 	DPRINTFN(1, "handle #%d, status=0x%x\n", ep.con_handle, ep.status);
    554  1.7.8.2  plunky 
    555  1.7.8.2  plunky 	link = hci_link_lookup_handle(unit, HCI_CON_HANDLE(ep.con_handle));
    556  1.7.8.2  plunky 	if (link)
    557  1.7.8.2  plunky 		hci_link_free(link, ENOLINK);
    558  1.7.8.2  plunky }
    559  1.7.8.2  plunky 
    560  1.7.8.2  plunky /*
    561  1.7.8.2  plunky  * Connect Request
    562  1.7.8.2  plunky  *
    563  1.7.8.2  plunky  * We check upstream for appropriate listeners and accept connections
    564  1.7.8.2  plunky  * that are wanted.
    565  1.7.8.2  plunky  */
    566  1.7.8.2  plunky static void
    567  1.7.8.2  plunky hci_event_con_req(struct hci_unit *unit, struct mbuf *m)
    568  1.7.8.2  plunky {
    569  1.7.8.2  plunky 	hci_con_req_ep ep;
    570  1.7.8.2  plunky 	hci_accept_con_cp ap;
    571  1.7.8.2  plunky 	hci_reject_con_cp rp;
    572  1.7.8.2  plunky 	struct hci_link *link;
    573  1.7.8.2  plunky 
    574  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(ep));
    575  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(ep), &ep);
    576  1.7.8.2  plunky 	m_adj(m, sizeof(ep));
    577  1.7.8.2  plunky 
    578  1.7.8.2  plunky 	DPRINTFN(1, "bdaddr %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x "
    579  1.7.8.2  plunky 		"class %2.2x%2.2x%2.2x type %s\n",
    580  1.7.8.2  plunky 		ep.bdaddr.b[5], ep.bdaddr.b[4], ep.bdaddr.b[3],
    581  1.7.8.2  plunky 		ep.bdaddr.b[2], ep.bdaddr.b[1], ep.bdaddr.b[0],
    582  1.7.8.2  plunky 		ep.uclass[0], ep.uclass[1], ep.uclass[2],
    583  1.7.8.2  plunky 		ep.link_type == HCI_LINK_ACL ? "ACL" : "SCO");
    584  1.7.8.2  plunky 
    585  1.7.8.2  plunky 	if (ep.link_type == HCI_LINK_ACL)
    586  1.7.8.2  plunky 		link = hci_acl_newconn(unit, &ep.bdaddr);
    587  1.7.8.2  plunky 	else
    588  1.7.8.2  plunky 		link = hci_sco_newconn(unit, &ep.bdaddr);
    589  1.7.8.2  plunky 
    590  1.7.8.2  plunky 	if (link == NULL) {
    591  1.7.8.2  plunky 		memset(&rp, 0, sizeof(rp));
    592  1.7.8.2  plunky 		bdaddr_copy(&rp.bdaddr, &ep.bdaddr);
    593  1.7.8.2  plunky 		rp.reason = 0x0f;	/* Unacceptable BD_ADDR */
    594  1.7.8.2  plunky 
    595  1.7.8.2  plunky 		hci_send_cmd(unit, HCI_CMD_REJECT_CON, &rp, sizeof(rp));
    596  1.7.8.2  plunky 	} else {
    597  1.7.8.2  plunky 		memset(&ap, 0, sizeof(ap));
    598  1.7.8.2  plunky 		bdaddr_copy(&ap.bdaddr, &ep.bdaddr);
    599  1.7.8.2  plunky 		if (unit->hci_link_policy & HCI_LINK_POLICY_ENABLE_ROLE_SWITCH)
    600  1.7.8.2  plunky 			ap.role = HCI_ROLE_MASTER;
    601  1.7.8.2  plunky 		else
    602  1.7.8.2  plunky 			ap.role = HCI_ROLE_SLAVE;
    603  1.7.8.2  plunky 
    604  1.7.8.2  plunky 		hci_send_cmd(unit, HCI_CMD_ACCEPT_CON, &ap, sizeof(ap));
    605  1.7.8.2  plunky 	}
    606  1.7.8.2  plunky }
    607  1.7.8.2  plunky 
    608  1.7.8.2  plunky /*
    609  1.7.8.2  plunky  * Auth Complete
    610  1.7.8.2  plunky  *
    611  1.7.8.2  plunky  * Authentication has been completed on an ACL link. We can notify the
    612  1.7.8.2  plunky  * upper layer protocols unless further mode changes are pending.
    613  1.7.8.2  plunky  */
    614  1.7.8.2  plunky static void
    615  1.7.8.2  plunky hci_event_auth_compl(struct hci_unit *unit, struct mbuf *m)
    616  1.7.8.2  plunky {
    617  1.7.8.2  plunky 	hci_auth_compl_ep ep;
    618  1.7.8.2  plunky 	struct hci_link *link;
    619  1.7.8.2  plunky 	int err;
    620  1.7.8.2  plunky 
    621  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(ep));
    622  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(ep), &ep);
    623  1.7.8.2  plunky 	m_adj(m, sizeof(ep));
    624  1.7.8.2  plunky 
    625  1.7.8.2  plunky 	ep.con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
    626  1.7.8.2  plunky 
    627  1.7.8.2  plunky 	DPRINTFN(1, "handle #%d, status=0x%x\n", ep.con_handle, ep.status);
    628  1.7.8.2  plunky 
    629  1.7.8.2  plunky 	link = hci_link_lookup_handle(unit, ep.con_handle);
    630  1.7.8.2  plunky 	if (link == NULL || link->hl_type != HCI_LINK_ACL)
    631  1.7.8.2  plunky 		return;
    632  1.7.8.2  plunky 
    633  1.7.8.2  plunky 	if (ep.status == 0) {
    634  1.7.8.2  plunky 		link->hl_flags |= HCI_LINK_AUTH;
    635  1.7.8.2  plunky 
    636  1.7.8.2  plunky 		if (link->hl_state == HCI_LINK_WAIT_AUTH)
    637  1.7.8.2  plunky 			link->hl_state = HCI_LINK_OPEN;
    638  1.7.8.2  plunky 
    639  1.7.8.2  plunky 		err = hci_acl_setmode(link);
    640  1.7.8.2  plunky 		if (err == EINPROGRESS)
    641  1.7.8.2  plunky 			return;
    642  1.7.8.2  plunky 	}
    643  1.7.8.2  plunky 
    644  1.7.8.2  plunky 	hci_acl_linkmode(link);
    645  1.7.8.2  plunky }
    646  1.7.8.2  plunky 
    647  1.7.8.2  plunky /*
    648  1.7.8.2  plunky  * Encryption Change
    649  1.7.8.2  plunky  *
    650  1.7.8.2  plunky  * The encryption status has changed. Basically, we note the change
    651  1.7.8.2  plunky  * then notify the upper layer protocol unless further mode changes
    652  1.7.8.2  plunky  * are pending.
    653  1.7.8.2  plunky  * Note that if encryption gets disabled when it has been requested,
    654  1.7.8.2  plunky  * we will attempt to enable it again.. (its a feature not a bug :)
    655  1.7.8.2  plunky  */
    656  1.7.8.2  plunky static void
    657  1.7.8.2  plunky hci_event_encryption_change(struct hci_unit *unit, struct mbuf *m)
    658  1.7.8.2  plunky {
    659  1.7.8.2  plunky 	hci_encryption_change_ep ep;
    660  1.7.8.2  plunky 	struct hci_link *link;
    661  1.7.8.2  plunky 	int err;
    662  1.7.8.2  plunky 
    663  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(ep));
    664  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(ep), &ep);
    665  1.7.8.2  plunky 	m_adj(m, sizeof(ep));
    666  1.7.8.2  plunky 
    667  1.7.8.2  plunky 	ep.con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
    668  1.7.8.2  plunky 
    669  1.7.8.2  plunky 	DPRINTFN(1, "handle #%d, status=0x%x, encryption_enable=0x%x\n",
    670  1.7.8.2  plunky 		 ep.con_handle, ep.status, ep.encryption_enable);
    671  1.7.8.2  plunky 
    672  1.7.8.2  plunky 	link = hci_link_lookup_handle(unit, ep.con_handle);
    673  1.7.8.2  plunky 	if (link == NULL || link->hl_type != HCI_LINK_ACL)
    674  1.7.8.2  plunky 		return;
    675  1.7.8.2  plunky 
    676  1.7.8.2  plunky 	if (ep.status == 0) {
    677  1.7.8.2  plunky 		if (ep.encryption_enable == 0)
    678  1.7.8.2  plunky 			link->hl_flags &= ~HCI_LINK_ENCRYPT;
    679  1.7.8.2  plunky 		else
    680  1.7.8.2  plunky 			link->hl_flags |= (HCI_LINK_AUTH | HCI_LINK_ENCRYPT);
    681  1.7.8.2  plunky 
    682  1.7.8.2  plunky 		if (link->hl_state == HCI_LINK_WAIT_ENCRYPT)
    683  1.7.8.2  plunky 			link->hl_state = HCI_LINK_OPEN;
    684  1.7.8.2  plunky 
    685  1.7.8.2  plunky 		err = hci_acl_setmode(link);
    686  1.7.8.2  plunky 		if (err == EINPROGRESS)
    687  1.7.8.2  plunky 			return;
    688  1.7.8.2  plunky 	}
    689  1.7.8.2  plunky 
    690  1.7.8.2  plunky 	hci_acl_linkmode(link);
    691  1.7.8.2  plunky }
    692  1.7.8.2  plunky 
    693  1.7.8.2  plunky /*
    694  1.7.8.2  plunky  * Change Connection Link Key Complete
    695  1.7.8.2  plunky  *
    696  1.7.8.2  plunky  * Link keys are handled in userland but if we are waiting to secure
    697  1.7.8.2  plunky  * this link, we should notify the upper protocols. A SECURE request
    698  1.7.8.2  plunky  * only needs a single key change, so we can cancel the request.
    699  1.7.8.2  plunky  */
    700  1.7.8.2  plunky static void
    701  1.7.8.2  plunky hci_event_change_con_link_key_compl(struct hci_unit *unit, struct mbuf *m)
    702  1.7.8.2  plunky {
    703  1.7.8.2  plunky 	hci_change_con_link_key_compl_ep ep;
    704  1.7.8.2  plunky 	struct hci_link *link;
    705  1.7.8.2  plunky 	int err;
    706  1.7.8.2  plunky 
    707  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(ep));
    708  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(ep), &ep);
    709  1.7.8.2  plunky 	m_adj(m, sizeof(ep));
    710  1.7.8.2  plunky 
    711  1.7.8.2  plunky 	ep.con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
    712  1.7.8.2  plunky 
    713  1.7.8.2  plunky 	DPRINTFN(1, "handle #%d, status=0x%x\n", ep.con_handle, ep.status);
    714  1.7.8.2  plunky 
    715  1.7.8.2  plunky 	link = hci_link_lookup_handle(unit, ep.con_handle);
    716  1.7.8.2  plunky 	if (link == NULL || link->hl_type != HCI_LINK_ACL)
    717  1.7.8.2  plunky 		return;
    718  1.7.8.2  plunky 
    719  1.7.8.2  plunky 	link->hl_flags &= ~HCI_LINK_SECURE_REQ;
    720  1.7.8.2  plunky 
    721  1.7.8.2  plunky 	if (ep.status == 0) {
    722  1.7.8.2  plunky 		link->hl_flags |= (HCI_LINK_AUTH | HCI_LINK_SECURE);
    723  1.7.8.2  plunky 
    724  1.7.8.2  plunky 		if (link->hl_state == HCI_LINK_WAIT_SECURE)
    725  1.7.8.2  plunky 			link->hl_state = HCI_LINK_OPEN;
    726  1.7.8.2  plunky 
    727  1.7.8.2  plunky 		err = hci_acl_setmode(link);
    728  1.7.8.2  plunky 		if (err == EINPROGRESS)
    729  1.7.8.2  plunky 			return;
    730  1.7.8.2  plunky 	}
    731  1.7.8.2  plunky 
    732  1.7.8.2  plunky 	hci_acl_linkmode(link);
    733  1.7.8.2  plunky }
    734  1.7.8.2  plunky 
    735  1.7.8.2  plunky /*
    736  1.7.8.2  plunky  * process results of read_bdaddr command_complete event
    737  1.7.8.2  plunky  */
    738  1.7.8.2  plunky static void
    739  1.7.8.2  plunky hci_cmd_read_bdaddr(struct hci_unit *unit, struct mbuf *m)
    740  1.7.8.2  plunky {
    741  1.7.8.2  plunky 	hci_read_bdaddr_rp rp;
    742  1.7.8.2  plunky 	int s;
    743  1.7.8.2  plunky 
    744  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(rp));
    745  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(rp), &rp);
    746  1.7.8.2  plunky 	m_adj(m, sizeof(rp));
    747  1.7.8.2  plunky 
    748  1.7.8.2  plunky 	if (rp.status > 0)
    749  1.7.8.2  plunky 		return;
    750  1.7.8.2  plunky 
    751  1.7.8.2  plunky 	if ((unit->hci_flags & BTF_INIT_BDADDR) == 0)
    752  1.7.8.2  plunky 		return;
    753  1.7.8.2  plunky 
    754  1.7.8.2  plunky 	bdaddr_copy(&unit->hci_bdaddr, &rp.bdaddr);
    755  1.7.8.2  plunky 
    756  1.7.8.2  plunky 	s = splraiseipl(unit->hci_ipl);
    757  1.7.8.2  plunky 	unit->hci_flags &= ~BTF_INIT_BDADDR;
    758  1.7.8.2  plunky 	splx(s);
    759  1.7.8.2  plunky 
    760  1.7.8.2  plunky 	wakeup(unit);
    761  1.7.8.2  plunky }
    762  1.7.8.2  plunky 
    763  1.7.8.2  plunky /*
    764  1.7.8.2  plunky  * process results of read_buffer_size command_complete event
    765  1.7.8.2  plunky  */
    766  1.7.8.2  plunky static void
    767  1.7.8.2  plunky hci_cmd_read_buffer_size(struct hci_unit *unit, struct mbuf *m)
    768  1.7.8.2  plunky {
    769  1.7.8.2  plunky 	hci_read_buffer_size_rp rp;
    770  1.7.8.2  plunky 	int s;
    771  1.7.8.2  plunky 
    772  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(rp));
    773  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(rp), &rp);
    774  1.7.8.2  plunky 	m_adj(m, sizeof(rp));
    775  1.7.8.2  plunky 
    776  1.7.8.2  plunky 	if (rp.status > 0)
    777  1.7.8.2  plunky 		return;
    778  1.7.8.2  plunky 
    779  1.7.8.2  plunky 	if ((unit->hci_flags & BTF_INIT_BUFFER_SIZE) == 0)
    780  1.7.8.2  plunky 		return;
    781  1.7.8.2  plunky 
    782  1.7.8.2  plunky 	unit->hci_max_acl_size = le16toh(rp.max_acl_size);
    783  1.7.8.2  plunky 	unit->hci_num_acl_pkts = le16toh(rp.num_acl_pkts);
    784  1.7.8.2  plunky 	unit->hci_max_sco_size = rp.max_sco_size;
    785  1.7.8.2  plunky 	unit->hci_num_sco_pkts = le16toh(rp.num_sco_pkts);
    786  1.7.8.2  plunky 
    787  1.7.8.2  plunky 	s = splraiseipl(unit->hci_ipl);
    788  1.7.8.2  plunky 	unit->hci_flags &= ~BTF_INIT_BUFFER_SIZE;
    789  1.7.8.2  plunky 	splx(s);
    790  1.7.8.2  plunky 
    791  1.7.8.2  plunky 	wakeup(unit);
    792  1.7.8.2  plunky }
    793  1.7.8.2  plunky 
    794  1.7.8.2  plunky /*
    795  1.7.8.2  plunky  * process results of read_local_features command_complete event
    796  1.7.8.2  plunky  */
    797  1.7.8.2  plunky static void
    798  1.7.8.2  plunky hci_cmd_read_local_features(struct hci_unit *unit, struct mbuf *m)
    799  1.7.8.2  plunky {
    800  1.7.8.2  plunky 	hci_read_local_features_rp rp;
    801  1.7.8.2  plunky 	int s;
    802  1.7.8.2  plunky 
    803  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(rp));
    804  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(rp), &rp);
    805  1.7.8.2  plunky 	m_adj(m, sizeof(rp));
    806  1.7.8.2  plunky 
    807  1.7.8.2  plunky 	if (rp.status > 0)
    808  1.7.8.2  plunky 		return;
    809  1.7.8.2  plunky 
    810  1.7.8.2  plunky 	if ((unit->hci_flags & BTF_INIT_FEATURES) == 0)
    811  1.7.8.2  plunky 		return;
    812  1.7.8.2  plunky 
    813  1.7.8.2  plunky 	unit->hci_lmp_mask = 0;
    814  1.7.8.2  plunky 
    815  1.7.8.2  plunky 	if (rp.features[0] & HCI_LMP_ROLE_SWITCH)
    816  1.7.8.2  plunky 		unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
    817  1.7.8.2  plunky 
    818  1.7.8.2  plunky 	if (rp.features[0] & HCI_LMP_HOLD_MODE)
    819  1.7.8.2  plunky 		unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_HOLD_MODE;
    820  1.7.8.2  plunky 
    821  1.7.8.2  plunky 	if (rp.features[0] & HCI_LMP_SNIFF_MODE)
    822  1.7.8.2  plunky 		unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
    823  1.7.8.2  plunky 
    824  1.7.8.2  plunky 	if (rp.features[1] & HCI_LMP_PARK_MODE)
    825  1.7.8.2  plunky 		unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_PARK_MODE;
    826  1.7.8.2  plunky 
    827  1.7.8.2  plunky 	/* ACL packet mask */
    828  1.7.8.2  plunky 	unit->hci_acl_mask = HCI_PKT_DM1 | HCI_PKT_DH1;
    829  1.7.8.2  plunky 
    830  1.7.8.2  plunky 	if (rp.features[0] & HCI_LMP_3SLOT)
    831  1.7.8.2  plunky 		unit->hci_acl_mask |= HCI_PKT_DM3 | HCI_PKT_DH3;
    832  1.7.8.2  plunky 
    833  1.7.8.2  plunky 	if (rp.features[0] & HCI_LMP_5SLOT)
    834  1.7.8.2  plunky 		unit->hci_acl_mask |= HCI_PKT_DM5 | HCI_PKT_DH5;
    835  1.7.8.2  plunky 
    836  1.7.8.2  plunky 	if ((rp.features[3] & HCI_LMP_EDR_ACL_2MBPS) == 0)
    837  1.7.8.2  plunky 		unit->hci_acl_mask |= HCI_PKT_2MBPS_DH1
    838  1.7.8.2  plunky 				    | HCI_PKT_2MBPS_DH3
    839  1.7.8.2  plunky 				    | HCI_PKT_2MBPS_DH5;
    840  1.7.8.2  plunky 
    841  1.7.8.2  plunky 	if ((rp.features[3] & HCI_LMP_EDR_ACL_3MBPS) == 0)
    842  1.7.8.2  plunky 		unit->hci_acl_mask |= HCI_PKT_3MBPS_DH1
    843  1.7.8.2  plunky 				    | HCI_PKT_3MBPS_DH3
    844  1.7.8.2  plunky 				    | HCI_PKT_3MBPS_DH5;
    845  1.7.8.2  plunky 
    846  1.7.8.2  plunky 	if ((rp.features[4] & HCI_LMP_3SLOT_EDR_ACL) == 0)
    847  1.7.8.2  plunky 		unit->hci_acl_mask |= HCI_PKT_2MBPS_DH3
    848  1.7.8.2  plunky 				    | HCI_PKT_3MBPS_DH3;
    849  1.7.8.2  plunky 
    850  1.7.8.2  plunky 	if ((rp.features[5] & HCI_LMP_5SLOT_EDR_ACL) == 0)
    851  1.7.8.2  plunky 		unit->hci_acl_mask |= HCI_PKT_2MBPS_DH5
    852  1.7.8.2  plunky 				    | HCI_PKT_3MBPS_DH5;
    853  1.7.8.2  plunky 
    854  1.7.8.2  plunky 	unit->hci_packet_type = unit->hci_acl_mask;
    855  1.7.8.2  plunky 
    856  1.7.8.2  plunky 	/* SCO packet mask */
    857  1.7.8.2  plunky 	unit->hci_sco_mask = 0;
    858  1.7.8.2  plunky 	if (rp.features[1] & HCI_LMP_SCO_LINK)
    859  1.7.8.2  plunky 		unit->hci_sco_mask |= HCI_PKT_HV1;
    860  1.7.8.2  plunky 
    861  1.7.8.2  plunky 	if (rp.features[1] & HCI_LMP_HV2_PKT)
    862  1.7.8.2  plunky 		unit->hci_sco_mask |= HCI_PKT_HV2;
    863  1.7.8.2  plunky 
    864  1.7.8.2  plunky 	if (rp.features[1] & HCI_LMP_HV3_PKT)
    865  1.7.8.2  plunky 		unit->hci_sco_mask |= HCI_PKT_HV3;
    866  1.7.8.2  plunky 
    867  1.7.8.2  plunky 	if (rp.features[3] & HCI_LMP_EV3_PKT)
    868  1.7.8.2  plunky 		unit->hci_sco_mask |= HCI_PKT_EV3;
    869  1.7.8.2  plunky 
    870  1.7.8.2  plunky 	if (rp.features[4] & HCI_LMP_EV4_PKT)
    871  1.7.8.2  plunky 		unit->hci_sco_mask |= HCI_PKT_EV4;
    872  1.7.8.2  plunky 
    873  1.7.8.2  plunky 	if (rp.features[4] & HCI_LMP_EV5_PKT)
    874  1.7.8.2  plunky 		unit->hci_sco_mask |= HCI_PKT_EV5;
    875  1.7.8.2  plunky 
    876  1.7.8.2  plunky 	/* XXX what do 2MBPS/3MBPS/3SLOT eSCO mean? */
    877  1.7.8.2  plunky 
    878  1.7.8.2  plunky 	s = splraiseipl(unit->hci_ipl);
    879  1.7.8.2  plunky 	unit->hci_flags &= ~BTF_INIT_FEATURES;
    880  1.7.8.2  plunky 	splx(s);
    881  1.7.8.2  plunky 
    882  1.7.8.2  plunky 	wakeup(unit);
    883  1.7.8.2  plunky 
    884  1.7.8.2  plunky 	DPRINTFN(1, "%s: lmp_mask %4.4x, acl_mask %4.4x, sco_mask %4.4x\n",
    885  1.7.8.2  plunky 		unit->hci_devname, unit->hci_lmp_mask,
    886  1.7.8.2  plunky 		unit->hci_acl_mask, unit->hci_sco_mask);
    887  1.7.8.2  plunky }
    888  1.7.8.2  plunky 
    889  1.7.8.2  plunky /*
    890  1.7.8.2  plunky  * process results of reset command_complete event
    891  1.7.8.2  plunky  *
    892  1.7.8.2  plunky  * This has killed all the connections, so close down anything we have left,
    893  1.7.8.2  plunky  * and reinitialise the unit.
    894  1.7.8.2  plunky  */
    895  1.7.8.2  plunky static void
    896  1.7.8.2  plunky hci_cmd_reset(struct hci_unit *unit, struct mbuf *m)
    897  1.7.8.2  plunky {
    898  1.7.8.2  plunky 	hci_reset_rp rp;
    899  1.7.8.2  plunky 	struct hci_link *link, *next;
    900  1.7.8.2  plunky 	int acl;
    901  1.7.8.2  plunky 
    902  1.7.8.2  plunky 	KASSERT(m->m_pkthdr.len >= sizeof(rp));
    903  1.7.8.2  plunky 	m_copydata(m, 0, sizeof(rp), &rp);
    904  1.7.8.2  plunky 	m_adj(m, sizeof(rp));
    905  1.7.8.2  plunky 
    906  1.7.8.2  plunky 	if (rp.status != 0)
    907  1.7.8.2  plunky 		return;
    908  1.7.8.2  plunky 
    909  1.7.8.2  plunky 	/*
    910  1.7.8.2  plunky 	 * release SCO links first, since they may be holding
    911  1.7.8.2  plunky 	 * an ACL link reference.
    912  1.7.8.2  plunky 	 */
    913  1.7.8.2  plunky 	for (acl = 0 ; acl < 2 ; acl++) {
    914  1.7.8.2  plunky 		next = TAILQ_FIRST(&unit->hci_links);
    915  1.7.8.2  plunky 		while ((link = next) != NULL) {
    916  1.7.8.2  plunky 			next = TAILQ_NEXT(link, hl_next);
    917  1.7.8.2  plunky 			if (acl || link->hl_type != HCI_LINK_ACL)
    918  1.7.8.2  plunky 				hci_link_free(link, ECONNABORTED);
    919  1.7.8.2  plunky 		}
    920  1.7.8.2  plunky 	}
    921  1.7.8.2  plunky 
    922  1.7.8.2  plunky 	unit->hci_num_acl_pkts = 0;
    923  1.7.8.2  plunky 	unit->hci_num_sco_pkts = 0;
    924  1.7.8.2  plunky 
    925  1.7.8.2  plunky 	if (hci_send_cmd(unit, HCI_CMD_READ_BDADDR, NULL, 0))
    926  1.7.8.2  plunky 		return;
    927  1.7.8.2  plunky 
    928  1.7.8.2  plunky 	if (hci_send_cmd(unit, HCI_CMD_READ_BUFFER_SIZE, NULL, 0))
    929  1.7.8.2  plunky 		return;
    930  1.7.8.2  plunky 
    931  1.7.8.2  plunky 	if (hci_send_cmd(unit, HCI_CMD_READ_LOCAL_FEATURES, NULL, 0))
    932  1.7.8.2  plunky 		return;
    933  1.7.8.2  plunky }
    934