28 #include <linux/export.h>
31 #include <linux/rfkill.h>
62 BT_DBG(
"%s command 0x%4.4x result 0x%2.2x", hdev->
name, cmd, result);
98 static void hci_req_cancel(
struct hci_dev *hdev,
int err)
110 static int __hci_request(
struct hci_dev *hdev,
112 unsigned long opt,
__u32 timeout)
153 static int hci_request(
struct hci_dev *hdev,
154 void (*
req)(
struct hci_dev *hdev,
unsigned long opt),
155 unsigned long opt,
__u32 timeout)
164 ret = __hci_request(hdev,
req, opt, timeout);
170 static void hci_reset_req(
struct hci_dev *hdev,
unsigned long opt)
179 static void bredr_init(
struct hci_dev *hdev)
225 static void amp_init(
struct hci_dev *hdev)
239 static void hci_init_req(
struct hci_dev *hdev,
unsigned long opt)
250 skb->
dev = (
void *) hdev;
259 hci_reset_req(hdev, 0);
276 static void hci_le_init_req(
struct hci_dev *hdev,
unsigned long opt)
284 static void hci_scan_req(
struct hci_dev *hdev,
unsigned long opt)
294 static void hci_auth_req(
struct hci_dev *hdev,
unsigned long opt)
304 static void hci_encrypt_req(
struct hci_dev *hdev,
unsigned long opt)
314 static void hci_linkpol_req(
struct hci_dev *hdev,
unsigned long opt)
337 if (d->id == index) {
338 hdev = hci_dev_hold(d);
352 switch (discov->
state) {
388 static void inquiry_cache_flush(
struct hci_dev *hdev)
398 INIT_LIST_HEAD(&cache->
unknown);
399 INIT_LIST_HEAD(&cache->
resolve);
411 if (!bacmp(&e->
data.bdaddr, bdaddr))
427 if (!bacmp(&e->
data.bdaddr, bdaddr))
441 BT_DBG(
"cache %p bdaddr %s state %d", cache,
batostr(bdaddr), state);
446 if (!bacmp(&e->
data.bdaddr, bdaddr))
469 list_add(&ie->
list, pos);
473 bool name_known,
bool *
ssp)
485 if (ie->
data.ssp_mode && ssp)
502 list_add(&ie->
all, &cache->
all);
528 static int inquiry_cache_dump(
struct hci_dev *hdev,
int num,
__u8 *
buf)
552 BT_DBG(
"cache %p, copied %d", cache, copied);
556 static void hci_inq_req(
struct hci_dev *hdev,
unsigned long opt)
578 int err = 0, do_inquiry = 0, max_rsp;
592 inquiry_cache_flush(hdev);
600 err = hci_request(hdev, hci_inq_req, (
unsigned long)&ir, timeo);
620 ir.
num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
678 if (hdev->
open(hdev)) {
691 ret = __hci_request(hdev, hci_le_init_req, 0,
734 static int hci_dev_do_close(
struct hci_dev *hdev)
742 hci_req_cancel(hdev,
ENODEV);
767 inquiry_cache_flush(hdev);
836 err = hci_dev_do_close(hdev);
861 inquiry_cache_flush(hdev);
911 err = hci_request(hdev, hci_auth_req, dr.
dev_opt,
923 err = hci_request(hdev, hci_auth_req, dr.
dev_opt,
929 err = hci_request(hdev, hci_encrypt_req, dr.
dev_opt,
934 err = hci_request(hdev, hci_scan_req, dr.
dev_opt,
939 err = hci_request(hdev, hci_linkpol_req, dr.
dev_opt,
982 if (!dev_num || dev_num > (
PAGE_SIZE * 2) /
sizeof(*dr))
985 size =
sizeof(*dl) + dev_num *
sizeof(*dr);
1002 (dr + n)->dev_opt = hdev->
flags;
1010 size =
sizeof(*dl) + n *
sizeof(*dr);
1015 return err ? -
EFAULT : 0;
1062 static int hci_rfkill_set_block(
void *
data,
bool blocked)
1066 BT_DBG(
"%p name %s blocked %d", hdev, hdev->
name, blocked);
1071 hci_dev_do_close(hdev);
1076 static const struct rfkill_ops hci_rfkill_ops = {
1077 .set_block = hci_rfkill_set_block,
1096 static void hci_power_off(
struct work_struct *work)
1103 hci_dev_do_close(hdev);
1106 static void hci_discov_off(
struct work_struct *work)
1173 if (bacmp(bdaddr, &k->
bdaddr) == 0)
1179 static bool hci_persistent_key(
struct hci_dev *hdev,
struct hci_conn *conn,
1180 u8 key_type,
u8 old_key_type)
1183 if (key_type < 0x03)
1220 if (k->
ediv != ediv ||
1237 bacmp(bdaddr, &k->
bdaddr) == 0)
1252 old_key_type = old_key->
type;
1255 old_key_type = conn ? conn->
key_type : 0xff;
1268 (!conn || conn->
remote_auth == 0xff) && old_key_type == 0xff) {
1274 bacpy(&key->
bdaddr, bdaddr);
1279 key->
type = old_key_type;
1286 persistent = hci_persistent_key(hdev, conn, type, old_key_type);
1297 int new_key,
u8 authenticated,
u8 tk[16],
u8 enc_size,
__le16
1315 bacpy(&key->
bdaddr, bdaddr);
1327 if (type & HCI_SMP_LTK)
1354 if (bacmp(bdaddr, &k->
bdaddr))
1367 static void hci_cmd_timeout(
unsigned long arg)
1369 struct hci_dev *hdev = (
void *) arg;
1390 if (bacmp(bdaddr, &data->
bdaddr) == 0)
1436 bacpy(&data->
bdaddr, bdaddr);
1453 if (bacmp(bdaddr, &b->
bdaddr) == 0)
1489 bacpy(&entry->
bdaddr, bdaddr);
1513 static void le_scan_param_req(
struct hci_dev *hdev,
unsigned long opt)
1526 static void le_scan_enable_req(
struct hci_dev *hdev,
unsigned long opt)
1555 err = __hci_request(hdev, le_scan_param_req, (
unsigned long) ¶m,
1558 err = __hci_request(hdev, le_scan_enable_req, 0, timeo);
1582 memset(&cp, 0,
sizeof(cp));
1589 static void le_scan_disable_work(
struct work_struct *work)
1602 static void le_scan_work(
struct work_struct *work)
1655 INIT_LIST_HEAD(&hdev->
uuids);
1672 skb_queue_head_init(&hdev->
rx_q);
1673 skb_queue_head_init(&hdev->
cmd_q);
1674 skb_queue_head_init(&hdev->
raw_q);
1681 discovery_init(hdev);
1791 hci_dev_do_close(hdev);
1859 bt_cb(skb)->incoming = 1;
1862 __net_timestamp(skb);
1871 static int hci_reassembly(
struct hci_dev *hdev,
int type,
void *data,
1906 scb = (
void *) skb->cb;
1910 skb->
dev = (
void *) hdev;
1915 scb = (
void *) skb->cb;
1931 if (skb_tailroom(skb) < scb->
expect) {
1944 if (skb_tailroom(skb) < scb->
expect) {
1957 if (skb_tailroom(skb) < scb->
expect) {
1988 rem = hci_reassembly(hdev, type, data, count, type - 1);
1992 data += (count - rem);
2000 #define STREAM_REASSEMBLY 0
2011 struct {
char type; } *pkt;
2020 type =
bt_cb(skb)->pkt_type;
2022 rem = hci_reassembly(hdev, type, data, count,
2027 data += (count - rem);
2061 static int hci_send_frame(
struct sk_buff *skb)
2073 __net_timestamp(skb);
2086 return hdev->
send(skb);
2096 BT_DBG(
"%s opcode 0x%4.4x plen %d", hdev->
name, opcode, plen);
2100 BT_ERR(
"%s no memory for command", hdev->
name);
2114 skb->
dev = (
void *) hdev;
2133 hdr = (
void *) hdev->
sent_cmd->data;
2138 BT_DBG(
"%s opcode 0x%4.4x", hdev->
name, opcode);
2150 skb_reset_transport_header(skb);
2151 hdr = (
struct hci_acl_hdr *)skb_transport_header(skb);
2162 skb->
len = skb_headlen(skb);
2166 hci_add_acl_hdr(skb, conn->
handle, flags);
2168 list = skb_shinfo(skb)->frag_list;
2171 BT_DBG(
"%s nonfrag skb %p len %d", hdev->
name, skb, skb->
len);
2178 skb_shinfo(skb)->frag_list =
NULL;
2181 spin_lock(&queue->
lock);
2183 __skb_queue_tail(queue, skb);
2190 skb->
dev = (
void *) hdev;
2192 hci_add_acl_hdr(skb, conn->
handle, flags);
2196 __skb_queue_tail(queue, skb);
2199 spin_unlock(&queue->
lock);
2208 BT_DBG(
"%s chan %p flags 0x%4.4x", hdev->
name, chan, flags);
2210 skb->
dev = (
void *) hdev;
2212 hci_queue_acl(conn, &chan->
data_q, skb, flags);
2229 skb_reset_transport_header(skb);
2232 skb->
dev = (
void *) hdev;
2247 unsigned int num = 0,
min = ~0;
2254 list_for_each_entry_rcu(c, &h->
list, list) {
2255 if (c->type != type || skb_queue_empty(&c->data_q))
2263 if (c->sent <
min) {
2268 if (hci_conn_num(hdev, type) == num)
2277 switch (conn->
type) {
2290 BT_ERR(
"Unknown link type");
2298 BT_DBG(
"conn %p quote %d", conn, *quote);
2302 static void hci_link_tx_to(
struct hci_dev *hdev,
__u8 type)
2312 list_for_each_entry_rcu(c, &h->
list, list) {
2314 BT_ERR(
"%s killing stalled connection %s",
2328 unsigned int num = 0,
min = ~0, cur_prio = 0;
2330 int cnt,
q, conn_num = 0;
2336 list_for_each_entry_rcu(conn, &h->
list, list) {
2339 if (conn->
type != type)
2347 list_for_each_entry_rcu(tmp, &conn->
chan_list, list) {
2350 if (skb_queue_empty(&tmp->
data_q))
2353 skb = skb_peek(&tmp->
data_q);
2371 if (hci_conn_num(hdev, type) == conn_num)
2380 switch (chan->
conn->type) {
2393 BT_ERR(
"Unknown link type");
2398 BT_DBG(
"chan %p quote %d", chan, *quote);
2402 static void hci_prio_recalculate(
struct hci_dev *hdev,
__u8 type)
2412 list_for_each_entry_rcu(conn, &h->
list, list) {
2415 if (conn->
type != type)
2423 list_for_each_entry_rcu(chan, &conn->
chan_list, list) {
2431 if (skb_queue_empty(&chan->
data_q))
2434 skb = skb_peek(&chan->
data_q);
2440 BT_DBG(
"chan %p skb %p promoted to %d", chan, skb,
2444 if (hci_conn_num(hdev, type) == num)
2452 static inline int __get_blocks(
struct hci_dev *hdev,
struct sk_buff *skb)
2458 static void __check_timeout(
struct hci_dev *hdev,
unsigned int cnt)
2469 static void hci_sched_acl_pkt(
struct hci_dev *hdev)
2471 unsigned int cnt = hdev->
acl_cnt;
2476 __check_timeout(hdev, cnt);
2479 (chan = hci_chan_sent(hdev,
ACL_LINK, "e))) {
2481 while (quote-- && (skb = skb_peek(&chan->
data_q))) {
2482 BT_DBG(
"chan %p skb %p len %d priority %u", chan, skb,
2492 bt_cb(skb)->force_active);
2494 hci_send_frame(skb);
2504 hci_prio_recalculate(hdev,
ACL_LINK);
2507 static void hci_sched_acl_blk(
struct hci_dev *hdev)
2514 __check_timeout(hdev, cnt);
2517 (chan = hci_chan_sent(hdev,
ACL_LINK, "e))) {
2518 u32 priority = (skb_peek(&chan->
data_q))->priority;
2519 while (quote > 0 && (skb = skb_peek(&chan->
data_q))) {
2522 BT_DBG(
"chan %p skb %p len %d priority %u", chan, skb,
2531 blocks = __get_blocks(hdev, skb);
2536 bt_cb(skb)->force_active);
2538 hci_send_frame(skb);
2544 chan->
sent += blocks;
2545 chan->
conn->sent += blocks;
2550 hci_prio_recalculate(hdev,
ACL_LINK);
2553 static void hci_sched_acl(
struct hci_dev *hdev)
2562 hci_sched_acl_pkt(hdev);
2566 hci_sched_acl_blk(hdev);
2572 static void hci_sched_sco(
struct hci_dev *hdev)
2583 while (hdev->
sco_cnt && (conn = hci_low_sent(hdev,
SCO_LINK, "e))) {
2586 hci_send_frame(skb);
2589 if (conn->
sent == ~0)
2595 static void hci_sched_esco(
struct hci_dev *hdev)
2610 hci_send_frame(skb);
2613 if (conn->
sent == ~0)
2619 static void hci_sched_le(
struct hci_dev *hdev)
2627 if (!hci_conn_num(hdev,
LE_LINK))
2635 hci_link_tx_to(hdev,
LE_LINK);
2640 while (cnt && (chan = hci_chan_sent(hdev,
LE_LINK, "e))) {
2641 u32 priority = (skb_peek(&chan->
data_q))->priority;
2642 while (quote-- && (skb = skb_peek(&chan->
data_q))) {
2643 BT_DBG(
"chan %p skb %p len %d priority %u", chan, skb,
2652 hci_send_frame(skb);
2667 hci_prio_recalculate(hdev,
LE_LINK);
2680 hci_sched_acl(hdev);
2682 hci_sched_sco(hdev);
2684 hci_sched_esco(hdev);
2690 hci_send_frame(skb);
2696 static void hci_acldata_packet(
struct hci_dev *hdev,
struct sk_buff *skb)
2700 __u16 handle, flags;
2708 BT_DBG(
"%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->
name, skb->
len,
2711 hdev->
stat.acl_rx++;
2714 conn = hci_conn_hash_lookup_handle(hdev, handle);
2732 BT_ERR(
"%s ACL packet for unknown connection handle %d",
2733 hdev->
name, handle);
2740 static void hci_scodata_packet(
struct hci_dev *hdev,
struct sk_buff *skb)
2750 BT_DBG(
"%s len %d handle 0x%4.4x", hdev->
name, skb->
len, handle);
2752 hdev->
stat.sco_rx++;
2755 conn = hci_conn_hash_lookup_handle(hdev, handle);
2763 BT_ERR(
"%s SCO packet for unknown connection handle %d",
2764 hdev->
name, handle);
2810 hci_acldata_packet(hdev, skb);
2815 hci_scodata_packet(hdev, skb);
2825 static void hci_cmd_work(
struct work_struct *work)
2830 BT_DBG(
"%s cmd_cnt %d cmd queued %d", hdev->
name,
2844 hci_send_frame(skb);
2860 u8 lap[3] = { 0x33, 0x8b, 0x9e };
2868 inquiry_cache_flush(hdev);
2870 memset(&cp, 0,
sizeof(cp));
2889 switch (bdaddr_type) {