16a98e383SMarcel Holtmann // SPDX-License-Identifier: GPL-2.0 26a98e383SMarcel Holtmann /* 36a98e383SMarcel Holtmann * BlueZ - Bluetooth protocol stack for Linux 46a98e383SMarcel Holtmann * 56a98e383SMarcel Holtmann * Copyright (C) 2021 Intel Corporation 66a98e383SMarcel Holtmann */ 76a98e383SMarcel Holtmann 8d0b13706SLuiz Augusto von Dentz #include <linux/property.h> 9d0b13706SLuiz Augusto von Dentz 106a98e383SMarcel Holtmann #include <net/bluetooth/bluetooth.h> 116a98e383SMarcel Holtmann #include <net/bluetooth/hci_core.h> 126a98e383SMarcel Holtmann #include <net/bluetooth/mgmt.h> 136a98e383SMarcel Holtmann 146a98e383SMarcel Holtmann #include "hci_request.h" 15d0b13706SLuiz Augusto von Dentz #include "hci_debugfs.h" 166a98e383SMarcel Holtmann #include "smp.h" 17161510ccSLuiz Augusto von Dentz #include "eir.h" 18d0b13706SLuiz Augusto von Dentz #include "msft.h" 19d0b13706SLuiz Augusto von Dentz #include "aosp.h" 20d0b13706SLuiz Augusto von Dentz #include "leds.h" 216a98e383SMarcel Holtmann 226a98e383SMarcel Holtmann static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode, 236a98e383SMarcel Holtmann struct sk_buff *skb) 246a98e383SMarcel Holtmann { 256a98e383SMarcel Holtmann bt_dev_dbg(hdev, "result 0x%2.2x", result); 266a98e383SMarcel Holtmann 276a98e383SMarcel Holtmann if (hdev->req_status != HCI_REQ_PEND) 286a98e383SMarcel Holtmann return; 296a98e383SMarcel Holtmann 306a98e383SMarcel Holtmann hdev->req_result = result; 316a98e383SMarcel Holtmann hdev->req_status = HCI_REQ_DONE; 326a98e383SMarcel Holtmann 33cba6b758SLuiz Augusto von Dentz if (skb) { 34cba6b758SLuiz Augusto von Dentz struct sock *sk = hci_skb_sk(skb); 35cba6b758SLuiz Augusto von Dentz 36cba6b758SLuiz Augusto von Dentz /* Drop sk reference if set */ 37cba6b758SLuiz Augusto von Dentz if (sk) 38cba6b758SLuiz Augusto von Dentz sock_put(sk); 39cba6b758SLuiz Augusto von Dentz 40cba6b758SLuiz Augusto von Dentz hdev->req_skb = skb_get(skb); 41cba6b758SLuiz Augusto von Dentz } 42cba6b758SLuiz Augusto von Dentz 436a98e383SMarcel Holtmann wake_up_interruptible(&hdev->req_wait_q); 446a98e383SMarcel Holtmann } 456a98e383SMarcel Holtmann 466a98e383SMarcel Holtmann static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode, 476a98e383SMarcel Holtmann u32 plen, const void *param, 486a98e383SMarcel Holtmann struct sock *sk) 496a98e383SMarcel Holtmann { 506a98e383SMarcel Holtmann int len = HCI_COMMAND_HDR_SIZE + plen; 516a98e383SMarcel Holtmann struct hci_command_hdr *hdr; 526a98e383SMarcel Holtmann struct sk_buff *skb; 536a98e383SMarcel Holtmann 546a98e383SMarcel Holtmann skb = bt_skb_alloc(len, GFP_ATOMIC); 556a98e383SMarcel Holtmann if (!skb) 566a98e383SMarcel Holtmann return NULL; 576a98e383SMarcel Holtmann 586a98e383SMarcel Holtmann hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE); 596a98e383SMarcel Holtmann hdr->opcode = cpu_to_le16(opcode); 606a98e383SMarcel Holtmann hdr->plen = plen; 616a98e383SMarcel Holtmann 626a98e383SMarcel Holtmann if (plen) 636a98e383SMarcel Holtmann skb_put_data(skb, param, plen); 646a98e383SMarcel Holtmann 656a98e383SMarcel Holtmann bt_dev_dbg(hdev, "skb len %d", skb->len); 666a98e383SMarcel Holtmann 676a98e383SMarcel Holtmann hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; 686a98e383SMarcel Holtmann hci_skb_opcode(skb) = opcode; 696a98e383SMarcel Holtmann 70cba6b758SLuiz Augusto von Dentz /* Grab a reference if command needs to be associated with a sock (e.g. 71cba6b758SLuiz Augusto von Dentz * likely mgmt socket that initiated the command). 72cba6b758SLuiz Augusto von Dentz */ 73cba6b758SLuiz Augusto von Dentz if (sk) { 74cba6b758SLuiz Augusto von Dentz hci_skb_sk(skb) = sk; 75cba6b758SLuiz Augusto von Dentz sock_hold(sk); 76cba6b758SLuiz Augusto von Dentz } 77cba6b758SLuiz Augusto von Dentz 786a98e383SMarcel Holtmann return skb; 796a98e383SMarcel Holtmann } 806a98e383SMarcel Holtmann 816a98e383SMarcel Holtmann static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen, 826a98e383SMarcel Holtmann const void *param, u8 event, struct sock *sk) 836a98e383SMarcel Holtmann { 846a98e383SMarcel Holtmann struct hci_dev *hdev = req->hdev; 856a98e383SMarcel Holtmann struct sk_buff *skb; 866a98e383SMarcel Holtmann 876a98e383SMarcel Holtmann bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); 886a98e383SMarcel Holtmann 896a98e383SMarcel Holtmann /* If an error occurred during request building, there is no point in 906a98e383SMarcel Holtmann * queueing the HCI command. We can simply return. 916a98e383SMarcel Holtmann */ 926a98e383SMarcel Holtmann if (req->err) 936a98e383SMarcel Holtmann return; 946a98e383SMarcel Holtmann 956a98e383SMarcel Holtmann skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk); 966a98e383SMarcel Holtmann if (!skb) { 976a98e383SMarcel Holtmann bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)", 986a98e383SMarcel Holtmann opcode); 996a98e383SMarcel Holtmann req->err = -ENOMEM; 1006a98e383SMarcel Holtmann return; 1016a98e383SMarcel Holtmann } 1026a98e383SMarcel Holtmann 1036a98e383SMarcel Holtmann if (skb_queue_empty(&req->cmd_q)) 1046a98e383SMarcel Holtmann bt_cb(skb)->hci.req_flags |= HCI_REQ_START; 1056a98e383SMarcel Holtmann 10685b56857SLuiz Augusto von Dentz hci_skb_event(skb) = event; 1076a98e383SMarcel Holtmann 1086a98e383SMarcel Holtmann skb_queue_tail(&req->cmd_q, skb); 1096a98e383SMarcel Holtmann } 1106a98e383SMarcel Holtmann 1116a98e383SMarcel Holtmann static int hci_cmd_sync_run(struct hci_request *req) 1126a98e383SMarcel Holtmann { 1136a98e383SMarcel Holtmann struct hci_dev *hdev = req->hdev; 1146a98e383SMarcel Holtmann struct sk_buff *skb; 1156a98e383SMarcel Holtmann unsigned long flags; 1166a98e383SMarcel Holtmann 1176a98e383SMarcel Holtmann bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q)); 1186a98e383SMarcel Holtmann 1196a98e383SMarcel Holtmann /* If an error occurred during request building, remove all HCI 1206a98e383SMarcel Holtmann * commands queued on the HCI request queue. 1216a98e383SMarcel Holtmann */ 1226a98e383SMarcel Holtmann if (req->err) { 1236a98e383SMarcel Holtmann skb_queue_purge(&req->cmd_q); 1246a98e383SMarcel Holtmann return req->err; 1256a98e383SMarcel Holtmann } 1266a98e383SMarcel Holtmann 1276a98e383SMarcel Holtmann /* Do not allow empty requests */ 1286a98e383SMarcel Holtmann if (skb_queue_empty(&req->cmd_q)) 1296a98e383SMarcel Holtmann return -ENODATA; 1306a98e383SMarcel Holtmann 1316a98e383SMarcel Holtmann skb = skb_peek_tail(&req->cmd_q); 1326a98e383SMarcel Holtmann bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete; 1336a98e383SMarcel Holtmann bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB; 1346a98e383SMarcel Holtmann 1356a98e383SMarcel Holtmann spin_lock_irqsave(&hdev->cmd_q.lock, flags); 1366a98e383SMarcel Holtmann skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); 1376a98e383SMarcel Holtmann spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); 1386a98e383SMarcel Holtmann 1396a98e383SMarcel Holtmann queue_work(hdev->workqueue, &hdev->cmd_work); 1406a98e383SMarcel Holtmann 1416a98e383SMarcel Holtmann return 0; 1426a98e383SMarcel Holtmann } 1436a98e383SMarcel Holtmann 1446a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */ 1456a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen, 1466a98e383SMarcel Holtmann const void *param, u8 event, u32 timeout, 1476a98e383SMarcel Holtmann struct sock *sk) 1486a98e383SMarcel Holtmann { 1496a98e383SMarcel Holtmann struct hci_request req; 1506a98e383SMarcel Holtmann struct sk_buff *skb; 1516a98e383SMarcel Holtmann int err = 0; 1526a98e383SMarcel Holtmann 153d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Opcode 0x%4x", opcode); 1546a98e383SMarcel Holtmann 1556a98e383SMarcel Holtmann hci_req_init(&req, hdev); 1566a98e383SMarcel Holtmann 1576a98e383SMarcel Holtmann hci_cmd_sync_add(&req, opcode, plen, param, event, sk); 1586a98e383SMarcel Holtmann 1596a98e383SMarcel Holtmann hdev->req_status = HCI_REQ_PEND; 1606a98e383SMarcel Holtmann 1616a98e383SMarcel Holtmann err = hci_cmd_sync_run(&req); 1626a98e383SMarcel Holtmann if (err < 0) 1636a98e383SMarcel Holtmann return ERR_PTR(err); 1646a98e383SMarcel Holtmann 1656a98e383SMarcel Holtmann err = wait_event_interruptible_timeout(hdev->req_wait_q, 1666a98e383SMarcel Holtmann hdev->req_status != HCI_REQ_PEND, 1676a98e383SMarcel Holtmann timeout); 1686a98e383SMarcel Holtmann 1696a98e383SMarcel Holtmann if (err == -ERESTARTSYS) 1706a98e383SMarcel Holtmann return ERR_PTR(-EINTR); 1716a98e383SMarcel Holtmann 1726a98e383SMarcel Holtmann switch (hdev->req_status) { 1736a98e383SMarcel Holtmann case HCI_REQ_DONE: 1746a98e383SMarcel Holtmann err = -bt_to_errno(hdev->req_result); 1756a98e383SMarcel Holtmann break; 1766a98e383SMarcel Holtmann 1776a98e383SMarcel Holtmann case HCI_REQ_CANCELED: 1786a98e383SMarcel Holtmann err = -hdev->req_result; 1796a98e383SMarcel Holtmann break; 1806a98e383SMarcel Holtmann 1816a98e383SMarcel Holtmann default: 1826a98e383SMarcel Holtmann err = -ETIMEDOUT; 1836a98e383SMarcel Holtmann break; 1846a98e383SMarcel Holtmann } 1856a98e383SMarcel Holtmann 1866a98e383SMarcel Holtmann hdev->req_status = 0; 1876a98e383SMarcel Holtmann hdev->req_result = 0; 1886a98e383SMarcel Holtmann skb = hdev->req_skb; 1896a98e383SMarcel Holtmann hdev->req_skb = NULL; 1906a98e383SMarcel Holtmann 1916a98e383SMarcel Holtmann bt_dev_dbg(hdev, "end: err %d", err); 1926a98e383SMarcel Holtmann 1936a98e383SMarcel Holtmann if (err < 0) { 1946a98e383SMarcel Holtmann kfree_skb(skb); 1956a98e383SMarcel Holtmann return ERR_PTR(err); 1966a98e383SMarcel Holtmann } 1976a98e383SMarcel Holtmann 1986a98e383SMarcel Holtmann return skb; 1996a98e383SMarcel Holtmann } 2006a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_sk); 2016a98e383SMarcel Holtmann 2026a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */ 2036a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, 2046a98e383SMarcel Holtmann const void *param, u32 timeout) 2056a98e383SMarcel Holtmann { 2066a98e383SMarcel Holtmann return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL); 2076a98e383SMarcel Holtmann } 2086a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync); 2096a98e383SMarcel Holtmann 2106a98e383SMarcel Holtmann /* Send HCI command and wait for command complete event */ 2116a98e383SMarcel Holtmann struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, 2126a98e383SMarcel Holtmann const void *param, u32 timeout) 2136a98e383SMarcel Holtmann { 2146a98e383SMarcel Holtmann struct sk_buff *skb; 2156a98e383SMarcel Holtmann 2166a98e383SMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) 2176a98e383SMarcel Holtmann return ERR_PTR(-ENETDOWN); 2186a98e383SMarcel Holtmann 2196a98e383SMarcel Holtmann bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); 2206a98e383SMarcel Holtmann 2216a98e383SMarcel Holtmann hci_req_sync_lock(hdev); 2226a98e383SMarcel Holtmann skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout); 2236a98e383SMarcel Holtmann hci_req_sync_unlock(hdev); 2246a98e383SMarcel Holtmann 2256a98e383SMarcel Holtmann return skb; 2266a98e383SMarcel Holtmann } 2276a98e383SMarcel Holtmann EXPORT_SYMBOL(hci_cmd_sync); 2286a98e383SMarcel Holtmann 2296a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */ 2306a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, 2316a98e383SMarcel Holtmann const void *param, u8 event, u32 timeout) 2326a98e383SMarcel Holtmann { 2336a98e383SMarcel Holtmann return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, 2346a98e383SMarcel Holtmann NULL); 2356a98e383SMarcel Holtmann } 2366a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_ev); 2376a98e383SMarcel Holtmann 2386a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */ 2396a98e383SMarcel Holtmann int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen, 2406a98e383SMarcel Holtmann const void *param, u8 event, u32 timeout, 2416a98e383SMarcel Holtmann struct sock *sk) 2426a98e383SMarcel Holtmann { 2436a98e383SMarcel Holtmann struct sk_buff *skb; 2446a98e383SMarcel Holtmann u8 status; 2456a98e383SMarcel Holtmann 2466a98e383SMarcel Holtmann skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk); 247abfeea47SLuiz Augusto von Dentz if (IS_ERR(skb)) { 2486a98e383SMarcel Holtmann bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode, 2496a98e383SMarcel Holtmann PTR_ERR(skb)); 2506a98e383SMarcel Holtmann return PTR_ERR(skb); 2516a98e383SMarcel Holtmann } 2526a98e383SMarcel Holtmann 253abfeea47SLuiz Augusto von Dentz /* If command return a status event skb will be set to NULL as there are 254abfeea47SLuiz Augusto von Dentz * no parameters, in case of failure IS_ERR(skb) would have be set to 255abfeea47SLuiz Augusto von Dentz * the actual error would be found with PTR_ERR(skb). 256abfeea47SLuiz Augusto von Dentz */ 257abfeea47SLuiz Augusto von Dentz if (!skb) 258abfeea47SLuiz Augusto von Dentz return 0; 259abfeea47SLuiz Augusto von Dentz 2606a98e383SMarcel Holtmann status = skb->data[0]; 2616a98e383SMarcel Holtmann 2626a98e383SMarcel Holtmann kfree_skb(skb); 2636a98e383SMarcel Holtmann 2646a98e383SMarcel Holtmann return status; 2656a98e383SMarcel Holtmann } 2666a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_status_sk); 2676a98e383SMarcel Holtmann 2686a98e383SMarcel Holtmann int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen, 2696a98e383SMarcel Holtmann const void *param, u32 timeout) 2706a98e383SMarcel Holtmann { 2716a98e383SMarcel Holtmann return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout, 2726a98e383SMarcel Holtmann NULL); 2736a98e383SMarcel Holtmann } 2746a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_status); 2756a98e383SMarcel Holtmann 2766a98e383SMarcel Holtmann static void hci_cmd_sync_work(struct work_struct *work) 2776a98e383SMarcel Holtmann { 2786a98e383SMarcel Holtmann struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work); 2796a98e383SMarcel Holtmann 2806a98e383SMarcel Holtmann bt_dev_dbg(hdev, ""); 2816a98e383SMarcel Holtmann 282008ee9ebSLuiz Augusto von Dentz /* Dequeue all entries and run them */ 283008ee9ebSLuiz Augusto von Dentz while (1) { 284008ee9ebSLuiz Augusto von Dentz struct hci_cmd_sync_work_entry *entry; 285008ee9ebSLuiz Augusto von Dentz 2866a98e383SMarcel Holtmann mutex_lock(&hdev->cmd_sync_work_lock); 287008ee9ebSLuiz Augusto von Dentz entry = list_first_entry_or_null(&hdev->cmd_sync_work_list, 288008ee9ebSLuiz Augusto von Dentz struct hci_cmd_sync_work_entry, 289008ee9ebSLuiz Augusto von Dentz list); 290008ee9ebSLuiz Augusto von Dentz if (entry) 2916a98e383SMarcel Holtmann list_del(&entry->list); 2926a98e383SMarcel Holtmann mutex_unlock(&hdev->cmd_sync_work_lock); 2936a98e383SMarcel Holtmann 294008ee9ebSLuiz Augusto von Dentz if (!entry) 295008ee9ebSLuiz Augusto von Dentz break; 296008ee9ebSLuiz Augusto von Dentz 297008ee9ebSLuiz Augusto von Dentz bt_dev_dbg(hdev, "entry %p", entry); 298008ee9ebSLuiz Augusto von Dentz 299008ee9ebSLuiz Augusto von Dentz if (entry->func) { 3006a98e383SMarcel Holtmann int err; 3016a98e383SMarcel Holtmann 3026a98e383SMarcel Holtmann hci_req_sync_lock(hdev); 303008ee9ebSLuiz Augusto von Dentz err = entry->func(hdev, entry->data); 304008ee9ebSLuiz Augusto von Dentz if (entry->destroy) 305008ee9ebSLuiz Augusto von Dentz entry->destroy(hdev, entry->data, err); 3066a98e383SMarcel Holtmann hci_req_sync_unlock(hdev); 3076a98e383SMarcel Holtmann } 308008ee9ebSLuiz Augusto von Dentz 309008ee9ebSLuiz Augusto von Dentz kfree(entry); 310008ee9ebSLuiz Augusto von Dentz } 3116a98e383SMarcel Holtmann } 3126a98e383SMarcel Holtmann 313744451c1SBenjamin Berg static void hci_cmd_sync_cancel_work(struct work_struct *work) 314744451c1SBenjamin Berg { 315744451c1SBenjamin Berg struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work); 316744451c1SBenjamin Berg 317744451c1SBenjamin Berg cancel_delayed_work_sync(&hdev->cmd_timer); 318744451c1SBenjamin Berg cancel_delayed_work_sync(&hdev->ncmd_timer); 319744451c1SBenjamin Berg atomic_set(&hdev->cmd_cnt, 1); 320744451c1SBenjamin Berg 321744451c1SBenjamin Berg wake_up_interruptible(&hdev->req_wait_q); 322744451c1SBenjamin Berg } 323744451c1SBenjamin Berg 324*8ffde2a7SBrian Gix static int hci_scan_disable_sync(struct hci_dev *hdev); 325*8ffde2a7SBrian Gix static int scan_disable_sync(struct hci_dev *hdev, void *data) 326*8ffde2a7SBrian Gix { 327*8ffde2a7SBrian Gix return hci_scan_disable_sync(hdev); 328*8ffde2a7SBrian Gix } 329*8ffde2a7SBrian Gix 330*8ffde2a7SBrian Gix static int hci_inquiry_sync(struct hci_dev *hdev, u8 length); 331*8ffde2a7SBrian Gix static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data) 332*8ffde2a7SBrian Gix { 333*8ffde2a7SBrian Gix return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN); 334*8ffde2a7SBrian Gix } 335*8ffde2a7SBrian Gix 336*8ffde2a7SBrian Gix static void le_scan_disable(struct work_struct *work) 337*8ffde2a7SBrian Gix { 338*8ffde2a7SBrian Gix struct hci_dev *hdev = container_of(work, struct hci_dev, 339*8ffde2a7SBrian Gix le_scan_disable.work); 340*8ffde2a7SBrian Gix int status; 341*8ffde2a7SBrian Gix 342*8ffde2a7SBrian Gix bt_dev_dbg(hdev, ""); 343*8ffde2a7SBrian Gix hci_dev_lock(hdev); 344*8ffde2a7SBrian Gix 345*8ffde2a7SBrian Gix if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) 346*8ffde2a7SBrian Gix goto _return; 347*8ffde2a7SBrian Gix 348*8ffde2a7SBrian Gix cancel_delayed_work(&hdev->le_scan_restart); 349*8ffde2a7SBrian Gix 350*8ffde2a7SBrian Gix status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL); 351*8ffde2a7SBrian Gix if (status) { 352*8ffde2a7SBrian Gix bt_dev_err(hdev, "failed to disable LE scan: %d", status); 353*8ffde2a7SBrian Gix goto _return; 354*8ffde2a7SBrian Gix } 355*8ffde2a7SBrian Gix 356*8ffde2a7SBrian Gix hdev->discovery.scan_start = 0; 357*8ffde2a7SBrian Gix 358*8ffde2a7SBrian Gix /* If we were running LE only scan, change discovery state. If 359*8ffde2a7SBrian Gix * we were running both LE and BR/EDR inquiry simultaneously, 360*8ffde2a7SBrian Gix * and BR/EDR inquiry is already finished, stop discovery, 361*8ffde2a7SBrian Gix * otherwise BR/EDR inquiry will stop discovery when finished. 362*8ffde2a7SBrian Gix * If we will resolve remote device name, do not change 363*8ffde2a7SBrian Gix * discovery state. 364*8ffde2a7SBrian Gix */ 365*8ffde2a7SBrian Gix 366*8ffde2a7SBrian Gix if (hdev->discovery.type == DISCOV_TYPE_LE) 367*8ffde2a7SBrian Gix goto discov_stopped; 368*8ffde2a7SBrian Gix 369*8ffde2a7SBrian Gix if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED) 370*8ffde2a7SBrian Gix goto _return; 371*8ffde2a7SBrian Gix 372*8ffde2a7SBrian Gix if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) { 373*8ffde2a7SBrian Gix if (!test_bit(HCI_INQUIRY, &hdev->flags) && 374*8ffde2a7SBrian Gix hdev->discovery.state != DISCOVERY_RESOLVING) 375*8ffde2a7SBrian Gix goto discov_stopped; 376*8ffde2a7SBrian Gix 377*8ffde2a7SBrian Gix goto _return; 378*8ffde2a7SBrian Gix } 379*8ffde2a7SBrian Gix 380*8ffde2a7SBrian Gix status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL); 381*8ffde2a7SBrian Gix if (status) { 382*8ffde2a7SBrian Gix bt_dev_err(hdev, "inquiry failed: status %d", status); 383*8ffde2a7SBrian Gix goto discov_stopped; 384*8ffde2a7SBrian Gix } 385*8ffde2a7SBrian Gix 386*8ffde2a7SBrian Gix goto _return; 387*8ffde2a7SBrian Gix 388*8ffde2a7SBrian Gix discov_stopped: 389*8ffde2a7SBrian Gix hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 390*8ffde2a7SBrian Gix 391*8ffde2a7SBrian Gix _return: 392*8ffde2a7SBrian Gix hci_dev_unlock(hdev); 393*8ffde2a7SBrian Gix } 394*8ffde2a7SBrian Gix 3956a98e383SMarcel Holtmann void hci_cmd_sync_init(struct hci_dev *hdev) 3966a98e383SMarcel Holtmann { 3976a98e383SMarcel Holtmann INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work); 3986a98e383SMarcel Holtmann INIT_LIST_HEAD(&hdev->cmd_sync_work_list); 3996a98e383SMarcel Holtmann mutex_init(&hdev->cmd_sync_work_lock); 400744451c1SBenjamin Berg 401744451c1SBenjamin Berg INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work); 402*8ffde2a7SBrian Gix INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable); 4036a98e383SMarcel Holtmann } 4046a98e383SMarcel Holtmann 4056a98e383SMarcel Holtmann void hci_cmd_sync_clear(struct hci_dev *hdev) 4066a98e383SMarcel Holtmann { 4076a98e383SMarcel Holtmann struct hci_cmd_sync_work_entry *entry, *tmp; 4086a98e383SMarcel Holtmann 4096a98e383SMarcel Holtmann cancel_work_sync(&hdev->cmd_sync_work); 4106a98e383SMarcel Holtmann 4116a98e383SMarcel Holtmann list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) { 4126a98e383SMarcel Holtmann if (entry->destroy) 4136a98e383SMarcel Holtmann entry->destroy(hdev, entry->data, -ECANCELED); 4146a98e383SMarcel Holtmann 4156a98e383SMarcel Holtmann list_del(&entry->list); 4166a98e383SMarcel Holtmann kfree(entry); 4176a98e383SMarcel Holtmann } 4186a98e383SMarcel Holtmann } 4196a98e383SMarcel Holtmann 420744451c1SBenjamin Berg void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err) 421914b08b3SBenjamin Berg { 422914b08b3SBenjamin Berg bt_dev_dbg(hdev, "err 0x%2.2x", err); 423914b08b3SBenjamin Berg 424914b08b3SBenjamin Berg if (hdev->req_status == HCI_REQ_PEND) { 425914b08b3SBenjamin Berg hdev->req_result = err; 426914b08b3SBenjamin Berg hdev->req_status = HCI_REQ_CANCELED; 427914b08b3SBenjamin Berg 428914b08b3SBenjamin Berg cancel_delayed_work_sync(&hdev->cmd_timer); 429914b08b3SBenjamin Berg cancel_delayed_work_sync(&hdev->ncmd_timer); 430914b08b3SBenjamin Berg atomic_set(&hdev->cmd_cnt, 1); 431914b08b3SBenjamin Berg 432914b08b3SBenjamin Berg wake_up_interruptible(&hdev->req_wait_q); 433914b08b3SBenjamin Berg } 434914b08b3SBenjamin Berg } 435744451c1SBenjamin Berg 436744451c1SBenjamin Berg void hci_cmd_sync_cancel(struct hci_dev *hdev, int err) 437744451c1SBenjamin Berg { 438744451c1SBenjamin Berg bt_dev_dbg(hdev, "err 0x%2.2x", err); 439744451c1SBenjamin Berg 440744451c1SBenjamin Berg if (hdev->req_status == HCI_REQ_PEND) { 441744451c1SBenjamin Berg hdev->req_result = err; 442744451c1SBenjamin Berg hdev->req_status = HCI_REQ_CANCELED; 443744451c1SBenjamin Berg 444744451c1SBenjamin Berg queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work); 445744451c1SBenjamin Berg } 446744451c1SBenjamin Berg } 447914b08b3SBenjamin Berg EXPORT_SYMBOL(hci_cmd_sync_cancel); 448914b08b3SBenjamin Berg 4496a98e383SMarcel Holtmann int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func, 4506a98e383SMarcel Holtmann void *data, hci_cmd_sync_work_destroy_t destroy) 4516a98e383SMarcel Holtmann { 4526a98e383SMarcel Holtmann struct hci_cmd_sync_work_entry *entry; 4536a98e383SMarcel Holtmann 4540b94f265SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) 4550b94f265SLuiz Augusto von Dentz return -ENODEV; 4560b94f265SLuiz Augusto von Dentz 4576a98e383SMarcel Holtmann entry = kmalloc(sizeof(*entry), GFP_KERNEL); 4586a98e383SMarcel Holtmann if (!entry) 4596a98e383SMarcel Holtmann return -ENOMEM; 4606a98e383SMarcel Holtmann 4616a98e383SMarcel Holtmann entry->func = func; 4626a98e383SMarcel Holtmann entry->data = data; 4636a98e383SMarcel Holtmann entry->destroy = destroy; 4646a98e383SMarcel Holtmann 4656a98e383SMarcel Holtmann mutex_lock(&hdev->cmd_sync_work_lock); 4666a98e383SMarcel Holtmann list_add_tail(&entry->list, &hdev->cmd_sync_work_list); 4676a98e383SMarcel Holtmann mutex_unlock(&hdev->cmd_sync_work_lock); 4686a98e383SMarcel Holtmann 4696a98e383SMarcel Holtmann queue_work(hdev->req_workqueue, &hdev->cmd_sync_work); 4706a98e383SMarcel Holtmann 4716a98e383SMarcel Holtmann return 0; 4726a98e383SMarcel Holtmann } 4736a98e383SMarcel Holtmann EXPORT_SYMBOL(hci_cmd_sync_queue); 474161510ccSLuiz Augusto von Dentz 475161510ccSLuiz Augusto von Dentz int hci_update_eir_sync(struct hci_dev *hdev) 476161510ccSLuiz Augusto von Dentz { 477161510ccSLuiz Augusto von Dentz struct hci_cp_write_eir cp; 478161510ccSLuiz Augusto von Dentz 479161510ccSLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 480161510ccSLuiz Augusto von Dentz 481161510ccSLuiz Augusto von Dentz if (!hdev_is_powered(hdev)) 482161510ccSLuiz Augusto von Dentz return 0; 483161510ccSLuiz Augusto von Dentz 484161510ccSLuiz Augusto von Dentz if (!lmp_ext_inq_capable(hdev)) 485161510ccSLuiz Augusto von Dentz return 0; 486161510ccSLuiz Augusto von Dentz 487161510ccSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) 488161510ccSLuiz Augusto von Dentz return 0; 489161510ccSLuiz Augusto von Dentz 490161510ccSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) 491161510ccSLuiz Augusto von Dentz return 0; 492161510ccSLuiz Augusto von Dentz 493161510ccSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 494161510ccSLuiz Augusto von Dentz 495161510ccSLuiz Augusto von Dentz eir_create(hdev, cp.data); 496161510ccSLuiz Augusto von Dentz 497161510ccSLuiz Augusto von Dentz if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) 498161510ccSLuiz Augusto von Dentz return 0; 499161510ccSLuiz Augusto von Dentz 500161510ccSLuiz Augusto von Dentz memcpy(hdev->eir, cp.data, sizeof(cp.data)); 501161510ccSLuiz Augusto von Dentz 502161510ccSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp, 503161510ccSLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 504161510ccSLuiz Augusto von Dentz } 505161510ccSLuiz Augusto von Dentz 506161510ccSLuiz Augusto von Dentz static u8 get_service_classes(struct hci_dev *hdev) 507161510ccSLuiz Augusto von Dentz { 508161510ccSLuiz Augusto von Dentz struct bt_uuid *uuid; 509161510ccSLuiz Augusto von Dentz u8 val = 0; 510161510ccSLuiz Augusto von Dentz 511161510ccSLuiz Augusto von Dentz list_for_each_entry(uuid, &hdev->uuids, list) 512161510ccSLuiz Augusto von Dentz val |= uuid->svc_hint; 513161510ccSLuiz Augusto von Dentz 514161510ccSLuiz Augusto von Dentz return val; 515161510ccSLuiz Augusto von Dentz } 516161510ccSLuiz Augusto von Dentz 517161510ccSLuiz Augusto von Dentz int hci_update_class_sync(struct hci_dev *hdev) 518161510ccSLuiz Augusto von Dentz { 519161510ccSLuiz Augusto von Dentz u8 cod[3]; 520161510ccSLuiz Augusto von Dentz 521161510ccSLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 522161510ccSLuiz Augusto von Dentz 523161510ccSLuiz Augusto von Dentz if (!hdev_is_powered(hdev)) 524161510ccSLuiz Augusto von Dentz return 0; 525161510ccSLuiz Augusto von Dentz 526161510ccSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 527161510ccSLuiz Augusto von Dentz return 0; 528161510ccSLuiz Augusto von Dentz 529161510ccSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) 530161510ccSLuiz Augusto von Dentz return 0; 531161510ccSLuiz Augusto von Dentz 532161510ccSLuiz Augusto von Dentz cod[0] = hdev->minor_class; 533161510ccSLuiz Augusto von Dentz cod[1] = hdev->major_class; 534161510ccSLuiz Augusto von Dentz cod[2] = get_service_classes(hdev); 535161510ccSLuiz Augusto von Dentz 536161510ccSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) 537161510ccSLuiz Augusto von Dentz cod[1] |= 0x20; 538161510ccSLuiz Augusto von Dentz 539161510ccSLuiz Augusto von Dentz if (memcmp(cod, hdev->dev_class, 3) == 0) 540161510ccSLuiz Augusto von Dentz return 0; 541161510ccSLuiz Augusto von Dentz 542161510ccSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV, 543161510ccSLuiz Augusto von Dentz sizeof(cod), cod, HCI_CMD_TIMEOUT); 544161510ccSLuiz Augusto von Dentz } 545cba6b758SLuiz Augusto von Dentz 546cba6b758SLuiz Augusto von Dentz static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable) 547cba6b758SLuiz Augusto von Dentz { 548cba6b758SLuiz Augusto von Dentz /* If there is no connection we are OK to advertise. */ 549cba6b758SLuiz Augusto von Dentz if (hci_conn_num(hdev, LE_LINK) == 0) 550cba6b758SLuiz Augusto von Dentz return true; 551cba6b758SLuiz Augusto von Dentz 552cba6b758SLuiz Augusto von Dentz /* Check le_states if there is any connection in peripheral role. */ 553cba6b758SLuiz Augusto von Dentz if (hdev->conn_hash.le_num_peripheral > 0) { 554cba6b758SLuiz Augusto von Dentz /* Peripheral connection state and non connectable mode 555cba6b758SLuiz Augusto von Dentz * bit 20. 556cba6b758SLuiz Augusto von Dentz */ 557cba6b758SLuiz Augusto von Dentz if (!connectable && !(hdev->le_states[2] & 0x10)) 558cba6b758SLuiz Augusto von Dentz return false; 559cba6b758SLuiz Augusto von Dentz 560cba6b758SLuiz Augusto von Dentz /* Peripheral connection state and connectable mode bit 38 561cba6b758SLuiz Augusto von Dentz * and scannable bit 21. 562cba6b758SLuiz Augusto von Dentz */ 563cba6b758SLuiz Augusto von Dentz if (connectable && (!(hdev->le_states[4] & 0x40) || 564cba6b758SLuiz Augusto von Dentz !(hdev->le_states[2] & 0x20))) 565cba6b758SLuiz Augusto von Dentz return false; 566cba6b758SLuiz Augusto von Dentz } 567cba6b758SLuiz Augusto von Dentz 568cba6b758SLuiz Augusto von Dentz /* Check le_states if there is any connection in central role. */ 569cba6b758SLuiz Augusto von Dentz if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) { 570cba6b758SLuiz Augusto von Dentz /* Central connection state and non connectable mode bit 18. */ 571cba6b758SLuiz Augusto von Dentz if (!connectable && !(hdev->le_states[2] & 0x02)) 572cba6b758SLuiz Augusto von Dentz return false; 573cba6b758SLuiz Augusto von Dentz 574cba6b758SLuiz Augusto von Dentz /* Central connection state and connectable mode bit 35 and 575cba6b758SLuiz Augusto von Dentz * scannable 19. 576cba6b758SLuiz Augusto von Dentz */ 577cba6b758SLuiz Augusto von Dentz if (connectable && (!(hdev->le_states[4] & 0x08) || 578cba6b758SLuiz Augusto von Dentz !(hdev->le_states[2] & 0x08))) 579cba6b758SLuiz Augusto von Dentz return false; 580cba6b758SLuiz Augusto von Dentz } 581cba6b758SLuiz Augusto von Dentz 582cba6b758SLuiz Augusto von Dentz return true; 583cba6b758SLuiz Augusto von Dentz } 584cba6b758SLuiz Augusto von Dentz 585cba6b758SLuiz Augusto von Dentz static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags) 586cba6b758SLuiz Augusto von Dentz { 587cba6b758SLuiz Augusto von Dentz /* If privacy is not enabled don't use RPA */ 588cba6b758SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) 589cba6b758SLuiz Augusto von Dentz return false; 590cba6b758SLuiz Augusto von Dentz 591cba6b758SLuiz Augusto von Dentz /* If basic privacy mode is enabled use RPA */ 592cba6b758SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) 593cba6b758SLuiz Augusto von Dentz return true; 594cba6b758SLuiz Augusto von Dentz 595cba6b758SLuiz Augusto von Dentz /* If limited privacy mode is enabled don't use RPA if we're 596cba6b758SLuiz Augusto von Dentz * both discoverable and bondable. 597cba6b758SLuiz Augusto von Dentz */ 598cba6b758SLuiz Augusto von Dentz if ((flags & MGMT_ADV_FLAG_DISCOV) && 599cba6b758SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_BONDABLE)) 600cba6b758SLuiz Augusto von Dentz return false; 601cba6b758SLuiz Augusto von Dentz 602cba6b758SLuiz Augusto von Dentz /* We're neither bondable nor discoverable in the limited 603cba6b758SLuiz Augusto von Dentz * privacy mode, therefore use RPA. 604cba6b758SLuiz Augusto von Dentz */ 605cba6b758SLuiz Augusto von Dentz return true; 606cba6b758SLuiz Augusto von Dentz } 607cba6b758SLuiz Augusto von Dentz 608cba6b758SLuiz Augusto von Dentz static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa) 609cba6b758SLuiz Augusto von Dentz { 610cba6b758SLuiz Augusto von Dentz /* If we're advertising or initiating an LE connection we can't 611cba6b758SLuiz Augusto von Dentz * go ahead and change the random address at this time. This is 612cba6b758SLuiz Augusto von Dentz * because the eventual initiator address used for the 613cba6b758SLuiz Augusto von Dentz * subsequently created connection will be undefined (some 614cba6b758SLuiz Augusto von Dentz * controllers use the new address and others the one we had 615cba6b758SLuiz Augusto von Dentz * when the operation started). 616cba6b758SLuiz Augusto von Dentz * 617cba6b758SLuiz Augusto von Dentz * In this kind of scenario skip the update and let the random 618cba6b758SLuiz Augusto von Dentz * address be updated at the next cycle. 619cba6b758SLuiz Augusto von Dentz */ 620cba6b758SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LE_ADV) || 621cba6b758SLuiz Augusto von Dentz hci_lookup_le_connect(hdev)) { 622cba6b758SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Deferring random address update"); 623cba6b758SLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); 624cba6b758SLuiz Augusto von Dentz return 0; 625cba6b758SLuiz Augusto von Dentz } 626cba6b758SLuiz Augusto von Dentz 627cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR, 628cba6b758SLuiz Augusto von Dentz 6, rpa, HCI_CMD_TIMEOUT); 629cba6b758SLuiz Augusto von Dentz } 630cba6b758SLuiz Augusto von Dentz 631cba6b758SLuiz Augusto von Dentz int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy, 632cba6b758SLuiz Augusto von Dentz bool rpa, u8 *own_addr_type) 633cba6b758SLuiz Augusto von Dentz { 634cba6b758SLuiz Augusto von Dentz int err; 635cba6b758SLuiz Augusto von Dentz 636cba6b758SLuiz Augusto von Dentz /* If privacy is enabled use a resolvable private address. If 637cba6b758SLuiz Augusto von Dentz * current RPA has expired or there is something else than 638cba6b758SLuiz Augusto von Dentz * the current RPA in use, then generate a new one. 639cba6b758SLuiz Augusto von Dentz */ 640cba6b758SLuiz Augusto von Dentz if (rpa) { 641cba6b758SLuiz Augusto von Dentz /* If Controller supports LL Privacy use own address type is 642cba6b758SLuiz Augusto von Dentz * 0x03 643cba6b758SLuiz Augusto von Dentz */ 644ad383c2cSLuiz Augusto von Dentz if (use_ll_privacy(hdev)) 645cba6b758SLuiz Augusto von Dentz *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED; 646cba6b758SLuiz Augusto von Dentz else 647cba6b758SLuiz Augusto von Dentz *own_addr_type = ADDR_LE_DEV_RANDOM; 648cba6b758SLuiz Augusto von Dentz 649cba6b758SLuiz Augusto von Dentz /* Check if RPA is valid */ 650cba6b758SLuiz Augusto von Dentz if (rpa_valid(hdev)) 651cba6b758SLuiz Augusto von Dentz return 0; 652cba6b758SLuiz Augusto von Dentz 653cba6b758SLuiz Augusto von Dentz err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa); 654cba6b758SLuiz Augusto von Dentz if (err < 0) { 655cba6b758SLuiz Augusto von Dentz bt_dev_err(hdev, "failed to generate new RPA"); 656cba6b758SLuiz Augusto von Dentz return err; 657cba6b758SLuiz Augusto von Dentz } 658cba6b758SLuiz Augusto von Dentz 659cba6b758SLuiz Augusto von Dentz err = hci_set_random_addr_sync(hdev, &hdev->rpa); 660cba6b758SLuiz Augusto von Dentz if (err) 661cba6b758SLuiz Augusto von Dentz return err; 662cba6b758SLuiz Augusto von Dentz 663cba6b758SLuiz Augusto von Dentz return 0; 664cba6b758SLuiz Augusto von Dentz } 665cba6b758SLuiz Augusto von Dentz 666cba6b758SLuiz Augusto von Dentz /* In case of required privacy without resolvable private address, 667cba6b758SLuiz Augusto von Dentz * use an non-resolvable private address. This is useful for active 668cba6b758SLuiz Augusto von Dentz * scanning and non-connectable advertising. 669cba6b758SLuiz Augusto von Dentz */ 670cba6b758SLuiz Augusto von Dentz if (require_privacy) { 671cba6b758SLuiz Augusto von Dentz bdaddr_t nrpa; 672cba6b758SLuiz Augusto von Dentz 673cba6b758SLuiz Augusto von Dentz while (true) { 674cba6b758SLuiz Augusto von Dentz /* The non-resolvable private address is generated 675cba6b758SLuiz Augusto von Dentz * from random six bytes with the two most significant 676cba6b758SLuiz Augusto von Dentz * bits cleared. 677cba6b758SLuiz Augusto von Dentz */ 678cba6b758SLuiz Augusto von Dentz get_random_bytes(&nrpa, 6); 679cba6b758SLuiz Augusto von Dentz nrpa.b[5] &= 0x3f; 680cba6b758SLuiz Augusto von Dentz 681cba6b758SLuiz Augusto von Dentz /* The non-resolvable private address shall not be 682cba6b758SLuiz Augusto von Dentz * equal to the public address. 683cba6b758SLuiz Augusto von Dentz */ 684cba6b758SLuiz Augusto von Dentz if (bacmp(&hdev->bdaddr, &nrpa)) 685cba6b758SLuiz Augusto von Dentz break; 686cba6b758SLuiz Augusto von Dentz } 687cba6b758SLuiz Augusto von Dentz 688cba6b758SLuiz Augusto von Dentz *own_addr_type = ADDR_LE_DEV_RANDOM; 689cba6b758SLuiz Augusto von Dentz 690cba6b758SLuiz Augusto von Dentz return hci_set_random_addr_sync(hdev, &nrpa); 691cba6b758SLuiz Augusto von Dentz } 692cba6b758SLuiz Augusto von Dentz 693cba6b758SLuiz Augusto von Dentz /* If forcing static address is in use or there is no public 694cba6b758SLuiz Augusto von Dentz * address use the static address as random address (but skip 695cba6b758SLuiz Augusto von Dentz * the HCI command if the current random address is already the 696cba6b758SLuiz Augusto von Dentz * static one. 697cba6b758SLuiz Augusto von Dentz * 698cba6b758SLuiz Augusto von Dentz * In case BR/EDR has been disabled on a dual-mode controller 699cba6b758SLuiz Augusto von Dentz * and a static address has been configured, then use that 700cba6b758SLuiz Augusto von Dentz * address instead of the public BR/EDR address. 701cba6b758SLuiz Augusto von Dentz */ 702cba6b758SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || 703cba6b758SLuiz Augusto von Dentz !bacmp(&hdev->bdaddr, BDADDR_ANY) || 704cba6b758SLuiz Augusto von Dentz (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && 705cba6b758SLuiz Augusto von Dentz bacmp(&hdev->static_addr, BDADDR_ANY))) { 706cba6b758SLuiz Augusto von Dentz *own_addr_type = ADDR_LE_DEV_RANDOM; 707cba6b758SLuiz Augusto von Dentz if (bacmp(&hdev->static_addr, &hdev->random_addr)) 708cba6b758SLuiz Augusto von Dentz return hci_set_random_addr_sync(hdev, 709cba6b758SLuiz Augusto von Dentz &hdev->static_addr); 710cba6b758SLuiz Augusto von Dentz return 0; 711cba6b758SLuiz Augusto von Dentz } 712cba6b758SLuiz Augusto von Dentz 713cba6b758SLuiz Augusto von Dentz /* Neither privacy nor static address is being used so use a 714cba6b758SLuiz Augusto von Dentz * public address. 715cba6b758SLuiz Augusto von Dentz */ 716cba6b758SLuiz Augusto von Dentz *own_addr_type = ADDR_LE_DEV_PUBLIC; 717cba6b758SLuiz Augusto von Dentz 718cba6b758SLuiz Augusto von Dentz return 0; 719cba6b758SLuiz Augusto von Dentz } 720cba6b758SLuiz Augusto von Dentz 721cba6b758SLuiz Augusto von Dentz static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) 722cba6b758SLuiz Augusto von Dentz { 723cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_ext_adv_enable *cp; 724cba6b758SLuiz Augusto von Dentz struct hci_cp_ext_adv_set *set; 725cba6b758SLuiz Augusto von Dentz u8 data[sizeof(*cp) + sizeof(*set) * 1]; 726cba6b758SLuiz Augusto von Dentz u8 size; 727cba6b758SLuiz Augusto von Dentz 728cba6b758SLuiz Augusto von Dentz /* If request specifies an instance that doesn't exist, fail */ 729cba6b758SLuiz Augusto von Dentz if (instance > 0) { 730cba6b758SLuiz Augusto von Dentz struct adv_info *adv; 731cba6b758SLuiz Augusto von Dentz 732cba6b758SLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance); 733cba6b758SLuiz Augusto von Dentz if (!adv) 734cba6b758SLuiz Augusto von Dentz return -EINVAL; 735cba6b758SLuiz Augusto von Dentz 736cba6b758SLuiz Augusto von Dentz /* If not enabled there is nothing to do */ 737cba6b758SLuiz Augusto von Dentz if (!adv->enabled) 738cba6b758SLuiz Augusto von Dentz return 0; 739cba6b758SLuiz Augusto von Dentz } 740cba6b758SLuiz Augusto von Dentz 741cba6b758SLuiz Augusto von Dentz memset(data, 0, sizeof(data)); 742cba6b758SLuiz Augusto von Dentz 743cba6b758SLuiz Augusto von Dentz cp = (void *)data; 744cba6b758SLuiz Augusto von Dentz set = (void *)cp->data; 745cba6b758SLuiz Augusto von Dentz 746cba6b758SLuiz Augusto von Dentz /* Instance 0x00 indicates all advertising instances will be disabled */ 747cba6b758SLuiz Augusto von Dentz cp->num_of_sets = !!instance; 748cba6b758SLuiz Augusto von Dentz cp->enable = 0x00; 749cba6b758SLuiz Augusto von Dentz 750cba6b758SLuiz Augusto von Dentz set->handle = instance; 751cba6b758SLuiz Augusto von Dentz 752cba6b758SLuiz Augusto von Dentz size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets; 753cba6b758SLuiz Augusto von Dentz 754cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, 755cba6b758SLuiz Augusto von Dentz size, data, HCI_CMD_TIMEOUT); 756cba6b758SLuiz Augusto von Dentz } 757cba6b758SLuiz Augusto von Dentz 758cba6b758SLuiz Augusto von Dentz static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance, 759cba6b758SLuiz Augusto von Dentz bdaddr_t *random_addr) 760cba6b758SLuiz Augusto von Dentz { 761cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_adv_set_rand_addr cp; 762cba6b758SLuiz Augusto von Dentz int err; 763cba6b758SLuiz Augusto von Dentz 764cba6b758SLuiz Augusto von Dentz if (!instance) { 765cba6b758SLuiz Augusto von Dentz /* Instance 0x00 doesn't have an adv_info, instead it uses 766cba6b758SLuiz Augusto von Dentz * hdev->random_addr to track its address so whenever it needs 767cba6b758SLuiz Augusto von Dentz * to be updated this also set the random address since 768cba6b758SLuiz Augusto von Dentz * hdev->random_addr is shared with scan state machine. 769cba6b758SLuiz Augusto von Dentz */ 770cba6b758SLuiz Augusto von Dentz err = hci_set_random_addr_sync(hdev, random_addr); 771cba6b758SLuiz Augusto von Dentz if (err) 772cba6b758SLuiz Augusto von Dentz return err; 773cba6b758SLuiz Augusto von Dentz } 774cba6b758SLuiz Augusto von Dentz 775cba6b758SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 776cba6b758SLuiz Augusto von Dentz 777cba6b758SLuiz Augusto von Dentz cp.handle = instance; 778cba6b758SLuiz Augusto von Dentz bacpy(&cp.bdaddr, random_addr); 779cba6b758SLuiz Augusto von Dentz 780cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR, 781cba6b758SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 782cba6b758SLuiz Augusto von Dentz } 783cba6b758SLuiz Augusto von Dentz 784cba6b758SLuiz Augusto von Dentz int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) 785cba6b758SLuiz Augusto von Dentz { 786cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_ext_adv_params cp; 787cba6b758SLuiz Augusto von Dentz bool connectable; 788cba6b758SLuiz Augusto von Dentz u32 flags; 789cba6b758SLuiz Augusto von Dentz bdaddr_t random_addr; 790cba6b758SLuiz Augusto von Dentz u8 own_addr_type; 791cba6b758SLuiz Augusto von Dentz int err; 792cba6b758SLuiz Augusto von Dentz struct adv_info *adv; 793cba6b758SLuiz Augusto von Dentz bool secondary_adv; 794cba6b758SLuiz Augusto von Dentz 795cba6b758SLuiz Augusto von Dentz if (instance > 0) { 796cba6b758SLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance); 797cba6b758SLuiz Augusto von Dentz if (!adv) 798cba6b758SLuiz Augusto von Dentz return -EINVAL; 799cba6b758SLuiz Augusto von Dentz } else { 800cba6b758SLuiz Augusto von Dentz adv = NULL; 801cba6b758SLuiz Augusto von Dentz } 802cba6b758SLuiz Augusto von Dentz 803cba6b758SLuiz Augusto von Dentz /* Updating parameters of an active instance will return a 804cba6b758SLuiz Augusto von Dentz * Command Disallowed error, so we must first disable the 805cba6b758SLuiz Augusto von Dentz * instance if it is active. 806cba6b758SLuiz Augusto von Dentz */ 807cba6b758SLuiz Augusto von Dentz if (adv && !adv->pending) { 808cba6b758SLuiz Augusto von Dentz err = hci_disable_ext_adv_instance_sync(hdev, instance); 809cba6b758SLuiz Augusto von Dentz if (err) 810cba6b758SLuiz Augusto von Dentz return err; 811cba6b758SLuiz Augusto von Dentz } 812cba6b758SLuiz Augusto von Dentz 813cba6b758SLuiz Augusto von Dentz flags = hci_adv_instance_flags(hdev, instance); 814cba6b758SLuiz Augusto von Dentz 815cba6b758SLuiz Augusto von Dentz /* If the "connectable" instance flag was not set, then choose between 816cba6b758SLuiz Augusto von Dentz * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. 817cba6b758SLuiz Augusto von Dentz */ 818cba6b758SLuiz Augusto von Dentz connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || 819cba6b758SLuiz Augusto von Dentz mgmt_get_connectable(hdev); 820cba6b758SLuiz Augusto von Dentz 821cba6b758SLuiz Augusto von Dentz if (!is_advertising_allowed(hdev, connectable)) 822cba6b758SLuiz Augusto von Dentz return -EPERM; 823cba6b758SLuiz Augusto von Dentz 824cba6b758SLuiz Augusto von Dentz /* Set require_privacy to true only when non-connectable 825cba6b758SLuiz Augusto von Dentz * advertising is used. In that case it is fine to use a 826cba6b758SLuiz Augusto von Dentz * non-resolvable private address. 827cba6b758SLuiz Augusto von Dentz */ 828cba6b758SLuiz Augusto von Dentz err = hci_get_random_address(hdev, !connectable, 829cba6b758SLuiz Augusto von Dentz adv_use_rpa(hdev, flags), adv, 830cba6b758SLuiz Augusto von Dentz &own_addr_type, &random_addr); 831cba6b758SLuiz Augusto von Dentz if (err < 0) 832cba6b758SLuiz Augusto von Dentz return err; 833cba6b758SLuiz Augusto von Dentz 834cba6b758SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 835cba6b758SLuiz Augusto von Dentz 836cba6b758SLuiz Augusto von Dentz if (adv) { 837cba6b758SLuiz Augusto von Dentz hci_cpu_to_le24(adv->min_interval, cp.min_interval); 838cba6b758SLuiz Augusto von Dentz hci_cpu_to_le24(adv->max_interval, cp.max_interval); 839cba6b758SLuiz Augusto von Dentz cp.tx_power = adv->tx_power; 840cba6b758SLuiz Augusto von Dentz } else { 841cba6b758SLuiz Augusto von Dentz hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval); 842cba6b758SLuiz Augusto von Dentz hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval); 843cba6b758SLuiz Augusto von Dentz cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; 844cba6b758SLuiz Augusto von Dentz } 845cba6b758SLuiz Augusto von Dentz 846cba6b758SLuiz Augusto von Dentz secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK); 847cba6b758SLuiz Augusto von Dentz 848cba6b758SLuiz Augusto von Dentz if (connectable) { 849cba6b758SLuiz Augusto von Dentz if (secondary_adv) 850cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND); 851cba6b758SLuiz Augusto von Dentz else 852cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND); 853cba6b758SLuiz Augusto von Dentz } else if (hci_adv_instance_is_scannable(hdev, instance) || 854cba6b758SLuiz Augusto von Dentz (flags & MGMT_ADV_PARAM_SCAN_RSP)) { 855cba6b758SLuiz Augusto von Dentz if (secondary_adv) 856cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND); 857cba6b758SLuiz Augusto von Dentz else 858cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND); 859cba6b758SLuiz Augusto von Dentz } else { 860cba6b758SLuiz Augusto von Dentz if (secondary_adv) 861cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND); 862cba6b758SLuiz Augusto von Dentz else 863cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND); 864cba6b758SLuiz Augusto von Dentz } 865cba6b758SLuiz Augusto von Dentz 866cf75ad8bSLuiz Augusto von Dentz /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter 867cf75ad8bSLuiz Augusto von Dentz * contains the peer’s Identity Address and the Peer_Address_Type 868cf75ad8bSLuiz Augusto von Dentz * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01). 869cf75ad8bSLuiz Augusto von Dentz * These parameters are used to locate the corresponding local IRK in 870cf75ad8bSLuiz Augusto von Dentz * the resolving list; this IRK is used to generate their own address 871cf75ad8bSLuiz Augusto von Dentz * used in the advertisement. 872cf75ad8bSLuiz Augusto von Dentz */ 873cf75ad8bSLuiz Augusto von Dentz if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) 874cf75ad8bSLuiz Augusto von Dentz hci_copy_identity_address(hdev, &cp.peer_addr, 875cf75ad8bSLuiz Augusto von Dentz &cp.peer_addr_type); 876cf75ad8bSLuiz Augusto von Dentz 877cba6b758SLuiz Augusto von Dentz cp.own_addr_type = own_addr_type; 878cba6b758SLuiz Augusto von Dentz cp.channel_map = hdev->le_adv_channel_map; 879cba6b758SLuiz Augusto von Dentz cp.handle = instance; 880cba6b758SLuiz Augusto von Dentz 881cba6b758SLuiz Augusto von Dentz if (flags & MGMT_ADV_FLAG_SEC_2M) { 882cba6b758SLuiz Augusto von Dentz cp.primary_phy = HCI_ADV_PHY_1M; 883cba6b758SLuiz Augusto von Dentz cp.secondary_phy = HCI_ADV_PHY_2M; 884cba6b758SLuiz Augusto von Dentz } else if (flags & MGMT_ADV_FLAG_SEC_CODED) { 885cba6b758SLuiz Augusto von Dentz cp.primary_phy = HCI_ADV_PHY_CODED; 886cba6b758SLuiz Augusto von Dentz cp.secondary_phy = HCI_ADV_PHY_CODED; 887cba6b758SLuiz Augusto von Dentz } else { 888cba6b758SLuiz Augusto von Dentz /* In all other cases use 1M */ 889cba6b758SLuiz Augusto von Dentz cp.primary_phy = HCI_ADV_PHY_1M; 890cba6b758SLuiz Augusto von Dentz cp.secondary_phy = HCI_ADV_PHY_1M; 891cba6b758SLuiz Augusto von Dentz } 892cba6b758SLuiz Augusto von Dentz 893cba6b758SLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, 894cba6b758SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 895cba6b758SLuiz Augusto von Dentz if (err) 896cba6b758SLuiz Augusto von Dentz return err; 897cba6b758SLuiz Augusto von Dentz 898cba6b758SLuiz Augusto von Dentz if ((own_addr_type == ADDR_LE_DEV_RANDOM || 899cba6b758SLuiz Augusto von Dentz own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) && 900cba6b758SLuiz Augusto von Dentz bacmp(&random_addr, BDADDR_ANY)) { 901cba6b758SLuiz Augusto von Dentz /* Check if random address need to be updated */ 902cba6b758SLuiz Augusto von Dentz if (adv) { 903cba6b758SLuiz Augusto von Dentz if (!bacmp(&random_addr, &adv->random_addr)) 904cba6b758SLuiz Augusto von Dentz return 0; 905cba6b758SLuiz Augusto von Dentz } else { 906cba6b758SLuiz Augusto von Dentz if (!bacmp(&random_addr, &hdev->random_addr)) 907cba6b758SLuiz Augusto von Dentz return 0; 908cba6b758SLuiz Augusto von Dentz } 909cba6b758SLuiz Augusto von Dentz 910cba6b758SLuiz Augusto von Dentz return hci_set_adv_set_random_addr_sync(hdev, instance, 911cba6b758SLuiz Augusto von Dentz &random_addr); 912cba6b758SLuiz Augusto von Dentz } 913cba6b758SLuiz Augusto von Dentz 914cba6b758SLuiz Augusto von Dentz return 0; 915cba6b758SLuiz Augusto von Dentz } 916cba6b758SLuiz Augusto von Dentz 917cba6b758SLuiz Augusto von Dentz static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) 918cba6b758SLuiz Augusto von Dentz { 919cba6b758SLuiz Augusto von Dentz struct { 920cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_ext_scan_rsp_data cp; 921cba6b758SLuiz Augusto von Dentz u8 data[HCI_MAX_EXT_AD_LENGTH]; 922cba6b758SLuiz Augusto von Dentz } pdu; 923cba6b758SLuiz Augusto von Dentz u8 len; 92434a718bcSLuiz Augusto von Dentz struct adv_info *adv = NULL; 92534a718bcSLuiz Augusto von Dentz int err; 926cba6b758SLuiz Augusto von Dentz 927cba6b758SLuiz Augusto von Dentz memset(&pdu, 0, sizeof(pdu)); 928cba6b758SLuiz Augusto von Dentz 92934a718bcSLuiz Augusto von Dentz if (instance) { 93034a718bcSLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance); 93134a718bcSLuiz Augusto von Dentz if (!adv || !adv->scan_rsp_changed) 932cba6b758SLuiz Augusto von Dentz return 0; 93334a718bcSLuiz Augusto von Dentz } 934cba6b758SLuiz Augusto von Dentz 93534a718bcSLuiz Augusto von Dentz len = eir_create_scan_rsp(hdev, instance, pdu.data); 936cba6b758SLuiz Augusto von Dentz 937cba6b758SLuiz Augusto von Dentz pdu.cp.handle = instance; 938cba6b758SLuiz Augusto von Dentz pdu.cp.length = len; 939cba6b758SLuiz Augusto von Dentz pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; 940cba6b758SLuiz Augusto von Dentz pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; 941cba6b758SLuiz Augusto von Dentz 94234a718bcSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, 943cba6b758SLuiz Augusto von Dentz sizeof(pdu.cp) + len, &pdu.cp, 944cba6b758SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 94534a718bcSLuiz Augusto von Dentz if (err) 94634a718bcSLuiz Augusto von Dentz return err; 94734a718bcSLuiz Augusto von Dentz 94834a718bcSLuiz Augusto von Dentz if (adv) { 94934a718bcSLuiz Augusto von Dentz adv->scan_rsp_changed = false; 95034a718bcSLuiz Augusto von Dentz } else { 95134a718bcSLuiz Augusto von Dentz memcpy(hdev->scan_rsp_data, pdu.data, len); 95234a718bcSLuiz Augusto von Dentz hdev->scan_rsp_data_len = len; 95334a718bcSLuiz Augusto von Dentz } 95434a718bcSLuiz Augusto von Dentz 95534a718bcSLuiz Augusto von Dentz return 0; 956cba6b758SLuiz Augusto von Dentz } 957cba6b758SLuiz Augusto von Dentz 958cba6b758SLuiz Augusto von Dentz static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) 959cba6b758SLuiz Augusto von Dentz { 960cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_scan_rsp_data cp; 961cba6b758SLuiz Augusto von Dentz u8 len; 962cba6b758SLuiz Augusto von Dentz 963cba6b758SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 964cba6b758SLuiz Augusto von Dentz 965cba6b758SLuiz Augusto von Dentz len = eir_create_scan_rsp(hdev, instance, cp.data); 966cba6b758SLuiz Augusto von Dentz 967cba6b758SLuiz Augusto von Dentz if (hdev->scan_rsp_data_len == len && 968cba6b758SLuiz Augusto von Dentz !memcmp(cp.data, hdev->scan_rsp_data, len)) 969cba6b758SLuiz Augusto von Dentz return 0; 970cba6b758SLuiz Augusto von Dentz 971cba6b758SLuiz Augusto von Dentz memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); 972cba6b758SLuiz Augusto von Dentz hdev->scan_rsp_data_len = len; 973cba6b758SLuiz Augusto von Dentz 974cba6b758SLuiz Augusto von Dentz cp.length = len; 975cba6b758SLuiz Augusto von Dentz 976cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA, 977cba6b758SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 978cba6b758SLuiz Augusto von Dentz } 979cba6b758SLuiz Augusto von Dentz 980cba6b758SLuiz Augusto von Dentz int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) 981cba6b758SLuiz Augusto von Dentz { 982cba6b758SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 983cba6b758SLuiz Augusto von Dentz return 0; 984cba6b758SLuiz Augusto von Dentz 985cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 986cba6b758SLuiz Augusto von Dentz return hci_set_ext_scan_rsp_data_sync(hdev, instance); 987cba6b758SLuiz Augusto von Dentz 988cba6b758SLuiz Augusto von Dentz return __hci_set_scan_rsp_data_sync(hdev, instance); 989cba6b758SLuiz Augusto von Dentz } 990cba6b758SLuiz Augusto von Dentz 991cba6b758SLuiz Augusto von Dentz int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance) 992cba6b758SLuiz Augusto von Dentz { 993cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_ext_adv_enable *cp; 994cba6b758SLuiz Augusto von Dentz struct hci_cp_ext_adv_set *set; 995cba6b758SLuiz Augusto von Dentz u8 data[sizeof(*cp) + sizeof(*set) * 1]; 996cba6b758SLuiz Augusto von Dentz struct adv_info *adv; 997cba6b758SLuiz Augusto von Dentz 998cba6b758SLuiz Augusto von Dentz if (instance > 0) { 999cba6b758SLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance); 1000cba6b758SLuiz Augusto von Dentz if (!adv) 1001cba6b758SLuiz Augusto von Dentz return -EINVAL; 1002cba6b758SLuiz Augusto von Dentz /* If already enabled there is nothing to do */ 1003cba6b758SLuiz Augusto von Dentz if (adv->enabled) 1004cba6b758SLuiz Augusto von Dentz return 0; 1005cba6b758SLuiz Augusto von Dentz } else { 1006cba6b758SLuiz Augusto von Dentz adv = NULL; 1007cba6b758SLuiz Augusto von Dentz } 1008cba6b758SLuiz Augusto von Dentz 1009cba6b758SLuiz Augusto von Dentz cp = (void *)data; 1010cba6b758SLuiz Augusto von Dentz set = (void *)cp->data; 1011cba6b758SLuiz Augusto von Dentz 1012cba6b758SLuiz Augusto von Dentz memset(cp, 0, sizeof(*cp)); 1013cba6b758SLuiz Augusto von Dentz 1014cba6b758SLuiz Augusto von Dentz cp->enable = 0x01; 1015cba6b758SLuiz Augusto von Dentz cp->num_of_sets = 0x01; 1016cba6b758SLuiz Augusto von Dentz 1017cba6b758SLuiz Augusto von Dentz memset(set, 0, sizeof(*set)); 1018cba6b758SLuiz Augusto von Dentz 1019cba6b758SLuiz Augusto von Dentz set->handle = instance; 1020cba6b758SLuiz Augusto von Dentz 1021cba6b758SLuiz Augusto von Dentz /* Set duration per instance since controller is responsible for 1022cba6b758SLuiz Augusto von Dentz * scheduling it. 1023cba6b758SLuiz Augusto von Dentz */ 1024f16a491cSLuiz Augusto von Dentz if (adv && adv->timeout) { 1025cba6b758SLuiz Augusto von Dentz u16 duration = adv->timeout * MSEC_PER_SEC; 1026cba6b758SLuiz Augusto von Dentz 1027cba6b758SLuiz Augusto von Dentz /* Time = N * 10 ms */ 1028cba6b758SLuiz Augusto von Dentz set->duration = cpu_to_le16(duration / 10); 1029cba6b758SLuiz Augusto von Dentz } 1030cba6b758SLuiz Augusto von Dentz 1031cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, 1032cba6b758SLuiz Augusto von Dentz sizeof(*cp) + 1033cba6b758SLuiz Augusto von Dentz sizeof(*set) * cp->num_of_sets, 1034cba6b758SLuiz Augusto von Dentz data, HCI_CMD_TIMEOUT); 1035cba6b758SLuiz Augusto von Dentz } 1036cba6b758SLuiz Augusto von Dentz 1037cba6b758SLuiz Augusto von Dentz int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance) 1038cba6b758SLuiz Augusto von Dentz { 1039cba6b758SLuiz Augusto von Dentz int err; 1040cba6b758SLuiz Augusto von Dentz 1041cba6b758SLuiz Augusto von Dentz err = hci_setup_ext_adv_instance_sync(hdev, instance); 1042cba6b758SLuiz Augusto von Dentz if (err) 1043cba6b758SLuiz Augusto von Dentz return err; 1044cba6b758SLuiz Augusto von Dentz 1045cba6b758SLuiz Augusto von Dentz err = hci_set_ext_scan_rsp_data_sync(hdev, instance); 1046cba6b758SLuiz Augusto von Dentz if (err) 1047cba6b758SLuiz Augusto von Dentz return err; 1048cba6b758SLuiz Augusto von Dentz 1049cba6b758SLuiz Augusto von Dentz return hci_enable_ext_advertising_sync(hdev, instance); 1050cba6b758SLuiz Augusto von Dentz } 1051cba6b758SLuiz Augusto von Dentz 1052eca0ae4aSLuiz Augusto von Dentz static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance) 1053eca0ae4aSLuiz Augusto von Dentz { 1054eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_set_per_adv_enable cp; 1055eca0ae4aSLuiz Augusto von Dentz 1056eca0ae4aSLuiz Augusto von Dentz /* If periodic advertising already disabled there is nothing to do. */ 1057eca0ae4aSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_PER_ADV)) 1058eca0ae4aSLuiz Augusto von Dentz return 0; 1059eca0ae4aSLuiz Augusto von Dentz 1060eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 1061eca0ae4aSLuiz Augusto von Dentz 1062eca0ae4aSLuiz Augusto von Dentz cp.enable = 0x00; 1063eca0ae4aSLuiz Augusto von Dentz cp.handle = instance; 1064eca0ae4aSLuiz Augusto von Dentz 1065eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE, 1066eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1067eca0ae4aSLuiz Augusto von Dentz } 1068eca0ae4aSLuiz Augusto von Dentz 1069eca0ae4aSLuiz Augusto von Dentz static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance, 1070eca0ae4aSLuiz Augusto von Dentz u16 min_interval, u16 max_interval) 1071eca0ae4aSLuiz Augusto von Dentz { 1072eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_set_per_adv_params cp; 1073eca0ae4aSLuiz Augusto von Dentz 1074eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 1075eca0ae4aSLuiz Augusto von Dentz 1076eca0ae4aSLuiz Augusto von Dentz if (!min_interval) 1077eca0ae4aSLuiz Augusto von Dentz min_interval = DISCOV_LE_PER_ADV_INT_MIN; 1078eca0ae4aSLuiz Augusto von Dentz 1079eca0ae4aSLuiz Augusto von Dentz if (!max_interval) 1080eca0ae4aSLuiz Augusto von Dentz max_interval = DISCOV_LE_PER_ADV_INT_MAX; 1081eca0ae4aSLuiz Augusto von Dentz 1082eca0ae4aSLuiz Augusto von Dentz cp.handle = instance; 1083eca0ae4aSLuiz Augusto von Dentz cp.min_interval = cpu_to_le16(min_interval); 1084eca0ae4aSLuiz Augusto von Dentz cp.max_interval = cpu_to_le16(max_interval); 1085eca0ae4aSLuiz Augusto von Dentz cp.periodic_properties = 0x0000; 1086eca0ae4aSLuiz Augusto von Dentz 1087eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS, 1088eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1089eca0ae4aSLuiz Augusto von Dentz } 1090eca0ae4aSLuiz Augusto von Dentz 1091eca0ae4aSLuiz Augusto von Dentz static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance) 1092eca0ae4aSLuiz Augusto von Dentz { 1093eca0ae4aSLuiz Augusto von Dentz struct { 1094eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_set_per_adv_data cp; 1095eca0ae4aSLuiz Augusto von Dentz u8 data[HCI_MAX_PER_AD_LENGTH]; 1096eca0ae4aSLuiz Augusto von Dentz } pdu; 1097eca0ae4aSLuiz Augusto von Dentz u8 len; 1098eca0ae4aSLuiz Augusto von Dentz 1099eca0ae4aSLuiz Augusto von Dentz memset(&pdu, 0, sizeof(pdu)); 1100eca0ae4aSLuiz Augusto von Dentz 1101eca0ae4aSLuiz Augusto von Dentz if (instance) { 1102eca0ae4aSLuiz Augusto von Dentz struct adv_info *adv = hci_find_adv_instance(hdev, instance); 1103eca0ae4aSLuiz Augusto von Dentz 1104eca0ae4aSLuiz Augusto von Dentz if (!adv || !adv->periodic) 1105eca0ae4aSLuiz Augusto von Dentz return 0; 1106eca0ae4aSLuiz Augusto von Dentz } 1107eca0ae4aSLuiz Augusto von Dentz 1108eca0ae4aSLuiz Augusto von Dentz len = eir_create_per_adv_data(hdev, instance, pdu.data); 1109eca0ae4aSLuiz Augusto von Dentz 1110eca0ae4aSLuiz Augusto von Dentz pdu.cp.length = len; 1111eca0ae4aSLuiz Augusto von Dentz pdu.cp.handle = instance; 1112eca0ae4aSLuiz Augusto von Dentz pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; 1113eca0ae4aSLuiz Augusto von Dentz 1114eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA, 1115eca0ae4aSLuiz Augusto von Dentz sizeof(pdu.cp) + len, &pdu, 1116eca0ae4aSLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 1117eca0ae4aSLuiz Augusto von Dentz } 1118eca0ae4aSLuiz Augusto von Dentz 1119eca0ae4aSLuiz Augusto von Dentz static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance) 1120eca0ae4aSLuiz Augusto von Dentz { 1121eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_set_per_adv_enable cp; 1122eca0ae4aSLuiz Augusto von Dentz 1123eca0ae4aSLuiz Augusto von Dentz /* If periodic advertising already enabled there is nothing to do. */ 1124eca0ae4aSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LE_PER_ADV)) 1125eca0ae4aSLuiz Augusto von Dentz return 0; 1126eca0ae4aSLuiz Augusto von Dentz 1127eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 1128eca0ae4aSLuiz Augusto von Dentz 1129eca0ae4aSLuiz Augusto von Dentz cp.enable = 0x01; 1130eca0ae4aSLuiz Augusto von Dentz cp.handle = instance; 1131eca0ae4aSLuiz Augusto von Dentz 1132eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE, 1133eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1134eca0ae4aSLuiz Augusto von Dentz } 1135eca0ae4aSLuiz Augusto von Dentz 1136eca0ae4aSLuiz Augusto von Dentz /* Checks if periodic advertising data contains a Basic Announcement and if it 1137eca0ae4aSLuiz Augusto von Dentz * does generates a Broadcast ID and add Broadcast Announcement. 1138eca0ae4aSLuiz Augusto von Dentz */ 1139eca0ae4aSLuiz Augusto von Dentz static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv) 1140eca0ae4aSLuiz Augusto von Dentz { 1141eca0ae4aSLuiz Augusto von Dentz u8 bid[3]; 1142eca0ae4aSLuiz Augusto von Dentz u8 ad[4 + 3]; 1143eca0ae4aSLuiz Augusto von Dentz 1144eca0ae4aSLuiz Augusto von Dentz /* Skip if NULL adv as instance 0x00 is used for general purpose 1145eca0ae4aSLuiz Augusto von Dentz * advertising so it cannot used for the likes of Broadcast Announcement 1146eca0ae4aSLuiz Augusto von Dentz * as it can be overwritten at any point. 1147eca0ae4aSLuiz Augusto von Dentz */ 1148eca0ae4aSLuiz Augusto von Dentz if (!adv) 1149eca0ae4aSLuiz Augusto von Dentz return 0; 1150eca0ae4aSLuiz Augusto von Dentz 1151eca0ae4aSLuiz Augusto von Dentz /* Check if PA data doesn't contains a Basic Audio Announcement then 1152eca0ae4aSLuiz Augusto von Dentz * there is nothing to do. 1153eca0ae4aSLuiz Augusto von Dentz */ 1154eca0ae4aSLuiz Augusto von Dentz if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len, 1155eca0ae4aSLuiz Augusto von Dentz 0x1851, NULL)) 1156eca0ae4aSLuiz Augusto von Dentz return 0; 1157eca0ae4aSLuiz Augusto von Dentz 1158eca0ae4aSLuiz Augusto von Dentz /* Check if advertising data already has a Broadcast Announcement since 1159eca0ae4aSLuiz Augusto von Dentz * the process may want to control the Broadcast ID directly and in that 1160eca0ae4aSLuiz Augusto von Dentz * case the kernel shall no interfere. 1161eca0ae4aSLuiz Augusto von Dentz */ 1162eca0ae4aSLuiz Augusto von Dentz if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852, 1163eca0ae4aSLuiz Augusto von Dentz NULL)) 1164eca0ae4aSLuiz Augusto von Dentz return 0; 1165eca0ae4aSLuiz Augusto von Dentz 1166eca0ae4aSLuiz Augusto von Dentz /* Generate Broadcast ID */ 1167eca0ae4aSLuiz Augusto von Dentz get_random_bytes(bid, sizeof(bid)); 1168eca0ae4aSLuiz Augusto von Dentz eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid)); 1169eca0ae4aSLuiz Augusto von Dentz hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL); 1170eca0ae4aSLuiz Augusto von Dentz 1171eca0ae4aSLuiz Augusto von Dentz return hci_update_adv_data_sync(hdev, adv->instance); 1172eca0ae4aSLuiz Augusto von Dentz } 1173eca0ae4aSLuiz Augusto von Dentz 1174eca0ae4aSLuiz Augusto von Dentz int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len, 1175eca0ae4aSLuiz Augusto von Dentz u8 *data, u32 flags, u16 min_interval, 1176eca0ae4aSLuiz Augusto von Dentz u16 max_interval, u16 sync_interval) 1177eca0ae4aSLuiz Augusto von Dentz { 1178eca0ae4aSLuiz Augusto von Dentz struct adv_info *adv = NULL; 1179eca0ae4aSLuiz Augusto von Dentz int err; 1180eca0ae4aSLuiz Augusto von Dentz bool added = false; 1181eca0ae4aSLuiz Augusto von Dentz 1182eca0ae4aSLuiz Augusto von Dentz hci_disable_per_advertising_sync(hdev, instance); 1183eca0ae4aSLuiz Augusto von Dentz 1184eca0ae4aSLuiz Augusto von Dentz if (instance) { 1185eca0ae4aSLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance); 1186eca0ae4aSLuiz Augusto von Dentz /* Create an instance if that could not be found */ 1187eca0ae4aSLuiz Augusto von Dentz if (!adv) { 1188eca0ae4aSLuiz Augusto von Dentz adv = hci_add_per_instance(hdev, instance, flags, 1189eca0ae4aSLuiz Augusto von Dentz data_len, data, 1190eca0ae4aSLuiz Augusto von Dentz sync_interval, 1191eca0ae4aSLuiz Augusto von Dentz sync_interval); 1192eca0ae4aSLuiz Augusto von Dentz if (IS_ERR(adv)) 1193eca0ae4aSLuiz Augusto von Dentz return PTR_ERR(adv); 1194eca0ae4aSLuiz Augusto von Dentz added = true; 1195eca0ae4aSLuiz Augusto von Dentz } 1196eca0ae4aSLuiz Augusto von Dentz } 1197eca0ae4aSLuiz Augusto von Dentz 1198eca0ae4aSLuiz Augusto von Dentz /* Only start advertising if instance 0 or if a dedicated instance has 1199eca0ae4aSLuiz Augusto von Dentz * been added. 1200eca0ae4aSLuiz Augusto von Dentz */ 1201eca0ae4aSLuiz Augusto von Dentz if (!adv || added) { 1202eca0ae4aSLuiz Augusto von Dentz err = hci_start_ext_adv_sync(hdev, instance); 1203eca0ae4aSLuiz Augusto von Dentz if (err < 0) 1204eca0ae4aSLuiz Augusto von Dentz goto fail; 1205eca0ae4aSLuiz Augusto von Dentz 1206eca0ae4aSLuiz Augusto von Dentz err = hci_adv_bcast_annoucement(hdev, adv); 1207eca0ae4aSLuiz Augusto von Dentz if (err < 0) 1208eca0ae4aSLuiz Augusto von Dentz goto fail; 1209eca0ae4aSLuiz Augusto von Dentz } 1210eca0ae4aSLuiz Augusto von Dentz 1211eca0ae4aSLuiz Augusto von Dentz err = hci_set_per_adv_params_sync(hdev, instance, min_interval, 1212eca0ae4aSLuiz Augusto von Dentz max_interval); 1213eca0ae4aSLuiz Augusto von Dentz if (err < 0) 1214eca0ae4aSLuiz Augusto von Dentz goto fail; 1215eca0ae4aSLuiz Augusto von Dentz 1216eca0ae4aSLuiz Augusto von Dentz err = hci_set_per_adv_data_sync(hdev, instance); 1217eca0ae4aSLuiz Augusto von Dentz if (err < 0) 1218eca0ae4aSLuiz Augusto von Dentz goto fail; 1219eca0ae4aSLuiz Augusto von Dentz 1220eca0ae4aSLuiz Augusto von Dentz err = hci_enable_per_advertising_sync(hdev, instance); 1221eca0ae4aSLuiz Augusto von Dentz if (err < 0) 1222eca0ae4aSLuiz Augusto von Dentz goto fail; 1223eca0ae4aSLuiz Augusto von Dentz 1224eca0ae4aSLuiz Augusto von Dentz return 0; 1225eca0ae4aSLuiz Augusto von Dentz 1226eca0ae4aSLuiz Augusto von Dentz fail: 1227eca0ae4aSLuiz Augusto von Dentz if (added) 1228eca0ae4aSLuiz Augusto von Dentz hci_remove_adv_instance(hdev, instance); 1229eca0ae4aSLuiz Augusto von Dentz 1230eca0ae4aSLuiz Augusto von Dentz return err; 1231eca0ae4aSLuiz Augusto von Dentz } 1232eca0ae4aSLuiz Augusto von Dentz 1233cba6b758SLuiz Augusto von Dentz static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance) 1234cba6b758SLuiz Augusto von Dentz { 1235cba6b758SLuiz Augusto von Dentz int err; 1236cba6b758SLuiz Augusto von Dentz 1237cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 1238cba6b758SLuiz Augusto von Dentz return hci_start_ext_adv_sync(hdev, instance); 1239cba6b758SLuiz Augusto von Dentz 1240cba6b758SLuiz Augusto von Dentz err = hci_update_adv_data_sync(hdev, instance); 1241cba6b758SLuiz Augusto von Dentz if (err) 1242cba6b758SLuiz Augusto von Dentz return err; 1243cba6b758SLuiz Augusto von Dentz 1244cba6b758SLuiz Augusto von Dentz err = hci_update_scan_rsp_data_sync(hdev, instance); 1245cba6b758SLuiz Augusto von Dentz if (err) 1246cba6b758SLuiz Augusto von Dentz return err; 1247cba6b758SLuiz Augusto von Dentz 1248cba6b758SLuiz Augusto von Dentz return hci_enable_advertising_sync(hdev); 1249cba6b758SLuiz Augusto von Dentz } 1250cba6b758SLuiz Augusto von Dentz 1251cba6b758SLuiz Augusto von Dentz int hci_enable_advertising_sync(struct hci_dev *hdev) 1252cba6b758SLuiz Augusto von Dentz { 1253cba6b758SLuiz Augusto von Dentz struct adv_info *adv_instance; 1254cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_adv_param cp; 1255cba6b758SLuiz Augusto von Dentz u8 own_addr_type, enable = 0x01; 1256cba6b758SLuiz Augusto von Dentz bool connectable; 1257cba6b758SLuiz Augusto von Dentz u16 adv_min_interval, adv_max_interval; 1258cba6b758SLuiz Augusto von Dentz u32 flags; 1259cba6b758SLuiz Augusto von Dentz u8 status; 1260cba6b758SLuiz Augusto von Dentz 1261ad383c2cSLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 1262ad383c2cSLuiz Augusto von Dentz return hci_enable_ext_advertising_sync(hdev, 1263ad383c2cSLuiz Augusto von Dentz hdev->cur_adv_instance); 1264ad383c2cSLuiz Augusto von Dentz 1265cba6b758SLuiz Augusto von Dentz flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance); 1266cba6b758SLuiz Augusto von Dentz adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); 1267cba6b758SLuiz Augusto von Dentz 1268cba6b758SLuiz Augusto von Dentz /* If the "connectable" instance flag was not set, then choose between 1269cba6b758SLuiz Augusto von Dentz * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. 1270cba6b758SLuiz Augusto von Dentz */ 1271cba6b758SLuiz Augusto von Dentz connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || 1272cba6b758SLuiz Augusto von Dentz mgmt_get_connectable(hdev); 1273cba6b758SLuiz Augusto von Dentz 1274cba6b758SLuiz Augusto von Dentz if (!is_advertising_allowed(hdev, connectable)) 1275cba6b758SLuiz Augusto von Dentz return -EINVAL; 1276cba6b758SLuiz Augusto von Dentz 1277cba6b758SLuiz Augusto von Dentz status = hci_disable_advertising_sync(hdev); 1278cba6b758SLuiz Augusto von Dentz if (status) 1279cba6b758SLuiz Augusto von Dentz return status; 1280cba6b758SLuiz Augusto von Dentz 1281cba6b758SLuiz Augusto von Dentz /* Clear the HCI_LE_ADV bit temporarily so that the 1282cba6b758SLuiz Augusto von Dentz * hci_update_random_address knows that it's safe to go ahead 1283cba6b758SLuiz Augusto von Dentz * and write a new random address. The flag will be set back on 1284cba6b758SLuiz Augusto von Dentz * as soon as the SET_ADV_ENABLE HCI command completes. 1285cba6b758SLuiz Augusto von Dentz */ 1286cba6b758SLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_LE_ADV); 1287cba6b758SLuiz Augusto von Dentz 1288cba6b758SLuiz Augusto von Dentz /* Set require_privacy to true only when non-connectable 1289cba6b758SLuiz Augusto von Dentz * advertising is used. In that case it is fine to use a 1290cba6b758SLuiz Augusto von Dentz * non-resolvable private address. 1291cba6b758SLuiz Augusto von Dentz */ 1292cba6b758SLuiz Augusto von Dentz status = hci_update_random_address_sync(hdev, !connectable, 1293cba6b758SLuiz Augusto von Dentz adv_use_rpa(hdev, flags), 1294cba6b758SLuiz Augusto von Dentz &own_addr_type); 1295cba6b758SLuiz Augusto von Dentz if (status) 1296cba6b758SLuiz Augusto von Dentz return status; 1297cba6b758SLuiz Augusto von Dentz 1298cba6b758SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 1299cba6b758SLuiz Augusto von Dentz 1300cba6b758SLuiz Augusto von Dentz if (adv_instance) { 1301cba6b758SLuiz Augusto von Dentz adv_min_interval = adv_instance->min_interval; 1302cba6b758SLuiz Augusto von Dentz adv_max_interval = adv_instance->max_interval; 1303cba6b758SLuiz Augusto von Dentz } else { 1304cba6b758SLuiz Augusto von Dentz adv_min_interval = hdev->le_adv_min_interval; 1305cba6b758SLuiz Augusto von Dentz adv_max_interval = hdev->le_adv_max_interval; 1306cba6b758SLuiz Augusto von Dentz } 1307cba6b758SLuiz Augusto von Dentz 1308cba6b758SLuiz Augusto von Dentz if (connectable) { 1309cba6b758SLuiz Augusto von Dentz cp.type = LE_ADV_IND; 1310cba6b758SLuiz Augusto von Dentz } else { 1311cba6b758SLuiz Augusto von Dentz if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance)) 1312cba6b758SLuiz Augusto von Dentz cp.type = LE_ADV_SCAN_IND; 1313cba6b758SLuiz Augusto von Dentz else 1314cba6b758SLuiz Augusto von Dentz cp.type = LE_ADV_NONCONN_IND; 1315cba6b758SLuiz Augusto von Dentz 1316cba6b758SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) || 1317cba6b758SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { 1318cba6b758SLuiz Augusto von Dentz adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN; 1319cba6b758SLuiz Augusto von Dentz adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX; 1320cba6b758SLuiz Augusto von Dentz } 1321cba6b758SLuiz Augusto von Dentz } 1322cba6b758SLuiz Augusto von Dentz 1323cba6b758SLuiz Augusto von Dentz cp.min_interval = cpu_to_le16(adv_min_interval); 1324cba6b758SLuiz Augusto von Dentz cp.max_interval = cpu_to_le16(adv_max_interval); 1325cba6b758SLuiz Augusto von Dentz cp.own_address_type = own_addr_type; 1326cba6b758SLuiz Augusto von Dentz cp.channel_map = hdev->le_adv_channel_map; 1327cba6b758SLuiz Augusto von Dentz 1328cba6b758SLuiz Augusto von Dentz status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM, 1329cba6b758SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1330cba6b758SLuiz Augusto von Dentz if (status) 1331cba6b758SLuiz Augusto von Dentz return status; 1332cba6b758SLuiz Augusto von Dentz 1333cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, 1334cba6b758SLuiz Augusto von Dentz sizeof(enable), &enable, HCI_CMD_TIMEOUT); 1335cba6b758SLuiz Augusto von Dentz } 1336cba6b758SLuiz Augusto von Dentz 1337abfeea47SLuiz Augusto von Dentz static int enable_advertising_sync(struct hci_dev *hdev, void *data) 1338abfeea47SLuiz Augusto von Dentz { 1339abfeea47SLuiz Augusto von Dentz return hci_enable_advertising_sync(hdev); 1340abfeea47SLuiz Augusto von Dentz } 1341abfeea47SLuiz Augusto von Dentz 1342abfeea47SLuiz Augusto von Dentz int hci_enable_advertising(struct hci_dev *hdev) 1343abfeea47SLuiz Augusto von Dentz { 1344abfeea47SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) && 1345abfeea47SLuiz Augusto von Dentz list_empty(&hdev->adv_instances)) 1346abfeea47SLuiz Augusto von Dentz return 0; 1347abfeea47SLuiz Augusto von Dentz 1348abfeea47SLuiz Augusto von Dentz return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL); 1349abfeea47SLuiz Augusto von Dentz } 1350abfeea47SLuiz Augusto von Dentz 1351abfeea47SLuiz Augusto von Dentz int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance, 1352cba6b758SLuiz Augusto von Dentz struct sock *sk) 1353cba6b758SLuiz Augusto von Dentz { 1354cba6b758SLuiz Augusto von Dentz int err; 1355cba6b758SLuiz Augusto von Dentz 1356cba6b758SLuiz Augusto von Dentz if (!ext_adv_capable(hdev)) 1357cba6b758SLuiz Augusto von Dentz return 0; 1358cba6b758SLuiz Augusto von Dentz 1359cba6b758SLuiz Augusto von Dentz err = hci_disable_ext_adv_instance_sync(hdev, instance); 1360cba6b758SLuiz Augusto von Dentz if (err) 1361cba6b758SLuiz Augusto von Dentz return err; 1362cba6b758SLuiz Augusto von Dentz 1363cba6b758SLuiz Augusto von Dentz /* If request specifies an instance that doesn't exist, fail */ 1364cba6b758SLuiz Augusto von Dentz if (instance > 0 && !hci_find_adv_instance(hdev, instance)) 1365cba6b758SLuiz Augusto von Dentz return -EINVAL; 1366cba6b758SLuiz Augusto von Dentz 1367cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET, 1368cba6b758SLuiz Augusto von Dentz sizeof(instance), &instance, 0, 1369cba6b758SLuiz Augusto von Dentz HCI_CMD_TIMEOUT, sk); 1370cba6b758SLuiz Augusto von Dentz } 1371cba6b758SLuiz Augusto von Dentz 1372eca0ae4aSLuiz Augusto von Dentz static int remove_ext_adv_sync(struct hci_dev *hdev, void *data) 1373eca0ae4aSLuiz Augusto von Dentz { 1374eca0ae4aSLuiz Augusto von Dentz struct adv_info *adv = data; 1375eca0ae4aSLuiz Augusto von Dentz u8 instance = 0; 1376eca0ae4aSLuiz Augusto von Dentz 1377eca0ae4aSLuiz Augusto von Dentz if (adv) 1378eca0ae4aSLuiz Augusto von Dentz instance = adv->instance; 1379eca0ae4aSLuiz Augusto von Dentz 1380eca0ae4aSLuiz Augusto von Dentz return hci_remove_ext_adv_instance_sync(hdev, instance, NULL); 1381eca0ae4aSLuiz Augusto von Dentz } 1382eca0ae4aSLuiz Augusto von Dentz 1383eca0ae4aSLuiz Augusto von Dentz int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance) 1384eca0ae4aSLuiz Augusto von Dentz { 1385eca0ae4aSLuiz Augusto von Dentz struct adv_info *adv = NULL; 1386eca0ae4aSLuiz Augusto von Dentz 1387eca0ae4aSLuiz Augusto von Dentz if (instance) { 1388eca0ae4aSLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance); 1389eca0ae4aSLuiz Augusto von Dentz if (!adv) 1390eca0ae4aSLuiz Augusto von Dentz return -EINVAL; 1391eca0ae4aSLuiz Augusto von Dentz } 1392eca0ae4aSLuiz Augusto von Dentz 1393eca0ae4aSLuiz Augusto von Dentz return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL); 1394eca0ae4aSLuiz Augusto von Dentz } 1395eca0ae4aSLuiz Augusto von Dentz 1396eca0ae4aSLuiz Augusto von Dentz int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason) 1397eca0ae4aSLuiz Augusto von Dentz { 1398eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_term_big cp; 1399eca0ae4aSLuiz Augusto von Dentz 1400eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 1401eca0ae4aSLuiz Augusto von Dentz cp.handle = handle; 1402eca0ae4aSLuiz Augusto von Dentz cp.reason = reason; 1403eca0ae4aSLuiz Augusto von Dentz 1404eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG, 1405eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1406eca0ae4aSLuiz Augusto von Dentz } 1407eca0ae4aSLuiz Augusto von Dentz 1408cba6b758SLuiz Augusto von Dentz static void cancel_adv_timeout(struct hci_dev *hdev) 1409cba6b758SLuiz Augusto von Dentz { 1410cba6b758SLuiz Augusto von Dentz if (hdev->adv_instance_timeout) { 1411cba6b758SLuiz Augusto von Dentz hdev->adv_instance_timeout = 0; 1412cba6b758SLuiz Augusto von Dentz cancel_delayed_work(&hdev->adv_instance_expire); 1413cba6b758SLuiz Augusto von Dentz } 1414cba6b758SLuiz Augusto von Dentz } 1415cba6b758SLuiz Augusto von Dentz 1416cba6b758SLuiz Augusto von Dentz static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance) 1417cba6b758SLuiz Augusto von Dentz { 1418cba6b758SLuiz Augusto von Dentz struct { 1419cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_ext_adv_data cp; 1420cba6b758SLuiz Augusto von Dentz u8 data[HCI_MAX_EXT_AD_LENGTH]; 1421cba6b758SLuiz Augusto von Dentz } pdu; 1422cba6b758SLuiz Augusto von Dentz u8 len; 142334a718bcSLuiz Augusto von Dentz struct adv_info *adv = NULL; 142434a718bcSLuiz Augusto von Dentz int err; 1425cba6b758SLuiz Augusto von Dentz 1426cba6b758SLuiz Augusto von Dentz memset(&pdu, 0, sizeof(pdu)); 1427cba6b758SLuiz Augusto von Dentz 142834a718bcSLuiz Augusto von Dentz if (instance) { 142934a718bcSLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance); 143034a718bcSLuiz Augusto von Dentz if (!adv || !adv->adv_data_changed) 1431cba6b758SLuiz Augusto von Dentz return 0; 143234a718bcSLuiz Augusto von Dentz } 1433cba6b758SLuiz Augusto von Dentz 143434a718bcSLuiz Augusto von Dentz len = eir_create_adv_data(hdev, instance, pdu.data); 1435cba6b758SLuiz Augusto von Dentz 1436cba6b758SLuiz Augusto von Dentz pdu.cp.length = len; 1437cba6b758SLuiz Augusto von Dentz pdu.cp.handle = instance; 1438cba6b758SLuiz Augusto von Dentz pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; 1439cba6b758SLuiz Augusto von Dentz pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; 1440cba6b758SLuiz Augusto von Dentz 144134a718bcSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA, 1442cba6b758SLuiz Augusto von Dentz sizeof(pdu.cp) + len, &pdu.cp, 1443cba6b758SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 144434a718bcSLuiz Augusto von Dentz if (err) 144534a718bcSLuiz Augusto von Dentz return err; 144634a718bcSLuiz Augusto von Dentz 144734a718bcSLuiz Augusto von Dentz /* Update data if the command succeed */ 144834a718bcSLuiz Augusto von Dentz if (adv) { 144934a718bcSLuiz Augusto von Dentz adv->adv_data_changed = false; 145034a718bcSLuiz Augusto von Dentz } else { 145134a718bcSLuiz Augusto von Dentz memcpy(hdev->adv_data, pdu.data, len); 145234a718bcSLuiz Augusto von Dentz hdev->adv_data_len = len; 145334a718bcSLuiz Augusto von Dentz } 145434a718bcSLuiz Augusto von Dentz 145534a718bcSLuiz Augusto von Dentz return 0; 1456cba6b758SLuiz Augusto von Dentz } 1457cba6b758SLuiz Augusto von Dentz 1458cba6b758SLuiz Augusto von Dentz static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance) 1459cba6b758SLuiz Augusto von Dentz { 1460cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_adv_data cp; 1461cba6b758SLuiz Augusto von Dentz u8 len; 1462cba6b758SLuiz Augusto von Dentz 1463cba6b758SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 1464cba6b758SLuiz Augusto von Dentz 1465cba6b758SLuiz Augusto von Dentz len = eir_create_adv_data(hdev, instance, cp.data); 1466cba6b758SLuiz Augusto von Dentz 1467cba6b758SLuiz Augusto von Dentz /* There's nothing to do if the data hasn't changed */ 1468cba6b758SLuiz Augusto von Dentz if (hdev->adv_data_len == len && 1469cba6b758SLuiz Augusto von Dentz memcmp(cp.data, hdev->adv_data, len) == 0) 1470cba6b758SLuiz Augusto von Dentz return 0; 1471cba6b758SLuiz Augusto von Dentz 1472cba6b758SLuiz Augusto von Dentz memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); 1473cba6b758SLuiz Augusto von Dentz hdev->adv_data_len = len; 1474cba6b758SLuiz Augusto von Dentz 1475cba6b758SLuiz Augusto von Dentz cp.length = len; 1476cba6b758SLuiz Augusto von Dentz 1477cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA, 1478cba6b758SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1479cba6b758SLuiz Augusto von Dentz } 1480cba6b758SLuiz Augusto von Dentz 1481cba6b758SLuiz Augusto von Dentz int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance) 1482cba6b758SLuiz Augusto von Dentz { 1483cba6b758SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 1484cba6b758SLuiz Augusto von Dentz return 0; 1485cba6b758SLuiz Augusto von Dentz 1486cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 1487cba6b758SLuiz Augusto von Dentz return hci_set_ext_adv_data_sync(hdev, instance); 1488cba6b758SLuiz Augusto von Dentz 1489cba6b758SLuiz Augusto von Dentz return hci_set_adv_data_sync(hdev, instance); 1490cba6b758SLuiz Augusto von Dentz } 1491cba6b758SLuiz Augusto von Dentz 1492cba6b758SLuiz Augusto von Dentz int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance, 1493cba6b758SLuiz Augusto von Dentz bool force) 1494cba6b758SLuiz Augusto von Dentz { 1495cba6b758SLuiz Augusto von Dentz struct adv_info *adv = NULL; 1496cba6b758SLuiz Augusto von Dentz u16 timeout; 1497cba6b758SLuiz Augusto von Dentz 1498cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev)) 1499cba6b758SLuiz Augusto von Dentz return -EPERM; 1500cba6b758SLuiz Augusto von Dentz 1501cba6b758SLuiz Augusto von Dentz if (hdev->adv_instance_timeout) 1502cba6b758SLuiz Augusto von Dentz return -EBUSY; 1503cba6b758SLuiz Augusto von Dentz 1504cba6b758SLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance); 1505cba6b758SLuiz Augusto von Dentz if (!adv) 1506cba6b758SLuiz Augusto von Dentz return -ENOENT; 1507cba6b758SLuiz Augusto von Dentz 1508cba6b758SLuiz Augusto von Dentz /* A zero timeout means unlimited advertising. As long as there is 1509cba6b758SLuiz Augusto von Dentz * only one instance, duration should be ignored. We still set a timeout 1510cba6b758SLuiz Augusto von Dentz * in case further instances are being added later on. 1511cba6b758SLuiz Augusto von Dentz * 1512cba6b758SLuiz Augusto von Dentz * If the remaining lifetime of the instance is more than the duration 1513cba6b758SLuiz Augusto von Dentz * then the timeout corresponds to the duration, otherwise it will be 1514cba6b758SLuiz Augusto von Dentz * reduced to the remaining instance lifetime. 1515cba6b758SLuiz Augusto von Dentz */ 1516cba6b758SLuiz Augusto von Dentz if (adv->timeout == 0 || adv->duration <= adv->remaining_time) 1517cba6b758SLuiz Augusto von Dentz timeout = adv->duration; 1518cba6b758SLuiz Augusto von Dentz else 1519cba6b758SLuiz Augusto von Dentz timeout = adv->remaining_time; 1520cba6b758SLuiz Augusto von Dentz 1521cba6b758SLuiz Augusto von Dentz /* The remaining time is being reduced unless the instance is being 1522cba6b758SLuiz Augusto von Dentz * advertised without time limit. 1523cba6b758SLuiz Augusto von Dentz */ 1524cba6b758SLuiz Augusto von Dentz if (adv->timeout) 1525cba6b758SLuiz Augusto von Dentz adv->remaining_time = adv->remaining_time - timeout; 1526cba6b758SLuiz Augusto von Dentz 1527cba6b758SLuiz Augusto von Dentz /* Only use work for scheduling instances with legacy advertising */ 1528cba6b758SLuiz Augusto von Dentz if (!ext_adv_capable(hdev)) { 1529cba6b758SLuiz Augusto von Dentz hdev->adv_instance_timeout = timeout; 1530cba6b758SLuiz Augusto von Dentz queue_delayed_work(hdev->req_workqueue, 1531cba6b758SLuiz Augusto von Dentz &hdev->adv_instance_expire, 1532cba6b758SLuiz Augusto von Dentz msecs_to_jiffies(timeout * 1000)); 1533cba6b758SLuiz Augusto von Dentz } 1534cba6b758SLuiz Augusto von Dentz 1535cba6b758SLuiz Augusto von Dentz /* If we're just re-scheduling the same instance again then do not 1536cba6b758SLuiz Augusto von Dentz * execute any HCI commands. This happens when a single instance is 1537cba6b758SLuiz Augusto von Dentz * being advertised. 1538cba6b758SLuiz Augusto von Dentz */ 1539cba6b758SLuiz Augusto von Dentz if (!force && hdev->cur_adv_instance == instance && 1540cba6b758SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_LE_ADV)) 1541cba6b758SLuiz Augusto von Dentz return 0; 1542cba6b758SLuiz Augusto von Dentz 1543cba6b758SLuiz Augusto von Dentz hdev->cur_adv_instance = instance; 1544cba6b758SLuiz Augusto von Dentz 1545cba6b758SLuiz Augusto von Dentz return hci_start_adv_sync(hdev, instance); 1546cba6b758SLuiz Augusto von Dentz } 1547cba6b758SLuiz Augusto von Dentz 1548cba6b758SLuiz Augusto von Dentz static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk) 1549cba6b758SLuiz Augusto von Dentz { 1550cba6b758SLuiz Augusto von Dentz int err; 1551cba6b758SLuiz Augusto von Dentz 1552cba6b758SLuiz Augusto von Dentz if (!ext_adv_capable(hdev)) 1553cba6b758SLuiz Augusto von Dentz return 0; 1554cba6b758SLuiz Augusto von Dentz 1555cba6b758SLuiz Augusto von Dentz /* Disable instance 0x00 to disable all instances */ 1556cba6b758SLuiz Augusto von Dentz err = hci_disable_ext_adv_instance_sync(hdev, 0x00); 1557cba6b758SLuiz Augusto von Dentz if (err) 1558cba6b758SLuiz Augusto von Dentz return err; 1559cba6b758SLuiz Augusto von Dentz 1560cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS, 1561cba6b758SLuiz Augusto von Dentz 0, NULL, 0, HCI_CMD_TIMEOUT, sk); 1562cba6b758SLuiz Augusto von Dentz } 1563cba6b758SLuiz Augusto von Dentz 1564cba6b758SLuiz Augusto von Dentz static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force) 1565cba6b758SLuiz Augusto von Dentz { 1566cba6b758SLuiz Augusto von Dentz struct adv_info *adv, *n; 1567cba6b758SLuiz Augusto von Dentz 1568cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 1569cba6b758SLuiz Augusto von Dentz /* Remove all existing sets */ 1570cba6b758SLuiz Augusto von Dentz return hci_clear_adv_sets_sync(hdev, sk); 1571cba6b758SLuiz Augusto von Dentz 1572cba6b758SLuiz Augusto von Dentz /* This is safe as long as there is no command send while the lock is 1573cba6b758SLuiz Augusto von Dentz * held. 1574cba6b758SLuiz Augusto von Dentz */ 1575cba6b758SLuiz Augusto von Dentz hci_dev_lock(hdev); 1576cba6b758SLuiz Augusto von Dentz 1577cba6b758SLuiz Augusto von Dentz /* Cleanup non-ext instances */ 1578cba6b758SLuiz Augusto von Dentz list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) { 1579cba6b758SLuiz Augusto von Dentz u8 instance = adv->instance; 1580cba6b758SLuiz Augusto von Dentz int err; 1581cba6b758SLuiz Augusto von Dentz 1582cba6b758SLuiz Augusto von Dentz if (!(force || adv->timeout)) 1583cba6b758SLuiz Augusto von Dentz continue; 1584cba6b758SLuiz Augusto von Dentz 1585cba6b758SLuiz Augusto von Dentz err = hci_remove_adv_instance(hdev, instance); 1586cba6b758SLuiz Augusto von Dentz if (!err) 1587cba6b758SLuiz Augusto von Dentz mgmt_advertising_removed(sk, hdev, instance); 1588cba6b758SLuiz Augusto von Dentz } 1589cba6b758SLuiz Augusto von Dentz 1590cba6b758SLuiz Augusto von Dentz hci_dev_unlock(hdev); 1591cba6b758SLuiz Augusto von Dentz 1592cba6b758SLuiz Augusto von Dentz return 0; 1593cba6b758SLuiz Augusto von Dentz } 1594cba6b758SLuiz Augusto von Dentz 1595cba6b758SLuiz Augusto von Dentz static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance, 1596cba6b758SLuiz Augusto von Dentz struct sock *sk) 1597cba6b758SLuiz Augusto von Dentz { 1598cba6b758SLuiz Augusto von Dentz int err; 1599cba6b758SLuiz Augusto von Dentz 1600cba6b758SLuiz Augusto von Dentz /* If we use extended advertising, instance has to be removed first. */ 1601cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 1602cba6b758SLuiz Augusto von Dentz return hci_remove_ext_adv_instance_sync(hdev, instance, sk); 1603cba6b758SLuiz Augusto von Dentz 1604cba6b758SLuiz Augusto von Dentz /* This is safe as long as there is no command send while the lock is 1605cba6b758SLuiz Augusto von Dentz * held. 1606cba6b758SLuiz Augusto von Dentz */ 1607cba6b758SLuiz Augusto von Dentz hci_dev_lock(hdev); 1608cba6b758SLuiz Augusto von Dentz 1609cba6b758SLuiz Augusto von Dentz err = hci_remove_adv_instance(hdev, instance); 1610cba6b758SLuiz Augusto von Dentz if (!err) 1611cba6b758SLuiz Augusto von Dentz mgmt_advertising_removed(sk, hdev, instance); 1612cba6b758SLuiz Augusto von Dentz 1613cba6b758SLuiz Augusto von Dentz hci_dev_unlock(hdev); 1614cba6b758SLuiz Augusto von Dentz 1615cba6b758SLuiz Augusto von Dentz return err; 1616cba6b758SLuiz Augusto von Dentz } 1617cba6b758SLuiz Augusto von Dentz 1618cba6b758SLuiz Augusto von Dentz /* For a single instance: 1619cba6b758SLuiz Augusto von Dentz * - force == true: The instance will be removed even when its remaining 1620cba6b758SLuiz Augusto von Dentz * lifetime is not zero. 1621cba6b758SLuiz Augusto von Dentz * - force == false: the instance will be deactivated but kept stored unless 1622cba6b758SLuiz Augusto von Dentz * the remaining lifetime is zero. 1623cba6b758SLuiz Augusto von Dentz * 1624cba6b758SLuiz Augusto von Dentz * For instance == 0x00: 1625cba6b758SLuiz Augusto von Dentz * - force == true: All instances will be removed regardless of their timeout 1626cba6b758SLuiz Augusto von Dentz * setting. 1627cba6b758SLuiz Augusto von Dentz * - force == false: Only instances that have a timeout will be removed. 1628cba6b758SLuiz Augusto von Dentz */ 1629cba6b758SLuiz Augusto von Dentz int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk, 1630cba6b758SLuiz Augusto von Dentz u8 instance, bool force) 1631cba6b758SLuiz Augusto von Dentz { 1632cba6b758SLuiz Augusto von Dentz struct adv_info *next = NULL; 1633cba6b758SLuiz Augusto von Dentz int err; 1634cba6b758SLuiz Augusto von Dentz 1635cba6b758SLuiz Augusto von Dentz /* Cancel any timeout concerning the removed instance(s). */ 1636cba6b758SLuiz Augusto von Dentz if (!instance || hdev->cur_adv_instance == instance) 1637cba6b758SLuiz Augusto von Dentz cancel_adv_timeout(hdev); 1638cba6b758SLuiz Augusto von Dentz 1639cba6b758SLuiz Augusto von Dentz /* Get the next instance to advertise BEFORE we remove 1640cba6b758SLuiz Augusto von Dentz * the current one. This can be the same instance again 1641cba6b758SLuiz Augusto von Dentz * if there is only one instance. 1642cba6b758SLuiz Augusto von Dentz */ 1643cba6b758SLuiz Augusto von Dentz if (hdev->cur_adv_instance == instance) 1644cba6b758SLuiz Augusto von Dentz next = hci_get_next_instance(hdev, instance); 1645cba6b758SLuiz Augusto von Dentz 1646cba6b758SLuiz Augusto von Dentz if (!instance) { 1647cba6b758SLuiz Augusto von Dentz err = hci_clear_adv_sync(hdev, sk, force); 1648cba6b758SLuiz Augusto von Dentz if (err) 1649cba6b758SLuiz Augusto von Dentz return err; 1650cba6b758SLuiz Augusto von Dentz } else { 1651cba6b758SLuiz Augusto von Dentz struct adv_info *adv = hci_find_adv_instance(hdev, instance); 1652cba6b758SLuiz Augusto von Dentz 1653cba6b758SLuiz Augusto von Dentz if (force || (adv && adv->timeout && !adv->remaining_time)) { 1654cba6b758SLuiz Augusto von Dentz /* Don't advertise a removed instance. */ 1655cba6b758SLuiz Augusto von Dentz if (next && next->instance == instance) 1656cba6b758SLuiz Augusto von Dentz next = NULL; 1657cba6b758SLuiz Augusto von Dentz 1658cba6b758SLuiz Augusto von Dentz err = hci_remove_adv_sync(hdev, instance, sk); 1659cba6b758SLuiz Augusto von Dentz if (err) 1660cba6b758SLuiz Augusto von Dentz return err; 1661cba6b758SLuiz Augusto von Dentz } 1662cba6b758SLuiz Augusto von Dentz } 1663cba6b758SLuiz Augusto von Dentz 1664cba6b758SLuiz Augusto von Dentz if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING)) 1665cba6b758SLuiz Augusto von Dentz return 0; 1666cba6b758SLuiz Augusto von Dentz 1667cba6b758SLuiz Augusto von Dentz if (next && !ext_adv_capable(hdev)) 1668cba6b758SLuiz Augusto von Dentz hci_schedule_adv_instance_sync(hdev, next->instance, false); 1669cba6b758SLuiz Augusto von Dentz 1670cba6b758SLuiz Augusto von Dentz return 0; 1671cba6b758SLuiz Augusto von Dentz } 1672cba6b758SLuiz Augusto von Dentz 167347db6b42SBrian Gix int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle) 167447db6b42SBrian Gix { 167547db6b42SBrian Gix struct hci_cp_read_rssi cp; 167647db6b42SBrian Gix 167747db6b42SBrian Gix cp.handle = handle; 167847db6b42SBrian Gix return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI, 167947db6b42SBrian Gix sizeof(cp), &cp, HCI_CMD_TIMEOUT); 168047db6b42SBrian Gix } 168147db6b42SBrian Gix 16825a750137SBrian Gix int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp) 16835a750137SBrian Gix { 16845a750137SBrian Gix return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK, 16855a750137SBrian Gix sizeof(*cp), cp, HCI_CMD_TIMEOUT); 16865a750137SBrian Gix } 16875a750137SBrian Gix 168847db6b42SBrian Gix int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type) 168947db6b42SBrian Gix { 169047db6b42SBrian Gix struct hci_cp_read_tx_power cp; 169147db6b42SBrian Gix 169247db6b42SBrian Gix cp.handle = handle; 169347db6b42SBrian Gix cp.type = type; 169447db6b42SBrian Gix return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER, 169547db6b42SBrian Gix sizeof(cp), &cp, HCI_CMD_TIMEOUT); 169647db6b42SBrian Gix } 169747db6b42SBrian Gix 1698cba6b758SLuiz Augusto von Dentz int hci_disable_advertising_sync(struct hci_dev *hdev) 1699cba6b758SLuiz Augusto von Dentz { 1700cba6b758SLuiz Augusto von Dentz u8 enable = 0x00; 1701cba6b758SLuiz Augusto von Dentz 1702ad383c2cSLuiz Augusto von Dentz /* If controller is not advertising we are done. */ 1703ad383c2cSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ADV)) 1704ad383c2cSLuiz Augusto von Dentz return 0; 1705ad383c2cSLuiz Augusto von Dentz 1706cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 1707cba6b758SLuiz Augusto von Dentz return hci_disable_ext_adv_instance_sync(hdev, 0x00); 1708cba6b758SLuiz Augusto von Dentz 1709cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, 1710cba6b758SLuiz Augusto von Dentz sizeof(enable), &enable, HCI_CMD_TIMEOUT); 1711cba6b758SLuiz Augusto von Dentz } 1712e8907f76SLuiz Augusto von Dentz 1713e8907f76SLuiz Augusto von Dentz static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val, 1714e8907f76SLuiz Augusto von Dentz u8 filter_dup) 1715e8907f76SLuiz Augusto von Dentz { 1716e8907f76SLuiz Augusto von Dentz struct hci_cp_le_set_ext_scan_enable cp; 1717e8907f76SLuiz Augusto von Dentz 1718e8907f76SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 1719e8907f76SLuiz Augusto von Dentz cp.enable = val; 1720e8907f76SLuiz Augusto von Dentz cp.filter_dup = filter_dup; 1721e8907f76SLuiz Augusto von Dentz 1722e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE, 1723e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1724e8907f76SLuiz Augusto von Dentz } 1725e8907f76SLuiz Augusto von Dentz 1726e8907f76SLuiz Augusto von Dentz static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val, 1727e8907f76SLuiz Augusto von Dentz u8 filter_dup) 1728e8907f76SLuiz Augusto von Dentz { 1729e8907f76SLuiz Augusto von Dentz struct hci_cp_le_set_scan_enable cp; 1730e8907f76SLuiz Augusto von Dentz 1731e8907f76SLuiz Augusto von Dentz if (use_ext_scan(hdev)) 1732e8907f76SLuiz Augusto von Dentz return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup); 1733e8907f76SLuiz Augusto von Dentz 1734e8907f76SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 1735e8907f76SLuiz Augusto von Dentz cp.enable = val; 1736e8907f76SLuiz Augusto von Dentz cp.filter_dup = filter_dup; 1737e8907f76SLuiz Augusto von Dentz 1738e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE, 1739e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1740e8907f76SLuiz Augusto von Dentz } 1741e8907f76SLuiz Augusto von Dentz 1742e8907f76SLuiz Augusto von Dentz static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val) 1743e8907f76SLuiz Augusto von Dentz { 1744ad383c2cSLuiz Augusto von Dentz if (!use_ll_privacy(hdev)) 1745ad383c2cSLuiz Augusto von Dentz return 0; 1746ad383c2cSLuiz Augusto von Dentz 1747ad383c2cSLuiz Augusto von Dentz /* If controller is not/already resolving we are done. */ 1748ad383c2cSLuiz Augusto von Dentz if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) 1749e8907f76SLuiz Augusto von Dentz return 0; 1750e8907f76SLuiz Augusto von Dentz 1751e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, 1752e8907f76SLuiz Augusto von Dentz sizeof(val), &val, HCI_CMD_TIMEOUT); 1753e8907f76SLuiz Augusto von Dentz } 1754e8907f76SLuiz Augusto von Dentz 175527592ca1SLuiz Augusto von Dentz static int hci_scan_disable_sync(struct hci_dev *hdev) 1756e8907f76SLuiz Augusto von Dentz { 1757e8907f76SLuiz Augusto von Dentz int err; 1758e8907f76SLuiz Augusto von Dentz 1759e8907f76SLuiz Augusto von Dentz /* If controller is not scanning we are done. */ 1760e8907f76SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) 1761e8907f76SLuiz Augusto von Dentz return 0; 1762e8907f76SLuiz Augusto von Dentz 1763e8907f76SLuiz Augusto von Dentz if (hdev->scanning_paused) { 1764e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Scanning is paused for suspend"); 1765e8907f76SLuiz Augusto von Dentz return 0; 1766e8907f76SLuiz Augusto von Dentz } 1767e8907f76SLuiz Augusto von Dentz 1768e8907f76SLuiz Augusto von Dentz err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00); 1769e8907f76SLuiz Augusto von Dentz if (err) { 1770e8907f76SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to disable scanning: %d", err); 1771e8907f76SLuiz Augusto von Dentz return err; 1772e8907f76SLuiz Augusto von Dentz } 1773e8907f76SLuiz Augusto von Dentz 1774e8907f76SLuiz Augusto von Dentz return err; 1775e8907f76SLuiz Augusto von Dentz } 1776e8907f76SLuiz Augusto von Dentz 1777e8907f76SLuiz Augusto von Dentz static bool scan_use_rpa(struct hci_dev *hdev) 1778e8907f76SLuiz Augusto von Dentz { 1779e8907f76SLuiz Augusto von Dentz return hci_dev_test_flag(hdev, HCI_PRIVACY); 1780e8907f76SLuiz Augusto von Dentz } 1781e8907f76SLuiz Augusto von Dentz 1782e8907f76SLuiz Augusto von Dentz static void hci_start_interleave_scan(struct hci_dev *hdev) 1783e8907f76SLuiz Augusto von Dentz { 1784e8907f76SLuiz Augusto von Dentz hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER; 1785e8907f76SLuiz Augusto von Dentz queue_delayed_work(hdev->req_workqueue, 1786e8907f76SLuiz Augusto von Dentz &hdev->interleave_scan, 0); 1787e8907f76SLuiz Augusto von Dentz } 1788e8907f76SLuiz Augusto von Dentz 1789e8907f76SLuiz Augusto von Dentz static bool is_interleave_scanning(struct hci_dev *hdev) 1790e8907f76SLuiz Augusto von Dentz { 1791e8907f76SLuiz Augusto von Dentz return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE; 1792e8907f76SLuiz Augusto von Dentz } 1793e8907f76SLuiz Augusto von Dentz 1794e8907f76SLuiz Augusto von Dentz static void cancel_interleave_scan(struct hci_dev *hdev) 1795e8907f76SLuiz Augusto von Dentz { 1796e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "cancelling interleave scan"); 1797e8907f76SLuiz Augusto von Dentz 1798e8907f76SLuiz Augusto von Dentz cancel_delayed_work_sync(&hdev->interleave_scan); 1799e8907f76SLuiz Augusto von Dentz 1800e8907f76SLuiz Augusto von Dentz hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE; 1801e8907f76SLuiz Augusto von Dentz } 1802e8907f76SLuiz Augusto von Dentz 1803e8907f76SLuiz Augusto von Dentz /* Return true if interleave_scan wasn't started until exiting this function, 1804e8907f76SLuiz Augusto von Dentz * otherwise, return false 1805e8907f76SLuiz Augusto von Dentz */ 1806e8907f76SLuiz Augusto von Dentz static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev) 1807e8907f76SLuiz Augusto von Dentz { 1808e8907f76SLuiz Augusto von Dentz /* Do interleaved scan only if all of the following are true: 1809e8907f76SLuiz Augusto von Dentz * - There is at least one ADV monitor 1810e8907f76SLuiz Augusto von Dentz * - At least one pending LE connection or one device to be scanned for 1811e8907f76SLuiz Augusto von Dentz * - Monitor offloading is not supported 1812e8907f76SLuiz Augusto von Dentz * If so, we should alternate between allowlist scan and one without 1813e8907f76SLuiz Augusto von Dentz * any filters to save power. 1814e8907f76SLuiz Augusto von Dentz */ 1815e8907f76SLuiz Augusto von Dentz bool use_interleaving = hci_is_adv_monitoring(hdev) && 1816e8907f76SLuiz Augusto von Dentz !(list_empty(&hdev->pend_le_conns) && 1817e8907f76SLuiz Augusto von Dentz list_empty(&hdev->pend_le_reports)) && 1818e8907f76SLuiz Augusto von Dentz hci_get_adv_monitor_offload_ext(hdev) == 1819e8907f76SLuiz Augusto von Dentz HCI_ADV_MONITOR_EXT_NONE; 1820e8907f76SLuiz Augusto von Dentz bool is_interleaving = is_interleave_scanning(hdev); 1821e8907f76SLuiz Augusto von Dentz 1822e8907f76SLuiz Augusto von Dentz if (use_interleaving && !is_interleaving) { 1823e8907f76SLuiz Augusto von Dentz hci_start_interleave_scan(hdev); 1824e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "starting interleave scan"); 1825e8907f76SLuiz Augusto von Dentz return true; 1826e8907f76SLuiz Augusto von Dentz } 1827e8907f76SLuiz Augusto von Dentz 1828e8907f76SLuiz Augusto von Dentz if (!use_interleaving && is_interleaving) 1829e8907f76SLuiz Augusto von Dentz cancel_interleave_scan(hdev); 1830e8907f76SLuiz Augusto von Dentz 1831e8907f76SLuiz Augusto von Dentz return false; 1832e8907f76SLuiz Augusto von Dentz } 1833e8907f76SLuiz Augusto von Dentz 1834e8907f76SLuiz Augusto von Dentz /* Removes connection to resolve list if needed.*/ 1835e8907f76SLuiz Augusto von Dentz static int hci_le_del_resolve_list_sync(struct hci_dev *hdev, 1836e8907f76SLuiz Augusto von Dentz bdaddr_t *bdaddr, u8 bdaddr_type) 1837e8907f76SLuiz Augusto von Dentz { 1838e8907f76SLuiz Augusto von Dentz struct hci_cp_le_del_from_resolv_list cp; 1839e8907f76SLuiz Augusto von Dentz struct bdaddr_list_with_irk *entry; 1840e8907f76SLuiz Augusto von Dentz 1841ad383c2cSLuiz Augusto von Dentz if (!use_ll_privacy(hdev)) 1842e8907f76SLuiz Augusto von Dentz return 0; 1843e8907f76SLuiz Augusto von Dentz 1844e8907f76SLuiz Augusto von Dentz /* Check if the IRK has been programmed */ 1845e8907f76SLuiz Augusto von Dentz entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr, 1846e8907f76SLuiz Augusto von Dentz bdaddr_type); 1847e8907f76SLuiz Augusto von Dentz if (!entry) 1848e8907f76SLuiz Augusto von Dentz return 0; 1849e8907f76SLuiz Augusto von Dentz 1850e8907f76SLuiz Augusto von Dentz cp.bdaddr_type = bdaddr_type; 1851e8907f76SLuiz Augusto von Dentz bacpy(&cp.bdaddr, bdaddr); 1852e8907f76SLuiz Augusto von Dentz 1853e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST, 1854e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1855e8907f76SLuiz Augusto von Dentz } 1856e8907f76SLuiz Augusto von Dentz 1857e8907f76SLuiz Augusto von Dentz static int hci_le_del_accept_list_sync(struct hci_dev *hdev, 1858e8907f76SLuiz Augusto von Dentz bdaddr_t *bdaddr, u8 bdaddr_type) 1859e8907f76SLuiz Augusto von Dentz { 1860e8907f76SLuiz Augusto von Dentz struct hci_cp_le_del_from_accept_list cp; 1861e8907f76SLuiz Augusto von Dentz int err; 1862e8907f76SLuiz Augusto von Dentz 1863e8907f76SLuiz Augusto von Dentz /* Check if device is on accept list before removing it */ 1864e8907f76SLuiz Augusto von Dentz if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type)) 1865e8907f76SLuiz Augusto von Dentz return 0; 1866e8907f76SLuiz Augusto von Dentz 1867e8907f76SLuiz Augusto von Dentz cp.bdaddr_type = bdaddr_type; 1868e8907f76SLuiz Augusto von Dentz bacpy(&cp.bdaddr, bdaddr); 1869e8907f76SLuiz Augusto von Dentz 1870ad383c2cSLuiz Augusto von Dentz /* Ignore errors when removing from resolving list as that is likely 1871ad383c2cSLuiz Augusto von Dentz * that the device was never added. 1872ad383c2cSLuiz Augusto von Dentz */ 1873ad383c2cSLuiz Augusto von Dentz hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type); 1874ad383c2cSLuiz Augusto von Dentz 1875e8907f76SLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST, 1876e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1877e8907f76SLuiz Augusto von Dentz if (err) { 1878e8907f76SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to remove from allow list: %d", err); 1879e8907f76SLuiz Augusto von Dentz return err; 1880e8907f76SLuiz Augusto von Dentz } 1881e8907f76SLuiz Augusto von Dentz 1882e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr, 1883e8907f76SLuiz Augusto von Dentz cp.bdaddr_type); 1884e8907f76SLuiz Augusto von Dentz 1885ad383c2cSLuiz Augusto von Dentz return 0; 1886e8907f76SLuiz Augusto von Dentz } 1887e8907f76SLuiz Augusto von Dentz 1888cf75ad8bSLuiz Augusto von Dentz /* Adds connection to resolve list if needed. 1889cf75ad8bSLuiz Augusto von Dentz * Setting params to NULL programs local hdev->irk 1890cf75ad8bSLuiz Augusto von Dentz */ 1891e8907f76SLuiz Augusto von Dentz static int hci_le_add_resolve_list_sync(struct hci_dev *hdev, 1892e8907f76SLuiz Augusto von Dentz struct hci_conn_params *params) 1893e8907f76SLuiz Augusto von Dentz { 1894e8907f76SLuiz Augusto von Dentz struct hci_cp_le_add_to_resolv_list cp; 1895e8907f76SLuiz Augusto von Dentz struct smp_irk *irk; 1896e8907f76SLuiz Augusto von Dentz struct bdaddr_list_with_irk *entry; 1897e8907f76SLuiz Augusto von Dentz 1898ad383c2cSLuiz Augusto von Dentz if (!use_ll_privacy(hdev)) 1899e8907f76SLuiz Augusto von Dentz return 0; 1900e8907f76SLuiz Augusto von Dentz 1901cf75ad8bSLuiz Augusto von Dentz /* Attempt to program local identity address, type and irk if params is 1902cf75ad8bSLuiz Augusto von Dentz * NULL. 1903cf75ad8bSLuiz Augusto von Dentz */ 1904cf75ad8bSLuiz Augusto von Dentz if (!params) { 1905cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) 1906cf75ad8bSLuiz Augusto von Dentz return 0; 1907cf75ad8bSLuiz Augusto von Dentz 1908cf75ad8bSLuiz Augusto von Dentz hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type); 1909cf75ad8bSLuiz Augusto von Dentz memcpy(cp.peer_irk, hdev->irk, 16); 1910cf75ad8bSLuiz Augusto von Dentz goto done; 1911cf75ad8bSLuiz Augusto von Dentz } 1912cf75ad8bSLuiz Augusto von Dentz 1913e8907f76SLuiz Augusto von Dentz irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type); 1914e8907f76SLuiz Augusto von Dentz if (!irk) 1915e8907f76SLuiz Augusto von Dentz return 0; 1916e8907f76SLuiz Augusto von Dentz 1917e8907f76SLuiz Augusto von Dentz /* Check if the IK has _not_ been programmed yet. */ 1918e8907f76SLuiz Augusto von Dentz entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, 1919e8907f76SLuiz Augusto von Dentz ¶ms->addr, 1920e8907f76SLuiz Augusto von Dentz params->addr_type); 1921e8907f76SLuiz Augusto von Dentz if (entry) 1922e8907f76SLuiz Augusto von Dentz return 0; 1923e8907f76SLuiz Augusto von Dentz 1924e8907f76SLuiz Augusto von Dentz cp.bdaddr_type = params->addr_type; 1925e8907f76SLuiz Augusto von Dentz bacpy(&cp.bdaddr, ¶ms->addr); 1926e8907f76SLuiz Augusto von Dentz memcpy(cp.peer_irk, irk->val, 16); 1927e8907f76SLuiz Augusto von Dentz 19280900b1c6SLuiz Augusto von Dentz /* Default privacy mode is always Network */ 19290900b1c6SLuiz Augusto von Dentz params->privacy_mode = HCI_NETWORK_PRIVACY; 19300900b1c6SLuiz Augusto von Dentz 1931cf75ad8bSLuiz Augusto von Dentz done: 1932e8907f76SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_PRIVACY)) 1933e8907f76SLuiz Augusto von Dentz memcpy(cp.local_irk, hdev->irk, 16); 1934e8907f76SLuiz Augusto von Dentz else 1935e8907f76SLuiz Augusto von Dentz memset(cp.local_irk, 0, 16); 1936e8907f76SLuiz Augusto von Dentz 1937e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST, 1938e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1939e8907f76SLuiz Augusto von Dentz } 1940e8907f76SLuiz Augusto von Dentz 1941853b70b5SLuiz Augusto von Dentz /* Set Device Privacy Mode. */ 1942853b70b5SLuiz Augusto von Dentz static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev, 1943853b70b5SLuiz Augusto von Dentz struct hci_conn_params *params) 1944853b70b5SLuiz Augusto von Dentz { 1945853b70b5SLuiz Augusto von Dentz struct hci_cp_le_set_privacy_mode cp; 1946853b70b5SLuiz Augusto von Dentz struct smp_irk *irk; 1947853b70b5SLuiz Augusto von Dentz 1948853b70b5SLuiz Augusto von Dentz /* If device privacy mode has already been set there is nothing to do */ 1949853b70b5SLuiz Augusto von Dentz if (params->privacy_mode == HCI_DEVICE_PRIVACY) 1950853b70b5SLuiz Augusto von Dentz return 0; 1951853b70b5SLuiz Augusto von Dentz 1952853b70b5SLuiz Augusto von Dentz /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also 1953853b70b5SLuiz Augusto von Dentz * indicates that LL Privacy has been enabled and 1954853b70b5SLuiz Augusto von Dentz * HCI_OP_LE_SET_PRIVACY_MODE is supported. 1955853b70b5SLuiz Augusto von Dentz */ 1956e1cff700SLinus Torvalds if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY)) 1957853b70b5SLuiz Augusto von Dentz return 0; 1958853b70b5SLuiz Augusto von Dentz 1959853b70b5SLuiz Augusto von Dentz irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type); 1960853b70b5SLuiz Augusto von Dentz if (!irk) 1961853b70b5SLuiz Augusto von Dentz return 0; 1962853b70b5SLuiz Augusto von Dentz 1963853b70b5SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 1964853b70b5SLuiz Augusto von Dentz cp.bdaddr_type = irk->addr_type; 1965853b70b5SLuiz Augusto von Dentz bacpy(&cp.bdaddr, &irk->bdaddr); 1966853b70b5SLuiz Augusto von Dentz cp.mode = HCI_DEVICE_PRIVACY; 1967853b70b5SLuiz Augusto von Dentz 1968853b70b5SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE, 1969853b70b5SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1970853b70b5SLuiz Augusto von Dentz } 1971853b70b5SLuiz Augusto von Dentz 1972e8907f76SLuiz Augusto von Dentz /* Adds connection to allow list if needed, if the device uses RPA (has IRK) 1973853b70b5SLuiz Augusto von Dentz * this attempts to program the device in the resolving list as well and 1974853b70b5SLuiz Augusto von Dentz * properly set the privacy mode. 1975e8907f76SLuiz Augusto von Dentz */ 1976e8907f76SLuiz Augusto von Dentz static int hci_le_add_accept_list_sync(struct hci_dev *hdev, 1977e8907f76SLuiz Augusto von Dentz struct hci_conn_params *params, 1978ad383c2cSLuiz Augusto von Dentz u8 *num_entries) 1979e8907f76SLuiz Augusto von Dentz { 1980e8907f76SLuiz Augusto von Dentz struct hci_cp_le_add_to_accept_list cp; 1981e8907f76SLuiz Augusto von Dentz int err; 1982e8907f76SLuiz Augusto von Dentz 19833b420553SLuiz Augusto von Dentz /* During suspend, only wakeable devices can be in acceptlist */ 19843b420553SLuiz Augusto von Dentz if (hdev->suspended && 1985e1cff700SLinus Torvalds !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) 19863b420553SLuiz Augusto von Dentz return 0; 19873b420553SLuiz Augusto von Dentz 1988e8907f76SLuiz Augusto von Dentz /* Select filter policy to accept all advertising */ 1989e8907f76SLuiz Augusto von Dentz if (*num_entries >= hdev->le_accept_list_size) 1990e8907f76SLuiz Augusto von Dentz return -ENOSPC; 1991e8907f76SLuiz Augusto von Dentz 1992e8907f76SLuiz Augusto von Dentz /* Accept list can not be used with RPAs */ 1993ad383c2cSLuiz Augusto von Dentz if (!use_ll_privacy(hdev) && 19943b420553SLuiz Augusto von Dentz hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type)) 1995e8907f76SLuiz Augusto von Dentz return -EINVAL; 1996e8907f76SLuiz Augusto von Dentz 1997ad383c2cSLuiz Augusto von Dentz /* Attempt to program the device in the resolving list first to avoid 1998ad383c2cSLuiz Augusto von Dentz * having to rollback in case it fails since the resolving list is 1999ad383c2cSLuiz Augusto von Dentz * dynamic it can probably be smaller than the accept list. 2000ad383c2cSLuiz Augusto von Dentz */ 2001ad383c2cSLuiz Augusto von Dentz err = hci_le_add_resolve_list_sync(hdev, params); 2002ad383c2cSLuiz Augusto von Dentz if (err) { 2003ad383c2cSLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to add to resolve list: %d", err); 2004ad383c2cSLuiz Augusto von Dentz return err; 2005ad383c2cSLuiz Augusto von Dentz } 2006ad383c2cSLuiz Augusto von Dentz 2007853b70b5SLuiz Augusto von Dentz /* Set Privacy Mode */ 2008853b70b5SLuiz Augusto von Dentz err = hci_le_set_privacy_mode_sync(hdev, params); 2009853b70b5SLuiz Augusto von Dentz if (err) { 2010853b70b5SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to set privacy mode: %d", err); 2011853b70b5SLuiz Augusto von Dentz return err; 2012853b70b5SLuiz Augusto von Dentz } 2013853b70b5SLuiz Augusto von Dentz 2014853b70b5SLuiz Augusto von Dentz /* Check if already in accept list */ 2015853b70b5SLuiz Augusto von Dentz if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr, 2016853b70b5SLuiz Augusto von Dentz params->addr_type)) 2017853b70b5SLuiz Augusto von Dentz return 0; 2018853b70b5SLuiz Augusto von Dentz 2019e8907f76SLuiz Augusto von Dentz *num_entries += 1; 2020e8907f76SLuiz Augusto von Dentz cp.bdaddr_type = params->addr_type; 2021e8907f76SLuiz Augusto von Dentz bacpy(&cp.bdaddr, ¶ms->addr); 2022e8907f76SLuiz Augusto von Dentz 2023e8907f76SLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST, 2024e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2025e8907f76SLuiz Augusto von Dentz if (err) { 2026e8907f76SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to add to allow list: %d", err); 2027ad383c2cSLuiz Augusto von Dentz /* Rollback the device from the resolving list */ 2028ad383c2cSLuiz Augusto von Dentz hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type); 2029e8907f76SLuiz Augusto von Dentz return err; 2030e8907f76SLuiz Augusto von Dentz } 2031e8907f76SLuiz Augusto von Dentz 2032e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr, 2033e8907f76SLuiz Augusto von Dentz cp.bdaddr_type); 2034e8907f76SLuiz Augusto von Dentz 2035ad383c2cSLuiz Augusto von Dentz return 0; 2036e8907f76SLuiz Augusto von Dentz } 2037e8907f76SLuiz Augusto von Dentz 2038182ee45dSLuiz Augusto von Dentz /* This function disables/pause all advertising instances */ 2039ad383c2cSLuiz Augusto von Dentz static int hci_pause_advertising_sync(struct hci_dev *hdev) 2040ad383c2cSLuiz Augusto von Dentz { 2041ad383c2cSLuiz Augusto von Dentz int err; 2042182ee45dSLuiz Augusto von Dentz int old_state; 2043ad383c2cSLuiz Augusto von Dentz 20449446bddeSLuiz Augusto von Dentz /* If already been paused there is nothing to do. */ 20459446bddeSLuiz Augusto von Dentz if (hdev->advertising_paused) 2046ad383c2cSLuiz Augusto von Dentz return 0; 2047ad383c2cSLuiz Augusto von Dentz 2048182ee45dSLuiz Augusto von Dentz bt_dev_dbg(hdev, "Pausing directed advertising"); 2049182ee45dSLuiz Augusto von Dentz 2050182ee45dSLuiz Augusto von Dentz /* Stop directed advertising */ 2051182ee45dSLuiz Augusto von Dentz old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING); 2052182ee45dSLuiz Augusto von Dentz if (old_state) { 2053182ee45dSLuiz Augusto von Dentz /* When discoverable timeout triggers, then just make sure 2054182ee45dSLuiz Augusto von Dentz * the limited discoverable flag is cleared. Even in the case 2055182ee45dSLuiz Augusto von Dentz * of a timeout triggered from general discoverable, it is 2056182ee45dSLuiz Augusto von Dentz * safe to unconditionally clear the flag. 2057182ee45dSLuiz Augusto von Dentz */ 2058182ee45dSLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 2059182ee45dSLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); 2060182ee45dSLuiz Augusto von Dentz hdev->discov_timeout = 0; 2061182ee45dSLuiz Augusto von Dentz } 2062182ee45dSLuiz Augusto von Dentz 2063ad383c2cSLuiz Augusto von Dentz bt_dev_dbg(hdev, "Pausing advertising instances"); 2064ad383c2cSLuiz Augusto von Dentz 2065ad383c2cSLuiz Augusto von Dentz /* Call to disable any advertisements active on the controller. 2066ad383c2cSLuiz Augusto von Dentz * This will succeed even if no advertisements are configured. 2067ad383c2cSLuiz Augusto von Dentz */ 2068ad383c2cSLuiz Augusto von Dentz err = hci_disable_advertising_sync(hdev); 2069ad383c2cSLuiz Augusto von Dentz if (err) 2070ad383c2cSLuiz Augusto von Dentz return err; 2071ad383c2cSLuiz Augusto von Dentz 2072ad383c2cSLuiz Augusto von Dentz /* If we are using software rotation, pause the loop */ 2073ad383c2cSLuiz Augusto von Dentz if (!ext_adv_capable(hdev)) 2074ad383c2cSLuiz Augusto von Dentz cancel_adv_timeout(hdev); 2075ad383c2cSLuiz Augusto von Dentz 2076ad383c2cSLuiz Augusto von Dentz hdev->advertising_paused = true; 2077182ee45dSLuiz Augusto von Dentz hdev->advertising_old_state = old_state; 2078ad383c2cSLuiz Augusto von Dentz 2079ad383c2cSLuiz Augusto von Dentz return 0; 2080ad383c2cSLuiz Augusto von Dentz } 2081ad383c2cSLuiz Augusto von Dentz 2082182ee45dSLuiz Augusto von Dentz /* This function enables all user advertising instances */ 2083ad383c2cSLuiz Augusto von Dentz static int hci_resume_advertising_sync(struct hci_dev *hdev) 2084ad383c2cSLuiz Augusto von Dentz { 2085ad383c2cSLuiz Augusto von Dentz struct adv_info *adv, *tmp; 2086ad383c2cSLuiz Augusto von Dentz int err; 2087ad383c2cSLuiz Augusto von Dentz 2088ad383c2cSLuiz Augusto von Dentz /* If advertising has not been paused there is nothing to do. */ 2089ad383c2cSLuiz Augusto von Dentz if (!hdev->advertising_paused) 2090ad383c2cSLuiz Augusto von Dentz return 0; 2091ad383c2cSLuiz Augusto von Dentz 2092182ee45dSLuiz Augusto von Dentz /* Resume directed advertising */ 2093182ee45dSLuiz Augusto von Dentz hdev->advertising_paused = false; 2094182ee45dSLuiz Augusto von Dentz if (hdev->advertising_old_state) { 2095182ee45dSLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_ADVERTISING); 2096182ee45dSLuiz Augusto von Dentz hdev->advertising_old_state = 0; 2097182ee45dSLuiz Augusto von Dentz } 2098182ee45dSLuiz Augusto von Dentz 2099ad383c2cSLuiz Augusto von Dentz bt_dev_dbg(hdev, "Resuming advertising instances"); 2100ad383c2cSLuiz Augusto von Dentz 2101ad383c2cSLuiz Augusto von Dentz if (ext_adv_capable(hdev)) { 2102ad383c2cSLuiz Augusto von Dentz /* Call for each tracked instance to be re-enabled */ 2103ad383c2cSLuiz Augusto von Dentz list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) { 2104ad383c2cSLuiz Augusto von Dentz err = hci_enable_ext_advertising_sync(hdev, 2105ad383c2cSLuiz Augusto von Dentz adv->instance); 2106ad383c2cSLuiz Augusto von Dentz if (!err) 2107ad383c2cSLuiz Augusto von Dentz continue; 2108ad383c2cSLuiz Augusto von Dentz 2109ad383c2cSLuiz Augusto von Dentz /* If the instance cannot be resumed remove it */ 2110ad383c2cSLuiz Augusto von Dentz hci_remove_ext_adv_instance_sync(hdev, adv->instance, 2111ad383c2cSLuiz Augusto von Dentz NULL); 2112ad383c2cSLuiz Augusto von Dentz } 2113ad383c2cSLuiz Augusto von Dentz } else { 2114ad383c2cSLuiz Augusto von Dentz /* Schedule for most recent instance to be restarted and begin 2115ad383c2cSLuiz Augusto von Dentz * the software rotation loop 2116ad383c2cSLuiz Augusto von Dentz */ 2117ad383c2cSLuiz Augusto von Dentz err = hci_schedule_adv_instance_sync(hdev, 2118ad383c2cSLuiz Augusto von Dentz hdev->cur_adv_instance, 2119ad383c2cSLuiz Augusto von Dentz true); 2120ad383c2cSLuiz Augusto von Dentz } 2121ad383c2cSLuiz Augusto von Dentz 2122ad383c2cSLuiz Augusto von Dentz hdev->advertising_paused = false; 2123ad383c2cSLuiz Augusto von Dentz 2124ad383c2cSLuiz Augusto von Dentz return err; 2125ad383c2cSLuiz Augusto von Dentz } 2126ad383c2cSLuiz Augusto von Dentz 2127f892244bSBrian Gix struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev, 2128f892244bSBrian Gix bool extended, struct sock *sk) 2129f892244bSBrian Gix { 2130f892244bSBrian Gix u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA : 2131f892244bSBrian Gix HCI_OP_READ_LOCAL_OOB_DATA; 2132f892244bSBrian Gix 2133f892244bSBrian Gix return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk); 2134f892244bSBrian Gix } 2135f892244bSBrian Gix 2136ad383c2cSLuiz Augusto von Dentz /* Device must not be scanning when updating the accept list. 2137ad383c2cSLuiz Augusto von Dentz * 2138ad383c2cSLuiz Augusto von Dentz * Update is done using the following sequence: 2139ad383c2cSLuiz Augusto von Dentz * 2140ad383c2cSLuiz Augusto von Dentz * use_ll_privacy((Disable Advertising) -> Disable Resolving List) -> 2141ad383c2cSLuiz Augusto von Dentz * Remove Devices From Accept List -> 2142ad383c2cSLuiz Augusto von Dentz * (has IRK && use_ll_privacy(Remove Devices From Resolving List))-> 2143ad383c2cSLuiz Augusto von Dentz * Add Devices to Accept List -> 2144ad383c2cSLuiz Augusto von Dentz * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) -> 2145ad383c2cSLuiz Augusto von Dentz * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) -> 2146ad383c2cSLuiz Augusto von Dentz * Enable Scanning 2147ad383c2cSLuiz Augusto von Dentz * 2148ad383c2cSLuiz Augusto von Dentz * In case of failure advertising shall be restored to its original state and 2149ad383c2cSLuiz Augusto von Dentz * return would disable accept list since either accept or resolving list could 2150ad383c2cSLuiz Augusto von Dentz * not be programmed. 2151ad383c2cSLuiz Augusto von Dentz * 2152ad383c2cSLuiz Augusto von Dentz */ 2153e8907f76SLuiz Augusto von Dentz static u8 hci_update_accept_list_sync(struct hci_dev *hdev) 2154e8907f76SLuiz Augusto von Dentz { 2155e8907f76SLuiz Augusto von Dentz struct hci_conn_params *params; 2156e8907f76SLuiz Augusto von Dentz struct bdaddr_list *b, *t; 2157e8907f76SLuiz Augusto von Dentz u8 num_entries = 0; 2158e8907f76SLuiz Augusto von Dentz bool pend_conn, pend_report; 215980740ebbSLuiz Augusto von Dentz u8 filter_policy; 2160ad383c2cSLuiz Augusto von Dentz int err; 2161e8907f76SLuiz Augusto von Dentz 2162ad383c2cSLuiz Augusto von Dentz /* Pause advertising if resolving list can be used as controllers are 2163ad383c2cSLuiz Augusto von Dentz * cannot accept resolving list modifications while advertising. 2164ad383c2cSLuiz Augusto von Dentz */ 2165ad383c2cSLuiz Augusto von Dentz if (use_ll_privacy(hdev)) { 2166ad383c2cSLuiz Augusto von Dentz err = hci_pause_advertising_sync(hdev); 2167ad383c2cSLuiz Augusto von Dentz if (err) { 2168ad383c2cSLuiz Augusto von Dentz bt_dev_err(hdev, "pause advertising failed: %d", err); 2169ad383c2cSLuiz Augusto von Dentz return 0x00; 2170ad383c2cSLuiz Augusto von Dentz } 2171ad383c2cSLuiz Augusto von Dentz } 2172ad383c2cSLuiz Augusto von Dentz 2173ad383c2cSLuiz Augusto von Dentz /* Disable address resolution while reprogramming accept list since 2174ad383c2cSLuiz Augusto von Dentz * devices that do have an IRK will be programmed in the resolving list 2175ad383c2cSLuiz Augusto von Dentz * when LL Privacy is enabled. 2176ad383c2cSLuiz Augusto von Dentz */ 2177ad383c2cSLuiz Augusto von Dentz err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00); 2178ad383c2cSLuiz Augusto von Dentz if (err) { 2179ad383c2cSLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to disable LL privacy: %d", err); 2180ad383c2cSLuiz Augusto von Dentz goto done; 2181ad383c2cSLuiz Augusto von Dentz } 2182e8907f76SLuiz Augusto von Dentz 2183e8907f76SLuiz Augusto von Dentz /* Go through the current accept list programmed into the 21846828b583SLuiz Augusto von Dentz * controller one by one and check if that address is connected or is 21856828b583SLuiz Augusto von Dentz * still in the list of pending connections or list of devices to 2186e8907f76SLuiz Augusto von Dentz * report. If not present in either list, then remove it from 2187e8907f76SLuiz Augusto von Dentz * the controller. 2188e8907f76SLuiz Augusto von Dentz */ 2189e8907f76SLuiz Augusto von Dentz list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) { 21906828b583SLuiz Augusto von Dentz if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type)) 21916828b583SLuiz Augusto von Dentz continue; 21926828b583SLuiz Augusto von Dentz 2193e8907f76SLuiz Augusto von Dentz pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns, 2194e8907f76SLuiz Augusto von Dentz &b->bdaddr, 2195e8907f76SLuiz Augusto von Dentz b->bdaddr_type); 2196e8907f76SLuiz Augusto von Dentz pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports, 2197e8907f76SLuiz Augusto von Dentz &b->bdaddr, 2198e8907f76SLuiz Augusto von Dentz b->bdaddr_type); 2199e8907f76SLuiz Augusto von Dentz 2200e8907f76SLuiz Augusto von Dentz /* If the device is not likely to connect or report, 2201e8907f76SLuiz Augusto von Dentz * remove it from the acceptlist. 2202e8907f76SLuiz Augusto von Dentz */ 2203e8907f76SLuiz Augusto von Dentz if (!pend_conn && !pend_report) { 2204e8907f76SLuiz Augusto von Dentz hci_le_del_accept_list_sync(hdev, &b->bdaddr, 2205e8907f76SLuiz Augusto von Dentz b->bdaddr_type); 2206e8907f76SLuiz Augusto von Dentz continue; 2207e8907f76SLuiz Augusto von Dentz } 2208e8907f76SLuiz Augusto von Dentz 2209e8907f76SLuiz Augusto von Dentz num_entries++; 2210e8907f76SLuiz Augusto von Dentz } 2211e8907f76SLuiz Augusto von Dentz 2212e8907f76SLuiz Augusto von Dentz /* Since all no longer valid accept list entries have been 2213e8907f76SLuiz Augusto von Dentz * removed, walk through the list of pending connections 2214e8907f76SLuiz Augusto von Dentz * and ensure that any new device gets programmed into 2215e8907f76SLuiz Augusto von Dentz * the controller. 2216e8907f76SLuiz Augusto von Dentz * 2217e8907f76SLuiz Augusto von Dentz * If the list of the devices is larger than the list of 2218e8907f76SLuiz Augusto von Dentz * available accept list entries in the controller, then 2219e8907f76SLuiz Augusto von Dentz * just abort and return filer policy value to not use the 2220e8907f76SLuiz Augusto von Dentz * accept list. 2221e8907f76SLuiz Augusto von Dentz */ 2222e8907f76SLuiz Augusto von Dentz list_for_each_entry(params, &hdev->pend_le_conns, action) { 2223ad383c2cSLuiz Augusto von Dentz err = hci_le_add_accept_list_sync(hdev, params, &num_entries); 2224ad383c2cSLuiz Augusto von Dentz if (err) 2225ad383c2cSLuiz Augusto von Dentz goto done; 2226e8907f76SLuiz Augusto von Dentz } 2227e8907f76SLuiz Augusto von Dentz 2228e8907f76SLuiz Augusto von Dentz /* After adding all new pending connections, walk through 2229e8907f76SLuiz Augusto von Dentz * the list of pending reports and also add these to the 2230e8907f76SLuiz Augusto von Dentz * accept list if there is still space. Abort if space runs out. 2231e8907f76SLuiz Augusto von Dentz */ 2232e8907f76SLuiz Augusto von Dentz list_for_each_entry(params, &hdev->pend_le_reports, action) { 2233ad383c2cSLuiz Augusto von Dentz err = hci_le_add_accept_list_sync(hdev, params, &num_entries); 2234ad383c2cSLuiz Augusto von Dentz if (err) 2235ad383c2cSLuiz Augusto von Dentz goto done; 2236e8907f76SLuiz Augusto von Dentz } 2237e8907f76SLuiz Augusto von Dentz 2238e8907f76SLuiz Augusto von Dentz /* Use the allowlist unless the following conditions are all true: 2239e8907f76SLuiz Augusto von Dentz * - We are not currently suspending 2240e8907f76SLuiz Augusto von Dentz * - There are 1 or more ADV monitors registered and it's not offloaded 2241e8907f76SLuiz Augusto von Dentz * - Interleaved scanning is not currently using the allowlist 2242e8907f76SLuiz Augusto von Dentz */ 2243e8907f76SLuiz Augusto von Dentz if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended && 2244e8907f76SLuiz Augusto von Dentz hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE && 2245e8907f76SLuiz Augusto von Dentz hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST) 2246ad383c2cSLuiz Augusto von Dentz err = -EINVAL; 2247ad383c2cSLuiz Augusto von Dentz 2248ad383c2cSLuiz Augusto von Dentz done: 224980740ebbSLuiz Augusto von Dentz filter_policy = err ? 0x00 : 0x01; 225080740ebbSLuiz Augusto von Dentz 2251ad383c2cSLuiz Augusto von Dentz /* Enable address resolution when LL Privacy is enabled. */ 2252ad383c2cSLuiz Augusto von Dentz err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01); 2253ad383c2cSLuiz Augusto von Dentz if (err) 2254ad383c2cSLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to enable LL privacy: %d", err); 2255ad383c2cSLuiz Augusto von Dentz 2256ad383c2cSLuiz Augusto von Dentz /* Resume advertising if it was paused */ 2257ad383c2cSLuiz Augusto von Dentz if (use_ll_privacy(hdev)) 2258ad383c2cSLuiz Augusto von Dentz hci_resume_advertising_sync(hdev); 2259e8907f76SLuiz Augusto von Dentz 2260e8907f76SLuiz Augusto von Dentz /* Select filter policy to use accept list */ 226180740ebbSLuiz Augusto von Dentz return filter_policy; 2262e8907f76SLuiz Augusto von Dentz } 2263e8907f76SLuiz Augusto von Dentz 2264e8907f76SLuiz Augusto von Dentz /* Returns true if an le connection is in the scanning state */ 2265e8907f76SLuiz Augusto von Dentz static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev) 2266e8907f76SLuiz Augusto von Dentz { 2267e8907f76SLuiz Augusto von Dentz struct hci_conn_hash *h = &hdev->conn_hash; 2268e8907f76SLuiz Augusto von Dentz struct hci_conn *c; 2269e8907f76SLuiz Augusto von Dentz 2270e8907f76SLuiz Augusto von Dentz rcu_read_lock(); 2271e8907f76SLuiz Augusto von Dentz 2272e8907f76SLuiz Augusto von Dentz list_for_each_entry_rcu(c, &h->list, list) { 2273e8907f76SLuiz Augusto von Dentz if (c->type == LE_LINK && c->state == BT_CONNECT && 2274e8907f76SLuiz Augusto von Dentz test_bit(HCI_CONN_SCANNING, &c->flags)) { 2275e8907f76SLuiz Augusto von Dentz rcu_read_unlock(); 2276e8907f76SLuiz Augusto von Dentz return true; 2277e8907f76SLuiz Augusto von Dentz } 2278e8907f76SLuiz Augusto von Dentz } 2279e8907f76SLuiz Augusto von Dentz 2280e8907f76SLuiz Augusto von Dentz rcu_read_unlock(); 2281e8907f76SLuiz Augusto von Dentz 2282e8907f76SLuiz Augusto von Dentz return false; 2283e8907f76SLuiz Augusto von Dentz } 2284e8907f76SLuiz Augusto von Dentz 2285e8907f76SLuiz Augusto von Dentz static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type, 2286e8907f76SLuiz Augusto von Dentz u16 interval, u16 window, 2287e8907f76SLuiz Augusto von Dentz u8 own_addr_type, u8 filter_policy) 2288e8907f76SLuiz Augusto von Dentz { 2289e8907f76SLuiz Augusto von Dentz struct hci_cp_le_set_ext_scan_params *cp; 2290e8907f76SLuiz Augusto von Dentz struct hci_cp_le_scan_phy_params *phy; 2291e8907f76SLuiz Augusto von Dentz u8 data[sizeof(*cp) + sizeof(*phy) * 2]; 2292e8907f76SLuiz Augusto von Dentz u8 num_phy = 0; 2293e8907f76SLuiz Augusto von Dentz 2294e8907f76SLuiz Augusto von Dentz cp = (void *)data; 2295e8907f76SLuiz Augusto von Dentz phy = (void *)cp->data; 2296e8907f76SLuiz Augusto von Dentz 2297e8907f76SLuiz Augusto von Dentz memset(data, 0, sizeof(data)); 2298e8907f76SLuiz Augusto von Dentz 2299e8907f76SLuiz Augusto von Dentz cp->own_addr_type = own_addr_type; 2300e8907f76SLuiz Augusto von Dentz cp->filter_policy = filter_policy; 2301e8907f76SLuiz Augusto von Dentz 2302e8907f76SLuiz Augusto von Dentz if (scan_1m(hdev) || scan_2m(hdev)) { 2303e8907f76SLuiz Augusto von Dentz cp->scanning_phys |= LE_SCAN_PHY_1M; 2304e8907f76SLuiz Augusto von Dentz 2305e8907f76SLuiz Augusto von Dentz phy->type = type; 2306e8907f76SLuiz Augusto von Dentz phy->interval = cpu_to_le16(interval); 2307e8907f76SLuiz Augusto von Dentz phy->window = cpu_to_le16(window); 2308e8907f76SLuiz Augusto von Dentz 2309e8907f76SLuiz Augusto von Dentz num_phy++; 2310e8907f76SLuiz Augusto von Dentz phy++; 2311e8907f76SLuiz Augusto von Dentz } 2312e8907f76SLuiz Augusto von Dentz 2313e8907f76SLuiz Augusto von Dentz if (scan_coded(hdev)) { 2314e8907f76SLuiz Augusto von Dentz cp->scanning_phys |= LE_SCAN_PHY_CODED; 2315e8907f76SLuiz Augusto von Dentz 2316e8907f76SLuiz Augusto von Dentz phy->type = type; 2317e8907f76SLuiz Augusto von Dentz phy->interval = cpu_to_le16(interval); 2318e8907f76SLuiz Augusto von Dentz phy->window = cpu_to_le16(window); 2319e8907f76SLuiz Augusto von Dentz 2320e8907f76SLuiz Augusto von Dentz num_phy++; 2321e8907f76SLuiz Augusto von Dentz phy++; 2322e8907f76SLuiz Augusto von Dentz } 2323e8907f76SLuiz Augusto von Dentz 2324e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS, 2325e8907f76SLuiz Augusto von Dentz sizeof(*cp) + sizeof(*phy) * num_phy, 2326e8907f76SLuiz Augusto von Dentz data, HCI_CMD_TIMEOUT); 2327e8907f76SLuiz Augusto von Dentz } 2328e8907f76SLuiz Augusto von Dentz 2329e8907f76SLuiz Augusto von Dentz static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type, 2330e8907f76SLuiz Augusto von Dentz u16 interval, u16 window, 2331e8907f76SLuiz Augusto von Dentz u8 own_addr_type, u8 filter_policy) 2332e8907f76SLuiz Augusto von Dentz { 2333e8907f76SLuiz Augusto von Dentz struct hci_cp_le_set_scan_param cp; 2334e8907f76SLuiz Augusto von Dentz 2335e8907f76SLuiz Augusto von Dentz if (use_ext_scan(hdev)) 2336e8907f76SLuiz Augusto von Dentz return hci_le_set_ext_scan_param_sync(hdev, type, interval, 2337e8907f76SLuiz Augusto von Dentz window, own_addr_type, 2338e8907f76SLuiz Augusto von Dentz filter_policy); 2339e8907f76SLuiz Augusto von Dentz 2340e8907f76SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 2341e8907f76SLuiz Augusto von Dentz cp.type = type; 2342e8907f76SLuiz Augusto von Dentz cp.interval = cpu_to_le16(interval); 2343e8907f76SLuiz Augusto von Dentz cp.window = cpu_to_le16(window); 2344e8907f76SLuiz Augusto von Dentz cp.own_address_type = own_addr_type; 2345e8907f76SLuiz Augusto von Dentz cp.filter_policy = filter_policy; 2346e8907f76SLuiz Augusto von Dentz 2347e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM, 2348e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2349e8907f76SLuiz Augusto von Dentz } 2350e8907f76SLuiz Augusto von Dentz 2351e8907f76SLuiz Augusto von Dentz static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval, 2352abfeea47SLuiz Augusto von Dentz u16 window, u8 own_addr_type, u8 filter_policy, 2353abfeea47SLuiz Augusto von Dentz u8 filter_dup) 2354e8907f76SLuiz Augusto von Dentz { 2355e8907f76SLuiz Augusto von Dentz int err; 2356e8907f76SLuiz Augusto von Dentz 2357e8907f76SLuiz Augusto von Dentz if (hdev->scanning_paused) { 2358e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Scanning is paused for suspend"); 2359e8907f76SLuiz Augusto von Dentz return 0; 2360e8907f76SLuiz Augusto von Dentz } 2361e8907f76SLuiz Augusto von Dentz 2362e8907f76SLuiz Augusto von Dentz err = hci_le_set_scan_param_sync(hdev, type, interval, window, 2363e8907f76SLuiz Augusto von Dentz own_addr_type, filter_policy); 2364e8907f76SLuiz Augusto von Dentz if (err) 2365e8907f76SLuiz Augusto von Dentz return err; 2366e8907f76SLuiz Augusto von Dentz 2367abfeea47SLuiz Augusto von Dentz return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup); 2368e8907f76SLuiz Augusto von Dentz } 2369e8907f76SLuiz Augusto von Dentz 237027592ca1SLuiz Augusto von Dentz static int hci_passive_scan_sync(struct hci_dev *hdev) 2371e8907f76SLuiz Augusto von Dentz { 2372e8907f76SLuiz Augusto von Dentz u8 own_addr_type; 2373e8907f76SLuiz Augusto von Dentz u8 filter_policy; 2374e8907f76SLuiz Augusto von Dentz u16 window, interval; 2375ad383c2cSLuiz Augusto von Dentz int err; 2376e8907f76SLuiz Augusto von Dentz 2377e8907f76SLuiz Augusto von Dentz if (hdev->scanning_paused) { 2378e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Scanning is paused for suspend"); 2379e8907f76SLuiz Augusto von Dentz return 0; 2380e8907f76SLuiz Augusto von Dentz } 2381e8907f76SLuiz Augusto von Dentz 2382ad383c2cSLuiz Augusto von Dentz err = hci_scan_disable_sync(hdev); 2383ad383c2cSLuiz Augusto von Dentz if (err) { 2384ad383c2cSLuiz Augusto von Dentz bt_dev_err(hdev, "disable scanning failed: %d", err); 2385ad383c2cSLuiz Augusto von Dentz return err; 2386ad383c2cSLuiz Augusto von Dentz } 2387ad383c2cSLuiz Augusto von Dentz 2388e8907f76SLuiz Augusto von Dentz /* Set require_privacy to false since no SCAN_REQ are send 2389e8907f76SLuiz Augusto von Dentz * during passive scanning. Not using an non-resolvable address 2390e8907f76SLuiz Augusto von Dentz * here is important so that peer devices using direct 2391e8907f76SLuiz Augusto von Dentz * advertising with our address will be correctly reported 2392e8907f76SLuiz Augusto von Dentz * by the controller. 2393e8907f76SLuiz Augusto von Dentz */ 2394e8907f76SLuiz Augusto von Dentz if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev), 2395e8907f76SLuiz Augusto von Dentz &own_addr_type)) 2396e8907f76SLuiz Augusto von Dentz return 0; 2397e8907f76SLuiz Augusto von Dentz 2398e8907f76SLuiz Augusto von Dentz if (hdev->enable_advmon_interleave_scan && 2399e8907f76SLuiz Augusto von Dentz hci_update_interleaved_scan_sync(hdev)) 2400e8907f76SLuiz Augusto von Dentz return 0; 2401e8907f76SLuiz Augusto von Dentz 2402e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state); 2403ad383c2cSLuiz Augusto von Dentz 2404e8907f76SLuiz Augusto von Dentz /* Adding or removing entries from the accept list must 2405e8907f76SLuiz Augusto von Dentz * happen before enabling scanning. The controller does 2406e8907f76SLuiz Augusto von Dentz * not allow accept list modification while scanning. 2407e8907f76SLuiz Augusto von Dentz */ 2408e8907f76SLuiz Augusto von Dentz filter_policy = hci_update_accept_list_sync(hdev); 2409e8907f76SLuiz Augusto von Dentz 2410e8907f76SLuiz Augusto von Dentz /* When the controller is using random resolvable addresses and 2411e8907f76SLuiz Augusto von Dentz * with that having LE privacy enabled, then controllers with 2412e8907f76SLuiz Augusto von Dentz * Extended Scanner Filter Policies support can now enable support 2413e8907f76SLuiz Augusto von Dentz * for handling directed advertising. 2414e8907f76SLuiz Augusto von Dentz * 2415e8907f76SLuiz Augusto von Dentz * So instead of using filter polices 0x00 (no acceptlist) 2416e8907f76SLuiz Augusto von Dentz * and 0x01 (acceptlist enabled) use the new filter policies 2417e8907f76SLuiz Augusto von Dentz * 0x02 (no acceptlist) and 0x03 (acceptlist enabled). 2418e8907f76SLuiz Augusto von Dentz */ 2419e8907f76SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_PRIVACY) && 2420e8907f76SLuiz Augusto von Dentz (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)) 2421e8907f76SLuiz Augusto von Dentz filter_policy |= 0x02; 2422e8907f76SLuiz Augusto von Dentz 2423e8907f76SLuiz Augusto von Dentz if (hdev->suspended) { 2424e8907f76SLuiz Augusto von Dentz window = hdev->le_scan_window_suspend; 2425e8907f76SLuiz Augusto von Dentz interval = hdev->le_scan_int_suspend; 2426e8907f76SLuiz Augusto von Dentz } else if (hci_is_le_conn_scanning(hdev)) { 2427e8907f76SLuiz Augusto von Dentz window = hdev->le_scan_window_connect; 2428e8907f76SLuiz Augusto von Dentz interval = hdev->le_scan_int_connect; 2429e8907f76SLuiz Augusto von Dentz } else if (hci_is_adv_monitoring(hdev)) { 2430e8907f76SLuiz Augusto von Dentz window = hdev->le_scan_window_adv_monitor; 2431e8907f76SLuiz Augusto von Dentz interval = hdev->le_scan_int_adv_monitor; 2432e8907f76SLuiz Augusto von Dentz } else { 2433e8907f76SLuiz Augusto von Dentz window = hdev->le_scan_window; 2434e8907f76SLuiz Augusto von Dentz interval = hdev->le_scan_interval; 2435e8907f76SLuiz Augusto von Dentz } 2436e8907f76SLuiz Augusto von Dentz 2437e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy); 2438e8907f76SLuiz Augusto von Dentz 2439e8907f76SLuiz Augusto von Dentz return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window, 2440abfeea47SLuiz Augusto von Dentz own_addr_type, filter_policy, 2441abfeea47SLuiz Augusto von Dentz LE_SCAN_FILTER_DUP_ENABLE); 2442e8907f76SLuiz Augusto von Dentz } 2443e8907f76SLuiz Augusto von Dentz 2444e8907f76SLuiz Augusto von Dentz /* This function controls the passive scanning based on hdev->pend_le_conns 2445e8907f76SLuiz Augusto von Dentz * list. If there are pending LE connection we start the background scanning, 2446ad383c2cSLuiz Augusto von Dentz * otherwise we stop it in the following sequence: 2447ad383c2cSLuiz Augusto von Dentz * 2448ad383c2cSLuiz Augusto von Dentz * If there are devices to scan: 2449ad383c2cSLuiz Augusto von Dentz * 2450ad383c2cSLuiz Augusto von Dentz * Disable Scanning -> Update Accept List -> 2451ad383c2cSLuiz Augusto von Dentz * use_ll_privacy((Disable Advertising) -> Disable Resolving List -> 2452ad383c2cSLuiz Augusto von Dentz * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) -> 2453ad383c2cSLuiz Augusto von Dentz * Enable Scanning 2454ad383c2cSLuiz Augusto von Dentz * 2455ad383c2cSLuiz Augusto von Dentz * Otherwise: 2456ad383c2cSLuiz Augusto von Dentz * 2457ad383c2cSLuiz Augusto von Dentz * Disable Scanning 2458e8907f76SLuiz Augusto von Dentz */ 2459e8907f76SLuiz Augusto von Dentz int hci_update_passive_scan_sync(struct hci_dev *hdev) 2460e8907f76SLuiz Augusto von Dentz { 2461e8907f76SLuiz Augusto von Dentz int err; 2462e8907f76SLuiz Augusto von Dentz 2463e8907f76SLuiz Augusto von Dentz if (!test_bit(HCI_UP, &hdev->flags) || 2464e8907f76SLuiz Augusto von Dentz test_bit(HCI_INIT, &hdev->flags) || 2465e8907f76SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_SETUP) || 2466e8907f76SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_CONFIG) || 2467e8907f76SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_AUTO_OFF) || 2468e8907f76SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_UNREGISTER)) 2469e8907f76SLuiz Augusto von Dentz return 0; 2470e8907f76SLuiz Augusto von Dentz 2471e8907f76SLuiz Augusto von Dentz /* No point in doing scanning if LE support hasn't been enabled */ 2472e8907f76SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 2473e8907f76SLuiz Augusto von Dentz return 0; 2474e8907f76SLuiz Augusto von Dentz 2475e8907f76SLuiz Augusto von Dentz /* If discovery is active don't interfere with it */ 2476e8907f76SLuiz Augusto von Dentz if (hdev->discovery.state != DISCOVERY_STOPPED) 2477e8907f76SLuiz Augusto von Dentz return 0; 2478e8907f76SLuiz Augusto von Dentz 2479e8907f76SLuiz Augusto von Dentz /* Reset RSSI and UUID filters when starting background scanning 2480e8907f76SLuiz Augusto von Dentz * since these filters are meant for service discovery only. 2481e8907f76SLuiz Augusto von Dentz * 2482e8907f76SLuiz Augusto von Dentz * The Start Discovery and Start Service Discovery operations 2483e8907f76SLuiz Augusto von Dentz * ensure to set proper values for RSSI threshold and UUID 2484e8907f76SLuiz Augusto von Dentz * filter list. So it is safe to just reset them here. 2485e8907f76SLuiz Augusto von Dentz */ 2486e8907f76SLuiz Augusto von Dentz hci_discovery_filter_clear(hdev); 2487e8907f76SLuiz Augusto von Dentz 2488e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "ADV monitoring is %s", 2489e8907f76SLuiz Augusto von Dentz hci_is_adv_monitoring(hdev) ? "on" : "off"); 2490e8907f76SLuiz Augusto von Dentz 2491e8907f76SLuiz Augusto von Dentz if (list_empty(&hdev->pend_le_conns) && 2492e8907f76SLuiz Augusto von Dentz list_empty(&hdev->pend_le_reports) && 2493eca0ae4aSLuiz Augusto von Dentz !hci_is_adv_monitoring(hdev) && 2494eca0ae4aSLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_PA_SYNC)) { 2495e8907f76SLuiz Augusto von Dentz /* If there is no pending LE connections or devices 2496e8907f76SLuiz Augusto von Dentz * to be scanned for or no ADV monitors, we should stop the 2497e8907f76SLuiz Augusto von Dentz * background scanning. 2498e8907f76SLuiz Augusto von Dentz */ 2499e8907f76SLuiz Augusto von Dentz 2500e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "stopping background scanning"); 2501e8907f76SLuiz Augusto von Dentz 2502ad383c2cSLuiz Augusto von Dentz err = hci_scan_disable_sync(hdev); 2503e8907f76SLuiz Augusto von Dentz if (err) 2504e8907f76SLuiz Augusto von Dentz bt_dev_err(hdev, "stop background scanning failed: %d", 2505e8907f76SLuiz Augusto von Dentz err); 2506e8907f76SLuiz Augusto von Dentz } else { 2507e8907f76SLuiz Augusto von Dentz /* If there is at least one pending LE connection, we should 2508e8907f76SLuiz Augusto von Dentz * keep the background scan running. 2509e8907f76SLuiz Augusto von Dentz */ 2510e8907f76SLuiz Augusto von Dentz 2511e8907f76SLuiz Augusto von Dentz /* If controller is connecting, we should not start scanning 2512e8907f76SLuiz Augusto von Dentz * since some controllers are not able to scan and connect at 2513e8907f76SLuiz Augusto von Dentz * the same time. 2514e8907f76SLuiz Augusto von Dentz */ 2515e8907f76SLuiz Augusto von Dentz if (hci_lookup_le_connect(hdev)) 2516e8907f76SLuiz Augusto von Dentz return 0; 2517e8907f76SLuiz Augusto von Dentz 2518e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "start background scanning"); 2519e8907f76SLuiz Augusto von Dentz 2520e8907f76SLuiz Augusto von Dentz err = hci_passive_scan_sync(hdev); 2521e8907f76SLuiz Augusto von Dentz if (err) 2522e8907f76SLuiz Augusto von Dentz bt_dev_err(hdev, "start background scanning failed: %d", 2523e8907f76SLuiz Augusto von Dentz err); 2524e8907f76SLuiz Augusto von Dentz } 2525e8907f76SLuiz Augusto von Dentz 2526e8907f76SLuiz Augusto von Dentz return err; 2527e8907f76SLuiz Augusto von Dentz } 2528ad383c2cSLuiz Augusto von Dentz 2529bb876725SBrian Gix static int update_scan_sync(struct hci_dev *hdev, void *data) 2530bb876725SBrian Gix { 2531bb876725SBrian Gix return hci_update_scan_sync(hdev); 2532bb876725SBrian Gix } 2533bb876725SBrian Gix 2534bb876725SBrian Gix int hci_update_scan(struct hci_dev *hdev) 2535bb876725SBrian Gix { 2536bb876725SBrian Gix return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL); 2537bb876725SBrian Gix } 2538bb876725SBrian Gix 2539ad383c2cSLuiz Augusto von Dentz static int update_passive_scan_sync(struct hci_dev *hdev, void *data) 2540ad383c2cSLuiz Augusto von Dentz { 2541ad383c2cSLuiz Augusto von Dentz return hci_update_passive_scan_sync(hdev); 2542ad383c2cSLuiz Augusto von Dentz } 2543ad383c2cSLuiz Augusto von Dentz 2544ad383c2cSLuiz Augusto von Dentz int hci_update_passive_scan(struct hci_dev *hdev) 2545ad383c2cSLuiz Augusto von Dentz { 25465bee2fd6SLuiz Augusto von Dentz /* Only queue if it would have any effect */ 25475bee2fd6SLuiz Augusto von Dentz if (!test_bit(HCI_UP, &hdev->flags) || 25485bee2fd6SLuiz Augusto von Dentz test_bit(HCI_INIT, &hdev->flags) || 25495bee2fd6SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_SETUP) || 25505bee2fd6SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_CONFIG) || 25515bee2fd6SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_AUTO_OFF) || 25525bee2fd6SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_UNREGISTER)) 25535bee2fd6SLuiz Augusto von Dentz return 0; 25545bee2fd6SLuiz Augusto von Dentz 2555ad383c2cSLuiz Augusto von Dentz return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL); 2556ad383c2cSLuiz Augusto von Dentz } 2557cf75ad8bSLuiz Augusto von Dentz 25582f2eb0c9SBrian Gix int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val) 2559cf75ad8bSLuiz Augusto von Dentz { 25602f2eb0c9SBrian Gix int err; 25612f2eb0c9SBrian Gix 2562cf75ad8bSLuiz Augusto von Dentz if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev)) 2563cf75ad8bSLuiz Augusto von Dentz return 0; 2564cf75ad8bSLuiz Augusto von Dentz 25652f2eb0c9SBrian Gix err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT, 2566cf75ad8bSLuiz Augusto von Dentz sizeof(val), &val, HCI_CMD_TIMEOUT); 25672f2eb0c9SBrian Gix 25682f2eb0c9SBrian Gix if (!err) { 25692f2eb0c9SBrian Gix if (val) { 25702f2eb0c9SBrian Gix hdev->features[1][0] |= LMP_HOST_SC; 25712f2eb0c9SBrian Gix hci_dev_set_flag(hdev, HCI_SC_ENABLED); 25722f2eb0c9SBrian Gix } else { 25732f2eb0c9SBrian Gix hdev->features[1][0] &= ~LMP_HOST_SC; 25742f2eb0c9SBrian Gix hci_dev_clear_flag(hdev, HCI_SC_ENABLED); 25752f2eb0c9SBrian Gix } 25762f2eb0c9SBrian Gix } 25772f2eb0c9SBrian Gix 25782f2eb0c9SBrian Gix return err; 2579cf75ad8bSLuiz Augusto von Dentz } 2580cf75ad8bSLuiz Augusto von Dentz 25813244845cSBrian Gix int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode) 2582cf75ad8bSLuiz Augusto von Dentz { 2583cf75ad8bSLuiz Augusto von Dentz int err; 2584cf75ad8bSLuiz Augusto von Dentz 2585cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) || 2586cf75ad8bSLuiz Augusto von Dentz lmp_host_ssp_capable(hdev)) 2587cf75ad8bSLuiz Augusto von Dentz return 0; 2588cf75ad8bSLuiz Augusto von Dentz 25893244845cSBrian Gix if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) { 25903244845cSBrian Gix __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, 25913244845cSBrian Gix sizeof(mode), &mode, HCI_CMD_TIMEOUT); 25923244845cSBrian Gix } 25933244845cSBrian Gix 2594cf75ad8bSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE, 2595cf75ad8bSLuiz Augusto von Dentz sizeof(mode), &mode, HCI_CMD_TIMEOUT); 2596cf75ad8bSLuiz Augusto von Dentz if (err) 2597cf75ad8bSLuiz Augusto von Dentz return err; 2598cf75ad8bSLuiz Augusto von Dentz 2599cf75ad8bSLuiz Augusto von Dentz return hci_write_sc_support_sync(hdev, 0x01); 2600cf75ad8bSLuiz Augusto von Dentz } 2601cf75ad8bSLuiz Augusto von Dentz 2602d81a494cSBrian Gix int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul) 2603cf75ad8bSLuiz Augusto von Dentz { 2604cf75ad8bSLuiz Augusto von Dentz struct hci_cp_write_le_host_supported cp; 2605cf75ad8bSLuiz Augusto von Dentz 2606cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) || 2607cf75ad8bSLuiz Augusto von Dentz !lmp_bredr_capable(hdev)) 2608cf75ad8bSLuiz Augusto von Dentz return 0; 2609cf75ad8bSLuiz Augusto von Dentz 2610cf75ad8bSLuiz Augusto von Dentz /* Check first if we already have the right host state 2611cf75ad8bSLuiz Augusto von Dentz * (host features set) 2612cf75ad8bSLuiz Augusto von Dentz */ 2613cf75ad8bSLuiz Augusto von Dentz if (le == lmp_host_le_capable(hdev) && 2614cf75ad8bSLuiz Augusto von Dentz simul == lmp_host_le_br_capable(hdev)) 2615cf75ad8bSLuiz Augusto von Dentz return 0; 2616cf75ad8bSLuiz Augusto von Dentz 2617cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 2618cf75ad8bSLuiz Augusto von Dentz 2619cf75ad8bSLuiz Augusto von Dentz cp.le = le; 2620cf75ad8bSLuiz Augusto von Dentz cp.simul = simul; 2621cf75ad8bSLuiz Augusto von Dentz 2622cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, 2623cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2624cf75ad8bSLuiz Augusto von Dentz } 2625cf75ad8bSLuiz Augusto von Dentz 2626cf75ad8bSLuiz Augusto von Dentz static int hci_powered_update_adv_sync(struct hci_dev *hdev) 2627cf75ad8bSLuiz Augusto von Dentz { 2628cf75ad8bSLuiz Augusto von Dentz struct adv_info *adv, *tmp; 2629cf75ad8bSLuiz Augusto von Dentz int err; 2630cf75ad8bSLuiz Augusto von Dentz 2631cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 2632cf75ad8bSLuiz Augusto von Dentz return 0; 2633cf75ad8bSLuiz Augusto von Dentz 2634cf75ad8bSLuiz Augusto von Dentz /* If RPA Resolution has not been enable yet it means the 2635cf75ad8bSLuiz Augusto von Dentz * resolving list is empty and we should attempt to program the 2636cf75ad8bSLuiz Augusto von Dentz * local IRK in order to support using own_addr_type 2637cf75ad8bSLuiz Augusto von Dentz * ADDR_LE_DEV_RANDOM_RESOLVED (0x03). 2638cf75ad8bSLuiz Augusto von Dentz */ 2639cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) { 2640cf75ad8bSLuiz Augusto von Dentz hci_le_add_resolve_list_sync(hdev, NULL); 2641cf75ad8bSLuiz Augusto von Dentz hci_le_set_addr_resolution_enable_sync(hdev, 0x01); 2642cf75ad8bSLuiz Augusto von Dentz } 2643cf75ad8bSLuiz Augusto von Dentz 2644cf75ad8bSLuiz Augusto von Dentz /* Make sure the controller has a good default for 2645cf75ad8bSLuiz Augusto von Dentz * advertising data. This also applies to the case 2646cf75ad8bSLuiz Augusto von Dentz * where BR/EDR was toggled during the AUTO_OFF phase. 2647cf75ad8bSLuiz Augusto von Dentz */ 2648cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || 2649cf75ad8bSLuiz Augusto von Dentz list_empty(&hdev->adv_instances)) { 2650cf75ad8bSLuiz Augusto von Dentz if (ext_adv_capable(hdev)) { 2651cf75ad8bSLuiz Augusto von Dentz err = hci_setup_ext_adv_instance_sync(hdev, 0x00); 2652cf75ad8bSLuiz Augusto von Dentz if (!err) 2653cf75ad8bSLuiz Augusto von Dentz hci_update_scan_rsp_data_sync(hdev, 0x00); 2654cf75ad8bSLuiz Augusto von Dentz } else { 2655cf75ad8bSLuiz Augusto von Dentz err = hci_update_adv_data_sync(hdev, 0x00); 2656cf75ad8bSLuiz Augusto von Dentz if (!err) 2657cf75ad8bSLuiz Augusto von Dentz hci_update_scan_rsp_data_sync(hdev, 0x00); 2658cf75ad8bSLuiz Augusto von Dentz } 2659cf75ad8bSLuiz Augusto von Dentz 2660cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) 2661cf75ad8bSLuiz Augusto von Dentz hci_enable_advertising_sync(hdev); 2662cf75ad8bSLuiz Augusto von Dentz } 2663cf75ad8bSLuiz Augusto von Dentz 2664cf75ad8bSLuiz Augusto von Dentz /* Call for each tracked instance to be scheduled */ 2665cf75ad8bSLuiz Augusto von Dentz list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) 2666cf75ad8bSLuiz Augusto von Dentz hci_schedule_adv_instance_sync(hdev, adv->instance, true); 2667cf75ad8bSLuiz Augusto von Dentz 2668cf75ad8bSLuiz Augusto von Dentz return 0; 2669cf75ad8bSLuiz Augusto von Dentz } 2670cf75ad8bSLuiz Augusto von Dentz 2671cf75ad8bSLuiz Augusto von Dentz static int hci_write_auth_enable_sync(struct hci_dev *hdev) 2672cf75ad8bSLuiz Augusto von Dentz { 2673cf75ad8bSLuiz Augusto von Dentz u8 link_sec; 2674cf75ad8bSLuiz Augusto von Dentz 2675cf75ad8bSLuiz Augusto von Dentz link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY); 2676cf75ad8bSLuiz Augusto von Dentz if (link_sec == test_bit(HCI_AUTH, &hdev->flags)) 2677cf75ad8bSLuiz Augusto von Dentz return 0; 2678cf75ad8bSLuiz Augusto von Dentz 2679cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE, 2680cf75ad8bSLuiz Augusto von Dentz sizeof(link_sec), &link_sec, 2681cf75ad8bSLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 2682cf75ad8bSLuiz Augusto von Dentz } 2683cf75ad8bSLuiz Augusto von Dentz 2684353a0249SBrian Gix int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable) 2685cf75ad8bSLuiz Augusto von Dentz { 2686cf75ad8bSLuiz Augusto von Dentz struct hci_cp_write_page_scan_activity cp; 2687cf75ad8bSLuiz Augusto von Dentz u8 type; 2688cf75ad8bSLuiz Augusto von Dentz int err = 0; 2689cf75ad8bSLuiz Augusto von Dentz 2690cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 2691cf75ad8bSLuiz Augusto von Dentz return 0; 2692cf75ad8bSLuiz Augusto von Dentz 2693cf75ad8bSLuiz Augusto von Dentz if (hdev->hci_ver < BLUETOOTH_VER_1_2) 2694cf75ad8bSLuiz Augusto von Dentz return 0; 2695cf75ad8bSLuiz Augusto von Dentz 2696cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 2697cf75ad8bSLuiz Augusto von Dentz 2698cf75ad8bSLuiz Augusto von Dentz if (enable) { 2699cf75ad8bSLuiz Augusto von Dentz type = PAGE_SCAN_TYPE_INTERLACED; 2700cf75ad8bSLuiz Augusto von Dentz 2701cf75ad8bSLuiz Augusto von Dentz /* 160 msec page scan interval */ 2702cf75ad8bSLuiz Augusto von Dentz cp.interval = cpu_to_le16(0x0100); 2703cf75ad8bSLuiz Augusto von Dentz } else { 2704cf75ad8bSLuiz Augusto von Dentz type = hdev->def_page_scan_type; 2705cf75ad8bSLuiz Augusto von Dentz cp.interval = cpu_to_le16(hdev->def_page_scan_int); 2706cf75ad8bSLuiz Augusto von Dentz } 2707cf75ad8bSLuiz Augusto von Dentz 2708cf75ad8bSLuiz Augusto von Dentz cp.window = cpu_to_le16(hdev->def_page_scan_window); 2709cf75ad8bSLuiz Augusto von Dentz 2710cf75ad8bSLuiz Augusto von Dentz if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval || 2711cf75ad8bSLuiz Augusto von Dentz __cpu_to_le16(hdev->page_scan_window) != cp.window) { 2712cf75ad8bSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, 2713cf75ad8bSLuiz Augusto von Dentz HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, 2714cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2715cf75ad8bSLuiz Augusto von Dentz if (err) 2716cf75ad8bSLuiz Augusto von Dentz return err; 2717cf75ad8bSLuiz Augusto von Dentz } 2718cf75ad8bSLuiz Augusto von Dentz 2719cf75ad8bSLuiz Augusto von Dentz if (hdev->page_scan_type != type) 2720cf75ad8bSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, 2721cf75ad8bSLuiz Augusto von Dentz HCI_OP_WRITE_PAGE_SCAN_TYPE, 2722cf75ad8bSLuiz Augusto von Dentz sizeof(type), &type, 2723cf75ad8bSLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 2724cf75ad8bSLuiz Augusto von Dentz 2725cf75ad8bSLuiz Augusto von Dentz return err; 2726cf75ad8bSLuiz Augusto von Dentz } 2727cf75ad8bSLuiz Augusto von Dentz 2728cf75ad8bSLuiz Augusto von Dentz static bool disconnected_accept_list_entries(struct hci_dev *hdev) 2729cf75ad8bSLuiz Augusto von Dentz { 2730cf75ad8bSLuiz Augusto von Dentz struct bdaddr_list *b; 2731cf75ad8bSLuiz Augusto von Dentz 2732cf75ad8bSLuiz Augusto von Dentz list_for_each_entry(b, &hdev->accept_list, list) { 2733cf75ad8bSLuiz Augusto von Dentz struct hci_conn *conn; 2734cf75ad8bSLuiz Augusto von Dentz 2735cf75ad8bSLuiz Augusto von Dentz conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr); 2736cf75ad8bSLuiz Augusto von Dentz if (!conn) 2737cf75ad8bSLuiz Augusto von Dentz return true; 2738cf75ad8bSLuiz Augusto von Dentz 2739cf75ad8bSLuiz Augusto von Dentz if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) 2740cf75ad8bSLuiz Augusto von Dentz return true; 2741cf75ad8bSLuiz Augusto von Dentz } 2742cf75ad8bSLuiz Augusto von Dentz 2743cf75ad8bSLuiz Augusto von Dentz return false; 2744cf75ad8bSLuiz Augusto von Dentz } 2745cf75ad8bSLuiz Augusto von Dentz 2746cf75ad8bSLuiz Augusto von Dentz static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val) 2747cf75ad8bSLuiz Augusto von Dentz { 2748cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE, 2749cf75ad8bSLuiz Augusto von Dentz sizeof(val), &val, 2750cf75ad8bSLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 2751cf75ad8bSLuiz Augusto von Dentz } 2752cf75ad8bSLuiz Augusto von Dentz 2753451d95a9SBrian Gix int hci_update_scan_sync(struct hci_dev *hdev) 2754cf75ad8bSLuiz Augusto von Dentz { 2755cf75ad8bSLuiz Augusto von Dentz u8 scan; 2756cf75ad8bSLuiz Augusto von Dentz 2757cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 2758cf75ad8bSLuiz Augusto von Dentz return 0; 2759cf75ad8bSLuiz Augusto von Dentz 2760cf75ad8bSLuiz Augusto von Dentz if (!hdev_is_powered(hdev)) 2761cf75ad8bSLuiz Augusto von Dentz return 0; 2762cf75ad8bSLuiz Augusto von Dentz 2763cf75ad8bSLuiz Augusto von Dentz if (mgmt_powering_down(hdev)) 2764cf75ad8bSLuiz Augusto von Dentz return 0; 2765cf75ad8bSLuiz Augusto von Dentz 2766cf75ad8bSLuiz Augusto von Dentz if (hdev->scanning_paused) 2767cf75ad8bSLuiz Augusto von Dentz return 0; 2768cf75ad8bSLuiz Augusto von Dentz 2769cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) || 2770cf75ad8bSLuiz Augusto von Dentz disconnected_accept_list_entries(hdev)) 2771cf75ad8bSLuiz Augusto von Dentz scan = SCAN_PAGE; 2772cf75ad8bSLuiz Augusto von Dentz else 2773cf75ad8bSLuiz Augusto von Dentz scan = SCAN_DISABLED; 2774cf75ad8bSLuiz Augusto von Dentz 2775cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) 2776cf75ad8bSLuiz Augusto von Dentz scan |= SCAN_INQUIRY; 2777cf75ad8bSLuiz Augusto von Dentz 2778cf75ad8bSLuiz Augusto von Dentz if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) && 2779cf75ad8bSLuiz Augusto von Dentz test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY)) 2780cf75ad8bSLuiz Augusto von Dentz return 0; 2781cf75ad8bSLuiz Augusto von Dentz 2782cf75ad8bSLuiz Augusto von Dentz return hci_write_scan_enable_sync(hdev, scan); 2783cf75ad8bSLuiz Augusto von Dentz } 2784cf75ad8bSLuiz Augusto von Dentz 27856f6ff38aSBrian Gix int hci_update_name_sync(struct hci_dev *hdev) 2786cf75ad8bSLuiz Augusto von Dentz { 2787cf75ad8bSLuiz Augusto von Dentz struct hci_cp_write_local_name cp; 2788cf75ad8bSLuiz Augusto von Dentz 2789cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 2790cf75ad8bSLuiz Augusto von Dentz 2791cf75ad8bSLuiz Augusto von Dentz memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); 2792cf75ad8bSLuiz Augusto von Dentz 2793cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME, 2794cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, 2795cf75ad8bSLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 2796cf75ad8bSLuiz Augusto von Dentz } 2797cf75ad8bSLuiz Augusto von Dentz 2798cf75ad8bSLuiz Augusto von Dentz /* This function perform powered update HCI command sequence after the HCI init 2799cf75ad8bSLuiz Augusto von Dentz * sequence which end up resetting all states, the sequence is as follows: 2800cf75ad8bSLuiz Augusto von Dentz * 2801cf75ad8bSLuiz Augusto von Dentz * HCI_SSP_ENABLED(Enable SSP) 2802cf75ad8bSLuiz Augusto von Dentz * HCI_LE_ENABLED(Enable LE) 2803cf75ad8bSLuiz Augusto von Dentz * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) -> 2804cf75ad8bSLuiz Augusto von Dentz * Update adv data) 2805cf75ad8bSLuiz Augusto von Dentz * Enable Authentication 2806cf75ad8bSLuiz Augusto von Dentz * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class -> 2807cf75ad8bSLuiz Augusto von Dentz * Set Name -> Set EIR) 2808cf75ad8bSLuiz Augusto von Dentz */ 2809cf75ad8bSLuiz Augusto von Dentz int hci_powered_update_sync(struct hci_dev *hdev) 2810cf75ad8bSLuiz Augusto von Dentz { 2811cf75ad8bSLuiz Augusto von Dentz int err; 2812cf75ad8bSLuiz Augusto von Dentz 2813cf75ad8bSLuiz Augusto von Dentz /* Register the available SMP channels (BR/EDR and LE) only when 2814cf75ad8bSLuiz Augusto von Dentz * successfully powering on the controller. This late 2815cf75ad8bSLuiz Augusto von Dentz * registration is required so that LE SMP can clearly decide if 2816cf75ad8bSLuiz Augusto von Dentz * the public address or static address is used. 2817cf75ad8bSLuiz Augusto von Dentz */ 2818cf75ad8bSLuiz Augusto von Dentz smp_register(hdev); 2819cf75ad8bSLuiz Augusto von Dentz 2820cf75ad8bSLuiz Augusto von Dentz err = hci_write_ssp_mode_sync(hdev, 0x01); 2821cf75ad8bSLuiz Augusto von Dentz if (err) 2822cf75ad8bSLuiz Augusto von Dentz return err; 2823cf75ad8bSLuiz Augusto von Dentz 2824cf75ad8bSLuiz Augusto von Dentz err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00); 2825cf75ad8bSLuiz Augusto von Dentz if (err) 2826cf75ad8bSLuiz Augusto von Dentz return err; 2827cf75ad8bSLuiz Augusto von Dentz 2828cf75ad8bSLuiz Augusto von Dentz err = hci_powered_update_adv_sync(hdev); 2829cf75ad8bSLuiz Augusto von Dentz if (err) 2830cf75ad8bSLuiz Augusto von Dentz return err; 2831cf75ad8bSLuiz Augusto von Dentz 2832cf75ad8bSLuiz Augusto von Dentz err = hci_write_auth_enable_sync(hdev); 2833cf75ad8bSLuiz Augusto von Dentz if (err) 2834cf75ad8bSLuiz Augusto von Dentz return err; 2835cf75ad8bSLuiz Augusto von Dentz 2836cf75ad8bSLuiz Augusto von Dentz if (lmp_bredr_capable(hdev)) { 2837cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) 2838cf75ad8bSLuiz Augusto von Dentz hci_write_fast_connectable_sync(hdev, true); 2839cf75ad8bSLuiz Augusto von Dentz else 2840cf75ad8bSLuiz Augusto von Dentz hci_write_fast_connectable_sync(hdev, false); 2841cf75ad8bSLuiz Augusto von Dentz hci_update_scan_sync(hdev); 2842cf75ad8bSLuiz Augusto von Dentz hci_update_class_sync(hdev); 2843cf75ad8bSLuiz Augusto von Dentz hci_update_name_sync(hdev); 2844cf75ad8bSLuiz Augusto von Dentz hci_update_eir_sync(hdev); 2845cf75ad8bSLuiz Augusto von Dentz } 2846cf75ad8bSLuiz Augusto von Dentz 2847cf75ad8bSLuiz Augusto von Dentz return 0; 2848cf75ad8bSLuiz Augusto von Dentz } 2849cf75ad8bSLuiz Augusto von Dentz 2850d0b13706SLuiz Augusto von Dentz /** 2851d0b13706SLuiz Augusto von Dentz * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address 2852d0b13706SLuiz Augusto von Dentz * (BD_ADDR) for a HCI device from 2853d0b13706SLuiz Augusto von Dentz * a firmware node property. 2854d0b13706SLuiz Augusto von Dentz * @hdev: The HCI device 2855d0b13706SLuiz Augusto von Dentz * 2856d0b13706SLuiz Augusto von Dentz * Search the firmware node for 'local-bd-address'. 2857d0b13706SLuiz Augusto von Dentz * 2858d0b13706SLuiz Augusto von Dentz * All-zero BD addresses are rejected, because those could be properties 2859d0b13706SLuiz Augusto von Dentz * that exist in the firmware tables, but were not updated by the firmware. For 2860d0b13706SLuiz Augusto von Dentz * example, the DTS could define 'local-bd-address', with zero BD addresses. 2861d0b13706SLuiz Augusto von Dentz */ 2862d0b13706SLuiz Augusto von Dentz static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev) 2863d0b13706SLuiz Augusto von Dentz { 2864d0b13706SLuiz Augusto von Dentz struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent); 2865d0b13706SLuiz Augusto von Dentz bdaddr_t ba; 2866d0b13706SLuiz Augusto von Dentz int ret; 2867d0b13706SLuiz Augusto von Dentz 2868d0b13706SLuiz Augusto von Dentz ret = fwnode_property_read_u8_array(fwnode, "local-bd-address", 2869d0b13706SLuiz Augusto von Dentz (u8 *)&ba, sizeof(ba)); 2870d0b13706SLuiz Augusto von Dentz if (ret < 0 || !bacmp(&ba, BDADDR_ANY)) 2871d0b13706SLuiz Augusto von Dentz return; 2872d0b13706SLuiz Augusto von Dentz 2873d0b13706SLuiz Augusto von Dentz bacpy(&hdev->public_addr, &ba); 2874d0b13706SLuiz Augusto von Dentz } 2875d0b13706SLuiz Augusto von Dentz 2876d0b13706SLuiz Augusto von Dentz struct hci_init_stage { 2877d0b13706SLuiz Augusto von Dentz int (*func)(struct hci_dev *hdev); 2878d0b13706SLuiz Augusto von Dentz }; 2879d0b13706SLuiz Augusto von Dentz 2880d0b13706SLuiz Augusto von Dentz /* Run init stage NULL terminated function table */ 2881d0b13706SLuiz Augusto von Dentz static int hci_init_stage_sync(struct hci_dev *hdev, 2882d0b13706SLuiz Augusto von Dentz const struct hci_init_stage *stage) 2883d0b13706SLuiz Augusto von Dentz { 2884d0b13706SLuiz Augusto von Dentz size_t i; 2885d0b13706SLuiz Augusto von Dentz 2886d0b13706SLuiz Augusto von Dentz for (i = 0; stage[i].func; i++) { 2887d0b13706SLuiz Augusto von Dentz int err; 2888d0b13706SLuiz Augusto von Dentz 2889d0b13706SLuiz Augusto von Dentz err = stage[i].func(hdev); 2890d0b13706SLuiz Augusto von Dentz if (err) 2891d0b13706SLuiz Augusto von Dentz return err; 2892d0b13706SLuiz Augusto von Dentz } 2893d0b13706SLuiz Augusto von Dentz 2894d0b13706SLuiz Augusto von Dentz return 0; 2895d0b13706SLuiz Augusto von Dentz } 2896d0b13706SLuiz Augusto von Dentz 2897d0b13706SLuiz Augusto von Dentz /* Read Local Version */ 2898d0b13706SLuiz Augusto von Dentz static int hci_read_local_version_sync(struct hci_dev *hdev) 2899d0b13706SLuiz Augusto von Dentz { 2900d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION, 2901d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 2902d0b13706SLuiz Augusto von Dentz } 2903d0b13706SLuiz Augusto von Dentz 2904d0b13706SLuiz Augusto von Dentz /* Read BD Address */ 2905d0b13706SLuiz Augusto von Dentz static int hci_read_bd_addr_sync(struct hci_dev *hdev) 2906d0b13706SLuiz Augusto von Dentz { 2907d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR, 2908d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 2909d0b13706SLuiz Augusto von Dentz } 2910d0b13706SLuiz Augusto von Dentz 2911d0b13706SLuiz Augusto von Dentz #define HCI_INIT(_func) \ 2912d0b13706SLuiz Augusto von Dentz { \ 2913d0b13706SLuiz Augusto von Dentz .func = _func, \ 2914d0b13706SLuiz Augusto von Dentz } 2915d0b13706SLuiz Augusto von Dentz 2916d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init0[] = { 2917d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_VERSION */ 2918d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_version_sync), 2919d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_BD_ADDR */ 2920d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_bd_addr_sync), 2921d0b13706SLuiz Augusto von Dentz {} 2922d0b13706SLuiz Augusto von Dentz }; 2923d0b13706SLuiz Augusto von Dentz 2924d0b13706SLuiz Augusto von Dentz int hci_reset_sync(struct hci_dev *hdev) 2925d0b13706SLuiz Augusto von Dentz { 2926d0b13706SLuiz Augusto von Dentz int err; 2927d0b13706SLuiz Augusto von Dentz 2928d0b13706SLuiz Augusto von Dentz set_bit(HCI_RESET, &hdev->flags); 2929d0b13706SLuiz Augusto von Dentz 2930d0b13706SLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL, 2931d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 2932d0b13706SLuiz Augusto von Dentz if (err) 2933d0b13706SLuiz Augusto von Dentz return err; 2934d0b13706SLuiz Augusto von Dentz 2935d0b13706SLuiz Augusto von Dentz return 0; 2936d0b13706SLuiz Augusto von Dentz } 2937d0b13706SLuiz Augusto von Dentz 2938d0b13706SLuiz Augusto von Dentz static int hci_init0_sync(struct hci_dev *hdev) 2939d0b13706SLuiz Augusto von Dentz { 2940d0b13706SLuiz Augusto von Dentz int err; 2941d0b13706SLuiz Augusto von Dentz 2942d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 2943d0b13706SLuiz Augusto von Dentz 2944d0b13706SLuiz Augusto von Dentz /* Reset */ 2945d0b13706SLuiz Augusto von Dentz if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { 2946d0b13706SLuiz Augusto von Dentz err = hci_reset_sync(hdev); 2947d0b13706SLuiz Augusto von Dentz if (err) 2948d0b13706SLuiz Augusto von Dentz return err; 2949d0b13706SLuiz Augusto von Dentz } 2950d0b13706SLuiz Augusto von Dentz 2951d0b13706SLuiz Augusto von Dentz return hci_init_stage_sync(hdev, hci_init0); 2952d0b13706SLuiz Augusto von Dentz } 2953d0b13706SLuiz Augusto von Dentz 2954d0b13706SLuiz Augusto von Dentz static int hci_unconf_init_sync(struct hci_dev *hdev) 2955d0b13706SLuiz Augusto von Dentz { 2956d0b13706SLuiz Augusto von Dentz int err; 2957d0b13706SLuiz Augusto von Dentz 2958d0b13706SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 2959d0b13706SLuiz Augusto von Dentz return 0; 2960d0b13706SLuiz Augusto von Dentz 2961d0b13706SLuiz Augusto von Dentz err = hci_init0_sync(hdev); 2962d0b13706SLuiz Augusto von Dentz if (err < 0) 2963d0b13706SLuiz Augusto von Dentz return err; 2964d0b13706SLuiz Augusto von Dentz 2965d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_SETUP)) 2966d0b13706SLuiz Augusto von Dentz hci_debugfs_create_basic(hdev); 2967d0b13706SLuiz Augusto von Dentz 2968d0b13706SLuiz Augusto von Dentz return 0; 2969d0b13706SLuiz Augusto von Dentz } 2970d0b13706SLuiz Augusto von Dentz 2971d0b13706SLuiz Augusto von Dentz /* Read Local Supported Features. */ 2972d0b13706SLuiz Augusto von Dentz static int hci_read_local_features_sync(struct hci_dev *hdev) 2973d0b13706SLuiz Augusto von Dentz { 2974d0b13706SLuiz Augusto von Dentz /* Not all AMP controllers support this command */ 2975d0b13706SLuiz Augusto von Dentz if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20)) 2976d0b13706SLuiz Augusto von Dentz return 0; 2977d0b13706SLuiz Augusto von Dentz 2978d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES, 2979d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 2980d0b13706SLuiz Augusto von Dentz } 2981d0b13706SLuiz Augusto von Dentz 2982d0b13706SLuiz Augusto von Dentz /* BR Controller init stage 1 command sequence */ 2983d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage br_init1[] = { 2984d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_FEATURES */ 2985d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_features_sync), 2986d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_VERSION */ 2987d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_version_sync), 2988d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_BD_ADDR */ 2989d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_bd_addr_sync), 2990d0b13706SLuiz Augusto von Dentz {} 2991d0b13706SLuiz Augusto von Dentz }; 2992d0b13706SLuiz Augusto von Dentz 2993d0b13706SLuiz Augusto von Dentz /* Read Local Commands */ 2994d0b13706SLuiz Augusto von Dentz static int hci_read_local_cmds_sync(struct hci_dev *hdev) 2995d0b13706SLuiz Augusto von Dentz { 2996d0b13706SLuiz Augusto von Dentz /* All Bluetooth 1.2 and later controllers should support the 2997d0b13706SLuiz Augusto von Dentz * HCI command for reading the local supported commands. 2998d0b13706SLuiz Augusto von Dentz * 2999d0b13706SLuiz Augusto von Dentz * Unfortunately some controllers indicate Bluetooth 1.2 support, 3000d0b13706SLuiz Augusto von Dentz * but do not have support for this command. If that is the case, 3001d0b13706SLuiz Augusto von Dentz * the driver can quirk the behavior and skip reading the local 3002d0b13706SLuiz Augusto von Dentz * supported commands. 3003d0b13706SLuiz Augusto von Dentz */ 3004d0b13706SLuiz Augusto von Dentz if (hdev->hci_ver > BLUETOOTH_VER_1_1 && 3005d0b13706SLuiz Augusto von Dentz !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks)) 3006d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS, 3007d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3008d0b13706SLuiz Augusto von Dentz 3009d0b13706SLuiz Augusto von Dentz return 0; 3010d0b13706SLuiz Augusto von Dentz } 3011d0b13706SLuiz Augusto von Dentz 3012d0b13706SLuiz Augusto von Dentz /* Read Local AMP Info */ 3013d0b13706SLuiz Augusto von Dentz static int hci_read_local_amp_info_sync(struct hci_dev *hdev) 3014d0b13706SLuiz Augusto von Dentz { 3015d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 3016d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3017d0b13706SLuiz Augusto von Dentz } 3018d0b13706SLuiz Augusto von Dentz 3019d0b13706SLuiz Augusto von Dentz /* Read Data Blk size */ 3020d0b13706SLuiz Augusto von Dentz static int hci_read_data_block_size_sync(struct hci_dev *hdev) 3021d0b13706SLuiz Augusto von Dentz { 3022d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE, 3023d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3024d0b13706SLuiz Augusto von Dentz } 3025d0b13706SLuiz Augusto von Dentz 3026d0b13706SLuiz Augusto von Dentz /* Read Flow Control Mode */ 3027d0b13706SLuiz Augusto von Dentz static int hci_read_flow_control_mode_sync(struct hci_dev *hdev) 3028d0b13706SLuiz Augusto von Dentz { 3029d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE, 3030d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3031d0b13706SLuiz Augusto von Dentz } 3032d0b13706SLuiz Augusto von Dentz 3033d0b13706SLuiz Augusto von Dentz /* Read Location Data */ 3034d0b13706SLuiz Augusto von Dentz static int hci_read_location_data_sync(struct hci_dev *hdev) 3035d0b13706SLuiz Augusto von Dentz { 3036d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA, 3037d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3038d0b13706SLuiz Augusto von Dentz } 3039d0b13706SLuiz Augusto von Dentz 3040d0b13706SLuiz Augusto von Dentz /* AMP Controller init stage 1 command sequence */ 3041d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage amp_init1[] = { 3042d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_VERSION */ 3043d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_version_sync), 3044d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_COMMANDS */ 3045d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_cmds_sync), 3046d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_AMP_INFO */ 3047d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_amp_info_sync), 3048d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_DATA_BLOCK_SIZE */ 3049d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_data_block_size_sync), 3050d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_FLOW_CONTROL_MODE */ 3051d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_flow_control_mode_sync), 3052d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCATION_DATA */ 3053d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_location_data_sync), 3054d0b13706SLuiz Augusto von Dentz }; 3055d0b13706SLuiz Augusto von Dentz 3056d0b13706SLuiz Augusto von Dentz static int hci_init1_sync(struct hci_dev *hdev) 3057d0b13706SLuiz Augusto von Dentz { 3058d0b13706SLuiz Augusto von Dentz int err; 3059d0b13706SLuiz Augusto von Dentz 3060d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 3061d0b13706SLuiz Augusto von Dentz 3062d0b13706SLuiz Augusto von Dentz /* Reset */ 3063d0b13706SLuiz Augusto von Dentz if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { 3064d0b13706SLuiz Augusto von Dentz err = hci_reset_sync(hdev); 3065d0b13706SLuiz Augusto von Dentz if (err) 3066d0b13706SLuiz Augusto von Dentz return err; 3067d0b13706SLuiz Augusto von Dentz } 3068d0b13706SLuiz Augusto von Dentz 3069d0b13706SLuiz Augusto von Dentz switch (hdev->dev_type) { 3070d0b13706SLuiz Augusto von Dentz case HCI_PRIMARY: 3071d0b13706SLuiz Augusto von Dentz hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; 3072d0b13706SLuiz Augusto von Dentz return hci_init_stage_sync(hdev, br_init1); 3073d0b13706SLuiz Augusto von Dentz case HCI_AMP: 3074d0b13706SLuiz Augusto von Dentz hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; 3075d0b13706SLuiz Augusto von Dentz return hci_init_stage_sync(hdev, amp_init1); 3076d0b13706SLuiz Augusto von Dentz default: 3077d0b13706SLuiz Augusto von Dentz bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type); 3078d0b13706SLuiz Augusto von Dentz break; 3079d0b13706SLuiz Augusto von Dentz } 3080d0b13706SLuiz Augusto von Dentz 3081d0b13706SLuiz Augusto von Dentz return 0; 3082d0b13706SLuiz Augusto von Dentz } 3083d0b13706SLuiz Augusto von Dentz 3084d0b13706SLuiz Augusto von Dentz /* AMP Controller init stage 2 command sequence */ 3085d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage amp_init2[] = { 3086d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_FEATURES */ 3087d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_features_sync), 3088d0b13706SLuiz Augusto von Dentz }; 3089d0b13706SLuiz Augusto von Dentz 3090d0b13706SLuiz Augusto von Dentz /* Read Buffer Size (ACL mtu, max pkt, etc.) */ 3091d0b13706SLuiz Augusto von Dentz static int hci_read_buffer_size_sync(struct hci_dev *hdev) 3092d0b13706SLuiz Augusto von Dentz { 309326afbd82SLuiz Augusto von Dentz /* Use Read LE Buffer Size V2 if supported */ 309426afbd82SLuiz Augusto von Dentz if (hdev->commands[41] & 0x20) 309526afbd82SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, 309626afbd82SLuiz Augusto von Dentz HCI_OP_LE_READ_BUFFER_SIZE_V2, 309726afbd82SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 309826afbd82SLuiz Augusto von Dentz 3099d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE, 3100d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3101d0b13706SLuiz Augusto von Dentz } 3102d0b13706SLuiz Augusto von Dentz 3103d0b13706SLuiz Augusto von Dentz /* Read Class of Device */ 3104d0b13706SLuiz Augusto von Dentz static int hci_read_dev_class_sync(struct hci_dev *hdev) 3105d0b13706SLuiz Augusto von Dentz { 3106d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV, 3107d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3108d0b13706SLuiz Augusto von Dentz } 3109d0b13706SLuiz Augusto von Dentz 3110d0b13706SLuiz Augusto von Dentz /* Read Local Name */ 3111d0b13706SLuiz Augusto von Dentz static int hci_read_local_name_sync(struct hci_dev *hdev) 3112d0b13706SLuiz Augusto von Dentz { 3113d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME, 3114d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3115d0b13706SLuiz Augusto von Dentz } 3116d0b13706SLuiz Augusto von Dentz 3117d0b13706SLuiz Augusto von Dentz /* Read Voice Setting */ 3118d0b13706SLuiz Augusto von Dentz static int hci_read_voice_setting_sync(struct hci_dev *hdev) 3119d0b13706SLuiz Augusto von Dentz { 3120d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING, 3121d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3122d0b13706SLuiz Augusto von Dentz } 3123d0b13706SLuiz Augusto von Dentz 3124d0b13706SLuiz Augusto von Dentz /* Read Number of Supported IAC */ 3125d0b13706SLuiz Augusto von Dentz static int hci_read_num_supported_iac_sync(struct hci_dev *hdev) 3126d0b13706SLuiz Augusto von Dentz { 3127d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC, 3128d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3129d0b13706SLuiz Augusto von Dentz } 3130d0b13706SLuiz Augusto von Dentz 3131d0b13706SLuiz Augusto von Dentz /* Read Current IAC LAP */ 3132d0b13706SLuiz Augusto von Dentz static int hci_read_current_iac_lap_sync(struct hci_dev *hdev) 3133d0b13706SLuiz Augusto von Dentz { 3134d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP, 3135d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3136d0b13706SLuiz Augusto von Dentz } 3137d0b13706SLuiz Augusto von Dentz 3138d0b13706SLuiz Augusto von Dentz static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type, 3139d0b13706SLuiz Augusto von Dentz u8 cond_type, bdaddr_t *bdaddr, 3140d0b13706SLuiz Augusto von Dentz u8 auto_accept) 3141d0b13706SLuiz Augusto von Dentz { 3142d0b13706SLuiz Augusto von Dentz struct hci_cp_set_event_filter cp; 3143d0b13706SLuiz Augusto von Dentz 3144d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 3145d0b13706SLuiz Augusto von Dentz return 0; 3146d0b13706SLuiz Augusto von Dentz 31470eaecfb2SIsmael Ferreras Morezuelas if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks)) 31480eaecfb2SIsmael Ferreras Morezuelas return 0; 31490eaecfb2SIsmael Ferreras Morezuelas 3150d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 3151d0b13706SLuiz Augusto von Dentz cp.flt_type = flt_type; 3152d0b13706SLuiz Augusto von Dentz 3153d0b13706SLuiz Augusto von Dentz if (flt_type != HCI_FLT_CLEAR_ALL) { 3154d0b13706SLuiz Augusto von Dentz cp.cond_type = cond_type; 3155d0b13706SLuiz Augusto von Dentz bacpy(&cp.addr_conn_flt.bdaddr, bdaddr); 3156d0b13706SLuiz Augusto von Dentz cp.addr_conn_flt.auto_accept = auto_accept; 3157d0b13706SLuiz Augusto von Dentz } 3158d0b13706SLuiz Augusto von Dentz 3159d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT, 3160d0b13706SLuiz Augusto von Dentz flt_type == HCI_FLT_CLEAR_ALL ? 3161d0b13706SLuiz Augusto von Dentz sizeof(cp.flt_type) : sizeof(cp), &cp, 3162d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 3163d0b13706SLuiz Augusto von Dentz } 3164d0b13706SLuiz Augusto von Dentz 3165d0b13706SLuiz Augusto von Dentz static int hci_clear_event_filter_sync(struct hci_dev *hdev) 3166d0b13706SLuiz Augusto von Dentz { 3167d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED)) 3168d0b13706SLuiz Augusto von Dentz return 0; 3169d0b13706SLuiz Augusto von Dentz 31700eaecfb2SIsmael Ferreras Morezuelas /* In theory the state machine should not reach here unless 31710eaecfb2SIsmael Ferreras Morezuelas * a hci_set_event_filter_sync() call succeeds, but we do 31720eaecfb2SIsmael Ferreras Morezuelas * the check both for parity and as a future reminder. 31730eaecfb2SIsmael Ferreras Morezuelas */ 31740eaecfb2SIsmael Ferreras Morezuelas if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks)) 31750eaecfb2SIsmael Ferreras Morezuelas return 0; 31760eaecfb2SIsmael Ferreras Morezuelas 3177d0b13706SLuiz Augusto von Dentz return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00, 3178d0b13706SLuiz Augusto von Dentz BDADDR_ANY, 0x00); 3179d0b13706SLuiz Augusto von Dentz } 3180d0b13706SLuiz Augusto von Dentz 3181d0b13706SLuiz Augusto von Dentz /* Connection accept timeout ~20 secs */ 3182d0b13706SLuiz Augusto von Dentz static int hci_write_ca_timeout_sync(struct hci_dev *hdev) 3183d0b13706SLuiz Augusto von Dentz { 3184d0b13706SLuiz Augusto von Dentz __le16 param = cpu_to_le16(0x7d00); 3185d0b13706SLuiz Augusto von Dentz 3186d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT, 3187d0b13706SLuiz Augusto von Dentz sizeof(param), ¶m, HCI_CMD_TIMEOUT); 3188d0b13706SLuiz Augusto von Dentz } 3189d0b13706SLuiz Augusto von Dentz 3190d0b13706SLuiz Augusto von Dentz /* BR Controller init stage 2 command sequence */ 3191d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage br_init2[] = { 3192d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_BUFFER_SIZE */ 3193d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_buffer_size_sync), 3194d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_CLASS_OF_DEV */ 3195d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_dev_class_sync), 3196d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_NAME */ 3197d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_name_sync), 3198d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_VOICE_SETTING */ 3199d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_voice_setting_sync), 3200d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_NUM_SUPPORTED_IAC */ 3201d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_num_supported_iac_sync), 3202d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_CURRENT_IAC_LAP */ 3203d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_current_iac_lap_sync), 3204d0b13706SLuiz Augusto von Dentz /* HCI_OP_SET_EVENT_FLT */ 3205d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_clear_event_filter_sync), 3206d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_CA_TIMEOUT */ 3207d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_ca_timeout_sync), 3208d0b13706SLuiz Augusto von Dentz {} 3209d0b13706SLuiz Augusto von Dentz }; 3210d0b13706SLuiz Augusto von Dentz 3211d0b13706SLuiz Augusto von Dentz static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev) 3212d0b13706SLuiz Augusto von Dentz { 3213d0b13706SLuiz Augusto von Dentz u8 mode = 0x01; 3214d0b13706SLuiz Augusto von Dentz 3215d0b13706SLuiz Augusto von Dentz if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) 3216d0b13706SLuiz Augusto von Dentz return 0; 3217d0b13706SLuiz Augusto von Dentz 3218d0b13706SLuiz Augusto von Dentz /* When SSP is available, then the host features page 3219d0b13706SLuiz Augusto von Dentz * should also be available as well. However some 3220d0b13706SLuiz Augusto von Dentz * controllers list the max_page as 0 as long as SSP 3221d0b13706SLuiz Augusto von Dentz * has not been enabled. To achieve proper debugging 3222d0b13706SLuiz Augusto von Dentz * output, force the minimum max_page to 1 at least. 3223d0b13706SLuiz Augusto von Dentz */ 3224d0b13706SLuiz Augusto von Dentz hdev->max_page = 0x01; 3225d0b13706SLuiz Augusto von Dentz 3226d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE, 3227d0b13706SLuiz Augusto von Dentz sizeof(mode), &mode, HCI_CMD_TIMEOUT); 3228d0b13706SLuiz Augusto von Dentz } 3229d0b13706SLuiz Augusto von Dentz 3230d0b13706SLuiz Augusto von Dentz static int hci_write_eir_sync(struct hci_dev *hdev) 3231d0b13706SLuiz Augusto von Dentz { 3232d0b13706SLuiz Augusto von Dentz struct hci_cp_write_eir cp; 3233d0b13706SLuiz Augusto von Dentz 3234d0b13706SLuiz Augusto von Dentz if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) 3235d0b13706SLuiz Augusto von Dentz return 0; 3236d0b13706SLuiz Augusto von Dentz 3237d0b13706SLuiz Augusto von Dentz memset(hdev->eir, 0, sizeof(hdev->eir)); 3238d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 3239d0b13706SLuiz Augusto von Dentz 3240d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp, 3241d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 3242d0b13706SLuiz Augusto von Dentz } 3243d0b13706SLuiz Augusto von Dentz 3244d0b13706SLuiz Augusto von Dentz static int hci_write_inquiry_mode_sync(struct hci_dev *hdev) 3245d0b13706SLuiz Augusto von Dentz { 3246d0b13706SLuiz Augusto von Dentz u8 mode; 3247d0b13706SLuiz Augusto von Dentz 3248d0b13706SLuiz Augusto von Dentz if (!lmp_inq_rssi_capable(hdev) && 3249d0b13706SLuiz Augusto von Dentz !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) 3250d0b13706SLuiz Augusto von Dentz return 0; 3251d0b13706SLuiz Augusto von Dentz 3252d0b13706SLuiz Augusto von Dentz /* If Extended Inquiry Result events are supported, then 3253d0b13706SLuiz Augusto von Dentz * they are clearly preferred over Inquiry Result with RSSI 3254d0b13706SLuiz Augusto von Dentz * events. 3255d0b13706SLuiz Augusto von Dentz */ 3256d0b13706SLuiz Augusto von Dentz mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01; 3257d0b13706SLuiz Augusto von Dentz 3258d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE, 3259d0b13706SLuiz Augusto von Dentz sizeof(mode), &mode, HCI_CMD_TIMEOUT); 3260d0b13706SLuiz Augusto von Dentz } 3261d0b13706SLuiz Augusto von Dentz 3262d0b13706SLuiz Augusto von Dentz static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev) 3263d0b13706SLuiz Augusto von Dentz { 3264d0b13706SLuiz Augusto von Dentz if (!lmp_inq_tx_pwr_capable(hdev)) 3265d0b13706SLuiz Augusto von Dentz return 0; 3266d0b13706SLuiz Augusto von Dentz 3267d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, 3268d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3269d0b13706SLuiz Augusto von Dentz } 3270d0b13706SLuiz Augusto von Dentz 3271d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page) 3272d0b13706SLuiz Augusto von Dentz { 3273d0b13706SLuiz Augusto von Dentz struct hci_cp_read_local_ext_features cp; 3274d0b13706SLuiz Augusto von Dentz 3275d0b13706SLuiz Augusto von Dentz if (!lmp_ext_feat_capable(hdev)) 3276d0b13706SLuiz Augusto von Dentz return 0; 3277d0b13706SLuiz Augusto von Dentz 3278d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 3279d0b13706SLuiz Augusto von Dentz cp.page = page; 3280d0b13706SLuiz Augusto von Dentz 3281d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES, 3282d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 3283d0b13706SLuiz Augusto von Dentz } 3284d0b13706SLuiz Augusto von Dentz 3285d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev) 3286d0b13706SLuiz Augusto von Dentz { 3287d0b13706SLuiz Augusto von Dentz return hci_read_local_ext_features_sync(hdev, 0x01); 3288d0b13706SLuiz Augusto von Dentz } 3289d0b13706SLuiz Augusto von Dentz 3290d0b13706SLuiz Augusto von Dentz /* HCI Controller init stage 2 command sequence */ 3291d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init2[] = { 3292d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_COMMANDS */ 3293d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_cmds_sync), 3294d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_SSP_MODE */ 3295d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_ssp_mode_1_sync), 3296d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_EIR */ 3297d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_eir_sync), 3298d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_INQUIRY_MODE */ 3299d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_inquiry_mode_sync), 3300d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_INQ_RSP_TX_POWER */ 3301d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_inq_rsp_tx_power_sync), 3302d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_EXT_FEATURES */ 3303d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_ext_features_1_sync), 3304d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_AUTH_ENABLE */ 3305d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_auth_enable_sync), 3306d0b13706SLuiz Augusto von Dentz {} 3307d0b13706SLuiz Augusto von Dentz }; 3308d0b13706SLuiz Augusto von Dentz 3309d0b13706SLuiz Augusto von Dentz /* Read LE Buffer Size */ 3310d0b13706SLuiz Augusto von Dentz static int hci_le_read_buffer_size_sync(struct hci_dev *hdev) 3311d0b13706SLuiz Augusto von Dentz { 3312d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 3313d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3314d0b13706SLuiz Augusto von Dentz } 3315d0b13706SLuiz Augusto von Dentz 3316d0b13706SLuiz Augusto von Dentz /* Read LE Local Supported Features */ 3317d0b13706SLuiz Augusto von Dentz static int hci_le_read_local_features_sync(struct hci_dev *hdev) 3318d0b13706SLuiz Augusto von Dentz { 3319d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES, 3320d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3321d0b13706SLuiz Augusto von Dentz } 3322d0b13706SLuiz Augusto von Dentz 3323d0b13706SLuiz Augusto von Dentz /* Read LE Supported States */ 3324d0b13706SLuiz Augusto von Dentz static int hci_le_read_supported_states_sync(struct hci_dev *hdev) 3325d0b13706SLuiz Augusto von Dentz { 3326d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES, 3327d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3328d0b13706SLuiz Augusto von Dentz } 3329d0b13706SLuiz Augusto von Dentz 3330d0b13706SLuiz Augusto von Dentz /* LE Controller init stage 2 command sequence */ 3331d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init2[] = { 3332d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_BUFFER_SIZE */ 3333d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_buffer_size_sync), 3334d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_LOCAL_FEATURES */ 3335d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_local_features_sync), 3336d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_SUPPORTED_STATES */ 3337d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_supported_states_sync), 3338d0b13706SLuiz Augusto von Dentz {} 3339d0b13706SLuiz Augusto von Dentz }; 3340d0b13706SLuiz Augusto von Dentz 3341d0b13706SLuiz Augusto von Dentz static int hci_init2_sync(struct hci_dev *hdev) 3342d0b13706SLuiz Augusto von Dentz { 3343d0b13706SLuiz Augusto von Dentz int err; 3344d0b13706SLuiz Augusto von Dentz 3345d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 3346d0b13706SLuiz Augusto von Dentz 3347d0b13706SLuiz Augusto von Dentz if (hdev->dev_type == HCI_AMP) 3348d0b13706SLuiz Augusto von Dentz return hci_init_stage_sync(hdev, amp_init2); 3349d0b13706SLuiz Augusto von Dentz 335026afbd82SLuiz Augusto von Dentz err = hci_init_stage_sync(hdev, hci_init2); 335126afbd82SLuiz Augusto von Dentz if (err) 335226afbd82SLuiz Augusto von Dentz return err; 335326afbd82SLuiz Augusto von Dentz 3354d0b13706SLuiz Augusto von Dentz if (lmp_bredr_capable(hdev)) { 3355d0b13706SLuiz Augusto von Dentz err = hci_init_stage_sync(hdev, br_init2); 3356d0b13706SLuiz Augusto von Dentz if (err) 3357d0b13706SLuiz Augusto von Dentz return err; 3358d0b13706SLuiz Augusto von Dentz } else { 3359d0b13706SLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); 3360d0b13706SLuiz Augusto von Dentz } 3361d0b13706SLuiz Augusto von Dentz 3362d0b13706SLuiz Augusto von Dentz if (lmp_le_capable(hdev)) { 3363d0b13706SLuiz Augusto von Dentz err = hci_init_stage_sync(hdev, le_init2); 3364d0b13706SLuiz Augusto von Dentz if (err) 3365d0b13706SLuiz Augusto von Dentz return err; 3366d0b13706SLuiz Augusto von Dentz /* LE-only controllers have LE implicitly enabled */ 3367d0b13706SLuiz Augusto von Dentz if (!lmp_bredr_capable(hdev)) 3368d0b13706SLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_LE_ENABLED); 3369d0b13706SLuiz Augusto von Dentz } 3370d0b13706SLuiz Augusto von Dentz 337126afbd82SLuiz Augusto von Dentz return 0; 3372d0b13706SLuiz Augusto von Dentz } 3373d0b13706SLuiz Augusto von Dentz 3374d0b13706SLuiz Augusto von Dentz static int hci_set_event_mask_sync(struct hci_dev *hdev) 3375d0b13706SLuiz Augusto von Dentz { 3376d0b13706SLuiz Augusto von Dentz /* The second byte is 0xff instead of 0x9f (two reserved bits 3377d0b13706SLuiz Augusto von Dentz * disabled) since a Broadcom 1.2 dongle doesn't respond to the 3378d0b13706SLuiz Augusto von Dentz * command otherwise. 3379d0b13706SLuiz Augusto von Dentz */ 3380d0b13706SLuiz Augusto von Dentz u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; 3381d0b13706SLuiz Augusto von Dentz 3382d0b13706SLuiz Augusto von Dentz /* CSR 1.1 dongles does not accept any bitfield so don't try to set 3383d0b13706SLuiz Augusto von Dentz * any event mask for pre 1.2 devices. 3384d0b13706SLuiz Augusto von Dentz */ 3385d0b13706SLuiz Augusto von Dentz if (hdev->hci_ver < BLUETOOTH_VER_1_2) 3386d0b13706SLuiz Augusto von Dentz return 0; 3387d0b13706SLuiz Augusto von Dentz 3388d0b13706SLuiz Augusto von Dentz if (lmp_bredr_capable(hdev)) { 3389d0b13706SLuiz Augusto von Dentz events[4] |= 0x01; /* Flow Specification Complete */ 3390182ee45dSLuiz Augusto von Dentz 3391182ee45dSLuiz Augusto von Dentz /* Don't set Disconnect Complete when suspended as that 3392182ee45dSLuiz Augusto von Dentz * would wakeup the host when disconnecting due to 3393182ee45dSLuiz Augusto von Dentz * suspend. 3394182ee45dSLuiz Augusto von Dentz */ 3395182ee45dSLuiz Augusto von Dentz if (hdev->suspended) 3396182ee45dSLuiz Augusto von Dentz events[0] &= 0xef; 3397d0b13706SLuiz Augusto von Dentz } else { 3398d0b13706SLuiz Augusto von Dentz /* Use a different default for LE-only devices */ 3399d0b13706SLuiz Augusto von Dentz memset(events, 0, sizeof(events)); 3400d0b13706SLuiz Augusto von Dentz events[1] |= 0x20; /* Command Complete */ 3401d0b13706SLuiz Augusto von Dentz events[1] |= 0x40; /* Command Status */ 3402d0b13706SLuiz Augusto von Dentz events[1] |= 0x80; /* Hardware Error */ 3403d0b13706SLuiz Augusto von Dentz 3404d0b13706SLuiz Augusto von Dentz /* If the controller supports the Disconnect command, enable 3405d0b13706SLuiz Augusto von Dentz * the corresponding event. In addition enable packet flow 3406d0b13706SLuiz Augusto von Dentz * control related events. 3407d0b13706SLuiz Augusto von Dentz */ 3408d0b13706SLuiz Augusto von Dentz if (hdev->commands[0] & 0x20) { 3409182ee45dSLuiz Augusto von Dentz /* Don't set Disconnect Complete when suspended as that 3410182ee45dSLuiz Augusto von Dentz * would wakeup the host when disconnecting due to 3411182ee45dSLuiz Augusto von Dentz * suspend. 3412182ee45dSLuiz Augusto von Dentz */ 3413182ee45dSLuiz Augusto von Dentz if (!hdev->suspended) 3414d0b13706SLuiz Augusto von Dentz events[0] |= 0x10; /* Disconnection Complete */ 3415d0b13706SLuiz Augusto von Dentz events[2] |= 0x04; /* Number of Completed Packets */ 3416d0b13706SLuiz Augusto von Dentz events[3] |= 0x02; /* Data Buffer Overflow */ 3417d0b13706SLuiz Augusto von Dentz } 3418d0b13706SLuiz Augusto von Dentz 3419d0b13706SLuiz Augusto von Dentz /* If the controller supports the Read Remote Version 3420d0b13706SLuiz Augusto von Dentz * Information command, enable the corresponding event. 3421d0b13706SLuiz Augusto von Dentz */ 3422d0b13706SLuiz Augusto von Dentz if (hdev->commands[2] & 0x80) 3423d0b13706SLuiz Augusto von Dentz events[1] |= 0x08; /* Read Remote Version Information 3424d0b13706SLuiz Augusto von Dentz * Complete 3425d0b13706SLuiz Augusto von Dentz */ 3426d0b13706SLuiz Augusto von Dentz 3427d0b13706SLuiz Augusto von Dentz if (hdev->le_features[0] & HCI_LE_ENCRYPTION) { 3428d0b13706SLuiz Augusto von Dentz events[0] |= 0x80; /* Encryption Change */ 3429d0b13706SLuiz Augusto von Dentz events[5] |= 0x80; /* Encryption Key Refresh Complete */ 3430d0b13706SLuiz Augusto von Dentz } 3431d0b13706SLuiz Augusto von Dentz } 3432d0b13706SLuiz Augusto von Dentz 3433d0b13706SLuiz Augusto von Dentz if (lmp_inq_rssi_capable(hdev) || 3434d0b13706SLuiz Augusto von Dentz test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) 3435d0b13706SLuiz Augusto von Dentz events[4] |= 0x02; /* Inquiry Result with RSSI */ 3436d0b13706SLuiz Augusto von Dentz 3437d0b13706SLuiz Augusto von Dentz if (lmp_ext_feat_capable(hdev)) 3438d0b13706SLuiz Augusto von Dentz events[4] |= 0x04; /* Read Remote Extended Features Complete */ 3439d0b13706SLuiz Augusto von Dentz 3440d0b13706SLuiz Augusto von Dentz if (lmp_esco_capable(hdev)) { 3441d0b13706SLuiz Augusto von Dentz events[5] |= 0x08; /* Synchronous Connection Complete */ 3442d0b13706SLuiz Augusto von Dentz events[5] |= 0x10; /* Synchronous Connection Changed */ 3443d0b13706SLuiz Augusto von Dentz } 3444d0b13706SLuiz Augusto von Dentz 3445d0b13706SLuiz Augusto von Dentz if (lmp_sniffsubr_capable(hdev)) 3446d0b13706SLuiz Augusto von Dentz events[5] |= 0x20; /* Sniff Subrating */ 3447d0b13706SLuiz Augusto von Dentz 3448d0b13706SLuiz Augusto von Dentz if (lmp_pause_enc_capable(hdev)) 3449d0b13706SLuiz Augusto von Dentz events[5] |= 0x80; /* Encryption Key Refresh Complete */ 3450d0b13706SLuiz Augusto von Dentz 3451d0b13706SLuiz Augusto von Dentz if (lmp_ext_inq_capable(hdev)) 3452d0b13706SLuiz Augusto von Dentz events[5] |= 0x40; /* Extended Inquiry Result */ 3453d0b13706SLuiz Augusto von Dentz 3454d0b13706SLuiz Augusto von Dentz if (lmp_no_flush_capable(hdev)) 3455d0b13706SLuiz Augusto von Dentz events[7] |= 0x01; /* Enhanced Flush Complete */ 3456d0b13706SLuiz Augusto von Dentz 3457d0b13706SLuiz Augusto von Dentz if (lmp_lsto_capable(hdev)) 3458d0b13706SLuiz Augusto von Dentz events[6] |= 0x80; /* Link Supervision Timeout Changed */ 3459d0b13706SLuiz Augusto von Dentz 3460d0b13706SLuiz Augusto von Dentz if (lmp_ssp_capable(hdev)) { 3461d0b13706SLuiz Augusto von Dentz events[6] |= 0x01; /* IO Capability Request */ 3462d0b13706SLuiz Augusto von Dentz events[6] |= 0x02; /* IO Capability Response */ 3463d0b13706SLuiz Augusto von Dentz events[6] |= 0x04; /* User Confirmation Request */ 3464d0b13706SLuiz Augusto von Dentz events[6] |= 0x08; /* User Passkey Request */ 3465d0b13706SLuiz Augusto von Dentz events[6] |= 0x10; /* Remote OOB Data Request */ 3466d0b13706SLuiz Augusto von Dentz events[6] |= 0x20; /* Simple Pairing Complete */ 3467d0b13706SLuiz Augusto von Dentz events[7] |= 0x04; /* User Passkey Notification */ 3468d0b13706SLuiz Augusto von Dentz events[7] |= 0x08; /* Keypress Notification */ 3469d0b13706SLuiz Augusto von Dentz events[7] |= 0x10; /* Remote Host Supported 3470d0b13706SLuiz Augusto von Dentz * Features Notification 3471d0b13706SLuiz Augusto von Dentz */ 3472d0b13706SLuiz Augusto von Dentz } 3473d0b13706SLuiz Augusto von Dentz 3474d0b13706SLuiz Augusto von Dentz if (lmp_le_capable(hdev)) 3475d0b13706SLuiz Augusto von Dentz events[7] |= 0x20; /* LE Meta-Event */ 3476d0b13706SLuiz Augusto von Dentz 3477d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK, 3478d0b13706SLuiz Augusto von Dentz sizeof(events), events, HCI_CMD_TIMEOUT); 3479d0b13706SLuiz Augusto von Dentz } 3480d0b13706SLuiz Augusto von Dentz 3481d0b13706SLuiz Augusto von Dentz static int hci_read_stored_link_key_sync(struct hci_dev *hdev) 3482d0b13706SLuiz Augusto von Dentz { 3483d0b13706SLuiz Augusto von Dentz struct hci_cp_read_stored_link_key cp; 3484d0b13706SLuiz Augusto von Dentz 3485d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[6] & 0x20) || 3486d0b13706SLuiz Augusto von Dentz test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) 3487d0b13706SLuiz Augusto von Dentz return 0; 3488d0b13706SLuiz Augusto von Dentz 3489d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 3490d0b13706SLuiz Augusto von Dentz bacpy(&cp.bdaddr, BDADDR_ANY); 3491d0b13706SLuiz Augusto von Dentz cp.read_all = 0x01; 3492d0b13706SLuiz Augusto von Dentz 3493d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY, 3494d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 3495d0b13706SLuiz Augusto von Dentz } 3496d0b13706SLuiz Augusto von Dentz 3497d0b13706SLuiz Augusto von Dentz static int hci_setup_link_policy_sync(struct hci_dev *hdev) 3498d0b13706SLuiz Augusto von Dentz { 3499d0b13706SLuiz Augusto von Dentz struct hci_cp_write_def_link_policy cp; 3500d0b13706SLuiz Augusto von Dentz u16 link_policy = 0; 3501d0b13706SLuiz Augusto von Dentz 3502d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[5] & 0x10)) 3503d0b13706SLuiz Augusto von Dentz return 0; 3504d0b13706SLuiz Augusto von Dentz 3505d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 3506d0b13706SLuiz Augusto von Dentz 3507d0b13706SLuiz Augusto von Dentz if (lmp_rswitch_capable(hdev)) 3508d0b13706SLuiz Augusto von Dentz link_policy |= HCI_LP_RSWITCH; 3509d0b13706SLuiz Augusto von Dentz if (lmp_hold_capable(hdev)) 3510d0b13706SLuiz Augusto von Dentz link_policy |= HCI_LP_HOLD; 3511d0b13706SLuiz Augusto von Dentz if (lmp_sniff_capable(hdev)) 3512d0b13706SLuiz Augusto von Dentz link_policy |= HCI_LP_SNIFF; 3513d0b13706SLuiz Augusto von Dentz if (lmp_park_capable(hdev)) 3514d0b13706SLuiz Augusto von Dentz link_policy |= HCI_LP_PARK; 3515d0b13706SLuiz Augusto von Dentz 3516d0b13706SLuiz Augusto von Dentz cp.policy = cpu_to_le16(link_policy); 3517d0b13706SLuiz Augusto von Dentz 3518d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 3519d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 3520d0b13706SLuiz Augusto von Dentz } 3521d0b13706SLuiz Augusto von Dentz 3522d0b13706SLuiz Augusto von Dentz static int hci_read_page_scan_activity_sync(struct hci_dev *hdev) 3523d0b13706SLuiz Augusto von Dentz { 3524d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[8] & 0x01)) 3525d0b13706SLuiz Augusto von Dentz return 0; 3526d0b13706SLuiz Augusto von Dentz 3527d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 3528d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3529d0b13706SLuiz Augusto von Dentz } 3530d0b13706SLuiz Augusto von Dentz 3531d0b13706SLuiz Augusto von Dentz static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev) 3532d0b13706SLuiz Augusto von Dentz { 3533d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[18] & 0x04) || 3534766ae242SZijun Hu !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING)) 3535d0b13706SLuiz Augusto von Dentz return 0; 3536d0b13706SLuiz Augusto von Dentz 3537d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING, 3538d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3539d0b13706SLuiz Augusto von Dentz } 3540d0b13706SLuiz Augusto von Dentz 3541d0b13706SLuiz Augusto von Dentz static int hci_read_page_scan_type_sync(struct hci_dev *hdev) 3542d0b13706SLuiz Augusto von Dentz { 3543d0b13706SLuiz Augusto von Dentz /* Some older Broadcom based Bluetooth 1.2 controllers do not 3544d0b13706SLuiz Augusto von Dentz * support the Read Page Scan Type command. Check support for 3545d0b13706SLuiz Augusto von Dentz * this command in the bit mask of supported commands. 3546d0b13706SLuiz Augusto von Dentz */ 3547d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[13] & 0x01)) 3548d0b13706SLuiz Augusto von Dentz return 0; 3549d0b13706SLuiz Augusto von Dentz 3550d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE, 3551d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3552d0b13706SLuiz Augusto von Dentz } 3553d0b13706SLuiz Augusto von Dentz 3554d0b13706SLuiz Augusto von Dentz /* Read features beyond page 1 if available */ 3555d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev) 3556d0b13706SLuiz Augusto von Dentz { 3557d0b13706SLuiz Augusto von Dentz u8 page; 3558d0b13706SLuiz Augusto von Dentz int err; 3559d0b13706SLuiz Augusto von Dentz 3560d0b13706SLuiz Augusto von Dentz if (!lmp_ext_feat_capable(hdev)) 3561d0b13706SLuiz Augusto von Dentz return 0; 3562d0b13706SLuiz Augusto von Dentz 3563d0b13706SLuiz Augusto von Dentz for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page; 3564d0b13706SLuiz Augusto von Dentz page++) { 3565d0b13706SLuiz Augusto von Dentz err = hci_read_local_ext_features_sync(hdev, page); 3566d0b13706SLuiz Augusto von Dentz if (err) 3567d0b13706SLuiz Augusto von Dentz return err; 3568d0b13706SLuiz Augusto von Dentz } 3569d0b13706SLuiz Augusto von Dentz 3570d0b13706SLuiz Augusto von Dentz return 0; 3571d0b13706SLuiz Augusto von Dentz } 3572d0b13706SLuiz Augusto von Dentz 3573d0b13706SLuiz Augusto von Dentz /* HCI Controller init stage 3 command sequence */ 3574d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init3[] = { 3575d0b13706SLuiz Augusto von Dentz /* HCI_OP_SET_EVENT_MASK */ 3576d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_set_event_mask_sync), 3577d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_STORED_LINK_KEY */ 3578d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_stored_link_key_sync), 3579d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_DEF_LINK_POLICY */ 3580d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_setup_link_policy_sync), 3581d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */ 3582d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_page_scan_activity_sync), 3583d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */ 3584d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_def_err_data_reporting_sync), 3585d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_PAGE_SCAN_TYPE */ 3586d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_page_scan_type_sync), 3587d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_EXT_FEATURES */ 3588d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_ext_features_all_sync), 3589d0b13706SLuiz Augusto von Dentz {} 3590d0b13706SLuiz Augusto von Dentz }; 3591d0b13706SLuiz Augusto von Dentz 3592d0b13706SLuiz Augusto von Dentz static int hci_le_set_event_mask_sync(struct hci_dev *hdev) 3593d0b13706SLuiz Augusto von Dentz { 3594d0b13706SLuiz Augusto von Dentz u8 events[8]; 3595d0b13706SLuiz Augusto von Dentz 3596d0b13706SLuiz Augusto von Dentz if (!lmp_le_capable(hdev)) 3597d0b13706SLuiz Augusto von Dentz return 0; 3598d0b13706SLuiz Augusto von Dentz 3599d0b13706SLuiz Augusto von Dentz memset(events, 0, sizeof(events)); 3600d0b13706SLuiz Augusto von Dentz 3601d0b13706SLuiz Augusto von Dentz if (hdev->le_features[0] & HCI_LE_ENCRYPTION) 3602d0b13706SLuiz Augusto von Dentz events[0] |= 0x10; /* LE Long Term Key Request */ 3603d0b13706SLuiz Augusto von Dentz 3604d0b13706SLuiz Augusto von Dentz /* If controller supports the Connection Parameters Request 3605d0b13706SLuiz Augusto von Dentz * Link Layer Procedure, enable the corresponding event. 3606d0b13706SLuiz Augusto von Dentz */ 3607d0b13706SLuiz Augusto von Dentz if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC) 3608d0b13706SLuiz Augusto von Dentz /* LE Remote Connection Parameter Request */ 3609d0b13706SLuiz Augusto von Dentz events[0] |= 0x20; 3610d0b13706SLuiz Augusto von Dentz 3611d0b13706SLuiz Augusto von Dentz /* If the controller supports the Data Length Extension 3612d0b13706SLuiz Augusto von Dentz * feature, enable the corresponding event. 3613d0b13706SLuiz Augusto von Dentz */ 3614d0b13706SLuiz Augusto von Dentz if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) 3615d0b13706SLuiz Augusto von Dentz events[0] |= 0x40; /* LE Data Length Change */ 3616d0b13706SLuiz Augusto von Dentz 3617a56a1138SLuiz Augusto von Dentz /* If the controller supports LL Privacy feature or LE Extended Adv, 3618a56a1138SLuiz Augusto von Dentz * enable the corresponding event. 3619d0b13706SLuiz Augusto von Dentz */ 3620a56a1138SLuiz Augusto von Dentz if (use_enhanced_conn_complete(hdev)) 3621d0b13706SLuiz Augusto von Dentz events[1] |= 0x02; /* LE Enhanced Connection Complete */ 3622d0b13706SLuiz Augusto von Dentz 3623d0b13706SLuiz Augusto von Dentz /* If the controller supports Extended Scanner Filter 3624d0b13706SLuiz Augusto von Dentz * Policies, enable the corresponding event. 3625d0b13706SLuiz Augusto von Dentz */ 3626d0b13706SLuiz Augusto von Dentz if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY) 3627d0b13706SLuiz Augusto von Dentz events[1] |= 0x04; /* LE Direct Advertising Report */ 3628d0b13706SLuiz Augusto von Dentz 3629d0b13706SLuiz Augusto von Dentz /* If the controller supports Channel Selection Algorithm #2 3630d0b13706SLuiz Augusto von Dentz * feature, enable the corresponding event. 3631d0b13706SLuiz Augusto von Dentz */ 3632d0b13706SLuiz Augusto von Dentz if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2) 3633d0b13706SLuiz Augusto von Dentz events[2] |= 0x08; /* LE Channel Selection Algorithm */ 3634d0b13706SLuiz Augusto von Dentz 3635d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Set Scan Enable command, 3636d0b13706SLuiz Augusto von Dentz * enable the corresponding advertising report event. 3637d0b13706SLuiz Augusto von Dentz */ 3638d0b13706SLuiz Augusto von Dentz if (hdev->commands[26] & 0x08) 3639d0b13706SLuiz Augusto von Dentz events[0] |= 0x02; /* LE Advertising Report */ 3640d0b13706SLuiz Augusto von Dentz 3641d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Create Connection 3642d0b13706SLuiz Augusto von Dentz * command, enable the corresponding event. 3643d0b13706SLuiz Augusto von Dentz */ 3644d0b13706SLuiz Augusto von Dentz if (hdev->commands[26] & 0x10) 3645d0b13706SLuiz Augusto von Dentz events[0] |= 0x01; /* LE Connection Complete */ 3646d0b13706SLuiz Augusto von Dentz 3647d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Connection Update 3648d0b13706SLuiz Augusto von Dentz * command, enable the corresponding event. 3649d0b13706SLuiz Augusto von Dentz */ 3650d0b13706SLuiz Augusto von Dentz if (hdev->commands[27] & 0x04) 3651d0b13706SLuiz Augusto von Dentz events[0] |= 0x04; /* LE Connection Update Complete */ 3652d0b13706SLuiz Augusto von Dentz 3653d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Read Remote Used Features 3654d0b13706SLuiz Augusto von Dentz * command, enable the corresponding event. 3655d0b13706SLuiz Augusto von Dentz */ 3656d0b13706SLuiz Augusto von Dentz if (hdev->commands[27] & 0x20) 3657d0b13706SLuiz Augusto von Dentz /* LE Read Remote Used Features Complete */ 3658d0b13706SLuiz Augusto von Dentz events[0] |= 0x08; 3659d0b13706SLuiz Augusto von Dentz 3660d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Read Local P-256 3661d0b13706SLuiz Augusto von Dentz * Public Key command, enable the corresponding event. 3662d0b13706SLuiz Augusto von Dentz */ 3663d0b13706SLuiz Augusto von Dentz if (hdev->commands[34] & 0x02) 3664d0b13706SLuiz Augusto von Dentz /* LE Read Local P-256 Public Key Complete */ 3665d0b13706SLuiz Augusto von Dentz events[0] |= 0x80; 3666d0b13706SLuiz Augusto von Dentz 3667d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Generate DHKey 3668d0b13706SLuiz Augusto von Dentz * command, enable the corresponding event. 3669d0b13706SLuiz Augusto von Dentz */ 3670d0b13706SLuiz Augusto von Dentz if (hdev->commands[34] & 0x04) 3671d0b13706SLuiz Augusto von Dentz events[1] |= 0x01; /* LE Generate DHKey Complete */ 3672d0b13706SLuiz Augusto von Dentz 3673d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Set Default PHY or 3674d0b13706SLuiz Augusto von Dentz * LE Set PHY commands, enable the corresponding event. 3675d0b13706SLuiz Augusto von Dentz */ 3676d0b13706SLuiz Augusto von Dentz if (hdev->commands[35] & (0x20 | 0x40)) 3677d0b13706SLuiz Augusto von Dentz events[1] |= 0x08; /* LE PHY Update Complete */ 3678d0b13706SLuiz Augusto von Dentz 3679d0b13706SLuiz Augusto von Dentz /* If the controller supports LE Set Extended Scan Parameters 3680d0b13706SLuiz Augusto von Dentz * and LE Set Extended Scan Enable commands, enable the 3681d0b13706SLuiz Augusto von Dentz * corresponding event. 3682d0b13706SLuiz Augusto von Dentz */ 3683d0b13706SLuiz Augusto von Dentz if (use_ext_scan(hdev)) 3684d0b13706SLuiz Augusto von Dentz events[1] |= 0x10; /* LE Extended Advertising Report */ 3685d0b13706SLuiz Augusto von Dentz 3686d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Extended Advertising 3687d0b13706SLuiz Augusto von Dentz * command, enable the corresponding event. 3688d0b13706SLuiz Augusto von Dentz */ 3689d0b13706SLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 3690d0b13706SLuiz Augusto von Dentz events[2] |= 0x02; /* LE Advertising Set Terminated */ 3691d0b13706SLuiz Augusto von Dentz 369226afbd82SLuiz Augusto von Dentz if (cis_capable(hdev)) { 369326afbd82SLuiz Augusto von Dentz events[3] |= 0x01; /* LE CIS Established */ 369426afbd82SLuiz Augusto von Dentz if (cis_peripheral_capable(hdev)) 369526afbd82SLuiz Augusto von Dentz events[3] |= 0x02; /* LE CIS Request */ 369626afbd82SLuiz Augusto von Dentz } 369726afbd82SLuiz Augusto von Dentz 3698eca0ae4aSLuiz Augusto von Dentz if (bis_capable(hdev)) { 3699eca0ae4aSLuiz Augusto von Dentz events[3] |= 0x04; /* LE Create BIG Complete */ 3700eca0ae4aSLuiz Augusto von Dentz events[3] |= 0x08; /* LE Terminate BIG Complete */ 3701eca0ae4aSLuiz Augusto von Dentz events[3] |= 0x10; /* LE BIG Sync Established */ 3702eca0ae4aSLuiz Augusto von Dentz events[3] |= 0x20; /* LE BIG Sync Loss */ 3703eca0ae4aSLuiz Augusto von Dentz } 3704eca0ae4aSLuiz Augusto von Dentz 3705d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK, 3706d0b13706SLuiz Augusto von Dentz sizeof(events), events, HCI_CMD_TIMEOUT); 3707d0b13706SLuiz Augusto von Dentz } 3708d0b13706SLuiz Augusto von Dentz 3709d0b13706SLuiz Augusto von Dentz /* Read LE Advertising Channel TX Power */ 3710d0b13706SLuiz Augusto von Dentz static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev) 3711d0b13706SLuiz Augusto von Dentz { 3712d0b13706SLuiz Augusto von Dentz if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) { 3713d0b13706SLuiz Augusto von Dentz /* HCI TS spec forbids mixing of legacy and extended 3714d0b13706SLuiz Augusto von Dentz * advertising commands wherein READ_ADV_TX_POWER is 3715d0b13706SLuiz Augusto von Dentz * also included. So do not call it if extended adv 3716d0b13706SLuiz Augusto von Dentz * is supported otherwise controller will return 3717d0b13706SLuiz Augusto von Dentz * COMMAND_DISALLOWED for extended commands. 3718d0b13706SLuiz Augusto von Dentz */ 3719d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, 3720d0b13706SLuiz Augusto von Dentz HCI_OP_LE_READ_ADV_TX_POWER, 3721d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3722d0b13706SLuiz Augusto von Dentz } 3723d0b13706SLuiz Augusto von Dentz 3724d0b13706SLuiz Augusto von Dentz return 0; 3725d0b13706SLuiz Augusto von Dentz } 3726d0b13706SLuiz Augusto von Dentz 3727d0b13706SLuiz Augusto von Dentz /* Read LE Min/Max Tx Power*/ 3728d0b13706SLuiz Augusto von Dentz static int hci_le_read_tx_power_sync(struct hci_dev *hdev) 3729d0b13706SLuiz Augusto von Dentz { 3730d2f8114fSAditya Garg if (!(hdev->commands[38] & 0x80) || 3731d2f8114fSAditya Garg test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks)) 3732d0b13706SLuiz Augusto von Dentz return 0; 3733d0b13706SLuiz Augusto von Dentz 3734d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER, 3735d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3736d0b13706SLuiz Augusto von Dentz } 3737d0b13706SLuiz Augusto von Dentz 3738d0b13706SLuiz Augusto von Dentz /* Read LE Accept List Size */ 3739d0b13706SLuiz Augusto von Dentz static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev) 3740d0b13706SLuiz Augusto von Dentz { 3741d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[26] & 0x40)) 3742d0b13706SLuiz Augusto von Dentz return 0; 3743d0b13706SLuiz Augusto von Dentz 3744d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE, 3745d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3746d0b13706SLuiz Augusto von Dentz } 3747d0b13706SLuiz Augusto von Dentz 3748d0b13706SLuiz Augusto von Dentz /* Clear LE Accept List */ 3749d0b13706SLuiz Augusto von Dentz static int hci_le_clear_accept_list_sync(struct hci_dev *hdev) 3750d0b13706SLuiz Augusto von Dentz { 3751d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[26] & 0x80)) 3752d0b13706SLuiz Augusto von Dentz return 0; 3753d0b13706SLuiz Augusto von Dentz 3754d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL, 3755d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 3756d0b13706SLuiz Augusto von Dentz } 3757d0b13706SLuiz Augusto von Dentz 3758d0b13706SLuiz Augusto von Dentz /* Read LE Resolving List Size */ 3759d0b13706SLuiz Augusto von Dentz static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev) 3760d0b13706SLuiz Augusto von Dentz { 3761d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[34] & 0x40)) 3762d0b13706SLuiz Augusto von Dentz return 0; 3763d0b13706SLuiz Augusto von Dentz 3764d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE, 3765d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3766d0b13706SLuiz Augusto von Dentz } 3767d0b13706SLuiz Augusto von Dentz 3768d0b13706SLuiz Augusto von Dentz /* Clear LE Resolving List */ 3769d0b13706SLuiz Augusto von Dentz static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev) 3770d0b13706SLuiz Augusto von Dentz { 3771d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[34] & 0x20)) 3772d0b13706SLuiz Augusto von Dentz return 0; 3773d0b13706SLuiz Augusto von Dentz 3774d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL, 3775d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 3776d0b13706SLuiz Augusto von Dentz } 3777d0b13706SLuiz Augusto von Dentz 3778d0b13706SLuiz Augusto von Dentz /* Set RPA timeout */ 3779d0b13706SLuiz Augusto von Dentz static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev) 3780d0b13706SLuiz Augusto von Dentz { 3781d0b13706SLuiz Augusto von Dentz __le16 timeout = cpu_to_le16(hdev->rpa_timeout); 3782d0b13706SLuiz Augusto von Dentz 3783d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[35] & 0x04)) 3784d0b13706SLuiz Augusto von Dentz return 0; 3785d0b13706SLuiz Augusto von Dentz 3786d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT, 3787d0b13706SLuiz Augusto von Dentz sizeof(timeout), &timeout, 3788d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 3789d0b13706SLuiz Augusto von Dentz } 3790d0b13706SLuiz Augusto von Dentz 3791d0b13706SLuiz Augusto von Dentz /* Read LE Maximum Data Length */ 3792d0b13706SLuiz Augusto von Dentz static int hci_le_read_max_data_len_sync(struct hci_dev *hdev) 3793d0b13706SLuiz Augusto von Dentz { 3794d0b13706SLuiz Augusto von Dentz if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)) 3795d0b13706SLuiz Augusto von Dentz return 0; 3796d0b13706SLuiz Augusto von Dentz 3797d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL, 3798d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 3799d0b13706SLuiz Augusto von Dentz } 3800d0b13706SLuiz Augusto von Dentz 3801d0b13706SLuiz Augusto von Dentz /* Read LE Suggested Default Data Length */ 3802d0b13706SLuiz Augusto von Dentz static int hci_le_read_def_data_len_sync(struct hci_dev *hdev) 3803d0b13706SLuiz Augusto von Dentz { 3804d0b13706SLuiz Augusto von Dentz if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)) 3805d0b13706SLuiz Augusto von Dentz return 0; 3806d0b13706SLuiz Augusto von Dentz 3807d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL, 3808d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 3809d0b13706SLuiz Augusto von Dentz } 3810d0b13706SLuiz Augusto von Dentz 3811d0b13706SLuiz Augusto von Dentz /* Read LE Number of Supported Advertising Sets */ 3812d0b13706SLuiz Augusto von Dentz static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev) 3813d0b13706SLuiz Augusto von Dentz { 3814d0b13706SLuiz Augusto von Dentz if (!ext_adv_capable(hdev)) 3815d0b13706SLuiz Augusto von Dentz return 0; 3816d0b13706SLuiz Augusto von Dentz 3817d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, 3818d0b13706SLuiz Augusto von Dentz HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS, 3819d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 3820d0b13706SLuiz Augusto von Dentz } 3821d0b13706SLuiz Augusto von Dentz 3822d0b13706SLuiz Augusto von Dentz /* Write LE Host Supported */ 3823d0b13706SLuiz Augusto von Dentz static int hci_set_le_support_sync(struct hci_dev *hdev) 3824d0b13706SLuiz Augusto von Dentz { 3825d0b13706SLuiz Augusto von Dentz struct hci_cp_write_le_host_supported cp; 3826d0b13706SLuiz Augusto von Dentz 3827d0b13706SLuiz Augusto von Dentz /* LE-only devices do not support explicit enablement */ 3828d0b13706SLuiz Augusto von Dentz if (!lmp_bredr_capable(hdev)) 3829d0b13706SLuiz Augusto von Dentz return 0; 3830d0b13706SLuiz Augusto von Dentz 3831d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 3832d0b13706SLuiz Augusto von Dentz 3833d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 3834d0b13706SLuiz Augusto von Dentz cp.le = 0x01; 3835d0b13706SLuiz Augusto von Dentz cp.simul = 0x00; 3836d0b13706SLuiz Augusto von Dentz } 3837d0b13706SLuiz Augusto von Dentz 3838d0b13706SLuiz Augusto von Dentz if (cp.le == lmp_host_le_capable(hdev)) 3839d0b13706SLuiz Augusto von Dentz return 0; 3840d0b13706SLuiz Augusto von Dentz 3841d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, 3842d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 3843d0b13706SLuiz Augusto von Dentz } 3844d0b13706SLuiz Augusto von Dentz 384526afbd82SLuiz Augusto von Dentz /* LE Set Host Feature */ 384626afbd82SLuiz Augusto von Dentz static int hci_le_set_host_feature_sync(struct hci_dev *hdev) 384726afbd82SLuiz Augusto von Dentz { 384826afbd82SLuiz Augusto von Dentz struct hci_cp_le_set_host_feature cp; 384926afbd82SLuiz Augusto von Dentz 385026afbd82SLuiz Augusto von Dentz if (!iso_capable(hdev)) 385126afbd82SLuiz Augusto von Dentz return 0; 385226afbd82SLuiz Augusto von Dentz 385326afbd82SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 385426afbd82SLuiz Augusto von Dentz 385526afbd82SLuiz Augusto von Dentz /* Isochronous Channels (Host Support) */ 385626afbd82SLuiz Augusto von Dentz cp.bit_number = 32; 385726afbd82SLuiz Augusto von Dentz cp.bit_value = 1; 385826afbd82SLuiz Augusto von Dentz 385926afbd82SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE, 386026afbd82SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 386126afbd82SLuiz Augusto von Dentz } 386226afbd82SLuiz Augusto von Dentz 3863d0b13706SLuiz Augusto von Dentz /* LE Controller init stage 3 command sequence */ 3864d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init3[] = { 3865d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_SET_EVENT_MASK */ 3866d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_set_event_mask_sync), 3867d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_ADV_TX_POWER */ 3868d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_adv_tx_power_sync), 3869d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_TRANSMIT_POWER */ 3870d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_tx_power_sync), 3871d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */ 3872d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_accept_list_size_sync), 3873d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_CLEAR_ACCEPT_LIST */ 3874d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_clear_accept_list_sync), 3875d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */ 3876d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_resolv_list_size_sync), 3877d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_CLEAR_RESOLV_LIST */ 3878d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_clear_resolv_list_sync), 3879d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_SET_RPA_TIMEOUT */ 3880d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_set_rpa_timeout_sync), 3881d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_MAX_DATA_LEN */ 3882d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_max_data_len_sync), 3883d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_DEF_DATA_LEN */ 3884d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_def_data_len_sync), 3885d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */ 3886d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_num_support_adv_sets_sync), 3887d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_LE_HOST_SUPPORTED */ 3888d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_set_le_support_sync), 388926afbd82SLuiz Augusto von Dentz /* HCI_OP_LE_SET_HOST_FEATURE */ 389026afbd82SLuiz Augusto von Dentz HCI_INIT(hci_le_set_host_feature_sync), 3891d0b13706SLuiz Augusto von Dentz {} 3892d0b13706SLuiz Augusto von Dentz }; 3893d0b13706SLuiz Augusto von Dentz 3894d0b13706SLuiz Augusto von Dentz static int hci_init3_sync(struct hci_dev *hdev) 3895d0b13706SLuiz Augusto von Dentz { 3896d0b13706SLuiz Augusto von Dentz int err; 3897d0b13706SLuiz Augusto von Dentz 3898d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 3899d0b13706SLuiz Augusto von Dentz 3900d0b13706SLuiz Augusto von Dentz err = hci_init_stage_sync(hdev, hci_init3); 3901d0b13706SLuiz Augusto von Dentz if (err) 3902d0b13706SLuiz Augusto von Dentz return err; 3903d0b13706SLuiz Augusto von Dentz 3904d0b13706SLuiz Augusto von Dentz if (lmp_le_capable(hdev)) 3905d0b13706SLuiz Augusto von Dentz return hci_init_stage_sync(hdev, le_init3); 3906d0b13706SLuiz Augusto von Dentz 3907d0b13706SLuiz Augusto von Dentz return 0; 3908d0b13706SLuiz Augusto von Dentz } 3909d0b13706SLuiz Augusto von Dentz 3910d0b13706SLuiz Augusto von Dentz static int hci_delete_stored_link_key_sync(struct hci_dev *hdev) 3911d0b13706SLuiz Augusto von Dentz { 3912d0b13706SLuiz Augusto von Dentz struct hci_cp_delete_stored_link_key cp; 3913d0b13706SLuiz Augusto von Dentz 3914d0b13706SLuiz Augusto von Dentz /* Some Broadcom based Bluetooth controllers do not support the 3915d0b13706SLuiz Augusto von Dentz * Delete Stored Link Key command. They are clearly indicating its 3916d0b13706SLuiz Augusto von Dentz * absence in the bit mask of supported commands. 3917d0b13706SLuiz Augusto von Dentz * 3918d0b13706SLuiz Augusto von Dentz * Check the supported commands and only if the command is marked 3919d0b13706SLuiz Augusto von Dentz * as supported send it. If not supported assume that the controller 3920d0b13706SLuiz Augusto von Dentz * does not have actual support for stored link keys which makes this 3921d0b13706SLuiz Augusto von Dentz * command redundant anyway. 3922d0b13706SLuiz Augusto von Dentz * 3923d0b13706SLuiz Augusto von Dentz * Some controllers indicate that they support handling deleting 3924d0b13706SLuiz Augusto von Dentz * stored link keys, but they don't. The quirk lets a driver 3925d0b13706SLuiz Augusto von Dentz * just disable this command. 3926d0b13706SLuiz Augusto von Dentz */ 3927d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[6] & 0x80) || 3928d0b13706SLuiz Augusto von Dentz test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) 3929d0b13706SLuiz Augusto von Dentz return 0; 3930d0b13706SLuiz Augusto von Dentz 3931d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 3932d0b13706SLuiz Augusto von Dentz bacpy(&cp.bdaddr, BDADDR_ANY); 3933d0b13706SLuiz Augusto von Dentz cp.delete_all = 0x01; 3934d0b13706SLuiz Augusto von Dentz 3935d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY, 3936d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 3937d0b13706SLuiz Augusto von Dentz } 3938d0b13706SLuiz Augusto von Dentz 3939d0b13706SLuiz Augusto von Dentz static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev) 3940d0b13706SLuiz Augusto von Dentz { 3941d0b13706SLuiz Augusto von Dentz u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 3942d0b13706SLuiz Augusto von Dentz bool changed = false; 3943d0b13706SLuiz Augusto von Dentz 3944d0b13706SLuiz Augusto von Dentz /* Set event mask page 2 if the HCI command for it is supported */ 3945d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[22] & 0x04)) 3946d0b13706SLuiz Augusto von Dentz return 0; 3947d0b13706SLuiz Augusto von Dentz 3948d0b13706SLuiz Augusto von Dentz /* If Connectionless Peripheral Broadcast central role is supported 3949d0b13706SLuiz Augusto von Dentz * enable all necessary events for it. 3950d0b13706SLuiz Augusto von Dentz */ 3951d0b13706SLuiz Augusto von Dentz if (lmp_cpb_central_capable(hdev)) { 3952d0b13706SLuiz Augusto von Dentz events[1] |= 0x40; /* Triggered Clock Capture */ 3953d0b13706SLuiz Augusto von Dentz events[1] |= 0x80; /* Synchronization Train Complete */ 39540feb8af0SZijun Hu events[2] |= 0x08; /* Truncated Page Complete */ 3955d0b13706SLuiz Augusto von Dentz events[2] |= 0x20; /* CPB Channel Map Change */ 3956d0b13706SLuiz Augusto von Dentz changed = true; 3957d0b13706SLuiz Augusto von Dentz } 3958d0b13706SLuiz Augusto von Dentz 3959d0b13706SLuiz Augusto von Dentz /* If Connectionless Peripheral Broadcast peripheral role is supported 3960d0b13706SLuiz Augusto von Dentz * enable all necessary events for it. 3961d0b13706SLuiz Augusto von Dentz */ 3962d0b13706SLuiz Augusto von Dentz if (lmp_cpb_peripheral_capable(hdev)) { 3963d0b13706SLuiz Augusto von Dentz events[2] |= 0x01; /* Synchronization Train Received */ 3964d0b13706SLuiz Augusto von Dentz events[2] |= 0x02; /* CPB Receive */ 3965d0b13706SLuiz Augusto von Dentz events[2] |= 0x04; /* CPB Timeout */ 39660feb8af0SZijun Hu events[2] |= 0x10; /* Peripheral Page Response Timeout */ 3967d0b13706SLuiz Augusto von Dentz changed = true; 3968d0b13706SLuiz Augusto von Dentz } 3969d0b13706SLuiz Augusto von Dentz 3970d0b13706SLuiz Augusto von Dentz /* Enable Authenticated Payload Timeout Expired event if supported */ 3971d0b13706SLuiz Augusto von Dentz if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) { 3972d0b13706SLuiz Augusto von Dentz events[2] |= 0x80; 3973d0b13706SLuiz Augusto von Dentz changed = true; 3974d0b13706SLuiz Augusto von Dentz } 3975d0b13706SLuiz Augusto von Dentz 3976d0b13706SLuiz Augusto von Dentz /* Some Broadcom based controllers indicate support for Set Event 3977d0b13706SLuiz Augusto von Dentz * Mask Page 2 command, but then actually do not support it. Since 3978d0b13706SLuiz Augusto von Dentz * the default value is all bits set to zero, the command is only 3979d0b13706SLuiz Augusto von Dentz * required if the event mask has to be changed. In case no change 3980d0b13706SLuiz Augusto von Dentz * to the event mask is needed, skip this command. 3981d0b13706SLuiz Augusto von Dentz */ 3982d0b13706SLuiz Augusto von Dentz if (!changed) 3983d0b13706SLuiz Augusto von Dentz return 0; 3984d0b13706SLuiz Augusto von Dentz 3985d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2, 3986d0b13706SLuiz Augusto von Dentz sizeof(events), events, HCI_CMD_TIMEOUT); 3987d0b13706SLuiz Augusto von Dentz } 3988d0b13706SLuiz Augusto von Dentz 3989d0b13706SLuiz Augusto von Dentz /* Read local codec list if the HCI command is supported */ 3990d0b13706SLuiz Augusto von Dentz static int hci_read_local_codecs_sync(struct hci_dev *hdev) 3991d0b13706SLuiz Augusto von Dentz { 3992d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[29] & 0x20)) 3993d0b13706SLuiz Augusto von Dentz return 0; 3994d0b13706SLuiz Augusto von Dentz 3995d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_CODECS, 0, NULL, 3996d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 3997d0b13706SLuiz Augusto von Dentz } 3998d0b13706SLuiz Augusto von Dentz 3999d0b13706SLuiz Augusto von Dentz /* Read local pairing options if the HCI command is supported */ 4000d0b13706SLuiz Augusto von Dentz static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev) 4001d0b13706SLuiz Augusto von Dentz { 4002d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[41] & 0x08)) 4003d0b13706SLuiz Augusto von Dentz return 0; 4004d0b13706SLuiz Augusto von Dentz 4005d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS, 4006d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 4007d0b13706SLuiz Augusto von Dentz } 4008d0b13706SLuiz Augusto von Dentz 4009d0b13706SLuiz Augusto von Dentz /* Get MWS transport configuration if the HCI command is supported */ 4010d0b13706SLuiz Augusto von Dentz static int hci_get_mws_transport_config_sync(struct hci_dev *hdev) 4011d0b13706SLuiz Augusto von Dentz { 4012d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[30] & 0x08)) 4013d0b13706SLuiz Augusto von Dentz return 0; 4014d0b13706SLuiz Augusto von Dentz 4015d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG, 4016d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 4017d0b13706SLuiz Augusto von Dentz } 4018d0b13706SLuiz Augusto von Dentz 4019d0b13706SLuiz Augusto von Dentz /* Check for Synchronization Train support */ 4020d0b13706SLuiz Augusto von Dentz static int hci_read_sync_train_params_sync(struct hci_dev *hdev) 4021d0b13706SLuiz Augusto von Dentz { 4022d0b13706SLuiz Augusto von Dentz if (!lmp_sync_train_capable(hdev)) 4023d0b13706SLuiz Augusto von Dentz return 0; 4024d0b13706SLuiz Augusto von Dentz 4025d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS, 4026d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 4027d0b13706SLuiz Augusto von Dentz } 4028d0b13706SLuiz Augusto von Dentz 4029d0b13706SLuiz Augusto von Dentz /* Enable Secure Connections if supported and configured */ 4030d0b13706SLuiz Augusto von Dentz static int hci_write_sc_support_1_sync(struct hci_dev *hdev) 4031d0b13706SLuiz Augusto von Dentz { 4032d0b13706SLuiz Augusto von Dentz u8 support = 0x01; 4033d0b13706SLuiz Augusto von Dentz 4034d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) || 4035d0b13706SLuiz Augusto von Dentz !bredr_sc_enabled(hdev)) 4036d0b13706SLuiz Augusto von Dentz return 0; 4037d0b13706SLuiz Augusto von Dentz 4038d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT, 4039d0b13706SLuiz Augusto von Dentz sizeof(support), &support, 4040d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 4041d0b13706SLuiz Augusto von Dentz } 4042d0b13706SLuiz Augusto von Dentz 4043d0b13706SLuiz Augusto von Dentz /* Set erroneous data reporting if supported to the wideband speech 4044d0b13706SLuiz Augusto von Dentz * setting value 4045d0b13706SLuiz Augusto von Dentz */ 4046d0b13706SLuiz Augusto von Dentz static int hci_set_err_data_report_sync(struct hci_dev *hdev) 4047d0b13706SLuiz Augusto von Dentz { 4048d0b13706SLuiz Augusto von Dentz struct hci_cp_write_def_err_data_reporting cp; 4049d0b13706SLuiz Augusto von Dentz bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED); 4050d0b13706SLuiz Augusto von Dentz 4051d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[18] & 0x08) || 4052766ae242SZijun Hu !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING)) 4053d0b13706SLuiz Augusto von Dentz return 0; 4054d0b13706SLuiz Augusto von Dentz 4055d0b13706SLuiz Augusto von Dentz if (enabled == hdev->err_data_reporting) 4056d0b13706SLuiz Augusto von Dentz return 0; 4057d0b13706SLuiz Augusto von Dentz 4058d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 4059d0b13706SLuiz Augusto von Dentz cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED : 4060d0b13706SLuiz Augusto von Dentz ERR_DATA_REPORTING_DISABLED; 4061d0b13706SLuiz Augusto von Dentz 4062d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING, 4063d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 4064d0b13706SLuiz Augusto von Dentz } 4065d0b13706SLuiz Augusto von Dentz 4066d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init4[] = { 4067d0b13706SLuiz Augusto von Dentz /* HCI_OP_DELETE_STORED_LINK_KEY */ 4068d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_delete_stored_link_key_sync), 4069d0b13706SLuiz Augusto von Dentz /* HCI_OP_SET_EVENT_MASK_PAGE_2 */ 4070d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_set_event_mask_page_2_sync), 4071d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_CODECS */ 4072d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_codecs_sync), 4073d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_PAIRING_OPTS */ 4074d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_pairing_opts_sync), 4075d0b13706SLuiz Augusto von Dentz /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */ 4076d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_get_mws_transport_config_sync), 4077d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_SYNC_TRAIN_PARAMS */ 4078d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_sync_train_params_sync), 4079d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_SC_SUPPORT */ 4080d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_sc_support_1_sync), 4081d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */ 4082d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_set_err_data_report_sync), 4083d0b13706SLuiz Augusto von Dentz {} 4084d0b13706SLuiz Augusto von Dentz }; 4085d0b13706SLuiz Augusto von Dentz 4086d0b13706SLuiz Augusto von Dentz /* Set Suggested Default Data Length to maximum if supported */ 4087d0b13706SLuiz Augusto von Dentz static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev) 4088d0b13706SLuiz Augusto von Dentz { 4089d0b13706SLuiz Augusto von Dentz struct hci_cp_le_write_def_data_len cp; 4090d0b13706SLuiz Augusto von Dentz 4091d0b13706SLuiz Augusto von Dentz if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)) 4092d0b13706SLuiz Augusto von Dentz return 0; 4093d0b13706SLuiz Augusto von Dentz 4094d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 4095d0b13706SLuiz Augusto von Dentz cp.tx_len = cpu_to_le16(hdev->le_max_tx_len); 4096d0b13706SLuiz Augusto von Dentz cp.tx_time = cpu_to_le16(hdev->le_max_tx_time); 4097d0b13706SLuiz Augusto von Dentz 4098d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN, 4099d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 4100d0b13706SLuiz Augusto von Dentz } 4101d0b13706SLuiz Augusto von Dentz 4102d0b13706SLuiz Augusto von Dentz /* Set Default PHY parameters if command is supported */ 4103d0b13706SLuiz Augusto von Dentz static int hci_le_set_default_phy_sync(struct hci_dev *hdev) 4104d0b13706SLuiz Augusto von Dentz { 4105d0b13706SLuiz Augusto von Dentz struct hci_cp_le_set_default_phy cp; 4106d0b13706SLuiz Augusto von Dentz 4107d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[35] & 0x20)) 4108d0b13706SLuiz Augusto von Dentz return 0; 4109d0b13706SLuiz Augusto von Dentz 4110d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 4111d0b13706SLuiz Augusto von Dentz cp.all_phys = 0x00; 4112d0b13706SLuiz Augusto von Dentz cp.tx_phys = hdev->le_tx_def_phys; 4113d0b13706SLuiz Augusto von Dentz cp.rx_phys = hdev->le_rx_def_phys; 4114d0b13706SLuiz Augusto von Dentz 4115d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY, 4116d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 4117d0b13706SLuiz Augusto von Dentz } 4118d0b13706SLuiz Augusto von Dentz 4119d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init4[] = { 4120d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_WRITE_DEF_DATA_LEN */ 4121d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_set_write_def_data_len_sync), 4122d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_SET_DEFAULT_PHY */ 4123d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_set_default_phy_sync), 4124d0b13706SLuiz Augusto von Dentz {} 4125d0b13706SLuiz Augusto von Dentz }; 4126d0b13706SLuiz Augusto von Dentz 4127d0b13706SLuiz Augusto von Dentz static int hci_init4_sync(struct hci_dev *hdev) 4128d0b13706SLuiz Augusto von Dentz { 4129d0b13706SLuiz Augusto von Dentz int err; 4130d0b13706SLuiz Augusto von Dentz 4131d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 4132d0b13706SLuiz Augusto von Dentz 4133d0b13706SLuiz Augusto von Dentz err = hci_init_stage_sync(hdev, hci_init4); 4134d0b13706SLuiz Augusto von Dentz if (err) 4135d0b13706SLuiz Augusto von Dentz return err; 4136d0b13706SLuiz Augusto von Dentz 4137d0b13706SLuiz Augusto von Dentz if (lmp_le_capable(hdev)) 4138d0b13706SLuiz Augusto von Dentz return hci_init_stage_sync(hdev, le_init4); 4139d0b13706SLuiz Augusto von Dentz 4140d0b13706SLuiz Augusto von Dentz return 0; 4141d0b13706SLuiz Augusto von Dentz } 4142d0b13706SLuiz Augusto von Dentz 4143d0b13706SLuiz Augusto von Dentz static int hci_init_sync(struct hci_dev *hdev) 4144d0b13706SLuiz Augusto von Dentz { 4145d0b13706SLuiz Augusto von Dentz int err; 4146d0b13706SLuiz Augusto von Dentz 4147d0b13706SLuiz Augusto von Dentz err = hci_init1_sync(hdev); 4148d0b13706SLuiz Augusto von Dentz if (err < 0) 4149d0b13706SLuiz Augusto von Dentz return err; 4150d0b13706SLuiz Augusto von Dentz 4151d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_SETUP)) 4152d0b13706SLuiz Augusto von Dentz hci_debugfs_create_basic(hdev); 4153d0b13706SLuiz Augusto von Dentz 4154d0b13706SLuiz Augusto von Dentz err = hci_init2_sync(hdev); 4155d0b13706SLuiz Augusto von Dentz if (err < 0) 4156d0b13706SLuiz Augusto von Dentz return err; 4157d0b13706SLuiz Augusto von Dentz 4158d0b13706SLuiz Augusto von Dentz /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode 4159d0b13706SLuiz Augusto von Dentz * BR/EDR/LE type controllers. AMP controllers only need the 4160d0b13706SLuiz Augusto von Dentz * first two stages of init. 4161d0b13706SLuiz Augusto von Dentz */ 4162d0b13706SLuiz Augusto von Dentz if (hdev->dev_type != HCI_PRIMARY) 4163d0b13706SLuiz Augusto von Dentz return 0; 4164d0b13706SLuiz Augusto von Dentz 4165d0b13706SLuiz Augusto von Dentz err = hci_init3_sync(hdev); 4166d0b13706SLuiz Augusto von Dentz if (err < 0) 4167d0b13706SLuiz Augusto von Dentz return err; 4168d0b13706SLuiz Augusto von Dentz 4169d0b13706SLuiz Augusto von Dentz err = hci_init4_sync(hdev); 4170d0b13706SLuiz Augusto von Dentz if (err < 0) 4171d0b13706SLuiz Augusto von Dentz return err; 4172d0b13706SLuiz Augusto von Dentz 4173d0b13706SLuiz Augusto von Dentz /* This function is only called when the controller is actually in 4174d0b13706SLuiz Augusto von Dentz * configured state. When the controller is marked as unconfigured, 4175d0b13706SLuiz Augusto von Dentz * this initialization procedure is not run. 4176d0b13706SLuiz Augusto von Dentz * 4177d0b13706SLuiz Augusto von Dentz * It means that it is possible that a controller runs through its 4178d0b13706SLuiz Augusto von Dentz * setup phase and then discovers missing settings. If that is the 4179d0b13706SLuiz Augusto von Dentz * case, then this function will not be called. It then will only 4180d0b13706SLuiz Augusto von Dentz * be called during the config phase. 4181d0b13706SLuiz Augusto von Dentz * 4182d0b13706SLuiz Augusto von Dentz * So only when in setup phase or config phase, create the debugfs 4183d0b13706SLuiz Augusto von Dentz * entries and register the SMP channels. 4184d0b13706SLuiz Augusto von Dentz */ 4185d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SETUP) && 4186d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_CONFIG)) 4187d0b13706SLuiz Augusto von Dentz return 0; 4188d0b13706SLuiz Augusto von Dentz 4189d0b13706SLuiz Augusto von Dentz hci_debugfs_create_common(hdev); 4190d0b13706SLuiz Augusto von Dentz 4191d0b13706SLuiz Augusto von Dentz if (lmp_bredr_capable(hdev)) 4192d0b13706SLuiz Augusto von Dentz hci_debugfs_create_bredr(hdev); 4193d0b13706SLuiz Augusto von Dentz 4194d0b13706SLuiz Augusto von Dentz if (lmp_le_capable(hdev)) 4195d0b13706SLuiz Augusto von Dentz hci_debugfs_create_le(hdev); 4196d0b13706SLuiz Augusto von Dentz 4197d0b13706SLuiz Augusto von Dentz return 0; 4198d0b13706SLuiz Augusto von Dentz } 4199d0b13706SLuiz Augusto von Dentz 42006b5c1cdaSLuiz Augusto von Dentz #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc } 42016b5c1cdaSLuiz Augusto von Dentz 42026b5c1cdaSLuiz Augusto von Dentz static const struct { 42036b5c1cdaSLuiz Augusto von Dentz unsigned long quirk; 42046b5c1cdaSLuiz Augusto von Dentz const char *desc; 42056b5c1cdaSLuiz Augusto von Dentz } hci_broken_table[] = { 42066b5c1cdaSLuiz Augusto von Dentz HCI_QUIRK_BROKEN(LOCAL_COMMANDS, 42076b5c1cdaSLuiz Augusto von Dentz "HCI Read Local Supported Commands not supported"), 42086b5c1cdaSLuiz Augusto von Dentz HCI_QUIRK_BROKEN(STORED_LINK_KEY, 42096b5c1cdaSLuiz Augusto von Dentz "HCI Delete Stored Link Key command is advertised, " 42106b5c1cdaSLuiz Augusto von Dentz "but not supported."), 42116b5c1cdaSLuiz Augusto von Dentz HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER, 42126b5c1cdaSLuiz Augusto von Dentz "HCI Read Transmit Power Level command is advertised, " 42136b5c1cdaSLuiz Augusto von Dentz "but not supported."), 42146b5c1cdaSLuiz Augusto von Dentz HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL, 42156b5c1cdaSLuiz Augusto von Dentz "HCI Set Event Filter command not supported."), 42166b5c1cdaSLuiz Augusto von Dentz HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN, 42176b5c1cdaSLuiz Augusto von Dentz "HCI Enhanced Setup Synchronous Connection command is " 42186b5c1cdaSLuiz Augusto von Dentz "advertised, but not supported.") 42196b5c1cdaSLuiz Augusto von Dentz }; 42206b5c1cdaSLuiz Augusto von Dentz 42211bbf4023SLuiz Augusto von Dentz /* This function handles hdev setup stage: 42221bbf4023SLuiz Augusto von Dentz * 42231bbf4023SLuiz Augusto von Dentz * Calls hdev->setup 42241bbf4023SLuiz Augusto von Dentz * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set. 42251bbf4023SLuiz Augusto von Dentz */ 42261bbf4023SLuiz Augusto von Dentz static int hci_dev_setup_sync(struct hci_dev *hdev) 4227d0b13706SLuiz Augusto von Dentz { 4228d0b13706SLuiz Augusto von Dentz int ret = 0; 42291bbf4023SLuiz Augusto von Dentz bool invalid_bdaddr; 42301bbf4023SLuiz Augusto von Dentz size_t i; 42311bbf4023SLuiz Augusto von Dentz 42321bbf4023SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SETUP) && 42331bbf4023SLuiz Augusto von Dentz !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) 42341bbf4023SLuiz Augusto von Dentz return 0; 42351bbf4023SLuiz Augusto von Dentz 42361bbf4023SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 42371bbf4023SLuiz Augusto von Dentz 42381bbf4023SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_SETUP); 42391bbf4023SLuiz Augusto von Dentz 42401bbf4023SLuiz Augusto von Dentz if (hdev->setup) 42411bbf4023SLuiz Augusto von Dentz ret = hdev->setup(hdev); 42421bbf4023SLuiz Augusto von Dentz 42431bbf4023SLuiz Augusto von Dentz for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) { 42441bbf4023SLuiz Augusto von Dentz if (test_bit(hci_broken_table[i].quirk, &hdev->quirks)) 42451bbf4023SLuiz Augusto von Dentz bt_dev_warn(hdev, "%s", hci_broken_table[i].desc); 42461bbf4023SLuiz Augusto von Dentz } 42471bbf4023SLuiz Augusto von Dentz 42481bbf4023SLuiz Augusto von Dentz /* The transport driver can set the quirk to mark the 42491bbf4023SLuiz Augusto von Dentz * BD_ADDR invalid before creating the HCI device or in 42501bbf4023SLuiz Augusto von Dentz * its setup callback. 42511bbf4023SLuiz Augusto von Dentz */ 42521bbf4023SLuiz Augusto von Dentz invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks); 42531bbf4023SLuiz Augusto von Dentz 42541bbf4023SLuiz Augusto von Dentz if (!ret) { 42551bbf4023SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) { 42561bbf4023SLuiz Augusto von Dentz if (!bacmp(&hdev->public_addr, BDADDR_ANY)) 42571bbf4023SLuiz Augusto von Dentz hci_dev_get_bd_addr_from_property(hdev); 42581bbf4023SLuiz Augusto von Dentz 42591bbf4023SLuiz Augusto von Dentz if (bacmp(&hdev->public_addr, BDADDR_ANY) && 42601bbf4023SLuiz Augusto von Dentz hdev->set_bdaddr) { 42611bbf4023SLuiz Augusto von Dentz ret = hdev->set_bdaddr(hdev, 42621bbf4023SLuiz Augusto von Dentz &hdev->public_addr); 42631bbf4023SLuiz Augusto von Dentz 42641bbf4023SLuiz Augusto von Dentz /* If setting of the BD_ADDR from the device 42651bbf4023SLuiz Augusto von Dentz * property succeeds, then treat the address 42661bbf4023SLuiz Augusto von Dentz * as valid even if the invalid BD_ADDR 42671bbf4023SLuiz Augusto von Dentz * quirk indicates otherwise. 42681bbf4023SLuiz Augusto von Dentz */ 42691bbf4023SLuiz Augusto von Dentz if (!ret) 42701bbf4023SLuiz Augusto von Dentz invalid_bdaddr = false; 42711bbf4023SLuiz Augusto von Dentz } 42721bbf4023SLuiz Augusto von Dentz } 42731bbf4023SLuiz Augusto von Dentz } 42741bbf4023SLuiz Augusto von Dentz 42751bbf4023SLuiz Augusto von Dentz /* The transport driver can set these quirks before 42761bbf4023SLuiz Augusto von Dentz * creating the HCI device or in its setup callback. 42771bbf4023SLuiz Augusto von Dentz * 42781bbf4023SLuiz Augusto von Dentz * For the invalid BD_ADDR quirk it is possible that 42791bbf4023SLuiz Augusto von Dentz * it becomes a valid address if the bootloader does 42801bbf4023SLuiz Augusto von Dentz * provide it (see above). 42811bbf4023SLuiz Augusto von Dentz * 42821bbf4023SLuiz Augusto von Dentz * In case any of them is set, the controller has to 42831bbf4023SLuiz Augusto von Dentz * start up as unconfigured. 42841bbf4023SLuiz Augusto von Dentz */ 42851bbf4023SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || 42861bbf4023SLuiz Augusto von Dentz invalid_bdaddr) 42871bbf4023SLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_UNCONFIGURED); 42881bbf4023SLuiz Augusto von Dentz 42891bbf4023SLuiz Augusto von Dentz /* For an unconfigured controller it is required to 42901bbf4023SLuiz Augusto von Dentz * read at least the version information provided by 42911bbf4023SLuiz Augusto von Dentz * the Read Local Version Information command. 42921bbf4023SLuiz Augusto von Dentz * 42931bbf4023SLuiz Augusto von Dentz * If the set_bdaddr driver callback is provided, then 42941bbf4023SLuiz Augusto von Dentz * also the original Bluetooth public device address 42951bbf4023SLuiz Augusto von Dentz * will be read using the Read BD Address command. 42961bbf4023SLuiz Augusto von Dentz */ 42971bbf4023SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 42981bbf4023SLuiz Augusto von Dentz return hci_unconf_init_sync(hdev); 42991bbf4023SLuiz Augusto von Dentz 43001bbf4023SLuiz Augusto von Dentz return ret; 43011bbf4023SLuiz Augusto von Dentz } 43021bbf4023SLuiz Augusto von Dentz 43031bbf4023SLuiz Augusto von Dentz /* This function handles hdev init stage: 43041bbf4023SLuiz Augusto von Dentz * 43051bbf4023SLuiz Augusto von Dentz * Calls hci_dev_setup_sync to perform setup stage 43061bbf4023SLuiz Augusto von Dentz * Calls hci_init_sync to perform HCI command init sequence 43071bbf4023SLuiz Augusto von Dentz */ 43081bbf4023SLuiz Augusto von Dentz static int hci_dev_init_sync(struct hci_dev *hdev) 43091bbf4023SLuiz Augusto von Dentz { 43101bbf4023SLuiz Augusto von Dentz int ret; 43111bbf4023SLuiz Augusto von Dentz 43121bbf4023SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 43131bbf4023SLuiz Augusto von Dentz 43141bbf4023SLuiz Augusto von Dentz atomic_set(&hdev->cmd_cnt, 1); 43151bbf4023SLuiz Augusto von Dentz set_bit(HCI_INIT, &hdev->flags); 43161bbf4023SLuiz Augusto von Dentz 43171bbf4023SLuiz Augusto von Dentz ret = hci_dev_setup_sync(hdev); 43181bbf4023SLuiz Augusto von Dentz 43191bbf4023SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_CONFIG)) { 43201bbf4023SLuiz Augusto von Dentz /* If public address change is configured, ensure that 43211bbf4023SLuiz Augusto von Dentz * the address gets programmed. If the driver does not 43221bbf4023SLuiz Augusto von Dentz * support changing the public address, fail the power 43231bbf4023SLuiz Augusto von Dentz * on procedure. 43241bbf4023SLuiz Augusto von Dentz */ 43251bbf4023SLuiz Augusto von Dentz if (bacmp(&hdev->public_addr, BDADDR_ANY) && 43261bbf4023SLuiz Augusto von Dentz hdev->set_bdaddr) 43271bbf4023SLuiz Augusto von Dentz ret = hdev->set_bdaddr(hdev, &hdev->public_addr); 43281bbf4023SLuiz Augusto von Dentz else 43291bbf4023SLuiz Augusto von Dentz ret = -EADDRNOTAVAIL; 43301bbf4023SLuiz Augusto von Dentz } 43311bbf4023SLuiz Augusto von Dentz 43321bbf4023SLuiz Augusto von Dentz if (!ret) { 43331bbf4023SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 43341bbf4023SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 43351bbf4023SLuiz Augusto von Dentz ret = hci_init_sync(hdev); 43361bbf4023SLuiz Augusto von Dentz if (!ret && hdev->post_init) 43371bbf4023SLuiz Augusto von Dentz ret = hdev->post_init(hdev); 43381bbf4023SLuiz Augusto von Dentz } 43391bbf4023SLuiz Augusto von Dentz } 43401bbf4023SLuiz Augusto von Dentz 43411bbf4023SLuiz Augusto von Dentz /* If the HCI Reset command is clearing all diagnostic settings, 43421bbf4023SLuiz Augusto von Dentz * then they need to be reprogrammed after the init procedure 43431bbf4023SLuiz Augusto von Dentz * completed. 43441bbf4023SLuiz Augusto von Dentz */ 43451bbf4023SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) && 43461bbf4023SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 43471bbf4023SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag) 43481bbf4023SLuiz Augusto von Dentz ret = hdev->set_diag(hdev, true); 43491bbf4023SLuiz Augusto von Dentz 43501bbf4023SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 43511bbf4023SLuiz Augusto von Dentz msft_do_open(hdev); 43521bbf4023SLuiz Augusto von Dentz aosp_do_open(hdev); 43531bbf4023SLuiz Augusto von Dentz } 43541bbf4023SLuiz Augusto von Dentz 43551bbf4023SLuiz Augusto von Dentz clear_bit(HCI_INIT, &hdev->flags); 43561bbf4023SLuiz Augusto von Dentz 43571bbf4023SLuiz Augusto von Dentz return ret; 43581bbf4023SLuiz Augusto von Dentz } 43591bbf4023SLuiz Augusto von Dentz 43601bbf4023SLuiz Augusto von Dentz int hci_dev_open_sync(struct hci_dev *hdev) 43611bbf4023SLuiz Augusto von Dentz { 43621bbf4023SLuiz Augusto von Dentz int ret; 4363d0b13706SLuiz Augusto von Dentz 4364d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 4365d0b13706SLuiz Augusto von Dentz 4366d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) { 4367d0b13706SLuiz Augusto von Dentz ret = -ENODEV; 4368d0b13706SLuiz Augusto von Dentz goto done; 4369d0b13706SLuiz Augusto von Dentz } 4370d0b13706SLuiz Augusto von Dentz 4371d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SETUP) && 4372d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_CONFIG)) { 4373d0b13706SLuiz Augusto von Dentz /* Check for rfkill but allow the HCI setup stage to 4374d0b13706SLuiz Augusto von Dentz * proceed (which in itself doesn't cause any RF activity). 4375d0b13706SLuiz Augusto von Dentz */ 4376d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_RFKILLED)) { 4377d0b13706SLuiz Augusto von Dentz ret = -ERFKILL; 4378d0b13706SLuiz Augusto von Dentz goto done; 4379d0b13706SLuiz Augusto von Dentz } 4380d0b13706SLuiz Augusto von Dentz 4381d0b13706SLuiz Augusto von Dentz /* Check for valid public address or a configured static 4382d0b13706SLuiz Augusto von Dentz * random address, but let the HCI setup proceed to 4383d0b13706SLuiz Augusto von Dentz * be able to determine if there is a public address 4384d0b13706SLuiz Augusto von Dentz * or not. 4385d0b13706SLuiz Augusto von Dentz * 4386d0b13706SLuiz Augusto von Dentz * In case of user channel usage, it is not important 4387d0b13706SLuiz Augusto von Dentz * if a public address or static random address is 4388d0b13706SLuiz Augusto von Dentz * available. 4389d0b13706SLuiz Augusto von Dentz * 4390d0b13706SLuiz Augusto von Dentz * This check is only valid for BR/EDR controllers 4391d0b13706SLuiz Augusto von Dentz * since AMP controllers do not have an address. 4392d0b13706SLuiz Augusto von Dentz */ 4393d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 4394d0b13706SLuiz Augusto von Dentz hdev->dev_type == HCI_PRIMARY && 4395d0b13706SLuiz Augusto von Dentz !bacmp(&hdev->bdaddr, BDADDR_ANY) && 4396d0b13706SLuiz Augusto von Dentz !bacmp(&hdev->static_addr, BDADDR_ANY)) { 4397d0b13706SLuiz Augusto von Dentz ret = -EADDRNOTAVAIL; 4398d0b13706SLuiz Augusto von Dentz goto done; 4399d0b13706SLuiz Augusto von Dentz } 4400d0b13706SLuiz Augusto von Dentz } 4401d0b13706SLuiz Augusto von Dentz 4402d0b13706SLuiz Augusto von Dentz if (test_bit(HCI_UP, &hdev->flags)) { 4403d0b13706SLuiz Augusto von Dentz ret = -EALREADY; 4404d0b13706SLuiz Augusto von Dentz goto done; 4405d0b13706SLuiz Augusto von Dentz } 4406d0b13706SLuiz Augusto von Dentz 4407d0b13706SLuiz Augusto von Dentz if (hdev->open(hdev)) { 4408d0b13706SLuiz Augusto von Dentz ret = -EIO; 4409d0b13706SLuiz Augusto von Dentz goto done; 4410d0b13706SLuiz Augusto von Dentz } 4411d0b13706SLuiz Augusto von Dentz 4412d0b13706SLuiz Augusto von Dentz set_bit(HCI_RUNNING, &hdev->flags); 4413d0b13706SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_OPEN); 4414d0b13706SLuiz Augusto von Dentz 44151bbf4023SLuiz Augusto von Dentz ret = hci_dev_init_sync(hdev); 4416d0b13706SLuiz Augusto von Dentz if (!ret) { 4417d0b13706SLuiz Augusto von Dentz hci_dev_hold(hdev); 4418d0b13706SLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); 4419d0b13706SLuiz Augusto von Dentz hci_adv_instances_set_rpa_expired(hdev, true); 4420d0b13706SLuiz Augusto von Dentz set_bit(HCI_UP, &hdev->flags); 4421d0b13706SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_UP); 4422d0b13706SLuiz Augusto von Dentz hci_leds_update_powered(hdev, true); 4423d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SETUP) && 4424d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_CONFIG) && 4425d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 4426d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 4427d0b13706SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_MGMT) && 4428d0b13706SLuiz Augusto von Dentz hdev->dev_type == HCI_PRIMARY) { 4429d0b13706SLuiz Augusto von Dentz ret = hci_powered_update_sync(hdev); 4430d0b13706SLuiz Augusto von Dentz } 4431d0b13706SLuiz Augusto von Dentz } else { 4432d0b13706SLuiz Augusto von Dentz /* Init failed, cleanup */ 4433d0b13706SLuiz Augusto von Dentz flush_work(&hdev->tx_work); 4434d0b13706SLuiz Augusto von Dentz 4435d0b13706SLuiz Augusto von Dentz /* Since hci_rx_work() is possible to awake new cmd_work 4436d0b13706SLuiz Augusto von Dentz * it should be flushed first to avoid unexpected call of 4437d0b13706SLuiz Augusto von Dentz * hci_cmd_work() 4438d0b13706SLuiz Augusto von Dentz */ 4439d0b13706SLuiz Augusto von Dentz flush_work(&hdev->rx_work); 4440d0b13706SLuiz Augusto von Dentz flush_work(&hdev->cmd_work); 4441d0b13706SLuiz Augusto von Dentz 4442d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->cmd_q); 4443d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->rx_q); 4444d0b13706SLuiz Augusto von Dentz 4445d0b13706SLuiz Augusto von Dentz if (hdev->flush) 4446d0b13706SLuiz Augusto von Dentz hdev->flush(hdev); 4447d0b13706SLuiz Augusto von Dentz 4448d0b13706SLuiz Augusto von Dentz if (hdev->sent_cmd) { 4449d0b13706SLuiz Augusto von Dentz kfree_skb(hdev->sent_cmd); 4450d0b13706SLuiz Augusto von Dentz hdev->sent_cmd = NULL; 4451d0b13706SLuiz Augusto von Dentz } 4452d0b13706SLuiz Augusto von Dentz 4453d0b13706SLuiz Augusto von Dentz clear_bit(HCI_RUNNING, &hdev->flags); 4454d0b13706SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_CLOSE); 4455d0b13706SLuiz Augusto von Dentz 4456d0b13706SLuiz Augusto von Dentz hdev->close(hdev); 4457d0b13706SLuiz Augusto von Dentz hdev->flags &= BIT(HCI_RAW); 4458d0b13706SLuiz Augusto von Dentz } 4459d0b13706SLuiz Augusto von Dentz 4460d0b13706SLuiz Augusto von Dentz done: 4461d0b13706SLuiz Augusto von Dentz return ret; 4462d0b13706SLuiz Augusto von Dentz } 4463d0b13706SLuiz Augusto von Dentz 4464d0b13706SLuiz Augusto von Dentz /* This function requires the caller holds hdev->lock */ 4465d0b13706SLuiz Augusto von Dentz static void hci_pend_le_actions_clear(struct hci_dev *hdev) 4466d0b13706SLuiz Augusto von Dentz { 4467d0b13706SLuiz Augusto von Dentz struct hci_conn_params *p; 4468d0b13706SLuiz Augusto von Dentz 4469d0b13706SLuiz Augusto von Dentz list_for_each_entry(p, &hdev->le_conn_params, list) { 4470d0b13706SLuiz Augusto von Dentz if (p->conn) { 4471d0b13706SLuiz Augusto von Dentz hci_conn_drop(p->conn); 4472d0b13706SLuiz Augusto von Dentz hci_conn_put(p->conn); 4473d0b13706SLuiz Augusto von Dentz p->conn = NULL; 4474d0b13706SLuiz Augusto von Dentz } 4475d0b13706SLuiz Augusto von Dentz list_del_init(&p->action); 4476d0b13706SLuiz Augusto von Dentz } 4477d0b13706SLuiz Augusto von Dentz 4478d0b13706SLuiz Augusto von Dentz BT_DBG("All LE pending actions cleared"); 4479d0b13706SLuiz Augusto von Dentz } 4480d0b13706SLuiz Augusto von Dentz 4481d0b13706SLuiz Augusto von Dentz int hci_dev_close_sync(struct hci_dev *hdev) 4482d0b13706SLuiz Augusto von Dentz { 4483d0b13706SLuiz Augusto von Dentz bool auto_off; 4484d0b13706SLuiz Augusto von Dentz int err = 0; 4485d0b13706SLuiz Augusto von Dentz 4486d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 4487d0b13706SLuiz Augusto von Dentz 4488d0b13706SLuiz Augusto von Dentz cancel_delayed_work(&hdev->power_off); 4489d0b13706SLuiz Augusto von Dentz cancel_delayed_work(&hdev->ncmd_timer); 4490*8ffde2a7SBrian Gix cancel_delayed_work(&hdev->le_scan_disable); 4491d0b13706SLuiz Augusto von Dentz 4492d0b13706SLuiz Augusto von Dentz hci_request_cancel_all(hdev); 4493d0b13706SLuiz Augusto von Dentz 4494d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && 4495d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 4496d0b13706SLuiz Augusto von Dentz test_bit(HCI_UP, &hdev->flags)) { 4497d0b13706SLuiz Augusto von Dentz /* Execute vendor specific shutdown routine */ 4498d0b13706SLuiz Augusto von Dentz if (hdev->shutdown) 4499d0b13706SLuiz Augusto von Dentz err = hdev->shutdown(hdev); 4500d0b13706SLuiz Augusto von Dentz } 4501d0b13706SLuiz Augusto von Dentz 4502d0b13706SLuiz Augusto von Dentz if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { 4503d0b13706SLuiz Augusto von Dentz cancel_delayed_work_sync(&hdev->cmd_timer); 4504d0b13706SLuiz Augusto von Dentz return err; 4505d0b13706SLuiz Augusto von Dentz } 4506d0b13706SLuiz Augusto von Dentz 4507d0b13706SLuiz Augusto von Dentz hci_leds_update_powered(hdev, false); 4508d0b13706SLuiz Augusto von Dentz 4509d0b13706SLuiz Augusto von Dentz /* Flush RX and TX works */ 4510d0b13706SLuiz Augusto von Dentz flush_work(&hdev->tx_work); 4511d0b13706SLuiz Augusto von Dentz flush_work(&hdev->rx_work); 4512d0b13706SLuiz Augusto von Dentz 4513d0b13706SLuiz Augusto von Dentz if (hdev->discov_timeout > 0) { 4514d0b13706SLuiz Augusto von Dentz hdev->discov_timeout = 0; 4515d0b13706SLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); 4516d0b13706SLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 4517d0b13706SLuiz Augusto von Dentz } 4518d0b13706SLuiz Augusto von Dentz 4519d0b13706SLuiz Augusto von Dentz if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) 4520d0b13706SLuiz Augusto von Dentz cancel_delayed_work(&hdev->service_cache); 4521d0b13706SLuiz Augusto von Dentz 4522d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_MGMT)) { 4523d0b13706SLuiz Augusto von Dentz struct adv_info *adv_instance; 4524d0b13706SLuiz Augusto von Dentz 4525d0b13706SLuiz Augusto von Dentz cancel_delayed_work_sync(&hdev->rpa_expired); 4526d0b13706SLuiz Augusto von Dentz 4527d0b13706SLuiz Augusto von Dentz list_for_each_entry(adv_instance, &hdev->adv_instances, list) 4528d0b13706SLuiz Augusto von Dentz cancel_delayed_work_sync(&adv_instance->rpa_expired_cb); 4529d0b13706SLuiz Augusto von Dentz } 4530d0b13706SLuiz Augusto von Dentz 4531d0b13706SLuiz Augusto von Dentz /* Avoid potential lockdep warnings from the *_flush() calls by 4532d0b13706SLuiz Augusto von Dentz * ensuring the workqueue is empty up front. 4533d0b13706SLuiz Augusto von Dentz */ 4534d0b13706SLuiz Augusto von Dentz drain_workqueue(hdev->workqueue); 4535d0b13706SLuiz Augusto von Dentz 4536d0b13706SLuiz Augusto von Dentz hci_dev_lock(hdev); 4537d0b13706SLuiz Augusto von Dentz 4538d0b13706SLuiz Augusto von Dentz hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 4539d0b13706SLuiz Augusto von Dentz 4540d0b13706SLuiz Augusto von Dentz auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF); 4541d0b13706SLuiz Augusto von Dentz 4542d0b13706SLuiz Augusto von Dentz if (!auto_off && hdev->dev_type == HCI_PRIMARY && 4543d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 4544d0b13706SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_MGMT)) 4545d0b13706SLuiz Augusto von Dentz __mgmt_power_off(hdev); 4546d0b13706SLuiz Augusto von Dentz 4547d0b13706SLuiz Augusto von Dentz hci_inquiry_cache_flush(hdev); 4548d0b13706SLuiz Augusto von Dentz hci_pend_le_actions_clear(hdev); 4549d0b13706SLuiz Augusto von Dentz hci_conn_hash_flush(hdev); 4550fa78d2d1SLin Ma /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */ 4551d0b13706SLuiz Augusto von Dentz smp_unregister(hdev); 4552fa78d2d1SLin Ma hci_dev_unlock(hdev); 4553d0b13706SLuiz Augusto von Dentz 4554d0b13706SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_DOWN); 4555d0b13706SLuiz Augusto von Dentz 4556385315deSJesse Melhuish if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 4557d0b13706SLuiz Augusto von Dentz aosp_do_close(hdev); 4558d0b13706SLuiz Augusto von Dentz msft_do_close(hdev); 4559385315deSJesse Melhuish } 4560d0b13706SLuiz Augusto von Dentz 4561d0b13706SLuiz Augusto von Dentz if (hdev->flush) 4562d0b13706SLuiz Augusto von Dentz hdev->flush(hdev); 4563d0b13706SLuiz Augusto von Dentz 4564d0b13706SLuiz Augusto von Dentz /* Reset device */ 4565d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->cmd_q); 4566d0b13706SLuiz Augusto von Dentz atomic_set(&hdev->cmd_cnt, 1); 4567d0b13706SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) && 4568d0b13706SLuiz Augusto von Dentz !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 4569d0b13706SLuiz Augusto von Dentz set_bit(HCI_INIT, &hdev->flags); 4570d0b13706SLuiz Augusto von Dentz hci_reset_sync(hdev); 4571d0b13706SLuiz Augusto von Dentz clear_bit(HCI_INIT, &hdev->flags); 4572d0b13706SLuiz Augusto von Dentz } 4573d0b13706SLuiz Augusto von Dentz 4574d0b13706SLuiz Augusto von Dentz /* flush cmd work */ 4575d0b13706SLuiz Augusto von Dentz flush_work(&hdev->cmd_work); 4576d0b13706SLuiz Augusto von Dentz 4577d0b13706SLuiz Augusto von Dentz /* Drop queues */ 4578d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->rx_q); 4579d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->cmd_q); 4580d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->raw_q); 4581d0b13706SLuiz Augusto von Dentz 4582d0b13706SLuiz Augusto von Dentz /* Drop last sent command */ 4583d0b13706SLuiz Augusto von Dentz if (hdev->sent_cmd) { 4584d0b13706SLuiz Augusto von Dentz cancel_delayed_work_sync(&hdev->cmd_timer); 4585d0b13706SLuiz Augusto von Dentz kfree_skb(hdev->sent_cmd); 4586d0b13706SLuiz Augusto von Dentz hdev->sent_cmd = NULL; 4587d0b13706SLuiz Augusto von Dentz } 4588d0b13706SLuiz Augusto von Dentz 4589d0b13706SLuiz Augusto von Dentz clear_bit(HCI_RUNNING, &hdev->flags); 4590d0b13706SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_CLOSE); 4591d0b13706SLuiz Augusto von Dentz 4592d0b13706SLuiz Augusto von Dentz /* After this point our queues are empty and no tasks are scheduled. */ 4593d0b13706SLuiz Augusto von Dentz hdev->close(hdev); 4594d0b13706SLuiz Augusto von Dentz 4595d0b13706SLuiz Augusto von Dentz /* Clear flags */ 4596d0b13706SLuiz Augusto von Dentz hdev->flags &= BIT(HCI_RAW); 4597d0b13706SLuiz Augusto von Dentz hci_dev_clear_volatile_flags(hdev); 4598d0b13706SLuiz Augusto von Dentz 4599d0b13706SLuiz Augusto von Dentz /* Controller radio is available but is currently powered down */ 4600d0b13706SLuiz Augusto von Dentz hdev->amp_status = AMP_STATUS_POWERED_DOWN; 4601d0b13706SLuiz Augusto von Dentz 4602d0b13706SLuiz Augusto von Dentz memset(hdev->eir, 0, sizeof(hdev->eir)); 4603d0b13706SLuiz Augusto von Dentz memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); 4604d0b13706SLuiz Augusto von Dentz bacpy(&hdev->random_addr, BDADDR_ANY); 4605d0b13706SLuiz Augusto von Dentz 4606d0b13706SLuiz Augusto von Dentz hci_dev_put(hdev); 4607d0b13706SLuiz Augusto von Dentz return err; 4608d0b13706SLuiz Augusto von Dentz } 4609d0b13706SLuiz Augusto von Dentz 4610cf75ad8bSLuiz Augusto von Dentz /* This function perform power on HCI command sequence as follows: 4611cf75ad8bSLuiz Augusto von Dentz * 4612cf75ad8bSLuiz Augusto von Dentz * If controller is already up (HCI_UP) performs hci_powered_update_sync 4613cf75ad8bSLuiz Augusto von Dentz * sequence otherwise run hci_dev_open_sync which will follow with 4614cf75ad8bSLuiz Augusto von Dentz * hci_powered_update_sync after the init sequence is completed. 4615cf75ad8bSLuiz Augusto von Dentz */ 4616cf75ad8bSLuiz Augusto von Dentz static int hci_power_on_sync(struct hci_dev *hdev) 4617cf75ad8bSLuiz Augusto von Dentz { 4618cf75ad8bSLuiz Augusto von Dentz int err; 4619cf75ad8bSLuiz Augusto von Dentz 4620cf75ad8bSLuiz Augusto von Dentz if (test_bit(HCI_UP, &hdev->flags) && 4621cf75ad8bSLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_MGMT) && 4622cf75ad8bSLuiz Augusto von Dentz hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) { 4623cf75ad8bSLuiz Augusto von Dentz cancel_delayed_work(&hdev->power_off); 4624cf75ad8bSLuiz Augusto von Dentz return hci_powered_update_sync(hdev); 4625cf75ad8bSLuiz Augusto von Dentz } 4626cf75ad8bSLuiz Augusto von Dentz 4627cf75ad8bSLuiz Augusto von Dentz err = hci_dev_open_sync(hdev); 4628cf75ad8bSLuiz Augusto von Dentz if (err < 0) 4629cf75ad8bSLuiz Augusto von Dentz return err; 4630cf75ad8bSLuiz Augusto von Dentz 4631cf75ad8bSLuiz Augusto von Dentz /* During the HCI setup phase, a few error conditions are 4632cf75ad8bSLuiz Augusto von Dentz * ignored and they need to be checked now. If they are still 4633cf75ad8bSLuiz Augusto von Dentz * valid, it is important to return the device back off. 4634cf75ad8bSLuiz Augusto von Dentz */ 4635cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_RFKILLED) || 4636cf75ad8bSLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_UNCONFIGURED) || 4637cf75ad8bSLuiz Augusto von Dentz (hdev->dev_type == HCI_PRIMARY && 4638cf75ad8bSLuiz Augusto von Dentz !bacmp(&hdev->bdaddr, BDADDR_ANY) && 4639cf75ad8bSLuiz Augusto von Dentz !bacmp(&hdev->static_addr, BDADDR_ANY))) { 4640cf75ad8bSLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_AUTO_OFF); 4641cf75ad8bSLuiz Augusto von Dentz hci_dev_close_sync(hdev); 4642cf75ad8bSLuiz Augusto von Dentz } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) { 4643cf75ad8bSLuiz Augusto von Dentz queue_delayed_work(hdev->req_workqueue, &hdev->power_off, 4644cf75ad8bSLuiz Augusto von Dentz HCI_AUTO_OFF_TIMEOUT); 4645cf75ad8bSLuiz Augusto von Dentz } 4646cf75ad8bSLuiz Augusto von Dentz 4647cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) { 4648cf75ad8bSLuiz Augusto von Dentz /* For unconfigured devices, set the HCI_RAW flag 4649cf75ad8bSLuiz Augusto von Dentz * so that userspace can easily identify them. 4650cf75ad8bSLuiz Augusto von Dentz */ 4651cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 4652cf75ad8bSLuiz Augusto von Dentz set_bit(HCI_RAW, &hdev->flags); 4653cf75ad8bSLuiz Augusto von Dentz 4654cf75ad8bSLuiz Augusto von Dentz /* For fully configured devices, this will send 4655cf75ad8bSLuiz Augusto von Dentz * the Index Added event. For unconfigured devices, 4656cf75ad8bSLuiz Augusto von Dentz * it will send Unconfigued Index Added event. 4657cf75ad8bSLuiz Augusto von Dentz * 4658cf75ad8bSLuiz Augusto von Dentz * Devices with HCI_QUIRK_RAW_DEVICE are ignored 4659cf75ad8bSLuiz Augusto von Dentz * and no event will be send. 4660cf75ad8bSLuiz Augusto von Dentz */ 4661cf75ad8bSLuiz Augusto von Dentz mgmt_index_added(hdev); 4662cf75ad8bSLuiz Augusto von Dentz } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) { 4663cf75ad8bSLuiz Augusto von Dentz /* When the controller is now configured, then it 4664cf75ad8bSLuiz Augusto von Dentz * is important to clear the HCI_RAW flag. 4665cf75ad8bSLuiz Augusto von Dentz */ 4666cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 4667cf75ad8bSLuiz Augusto von Dentz clear_bit(HCI_RAW, &hdev->flags); 4668cf75ad8bSLuiz Augusto von Dentz 4669cf75ad8bSLuiz Augusto von Dentz /* Powering on the controller with HCI_CONFIG set only 4670cf75ad8bSLuiz Augusto von Dentz * happens with the transition from unconfigured to 4671cf75ad8bSLuiz Augusto von Dentz * configured. This will send the Index Added event. 4672cf75ad8bSLuiz Augusto von Dentz */ 4673cf75ad8bSLuiz Augusto von Dentz mgmt_index_added(hdev); 4674cf75ad8bSLuiz Augusto von Dentz } 4675cf75ad8bSLuiz Augusto von Dentz 4676cf75ad8bSLuiz Augusto von Dentz return 0; 4677cf75ad8bSLuiz Augusto von Dentz } 4678cf75ad8bSLuiz Augusto von Dentz 4679cf75ad8bSLuiz Augusto von Dentz static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr) 4680cf75ad8bSLuiz Augusto von Dentz { 4681cf75ad8bSLuiz Augusto von Dentz struct hci_cp_remote_name_req_cancel cp; 4682cf75ad8bSLuiz Augusto von Dentz 4683cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 4684cf75ad8bSLuiz Augusto von Dentz bacpy(&cp.bdaddr, addr); 4685cf75ad8bSLuiz Augusto von Dentz 4686cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL, 4687cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 4688cf75ad8bSLuiz Augusto von Dentz } 4689cf75ad8bSLuiz Augusto von Dentz 4690abfeea47SLuiz Augusto von Dentz int hci_stop_discovery_sync(struct hci_dev *hdev) 4691cf75ad8bSLuiz Augusto von Dentz { 4692cf75ad8bSLuiz Augusto von Dentz struct discovery_state *d = &hdev->discovery; 4693cf75ad8bSLuiz Augusto von Dentz struct inquiry_entry *e; 4694cf75ad8bSLuiz Augusto von Dentz int err; 4695cf75ad8bSLuiz Augusto von Dentz 4696cf75ad8bSLuiz Augusto von Dentz bt_dev_dbg(hdev, "state %u", hdev->discovery.state); 4697cf75ad8bSLuiz Augusto von Dentz 4698cf75ad8bSLuiz Augusto von Dentz if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) { 4699cf75ad8bSLuiz Augusto von Dentz if (test_bit(HCI_INQUIRY, &hdev->flags)) { 4700cf75ad8bSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 4701cf75ad8bSLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT); 4702cf75ad8bSLuiz Augusto von Dentz if (err) 4703cf75ad8bSLuiz Augusto von Dentz return err; 4704cf75ad8bSLuiz Augusto von Dentz } 4705cf75ad8bSLuiz Augusto von Dentz 4706cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { 4707cf75ad8bSLuiz Augusto von Dentz cancel_delayed_work(&hdev->le_scan_disable); 4708cf75ad8bSLuiz Augusto von Dentz cancel_delayed_work(&hdev->le_scan_restart); 4709cf75ad8bSLuiz Augusto von Dentz 4710cf75ad8bSLuiz Augusto von Dentz err = hci_scan_disable_sync(hdev); 4711cf75ad8bSLuiz Augusto von Dentz if (err) 4712cf75ad8bSLuiz Augusto von Dentz return err; 4713cf75ad8bSLuiz Augusto von Dentz } 4714cf75ad8bSLuiz Augusto von Dentz 4715cf75ad8bSLuiz Augusto von Dentz } else { 4716cf75ad8bSLuiz Augusto von Dentz err = hci_scan_disable_sync(hdev); 4717cf75ad8bSLuiz Augusto von Dentz if (err) 4718cf75ad8bSLuiz Augusto von Dentz return err; 4719cf75ad8bSLuiz Augusto von Dentz } 4720cf75ad8bSLuiz Augusto von Dentz 4721abfeea47SLuiz Augusto von Dentz /* Resume advertising if it was paused */ 4722abfeea47SLuiz Augusto von Dentz if (use_ll_privacy(hdev)) 4723abfeea47SLuiz Augusto von Dentz hci_resume_advertising_sync(hdev); 4724abfeea47SLuiz Augusto von Dentz 4725cf75ad8bSLuiz Augusto von Dentz /* No further actions needed for LE-only discovery */ 4726cf75ad8bSLuiz Augusto von Dentz if (d->type == DISCOV_TYPE_LE) 4727cf75ad8bSLuiz Augusto von Dentz return 0; 4728cf75ad8bSLuiz Augusto von Dentz 4729cf75ad8bSLuiz Augusto von Dentz if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) { 4730cf75ad8bSLuiz Augusto von Dentz e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, 4731cf75ad8bSLuiz Augusto von Dentz NAME_PENDING); 4732cf75ad8bSLuiz Augusto von Dentz if (!e) 4733cf75ad8bSLuiz Augusto von Dentz return 0; 4734cf75ad8bSLuiz Augusto von Dentz 4735cf75ad8bSLuiz Augusto von Dentz return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr); 4736cf75ad8bSLuiz Augusto von Dentz } 4737cf75ad8bSLuiz Augusto von Dentz 4738cf75ad8bSLuiz Augusto von Dentz return 0; 4739cf75ad8bSLuiz Augusto von Dentz } 4740cf75ad8bSLuiz Augusto von Dentz 4741cf75ad8bSLuiz Augusto von Dentz static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle, 4742cf75ad8bSLuiz Augusto von Dentz u8 reason) 4743cf75ad8bSLuiz Augusto von Dentz { 4744cf75ad8bSLuiz Augusto von Dentz struct hci_cp_disconn_phy_link cp; 4745cf75ad8bSLuiz Augusto von Dentz 4746cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 4747cf75ad8bSLuiz Augusto von Dentz cp.phy_handle = HCI_PHY_HANDLE(handle); 4748cf75ad8bSLuiz Augusto von Dentz cp.reason = reason; 4749cf75ad8bSLuiz Augusto von Dentz 4750cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK, 4751cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 4752cf75ad8bSLuiz Augusto von Dentz } 4753cf75ad8bSLuiz Augusto von Dentz 4754cf75ad8bSLuiz Augusto von Dentz static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn, 4755cf75ad8bSLuiz Augusto von Dentz u8 reason) 4756cf75ad8bSLuiz Augusto von Dentz { 4757cf75ad8bSLuiz Augusto von Dentz struct hci_cp_disconnect cp; 4758cf75ad8bSLuiz Augusto von Dentz 4759cf75ad8bSLuiz Augusto von Dentz if (conn->type == AMP_LINK) 4760cf75ad8bSLuiz Augusto von Dentz return hci_disconnect_phy_link_sync(hdev, conn->handle, reason); 4761cf75ad8bSLuiz Augusto von Dentz 4762cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 4763cf75ad8bSLuiz Augusto von Dentz cp.handle = cpu_to_le16(conn->handle); 4764cf75ad8bSLuiz Augusto von Dentz cp.reason = reason; 4765cf75ad8bSLuiz Augusto von Dentz 4766d0b13706SLuiz Augusto von Dentz /* Wait for HCI_EV_DISCONN_COMPLETE not HCI_EV_CMD_STATUS when not 4767d0b13706SLuiz Augusto von Dentz * suspending. 4768d0b13706SLuiz Augusto von Dentz */ 4769d0b13706SLuiz Augusto von Dentz if (!hdev->suspended) 4770d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT, 4771d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, 4772d0b13706SLuiz Augusto von Dentz HCI_EV_DISCONN_COMPLETE, 4773d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT, NULL); 4774d0b13706SLuiz Augusto von Dentz 4775d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp, 4776d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 4777cf75ad8bSLuiz Augusto von Dentz } 4778cf75ad8bSLuiz Augusto von Dentz 4779cf75ad8bSLuiz Augusto von Dentz static int hci_le_connect_cancel_sync(struct hci_dev *hdev, 4780cf75ad8bSLuiz Augusto von Dentz struct hci_conn *conn) 4781cf75ad8bSLuiz Augusto von Dentz { 4782cf75ad8bSLuiz Augusto von Dentz if (test_bit(HCI_CONN_SCANNING, &conn->flags)) 4783cf75ad8bSLuiz Augusto von Dentz return 0; 4784cf75ad8bSLuiz Augusto von Dentz 4785cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 4786cf75ad8bSLuiz Augusto von Dentz 6, &conn->dst, HCI_CMD_TIMEOUT); 4787cf75ad8bSLuiz Augusto von Dentz } 4788cf75ad8bSLuiz Augusto von Dentz 4789cf75ad8bSLuiz Augusto von Dentz static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn) 4790cf75ad8bSLuiz Augusto von Dentz { 4791cf75ad8bSLuiz Augusto von Dentz if (conn->type == LE_LINK) 4792cf75ad8bSLuiz Augusto von Dentz return hci_le_connect_cancel_sync(hdev, conn); 4793cf75ad8bSLuiz Augusto von Dentz 4794cf75ad8bSLuiz Augusto von Dentz if (hdev->hci_ver < BLUETOOTH_VER_1_2) 4795cf75ad8bSLuiz Augusto von Dentz return 0; 4796cf75ad8bSLuiz Augusto von Dentz 4797cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL, 4798cf75ad8bSLuiz Augusto von Dentz 6, &conn->dst, HCI_CMD_TIMEOUT); 4799cf75ad8bSLuiz Augusto von Dentz } 4800cf75ad8bSLuiz Augusto von Dentz 4801cf75ad8bSLuiz Augusto von Dentz static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn, 4802cf75ad8bSLuiz Augusto von Dentz u8 reason) 4803cf75ad8bSLuiz Augusto von Dentz { 4804cf75ad8bSLuiz Augusto von Dentz struct hci_cp_reject_sync_conn_req cp; 4805cf75ad8bSLuiz Augusto von Dentz 4806cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 4807cf75ad8bSLuiz Augusto von Dentz bacpy(&cp.bdaddr, &conn->dst); 4808cf75ad8bSLuiz Augusto von Dentz cp.reason = reason; 4809cf75ad8bSLuiz Augusto von Dentz 4810cf75ad8bSLuiz Augusto von Dentz /* SCO rejection has its own limited set of 4811cf75ad8bSLuiz Augusto von Dentz * allowed error values (0x0D-0x0F). 4812cf75ad8bSLuiz Augusto von Dentz */ 4813cf75ad8bSLuiz Augusto von Dentz if (reason < 0x0d || reason > 0x0f) 4814cf75ad8bSLuiz Augusto von Dentz cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES; 4815cf75ad8bSLuiz Augusto von Dentz 4816cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ, 4817cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 4818cf75ad8bSLuiz Augusto von Dentz } 4819cf75ad8bSLuiz Augusto von Dentz 4820cf75ad8bSLuiz Augusto von Dentz static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, 4821cf75ad8bSLuiz Augusto von Dentz u8 reason) 4822cf75ad8bSLuiz Augusto von Dentz { 4823cf75ad8bSLuiz Augusto von Dentz struct hci_cp_reject_conn_req cp; 4824cf75ad8bSLuiz Augusto von Dentz 4825cf75ad8bSLuiz Augusto von Dentz if (conn->type == SCO_LINK || conn->type == ESCO_LINK) 4826cf75ad8bSLuiz Augusto von Dentz return hci_reject_sco_sync(hdev, conn, reason); 4827cf75ad8bSLuiz Augusto von Dentz 4828cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 4829cf75ad8bSLuiz Augusto von Dentz bacpy(&cp.bdaddr, &conn->dst); 4830cf75ad8bSLuiz Augusto von Dentz cp.reason = reason; 4831cf75ad8bSLuiz Augusto von Dentz 4832cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ, 4833cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 4834cf75ad8bSLuiz Augusto von Dentz } 4835cf75ad8bSLuiz Augusto von Dentz 48361f7435c8SLuiz Augusto von Dentz int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason) 4837cf75ad8bSLuiz Augusto von Dentz { 48389b3628d7SLuiz Augusto von Dentz int err; 48399b3628d7SLuiz Augusto von Dentz 4840cf75ad8bSLuiz Augusto von Dentz switch (conn->state) { 4841cf75ad8bSLuiz Augusto von Dentz case BT_CONNECTED: 4842cf75ad8bSLuiz Augusto von Dentz case BT_CONFIG: 4843cf75ad8bSLuiz Augusto von Dentz return hci_disconnect_sync(hdev, conn, reason); 4844cf75ad8bSLuiz Augusto von Dentz case BT_CONNECT: 48459b3628d7SLuiz Augusto von Dentz err = hci_connect_cancel_sync(hdev, conn); 48469b3628d7SLuiz Augusto von Dentz /* Cleanup hci_conn object if it cannot be cancelled as it 48479b3628d7SLuiz Augusto von Dentz * likelly means the controller and host stack are out of sync. 48489b3628d7SLuiz Augusto von Dentz */ 48499b3628d7SLuiz Augusto von Dentz if (err) 48509b3628d7SLuiz Augusto von Dentz hci_conn_failed(conn, err); 48519b3628d7SLuiz Augusto von Dentz 48529b3628d7SLuiz Augusto von Dentz return err; 4853cf75ad8bSLuiz Augusto von Dentz case BT_CONNECT2: 4854cf75ad8bSLuiz Augusto von Dentz return hci_reject_conn_sync(hdev, conn, reason); 4855cf75ad8bSLuiz Augusto von Dentz default: 4856cf75ad8bSLuiz Augusto von Dentz conn->state = BT_CLOSED; 4857cf75ad8bSLuiz Augusto von Dentz break; 4858cf75ad8bSLuiz Augusto von Dentz } 4859cf75ad8bSLuiz Augusto von Dentz 4860cf75ad8bSLuiz Augusto von Dentz return 0; 4861cf75ad8bSLuiz Augusto von Dentz } 4862cf75ad8bSLuiz Augusto von Dentz 4863182ee45dSLuiz Augusto von Dentz static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason) 4864182ee45dSLuiz Augusto von Dentz { 4865182ee45dSLuiz Augusto von Dentz struct hci_conn *conn, *tmp; 4866182ee45dSLuiz Augusto von Dentz int err; 4867182ee45dSLuiz Augusto von Dentz 4868182ee45dSLuiz Augusto von Dentz list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) { 4869182ee45dSLuiz Augusto von Dentz err = hci_abort_conn_sync(hdev, conn, reason); 4870182ee45dSLuiz Augusto von Dentz if (err) 4871182ee45dSLuiz Augusto von Dentz return err; 4872182ee45dSLuiz Augusto von Dentz } 4873182ee45dSLuiz Augusto von Dentz 48748cd3c55cSTom Rix return 0; 4875182ee45dSLuiz Augusto von Dentz } 4876182ee45dSLuiz Augusto von Dentz 4877cf75ad8bSLuiz Augusto von Dentz /* This function perform power off HCI command sequence as follows: 4878cf75ad8bSLuiz Augusto von Dentz * 4879cf75ad8bSLuiz Augusto von Dentz * Clear Advertising 4880cf75ad8bSLuiz Augusto von Dentz * Stop Discovery 4881cf75ad8bSLuiz Augusto von Dentz * Disconnect all connections 4882cf75ad8bSLuiz Augusto von Dentz * hci_dev_close_sync 4883cf75ad8bSLuiz Augusto von Dentz */ 4884cf75ad8bSLuiz Augusto von Dentz static int hci_power_off_sync(struct hci_dev *hdev) 4885cf75ad8bSLuiz Augusto von Dentz { 4886cf75ad8bSLuiz Augusto von Dentz int err; 4887cf75ad8bSLuiz Augusto von Dentz 4888cf75ad8bSLuiz Augusto von Dentz /* If controller is already down there is nothing to do */ 4889cf75ad8bSLuiz Augusto von Dentz if (!test_bit(HCI_UP, &hdev->flags)) 4890cf75ad8bSLuiz Augusto von Dentz return 0; 4891cf75ad8bSLuiz Augusto von Dentz 4892cf75ad8bSLuiz Augusto von Dentz if (test_bit(HCI_ISCAN, &hdev->flags) || 4893cf75ad8bSLuiz Augusto von Dentz test_bit(HCI_PSCAN, &hdev->flags)) { 4894cf75ad8bSLuiz Augusto von Dentz err = hci_write_scan_enable_sync(hdev, 0x00); 4895cf75ad8bSLuiz Augusto von Dentz if (err) 4896cf75ad8bSLuiz Augusto von Dentz return err; 4897cf75ad8bSLuiz Augusto von Dentz } 4898cf75ad8bSLuiz Augusto von Dentz 4899cf75ad8bSLuiz Augusto von Dentz err = hci_clear_adv_sync(hdev, NULL, false); 4900cf75ad8bSLuiz Augusto von Dentz if (err) 4901cf75ad8bSLuiz Augusto von Dentz return err; 4902cf75ad8bSLuiz Augusto von Dentz 4903cf75ad8bSLuiz Augusto von Dentz err = hci_stop_discovery_sync(hdev); 4904cf75ad8bSLuiz Augusto von Dentz if (err) 4905cf75ad8bSLuiz Augusto von Dentz return err; 4906cf75ad8bSLuiz Augusto von Dentz 4907182ee45dSLuiz Augusto von Dentz /* Terminated due to Power Off */ 4908182ee45dSLuiz Augusto von Dentz err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF); 4909182ee45dSLuiz Augusto von Dentz if (err) 4910182ee45dSLuiz Augusto von Dentz return err; 4911cf75ad8bSLuiz Augusto von Dentz 4912cf75ad8bSLuiz Augusto von Dentz return hci_dev_close_sync(hdev); 4913cf75ad8bSLuiz Augusto von Dentz } 4914cf75ad8bSLuiz Augusto von Dentz 4915cf75ad8bSLuiz Augusto von Dentz int hci_set_powered_sync(struct hci_dev *hdev, u8 val) 4916cf75ad8bSLuiz Augusto von Dentz { 4917cf75ad8bSLuiz Augusto von Dentz if (val) 4918cf75ad8bSLuiz Augusto von Dentz return hci_power_on_sync(hdev); 4919cf75ad8bSLuiz Augusto von Dentz 4920cf75ad8bSLuiz Augusto von Dentz return hci_power_off_sync(hdev); 4921cf75ad8bSLuiz Augusto von Dentz } 4922abfeea47SLuiz Augusto von Dentz 49232bd1b237SLuiz Augusto von Dentz static int hci_write_iac_sync(struct hci_dev *hdev) 49242bd1b237SLuiz Augusto von Dentz { 49252bd1b237SLuiz Augusto von Dentz struct hci_cp_write_current_iac_lap cp; 49262bd1b237SLuiz Augusto von Dentz 49272bd1b237SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) 49282bd1b237SLuiz Augusto von Dentz return 0; 49292bd1b237SLuiz Augusto von Dentz 49302bd1b237SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 49312bd1b237SLuiz Augusto von Dentz 49322bd1b237SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { 49332bd1b237SLuiz Augusto von Dentz /* Limited discoverable mode */ 49342bd1b237SLuiz Augusto von Dentz cp.num_iac = min_t(u8, hdev->num_iac, 2); 49352bd1b237SLuiz Augusto von Dentz cp.iac_lap[0] = 0x00; /* LIAC */ 49362bd1b237SLuiz Augusto von Dentz cp.iac_lap[1] = 0x8b; 49372bd1b237SLuiz Augusto von Dentz cp.iac_lap[2] = 0x9e; 49382bd1b237SLuiz Augusto von Dentz cp.iac_lap[3] = 0x33; /* GIAC */ 49392bd1b237SLuiz Augusto von Dentz cp.iac_lap[4] = 0x8b; 49402bd1b237SLuiz Augusto von Dentz cp.iac_lap[5] = 0x9e; 49412bd1b237SLuiz Augusto von Dentz } else { 49422bd1b237SLuiz Augusto von Dentz /* General discoverable mode */ 49432bd1b237SLuiz Augusto von Dentz cp.num_iac = 1; 49442bd1b237SLuiz Augusto von Dentz cp.iac_lap[0] = 0x33; /* GIAC */ 49452bd1b237SLuiz Augusto von Dentz cp.iac_lap[1] = 0x8b; 49462bd1b237SLuiz Augusto von Dentz cp.iac_lap[2] = 0x9e; 49472bd1b237SLuiz Augusto von Dentz } 49482bd1b237SLuiz Augusto von Dentz 49492bd1b237SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP, 49502bd1b237SLuiz Augusto von Dentz (cp.num_iac * 3) + 1, &cp, 49512bd1b237SLuiz Augusto von Dentz HCI_CMD_TIMEOUT); 49522bd1b237SLuiz Augusto von Dentz } 49532bd1b237SLuiz Augusto von Dentz 49542bd1b237SLuiz Augusto von Dentz int hci_update_discoverable_sync(struct hci_dev *hdev) 49552bd1b237SLuiz Augusto von Dentz { 49562bd1b237SLuiz Augusto von Dentz int err = 0; 49572bd1b237SLuiz Augusto von Dentz 49582bd1b237SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 49592bd1b237SLuiz Augusto von Dentz err = hci_write_iac_sync(hdev); 49602bd1b237SLuiz Augusto von Dentz if (err) 49612bd1b237SLuiz Augusto von Dentz return err; 49622bd1b237SLuiz Augusto von Dentz 49632bd1b237SLuiz Augusto von Dentz err = hci_update_scan_sync(hdev); 49642bd1b237SLuiz Augusto von Dentz if (err) 49652bd1b237SLuiz Augusto von Dentz return err; 49662bd1b237SLuiz Augusto von Dentz 49672bd1b237SLuiz Augusto von Dentz err = hci_update_class_sync(hdev); 49682bd1b237SLuiz Augusto von Dentz if (err) 49692bd1b237SLuiz Augusto von Dentz return err; 49702bd1b237SLuiz Augusto von Dentz } 49712bd1b237SLuiz Augusto von Dentz 49722bd1b237SLuiz Augusto von Dentz /* Advertising instances don't use the global discoverable setting, so 49732bd1b237SLuiz Augusto von Dentz * only update AD if advertising was enabled using Set Advertising. 49742bd1b237SLuiz Augusto von Dentz */ 49752bd1b237SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) { 49762bd1b237SLuiz Augusto von Dentz err = hci_update_adv_data_sync(hdev, 0x00); 49772bd1b237SLuiz Augusto von Dentz if (err) 49782bd1b237SLuiz Augusto von Dentz return err; 49792bd1b237SLuiz Augusto von Dentz 49802bd1b237SLuiz Augusto von Dentz /* Discoverable mode affects the local advertising 49812bd1b237SLuiz Augusto von Dentz * address in limited privacy mode. 49822bd1b237SLuiz Augusto von Dentz */ 49832bd1b237SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) { 49842bd1b237SLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 49852bd1b237SLuiz Augusto von Dentz err = hci_start_ext_adv_sync(hdev, 0x00); 49862bd1b237SLuiz Augusto von Dentz else 49872bd1b237SLuiz Augusto von Dentz err = hci_enable_advertising_sync(hdev); 49882bd1b237SLuiz Augusto von Dentz } 49892bd1b237SLuiz Augusto von Dentz } 49902bd1b237SLuiz Augusto von Dentz 49912bd1b237SLuiz Augusto von Dentz return err; 49922bd1b237SLuiz Augusto von Dentz } 49932bd1b237SLuiz Augusto von Dentz 49942bd1b237SLuiz Augusto von Dentz static int update_discoverable_sync(struct hci_dev *hdev, void *data) 49952bd1b237SLuiz Augusto von Dentz { 49962bd1b237SLuiz Augusto von Dentz return hci_update_discoverable_sync(hdev); 49972bd1b237SLuiz Augusto von Dentz } 49982bd1b237SLuiz Augusto von Dentz 49992bd1b237SLuiz Augusto von Dentz int hci_update_discoverable(struct hci_dev *hdev) 50002bd1b237SLuiz Augusto von Dentz { 50012bd1b237SLuiz Augusto von Dentz /* Only queue if it would have any effect */ 50022bd1b237SLuiz Augusto von Dentz if (hdev_is_powered(hdev) && 50032bd1b237SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_ADVERTISING) && 50042bd1b237SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && 50052bd1b237SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) 50062bd1b237SLuiz Augusto von Dentz return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL, 50072bd1b237SLuiz Augusto von Dentz NULL); 50082bd1b237SLuiz Augusto von Dentz 50092bd1b237SLuiz Augusto von Dentz return 0; 50102bd1b237SLuiz Augusto von Dentz } 50112bd1b237SLuiz Augusto von Dentz 5012f056a657SLuiz Augusto von Dentz int hci_update_connectable_sync(struct hci_dev *hdev) 5013f056a657SLuiz Augusto von Dentz { 5014f056a657SLuiz Augusto von Dentz int err; 5015f056a657SLuiz Augusto von Dentz 5016f056a657SLuiz Augusto von Dentz err = hci_update_scan_sync(hdev); 5017f056a657SLuiz Augusto von Dentz if (err) 5018f056a657SLuiz Augusto von Dentz return err; 5019f056a657SLuiz Augusto von Dentz 5020f056a657SLuiz Augusto von Dentz /* If BR/EDR is not enabled and we disable advertising as a 5021f056a657SLuiz Augusto von Dentz * by-product of disabling connectable, we need to update the 5022f056a657SLuiz Augusto von Dentz * advertising flags. 5023f056a657SLuiz Augusto von Dentz */ 5024f056a657SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 5025f056a657SLuiz Augusto von Dentz err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance); 5026f056a657SLuiz Augusto von Dentz 5027f056a657SLuiz Augusto von Dentz /* Update the advertising parameters if necessary */ 5028f056a657SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || 5029f056a657SLuiz Augusto von Dentz !list_empty(&hdev->adv_instances)) { 5030f056a657SLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 5031f056a657SLuiz Augusto von Dentz err = hci_start_ext_adv_sync(hdev, 5032f056a657SLuiz Augusto von Dentz hdev->cur_adv_instance); 5033f056a657SLuiz Augusto von Dentz else 5034f056a657SLuiz Augusto von Dentz err = hci_enable_advertising_sync(hdev); 5035f056a657SLuiz Augusto von Dentz 5036f056a657SLuiz Augusto von Dentz if (err) 5037f056a657SLuiz Augusto von Dentz return err; 5038f056a657SLuiz Augusto von Dentz } 5039f056a657SLuiz Augusto von Dentz 5040f056a657SLuiz Augusto von Dentz return hci_update_passive_scan_sync(hdev); 5041f056a657SLuiz Augusto von Dentz } 5042f056a657SLuiz Augusto von Dentz 5043abfeea47SLuiz Augusto von Dentz static int hci_inquiry_sync(struct hci_dev *hdev, u8 length) 5044abfeea47SLuiz Augusto von Dentz { 5045abfeea47SLuiz Augusto von Dentz const u8 giac[3] = { 0x33, 0x8b, 0x9e }; 5046abfeea47SLuiz Augusto von Dentz const u8 liac[3] = { 0x00, 0x8b, 0x9e }; 5047abfeea47SLuiz Augusto von Dentz struct hci_cp_inquiry cp; 5048abfeea47SLuiz Augusto von Dentz 5049abfeea47SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 5050abfeea47SLuiz Augusto von Dentz 5051abfeea47SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_INQUIRY)) 5052abfeea47SLuiz Augusto von Dentz return 0; 5053abfeea47SLuiz Augusto von Dentz 5054abfeea47SLuiz Augusto von Dentz hci_dev_lock(hdev); 5055abfeea47SLuiz Augusto von Dentz hci_inquiry_cache_flush(hdev); 5056abfeea47SLuiz Augusto von Dentz hci_dev_unlock(hdev); 5057abfeea47SLuiz Augusto von Dentz 5058abfeea47SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 5059abfeea47SLuiz Augusto von Dentz 5060abfeea47SLuiz Augusto von Dentz if (hdev->discovery.limited) 5061abfeea47SLuiz Augusto von Dentz memcpy(&cp.lap, liac, sizeof(cp.lap)); 5062abfeea47SLuiz Augusto von Dentz else 5063abfeea47SLuiz Augusto von Dentz memcpy(&cp.lap, giac, sizeof(cp.lap)); 5064abfeea47SLuiz Augusto von Dentz 5065abfeea47SLuiz Augusto von Dentz cp.length = length; 5066abfeea47SLuiz Augusto von Dentz 5067abfeea47SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY, 5068abfeea47SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 5069abfeea47SLuiz Augusto von Dentz } 5070abfeea47SLuiz Augusto von Dentz 5071abfeea47SLuiz Augusto von Dentz static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval) 5072abfeea47SLuiz Augusto von Dentz { 5073abfeea47SLuiz Augusto von Dentz u8 own_addr_type; 5074abfeea47SLuiz Augusto von Dentz /* Accept list is not used for discovery */ 5075abfeea47SLuiz Augusto von Dentz u8 filter_policy = 0x00; 5076abfeea47SLuiz Augusto von Dentz /* Default is to enable duplicates filter */ 5077abfeea47SLuiz Augusto von Dentz u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE; 5078abfeea47SLuiz Augusto von Dentz int err; 5079abfeea47SLuiz Augusto von Dentz 5080abfeea47SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 5081abfeea47SLuiz Augusto von Dentz 5082abfeea47SLuiz Augusto von Dentz /* If controller is scanning, it means the passive scanning is 5083abfeea47SLuiz Augusto von Dentz * running. Thus, we should temporarily stop it in order to set the 5084abfeea47SLuiz Augusto von Dentz * discovery scanning parameters. 5085abfeea47SLuiz Augusto von Dentz */ 5086abfeea47SLuiz Augusto von Dentz err = hci_scan_disable_sync(hdev); 5087abfeea47SLuiz Augusto von Dentz if (err) { 5088abfeea47SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to disable scanning: %d", err); 5089abfeea47SLuiz Augusto von Dentz return err; 5090abfeea47SLuiz Augusto von Dentz } 5091abfeea47SLuiz Augusto von Dentz 5092abfeea47SLuiz Augusto von Dentz cancel_interleave_scan(hdev); 5093abfeea47SLuiz Augusto von Dentz 5094abfeea47SLuiz Augusto von Dentz /* Pause advertising since active scanning disables address resolution 5095abfeea47SLuiz Augusto von Dentz * which advertising depend on in order to generate its RPAs. 5096abfeea47SLuiz Augusto von Dentz */ 5097abfeea47SLuiz Augusto von Dentz if (use_ll_privacy(hdev)) { 5098abfeea47SLuiz Augusto von Dentz err = hci_pause_advertising_sync(hdev); 5099abfeea47SLuiz Augusto von Dentz if (err) { 5100abfeea47SLuiz Augusto von Dentz bt_dev_err(hdev, "pause advertising failed: %d", err); 5101abfeea47SLuiz Augusto von Dentz goto failed; 5102abfeea47SLuiz Augusto von Dentz } 5103abfeea47SLuiz Augusto von Dentz } 5104abfeea47SLuiz Augusto von Dentz 5105abfeea47SLuiz Augusto von Dentz /* Disable address resolution while doing active scanning since the 5106abfeea47SLuiz Augusto von Dentz * accept list shall not be used and all reports shall reach the host 5107abfeea47SLuiz Augusto von Dentz * anyway. 5108abfeea47SLuiz Augusto von Dentz */ 5109abfeea47SLuiz Augusto von Dentz err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00); 5110abfeea47SLuiz Augusto von Dentz if (err) { 5111abfeea47SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to disable Address Resolution: %d", 5112abfeea47SLuiz Augusto von Dentz err); 5113abfeea47SLuiz Augusto von Dentz goto failed; 5114abfeea47SLuiz Augusto von Dentz } 5115abfeea47SLuiz Augusto von Dentz 5116abfeea47SLuiz Augusto von Dentz /* All active scans will be done with either a resolvable private 5117abfeea47SLuiz Augusto von Dentz * address (when privacy feature has been enabled) or non-resolvable 5118abfeea47SLuiz Augusto von Dentz * private address. 5119abfeea47SLuiz Augusto von Dentz */ 5120abfeea47SLuiz Augusto von Dentz err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev), 5121abfeea47SLuiz Augusto von Dentz &own_addr_type); 5122abfeea47SLuiz Augusto von Dentz if (err < 0) 5123abfeea47SLuiz Augusto von Dentz own_addr_type = ADDR_LE_DEV_PUBLIC; 5124abfeea47SLuiz Augusto von Dentz 5125abfeea47SLuiz Augusto von Dentz if (hci_is_adv_monitoring(hdev)) { 5126abfeea47SLuiz Augusto von Dentz /* Duplicate filter should be disabled when some advertisement 5127abfeea47SLuiz Augusto von Dentz * monitor is activated, otherwise AdvMon can only receive one 5128abfeea47SLuiz Augusto von Dentz * advertisement for one peer(*) during active scanning, and 5129abfeea47SLuiz Augusto von Dentz * might report loss to these peers. 5130abfeea47SLuiz Augusto von Dentz * 5131abfeea47SLuiz Augusto von Dentz * Note that different controllers have different meanings of 5132abfeea47SLuiz Augusto von Dentz * |duplicate|. Some of them consider packets with the same 5133abfeea47SLuiz Augusto von Dentz * address as duplicate, and others consider packets with the 5134abfeea47SLuiz Augusto von Dentz * same address and the same RSSI as duplicate. Although in the 5135abfeea47SLuiz Augusto von Dentz * latter case we don't need to disable duplicate filter, but 5136abfeea47SLuiz Augusto von Dentz * it is common to have active scanning for a short period of 5137abfeea47SLuiz Augusto von Dentz * time, the power impact should be neglectable. 5138abfeea47SLuiz Augusto von Dentz */ 5139abfeea47SLuiz Augusto von Dentz filter_dup = LE_SCAN_FILTER_DUP_DISABLE; 5140abfeea47SLuiz Augusto von Dentz } 5141abfeea47SLuiz Augusto von Dentz 5142abfeea47SLuiz Augusto von Dentz err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval, 5143abfeea47SLuiz Augusto von Dentz hdev->le_scan_window_discovery, 5144abfeea47SLuiz Augusto von Dentz own_addr_type, filter_policy, filter_dup); 5145abfeea47SLuiz Augusto von Dentz if (!err) 5146abfeea47SLuiz Augusto von Dentz return err; 5147abfeea47SLuiz Augusto von Dentz 5148abfeea47SLuiz Augusto von Dentz failed: 5149abfeea47SLuiz Augusto von Dentz /* Resume advertising if it was paused */ 5150abfeea47SLuiz Augusto von Dentz if (use_ll_privacy(hdev)) 5151abfeea47SLuiz Augusto von Dentz hci_resume_advertising_sync(hdev); 5152abfeea47SLuiz Augusto von Dentz 5153abfeea47SLuiz Augusto von Dentz /* Resume passive scanning */ 5154abfeea47SLuiz Augusto von Dentz hci_update_passive_scan_sync(hdev); 5155abfeea47SLuiz Augusto von Dentz return err; 5156abfeea47SLuiz Augusto von Dentz } 5157abfeea47SLuiz Augusto von Dentz 5158abfeea47SLuiz Augusto von Dentz static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev) 5159abfeea47SLuiz Augusto von Dentz { 5160abfeea47SLuiz Augusto von Dentz int err; 5161abfeea47SLuiz Augusto von Dentz 5162abfeea47SLuiz Augusto von Dentz bt_dev_dbg(hdev, ""); 5163abfeea47SLuiz Augusto von Dentz 5164abfeea47SLuiz Augusto von Dentz err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2); 5165abfeea47SLuiz Augusto von Dentz if (err) 5166abfeea47SLuiz Augusto von Dentz return err; 5167abfeea47SLuiz Augusto von Dentz 5168abfeea47SLuiz Augusto von Dentz return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN); 5169abfeea47SLuiz Augusto von Dentz } 5170abfeea47SLuiz Augusto von Dentz 5171abfeea47SLuiz Augusto von Dentz int hci_start_discovery_sync(struct hci_dev *hdev) 5172abfeea47SLuiz Augusto von Dentz { 5173abfeea47SLuiz Augusto von Dentz unsigned long timeout; 5174abfeea47SLuiz Augusto von Dentz int err; 5175abfeea47SLuiz Augusto von Dentz 5176abfeea47SLuiz Augusto von Dentz bt_dev_dbg(hdev, "type %u", hdev->discovery.type); 5177abfeea47SLuiz Augusto von Dentz 5178abfeea47SLuiz Augusto von Dentz switch (hdev->discovery.type) { 5179abfeea47SLuiz Augusto von Dentz case DISCOV_TYPE_BREDR: 5180abfeea47SLuiz Augusto von Dentz return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN); 5181abfeea47SLuiz Augusto von Dentz case DISCOV_TYPE_INTERLEAVED: 5182abfeea47SLuiz Augusto von Dentz /* When running simultaneous discovery, the LE scanning time 5183abfeea47SLuiz Augusto von Dentz * should occupy the whole discovery time sine BR/EDR inquiry 5184abfeea47SLuiz Augusto von Dentz * and LE scanning are scheduled by the controller. 5185abfeea47SLuiz Augusto von Dentz * 5186abfeea47SLuiz Augusto von Dentz * For interleaving discovery in comparison, BR/EDR inquiry 5187abfeea47SLuiz Augusto von Dentz * and LE scanning are done sequentially with separate 5188abfeea47SLuiz Augusto von Dentz * timeouts. 5189abfeea47SLuiz Augusto von Dentz */ 5190abfeea47SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, 5191abfeea47SLuiz Augusto von Dentz &hdev->quirks)) { 5192abfeea47SLuiz Augusto von Dentz timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); 5193abfeea47SLuiz Augusto von Dentz /* During simultaneous discovery, we double LE scan 5194abfeea47SLuiz Augusto von Dentz * interval. We must leave some time for the controller 5195abfeea47SLuiz Augusto von Dentz * to do BR/EDR inquiry. 5196abfeea47SLuiz Augusto von Dentz */ 5197abfeea47SLuiz Augusto von Dentz err = hci_start_interleaved_discovery_sync(hdev); 5198abfeea47SLuiz Augusto von Dentz break; 5199abfeea47SLuiz Augusto von Dentz } 5200abfeea47SLuiz Augusto von Dentz 5201abfeea47SLuiz Augusto von Dentz timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); 5202abfeea47SLuiz Augusto von Dentz err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery); 5203abfeea47SLuiz Augusto von Dentz break; 5204abfeea47SLuiz Augusto von Dentz case DISCOV_TYPE_LE: 5205abfeea47SLuiz Augusto von Dentz timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); 5206abfeea47SLuiz Augusto von Dentz err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery); 5207abfeea47SLuiz Augusto von Dentz break; 5208abfeea47SLuiz Augusto von Dentz default: 5209abfeea47SLuiz Augusto von Dentz return -EINVAL; 5210abfeea47SLuiz Augusto von Dentz } 5211abfeea47SLuiz Augusto von Dentz 5212abfeea47SLuiz Augusto von Dentz if (err) 5213abfeea47SLuiz Augusto von Dentz return err; 5214abfeea47SLuiz Augusto von Dentz 5215abfeea47SLuiz Augusto von Dentz bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout)); 5216abfeea47SLuiz Augusto von Dentz 5217abfeea47SLuiz Augusto von Dentz /* When service discovery is used and the controller has a 5218abfeea47SLuiz Augusto von Dentz * strict duplicate filter, it is important to remember the 5219abfeea47SLuiz Augusto von Dentz * start and duration of the scan. This is required for 5220abfeea47SLuiz Augusto von Dentz * restarting scanning during the discovery phase. 5221abfeea47SLuiz Augusto von Dentz */ 5222abfeea47SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) && 5223abfeea47SLuiz Augusto von Dentz hdev->discovery.result_filtering) { 5224abfeea47SLuiz Augusto von Dentz hdev->discovery.scan_start = jiffies; 5225abfeea47SLuiz Augusto von Dentz hdev->discovery.scan_duration = timeout; 5226abfeea47SLuiz Augusto von Dentz } 5227abfeea47SLuiz Augusto von Dentz 5228abfeea47SLuiz Augusto von Dentz queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable, 5229abfeea47SLuiz Augusto von Dentz timeout); 5230abfeea47SLuiz Augusto von Dentz return 0; 5231abfeea47SLuiz Augusto von Dentz } 5232182ee45dSLuiz Augusto von Dentz 5233182ee45dSLuiz Augusto von Dentz static void hci_suspend_monitor_sync(struct hci_dev *hdev) 5234182ee45dSLuiz Augusto von Dentz { 5235182ee45dSLuiz Augusto von Dentz switch (hci_get_adv_monitor_offload_ext(hdev)) { 5236182ee45dSLuiz Augusto von Dentz case HCI_ADV_MONITOR_EXT_MSFT: 5237182ee45dSLuiz Augusto von Dentz msft_suspend_sync(hdev); 5238182ee45dSLuiz Augusto von Dentz break; 5239182ee45dSLuiz Augusto von Dentz default: 5240182ee45dSLuiz Augusto von Dentz return; 5241182ee45dSLuiz Augusto von Dentz } 5242182ee45dSLuiz Augusto von Dentz } 5243182ee45dSLuiz Augusto von Dentz 5244182ee45dSLuiz Augusto von Dentz /* This function disables discovery and mark it as paused */ 5245182ee45dSLuiz Augusto von Dentz static int hci_pause_discovery_sync(struct hci_dev *hdev) 5246182ee45dSLuiz Augusto von Dentz { 5247182ee45dSLuiz Augusto von Dentz int old_state = hdev->discovery.state; 5248182ee45dSLuiz Augusto von Dentz int err; 5249182ee45dSLuiz Augusto von Dentz 5250182ee45dSLuiz Augusto von Dentz /* If discovery already stopped/stopping/paused there nothing to do */ 5251182ee45dSLuiz Augusto von Dentz if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING || 5252182ee45dSLuiz Augusto von Dentz hdev->discovery_paused) 5253182ee45dSLuiz Augusto von Dentz return 0; 5254182ee45dSLuiz Augusto von Dentz 5255182ee45dSLuiz Augusto von Dentz hci_discovery_set_state(hdev, DISCOVERY_STOPPING); 5256182ee45dSLuiz Augusto von Dentz err = hci_stop_discovery_sync(hdev); 5257182ee45dSLuiz Augusto von Dentz if (err) 5258182ee45dSLuiz Augusto von Dentz return err; 5259182ee45dSLuiz Augusto von Dentz 5260182ee45dSLuiz Augusto von Dentz hdev->discovery_paused = true; 5261182ee45dSLuiz Augusto von Dentz hdev->discovery_old_state = old_state; 5262182ee45dSLuiz Augusto von Dentz hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 5263182ee45dSLuiz Augusto von Dentz 5264182ee45dSLuiz Augusto von Dentz return 0; 5265182ee45dSLuiz Augusto von Dentz } 5266182ee45dSLuiz Augusto von Dentz 5267182ee45dSLuiz Augusto von Dentz static int hci_update_event_filter_sync(struct hci_dev *hdev) 5268182ee45dSLuiz Augusto von Dentz { 5269182ee45dSLuiz Augusto von Dentz struct bdaddr_list_with_flags *b; 5270182ee45dSLuiz Augusto von Dentz u8 scan = SCAN_DISABLED; 5271182ee45dSLuiz Augusto von Dentz bool scanning = test_bit(HCI_PSCAN, &hdev->flags); 5272182ee45dSLuiz Augusto von Dentz int err; 5273182ee45dSLuiz Augusto von Dentz 5274182ee45dSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 5275182ee45dSLuiz Augusto von Dentz return 0; 5276182ee45dSLuiz Augusto von Dentz 52770eaecfb2SIsmael Ferreras Morezuelas /* Some fake CSR controllers lock up after setting this type of 52780eaecfb2SIsmael Ferreras Morezuelas * filter, so avoid sending the request altogether. 52790eaecfb2SIsmael Ferreras Morezuelas */ 52800eaecfb2SIsmael Ferreras Morezuelas if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks)) 52810eaecfb2SIsmael Ferreras Morezuelas return 0; 52820eaecfb2SIsmael Ferreras Morezuelas 5283182ee45dSLuiz Augusto von Dentz /* Always clear event filter when starting */ 5284182ee45dSLuiz Augusto von Dentz hci_clear_event_filter_sync(hdev); 5285182ee45dSLuiz Augusto von Dentz 5286182ee45dSLuiz Augusto von Dentz list_for_each_entry(b, &hdev->accept_list, list) { 5287e1cff700SLinus Torvalds if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) 5288182ee45dSLuiz Augusto von Dentz continue; 5289182ee45dSLuiz Augusto von Dentz 5290182ee45dSLuiz Augusto von Dentz bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr); 5291182ee45dSLuiz Augusto von Dentz 5292182ee45dSLuiz Augusto von Dentz err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP, 5293182ee45dSLuiz Augusto von Dentz HCI_CONN_SETUP_ALLOW_BDADDR, 5294182ee45dSLuiz Augusto von Dentz &b->bdaddr, 5295182ee45dSLuiz Augusto von Dentz HCI_CONN_SETUP_AUTO_ON); 5296182ee45dSLuiz Augusto von Dentz if (err) 5297182ee45dSLuiz Augusto von Dentz bt_dev_dbg(hdev, "Failed to set event filter for %pMR", 5298182ee45dSLuiz Augusto von Dentz &b->bdaddr); 5299182ee45dSLuiz Augusto von Dentz else 5300182ee45dSLuiz Augusto von Dentz scan = SCAN_PAGE; 5301182ee45dSLuiz Augusto von Dentz } 5302182ee45dSLuiz Augusto von Dentz 5303182ee45dSLuiz Augusto von Dentz if (scan && !scanning) 5304182ee45dSLuiz Augusto von Dentz hci_write_scan_enable_sync(hdev, scan); 5305182ee45dSLuiz Augusto von Dentz else if (!scan && scanning) 5306182ee45dSLuiz Augusto von Dentz hci_write_scan_enable_sync(hdev, scan); 5307182ee45dSLuiz Augusto von Dentz 5308182ee45dSLuiz Augusto von Dentz return 0; 5309182ee45dSLuiz Augusto von Dentz } 5310182ee45dSLuiz Augusto von Dentz 53113b420553SLuiz Augusto von Dentz /* This function disables scan (BR and LE) and mark it as paused */ 53123b420553SLuiz Augusto von Dentz static int hci_pause_scan_sync(struct hci_dev *hdev) 53133b420553SLuiz Augusto von Dentz { 53143b420553SLuiz Augusto von Dentz if (hdev->scanning_paused) 53153b420553SLuiz Augusto von Dentz return 0; 53163b420553SLuiz Augusto von Dentz 53173b420553SLuiz Augusto von Dentz /* Disable page scan if enabled */ 53183b420553SLuiz Augusto von Dentz if (test_bit(HCI_PSCAN, &hdev->flags)) 53193b420553SLuiz Augusto von Dentz hci_write_scan_enable_sync(hdev, SCAN_DISABLED); 53203b420553SLuiz Augusto von Dentz 53213b420553SLuiz Augusto von Dentz hci_scan_disable_sync(hdev); 53223b420553SLuiz Augusto von Dentz 53233b420553SLuiz Augusto von Dentz hdev->scanning_paused = true; 53243b420553SLuiz Augusto von Dentz 53253b420553SLuiz Augusto von Dentz return 0; 53263b420553SLuiz Augusto von Dentz } 53273b420553SLuiz Augusto von Dentz 5328182ee45dSLuiz Augusto von Dentz /* This function performs the HCI suspend procedures in the follow order: 5329182ee45dSLuiz Augusto von Dentz * 5330182ee45dSLuiz Augusto von Dentz * Pause discovery (active scanning/inquiry) 5331182ee45dSLuiz Augusto von Dentz * Pause Directed Advertising/Advertising 53323b420553SLuiz Augusto von Dentz * Pause Scanning (passive scanning in case discovery was not active) 5333182ee45dSLuiz Augusto von Dentz * Disconnect all connections 5334182ee45dSLuiz Augusto von Dentz * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup 5335182ee45dSLuiz Augusto von Dentz * otherwise: 5336182ee45dSLuiz Augusto von Dentz * Update event mask (only set events that are allowed to wake up the host) 5337182ee45dSLuiz Augusto von Dentz * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP) 5338182ee45dSLuiz Augusto von Dentz * Update passive scanning (lower duty cycle) 5339182ee45dSLuiz Augusto von Dentz * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE 5340182ee45dSLuiz Augusto von Dentz */ 5341182ee45dSLuiz Augusto von Dentz int hci_suspend_sync(struct hci_dev *hdev) 5342182ee45dSLuiz Augusto von Dentz { 5343182ee45dSLuiz Augusto von Dentz int err; 5344182ee45dSLuiz Augusto von Dentz 5345182ee45dSLuiz Augusto von Dentz /* If marked as suspended there nothing to do */ 5346182ee45dSLuiz Augusto von Dentz if (hdev->suspended) 5347182ee45dSLuiz Augusto von Dentz return 0; 5348182ee45dSLuiz Augusto von Dentz 5349182ee45dSLuiz Augusto von Dentz /* Mark device as suspended */ 5350182ee45dSLuiz Augusto von Dentz hdev->suspended = true; 5351182ee45dSLuiz Augusto von Dentz 5352182ee45dSLuiz Augusto von Dentz /* Pause discovery if not already stopped */ 5353182ee45dSLuiz Augusto von Dentz hci_pause_discovery_sync(hdev); 5354182ee45dSLuiz Augusto von Dentz 5355182ee45dSLuiz Augusto von Dentz /* Pause other advertisements */ 5356182ee45dSLuiz Augusto von Dentz hci_pause_advertising_sync(hdev); 5357182ee45dSLuiz Augusto von Dentz 5358182ee45dSLuiz Augusto von Dentz /* Suspend monitor filters */ 5359182ee45dSLuiz Augusto von Dentz hci_suspend_monitor_sync(hdev); 5360182ee45dSLuiz Augusto von Dentz 5361182ee45dSLuiz Augusto von Dentz /* Prevent disconnects from causing scanning to be re-enabled */ 53623b420553SLuiz Augusto von Dentz hci_pause_scan_sync(hdev); 5363182ee45dSLuiz Augusto von Dentz 5364182ee45dSLuiz Augusto von Dentz /* Soft disconnect everything (power off) */ 5365182ee45dSLuiz Augusto von Dentz err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF); 5366182ee45dSLuiz Augusto von Dentz if (err) { 5367182ee45dSLuiz Augusto von Dentz /* Set state to BT_RUNNING so resume doesn't notify */ 5368182ee45dSLuiz Augusto von Dentz hdev->suspend_state = BT_RUNNING; 5369182ee45dSLuiz Augusto von Dentz hci_resume_sync(hdev); 5370182ee45dSLuiz Augusto von Dentz return err; 5371182ee45dSLuiz Augusto von Dentz } 5372182ee45dSLuiz Augusto von Dentz 5373ef61b6eaSAbhishek Pandit-Subedi /* Update event mask so only the allowed event can wakeup the host */ 5374ef61b6eaSAbhishek Pandit-Subedi hci_set_event_mask_sync(hdev); 5375ef61b6eaSAbhishek Pandit-Subedi 5376182ee45dSLuiz Augusto von Dentz /* Only configure accept list if disconnect succeeded and wake 5377182ee45dSLuiz Augusto von Dentz * isn't being prevented. 5378182ee45dSLuiz Augusto von Dentz */ 5379182ee45dSLuiz Augusto von Dentz if (!hdev->wakeup || !hdev->wakeup(hdev)) { 5380182ee45dSLuiz Augusto von Dentz hdev->suspend_state = BT_SUSPEND_DISCONNECT; 5381182ee45dSLuiz Augusto von Dentz return 0; 5382182ee45dSLuiz Augusto von Dentz } 5383182ee45dSLuiz Augusto von Dentz 5384182ee45dSLuiz Augusto von Dentz /* Unpause to take care of updating scanning params */ 5385182ee45dSLuiz Augusto von Dentz hdev->scanning_paused = false; 5386182ee45dSLuiz Augusto von Dentz 5387182ee45dSLuiz Augusto von Dentz /* Enable event filter for paired devices */ 5388182ee45dSLuiz Augusto von Dentz hci_update_event_filter_sync(hdev); 5389182ee45dSLuiz Augusto von Dentz 5390182ee45dSLuiz Augusto von Dentz /* Update LE passive scan if enabled */ 5391182ee45dSLuiz Augusto von Dentz hci_update_passive_scan_sync(hdev); 5392182ee45dSLuiz Augusto von Dentz 5393182ee45dSLuiz Augusto von Dentz /* Pause scan changes again. */ 5394182ee45dSLuiz Augusto von Dentz hdev->scanning_paused = true; 5395182ee45dSLuiz Augusto von Dentz 5396182ee45dSLuiz Augusto von Dentz hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE; 5397182ee45dSLuiz Augusto von Dentz 5398182ee45dSLuiz Augusto von Dentz return 0; 5399182ee45dSLuiz Augusto von Dentz } 5400182ee45dSLuiz Augusto von Dentz 5401182ee45dSLuiz Augusto von Dentz /* This function resumes discovery */ 5402182ee45dSLuiz Augusto von Dentz static int hci_resume_discovery_sync(struct hci_dev *hdev) 5403182ee45dSLuiz Augusto von Dentz { 5404182ee45dSLuiz Augusto von Dentz int err; 5405182ee45dSLuiz Augusto von Dentz 5406182ee45dSLuiz Augusto von Dentz /* If discovery not paused there nothing to do */ 5407182ee45dSLuiz Augusto von Dentz if (!hdev->discovery_paused) 5408182ee45dSLuiz Augusto von Dentz return 0; 5409182ee45dSLuiz Augusto von Dentz 5410182ee45dSLuiz Augusto von Dentz hdev->discovery_paused = false; 5411182ee45dSLuiz Augusto von Dentz 5412182ee45dSLuiz Augusto von Dentz hci_discovery_set_state(hdev, DISCOVERY_STARTING); 5413182ee45dSLuiz Augusto von Dentz 5414182ee45dSLuiz Augusto von Dentz err = hci_start_discovery_sync(hdev); 5415182ee45dSLuiz Augusto von Dentz 5416182ee45dSLuiz Augusto von Dentz hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED : 5417182ee45dSLuiz Augusto von Dentz DISCOVERY_FINDING); 5418182ee45dSLuiz Augusto von Dentz 5419182ee45dSLuiz Augusto von Dentz return err; 5420182ee45dSLuiz Augusto von Dentz } 5421182ee45dSLuiz Augusto von Dentz 5422182ee45dSLuiz Augusto von Dentz static void hci_resume_monitor_sync(struct hci_dev *hdev) 5423182ee45dSLuiz Augusto von Dentz { 5424182ee45dSLuiz Augusto von Dentz switch (hci_get_adv_monitor_offload_ext(hdev)) { 5425182ee45dSLuiz Augusto von Dentz case HCI_ADV_MONITOR_EXT_MSFT: 5426182ee45dSLuiz Augusto von Dentz msft_resume_sync(hdev); 5427182ee45dSLuiz Augusto von Dentz break; 5428182ee45dSLuiz Augusto von Dentz default: 5429182ee45dSLuiz Augusto von Dentz return; 5430182ee45dSLuiz Augusto von Dentz } 5431182ee45dSLuiz Augusto von Dentz } 5432182ee45dSLuiz Augusto von Dentz 54333b420553SLuiz Augusto von Dentz /* This function resume scan and reset paused flag */ 54343b420553SLuiz Augusto von Dentz static int hci_resume_scan_sync(struct hci_dev *hdev) 54353b420553SLuiz Augusto von Dentz { 54363b420553SLuiz Augusto von Dentz if (!hdev->scanning_paused) 54373b420553SLuiz Augusto von Dentz return 0; 54383b420553SLuiz Augusto von Dentz 543968253f3cSZhengping Jiang hdev->scanning_paused = false; 544068253f3cSZhengping Jiang 54413b420553SLuiz Augusto von Dentz hci_update_scan_sync(hdev); 54423b420553SLuiz Augusto von Dentz 54433b420553SLuiz Augusto von Dentz /* Reset passive scanning to normal */ 54443b420553SLuiz Augusto von Dentz hci_update_passive_scan_sync(hdev); 54453b420553SLuiz Augusto von Dentz 54463b420553SLuiz Augusto von Dentz return 0; 54473b420553SLuiz Augusto von Dentz } 54483b420553SLuiz Augusto von Dentz 5449182ee45dSLuiz Augusto von Dentz /* This function performs the HCI suspend procedures in the follow order: 5450182ee45dSLuiz Augusto von Dentz * 5451182ee45dSLuiz Augusto von Dentz * Restore event mask 5452182ee45dSLuiz Augusto von Dentz * Clear event filter 5453182ee45dSLuiz Augusto von Dentz * Update passive scanning (normal duty cycle) 5454182ee45dSLuiz Augusto von Dentz * Resume Directed Advertising/Advertising 5455182ee45dSLuiz Augusto von Dentz * Resume discovery (active scanning/inquiry) 5456182ee45dSLuiz Augusto von Dentz */ 5457182ee45dSLuiz Augusto von Dentz int hci_resume_sync(struct hci_dev *hdev) 5458182ee45dSLuiz Augusto von Dentz { 5459182ee45dSLuiz Augusto von Dentz /* If not marked as suspended there nothing to do */ 5460182ee45dSLuiz Augusto von Dentz if (!hdev->suspended) 5461182ee45dSLuiz Augusto von Dentz return 0; 5462182ee45dSLuiz Augusto von Dentz 5463182ee45dSLuiz Augusto von Dentz hdev->suspended = false; 5464182ee45dSLuiz Augusto von Dentz 5465182ee45dSLuiz Augusto von Dentz /* Restore event mask */ 5466182ee45dSLuiz Augusto von Dentz hci_set_event_mask_sync(hdev); 5467182ee45dSLuiz Augusto von Dentz 5468182ee45dSLuiz Augusto von Dentz /* Clear any event filters and restore scan state */ 5469182ee45dSLuiz Augusto von Dentz hci_clear_event_filter_sync(hdev); 5470182ee45dSLuiz Augusto von Dentz 54713b420553SLuiz Augusto von Dentz /* Resume scanning */ 54723b420553SLuiz Augusto von Dentz hci_resume_scan_sync(hdev); 5473182ee45dSLuiz Augusto von Dentz 5474182ee45dSLuiz Augusto von Dentz /* Resume monitor filters */ 5475182ee45dSLuiz Augusto von Dentz hci_resume_monitor_sync(hdev); 5476182ee45dSLuiz Augusto von Dentz 5477182ee45dSLuiz Augusto von Dentz /* Resume other advertisements */ 5478182ee45dSLuiz Augusto von Dentz hci_resume_advertising_sync(hdev); 5479182ee45dSLuiz Augusto von Dentz 5480182ee45dSLuiz Augusto von Dentz /* Resume discovery */ 5481182ee45dSLuiz Augusto von Dentz hci_resume_discovery_sync(hdev); 5482182ee45dSLuiz Augusto von Dentz 5483182ee45dSLuiz Augusto von Dentz return 0; 5484182ee45dSLuiz Augusto von Dentz } 54858e8b92eeSLuiz Augusto von Dentz 54868e8b92eeSLuiz Augusto von Dentz static bool conn_use_rpa(struct hci_conn *conn) 54878e8b92eeSLuiz Augusto von Dentz { 54888e8b92eeSLuiz Augusto von Dentz struct hci_dev *hdev = conn->hdev; 54898e8b92eeSLuiz Augusto von Dentz 54908e8b92eeSLuiz Augusto von Dentz return hci_dev_test_flag(hdev, HCI_PRIVACY); 54918e8b92eeSLuiz Augusto von Dentz } 54928e8b92eeSLuiz Augusto von Dentz 54938e8b92eeSLuiz Augusto von Dentz static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev, 54948e8b92eeSLuiz Augusto von Dentz struct hci_conn *conn) 54958e8b92eeSLuiz Augusto von Dentz { 54968e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_set_ext_adv_params cp; 54978e8b92eeSLuiz Augusto von Dentz int err; 54988e8b92eeSLuiz Augusto von Dentz bdaddr_t random_addr; 54998e8b92eeSLuiz Augusto von Dentz u8 own_addr_type; 55008e8b92eeSLuiz Augusto von Dentz 55018e8b92eeSLuiz Augusto von Dentz err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn), 55028e8b92eeSLuiz Augusto von Dentz &own_addr_type); 55038e8b92eeSLuiz Augusto von Dentz if (err) 55048e8b92eeSLuiz Augusto von Dentz return err; 55058e8b92eeSLuiz Augusto von Dentz 55068e8b92eeSLuiz Augusto von Dentz /* Set require_privacy to false so that the remote device has a 55078e8b92eeSLuiz Augusto von Dentz * chance of identifying us. 55088e8b92eeSLuiz Augusto von Dentz */ 55098e8b92eeSLuiz Augusto von Dentz err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL, 55108e8b92eeSLuiz Augusto von Dentz &own_addr_type, &random_addr); 55118e8b92eeSLuiz Augusto von Dentz if (err) 55128e8b92eeSLuiz Augusto von Dentz return err; 55138e8b92eeSLuiz Augusto von Dentz 55148e8b92eeSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 55158e8b92eeSLuiz Augusto von Dentz 55168e8b92eeSLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND); 55178e8b92eeSLuiz Augusto von Dentz cp.own_addr_type = own_addr_type; 55188e8b92eeSLuiz Augusto von Dentz cp.channel_map = hdev->le_adv_channel_map; 55198e8b92eeSLuiz Augusto von Dentz cp.tx_power = HCI_TX_POWER_INVALID; 55208e8b92eeSLuiz Augusto von Dentz cp.primary_phy = HCI_ADV_PHY_1M; 55218e8b92eeSLuiz Augusto von Dentz cp.secondary_phy = HCI_ADV_PHY_1M; 55228e8b92eeSLuiz Augusto von Dentz cp.handle = 0x00; /* Use instance 0 for directed adv */ 55238e8b92eeSLuiz Augusto von Dentz cp.own_addr_type = own_addr_type; 55248e8b92eeSLuiz Augusto von Dentz cp.peer_addr_type = conn->dst_type; 55258e8b92eeSLuiz Augusto von Dentz bacpy(&cp.peer_addr, &conn->dst); 55268e8b92eeSLuiz Augusto von Dentz 55278e8b92eeSLuiz Augusto von Dentz /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for 55288e8b92eeSLuiz Augusto von Dentz * advertising_event_property LE_LEGACY_ADV_DIRECT_IND 55298e8b92eeSLuiz Augusto von Dentz * does not supports advertising data when the advertising set already 55308e8b92eeSLuiz Augusto von Dentz * contains some, the controller shall return erroc code 'Invalid 55318e8b92eeSLuiz Augusto von Dentz * HCI Command Parameters(0x12). 55328e8b92eeSLuiz Augusto von Dentz * So it is required to remove adv set for handle 0x00. since we use 55338e8b92eeSLuiz Augusto von Dentz * instance 0 for directed adv. 55348e8b92eeSLuiz Augusto von Dentz */ 55358e8b92eeSLuiz Augusto von Dentz err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL); 55368e8b92eeSLuiz Augusto von Dentz if (err) 55378e8b92eeSLuiz Augusto von Dentz return err; 55388e8b92eeSLuiz Augusto von Dentz 55398e8b92eeSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, 55408e8b92eeSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 55418e8b92eeSLuiz Augusto von Dentz if (err) 55428e8b92eeSLuiz Augusto von Dentz return err; 55438e8b92eeSLuiz Augusto von Dentz 55448e8b92eeSLuiz Augusto von Dentz /* Check if random address need to be updated */ 55458e8b92eeSLuiz Augusto von Dentz if (own_addr_type == ADDR_LE_DEV_RANDOM && 55468e8b92eeSLuiz Augusto von Dentz bacmp(&random_addr, BDADDR_ANY) && 55478e8b92eeSLuiz Augusto von Dentz bacmp(&random_addr, &hdev->random_addr)) { 55488e8b92eeSLuiz Augusto von Dentz err = hci_set_adv_set_random_addr_sync(hdev, 0x00, 55498e8b92eeSLuiz Augusto von Dentz &random_addr); 55508e8b92eeSLuiz Augusto von Dentz if (err) 55518e8b92eeSLuiz Augusto von Dentz return err; 55528e8b92eeSLuiz Augusto von Dentz } 55538e8b92eeSLuiz Augusto von Dentz 55548e8b92eeSLuiz Augusto von Dentz return hci_enable_ext_advertising_sync(hdev, 0x00); 55558e8b92eeSLuiz Augusto von Dentz } 55568e8b92eeSLuiz Augusto von Dentz 55578e8b92eeSLuiz Augusto von Dentz static int hci_le_directed_advertising_sync(struct hci_dev *hdev, 55588e8b92eeSLuiz Augusto von Dentz struct hci_conn *conn) 55598e8b92eeSLuiz Augusto von Dentz { 55608e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_set_adv_param cp; 55618e8b92eeSLuiz Augusto von Dentz u8 status; 55628e8b92eeSLuiz Augusto von Dentz u8 own_addr_type; 55638e8b92eeSLuiz Augusto von Dentz u8 enable; 55648e8b92eeSLuiz Augusto von Dentz 55658e8b92eeSLuiz Augusto von Dentz if (ext_adv_capable(hdev)) 55668e8b92eeSLuiz Augusto von Dentz return hci_le_ext_directed_advertising_sync(hdev, conn); 55678e8b92eeSLuiz Augusto von Dentz 55688e8b92eeSLuiz Augusto von Dentz /* Clear the HCI_LE_ADV bit temporarily so that the 55698e8b92eeSLuiz Augusto von Dentz * hci_update_random_address knows that it's safe to go ahead 55708e8b92eeSLuiz Augusto von Dentz * and write a new random address. The flag will be set back on 55718e8b92eeSLuiz Augusto von Dentz * as soon as the SET_ADV_ENABLE HCI command completes. 55728e8b92eeSLuiz Augusto von Dentz */ 55738e8b92eeSLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_LE_ADV); 55748e8b92eeSLuiz Augusto von Dentz 55758e8b92eeSLuiz Augusto von Dentz /* Set require_privacy to false so that the remote device has a 55768e8b92eeSLuiz Augusto von Dentz * chance of identifying us. 55778e8b92eeSLuiz Augusto von Dentz */ 55788e8b92eeSLuiz Augusto von Dentz status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn), 55798e8b92eeSLuiz Augusto von Dentz &own_addr_type); 55808e8b92eeSLuiz Augusto von Dentz if (status) 55818e8b92eeSLuiz Augusto von Dentz return status; 55828e8b92eeSLuiz Augusto von Dentz 55838e8b92eeSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 55848e8b92eeSLuiz Augusto von Dentz 55858e8b92eeSLuiz Augusto von Dentz /* Some controllers might reject command if intervals are not 55868e8b92eeSLuiz Augusto von Dentz * within range for undirected advertising. 55878e8b92eeSLuiz Augusto von Dentz * BCM20702A0 is known to be affected by this. 55888e8b92eeSLuiz Augusto von Dentz */ 55898e8b92eeSLuiz Augusto von Dentz cp.min_interval = cpu_to_le16(0x0020); 55908e8b92eeSLuiz Augusto von Dentz cp.max_interval = cpu_to_le16(0x0020); 55918e8b92eeSLuiz Augusto von Dentz 55928e8b92eeSLuiz Augusto von Dentz cp.type = LE_ADV_DIRECT_IND; 55938e8b92eeSLuiz Augusto von Dentz cp.own_address_type = own_addr_type; 55948e8b92eeSLuiz Augusto von Dentz cp.direct_addr_type = conn->dst_type; 55958e8b92eeSLuiz Augusto von Dentz bacpy(&cp.direct_addr, &conn->dst); 55968e8b92eeSLuiz Augusto von Dentz cp.channel_map = hdev->le_adv_channel_map; 55978e8b92eeSLuiz Augusto von Dentz 55988e8b92eeSLuiz Augusto von Dentz status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM, 55998e8b92eeSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 56008e8b92eeSLuiz Augusto von Dentz if (status) 56018e8b92eeSLuiz Augusto von Dentz return status; 56028e8b92eeSLuiz Augusto von Dentz 56038e8b92eeSLuiz Augusto von Dentz enable = 0x01; 56048e8b92eeSLuiz Augusto von Dentz 56058e8b92eeSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, 56068e8b92eeSLuiz Augusto von Dentz sizeof(enable), &enable, HCI_CMD_TIMEOUT); 56078e8b92eeSLuiz Augusto von Dentz } 56088e8b92eeSLuiz Augusto von Dentz 56098e8b92eeSLuiz Augusto von Dentz static void set_ext_conn_params(struct hci_conn *conn, 56108e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_ext_conn_param *p) 56118e8b92eeSLuiz Augusto von Dentz { 56128e8b92eeSLuiz Augusto von Dentz struct hci_dev *hdev = conn->hdev; 56138e8b92eeSLuiz Augusto von Dentz 56148e8b92eeSLuiz Augusto von Dentz memset(p, 0, sizeof(*p)); 56158e8b92eeSLuiz Augusto von Dentz 56168e8b92eeSLuiz Augusto von Dentz p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect); 56178e8b92eeSLuiz Augusto von Dentz p->scan_window = cpu_to_le16(hdev->le_scan_window_connect); 56188e8b92eeSLuiz Augusto von Dentz p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval); 56198e8b92eeSLuiz Augusto von Dentz p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval); 56208e8b92eeSLuiz Augusto von Dentz p->conn_latency = cpu_to_le16(conn->le_conn_latency); 56218e8b92eeSLuiz Augusto von Dentz p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout); 56228e8b92eeSLuiz Augusto von Dentz p->min_ce_len = cpu_to_le16(0x0000); 56238e8b92eeSLuiz Augusto von Dentz p->max_ce_len = cpu_to_le16(0x0000); 56248e8b92eeSLuiz Augusto von Dentz } 56258e8b92eeSLuiz Augusto von Dentz 562689a0b8b9SLuiz Augusto von Dentz static int hci_le_ext_create_conn_sync(struct hci_dev *hdev, 562789a0b8b9SLuiz Augusto von Dentz struct hci_conn *conn, u8 own_addr_type) 56288e8b92eeSLuiz Augusto von Dentz { 56298e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_ext_create_conn *cp; 56308e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_ext_conn_param *p; 56318e8b92eeSLuiz Augusto von Dentz u8 data[sizeof(*cp) + sizeof(*p) * 3]; 56328e8b92eeSLuiz Augusto von Dentz u32 plen; 56338e8b92eeSLuiz Augusto von Dentz 56348e8b92eeSLuiz Augusto von Dentz cp = (void *)data; 56358e8b92eeSLuiz Augusto von Dentz p = (void *)cp->data; 56368e8b92eeSLuiz Augusto von Dentz 56378e8b92eeSLuiz Augusto von Dentz memset(cp, 0, sizeof(*cp)); 56388e8b92eeSLuiz Augusto von Dentz 56398e8b92eeSLuiz Augusto von Dentz bacpy(&cp->peer_addr, &conn->dst); 56408e8b92eeSLuiz Augusto von Dentz cp->peer_addr_type = conn->dst_type; 56418e8b92eeSLuiz Augusto von Dentz cp->own_addr_type = own_addr_type; 56428e8b92eeSLuiz Augusto von Dentz 56438e8b92eeSLuiz Augusto von Dentz plen = sizeof(*cp); 56448e8b92eeSLuiz Augusto von Dentz 56458e8b92eeSLuiz Augusto von Dentz if (scan_1m(hdev)) { 56468e8b92eeSLuiz Augusto von Dentz cp->phys |= LE_SCAN_PHY_1M; 56478e8b92eeSLuiz Augusto von Dentz set_ext_conn_params(conn, p); 56488e8b92eeSLuiz Augusto von Dentz 56498e8b92eeSLuiz Augusto von Dentz p++; 56508e8b92eeSLuiz Augusto von Dentz plen += sizeof(*p); 56518e8b92eeSLuiz Augusto von Dentz } 56528e8b92eeSLuiz Augusto von Dentz 56538e8b92eeSLuiz Augusto von Dentz if (scan_2m(hdev)) { 56548e8b92eeSLuiz Augusto von Dentz cp->phys |= LE_SCAN_PHY_2M; 56558e8b92eeSLuiz Augusto von Dentz set_ext_conn_params(conn, p); 56568e8b92eeSLuiz Augusto von Dentz 56578e8b92eeSLuiz Augusto von Dentz p++; 56588e8b92eeSLuiz Augusto von Dentz plen += sizeof(*p); 56598e8b92eeSLuiz Augusto von Dentz } 56608e8b92eeSLuiz Augusto von Dentz 56618e8b92eeSLuiz Augusto von Dentz if (scan_coded(hdev)) { 56628e8b92eeSLuiz Augusto von Dentz cp->phys |= LE_SCAN_PHY_CODED; 56638e8b92eeSLuiz Augusto von Dentz set_ext_conn_params(conn, p); 56648e8b92eeSLuiz Augusto von Dentz 56658e8b92eeSLuiz Augusto von Dentz plen += sizeof(*p); 56668e8b92eeSLuiz Augusto von Dentz } 56678e8b92eeSLuiz Augusto von Dentz 56686cd29ec6SLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN, 56696cd29ec6SLuiz Augusto von Dentz plen, data, 56706cd29ec6SLuiz Augusto von Dentz HCI_EV_LE_ENHANCED_CONN_COMPLETE, 5671a56a1138SLuiz Augusto von Dentz conn->conn_timeout, NULL); 56728e8b92eeSLuiz Augusto von Dentz } 56738e8b92eeSLuiz Augusto von Dentz 56748e8b92eeSLuiz Augusto von Dentz int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn) 56758e8b92eeSLuiz Augusto von Dentz { 56768e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_create_conn cp; 56778e8b92eeSLuiz Augusto von Dentz struct hci_conn_params *params; 56788e8b92eeSLuiz Augusto von Dentz u8 own_addr_type; 56798e8b92eeSLuiz Augusto von Dentz int err; 56808e8b92eeSLuiz Augusto von Dentz 56818e8b92eeSLuiz Augusto von Dentz /* If requested to connect as peripheral use directed advertising */ 56828e8b92eeSLuiz Augusto von Dentz if (conn->role == HCI_ROLE_SLAVE) { 568376d0685bSLuiz Augusto von Dentz /* If we're active scanning and simultaneous roles is not 568476d0685bSLuiz Augusto von Dentz * enabled simply reject the attempt. 56858e8b92eeSLuiz Augusto von Dentz */ 56868e8b92eeSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && 56874fc9857aSLuiz Augusto von Dentz hdev->le_scan_type == LE_SCAN_ACTIVE && 568876d0685bSLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) { 56898e8b92eeSLuiz Augusto von Dentz hci_conn_del(conn); 56908e8b92eeSLuiz Augusto von Dentz return -EBUSY; 56918e8b92eeSLuiz Augusto von Dentz } 56928e8b92eeSLuiz Augusto von Dentz 56934fc9857aSLuiz Augusto von Dentz /* Pause advertising while doing directed advertising. */ 56944fc9857aSLuiz Augusto von Dentz hci_pause_advertising_sync(hdev); 56954fc9857aSLuiz Augusto von Dentz 56968e8b92eeSLuiz Augusto von Dentz err = hci_le_directed_advertising_sync(hdev, conn); 56978e8b92eeSLuiz Augusto von Dentz goto done; 56988e8b92eeSLuiz Augusto von Dentz } 56998e8b92eeSLuiz Augusto von Dentz 570076d0685bSLuiz Augusto von Dentz /* Disable advertising if simultaneous roles is not in use. */ 570176d0685bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) 57024fc9857aSLuiz Augusto von Dentz hci_pause_advertising_sync(hdev); 57034fc9857aSLuiz Augusto von Dentz 57048e8b92eeSLuiz Augusto von Dentz params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); 57058e8b92eeSLuiz Augusto von Dentz if (params) { 57068e8b92eeSLuiz Augusto von Dentz conn->le_conn_min_interval = params->conn_min_interval; 57078e8b92eeSLuiz Augusto von Dentz conn->le_conn_max_interval = params->conn_max_interval; 57088e8b92eeSLuiz Augusto von Dentz conn->le_conn_latency = params->conn_latency; 57098e8b92eeSLuiz Augusto von Dentz conn->le_supv_timeout = params->supervision_timeout; 57108e8b92eeSLuiz Augusto von Dentz } else { 57118e8b92eeSLuiz Augusto von Dentz conn->le_conn_min_interval = hdev->le_conn_min_interval; 57128e8b92eeSLuiz Augusto von Dentz conn->le_conn_max_interval = hdev->le_conn_max_interval; 57138e8b92eeSLuiz Augusto von Dentz conn->le_conn_latency = hdev->le_conn_latency; 57148e8b92eeSLuiz Augusto von Dentz conn->le_supv_timeout = hdev->le_supv_timeout; 57158e8b92eeSLuiz Augusto von Dentz } 57168e8b92eeSLuiz Augusto von Dentz 57178e8b92eeSLuiz Augusto von Dentz /* If controller is scanning, we stop it since some controllers are 57188e8b92eeSLuiz Augusto von Dentz * not able to scan and connect at the same time. Also set the 57198e8b92eeSLuiz Augusto von Dentz * HCI_LE_SCAN_INTERRUPTED flag so that the command complete 57208e8b92eeSLuiz Augusto von Dentz * handler for scan disabling knows to set the correct discovery 57218e8b92eeSLuiz Augusto von Dentz * state. 57228e8b92eeSLuiz Augusto von Dentz */ 57238e8b92eeSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { 57248e8b92eeSLuiz Augusto von Dentz hci_scan_disable_sync(hdev); 57258e8b92eeSLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED); 57268e8b92eeSLuiz Augusto von Dentz } 57278e8b92eeSLuiz Augusto von Dentz 57288e8b92eeSLuiz Augusto von Dentz /* Update random address, but set require_privacy to false so 57298e8b92eeSLuiz Augusto von Dentz * that we never connect with an non-resolvable address. 57308e8b92eeSLuiz Augusto von Dentz */ 57318e8b92eeSLuiz Augusto von Dentz err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn), 57328e8b92eeSLuiz Augusto von Dentz &own_addr_type); 57338e8b92eeSLuiz Augusto von Dentz if (err) 57348e8b92eeSLuiz Augusto von Dentz goto done; 57358e8b92eeSLuiz Augusto von Dentz 57368e8b92eeSLuiz Augusto von Dentz if (use_ext_conn(hdev)) { 57378e8b92eeSLuiz Augusto von Dentz err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type); 57388e8b92eeSLuiz Augusto von Dentz goto done; 57398e8b92eeSLuiz Augusto von Dentz } 57408e8b92eeSLuiz Augusto von Dentz 57418e8b92eeSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 57428e8b92eeSLuiz Augusto von Dentz 57438e8b92eeSLuiz Augusto von Dentz cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect); 57448e8b92eeSLuiz Augusto von Dentz cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect); 57458e8b92eeSLuiz Augusto von Dentz 57468e8b92eeSLuiz Augusto von Dentz bacpy(&cp.peer_addr, &conn->dst); 57478e8b92eeSLuiz Augusto von Dentz cp.peer_addr_type = conn->dst_type; 57488e8b92eeSLuiz Augusto von Dentz cp.own_address_type = own_addr_type; 57498e8b92eeSLuiz Augusto von Dentz cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval); 57508e8b92eeSLuiz Augusto von Dentz cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval); 57518e8b92eeSLuiz Augusto von Dentz cp.conn_latency = cpu_to_le16(conn->le_conn_latency); 57528e8b92eeSLuiz Augusto von Dentz cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout); 57538e8b92eeSLuiz Augusto von Dentz cp.min_ce_len = cpu_to_le16(0x0000); 57548e8b92eeSLuiz Augusto von Dentz cp.max_ce_len = cpu_to_le16(0x0000); 57558e8b92eeSLuiz Augusto von Dentz 5756a56a1138SLuiz Augusto von Dentz /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261: 5757a56a1138SLuiz Augusto von Dentz * 5758a56a1138SLuiz Augusto von Dentz * If this event is unmasked and the HCI_LE_Connection_Complete event 5759a56a1138SLuiz Augusto von Dentz * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is 5760a56a1138SLuiz Augusto von Dentz * sent when a new connection has been created. 5761a56a1138SLuiz Augusto von Dentz */ 57626cd29ec6SLuiz Augusto von Dentz err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN, 5763a56a1138SLuiz Augusto von Dentz sizeof(cp), &cp, 5764a56a1138SLuiz Augusto von Dentz use_enhanced_conn_complete(hdev) ? 5765a56a1138SLuiz Augusto von Dentz HCI_EV_LE_ENHANCED_CONN_COMPLETE : 5766a56a1138SLuiz Augusto von Dentz HCI_EV_LE_CONN_COMPLETE, 5767a56a1138SLuiz Augusto von Dentz conn->conn_timeout, NULL); 57688e8b92eeSLuiz Augusto von Dentz 57698e8b92eeSLuiz Augusto von Dentz done: 57704fc9857aSLuiz Augusto von Dentz /* Re-enable advertising after the connection attempt is finished. */ 57718e8b92eeSLuiz Augusto von Dentz hci_resume_advertising_sync(hdev); 57728e8b92eeSLuiz Augusto von Dentz return err; 57738e8b92eeSLuiz Augusto von Dentz } 577426afbd82SLuiz Augusto von Dentz 577526afbd82SLuiz Augusto von Dentz int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle) 577626afbd82SLuiz Augusto von Dentz { 577726afbd82SLuiz Augusto von Dentz struct hci_cp_le_remove_cig cp; 577826afbd82SLuiz Augusto von Dentz 577926afbd82SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 578026afbd82SLuiz Augusto von Dentz cp.cig_id = handle; 578126afbd82SLuiz Augusto von Dentz 578226afbd82SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp), 578326afbd82SLuiz Augusto von Dentz &cp, HCI_CMD_TIMEOUT); 578426afbd82SLuiz Augusto von Dentz } 5785eca0ae4aSLuiz Augusto von Dentz 5786eca0ae4aSLuiz Augusto von Dentz int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle) 5787eca0ae4aSLuiz Augusto von Dentz { 5788eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_big_term_sync cp; 5789eca0ae4aSLuiz Augusto von Dentz 5790eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 5791eca0ae4aSLuiz Augusto von Dentz cp.handle = handle; 5792eca0ae4aSLuiz Augusto von Dentz 5793eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC, 5794eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 5795eca0ae4aSLuiz Augusto von Dentz } 5796eca0ae4aSLuiz Augusto von Dentz 5797eca0ae4aSLuiz Augusto von Dentz int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle) 5798eca0ae4aSLuiz Augusto von Dentz { 5799eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_pa_term_sync cp; 5800eca0ae4aSLuiz Augusto von Dentz 5801eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp)); 5802eca0ae4aSLuiz Augusto von Dentz cp.handle = cpu_to_le16(handle); 5803eca0ae4aSLuiz Augusto von Dentz 5804eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC, 5805eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT); 5806eca0ae4aSLuiz Augusto von Dentz } 5807