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
66a42e9bfSIulia Tanasescu * Copyright 2023 NXP
76a98e383SMarcel Holtmann */
86a98e383SMarcel Holtmann
9d0b13706SLuiz Augusto von Dentz #include <linux/property.h>
10d0b13706SLuiz Augusto von Dentz
116a98e383SMarcel Holtmann #include <net/bluetooth/bluetooth.h>
126a98e383SMarcel Holtmann #include <net/bluetooth/hci_core.h>
136a98e383SMarcel Holtmann #include <net/bluetooth/mgmt.h>
146a98e383SMarcel Holtmann
156a98e383SMarcel Holtmann #include "hci_request.h"
16828cea2bSChethan T N #include "hci_codec.h"
17d0b13706SLuiz Augusto von Dentz #include "hci_debugfs.h"
186a98e383SMarcel Holtmann #include "smp.h"
19161510ccSLuiz Augusto von Dentz #include "eir.h"
20d0b13706SLuiz Augusto von Dentz #include "msft.h"
21d0b13706SLuiz Augusto von Dentz #include "aosp.h"
22d0b13706SLuiz Augusto von Dentz #include "leds.h"
236a98e383SMarcel Holtmann
hci_cmd_sync_complete(struct hci_dev * hdev,u8 result,u16 opcode,struct sk_buff * skb)246a98e383SMarcel Holtmann static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
256a98e383SMarcel Holtmann struct sk_buff *skb)
266a98e383SMarcel Holtmann {
276a98e383SMarcel Holtmann bt_dev_dbg(hdev, "result 0x%2.2x", result);
286a98e383SMarcel Holtmann
296a98e383SMarcel Holtmann if (hdev->req_status != HCI_REQ_PEND)
306a98e383SMarcel Holtmann return;
316a98e383SMarcel Holtmann
326a98e383SMarcel Holtmann hdev->req_result = result;
336a98e383SMarcel Holtmann hdev->req_status = HCI_REQ_DONE;
346a98e383SMarcel Holtmann
352af7aa66SLuiz Augusto von Dentz /* Free the request command so it is not used as response */
362af7aa66SLuiz Augusto von Dentz kfree_skb(hdev->req_skb);
372af7aa66SLuiz Augusto von Dentz hdev->req_skb = NULL;
382af7aa66SLuiz Augusto von Dentz
39cba6b758SLuiz Augusto von Dentz if (skb) {
40cba6b758SLuiz Augusto von Dentz struct sock *sk = hci_skb_sk(skb);
41cba6b758SLuiz Augusto von Dentz
42cba6b758SLuiz Augusto von Dentz /* Drop sk reference if set */
43cba6b758SLuiz Augusto von Dentz if (sk)
44cba6b758SLuiz Augusto von Dentz sock_put(sk);
45cba6b758SLuiz Augusto von Dentz
462af7aa66SLuiz Augusto von Dentz hdev->req_rsp = skb_get(skb);
47cba6b758SLuiz Augusto von Dentz }
48cba6b758SLuiz Augusto von Dentz
496a98e383SMarcel Holtmann wake_up_interruptible(&hdev->req_wait_q);
506a98e383SMarcel Holtmann }
516a98e383SMarcel Holtmann
hci_cmd_sync_alloc(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,struct sock * sk)526a98e383SMarcel Holtmann static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
536a98e383SMarcel Holtmann u32 plen, const void *param,
546a98e383SMarcel Holtmann struct sock *sk)
556a98e383SMarcel Holtmann {
566a98e383SMarcel Holtmann int len = HCI_COMMAND_HDR_SIZE + plen;
576a98e383SMarcel Holtmann struct hci_command_hdr *hdr;
586a98e383SMarcel Holtmann struct sk_buff *skb;
596a98e383SMarcel Holtmann
606a98e383SMarcel Holtmann skb = bt_skb_alloc(len, GFP_ATOMIC);
616a98e383SMarcel Holtmann if (!skb)
626a98e383SMarcel Holtmann return NULL;
636a98e383SMarcel Holtmann
646a98e383SMarcel Holtmann hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
656a98e383SMarcel Holtmann hdr->opcode = cpu_to_le16(opcode);
666a98e383SMarcel Holtmann hdr->plen = plen;
676a98e383SMarcel Holtmann
686a98e383SMarcel Holtmann if (plen)
696a98e383SMarcel Holtmann skb_put_data(skb, param, plen);
706a98e383SMarcel Holtmann
716a98e383SMarcel Holtmann bt_dev_dbg(hdev, "skb len %d", skb->len);
726a98e383SMarcel Holtmann
736a98e383SMarcel Holtmann hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
746a98e383SMarcel Holtmann hci_skb_opcode(skb) = opcode;
756a98e383SMarcel Holtmann
76cba6b758SLuiz Augusto von Dentz /* Grab a reference if command needs to be associated with a sock (e.g.
77cba6b758SLuiz Augusto von Dentz * likely mgmt socket that initiated the command).
78cba6b758SLuiz Augusto von Dentz */
79cba6b758SLuiz Augusto von Dentz if (sk) {
80cba6b758SLuiz Augusto von Dentz hci_skb_sk(skb) = sk;
81cba6b758SLuiz Augusto von Dentz sock_hold(sk);
82cba6b758SLuiz Augusto von Dentz }
83cba6b758SLuiz Augusto von Dentz
846a98e383SMarcel Holtmann return skb;
856a98e383SMarcel Holtmann }
866a98e383SMarcel Holtmann
hci_cmd_sync_add(struct hci_request * req,u16 opcode,u32 plen,const void * param,u8 event,struct sock * sk)876a98e383SMarcel Holtmann static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
886a98e383SMarcel Holtmann const void *param, u8 event, struct sock *sk)
896a98e383SMarcel Holtmann {
906a98e383SMarcel Holtmann struct hci_dev *hdev = req->hdev;
916a98e383SMarcel Holtmann struct sk_buff *skb;
926a98e383SMarcel Holtmann
936a98e383SMarcel Holtmann bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
946a98e383SMarcel Holtmann
956a98e383SMarcel Holtmann /* If an error occurred during request building, there is no point in
966a98e383SMarcel Holtmann * queueing the HCI command. We can simply return.
976a98e383SMarcel Holtmann */
986a98e383SMarcel Holtmann if (req->err)
996a98e383SMarcel Holtmann return;
1006a98e383SMarcel Holtmann
1016a98e383SMarcel Holtmann skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
1026a98e383SMarcel Holtmann if (!skb) {
1036a98e383SMarcel Holtmann bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
1046a98e383SMarcel Holtmann opcode);
1056a98e383SMarcel Holtmann req->err = -ENOMEM;
1066a98e383SMarcel Holtmann return;
1076a98e383SMarcel Holtmann }
1086a98e383SMarcel Holtmann
1096a98e383SMarcel Holtmann if (skb_queue_empty(&req->cmd_q))
1106a98e383SMarcel Holtmann bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
1116a98e383SMarcel Holtmann
11285b56857SLuiz Augusto von Dentz hci_skb_event(skb) = event;
1136a98e383SMarcel Holtmann
1146a98e383SMarcel Holtmann skb_queue_tail(&req->cmd_q, skb);
1156a98e383SMarcel Holtmann }
1166a98e383SMarcel Holtmann
hci_req_sync_run(struct hci_request * req)117d56412eeSLuiz Augusto von Dentz static int hci_req_sync_run(struct hci_request *req)
1186a98e383SMarcel Holtmann {
1196a98e383SMarcel Holtmann struct hci_dev *hdev = req->hdev;
1206a98e383SMarcel Holtmann struct sk_buff *skb;
1216a98e383SMarcel Holtmann unsigned long flags;
1226a98e383SMarcel Holtmann
1236a98e383SMarcel Holtmann bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
1246a98e383SMarcel Holtmann
1256a98e383SMarcel Holtmann /* If an error occurred during request building, remove all HCI
1266a98e383SMarcel Holtmann * commands queued on the HCI request queue.
1276a98e383SMarcel Holtmann */
1286a98e383SMarcel Holtmann if (req->err) {
1296a98e383SMarcel Holtmann skb_queue_purge(&req->cmd_q);
1306a98e383SMarcel Holtmann return req->err;
1316a98e383SMarcel Holtmann }
1326a98e383SMarcel Holtmann
1336a98e383SMarcel Holtmann /* Do not allow empty requests */
1346a98e383SMarcel Holtmann if (skb_queue_empty(&req->cmd_q))
1356a98e383SMarcel Holtmann return -ENODATA;
1366a98e383SMarcel Holtmann
1376a98e383SMarcel Holtmann skb = skb_peek_tail(&req->cmd_q);
1386a98e383SMarcel Holtmann bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
1396a98e383SMarcel Holtmann bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
1406a98e383SMarcel Holtmann
1416a98e383SMarcel Holtmann spin_lock_irqsave(&hdev->cmd_q.lock, flags);
1426a98e383SMarcel Holtmann skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
1436a98e383SMarcel Holtmann spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
1446a98e383SMarcel Holtmann
1456a98e383SMarcel Holtmann queue_work(hdev->workqueue, &hdev->cmd_work);
1466a98e383SMarcel Holtmann
1476a98e383SMarcel Holtmann return 0;
1486a98e383SMarcel Holtmann }
1496a98e383SMarcel Holtmann
1506a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync_sk(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u8 event,u32 timeout,struct sock * sk)1516a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
1526a98e383SMarcel Holtmann const void *param, u8 event, u32 timeout,
1536a98e383SMarcel Holtmann struct sock *sk)
1546a98e383SMarcel Holtmann {
1556a98e383SMarcel Holtmann struct hci_request req;
1566a98e383SMarcel Holtmann struct sk_buff *skb;
1576a98e383SMarcel Holtmann int err = 0;
1586a98e383SMarcel Holtmann
159d4b8c082SMarcel Ziswiler bt_dev_dbg(hdev, "Opcode 0x%4.4x", opcode);
1606a98e383SMarcel Holtmann
1616a98e383SMarcel Holtmann hci_req_init(&req, hdev);
1626a98e383SMarcel Holtmann
1636a98e383SMarcel Holtmann hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
1646a98e383SMarcel Holtmann
1656a98e383SMarcel Holtmann hdev->req_status = HCI_REQ_PEND;
1666a98e383SMarcel Holtmann
167d56412eeSLuiz Augusto von Dentz err = hci_req_sync_run(&req);
1686a98e383SMarcel Holtmann if (err < 0)
1696a98e383SMarcel Holtmann return ERR_PTR(err);
1706a98e383SMarcel Holtmann
1716a98e383SMarcel Holtmann err = wait_event_interruptible_timeout(hdev->req_wait_q,
1726a98e383SMarcel Holtmann hdev->req_status != HCI_REQ_PEND,
1736a98e383SMarcel Holtmann timeout);
1746a98e383SMarcel Holtmann
1756a98e383SMarcel Holtmann if (err == -ERESTARTSYS)
1766a98e383SMarcel Holtmann return ERR_PTR(-EINTR);
1776a98e383SMarcel Holtmann
1786a98e383SMarcel Holtmann switch (hdev->req_status) {
1796a98e383SMarcel Holtmann case HCI_REQ_DONE:
1806a98e383SMarcel Holtmann err = -bt_to_errno(hdev->req_result);
1816a98e383SMarcel Holtmann break;
1826a98e383SMarcel Holtmann
1836a98e383SMarcel Holtmann case HCI_REQ_CANCELED:
1846a98e383SMarcel Holtmann err = -hdev->req_result;
1856a98e383SMarcel Holtmann break;
1866a98e383SMarcel Holtmann
1876a98e383SMarcel Holtmann default:
1886a98e383SMarcel Holtmann err = -ETIMEDOUT;
1896a98e383SMarcel Holtmann break;
1906a98e383SMarcel Holtmann }
1916a98e383SMarcel Holtmann
1926a98e383SMarcel Holtmann hdev->req_status = 0;
1936a98e383SMarcel Holtmann hdev->req_result = 0;
1942af7aa66SLuiz Augusto von Dentz skb = hdev->req_rsp;
1952af7aa66SLuiz Augusto von Dentz hdev->req_rsp = NULL;
1966a98e383SMarcel Holtmann
1976a98e383SMarcel Holtmann bt_dev_dbg(hdev, "end: err %d", err);
1986a98e383SMarcel Holtmann
1996a98e383SMarcel Holtmann if (err < 0) {
2006a98e383SMarcel Holtmann kfree_skb(skb);
2016a98e383SMarcel Holtmann return ERR_PTR(err);
2026a98e383SMarcel Holtmann }
2036a98e383SMarcel Holtmann
2041f176446SSungwoo Kim /* If command return a status event skb will be set to NULL as there are
2051f176446SSungwoo Kim * no parameters.
2061f176446SSungwoo Kim */
2071f176446SSungwoo Kim if (!skb)
2081f176446SSungwoo Kim return ERR_PTR(-ENODATA);
2091f176446SSungwoo Kim
2106a98e383SMarcel Holtmann return skb;
2116a98e383SMarcel Holtmann }
2126a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_sk);
2136a98e383SMarcel Holtmann
2146a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)2156a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
2166a98e383SMarcel Holtmann const void *param, u32 timeout)
2176a98e383SMarcel Holtmann {
2186a98e383SMarcel Holtmann return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
2196a98e383SMarcel Holtmann }
2206a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync);
2216a98e383SMarcel Holtmann
2226a98e383SMarcel Holtmann /* Send HCI command and wait for command complete event */
hci_cmd_sync(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)2236a98e383SMarcel Holtmann struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
2246a98e383SMarcel Holtmann const void *param, u32 timeout)
2256a98e383SMarcel Holtmann {
2266a98e383SMarcel Holtmann struct sk_buff *skb;
2276a98e383SMarcel Holtmann
2286a98e383SMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags))
2296a98e383SMarcel Holtmann return ERR_PTR(-ENETDOWN);
2306a98e383SMarcel Holtmann
2316a98e383SMarcel Holtmann bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
2326a98e383SMarcel Holtmann
2336a98e383SMarcel Holtmann hci_req_sync_lock(hdev);
2346a98e383SMarcel Holtmann skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
2356a98e383SMarcel Holtmann hci_req_sync_unlock(hdev);
2366a98e383SMarcel Holtmann
2376a98e383SMarcel Holtmann return skb;
2386a98e383SMarcel Holtmann }
2396a98e383SMarcel Holtmann EXPORT_SYMBOL(hci_cmd_sync);
2406a98e383SMarcel Holtmann
2416a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync_ev(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u8 event,u32 timeout)2426a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
2436a98e383SMarcel Holtmann const void *param, u8 event, u32 timeout)
2446a98e383SMarcel Holtmann {
2456a98e383SMarcel Holtmann return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
2466a98e383SMarcel Holtmann NULL);
2476a98e383SMarcel Holtmann }
2486a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_ev);
2496a98e383SMarcel Holtmann
2506a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync_status_sk(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u8 event,u32 timeout,struct sock * sk)2516a98e383SMarcel Holtmann int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
2526a98e383SMarcel Holtmann const void *param, u8 event, u32 timeout,
2536a98e383SMarcel Holtmann struct sock *sk)
2546a98e383SMarcel Holtmann {
2556a98e383SMarcel Holtmann struct sk_buff *skb;
2566a98e383SMarcel Holtmann u8 status;
2576a98e383SMarcel Holtmann
2586a98e383SMarcel Holtmann skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
2591f176446SSungwoo Kim
2601f176446SSungwoo Kim /* If command return a status event, skb will be set to -ENODATA */
2611f176446SSungwoo Kim if (skb == ERR_PTR(-ENODATA))
2621f176446SSungwoo Kim return 0;
2631f176446SSungwoo Kim
264abfeea47SLuiz Augusto von Dentz if (IS_ERR(skb)) {
265b62e7220SLuiz Augusto von Dentz if (!event)
266d4b8c082SMarcel Ziswiler bt_dev_err(hdev, "Opcode 0x%4.4x failed: %ld", opcode,
2676a98e383SMarcel Holtmann PTR_ERR(skb));
2686a98e383SMarcel Holtmann return PTR_ERR(skb);
2696a98e383SMarcel Holtmann }
2706a98e383SMarcel Holtmann
2716a98e383SMarcel Holtmann status = skb->data[0];
2726a98e383SMarcel Holtmann
2736a98e383SMarcel Holtmann kfree_skb(skb);
2746a98e383SMarcel Holtmann
2756a98e383SMarcel Holtmann return status;
2766a98e383SMarcel Holtmann }
2776a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
2786a98e383SMarcel Holtmann
__hci_cmd_sync_status(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)2796a98e383SMarcel Holtmann int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
2806a98e383SMarcel Holtmann const void *param, u32 timeout)
2816a98e383SMarcel Holtmann {
2826a98e383SMarcel Holtmann return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
2836a98e383SMarcel Holtmann NULL);
2846a98e383SMarcel Holtmann }
2856a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_status);
2866a98e383SMarcel Holtmann
hci_cmd_sync_status(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)287b0fc1bd2SLuiz Augusto von Dentz int hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
288b0fc1bd2SLuiz Augusto von Dentz const void *param, u32 timeout)
289b0fc1bd2SLuiz Augusto von Dentz {
290b0fc1bd2SLuiz Augusto von Dentz int err;
291b0fc1bd2SLuiz Augusto von Dentz
292b0fc1bd2SLuiz Augusto von Dentz hci_req_sync_lock(hdev);
293b0fc1bd2SLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, opcode, plen, param, timeout);
294b0fc1bd2SLuiz Augusto von Dentz hci_req_sync_unlock(hdev);
295b0fc1bd2SLuiz Augusto von Dentz
296b0fc1bd2SLuiz Augusto von Dentz return err;
297b0fc1bd2SLuiz Augusto von Dentz }
298b0fc1bd2SLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_status);
299b0fc1bd2SLuiz Augusto von Dentz
hci_cmd_sync_work(struct work_struct * work)3006a98e383SMarcel Holtmann static void hci_cmd_sync_work(struct work_struct *work)
3016a98e383SMarcel Holtmann {
3026a98e383SMarcel Holtmann struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
3036a98e383SMarcel Holtmann
3046a98e383SMarcel Holtmann bt_dev_dbg(hdev, "");
3056a98e383SMarcel Holtmann
306008ee9ebSLuiz Augusto von Dentz /* Dequeue all entries and run them */
307008ee9ebSLuiz Augusto von Dentz while (1) {
308008ee9ebSLuiz Augusto von Dentz struct hci_cmd_sync_work_entry *entry;
309008ee9ebSLuiz Augusto von Dentz
3106a98e383SMarcel Holtmann mutex_lock(&hdev->cmd_sync_work_lock);
311008ee9ebSLuiz Augusto von Dentz entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
312008ee9ebSLuiz Augusto von Dentz struct hci_cmd_sync_work_entry,
313008ee9ebSLuiz Augusto von Dentz list);
314008ee9ebSLuiz Augusto von Dentz if (entry)
3156a98e383SMarcel Holtmann list_del(&entry->list);
3166a98e383SMarcel Holtmann mutex_unlock(&hdev->cmd_sync_work_lock);
3176a98e383SMarcel Holtmann
318008ee9ebSLuiz Augusto von Dentz if (!entry)
319008ee9ebSLuiz Augusto von Dentz break;
320008ee9ebSLuiz Augusto von Dentz
321008ee9ebSLuiz Augusto von Dentz bt_dev_dbg(hdev, "entry %p", entry);
322008ee9ebSLuiz Augusto von Dentz
323008ee9ebSLuiz Augusto von Dentz if (entry->func) {
3246a98e383SMarcel Holtmann int err;
3256a98e383SMarcel Holtmann
3266a98e383SMarcel Holtmann hci_req_sync_lock(hdev);
327008ee9ebSLuiz Augusto von Dentz err = entry->func(hdev, entry->data);
328008ee9ebSLuiz Augusto von Dentz if (entry->destroy)
329008ee9ebSLuiz Augusto von Dentz entry->destroy(hdev, entry->data, err);
3306a98e383SMarcel Holtmann hci_req_sync_unlock(hdev);
3316a98e383SMarcel Holtmann }
332008ee9ebSLuiz Augusto von Dentz
333008ee9ebSLuiz Augusto von Dentz kfree(entry);
334008ee9ebSLuiz Augusto von Dentz }
3356a98e383SMarcel Holtmann }
3366a98e383SMarcel Holtmann
hci_cmd_sync_cancel_work(struct work_struct * work)337744451c1SBenjamin Berg static void hci_cmd_sync_cancel_work(struct work_struct *work)
338744451c1SBenjamin Berg {
339744451c1SBenjamin Berg struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
340744451c1SBenjamin Berg
341744451c1SBenjamin Berg cancel_delayed_work_sync(&hdev->cmd_timer);
342744451c1SBenjamin Berg cancel_delayed_work_sync(&hdev->ncmd_timer);
343744451c1SBenjamin Berg atomic_set(&hdev->cmd_cnt, 1);
344744451c1SBenjamin Berg
345744451c1SBenjamin Berg wake_up_interruptible(&hdev->req_wait_q);
346744451c1SBenjamin Berg }
347744451c1SBenjamin Berg
3488ffde2a7SBrian Gix static int hci_scan_disable_sync(struct hci_dev *hdev);
scan_disable_sync(struct hci_dev * hdev,void * data)3498ffde2a7SBrian Gix static int scan_disable_sync(struct hci_dev *hdev, void *data)
3508ffde2a7SBrian Gix {
3518ffde2a7SBrian Gix return hci_scan_disable_sync(hdev);
3528ffde2a7SBrian Gix }
3538ffde2a7SBrian Gix
3548ffde2a7SBrian Gix static int hci_inquiry_sync(struct hci_dev *hdev, u8 length);
interleaved_inquiry_sync(struct hci_dev * hdev,void * data)3558ffde2a7SBrian Gix static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data)
3568ffde2a7SBrian Gix {
3578ffde2a7SBrian Gix return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN);
3588ffde2a7SBrian Gix }
3598ffde2a7SBrian Gix
le_scan_disable(struct work_struct * work)3608ffde2a7SBrian Gix static void le_scan_disable(struct work_struct *work)
3618ffde2a7SBrian Gix {
3628ffde2a7SBrian Gix struct hci_dev *hdev = container_of(work, struct hci_dev,
3638ffde2a7SBrian Gix le_scan_disable.work);
3648ffde2a7SBrian Gix int status;
3658ffde2a7SBrian Gix
3668ffde2a7SBrian Gix bt_dev_dbg(hdev, "");
3678ffde2a7SBrian Gix hci_dev_lock(hdev);
3688ffde2a7SBrian Gix
3698ffde2a7SBrian Gix if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
3708ffde2a7SBrian Gix goto _return;
3718ffde2a7SBrian Gix
3728ffde2a7SBrian Gix cancel_delayed_work(&hdev->le_scan_restart);
3738ffde2a7SBrian Gix
3748ffde2a7SBrian Gix status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
3758ffde2a7SBrian Gix if (status) {
3768ffde2a7SBrian Gix bt_dev_err(hdev, "failed to disable LE scan: %d", status);
3778ffde2a7SBrian Gix goto _return;
3788ffde2a7SBrian Gix }
3798ffde2a7SBrian Gix
3808ffde2a7SBrian Gix hdev->discovery.scan_start = 0;
3818ffde2a7SBrian Gix
3828ffde2a7SBrian Gix /* If we were running LE only scan, change discovery state. If
3838ffde2a7SBrian Gix * we were running both LE and BR/EDR inquiry simultaneously,
3848ffde2a7SBrian Gix * and BR/EDR inquiry is already finished, stop discovery,
3858ffde2a7SBrian Gix * otherwise BR/EDR inquiry will stop discovery when finished.
3868ffde2a7SBrian Gix * If we will resolve remote device name, do not change
3878ffde2a7SBrian Gix * discovery state.
3888ffde2a7SBrian Gix */
3898ffde2a7SBrian Gix
3908ffde2a7SBrian Gix if (hdev->discovery.type == DISCOV_TYPE_LE)
3918ffde2a7SBrian Gix goto discov_stopped;
3928ffde2a7SBrian Gix
3938ffde2a7SBrian Gix if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
3948ffde2a7SBrian Gix goto _return;
3958ffde2a7SBrian Gix
3968ffde2a7SBrian Gix if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
3978ffde2a7SBrian Gix if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
3988ffde2a7SBrian Gix hdev->discovery.state != DISCOVERY_RESOLVING)
3998ffde2a7SBrian Gix goto discov_stopped;
4008ffde2a7SBrian Gix
4018ffde2a7SBrian Gix goto _return;
4028ffde2a7SBrian Gix }
4038ffde2a7SBrian Gix
4048ffde2a7SBrian Gix status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
4058ffde2a7SBrian Gix if (status) {
4068ffde2a7SBrian Gix bt_dev_err(hdev, "inquiry failed: status %d", status);
4078ffde2a7SBrian Gix goto discov_stopped;
4088ffde2a7SBrian Gix }
4098ffde2a7SBrian Gix
4108ffde2a7SBrian Gix goto _return;
4118ffde2a7SBrian Gix
4128ffde2a7SBrian Gix discov_stopped:
4138ffde2a7SBrian Gix hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4148ffde2a7SBrian Gix
4158ffde2a7SBrian Gix _return:
4168ffde2a7SBrian Gix hci_dev_unlock(hdev);
4178ffde2a7SBrian Gix }
4188ffde2a7SBrian Gix
41927d54b77SBrian Gix static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
42027d54b77SBrian Gix u8 filter_dup);
hci_le_scan_restart_sync(struct hci_dev * hdev)42127d54b77SBrian Gix static int hci_le_scan_restart_sync(struct hci_dev *hdev)
42227d54b77SBrian Gix {
42327d54b77SBrian Gix /* If controller is not scanning we are done. */
42427d54b77SBrian Gix if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
42527d54b77SBrian Gix return 0;
42627d54b77SBrian Gix
42727d54b77SBrian Gix if (hdev->scanning_paused) {
42827d54b77SBrian Gix bt_dev_dbg(hdev, "Scanning is paused for suspend");
42927d54b77SBrian Gix return 0;
43027d54b77SBrian Gix }
43127d54b77SBrian Gix
43227d54b77SBrian Gix hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
43327d54b77SBrian Gix return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE,
43427d54b77SBrian Gix LE_SCAN_FILTER_DUP_ENABLE);
43527d54b77SBrian Gix }
43627d54b77SBrian Gix
le_scan_restart(struct work_struct * work)43727d54b77SBrian Gix static void le_scan_restart(struct work_struct *work)
43827d54b77SBrian Gix {
43927d54b77SBrian Gix struct hci_dev *hdev = container_of(work, struct hci_dev,
44027d54b77SBrian Gix le_scan_restart.work);
44127d54b77SBrian Gix unsigned long timeout, duration, scan_start, now;
44227d54b77SBrian Gix int status;
44327d54b77SBrian Gix
44427d54b77SBrian Gix bt_dev_dbg(hdev, "");
44527d54b77SBrian Gix
446941c998bSLuiz Augusto von Dentz status = hci_le_scan_restart_sync(hdev);
44727d54b77SBrian Gix if (status) {
44827d54b77SBrian Gix bt_dev_err(hdev, "failed to restart LE scan: status %d",
44927d54b77SBrian Gix status);
450941c998bSLuiz Augusto von Dentz return;
45127d54b77SBrian Gix }
45227d54b77SBrian Gix
453941c998bSLuiz Augusto von Dentz hci_dev_lock(hdev);
454941c998bSLuiz Augusto von Dentz
45527d54b77SBrian Gix if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
45627d54b77SBrian Gix !hdev->discovery.scan_start)
45727d54b77SBrian Gix goto unlock;
45827d54b77SBrian Gix
45927d54b77SBrian Gix /* When the scan was started, hdev->le_scan_disable has been queued
46027d54b77SBrian Gix * after duration from scan_start. During scan restart this job
46127d54b77SBrian Gix * has been canceled, and we need to queue it again after proper
46227d54b77SBrian Gix * timeout, to make sure that scan does not run indefinitely.
46327d54b77SBrian Gix */
46427d54b77SBrian Gix duration = hdev->discovery.scan_duration;
46527d54b77SBrian Gix scan_start = hdev->discovery.scan_start;
46627d54b77SBrian Gix now = jiffies;
46727d54b77SBrian Gix if (now - scan_start <= duration) {
46827d54b77SBrian Gix int elapsed;
46927d54b77SBrian Gix
47027d54b77SBrian Gix if (now >= scan_start)
47127d54b77SBrian Gix elapsed = now - scan_start;
47227d54b77SBrian Gix else
47327d54b77SBrian Gix elapsed = ULONG_MAX - scan_start + now;
47427d54b77SBrian Gix
47527d54b77SBrian Gix timeout = duration - elapsed;
47627d54b77SBrian Gix } else {
47727d54b77SBrian Gix timeout = 0;
47827d54b77SBrian Gix }
47927d54b77SBrian Gix
48027d54b77SBrian Gix queue_delayed_work(hdev->req_workqueue,
48127d54b77SBrian Gix &hdev->le_scan_disable, timeout);
48227d54b77SBrian Gix
48327d54b77SBrian Gix unlock:
48427d54b77SBrian Gix hci_dev_unlock(hdev);
48527d54b77SBrian Gix }
48627d54b77SBrian Gix
reenable_adv_sync(struct hci_dev * hdev,void * data)487b338d917SBrian Gix static int reenable_adv_sync(struct hci_dev *hdev, void *data)
488b338d917SBrian Gix {
489b338d917SBrian Gix bt_dev_dbg(hdev, "");
490b338d917SBrian Gix
491b338d917SBrian Gix if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
492b338d917SBrian Gix list_empty(&hdev->adv_instances))
493b338d917SBrian Gix return 0;
494b338d917SBrian Gix
495b338d917SBrian Gix if (hdev->cur_adv_instance) {
496b338d917SBrian Gix return hci_schedule_adv_instance_sync(hdev,
497b338d917SBrian Gix hdev->cur_adv_instance,
498b338d917SBrian Gix true);
499b338d917SBrian Gix } else {
500b338d917SBrian Gix if (ext_adv_capable(hdev)) {
501b338d917SBrian Gix hci_start_ext_adv_sync(hdev, 0x00);
502b338d917SBrian Gix } else {
503b338d917SBrian Gix hci_update_adv_data_sync(hdev, 0x00);
504b338d917SBrian Gix hci_update_scan_rsp_data_sync(hdev, 0x00);
505b338d917SBrian Gix hci_enable_advertising_sync(hdev);
506b338d917SBrian Gix }
507b338d917SBrian Gix }
508b338d917SBrian Gix
509b338d917SBrian Gix return 0;
510b338d917SBrian Gix }
511b338d917SBrian Gix
reenable_adv(struct work_struct * work)512b338d917SBrian Gix static void reenable_adv(struct work_struct *work)
513b338d917SBrian Gix {
514b338d917SBrian Gix struct hci_dev *hdev = container_of(work, struct hci_dev,
515b338d917SBrian Gix reenable_adv_work);
516b338d917SBrian Gix int status;
517b338d917SBrian Gix
518b338d917SBrian Gix bt_dev_dbg(hdev, "");
519b338d917SBrian Gix
520b338d917SBrian Gix hci_dev_lock(hdev);
521b338d917SBrian Gix
522b338d917SBrian Gix status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
523b338d917SBrian Gix if (status)
524b338d917SBrian Gix bt_dev_err(hdev, "failed to reenable ADV: %d", status);
525b338d917SBrian Gix
526b338d917SBrian Gix hci_dev_unlock(hdev);
527b338d917SBrian Gix }
528b338d917SBrian Gix
cancel_adv_timeout(struct hci_dev * hdev)529c249ea9bSBrian Gix static void cancel_adv_timeout(struct hci_dev *hdev)
530c249ea9bSBrian Gix {
531c249ea9bSBrian Gix if (hdev->adv_instance_timeout) {
532c249ea9bSBrian Gix hdev->adv_instance_timeout = 0;
533c249ea9bSBrian Gix cancel_delayed_work(&hdev->adv_instance_expire);
534c249ea9bSBrian Gix }
535c249ea9bSBrian Gix }
536c249ea9bSBrian Gix
537c249ea9bSBrian Gix /* For a single instance:
538c249ea9bSBrian Gix * - force == true: The instance will be removed even when its remaining
539c249ea9bSBrian Gix * lifetime is not zero.
540c249ea9bSBrian Gix * - force == false: the instance will be deactivated but kept stored unless
541c249ea9bSBrian Gix * the remaining lifetime is zero.
542c249ea9bSBrian Gix *
543c249ea9bSBrian Gix * For instance == 0x00:
544c249ea9bSBrian Gix * - force == true: All instances will be removed regardless of their timeout
545c249ea9bSBrian Gix * setting.
546c249ea9bSBrian Gix * - force == false: Only instances that have a timeout will be removed.
547c249ea9bSBrian Gix */
hci_clear_adv_instance_sync(struct hci_dev * hdev,struct sock * sk,u8 instance,bool force)548c249ea9bSBrian Gix int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
549c249ea9bSBrian Gix u8 instance, bool force)
550c249ea9bSBrian Gix {
551c249ea9bSBrian Gix struct adv_info *adv_instance, *n, *next_instance = NULL;
552c249ea9bSBrian Gix int err;
553c249ea9bSBrian Gix u8 rem_inst;
554c249ea9bSBrian Gix
555c249ea9bSBrian Gix /* Cancel any timeout concerning the removed instance(s). */
556c249ea9bSBrian Gix if (!instance || hdev->cur_adv_instance == instance)
557c249ea9bSBrian Gix cancel_adv_timeout(hdev);
558c249ea9bSBrian Gix
559c249ea9bSBrian Gix /* Get the next instance to advertise BEFORE we remove
560c249ea9bSBrian Gix * the current one. This can be the same instance again
561c249ea9bSBrian Gix * if there is only one instance.
562c249ea9bSBrian Gix */
563c249ea9bSBrian Gix if (instance && hdev->cur_adv_instance == instance)
564c249ea9bSBrian Gix next_instance = hci_get_next_instance(hdev, instance);
565c249ea9bSBrian Gix
566c249ea9bSBrian Gix if (instance == 0x00) {
567c249ea9bSBrian Gix list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
568c249ea9bSBrian Gix list) {
569c249ea9bSBrian Gix if (!(force || adv_instance->timeout))
570c249ea9bSBrian Gix continue;
571c249ea9bSBrian Gix
572c249ea9bSBrian Gix rem_inst = adv_instance->instance;
573c249ea9bSBrian Gix err = hci_remove_adv_instance(hdev, rem_inst);
574c249ea9bSBrian Gix if (!err)
575c249ea9bSBrian Gix mgmt_advertising_removed(sk, hdev, rem_inst);
576c249ea9bSBrian Gix }
577c249ea9bSBrian Gix } else {
578c249ea9bSBrian Gix adv_instance = hci_find_adv_instance(hdev, instance);
579c249ea9bSBrian Gix
580c249ea9bSBrian Gix if (force || (adv_instance && adv_instance->timeout &&
581c249ea9bSBrian Gix !adv_instance->remaining_time)) {
582c249ea9bSBrian Gix /* Don't advertise a removed instance. */
583c249ea9bSBrian Gix if (next_instance &&
584c249ea9bSBrian Gix next_instance->instance == instance)
585c249ea9bSBrian Gix next_instance = NULL;
586c249ea9bSBrian Gix
587c249ea9bSBrian Gix err = hci_remove_adv_instance(hdev, instance);
588c249ea9bSBrian Gix if (!err)
589c249ea9bSBrian Gix mgmt_advertising_removed(sk, hdev, instance);
590c249ea9bSBrian Gix }
591c249ea9bSBrian Gix }
592c249ea9bSBrian Gix
593c249ea9bSBrian Gix if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
594c249ea9bSBrian Gix return 0;
595c249ea9bSBrian Gix
596c249ea9bSBrian Gix if (next_instance && !ext_adv_capable(hdev))
597c249ea9bSBrian Gix return hci_schedule_adv_instance_sync(hdev,
598c249ea9bSBrian Gix next_instance->instance,
599c249ea9bSBrian Gix false);
600c249ea9bSBrian Gix
601c249ea9bSBrian Gix return 0;
602c249ea9bSBrian Gix }
603c249ea9bSBrian Gix
adv_timeout_expire_sync(struct hci_dev * hdev,void * data)604c249ea9bSBrian Gix static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
605c249ea9bSBrian Gix {
606c249ea9bSBrian Gix u8 instance = *(u8 *)data;
607c249ea9bSBrian Gix
608c249ea9bSBrian Gix kfree(data);
609c249ea9bSBrian Gix
610c249ea9bSBrian Gix hci_clear_adv_instance_sync(hdev, NULL, instance, false);
611c249ea9bSBrian Gix
612c249ea9bSBrian Gix if (list_empty(&hdev->adv_instances))
613c249ea9bSBrian Gix return hci_disable_advertising_sync(hdev);
614c249ea9bSBrian Gix
615c249ea9bSBrian Gix return 0;
616c249ea9bSBrian Gix }
617c249ea9bSBrian Gix
adv_timeout_expire(struct work_struct * work)618c249ea9bSBrian Gix static void adv_timeout_expire(struct work_struct *work)
619c249ea9bSBrian Gix {
620c249ea9bSBrian Gix u8 *inst_ptr;
621c249ea9bSBrian Gix struct hci_dev *hdev = container_of(work, struct hci_dev,
622c249ea9bSBrian Gix adv_instance_expire.work);
623c249ea9bSBrian Gix
624c249ea9bSBrian Gix bt_dev_dbg(hdev, "");
625c249ea9bSBrian Gix
626c249ea9bSBrian Gix hci_dev_lock(hdev);
627c249ea9bSBrian Gix
628c249ea9bSBrian Gix hdev->adv_instance_timeout = 0;
629c249ea9bSBrian Gix
630c249ea9bSBrian Gix if (hdev->cur_adv_instance == 0x00)
631c249ea9bSBrian Gix goto unlock;
632c249ea9bSBrian Gix
633c249ea9bSBrian Gix inst_ptr = kmalloc(1, GFP_KERNEL);
634c249ea9bSBrian Gix if (!inst_ptr)
635c249ea9bSBrian Gix goto unlock;
636c249ea9bSBrian Gix
637c249ea9bSBrian Gix *inst_ptr = hdev->cur_adv_instance;
638c249ea9bSBrian Gix hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
639c249ea9bSBrian Gix
640c249ea9bSBrian Gix unlock:
641c249ea9bSBrian Gix hci_dev_unlock(hdev);
642c249ea9bSBrian Gix }
643c249ea9bSBrian Gix
hci_cmd_sync_init(struct hci_dev * hdev)6446a98e383SMarcel Holtmann void hci_cmd_sync_init(struct hci_dev *hdev)
6456a98e383SMarcel Holtmann {
6466a98e383SMarcel Holtmann INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
6476a98e383SMarcel Holtmann INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
6486a98e383SMarcel Holtmann mutex_init(&hdev->cmd_sync_work_lock);
6491857c199SZhengping Jiang mutex_init(&hdev->unregister_lock);
650744451c1SBenjamin Berg
651744451c1SBenjamin Berg INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
652b338d917SBrian Gix INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
6538ffde2a7SBrian Gix INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
65427d54b77SBrian Gix INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart);
655c249ea9bSBrian Gix INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
6566a98e383SMarcel Holtmann }
6576a98e383SMarcel Holtmann
_hci_cmd_sync_cancel_entry(struct hci_dev * hdev,struct hci_cmd_sync_work_entry * entry,int err)6581499f799SLuiz Augusto von Dentz static void _hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
6591499f799SLuiz Augusto von Dentz struct hci_cmd_sync_work_entry *entry,
6601499f799SLuiz Augusto von Dentz int err)
6611499f799SLuiz Augusto von Dentz {
6621499f799SLuiz Augusto von Dentz if (entry->destroy)
6631499f799SLuiz Augusto von Dentz entry->destroy(hdev, entry->data, err);
6641499f799SLuiz Augusto von Dentz
6651499f799SLuiz Augusto von Dentz list_del(&entry->list);
6661499f799SLuiz Augusto von Dentz kfree(entry);
6671499f799SLuiz Augusto von Dentz }
6681499f799SLuiz Augusto von Dentz
hci_cmd_sync_clear(struct hci_dev * hdev)6696a98e383SMarcel Holtmann void hci_cmd_sync_clear(struct hci_dev *hdev)
6706a98e383SMarcel Holtmann {
6716a98e383SMarcel Holtmann struct hci_cmd_sync_work_entry *entry, *tmp;
6726a98e383SMarcel Holtmann
6736a98e383SMarcel Holtmann cancel_work_sync(&hdev->cmd_sync_work);
674b338d917SBrian Gix cancel_work_sync(&hdev->reenable_adv_work);
6756a98e383SMarcel Holtmann
6761c66bee4SMin Li mutex_lock(&hdev->cmd_sync_work_lock);
6771499f799SLuiz Augusto von Dentz list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list)
6781499f799SLuiz Augusto von Dentz _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
6791c66bee4SMin Li mutex_unlock(&hdev->cmd_sync_work_lock);
6806a98e383SMarcel Holtmann }
6816a98e383SMarcel Holtmann
hci_cmd_sync_cancel(struct hci_dev * hdev,int err)682744451c1SBenjamin Berg void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
683744451c1SBenjamin Berg {
684744451c1SBenjamin Berg bt_dev_dbg(hdev, "err 0x%2.2x", err);
685744451c1SBenjamin Berg
686744451c1SBenjamin Berg if (hdev->req_status == HCI_REQ_PEND) {
687744451c1SBenjamin Berg hdev->req_result = err;
688744451c1SBenjamin Berg hdev->req_status = HCI_REQ_CANCELED;
689744451c1SBenjamin Berg
690744451c1SBenjamin Berg queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
691744451c1SBenjamin Berg }
692744451c1SBenjamin Berg }
693914b08b3SBenjamin Berg EXPORT_SYMBOL(hci_cmd_sync_cancel);
694914b08b3SBenjamin Berg
6950ce1229cSLuiz Augusto von Dentz /* Cancel ongoing command request synchronously:
6960ce1229cSLuiz Augusto von Dentz *
6970ce1229cSLuiz Augusto von Dentz * - Set result and mark status to HCI_REQ_CANCELED
6980ce1229cSLuiz Augusto von Dentz * - Wakeup command sync thread
6990ce1229cSLuiz Augusto von Dentz */
hci_cmd_sync_cancel_sync(struct hci_dev * hdev,int err)7000ce1229cSLuiz Augusto von Dentz void hci_cmd_sync_cancel_sync(struct hci_dev *hdev, int err)
7010ce1229cSLuiz Augusto von Dentz {
7020ce1229cSLuiz Augusto von Dentz bt_dev_dbg(hdev, "err 0x%2.2x", err);
7030ce1229cSLuiz Augusto von Dentz
7040ce1229cSLuiz Augusto von Dentz if (hdev->req_status == HCI_REQ_PEND) {
7059ae3954dSLuiz Augusto von Dentz /* req_result is __u32 so error must be positive to be properly
7069ae3954dSLuiz Augusto von Dentz * propagated.
7079ae3954dSLuiz Augusto von Dentz */
7089ae3954dSLuiz Augusto von Dentz hdev->req_result = err < 0 ? -err : err;
7090ce1229cSLuiz Augusto von Dentz hdev->req_status = HCI_REQ_CANCELED;
7100ce1229cSLuiz Augusto von Dentz
7110ce1229cSLuiz Augusto von Dentz wake_up_interruptible(&hdev->req_wait_q);
7120ce1229cSLuiz Augusto von Dentz }
7130ce1229cSLuiz Augusto von Dentz }
7140ce1229cSLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_cancel_sync);
7150ce1229cSLuiz Augusto von Dentz
716d883a466SLuiz Augusto von Dentz /* Submit HCI command to be run in as cmd_sync_work:
717d883a466SLuiz Augusto von Dentz *
718d883a466SLuiz Augusto von Dentz * - hdev must _not_ be unregistered
719d883a466SLuiz Augusto von Dentz */
hci_cmd_sync_submit(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)720d883a466SLuiz Augusto von Dentz int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
7216a98e383SMarcel Holtmann void *data, hci_cmd_sync_work_destroy_t destroy)
7226a98e383SMarcel Holtmann {
7236a98e383SMarcel Holtmann struct hci_cmd_sync_work_entry *entry;
7241857c199SZhengping Jiang int err = 0;
7256a98e383SMarcel Holtmann
7261857c199SZhengping Jiang mutex_lock(&hdev->unregister_lock);
7271857c199SZhengping Jiang if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
7281857c199SZhengping Jiang err = -ENODEV;
7291857c199SZhengping Jiang goto unlock;
7301857c199SZhengping Jiang }
7310b94f265SLuiz Augusto von Dentz
7326a98e383SMarcel Holtmann entry = kmalloc(sizeof(*entry), GFP_KERNEL);
7331857c199SZhengping Jiang if (!entry) {
7341857c199SZhengping Jiang err = -ENOMEM;
7351857c199SZhengping Jiang goto unlock;
7361857c199SZhengping Jiang }
7376a98e383SMarcel Holtmann entry->func = func;
7386a98e383SMarcel Holtmann entry->data = data;
7396a98e383SMarcel Holtmann entry->destroy = destroy;
7406a98e383SMarcel Holtmann
7416a98e383SMarcel Holtmann mutex_lock(&hdev->cmd_sync_work_lock);
7426a98e383SMarcel Holtmann list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
7436a98e383SMarcel Holtmann mutex_unlock(&hdev->cmd_sync_work_lock);
7446a98e383SMarcel Holtmann
7456a98e383SMarcel Holtmann queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
7466a98e383SMarcel Holtmann
7471857c199SZhengping Jiang unlock:
7481857c199SZhengping Jiang mutex_unlock(&hdev->unregister_lock);
7491857c199SZhengping Jiang return err;
7506a98e383SMarcel Holtmann }
751d883a466SLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_submit);
752d883a466SLuiz Augusto von Dentz
753d883a466SLuiz Augusto von Dentz /* Queue HCI command:
754d883a466SLuiz Augusto von Dentz *
755d883a466SLuiz Augusto von Dentz * - hdev must be running
756d883a466SLuiz Augusto von Dentz */
hci_cmd_sync_queue(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)757d883a466SLuiz Augusto von Dentz int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
758d883a466SLuiz Augusto von Dentz void *data, hci_cmd_sync_work_destroy_t destroy)
759d883a466SLuiz Augusto von Dentz {
760d883a466SLuiz Augusto von Dentz /* Only queue command if hdev is running which means it had been opened
761d883a466SLuiz Augusto von Dentz * and is either on init phase or is already up.
762d883a466SLuiz Augusto von Dentz */
763d883a466SLuiz Augusto von Dentz if (!test_bit(HCI_RUNNING, &hdev->flags))
764d883a466SLuiz Augusto von Dentz return -ENETDOWN;
765d883a466SLuiz Augusto von Dentz
766d883a466SLuiz Augusto von Dentz return hci_cmd_sync_submit(hdev, func, data, destroy);
767d883a466SLuiz Augusto von Dentz }
7686a98e383SMarcel Holtmann EXPORT_SYMBOL(hci_cmd_sync_queue);
769161510ccSLuiz Augusto von Dentz
7701499f799SLuiz Augusto von Dentz static struct hci_cmd_sync_work_entry *
_hci_cmd_sync_lookup_entry(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)7711499f799SLuiz Augusto von Dentz _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
7721499f799SLuiz Augusto von Dentz void *data, hci_cmd_sync_work_destroy_t destroy)
7731499f799SLuiz Augusto von Dentz {
7741499f799SLuiz Augusto von Dentz struct hci_cmd_sync_work_entry *entry, *tmp;
7751499f799SLuiz Augusto von Dentz
7761499f799SLuiz Augusto von Dentz list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
7771499f799SLuiz Augusto von Dentz if (func && entry->func != func)
7781499f799SLuiz Augusto von Dentz continue;
7791499f799SLuiz Augusto von Dentz
7801499f799SLuiz Augusto von Dentz if (data && entry->data != data)
7811499f799SLuiz Augusto von Dentz continue;
7821499f799SLuiz Augusto von Dentz
7831499f799SLuiz Augusto von Dentz if (destroy && entry->destroy != destroy)
7841499f799SLuiz Augusto von Dentz continue;
7851499f799SLuiz Augusto von Dentz
7861499f799SLuiz Augusto von Dentz return entry;
7871499f799SLuiz Augusto von Dentz }
7881499f799SLuiz Augusto von Dentz
7891499f799SLuiz Augusto von Dentz return NULL;
7901499f799SLuiz Augusto von Dentz }
7911499f799SLuiz Augusto von Dentz
7921499f799SLuiz Augusto von Dentz /* Queue HCI command entry once:
7931499f799SLuiz Augusto von Dentz *
7941499f799SLuiz Augusto von Dentz * - Lookup if an entry already exist and only if it doesn't creates a new entry
7951499f799SLuiz Augusto von Dentz * and queue it.
7961499f799SLuiz Augusto von Dentz */
hci_cmd_sync_queue_once(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)7971499f799SLuiz Augusto von Dentz int hci_cmd_sync_queue_once(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
7981499f799SLuiz Augusto von Dentz void *data, hci_cmd_sync_work_destroy_t destroy)
7991499f799SLuiz Augusto von Dentz {
8001499f799SLuiz Augusto von Dentz if (hci_cmd_sync_lookup_entry(hdev, func, data, destroy))
8011499f799SLuiz Augusto von Dentz return 0;
8021499f799SLuiz Augusto von Dentz
8031499f799SLuiz Augusto von Dentz return hci_cmd_sync_queue(hdev, func, data, destroy);
8041499f799SLuiz Augusto von Dentz }
8051499f799SLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_queue_once);
8061499f799SLuiz Augusto von Dentz
807d56412eeSLuiz Augusto von Dentz /* Run HCI command:
808d56412eeSLuiz Augusto von Dentz *
809d56412eeSLuiz Augusto von Dentz * - hdev must be running
810d56412eeSLuiz Augusto von Dentz * - if on cmd_sync_work then run immediately otherwise queue
811d56412eeSLuiz Augusto von Dentz */
hci_cmd_sync_run(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)812d56412eeSLuiz Augusto von Dentz int hci_cmd_sync_run(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
813d56412eeSLuiz Augusto von Dentz void *data, hci_cmd_sync_work_destroy_t destroy)
814d56412eeSLuiz Augusto von Dentz {
815d56412eeSLuiz Augusto von Dentz /* Only queue command if hdev is running which means it had been opened
816d56412eeSLuiz Augusto von Dentz * and is either on init phase or is already up.
817d56412eeSLuiz Augusto von Dentz */
818d56412eeSLuiz Augusto von Dentz if (!test_bit(HCI_RUNNING, &hdev->flags))
819d56412eeSLuiz Augusto von Dentz return -ENETDOWN;
820d56412eeSLuiz Augusto von Dentz
821d56412eeSLuiz Augusto von Dentz /* If on cmd_sync_work then run immediately otherwise queue */
822d56412eeSLuiz Augusto von Dentz if (current_work() == &hdev->cmd_sync_work)
823d56412eeSLuiz Augusto von Dentz return func(hdev, data);
824d56412eeSLuiz Augusto von Dentz
825d56412eeSLuiz Augusto von Dentz return hci_cmd_sync_submit(hdev, func, data, destroy);
826d56412eeSLuiz Augusto von Dentz }
827d56412eeSLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_run);
828d56412eeSLuiz Augusto von Dentz
829d56412eeSLuiz Augusto von Dentz /* Run HCI command entry once:
830d56412eeSLuiz Augusto von Dentz *
831d56412eeSLuiz Augusto von Dentz * - Lookup if an entry already exist and only if it doesn't creates a new entry
832d56412eeSLuiz Augusto von Dentz * and run it.
833d56412eeSLuiz Augusto von Dentz * - if on cmd_sync_work then run immediately otherwise queue
834d56412eeSLuiz Augusto von Dentz */
hci_cmd_sync_run_once(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)835d56412eeSLuiz Augusto von Dentz int hci_cmd_sync_run_once(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
836d56412eeSLuiz Augusto von Dentz void *data, hci_cmd_sync_work_destroy_t destroy)
837d56412eeSLuiz Augusto von Dentz {
838d56412eeSLuiz Augusto von Dentz if (hci_cmd_sync_lookup_entry(hdev, func, data, destroy))
839d56412eeSLuiz Augusto von Dentz return 0;
840d56412eeSLuiz Augusto von Dentz
841d56412eeSLuiz Augusto von Dentz return hci_cmd_sync_run(hdev, func, data, destroy);
842d56412eeSLuiz Augusto von Dentz }
843d56412eeSLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_run_once);
844d56412eeSLuiz Augusto von Dentz
8451499f799SLuiz Augusto von Dentz /* Lookup HCI command entry:
8461499f799SLuiz Augusto von Dentz *
8471499f799SLuiz Augusto von Dentz * - Return first entry that matches by function callback or data or
8481499f799SLuiz Augusto von Dentz * destroy callback.
8491499f799SLuiz Augusto von Dentz */
8501499f799SLuiz Augusto von Dentz struct hci_cmd_sync_work_entry *
hci_cmd_sync_lookup_entry(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)8511499f799SLuiz Augusto von Dentz hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
8521499f799SLuiz Augusto von Dentz void *data, hci_cmd_sync_work_destroy_t destroy)
8531499f799SLuiz Augusto von Dentz {
8541499f799SLuiz Augusto von Dentz struct hci_cmd_sync_work_entry *entry;
8551499f799SLuiz Augusto von Dentz
8561499f799SLuiz Augusto von Dentz mutex_lock(&hdev->cmd_sync_work_lock);
8571499f799SLuiz Augusto von Dentz entry = _hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
8581499f799SLuiz Augusto von Dentz mutex_unlock(&hdev->cmd_sync_work_lock);
8591499f799SLuiz Augusto von Dentz
8601499f799SLuiz Augusto von Dentz return entry;
8611499f799SLuiz Augusto von Dentz }
8621499f799SLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_lookup_entry);
8631499f799SLuiz Augusto von Dentz
8641499f799SLuiz Augusto von Dentz /* Cancel HCI command entry */
hci_cmd_sync_cancel_entry(struct hci_dev * hdev,struct hci_cmd_sync_work_entry * entry)8651499f799SLuiz Augusto von Dentz void hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
8661499f799SLuiz Augusto von Dentz struct hci_cmd_sync_work_entry *entry)
8671499f799SLuiz Augusto von Dentz {
8681499f799SLuiz Augusto von Dentz mutex_lock(&hdev->cmd_sync_work_lock);
8691499f799SLuiz Augusto von Dentz _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
8701499f799SLuiz Augusto von Dentz mutex_unlock(&hdev->cmd_sync_work_lock);
8711499f799SLuiz Augusto von Dentz }
8721499f799SLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_cancel_entry);
8731499f799SLuiz Augusto von Dentz
8741499f799SLuiz Augusto von Dentz /* Dequeue one HCI command entry:
8751499f799SLuiz Augusto von Dentz *
8761499f799SLuiz Augusto von Dentz * - Lookup and cancel first entry that matches.
8771499f799SLuiz Augusto von Dentz */
hci_cmd_sync_dequeue_once(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)8781499f799SLuiz Augusto von Dentz bool hci_cmd_sync_dequeue_once(struct hci_dev *hdev,
8791499f799SLuiz Augusto von Dentz hci_cmd_sync_work_func_t func,
8801499f799SLuiz Augusto von Dentz void *data, hci_cmd_sync_work_destroy_t destroy)
8811499f799SLuiz Augusto von Dentz {
8821499f799SLuiz Augusto von Dentz struct hci_cmd_sync_work_entry *entry;
8831499f799SLuiz Augusto von Dentz
8841499f799SLuiz Augusto von Dentz entry = hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
8851499f799SLuiz Augusto von Dentz if (!entry)
8861499f799SLuiz Augusto von Dentz return false;
8871499f799SLuiz Augusto von Dentz
8881499f799SLuiz Augusto von Dentz hci_cmd_sync_cancel_entry(hdev, entry);
8891499f799SLuiz Augusto von Dentz
8901499f799SLuiz Augusto von Dentz return true;
8911499f799SLuiz Augusto von Dentz }
8921499f799SLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_dequeue_once);
8931499f799SLuiz Augusto von Dentz
8941499f799SLuiz Augusto von Dentz /* Dequeue HCI command entry:
8951499f799SLuiz Augusto von Dentz *
8961499f799SLuiz Augusto von Dentz * - Lookup and cancel any entry that matches by function callback or data or
8971499f799SLuiz Augusto von Dentz * destroy callback.
8981499f799SLuiz Augusto von Dentz */
hci_cmd_sync_dequeue(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)8991499f799SLuiz Augusto von Dentz bool hci_cmd_sync_dequeue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
9001499f799SLuiz Augusto von Dentz void *data, hci_cmd_sync_work_destroy_t destroy)
9011499f799SLuiz Augusto von Dentz {
9021499f799SLuiz Augusto von Dentz struct hci_cmd_sync_work_entry *entry;
9031499f799SLuiz Augusto von Dentz bool ret = false;
9041499f799SLuiz Augusto von Dentz
9051499f799SLuiz Augusto von Dentz mutex_lock(&hdev->cmd_sync_work_lock);
9061499f799SLuiz Augusto von Dentz while ((entry = _hci_cmd_sync_lookup_entry(hdev, func, data,
9071499f799SLuiz Augusto von Dentz destroy))) {
9081499f799SLuiz Augusto von Dentz _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
9091499f799SLuiz Augusto von Dentz ret = true;
9101499f799SLuiz Augusto von Dentz }
9111499f799SLuiz Augusto von Dentz mutex_unlock(&hdev->cmd_sync_work_lock);
9121499f799SLuiz Augusto von Dentz
9131499f799SLuiz Augusto von Dentz return ret;
9141499f799SLuiz Augusto von Dentz }
9151499f799SLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_dequeue);
9161499f799SLuiz Augusto von Dentz
hci_update_eir_sync(struct hci_dev * hdev)917161510ccSLuiz Augusto von Dentz int hci_update_eir_sync(struct hci_dev *hdev)
918161510ccSLuiz Augusto von Dentz {
919161510ccSLuiz Augusto von Dentz struct hci_cp_write_eir cp;
920161510ccSLuiz Augusto von Dentz
921161510ccSLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
922161510ccSLuiz Augusto von Dentz
923161510ccSLuiz Augusto von Dentz if (!hdev_is_powered(hdev))
924161510ccSLuiz Augusto von Dentz return 0;
925161510ccSLuiz Augusto von Dentz
926161510ccSLuiz Augusto von Dentz if (!lmp_ext_inq_capable(hdev))
927161510ccSLuiz Augusto von Dentz return 0;
928161510ccSLuiz Augusto von Dentz
929161510ccSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
930161510ccSLuiz Augusto von Dentz return 0;
931161510ccSLuiz Augusto von Dentz
932161510ccSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
933161510ccSLuiz Augusto von Dentz return 0;
934161510ccSLuiz Augusto von Dentz
935161510ccSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
936161510ccSLuiz Augusto von Dentz
937161510ccSLuiz Augusto von Dentz eir_create(hdev, cp.data);
938161510ccSLuiz Augusto von Dentz
939161510ccSLuiz Augusto von Dentz if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
940161510ccSLuiz Augusto von Dentz return 0;
941161510ccSLuiz Augusto von Dentz
942161510ccSLuiz Augusto von Dentz memcpy(hdev->eir, cp.data, sizeof(cp.data));
943161510ccSLuiz Augusto von Dentz
944161510ccSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
945161510ccSLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
946161510ccSLuiz Augusto von Dentz }
947161510ccSLuiz Augusto von Dentz
get_service_classes(struct hci_dev * hdev)948161510ccSLuiz Augusto von Dentz static u8 get_service_classes(struct hci_dev *hdev)
949161510ccSLuiz Augusto von Dentz {
950161510ccSLuiz Augusto von Dentz struct bt_uuid *uuid;
951161510ccSLuiz Augusto von Dentz u8 val = 0;
952161510ccSLuiz Augusto von Dentz
953161510ccSLuiz Augusto von Dentz list_for_each_entry(uuid, &hdev->uuids, list)
954161510ccSLuiz Augusto von Dentz val |= uuid->svc_hint;
955161510ccSLuiz Augusto von Dentz
956161510ccSLuiz Augusto von Dentz return val;
957161510ccSLuiz Augusto von Dentz }
958161510ccSLuiz Augusto von Dentz
hci_update_class_sync(struct hci_dev * hdev)959161510ccSLuiz Augusto von Dentz int hci_update_class_sync(struct hci_dev *hdev)
960161510ccSLuiz Augusto von Dentz {
961161510ccSLuiz Augusto von Dentz u8 cod[3];
962161510ccSLuiz Augusto von Dentz
963161510ccSLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
964161510ccSLuiz Augusto von Dentz
965161510ccSLuiz Augusto von Dentz if (!hdev_is_powered(hdev))
966161510ccSLuiz Augusto von Dentz return 0;
967161510ccSLuiz Augusto von Dentz
968161510ccSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
969161510ccSLuiz Augusto von Dentz return 0;
970161510ccSLuiz Augusto von Dentz
971161510ccSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
972161510ccSLuiz Augusto von Dentz return 0;
973161510ccSLuiz Augusto von Dentz
974161510ccSLuiz Augusto von Dentz cod[0] = hdev->minor_class;
975161510ccSLuiz Augusto von Dentz cod[1] = hdev->major_class;
976161510ccSLuiz Augusto von Dentz cod[2] = get_service_classes(hdev);
977161510ccSLuiz Augusto von Dentz
978161510ccSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
979161510ccSLuiz Augusto von Dentz cod[1] |= 0x20;
980161510ccSLuiz Augusto von Dentz
981161510ccSLuiz Augusto von Dentz if (memcmp(cod, hdev->dev_class, 3) == 0)
982161510ccSLuiz Augusto von Dentz return 0;
983161510ccSLuiz Augusto von Dentz
984161510ccSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
985161510ccSLuiz Augusto von Dentz sizeof(cod), cod, HCI_CMD_TIMEOUT);
986161510ccSLuiz Augusto von Dentz }
987cba6b758SLuiz Augusto von Dentz
is_advertising_allowed(struct hci_dev * hdev,bool connectable)988cba6b758SLuiz Augusto von Dentz static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
989cba6b758SLuiz Augusto von Dentz {
990cba6b758SLuiz Augusto von Dentz /* If there is no connection we are OK to advertise. */
991cba6b758SLuiz Augusto von Dentz if (hci_conn_num(hdev, LE_LINK) == 0)
992cba6b758SLuiz Augusto von Dentz return true;
993cba6b758SLuiz Augusto von Dentz
994cba6b758SLuiz Augusto von Dentz /* Check le_states if there is any connection in peripheral role. */
995cba6b758SLuiz Augusto von Dentz if (hdev->conn_hash.le_num_peripheral > 0) {
996cba6b758SLuiz Augusto von Dentz /* Peripheral connection state and non connectable mode
997cba6b758SLuiz Augusto von Dentz * bit 20.
998cba6b758SLuiz Augusto von Dentz */
999cba6b758SLuiz Augusto von Dentz if (!connectable && !(hdev->le_states[2] & 0x10))
1000cba6b758SLuiz Augusto von Dentz return false;
1001cba6b758SLuiz Augusto von Dentz
1002cba6b758SLuiz Augusto von Dentz /* Peripheral connection state and connectable mode bit 38
1003cba6b758SLuiz Augusto von Dentz * and scannable bit 21.
1004cba6b758SLuiz Augusto von Dentz */
1005cba6b758SLuiz Augusto von Dentz if (connectable && (!(hdev->le_states[4] & 0x40) ||
1006cba6b758SLuiz Augusto von Dentz !(hdev->le_states[2] & 0x20)))
1007cba6b758SLuiz Augusto von Dentz return false;
1008cba6b758SLuiz Augusto von Dentz }
1009cba6b758SLuiz Augusto von Dentz
1010cba6b758SLuiz Augusto von Dentz /* Check le_states if there is any connection in central role. */
1011cba6b758SLuiz Augusto von Dentz if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
1012cba6b758SLuiz Augusto von Dentz /* Central connection state and non connectable mode bit 18. */
1013cba6b758SLuiz Augusto von Dentz if (!connectable && !(hdev->le_states[2] & 0x02))
1014cba6b758SLuiz Augusto von Dentz return false;
1015cba6b758SLuiz Augusto von Dentz
1016cba6b758SLuiz Augusto von Dentz /* Central connection state and connectable mode bit 35 and
1017cba6b758SLuiz Augusto von Dentz * scannable 19.
1018cba6b758SLuiz Augusto von Dentz */
1019cba6b758SLuiz Augusto von Dentz if (connectable && (!(hdev->le_states[4] & 0x08) ||
1020cba6b758SLuiz Augusto von Dentz !(hdev->le_states[2] & 0x08)))
1021cba6b758SLuiz Augusto von Dentz return false;
1022cba6b758SLuiz Augusto von Dentz }
1023cba6b758SLuiz Augusto von Dentz
1024cba6b758SLuiz Augusto von Dentz return true;
1025cba6b758SLuiz Augusto von Dentz }
1026cba6b758SLuiz Augusto von Dentz
adv_use_rpa(struct hci_dev * hdev,uint32_t flags)1027cba6b758SLuiz Augusto von Dentz static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
1028cba6b758SLuiz Augusto von Dentz {
1029cba6b758SLuiz Augusto von Dentz /* If privacy is not enabled don't use RPA */
1030cba6b758SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
1031cba6b758SLuiz Augusto von Dentz return false;
1032cba6b758SLuiz Augusto von Dentz
1033cba6b758SLuiz Augusto von Dentz /* If basic privacy mode is enabled use RPA */
1034cba6b758SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
1035cba6b758SLuiz Augusto von Dentz return true;
1036cba6b758SLuiz Augusto von Dentz
1037cba6b758SLuiz Augusto von Dentz /* If limited privacy mode is enabled don't use RPA if we're
1038cba6b758SLuiz Augusto von Dentz * both discoverable and bondable.
1039cba6b758SLuiz Augusto von Dentz */
1040cba6b758SLuiz Augusto von Dentz if ((flags & MGMT_ADV_FLAG_DISCOV) &&
1041cba6b758SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_BONDABLE))
1042cba6b758SLuiz Augusto von Dentz return false;
1043cba6b758SLuiz Augusto von Dentz
1044cba6b758SLuiz Augusto von Dentz /* We're neither bondable nor discoverable in the limited
1045cba6b758SLuiz Augusto von Dentz * privacy mode, therefore use RPA.
1046cba6b758SLuiz Augusto von Dentz */
1047cba6b758SLuiz Augusto von Dentz return true;
1048cba6b758SLuiz Augusto von Dentz }
1049cba6b758SLuiz Augusto von Dentz
hci_set_random_addr_sync(struct hci_dev * hdev,bdaddr_t * rpa)1050cba6b758SLuiz Augusto von Dentz static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
1051cba6b758SLuiz Augusto von Dentz {
1052*9ba06f07SLuiz Augusto von Dentz /* If a random_addr has been set we're advertising or initiating an LE
1053*9ba06f07SLuiz Augusto von Dentz * connection we can't go ahead and change the random address at this
1054*9ba06f07SLuiz Augusto von Dentz * time. This is because the eventual initiator address used for the
1055cba6b758SLuiz Augusto von Dentz * subsequently created connection will be undefined (some
1056cba6b758SLuiz Augusto von Dentz * controllers use the new address and others the one we had
1057cba6b758SLuiz Augusto von Dentz * when the operation started).
1058cba6b758SLuiz Augusto von Dentz *
1059cba6b758SLuiz Augusto von Dentz * In this kind of scenario skip the update and let the random
1060cba6b758SLuiz Augusto von Dentz * address be updated at the next cycle.
1061cba6b758SLuiz Augusto von Dentz */
1062*9ba06f07SLuiz Augusto von Dentz if (bacmp(&hdev->random_addr, BDADDR_ANY) &&
1063*9ba06f07SLuiz Augusto von Dentz (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
1064*9ba06f07SLuiz Augusto von Dentz hci_lookup_le_connect(hdev))) {
1065cba6b758SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Deferring random address update");
1066cba6b758SLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
1067cba6b758SLuiz Augusto von Dentz return 0;
1068cba6b758SLuiz Augusto von Dentz }
1069cba6b758SLuiz Augusto von Dentz
1070cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
1071cba6b758SLuiz Augusto von Dentz 6, rpa, HCI_CMD_TIMEOUT);
1072cba6b758SLuiz Augusto von Dentz }
hci_update_random_address_sync(struct hci_dev * hdev,bool require_privacy,bool rpa,u8 * own_addr_type)1073cba6b758SLuiz Augusto von Dentz
1074cba6b758SLuiz Augusto von Dentz int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
1075cba6b758SLuiz Augusto von Dentz bool rpa, u8 *own_addr_type)
1076cba6b758SLuiz Augusto von Dentz {
1077cba6b758SLuiz Augusto von Dentz int err;
1078cba6b758SLuiz Augusto von Dentz
1079cba6b758SLuiz Augusto von Dentz /* If privacy is enabled use a resolvable private address. If
1080cba6b758SLuiz Augusto von Dentz * current RPA has expired or there is something else than
1081cba6b758SLuiz Augusto von Dentz * the current RPA in use, then generate a new one.
1082cba6b758SLuiz Augusto von Dentz */
1083cba6b758SLuiz Augusto von Dentz if (rpa) {
1084cba6b758SLuiz Augusto von Dentz /* If Controller supports LL Privacy use own address type is
1085cba6b758SLuiz Augusto von Dentz * 0x03
1086cba6b758SLuiz Augusto von Dentz */
1087ad383c2cSLuiz Augusto von Dentz if (use_ll_privacy(hdev))
1088cba6b758SLuiz Augusto von Dentz *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
1089cba6b758SLuiz Augusto von Dentz else
1090cba6b758SLuiz Augusto von Dentz *own_addr_type = ADDR_LE_DEV_RANDOM;
1091cba6b758SLuiz Augusto von Dentz
1092cba6b758SLuiz Augusto von Dentz /* Check if RPA is valid */
1093cba6b758SLuiz Augusto von Dentz if (rpa_valid(hdev))
1094cba6b758SLuiz Augusto von Dentz return 0;
1095cba6b758SLuiz Augusto von Dentz
1096cba6b758SLuiz Augusto von Dentz err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
1097cba6b758SLuiz Augusto von Dentz if (err < 0) {
1098cba6b758SLuiz Augusto von Dentz bt_dev_err(hdev, "failed to generate new RPA");
1099cba6b758SLuiz Augusto von Dentz return err;
1100cba6b758SLuiz Augusto von Dentz }
1101cba6b758SLuiz Augusto von Dentz
1102cba6b758SLuiz Augusto von Dentz err = hci_set_random_addr_sync(hdev, &hdev->rpa);
1103cba6b758SLuiz Augusto von Dentz if (err)
1104cba6b758SLuiz Augusto von Dentz return err;
1105cba6b758SLuiz Augusto von Dentz
1106cba6b758SLuiz Augusto von Dentz return 0;
1107cba6b758SLuiz Augusto von Dentz }
1108cba6b758SLuiz Augusto von Dentz
1109cba6b758SLuiz Augusto von Dentz /* In case of required privacy without resolvable private address,
1110cba6b758SLuiz Augusto von Dentz * use an non-resolvable private address. This is useful for active
1111cba6b758SLuiz Augusto von Dentz * scanning and non-connectable advertising.
1112cba6b758SLuiz Augusto von Dentz */
1113cba6b758SLuiz Augusto von Dentz if (require_privacy) {
1114cba6b758SLuiz Augusto von Dentz bdaddr_t nrpa;
1115cba6b758SLuiz Augusto von Dentz
1116cba6b758SLuiz Augusto von Dentz while (true) {
1117cba6b758SLuiz Augusto von Dentz /* The non-resolvable private address is generated
1118cba6b758SLuiz Augusto von Dentz * from random six bytes with the two most significant
1119cba6b758SLuiz Augusto von Dentz * bits cleared.
1120cba6b758SLuiz Augusto von Dentz */
1121cba6b758SLuiz Augusto von Dentz get_random_bytes(&nrpa, 6);
1122cba6b758SLuiz Augusto von Dentz nrpa.b[5] &= 0x3f;
1123cba6b758SLuiz Augusto von Dentz
1124cba6b758SLuiz Augusto von Dentz /* The non-resolvable private address shall not be
1125cba6b758SLuiz Augusto von Dentz * equal to the public address.
1126cba6b758SLuiz Augusto von Dentz */
1127cba6b758SLuiz Augusto von Dentz if (bacmp(&hdev->bdaddr, &nrpa))
1128cba6b758SLuiz Augusto von Dentz break;
1129cba6b758SLuiz Augusto von Dentz }
1130cba6b758SLuiz Augusto von Dentz
1131cba6b758SLuiz Augusto von Dentz *own_addr_type = ADDR_LE_DEV_RANDOM;
1132cba6b758SLuiz Augusto von Dentz
1133cba6b758SLuiz Augusto von Dentz return hci_set_random_addr_sync(hdev, &nrpa);
1134cba6b758SLuiz Augusto von Dentz }
1135cba6b758SLuiz Augusto von Dentz
1136cba6b758SLuiz Augusto von Dentz /* If forcing static address is in use or there is no public
1137cba6b758SLuiz Augusto von Dentz * address use the static address as random address (but skip
1138cba6b758SLuiz Augusto von Dentz * the HCI command if the current random address is already the
1139cba6b758SLuiz Augusto von Dentz * static one.
1140cba6b758SLuiz Augusto von Dentz *
1141cba6b758SLuiz Augusto von Dentz * In case BR/EDR has been disabled on a dual-mode controller
1142cba6b758SLuiz Augusto von Dentz * and a static address has been configured, then use that
1143cba6b758SLuiz Augusto von Dentz * address instead of the public BR/EDR address.
1144cba6b758SLuiz Augusto von Dentz */
1145cba6b758SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
1146cba6b758SLuiz Augusto von Dentz !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
1147cba6b758SLuiz Augusto von Dentz (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
1148cba6b758SLuiz Augusto von Dentz bacmp(&hdev->static_addr, BDADDR_ANY))) {
1149cba6b758SLuiz Augusto von Dentz *own_addr_type = ADDR_LE_DEV_RANDOM;
1150cba6b758SLuiz Augusto von Dentz if (bacmp(&hdev->static_addr, &hdev->random_addr))
1151cba6b758SLuiz Augusto von Dentz return hci_set_random_addr_sync(hdev,
1152cba6b758SLuiz Augusto von Dentz &hdev->static_addr);
1153cba6b758SLuiz Augusto von Dentz return 0;
1154cba6b758SLuiz Augusto von Dentz }
1155cba6b758SLuiz Augusto von Dentz
1156cba6b758SLuiz Augusto von Dentz /* Neither privacy nor static address is being used so use a
1157cba6b758SLuiz Augusto von Dentz * public address.
1158cba6b758SLuiz Augusto von Dentz */
1159cba6b758SLuiz Augusto von Dentz *own_addr_type = ADDR_LE_DEV_PUBLIC;
1160cba6b758SLuiz Augusto von Dentz
1161cba6b758SLuiz Augusto von Dentz return 0;
1162cba6b758SLuiz Augusto von Dentz }
hci_disable_ext_adv_instance_sync(struct hci_dev * hdev,u8 instance)1163cba6b758SLuiz Augusto von Dentz
1164cba6b758SLuiz Augusto von Dentz static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1165cba6b758SLuiz Augusto von Dentz {
1166cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_ext_adv_enable *cp;
1167cba6b758SLuiz Augusto von Dentz struct hci_cp_ext_adv_set *set;
1168cba6b758SLuiz Augusto von Dentz u8 data[sizeof(*cp) + sizeof(*set) * 1];
1169cba6b758SLuiz Augusto von Dentz u8 size;
1170cba6b758SLuiz Augusto von Dentz
1171cba6b758SLuiz Augusto von Dentz /* If request specifies an instance that doesn't exist, fail */
1172cba6b758SLuiz Augusto von Dentz if (instance > 0) {
1173cba6b758SLuiz Augusto von Dentz struct adv_info *adv;
1174cba6b758SLuiz Augusto von Dentz
1175cba6b758SLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance);
1176cba6b758SLuiz Augusto von Dentz if (!adv)
1177cba6b758SLuiz Augusto von Dentz return -EINVAL;
1178cba6b758SLuiz Augusto von Dentz
1179cba6b758SLuiz Augusto von Dentz /* If not enabled there is nothing to do */
1180cba6b758SLuiz Augusto von Dentz if (!adv->enabled)
1181cba6b758SLuiz Augusto von Dentz return 0;
1182cba6b758SLuiz Augusto von Dentz }
1183cba6b758SLuiz Augusto von Dentz
1184cba6b758SLuiz Augusto von Dentz memset(data, 0, sizeof(data));
1185cba6b758SLuiz Augusto von Dentz
1186cba6b758SLuiz Augusto von Dentz cp = (void *)data;
1187cba6b758SLuiz Augusto von Dentz set = (void *)cp->data;
1188cba6b758SLuiz Augusto von Dentz
1189cba6b758SLuiz Augusto von Dentz /* Instance 0x00 indicates all advertising instances will be disabled */
1190cba6b758SLuiz Augusto von Dentz cp->num_of_sets = !!instance;
1191cba6b758SLuiz Augusto von Dentz cp->enable = 0x00;
1192cba6b758SLuiz Augusto von Dentz
1193cba6b758SLuiz Augusto von Dentz set->handle = instance;
1194cba6b758SLuiz Augusto von Dentz
1195cba6b758SLuiz Augusto von Dentz size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
1196cba6b758SLuiz Augusto von Dentz
1197cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1198cba6b758SLuiz Augusto von Dentz size, data, HCI_CMD_TIMEOUT);
1199cba6b758SLuiz Augusto von Dentz }
hci_set_adv_set_random_addr_sync(struct hci_dev * hdev,u8 instance,bdaddr_t * random_addr)1200cba6b758SLuiz Augusto von Dentz
1201cba6b758SLuiz Augusto von Dentz static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1202cba6b758SLuiz Augusto von Dentz bdaddr_t *random_addr)
1203cba6b758SLuiz Augusto von Dentz {
1204cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_adv_set_rand_addr cp;
1205cba6b758SLuiz Augusto von Dentz int err;
1206cba6b758SLuiz Augusto von Dentz
1207cba6b758SLuiz Augusto von Dentz if (!instance) {
1208cba6b758SLuiz Augusto von Dentz /* Instance 0x00 doesn't have an adv_info, instead it uses
1209cba6b758SLuiz Augusto von Dentz * hdev->random_addr to track its address so whenever it needs
1210cba6b758SLuiz Augusto von Dentz * to be updated this also set the random address since
1211cba6b758SLuiz Augusto von Dentz * hdev->random_addr is shared with scan state machine.
1212cba6b758SLuiz Augusto von Dentz */
1213cba6b758SLuiz Augusto von Dentz err = hci_set_random_addr_sync(hdev, random_addr);
1214cba6b758SLuiz Augusto von Dentz if (err)
1215cba6b758SLuiz Augusto von Dentz return err;
1216cba6b758SLuiz Augusto von Dentz }
1217cba6b758SLuiz Augusto von Dentz
1218cba6b758SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
1219cba6b758SLuiz Augusto von Dentz
1220cba6b758SLuiz Augusto von Dentz cp.handle = instance;
1221cba6b758SLuiz Augusto von Dentz bacpy(&cp.bdaddr, random_addr);
1222cba6b758SLuiz Augusto von Dentz
1223cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1224cba6b758SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1225cba6b758SLuiz Augusto von Dentz }
hci_setup_ext_adv_instance_sync(struct hci_dev * hdev,u8 instance)1226cba6b758SLuiz Augusto von Dentz
1227cba6b758SLuiz Augusto von Dentz int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1228cba6b758SLuiz Augusto von Dentz {
1229cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_ext_adv_params cp;
1230cba6b758SLuiz Augusto von Dentz bool connectable;
1231cba6b758SLuiz Augusto von Dentz u32 flags;
1232cba6b758SLuiz Augusto von Dentz bdaddr_t random_addr;
1233cba6b758SLuiz Augusto von Dentz u8 own_addr_type;
1234cba6b758SLuiz Augusto von Dentz int err;
1235cba6b758SLuiz Augusto von Dentz struct adv_info *adv;
1236cba6b758SLuiz Augusto von Dentz bool secondary_adv;
1237cba6b758SLuiz Augusto von Dentz
1238cba6b758SLuiz Augusto von Dentz if (instance > 0) {
1239cba6b758SLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance);
1240cba6b758SLuiz Augusto von Dentz if (!adv)
1241cba6b758SLuiz Augusto von Dentz return -EINVAL;
1242cba6b758SLuiz Augusto von Dentz } else {
1243cba6b758SLuiz Augusto von Dentz adv = NULL;
1244cba6b758SLuiz Augusto von Dentz }
1245cba6b758SLuiz Augusto von Dentz
1246cba6b758SLuiz Augusto von Dentz /* Updating parameters of an active instance will return a
1247cba6b758SLuiz Augusto von Dentz * Command Disallowed error, so we must first disable the
1248cba6b758SLuiz Augusto von Dentz * instance if it is active.
1249cba6b758SLuiz Augusto von Dentz */
1250cba6b758SLuiz Augusto von Dentz if (adv && !adv->pending) {
1251cba6b758SLuiz Augusto von Dentz err = hci_disable_ext_adv_instance_sync(hdev, instance);
1252cba6b758SLuiz Augusto von Dentz if (err)
1253cba6b758SLuiz Augusto von Dentz return err;
1254cba6b758SLuiz Augusto von Dentz }
1255cba6b758SLuiz Augusto von Dentz
1256cba6b758SLuiz Augusto von Dentz flags = hci_adv_instance_flags(hdev, instance);
1257cba6b758SLuiz Augusto von Dentz
1258cba6b758SLuiz Augusto von Dentz /* If the "connectable" instance flag was not set, then choose between
1259cba6b758SLuiz Augusto von Dentz * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1260cba6b758SLuiz Augusto von Dentz */
1261cba6b758SLuiz Augusto von Dentz connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1262cba6b758SLuiz Augusto von Dentz mgmt_get_connectable(hdev);
1263cba6b758SLuiz Augusto von Dentz
1264cba6b758SLuiz Augusto von Dentz if (!is_advertising_allowed(hdev, connectable))
1265cba6b758SLuiz Augusto von Dentz return -EPERM;
1266cba6b758SLuiz Augusto von Dentz
1267cba6b758SLuiz Augusto von Dentz /* Set require_privacy to true only when non-connectable
1268cba6b758SLuiz Augusto von Dentz * advertising is used. In that case it is fine to use a
1269cba6b758SLuiz Augusto von Dentz * non-resolvable private address.
1270cba6b758SLuiz Augusto von Dentz */
1271cba6b758SLuiz Augusto von Dentz err = hci_get_random_address(hdev, !connectable,
1272cba6b758SLuiz Augusto von Dentz adv_use_rpa(hdev, flags), adv,
1273cba6b758SLuiz Augusto von Dentz &own_addr_type, &random_addr);
1274cba6b758SLuiz Augusto von Dentz if (err < 0)
1275cba6b758SLuiz Augusto von Dentz return err;
1276cba6b758SLuiz Augusto von Dentz
1277cba6b758SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
1278cba6b758SLuiz Augusto von Dentz
1279cba6b758SLuiz Augusto von Dentz if (adv) {
1280cba6b758SLuiz Augusto von Dentz hci_cpu_to_le24(adv->min_interval, cp.min_interval);
1281cba6b758SLuiz Augusto von Dentz hci_cpu_to_le24(adv->max_interval, cp.max_interval);
1282cba6b758SLuiz Augusto von Dentz cp.tx_power = adv->tx_power;
1283cba6b758SLuiz Augusto von Dentz } else {
1284cba6b758SLuiz Augusto von Dentz hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1285cba6b758SLuiz Augusto von Dentz hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1286cba6b758SLuiz Augusto von Dentz cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
1287cba6b758SLuiz Augusto von Dentz }
1288cba6b758SLuiz Augusto von Dentz
1289cba6b758SLuiz Augusto von Dentz secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
1290cba6b758SLuiz Augusto von Dentz
1291cba6b758SLuiz Augusto von Dentz if (connectable) {
1292cba6b758SLuiz Augusto von Dentz if (secondary_adv)
1293cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
1294cba6b758SLuiz Augusto von Dentz else
1295cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
1296cba6b758SLuiz Augusto von Dentz } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1297cba6b758SLuiz Augusto von Dentz (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
1298cba6b758SLuiz Augusto von Dentz if (secondary_adv)
1299cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
1300cba6b758SLuiz Augusto von Dentz else
1301cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
1302cba6b758SLuiz Augusto von Dentz } else {
1303cba6b758SLuiz Augusto von Dentz if (secondary_adv)
1304cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
1305cba6b758SLuiz Augusto von Dentz else
1306cba6b758SLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
1307cba6b758SLuiz Augusto von Dentz }
1308cba6b758SLuiz Augusto von Dentz
1309cf75ad8bSLuiz Augusto von Dentz /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
1310cf75ad8bSLuiz Augusto von Dentz * contains the peer’s Identity Address and the Peer_Address_Type
1311cf75ad8bSLuiz Augusto von Dentz * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
1312cf75ad8bSLuiz Augusto von Dentz * These parameters are used to locate the corresponding local IRK in
1313cf75ad8bSLuiz Augusto von Dentz * the resolving list; this IRK is used to generate their own address
1314cf75ad8bSLuiz Augusto von Dentz * used in the advertisement.
1315cf75ad8bSLuiz Augusto von Dentz */
1316cf75ad8bSLuiz Augusto von Dentz if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
1317cf75ad8bSLuiz Augusto von Dentz hci_copy_identity_address(hdev, &cp.peer_addr,
1318cf75ad8bSLuiz Augusto von Dentz &cp.peer_addr_type);
1319cf75ad8bSLuiz Augusto von Dentz
1320cba6b758SLuiz Augusto von Dentz cp.own_addr_type = own_addr_type;
1321cba6b758SLuiz Augusto von Dentz cp.channel_map = hdev->le_adv_channel_map;
1322cba6b758SLuiz Augusto von Dentz cp.handle = instance;
1323cba6b758SLuiz Augusto von Dentz
1324cba6b758SLuiz Augusto von Dentz if (flags & MGMT_ADV_FLAG_SEC_2M) {
1325cba6b758SLuiz Augusto von Dentz cp.primary_phy = HCI_ADV_PHY_1M;
1326cba6b758SLuiz Augusto von Dentz cp.secondary_phy = HCI_ADV_PHY_2M;
1327cba6b758SLuiz Augusto von Dentz } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
1328cba6b758SLuiz Augusto von Dentz cp.primary_phy = HCI_ADV_PHY_CODED;
1329cba6b758SLuiz Augusto von Dentz cp.secondary_phy = HCI_ADV_PHY_CODED;
1330cba6b758SLuiz Augusto von Dentz } else {
1331cba6b758SLuiz Augusto von Dentz /* In all other cases use 1M */
1332cba6b758SLuiz Augusto von Dentz cp.primary_phy = HCI_ADV_PHY_1M;
1333cba6b758SLuiz Augusto von Dentz cp.secondary_phy = HCI_ADV_PHY_1M;
1334cba6b758SLuiz Augusto von Dentz }
1335cba6b758SLuiz Augusto von Dentz
1336cba6b758SLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1337cba6b758SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1338cba6b758SLuiz Augusto von Dentz if (err)
1339cba6b758SLuiz Augusto von Dentz return err;
1340cba6b758SLuiz Augusto von Dentz
1341cba6b758SLuiz Augusto von Dentz if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
1342cba6b758SLuiz Augusto von Dentz own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
1343cba6b758SLuiz Augusto von Dentz bacmp(&random_addr, BDADDR_ANY)) {
1344cba6b758SLuiz Augusto von Dentz /* Check if random address need to be updated */
1345cba6b758SLuiz Augusto von Dentz if (adv) {
1346cba6b758SLuiz Augusto von Dentz if (!bacmp(&random_addr, &adv->random_addr))
1347cba6b758SLuiz Augusto von Dentz return 0;
1348cba6b758SLuiz Augusto von Dentz } else {
1349cba6b758SLuiz Augusto von Dentz if (!bacmp(&random_addr, &hdev->random_addr))
1350cba6b758SLuiz Augusto von Dentz return 0;
1351cba6b758SLuiz Augusto von Dentz }
1352cba6b758SLuiz Augusto von Dentz
1353cba6b758SLuiz Augusto von Dentz return hci_set_adv_set_random_addr_sync(hdev, instance,
1354cba6b758SLuiz Augusto von Dentz &random_addr);
1355cba6b758SLuiz Augusto von Dentz }
1356cba6b758SLuiz Augusto von Dentz
1357cba6b758SLuiz Augusto von Dentz return 0;
1358cba6b758SLuiz Augusto von Dentz }
hci_set_ext_scan_rsp_data_sync(struct hci_dev * hdev,u8 instance)1359cba6b758SLuiz Augusto von Dentz
1360cba6b758SLuiz Augusto von Dentz static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1361cba6b758SLuiz Augusto von Dentz {
1362cba6b758SLuiz Augusto von Dentz struct {
1363cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_ext_scan_rsp_data cp;
1364cba6b758SLuiz Augusto von Dentz u8 data[HCI_MAX_EXT_AD_LENGTH];
1365cba6b758SLuiz Augusto von Dentz } pdu;
1366cba6b758SLuiz Augusto von Dentz u8 len;
136734a718bcSLuiz Augusto von Dentz struct adv_info *adv = NULL;
136834a718bcSLuiz Augusto von Dentz int err;
1369cba6b758SLuiz Augusto von Dentz
1370cba6b758SLuiz Augusto von Dentz memset(&pdu, 0, sizeof(pdu));
1371cba6b758SLuiz Augusto von Dentz
137234a718bcSLuiz Augusto von Dentz if (instance) {
137334a718bcSLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance);
137434a718bcSLuiz Augusto von Dentz if (!adv || !adv->scan_rsp_changed)
1375cba6b758SLuiz Augusto von Dentz return 0;
137634a718bcSLuiz Augusto von Dentz }
1377cba6b758SLuiz Augusto von Dentz
137834a718bcSLuiz Augusto von Dentz len = eir_create_scan_rsp(hdev, instance, pdu.data);
1379cba6b758SLuiz Augusto von Dentz
1380cba6b758SLuiz Augusto von Dentz pdu.cp.handle = instance;
1381cba6b758SLuiz Augusto von Dentz pdu.cp.length = len;
1382cba6b758SLuiz Augusto von Dentz pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1383cba6b758SLuiz Augusto von Dentz pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1384cba6b758SLuiz Augusto von Dentz
138534a718bcSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1386cba6b758SLuiz Augusto von Dentz sizeof(pdu.cp) + len, &pdu.cp,
1387cba6b758SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
138834a718bcSLuiz Augusto von Dentz if (err)
138934a718bcSLuiz Augusto von Dentz return err;
139034a718bcSLuiz Augusto von Dentz
139134a718bcSLuiz Augusto von Dentz if (adv) {
139234a718bcSLuiz Augusto von Dentz adv->scan_rsp_changed = false;
139334a718bcSLuiz Augusto von Dentz } else {
139434a718bcSLuiz Augusto von Dentz memcpy(hdev->scan_rsp_data, pdu.data, len);
139534a718bcSLuiz Augusto von Dentz hdev->scan_rsp_data_len = len;
139634a718bcSLuiz Augusto von Dentz }
139734a718bcSLuiz Augusto von Dentz
139834a718bcSLuiz Augusto von Dentz return 0;
1399cba6b758SLuiz Augusto von Dentz }
__hci_set_scan_rsp_data_sync(struct hci_dev * hdev,u8 instance)1400cba6b758SLuiz Augusto von Dentz
1401cba6b758SLuiz Augusto von Dentz static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1402cba6b758SLuiz Augusto von Dentz {
1403cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_scan_rsp_data cp;
1404cba6b758SLuiz Augusto von Dentz u8 len;
1405cba6b758SLuiz Augusto von Dentz
1406cba6b758SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
1407cba6b758SLuiz Augusto von Dentz
1408cba6b758SLuiz Augusto von Dentz len = eir_create_scan_rsp(hdev, instance, cp.data);
1409cba6b758SLuiz Augusto von Dentz
1410cba6b758SLuiz Augusto von Dentz if (hdev->scan_rsp_data_len == len &&
1411cba6b758SLuiz Augusto von Dentz !memcmp(cp.data, hdev->scan_rsp_data, len))
1412cba6b758SLuiz Augusto von Dentz return 0;
1413cba6b758SLuiz Augusto von Dentz
1414cba6b758SLuiz Augusto von Dentz memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1415cba6b758SLuiz Augusto von Dentz hdev->scan_rsp_data_len = len;
1416cba6b758SLuiz Augusto von Dentz
1417cba6b758SLuiz Augusto von Dentz cp.length = len;
1418cba6b758SLuiz Augusto von Dentz
1419cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1420cba6b758SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1421cba6b758SLuiz Augusto von Dentz }
hci_update_scan_rsp_data_sync(struct hci_dev * hdev,u8 instance)1422cba6b758SLuiz Augusto von Dentz
1423cba6b758SLuiz Augusto von Dentz int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1424cba6b758SLuiz Augusto von Dentz {
1425cba6b758SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1426cba6b758SLuiz Augusto von Dentz return 0;
1427cba6b758SLuiz Augusto von Dentz
1428cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev))
1429cba6b758SLuiz Augusto von Dentz return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1430cba6b758SLuiz Augusto von Dentz
1431cba6b758SLuiz Augusto von Dentz return __hci_set_scan_rsp_data_sync(hdev, instance);
1432cba6b758SLuiz Augusto von Dentz }
hci_enable_ext_advertising_sync(struct hci_dev * hdev,u8 instance)1433cba6b758SLuiz Augusto von Dentz
1434cba6b758SLuiz Augusto von Dentz int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1435cba6b758SLuiz Augusto von Dentz {
1436cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_ext_adv_enable *cp;
1437cba6b758SLuiz Augusto von Dentz struct hci_cp_ext_adv_set *set;
1438cba6b758SLuiz Augusto von Dentz u8 data[sizeof(*cp) + sizeof(*set) * 1];
1439cba6b758SLuiz Augusto von Dentz struct adv_info *adv;
1440cba6b758SLuiz Augusto von Dentz
1441cba6b758SLuiz Augusto von Dentz if (instance > 0) {
1442cba6b758SLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance);
1443cba6b758SLuiz Augusto von Dentz if (!adv)
1444cba6b758SLuiz Augusto von Dentz return -EINVAL;
1445cba6b758SLuiz Augusto von Dentz /* If already enabled there is nothing to do */
1446cba6b758SLuiz Augusto von Dentz if (adv->enabled)
1447cba6b758SLuiz Augusto von Dentz return 0;
1448cba6b758SLuiz Augusto von Dentz } else {
1449cba6b758SLuiz Augusto von Dentz adv = NULL;
1450cba6b758SLuiz Augusto von Dentz }
1451cba6b758SLuiz Augusto von Dentz
1452cba6b758SLuiz Augusto von Dentz cp = (void *)data;
1453cba6b758SLuiz Augusto von Dentz set = (void *)cp->data;
1454cba6b758SLuiz Augusto von Dentz
1455cba6b758SLuiz Augusto von Dentz memset(cp, 0, sizeof(*cp));
1456cba6b758SLuiz Augusto von Dentz
1457cba6b758SLuiz Augusto von Dentz cp->enable = 0x01;
1458cba6b758SLuiz Augusto von Dentz cp->num_of_sets = 0x01;
1459cba6b758SLuiz Augusto von Dentz
1460cba6b758SLuiz Augusto von Dentz memset(set, 0, sizeof(*set));
1461cba6b758SLuiz Augusto von Dentz
1462cba6b758SLuiz Augusto von Dentz set->handle = instance;
1463cba6b758SLuiz Augusto von Dentz
1464cba6b758SLuiz Augusto von Dentz /* Set duration per instance since controller is responsible for
1465cba6b758SLuiz Augusto von Dentz * scheduling it.
1466cba6b758SLuiz Augusto von Dentz */
1467f16a491cSLuiz Augusto von Dentz if (adv && adv->timeout) {
1468cba6b758SLuiz Augusto von Dentz u16 duration = adv->timeout * MSEC_PER_SEC;
1469cba6b758SLuiz Augusto von Dentz
1470cba6b758SLuiz Augusto von Dentz /* Time = N * 10 ms */
1471cba6b758SLuiz Augusto von Dentz set->duration = cpu_to_le16(duration / 10);
1472cba6b758SLuiz Augusto von Dentz }
1473cba6b758SLuiz Augusto von Dentz
1474cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1475cba6b758SLuiz Augusto von Dentz sizeof(*cp) +
1476cba6b758SLuiz Augusto von Dentz sizeof(*set) * cp->num_of_sets,
1477cba6b758SLuiz Augusto von Dentz data, HCI_CMD_TIMEOUT);
1478cba6b758SLuiz Augusto von Dentz }
hci_start_ext_adv_sync(struct hci_dev * hdev,u8 instance)1479cba6b758SLuiz Augusto von Dentz
1480cba6b758SLuiz Augusto von Dentz int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1481cba6b758SLuiz Augusto von Dentz {
1482cba6b758SLuiz Augusto von Dentz int err;
1483cba6b758SLuiz Augusto von Dentz
1484cba6b758SLuiz Augusto von Dentz err = hci_setup_ext_adv_instance_sync(hdev, instance);
1485cba6b758SLuiz Augusto von Dentz if (err)
1486cba6b758SLuiz Augusto von Dentz return err;
1487cba6b758SLuiz Augusto von Dentz
1488cba6b758SLuiz Augusto von Dentz err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1489cba6b758SLuiz Augusto von Dentz if (err)
1490cba6b758SLuiz Augusto von Dentz return err;
1491cba6b758SLuiz Augusto von Dentz
1492cba6b758SLuiz Augusto von Dentz return hci_enable_ext_advertising_sync(hdev, instance);
1493cba6b758SLuiz Augusto von Dentz }
hci_disable_per_advertising_sync(struct hci_dev * hdev,u8 instance)1494cba6b758SLuiz Augusto von Dentz
1495f03d3322SIulia Tanasescu int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1496eca0ae4aSLuiz Augusto von Dentz {
1497eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_set_per_adv_enable cp;
14986a42e9bfSIulia Tanasescu struct adv_info *adv = NULL;
1499eca0ae4aSLuiz Augusto von Dentz
1500eca0ae4aSLuiz Augusto von Dentz /* If periodic advertising already disabled there is nothing to do. */
15016a42e9bfSIulia Tanasescu adv = hci_find_adv_instance(hdev, instance);
15026a42e9bfSIulia Tanasescu if (!adv || !adv->periodic || !adv->enabled)
1503eca0ae4aSLuiz Augusto von Dentz return 0;
1504eca0ae4aSLuiz Augusto von Dentz
1505eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
1506eca0ae4aSLuiz Augusto von Dentz
1507eca0ae4aSLuiz Augusto von Dentz cp.enable = 0x00;
1508eca0ae4aSLuiz Augusto von Dentz cp.handle = instance;
1509eca0ae4aSLuiz Augusto von Dentz
1510eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1511eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1512eca0ae4aSLuiz Augusto von Dentz }
hci_set_per_adv_params_sync(struct hci_dev * hdev,u8 instance,u16 min_interval,u16 max_interval)1513eca0ae4aSLuiz Augusto von Dentz
1514eca0ae4aSLuiz Augusto von Dentz static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1515eca0ae4aSLuiz Augusto von Dentz u16 min_interval, u16 max_interval)
1516eca0ae4aSLuiz Augusto von Dentz {
1517eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_set_per_adv_params cp;
1518eca0ae4aSLuiz Augusto von Dentz
1519eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
1520eca0ae4aSLuiz Augusto von Dentz
1521eca0ae4aSLuiz Augusto von Dentz if (!min_interval)
1522eca0ae4aSLuiz Augusto von Dentz min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1523eca0ae4aSLuiz Augusto von Dentz
1524eca0ae4aSLuiz Augusto von Dentz if (!max_interval)
1525eca0ae4aSLuiz Augusto von Dentz max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1526eca0ae4aSLuiz Augusto von Dentz
1527eca0ae4aSLuiz Augusto von Dentz cp.handle = instance;
1528eca0ae4aSLuiz Augusto von Dentz cp.min_interval = cpu_to_le16(min_interval);
1529eca0ae4aSLuiz Augusto von Dentz cp.max_interval = cpu_to_le16(max_interval);
1530eca0ae4aSLuiz Augusto von Dentz cp.periodic_properties = 0x0000;
1531eca0ae4aSLuiz Augusto von Dentz
1532eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1533eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1534eca0ae4aSLuiz Augusto von Dentz }
hci_set_per_adv_data_sync(struct hci_dev * hdev,u8 instance)1535eca0ae4aSLuiz Augusto von Dentz
1536eca0ae4aSLuiz Augusto von Dentz static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1537eca0ae4aSLuiz Augusto von Dentz {
1538eca0ae4aSLuiz Augusto von Dentz struct {
1539eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_set_per_adv_data cp;
1540eca0ae4aSLuiz Augusto von Dentz u8 data[HCI_MAX_PER_AD_LENGTH];
1541eca0ae4aSLuiz Augusto von Dentz } pdu;
1542eca0ae4aSLuiz Augusto von Dentz u8 len;
1543eca0ae4aSLuiz Augusto von Dentz
1544eca0ae4aSLuiz Augusto von Dentz memset(&pdu, 0, sizeof(pdu));
1545eca0ae4aSLuiz Augusto von Dentz
1546eca0ae4aSLuiz Augusto von Dentz if (instance) {
1547eca0ae4aSLuiz Augusto von Dentz struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1548eca0ae4aSLuiz Augusto von Dentz
1549eca0ae4aSLuiz Augusto von Dentz if (!adv || !adv->periodic)
1550eca0ae4aSLuiz Augusto von Dentz return 0;
1551eca0ae4aSLuiz Augusto von Dentz }
1552eca0ae4aSLuiz Augusto von Dentz
1553eca0ae4aSLuiz Augusto von Dentz len = eir_create_per_adv_data(hdev, instance, pdu.data);
1554eca0ae4aSLuiz Augusto von Dentz
1555eca0ae4aSLuiz Augusto von Dentz pdu.cp.length = len;
1556eca0ae4aSLuiz Augusto von Dentz pdu.cp.handle = instance;
1557eca0ae4aSLuiz Augusto von Dentz pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1558eca0ae4aSLuiz Augusto von Dentz
1559eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1560eca0ae4aSLuiz Augusto von Dentz sizeof(pdu.cp) + len, &pdu,
1561eca0ae4aSLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
1562eca0ae4aSLuiz Augusto von Dentz }
hci_enable_per_advertising_sync(struct hci_dev * hdev,u8 instance)1563eca0ae4aSLuiz Augusto von Dentz
1564eca0ae4aSLuiz Augusto von Dentz static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1565eca0ae4aSLuiz Augusto von Dentz {
1566eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_set_per_adv_enable cp;
15676a42e9bfSIulia Tanasescu struct adv_info *adv = NULL;
1568eca0ae4aSLuiz Augusto von Dentz
1569eca0ae4aSLuiz Augusto von Dentz /* If periodic advertising already enabled there is nothing to do. */
15706a42e9bfSIulia Tanasescu adv = hci_find_adv_instance(hdev, instance);
15716a42e9bfSIulia Tanasescu if (adv && adv->periodic && adv->enabled)
1572eca0ae4aSLuiz Augusto von Dentz return 0;
1573eca0ae4aSLuiz Augusto von Dentz
1574eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
1575eca0ae4aSLuiz Augusto von Dentz
1576eca0ae4aSLuiz Augusto von Dentz cp.enable = 0x01;
1577eca0ae4aSLuiz Augusto von Dentz cp.handle = instance;
1578eca0ae4aSLuiz Augusto von Dentz
1579eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1580eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1581eca0ae4aSLuiz Augusto von Dentz }
1582eca0ae4aSLuiz Augusto von Dentz
1583eca0ae4aSLuiz Augusto von Dentz /* Checks if periodic advertising data contains a Basic Announcement and if it
1584eca0ae4aSLuiz Augusto von Dentz * does generates a Broadcast ID and add Broadcast Announcement.
hci_adv_bcast_annoucement(struct hci_dev * hdev,struct adv_info * adv)1585eca0ae4aSLuiz Augusto von Dentz */
1586eca0ae4aSLuiz Augusto von Dentz static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1587eca0ae4aSLuiz Augusto von Dentz {
1588eca0ae4aSLuiz Augusto von Dentz u8 bid[3];
1589eca0ae4aSLuiz Augusto von Dentz u8 ad[4 + 3];
1590eca0ae4aSLuiz Augusto von Dentz
1591eca0ae4aSLuiz Augusto von Dentz /* Skip if NULL adv as instance 0x00 is used for general purpose
1592eca0ae4aSLuiz Augusto von Dentz * advertising so it cannot used for the likes of Broadcast Announcement
1593eca0ae4aSLuiz Augusto von Dentz * as it can be overwritten at any point.
1594eca0ae4aSLuiz Augusto von Dentz */
1595eca0ae4aSLuiz Augusto von Dentz if (!adv)
1596eca0ae4aSLuiz Augusto von Dentz return 0;
1597eca0ae4aSLuiz Augusto von Dentz
1598eca0ae4aSLuiz Augusto von Dentz /* Check if PA data doesn't contains a Basic Audio Announcement then
1599eca0ae4aSLuiz Augusto von Dentz * there is nothing to do.
1600eca0ae4aSLuiz Augusto von Dentz */
1601eca0ae4aSLuiz Augusto von Dentz if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1602eca0ae4aSLuiz Augusto von Dentz 0x1851, NULL))
1603eca0ae4aSLuiz Augusto von Dentz return 0;
1604eca0ae4aSLuiz Augusto von Dentz
1605eca0ae4aSLuiz Augusto von Dentz /* Check if advertising data already has a Broadcast Announcement since
1606eca0ae4aSLuiz Augusto von Dentz * the process may want to control the Broadcast ID directly and in that
1607eca0ae4aSLuiz Augusto von Dentz * case the kernel shall no interfere.
1608eca0ae4aSLuiz Augusto von Dentz */
1609eca0ae4aSLuiz Augusto von Dentz if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1610eca0ae4aSLuiz Augusto von Dentz NULL))
1611eca0ae4aSLuiz Augusto von Dentz return 0;
1612eca0ae4aSLuiz Augusto von Dentz
1613eca0ae4aSLuiz Augusto von Dentz /* Generate Broadcast ID */
1614eca0ae4aSLuiz Augusto von Dentz get_random_bytes(bid, sizeof(bid));
1615eca0ae4aSLuiz Augusto von Dentz eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1616eca0ae4aSLuiz Augusto von Dentz hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1617eca0ae4aSLuiz Augusto von Dentz
1618eca0ae4aSLuiz Augusto von Dentz return hci_update_adv_data_sync(hdev, adv->instance);
1619eca0ae4aSLuiz Augusto von Dentz }
hci_start_per_adv_sync(struct hci_dev * hdev,u8 instance,u8 data_len,u8 * data,u32 flags,u16 min_interval,u16 max_interval,u16 sync_interval)1620eca0ae4aSLuiz Augusto von Dentz
1621eca0ae4aSLuiz Augusto von Dentz int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1622eca0ae4aSLuiz Augusto von Dentz u8 *data, u32 flags, u16 min_interval,
1623eca0ae4aSLuiz Augusto von Dentz u16 max_interval, u16 sync_interval)
1624eca0ae4aSLuiz Augusto von Dentz {
1625eca0ae4aSLuiz Augusto von Dentz struct adv_info *adv = NULL;
1626eca0ae4aSLuiz Augusto von Dentz int err;
1627eca0ae4aSLuiz Augusto von Dentz bool added = false;
1628eca0ae4aSLuiz Augusto von Dentz
1629eca0ae4aSLuiz Augusto von Dentz hci_disable_per_advertising_sync(hdev, instance);
1630eca0ae4aSLuiz Augusto von Dentz
1631eca0ae4aSLuiz Augusto von Dentz if (instance) {
1632eca0ae4aSLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance);
1633eca0ae4aSLuiz Augusto von Dentz /* Create an instance if that could not be found */
1634eca0ae4aSLuiz Augusto von Dentz if (!adv) {
1635eca0ae4aSLuiz Augusto von Dentz adv = hci_add_per_instance(hdev, instance, flags,
1636eca0ae4aSLuiz Augusto von Dentz data_len, data,
1637eca0ae4aSLuiz Augusto von Dentz sync_interval,
1638eca0ae4aSLuiz Augusto von Dentz sync_interval);
1639eca0ae4aSLuiz Augusto von Dentz if (IS_ERR(adv))
1640eca0ae4aSLuiz Augusto von Dentz return PTR_ERR(adv);
16416a42e9bfSIulia Tanasescu adv->pending = false;
1642eca0ae4aSLuiz Augusto von Dentz added = true;
1643eca0ae4aSLuiz Augusto von Dentz }
1644eca0ae4aSLuiz Augusto von Dentz }
1645eca0ae4aSLuiz Augusto von Dentz
16466a42e9bfSIulia Tanasescu /* Start advertising */
1647eca0ae4aSLuiz Augusto von Dentz err = hci_start_ext_adv_sync(hdev, instance);
1648eca0ae4aSLuiz Augusto von Dentz if (err < 0)
1649eca0ae4aSLuiz Augusto von Dentz goto fail;
1650eca0ae4aSLuiz Augusto von Dentz
1651eca0ae4aSLuiz Augusto von Dentz err = hci_adv_bcast_annoucement(hdev, adv);
1652eca0ae4aSLuiz Augusto von Dentz if (err < 0)
1653eca0ae4aSLuiz Augusto von Dentz goto fail;
1654eca0ae4aSLuiz Augusto von Dentz
1655eca0ae4aSLuiz Augusto von Dentz err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1656eca0ae4aSLuiz Augusto von Dentz max_interval);
1657eca0ae4aSLuiz Augusto von Dentz if (err < 0)
1658eca0ae4aSLuiz Augusto von Dentz goto fail;
1659eca0ae4aSLuiz Augusto von Dentz
1660eca0ae4aSLuiz Augusto von Dentz err = hci_set_per_adv_data_sync(hdev, instance);
1661eca0ae4aSLuiz Augusto von Dentz if (err < 0)
1662eca0ae4aSLuiz Augusto von Dentz goto fail;
1663eca0ae4aSLuiz Augusto von Dentz
1664eca0ae4aSLuiz Augusto von Dentz err = hci_enable_per_advertising_sync(hdev, instance);
1665eca0ae4aSLuiz Augusto von Dentz if (err < 0)
1666eca0ae4aSLuiz Augusto von Dentz goto fail;
1667eca0ae4aSLuiz Augusto von Dentz
1668eca0ae4aSLuiz Augusto von Dentz return 0;
1669eca0ae4aSLuiz Augusto von Dentz
1670eca0ae4aSLuiz Augusto von Dentz fail:
1671eca0ae4aSLuiz Augusto von Dentz if (added)
1672eca0ae4aSLuiz Augusto von Dentz hci_remove_adv_instance(hdev, instance);
1673eca0ae4aSLuiz Augusto von Dentz
1674eca0ae4aSLuiz Augusto von Dentz return err;
1675eca0ae4aSLuiz Augusto von Dentz }
hci_start_adv_sync(struct hci_dev * hdev,u8 instance)1676eca0ae4aSLuiz Augusto von Dentz
1677cba6b758SLuiz Augusto von Dentz static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1678cba6b758SLuiz Augusto von Dentz {
1679cba6b758SLuiz Augusto von Dentz int err;
1680cba6b758SLuiz Augusto von Dentz
1681cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev))
1682cba6b758SLuiz Augusto von Dentz return hci_start_ext_adv_sync(hdev, instance);
1683cba6b758SLuiz Augusto von Dentz
1684cba6b758SLuiz Augusto von Dentz err = hci_update_adv_data_sync(hdev, instance);
1685cba6b758SLuiz Augusto von Dentz if (err)
1686cba6b758SLuiz Augusto von Dentz return err;
1687cba6b758SLuiz Augusto von Dentz
1688cba6b758SLuiz Augusto von Dentz err = hci_update_scan_rsp_data_sync(hdev, instance);
1689cba6b758SLuiz Augusto von Dentz if (err)
1690cba6b758SLuiz Augusto von Dentz return err;
1691cba6b758SLuiz Augusto von Dentz
1692cba6b758SLuiz Augusto von Dentz return hci_enable_advertising_sync(hdev);
1693cba6b758SLuiz Augusto von Dentz }
hci_enable_advertising_sync(struct hci_dev * hdev)1694cba6b758SLuiz Augusto von Dentz
1695cba6b758SLuiz Augusto von Dentz int hci_enable_advertising_sync(struct hci_dev *hdev)
1696cba6b758SLuiz Augusto von Dentz {
1697cba6b758SLuiz Augusto von Dentz struct adv_info *adv_instance;
1698cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_adv_param cp;
1699cba6b758SLuiz Augusto von Dentz u8 own_addr_type, enable = 0x01;
1700cba6b758SLuiz Augusto von Dentz bool connectable;
1701cba6b758SLuiz Augusto von Dentz u16 adv_min_interval, adv_max_interval;
1702cba6b758SLuiz Augusto von Dentz u32 flags;
1703cba6b758SLuiz Augusto von Dentz u8 status;
1704cba6b758SLuiz Augusto von Dentz
1705ad383c2cSLuiz Augusto von Dentz if (ext_adv_capable(hdev))
1706ad383c2cSLuiz Augusto von Dentz return hci_enable_ext_advertising_sync(hdev,
1707ad383c2cSLuiz Augusto von Dentz hdev->cur_adv_instance);
1708ad383c2cSLuiz Augusto von Dentz
1709cba6b758SLuiz Augusto von Dentz flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1710cba6b758SLuiz Augusto von Dentz adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1711cba6b758SLuiz Augusto von Dentz
1712cba6b758SLuiz Augusto von Dentz /* If the "connectable" instance flag was not set, then choose between
1713cba6b758SLuiz Augusto von Dentz * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1714cba6b758SLuiz Augusto von Dentz */
1715cba6b758SLuiz Augusto von Dentz connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1716cba6b758SLuiz Augusto von Dentz mgmt_get_connectable(hdev);
1717cba6b758SLuiz Augusto von Dentz
1718cba6b758SLuiz Augusto von Dentz if (!is_advertising_allowed(hdev, connectable))
1719cba6b758SLuiz Augusto von Dentz return -EINVAL;
1720cba6b758SLuiz Augusto von Dentz
1721cba6b758SLuiz Augusto von Dentz status = hci_disable_advertising_sync(hdev);
1722cba6b758SLuiz Augusto von Dentz if (status)
1723cba6b758SLuiz Augusto von Dentz return status;
1724cba6b758SLuiz Augusto von Dentz
1725cba6b758SLuiz Augusto von Dentz /* Clear the HCI_LE_ADV bit temporarily so that the
1726cba6b758SLuiz Augusto von Dentz * hci_update_random_address knows that it's safe to go ahead
1727cba6b758SLuiz Augusto von Dentz * and write a new random address. The flag will be set back on
1728cba6b758SLuiz Augusto von Dentz * as soon as the SET_ADV_ENABLE HCI command completes.
1729cba6b758SLuiz Augusto von Dentz */
1730cba6b758SLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_LE_ADV);
1731cba6b758SLuiz Augusto von Dentz
1732cba6b758SLuiz Augusto von Dentz /* Set require_privacy to true only when non-connectable
1733cba6b758SLuiz Augusto von Dentz * advertising is used. In that case it is fine to use a
1734cba6b758SLuiz Augusto von Dentz * non-resolvable private address.
1735cba6b758SLuiz Augusto von Dentz */
1736cba6b758SLuiz Augusto von Dentz status = hci_update_random_address_sync(hdev, !connectable,
1737cba6b758SLuiz Augusto von Dentz adv_use_rpa(hdev, flags),
1738cba6b758SLuiz Augusto von Dentz &own_addr_type);
1739cba6b758SLuiz Augusto von Dentz if (status)
1740cba6b758SLuiz Augusto von Dentz return status;
1741cba6b758SLuiz Augusto von Dentz
1742cba6b758SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
1743cba6b758SLuiz Augusto von Dentz
1744cba6b758SLuiz Augusto von Dentz if (adv_instance) {
1745cba6b758SLuiz Augusto von Dentz adv_min_interval = adv_instance->min_interval;
1746cba6b758SLuiz Augusto von Dentz adv_max_interval = adv_instance->max_interval;
1747cba6b758SLuiz Augusto von Dentz } else {
1748cba6b758SLuiz Augusto von Dentz adv_min_interval = hdev->le_adv_min_interval;
1749cba6b758SLuiz Augusto von Dentz adv_max_interval = hdev->le_adv_max_interval;
1750cba6b758SLuiz Augusto von Dentz }
1751cba6b758SLuiz Augusto von Dentz
1752cba6b758SLuiz Augusto von Dentz if (connectable) {
1753cba6b758SLuiz Augusto von Dentz cp.type = LE_ADV_IND;
1754cba6b758SLuiz Augusto von Dentz } else {
1755cba6b758SLuiz Augusto von Dentz if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1756cba6b758SLuiz Augusto von Dentz cp.type = LE_ADV_SCAN_IND;
1757cba6b758SLuiz Augusto von Dentz else
1758cba6b758SLuiz Augusto von Dentz cp.type = LE_ADV_NONCONN_IND;
1759cba6b758SLuiz Augusto von Dentz
1760cba6b758SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1761cba6b758SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1762cba6b758SLuiz Augusto von Dentz adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1763cba6b758SLuiz Augusto von Dentz adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1764cba6b758SLuiz Augusto von Dentz }
1765cba6b758SLuiz Augusto von Dentz }
1766cba6b758SLuiz Augusto von Dentz
1767cba6b758SLuiz Augusto von Dentz cp.min_interval = cpu_to_le16(adv_min_interval);
1768cba6b758SLuiz Augusto von Dentz cp.max_interval = cpu_to_le16(adv_max_interval);
1769cba6b758SLuiz Augusto von Dentz cp.own_address_type = own_addr_type;
1770cba6b758SLuiz Augusto von Dentz cp.channel_map = hdev->le_adv_channel_map;
1771cba6b758SLuiz Augusto von Dentz
1772cba6b758SLuiz Augusto von Dentz status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1773cba6b758SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1774cba6b758SLuiz Augusto von Dentz if (status)
1775cba6b758SLuiz Augusto von Dentz return status;
1776cba6b758SLuiz Augusto von Dentz
1777cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1778cba6b758SLuiz Augusto von Dentz sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1779cba6b758SLuiz Augusto von Dentz }
enable_advertising_sync(struct hci_dev * hdev,void * data)1780cba6b758SLuiz Augusto von Dentz
1781abfeea47SLuiz Augusto von Dentz static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1782abfeea47SLuiz Augusto von Dentz {
1783abfeea47SLuiz Augusto von Dentz return hci_enable_advertising_sync(hdev);
1784abfeea47SLuiz Augusto von Dentz }
hci_enable_advertising(struct hci_dev * hdev)1785abfeea47SLuiz Augusto von Dentz
1786abfeea47SLuiz Augusto von Dentz int hci_enable_advertising(struct hci_dev *hdev)
1787abfeea47SLuiz Augusto von Dentz {
1788abfeea47SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1789abfeea47SLuiz Augusto von Dentz list_empty(&hdev->adv_instances))
1790abfeea47SLuiz Augusto von Dentz return 0;
1791abfeea47SLuiz Augusto von Dentz
1792abfeea47SLuiz Augusto von Dentz return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1793abfeea47SLuiz Augusto von Dentz }
hci_remove_ext_adv_instance_sync(struct hci_dev * hdev,u8 instance,struct sock * sk)1794abfeea47SLuiz Augusto von Dentz
1795abfeea47SLuiz Augusto von Dentz int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1796cba6b758SLuiz Augusto von Dentz struct sock *sk)
1797cba6b758SLuiz Augusto von Dentz {
1798cba6b758SLuiz Augusto von Dentz int err;
1799cba6b758SLuiz Augusto von Dentz
1800cba6b758SLuiz Augusto von Dentz if (!ext_adv_capable(hdev))
1801cba6b758SLuiz Augusto von Dentz return 0;
1802cba6b758SLuiz Augusto von Dentz
1803cba6b758SLuiz Augusto von Dentz err = hci_disable_ext_adv_instance_sync(hdev, instance);
1804cba6b758SLuiz Augusto von Dentz if (err)
1805cba6b758SLuiz Augusto von Dentz return err;
1806cba6b758SLuiz Augusto von Dentz
1807cba6b758SLuiz Augusto von Dentz /* If request specifies an instance that doesn't exist, fail */
1808cba6b758SLuiz Augusto von Dentz if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1809cba6b758SLuiz Augusto von Dentz return -EINVAL;
1810cba6b758SLuiz Augusto von Dentz
1811cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1812cba6b758SLuiz Augusto von Dentz sizeof(instance), &instance, 0,
1813cba6b758SLuiz Augusto von Dentz HCI_CMD_TIMEOUT, sk);
1814cba6b758SLuiz Augusto von Dentz }
remove_ext_adv_sync(struct hci_dev * hdev,void * data)1815cba6b758SLuiz Augusto von Dentz
1816eca0ae4aSLuiz Augusto von Dentz static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1817eca0ae4aSLuiz Augusto von Dentz {
1818eca0ae4aSLuiz Augusto von Dentz struct adv_info *adv = data;
1819eca0ae4aSLuiz Augusto von Dentz u8 instance = 0;
1820eca0ae4aSLuiz Augusto von Dentz
1821eca0ae4aSLuiz Augusto von Dentz if (adv)
1822eca0ae4aSLuiz Augusto von Dentz instance = adv->instance;
1823eca0ae4aSLuiz Augusto von Dentz
1824eca0ae4aSLuiz Augusto von Dentz return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1825eca0ae4aSLuiz Augusto von Dentz }
hci_remove_ext_adv_instance(struct hci_dev * hdev,u8 instance)1826eca0ae4aSLuiz Augusto von Dentz
1827eca0ae4aSLuiz Augusto von Dentz int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1828eca0ae4aSLuiz Augusto von Dentz {
1829eca0ae4aSLuiz Augusto von Dentz struct adv_info *adv = NULL;
1830eca0ae4aSLuiz Augusto von Dentz
1831eca0ae4aSLuiz Augusto von Dentz if (instance) {
1832eca0ae4aSLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance);
1833eca0ae4aSLuiz Augusto von Dentz if (!adv)
1834eca0ae4aSLuiz Augusto von Dentz return -EINVAL;
1835eca0ae4aSLuiz Augusto von Dentz }
1836eca0ae4aSLuiz Augusto von Dentz
1837eca0ae4aSLuiz Augusto von Dentz return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1838eca0ae4aSLuiz Augusto von Dentz }
hci_le_terminate_big_sync(struct hci_dev * hdev,u8 handle,u8 reason)1839eca0ae4aSLuiz Augusto von Dentz
1840eca0ae4aSLuiz Augusto von Dentz int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1841eca0ae4aSLuiz Augusto von Dentz {
1842eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_term_big cp;
1843eca0ae4aSLuiz Augusto von Dentz
1844eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
1845eca0ae4aSLuiz Augusto von Dentz cp.handle = handle;
1846eca0ae4aSLuiz Augusto von Dentz cp.reason = reason;
1847eca0ae4aSLuiz Augusto von Dentz
1848eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1849eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1850eca0ae4aSLuiz Augusto von Dentz }
hci_set_ext_adv_data_sync(struct hci_dev * hdev,u8 instance)1851eca0ae4aSLuiz Augusto von Dentz
1852cba6b758SLuiz Augusto von Dentz static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1853cba6b758SLuiz Augusto von Dentz {
1854cba6b758SLuiz Augusto von Dentz struct {
1855cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_ext_adv_data cp;
1856cba6b758SLuiz Augusto von Dentz u8 data[HCI_MAX_EXT_AD_LENGTH];
1857cba6b758SLuiz Augusto von Dentz } pdu;
1858cba6b758SLuiz Augusto von Dentz u8 len;
185934a718bcSLuiz Augusto von Dentz struct adv_info *adv = NULL;
186034a718bcSLuiz Augusto von Dentz int err;
1861cba6b758SLuiz Augusto von Dentz
1862cba6b758SLuiz Augusto von Dentz memset(&pdu, 0, sizeof(pdu));
1863cba6b758SLuiz Augusto von Dentz
186434a718bcSLuiz Augusto von Dentz if (instance) {
186534a718bcSLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance);
186634a718bcSLuiz Augusto von Dentz if (!adv || !adv->adv_data_changed)
1867cba6b758SLuiz Augusto von Dentz return 0;
186834a718bcSLuiz Augusto von Dentz }
1869cba6b758SLuiz Augusto von Dentz
187034a718bcSLuiz Augusto von Dentz len = eir_create_adv_data(hdev, instance, pdu.data);
1871cba6b758SLuiz Augusto von Dentz
1872cba6b758SLuiz Augusto von Dentz pdu.cp.length = len;
1873cba6b758SLuiz Augusto von Dentz pdu.cp.handle = instance;
1874cba6b758SLuiz Augusto von Dentz pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1875cba6b758SLuiz Augusto von Dentz pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1876cba6b758SLuiz Augusto von Dentz
187734a718bcSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1878cba6b758SLuiz Augusto von Dentz sizeof(pdu.cp) + len, &pdu.cp,
1879cba6b758SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
188034a718bcSLuiz Augusto von Dentz if (err)
188134a718bcSLuiz Augusto von Dentz return err;
188234a718bcSLuiz Augusto von Dentz
188334a718bcSLuiz Augusto von Dentz /* Update data if the command succeed */
188434a718bcSLuiz Augusto von Dentz if (adv) {
188534a718bcSLuiz Augusto von Dentz adv->adv_data_changed = false;
188634a718bcSLuiz Augusto von Dentz } else {
188734a718bcSLuiz Augusto von Dentz memcpy(hdev->adv_data, pdu.data, len);
188834a718bcSLuiz Augusto von Dentz hdev->adv_data_len = len;
188934a718bcSLuiz Augusto von Dentz }
189034a718bcSLuiz Augusto von Dentz
189134a718bcSLuiz Augusto von Dentz return 0;
1892cba6b758SLuiz Augusto von Dentz }
hci_set_adv_data_sync(struct hci_dev * hdev,u8 instance)1893cba6b758SLuiz Augusto von Dentz
1894cba6b758SLuiz Augusto von Dentz static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1895cba6b758SLuiz Augusto von Dentz {
1896cba6b758SLuiz Augusto von Dentz struct hci_cp_le_set_adv_data cp;
1897cba6b758SLuiz Augusto von Dentz u8 len;
1898cba6b758SLuiz Augusto von Dentz
1899cba6b758SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
1900cba6b758SLuiz Augusto von Dentz
1901cba6b758SLuiz Augusto von Dentz len = eir_create_adv_data(hdev, instance, cp.data);
1902cba6b758SLuiz Augusto von Dentz
1903cba6b758SLuiz Augusto von Dentz /* There's nothing to do if the data hasn't changed */
1904cba6b758SLuiz Augusto von Dentz if (hdev->adv_data_len == len &&
1905cba6b758SLuiz Augusto von Dentz memcmp(cp.data, hdev->adv_data, len) == 0)
1906cba6b758SLuiz Augusto von Dentz return 0;
1907cba6b758SLuiz Augusto von Dentz
1908cba6b758SLuiz Augusto von Dentz memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1909cba6b758SLuiz Augusto von Dentz hdev->adv_data_len = len;
1910cba6b758SLuiz Augusto von Dentz
1911cba6b758SLuiz Augusto von Dentz cp.length = len;
1912cba6b758SLuiz Augusto von Dentz
1913cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1914cba6b758SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1915cba6b758SLuiz Augusto von Dentz }
hci_update_adv_data_sync(struct hci_dev * hdev,u8 instance)1916cba6b758SLuiz Augusto von Dentz
1917cba6b758SLuiz Augusto von Dentz int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1918cba6b758SLuiz Augusto von Dentz {
1919cba6b758SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1920cba6b758SLuiz Augusto von Dentz return 0;
1921cba6b758SLuiz Augusto von Dentz
1922cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev))
1923cba6b758SLuiz Augusto von Dentz return hci_set_ext_adv_data_sync(hdev, instance);
1924cba6b758SLuiz Augusto von Dentz
1925cba6b758SLuiz Augusto von Dentz return hci_set_adv_data_sync(hdev, instance);
1926cba6b758SLuiz Augusto von Dentz }
hci_schedule_adv_instance_sync(struct hci_dev * hdev,u8 instance,bool force)1927cba6b758SLuiz Augusto von Dentz
1928cba6b758SLuiz Augusto von Dentz int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1929cba6b758SLuiz Augusto von Dentz bool force)
1930cba6b758SLuiz Augusto von Dentz {
1931cba6b758SLuiz Augusto von Dentz struct adv_info *adv = NULL;
1932cba6b758SLuiz Augusto von Dentz u16 timeout;
1933cba6b758SLuiz Augusto von Dentz
1934cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1935cba6b758SLuiz Augusto von Dentz return -EPERM;
1936cba6b758SLuiz Augusto von Dentz
1937cba6b758SLuiz Augusto von Dentz if (hdev->adv_instance_timeout)
1938cba6b758SLuiz Augusto von Dentz return -EBUSY;
1939cba6b758SLuiz Augusto von Dentz
1940cba6b758SLuiz Augusto von Dentz adv = hci_find_adv_instance(hdev, instance);
1941cba6b758SLuiz Augusto von Dentz if (!adv)
1942cba6b758SLuiz Augusto von Dentz return -ENOENT;
1943cba6b758SLuiz Augusto von Dentz
1944cba6b758SLuiz Augusto von Dentz /* A zero timeout means unlimited advertising. As long as there is
1945cba6b758SLuiz Augusto von Dentz * only one instance, duration should be ignored. We still set a timeout
1946cba6b758SLuiz Augusto von Dentz * in case further instances are being added later on.
1947cba6b758SLuiz Augusto von Dentz *
1948cba6b758SLuiz Augusto von Dentz * If the remaining lifetime of the instance is more than the duration
1949cba6b758SLuiz Augusto von Dentz * then the timeout corresponds to the duration, otherwise it will be
1950cba6b758SLuiz Augusto von Dentz * reduced to the remaining instance lifetime.
1951cba6b758SLuiz Augusto von Dentz */
1952cba6b758SLuiz Augusto von Dentz if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1953cba6b758SLuiz Augusto von Dentz timeout = adv->duration;
1954cba6b758SLuiz Augusto von Dentz else
1955cba6b758SLuiz Augusto von Dentz timeout = adv->remaining_time;
1956cba6b758SLuiz Augusto von Dentz
1957cba6b758SLuiz Augusto von Dentz /* The remaining time is being reduced unless the instance is being
1958cba6b758SLuiz Augusto von Dentz * advertised without time limit.
1959cba6b758SLuiz Augusto von Dentz */
1960cba6b758SLuiz Augusto von Dentz if (adv->timeout)
1961cba6b758SLuiz Augusto von Dentz adv->remaining_time = adv->remaining_time - timeout;
1962cba6b758SLuiz Augusto von Dentz
1963cba6b758SLuiz Augusto von Dentz /* Only use work for scheduling instances with legacy advertising */
1964cba6b758SLuiz Augusto von Dentz if (!ext_adv_capable(hdev)) {
1965cba6b758SLuiz Augusto von Dentz hdev->adv_instance_timeout = timeout;
1966cba6b758SLuiz Augusto von Dentz queue_delayed_work(hdev->req_workqueue,
1967cba6b758SLuiz Augusto von Dentz &hdev->adv_instance_expire,
1968cba6b758SLuiz Augusto von Dentz msecs_to_jiffies(timeout * 1000));
1969cba6b758SLuiz Augusto von Dentz }
1970cba6b758SLuiz Augusto von Dentz
1971cba6b758SLuiz Augusto von Dentz /* If we're just re-scheduling the same instance again then do not
1972cba6b758SLuiz Augusto von Dentz * execute any HCI commands. This happens when a single instance is
1973cba6b758SLuiz Augusto von Dentz * being advertised.
1974cba6b758SLuiz Augusto von Dentz */
1975cba6b758SLuiz Augusto von Dentz if (!force && hdev->cur_adv_instance == instance &&
1976cba6b758SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_LE_ADV))
1977cba6b758SLuiz Augusto von Dentz return 0;
1978cba6b758SLuiz Augusto von Dentz
1979cba6b758SLuiz Augusto von Dentz hdev->cur_adv_instance = instance;
1980cba6b758SLuiz Augusto von Dentz
1981cba6b758SLuiz Augusto von Dentz return hci_start_adv_sync(hdev, instance);
1982cba6b758SLuiz Augusto von Dentz }
hci_clear_adv_sets_sync(struct hci_dev * hdev,struct sock * sk)1983cba6b758SLuiz Augusto von Dentz
1984cba6b758SLuiz Augusto von Dentz static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1985cba6b758SLuiz Augusto von Dentz {
1986cba6b758SLuiz Augusto von Dentz int err;
1987cba6b758SLuiz Augusto von Dentz
1988cba6b758SLuiz Augusto von Dentz if (!ext_adv_capable(hdev))
1989cba6b758SLuiz Augusto von Dentz return 0;
1990cba6b758SLuiz Augusto von Dentz
1991cba6b758SLuiz Augusto von Dentz /* Disable instance 0x00 to disable all instances */
1992cba6b758SLuiz Augusto von Dentz err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1993cba6b758SLuiz Augusto von Dentz if (err)
1994cba6b758SLuiz Augusto von Dentz return err;
1995cba6b758SLuiz Augusto von Dentz
1996cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1997cba6b758SLuiz Augusto von Dentz 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1998cba6b758SLuiz Augusto von Dentz }
hci_clear_adv_sync(struct hci_dev * hdev,struct sock * sk,bool force)1999cba6b758SLuiz Augusto von Dentz
2000cba6b758SLuiz Augusto von Dentz static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
2001cba6b758SLuiz Augusto von Dentz {
2002cba6b758SLuiz Augusto von Dentz struct adv_info *adv, *n;
2003b338d917SBrian Gix int err = 0;
2004cba6b758SLuiz Augusto von Dentz
2005cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev))
2006cba6b758SLuiz Augusto von Dentz /* Remove all existing sets */
2007b338d917SBrian Gix err = hci_clear_adv_sets_sync(hdev, sk);
2008b338d917SBrian Gix if (ext_adv_capable(hdev))
2009b338d917SBrian Gix return err;
2010cba6b758SLuiz Augusto von Dentz
2011cba6b758SLuiz Augusto von Dentz /* This is safe as long as there is no command send while the lock is
2012cba6b758SLuiz Augusto von Dentz * held.
2013cba6b758SLuiz Augusto von Dentz */
2014cba6b758SLuiz Augusto von Dentz hci_dev_lock(hdev);
2015cba6b758SLuiz Augusto von Dentz
2016cba6b758SLuiz Augusto von Dentz /* Cleanup non-ext instances */
2017cba6b758SLuiz Augusto von Dentz list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
2018cba6b758SLuiz Augusto von Dentz u8 instance = adv->instance;
2019cba6b758SLuiz Augusto von Dentz int err;
2020cba6b758SLuiz Augusto von Dentz
2021cba6b758SLuiz Augusto von Dentz if (!(force || adv->timeout))
2022cba6b758SLuiz Augusto von Dentz continue;
2023cba6b758SLuiz Augusto von Dentz
2024cba6b758SLuiz Augusto von Dentz err = hci_remove_adv_instance(hdev, instance);
2025cba6b758SLuiz Augusto von Dentz if (!err)
2026cba6b758SLuiz Augusto von Dentz mgmt_advertising_removed(sk, hdev, instance);
2027cba6b758SLuiz Augusto von Dentz }
2028cba6b758SLuiz Augusto von Dentz
2029cba6b758SLuiz Augusto von Dentz hci_dev_unlock(hdev);
2030cba6b758SLuiz Augusto von Dentz
2031cba6b758SLuiz Augusto von Dentz return 0;
2032cba6b758SLuiz Augusto von Dentz }
hci_remove_adv_sync(struct hci_dev * hdev,u8 instance,struct sock * sk)2033cba6b758SLuiz Augusto von Dentz
2034cba6b758SLuiz Augusto von Dentz static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
2035cba6b758SLuiz Augusto von Dentz struct sock *sk)
2036cba6b758SLuiz Augusto von Dentz {
2037b338d917SBrian Gix int err = 0;
2038cba6b758SLuiz Augusto von Dentz
2039cba6b758SLuiz Augusto von Dentz /* If we use extended advertising, instance has to be removed first. */
2040cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev))
2041b338d917SBrian Gix err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
2042b338d917SBrian Gix if (ext_adv_capable(hdev))
2043b338d917SBrian Gix return err;
2044cba6b758SLuiz Augusto von Dentz
2045cba6b758SLuiz Augusto von Dentz /* This is safe as long as there is no command send while the lock is
2046cba6b758SLuiz Augusto von Dentz * held.
2047cba6b758SLuiz Augusto von Dentz */
2048cba6b758SLuiz Augusto von Dentz hci_dev_lock(hdev);
2049cba6b758SLuiz Augusto von Dentz
2050cba6b758SLuiz Augusto von Dentz err = hci_remove_adv_instance(hdev, instance);
2051cba6b758SLuiz Augusto von Dentz if (!err)
2052cba6b758SLuiz Augusto von Dentz mgmt_advertising_removed(sk, hdev, instance);
2053cba6b758SLuiz Augusto von Dentz
2054cba6b758SLuiz Augusto von Dentz hci_dev_unlock(hdev);
2055cba6b758SLuiz Augusto von Dentz
2056cba6b758SLuiz Augusto von Dentz return err;
2057cba6b758SLuiz Augusto von Dentz }
2058cba6b758SLuiz Augusto von Dentz
2059cba6b758SLuiz Augusto von Dentz /* For a single instance:
2060cba6b758SLuiz Augusto von Dentz * - force == true: The instance will be removed even when its remaining
2061cba6b758SLuiz Augusto von Dentz * lifetime is not zero.
2062cba6b758SLuiz Augusto von Dentz * - force == false: the instance will be deactivated but kept stored unless
2063cba6b758SLuiz Augusto von Dentz * the remaining lifetime is zero.
2064cba6b758SLuiz Augusto von Dentz *
2065cba6b758SLuiz Augusto von Dentz * For instance == 0x00:
2066cba6b758SLuiz Augusto von Dentz * - force == true: All instances will be removed regardless of their timeout
2067cba6b758SLuiz Augusto von Dentz * setting.
2068cba6b758SLuiz Augusto von Dentz * - force == false: Only instances that have a timeout will be removed.
hci_remove_advertising_sync(struct hci_dev * hdev,struct sock * sk,u8 instance,bool force)2069cba6b758SLuiz Augusto von Dentz */
2070cba6b758SLuiz Augusto von Dentz int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
2071cba6b758SLuiz Augusto von Dentz u8 instance, bool force)
2072cba6b758SLuiz Augusto von Dentz {
2073cba6b758SLuiz Augusto von Dentz struct adv_info *next = NULL;
2074cba6b758SLuiz Augusto von Dentz int err;
2075cba6b758SLuiz Augusto von Dentz
2076cba6b758SLuiz Augusto von Dentz /* Cancel any timeout concerning the removed instance(s). */
2077cba6b758SLuiz Augusto von Dentz if (!instance || hdev->cur_adv_instance == instance)
2078cba6b758SLuiz Augusto von Dentz cancel_adv_timeout(hdev);
2079cba6b758SLuiz Augusto von Dentz
2080cba6b758SLuiz Augusto von Dentz /* Get the next instance to advertise BEFORE we remove
2081cba6b758SLuiz Augusto von Dentz * the current one. This can be the same instance again
2082cba6b758SLuiz Augusto von Dentz * if there is only one instance.
2083cba6b758SLuiz Augusto von Dentz */
2084cba6b758SLuiz Augusto von Dentz if (hdev->cur_adv_instance == instance)
2085cba6b758SLuiz Augusto von Dentz next = hci_get_next_instance(hdev, instance);
2086cba6b758SLuiz Augusto von Dentz
2087cba6b758SLuiz Augusto von Dentz if (!instance) {
2088cba6b758SLuiz Augusto von Dentz err = hci_clear_adv_sync(hdev, sk, force);
2089cba6b758SLuiz Augusto von Dentz if (err)
2090cba6b758SLuiz Augusto von Dentz return err;
2091cba6b758SLuiz Augusto von Dentz } else {
2092cba6b758SLuiz Augusto von Dentz struct adv_info *adv = hci_find_adv_instance(hdev, instance);
2093cba6b758SLuiz Augusto von Dentz
2094cba6b758SLuiz Augusto von Dentz if (force || (adv && adv->timeout && !adv->remaining_time)) {
2095cba6b758SLuiz Augusto von Dentz /* Don't advertise a removed instance. */
2096cba6b758SLuiz Augusto von Dentz if (next && next->instance == instance)
2097cba6b758SLuiz Augusto von Dentz next = NULL;
2098cba6b758SLuiz Augusto von Dentz
2099cba6b758SLuiz Augusto von Dentz err = hci_remove_adv_sync(hdev, instance, sk);
2100cba6b758SLuiz Augusto von Dentz if (err)
2101cba6b758SLuiz Augusto von Dentz return err;
2102cba6b758SLuiz Augusto von Dentz }
2103cba6b758SLuiz Augusto von Dentz }
2104cba6b758SLuiz Augusto von Dentz
2105cba6b758SLuiz Augusto von Dentz if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
2106cba6b758SLuiz Augusto von Dentz return 0;
2107cba6b758SLuiz Augusto von Dentz
2108cba6b758SLuiz Augusto von Dentz if (next && !ext_adv_capable(hdev))
2109cba6b758SLuiz Augusto von Dentz hci_schedule_adv_instance_sync(hdev, next->instance, false);
2110cba6b758SLuiz Augusto von Dentz
2111cba6b758SLuiz Augusto von Dentz return 0;
2112cba6b758SLuiz Augusto von Dentz }
hci_read_rssi_sync(struct hci_dev * hdev,__le16 handle)2113cba6b758SLuiz Augusto von Dentz
211447db6b42SBrian Gix int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
211547db6b42SBrian Gix {
211647db6b42SBrian Gix struct hci_cp_read_rssi cp;
211747db6b42SBrian Gix
211847db6b42SBrian Gix cp.handle = handle;
211947db6b42SBrian Gix return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
212047db6b42SBrian Gix sizeof(cp), &cp, HCI_CMD_TIMEOUT);
212147db6b42SBrian Gix }
hci_read_clock_sync(struct hci_dev * hdev,struct hci_cp_read_clock * cp)212247db6b42SBrian Gix
21235a750137SBrian Gix int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
21245a750137SBrian Gix {
21255a750137SBrian Gix return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
21265a750137SBrian Gix sizeof(*cp), cp, HCI_CMD_TIMEOUT);
21275a750137SBrian Gix }
hci_read_tx_power_sync(struct hci_dev * hdev,__le16 handle,u8 type)21285a750137SBrian Gix
212947db6b42SBrian Gix int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
213047db6b42SBrian Gix {
213147db6b42SBrian Gix struct hci_cp_read_tx_power cp;
213247db6b42SBrian Gix
213347db6b42SBrian Gix cp.handle = handle;
213447db6b42SBrian Gix cp.type = type;
213547db6b42SBrian Gix return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
213647db6b42SBrian Gix sizeof(cp), &cp, HCI_CMD_TIMEOUT);
213747db6b42SBrian Gix }
hci_disable_advertising_sync(struct hci_dev * hdev)213847db6b42SBrian Gix
2139cba6b758SLuiz Augusto von Dentz int hci_disable_advertising_sync(struct hci_dev *hdev)
2140cba6b758SLuiz Augusto von Dentz {
2141cba6b758SLuiz Augusto von Dentz u8 enable = 0x00;
2142b338d917SBrian Gix int err = 0;
2143cba6b758SLuiz Augusto von Dentz
2144ad383c2cSLuiz Augusto von Dentz /* If controller is not advertising we are done. */
2145ad383c2cSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
2146ad383c2cSLuiz Augusto von Dentz return 0;
2147ad383c2cSLuiz Augusto von Dentz
2148cba6b758SLuiz Augusto von Dentz if (ext_adv_capable(hdev))
2149b338d917SBrian Gix err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
2150b338d917SBrian Gix if (ext_adv_capable(hdev))
2151b338d917SBrian Gix return err;
2152cba6b758SLuiz Augusto von Dentz
2153cba6b758SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
2154cba6b758SLuiz Augusto von Dentz sizeof(enable), &enable, HCI_CMD_TIMEOUT);
2155cba6b758SLuiz Augusto von Dentz }
hci_le_set_ext_scan_enable_sync(struct hci_dev * hdev,u8 val,u8 filter_dup)2156e8907f76SLuiz Augusto von Dentz
2157e8907f76SLuiz Augusto von Dentz static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
2158e8907f76SLuiz Augusto von Dentz u8 filter_dup)
2159e8907f76SLuiz Augusto von Dentz {
2160e8907f76SLuiz Augusto von Dentz struct hci_cp_le_set_ext_scan_enable cp;
2161e8907f76SLuiz Augusto von Dentz
2162e8907f76SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
2163e8907f76SLuiz Augusto von Dentz cp.enable = val;
2164b338d917SBrian Gix
2165b338d917SBrian Gix if (hci_dev_test_flag(hdev, HCI_MESH))
2166b338d917SBrian Gix cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2167b338d917SBrian Gix else
2168e8907f76SLuiz Augusto von Dentz cp.filter_dup = filter_dup;
2169e8907f76SLuiz Augusto von Dentz
2170e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
2171e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2172e8907f76SLuiz Augusto von Dentz }
hci_le_set_scan_enable_sync(struct hci_dev * hdev,u8 val,u8 filter_dup)2173e8907f76SLuiz Augusto von Dentz
2174e8907f76SLuiz Augusto von Dentz static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2175e8907f76SLuiz Augusto von Dentz u8 filter_dup)
2176e8907f76SLuiz Augusto von Dentz {
2177e8907f76SLuiz Augusto von Dentz struct hci_cp_le_set_scan_enable cp;
2178e8907f76SLuiz Augusto von Dentz
2179e8907f76SLuiz Augusto von Dentz if (use_ext_scan(hdev))
2180e8907f76SLuiz Augusto von Dentz return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2181e8907f76SLuiz Augusto von Dentz
2182e8907f76SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
2183e8907f76SLuiz Augusto von Dentz cp.enable = val;
2184b338d917SBrian Gix
2185b338d917SBrian Gix if (val && hci_dev_test_flag(hdev, HCI_MESH))
2186b338d917SBrian Gix cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2187b338d917SBrian Gix else
2188e8907f76SLuiz Augusto von Dentz cp.filter_dup = filter_dup;
2189e8907f76SLuiz Augusto von Dentz
2190e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2191e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2192e8907f76SLuiz Augusto von Dentz }
hci_le_set_addr_resolution_enable_sync(struct hci_dev * hdev,u8 val)2193e8907f76SLuiz Augusto von Dentz
2194e8907f76SLuiz Augusto von Dentz static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2195e8907f76SLuiz Augusto von Dentz {
2196ad383c2cSLuiz Augusto von Dentz if (!use_ll_privacy(hdev))
2197ad383c2cSLuiz Augusto von Dentz return 0;
2198ad383c2cSLuiz Augusto von Dentz
2199ad383c2cSLuiz Augusto von Dentz /* If controller is not/already resolving we are done. */
2200ad383c2cSLuiz Augusto von Dentz if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2201e8907f76SLuiz Augusto von Dentz return 0;
2202e8907f76SLuiz Augusto von Dentz
2203e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2204e8907f76SLuiz Augusto von Dentz sizeof(val), &val, HCI_CMD_TIMEOUT);
2205e8907f76SLuiz Augusto von Dentz }
hci_scan_disable_sync(struct hci_dev * hdev)2206e8907f76SLuiz Augusto von Dentz
220727592ca1SLuiz Augusto von Dentz static int hci_scan_disable_sync(struct hci_dev *hdev)
2208e8907f76SLuiz Augusto von Dentz {
2209e8907f76SLuiz Augusto von Dentz int err;
2210e8907f76SLuiz Augusto von Dentz
2211e8907f76SLuiz Augusto von Dentz /* If controller is not scanning we are done. */
2212e8907f76SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2213e8907f76SLuiz Augusto von Dentz return 0;
2214e8907f76SLuiz Augusto von Dentz
2215e8907f76SLuiz Augusto von Dentz if (hdev->scanning_paused) {
2216e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Scanning is paused for suspend");
2217e8907f76SLuiz Augusto von Dentz return 0;
2218e8907f76SLuiz Augusto von Dentz }
2219e8907f76SLuiz Augusto von Dentz
2220e8907f76SLuiz Augusto von Dentz err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2221e8907f76SLuiz Augusto von Dentz if (err) {
2222e8907f76SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2223e8907f76SLuiz Augusto von Dentz return err;
2224e8907f76SLuiz Augusto von Dentz }
2225e8907f76SLuiz Augusto von Dentz
2226e8907f76SLuiz Augusto von Dentz return err;
2227e8907f76SLuiz Augusto von Dentz }
scan_use_rpa(struct hci_dev * hdev)2228e8907f76SLuiz Augusto von Dentz
2229e8907f76SLuiz Augusto von Dentz static bool scan_use_rpa(struct hci_dev *hdev)
2230e8907f76SLuiz Augusto von Dentz {
2231e8907f76SLuiz Augusto von Dentz return hci_dev_test_flag(hdev, HCI_PRIVACY);
2232e8907f76SLuiz Augusto von Dentz }
hci_start_interleave_scan(struct hci_dev * hdev)2233e8907f76SLuiz Augusto von Dentz
2234e8907f76SLuiz Augusto von Dentz static void hci_start_interleave_scan(struct hci_dev *hdev)
2235e8907f76SLuiz Augusto von Dentz {
2236e8907f76SLuiz Augusto von Dentz hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2237e8907f76SLuiz Augusto von Dentz queue_delayed_work(hdev->req_workqueue,
2238e8907f76SLuiz Augusto von Dentz &hdev->interleave_scan, 0);
2239e8907f76SLuiz Augusto von Dentz }
is_interleave_scanning(struct hci_dev * hdev)2240e8907f76SLuiz Augusto von Dentz
2241e8907f76SLuiz Augusto von Dentz static bool is_interleave_scanning(struct hci_dev *hdev)
2242e8907f76SLuiz Augusto von Dentz {
2243e8907f76SLuiz Augusto von Dentz return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2244e8907f76SLuiz Augusto von Dentz }
cancel_interleave_scan(struct hci_dev * hdev)2245e8907f76SLuiz Augusto von Dentz
2246e8907f76SLuiz Augusto von Dentz static void cancel_interleave_scan(struct hci_dev *hdev)
2247e8907f76SLuiz Augusto von Dentz {
2248e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "cancelling interleave scan");
2249e8907f76SLuiz Augusto von Dentz
2250e8907f76SLuiz Augusto von Dentz cancel_delayed_work_sync(&hdev->interleave_scan);
2251e8907f76SLuiz Augusto von Dentz
2252e8907f76SLuiz Augusto von Dentz hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2253e8907f76SLuiz Augusto von Dentz }
2254e8907f76SLuiz Augusto von Dentz
2255e8907f76SLuiz Augusto von Dentz /* Return true if interleave_scan wasn't started until exiting this function,
2256e8907f76SLuiz Augusto von Dentz * otherwise, return false
hci_update_interleaved_scan_sync(struct hci_dev * hdev)2257e8907f76SLuiz Augusto von Dentz */
2258e8907f76SLuiz Augusto von Dentz static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2259e8907f76SLuiz Augusto von Dentz {
2260e8907f76SLuiz Augusto von Dentz /* Do interleaved scan only if all of the following are true:
2261e8907f76SLuiz Augusto von Dentz * - There is at least one ADV monitor
2262e8907f76SLuiz Augusto von Dentz * - At least one pending LE connection or one device to be scanned for
2263e8907f76SLuiz Augusto von Dentz * - Monitor offloading is not supported
2264e8907f76SLuiz Augusto von Dentz * If so, we should alternate between allowlist scan and one without
2265e8907f76SLuiz Augusto von Dentz * any filters to save power.
2266e8907f76SLuiz Augusto von Dentz */
2267e8907f76SLuiz Augusto von Dentz bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2268e8907f76SLuiz Augusto von Dentz !(list_empty(&hdev->pend_le_conns) &&
2269e8907f76SLuiz Augusto von Dentz list_empty(&hdev->pend_le_reports)) &&
2270e8907f76SLuiz Augusto von Dentz hci_get_adv_monitor_offload_ext(hdev) ==
2271e8907f76SLuiz Augusto von Dentz HCI_ADV_MONITOR_EXT_NONE;
2272e8907f76SLuiz Augusto von Dentz bool is_interleaving = is_interleave_scanning(hdev);
2273e8907f76SLuiz Augusto von Dentz
2274e8907f76SLuiz Augusto von Dentz if (use_interleaving && !is_interleaving) {
2275e8907f76SLuiz Augusto von Dentz hci_start_interleave_scan(hdev);
2276e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "starting interleave scan");
2277e8907f76SLuiz Augusto von Dentz return true;
2278e8907f76SLuiz Augusto von Dentz }
2279e8907f76SLuiz Augusto von Dentz
2280e8907f76SLuiz Augusto von Dentz if (!use_interleaving && is_interleaving)
2281e8907f76SLuiz Augusto von Dentz cancel_interleave_scan(hdev);
2282e8907f76SLuiz Augusto von Dentz
2283e8907f76SLuiz Augusto von Dentz return false;
2284e8907f76SLuiz Augusto von Dentz }
2285e8907f76SLuiz Augusto von Dentz
hci_le_del_resolve_list_sync(struct hci_dev * hdev,bdaddr_t * bdaddr,u8 bdaddr_type)2286e8907f76SLuiz Augusto von Dentz /* Removes connection to resolve list if needed.*/
2287e8907f76SLuiz Augusto von Dentz static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2288e8907f76SLuiz Augusto von Dentz bdaddr_t *bdaddr, u8 bdaddr_type)
2289e8907f76SLuiz Augusto von Dentz {
2290e8907f76SLuiz Augusto von Dentz struct hci_cp_le_del_from_resolv_list cp;
2291e8907f76SLuiz Augusto von Dentz struct bdaddr_list_with_irk *entry;
2292e8907f76SLuiz Augusto von Dentz
2293ad383c2cSLuiz Augusto von Dentz if (!use_ll_privacy(hdev))
2294e8907f76SLuiz Augusto von Dentz return 0;
2295e8907f76SLuiz Augusto von Dentz
2296e8907f76SLuiz Augusto von Dentz /* Check if the IRK has been programmed */
2297e8907f76SLuiz Augusto von Dentz entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2298e8907f76SLuiz Augusto von Dentz bdaddr_type);
2299e8907f76SLuiz Augusto von Dentz if (!entry)
2300e8907f76SLuiz Augusto von Dentz return 0;
2301e8907f76SLuiz Augusto von Dentz
2302e8907f76SLuiz Augusto von Dentz cp.bdaddr_type = bdaddr_type;
2303e8907f76SLuiz Augusto von Dentz bacpy(&cp.bdaddr, bdaddr);
2304e8907f76SLuiz Augusto von Dentz
2305e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2306e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2307e8907f76SLuiz Augusto von Dentz }
hci_le_del_accept_list_sync(struct hci_dev * hdev,bdaddr_t * bdaddr,u8 bdaddr_type)2308e8907f76SLuiz Augusto von Dentz
2309e8907f76SLuiz Augusto von Dentz static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2310e8907f76SLuiz Augusto von Dentz bdaddr_t *bdaddr, u8 bdaddr_type)
2311e8907f76SLuiz Augusto von Dentz {
2312e8907f76SLuiz Augusto von Dentz struct hci_cp_le_del_from_accept_list cp;
2313e8907f76SLuiz Augusto von Dentz int err;
2314e8907f76SLuiz Augusto von Dentz
2315e8907f76SLuiz Augusto von Dentz /* Check if device is on accept list before removing it */
2316e8907f76SLuiz Augusto von Dentz if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2317e8907f76SLuiz Augusto von Dentz return 0;
2318e8907f76SLuiz Augusto von Dentz
2319e8907f76SLuiz Augusto von Dentz cp.bdaddr_type = bdaddr_type;
2320e8907f76SLuiz Augusto von Dentz bacpy(&cp.bdaddr, bdaddr);
2321e8907f76SLuiz Augusto von Dentz
2322ad383c2cSLuiz Augusto von Dentz /* Ignore errors when removing from resolving list as that is likely
2323ad383c2cSLuiz Augusto von Dentz * that the device was never added.
2324ad383c2cSLuiz Augusto von Dentz */
2325ad383c2cSLuiz Augusto von Dentz hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2326ad383c2cSLuiz Augusto von Dentz
2327e8907f76SLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2328e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2329e8907f76SLuiz Augusto von Dentz if (err) {
2330e8907f76SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2331e8907f76SLuiz Augusto von Dentz return err;
2332e8907f76SLuiz Augusto von Dentz }
2333e8907f76SLuiz Augusto von Dentz
2334e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2335e8907f76SLuiz Augusto von Dentz cp.bdaddr_type);
2336e8907f76SLuiz Augusto von Dentz
2337ad383c2cSLuiz Augusto von Dentz return 0;
2338e8907f76SLuiz Augusto von Dentz }
2339e8907f76SLuiz Augusto von Dentz
2340195ef75eSPauli Virtanen struct conn_params {
2341195ef75eSPauli Virtanen bdaddr_t addr;
2342195ef75eSPauli Virtanen u8 addr_type;
2343195ef75eSPauli Virtanen hci_conn_flags_t flags;
2344195ef75eSPauli Virtanen u8 privacy_mode;
2345195ef75eSPauli Virtanen };
2346195ef75eSPauli Virtanen
2347cf75ad8bSLuiz Augusto von Dentz /* Adds connection to resolve list if needed.
2348cf75ad8bSLuiz Augusto von Dentz * Setting params to NULL programs local hdev->irk
hci_le_add_resolve_list_sync(struct hci_dev * hdev,struct conn_params * params)2349cf75ad8bSLuiz Augusto von Dentz */
2350e8907f76SLuiz Augusto von Dentz static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2351195ef75eSPauli Virtanen struct conn_params *params)
2352e8907f76SLuiz Augusto von Dentz {
2353e8907f76SLuiz Augusto von Dentz struct hci_cp_le_add_to_resolv_list cp;
2354e8907f76SLuiz Augusto von Dentz struct smp_irk *irk;
2355e8907f76SLuiz Augusto von Dentz struct bdaddr_list_with_irk *entry;
2356195ef75eSPauli Virtanen struct hci_conn_params *p;
2357e8907f76SLuiz Augusto von Dentz
2358ad383c2cSLuiz Augusto von Dentz if (!use_ll_privacy(hdev))
2359e8907f76SLuiz Augusto von Dentz return 0;
2360e8907f76SLuiz Augusto von Dentz
2361cf75ad8bSLuiz Augusto von Dentz /* Attempt to program local identity address, type and irk if params is
2362cf75ad8bSLuiz Augusto von Dentz * NULL.
2363cf75ad8bSLuiz Augusto von Dentz */
2364cf75ad8bSLuiz Augusto von Dentz if (!params) {
2365cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2366cf75ad8bSLuiz Augusto von Dentz return 0;
2367cf75ad8bSLuiz Augusto von Dentz
2368cf75ad8bSLuiz Augusto von Dentz hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2369cf75ad8bSLuiz Augusto von Dentz memcpy(cp.peer_irk, hdev->irk, 16);
2370cf75ad8bSLuiz Augusto von Dentz goto done;
2371cf75ad8bSLuiz Augusto von Dentz }
2372cf75ad8bSLuiz Augusto von Dentz
2373e8907f76SLuiz Augusto von Dentz irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2374e8907f76SLuiz Augusto von Dentz if (!irk)
2375e8907f76SLuiz Augusto von Dentz return 0;
2376e8907f76SLuiz Augusto von Dentz
2377e8907f76SLuiz Augusto von Dentz /* Check if the IK has _not_ been programmed yet. */
2378e8907f76SLuiz Augusto von Dentz entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2379e8907f76SLuiz Augusto von Dentz ¶ms->addr,
2380e8907f76SLuiz Augusto von Dentz params->addr_type);
2381e8907f76SLuiz Augusto von Dentz if (entry)
2382e8907f76SLuiz Augusto von Dentz return 0;
2383e8907f76SLuiz Augusto von Dentz
2384e8907f76SLuiz Augusto von Dentz cp.bdaddr_type = params->addr_type;
2385e8907f76SLuiz Augusto von Dentz bacpy(&cp.bdaddr, ¶ms->addr);
2386e8907f76SLuiz Augusto von Dentz memcpy(cp.peer_irk, irk->val, 16);
2387e8907f76SLuiz Augusto von Dentz
23880900b1c6SLuiz Augusto von Dentz /* Default privacy mode is always Network */
23890900b1c6SLuiz Augusto von Dentz params->privacy_mode = HCI_NETWORK_PRIVACY;
23900900b1c6SLuiz Augusto von Dentz
2391195ef75eSPauli Virtanen rcu_read_lock();
2392195ef75eSPauli Virtanen p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2393195ef75eSPauli Virtanen ¶ms->addr, params->addr_type);
2394195ef75eSPauli Virtanen if (!p)
2395195ef75eSPauli Virtanen p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2396195ef75eSPauli Virtanen ¶ms->addr, params->addr_type);
2397195ef75eSPauli Virtanen if (p)
2398195ef75eSPauli Virtanen WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY);
2399195ef75eSPauli Virtanen rcu_read_unlock();
2400195ef75eSPauli Virtanen
2401cf75ad8bSLuiz Augusto von Dentz done:
2402e8907f76SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2403e8907f76SLuiz Augusto von Dentz memcpy(cp.local_irk, hdev->irk, 16);
2404e8907f76SLuiz Augusto von Dentz else
2405e8907f76SLuiz Augusto von Dentz memset(cp.local_irk, 0, 16);
2406e8907f76SLuiz Augusto von Dentz
2407e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2408e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2409e8907f76SLuiz Augusto von Dentz }
2410e8907f76SLuiz Augusto von Dentz
hci_le_set_privacy_mode_sync(struct hci_dev * hdev,struct conn_params * params)2411853b70b5SLuiz Augusto von Dentz /* Set Device Privacy Mode. */
2412853b70b5SLuiz Augusto von Dentz static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2413195ef75eSPauli Virtanen struct conn_params *params)
2414853b70b5SLuiz Augusto von Dentz {
2415853b70b5SLuiz Augusto von Dentz struct hci_cp_le_set_privacy_mode cp;
2416853b70b5SLuiz Augusto von Dentz struct smp_irk *irk;
2417853b70b5SLuiz Augusto von Dentz
2418853b70b5SLuiz Augusto von Dentz /* If device privacy mode has already been set there is nothing to do */
2419853b70b5SLuiz Augusto von Dentz if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2420853b70b5SLuiz Augusto von Dentz return 0;
2421853b70b5SLuiz Augusto von Dentz
2422853b70b5SLuiz Augusto von Dentz /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2423853b70b5SLuiz Augusto von Dentz * indicates that LL Privacy has been enabled and
2424853b70b5SLuiz Augusto von Dentz * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2425853b70b5SLuiz Augusto von Dentz */
2426e1cff700SLinus Torvalds if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2427853b70b5SLuiz Augusto von Dentz return 0;
2428853b70b5SLuiz Augusto von Dentz
2429853b70b5SLuiz Augusto von Dentz irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2430853b70b5SLuiz Augusto von Dentz if (!irk)
2431853b70b5SLuiz Augusto von Dentz return 0;
2432853b70b5SLuiz Augusto von Dentz
2433853b70b5SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
2434853b70b5SLuiz Augusto von Dentz cp.bdaddr_type = irk->addr_type;
2435853b70b5SLuiz Augusto von Dentz bacpy(&cp.bdaddr, &irk->bdaddr);
2436853b70b5SLuiz Augusto von Dentz cp.mode = HCI_DEVICE_PRIVACY;
2437853b70b5SLuiz Augusto von Dentz
2438195ef75eSPauli Virtanen /* Note: params->privacy_mode is not updated since it is a copy */
2439195ef75eSPauli Virtanen
2440853b70b5SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2441853b70b5SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2442853b70b5SLuiz Augusto von Dentz }
2443853b70b5SLuiz Augusto von Dentz
2444e8907f76SLuiz Augusto von Dentz /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2445853b70b5SLuiz Augusto von Dentz * this attempts to program the device in the resolving list as well and
2446853b70b5SLuiz Augusto von Dentz * properly set the privacy mode.
hci_le_add_accept_list_sync(struct hci_dev * hdev,struct conn_params * params,u8 * num_entries)2447e8907f76SLuiz Augusto von Dentz */
2448e8907f76SLuiz Augusto von Dentz static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2449195ef75eSPauli Virtanen struct conn_params *params,
2450ad383c2cSLuiz Augusto von Dentz u8 *num_entries)
2451e8907f76SLuiz Augusto von Dentz {
2452e8907f76SLuiz Augusto von Dentz struct hci_cp_le_add_to_accept_list cp;
2453e8907f76SLuiz Augusto von Dentz int err;
2454e8907f76SLuiz Augusto von Dentz
24553b420553SLuiz Augusto von Dentz /* During suspend, only wakeable devices can be in acceptlist */
24563b420553SLuiz Augusto von Dentz if (hdev->suspended &&
24576699d7ceSLuiz Augusto von Dentz !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) {
24586699d7ceSLuiz Augusto von Dentz hci_le_del_accept_list_sync(hdev, ¶ms->addr,
24596699d7ceSLuiz Augusto von Dentz params->addr_type);
24603b420553SLuiz Augusto von Dentz return 0;
24616699d7ceSLuiz Augusto von Dentz }
24623b420553SLuiz Augusto von Dentz
2463e8907f76SLuiz Augusto von Dentz /* Select filter policy to accept all advertising */
2464e8907f76SLuiz Augusto von Dentz if (*num_entries >= hdev->le_accept_list_size)
2465e8907f76SLuiz Augusto von Dentz return -ENOSPC;
2466e8907f76SLuiz Augusto von Dentz
2467e8907f76SLuiz Augusto von Dentz /* Accept list can not be used with RPAs */
2468ad383c2cSLuiz Augusto von Dentz if (!use_ll_privacy(hdev) &&
24693b420553SLuiz Augusto von Dentz hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type))
2470e8907f76SLuiz Augusto von Dentz return -EINVAL;
2471e8907f76SLuiz Augusto von Dentz
2472ad383c2cSLuiz Augusto von Dentz /* Attempt to program the device in the resolving list first to avoid
2473ad383c2cSLuiz Augusto von Dentz * having to rollback in case it fails since the resolving list is
2474ad383c2cSLuiz Augusto von Dentz * dynamic it can probably be smaller than the accept list.
2475ad383c2cSLuiz Augusto von Dentz */
2476ad383c2cSLuiz Augusto von Dentz err = hci_le_add_resolve_list_sync(hdev, params);
2477ad383c2cSLuiz Augusto von Dentz if (err) {
2478ad383c2cSLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2479ad383c2cSLuiz Augusto von Dentz return err;
2480ad383c2cSLuiz Augusto von Dentz }
2481ad383c2cSLuiz Augusto von Dentz
2482853b70b5SLuiz Augusto von Dentz /* Set Privacy Mode */
2483853b70b5SLuiz Augusto von Dentz err = hci_le_set_privacy_mode_sync(hdev, params);
2484853b70b5SLuiz Augusto von Dentz if (err) {
2485853b70b5SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2486853b70b5SLuiz Augusto von Dentz return err;
2487853b70b5SLuiz Augusto von Dentz }
2488853b70b5SLuiz Augusto von Dentz
2489853b70b5SLuiz Augusto von Dentz /* Check if already in accept list */
2490853b70b5SLuiz Augusto von Dentz if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
2491853b70b5SLuiz Augusto von Dentz params->addr_type))
2492853b70b5SLuiz Augusto von Dentz return 0;
2493853b70b5SLuiz Augusto von Dentz
2494e8907f76SLuiz Augusto von Dentz *num_entries += 1;
2495e8907f76SLuiz Augusto von Dentz cp.bdaddr_type = params->addr_type;
2496e8907f76SLuiz Augusto von Dentz bacpy(&cp.bdaddr, ¶ms->addr);
2497e8907f76SLuiz Augusto von Dentz
2498e8907f76SLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2499e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2500e8907f76SLuiz Augusto von Dentz if (err) {
2501e8907f76SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2502ad383c2cSLuiz Augusto von Dentz /* Rollback the device from the resolving list */
2503ad383c2cSLuiz Augusto von Dentz hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2504e8907f76SLuiz Augusto von Dentz return err;
2505e8907f76SLuiz Augusto von Dentz }
2506e8907f76SLuiz Augusto von Dentz
2507e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2508e8907f76SLuiz Augusto von Dentz cp.bdaddr_type);
2509e8907f76SLuiz Augusto von Dentz
2510ad383c2cSLuiz Augusto von Dentz return 0;
2511e8907f76SLuiz Augusto von Dentz }
2512e8907f76SLuiz Augusto von Dentz
hci_pause_advertising_sync(struct hci_dev * hdev)2513182ee45dSLuiz Augusto von Dentz /* This function disables/pause all advertising instances */
2514ad383c2cSLuiz Augusto von Dentz static int hci_pause_advertising_sync(struct hci_dev *hdev)
2515ad383c2cSLuiz Augusto von Dentz {
2516ad383c2cSLuiz Augusto von Dentz int err;
2517182ee45dSLuiz Augusto von Dentz int old_state;
2518ad383c2cSLuiz Augusto von Dentz
25199446bddeSLuiz Augusto von Dentz /* If already been paused there is nothing to do. */
25209446bddeSLuiz Augusto von Dentz if (hdev->advertising_paused)
2521ad383c2cSLuiz Augusto von Dentz return 0;
2522ad383c2cSLuiz Augusto von Dentz
2523182ee45dSLuiz Augusto von Dentz bt_dev_dbg(hdev, "Pausing directed advertising");
2524182ee45dSLuiz Augusto von Dentz
2525182ee45dSLuiz Augusto von Dentz /* Stop directed advertising */
2526182ee45dSLuiz Augusto von Dentz old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2527182ee45dSLuiz Augusto von Dentz if (old_state) {
2528182ee45dSLuiz Augusto von Dentz /* When discoverable timeout triggers, then just make sure
2529182ee45dSLuiz Augusto von Dentz * the limited discoverable flag is cleared. Even in the case
2530182ee45dSLuiz Augusto von Dentz * of a timeout triggered from general discoverable, it is
2531182ee45dSLuiz Augusto von Dentz * safe to unconditionally clear the flag.
2532182ee45dSLuiz Augusto von Dentz */
2533182ee45dSLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2534182ee45dSLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2535182ee45dSLuiz Augusto von Dentz hdev->discov_timeout = 0;
2536182ee45dSLuiz Augusto von Dentz }
2537182ee45dSLuiz Augusto von Dentz
2538ad383c2cSLuiz Augusto von Dentz bt_dev_dbg(hdev, "Pausing advertising instances");
2539ad383c2cSLuiz Augusto von Dentz
2540ad383c2cSLuiz Augusto von Dentz /* Call to disable any advertisements active on the controller.
2541ad383c2cSLuiz Augusto von Dentz * This will succeed even if no advertisements are configured.
2542ad383c2cSLuiz Augusto von Dentz */
2543ad383c2cSLuiz Augusto von Dentz err = hci_disable_advertising_sync(hdev);
2544ad383c2cSLuiz Augusto von Dentz if (err)
2545ad383c2cSLuiz Augusto von Dentz return err;
2546ad383c2cSLuiz Augusto von Dentz
2547ad383c2cSLuiz Augusto von Dentz /* If we are using software rotation, pause the loop */
2548ad383c2cSLuiz Augusto von Dentz if (!ext_adv_capable(hdev))
2549ad383c2cSLuiz Augusto von Dentz cancel_adv_timeout(hdev);
2550ad383c2cSLuiz Augusto von Dentz
2551ad383c2cSLuiz Augusto von Dentz hdev->advertising_paused = true;
2552182ee45dSLuiz Augusto von Dentz hdev->advertising_old_state = old_state;
2553ad383c2cSLuiz Augusto von Dentz
2554ad383c2cSLuiz Augusto von Dentz return 0;
2555ad383c2cSLuiz Augusto von Dentz }
2556ad383c2cSLuiz Augusto von Dentz
hci_resume_advertising_sync(struct hci_dev * hdev)2557182ee45dSLuiz Augusto von Dentz /* This function enables all user advertising instances */
2558ad383c2cSLuiz Augusto von Dentz static int hci_resume_advertising_sync(struct hci_dev *hdev)
2559ad383c2cSLuiz Augusto von Dentz {
2560ad383c2cSLuiz Augusto von Dentz struct adv_info *adv, *tmp;
2561ad383c2cSLuiz Augusto von Dentz int err;
2562ad383c2cSLuiz Augusto von Dentz
2563ad383c2cSLuiz Augusto von Dentz /* If advertising has not been paused there is nothing to do. */
2564ad383c2cSLuiz Augusto von Dentz if (!hdev->advertising_paused)
2565ad383c2cSLuiz Augusto von Dentz return 0;
2566ad383c2cSLuiz Augusto von Dentz
2567182ee45dSLuiz Augusto von Dentz /* Resume directed advertising */
2568182ee45dSLuiz Augusto von Dentz hdev->advertising_paused = false;
2569182ee45dSLuiz Augusto von Dentz if (hdev->advertising_old_state) {
2570182ee45dSLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_ADVERTISING);
2571182ee45dSLuiz Augusto von Dentz hdev->advertising_old_state = 0;
2572182ee45dSLuiz Augusto von Dentz }
2573182ee45dSLuiz Augusto von Dentz
2574ad383c2cSLuiz Augusto von Dentz bt_dev_dbg(hdev, "Resuming advertising instances");
2575ad383c2cSLuiz Augusto von Dentz
2576ad383c2cSLuiz Augusto von Dentz if (ext_adv_capable(hdev)) {
2577ad383c2cSLuiz Augusto von Dentz /* Call for each tracked instance to be re-enabled */
2578ad383c2cSLuiz Augusto von Dentz list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2579ad383c2cSLuiz Augusto von Dentz err = hci_enable_ext_advertising_sync(hdev,
2580ad383c2cSLuiz Augusto von Dentz adv->instance);
2581ad383c2cSLuiz Augusto von Dentz if (!err)
2582ad383c2cSLuiz Augusto von Dentz continue;
2583ad383c2cSLuiz Augusto von Dentz
2584ad383c2cSLuiz Augusto von Dentz /* If the instance cannot be resumed remove it */
2585ad383c2cSLuiz Augusto von Dentz hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2586ad383c2cSLuiz Augusto von Dentz NULL);
2587ad383c2cSLuiz Augusto von Dentz }
2588ad383c2cSLuiz Augusto von Dentz } else {
2589ad383c2cSLuiz Augusto von Dentz /* Schedule for most recent instance to be restarted and begin
2590ad383c2cSLuiz Augusto von Dentz * the software rotation loop
2591ad383c2cSLuiz Augusto von Dentz */
2592ad383c2cSLuiz Augusto von Dentz err = hci_schedule_adv_instance_sync(hdev,
2593ad383c2cSLuiz Augusto von Dentz hdev->cur_adv_instance,
2594ad383c2cSLuiz Augusto von Dentz true);
2595ad383c2cSLuiz Augusto von Dentz }
2596ad383c2cSLuiz Augusto von Dentz
2597ad383c2cSLuiz Augusto von Dentz hdev->advertising_paused = false;
2598ad383c2cSLuiz Augusto von Dentz
2599ad383c2cSLuiz Augusto von Dentz return err;
2600ad383c2cSLuiz Augusto von Dentz }
hci_pause_addr_resolution(struct hci_dev * hdev)2601ad383c2cSLuiz Augusto von Dentz
26023c44a431SZhengping Jiang static int hci_pause_addr_resolution(struct hci_dev *hdev)
26033c44a431SZhengping Jiang {
26043c44a431SZhengping Jiang int err;
26053c44a431SZhengping Jiang
26063c44a431SZhengping Jiang if (!use_ll_privacy(hdev))
26073c44a431SZhengping Jiang return 0;
26083c44a431SZhengping Jiang
26093c44a431SZhengping Jiang if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
26103c44a431SZhengping Jiang return 0;
26113c44a431SZhengping Jiang
26123c44a431SZhengping Jiang /* Cannot disable addr resolution if scanning is enabled or
26133c44a431SZhengping Jiang * when initiating an LE connection.
26143c44a431SZhengping Jiang */
26153c44a431SZhengping Jiang if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
26163c44a431SZhengping Jiang hci_lookup_le_connect(hdev)) {
26173c44a431SZhengping Jiang bt_dev_err(hdev, "Command not allowed when scan/LE connect");
26183c44a431SZhengping Jiang return -EPERM;
26193c44a431SZhengping Jiang }
26203c44a431SZhengping Jiang
26213c44a431SZhengping Jiang /* Cannot disable addr resolution if advertising is enabled. */
26223c44a431SZhengping Jiang err = hci_pause_advertising_sync(hdev);
26233c44a431SZhengping Jiang if (err) {
26243c44a431SZhengping Jiang bt_dev_err(hdev, "Pause advertising failed: %d", err);
26253c44a431SZhengping Jiang return err;
26263c44a431SZhengping Jiang }
26273c44a431SZhengping Jiang
26283c44a431SZhengping Jiang err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
26293c44a431SZhengping Jiang if (err)
26303c44a431SZhengping Jiang bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
26313c44a431SZhengping Jiang err);
26323c44a431SZhengping Jiang
26333c44a431SZhengping Jiang /* Return if address resolution is disabled and RPA is not used. */
26343c44a431SZhengping Jiang if (!err && scan_use_rpa(hdev))
2635ae051b04SLuiz Augusto von Dentz return 0;
26363c44a431SZhengping Jiang
26373c44a431SZhengping Jiang hci_resume_advertising_sync(hdev);
26383c44a431SZhengping Jiang return err;
26393c44a431SZhengping Jiang }
hci_read_local_oob_data_sync(struct hci_dev * hdev,bool extended,struct sock * sk)26403c44a431SZhengping Jiang
2641f892244bSBrian Gix struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2642f892244bSBrian Gix bool extended, struct sock *sk)
2643f892244bSBrian Gix {
2644f892244bSBrian Gix u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2645f892244bSBrian Gix HCI_OP_READ_LOCAL_OOB_DATA;
2646f892244bSBrian Gix
2647f892244bSBrian Gix return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2648f892244bSBrian Gix }
conn_params_copy(struct list_head * list,size_t * n)2649f892244bSBrian Gix
2650195ef75eSPauli Virtanen static struct conn_params *conn_params_copy(struct list_head *list, size_t *n)
2651195ef75eSPauli Virtanen {
2652195ef75eSPauli Virtanen struct hci_conn_params *params;
2653195ef75eSPauli Virtanen struct conn_params *p;
2654195ef75eSPauli Virtanen size_t i;
2655195ef75eSPauli Virtanen
2656195ef75eSPauli Virtanen rcu_read_lock();
2657195ef75eSPauli Virtanen
2658195ef75eSPauli Virtanen i = 0;
2659195ef75eSPauli Virtanen list_for_each_entry_rcu(params, list, action)
2660195ef75eSPauli Virtanen ++i;
2661195ef75eSPauli Virtanen *n = i;
2662195ef75eSPauli Virtanen
2663195ef75eSPauli Virtanen rcu_read_unlock();
2664195ef75eSPauli Virtanen
2665195ef75eSPauli Virtanen p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL);
2666195ef75eSPauli Virtanen if (!p)
2667195ef75eSPauli Virtanen return NULL;
2668195ef75eSPauli Virtanen
2669195ef75eSPauli Virtanen rcu_read_lock();
2670195ef75eSPauli Virtanen
2671195ef75eSPauli Virtanen i = 0;
2672195ef75eSPauli Virtanen list_for_each_entry_rcu(params, list, action) {
2673195ef75eSPauli Virtanen /* Racing adds are handled in next scan update */
2674195ef75eSPauli Virtanen if (i >= *n)
2675195ef75eSPauli Virtanen break;
2676195ef75eSPauli Virtanen
2677195ef75eSPauli Virtanen /* No hdev->lock, but: addr, addr_type are immutable.
2678195ef75eSPauli Virtanen * privacy_mode is only written by us or in
2679195ef75eSPauli Virtanen * hci_cc_le_set_privacy_mode that we wait for.
2680195ef75eSPauli Virtanen * We should be idempotent so MGMT updating flags
2681195ef75eSPauli Virtanen * while we are processing is OK.
2682195ef75eSPauli Virtanen */
2683195ef75eSPauli Virtanen bacpy(&p[i].addr, ¶ms->addr);
2684195ef75eSPauli Virtanen p[i].addr_type = params->addr_type;
2685195ef75eSPauli Virtanen p[i].flags = READ_ONCE(params->flags);
2686195ef75eSPauli Virtanen p[i].privacy_mode = READ_ONCE(params->privacy_mode);
2687195ef75eSPauli Virtanen ++i;
2688195ef75eSPauli Virtanen }
2689195ef75eSPauli Virtanen
2690195ef75eSPauli Virtanen rcu_read_unlock();
2691195ef75eSPauli Virtanen
2692195ef75eSPauli Virtanen *n = i;
2693195ef75eSPauli Virtanen return p;
2694195ef75eSPauli Virtanen }
2695195ef75eSPauli Virtanen
2696ad383c2cSLuiz Augusto von Dentz /* Device must not be scanning when updating the accept list.
2697ad383c2cSLuiz Augusto von Dentz *
2698ad383c2cSLuiz Augusto von Dentz * Update is done using the following sequence:
2699ad383c2cSLuiz Augusto von Dentz *
2700ad383c2cSLuiz Augusto von Dentz * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
2701ad383c2cSLuiz Augusto von Dentz * Remove Devices From Accept List ->
2702ad383c2cSLuiz Augusto von Dentz * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
2703ad383c2cSLuiz Augusto von Dentz * Add Devices to Accept List ->
2704ad383c2cSLuiz Augusto von Dentz * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
2705ad383c2cSLuiz Augusto von Dentz * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
2706ad383c2cSLuiz Augusto von Dentz * Enable Scanning
2707ad383c2cSLuiz Augusto von Dentz *
2708ad383c2cSLuiz Augusto von Dentz * In case of failure advertising shall be restored to its original state and
2709ad383c2cSLuiz Augusto von Dentz * return would disable accept list since either accept or resolving list could
2710ad383c2cSLuiz Augusto von Dentz * not be programmed.
2711ad383c2cSLuiz Augusto von Dentz *
hci_update_accept_list_sync(struct hci_dev * hdev)2712ad383c2cSLuiz Augusto von Dentz */
2713e8907f76SLuiz Augusto von Dentz static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2714e8907f76SLuiz Augusto von Dentz {
2715195ef75eSPauli Virtanen struct conn_params *params;
2716e8907f76SLuiz Augusto von Dentz struct bdaddr_list *b, *t;
2717e8907f76SLuiz Augusto von Dentz u8 num_entries = 0;
2718e8907f76SLuiz Augusto von Dentz bool pend_conn, pend_report;
271980740ebbSLuiz Augusto von Dentz u8 filter_policy;
2720195ef75eSPauli Virtanen size_t i, n;
2721ad383c2cSLuiz Augusto von Dentz int err;
2722e8907f76SLuiz Augusto von Dentz
27233c44a431SZhengping Jiang /* Pause advertising if resolving list can be used as controllers
2724ad383c2cSLuiz Augusto von Dentz * cannot accept resolving list modifications while advertising.
2725ad383c2cSLuiz Augusto von Dentz */
2726ad383c2cSLuiz Augusto von Dentz if (use_ll_privacy(hdev)) {
2727ad383c2cSLuiz Augusto von Dentz err = hci_pause_advertising_sync(hdev);
2728ad383c2cSLuiz Augusto von Dentz if (err) {
2729ad383c2cSLuiz Augusto von Dentz bt_dev_err(hdev, "pause advertising failed: %d", err);
2730ad383c2cSLuiz Augusto von Dentz return 0x00;
2731ad383c2cSLuiz Augusto von Dentz }
2732ad383c2cSLuiz Augusto von Dentz }
2733ad383c2cSLuiz Augusto von Dentz
2734ad383c2cSLuiz Augusto von Dentz /* Disable address resolution while reprogramming accept list since
2735ad383c2cSLuiz Augusto von Dentz * devices that do have an IRK will be programmed in the resolving list
2736ad383c2cSLuiz Augusto von Dentz * when LL Privacy is enabled.
2737ad383c2cSLuiz Augusto von Dentz */
2738ad383c2cSLuiz Augusto von Dentz err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2739ad383c2cSLuiz Augusto von Dentz if (err) {
2740ad383c2cSLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2741ad383c2cSLuiz Augusto von Dentz goto done;
2742ad383c2cSLuiz Augusto von Dentz }
2743e8907f76SLuiz Augusto von Dentz
2744e8907f76SLuiz Augusto von Dentz /* Go through the current accept list programmed into the
27456828b583SLuiz Augusto von Dentz * controller one by one and check if that address is connected or is
27466828b583SLuiz Augusto von Dentz * still in the list of pending connections or list of devices to
2747e8907f76SLuiz Augusto von Dentz * report. If not present in either list, then remove it from
2748e8907f76SLuiz Augusto von Dentz * the controller.
2749e8907f76SLuiz Augusto von Dentz */
2750e8907f76SLuiz Augusto von Dentz list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
27516828b583SLuiz Augusto von Dentz if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
27526828b583SLuiz Augusto von Dentz continue;
27536828b583SLuiz Augusto von Dentz
2754195ef75eSPauli Virtanen /* Pointers not dereferenced, no locks needed */
2755e8907f76SLuiz Augusto von Dentz pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2756e8907f76SLuiz Augusto von Dentz &b->bdaddr,
2757e8907f76SLuiz Augusto von Dentz b->bdaddr_type);
2758e8907f76SLuiz Augusto von Dentz pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2759e8907f76SLuiz Augusto von Dentz &b->bdaddr,
2760e8907f76SLuiz Augusto von Dentz b->bdaddr_type);
2761e8907f76SLuiz Augusto von Dentz
2762e8907f76SLuiz Augusto von Dentz /* If the device is not likely to connect or report,
2763e8907f76SLuiz Augusto von Dentz * remove it from the acceptlist.
2764e8907f76SLuiz Augusto von Dentz */
2765e8907f76SLuiz Augusto von Dentz if (!pend_conn && !pend_report) {
2766e8907f76SLuiz Augusto von Dentz hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2767e8907f76SLuiz Augusto von Dentz b->bdaddr_type);
2768e8907f76SLuiz Augusto von Dentz continue;
2769e8907f76SLuiz Augusto von Dentz }
2770e8907f76SLuiz Augusto von Dentz
2771e8907f76SLuiz Augusto von Dentz num_entries++;
2772e8907f76SLuiz Augusto von Dentz }
2773e8907f76SLuiz Augusto von Dentz
2774e8907f76SLuiz Augusto von Dentz /* Since all no longer valid accept list entries have been
2775e8907f76SLuiz Augusto von Dentz * removed, walk through the list of pending connections
2776e8907f76SLuiz Augusto von Dentz * and ensure that any new device gets programmed into
2777e8907f76SLuiz Augusto von Dentz * the controller.
2778e8907f76SLuiz Augusto von Dentz *
2779e8907f76SLuiz Augusto von Dentz * If the list of the devices is larger than the list of
2780e8907f76SLuiz Augusto von Dentz * available accept list entries in the controller, then
2781e8907f76SLuiz Augusto von Dentz * just abort and return filer policy value to not use the
2782e8907f76SLuiz Augusto von Dentz * accept list.
2783195ef75eSPauli Virtanen *
2784195ef75eSPauli Virtanen * The list and params may be mutated while we wait for events,
2785195ef75eSPauli Virtanen * so make a copy and iterate it.
2786e8907f76SLuiz Augusto von Dentz */
2787195ef75eSPauli Virtanen
2788195ef75eSPauli Virtanen params = conn_params_copy(&hdev->pend_le_conns, &n);
2789195ef75eSPauli Virtanen if (!params) {
2790195ef75eSPauli Virtanen err = -ENOMEM;
2791ad383c2cSLuiz Augusto von Dentz goto done;
2792e8907f76SLuiz Augusto von Dentz }
2793e8907f76SLuiz Augusto von Dentz
2794195ef75eSPauli Virtanen for (i = 0; i < n; ++i) {
2795195ef75eSPauli Virtanen err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2796195ef75eSPauli Virtanen &num_entries);
2797195ef75eSPauli Virtanen if (err) {
2798195ef75eSPauli Virtanen kvfree(params);
2799195ef75eSPauli Virtanen goto done;
2800195ef75eSPauli Virtanen }
2801195ef75eSPauli Virtanen }
2802195ef75eSPauli Virtanen
2803195ef75eSPauli Virtanen kvfree(params);
2804195ef75eSPauli Virtanen
2805e8907f76SLuiz Augusto von Dentz /* After adding all new pending connections, walk through
2806e8907f76SLuiz Augusto von Dentz * the list of pending reports and also add these to the
2807e8907f76SLuiz Augusto von Dentz * accept list if there is still space. Abort if space runs out.
2808e8907f76SLuiz Augusto von Dentz */
2809195ef75eSPauli Virtanen
2810195ef75eSPauli Virtanen params = conn_params_copy(&hdev->pend_le_reports, &n);
2811195ef75eSPauli Virtanen if (!params) {
2812195ef75eSPauli Virtanen err = -ENOMEM;
2813ad383c2cSLuiz Augusto von Dentz goto done;
2814e8907f76SLuiz Augusto von Dentz }
2815e8907f76SLuiz Augusto von Dentz
2816195ef75eSPauli Virtanen for (i = 0; i < n; ++i) {
2817195ef75eSPauli Virtanen err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2818195ef75eSPauli Virtanen &num_entries);
2819195ef75eSPauli Virtanen if (err) {
2820195ef75eSPauli Virtanen kvfree(params);
2821195ef75eSPauli Virtanen goto done;
2822195ef75eSPauli Virtanen }
2823195ef75eSPauli Virtanen }
2824195ef75eSPauli Virtanen
2825195ef75eSPauli Virtanen kvfree(params);
2826195ef75eSPauli Virtanen
2827e8907f76SLuiz Augusto von Dentz /* Use the allowlist unless the following conditions are all true:
2828e8907f76SLuiz Augusto von Dentz * - We are not currently suspending
2829e8907f76SLuiz Augusto von Dentz * - There are 1 or more ADV monitors registered and it's not offloaded
2830e8907f76SLuiz Augusto von Dentz * - Interleaved scanning is not currently using the allowlist
2831e8907f76SLuiz Augusto von Dentz */
2832e8907f76SLuiz Augusto von Dentz if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2833e8907f76SLuiz Augusto von Dentz hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2834e8907f76SLuiz Augusto von Dentz hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2835ad383c2cSLuiz Augusto von Dentz err = -EINVAL;
2836ad383c2cSLuiz Augusto von Dentz
2837ad383c2cSLuiz Augusto von Dentz done:
283880740ebbSLuiz Augusto von Dentz filter_policy = err ? 0x00 : 0x01;
283980740ebbSLuiz Augusto von Dentz
2840ad383c2cSLuiz Augusto von Dentz /* Enable address resolution when LL Privacy is enabled. */
2841ad383c2cSLuiz Augusto von Dentz err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2842ad383c2cSLuiz Augusto von Dentz if (err)
2843ad383c2cSLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2844ad383c2cSLuiz Augusto von Dentz
2845ad383c2cSLuiz Augusto von Dentz /* Resume advertising if it was paused */
2846ad383c2cSLuiz Augusto von Dentz if (use_ll_privacy(hdev))
2847ad383c2cSLuiz Augusto von Dentz hci_resume_advertising_sync(hdev);
2848e8907f76SLuiz Augusto von Dentz
2849e8907f76SLuiz Augusto von Dentz /* Select filter policy to use accept list */
285080740ebbSLuiz Augusto von Dentz return filter_policy;
2851e8907f76SLuiz Augusto von Dentz }
hci_le_scan_phy_params(struct hci_cp_le_scan_phy_params * cp,u8 type,u16 interval,u16 window)2852e8907f76SLuiz Augusto von Dentz
2853de3c85d3SLuiz Augusto von Dentz static void hci_le_scan_phy_params(struct hci_cp_le_scan_phy_params *cp,
2854de3c85d3SLuiz Augusto von Dentz u8 type, u16 interval, u16 window)
2855de3c85d3SLuiz Augusto von Dentz {
2856de3c85d3SLuiz Augusto von Dentz cp->type = type;
2857de3c85d3SLuiz Augusto von Dentz cp->interval = cpu_to_le16(interval);
2858de3c85d3SLuiz Augusto von Dentz cp->window = cpu_to_le16(window);
2859de3c85d3SLuiz Augusto von Dentz }
hci_le_set_ext_scan_param_sync(struct hci_dev * hdev,u8 type,u16 interval,u16 window,u8 own_addr_type,u8 filter_policy)2860de3c85d3SLuiz Augusto von Dentz
2861e8907f76SLuiz Augusto von Dentz static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2862e8907f76SLuiz Augusto von Dentz u16 interval, u16 window,
2863e8907f76SLuiz Augusto von Dentz u8 own_addr_type, u8 filter_policy)
2864e8907f76SLuiz Augusto von Dentz {
2865e8907f76SLuiz Augusto von Dentz struct hci_cp_le_set_ext_scan_params *cp;
2866e8907f76SLuiz Augusto von Dentz struct hci_cp_le_scan_phy_params *phy;
2867e8907f76SLuiz Augusto von Dentz u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2868de3c85d3SLuiz Augusto von Dentz u8 num_phy = 0x00;
2869e8907f76SLuiz Augusto von Dentz
2870e8907f76SLuiz Augusto von Dentz cp = (void *)data;
2871e8907f76SLuiz Augusto von Dentz phy = (void *)cp->data;
2872e8907f76SLuiz Augusto von Dentz
2873e8907f76SLuiz Augusto von Dentz memset(data, 0, sizeof(data));
2874e8907f76SLuiz Augusto von Dentz
2875e8907f76SLuiz Augusto von Dentz cp->own_addr_type = own_addr_type;
2876e8907f76SLuiz Augusto von Dentz cp->filter_policy = filter_policy;
2877e8907f76SLuiz Augusto von Dentz
2878de3c85d3SLuiz Augusto von Dentz /* Check if PA Sync is in progress then select the PHY based on the
2879de3c85d3SLuiz Augusto von Dentz * hci_conn.iso_qos.
2880de3c85d3SLuiz Augusto von Dentz */
2881de3c85d3SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2882de3c85d3SLuiz Augusto von Dentz struct hci_cp_le_add_to_accept_list *sent;
2883de3c85d3SLuiz Augusto von Dentz
2884de3c85d3SLuiz Augusto von Dentz sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
2885de3c85d3SLuiz Augusto von Dentz if (sent) {
2886de3c85d3SLuiz Augusto von Dentz struct hci_conn *conn;
2887de3c85d3SLuiz Augusto von Dentz
2888de3c85d3SLuiz Augusto von Dentz conn = hci_conn_hash_lookup_ba(hdev, ISO_LINK,
2889de3c85d3SLuiz Augusto von Dentz &sent->bdaddr);
2890de3c85d3SLuiz Augusto von Dentz if (conn) {
2891de3c85d3SLuiz Augusto von Dentz struct bt_iso_qos *qos = &conn->iso_qos;
2892de3c85d3SLuiz Augusto von Dentz
2893de3c85d3SLuiz Augusto von Dentz if (qos->bcast.in.phy & BT_ISO_PHY_1M ||
2894de3c85d3SLuiz Augusto von Dentz qos->bcast.in.phy & BT_ISO_PHY_2M) {
2895de3c85d3SLuiz Augusto von Dentz cp->scanning_phys |= LE_SCAN_PHY_1M;
2896de3c85d3SLuiz Augusto von Dentz hci_le_scan_phy_params(phy, type,
2897de3c85d3SLuiz Augusto von Dentz interval,
2898de3c85d3SLuiz Augusto von Dentz window);
2899de3c85d3SLuiz Augusto von Dentz num_phy++;
2900de3c85d3SLuiz Augusto von Dentz phy++;
2901de3c85d3SLuiz Augusto von Dentz }
2902de3c85d3SLuiz Augusto von Dentz
2903de3c85d3SLuiz Augusto von Dentz if (qos->bcast.in.phy & BT_ISO_PHY_CODED) {
2904de3c85d3SLuiz Augusto von Dentz cp->scanning_phys |= LE_SCAN_PHY_CODED;
2905de3c85d3SLuiz Augusto von Dentz hci_le_scan_phy_params(phy, type,
2906cc950d58SLuiz Augusto von Dentz interval * 3,
2907cc950d58SLuiz Augusto von Dentz window * 3);
2908de3c85d3SLuiz Augusto von Dentz num_phy++;
2909de3c85d3SLuiz Augusto von Dentz phy++;
2910de3c85d3SLuiz Augusto von Dentz }
2911de3c85d3SLuiz Augusto von Dentz
2912de3c85d3SLuiz Augusto von Dentz if (num_phy)
2913de3c85d3SLuiz Augusto von Dentz goto done;
2914de3c85d3SLuiz Augusto von Dentz }
2915de3c85d3SLuiz Augusto von Dentz }
2916de3c85d3SLuiz Augusto von Dentz }
2917de3c85d3SLuiz Augusto von Dentz
2918e8907f76SLuiz Augusto von Dentz if (scan_1m(hdev) || scan_2m(hdev)) {
2919e8907f76SLuiz Augusto von Dentz cp->scanning_phys |= LE_SCAN_PHY_1M;
2920de3c85d3SLuiz Augusto von Dentz hci_le_scan_phy_params(phy, type, interval, window);
2921e8907f76SLuiz Augusto von Dentz num_phy++;
2922e8907f76SLuiz Augusto von Dentz phy++;
2923e8907f76SLuiz Augusto von Dentz }
2924e8907f76SLuiz Augusto von Dentz
2925e8907f76SLuiz Augusto von Dentz if (scan_coded(hdev)) {
2926e8907f76SLuiz Augusto von Dentz cp->scanning_phys |= LE_SCAN_PHY_CODED;
2927cc950d58SLuiz Augusto von Dentz hci_le_scan_phy_params(phy, type, interval * 3, window * 3);
2928e8907f76SLuiz Augusto von Dentz num_phy++;
2929e8907f76SLuiz Augusto von Dentz phy++;
2930e8907f76SLuiz Augusto von Dentz }
2931e8907f76SLuiz Augusto von Dentz
2932de3c85d3SLuiz Augusto von Dentz done:
2933de3c85d3SLuiz Augusto von Dentz if (!num_phy)
2934de3c85d3SLuiz Augusto von Dentz return -EINVAL;
2935de3c85d3SLuiz Augusto von Dentz
2936e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2937e8907f76SLuiz Augusto von Dentz sizeof(*cp) + sizeof(*phy) * num_phy,
2938e8907f76SLuiz Augusto von Dentz data, HCI_CMD_TIMEOUT);
2939e8907f76SLuiz Augusto von Dentz }
hci_le_set_scan_param_sync(struct hci_dev * hdev,u8 type,u16 interval,u16 window,u8 own_addr_type,u8 filter_policy)2940e8907f76SLuiz Augusto von Dentz
2941e8907f76SLuiz Augusto von Dentz static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2942e8907f76SLuiz Augusto von Dentz u16 interval, u16 window,
2943e8907f76SLuiz Augusto von Dentz u8 own_addr_type, u8 filter_policy)
2944e8907f76SLuiz Augusto von Dentz {
2945e8907f76SLuiz Augusto von Dentz struct hci_cp_le_set_scan_param cp;
2946e8907f76SLuiz Augusto von Dentz
2947e8907f76SLuiz Augusto von Dentz if (use_ext_scan(hdev))
2948e8907f76SLuiz Augusto von Dentz return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2949e8907f76SLuiz Augusto von Dentz window, own_addr_type,
2950e8907f76SLuiz Augusto von Dentz filter_policy);
2951e8907f76SLuiz Augusto von Dentz
2952e8907f76SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
2953e8907f76SLuiz Augusto von Dentz cp.type = type;
2954e8907f76SLuiz Augusto von Dentz cp.interval = cpu_to_le16(interval);
2955e8907f76SLuiz Augusto von Dentz cp.window = cpu_to_le16(window);
2956e8907f76SLuiz Augusto von Dentz cp.own_address_type = own_addr_type;
2957e8907f76SLuiz Augusto von Dentz cp.filter_policy = filter_policy;
2958e8907f76SLuiz Augusto von Dentz
2959e8907f76SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2960e8907f76SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2961e8907f76SLuiz Augusto von Dentz }
hci_start_scan_sync(struct hci_dev * hdev,u8 type,u16 interval,u16 window,u8 own_addr_type,u8 filter_policy,u8 filter_dup)2962e8907f76SLuiz Augusto von Dentz
2963e8907f76SLuiz Augusto von Dentz static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2964abfeea47SLuiz Augusto von Dentz u16 window, u8 own_addr_type, u8 filter_policy,
2965abfeea47SLuiz Augusto von Dentz u8 filter_dup)
2966e8907f76SLuiz Augusto von Dentz {
2967e8907f76SLuiz Augusto von Dentz int err;
2968e8907f76SLuiz Augusto von Dentz
2969e8907f76SLuiz Augusto von Dentz if (hdev->scanning_paused) {
2970e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Scanning is paused for suspend");
2971e8907f76SLuiz Augusto von Dentz return 0;
2972e8907f76SLuiz Augusto von Dentz }
2973e8907f76SLuiz Augusto von Dentz
2974e8907f76SLuiz Augusto von Dentz err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2975e8907f76SLuiz Augusto von Dentz own_addr_type, filter_policy);
2976e8907f76SLuiz Augusto von Dentz if (err)
2977e8907f76SLuiz Augusto von Dentz return err;
2978e8907f76SLuiz Augusto von Dentz
2979abfeea47SLuiz Augusto von Dentz return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2980e8907f76SLuiz Augusto von Dentz }
hci_passive_scan_sync(struct hci_dev * hdev)2981e8907f76SLuiz Augusto von Dentz
298227592ca1SLuiz Augusto von Dentz static int hci_passive_scan_sync(struct hci_dev *hdev)
2983e8907f76SLuiz Augusto von Dentz {
2984e8907f76SLuiz Augusto von Dentz u8 own_addr_type;
2985e8907f76SLuiz Augusto von Dentz u8 filter_policy;
2986e8907f76SLuiz Augusto von Dentz u16 window, interval;
2987b338d917SBrian Gix u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2988ad383c2cSLuiz Augusto von Dentz int err;
2989e8907f76SLuiz Augusto von Dentz
2990e8907f76SLuiz Augusto von Dentz if (hdev->scanning_paused) {
2991e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "Scanning is paused for suspend");
2992e8907f76SLuiz Augusto von Dentz return 0;
2993e8907f76SLuiz Augusto von Dentz }
2994e8907f76SLuiz Augusto von Dentz
2995ad383c2cSLuiz Augusto von Dentz err = hci_scan_disable_sync(hdev);
2996ad383c2cSLuiz Augusto von Dentz if (err) {
2997ad383c2cSLuiz Augusto von Dentz bt_dev_err(hdev, "disable scanning failed: %d", err);
2998ad383c2cSLuiz Augusto von Dentz return err;
2999ad383c2cSLuiz Augusto von Dentz }
3000ad383c2cSLuiz Augusto von Dentz
3001e8907f76SLuiz Augusto von Dentz /* Set require_privacy to false since no SCAN_REQ are send
3002e8907f76SLuiz Augusto von Dentz * during passive scanning. Not using an non-resolvable address
3003e8907f76SLuiz Augusto von Dentz * here is important so that peer devices using direct
3004e8907f76SLuiz Augusto von Dentz * advertising with our address will be correctly reported
3005e8907f76SLuiz Augusto von Dentz * by the controller.
3006e8907f76SLuiz Augusto von Dentz */
3007e8907f76SLuiz Augusto von Dentz if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
3008e8907f76SLuiz Augusto von Dentz &own_addr_type))
3009e8907f76SLuiz Augusto von Dentz return 0;
3010e8907f76SLuiz Augusto von Dentz
3011e8907f76SLuiz Augusto von Dentz if (hdev->enable_advmon_interleave_scan &&
3012e8907f76SLuiz Augusto von Dentz hci_update_interleaved_scan_sync(hdev))
3013e8907f76SLuiz Augusto von Dentz return 0;
3014e8907f76SLuiz Augusto von Dentz
3015e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
3016ad383c2cSLuiz Augusto von Dentz
3017e8907f76SLuiz Augusto von Dentz /* Adding or removing entries from the accept list must
3018e8907f76SLuiz Augusto von Dentz * happen before enabling scanning. The controller does
3019e8907f76SLuiz Augusto von Dentz * not allow accept list modification while scanning.
3020e8907f76SLuiz Augusto von Dentz */
3021e8907f76SLuiz Augusto von Dentz filter_policy = hci_update_accept_list_sync(hdev);
3022e8907f76SLuiz Augusto von Dentz
3023ae8853d0SLuiz Augusto von Dentz /* If suspended and filter_policy set to 0x00 (no acceptlist) then
3024ae8853d0SLuiz Augusto von Dentz * passive scanning cannot be started since that would require the host
3025ae8853d0SLuiz Augusto von Dentz * to be woken up to process the reports.
3026ae8853d0SLuiz Augusto von Dentz */
3027ae8853d0SLuiz Augusto von Dentz if (hdev->suspended && !filter_policy) {
3028ae8853d0SLuiz Augusto von Dentz /* Check if accept list is empty then there is no need to scan
3029ae8853d0SLuiz Augusto von Dentz * while suspended.
3030ae8853d0SLuiz Augusto von Dentz */
3031ae8853d0SLuiz Augusto von Dentz if (list_empty(&hdev->le_accept_list))
3032ae8853d0SLuiz Augusto von Dentz return 0;
3033ae8853d0SLuiz Augusto von Dentz
3034ae8853d0SLuiz Augusto von Dentz /* If there are devices is the accept_list that means some
3035ae8853d0SLuiz Augusto von Dentz * devices could not be programmed which in non-suspended case
3036ae8853d0SLuiz Augusto von Dentz * means filter_policy needs to be set to 0x00 so the host needs
3037ae8853d0SLuiz Augusto von Dentz * to filter, but since this is treating suspended case we
3038ae8853d0SLuiz Augusto von Dentz * can ignore device needing host to filter to allow devices in
3039ae8853d0SLuiz Augusto von Dentz * the acceptlist to be able to wakeup the system.
3040ae8853d0SLuiz Augusto von Dentz */
3041ae8853d0SLuiz Augusto von Dentz filter_policy = 0x01;
3042ae8853d0SLuiz Augusto von Dentz }
3043ae8853d0SLuiz Augusto von Dentz
3044e8907f76SLuiz Augusto von Dentz /* When the controller is using random resolvable addresses and
3045e8907f76SLuiz Augusto von Dentz * with that having LE privacy enabled, then controllers with
3046e8907f76SLuiz Augusto von Dentz * Extended Scanner Filter Policies support can now enable support
3047e8907f76SLuiz Augusto von Dentz * for handling directed advertising.
3048e8907f76SLuiz Augusto von Dentz *
3049e8907f76SLuiz Augusto von Dentz * So instead of using filter polices 0x00 (no acceptlist)
3050e8907f76SLuiz Augusto von Dentz * and 0x01 (acceptlist enabled) use the new filter policies
3051e8907f76SLuiz Augusto von Dentz * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
3052e8907f76SLuiz Augusto von Dentz */
3053e8907f76SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
3054e8907f76SLuiz Augusto von Dentz (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
3055e8907f76SLuiz Augusto von Dentz filter_policy |= 0x02;
3056e8907f76SLuiz Augusto von Dentz
3057e8907f76SLuiz Augusto von Dentz if (hdev->suspended) {
3058e8907f76SLuiz Augusto von Dentz window = hdev->le_scan_window_suspend;
3059e8907f76SLuiz Augusto von Dentz interval = hdev->le_scan_int_suspend;
3060e8907f76SLuiz Augusto von Dentz } else if (hci_is_le_conn_scanning(hdev)) {
3061e8907f76SLuiz Augusto von Dentz window = hdev->le_scan_window_connect;
3062e8907f76SLuiz Augusto von Dentz interval = hdev->le_scan_int_connect;
3063e8907f76SLuiz Augusto von Dentz } else if (hci_is_adv_monitoring(hdev)) {
3064e8907f76SLuiz Augusto von Dentz window = hdev->le_scan_window_adv_monitor;
3065e8907f76SLuiz Augusto von Dentz interval = hdev->le_scan_int_adv_monitor;
30661a7a06e9SAnton Khirnov
30671a7a06e9SAnton Khirnov /* Disable duplicates filter when scanning for advertisement
30681a7a06e9SAnton Khirnov * monitor for the following reasons.
30691a7a06e9SAnton Khirnov *
30701a7a06e9SAnton Khirnov * For HW pattern filtering (ex. MSFT), Realtek and Qualcomm
30711a7a06e9SAnton Khirnov * controllers ignore RSSI_Sampling_Period when the duplicates
30721a7a06e9SAnton Khirnov * filter is enabled.
30731a7a06e9SAnton Khirnov *
30741a7a06e9SAnton Khirnov * For SW pattern filtering, when we're not doing interleaved
30751a7a06e9SAnton Khirnov * scanning, it is necessary to disable duplicates filter,
30761a7a06e9SAnton Khirnov * otherwise hosts can only receive one advertisement and it's
30771a7a06e9SAnton Khirnov * impossible to know if a peer is still in range.
30781a7a06e9SAnton Khirnov */
30791a7a06e9SAnton Khirnov filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
3080e8907f76SLuiz Augusto von Dentz } else {
3081e8907f76SLuiz Augusto von Dentz window = hdev->le_scan_window;
3082e8907f76SLuiz Augusto von Dentz interval = hdev->le_scan_interval;
3083e8907f76SLuiz Augusto von Dentz }
3084e8907f76SLuiz Augusto von Dentz
3085b338d917SBrian Gix /* Disable all filtering for Mesh */
3086b338d917SBrian Gix if (hci_dev_test_flag(hdev, HCI_MESH)) {
3087b338d917SBrian Gix filter_policy = 0;
3088b338d917SBrian Gix filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
3089b338d917SBrian Gix }
3090b338d917SBrian Gix
3091e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
3092e8907f76SLuiz Augusto von Dentz
3093e8907f76SLuiz Augusto von Dentz return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
3094b338d917SBrian Gix own_addr_type, filter_policy, filter_dups);
3095e8907f76SLuiz Augusto von Dentz }
3096e8907f76SLuiz Augusto von Dentz
3097e8907f76SLuiz Augusto von Dentz /* This function controls the passive scanning based on hdev->pend_le_conns
3098e8907f76SLuiz Augusto von Dentz * list. If there are pending LE connection we start the background scanning,
3099ad383c2cSLuiz Augusto von Dentz * otherwise we stop it in the following sequence:
3100ad383c2cSLuiz Augusto von Dentz *
3101ad383c2cSLuiz Augusto von Dentz * If there are devices to scan:
3102ad383c2cSLuiz Augusto von Dentz *
3103ad383c2cSLuiz Augusto von Dentz * Disable Scanning -> Update Accept List ->
3104ad383c2cSLuiz Augusto von Dentz * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
3105ad383c2cSLuiz Augusto von Dentz * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
3106ad383c2cSLuiz Augusto von Dentz * Enable Scanning
3107ad383c2cSLuiz Augusto von Dentz *
3108ad383c2cSLuiz Augusto von Dentz * Otherwise:
3109ad383c2cSLuiz Augusto von Dentz *
3110ad383c2cSLuiz Augusto von Dentz * Disable Scanning
hci_update_passive_scan_sync(struct hci_dev * hdev)3111e8907f76SLuiz Augusto von Dentz */
3112e8907f76SLuiz Augusto von Dentz int hci_update_passive_scan_sync(struct hci_dev *hdev)
3113e8907f76SLuiz Augusto von Dentz {
3114e8907f76SLuiz Augusto von Dentz int err;
3115e8907f76SLuiz Augusto von Dentz
3116e8907f76SLuiz Augusto von Dentz if (!test_bit(HCI_UP, &hdev->flags) ||
3117e8907f76SLuiz Augusto von Dentz test_bit(HCI_INIT, &hdev->flags) ||
3118e8907f76SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_SETUP) ||
3119e8907f76SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_CONFIG) ||
3120e8907f76SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
3121e8907f76SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_UNREGISTER))
3122e8907f76SLuiz Augusto von Dentz return 0;
3123e8907f76SLuiz Augusto von Dentz
3124e8907f76SLuiz Augusto von Dentz /* No point in doing scanning if LE support hasn't been enabled */
3125e8907f76SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3126e8907f76SLuiz Augusto von Dentz return 0;
3127e8907f76SLuiz Augusto von Dentz
3128e8907f76SLuiz Augusto von Dentz /* If discovery is active don't interfere with it */
3129e8907f76SLuiz Augusto von Dentz if (hdev->discovery.state != DISCOVERY_STOPPED)
3130e8907f76SLuiz Augusto von Dentz return 0;
3131e8907f76SLuiz Augusto von Dentz
3132e8907f76SLuiz Augusto von Dentz /* Reset RSSI and UUID filters when starting background scanning
3133e8907f76SLuiz Augusto von Dentz * since these filters are meant for service discovery only.
3134e8907f76SLuiz Augusto von Dentz *
3135e8907f76SLuiz Augusto von Dentz * The Start Discovery and Start Service Discovery operations
3136e8907f76SLuiz Augusto von Dentz * ensure to set proper values for RSSI threshold and UUID
3137e8907f76SLuiz Augusto von Dentz * filter list. So it is safe to just reset them here.
3138e8907f76SLuiz Augusto von Dentz */
3139e8907f76SLuiz Augusto von Dentz hci_discovery_filter_clear(hdev);
3140e8907f76SLuiz Augusto von Dentz
3141e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "ADV monitoring is %s",
3142e8907f76SLuiz Augusto von Dentz hci_is_adv_monitoring(hdev) ? "on" : "off");
3143e8907f76SLuiz Augusto von Dentz
3144b338d917SBrian Gix if (!hci_dev_test_flag(hdev, HCI_MESH) &&
3145b338d917SBrian Gix list_empty(&hdev->pend_le_conns) &&
3146e8907f76SLuiz Augusto von Dentz list_empty(&hdev->pend_le_reports) &&
3147eca0ae4aSLuiz Augusto von Dentz !hci_is_adv_monitoring(hdev) &&
3148eca0ae4aSLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
3149e8907f76SLuiz Augusto von Dentz /* If there is no pending LE connections or devices
3150e8907f76SLuiz Augusto von Dentz * to be scanned for or no ADV monitors, we should stop the
3151e8907f76SLuiz Augusto von Dentz * background scanning.
3152e8907f76SLuiz Augusto von Dentz */
3153e8907f76SLuiz Augusto von Dentz
3154e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "stopping background scanning");
3155e8907f76SLuiz Augusto von Dentz
3156ad383c2cSLuiz Augusto von Dentz err = hci_scan_disable_sync(hdev);
3157e8907f76SLuiz Augusto von Dentz if (err)
3158e8907f76SLuiz Augusto von Dentz bt_dev_err(hdev, "stop background scanning failed: %d",
3159e8907f76SLuiz Augusto von Dentz err);
3160e8907f76SLuiz Augusto von Dentz } else {
3161e8907f76SLuiz Augusto von Dentz /* If there is at least one pending LE connection, we should
3162e8907f76SLuiz Augusto von Dentz * keep the background scan running.
3163e8907f76SLuiz Augusto von Dentz */
3164e8907f76SLuiz Augusto von Dentz
3165e8907f76SLuiz Augusto von Dentz /* If controller is connecting, we should not start scanning
3166e8907f76SLuiz Augusto von Dentz * since some controllers are not able to scan and connect at
3167e8907f76SLuiz Augusto von Dentz * the same time.
3168e8907f76SLuiz Augusto von Dentz */
3169e8907f76SLuiz Augusto von Dentz if (hci_lookup_le_connect(hdev))
3170e8907f76SLuiz Augusto von Dentz return 0;
3171e8907f76SLuiz Augusto von Dentz
3172e8907f76SLuiz Augusto von Dentz bt_dev_dbg(hdev, "start background scanning");
3173e8907f76SLuiz Augusto von Dentz
3174e8907f76SLuiz Augusto von Dentz err = hci_passive_scan_sync(hdev);
3175e8907f76SLuiz Augusto von Dentz if (err)
3176e8907f76SLuiz Augusto von Dentz bt_dev_err(hdev, "start background scanning failed: %d",
3177e8907f76SLuiz Augusto von Dentz err);
3178e8907f76SLuiz Augusto von Dentz }
3179e8907f76SLuiz Augusto von Dentz
3180e8907f76SLuiz Augusto von Dentz return err;
3181e8907f76SLuiz Augusto von Dentz }
update_scan_sync(struct hci_dev * hdev,void * data)3182ad383c2cSLuiz Augusto von Dentz
3183bb876725SBrian Gix static int update_scan_sync(struct hci_dev *hdev, void *data)
3184bb876725SBrian Gix {
3185bb876725SBrian Gix return hci_update_scan_sync(hdev);
3186bb876725SBrian Gix }
hci_update_scan(struct hci_dev * hdev)3187bb876725SBrian Gix
3188bb876725SBrian Gix int hci_update_scan(struct hci_dev *hdev)
3189bb876725SBrian Gix {
3190bb876725SBrian Gix return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
3191bb876725SBrian Gix }
update_passive_scan_sync(struct hci_dev * hdev,void * data)3192bb876725SBrian Gix
3193ad383c2cSLuiz Augusto von Dentz static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
3194ad383c2cSLuiz Augusto von Dentz {
3195ad383c2cSLuiz Augusto von Dentz return hci_update_passive_scan_sync(hdev);
3196ad383c2cSLuiz Augusto von Dentz }
hci_update_passive_scan(struct hci_dev * hdev)3197ad383c2cSLuiz Augusto von Dentz
3198ad383c2cSLuiz Augusto von Dentz int hci_update_passive_scan(struct hci_dev *hdev)
3199ad383c2cSLuiz Augusto von Dentz {
32005bee2fd6SLuiz Augusto von Dentz /* Only queue if it would have any effect */
32015bee2fd6SLuiz Augusto von Dentz if (!test_bit(HCI_UP, &hdev->flags) ||
32025bee2fd6SLuiz Augusto von Dentz test_bit(HCI_INIT, &hdev->flags) ||
32035bee2fd6SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_SETUP) ||
32045bee2fd6SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_CONFIG) ||
32055bee2fd6SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
32065bee2fd6SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_UNREGISTER))
32075bee2fd6SLuiz Augusto von Dentz return 0;
32085bee2fd6SLuiz Augusto von Dentz
32091499f799SLuiz Augusto von Dentz return hci_cmd_sync_queue_once(hdev, update_passive_scan_sync, NULL,
32101499f799SLuiz Augusto von Dentz NULL);
3211ad383c2cSLuiz Augusto von Dentz }
hci_write_sc_support_sync(struct hci_dev * hdev,u8 val)3212cf75ad8bSLuiz Augusto von Dentz
32132f2eb0c9SBrian Gix int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
3214cf75ad8bSLuiz Augusto von Dentz {
32152f2eb0c9SBrian Gix int err;
32162f2eb0c9SBrian Gix
3217cf75ad8bSLuiz Augusto von Dentz if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
3218cf75ad8bSLuiz Augusto von Dentz return 0;
3219cf75ad8bSLuiz Augusto von Dentz
32202f2eb0c9SBrian Gix err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3221cf75ad8bSLuiz Augusto von Dentz sizeof(val), &val, HCI_CMD_TIMEOUT);
32222f2eb0c9SBrian Gix
32232f2eb0c9SBrian Gix if (!err) {
32242f2eb0c9SBrian Gix if (val) {
32252f2eb0c9SBrian Gix hdev->features[1][0] |= LMP_HOST_SC;
32262f2eb0c9SBrian Gix hci_dev_set_flag(hdev, HCI_SC_ENABLED);
32272f2eb0c9SBrian Gix } else {
32282f2eb0c9SBrian Gix hdev->features[1][0] &= ~LMP_HOST_SC;
32292f2eb0c9SBrian Gix hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
32302f2eb0c9SBrian Gix }
32312f2eb0c9SBrian Gix }
32322f2eb0c9SBrian Gix
32332f2eb0c9SBrian Gix return err;
3234cf75ad8bSLuiz Augusto von Dentz }
hci_write_ssp_mode_sync(struct hci_dev * hdev,u8 mode)3235cf75ad8bSLuiz Augusto von Dentz
32363244845cSBrian Gix int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
3237cf75ad8bSLuiz Augusto von Dentz {
3238cf75ad8bSLuiz Augusto von Dentz int err;
3239cf75ad8bSLuiz Augusto von Dentz
3240cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3241cf75ad8bSLuiz Augusto von Dentz lmp_host_ssp_capable(hdev))
3242cf75ad8bSLuiz Augusto von Dentz return 0;
3243cf75ad8bSLuiz Augusto von Dentz
32443244845cSBrian Gix if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
32453244845cSBrian Gix __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
32463244845cSBrian Gix sizeof(mode), &mode, HCI_CMD_TIMEOUT);
32473244845cSBrian Gix }
32483244845cSBrian Gix
3249cf75ad8bSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3250cf75ad8bSLuiz Augusto von Dentz sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3251cf75ad8bSLuiz Augusto von Dentz if (err)
3252cf75ad8bSLuiz Augusto von Dentz return err;
3253cf75ad8bSLuiz Augusto von Dentz
3254cf75ad8bSLuiz Augusto von Dentz return hci_write_sc_support_sync(hdev, 0x01);
3255cf75ad8bSLuiz Augusto von Dentz }
hci_write_le_host_supported_sync(struct hci_dev * hdev,u8 le,u8 simul)3256cf75ad8bSLuiz Augusto von Dentz
3257d81a494cSBrian Gix int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
3258cf75ad8bSLuiz Augusto von Dentz {
3259cf75ad8bSLuiz Augusto von Dentz struct hci_cp_write_le_host_supported cp;
3260cf75ad8bSLuiz Augusto von Dentz
3261cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
3262cf75ad8bSLuiz Augusto von Dentz !lmp_bredr_capable(hdev))
3263cf75ad8bSLuiz Augusto von Dentz return 0;
3264cf75ad8bSLuiz Augusto von Dentz
3265cf75ad8bSLuiz Augusto von Dentz /* Check first if we already have the right host state
3266cf75ad8bSLuiz Augusto von Dentz * (host features set)
3267cf75ad8bSLuiz Augusto von Dentz */
3268cf75ad8bSLuiz Augusto von Dentz if (le == lmp_host_le_capable(hdev) &&
3269cf75ad8bSLuiz Augusto von Dentz simul == lmp_host_le_br_capable(hdev))
3270cf75ad8bSLuiz Augusto von Dentz return 0;
3271cf75ad8bSLuiz Augusto von Dentz
3272cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
3273cf75ad8bSLuiz Augusto von Dentz
3274cf75ad8bSLuiz Augusto von Dentz cp.le = le;
3275cf75ad8bSLuiz Augusto von Dentz cp.simul = simul;
3276cf75ad8bSLuiz Augusto von Dentz
3277cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3278cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3279cf75ad8bSLuiz Augusto von Dentz }
hci_powered_update_adv_sync(struct hci_dev * hdev)3280cf75ad8bSLuiz Augusto von Dentz
3281cf75ad8bSLuiz Augusto von Dentz static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3282cf75ad8bSLuiz Augusto von Dentz {
3283cf75ad8bSLuiz Augusto von Dentz struct adv_info *adv, *tmp;
3284cf75ad8bSLuiz Augusto von Dentz int err;
3285cf75ad8bSLuiz Augusto von Dentz
3286cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3287cf75ad8bSLuiz Augusto von Dentz return 0;
3288cf75ad8bSLuiz Augusto von Dentz
3289cf75ad8bSLuiz Augusto von Dentz /* If RPA Resolution has not been enable yet it means the
3290cf75ad8bSLuiz Augusto von Dentz * resolving list is empty and we should attempt to program the
3291cf75ad8bSLuiz Augusto von Dentz * local IRK in order to support using own_addr_type
3292cf75ad8bSLuiz Augusto von Dentz * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
3293cf75ad8bSLuiz Augusto von Dentz */
3294cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3295cf75ad8bSLuiz Augusto von Dentz hci_le_add_resolve_list_sync(hdev, NULL);
3296cf75ad8bSLuiz Augusto von Dentz hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3297cf75ad8bSLuiz Augusto von Dentz }
3298cf75ad8bSLuiz Augusto von Dentz
3299cf75ad8bSLuiz Augusto von Dentz /* Make sure the controller has a good default for
3300cf75ad8bSLuiz Augusto von Dentz * advertising data. This also applies to the case
3301cf75ad8bSLuiz Augusto von Dentz * where BR/EDR was toggled during the AUTO_OFF phase.
3302cf75ad8bSLuiz Augusto von Dentz */
3303cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3304cf75ad8bSLuiz Augusto von Dentz list_empty(&hdev->adv_instances)) {
3305cf75ad8bSLuiz Augusto von Dentz if (ext_adv_capable(hdev)) {
3306cf75ad8bSLuiz Augusto von Dentz err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3307cf75ad8bSLuiz Augusto von Dentz if (!err)
3308cf75ad8bSLuiz Augusto von Dentz hci_update_scan_rsp_data_sync(hdev, 0x00);
3309cf75ad8bSLuiz Augusto von Dentz } else {
3310cf75ad8bSLuiz Augusto von Dentz err = hci_update_adv_data_sync(hdev, 0x00);
3311cf75ad8bSLuiz Augusto von Dentz if (!err)
3312cf75ad8bSLuiz Augusto von Dentz hci_update_scan_rsp_data_sync(hdev, 0x00);
3313cf75ad8bSLuiz Augusto von Dentz }
3314cf75ad8bSLuiz Augusto von Dentz
3315cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3316cf75ad8bSLuiz Augusto von Dentz hci_enable_advertising_sync(hdev);
3317cf75ad8bSLuiz Augusto von Dentz }
3318cf75ad8bSLuiz Augusto von Dentz
3319cf75ad8bSLuiz Augusto von Dentz /* Call for each tracked instance to be scheduled */
3320cf75ad8bSLuiz Augusto von Dentz list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3321cf75ad8bSLuiz Augusto von Dentz hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3322cf75ad8bSLuiz Augusto von Dentz
3323cf75ad8bSLuiz Augusto von Dentz return 0;
3324cf75ad8bSLuiz Augusto von Dentz }
hci_write_auth_enable_sync(struct hci_dev * hdev)3325cf75ad8bSLuiz Augusto von Dentz
3326cf75ad8bSLuiz Augusto von Dentz static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3327cf75ad8bSLuiz Augusto von Dentz {
3328cf75ad8bSLuiz Augusto von Dentz u8 link_sec;
3329cf75ad8bSLuiz Augusto von Dentz
3330cf75ad8bSLuiz Augusto von Dentz link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3331cf75ad8bSLuiz Augusto von Dentz if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3332cf75ad8bSLuiz Augusto von Dentz return 0;
3333cf75ad8bSLuiz Augusto von Dentz
3334cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3335cf75ad8bSLuiz Augusto von Dentz sizeof(link_sec), &link_sec,
3336cf75ad8bSLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
3337cf75ad8bSLuiz Augusto von Dentz }
hci_write_fast_connectable_sync(struct hci_dev * hdev,bool enable)3338cf75ad8bSLuiz Augusto von Dentz
3339353a0249SBrian Gix int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3340cf75ad8bSLuiz Augusto von Dentz {
3341cf75ad8bSLuiz Augusto von Dentz struct hci_cp_write_page_scan_activity cp;
3342cf75ad8bSLuiz Augusto von Dentz u8 type;
3343cf75ad8bSLuiz Augusto von Dentz int err = 0;
3344cf75ad8bSLuiz Augusto von Dentz
3345cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3346cf75ad8bSLuiz Augusto von Dentz return 0;
3347cf75ad8bSLuiz Augusto von Dentz
3348cf75ad8bSLuiz Augusto von Dentz if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3349cf75ad8bSLuiz Augusto von Dentz return 0;
3350cf75ad8bSLuiz Augusto von Dentz
3351cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
3352cf75ad8bSLuiz Augusto von Dentz
3353cf75ad8bSLuiz Augusto von Dentz if (enable) {
3354cf75ad8bSLuiz Augusto von Dentz type = PAGE_SCAN_TYPE_INTERLACED;
3355cf75ad8bSLuiz Augusto von Dentz
3356cf75ad8bSLuiz Augusto von Dentz /* 160 msec page scan interval */
3357cf75ad8bSLuiz Augusto von Dentz cp.interval = cpu_to_le16(0x0100);
3358cf75ad8bSLuiz Augusto von Dentz } else {
3359cf75ad8bSLuiz Augusto von Dentz type = hdev->def_page_scan_type;
3360cf75ad8bSLuiz Augusto von Dentz cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3361cf75ad8bSLuiz Augusto von Dentz }
3362cf75ad8bSLuiz Augusto von Dentz
3363cf75ad8bSLuiz Augusto von Dentz cp.window = cpu_to_le16(hdev->def_page_scan_window);
3364cf75ad8bSLuiz Augusto von Dentz
3365cf75ad8bSLuiz Augusto von Dentz if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3366cf75ad8bSLuiz Augusto von Dentz __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3367cf75ad8bSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev,
3368cf75ad8bSLuiz Augusto von Dentz HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
3369cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3370cf75ad8bSLuiz Augusto von Dentz if (err)
3371cf75ad8bSLuiz Augusto von Dentz return err;
3372cf75ad8bSLuiz Augusto von Dentz }
3373cf75ad8bSLuiz Augusto von Dentz
3374cf75ad8bSLuiz Augusto von Dentz if (hdev->page_scan_type != type)
3375cf75ad8bSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev,
3376cf75ad8bSLuiz Augusto von Dentz HCI_OP_WRITE_PAGE_SCAN_TYPE,
3377cf75ad8bSLuiz Augusto von Dentz sizeof(type), &type,
3378cf75ad8bSLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
3379cf75ad8bSLuiz Augusto von Dentz
3380cf75ad8bSLuiz Augusto von Dentz return err;
3381cf75ad8bSLuiz Augusto von Dentz }
disconnected_accept_list_entries(struct hci_dev * hdev)3382cf75ad8bSLuiz Augusto von Dentz
3383cf75ad8bSLuiz Augusto von Dentz static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3384cf75ad8bSLuiz Augusto von Dentz {
3385cf75ad8bSLuiz Augusto von Dentz struct bdaddr_list *b;
3386cf75ad8bSLuiz Augusto von Dentz
3387cf75ad8bSLuiz Augusto von Dentz list_for_each_entry(b, &hdev->accept_list, list) {
3388cf75ad8bSLuiz Augusto von Dentz struct hci_conn *conn;
3389cf75ad8bSLuiz Augusto von Dentz
3390cf75ad8bSLuiz Augusto von Dentz conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3391cf75ad8bSLuiz Augusto von Dentz if (!conn)
3392cf75ad8bSLuiz Augusto von Dentz return true;
3393cf75ad8bSLuiz Augusto von Dentz
3394cf75ad8bSLuiz Augusto von Dentz if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3395cf75ad8bSLuiz Augusto von Dentz return true;
3396cf75ad8bSLuiz Augusto von Dentz }
3397cf75ad8bSLuiz Augusto von Dentz
3398cf75ad8bSLuiz Augusto von Dentz return false;
3399cf75ad8bSLuiz Augusto von Dentz }
hci_write_scan_enable_sync(struct hci_dev * hdev,u8 val)3400cf75ad8bSLuiz Augusto von Dentz
3401cf75ad8bSLuiz Augusto von Dentz static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3402cf75ad8bSLuiz Augusto von Dentz {
3403cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3404cf75ad8bSLuiz Augusto von Dentz sizeof(val), &val,
3405cf75ad8bSLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
3406cf75ad8bSLuiz Augusto von Dentz }
hci_update_scan_sync(struct hci_dev * hdev)3407cf75ad8bSLuiz Augusto von Dentz
3408451d95a9SBrian Gix int hci_update_scan_sync(struct hci_dev *hdev)
3409cf75ad8bSLuiz Augusto von Dentz {
3410cf75ad8bSLuiz Augusto von Dentz u8 scan;
3411cf75ad8bSLuiz Augusto von Dentz
3412cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3413cf75ad8bSLuiz Augusto von Dentz return 0;
3414cf75ad8bSLuiz Augusto von Dentz
3415cf75ad8bSLuiz Augusto von Dentz if (!hdev_is_powered(hdev))
3416cf75ad8bSLuiz Augusto von Dentz return 0;
3417cf75ad8bSLuiz Augusto von Dentz
3418cf75ad8bSLuiz Augusto von Dentz if (mgmt_powering_down(hdev))
3419cf75ad8bSLuiz Augusto von Dentz return 0;
3420cf75ad8bSLuiz Augusto von Dentz
3421cf75ad8bSLuiz Augusto von Dentz if (hdev->scanning_paused)
3422cf75ad8bSLuiz Augusto von Dentz return 0;
3423cf75ad8bSLuiz Augusto von Dentz
3424cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3425cf75ad8bSLuiz Augusto von Dentz disconnected_accept_list_entries(hdev))
3426cf75ad8bSLuiz Augusto von Dentz scan = SCAN_PAGE;
3427cf75ad8bSLuiz Augusto von Dentz else
3428cf75ad8bSLuiz Augusto von Dentz scan = SCAN_DISABLED;
3429cf75ad8bSLuiz Augusto von Dentz
3430cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3431cf75ad8bSLuiz Augusto von Dentz scan |= SCAN_INQUIRY;
3432cf75ad8bSLuiz Augusto von Dentz
3433cf75ad8bSLuiz Augusto von Dentz if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3434cf75ad8bSLuiz Augusto von Dentz test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3435cf75ad8bSLuiz Augusto von Dentz return 0;
3436cf75ad8bSLuiz Augusto von Dentz
3437cf75ad8bSLuiz Augusto von Dentz return hci_write_scan_enable_sync(hdev, scan);
3438cf75ad8bSLuiz Augusto von Dentz }
hci_update_name_sync(struct hci_dev * hdev)3439cf75ad8bSLuiz Augusto von Dentz
34406f6ff38aSBrian Gix int hci_update_name_sync(struct hci_dev *hdev)
3441cf75ad8bSLuiz Augusto von Dentz {
3442cf75ad8bSLuiz Augusto von Dentz struct hci_cp_write_local_name cp;
3443cf75ad8bSLuiz Augusto von Dentz
3444cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
3445cf75ad8bSLuiz Augusto von Dentz
3446cf75ad8bSLuiz Augusto von Dentz memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3447cf75ad8bSLuiz Augusto von Dentz
3448cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3449cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp,
3450cf75ad8bSLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
3451cf75ad8bSLuiz Augusto von Dentz }
3452cf75ad8bSLuiz Augusto von Dentz
3453cf75ad8bSLuiz Augusto von Dentz /* This function perform powered update HCI command sequence after the HCI init
3454cf75ad8bSLuiz Augusto von Dentz * sequence which end up resetting all states, the sequence is as follows:
3455cf75ad8bSLuiz Augusto von Dentz *
3456cf75ad8bSLuiz Augusto von Dentz * HCI_SSP_ENABLED(Enable SSP)
3457cf75ad8bSLuiz Augusto von Dentz * HCI_LE_ENABLED(Enable LE)
3458cf75ad8bSLuiz Augusto von Dentz * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
3459cf75ad8bSLuiz Augusto von Dentz * Update adv data)
3460cf75ad8bSLuiz Augusto von Dentz * Enable Authentication
3461cf75ad8bSLuiz Augusto von Dentz * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3462cf75ad8bSLuiz Augusto von Dentz * Set Name -> Set EIR)
3463e411443cSLuiz Augusto von Dentz * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address)
hci_powered_update_sync(struct hci_dev * hdev)3464cf75ad8bSLuiz Augusto von Dentz */
3465cf75ad8bSLuiz Augusto von Dentz int hci_powered_update_sync(struct hci_dev *hdev)
3466cf75ad8bSLuiz Augusto von Dentz {
3467cf75ad8bSLuiz Augusto von Dentz int err;
3468cf75ad8bSLuiz Augusto von Dentz
3469cf75ad8bSLuiz Augusto von Dentz /* Register the available SMP channels (BR/EDR and LE) only when
3470cf75ad8bSLuiz Augusto von Dentz * successfully powering on the controller. This late
3471cf75ad8bSLuiz Augusto von Dentz * registration is required so that LE SMP can clearly decide if
3472cf75ad8bSLuiz Augusto von Dentz * the public address or static address is used.
3473cf75ad8bSLuiz Augusto von Dentz */
3474cf75ad8bSLuiz Augusto von Dentz smp_register(hdev);
3475cf75ad8bSLuiz Augusto von Dentz
3476cf75ad8bSLuiz Augusto von Dentz err = hci_write_ssp_mode_sync(hdev, 0x01);
3477cf75ad8bSLuiz Augusto von Dentz if (err)
3478cf75ad8bSLuiz Augusto von Dentz return err;
3479cf75ad8bSLuiz Augusto von Dentz
3480cf75ad8bSLuiz Augusto von Dentz err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3481cf75ad8bSLuiz Augusto von Dentz if (err)
3482cf75ad8bSLuiz Augusto von Dentz return err;
3483cf75ad8bSLuiz Augusto von Dentz
3484cf75ad8bSLuiz Augusto von Dentz err = hci_powered_update_adv_sync(hdev);
3485cf75ad8bSLuiz Augusto von Dentz if (err)
3486cf75ad8bSLuiz Augusto von Dentz return err;
3487cf75ad8bSLuiz Augusto von Dentz
3488cf75ad8bSLuiz Augusto von Dentz err = hci_write_auth_enable_sync(hdev);
3489cf75ad8bSLuiz Augusto von Dentz if (err)
3490cf75ad8bSLuiz Augusto von Dentz return err;
3491cf75ad8bSLuiz Augusto von Dentz
3492cf75ad8bSLuiz Augusto von Dentz if (lmp_bredr_capable(hdev)) {
3493cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3494cf75ad8bSLuiz Augusto von Dentz hci_write_fast_connectable_sync(hdev, true);
3495cf75ad8bSLuiz Augusto von Dentz else
3496cf75ad8bSLuiz Augusto von Dentz hci_write_fast_connectable_sync(hdev, false);
3497cf75ad8bSLuiz Augusto von Dentz hci_update_scan_sync(hdev);
3498cf75ad8bSLuiz Augusto von Dentz hci_update_class_sync(hdev);
3499cf75ad8bSLuiz Augusto von Dentz hci_update_name_sync(hdev);
3500cf75ad8bSLuiz Augusto von Dentz hci_update_eir_sync(hdev);
3501cf75ad8bSLuiz Augusto von Dentz }
3502cf75ad8bSLuiz Augusto von Dentz
3503e411443cSLuiz Augusto von Dentz /* If forcing static address is in use or there is no public
3504e411443cSLuiz Augusto von Dentz * address use the static address as random address (but skip
3505e411443cSLuiz Augusto von Dentz * the HCI command if the current random address is already the
3506e411443cSLuiz Augusto von Dentz * static one.
3507e411443cSLuiz Augusto von Dentz *
3508e411443cSLuiz Augusto von Dentz * In case BR/EDR has been disabled on a dual-mode controller
3509e411443cSLuiz Augusto von Dentz * and a static address has been configured, then use that
3510e411443cSLuiz Augusto von Dentz * address instead of the public BR/EDR address.
3511e411443cSLuiz Augusto von Dentz */
3512e411443cSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3513e411443cSLuiz Augusto von Dentz (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3514e411443cSLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3515e411443cSLuiz Augusto von Dentz if (bacmp(&hdev->static_addr, BDADDR_ANY))
3516e411443cSLuiz Augusto von Dentz return hci_set_random_addr_sync(hdev,
3517e411443cSLuiz Augusto von Dentz &hdev->static_addr);
3518e411443cSLuiz Augusto von Dentz }
3519e411443cSLuiz Augusto von Dentz
3520cf75ad8bSLuiz Augusto von Dentz return 0;
3521cf75ad8bSLuiz Augusto von Dentz }
3522cf75ad8bSLuiz Augusto von Dentz
3523d0b13706SLuiz Augusto von Dentz /**
3524d0b13706SLuiz Augusto von Dentz * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3525d0b13706SLuiz Augusto von Dentz * (BD_ADDR) for a HCI device from
3526d0b13706SLuiz Augusto von Dentz * a firmware node property.
3527d0b13706SLuiz Augusto von Dentz * @hdev: The HCI device
3528d0b13706SLuiz Augusto von Dentz *
3529d0b13706SLuiz Augusto von Dentz * Search the firmware node for 'local-bd-address'.
3530d0b13706SLuiz Augusto von Dentz *
3531d0b13706SLuiz Augusto von Dentz * All-zero BD addresses are rejected, because those could be properties
3532d0b13706SLuiz Augusto von Dentz * that exist in the firmware tables, but were not updated by the firmware. For
3533d0b13706SLuiz Augusto von Dentz * example, the DTS could define 'local-bd-address', with zero BD addresses.
hci_dev_get_bd_addr_from_property(struct hci_dev * hdev)3534d0b13706SLuiz Augusto von Dentz */
3535d0b13706SLuiz Augusto von Dentz static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3536d0b13706SLuiz Augusto von Dentz {
3537d0b13706SLuiz Augusto von Dentz struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3538d0b13706SLuiz Augusto von Dentz bdaddr_t ba;
3539d0b13706SLuiz Augusto von Dentz int ret;
3540d0b13706SLuiz Augusto von Dentz
3541d0b13706SLuiz Augusto von Dentz ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3542d0b13706SLuiz Augusto von Dentz (u8 *)&ba, sizeof(ba));
3543d0b13706SLuiz Augusto von Dentz if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3544d0b13706SLuiz Augusto von Dentz return;
3545d0b13706SLuiz Augusto von Dentz
354657e089d3SJohan Hovold if (test_bit(HCI_QUIRK_BDADDR_PROPERTY_BROKEN, &hdev->quirks))
354757e089d3SJohan Hovold baswap(&hdev->public_addr, &ba);
354857e089d3SJohan Hovold else
3549d0b13706SLuiz Augusto von Dentz bacpy(&hdev->public_addr, &ba);
3550d0b13706SLuiz Augusto von Dentz }
3551d0b13706SLuiz Augusto von Dentz
3552d0b13706SLuiz Augusto von Dentz struct hci_init_stage {
3553d0b13706SLuiz Augusto von Dentz int (*func)(struct hci_dev *hdev);
3554d0b13706SLuiz Augusto von Dentz };
3555d0b13706SLuiz Augusto von Dentz
hci_init_stage_sync(struct hci_dev * hdev,const struct hci_init_stage * stage)3556d0b13706SLuiz Augusto von Dentz /* Run init stage NULL terminated function table */
3557d0b13706SLuiz Augusto von Dentz static int hci_init_stage_sync(struct hci_dev *hdev,
3558d0b13706SLuiz Augusto von Dentz const struct hci_init_stage *stage)
3559d0b13706SLuiz Augusto von Dentz {
3560d0b13706SLuiz Augusto von Dentz size_t i;
3561d0b13706SLuiz Augusto von Dentz
3562d0b13706SLuiz Augusto von Dentz for (i = 0; stage[i].func; i++) {
3563d0b13706SLuiz Augusto von Dentz int err;
3564d0b13706SLuiz Augusto von Dentz
3565d0b13706SLuiz Augusto von Dentz err = stage[i].func(hdev);
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
hci_read_local_version_sync(struct hci_dev * hdev)3573d0b13706SLuiz Augusto von Dentz /* Read Local Version */
3574d0b13706SLuiz Augusto von Dentz static int hci_read_local_version_sync(struct hci_dev *hdev)
3575d0b13706SLuiz Augusto von Dentz {
3576d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3577d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3578d0b13706SLuiz Augusto von Dentz }
3579d0b13706SLuiz Augusto von Dentz
hci_read_bd_addr_sync(struct hci_dev * hdev)3580d0b13706SLuiz Augusto von Dentz /* Read BD Address */
3581d0b13706SLuiz Augusto von Dentz static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3582d0b13706SLuiz Augusto von Dentz {
3583d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3584d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3585d0b13706SLuiz Augusto von Dentz }
3586d0b13706SLuiz Augusto von Dentz
3587d0b13706SLuiz Augusto von Dentz #define HCI_INIT(_func) \
3588d0b13706SLuiz Augusto von Dentz { \
3589d0b13706SLuiz Augusto von Dentz .func = _func, \
3590d0b13706SLuiz Augusto von Dentz }
3591d0b13706SLuiz Augusto von Dentz
3592d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init0[] = {
3593d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_VERSION */
3594d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_version_sync),
3595d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_BD_ADDR */
3596d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_bd_addr_sync),
3597d0b13706SLuiz Augusto von Dentz {}
3598d0b13706SLuiz Augusto von Dentz };
hci_reset_sync(struct hci_dev * hdev)3599d0b13706SLuiz Augusto von Dentz
3600d0b13706SLuiz Augusto von Dentz int hci_reset_sync(struct hci_dev *hdev)
3601d0b13706SLuiz Augusto von Dentz {
3602d0b13706SLuiz Augusto von Dentz int err;
3603d0b13706SLuiz Augusto von Dentz
3604d0b13706SLuiz Augusto von Dentz set_bit(HCI_RESET, &hdev->flags);
3605d0b13706SLuiz Augusto von Dentz
3606d0b13706SLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3607d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
3608d0b13706SLuiz Augusto von Dentz if (err)
3609d0b13706SLuiz Augusto von Dentz return err;
3610d0b13706SLuiz Augusto von Dentz
3611d0b13706SLuiz Augusto von Dentz return 0;
3612d0b13706SLuiz Augusto von Dentz }
hci_init0_sync(struct hci_dev * hdev)3613d0b13706SLuiz Augusto von Dentz
3614d0b13706SLuiz Augusto von Dentz static int hci_init0_sync(struct hci_dev *hdev)
3615d0b13706SLuiz Augusto von Dentz {
3616d0b13706SLuiz Augusto von Dentz int err;
3617d0b13706SLuiz Augusto von Dentz
3618d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
3619d0b13706SLuiz Augusto von Dentz
3620d0b13706SLuiz Augusto von Dentz /* Reset */
3621d0b13706SLuiz Augusto von Dentz if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3622d0b13706SLuiz Augusto von Dentz err = hci_reset_sync(hdev);
3623d0b13706SLuiz Augusto von Dentz if (err)
3624d0b13706SLuiz Augusto von Dentz return err;
3625d0b13706SLuiz Augusto von Dentz }
3626d0b13706SLuiz Augusto von Dentz
3627d0b13706SLuiz Augusto von Dentz return hci_init_stage_sync(hdev, hci_init0);
3628d0b13706SLuiz Augusto von Dentz }
hci_unconf_init_sync(struct hci_dev * hdev)3629d0b13706SLuiz Augusto von Dentz
3630d0b13706SLuiz Augusto von Dentz static int hci_unconf_init_sync(struct hci_dev *hdev)
3631d0b13706SLuiz Augusto von Dentz {
3632d0b13706SLuiz Augusto von Dentz int err;
3633d0b13706SLuiz Augusto von Dentz
3634d0b13706SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3635d0b13706SLuiz Augusto von Dentz return 0;
3636d0b13706SLuiz Augusto von Dentz
3637d0b13706SLuiz Augusto von Dentz err = hci_init0_sync(hdev);
3638d0b13706SLuiz Augusto von Dentz if (err < 0)
3639d0b13706SLuiz Augusto von Dentz return err;
3640d0b13706SLuiz Augusto von Dentz
3641d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_SETUP))
3642d0b13706SLuiz Augusto von Dentz hci_debugfs_create_basic(hdev);
3643d0b13706SLuiz Augusto von Dentz
3644d0b13706SLuiz Augusto von Dentz return 0;
3645d0b13706SLuiz Augusto von Dentz }
3646d0b13706SLuiz Augusto von Dentz
hci_read_local_features_sync(struct hci_dev * hdev)3647d0b13706SLuiz Augusto von Dentz /* Read Local Supported Features. */
3648d0b13706SLuiz Augusto von Dentz static int hci_read_local_features_sync(struct hci_dev *hdev)
3649d0b13706SLuiz Augusto von Dentz {
3650d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3651d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3652d0b13706SLuiz Augusto von Dentz }
3653d0b13706SLuiz Augusto von Dentz
3654d0b13706SLuiz Augusto von Dentz /* BR Controller init stage 1 command sequence */
3655d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage br_init1[] = {
3656d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_FEATURES */
3657d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_features_sync),
3658d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_VERSION */
3659d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_version_sync),
3660d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_BD_ADDR */
3661d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_bd_addr_sync),
3662d0b13706SLuiz Augusto von Dentz {}
3663d0b13706SLuiz Augusto von Dentz };
3664d0b13706SLuiz Augusto von Dentz
hci_read_local_cmds_sync(struct hci_dev * hdev)3665d0b13706SLuiz Augusto von Dentz /* Read Local Commands */
3666d0b13706SLuiz Augusto von Dentz static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3667d0b13706SLuiz Augusto von Dentz {
3668d0b13706SLuiz Augusto von Dentz /* All Bluetooth 1.2 and later controllers should support the
3669d0b13706SLuiz Augusto von Dentz * HCI command for reading the local supported commands.
3670d0b13706SLuiz Augusto von Dentz *
3671d0b13706SLuiz Augusto von Dentz * Unfortunately some controllers indicate Bluetooth 1.2 support,
3672d0b13706SLuiz Augusto von Dentz * but do not have support for this command. If that is the case,
3673d0b13706SLuiz Augusto von Dentz * the driver can quirk the behavior and skip reading the local
3674d0b13706SLuiz Augusto von Dentz * supported commands.
3675d0b13706SLuiz Augusto von Dentz */
3676d0b13706SLuiz Augusto von Dentz if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3677d0b13706SLuiz Augusto von Dentz !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3678d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3679d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3680d0b13706SLuiz Augusto von Dentz
3681d0b13706SLuiz Augusto von Dentz return 0;
3682d0b13706SLuiz Augusto von Dentz }
hci_init1_sync(struct hci_dev * hdev)3683d0b13706SLuiz Augusto von Dentz
3684d0b13706SLuiz Augusto von Dentz static int hci_init1_sync(struct hci_dev *hdev)
3685d0b13706SLuiz Augusto von Dentz {
3686d0b13706SLuiz Augusto von Dentz int err;
3687d0b13706SLuiz Augusto von Dentz
3688d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
3689d0b13706SLuiz Augusto von Dentz
3690d0b13706SLuiz Augusto von Dentz /* Reset */
3691d0b13706SLuiz Augusto von Dentz if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3692d0b13706SLuiz Augusto von Dentz err = hci_reset_sync(hdev);
3693d0b13706SLuiz Augusto von Dentz if (err)
3694d0b13706SLuiz Augusto von Dentz return err;
3695d0b13706SLuiz Augusto von Dentz }
3696d0b13706SLuiz Augusto von Dentz
3697d0b13706SLuiz Augusto von Dentz return hci_init_stage_sync(hdev, br_init1);
3698d0b13706SLuiz Augusto von Dentz }
3699d0b13706SLuiz Augusto von Dentz
hci_read_buffer_size_sync(struct hci_dev * hdev)3700d0b13706SLuiz Augusto von Dentz /* Read Buffer Size (ACL mtu, max pkt, etc.) */
3701d0b13706SLuiz Augusto von Dentz static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3702d0b13706SLuiz Augusto von Dentz {
3703d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3704d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3705d0b13706SLuiz Augusto von Dentz }
3706d0b13706SLuiz Augusto von Dentz
hci_read_dev_class_sync(struct hci_dev * hdev)3707d0b13706SLuiz Augusto von Dentz /* Read Class of Device */
3708d0b13706SLuiz Augusto von Dentz static int hci_read_dev_class_sync(struct hci_dev *hdev)
3709d0b13706SLuiz Augusto von Dentz {
3710d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3711d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3712d0b13706SLuiz Augusto von Dentz }
3713d0b13706SLuiz Augusto von Dentz
hci_read_local_name_sync(struct hci_dev * hdev)3714d0b13706SLuiz Augusto von Dentz /* Read Local Name */
3715d0b13706SLuiz Augusto von Dentz static int hci_read_local_name_sync(struct hci_dev *hdev)
3716d0b13706SLuiz Augusto von Dentz {
3717d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3718d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3719d0b13706SLuiz Augusto von Dentz }
3720d0b13706SLuiz Augusto von Dentz
hci_read_voice_setting_sync(struct hci_dev * hdev)3721d0b13706SLuiz Augusto von Dentz /* Read Voice Setting */
3722d0b13706SLuiz Augusto von Dentz static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3723d0b13706SLuiz Augusto von Dentz {
3724d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3725d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3726d0b13706SLuiz Augusto von Dentz }
3727d0b13706SLuiz Augusto von Dentz
hci_read_num_supported_iac_sync(struct hci_dev * hdev)3728d0b13706SLuiz Augusto von Dentz /* Read Number of Supported IAC */
3729d0b13706SLuiz Augusto von Dentz static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3730d0b13706SLuiz Augusto von Dentz {
3731d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3732d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3733d0b13706SLuiz Augusto von Dentz }
3734d0b13706SLuiz Augusto von Dentz
hci_read_current_iac_lap_sync(struct hci_dev * hdev)3735d0b13706SLuiz Augusto von Dentz /* Read Current IAC LAP */
3736d0b13706SLuiz Augusto von Dentz static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3737d0b13706SLuiz Augusto von Dentz {
3738d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3739d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3740d0b13706SLuiz Augusto von Dentz }
hci_set_event_filter_sync(struct hci_dev * hdev,u8 flt_type,u8 cond_type,bdaddr_t * bdaddr,u8 auto_accept)3741d0b13706SLuiz Augusto von Dentz
3742d0b13706SLuiz Augusto von Dentz static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3743d0b13706SLuiz Augusto von Dentz u8 cond_type, bdaddr_t *bdaddr,
3744d0b13706SLuiz Augusto von Dentz u8 auto_accept)
3745d0b13706SLuiz Augusto von Dentz {
3746d0b13706SLuiz Augusto von Dentz struct hci_cp_set_event_filter cp;
3747d0b13706SLuiz Augusto von Dentz
3748d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3749d0b13706SLuiz Augusto von Dentz return 0;
3750d0b13706SLuiz Augusto von Dentz
37510eaecfb2SIsmael Ferreras Morezuelas if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
37520eaecfb2SIsmael Ferreras Morezuelas return 0;
37530eaecfb2SIsmael Ferreras Morezuelas
3754d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
3755d0b13706SLuiz Augusto von Dentz cp.flt_type = flt_type;
3756d0b13706SLuiz Augusto von Dentz
3757d0b13706SLuiz Augusto von Dentz if (flt_type != HCI_FLT_CLEAR_ALL) {
3758d0b13706SLuiz Augusto von Dentz cp.cond_type = cond_type;
3759d0b13706SLuiz Augusto von Dentz bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3760d0b13706SLuiz Augusto von Dentz cp.addr_conn_flt.auto_accept = auto_accept;
3761d0b13706SLuiz Augusto von Dentz }
3762d0b13706SLuiz Augusto von Dentz
3763d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3764d0b13706SLuiz Augusto von Dentz flt_type == HCI_FLT_CLEAR_ALL ?
3765d0b13706SLuiz Augusto von Dentz sizeof(cp.flt_type) : sizeof(cp), &cp,
3766d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
3767d0b13706SLuiz Augusto von Dentz }
hci_clear_event_filter_sync(struct hci_dev * hdev)3768d0b13706SLuiz Augusto von Dentz
3769d0b13706SLuiz Augusto von Dentz static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3770d0b13706SLuiz Augusto von Dentz {
3771d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3772d0b13706SLuiz Augusto von Dentz return 0;
3773d0b13706SLuiz Augusto von Dentz
37740eaecfb2SIsmael Ferreras Morezuelas /* In theory the state machine should not reach here unless
37750eaecfb2SIsmael Ferreras Morezuelas * a hci_set_event_filter_sync() call succeeds, but we do
37760eaecfb2SIsmael Ferreras Morezuelas * the check both for parity and as a future reminder.
37770eaecfb2SIsmael Ferreras Morezuelas */
37780eaecfb2SIsmael Ferreras Morezuelas if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
37790eaecfb2SIsmael Ferreras Morezuelas return 0;
37800eaecfb2SIsmael Ferreras Morezuelas
3781d0b13706SLuiz Augusto von Dentz return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3782d0b13706SLuiz Augusto von Dentz BDADDR_ANY, 0x00);
3783d0b13706SLuiz Augusto von Dentz }
3784d0b13706SLuiz Augusto von Dentz
hci_write_ca_timeout_sync(struct hci_dev * hdev)3785d0b13706SLuiz Augusto von Dentz /* Connection accept timeout ~20 secs */
3786d0b13706SLuiz Augusto von Dentz static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3787d0b13706SLuiz Augusto von Dentz {
3788d0b13706SLuiz Augusto von Dentz __le16 param = cpu_to_le16(0x7d00);
3789d0b13706SLuiz Augusto von Dentz
3790d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3791d0b13706SLuiz Augusto von Dentz sizeof(param), ¶m, HCI_CMD_TIMEOUT);
3792d0b13706SLuiz Augusto von Dentz }
3793d0b13706SLuiz Augusto von Dentz
3794d0b13706SLuiz Augusto von Dentz /* BR Controller init stage 2 command sequence */
3795d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage br_init2[] = {
3796d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_BUFFER_SIZE */
3797d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_buffer_size_sync),
3798d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_CLASS_OF_DEV */
3799d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_dev_class_sync),
3800d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_NAME */
3801d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_name_sync),
3802d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_VOICE_SETTING */
3803d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_voice_setting_sync),
3804d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_NUM_SUPPORTED_IAC */
3805d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_num_supported_iac_sync),
3806d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_CURRENT_IAC_LAP */
3807d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_current_iac_lap_sync),
3808d0b13706SLuiz Augusto von Dentz /* HCI_OP_SET_EVENT_FLT */
3809d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_clear_event_filter_sync),
3810d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_CA_TIMEOUT */
3811d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_ca_timeout_sync),
3812d0b13706SLuiz Augusto von Dentz {}
3813d0b13706SLuiz Augusto von Dentz };
hci_write_ssp_mode_1_sync(struct hci_dev * hdev)3814d0b13706SLuiz Augusto von Dentz
3815d0b13706SLuiz Augusto von Dentz static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3816d0b13706SLuiz Augusto von Dentz {
3817d0b13706SLuiz Augusto von Dentz u8 mode = 0x01;
3818d0b13706SLuiz Augusto von Dentz
3819d0b13706SLuiz Augusto von Dentz if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3820d0b13706SLuiz Augusto von Dentz return 0;
3821d0b13706SLuiz Augusto von Dentz
3822d0b13706SLuiz Augusto von Dentz /* When SSP is available, then the host features page
3823d0b13706SLuiz Augusto von Dentz * should also be available as well. However some
3824d0b13706SLuiz Augusto von Dentz * controllers list the max_page as 0 as long as SSP
3825d0b13706SLuiz Augusto von Dentz * has not been enabled. To achieve proper debugging
3826d0b13706SLuiz Augusto von Dentz * output, force the minimum max_page to 1 at least.
3827d0b13706SLuiz Augusto von Dentz */
3828d0b13706SLuiz Augusto von Dentz hdev->max_page = 0x01;
3829d0b13706SLuiz Augusto von Dentz
3830d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3831d0b13706SLuiz Augusto von Dentz sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3832d0b13706SLuiz Augusto von Dentz }
hci_write_eir_sync(struct hci_dev * hdev)3833d0b13706SLuiz Augusto von Dentz
3834d0b13706SLuiz Augusto von Dentz static int hci_write_eir_sync(struct hci_dev *hdev)
3835d0b13706SLuiz Augusto von Dentz {
3836d0b13706SLuiz Augusto von Dentz struct hci_cp_write_eir cp;
3837d0b13706SLuiz Augusto von Dentz
3838d0b13706SLuiz Augusto von Dentz if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3839d0b13706SLuiz Augusto von Dentz return 0;
3840d0b13706SLuiz Augusto von Dentz
3841d0b13706SLuiz Augusto von Dentz memset(hdev->eir, 0, sizeof(hdev->eir));
3842d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
3843d0b13706SLuiz Augusto von Dentz
3844d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3845d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
3846d0b13706SLuiz Augusto von Dentz }
hci_write_inquiry_mode_sync(struct hci_dev * hdev)3847d0b13706SLuiz Augusto von Dentz
3848d0b13706SLuiz Augusto von Dentz static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3849d0b13706SLuiz Augusto von Dentz {
3850d0b13706SLuiz Augusto von Dentz u8 mode;
3851d0b13706SLuiz Augusto von Dentz
3852d0b13706SLuiz Augusto von Dentz if (!lmp_inq_rssi_capable(hdev) &&
3853d0b13706SLuiz Augusto von Dentz !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3854d0b13706SLuiz Augusto von Dentz return 0;
3855d0b13706SLuiz Augusto von Dentz
3856d0b13706SLuiz Augusto von Dentz /* If Extended Inquiry Result events are supported, then
3857d0b13706SLuiz Augusto von Dentz * they are clearly preferred over Inquiry Result with RSSI
3858d0b13706SLuiz Augusto von Dentz * events.
3859d0b13706SLuiz Augusto von Dentz */
3860d0b13706SLuiz Augusto von Dentz mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3861d0b13706SLuiz Augusto von Dentz
3862d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3863d0b13706SLuiz Augusto von Dentz sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3864d0b13706SLuiz Augusto von Dentz }
hci_read_inq_rsp_tx_power_sync(struct hci_dev * hdev)3865d0b13706SLuiz Augusto von Dentz
3866d0b13706SLuiz Augusto von Dentz static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3867d0b13706SLuiz Augusto von Dentz {
3868d0b13706SLuiz Augusto von Dentz if (!lmp_inq_tx_pwr_capable(hdev))
3869d0b13706SLuiz Augusto von Dentz return 0;
3870d0b13706SLuiz Augusto von Dentz
3871d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3872d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3873d0b13706SLuiz Augusto von Dentz }
hci_read_local_ext_features_sync(struct hci_dev * hdev,u8 page)3874d0b13706SLuiz Augusto von Dentz
3875d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3876d0b13706SLuiz Augusto von Dentz {
3877d0b13706SLuiz Augusto von Dentz struct hci_cp_read_local_ext_features cp;
3878d0b13706SLuiz Augusto von Dentz
3879d0b13706SLuiz Augusto von Dentz if (!lmp_ext_feat_capable(hdev))
3880d0b13706SLuiz Augusto von Dentz return 0;
3881d0b13706SLuiz Augusto von Dentz
3882d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
3883d0b13706SLuiz Augusto von Dentz cp.page = page;
3884d0b13706SLuiz Augusto von Dentz
3885d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3886d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3887d0b13706SLuiz Augusto von Dentz }
hci_read_local_ext_features_1_sync(struct hci_dev * hdev)3888d0b13706SLuiz Augusto von Dentz
3889d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3890d0b13706SLuiz Augusto von Dentz {
3891d0b13706SLuiz Augusto von Dentz return hci_read_local_ext_features_sync(hdev, 0x01);
3892d0b13706SLuiz Augusto von Dentz }
3893d0b13706SLuiz Augusto von Dentz
3894d0b13706SLuiz Augusto von Dentz /* HCI Controller init stage 2 command sequence */
3895d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init2[] = {
3896d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_COMMANDS */
3897d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_cmds_sync),
3898d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_SSP_MODE */
3899d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_ssp_mode_1_sync),
3900d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_EIR */
3901d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_eir_sync),
3902d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_INQUIRY_MODE */
3903d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_inquiry_mode_sync),
3904d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_INQ_RSP_TX_POWER */
3905d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3906d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3907d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_ext_features_1_sync),
3908d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_AUTH_ENABLE */
3909d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_auth_enable_sync),
3910d0b13706SLuiz Augusto von Dentz {}
3911d0b13706SLuiz Augusto von Dentz };
3912d0b13706SLuiz Augusto von Dentz
hci_le_read_buffer_size_sync(struct hci_dev * hdev)3913d0b13706SLuiz Augusto von Dentz /* Read LE Buffer Size */
3914d0b13706SLuiz Augusto von Dentz static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3915d0b13706SLuiz Augusto von Dentz {
3916c1631dbcSLuiz Augusto von Dentz /* Use Read LE Buffer Size V2 if supported */
39173a4d29b6SLuiz Augusto von Dentz if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3918c1631dbcSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev,
3919c1631dbcSLuiz Augusto von Dentz HCI_OP_LE_READ_BUFFER_SIZE_V2,
3920c1631dbcSLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3921c1631dbcSLuiz Augusto von Dentz
3922d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3923d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3924d0b13706SLuiz Augusto von Dentz }
3925d0b13706SLuiz Augusto von Dentz
hci_le_read_local_features_sync(struct hci_dev * hdev)3926d0b13706SLuiz Augusto von Dentz /* Read LE Local Supported Features */
3927d0b13706SLuiz Augusto von Dentz static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3928d0b13706SLuiz Augusto von Dentz {
3929d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3930d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3931d0b13706SLuiz Augusto von Dentz }
3932d0b13706SLuiz Augusto von Dentz
hci_le_read_supported_states_sync(struct hci_dev * hdev)3933d0b13706SLuiz Augusto von Dentz /* Read LE Supported States */
3934d0b13706SLuiz Augusto von Dentz static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3935d0b13706SLuiz Augusto von Dentz {
3936d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3937d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
3938d0b13706SLuiz Augusto von Dentz }
3939d0b13706SLuiz Augusto von Dentz
3940d0b13706SLuiz Augusto von Dentz /* LE Controller init stage 2 command sequence */
3941d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init2[] = {
3942d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_LOCAL_FEATURES */
3943d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_local_features_sync),
39443a4d29b6SLuiz Augusto von Dentz /* HCI_OP_LE_READ_BUFFER_SIZE */
39453a4d29b6SLuiz Augusto von Dentz HCI_INIT(hci_le_read_buffer_size_sync),
3946d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_SUPPORTED_STATES */
3947d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_supported_states_sync),
3948d0b13706SLuiz Augusto von Dentz {}
3949d0b13706SLuiz Augusto von Dentz };
hci_init2_sync(struct hci_dev * hdev)3950d0b13706SLuiz Augusto von Dentz
3951d0b13706SLuiz Augusto von Dentz static int hci_init2_sync(struct hci_dev *hdev)
3952d0b13706SLuiz Augusto von Dentz {
3953d0b13706SLuiz Augusto von Dentz int err;
3954d0b13706SLuiz Augusto von Dentz
3955d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
3956d0b13706SLuiz Augusto von Dentz
395726afbd82SLuiz Augusto von Dentz err = hci_init_stage_sync(hdev, hci_init2);
395826afbd82SLuiz Augusto von Dentz if (err)
395926afbd82SLuiz Augusto von Dentz return err;
396026afbd82SLuiz Augusto von Dentz
3961d0b13706SLuiz Augusto von Dentz if (lmp_bredr_capable(hdev)) {
3962d0b13706SLuiz Augusto von Dentz err = hci_init_stage_sync(hdev, br_init2);
3963d0b13706SLuiz Augusto von Dentz if (err)
3964d0b13706SLuiz Augusto von Dentz return err;
3965d0b13706SLuiz Augusto von Dentz } else {
3966d0b13706SLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3967d0b13706SLuiz Augusto von Dentz }
3968d0b13706SLuiz Augusto von Dentz
3969d0b13706SLuiz Augusto von Dentz if (lmp_le_capable(hdev)) {
3970d0b13706SLuiz Augusto von Dentz err = hci_init_stage_sync(hdev, le_init2);
3971d0b13706SLuiz Augusto von Dentz if (err)
3972d0b13706SLuiz Augusto von Dentz return err;
3973d0b13706SLuiz Augusto von Dentz /* LE-only controllers have LE implicitly enabled */
3974d0b13706SLuiz Augusto von Dentz if (!lmp_bredr_capable(hdev))
3975d0b13706SLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3976d0b13706SLuiz Augusto von Dentz }
3977d0b13706SLuiz Augusto von Dentz
397826afbd82SLuiz Augusto von Dentz return 0;
3979d0b13706SLuiz Augusto von Dentz }
hci_set_event_mask_sync(struct hci_dev * hdev)3980d0b13706SLuiz Augusto von Dentz
3981d0b13706SLuiz Augusto von Dentz static int hci_set_event_mask_sync(struct hci_dev *hdev)
3982d0b13706SLuiz Augusto von Dentz {
3983d0b13706SLuiz Augusto von Dentz /* The second byte is 0xff instead of 0x9f (two reserved bits
3984d0b13706SLuiz Augusto von Dentz * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3985d0b13706SLuiz Augusto von Dentz * command otherwise.
3986d0b13706SLuiz Augusto von Dentz */
3987d0b13706SLuiz Augusto von Dentz u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3988d0b13706SLuiz Augusto von Dentz
3989d0b13706SLuiz Augusto von Dentz /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3990d0b13706SLuiz Augusto von Dentz * any event mask for pre 1.2 devices.
3991d0b13706SLuiz Augusto von Dentz */
3992d0b13706SLuiz Augusto von Dentz if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3993d0b13706SLuiz Augusto von Dentz return 0;
3994d0b13706SLuiz Augusto von Dentz
3995d0b13706SLuiz Augusto von Dentz if (lmp_bredr_capable(hdev)) {
3996d0b13706SLuiz Augusto von Dentz events[4] |= 0x01; /* Flow Specification Complete */
3997182ee45dSLuiz Augusto von Dentz
3998e266b7b2Sclancy shang /* Don't set Disconnect Complete and mode change when
3999e266b7b2Sclancy shang * suspended as that would wakeup the host when disconnecting
4000e266b7b2Sclancy shang * due to suspend.
4001182ee45dSLuiz Augusto von Dentz */
4002e266b7b2Sclancy shang if (hdev->suspended) {
4003182ee45dSLuiz Augusto von Dentz events[0] &= 0xef;
4004e266b7b2Sclancy shang events[2] &= 0xf7;
4005e266b7b2Sclancy shang }
4006d0b13706SLuiz Augusto von Dentz } else {
4007d0b13706SLuiz Augusto von Dentz /* Use a different default for LE-only devices */
4008d0b13706SLuiz Augusto von Dentz memset(events, 0, sizeof(events));
4009d0b13706SLuiz Augusto von Dentz events[1] |= 0x20; /* Command Complete */
4010d0b13706SLuiz Augusto von Dentz events[1] |= 0x40; /* Command Status */
4011d0b13706SLuiz Augusto von Dentz events[1] |= 0x80; /* Hardware Error */
4012d0b13706SLuiz Augusto von Dentz
4013d0b13706SLuiz Augusto von Dentz /* If the controller supports the Disconnect command, enable
4014d0b13706SLuiz Augusto von Dentz * the corresponding event. In addition enable packet flow
4015d0b13706SLuiz Augusto von Dentz * control related events.
4016d0b13706SLuiz Augusto von Dentz */
4017d0b13706SLuiz Augusto von Dentz if (hdev->commands[0] & 0x20) {
4018182ee45dSLuiz Augusto von Dentz /* Don't set Disconnect Complete when suspended as that
4019182ee45dSLuiz Augusto von Dentz * would wakeup the host when disconnecting due to
4020182ee45dSLuiz Augusto von Dentz * suspend.
4021182ee45dSLuiz Augusto von Dentz */
4022182ee45dSLuiz Augusto von Dentz if (!hdev->suspended)
4023d0b13706SLuiz Augusto von Dentz events[0] |= 0x10; /* Disconnection Complete */
4024d0b13706SLuiz Augusto von Dentz events[2] |= 0x04; /* Number of Completed Packets */
4025d0b13706SLuiz Augusto von Dentz events[3] |= 0x02; /* Data Buffer Overflow */
4026d0b13706SLuiz Augusto von Dentz }
4027d0b13706SLuiz Augusto von Dentz
4028d0b13706SLuiz Augusto von Dentz /* If the controller supports the Read Remote Version
4029d0b13706SLuiz Augusto von Dentz * Information command, enable the corresponding event.
4030d0b13706SLuiz Augusto von Dentz */
4031d0b13706SLuiz Augusto von Dentz if (hdev->commands[2] & 0x80)
4032d0b13706SLuiz Augusto von Dentz events[1] |= 0x08; /* Read Remote Version Information
4033d0b13706SLuiz Augusto von Dentz * Complete
4034d0b13706SLuiz Augusto von Dentz */
4035d0b13706SLuiz Augusto von Dentz
4036d0b13706SLuiz Augusto von Dentz if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
4037d0b13706SLuiz Augusto von Dentz events[0] |= 0x80; /* Encryption Change */
4038d0b13706SLuiz Augusto von Dentz events[5] |= 0x80; /* Encryption Key Refresh Complete */
4039d0b13706SLuiz Augusto von Dentz }
4040d0b13706SLuiz Augusto von Dentz }
4041d0b13706SLuiz Augusto von Dentz
4042d0b13706SLuiz Augusto von Dentz if (lmp_inq_rssi_capable(hdev) ||
4043d0b13706SLuiz Augusto von Dentz test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
4044d0b13706SLuiz Augusto von Dentz events[4] |= 0x02; /* Inquiry Result with RSSI */
4045d0b13706SLuiz Augusto von Dentz
4046d0b13706SLuiz Augusto von Dentz if (lmp_ext_feat_capable(hdev))
4047d0b13706SLuiz Augusto von Dentz events[4] |= 0x04; /* Read Remote Extended Features Complete */
4048d0b13706SLuiz Augusto von Dentz
4049d0b13706SLuiz Augusto von Dentz if (lmp_esco_capable(hdev)) {
4050d0b13706SLuiz Augusto von Dentz events[5] |= 0x08; /* Synchronous Connection Complete */
4051d0b13706SLuiz Augusto von Dentz events[5] |= 0x10; /* Synchronous Connection Changed */
4052d0b13706SLuiz Augusto von Dentz }
4053d0b13706SLuiz Augusto von Dentz
4054d0b13706SLuiz Augusto von Dentz if (lmp_sniffsubr_capable(hdev))
4055d0b13706SLuiz Augusto von Dentz events[5] |= 0x20; /* Sniff Subrating */
4056d0b13706SLuiz Augusto von Dentz
4057d0b13706SLuiz Augusto von Dentz if (lmp_pause_enc_capable(hdev))
4058d0b13706SLuiz Augusto von Dentz events[5] |= 0x80; /* Encryption Key Refresh Complete */
4059d0b13706SLuiz Augusto von Dentz
4060d0b13706SLuiz Augusto von Dentz if (lmp_ext_inq_capable(hdev))
4061d0b13706SLuiz Augusto von Dentz events[5] |= 0x40; /* Extended Inquiry Result */
4062d0b13706SLuiz Augusto von Dentz
4063d0b13706SLuiz Augusto von Dentz if (lmp_no_flush_capable(hdev))
4064d0b13706SLuiz Augusto von Dentz events[7] |= 0x01; /* Enhanced Flush Complete */
4065d0b13706SLuiz Augusto von Dentz
4066d0b13706SLuiz Augusto von Dentz if (lmp_lsto_capable(hdev))
4067d0b13706SLuiz Augusto von Dentz events[6] |= 0x80; /* Link Supervision Timeout Changed */
4068d0b13706SLuiz Augusto von Dentz
4069d0b13706SLuiz Augusto von Dentz if (lmp_ssp_capable(hdev)) {
4070d0b13706SLuiz Augusto von Dentz events[6] |= 0x01; /* IO Capability Request */
4071d0b13706SLuiz Augusto von Dentz events[6] |= 0x02; /* IO Capability Response */
4072d0b13706SLuiz Augusto von Dentz events[6] |= 0x04; /* User Confirmation Request */
4073d0b13706SLuiz Augusto von Dentz events[6] |= 0x08; /* User Passkey Request */
4074d0b13706SLuiz Augusto von Dentz events[6] |= 0x10; /* Remote OOB Data Request */
4075d0b13706SLuiz Augusto von Dentz events[6] |= 0x20; /* Simple Pairing Complete */
4076d0b13706SLuiz Augusto von Dentz events[7] |= 0x04; /* User Passkey Notification */
4077d0b13706SLuiz Augusto von Dentz events[7] |= 0x08; /* Keypress Notification */
4078d0b13706SLuiz Augusto von Dentz events[7] |= 0x10; /* Remote Host Supported
4079d0b13706SLuiz Augusto von Dentz * Features Notification
4080d0b13706SLuiz Augusto von Dentz */
4081d0b13706SLuiz Augusto von Dentz }
4082d0b13706SLuiz Augusto von Dentz
4083d0b13706SLuiz Augusto von Dentz if (lmp_le_capable(hdev))
4084d0b13706SLuiz Augusto von Dentz events[7] |= 0x20; /* LE Meta-Event */
4085d0b13706SLuiz Augusto von Dentz
4086d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
4087d0b13706SLuiz Augusto von Dentz sizeof(events), events, HCI_CMD_TIMEOUT);
4088d0b13706SLuiz Augusto von Dentz }
hci_read_stored_link_key_sync(struct hci_dev * hdev)4089d0b13706SLuiz Augusto von Dentz
4090d0b13706SLuiz Augusto von Dentz static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
4091d0b13706SLuiz Augusto von Dentz {
4092d0b13706SLuiz Augusto von Dentz struct hci_cp_read_stored_link_key cp;
4093d0b13706SLuiz Augusto von Dentz
4094d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[6] & 0x20) ||
4095d0b13706SLuiz Augusto von Dentz test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4096d0b13706SLuiz Augusto von Dentz return 0;
4097d0b13706SLuiz Augusto von Dentz
4098d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
4099d0b13706SLuiz Augusto von Dentz bacpy(&cp.bdaddr, BDADDR_ANY);
4100d0b13706SLuiz Augusto von Dentz cp.read_all = 0x01;
4101d0b13706SLuiz Augusto von Dentz
4102d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
4103d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4104d0b13706SLuiz Augusto von Dentz }
hci_setup_link_policy_sync(struct hci_dev * hdev)4105d0b13706SLuiz Augusto von Dentz
4106d0b13706SLuiz Augusto von Dentz static int hci_setup_link_policy_sync(struct hci_dev *hdev)
4107d0b13706SLuiz Augusto von Dentz {
4108d0b13706SLuiz Augusto von Dentz struct hci_cp_write_def_link_policy cp;
4109d0b13706SLuiz Augusto von Dentz u16 link_policy = 0;
4110d0b13706SLuiz Augusto von Dentz
4111d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[5] & 0x10))
4112d0b13706SLuiz Augusto von Dentz return 0;
4113d0b13706SLuiz Augusto von Dentz
4114d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
4115d0b13706SLuiz Augusto von Dentz
4116d0b13706SLuiz Augusto von Dentz if (lmp_rswitch_capable(hdev))
4117d0b13706SLuiz Augusto von Dentz link_policy |= HCI_LP_RSWITCH;
4118d0b13706SLuiz Augusto von Dentz if (lmp_hold_capable(hdev))
4119d0b13706SLuiz Augusto von Dentz link_policy |= HCI_LP_HOLD;
4120d0b13706SLuiz Augusto von Dentz if (lmp_sniff_capable(hdev))
4121d0b13706SLuiz Augusto von Dentz link_policy |= HCI_LP_SNIFF;
4122d0b13706SLuiz Augusto von Dentz if (lmp_park_capable(hdev))
4123d0b13706SLuiz Augusto von Dentz link_policy |= HCI_LP_PARK;
4124d0b13706SLuiz Augusto von Dentz
4125d0b13706SLuiz Augusto von Dentz cp.policy = cpu_to_le16(link_policy);
4126d0b13706SLuiz Augusto von Dentz
4127d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
4128d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4129d0b13706SLuiz Augusto von Dentz }
hci_read_page_scan_activity_sync(struct hci_dev * hdev)4130d0b13706SLuiz Augusto von Dentz
4131d0b13706SLuiz Augusto von Dentz static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
4132d0b13706SLuiz Augusto von Dentz {
4133d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[8] & 0x01))
4134d0b13706SLuiz Augusto von Dentz return 0;
4135d0b13706SLuiz Augusto von Dentz
4136d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
4137d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4138d0b13706SLuiz Augusto von Dentz }
hci_read_def_err_data_reporting_sync(struct hci_dev * hdev)4139d0b13706SLuiz Augusto von Dentz
4140d0b13706SLuiz Augusto von Dentz static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
4141d0b13706SLuiz Augusto von Dentz {
4142d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[18] & 0x04) ||
414342d7731eSIsmael Ferreras Morezuelas !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
414442d7731eSIsmael Ferreras Morezuelas test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4145d0b13706SLuiz Augusto von Dentz return 0;
4146d0b13706SLuiz Augusto von Dentz
4147d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
4148d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4149d0b13706SLuiz Augusto von Dentz }
hci_read_page_scan_type_sync(struct hci_dev * hdev)4150d0b13706SLuiz Augusto von Dentz
4151d0b13706SLuiz Augusto von Dentz static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
4152d0b13706SLuiz Augusto von Dentz {
4153d0b13706SLuiz Augusto von Dentz /* Some older Broadcom based Bluetooth 1.2 controllers do not
4154d0b13706SLuiz Augusto von Dentz * support the Read Page Scan Type command. Check support for
4155d0b13706SLuiz Augusto von Dentz * this command in the bit mask of supported commands.
4156d0b13706SLuiz Augusto von Dentz */
4157d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[13] & 0x01))
4158d0b13706SLuiz Augusto von Dentz return 0;
4159d0b13706SLuiz Augusto von Dentz
4160d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
4161d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4162d0b13706SLuiz Augusto von Dentz }
4163d0b13706SLuiz Augusto von Dentz
hci_read_local_ext_features_all_sync(struct hci_dev * hdev)4164d0b13706SLuiz Augusto von Dentz /* Read features beyond page 1 if available */
4165d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
4166d0b13706SLuiz Augusto von Dentz {
4167d0b13706SLuiz Augusto von Dentz u8 page;
4168d0b13706SLuiz Augusto von Dentz int err;
4169d0b13706SLuiz Augusto von Dentz
4170d0b13706SLuiz Augusto von Dentz if (!lmp_ext_feat_capable(hdev))
4171d0b13706SLuiz Augusto von Dentz return 0;
4172d0b13706SLuiz Augusto von Dentz
4173d0b13706SLuiz Augusto von Dentz for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
4174d0b13706SLuiz Augusto von Dentz page++) {
4175d0b13706SLuiz Augusto von Dentz err = hci_read_local_ext_features_sync(hdev, page);
4176d0b13706SLuiz Augusto von Dentz if (err)
4177d0b13706SLuiz Augusto von Dentz return err;
4178d0b13706SLuiz Augusto von Dentz }
4179d0b13706SLuiz Augusto von Dentz
4180d0b13706SLuiz Augusto von Dentz return 0;
4181d0b13706SLuiz Augusto von Dentz }
4182d0b13706SLuiz Augusto von Dentz
4183d0b13706SLuiz Augusto von Dentz /* HCI Controller init stage 3 command sequence */
4184d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init3[] = {
4185d0b13706SLuiz Augusto von Dentz /* HCI_OP_SET_EVENT_MASK */
4186d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_set_event_mask_sync),
4187d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_STORED_LINK_KEY */
4188d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_stored_link_key_sync),
4189d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_DEF_LINK_POLICY */
4190d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_setup_link_policy_sync),
4191d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
4192d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_page_scan_activity_sync),
4193d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
4194d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_def_err_data_reporting_sync),
4195d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_PAGE_SCAN_TYPE */
4196d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_page_scan_type_sync),
4197d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_EXT_FEATURES */
4198d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_ext_features_all_sync),
4199d0b13706SLuiz Augusto von Dentz {}
4200d0b13706SLuiz Augusto von Dentz };
hci_le_set_event_mask_sync(struct hci_dev * hdev)4201d0b13706SLuiz Augusto von Dentz
4202d0b13706SLuiz Augusto von Dentz static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4203d0b13706SLuiz Augusto von Dentz {
4204d0b13706SLuiz Augusto von Dentz u8 events[8];
4205d0b13706SLuiz Augusto von Dentz
4206d0b13706SLuiz Augusto von Dentz if (!lmp_le_capable(hdev))
4207d0b13706SLuiz Augusto von Dentz return 0;
4208d0b13706SLuiz Augusto von Dentz
4209d0b13706SLuiz Augusto von Dentz memset(events, 0, sizeof(events));
4210d0b13706SLuiz Augusto von Dentz
4211d0b13706SLuiz Augusto von Dentz if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4212d0b13706SLuiz Augusto von Dentz events[0] |= 0x10; /* LE Long Term Key Request */
4213d0b13706SLuiz Augusto von Dentz
4214d0b13706SLuiz Augusto von Dentz /* If controller supports the Connection Parameters Request
4215d0b13706SLuiz Augusto von Dentz * Link Layer Procedure, enable the corresponding event.
4216d0b13706SLuiz Augusto von Dentz */
4217d0b13706SLuiz Augusto von Dentz if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4218d0b13706SLuiz Augusto von Dentz /* LE Remote Connection Parameter Request */
4219d0b13706SLuiz Augusto von Dentz events[0] |= 0x20;
4220d0b13706SLuiz Augusto von Dentz
4221d0b13706SLuiz Augusto von Dentz /* If the controller supports the Data Length Extension
4222d0b13706SLuiz Augusto von Dentz * feature, enable the corresponding event.
4223d0b13706SLuiz Augusto von Dentz */
4224d0b13706SLuiz Augusto von Dentz if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4225d0b13706SLuiz Augusto von Dentz events[0] |= 0x40; /* LE Data Length Change */
4226d0b13706SLuiz Augusto von Dentz
4227a56a1138SLuiz Augusto von Dentz /* If the controller supports LL Privacy feature or LE Extended Adv,
4228a56a1138SLuiz Augusto von Dentz * enable the corresponding event.
4229d0b13706SLuiz Augusto von Dentz */
4230a56a1138SLuiz Augusto von Dentz if (use_enhanced_conn_complete(hdev))
4231d0b13706SLuiz Augusto von Dentz events[1] |= 0x02; /* LE Enhanced Connection Complete */
4232d0b13706SLuiz Augusto von Dentz
4233d0b13706SLuiz Augusto von Dentz /* If the controller supports Extended Scanner Filter
4234d0b13706SLuiz Augusto von Dentz * Policies, enable the corresponding event.
4235d0b13706SLuiz Augusto von Dentz */
4236d0b13706SLuiz Augusto von Dentz if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4237d0b13706SLuiz Augusto von Dentz events[1] |= 0x04; /* LE Direct Advertising Report */
4238d0b13706SLuiz Augusto von Dentz
4239d0b13706SLuiz Augusto von Dentz /* If the controller supports Channel Selection Algorithm #2
4240d0b13706SLuiz Augusto von Dentz * feature, enable the corresponding event.
4241d0b13706SLuiz Augusto von Dentz */
4242d0b13706SLuiz Augusto von Dentz if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4243d0b13706SLuiz Augusto von Dentz events[2] |= 0x08; /* LE Channel Selection Algorithm */
4244d0b13706SLuiz Augusto von Dentz
4245d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Set Scan Enable command,
4246d0b13706SLuiz Augusto von Dentz * enable the corresponding advertising report event.
4247d0b13706SLuiz Augusto von Dentz */
4248d0b13706SLuiz Augusto von Dentz if (hdev->commands[26] & 0x08)
4249d0b13706SLuiz Augusto von Dentz events[0] |= 0x02; /* LE Advertising Report */
4250d0b13706SLuiz Augusto von Dentz
4251d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Create Connection
4252d0b13706SLuiz Augusto von Dentz * command, enable the corresponding event.
4253d0b13706SLuiz Augusto von Dentz */
4254d0b13706SLuiz Augusto von Dentz if (hdev->commands[26] & 0x10)
4255d0b13706SLuiz Augusto von Dentz events[0] |= 0x01; /* LE Connection Complete */
4256d0b13706SLuiz Augusto von Dentz
4257d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Connection Update
4258d0b13706SLuiz Augusto von Dentz * command, enable the corresponding event.
4259d0b13706SLuiz Augusto von Dentz */
4260d0b13706SLuiz Augusto von Dentz if (hdev->commands[27] & 0x04)
4261d0b13706SLuiz Augusto von Dentz events[0] |= 0x04; /* LE Connection Update Complete */
4262d0b13706SLuiz Augusto von Dentz
4263d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Read Remote Used Features
4264d0b13706SLuiz Augusto von Dentz * command, enable the corresponding event.
4265d0b13706SLuiz Augusto von Dentz */
4266d0b13706SLuiz Augusto von Dentz if (hdev->commands[27] & 0x20)
4267d0b13706SLuiz Augusto von Dentz /* LE Read Remote Used Features Complete */
4268d0b13706SLuiz Augusto von Dentz events[0] |= 0x08;
4269d0b13706SLuiz Augusto von Dentz
4270d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Read Local P-256
4271d0b13706SLuiz Augusto von Dentz * Public Key command, enable the corresponding event.
4272d0b13706SLuiz Augusto von Dentz */
4273d0b13706SLuiz Augusto von Dentz if (hdev->commands[34] & 0x02)
4274d0b13706SLuiz Augusto von Dentz /* LE Read Local P-256 Public Key Complete */
4275d0b13706SLuiz Augusto von Dentz events[0] |= 0x80;
4276d0b13706SLuiz Augusto von Dentz
4277d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Generate DHKey
4278d0b13706SLuiz Augusto von Dentz * command, enable the corresponding event.
4279d0b13706SLuiz Augusto von Dentz */
4280d0b13706SLuiz Augusto von Dentz if (hdev->commands[34] & 0x04)
4281d0b13706SLuiz Augusto von Dentz events[1] |= 0x01; /* LE Generate DHKey Complete */
4282d0b13706SLuiz Augusto von Dentz
4283d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Set Default PHY or
4284d0b13706SLuiz Augusto von Dentz * LE Set PHY commands, enable the corresponding event.
4285d0b13706SLuiz Augusto von Dentz */
4286d0b13706SLuiz Augusto von Dentz if (hdev->commands[35] & (0x20 | 0x40))
4287d0b13706SLuiz Augusto von Dentz events[1] |= 0x08; /* LE PHY Update Complete */
4288d0b13706SLuiz Augusto von Dentz
4289d0b13706SLuiz Augusto von Dentz /* If the controller supports LE Set Extended Scan Parameters
4290d0b13706SLuiz Augusto von Dentz * and LE Set Extended Scan Enable commands, enable the
4291d0b13706SLuiz Augusto von Dentz * corresponding event.
4292d0b13706SLuiz Augusto von Dentz */
4293d0b13706SLuiz Augusto von Dentz if (use_ext_scan(hdev))
4294d0b13706SLuiz Augusto von Dentz events[1] |= 0x10; /* LE Extended Advertising Report */
4295d0b13706SLuiz Augusto von Dentz
4296d0b13706SLuiz Augusto von Dentz /* If the controller supports the LE Extended Advertising
4297d0b13706SLuiz Augusto von Dentz * command, enable the corresponding event.
4298d0b13706SLuiz Augusto von Dentz */
4299d0b13706SLuiz Augusto von Dentz if (ext_adv_capable(hdev))
4300d0b13706SLuiz Augusto von Dentz events[2] |= 0x02; /* LE Advertising Set Terminated */
4301d0b13706SLuiz Augusto von Dentz
430226afbd82SLuiz Augusto von Dentz if (cis_capable(hdev)) {
430326afbd82SLuiz Augusto von Dentz events[3] |= 0x01; /* LE CIS Established */
430426afbd82SLuiz Augusto von Dentz if (cis_peripheral_capable(hdev))
430526afbd82SLuiz Augusto von Dentz events[3] |= 0x02; /* LE CIS Request */
430626afbd82SLuiz Augusto von Dentz }
430726afbd82SLuiz Augusto von Dentz
4308eca0ae4aSLuiz Augusto von Dentz if (bis_capable(hdev)) {
4309c33362a5SClaudia Draghicescu events[1] |= 0x20; /* LE PA Report */
4310c33362a5SClaudia Draghicescu events[1] |= 0x40; /* LE PA Sync Established */
4311eca0ae4aSLuiz Augusto von Dentz events[3] |= 0x04; /* LE Create BIG Complete */
4312eca0ae4aSLuiz Augusto von Dentz events[3] |= 0x08; /* LE Terminate BIG Complete */
4313eca0ae4aSLuiz Augusto von Dentz events[3] |= 0x10; /* LE BIG Sync Established */
4314eca0ae4aSLuiz Augusto von Dentz events[3] |= 0x20; /* LE BIG Sync Loss */
4315c33362a5SClaudia Draghicescu events[4] |= 0x02; /* LE BIG Info Advertising Report */
4316eca0ae4aSLuiz Augusto von Dentz }
4317eca0ae4aSLuiz Augusto von Dentz
4318d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4319d0b13706SLuiz Augusto von Dentz sizeof(events), events, HCI_CMD_TIMEOUT);
4320d0b13706SLuiz Augusto von Dentz }
4321d0b13706SLuiz Augusto von Dentz
hci_le_read_adv_tx_power_sync(struct hci_dev * hdev)4322d0b13706SLuiz Augusto von Dentz /* Read LE Advertising Channel TX Power */
4323d0b13706SLuiz Augusto von Dentz static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4324d0b13706SLuiz Augusto von Dentz {
4325d0b13706SLuiz Augusto von Dentz if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4326d0b13706SLuiz Augusto von Dentz /* HCI TS spec forbids mixing of legacy and extended
4327d0b13706SLuiz Augusto von Dentz * advertising commands wherein READ_ADV_TX_POWER is
4328d0b13706SLuiz Augusto von Dentz * also included. So do not call it if extended adv
4329d0b13706SLuiz Augusto von Dentz * is supported otherwise controller will return
4330d0b13706SLuiz Augusto von Dentz * COMMAND_DISALLOWED for extended commands.
4331d0b13706SLuiz Augusto von Dentz */
4332d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev,
4333d0b13706SLuiz Augusto von Dentz HCI_OP_LE_READ_ADV_TX_POWER,
4334d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4335d0b13706SLuiz Augusto von Dentz }
4336d0b13706SLuiz Augusto von Dentz
4337d0b13706SLuiz Augusto von Dentz return 0;
4338d0b13706SLuiz Augusto von Dentz }
4339d0b13706SLuiz Augusto von Dentz
hci_le_read_tx_power_sync(struct hci_dev * hdev)4340d0b13706SLuiz Augusto von Dentz /* Read LE Min/Max Tx Power*/
4341d0b13706SLuiz Augusto von Dentz static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4342d0b13706SLuiz Augusto von Dentz {
4343d2f8114fSAditya Garg if (!(hdev->commands[38] & 0x80) ||
4344d2f8114fSAditya Garg test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4345d0b13706SLuiz Augusto von Dentz return 0;
4346d0b13706SLuiz Augusto von Dentz
4347d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4348d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4349d0b13706SLuiz Augusto von Dentz }
4350d0b13706SLuiz Augusto von Dentz
hci_le_read_accept_list_size_sync(struct hci_dev * hdev)4351d0b13706SLuiz Augusto von Dentz /* Read LE Accept List Size */
4352d0b13706SLuiz Augusto von Dentz static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4353d0b13706SLuiz Augusto von Dentz {
4354d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[26] & 0x40))
4355d0b13706SLuiz Augusto von Dentz return 0;
4356d0b13706SLuiz Augusto von Dentz
4357d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4358d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4359d0b13706SLuiz Augusto von Dentz }
4360d0b13706SLuiz Augusto von Dentz
hci_le_clear_accept_list_sync(struct hci_dev * hdev)4361d0b13706SLuiz Augusto von Dentz /* Clear LE Accept List */
4362d0b13706SLuiz Augusto von Dentz static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
4363d0b13706SLuiz Augusto von Dentz {
4364d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[26] & 0x80))
4365d0b13706SLuiz Augusto von Dentz return 0;
4366d0b13706SLuiz Augusto von Dentz
4367d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
4368d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
4369d0b13706SLuiz Augusto von Dentz }
4370d0b13706SLuiz Augusto von Dentz
hci_le_read_resolv_list_size_sync(struct hci_dev * hdev)4371d0b13706SLuiz Augusto von Dentz /* Read LE Resolving List Size */
4372d0b13706SLuiz Augusto von Dentz static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4373d0b13706SLuiz Augusto von Dentz {
4374d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[34] & 0x40))
4375d0b13706SLuiz Augusto von Dentz return 0;
4376d0b13706SLuiz Augusto von Dentz
4377d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4378d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4379d0b13706SLuiz Augusto von Dentz }
4380d0b13706SLuiz Augusto von Dentz
hci_le_clear_resolv_list_sync(struct hci_dev * hdev)4381d0b13706SLuiz Augusto von Dentz /* Clear LE Resolving List */
4382d0b13706SLuiz Augusto von Dentz static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4383d0b13706SLuiz Augusto von Dentz {
4384d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[34] & 0x20))
4385d0b13706SLuiz Augusto von Dentz return 0;
4386d0b13706SLuiz Augusto von Dentz
4387d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4388d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
4389d0b13706SLuiz Augusto von Dentz }
4390d0b13706SLuiz Augusto von Dentz
hci_le_set_rpa_timeout_sync(struct hci_dev * hdev)4391d0b13706SLuiz Augusto von Dentz /* Set RPA timeout */
4392d0b13706SLuiz Augusto von Dentz static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4393d0b13706SLuiz Augusto von Dentz {
4394d0b13706SLuiz Augusto von Dentz __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4395d0b13706SLuiz Augusto von Dentz
439691b6d02dSRaul Cheleguini if (!(hdev->commands[35] & 0x04) ||
439791b6d02dSRaul Cheleguini test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4398d0b13706SLuiz Augusto von Dentz return 0;
4399d0b13706SLuiz Augusto von Dentz
4400d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4401d0b13706SLuiz Augusto von Dentz sizeof(timeout), &timeout,
4402d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
4403d0b13706SLuiz Augusto von Dentz }
4404d0b13706SLuiz Augusto von Dentz
hci_le_read_max_data_len_sync(struct hci_dev * hdev)4405d0b13706SLuiz Augusto von Dentz /* Read LE Maximum Data Length */
4406d0b13706SLuiz Augusto von Dentz static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4407d0b13706SLuiz Augusto von Dentz {
4408d0b13706SLuiz Augusto von Dentz if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4409d0b13706SLuiz Augusto von Dentz return 0;
4410d0b13706SLuiz Augusto von Dentz
4411d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4412d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
4413d0b13706SLuiz Augusto von Dentz }
4414d0b13706SLuiz Augusto von Dentz
hci_le_read_def_data_len_sync(struct hci_dev * hdev)4415d0b13706SLuiz Augusto von Dentz /* Read LE Suggested Default Data Length */
4416d0b13706SLuiz Augusto von Dentz static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4417d0b13706SLuiz Augusto von Dentz {
4418d0b13706SLuiz Augusto von Dentz if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4419d0b13706SLuiz Augusto von Dentz return 0;
4420d0b13706SLuiz Augusto von Dentz
4421d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4422d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
4423d0b13706SLuiz Augusto von Dentz }
4424d0b13706SLuiz Augusto von Dentz
hci_le_read_num_support_adv_sets_sync(struct hci_dev * hdev)4425d0b13706SLuiz Augusto von Dentz /* Read LE Number of Supported Advertising Sets */
4426d0b13706SLuiz Augusto von Dentz static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4427d0b13706SLuiz Augusto von Dentz {
4428d0b13706SLuiz Augusto von Dentz if (!ext_adv_capable(hdev))
4429d0b13706SLuiz Augusto von Dentz return 0;
4430d0b13706SLuiz Augusto von Dentz
4431d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev,
4432d0b13706SLuiz Augusto von Dentz HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4433d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4434d0b13706SLuiz Augusto von Dentz }
4435d0b13706SLuiz Augusto von Dentz
hci_set_le_support_sync(struct hci_dev * hdev)4436d0b13706SLuiz Augusto von Dentz /* Write LE Host Supported */
4437d0b13706SLuiz Augusto von Dentz static int hci_set_le_support_sync(struct hci_dev *hdev)
4438d0b13706SLuiz Augusto von Dentz {
4439d0b13706SLuiz Augusto von Dentz struct hci_cp_write_le_host_supported cp;
4440d0b13706SLuiz Augusto von Dentz
4441d0b13706SLuiz Augusto von Dentz /* LE-only devices do not support explicit enablement */
4442d0b13706SLuiz Augusto von Dentz if (!lmp_bredr_capable(hdev))
4443d0b13706SLuiz Augusto von Dentz return 0;
4444d0b13706SLuiz Augusto von Dentz
4445d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
4446d0b13706SLuiz Augusto von Dentz
4447d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4448d0b13706SLuiz Augusto von Dentz cp.le = 0x01;
4449d0b13706SLuiz Augusto von Dentz cp.simul = 0x00;
4450d0b13706SLuiz Augusto von Dentz }
4451d0b13706SLuiz Augusto von Dentz
4452d0b13706SLuiz Augusto von Dentz if (cp.le == lmp_host_le_capable(hdev))
4453d0b13706SLuiz Augusto von Dentz return 0;
4454d0b13706SLuiz Augusto von Dentz
4455d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4456d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4457d0b13706SLuiz Augusto von Dentz }
4458d0b13706SLuiz Augusto von Dentz
hci_le_set_host_feature_sync(struct hci_dev * hdev)445926afbd82SLuiz Augusto von Dentz /* LE Set Host Feature */
446026afbd82SLuiz Augusto von Dentz static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
446126afbd82SLuiz Augusto von Dentz {
446226afbd82SLuiz Augusto von Dentz struct hci_cp_le_set_host_feature cp;
446326afbd82SLuiz Augusto von Dentz
446426afbd82SLuiz Augusto von Dentz if (!iso_capable(hdev))
446526afbd82SLuiz Augusto von Dentz return 0;
446626afbd82SLuiz Augusto von Dentz
446726afbd82SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
446826afbd82SLuiz Augusto von Dentz
446926afbd82SLuiz Augusto von Dentz /* Isochronous Channels (Host Support) */
447026afbd82SLuiz Augusto von Dentz cp.bit_number = 32;
447126afbd82SLuiz Augusto von Dentz cp.bit_value = 1;
447226afbd82SLuiz Augusto von Dentz
447326afbd82SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
447426afbd82SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
447526afbd82SLuiz Augusto von Dentz }
447626afbd82SLuiz Augusto von Dentz
4477d0b13706SLuiz Augusto von Dentz /* LE Controller init stage 3 command sequence */
4478d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init3[] = {
4479d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_SET_EVENT_MASK */
4480d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_set_event_mask_sync),
4481d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_ADV_TX_POWER */
4482d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_adv_tx_power_sync),
4483d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_TRANSMIT_POWER */
4484d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_tx_power_sync),
4485d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4486d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_accept_list_size_sync),
4487d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4488d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_clear_accept_list_sync),
4489d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4490d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_resolv_list_size_sync),
4491d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_CLEAR_RESOLV_LIST */
4492d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_clear_resolv_list_sync),
4493d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_SET_RPA_TIMEOUT */
4494d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_set_rpa_timeout_sync),
4495d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_MAX_DATA_LEN */
4496d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_max_data_len_sync),
4497d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_DEF_DATA_LEN */
4498d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_def_data_len_sync),
4499d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4500d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4501d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4502d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_set_le_support_sync),
450326afbd82SLuiz Augusto von Dentz /* HCI_OP_LE_SET_HOST_FEATURE */
450426afbd82SLuiz Augusto von Dentz HCI_INIT(hci_le_set_host_feature_sync),
4505d0b13706SLuiz Augusto von Dentz {}
4506d0b13706SLuiz Augusto von Dentz };
hci_init3_sync(struct hci_dev * hdev)4507d0b13706SLuiz Augusto von Dentz
4508d0b13706SLuiz Augusto von Dentz static int hci_init3_sync(struct hci_dev *hdev)
4509d0b13706SLuiz Augusto von Dentz {
4510d0b13706SLuiz Augusto von Dentz int err;
4511d0b13706SLuiz Augusto von Dentz
4512d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
4513d0b13706SLuiz Augusto von Dentz
4514d0b13706SLuiz Augusto von Dentz err = hci_init_stage_sync(hdev, hci_init3);
4515d0b13706SLuiz Augusto von Dentz if (err)
4516d0b13706SLuiz Augusto von Dentz return err;
4517d0b13706SLuiz Augusto von Dentz
4518d0b13706SLuiz Augusto von Dentz if (lmp_le_capable(hdev))
4519d0b13706SLuiz Augusto von Dentz return hci_init_stage_sync(hdev, le_init3);
4520d0b13706SLuiz Augusto von Dentz
4521d0b13706SLuiz Augusto von Dentz return 0;
4522d0b13706SLuiz Augusto von Dentz }
hci_delete_stored_link_key_sync(struct hci_dev * hdev)4523d0b13706SLuiz Augusto von Dentz
4524d0b13706SLuiz Augusto von Dentz static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4525d0b13706SLuiz Augusto von Dentz {
4526d0b13706SLuiz Augusto von Dentz struct hci_cp_delete_stored_link_key cp;
4527d0b13706SLuiz Augusto von Dentz
4528d0b13706SLuiz Augusto von Dentz /* Some Broadcom based Bluetooth controllers do not support the
4529d0b13706SLuiz Augusto von Dentz * Delete Stored Link Key command. They are clearly indicating its
4530d0b13706SLuiz Augusto von Dentz * absence in the bit mask of supported commands.
4531d0b13706SLuiz Augusto von Dentz *
4532d0b13706SLuiz Augusto von Dentz * Check the supported commands and only if the command is marked
4533d0b13706SLuiz Augusto von Dentz * as supported send it. If not supported assume that the controller
4534d0b13706SLuiz Augusto von Dentz * does not have actual support for stored link keys which makes this
4535d0b13706SLuiz Augusto von Dentz * command redundant anyway.
4536d0b13706SLuiz Augusto von Dentz *
4537d0b13706SLuiz Augusto von Dentz * Some controllers indicate that they support handling deleting
4538d0b13706SLuiz Augusto von Dentz * stored link keys, but they don't. The quirk lets a driver
4539d0b13706SLuiz Augusto von Dentz * just disable this command.
4540d0b13706SLuiz Augusto von Dentz */
4541d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[6] & 0x80) ||
4542d0b13706SLuiz Augusto von Dentz test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4543d0b13706SLuiz Augusto von Dentz return 0;
4544d0b13706SLuiz Augusto von Dentz
4545d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
4546d0b13706SLuiz Augusto von Dentz bacpy(&cp.bdaddr, BDADDR_ANY);
4547d0b13706SLuiz Augusto von Dentz cp.delete_all = 0x01;
4548d0b13706SLuiz Augusto von Dentz
4549d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4550d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4551d0b13706SLuiz Augusto von Dentz }
hci_set_event_mask_page_2_sync(struct hci_dev * hdev)4552d0b13706SLuiz Augusto von Dentz
4553d0b13706SLuiz Augusto von Dentz static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4554d0b13706SLuiz Augusto von Dentz {
4555d0b13706SLuiz Augusto von Dentz u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4556d0b13706SLuiz Augusto von Dentz bool changed = false;
4557d0b13706SLuiz Augusto von Dentz
4558d0b13706SLuiz Augusto von Dentz /* Set event mask page 2 if the HCI command for it is supported */
4559d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[22] & 0x04))
4560d0b13706SLuiz Augusto von Dentz return 0;
4561d0b13706SLuiz Augusto von Dentz
4562d0b13706SLuiz Augusto von Dentz /* If Connectionless Peripheral Broadcast central role is supported
4563d0b13706SLuiz Augusto von Dentz * enable all necessary events for it.
4564d0b13706SLuiz Augusto von Dentz */
4565d0b13706SLuiz Augusto von Dentz if (lmp_cpb_central_capable(hdev)) {
4566d0b13706SLuiz Augusto von Dentz events[1] |= 0x40; /* Triggered Clock Capture */
4567d0b13706SLuiz Augusto von Dentz events[1] |= 0x80; /* Synchronization Train Complete */
45680feb8af0SZijun Hu events[2] |= 0x08; /* Truncated Page Complete */
4569d0b13706SLuiz Augusto von Dentz events[2] |= 0x20; /* CPB Channel Map Change */
4570d0b13706SLuiz Augusto von Dentz changed = true;
4571d0b13706SLuiz Augusto von Dentz }
4572d0b13706SLuiz Augusto von Dentz
4573d0b13706SLuiz Augusto von Dentz /* If Connectionless Peripheral Broadcast peripheral role is supported
4574d0b13706SLuiz Augusto von Dentz * enable all necessary events for it.
4575d0b13706SLuiz Augusto von Dentz */
4576d0b13706SLuiz Augusto von Dentz if (lmp_cpb_peripheral_capable(hdev)) {
4577d0b13706SLuiz Augusto von Dentz events[2] |= 0x01; /* Synchronization Train Received */
4578d0b13706SLuiz Augusto von Dentz events[2] |= 0x02; /* CPB Receive */
4579d0b13706SLuiz Augusto von Dentz events[2] |= 0x04; /* CPB Timeout */
45800feb8af0SZijun Hu events[2] |= 0x10; /* Peripheral Page Response Timeout */
4581d0b13706SLuiz Augusto von Dentz changed = true;
4582d0b13706SLuiz Augusto von Dentz }
4583d0b13706SLuiz Augusto von Dentz
4584d0b13706SLuiz Augusto von Dentz /* Enable Authenticated Payload Timeout Expired event if supported */
4585d0b13706SLuiz Augusto von Dentz if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4586d0b13706SLuiz Augusto von Dentz events[2] |= 0x80;
4587d0b13706SLuiz Augusto von Dentz changed = true;
4588d0b13706SLuiz Augusto von Dentz }
4589d0b13706SLuiz Augusto von Dentz
4590d0b13706SLuiz Augusto von Dentz /* Some Broadcom based controllers indicate support for Set Event
4591d0b13706SLuiz Augusto von Dentz * Mask Page 2 command, but then actually do not support it. Since
4592d0b13706SLuiz Augusto von Dentz * the default value is all bits set to zero, the command is only
4593d0b13706SLuiz Augusto von Dentz * required if the event mask has to be changed. In case no change
4594d0b13706SLuiz Augusto von Dentz * to the event mask is needed, skip this command.
4595d0b13706SLuiz Augusto von Dentz */
4596d0b13706SLuiz Augusto von Dentz if (!changed)
4597d0b13706SLuiz Augusto von Dentz return 0;
4598d0b13706SLuiz Augusto von Dentz
4599d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4600d0b13706SLuiz Augusto von Dentz sizeof(events), events, HCI_CMD_TIMEOUT);
4601d0b13706SLuiz Augusto von Dentz }
4602d0b13706SLuiz Augusto von Dentz
hci_read_local_codecs_sync(struct hci_dev * hdev)4603d0b13706SLuiz Augusto von Dentz /* Read local codec list if the HCI command is supported */
4604d0b13706SLuiz Augusto von Dentz static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4605d0b13706SLuiz Augusto von Dentz {
4606828cea2bSChethan T N if (hdev->commands[45] & 0x04)
4607828cea2bSChethan T N hci_read_supported_codecs_v2(hdev);
4608828cea2bSChethan T N else if (hdev->commands[29] & 0x20)
4609828cea2bSChethan T N hci_read_supported_codecs(hdev);
4610d0b13706SLuiz Augusto von Dentz
4611828cea2bSChethan T N return 0;
4612d0b13706SLuiz Augusto von Dentz }
4613d0b13706SLuiz Augusto von Dentz
hci_read_local_pairing_opts_sync(struct hci_dev * hdev)4614d0b13706SLuiz Augusto von Dentz /* Read local pairing options if the HCI command is supported */
4615d0b13706SLuiz Augusto von Dentz static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4616d0b13706SLuiz Augusto von Dentz {
4617d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[41] & 0x08))
4618d0b13706SLuiz Augusto von Dentz return 0;
4619d0b13706SLuiz Augusto von Dentz
4620d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4621d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4622d0b13706SLuiz Augusto von Dentz }
4623d0b13706SLuiz Augusto von Dentz
hci_get_mws_transport_config_sync(struct hci_dev * hdev)4624d0b13706SLuiz Augusto von Dentz /* Get MWS transport configuration if the HCI command is supported */
4625d0b13706SLuiz Augusto von Dentz static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4626d0b13706SLuiz Augusto von Dentz {
4627ffcb0a44SSven Peter if (!mws_transport_config_capable(hdev))
4628d0b13706SLuiz Augusto von Dentz return 0;
4629d0b13706SLuiz Augusto von Dentz
4630d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4631d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4632d0b13706SLuiz Augusto von Dentz }
4633d0b13706SLuiz Augusto von Dentz
hci_read_sync_train_params_sync(struct hci_dev * hdev)4634d0b13706SLuiz Augusto von Dentz /* Check for Synchronization Train support */
4635d0b13706SLuiz Augusto von Dentz static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4636d0b13706SLuiz Augusto von Dentz {
4637d0b13706SLuiz Augusto von Dentz if (!lmp_sync_train_capable(hdev))
4638d0b13706SLuiz Augusto von Dentz return 0;
4639d0b13706SLuiz Augusto von Dentz
4640d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4641d0b13706SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
4642d0b13706SLuiz Augusto von Dentz }
4643d0b13706SLuiz Augusto von Dentz
hci_write_sc_support_1_sync(struct hci_dev * hdev)4644d0b13706SLuiz Augusto von Dentz /* Enable Secure Connections if supported and configured */
4645d0b13706SLuiz Augusto von Dentz static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4646d0b13706SLuiz Augusto von Dentz {
4647d0b13706SLuiz Augusto von Dentz u8 support = 0x01;
4648d0b13706SLuiz Augusto von Dentz
4649d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4650d0b13706SLuiz Augusto von Dentz !bredr_sc_enabled(hdev))
4651d0b13706SLuiz Augusto von Dentz return 0;
4652d0b13706SLuiz Augusto von Dentz
4653d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4654d0b13706SLuiz Augusto von Dentz sizeof(support), &support,
4655d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
4656d0b13706SLuiz Augusto von Dentz }
4657d0b13706SLuiz Augusto von Dentz
4658d0b13706SLuiz Augusto von Dentz /* Set erroneous data reporting if supported to the wideband speech
4659d0b13706SLuiz Augusto von Dentz * setting value
hci_set_err_data_report_sync(struct hci_dev * hdev)4660d0b13706SLuiz Augusto von Dentz */
4661d0b13706SLuiz Augusto von Dentz static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4662d0b13706SLuiz Augusto von Dentz {
4663d0b13706SLuiz Augusto von Dentz struct hci_cp_write_def_err_data_reporting cp;
4664d0b13706SLuiz Augusto von Dentz bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4665d0b13706SLuiz Augusto von Dentz
4666d0b13706SLuiz Augusto von Dentz if (!(hdev->commands[18] & 0x08) ||
466742d7731eSIsmael Ferreras Morezuelas !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
466842d7731eSIsmael Ferreras Morezuelas test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4669d0b13706SLuiz Augusto von Dentz return 0;
4670d0b13706SLuiz Augusto von Dentz
4671d0b13706SLuiz Augusto von Dentz if (enabled == hdev->err_data_reporting)
4672d0b13706SLuiz Augusto von Dentz return 0;
4673d0b13706SLuiz Augusto von Dentz
4674d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
4675d0b13706SLuiz Augusto von Dentz cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4676d0b13706SLuiz Augusto von Dentz ERR_DATA_REPORTING_DISABLED;
4677d0b13706SLuiz Augusto von Dentz
4678d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4679d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4680d0b13706SLuiz Augusto von Dentz }
4681d0b13706SLuiz Augusto von Dentz
4682d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init4[] = {
4683d0b13706SLuiz Augusto von Dentz /* HCI_OP_DELETE_STORED_LINK_KEY */
4684d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_delete_stored_link_key_sync),
4685d0b13706SLuiz Augusto von Dentz /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4686d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_set_event_mask_page_2_sync),
4687d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_CODECS */
4688d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_codecs_sync),
4689d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4690d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_local_pairing_opts_sync),
4691d0b13706SLuiz Augusto von Dentz /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4692d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_get_mws_transport_config_sync),
4693d0b13706SLuiz Augusto von Dentz /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4694d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_read_sync_train_params_sync),
4695d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_SC_SUPPORT */
4696d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_write_sc_support_1_sync),
4697d0b13706SLuiz Augusto von Dentz /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4698d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_set_err_data_report_sync),
4699d0b13706SLuiz Augusto von Dentz {}
4700d0b13706SLuiz Augusto von Dentz };
4701d0b13706SLuiz Augusto von Dentz
hci_le_set_write_def_data_len_sync(struct hci_dev * hdev)4702d0b13706SLuiz Augusto von Dentz /* Set Suggested Default Data Length to maximum if supported */
4703d0b13706SLuiz Augusto von Dentz static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4704d0b13706SLuiz Augusto von Dentz {
4705d0b13706SLuiz Augusto von Dentz struct hci_cp_le_write_def_data_len cp;
4706d0b13706SLuiz Augusto von Dentz
4707d0b13706SLuiz Augusto von Dentz if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4708d0b13706SLuiz Augusto von Dentz return 0;
4709d0b13706SLuiz Augusto von Dentz
4710d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
4711d0b13706SLuiz Augusto von Dentz cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4712d0b13706SLuiz Augusto von Dentz cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4713d0b13706SLuiz Augusto von Dentz
4714d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4715d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4716d0b13706SLuiz Augusto von Dentz }
4717d0b13706SLuiz Augusto von Dentz
4718288c9022SLuiz Augusto von Dentz /* Set Default PHY parameters if command is supported, enables all supported
4719288c9022SLuiz Augusto von Dentz * PHYs according to the LE Features bits.
hci_le_set_default_phy_sync(struct hci_dev * hdev)4720288c9022SLuiz Augusto von Dentz */
4721d0b13706SLuiz Augusto von Dentz static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4722d0b13706SLuiz Augusto von Dentz {
4723d0b13706SLuiz Augusto von Dentz struct hci_cp_le_set_default_phy cp;
4724d0b13706SLuiz Augusto von Dentz
4725288c9022SLuiz Augusto von Dentz if (!(hdev->commands[35] & 0x20)) {
4726288c9022SLuiz Augusto von Dentz /* If the command is not supported it means only 1M PHY is
4727288c9022SLuiz Augusto von Dentz * supported.
4728288c9022SLuiz Augusto von Dentz */
4729288c9022SLuiz Augusto von Dentz hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4730288c9022SLuiz Augusto von Dentz hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4731d0b13706SLuiz Augusto von Dentz return 0;
4732288c9022SLuiz Augusto von Dentz }
4733d0b13706SLuiz Augusto von Dentz
4734d0b13706SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
4735d0b13706SLuiz Augusto von Dentz cp.all_phys = 0x00;
4736288c9022SLuiz Augusto von Dentz cp.tx_phys = HCI_LE_SET_PHY_1M;
4737288c9022SLuiz Augusto von Dentz cp.rx_phys = HCI_LE_SET_PHY_1M;
4738288c9022SLuiz Augusto von Dentz
4739288c9022SLuiz Augusto von Dentz /* Enables 2M PHY if supported */
4740288c9022SLuiz Augusto von Dentz if (le_2m_capable(hdev)) {
4741288c9022SLuiz Augusto von Dentz cp.tx_phys |= HCI_LE_SET_PHY_2M;
4742288c9022SLuiz Augusto von Dentz cp.rx_phys |= HCI_LE_SET_PHY_2M;
4743288c9022SLuiz Augusto von Dentz }
4744288c9022SLuiz Augusto von Dentz
4745288c9022SLuiz Augusto von Dentz /* Enables Coded PHY if supported */
4746288c9022SLuiz Augusto von Dentz if (le_coded_capable(hdev)) {
4747288c9022SLuiz Augusto von Dentz cp.tx_phys |= HCI_LE_SET_PHY_CODED;
4748288c9022SLuiz Augusto von Dentz cp.rx_phys |= HCI_LE_SET_PHY_CODED;
4749288c9022SLuiz Augusto von Dentz }
4750d0b13706SLuiz Augusto von Dentz
4751d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4752d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4753d0b13706SLuiz Augusto von Dentz }
4754d0b13706SLuiz Augusto von Dentz
4755d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init4[] = {
4756d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4757d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_set_write_def_data_len_sync),
4758d0b13706SLuiz Augusto von Dentz /* HCI_OP_LE_SET_DEFAULT_PHY */
4759d0b13706SLuiz Augusto von Dentz HCI_INIT(hci_le_set_default_phy_sync),
4760d0b13706SLuiz Augusto von Dentz {}
4761d0b13706SLuiz Augusto von Dentz };
hci_init4_sync(struct hci_dev * hdev)4762d0b13706SLuiz Augusto von Dentz
4763d0b13706SLuiz Augusto von Dentz static int hci_init4_sync(struct hci_dev *hdev)
4764d0b13706SLuiz Augusto von Dentz {
4765d0b13706SLuiz Augusto von Dentz int err;
4766d0b13706SLuiz Augusto von Dentz
4767d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
4768d0b13706SLuiz Augusto von Dentz
4769d0b13706SLuiz Augusto von Dentz err = hci_init_stage_sync(hdev, hci_init4);
4770d0b13706SLuiz Augusto von Dentz if (err)
4771d0b13706SLuiz Augusto von Dentz return err;
4772d0b13706SLuiz Augusto von Dentz
4773d0b13706SLuiz Augusto von Dentz if (lmp_le_capable(hdev))
4774d0b13706SLuiz Augusto von Dentz return hci_init_stage_sync(hdev, le_init4);
4775d0b13706SLuiz Augusto von Dentz
4776d0b13706SLuiz Augusto von Dentz return 0;
4777d0b13706SLuiz Augusto von Dentz }
hci_init_sync(struct hci_dev * hdev)4778d0b13706SLuiz Augusto von Dentz
4779d0b13706SLuiz Augusto von Dentz static int hci_init_sync(struct hci_dev *hdev)
4780d0b13706SLuiz Augusto von Dentz {
4781d0b13706SLuiz Augusto von Dentz int err;
4782d0b13706SLuiz Augusto von Dentz
4783d0b13706SLuiz Augusto von Dentz err = hci_init1_sync(hdev);
4784d0b13706SLuiz Augusto von Dentz if (err < 0)
4785d0b13706SLuiz Augusto von Dentz return err;
4786d0b13706SLuiz Augusto von Dentz
4787d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_SETUP))
4788d0b13706SLuiz Augusto von Dentz hci_debugfs_create_basic(hdev);
4789d0b13706SLuiz Augusto von Dentz
4790d0b13706SLuiz Augusto von Dentz err = hci_init2_sync(hdev);
4791d0b13706SLuiz Augusto von Dentz if (err < 0)
4792d0b13706SLuiz Augusto von Dentz return err;
4793d0b13706SLuiz Augusto von Dentz
4794d0b13706SLuiz Augusto von Dentz err = hci_init3_sync(hdev);
4795d0b13706SLuiz Augusto von Dentz if (err < 0)
4796d0b13706SLuiz Augusto von Dentz return err;
4797d0b13706SLuiz Augusto von Dentz
4798d0b13706SLuiz Augusto von Dentz err = hci_init4_sync(hdev);
4799d0b13706SLuiz Augusto von Dentz if (err < 0)
4800d0b13706SLuiz Augusto von Dentz return err;
4801d0b13706SLuiz Augusto von Dentz
4802d0b13706SLuiz Augusto von Dentz /* This function is only called when the controller is actually in
4803d0b13706SLuiz Augusto von Dentz * configured state. When the controller is marked as unconfigured,
4804d0b13706SLuiz Augusto von Dentz * this initialization procedure is not run.
4805d0b13706SLuiz Augusto von Dentz *
4806d0b13706SLuiz Augusto von Dentz * It means that it is possible that a controller runs through its
4807d0b13706SLuiz Augusto von Dentz * setup phase and then discovers missing settings. If that is the
4808d0b13706SLuiz Augusto von Dentz * case, then this function will not be called. It then will only
4809d0b13706SLuiz Augusto von Dentz * be called during the config phase.
4810d0b13706SLuiz Augusto von Dentz *
4811d0b13706SLuiz Augusto von Dentz * So only when in setup phase or config phase, create the debugfs
4812d0b13706SLuiz Augusto von Dentz * entries and register the SMP channels.
4813d0b13706SLuiz Augusto von Dentz */
4814d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4815d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_CONFIG))
4816d0b13706SLuiz Augusto von Dentz return 0;
4817d0b13706SLuiz Augusto von Dentz
4818fe2ccc6cSJohan Hovold if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4819fe2ccc6cSJohan Hovold return 0;
4820fe2ccc6cSJohan Hovold
4821d0b13706SLuiz Augusto von Dentz hci_debugfs_create_common(hdev);
4822d0b13706SLuiz Augusto von Dentz
4823d0b13706SLuiz Augusto von Dentz if (lmp_bredr_capable(hdev))
4824d0b13706SLuiz Augusto von Dentz hci_debugfs_create_bredr(hdev);
4825d0b13706SLuiz Augusto von Dentz
4826d0b13706SLuiz Augusto von Dentz if (lmp_le_capable(hdev))
4827d0b13706SLuiz Augusto von Dentz hci_debugfs_create_le(hdev);
4828d0b13706SLuiz Augusto von Dentz
4829d0b13706SLuiz Augusto von Dentz return 0;
4830d0b13706SLuiz Augusto von Dentz }
4831d0b13706SLuiz Augusto von Dentz
48326b5c1cdaSLuiz Augusto von Dentz #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
48336b5c1cdaSLuiz Augusto von Dentz
48346b5c1cdaSLuiz Augusto von Dentz static const struct {
48356b5c1cdaSLuiz Augusto von Dentz unsigned long quirk;
48366b5c1cdaSLuiz Augusto von Dentz const char *desc;
48376b5c1cdaSLuiz Augusto von Dentz } hci_broken_table[] = {
48386b5c1cdaSLuiz Augusto von Dentz HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
48396b5c1cdaSLuiz Augusto von Dentz "HCI Read Local Supported Commands not supported"),
48406b5c1cdaSLuiz Augusto von Dentz HCI_QUIRK_BROKEN(STORED_LINK_KEY,
48416b5c1cdaSLuiz Augusto von Dentz "HCI Delete Stored Link Key command is advertised, "
48426b5c1cdaSLuiz Augusto von Dentz "but not supported."),
484342d7731eSIsmael Ferreras Morezuelas HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
484442d7731eSIsmael Ferreras Morezuelas "HCI Read Default Erroneous Data Reporting command is "
484542d7731eSIsmael Ferreras Morezuelas "advertised, but not supported."),
48466b5c1cdaSLuiz Augusto von Dentz HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
48476b5c1cdaSLuiz Augusto von Dentz "HCI Read Transmit Power Level command is advertised, "
48486b5c1cdaSLuiz Augusto von Dentz "but not supported."),
48496b5c1cdaSLuiz Augusto von Dentz HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
48506b5c1cdaSLuiz Augusto von Dentz "HCI Set Event Filter command not supported."),
48516b5c1cdaSLuiz Augusto von Dentz HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
48526b5c1cdaSLuiz Augusto von Dentz "HCI Enhanced Setup Synchronous Connection command is "
485391b6d02dSRaul Cheleguini "advertised, but not supported."),
485491b6d02dSRaul Cheleguini HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
485591b6d02dSRaul Cheleguini "HCI LE Set Random Private Address Timeout command is "
4856253f3399SLuiz Augusto von Dentz "advertised, but not supported."),
4857ac22911fSDanil Pylaev HCI_QUIRK_BROKEN(EXT_CREATE_CONN,
4858ac22911fSDanil Pylaev "HCI LE Extended Create Connection command is "
4859ac22911fSDanil Pylaev "advertised, but not supported."),
4860ac22911fSDanil Pylaev HCI_QUIRK_BROKEN(WRITE_AUTH_PAYLOAD_TIMEOUT,
4861ac22911fSDanil Pylaev "HCI WRITE AUTH PAYLOAD TIMEOUT command leads "
4862ac22911fSDanil Pylaev "to unexpected SMP errors when pairing "
4863ac22911fSDanil Pylaev "and will not be used."),
4864253f3399SLuiz Augusto von Dentz HCI_QUIRK_BROKEN(LE_CODED,
4865253f3399SLuiz Augusto von Dentz "HCI LE Coded PHY feature bit is set, "
4866253f3399SLuiz Augusto von Dentz "but its usage is not supported.")
48676b5c1cdaSLuiz Augusto von Dentz };
48686b5c1cdaSLuiz Augusto von Dentz
48691bbf4023SLuiz Augusto von Dentz /* This function handles hdev setup stage:
48701bbf4023SLuiz Augusto von Dentz *
48711bbf4023SLuiz Augusto von Dentz * Calls hdev->setup
48721bbf4023SLuiz Augusto von Dentz * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
hci_dev_setup_sync(struct hci_dev * hdev)48731bbf4023SLuiz Augusto von Dentz */
48741bbf4023SLuiz Augusto von Dentz static int hci_dev_setup_sync(struct hci_dev *hdev)
4875d0b13706SLuiz Augusto von Dentz {
4876d0b13706SLuiz Augusto von Dentz int ret = 0;
48771bbf4023SLuiz Augusto von Dentz bool invalid_bdaddr;
48781bbf4023SLuiz Augusto von Dentz size_t i;
48791bbf4023SLuiz Augusto von Dentz
48801bbf4023SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
48811bbf4023SLuiz Augusto von Dentz !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
48821bbf4023SLuiz Augusto von Dentz return 0;
48831bbf4023SLuiz Augusto von Dentz
48841bbf4023SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
48851bbf4023SLuiz Augusto von Dentz
48861bbf4023SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_SETUP);
48871bbf4023SLuiz Augusto von Dentz
48881bbf4023SLuiz Augusto von Dentz if (hdev->setup)
48891bbf4023SLuiz Augusto von Dentz ret = hdev->setup(hdev);
48901bbf4023SLuiz Augusto von Dentz
48911bbf4023SLuiz Augusto von Dentz for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
48921bbf4023SLuiz Augusto von Dentz if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
48931bbf4023SLuiz Augusto von Dentz bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
48941bbf4023SLuiz Augusto von Dentz }
48951bbf4023SLuiz Augusto von Dentz
48961bbf4023SLuiz Augusto von Dentz /* The transport driver can set the quirk to mark the
48971bbf4023SLuiz Augusto von Dentz * BD_ADDR invalid before creating the HCI device or in
48981bbf4023SLuiz Augusto von Dentz * its setup callback.
48991bbf4023SLuiz Augusto von Dentz */
49006945795bSJohan Hovold invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
49016945795bSJohan Hovold test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
49021bbf4023SLuiz Augusto von Dentz if (!ret) {
49030cb73658SJohan Hovold if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
49040cb73658SJohan Hovold !bacmp(&hdev->public_addr, BDADDR_ANY))
49051bbf4023SLuiz Augusto von Dentz hci_dev_get_bd_addr_from_property(hdev);
49061bbf4023SLuiz Augusto von Dentz
49076945795bSJohan Hovold if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
49081bbf4023SLuiz Augusto von Dentz hdev->set_bdaddr) {
49090cb73658SJohan Hovold ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
49101bbf4023SLuiz Augusto von Dentz if (!ret)
49111bbf4023SLuiz Augusto von Dentz invalid_bdaddr = false;
49121bbf4023SLuiz Augusto von Dentz }
49131bbf4023SLuiz Augusto von Dentz }
49141bbf4023SLuiz Augusto von Dentz
49151bbf4023SLuiz Augusto von Dentz /* The transport driver can set these quirks before
49161bbf4023SLuiz Augusto von Dentz * creating the HCI device or in its setup callback.
49171bbf4023SLuiz Augusto von Dentz *
49181bbf4023SLuiz Augusto von Dentz * For the invalid BD_ADDR quirk it is possible that
49191bbf4023SLuiz Augusto von Dentz * it becomes a valid address if the bootloader does
49201bbf4023SLuiz Augusto von Dentz * provide it (see above).
49211bbf4023SLuiz Augusto von Dentz *
49221bbf4023SLuiz Augusto von Dentz * In case any of them is set, the controller has to
49231bbf4023SLuiz Augusto von Dentz * start up as unconfigured.
49241bbf4023SLuiz Augusto von Dentz */
49251bbf4023SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
49261bbf4023SLuiz Augusto von Dentz invalid_bdaddr)
49271bbf4023SLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
49281bbf4023SLuiz Augusto von Dentz
49291bbf4023SLuiz Augusto von Dentz /* For an unconfigured controller it is required to
49301bbf4023SLuiz Augusto von Dentz * read at least the version information provided by
49311bbf4023SLuiz Augusto von Dentz * the Read Local Version Information command.
49321bbf4023SLuiz Augusto von Dentz *
49331bbf4023SLuiz Augusto von Dentz * If the set_bdaddr driver callback is provided, then
49341bbf4023SLuiz Augusto von Dentz * also the original Bluetooth public device address
49351bbf4023SLuiz Augusto von Dentz * will be read using the Read BD Address command.
49361bbf4023SLuiz Augusto von Dentz */
49371bbf4023SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
49381bbf4023SLuiz Augusto von Dentz return hci_unconf_init_sync(hdev);
49391bbf4023SLuiz Augusto von Dentz
49401bbf4023SLuiz Augusto von Dentz return ret;
49411bbf4023SLuiz Augusto von Dentz }
49421bbf4023SLuiz Augusto von Dentz
49431bbf4023SLuiz Augusto von Dentz /* This function handles hdev init stage:
49441bbf4023SLuiz Augusto von Dentz *
49451bbf4023SLuiz Augusto von Dentz * Calls hci_dev_setup_sync to perform setup stage
49461bbf4023SLuiz Augusto von Dentz * Calls hci_init_sync to perform HCI command init sequence
hci_dev_init_sync(struct hci_dev * hdev)49471bbf4023SLuiz Augusto von Dentz */
49481bbf4023SLuiz Augusto von Dentz static int hci_dev_init_sync(struct hci_dev *hdev)
49491bbf4023SLuiz Augusto von Dentz {
49501bbf4023SLuiz Augusto von Dentz int ret;
49511bbf4023SLuiz Augusto von Dentz
49521bbf4023SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
49531bbf4023SLuiz Augusto von Dentz
49541bbf4023SLuiz Augusto von Dentz atomic_set(&hdev->cmd_cnt, 1);
49551bbf4023SLuiz Augusto von Dentz set_bit(HCI_INIT, &hdev->flags);
49561bbf4023SLuiz Augusto von Dentz
49571bbf4023SLuiz Augusto von Dentz ret = hci_dev_setup_sync(hdev);
49581bbf4023SLuiz Augusto von Dentz
49591bbf4023SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
49601bbf4023SLuiz Augusto von Dentz /* If public address change is configured, ensure that
49611bbf4023SLuiz Augusto von Dentz * the address gets programmed. If the driver does not
49621bbf4023SLuiz Augusto von Dentz * support changing the public address, fail the power
49631bbf4023SLuiz Augusto von Dentz * on procedure.
49641bbf4023SLuiz Augusto von Dentz */
49651bbf4023SLuiz Augusto von Dentz if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
49661bbf4023SLuiz Augusto von Dentz hdev->set_bdaddr)
49671bbf4023SLuiz Augusto von Dentz ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
49681bbf4023SLuiz Augusto von Dentz else
49691bbf4023SLuiz Augusto von Dentz ret = -EADDRNOTAVAIL;
49701bbf4023SLuiz Augusto von Dentz }
49711bbf4023SLuiz Augusto von Dentz
49721bbf4023SLuiz Augusto von Dentz if (!ret) {
49731bbf4023SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
49741bbf4023SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
49751bbf4023SLuiz Augusto von Dentz ret = hci_init_sync(hdev);
49761bbf4023SLuiz Augusto von Dentz if (!ret && hdev->post_init)
49771bbf4023SLuiz Augusto von Dentz ret = hdev->post_init(hdev);
49781bbf4023SLuiz Augusto von Dentz }
49791bbf4023SLuiz Augusto von Dentz }
49801bbf4023SLuiz Augusto von Dentz
49811bbf4023SLuiz Augusto von Dentz /* If the HCI Reset command is clearing all diagnostic settings,
49821bbf4023SLuiz Augusto von Dentz * then they need to be reprogrammed after the init procedure
49831bbf4023SLuiz Augusto von Dentz * completed.
49841bbf4023SLuiz Augusto von Dentz */
49851bbf4023SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
49861bbf4023SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
49871bbf4023SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
49881bbf4023SLuiz Augusto von Dentz ret = hdev->set_diag(hdev, true);
49891bbf4023SLuiz Augusto von Dentz
49901bbf4023SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
49911bbf4023SLuiz Augusto von Dentz msft_do_open(hdev);
49921bbf4023SLuiz Augusto von Dentz aosp_do_open(hdev);
49931bbf4023SLuiz Augusto von Dentz }
49941bbf4023SLuiz Augusto von Dentz
49951bbf4023SLuiz Augusto von Dentz clear_bit(HCI_INIT, &hdev->flags);
49961bbf4023SLuiz Augusto von Dentz
49971bbf4023SLuiz Augusto von Dentz return ret;
49981bbf4023SLuiz Augusto von Dentz }
hci_dev_open_sync(struct hci_dev * hdev)49991bbf4023SLuiz Augusto von Dentz
50001bbf4023SLuiz Augusto von Dentz int hci_dev_open_sync(struct hci_dev *hdev)
50011bbf4023SLuiz Augusto von Dentz {
50021bbf4023SLuiz Augusto von Dentz int ret;
5003d0b13706SLuiz Augusto von Dentz
5004d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
5005d0b13706SLuiz Augusto von Dentz
5006d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
5007d0b13706SLuiz Augusto von Dentz ret = -ENODEV;
5008d0b13706SLuiz Augusto von Dentz goto done;
5009d0b13706SLuiz Augusto von Dentz }
5010d0b13706SLuiz Augusto von Dentz
5011d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
5012d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_CONFIG)) {
5013d0b13706SLuiz Augusto von Dentz /* Check for rfkill but allow the HCI setup stage to
5014d0b13706SLuiz Augusto von Dentz * proceed (which in itself doesn't cause any RF activity).
5015d0b13706SLuiz Augusto von Dentz */
5016d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
5017d0b13706SLuiz Augusto von Dentz ret = -ERFKILL;
5018d0b13706SLuiz Augusto von Dentz goto done;
5019d0b13706SLuiz Augusto von Dentz }
5020d0b13706SLuiz Augusto von Dentz
5021d0b13706SLuiz Augusto von Dentz /* Check for valid public address or a configured static
5022d0b13706SLuiz Augusto von Dentz * random address, but let the HCI setup proceed to
5023d0b13706SLuiz Augusto von Dentz * be able to determine if there is a public address
5024d0b13706SLuiz Augusto von Dentz * or not.
5025d0b13706SLuiz Augusto von Dentz *
5026d0b13706SLuiz Augusto von Dentz * In case of user channel usage, it is not important
5027d0b13706SLuiz Augusto von Dentz * if a public address or static random address is
5028d0b13706SLuiz Augusto von Dentz * available.
5029d0b13706SLuiz Augusto von Dentz */
5030d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5031d0b13706SLuiz Augusto von Dentz !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5032d0b13706SLuiz Augusto von Dentz !bacmp(&hdev->static_addr, BDADDR_ANY)) {
5033d0b13706SLuiz Augusto von Dentz ret = -EADDRNOTAVAIL;
5034d0b13706SLuiz Augusto von Dentz goto done;
5035d0b13706SLuiz Augusto von Dentz }
5036d0b13706SLuiz Augusto von Dentz }
5037d0b13706SLuiz Augusto von Dentz
5038d0b13706SLuiz Augusto von Dentz if (test_bit(HCI_UP, &hdev->flags)) {
5039d0b13706SLuiz Augusto von Dentz ret = -EALREADY;
5040d0b13706SLuiz Augusto von Dentz goto done;
5041d0b13706SLuiz Augusto von Dentz }
5042d0b13706SLuiz Augusto von Dentz
5043d0b13706SLuiz Augusto von Dentz if (hdev->open(hdev)) {
5044d0b13706SLuiz Augusto von Dentz ret = -EIO;
5045d0b13706SLuiz Augusto von Dentz goto done;
5046d0b13706SLuiz Augusto von Dentz }
5047d0b13706SLuiz Augusto von Dentz
50489695ef87SAbhishek Pandit-Subedi hci_devcd_reset(hdev);
50499695ef87SAbhishek Pandit-Subedi
5050d0b13706SLuiz Augusto von Dentz set_bit(HCI_RUNNING, &hdev->flags);
5051d0b13706SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_OPEN);
5052d0b13706SLuiz Augusto von Dentz
50531bbf4023SLuiz Augusto von Dentz ret = hci_dev_init_sync(hdev);
5054d0b13706SLuiz Augusto von Dentz if (!ret) {
5055d0b13706SLuiz Augusto von Dentz hci_dev_hold(hdev);
5056d0b13706SLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
5057d0b13706SLuiz Augusto von Dentz hci_adv_instances_set_rpa_expired(hdev, true);
5058d0b13706SLuiz Augusto von Dentz set_bit(HCI_UP, &hdev->flags);
5059d0b13706SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_UP);
5060d0b13706SLuiz Augusto von Dentz hci_leds_update_powered(hdev, true);
5061d0b13706SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
5062d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_CONFIG) &&
5063d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
5064d0b13706SLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
50655af2e235SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_MGMT)) {
5066d0b13706SLuiz Augusto von Dentz ret = hci_powered_update_sync(hdev);
50676abf0daeSLuiz Augusto von Dentz mgmt_power_on(hdev, ret);
5068d0b13706SLuiz Augusto von Dentz }
5069d0b13706SLuiz Augusto von Dentz } else {
5070d0b13706SLuiz Augusto von Dentz /* Init failed, cleanup */
5071d0b13706SLuiz Augusto von Dentz flush_work(&hdev->tx_work);
5072d0b13706SLuiz Augusto von Dentz
5073d0b13706SLuiz Augusto von Dentz /* Since hci_rx_work() is possible to awake new cmd_work
5074d0b13706SLuiz Augusto von Dentz * it should be flushed first to avoid unexpected call of
5075d0b13706SLuiz Augusto von Dentz * hci_cmd_work()
5076d0b13706SLuiz Augusto von Dentz */
5077d0b13706SLuiz Augusto von Dentz flush_work(&hdev->rx_work);
5078d0b13706SLuiz Augusto von Dentz flush_work(&hdev->cmd_work);
5079d0b13706SLuiz Augusto von Dentz
5080d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->cmd_q);
5081d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->rx_q);
5082d0b13706SLuiz Augusto von Dentz
5083d0b13706SLuiz Augusto von Dentz if (hdev->flush)
5084d0b13706SLuiz Augusto von Dentz hdev->flush(hdev);
5085d0b13706SLuiz Augusto von Dentz
5086d0b13706SLuiz Augusto von Dentz if (hdev->sent_cmd) {
508797dfaf07SArchie Pusaka cancel_delayed_work_sync(&hdev->cmd_timer);
5088d0b13706SLuiz Augusto von Dentz kfree_skb(hdev->sent_cmd);
5089d0b13706SLuiz Augusto von Dentz hdev->sent_cmd = NULL;
5090d0b13706SLuiz Augusto von Dentz }
5091d0b13706SLuiz Augusto von Dentz
50922af7aa66SLuiz Augusto von Dentz if (hdev->req_skb) {
50932af7aa66SLuiz Augusto von Dentz kfree_skb(hdev->req_skb);
50942af7aa66SLuiz Augusto von Dentz hdev->req_skb = NULL;
50952af7aa66SLuiz Augusto von Dentz }
50962af7aa66SLuiz Augusto von Dentz
5097d0b13706SLuiz Augusto von Dentz clear_bit(HCI_RUNNING, &hdev->flags);
5098d0b13706SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5099d0b13706SLuiz Augusto von Dentz
5100d0b13706SLuiz Augusto von Dentz hdev->close(hdev);
5101d0b13706SLuiz Augusto von Dentz hdev->flags &= BIT(HCI_RAW);
5102d0b13706SLuiz Augusto von Dentz }
5103d0b13706SLuiz Augusto von Dentz
5104d0b13706SLuiz Augusto von Dentz done:
5105d0b13706SLuiz Augusto von Dentz return ret;
5106d0b13706SLuiz Augusto von Dentz }
5107d0b13706SLuiz Augusto von Dentz
hci_pend_le_actions_clear(struct hci_dev * hdev)5108d0b13706SLuiz Augusto von Dentz /* This function requires the caller holds hdev->lock */
5109d0b13706SLuiz Augusto von Dentz static void hci_pend_le_actions_clear(struct hci_dev *hdev)
5110d0b13706SLuiz Augusto von Dentz {
5111d0b13706SLuiz Augusto von Dentz struct hci_conn_params *p;
5112d0b13706SLuiz Augusto von Dentz
5113d0b13706SLuiz Augusto von Dentz list_for_each_entry(p, &hdev->le_conn_params, list) {
5114195ef75eSPauli Virtanen hci_pend_le_list_del_init(p);
5115d0b13706SLuiz Augusto von Dentz if (p->conn) {
5116d0b13706SLuiz Augusto von Dentz hci_conn_drop(p->conn);
5117d0b13706SLuiz Augusto von Dentz hci_conn_put(p->conn);
5118d0b13706SLuiz Augusto von Dentz p->conn = NULL;
5119d0b13706SLuiz Augusto von Dentz }
5120d0b13706SLuiz Augusto von Dentz }
5121d0b13706SLuiz Augusto von Dentz
5122d0b13706SLuiz Augusto von Dentz BT_DBG("All LE pending actions cleared");
5123d0b13706SLuiz Augusto von Dentz }
hci_dev_shutdown(struct hci_dev * hdev)5124d0b13706SLuiz Augusto von Dentz
51258dbc3e75SAbhishek Pandit-Subedi static int hci_dev_shutdown(struct hci_dev *hdev)
51268dbc3e75SAbhishek Pandit-Subedi {
51278dbc3e75SAbhishek Pandit-Subedi int err = 0;
51288dbc3e75SAbhishek Pandit-Subedi /* Similar to how we first do setup and then set the exclusive access
51298dbc3e75SAbhishek Pandit-Subedi * bit for userspace, we must first unset userchannel and then clean up.
51308dbc3e75SAbhishek Pandit-Subedi * Otherwise, the kernel can't properly use the hci channel to clean up
51318dbc3e75SAbhishek Pandit-Subedi * the controller (some shutdown routines require sending additional
51328dbc3e75SAbhishek Pandit-Subedi * commands to the controller for example).
51338dbc3e75SAbhishek Pandit-Subedi */
51348dbc3e75SAbhishek Pandit-Subedi bool was_userchannel =
51358dbc3e75SAbhishek Pandit-Subedi hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
51368dbc3e75SAbhishek Pandit-Subedi
51378dbc3e75SAbhishek Pandit-Subedi if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
51388dbc3e75SAbhishek Pandit-Subedi test_bit(HCI_UP, &hdev->flags)) {
51398dbc3e75SAbhishek Pandit-Subedi /* Execute vendor specific shutdown routine */
51408dbc3e75SAbhishek Pandit-Subedi if (hdev->shutdown)
51418dbc3e75SAbhishek Pandit-Subedi err = hdev->shutdown(hdev);
51428dbc3e75SAbhishek Pandit-Subedi }
51438dbc3e75SAbhishek Pandit-Subedi
51448dbc3e75SAbhishek Pandit-Subedi if (was_userchannel)
51458dbc3e75SAbhishek Pandit-Subedi hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
51468dbc3e75SAbhishek Pandit-Subedi
51478dbc3e75SAbhishek Pandit-Subedi return err;
51488dbc3e75SAbhishek Pandit-Subedi }
hci_dev_close_sync(struct hci_dev * hdev)51498dbc3e75SAbhishek Pandit-Subedi
5150d0b13706SLuiz Augusto von Dentz int hci_dev_close_sync(struct hci_dev *hdev)
5151d0b13706SLuiz Augusto von Dentz {
5152d0b13706SLuiz Augusto von Dentz bool auto_off;
5153d0b13706SLuiz Augusto von Dentz int err = 0;
5154d0b13706SLuiz Augusto von Dentz
5155d0b13706SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
5156d0b13706SLuiz Augusto von Dentz
5157d0b13706SLuiz Augusto von Dentz cancel_delayed_work(&hdev->power_off);
5158d0b13706SLuiz Augusto von Dentz cancel_delayed_work(&hdev->ncmd_timer);
51598ffde2a7SBrian Gix cancel_delayed_work(&hdev->le_scan_disable);
516027d54b77SBrian Gix cancel_delayed_work(&hdev->le_scan_restart);
5161d0b13706SLuiz Augusto von Dentz
5162d0b13706SLuiz Augusto von Dentz hci_request_cancel_all(hdev);
5163d0b13706SLuiz Augusto von Dentz
5164c249ea9bSBrian Gix if (hdev->adv_instance_timeout) {
5165c249ea9bSBrian Gix cancel_delayed_work_sync(&hdev->adv_instance_expire);
5166c249ea9bSBrian Gix hdev->adv_instance_timeout = 0;
5167c249ea9bSBrian Gix }
5168c249ea9bSBrian Gix
51698dbc3e75SAbhishek Pandit-Subedi err = hci_dev_shutdown(hdev);
5170d0b13706SLuiz Augusto von Dentz
5171d0b13706SLuiz Augusto von Dentz if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
5172d0b13706SLuiz Augusto von Dentz cancel_delayed_work_sync(&hdev->cmd_timer);
5173d0b13706SLuiz Augusto von Dentz return err;
5174d0b13706SLuiz Augusto von Dentz }
5175d0b13706SLuiz Augusto von Dentz
5176d0b13706SLuiz Augusto von Dentz hci_leds_update_powered(hdev, false);
5177d0b13706SLuiz Augusto von Dentz
5178d0b13706SLuiz Augusto von Dentz /* Flush RX and TX works */
5179d0b13706SLuiz Augusto von Dentz flush_work(&hdev->tx_work);
5180d0b13706SLuiz Augusto von Dentz flush_work(&hdev->rx_work);
5181d0b13706SLuiz Augusto von Dentz
5182d0b13706SLuiz Augusto von Dentz if (hdev->discov_timeout > 0) {
5183d0b13706SLuiz Augusto von Dentz hdev->discov_timeout = 0;
5184d0b13706SLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5185d0b13706SLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
5186d0b13706SLuiz Augusto von Dentz }
5187d0b13706SLuiz Augusto von Dentz
5188d0b13706SLuiz Augusto von Dentz if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
5189d0b13706SLuiz Augusto von Dentz cancel_delayed_work(&hdev->service_cache);
5190d0b13706SLuiz Augusto von Dentz
5191d0b13706SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_MGMT)) {
5192d0b13706SLuiz Augusto von Dentz struct adv_info *adv_instance;
5193d0b13706SLuiz Augusto von Dentz
5194d0b13706SLuiz Augusto von Dentz cancel_delayed_work_sync(&hdev->rpa_expired);
5195d0b13706SLuiz Augusto von Dentz
5196d0b13706SLuiz Augusto von Dentz list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5197d0b13706SLuiz Augusto von Dentz cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
5198d0b13706SLuiz Augusto von Dentz }
5199d0b13706SLuiz Augusto von Dentz
5200d0b13706SLuiz Augusto von Dentz /* Avoid potential lockdep warnings from the *_flush() calls by
5201d0b13706SLuiz Augusto von Dentz * ensuring the workqueue is empty up front.
5202d0b13706SLuiz Augusto von Dentz */
5203d0b13706SLuiz Augusto von Dentz drain_workqueue(hdev->workqueue);
5204d0b13706SLuiz Augusto von Dentz
5205d0b13706SLuiz Augusto von Dentz hci_dev_lock(hdev);
5206d0b13706SLuiz Augusto von Dentz
5207d0b13706SLuiz Augusto von Dentz hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5208d0b13706SLuiz Augusto von Dentz
5209d0b13706SLuiz Augusto von Dentz auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5210d0b13706SLuiz Augusto von Dentz
52115af2e235SLuiz Augusto von Dentz if (!auto_off && !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5212d0b13706SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_MGMT))
5213d0b13706SLuiz Augusto von Dentz __mgmt_power_off(hdev);
5214d0b13706SLuiz Augusto von Dentz
5215d0b13706SLuiz Augusto von Dentz hci_inquiry_cache_flush(hdev);
5216d0b13706SLuiz Augusto von Dentz hci_pend_le_actions_clear(hdev);
5217d0b13706SLuiz Augusto von Dentz hci_conn_hash_flush(hdev);
5218fa78d2d1SLin Ma /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5219d0b13706SLuiz Augusto von Dentz smp_unregister(hdev);
5220fa78d2d1SLin Ma hci_dev_unlock(hdev);
5221d0b13706SLuiz Augusto von Dentz
5222d0b13706SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5223d0b13706SLuiz Augusto von Dentz
5224385315deSJesse Melhuish if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5225d0b13706SLuiz Augusto von Dentz aosp_do_close(hdev);
5226d0b13706SLuiz Augusto von Dentz msft_do_close(hdev);
5227385315deSJesse Melhuish }
5228d0b13706SLuiz Augusto von Dentz
5229d0b13706SLuiz Augusto von Dentz if (hdev->flush)
5230d0b13706SLuiz Augusto von Dentz hdev->flush(hdev);
5231d0b13706SLuiz Augusto von Dentz
5232d0b13706SLuiz Augusto von Dentz /* Reset device */
5233d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->cmd_q);
5234d0b13706SLuiz Augusto von Dentz atomic_set(&hdev->cmd_cnt, 1);
5235d0b13706SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5236d0b13706SLuiz Augusto von Dentz !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5237d0b13706SLuiz Augusto von Dentz set_bit(HCI_INIT, &hdev->flags);
5238d0b13706SLuiz Augusto von Dentz hci_reset_sync(hdev);
5239d0b13706SLuiz Augusto von Dentz clear_bit(HCI_INIT, &hdev->flags);
5240d0b13706SLuiz Augusto von Dentz }
5241d0b13706SLuiz Augusto von Dentz
5242d0b13706SLuiz Augusto von Dentz /* flush cmd work */
5243d0b13706SLuiz Augusto von Dentz flush_work(&hdev->cmd_work);
5244d0b13706SLuiz Augusto von Dentz
5245d0b13706SLuiz Augusto von Dentz /* Drop queues */
5246d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->rx_q);
5247d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->cmd_q);
5248d0b13706SLuiz Augusto von Dentz skb_queue_purge(&hdev->raw_q);
5249d0b13706SLuiz Augusto von Dentz
5250d0b13706SLuiz Augusto von Dentz /* Drop last sent command */
5251d0b13706SLuiz Augusto von Dentz if (hdev->sent_cmd) {
5252d0b13706SLuiz Augusto von Dentz cancel_delayed_work_sync(&hdev->cmd_timer);
5253d0b13706SLuiz Augusto von Dentz kfree_skb(hdev->sent_cmd);
5254d0b13706SLuiz Augusto von Dentz hdev->sent_cmd = NULL;
5255d0b13706SLuiz Augusto von Dentz }
5256d0b13706SLuiz Augusto von Dentz
52572af7aa66SLuiz Augusto von Dentz /* Drop last request */
52582af7aa66SLuiz Augusto von Dentz if (hdev->req_skb) {
52592af7aa66SLuiz Augusto von Dentz kfree_skb(hdev->req_skb);
52602af7aa66SLuiz Augusto von Dentz hdev->req_skb = NULL;
52612af7aa66SLuiz Augusto von Dentz }
52622af7aa66SLuiz Augusto von Dentz
5263d0b13706SLuiz Augusto von Dentz clear_bit(HCI_RUNNING, &hdev->flags);
5264d0b13706SLuiz Augusto von Dentz hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5265d0b13706SLuiz Augusto von Dentz
5266d0b13706SLuiz Augusto von Dentz /* After this point our queues are empty and no tasks are scheduled. */
5267d0b13706SLuiz Augusto von Dentz hdev->close(hdev);
5268d0b13706SLuiz Augusto von Dentz
5269d0b13706SLuiz Augusto von Dentz /* Clear flags */
5270d0b13706SLuiz Augusto von Dentz hdev->flags &= BIT(HCI_RAW);
5271d0b13706SLuiz Augusto von Dentz hci_dev_clear_volatile_flags(hdev);
5272d0b13706SLuiz Augusto von Dentz
5273d0b13706SLuiz Augusto von Dentz memset(hdev->eir, 0, sizeof(hdev->eir));
5274d0b13706SLuiz Augusto von Dentz memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5275d0b13706SLuiz Augusto von Dentz bacpy(&hdev->random_addr, BDADDR_ANY);
5276b938790eSLuiz Augusto von Dentz hci_codec_list_clear(&hdev->local_codecs);
5277d0b13706SLuiz Augusto von Dentz
5278d0b13706SLuiz Augusto von Dentz hci_dev_put(hdev);
5279d0b13706SLuiz Augusto von Dentz return err;
5280d0b13706SLuiz Augusto von Dentz }
5281d0b13706SLuiz Augusto von Dentz
5282cf75ad8bSLuiz Augusto von Dentz /* This function perform power on HCI command sequence as follows:
5283cf75ad8bSLuiz Augusto von Dentz *
5284cf75ad8bSLuiz Augusto von Dentz * If controller is already up (HCI_UP) performs hci_powered_update_sync
5285cf75ad8bSLuiz Augusto von Dentz * sequence otherwise run hci_dev_open_sync which will follow with
5286cf75ad8bSLuiz Augusto von Dentz * hci_powered_update_sync after the init sequence is completed.
hci_power_on_sync(struct hci_dev * hdev)5287cf75ad8bSLuiz Augusto von Dentz */
5288cf75ad8bSLuiz Augusto von Dentz static int hci_power_on_sync(struct hci_dev *hdev)
5289cf75ad8bSLuiz Augusto von Dentz {
5290cf75ad8bSLuiz Augusto von Dentz int err;
5291cf75ad8bSLuiz Augusto von Dentz
5292cf75ad8bSLuiz Augusto von Dentz if (test_bit(HCI_UP, &hdev->flags) &&
5293cf75ad8bSLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_MGMT) &&
5294cf75ad8bSLuiz Augusto von Dentz hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5295cf75ad8bSLuiz Augusto von Dentz cancel_delayed_work(&hdev->power_off);
5296cf75ad8bSLuiz Augusto von Dentz return hci_powered_update_sync(hdev);
5297cf75ad8bSLuiz Augusto von Dentz }
5298cf75ad8bSLuiz Augusto von Dentz
5299cf75ad8bSLuiz Augusto von Dentz err = hci_dev_open_sync(hdev);
5300cf75ad8bSLuiz Augusto von Dentz if (err < 0)
5301cf75ad8bSLuiz Augusto von Dentz return err;
5302cf75ad8bSLuiz Augusto von Dentz
5303cf75ad8bSLuiz Augusto von Dentz /* During the HCI setup phase, a few error conditions are
5304cf75ad8bSLuiz Augusto von Dentz * ignored and they need to be checked now. If they are still
5305cf75ad8bSLuiz Augusto von Dentz * valid, it is important to return the device back off.
5306cf75ad8bSLuiz Augusto von Dentz */
5307cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5308cf75ad8bSLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
53095af2e235SLuiz Augusto von Dentz (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5310cf75ad8bSLuiz Augusto von Dentz !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5311cf75ad8bSLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5312cf75ad8bSLuiz Augusto von Dentz hci_dev_close_sync(hdev);
5313cf75ad8bSLuiz Augusto von Dentz } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5314cf75ad8bSLuiz Augusto von Dentz queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5315cf75ad8bSLuiz Augusto von Dentz HCI_AUTO_OFF_TIMEOUT);
5316cf75ad8bSLuiz Augusto von Dentz }
5317cf75ad8bSLuiz Augusto von Dentz
5318cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5319cf75ad8bSLuiz Augusto von Dentz /* For unconfigured devices, set the HCI_RAW flag
5320cf75ad8bSLuiz Augusto von Dentz * so that userspace can easily identify them.
5321cf75ad8bSLuiz Augusto von Dentz */
5322cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5323cf75ad8bSLuiz Augusto von Dentz set_bit(HCI_RAW, &hdev->flags);
5324cf75ad8bSLuiz Augusto von Dentz
5325cf75ad8bSLuiz Augusto von Dentz /* For fully configured devices, this will send
5326cf75ad8bSLuiz Augusto von Dentz * the Index Added event. For unconfigured devices,
5327cf75ad8bSLuiz Augusto von Dentz * it will send Unconfigued Index Added event.
5328cf75ad8bSLuiz Augusto von Dentz *
5329cf75ad8bSLuiz Augusto von Dentz * Devices with HCI_QUIRK_RAW_DEVICE are ignored
5330cf75ad8bSLuiz Augusto von Dentz * and no event will be send.
5331cf75ad8bSLuiz Augusto von Dentz */
5332cf75ad8bSLuiz Augusto von Dentz mgmt_index_added(hdev);
5333cf75ad8bSLuiz Augusto von Dentz } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5334cf75ad8bSLuiz Augusto von Dentz /* When the controller is now configured, then it
5335cf75ad8bSLuiz Augusto von Dentz * is important to clear the HCI_RAW flag.
5336cf75ad8bSLuiz Augusto von Dentz */
5337cf75ad8bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5338cf75ad8bSLuiz Augusto von Dentz clear_bit(HCI_RAW, &hdev->flags);
5339cf75ad8bSLuiz Augusto von Dentz
5340cf75ad8bSLuiz Augusto von Dentz /* Powering on the controller with HCI_CONFIG set only
5341cf75ad8bSLuiz Augusto von Dentz * happens with the transition from unconfigured to
5342cf75ad8bSLuiz Augusto von Dentz * configured. This will send the Index Added event.
5343cf75ad8bSLuiz Augusto von Dentz */
5344cf75ad8bSLuiz Augusto von Dentz mgmt_index_added(hdev);
5345cf75ad8bSLuiz Augusto von Dentz }
5346cf75ad8bSLuiz Augusto von Dentz
5347cf75ad8bSLuiz Augusto von Dentz return 0;
5348cf75ad8bSLuiz Augusto von Dentz }
hci_remote_name_cancel_sync(struct hci_dev * hdev,bdaddr_t * addr)5349cf75ad8bSLuiz Augusto von Dentz
5350cf75ad8bSLuiz Augusto von Dentz static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5351cf75ad8bSLuiz Augusto von Dentz {
5352cf75ad8bSLuiz Augusto von Dentz struct hci_cp_remote_name_req_cancel cp;
5353cf75ad8bSLuiz Augusto von Dentz
5354cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
5355cf75ad8bSLuiz Augusto von Dentz bacpy(&cp.bdaddr, addr);
5356cf75ad8bSLuiz Augusto von Dentz
5357cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5358cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5359cf75ad8bSLuiz Augusto von Dentz }
hci_stop_discovery_sync(struct hci_dev * hdev)5360cf75ad8bSLuiz Augusto von Dentz
5361abfeea47SLuiz Augusto von Dentz int hci_stop_discovery_sync(struct hci_dev *hdev)
5362cf75ad8bSLuiz Augusto von Dentz {
5363cf75ad8bSLuiz Augusto von Dentz struct discovery_state *d = &hdev->discovery;
5364cf75ad8bSLuiz Augusto von Dentz struct inquiry_entry *e;
5365cf75ad8bSLuiz Augusto von Dentz int err;
5366cf75ad8bSLuiz Augusto von Dentz
5367cf75ad8bSLuiz Augusto von Dentz bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5368cf75ad8bSLuiz Augusto von Dentz
5369cf75ad8bSLuiz Augusto von Dentz if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
5370cf75ad8bSLuiz Augusto von Dentz if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5371cf75ad8bSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5372cf75ad8bSLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
5373cf75ad8bSLuiz Augusto von Dentz if (err)
5374cf75ad8bSLuiz Augusto von Dentz return err;
5375cf75ad8bSLuiz Augusto von Dentz }
5376cf75ad8bSLuiz Augusto von Dentz
5377cf75ad8bSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5378cf75ad8bSLuiz Augusto von Dentz cancel_delayed_work(&hdev->le_scan_disable);
5379cf75ad8bSLuiz Augusto von Dentz cancel_delayed_work(&hdev->le_scan_restart);
5380cf75ad8bSLuiz Augusto von Dentz
5381cf75ad8bSLuiz Augusto von Dentz err = hci_scan_disable_sync(hdev);
5382cf75ad8bSLuiz Augusto von Dentz if (err)
5383cf75ad8bSLuiz Augusto von Dentz return err;
5384cf75ad8bSLuiz Augusto von Dentz }
5385cf75ad8bSLuiz Augusto von Dentz
5386cf75ad8bSLuiz Augusto von Dentz } else {
5387cf75ad8bSLuiz Augusto von Dentz err = hci_scan_disable_sync(hdev);
5388cf75ad8bSLuiz Augusto von Dentz if (err)
5389cf75ad8bSLuiz Augusto von Dentz return err;
5390cf75ad8bSLuiz Augusto von Dentz }
5391cf75ad8bSLuiz Augusto von Dentz
5392abfeea47SLuiz Augusto von Dentz /* Resume advertising if it was paused */
5393abfeea47SLuiz Augusto von Dentz if (use_ll_privacy(hdev))
5394abfeea47SLuiz Augusto von Dentz hci_resume_advertising_sync(hdev);
5395abfeea47SLuiz Augusto von Dentz
5396cf75ad8bSLuiz Augusto von Dentz /* No further actions needed for LE-only discovery */
5397cf75ad8bSLuiz Augusto von Dentz if (d->type == DISCOV_TYPE_LE)
5398cf75ad8bSLuiz Augusto von Dentz return 0;
5399cf75ad8bSLuiz Augusto von Dentz
5400cf75ad8bSLuiz Augusto von Dentz if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5401cf75ad8bSLuiz Augusto von Dentz e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5402cf75ad8bSLuiz Augusto von Dentz NAME_PENDING);
5403cf75ad8bSLuiz Augusto von Dentz if (!e)
5404cf75ad8bSLuiz Augusto von Dentz return 0;
5405cf75ad8bSLuiz Augusto von Dentz
54067eebbddeSLuiz Augusto von Dentz /* Ignore cancel errors since it should interfere with stopping
54077eebbddeSLuiz Augusto von Dentz * of the discovery.
54087eebbddeSLuiz Augusto von Dentz */
54097eebbddeSLuiz Augusto von Dentz hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5410cf75ad8bSLuiz Augusto von Dentz }
5411cf75ad8bSLuiz Augusto von Dentz
5412cf75ad8bSLuiz Augusto von Dentz return 0;
5413cf75ad8bSLuiz Augusto von Dentz }
hci_disconnect_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5414cf75ad8bSLuiz Augusto von Dentz
5415cf75ad8bSLuiz Augusto von Dentz static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5416cf75ad8bSLuiz Augusto von Dentz u8 reason)
5417cf75ad8bSLuiz Augusto von Dentz {
5418cf75ad8bSLuiz Augusto von Dentz struct hci_cp_disconnect cp;
5419cf75ad8bSLuiz Augusto von Dentz
5420f03d3322SIulia Tanasescu if (test_bit(HCI_CONN_BIG_CREATED, &conn->flags)) {
5421f03d3322SIulia Tanasescu /* This is a BIS connection, hci_conn_del will
5422f03d3322SIulia Tanasescu * do the necessary cleanup.
5423f03d3322SIulia Tanasescu */
5424f03d3322SIulia Tanasescu hci_dev_lock(hdev);
5425f03d3322SIulia Tanasescu hci_conn_failed(conn, reason);
5426f03d3322SIulia Tanasescu hci_dev_unlock(hdev);
5427f03d3322SIulia Tanasescu
5428f03d3322SIulia Tanasescu return 0;
5429f03d3322SIulia Tanasescu }
5430f03d3322SIulia Tanasescu
5431cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
5432cf75ad8bSLuiz Augusto von Dentz cp.handle = cpu_to_le16(conn->handle);
5433cf75ad8bSLuiz Augusto von Dentz cp.reason = reason;
5434cf75ad8bSLuiz Augusto von Dentz
5435f5d13029SArchie Pusaka /* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5436f5d13029SArchie Pusaka * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5437f5d13029SArchie Pusaka * used when suspending or powering off, where we don't want to wait
5438f5d13029SArchie Pusaka * for the peer's response.
5439d0b13706SLuiz Augusto von Dentz */
5440f5d13029SArchie Pusaka if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5441d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5442d0b13706SLuiz Augusto von Dentz sizeof(cp), &cp,
5443d0b13706SLuiz Augusto von Dentz HCI_EV_DISCONN_COMPLETE,
5444d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT, NULL);
5445d0b13706SLuiz Augusto von Dentz
5446d0b13706SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5447d0b13706SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
5448cf75ad8bSLuiz Augusto von Dentz }
hci_le_connect_cancel_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5449cf75ad8bSLuiz Augusto von Dentz
5450cf75ad8bSLuiz Augusto von Dentz static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5451a13f316eSLuiz Augusto von Dentz struct hci_conn *conn, u8 reason)
5452cf75ad8bSLuiz Augusto von Dentz {
5453a13f316eSLuiz Augusto von Dentz /* Return reason if scanning since the connection shall probably be
5454a13f316eSLuiz Augusto von Dentz * cleanup directly.
5455a13f316eSLuiz Augusto von Dentz */
5456cf75ad8bSLuiz Augusto von Dentz if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5457a13f316eSLuiz Augusto von Dentz return reason;
5458cf75ad8bSLuiz Augusto von Dentz
5459a13f316eSLuiz Augusto von Dentz if (conn->role == HCI_ROLE_SLAVE ||
5460a13f316eSLuiz Augusto von Dentz test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
5461b62e7220SLuiz Augusto von Dentz return 0;
5462b62e7220SLuiz Augusto von Dentz
5463cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5464b62e7220SLuiz Augusto von Dentz 0, NULL, HCI_CMD_TIMEOUT);
5465cf75ad8bSLuiz Augusto von Dentz }
hci_connect_cancel_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5466cf75ad8bSLuiz Augusto von Dentz
5467a13f316eSLuiz Augusto von Dentz static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5468a13f316eSLuiz Augusto von Dentz u8 reason)
5469cf75ad8bSLuiz Augusto von Dentz {
5470cf75ad8bSLuiz Augusto von Dentz if (conn->type == LE_LINK)
5471a13f316eSLuiz Augusto von Dentz return hci_le_connect_cancel_sync(hdev, conn, reason);
5472cf75ad8bSLuiz Augusto von Dentz
547304a51d61SLuiz Augusto von Dentz if (conn->type == ISO_LINK) {
547404a51d61SLuiz Augusto von Dentz /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
547504a51d61SLuiz Augusto von Dentz * page 1857:
547604a51d61SLuiz Augusto von Dentz *
547704a51d61SLuiz Augusto von Dentz * If this command is issued for a CIS on the Central and the
547804a51d61SLuiz Augusto von Dentz * CIS is successfully terminated before being established,
547904a51d61SLuiz Augusto von Dentz * then an HCI_LE_CIS_Established event shall also be sent for
548004a51d61SLuiz Augusto von Dentz * this CIS with the Status Operation Cancelled by Host (0x44).
548104a51d61SLuiz Augusto von Dentz */
548204a51d61SLuiz Augusto von Dentz if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
548304a51d61SLuiz Augusto von Dentz return hci_disconnect_sync(hdev, conn, reason);
548404a51d61SLuiz Augusto von Dentz
54852889bdd0SPauli Virtanen /* CIS with no Create CIS sent have nothing to cancel */
54862889bdd0SPauli Virtanen if (bacmp(&conn->dst, BDADDR_ANY))
54872889bdd0SPauli Virtanen return HCI_ERROR_LOCAL_HOST_TERM;
54882889bdd0SPauli Virtanen
548904a51d61SLuiz Augusto von Dentz /* There is no way to cancel a BIS without terminating the BIG
549004a51d61SLuiz Augusto von Dentz * which is done later on connection cleanup.
549104a51d61SLuiz Augusto von Dentz */
549204a51d61SLuiz Augusto von Dentz return 0;
549304a51d61SLuiz Augusto von Dentz }
549404a51d61SLuiz Augusto von Dentz
5495cf75ad8bSLuiz Augusto von Dentz if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5496cf75ad8bSLuiz Augusto von Dentz return 0;
5497cf75ad8bSLuiz Augusto von Dentz
5498094e3639SLuiz Augusto von Dentz /* Wait for HCI_EV_CONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5499094e3639SLuiz Augusto von Dentz * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5500094e3639SLuiz Augusto von Dentz * used when suspending or powering off, where we don't want to wait
5501094e3639SLuiz Augusto von Dentz * for the peer's response.
5502094e3639SLuiz Augusto von Dentz */
5503094e3639SLuiz Augusto von Dentz if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5504094e3639SLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5505094e3639SLuiz Augusto von Dentz 6, &conn->dst,
5506094e3639SLuiz Augusto von Dentz HCI_EV_CONN_COMPLETE,
5507094e3639SLuiz Augusto von Dentz HCI_CMD_TIMEOUT, NULL);
5508094e3639SLuiz Augusto von Dentz
5509cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5510cf75ad8bSLuiz Augusto von Dentz 6, &conn->dst, HCI_CMD_TIMEOUT);
5511cf75ad8bSLuiz Augusto von Dentz }
hci_reject_sco_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5512cf75ad8bSLuiz Augusto von Dentz
5513cf75ad8bSLuiz Augusto von Dentz static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5514cf75ad8bSLuiz Augusto von Dentz u8 reason)
5515cf75ad8bSLuiz Augusto von Dentz {
5516cf75ad8bSLuiz Augusto von Dentz struct hci_cp_reject_sync_conn_req cp;
5517cf75ad8bSLuiz Augusto von Dentz
5518cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
5519cf75ad8bSLuiz Augusto von Dentz bacpy(&cp.bdaddr, &conn->dst);
5520cf75ad8bSLuiz Augusto von Dentz cp.reason = reason;
5521cf75ad8bSLuiz Augusto von Dentz
5522cf75ad8bSLuiz Augusto von Dentz /* SCO rejection has its own limited set of
5523cf75ad8bSLuiz Augusto von Dentz * allowed error values (0x0D-0x0F).
5524cf75ad8bSLuiz Augusto von Dentz */
5525cf75ad8bSLuiz Augusto von Dentz if (reason < 0x0d || reason > 0x0f)
5526cf75ad8bSLuiz Augusto von Dentz cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5527cf75ad8bSLuiz Augusto von Dentz
5528cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5529cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5530cf75ad8bSLuiz Augusto von Dentz }
hci_le_reject_cis_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5531cf75ad8bSLuiz Augusto von Dentz
553204a51d61SLuiz Augusto von Dentz static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
553304a51d61SLuiz Augusto von Dentz u8 reason)
553404a51d61SLuiz Augusto von Dentz {
553504a51d61SLuiz Augusto von Dentz struct hci_cp_le_reject_cis cp;
553604a51d61SLuiz Augusto von Dentz
553704a51d61SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
553804a51d61SLuiz Augusto von Dentz cp.handle = cpu_to_le16(conn->handle);
553904a51d61SLuiz Augusto von Dentz cp.reason = reason;
554004a51d61SLuiz Augusto von Dentz
554104a51d61SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
554204a51d61SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
554304a51d61SLuiz Augusto von Dentz }
hci_reject_conn_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)554404a51d61SLuiz Augusto von Dentz
5545cf75ad8bSLuiz Augusto von Dentz static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5546cf75ad8bSLuiz Augusto von Dentz u8 reason)
5547cf75ad8bSLuiz Augusto von Dentz {
5548cf75ad8bSLuiz Augusto von Dentz struct hci_cp_reject_conn_req cp;
5549cf75ad8bSLuiz Augusto von Dentz
555004a51d61SLuiz Augusto von Dentz if (conn->type == ISO_LINK)
555104a51d61SLuiz Augusto von Dentz return hci_le_reject_cis_sync(hdev, conn, reason);
555204a51d61SLuiz Augusto von Dentz
5553cf75ad8bSLuiz Augusto von Dentz if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5554cf75ad8bSLuiz Augusto von Dentz return hci_reject_sco_sync(hdev, conn, reason);
5555cf75ad8bSLuiz Augusto von Dentz
5556cf75ad8bSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
5557cf75ad8bSLuiz Augusto von Dentz bacpy(&cp.bdaddr, &conn->dst);
5558cf75ad8bSLuiz Augusto von Dentz cp.reason = reason;
5559cf75ad8bSLuiz Augusto von Dentz
5560cf75ad8bSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5561cf75ad8bSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5562cf75ad8bSLuiz Augusto von Dentz }
hci_abort_conn_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5563cf75ad8bSLuiz Augusto von Dentz
55641f7435c8SLuiz Augusto von Dentz int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5565cf75ad8bSLuiz Augusto von Dentz {
55665af1f84eSLuiz Augusto von Dentz int err = 0;
55675af1f84eSLuiz Augusto von Dentz u16 handle = conn->handle;
5568a239110eSPauli Virtanen bool disconnect = false;
556994d9ba9fSLuiz Augusto von Dentz struct hci_conn *c;
55709b3628d7SLuiz Augusto von Dentz
5571cf75ad8bSLuiz Augusto von Dentz switch (conn->state) {
5572cf75ad8bSLuiz Augusto von Dentz case BT_CONNECTED:
5573cf75ad8bSLuiz Augusto von Dentz case BT_CONFIG:
55745af1f84eSLuiz Augusto von Dentz err = hci_disconnect_sync(hdev, conn, reason);
55755af1f84eSLuiz Augusto von Dentz break;
5576cf75ad8bSLuiz Augusto von Dentz case BT_CONNECT:
5577a13f316eSLuiz Augusto von Dentz err = hci_connect_cancel_sync(hdev, conn, reason);
55785af1f84eSLuiz Augusto von Dentz break;
5579cf75ad8bSLuiz Augusto von Dentz case BT_CONNECT2:
55805af1f84eSLuiz Augusto von Dentz err = hci_reject_conn_sync(hdev, conn, reason);
55815af1f84eSLuiz Augusto von Dentz break;
5582f777d882SIulia Tanasescu case BT_OPEN:
55832889bdd0SPauli Virtanen case BT_BOUND:
5584a239110eSPauli Virtanen break;
5585cf75ad8bSLuiz Augusto von Dentz default:
5586a239110eSPauli Virtanen disconnect = true;
5587a239110eSPauli Virtanen break;
5588cf75ad8bSLuiz Augusto von Dentz }
5589cf75ad8bSLuiz Augusto von Dentz
559094d9ba9fSLuiz Augusto von Dentz hci_dev_lock(hdev);
559194d9ba9fSLuiz Augusto von Dentz
5592a239110eSPauli Virtanen /* Check if the connection has been cleaned up concurrently */
559394d9ba9fSLuiz Augusto von Dentz c = hci_conn_hash_lookup_handle(hdev, handle);
559494d9ba9fSLuiz Augusto von Dentz if (!c || c != conn) {
559594d9ba9fSLuiz Augusto von Dentz err = 0;
559694d9ba9fSLuiz Augusto von Dentz goto unlock;
559794d9ba9fSLuiz Augusto von Dentz }
559894d9ba9fSLuiz Augusto von Dentz
55995af1f84eSLuiz Augusto von Dentz /* Cleanup hci_conn object if it cannot be cancelled as it
56005af1f84eSLuiz Augusto von Dentz * likelly means the controller and host stack are out of sync
56015af1f84eSLuiz Augusto von Dentz * or in case of LE it was still scanning so it can be cleanup
56025af1f84eSLuiz Augusto von Dentz * safely.
56035af1f84eSLuiz Augusto von Dentz */
5604a239110eSPauli Virtanen if (disconnect) {
5605a239110eSPauli Virtanen conn->state = BT_CLOSED;
5606a239110eSPauli Virtanen hci_disconn_cfm(conn, reason);
5607a239110eSPauli Virtanen hci_conn_del(conn);
5608a239110eSPauli Virtanen } else {
560994d9ba9fSLuiz Augusto von Dentz hci_conn_failed(conn, reason);
5610a239110eSPauli Virtanen }
56115af1f84eSLuiz Augusto von Dentz
561294d9ba9fSLuiz Augusto von Dentz unlock:
56135af1f84eSLuiz Augusto von Dentz hci_dev_unlock(hdev);
56145af1f84eSLuiz Augusto von Dentz return err;
5615cf75ad8bSLuiz Augusto von Dentz }
hci_disconnect_all_sync(struct hci_dev * hdev,u8 reason)5616cf75ad8bSLuiz Augusto von Dentz
5617182ee45dSLuiz Augusto von Dentz static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5618182ee45dSLuiz Augusto von Dentz {
561994d9ba9fSLuiz Augusto von Dentz struct list_head *head = &hdev->conn_hash.list;
562094d9ba9fSLuiz Augusto von Dentz struct hci_conn *conn;
5621182ee45dSLuiz Augusto von Dentz
562294d9ba9fSLuiz Augusto von Dentz rcu_read_lock();
562394d9ba9fSLuiz Augusto von Dentz while ((conn = list_first_or_null_rcu(head, struct hci_conn, list))) {
562494d9ba9fSLuiz Augusto von Dentz /* Make sure the connection is not freed while unlocking */
562594d9ba9fSLuiz Augusto von Dentz conn = hci_conn_get(conn);
562694d9ba9fSLuiz Augusto von Dentz rcu_read_unlock();
562794d9ba9fSLuiz Augusto von Dentz /* Disregard possible errors since hci_conn_del shall have been
562894d9ba9fSLuiz Augusto von Dentz * called even in case of errors had occurred since it would
562994d9ba9fSLuiz Augusto von Dentz * then cause hci_conn_failed to be called which calls
563094d9ba9fSLuiz Augusto von Dentz * hci_conn_del internally.
563194d9ba9fSLuiz Augusto von Dentz */
563294d9ba9fSLuiz Augusto von Dentz hci_abort_conn_sync(hdev, conn, reason);
563394d9ba9fSLuiz Augusto von Dentz hci_conn_put(conn);
563494d9ba9fSLuiz Augusto von Dentz rcu_read_lock();
5635182ee45dSLuiz Augusto von Dentz }
563694d9ba9fSLuiz Augusto von Dentz rcu_read_unlock();
5637182ee45dSLuiz Augusto von Dentz
56388cd3c55cSTom Rix return 0;
5639182ee45dSLuiz Augusto von Dentz }
5640182ee45dSLuiz Augusto von Dentz
5641cf75ad8bSLuiz Augusto von Dentz /* This function perform power off HCI command sequence as follows:
5642cf75ad8bSLuiz Augusto von Dentz *
5643cf75ad8bSLuiz Augusto von Dentz * Clear Advertising
5644cf75ad8bSLuiz Augusto von Dentz * Stop Discovery
5645cf75ad8bSLuiz Augusto von Dentz * Disconnect all connections
5646cf75ad8bSLuiz Augusto von Dentz * hci_dev_close_sync
hci_power_off_sync(struct hci_dev * hdev)5647cf75ad8bSLuiz Augusto von Dentz */
5648cf75ad8bSLuiz Augusto von Dentz static int hci_power_off_sync(struct hci_dev *hdev)
5649cf75ad8bSLuiz Augusto von Dentz {
5650cf75ad8bSLuiz Augusto von Dentz int err;
5651cf75ad8bSLuiz Augusto von Dentz
5652cf75ad8bSLuiz Augusto von Dentz /* If controller is already down there is nothing to do */
5653cf75ad8bSLuiz Augusto von Dentz if (!test_bit(HCI_UP, &hdev->flags))
5654cf75ad8bSLuiz Augusto von Dentz return 0;
5655cf75ad8bSLuiz Augusto von Dentz
5656cf75ad8bSLuiz Augusto von Dentz if (test_bit(HCI_ISCAN, &hdev->flags) ||
5657cf75ad8bSLuiz Augusto von Dentz test_bit(HCI_PSCAN, &hdev->flags)) {
5658cf75ad8bSLuiz Augusto von Dentz err = hci_write_scan_enable_sync(hdev, 0x00);
5659cf75ad8bSLuiz Augusto von Dentz if (err)
5660cf75ad8bSLuiz Augusto von Dentz return err;
5661cf75ad8bSLuiz Augusto von Dentz }
5662cf75ad8bSLuiz Augusto von Dentz
5663cf75ad8bSLuiz Augusto von Dentz err = hci_clear_adv_sync(hdev, NULL, false);
5664cf75ad8bSLuiz Augusto von Dentz if (err)
5665cf75ad8bSLuiz Augusto von Dentz return err;
5666cf75ad8bSLuiz Augusto von Dentz
5667cf75ad8bSLuiz Augusto von Dentz err = hci_stop_discovery_sync(hdev);
5668cf75ad8bSLuiz Augusto von Dentz if (err)
5669cf75ad8bSLuiz Augusto von Dentz return err;
5670cf75ad8bSLuiz Augusto von Dentz
5671182ee45dSLuiz Augusto von Dentz /* Terminated due to Power Off */
5672182ee45dSLuiz Augusto von Dentz err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5673182ee45dSLuiz Augusto von Dentz if (err)
5674182ee45dSLuiz Augusto von Dentz return err;
5675cf75ad8bSLuiz Augusto von Dentz
5676cf75ad8bSLuiz Augusto von Dentz return hci_dev_close_sync(hdev);
5677cf75ad8bSLuiz Augusto von Dentz }
hci_set_powered_sync(struct hci_dev * hdev,u8 val)5678cf75ad8bSLuiz Augusto von Dentz
5679cf75ad8bSLuiz Augusto von Dentz int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5680cf75ad8bSLuiz Augusto von Dentz {
5681cf75ad8bSLuiz Augusto von Dentz if (val)
5682cf75ad8bSLuiz Augusto von Dentz return hci_power_on_sync(hdev);
5683cf75ad8bSLuiz Augusto von Dentz
5684cf75ad8bSLuiz Augusto von Dentz return hci_power_off_sync(hdev);
5685cf75ad8bSLuiz Augusto von Dentz }
hci_write_iac_sync(struct hci_dev * hdev)5686abfeea47SLuiz Augusto von Dentz
56872bd1b237SLuiz Augusto von Dentz static int hci_write_iac_sync(struct hci_dev *hdev)
56882bd1b237SLuiz Augusto von Dentz {
56892bd1b237SLuiz Augusto von Dentz struct hci_cp_write_current_iac_lap cp;
56902bd1b237SLuiz Augusto von Dentz
56912bd1b237SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
56922bd1b237SLuiz Augusto von Dentz return 0;
56932bd1b237SLuiz Augusto von Dentz
56942bd1b237SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
56952bd1b237SLuiz Augusto von Dentz
56962bd1b237SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
56972bd1b237SLuiz Augusto von Dentz /* Limited discoverable mode */
56982bd1b237SLuiz Augusto von Dentz cp.num_iac = min_t(u8, hdev->num_iac, 2);
56992bd1b237SLuiz Augusto von Dentz cp.iac_lap[0] = 0x00; /* LIAC */
57002bd1b237SLuiz Augusto von Dentz cp.iac_lap[1] = 0x8b;
57012bd1b237SLuiz Augusto von Dentz cp.iac_lap[2] = 0x9e;
57022bd1b237SLuiz Augusto von Dentz cp.iac_lap[3] = 0x33; /* GIAC */
57032bd1b237SLuiz Augusto von Dentz cp.iac_lap[4] = 0x8b;
57042bd1b237SLuiz Augusto von Dentz cp.iac_lap[5] = 0x9e;
57052bd1b237SLuiz Augusto von Dentz } else {
57062bd1b237SLuiz Augusto von Dentz /* General discoverable mode */
57072bd1b237SLuiz Augusto von Dentz cp.num_iac = 1;
57082bd1b237SLuiz Augusto von Dentz cp.iac_lap[0] = 0x33; /* GIAC */
57092bd1b237SLuiz Augusto von Dentz cp.iac_lap[1] = 0x8b;
57102bd1b237SLuiz Augusto von Dentz cp.iac_lap[2] = 0x9e;
57112bd1b237SLuiz Augusto von Dentz }
57122bd1b237SLuiz Augusto von Dentz
57132bd1b237SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
57142bd1b237SLuiz Augusto von Dentz (cp.num_iac * 3) + 1, &cp,
57152bd1b237SLuiz Augusto von Dentz HCI_CMD_TIMEOUT);
57162bd1b237SLuiz Augusto von Dentz }
hci_update_discoverable_sync(struct hci_dev * hdev)57172bd1b237SLuiz Augusto von Dentz
57182bd1b237SLuiz Augusto von Dentz int hci_update_discoverable_sync(struct hci_dev *hdev)
57192bd1b237SLuiz Augusto von Dentz {
57202bd1b237SLuiz Augusto von Dentz int err = 0;
57212bd1b237SLuiz Augusto von Dentz
57222bd1b237SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
57232bd1b237SLuiz Augusto von Dentz err = hci_write_iac_sync(hdev);
57242bd1b237SLuiz Augusto von Dentz if (err)
57252bd1b237SLuiz Augusto von Dentz return err;
57262bd1b237SLuiz Augusto von Dentz
57272bd1b237SLuiz Augusto von Dentz err = hci_update_scan_sync(hdev);
57282bd1b237SLuiz Augusto von Dentz if (err)
57292bd1b237SLuiz Augusto von Dentz return err;
57302bd1b237SLuiz Augusto von Dentz
57312bd1b237SLuiz Augusto von Dentz err = hci_update_class_sync(hdev);
57322bd1b237SLuiz Augusto von Dentz if (err)
57332bd1b237SLuiz Augusto von Dentz return err;
57342bd1b237SLuiz Augusto von Dentz }
57352bd1b237SLuiz Augusto von Dentz
57362bd1b237SLuiz Augusto von Dentz /* Advertising instances don't use the global discoverable setting, so
57372bd1b237SLuiz Augusto von Dentz * only update AD if advertising was enabled using Set Advertising.
57382bd1b237SLuiz Augusto von Dentz */
57392bd1b237SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
57402bd1b237SLuiz Augusto von Dentz err = hci_update_adv_data_sync(hdev, 0x00);
57412bd1b237SLuiz Augusto von Dentz if (err)
57422bd1b237SLuiz Augusto von Dentz return err;
57432bd1b237SLuiz Augusto von Dentz
57442bd1b237SLuiz Augusto von Dentz /* Discoverable mode affects the local advertising
57452bd1b237SLuiz Augusto von Dentz * address in limited privacy mode.
57462bd1b237SLuiz Augusto von Dentz */
57472bd1b237SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
57482bd1b237SLuiz Augusto von Dentz if (ext_adv_capable(hdev))
57492bd1b237SLuiz Augusto von Dentz err = hci_start_ext_adv_sync(hdev, 0x00);
57502bd1b237SLuiz Augusto von Dentz else
57512bd1b237SLuiz Augusto von Dentz err = hci_enable_advertising_sync(hdev);
57522bd1b237SLuiz Augusto von Dentz }
57532bd1b237SLuiz Augusto von Dentz }
57542bd1b237SLuiz Augusto von Dentz
57552bd1b237SLuiz Augusto von Dentz return err;
57562bd1b237SLuiz Augusto von Dentz }
update_discoverable_sync(struct hci_dev * hdev,void * data)57572bd1b237SLuiz Augusto von Dentz
57582bd1b237SLuiz Augusto von Dentz static int update_discoverable_sync(struct hci_dev *hdev, void *data)
57592bd1b237SLuiz Augusto von Dentz {
57602bd1b237SLuiz Augusto von Dentz return hci_update_discoverable_sync(hdev);
57612bd1b237SLuiz Augusto von Dentz }
hci_update_discoverable(struct hci_dev * hdev)57622bd1b237SLuiz Augusto von Dentz
57632bd1b237SLuiz Augusto von Dentz int hci_update_discoverable(struct hci_dev *hdev)
57642bd1b237SLuiz Augusto von Dentz {
57652bd1b237SLuiz Augusto von Dentz /* Only queue if it would have any effect */
57662bd1b237SLuiz Augusto von Dentz if (hdev_is_powered(hdev) &&
57672bd1b237SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
57682bd1b237SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
57692bd1b237SLuiz Augusto von Dentz hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
57702bd1b237SLuiz Augusto von Dentz return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
57712bd1b237SLuiz Augusto von Dentz NULL);
57722bd1b237SLuiz Augusto von Dentz
57732bd1b237SLuiz Augusto von Dentz return 0;
57742bd1b237SLuiz Augusto von Dentz }
hci_update_connectable_sync(struct hci_dev * hdev)57752bd1b237SLuiz Augusto von Dentz
5776f056a657SLuiz Augusto von Dentz int hci_update_connectable_sync(struct hci_dev *hdev)
5777f056a657SLuiz Augusto von Dentz {
5778f056a657SLuiz Augusto von Dentz int err;
5779f056a657SLuiz Augusto von Dentz
5780f056a657SLuiz Augusto von Dentz err = hci_update_scan_sync(hdev);
5781f056a657SLuiz Augusto von Dentz if (err)
5782f056a657SLuiz Augusto von Dentz return err;
5783f056a657SLuiz Augusto von Dentz
5784f056a657SLuiz Augusto von Dentz /* If BR/EDR is not enabled and we disable advertising as a
5785f056a657SLuiz Augusto von Dentz * by-product of disabling connectable, we need to update the
5786f056a657SLuiz Augusto von Dentz * advertising flags.
5787f056a657SLuiz Augusto von Dentz */
5788f056a657SLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5789f056a657SLuiz Augusto von Dentz err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5790f056a657SLuiz Augusto von Dentz
5791f056a657SLuiz Augusto von Dentz /* Update the advertising parameters if necessary */
5792f056a657SLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5793f056a657SLuiz Augusto von Dentz !list_empty(&hdev->adv_instances)) {
5794f056a657SLuiz Augusto von Dentz if (ext_adv_capable(hdev))
5795f056a657SLuiz Augusto von Dentz err = hci_start_ext_adv_sync(hdev,
5796f056a657SLuiz Augusto von Dentz hdev->cur_adv_instance);
5797f056a657SLuiz Augusto von Dentz else
5798f056a657SLuiz Augusto von Dentz err = hci_enable_advertising_sync(hdev);
5799f056a657SLuiz Augusto von Dentz
5800f056a657SLuiz Augusto von Dentz if (err)
5801f056a657SLuiz Augusto von Dentz return err;
5802f056a657SLuiz Augusto von Dentz }
5803f056a657SLuiz Augusto von Dentz
5804f056a657SLuiz Augusto von Dentz return hci_update_passive_scan_sync(hdev);
5805f056a657SLuiz Augusto von Dentz }
hci_inquiry_sync(struct hci_dev * hdev,u8 length)5806f056a657SLuiz Augusto von Dentz
5807abfeea47SLuiz Augusto von Dentz static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5808abfeea47SLuiz Augusto von Dentz {
5809abfeea47SLuiz Augusto von Dentz const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5810abfeea47SLuiz Augusto von Dentz const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5811abfeea47SLuiz Augusto von Dentz struct hci_cp_inquiry cp;
5812abfeea47SLuiz Augusto von Dentz
5813abfeea47SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
5814abfeea47SLuiz Augusto von Dentz
58150a07fa1fSJonas Dreßler if (test_bit(HCI_INQUIRY, &hdev->flags))
5816abfeea47SLuiz Augusto von Dentz return 0;
5817abfeea47SLuiz Augusto von Dentz
5818abfeea47SLuiz Augusto von Dentz hci_dev_lock(hdev);
5819abfeea47SLuiz Augusto von Dentz hci_inquiry_cache_flush(hdev);
5820abfeea47SLuiz Augusto von Dentz hci_dev_unlock(hdev);
5821abfeea47SLuiz Augusto von Dentz
5822abfeea47SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
5823abfeea47SLuiz Augusto von Dentz
5824abfeea47SLuiz Augusto von Dentz if (hdev->discovery.limited)
5825abfeea47SLuiz Augusto von Dentz memcpy(&cp.lap, liac, sizeof(cp.lap));
5826abfeea47SLuiz Augusto von Dentz else
5827abfeea47SLuiz Augusto von Dentz memcpy(&cp.lap, giac, sizeof(cp.lap));
5828abfeea47SLuiz Augusto von Dentz
5829abfeea47SLuiz Augusto von Dentz cp.length = length;
5830abfeea47SLuiz Augusto von Dentz
5831abfeea47SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5832abfeea47SLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5833abfeea47SLuiz Augusto von Dentz }
hci_active_scan_sync(struct hci_dev * hdev,uint16_t interval)5834abfeea47SLuiz Augusto von Dentz
5835abfeea47SLuiz Augusto von Dentz static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5836abfeea47SLuiz Augusto von Dentz {
5837abfeea47SLuiz Augusto von Dentz u8 own_addr_type;
5838abfeea47SLuiz Augusto von Dentz /* Accept list is not used for discovery */
5839abfeea47SLuiz Augusto von Dentz u8 filter_policy = 0x00;
5840abfeea47SLuiz Augusto von Dentz /* Default is to enable duplicates filter */
5841abfeea47SLuiz Augusto von Dentz u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5842abfeea47SLuiz Augusto von Dentz int err;
5843abfeea47SLuiz Augusto von Dentz
5844abfeea47SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
5845abfeea47SLuiz Augusto von Dentz
5846abfeea47SLuiz Augusto von Dentz /* If controller is scanning, it means the passive scanning is
5847abfeea47SLuiz Augusto von Dentz * running. Thus, we should temporarily stop it in order to set the
5848abfeea47SLuiz Augusto von Dentz * discovery scanning parameters.
5849abfeea47SLuiz Augusto von Dentz */
5850abfeea47SLuiz Augusto von Dentz err = hci_scan_disable_sync(hdev);
5851abfeea47SLuiz Augusto von Dentz if (err) {
5852abfeea47SLuiz Augusto von Dentz bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5853abfeea47SLuiz Augusto von Dentz return err;
5854abfeea47SLuiz Augusto von Dentz }
5855abfeea47SLuiz Augusto von Dentz
5856abfeea47SLuiz Augusto von Dentz cancel_interleave_scan(hdev);
5857abfeea47SLuiz Augusto von Dentz
58583c44a431SZhengping Jiang /* Pause address resolution for active scan and stop advertising if
58593c44a431SZhengping Jiang * privacy is enabled.
5860abfeea47SLuiz Augusto von Dentz */
58613c44a431SZhengping Jiang err = hci_pause_addr_resolution(hdev);
58623c44a431SZhengping Jiang if (err)
5863abfeea47SLuiz Augusto von Dentz goto failed;
5864abfeea47SLuiz Augusto von Dentz
5865abfeea47SLuiz Augusto von Dentz /* All active scans will be done with either a resolvable private
5866abfeea47SLuiz Augusto von Dentz * address (when privacy feature has been enabled) or non-resolvable
5867abfeea47SLuiz Augusto von Dentz * private address.
5868abfeea47SLuiz Augusto von Dentz */
5869abfeea47SLuiz Augusto von Dentz err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5870abfeea47SLuiz Augusto von Dentz &own_addr_type);
5871abfeea47SLuiz Augusto von Dentz if (err < 0)
5872abfeea47SLuiz Augusto von Dentz own_addr_type = ADDR_LE_DEV_PUBLIC;
5873abfeea47SLuiz Augusto von Dentz
5874abfeea47SLuiz Augusto von Dentz if (hci_is_adv_monitoring(hdev)) {
5875abfeea47SLuiz Augusto von Dentz /* Duplicate filter should be disabled when some advertisement
5876abfeea47SLuiz Augusto von Dentz * monitor is activated, otherwise AdvMon can only receive one
5877abfeea47SLuiz Augusto von Dentz * advertisement for one peer(*) during active scanning, and
5878abfeea47SLuiz Augusto von Dentz * might report loss to these peers.
5879abfeea47SLuiz Augusto von Dentz *
5880abfeea47SLuiz Augusto von Dentz * Note that different controllers have different meanings of
5881abfeea47SLuiz Augusto von Dentz * |duplicate|. Some of them consider packets with the same
5882abfeea47SLuiz Augusto von Dentz * address as duplicate, and others consider packets with the
5883abfeea47SLuiz Augusto von Dentz * same address and the same RSSI as duplicate. Although in the
5884abfeea47SLuiz Augusto von Dentz * latter case we don't need to disable duplicate filter, but
5885abfeea47SLuiz Augusto von Dentz * it is common to have active scanning for a short period of
5886abfeea47SLuiz Augusto von Dentz * time, the power impact should be neglectable.
5887abfeea47SLuiz Augusto von Dentz */
5888abfeea47SLuiz Augusto von Dentz filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5889abfeea47SLuiz Augusto von Dentz }
5890abfeea47SLuiz Augusto von Dentz
5891abfeea47SLuiz Augusto von Dentz err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5892abfeea47SLuiz Augusto von Dentz hdev->le_scan_window_discovery,
5893abfeea47SLuiz Augusto von Dentz own_addr_type, filter_policy, filter_dup);
5894abfeea47SLuiz Augusto von Dentz if (!err)
5895abfeea47SLuiz Augusto von Dentz return err;
5896abfeea47SLuiz Augusto von Dentz
5897abfeea47SLuiz Augusto von Dentz failed:
5898abfeea47SLuiz Augusto von Dentz /* Resume advertising if it was paused */
5899abfeea47SLuiz Augusto von Dentz if (use_ll_privacy(hdev))
5900abfeea47SLuiz Augusto von Dentz hci_resume_advertising_sync(hdev);
5901abfeea47SLuiz Augusto von Dentz
5902abfeea47SLuiz Augusto von Dentz /* Resume passive scanning */
5903abfeea47SLuiz Augusto von Dentz hci_update_passive_scan_sync(hdev);
5904abfeea47SLuiz Augusto von Dentz return err;
5905abfeea47SLuiz Augusto von Dentz }
hci_start_interleaved_discovery_sync(struct hci_dev * hdev)5906abfeea47SLuiz Augusto von Dentz
5907abfeea47SLuiz Augusto von Dentz static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5908abfeea47SLuiz Augusto von Dentz {
5909abfeea47SLuiz Augusto von Dentz int err;
5910abfeea47SLuiz Augusto von Dentz
5911abfeea47SLuiz Augusto von Dentz bt_dev_dbg(hdev, "");
5912abfeea47SLuiz Augusto von Dentz
5913abfeea47SLuiz Augusto von Dentz err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5914abfeea47SLuiz Augusto von Dentz if (err)
5915abfeea47SLuiz Augusto von Dentz return err;
5916abfeea47SLuiz Augusto von Dentz
5917abfeea47SLuiz Augusto von Dentz return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5918abfeea47SLuiz Augusto von Dentz }
hci_start_discovery_sync(struct hci_dev * hdev)5919abfeea47SLuiz Augusto von Dentz
5920abfeea47SLuiz Augusto von Dentz int hci_start_discovery_sync(struct hci_dev *hdev)
5921abfeea47SLuiz Augusto von Dentz {
5922abfeea47SLuiz Augusto von Dentz unsigned long timeout;
5923abfeea47SLuiz Augusto von Dentz int err;
5924abfeea47SLuiz Augusto von Dentz
5925abfeea47SLuiz Augusto von Dentz bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5926abfeea47SLuiz Augusto von Dentz
5927abfeea47SLuiz Augusto von Dentz switch (hdev->discovery.type) {
5928abfeea47SLuiz Augusto von Dentz case DISCOV_TYPE_BREDR:
5929abfeea47SLuiz Augusto von Dentz return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5930abfeea47SLuiz Augusto von Dentz case DISCOV_TYPE_INTERLEAVED:
5931abfeea47SLuiz Augusto von Dentz /* When running simultaneous discovery, the LE scanning time
5932abfeea47SLuiz Augusto von Dentz * should occupy the whole discovery time sine BR/EDR inquiry
5933abfeea47SLuiz Augusto von Dentz * and LE scanning are scheduled by the controller.
5934abfeea47SLuiz Augusto von Dentz *
5935abfeea47SLuiz Augusto von Dentz * For interleaving discovery in comparison, BR/EDR inquiry
5936abfeea47SLuiz Augusto von Dentz * and LE scanning are done sequentially with separate
5937abfeea47SLuiz Augusto von Dentz * timeouts.
5938abfeea47SLuiz Augusto von Dentz */
5939abfeea47SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5940abfeea47SLuiz Augusto von Dentz &hdev->quirks)) {
5941abfeea47SLuiz Augusto von Dentz timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5942abfeea47SLuiz Augusto von Dentz /* During simultaneous discovery, we double LE scan
5943abfeea47SLuiz Augusto von Dentz * interval. We must leave some time for the controller
5944abfeea47SLuiz Augusto von Dentz * to do BR/EDR inquiry.
5945abfeea47SLuiz Augusto von Dentz */
5946abfeea47SLuiz Augusto von Dentz err = hci_start_interleaved_discovery_sync(hdev);
5947abfeea47SLuiz Augusto von Dentz break;
5948abfeea47SLuiz Augusto von Dentz }
5949abfeea47SLuiz Augusto von Dentz
5950abfeea47SLuiz Augusto von Dentz timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5951abfeea47SLuiz Augusto von Dentz err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5952abfeea47SLuiz Augusto von Dentz break;
5953abfeea47SLuiz Augusto von Dentz case DISCOV_TYPE_LE:
5954abfeea47SLuiz Augusto von Dentz timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5955abfeea47SLuiz Augusto von Dentz err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5956abfeea47SLuiz Augusto von Dentz break;
5957abfeea47SLuiz Augusto von Dentz default:
5958abfeea47SLuiz Augusto von Dentz return -EINVAL;
5959abfeea47SLuiz Augusto von Dentz }
5960abfeea47SLuiz Augusto von Dentz
5961abfeea47SLuiz Augusto von Dentz if (err)
5962abfeea47SLuiz Augusto von Dentz return err;
5963abfeea47SLuiz Augusto von Dentz
5964abfeea47SLuiz Augusto von Dentz bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5965abfeea47SLuiz Augusto von Dentz
5966abfeea47SLuiz Augusto von Dentz /* When service discovery is used and the controller has a
5967abfeea47SLuiz Augusto von Dentz * strict duplicate filter, it is important to remember the
5968abfeea47SLuiz Augusto von Dentz * start and duration of the scan. This is required for
5969abfeea47SLuiz Augusto von Dentz * restarting scanning during the discovery phase.
5970abfeea47SLuiz Augusto von Dentz */
5971abfeea47SLuiz Augusto von Dentz if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5972abfeea47SLuiz Augusto von Dentz hdev->discovery.result_filtering) {
5973abfeea47SLuiz Augusto von Dentz hdev->discovery.scan_start = jiffies;
5974abfeea47SLuiz Augusto von Dentz hdev->discovery.scan_duration = timeout;
5975abfeea47SLuiz Augusto von Dentz }
5976abfeea47SLuiz Augusto von Dentz
5977abfeea47SLuiz Augusto von Dentz queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5978abfeea47SLuiz Augusto von Dentz timeout);
5979abfeea47SLuiz Augusto von Dentz return 0;
5980abfeea47SLuiz Augusto von Dentz }
hci_suspend_monitor_sync(struct hci_dev * hdev)5981182ee45dSLuiz Augusto von Dentz
5982182ee45dSLuiz Augusto von Dentz static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5983182ee45dSLuiz Augusto von Dentz {
5984182ee45dSLuiz Augusto von Dentz switch (hci_get_adv_monitor_offload_ext(hdev)) {
5985182ee45dSLuiz Augusto von Dentz case HCI_ADV_MONITOR_EXT_MSFT:
5986182ee45dSLuiz Augusto von Dentz msft_suspend_sync(hdev);
5987182ee45dSLuiz Augusto von Dentz break;
5988182ee45dSLuiz Augusto von Dentz default:
5989182ee45dSLuiz Augusto von Dentz return;
5990182ee45dSLuiz Augusto von Dentz }
5991182ee45dSLuiz Augusto von Dentz }
5992182ee45dSLuiz Augusto von Dentz
hci_pause_discovery_sync(struct hci_dev * hdev)5993182ee45dSLuiz Augusto von Dentz /* This function disables discovery and mark it as paused */
5994182ee45dSLuiz Augusto von Dentz static int hci_pause_discovery_sync(struct hci_dev *hdev)
5995182ee45dSLuiz Augusto von Dentz {
5996182ee45dSLuiz Augusto von Dentz int old_state = hdev->discovery.state;
5997182ee45dSLuiz Augusto von Dentz int err;
5998182ee45dSLuiz Augusto von Dentz
5999182ee45dSLuiz Augusto von Dentz /* If discovery already stopped/stopping/paused there nothing to do */
6000182ee45dSLuiz Augusto von Dentz if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
6001182ee45dSLuiz Augusto von Dentz hdev->discovery_paused)
6002182ee45dSLuiz Augusto von Dentz return 0;
6003182ee45dSLuiz Augusto von Dentz
6004182ee45dSLuiz Augusto von Dentz hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
6005182ee45dSLuiz Augusto von Dentz err = hci_stop_discovery_sync(hdev);
6006182ee45dSLuiz Augusto von Dentz if (err)
6007182ee45dSLuiz Augusto von Dentz return err;
6008182ee45dSLuiz Augusto von Dentz
6009182ee45dSLuiz Augusto von Dentz hdev->discovery_paused = true;
6010182ee45dSLuiz Augusto von Dentz hdev->discovery_old_state = old_state;
6011182ee45dSLuiz Augusto von Dentz hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
6012182ee45dSLuiz Augusto von Dentz
6013182ee45dSLuiz Augusto von Dentz return 0;
6014182ee45dSLuiz Augusto von Dentz }
hci_update_event_filter_sync(struct hci_dev * hdev)6015182ee45dSLuiz Augusto von Dentz
6016182ee45dSLuiz Augusto von Dentz static int hci_update_event_filter_sync(struct hci_dev *hdev)
6017182ee45dSLuiz Augusto von Dentz {
6018182ee45dSLuiz Augusto von Dentz struct bdaddr_list_with_flags *b;
6019182ee45dSLuiz Augusto von Dentz u8 scan = SCAN_DISABLED;
6020182ee45dSLuiz Augusto von Dentz bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
6021182ee45dSLuiz Augusto von Dentz int err;
6022182ee45dSLuiz Augusto von Dentz
6023182ee45dSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
6024182ee45dSLuiz Augusto von Dentz return 0;
6025182ee45dSLuiz Augusto von Dentz
60260eaecfb2SIsmael Ferreras Morezuelas /* Some fake CSR controllers lock up after setting this type of
60270eaecfb2SIsmael Ferreras Morezuelas * filter, so avoid sending the request altogether.
60280eaecfb2SIsmael Ferreras Morezuelas */
60290eaecfb2SIsmael Ferreras Morezuelas if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
60300eaecfb2SIsmael Ferreras Morezuelas return 0;
60310eaecfb2SIsmael Ferreras Morezuelas
6032182ee45dSLuiz Augusto von Dentz /* Always clear event filter when starting */
6033182ee45dSLuiz Augusto von Dentz hci_clear_event_filter_sync(hdev);
6034182ee45dSLuiz Augusto von Dentz
6035182ee45dSLuiz Augusto von Dentz list_for_each_entry(b, &hdev->accept_list, list) {
6036e1cff700SLinus Torvalds if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
6037182ee45dSLuiz Augusto von Dentz continue;
6038182ee45dSLuiz Augusto von Dentz
6039182ee45dSLuiz Augusto von Dentz bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
6040182ee45dSLuiz Augusto von Dentz
6041182ee45dSLuiz Augusto von Dentz err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
6042182ee45dSLuiz Augusto von Dentz HCI_CONN_SETUP_ALLOW_BDADDR,
6043182ee45dSLuiz Augusto von Dentz &b->bdaddr,
6044182ee45dSLuiz Augusto von Dentz HCI_CONN_SETUP_AUTO_ON);
6045182ee45dSLuiz Augusto von Dentz if (err)
6046182ee45dSLuiz Augusto von Dentz bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
6047182ee45dSLuiz Augusto von Dentz &b->bdaddr);
6048182ee45dSLuiz Augusto von Dentz else
6049182ee45dSLuiz Augusto von Dentz scan = SCAN_PAGE;
6050182ee45dSLuiz Augusto von Dentz }
6051182ee45dSLuiz Augusto von Dentz
6052182ee45dSLuiz Augusto von Dentz if (scan && !scanning)
6053182ee45dSLuiz Augusto von Dentz hci_write_scan_enable_sync(hdev, scan);
6054182ee45dSLuiz Augusto von Dentz else if (!scan && scanning)
6055182ee45dSLuiz Augusto von Dentz hci_write_scan_enable_sync(hdev, scan);
6056182ee45dSLuiz Augusto von Dentz
6057182ee45dSLuiz Augusto von Dentz return 0;
6058182ee45dSLuiz Augusto von Dentz }
6059182ee45dSLuiz Augusto von Dentz
hci_pause_scan_sync(struct hci_dev * hdev)60603b420553SLuiz Augusto von Dentz /* This function disables scan (BR and LE) and mark it as paused */
60613b420553SLuiz Augusto von Dentz static int hci_pause_scan_sync(struct hci_dev *hdev)
60623b420553SLuiz Augusto von Dentz {
60633b420553SLuiz Augusto von Dentz if (hdev->scanning_paused)
60643b420553SLuiz Augusto von Dentz return 0;
60653b420553SLuiz Augusto von Dentz
60663b420553SLuiz Augusto von Dentz /* Disable page scan if enabled */
60673b420553SLuiz Augusto von Dentz if (test_bit(HCI_PSCAN, &hdev->flags))
60683b420553SLuiz Augusto von Dentz hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
60693b420553SLuiz Augusto von Dentz
60703b420553SLuiz Augusto von Dentz hci_scan_disable_sync(hdev);
60713b420553SLuiz Augusto von Dentz
60723b420553SLuiz Augusto von Dentz hdev->scanning_paused = true;
60733b420553SLuiz Augusto von Dentz
60743b420553SLuiz Augusto von Dentz return 0;
60753b420553SLuiz Augusto von Dentz }
60763b420553SLuiz Augusto von Dentz
6077182ee45dSLuiz Augusto von Dentz /* This function performs the HCI suspend procedures in the follow order:
6078182ee45dSLuiz Augusto von Dentz *
6079182ee45dSLuiz Augusto von Dentz * Pause discovery (active scanning/inquiry)
6080182ee45dSLuiz Augusto von Dentz * Pause Directed Advertising/Advertising
60813b420553SLuiz Augusto von Dentz * Pause Scanning (passive scanning in case discovery was not active)
6082182ee45dSLuiz Augusto von Dentz * Disconnect all connections
6083182ee45dSLuiz Augusto von Dentz * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
6084182ee45dSLuiz Augusto von Dentz * otherwise:
6085182ee45dSLuiz Augusto von Dentz * Update event mask (only set events that are allowed to wake up the host)
6086182ee45dSLuiz Augusto von Dentz * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
6087182ee45dSLuiz Augusto von Dentz * Update passive scanning (lower duty cycle)
6088182ee45dSLuiz Augusto von Dentz * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
hci_suspend_sync(struct hci_dev * hdev)6089182ee45dSLuiz Augusto von Dentz */
6090182ee45dSLuiz Augusto von Dentz int hci_suspend_sync(struct hci_dev *hdev)
6091182ee45dSLuiz Augusto von Dentz {
6092182ee45dSLuiz Augusto von Dentz int err;
6093182ee45dSLuiz Augusto von Dentz
6094182ee45dSLuiz Augusto von Dentz /* If marked as suspended there nothing to do */
6095182ee45dSLuiz Augusto von Dentz if (hdev->suspended)
6096182ee45dSLuiz Augusto von Dentz return 0;
6097182ee45dSLuiz Augusto von Dentz
6098182ee45dSLuiz Augusto von Dentz /* Mark device as suspended */
6099182ee45dSLuiz Augusto von Dentz hdev->suspended = true;
6100182ee45dSLuiz Augusto von Dentz
6101182ee45dSLuiz Augusto von Dentz /* Pause discovery if not already stopped */
6102182ee45dSLuiz Augusto von Dentz hci_pause_discovery_sync(hdev);
6103182ee45dSLuiz Augusto von Dentz
6104182ee45dSLuiz Augusto von Dentz /* Pause other advertisements */
6105182ee45dSLuiz Augusto von Dentz hci_pause_advertising_sync(hdev);
6106182ee45dSLuiz Augusto von Dentz
6107182ee45dSLuiz Augusto von Dentz /* Suspend monitor filters */
6108182ee45dSLuiz Augusto von Dentz hci_suspend_monitor_sync(hdev);
6109182ee45dSLuiz Augusto von Dentz
6110182ee45dSLuiz Augusto von Dentz /* Prevent disconnects from causing scanning to be re-enabled */
61113b420553SLuiz Augusto von Dentz hci_pause_scan_sync(hdev);
6112182ee45dSLuiz Augusto von Dentz
6113123f6d3aSLuiz Augusto von Dentz if (hci_conn_count(hdev)) {
6114182ee45dSLuiz Augusto von Dentz /* Soft disconnect everything (power off) */
6115182ee45dSLuiz Augusto von Dentz err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
6116182ee45dSLuiz Augusto von Dentz if (err) {
6117182ee45dSLuiz Augusto von Dentz /* Set state to BT_RUNNING so resume doesn't notify */
6118182ee45dSLuiz Augusto von Dentz hdev->suspend_state = BT_RUNNING;
6119182ee45dSLuiz Augusto von Dentz hci_resume_sync(hdev);
6120182ee45dSLuiz Augusto von Dentz return err;
6121182ee45dSLuiz Augusto von Dentz }
6122182ee45dSLuiz Augusto von Dentz
6123123f6d3aSLuiz Augusto von Dentz /* Update event mask so only the allowed event can wakeup the
6124123f6d3aSLuiz Augusto von Dentz * host.
6125123f6d3aSLuiz Augusto von Dentz */
6126ef61b6eaSAbhishek Pandit-Subedi hci_set_event_mask_sync(hdev);
6127123f6d3aSLuiz Augusto von Dentz }
6128ef61b6eaSAbhishek Pandit-Subedi
6129182ee45dSLuiz Augusto von Dentz /* Only configure accept list if disconnect succeeded and wake
6130182ee45dSLuiz Augusto von Dentz * isn't being prevented.
6131182ee45dSLuiz Augusto von Dentz */
6132182ee45dSLuiz Augusto von Dentz if (!hdev->wakeup || !hdev->wakeup(hdev)) {
6133182ee45dSLuiz Augusto von Dentz hdev->suspend_state = BT_SUSPEND_DISCONNECT;
6134182ee45dSLuiz Augusto von Dentz return 0;
6135182ee45dSLuiz Augusto von Dentz }
6136182ee45dSLuiz Augusto von Dentz
6137182ee45dSLuiz Augusto von Dentz /* Unpause to take care of updating scanning params */
6138182ee45dSLuiz Augusto von Dentz hdev->scanning_paused = false;
6139182ee45dSLuiz Augusto von Dentz
6140182ee45dSLuiz Augusto von Dentz /* Enable event filter for paired devices */
6141182ee45dSLuiz Augusto von Dentz hci_update_event_filter_sync(hdev);
6142182ee45dSLuiz Augusto von Dentz
6143182ee45dSLuiz Augusto von Dentz /* Update LE passive scan if enabled */
6144182ee45dSLuiz Augusto von Dentz hci_update_passive_scan_sync(hdev);
6145182ee45dSLuiz Augusto von Dentz
6146182ee45dSLuiz Augusto von Dentz /* Pause scan changes again. */
6147182ee45dSLuiz Augusto von Dentz hdev->scanning_paused = true;
6148182ee45dSLuiz Augusto von Dentz
6149182ee45dSLuiz Augusto von Dentz hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
6150182ee45dSLuiz Augusto von Dentz
6151182ee45dSLuiz Augusto von Dentz return 0;
6152182ee45dSLuiz Augusto von Dentz }
6153182ee45dSLuiz Augusto von Dentz
hci_resume_discovery_sync(struct hci_dev * hdev)6154182ee45dSLuiz Augusto von Dentz /* This function resumes discovery */
6155182ee45dSLuiz Augusto von Dentz static int hci_resume_discovery_sync(struct hci_dev *hdev)
6156182ee45dSLuiz Augusto von Dentz {
6157182ee45dSLuiz Augusto von Dentz int err;
6158182ee45dSLuiz Augusto von Dentz
6159182ee45dSLuiz Augusto von Dentz /* If discovery not paused there nothing to do */
6160182ee45dSLuiz Augusto von Dentz if (!hdev->discovery_paused)
6161182ee45dSLuiz Augusto von Dentz return 0;
6162182ee45dSLuiz Augusto von Dentz
6163182ee45dSLuiz Augusto von Dentz hdev->discovery_paused = false;
6164182ee45dSLuiz Augusto von Dentz
6165182ee45dSLuiz Augusto von Dentz hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6166182ee45dSLuiz Augusto von Dentz
6167182ee45dSLuiz Augusto von Dentz err = hci_start_discovery_sync(hdev);
6168182ee45dSLuiz Augusto von Dentz
6169182ee45dSLuiz Augusto von Dentz hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
6170182ee45dSLuiz Augusto von Dentz DISCOVERY_FINDING);
6171182ee45dSLuiz Augusto von Dentz
6172182ee45dSLuiz Augusto von Dentz return err;
6173182ee45dSLuiz Augusto von Dentz }
hci_resume_monitor_sync(struct hci_dev * hdev)6174182ee45dSLuiz Augusto von Dentz
6175182ee45dSLuiz Augusto von Dentz static void hci_resume_monitor_sync(struct hci_dev *hdev)
6176182ee45dSLuiz Augusto von Dentz {
6177182ee45dSLuiz Augusto von Dentz switch (hci_get_adv_monitor_offload_ext(hdev)) {
6178182ee45dSLuiz Augusto von Dentz case HCI_ADV_MONITOR_EXT_MSFT:
6179182ee45dSLuiz Augusto von Dentz msft_resume_sync(hdev);
6180182ee45dSLuiz Augusto von Dentz break;
6181182ee45dSLuiz Augusto von Dentz default:
6182182ee45dSLuiz Augusto von Dentz return;
6183182ee45dSLuiz Augusto von Dentz }
6184182ee45dSLuiz Augusto von Dentz }
6185182ee45dSLuiz Augusto von Dentz
hci_resume_scan_sync(struct hci_dev * hdev)61863b420553SLuiz Augusto von Dentz /* This function resume scan and reset paused flag */
61873b420553SLuiz Augusto von Dentz static int hci_resume_scan_sync(struct hci_dev *hdev)
61883b420553SLuiz Augusto von Dentz {
61893b420553SLuiz Augusto von Dentz if (!hdev->scanning_paused)
61903b420553SLuiz Augusto von Dentz return 0;
61913b420553SLuiz Augusto von Dentz
619268253f3cSZhengping Jiang hdev->scanning_paused = false;
619368253f3cSZhengping Jiang
61943b420553SLuiz Augusto von Dentz hci_update_scan_sync(hdev);
61953b420553SLuiz Augusto von Dentz
61963b420553SLuiz Augusto von Dentz /* Reset passive scanning to normal */
61973b420553SLuiz Augusto von Dentz hci_update_passive_scan_sync(hdev);
61983b420553SLuiz Augusto von Dentz
61993b420553SLuiz Augusto von Dentz return 0;
62003b420553SLuiz Augusto von Dentz }
62013b420553SLuiz Augusto von Dentz
6202182ee45dSLuiz Augusto von Dentz /* This function performs the HCI suspend procedures in the follow order:
6203182ee45dSLuiz Augusto von Dentz *
6204182ee45dSLuiz Augusto von Dentz * Restore event mask
6205182ee45dSLuiz Augusto von Dentz * Clear event filter
6206182ee45dSLuiz Augusto von Dentz * Update passive scanning (normal duty cycle)
6207182ee45dSLuiz Augusto von Dentz * Resume Directed Advertising/Advertising
6208182ee45dSLuiz Augusto von Dentz * Resume discovery (active scanning/inquiry)
hci_resume_sync(struct hci_dev * hdev)6209182ee45dSLuiz Augusto von Dentz */
6210182ee45dSLuiz Augusto von Dentz int hci_resume_sync(struct hci_dev *hdev)
6211182ee45dSLuiz Augusto von Dentz {
6212182ee45dSLuiz Augusto von Dentz /* If not marked as suspended there nothing to do */
6213182ee45dSLuiz Augusto von Dentz if (!hdev->suspended)
6214182ee45dSLuiz Augusto von Dentz return 0;
6215182ee45dSLuiz Augusto von Dentz
6216182ee45dSLuiz Augusto von Dentz hdev->suspended = false;
6217182ee45dSLuiz Augusto von Dentz
6218182ee45dSLuiz Augusto von Dentz /* Restore event mask */
6219182ee45dSLuiz Augusto von Dentz hci_set_event_mask_sync(hdev);
6220182ee45dSLuiz Augusto von Dentz
6221182ee45dSLuiz Augusto von Dentz /* Clear any event filters and restore scan state */
6222182ee45dSLuiz Augusto von Dentz hci_clear_event_filter_sync(hdev);
6223182ee45dSLuiz Augusto von Dentz
62243b420553SLuiz Augusto von Dentz /* Resume scanning */
62253b420553SLuiz Augusto von Dentz hci_resume_scan_sync(hdev);
6226182ee45dSLuiz Augusto von Dentz
6227182ee45dSLuiz Augusto von Dentz /* Resume monitor filters */
6228182ee45dSLuiz Augusto von Dentz hci_resume_monitor_sync(hdev);
6229182ee45dSLuiz Augusto von Dentz
6230182ee45dSLuiz Augusto von Dentz /* Resume other advertisements */
6231182ee45dSLuiz Augusto von Dentz hci_resume_advertising_sync(hdev);
6232182ee45dSLuiz Augusto von Dentz
6233182ee45dSLuiz Augusto von Dentz /* Resume discovery */
6234182ee45dSLuiz Augusto von Dentz hci_resume_discovery_sync(hdev);
6235182ee45dSLuiz Augusto von Dentz
6236182ee45dSLuiz Augusto von Dentz return 0;
6237182ee45dSLuiz Augusto von Dentz }
conn_use_rpa(struct hci_conn * conn)62388e8b92eeSLuiz Augusto von Dentz
62398e8b92eeSLuiz Augusto von Dentz static bool conn_use_rpa(struct hci_conn *conn)
62408e8b92eeSLuiz Augusto von Dentz {
62418e8b92eeSLuiz Augusto von Dentz struct hci_dev *hdev = conn->hdev;
62428e8b92eeSLuiz Augusto von Dentz
62438e8b92eeSLuiz Augusto von Dentz return hci_dev_test_flag(hdev, HCI_PRIVACY);
62448e8b92eeSLuiz Augusto von Dentz }
hci_le_ext_directed_advertising_sync(struct hci_dev * hdev,struct hci_conn * conn)62458e8b92eeSLuiz Augusto von Dentz
62468e8b92eeSLuiz Augusto von Dentz static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
62478e8b92eeSLuiz Augusto von Dentz struct hci_conn *conn)
62488e8b92eeSLuiz Augusto von Dentz {
62498e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_set_ext_adv_params cp;
62508e8b92eeSLuiz Augusto von Dentz int err;
62518e8b92eeSLuiz Augusto von Dentz bdaddr_t random_addr;
62528e8b92eeSLuiz Augusto von Dentz u8 own_addr_type;
62538e8b92eeSLuiz Augusto von Dentz
62548e8b92eeSLuiz Augusto von Dentz err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
62558e8b92eeSLuiz Augusto von Dentz &own_addr_type);
62568e8b92eeSLuiz Augusto von Dentz if (err)
62578e8b92eeSLuiz Augusto von Dentz return err;
62588e8b92eeSLuiz Augusto von Dentz
62598e8b92eeSLuiz Augusto von Dentz /* Set require_privacy to false so that the remote device has a
62608e8b92eeSLuiz Augusto von Dentz * chance of identifying us.
62618e8b92eeSLuiz Augusto von Dentz */
62628e8b92eeSLuiz Augusto von Dentz err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
62638e8b92eeSLuiz Augusto von Dentz &own_addr_type, &random_addr);
62648e8b92eeSLuiz Augusto von Dentz if (err)
62658e8b92eeSLuiz Augusto von Dentz return err;
62668e8b92eeSLuiz Augusto von Dentz
62678e8b92eeSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
62688e8b92eeSLuiz Augusto von Dentz
62698e8b92eeSLuiz Augusto von Dentz cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
62708e8b92eeSLuiz Augusto von Dentz cp.channel_map = hdev->le_adv_channel_map;
62718e8b92eeSLuiz Augusto von Dentz cp.tx_power = HCI_TX_POWER_INVALID;
62728e8b92eeSLuiz Augusto von Dentz cp.primary_phy = HCI_ADV_PHY_1M;
62738e8b92eeSLuiz Augusto von Dentz cp.secondary_phy = HCI_ADV_PHY_1M;
62748e8b92eeSLuiz Augusto von Dentz cp.handle = 0x00; /* Use instance 0 for directed adv */
62758e8b92eeSLuiz Augusto von Dentz cp.own_addr_type = own_addr_type;
62768e8b92eeSLuiz Augusto von Dentz cp.peer_addr_type = conn->dst_type;
62778e8b92eeSLuiz Augusto von Dentz bacpy(&cp.peer_addr, &conn->dst);
62788e8b92eeSLuiz Augusto von Dentz
62798e8b92eeSLuiz Augusto von Dentz /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
62808e8b92eeSLuiz Augusto von Dentz * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
62818e8b92eeSLuiz Augusto von Dentz * does not supports advertising data when the advertising set already
62828e8b92eeSLuiz Augusto von Dentz * contains some, the controller shall return erroc code 'Invalid
62838e8b92eeSLuiz Augusto von Dentz * HCI Command Parameters(0x12).
62848e8b92eeSLuiz Augusto von Dentz * So it is required to remove adv set for handle 0x00. since we use
62858e8b92eeSLuiz Augusto von Dentz * instance 0 for directed adv.
62868e8b92eeSLuiz Augusto von Dentz */
62878e8b92eeSLuiz Augusto von Dentz err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
62888e8b92eeSLuiz Augusto von Dentz if (err)
62898e8b92eeSLuiz Augusto von Dentz return err;
62908e8b92eeSLuiz Augusto von Dentz
62918e8b92eeSLuiz Augusto von Dentz err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
62928e8b92eeSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
62938e8b92eeSLuiz Augusto von Dentz if (err)
62948e8b92eeSLuiz Augusto von Dentz return err;
62958e8b92eeSLuiz Augusto von Dentz
62968e8b92eeSLuiz Augusto von Dentz /* Check if random address need to be updated */
62978e8b92eeSLuiz Augusto von Dentz if (own_addr_type == ADDR_LE_DEV_RANDOM &&
62988e8b92eeSLuiz Augusto von Dentz bacmp(&random_addr, BDADDR_ANY) &&
62998e8b92eeSLuiz Augusto von Dentz bacmp(&random_addr, &hdev->random_addr)) {
63008e8b92eeSLuiz Augusto von Dentz err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
63018e8b92eeSLuiz Augusto von Dentz &random_addr);
63028e8b92eeSLuiz Augusto von Dentz if (err)
63038e8b92eeSLuiz Augusto von Dentz return err;
63048e8b92eeSLuiz Augusto von Dentz }
63058e8b92eeSLuiz Augusto von Dentz
63068e8b92eeSLuiz Augusto von Dentz return hci_enable_ext_advertising_sync(hdev, 0x00);
63078e8b92eeSLuiz Augusto von Dentz }
hci_le_directed_advertising_sync(struct hci_dev * hdev,struct hci_conn * conn)63088e8b92eeSLuiz Augusto von Dentz
63098e8b92eeSLuiz Augusto von Dentz static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
63108e8b92eeSLuiz Augusto von Dentz struct hci_conn *conn)
63118e8b92eeSLuiz Augusto von Dentz {
63128e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_set_adv_param cp;
63138e8b92eeSLuiz Augusto von Dentz u8 status;
63148e8b92eeSLuiz Augusto von Dentz u8 own_addr_type;
63158e8b92eeSLuiz Augusto von Dentz u8 enable;
63168e8b92eeSLuiz Augusto von Dentz
63178e8b92eeSLuiz Augusto von Dentz if (ext_adv_capable(hdev))
63188e8b92eeSLuiz Augusto von Dentz return hci_le_ext_directed_advertising_sync(hdev, conn);
63198e8b92eeSLuiz Augusto von Dentz
63208e8b92eeSLuiz Augusto von Dentz /* Clear the HCI_LE_ADV bit temporarily so that the
63218e8b92eeSLuiz Augusto von Dentz * hci_update_random_address knows that it's safe to go ahead
63228e8b92eeSLuiz Augusto von Dentz * and write a new random address. The flag will be set back on
63238e8b92eeSLuiz Augusto von Dentz * as soon as the SET_ADV_ENABLE HCI command completes.
63248e8b92eeSLuiz Augusto von Dentz */
63258e8b92eeSLuiz Augusto von Dentz hci_dev_clear_flag(hdev, HCI_LE_ADV);
63268e8b92eeSLuiz Augusto von Dentz
63278e8b92eeSLuiz Augusto von Dentz /* Set require_privacy to false so that the remote device has a
63288e8b92eeSLuiz Augusto von Dentz * chance of identifying us.
63298e8b92eeSLuiz Augusto von Dentz */
63308e8b92eeSLuiz Augusto von Dentz status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
63318e8b92eeSLuiz Augusto von Dentz &own_addr_type);
63328e8b92eeSLuiz Augusto von Dentz if (status)
63338e8b92eeSLuiz Augusto von Dentz return status;
63348e8b92eeSLuiz Augusto von Dentz
63358e8b92eeSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
63368e8b92eeSLuiz Augusto von Dentz
63378e8b92eeSLuiz Augusto von Dentz /* Some controllers might reject command if intervals are not
63388e8b92eeSLuiz Augusto von Dentz * within range for undirected advertising.
63398e8b92eeSLuiz Augusto von Dentz * BCM20702A0 is known to be affected by this.
63408e8b92eeSLuiz Augusto von Dentz */
63418e8b92eeSLuiz Augusto von Dentz cp.min_interval = cpu_to_le16(0x0020);
63428e8b92eeSLuiz Augusto von Dentz cp.max_interval = cpu_to_le16(0x0020);
63438e8b92eeSLuiz Augusto von Dentz
63448e8b92eeSLuiz Augusto von Dentz cp.type = LE_ADV_DIRECT_IND;
63458e8b92eeSLuiz Augusto von Dentz cp.own_address_type = own_addr_type;
63468e8b92eeSLuiz Augusto von Dentz cp.direct_addr_type = conn->dst_type;
63478e8b92eeSLuiz Augusto von Dentz bacpy(&cp.direct_addr, &conn->dst);
63488e8b92eeSLuiz Augusto von Dentz cp.channel_map = hdev->le_adv_channel_map;
63498e8b92eeSLuiz Augusto von Dentz
63508e8b92eeSLuiz Augusto von Dentz status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
63518e8b92eeSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
63528e8b92eeSLuiz Augusto von Dentz if (status)
63538e8b92eeSLuiz Augusto von Dentz return status;
63548e8b92eeSLuiz Augusto von Dentz
63558e8b92eeSLuiz Augusto von Dentz enable = 0x01;
63568e8b92eeSLuiz Augusto von Dentz
63578e8b92eeSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
63588e8b92eeSLuiz Augusto von Dentz sizeof(enable), &enable, HCI_CMD_TIMEOUT);
63598e8b92eeSLuiz Augusto von Dentz }
set_ext_conn_params(struct hci_conn * conn,struct hci_cp_le_ext_conn_param * p)63608e8b92eeSLuiz Augusto von Dentz
63618e8b92eeSLuiz Augusto von Dentz static void set_ext_conn_params(struct hci_conn *conn,
63628e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_ext_conn_param *p)
63638e8b92eeSLuiz Augusto von Dentz {
63648e8b92eeSLuiz Augusto von Dentz struct hci_dev *hdev = conn->hdev;
63658e8b92eeSLuiz Augusto von Dentz
63668e8b92eeSLuiz Augusto von Dentz memset(p, 0, sizeof(*p));
63678e8b92eeSLuiz Augusto von Dentz
63688e8b92eeSLuiz Augusto von Dentz p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
63698e8b92eeSLuiz Augusto von Dentz p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
63708e8b92eeSLuiz Augusto von Dentz p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
63718e8b92eeSLuiz Augusto von Dentz p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
63728e8b92eeSLuiz Augusto von Dentz p->conn_latency = cpu_to_le16(conn->le_conn_latency);
63738e8b92eeSLuiz Augusto von Dentz p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
63748e8b92eeSLuiz Augusto von Dentz p->min_ce_len = cpu_to_le16(0x0000);
63758e8b92eeSLuiz Augusto von Dentz p->max_ce_len = cpu_to_le16(0x0000);
63768e8b92eeSLuiz Augusto von Dentz }
hci_le_ext_create_conn_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 own_addr_type)63778e8b92eeSLuiz Augusto von Dentz
637889a0b8b9SLuiz Augusto von Dentz static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
637989a0b8b9SLuiz Augusto von Dentz struct hci_conn *conn, u8 own_addr_type)
63808e8b92eeSLuiz Augusto von Dentz {
63818e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_ext_create_conn *cp;
63828e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_ext_conn_param *p;
63838e8b92eeSLuiz Augusto von Dentz u8 data[sizeof(*cp) + sizeof(*p) * 3];
63848e8b92eeSLuiz Augusto von Dentz u32 plen;
63858e8b92eeSLuiz Augusto von Dentz
63868e8b92eeSLuiz Augusto von Dentz cp = (void *)data;
63878e8b92eeSLuiz Augusto von Dentz p = (void *)cp->data;
63888e8b92eeSLuiz Augusto von Dentz
63898e8b92eeSLuiz Augusto von Dentz memset(cp, 0, sizeof(*cp));
63908e8b92eeSLuiz Augusto von Dentz
63918e8b92eeSLuiz Augusto von Dentz bacpy(&cp->peer_addr, &conn->dst);
63928e8b92eeSLuiz Augusto von Dentz cp->peer_addr_type = conn->dst_type;
63938e8b92eeSLuiz Augusto von Dentz cp->own_addr_type = own_addr_type;
63948e8b92eeSLuiz Augusto von Dentz
63958e8b92eeSLuiz Augusto von Dentz plen = sizeof(*cp);
63968e8b92eeSLuiz Augusto von Dentz
63978e8b92eeSLuiz Augusto von Dentz if (scan_1m(hdev)) {
63988e8b92eeSLuiz Augusto von Dentz cp->phys |= LE_SCAN_PHY_1M;
63998e8b92eeSLuiz Augusto von Dentz set_ext_conn_params(conn, p);
64008e8b92eeSLuiz Augusto von Dentz
64018e8b92eeSLuiz Augusto von Dentz p++;
64028e8b92eeSLuiz Augusto von Dentz plen += sizeof(*p);
64038e8b92eeSLuiz Augusto von Dentz }
64048e8b92eeSLuiz Augusto von Dentz
64058e8b92eeSLuiz Augusto von Dentz if (scan_2m(hdev)) {
64068e8b92eeSLuiz Augusto von Dentz cp->phys |= LE_SCAN_PHY_2M;
64078e8b92eeSLuiz Augusto von Dentz set_ext_conn_params(conn, p);
64088e8b92eeSLuiz Augusto von Dentz
64098e8b92eeSLuiz Augusto von Dentz p++;
64108e8b92eeSLuiz Augusto von Dentz plen += sizeof(*p);
64118e8b92eeSLuiz Augusto von Dentz }
64128e8b92eeSLuiz Augusto von Dentz
64138e8b92eeSLuiz Augusto von Dentz if (scan_coded(hdev)) {
64148e8b92eeSLuiz Augusto von Dentz cp->phys |= LE_SCAN_PHY_CODED;
64158e8b92eeSLuiz Augusto von Dentz set_ext_conn_params(conn, p);
64168e8b92eeSLuiz Augusto von Dentz
64178e8b92eeSLuiz Augusto von Dentz plen += sizeof(*p);
64188e8b92eeSLuiz Augusto von Dentz }
64198e8b92eeSLuiz Augusto von Dentz
64206cd29ec6SLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
64216cd29ec6SLuiz Augusto von Dentz plen, data,
64226cd29ec6SLuiz Augusto von Dentz HCI_EV_LE_ENHANCED_CONN_COMPLETE,
6423a56a1138SLuiz Augusto von Dentz conn->conn_timeout, NULL);
64248e8b92eeSLuiz Augusto von Dentz }
hci_le_create_conn_sync(struct hci_dev * hdev,void * data)64258e8b92eeSLuiz Augusto von Dentz
6426d948e1ffSLuiz Augusto von Dentz static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
64278e8b92eeSLuiz Augusto von Dentz {
64288e8b92eeSLuiz Augusto von Dentz struct hci_cp_le_create_conn cp;
64298e8b92eeSLuiz Augusto von Dentz struct hci_conn_params *params;
64308e8b92eeSLuiz Augusto von Dentz u8 own_addr_type;
64318e8b92eeSLuiz Augusto von Dentz int err;
6432d948e1ffSLuiz Augusto von Dentz struct hci_conn *conn = data;
6433d948e1ffSLuiz Augusto von Dentz
6434d948e1ffSLuiz Augusto von Dentz if (!hci_conn_valid(hdev, conn))
6435d948e1ffSLuiz Augusto von Dentz return -ECANCELED;
6436d948e1ffSLuiz Augusto von Dentz
6437d948e1ffSLuiz Augusto von Dentz bt_dev_dbg(hdev, "conn %p", conn);
6438d948e1ffSLuiz Augusto von Dentz
6439d948e1ffSLuiz Augusto von Dentz clear_bit(HCI_CONN_SCANNING, &conn->flags);
6440d948e1ffSLuiz Augusto von Dentz conn->state = BT_CONNECT;
64418e8b92eeSLuiz Augusto von Dentz
64428e8b92eeSLuiz Augusto von Dentz /* If requested to connect as peripheral use directed advertising */
64438e8b92eeSLuiz Augusto von Dentz if (conn->role == HCI_ROLE_SLAVE) {
644476d0685bSLuiz Augusto von Dentz /* If we're active scanning and simultaneous roles is not
644576d0685bSLuiz Augusto von Dentz * enabled simply reject the attempt.
64468e8b92eeSLuiz Augusto von Dentz */
64478e8b92eeSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
64484fc9857aSLuiz Augusto von Dentz hdev->le_scan_type == LE_SCAN_ACTIVE &&
644976d0685bSLuiz Augusto von Dentz !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
64508e8b92eeSLuiz Augusto von Dentz hci_conn_del(conn);
64518e8b92eeSLuiz Augusto von Dentz return -EBUSY;
64528e8b92eeSLuiz Augusto von Dentz }
64538e8b92eeSLuiz Augusto von Dentz
64544fc9857aSLuiz Augusto von Dentz /* Pause advertising while doing directed advertising. */
64554fc9857aSLuiz Augusto von Dentz hci_pause_advertising_sync(hdev);
64564fc9857aSLuiz Augusto von Dentz
64578e8b92eeSLuiz Augusto von Dentz err = hci_le_directed_advertising_sync(hdev, conn);
64588e8b92eeSLuiz Augusto von Dentz goto done;
64598e8b92eeSLuiz Augusto von Dentz }
64608e8b92eeSLuiz Augusto von Dentz
646176d0685bSLuiz Augusto von Dentz /* Disable advertising if simultaneous roles is not in use. */
646276d0685bSLuiz Augusto von Dentz if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
64634fc9857aSLuiz Augusto von Dentz hci_pause_advertising_sync(hdev);
64644fc9857aSLuiz Augusto von Dentz
64658e8b92eeSLuiz Augusto von Dentz params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
64668e8b92eeSLuiz Augusto von Dentz if (params) {
64678e8b92eeSLuiz Augusto von Dentz conn->le_conn_min_interval = params->conn_min_interval;
64688e8b92eeSLuiz Augusto von Dentz conn->le_conn_max_interval = params->conn_max_interval;
64698e8b92eeSLuiz Augusto von Dentz conn->le_conn_latency = params->conn_latency;
64708e8b92eeSLuiz Augusto von Dentz conn->le_supv_timeout = params->supervision_timeout;
64718e8b92eeSLuiz Augusto von Dentz } else {
64728e8b92eeSLuiz Augusto von Dentz conn->le_conn_min_interval = hdev->le_conn_min_interval;
64738e8b92eeSLuiz Augusto von Dentz conn->le_conn_max_interval = hdev->le_conn_max_interval;
64748e8b92eeSLuiz Augusto von Dentz conn->le_conn_latency = hdev->le_conn_latency;
64758e8b92eeSLuiz Augusto von Dentz conn->le_supv_timeout = hdev->le_supv_timeout;
64768e8b92eeSLuiz Augusto von Dentz }
64778e8b92eeSLuiz Augusto von Dentz
64788e8b92eeSLuiz Augusto von Dentz /* If controller is scanning, we stop it since some controllers are
64798e8b92eeSLuiz Augusto von Dentz * not able to scan and connect at the same time. Also set the
64808e8b92eeSLuiz Augusto von Dentz * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
64818e8b92eeSLuiz Augusto von Dentz * handler for scan disabling knows to set the correct discovery
64828e8b92eeSLuiz Augusto von Dentz * state.
64838e8b92eeSLuiz Augusto von Dentz */
64848e8b92eeSLuiz Augusto von Dentz if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
64858e8b92eeSLuiz Augusto von Dentz hci_scan_disable_sync(hdev);
64868e8b92eeSLuiz Augusto von Dentz hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
64878e8b92eeSLuiz Augusto von Dentz }
64888e8b92eeSLuiz Augusto von Dentz
64898e8b92eeSLuiz Augusto von Dentz /* Update random address, but set require_privacy to false so
64908e8b92eeSLuiz Augusto von Dentz * that we never connect with an non-resolvable address.
64918e8b92eeSLuiz Augusto von Dentz */
64928e8b92eeSLuiz Augusto von Dentz err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
64938e8b92eeSLuiz Augusto von Dentz &own_addr_type);
64948e8b92eeSLuiz Augusto von Dentz if (err)
64958e8b92eeSLuiz Augusto von Dentz goto done;
6496ac22911fSDanil Pylaev /* Send command LE Extended Create Connection if supported */
64978e8b92eeSLuiz Augusto von Dentz if (use_ext_conn(hdev)) {
64988e8b92eeSLuiz Augusto von Dentz err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
64998e8b92eeSLuiz Augusto von Dentz goto done;
65008e8b92eeSLuiz Augusto von Dentz }
65018e8b92eeSLuiz Augusto von Dentz
65028e8b92eeSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
65038e8b92eeSLuiz Augusto von Dentz
65048e8b92eeSLuiz Augusto von Dentz cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
65058e8b92eeSLuiz Augusto von Dentz cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
65068e8b92eeSLuiz Augusto von Dentz
65078e8b92eeSLuiz Augusto von Dentz bacpy(&cp.peer_addr, &conn->dst);
65088e8b92eeSLuiz Augusto von Dentz cp.peer_addr_type = conn->dst_type;
65098e8b92eeSLuiz Augusto von Dentz cp.own_address_type = own_addr_type;
65108e8b92eeSLuiz Augusto von Dentz cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
65118e8b92eeSLuiz Augusto von Dentz cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
65128e8b92eeSLuiz Augusto von Dentz cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
65138e8b92eeSLuiz Augusto von Dentz cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
65148e8b92eeSLuiz Augusto von Dentz cp.min_ce_len = cpu_to_le16(0x0000);
65158e8b92eeSLuiz Augusto von Dentz cp.max_ce_len = cpu_to_le16(0x0000);
65168e8b92eeSLuiz Augusto von Dentz
6517a56a1138SLuiz Augusto von Dentz /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6518a56a1138SLuiz Augusto von Dentz *
6519a56a1138SLuiz Augusto von Dentz * If this event is unmasked and the HCI_LE_Connection_Complete event
6520a56a1138SLuiz Augusto von Dentz * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6521a56a1138SLuiz Augusto von Dentz * sent when a new connection has been created.
6522a56a1138SLuiz Augusto von Dentz */
65236cd29ec6SLuiz Augusto von Dentz err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6524a56a1138SLuiz Augusto von Dentz sizeof(cp), &cp,
6525a56a1138SLuiz Augusto von Dentz use_enhanced_conn_complete(hdev) ?
6526a56a1138SLuiz Augusto von Dentz HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6527a56a1138SLuiz Augusto von Dentz HCI_EV_LE_CONN_COMPLETE,
6528a56a1138SLuiz Augusto von Dentz conn->conn_timeout, NULL);
65298e8b92eeSLuiz Augusto von Dentz
65308e8b92eeSLuiz Augusto von Dentz done:
6531b62e7220SLuiz Augusto von Dentz if (err == -ETIMEDOUT)
6532a13f316eSLuiz Augusto von Dentz hci_le_connect_cancel_sync(hdev, conn, 0x00);
6533b62e7220SLuiz Augusto von Dentz
65344fc9857aSLuiz Augusto von Dentz /* Re-enable advertising after the connection attempt is finished. */
65358e8b92eeSLuiz Augusto von Dentz hci_resume_advertising_sync(hdev);
65368e8b92eeSLuiz Augusto von Dentz return err;
65378e8b92eeSLuiz Augusto von Dentz }
hci_le_create_cis_sync(struct hci_dev * hdev)653826afbd82SLuiz Augusto von Dentz
65397f74563eSPauli Virtanen int hci_le_create_cis_sync(struct hci_dev *hdev)
6540c09b80beSLuiz Augusto von Dentz {
6541c09b80beSLuiz Augusto von Dentz struct {
6542c09b80beSLuiz Augusto von Dentz struct hci_cp_le_create_cis cp;
6543c09b80beSLuiz Augusto von Dentz struct hci_cis cis[0x1f];
6544c09b80beSLuiz Augusto von Dentz } cmd;
65457f74563eSPauli Virtanen struct hci_conn *conn;
65467f74563eSPauli Virtanen u8 cig = BT_ISO_QOS_CIG_UNSET;
6547c09b80beSLuiz Augusto von Dentz
65487f74563eSPauli Virtanen /* The spec allows only one pending LE Create CIS command at a time. If
65497f74563eSPauli Virtanen * the command is pending now, don't do anything. We check for pending
65507f74563eSPauli Virtanen * connections after each CIS Established event.
6551c09b80beSLuiz Augusto von Dentz *
6552c09b80beSLuiz Augusto von Dentz * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6553c09b80beSLuiz Augusto von Dentz * page 2566:
6554c09b80beSLuiz Augusto von Dentz *
6555c09b80beSLuiz Augusto von Dentz * If the Host issues this command before all the
6556c09b80beSLuiz Augusto von Dentz * HCI_LE_CIS_Established events from the previous use of the
6557c09b80beSLuiz Augusto von Dentz * command have been generated, the Controller shall return the
6558c09b80beSLuiz Augusto von Dentz * error code Command Disallowed (0x0C).
65597f74563eSPauli Virtanen *
65607f74563eSPauli Virtanen * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
65617f74563eSPauli Virtanen * page 2567:
65627f74563eSPauli Virtanen *
65637f74563eSPauli Virtanen * When the Controller receives the HCI_LE_Create_CIS command, the
65647f74563eSPauli Virtanen * Controller sends the HCI_Command_Status event to the Host. An
65657f74563eSPauli Virtanen * HCI_LE_CIS_Established event will be generated for each CIS when it
65667f74563eSPauli Virtanen * is established or if it is disconnected or considered lost before
65677f74563eSPauli Virtanen * being established; until all the events are generated, the command
65687f74563eSPauli Virtanen * remains pending.
6569c09b80beSLuiz Augusto von Dentz */
65707f74563eSPauli Virtanen
65717f74563eSPauli Virtanen memset(&cmd, 0, sizeof(cmd));
65727f74563eSPauli Virtanen
65737f74563eSPauli Virtanen hci_dev_lock(hdev);
65747f74563eSPauli Virtanen
65757f74563eSPauli Virtanen rcu_read_lock();
65767f74563eSPauli Virtanen
65777f74563eSPauli Virtanen /* Wait until previous Create CIS has completed */
65787f74563eSPauli Virtanen list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
65797f74563eSPauli Virtanen if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
65807f74563eSPauli Virtanen goto done;
65817f74563eSPauli Virtanen }
65827f74563eSPauli Virtanen
65837f74563eSPauli Virtanen /* Find CIG with all CIS ready */
65847f74563eSPauli Virtanen list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
65857f74563eSPauli Virtanen struct hci_conn *link;
65867f74563eSPauli Virtanen
65877f74563eSPauli Virtanen if (hci_conn_check_create_cis(conn))
65887f74563eSPauli Virtanen continue;
65897f74563eSPauli Virtanen
65907f74563eSPauli Virtanen cig = conn->iso_qos.ucast.cig;
65917f74563eSPauli Virtanen
65927f74563eSPauli Virtanen list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
65937f74563eSPauli Virtanen if (hci_conn_check_create_cis(link) > 0 &&
65947f74563eSPauli Virtanen link->iso_qos.ucast.cig == cig &&
65957f74563eSPauli Virtanen link->state != BT_CONNECTED) {
65967f74563eSPauli Virtanen cig = BT_ISO_QOS_CIG_UNSET;
65977f74563eSPauli Virtanen break;
65987f74563eSPauli Virtanen }
65997f74563eSPauli Virtanen }
66007f74563eSPauli Virtanen
66017f74563eSPauli Virtanen if (cig != BT_ISO_QOS_CIG_UNSET)
66027f74563eSPauli Virtanen break;
66037f74563eSPauli Virtanen }
66047f74563eSPauli Virtanen
66057f74563eSPauli Virtanen if (cig == BT_ISO_QOS_CIG_UNSET)
66067f74563eSPauli Virtanen goto done;
66077f74563eSPauli Virtanen
66087f74563eSPauli Virtanen list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
66097f74563eSPauli Virtanen struct hci_cis *cis = &cmd.cis[cmd.cp.num_cis];
66107f74563eSPauli Virtanen
66117f74563eSPauli Virtanen if (hci_conn_check_create_cis(conn) ||
66127f74563eSPauli Virtanen conn->iso_qos.ucast.cig != cig)
66137f74563eSPauli Virtanen continue;
66147f74563eSPauli Virtanen
66157f74563eSPauli Virtanen set_bit(HCI_CONN_CREATE_CIS, &conn->flags);
6616c09b80beSLuiz Augusto von Dentz cis->acl_handle = cpu_to_le16(conn->parent->handle);
6617c09b80beSLuiz Augusto von Dentz cis->cis_handle = cpu_to_le16(conn->handle);
6618c09b80beSLuiz Augusto von Dentz cmd.cp.num_cis++;
66197f74563eSPauli Virtanen
66207f74563eSPauli Virtanen if (cmd.cp.num_cis >= ARRAY_SIZE(cmd.cis))
66217f74563eSPauli Virtanen break;
6622c09b80beSLuiz Augusto von Dentz }
6623c09b80beSLuiz Augusto von Dentz
66247f74563eSPauli Virtanen done:
6625c09b80beSLuiz Augusto von Dentz rcu_read_unlock();
6626c09b80beSLuiz Augusto von Dentz
6627c09b80beSLuiz Augusto von Dentz hci_dev_unlock(hdev);
6628c09b80beSLuiz Augusto von Dentz
6629c09b80beSLuiz Augusto von Dentz if (!cmd.cp.num_cis)
6630c09b80beSLuiz Augusto von Dentz return 0;
6631c09b80beSLuiz Augusto von Dentz
6632c09b80beSLuiz Augusto von Dentz /* Wait for HCI_LE_CIS_Established */
6633c09b80beSLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6634c09b80beSLuiz Augusto von Dentz sizeof(cmd.cp) + sizeof(cmd.cis[0]) *
6635c09b80beSLuiz Augusto von Dentz cmd.cp.num_cis, &cmd,
6636c09b80beSLuiz Augusto von Dentz HCI_EVT_LE_CIS_ESTABLISHED,
6637c09b80beSLuiz Augusto von Dentz conn->conn_timeout, NULL);
6638c09b80beSLuiz Augusto von Dentz }
hci_le_remove_cig_sync(struct hci_dev * hdev,u8 handle)6639c09b80beSLuiz Augusto von Dentz
664026afbd82SLuiz Augusto von Dentz int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
664126afbd82SLuiz Augusto von Dentz {
664226afbd82SLuiz Augusto von Dentz struct hci_cp_le_remove_cig cp;
664326afbd82SLuiz Augusto von Dentz
664426afbd82SLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
664526afbd82SLuiz Augusto von Dentz cp.cig_id = handle;
664626afbd82SLuiz Augusto von Dentz
664726afbd82SLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
664826afbd82SLuiz Augusto von Dentz &cp, HCI_CMD_TIMEOUT);
664926afbd82SLuiz Augusto von Dentz }
hci_le_big_terminate_sync(struct hci_dev * hdev,u8 handle)6650eca0ae4aSLuiz Augusto von Dentz
6651eca0ae4aSLuiz Augusto von Dentz int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6652eca0ae4aSLuiz Augusto von Dentz {
6653eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_big_term_sync cp;
6654eca0ae4aSLuiz Augusto von Dentz
6655eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
6656eca0ae4aSLuiz Augusto von Dentz cp.handle = handle;
6657eca0ae4aSLuiz Augusto von Dentz
6658eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6659eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6660eca0ae4aSLuiz Augusto von Dentz }
hci_le_pa_terminate_sync(struct hci_dev * hdev,u16 handle)6661eca0ae4aSLuiz Augusto von Dentz
6662eca0ae4aSLuiz Augusto von Dentz int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6663eca0ae4aSLuiz Augusto von Dentz {
6664eca0ae4aSLuiz Augusto von Dentz struct hci_cp_le_pa_term_sync cp;
6665eca0ae4aSLuiz Augusto von Dentz
6666eca0ae4aSLuiz Augusto von Dentz memset(&cp, 0, sizeof(cp));
6667eca0ae4aSLuiz Augusto von Dentz cp.handle = cpu_to_le16(handle);
6668eca0ae4aSLuiz Augusto von Dentz
6669eca0ae4aSLuiz Augusto von Dentz return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6670eca0ae4aSLuiz Augusto von Dentz sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6671eca0ae4aSLuiz Augusto von Dentz }
hci_get_random_address(struct hci_dev * hdev,bool require_privacy,bool use_rpa,struct adv_info * adv_instance,u8 * own_addr_type,bdaddr_t * rand_addr)66723fe318eeSBrian Gix
66733fe318eeSBrian Gix int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
66743fe318eeSBrian Gix bool use_rpa, struct adv_info *adv_instance,
66753fe318eeSBrian Gix u8 *own_addr_type, bdaddr_t *rand_addr)
66763fe318eeSBrian Gix {
66773fe318eeSBrian Gix int err;
66783fe318eeSBrian Gix
66793fe318eeSBrian Gix bacpy(rand_addr, BDADDR_ANY);
66803fe318eeSBrian Gix
66813fe318eeSBrian Gix /* If privacy is enabled use a resolvable private address. If
66823fe318eeSBrian Gix * current RPA has expired then generate a new one.
66833fe318eeSBrian Gix */
66843fe318eeSBrian Gix if (use_rpa) {
66853fe318eeSBrian Gix /* If Controller supports LL Privacy use own address type is
66863fe318eeSBrian Gix * 0x03
66873fe318eeSBrian Gix */
66883fe318eeSBrian Gix if (use_ll_privacy(hdev))
66893fe318eeSBrian Gix *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
66903fe318eeSBrian Gix else
66913fe318eeSBrian Gix *own_addr_type = ADDR_LE_DEV_RANDOM;
66923fe318eeSBrian Gix
66933fe318eeSBrian Gix if (adv_instance) {
66943fe318eeSBrian Gix if (adv_rpa_valid(adv_instance))
66953fe318eeSBrian Gix return 0;
66963fe318eeSBrian Gix } else {
66973fe318eeSBrian Gix if (rpa_valid(hdev))
66983fe318eeSBrian Gix return 0;
66993fe318eeSBrian Gix }
67003fe318eeSBrian Gix
67013fe318eeSBrian Gix err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
67023fe318eeSBrian Gix if (err < 0) {
67033fe318eeSBrian Gix bt_dev_err(hdev, "failed to generate new RPA");
67043fe318eeSBrian Gix return err;
67053fe318eeSBrian Gix }
67063fe318eeSBrian Gix
67073fe318eeSBrian Gix bacpy(rand_addr, &hdev->rpa);
67083fe318eeSBrian Gix
67093fe318eeSBrian Gix return 0;
67103fe318eeSBrian Gix }
67113fe318eeSBrian Gix
67123fe318eeSBrian Gix /* In case of required privacy without resolvable private address,
67133fe318eeSBrian Gix * use an non-resolvable private address. This is useful for
67143fe318eeSBrian Gix * non-connectable advertising.
67153fe318eeSBrian Gix */
67163fe318eeSBrian Gix if (require_privacy) {
67173fe318eeSBrian Gix bdaddr_t nrpa;
67183fe318eeSBrian Gix
67193fe318eeSBrian Gix while (true) {
67203fe318eeSBrian Gix /* The non-resolvable private address is generated
67213fe318eeSBrian Gix * from random six bytes with the two most significant
67223fe318eeSBrian Gix * bits cleared.
67233fe318eeSBrian Gix */
67243fe318eeSBrian Gix get_random_bytes(&nrpa, 6);
67253fe318eeSBrian Gix nrpa.b[5] &= 0x3f;
67263fe318eeSBrian Gix
67273fe318eeSBrian Gix /* The non-resolvable private address shall not be
67283fe318eeSBrian Gix * equal to the public address.
67293fe318eeSBrian Gix */
67303fe318eeSBrian Gix if (bacmp(&hdev->bdaddr, &nrpa))
67313fe318eeSBrian Gix break;
67323fe318eeSBrian Gix }
67333fe318eeSBrian Gix
67343fe318eeSBrian Gix *own_addr_type = ADDR_LE_DEV_RANDOM;
67353fe318eeSBrian Gix bacpy(rand_addr, &nrpa);
67363fe318eeSBrian Gix
67373fe318eeSBrian Gix return 0;
67383fe318eeSBrian Gix }
67393fe318eeSBrian Gix
67403fe318eeSBrian Gix /* No privacy so use a public address. */
67413fe318eeSBrian Gix *own_addr_type = ADDR_LE_DEV_PUBLIC;
67423fe318eeSBrian Gix
67433fe318eeSBrian Gix return 0;
67443fe318eeSBrian Gix }
_update_adv_data_sync(struct hci_dev * hdev,void * data)6745651cd3d6SBrian Gix
6746651cd3d6SBrian Gix static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6747651cd3d6SBrian Gix {
6748a1f6c3aeSLuiz Augusto von Dentz u8 instance = PTR_UINT(data);
6749651cd3d6SBrian Gix
6750651cd3d6SBrian Gix return hci_update_adv_data_sync(hdev, instance);
6751651cd3d6SBrian Gix }
hci_update_adv_data(struct hci_dev * hdev,u8 instance)6752651cd3d6SBrian Gix
6753651cd3d6SBrian Gix int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6754651cd3d6SBrian Gix {
67551ed8b37cSZhengchao Shao return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6756a1f6c3aeSLuiz Augusto von Dentz UINT_PTR(instance), NULL);
6757651cd3d6SBrian Gix }
hci_acl_create_conn_sync(struct hci_dev * hdev,void * data)6758c57edb54SJonas Dreßler
675998f66ea4SLuiz Augusto von Dentz static int hci_acl_create_conn_sync(struct hci_dev *hdev, void *data)
6760c57edb54SJonas Dreßler {
6761d948e1ffSLuiz Augusto von Dentz struct hci_conn *conn = data;
6762c57edb54SJonas Dreßler struct inquiry_entry *ie;
6763c57edb54SJonas Dreßler struct hci_cp_create_conn cp;
6764c57edb54SJonas Dreßler int err;
6765c57edb54SJonas Dreßler
676678155f30SLuiz Augusto von Dentz if (!hci_conn_valid(hdev, conn))
676778155f30SLuiz Augusto von Dentz return -ECANCELED;
676878155f30SLuiz Augusto von Dentz
6769c57edb54SJonas Dreßler /* Many controllers disallow HCI Create Connection while it is doing
6770c57edb54SJonas Dreßler * HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create
6771c57edb54SJonas Dreßler * Connection. This may cause the MGMT discovering state to become false
6772c57edb54SJonas Dreßler * without user space's request but it is okay since the MGMT Discovery
6773c57edb54SJonas Dreßler * APIs do not promise that discovery should be done forever. Instead,
6774c57edb54SJonas Dreßler * the user space monitors the status of MGMT discovering and it may
6775c57edb54SJonas Dreßler * request for discovery again when this flag becomes false.
6776c57edb54SJonas Dreßler */
6777c57edb54SJonas Dreßler if (test_bit(HCI_INQUIRY, &hdev->flags)) {
6778c57edb54SJonas Dreßler err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 0,
6779c57edb54SJonas Dreßler NULL, HCI_CMD_TIMEOUT);
6780c57edb54SJonas Dreßler if (err)
6781c57edb54SJonas Dreßler bt_dev_warn(hdev, "Failed to cancel inquiry %d", err);
6782c57edb54SJonas Dreßler }
6783c57edb54SJonas Dreßler
6784c57edb54SJonas Dreßler conn->state = BT_CONNECT;
6785c57edb54SJonas Dreßler conn->out = true;
6786c57edb54SJonas Dreßler conn->role = HCI_ROLE_MASTER;
6787c57edb54SJonas Dreßler
6788c57edb54SJonas Dreßler conn->attempt++;
6789c57edb54SJonas Dreßler
6790c57edb54SJonas Dreßler conn->link_policy = hdev->link_policy;
6791c57edb54SJonas Dreßler
6792c57edb54SJonas Dreßler memset(&cp, 0, sizeof(cp));
6793c57edb54SJonas Dreßler bacpy(&cp.bdaddr, &conn->dst);
6794c57edb54SJonas Dreßler cp.pscan_rep_mode = 0x02;
6795c57edb54SJonas Dreßler
6796c57edb54SJonas Dreßler ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
6797c57edb54SJonas Dreßler if (ie) {
6798c57edb54SJonas Dreßler if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
6799c57edb54SJonas Dreßler cp.pscan_rep_mode = ie->data.pscan_rep_mode;
6800c57edb54SJonas Dreßler cp.pscan_mode = ie->data.pscan_mode;
6801c57edb54SJonas Dreßler cp.clock_offset = ie->data.clock_offset |
6802c57edb54SJonas Dreßler cpu_to_le16(0x8000);
6803c57edb54SJonas Dreßler }
6804c57edb54SJonas Dreßler
6805c57edb54SJonas Dreßler memcpy(conn->dev_class, ie->data.dev_class, 3);
6806c57edb54SJonas Dreßler }
6807c57edb54SJonas Dreßler
6808c57edb54SJonas Dreßler cp.pkt_type = cpu_to_le16(conn->pkt_type);
6809c57edb54SJonas Dreßler if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
6810c57edb54SJonas Dreßler cp.role_switch = 0x01;
6811c57edb54SJonas Dreßler else
6812c57edb54SJonas Dreßler cp.role_switch = 0x00;
6813c57edb54SJonas Dreßler
6814611e4281SLuiz Augusto von Dentz return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN,
6815c57edb54SJonas Dreßler sizeof(cp), &cp,
6816c57edb54SJonas Dreßler HCI_EV_CONN_COMPLETE,
6817611e4281SLuiz Augusto von Dentz conn->conn_timeout, NULL);
6818c57edb54SJonas Dreßler }
hci_connect_acl_sync(struct hci_dev * hdev,struct hci_conn * conn)6819c57edb54SJonas Dreßler
682098f66ea4SLuiz Augusto von Dentz int hci_connect_acl_sync(struct hci_dev *hdev, struct hci_conn *conn)
6821c57edb54SJonas Dreßler {
6822d948e1ffSLuiz Augusto von Dentz return hci_cmd_sync_queue_once(hdev, hci_acl_create_conn_sync, conn,
6823d948e1ffSLuiz Augusto von Dentz NULL);
6824d948e1ffSLuiz Augusto von Dentz }
create_le_conn_complete(struct hci_dev * hdev,void * data,int err)6825d948e1ffSLuiz Augusto von Dentz
6826d948e1ffSLuiz Augusto von Dentz static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err)
6827d948e1ffSLuiz Augusto von Dentz {
6828d948e1ffSLuiz Augusto von Dentz struct hci_conn *conn = data;
6829d948e1ffSLuiz Augusto von Dentz
6830d948e1ffSLuiz Augusto von Dentz bt_dev_dbg(hdev, "err %d", err);
6831d948e1ffSLuiz Augusto von Dentz
6832d948e1ffSLuiz Augusto von Dentz if (err == -ECANCELED)
6833d948e1ffSLuiz Augusto von Dentz return;
6834d948e1ffSLuiz Augusto von Dentz
6835d948e1ffSLuiz Augusto von Dentz hci_dev_lock(hdev);
6836d948e1ffSLuiz Augusto von Dentz
68374d6cf010SLuiz Augusto von Dentz if (!hci_conn_valid(hdev, conn))
68384d6cf010SLuiz Augusto von Dentz goto done;
68394d6cf010SLuiz Augusto von Dentz
6840d948e1ffSLuiz Augusto von Dentz if (!err) {
6841d948e1ffSLuiz Augusto von Dentz hci_connect_le_scan_cleanup(conn, 0x00);
6842d948e1ffSLuiz Augusto von Dentz goto done;
6843d948e1ffSLuiz Augusto von Dentz }
6844d948e1ffSLuiz Augusto von Dentz
6845d948e1ffSLuiz Augusto von Dentz /* Check if connection is still pending */
6846d948e1ffSLuiz Augusto von Dentz if (conn != hci_lookup_le_connect(hdev))
6847d948e1ffSLuiz Augusto von Dentz goto done;
6848d948e1ffSLuiz Augusto von Dentz
6849d948e1ffSLuiz Augusto von Dentz /* Flush to make sure we send create conn cancel command if needed */
6850d948e1ffSLuiz Augusto von Dentz flush_delayed_work(&conn->le_conn_timeout);
6851d948e1ffSLuiz Augusto von Dentz hci_conn_failed(conn, bt_status(err));
6852d948e1ffSLuiz Augusto von Dentz
6853d948e1ffSLuiz Augusto von Dentz done:
6854d948e1ffSLuiz Augusto von Dentz hci_dev_unlock(hdev);
6855d948e1ffSLuiz Augusto von Dentz }
hci_connect_le_sync(struct hci_dev * hdev,struct hci_conn * conn)6856d948e1ffSLuiz Augusto von Dentz
6857d948e1ffSLuiz Augusto von Dentz int hci_connect_le_sync(struct hci_dev *hdev, struct hci_conn *conn)
6858d948e1ffSLuiz Augusto von Dentz {
6859d948e1ffSLuiz Augusto von Dentz return hci_cmd_sync_queue_once(hdev, hci_le_create_conn_sync, conn,
6860d948e1ffSLuiz Augusto von Dentz create_le_conn_complete);
6861d948e1ffSLuiz Augusto von Dentz }
hci_cancel_connect_sync(struct hci_dev * hdev,struct hci_conn * conn)6862d948e1ffSLuiz Augusto von Dentz
6863d948e1ffSLuiz Augusto von Dentz int hci_cancel_connect_sync(struct hci_dev *hdev, struct hci_conn *conn)
6864d948e1ffSLuiz Augusto von Dentz {
6865d948e1ffSLuiz Augusto von Dentz if (conn->state != BT_OPEN)
6866d948e1ffSLuiz Augusto von Dentz return -EINVAL;
6867d948e1ffSLuiz Augusto von Dentz
6868d948e1ffSLuiz Augusto von Dentz switch (conn->type) {
6869d948e1ffSLuiz Augusto von Dentz case ACL_LINK:
6870d948e1ffSLuiz Augusto von Dentz return !hci_cmd_sync_dequeue_once(hdev,
6871d948e1ffSLuiz Augusto von Dentz hci_acl_create_conn_sync,
6872d948e1ffSLuiz Augusto von Dentz conn, NULL);
6873d948e1ffSLuiz Augusto von Dentz case LE_LINK:
6874d948e1ffSLuiz Augusto von Dentz return !hci_cmd_sync_dequeue_once(hdev, hci_le_create_conn_sync,
6875d948e1ffSLuiz Augusto von Dentz conn, create_le_conn_complete);
6876d948e1ffSLuiz Augusto von Dentz }
6877d948e1ffSLuiz Augusto von Dentz
6878d948e1ffSLuiz Augusto von Dentz return -ENOENT;
6879c57edb54SJonas Dreßler }
6880