#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: hci_event.c,v 1.26 2019/09/28 07:06:33 plunky Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <netbt/bluetooth.h>
#include <netbt/hci.h>
#include <netbt/sco.h>
static void hci_event_inquiry_result(struct hci_unit *, struct mbuf *);
static void hci_event_rssi_result(struct hci_unit *, struct mbuf *);
static void hci_event_extended_result(struct hci_unit *, struct mbuf *);
static void hci_event_command_status(struct hci_unit *, struct mbuf *);
static void hci_event_command_compl(struct hci_unit *, struct mbuf *);
static void hci_event_con_compl(struct hci_unit *, struct mbuf *);
static void hci_event_discon_compl(struct hci_unit *, struct mbuf *);
static void hci_event_con_req(struct hci_unit *, struct mbuf *);
static void hci_event_num_compl_pkts(struct hci_unit *, struct mbuf *);
static void hci_event_auth_compl(struct hci_unit *, struct mbuf *);
static void hci_event_encryption_change(struct hci_unit *, struct mbuf *);
static void hci_event_change_con_link_key_compl(struct hci_unit *, struct mbuf *);
static void hci_event_read_clock_offset_compl(struct hci_unit *, struct mbuf *);
static void hci_cmd_read_bdaddr(struct hci_unit *, struct mbuf *);
static void hci_cmd_read_buffer_size(struct hci_unit *, struct mbuf *);
static void hci_cmd_read_local_features(struct hci_unit *, struct mbuf *);
static void hci_cmd_read_local_extended_features(struct hci_unit *, struct mbuf *);
static void hci_cmd_read_local_ver(struct hci_unit *, struct mbuf *);
static void hci_cmd_read_local_commands(struct hci_unit *, struct mbuf *);
static void hci_cmd_read_encryption_key_size(struct hci_unit *, struct mbuf *);
static void hci_cmd_reset(struct hci_unit *, struct mbuf *);
static void hci_cmd_create_con(struct hci_unit *unit, uint8_t status);
#ifdef BLUETOOTH_DEBUG
int bluetooth_debug;
static const char *hci_eventnames[] = {
"NULL",
"INQUIRY COMPLETE",
"INQUIRY RESULT",
"CONN COMPLETE",
"CONN REQ",
"DISCONN COMPLETE",
"AUTH COMPLETE",
"REMOTE NAME REQ COMPLETE",
"ENCRYPTION CHANGE",
"CHANGE CONN LINK KEY COMPLETE",
"MASTER LINK KEY COMPLETE",
"READ REMOTE FEATURES COMPLETE",
"READ REMOTE VERSION INFO COMPLETE",
"QoS SETUP COMPLETE",
"COMMAND COMPLETE",
"COMMAND STATUS",
"HARDWARE ERROR",
"FLUSH OCCUR",
"ROLE CHANGE",
"NUM COMPLETED PACKETS",
"MODE CHANGE",
"RETURN LINK KEYS",
"PIN CODE REQ",
"LINK KEY REQ",
"LINK KEY NOTIFICATION",
"LOOPBACK COMMAND",
"DATA BUFFER OVERFLOW",
"MAX SLOT CHANGE",
"READ CLOCK OFFSET COMPLETE",
"CONN PKT TYPE CHANGED",
"QOS VIOLATION",
"PAGE SCAN MODE CHANGE",
"PAGE SCAN REP MODE CHANGE",
"FLOW SPECIFICATION COMPLETE",
"RSSI RESULT",
"READ REMOTE EXT FEATURES",
"UNKNOWN",
"UNKNOWN",
"UNKNOWN",
"UNKNOWN",
"UNKNOWN",
"UNKNOWN",
"UNKNOWN",
"UNKNOWN",
"SCO CON COMPLETE",
"SCO CON CHANGED",
"SNIFF SUBRATING",
"EXTENDED INQUIRY RESULT",
"ENCRYPTION KEY REFRESH",
"IO CAPABILITY REQUEST",
"IO CAPABILITY RESPONSE",
"USER CONFIRM REQUEST",
"USER PASSKEY REQUEST",
"REMOTE OOB DATA REQUEST",
"SIMPLE PAIRING COMPLETE",
"UNKNOWN",
"LINK SUPERVISION TIMEOUT CHANGED",
"ENHANCED FLUSH COMPLETE",
"UNKNOWN",
"USER PASSKEY NOTIFICATION",
"KEYPRESS NOTIFICATION",
"REMOTE HOST FEATURES NOTIFICATION",
};
static const char *
hci_eventstr(unsigned int event)
{
if (event < __arraycount(hci_eventnames))
return hci_eventnames[event];
switch (event) {
case HCI_EVENT_BT_LOGO:
return "BT_LOGO";
case HCI_EVENT_VENDOR:
return "VENDOR";
}
return "UNKNOWN";
}
#endif
void
hci_event(struct mbuf *m, struct hci_unit *unit)
{
hci_event_hdr_t hdr;
KASSERT(m->m_flags & M_PKTHDR);
if (m->m_pkthdr.len < sizeof(hdr))
goto done;
m_copydata(m, 0, sizeof(hdr), &hdr);
m_adj(m, sizeof(hdr));
KASSERT(hdr.type == HCI_EVENT_PKT);
if (m->m_pkthdr.len != hdr.length)
goto done;
DPRINTFN(1, "(%s) event %s\n",
device_xname(unit->hci_dev), hci_eventstr(hdr.event));
switch(hdr.event) {
case HCI_EVENT_COMMAND_STATUS:
hci_event_command_status(unit, m);
break;
case HCI_EVENT_COMMAND_COMPL:
hci_event_command_compl(unit, m);
break;
case HCI_EVENT_NUM_COMPL_PKTS:
hci_event_num_compl_pkts(unit, m);
break;
case HCI_EVENT_INQUIRY_RESULT:
hci_event_inquiry_result(unit, m);
break;
case HCI_EVENT_RSSI_RESULT:
hci_event_rssi_result(unit, m);
break;
case HCI_EVENT_EXTENDED_RESULT:
hci_event_extended_result(unit, m);
break;
case HCI_EVENT_CON_COMPL:
hci_event_con_compl(unit, m);
break;
case HCI_EVENT_DISCON_COMPL:
hci_event_discon_compl(unit, m);
break;
case HCI_EVENT_CON_REQ:
hci_event_con_req(unit, m);
break;
case HCI_EVENT_AUTH_COMPL:
hci_event_auth_compl(unit, m);
break;
case HCI_EVENT_ENCRYPTION_CHANGE:
hci_event_encryption_change(unit, m);
break;
case HCI_EVENT_CHANGE_CON_LINK_KEY_COMPL:
hci_event_change_con_link_key_compl(unit, m);
break;
case HCI_EVENT_READ_CLOCK_OFFSET_COMPL:
hci_event_read_clock_offset_compl(unit, m);
break;
default:
break;
}
done:
m_freem(m);
}
static void
hci_event_command_status(struct hci_unit *unit, struct mbuf *m)
{
hci_command_status_ep ep;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
ep.opcode = le16toh(ep.opcode);
DPRINTFN(1, "(%s) opcode (%03x|%04x) status = 0x%x num_cmd_pkts = %d\n",
device_xname(unit->hci_dev),
HCI_OGF(ep.opcode), HCI_OCF(ep.opcode),
ep.status,
ep.num_cmd_pkts);
hci_num_cmds(unit, ep.num_cmd_pkts);
switch(ep.opcode) {
case HCI_CMD_CREATE_CON:
hci_cmd_create_con(unit, ep.status);
break;
default:
if (ep.status == 0)
break;
aprint_error_dev(unit->hci_dev,
"CommandStatus opcode (%03x|%04x) failed (status=0x%02x)\n",
HCI_OGF(ep.opcode), HCI_OCF(ep.opcode),
ep.status);
break;
}
}
static void
hci_event_command_compl(struct hci_unit *unit, struct mbuf *m)
{
hci_command_compl_ep ep;
hci_status_rp rp;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
DPRINTFN(1, "(%s) opcode (%03x|%04x) num_cmd_pkts = %d\n",
device_xname(unit->hci_dev),
HCI_OGF(le16toh(ep.opcode)), HCI_OCF(le16toh(ep.opcode)),
ep.num_cmd_pkts);
hci_num_cmds(unit, ep.num_cmd_pkts);
if (m->m_pkthdr.len >= sizeof(rp)) {
m_copydata(m, 0, sizeof(rp), &rp);
if (rp.status > 0)
aprint_error_dev(unit->hci_dev,
"CommandComplete opcode (%03x|%04x) failed (status=0x%02x)\n",
HCI_OGF(le16toh(ep.opcode)), HCI_OCF(le16toh(ep.opcode)),
rp.status);
}
switch(le16toh(ep.opcode)) {
case HCI_CMD_READ_BDADDR:
hci_cmd_read_bdaddr(unit, m);
break;
case HCI_CMD_READ_BUFFER_SIZE:
hci_cmd_read_buffer_size(unit, m);
break;
case HCI_CMD_READ_LOCAL_FEATURES:
hci_cmd_read_local_features(unit, m);
break;
case HCI_CMD_READ_LOCAL_EXTENDED_FEATURES:
hci_cmd_read_local_extended_features(unit, m);
break;
case HCI_CMD_READ_LOCAL_VER:
hci_cmd_read_local_ver(unit, m);
break;
case HCI_CMD_READ_LOCAL_COMMANDS:
hci_cmd_read_local_commands(unit, m);
break;
case HCI_CMD_READ_ENCRYPTION_KEY_SIZE:
hci_cmd_read_encryption_key_size(unit, m);
break;
case HCI_CMD_RESET:
hci_cmd_reset(unit, m);
break;
default:
break;
}
}
static void
hci_event_num_compl_pkts(struct hci_unit *unit, struct mbuf *m)
{
hci_num_compl_pkts_ep ep;
struct hci_link *link, *next;
uint16_t handle, num;
int num_acl = 0, num_sco = 0;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
if (m->m_pkthdr.len < ep.num_con_handles * (sizeof(handle) + sizeof(num)))
return;
while (ep.num_con_handles--) {
m_copydata(m, 0, sizeof(handle), &handle);
m_adj(m, sizeof(handle));
handle = le16toh(handle);
m_copydata(m, 0, sizeof(num), &num);
m_adj(m, sizeof(num));
num = le16toh(num);
link = hci_link_lookup_handle(unit, handle);
if (link) {
if (link->hl_type == HCI_LINK_ACL) {
num_acl += num;
hci_acl_complete(link, num);
} else {
num_sco += num;
hci_sco_complete(link, num);
}
} else {
aprint_error_dev(unit->hci_dev,
"unknown handle %d! (losing track of %d packet buffer%s)\n",
handle, num, (num == 1 ? "" : "s"));
}
}
unit->hci_num_acl_pkts += num_acl;
unit->hci_num_sco_pkts += num_sco;
link = TAILQ_FIRST(&unit->hci_links);
while (link && (unit->hci_num_acl_pkts > 0 || unit->hci_num_sco_pkts > 0)) {
next = TAILQ_NEXT(link, hl_next);
if (link->hl_type == HCI_LINK_ACL) {
if (unit->hci_num_acl_pkts > 0 && link->hl_txqlen > 0)
hci_acl_start(link);
} else {
if (unit->hci_num_sco_pkts > 0 && link->hl_txqlen > 0)
hci_sco_start(link);
}
link = next;
}
}
static void
hci_event_inquiry_result(struct hci_unit *unit, struct mbuf *m)
{
hci_inquiry_result_ep ep;
hci_inquiry_response ir;
struct hci_memo *memo;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
DPRINTFN(1, "%d response%s\n", ep.num_responses,
(ep.num_responses == 1 ? "" : "s"));
while(ep.num_responses--) {
if (m->m_pkthdr.len < sizeof(ir))
return;
m_copydata(m, 0, sizeof(ir), &ir);
m_adj(m, sizeof(ir));
DPRINTFN(1, "bdaddr %02x:%02x:%02x:%02x:%02x:%02x\n",
ir.bdaddr.b[5], ir.bdaddr.b[4], ir.bdaddr.b[3],
ir.bdaddr.b[2], ir.bdaddr.b[1], ir.bdaddr.b[0]);
memo = hci_memo_new(unit, &ir.bdaddr);
if (memo != NULL) {
memo->page_scan_rep_mode = ir.page_scan_rep_mode;
memo->page_scan_mode = ir.page_scan_mode;
memo->clock_offset = ir.clock_offset;
}
}
}
static void
hci_event_rssi_result(struct hci_unit *unit, struct mbuf *m)
{
hci_rssi_result_ep ep;
hci_rssi_response rr;
struct hci_memo *memo;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
DPRINTFN(1, "%d response%s\n", ep.num_responses,
(ep.num_responses == 1 ? "" : "s"));
while(ep.num_responses--) {
if (m->m_pkthdr.len < sizeof(rr))
return;
m_copydata(m, 0, sizeof(rr), &rr);
m_adj(m, sizeof(rr));
DPRINTFN(1, "bdaddr %02x:%02x:%02x:%02x:%02x:%02x\n",
rr.bdaddr.b[5], rr.bdaddr.b[4], rr.bdaddr.b[3],
rr.bdaddr.b[2], rr.bdaddr.b[1], rr.bdaddr.b[0]);
memo = hci_memo_new(unit, &rr.bdaddr);
if (memo != NULL) {
memo->page_scan_rep_mode = rr.page_scan_rep_mode;
memo->page_scan_mode = 0;
memo->clock_offset = rr.clock_offset;
}
}
}
static void
hci_event_extended_result(struct hci_unit *unit, struct mbuf *m)
{
hci_extended_result_ep ep;
struct hci_memo *memo;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
if (ep.num_responses != 1)
return;
DPRINTFN(1, "bdaddr %02x:%02x:%02x:%02x:%02x:%02x\n",
ep.bdaddr.b[5], ep.bdaddr.b[4], ep.bdaddr.b[3],
ep.bdaddr.b[2], ep.bdaddr.b[1], ep.bdaddr.b[0]);
memo = hci_memo_new(unit, &ep.bdaddr);
if (memo != NULL) {
memo->page_scan_rep_mode = ep.page_scan_rep_mode;
memo->page_scan_mode = 0;
memo->clock_offset = ep.clock_offset;
}
}
static void
hci_event_con_compl(struct hci_unit *unit, struct mbuf *m)
{
hci_con_compl_ep ep;
hci_write_link_policy_settings_cp cp;
struct hci_link *link;
int err;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
DPRINTFN(1, "(%s) %s connection complete for "
"%02x:%02x:%02x:%02x:%02x:%02x status %#x\n",
device_xname(unit->hci_dev),
(ep.link_type == HCI_LINK_ACL ? "ACL" : "SCO"),
ep.bdaddr.b[5], ep.bdaddr.b[4], ep.bdaddr.b[3],
ep.bdaddr.b[2], ep.bdaddr.b[1], ep.bdaddr.b[0],
ep.status);
link = hci_link_lookup_bdaddr(unit, &ep.bdaddr, ep.link_type);
if (ep.status) {
if (link != NULL) {
switch (ep.status) {
case 0x04:
err = EHOSTDOWN;
break;
case 0x08:
err = ETIMEDOUT;
break;
case 0x16:
err = 0;
break;
default:
err = ECONNREFUSED;
break;
}
hci_link_free(link, err);
}
return;
}
if (link == NULL) {
hci_discon_cp dp;
dp.con_handle = ep.con_handle;
dp.reason = 0x13;
hci_send_cmd(unit, HCI_CMD_DISCONNECT, &dp, sizeof(dp));
return;
}
link->hl_state = HCI_LINK_OPEN;
link->hl_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
if (ep.link_type == HCI_LINK_ACL) {
cp.con_handle = ep.con_handle;
cp.settings = htole16(unit->hci_link_policy);
err = hci_send_cmd(unit, HCI_CMD_WRITE_LINK_POLICY_SETTINGS,
&cp, sizeof(cp));
if (err)
aprint_error_dev(unit->hci_dev,
"Warning, could not write link policy\n");
err = hci_send_cmd(unit, HCI_CMD_READ_CLOCK_OFFSET,
&cp.con_handle, sizeof(cp.con_handle));
if (err)
aprint_error_dev(unit->hci_dev,
"Warning, could not read clock offset\n");
err = hci_acl_setmode(link);
if (err == EINPROGRESS)
return;
hci_acl_linkmode(link);
} else {
(*link->hl_sco->sp_proto->connected)(link->hl_sco->sp_upper);
}
}
static void
hci_event_discon_compl(struct hci_unit *unit, struct mbuf *m)
{
hci_discon_compl_ep ep;
struct hci_link *link;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
ep.con_handle = le16toh(ep.con_handle);
DPRINTFN(1, "handle #%d, status=0x%x\n", ep.con_handle, ep.status);
link = hci_link_lookup_handle(unit, HCI_CON_HANDLE(ep.con_handle));
if (link)
hci_link_free(link, ENOLINK);
}
static void
hci_event_con_req(struct hci_unit *unit, struct mbuf *m)
{
hci_con_req_ep ep;
hci_accept_con_cp ap;
hci_reject_con_cp rp;
struct hci_link *link;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
DPRINTFN(1, "bdaddr %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x "
"class %2.2x%2.2x%2.2x type %s\n",
ep.bdaddr.b[5], ep.bdaddr.b[4], ep.bdaddr.b[3],
ep.bdaddr.b[2], ep.bdaddr.b[1], ep.bdaddr.b[0],
ep.uclass[0], ep.uclass[1], ep.uclass[2],
ep.link_type == HCI_LINK_ACL ? "ACL" : "SCO");
if (ep.link_type == HCI_LINK_ACL)
link = hci_acl_newconn(unit, &ep.bdaddr);
else
link = hci_sco_newconn(unit, &ep.bdaddr);
if (link == NULL) {
memset(&rp, 0, sizeof(rp));
bdaddr_copy(&rp.bdaddr, &ep.bdaddr);
rp.reason = 0x0f;
hci_send_cmd(unit, HCI_CMD_REJECT_CON, &rp, sizeof(rp));
} else {
memset(&ap, 0, sizeof(ap));
bdaddr_copy(&ap.bdaddr, &ep.bdaddr);
if (unit->hci_flags & BTF_MASTER)
ap.role = HCI_ROLE_MASTER;
else
ap.role = HCI_ROLE_SLAVE;
hci_send_cmd(unit, HCI_CMD_ACCEPT_CON, &ap, sizeof(ap));
}
}
static void
hci_event_auth_compl(struct hci_unit *unit, struct mbuf *m)
{
hci_auth_compl_ep ep;
struct hci_link *link;
int err;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
ep.con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
DPRINTFN(1, "handle #%d, status=0x%x\n", ep.con_handle, ep.status);
link = hci_link_lookup_handle(unit, ep.con_handle);
if (link == NULL || link->hl_type != HCI_LINK_ACL)
return;
if (ep.status == 0) {
link->hl_flags |= HCI_LINK_AUTH;
if (link->hl_state == HCI_LINK_WAIT_AUTH)
link->hl_state = HCI_LINK_OPEN;
err = hci_acl_setmode(link);
if (err == EINPROGRESS)
return;
}
hci_acl_linkmode(link);
}
static void
hci_event_encryption_change(struct hci_unit *unit, struct mbuf *m)
{
hci_encryption_change_ep ep;
struct hci_link *link;
uint16_t con_handle;
int err;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
DPRINTFN(1, "handle #%d, status=0x%x, encryption_enable=0x%x\n",
con_handle, ep.status, ep.encryption_enable);
link = hci_link_lookup_handle(unit, con_handle);
if (link == NULL || link->hl_type != HCI_LINK_ACL)
return;
if (ep.status == 0) {
if (ep.encryption_enable == 0) {
link->hl_flags &= ~HCI_LINK_ENCRYPT;
} else if (unit->hci_cmds[20] & (1<<4)) {
err = hci_send_cmd(unit, HCI_CMD_READ_ENCRYPTION_KEY_SIZE,
&ep.con_handle, sizeof(ep.con_handle));
if (err == 0)
return;
} else {
link->hl_flags |= (HCI_LINK_AUTH | HCI_LINK_ENCRYPT);
if (link->hl_state == HCI_LINK_WAIT_ENCRYPT)
link->hl_state = HCI_LINK_OPEN;
err = hci_acl_setmode(link);
if (err == EINPROGRESS)
return;
}
}
hci_acl_linkmode(link);
}
static void
hci_event_change_con_link_key_compl(struct hci_unit *unit, struct mbuf *m)
{
hci_change_con_link_key_compl_ep ep;
struct hci_link *link;
int err;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
ep.con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
DPRINTFN(1, "handle #%d, status=0x%x\n", ep.con_handle, ep.status);
link = hci_link_lookup_handle(unit, ep.con_handle);
if (link == NULL || link->hl_type != HCI_LINK_ACL)
return;
link->hl_flags &= ~HCI_LINK_SECURE_REQ;
if (ep.status == 0) {
link->hl_flags |= (HCI_LINK_AUTH | HCI_LINK_SECURE);
if (link->hl_state == HCI_LINK_WAIT_SECURE)
link->hl_state = HCI_LINK_OPEN;
err = hci_acl_setmode(link);
if (err == EINPROGRESS)
return;
}
hci_acl_linkmode(link);
}
static void
hci_event_read_clock_offset_compl(struct hci_unit *unit, struct mbuf *m)
{
hci_read_clock_offset_compl_ep ep;
struct hci_link *link;
if (m->m_pkthdr.len < sizeof(ep))
return;
m_copydata(m, 0, sizeof(ep), &ep);
m_adj(m, sizeof(ep));
DPRINTFN(1, "handle #%d, offset=%u, status=0x%x\n",
le16toh(ep.con_handle), le16toh(ep.clock_offset), ep.status);
ep.con_handle = HCI_CON_HANDLE(le16toh(ep.con_handle));
link = hci_link_lookup_handle(unit, ep.con_handle);
if (link == NULL || link->hl_type != HCI_LINK_ACL)
return;
if (ep.status == 0)
link->hl_clock = ep.clock_offset;
}
static void
hci_cmd_read_bdaddr(struct hci_unit *unit, struct mbuf *m)
{
hci_read_bdaddr_rp rp;
if (m->m_pkthdr.len < sizeof(rp))
return;
m_copydata(m, 0, sizeof(rp), &rp);
m_adj(m, sizeof(rp));
if (rp.status > 0)
return;
if ((unit->hci_flags & BTF_INIT_BDADDR) == 0)
return;
bdaddr_copy(&unit->hci_bdaddr, &rp.bdaddr);
unit->hci_flags &= ~BTF_INIT_BDADDR;
cv_broadcast(&unit->hci_init);
}
static void
hci_cmd_read_buffer_size(struct hci_unit *unit, struct mbuf *m)
{
hci_read_buffer_size_rp rp;
if (m->m_pkthdr.len < sizeof(rp))
return;
m_copydata(m, 0, sizeof(rp), &rp);
m_adj(m, sizeof(rp));
if (rp.status > 0)
return;
if ((unit->hci_flags & BTF_INIT_BUFFER_SIZE) == 0)
return;
unit->hci_max_acl_size = le16toh(rp.max_acl_size);
unit->hci_num_acl_pkts = le16toh(rp.num_acl_pkts);
unit->hci_max_acl_pkts = le16toh(rp.num_acl_pkts);
unit->hci_max_sco_size = rp.max_sco_size;
unit->hci_num_sco_pkts = le16toh(rp.num_sco_pkts);
unit->hci_max_sco_pkts = le16toh(rp.num_sco_pkts);
unit->hci_flags &= ~BTF_INIT_BUFFER_SIZE;
cv_broadcast(&unit->hci_init);
}
static void
hci_cmd_read_local_features(struct hci_unit *unit, struct mbuf *m)
{
hci_read_local_features_rp rp;
if (m->m_pkthdr.len < sizeof(rp))
return;
m_copydata(m, 0, sizeof(rp), &rp);
m_adj(m, sizeof(rp));
if (rp.status > 0)
return;
if ((unit->hci_flags & BTF_INIT_FEATURES) == 0)
return;
memcpy(unit->hci_feat0, rp.features, HCI_FEATURES_SIZE);
unit->hci_lmp_mask = 0;
if (rp.features[0] & HCI_LMP_ROLE_SWITCH)
unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_ROLE_SWITCH;
if (rp.features[0] & HCI_LMP_HOLD_MODE)
unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_HOLD_MODE;
if (rp.features[0] & HCI_LMP_SNIFF_MODE)
unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_SNIFF_MODE;
if (rp.features[1] & HCI_LMP_PARK_MODE)
unit->hci_lmp_mask |= HCI_LINK_POLICY_ENABLE_PARK_MODE;
DPRINTFN(1, "%s: lmp_mask %4.4x\n",
device_xname(unit->hci_dev), unit->hci_lmp_mask);
unit->hci_acl_mask = HCI_PKT_DM1 | HCI_PKT_DH1;
if (rp.features[0] & HCI_LMP_3SLOT)
unit->hci_acl_mask |= HCI_PKT_DM3 | HCI_PKT_DH3;
if (rp.features[0] & HCI_LMP_5SLOT)
unit->hci_acl_mask |= HCI_PKT_DM5 | HCI_PKT_DH5;
if ((rp.features[3] & HCI_LMP_EDR_ACL_2MBPS) == 0)
unit->hci_acl_mask |= HCI_PKT_2MBPS_DH1
| HCI_PKT_2MBPS_DH3
| HCI_PKT_2MBPS_DH5;
if ((rp.features[3] & HCI_LMP_EDR_ACL_3MBPS) == 0)
unit->hci_acl_mask |= HCI_PKT_3MBPS_DH1
| HCI_PKT_3MBPS_DH3
| HCI_PKT_3MBPS_DH5;
if ((rp.features[4] & HCI_LMP_3SLOT_EDR_ACL) == 0)
unit->hci_acl_mask |= HCI_PKT_2MBPS_DH3
| HCI_PKT_3MBPS_DH3;
if ((rp.features[5] & HCI_LMP_5SLOT_EDR_ACL) == 0)
unit->hci_acl_mask |= HCI_PKT_2MBPS_DH5
| HCI_PKT_3MBPS_DH5;
DPRINTFN(1, "%s: acl_mask %4.4x\n",
device_xname(unit->hci_dev), unit->hci_acl_mask);
unit->hci_packet_type = unit->hci_acl_mask;
unit->hci_sco_mask = 0;
if (rp.features[1] & HCI_LMP_SCO_LINK)
unit->hci_sco_mask |= HCI_PKT_HV1;
if (rp.features[1] & HCI_LMP_HV2_PKT)
unit->hci_sco_mask |= HCI_PKT_HV2;
if (rp.features[1] & HCI_LMP_HV3_PKT)
unit->hci_sco_mask |= HCI_PKT_HV3;
if (rp.features[3] & HCI_LMP_EV3_PKT)
unit->hci_sco_mask |= HCI_PKT_EV3;
if (rp.features[4] & HCI_LMP_EV4_PKT)
unit->hci_sco_mask |= HCI_PKT_EV4;
if (rp.features[4] & HCI_LMP_EV5_PKT)
unit->hci_sco_mask |= HCI_PKT_EV5;
DPRINTFN(1, "%s: sco_mask %4.4x\n",
device_xname(unit->hci_dev), unit->hci_sco_mask);
if (rp.features[7] & HCI_LMP_EXTENDED_FEATURES) {
hci_read_local_extended_features_cp cp;
cp.page = 0;
hci_send_cmd(unit, HCI_CMD_READ_LOCAL_EXTENDED_FEATURES,
&cp, sizeof(cp));
return;
}
unit->hci_flags &= ~BTF_INIT_FEATURES;
cv_broadcast(&unit->hci_init);
}
static void
hci_cmd_read_local_extended_features(struct hci_unit *unit, struct mbuf *m)
{
hci_read_local_extended_features_rp rp;
if (m->m_pkthdr.len < sizeof(rp))
return;
m_copydata(m, 0, sizeof(rp), &rp);
m_adj(m, sizeof(rp));
if (rp.status > 0)
return;
if ((unit->hci_flags & BTF_INIT_FEATURES) == 0)
return;
DPRINTFN(1, "%s: page %d of %d\n", device_xname(unit->hci_dev),
rp.page, rp.max_page);
switch (rp.page) {
case 2:
memcpy(unit->hci_feat2, rp.features, HCI_FEATURES_SIZE);
break;
case 1:
memcpy(unit->hci_feat1, rp.features, HCI_FEATURES_SIZE);
break;
case 0:
default:
break;
}
if (rp.page < rp.max_page) {
hci_read_local_extended_features_cp cp;
cp.page = rp.page + 1;
hci_send_cmd(unit, HCI_CMD_READ_LOCAL_EXTENDED_FEATURES,
&cp, sizeof(cp));
return;
}
unit->hci_flags &= ~BTF_INIT_FEATURES;
cv_broadcast(&unit->hci_init);
}
static void
hci_cmd_read_local_ver(struct hci_unit *unit, struct mbuf *m)
{
hci_read_local_ver_rp rp;
if (m->m_pkthdr.len < sizeof(rp))
return;
m_copydata(m, 0, sizeof(rp), &rp);
m_adj(m, sizeof(rp));
if (rp.status != 0)
return;
if ((unit->hci_flags & BTF_INIT_COMMANDS) == 0)
return;
if (rp.hci_version < HCI_SPEC_V12) {
unit->hci_flags &= ~BTF_INIT_COMMANDS;
cv_broadcast(&unit->hci_init);
return;
}
hci_send_cmd(unit, HCI_CMD_READ_LOCAL_COMMANDS, NULL, 0);
}
static void
hci_cmd_read_local_commands(struct hci_unit *unit, struct mbuf *m)
{
hci_read_local_commands_rp rp;
if (m->m_pkthdr.len < sizeof(rp))
return;
m_copydata(m, 0, sizeof(rp), &rp);
m_adj(m, sizeof(rp));
if (rp.status != 0)
return;
if ((unit->hci_flags & BTF_INIT_COMMANDS) == 0)
return;
unit->hci_flags &= ~BTF_INIT_COMMANDS;
memcpy(unit->hci_cmds, rp.commands, HCI_COMMANDS_SIZE);
cv_broadcast(&unit->hci_init);
}
static void
hci_cmd_read_encryption_key_size(struct hci_unit *unit, struct mbuf *m)
{
hci_read_encryption_key_size_rp rp;
struct hci_link *link;
int err;
if (m->m_pkthdr.len < sizeof(rp))
return;
m_copydata(m, 0, sizeof(rp), &rp);
m_adj(m, sizeof(rp));
if (rp.status != 0)
return;
rp.con_handle = HCI_CON_HANDLE(le16toh(rp.con_handle));
DPRINTFN(1, "handle #%d, status=0x%x, key_size=0x%x\n",
rp.con_handle, rp.status, rp.size);
link = hci_link_lookup_handle(unit, rp.con_handle);
if (link == NULL || link->hl_type != HCI_LINK_ACL)
return;
if (rp.status == 0) {
if (rp.size < 7) {
link->hl_flags &= ~HCI_LINK_ENCRYPT;
} else {
link->hl_flags |= (HCI_LINK_AUTH | HCI_LINK_ENCRYPT);
if (link->hl_state == HCI_LINK_WAIT_ENCRYPT)
link->hl_state = HCI_LINK_OPEN;
err = hci_acl_setmode(link);
if (err == EINPROGRESS)
return;
}
}
hci_acl_linkmode(link);
}
static void
hci_cmd_reset(struct hci_unit *unit, struct mbuf *m)
{
hci_reset_rp rp;
struct hci_link *link, *next;
int acl;
if (m->m_pkthdr.len < sizeof(rp))
return;
m_copydata(m, 0, sizeof(rp), &rp);
m_adj(m, sizeof(rp));
if (rp.status != 0)
return;
for (acl = 0 ; acl < 2 ; acl++) {
next = TAILQ_FIRST(&unit->hci_links);
while ((link = next) != NULL) {
next = TAILQ_NEXT(link, hl_next);
if (acl || link->hl_type != HCI_LINK_ACL)
hci_link_free(link, ECONNABORTED);
}
}
unit->hci_num_acl_pkts = 0;
unit->hci_num_sco_pkts = 0;
if (hci_send_cmd(unit, HCI_CMD_READ_BDADDR, NULL, 0))
return;
if (hci_send_cmd(unit, HCI_CMD_READ_BUFFER_SIZE, NULL, 0))
return;
if (hci_send_cmd(unit, HCI_CMD_READ_LOCAL_FEATURES, NULL, 0))
return;
if (hci_send_cmd(unit, HCI_CMD_READ_LOCAL_VER, NULL, 0))
return;
}
static void
hci_cmd_create_con(struct hci_unit *unit, uint8_t status)
{
struct hci_link *link;
TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
if ((link->hl_flags & HCI_LINK_CREATE_CON) == 0)
continue;
link->hl_flags &= ~HCI_LINK_CREATE_CON;
switch(status) {
case 0x00:
break;
case 0x0c:
hci_link_free(link, EBUSY);
break;
default:
hci_link_free(link, EPROTO);
break;
}
return;
}
}