xref: /openbmc/linux/net/bluetooth/hci_sync.c (revision 3b42055388c30f2761a2d9cd9af2c99611dfe457)
16a98e383SMarcel Holtmann // SPDX-License-Identifier: GPL-2.0
26a98e383SMarcel Holtmann /*
36a98e383SMarcel Holtmann  * BlueZ - Bluetooth protocol stack for Linux
46a98e383SMarcel Holtmann  *
56a98e383SMarcel Holtmann  * Copyright (C) 2021 Intel Corporation
66a98e383SMarcel Holtmann  */
76a98e383SMarcel Holtmann 
8d0b13706SLuiz Augusto von Dentz #include <linux/property.h>
9d0b13706SLuiz Augusto von Dentz 
106a98e383SMarcel Holtmann #include <net/bluetooth/bluetooth.h>
116a98e383SMarcel Holtmann #include <net/bluetooth/hci_core.h>
126a98e383SMarcel Holtmann #include <net/bluetooth/mgmt.h>
136a98e383SMarcel Holtmann 
146a98e383SMarcel Holtmann #include "hci_request.h"
15d0b13706SLuiz Augusto von Dentz #include "hci_debugfs.h"
166a98e383SMarcel Holtmann #include "smp.h"
17161510ccSLuiz Augusto von Dentz #include "eir.h"
18d0b13706SLuiz Augusto von Dentz #include "msft.h"
19d0b13706SLuiz Augusto von Dentz #include "aosp.h"
20d0b13706SLuiz Augusto von Dentz #include "leds.h"
216a98e383SMarcel Holtmann 
226a98e383SMarcel Holtmann static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
236a98e383SMarcel Holtmann 				  struct sk_buff *skb)
246a98e383SMarcel Holtmann {
256a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "result 0x%2.2x", result);
266a98e383SMarcel Holtmann 
276a98e383SMarcel Holtmann 	if (hdev->req_status != HCI_REQ_PEND)
286a98e383SMarcel Holtmann 		return;
296a98e383SMarcel Holtmann 
306a98e383SMarcel Holtmann 	hdev->req_result = result;
316a98e383SMarcel Holtmann 	hdev->req_status = HCI_REQ_DONE;
326a98e383SMarcel Holtmann 
33cba6b758SLuiz Augusto von Dentz 	if (skb) {
34cba6b758SLuiz Augusto von Dentz 		struct sock *sk = hci_skb_sk(skb);
35cba6b758SLuiz Augusto von Dentz 
36cba6b758SLuiz Augusto von Dentz 		/* Drop sk reference if set */
37cba6b758SLuiz Augusto von Dentz 		if (sk)
38cba6b758SLuiz Augusto von Dentz 			sock_put(sk);
39cba6b758SLuiz Augusto von Dentz 
40cba6b758SLuiz Augusto von Dentz 		hdev->req_skb = skb_get(skb);
41cba6b758SLuiz Augusto von Dentz 	}
42cba6b758SLuiz Augusto von Dentz 
436a98e383SMarcel Holtmann 	wake_up_interruptible(&hdev->req_wait_q);
446a98e383SMarcel Holtmann }
456a98e383SMarcel Holtmann 
466a98e383SMarcel Holtmann static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
476a98e383SMarcel Holtmann 					  u32 plen, const void *param,
486a98e383SMarcel Holtmann 					  struct sock *sk)
496a98e383SMarcel Holtmann {
506a98e383SMarcel Holtmann 	int len = HCI_COMMAND_HDR_SIZE + plen;
516a98e383SMarcel Holtmann 	struct hci_command_hdr *hdr;
526a98e383SMarcel Holtmann 	struct sk_buff *skb;
536a98e383SMarcel Holtmann 
546a98e383SMarcel Holtmann 	skb = bt_skb_alloc(len, GFP_ATOMIC);
556a98e383SMarcel Holtmann 	if (!skb)
566a98e383SMarcel Holtmann 		return NULL;
576a98e383SMarcel Holtmann 
586a98e383SMarcel Holtmann 	hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
596a98e383SMarcel Holtmann 	hdr->opcode = cpu_to_le16(opcode);
606a98e383SMarcel Holtmann 	hdr->plen   = plen;
616a98e383SMarcel Holtmann 
626a98e383SMarcel Holtmann 	if (plen)
636a98e383SMarcel Holtmann 		skb_put_data(skb, param, plen);
646a98e383SMarcel Holtmann 
656a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "skb len %d", skb->len);
666a98e383SMarcel Holtmann 
676a98e383SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
686a98e383SMarcel Holtmann 	hci_skb_opcode(skb) = opcode;
696a98e383SMarcel Holtmann 
70cba6b758SLuiz Augusto von Dentz 	/* Grab a reference if command needs to be associated with a sock (e.g.
71cba6b758SLuiz Augusto von Dentz 	 * likely mgmt socket that initiated the command).
72cba6b758SLuiz Augusto von Dentz 	 */
73cba6b758SLuiz Augusto von Dentz 	if (sk) {
74cba6b758SLuiz Augusto von Dentz 		hci_skb_sk(skb) = sk;
75cba6b758SLuiz Augusto von Dentz 		sock_hold(sk);
76cba6b758SLuiz Augusto von Dentz 	}
77cba6b758SLuiz Augusto von Dentz 
786a98e383SMarcel Holtmann 	return skb;
796a98e383SMarcel Holtmann }
806a98e383SMarcel Holtmann 
816a98e383SMarcel Holtmann static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
826a98e383SMarcel Holtmann 			     const void *param, u8 event, struct sock *sk)
836a98e383SMarcel Holtmann {
846a98e383SMarcel Holtmann 	struct hci_dev *hdev = req->hdev;
856a98e383SMarcel Holtmann 	struct sk_buff *skb;
866a98e383SMarcel Holtmann 
876a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
886a98e383SMarcel Holtmann 
896a98e383SMarcel Holtmann 	/* If an error occurred during request building, there is no point in
906a98e383SMarcel Holtmann 	 * queueing the HCI command. We can simply return.
916a98e383SMarcel Holtmann 	 */
926a98e383SMarcel Holtmann 	if (req->err)
936a98e383SMarcel Holtmann 		return;
946a98e383SMarcel Holtmann 
956a98e383SMarcel Holtmann 	skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
966a98e383SMarcel Holtmann 	if (!skb) {
976a98e383SMarcel Holtmann 		bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
986a98e383SMarcel Holtmann 			   opcode);
996a98e383SMarcel Holtmann 		req->err = -ENOMEM;
1006a98e383SMarcel Holtmann 		return;
1016a98e383SMarcel Holtmann 	}
1026a98e383SMarcel Holtmann 
1036a98e383SMarcel Holtmann 	if (skb_queue_empty(&req->cmd_q))
1046a98e383SMarcel Holtmann 		bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
1056a98e383SMarcel Holtmann 
10685b56857SLuiz Augusto von Dentz 	hci_skb_event(skb) = event;
1076a98e383SMarcel Holtmann 
1086a98e383SMarcel Holtmann 	skb_queue_tail(&req->cmd_q, skb);
1096a98e383SMarcel Holtmann }
1106a98e383SMarcel Holtmann 
1116a98e383SMarcel Holtmann static int hci_cmd_sync_run(struct hci_request *req)
1126a98e383SMarcel Holtmann {
1136a98e383SMarcel Holtmann 	struct hci_dev *hdev = req->hdev;
1146a98e383SMarcel Holtmann 	struct sk_buff *skb;
1156a98e383SMarcel Holtmann 	unsigned long flags;
1166a98e383SMarcel Holtmann 
1176a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
1186a98e383SMarcel Holtmann 
1196a98e383SMarcel Holtmann 	/* If an error occurred during request building, remove all HCI
1206a98e383SMarcel Holtmann 	 * commands queued on the HCI request queue.
1216a98e383SMarcel Holtmann 	 */
1226a98e383SMarcel Holtmann 	if (req->err) {
1236a98e383SMarcel Holtmann 		skb_queue_purge(&req->cmd_q);
1246a98e383SMarcel Holtmann 		return req->err;
1256a98e383SMarcel Holtmann 	}
1266a98e383SMarcel Holtmann 
1276a98e383SMarcel Holtmann 	/* Do not allow empty requests */
1286a98e383SMarcel Holtmann 	if (skb_queue_empty(&req->cmd_q))
1296a98e383SMarcel Holtmann 		return -ENODATA;
1306a98e383SMarcel Holtmann 
1316a98e383SMarcel Holtmann 	skb = skb_peek_tail(&req->cmd_q);
1326a98e383SMarcel Holtmann 	bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
1336a98e383SMarcel Holtmann 	bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
1346a98e383SMarcel Holtmann 
1356a98e383SMarcel Holtmann 	spin_lock_irqsave(&hdev->cmd_q.lock, flags);
1366a98e383SMarcel Holtmann 	skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
1376a98e383SMarcel Holtmann 	spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
1386a98e383SMarcel Holtmann 
1396a98e383SMarcel Holtmann 	queue_work(hdev->workqueue, &hdev->cmd_work);
1406a98e383SMarcel Holtmann 
1416a98e383SMarcel Holtmann 	return 0;
1426a98e383SMarcel Holtmann }
1436a98e383SMarcel Holtmann 
1446a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
1456a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
1466a98e383SMarcel Holtmann 				  const void *param, u8 event, u32 timeout,
1476a98e383SMarcel Holtmann 				  struct sock *sk)
1486a98e383SMarcel Holtmann {
1496a98e383SMarcel Holtmann 	struct hci_request req;
1506a98e383SMarcel Holtmann 	struct sk_buff *skb;
1516a98e383SMarcel Holtmann 	int err = 0;
1526a98e383SMarcel Holtmann 
153d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Opcode 0x%4x", opcode);
1546a98e383SMarcel Holtmann 
1556a98e383SMarcel Holtmann 	hci_req_init(&req, hdev);
1566a98e383SMarcel Holtmann 
1576a98e383SMarcel Holtmann 	hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
1586a98e383SMarcel Holtmann 
1596a98e383SMarcel Holtmann 	hdev->req_status = HCI_REQ_PEND;
1606a98e383SMarcel Holtmann 
1616a98e383SMarcel Holtmann 	err = hci_cmd_sync_run(&req);
1626a98e383SMarcel Holtmann 	if (err < 0)
1636a98e383SMarcel Holtmann 		return ERR_PTR(err);
1646a98e383SMarcel Holtmann 
1656a98e383SMarcel Holtmann 	err = wait_event_interruptible_timeout(hdev->req_wait_q,
1666a98e383SMarcel Holtmann 					       hdev->req_status != HCI_REQ_PEND,
1676a98e383SMarcel Holtmann 					       timeout);
1686a98e383SMarcel Holtmann 
1696a98e383SMarcel Holtmann 	if (err == -ERESTARTSYS)
1706a98e383SMarcel Holtmann 		return ERR_PTR(-EINTR);
1716a98e383SMarcel Holtmann 
1726a98e383SMarcel Holtmann 	switch (hdev->req_status) {
1736a98e383SMarcel Holtmann 	case HCI_REQ_DONE:
1746a98e383SMarcel Holtmann 		err = -bt_to_errno(hdev->req_result);
1756a98e383SMarcel Holtmann 		break;
1766a98e383SMarcel Holtmann 
1776a98e383SMarcel Holtmann 	case HCI_REQ_CANCELED:
1786a98e383SMarcel Holtmann 		err = -hdev->req_result;
1796a98e383SMarcel Holtmann 		break;
1806a98e383SMarcel Holtmann 
1816a98e383SMarcel Holtmann 	default:
1826a98e383SMarcel Holtmann 		err = -ETIMEDOUT;
1836a98e383SMarcel Holtmann 		break;
1846a98e383SMarcel Holtmann 	}
1856a98e383SMarcel Holtmann 
1866a98e383SMarcel Holtmann 	hdev->req_status = 0;
1876a98e383SMarcel Holtmann 	hdev->req_result = 0;
1886a98e383SMarcel Holtmann 	skb = hdev->req_skb;
1896a98e383SMarcel Holtmann 	hdev->req_skb = NULL;
1906a98e383SMarcel Holtmann 
1916a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "end: err %d", err);
1926a98e383SMarcel Holtmann 
1936a98e383SMarcel Holtmann 	if (err < 0) {
1946a98e383SMarcel Holtmann 		kfree_skb(skb);
1956a98e383SMarcel Holtmann 		return ERR_PTR(err);
1966a98e383SMarcel Holtmann 	}
1976a98e383SMarcel Holtmann 
1986a98e383SMarcel Holtmann 	return skb;
1996a98e383SMarcel Holtmann }
2006a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_sk);
2016a98e383SMarcel Holtmann 
2026a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
2036a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
2046a98e383SMarcel Holtmann 			       const void *param, u32 timeout)
2056a98e383SMarcel Holtmann {
2066a98e383SMarcel Holtmann 	return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
2076a98e383SMarcel Holtmann }
2086a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync);
2096a98e383SMarcel Holtmann 
2106a98e383SMarcel Holtmann /* Send HCI command and wait for command complete event */
2116a98e383SMarcel Holtmann struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
2126a98e383SMarcel Holtmann 			     const void *param, u32 timeout)
2136a98e383SMarcel Holtmann {
2146a98e383SMarcel Holtmann 	struct sk_buff *skb;
2156a98e383SMarcel Holtmann 
2166a98e383SMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags))
2176a98e383SMarcel Holtmann 		return ERR_PTR(-ENETDOWN);
2186a98e383SMarcel Holtmann 
2196a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
2206a98e383SMarcel Holtmann 
2216a98e383SMarcel Holtmann 	hci_req_sync_lock(hdev);
2226a98e383SMarcel Holtmann 	skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
2236a98e383SMarcel Holtmann 	hci_req_sync_unlock(hdev);
2246a98e383SMarcel Holtmann 
2256a98e383SMarcel Holtmann 	return skb;
2266a98e383SMarcel Holtmann }
2276a98e383SMarcel Holtmann EXPORT_SYMBOL(hci_cmd_sync);
2286a98e383SMarcel Holtmann 
2296a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
2306a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
2316a98e383SMarcel Holtmann 				  const void *param, u8 event, u32 timeout)
2326a98e383SMarcel Holtmann {
2336a98e383SMarcel Holtmann 	return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
2346a98e383SMarcel Holtmann 				 NULL);
2356a98e383SMarcel Holtmann }
2366a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_ev);
2376a98e383SMarcel Holtmann 
2386a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
2396a98e383SMarcel Holtmann int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
2406a98e383SMarcel Holtmann 			     const void *param, u8 event, u32 timeout,
2416a98e383SMarcel Holtmann 			     struct sock *sk)
2426a98e383SMarcel Holtmann {
2436a98e383SMarcel Holtmann 	struct sk_buff *skb;
2446a98e383SMarcel Holtmann 	u8 status;
2456a98e383SMarcel Holtmann 
2466a98e383SMarcel Holtmann 	skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
247abfeea47SLuiz Augusto von Dentz 	if (IS_ERR(skb)) {
2486a98e383SMarcel Holtmann 		bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode,
2496a98e383SMarcel Holtmann 			   PTR_ERR(skb));
2506a98e383SMarcel Holtmann 		return PTR_ERR(skb);
2516a98e383SMarcel Holtmann 	}
2526a98e383SMarcel Holtmann 
253abfeea47SLuiz Augusto von Dentz 	/* If command return a status event skb will be set to NULL as there are
254abfeea47SLuiz Augusto von Dentz 	 * no parameters, in case of failure IS_ERR(skb) would have be set to
255abfeea47SLuiz Augusto von Dentz 	 * the actual error would be found with PTR_ERR(skb).
256abfeea47SLuiz Augusto von Dentz 	 */
257abfeea47SLuiz Augusto von Dentz 	if (!skb)
258abfeea47SLuiz Augusto von Dentz 		return 0;
259abfeea47SLuiz Augusto von Dentz 
2606a98e383SMarcel Holtmann 	status = skb->data[0];
2616a98e383SMarcel Holtmann 
2626a98e383SMarcel Holtmann 	kfree_skb(skb);
2636a98e383SMarcel Holtmann 
2646a98e383SMarcel Holtmann 	return status;
2656a98e383SMarcel Holtmann }
2666a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
2676a98e383SMarcel Holtmann 
2686a98e383SMarcel Holtmann int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
2696a98e383SMarcel Holtmann 			  const void *param, u32 timeout)
2706a98e383SMarcel Holtmann {
2716a98e383SMarcel Holtmann 	return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
2726a98e383SMarcel Holtmann 					NULL);
2736a98e383SMarcel Holtmann }
2746a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_status);
2756a98e383SMarcel Holtmann 
2766a98e383SMarcel Holtmann static void hci_cmd_sync_work(struct work_struct *work)
2776a98e383SMarcel Holtmann {
2786a98e383SMarcel Holtmann 	struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
2796a98e383SMarcel Holtmann 
2806a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "");
2816a98e383SMarcel Holtmann 
282008ee9ebSLuiz Augusto von Dentz 	/* Dequeue all entries and run them */
283008ee9ebSLuiz Augusto von Dentz 	while (1) {
284008ee9ebSLuiz Augusto von Dentz 		struct hci_cmd_sync_work_entry *entry;
285008ee9ebSLuiz Augusto von Dentz 
2866a98e383SMarcel Holtmann 		mutex_lock(&hdev->cmd_sync_work_lock);
287008ee9ebSLuiz Augusto von Dentz 		entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
288008ee9ebSLuiz Augusto von Dentz 						 struct hci_cmd_sync_work_entry,
289008ee9ebSLuiz Augusto von Dentz 						 list);
290008ee9ebSLuiz Augusto von Dentz 		if (entry)
2916a98e383SMarcel Holtmann 			list_del(&entry->list);
2926a98e383SMarcel Holtmann 		mutex_unlock(&hdev->cmd_sync_work_lock);
2936a98e383SMarcel Holtmann 
294008ee9ebSLuiz Augusto von Dentz 		if (!entry)
295008ee9ebSLuiz Augusto von Dentz 			break;
296008ee9ebSLuiz Augusto von Dentz 
297008ee9ebSLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "entry %p", entry);
298008ee9ebSLuiz Augusto von Dentz 
299008ee9ebSLuiz Augusto von Dentz 		if (entry->func) {
3006a98e383SMarcel Holtmann 			int err;
3016a98e383SMarcel Holtmann 
3026a98e383SMarcel Holtmann 			hci_req_sync_lock(hdev);
303008ee9ebSLuiz Augusto von Dentz 			err = entry->func(hdev, entry->data);
304008ee9ebSLuiz Augusto von Dentz 			if (entry->destroy)
305008ee9ebSLuiz Augusto von Dentz 				entry->destroy(hdev, entry->data, err);
3066a98e383SMarcel Holtmann 			hci_req_sync_unlock(hdev);
3076a98e383SMarcel Holtmann 		}
308008ee9ebSLuiz Augusto von Dentz 
309008ee9ebSLuiz Augusto von Dentz 		kfree(entry);
310008ee9ebSLuiz Augusto von Dentz 	}
3116a98e383SMarcel Holtmann }
3126a98e383SMarcel Holtmann 
313744451c1SBenjamin Berg static void hci_cmd_sync_cancel_work(struct work_struct *work)
314744451c1SBenjamin Berg {
315744451c1SBenjamin Berg 	struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
316744451c1SBenjamin Berg 
317744451c1SBenjamin Berg 	cancel_delayed_work_sync(&hdev->cmd_timer);
318744451c1SBenjamin Berg 	cancel_delayed_work_sync(&hdev->ncmd_timer);
319744451c1SBenjamin Berg 	atomic_set(&hdev->cmd_cnt, 1);
320744451c1SBenjamin Berg 
321744451c1SBenjamin Berg 	wake_up_interruptible(&hdev->req_wait_q);
322744451c1SBenjamin Berg }
323744451c1SBenjamin Berg 
3246a98e383SMarcel Holtmann void hci_cmd_sync_init(struct hci_dev *hdev)
3256a98e383SMarcel Holtmann {
3266a98e383SMarcel Holtmann 	INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
3276a98e383SMarcel Holtmann 	INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
3286a98e383SMarcel Holtmann 	mutex_init(&hdev->cmd_sync_work_lock);
329744451c1SBenjamin Berg 
330744451c1SBenjamin Berg 	INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
3316a98e383SMarcel Holtmann }
3326a98e383SMarcel Holtmann 
3336a98e383SMarcel Holtmann void hci_cmd_sync_clear(struct hci_dev *hdev)
3346a98e383SMarcel Holtmann {
3356a98e383SMarcel Holtmann 	struct hci_cmd_sync_work_entry *entry, *tmp;
3366a98e383SMarcel Holtmann 
3376a98e383SMarcel Holtmann 	cancel_work_sync(&hdev->cmd_sync_work);
3386a98e383SMarcel Holtmann 
3396a98e383SMarcel Holtmann 	list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
3406a98e383SMarcel Holtmann 		if (entry->destroy)
3416a98e383SMarcel Holtmann 			entry->destroy(hdev, entry->data, -ECANCELED);
3426a98e383SMarcel Holtmann 
3436a98e383SMarcel Holtmann 		list_del(&entry->list);
3446a98e383SMarcel Holtmann 		kfree(entry);
3456a98e383SMarcel Holtmann 	}
3466a98e383SMarcel Holtmann }
3476a98e383SMarcel Holtmann 
348744451c1SBenjamin Berg void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
349914b08b3SBenjamin Berg {
350914b08b3SBenjamin Berg 	bt_dev_dbg(hdev, "err 0x%2.2x", err);
351914b08b3SBenjamin Berg 
352914b08b3SBenjamin Berg 	if (hdev->req_status == HCI_REQ_PEND) {
353914b08b3SBenjamin Berg 		hdev->req_result = err;
354914b08b3SBenjamin Berg 		hdev->req_status = HCI_REQ_CANCELED;
355914b08b3SBenjamin Berg 
356914b08b3SBenjamin Berg 		cancel_delayed_work_sync(&hdev->cmd_timer);
357914b08b3SBenjamin Berg 		cancel_delayed_work_sync(&hdev->ncmd_timer);
358914b08b3SBenjamin Berg 		atomic_set(&hdev->cmd_cnt, 1);
359914b08b3SBenjamin Berg 
360914b08b3SBenjamin Berg 		wake_up_interruptible(&hdev->req_wait_q);
361914b08b3SBenjamin Berg 	}
362914b08b3SBenjamin Berg }
363744451c1SBenjamin Berg 
364744451c1SBenjamin Berg void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
365744451c1SBenjamin Berg {
366744451c1SBenjamin Berg 	bt_dev_dbg(hdev, "err 0x%2.2x", err);
367744451c1SBenjamin Berg 
368744451c1SBenjamin Berg 	if (hdev->req_status == HCI_REQ_PEND) {
369744451c1SBenjamin Berg 		hdev->req_result = err;
370744451c1SBenjamin Berg 		hdev->req_status = HCI_REQ_CANCELED;
371744451c1SBenjamin Berg 
372744451c1SBenjamin Berg 		queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
373744451c1SBenjamin Berg 	}
374744451c1SBenjamin Berg }
375914b08b3SBenjamin Berg EXPORT_SYMBOL(hci_cmd_sync_cancel);
376914b08b3SBenjamin Berg 
3776a98e383SMarcel Holtmann int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
3786a98e383SMarcel Holtmann 		       void *data, hci_cmd_sync_work_destroy_t destroy)
3796a98e383SMarcel Holtmann {
3806a98e383SMarcel Holtmann 	struct hci_cmd_sync_work_entry *entry;
3816a98e383SMarcel Holtmann 
3820b94f265SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
3830b94f265SLuiz Augusto von Dentz 		return -ENODEV;
3840b94f265SLuiz Augusto von Dentz 
3856a98e383SMarcel Holtmann 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3866a98e383SMarcel Holtmann 	if (!entry)
3876a98e383SMarcel Holtmann 		return -ENOMEM;
3886a98e383SMarcel Holtmann 
3896a98e383SMarcel Holtmann 	entry->func = func;
3906a98e383SMarcel Holtmann 	entry->data = data;
3916a98e383SMarcel Holtmann 	entry->destroy = destroy;
3926a98e383SMarcel Holtmann 
3936a98e383SMarcel Holtmann 	mutex_lock(&hdev->cmd_sync_work_lock);
3946a98e383SMarcel Holtmann 	list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
3956a98e383SMarcel Holtmann 	mutex_unlock(&hdev->cmd_sync_work_lock);
3966a98e383SMarcel Holtmann 
3976a98e383SMarcel Holtmann 	queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
3986a98e383SMarcel Holtmann 
3996a98e383SMarcel Holtmann 	return 0;
4006a98e383SMarcel Holtmann }
4016a98e383SMarcel Holtmann EXPORT_SYMBOL(hci_cmd_sync_queue);
402161510ccSLuiz Augusto von Dentz 
403161510ccSLuiz Augusto von Dentz int hci_update_eir_sync(struct hci_dev *hdev)
404161510ccSLuiz Augusto von Dentz {
405161510ccSLuiz Augusto von Dentz 	struct hci_cp_write_eir cp;
406161510ccSLuiz Augusto von Dentz 
407161510ccSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
408161510ccSLuiz Augusto von Dentz 
409161510ccSLuiz Augusto von Dentz 	if (!hdev_is_powered(hdev))
410161510ccSLuiz Augusto von Dentz 		return 0;
411161510ccSLuiz Augusto von Dentz 
412161510ccSLuiz Augusto von Dentz 	if (!lmp_ext_inq_capable(hdev))
413161510ccSLuiz Augusto von Dentz 		return 0;
414161510ccSLuiz Augusto von Dentz 
415161510ccSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
416161510ccSLuiz Augusto von Dentz 		return 0;
417161510ccSLuiz Augusto von Dentz 
418161510ccSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
419161510ccSLuiz Augusto von Dentz 		return 0;
420161510ccSLuiz Augusto von Dentz 
421161510ccSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
422161510ccSLuiz Augusto von Dentz 
423161510ccSLuiz Augusto von Dentz 	eir_create(hdev, cp.data);
424161510ccSLuiz Augusto von Dentz 
425161510ccSLuiz Augusto von Dentz 	if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
426161510ccSLuiz Augusto von Dentz 		return 0;
427161510ccSLuiz Augusto von Dentz 
428161510ccSLuiz Augusto von Dentz 	memcpy(hdev->eir, cp.data, sizeof(cp.data));
429161510ccSLuiz Augusto von Dentz 
430161510ccSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
431161510ccSLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
432161510ccSLuiz Augusto von Dentz }
433161510ccSLuiz Augusto von Dentz 
434161510ccSLuiz Augusto von Dentz static u8 get_service_classes(struct hci_dev *hdev)
435161510ccSLuiz Augusto von Dentz {
436161510ccSLuiz Augusto von Dentz 	struct bt_uuid *uuid;
437161510ccSLuiz Augusto von Dentz 	u8 val = 0;
438161510ccSLuiz Augusto von Dentz 
439161510ccSLuiz Augusto von Dentz 	list_for_each_entry(uuid, &hdev->uuids, list)
440161510ccSLuiz Augusto von Dentz 		val |= uuid->svc_hint;
441161510ccSLuiz Augusto von Dentz 
442161510ccSLuiz Augusto von Dentz 	return val;
443161510ccSLuiz Augusto von Dentz }
444161510ccSLuiz Augusto von Dentz 
445161510ccSLuiz Augusto von Dentz int hci_update_class_sync(struct hci_dev *hdev)
446161510ccSLuiz Augusto von Dentz {
447161510ccSLuiz Augusto von Dentz 	u8 cod[3];
448161510ccSLuiz Augusto von Dentz 
449161510ccSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
450161510ccSLuiz Augusto von Dentz 
451161510ccSLuiz Augusto von Dentz 	if (!hdev_is_powered(hdev))
452161510ccSLuiz Augusto von Dentz 		return 0;
453161510ccSLuiz Augusto von Dentz 
454161510ccSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
455161510ccSLuiz Augusto von Dentz 		return 0;
456161510ccSLuiz Augusto von Dentz 
457161510ccSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
458161510ccSLuiz Augusto von Dentz 		return 0;
459161510ccSLuiz Augusto von Dentz 
460161510ccSLuiz Augusto von Dentz 	cod[0] = hdev->minor_class;
461161510ccSLuiz Augusto von Dentz 	cod[1] = hdev->major_class;
462161510ccSLuiz Augusto von Dentz 	cod[2] = get_service_classes(hdev);
463161510ccSLuiz Augusto von Dentz 
464161510ccSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
465161510ccSLuiz Augusto von Dentz 		cod[1] |= 0x20;
466161510ccSLuiz Augusto von Dentz 
467161510ccSLuiz Augusto von Dentz 	if (memcmp(cod, hdev->dev_class, 3) == 0)
468161510ccSLuiz Augusto von Dentz 		return 0;
469161510ccSLuiz Augusto von Dentz 
470161510ccSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
471161510ccSLuiz Augusto von Dentz 				     sizeof(cod), cod, HCI_CMD_TIMEOUT);
472161510ccSLuiz Augusto von Dentz }
473cba6b758SLuiz Augusto von Dentz 
474cba6b758SLuiz Augusto von Dentz static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
475cba6b758SLuiz Augusto von Dentz {
476cba6b758SLuiz Augusto von Dentz 	/* If there is no connection we are OK to advertise. */
477cba6b758SLuiz Augusto von Dentz 	if (hci_conn_num(hdev, LE_LINK) == 0)
478cba6b758SLuiz Augusto von Dentz 		return true;
479cba6b758SLuiz Augusto von Dentz 
480cba6b758SLuiz Augusto von Dentz 	/* Check le_states if there is any connection in peripheral role. */
481cba6b758SLuiz Augusto von Dentz 	if (hdev->conn_hash.le_num_peripheral > 0) {
482cba6b758SLuiz Augusto von Dentz 		/* Peripheral connection state and non connectable mode
483cba6b758SLuiz Augusto von Dentz 		 * bit 20.
484cba6b758SLuiz Augusto von Dentz 		 */
485cba6b758SLuiz Augusto von Dentz 		if (!connectable && !(hdev->le_states[2] & 0x10))
486cba6b758SLuiz Augusto von Dentz 			return false;
487cba6b758SLuiz Augusto von Dentz 
488cba6b758SLuiz Augusto von Dentz 		/* Peripheral connection state and connectable mode bit 38
489cba6b758SLuiz Augusto von Dentz 		 * and scannable bit 21.
490cba6b758SLuiz Augusto von Dentz 		 */
491cba6b758SLuiz Augusto von Dentz 		if (connectable && (!(hdev->le_states[4] & 0x40) ||
492cba6b758SLuiz Augusto von Dentz 				    !(hdev->le_states[2] & 0x20)))
493cba6b758SLuiz Augusto von Dentz 			return false;
494cba6b758SLuiz Augusto von Dentz 	}
495cba6b758SLuiz Augusto von Dentz 
496cba6b758SLuiz Augusto von Dentz 	/* Check le_states if there is any connection in central role. */
497cba6b758SLuiz Augusto von Dentz 	if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
498cba6b758SLuiz Augusto von Dentz 		/* Central connection state and non connectable mode bit 18. */
499cba6b758SLuiz Augusto von Dentz 		if (!connectable && !(hdev->le_states[2] & 0x02))
500cba6b758SLuiz Augusto von Dentz 			return false;
501cba6b758SLuiz Augusto von Dentz 
502cba6b758SLuiz Augusto von Dentz 		/* Central connection state and connectable mode bit 35 and
503cba6b758SLuiz Augusto von Dentz 		 * scannable 19.
504cba6b758SLuiz Augusto von Dentz 		 */
505cba6b758SLuiz Augusto von Dentz 		if (connectable && (!(hdev->le_states[4] & 0x08) ||
506cba6b758SLuiz Augusto von Dentz 				    !(hdev->le_states[2] & 0x08)))
507cba6b758SLuiz Augusto von Dentz 			return false;
508cba6b758SLuiz Augusto von Dentz 	}
509cba6b758SLuiz Augusto von Dentz 
510cba6b758SLuiz Augusto von Dentz 	return true;
511cba6b758SLuiz Augusto von Dentz }
512cba6b758SLuiz Augusto von Dentz 
513cba6b758SLuiz Augusto von Dentz static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
514cba6b758SLuiz Augusto von Dentz {
515cba6b758SLuiz Augusto von Dentz 	/* If privacy is not enabled don't use RPA */
516cba6b758SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
517cba6b758SLuiz Augusto von Dentz 		return false;
518cba6b758SLuiz Augusto von Dentz 
519cba6b758SLuiz Augusto von Dentz 	/* If basic privacy mode is enabled use RPA */
520cba6b758SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
521cba6b758SLuiz Augusto von Dentz 		return true;
522cba6b758SLuiz Augusto von Dentz 
523cba6b758SLuiz Augusto von Dentz 	/* If limited privacy mode is enabled don't use RPA if we're
524cba6b758SLuiz Augusto von Dentz 	 * both discoverable and bondable.
525cba6b758SLuiz Augusto von Dentz 	 */
526cba6b758SLuiz Augusto von Dentz 	if ((flags & MGMT_ADV_FLAG_DISCOV) &&
527cba6b758SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_BONDABLE))
528cba6b758SLuiz Augusto von Dentz 		return false;
529cba6b758SLuiz Augusto von Dentz 
530cba6b758SLuiz Augusto von Dentz 	/* We're neither bondable nor discoverable in the limited
531cba6b758SLuiz Augusto von Dentz 	 * privacy mode, therefore use RPA.
532cba6b758SLuiz Augusto von Dentz 	 */
533cba6b758SLuiz Augusto von Dentz 	return true;
534cba6b758SLuiz Augusto von Dentz }
535cba6b758SLuiz Augusto von Dentz 
536cba6b758SLuiz Augusto von Dentz static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
537cba6b758SLuiz Augusto von Dentz {
538cba6b758SLuiz Augusto von Dentz 	/* If we're advertising or initiating an LE connection we can't
539cba6b758SLuiz Augusto von Dentz 	 * go ahead and change the random address at this time. This is
540cba6b758SLuiz Augusto von Dentz 	 * because the eventual initiator address used for the
541cba6b758SLuiz Augusto von Dentz 	 * subsequently created connection will be undefined (some
542cba6b758SLuiz Augusto von Dentz 	 * controllers use the new address and others the one we had
543cba6b758SLuiz Augusto von Dentz 	 * when the operation started).
544cba6b758SLuiz Augusto von Dentz 	 *
545cba6b758SLuiz Augusto von Dentz 	 * In this kind of scenario skip the update and let the random
546cba6b758SLuiz Augusto von Dentz 	 * address be updated at the next cycle.
547cba6b758SLuiz Augusto von Dentz 	 */
548cba6b758SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
549cba6b758SLuiz Augusto von Dentz 	    hci_lookup_le_connect(hdev)) {
550cba6b758SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "Deferring random address update");
551cba6b758SLuiz Augusto von Dentz 		hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
552cba6b758SLuiz Augusto von Dentz 		return 0;
553cba6b758SLuiz Augusto von Dentz 	}
554cba6b758SLuiz Augusto von Dentz 
555cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
556cba6b758SLuiz Augusto von Dentz 				     6, rpa, HCI_CMD_TIMEOUT);
557cba6b758SLuiz Augusto von Dentz }
558cba6b758SLuiz Augusto von Dentz 
559cba6b758SLuiz Augusto von Dentz int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
560cba6b758SLuiz Augusto von Dentz 				   bool rpa, u8 *own_addr_type)
561cba6b758SLuiz Augusto von Dentz {
562cba6b758SLuiz Augusto von Dentz 	int err;
563cba6b758SLuiz Augusto von Dentz 
564cba6b758SLuiz Augusto von Dentz 	/* If privacy is enabled use a resolvable private address. If
565cba6b758SLuiz Augusto von Dentz 	 * current RPA has expired or there is something else than
566cba6b758SLuiz Augusto von Dentz 	 * the current RPA in use, then generate a new one.
567cba6b758SLuiz Augusto von Dentz 	 */
568cba6b758SLuiz Augusto von Dentz 	if (rpa) {
569cba6b758SLuiz Augusto von Dentz 		/* If Controller supports LL Privacy use own address type is
570cba6b758SLuiz Augusto von Dentz 		 * 0x03
571cba6b758SLuiz Augusto von Dentz 		 */
572ad383c2cSLuiz Augusto von Dentz 		if (use_ll_privacy(hdev))
573cba6b758SLuiz Augusto von Dentz 			*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
574cba6b758SLuiz Augusto von Dentz 		else
575cba6b758SLuiz Augusto von Dentz 			*own_addr_type = ADDR_LE_DEV_RANDOM;
576cba6b758SLuiz Augusto von Dentz 
577cba6b758SLuiz Augusto von Dentz 		/* Check if RPA is valid */
578cba6b758SLuiz Augusto von Dentz 		if (rpa_valid(hdev))
579cba6b758SLuiz Augusto von Dentz 			return 0;
580cba6b758SLuiz Augusto von Dentz 
581cba6b758SLuiz Augusto von Dentz 		err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
582cba6b758SLuiz Augusto von Dentz 		if (err < 0) {
583cba6b758SLuiz Augusto von Dentz 			bt_dev_err(hdev, "failed to generate new RPA");
584cba6b758SLuiz Augusto von Dentz 			return err;
585cba6b758SLuiz Augusto von Dentz 		}
586cba6b758SLuiz Augusto von Dentz 
587cba6b758SLuiz Augusto von Dentz 		err = hci_set_random_addr_sync(hdev, &hdev->rpa);
588cba6b758SLuiz Augusto von Dentz 		if (err)
589cba6b758SLuiz Augusto von Dentz 			return err;
590cba6b758SLuiz Augusto von Dentz 
591cba6b758SLuiz Augusto von Dentz 		return 0;
592cba6b758SLuiz Augusto von Dentz 	}
593cba6b758SLuiz Augusto von Dentz 
594cba6b758SLuiz Augusto von Dentz 	/* In case of required privacy without resolvable private address,
595cba6b758SLuiz Augusto von Dentz 	 * use an non-resolvable private address. This is useful for active
596cba6b758SLuiz Augusto von Dentz 	 * scanning and non-connectable advertising.
597cba6b758SLuiz Augusto von Dentz 	 */
598cba6b758SLuiz Augusto von Dentz 	if (require_privacy) {
599cba6b758SLuiz Augusto von Dentz 		bdaddr_t nrpa;
600cba6b758SLuiz Augusto von Dentz 
601cba6b758SLuiz Augusto von Dentz 		while (true) {
602cba6b758SLuiz Augusto von Dentz 			/* The non-resolvable private address is generated
603cba6b758SLuiz Augusto von Dentz 			 * from random six bytes with the two most significant
604cba6b758SLuiz Augusto von Dentz 			 * bits cleared.
605cba6b758SLuiz Augusto von Dentz 			 */
606cba6b758SLuiz Augusto von Dentz 			get_random_bytes(&nrpa, 6);
607cba6b758SLuiz Augusto von Dentz 			nrpa.b[5] &= 0x3f;
608cba6b758SLuiz Augusto von Dentz 
609cba6b758SLuiz Augusto von Dentz 			/* The non-resolvable private address shall not be
610cba6b758SLuiz Augusto von Dentz 			 * equal to the public address.
611cba6b758SLuiz Augusto von Dentz 			 */
612cba6b758SLuiz Augusto von Dentz 			if (bacmp(&hdev->bdaddr, &nrpa))
613cba6b758SLuiz Augusto von Dentz 				break;
614cba6b758SLuiz Augusto von Dentz 		}
615cba6b758SLuiz Augusto von Dentz 
616cba6b758SLuiz Augusto von Dentz 		*own_addr_type = ADDR_LE_DEV_RANDOM;
617cba6b758SLuiz Augusto von Dentz 
618cba6b758SLuiz Augusto von Dentz 		return hci_set_random_addr_sync(hdev, &nrpa);
619cba6b758SLuiz Augusto von Dentz 	}
620cba6b758SLuiz Augusto von Dentz 
621cba6b758SLuiz Augusto von Dentz 	/* If forcing static address is in use or there is no public
622cba6b758SLuiz Augusto von Dentz 	 * address use the static address as random address (but skip
623cba6b758SLuiz Augusto von Dentz 	 * the HCI command if the current random address is already the
624cba6b758SLuiz Augusto von Dentz 	 * static one.
625cba6b758SLuiz Augusto von Dentz 	 *
626cba6b758SLuiz Augusto von Dentz 	 * In case BR/EDR has been disabled on a dual-mode controller
627cba6b758SLuiz Augusto von Dentz 	 * and a static address has been configured, then use that
628cba6b758SLuiz Augusto von Dentz 	 * address instead of the public BR/EDR address.
629cba6b758SLuiz Augusto von Dentz 	 */
630cba6b758SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
631cba6b758SLuiz Augusto von Dentz 	    !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
632cba6b758SLuiz Augusto von Dentz 	    (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
633cba6b758SLuiz Augusto von Dentz 	     bacmp(&hdev->static_addr, BDADDR_ANY))) {
634cba6b758SLuiz Augusto von Dentz 		*own_addr_type = ADDR_LE_DEV_RANDOM;
635cba6b758SLuiz Augusto von Dentz 		if (bacmp(&hdev->static_addr, &hdev->random_addr))
636cba6b758SLuiz Augusto von Dentz 			return hci_set_random_addr_sync(hdev,
637cba6b758SLuiz Augusto von Dentz 							&hdev->static_addr);
638cba6b758SLuiz Augusto von Dentz 		return 0;
639cba6b758SLuiz Augusto von Dentz 	}
640cba6b758SLuiz Augusto von Dentz 
641cba6b758SLuiz Augusto von Dentz 	/* Neither privacy nor static address is being used so use a
642cba6b758SLuiz Augusto von Dentz 	 * public address.
643cba6b758SLuiz Augusto von Dentz 	 */
644cba6b758SLuiz Augusto von Dentz 	*own_addr_type = ADDR_LE_DEV_PUBLIC;
645cba6b758SLuiz Augusto von Dentz 
646cba6b758SLuiz Augusto von Dentz 	return 0;
647cba6b758SLuiz Augusto von Dentz }
648cba6b758SLuiz Augusto von Dentz 
649cba6b758SLuiz Augusto von Dentz static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
650cba6b758SLuiz Augusto von Dentz {
651cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_adv_enable *cp;
652cba6b758SLuiz Augusto von Dentz 	struct hci_cp_ext_adv_set *set;
653cba6b758SLuiz Augusto von Dentz 	u8 data[sizeof(*cp) + sizeof(*set) * 1];
654cba6b758SLuiz Augusto von Dentz 	u8 size;
655cba6b758SLuiz Augusto von Dentz 
656cba6b758SLuiz Augusto von Dentz 	/* If request specifies an instance that doesn't exist, fail */
657cba6b758SLuiz Augusto von Dentz 	if (instance > 0) {
658cba6b758SLuiz Augusto von Dentz 		struct adv_info *adv;
659cba6b758SLuiz Augusto von Dentz 
660cba6b758SLuiz Augusto von Dentz 		adv = hci_find_adv_instance(hdev, instance);
661cba6b758SLuiz Augusto von Dentz 		if (!adv)
662cba6b758SLuiz Augusto von Dentz 			return -EINVAL;
663cba6b758SLuiz Augusto von Dentz 
664cba6b758SLuiz Augusto von Dentz 		/* If not enabled there is nothing to do */
665cba6b758SLuiz Augusto von Dentz 		if (!adv->enabled)
666cba6b758SLuiz Augusto von Dentz 			return 0;
667cba6b758SLuiz Augusto von Dentz 	}
668cba6b758SLuiz Augusto von Dentz 
669cba6b758SLuiz Augusto von Dentz 	memset(data, 0, sizeof(data));
670cba6b758SLuiz Augusto von Dentz 
671cba6b758SLuiz Augusto von Dentz 	cp = (void *)data;
672cba6b758SLuiz Augusto von Dentz 	set = (void *)cp->data;
673cba6b758SLuiz Augusto von Dentz 
674cba6b758SLuiz Augusto von Dentz 	/* Instance 0x00 indicates all advertising instances will be disabled */
675cba6b758SLuiz Augusto von Dentz 	cp->num_of_sets = !!instance;
676cba6b758SLuiz Augusto von Dentz 	cp->enable = 0x00;
677cba6b758SLuiz Augusto von Dentz 
678cba6b758SLuiz Augusto von Dentz 	set->handle = instance;
679cba6b758SLuiz Augusto von Dentz 
680cba6b758SLuiz Augusto von Dentz 	size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
681cba6b758SLuiz Augusto von Dentz 
682cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
683cba6b758SLuiz Augusto von Dentz 				     size, data, HCI_CMD_TIMEOUT);
684cba6b758SLuiz Augusto von Dentz }
685cba6b758SLuiz Augusto von Dentz 
686cba6b758SLuiz Augusto von Dentz static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
687cba6b758SLuiz Augusto von Dentz 					    bdaddr_t *random_addr)
688cba6b758SLuiz Augusto von Dentz {
689cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_adv_set_rand_addr cp;
690cba6b758SLuiz Augusto von Dentz 	int err;
691cba6b758SLuiz Augusto von Dentz 
692cba6b758SLuiz Augusto von Dentz 	if (!instance) {
693cba6b758SLuiz Augusto von Dentz 		/* Instance 0x00 doesn't have an adv_info, instead it uses
694cba6b758SLuiz Augusto von Dentz 		 * hdev->random_addr to track its address so whenever it needs
695cba6b758SLuiz Augusto von Dentz 		 * to be updated this also set the random address since
696cba6b758SLuiz Augusto von Dentz 		 * hdev->random_addr is shared with scan state machine.
697cba6b758SLuiz Augusto von Dentz 		 */
698cba6b758SLuiz Augusto von Dentz 		err = hci_set_random_addr_sync(hdev, random_addr);
699cba6b758SLuiz Augusto von Dentz 		if (err)
700cba6b758SLuiz Augusto von Dentz 			return err;
701cba6b758SLuiz Augusto von Dentz 	}
702cba6b758SLuiz Augusto von Dentz 
703cba6b758SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
704cba6b758SLuiz Augusto von Dentz 
705cba6b758SLuiz Augusto von Dentz 	cp.handle = instance;
706cba6b758SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, random_addr);
707cba6b758SLuiz Augusto von Dentz 
708cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
709cba6b758SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
710cba6b758SLuiz Augusto von Dentz }
711cba6b758SLuiz Augusto von Dentz 
712cba6b758SLuiz Augusto von Dentz int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
713cba6b758SLuiz Augusto von Dentz {
714cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_adv_params cp;
715cba6b758SLuiz Augusto von Dentz 	bool connectable;
716cba6b758SLuiz Augusto von Dentz 	u32 flags;
717cba6b758SLuiz Augusto von Dentz 	bdaddr_t random_addr;
718cba6b758SLuiz Augusto von Dentz 	u8 own_addr_type;
719cba6b758SLuiz Augusto von Dentz 	int err;
720cba6b758SLuiz Augusto von Dentz 	struct adv_info *adv;
721cba6b758SLuiz Augusto von Dentz 	bool secondary_adv;
722cba6b758SLuiz Augusto von Dentz 
723cba6b758SLuiz Augusto von Dentz 	if (instance > 0) {
724cba6b758SLuiz Augusto von Dentz 		adv = hci_find_adv_instance(hdev, instance);
725cba6b758SLuiz Augusto von Dentz 		if (!adv)
726cba6b758SLuiz Augusto von Dentz 			return -EINVAL;
727cba6b758SLuiz Augusto von Dentz 	} else {
728cba6b758SLuiz Augusto von Dentz 		adv = NULL;
729cba6b758SLuiz Augusto von Dentz 	}
730cba6b758SLuiz Augusto von Dentz 
731cba6b758SLuiz Augusto von Dentz 	/* Updating parameters of an active instance will return a
732cba6b758SLuiz Augusto von Dentz 	 * Command Disallowed error, so we must first disable the
733cba6b758SLuiz Augusto von Dentz 	 * instance if it is active.
734cba6b758SLuiz Augusto von Dentz 	 */
735cba6b758SLuiz Augusto von Dentz 	if (adv && !adv->pending) {
736cba6b758SLuiz Augusto von Dentz 		err = hci_disable_ext_adv_instance_sync(hdev, instance);
737cba6b758SLuiz Augusto von Dentz 		if (err)
738cba6b758SLuiz Augusto von Dentz 			return err;
739cba6b758SLuiz Augusto von Dentz 	}
740cba6b758SLuiz Augusto von Dentz 
741cba6b758SLuiz Augusto von Dentz 	flags = hci_adv_instance_flags(hdev, instance);
742cba6b758SLuiz Augusto von Dentz 
743cba6b758SLuiz Augusto von Dentz 	/* If the "connectable" instance flag was not set, then choose between
744cba6b758SLuiz Augusto von Dentz 	 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
745cba6b758SLuiz Augusto von Dentz 	 */
746cba6b758SLuiz Augusto von Dentz 	connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
747cba6b758SLuiz Augusto von Dentz 		      mgmt_get_connectable(hdev);
748cba6b758SLuiz Augusto von Dentz 
749cba6b758SLuiz Augusto von Dentz 	if (!is_advertising_allowed(hdev, connectable))
750cba6b758SLuiz Augusto von Dentz 		return -EPERM;
751cba6b758SLuiz Augusto von Dentz 
752cba6b758SLuiz Augusto von Dentz 	/* Set require_privacy to true only when non-connectable
753cba6b758SLuiz Augusto von Dentz 	 * advertising is used. In that case it is fine to use a
754cba6b758SLuiz Augusto von Dentz 	 * non-resolvable private address.
755cba6b758SLuiz Augusto von Dentz 	 */
756cba6b758SLuiz Augusto von Dentz 	err = hci_get_random_address(hdev, !connectable,
757cba6b758SLuiz Augusto von Dentz 				     adv_use_rpa(hdev, flags), adv,
758cba6b758SLuiz Augusto von Dentz 				     &own_addr_type, &random_addr);
759cba6b758SLuiz Augusto von Dentz 	if (err < 0)
760cba6b758SLuiz Augusto von Dentz 		return err;
761cba6b758SLuiz Augusto von Dentz 
762cba6b758SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
763cba6b758SLuiz Augusto von Dentz 
764cba6b758SLuiz Augusto von Dentz 	if (adv) {
765cba6b758SLuiz Augusto von Dentz 		hci_cpu_to_le24(adv->min_interval, cp.min_interval);
766cba6b758SLuiz Augusto von Dentz 		hci_cpu_to_le24(adv->max_interval, cp.max_interval);
767cba6b758SLuiz Augusto von Dentz 		cp.tx_power = adv->tx_power;
768cba6b758SLuiz Augusto von Dentz 	} else {
769cba6b758SLuiz Augusto von Dentz 		hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
770cba6b758SLuiz Augusto von Dentz 		hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
771cba6b758SLuiz Augusto von Dentz 		cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
772cba6b758SLuiz Augusto von Dentz 	}
773cba6b758SLuiz Augusto von Dentz 
774cba6b758SLuiz Augusto von Dentz 	secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
775cba6b758SLuiz Augusto von Dentz 
776cba6b758SLuiz Augusto von Dentz 	if (connectable) {
777cba6b758SLuiz Augusto von Dentz 		if (secondary_adv)
778cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
779cba6b758SLuiz Augusto von Dentz 		else
780cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
781cba6b758SLuiz Augusto von Dentz 	} else if (hci_adv_instance_is_scannable(hdev, instance) ||
782cba6b758SLuiz Augusto von Dentz 		   (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
783cba6b758SLuiz Augusto von Dentz 		if (secondary_adv)
784cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
785cba6b758SLuiz Augusto von Dentz 		else
786cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
787cba6b758SLuiz Augusto von Dentz 	} else {
788cba6b758SLuiz Augusto von Dentz 		if (secondary_adv)
789cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
790cba6b758SLuiz Augusto von Dentz 		else
791cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
792cba6b758SLuiz Augusto von Dentz 	}
793cba6b758SLuiz Augusto von Dentz 
794cf75ad8bSLuiz Augusto von Dentz 	/* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
795cf75ad8bSLuiz Augusto von Dentz 	 * contains the peer’s Identity Address and the Peer_Address_Type
796cf75ad8bSLuiz Augusto von Dentz 	 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
797cf75ad8bSLuiz Augusto von Dentz 	 * These parameters are used to locate the corresponding local IRK in
798cf75ad8bSLuiz Augusto von Dentz 	 * the resolving list; this IRK is used to generate their own address
799cf75ad8bSLuiz Augusto von Dentz 	 * used in the advertisement.
800cf75ad8bSLuiz Augusto von Dentz 	 */
801cf75ad8bSLuiz Augusto von Dentz 	if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
802cf75ad8bSLuiz Augusto von Dentz 		hci_copy_identity_address(hdev, &cp.peer_addr,
803cf75ad8bSLuiz Augusto von Dentz 					  &cp.peer_addr_type);
804cf75ad8bSLuiz Augusto von Dentz 
805cba6b758SLuiz Augusto von Dentz 	cp.own_addr_type = own_addr_type;
806cba6b758SLuiz Augusto von Dentz 	cp.channel_map = hdev->le_adv_channel_map;
807cba6b758SLuiz Augusto von Dentz 	cp.handle = instance;
808cba6b758SLuiz Augusto von Dentz 
809cba6b758SLuiz Augusto von Dentz 	if (flags & MGMT_ADV_FLAG_SEC_2M) {
810cba6b758SLuiz Augusto von Dentz 		cp.primary_phy = HCI_ADV_PHY_1M;
811cba6b758SLuiz Augusto von Dentz 		cp.secondary_phy = HCI_ADV_PHY_2M;
812cba6b758SLuiz Augusto von Dentz 	} else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
813cba6b758SLuiz Augusto von Dentz 		cp.primary_phy = HCI_ADV_PHY_CODED;
814cba6b758SLuiz Augusto von Dentz 		cp.secondary_phy = HCI_ADV_PHY_CODED;
815cba6b758SLuiz Augusto von Dentz 	} else {
816cba6b758SLuiz Augusto von Dentz 		/* In all other cases use 1M */
817cba6b758SLuiz Augusto von Dentz 		cp.primary_phy = HCI_ADV_PHY_1M;
818cba6b758SLuiz Augusto von Dentz 		cp.secondary_phy = HCI_ADV_PHY_1M;
819cba6b758SLuiz Augusto von Dentz 	}
820cba6b758SLuiz Augusto von Dentz 
821cba6b758SLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
822cba6b758SLuiz Augusto von Dentz 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
823cba6b758SLuiz Augusto von Dentz 	if (err)
824cba6b758SLuiz Augusto von Dentz 		return err;
825cba6b758SLuiz Augusto von Dentz 
826cba6b758SLuiz Augusto von Dentz 	if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
827cba6b758SLuiz Augusto von Dentz 	     own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
828cba6b758SLuiz Augusto von Dentz 	    bacmp(&random_addr, BDADDR_ANY)) {
829cba6b758SLuiz Augusto von Dentz 		/* Check if random address need to be updated */
830cba6b758SLuiz Augusto von Dentz 		if (adv) {
831cba6b758SLuiz Augusto von Dentz 			if (!bacmp(&random_addr, &adv->random_addr))
832cba6b758SLuiz Augusto von Dentz 				return 0;
833cba6b758SLuiz Augusto von Dentz 		} else {
834cba6b758SLuiz Augusto von Dentz 			if (!bacmp(&random_addr, &hdev->random_addr))
835cba6b758SLuiz Augusto von Dentz 				return 0;
836cba6b758SLuiz Augusto von Dentz 		}
837cba6b758SLuiz Augusto von Dentz 
838cba6b758SLuiz Augusto von Dentz 		return hci_set_adv_set_random_addr_sync(hdev, instance,
839cba6b758SLuiz Augusto von Dentz 							&random_addr);
840cba6b758SLuiz Augusto von Dentz 	}
841cba6b758SLuiz Augusto von Dentz 
842cba6b758SLuiz Augusto von Dentz 	return 0;
843cba6b758SLuiz Augusto von Dentz }
844cba6b758SLuiz Augusto von Dentz 
845cba6b758SLuiz Augusto von Dentz static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
846cba6b758SLuiz Augusto von Dentz {
847cba6b758SLuiz Augusto von Dentz 	struct {
848cba6b758SLuiz Augusto von Dentz 		struct hci_cp_le_set_ext_scan_rsp_data cp;
849cba6b758SLuiz Augusto von Dentz 		u8 data[HCI_MAX_EXT_AD_LENGTH];
850cba6b758SLuiz Augusto von Dentz 	} pdu;
851cba6b758SLuiz Augusto von Dentz 	u8 len;
852cba6b758SLuiz Augusto von Dentz 
853cba6b758SLuiz Augusto von Dentz 	memset(&pdu, 0, sizeof(pdu));
854cba6b758SLuiz Augusto von Dentz 
855cba6b758SLuiz Augusto von Dentz 	len = eir_create_scan_rsp(hdev, instance, pdu.data);
856cba6b758SLuiz Augusto von Dentz 
857cba6b758SLuiz Augusto von Dentz 	if (hdev->scan_rsp_data_len == len &&
858cba6b758SLuiz Augusto von Dentz 	    !memcmp(pdu.data, hdev->scan_rsp_data, len))
859cba6b758SLuiz Augusto von Dentz 		return 0;
860cba6b758SLuiz Augusto von Dentz 
861cba6b758SLuiz Augusto von Dentz 	memcpy(hdev->scan_rsp_data, pdu.data, len);
862cba6b758SLuiz Augusto von Dentz 	hdev->scan_rsp_data_len = len;
863cba6b758SLuiz Augusto von Dentz 
864cba6b758SLuiz Augusto von Dentz 	pdu.cp.handle = instance;
865cba6b758SLuiz Augusto von Dentz 	pdu.cp.length = len;
866cba6b758SLuiz Augusto von Dentz 	pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
867cba6b758SLuiz Augusto von Dentz 	pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
868cba6b758SLuiz Augusto von Dentz 
869cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
870cba6b758SLuiz Augusto von Dentz 				     sizeof(pdu.cp) + len, &pdu.cp,
871cba6b758SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
872cba6b758SLuiz Augusto von Dentz }
873cba6b758SLuiz Augusto von Dentz 
874cba6b758SLuiz Augusto von Dentz static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
875cba6b758SLuiz Augusto von Dentz {
876cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_scan_rsp_data cp;
877cba6b758SLuiz Augusto von Dentz 	u8 len;
878cba6b758SLuiz Augusto von Dentz 
879cba6b758SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
880cba6b758SLuiz Augusto von Dentz 
881cba6b758SLuiz Augusto von Dentz 	len = eir_create_scan_rsp(hdev, instance, cp.data);
882cba6b758SLuiz Augusto von Dentz 
883cba6b758SLuiz Augusto von Dentz 	if (hdev->scan_rsp_data_len == len &&
884cba6b758SLuiz Augusto von Dentz 	    !memcmp(cp.data, hdev->scan_rsp_data, len))
885cba6b758SLuiz Augusto von Dentz 		return 0;
886cba6b758SLuiz Augusto von Dentz 
887cba6b758SLuiz Augusto von Dentz 	memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
888cba6b758SLuiz Augusto von Dentz 	hdev->scan_rsp_data_len = len;
889cba6b758SLuiz Augusto von Dentz 
890cba6b758SLuiz Augusto von Dentz 	cp.length = len;
891cba6b758SLuiz Augusto von Dentz 
892cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
893cba6b758SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
894cba6b758SLuiz Augusto von Dentz }
895cba6b758SLuiz Augusto von Dentz 
896cba6b758SLuiz Augusto von Dentz int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
897cba6b758SLuiz Augusto von Dentz {
898cba6b758SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
899cba6b758SLuiz Augusto von Dentz 		return 0;
900cba6b758SLuiz Augusto von Dentz 
901cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
902cba6b758SLuiz Augusto von Dentz 		return hci_set_ext_scan_rsp_data_sync(hdev, instance);
903cba6b758SLuiz Augusto von Dentz 
904cba6b758SLuiz Augusto von Dentz 	return __hci_set_scan_rsp_data_sync(hdev, instance);
905cba6b758SLuiz Augusto von Dentz }
906cba6b758SLuiz Augusto von Dentz 
907cba6b758SLuiz Augusto von Dentz int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
908cba6b758SLuiz Augusto von Dentz {
909cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_adv_enable *cp;
910cba6b758SLuiz Augusto von Dentz 	struct hci_cp_ext_adv_set *set;
911cba6b758SLuiz Augusto von Dentz 	u8 data[sizeof(*cp) + sizeof(*set) * 1];
912cba6b758SLuiz Augusto von Dentz 	struct adv_info *adv;
913cba6b758SLuiz Augusto von Dentz 
914cba6b758SLuiz Augusto von Dentz 	if (instance > 0) {
915cba6b758SLuiz Augusto von Dentz 		adv = hci_find_adv_instance(hdev, instance);
916cba6b758SLuiz Augusto von Dentz 		if (!adv)
917cba6b758SLuiz Augusto von Dentz 			return -EINVAL;
918cba6b758SLuiz Augusto von Dentz 		/* If already enabled there is nothing to do */
919cba6b758SLuiz Augusto von Dentz 		if (adv->enabled)
920cba6b758SLuiz Augusto von Dentz 			return 0;
921cba6b758SLuiz Augusto von Dentz 	} else {
922cba6b758SLuiz Augusto von Dentz 		adv = NULL;
923cba6b758SLuiz Augusto von Dentz 	}
924cba6b758SLuiz Augusto von Dentz 
925cba6b758SLuiz Augusto von Dentz 	cp = (void *)data;
926cba6b758SLuiz Augusto von Dentz 	set = (void *)cp->data;
927cba6b758SLuiz Augusto von Dentz 
928cba6b758SLuiz Augusto von Dentz 	memset(cp, 0, sizeof(*cp));
929cba6b758SLuiz Augusto von Dentz 
930cba6b758SLuiz Augusto von Dentz 	cp->enable = 0x01;
931cba6b758SLuiz Augusto von Dentz 	cp->num_of_sets = 0x01;
932cba6b758SLuiz Augusto von Dentz 
933cba6b758SLuiz Augusto von Dentz 	memset(set, 0, sizeof(*set));
934cba6b758SLuiz Augusto von Dentz 
935cba6b758SLuiz Augusto von Dentz 	set->handle = instance;
936cba6b758SLuiz Augusto von Dentz 
937cba6b758SLuiz Augusto von Dentz 	/* Set duration per instance since controller is responsible for
938cba6b758SLuiz Augusto von Dentz 	 * scheduling it.
939cba6b758SLuiz Augusto von Dentz 	 */
940f16a491cSLuiz Augusto von Dentz 	if (adv && adv->timeout) {
941cba6b758SLuiz Augusto von Dentz 		u16 duration = adv->timeout * MSEC_PER_SEC;
942cba6b758SLuiz Augusto von Dentz 
943cba6b758SLuiz Augusto von Dentz 		/* Time = N * 10 ms */
944cba6b758SLuiz Augusto von Dentz 		set->duration = cpu_to_le16(duration / 10);
945cba6b758SLuiz Augusto von Dentz 	}
946cba6b758SLuiz Augusto von Dentz 
947cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
948cba6b758SLuiz Augusto von Dentz 				     sizeof(*cp) +
949cba6b758SLuiz Augusto von Dentz 				     sizeof(*set) * cp->num_of_sets,
950cba6b758SLuiz Augusto von Dentz 				     data, HCI_CMD_TIMEOUT);
951cba6b758SLuiz Augusto von Dentz }
952cba6b758SLuiz Augusto von Dentz 
953cba6b758SLuiz Augusto von Dentz int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
954cba6b758SLuiz Augusto von Dentz {
955cba6b758SLuiz Augusto von Dentz 	int err;
956cba6b758SLuiz Augusto von Dentz 
957cba6b758SLuiz Augusto von Dentz 	err = hci_setup_ext_adv_instance_sync(hdev, instance);
958cba6b758SLuiz Augusto von Dentz 	if (err)
959cba6b758SLuiz Augusto von Dentz 		return err;
960cba6b758SLuiz Augusto von Dentz 
961cba6b758SLuiz Augusto von Dentz 	err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
962cba6b758SLuiz Augusto von Dentz 	if (err)
963cba6b758SLuiz Augusto von Dentz 		return err;
964cba6b758SLuiz Augusto von Dentz 
965cba6b758SLuiz Augusto von Dentz 	return hci_enable_ext_advertising_sync(hdev, instance);
966cba6b758SLuiz Augusto von Dentz }
967cba6b758SLuiz Augusto von Dentz 
968cba6b758SLuiz Augusto von Dentz static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
969cba6b758SLuiz Augusto von Dentz {
970cba6b758SLuiz Augusto von Dentz 	int err;
971cba6b758SLuiz Augusto von Dentz 
972cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
973cba6b758SLuiz Augusto von Dentz 		return hci_start_ext_adv_sync(hdev, instance);
974cba6b758SLuiz Augusto von Dentz 
975cba6b758SLuiz Augusto von Dentz 	err = hci_update_adv_data_sync(hdev, instance);
976cba6b758SLuiz Augusto von Dentz 	if (err)
977cba6b758SLuiz Augusto von Dentz 		return err;
978cba6b758SLuiz Augusto von Dentz 
979cba6b758SLuiz Augusto von Dentz 	err = hci_update_scan_rsp_data_sync(hdev, instance);
980cba6b758SLuiz Augusto von Dentz 	if (err)
981cba6b758SLuiz Augusto von Dentz 		return err;
982cba6b758SLuiz Augusto von Dentz 
983cba6b758SLuiz Augusto von Dentz 	return hci_enable_advertising_sync(hdev);
984cba6b758SLuiz Augusto von Dentz }
985cba6b758SLuiz Augusto von Dentz 
986cba6b758SLuiz Augusto von Dentz int hci_enable_advertising_sync(struct hci_dev *hdev)
987cba6b758SLuiz Augusto von Dentz {
988cba6b758SLuiz Augusto von Dentz 	struct adv_info *adv_instance;
989cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_adv_param cp;
990cba6b758SLuiz Augusto von Dentz 	u8 own_addr_type, enable = 0x01;
991cba6b758SLuiz Augusto von Dentz 	bool connectable;
992cba6b758SLuiz Augusto von Dentz 	u16 adv_min_interval, adv_max_interval;
993cba6b758SLuiz Augusto von Dentz 	u32 flags;
994cba6b758SLuiz Augusto von Dentz 	u8 status;
995cba6b758SLuiz Augusto von Dentz 
996ad383c2cSLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
997ad383c2cSLuiz Augusto von Dentz 		return hci_enable_ext_advertising_sync(hdev,
998ad383c2cSLuiz Augusto von Dentz 						       hdev->cur_adv_instance);
999ad383c2cSLuiz Augusto von Dentz 
1000cba6b758SLuiz Augusto von Dentz 	flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1001cba6b758SLuiz Augusto von Dentz 	adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1002cba6b758SLuiz Augusto von Dentz 
1003cba6b758SLuiz Augusto von Dentz 	/* If the "connectable" instance flag was not set, then choose between
1004cba6b758SLuiz Augusto von Dentz 	 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1005cba6b758SLuiz Augusto von Dentz 	 */
1006cba6b758SLuiz Augusto von Dentz 	connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1007cba6b758SLuiz Augusto von Dentz 		      mgmt_get_connectable(hdev);
1008cba6b758SLuiz Augusto von Dentz 
1009cba6b758SLuiz Augusto von Dentz 	if (!is_advertising_allowed(hdev, connectable))
1010cba6b758SLuiz Augusto von Dentz 		return -EINVAL;
1011cba6b758SLuiz Augusto von Dentz 
1012cba6b758SLuiz Augusto von Dentz 	status = hci_disable_advertising_sync(hdev);
1013cba6b758SLuiz Augusto von Dentz 	if (status)
1014cba6b758SLuiz Augusto von Dentz 		return status;
1015cba6b758SLuiz Augusto von Dentz 
1016cba6b758SLuiz Augusto von Dentz 	/* Clear the HCI_LE_ADV bit temporarily so that the
1017cba6b758SLuiz Augusto von Dentz 	 * hci_update_random_address knows that it's safe to go ahead
1018cba6b758SLuiz Augusto von Dentz 	 * and write a new random address. The flag will be set back on
1019cba6b758SLuiz Augusto von Dentz 	 * as soon as the SET_ADV_ENABLE HCI command completes.
1020cba6b758SLuiz Augusto von Dentz 	 */
1021cba6b758SLuiz Augusto von Dentz 	hci_dev_clear_flag(hdev, HCI_LE_ADV);
1022cba6b758SLuiz Augusto von Dentz 
1023cba6b758SLuiz Augusto von Dentz 	/* Set require_privacy to true only when non-connectable
1024cba6b758SLuiz Augusto von Dentz 	 * advertising is used. In that case it is fine to use a
1025cba6b758SLuiz Augusto von Dentz 	 * non-resolvable private address.
1026cba6b758SLuiz Augusto von Dentz 	 */
1027cba6b758SLuiz Augusto von Dentz 	status = hci_update_random_address_sync(hdev, !connectable,
1028cba6b758SLuiz Augusto von Dentz 						adv_use_rpa(hdev, flags),
1029cba6b758SLuiz Augusto von Dentz 						&own_addr_type);
1030cba6b758SLuiz Augusto von Dentz 	if (status)
1031cba6b758SLuiz Augusto von Dentz 		return status;
1032cba6b758SLuiz Augusto von Dentz 
1033cba6b758SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1034cba6b758SLuiz Augusto von Dentz 
1035cba6b758SLuiz Augusto von Dentz 	if (adv_instance) {
1036cba6b758SLuiz Augusto von Dentz 		adv_min_interval = adv_instance->min_interval;
1037cba6b758SLuiz Augusto von Dentz 		adv_max_interval = adv_instance->max_interval;
1038cba6b758SLuiz Augusto von Dentz 	} else {
1039cba6b758SLuiz Augusto von Dentz 		adv_min_interval = hdev->le_adv_min_interval;
1040cba6b758SLuiz Augusto von Dentz 		adv_max_interval = hdev->le_adv_max_interval;
1041cba6b758SLuiz Augusto von Dentz 	}
1042cba6b758SLuiz Augusto von Dentz 
1043cba6b758SLuiz Augusto von Dentz 	if (connectable) {
1044cba6b758SLuiz Augusto von Dentz 		cp.type = LE_ADV_IND;
1045cba6b758SLuiz Augusto von Dentz 	} else {
1046cba6b758SLuiz Augusto von Dentz 		if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1047cba6b758SLuiz Augusto von Dentz 			cp.type = LE_ADV_SCAN_IND;
1048cba6b758SLuiz Augusto von Dentz 		else
1049cba6b758SLuiz Augusto von Dentz 			cp.type = LE_ADV_NONCONN_IND;
1050cba6b758SLuiz Augusto von Dentz 
1051cba6b758SLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1052cba6b758SLuiz Augusto von Dentz 		    hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1053cba6b758SLuiz Augusto von Dentz 			adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1054cba6b758SLuiz Augusto von Dentz 			adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1055cba6b758SLuiz Augusto von Dentz 		}
1056cba6b758SLuiz Augusto von Dentz 	}
1057cba6b758SLuiz Augusto von Dentz 
1058cba6b758SLuiz Augusto von Dentz 	cp.min_interval = cpu_to_le16(adv_min_interval);
1059cba6b758SLuiz Augusto von Dentz 	cp.max_interval = cpu_to_le16(adv_max_interval);
1060cba6b758SLuiz Augusto von Dentz 	cp.own_address_type = own_addr_type;
1061cba6b758SLuiz Augusto von Dentz 	cp.channel_map = hdev->le_adv_channel_map;
1062cba6b758SLuiz Augusto von Dentz 
1063cba6b758SLuiz Augusto von Dentz 	status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1064cba6b758SLuiz Augusto von Dentz 				       sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1065cba6b758SLuiz Augusto von Dentz 	if (status)
1066cba6b758SLuiz Augusto von Dentz 		return status;
1067cba6b758SLuiz Augusto von Dentz 
1068cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1069cba6b758SLuiz Augusto von Dentz 				     sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1070cba6b758SLuiz Augusto von Dentz }
1071cba6b758SLuiz Augusto von Dentz 
1072abfeea47SLuiz Augusto von Dentz static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1073abfeea47SLuiz Augusto von Dentz {
1074abfeea47SLuiz Augusto von Dentz 	return hci_enable_advertising_sync(hdev);
1075abfeea47SLuiz Augusto von Dentz }
1076abfeea47SLuiz Augusto von Dentz 
1077abfeea47SLuiz Augusto von Dentz int hci_enable_advertising(struct hci_dev *hdev)
1078abfeea47SLuiz Augusto von Dentz {
1079abfeea47SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1080abfeea47SLuiz Augusto von Dentz 	    list_empty(&hdev->adv_instances))
1081abfeea47SLuiz Augusto von Dentz 		return 0;
1082abfeea47SLuiz Augusto von Dentz 
1083abfeea47SLuiz Augusto von Dentz 	return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1084abfeea47SLuiz Augusto von Dentz }
1085abfeea47SLuiz Augusto von Dentz 
1086abfeea47SLuiz Augusto von Dentz int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1087cba6b758SLuiz Augusto von Dentz 				     struct sock *sk)
1088cba6b758SLuiz Augusto von Dentz {
1089cba6b758SLuiz Augusto von Dentz 	int err;
1090cba6b758SLuiz Augusto von Dentz 
1091cba6b758SLuiz Augusto von Dentz 	if (!ext_adv_capable(hdev))
1092cba6b758SLuiz Augusto von Dentz 		return 0;
1093cba6b758SLuiz Augusto von Dentz 
1094cba6b758SLuiz Augusto von Dentz 	err = hci_disable_ext_adv_instance_sync(hdev, instance);
1095cba6b758SLuiz Augusto von Dentz 	if (err)
1096cba6b758SLuiz Augusto von Dentz 		return err;
1097cba6b758SLuiz Augusto von Dentz 
1098cba6b758SLuiz Augusto von Dentz 	/* If request specifies an instance that doesn't exist, fail */
1099cba6b758SLuiz Augusto von Dentz 	if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1100cba6b758SLuiz Augusto von Dentz 		return -EINVAL;
1101cba6b758SLuiz Augusto von Dentz 
1102cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1103cba6b758SLuiz Augusto von Dentz 					sizeof(instance), &instance, 0,
1104cba6b758SLuiz Augusto von Dentz 					HCI_CMD_TIMEOUT, sk);
1105cba6b758SLuiz Augusto von Dentz }
1106cba6b758SLuiz Augusto von Dentz 
1107cba6b758SLuiz Augusto von Dentz static void cancel_adv_timeout(struct hci_dev *hdev)
1108cba6b758SLuiz Augusto von Dentz {
1109cba6b758SLuiz Augusto von Dentz 	if (hdev->adv_instance_timeout) {
1110cba6b758SLuiz Augusto von Dentz 		hdev->adv_instance_timeout = 0;
1111cba6b758SLuiz Augusto von Dentz 		cancel_delayed_work(&hdev->adv_instance_expire);
1112cba6b758SLuiz Augusto von Dentz 	}
1113cba6b758SLuiz Augusto von Dentz }
1114cba6b758SLuiz Augusto von Dentz 
1115cba6b758SLuiz Augusto von Dentz static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1116cba6b758SLuiz Augusto von Dentz {
1117cba6b758SLuiz Augusto von Dentz 	struct {
1118cba6b758SLuiz Augusto von Dentz 		struct hci_cp_le_set_ext_adv_data cp;
1119cba6b758SLuiz Augusto von Dentz 		u8 data[HCI_MAX_EXT_AD_LENGTH];
1120cba6b758SLuiz Augusto von Dentz 	} pdu;
1121cba6b758SLuiz Augusto von Dentz 	u8 len;
1122cba6b758SLuiz Augusto von Dentz 
1123cba6b758SLuiz Augusto von Dentz 	memset(&pdu, 0, sizeof(pdu));
1124cba6b758SLuiz Augusto von Dentz 
1125cba6b758SLuiz Augusto von Dentz 	len = eir_create_adv_data(hdev, instance, pdu.data);
1126cba6b758SLuiz Augusto von Dentz 
1127cba6b758SLuiz Augusto von Dentz 	/* There's nothing to do if the data hasn't changed */
1128cba6b758SLuiz Augusto von Dentz 	if (hdev->adv_data_len == len &&
1129cba6b758SLuiz Augusto von Dentz 	    memcmp(pdu.data, hdev->adv_data, len) == 0)
1130cba6b758SLuiz Augusto von Dentz 		return 0;
1131cba6b758SLuiz Augusto von Dentz 
1132cba6b758SLuiz Augusto von Dentz 	memcpy(hdev->adv_data, pdu.data, len);
1133cba6b758SLuiz Augusto von Dentz 	hdev->adv_data_len = len;
1134cba6b758SLuiz Augusto von Dentz 
1135cba6b758SLuiz Augusto von Dentz 	pdu.cp.length = len;
1136cba6b758SLuiz Augusto von Dentz 	pdu.cp.handle = instance;
1137cba6b758SLuiz Augusto von Dentz 	pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1138cba6b758SLuiz Augusto von Dentz 	pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1139cba6b758SLuiz Augusto von Dentz 
1140cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1141cba6b758SLuiz Augusto von Dentz 				     sizeof(pdu.cp) + len, &pdu.cp,
1142cba6b758SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
1143cba6b758SLuiz Augusto von Dentz }
1144cba6b758SLuiz Augusto von Dentz 
1145cba6b758SLuiz Augusto von Dentz static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1146cba6b758SLuiz Augusto von Dentz {
1147cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_adv_data cp;
1148cba6b758SLuiz Augusto von Dentz 	u8 len;
1149cba6b758SLuiz Augusto von Dentz 
1150cba6b758SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1151cba6b758SLuiz Augusto von Dentz 
1152cba6b758SLuiz Augusto von Dentz 	len = eir_create_adv_data(hdev, instance, cp.data);
1153cba6b758SLuiz Augusto von Dentz 
1154cba6b758SLuiz Augusto von Dentz 	/* There's nothing to do if the data hasn't changed */
1155cba6b758SLuiz Augusto von Dentz 	if (hdev->adv_data_len == len &&
1156cba6b758SLuiz Augusto von Dentz 	    memcmp(cp.data, hdev->adv_data, len) == 0)
1157cba6b758SLuiz Augusto von Dentz 		return 0;
1158cba6b758SLuiz Augusto von Dentz 
1159cba6b758SLuiz Augusto von Dentz 	memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1160cba6b758SLuiz Augusto von Dentz 	hdev->adv_data_len = len;
1161cba6b758SLuiz Augusto von Dentz 
1162cba6b758SLuiz Augusto von Dentz 	cp.length = len;
1163cba6b758SLuiz Augusto von Dentz 
1164cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1165cba6b758SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1166cba6b758SLuiz Augusto von Dentz }
1167cba6b758SLuiz Augusto von Dentz 
1168cba6b758SLuiz Augusto von Dentz int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1169cba6b758SLuiz Augusto von Dentz {
1170cba6b758SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1171cba6b758SLuiz Augusto von Dentz 		return 0;
1172cba6b758SLuiz Augusto von Dentz 
1173cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1174cba6b758SLuiz Augusto von Dentz 		return hci_set_ext_adv_data_sync(hdev, instance);
1175cba6b758SLuiz Augusto von Dentz 
1176cba6b758SLuiz Augusto von Dentz 	return hci_set_adv_data_sync(hdev, instance);
1177cba6b758SLuiz Augusto von Dentz }
1178cba6b758SLuiz Augusto von Dentz 
1179cba6b758SLuiz Augusto von Dentz int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1180cba6b758SLuiz Augusto von Dentz 				   bool force)
1181cba6b758SLuiz Augusto von Dentz {
1182cba6b758SLuiz Augusto von Dentz 	struct adv_info *adv = NULL;
1183cba6b758SLuiz Augusto von Dentz 	u16 timeout;
1184cba6b758SLuiz Augusto von Dentz 
1185cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1186cba6b758SLuiz Augusto von Dentz 		return -EPERM;
1187cba6b758SLuiz Augusto von Dentz 
1188cba6b758SLuiz Augusto von Dentz 	if (hdev->adv_instance_timeout)
1189cba6b758SLuiz Augusto von Dentz 		return -EBUSY;
1190cba6b758SLuiz Augusto von Dentz 
1191cba6b758SLuiz Augusto von Dentz 	adv = hci_find_adv_instance(hdev, instance);
1192cba6b758SLuiz Augusto von Dentz 	if (!adv)
1193cba6b758SLuiz Augusto von Dentz 		return -ENOENT;
1194cba6b758SLuiz Augusto von Dentz 
1195cba6b758SLuiz Augusto von Dentz 	/* A zero timeout means unlimited advertising. As long as there is
1196cba6b758SLuiz Augusto von Dentz 	 * only one instance, duration should be ignored. We still set a timeout
1197cba6b758SLuiz Augusto von Dentz 	 * in case further instances are being added later on.
1198cba6b758SLuiz Augusto von Dentz 	 *
1199cba6b758SLuiz Augusto von Dentz 	 * If the remaining lifetime of the instance is more than the duration
1200cba6b758SLuiz Augusto von Dentz 	 * then the timeout corresponds to the duration, otherwise it will be
1201cba6b758SLuiz Augusto von Dentz 	 * reduced to the remaining instance lifetime.
1202cba6b758SLuiz Augusto von Dentz 	 */
1203cba6b758SLuiz Augusto von Dentz 	if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1204cba6b758SLuiz Augusto von Dentz 		timeout = adv->duration;
1205cba6b758SLuiz Augusto von Dentz 	else
1206cba6b758SLuiz Augusto von Dentz 		timeout = adv->remaining_time;
1207cba6b758SLuiz Augusto von Dentz 
1208cba6b758SLuiz Augusto von Dentz 	/* The remaining time is being reduced unless the instance is being
1209cba6b758SLuiz Augusto von Dentz 	 * advertised without time limit.
1210cba6b758SLuiz Augusto von Dentz 	 */
1211cba6b758SLuiz Augusto von Dentz 	if (adv->timeout)
1212cba6b758SLuiz Augusto von Dentz 		adv->remaining_time = adv->remaining_time - timeout;
1213cba6b758SLuiz Augusto von Dentz 
1214cba6b758SLuiz Augusto von Dentz 	/* Only use work for scheduling instances with legacy advertising */
1215cba6b758SLuiz Augusto von Dentz 	if (!ext_adv_capable(hdev)) {
1216cba6b758SLuiz Augusto von Dentz 		hdev->adv_instance_timeout = timeout;
1217cba6b758SLuiz Augusto von Dentz 		queue_delayed_work(hdev->req_workqueue,
1218cba6b758SLuiz Augusto von Dentz 				   &hdev->adv_instance_expire,
1219cba6b758SLuiz Augusto von Dentz 				   msecs_to_jiffies(timeout * 1000));
1220cba6b758SLuiz Augusto von Dentz 	}
1221cba6b758SLuiz Augusto von Dentz 
1222cba6b758SLuiz Augusto von Dentz 	/* If we're just re-scheduling the same instance again then do not
1223cba6b758SLuiz Augusto von Dentz 	 * execute any HCI commands. This happens when a single instance is
1224cba6b758SLuiz Augusto von Dentz 	 * being advertised.
1225cba6b758SLuiz Augusto von Dentz 	 */
1226cba6b758SLuiz Augusto von Dentz 	if (!force && hdev->cur_adv_instance == instance &&
1227cba6b758SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_LE_ADV))
1228cba6b758SLuiz Augusto von Dentz 		return 0;
1229cba6b758SLuiz Augusto von Dentz 
1230cba6b758SLuiz Augusto von Dentz 	hdev->cur_adv_instance = instance;
1231cba6b758SLuiz Augusto von Dentz 
1232cba6b758SLuiz Augusto von Dentz 	return hci_start_adv_sync(hdev, instance);
1233cba6b758SLuiz Augusto von Dentz }
1234cba6b758SLuiz Augusto von Dentz 
1235cba6b758SLuiz Augusto von Dentz static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1236cba6b758SLuiz Augusto von Dentz {
1237cba6b758SLuiz Augusto von Dentz 	int err;
1238cba6b758SLuiz Augusto von Dentz 
1239cba6b758SLuiz Augusto von Dentz 	if (!ext_adv_capable(hdev))
1240cba6b758SLuiz Augusto von Dentz 		return 0;
1241cba6b758SLuiz Augusto von Dentz 
1242cba6b758SLuiz Augusto von Dentz 	/* Disable instance 0x00 to disable all instances */
1243cba6b758SLuiz Augusto von Dentz 	err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1244cba6b758SLuiz Augusto von Dentz 	if (err)
1245cba6b758SLuiz Augusto von Dentz 		return err;
1246cba6b758SLuiz Augusto von Dentz 
1247cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1248cba6b758SLuiz Augusto von Dentz 					0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1249cba6b758SLuiz Augusto von Dentz }
1250cba6b758SLuiz Augusto von Dentz 
1251cba6b758SLuiz Augusto von Dentz static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1252cba6b758SLuiz Augusto von Dentz {
1253cba6b758SLuiz Augusto von Dentz 	struct adv_info *adv, *n;
1254cba6b758SLuiz Augusto von Dentz 
1255cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1256cba6b758SLuiz Augusto von Dentz 		/* Remove all existing sets */
1257cba6b758SLuiz Augusto von Dentz 		return hci_clear_adv_sets_sync(hdev, sk);
1258cba6b758SLuiz Augusto von Dentz 
1259cba6b758SLuiz Augusto von Dentz 	/* This is safe as long as there is no command send while the lock is
1260cba6b758SLuiz Augusto von Dentz 	 * held.
1261cba6b758SLuiz Augusto von Dentz 	 */
1262cba6b758SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
1263cba6b758SLuiz Augusto von Dentz 
1264cba6b758SLuiz Augusto von Dentz 	/* Cleanup non-ext instances */
1265cba6b758SLuiz Augusto von Dentz 	list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1266cba6b758SLuiz Augusto von Dentz 		u8 instance = adv->instance;
1267cba6b758SLuiz Augusto von Dentz 		int err;
1268cba6b758SLuiz Augusto von Dentz 
1269cba6b758SLuiz Augusto von Dentz 		if (!(force || adv->timeout))
1270cba6b758SLuiz Augusto von Dentz 			continue;
1271cba6b758SLuiz Augusto von Dentz 
1272cba6b758SLuiz Augusto von Dentz 		err = hci_remove_adv_instance(hdev, instance);
1273cba6b758SLuiz Augusto von Dentz 		if (!err)
1274cba6b758SLuiz Augusto von Dentz 			mgmt_advertising_removed(sk, hdev, instance);
1275cba6b758SLuiz Augusto von Dentz 	}
1276cba6b758SLuiz Augusto von Dentz 
1277cba6b758SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
1278cba6b758SLuiz Augusto von Dentz 
1279cba6b758SLuiz Augusto von Dentz 	return 0;
1280cba6b758SLuiz Augusto von Dentz }
1281cba6b758SLuiz Augusto von Dentz 
1282cba6b758SLuiz Augusto von Dentz static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1283cba6b758SLuiz Augusto von Dentz 			       struct sock *sk)
1284cba6b758SLuiz Augusto von Dentz {
1285cba6b758SLuiz Augusto von Dentz 	int err;
1286cba6b758SLuiz Augusto von Dentz 
1287cba6b758SLuiz Augusto von Dentz 	/* If we use extended advertising, instance has to be removed first. */
1288cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1289cba6b758SLuiz Augusto von Dentz 		return hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1290cba6b758SLuiz Augusto von Dentz 
1291cba6b758SLuiz Augusto von Dentz 	/* This is safe as long as there is no command send while the lock is
1292cba6b758SLuiz Augusto von Dentz 	 * held.
1293cba6b758SLuiz Augusto von Dentz 	 */
1294cba6b758SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
1295cba6b758SLuiz Augusto von Dentz 
1296cba6b758SLuiz Augusto von Dentz 	err = hci_remove_adv_instance(hdev, instance);
1297cba6b758SLuiz Augusto von Dentz 	if (!err)
1298cba6b758SLuiz Augusto von Dentz 		mgmt_advertising_removed(sk, hdev, instance);
1299cba6b758SLuiz Augusto von Dentz 
1300cba6b758SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
1301cba6b758SLuiz Augusto von Dentz 
1302cba6b758SLuiz Augusto von Dentz 	return err;
1303cba6b758SLuiz Augusto von Dentz }
1304cba6b758SLuiz Augusto von Dentz 
1305cba6b758SLuiz Augusto von Dentz /* For a single instance:
1306cba6b758SLuiz Augusto von Dentz  * - force == true: The instance will be removed even when its remaining
1307cba6b758SLuiz Augusto von Dentz  *   lifetime is not zero.
1308cba6b758SLuiz Augusto von Dentz  * - force == false: the instance will be deactivated but kept stored unless
1309cba6b758SLuiz Augusto von Dentz  *   the remaining lifetime is zero.
1310cba6b758SLuiz Augusto von Dentz  *
1311cba6b758SLuiz Augusto von Dentz  * For instance == 0x00:
1312cba6b758SLuiz Augusto von Dentz  * - force == true: All instances will be removed regardless of their timeout
1313cba6b758SLuiz Augusto von Dentz  *   setting.
1314cba6b758SLuiz Augusto von Dentz  * - force == false: Only instances that have a timeout will be removed.
1315cba6b758SLuiz Augusto von Dentz  */
1316cba6b758SLuiz Augusto von Dentz int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1317cba6b758SLuiz Augusto von Dentz 				u8 instance, bool force)
1318cba6b758SLuiz Augusto von Dentz {
1319cba6b758SLuiz Augusto von Dentz 	struct adv_info *next = NULL;
1320cba6b758SLuiz Augusto von Dentz 	int err;
1321cba6b758SLuiz Augusto von Dentz 
1322cba6b758SLuiz Augusto von Dentz 	/* Cancel any timeout concerning the removed instance(s). */
1323cba6b758SLuiz Augusto von Dentz 	if (!instance || hdev->cur_adv_instance == instance)
1324cba6b758SLuiz Augusto von Dentz 		cancel_adv_timeout(hdev);
1325cba6b758SLuiz Augusto von Dentz 
1326cba6b758SLuiz Augusto von Dentz 	/* Get the next instance to advertise BEFORE we remove
1327cba6b758SLuiz Augusto von Dentz 	 * the current one. This can be the same instance again
1328cba6b758SLuiz Augusto von Dentz 	 * if there is only one instance.
1329cba6b758SLuiz Augusto von Dentz 	 */
1330cba6b758SLuiz Augusto von Dentz 	if (hdev->cur_adv_instance == instance)
1331cba6b758SLuiz Augusto von Dentz 		next = hci_get_next_instance(hdev, instance);
1332cba6b758SLuiz Augusto von Dentz 
1333cba6b758SLuiz Augusto von Dentz 	if (!instance) {
1334cba6b758SLuiz Augusto von Dentz 		err = hci_clear_adv_sync(hdev, sk, force);
1335cba6b758SLuiz Augusto von Dentz 		if (err)
1336cba6b758SLuiz Augusto von Dentz 			return err;
1337cba6b758SLuiz Augusto von Dentz 	} else {
1338cba6b758SLuiz Augusto von Dentz 		struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1339cba6b758SLuiz Augusto von Dentz 
1340cba6b758SLuiz Augusto von Dentz 		if (force || (adv && adv->timeout && !adv->remaining_time)) {
1341cba6b758SLuiz Augusto von Dentz 			/* Don't advertise a removed instance. */
1342cba6b758SLuiz Augusto von Dentz 			if (next && next->instance == instance)
1343cba6b758SLuiz Augusto von Dentz 				next = NULL;
1344cba6b758SLuiz Augusto von Dentz 
1345cba6b758SLuiz Augusto von Dentz 			err = hci_remove_adv_sync(hdev, instance, sk);
1346cba6b758SLuiz Augusto von Dentz 			if (err)
1347cba6b758SLuiz Augusto von Dentz 				return err;
1348cba6b758SLuiz Augusto von Dentz 		}
1349cba6b758SLuiz Augusto von Dentz 	}
1350cba6b758SLuiz Augusto von Dentz 
1351cba6b758SLuiz Augusto von Dentz 	if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1352cba6b758SLuiz Augusto von Dentz 		return 0;
1353cba6b758SLuiz Augusto von Dentz 
1354cba6b758SLuiz Augusto von Dentz 	if (next && !ext_adv_capable(hdev))
1355cba6b758SLuiz Augusto von Dentz 		hci_schedule_adv_instance_sync(hdev, next->instance, false);
1356cba6b758SLuiz Augusto von Dentz 
1357cba6b758SLuiz Augusto von Dentz 	return 0;
1358cba6b758SLuiz Augusto von Dentz }
1359cba6b758SLuiz Augusto von Dentz 
136047db6b42SBrian Gix int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
136147db6b42SBrian Gix {
136247db6b42SBrian Gix 	struct hci_cp_read_rssi cp;
136347db6b42SBrian Gix 
136447db6b42SBrian Gix 	cp.handle = handle;
136547db6b42SBrian Gix 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
136647db6b42SBrian Gix 					sizeof(cp), &cp, HCI_CMD_TIMEOUT);
136747db6b42SBrian Gix }
136847db6b42SBrian Gix 
13695a750137SBrian Gix int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
13705a750137SBrian Gix {
13715a750137SBrian Gix 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
13725a750137SBrian Gix 					sizeof(*cp), cp, HCI_CMD_TIMEOUT);
13735a750137SBrian Gix }
13745a750137SBrian Gix 
137547db6b42SBrian Gix int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
137647db6b42SBrian Gix {
137747db6b42SBrian Gix 	struct hci_cp_read_tx_power cp;
137847db6b42SBrian Gix 
137947db6b42SBrian Gix 	cp.handle = handle;
138047db6b42SBrian Gix 	cp.type = type;
138147db6b42SBrian Gix 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
138247db6b42SBrian Gix 					sizeof(cp), &cp, HCI_CMD_TIMEOUT);
138347db6b42SBrian Gix }
138447db6b42SBrian Gix 
1385cba6b758SLuiz Augusto von Dentz int hci_disable_advertising_sync(struct hci_dev *hdev)
1386cba6b758SLuiz Augusto von Dentz {
1387cba6b758SLuiz Augusto von Dentz 	u8 enable = 0x00;
1388cba6b758SLuiz Augusto von Dentz 
1389ad383c2cSLuiz Augusto von Dentz 	/* If controller is not advertising we are done. */
1390ad383c2cSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1391ad383c2cSLuiz Augusto von Dentz 		return 0;
1392ad383c2cSLuiz Augusto von Dentz 
1393cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1394cba6b758SLuiz Augusto von Dentz 		return hci_disable_ext_adv_instance_sync(hdev, 0x00);
1395cba6b758SLuiz Augusto von Dentz 
1396cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1397cba6b758SLuiz Augusto von Dentz 				     sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1398cba6b758SLuiz Augusto von Dentz }
1399e8907f76SLuiz Augusto von Dentz 
1400e8907f76SLuiz Augusto von Dentz static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1401e8907f76SLuiz Augusto von Dentz 					   u8 filter_dup)
1402e8907f76SLuiz Augusto von Dentz {
1403e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_scan_enable cp;
1404e8907f76SLuiz Augusto von Dentz 
1405e8907f76SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1406e8907f76SLuiz Augusto von Dentz 	cp.enable = val;
1407e8907f76SLuiz Augusto von Dentz 	cp.filter_dup = filter_dup;
1408e8907f76SLuiz Augusto von Dentz 
1409e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1410e8907f76SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1411e8907f76SLuiz Augusto von Dentz }
1412e8907f76SLuiz Augusto von Dentz 
1413e8907f76SLuiz Augusto von Dentz static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
1414e8907f76SLuiz Augusto von Dentz 				       u8 filter_dup)
1415e8907f76SLuiz Augusto von Dentz {
1416e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_set_scan_enable cp;
1417e8907f76SLuiz Augusto von Dentz 
1418e8907f76SLuiz Augusto von Dentz 	if (use_ext_scan(hdev))
1419e8907f76SLuiz Augusto von Dentz 		return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
1420e8907f76SLuiz Augusto von Dentz 
1421e8907f76SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1422e8907f76SLuiz Augusto von Dentz 	cp.enable = val;
1423e8907f76SLuiz Augusto von Dentz 	cp.filter_dup = filter_dup;
1424e8907f76SLuiz Augusto von Dentz 
1425e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
1426e8907f76SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1427e8907f76SLuiz Augusto von Dentz }
1428e8907f76SLuiz Augusto von Dentz 
1429e8907f76SLuiz Augusto von Dentz static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
1430e8907f76SLuiz Augusto von Dentz {
1431ad383c2cSLuiz Augusto von Dentz 	if (!use_ll_privacy(hdev))
1432ad383c2cSLuiz Augusto von Dentz 		return 0;
1433ad383c2cSLuiz Augusto von Dentz 
1434ad383c2cSLuiz Augusto von Dentz 	/* If controller is not/already resolving we are done. */
1435ad383c2cSLuiz Augusto von Dentz 	if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
1436e8907f76SLuiz Augusto von Dentz 		return 0;
1437e8907f76SLuiz Augusto von Dentz 
1438e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
1439e8907f76SLuiz Augusto von Dentz 				     sizeof(val), &val, HCI_CMD_TIMEOUT);
1440e8907f76SLuiz Augusto von Dentz }
1441e8907f76SLuiz Augusto von Dentz 
144227592ca1SLuiz Augusto von Dentz static int hci_scan_disable_sync(struct hci_dev *hdev)
1443e8907f76SLuiz Augusto von Dentz {
1444e8907f76SLuiz Augusto von Dentz 	int err;
1445e8907f76SLuiz Augusto von Dentz 
1446e8907f76SLuiz Augusto von Dentz 	/* If controller is not scanning we are done. */
1447e8907f76SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
1448e8907f76SLuiz Augusto von Dentz 		return 0;
1449e8907f76SLuiz Augusto von Dentz 
1450e8907f76SLuiz Augusto von Dentz 	if (hdev->scanning_paused) {
1451e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
1452e8907f76SLuiz Augusto von Dentz 		return 0;
1453e8907f76SLuiz Augusto von Dentz 	}
1454e8907f76SLuiz Augusto von Dentz 
1455e8907f76SLuiz Augusto von Dentz 	err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
1456e8907f76SLuiz Augusto von Dentz 	if (err) {
1457e8907f76SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to disable scanning: %d", err);
1458e8907f76SLuiz Augusto von Dentz 		return err;
1459e8907f76SLuiz Augusto von Dentz 	}
1460e8907f76SLuiz Augusto von Dentz 
1461e8907f76SLuiz Augusto von Dentz 	return err;
1462e8907f76SLuiz Augusto von Dentz }
1463e8907f76SLuiz Augusto von Dentz 
1464e8907f76SLuiz Augusto von Dentz static bool scan_use_rpa(struct hci_dev *hdev)
1465e8907f76SLuiz Augusto von Dentz {
1466e8907f76SLuiz Augusto von Dentz 	return hci_dev_test_flag(hdev, HCI_PRIVACY);
1467e8907f76SLuiz Augusto von Dentz }
1468e8907f76SLuiz Augusto von Dentz 
1469e8907f76SLuiz Augusto von Dentz static void hci_start_interleave_scan(struct hci_dev *hdev)
1470e8907f76SLuiz Augusto von Dentz {
1471e8907f76SLuiz Augusto von Dentz 	hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
1472e8907f76SLuiz Augusto von Dentz 	queue_delayed_work(hdev->req_workqueue,
1473e8907f76SLuiz Augusto von Dentz 			   &hdev->interleave_scan, 0);
1474e8907f76SLuiz Augusto von Dentz }
1475e8907f76SLuiz Augusto von Dentz 
1476e8907f76SLuiz Augusto von Dentz static bool is_interleave_scanning(struct hci_dev *hdev)
1477e8907f76SLuiz Augusto von Dentz {
1478e8907f76SLuiz Augusto von Dentz 	return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
1479e8907f76SLuiz Augusto von Dentz }
1480e8907f76SLuiz Augusto von Dentz 
1481e8907f76SLuiz Augusto von Dentz static void cancel_interleave_scan(struct hci_dev *hdev)
1482e8907f76SLuiz Augusto von Dentz {
1483e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "cancelling interleave scan");
1484e8907f76SLuiz Augusto von Dentz 
1485e8907f76SLuiz Augusto von Dentz 	cancel_delayed_work_sync(&hdev->interleave_scan);
1486e8907f76SLuiz Augusto von Dentz 
1487e8907f76SLuiz Augusto von Dentz 	hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
1488e8907f76SLuiz Augusto von Dentz }
1489e8907f76SLuiz Augusto von Dentz 
1490e8907f76SLuiz Augusto von Dentz /* Return true if interleave_scan wasn't started until exiting this function,
1491e8907f76SLuiz Augusto von Dentz  * otherwise, return false
1492e8907f76SLuiz Augusto von Dentz  */
1493e8907f76SLuiz Augusto von Dentz static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
1494e8907f76SLuiz Augusto von Dentz {
1495e8907f76SLuiz Augusto von Dentz 	/* Do interleaved scan only if all of the following are true:
1496e8907f76SLuiz Augusto von Dentz 	 * - There is at least one ADV monitor
1497e8907f76SLuiz Augusto von Dentz 	 * - At least one pending LE connection or one device to be scanned for
1498e8907f76SLuiz Augusto von Dentz 	 * - Monitor offloading is not supported
1499e8907f76SLuiz Augusto von Dentz 	 * If so, we should alternate between allowlist scan and one without
1500e8907f76SLuiz Augusto von Dentz 	 * any filters to save power.
1501e8907f76SLuiz Augusto von Dentz 	 */
1502e8907f76SLuiz Augusto von Dentz 	bool use_interleaving = hci_is_adv_monitoring(hdev) &&
1503e8907f76SLuiz Augusto von Dentz 				!(list_empty(&hdev->pend_le_conns) &&
1504e8907f76SLuiz Augusto von Dentz 				  list_empty(&hdev->pend_le_reports)) &&
1505e8907f76SLuiz Augusto von Dentz 				hci_get_adv_monitor_offload_ext(hdev) ==
1506e8907f76SLuiz Augusto von Dentz 				    HCI_ADV_MONITOR_EXT_NONE;
1507e8907f76SLuiz Augusto von Dentz 	bool is_interleaving = is_interleave_scanning(hdev);
1508e8907f76SLuiz Augusto von Dentz 
1509e8907f76SLuiz Augusto von Dentz 	if (use_interleaving && !is_interleaving) {
1510e8907f76SLuiz Augusto von Dentz 		hci_start_interleave_scan(hdev);
1511e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "starting interleave scan");
1512e8907f76SLuiz Augusto von Dentz 		return true;
1513e8907f76SLuiz Augusto von Dentz 	}
1514e8907f76SLuiz Augusto von Dentz 
1515e8907f76SLuiz Augusto von Dentz 	if (!use_interleaving && is_interleaving)
1516e8907f76SLuiz Augusto von Dentz 		cancel_interleave_scan(hdev);
1517e8907f76SLuiz Augusto von Dentz 
1518e8907f76SLuiz Augusto von Dentz 	return false;
1519e8907f76SLuiz Augusto von Dentz }
1520e8907f76SLuiz Augusto von Dentz 
1521e8907f76SLuiz Augusto von Dentz /* Removes connection to resolve list if needed.*/
1522e8907f76SLuiz Augusto von Dentz static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
1523e8907f76SLuiz Augusto von Dentz 					bdaddr_t *bdaddr, u8 bdaddr_type)
1524e8907f76SLuiz Augusto von Dentz {
1525e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_del_from_resolv_list cp;
1526e8907f76SLuiz Augusto von Dentz 	struct bdaddr_list_with_irk *entry;
1527e8907f76SLuiz Augusto von Dentz 
1528ad383c2cSLuiz Augusto von Dentz 	if (!use_ll_privacy(hdev))
1529e8907f76SLuiz Augusto von Dentz 		return 0;
1530e8907f76SLuiz Augusto von Dentz 
1531e8907f76SLuiz Augusto von Dentz 	/* Check if the IRK has been programmed */
1532e8907f76SLuiz Augusto von Dentz 	entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
1533e8907f76SLuiz Augusto von Dentz 						bdaddr_type);
1534e8907f76SLuiz Augusto von Dentz 	if (!entry)
1535e8907f76SLuiz Augusto von Dentz 		return 0;
1536e8907f76SLuiz Augusto von Dentz 
1537e8907f76SLuiz Augusto von Dentz 	cp.bdaddr_type = bdaddr_type;
1538e8907f76SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, bdaddr);
1539e8907f76SLuiz Augusto von Dentz 
1540e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
1541e8907f76SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1542e8907f76SLuiz Augusto von Dentz }
1543e8907f76SLuiz Augusto von Dentz 
1544e8907f76SLuiz Augusto von Dentz static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
1545e8907f76SLuiz Augusto von Dentz 				       bdaddr_t *bdaddr, u8 bdaddr_type)
1546e8907f76SLuiz Augusto von Dentz {
1547e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_del_from_accept_list cp;
1548e8907f76SLuiz Augusto von Dentz 	int err;
1549e8907f76SLuiz Augusto von Dentz 
1550e8907f76SLuiz Augusto von Dentz 	/* Check if device is on accept list before removing it */
1551e8907f76SLuiz Augusto von Dentz 	if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
1552e8907f76SLuiz Augusto von Dentz 		return 0;
1553e8907f76SLuiz Augusto von Dentz 
1554e8907f76SLuiz Augusto von Dentz 	cp.bdaddr_type = bdaddr_type;
1555e8907f76SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, bdaddr);
1556e8907f76SLuiz Augusto von Dentz 
1557ad383c2cSLuiz Augusto von Dentz 	/* Ignore errors when removing from resolving list as that is likely
1558ad383c2cSLuiz Augusto von Dentz 	 * that the device was never added.
1559ad383c2cSLuiz Augusto von Dentz 	 */
1560ad383c2cSLuiz Augusto von Dentz 	hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
1561ad383c2cSLuiz Augusto von Dentz 
1562e8907f76SLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
1563e8907f76SLuiz Augusto von Dentz 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1564e8907f76SLuiz Augusto von Dentz 	if (err) {
1565e8907f76SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
1566e8907f76SLuiz Augusto von Dentz 		return err;
1567e8907f76SLuiz Augusto von Dentz 	}
1568e8907f76SLuiz Augusto von Dentz 
1569e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
1570e8907f76SLuiz Augusto von Dentz 		   cp.bdaddr_type);
1571e8907f76SLuiz Augusto von Dentz 
1572ad383c2cSLuiz Augusto von Dentz 	return 0;
1573e8907f76SLuiz Augusto von Dentz }
1574e8907f76SLuiz Augusto von Dentz 
1575cf75ad8bSLuiz Augusto von Dentz /* Adds connection to resolve list if needed.
1576cf75ad8bSLuiz Augusto von Dentz  * Setting params to NULL programs local hdev->irk
1577cf75ad8bSLuiz Augusto von Dentz  */
1578e8907f76SLuiz Augusto von Dentz static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
1579e8907f76SLuiz Augusto von Dentz 					struct hci_conn_params *params)
1580e8907f76SLuiz Augusto von Dentz {
1581e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_add_to_resolv_list cp;
1582e8907f76SLuiz Augusto von Dentz 	struct smp_irk *irk;
1583e8907f76SLuiz Augusto von Dentz 	struct bdaddr_list_with_irk *entry;
1584e8907f76SLuiz Augusto von Dentz 
1585ad383c2cSLuiz Augusto von Dentz 	if (!use_ll_privacy(hdev))
1586e8907f76SLuiz Augusto von Dentz 		return 0;
1587e8907f76SLuiz Augusto von Dentz 
1588cf75ad8bSLuiz Augusto von Dentz 	/* Attempt to program local identity address, type and irk if params is
1589cf75ad8bSLuiz Augusto von Dentz 	 * NULL.
1590cf75ad8bSLuiz Augusto von Dentz 	 */
1591cf75ad8bSLuiz Augusto von Dentz 	if (!params) {
1592cf75ad8bSLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
1593cf75ad8bSLuiz Augusto von Dentz 			return 0;
1594cf75ad8bSLuiz Augusto von Dentz 
1595cf75ad8bSLuiz Augusto von Dentz 		hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
1596cf75ad8bSLuiz Augusto von Dentz 		memcpy(cp.peer_irk, hdev->irk, 16);
1597cf75ad8bSLuiz Augusto von Dentz 		goto done;
1598cf75ad8bSLuiz Augusto von Dentz 	}
1599cf75ad8bSLuiz Augusto von Dentz 
1600e8907f76SLuiz Augusto von Dentz 	irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
1601e8907f76SLuiz Augusto von Dentz 	if (!irk)
1602e8907f76SLuiz Augusto von Dentz 		return 0;
1603e8907f76SLuiz Augusto von Dentz 
1604e8907f76SLuiz Augusto von Dentz 	/* Check if the IK has _not_ been programmed yet. */
1605e8907f76SLuiz Augusto von Dentz 	entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
1606e8907f76SLuiz Augusto von Dentz 						&params->addr,
1607e8907f76SLuiz Augusto von Dentz 						params->addr_type);
1608e8907f76SLuiz Augusto von Dentz 	if (entry)
1609e8907f76SLuiz Augusto von Dentz 		return 0;
1610e8907f76SLuiz Augusto von Dentz 
1611e8907f76SLuiz Augusto von Dentz 	cp.bdaddr_type = params->addr_type;
1612e8907f76SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, &params->addr);
1613e8907f76SLuiz Augusto von Dentz 	memcpy(cp.peer_irk, irk->val, 16);
1614e8907f76SLuiz Augusto von Dentz 
1615cf75ad8bSLuiz Augusto von Dentz done:
1616e8907f76SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_PRIVACY))
1617e8907f76SLuiz Augusto von Dentz 		memcpy(cp.local_irk, hdev->irk, 16);
1618e8907f76SLuiz Augusto von Dentz 	else
1619e8907f76SLuiz Augusto von Dentz 		memset(cp.local_irk, 0, 16);
1620e8907f76SLuiz Augusto von Dentz 
1621e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
1622e8907f76SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1623e8907f76SLuiz Augusto von Dentz }
1624e8907f76SLuiz Augusto von Dentz 
1625853b70b5SLuiz Augusto von Dentz /* Set Device Privacy Mode. */
1626853b70b5SLuiz Augusto von Dentz static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
1627853b70b5SLuiz Augusto von Dentz 					struct hci_conn_params *params)
1628853b70b5SLuiz Augusto von Dentz {
1629853b70b5SLuiz Augusto von Dentz 	struct hci_cp_le_set_privacy_mode cp;
1630853b70b5SLuiz Augusto von Dentz 	struct smp_irk *irk;
1631853b70b5SLuiz Augusto von Dentz 
1632853b70b5SLuiz Augusto von Dentz 	/* If device privacy mode has already been set there is nothing to do */
1633853b70b5SLuiz Augusto von Dentz 	if (params->privacy_mode == HCI_DEVICE_PRIVACY)
1634853b70b5SLuiz Augusto von Dentz 		return 0;
1635853b70b5SLuiz Augusto von Dentz 
1636853b70b5SLuiz Augusto von Dentz 	/* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
1637853b70b5SLuiz Augusto von Dentz 	 * indicates that LL Privacy has been enabled and
1638853b70b5SLuiz Augusto von Dentz 	 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
1639853b70b5SLuiz Augusto von Dentz 	 */
1640853b70b5SLuiz Augusto von Dentz 	if (!test_bit(HCI_CONN_FLAG_DEVICE_PRIVACY, params->flags))
1641853b70b5SLuiz Augusto von Dentz 		return 0;
1642853b70b5SLuiz Augusto von Dentz 
1643853b70b5SLuiz Augusto von Dentz 	irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
1644853b70b5SLuiz Augusto von Dentz 	if (!irk)
1645853b70b5SLuiz Augusto von Dentz 		return 0;
1646853b70b5SLuiz Augusto von Dentz 
1647853b70b5SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1648853b70b5SLuiz Augusto von Dentz 	cp.bdaddr_type = irk->addr_type;
1649853b70b5SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, &irk->bdaddr);
1650853b70b5SLuiz Augusto von Dentz 	cp.mode = HCI_DEVICE_PRIVACY;
1651853b70b5SLuiz Augusto von Dentz 
1652853b70b5SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
1653853b70b5SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1654853b70b5SLuiz Augusto von Dentz }
1655853b70b5SLuiz Augusto von Dentz 
1656e8907f76SLuiz Augusto von Dentz /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
1657853b70b5SLuiz Augusto von Dentz  * this attempts to program the device in the resolving list as well and
1658853b70b5SLuiz Augusto von Dentz  * properly set the privacy mode.
1659e8907f76SLuiz Augusto von Dentz  */
1660e8907f76SLuiz Augusto von Dentz static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
1661e8907f76SLuiz Augusto von Dentz 				       struct hci_conn_params *params,
1662ad383c2cSLuiz Augusto von Dentz 				       u8 *num_entries)
1663e8907f76SLuiz Augusto von Dentz {
1664e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_add_to_accept_list cp;
1665e8907f76SLuiz Augusto von Dentz 	int err;
1666e8907f76SLuiz Augusto von Dentz 
1667*3b420553SLuiz Augusto von Dentz 	/* During suspend, only wakeable devices can be in acceptlist */
1668*3b420553SLuiz Augusto von Dentz 	if (hdev->suspended &&
1669*3b420553SLuiz Augusto von Dentz 	    !test_bit(HCI_CONN_FLAG_REMOTE_WAKEUP, params->flags))
1670*3b420553SLuiz Augusto von Dentz 		return 0;
1671*3b420553SLuiz Augusto von Dentz 
1672e8907f76SLuiz Augusto von Dentz 	/* Select filter policy to accept all advertising */
1673e8907f76SLuiz Augusto von Dentz 	if (*num_entries >= hdev->le_accept_list_size)
1674e8907f76SLuiz Augusto von Dentz 		return -ENOSPC;
1675e8907f76SLuiz Augusto von Dentz 
1676e8907f76SLuiz Augusto von Dentz 	/* Accept list can not be used with RPAs */
1677ad383c2cSLuiz Augusto von Dentz 	if (!use_ll_privacy(hdev) &&
1678*3b420553SLuiz Augusto von Dentz 	    hci_find_irk_by_addr(hdev, &params->addr, params->addr_type))
1679e8907f76SLuiz Augusto von Dentz 		return -EINVAL;
1680e8907f76SLuiz Augusto von Dentz 
1681ad383c2cSLuiz Augusto von Dentz 	/* Attempt to program the device in the resolving list first to avoid
1682ad383c2cSLuiz Augusto von Dentz 	 * having to rollback in case it fails since the resolving list is
1683ad383c2cSLuiz Augusto von Dentz 	 * dynamic it can probably be smaller than the accept list.
1684ad383c2cSLuiz Augusto von Dentz 	 */
1685ad383c2cSLuiz Augusto von Dentz 	err = hci_le_add_resolve_list_sync(hdev, params);
1686ad383c2cSLuiz Augusto von Dentz 	if (err) {
1687ad383c2cSLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
1688ad383c2cSLuiz Augusto von Dentz 		return err;
1689ad383c2cSLuiz Augusto von Dentz 	}
1690ad383c2cSLuiz Augusto von Dentz 
1691853b70b5SLuiz Augusto von Dentz 	/* Set Privacy Mode */
1692853b70b5SLuiz Augusto von Dentz 	err = hci_le_set_privacy_mode_sync(hdev, params);
1693853b70b5SLuiz Augusto von Dentz 	if (err) {
1694853b70b5SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
1695853b70b5SLuiz Augusto von Dentz 		return err;
1696853b70b5SLuiz Augusto von Dentz 	}
1697853b70b5SLuiz Augusto von Dentz 
1698853b70b5SLuiz Augusto von Dentz 	/* Check if already in accept list */
1699853b70b5SLuiz Augusto von Dentz 	if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
1700853b70b5SLuiz Augusto von Dentz 				   params->addr_type))
1701853b70b5SLuiz Augusto von Dentz 		return 0;
1702853b70b5SLuiz Augusto von Dentz 
1703e8907f76SLuiz Augusto von Dentz 	*num_entries += 1;
1704e8907f76SLuiz Augusto von Dentz 	cp.bdaddr_type = params->addr_type;
1705e8907f76SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, &params->addr);
1706e8907f76SLuiz Augusto von Dentz 
1707e8907f76SLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
1708e8907f76SLuiz Augusto von Dentz 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1709e8907f76SLuiz Augusto von Dentz 	if (err) {
1710e8907f76SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to add to allow list: %d", err);
1711ad383c2cSLuiz Augusto von Dentz 		/* Rollback the device from the resolving list */
1712ad383c2cSLuiz Augusto von Dentz 		hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
1713e8907f76SLuiz Augusto von Dentz 		return err;
1714e8907f76SLuiz Augusto von Dentz 	}
1715e8907f76SLuiz Augusto von Dentz 
1716e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
1717e8907f76SLuiz Augusto von Dentz 		   cp.bdaddr_type);
1718e8907f76SLuiz Augusto von Dentz 
1719ad383c2cSLuiz Augusto von Dentz 	return 0;
1720e8907f76SLuiz Augusto von Dentz }
1721e8907f76SLuiz Augusto von Dentz 
1722182ee45dSLuiz Augusto von Dentz /* This function disables/pause all advertising instances */
1723ad383c2cSLuiz Augusto von Dentz static int hci_pause_advertising_sync(struct hci_dev *hdev)
1724ad383c2cSLuiz Augusto von Dentz {
1725ad383c2cSLuiz Augusto von Dentz 	int err;
1726182ee45dSLuiz Augusto von Dentz 	int old_state;
1727ad383c2cSLuiz Augusto von Dentz 
17289446bddeSLuiz Augusto von Dentz 	/* If already been paused there is nothing to do. */
17299446bddeSLuiz Augusto von Dentz 	if (hdev->advertising_paused)
1730ad383c2cSLuiz Augusto von Dentz 		return 0;
1731ad383c2cSLuiz Augusto von Dentz 
1732182ee45dSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Pausing directed advertising");
1733182ee45dSLuiz Augusto von Dentz 
1734182ee45dSLuiz Augusto von Dentz 	/* Stop directed advertising */
1735182ee45dSLuiz Augusto von Dentz 	old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
1736182ee45dSLuiz Augusto von Dentz 	if (old_state) {
1737182ee45dSLuiz Augusto von Dentz 		/* When discoverable timeout triggers, then just make sure
1738182ee45dSLuiz Augusto von Dentz 		 * the limited discoverable flag is cleared. Even in the case
1739182ee45dSLuiz Augusto von Dentz 		 * of a timeout triggered from general discoverable, it is
1740182ee45dSLuiz Augusto von Dentz 		 * safe to unconditionally clear the flag.
1741182ee45dSLuiz Augusto von Dentz 		 */
1742182ee45dSLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1743182ee45dSLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1744182ee45dSLuiz Augusto von Dentz 		hdev->discov_timeout = 0;
1745182ee45dSLuiz Augusto von Dentz 	}
1746182ee45dSLuiz Augusto von Dentz 
1747ad383c2cSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Pausing advertising instances");
1748ad383c2cSLuiz Augusto von Dentz 
1749ad383c2cSLuiz Augusto von Dentz 	/* Call to disable any advertisements active on the controller.
1750ad383c2cSLuiz Augusto von Dentz 	 * This will succeed even if no advertisements are configured.
1751ad383c2cSLuiz Augusto von Dentz 	 */
1752ad383c2cSLuiz Augusto von Dentz 	err = hci_disable_advertising_sync(hdev);
1753ad383c2cSLuiz Augusto von Dentz 	if (err)
1754ad383c2cSLuiz Augusto von Dentz 		return err;
1755ad383c2cSLuiz Augusto von Dentz 
1756ad383c2cSLuiz Augusto von Dentz 	/* If we are using software rotation, pause the loop */
1757ad383c2cSLuiz Augusto von Dentz 	if (!ext_adv_capable(hdev))
1758ad383c2cSLuiz Augusto von Dentz 		cancel_adv_timeout(hdev);
1759ad383c2cSLuiz Augusto von Dentz 
1760ad383c2cSLuiz Augusto von Dentz 	hdev->advertising_paused = true;
1761182ee45dSLuiz Augusto von Dentz 	hdev->advertising_old_state = old_state;
1762ad383c2cSLuiz Augusto von Dentz 
1763ad383c2cSLuiz Augusto von Dentz 	return 0;
1764ad383c2cSLuiz Augusto von Dentz }
1765ad383c2cSLuiz Augusto von Dentz 
1766182ee45dSLuiz Augusto von Dentz /* This function enables all user advertising instances */
1767ad383c2cSLuiz Augusto von Dentz static int hci_resume_advertising_sync(struct hci_dev *hdev)
1768ad383c2cSLuiz Augusto von Dentz {
1769ad383c2cSLuiz Augusto von Dentz 	struct adv_info *adv, *tmp;
1770ad383c2cSLuiz Augusto von Dentz 	int err;
1771ad383c2cSLuiz Augusto von Dentz 
1772ad383c2cSLuiz Augusto von Dentz 	/* If advertising has not been paused there is nothing  to do. */
1773ad383c2cSLuiz Augusto von Dentz 	if (!hdev->advertising_paused)
1774ad383c2cSLuiz Augusto von Dentz 		return 0;
1775ad383c2cSLuiz Augusto von Dentz 
1776182ee45dSLuiz Augusto von Dentz 	/* Resume directed advertising */
1777182ee45dSLuiz Augusto von Dentz 	hdev->advertising_paused = false;
1778182ee45dSLuiz Augusto von Dentz 	if (hdev->advertising_old_state) {
1779182ee45dSLuiz Augusto von Dentz 		hci_dev_set_flag(hdev, HCI_ADVERTISING);
1780182ee45dSLuiz Augusto von Dentz 		hdev->advertising_old_state = 0;
1781182ee45dSLuiz Augusto von Dentz 	}
1782182ee45dSLuiz Augusto von Dentz 
1783ad383c2cSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Resuming advertising instances");
1784ad383c2cSLuiz Augusto von Dentz 
1785ad383c2cSLuiz Augusto von Dentz 	if (ext_adv_capable(hdev)) {
1786ad383c2cSLuiz Augusto von Dentz 		/* Call for each tracked instance to be re-enabled */
1787ad383c2cSLuiz Augusto von Dentz 		list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
1788ad383c2cSLuiz Augusto von Dentz 			err = hci_enable_ext_advertising_sync(hdev,
1789ad383c2cSLuiz Augusto von Dentz 							      adv->instance);
1790ad383c2cSLuiz Augusto von Dentz 			if (!err)
1791ad383c2cSLuiz Augusto von Dentz 				continue;
1792ad383c2cSLuiz Augusto von Dentz 
1793ad383c2cSLuiz Augusto von Dentz 			/* If the instance cannot be resumed remove it */
1794ad383c2cSLuiz Augusto von Dentz 			hci_remove_ext_adv_instance_sync(hdev, adv->instance,
1795ad383c2cSLuiz Augusto von Dentz 							 NULL);
1796ad383c2cSLuiz Augusto von Dentz 		}
1797ad383c2cSLuiz Augusto von Dentz 	} else {
1798ad383c2cSLuiz Augusto von Dentz 		/* Schedule for most recent instance to be restarted and begin
1799ad383c2cSLuiz Augusto von Dentz 		 * the software rotation loop
1800ad383c2cSLuiz Augusto von Dentz 		 */
1801ad383c2cSLuiz Augusto von Dentz 		err = hci_schedule_adv_instance_sync(hdev,
1802ad383c2cSLuiz Augusto von Dentz 						     hdev->cur_adv_instance,
1803ad383c2cSLuiz Augusto von Dentz 						     true);
1804ad383c2cSLuiz Augusto von Dentz 	}
1805ad383c2cSLuiz Augusto von Dentz 
1806ad383c2cSLuiz Augusto von Dentz 	hdev->advertising_paused = false;
1807ad383c2cSLuiz Augusto von Dentz 
1808ad383c2cSLuiz Augusto von Dentz 	return err;
1809ad383c2cSLuiz Augusto von Dentz }
1810ad383c2cSLuiz Augusto von Dentz 
1811f892244bSBrian Gix struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
1812f892244bSBrian Gix 					     bool extended, struct sock *sk)
1813f892244bSBrian Gix {
1814f892244bSBrian Gix 	u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
1815f892244bSBrian Gix 					HCI_OP_READ_LOCAL_OOB_DATA;
1816f892244bSBrian Gix 
1817f892244bSBrian Gix 	return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1818f892244bSBrian Gix }
1819f892244bSBrian Gix 
1820ad383c2cSLuiz Augusto von Dentz /* Device must not be scanning when updating the accept list.
1821ad383c2cSLuiz Augusto von Dentz  *
1822ad383c2cSLuiz Augusto von Dentz  * Update is done using the following sequence:
1823ad383c2cSLuiz Augusto von Dentz  *
1824ad383c2cSLuiz Augusto von Dentz  * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
1825ad383c2cSLuiz Augusto von Dentz  * Remove Devices From Accept List ->
1826ad383c2cSLuiz Augusto von Dentz  * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
1827ad383c2cSLuiz Augusto von Dentz  * Add Devices to Accept List ->
1828ad383c2cSLuiz Augusto von Dentz  * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
1829ad383c2cSLuiz Augusto von Dentz  * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
1830ad383c2cSLuiz Augusto von Dentz  * Enable Scanning
1831ad383c2cSLuiz Augusto von Dentz  *
1832ad383c2cSLuiz Augusto von Dentz  * In case of failure advertising shall be restored to its original state and
1833ad383c2cSLuiz Augusto von Dentz  * return would disable accept list since either accept or resolving list could
1834ad383c2cSLuiz Augusto von Dentz  * not be programmed.
1835ad383c2cSLuiz Augusto von Dentz  *
1836ad383c2cSLuiz Augusto von Dentz  */
1837e8907f76SLuiz Augusto von Dentz static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
1838e8907f76SLuiz Augusto von Dentz {
1839e8907f76SLuiz Augusto von Dentz 	struct hci_conn_params *params;
1840e8907f76SLuiz Augusto von Dentz 	struct bdaddr_list *b, *t;
1841e8907f76SLuiz Augusto von Dentz 	u8 num_entries = 0;
1842e8907f76SLuiz Augusto von Dentz 	bool pend_conn, pend_report;
184380740ebbSLuiz Augusto von Dentz 	u8 filter_policy;
1844ad383c2cSLuiz Augusto von Dentz 	int err;
1845e8907f76SLuiz Augusto von Dentz 
1846ad383c2cSLuiz Augusto von Dentz 	/* Pause advertising if resolving list can be used as controllers are
1847ad383c2cSLuiz Augusto von Dentz 	 * cannot accept resolving list modifications while advertising.
1848ad383c2cSLuiz Augusto von Dentz 	 */
1849ad383c2cSLuiz Augusto von Dentz 	if (use_ll_privacy(hdev)) {
1850ad383c2cSLuiz Augusto von Dentz 		err = hci_pause_advertising_sync(hdev);
1851ad383c2cSLuiz Augusto von Dentz 		if (err) {
1852ad383c2cSLuiz Augusto von Dentz 			bt_dev_err(hdev, "pause advertising failed: %d", err);
1853ad383c2cSLuiz Augusto von Dentz 			return 0x00;
1854ad383c2cSLuiz Augusto von Dentz 		}
1855ad383c2cSLuiz Augusto von Dentz 	}
1856ad383c2cSLuiz Augusto von Dentz 
1857ad383c2cSLuiz Augusto von Dentz 	/* Disable address resolution while reprogramming accept list since
1858ad383c2cSLuiz Augusto von Dentz 	 * devices that do have an IRK will be programmed in the resolving list
1859ad383c2cSLuiz Augusto von Dentz 	 * when LL Privacy is enabled.
1860ad383c2cSLuiz Augusto von Dentz 	 */
1861ad383c2cSLuiz Augusto von Dentz 	err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
1862ad383c2cSLuiz Augusto von Dentz 	if (err) {
1863ad383c2cSLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
1864ad383c2cSLuiz Augusto von Dentz 		goto done;
1865ad383c2cSLuiz Augusto von Dentz 	}
1866e8907f76SLuiz Augusto von Dentz 
1867e8907f76SLuiz Augusto von Dentz 	/* Go through the current accept list programmed into the
1868e8907f76SLuiz Augusto von Dentz 	 * controller one by one and check if that address is still
1869e8907f76SLuiz Augusto von Dentz 	 * in the list of pending connections or list of devices to
1870e8907f76SLuiz Augusto von Dentz 	 * report. If not present in either list, then remove it from
1871e8907f76SLuiz Augusto von Dentz 	 * the controller.
1872e8907f76SLuiz Augusto von Dentz 	 */
1873e8907f76SLuiz Augusto von Dentz 	list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
1874e8907f76SLuiz Augusto von Dentz 		pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
1875e8907f76SLuiz Augusto von Dentz 						      &b->bdaddr,
1876e8907f76SLuiz Augusto von Dentz 						      b->bdaddr_type);
1877e8907f76SLuiz Augusto von Dentz 		pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
1878e8907f76SLuiz Augusto von Dentz 							&b->bdaddr,
1879e8907f76SLuiz Augusto von Dentz 							b->bdaddr_type);
1880e8907f76SLuiz Augusto von Dentz 
1881e8907f76SLuiz Augusto von Dentz 		/* If the device is not likely to connect or report,
1882e8907f76SLuiz Augusto von Dentz 		 * remove it from the acceptlist.
1883e8907f76SLuiz Augusto von Dentz 		 */
1884e8907f76SLuiz Augusto von Dentz 		if (!pend_conn && !pend_report) {
1885e8907f76SLuiz Augusto von Dentz 			hci_le_del_accept_list_sync(hdev, &b->bdaddr,
1886e8907f76SLuiz Augusto von Dentz 						    b->bdaddr_type);
1887e8907f76SLuiz Augusto von Dentz 			continue;
1888e8907f76SLuiz Augusto von Dentz 		}
1889e8907f76SLuiz Augusto von Dentz 
1890e8907f76SLuiz Augusto von Dentz 		num_entries++;
1891e8907f76SLuiz Augusto von Dentz 	}
1892e8907f76SLuiz Augusto von Dentz 
1893e8907f76SLuiz Augusto von Dentz 	/* Since all no longer valid accept list entries have been
1894e8907f76SLuiz Augusto von Dentz 	 * removed, walk through the list of pending connections
1895e8907f76SLuiz Augusto von Dentz 	 * and ensure that any new device gets programmed into
1896e8907f76SLuiz Augusto von Dentz 	 * the controller.
1897e8907f76SLuiz Augusto von Dentz 	 *
1898e8907f76SLuiz Augusto von Dentz 	 * If the list of the devices is larger than the list of
1899e8907f76SLuiz Augusto von Dentz 	 * available accept list entries in the controller, then
1900e8907f76SLuiz Augusto von Dentz 	 * just abort and return filer policy value to not use the
1901e8907f76SLuiz Augusto von Dentz 	 * accept list.
1902e8907f76SLuiz Augusto von Dentz 	 */
1903e8907f76SLuiz Augusto von Dentz 	list_for_each_entry(params, &hdev->pend_le_conns, action) {
1904ad383c2cSLuiz Augusto von Dentz 		err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
1905ad383c2cSLuiz Augusto von Dentz 		if (err)
1906ad383c2cSLuiz Augusto von Dentz 			goto done;
1907e8907f76SLuiz Augusto von Dentz 	}
1908e8907f76SLuiz Augusto von Dentz 
1909e8907f76SLuiz Augusto von Dentz 	/* After adding all new pending connections, walk through
1910e8907f76SLuiz Augusto von Dentz 	 * the list of pending reports and also add these to the
1911e8907f76SLuiz Augusto von Dentz 	 * accept list if there is still space. Abort if space runs out.
1912e8907f76SLuiz Augusto von Dentz 	 */
1913e8907f76SLuiz Augusto von Dentz 	list_for_each_entry(params, &hdev->pend_le_reports, action) {
1914ad383c2cSLuiz Augusto von Dentz 		err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
1915ad383c2cSLuiz Augusto von Dentz 		if (err)
1916ad383c2cSLuiz Augusto von Dentz 			goto done;
1917e8907f76SLuiz Augusto von Dentz 	}
1918e8907f76SLuiz Augusto von Dentz 
1919e8907f76SLuiz Augusto von Dentz 	/* Use the allowlist unless the following conditions are all true:
1920e8907f76SLuiz Augusto von Dentz 	 * - We are not currently suspending
1921e8907f76SLuiz Augusto von Dentz 	 * - There are 1 or more ADV monitors registered and it's not offloaded
1922e8907f76SLuiz Augusto von Dentz 	 * - Interleaved scanning is not currently using the allowlist
1923e8907f76SLuiz Augusto von Dentz 	 */
1924e8907f76SLuiz Augusto von Dentz 	if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
1925e8907f76SLuiz Augusto von Dentz 	    hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
1926e8907f76SLuiz Augusto von Dentz 	    hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
1927ad383c2cSLuiz Augusto von Dentz 		err = -EINVAL;
1928ad383c2cSLuiz Augusto von Dentz 
1929ad383c2cSLuiz Augusto von Dentz done:
193080740ebbSLuiz Augusto von Dentz 	filter_policy = err ? 0x00 : 0x01;
193180740ebbSLuiz Augusto von Dentz 
1932ad383c2cSLuiz Augusto von Dentz 	/* Enable address resolution when LL Privacy is enabled. */
1933ad383c2cSLuiz Augusto von Dentz 	err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
1934ad383c2cSLuiz Augusto von Dentz 	if (err)
1935ad383c2cSLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
1936ad383c2cSLuiz Augusto von Dentz 
1937ad383c2cSLuiz Augusto von Dentz 	/* Resume advertising if it was paused */
1938ad383c2cSLuiz Augusto von Dentz 	if (use_ll_privacy(hdev))
1939ad383c2cSLuiz Augusto von Dentz 		hci_resume_advertising_sync(hdev);
1940e8907f76SLuiz Augusto von Dentz 
1941e8907f76SLuiz Augusto von Dentz 	/* Select filter policy to use accept list */
194280740ebbSLuiz Augusto von Dentz 	return filter_policy;
1943e8907f76SLuiz Augusto von Dentz }
1944e8907f76SLuiz Augusto von Dentz 
1945e8907f76SLuiz Augusto von Dentz /* Returns true if an le connection is in the scanning state */
1946e8907f76SLuiz Augusto von Dentz static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
1947e8907f76SLuiz Augusto von Dentz {
1948e8907f76SLuiz Augusto von Dentz 	struct hci_conn_hash *h = &hdev->conn_hash;
1949e8907f76SLuiz Augusto von Dentz 	struct hci_conn  *c;
1950e8907f76SLuiz Augusto von Dentz 
1951e8907f76SLuiz Augusto von Dentz 	rcu_read_lock();
1952e8907f76SLuiz Augusto von Dentz 
1953e8907f76SLuiz Augusto von Dentz 	list_for_each_entry_rcu(c, &h->list, list) {
1954e8907f76SLuiz Augusto von Dentz 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
1955e8907f76SLuiz Augusto von Dentz 		    test_bit(HCI_CONN_SCANNING, &c->flags)) {
1956e8907f76SLuiz Augusto von Dentz 			rcu_read_unlock();
1957e8907f76SLuiz Augusto von Dentz 			return true;
1958e8907f76SLuiz Augusto von Dentz 		}
1959e8907f76SLuiz Augusto von Dentz 	}
1960e8907f76SLuiz Augusto von Dentz 
1961e8907f76SLuiz Augusto von Dentz 	rcu_read_unlock();
1962e8907f76SLuiz Augusto von Dentz 
1963e8907f76SLuiz Augusto von Dentz 	return false;
1964e8907f76SLuiz Augusto von Dentz }
1965e8907f76SLuiz Augusto von Dentz 
1966e8907f76SLuiz Augusto von Dentz static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
1967e8907f76SLuiz Augusto von Dentz 					  u16 interval, u16 window,
1968e8907f76SLuiz Augusto von Dentz 					  u8 own_addr_type, u8 filter_policy)
1969e8907f76SLuiz Augusto von Dentz {
1970e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_scan_params *cp;
1971e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_scan_phy_params *phy;
1972e8907f76SLuiz Augusto von Dentz 	u8 data[sizeof(*cp) + sizeof(*phy) * 2];
1973e8907f76SLuiz Augusto von Dentz 	u8 num_phy = 0;
1974e8907f76SLuiz Augusto von Dentz 
1975e8907f76SLuiz Augusto von Dentz 	cp = (void *)data;
1976e8907f76SLuiz Augusto von Dentz 	phy = (void *)cp->data;
1977e8907f76SLuiz Augusto von Dentz 
1978e8907f76SLuiz Augusto von Dentz 	memset(data, 0, sizeof(data));
1979e8907f76SLuiz Augusto von Dentz 
1980e8907f76SLuiz Augusto von Dentz 	cp->own_addr_type = own_addr_type;
1981e8907f76SLuiz Augusto von Dentz 	cp->filter_policy = filter_policy;
1982e8907f76SLuiz Augusto von Dentz 
1983e8907f76SLuiz Augusto von Dentz 	if (scan_1m(hdev) || scan_2m(hdev)) {
1984e8907f76SLuiz Augusto von Dentz 		cp->scanning_phys |= LE_SCAN_PHY_1M;
1985e8907f76SLuiz Augusto von Dentz 
1986e8907f76SLuiz Augusto von Dentz 		phy->type = type;
1987e8907f76SLuiz Augusto von Dentz 		phy->interval = cpu_to_le16(interval);
1988e8907f76SLuiz Augusto von Dentz 		phy->window = cpu_to_le16(window);
1989e8907f76SLuiz Augusto von Dentz 
1990e8907f76SLuiz Augusto von Dentz 		num_phy++;
1991e8907f76SLuiz Augusto von Dentz 		phy++;
1992e8907f76SLuiz Augusto von Dentz 	}
1993e8907f76SLuiz Augusto von Dentz 
1994e8907f76SLuiz Augusto von Dentz 	if (scan_coded(hdev)) {
1995e8907f76SLuiz Augusto von Dentz 		cp->scanning_phys |= LE_SCAN_PHY_CODED;
1996e8907f76SLuiz Augusto von Dentz 
1997e8907f76SLuiz Augusto von Dentz 		phy->type = type;
1998e8907f76SLuiz Augusto von Dentz 		phy->interval = cpu_to_le16(interval);
1999e8907f76SLuiz Augusto von Dentz 		phy->window = cpu_to_le16(window);
2000e8907f76SLuiz Augusto von Dentz 
2001e8907f76SLuiz Augusto von Dentz 		num_phy++;
2002e8907f76SLuiz Augusto von Dentz 		phy++;
2003e8907f76SLuiz Augusto von Dentz 	}
2004e8907f76SLuiz Augusto von Dentz 
2005e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2006e8907f76SLuiz Augusto von Dentz 				     sizeof(*cp) + sizeof(*phy) * num_phy,
2007e8907f76SLuiz Augusto von Dentz 				     data, HCI_CMD_TIMEOUT);
2008e8907f76SLuiz Augusto von Dentz }
2009e8907f76SLuiz Augusto von Dentz 
2010e8907f76SLuiz Augusto von Dentz static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2011e8907f76SLuiz Augusto von Dentz 				      u16 interval, u16 window,
2012e8907f76SLuiz Augusto von Dentz 				      u8 own_addr_type, u8 filter_policy)
2013e8907f76SLuiz Augusto von Dentz {
2014e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_set_scan_param cp;
2015e8907f76SLuiz Augusto von Dentz 
2016e8907f76SLuiz Augusto von Dentz 	if (use_ext_scan(hdev))
2017e8907f76SLuiz Augusto von Dentz 		return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2018e8907f76SLuiz Augusto von Dentz 						      window, own_addr_type,
2019e8907f76SLuiz Augusto von Dentz 						      filter_policy);
2020e8907f76SLuiz Augusto von Dentz 
2021e8907f76SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
2022e8907f76SLuiz Augusto von Dentz 	cp.type = type;
2023e8907f76SLuiz Augusto von Dentz 	cp.interval = cpu_to_le16(interval);
2024e8907f76SLuiz Augusto von Dentz 	cp.window = cpu_to_le16(window);
2025e8907f76SLuiz Augusto von Dentz 	cp.own_address_type = own_addr_type;
2026e8907f76SLuiz Augusto von Dentz 	cp.filter_policy = filter_policy;
2027e8907f76SLuiz Augusto von Dentz 
2028e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2029e8907f76SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2030e8907f76SLuiz Augusto von Dentz }
2031e8907f76SLuiz Augusto von Dentz 
2032e8907f76SLuiz Augusto von Dentz static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2033abfeea47SLuiz Augusto von Dentz 			       u16 window, u8 own_addr_type, u8 filter_policy,
2034abfeea47SLuiz Augusto von Dentz 			       u8 filter_dup)
2035e8907f76SLuiz Augusto von Dentz {
2036e8907f76SLuiz Augusto von Dentz 	int err;
2037e8907f76SLuiz Augusto von Dentz 
2038e8907f76SLuiz Augusto von Dentz 	if (hdev->scanning_paused) {
2039e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
2040e8907f76SLuiz Augusto von Dentz 		return 0;
2041e8907f76SLuiz Augusto von Dentz 	}
2042e8907f76SLuiz Augusto von Dentz 
2043e8907f76SLuiz Augusto von Dentz 	err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2044e8907f76SLuiz Augusto von Dentz 					 own_addr_type, filter_policy);
2045e8907f76SLuiz Augusto von Dentz 	if (err)
2046e8907f76SLuiz Augusto von Dentz 		return err;
2047e8907f76SLuiz Augusto von Dentz 
2048abfeea47SLuiz Augusto von Dentz 	return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2049e8907f76SLuiz Augusto von Dentz }
2050e8907f76SLuiz Augusto von Dentz 
205127592ca1SLuiz Augusto von Dentz static int hci_passive_scan_sync(struct hci_dev *hdev)
2052e8907f76SLuiz Augusto von Dentz {
2053e8907f76SLuiz Augusto von Dentz 	u8 own_addr_type;
2054e8907f76SLuiz Augusto von Dentz 	u8 filter_policy;
2055e8907f76SLuiz Augusto von Dentz 	u16 window, interval;
2056ad383c2cSLuiz Augusto von Dentz 	int err;
2057e8907f76SLuiz Augusto von Dentz 
2058e8907f76SLuiz Augusto von Dentz 	if (hdev->scanning_paused) {
2059e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
2060e8907f76SLuiz Augusto von Dentz 		return 0;
2061e8907f76SLuiz Augusto von Dentz 	}
2062e8907f76SLuiz Augusto von Dentz 
2063ad383c2cSLuiz Augusto von Dentz 	err = hci_scan_disable_sync(hdev);
2064ad383c2cSLuiz Augusto von Dentz 	if (err) {
2065ad383c2cSLuiz Augusto von Dentz 		bt_dev_err(hdev, "disable scanning failed: %d", err);
2066ad383c2cSLuiz Augusto von Dentz 		return err;
2067ad383c2cSLuiz Augusto von Dentz 	}
2068ad383c2cSLuiz Augusto von Dentz 
2069e8907f76SLuiz Augusto von Dentz 	/* Set require_privacy to false since no SCAN_REQ are send
2070e8907f76SLuiz Augusto von Dentz 	 * during passive scanning. Not using an non-resolvable address
2071e8907f76SLuiz Augusto von Dentz 	 * here is important so that peer devices using direct
2072e8907f76SLuiz Augusto von Dentz 	 * advertising with our address will be correctly reported
2073e8907f76SLuiz Augusto von Dentz 	 * by the controller.
2074e8907f76SLuiz Augusto von Dentz 	 */
2075e8907f76SLuiz Augusto von Dentz 	if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2076e8907f76SLuiz Augusto von Dentz 					   &own_addr_type))
2077e8907f76SLuiz Augusto von Dentz 		return 0;
2078e8907f76SLuiz Augusto von Dentz 
2079e8907f76SLuiz Augusto von Dentz 	if (hdev->enable_advmon_interleave_scan &&
2080e8907f76SLuiz Augusto von Dentz 	    hci_update_interleaved_scan_sync(hdev))
2081e8907f76SLuiz Augusto von Dentz 		return 0;
2082e8907f76SLuiz Augusto von Dentz 
2083e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2084ad383c2cSLuiz Augusto von Dentz 
2085e8907f76SLuiz Augusto von Dentz 	/* Adding or removing entries from the accept list must
2086e8907f76SLuiz Augusto von Dentz 	 * happen before enabling scanning. The controller does
2087e8907f76SLuiz Augusto von Dentz 	 * not allow accept list modification while scanning.
2088e8907f76SLuiz Augusto von Dentz 	 */
2089e8907f76SLuiz Augusto von Dentz 	filter_policy = hci_update_accept_list_sync(hdev);
2090e8907f76SLuiz Augusto von Dentz 
2091e8907f76SLuiz Augusto von Dentz 	/* When the controller is using random resolvable addresses and
2092e8907f76SLuiz Augusto von Dentz 	 * with that having LE privacy enabled, then controllers with
2093e8907f76SLuiz Augusto von Dentz 	 * Extended Scanner Filter Policies support can now enable support
2094e8907f76SLuiz Augusto von Dentz 	 * for handling directed advertising.
2095e8907f76SLuiz Augusto von Dentz 	 *
2096e8907f76SLuiz Augusto von Dentz 	 * So instead of using filter polices 0x00 (no acceptlist)
2097e8907f76SLuiz Augusto von Dentz 	 * and 0x01 (acceptlist enabled) use the new filter policies
2098e8907f76SLuiz Augusto von Dentz 	 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2099e8907f76SLuiz Augusto von Dentz 	 */
2100e8907f76SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2101e8907f76SLuiz Augusto von Dentz 	    (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2102e8907f76SLuiz Augusto von Dentz 		filter_policy |= 0x02;
2103e8907f76SLuiz Augusto von Dentz 
2104e8907f76SLuiz Augusto von Dentz 	if (hdev->suspended) {
2105e8907f76SLuiz Augusto von Dentz 		window = hdev->le_scan_window_suspend;
2106e8907f76SLuiz Augusto von Dentz 		interval = hdev->le_scan_int_suspend;
2107e8907f76SLuiz Augusto von Dentz 	} else if (hci_is_le_conn_scanning(hdev)) {
2108e8907f76SLuiz Augusto von Dentz 		window = hdev->le_scan_window_connect;
2109e8907f76SLuiz Augusto von Dentz 		interval = hdev->le_scan_int_connect;
2110e8907f76SLuiz Augusto von Dentz 	} else if (hci_is_adv_monitoring(hdev)) {
2111e8907f76SLuiz Augusto von Dentz 		window = hdev->le_scan_window_adv_monitor;
2112e8907f76SLuiz Augusto von Dentz 		interval = hdev->le_scan_int_adv_monitor;
2113e8907f76SLuiz Augusto von Dentz 	} else {
2114e8907f76SLuiz Augusto von Dentz 		window = hdev->le_scan_window;
2115e8907f76SLuiz Augusto von Dentz 		interval = hdev->le_scan_interval;
2116e8907f76SLuiz Augusto von Dentz 	}
2117e8907f76SLuiz Augusto von Dentz 
2118e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2119e8907f76SLuiz Augusto von Dentz 
2120e8907f76SLuiz Augusto von Dentz 	return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2121abfeea47SLuiz Augusto von Dentz 				   own_addr_type, filter_policy,
2122abfeea47SLuiz Augusto von Dentz 				   LE_SCAN_FILTER_DUP_ENABLE);
2123e8907f76SLuiz Augusto von Dentz }
2124e8907f76SLuiz Augusto von Dentz 
2125e8907f76SLuiz Augusto von Dentz /* This function controls the passive scanning based on hdev->pend_le_conns
2126e8907f76SLuiz Augusto von Dentz  * list. If there are pending LE connection we start the background scanning,
2127ad383c2cSLuiz Augusto von Dentz  * otherwise we stop it in the following sequence:
2128ad383c2cSLuiz Augusto von Dentz  *
2129ad383c2cSLuiz Augusto von Dentz  * If there are devices to scan:
2130ad383c2cSLuiz Augusto von Dentz  *
2131ad383c2cSLuiz Augusto von Dentz  * Disable Scanning -> Update Accept List ->
2132ad383c2cSLuiz Augusto von Dentz  * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2133ad383c2cSLuiz Augusto von Dentz  * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2134ad383c2cSLuiz Augusto von Dentz  * Enable Scanning
2135ad383c2cSLuiz Augusto von Dentz  *
2136ad383c2cSLuiz Augusto von Dentz  * Otherwise:
2137ad383c2cSLuiz Augusto von Dentz  *
2138ad383c2cSLuiz Augusto von Dentz  * Disable Scanning
2139e8907f76SLuiz Augusto von Dentz  */
2140e8907f76SLuiz Augusto von Dentz int hci_update_passive_scan_sync(struct hci_dev *hdev)
2141e8907f76SLuiz Augusto von Dentz {
2142e8907f76SLuiz Augusto von Dentz 	int err;
2143e8907f76SLuiz Augusto von Dentz 
2144e8907f76SLuiz Augusto von Dentz 	if (!test_bit(HCI_UP, &hdev->flags) ||
2145e8907f76SLuiz Augusto von Dentz 	    test_bit(HCI_INIT, &hdev->flags) ||
2146e8907f76SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_SETUP) ||
2147e8907f76SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_CONFIG) ||
2148e8907f76SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2149e8907f76SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_UNREGISTER))
2150e8907f76SLuiz Augusto von Dentz 		return 0;
2151e8907f76SLuiz Augusto von Dentz 
2152e8907f76SLuiz Augusto von Dentz 	/* No point in doing scanning if LE support hasn't been enabled */
2153e8907f76SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2154e8907f76SLuiz Augusto von Dentz 		return 0;
2155e8907f76SLuiz Augusto von Dentz 
2156e8907f76SLuiz Augusto von Dentz 	/* If discovery is active don't interfere with it */
2157e8907f76SLuiz Augusto von Dentz 	if (hdev->discovery.state != DISCOVERY_STOPPED)
2158e8907f76SLuiz Augusto von Dentz 		return 0;
2159e8907f76SLuiz Augusto von Dentz 
2160e8907f76SLuiz Augusto von Dentz 	/* Reset RSSI and UUID filters when starting background scanning
2161e8907f76SLuiz Augusto von Dentz 	 * since these filters are meant for service discovery only.
2162e8907f76SLuiz Augusto von Dentz 	 *
2163e8907f76SLuiz Augusto von Dentz 	 * The Start Discovery and Start Service Discovery operations
2164e8907f76SLuiz Augusto von Dentz 	 * ensure to set proper values for RSSI threshold and UUID
2165e8907f76SLuiz Augusto von Dentz 	 * filter list. So it is safe to just reset them here.
2166e8907f76SLuiz Augusto von Dentz 	 */
2167e8907f76SLuiz Augusto von Dentz 	hci_discovery_filter_clear(hdev);
2168e8907f76SLuiz Augusto von Dentz 
2169e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "ADV monitoring is %s",
2170e8907f76SLuiz Augusto von Dentz 		   hci_is_adv_monitoring(hdev) ? "on" : "off");
2171e8907f76SLuiz Augusto von Dentz 
2172e8907f76SLuiz Augusto von Dentz 	if (list_empty(&hdev->pend_le_conns) &&
2173e8907f76SLuiz Augusto von Dentz 	    list_empty(&hdev->pend_le_reports) &&
2174e8907f76SLuiz Augusto von Dentz 	    !hci_is_adv_monitoring(hdev)) {
2175e8907f76SLuiz Augusto von Dentz 		/* If there is no pending LE connections or devices
2176e8907f76SLuiz Augusto von Dentz 		 * to be scanned for or no ADV monitors, we should stop the
2177e8907f76SLuiz Augusto von Dentz 		 * background scanning.
2178e8907f76SLuiz Augusto von Dentz 		 */
2179e8907f76SLuiz Augusto von Dentz 
2180e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "stopping background scanning");
2181e8907f76SLuiz Augusto von Dentz 
2182ad383c2cSLuiz Augusto von Dentz 		err = hci_scan_disable_sync(hdev);
2183e8907f76SLuiz Augusto von Dentz 		if (err)
2184e8907f76SLuiz Augusto von Dentz 			bt_dev_err(hdev, "stop background scanning failed: %d",
2185e8907f76SLuiz Augusto von Dentz 				   err);
2186e8907f76SLuiz Augusto von Dentz 	} else {
2187e8907f76SLuiz Augusto von Dentz 		/* If there is at least one pending LE connection, we should
2188e8907f76SLuiz Augusto von Dentz 		 * keep the background scan running.
2189e8907f76SLuiz Augusto von Dentz 		 */
2190e8907f76SLuiz Augusto von Dentz 
2191e8907f76SLuiz Augusto von Dentz 		/* If controller is connecting, we should not start scanning
2192e8907f76SLuiz Augusto von Dentz 		 * since some controllers are not able to scan and connect at
2193e8907f76SLuiz Augusto von Dentz 		 * the same time.
2194e8907f76SLuiz Augusto von Dentz 		 */
2195e8907f76SLuiz Augusto von Dentz 		if (hci_lookup_le_connect(hdev))
2196e8907f76SLuiz Augusto von Dentz 			return 0;
2197e8907f76SLuiz Augusto von Dentz 
2198e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "start background scanning");
2199e8907f76SLuiz Augusto von Dentz 
2200e8907f76SLuiz Augusto von Dentz 		err = hci_passive_scan_sync(hdev);
2201e8907f76SLuiz Augusto von Dentz 		if (err)
2202e8907f76SLuiz Augusto von Dentz 			bt_dev_err(hdev, "start background scanning failed: %d",
2203e8907f76SLuiz Augusto von Dentz 				   err);
2204e8907f76SLuiz Augusto von Dentz 	}
2205e8907f76SLuiz Augusto von Dentz 
2206e8907f76SLuiz Augusto von Dentz 	return err;
2207e8907f76SLuiz Augusto von Dentz }
2208ad383c2cSLuiz Augusto von Dentz 
2209ad383c2cSLuiz Augusto von Dentz static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2210ad383c2cSLuiz Augusto von Dentz {
2211ad383c2cSLuiz Augusto von Dentz 	return hci_update_passive_scan_sync(hdev);
2212ad383c2cSLuiz Augusto von Dentz }
2213ad383c2cSLuiz Augusto von Dentz 
2214ad383c2cSLuiz Augusto von Dentz int hci_update_passive_scan(struct hci_dev *hdev)
2215ad383c2cSLuiz Augusto von Dentz {
22165bee2fd6SLuiz Augusto von Dentz 	/* Only queue if it would have any effect */
22175bee2fd6SLuiz Augusto von Dentz 	if (!test_bit(HCI_UP, &hdev->flags) ||
22185bee2fd6SLuiz Augusto von Dentz 	    test_bit(HCI_INIT, &hdev->flags) ||
22195bee2fd6SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_SETUP) ||
22205bee2fd6SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_CONFIG) ||
22215bee2fd6SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
22225bee2fd6SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_UNREGISTER))
22235bee2fd6SLuiz Augusto von Dentz 		return 0;
22245bee2fd6SLuiz Augusto von Dentz 
2225ad383c2cSLuiz Augusto von Dentz 	return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2226ad383c2cSLuiz Augusto von Dentz }
2227cf75ad8bSLuiz Augusto von Dentz 
22282f2eb0c9SBrian Gix int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2229cf75ad8bSLuiz Augusto von Dentz {
22302f2eb0c9SBrian Gix 	int err;
22312f2eb0c9SBrian Gix 
2232cf75ad8bSLuiz Augusto von Dentz 	if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2233cf75ad8bSLuiz Augusto von Dentz 		return 0;
2234cf75ad8bSLuiz Augusto von Dentz 
22352f2eb0c9SBrian Gix 	err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2236cf75ad8bSLuiz Augusto von Dentz 				    sizeof(val), &val, HCI_CMD_TIMEOUT);
22372f2eb0c9SBrian Gix 
22382f2eb0c9SBrian Gix 	if (!err) {
22392f2eb0c9SBrian Gix 		if (val) {
22402f2eb0c9SBrian Gix 			hdev->features[1][0] |= LMP_HOST_SC;
22412f2eb0c9SBrian Gix 			hci_dev_set_flag(hdev, HCI_SC_ENABLED);
22422f2eb0c9SBrian Gix 		} else {
22432f2eb0c9SBrian Gix 			hdev->features[1][0] &= ~LMP_HOST_SC;
22442f2eb0c9SBrian Gix 			hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
22452f2eb0c9SBrian Gix 		}
22462f2eb0c9SBrian Gix 	}
22472f2eb0c9SBrian Gix 
22482f2eb0c9SBrian Gix 	return err;
2249cf75ad8bSLuiz Augusto von Dentz }
2250cf75ad8bSLuiz Augusto von Dentz 
22513244845cSBrian Gix int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2252cf75ad8bSLuiz Augusto von Dentz {
2253cf75ad8bSLuiz Augusto von Dentz 	int err;
2254cf75ad8bSLuiz Augusto von Dentz 
2255cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2256cf75ad8bSLuiz Augusto von Dentz 	    lmp_host_ssp_capable(hdev))
2257cf75ad8bSLuiz Augusto von Dentz 		return 0;
2258cf75ad8bSLuiz Augusto von Dentz 
22593244845cSBrian Gix 	if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
22603244845cSBrian Gix 		__hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
22613244845cSBrian Gix 				      sizeof(mode), &mode, HCI_CMD_TIMEOUT);
22623244845cSBrian Gix 	}
22633244845cSBrian Gix 
2264cf75ad8bSLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2265cf75ad8bSLuiz Augusto von Dentz 				    sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2266cf75ad8bSLuiz Augusto von Dentz 	if (err)
2267cf75ad8bSLuiz Augusto von Dentz 		return err;
2268cf75ad8bSLuiz Augusto von Dentz 
2269cf75ad8bSLuiz Augusto von Dentz 	return hci_write_sc_support_sync(hdev, 0x01);
2270cf75ad8bSLuiz Augusto von Dentz }
2271cf75ad8bSLuiz Augusto von Dentz 
2272d81a494cSBrian Gix int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
2273cf75ad8bSLuiz Augusto von Dentz {
2274cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_write_le_host_supported cp;
2275cf75ad8bSLuiz Augusto von Dentz 
2276cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
2277cf75ad8bSLuiz Augusto von Dentz 	    !lmp_bredr_capable(hdev))
2278cf75ad8bSLuiz Augusto von Dentz 		return 0;
2279cf75ad8bSLuiz Augusto von Dentz 
2280cf75ad8bSLuiz Augusto von Dentz 	/* Check first if we already have the right host state
2281cf75ad8bSLuiz Augusto von Dentz 	 * (host features set)
2282cf75ad8bSLuiz Augusto von Dentz 	 */
2283cf75ad8bSLuiz Augusto von Dentz 	if (le == lmp_host_le_capable(hdev) &&
2284cf75ad8bSLuiz Augusto von Dentz 	    simul == lmp_host_le_br_capable(hdev))
2285cf75ad8bSLuiz Augusto von Dentz 		return 0;
2286cf75ad8bSLuiz Augusto von Dentz 
2287cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
2288cf75ad8bSLuiz Augusto von Dentz 
2289cf75ad8bSLuiz Augusto von Dentz 	cp.le = le;
2290cf75ad8bSLuiz Augusto von Dentz 	cp.simul = simul;
2291cf75ad8bSLuiz Augusto von Dentz 
2292cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
2293cf75ad8bSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2294cf75ad8bSLuiz Augusto von Dentz }
2295cf75ad8bSLuiz Augusto von Dentz 
2296cf75ad8bSLuiz Augusto von Dentz static int hci_powered_update_adv_sync(struct hci_dev *hdev)
2297cf75ad8bSLuiz Augusto von Dentz {
2298cf75ad8bSLuiz Augusto von Dentz 	struct adv_info *adv, *tmp;
2299cf75ad8bSLuiz Augusto von Dentz 	int err;
2300cf75ad8bSLuiz Augusto von Dentz 
2301cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2302cf75ad8bSLuiz Augusto von Dentz 		return 0;
2303cf75ad8bSLuiz Augusto von Dentz 
2304cf75ad8bSLuiz Augusto von Dentz 	/* If RPA Resolution has not been enable yet it means the
2305cf75ad8bSLuiz Augusto von Dentz 	 * resolving list is empty and we should attempt to program the
2306cf75ad8bSLuiz Augusto von Dentz 	 * local IRK in order to support using own_addr_type
2307cf75ad8bSLuiz Augusto von Dentz 	 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
2308cf75ad8bSLuiz Augusto von Dentz 	 */
2309cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
2310cf75ad8bSLuiz Augusto von Dentz 		hci_le_add_resolve_list_sync(hdev, NULL);
2311cf75ad8bSLuiz Augusto von Dentz 		hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2312cf75ad8bSLuiz Augusto von Dentz 	}
2313cf75ad8bSLuiz Augusto von Dentz 
2314cf75ad8bSLuiz Augusto von Dentz 	/* Make sure the controller has a good default for
2315cf75ad8bSLuiz Augusto von Dentz 	 * advertising data. This also applies to the case
2316cf75ad8bSLuiz Augusto von Dentz 	 * where BR/EDR was toggled during the AUTO_OFF phase.
2317cf75ad8bSLuiz Augusto von Dentz 	 */
2318cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2319cf75ad8bSLuiz Augusto von Dentz 	    list_empty(&hdev->adv_instances)) {
2320cf75ad8bSLuiz Augusto von Dentz 		if (ext_adv_capable(hdev)) {
2321cf75ad8bSLuiz Augusto von Dentz 			err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
2322cf75ad8bSLuiz Augusto von Dentz 			if (!err)
2323cf75ad8bSLuiz Augusto von Dentz 				hci_update_scan_rsp_data_sync(hdev, 0x00);
2324cf75ad8bSLuiz Augusto von Dentz 		} else {
2325cf75ad8bSLuiz Augusto von Dentz 			err = hci_update_adv_data_sync(hdev, 0x00);
2326cf75ad8bSLuiz Augusto von Dentz 			if (!err)
2327cf75ad8bSLuiz Augusto von Dentz 				hci_update_scan_rsp_data_sync(hdev, 0x00);
2328cf75ad8bSLuiz Augusto von Dentz 		}
2329cf75ad8bSLuiz Augusto von Dentz 
2330cf75ad8bSLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2331cf75ad8bSLuiz Augusto von Dentz 			hci_enable_advertising_sync(hdev);
2332cf75ad8bSLuiz Augusto von Dentz 	}
2333cf75ad8bSLuiz Augusto von Dentz 
2334cf75ad8bSLuiz Augusto von Dentz 	/* Call for each tracked instance to be scheduled */
2335cf75ad8bSLuiz Augusto von Dentz 	list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
2336cf75ad8bSLuiz Augusto von Dentz 		hci_schedule_adv_instance_sync(hdev, adv->instance, true);
2337cf75ad8bSLuiz Augusto von Dentz 
2338cf75ad8bSLuiz Augusto von Dentz 	return 0;
2339cf75ad8bSLuiz Augusto von Dentz }
2340cf75ad8bSLuiz Augusto von Dentz 
2341cf75ad8bSLuiz Augusto von Dentz static int hci_write_auth_enable_sync(struct hci_dev *hdev)
2342cf75ad8bSLuiz Augusto von Dentz {
2343cf75ad8bSLuiz Augusto von Dentz 	u8 link_sec;
2344cf75ad8bSLuiz Augusto von Dentz 
2345cf75ad8bSLuiz Augusto von Dentz 	link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
2346cf75ad8bSLuiz Augusto von Dentz 	if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
2347cf75ad8bSLuiz Augusto von Dentz 		return 0;
2348cf75ad8bSLuiz Augusto von Dentz 
2349cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
2350cf75ad8bSLuiz Augusto von Dentz 				     sizeof(link_sec), &link_sec,
2351cf75ad8bSLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
2352cf75ad8bSLuiz Augusto von Dentz }
2353cf75ad8bSLuiz Augusto von Dentz 
2354353a0249SBrian Gix int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
2355cf75ad8bSLuiz Augusto von Dentz {
2356cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_write_page_scan_activity cp;
2357cf75ad8bSLuiz Augusto von Dentz 	u8 type;
2358cf75ad8bSLuiz Augusto von Dentz 	int err = 0;
2359cf75ad8bSLuiz Augusto von Dentz 
2360cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2361cf75ad8bSLuiz Augusto von Dentz 		return 0;
2362cf75ad8bSLuiz Augusto von Dentz 
2363cf75ad8bSLuiz Augusto von Dentz 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
2364cf75ad8bSLuiz Augusto von Dentz 		return 0;
2365cf75ad8bSLuiz Augusto von Dentz 
2366cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
2367cf75ad8bSLuiz Augusto von Dentz 
2368cf75ad8bSLuiz Augusto von Dentz 	if (enable) {
2369cf75ad8bSLuiz Augusto von Dentz 		type = PAGE_SCAN_TYPE_INTERLACED;
2370cf75ad8bSLuiz Augusto von Dentz 
2371cf75ad8bSLuiz Augusto von Dentz 		/* 160 msec page scan interval */
2372cf75ad8bSLuiz Augusto von Dentz 		cp.interval = cpu_to_le16(0x0100);
2373cf75ad8bSLuiz Augusto von Dentz 	} else {
2374cf75ad8bSLuiz Augusto von Dentz 		type = hdev->def_page_scan_type;
2375cf75ad8bSLuiz Augusto von Dentz 		cp.interval = cpu_to_le16(hdev->def_page_scan_int);
2376cf75ad8bSLuiz Augusto von Dentz 	}
2377cf75ad8bSLuiz Augusto von Dentz 
2378cf75ad8bSLuiz Augusto von Dentz 	cp.window = cpu_to_le16(hdev->def_page_scan_window);
2379cf75ad8bSLuiz Augusto von Dentz 
2380cf75ad8bSLuiz Augusto von Dentz 	if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
2381cf75ad8bSLuiz Augusto von Dentz 	    __cpu_to_le16(hdev->page_scan_window) != cp.window) {
2382cf75ad8bSLuiz Augusto von Dentz 		err = __hci_cmd_sync_status(hdev,
2383cf75ad8bSLuiz Augusto von Dentz 					    HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
2384cf75ad8bSLuiz Augusto von Dentz 					    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2385cf75ad8bSLuiz Augusto von Dentz 		if (err)
2386cf75ad8bSLuiz Augusto von Dentz 			return err;
2387cf75ad8bSLuiz Augusto von Dentz 	}
2388cf75ad8bSLuiz Augusto von Dentz 
2389cf75ad8bSLuiz Augusto von Dentz 	if (hdev->page_scan_type != type)
2390cf75ad8bSLuiz Augusto von Dentz 		err = __hci_cmd_sync_status(hdev,
2391cf75ad8bSLuiz Augusto von Dentz 					    HCI_OP_WRITE_PAGE_SCAN_TYPE,
2392cf75ad8bSLuiz Augusto von Dentz 					    sizeof(type), &type,
2393cf75ad8bSLuiz Augusto von Dentz 					    HCI_CMD_TIMEOUT);
2394cf75ad8bSLuiz Augusto von Dentz 
2395cf75ad8bSLuiz Augusto von Dentz 	return err;
2396cf75ad8bSLuiz Augusto von Dentz }
2397cf75ad8bSLuiz Augusto von Dentz 
2398cf75ad8bSLuiz Augusto von Dentz static bool disconnected_accept_list_entries(struct hci_dev *hdev)
2399cf75ad8bSLuiz Augusto von Dentz {
2400cf75ad8bSLuiz Augusto von Dentz 	struct bdaddr_list *b;
2401cf75ad8bSLuiz Augusto von Dentz 
2402cf75ad8bSLuiz Augusto von Dentz 	list_for_each_entry(b, &hdev->accept_list, list) {
2403cf75ad8bSLuiz Augusto von Dentz 		struct hci_conn *conn;
2404cf75ad8bSLuiz Augusto von Dentz 
2405cf75ad8bSLuiz Augusto von Dentz 		conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
2406cf75ad8bSLuiz Augusto von Dentz 		if (!conn)
2407cf75ad8bSLuiz Augusto von Dentz 			return true;
2408cf75ad8bSLuiz Augusto von Dentz 
2409cf75ad8bSLuiz Augusto von Dentz 		if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2410cf75ad8bSLuiz Augusto von Dentz 			return true;
2411cf75ad8bSLuiz Augusto von Dentz 	}
2412cf75ad8bSLuiz Augusto von Dentz 
2413cf75ad8bSLuiz Augusto von Dentz 	return false;
2414cf75ad8bSLuiz Augusto von Dentz }
2415cf75ad8bSLuiz Augusto von Dentz 
2416cf75ad8bSLuiz Augusto von Dentz static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
2417cf75ad8bSLuiz Augusto von Dentz {
2418cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
2419cf75ad8bSLuiz Augusto von Dentz 					    sizeof(val), &val,
2420cf75ad8bSLuiz Augusto von Dentz 					    HCI_CMD_TIMEOUT);
2421cf75ad8bSLuiz Augusto von Dentz }
2422cf75ad8bSLuiz Augusto von Dentz 
2423451d95a9SBrian Gix int hci_update_scan_sync(struct hci_dev *hdev)
2424cf75ad8bSLuiz Augusto von Dentz {
2425cf75ad8bSLuiz Augusto von Dentz 	u8 scan;
2426cf75ad8bSLuiz Augusto von Dentz 
2427cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2428cf75ad8bSLuiz Augusto von Dentz 		return 0;
2429cf75ad8bSLuiz Augusto von Dentz 
2430cf75ad8bSLuiz Augusto von Dentz 	if (!hdev_is_powered(hdev))
2431cf75ad8bSLuiz Augusto von Dentz 		return 0;
2432cf75ad8bSLuiz Augusto von Dentz 
2433cf75ad8bSLuiz Augusto von Dentz 	if (mgmt_powering_down(hdev))
2434cf75ad8bSLuiz Augusto von Dentz 		return 0;
2435cf75ad8bSLuiz Augusto von Dentz 
2436cf75ad8bSLuiz Augusto von Dentz 	if (hdev->scanning_paused)
2437cf75ad8bSLuiz Augusto von Dentz 		return 0;
2438cf75ad8bSLuiz Augusto von Dentz 
2439cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
2440cf75ad8bSLuiz Augusto von Dentz 	    disconnected_accept_list_entries(hdev))
2441cf75ad8bSLuiz Augusto von Dentz 		scan = SCAN_PAGE;
2442cf75ad8bSLuiz Augusto von Dentz 	else
2443cf75ad8bSLuiz Augusto von Dentz 		scan = SCAN_DISABLED;
2444cf75ad8bSLuiz Augusto von Dentz 
2445cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
2446cf75ad8bSLuiz Augusto von Dentz 		scan |= SCAN_INQUIRY;
2447cf75ad8bSLuiz Augusto von Dentz 
2448cf75ad8bSLuiz Augusto von Dentz 	if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
2449cf75ad8bSLuiz Augusto von Dentz 	    test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
2450cf75ad8bSLuiz Augusto von Dentz 		return 0;
2451cf75ad8bSLuiz Augusto von Dentz 
2452cf75ad8bSLuiz Augusto von Dentz 	return hci_write_scan_enable_sync(hdev, scan);
2453cf75ad8bSLuiz Augusto von Dentz }
2454cf75ad8bSLuiz Augusto von Dentz 
24556f6ff38aSBrian Gix int hci_update_name_sync(struct hci_dev *hdev)
2456cf75ad8bSLuiz Augusto von Dentz {
2457cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_write_local_name cp;
2458cf75ad8bSLuiz Augusto von Dentz 
2459cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
2460cf75ad8bSLuiz Augusto von Dentz 
2461cf75ad8bSLuiz Augusto von Dentz 	memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
2462cf75ad8bSLuiz Augusto von Dentz 
2463cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
2464cf75ad8bSLuiz Augusto von Dentz 					    sizeof(cp), &cp,
2465cf75ad8bSLuiz Augusto von Dentz 					    HCI_CMD_TIMEOUT);
2466cf75ad8bSLuiz Augusto von Dentz }
2467cf75ad8bSLuiz Augusto von Dentz 
2468cf75ad8bSLuiz Augusto von Dentz /* This function perform powered update HCI command sequence after the HCI init
2469cf75ad8bSLuiz Augusto von Dentz  * sequence which end up resetting all states, the sequence is as follows:
2470cf75ad8bSLuiz Augusto von Dentz  *
2471cf75ad8bSLuiz Augusto von Dentz  * HCI_SSP_ENABLED(Enable SSP)
2472cf75ad8bSLuiz Augusto von Dentz  * HCI_LE_ENABLED(Enable LE)
2473cf75ad8bSLuiz Augusto von Dentz  * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
2474cf75ad8bSLuiz Augusto von Dentz  * Update adv data)
2475cf75ad8bSLuiz Augusto von Dentz  * Enable Authentication
2476cf75ad8bSLuiz Augusto von Dentz  * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
2477cf75ad8bSLuiz Augusto von Dentz  * Set Name -> Set EIR)
2478cf75ad8bSLuiz Augusto von Dentz  */
2479cf75ad8bSLuiz Augusto von Dentz int hci_powered_update_sync(struct hci_dev *hdev)
2480cf75ad8bSLuiz Augusto von Dentz {
2481cf75ad8bSLuiz Augusto von Dentz 	int err;
2482cf75ad8bSLuiz Augusto von Dentz 
2483cf75ad8bSLuiz Augusto von Dentz 	/* Register the available SMP channels (BR/EDR and LE) only when
2484cf75ad8bSLuiz Augusto von Dentz 	 * successfully powering on the controller. This late
2485cf75ad8bSLuiz Augusto von Dentz 	 * registration is required so that LE SMP can clearly decide if
2486cf75ad8bSLuiz Augusto von Dentz 	 * the public address or static address is used.
2487cf75ad8bSLuiz Augusto von Dentz 	 */
2488cf75ad8bSLuiz Augusto von Dentz 	smp_register(hdev);
2489cf75ad8bSLuiz Augusto von Dentz 
2490cf75ad8bSLuiz Augusto von Dentz 	err = hci_write_ssp_mode_sync(hdev, 0x01);
2491cf75ad8bSLuiz Augusto von Dentz 	if (err)
2492cf75ad8bSLuiz Augusto von Dentz 		return err;
2493cf75ad8bSLuiz Augusto von Dentz 
2494cf75ad8bSLuiz Augusto von Dentz 	err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
2495cf75ad8bSLuiz Augusto von Dentz 	if (err)
2496cf75ad8bSLuiz Augusto von Dentz 		return err;
2497cf75ad8bSLuiz Augusto von Dentz 
2498cf75ad8bSLuiz Augusto von Dentz 	err = hci_powered_update_adv_sync(hdev);
2499cf75ad8bSLuiz Augusto von Dentz 	if (err)
2500cf75ad8bSLuiz Augusto von Dentz 		return err;
2501cf75ad8bSLuiz Augusto von Dentz 
2502cf75ad8bSLuiz Augusto von Dentz 	err = hci_write_auth_enable_sync(hdev);
2503cf75ad8bSLuiz Augusto von Dentz 	if (err)
2504cf75ad8bSLuiz Augusto von Dentz 		return err;
2505cf75ad8bSLuiz Augusto von Dentz 
2506cf75ad8bSLuiz Augusto von Dentz 	if (lmp_bredr_capable(hdev)) {
2507cf75ad8bSLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
2508cf75ad8bSLuiz Augusto von Dentz 			hci_write_fast_connectable_sync(hdev, true);
2509cf75ad8bSLuiz Augusto von Dentz 		else
2510cf75ad8bSLuiz Augusto von Dentz 			hci_write_fast_connectable_sync(hdev, false);
2511cf75ad8bSLuiz Augusto von Dentz 		hci_update_scan_sync(hdev);
2512cf75ad8bSLuiz Augusto von Dentz 		hci_update_class_sync(hdev);
2513cf75ad8bSLuiz Augusto von Dentz 		hci_update_name_sync(hdev);
2514cf75ad8bSLuiz Augusto von Dentz 		hci_update_eir_sync(hdev);
2515cf75ad8bSLuiz Augusto von Dentz 	}
2516cf75ad8bSLuiz Augusto von Dentz 
2517cf75ad8bSLuiz Augusto von Dentz 	return 0;
2518cf75ad8bSLuiz Augusto von Dentz }
2519cf75ad8bSLuiz Augusto von Dentz 
2520d0b13706SLuiz Augusto von Dentz /**
2521d0b13706SLuiz Augusto von Dentz  * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
2522d0b13706SLuiz Augusto von Dentz  *				       (BD_ADDR) for a HCI device from
2523d0b13706SLuiz Augusto von Dentz  *				       a firmware node property.
2524d0b13706SLuiz Augusto von Dentz  * @hdev:	The HCI device
2525d0b13706SLuiz Augusto von Dentz  *
2526d0b13706SLuiz Augusto von Dentz  * Search the firmware node for 'local-bd-address'.
2527d0b13706SLuiz Augusto von Dentz  *
2528d0b13706SLuiz Augusto von Dentz  * All-zero BD addresses are rejected, because those could be properties
2529d0b13706SLuiz Augusto von Dentz  * that exist in the firmware tables, but were not updated by the firmware. For
2530d0b13706SLuiz Augusto von Dentz  * example, the DTS could define 'local-bd-address', with zero BD addresses.
2531d0b13706SLuiz Augusto von Dentz  */
2532d0b13706SLuiz Augusto von Dentz static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
2533d0b13706SLuiz Augusto von Dentz {
2534d0b13706SLuiz Augusto von Dentz 	struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
2535d0b13706SLuiz Augusto von Dentz 	bdaddr_t ba;
2536d0b13706SLuiz Augusto von Dentz 	int ret;
2537d0b13706SLuiz Augusto von Dentz 
2538d0b13706SLuiz Augusto von Dentz 	ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
2539d0b13706SLuiz Augusto von Dentz 					    (u8 *)&ba, sizeof(ba));
2540d0b13706SLuiz Augusto von Dentz 	if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
2541d0b13706SLuiz Augusto von Dentz 		return;
2542d0b13706SLuiz Augusto von Dentz 
2543d0b13706SLuiz Augusto von Dentz 	bacpy(&hdev->public_addr, &ba);
2544d0b13706SLuiz Augusto von Dentz }
2545d0b13706SLuiz Augusto von Dentz 
2546d0b13706SLuiz Augusto von Dentz struct hci_init_stage {
2547d0b13706SLuiz Augusto von Dentz 	int (*func)(struct hci_dev *hdev);
2548d0b13706SLuiz Augusto von Dentz };
2549d0b13706SLuiz Augusto von Dentz 
2550d0b13706SLuiz Augusto von Dentz /* Run init stage NULL terminated function table */
2551d0b13706SLuiz Augusto von Dentz static int hci_init_stage_sync(struct hci_dev *hdev,
2552d0b13706SLuiz Augusto von Dentz 			       const struct hci_init_stage *stage)
2553d0b13706SLuiz Augusto von Dentz {
2554d0b13706SLuiz Augusto von Dentz 	size_t i;
2555d0b13706SLuiz Augusto von Dentz 
2556d0b13706SLuiz Augusto von Dentz 	for (i = 0; stage[i].func; i++) {
2557d0b13706SLuiz Augusto von Dentz 		int err;
2558d0b13706SLuiz Augusto von Dentz 
2559d0b13706SLuiz Augusto von Dentz 		err = stage[i].func(hdev);
2560d0b13706SLuiz Augusto von Dentz 		if (err)
2561d0b13706SLuiz Augusto von Dentz 			return err;
2562d0b13706SLuiz Augusto von Dentz 	}
2563d0b13706SLuiz Augusto von Dentz 
2564d0b13706SLuiz Augusto von Dentz 	return 0;
2565d0b13706SLuiz Augusto von Dentz }
2566d0b13706SLuiz Augusto von Dentz 
2567d0b13706SLuiz Augusto von Dentz /* Read Local Version */
2568d0b13706SLuiz Augusto von Dentz static int hci_read_local_version_sync(struct hci_dev *hdev)
2569d0b13706SLuiz Augusto von Dentz {
2570d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
2571d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2572d0b13706SLuiz Augusto von Dentz }
2573d0b13706SLuiz Augusto von Dentz 
2574d0b13706SLuiz Augusto von Dentz /* Read BD Address */
2575d0b13706SLuiz Augusto von Dentz static int hci_read_bd_addr_sync(struct hci_dev *hdev)
2576d0b13706SLuiz Augusto von Dentz {
2577d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
2578d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2579d0b13706SLuiz Augusto von Dentz }
2580d0b13706SLuiz Augusto von Dentz 
2581d0b13706SLuiz Augusto von Dentz #define HCI_INIT(_func) \
2582d0b13706SLuiz Augusto von Dentz { \
2583d0b13706SLuiz Augusto von Dentz 	.func = _func, \
2584d0b13706SLuiz Augusto von Dentz }
2585d0b13706SLuiz Augusto von Dentz 
2586d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init0[] = {
2587d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_VERSION */
2588d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_version_sync),
2589d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_BD_ADDR */
2590d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_bd_addr_sync),
2591d0b13706SLuiz Augusto von Dentz 	{}
2592d0b13706SLuiz Augusto von Dentz };
2593d0b13706SLuiz Augusto von Dentz 
2594d0b13706SLuiz Augusto von Dentz int hci_reset_sync(struct hci_dev *hdev)
2595d0b13706SLuiz Augusto von Dentz {
2596d0b13706SLuiz Augusto von Dentz 	int err;
2597d0b13706SLuiz Augusto von Dentz 
2598d0b13706SLuiz Augusto von Dentz 	set_bit(HCI_RESET, &hdev->flags);
2599d0b13706SLuiz Augusto von Dentz 
2600d0b13706SLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
2601d0b13706SLuiz Augusto von Dentz 				    HCI_CMD_TIMEOUT);
2602d0b13706SLuiz Augusto von Dentz 	if (err)
2603d0b13706SLuiz Augusto von Dentz 		return err;
2604d0b13706SLuiz Augusto von Dentz 
2605d0b13706SLuiz Augusto von Dentz 	return 0;
2606d0b13706SLuiz Augusto von Dentz }
2607d0b13706SLuiz Augusto von Dentz 
2608d0b13706SLuiz Augusto von Dentz static int hci_init0_sync(struct hci_dev *hdev)
2609d0b13706SLuiz Augusto von Dentz {
2610d0b13706SLuiz Augusto von Dentz 	int err;
2611d0b13706SLuiz Augusto von Dentz 
2612d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
2613d0b13706SLuiz Augusto von Dentz 
2614d0b13706SLuiz Augusto von Dentz 	/* Reset */
2615d0b13706SLuiz Augusto von Dentz 	if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
2616d0b13706SLuiz Augusto von Dentz 		err = hci_reset_sync(hdev);
2617d0b13706SLuiz Augusto von Dentz 		if (err)
2618d0b13706SLuiz Augusto von Dentz 			return err;
2619d0b13706SLuiz Augusto von Dentz 	}
2620d0b13706SLuiz Augusto von Dentz 
2621d0b13706SLuiz Augusto von Dentz 	return hci_init_stage_sync(hdev, hci_init0);
2622d0b13706SLuiz Augusto von Dentz }
2623d0b13706SLuiz Augusto von Dentz 
2624d0b13706SLuiz Augusto von Dentz static int hci_unconf_init_sync(struct hci_dev *hdev)
2625d0b13706SLuiz Augusto von Dentz {
2626d0b13706SLuiz Augusto von Dentz 	int err;
2627d0b13706SLuiz Augusto von Dentz 
2628d0b13706SLuiz Augusto von Dentz 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
2629d0b13706SLuiz Augusto von Dentz 		return 0;
2630d0b13706SLuiz Augusto von Dentz 
2631d0b13706SLuiz Augusto von Dentz 	err = hci_init0_sync(hdev);
2632d0b13706SLuiz Augusto von Dentz 	if (err < 0)
2633d0b13706SLuiz Augusto von Dentz 		return err;
2634d0b13706SLuiz Augusto von Dentz 
2635d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_SETUP))
2636d0b13706SLuiz Augusto von Dentz 		hci_debugfs_create_basic(hdev);
2637d0b13706SLuiz Augusto von Dentz 
2638d0b13706SLuiz Augusto von Dentz 	return 0;
2639d0b13706SLuiz Augusto von Dentz }
2640d0b13706SLuiz Augusto von Dentz 
2641d0b13706SLuiz Augusto von Dentz /* Read Local Supported Features. */
2642d0b13706SLuiz Augusto von Dentz static int hci_read_local_features_sync(struct hci_dev *hdev)
2643d0b13706SLuiz Augusto von Dentz {
2644d0b13706SLuiz Augusto von Dentz 	 /* Not all AMP controllers support this command */
2645d0b13706SLuiz Augusto von Dentz 	if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
2646d0b13706SLuiz Augusto von Dentz 		return 0;
2647d0b13706SLuiz Augusto von Dentz 
2648d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
2649d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2650d0b13706SLuiz Augusto von Dentz }
2651d0b13706SLuiz Augusto von Dentz 
2652d0b13706SLuiz Augusto von Dentz /* BR Controller init stage 1 command sequence */
2653d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage br_init1[] = {
2654d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_FEATURES */
2655d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_features_sync),
2656d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_VERSION */
2657d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_version_sync),
2658d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_BD_ADDR */
2659d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_bd_addr_sync),
2660d0b13706SLuiz Augusto von Dentz 	{}
2661d0b13706SLuiz Augusto von Dentz };
2662d0b13706SLuiz Augusto von Dentz 
2663d0b13706SLuiz Augusto von Dentz /* Read Local Commands */
2664d0b13706SLuiz Augusto von Dentz static int hci_read_local_cmds_sync(struct hci_dev *hdev)
2665d0b13706SLuiz Augusto von Dentz {
2666d0b13706SLuiz Augusto von Dentz 	/* All Bluetooth 1.2 and later controllers should support the
2667d0b13706SLuiz Augusto von Dentz 	 * HCI command for reading the local supported commands.
2668d0b13706SLuiz Augusto von Dentz 	 *
2669d0b13706SLuiz Augusto von Dentz 	 * Unfortunately some controllers indicate Bluetooth 1.2 support,
2670d0b13706SLuiz Augusto von Dentz 	 * but do not have support for this command. If that is the case,
2671d0b13706SLuiz Augusto von Dentz 	 * the driver can quirk the behavior and skip reading the local
2672d0b13706SLuiz Augusto von Dentz 	 * supported commands.
2673d0b13706SLuiz Augusto von Dentz 	 */
2674d0b13706SLuiz Augusto von Dentz 	if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
2675d0b13706SLuiz Augusto von Dentz 	    !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
2676d0b13706SLuiz Augusto von Dentz 		return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
2677d0b13706SLuiz Augusto von Dentz 					     0, NULL, HCI_CMD_TIMEOUT);
2678d0b13706SLuiz Augusto von Dentz 
2679d0b13706SLuiz Augusto von Dentz 	return 0;
2680d0b13706SLuiz Augusto von Dentz }
2681d0b13706SLuiz Augusto von Dentz 
2682d0b13706SLuiz Augusto von Dentz /* Read Local AMP Info */
2683d0b13706SLuiz Augusto von Dentz static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
2684d0b13706SLuiz Augusto von Dentz {
2685d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
2686d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2687d0b13706SLuiz Augusto von Dentz }
2688d0b13706SLuiz Augusto von Dentz 
2689d0b13706SLuiz Augusto von Dentz /* Read Data Blk size */
2690d0b13706SLuiz Augusto von Dentz static int hci_read_data_block_size_sync(struct hci_dev *hdev)
2691d0b13706SLuiz Augusto von Dentz {
2692d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
2693d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2694d0b13706SLuiz Augusto von Dentz }
2695d0b13706SLuiz Augusto von Dentz 
2696d0b13706SLuiz Augusto von Dentz /* Read Flow Control Mode */
2697d0b13706SLuiz Augusto von Dentz static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
2698d0b13706SLuiz Augusto von Dentz {
2699d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
2700d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2701d0b13706SLuiz Augusto von Dentz }
2702d0b13706SLuiz Augusto von Dentz 
2703d0b13706SLuiz Augusto von Dentz /* Read Location Data */
2704d0b13706SLuiz Augusto von Dentz static int hci_read_location_data_sync(struct hci_dev *hdev)
2705d0b13706SLuiz Augusto von Dentz {
2706d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
2707d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2708d0b13706SLuiz Augusto von Dentz }
2709d0b13706SLuiz Augusto von Dentz 
2710d0b13706SLuiz Augusto von Dentz /* AMP Controller init stage 1 command sequence */
2711d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage amp_init1[] = {
2712d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_VERSION */
2713d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_version_sync),
2714d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_COMMANDS */
2715d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_cmds_sync),
2716d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_AMP_INFO */
2717d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_amp_info_sync),
2718d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_DATA_BLOCK_SIZE */
2719d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_data_block_size_sync),
2720d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_FLOW_CONTROL_MODE */
2721d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_flow_control_mode_sync),
2722d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCATION_DATA */
2723d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_location_data_sync),
2724d0b13706SLuiz Augusto von Dentz };
2725d0b13706SLuiz Augusto von Dentz 
2726d0b13706SLuiz Augusto von Dentz static int hci_init1_sync(struct hci_dev *hdev)
2727d0b13706SLuiz Augusto von Dentz {
2728d0b13706SLuiz Augusto von Dentz 	int err;
2729d0b13706SLuiz Augusto von Dentz 
2730d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
2731d0b13706SLuiz Augusto von Dentz 
2732d0b13706SLuiz Augusto von Dentz 	/* Reset */
2733d0b13706SLuiz Augusto von Dentz 	if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
2734d0b13706SLuiz Augusto von Dentz 		err = hci_reset_sync(hdev);
2735d0b13706SLuiz Augusto von Dentz 		if (err)
2736d0b13706SLuiz Augusto von Dentz 			return err;
2737d0b13706SLuiz Augusto von Dentz 	}
2738d0b13706SLuiz Augusto von Dentz 
2739d0b13706SLuiz Augusto von Dentz 	switch (hdev->dev_type) {
2740d0b13706SLuiz Augusto von Dentz 	case HCI_PRIMARY:
2741d0b13706SLuiz Augusto von Dentz 		hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
2742d0b13706SLuiz Augusto von Dentz 		return hci_init_stage_sync(hdev, br_init1);
2743d0b13706SLuiz Augusto von Dentz 	case HCI_AMP:
2744d0b13706SLuiz Augusto von Dentz 		hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
2745d0b13706SLuiz Augusto von Dentz 		return hci_init_stage_sync(hdev, amp_init1);
2746d0b13706SLuiz Augusto von Dentz 	default:
2747d0b13706SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
2748d0b13706SLuiz Augusto von Dentz 		break;
2749d0b13706SLuiz Augusto von Dentz 	}
2750d0b13706SLuiz Augusto von Dentz 
2751d0b13706SLuiz Augusto von Dentz 	return 0;
2752d0b13706SLuiz Augusto von Dentz }
2753d0b13706SLuiz Augusto von Dentz 
2754d0b13706SLuiz Augusto von Dentz /* AMP Controller init stage 2 command sequence */
2755d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage amp_init2[] = {
2756d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_FEATURES */
2757d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_features_sync),
2758d0b13706SLuiz Augusto von Dentz };
2759d0b13706SLuiz Augusto von Dentz 
2760d0b13706SLuiz Augusto von Dentz /* Read Buffer Size (ACL mtu, max pkt, etc.) */
2761d0b13706SLuiz Augusto von Dentz static int hci_read_buffer_size_sync(struct hci_dev *hdev)
2762d0b13706SLuiz Augusto von Dentz {
2763d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
2764d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2765d0b13706SLuiz Augusto von Dentz }
2766d0b13706SLuiz Augusto von Dentz 
2767d0b13706SLuiz Augusto von Dentz /* Read Class of Device */
2768d0b13706SLuiz Augusto von Dentz static int hci_read_dev_class_sync(struct hci_dev *hdev)
2769d0b13706SLuiz Augusto von Dentz {
2770d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
2771d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2772d0b13706SLuiz Augusto von Dentz }
2773d0b13706SLuiz Augusto von Dentz 
2774d0b13706SLuiz Augusto von Dentz /* Read Local Name */
2775d0b13706SLuiz Augusto von Dentz static int hci_read_local_name_sync(struct hci_dev *hdev)
2776d0b13706SLuiz Augusto von Dentz {
2777d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
2778d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2779d0b13706SLuiz Augusto von Dentz }
2780d0b13706SLuiz Augusto von Dentz 
2781d0b13706SLuiz Augusto von Dentz /* Read Voice Setting */
2782d0b13706SLuiz Augusto von Dentz static int hci_read_voice_setting_sync(struct hci_dev *hdev)
2783d0b13706SLuiz Augusto von Dentz {
2784d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
2785d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2786d0b13706SLuiz Augusto von Dentz }
2787d0b13706SLuiz Augusto von Dentz 
2788d0b13706SLuiz Augusto von Dentz /* Read Number of Supported IAC */
2789d0b13706SLuiz Augusto von Dentz static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
2790d0b13706SLuiz Augusto von Dentz {
2791d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
2792d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2793d0b13706SLuiz Augusto von Dentz }
2794d0b13706SLuiz Augusto von Dentz 
2795d0b13706SLuiz Augusto von Dentz /* Read Current IAC LAP */
2796d0b13706SLuiz Augusto von Dentz static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
2797d0b13706SLuiz Augusto von Dentz {
2798d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
2799d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2800d0b13706SLuiz Augusto von Dentz }
2801d0b13706SLuiz Augusto von Dentz 
2802d0b13706SLuiz Augusto von Dentz static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
2803d0b13706SLuiz Augusto von Dentz 				     u8 cond_type, bdaddr_t *bdaddr,
2804d0b13706SLuiz Augusto von Dentz 				     u8 auto_accept)
2805d0b13706SLuiz Augusto von Dentz {
2806d0b13706SLuiz Augusto von Dentz 	struct hci_cp_set_event_filter cp;
2807d0b13706SLuiz Augusto von Dentz 
2808d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2809d0b13706SLuiz Augusto von Dentz 		return 0;
2810d0b13706SLuiz Augusto von Dentz 
28110eaecfb2SIsmael Ferreras Morezuelas 	if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
28120eaecfb2SIsmael Ferreras Morezuelas 		return 0;
28130eaecfb2SIsmael Ferreras Morezuelas 
2814d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
2815d0b13706SLuiz Augusto von Dentz 	cp.flt_type = flt_type;
2816d0b13706SLuiz Augusto von Dentz 
2817d0b13706SLuiz Augusto von Dentz 	if (flt_type != HCI_FLT_CLEAR_ALL) {
2818d0b13706SLuiz Augusto von Dentz 		cp.cond_type = cond_type;
2819d0b13706SLuiz Augusto von Dentz 		bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
2820d0b13706SLuiz Augusto von Dentz 		cp.addr_conn_flt.auto_accept = auto_accept;
2821d0b13706SLuiz Augusto von Dentz 	}
2822d0b13706SLuiz Augusto von Dentz 
2823d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
2824d0b13706SLuiz Augusto von Dentz 				     flt_type == HCI_FLT_CLEAR_ALL ?
2825d0b13706SLuiz Augusto von Dentz 				     sizeof(cp.flt_type) : sizeof(cp), &cp,
2826d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
2827d0b13706SLuiz Augusto von Dentz }
2828d0b13706SLuiz Augusto von Dentz 
2829d0b13706SLuiz Augusto von Dentz static int hci_clear_event_filter_sync(struct hci_dev *hdev)
2830d0b13706SLuiz Augusto von Dentz {
2831d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
2832d0b13706SLuiz Augusto von Dentz 		return 0;
2833d0b13706SLuiz Augusto von Dentz 
28340eaecfb2SIsmael Ferreras Morezuelas 	/* In theory the state machine should not reach here unless
28350eaecfb2SIsmael Ferreras Morezuelas 	 * a hci_set_event_filter_sync() call succeeds, but we do
28360eaecfb2SIsmael Ferreras Morezuelas 	 * the check both for parity and as a future reminder.
28370eaecfb2SIsmael Ferreras Morezuelas 	 */
28380eaecfb2SIsmael Ferreras Morezuelas 	if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
28390eaecfb2SIsmael Ferreras Morezuelas 		return 0;
28400eaecfb2SIsmael Ferreras Morezuelas 
2841d0b13706SLuiz Augusto von Dentz 	return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
2842d0b13706SLuiz Augusto von Dentz 					 BDADDR_ANY, 0x00);
2843d0b13706SLuiz Augusto von Dentz }
2844d0b13706SLuiz Augusto von Dentz 
2845d0b13706SLuiz Augusto von Dentz /* Connection accept timeout ~20 secs */
2846d0b13706SLuiz Augusto von Dentz static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
2847d0b13706SLuiz Augusto von Dentz {
2848d0b13706SLuiz Augusto von Dentz 	__le16 param = cpu_to_le16(0x7d00);
2849d0b13706SLuiz Augusto von Dentz 
2850d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
2851d0b13706SLuiz Augusto von Dentz 				     sizeof(param), &param, HCI_CMD_TIMEOUT);
2852d0b13706SLuiz Augusto von Dentz }
2853d0b13706SLuiz Augusto von Dentz 
2854d0b13706SLuiz Augusto von Dentz /* BR Controller init stage 2 command sequence */
2855d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage br_init2[] = {
2856d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_BUFFER_SIZE */
2857d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_buffer_size_sync),
2858d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_CLASS_OF_DEV */
2859d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_dev_class_sync),
2860d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_NAME */
2861d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_name_sync),
2862d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_VOICE_SETTING */
2863d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_voice_setting_sync),
2864d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_NUM_SUPPORTED_IAC */
2865d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_num_supported_iac_sync),
2866d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_CURRENT_IAC_LAP */
2867d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_current_iac_lap_sync),
2868d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_SET_EVENT_FLT */
2869d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_clear_event_filter_sync),
2870d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_CA_TIMEOUT */
2871d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_ca_timeout_sync),
2872d0b13706SLuiz Augusto von Dentz 	{}
2873d0b13706SLuiz Augusto von Dentz };
2874d0b13706SLuiz Augusto von Dentz 
2875d0b13706SLuiz Augusto von Dentz static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
2876d0b13706SLuiz Augusto von Dentz {
2877d0b13706SLuiz Augusto von Dentz 	u8 mode = 0x01;
2878d0b13706SLuiz Augusto von Dentz 
2879d0b13706SLuiz Augusto von Dentz 	if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2880d0b13706SLuiz Augusto von Dentz 		return 0;
2881d0b13706SLuiz Augusto von Dentz 
2882d0b13706SLuiz Augusto von Dentz 	/* When SSP is available, then the host features page
2883d0b13706SLuiz Augusto von Dentz 	 * should also be available as well. However some
2884d0b13706SLuiz Augusto von Dentz 	 * controllers list the max_page as 0 as long as SSP
2885d0b13706SLuiz Augusto von Dentz 	 * has not been enabled. To achieve proper debugging
2886d0b13706SLuiz Augusto von Dentz 	 * output, force the minimum max_page to 1 at least.
2887d0b13706SLuiz Augusto von Dentz 	 */
2888d0b13706SLuiz Augusto von Dentz 	hdev->max_page = 0x01;
2889d0b13706SLuiz Augusto von Dentz 
2890d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2891d0b13706SLuiz Augusto von Dentz 				     sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2892d0b13706SLuiz Augusto von Dentz }
2893d0b13706SLuiz Augusto von Dentz 
2894d0b13706SLuiz Augusto von Dentz static int hci_write_eir_sync(struct hci_dev *hdev)
2895d0b13706SLuiz Augusto von Dentz {
2896d0b13706SLuiz Augusto von Dentz 	struct hci_cp_write_eir cp;
2897d0b13706SLuiz Augusto von Dentz 
2898d0b13706SLuiz Augusto von Dentz 	if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2899d0b13706SLuiz Augusto von Dentz 		return 0;
2900d0b13706SLuiz Augusto von Dentz 
2901d0b13706SLuiz Augusto von Dentz 	memset(hdev->eir, 0, sizeof(hdev->eir));
2902d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
2903d0b13706SLuiz Augusto von Dentz 
2904d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
2905d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
2906d0b13706SLuiz Augusto von Dentz }
2907d0b13706SLuiz Augusto von Dentz 
2908d0b13706SLuiz Augusto von Dentz static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
2909d0b13706SLuiz Augusto von Dentz {
2910d0b13706SLuiz Augusto von Dentz 	u8 mode;
2911d0b13706SLuiz Augusto von Dentz 
2912d0b13706SLuiz Augusto von Dentz 	if (!lmp_inq_rssi_capable(hdev) &&
2913d0b13706SLuiz Augusto von Dentz 	    !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
2914d0b13706SLuiz Augusto von Dentz 		return 0;
2915d0b13706SLuiz Augusto von Dentz 
2916d0b13706SLuiz Augusto von Dentz 	/* If Extended Inquiry Result events are supported, then
2917d0b13706SLuiz Augusto von Dentz 	 * they are clearly preferred over Inquiry Result with RSSI
2918d0b13706SLuiz Augusto von Dentz 	 * events.
2919d0b13706SLuiz Augusto von Dentz 	 */
2920d0b13706SLuiz Augusto von Dentz 	mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
2921d0b13706SLuiz Augusto von Dentz 
2922d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
2923d0b13706SLuiz Augusto von Dentz 				     sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2924d0b13706SLuiz Augusto von Dentz }
2925d0b13706SLuiz Augusto von Dentz 
2926d0b13706SLuiz Augusto von Dentz static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
2927d0b13706SLuiz Augusto von Dentz {
2928d0b13706SLuiz Augusto von Dentz 	if (!lmp_inq_tx_pwr_capable(hdev))
2929d0b13706SLuiz Augusto von Dentz 		return 0;
2930d0b13706SLuiz Augusto von Dentz 
2931d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
2932d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2933d0b13706SLuiz Augusto von Dentz }
2934d0b13706SLuiz Augusto von Dentz 
2935d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
2936d0b13706SLuiz Augusto von Dentz {
2937d0b13706SLuiz Augusto von Dentz 	struct hci_cp_read_local_ext_features cp;
2938d0b13706SLuiz Augusto von Dentz 
2939d0b13706SLuiz Augusto von Dentz 	if (!lmp_ext_feat_capable(hdev))
2940d0b13706SLuiz Augusto von Dentz 		return 0;
2941d0b13706SLuiz Augusto von Dentz 
2942d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
2943d0b13706SLuiz Augusto von Dentz 	cp.page = page;
2944d0b13706SLuiz Augusto von Dentz 
2945d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
2946d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2947d0b13706SLuiz Augusto von Dentz }
2948d0b13706SLuiz Augusto von Dentz 
2949d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
2950d0b13706SLuiz Augusto von Dentz {
2951d0b13706SLuiz Augusto von Dentz 	return hci_read_local_ext_features_sync(hdev, 0x01);
2952d0b13706SLuiz Augusto von Dentz }
2953d0b13706SLuiz Augusto von Dentz 
2954d0b13706SLuiz Augusto von Dentz /* HCI Controller init stage 2 command sequence */
2955d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init2[] = {
2956d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_COMMANDS */
2957d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_cmds_sync),
2958d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_SSP_MODE */
2959d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_ssp_mode_1_sync),
2960d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_EIR */
2961d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_eir_sync),
2962d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_INQUIRY_MODE */
2963d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_inquiry_mode_sync),
2964d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_INQ_RSP_TX_POWER */
2965d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_inq_rsp_tx_power_sync),
2966d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_EXT_FEATURES */
2967d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_ext_features_1_sync),
2968d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_AUTH_ENABLE */
2969d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_auth_enable_sync),
2970d0b13706SLuiz Augusto von Dentz 	{}
2971d0b13706SLuiz Augusto von Dentz };
2972d0b13706SLuiz Augusto von Dentz 
2973d0b13706SLuiz Augusto von Dentz /* Read LE Buffer Size */
2974d0b13706SLuiz Augusto von Dentz static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
2975d0b13706SLuiz Augusto von Dentz {
2976d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
2977d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2978d0b13706SLuiz Augusto von Dentz }
2979d0b13706SLuiz Augusto von Dentz 
2980d0b13706SLuiz Augusto von Dentz /* Read LE Local Supported Features */
2981d0b13706SLuiz Augusto von Dentz static int hci_le_read_local_features_sync(struct hci_dev *hdev)
2982d0b13706SLuiz Augusto von Dentz {
2983d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
2984d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2985d0b13706SLuiz Augusto von Dentz }
2986d0b13706SLuiz Augusto von Dentz 
2987d0b13706SLuiz Augusto von Dentz /* Read LE Supported States */
2988d0b13706SLuiz Augusto von Dentz static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
2989d0b13706SLuiz Augusto von Dentz {
2990d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
2991d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
2992d0b13706SLuiz Augusto von Dentz }
2993d0b13706SLuiz Augusto von Dentz 
2994d0b13706SLuiz Augusto von Dentz /* LE Controller init stage 2 command sequence */
2995d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init2[] = {
2996d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_BUFFER_SIZE */
2997d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_buffer_size_sync),
2998d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_LOCAL_FEATURES */
2999d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_local_features_sync),
3000d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_SUPPORTED_STATES */
3001d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_supported_states_sync),
3002d0b13706SLuiz Augusto von Dentz 	{}
3003d0b13706SLuiz Augusto von Dentz };
3004d0b13706SLuiz Augusto von Dentz 
3005d0b13706SLuiz Augusto von Dentz static int hci_init2_sync(struct hci_dev *hdev)
3006d0b13706SLuiz Augusto von Dentz {
3007d0b13706SLuiz Augusto von Dentz 	int err;
3008d0b13706SLuiz Augusto von Dentz 
3009d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
3010d0b13706SLuiz Augusto von Dentz 
3011d0b13706SLuiz Augusto von Dentz 	if (hdev->dev_type == HCI_AMP)
3012d0b13706SLuiz Augusto von Dentz 		return hci_init_stage_sync(hdev, amp_init2);
3013d0b13706SLuiz Augusto von Dentz 
3014d0b13706SLuiz Augusto von Dentz 	if (lmp_bredr_capable(hdev)) {
3015d0b13706SLuiz Augusto von Dentz 		err = hci_init_stage_sync(hdev, br_init2);
3016d0b13706SLuiz Augusto von Dentz 		if (err)
3017d0b13706SLuiz Augusto von Dentz 			return err;
3018d0b13706SLuiz Augusto von Dentz 	} else {
3019d0b13706SLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3020d0b13706SLuiz Augusto von Dentz 	}
3021d0b13706SLuiz Augusto von Dentz 
3022d0b13706SLuiz Augusto von Dentz 	if (lmp_le_capable(hdev)) {
3023d0b13706SLuiz Augusto von Dentz 		err = hci_init_stage_sync(hdev, le_init2);
3024d0b13706SLuiz Augusto von Dentz 		if (err)
3025d0b13706SLuiz Augusto von Dentz 			return err;
3026d0b13706SLuiz Augusto von Dentz 		/* LE-only controllers have LE implicitly enabled */
3027d0b13706SLuiz Augusto von Dentz 		if (!lmp_bredr_capable(hdev))
3028d0b13706SLuiz Augusto von Dentz 			hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3029d0b13706SLuiz Augusto von Dentz 	}
3030d0b13706SLuiz Augusto von Dentz 
3031d0b13706SLuiz Augusto von Dentz 	return hci_init_stage_sync(hdev, hci_init2);
3032d0b13706SLuiz Augusto von Dentz }
3033d0b13706SLuiz Augusto von Dentz 
3034d0b13706SLuiz Augusto von Dentz static int hci_set_event_mask_sync(struct hci_dev *hdev)
3035d0b13706SLuiz Augusto von Dentz {
3036d0b13706SLuiz Augusto von Dentz 	/* The second byte is 0xff instead of 0x9f (two reserved bits
3037d0b13706SLuiz Augusto von Dentz 	 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3038d0b13706SLuiz Augusto von Dentz 	 * command otherwise.
3039d0b13706SLuiz Augusto von Dentz 	 */
3040d0b13706SLuiz Augusto von Dentz 	u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3041d0b13706SLuiz Augusto von Dentz 
3042d0b13706SLuiz Augusto von Dentz 	/* CSR 1.1 dongles does not accept any bitfield so don't try to set
3043d0b13706SLuiz Augusto von Dentz 	 * any event mask for pre 1.2 devices.
3044d0b13706SLuiz Augusto von Dentz 	 */
3045d0b13706SLuiz Augusto von Dentz 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3046d0b13706SLuiz Augusto von Dentz 		return 0;
3047d0b13706SLuiz Augusto von Dentz 
3048d0b13706SLuiz Augusto von Dentz 	if (lmp_bredr_capable(hdev)) {
3049d0b13706SLuiz Augusto von Dentz 		events[4] |= 0x01; /* Flow Specification Complete */
3050182ee45dSLuiz Augusto von Dentz 
3051182ee45dSLuiz Augusto von Dentz 		/* Don't set Disconnect Complete when suspended as that
3052182ee45dSLuiz Augusto von Dentz 		 * would wakeup the host when disconnecting due to
3053182ee45dSLuiz Augusto von Dentz 		 * suspend.
3054182ee45dSLuiz Augusto von Dentz 		 */
3055182ee45dSLuiz Augusto von Dentz 		if (hdev->suspended)
3056182ee45dSLuiz Augusto von Dentz 			events[0] &= 0xef;
3057d0b13706SLuiz Augusto von Dentz 	} else {
3058d0b13706SLuiz Augusto von Dentz 		/* Use a different default for LE-only devices */
3059d0b13706SLuiz Augusto von Dentz 		memset(events, 0, sizeof(events));
3060d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x20; /* Command Complete */
3061d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x40; /* Command Status */
3062d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x80; /* Hardware Error */
3063d0b13706SLuiz Augusto von Dentz 
3064d0b13706SLuiz Augusto von Dentz 		/* If the controller supports the Disconnect command, enable
3065d0b13706SLuiz Augusto von Dentz 		 * the corresponding event. In addition enable packet flow
3066d0b13706SLuiz Augusto von Dentz 		 * control related events.
3067d0b13706SLuiz Augusto von Dentz 		 */
3068d0b13706SLuiz Augusto von Dentz 		if (hdev->commands[0] & 0x20) {
3069182ee45dSLuiz Augusto von Dentz 			/* Don't set Disconnect Complete when suspended as that
3070182ee45dSLuiz Augusto von Dentz 			 * would wakeup the host when disconnecting due to
3071182ee45dSLuiz Augusto von Dentz 			 * suspend.
3072182ee45dSLuiz Augusto von Dentz 			 */
3073182ee45dSLuiz Augusto von Dentz 			if (!hdev->suspended)
3074d0b13706SLuiz Augusto von Dentz 				events[0] |= 0x10; /* Disconnection Complete */
3075d0b13706SLuiz Augusto von Dentz 			events[2] |= 0x04; /* Number of Completed Packets */
3076d0b13706SLuiz Augusto von Dentz 			events[3] |= 0x02; /* Data Buffer Overflow */
3077d0b13706SLuiz Augusto von Dentz 		}
3078d0b13706SLuiz Augusto von Dentz 
3079d0b13706SLuiz Augusto von Dentz 		/* If the controller supports the Read Remote Version
3080d0b13706SLuiz Augusto von Dentz 		 * Information command, enable the corresponding event.
3081d0b13706SLuiz Augusto von Dentz 		 */
3082d0b13706SLuiz Augusto von Dentz 		if (hdev->commands[2] & 0x80)
3083d0b13706SLuiz Augusto von Dentz 			events[1] |= 0x08; /* Read Remote Version Information
3084d0b13706SLuiz Augusto von Dentz 					    * Complete
3085d0b13706SLuiz Augusto von Dentz 					    */
3086d0b13706SLuiz Augusto von Dentz 
3087d0b13706SLuiz Augusto von Dentz 		if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3088d0b13706SLuiz Augusto von Dentz 			events[0] |= 0x80; /* Encryption Change */
3089d0b13706SLuiz Augusto von Dentz 			events[5] |= 0x80; /* Encryption Key Refresh Complete */
3090d0b13706SLuiz Augusto von Dentz 		}
3091d0b13706SLuiz Augusto von Dentz 	}
3092d0b13706SLuiz Augusto von Dentz 
3093d0b13706SLuiz Augusto von Dentz 	if (lmp_inq_rssi_capable(hdev) ||
3094d0b13706SLuiz Augusto von Dentz 	    test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3095d0b13706SLuiz Augusto von Dentz 		events[4] |= 0x02; /* Inquiry Result with RSSI */
3096d0b13706SLuiz Augusto von Dentz 
3097d0b13706SLuiz Augusto von Dentz 	if (lmp_ext_feat_capable(hdev))
3098d0b13706SLuiz Augusto von Dentz 		events[4] |= 0x04; /* Read Remote Extended Features Complete */
3099d0b13706SLuiz Augusto von Dentz 
3100d0b13706SLuiz Augusto von Dentz 	if (lmp_esco_capable(hdev)) {
3101d0b13706SLuiz Augusto von Dentz 		events[5] |= 0x08; /* Synchronous Connection Complete */
3102d0b13706SLuiz Augusto von Dentz 		events[5] |= 0x10; /* Synchronous Connection Changed */
3103d0b13706SLuiz Augusto von Dentz 	}
3104d0b13706SLuiz Augusto von Dentz 
3105d0b13706SLuiz Augusto von Dentz 	if (lmp_sniffsubr_capable(hdev))
3106d0b13706SLuiz Augusto von Dentz 		events[5] |= 0x20; /* Sniff Subrating */
3107d0b13706SLuiz Augusto von Dentz 
3108d0b13706SLuiz Augusto von Dentz 	if (lmp_pause_enc_capable(hdev))
3109d0b13706SLuiz Augusto von Dentz 		events[5] |= 0x80; /* Encryption Key Refresh Complete */
3110d0b13706SLuiz Augusto von Dentz 
3111d0b13706SLuiz Augusto von Dentz 	if (lmp_ext_inq_capable(hdev))
3112d0b13706SLuiz Augusto von Dentz 		events[5] |= 0x40; /* Extended Inquiry Result */
3113d0b13706SLuiz Augusto von Dentz 
3114d0b13706SLuiz Augusto von Dentz 	if (lmp_no_flush_capable(hdev))
3115d0b13706SLuiz Augusto von Dentz 		events[7] |= 0x01; /* Enhanced Flush Complete */
3116d0b13706SLuiz Augusto von Dentz 
3117d0b13706SLuiz Augusto von Dentz 	if (lmp_lsto_capable(hdev))
3118d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x80; /* Link Supervision Timeout Changed */
3119d0b13706SLuiz Augusto von Dentz 
3120d0b13706SLuiz Augusto von Dentz 	if (lmp_ssp_capable(hdev)) {
3121d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x01;	/* IO Capability Request */
3122d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x02;	/* IO Capability Response */
3123d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x04;	/* User Confirmation Request */
3124d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x08;	/* User Passkey Request */
3125d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x10;	/* Remote OOB Data Request */
3126d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x20;	/* Simple Pairing Complete */
3127d0b13706SLuiz Augusto von Dentz 		events[7] |= 0x04;	/* User Passkey Notification */
3128d0b13706SLuiz Augusto von Dentz 		events[7] |= 0x08;	/* Keypress Notification */
3129d0b13706SLuiz Augusto von Dentz 		events[7] |= 0x10;	/* Remote Host Supported
3130d0b13706SLuiz Augusto von Dentz 					 * Features Notification
3131d0b13706SLuiz Augusto von Dentz 					 */
3132d0b13706SLuiz Augusto von Dentz 	}
3133d0b13706SLuiz Augusto von Dentz 
3134d0b13706SLuiz Augusto von Dentz 	if (lmp_le_capable(hdev))
3135d0b13706SLuiz Augusto von Dentz 		events[7] |= 0x20;	/* LE Meta-Event */
3136d0b13706SLuiz Augusto von Dentz 
3137d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3138d0b13706SLuiz Augusto von Dentz 				     sizeof(events), events, HCI_CMD_TIMEOUT);
3139d0b13706SLuiz Augusto von Dentz }
3140d0b13706SLuiz Augusto von Dentz 
3141d0b13706SLuiz Augusto von Dentz static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3142d0b13706SLuiz Augusto von Dentz {
3143d0b13706SLuiz Augusto von Dentz 	struct hci_cp_read_stored_link_key cp;
3144d0b13706SLuiz Augusto von Dentz 
3145d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[6] & 0x20) ||
3146d0b13706SLuiz Augusto von Dentz 	    test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3147d0b13706SLuiz Augusto von Dentz 		return 0;
3148d0b13706SLuiz Augusto von Dentz 
3149d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3150d0b13706SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, BDADDR_ANY);
3151d0b13706SLuiz Augusto von Dentz 	cp.read_all = 0x01;
3152d0b13706SLuiz Augusto von Dentz 
3153d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3154d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3155d0b13706SLuiz Augusto von Dentz }
3156d0b13706SLuiz Augusto von Dentz 
3157d0b13706SLuiz Augusto von Dentz static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3158d0b13706SLuiz Augusto von Dentz {
3159d0b13706SLuiz Augusto von Dentz 	struct hci_cp_write_def_link_policy cp;
3160d0b13706SLuiz Augusto von Dentz 	u16 link_policy = 0;
3161d0b13706SLuiz Augusto von Dentz 
3162d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[5] & 0x10))
3163d0b13706SLuiz Augusto von Dentz 		return 0;
3164d0b13706SLuiz Augusto von Dentz 
3165d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3166d0b13706SLuiz Augusto von Dentz 
3167d0b13706SLuiz Augusto von Dentz 	if (lmp_rswitch_capable(hdev))
3168d0b13706SLuiz Augusto von Dentz 		link_policy |= HCI_LP_RSWITCH;
3169d0b13706SLuiz Augusto von Dentz 	if (lmp_hold_capable(hdev))
3170d0b13706SLuiz Augusto von Dentz 		link_policy |= HCI_LP_HOLD;
3171d0b13706SLuiz Augusto von Dentz 	if (lmp_sniff_capable(hdev))
3172d0b13706SLuiz Augusto von Dentz 		link_policy |= HCI_LP_SNIFF;
3173d0b13706SLuiz Augusto von Dentz 	if (lmp_park_capable(hdev))
3174d0b13706SLuiz Augusto von Dentz 		link_policy |= HCI_LP_PARK;
3175d0b13706SLuiz Augusto von Dentz 
3176d0b13706SLuiz Augusto von Dentz 	cp.policy = cpu_to_le16(link_policy);
3177d0b13706SLuiz Augusto von Dentz 
3178d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3179d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3180d0b13706SLuiz Augusto von Dentz }
3181d0b13706SLuiz Augusto von Dentz 
3182d0b13706SLuiz Augusto von Dentz static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3183d0b13706SLuiz Augusto von Dentz {
3184d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[8] & 0x01))
3185d0b13706SLuiz Augusto von Dentz 		return 0;
3186d0b13706SLuiz Augusto von Dentz 
3187d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3188d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3189d0b13706SLuiz Augusto von Dentz }
3190d0b13706SLuiz Augusto von Dentz 
3191d0b13706SLuiz Augusto von Dentz static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3192d0b13706SLuiz Augusto von Dentz {
3193d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[18] & 0x04) ||
3194d0b13706SLuiz Augusto von Dentz 	    test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3195d0b13706SLuiz Augusto von Dentz 		return 0;
3196d0b13706SLuiz Augusto von Dentz 
3197d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3198d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3199d0b13706SLuiz Augusto von Dentz }
3200d0b13706SLuiz Augusto von Dentz 
3201d0b13706SLuiz Augusto von Dentz static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3202d0b13706SLuiz Augusto von Dentz {
3203d0b13706SLuiz Augusto von Dentz 	/* Some older Broadcom based Bluetooth 1.2 controllers do not
3204d0b13706SLuiz Augusto von Dentz 	 * support the Read Page Scan Type command. Check support for
3205d0b13706SLuiz Augusto von Dentz 	 * this command in the bit mask of supported commands.
3206d0b13706SLuiz Augusto von Dentz 	 */
3207d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[13] & 0x01))
3208d0b13706SLuiz Augusto von Dentz 		return 0;
3209d0b13706SLuiz Augusto von Dentz 
3210d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3211d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3212d0b13706SLuiz Augusto von Dentz }
3213d0b13706SLuiz Augusto von Dentz 
3214d0b13706SLuiz Augusto von Dentz /* Read features beyond page 1 if available */
3215d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3216d0b13706SLuiz Augusto von Dentz {
3217d0b13706SLuiz Augusto von Dentz 	u8 page;
3218d0b13706SLuiz Augusto von Dentz 	int err;
3219d0b13706SLuiz Augusto von Dentz 
3220d0b13706SLuiz Augusto von Dentz 	if (!lmp_ext_feat_capable(hdev))
3221d0b13706SLuiz Augusto von Dentz 		return 0;
3222d0b13706SLuiz Augusto von Dentz 
3223d0b13706SLuiz Augusto von Dentz 	for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3224d0b13706SLuiz Augusto von Dentz 	     page++) {
3225d0b13706SLuiz Augusto von Dentz 		err = hci_read_local_ext_features_sync(hdev, page);
3226d0b13706SLuiz Augusto von Dentz 		if (err)
3227d0b13706SLuiz Augusto von Dentz 			return err;
3228d0b13706SLuiz Augusto von Dentz 	}
3229d0b13706SLuiz Augusto von Dentz 
3230d0b13706SLuiz Augusto von Dentz 	return 0;
3231d0b13706SLuiz Augusto von Dentz }
3232d0b13706SLuiz Augusto von Dentz 
3233d0b13706SLuiz Augusto von Dentz /* HCI Controller init stage 3 command sequence */
3234d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init3[] = {
3235d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_SET_EVENT_MASK */
3236d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_set_event_mask_sync),
3237d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_STORED_LINK_KEY */
3238d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_stored_link_key_sync),
3239d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_DEF_LINK_POLICY */
3240d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_setup_link_policy_sync),
3241d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
3242d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_page_scan_activity_sync),
3243d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
3244d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_def_err_data_reporting_sync),
3245d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_PAGE_SCAN_TYPE */
3246d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_page_scan_type_sync),
3247d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_EXT_FEATURES */
3248d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_ext_features_all_sync),
3249d0b13706SLuiz Augusto von Dentz 	{}
3250d0b13706SLuiz Augusto von Dentz };
3251d0b13706SLuiz Augusto von Dentz 
3252d0b13706SLuiz Augusto von Dentz static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
3253d0b13706SLuiz Augusto von Dentz {
3254d0b13706SLuiz Augusto von Dentz 	u8 events[8];
3255d0b13706SLuiz Augusto von Dentz 
3256d0b13706SLuiz Augusto von Dentz 	if (!lmp_le_capable(hdev))
3257d0b13706SLuiz Augusto von Dentz 		return 0;
3258d0b13706SLuiz Augusto von Dentz 
3259d0b13706SLuiz Augusto von Dentz 	memset(events, 0, sizeof(events));
3260d0b13706SLuiz Augusto von Dentz 
3261d0b13706SLuiz Augusto von Dentz 	if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
3262d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x10;	/* LE Long Term Key Request */
3263d0b13706SLuiz Augusto von Dentz 
3264d0b13706SLuiz Augusto von Dentz 	/* If controller supports the Connection Parameters Request
3265d0b13706SLuiz Augusto von Dentz 	 * Link Layer Procedure, enable the corresponding event.
3266d0b13706SLuiz Augusto von Dentz 	 */
3267d0b13706SLuiz Augusto von Dentz 	if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
3268d0b13706SLuiz Augusto von Dentz 		/* LE Remote Connection Parameter Request */
3269d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x20;
3270d0b13706SLuiz Augusto von Dentz 
3271d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the Data Length Extension
3272d0b13706SLuiz Augusto von Dentz 	 * feature, enable the corresponding event.
3273d0b13706SLuiz Augusto von Dentz 	 */
3274d0b13706SLuiz Augusto von Dentz 	if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
3275d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x40;	/* LE Data Length Change */
3276d0b13706SLuiz Augusto von Dentz 
3277a56a1138SLuiz Augusto von Dentz 	/* If the controller supports LL Privacy feature or LE Extended Adv,
3278a56a1138SLuiz Augusto von Dentz 	 * enable the corresponding event.
3279d0b13706SLuiz Augusto von Dentz 	 */
3280a56a1138SLuiz Augusto von Dentz 	if (use_enhanced_conn_complete(hdev))
3281d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x02;	/* LE Enhanced Connection Complete */
3282d0b13706SLuiz Augusto von Dentz 
3283d0b13706SLuiz Augusto von Dentz 	/* If the controller supports Extended Scanner Filter
3284d0b13706SLuiz Augusto von Dentz 	 * Policies, enable the corresponding event.
3285d0b13706SLuiz Augusto von Dentz 	 */
3286d0b13706SLuiz Augusto von Dentz 	if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
3287d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x04;	/* LE Direct Advertising Report */
3288d0b13706SLuiz Augusto von Dentz 
3289d0b13706SLuiz Augusto von Dentz 	/* If the controller supports Channel Selection Algorithm #2
3290d0b13706SLuiz Augusto von Dentz 	 * feature, enable the corresponding event.
3291d0b13706SLuiz Augusto von Dentz 	 */
3292d0b13706SLuiz Augusto von Dentz 	if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
3293d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x08;	/* LE Channel Selection Algorithm */
3294d0b13706SLuiz Augusto von Dentz 
3295d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Set Scan Enable command,
3296d0b13706SLuiz Augusto von Dentz 	 * enable the corresponding advertising report event.
3297d0b13706SLuiz Augusto von Dentz 	 */
3298d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[26] & 0x08)
3299d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x02;	/* LE Advertising Report */
3300d0b13706SLuiz Augusto von Dentz 
3301d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Create Connection
3302d0b13706SLuiz Augusto von Dentz 	 * command, enable the corresponding event.
3303d0b13706SLuiz Augusto von Dentz 	 */
3304d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[26] & 0x10)
3305d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x01;	/* LE Connection Complete */
3306d0b13706SLuiz Augusto von Dentz 
3307d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Connection Update
3308d0b13706SLuiz Augusto von Dentz 	 * command, enable the corresponding event.
3309d0b13706SLuiz Augusto von Dentz 	 */
3310d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[27] & 0x04)
3311d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x04;	/* LE Connection Update Complete */
3312d0b13706SLuiz Augusto von Dentz 
3313d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Read Remote Used Features
3314d0b13706SLuiz Augusto von Dentz 	 * command, enable the corresponding event.
3315d0b13706SLuiz Augusto von Dentz 	 */
3316d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[27] & 0x20)
3317d0b13706SLuiz Augusto von Dentz 		/* LE Read Remote Used Features Complete */
3318d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x08;
3319d0b13706SLuiz Augusto von Dentz 
3320d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Read Local P-256
3321d0b13706SLuiz Augusto von Dentz 	 * Public Key command, enable the corresponding event.
3322d0b13706SLuiz Augusto von Dentz 	 */
3323d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[34] & 0x02)
3324d0b13706SLuiz Augusto von Dentz 		/* LE Read Local P-256 Public Key Complete */
3325d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x80;
3326d0b13706SLuiz Augusto von Dentz 
3327d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Generate DHKey
3328d0b13706SLuiz Augusto von Dentz 	 * command, enable the corresponding event.
3329d0b13706SLuiz Augusto von Dentz 	 */
3330d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[34] & 0x04)
3331d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x01;	/* LE Generate DHKey Complete */
3332d0b13706SLuiz Augusto von Dentz 
3333d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Set Default PHY or
3334d0b13706SLuiz Augusto von Dentz 	 * LE Set PHY commands, enable the corresponding event.
3335d0b13706SLuiz Augusto von Dentz 	 */
3336d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[35] & (0x20 | 0x40))
3337d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x08;        /* LE PHY Update Complete */
3338d0b13706SLuiz Augusto von Dentz 
3339d0b13706SLuiz Augusto von Dentz 	/* If the controller supports LE Set Extended Scan Parameters
3340d0b13706SLuiz Augusto von Dentz 	 * and LE Set Extended Scan Enable commands, enable the
3341d0b13706SLuiz Augusto von Dentz 	 * corresponding event.
3342d0b13706SLuiz Augusto von Dentz 	 */
3343d0b13706SLuiz Augusto von Dentz 	if (use_ext_scan(hdev))
3344d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x10;	/* LE Extended Advertising Report */
3345d0b13706SLuiz Augusto von Dentz 
3346d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Extended Advertising
3347d0b13706SLuiz Augusto von Dentz 	 * command, enable the corresponding event.
3348d0b13706SLuiz Augusto von Dentz 	 */
3349d0b13706SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
3350d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x02;	/* LE Advertising Set Terminated */
3351d0b13706SLuiz Augusto von Dentz 
3352d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
3353d0b13706SLuiz Augusto von Dentz 				     sizeof(events), events, HCI_CMD_TIMEOUT);
3354d0b13706SLuiz Augusto von Dentz }
3355d0b13706SLuiz Augusto von Dentz 
3356d0b13706SLuiz Augusto von Dentz /* Read LE Advertising Channel TX Power */
3357d0b13706SLuiz Augusto von Dentz static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
3358d0b13706SLuiz Augusto von Dentz {
3359d0b13706SLuiz Augusto von Dentz 	if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
3360d0b13706SLuiz Augusto von Dentz 		/* HCI TS spec forbids mixing of legacy and extended
3361d0b13706SLuiz Augusto von Dentz 		 * advertising commands wherein READ_ADV_TX_POWER is
3362d0b13706SLuiz Augusto von Dentz 		 * also included. So do not call it if extended adv
3363d0b13706SLuiz Augusto von Dentz 		 * is supported otherwise controller will return
3364d0b13706SLuiz Augusto von Dentz 		 * COMMAND_DISALLOWED for extended commands.
3365d0b13706SLuiz Augusto von Dentz 		 */
3366d0b13706SLuiz Augusto von Dentz 		return __hci_cmd_sync_status(hdev,
3367d0b13706SLuiz Augusto von Dentz 					       HCI_OP_LE_READ_ADV_TX_POWER,
3368d0b13706SLuiz Augusto von Dentz 					       0, NULL, HCI_CMD_TIMEOUT);
3369d0b13706SLuiz Augusto von Dentz 	}
3370d0b13706SLuiz Augusto von Dentz 
3371d0b13706SLuiz Augusto von Dentz 	return 0;
3372d0b13706SLuiz Augusto von Dentz }
3373d0b13706SLuiz Augusto von Dentz 
3374d0b13706SLuiz Augusto von Dentz /* Read LE Min/Max Tx Power*/
3375d0b13706SLuiz Augusto von Dentz static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
3376d0b13706SLuiz Augusto von Dentz {
3377d2f8114fSAditya Garg 	if (!(hdev->commands[38] & 0x80) ||
3378d2f8114fSAditya Garg 	    test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
3379d0b13706SLuiz Augusto von Dentz 		return 0;
3380d0b13706SLuiz Augusto von Dentz 
3381d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
3382d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3383d0b13706SLuiz Augusto von Dentz }
3384d0b13706SLuiz Augusto von Dentz 
3385d0b13706SLuiz Augusto von Dentz /* Read LE Accept List Size */
3386d0b13706SLuiz Augusto von Dentz static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
3387d0b13706SLuiz Augusto von Dentz {
3388d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[26] & 0x40))
3389d0b13706SLuiz Augusto von Dentz 		return 0;
3390d0b13706SLuiz Augusto von Dentz 
3391d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
3392d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3393d0b13706SLuiz Augusto von Dentz }
3394d0b13706SLuiz Augusto von Dentz 
3395d0b13706SLuiz Augusto von Dentz /* Clear LE Accept List */
3396d0b13706SLuiz Augusto von Dentz static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
3397d0b13706SLuiz Augusto von Dentz {
3398d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[26] & 0x80))
3399d0b13706SLuiz Augusto von Dentz 		return 0;
3400d0b13706SLuiz Augusto von Dentz 
3401d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
3402d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
3403d0b13706SLuiz Augusto von Dentz }
3404d0b13706SLuiz Augusto von Dentz 
3405d0b13706SLuiz Augusto von Dentz /* Read LE Resolving List Size */
3406d0b13706SLuiz Augusto von Dentz static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
3407d0b13706SLuiz Augusto von Dentz {
3408d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[34] & 0x40))
3409d0b13706SLuiz Augusto von Dentz 		return 0;
3410d0b13706SLuiz Augusto von Dentz 
3411d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
3412d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3413d0b13706SLuiz Augusto von Dentz }
3414d0b13706SLuiz Augusto von Dentz 
3415d0b13706SLuiz Augusto von Dentz /* Clear LE Resolving List */
3416d0b13706SLuiz Augusto von Dentz static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
3417d0b13706SLuiz Augusto von Dentz {
3418d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[34] & 0x20))
3419d0b13706SLuiz Augusto von Dentz 		return 0;
3420d0b13706SLuiz Augusto von Dentz 
3421d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
3422d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
3423d0b13706SLuiz Augusto von Dentz }
3424d0b13706SLuiz Augusto von Dentz 
3425d0b13706SLuiz Augusto von Dentz /* Set RPA timeout */
3426d0b13706SLuiz Augusto von Dentz static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
3427d0b13706SLuiz Augusto von Dentz {
3428d0b13706SLuiz Augusto von Dentz 	__le16 timeout = cpu_to_le16(hdev->rpa_timeout);
3429d0b13706SLuiz Augusto von Dentz 
3430d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[35] & 0x04))
3431d0b13706SLuiz Augusto von Dentz 		return 0;
3432d0b13706SLuiz Augusto von Dentz 
3433d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
3434d0b13706SLuiz Augusto von Dentz 				     sizeof(timeout), &timeout,
3435d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
3436d0b13706SLuiz Augusto von Dentz }
3437d0b13706SLuiz Augusto von Dentz 
3438d0b13706SLuiz Augusto von Dentz /* Read LE Maximum Data Length */
3439d0b13706SLuiz Augusto von Dentz static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
3440d0b13706SLuiz Augusto von Dentz {
3441d0b13706SLuiz Augusto von Dentz 	if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3442d0b13706SLuiz Augusto von Dentz 		return 0;
3443d0b13706SLuiz Augusto von Dentz 
3444d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
3445d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
3446d0b13706SLuiz Augusto von Dentz }
3447d0b13706SLuiz Augusto von Dentz 
3448d0b13706SLuiz Augusto von Dentz /* Read LE Suggested Default Data Length */
3449d0b13706SLuiz Augusto von Dentz static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
3450d0b13706SLuiz Augusto von Dentz {
3451d0b13706SLuiz Augusto von Dentz 	if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3452d0b13706SLuiz Augusto von Dentz 		return 0;
3453d0b13706SLuiz Augusto von Dentz 
3454d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
3455d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
3456d0b13706SLuiz Augusto von Dentz }
3457d0b13706SLuiz Augusto von Dentz 
3458d0b13706SLuiz Augusto von Dentz /* Read LE Number of Supported Advertising Sets */
3459d0b13706SLuiz Augusto von Dentz static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
3460d0b13706SLuiz Augusto von Dentz {
3461d0b13706SLuiz Augusto von Dentz 	if (!ext_adv_capable(hdev))
3462d0b13706SLuiz Augusto von Dentz 		return 0;
3463d0b13706SLuiz Augusto von Dentz 
3464d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev,
3465d0b13706SLuiz Augusto von Dentz 				     HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
3466d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3467d0b13706SLuiz Augusto von Dentz }
3468d0b13706SLuiz Augusto von Dentz 
3469d0b13706SLuiz Augusto von Dentz /* Write LE Host Supported */
3470d0b13706SLuiz Augusto von Dentz static int hci_set_le_support_sync(struct hci_dev *hdev)
3471d0b13706SLuiz Augusto von Dentz {
3472d0b13706SLuiz Augusto von Dentz 	struct hci_cp_write_le_host_supported cp;
3473d0b13706SLuiz Augusto von Dentz 
3474d0b13706SLuiz Augusto von Dentz 	/* LE-only devices do not support explicit enablement */
3475d0b13706SLuiz Augusto von Dentz 	if (!lmp_bredr_capable(hdev))
3476d0b13706SLuiz Augusto von Dentz 		return 0;
3477d0b13706SLuiz Augusto von Dentz 
3478d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3479d0b13706SLuiz Augusto von Dentz 
3480d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
3481d0b13706SLuiz Augusto von Dentz 		cp.le = 0x01;
3482d0b13706SLuiz Augusto von Dentz 		cp.simul = 0x00;
3483d0b13706SLuiz Augusto von Dentz 	}
3484d0b13706SLuiz Augusto von Dentz 
3485d0b13706SLuiz Augusto von Dentz 	if (cp.le == lmp_host_le_capable(hdev))
3486d0b13706SLuiz Augusto von Dentz 		return 0;
3487d0b13706SLuiz Augusto von Dentz 
3488d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3489d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3490d0b13706SLuiz Augusto von Dentz }
3491d0b13706SLuiz Augusto von Dentz 
3492d0b13706SLuiz Augusto von Dentz /* LE Controller init stage 3 command sequence */
3493d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init3[] = {
3494d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_SET_EVENT_MASK */
3495d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_set_event_mask_sync),
3496d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_ADV_TX_POWER */
3497d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_adv_tx_power_sync),
3498d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_TRANSMIT_POWER */
3499d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_tx_power_sync),
3500d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
3501d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_accept_list_size_sync),
3502d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_CLEAR_ACCEPT_LIST */
3503d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_clear_accept_list_sync),
3504d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
3505d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_resolv_list_size_sync),
3506d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_CLEAR_RESOLV_LIST */
3507d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_clear_resolv_list_sync),
3508d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_SET_RPA_TIMEOUT */
3509d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_set_rpa_timeout_sync),
3510d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_MAX_DATA_LEN */
3511d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_max_data_len_sync),
3512d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_DEF_DATA_LEN */
3513d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_def_data_len_sync),
3514d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
3515d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_num_support_adv_sets_sync),
3516d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_LE_HOST_SUPPORTED */
3517d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_set_le_support_sync),
3518d0b13706SLuiz Augusto von Dentz 	{}
3519d0b13706SLuiz Augusto von Dentz };
3520d0b13706SLuiz Augusto von Dentz 
3521d0b13706SLuiz Augusto von Dentz static int hci_init3_sync(struct hci_dev *hdev)
3522d0b13706SLuiz Augusto von Dentz {
3523d0b13706SLuiz Augusto von Dentz 	int err;
3524d0b13706SLuiz Augusto von Dentz 
3525d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
3526d0b13706SLuiz Augusto von Dentz 
3527d0b13706SLuiz Augusto von Dentz 	err = hci_init_stage_sync(hdev, hci_init3);
3528d0b13706SLuiz Augusto von Dentz 	if (err)
3529d0b13706SLuiz Augusto von Dentz 		return err;
3530d0b13706SLuiz Augusto von Dentz 
3531d0b13706SLuiz Augusto von Dentz 	if (lmp_le_capable(hdev))
3532d0b13706SLuiz Augusto von Dentz 		return hci_init_stage_sync(hdev, le_init3);
3533d0b13706SLuiz Augusto von Dentz 
3534d0b13706SLuiz Augusto von Dentz 	return 0;
3535d0b13706SLuiz Augusto von Dentz }
3536d0b13706SLuiz Augusto von Dentz 
3537d0b13706SLuiz Augusto von Dentz static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
3538d0b13706SLuiz Augusto von Dentz {
3539d0b13706SLuiz Augusto von Dentz 	struct hci_cp_delete_stored_link_key cp;
3540d0b13706SLuiz Augusto von Dentz 
3541d0b13706SLuiz Augusto von Dentz 	/* Some Broadcom based Bluetooth controllers do not support the
3542d0b13706SLuiz Augusto von Dentz 	 * Delete Stored Link Key command. They are clearly indicating its
3543d0b13706SLuiz Augusto von Dentz 	 * absence in the bit mask of supported commands.
3544d0b13706SLuiz Augusto von Dentz 	 *
3545d0b13706SLuiz Augusto von Dentz 	 * Check the supported commands and only if the command is marked
3546d0b13706SLuiz Augusto von Dentz 	 * as supported send it. If not supported assume that the controller
3547d0b13706SLuiz Augusto von Dentz 	 * does not have actual support for stored link keys which makes this
3548d0b13706SLuiz Augusto von Dentz 	 * command redundant anyway.
3549d0b13706SLuiz Augusto von Dentz 	 *
3550d0b13706SLuiz Augusto von Dentz 	 * Some controllers indicate that they support handling deleting
3551d0b13706SLuiz Augusto von Dentz 	 * stored link keys, but they don't. The quirk lets a driver
3552d0b13706SLuiz Augusto von Dentz 	 * just disable this command.
3553d0b13706SLuiz Augusto von Dentz 	 */
3554d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[6] & 0x80) ||
3555d0b13706SLuiz Augusto von Dentz 	    test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3556d0b13706SLuiz Augusto von Dentz 		return 0;
3557d0b13706SLuiz Augusto von Dentz 
3558d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3559d0b13706SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, BDADDR_ANY);
3560d0b13706SLuiz Augusto von Dentz 	cp.delete_all = 0x01;
3561d0b13706SLuiz Augusto von Dentz 
3562d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
3563d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3564d0b13706SLuiz Augusto von Dentz }
3565d0b13706SLuiz Augusto von Dentz 
3566d0b13706SLuiz Augusto von Dentz static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
3567d0b13706SLuiz Augusto von Dentz {
3568d0b13706SLuiz Augusto von Dentz 	u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3569d0b13706SLuiz Augusto von Dentz 	bool changed = false;
3570d0b13706SLuiz Augusto von Dentz 
3571d0b13706SLuiz Augusto von Dentz 	/* Set event mask page 2 if the HCI command for it is supported */
3572d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[22] & 0x04))
3573d0b13706SLuiz Augusto von Dentz 		return 0;
3574d0b13706SLuiz Augusto von Dentz 
3575d0b13706SLuiz Augusto von Dentz 	/* If Connectionless Peripheral Broadcast central role is supported
3576d0b13706SLuiz Augusto von Dentz 	 * enable all necessary events for it.
3577d0b13706SLuiz Augusto von Dentz 	 */
3578d0b13706SLuiz Augusto von Dentz 	if (lmp_cpb_central_capable(hdev)) {
3579d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x40;	/* Triggered Clock Capture */
3580d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x80;	/* Synchronization Train Complete */
3581d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x10;	/* Peripheral Page Response Timeout */
3582d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x20;	/* CPB Channel Map Change */
3583d0b13706SLuiz Augusto von Dentz 		changed = true;
3584d0b13706SLuiz Augusto von Dentz 	}
3585d0b13706SLuiz Augusto von Dentz 
3586d0b13706SLuiz Augusto von Dentz 	/* If Connectionless Peripheral Broadcast peripheral role is supported
3587d0b13706SLuiz Augusto von Dentz 	 * enable all necessary events for it.
3588d0b13706SLuiz Augusto von Dentz 	 */
3589d0b13706SLuiz Augusto von Dentz 	if (lmp_cpb_peripheral_capable(hdev)) {
3590d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x01;	/* Synchronization Train Received */
3591d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x02;	/* CPB Receive */
3592d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x04;	/* CPB Timeout */
3593d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x08;	/* Truncated Page Complete */
3594d0b13706SLuiz Augusto von Dentz 		changed = true;
3595d0b13706SLuiz Augusto von Dentz 	}
3596d0b13706SLuiz Augusto von Dentz 
3597d0b13706SLuiz Augusto von Dentz 	/* Enable Authenticated Payload Timeout Expired event if supported */
3598d0b13706SLuiz Augusto von Dentz 	if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
3599d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x80;
3600d0b13706SLuiz Augusto von Dentz 		changed = true;
3601d0b13706SLuiz Augusto von Dentz 	}
3602d0b13706SLuiz Augusto von Dentz 
3603d0b13706SLuiz Augusto von Dentz 	/* Some Broadcom based controllers indicate support for Set Event
3604d0b13706SLuiz Augusto von Dentz 	 * Mask Page 2 command, but then actually do not support it. Since
3605d0b13706SLuiz Augusto von Dentz 	 * the default value is all bits set to zero, the command is only
3606d0b13706SLuiz Augusto von Dentz 	 * required if the event mask has to be changed. In case no change
3607d0b13706SLuiz Augusto von Dentz 	 * to the event mask is needed, skip this command.
3608d0b13706SLuiz Augusto von Dentz 	 */
3609d0b13706SLuiz Augusto von Dentz 	if (!changed)
3610d0b13706SLuiz Augusto von Dentz 		return 0;
3611d0b13706SLuiz Augusto von Dentz 
3612d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
3613d0b13706SLuiz Augusto von Dentz 				     sizeof(events), events, HCI_CMD_TIMEOUT);
3614d0b13706SLuiz Augusto von Dentz }
3615d0b13706SLuiz Augusto von Dentz 
3616d0b13706SLuiz Augusto von Dentz /* Read local codec list if the HCI command is supported */
3617d0b13706SLuiz Augusto von Dentz static int hci_read_local_codecs_sync(struct hci_dev *hdev)
3618d0b13706SLuiz Augusto von Dentz {
3619d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[29] & 0x20))
3620d0b13706SLuiz Augusto von Dentz 		return 0;
3621d0b13706SLuiz Augusto von Dentz 
3622d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_CODECS, 0, NULL,
3623d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
3624d0b13706SLuiz Augusto von Dentz }
3625d0b13706SLuiz Augusto von Dentz 
3626d0b13706SLuiz Augusto von Dentz /* Read local pairing options if the HCI command is supported */
3627d0b13706SLuiz Augusto von Dentz static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
3628d0b13706SLuiz Augusto von Dentz {
3629d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[41] & 0x08))
3630d0b13706SLuiz Augusto von Dentz 		return 0;
3631d0b13706SLuiz Augusto von Dentz 
3632d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
3633d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3634d0b13706SLuiz Augusto von Dentz }
3635d0b13706SLuiz Augusto von Dentz 
3636d0b13706SLuiz Augusto von Dentz /* Get MWS transport configuration if the HCI command is supported */
3637d0b13706SLuiz Augusto von Dentz static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
3638d0b13706SLuiz Augusto von Dentz {
3639d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[30] & 0x08))
3640d0b13706SLuiz Augusto von Dentz 		return 0;
3641d0b13706SLuiz Augusto von Dentz 
3642d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
3643d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3644d0b13706SLuiz Augusto von Dentz }
3645d0b13706SLuiz Augusto von Dentz 
3646d0b13706SLuiz Augusto von Dentz /* Check for Synchronization Train support */
3647d0b13706SLuiz Augusto von Dentz static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
3648d0b13706SLuiz Augusto von Dentz {
3649d0b13706SLuiz Augusto von Dentz 	if (!lmp_sync_train_capable(hdev))
3650d0b13706SLuiz Augusto von Dentz 		return 0;
3651d0b13706SLuiz Augusto von Dentz 
3652d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
3653d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3654d0b13706SLuiz Augusto von Dentz }
3655d0b13706SLuiz Augusto von Dentz 
3656d0b13706SLuiz Augusto von Dentz /* Enable Secure Connections if supported and configured */
3657d0b13706SLuiz Augusto von Dentz static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
3658d0b13706SLuiz Augusto von Dentz {
3659d0b13706SLuiz Augusto von Dentz 	u8 support = 0x01;
3660d0b13706SLuiz Augusto von Dentz 
3661d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3662d0b13706SLuiz Augusto von Dentz 	    !bredr_sc_enabled(hdev))
3663d0b13706SLuiz Augusto von Dentz 		return 0;
3664d0b13706SLuiz Augusto von Dentz 
3665d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3666d0b13706SLuiz Augusto von Dentz 				     sizeof(support), &support,
3667d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
3668d0b13706SLuiz Augusto von Dentz }
3669d0b13706SLuiz Augusto von Dentz 
3670d0b13706SLuiz Augusto von Dentz /* Set erroneous data reporting if supported to the wideband speech
3671d0b13706SLuiz Augusto von Dentz  * setting value
3672d0b13706SLuiz Augusto von Dentz  */
3673d0b13706SLuiz Augusto von Dentz static int hci_set_err_data_report_sync(struct hci_dev *hdev)
3674d0b13706SLuiz Augusto von Dentz {
3675d0b13706SLuiz Augusto von Dentz 	struct hci_cp_write_def_err_data_reporting cp;
3676d0b13706SLuiz Augusto von Dentz 	bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
3677d0b13706SLuiz Augusto von Dentz 
3678d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[18] & 0x08) ||
3679d0b13706SLuiz Augusto von Dentz 	    test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3680d0b13706SLuiz Augusto von Dentz 		return 0;
3681d0b13706SLuiz Augusto von Dentz 
3682d0b13706SLuiz Augusto von Dentz 	if (enabled == hdev->err_data_reporting)
3683d0b13706SLuiz Augusto von Dentz 		return 0;
3684d0b13706SLuiz Augusto von Dentz 
3685d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3686d0b13706SLuiz Augusto von Dentz 	cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
3687d0b13706SLuiz Augusto von Dentz 				ERR_DATA_REPORTING_DISABLED;
3688d0b13706SLuiz Augusto von Dentz 
3689d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
3690d0b13706SLuiz Augusto von Dentz 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3691d0b13706SLuiz Augusto von Dentz }
3692d0b13706SLuiz Augusto von Dentz 
3693d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init4[] = {
3694d0b13706SLuiz Augusto von Dentz 	 /* HCI_OP_DELETE_STORED_LINK_KEY */
3695d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_delete_stored_link_key_sync),
3696d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_SET_EVENT_MASK_PAGE_2 */
3697d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_set_event_mask_page_2_sync),
3698d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_CODECS */
3699d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_codecs_sync),
3700d0b13706SLuiz Augusto von Dentz 	 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
3701d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_pairing_opts_sync),
3702d0b13706SLuiz Augusto von Dentz 	 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
3703d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_get_mws_transport_config_sync),
3704d0b13706SLuiz Augusto von Dentz 	 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
3705d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_sync_train_params_sync),
3706d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_SC_SUPPORT */
3707d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_sc_support_1_sync),
3708d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
3709d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_set_err_data_report_sync),
3710d0b13706SLuiz Augusto von Dentz 	{}
3711d0b13706SLuiz Augusto von Dentz };
3712d0b13706SLuiz Augusto von Dentz 
3713d0b13706SLuiz Augusto von Dentz /* Set Suggested Default Data Length to maximum if supported */
3714d0b13706SLuiz Augusto von Dentz static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
3715d0b13706SLuiz Augusto von Dentz {
3716d0b13706SLuiz Augusto von Dentz 	struct hci_cp_le_write_def_data_len cp;
3717d0b13706SLuiz Augusto von Dentz 
3718d0b13706SLuiz Augusto von Dentz 	if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3719d0b13706SLuiz Augusto von Dentz 		return 0;
3720d0b13706SLuiz Augusto von Dentz 
3721d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3722d0b13706SLuiz Augusto von Dentz 	cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
3723d0b13706SLuiz Augusto von Dentz 	cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
3724d0b13706SLuiz Augusto von Dentz 
3725d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
3726d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3727d0b13706SLuiz Augusto von Dentz }
3728d0b13706SLuiz Augusto von Dentz 
3729d0b13706SLuiz Augusto von Dentz /* Set Default PHY parameters if command is supported */
3730d0b13706SLuiz Augusto von Dentz static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
3731d0b13706SLuiz Augusto von Dentz {
3732d0b13706SLuiz Augusto von Dentz 	struct hci_cp_le_set_default_phy cp;
3733d0b13706SLuiz Augusto von Dentz 
3734d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[35] & 0x20))
3735d0b13706SLuiz Augusto von Dentz 		return 0;
3736d0b13706SLuiz Augusto von Dentz 
3737d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3738d0b13706SLuiz Augusto von Dentz 	cp.all_phys = 0x00;
3739d0b13706SLuiz Augusto von Dentz 	cp.tx_phys = hdev->le_tx_def_phys;
3740d0b13706SLuiz Augusto von Dentz 	cp.rx_phys = hdev->le_rx_def_phys;
3741d0b13706SLuiz Augusto von Dentz 
3742d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
3743d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3744d0b13706SLuiz Augusto von Dentz }
3745d0b13706SLuiz Augusto von Dentz 
3746d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init4[] = {
3747d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_WRITE_DEF_DATA_LEN */
3748d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_set_write_def_data_len_sync),
3749d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_SET_DEFAULT_PHY */
3750d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_set_default_phy_sync),
3751d0b13706SLuiz Augusto von Dentz 	{}
3752d0b13706SLuiz Augusto von Dentz };
3753d0b13706SLuiz Augusto von Dentz 
3754d0b13706SLuiz Augusto von Dentz static int hci_init4_sync(struct hci_dev *hdev)
3755d0b13706SLuiz Augusto von Dentz {
3756d0b13706SLuiz Augusto von Dentz 	int err;
3757d0b13706SLuiz Augusto von Dentz 
3758d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
3759d0b13706SLuiz Augusto von Dentz 
3760d0b13706SLuiz Augusto von Dentz 	err = hci_init_stage_sync(hdev, hci_init4);
3761d0b13706SLuiz Augusto von Dentz 	if (err)
3762d0b13706SLuiz Augusto von Dentz 		return err;
3763d0b13706SLuiz Augusto von Dentz 
3764d0b13706SLuiz Augusto von Dentz 	if (lmp_le_capable(hdev))
3765d0b13706SLuiz Augusto von Dentz 		return hci_init_stage_sync(hdev, le_init4);
3766d0b13706SLuiz Augusto von Dentz 
3767d0b13706SLuiz Augusto von Dentz 	return 0;
3768d0b13706SLuiz Augusto von Dentz }
3769d0b13706SLuiz Augusto von Dentz 
3770d0b13706SLuiz Augusto von Dentz static int hci_init_sync(struct hci_dev *hdev)
3771d0b13706SLuiz Augusto von Dentz {
3772d0b13706SLuiz Augusto von Dentz 	int err;
3773d0b13706SLuiz Augusto von Dentz 
3774d0b13706SLuiz Augusto von Dentz 	err = hci_init1_sync(hdev);
3775d0b13706SLuiz Augusto von Dentz 	if (err < 0)
3776d0b13706SLuiz Augusto von Dentz 		return err;
3777d0b13706SLuiz Augusto von Dentz 
3778d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_SETUP))
3779d0b13706SLuiz Augusto von Dentz 		hci_debugfs_create_basic(hdev);
3780d0b13706SLuiz Augusto von Dentz 
3781d0b13706SLuiz Augusto von Dentz 	err = hci_init2_sync(hdev);
3782d0b13706SLuiz Augusto von Dentz 	if (err < 0)
3783d0b13706SLuiz Augusto von Dentz 		return err;
3784d0b13706SLuiz Augusto von Dentz 
3785d0b13706SLuiz Augusto von Dentz 	/* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
3786d0b13706SLuiz Augusto von Dentz 	 * BR/EDR/LE type controllers. AMP controllers only need the
3787d0b13706SLuiz Augusto von Dentz 	 * first two stages of init.
3788d0b13706SLuiz Augusto von Dentz 	 */
3789d0b13706SLuiz Augusto von Dentz 	if (hdev->dev_type != HCI_PRIMARY)
3790d0b13706SLuiz Augusto von Dentz 		return 0;
3791d0b13706SLuiz Augusto von Dentz 
3792d0b13706SLuiz Augusto von Dentz 	err = hci_init3_sync(hdev);
3793d0b13706SLuiz Augusto von Dentz 	if (err < 0)
3794d0b13706SLuiz Augusto von Dentz 		return err;
3795d0b13706SLuiz Augusto von Dentz 
3796d0b13706SLuiz Augusto von Dentz 	err = hci_init4_sync(hdev);
3797d0b13706SLuiz Augusto von Dentz 	if (err < 0)
3798d0b13706SLuiz Augusto von Dentz 		return err;
3799d0b13706SLuiz Augusto von Dentz 
3800d0b13706SLuiz Augusto von Dentz 	/* This function is only called when the controller is actually in
3801d0b13706SLuiz Augusto von Dentz 	 * configured state. When the controller is marked as unconfigured,
3802d0b13706SLuiz Augusto von Dentz 	 * this initialization procedure is not run.
3803d0b13706SLuiz Augusto von Dentz 	 *
3804d0b13706SLuiz Augusto von Dentz 	 * It means that it is possible that a controller runs through its
3805d0b13706SLuiz Augusto von Dentz 	 * setup phase and then discovers missing settings. If that is the
3806d0b13706SLuiz Augusto von Dentz 	 * case, then this function will not be called. It then will only
3807d0b13706SLuiz Augusto von Dentz 	 * be called during the config phase.
3808d0b13706SLuiz Augusto von Dentz 	 *
3809d0b13706SLuiz Augusto von Dentz 	 * So only when in setup phase or config phase, create the debugfs
3810d0b13706SLuiz Augusto von Dentz 	 * entries and register the SMP channels.
3811d0b13706SLuiz Augusto von Dentz 	 */
3812d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3813d0b13706SLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_CONFIG))
3814d0b13706SLuiz Augusto von Dentz 		return 0;
3815d0b13706SLuiz Augusto von Dentz 
3816d0b13706SLuiz Augusto von Dentz 	hci_debugfs_create_common(hdev);
3817d0b13706SLuiz Augusto von Dentz 
3818d0b13706SLuiz Augusto von Dentz 	if (lmp_bredr_capable(hdev))
3819d0b13706SLuiz Augusto von Dentz 		hci_debugfs_create_bredr(hdev);
3820d0b13706SLuiz Augusto von Dentz 
3821d0b13706SLuiz Augusto von Dentz 	if (lmp_le_capable(hdev))
3822d0b13706SLuiz Augusto von Dentz 		hci_debugfs_create_le(hdev);
3823d0b13706SLuiz Augusto von Dentz 
3824d0b13706SLuiz Augusto von Dentz 	return 0;
3825d0b13706SLuiz Augusto von Dentz }
3826d0b13706SLuiz Augusto von Dentz 
38276b5c1cdaSLuiz Augusto von Dentz #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
38286b5c1cdaSLuiz Augusto von Dentz 
38296b5c1cdaSLuiz Augusto von Dentz static const struct {
38306b5c1cdaSLuiz Augusto von Dentz 	unsigned long quirk;
38316b5c1cdaSLuiz Augusto von Dentz 	const char *desc;
38326b5c1cdaSLuiz Augusto von Dentz } hci_broken_table[] = {
38336b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
38346b5c1cdaSLuiz Augusto von Dentz 			 "HCI Read Local Supported Commands not supported"),
38356b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(STORED_LINK_KEY,
38366b5c1cdaSLuiz Augusto von Dentz 			 "HCI Delete Stored Link Key command is advertised, "
38376b5c1cdaSLuiz Augusto von Dentz 			 "but not supported."),
38386b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
38396b5c1cdaSLuiz Augusto von Dentz 			 "HCI Read Default Erroneous Data Reporting command is "
38406b5c1cdaSLuiz Augusto von Dentz 			 "advertised, but not supported."),
38416b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
38426b5c1cdaSLuiz Augusto von Dentz 			 "HCI Read Transmit Power Level command is advertised, "
38436b5c1cdaSLuiz Augusto von Dentz 			 "but not supported."),
38446b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
38456b5c1cdaSLuiz Augusto von Dentz 			 "HCI Set Event Filter command not supported."),
38466b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
38476b5c1cdaSLuiz Augusto von Dentz 			 "HCI Enhanced Setup Synchronous Connection command is "
38486b5c1cdaSLuiz Augusto von Dentz 			 "advertised, but not supported.")
38496b5c1cdaSLuiz Augusto von Dentz };
38506b5c1cdaSLuiz Augusto von Dentz 
3851d0b13706SLuiz Augusto von Dentz int hci_dev_open_sync(struct hci_dev *hdev)
3852d0b13706SLuiz Augusto von Dentz {
3853d0b13706SLuiz Augusto von Dentz 	int ret = 0;
3854d0b13706SLuiz Augusto von Dentz 
3855d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
3856d0b13706SLuiz Augusto von Dentz 
3857d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
3858d0b13706SLuiz Augusto von Dentz 		ret = -ENODEV;
3859d0b13706SLuiz Augusto von Dentz 		goto done;
3860d0b13706SLuiz Augusto von Dentz 	}
3861d0b13706SLuiz Augusto von Dentz 
3862d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3863d0b13706SLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_CONFIG)) {
3864d0b13706SLuiz Augusto von Dentz 		/* Check for rfkill but allow the HCI setup stage to
3865d0b13706SLuiz Augusto von Dentz 		 * proceed (which in itself doesn't cause any RF activity).
3866d0b13706SLuiz Augusto von Dentz 		 */
3867d0b13706SLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
3868d0b13706SLuiz Augusto von Dentz 			ret = -ERFKILL;
3869d0b13706SLuiz Augusto von Dentz 			goto done;
3870d0b13706SLuiz Augusto von Dentz 		}
3871d0b13706SLuiz Augusto von Dentz 
3872d0b13706SLuiz Augusto von Dentz 		/* Check for valid public address or a configured static
3873d0b13706SLuiz Augusto von Dentz 		 * random address, but let the HCI setup proceed to
3874d0b13706SLuiz Augusto von Dentz 		 * be able to determine if there is a public address
3875d0b13706SLuiz Augusto von Dentz 		 * or not.
3876d0b13706SLuiz Augusto von Dentz 		 *
3877d0b13706SLuiz Augusto von Dentz 		 * In case of user channel usage, it is not important
3878d0b13706SLuiz Augusto von Dentz 		 * if a public address or static random address is
3879d0b13706SLuiz Augusto von Dentz 		 * available.
3880d0b13706SLuiz Augusto von Dentz 		 *
3881d0b13706SLuiz Augusto von Dentz 		 * This check is only valid for BR/EDR controllers
3882d0b13706SLuiz Augusto von Dentz 		 * since AMP controllers do not have an address.
3883d0b13706SLuiz Augusto von Dentz 		 */
3884d0b13706SLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
3885d0b13706SLuiz Augusto von Dentz 		    hdev->dev_type == HCI_PRIMARY &&
3886d0b13706SLuiz Augusto von Dentz 		    !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3887d0b13706SLuiz Augusto von Dentz 		    !bacmp(&hdev->static_addr, BDADDR_ANY)) {
3888d0b13706SLuiz Augusto von Dentz 			ret = -EADDRNOTAVAIL;
3889d0b13706SLuiz Augusto von Dentz 			goto done;
3890d0b13706SLuiz Augusto von Dentz 		}
3891d0b13706SLuiz Augusto von Dentz 	}
3892d0b13706SLuiz Augusto von Dentz 
3893d0b13706SLuiz Augusto von Dentz 	if (test_bit(HCI_UP, &hdev->flags)) {
3894d0b13706SLuiz Augusto von Dentz 		ret = -EALREADY;
3895d0b13706SLuiz Augusto von Dentz 		goto done;
3896d0b13706SLuiz Augusto von Dentz 	}
3897d0b13706SLuiz Augusto von Dentz 
3898d0b13706SLuiz Augusto von Dentz 	if (hdev->open(hdev)) {
3899d0b13706SLuiz Augusto von Dentz 		ret = -EIO;
3900d0b13706SLuiz Augusto von Dentz 		goto done;
3901d0b13706SLuiz Augusto von Dentz 	}
3902d0b13706SLuiz Augusto von Dentz 
3903d0b13706SLuiz Augusto von Dentz 	set_bit(HCI_RUNNING, &hdev->flags);
3904d0b13706SLuiz Augusto von Dentz 	hci_sock_dev_event(hdev, HCI_DEV_OPEN);
3905d0b13706SLuiz Augusto von Dentz 
3906d0b13706SLuiz Augusto von Dentz 	atomic_set(&hdev->cmd_cnt, 1);
3907d0b13706SLuiz Augusto von Dentz 	set_bit(HCI_INIT, &hdev->flags);
3908d0b13706SLuiz Augusto von Dentz 
3909d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_SETUP) ||
3910d0b13706SLuiz Augusto von Dentz 	    test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) {
3911d0b13706SLuiz Augusto von Dentz 		bool invalid_bdaddr;
39126b5c1cdaSLuiz Augusto von Dentz 		size_t i;
3913d0b13706SLuiz Augusto von Dentz 
3914d0b13706SLuiz Augusto von Dentz 		hci_sock_dev_event(hdev, HCI_DEV_SETUP);
3915d0b13706SLuiz Augusto von Dentz 
3916d0b13706SLuiz Augusto von Dentz 		if (hdev->setup)
3917d0b13706SLuiz Augusto von Dentz 			ret = hdev->setup(hdev);
3918d0b13706SLuiz Augusto von Dentz 
39196b5c1cdaSLuiz Augusto von Dentz 		for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
39206b5c1cdaSLuiz Augusto von Dentz 			if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
39216b5c1cdaSLuiz Augusto von Dentz 				bt_dev_warn(hdev, "%s",
39226b5c1cdaSLuiz Augusto von Dentz 					    hci_broken_table[i].desc);
39236b5c1cdaSLuiz Augusto von Dentz 		}
39246b5c1cdaSLuiz Augusto von Dentz 
3925d0b13706SLuiz Augusto von Dentz 		/* The transport driver can set the quirk to mark the
3926d0b13706SLuiz Augusto von Dentz 		 * BD_ADDR invalid before creating the HCI device or in
3927d0b13706SLuiz Augusto von Dentz 		 * its setup callback.
3928d0b13706SLuiz Augusto von Dentz 		 */
3929d0b13706SLuiz Augusto von Dentz 		invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR,
3930d0b13706SLuiz Augusto von Dentz 					  &hdev->quirks);
3931d0b13706SLuiz Augusto von Dentz 
3932d0b13706SLuiz Augusto von Dentz 		if (ret)
3933d0b13706SLuiz Augusto von Dentz 			goto setup_failed;
3934d0b13706SLuiz Augusto von Dentz 
3935d0b13706SLuiz Augusto von Dentz 		if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) {
3936d0b13706SLuiz Augusto von Dentz 			if (!bacmp(&hdev->public_addr, BDADDR_ANY))
3937d0b13706SLuiz Augusto von Dentz 				hci_dev_get_bd_addr_from_property(hdev);
3938d0b13706SLuiz Augusto von Dentz 
3939d0b13706SLuiz Augusto von Dentz 			if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
3940d0b13706SLuiz Augusto von Dentz 			    hdev->set_bdaddr) {
3941d0b13706SLuiz Augusto von Dentz 				ret = hdev->set_bdaddr(hdev,
3942d0b13706SLuiz Augusto von Dentz 						       &hdev->public_addr);
3943d0b13706SLuiz Augusto von Dentz 
3944d0b13706SLuiz Augusto von Dentz 				/* If setting of the BD_ADDR from the device
3945d0b13706SLuiz Augusto von Dentz 				 * property succeeds, then treat the address
3946d0b13706SLuiz Augusto von Dentz 				 * as valid even if the invalid BD_ADDR
3947d0b13706SLuiz Augusto von Dentz 				 * quirk indicates otherwise.
3948d0b13706SLuiz Augusto von Dentz 				 */
3949d0b13706SLuiz Augusto von Dentz 				if (!ret)
3950d0b13706SLuiz Augusto von Dentz 					invalid_bdaddr = false;
3951d0b13706SLuiz Augusto von Dentz 			}
3952d0b13706SLuiz Augusto von Dentz 		}
3953d0b13706SLuiz Augusto von Dentz 
3954d0b13706SLuiz Augusto von Dentz setup_failed:
3955d0b13706SLuiz Augusto von Dentz 		/* The transport driver can set these quirks before
3956d0b13706SLuiz Augusto von Dentz 		 * creating the HCI device or in its setup callback.
3957d0b13706SLuiz Augusto von Dentz 		 *
3958d0b13706SLuiz Augusto von Dentz 		 * For the invalid BD_ADDR quirk it is possible that
3959d0b13706SLuiz Augusto von Dentz 		 * it becomes a valid address if the bootloader does
3960d0b13706SLuiz Augusto von Dentz 		 * provide it (see above).
3961d0b13706SLuiz Augusto von Dentz 		 *
3962d0b13706SLuiz Augusto von Dentz 		 * In case any of them is set, the controller has to
3963d0b13706SLuiz Augusto von Dentz 		 * start up as unconfigured.
3964d0b13706SLuiz Augusto von Dentz 		 */
3965d0b13706SLuiz Augusto von Dentz 		if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
3966d0b13706SLuiz Augusto von Dentz 		    invalid_bdaddr)
3967d0b13706SLuiz Augusto von Dentz 			hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
3968d0b13706SLuiz Augusto von Dentz 
3969d0b13706SLuiz Augusto von Dentz 		/* For an unconfigured controller it is required to
3970d0b13706SLuiz Augusto von Dentz 		 * read at least the version information provided by
3971d0b13706SLuiz Augusto von Dentz 		 * the Read Local Version Information command.
3972d0b13706SLuiz Augusto von Dentz 		 *
3973d0b13706SLuiz Augusto von Dentz 		 * If the set_bdaddr driver callback is provided, then
3974d0b13706SLuiz Augusto von Dentz 		 * also the original Bluetooth public device address
3975d0b13706SLuiz Augusto von Dentz 		 * will be read using the Read BD Address command.
3976d0b13706SLuiz Augusto von Dentz 		 */
3977d0b13706SLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
3978d0b13706SLuiz Augusto von Dentz 			ret = hci_unconf_init_sync(hdev);
3979d0b13706SLuiz Augusto von Dentz 	}
3980d0b13706SLuiz Augusto von Dentz 
3981d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
3982d0b13706SLuiz Augusto von Dentz 		/* If public address change is configured, ensure that
3983d0b13706SLuiz Augusto von Dentz 		 * the address gets programmed. If the driver does not
3984d0b13706SLuiz Augusto von Dentz 		 * support changing the public address, fail the power
3985d0b13706SLuiz Augusto von Dentz 		 * on procedure.
3986d0b13706SLuiz Augusto von Dentz 		 */
3987d0b13706SLuiz Augusto von Dentz 		if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
3988d0b13706SLuiz Augusto von Dentz 		    hdev->set_bdaddr)
3989d0b13706SLuiz Augusto von Dentz 			ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
3990d0b13706SLuiz Augusto von Dentz 		else
3991d0b13706SLuiz Augusto von Dentz 			ret = -EADDRNOTAVAIL;
3992d0b13706SLuiz Augusto von Dentz 	}
3993d0b13706SLuiz Augusto von Dentz 
3994d0b13706SLuiz Augusto von Dentz 	if (!ret) {
3995d0b13706SLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
3996d0b13706SLuiz Augusto von Dentz 		    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
3997d0b13706SLuiz Augusto von Dentz 			ret = hci_init_sync(hdev);
3998d0b13706SLuiz Augusto von Dentz 			if (!ret && hdev->post_init)
3999d0b13706SLuiz Augusto von Dentz 				ret = hdev->post_init(hdev);
4000d0b13706SLuiz Augusto von Dentz 		}
4001d0b13706SLuiz Augusto von Dentz 	}
4002d0b13706SLuiz Augusto von Dentz 
4003d0b13706SLuiz Augusto von Dentz 	/* If the HCI Reset command is clearing all diagnostic settings,
4004d0b13706SLuiz Augusto von Dentz 	 * then they need to be reprogrammed after the init procedure
4005d0b13706SLuiz Augusto von Dentz 	 * completed.
4006d0b13706SLuiz Augusto von Dentz 	 */
4007d0b13706SLuiz Augusto von Dentz 	if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4008d0b13706SLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4009d0b13706SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4010d0b13706SLuiz Augusto von Dentz 		ret = hdev->set_diag(hdev, true);
4011d0b13706SLuiz Augusto von Dentz 
4012385315deSJesse Melhuish 	if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4013d0b13706SLuiz Augusto von Dentz 		msft_do_open(hdev);
4014d0b13706SLuiz Augusto von Dentz 		aosp_do_open(hdev);
4015385315deSJesse Melhuish 	}
4016d0b13706SLuiz Augusto von Dentz 
4017d0b13706SLuiz Augusto von Dentz 	clear_bit(HCI_INIT, &hdev->flags);
4018d0b13706SLuiz Augusto von Dentz 
4019d0b13706SLuiz Augusto von Dentz 	if (!ret) {
4020d0b13706SLuiz Augusto von Dentz 		hci_dev_hold(hdev);
4021d0b13706SLuiz Augusto von Dentz 		hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4022d0b13706SLuiz Augusto von Dentz 		hci_adv_instances_set_rpa_expired(hdev, true);
4023d0b13706SLuiz Augusto von Dentz 		set_bit(HCI_UP, &hdev->flags);
4024d0b13706SLuiz Augusto von Dentz 		hci_sock_dev_event(hdev, HCI_DEV_UP);
4025d0b13706SLuiz Augusto von Dentz 		hci_leds_update_powered(hdev, true);
4026d0b13706SLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4027d0b13706SLuiz Augusto von Dentz 		    !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4028d0b13706SLuiz Augusto von Dentz 		    !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4029d0b13706SLuiz Augusto von Dentz 		    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4030d0b13706SLuiz Augusto von Dentz 		    hci_dev_test_flag(hdev, HCI_MGMT) &&
4031d0b13706SLuiz Augusto von Dentz 		    hdev->dev_type == HCI_PRIMARY) {
4032d0b13706SLuiz Augusto von Dentz 			ret = hci_powered_update_sync(hdev);
4033d0b13706SLuiz Augusto von Dentz 		}
4034d0b13706SLuiz Augusto von Dentz 	} else {
4035d0b13706SLuiz Augusto von Dentz 		/* Init failed, cleanup */
4036d0b13706SLuiz Augusto von Dentz 		flush_work(&hdev->tx_work);
4037d0b13706SLuiz Augusto von Dentz 
4038d0b13706SLuiz Augusto von Dentz 		/* Since hci_rx_work() is possible to awake new cmd_work
4039d0b13706SLuiz Augusto von Dentz 		 * it should be flushed first to avoid unexpected call of
4040d0b13706SLuiz Augusto von Dentz 		 * hci_cmd_work()
4041d0b13706SLuiz Augusto von Dentz 		 */
4042d0b13706SLuiz Augusto von Dentz 		flush_work(&hdev->rx_work);
4043d0b13706SLuiz Augusto von Dentz 		flush_work(&hdev->cmd_work);
4044d0b13706SLuiz Augusto von Dentz 
4045d0b13706SLuiz Augusto von Dentz 		skb_queue_purge(&hdev->cmd_q);
4046d0b13706SLuiz Augusto von Dentz 		skb_queue_purge(&hdev->rx_q);
4047d0b13706SLuiz Augusto von Dentz 
4048d0b13706SLuiz Augusto von Dentz 		if (hdev->flush)
4049d0b13706SLuiz Augusto von Dentz 			hdev->flush(hdev);
4050d0b13706SLuiz Augusto von Dentz 
4051d0b13706SLuiz Augusto von Dentz 		if (hdev->sent_cmd) {
4052d0b13706SLuiz Augusto von Dentz 			kfree_skb(hdev->sent_cmd);
4053d0b13706SLuiz Augusto von Dentz 			hdev->sent_cmd = NULL;
4054d0b13706SLuiz Augusto von Dentz 		}
4055d0b13706SLuiz Augusto von Dentz 
4056d0b13706SLuiz Augusto von Dentz 		clear_bit(HCI_RUNNING, &hdev->flags);
4057d0b13706SLuiz Augusto von Dentz 		hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4058d0b13706SLuiz Augusto von Dentz 
4059d0b13706SLuiz Augusto von Dentz 		hdev->close(hdev);
4060d0b13706SLuiz Augusto von Dentz 		hdev->flags &= BIT(HCI_RAW);
4061d0b13706SLuiz Augusto von Dentz 	}
4062d0b13706SLuiz Augusto von Dentz 
4063d0b13706SLuiz Augusto von Dentz done:
4064d0b13706SLuiz Augusto von Dentz 	return ret;
4065d0b13706SLuiz Augusto von Dentz }
4066d0b13706SLuiz Augusto von Dentz 
4067d0b13706SLuiz Augusto von Dentz /* This function requires the caller holds hdev->lock */
4068d0b13706SLuiz Augusto von Dentz static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4069d0b13706SLuiz Augusto von Dentz {
4070d0b13706SLuiz Augusto von Dentz 	struct hci_conn_params *p;
4071d0b13706SLuiz Augusto von Dentz 
4072d0b13706SLuiz Augusto von Dentz 	list_for_each_entry(p, &hdev->le_conn_params, list) {
4073d0b13706SLuiz Augusto von Dentz 		if (p->conn) {
4074d0b13706SLuiz Augusto von Dentz 			hci_conn_drop(p->conn);
4075d0b13706SLuiz Augusto von Dentz 			hci_conn_put(p->conn);
4076d0b13706SLuiz Augusto von Dentz 			p->conn = NULL;
4077d0b13706SLuiz Augusto von Dentz 		}
4078d0b13706SLuiz Augusto von Dentz 		list_del_init(&p->action);
4079d0b13706SLuiz Augusto von Dentz 	}
4080d0b13706SLuiz Augusto von Dentz 
4081d0b13706SLuiz Augusto von Dentz 	BT_DBG("All LE pending actions cleared");
4082d0b13706SLuiz Augusto von Dentz }
4083d0b13706SLuiz Augusto von Dentz 
4084d0b13706SLuiz Augusto von Dentz int hci_dev_close_sync(struct hci_dev *hdev)
4085d0b13706SLuiz Augusto von Dentz {
4086d0b13706SLuiz Augusto von Dentz 	bool auto_off;
4087d0b13706SLuiz Augusto von Dentz 	int err = 0;
4088d0b13706SLuiz Augusto von Dentz 
4089d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
4090d0b13706SLuiz Augusto von Dentz 
4091ff7f2926SVasyl Vavrychuk 	cancel_work_sync(&hdev->power_on);
4092d0b13706SLuiz Augusto von Dentz 	cancel_delayed_work(&hdev->power_off);
4093d0b13706SLuiz Augusto von Dentz 	cancel_delayed_work(&hdev->ncmd_timer);
4094d0b13706SLuiz Augusto von Dentz 
4095d0b13706SLuiz Augusto von Dentz 	hci_request_cancel_all(hdev);
4096d0b13706SLuiz Augusto von Dentz 
4097d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4098d0b13706SLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4099d0b13706SLuiz Augusto von Dentz 	    test_bit(HCI_UP, &hdev->flags)) {
4100d0b13706SLuiz Augusto von Dentz 		/* Execute vendor specific shutdown routine */
4101d0b13706SLuiz Augusto von Dentz 		if (hdev->shutdown)
4102d0b13706SLuiz Augusto von Dentz 			err = hdev->shutdown(hdev);
4103d0b13706SLuiz Augusto von Dentz 	}
4104d0b13706SLuiz Augusto von Dentz 
4105d0b13706SLuiz Augusto von Dentz 	if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4106d0b13706SLuiz Augusto von Dentz 		cancel_delayed_work_sync(&hdev->cmd_timer);
4107d0b13706SLuiz Augusto von Dentz 		return err;
4108d0b13706SLuiz Augusto von Dentz 	}
4109d0b13706SLuiz Augusto von Dentz 
4110d0b13706SLuiz Augusto von Dentz 	hci_leds_update_powered(hdev, false);
4111d0b13706SLuiz Augusto von Dentz 
4112d0b13706SLuiz Augusto von Dentz 	/* Flush RX and TX works */
4113d0b13706SLuiz Augusto von Dentz 	flush_work(&hdev->tx_work);
4114d0b13706SLuiz Augusto von Dentz 	flush_work(&hdev->rx_work);
4115d0b13706SLuiz Augusto von Dentz 
4116d0b13706SLuiz Augusto von Dentz 	if (hdev->discov_timeout > 0) {
4117d0b13706SLuiz Augusto von Dentz 		hdev->discov_timeout = 0;
4118d0b13706SLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4119d0b13706SLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
4120d0b13706SLuiz Augusto von Dentz 	}
4121d0b13706SLuiz Augusto von Dentz 
4122d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
4123d0b13706SLuiz Augusto von Dentz 		cancel_delayed_work(&hdev->service_cache);
4124d0b13706SLuiz Augusto von Dentz 
4125d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4126d0b13706SLuiz Augusto von Dentz 		struct adv_info *adv_instance;
4127d0b13706SLuiz Augusto von Dentz 
4128d0b13706SLuiz Augusto von Dentz 		cancel_delayed_work_sync(&hdev->rpa_expired);
4129d0b13706SLuiz Augusto von Dentz 
4130d0b13706SLuiz Augusto von Dentz 		list_for_each_entry(adv_instance, &hdev->adv_instances, list)
4131d0b13706SLuiz Augusto von Dentz 			cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
4132d0b13706SLuiz Augusto von Dentz 	}
4133d0b13706SLuiz Augusto von Dentz 
4134d0b13706SLuiz Augusto von Dentz 	/* Avoid potential lockdep warnings from the *_flush() calls by
4135d0b13706SLuiz Augusto von Dentz 	 * ensuring the workqueue is empty up front.
4136d0b13706SLuiz Augusto von Dentz 	 */
4137d0b13706SLuiz Augusto von Dentz 	drain_workqueue(hdev->workqueue);
4138d0b13706SLuiz Augusto von Dentz 
4139d0b13706SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
4140d0b13706SLuiz Augusto von Dentz 
4141d0b13706SLuiz Augusto von Dentz 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4142d0b13706SLuiz Augusto von Dentz 
4143d0b13706SLuiz Augusto von Dentz 	auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
4144d0b13706SLuiz Augusto von Dentz 
4145d0b13706SLuiz Augusto von Dentz 	if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
4146d0b13706SLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4147d0b13706SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_MGMT))
4148d0b13706SLuiz Augusto von Dentz 		__mgmt_power_off(hdev);
4149d0b13706SLuiz Augusto von Dentz 
4150d0b13706SLuiz Augusto von Dentz 	hci_inquiry_cache_flush(hdev);
4151d0b13706SLuiz Augusto von Dentz 	hci_pend_le_actions_clear(hdev);
4152d0b13706SLuiz Augusto von Dentz 	hci_conn_hash_flush(hdev);
4153fa78d2d1SLin Ma 	/* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
4154d0b13706SLuiz Augusto von Dentz 	smp_unregister(hdev);
4155fa78d2d1SLin Ma 	hci_dev_unlock(hdev);
4156d0b13706SLuiz Augusto von Dentz 
4157d0b13706SLuiz Augusto von Dentz 	hci_sock_dev_event(hdev, HCI_DEV_DOWN);
4158d0b13706SLuiz Augusto von Dentz 
4159385315deSJesse Melhuish 	if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4160d0b13706SLuiz Augusto von Dentz 		aosp_do_close(hdev);
4161d0b13706SLuiz Augusto von Dentz 		msft_do_close(hdev);
4162385315deSJesse Melhuish 	}
4163d0b13706SLuiz Augusto von Dentz 
4164d0b13706SLuiz Augusto von Dentz 	if (hdev->flush)
4165d0b13706SLuiz Augusto von Dentz 		hdev->flush(hdev);
4166d0b13706SLuiz Augusto von Dentz 
4167d0b13706SLuiz Augusto von Dentz 	/* Reset device */
4168d0b13706SLuiz Augusto von Dentz 	skb_queue_purge(&hdev->cmd_q);
4169d0b13706SLuiz Augusto von Dentz 	atomic_set(&hdev->cmd_cnt, 1);
4170d0b13706SLuiz Augusto von Dentz 	if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
4171d0b13706SLuiz Augusto von Dentz 	    !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4172d0b13706SLuiz Augusto von Dentz 		set_bit(HCI_INIT, &hdev->flags);
4173d0b13706SLuiz Augusto von Dentz 		hci_reset_sync(hdev);
4174d0b13706SLuiz Augusto von Dentz 		clear_bit(HCI_INIT, &hdev->flags);
4175d0b13706SLuiz Augusto von Dentz 	}
4176d0b13706SLuiz Augusto von Dentz 
4177d0b13706SLuiz Augusto von Dentz 	/* flush cmd  work */
4178d0b13706SLuiz Augusto von Dentz 	flush_work(&hdev->cmd_work);
4179d0b13706SLuiz Augusto von Dentz 
4180d0b13706SLuiz Augusto von Dentz 	/* Drop queues */
4181d0b13706SLuiz Augusto von Dentz 	skb_queue_purge(&hdev->rx_q);
4182d0b13706SLuiz Augusto von Dentz 	skb_queue_purge(&hdev->cmd_q);
4183d0b13706SLuiz Augusto von Dentz 	skb_queue_purge(&hdev->raw_q);
4184d0b13706SLuiz Augusto von Dentz 
4185d0b13706SLuiz Augusto von Dentz 	/* Drop last sent command */
4186d0b13706SLuiz Augusto von Dentz 	if (hdev->sent_cmd) {
4187d0b13706SLuiz Augusto von Dentz 		cancel_delayed_work_sync(&hdev->cmd_timer);
4188d0b13706SLuiz Augusto von Dentz 		kfree_skb(hdev->sent_cmd);
4189d0b13706SLuiz Augusto von Dentz 		hdev->sent_cmd = NULL;
4190d0b13706SLuiz Augusto von Dentz 	}
4191d0b13706SLuiz Augusto von Dentz 
4192d0b13706SLuiz Augusto von Dentz 	clear_bit(HCI_RUNNING, &hdev->flags);
4193d0b13706SLuiz Augusto von Dentz 	hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4194d0b13706SLuiz Augusto von Dentz 
4195d0b13706SLuiz Augusto von Dentz 	/* After this point our queues are empty and no tasks are scheduled. */
4196d0b13706SLuiz Augusto von Dentz 	hdev->close(hdev);
4197d0b13706SLuiz Augusto von Dentz 
4198d0b13706SLuiz Augusto von Dentz 	/* Clear flags */
4199d0b13706SLuiz Augusto von Dentz 	hdev->flags &= BIT(HCI_RAW);
4200d0b13706SLuiz Augusto von Dentz 	hci_dev_clear_volatile_flags(hdev);
4201d0b13706SLuiz Augusto von Dentz 
4202d0b13706SLuiz Augusto von Dentz 	/* Controller radio is available but is currently powered down */
4203d0b13706SLuiz Augusto von Dentz 	hdev->amp_status = AMP_STATUS_POWERED_DOWN;
4204d0b13706SLuiz Augusto von Dentz 
4205d0b13706SLuiz Augusto von Dentz 	memset(hdev->eir, 0, sizeof(hdev->eir));
4206d0b13706SLuiz Augusto von Dentz 	memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
4207d0b13706SLuiz Augusto von Dentz 	bacpy(&hdev->random_addr, BDADDR_ANY);
4208d0b13706SLuiz Augusto von Dentz 
4209d0b13706SLuiz Augusto von Dentz 	hci_dev_put(hdev);
4210d0b13706SLuiz Augusto von Dentz 	return err;
4211d0b13706SLuiz Augusto von Dentz }
4212d0b13706SLuiz Augusto von Dentz 
4213cf75ad8bSLuiz Augusto von Dentz /* This function perform power on HCI command sequence as follows:
4214cf75ad8bSLuiz Augusto von Dentz  *
4215cf75ad8bSLuiz Augusto von Dentz  * If controller is already up (HCI_UP) performs hci_powered_update_sync
4216cf75ad8bSLuiz Augusto von Dentz  * sequence otherwise run hci_dev_open_sync which will follow with
4217cf75ad8bSLuiz Augusto von Dentz  * hci_powered_update_sync after the init sequence is completed.
4218cf75ad8bSLuiz Augusto von Dentz  */
4219cf75ad8bSLuiz Augusto von Dentz static int hci_power_on_sync(struct hci_dev *hdev)
4220cf75ad8bSLuiz Augusto von Dentz {
4221cf75ad8bSLuiz Augusto von Dentz 	int err;
4222cf75ad8bSLuiz Augusto von Dentz 
4223cf75ad8bSLuiz Augusto von Dentz 	if (test_bit(HCI_UP, &hdev->flags) &&
4224cf75ad8bSLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_MGMT) &&
4225cf75ad8bSLuiz Augusto von Dentz 	    hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
4226cf75ad8bSLuiz Augusto von Dentz 		cancel_delayed_work(&hdev->power_off);
4227cf75ad8bSLuiz Augusto von Dentz 		return hci_powered_update_sync(hdev);
4228cf75ad8bSLuiz Augusto von Dentz 	}
4229cf75ad8bSLuiz Augusto von Dentz 
4230cf75ad8bSLuiz Augusto von Dentz 	err = hci_dev_open_sync(hdev);
4231cf75ad8bSLuiz Augusto von Dentz 	if (err < 0)
4232cf75ad8bSLuiz Augusto von Dentz 		return err;
4233cf75ad8bSLuiz Augusto von Dentz 
4234cf75ad8bSLuiz Augusto von Dentz 	/* During the HCI setup phase, a few error conditions are
4235cf75ad8bSLuiz Augusto von Dentz 	 * ignored and they need to be checked now. If they are still
4236cf75ad8bSLuiz Augusto von Dentz 	 * valid, it is important to return the device back off.
4237cf75ad8bSLuiz Augusto von Dentz 	 */
4238cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
4239cf75ad8bSLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
4240cf75ad8bSLuiz Augusto von Dentz 	    (hdev->dev_type == HCI_PRIMARY &&
4241cf75ad8bSLuiz Augusto von Dentz 	     !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4242cf75ad8bSLuiz Augusto von Dentz 	     !bacmp(&hdev->static_addr, BDADDR_ANY))) {
4243cf75ad8bSLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
4244cf75ad8bSLuiz Augusto von Dentz 		hci_dev_close_sync(hdev);
4245cf75ad8bSLuiz Augusto von Dentz 	} else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
4246cf75ad8bSLuiz Augusto von Dentz 		queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
4247cf75ad8bSLuiz Augusto von Dentz 				   HCI_AUTO_OFF_TIMEOUT);
4248cf75ad8bSLuiz Augusto von Dentz 	}
4249cf75ad8bSLuiz Augusto von Dentz 
4250cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
4251cf75ad8bSLuiz Augusto von Dentz 		/* For unconfigured devices, set the HCI_RAW flag
4252cf75ad8bSLuiz Augusto von Dentz 		 * so that userspace can easily identify them.
4253cf75ad8bSLuiz Augusto von Dentz 		 */
4254cf75ad8bSLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4255cf75ad8bSLuiz Augusto von Dentz 			set_bit(HCI_RAW, &hdev->flags);
4256cf75ad8bSLuiz Augusto von Dentz 
4257cf75ad8bSLuiz Augusto von Dentz 		/* For fully configured devices, this will send
4258cf75ad8bSLuiz Augusto von Dentz 		 * the Index Added event. For unconfigured devices,
4259cf75ad8bSLuiz Augusto von Dentz 		 * it will send Unconfigued Index Added event.
4260cf75ad8bSLuiz Augusto von Dentz 		 *
4261cf75ad8bSLuiz Augusto von Dentz 		 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
4262cf75ad8bSLuiz Augusto von Dentz 		 * and no event will be send.
4263cf75ad8bSLuiz Augusto von Dentz 		 */
4264cf75ad8bSLuiz Augusto von Dentz 		mgmt_index_added(hdev);
4265cf75ad8bSLuiz Augusto von Dentz 	} else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
4266cf75ad8bSLuiz Augusto von Dentz 		/* When the controller is now configured, then it
4267cf75ad8bSLuiz Augusto von Dentz 		 * is important to clear the HCI_RAW flag.
4268cf75ad8bSLuiz Augusto von Dentz 		 */
4269cf75ad8bSLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4270cf75ad8bSLuiz Augusto von Dentz 			clear_bit(HCI_RAW, &hdev->flags);
4271cf75ad8bSLuiz Augusto von Dentz 
4272cf75ad8bSLuiz Augusto von Dentz 		/* Powering on the controller with HCI_CONFIG set only
4273cf75ad8bSLuiz Augusto von Dentz 		 * happens with the transition from unconfigured to
4274cf75ad8bSLuiz Augusto von Dentz 		 * configured. This will send the Index Added event.
4275cf75ad8bSLuiz Augusto von Dentz 		 */
4276cf75ad8bSLuiz Augusto von Dentz 		mgmt_index_added(hdev);
4277cf75ad8bSLuiz Augusto von Dentz 	}
4278cf75ad8bSLuiz Augusto von Dentz 
4279cf75ad8bSLuiz Augusto von Dentz 	return 0;
4280cf75ad8bSLuiz Augusto von Dentz }
4281cf75ad8bSLuiz Augusto von Dentz 
4282cf75ad8bSLuiz Augusto von Dentz static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
4283cf75ad8bSLuiz Augusto von Dentz {
4284cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_remote_name_req_cancel cp;
4285cf75ad8bSLuiz Augusto von Dentz 
4286cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4287cf75ad8bSLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, addr);
4288cf75ad8bSLuiz Augusto von Dentz 
4289cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
4290cf75ad8bSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4291cf75ad8bSLuiz Augusto von Dentz }
4292cf75ad8bSLuiz Augusto von Dentz 
4293abfeea47SLuiz Augusto von Dentz int hci_stop_discovery_sync(struct hci_dev *hdev)
4294cf75ad8bSLuiz Augusto von Dentz {
4295cf75ad8bSLuiz Augusto von Dentz 	struct discovery_state *d = &hdev->discovery;
4296cf75ad8bSLuiz Augusto von Dentz 	struct inquiry_entry *e;
4297cf75ad8bSLuiz Augusto von Dentz 	int err;
4298cf75ad8bSLuiz Augusto von Dentz 
4299cf75ad8bSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
4300cf75ad8bSLuiz Augusto von Dentz 
4301cf75ad8bSLuiz Augusto von Dentz 	if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
4302cf75ad8bSLuiz Augusto von Dentz 		if (test_bit(HCI_INQUIRY, &hdev->flags)) {
4303cf75ad8bSLuiz Augusto von Dentz 			err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
4304cf75ad8bSLuiz Augusto von Dentz 						    0, NULL, HCI_CMD_TIMEOUT);
4305cf75ad8bSLuiz Augusto von Dentz 			if (err)
4306cf75ad8bSLuiz Augusto von Dentz 				return err;
4307cf75ad8bSLuiz Augusto von Dentz 		}
4308cf75ad8bSLuiz Augusto von Dentz 
4309cf75ad8bSLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
4310cf75ad8bSLuiz Augusto von Dentz 			cancel_delayed_work(&hdev->le_scan_disable);
4311cf75ad8bSLuiz Augusto von Dentz 			cancel_delayed_work(&hdev->le_scan_restart);
4312cf75ad8bSLuiz Augusto von Dentz 
4313cf75ad8bSLuiz Augusto von Dentz 			err = hci_scan_disable_sync(hdev);
4314cf75ad8bSLuiz Augusto von Dentz 			if (err)
4315cf75ad8bSLuiz Augusto von Dentz 				return err;
4316cf75ad8bSLuiz Augusto von Dentz 		}
4317cf75ad8bSLuiz Augusto von Dentz 
4318cf75ad8bSLuiz Augusto von Dentz 	} else {
4319cf75ad8bSLuiz Augusto von Dentz 		err = hci_scan_disable_sync(hdev);
4320cf75ad8bSLuiz Augusto von Dentz 		if (err)
4321cf75ad8bSLuiz Augusto von Dentz 			return err;
4322cf75ad8bSLuiz Augusto von Dentz 	}
4323cf75ad8bSLuiz Augusto von Dentz 
4324abfeea47SLuiz Augusto von Dentz 	/* Resume advertising if it was paused */
4325abfeea47SLuiz Augusto von Dentz 	if (use_ll_privacy(hdev))
4326abfeea47SLuiz Augusto von Dentz 		hci_resume_advertising_sync(hdev);
4327abfeea47SLuiz Augusto von Dentz 
4328cf75ad8bSLuiz Augusto von Dentz 	/* No further actions needed for LE-only discovery */
4329cf75ad8bSLuiz Augusto von Dentz 	if (d->type == DISCOV_TYPE_LE)
4330cf75ad8bSLuiz Augusto von Dentz 		return 0;
4331cf75ad8bSLuiz Augusto von Dentz 
4332cf75ad8bSLuiz Augusto von Dentz 	if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
4333cf75ad8bSLuiz Augusto von Dentz 		e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
4334cf75ad8bSLuiz Augusto von Dentz 						     NAME_PENDING);
4335cf75ad8bSLuiz Augusto von Dentz 		if (!e)
4336cf75ad8bSLuiz Augusto von Dentz 			return 0;
4337cf75ad8bSLuiz Augusto von Dentz 
4338cf75ad8bSLuiz Augusto von Dentz 		return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
4339cf75ad8bSLuiz Augusto von Dentz 	}
4340cf75ad8bSLuiz Augusto von Dentz 
4341cf75ad8bSLuiz Augusto von Dentz 	return 0;
4342cf75ad8bSLuiz Augusto von Dentz }
4343cf75ad8bSLuiz Augusto von Dentz 
4344cf75ad8bSLuiz Augusto von Dentz static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
4345cf75ad8bSLuiz Augusto von Dentz 					u8 reason)
4346cf75ad8bSLuiz Augusto von Dentz {
4347cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_disconn_phy_link cp;
4348cf75ad8bSLuiz Augusto von Dentz 
4349cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4350cf75ad8bSLuiz Augusto von Dentz 	cp.phy_handle = HCI_PHY_HANDLE(handle);
4351cf75ad8bSLuiz Augusto von Dentz 	cp.reason = reason;
4352cf75ad8bSLuiz Augusto von Dentz 
4353cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
4354cf75ad8bSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4355cf75ad8bSLuiz Augusto von Dentz }
4356cf75ad8bSLuiz Augusto von Dentz 
4357cf75ad8bSLuiz Augusto von Dentz static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
4358cf75ad8bSLuiz Augusto von Dentz 			       u8 reason)
4359cf75ad8bSLuiz Augusto von Dentz {
4360cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_disconnect cp;
4361cf75ad8bSLuiz Augusto von Dentz 
4362cf75ad8bSLuiz Augusto von Dentz 	if (conn->type == AMP_LINK)
4363cf75ad8bSLuiz Augusto von Dentz 		return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
4364cf75ad8bSLuiz Augusto von Dentz 
4365cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4366cf75ad8bSLuiz Augusto von Dentz 	cp.handle = cpu_to_le16(conn->handle);
4367cf75ad8bSLuiz Augusto von Dentz 	cp.reason = reason;
4368cf75ad8bSLuiz Augusto von Dentz 
4369d0b13706SLuiz Augusto von Dentz 	/* Wait for HCI_EV_DISCONN_COMPLETE not HCI_EV_CMD_STATUS when not
4370d0b13706SLuiz Augusto von Dentz 	 * suspending.
4371d0b13706SLuiz Augusto von Dentz 	 */
4372d0b13706SLuiz Augusto von Dentz 	if (!hdev->suspended)
4373d0b13706SLuiz Augusto von Dentz 		return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
4374d0b13706SLuiz Augusto von Dentz 						sizeof(cp), &cp,
4375d0b13706SLuiz Augusto von Dentz 						HCI_EV_DISCONN_COMPLETE,
4376d0b13706SLuiz Augusto von Dentz 						HCI_CMD_TIMEOUT, NULL);
4377d0b13706SLuiz Augusto von Dentz 
4378d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
4379d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
4380cf75ad8bSLuiz Augusto von Dentz }
4381cf75ad8bSLuiz Augusto von Dentz 
4382cf75ad8bSLuiz Augusto von Dentz static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
4383cf75ad8bSLuiz Augusto von Dentz 				      struct hci_conn *conn)
4384cf75ad8bSLuiz Augusto von Dentz {
4385cf75ad8bSLuiz Augusto von Dentz 	if (test_bit(HCI_CONN_SCANNING, &conn->flags))
4386cf75ad8bSLuiz Augusto von Dentz 		return 0;
4387cf75ad8bSLuiz Augusto von Dentz 
4388cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
4389cf75ad8bSLuiz Augusto von Dentz 				     6, &conn->dst, HCI_CMD_TIMEOUT);
4390cf75ad8bSLuiz Augusto von Dentz }
4391cf75ad8bSLuiz Augusto von Dentz 
4392cf75ad8bSLuiz Augusto von Dentz static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn)
4393cf75ad8bSLuiz Augusto von Dentz {
4394cf75ad8bSLuiz Augusto von Dentz 	if (conn->type == LE_LINK)
4395cf75ad8bSLuiz Augusto von Dentz 		return hci_le_connect_cancel_sync(hdev, conn);
4396cf75ad8bSLuiz Augusto von Dentz 
4397cf75ad8bSLuiz Augusto von Dentz 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
4398cf75ad8bSLuiz Augusto von Dentz 		return 0;
4399cf75ad8bSLuiz Augusto von Dentz 
4400cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
4401cf75ad8bSLuiz Augusto von Dentz 				     6, &conn->dst, HCI_CMD_TIMEOUT);
4402cf75ad8bSLuiz Augusto von Dentz }
4403cf75ad8bSLuiz Augusto von Dentz 
4404cf75ad8bSLuiz Augusto von Dentz static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
4405cf75ad8bSLuiz Augusto von Dentz 			       u8 reason)
4406cf75ad8bSLuiz Augusto von Dentz {
4407cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_reject_sync_conn_req cp;
4408cf75ad8bSLuiz Augusto von Dentz 
4409cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4410cf75ad8bSLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, &conn->dst);
4411cf75ad8bSLuiz Augusto von Dentz 	cp.reason = reason;
4412cf75ad8bSLuiz Augusto von Dentz 
4413cf75ad8bSLuiz Augusto von Dentz 	/* SCO rejection has its own limited set of
4414cf75ad8bSLuiz Augusto von Dentz 	 * allowed error values (0x0D-0x0F).
4415cf75ad8bSLuiz Augusto von Dentz 	 */
4416cf75ad8bSLuiz Augusto von Dentz 	if (reason < 0x0d || reason > 0x0f)
4417cf75ad8bSLuiz Augusto von Dentz 		cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
4418cf75ad8bSLuiz Augusto von Dentz 
4419cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
4420cf75ad8bSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4421cf75ad8bSLuiz Augusto von Dentz }
4422cf75ad8bSLuiz Augusto von Dentz 
4423cf75ad8bSLuiz Augusto von Dentz static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
4424cf75ad8bSLuiz Augusto von Dentz 				u8 reason)
4425cf75ad8bSLuiz Augusto von Dentz {
4426cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_reject_conn_req cp;
4427cf75ad8bSLuiz Augusto von Dentz 
4428cf75ad8bSLuiz Augusto von Dentz 	if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
4429cf75ad8bSLuiz Augusto von Dentz 		return hci_reject_sco_sync(hdev, conn, reason);
4430cf75ad8bSLuiz Augusto von Dentz 
4431cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4432cf75ad8bSLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, &conn->dst);
4433cf75ad8bSLuiz Augusto von Dentz 	cp.reason = reason;
4434cf75ad8bSLuiz Augusto von Dentz 
4435cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
4436cf75ad8bSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4437cf75ad8bSLuiz Augusto von Dentz }
4438cf75ad8bSLuiz Augusto von Dentz 
4439cf75ad8bSLuiz Augusto von Dentz static int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
4440cf75ad8bSLuiz Augusto von Dentz 			       u8 reason)
4441cf75ad8bSLuiz Augusto von Dentz {
44429b3628d7SLuiz Augusto von Dentz 	int err;
44439b3628d7SLuiz Augusto von Dentz 
4444cf75ad8bSLuiz Augusto von Dentz 	switch (conn->state) {
4445cf75ad8bSLuiz Augusto von Dentz 	case BT_CONNECTED:
4446cf75ad8bSLuiz Augusto von Dentz 	case BT_CONFIG:
4447cf75ad8bSLuiz Augusto von Dentz 		return hci_disconnect_sync(hdev, conn, reason);
4448cf75ad8bSLuiz Augusto von Dentz 	case BT_CONNECT:
44499b3628d7SLuiz Augusto von Dentz 		err = hci_connect_cancel_sync(hdev, conn);
44509b3628d7SLuiz Augusto von Dentz 		/* Cleanup hci_conn object if it cannot be cancelled as it
44519b3628d7SLuiz Augusto von Dentz 		 * likelly means the controller and host stack are out of sync.
44529b3628d7SLuiz Augusto von Dentz 		 */
44539b3628d7SLuiz Augusto von Dentz 		if (err)
44549b3628d7SLuiz Augusto von Dentz 			hci_conn_failed(conn, err);
44559b3628d7SLuiz Augusto von Dentz 
44569b3628d7SLuiz Augusto von Dentz 		return err;
4457cf75ad8bSLuiz Augusto von Dentz 	case BT_CONNECT2:
4458cf75ad8bSLuiz Augusto von Dentz 		return hci_reject_conn_sync(hdev, conn, reason);
4459cf75ad8bSLuiz Augusto von Dentz 	default:
4460cf75ad8bSLuiz Augusto von Dentz 		conn->state = BT_CLOSED;
4461cf75ad8bSLuiz Augusto von Dentz 		break;
4462cf75ad8bSLuiz Augusto von Dentz 	}
4463cf75ad8bSLuiz Augusto von Dentz 
4464cf75ad8bSLuiz Augusto von Dentz 	return 0;
4465cf75ad8bSLuiz Augusto von Dentz }
4466cf75ad8bSLuiz Augusto von Dentz 
4467182ee45dSLuiz Augusto von Dentz static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
4468182ee45dSLuiz Augusto von Dentz {
4469182ee45dSLuiz Augusto von Dentz 	struct hci_conn *conn, *tmp;
4470182ee45dSLuiz Augusto von Dentz 	int err;
4471182ee45dSLuiz Augusto von Dentz 
4472182ee45dSLuiz Augusto von Dentz 	list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
4473182ee45dSLuiz Augusto von Dentz 		err = hci_abort_conn_sync(hdev, conn, reason);
4474182ee45dSLuiz Augusto von Dentz 		if (err)
4475182ee45dSLuiz Augusto von Dentz 			return err;
4476182ee45dSLuiz Augusto von Dentz 	}
4477182ee45dSLuiz Augusto von Dentz 
44788cd3c55cSTom Rix 	return 0;
4479182ee45dSLuiz Augusto von Dentz }
4480182ee45dSLuiz Augusto von Dentz 
4481cf75ad8bSLuiz Augusto von Dentz /* This function perform power off HCI command sequence as follows:
4482cf75ad8bSLuiz Augusto von Dentz  *
4483cf75ad8bSLuiz Augusto von Dentz  * Clear Advertising
4484cf75ad8bSLuiz Augusto von Dentz  * Stop Discovery
4485cf75ad8bSLuiz Augusto von Dentz  * Disconnect all connections
4486cf75ad8bSLuiz Augusto von Dentz  * hci_dev_close_sync
4487cf75ad8bSLuiz Augusto von Dentz  */
4488cf75ad8bSLuiz Augusto von Dentz static int hci_power_off_sync(struct hci_dev *hdev)
4489cf75ad8bSLuiz Augusto von Dentz {
4490cf75ad8bSLuiz Augusto von Dentz 	int err;
4491cf75ad8bSLuiz Augusto von Dentz 
4492cf75ad8bSLuiz Augusto von Dentz 	/* If controller is already down there is nothing to do */
4493cf75ad8bSLuiz Augusto von Dentz 	if (!test_bit(HCI_UP, &hdev->flags))
4494cf75ad8bSLuiz Augusto von Dentz 		return 0;
4495cf75ad8bSLuiz Augusto von Dentz 
4496cf75ad8bSLuiz Augusto von Dentz 	if (test_bit(HCI_ISCAN, &hdev->flags) ||
4497cf75ad8bSLuiz Augusto von Dentz 	    test_bit(HCI_PSCAN, &hdev->flags)) {
4498cf75ad8bSLuiz Augusto von Dentz 		err = hci_write_scan_enable_sync(hdev, 0x00);
4499cf75ad8bSLuiz Augusto von Dentz 		if (err)
4500cf75ad8bSLuiz Augusto von Dentz 			return err;
4501cf75ad8bSLuiz Augusto von Dentz 	}
4502cf75ad8bSLuiz Augusto von Dentz 
4503cf75ad8bSLuiz Augusto von Dentz 	err = hci_clear_adv_sync(hdev, NULL, false);
4504cf75ad8bSLuiz Augusto von Dentz 	if (err)
4505cf75ad8bSLuiz Augusto von Dentz 		return err;
4506cf75ad8bSLuiz Augusto von Dentz 
4507cf75ad8bSLuiz Augusto von Dentz 	err = hci_stop_discovery_sync(hdev);
4508cf75ad8bSLuiz Augusto von Dentz 	if (err)
4509cf75ad8bSLuiz Augusto von Dentz 		return err;
4510cf75ad8bSLuiz Augusto von Dentz 
4511182ee45dSLuiz Augusto von Dentz 	/* Terminated due to Power Off */
4512182ee45dSLuiz Augusto von Dentz 	err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
4513182ee45dSLuiz Augusto von Dentz 	if (err)
4514182ee45dSLuiz Augusto von Dentz 		return err;
4515cf75ad8bSLuiz Augusto von Dentz 
4516cf75ad8bSLuiz Augusto von Dentz 	return hci_dev_close_sync(hdev);
4517cf75ad8bSLuiz Augusto von Dentz }
4518cf75ad8bSLuiz Augusto von Dentz 
4519cf75ad8bSLuiz Augusto von Dentz int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
4520cf75ad8bSLuiz Augusto von Dentz {
4521cf75ad8bSLuiz Augusto von Dentz 	if (val)
4522cf75ad8bSLuiz Augusto von Dentz 		return hci_power_on_sync(hdev);
4523cf75ad8bSLuiz Augusto von Dentz 
4524cf75ad8bSLuiz Augusto von Dentz 	return hci_power_off_sync(hdev);
4525cf75ad8bSLuiz Augusto von Dentz }
4526abfeea47SLuiz Augusto von Dentz 
45272bd1b237SLuiz Augusto von Dentz static int hci_write_iac_sync(struct hci_dev *hdev)
45282bd1b237SLuiz Augusto von Dentz {
45292bd1b237SLuiz Augusto von Dentz 	struct hci_cp_write_current_iac_lap cp;
45302bd1b237SLuiz Augusto von Dentz 
45312bd1b237SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
45322bd1b237SLuiz Augusto von Dentz 		return 0;
45332bd1b237SLuiz Augusto von Dentz 
45342bd1b237SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
45352bd1b237SLuiz Augusto von Dentz 
45362bd1b237SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
45372bd1b237SLuiz Augusto von Dentz 		/* Limited discoverable mode */
45382bd1b237SLuiz Augusto von Dentz 		cp.num_iac = min_t(u8, hdev->num_iac, 2);
45392bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[0] = 0x00;	/* LIAC */
45402bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[1] = 0x8b;
45412bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[2] = 0x9e;
45422bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[3] = 0x33;	/* GIAC */
45432bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[4] = 0x8b;
45442bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[5] = 0x9e;
45452bd1b237SLuiz Augusto von Dentz 	} else {
45462bd1b237SLuiz Augusto von Dentz 		/* General discoverable mode */
45472bd1b237SLuiz Augusto von Dentz 		cp.num_iac = 1;
45482bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[0] = 0x33;	/* GIAC */
45492bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[1] = 0x8b;
45502bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[2] = 0x9e;
45512bd1b237SLuiz Augusto von Dentz 	}
45522bd1b237SLuiz Augusto von Dentz 
45532bd1b237SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
45542bd1b237SLuiz Augusto von Dentz 				     (cp.num_iac * 3) + 1, &cp,
45552bd1b237SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
45562bd1b237SLuiz Augusto von Dentz }
45572bd1b237SLuiz Augusto von Dentz 
45582bd1b237SLuiz Augusto von Dentz int hci_update_discoverable_sync(struct hci_dev *hdev)
45592bd1b237SLuiz Augusto von Dentz {
45602bd1b237SLuiz Augusto von Dentz 	int err = 0;
45612bd1b237SLuiz Augusto von Dentz 
45622bd1b237SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
45632bd1b237SLuiz Augusto von Dentz 		err = hci_write_iac_sync(hdev);
45642bd1b237SLuiz Augusto von Dentz 		if (err)
45652bd1b237SLuiz Augusto von Dentz 			return err;
45662bd1b237SLuiz Augusto von Dentz 
45672bd1b237SLuiz Augusto von Dentz 		err = hci_update_scan_sync(hdev);
45682bd1b237SLuiz Augusto von Dentz 		if (err)
45692bd1b237SLuiz Augusto von Dentz 			return err;
45702bd1b237SLuiz Augusto von Dentz 
45712bd1b237SLuiz Augusto von Dentz 		err = hci_update_class_sync(hdev);
45722bd1b237SLuiz Augusto von Dentz 		if (err)
45732bd1b237SLuiz Augusto von Dentz 			return err;
45742bd1b237SLuiz Augusto von Dentz 	}
45752bd1b237SLuiz Augusto von Dentz 
45762bd1b237SLuiz Augusto von Dentz 	/* Advertising instances don't use the global discoverable setting, so
45772bd1b237SLuiz Augusto von Dentz 	 * only update AD if advertising was enabled using Set Advertising.
45782bd1b237SLuiz Augusto von Dentz 	 */
45792bd1b237SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
45802bd1b237SLuiz Augusto von Dentz 		err = hci_update_adv_data_sync(hdev, 0x00);
45812bd1b237SLuiz Augusto von Dentz 		if (err)
45822bd1b237SLuiz Augusto von Dentz 			return err;
45832bd1b237SLuiz Augusto von Dentz 
45842bd1b237SLuiz Augusto von Dentz 		/* Discoverable mode affects the local advertising
45852bd1b237SLuiz Augusto von Dentz 		 * address in limited privacy mode.
45862bd1b237SLuiz Augusto von Dentz 		 */
45872bd1b237SLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
45882bd1b237SLuiz Augusto von Dentz 			if (ext_adv_capable(hdev))
45892bd1b237SLuiz Augusto von Dentz 				err = hci_start_ext_adv_sync(hdev, 0x00);
45902bd1b237SLuiz Augusto von Dentz 			else
45912bd1b237SLuiz Augusto von Dentz 				err = hci_enable_advertising_sync(hdev);
45922bd1b237SLuiz Augusto von Dentz 		}
45932bd1b237SLuiz Augusto von Dentz 	}
45942bd1b237SLuiz Augusto von Dentz 
45952bd1b237SLuiz Augusto von Dentz 	return err;
45962bd1b237SLuiz Augusto von Dentz }
45972bd1b237SLuiz Augusto von Dentz 
45982bd1b237SLuiz Augusto von Dentz static int update_discoverable_sync(struct hci_dev *hdev, void *data)
45992bd1b237SLuiz Augusto von Dentz {
46002bd1b237SLuiz Augusto von Dentz 	return hci_update_discoverable_sync(hdev);
46012bd1b237SLuiz Augusto von Dentz }
46022bd1b237SLuiz Augusto von Dentz 
46032bd1b237SLuiz Augusto von Dentz int hci_update_discoverable(struct hci_dev *hdev)
46042bd1b237SLuiz Augusto von Dentz {
46052bd1b237SLuiz Augusto von Dentz 	/* Only queue if it would have any effect */
46062bd1b237SLuiz Augusto von Dentz 	if (hdev_is_powered(hdev) &&
46072bd1b237SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
46082bd1b237SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
46092bd1b237SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
46102bd1b237SLuiz Augusto von Dentz 		return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
46112bd1b237SLuiz Augusto von Dentz 					  NULL);
46122bd1b237SLuiz Augusto von Dentz 
46132bd1b237SLuiz Augusto von Dentz 	return 0;
46142bd1b237SLuiz Augusto von Dentz }
46152bd1b237SLuiz Augusto von Dentz 
4616f056a657SLuiz Augusto von Dentz int hci_update_connectable_sync(struct hci_dev *hdev)
4617f056a657SLuiz Augusto von Dentz {
4618f056a657SLuiz Augusto von Dentz 	int err;
4619f056a657SLuiz Augusto von Dentz 
4620f056a657SLuiz Augusto von Dentz 	err = hci_update_scan_sync(hdev);
4621f056a657SLuiz Augusto von Dentz 	if (err)
4622f056a657SLuiz Augusto von Dentz 		return err;
4623f056a657SLuiz Augusto von Dentz 
4624f056a657SLuiz Augusto von Dentz 	/* If BR/EDR is not enabled and we disable advertising as a
4625f056a657SLuiz Augusto von Dentz 	 * by-product of disabling connectable, we need to update the
4626f056a657SLuiz Augusto von Dentz 	 * advertising flags.
4627f056a657SLuiz Augusto von Dentz 	 */
4628f056a657SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4629f056a657SLuiz Augusto von Dentz 		err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
4630f056a657SLuiz Augusto von Dentz 
4631f056a657SLuiz Augusto von Dentz 	/* Update the advertising parameters if necessary */
4632f056a657SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4633f056a657SLuiz Augusto von Dentz 	    !list_empty(&hdev->adv_instances)) {
4634f056a657SLuiz Augusto von Dentz 		if (ext_adv_capable(hdev))
4635f056a657SLuiz Augusto von Dentz 			err = hci_start_ext_adv_sync(hdev,
4636f056a657SLuiz Augusto von Dentz 						     hdev->cur_adv_instance);
4637f056a657SLuiz Augusto von Dentz 		else
4638f056a657SLuiz Augusto von Dentz 			err = hci_enable_advertising_sync(hdev);
4639f056a657SLuiz Augusto von Dentz 
4640f056a657SLuiz Augusto von Dentz 		if (err)
4641f056a657SLuiz Augusto von Dentz 			return err;
4642f056a657SLuiz Augusto von Dentz 	}
4643f056a657SLuiz Augusto von Dentz 
4644f056a657SLuiz Augusto von Dentz 	return hci_update_passive_scan_sync(hdev);
4645f056a657SLuiz Augusto von Dentz }
4646f056a657SLuiz Augusto von Dentz 
4647abfeea47SLuiz Augusto von Dentz static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
4648abfeea47SLuiz Augusto von Dentz {
4649abfeea47SLuiz Augusto von Dentz 	const u8 giac[3] = { 0x33, 0x8b, 0x9e };
4650abfeea47SLuiz Augusto von Dentz 	const u8 liac[3] = { 0x00, 0x8b, 0x9e };
4651abfeea47SLuiz Augusto von Dentz 	struct hci_cp_inquiry cp;
4652abfeea47SLuiz Augusto von Dentz 
4653abfeea47SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
4654abfeea47SLuiz Augusto von Dentz 
4655abfeea47SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_INQUIRY))
4656abfeea47SLuiz Augusto von Dentz 		return 0;
4657abfeea47SLuiz Augusto von Dentz 
4658abfeea47SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
4659abfeea47SLuiz Augusto von Dentz 	hci_inquiry_cache_flush(hdev);
4660abfeea47SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
4661abfeea47SLuiz Augusto von Dentz 
4662abfeea47SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4663abfeea47SLuiz Augusto von Dentz 
4664abfeea47SLuiz Augusto von Dentz 	if (hdev->discovery.limited)
4665abfeea47SLuiz Augusto von Dentz 		memcpy(&cp.lap, liac, sizeof(cp.lap));
4666abfeea47SLuiz Augusto von Dentz 	else
4667abfeea47SLuiz Augusto von Dentz 		memcpy(&cp.lap, giac, sizeof(cp.lap));
4668abfeea47SLuiz Augusto von Dentz 
4669abfeea47SLuiz Augusto von Dentz 	cp.length = length;
4670abfeea47SLuiz Augusto von Dentz 
4671abfeea47SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
4672abfeea47SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4673abfeea47SLuiz Augusto von Dentz }
4674abfeea47SLuiz Augusto von Dentz 
4675abfeea47SLuiz Augusto von Dentz static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
4676abfeea47SLuiz Augusto von Dentz {
4677abfeea47SLuiz Augusto von Dentz 	u8 own_addr_type;
4678abfeea47SLuiz Augusto von Dentz 	/* Accept list is not used for discovery */
4679abfeea47SLuiz Augusto von Dentz 	u8 filter_policy = 0x00;
4680abfeea47SLuiz Augusto von Dentz 	/* Default is to enable duplicates filter */
4681abfeea47SLuiz Augusto von Dentz 	u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
4682abfeea47SLuiz Augusto von Dentz 	int err;
4683abfeea47SLuiz Augusto von Dentz 
4684abfeea47SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
4685abfeea47SLuiz Augusto von Dentz 
4686abfeea47SLuiz Augusto von Dentz 	/* If controller is scanning, it means the passive scanning is
4687abfeea47SLuiz Augusto von Dentz 	 * running. Thus, we should temporarily stop it in order to set the
4688abfeea47SLuiz Augusto von Dentz 	 * discovery scanning parameters.
4689abfeea47SLuiz Augusto von Dentz 	 */
4690abfeea47SLuiz Augusto von Dentz 	err = hci_scan_disable_sync(hdev);
4691abfeea47SLuiz Augusto von Dentz 	if (err) {
4692abfeea47SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to disable scanning: %d", err);
4693abfeea47SLuiz Augusto von Dentz 		return err;
4694abfeea47SLuiz Augusto von Dentz 	}
4695abfeea47SLuiz Augusto von Dentz 
4696abfeea47SLuiz Augusto von Dentz 	cancel_interleave_scan(hdev);
4697abfeea47SLuiz Augusto von Dentz 
4698abfeea47SLuiz Augusto von Dentz 	/* Pause advertising since active scanning disables address resolution
4699abfeea47SLuiz Augusto von Dentz 	 * which advertising depend on in order to generate its RPAs.
4700abfeea47SLuiz Augusto von Dentz 	 */
4701abfeea47SLuiz Augusto von Dentz 	if (use_ll_privacy(hdev)) {
4702abfeea47SLuiz Augusto von Dentz 		err = hci_pause_advertising_sync(hdev);
4703abfeea47SLuiz Augusto von Dentz 		if (err) {
4704abfeea47SLuiz Augusto von Dentz 			bt_dev_err(hdev, "pause advertising failed: %d", err);
4705abfeea47SLuiz Augusto von Dentz 			goto failed;
4706abfeea47SLuiz Augusto von Dentz 		}
4707abfeea47SLuiz Augusto von Dentz 	}
4708abfeea47SLuiz Augusto von Dentz 
4709abfeea47SLuiz Augusto von Dentz 	/* Disable address resolution while doing active scanning since the
4710abfeea47SLuiz Augusto von Dentz 	 * accept list shall not be used and all reports shall reach the host
4711abfeea47SLuiz Augusto von Dentz 	 * anyway.
4712abfeea47SLuiz Augusto von Dentz 	 */
4713abfeea47SLuiz Augusto von Dentz 	err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
4714abfeea47SLuiz Augusto von Dentz 	if (err) {
4715abfeea47SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
4716abfeea47SLuiz Augusto von Dentz 			   err);
4717abfeea47SLuiz Augusto von Dentz 		goto failed;
4718abfeea47SLuiz Augusto von Dentz 	}
4719abfeea47SLuiz Augusto von Dentz 
4720abfeea47SLuiz Augusto von Dentz 	/* All active scans will be done with either a resolvable private
4721abfeea47SLuiz Augusto von Dentz 	 * address (when privacy feature has been enabled) or non-resolvable
4722abfeea47SLuiz Augusto von Dentz 	 * private address.
4723abfeea47SLuiz Augusto von Dentz 	 */
4724abfeea47SLuiz Augusto von Dentz 	err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
4725abfeea47SLuiz Augusto von Dentz 					     &own_addr_type);
4726abfeea47SLuiz Augusto von Dentz 	if (err < 0)
4727abfeea47SLuiz Augusto von Dentz 		own_addr_type = ADDR_LE_DEV_PUBLIC;
4728abfeea47SLuiz Augusto von Dentz 
4729abfeea47SLuiz Augusto von Dentz 	if (hci_is_adv_monitoring(hdev)) {
4730abfeea47SLuiz Augusto von Dentz 		/* Duplicate filter should be disabled when some advertisement
4731abfeea47SLuiz Augusto von Dentz 		 * monitor is activated, otherwise AdvMon can only receive one
4732abfeea47SLuiz Augusto von Dentz 		 * advertisement for one peer(*) during active scanning, and
4733abfeea47SLuiz Augusto von Dentz 		 * might report loss to these peers.
4734abfeea47SLuiz Augusto von Dentz 		 *
4735abfeea47SLuiz Augusto von Dentz 		 * Note that different controllers have different meanings of
4736abfeea47SLuiz Augusto von Dentz 		 * |duplicate|. Some of them consider packets with the same
4737abfeea47SLuiz Augusto von Dentz 		 * address as duplicate, and others consider packets with the
4738abfeea47SLuiz Augusto von Dentz 		 * same address and the same RSSI as duplicate. Although in the
4739abfeea47SLuiz Augusto von Dentz 		 * latter case we don't need to disable duplicate filter, but
4740abfeea47SLuiz Augusto von Dentz 		 * it is common to have active scanning for a short period of
4741abfeea47SLuiz Augusto von Dentz 		 * time, the power impact should be neglectable.
4742abfeea47SLuiz Augusto von Dentz 		 */
4743abfeea47SLuiz Augusto von Dentz 		filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
4744abfeea47SLuiz Augusto von Dentz 	}
4745abfeea47SLuiz Augusto von Dentz 
4746abfeea47SLuiz Augusto von Dentz 	err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
4747abfeea47SLuiz Augusto von Dentz 				  hdev->le_scan_window_discovery,
4748abfeea47SLuiz Augusto von Dentz 				  own_addr_type, filter_policy, filter_dup);
4749abfeea47SLuiz Augusto von Dentz 	if (!err)
4750abfeea47SLuiz Augusto von Dentz 		return err;
4751abfeea47SLuiz Augusto von Dentz 
4752abfeea47SLuiz Augusto von Dentz failed:
4753abfeea47SLuiz Augusto von Dentz 	/* Resume advertising if it was paused */
4754abfeea47SLuiz Augusto von Dentz 	if (use_ll_privacy(hdev))
4755abfeea47SLuiz Augusto von Dentz 		hci_resume_advertising_sync(hdev);
4756abfeea47SLuiz Augusto von Dentz 
4757abfeea47SLuiz Augusto von Dentz 	/* Resume passive scanning */
4758abfeea47SLuiz Augusto von Dentz 	hci_update_passive_scan_sync(hdev);
4759abfeea47SLuiz Augusto von Dentz 	return err;
4760abfeea47SLuiz Augusto von Dentz }
4761abfeea47SLuiz Augusto von Dentz 
4762abfeea47SLuiz Augusto von Dentz static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
4763abfeea47SLuiz Augusto von Dentz {
4764abfeea47SLuiz Augusto von Dentz 	int err;
4765abfeea47SLuiz Augusto von Dentz 
4766abfeea47SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
4767abfeea47SLuiz Augusto von Dentz 
4768abfeea47SLuiz Augusto von Dentz 	err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
4769abfeea47SLuiz Augusto von Dentz 	if (err)
4770abfeea47SLuiz Augusto von Dentz 		return err;
4771abfeea47SLuiz Augusto von Dentz 
4772abfeea47SLuiz Augusto von Dentz 	return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
4773abfeea47SLuiz Augusto von Dentz }
4774abfeea47SLuiz Augusto von Dentz 
4775abfeea47SLuiz Augusto von Dentz int hci_start_discovery_sync(struct hci_dev *hdev)
4776abfeea47SLuiz Augusto von Dentz {
4777abfeea47SLuiz Augusto von Dentz 	unsigned long timeout;
4778abfeea47SLuiz Augusto von Dentz 	int err;
4779abfeea47SLuiz Augusto von Dentz 
4780abfeea47SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
4781abfeea47SLuiz Augusto von Dentz 
4782abfeea47SLuiz Augusto von Dentz 	switch (hdev->discovery.type) {
4783abfeea47SLuiz Augusto von Dentz 	case DISCOV_TYPE_BREDR:
4784abfeea47SLuiz Augusto von Dentz 		return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
4785abfeea47SLuiz Augusto von Dentz 	case DISCOV_TYPE_INTERLEAVED:
4786abfeea47SLuiz Augusto von Dentz 		/* When running simultaneous discovery, the LE scanning time
4787abfeea47SLuiz Augusto von Dentz 		 * should occupy the whole discovery time sine BR/EDR inquiry
4788abfeea47SLuiz Augusto von Dentz 		 * and LE scanning are scheduled by the controller.
4789abfeea47SLuiz Augusto von Dentz 		 *
4790abfeea47SLuiz Augusto von Dentz 		 * For interleaving discovery in comparison, BR/EDR inquiry
4791abfeea47SLuiz Augusto von Dentz 		 * and LE scanning are done sequentially with separate
4792abfeea47SLuiz Augusto von Dentz 		 * timeouts.
4793abfeea47SLuiz Augusto von Dentz 		 */
4794abfeea47SLuiz Augusto von Dentz 		if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
4795abfeea47SLuiz Augusto von Dentz 			     &hdev->quirks)) {
4796abfeea47SLuiz Augusto von Dentz 			timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4797abfeea47SLuiz Augusto von Dentz 			/* During simultaneous discovery, we double LE scan
4798abfeea47SLuiz Augusto von Dentz 			 * interval. We must leave some time for the controller
4799abfeea47SLuiz Augusto von Dentz 			 * to do BR/EDR inquiry.
4800abfeea47SLuiz Augusto von Dentz 			 */
4801abfeea47SLuiz Augusto von Dentz 			err = hci_start_interleaved_discovery_sync(hdev);
4802abfeea47SLuiz Augusto von Dentz 			break;
4803abfeea47SLuiz Augusto von Dentz 		}
4804abfeea47SLuiz Augusto von Dentz 
4805abfeea47SLuiz Augusto von Dentz 		timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
4806abfeea47SLuiz Augusto von Dentz 		err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
4807abfeea47SLuiz Augusto von Dentz 		break;
4808abfeea47SLuiz Augusto von Dentz 	case DISCOV_TYPE_LE:
4809abfeea47SLuiz Augusto von Dentz 		timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4810abfeea47SLuiz Augusto von Dentz 		err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
4811abfeea47SLuiz Augusto von Dentz 		break;
4812abfeea47SLuiz Augusto von Dentz 	default:
4813abfeea47SLuiz Augusto von Dentz 		return -EINVAL;
4814abfeea47SLuiz Augusto von Dentz 	}
4815abfeea47SLuiz Augusto von Dentz 
4816abfeea47SLuiz Augusto von Dentz 	if (err)
4817abfeea47SLuiz Augusto von Dentz 		return err;
4818abfeea47SLuiz Augusto von Dentz 
4819abfeea47SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
4820abfeea47SLuiz Augusto von Dentz 
4821abfeea47SLuiz Augusto von Dentz 	/* When service discovery is used and the controller has a
4822abfeea47SLuiz Augusto von Dentz 	 * strict duplicate filter, it is important to remember the
4823abfeea47SLuiz Augusto von Dentz 	 * start and duration of the scan. This is required for
4824abfeea47SLuiz Augusto von Dentz 	 * restarting scanning during the discovery phase.
4825abfeea47SLuiz Augusto von Dentz 	 */
4826abfeea47SLuiz Augusto von Dentz 	if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
4827abfeea47SLuiz Augusto von Dentz 	    hdev->discovery.result_filtering) {
4828abfeea47SLuiz Augusto von Dentz 		hdev->discovery.scan_start = jiffies;
4829abfeea47SLuiz Augusto von Dentz 		hdev->discovery.scan_duration = timeout;
4830abfeea47SLuiz Augusto von Dentz 	}
4831abfeea47SLuiz Augusto von Dentz 
4832abfeea47SLuiz Augusto von Dentz 	queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
4833abfeea47SLuiz Augusto von Dentz 			   timeout);
4834abfeea47SLuiz Augusto von Dentz 	return 0;
4835abfeea47SLuiz Augusto von Dentz }
4836182ee45dSLuiz Augusto von Dentz 
4837182ee45dSLuiz Augusto von Dentz static void hci_suspend_monitor_sync(struct hci_dev *hdev)
4838182ee45dSLuiz Augusto von Dentz {
4839182ee45dSLuiz Augusto von Dentz 	switch (hci_get_adv_monitor_offload_ext(hdev)) {
4840182ee45dSLuiz Augusto von Dentz 	case HCI_ADV_MONITOR_EXT_MSFT:
4841182ee45dSLuiz Augusto von Dentz 		msft_suspend_sync(hdev);
4842182ee45dSLuiz Augusto von Dentz 		break;
4843182ee45dSLuiz Augusto von Dentz 	default:
4844182ee45dSLuiz Augusto von Dentz 		return;
4845182ee45dSLuiz Augusto von Dentz 	}
4846182ee45dSLuiz Augusto von Dentz }
4847182ee45dSLuiz Augusto von Dentz 
4848182ee45dSLuiz Augusto von Dentz /* This function disables discovery and mark it as paused */
4849182ee45dSLuiz Augusto von Dentz static int hci_pause_discovery_sync(struct hci_dev *hdev)
4850182ee45dSLuiz Augusto von Dentz {
4851182ee45dSLuiz Augusto von Dentz 	int old_state = hdev->discovery.state;
4852182ee45dSLuiz Augusto von Dentz 	int err;
4853182ee45dSLuiz Augusto von Dentz 
4854182ee45dSLuiz Augusto von Dentz 	/* If discovery already stopped/stopping/paused there nothing to do */
4855182ee45dSLuiz Augusto von Dentz 	if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
4856182ee45dSLuiz Augusto von Dentz 	    hdev->discovery_paused)
4857182ee45dSLuiz Augusto von Dentz 		return 0;
4858182ee45dSLuiz Augusto von Dentz 
4859182ee45dSLuiz Augusto von Dentz 	hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
4860182ee45dSLuiz Augusto von Dentz 	err = hci_stop_discovery_sync(hdev);
4861182ee45dSLuiz Augusto von Dentz 	if (err)
4862182ee45dSLuiz Augusto von Dentz 		return err;
4863182ee45dSLuiz Augusto von Dentz 
4864182ee45dSLuiz Augusto von Dentz 	hdev->discovery_paused = true;
4865182ee45dSLuiz Augusto von Dentz 	hdev->discovery_old_state = old_state;
4866182ee45dSLuiz Augusto von Dentz 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4867182ee45dSLuiz Augusto von Dentz 
4868182ee45dSLuiz Augusto von Dentz 	return 0;
4869182ee45dSLuiz Augusto von Dentz }
4870182ee45dSLuiz Augusto von Dentz 
4871182ee45dSLuiz Augusto von Dentz static int hci_update_event_filter_sync(struct hci_dev *hdev)
4872182ee45dSLuiz Augusto von Dentz {
4873182ee45dSLuiz Augusto von Dentz 	struct bdaddr_list_with_flags *b;
4874182ee45dSLuiz Augusto von Dentz 	u8 scan = SCAN_DISABLED;
4875182ee45dSLuiz Augusto von Dentz 	bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
4876182ee45dSLuiz Augusto von Dentz 	int err;
4877182ee45dSLuiz Augusto von Dentz 
4878182ee45dSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4879182ee45dSLuiz Augusto von Dentz 		return 0;
4880182ee45dSLuiz Augusto von Dentz 
48810eaecfb2SIsmael Ferreras Morezuelas 	/* Some fake CSR controllers lock up after setting this type of
48820eaecfb2SIsmael Ferreras Morezuelas 	 * filter, so avoid sending the request altogether.
48830eaecfb2SIsmael Ferreras Morezuelas 	 */
48840eaecfb2SIsmael Ferreras Morezuelas 	if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
48850eaecfb2SIsmael Ferreras Morezuelas 		return 0;
48860eaecfb2SIsmael Ferreras Morezuelas 
4887182ee45dSLuiz Augusto von Dentz 	/* Always clear event filter when starting */
4888182ee45dSLuiz Augusto von Dentz 	hci_clear_event_filter_sync(hdev);
4889182ee45dSLuiz Augusto von Dentz 
4890182ee45dSLuiz Augusto von Dentz 	list_for_each_entry(b, &hdev->accept_list, list) {
4891fe92ee64SLuiz Augusto von Dentz 		if (!test_bit(HCI_CONN_FLAG_REMOTE_WAKEUP, b->flags))
4892182ee45dSLuiz Augusto von Dentz 			continue;
4893182ee45dSLuiz Augusto von Dentz 
4894182ee45dSLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
4895182ee45dSLuiz Augusto von Dentz 
4896182ee45dSLuiz Augusto von Dentz 		err =  hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
4897182ee45dSLuiz Augusto von Dentz 						 HCI_CONN_SETUP_ALLOW_BDADDR,
4898182ee45dSLuiz Augusto von Dentz 						 &b->bdaddr,
4899182ee45dSLuiz Augusto von Dentz 						 HCI_CONN_SETUP_AUTO_ON);
4900182ee45dSLuiz Augusto von Dentz 		if (err)
4901182ee45dSLuiz Augusto von Dentz 			bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
4902182ee45dSLuiz Augusto von Dentz 				   &b->bdaddr);
4903182ee45dSLuiz Augusto von Dentz 		else
4904182ee45dSLuiz Augusto von Dentz 			scan = SCAN_PAGE;
4905182ee45dSLuiz Augusto von Dentz 	}
4906182ee45dSLuiz Augusto von Dentz 
4907182ee45dSLuiz Augusto von Dentz 	if (scan && !scanning)
4908182ee45dSLuiz Augusto von Dentz 		hci_write_scan_enable_sync(hdev, scan);
4909182ee45dSLuiz Augusto von Dentz 	else if (!scan && scanning)
4910182ee45dSLuiz Augusto von Dentz 		hci_write_scan_enable_sync(hdev, scan);
4911182ee45dSLuiz Augusto von Dentz 
4912182ee45dSLuiz Augusto von Dentz 	return 0;
4913182ee45dSLuiz Augusto von Dentz }
4914182ee45dSLuiz Augusto von Dentz 
4915*3b420553SLuiz Augusto von Dentz /* This function disables scan (BR and LE) and mark it as paused */
4916*3b420553SLuiz Augusto von Dentz static int hci_pause_scan_sync(struct hci_dev *hdev)
4917*3b420553SLuiz Augusto von Dentz {
4918*3b420553SLuiz Augusto von Dentz 	if (hdev->scanning_paused)
4919*3b420553SLuiz Augusto von Dentz 		return 0;
4920*3b420553SLuiz Augusto von Dentz 
4921*3b420553SLuiz Augusto von Dentz 	/* Disable page scan if enabled */
4922*3b420553SLuiz Augusto von Dentz 	if (test_bit(HCI_PSCAN, &hdev->flags))
4923*3b420553SLuiz Augusto von Dentz 		hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
4924*3b420553SLuiz Augusto von Dentz 
4925*3b420553SLuiz Augusto von Dentz 	hci_scan_disable_sync(hdev);
4926*3b420553SLuiz Augusto von Dentz 
4927*3b420553SLuiz Augusto von Dentz 	hdev->scanning_paused = true;
4928*3b420553SLuiz Augusto von Dentz 
4929*3b420553SLuiz Augusto von Dentz 	return 0;
4930*3b420553SLuiz Augusto von Dentz }
4931*3b420553SLuiz Augusto von Dentz 
4932182ee45dSLuiz Augusto von Dentz /* This function performs the HCI suspend procedures in the follow order:
4933182ee45dSLuiz Augusto von Dentz  *
4934182ee45dSLuiz Augusto von Dentz  * Pause discovery (active scanning/inquiry)
4935182ee45dSLuiz Augusto von Dentz  * Pause Directed Advertising/Advertising
4936*3b420553SLuiz Augusto von Dentz  * Pause Scanning (passive scanning in case discovery was not active)
4937182ee45dSLuiz Augusto von Dentz  * Disconnect all connections
4938182ee45dSLuiz Augusto von Dentz  * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
4939182ee45dSLuiz Augusto von Dentz  * otherwise:
4940182ee45dSLuiz Augusto von Dentz  * Update event mask (only set events that are allowed to wake up the host)
4941182ee45dSLuiz Augusto von Dentz  * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
4942182ee45dSLuiz Augusto von Dentz  * Update passive scanning (lower duty cycle)
4943182ee45dSLuiz Augusto von Dentz  * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
4944182ee45dSLuiz Augusto von Dentz  */
4945182ee45dSLuiz Augusto von Dentz int hci_suspend_sync(struct hci_dev *hdev)
4946182ee45dSLuiz Augusto von Dentz {
4947182ee45dSLuiz Augusto von Dentz 	int err;
4948182ee45dSLuiz Augusto von Dentz 
4949182ee45dSLuiz Augusto von Dentz 	/* If marked as suspended there nothing to do */
4950182ee45dSLuiz Augusto von Dentz 	if (hdev->suspended)
4951182ee45dSLuiz Augusto von Dentz 		return 0;
4952182ee45dSLuiz Augusto von Dentz 
4953182ee45dSLuiz Augusto von Dentz 	/* Mark device as suspended */
4954182ee45dSLuiz Augusto von Dentz 	hdev->suspended = true;
4955182ee45dSLuiz Augusto von Dentz 
4956182ee45dSLuiz Augusto von Dentz 	/* Pause discovery if not already stopped */
4957182ee45dSLuiz Augusto von Dentz 	hci_pause_discovery_sync(hdev);
4958182ee45dSLuiz Augusto von Dentz 
4959182ee45dSLuiz Augusto von Dentz 	/* Pause other advertisements */
4960182ee45dSLuiz Augusto von Dentz 	hci_pause_advertising_sync(hdev);
4961182ee45dSLuiz Augusto von Dentz 
4962182ee45dSLuiz Augusto von Dentz 	/* Suspend monitor filters */
4963182ee45dSLuiz Augusto von Dentz 	hci_suspend_monitor_sync(hdev);
4964182ee45dSLuiz Augusto von Dentz 
4965182ee45dSLuiz Augusto von Dentz 	/* Prevent disconnects from causing scanning to be re-enabled */
4966*3b420553SLuiz Augusto von Dentz 	hci_pause_scan_sync(hdev);
4967182ee45dSLuiz Augusto von Dentz 
4968182ee45dSLuiz Augusto von Dentz 	/* Soft disconnect everything (power off) */
4969182ee45dSLuiz Augusto von Dentz 	err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
4970182ee45dSLuiz Augusto von Dentz 	if (err) {
4971182ee45dSLuiz Augusto von Dentz 		/* Set state to BT_RUNNING so resume doesn't notify */
4972182ee45dSLuiz Augusto von Dentz 		hdev->suspend_state = BT_RUNNING;
4973182ee45dSLuiz Augusto von Dentz 		hci_resume_sync(hdev);
4974182ee45dSLuiz Augusto von Dentz 		return err;
4975182ee45dSLuiz Augusto von Dentz 	}
4976182ee45dSLuiz Augusto von Dentz 
4977182ee45dSLuiz Augusto von Dentz 	/* Only configure accept list if disconnect succeeded and wake
4978182ee45dSLuiz Augusto von Dentz 	 * isn't being prevented.
4979182ee45dSLuiz Augusto von Dentz 	 */
4980182ee45dSLuiz Augusto von Dentz 	if (!hdev->wakeup || !hdev->wakeup(hdev)) {
4981182ee45dSLuiz Augusto von Dentz 		hdev->suspend_state = BT_SUSPEND_DISCONNECT;
4982182ee45dSLuiz Augusto von Dentz 		return 0;
4983182ee45dSLuiz Augusto von Dentz 	}
4984182ee45dSLuiz Augusto von Dentz 
4985182ee45dSLuiz Augusto von Dentz 	/* Unpause to take care of updating scanning params */
4986182ee45dSLuiz Augusto von Dentz 	hdev->scanning_paused = false;
4987182ee45dSLuiz Augusto von Dentz 
4988182ee45dSLuiz Augusto von Dentz 	/* Update event mask so only the allowed event can wakeup the host */
4989182ee45dSLuiz Augusto von Dentz 	hci_set_event_mask_sync(hdev);
4990182ee45dSLuiz Augusto von Dentz 
4991182ee45dSLuiz Augusto von Dentz 	/* Enable event filter for paired devices */
4992182ee45dSLuiz Augusto von Dentz 	hci_update_event_filter_sync(hdev);
4993182ee45dSLuiz Augusto von Dentz 
4994182ee45dSLuiz Augusto von Dentz 	/* Update LE passive scan if enabled */
4995182ee45dSLuiz Augusto von Dentz 	hci_update_passive_scan_sync(hdev);
4996182ee45dSLuiz Augusto von Dentz 
4997182ee45dSLuiz Augusto von Dentz 	/* Pause scan changes again. */
4998182ee45dSLuiz Augusto von Dentz 	hdev->scanning_paused = true;
4999182ee45dSLuiz Augusto von Dentz 
5000182ee45dSLuiz Augusto von Dentz 	hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5001182ee45dSLuiz Augusto von Dentz 
5002182ee45dSLuiz Augusto von Dentz 	return 0;
5003182ee45dSLuiz Augusto von Dentz }
5004182ee45dSLuiz Augusto von Dentz 
5005182ee45dSLuiz Augusto von Dentz /* This function resumes discovery */
5006182ee45dSLuiz Augusto von Dentz static int hci_resume_discovery_sync(struct hci_dev *hdev)
5007182ee45dSLuiz Augusto von Dentz {
5008182ee45dSLuiz Augusto von Dentz 	int err;
5009182ee45dSLuiz Augusto von Dentz 
5010182ee45dSLuiz Augusto von Dentz 	/* If discovery not paused there nothing to do */
5011182ee45dSLuiz Augusto von Dentz 	if (!hdev->discovery_paused)
5012182ee45dSLuiz Augusto von Dentz 		return 0;
5013182ee45dSLuiz Augusto von Dentz 
5014182ee45dSLuiz Augusto von Dentz 	hdev->discovery_paused = false;
5015182ee45dSLuiz Augusto von Dentz 
5016182ee45dSLuiz Augusto von Dentz 	hci_discovery_set_state(hdev, DISCOVERY_STARTING);
5017182ee45dSLuiz Augusto von Dentz 
5018182ee45dSLuiz Augusto von Dentz 	err = hci_start_discovery_sync(hdev);
5019182ee45dSLuiz Augusto von Dentz 
5020182ee45dSLuiz Augusto von Dentz 	hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
5021182ee45dSLuiz Augusto von Dentz 				DISCOVERY_FINDING);
5022182ee45dSLuiz Augusto von Dentz 
5023182ee45dSLuiz Augusto von Dentz 	return err;
5024182ee45dSLuiz Augusto von Dentz }
5025182ee45dSLuiz Augusto von Dentz 
5026182ee45dSLuiz Augusto von Dentz static void hci_resume_monitor_sync(struct hci_dev *hdev)
5027182ee45dSLuiz Augusto von Dentz {
5028182ee45dSLuiz Augusto von Dentz 	switch (hci_get_adv_monitor_offload_ext(hdev)) {
5029182ee45dSLuiz Augusto von Dentz 	case HCI_ADV_MONITOR_EXT_MSFT:
5030182ee45dSLuiz Augusto von Dentz 		msft_resume_sync(hdev);
5031182ee45dSLuiz Augusto von Dentz 		break;
5032182ee45dSLuiz Augusto von Dentz 	default:
5033182ee45dSLuiz Augusto von Dentz 		return;
5034182ee45dSLuiz Augusto von Dentz 	}
5035182ee45dSLuiz Augusto von Dentz }
5036182ee45dSLuiz Augusto von Dentz 
5037*3b420553SLuiz Augusto von Dentz /* This function resume scan and reset paused flag */
5038*3b420553SLuiz Augusto von Dentz static int hci_resume_scan_sync(struct hci_dev *hdev)
5039*3b420553SLuiz Augusto von Dentz {
5040*3b420553SLuiz Augusto von Dentz 	if (!hdev->scanning_paused)
5041*3b420553SLuiz Augusto von Dentz 		return 0;
5042*3b420553SLuiz Augusto von Dentz 
5043*3b420553SLuiz Augusto von Dentz 	hci_update_scan_sync(hdev);
5044*3b420553SLuiz Augusto von Dentz 
5045*3b420553SLuiz Augusto von Dentz 	/* Reset passive scanning to normal */
5046*3b420553SLuiz Augusto von Dentz 	hci_update_passive_scan_sync(hdev);
5047*3b420553SLuiz Augusto von Dentz 
5048*3b420553SLuiz Augusto von Dentz 	hdev->scanning_paused = false;
5049*3b420553SLuiz Augusto von Dentz 
5050*3b420553SLuiz Augusto von Dentz 	return 0;
5051*3b420553SLuiz Augusto von Dentz }
5052*3b420553SLuiz Augusto von Dentz 
5053182ee45dSLuiz Augusto von Dentz /* This function performs the HCI suspend procedures in the follow order:
5054182ee45dSLuiz Augusto von Dentz  *
5055182ee45dSLuiz Augusto von Dentz  * Restore event mask
5056182ee45dSLuiz Augusto von Dentz  * Clear event filter
5057182ee45dSLuiz Augusto von Dentz  * Update passive scanning (normal duty cycle)
5058182ee45dSLuiz Augusto von Dentz  * Resume Directed Advertising/Advertising
5059182ee45dSLuiz Augusto von Dentz  * Resume discovery (active scanning/inquiry)
5060182ee45dSLuiz Augusto von Dentz  */
5061182ee45dSLuiz Augusto von Dentz int hci_resume_sync(struct hci_dev *hdev)
5062182ee45dSLuiz Augusto von Dentz {
5063182ee45dSLuiz Augusto von Dentz 	/* If not marked as suspended there nothing to do */
5064182ee45dSLuiz Augusto von Dentz 	if (!hdev->suspended)
5065182ee45dSLuiz Augusto von Dentz 		return 0;
5066182ee45dSLuiz Augusto von Dentz 
5067182ee45dSLuiz Augusto von Dentz 	hdev->suspended = false;
5068182ee45dSLuiz Augusto von Dentz 	hdev->scanning_paused = false;
5069182ee45dSLuiz Augusto von Dentz 
5070182ee45dSLuiz Augusto von Dentz 	/* Restore event mask */
5071182ee45dSLuiz Augusto von Dentz 	hci_set_event_mask_sync(hdev);
5072182ee45dSLuiz Augusto von Dentz 
5073182ee45dSLuiz Augusto von Dentz 	/* Clear any event filters and restore scan state */
5074182ee45dSLuiz Augusto von Dentz 	hci_clear_event_filter_sync(hdev);
5075182ee45dSLuiz Augusto von Dentz 
5076*3b420553SLuiz Augusto von Dentz 	/* Resume scanning */
5077*3b420553SLuiz Augusto von Dentz 	hci_resume_scan_sync(hdev);
5078182ee45dSLuiz Augusto von Dentz 
5079182ee45dSLuiz Augusto von Dentz 	/* Resume monitor filters */
5080182ee45dSLuiz Augusto von Dentz 	hci_resume_monitor_sync(hdev);
5081182ee45dSLuiz Augusto von Dentz 
5082182ee45dSLuiz Augusto von Dentz 	/* Resume other advertisements */
5083182ee45dSLuiz Augusto von Dentz 	hci_resume_advertising_sync(hdev);
5084182ee45dSLuiz Augusto von Dentz 
5085182ee45dSLuiz Augusto von Dentz 	/* Resume discovery */
5086182ee45dSLuiz Augusto von Dentz 	hci_resume_discovery_sync(hdev);
5087182ee45dSLuiz Augusto von Dentz 
5088182ee45dSLuiz Augusto von Dentz 	return 0;
5089182ee45dSLuiz Augusto von Dentz }
50908e8b92eeSLuiz Augusto von Dentz 
50918e8b92eeSLuiz Augusto von Dentz static bool conn_use_rpa(struct hci_conn *conn)
50928e8b92eeSLuiz Augusto von Dentz {
50938e8b92eeSLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
50948e8b92eeSLuiz Augusto von Dentz 
50958e8b92eeSLuiz Augusto von Dentz 	return hci_dev_test_flag(hdev, HCI_PRIVACY);
50968e8b92eeSLuiz Augusto von Dentz }
50978e8b92eeSLuiz Augusto von Dentz 
50988e8b92eeSLuiz Augusto von Dentz static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
50998e8b92eeSLuiz Augusto von Dentz 						struct hci_conn *conn)
51008e8b92eeSLuiz Augusto von Dentz {
51018e8b92eeSLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_adv_params cp;
51028e8b92eeSLuiz Augusto von Dentz 	int err;
51038e8b92eeSLuiz Augusto von Dentz 	bdaddr_t random_addr;
51048e8b92eeSLuiz Augusto von Dentz 	u8 own_addr_type;
51058e8b92eeSLuiz Augusto von Dentz 
51068e8b92eeSLuiz Augusto von Dentz 	err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
51078e8b92eeSLuiz Augusto von Dentz 					     &own_addr_type);
51088e8b92eeSLuiz Augusto von Dentz 	if (err)
51098e8b92eeSLuiz Augusto von Dentz 		return err;
51108e8b92eeSLuiz Augusto von Dentz 
51118e8b92eeSLuiz Augusto von Dentz 	/* Set require_privacy to false so that the remote device has a
51128e8b92eeSLuiz Augusto von Dentz 	 * chance of identifying us.
51138e8b92eeSLuiz Augusto von Dentz 	 */
51148e8b92eeSLuiz Augusto von Dentz 	err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
51158e8b92eeSLuiz Augusto von Dentz 				     &own_addr_type, &random_addr);
51168e8b92eeSLuiz Augusto von Dentz 	if (err)
51178e8b92eeSLuiz Augusto von Dentz 		return err;
51188e8b92eeSLuiz Augusto von Dentz 
51198e8b92eeSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
51208e8b92eeSLuiz Augusto von Dentz 
51218e8b92eeSLuiz Augusto von Dentz 	cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
51228e8b92eeSLuiz Augusto von Dentz 	cp.own_addr_type = own_addr_type;
51238e8b92eeSLuiz Augusto von Dentz 	cp.channel_map = hdev->le_adv_channel_map;
51248e8b92eeSLuiz Augusto von Dentz 	cp.tx_power = HCI_TX_POWER_INVALID;
51258e8b92eeSLuiz Augusto von Dentz 	cp.primary_phy = HCI_ADV_PHY_1M;
51268e8b92eeSLuiz Augusto von Dentz 	cp.secondary_phy = HCI_ADV_PHY_1M;
51278e8b92eeSLuiz Augusto von Dentz 	cp.handle = 0x00; /* Use instance 0 for directed adv */
51288e8b92eeSLuiz Augusto von Dentz 	cp.own_addr_type = own_addr_type;
51298e8b92eeSLuiz Augusto von Dentz 	cp.peer_addr_type = conn->dst_type;
51308e8b92eeSLuiz Augusto von Dentz 	bacpy(&cp.peer_addr, &conn->dst);
51318e8b92eeSLuiz Augusto von Dentz 
51328e8b92eeSLuiz Augusto von Dentz 	/* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
51338e8b92eeSLuiz Augusto von Dentz 	 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
51348e8b92eeSLuiz Augusto von Dentz 	 * does not supports advertising data when the advertising set already
51358e8b92eeSLuiz Augusto von Dentz 	 * contains some, the controller shall return erroc code 'Invalid
51368e8b92eeSLuiz Augusto von Dentz 	 * HCI Command Parameters(0x12).
51378e8b92eeSLuiz Augusto von Dentz 	 * So it is required to remove adv set for handle 0x00. since we use
51388e8b92eeSLuiz Augusto von Dentz 	 * instance 0 for directed adv.
51398e8b92eeSLuiz Augusto von Dentz 	 */
51408e8b92eeSLuiz Augusto von Dentz 	err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
51418e8b92eeSLuiz Augusto von Dentz 	if (err)
51428e8b92eeSLuiz Augusto von Dentz 		return err;
51438e8b92eeSLuiz Augusto von Dentz 
51448e8b92eeSLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
51458e8b92eeSLuiz Augusto von Dentz 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
51468e8b92eeSLuiz Augusto von Dentz 	if (err)
51478e8b92eeSLuiz Augusto von Dentz 		return err;
51488e8b92eeSLuiz Augusto von Dentz 
51498e8b92eeSLuiz Augusto von Dentz 	/* Check if random address need to be updated */
51508e8b92eeSLuiz Augusto von Dentz 	if (own_addr_type == ADDR_LE_DEV_RANDOM &&
51518e8b92eeSLuiz Augusto von Dentz 	    bacmp(&random_addr, BDADDR_ANY) &&
51528e8b92eeSLuiz Augusto von Dentz 	    bacmp(&random_addr, &hdev->random_addr)) {
51538e8b92eeSLuiz Augusto von Dentz 		err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
51548e8b92eeSLuiz Augusto von Dentz 						       &random_addr);
51558e8b92eeSLuiz Augusto von Dentz 		if (err)
51568e8b92eeSLuiz Augusto von Dentz 			return err;
51578e8b92eeSLuiz Augusto von Dentz 	}
51588e8b92eeSLuiz Augusto von Dentz 
51598e8b92eeSLuiz Augusto von Dentz 	return hci_enable_ext_advertising_sync(hdev, 0x00);
51608e8b92eeSLuiz Augusto von Dentz }
51618e8b92eeSLuiz Augusto von Dentz 
51628e8b92eeSLuiz Augusto von Dentz static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
51638e8b92eeSLuiz Augusto von Dentz 					    struct hci_conn *conn)
51648e8b92eeSLuiz Augusto von Dentz {
51658e8b92eeSLuiz Augusto von Dentz 	struct hci_cp_le_set_adv_param cp;
51668e8b92eeSLuiz Augusto von Dentz 	u8 status;
51678e8b92eeSLuiz Augusto von Dentz 	u8 own_addr_type;
51688e8b92eeSLuiz Augusto von Dentz 	u8 enable;
51698e8b92eeSLuiz Augusto von Dentz 
51708e8b92eeSLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
51718e8b92eeSLuiz Augusto von Dentz 		return hci_le_ext_directed_advertising_sync(hdev, conn);
51728e8b92eeSLuiz Augusto von Dentz 
51738e8b92eeSLuiz Augusto von Dentz 	/* Clear the HCI_LE_ADV bit temporarily so that the
51748e8b92eeSLuiz Augusto von Dentz 	 * hci_update_random_address knows that it's safe to go ahead
51758e8b92eeSLuiz Augusto von Dentz 	 * and write a new random address. The flag will be set back on
51768e8b92eeSLuiz Augusto von Dentz 	 * as soon as the SET_ADV_ENABLE HCI command completes.
51778e8b92eeSLuiz Augusto von Dentz 	 */
51788e8b92eeSLuiz Augusto von Dentz 	hci_dev_clear_flag(hdev, HCI_LE_ADV);
51798e8b92eeSLuiz Augusto von Dentz 
51808e8b92eeSLuiz Augusto von Dentz 	/* Set require_privacy to false so that the remote device has a
51818e8b92eeSLuiz Augusto von Dentz 	 * chance of identifying us.
51828e8b92eeSLuiz Augusto von Dentz 	 */
51838e8b92eeSLuiz Augusto von Dentz 	status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
51848e8b92eeSLuiz Augusto von Dentz 						&own_addr_type);
51858e8b92eeSLuiz Augusto von Dentz 	if (status)
51868e8b92eeSLuiz Augusto von Dentz 		return status;
51878e8b92eeSLuiz Augusto von Dentz 
51888e8b92eeSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
51898e8b92eeSLuiz Augusto von Dentz 
51908e8b92eeSLuiz Augusto von Dentz 	/* Some controllers might reject command if intervals are not
51918e8b92eeSLuiz Augusto von Dentz 	 * within range for undirected advertising.
51928e8b92eeSLuiz Augusto von Dentz 	 * BCM20702A0 is known to be affected by this.
51938e8b92eeSLuiz Augusto von Dentz 	 */
51948e8b92eeSLuiz Augusto von Dentz 	cp.min_interval = cpu_to_le16(0x0020);
51958e8b92eeSLuiz Augusto von Dentz 	cp.max_interval = cpu_to_le16(0x0020);
51968e8b92eeSLuiz Augusto von Dentz 
51978e8b92eeSLuiz Augusto von Dentz 	cp.type = LE_ADV_DIRECT_IND;
51988e8b92eeSLuiz Augusto von Dentz 	cp.own_address_type = own_addr_type;
51998e8b92eeSLuiz Augusto von Dentz 	cp.direct_addr_type = conn->dst_type;
52008e8b92eeSLuiz Augusto von Dentz 	bacpy(&cp.direct_addr, &conn->dst);
52018e8b92eeSLuiz Augusto von Dentz 	cp.channel_map = hdev->le_adv_channel_map;
52028e8b92eeSLuiz Augusto von Dentz 
52038e8b92eeSLuiz Augusto von Dentz 	status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
52048e8b92eeSLuiz Augusto von Dentz 				       sizeof(cp), &cp, HCI_CMD_TIMEOUT);
52058e8b92eeSLuiz Augusto von Dentz 	if (status)
52068e8b92eeSLuiz Augusto von Dentz 		return status;
52078e8b92eeSLuiz Augusto von Dentz 
52088e8b92eeSLuiz Augusto von Dentz 	enable = 0x01;
52098e8b92eeSLuiz Augusto von Dentz 
52108e8b92eeSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
52118e8b92eeSLuiz Augusto von Dentz 				     sizeof(enable), &enable, HCI_CMD_TIMEOUT);
52128e8b92eeSLuiz Augusto von Dentz }
52138e8b92eeSLuiz Augusto von Dentz 
52148e8b92eeSLuiz Augusto von Dentz static void set_ext_conn_params(struct hci_conn *conn,
52158e8b92eeSLuiz Augusto von Dentz 				struct hci_cp_le_ext_conn_param *p)
52168e8b92eeSLuiz Augusto von Dentz {
52178e8b92eeSLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
52188e8b92eeSLuiz Augusto von Dentz 
52198e8b92eeSLuiz Augusto von Dentz 	memset(p, 0, sizeof(*p));
52208e8b92eeSLuiz Augusto von Dentz 
52218e8b92eeSLuiz Augusto von Dentz 	p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
52228e8b92eeSLuiz Augusto von Dentz 	p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
52238e8b92eeSLuiz Augusto von Dentz 	p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
52248e8b92eeSLuiz Augusto von Dentz 	p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
52258e8b92eeSLuiz Augusto von Dentz 	p->conn_latency = cpu_to_le16(conn->le_conn_latency);
52268e8b92eeSLuiz Augusto von Dentz 	p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
52278e8b92eeSLuiz Augusto von Dentz 	p->min_ce_len = cpu_to_le16(0x0000);
52288e8b92eeSLuiz Augusto von Dentz 	p->max_ce_len = cpu_to_le16(0x0000);
52298e8b92eeSLuiz Augusto von Dentz }
52308e8b92eeSLuiz Augusto von Dentz 
523189a0b8b9SLuiz Augusto von Dentz static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
523289a0b8b9SLuiz Augusto von Dentz 				       struct hci_conn *conn, u8 own_addr_type)
52338e8b92eeSLuiz Augusto von Dentz {
52348e8b92eeSLuiz Augusto von Dentz 	struct hci_cp_le_ext_create_conn *cp;
52358e8b92eeSLuiz Augusto von Dentz 	struct hci_cp_le_ext_conn_param *p;
52368e8b92eeSLuiz Augusto von Dentz 	u8 data[sizeof(*cp) + sizeof(*p) * 3];
52378e8b92eeSLuiz Augusto von Dentz 	u32 plen;
52388e8b92eeSLuiz Augusto von Dentz 
52398e8b92eeSLuiz Augusto von Dentz 	cp = (void *)data;
52408e8b92eeSLuiz Augusto von Dentz 	p = (void *)cp->data;
52418e8b92eeSLuiz Augusto von Dentz 
52428e8b92eeSLuiz Augusto von Dentz 	memset(cp, 0, sizeof(*cp));
52438e8b92eeSLuiz Augusto von Dentz 
52448e8b92eeSLuiz Augusto von Dentz 	bacpy(&cp->peer_addr, &conn->dst);
52458e8b92eeSLuiz Augusto von Dentz 	cp->peer_addr_type = conn->dst_type;
52468e8b92eeSLuiz Augusto von Dentz 	cp->own_addr_type = own_addr_type;
52478e8b92eeSLuiz Augusto von Dentz 
52488e8b92eeSLuiz Augusto von Dentz 	plen = sizeof(*cp);
52498e8b92eeSLuiz Augusto von Dentz 
52508e8b92eeSLuiz Augusto von Dentz 	if (scan_1m(hdev)) {
52518e8b92eeSLuiz Augusto von Dentz 		cp->phys |= LE_SCAN_PHY_1M;
52528e8b92eeSLuiz Augusto von Dentz 		set_ext_conn_params(conn, p);
52538e8b92eeSLuiz Augusto von Dentz 
52548e8b92eeSLuiz Augusto von Dentz 		p++;
52558e8b92eeSLuiz Augusto von Dentz 		plen += sizeof(*p);
52568e8b92eeSLuiz Augusto von Dentz 	}
52578e8b92eeSLuiz Augusto von Dentz 
52588e8b92eeSLuiz Augusto von Dentz 	if (scan_2m(hdev)) {
52598e8b92eeSLuiz Augusto von Dentz 		cp->phys |= LE_SCAN_PHY_2M;
52608e8b92eeSLuiz Augusto von Dentz 		set_ext_conn_params(conn, p);
52618e8b92eeSLuiz Augusto von Dentz 
52628e8b92eeSLuiz Augusto von Dentz 		p++;
52638e8b92eeSLuiz Augusto von Dentz 		plen += sizeof(*p);
52648e8b92eeSLuiz Augusto von Dentz 	}
52658e8b92eeSLuiz Augusto von Dentz 
52668e8b92eeSLuiz Augusto von Dentz 	if (scan_coded(hdev)) {
52678e8b92eeSLuiz Augusto von Dentz 		cp->phys |= LE_SCAN_PHY_CODED;
52688e8b92eeSLuiz Augusto von Dentz 		set_ext_conn_params(conn, p);
52698e8b92eeSLuiz Augusto von Dentz 
52708e8b92eeSLuiz Augusto von Dentz 		plen += sizeof(*p);
52718e8b92eeSLuiz Augusto von Dentz 	}
52728e8b92eeSLuiz Augusto von Dentz 
52736cd29ec6SLuiz Augusto von Dentz 	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
52746cd29ec6SLuiz Augusto von Dentz 					plen, data,
52756cd29ec6SLuiz Augusto von Dentz 					HCI_EV_LE_ENHANCED_CONN_COMPLETE,
5276a56a1138SLuiz Augusto von Dentz 					conn->conn_timeout, NULL);
52778e8b92eeSLuiz Augusto von Dentz }
52788e8b92eeSLuiz Augusto von Dentz 
52798e8b92eeSLuiz Augusto von Dentz int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
52808e8b92eeSLuiz Augusto von Dentz {
52818e8b92eeSLuiz Augusto von Dentz 	struct hci_cp_le_create_conn cp;
52828e8b92eeSLuiz Augusto von Dentz 	struct hci_conn_params *params;
52838e8b92eeSLuiz Augusto von Dentz 	u8 own_addr_type;
52848e8b92eeSLuiz Augusto von Dentz 	int err;
52858e8b92eeSLuiz Augusto von Dentz 
52868e8b92eeSLuiz Augusto von Dentz 	/* If requested to connect as peripheral use directed advertising */
52878e8b92eeSLuiz Augusto von Dentz 	if (conn->role == HCI_ROLE_SLAVE) {
528876d0685bSLuiz Augusto von Dentz 		/* If we're active scanning and simultaneous roles is not
528976d0685bSLuiz Augusto von Dentz 		 * enabled simply reject the attempt.
52908e8b92eeSLuiz Augusto von Dentz 		 */
52918e8b92eeSLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
52924fc9857aSLuiz Augusto von Dentz 		    hdev->le_scan_type == LE_SCAN_ACTIVE &&
529376d0685bSLuiz Augusto von Dentz 		    !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
52948e8b92eeSLuiz Augusto von Dentz 			hci_conn_del(conn);
52958e8b92eeSLuiz Augusto von Dentz 			return -EBUSY;
52968e8b92eeSLuiz Augusto von Dentz 		}
52978e8b92eeSLuiz Augusto von Dentz 
52984fc9857aSLuiz Augusto von Dentz 		/* Pause advertising while doing directed advertising. */
52994fc9857aSLuiz Augusto von Dentz 		hci_pause_advertising_sync(hdev);
53004fc9857aSLuiz Augusto von Dentz 
53018e8b92eeSLuiz Augusto von Dentz 		err = hci_le_directed_advertising_sync(hdev, conn);
53028e8b92eeSLuiz Augusto von Dentz 		goto done;
53038e8b92eeSLuiz Augusto von Dentz 	}
53048e8b92eeSLuiz Augusto von Dentz 
530576d0685bSLuiz Augusto von Dentz 	/* Disable advertising if simultaneous roles is not in use. */
530676d0685bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
53074fc9857aSLuiz Augusto von Dentz 		hci_pause_advertising_sync(hdev);
53084fc9857aSLuiz Augusto von Dentz 
53098e8b92eeSLuiz Augusto von Dentz 	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
53108e8b92eeSLuiz Augusto von Dentz 	if (params) {
53118e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_min_interval = params->conn_min_interval;
53128e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_max_interval = params->conn_max_interval;
53138e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_latency = params->conn_latency;
53148e8b92eeSLuiz Augusto von Dentz 		conn->le_supv_timeout = params->supervision_timeout;
53158e8b92eeSLuiz Augusto von Dentz 	} else {
53168e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_min_interval = hdev->le_conn_min_interval;
53178e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_max_interval = hdev->le_conn_max_interval;
53188e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_latency = hdev->le_conn_latency;
53198e8b92eeSLuiz Augusto von Dentz 		conn->le_supv_timeout = hdev->le_supv_timeout;
53208e8b92eeSLuiz Augusto von Dentz 	}
53218e8b92eeSLuiz Augusto von Dentz 
53228e8b92eeSLuiz Augusto von Dentz 	/* If controller is scanning, we stop it since some controllers are
53238e8b92eeSLuiz Augusto von Dentz 	 * not able to scan and connect at the same time. Also set the
53248e8b92eeSLuiz Augusto von Dentz 	 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
53258e8b92eeSLuiz Augusto von Dentz 	 * handler for scan disabling knows to set the correct discovery
53268e8b92eeSLuiz Augusto von Dentz 	 * state.
53278e8b92eeSLuiz Augusto von Dentz 	 */
53288e8b92eeSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
53298e8b92eeSLuiz Augusto von Dentz 		hci_scan_disable_sync(hdev);
53308e8b92eeSLuiz Augusto von Dentz 		hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
53318e8b92eeSLuiz Augusto von Dentz 	}
53328e8b92eeSLuiz Augusto von Dentz 
53338e8b92eeSLuiz Augusto von Dentz 	/* Update random address, but set require_privacy to false so
53348e8b92eeSLuiz Augusto von Dentz 	 * that we never connect with an non-resolvable address.
53358e8b92eeSLuiz Augusto von Dentz 	 */
53368e8b92eeSLuiz Augusto von Dentz 	err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
53378e8b92eeSLuiz Augusto von Dentz 					     &own_addr_type);
53388e8b92eeSLuiz Augusto von Dentz 	if (err)
53398e8b92eeSLuiz Augusto von Dentz 		goto done;
53408e8b92eeSLuiz Augusto von Dentz 
53418e8b92eeSLuiz Augusto von Dentz 	if (use_ext_conn(hdev)) {
53428e8b92eeSLuiz Augusto von Dentz 		err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
53438e8b92eeSLuiz Augusto von Dentz 		goto done;
53448e8b92eeSLuiz Augusto von Dentz 	}
53458e8b92eeSLuiz Augusto von Dentz 
53468e8b92eeSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
53478e8b92eeSLuiz Augusto von Dentz 
53488e8b92eeSLuiz Augusto von Dentz 	cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
53498e8b92eeSLuiz Augusto von Dentz 	cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
53508e8b92eeSLuiz Augusto von Dentz 
53518e8b92eeSLuiz Augusto von Dentz 	bacpy(&cp.peer_addr, &conn->dst);
53528e8b92eeSLuiz Augusto von Dentz 	cp.peer_addr_type = conn->dst_type;
53538e8b92eeSLuiz Augusto von Dentz 	cp.own_address_type = own_addr_type;
53548e8b92eeSLuiz Augusto von Dentz 	cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
53558e8b92eeSLuiz Augusto von Dentz 	cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
53568e8b92eeSLuiz Augusto von Dentz 	cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
53578e8b92eeSLuiz Augusto von Dentz 	cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
53588e8b92eeSLuiz Augusto von Dentz 	cp.min_ce_len = cpu_to_le16(0x0000);
53598e8b92eeSLuiz Augusto von Dentz 	cp.max_ce_len = cpu_to_le16(0x0000);
53608e8b92eeSLuiz Augusto von Dentz 
5361a56a1138SLuiz Augusto von Dentz 	/* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
5362a56a1138SLuiz Augusto von Dentz 	 *
5363a56a1138SLuiz Augusto von Dentz 	 * If this event is unmasked and the HCI_LE_Connection_Complete event
5364a56a1138SLuiz Augusto von Dentz 	 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
5365a56a1138SLuiz Augusto von Dentz 	 * sent when a new connection has been created.
5366a56a1138SLuiz Augusto von Dentz 	 */
53676cd29ec6SLuiz Augusto von Dentz 	err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
5368a56a1138SLuiz Augusto von Dentz 				       sizeof(cp), &cp,
5369a56a1138SLuiz Augusto von Dentz 				       use_enhanced_conn_complete(hdev) ?
5370a56a1138SLuiz Augusto von Dentz 				       HCI_EV_LE_ENHANCED_CONN_COMPLETE :
5371a56a1138SLuiz Augusto von Dentz 				       HCI_EV_LE_CONN_COMPLETE,
5372a56a1138SLuiz Augusto von Dentz 				       conn->conn_timeout, NULL);
53738e8b92eeSLuiz Augusto von Dentz 
53748e8b92eeSLuiz Augusto von Dentz done:
53754fc9857aSLuiz Augusto von Dentz 	/* Re-enable advertising after the connection attempt is finished. */
53768e8b92eeSLuiz Augusto von Dentz 	hci_resume_advertising_sync(hdev);
53778e8b92eeSLuiz Augusto von Dentz 	return err;
53788e8b92eeSLuiz Augusto von Dentz }
5379