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