xref: /openbmc/linux/net/bluetooth/hci_sync.c (revision 5af1f84ed13a416297ab9ced7537f4d5ae7f329a)
16a98e383SMarcel Holtmann // SPDX-License-Identifier: GPL-2.0
26a98e383SMarcel Holtmann /*
36a98e383SMarcel Holtmann  * BlueZ - Bluetooth protocol stack for Linux
46a98e383SMarcel Holtmann  *
56a98e383SMarcel Holtmann  * Copyright (C) 2021 Intel Corporation
66a42e9bfSIulia Tanasescu  * Copyright 2023 NXP
76a98e383SMarcel Holtmann  */
86a98e383SMarcel Holtmann 
9d0b13706SLuiz Augusto von Dentz #include <linux/property.h>
10d0b13706SLuiz Augusto von Dentz 
116a98e383SMarcel Holtmann #include <net/bluetooth/bluetooth.h>
126a98e383SMarcel Holtmann #include <net/bluetooth/hci_core.h>
136a98e383SMarcel Holtmann #include <net/bluetooth/mgmt.h>
146a98e383SMarcel Holtmann 
156a98e383SMarcel Holtmann #include "hci_request.h"
16828cea2bSChethan T N #include "hci_codec.h"
17d0b13706SLuiz Augusto von Dentz #include "hci_debugfs.h"
186a98e383SMarcel Holtmann #include "smp.h"
19161510ccSLuiz Augusto von Dentz #include "eir.h"
20d0b13706SLuiz Augusto von Dentz #include "msft.h"
21d0b13706SLuiz Augusto von Dentz #include "aosp.h"
22d0b13706SLuiz Augusto von Dentz #include "leds.h"
236a98e383SMarcel Holtmann 
246a98e383SMarcel Holtmann static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
256a98e383SMarcel Holtmann 				  struct sk_buff *skb)
266a98e383SMarcel Holtmann {
276a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "result 0x%2.2x", result);
286a98e383SMarcel Holtmann 
296a98e383SMarcel Holtmann 	if (hdev->req_status != HCI_REQ_PEND)
306a98e383SMarcel Holtmann 		return;
316a98e383SMarcel Holtmann 
326a98e383SMarcel Holtmann 	hdev->req_result = result;
336a98e383SMarcel Holtmann 	hdev->req_status = HCI_REQ_DONE;
346a98e383SMarcel Holtmann 
35cba6b758SLuiz Augusto von Dentz 	if (skb) {
36cba6b758SLuiz Augusto von Dentz 		struct sock *sk = hci_skb_sk(skb);
37cba6b758SLuiz Augusto von Dentz 
38cba6b758SLuiz Augusto von Dentz 		/* Drop sk reference if set */
39cba6b758SLuiz Augusto von Dentz 		if (sk)
40cba6b758SLuiz Augusto von Dentz 			sock_put(sk);
41cba6b758SLuiz Augusto von Dentz 
42cba6b758SLuiz Augusto von Dentz 		hdev->req_skb = skb_get(skb);
43cba6b758SLuiz Augusto von Dentz 	}
44cba6b758SLuiz Augusto von Dentz 
456a98e383SMarcel Holtmann 	wake_up_interruptible(&hdev->req_wait_q);
466a98e383SMarcel Holtmann }
476a98e383SMarcel Holtmann 
486a98e383SMarcel Holtmann static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
496a98e383SMarcel Holtmann 					  u32 plen, const void *param,
506a98e383SMarcel Holtmann 					  struct sock *sk)
516a98e383SMarcel Holtmann {
526a98e383SMarcel Holtmann 	int len = HCI_COMMAND_HDR_SIZE + plen;
536a98e383SMarcel Holtmann 	struct hci_command_hdr *hdr;
546a98e383SMarcel Holtmann 	struct sk_buff *skb;
556a98e383SMarcel Holtmann 
566a98e383SMarcel Holtmann 	skb = bt_skb_alloc(len, GFP_ATOMIC);
576a98e383SMarcel Holtmann 	if (!skb)
586a98e383SMarcel Holtmann 		return NULL;
596a98e383SMarcel Holtmann 
606a98e383SMarcel Holtmann 	hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
616a98e383SMarcel Holtmann 	hdr->opcode = cpu_to_le16(opcode);
626a98e383SMarcel Holtmann 	hdr->plen   = plen;
636a98e383SMarcel Holtmann 
646a98e383SMarcel Holtmann 	if (plen)
656a98e383SMarcel Holtmann 		skb_put_data(skb, param, plen);
666a98e383SMarcel Holtmann 
676a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "skb len %d", skb->len);
686a98e383SMarcel Holtmann 
696a98e383SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
706a98e383SMarcel Holtmann 	hci_skb_opcode(skb) = opcode;
716a98e383SMarcel Holtmann 
72cba6b758SLuiz Augusto von Dentz 	/* Grab a reference if command needs to be associated with a sock (e.g.
73cba6b758SLuiz Augusto von Dentz 	 * likely mgmt socket that initiated the command).
74cba6b758SLuiz Augusto von Dentz 	 */
75cba6b758SLuiz Augusto von Dentz 	if (sk) {
76cba6b758SLuiz Augusto von Dentz 		hci_skb_sk(skb) = sk;
77cba6b758SLuiz Augusto von Dentz 		sock_hold(sk);
78cba6b758SLuiz Augusto von Dentz 	}
79cba6b758SLuiz Augusto von Dentz 
806a98e383SMarcel Holtmann 	return skb;
816a98e383SMarcel Holtmann }
826a98e383SMarcel Holtmann 
836a98e383SMarcel Holtmann static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
846a98e383SMarcel Holtmann 			     const void *param, u8 event, struct sock *sk)
856a98e383SMarcel Holtmann {
866a98e383SMarcel Holtmann 	struct hci_dev *hdev = req->hdev;
876a98e383SMarcel Holtmann 	struct sk_buff *skb;
886a98e383SMarcel Holtmann 
896a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
906a98e383SMarcel Holtmann 
916a98e383SMarcel Holtmann 	/* If an error occurred during request building, there is no point in
926a98e383SMarcel Holtmann 	 * queueing the HCI command. We can simply return.
936a98e383SMarcel Holtmann 	 */
946a98e383SMarcel Holtmann 	if (req->err)
956a98e383SMarcel Holtmann 		return;
966a98e383SMarcel Holtmann 
976a98e383SMarcel Holtmann 	skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
986a98e383SMarcel Holtmann 	if (!skb) {
996a98e383SMarcel Holtmann 		bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
1006a98e383SMarcel Holtmann 			   opcode);
1016a98e383SMarcel Holtmann 		req->err = -ENOMEM;
1026a98e383SMarcel Holtmann 		return;
1036a98e383SMarcel Holtmann 	}
1046a98e383SMarcel Holtmann 
1056a98e383SMarcel Holtmann 	if (skb_queue_empty(&req->cmd_q))
1066a98e383SMarcel Holtmann 		bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
1076a98e383SMarcel Holtmann 
10885b56857SLuiz Augusto von Dentz 	hci_skb_event(skb) = event;
1096a98e383SMarcel Holtmann 
1106a98e383SMarcel Holtmann 	skb_queue_tail(&req->cmd_q, skb);
1116a98e383SMarcel Holtmann }
1126a98e383SMarcel Holtmann 
1136a98e383SMarcel Holtmann static int hci_cmd_sync_run(struct hci_request *req)
1146a98e383SMarcel Holtmann {
1156a98e383SMarcel Holtmann 	struct hci_dev *hdev = req->hdev;
1166a98e383SMarcel Holtmann 	struct sk_buff *skb;
1176a98e383SMarcel Holtmann 	unsigned long flags;
1186a98e383SMarcel Holtmann 
1196a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
1206a98e383SMarcel Holtmann 
1216a98e383SMarcel Holtmann 	/* If an error occurred during request building, remove all HCI
1226a98e383SMarcel Holtmann 	 * commands queued on the HCI request queue.
1236a98e383SMarcel Holtmann 	 */
1246a98e383SMarcel Holtmann 	if (req->err) {
1256a98e383SMarcel Holtmann 		skb_queue_purge(&req->cmd_q);
1266a98e383SMarcel Holtmann 		return req->err;
1276a98e383SMarcel Holtmann 	}
1286a98e383SMarcel Holtmann 
1296a98e383SMarcel Holtmann 	/* Do not allow empty requests */
1306a98e383SMarcel Holtmann 	if (skb_queue_empty(&req->cmd_q))
1316a98e383SMarcel Holtmann 		return -ENODATA;
1326a98e383SMarcel Holtmann 
1336a98e383SMarcel Holtmann 	skb = skb_peek_tail(&req->cmd_q);
1346a98e383SMarcel Holtmann 	bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
1356a98e383SMarcel Holtmann 	bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
1366a98e383SMarcel Holtmann 
1376a98e383SMarcel Holtmann 	spin_lock_irqsave(&hdev->cmd_q.lock, flags);
1386a98e383SMarcel Holtmann 	skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
1396a98e383SMarcel Holtmann 	spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
1406a98e383SMarcel Holtmann 
1416a98e383SMarcel Holtmann 	queue_work(hdev->workqueue, &hdev->cmd_work);
1426a98e383SMarcel Holtmann 
1436a98e383SMarcel Holtmann 	return 0;
1446a98e383SMarcel Holtmann }
1456a98e383SMarcel Holtmann 
1466a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
1476a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
1486a98e383SMarcel Holtmann 				  const void *param, u8 event, u32 timeout,
1496a98e383SMarcel Holtmann 				  struct sock *sk)
1506a98e383SMarcel Holtmann {
1516a98e383SMarcel Holtmann 	struct hci_request req;
1526a98e383SMarcel Holtmann 	struct sk_buff *skb;
1536a98e383SMarcel Holtmann 	int err = 0;
1546a98e383SMarcel Holtmann 
155d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Opcode 0x%4x", opcode);
1566a98e383SMarcel Holtmann 
1576a98e383SMarcel Holtmann 	hci_req_init(&req, hdev);
1586a98e383SMarcel Holtmann 
1596a98e383SMarcel Holtmann 	hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
1606a98e383SMarcel Holtmann 
1616a98e383SMarcel Holtmann 	hdev->req_status = HCI_REQ_PEND;
1626a98e383SMarcel Holtmann 
1636a98e383SMarcel Holtmann 	err = hci_cmd_sync_run(&req);
1646a98e383SMarcel Holtmann 	if (err < 0)
1656a98e383SMarcel Holtmann 		return ERR_PTR(err);
1666a98e383SMarcel Holtmann 
1676a98e383SMarcel Holtmann 	err = wait_event_interruptible_timeout(hdev->req_wait_q,
1686a98e383SMarcel Holtmann 					       hdev->req_status != HCI_REQ_PEND,
1696a98e383SMarcel Holtmann 					       timeout);
1706a98e383SMarcel Holtmann 
1716a98e383SMarcel Holtmann 	if (err == -ERESTARTSYS)
1726a98e383SMarcel Holtmann 		return ERR_PTR(-EINTR);
1736a98e383SMarcel Holtmann 
1746a98e383SMarcel Holtmann 	switch (hdev->req_status) {
1756a98e383SMarcel Holtmann 	case HCI_REQ_DONE:
1766a98e383SMarcel Holtmann 		err = -bt_to_errno(hdev->req_result);
1776a98e383SMarcel Holtmann 		break;
1786a98e383SMarcel Holtmann 
1796a98e383SMarcel Holtmann 	case HCI_REQ_CANCELED:
1806a98e383SMarcel Holtmann 		err = -hdev->req_result;
1816a98e383SMarcel Holtmann 		break;
1826a98e383SMarcel Holtmann 
1836a98e383SMarcel Holtmann 	default:
1846a98e383SMarcel Holtmann 		err = -ETIMEDOUT;
1856a98e383SMarcel Holtmann 		break;
1866a98e383SMarcel Holtmann 	}
1876a98e383SMarcel Holtmann 
1886a98e383SMarcel Holtmann 	hdev->req_status = 0;
1896a98e383SMarcel Holtmann 	hdev->req_result = 0;
1906a98e383SMarcel Holtmann 	skb = hdev->req_skb;
1916a98e383SMarcel Holtmann 	hdev->req_skb = NULL;
1926a98e383SMarcel Holtmann 
1936a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "end: err %d", err);
1946a98e383SMarcel Holtmann 
1956a98e383SMarcel Holtmann 	if (err < 0) {
1966a98e383SMarcel Holtmann 		kfree_skb(skb);
1976a98e383SMarcel Holtmann 		return ERR_PTR(err);
1986a98e383SMarcel Holtmann 	}
1996a98e383SMarcel Holtmann 
2006a98e383SMarcel Holtmann 	return skb;
2016a98e383SMarcel Holtmann }
2026a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_sk);
2036a98e383SMarcel Holtmann 
2046a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
2056a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
2066a98e383SMarcel Holtmann 			       const void *param, u32 timeout)
2076a98e383SMarcel Holtmann {
2086a98e383SMarcel Holtmann 	return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
2096a98e383SMarcel Holtmann }
2106a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync);
2116a98e383SMarcel Holtmann 
2126a98e383SMarcel Holtmann /* Send HCI command and wait for command complete event */
2136a98e383SMarcel Holtmann struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
2146a98e383SMarcel Holtmann 			     const void *param, u32 timeout)
2156a98e383SMarcel Holtmann {
2166a98e383SMarcel Holtmann 	struct sk_buff *skb;
2176a98e383SMarcel Holtmann 
2186a98e383SMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags))
2196a98e383SMarcel Holtmann 		return ERR_PTR(-ENETDOWN);
2206a98e383SMarcel Holtmann 
2216a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
2226a98e383SMarcel Holtmann 
2236a98e383SMarcel Holtmann 	hci_req_sync_lock(hdev);
2246a98e383SMarcel Holtmann 	skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
2256a98e383SMarcel Holtmann 	hci_req_sync_unlock(hdev);
2266a98e383SMarcel Holtmann 
2276a98e383SMarcel Holtmann 	return skb;
2286a98e383SMarcel Holtmann }
2296a98e383SMarcel Holtmann EXPORT_SYMBOL(hci_cmd_sync);
2306a98e383SMarcel Holtmann 
2316a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
2326a98e383SMarcel Holtmann struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
2336a98e383SMarcel Holtmann 				  const void *param, u8 event, u32 timeout)
2346a98e383SMarcel Holtmann {
2356a98e383SMarcel Holtmann 	return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
2366a98e383SMarcel Holtmann 				 NULL);
2376a98e383SMarcel Holtmann }
2386a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_ev);
2396a98e383SMarcel Holtmann 
2406a98e383SMarcel Holtmann /* This function requires the caller holds hdev->req_lock. */
2416a98e383SMarcel Holtmann int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
2426a98e383SMarcel Holtmann 			     const void *param, u8 event, u32 timeout,
2436a98e383SMarcel Holtmann 			     struct sock *sk)
2446a98e383SMarcel Holtmann {
2456a98e383SMarcel Holtmann 	struct sk_buff *skb;
2466a98e383SMarcel Holtmann 	u8 status;
2476a98e383SMarcel Holtmann 
2486a98e383SMarcel Holtmann 	skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
249abfeea47SLuiz Augusto von Dentz 	if (IS_ERR(skb)) {
250b62e7220SLuiz Augusto von Dentz 		if (!event)
2516a98e383SMarcel Holtmann 			bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode,
2526a98e383SMarcel Holtmann 				   PTR_ERR(skb));
2536a98e383SMarcel Holtmann 		return PTR_ERR(skb);
2546a98e383SMarcel Holtmann 	}
2556a98e383SMarcel Holtmann 
256abfeea47SLuiz Augusto von Dentz 	/* If command return a status event skb will be set to NULL as there are
257abfeea47SLuiz Augusto von Dentz 	 * no parameters, in case of failure IS_ERR(skb) would have be set to
258abfeea47SLuiz Augusto von Dentz 	 * the actual error would be found with PTR_ERR(skb).
259abfeea47SLuiz Augusto von Dentz 	 */
260abfeea47SLuiz Augusto von Dentz 	if (!skb)
261abfeea47SLuiz Augusto von Dentz 		return 0;
262abfeea47SLuiz Augusto von Dentz 
2636a98e383SMarcel Holtmann 	status = skb->data[0];
2646a98e383SMarcel Holtmann 
2656a98e383SMarcel Holtmann 	kfree_skb(skb);
2666a98e383SMarcel Holtmann 
2676a98e383SMarcel Holtmann 	return status;
2686a98e383SMarcel Holtmann }
2696a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
2706a98e383SMarcel Holtmann 
2716a98e383SMarcel Holtmann int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
2726a98e383SMarcel Holtmann 			  const void *param, u32 timeout)
2736a98e383SMarcel Holtmann {
2746a98e383SMarcel Holtmann 	return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
2756a98e383SMarcel Holtmann 					NULL);
2766a98e383SMarcel Holtmann }
2776a98e383SMarcel Holtmann EXPORT_SYMBOL(__hci_cmd_sync_status);
2786a98e383SMarcel Holtmann 
2796a98e383SMarcel Holtmann static void hci_cmd_sync_work(struct work_struct *work)
2806a98e383SMarcel Holtmann {
2816a98e383SMarcel Holtmann 	struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
2826a98e383SMarcel Holtmann 
2836a98e383SMarcel Holtmann 	bt_dev_dbg(hdev, "");
2846a98e383SMarcel Holtmann 
285008ee9ebSLuiz Augusto von Dentz 	/* Dequeue all entries and run them */
286008ee9ebSLuiz Augusto von Dentz 	while (1) {
287008ee9ebSLuiz Augusto von Dentz 		struct hci_cmd_sync_work_entry *entry;
288008ee9ebSLuiz Augusto von Dentz 
2896a98e383SMarcel Holtmann 		mutex_lock(&hdev->cmd_sync_work_lock);
290008ee9ebSLuiz Augusto von Dentz 		entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
291008ee9ebSLuiz Augusto von Dentz 						 struct hci_cmd_sync_work_entry,
292008ee9ebSLuiz Augusto von Dentz 						 list);
293008ee9ebSLuiz Augusto von Dentz 		if (entry)
2946a98e383SMarcel Holtmann 			list_del(&entry->list);
2956a98e383SMarcel Holtmann 		mutex_unlock(&hdev->cmd_sync_work_lock);
2966a98e383SMarcel Holtmann 
297008ee9ebSLuiz Augusto von Dentz 		if (!entry)
298008ee9ebSLuiz Augusto von Dentz 			break;
299008ee9ebSLuiz Augusto von Dentz 
300008ee9ebSLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "entry %p", entry);
301008ee9ebSLuiz Augusto von Dentz 
302008ee9ebSLuiz Augusto von Dentz 		if (entry->func) {
3036a98e383SMarcel Holtmann 			int err;
3046a98e383SMarcel Holtmann 
3056a98e383SMarcel Holtmann 			hci_req_sync_lock(hdev);
306008ee9ebSLuiz Augusto von Dentz 			err = entry->func(hdev, entry->data);
307008ee9ebSLuiz Augusto von Dentz 			if (entry->destroy)
308008ee9ebSLuiz Augusto von Dentz 				entry->destroy(hdev, entry->data, err);
3096a98e383SMarcel Holtmann 			hci_req_sync_unlock(hdev);
3106a98e383SMarcel Holtmann 		}
311008ee9ebSLuiz Augusto von Dentz 
312008ee9ebSLuiz Augusto von Dentz 		kfree(entry);
313008ee9ebSLuiz Augusto von Dentz 	}
3146a98e383SMarcel Holtmann }
3156a98e383SMarcel Holtmann 
316744451c1SBenjamin Berg static void hci_cmd_sync_cancel_work(struct work_struct *work)
317744451c1SBenjamin Berg {
318744451c1SBenjamin Berg 	struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
319744451c1SBenjamin Berg 
320744451c1SBenjamin Berg 	cancel_delayed_work_sync(&hdev->cmd_timer);
321744451c1SBenjamin Berg 	cancel_delayed_work_sync(&hdev->ncmd_timer);
322744451c1SBenjamin Berg 	atomic_set(&hdev->cmd_cnt, 1);
323744451c1SBenjamin Berg 
324744451c1SBenjamin Berg 	wake_up_interruptible(&hdev->req_wait_q);
325744451c1SBenjamin Berg }
326744451c1SBenjamin Berg 
3278ffde2a7SBrian Gix static int hci_scan_disable_sync(struct hci_dev *hdev);
3288ffde2a7SBrian Gix static int scan_disable_sync(struct hci_dev *hdev, void *data)
3298ffde2a7SBrian Gix {
3308ffde2a7SBrian Gix 	return hci_scan_disable_sync(hdev);
3318ffde2a7SBrian Gix }
3328ffde2a7SBrian Gix 
3338ffde2a7SBrian Gix static int hci_inquiry_sync(struct hci_dev *hdev, u8 length);
3348ffde2a7SBrian Gix static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data)
3358ffde2a7SBrian Gix {
3368ffde2a7SBrian Gix 	return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN);
3378ffde2a7SBrian Gix }
3388ffde2a7SBrian Gix 
3398ffde2a7SBrian Gix static void le_scan_disable(struct work_struct *work)
3408ffde2a7SBrian Gix {
3418ffde2a7SBrian Gix 	struct hci_dev *hdev = container_of(work, struct hci_dev,
3428ffde2a7SBrian Gix 					    le_scan_disable.work);
3438ffde2a7SBrian Gix 	int status;
3448ffde2a7SBrian Gix 
3458ffde2a7SBrian Gix 	bt_dev_dbg(hdev, "");
3468ffde2a7SBrian Gix 	hci_dev_lock(hdev);
3478ffde2a7SBrian Gix 
3488ffde2a7SBrian Gix 	if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
3498ffde2a7SBrian Gix 		goto _return;
3508ffde2a7SBrian Gix 
3518ffde2a7SBrian Gix 	cancel_delayed_work(&hdev->le_scan_restart);
3528ffde2a7SBrian Gix 
3538ffde2a7SBrian Gix 	status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
3548ffde2a7SBrian Gix 	if (status) {
3558ffde2a7SBrian Gix 		bt_dev_err(hdev, "failed to disable LE scan: %d", status);
3568ffde2a7SBrian Gix 		goto _return;
3578ffde2a7SBrian Gix 	}
3588ffde2a7SBrian Gix 
3598ffde2a7SBrian Gix 	hdev->discovery.scan_start = 0;
3608ffde2a7SBrian Gix 
3618ffde2a7SBrian Gix 	/* If we were running LE only scan, change discovery state. If
3628ffde2a7SBrian Gix 	 * we were running both LE and BR/EDR inquiry simultaneously,
3638ffde2a7SBrian Gix 	 * and BR/EDR inquiry is already finished, stop discovery,
3648ffde2a7SBrian Gix 	 * otherwise BR/EDR inquiry will stop discovery when finished.
3658ffde2a7SBrian Gix 	 * If we will resolve remote device name, do not change
3668ffde2a7SBrian Gix 	 * discovery state.
3678ffde2a7SBrian Gix 	 */
3688ffde2a7SBrian Gix 
3698ffde2a7SBrian Gix 	if (hdev->discovery.type == DISCOV_TYPE_LE)
3708ffde2a7SBrian Gix 		goto discov_stopped;
3718ffde2a7SBrian Gix 
3728ffde2a7SBrian Gix 	if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
3738ffde2a7SBrian Gix 		goto _return;
3748ffde2a7SBrian Gix 
3758ffde2a7SBrian Gix 	if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
3768ffde2a7SBrian Gix 		if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
3778ffde2a7SBrian Gix 		    hdev->discovery.state != DISCOVERY_RESOLVING)
3788ffde2a7SBrian Gix 			goto discov_stopped;
3798ffde2a7SBrian Gix 
3808ffde2a7SBrian Gix 		goto _return;
3818ffde2a7SBrian Gix 	}
3828ffde2a7SBrian Gix 
3838ffde2a7SBrian Gix 	status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
3848ffde2a7SBrian Gix 	if (status) {
3858ffde2a7SBrian Gix 		bt_dev_err(hdev, "inquiry failed: status %d", status);
3868ffde2a7SBrian Gix 		goto discov_stopped;
3878ffde2a7SBrian Gix 	}
3888ffde2a7SBrian Gix 
3898ffde2a7SBrian Gix 	goto _return;
3908ffde2a7SBrian Gix 
3918ffde2a7SBrian Gix discov_stopped:
3928ffde2a7SBrian Gix 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3938ffde2a7SBrian Gix 
3948ffde2a7SBrian Gix _return:
3958ffde2a7SBrian Gix 	hci_dev_unlock(hdev);
3968ffde2a7SBrian Gix }
3978ffde2a7SBrian Gix 
39827d54b77SBrian Gix static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
39927d54b77SBrian Gix 				       u8 filter_dup);
40027d54b77SBrian Gix static int hci_le_scan_restart_sync(struct hci_dev *hdev)
40127d54b77SBrian Gix {
40227d54b77SBrian Gix 	/* If controller is not scanning we are done. */
40327d54b77SBrian Gix 	if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
40427d54b77SBrian Gix 		return 0;
40527d54b77SBrian Gix 
40627d54b77SBrian Gix 	if (hdev->scanning_paused) {
40727d54b77SBrian Gix 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
40827d54b77SBrian Gix 		return 0;
40927d54b77SBrian Gix 	}
41027d54b77SBrian Gix 
41127d54b77SBrian Gix 	hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
41227d54b77SBrian Gix 	return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE,
41327d54b77SBrian Gix 					   LE_SCAN_FILTER_DUP_ENABLE);
41427d54b77SBrian Gix }
41527d54b77SBrian Gix 
41627d54b77SBrian Gix static int le_scan_restart_sync(struct hci_dev *hdev, void *data)
41727d54b77SBrian Gix {
41827d54b77SBrian Gix 	return hci_le_scan_restart_sync(hdev);
41927d54b77SBrian Gix }
42027d54b77SBrian Gix 
42127d54b77SBrian Gix static void le_scan_restart(struct work_struct *work)
42227d54b77SBrian Gix {
42327d54b77SBrian Gix 	struct hci_dev *hdev = container_of(work, struct hci_dev,
42427d54b77SBrian Gix 					    le_scan_restart.work);
42527d54b77SBrian Gix 	unsigned long timeout, duration, scan_start, now;
42627d54b77SBrian Gix 	int status;
42727d54b77SBrian Gix 
42827d54b77SBrian Gix 	bt_dev_dbg(hdev, "");
42927d54b77SBrian Gix 
43027d54b77SBrian Gix 	hci_dev_lock(hdev);
43127d54b77SBrian Gix 
43227d54b77SBrian Gix 	status = hci_cmd_sync_queue(hdev, le_scan_restart_sync, NULL, NULL);
43327d54b77SBrian Gix 	if (status) {
43427d54b77SBrian Gix 		bt_dev_err(hdev, "failed to restart LE scan: status %d",
43527d54b77SBrian Gix 			   status);
43627d54b77SBrian Gix 		goto unlock;
43727d54b77SBrian Gix 	}
43827d54b77SBrian Gix 
43927d54b77SBrian Gix 	if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
44027d54b77SBrian Gix 	    !hdev->discovery.scan_start)
44127d54b77SBrian Gix 		goto unlock;
44227d54b77SBrian Gix 
44327d54b77SBrian Gix 	/* When the scan was started, hdev->le_scan_disable has been queued
44427d54b77SBrian Gix 	 * after duration from scan_start. During scan restart this job
44527d54b77SBrian Gix 	 * has been canceled, and we need to queue it again after proper
44627d54b77SBrian Gix 	 * timeout, to make sure that scan does not run indefinitely.
44727d54b77SBrian Gix 	 */
44827d54b77SBrian Gix 	duration = hdev->discovery.scan_duration;
44927d54b77SBrian Gix 	scan_start = hdev->discovery.scan_start;
45027d54b77SBrian Gix 	now = jiffies;
45127d54b77SBrian Gix 	if (now - scan_start <= duration) {
45227d54b77SBrian Gix 		int elapsed;
45327d54b77SBrian Gix 
45427d54b77SBrian Gix 		if (now >= scan_start)
45527d54b77SBrian Gix 			elapsed = now - scan_start;
45627d54b77SBrian Gix 		else
45727d54b77SBrian Gix 			elapsed = ULONG_MAX - scan_start + now;
45827d54b77SBrian Gix 
45927d54b77SBrian Gix 		timeout = duration - elapsed;
46027d54b77SBrian Gix 	} else {
46127d54b77SBrian Gix 		timeout = 0;
46227d54b77SBrian Gix 	}
46327d54b77SBrian Gix 
46427d54b77SBrian Gix 	queue_delayed_work(hdev->req_workqueue,
46527d54b77SBrian Gix 			   &hdev->le_scan_disable, timeout);
46627d54b77SBrian Gix 
46727d54b77SBrian Gix unlock:
46827d54b77SBrian Gix 	hci_dev_unlock(hdev);
46927d54b77SBrian Gix }
47027d54b77SBrian Gix 
471b338d917SBrian Gix static int reenable_adv_sync(struct hci_dev *hdev, void *data)
472b338d917SBrian Gix {
473b338d917SBrian Gix 	bt_dev_dbg(hdev, "");
474b338d917SBrian Gix 
475b338d917SBrian Gix 	if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
476b338d917SBrian Gix 	    list_empty(&hdev->adv_instances))
477b338d917SBrian Gix 		return 0;
478b338d917SBrian Gix 
479b338d917SBrian Gix 	if (hdev->cur_adv_instance) {
480b338d917SBrian Gix 		return hci_schedule_adv_instance_sync(hdev,
481b338d917SBrian Gix 						      hdev->cur_adv_instance,
482b338d917SBrian Gix 						      true);
483b338d917SBrian Gix 	} else {
484b338d917SBrian Gix 		if (ext_adv_capable(hdev)) {
485b338d917SBrian Gix 			hci_start_ext_adv_sync(hdev, 0x00);
486b338d917SBrian Gix 		} else {
487b338d917SBrian Gix 			hci_update_adv_data_sync(hdev, 0x00);
488b338d917SBrian Gix 			hci_update_scan_rsp_data_sync(hdev, 0x00);
489b338d917SBrian Gix 			hci_enable_advertising_sync(hdev);
490b338d917SBrian Gix 		}
491b338d917SBrian Gix 	}
492b338d917SBrian Gix 
493b338d917SBrian Gix 	return 0;
494b338d917SBrian Gix }
495b338d917SBrian Gix 
496b338d917SBrian Gix static void reenable_adv(struct work_struct *work)
497b338d917SBrian Gix {
498b338d917SBrian Gix 	struct hci_dev *hdev = container_of(work, struct hci_dev,
499b338d917SBrian Gix 					    reenable_adv_work);
500b338d917SBrian Gix 	int status;
501b338d917SBrian Gix 
502b338d917SBrian Gix 	bt_dev_dbg(hdev, "");
503b338d917SBrian Gix 
504b338d917SBrian Gix 	hci_dev_lock(hdev);
505b338d917SBrian Gix 
506b338d917SBrian Gix 	status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
507b338d917SBrian Gix 	if (status)
508b338d917SBrian Gix 		bt_dev_err(hdev, "failed to reenable ADV: %d", status);
509b338d917SBrian Gix 
510b338d917SBrian Gix 	hci_dev_unlock(hdev);
511b338d917SBrian Gix }
512b338d917SBrian Gix 
513c249ea9bSBrian Gix static void cancel_adv_timeout(struct hci_dev *hdev)
514c249ea9bSBrian Gix {
515c249ea9bSBrian Gix 	if (hdev->adv_instance_timeout) {
516c249ea9bSBrian Gix 		hdev->adv_instance_timeout = 0;
517c249ea9bSBrian Gix 		cancel_delayed_work(&hdev->adv_instance_expire);
518c249ea9bSBrian Gix 	}
519c249ea9bSBrian Gix }
520c249ea9bSBrian Gix 
521c249ea9bSBrian Gix /* For a single instance:
522c249ea9bSBrian Gix  * - force == true: The instance will be removed even when its remaining
523c249ea9bSBrian Gix  *   lifetime is not zero.
524c249ea9bSBrian Gix  * - force == false: the instance will be deactivated but kept stored unless
525c249ea9bSBrian Gix  *   the remaining lifetime is zero.
526c249ea9bSBrian Gix  *
527c249ea9bSBrian Gix  * For instance == 0x00:
528c249ea9bSBrian Gix  * - force == true: All instances will be removed regardless of their timeout
529c249ea9bSBrian Gix  *   setting.
530c249ea9bSBrian Gix  * - force == false: Only instances that have a timeout will be removed.
531c249ea9bSBrian Gix  */
532c249ea9bSBrian Gix int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
533c249ea9bSBrian Gix 				u8 instance, bool force)
534c249ea9bSBrian Gix {
535c249ea9bSBrian Gix 	struct adv_info *adv_instance, *n, *next_instance = NULL;
536c249ea9bSBrian Gix 	int err;
537c249ea9bSBrian Gix 	u8 rem_inst;
538c249ea9bSBrian Gix 
539c249ea9bSBrian Gix 	/* Cancel any timeout concerning the removed instance(s). */
540c249ea9bSBrian Gix 	if (!instance || hdev->cur_adv_instance == instance)
541c249ea9bSBrian Gix 		cancel_adv_timeout(hdev);
542c249ea9bSBrian Gix 
543c249ea9bSBrian Gix 	/* Get the next instance to advertise BEFORE we remove
544c249ea9bSBrian Gix 	 * the current one. This can be the same instance again
545c249ea9bSBrian Gix 	 * if there is only one instance.
546c249ea9bSBrian Gix 	 */
547c249ea9bSBrian Gix 	if (instance && hdev->cur_adv_instance == instance)
548c249ea9bSBrian Gix 		next_instance = hci_get_next_instance(hdev, instance);
549c249ea9bSBrian Gix 
550c249ea9bSBrian Gix 	if (instance == 0x00) {
551c249ea9bSBrian Gix 		list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
552c249ea9bSBrian Gix 					 list) {
553c249ea9bSBrian Gix 			if (!(force || adv_instance->timeout))
554c249ea9bSBrian Gix 				continue;
555c249ea9bSBrian Gix 
556c249ea9bSBrian Gix 			rem_inst = adv_instance->instance;
557c249ea9bSBrian Gix 			err = hci_remove_adv_instance(hdev, rem_inst);
558c249ea9bSBrian Gix 			if (!err)
559c249ea9bSBrian Gix 				mgmt_advertising_removed(sk, hdev, rem_inst);
560c249ea9bSBrian Gix 		}
561c249ea9bSBrian Gix 	} else {
562c249ea9bSBrian Gix 		adv_instance = hci_find_adv_instance(hdev, instance);
563c249ea9bSBrian Gix 
564c249ea9bSBrian Gix 		if (force || (adv_instance && adv_instance->timeout &&
565c249ea9bSBrian Gix 			      !adv_instance->remaining_time)) {
566c249ea9bSBrian Gix 			/* Don't advertise a removed instance. */
567c249ea9bSBrian Gix 			if (next_instance &&
568c249ea9bSBrian Gix 			    next_instance->instance == instance)
569c249ea9bSBrian Gix 				next_instance = NULL;
570c249ea9bSBrian Gix 
571c249ea9bSBrian Gix 			err = hci_remove_adv_instance(hdev, instance);
572c249ea9bSBrian Gix 			if (!err)
573c249ea9bSBrian Gix 				mgmt_advertising_removed(sk, hdev, instance);
574c249ea9bSBrian Gix 		}
575c249ea9bSBrian Gix 	}
576c249ea9bSBrian Gix 
577c249ea9bSBrian Gix 	if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
578c249ea9bSBrian Gix 		return 0;
579c249ea9bSBrian Gix 
580c249ea9bSBrian Gix 	if (next_instance && !ext_adv_capable(hdev))
581c249ea9bSBrian Gix 		return hci_schedule_adv_instance_sync(hdev,
582c249ea9bSBrian Gix 						      next_instance->instance,
583c249ea9bSBrian Gix 						      false);
584c249ea9bSBrian Gix 
585c249ea9bSBrian Gix 	return 0;
586c249ea9bSBrian Gix }
587c249ea9bSBrian Gix 
588c249ea9bSBrian Gix static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
589c249ea9bSBrian Gix {
590c249ea9bSBrian Gix 	u8 instance = *(u8 *)data;
591c249ea9bSBrian Gix 
592c249ea9bSBrian Gix 	kfree(data);
593c249ea9bSBrian Gix 
594c249ea9bSBrian Gix 	hci_clear_adv_instance_sync(hdev, NULL, instance, false);
595c249ea9bSBrian Gix 
596c249ea9bSBrian Gix 	if (list_empty(&hdev->adv_instances))
597c249ea9bSBrian Gix 		return hci_disable_advertising_sync(hdev);
598c249ea9bSBrian Gix 
599c249ea9bSBrian Gix 	return 0;
600c249ea9bSBrian Gix }
601c249ea9bSBrian Gix 
602c249ea9bSBrian Gix static void adv_timeout_expire(struct work_struct *work)
603c249ea9bSBrian Gix {
604c249ea9bSBrian Gix 	u8 *inst_ptr;
605c249ea9bSBrian Gix 	struct hci_dev *hdev = container_of(work, struct hci_dev,
606c249ea9bSBrian Gix 					    adv_instance_expire.work);
607c249ea9bSBrian Gix 
608c249ea9bSBrian Gix 	bt_dev_dbg(hdev, "");
609c249ea9bSBrian Gix 
610c249ea9bSBrian Gix 	hci_dev_lock(hdev);
611c249ea9bSBrian Gix 
612c249ea9bSBrian Gix 	hdev->adv_instance_timeout = 0;
613c249ea9bSBrian Gix 
614c249ea9bSBrian Gix 	if (hdev->cur_adv_instance == 0x00)
615c249ea9bSBrian Gix 		goto unlock;
616c249ea9bSBrian Gix 
617c249ea9bSBrian Gix 	inst_ptr = kmalloc(1, GFP_KERNEL);
618c249ea9bSBrian Gix 	if (!inst_ptr)
619c249ea9bSBrian Gix 		goto unlock;
620c249ea9bSBrian Gix 
621c249ea9bSBrian Gix 	*inst_ptr = hdev->cur_adv_instance;
622c249ea9bSBrian Gix 	hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
623c249ea9bSBrian Gix 
624c249ea9bSBrian Gix unlock:
625c249ea9bSBrian Gix 	hci_dev_unlock(hdev);
626c249ea9bSBrian Gix }
627c249ea9bSBrian Gix 
6286a98e383SMarcel Holtmann void hci_cmd_sync_init(struct hci_dev *hdev)
6296a98e383SMarcel Holtmann {
6306a98e383SMarcel Holtmann 	INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
6316a98e383SMarcel Holtmann 	INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
6326a98e383SMarcel Holtmann 	mutex_init(&hdev->cmd_sync_work_lock);
6331857c199SZhengping Jiang 	mutex_init(&hdev->unregister_lock);
634744451c1SBenjamin Berg 
635744451c1SBenjamin Berg 	INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
636b338d917SBrian Gix 	INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
6378ffde2a7SBrian Gix 	INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
63827d54b77SBrian Gix 	INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart);
639c249ea9bSBrian Gix 	INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
6406a98e383SMarcel Holtmann }
6416a98e383SMarcel Holtmann 
6426a98e383SMarcel Holtmann void hci_cmd_sync_clear(struct hci_dev *hdev)
6436a98e383SMarcel Holtmann {
6446a98e383SMarcel Holtmann 	struct hci_cmd_sync_work_entry *entry, *tmp;
6456a98e383SMarcel Holtmann 
6466a98e383SMarcel Holtmann 	cancel_work_sync(&hdev->cmd_sync_work);
647b338d917SBrian Gix 	cancel_work_sync(&hdev->reenable_adv_work);
6486a98e383SMarcel Holtmann 
6491c66bee4SMin Li 	mutex_lock(&hdev->cmd_sync_work_lock);
6506a98e383SMarcel Holtmann 	list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
6516a98e383SMarcel Holtmann 		if (entry->destroy)
6526a98e383SMarcel Holtmann 			entry->destroy(hdev, entry->data, -ECANCELED);
6536a98e383SMarcel Holtmann 
6546a98e383SMarcel Holtmann 		list_del(&entry->list);
6556a98e383SMarcel Holtmann 		kfree(entry);
6566a98e383SMarcel Holtmann 	}
6571c66bee4SMin Li 	mutex_unlock(&hdev->cmd_sync_work_lock);
6586a98e383SMarcel Holtmann }
6596a98e383SMarcel Holtmann 
660744451c1SBenjamin Berg void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
661914b08b3SBenjamin Berg {
662914b08b3SBenjamin Berg 	bt_dev_dbg(hdev, "err 0x%2.2x", err);
663914b08b3SBenjamin Berg 
664914b08b3SBenjamin Berg 	if (hdev->req_status == HCI_REQ_PEND) {
665914b08b3SBenjamin Berg 		hdev->req_result = err;
666914b08b3SBenjamin Berg 		hdev->req_status = HCI_REQ_CANCELED;
667914b08b3SBenjamin Berg 
668914b08b3SBenjamin Berg 		cancel_delayed_work_sync(&hdev->cmd_timer);
669914b08b3SBenjamin Berg 		cancel_delayed_work_sync(&hdev->ncmd_timer);
670914b08b3SBenjamin Berg 		atomic_set(&hdev->cmd_cnt, 1);
671914b08b3SBenjamin Berg 
672914b08b3SBenjamin Berg 		wake_up_interruptible(&hdev->req_wait_q);
673914b08b3SBenjamin Berg 	}
674914b08b3SBenjamin Berg }
675744451c1SBenjamin Berg 
676744451c1SBenjamin Berg void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
677744451c1SBenjamin Berg {
678744451c1SBenjamin Berg 	bt_dev_dbg(hdev, "err 0x%2.2x", err);
679744451c1SBenjamin Berg 
680744451c1SBenjamin Berg 	if (hdev->req_status == HCI_REQ_PEND) {
681744451c1SBenjamin Berg 		hdev->req_result = err;
682744451c1SBenjamin Berg 		hdev->req_status = HCI_REQ_CANCELED;
683744451c1SBenjamin Berg 
684744451c1SBenjamin Berg 		queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
685744451c1SBenjamin Berg 	}
686744451c1SBenjamin Berg }
687914b08b3SBenjamin Berg EXPORT_SYMBOL(hci_cmd_sync_cancel);
688914b08b3SBenjamin Berg 
689d883a466SLuiz Augusto von Dentz /* Submit HCI command to be run in as cmd_sync_work:
690d883a466SLuiz Augusto von Dentz  *
691d883a466SLuiz Augusto von Dentz  * - hdev must _not_ be unregistered
692d883a466SLuiz Augusto von Dentz  */
693d883a466SLuiz Augusto von Dentz int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
6946a98e383SMarcel Holtmann 			void *data, hci_cmd_sync_work_destroy_t destroy)
6956a98e383SMarcel Holtmann {
6966a98e383SMarcel Holtmann 	struct hci_cmd_sync_work_entry *entry;
6971857c199SZhengping Jiang 	int err = 0;
6986a98e383SMarcel Holtmann 
6991857c199SZhengping Jiang 	mutex_lock(&hdev->unregister_lock);
7001857c199SZhengping Jiang 	if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
7011857c199SZhengping Jiang 		err = -ENODEV;
7021857c199SZhengping Jiang 		goto unlock;
7031857c199SZhengping Jiang 	}
7040b94f265SLuiz Augusto von Dentz 
7056a98e383SMarcel Holtmann 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
7061857c199SZhengping Jiang 	if (!entry) {
7071857c199SZhengping Jiang 		err = -ENOMEM;
7081857c199SZhengping Jiang 		goto unlock;
7091857c199SZhengping Jiang 	}
7106a98e383SMarcel Holtmann 	entry->func = func;
7116a98e383SMarcel Holtmann 	entry->data = data;
7126a98e383SMarcel Holtmann 	entry->destroy = destroy;
7136a98e383SMarcel Holtmann 
7146a98e383SMarcel Holtmann 	mutex_lock(&hdev->cmd_sync_work_lock);
7156a98e383SMarcel Holtmann 	list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
7166a98e383SMarcel Holtmann 	mutex_unlock(&hdev->cmd_sync_work_lock);
7176a98e383SMarcel Holtmann 
7186a98e383SMarcel Holtmann 	queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
7196a98e383SMarcel Holtmann 
7201857c199SZhengping Jiang unlock:
7211857c199SZhengping Jiang 	mutex_unlock(&hdev->unregister_lock);
7221857c199SZhengping Jiang 	return err;
7236a98e383SMarcel Holtmann }
724d883a466SLuiz Augusto von Dentz EXPORT_SYMBOL(hci_cmd_sync_submit);
725d883a466SLuiz Augusto von Dentz 
726d883a466SLuiz Augusto von Dentz /* Queue HCI command:
727d883a466SLuiz Augusto von Dentz  *
728d883a466SLuiz Augusto von Dentz  * - hdev must be running
729d883a466SLuiz Augusto von Dentz  */
730d883a466SLuiz Augusto von Dentz int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
731d883a466SLuiz Augusto von Dentz 		       void *data, hci_cmd_sync_work_destroy_t destroy)
732d883a466SLuiz Augusto von Dentz {
733d883a466SLuiz Augusto von Dentz 	/* Only queue command if hdev is running which means it had been opened
734d883a466SLuiz Augusto von Dentz 	 * and is either on init phase or is already up.
735d883a466SLuiz Augusto von Dentz 	 */
736d883a466SLuiz Augusto von Dentz 	if (!test_bit(HCI_RUNNING, &hdev->flags))
737d883a466SLuiz Augusto von Dentz 		return -ENETDOWN;
738d883a466SLuiz Augusto von Dentz 
739d883a466SLuiz Augusto von Dentz 	return hci_cmd_sync_submit(hdev, func, data, destroy);
740d883a466SLuiz Augusto von Dentz }
7416a98e383SMarcel Holtmann EXPORT_SYMBOL(hci_cmd_sync_queue);
742161510ccSLuiz Augusto von Dentz 
743161510ccSLuiz Augusto von Dentz int hci_update_eir_sync(struct hci_dev *hdev)
744161510ccSLuiz Augusto von Dentz {
745161510ccSLuiz Augusto von Dentz 	struct hci_cp_write_eir cp;
746161510ccSLuiz Augusto von Dentz 
747161510ccSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
748161510ccSLuiz Augusto von Dentz 
749161510ccSLuiz Augusto von Dentz 	if (!hdev_is_powered(hdev))
750161510ccSLuiz Augusto von Dentz 		return 0;
751161510ccSLuiz Augusto von Dentz 
752161510ccSLuiz Augusto von Dentz 	if (!lmp_ext_inq_capable(hdev))
753161510ccSLuiz Augusto von Dentz 		return 0;
754161510ccSLuiz Augusto von Dentz 
755161510ccSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
756161510ccSLuiz Augusto von Dentz 		return 0;
757161510ccSLuiz Augusto von Dentz 
758161510ccSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
759161510ccSLuiz Augusto von Dentz 		return 0;
760161510ccSLuiz Augusto von Dentz 
761161510ccSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
762161510ccSLuiz Augusto von Dentz 
763161510ccSLuiz Augusto von Dentz 	eir_create(hdev, cp.data);
764161510ccSLuiz Augusto von Dentz 
765161510ccSLuiz Augusto von Dentz 	if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
766161510ccSLuiz Augusto von Dentz 		return 0;
767161510ccSLuiz Augusto von Dentz 
768161510ccSLuiz Augusto von Dentz 	memcpy(hdev->eir, cp.data, sizeof(cp.data));
769161510ccSLuiz Augusto von Dentz 
770161510ccSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
771161510ccSLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
772161510ccSLuiz Augusto von Dentz }
773161510ccSLuiz Augusto von Dentz 
774161510ccSLuiz Augusto von Dentz static u8 get_service_classes(struct hci_dev *hdev)
775161510ccSLuiz Augusto von Dentz {
776161510ccSLuiz Augusto von Dentz 	struct bt_uuid *uuid;
777161510ccSLuiz Augusto von Dentz 	u8 val = 0;
778161510ccSLuiz Augusto von Dentz 
779161510ccSLuiz Augusto von Dentz 	list_for_each_entry(uuid, &hdev->uuids, list)
780161510ccSLuiz Augusto von Dentz 		val |= uuid->svc_hint;
781161510ccSLuiz Augusto von Dentz 
782161510ccSLuiz Augusto von Dentz 	return val;
783161510ccSLuiz Augusto von Dentz }
784161510ccSLuiz Augusto von Dentz 
785161510ccSLuiz Augusto von Dentz int hci_update_class_sync(struct hci_dev *hdev)
786161510ccSLuiz Augusto von Dentz {
787161510ccSLuiz Augusto von Dentz 	u8 cod[3];
788161510ccSLuiz Augusto von Dentz 
789161510ccSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
790161510ccSLuiz Augusto von Dentz 
791161510ccSLuiz Augusto von Dentz 	if (!hdev_is_powered(hdev))
792161510ccSLuiz Augusto von Dentz 		return 0;
793161510ccSLuiz Augusto von Dentz 
794161510ccSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
795161510ccSLuiz Augusto von Dentz 		return 0;
796161510ccSLuiz Augusto von Dentz 
797161510ccSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
798161510ccSLuiz Augusto von Dentz 		return 0;
799161510ccSLuiz Augusto von Dentz 
800161510ccSLuiz Augusto von Dentz 	cod[0] = hdev->minor_class;
801161510ccSLuiz Augusto von Dentz 	cod[1] = hdev->major_class;
802161510ccSLuiz Augusto von Dentz 	cod[2] = get_service_classes(hdev);
803161510ccSLuiz Augusto von Dentz 
804161510ccSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
805161510ccSLuiz Augusto von Dentz 		cod[1] |= 0x20;
806161510ccSLuiz Augusto von Dentz 
807161510ccSLuiz Augusto von Dentz 	if (memcmp(cod, hdev->dev_class, 3) == 0)
808161510ccSLuiz Augusto von Dentz 		return 0;
809161510ccSLuiz Augusto von Dentz 
810161510ccSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
811161510ccSLuiz Augusto von Dentz 				     sizeof(cod), cod, HCI_CMD_TIMEOUT);
812161510ccSLuiz Augusto von Dentz }
813cba6b758SLuiz Augusto von Dentz 
814cba6b758SLuiz Augusto von Dentz static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
815cba6b758SLuiz Augusto von Dentz {
816cba6b758SLuiz Augusto von Dentz 	/* If there is no connection we are OK to advertise. */
817cba6b758SLuiz Augusto von Dentz 	if (hci_conn_num(hdev, LE_LINK) == 0)
818cba6b758SLuiz Augusto von Dentz 		return true;
819cba6b758SLuiz Augusto von Dentz 
820cba6b758SLuiz Augusto von Dentz 	/* Check le_states if there is any connection in peripheral role. */
821cba6b758SLuiz Augusto von Dentz 	if (hdev->conn_hash.le_num_peripheral > 0) {
822cba6b758SLuiz Augusto von Dentz 		/* Peripheral connection state and non connectable mode
823cba6b758SLuiz Augusto von Dentz 		 * bit 20.
824cba6b758SLuiz Augusto von Dentz 		 */
825cba6b758SLuiz Augusto von Dentz 		if (!connectable && !(hdev->le_states[2] & 0x10))
826cba6b758SLuiz Augusto von Dentz 			return false;
827cba6b758SLuiz Augusto von Dentz 
828cba6b758SLuiz Augusto von Dentz 		/* Peripheral connection state and connectable mode bit 38
829cba6b758SLuiz Augusto von Dentz 		 * and scannable bit 21.
830cba6b758SLuiz Augusto von Dentz 		 */
831cba6b758SLuiz Augusto von Dentz 		if (connectable && (!(hdev->le_states[4] & 0x40) ||
832cba6b758SLuiz Augusto von Dentz 				    !(hdev->le_states[2] & 0x20)))
833cba6b758SLuiz Augusto von Dentz 			return false;
834cba6b758SLuiz Augusto von Dentz 	}
835cba6b758SLuiz Augusto von Dentz 
836cba6b758SLuiz Augusto von Dentz 	/* Check le_states if there is any connection in central role. */
837cba6b758SLuiz Augusto von Dentz 	if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
838cba6b758SLuiz Augusto von Dentz 		/* Central connection state and non connectable mode bit 18. */
839cba6b758SLuiz Augusto von Dentz 		if (!connectable && !(hdev->le_states[2] & 0x02))
840cba6b758SLuiz Augusto von Dentz 			return false;
841cba6b758SLuiz Augusto von Dentz 
842cba6b758SLuiz Augusto von Dentz 		/* Central connection state and connectable mode bit 35 and
843cba6b758SLuiz Augusto von Dentz 		 * scannable 19.
844cba6b758SLuiz Augusto von Dentz 		 */
845cba6b758SLuiz Augusto von Dentz 		if (connectable && (!(hdev->le_states[4] & 0x08) ||
846cba6b758SLuiz Augusto von Dentz 				    !(hdev->le_states[2] & 0x08)))
847cba6b758SLuiz Augusto von Dentz 			return false;
848cba6b758SLuiz Augusto von Dentz 	}
849cba6b758SLuiz Augusto von Dentz 
850cba6b758SLuiz Augusto von Dentz 	return true;
851cba6b758SLuiz Augusto von Dentz }
852cba6b758SLuiz Augusto von Dentz 
853cba6b758SLuiz Augusto von Dentz static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
854cba6b758SLuiz Augusto von Dentz {
855cba6b758SLuiz Augusto von Dentz 	/* If privacy is not enabled don't use RPA */
856cba6b758SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
857cba6b758SLuiz Augusto von Dentz 		return false;
858cba6b758SLuiz Augusto von Dentz 
859cba6b758SLuiz Augusto von Dentz 	/* If basic privacy mode is enabled use RPA */
860cba6b758SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
861cba6b758SLuiz Augusto von Dentz 		return true;
862cba6b758SLuiz Augusto von Dentz 
863cba6b758SLuiz Augusto von Dentz 	/* If limited privacy mode is enabled don't use RPA if we're
864cba6b758SLuiz Augusto von Dentz 	 * both discoverable and bondable.
865cba6b758SLuiz Augusto von Dentz 	 */
866cba6b758SLuiz Augusto von Dentz 	if ((flags & MGMT_ADV_FLAG_DISCOV) &&
867cba6b758SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_BONDABLE))
868cba6b758SLuiz Augusto von Dentz 		return false;
869cba6b758SLuiz Augusto von Dentz 
870cba6b758SLuiz Augusto von Dentz 	/* We're neither bondable nor discoverable in the limited
871cba6b758SLuiz Augusto von Dentz 	 * privacy mode, therefore use RPA.
872cba6b758SLuiz Augusto von Dentz 	 */
873cba6b758SLuiz Augusto von Dentz 	return true;
874cba6b758SLuiz Augusto von Dentz }
875cba6b758SLuiz Augusto von Dentz 
876cba6b758SLuiz Augusto von Dentz static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
877cba6b758SLuiz Augusto von Dentz {
878cba6b758SLuiz Augusto von Dentz 	/* If we're advertising or initiating an LE connection we can't
879cba6b758SLuiz Augusto von Dentz 	 * go ahead and change the random address at this time. This is
880cba6b758SLuiz Augusto von Dentz 	 * because the eventual initiator address used for the
881cba6b758SLuiz Augusto von Dentz 	 * subsequently created connection will be undefined (some
882cba6b758SLuiz Augusto von Dentz 	 * controllers use the new address and others the one we had
883cba6b758SLuiz Augusto von Dentz 	 * when the operation started).
884cba6b758SLuiz Augusto von Dentz 	 *
885cba6b758SLuiz Augusto von Dentz 	 * In this kind of scenario skip the update and let the random
886cba6b758SLuiz Augusto von Dentz 	 * address be updated at the next cycle.
887cba6b758SLuiz Augusto von Dentz 	 */
888cba6b758SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
889cba6b758SLuiz Augusto von Dentz 	    hci_lookup_le_connect(hdev)) {
890cba6b758SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "Deferring random address update");
891cba6b758SLuiz Augusto von Dentz 		hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
892cba6b758SLuiz Augusto von Dentz 		return 0;
893cba6b758SLuiz Augusto von Dentz 	}
894cba6b758SLuiz Augusto von Dentz 
895cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
896cba6b758SLuiz Augusto von Dentz 				     6, rpa, HCI_CMD_TIMEOUT);
897cba6b758SLuiz Augusto von Dentz }
898cba6b758SLuiz Augusto von Dentz 
899cba6b758SLuiz Augusto von Dentz int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
900cba6b758SLuiz Augusto von Dentz 				   bool rpa, u8 *own_addr_type)
901cba6b758SLuiz Augusto von Dentz {
902cba6b758SLuiz Augusto von Dentz 	int err;
903cba6b758SLuiz Augusto von Dentz 
904cba6b758SLuiz Augusto von Dentz 	/* If privacy is enabled use a resolvable private address. If
905cba6b758SLuiz Augusto von Dentz 	 * current RPA has expired or there is something else than
906cba6b758SLuiz Augusto von Dentz 	 * the current RPA in use, then generate a new one.
907cba6b758SLuiz Augusto von Dentz 	 */
908cba6b758SLuiz Augusto von Dentz 	if (rpa) {
909cba6b758SLuiz Augusto von Dentz 		/* If Controller supports LL Privacy use own address type is
910cba6b758SLuiz Augusto von Dentz 		 * 0x03
911cba6b758SLuiz Augusto von Dentz 		 */
912ad383c2cSLuiz Augusto von Dentz 		if (use_ll_privacy(hdev))
913cba6b758SLuiz Augusto von Dentz 			*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
914cba6b758SLuiz Augusto von Dentz 		else
915cba6b758SLuiz Augusto von Dentz 			*own_addr_type = ADDR_LE_DEV_RANDOM;
916cba6b758SLuiz Augusto von Dentz 
917cba6b758SLuiz Augusto von Dentz 		/* Check if RPA is valid */
918cba6b758SLuiz Augusto von Dentz 		if (rpa_valid(hdev))
919cba6b758SLuiz Augusto von Dentz 			return 0;
920cba6b758SLuiz Augusto von Dentz 
921cba6b758SLuiz Augusto von Dentz 		err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
922cba6b758SLuiz Augusto von Dentz 		if (err < 0) {
923cba6b758SLuiz Augusto von Dentz 			bt_dev_err(hdev, "failed to generate new RPA");
924cba6b758SLuiz Augusto von Dentz 			return err;
925cba6b758SLuiz Augusto von Dentz 		}
926cba6b758SLuiz Augusto von Dentz 
927cba6b758SLuiz Augusto von Dentz 		err = hci_set_random_addr_sync(hdev, &hdev->rpa);
928cba6b758SLuiz Augusto von Dentz 		if (err)
929cba6b758SLuiz Augusto von Dentz 			return err;
930cba6b758SLuiz Augusto von Dentz 
931cba6b758SLuiz Augusto von Dentz 		return 0;
932cba6b758SLuiz Augusto von Dentz 	}
933cba6b758SLuiz Augusto von Dentz 
934cba6b758SLuiz Augusto von Dentz 	/* In case of required privacy without resolvable private address,
935cba6b758SLuiz Augusto von Dentz 	 * use an non-resolvable private address. This is useful for active
936cba6b758SLuiz Augusto von Dentz 	 * scanning and non-connectable advertising.
937cba6b758SLuiz Augusto von Dentz 	 */
938cba6b758SLuiz Augusto von Dentz 	if (require_privacy) {
939cba6b758SLuiz Augusto von Dentz 		bdaddr_t nrpa;
940cba6b758SLuiz Augusto von Dentz 
941cba6b758SLuiz Augusto von Dentz 		while (true) {
942cba6b758SLuiz Augusto von Dentz 			/* The non-resolvable private address is generated
943cba6b758SLuiz Augusto von Dentz 			 * from random six bytes with the two most significant
944cba6b758SLuiz Augusto von Dentz 			 * bits cleared.
945cba6b758SLuiz Augusto von Dentz 			 */
946cba6b758SLuiz Augusto von Dentz 			get_random_bytes(&nrpa, 6);
947cba6b758SLuiz Augusto von Dentz 			nrpa.b[5] &= 0x3f;
948cba6b758SLuiz Augusto von Dentz 
949cba6b758SLuiz Augusto von Dentz 			/* The non-resolvable private address shall not be
950cba6b758SLuiz Augusto von Dentz 			 * equal to the public address.
951cba6b758SLuiz Augusto von Dentz 			 */
952cba6b758SLuiz Augusto von Dentz 			if (bacmp(&hdev->bdaddr, &nrpa))
953cba6b758SLuiz Augusto von Dentz 				break;
954cba6b758SLuiz Augusto von Dentz 		}
955cba6b758SLuiz Augusto von Dentz 
956cba6b758SLuiz Augusto von Dentz 		*own_addr_type = ADDR_LE_DEV_RANDOM;
957cba6b758SLuiz Augusto von Dentz 
958cba6b758SLuiz Augusto von Dentz 		return hci_set_random_addr_sync(hdev, &nrpa);
959cba6b758SLuiz Augusto von Dentz 	}
960cba6b758SLuiz Augusto von Dentz 
961cba6b758SLuiz Augusto von Dentz 	/* If forcing static address is in use or there is no public
962cba6b758SLuiz Augusto von Dentz 	 * address use the static address as random address (but skip
963cba6b758SLuiz Augusto von Dentz 	 * the HCI command if the current random address is already the
964cba6b758SLuiz Augusto von Dentz 	 * static one.
965cba6b758SLuiz Augusto von Dentz 	 *
966cba6b758SLuiz Augusto von Dentz 	 * In case BR/EDR has been disabled on a dual-mode controller
967cba6b758SLuiz Augusto von Dentz 	 * and a static address has been configured, then use that
968cba6b758SLuiz Augusto von Dentz 	 * address instead of the public BR/EDR address.
969cba6b758SLuiz Augusto von Dentz 	 */
970cba6b758SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
971cba6b758SLuiz Augusto von Dentz 	    !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
972cba6b758SLuiz Augusto von Dentz 	    (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
973cba6b758SLuiz Augusto von Dentz 	     bacmp(&hdev->static_addr, BDADDR_ANY))) {
974cba6b758SLuiz Augusto von Dentz 		*own_addr_type = ADDR_LE_DEV_RANDOM;
975cba6b758SLuiz Augusto von Dentz 		if (bacmp(&hdev->static_addr, &hdev->random_addr))
976cba6b758SLuiz Augusto von Dentz 			return hci_set_random_addr_sync(hdev,
977cba6b758SLuiz Augusto von Dentz 							&hdev->static_addr);
978cba6b758SLuiz Augusto von Dentz 		return 0;
979cba6b758SLuiz Augusto von Dentz 	}
980cba6b758SLuiz Augusto von Dentz 
981cba6b758SLuiz Augusto von Dentz 	/* Neither privacy nor static address is being used so use a
982cba6b758SLuiz Augusto von Dentz 	 * public address.
983cba6b758SLuiz Augusto von Dentz 	 */
984cba6b758SLuiz Augusto von Dentz 	*own_addr_type = ADDR_LE_DEV_PUBLIC;
985cba6b758SLuiz Augusto von Dentz 
986cba6b758SLuiz Augusto von Dentz 	return 0;
987cba6b758SLuiz Augusto von Dentz }
988cba6b758SLuiz Augusto von Dentz 
989cba6b758SLuiz Augusto von Dentz static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
990cba6b758SLuiz Augusto von Dentz {
991cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_adv_enable *cp;
992cba6b758SLuiz Augusto von Dentz 	struct hci_cp_ext_adv_set *set;
993cba6b758SLuiz Augusto von Dentz 	u8 data[sizeof(*cp) + sizeof(*set) * 1];
994cba6b758SLuiz Augusto von Dentz 	u8 size;
995cba6b758SLuiz Augusto von Dentz 
996cba6b758SLuiz Augusto von Dentz 	/* If request specifies an instance that doesn't exist, fail */
997cba6b758SLuiz Augusto von Dentz 	if (instance > 0) {
998cba6b758SLuiz Augusto von Dentz 		struct adv_info *adv;
999cba6b758SLuiz Augusto von Dentz 
1000cba6b758SLuiz Augusto von Dentz 		adv = hci_find_adv_instance(hdev, instance);
1001cba6b758SLuiz Augusto von Dentz 		if (!adv)
1002cba6b758SLuiz Augusto von Dentz 			return -EINVAL;
1003cba6b758SLuiz Augusto von Dentz 
1004cba6b758SLuiz Augusto von Dentz 		/* If not enabled there is nothing to do */
1005cba6b758SLuiz Augusto von Dentz 		if (!adv->enabled)
1006cba6b758SLuiz Augusto von Dentz 			return 0;
1007cba6b758SLuiz Augusto von Dentz 	}
1008cba6b758SLuiz Augusto von Dentz 
1009cba6b758SLuiz Augusto von Dentz 	memset(data, 0, sizeof(data));
1010cba6b758SLuiz Augusto von Dentz 
1011cba6b758SLuiz Augusto von Dentz 	cp = (void *)data;
1012cba6b758SLuiz Augusto von Dentz 	set = (void *)cp->data;
1013cba6b758SLuiz Augusto von Dentz 
1014cba6b758SLuiz Augusto von Dentz 	/* Instance 0x00 indicates all advertising instances will be disabled */
1015cba6b758SLuiz Augusto von Dentz 	cp->num_of_sets = !!instance;
1016cba6b758SLuiz Augusto von Dentz 	cp->enable = 0x00;
1017cba6b758SLuiz Augusto von Dentz 
1018cba6b758SLuiz Augusto von Dentz 	set->handle = instance;
1019cba6b758SLuiz Augusto von Dentz 
1020cba6b758SLuiz Augusto von Dentz 	size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
1021cba6b758SLuiz Augusto von Dentz 
1022cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1023cba6b758SLuiz Augusto von Dentz 				     size, data, HCI_CMD_TIMEOUT);
1024cba6b758SLuiz Augusto von Dentz }
1025cba6b758SLuiz Augusto von Dentz 
1026cba6b758SLuiz Augusto von Dentz static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1027cba6b758SLuiz Augusto von Dentz 					    bdaddr_t *random_addr)
1028cba6b758SLuiz Augusto von Dentz {
1029cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_adv_set_rand_addr cp;
1030cba6b758SLuiz Augusto von Dentz 	int err;
1031cba6b758SLuiz Augusto von Dentz 
1032cba6b758SLuiz Augusto von Dentz 	if (!instance) {
1033cba6b758SLuiz Augusto von Dentz 		/* Instance 0x00 doesn't have an adv_info, instead it uses
1034cba6b758SLuiz Augusto von Dentz 		 * hdev->random_addr to track its address so whenever it needs
1035cba6b758SLuiz Augusto von Dentz 		 * to be updated this also set the random address since
1036cba6b758SLuiz Augusto von Dentz 		 * hdev->random_addr is shared with scan state machine.
1037cba6b758SLuiz Augusto von Dentz 		 */
1038cba6b758SLuiz Augusto von Dentz 		err = hci_set_random_addr_sync(hdev, random_addr);
1039cba6b758SLuiz Augusto von Dentz 		if (err)
1040cba6b758SLuiz Augusto von Dentz 			return err;
1041cba6b758SLuiz Augusto von Dentz 	}
1042cba6b758SLuiz Augusto von Dentz 
1043cba6b758SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1044cba6b758SLuiz Augusto von Dentz 
1045cba6b758SLuiz Augusto von Dentz 	cp.handle = instance;
1046cba6b758SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, random_addr);
1047cba6b758SLuiz Augusto von Dentz 
1048cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1049cba6b758SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1050cba6b758SLuiz Augusto von Dentz }
1051cba6b758SLuiz Augusto von Dentz 
1052cba6b758SLuiz Augusto von Dentz int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1053cba6b758SLuiz Augusto von Dentz {
1054cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_adv_params cp;
1055cba6b758SLuiz Augusto von Dentz 	bool connectable;
1056cba6b758SLuiz Augusto von Dentz 	u32 flags;
1057cba6b758SLuiz Augusto von Dentz 	bdaddr_t random_addr;
1058cba6b758SLuiz Augusto von Dentz 	u8 own_addr_type;
1059cba6b758SLuiz Augusto von Dentz 	int err;
1060cba6b758SLuiz Augusto von Dentz 	struct adv_info *adv;
1061cba6b758SLuiz Augusto von Dentz 	bool secondary_adv;
1062cba6b758SLuiz Augusto von Dentz 
1063cba6b758SLuiz Augusto von Dentz 	if (instance > 0) {
1064cba6b758SLuiz Augusto von Dentz 		adv = hci_find_adv_instance(hdev, instance);
1065cba6b758SLuiz Augusto von Dentz 		if (!adv)
1066cba6b758SLuiz Augusto von Dentz 			return -EINVAL;
1067cba6b758SLuiz Augusto von Dentz 	} else {
1068cba6b758SLuiz Augusto von Dentz 		adv = NULL;
1069cba6b758SLuiz Augusto von Dentz 	}
1070cba6b758SLuiz Augusto von Dentz 
1071cba6b758SLuiz Augusto von Dentz 	/* Updating parameters of an active instance will return a
1072cba6b758SLuiz Augusto von Dentz 	 * Command Disallowed error, so we must first disable the
1073cba6b758SLuiz Augusto von Dentz 	 * instance if it is active.
1074cba6b758SLuiz Augusto von Dentz 	 */
1075cba6b758SLuiz Augusto von Dentz 	if (adv && !adv->pending) {
1076cba6b758SLuiz Augusto von Dentz 		err = hci_disable_ext_adv_instance_sync(hdev, instance);
1077cba6b758SLuiz Augusto von Dentz 		if (err)
1078cba6b758SLuiz Augusto von Dentz 			return err;
1079cba6b758SLuiz Augusto von Dentz 	}
1080cba6b758SLuiz Augusto von Dentz 
1081cba6b758SLuiz Augusto von Dentz 	flags = hci_adv_instance_flags(hdev, instance);
1082cba6b758SLuiz Augusto von Dentz 
1083cba6b758SLuiz Augusto von Dentz 	/* If the "connectable" instance flag was not set, then choose between
1084cba6b758SLuiz Augusto von Dentz 	 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1085cba6b758SLuiz Augusto von Dentz 	 */
1086cba6b758SLuiz Augusto von Dentz 	connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1087cba6b758SLuiz Augusto von Dentz 		      mgmt_get_connectable(hdev);
1088cba6b758SLuiz Augusto von Dentz 
1089cba6b758SLuiz Augusto von Dentz 	if (!is_advertising_allowed(hdev, connectable))
1090cba6b758SLuiz Augusto von Dentz 		return -EPERM;
1091cba6b758SLuiz Augusto von Dentz 
1092cba6b758SLuiz Augusto von Dentz 	/* Set require_privacy to true only when non-connectable
1093cba6b758SLuiz Augusto von Dentz 	 * advertising is used. In that case it is fine to use a
1094cba6b758SLuiz Augusto von Dentz 	 * non-resolvable private address.
1095cba6b758SLuiz Augusto von Dentz 	 */
1096cba6b758SLuiz Augusto von Dentz 	err = hci_get_random_address(hdev, !connectable,
1097cba6b758SLuiz Augusto von Dentz 				     adv_use_rpa(hdev, flags), adv,
1098cba6b758SLuiz Augusto von Dentz 				     &own_addr_type, &random_addr);
1099cba6b758SLuiz Augusto von Dentz 	if (err < 0)
1100cba6b758SLuiz Augusto von Dentz 		return err;
1101cba6b758SLuiz Augusto von Dentz 
1102cba6b758SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1103cba6b758SLuiz Augusto von Dentz 
1104cba6b758SLuiz Augusto von Dentz 	if (adv) {
1105cba6b758SLuiz Augusto von Dentz 		hci_cpu_to_le24(adv->min_interval, cp.min_interval);
1106cba6b758SLuiz Augusto von Dentz 		hci_cpu_to_le24(adv->max_interval, cp.max_interval);
1107cba6b758SLuiz Augusto von Dentz 		cp.tx_power = adv->tx_power;
1108cba6b758SLuiz Augusto von Dentz 	} else {
1109cba6b758SLuiz Augusto von Dentz 		hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1110cba6b758SLuiz Augusto von Dentz 		hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1111cba6b758SLuiz Augusto von Dentz 		cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
1112cba6b758SLuiz Augusto von Dentz 	}
1113cba6b758SLuiz Augusto von Dentz 
1114cba6b758SLuiz Augusto von Dentz 	secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
1115cba6b758SLuiz Augusto von Dentz 
1116cba6b758SLuiz Augusto von Dentz 	if (connectable) {
1117cba6b758SLuiz Augusto von Dentz 		if (secondary_adv)
1118cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
1119cba6b758SLuiz Augusto von Dentz 		else
1120cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
1121cba6b758SLuiz Augusto von Dentz 	} else if (hci_adv_instance_is_scannable(hdev, instance) ||
1122cba6b758SLuiz Augusto von Dentz 		   (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
1123cba6b758SLuiz Augusto von Dentz 		if (secondary_adv)
1124cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
1125cba6b758SLuiz Augusto von Dentz 		else
1126cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
1127cba6b758SLuiz Augusto von Dentz 	} else {
1128cba6b758SLuiz Augusto von Dentz 		if (secondary_adv)
1129cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
1130cba6b758SLuiz Augusto von Dentz 		else
1131cba6b758SLuiz Augusto von Dentz 			cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
1132cba6b758SLuiz Augusto von Dentz 	}
1133cba6b758SLuiz Augusto von Dentz 
1134cf75ad8bSLuiz Augusto von Dentz 	/* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
1135cf75ad8bSLuiz Augusto von Dentz 	 * contains the peer’s Identity Address and the Peer_Address_Type
1136cf75ad8bSLuiz Augusto von Dentz 	 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
1137cf75ad8bSLuiz Augusto von Dentz 	 * These parameters are used to locate the corresponding local IRK in
1138cf75ad8bSLuiz Augusto von Dentz 	 * the resolving list; this IRK is used to generate their own address
1139cf75ad8bSLuiz Augusto von Dentz 	 * used in the advertisement.
1140cf75ad8bSLuiz Augusto von Dentz 	 */
1141cf75ad8bSLuiz Augusto von Dentz 	if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
1142cf75ad8bSLuiz Augusto von Dentz 		hci_copy_identity_address(hdev, &cp.peer_addr,
1143cf75ad8bSLuiz Augusto von Dentz 					  &cp.peer_addr_type);
1144cf75ad8bSLuiz Augusto von Dentz 
1145cba6b758SLuiz Augusto von Dentz 	cp.own_addr_type = own_addr_type;
1146cba6b758SLuiz Augusto von Dentz 	cp.channel_map = hdev->le_adv_channel_map;
1147cba6b758SLuiz Augusto von Dentz 	cp.handle = instance;
1148cba6b758SLuiz Augusto von Dentz 
1149cba6b758SLuiz Augusto von Dentz 	if (flags & MGMT_ADV_FLAG_SEC_2M) {
1150cba6b758SLuiz Augusto von Dentz 		cp.primary_phy = HCI_ADV_PHY_1M;
1151cba6b758SLuiz Augusto von Dentz 		cp.secondary_phy = HCI_ADV_PHY_2M;
1152cba6b758SLuiz Augusto von Dentz 	} else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
1153cba6b758SLuiz Augusto von Dentz 		cp.primary_phy = HCI_ADV_PHY_CODED;
1154cba6b758SLuiz Augusto von Dentz 		cp.secondary_phy = HCI_ADV_PHY_CODED;
1155cba6b758SLuiz Augusto von Dentz 	} else {
1156cba6b758SLuiz Augusto von Dentz 		/* In all other cases use 1M */
1157cba6b758SLuiz Augusto von Dentz 		cp.primary_phy = HCI_ADV_PHY_1M;
1158cba6b758SLuiz Augusto von Dentz 		cp.secondary_phy = HCI_ADV_PHY_1M;
1159cba6b758SLuiz Augusto von Dentz 	}
1160cba6b758SLuiz Augusto von Dentz 
1161cba6b758SLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1162cba6b758SLuiz Augusto von Dentz 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1163cba6b758SLuiz Augusto von Dentz 	if (err)
1164cba6b758SLuiz Augusto von Dentz 		return err;
1165cba6b758SLuiz Augusto von Dentz 
1166cba6b758SLuiz Augusto von Dentz 	if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
1167cba6b758SLuiz Augusto von Dentz 	     own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
1168cba6b758SLuiz Augusto von Dentz 	    bacmp(&random_addr, BDADDR_ANY)) {
1169cba6b758SLuiz Augusto von Dentz 		/* Check if random address need to be updated */
1170cba6b758SLuiz Augusto von Dentz 		if (adv) {
1171cba6b758SLuiz Augusto von Dentz 			if (!bacmp(&random_addr, &adv->random_addr))
1172cba6b758SLuiz Augusto von Dentz 				return 0;
1173cba6b758SLuiz Augusto von Dentz 		} else {
1174cba6b758SLuiz Augusto von Dentz 			if (!bacmp(&random_addr, &hdev->random_addr))
1175cba6b758SLuiz Augusto von Dentz 				return 0;
1176cba6b758SLuiz Augusto von Dentz 		}
1177cba6b758SLuiz Augusto von Dentz 
1178cba6b758SLuiz Augusto von Dentz 		return hci_set_adv_set_random_addr_sync(hdev, instance,
1179cba6b758SLuiz Augusto von Dentz 							&random_addr);
1180cba6b758SLuiz Augusto von Dentz 	}
1181cba6b758SLuiz Augusto von Dentz 
1182cba6b758SLuiz Augusto von Dentz 	return 0;
1183cba6b758SLuiz Augusto von Dentz }
1184cba6b758SLuiz Augusto von Dentz 
1185cba6b758SLuiz Augusto von Dentz static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1186cba6b758SLuiz Augusto von Dentz {
1187cba6b758SLuiz Augusto von Dentz 	struct {
1188cba6b758SLuiz Augusto von Dentz 		struct hci_cp_le_set_ext_scan_rsp_data cp;
1189cba6b758SLuiz Augusto von Dentz 		u8 data[HCI_MAX_EXT_AD_LENGTH];
1190cba6b758SLuiz Augusto von Dentz 	} pdu;
1191cba6b758SLuiz Augusto von Dentz 	u8 len;
119234a718bcSLuiz Augusto von Dentz 	struct adv_info *adv = NULL;
119334a718bcSLuiz Augusto von Dentz 	int err;
1194cba6b758SLuiz Augusto von Dentz 
1195cba6b758SLuiz Augusto von Dentz 	memset(&pdu, 0, sizeof(pdu));
1196cba6b758SLuiz Augusto von Dentz 
119734a718bcSLuiz Augusto von Dentz 	if (instance) {
119834a718bcSLuiz Augusto von Dentz 		adv = hci_find_adv_instance(hdev, instance);
119934a718bcSLuiz Augusto von Dentz 		if (!adv || !adv->scan_rsp_changed)
1200cba6b758SLuiz Augusto von Dentz 			return 0;
120134a718bcSLuiz Augusto von Dentz 	}
1202cba6b758SLuiz Augusto von Dentz 
120334a718bcSLuiz Augusto von Dentz 	len = eir_create_scan_rsp(hdev, instance, pdu.data);
1204cba6b758SLuiz Augusto von Dentz 
1205cba6b758SLuiz Augusto von Dentz 	pdu.cp.handle = instance;
1206cba6b758SLuiz Augusto von Dentz 	pdu.cp.length = len;
1207cba6b758SLuiz Augusto von Dentz 	pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1208cba6b758SLuiz Augusto von Dentz 	pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1209cba6b758SLuiz Augusto von Dentz 
121034a718bcSLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1211cba6b758SLuiz Augusto von Dentz 				    sizeof(pdu.cp) + len, &pdu.cp,
1212cba6b758SLuiz Augusto von Dentz 				    HCI_CMD_TIMEOUT);
121334a718bcSLuiz Augusto von Dentz 	if (err)
121434a718bcSLuiz Augusto von Dentz 		return err;
121534a718bcSLuiz Augusto von Dentz 
121634a718bcSLuiz Augusto von Dentz 	if (adv) {
121734a718bcSLuiz Augusto von Dentz 		adv->scan_rsp_changed = false;
121834a718bcSLuiz Augusto von Dentz 	} else {
121934a718bcSLuiz Augusto von Dentz 		memcpy(hdev->scan_rsp_data, pdu.data, len);
122034a718bcSLuiz Augusto von Dentz 		hdev->scan_rsp_data_len = len;
122134a718bcSLuiz Augusto von Dentz 	}
122234a718bcSLuiz Augusto von Dentz 
122334a718bcSLuiz Augusto von Dentz 	return 0;
1224cba6b758SLuiz Augusto von Dentz }
1225cba6b758SLuiz Augusto von Dentz 
1226cba6b758SLuiz Augusto von Dentz static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1227cba6b758SLuiz Augusto von Dentz {
1228cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_scan_rsp_data cp;
1229cba6b758SLuiz Augusto von Dentz 	u8 len;
1230cba6b758SLuiz Augusto von Dentz 
1231cba6b758SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1232cba6b758SLuiz Augusto von Dentz 
1233cba6b758SLuiz Augusto von Dentz 	len = eir_create_scan_rsp(hdev, instance, cp.data);
1234cba6b758SLuiz Augusto von Dentz 
1235cba6b758SLuiz Augusto von Dentz 	if (hdev->scan_rsp_data_len == len &&
1236cba6b758SLuiz Augusto von Dentz 	    !memcmp(cp.data, hdev->scan_rsp_data, len))
1237cba6b758SLuiz Augusto von Dentz 		return 0;
1238cba6b758SLuiz Augusto von Dentz 
1239cba6b758SLuiz Augusto von Dentz 	memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1240cba6b758SLuiz Augusto von Dentz 	hdev->scan_rsp_data_len = len;
1241cba6b758SLuiz Augusto von Dentz 
1242cba6b758SLuiz Augusto von Dentz 	cp.length = len;
1243cba6b758SLuiz Augusto von Dentz 
1244cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1245cba6b758SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1246cba6b758SLuiz Augusto von Dentz }
1247cba6b758SLuiz Augusto von Dentz 
1248cba6b758SLuiz Augusto von Dentz int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1249cba6b758SLuiz Augusto von Dentz {
1250cba6b758SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1251cba6b758SLuiz Augusto von Dentz 		return 0;
1252cba6b758SLuiz Augusto von Dentz 
1253cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1254cba6b758SLuiz Augusto von Dentz 		return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1255cba6b758SLuiz Augusto von Dentz 
1256cba6b758SLuiz Augusto von Dentz 	return __hci_set_scan_rsp_data_sync(hdev, instance);
1257cba6b758SLuiz Augusto von Dentz }
1258cba6b758SLuiz Augusto von Dentz 
1259cba6b758SLuiz Augusto von Dentz int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1260cba6b758SLuiz Augusto von Dentz {
1261cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_adv_enable *cp;
1262cba6b758SLuiz Augusto von Dentz 	struct hci_cp_ext_adv_set *set;
1263cba6b758SLuiz Augusto von Dentz 	u8 data[sizeof(*cp) + sizeof(*set) * 1];
1264cba6b758SLuiz Augusto von Dentz 	struct adv_info *adv;
1265cba6b758SLuiz Augusto von Dentz 
1266cba6b758SLuiz Augusto von Dentz 	if (instance > 0) {
1267cba6b758SLuiz Augusto von Dentz 		adv = hci_find_adv_instance(hdev, instance);
1268cba6b758SLuiz Augusto von Dentz 		if (!adv)
1269cba6b758SLuiz Augusto von Dentz 			return -EINVAL;
1270cba6b758SLuiz Augusto von Dentz 		/* If already enabled there is nothing to do */
1271cba6b758SLuiz Augusto von Dentz 		if (adv->enabled)
1272cba6b758SLuiz Augusto von Dentz 			return 0;
1273cba6b758SLuiz Augusto von Dentz 	} else {
1274cba6b758SLuiz Augusto von Dentz 		adv = NULL;
1275cba6b758SLuiz Augusto von Dentz 	}
1276cba6b758SLuiz Augusto von Dentz 
1277cba6b758SLuiz Augusto von Dentz 	cp = (void *)data;
1278cba6b758SLuiz Augusto von Dentz 	set = (void *)cp->data;
1279cba6b758SLuiz Augusto von Dentz 
1280cba6b758SLuiz Augusto von Dentz 	memset(cp, 0, sizeof(*cp));
1281cba6b758SLuiz Augusto von Dentz 
1282cba6b758SLuiz Augusto von Dentz 	cp->enable = 0x01;
1283cba6b758SLuiz Augusto von Dentz 	cp->num_of_sets = 0x01;
1284cba6b758SLuiz Augusto von Dentz 
1285cba6b758SLuiz Augusto von Dentz 	memset(set, 0, sizeof(*set));
1286cba6b758SLuiz Augusto von Dentz 
1287cba6b758SLuiz Augusto von Dentz 	set->handle = instance;
1288cba6b758SLuiz Augusto von Dentz 
1289cba6b758SLuiz Augusto von Dentz 	/* Set duration per instance since controller is responsible for
1290cba6b758SLuiz Augusto von Dentz 	 * scheduling it.
1291cba6b758SLuiz Augusto von Dentz 	 */
1292f16a491cSLuiz Augusto von Dentz 	if (adv && adv->timeout) {
1293cba6b758SLuiz Augusto von Dentz 		u16 duration = adv->timeout * MSEC_PER_SEC;
1294cba6b758SLuiz Augusto von Dentz 
1295cba6b758SLuiz Augusto von Dentz 		/* Time = N * 10 ms */
1296cba6b758SLuiz Augusto von Dentz 		set->duration = cpu_to_le16(duration / 10);
1297cba6b758SLuiz Augusto von Dentz 	}
1298cba6b758SLuiz Augusto von Dentz 
1299cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1300cba6b758SLuiz Augusto von Dentz 				     sizeof(*cp) +
1301cba6b758SLuiz Augusto von Dentz 				     sizeof(*set) * cp->num_of_sets,
1302cba6b758SLuiz Augusto von Dentz 				     data, HCI_CMD_TIMEOUT);
1303cba6b758SLuiz Augusto von Dentz }
1304cba6b758SLuiz Augusto von Dentz 
1305cba6b758SLuiz Augusto von Dentz int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1306cba6b758SLuiz Augusto von Dentz {
1307cba6b758SLuiz Augusto von Dentz 	int err;
1308cba6b758SLuiz Augusto von Dentz 
1309cba6b758SLuiz Augusto von Dentz 	err = hci_setup_ext_adv_instance_sync(hdev, instance);
1310cba6b758SLuiz Augusto von Dentz 	if (err)
1311cba6b758SLuiz Augusto von Dentz 		return err;
1312cba6b758SLuiz Augusto von Dentz 
1313cba6b758SLuiz Augusto von Dentz 	err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1314cba6b758SLuiz Augusto von Dentz 	if (err)
1315cba6b758SLuiz Augusto von Dentz 		return err;
1316cba6b758SLuiz Augusto von Dentz 
1317cba6b758SLuiz Augusto von Dentz 	return hci_enable_ext_advertising_sync(hdev, instance);
1318cba6b758SLuiz Augusto von Dentz }
1319cba6b758SLuiz Augusto von Dentz 
1320eca0ae4aSLuiz Augusto von Dentz static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1321eca0ae4aSLuiz Augusto von Dentz {
1322eca0ae4aSLuiz Augusto von Dentz 	struct hci_cp_le_set_per_adv_enable cp;
13236a42e9bfSIulia Tanasescu 	struct adv_info *adv = NULL;
1324eca0ae4aSLuiz Augusto von Dentz 
1325eca0ae4aSLuiz Augusto von Dentz 	/* If periodic advertising already disabled there is nothing to do. */
13266a42e9bfSIulia Tanasescu 	adv = hci_find_adv_instance(hdev, instance);
13276a42e9bfSIulia Tanasescu 	if (!adv || !adv->periodic || !adv->enabled)
1328eca0ae4aSLuiz Augusto von Dentz 		return 0;
1329eca0ae4aSLuiz Augusto von Dentz 
1330eca0ae4aSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1331eca0ae4aSLuiz Augusto von Dentz 
1332eca0ae4aSLuiz Augusto von Dentz 	cp.enable = 0x00;
1333eca0ae4aSLuiz Augusto von Dentz 	cp.handle = instance;
1334eca0ae4aSLuiz Augusto von Dentz 
1335eca0ae4aSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1336eca0ae4aSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1337eca0ae4aSLuiz Augusto von Dentz }
1338eca0ae4aSLuiz Augusto von Dentz 
1339eca0ae4aSLuiz Augusto von Dentz static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1340eca0ae4aSLuiz Augusto von Dentz 				       u16 min_interval, u16 max_interval)
1341eca0ae4aSLuiz Augusto von Dentz {
1342eca0ae4aSLuiz Augusto von Dentz 	struct hci_cp_le_set_per_adv_params cp;
1343eca0ae4aSLuiz Augusto von Dentz 
1344eca0ae4aSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1345eca0ae4aSLuiz Augusto von Dentz 
1346eca0ae4aSLuiz Augusto von Dentz 	if (!min_interval)
1347eca0ae4aSLuiz Augusto von Dentz 		min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1348eca0ae4aSLuiz Augusto von Dentz 
1349eca0ae4aSLuiz Augusto von Dentz 	if (!max_interval)
1350eca0ae4aSLuiz Augusto von Dentz 		max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1351eca0ae4aSLuiz Augusto von Dentz 
1352eca0ae4aSLuiz Augusto von Dentz 	cp.handle = instance;
1353eca0ae4aSLuiz Augusto von Dentz 	cp.min_interval = cpu_to_le16(min_interval);
1354eca0ae4aSLuiz Augusto von Dentz 	cp.max_interval = cpu_to_le16(max_interval);
1355eca0ae4aSLuiz Augusto von Dentz 	cp.periodic_properties = 0x0000;
1356eca0ae4aSLuiz Augusto von Dentz 
1357eca0ae4aSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1358eca0ae4aSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1359eca0ae4aSLuiz Augusto von Dentz }
1360eca0ae4aSLuiz Augusto von Dentz 
1361eca0ae4aSLuiz Augusto von Dentz static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1362eca0ae4aSLuiz Augusto von Dentz {
1363eca0ae4aSLuiz Augusto von Dentz 	struct {
1364eca0ae4aSLuiz Augusto von Dentz 		struct hci_cp_le_set_per_adv_data cp;
1365eca0ae4aSLuiz Augusto von Dentz 		u8 data[HCI_MAX_PER_AD_LENGTH];
1366eca0ae4aSLuiz Augusto von Dentz 	} pdu;
1367eca0ae4aSLuiz Augusto von Dentz 	u8 len;
1368eca0ae4aSLuiz Augusto von Dentz 
1369eca0ae4aSLuiz Augusto von Dentz 	memset(&pdu, 0, sizeof(pdu));
1370eca0ae4aSLuiz Augusto von Dentz 
1371eca0ae4aSLuiz Augusto von Dentz 	if (instance) {
1372eca0ae4aSLuiz Augusto von Dentz 		struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1373eca0ae4aSLuiz Augusto von Dentz 
1374eca0ae4aSLuiz Augusto von Dentz 		if (!adv || !adv->periodic)
1375eca0ae4aSLuiz Augusto von Dentz 			return 0;
1376eca0ae4aSLuiz Augusto von Dentz 	}
1377eca0ae4aSLuiz Augusto von Dentz 
1378eca0ae4aSLuiz Augusto von Dentz 	len = eir_create_per_adv_data(hdev, instance, pdu.data);
1379eca0ae4aSLuiz Augusto von Dentz 
1380eca0ae4aSLuiz Augusto von Dentz 	pdu.cp.length = len;
1381eca0ae4aSLuiz Augusto von Dentz 	pdu.cp.handle = instance;
1382eca0ae4aSLuiz Augusto von Dentz 	pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1383eca0ae4aSLuiz Augusto von Dentz 
1384eca0ae4aSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1385eca0ae4aSLuiz Augusto von Dentz 				     sizeof(pdu.cp) + len, &pdu,
1386eca0ae4aSLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
1387eca0ae4aSLuiz Augusto von Dentz }
1388eca0ae4aSLuiz Augusto von Dentz 
1389eca0ae4aSLuiz Augusto von Dentz static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1390eca0ae4aSLuiz Augusto von Dentz {
1391eca0ae4aSLuiz Augusto von Dentz 	struct hci_cp_le_set_per_adv_enable cp;
13926a42e9bfSIulia Tanasescu 	struct adv_info *adv = NULL;
1393eca0ae4aSLuiz Augusto von Dentz 
1394eca0ae4aSLuiz Augusto von Dentz 	/* If periodic advertising already enabled there is nothing to do. */
13956a42e9bfSIulia Tanasescu 	adv = hci_find_adv_instance(hdev, instance);
13966a42e9bfSIulia Tanasescu 	if (adv && adv->periodic && adv->enabled)
1397eca0ae4aSLuiz Augusto von Dentz 		return 0;
1398eca0ae4aSLuiz Augusto von Dentz 
1399eca0ae4aSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1400eca0ae4aSLuiz Augusto von Dentz 
1401eca0ae4aSLuiz Augusto von Dentz 	cp.enable = 0x01;
1402eca0ae4aSLuiz Augusto von Dentz 	cp.handle = instance;
1403eca0ae4aSLuiz Augusto von Dentz 
1404eca0ae4aSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1405eca0ae4aSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1406eca0ae4aSLuiz Augusto von Dentz }
1407eca0ae4aSLuiz Augusto von Dentz 
1408eca0ae4aSLuiz Augusto von Dentz /* Checks if periodic advertising data contains a Basic Announcement and if it
1409eca0ae4aSLuiz Augusto von Dentz  * does generates a Broadcast ID and add Broadcast Announcement.
1410eca0ae4aSLuiz Augusto von Dentz  */
1411eca0ae4aSLuiz Augusto von Dentz static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1412eca0ae4aSLuiz Augusto von Dentz {
1413eca0ae4aSLuiz Augusto von Dentz 	u8 bid[3];
1414eca0ae4aSLuiz Augusto von Dentz 	u8 ad[4 + 3];
1415eca0ae4aSLuiz Augusto von Dentz 
1416eca0ae4aSLuiz Augusto von Dentz 	/* Skip if NULL adv as instance 0x00 is used for general purpose
1417eca0ae4aSLuiz Augusto von Dentz 	 * advertising so it cannot used for the likes of Broadcast Announcement
1418eca0ae4aSLuiz Augusto von Dentz 	 * as it can be overwritten at any point.
1419eca0ae4aSLuiz Augusto von Dentz 	 */
1420eca0ae4aSLuiz Augusto von Dentz 	if (!adv)
1421eca0ae4aSLuiz Augusto von Dentz 		return 0;
1422eca0ae4aSLuiz Augusto von Dentz 
1423eca0ae4aSLuiz Augusto von Dentz 	/* Check if PA data doesn't contains a Basic Audio Announcement then
1424eca0ae4aSLuiz Augusto von Dentz 	 * there is nothing to do.
1425eca0ae4aSLuiz Augusto von Dentz 	 */
1426eca0ae4aSLuiz Augusto von Dentz 	if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1427eca0ae4aSLuiz Augusto von Dentz 				  0x1851, NULL))
1428eca0ae4aSLuiz Augusto von Dentz 		return 0;
1429eca0ae4aSLuiz Augusto von Dentz 
1430eca0ae4aSLuiz Augusto von Dentz 	/* Check if advertising data already has a Broadcast Announcement since
1431eca0ae4aSLuiz Augusto von Dentz 	 * the process may want to control the Broadcast ID directly and in that
1432eca0ae4aSLuiz Augusto von Dentz 	 * case the kernel shall no interfere.
1433eca0ae4aSLuiz Augusto von Dentz 	 */
1434eca0ae4aSLuiz Augusto von Dentz 	if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1435eca0ae4aSLuiz Augusto von Dentz 				 NULL))
1436eca0ae4aSLuiz Augusto von Dentz 		return 0;
1437eca0ae4aSLuiz Augusto von Dentz 
1438eca0ae4aSLuiz Augusto von Dentz 	/* Generate Broadcast ID */
1439eca0ae4aSLuiz Augusto von Dentz 	get_random_bytes(bid, sizeof(bid));
1440eca0ae4aSLuiz Augusto von Dentz 	eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1441eca0ae4aSLuiz Augusto von Dentz 	hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1442eca0ae4aSLuiz Augusto von Dentz 
1443eca0ae4aSLuiz Augusto von Dentz 	return hci_update_adv_data_sync(hdev, adv->instance);
1444eca0ae4aSLuiz Augusto von Dentz }
1445eca0ae4aSLuiz Augusto von Dentz 
1446eca0ae4aSLuiz Augusto von Dentz int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1447eca0ae4aSLuiz Augusto von Dentz 			   u8 *data, u32 flags, u16 min_interval,
1448eca0ae4aSLuiz Augusto von Dentz 			   u16 max_interval, u16 sync_interval)
1449eca0ae4aSLuiz Augusto von Dentz {
1450eca0ae4aSLuiz Augusto von Dentz 	struct adv_info *adv = NULL;
1451eca0ae4aSLuiz Augusto von Dentz 	int err;
1452eca0ae4aSLuiz Augusto von Dentz 	bool added = false;
1453eca0ae4aSLuiz Augusto von Dentz 
1454eca0ae4aSLuiz Augusto von Dentz 	hci_disable_per_advertising_sync(hdev, instance);
1455eca0ae4aSLuiz Augusto von Dentz 
1456eca0ae4aSLuiz Augusto von Dentz 	if (instance) {
1457eca0ae4aSLuiz Augusto von Dentz 		adv = hci_find_adv_instance(hdev, instance);
1458eca0ae4aSLuiz Augusto von Dentz 		/* Create an instance if that could not be found */
1459eca0ae4aSLuiz Augusto von Dentz 		if (!adv) {
1460eca0ae4aSLuiz Augusto von Dentz 			adv = hci_add_per_instance(hdev, instance, flags,
1461eca0ae4aSLuiz Augusto von Dentz 						   data_len, data,
1462eca0ae4aSLuiz Augusto von Dentz 						   sync_interval,
1463eca0ae4aSLuiz Augusto von Dentz 						   sync_interval);
1464eca0ae4aSLuiz Augusto von Dentz 			if (IS_ERR(adv))
1465eca0ae4aSLuiz Augusto von Dentz 				return PTR_ERR(adv);
14666a42e9bfSIulia Tanasescu 			adv->pending = false;
1467eca0ae4aSLuiz Augusto von Dentz 			added = true;
1468eca0ae4aSLuiz Augusto von Dentz 		}
1469eca0ae4aSLuiz Augusto von Dentz 	}
1470eca0ae4aSLuiz Augusto von Dentz 
14716a42e9bfSIulia Tanasescu 	/* Start advertising */
1472eca0ae4aSLuiz Augusto von Dentz 	err = hci_start_ext_adv_sync(hdev, instance);
1473eca0ae4aSLuiz Augusto von Dentz 	if (err < 0)
1474eca0ae4aSLuiz Augusto von Dentz 		goto fail;
1475eca0ae4aSLuiz Augusto von Dentz 
1476eca0ae4aSLuiz Augusto von Dentz 	err = hci_adv_bcast_annoucement(hdev, adv);
1477eca0ae4aSLuiz Augusto von Dentz 	if (err < 0)
1478eca0ae4aSLuiz Augusto von Dentz 		goto fail;
1479eca0ae4aSLuiz Augusto von Dentz 
1480eca0ae4aSLuiz Augusto von Dentz 	err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1481eca0ae4aSLuiz Augusto von Dentz 					  max_interval);
1482eca0ae4aSLuiz Augusto von Dentz 	if (err < 0)
1483eca0ae4aSLuiz Augusto von Dentz 		goto fail;
1484eca0ae4aSLuiz Augusto von Dentz 
1485eca0ae4aSLuiz Augusto von Dentz 	err = hci_set_per_adv_data_sync(hdev, instance);
1486eca0ae4aSLuiz Augusto von Dentz 	if (err < 0)
1487eca0ae4aSLuiz Augusto von Dentz 		goto fail;
1488eca0ae4aSLuiz Augusto von Dentz 
1489eca0ae4aSLuiz Augusto von Dentz 	err = hci_enable_per_advertising_sync(hdev, instance);
1490eca0ae4aSLuiz Augusto von Dentz 	if (err < 0)
1491eca0ae4aSLuiz Augusto von Dentz 		goto fail;
1492eca0ae4aSLuiz Augusto von Dentz 
1493eca0ae4aSLuiz Augusto von Dentz 	return 0;
1494eca0ae4aSLuiz Augusto von Dentz 
1495eca0ae4aSLuiz Augusto von Dentz fail:
1496eca0ae4aSLuiz Augusto von Dentz 	if (added)
1497eca0ae4aSLuiz Augusto von Dentz 		hci_remove_adv_instance(hdev, instance);
1498eca0ae4aSLuiz Augusto von Dentz 
1499eca0ae4aSLuiz Augusto von Dentz 	return err;
1500eca0ae4aSLuiz Augusto von Dentz }
1501eca0ae4aSLuiz Augusto von Dentz 
1502cba6b758SLuiz Augusto von Dentz static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1503cba6b758SLuiz Augusto von Dentz {
1504cba6b758SLuiz Augusto von Dentz 	int err;
1505cba6b758SLuiz Augusto von Dentz 
1506cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1507cba6b758SLuiz Augusto von Dentz 		return hci_start_ext_adv_sync(hdev, instance);
1508cba6b758SLuiz Augusto von Dentz 
1509cba6b758SLuiz Augusto von Dentz 	err = hci_update_adv_data_sync(hdev, instance);
1510cba6b758SLuiz Augusto von Dentz 	if (err)
1511cba6b758SLuiz Augusto von Dentz 		return err;
1512cba6b758SLuiz Augusto von Dentz 
1513cba6b758SLuiz Augusto von Dentz 	err = hci_update_scan_rsp_data_sync(hdev, instance);
1514cba6b758SLuiz Augusto von Dentz 	if (err)
1515cba6b758SLuiz Augusto von Dentz 		return err;
1516cba6b758SLuiz Augusto von Dentz 
1517cba6b758SLuiz Augusto von Dentz 	return hci_enable_advertising_sync(hdev);
1518cba6b758SLuiz Augusto von Dentz }
1519cba6b758SLuiz Augusto von Dentz 
1520cba6b758SLuiz Augusto von Dentz int hci_enable_advertising_sync(struct hci_dev *hdev)
1521cba6b758SLuiz Augusto von Dentz {
1522cba6b758SLuiz Augusto von Dentz 	struct adv_info *adv_instance;
1523cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_adv_param cp;
1524cba6b758SLuiz Augusto von Dentz 	u8 own_addr_type, enable = 0x01;
1525cba6b758SLuiz Augusto von Dentz 	bool connectable;
1526cba6b758SLuiz Augusto von Dentz 	u16 adv_min_interval, adv_max_interval;
1527cba6b758SLuiz Augusto von Dentz 	u32 flags;
1528cba6b758SLuiz Augusto von Dentz 	u8 status;
1529cba6b758SLuiz Augusto von Dentz 
1530ad383c2cSLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1531ad383c2cSLuiz Augusto von Dentz 		return hci_enable_ext_advertising_sync(hdev,
1532ad383c2cSLuiz Augusto von Dentz 						       hdev->cur_adv_instance);
1533ad383c2cSLuiz Augusto von Dentz 
1534cba6b758SLuiz Augusto von Dentz 	flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1535cba6b758SLuiz Augusto von Dentz 	adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1536cba6b758SLuiz Augusto von Dentz 
1537cba6b758SLuiz Augusto von Dentz 	/* If the "connectable" instance flag was not set, then choose between
1538cba6b758SLuiz Augusto von Dentz 	 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1539cba6b758SLuiz Augusto von Dentz 	 */
1540cba6b758SLuiz Augusto von Dentz 	connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1541cba6b758SLuiz Augusto von Dentz 		      mgmt_get_connectable(hdev);
1542cba6b758SLuiz Augusto von Dentz 
1543cba6b758SLuiz Augusto von Dentz 	if (!is_advertising_allowed(hdev, connectable))
1544cba6b758SLuiz Augusto von Dentz 		return -EINVAL;
1545cba6b758SLuiz Augusto von Dentz 
1546cba6b758SLuiz Augusto von Dentz 	status = hci_disable_advertising_sync(hdev);
1547cba6b758SLuiz Augusto von Dentz 	if (status)
1548cba6b758SLuiz Augusto von Dentz 		return status;
1549cba6b758SLuiz Augusto von Dentz 
1550cba6b758SLuiz Augusto von Dentz 	/* Clear the HCI_LE_ADV bit temporarily so that the
1551cba6b758SLuiz Augusto von Dentz 	 * hci_update_random_address knows that it's safe to go ahead
1552cba6b758SLuiz Augusto von Dentz 	 * and write a new random address. The flag will be set back on
1553cba6b758SLuiz Augusto von Dentz 	 * as soon as the SET_ADV_ENABLE HCI command completes.
1554cba6b758SLuiz Augusto von Dentz 	 */
1555cba6b758SLuiz Augusto von Dentz 	hci_dev_clear_flag(hdev, HCI_LE_ADV);
1556cba6b758SLuiz Augusto von Dentz 
1557cba6b758SLuiz Augusto von Dentz 	/* Set require_privacy to true only when non-connectable
1558cba6b758SLuiz Augusto von Dentz 	 * advertising is used. In that case it is fine to use a
1559cba6b758SLuiz Augusto von Dentz 	 * non-resolvable private address.
1560cba6b758SLuiz Augusto von Dentz 	 */
1561cba6b758SLuiz Augusto von Dentz 	status = hci_update_random_address_sync(hdev, !connectable,
1562cba6b758SLuiz Augusto von Dentz 						adv_use_rpa(hdev, flags),
1563cba6b758SLuiz Augusto von Dentz 						&own_addr_type);
1564cba6b758SLuiz Augusto von Dentz 	if (status)
1565cba6b758SLuiz Augusto von Dentz 		return status;
1566cba6b758SLuiz Augusto von Dentz 
1567cba6b758SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1568cba6b758SLuiz Augusto von Dentz 
1569cba6b758SLuiz Augusto von Dentz 	if (adv_instance) {
1570cba6b758SLuiz Augusto von Dentz 		adv_min_interval = adv_instance->min_interval;
1571cba6b758SLuiz Augusto von Dentz 		adv_max_interval = adv_instance->max_interval;
1572cba6b758SLuiz Augusto von Dentz 	} else {
1573cba6b758SLuiz Augusto von Dentz 		adv_min_interval = hdev->le_adv_min_interval;
1574cba6b758SLuiz Augusto von Dentz 		adv_max_interval = hdev->le_adv_max_interval;
1575cba6b758SLuiz Augusto von Dentz 	}
1576cba6b758SLuiz Augusto von Dentz 
1577cba6b758SLuiz Augusto von Dentz 	if (connectable) {
1578cba6b758SLuiz Augusto von Dentz 		cp.type = LE_ADV_IND;
1579cba6b758SLuiz Augusto von Dentz 	} else {
1580cba6b758SLuiz Augusto von Dentz 		if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1581cba6b758SLuiz Augusto von Dentz 			cp.type = LE_ADV_SCAN_IND;
1582cba6b758SLuiz Augusto von Dentz 		else
1583cba6b758SLuiz Augusto von Dentz 			cp.type = LE_ADV_NONCONN_IND;
1584cba6b758SLuiz Augusto von Dentz 
1585cba6b758SLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1586cba6b758SLuiz Augusto von Dentz 		    hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1587cba6b758SLuiz Augusto von Dentz 			adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1588cba6b758SLuiz Augusto von Dentz 			adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1589cba6b758SLuiz Augusto von Dentz 		}
1590cba6b758SLuiz Augusto von Dentz 	}
1591cba6b758SLuiz Augusto von Dentz 
1592cba6b758SLuiz Augusto von Dentz 	cp.min_interval = cpu_to_le16(adv_min_interval);
1593cba6b758SLuiz Augusto von Dentz 	cp.max_interval = cpu_to_le16(adv_max_interval);
1594cba6b758SLuiz Augusto von Dentz 	cp.own_address_type = own_addr_type;
1595cba6b758SLuiz Augusto von Dentz 	cp.channel_map = hdev->le_adv_channel_map;
1596cba6b758SLuiz Augusto von Dentz 
1597cba6b758SLuiz Augusto von Dentz 	status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1598cba6b758SLuiz Augusto von Dentz 				       sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1599cba6b758SLuiz Augusto von Dentz 	if (status)
1600cba6b758SLuiz Augusto von Dentz 		return status;
1601cba6b758SLuiz Augusto von Dentz 
1602cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1603cba6b758SLuiz Augusto von Dentz 				     sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1604cba6b758SLuiz Augusto von Dentz }
1605cba6b758SLuiz Augusto von Dentz 
1606abfeea47SLuiz Augusto von Dentz static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1607abfeea47SLuiz Augusto von Dentz {
1608abfeea47SLuiz Augusto von Dentz 	return hci_enable_advertising_sync(hdev);
1609abfeea47SLuiz Augusto von Dentz }
1610abfeea47SLuiz Augusto von Dentz 
1611abfeea47SLuiz Augusto von Dentz int hci_enable_advertising(struct hci_dev *hdev)
1612abfeea47SLuiz Augusto von Dentz {
1613abfeea47SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1614abfeea47SLuiz Augusto von Dentz 	    list_empty(&hdev->adv_instances))
1615abfeea47SLuiz Augusto von Dentz 		return 0;
1616abfeea47SLuiz Augusto von Dentz 
1617abfeea47SLuiz Augusto von Dentz 	return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1618abfeea47SLuiz Augusto von Dentz }
1619abfeea47SLuiz Augusto von Dentz 
1620abfeea47SLuiz Augusto von Dentz int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1621cba6b758SLuiz Augusto von Dentz 				     struct sock *sk)
1622cba6b758SLuiz Augusto von Dentz {
1623cba6b758SLuiz Augusto von Dentz 	int err;
1624cba6b758SLuiz Augusto von Dentz 
1625cba6b758SLuiz Augusto von Dentz 	if (!ext_adv_capable(hdev))
1626cba6b758SLuiz Augusto von Dentz 		return 0;
1627cba6b758SLuiz Augusto von Dentz 
1628cba6b758SLuiz Augusto von Dentz 	err = hci_disable_ext_adv_instance_sync(hdev, instance);
1629cba6b758SLuiz Augusto von Dentz 	if (err)
1630cba6b758SLuiz Augusto von Dentz 		return err;
1631cba6b758SLuiz Augusto von Dentz 
1632cba6b758SLuiz Augusto von Dentz 	/* If request specifies an instance that doesn't exist, fail */
1633cba6b758SLuiz Augusto von Dentz 	if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1634cba6b758SLuiz Augusto von Dentz 		return -EINVAL;
1635cba6b758SLuiz Augusto von Dentz 
1636cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1637cba6b758SLuiz Augusto von Dentz 					sizeof(instance), &instance, 0,
1638cba6b758SLuiz Augusto von Dentz 					HCI_CMD_TIMEOUT, sk);
1639cba6b758SLuiz Augusto von Dentz }
1640cba6b758SLuiz Augusto von Dentz 
1641eca0ae4aSLuiz Augusto von Dentz static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1642eca0ae4aSLuiz Augusto von Dentz {
1643eca0ae4aSLuiz Augusto von Dentz 	struct adv_info *adv = data;
1644eca0ae4aSLuiz Augusto von Dentz 	u8 instance = 0;
1645eca0ae4aSLuiz Augusto von Dentz 
1646eca0ae4aSLuiz Augusto von Dentz 	if (adv)
1647eca0ae4aSLuiz Augusto von Dentz 		instance = adv->instance;
1648eca0ae4aSLuiz Augusto von Dentz 
1649eca0ae4aSLuiz Augusto von Dentz 	return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1650eca0ae4aSLuiz Augusto von Dentz }
1651eca0ae4aSLuiz Augusto von Dentz 
1652eca0ae4aSLuiz Augusto von Dentz int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1653eca0ae4aSLuiz Augusto von Dentz {
1654eca0ae4aSLuiz Augusto von Dentz 	struct adv_info *adv = NULL;
1655eca0ae4aSLuiz Augusto von Dentz 
1656eca0ae4aSLuiz Augusto von Dentz 	if (instance) {
1657eca0ae4aSLuiz Augusto von Dentz 		adv = hci_find_adv_instance(hdev, instance);
1658eca0ae4aSLuiz Augusto von Dentz 		if (!adv)
1659eca0ae4aSLuiz Augusto von Dentz 			return -EINVAL;
1660eca0ae4aSLuiz Augusto von Dentz 	}
1661eca0ae4aSLuiz Augusto von Dentz 
1662eca0ae4aSLuiz Augusto von Dentz 	return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1663eca0ae4aSLuiz Augusto von Dentz }
1664eca0ae4aSLuiz Augusto von Dentz 
1665eca0ae4aSLuiz Augusto von Dentz int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1666eca0ae4aSLuiz Augusto von Dentz {
1667eca0ae4aSLuiz Augusto von Dentz 	struct hci_cp_le_term_big cp;
1668eca0ae4aSLuiz Augusto von Dentz 
1669eca0ae4aSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1670eca0ae4aSLuiz Augusto von Dentz 	cp.handle = handle;
1671eca0ae4aSLuiz Augusto von Dentz 	cp.reason = reason;
1672eca0ae4aSLuiz Augusto von Dentz 
1673eca0ae4aSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1674eca0ae4aSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1675eca0ae4aSLuiz Augusto von Dentz }
1676eca0ae4aSLuiz Augusto von Dentz 
1677cba6b758SLuiz Augusto von Dentz static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1678cba6b758SLuiz Augusto von Dentz {
1679cba6b758SLuiz Augusto von Dentz 	struct {
1680cba6b758SLuiz Augusto von Dentz 		struct hci_cp_le_set_ext_adv_data cp;
1681cba6b758SLuiz Augusto von Dentz 		u8 data[HCI_MAX_EXT_AD_LENGTH];
1682cba6b758SLuiz Augusto von Dentz 	} pdu;
1683cba6b758SLuiz Augusto von Dentz 	u8 len;
168434a718bcSLuiz Augusto von Dentz 	struct adv_info *adv = NULL;
168534a718bcSLuiz Augusto von Dentz 	int err;
1686cba6b758SLuiz Augusto von Dentz 
1687cba6b758SLuiz Augusto von Dentz 	memset(&pdu, 0, sizeof(pdu));
1688cba6b758SLuiz Augusto von Dentz 
168934a718bcSLuiz Augusto von Dentz 	if (instance) {
169034a718bcSLuiz Augusto von Dentz 		adv = hci_find_adv_instance(hdev, instance);
169134a718bcSLuiz Augusto von Dentz 		if (!adv || !adv->adv_data_changed)
1692cba6b758SLuiz Augusto von Dentz 			return 0;
169334a718bcSLuiz Augusto von Dentz 	}
1694cba6b758SLuiz Augusto von Dentz 
169534a718bcSLuiz Augusto von Dentz 	len = eir_create_adv_data(hdev, instance, pdu.data);
1696cba6b758SLuiz Augusto von Dentz 
1697cba6b758SLuiz Augusto von Dentz 	pdu.cp.length = len;
1698cba6b758SLuiz Augusto von Dentz 	pdu.cp.handle = instance;
1699cba6b758SLuiz Augusto von Dentz 	pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1700cba6b758SLuiz Augusto von Dentz 	pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1701cba6b758SLuiz Augusto von Dentz 
170234a718bcSLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1703cba6b758SLuiz Augusto von Dentz 				    sizeof(pdu.cp) + len, &pdu.cp,
1704cba6b758SLuiz Augusto von Dentz 				    HCI_CMD_TIMEOUT);
170534a718bcSLuiz Augusto von Dentz 	if (err)
170634a718bcSLuiz Augusto von Dentz 		return err;
170734a718bcSLuiz Augusto von Dentz 
170834a718bcSLuiz Augusto von Dentz 	/* Update data if the command succeed */
170934a718bcSLuiz Augusto von Dentz 	if (adv) {
171034a718bcSLuiz Augusto von Dentz 		adv->adv_data_changed = false;
171134a718bcSLuiz Augusto von Dentz 	} else {
171234a718bcSLuiz Augusto von Dentz 		memcpy(hdev->adv_data, pdu.data, len);
171334a718bcSLuiz Augusto von Dentz 		hdev->adv_data_len = len;
171434a718bcSLuiz Augusto von Dentz 	}
171534a718bcSLuiz Augusto von Dentz 
171634a718bcSLuiz Augusto von Dentz 	return 0;
1717cba6b758SLuiz Augusto von Dentz }
1718cba6b758SLuiz Augusto von Dentz 
1719cba6b758SLuiz Augusto von Dentz static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1720cba6b758SLuiz Augusto von Dentz {
1721cba6b758SLuiz Augusto von Dentz 	struct hci_cp_le_set_adv_data cp;
1722cba6b758SLuiz Augusto von Dentz 	u8 len;
1723cba6b758SLuiz Augusto von Dentz 
1724cba6b758SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1725cba6b758SLuiz Augusto von Dentz 
1726cba6b758SLuiz Augusto von Dentz 	len = eir_create_adv_data(hdev, instance, cp.data);
1727cba6b758SLuiz Augusto von Dentz 
1728cba6b758SLuiz Augusto von Dentz 	/* There's nothing to do if the data hasn't changed */
1729cba6b758SLuiz Augusto von Dentz 	if (hdev->adv_data_len == len &&
1730cba6b758SLuiz Augusto von Dentz 	    memcmp(cp.data, hdev->adv_data, len) == 0)
1731cba6b758SLuiz Augusto von Dentz 		return 0;
1732cba6b758SLuiz Augusto von Dentz 
1733cba6b758SLuiz Augusto von Dentz 	memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1734cba6b758SLuiz Augusto von Dentz 	hdev->adv_data_len = len;
1735cba6b758SLuiz Augusto von Dentz 
1736cba6b758SLuiz Augusto von Dentz 	cp.length = len;
1737cba6b758SLuiz Augusto von Dentz 
1738cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1739cba6b758SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1740cba6b758SLuiz Augusto von Dentz }
1741cba6b758SLuiz Augusto von Dentz 
1742cba6b758SLuiz Augusto von Dentz int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1743cba6b758SLuiz Augusto von Dentz {
1744cba6b758SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1745cba6b758SLuiz Augusto von Dentz 		return 0;
1746cba6b758SLuiz Augusto von Dentz 
1747cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1748cba6b758SLuiz Augusto von Dentz 		return hci_set_ext_adv_data_sync(hdev, instance);
1749cba6b758SLuiz Augusto von Dentz 
1750cba6b758SLuiz Augusto von Dentz 	return hci_set_adv_data_sync(hdev, instance);
1751cba6b758SLuiz Augusto von Dentz }
1752cba6b758SLuiz Augusto von Dentz 
1753cba6b758SLuiz Augusto von Dentz int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1754cba6b758SLuiz Augusto von Dentz 				   bool force)
1755cba6b758SLuiz Augusto von Dentz {
1756cba6b758SLuiz Augusto von Dentz 	struct adv_info *adv = NULL;
1757cba6b758SLuiz Augusto von Dentz 	u16 timeout;
1758cba6b758SLuiz Augusto von Dentz 
1759cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1760cba6b758SLuiz Augusto von Dentz 		return -EPERM;
1761cba6b758SLuiz Augusto von Dentz 
1762cba6b758SLuiz Augusto von Dentz 	if (hdev->adv_instance_timeout)
1763cba6b758SLuiz Augusto von Dentz 		return -EBUSY;
1764cba6b758SLuiz Augusto von Dentz 
1765cba6b758SLuiz Augusto von Dentz 	adv = hci_find_adv_instance(hdev, instance);
1766cba6b758SLuiz Augusto von Dentz 	if (!adv)
1767cba6b758SLuiz Augusto von Dentz 		return -ENOENT;
1768cba6b758SLuiz Augusto von Dentz 
1769cba6b758SLuiz Augusto von Dentz 	/* A zero timeout means unlimited advertising. As long as there is
1770cba6b758SLuiz Augusto von Dentz 	 * only one instance, duration should be ignored. We still set a timeout
1771cba6b758SLuiz Augusto von Dentz 	 * in case further instances are being added later on.
1772cba6b758SLuiz Augusto von Dentz 	 *
1773cba6b758SLuiz Augusto von Dentz 	 * If the remaining lifetime of the instance is more than the duration
1774cba6b758SLuiz Augusto von Dentz 	 * then the timeout corresponds to the duration, otherwise it will be
1775cba6b758SLuiz Augusto von Dentz 	 * reduced to the remaining instance lifetime.
1776cba6b758SLuiz Augusto von Dentz 	 */
1777cba6b758SLuiz Augusto von Dentz 	if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1778cba6b758SLuiz Augusto von Dentz 		timeout = adv->duration;
1779cba6b758SLuiz Augusto von Dentz 	else
1780cba6b758SLuiz Augusto von Dentz 		timeout = adv->remaining_time;
1781cba6b758SLuiz Augusto von Dentz 
1782cba6b758SLuiz Augusto von Dentz 	/* The remaining time is being reduced unless the instance is being
1783cba6b758SLuiz Augusto von Dentz 	 * advertised without time limit.
1784cba6b758SLuiz Augusto von Dentz 	 */
1785cba6b758SLuiz Augusto von Dentz 	if (adv->timeout)
1786cba6b758SLuiz Augusto von Dentz 		adv->remaining_time = adv->remaining_time - timeout;
1787cba6b758SLuiz Augusto von Dentz 
1788cba6b758SLuiz Augusto von Dentz 	/* Only use work for scheduling instances with legacy advertising */
1789cba6b758SLuiz Augusto von Dentz 	if (!ext_adv_capable(hdev)) {
1790cba6b758SLuiz Augusto von Dentz 		hdev->adv_instance_timeout = timeout;
1791cba6b758SLuiz Augusto von Dentz 		queue_delayed_work(hdev->req_workqueue,
1792cba6b758SLuiz Augusto von Dentz 				   &hdev->adv_instance_expire,
1793cba6b758SLuiz Augusto von Dentz 				   msecs_to_jiffies(timeout * 1000));
1794cba6b758SLuiz Augusto von Dentz 	}
1795cba6b758SLuiz Augusto von Dentz 
1796cba6b758SLuiz Augusto von Dentz 	/* If we're just re-scheduling the same instance again then do not
1797cba6b758SLuiz Augusto von Dentz 	 * execute any HCI commands. This happens when a single instance is
1798cba6b758SLuiz Augusto von Dentz 	 * being advertised.
1799cba6b758SLuiz Augusto von Dentz 	 */
1800cba6b758SLuiz Augusto von Dentz 	if (!force && hdev->cur_adv_instance == instance &&
1801cba6b758SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_LE_ADV))
1802cba6b758SLuiz Augusto von Dentz 		return 0;
1803cba6b758SLuiz Augusto von Dentz 
1804cba6b758SLuiz Augusto von Dentz 	hdev->cur_adv_instance = instance;
1805cba6b758SLuiz Augusto von Dentz 
1806cba6b758SLuiz Augusto von Dentz 	return hci_start_adv_sync(hdev, instance);
1807cba6b758SLuiz Augusto von Dentz }
1808cba6b758SLuiz Augusto von Dentz 
1809cba6b758SLuiz Augusto von Dentz static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1810cba6b758SLuiz Augusto von Dentz {
1811cba6b758SLuiz Augusto von Dentz 	int err;
1812cba6b758SLuiz Augusto von Dentz 
1813cba6b758SLuiz Augusto von Dentz 	if (!ext_adv_capable(hdev))
1814cba6b758SLuiz Augusto von Dentz 		return 0;
1815cba6b758SLuiz Augusto von Dentz 
1816cba6b758SLuiz Augusto von Dentz 	/* Disable instance 0x00 to disable all instances */
1817cba6b758SLuiz Augusto von Dentz 	err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1818cba6b758SLuiz Augusto von Dentz 	if (err)
1819cba6b758SLuiz Augusto von Dentz 		return err;
1820cba6b758SLuiz Augusto von Dentz 
1821cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1822cba6b758SLuiz Augusto von Dentz 					0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1823cba6b758SLuiz Augusto von Dentz }
1824cba6b758SLuiz Augusto von Dentz 
1825cba6b758SLuiz Augusto von Dentz static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1826cba6b758SLuiz Augusto von Dentz {
1827cba6b758SLuiz Augusto von Dentz 	struct adv_info *adv, *n;
1828b338d917SBrian Gix 	int err = 0;
1829cba6b758SLuiz Augusto von Dentz 
1830cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1831cba6b758SLuiz Augusto von Dentz 		/* Remove all existing sets */
1832b338d917SBrian Gix 		err = hci_clear_adv_sets_sync(hdev, sk);
1833b338d917SBrian Gix 	if (ext_adv_capable(hdev))
1834b338d917SBrian Gix 		return err;
1835cba6b758SLuiz Augusto von Dentz 
1836cba6b758SLuiz Augusto von Dentz 	/* This is safe as long as there is no command send while the lock is
1837cba6b758SLuiz Augusto von Dentz 	 * held.
1838cba6b758SLuiz Augusto von Dentz 	 */
1839cba6b758SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
1840cba6b758SLuiz Augusto von Dentz 
1841cba6b758SLuiz Augusto von Dentz 	/* Cleanup non-ext instances */
1842cba6b758SLuiz Augusto von Dentz 	list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1843cba6b758SLuiz Augusto von Dentz 		u8 instance = adv->instance;
1844cba6b758SLuiz Augusto von Dentz 		int err;
1845cba6b758SLuiz Augusto von Dentz 
1846cba6b758SLuiz Augusto von Dentz 		if (!(force || adv->timeout))
1847cba6b758SLuiz Augusto von Dentz 			continue;
1848cba6b758SLuiz Augusto von Dentz 
1849cba6b758SLuiz Augusto von Dentz 		err = hci_remove_adv_instance(hdev, instance);
1850cba6b758SLuiz Augusto von Dentz 		if (!err)
1851cba6b758SLuiz Augusto von Dentz 			mgmt_advertising_removed(sk, hdev, instance);
1852cba6b758SLuiz Augusto von Dentz 	}
1853cba6b758SLuiz Augusto von Dentz 
1854cba6b758SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
1855cba6b758SLuiz Augusto von Dentz 
1856cba6b758SLuiz Augusto von Dentz 	return 0;
1857cba6b758SLuiz Augusto von Dentz }
1858cba6b758SLuiz Augusto von Dentz 
1859cba6b758SLuiz Augusto von Dentz static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1860cba6b758SLuiz Augusto von Dentz 			       struct sock *sk)
1861cba6b758SLuiz Augusto von Dentz {
1862b338d917SBrian Gix 	int err = 0;
1863cba6b758SLuiz Augusto von Dentz 
1864cba6b758SLuiz Augusto von Dentz 	/* If we use extended advertising, instance has to be removed first. */
1865cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1866b338d917SBrian Gix 		err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1867b338d917SBrian Gix 	if (ext_adv_capable(hdev))
1868b338d917SBrian Gix 		return err;
1869cba6b758SLuiz Augusto von Dentz 
1870cba6b758SLuiz Augusto von Dentz 	/* This is safe as long as there is no command send while the lock is
1871cba6b758SLuiz Augusto von Dentz 	 * held.
1872cba6b758SLuiz Augusto von Dentz 	 */
1873cba6b758SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
1874cba6b758SLuiz Augusto von Dentz 
1875cba6b758SLuiz Augusto von Dentz 	err = hci_remove_adv_instance(hdev, instance);
1876cba6b758SLuiz Augusto von Dentz 	if (!err)
1877cba6b758SLuiz Augusto von Dentz 		mgmt_advertising_removed(sk, hdev, instance);
1878cba6b758SLuiz Augusto von Dentz 
1879cba6b758SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
1880cba6b758SLuiz Augusto von Dentz 
1881cba6b758SLuiz Augusto von Dentz 	return err;
1882cba6b758SLuiz Augusto von Dentz }
1883cba6b758SLuiz Augusto von Dentz 
1884cba6b758SLuiz Augusto von Dentz /* For a single instance:
1885cba6b758SLuiz Augusto von Dentz  * - force == true: The instance will be removed even when its remaining
1886cba6b758SLuiz Augusto von Dentz  *   lifetime is not zero.
1887cba6b758SLuiz Augusto von Dentz  * - force == false: the instance will be deactivated but kept stored unless
1888cba6b758SLuiz Augusto von Dentz  *   the remaining lifetime is zero.
1889cba6b758SLuiz Augusto von Dentz  *
1890cba6b758SLuiz Augusto von Dentz  * For instance == 0x00:
1891cba6b758SLuiz Augusto von Dentz  * - force == true: All instances will be removed regardless of their timeout
1892cba6b758SLuiz Augusto von Dentz  *   setting.
1893cba6b758SLuiz Augusto von Dentz  * - force == false: Only instances that have a timeout will be removed.
1894cba6b758SLuiz Augusto von Dentz  */
1895cba6b758SLuiz Augusto von Dentz int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1896cba6b758SLuiz Augusto von Dentz 				u8 instance, bool force)
1897cba6b758SLuiz Augusto von Dentz {
1898cba6b758SLuiz Augusto von Dentz 	struct adv_info *next = NULL;
1899cba6b758SLuiz Augusto von Dentz 	int err;
1900cba6b758SLuiz Augusto von Dentz 
1901cba6b758SLuiz Augusto von Dentz 	/* Cancel any timeout concerning the removed instance(s). */
1902cba6b758SLuiz Augusto von Dentz 	if (!instance || hdev->cur_adv_instance == instance)
1903cba6b758SLuiz Augusto von Dentz 		cancel_adv_timeout(hdev);
1904cba6b758SLuiz Augusto von Dentz 
1905cba6b758SLuiz Augusto von Dentz 	/* Get the next instance to advertise BEFORE we remove
1906cba6b758SLuiz Augusto von Dentz 	 * the current one. This can be the same instance again
1907cba6b758SLuiz Augusto von Dentz 	 * if there is only one instance.
1908cba6b758SLuiz Augusto von Dentz 	 */
1909cba6b758SLuiz Augusto von Dentz 	if (hdev->cur_adv_instance == instance)
1910cba6b758SLuiz Augusto von Dentz 		next = hci_get_next_instance(hdev, instance);
1911cba6b758SLuiz Augusto von Dentz 
1912cba6b758SLuiz Augusto von Dentz 	if (!instance) {
1913cba6b758SLuiz Augusto von Dentz 		err = hci_clear_adv_sync(hdev, sk, force);
1914cba6b758SLuiz Augusto von Dentz 		if (err)
1915cba6b758SLuiz Augusto von Dentz 			return err;
1916cba6b758SLuiz Augusto von Dentz 	} else {
1917cba6b758SLuiz Augusto von Dentz 		struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1918cba6b758SLuiz Augusto von Dentz 
1919cba6b758SLuiz Augusto von Dentz 		if (force || (adv && adv->timeout && !adv->remaining_time)) {
1920cba6b758SLuiz Augusto von Dentz 			/* Don't advertise a removed instance. */
1921cba6b758SLuiz Augusto von Dentz 			if (next && next->instance == instance)
1922cba6b758SLuiz Augusto von Dentz 				next = NULL;
1923cba6b758SLuiz Augusto von Dentz 
1924cba6b758SLuiz Augusto von Dentz 			err = hci_remove_adv_sync(hdev, instance, sk);
1925cba6b758SLuiz Augusto von Dentz 			if (err)
1926cba6b758SLuiz Augusto von Dentz 				return err;
1927cba6b758SLuiz Augusto von Dentz 		}
1928cba6b758SLuiz Augusto von Dentz 	}
1929cba6b758SLuiz Augusto von Dentz 
1930cba6b758SLuiz Augusto von Dentz 	if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1931cba6b758SLuiz Augusto von Dentz 		return 0;
1932cba6b758SLuiz Augusto von Dentz 
1933cba6b758SLuiz Augusto von Dentz 	if (next && !ext_adv_capable(hdev))
1934cba6b758SLuiz Augusto von Dentz 		hci_schedule_adv_instance_sync(hdev, next->instance, false);
1935cba6b758SLuiz Augusto von Dentz 
1936cba6b758SLuiz Augusto von Dentz 	return 0;
1937cba6b758SLuiz Augusto von Dentz }
1938cba6b758SLuiz Augusto von Dentz 
193947db6b42SBrian Gix int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
194047db6b42SBrian Gix {
194147db6b42SBrian Gix 	struct hci_cp_read_rssi cp;
194247db6b42SBrian Gix 
194347db6b42SBrian Gix 	cp.handle = handle;
194447db6b42SBrian Gix 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
194547db6b42SBrian Gix 					sizeof(cp), &cp, HCI_CMD_TIMEOUT);
194647db6b42SBrian Gix }
194747db6b42SBrian Gix 
19485a750137SBrian Gix int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
19495a750137SBrian Gix {
19505a750137SBrian Gix 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
19515a750137SBrian Gix 					sizeof(*cp), cp, HCI_CMD_TIMEOUT);
19525a750137SBrian Gix }
19535a750137SBrian Gix 
195447db6b42SBrian Gix int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
195547db6b42SBrian Gix {
195647db6b42SBrian Gix 	struct hci_cp_read_tx_power cp;
195747db6b42SBrian Gix 
195847db6b42SBrian Gix 	cp.handle = handle;
195947db6b42SBrian Gix 	cp.type = type;
196047db6b42SBrian Gix 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
196147db6b42SBrian Gix 					sizeof(cp), &cp, HCI_CMD_TIMEOUT);
196247db6b42SBrian Gix }
196347db6b42SBrian Gix 
1964cba6b758SLuiz Augusto von Dentz int hci_disable_advertising_sync(struct hci_dev *hdev)
1965cba6b758SLuiz Augusto von Dentz {
1966cba6b758SLuiz Augusto von Dentz 	u8 enable = 0x00;
1967b338d917SBrian Gix 	int err = 0;
1968cba6b758SLuiz Augusto von Dentz 
1969ad383c2cSLuiz Augusto von Dentz 	/* If controller is not advertising we are done. */
1970ad383c2cSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1971ad383c2cSLuiz Augusto von Dentz 		return 0;
1972ad383c2cSLuiz Augusto von Dentz 
1973cba6b758SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
1974b338d917SBrian Gix 		err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1975b338d917SBrian Gix 	if (ext_adv_capable(hdev))
1976b338d917SBrian Gix 		return err;
1977cba6b758SLuiz Augusto von Dentz 
1978cba6b758SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1979cba6b758SLuiz Augusto von Dentz 				     sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1980cba6b758SLuiz Augusto von Dentz }
1981e8907f76SLuiz Augusto von Dentz 
1982e8907f76SLuiz Augusto von Dentz static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1983e8907f76SLuiz Augusto von Dentz 					   u8 filter_dup)
1984e8907f76SLuiz Augusto von Dentz {
1985e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_scan_enable cp;
1986e8907f76SLuiz Augusto von Dentz 
1987e8907f76SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1988e8907f76SLuiz Augusto von Dentz 	cp.enable = val;
1989b338d917SBrian Gix 
1990b338d917SBrian Gix 	if (hci_dev_test_flag(hdev, HCI_MESH))
1991b338d917SBrian Gix 		cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
1992b338d917SBrian Gix 	else
1993e8907f76SLuiz Augusto von Dentz 		cp.filter_dup = filter_dup;
1994e8907f76SLuiz Augusto von Dentz 
1995e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1996e8907f76SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1997e8907f76SLuiz Augusto von Dentz }
1998e8907f76SLuiz Augusto von Dentz 
1999e8907f76SLuiz Augusto von Dentz static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2000e8907f76SLuiz Augusto von Dentz 				       u8 filter_dup)
2001e8907f76SLuiz Augusto von Dentz {
2002e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_set_scan_enable cp;
2003e8907f76SLuiz Augusto von Dentz 
2004e8907f76SLuiz Augusto von Dentz 	if (use_ext_scan(hdev))
2005e8907f76SLuiz Augusto von Dentz 		return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2006e8907f76SLuiz Augusto von Dentz 
2007e8907f76SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
2008e8907f76SLuiz Augusto von Dentz 	cp.enable = val;
2009b338d917SBrian Gix 
2010b338d917SBrian Gix 	if (val && hci_dev_test_flag(hdev, HCI_MESH))
2011b338d917SBrian Gix 		cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2012b338d917SBrian Gix 	else
2013e8907f76SLuiz Augusto von Dentz 		cp.filter_dup = filter_dup;
2014e8907f76SLuiz Augusto von Dentz 
2015e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2016e8907f76SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2017e8907f76SLuiz Augusto von Dentz }
2018e8907f76SLuiz Augusto von Dentz 
2019e8907f76SLuiz Augusto von Dentz static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2020e8907f76SLuiz Augusto von Dentz {
2021ad383c2cSLuiz Augusto von Dentz 	if (!use_ll_privacy(hdev))
2022ad383c2cSLuiz Augusto von Dentz 		return 0;
2023ad383c2cSLuiz Augusto von Dentz 
2024ad383c2cSLuiz Augusto von Dentz 	/* If controller is not/already resolving we are done. */
2025ad383c2cSLuiz Augusto von Dentz 	if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2026e8907f76SLuiz Augusto von Dentz 		return 0;
2027e8907f76SLuiz Augusto von Dentz 
2028e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2029e8907f76SLuiz Augusto von Dentz 				     sizeof(val), &val, HCI_CMD_TIMEOUT);
2030e8907f76SLuiz Augusto von Dentz }
2031e8907f76SLuiz Augusto von Dentz 
203227592ca1SLuiz Augusto von Dentz static int hci_scan_disable_sync(struct hci_dev *hdev)
2033e8907f76SLuiz Augusto von Dentz {
2034e8907f76SLuiz Augusto von Dentz 	int err;
2035e8907f76SLuiz Augusto von Dentz 
2036e8907f76SLuiz Augusto von Dentz 	/* If controller is not scanning we are done. */
2037e8907f76SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2038e8907f76SLuiz Augusto von Dentz 		return 0;
2039e8907f76SLuiz Augusto von Dentz 
2040e8907f76SLuiz Augusto von Dentz 	if (hdev->scanning_paused) {
2041e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
2042e8907f76SLuiz Augusto von Dentz 		return 0;
2043e8907f76SLuiz Augusto von Dentz 	}
2044e8907f76SLuiz Augusto von Dentz 
2045e8907f76SLuiz Augusto von Dentz 	err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2046e8907f76SLuiz Augusto von Dentz 	if (err) {
2047e8907f76SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2048e8907f76SLuiz Augusto von Dentz 		return err;
2049e8907f76SLuiz Augusto von Dentz 	}
2050e8907f76SLuiz Augusto von Dentz 
2051e8907f76SLuiz Augusto von Dentz 	return err;
2052e8907f76SLuiz Augusto von Dentz }
2053e8907f76SLuiz Augusto von Dentz 
2054e8907f76SLuiz Augusto von Dentz static bool scan_use_rpa(struct hci_dev *hdev)
2055e8907f76SLuiz Augusto von Dentz {
2056e8907f76SLuiz Augusto von Dentz 	return hci_dev_test_flag(hdev, HCI_PRIVACY);
2057e8907f76SLuiz Augusto von Dentz }
2058e8907f76SLuiz Augusto von Dentz 
2059e8907f76SLuiz Augusto von Dentz static void hci_start_interleave_scan(struct hci_dev *hdev)
2060e8907f76SLuiz Augusto von Dentz {
2061e8907f76SLuiz Augusto von Dentz 	hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2062e8907f76SLuiz Augusto von Dentz 	queue_delayed_work(hdev->req_workqueue,
2063e8907f76SLuiz Augusto von Dentz 			   &hdev->interleave_scan, 0);
2064e8907f76SLuiz Augusto von Dentz }
2065e8907f76SLuiz Augusto von Dentz 
2066e8907f76SLuiz Augusto von Dentz static bool is_interleave_scanning(struct hci_dev *hdev)
2067e8907f76SLuiz Augusto von Dentz {
2068e8907f76SLuiz Augusto von Dentz 	return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2069e8907f76SLuiz Augusto von Dentz }
2070e8907f76SLuiz Augusto von Dentz 
2071e8907f76SLuiz Augusto von Dentz static void cancel_interleave_scan(struct hci_dev *hdev)
2072e8907f76SLuiz Augusto von Dentz {
2073e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "cancelling interleave scan");
2074e8907f76SLuiz Augusto von Dentz 
2075e8907f76SLuiz Augusto von Dentz 	cancel_delayed_work_sync(&hdev->interleave_scan);
2076e8907f76SLuiz Augusto von Dentz 
2077e8907f76SLuiz Augusto von Dentz 	hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2078e8907f76SLuiz Augusto von Dentz }
2079e8907f76SLuiz Augusto von Dentz 
2080e8907f76SLuiz Augusto von Dentz /* Return true if interleave_scan wasn't started until exiting this function,
2081e8907f76SLuiz Augusto von Dentz  * otherwise, return false
2082e8907f76SLuiz Augusto von Dentz  */
2083e8907f76SLuiz Augusto von Dentz static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2084e8907f76SLuiz Augusto von Dentz {
2085e8907f76SLuiz Augusto von Dentz 	/* Do interleaved scan only if all of the following are true:
2086e8907f76SLuiz Augusto von Dentz 	 * - There is at least one ADV monitor
2087e8907f76SLuiz Augusto von Dentz 	 * - At least one pending LE connection or one device to be scanned for
2088e8907f76SLuiz Augusto von Dentz 	 * - Monitor offloading is not supported
2089e8907f76SLuiz Augusto von Dentz 	 * If so, we should alternate between allowlist scan and one without
2090e8907f76SLuiz Augusto von Dentz 	 * any filters to save power.
2091e8907f76SLuiz Augusto von Dentz 	 */
2092e8907f76SLuiz Augusto von Dentz 	bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2093e8907f76SLuiz Augusto von Dentz 				!(list_empty(&hdev->pend_le_conns) &&
2094e8907f76SLuiz Augusto von Dentz 				  list_empty(&hdev->pend_le_reports)) &&
2095e8907f76SLuiz Augusto von Dentz 				hci_get_adv_monitor_offload_ext(hdev) ==
2096e8907f76SLuiz Augusto von Dentz 				    HCI_ADV_MONITOR_EXT_NONE;
2097e8907f76SLuiz Augusto von Dentz 	bool is_interleaving = is_interleave_scanning(hdev);
2098e8907f76SLuiz Augusto von Dentz 
2099e8907f76SLuiz Augusto von Dentz 	if (use_interleaving && !is_interleaving) {
2100e8907f76SLuiz Augusto von Dentz 		hci_start_interleave_scan(hdev);
2101e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "starting interleave scan");
2102e8907f76SLuiz Augusto von Dentz 		return true;
2103e8907f76SLuiz Augusto von Dentz 	}
2104e8907f76SLuiz Augusto von Dentz 
2105e8907f76SLuiz Augusto von Dentz 	if (!use_interleaving && is_interleaving)
2106e8907f76SLuiz Augusto von Dentz 		cancel_interleave_scan(hdev);
2107e8907f76SLuiz Augusto von Dentz 
2108e8907f76SLuiz Augusto von Dentz 	return false;
2109e8907f76SLuiz Augusto von Dentz }
2110e8907f76SLuiz Augusto von Dentz 
2111e8907f76SLuiz Augusto von Dentz /* Removes connection to resolve list if needed.*/
2112e8907f76SLuiz Augusto von Dentz static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2113e8907f76SLuiz Augusto von Dentz 					bdaddr_t *bdaddr, u8 bdaddr_type)
2114e8907f76SLuiz Augusto von Dentz {
2115e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_del_from_resolv_list cp;
2116e8907f76SLuiz Augusto von Dentz 	struct bdaddr_list_with_irk *entry;
2117e8907f76SLuiz Augusto von Dentz 
2118ad383c2cSLuiz Augusto von Dentz 	if (!use_ll_privacy(hdev))
2119e8907f76SLuiz Augusto von Dentz 		return 0;
2120e8907f76SLuiz Augusto von Dentz 
2121e8907f76SLuiz Augusto von Dentz 	/* Check if the IRK has been programmed */
2122e8907f76SLuiz Augusto von Dentz 	entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2123e8907f76SLuiz Augusto von Dentz 						bdaddr_type);
2124e8907f76SLuiz Augusto von Dentz 	if (!entry)
2125e8907f76SLuiz Augusto von Dentz 		return 0;
2126e8907f76SLuiz Augusto von Dentz 
2127e8907f76SLuiz Augusto von Dentz 	cp.bdaddr_type = bdaddr_type;
2128e8907f76SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, bdaddr);
2129e8907f76SLuiz Augusto von Dentz 
2130e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2131e8907f76SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2132e8907f76SLuiz Augusto von Dentz }
2133e8907f76SLuiz Augusto von Dentz 
2134e8907f76SLuiz Augusto von Dentz static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2135e8907f76SLuiz Augusto von Dentz 				       bdaddr_t *bdaddr, u8 bdaddr_type)
2136e8907f76SLuiz Augusto von Dentz {
2137e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_del_from_accept_list cp;
2138e8907f76SLuiz Augusto von Dentz 	int err;
2139e8907f76SLuiz Augusto von Dentz 
2140e8907f76SLuiz Augusto von Dentz 	/* Check if device is on accept list before removing it */
2141e8907f76SLuiz Augusto von Dentz 	if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2142e8907f76SLuiz Augusto von Dentz 		return 0;
2143e8907f76SLuiz Augusto von Dentz 
2144e8907f76SLuiz Augusto von Dentz 	cp.bdaddr_type = bdaddr_type;
2145e8907f76SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, bdaddr);
2146e8907f76SLuiz Augusto von Dentz 
2147ad383c2cSLuiz Augusto von Dentz 	/* Ignore errors when removing from resolving list as that is likely
2148ad383c2cSLuiz Augusto von Dentz 	 * that the device was never added.
2149ad383c2cSLuiz Augusto von Dentz 	 */
2150ad383c2cSLuiz Augusto von Dentz 	hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2151ad383c2cSLuiz Augusto von Dentz 
2152e8907f76SLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2153e8907f76SLuiz Augusto von Dentz 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2154e8907f76SLuiz Augusto von Dentz 	if (err) {
2155e8907f76SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2156e8907f76SLuiz Augusto von Dentz 		return err;
2157e8907f76SLuiz Augusto von Dentz 	}
2158e8907f76SLuiz Augusto von Dentz 
2159e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2160e8907f76SLuiz Augusto von Dentz 		   cp.bdaddr_type);
2161e8907f76SLuiz Augusto von Dentz 
2162ad383c2cSLuiz Augusto von Dentz 	return 0;
2163e8907f76SLuiz Augusto von Dentz }
2164e8907f76SLuiz Augusto von Dentz 
2165195ef75eSPauli Virtanen struct conn_params {
2166195ef75eSPauli Virtanen 	bdaddr_t addr;
2167195ef75eSPauli Virtanen 	u8 addr_type;
2168195ef75eSPauli Virtanen 	hci_conn_flags_t flags;
2169195ef75eSPauli Virtanen 	u8 privacy_mode;
2170195ef75eSPauli Virtanen };
2171195ef75eSPauli Virtanen 
2172cf75ad8bSLuiz Augusto von Dentz /* Adds connection to resolve list if needed.
2173cf75ad8bSLuiz Augusto von Dentz  * Setting params to NULL programs local hdev->irk
2174cf75ad8bSLuiz Augusto von Dentz  */
2175e8907f76SLuiz Augusto von Dentz static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2176195ef75eSPauli Virtanen 					struct conn_params *params)
2177e8907f76SLuiz Augusto von Dentz {
2178e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_add_to_resolv_list cp;
2179e8907f76SLuiz Augusto von Dentz 	struct smp_irk *irk;
2180e8907f76SLuiz Augusto von Dentz 	struct bdaddr_list_with_irk *entry;
2181195ef75eSPauli Virtanen 	struct hci_conn_params *p;
2182e8907f76SLuiz Augusto von Dentz 
2183ad383c2cSLuiz Augusto von Dentz 	if (!use_ll_privacy(hdev))
2184e8907f76SLuiz Augusto von Dentz 		return 0;
2185e8907f76SLuiz Augusto von Dentz 
2186cf75ad8bSLuiz Augusto von Dentz 	/* Attempt to program local identity address, type and irk if params is
2187cf75ad8bSLuiz Augusto von Dentz 	 * NULL.
2188cf75ad8bSLuiz Augusto von Dentz 	 */
2189cf75ad8bSLuiz Augusto von Dentz 	if (!params) {
2190cf75ad8bSLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2191cf75ad8bSLuiz Augusto von Dentz 			return 0;
2192cf75ad8bSLuiz Augusto von Dentz 
2193cf75ad8bSLuiz Augusto von Dentz 		hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2194cf75ad8bSLuiz Augusto von Dentz 		memcpy(cp.peer_irk, hdev->irk, 16);
2195cf75ad8bSLuiz Augusto von Dentz 		goto done;
2196cf75ad8bSLuiz Augusto von Dentz 	}
2197cf75ad8bSLuiz Augusto von Dentz 
2198e8907f76SLuiz Augusto von Dentz 	irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
2199e8907f76SLuiz Augusto von Dentz 	if (!irk)
2200e8907f76SLuiz Augusto von Dentz 		return 0;
2201e8907f76SLuiz Augusto von Dentz 
2202e8907f76SLuiz Augusto von Dentz 	/* Check if the IK has _not_ been programmed yet. */
2203e8907f76SLuiz Augusto von Dentz 	entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2204e8907f76SLuiz Augusto von Dentz 						&params->addr,
2205e8907f76SLuiz Augusto von Dentz 						params->addr_type);
2206e8907f76SLuiz Augusto von Dentz 	if (entry)
2207e8907f76SLuiz Augusto von Dentz 		return 0;
2208e8907f76SLuiz Augusto von Dentz 
2209e8907f76SLuiz Augusto von Dentz 	cp.bdaddr_type = params->addr_type;
2210e8907f76SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, &params->addr);
2211e8907f76SLuiz Augusto von Dentz 	memcpy(cp.peer_irk, irk->val, 16);
2212e8907f76SLuiz Augusto von Dentz 
22130900b1c6SLuiz Augusto von Dentz 	/* Default privacy mode is always Network */
22140900b1c6SLuiz Augusto von Dentz 	params->privacy_mode = HCI_NETWORK_PRIVACY;
22150900b1c6SLuiz Augusto von Dentz 
2216195ef75eSPauli Virtanen 	rcu_read_lock();
2217195ef75eSPauli Virtanen 	p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2218195ef75eSPauli Virtanen 				      &params->addr, params->addr_type);
2219195ef75eSPauli Virtanen 	if (!p)
2220195ef75eSPauli Virtanen 		p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2221195ef75eSPauli Virtanen 					      &params->addr, params->addr_type);
2222195ef75eSPauli Virtanen 	if (p)
2223195ef75eSPauli Virtanen 		WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY);
2224195ef75eSPauli Virtanen 	rcu_read_unlock();
2225195ef75eSPauli Virtanen 
2226cf75ad8bSLuiz Augusto von Dentz done:
2227e8907f76SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2228e8907f76SLuiz Augusto von Dentz 		memcpy(cp.local_irk, hdev->irk, 16);
2229e8907f76SLuiz Augusto von Dentz 	else
2230e8907f76SLuiz Augusto von Dentz 		memset(cp.local_irk, 0, 16);
2231e8907f76SLuiz Augusto von Dentz 
2232e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2233e8907f76SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2234e8907f76SLuiz Augusto von Dentz }
2235e8907f76SLuiz Augusto von Dentz 
2236853b70b5SLuiz Augusto von Dentz /* Set Device Privacy Mode. */
2237853b70b5SLuiz Augusto von Dentz static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2238195ef75eSPauli Virtanen 					struct conn_params *params)
2239853b70b5SLuiz Augusto von Dentz {
2240853b70b5SLuiz Augusto von Dentz 	struct hci_cp_le_set_privacy_mode cp;
2241853b70b5SLuiz Augusto von Dentz 	struct smp_irk *irk;
2242853b70b5SLuiz Augusto von Dentz 
2243853b70b5SLuiz Augusto von Dentz 	/* If device privacy mode has already been set there is nothing to do */
2244853b70b5SLuiz Augusto von Dentz 	if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2245853b70b5SLuiz Augusto von Dentz 		return 0;
2246853b70b5SLuiz Augusto von Dentz 
2247853b70b5SLuiz Augusto von Dentz 	/* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2248853b70b5SLuiz Augusto von Dentz 	 * indicates that LL Privacy has been enabled and
2249853b70b5SLuiz Augusto von Dentz 	 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2250853b70b5SLuiz Augusto von Dentz 	 */
2251e1cff700SLinus Torvalds 	if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2252853b70b5SLuiz Augusto von Dentz 		return 0;
2253853b70b5SLuiz Augusto von Dentz 
2254853b70b5SLuiz Augusto von Dentz 	irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
2255853b70b5SLuiz Augusto von Dentz 	if (!irk)
2256853b70b5SLuiz Augusto von Dentz 		return 0;
2257853b70b5SLuiz Augusto von Dentz 
2258853b70b5SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
2259853b70b5SLuiz Augusto von Dentz 	cp.bdaddr_type = irk->addr_type;
2260853b70b5SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, &irk->bdaddr);
2261853b70b5SLuiz Augusto von Dentz 	cp.mode = HCI_DEVICE_PRIVACY;
2262853b70b5SLuiz Augusto von Dentz 
2263195ef75eSPauli Virtanen 	/* Note: params->privacy_mode is not updated since it is a copy */
2264195ef75eSPauli Virtanen 
2265853b70b5SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2266853b70b5SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2267853b70b5SLuiz Augusto von Dentz }
2268853b70b5SLuiz Augusto von Dentz 
2269e8907f76SLuiz Augusto von Dentz /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2270853b70b5SLuiz Augusto von Dentz  * this attempts to program the device in the resolving list as well and
2271853b70b5SLuiz Augusto von Dentz  * properly set the privacy mode.
2272e8907f76SLuiz Augusto von Dentz  */
2273e8907f76SLuiz Augusto von Dentz static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2274195ef75eSPauli Virtanen 				       struct conn_params *params,
2275ad383c2cSLuiz Augusto von Dentz 				       u8 *num_entries)
2276e8907f76SLuiz Augusto von Dentz {
2277e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_add_to_accept_list cp;
2278e8907f76SLuiz Augusto von Dentz 	int err;
2279e8907f76SLuiz Augusto von Dentz 
22803b420553SLuiz Augusto von Dentz 	/* During suspend, only wakeable devices can be in acceptlist */
22813b420553SLuiz Augusto von Dentz 	if (hdev->suspended &&
2282e1cff700SLinus Torvalds 	    !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
22833b420553SLuiz Augusto von Dentz 		return 0;
22843b420553SLuiz Augusto von Dentz 
2285e8907f76SLuiz Augusto von Dentz 	/* Select filter policy to accept all advertising */
2286e8907f76SLuiz Augusto von Dentz 	if (*num_entries >= hdev->le_accept_list_size)
2287e8907f76SLuiz Augusto von Dentz 		return -ENOSPC;
2288e8907f76SLuiz Augusto von Dentz 
2289e8907f76SLuiz Augusto von Dentz 	/* Accept list can not be used with RPAs */
2290ad383c2cSLuiz Augusto von Dentz 	if (!use_ll_privacy(hdev) &&
22913b420553SLuiz Augusto von Dentz 	    hci_find_irk_by_addr(hdev, &params->addr, params->addr_type))
2292e8907f76SLuiz Augusto von Dentz 		return -EINVAL;
2293e8907f76SLuiz Augusto von Dentz 
2294ad383c2cSLuiz Augusto von Dentz 	/* Attempt to program the device in the resolving list first to avoid
2295ad383c2cSLuiz Augusto von Dentz 	 * having to rollback in case it fails since the resolving list is
2296ad383c2cSLuiz Augusto von Dentz 	 * dynamic it can probably be smaller than the accept list.
2297ad383c2cSLuiz Augusto von Dentz 	 */
2298ad383c2cSLuiz Augusto von Dentz 	err = hci_le_add_resolve_list_sync(hdev, params);
2299ad383c2cSLuiz Augusto von Dentz 	if (err) {
2300ad383c2cSLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2301ad383c2cSLuiz Augusto von Dentz 		return err;
2302ad383c2cSLuiz Augusto von Dentz 	}
2303ad383c2cSLuiz Augusto von Dentz 
2304853b70b5SLuiz Augusto von Dentz 	/* Set Privacy Mode */
2305853b70b5SLuiz Augusto von Dentz 	err = hci_le_set_privacy_mode_sync(hdev, params);
2306853b70b5SLuiz Augusto von Dentz 	if (err) {
2307853b70b5SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2308853b70b5SLuiz Augusto von Dentz 		return err;
2309853b70b5SLuiz Augusto von Dentz 	}
2310853b70b5SLuiz Augusto von Dentz 
2311853b70b5SLuiz Augusto von Dentz 	/* Check if already in accept list */
2312853b70b5SLuiz Augusto von Dentz 	if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
2313853b70b5SLuiz Augusto von Dentz 				   params->addr_type))
2314853b70b5SLuiz Augusto von Dentz 		return 0;
2315853b70b5SLuiz Augusto von Dentz 
2316e8907f76SLuiz Augusto von Dentz 	*num_entries += 1;
2317e8907f76SLuiz Augusto von Dentz 	cp.bdaddr_type = params->addr_type;
2318e8907f76SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, &params->addr);
2319e8907f76SLuiz Augusto von Dentz 
2320e8907f76SLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2321e8907f76SLuiz Augusto von Dentz 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2322e8907f76SLuiz Augusto von Dentz 	if (err) {
2323e8907f76SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2324ad383c2cSLuiz Augusto von Dentz 		/* Rollback the device from the resolving list */
2325ad383c2cSLuiz Augusto von Dentz 		hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2326e8907f76SLuiz Augusto von Dentz 		return err;
2327e8907f76SLuiz Augusto von Dentz 	}
2328e8907f76SLuiz Augusto von Dentz 
2329e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2330e8907f76SLuiz Augusto von Dentz 		   cp.bdaddr_type);
2331e8907f76SLuiz Augusto von Dentz 
2332ad383c2cSLuiz Augusto von Dentz 	return 0;
2333e8907f76SLuiz Augusto von Dentz }
2334e8907f76SLuiz Augusto von Dentz 
2335182ee45dSLuiz Augusto von Dentz /* This function disables/pause all advertising instances */
2336ad383c2cSLuiz Augusto von Dentz static int hci_pause_advertising_sync(struct hci_dev *hdev)
2337ad383c2cSLuiz Augusto von Dentz {
2338ad383c2cSLuiz Augusto von Dentz 	int err;
2339182ee45dSLuiz Augusto von Dentz 	int old_state;
2340ad383c2cSLuiz Augusto von Dentz 
23419446bddeSLuiz Augusto von Dentz 	/* If already been paused there is nothing to do. */
23429446bddeSLuiz Augusto von Dentz 	if (hdev->advertising_paused)
2343ad383c2cSLuiz Augusto von Dentz 		return 0;
2344ad383c2cSLuiz Augusto von Dentz 
2345182ee45dSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Pausing directed advertising");
2346182ee45dSLuiz Augusto von Dentz 
2347182ee45dSLuiz Augusto von Dentz 	/* Stop directed advertising */
2348182ee45dSLuiz Augusto von Dentz 	old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2349182ee45dSLuiz Augusto von Dentz 	if (old_state) {
2350182ee45dSLuiz Augusto von Dentz 		/* When discoverable timeout triggers, then just make sure
2351182ee45dSLuiz Augusto von Dentz 		 * the limited discoverable flag is cleared. Even in the case
2352182ee45dSLuiz Augusto von Dentz 		 * of a timeout triggered from general discoverable, it is
2353182ee45dSLuiz Augusto von Dentz 		 * safe to unconditionally clear the flag.
2354182ee45dSLuiz Augusto von Dentz 		 */
2355182ee45dSLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2356182ee45dSLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2357182ee45dSLuiz Augusto von Dentz 		hdev->discov_timeout = 0;
2358182ee45dSLuiz Augusto von Dentz 	}
2359182ee45dSLuiz Augusto von Dentz 
2360ad383c2cSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Pausing advertising instances");
2361ad383c2cSLuiz Augusto von Dentz 
2362ad383c2cSLuiz Augusto von Dentz 	/* Call to disable any advertisements active on the controller.
2363ad383c2cSLuiz Augusto von Dentz 	 * This will succeed even if no advertisements are configured.
2364ad383c2cSLuiz Augusto von Dentz 	 */
2365ad383c2cSLuiz Augusto von Dentz 	err = hci_disable_advertising_sync(hdev);
2366ad383c2cSLuiz Augusto von Dentz 	if (err)
2367ad383c2cSLuiz Augusto von Dentz 		return err;
2368ad383c2cSLuiz Augusto von Dentz 
2369ad383c2cSLuiz Augusto von Dentz 	/* If we are using software rotation, pause the loop */
2370ad383c2cSLuiz Augusto von Dentz 	if (!ext_adv_capable(hdev))
2371ad383c2cSLuiz Augusto von Dentz 		cancel_adv_timeout(hdev);
2372ad383c2cSLuiz Augusto von Dentz 
2373ad383c2cSLuiz Augusto von Dentz 	hdev->advertising_paused = true;
2374182ee45dSLuiz Augusto von Dentz 	hdev->advertising_old_state = old_state;
2375ad383c2cSLuiz Augusto von Dentz 
2376ad383c2cSLuiz Augusto von Dentz 	return 0;
2377ad383c2cSLuiz Augusto von Dentz }
2378ad383c2cSLuiz Augusto von Dentz 
2379182ee45dSLuiz Augusto von Dentz /* This function enables all user advertising instances */
2380ad383c2cSLuiz Augusto von Dentz static int hci_resume_advertising_sync(struct hci_dev *hdev)
2381ad383c2cSLuiz Augusto von Dentz {
2382ad383c2cSLuiz Augusto von Dentz 	struct adv_info *adv, *tmp;
2383ad383c2cSLuiz Augusto von Dentz 	int err;
2384ad383c2cSLuiz Augusto von Dentz 
2385ad383c2cSLuiz Augusto von Dentz 	/* If advertising has not been paused there is nothing  to do. */
2386ad383c2cSLuiz Augusto von Dentz 	if (!hdev->advertising_paused)
2387ad383c2cSLuiz Augusto von Dentz 		return 0;
2388ad383c2cSLuiz Augusto von Dentz 
2389182ee45dSLuiz Augusto von Dentz 	/* Resume directed advertising */
2390182ee45dSLuiz Augusto von Dentz 	hdev->advertising_paused = false;
2391182ee45dSLuiz Augusto von Dentz 	if (hdev->advertising_old_state) {
2392182ee45dSLuiz Augusto von Dentz 		hci_dev_set_flag(hdev, HCI_ADVERTISING);
2393182ee45dSLuiz Augusto von Dentz 		hdev->advertising_old_state = 0;
2394182ee45dSLuiz Augusto von Dentz 	}
2395182ee45dSLuiz Augusto von Dentz 
2396ad383c2cSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "Resuming advertising instances");
2397ad383c2cSLuiz Augusto von Dentz 
2398ad383c2cSLuiz Augusto von Dentz 	if (ext_adv_capable(hdev)) {
2399ad383c2cSLuiz Augusto von Dentz 		/* Call for each tracked instance to be re-enabled */
2400ad383c2cSLuiz Augusto von Dentz 		list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2401ad383c2cSLuiz Augusto von Dentz 			err = hci_enable_ext_advertising_sync(hdev,
2402ad383c2cSLuiz Augusto von Dentz 							      adv->instance);
2403ad383c2cSLuiz Augusto von Dentz 			if (!err)
2404ad383c2cSLuiz Augusto von Dentz 				continue;
2405ad383c2cSLuiz Augusto von Dentz 
2406ad383c2cSLuiz Augusto von Dentz 			/* If the instance cannot be resumed remove it */
2407ad383c2cSLuiz Augusto von Dentz 			hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2408ad383c2cSLuiz Augusto von Dentz 							 NULL);
2409ad383c2cSLuiz Augusto von Dentz 		}
2410ad383c2cSLuiz Augusto von Dentz 	} else {
2411ad383c2cSLuiz Augusto von Dentz 		/* Schedule for most recent instance to be restarted and begin
2412ad383c2cSLuiz Augusto von Dentz 		 * the software rotation loop
2413ad383c2cSLuiz Augusto von Dentz 		 */
2414ad383c2cSLuiz Augusto von Dentz 		err = hci_schedule_adv_instance_sync(hdev,
2415ad383c2cSLuiz Augusto von Dentz 						     hdev->cur_adv_instance,
2416ad383c2cSLuiz Augusto von Dentz 						     true);
2417ad383c2cSLuiz Augusto von Dentz 	}
2418ad383c2cSLuiz Augusto von Dentz 
2419ad383c2cSLuiz Augusto von Dentz 	hdev->advertising_paused = false;
2420ad383c2cSLuiz Augusto von Dentz 
2421ad383c2cSLuiz Augusto von Dentz 	return err;
2422ad383c2cSLuiz Augusto von Dentz }
2423ad383c2cSLuiz Augusto von Dentz 
24243c44a431SZhengping Jiang static int hci_pause_addr_resolution(struct hci_dev *hdev)
24253c44a431SZhengping Jiang {
24263c44a431SZhengping Jiang 	int err;
24273c44a431SZhengping Jiang 
24283c44a431SZhengping Jiang 	if (!use_ll_privacy(hdev))
24293c44a431SZhengping Jiang 		return 0;
24303c44a431SZhengping Jiang 
24313c44a431SZhengping Jiang 	if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
24323c44a431SZhengping Jiang 		return 0;
24333c44a431SZhengping Jiang 
24343c44a431SZhengping Jiang 	/* Cannot disable addr resolution if scanning is enabled or
24353c44a431SZhengping Jiang 	 * when initiating an LE connection.
24363c44a431SZhengping Jiang 	 */
24373c44a431SZhengping Jiang 	if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
24383c44a431SZhengping Jiang 	    hci_lookup_le_connect(hdev)) {
24393c44a431SZhengping Jiang 		bt_dev_err(hdev, "Command not allowed when scan/LE connect");
24403c44a431SZhengping Jiang 		return -EPERM;
24413c44a431SZhengping Jiang 	}
24423c44a431SZhengping Jiang 
24433c44a431SZhengping Jiang 	/* Cannot disable addr resolution if advertising is enabled. */
24443c44a431SZhengping Jiang 	err = hci_pause_advertising_sync(hdev);
24453c44a431SZhengping Jiang 	if (err) {
24463c44a431SZhengping Jiang 		bt_dev_err(hdev, "Pause advertising failed: %d", err);
24473c44a431SZhengping Jiang 		return err;
24483c44a431SZhengping Jiang 	}
24493c44a431SZhengping Jiang 
24503c44a431SZhengping Jiang 	err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
24513c44a431SZhengping Jiang 	if (err)
24523c44a431SZhengping Jiang 		bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
24533c44a431SZhengping Jiang 			   err);
24543c44a431SZhengping Jiang 
24553c44a431SZhengping Jiang 	/* Return if address resolution is disabled and RPA is not used. */
24563c44a431SZhengping Jiang 	if (!err && scan_use_rpa(hdev))
2457ae051b04SLuiz Augusto von Dentz 		return 0;
24583c44a431SZhengping Jiang 
24593c44a431SZhengping Jiang 	hci_resume_advertising_sync(hdev);
24603c44a431SZhengping Jiang 	return err;
24613c44a431SZhengping Jiang }
24623c44a431SZhengping Jiang 
2463f892244bSBrian Gix struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2464f892244bSBrian Gix 					     bool extended, struct sock *sk)
2465f892244bSBrian Gix {
2466f892244bSBrian Gix 	u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2467f892244bSBrian Gix 					HCI_OP_READ_LOCAL_OOB_DATA;
2468f892244bSBrian Gix 
2469f892244bSBrian Gix 	return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2470f892244bSBrian Gix }
2471f892244bSBrian Gix 
2472195ef75eSPauli Virtanen static struct conn_params *conn_params_copy(struct list_head *list, size_t *n)
2473195ef75eSPauli Virtanen {
2474195ef75eSPauli Virtanen 	struct hci_conn_params *params;
2475195ef75eSPauli Virtanen 	struct conn_params *p;
2476195ef75eSPauli Virtanen 	size_t i;
2477195ef75eSPauli Virtanen 
2478195ef75eSPauli Virtanen 	rcu_read_lock();
2479195ef75eSPauli Virtanen 
2480195ef75eSPauli Virtanen 	i = 0;
2481195ef75eSPauli Virtanen 	list_for_each_entry_rcu(params, list, action)
2482195ef75eSPauli Virtanen 		++i;
2483195ef75eSPauli Virtanen 	*n = i;
2484195ef75eSPauli Virtanen 
2485195ef75eSPauli Virtanen 	rcu_read_unlock();
2486195ef75eSPauli Virtanen 
2487195ef75eSPauli Virtanen 	p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL);
2488195ef75eSPauli Virtanen 	if (!p)
2489195ef75eSPauli Virtanen 		return NULL;
2490195ef75eSPauli Virtanen 
2491195ef75eSPauli Virtanen 	rcu_read_lock();
2492195ef75eSPauli Virtanen 
2493195ef75eSPauli Virtanen 	i = 0;
2494195ef75eSPauli Virtanen 	list_for_each_entry_rcu(params, list, action) {
2495195ef75eSPauli Virtanen 		/* Racing adds are handled in next scan update */
2496195ef75eSPauli Virtanen 		if (i >= *n)
2497195ef75eSPauli Virtanen 			break;
2498195ef75eSPauli Virtanen 
2499195ef75eSPauli Virtanen 		/* No hdev->lock, but: addr, addr_type are immutable.
2500195ef75eSPauli Virtanen 		 * privacy_mode is only written by us or in
2501195ef75eSPauli Virtanen 		 * hci_cc_le_set_privacy_mode that we wait for.
2502195ef75eSPauli Virtanen 		 * We should be idempotent so MGMT updating flags
2503195ef75eSPauli Virtanen 		 * while we are processing is OK.
2504195ef75eSPauli Virtanen 		 */
2505195ef75eSPauli Virtanen 		bacpy(&p[i].addr, &params->addr);
2506195ef75eSPauli Virtanen 		p[i].addr_type = params->addr_type;
2507195ef75eSPauli Virtanen 		p[i].flags = READ_ONCE(params->flags);
2508195ef75eSPauli Virtanen 		p[i].privacy_mode = READ_ONCE(params->privacy_mode);
2509195ef75eSPauli Virtanen 		++i;
2510195ef75eSPauli Virtanen 	}
2511195ef75eSPauli Virtanen 
2512195ef75eSPauli Virtanen 	rcu_read_unlock();
2513195ef75eSPauli Virtanen 
2514195ef75eSPauli Virtanen 	*n = i;
2515195ef75eSPauli Virtanen 	return p;
2516195ef75eSPauli Virtanen }
2517195ef75eSPauli Virtanen 
2518ad383c2cSLuiz Augusto von Dentz /* Device must not be scanning when updating the accept list.
2519ad383c2cSLuiz Augusto von Dentz  *
2520ad383c2cSLuiz Augusto von Dentz  * Update is done using the following sequence:
2521ad383c2cSLuiz Augusto von Dentz  *
2522ad383c2cSLuiz Augusto von Dentz  * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
2523ad383c2cSLuiz Augusto von Dentz  * Remove Devices From Accept List ->
2524ad383c2cSLuiz Augusto von Dentz  * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
2525ad383c2cSLuiz Augusto von Dentz  * Add Devices to Accept List ->
2526ad383c2cSLuiz Augusto von Dentz  * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
2527ad383c2cSLuiz Augusto von Dentz  * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
2528ad383c2cSLuiz Augusto von Dentz  * Enable Scanning
2529ad383c2cSLuiz Augusto von Dentz  *
2530ad383c2cSLuiz Augusto von Dentz  * In case of failure advertising shall be restored to its original state and
2531ad383c2cSLuiz Augusto von Dentz  * return would disable accept list since either accept or resolving list could
2532ad383c2cSLuiz Augusto von Dentz  * not be programmed.
2533ad383c2cSLuiz Augusto von Dentz  *
2534ad383c2cSLuiz Augusto von Dentz  */
2535e8907f76SLuiz Augusto von Dentz static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2536e8907f76SLuiz Augusto von Dentz {
2537195ef75eSPauli Virtanen 	struct conn_params *params;
2538e8907f76SLuiz Augusto von Dentz 	struct bdaddr_list *b, *t;
2539e8907f76SLuiz Augusto von Dentz 	u8 num_entries = 0;
2540e8907f76SLuiz Augusto von Dentz 	bool pend_conn, pend_report;
254180740ebbSLuiz Augusto von Dentz 	u8 filter_policy;
2542195ef75eSPauli Virtanen 	size_t i, n;
2543ad383c2cSLuiz Augusto von Dentz 	int err;
2544e8907f76SLuiz Augusto von Dentz 
25453c44a431SZhengping Jiang 	/* Pause advertising if resolving list can be used as controllers
2546ad383c2cSLuiz Augusto von Dentz 	 * cannot accept resolving list modifications while advertising.
2547ad383c2cSLuiz Augusto von Dentz 	 */
2548ad383c2cSLuiz Augusto von Dentz 	if (use_ll_privacy(hdev)) {
2549ad383c2cSLuiz Augusto von Dentz 		err = hci_pause_advertising_sync(hdev);
2550ad383c2cSLuiz Augusto von Dentz 		if (err) {
2551ad383c2cSLuiz Augusto von Dentz 			bt_dev_err(hdev, "pause advertising failed: %d", err);
2552ad383c2cSLuiz Augusto von Dentz 			return 0x00;
2553ad383c2cSLuiz Augusto von Dentz 		}
2554ad383c2cSLuiz Augusto von Dentz 	}
2555ad383c2cSLuiz Augusto von Dentz 
2556ad383c2cSLuiz Augusto von Dentz 	/* Disable address resolution while reprogramming accept list since
2557ad383c2cSLuiz Augusto von Dentz 	 * devices that do have an IRK will be programmed in the resolving list
2558ad383c2cSLuiz Augusto von Dentz 	 * when LL Privacy is enabled.
2559ad383c2cSLuiz Augusto von Dentz 	 */
2560ad383c2cSLuiz Augusto von Dentz 	err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2561ad383c2cSLuiz Augusto von Dentz 	if (err) {
2562ad383c2cSLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2563ad383c2cSLuiz Augusto von Dentz 		goto done;
2564ad383c2cSLuiz Augusto von Dentz 	}
2565e8907f76SLuiz Augusto von Dentz 
2566e8907f76SLuiz Augusto von Dentz 	/* Go through the current accept list programmed into the
25676828b583SLuiz Augusto von Dentz 	 * controller one by one and check if that address is connected or is
25686828b583SLuiz Augusto von Dentz 	 * still in the list of pending connections or list of devices to
2569e8907f76SLuiz Augusto von Dentz 	 * report. If not present in either list, then remove it from
2570e8907f76SLuiz Augusto von Dentz 	 * the controller.
2571e8907f76SLuiz Augusto von Dentz 	 */
2572e8907f76SLuiz Augusto von Dentz 	list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
25736828b583SLuiz Augusto von Dentz 		if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
25746828b583SLuiz Augusto von Dentz 			continue;
25756828b583SLuiz Augusto von Dentz 
2576195ef75eSPauli Virtanen 		/* Pointers not dereferenced, no locks needed */
2577e8907f76SLuiz Augusto von Dentz 		pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2578e8907f76SLuiz Augusto von Dentz 						      &b->bdaddr,
2579e8907f76SLuiz Augusto von Dentz 						      b->bdaddr_type);
2580e8907f76SLuiz Augusto von Dentz 		pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2581e8907f76SLuiz Augusto von Dentz 							&b->bdaddr,
2582e8907f76SLuiz Augusto von Dentz 							b->bdaddr_type);
2583e8907f76SLuiz Augusto von Dentz 
2584e8907f76SLuiz Augusto von Dentz 		/* If the device is not likely to connect or report,
2585e8907f76SLuiz Augusto von Dentz 		 * remove it from the acceptlist.
2586e8907f76SLuiz Augusto von Dentz 		 */
2587e8907f76SLuiz Augusto von Dentz 		if (!pend_conn && !pend_report) {
2588e8907f76SLuiz Augusto von Dentz 			hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2589e8907f76SLuiz Augusto von Dentz 						    b->bdaddr_type);
2590e8907f76SLuiz Augusto von Dentz 			continue;
2591e8907f76SLuiz Augusto von Dentz 		}
2592e8907f76SLuiz Augusto von Dentz 
2593e8907f76SLuiz Augusto von Dentz 		num_entries++;
2594e8907f76SLuiz Augusto von Dentz 	}
2595e8907f76SLuiz Augusto von Dentz 
2596e8907f76SLuiz Augusto von Dentz 	/* Since all no longer valid accept list entries have been
2597e8907f76SLuiz Augusto von Dentz 	 * removed, walk through the list of pending connections
2598e8907f76SLuiz Augusto von Dentz 	 * and ensure that any new device gets programmed into
2599e8907f76SLuiz Augusto von Dentz 	 * the controller.
2600e8907f76SLuiz Augusto von Dentz 	 *
2601e8907f76SLuiz Augusto von Dentz 	 * If the list of the devices is larger than the list of
2602e8907f76SLuiz Augusto von Dentz 	 * available accept list entries in the controller, then
2603e8907f76SLuiz Augusto von Dentz 	 * just abort and return filer policy value to not use the
2604e8907f76SLuiz Augusto von Dentz 	 * accept list.
2605195ef75eSPauli Virtanen 	 *
2606195ef75eSPauli Virtanen 	 * The list and params may be mutated while we wait for events,
2607195ef75eSPauli Virtanen 	 * so make a copy and iterate it.
2608e8907f76SLuiz Augusto von Dentz 	 */
2609195ef75eSPauli Virtanen 
2610195ef75eSPauli Virtanen 	params = conn_params_copy(&hdev->pend_le_conns, &n);
2611195ef75eSPauli Virtanen 	if (!params) {
2612195ef75eSPauli Virtanen 		err = -ENOMEM;
2613ad383c2cSLuiz Augusto von Dentz 		goto done;
2614e8907f76SLuiz Augusto von Dentz 	}
2615e8907f76SLuiz Augusto von Dentz 
2616195ef75eSPauli Virtanen 	for (i = 0; i < n; ++i) {
2617195ef75eSPauli Virtanen 		err = hci_le_add_accept_list_sync(hdev, &params[i],
2618195ef75eSPauli Virtanen 						  &num_entries);
2619195ef75eSPauli Virtanen 		if (err) {
2620195ef75eSPauli Virtanen 			kvfree(params);
2621195ef75eSPauli Virtanen 			goto done;
2622195ef75eSPauli Virtanen 		}
2623195ef75eSPauli Virtanen 	}
2624195ef75eSPauli Virtanen 
2625195ef75eSPauli Virtanen 	kvfree(params);
2626195ef75eSPauli Virtanen 
2627e8907f76SLuiz Augusto von Dentz 	/* After adding all new pending connections, walk through
2628e8907f76SLuiz Augusto von Dentz 	 * the list of pending reports and also add these to the
2629e8907f76SLuiz Augusto von Dentz 	 * accept list if there is still space. Abort if space runs out.
2630e8907f76SLuiz Augusto von Dentz 	 */
2631195ef75eSPauli Virtanen 
2632195ef75eSPauli Virtanen 	params = conn_params_copy(&hdev->pend_le_reports, &n);
2633195ef75eSPauli Virtanen 	if (!params) {
2634195ef75eSPauli Virtanen 		err = -ENOMEM;
2635ad383c2cSLuiz Augusto von Dentz 		goto done;
2636e8907f76SLuiz Augusto von Dentz 	}
2637e8907f76SLuiz Augusto von Dentz 
2638195ef75eSPauli Virtanen 	for (i = 0; i < n; ++i) {
2639195ef75eSPauli Virtanen 		err = hci_le_add_accept_list_sync(hdev, &params[i],
2640195ef75eSPauli Virtanen 						  &num_entries);
2641195ef75eSPauli Virtanen 		if (err) {
2642195ef75eSPauli Virtanen 			kvfree(params);
2643195ef75eSPauli Virtanen 			goto done;
2644195ef75eSPauli Virtanen 		}
2645195ef75eSPauli Virtanen 	}
2646195ef75eSPauli Virtanen 
2647195ef75eSPauli Virtanen 	kvfree(params);
2648195ef75eSPauli Virtanen 
2649e8907f76SLuiz Augusto von Dentz 	/* Use the allowlist unless the following conditions are all true:
2650e8907f76SLuiz Augusto von Dentz 	 * - We are not currently suspending
2651e8907f76SLuiz Augusto von Dentz 	 * - There are 1 or more ADV monitors registered and it's not offloaded
2652e8907f76SLuiz Augusto von Dentz 	 * - Interleaved scanning is not currently using the allowlist
2653e8907f76SLuiz Augusto von Dentz 	 */
2654e8907f76SLuiz Augusto von Dentz 	if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2655e8907f76SLuiz Augusto von Dentz 	    hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2656e8907f76SLuiz Augusto von Dentz 	    hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2657ad383c2cSLuiz Augusto von Dentz 		err = -EINVAL;
2658ad383c2cSLuiz Augusto von Dentz 
2659ad383c2cSLuiz Augusto von Dentz done:
266080740ebbSLuiz Augusto von Dentz 	filter_policy = err ? 0x00 : 0x01;
266180740ebbSLuiz Augusto von Dentz 
2662ad383c2cSLuiz Augusto von Dentz 	/* Enable address resolution when LL Privacy is enabled. */
2663ad383c2cSLuiz Augusto von Dentz 	err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2664ad383c2cSLuiz Augusto von Dentz 	if (err)
2665ad383c2cSLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2666ad383c2cSLuiz Augusto von Dentz 
2667ad383c2cSLuiz Augusto von Dentz 	/* Resume advertising if it was paused */
2668ad383c2cSLuiz Augusto von Dentz 	if (use_ll_privacy(hdev))
2669ad383c2cSLuiz Augusto von Dentz 		hci_resume_advertising_sync(hdev);
2670e8907f76SLuiz Augusto von Dentz 
2671e8907f76SLuiz Augusto von Dentz 	/* Select filter policy to use accept list */
267280740ebbSLuiz Augusto von Dentz 	return filter_policy;
2673e8907f76SLuiz Augusto von Dentz }
2674e8907f76SLuiz Augusto von Dentz 
2675e8907f76SLuiz Augusto von Dentz /* Returns true if an le connection is in the scanning state */
2676e8907f76SLuiz Augusto von Dentz static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
2677e8907f76SLuiz Augusto von Dentz {
2678e8907f76SLuiz Augusto von Dentz 	struct hci_conn_hash *h = &hdev->conn_hash;
2679e8907f76SLuiz Augusto von Dentz 	struct hci_conn  *c;
2680e8907f76SLuiz Augusto von Dentz 
2681e8907f76SLuiz Augusto von Dentz 	rcu_read_lock();
2682e8907f76SLuiz Augusto von Dentz 
2683e8907f76SLuiz Augusto von Dentz 	list_for_each_entry_rcu(c, &h->list, list) {
2684e8907f76SLuiz Augusto von Dentz 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
2685e8907f76SLuiz Augusto von Dentz 		    test_bit(HCI_CONN_SCANNING, &c->flags)) {
2686e8907f76SLuiz Augusto von Dentz 			rcu_read_unlock();
2687e8907f76SLuiz Augusto von Dentz 			return true;
2688e8907f76SLuiz Augusto von Dentz 		}
2689e8907f76SLuiz Augusto von Dentz 	}
2690e8907f76SLuiz Augusto von Dentz 
2691e8907f76SLuiz Augusto von Dentz 	rcu_read_unlock();
2692e8907f76SLuiz Augusto von Dentz 
2693e8907f76SLuiz Augusto von Dentz 	return false;
2694e8907f76SLuiz Augusto von Dentz }
2695e8907f76SLuiz Augusto von Dentz 
2696e8907f76SLuiz Augusto von Dentz static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2697e8907f76SLuiz Augusto von Dentz 					  u16 interval, u16 window,
2698e8907f76SLuiz Augusto von Dentz 					  u8 own_addr_type, u8 filter_policy)
2699e8907f76SLuiz Augusto von Dentz {
2700e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_scan_params *cp;
2701e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_scan_phy_params *phy;
2702e8907f76SLuiz Augusto von Dentz 	u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2703e8907f76SLuiz Augusto von Dentz 	u8 num_phy = 0;
2704e8907f76SLuiz Augusto von Dentz 
2705e8907f76SLuiz Augusto von Dentz 	cp = (void *)data;
2706e8907f76SLuiz Augusto von Dentz 	phy = (void *)cp->data;
2707e8907f76SLuiz Augusto von Dentz 
2708e8907f76SLuiz Augusto von Dentz 	memset(data, 0, sizeof(data));
2709e8907f76SLuiz Augusto von Dentz 
2710e8907f76SLuiz Augusto von Dentz 	cp->own_addr_type = own_addr_type;
2711e8907f76SLuiz Augusto von Dentz 	cp->filter_policy = filter_policy;
2712e8907f76SLuiz Augusto von Dentz 
2713e8907f76SLuiz Augusto von Dentz 	if (scan_1m(hdev) || scan_2m(hdev)) {
2714e8907f76SLuiz Augusto von Dentz 		cp->scanning_phys |= LE_SCAN_PHY_1M;
2715e8907f76SLuiz Augusto von Dentz 
2716e8907f76SLuiz Augusto von Dentz 		phy->type = type;
2717e8907f76SLuiz Augusto von Dentz 		phy->interval = cpu_to_le16(interval);
2718e8907f76SLuiz Augusto von Dentz 		phy->window = cpu_to_le16(window);
2719e8907f76SLuiz Augusto von Dentz 
2720e8907f76SLuiz Augusto von Dentz 		num_phy++;
2721e8907f76SLuiz Augusto von Dentz 		phy++;
2722e8907f76SLuiz Augusto von Dentz 	}
2723e8907f76SLuiz Augusto von Dentz 
2724e8907f76SLuiz Augusto von Dentz 	if (scan_coded(hdev)) {
2725e8907f76SLuiz Augusto von Dentz 		cp->scanning_phys |= LE_SCAN_PHY_CODED;
2726e8907f76SLuiz Augusto von Dentz 
2727e8907f76SLuiz Augusto von Dentz 		phy->type = type;
2728e8907f76SLuiz Augusto von Dentz 		phy->interval = cpu_to_le16(interval);
2729e8907f76SLuiz Augusto von Dentz 		phy->window = cpu_to_le16(window);
2730e8907f76SLuiz Augusto von Dentz 
2731e8907f76SLuiz Augusto von Dentz 		num_phy++;
2732e8907f76SLuiz Augusto von Dentz 		phy++;
2733e8907f76SLuiz Augusto von Dentz 	}
2734e8907f76SLuiz Augusto von Dentz 
2735e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2736e8907f76SLuiz Augusto von Dentz 				     sizeof(*cp) + sizeof(*phy) * num_phy,
2737e8907f76SLuiz Augusto von Dentz 				     data, HCI_CMD_TIMEOUT);
2738e8907f76SLuiz Augusto von Dentz }
2739e8907f76SLuiz Augusto von Dentz 
2740e8907f76SLuiz Augusto von Dentz static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2741e8907f76SLuiz Augusto von Dentz 				      u16 interval, u16 window,
2742e8907f76SLuiz Augusto von Dentz 				      u8 own_addr_type, u8 filter_policy)
2743e8907f76SLuiz Augusto von Dentz {
2744e8907f76SLuiz Augusto von Dentz 	struct hci_cp_le_set_scan_param cp;
2745e8907f76SLuiz Augusto von Dentz 
2746e8907f76SLuiz Augusto von Dentz 	if (use_ext_scan(hdev))
2747e8907f76SLuiz Augusto von Dentz 		return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2748e8907f76SLuiz Augusto von Dentz 						      window, own_addr_type,
2749e8907f76SLuiz Augusto von Dentz 						      filter_policy);
2750e8907f76SLuiz Augusto von Dentz 
2751e8907f76SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
2752e8907f76SLuiz Augusto von Dentz 	cp.type = type;
2753e8907f76SLuiz Augusto von Dentz 	cp.interval = cpu_to_le16(interval);
2754e8907f76SLuiz Augusto von Dentz 	cp.window = cpu_to_le16(window);
2755e8907f76SLuiz Augusto von Dentz 	cp.own_address_type = own_addr_type;
2756e8907f76SLuiz Augusto von Dentz 	cp.filter_policy = filter_policy;
2757e8907f76SLuiz Augusto von Dentz 
2758e8907f76SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2759e8907f76SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2760e8907f76SLuiz Augusto von Dentz }
2761e8907f76SLuiz Augusto von Dentz 
2762e8907f76SLuiz Augusto von Dentz static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2763abfeea47SLuiz Augusto von Dentz 			       u16 window, u8 own_addr_type, u8 filter_policy,
2764abfeea47SLuiz Augusto von Dentz 			       u8 filter_dup)
2765e8907f76SLuiz Augusto von Dentz {
2766e8907f76SLuiz Augusto von Dentz 	int err;
2767e8907f76SLuiz Augusto von Dentz 
2768e8907f76SLuiz Augusto von Dentz 	if (hdev->scanning_paused) {
2769e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
2770e8907f76SLuiz Augusto von Dentz 		return 0;
2771e8907f76SLuiz Augusto von Dentz 	}
2772e8907f76SLuiz Augusto von Dentz 
2773e8907f76SLuiz Augusto von Dentz 	err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2774e8907f76SLuiz Augusto von Dentz 					 own_addr_type, filter_policy);
2775e8907f76SLuiz Augusto von Dentz 	if (err)
2776e8907f76SLuiz Augusto von Dentz 		return err;
2777e8907f76SLuiz Augusto von Dentz 
2778abfeea47SLuiz Augusto von Dentz 	return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2779e8907f76SLuiz Augusto von Dentz }
2780e8907f76SLuiz Augusto von Dentz 
278127592ca1SLuiz Augusto von Dentz static int hci_passive_scan_sync(struct hci_dev *hdev)
2782e8907f76SLuiz Augusto von Dentz {
2783e8907f76SLuiz Augusto von Dentz 	u8 own_addr_type;
2784e8907f76SLuiz Augusto von Dentz 	u8 filter_policy;
2785e8907f76SLuiz Augusto von Dentz 	u16 window, interval;
2786b338d917SBrian Gix 	u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2787ad383c2cSLuiz Augusto von Dentz 	int err;
2788e8907f76SLuiz Augusto von Dentz 
2789e8907f76SLuiz Augusto von Dentz 	if (hdev->scanning_paused) {
2790e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
2791e8907f76SLuiz Augusto von Dentz 		return 0;
2792e8907f76SLuiz Augusto von Dentz 	}
2793e8907f76SLuiz Augusto von Dentz 
2794ad383c2cSLuiz Augusto von Dentz 	err = hci_scan_disable_sync(hdev);
2795ad383c2cSLuiz Augusto von Dentz 	if (err) {
2796ad383c2cSLuiz Augusto von Dentz 		bt_dev_err(hdev, "disable scanning failed: %d", err);
2797ad383c2cSLuiz Augusto von Dentz 		return err;
2798ad383c2cSLuiz Augusto von Dentz 	}
2799ad383c2cSLuiz Augusto von Dentz 
2800e8907f76SLuiz Augusto von Dentz 	/* Set require_privacy to false since no SCAN_REQ are send
2801e8907f76SLuiz Augusto von Dentz 	 * during passive scanning. Not using an non-resolvable address
2802e8907f76SLuiz Augusto von Dentz 	 * here is important so that peer devices using direct
2803e8907f76SLuiz Augusto von Dentz 	 * advertising with our address will be correctly reported
2804e8907f76SLuiz Augusto von Dentz 	 * by the controller.
2805e8907f76SLuiz Augusto von Dentz 	 */
2806e8907f76SLuiz Augusto von Dentz 	if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2807e8907f76SLuiz Augusto von Dentz 					   &own_addr_type))
2808e8907f76SLuiz Augusto von Dentz 		return 0;
2809e8907f76SLuiz Augusto von Dentz 
2810e8907f76SLuiz Augusto von Dentz 	if (hdev->enable_advmon_interleave_scan &&
2811e8907f76SLuiz Augusto von Dentz 	    hci_update_interleaved_scan_sync(hdev))
2812e8907f76SLuiz Augusto von Dentz 		return 0;
2813e8907f76SLuiz Augusto von Dentz 
2814e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2815ad383c2cSLuiz Augusto von Dentz 
2816e8907f76SLuiz Augusto von Dentz 	/* Adding or removing entries from the accept list must
2817e8907f76SLuiz Augusto von Dentz 	 * happen before enabling scanning. The controller does
2818e8907f76SLuiz Augusto von Dentz 	 * not allow accept list modification while scanning.
2819e8907f76SLuiz Augusto von Dentz 	 */
2820e8907f76SLuiz Augusto von Dentz 	filter_policy = hci_update_accept_list_sync(hdev);
2821e8907f76SLuiz Augusto von Dentz 
2822e8907f76SLuiz Augusto von Dentz 	/* When the controller is using random resolvable addresses and
2823e8907f76SLuiz Augusto von Dentz 	 * with that having LE privacy enabled, then controllers with
2824e8907f76SLuiz Augusto von Dentz 	 * Extended Scanner Filter Policies support can now enable support
2825e8907f76SLuiz Augusto von Dentz 	 * for handling directed advertising.
2826e8907f76SLuiz Augusto von Dentz 	 *
2827e8907f76SLuiz Augusto von Dentz 	 * So instead of using filter polices 0x00 (no acceptlist)
2828e8907f76SLuiz Augusto von Dentz 	 * and 0x01 (acceptlist enabled) use the new filter policies
2829e8907f76SLuiz Augusto von Dentz 	 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2830e8907f76SLuiz Augusto von Dentz 	 */
2831e8907f76SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2832e8907f76SLuiz Augusto von Dentz 	    (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2833e8907f76SLuiz Augusto von Dentz 		filter_policy |= 0x02;
2834e8907f76SLuiz Augusto von Dentz 
2835e8907f76SLuiz Augusto von Dentz 	if (hdev->suspended) {
2836e8907f76SLuiz Augusto von Dentz 		window = hdev->le_scan_window_suspend;
2837e8907f76SLuiz Augusto von Dentz 		interval = hdev->le_scan_int_suspend;
2838e8907f76SLuiz Augusto von Dentz 	} else if (hci_is_le_conn_scanning(hdev)) {
2839e8907f76SLuiz Augusto von Dentz 		window = hdev->le_scan_window_connect;
2840e8907f76SLuiz Augusto von Dentz 		interval = hdev->le_scan_int_connect;
2841e8907f76SLuiz Augusto von Dentz 	} else if (hci_is_adv_monitoring(hdev)) {
2842e8907f76SLuiz Augusto von Dentz 		window = hdev->le_scan_window_adv_monitor;
2843e8907f76SLuiz Augusto von Dentz 		interval = hdev->le_scan_int_adv_monitor;
2844e8907f76SLuiz Augusto von Dentz 	} else {
2845e8907f76SLuiz Augusto von Dentz 		window = hdev->le_scan_window;
2846e8907f76SLuiz Augusto von Dentz 		interval = hdev->le_scan_interval;
2847e8907f76SLuiz Augusto von Dentz 	}
2848e8907f76SLuiz Augusto von Dentz 
2849b338d917SBrian Gix 	/* Disable all filtering for Mesh */
2850b338d917SBrian Gix 	if (hci_dev_test_flag(hdev, HCI_MESH)) {
2851b338d917SBrian Gix 		filter_policy = 0;
2852b338d917SBrian Gix 		filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
2853b338d917SBrian Gix 	}
2854b338d917SBrian Gix 
2855e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2856e8907f76SLuiz Augusto von Dentz 
2857e8907f76SLuiz Augusto von Dentz 	return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2858b338d917SBrian Gix 				   own_addr_type, filter_policy, filter_dups);
2859e8907f76SLuiz Augusto von Dentz }
2860e8907f76SLuiz Augusto von Dentz 
2861e8907f76SLuiz Augusto von Dentz /* This function controls the passive scanning based on hdev->pend_le_conns
2862e8907f76SLuiz Augusto von Dentz  * list. If there are pending LE connection we start the background scanning,
2863ad383c2cSLuiz Augusto von Dentz  * otherwise we stop it in the following sequence:
2864ad383c2cSLuiz Augusto von Dentz  *
2865ad383c2cSLuiz Augusto von Dentz  * If there are devices to scan:
2866ad383c2cSLuiz Augusto von Dentz  *
2867ad383c2cSLuiz Augusto von Dentz  * Disable Scanning -> Update Accept List ->
2868ad383c2cSLuiz Augusto von Dentz  * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2869ad383c2cSLuiz Augusto von Dentz  * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2870ad383c2cSLuiz Augusto von Dentz  * Enable Scanning
2871ad383c2cSLuiz Augusto von Dentz  *
2872ad383c2cSLuiz Augusto von Dentz  * Otherwise:
2873ad383c2cSLuiz Augusto von Dentz  *
2874ad383c2cSLuiz Augusto von Dentz  * Disable Scanning
2875e8907f76SLuiz Augusto von Dentz  */
2876e8907f76SLuiz Augusto von Dentz int hci_update_passive_scan_sync(struct hci_dev *hdev)
2877e8907f76SLuiz Augusto von Dentz {
2878e8907f76SLuiz Augusto von Dentz 	int err;
2879e8907f76SLuiz Augusto von Dentz 
2880e8907f76SLuiz Augusto von Dentz 	if (!test_bit(HCI_UP, &hdev->flags) ||
2881e8907f76SLuiz Augusto von Dentz 	    test_bit(HCI_INIT, &hdev->flags) ||
2882e8907f76SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_SETUP) ||
2883e8907f76SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_CONFIG) ||
2884e8907f76SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2885e8907f76SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_UNREGISTER))
2886e8907f76SLuiz Augusto von Dentz 		return 0;
2887e8907f76SLuiz Augusto von Dentz 
2888e8907f76SLuiz Augusto von Dentz 	/* No point in doing scanning if LE support hasn't been enabled */
2889e8907f76SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2890e8907f76SLuiz Augusto von Dentz 		return 0;
2891e8907f76SLuiz Augusto von Dentz 
2892e8907f76SLuiz Augusto von Dentz 	/* If discovery is active don't interfere with it */
2893e8907f76SLuiz Augusto von Dentz 	if (hdev->discovery.state != DISCOVERY_STOPPED)
2894e8907f76SLuiz Augusto von Dentz 		return 0;
2895e8907f76SLuiz Augusto von Dentz 
2896e8907f76SLuiz Augusto von Dentz 	/* Reset RSSI and UUID filters when starting background scanning
2897e8907f76SLuiz Augusto von Dentz 	 * since these filters are meant for service discovery only.
2898e8907f76SLuiz Augusto von Dentz 	 *
2899e8907f76SLuiz Augusto von Dentz 	 * The Start Discovery and Start Service Discovery operations
2900e8907f76SLuiz Augusto von Dentz 	 * ensure to set proper values for RSSI threshold and UUID
2901e8907f76SLuiz Augusto von Dentz 	 * filter list. So it is safe to just reset them here.
2902e8907f76SLuiz Augusto von Dentz 	 */
2903e8907f76SLuiz Augusto von Dentz 	hci_discovery_filter_clear(hdev);
2904e8907f76SLuiz Augusto von Dentz 
2905e8907f76SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "ADV monitoring is %s",
2906e8907f76SLuiz Augusto von Dentz 		   hci_is_adv_monitoring(hdev) ? "on" : "off");
2907e8907f76SLuiz Augusto von Dentz 
2908b338d917SBrian Gix 	if (!hci_dev_test_flag(hdev, HCI_MESH) &&
2909b338d917SBrian Gix 	    list_empty(&hdev->pend_le_conns) &&
2910e8907f76SLuiz Augusto von Dentz 	    list_empty(&hdev->pend_le_reports) &&
2911eca0ae4aSLuiz Augusto von Dentz 	    !hci_is_adv_monitoring(hdev) &&
2912eca0ae4aSLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2913e8907f76SLuiz Augusto von Dentz 		/* If there is no pending LE connections or devices
2914e8907f76SLuiz Augusto von Dentz 		 * to be scanned for or no ADV monitors, we should stop the
2915e8907f76SLuiz Augusto von Dentz 		 * background scanning.
2916e8907f76SLuiz Augusto von Dentz 		 */
2917e8907f76SLuiz Augusto von Dentz 
2918e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "stopping background scanning");
2919e8907f76SLuiz Augusto von Dentz 
2920ad383c2cSLuiz Augusto von Dentz 		err = hci_scan_disable_sync(hdev);
2921e8907f76SLuiz Augusto von Dentz 		if (err)
2922e8907f76SLuiz Augusto von Dentz 			bt_dev_err(hdev, "stop background scanning failed: %d",
2923e8907f76SLuiz Augusto von Dentz 				   err);
2924e8907f76SLuiz Augusto von Dentz 	} else {
2925e8907f76SLuiz Augusto von Dentz 		/* If there is at least one pending LE connection, we should
2926e8907f76SLuiz Augusto von Dentz 		 * keep the background scan running.
2927e8907f76SLuiz Augusto von Dentz 		 */
2928e8907f76SLuiz Augusto von Dentz 
2929e8907f76SLuiz Augusto von Dentz 		/* If controller is connecting, we should not start scanning
2930e8907f76SLuiz Augusto von Dentz 		 * since some controllers are not able to scan and connect at
2931e8907f76SLuiz Augusto von Dentz 		 * the same time.
2932e8907f76SLuiz Augusto von Dentz 		 */
2933e8907f76SLuiz Augusto von Dentz 		if (hci_lookup_le_connect(hdev))
2934e8907f76SLuiz Augusto von Dentz 			return 0;
2935e8907f76SLuiz Augusto von Dentz 
2936e8907f76SLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "start background scanning");
2937e8907f76SLuiz Augusto von Dentz 
2938e8907f76SLuiz Augusto von Dentz 		err = hci_passive_scan_sync(hdev);
2939e8907f76SLuiz Augusto von Dentz 		if (err)
2940e8907f76SLuiz Augusto von Dentz 			bt_dev_err(hdev, "start background scanning failed: %d",
2941e8907f76SLuiz Augusto von Dentz 				   err);
2942e8907f76SLuiz Augusto von Dentz 	}
2943e8907f76SLuiz Augusto von Dentz 
2944e8907f76SLuiz Augusto von Dentz 	return err;
2945e8907f76SLuiz Augusto von Dentz }
2946ad383c2cSLuiz Augusto von Dentz 
2947bb876725SBrian Gix static int update_scan_sync(struct hci_dev *hdev, void *data)
2948bb876725SBrian Gix {
2949bb876725SBrian Gix 	return hci_update_scan_sync(hdev);
2950bb876725SBrian Gix }
2951bb876725SBrian Gix 
2952bb876725SBrian Gix int hci_update_scan(struct hci_dev *hdev)
2953bb876725SBrian Gix {
2954bb876725SBrian Gix 	return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
2955bb876725SBrian Gix }
2956bb876725SBrian Gix 
2957ad383c2cSLuiz Augusto von Dentz static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2958ad383c2cSLuiz Augusto von Dentz {
2959ad383c2cSLuiz Augusto von Dentz 	return hci_update_passive_scan_sync(hdev);
2960ad383c2cSLuiz Augusto von Dentz }
2961ad383c2cSLuiz Augusto von Dentz 
2962ad383c2cSLuiz Augusto von Dentz int hci_update_passive_scan(struct hci_dev *hdev)
2963ad383c2cSLuiz Augusto von Dentz {
29645bee2fd6SLuiz Augusto von Dentz 	/* Only queue if it would have any effect */
29655bee2fd6SLuiz Augusto von Dentz 	if (!test_bit(HCI_UP, &hdev->flags) ||
29665bee2fd6SLuiz Augusto von Dentz 	    test_bit(HCI_INIT, &hdev->flags) ||
29675bee2fd6SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_SETUP) ||
29685bee2fd6SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_CONFIG) ||
29695bee2fd6SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
29705bee2fd6SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_UNREGISTER))
29715bee2fd6SLuiz Augusto von Dentz 		return 0;
29725bee2fd6SLuiz Augusto von Dentz 
2973ad383c2cSLuiz Augusto von Dentz 	return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2974ad383c2cSLuiz Augusto von Dentz }
2975cf75ad8bSLuiz Augusto von Dentz 
29762f2eb0c9SBrian Gix int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2977cf75ad8bSLuiz Augusto von Dentz {
29782f2eb0c9SBrian Gix 	int err;
29792f2eb0c9SBrian Gix 
2980cf75ad8bSLuiz Augusto von Dentz 	if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2981cf75ad8bSLuiz Augusto von Dentz 		return 0;
2982cf75ad8bSLuiz Augusto von Dentz 
29832f2eb0c9SBrian Gix 	err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2984cf75ad8bSLuiz Augusto von Dentz 				    sizeof(val), &val, HCI_CMD_TIMEOUT);
29852f2eb0c9SBrian Gix 
29862f2eb0c9SBrian Gix 	if (!err) {
29872f2eb0c9SBrian Gix 		if (val) {
29882f2eb0c9SBrian Gix 			hdev->features[1][0] |= LMP_HOST_SC;
29892f2eb0c9SBrian Gix 			hci_dev_set_flag(hdev, HCI_SC_ENABLED);
29902f2eb0c9SBrian Gix 		} else {
29912f2eb0c9SBrian Gix 			hdev->features[1][0] &= ~LMP_HOST_SC;
29922f2eb0c9SBrian Gix 			hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
29932f2eb0c9SBrian Gix 		}
29942f2eb0c9SBrian Gix 	}
29952f2eb0c9SBrian Gix 
29962f2eb0c9SBrian Gix 	return err;
2997cf75ad8bSLuiz Augusto von Dentz }
2998cf75ad8bSLuiz Augusto von Dentz 
29993244845cSBrian Gix int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
3000cf75ad8bSLuiz Augusto von Dentz {
3001cf75ad8bSLuiz Augusto von Dentz 	int err;
3002cf75ad8bSLuiz Augusto von Dentz 
3003cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3004cf75ad8bSLuiz Augusto von Dentz 	    lmp_host_ssp_capable(hdev))
3005cf75ad8bSLuiz Augusto von Dentz 		return 0;
3006cf75ad8bSLuiz Augusto von Dentz 
30073244845cSBrian Gix 	if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
30083244845cSBrian Gix 		__hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
30093244845cSBrian Gix 				      sizeof(mode), &mode, HCI_CMD_TIMEOUT);
30103244845cSBrian Gix 	}
30113244845cSBrian Gix 
3012cf75ad8bSLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3013cf75ad8bSLuiz Augusto von Dentz 				    sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3014cf75ad8bSLuiz Augusto von Dentz 	if (err)
3015cf75ad8bSLuiz Augusto von Dentz 		return err;
3016cf75ad8bSLuiz Augusto von Dentz 
3017cf75ad8bSLuiz Augusto von Dentz 	return hci_write_sc_support_sync(hdev, 0x01);
3018cf75ad8bSLuiz Augusto von Dentz }
3019cf75ad8bSLuiz Augusto von Dentz 
3020d81a494cSBrian Gix int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
3021cf75ad8bSLuiz Augusto von Dentz {
3022cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_write_le_host_supported cp;
3023cf75ad8bSLuiz Augusto von Dentz 
3024cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
3025cf75ad8bSLuiz Augusto von Dentz 	    !lmp_bredr_capable(hdev))
3026cf75ad8bSLuiz Augusto von Dentz 		return 0;
3027cf75ad8bSLuiz Augusto von Dentz 
3028cf75ad8bSLuiz Augusto von Dentz 	/* Check first if we already have the right host state
3029cf75ad8bSLuiz Augusto von Dentz 	 * (host features set)
3030cf75ad8bSLuiz Augusto von Dentz 	 */
3031cf75ad8bSLuiz Augusto von Dentz 	if (le == lmp_host_le_capable(hdev) &&
3032cf75ad8bSLuiz Augusto von Dentz 	    simul == lmp_host_le_br_capable(hdev))
3033cf75ad8bSLuiz Augusto von Dentz 		return 0;
3034cf75ad8bSLuiz Augusto von Dentz 
3035cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3036cf75ad8bSLuiz Augusto von Dentz 
3037cf75ad8bSLuiz Augusto von Dentz 	cp.le = le;
3038cf75ad8bSLuiz Augusto von Dentz 	cp.simul = simul;
3039cf75ad8bSLuiz Augusto von Dentz 
3040cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3041cf75ad8bSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3042cf75ad8bSLuiz Augusto von Dentz }
3043cf75ad8bSLuiz Augusto von Dentz 
3044cf75ad8bSLuiz Augusto von Dentz static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3045cf75ad8bSLuiz Augusto von Dentz {
3046cf75ad8bSLuiz Augusto von Dentz 	struct adv_info *adv, *tmp;
3047cf75ad8bSLuiz Augusto von Dentz 	int err;
3048cf75ad8bSLuiz Augusto von Dentz 
3049cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3050cf75ad8bSLuiz Augusto von Dentz 		return 0;
3051cf75ad8bSLuiz Augusto von Dentz 
3052cf75ad8bSLuiz Augusto von Dentz 	/* If RPA Resolution has not been enable yet it means the
3053cf75ad8bSLuiz Augusto von Dentz 	 * resolving list is empty and we should attempt to program the
3054cf75ad8bSLuiz Augusto von Dentz 	 * local IRK in order to support using own_addr_type
3055cf75ad8bSLuiz Augusto von Dentz 	 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
3056cf75ad8bSLuiz Augusto von Dentz 	 */
3057cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3058cf75ad8bSLuiz Augusto von Dentz 		hci_le_add_resolve_list_sync(hdev, NULL);
3059cf75ad8bSLuiz Augusto von Dentz 		hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3060cf75ad8bSLuiz Augusto von Dentz 	}
3061cf75ad8bSLuiz Augusto von Dentz 
3062cf75ad8bSLuiz Augusto von Dentz 	/* Make sure the controller has a good default for
3063cf75ad8bSLuiz Augusto von Dentz 	 * advertising data. This also applies to the case
3064cf75ad8bSLuiz Augusto von Dentz 	 * where BR/EDR was toggled during the AUTO_OFF phase.
3065cf75ad8bSLuiz Augusto von Dentz 	 */
3066cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3067cf75ad8bSLuiz Augusto von Dentz 	    list_empty(&hdev->adv_instances)) {
3068cf75ad8bSLuiz Augusto von Dentz 		if (ext_adv_capable(hdev)) {
3069cf75ad8bSLuiz Augusto von Dentz 			err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3070cf75ad8bSLuiz Augusto von Dentz 			if (!err)
3071cf75ad8bSLuiz Augusto von Dentz 				hci_update_scan_rsp_data_sync(hdev, 0x00);
3072cf75ad8bSLuiz Augusto von Dentz 		} else {
3073cf75ad8bSLuiz Augusto von Dentz 			err = hci_update_adv_data_sync(hdev, 0x00);
3074cf75ad8bSLuiz Augusto von Dentz 			if (!err)
3075cf75ad8bSLuiz Augusto von Dentz 				hci_update_scan_rsp_data_sync(hdev, 0x00);
3076cf75ad8bSLuiz Augusto von Dentz 		}
3077cf75ad8bSLuiz Augusto von Dentz 
3078cf75ad8bSLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3079cf75ad8bSLuiz Augusto von Dentz 			hci_enable_advertising_sync(hdev);
3080cf75ad8bSLuiz Augusto von Dentz 	}
3081cf75ad8bSLuiz Augusto von Dentz 
3082cf75ad8bSLuiz Augusto von Dentz 	/* Call for each tracked instance to be scheduled */
3083cf75ad8bSLuiz Augusto von Dentz 	list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3084cf75ad8bSLuiz Augusto von Dentz 		hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3085cf75ad8bSLuiz Augusto von Dentz 
3086cf75ad8bSLuiz Augusto von Dentz 	return 0;
3087cf75ad8bSLuiz Augusto von Dentz }
3088cf75ad8bSLuiz Augusto von Dentz 
3089cf75ad8bSLuiz Augusto von Dentz static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3090cf75ad8bSLuiz Augusto von Dentz {
3091cf75ad8bSLuiz Augusto von Dentz 	u8 link_sec;
3092cf75ad8bSLuiz Augusto von Dentz 
3093cf75ad8bSLuiz Augusto von Dentz 	link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3094cf75ad8bSLuiz Augusto von Dentz 	if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3095cf75ad8bSLuiz Augusto von Dentz 		return 0;
3096cf75ad8bSLuiz Augusto von Dentz 
3097cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3098cf75ad8bSLuiz Augusto von Dentz 				     sizeof(link_sec), &link_sec,
3099cf75ad8bSLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
3100cf75ad8bSLuiz Augusto von Dentz }
3101cf75ad8bSLuiz Augusto von Dentz 
3102353a0249SBrian Gix int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3103cf75ad8bSLuiz Augusto von Dentz {
3104cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_write_page_scan_activity cp;
3105cf75ad8bSLuiz Augusto von Dentz 	u8 type;
3106cf75ad8bSLuiz Augusto von Dentz 	int err = 0;
3107cf75ad8bSLuiz Augusto von Dentz 
3108cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3109cf75ad8bSLuiz Augusto von Dentz 		return 0;
3110cf75ad8bSLuiz Augusto von Dentz 
3111cf75ad8bSLuiz Augusto von Dentz 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3112cf75ad8bSLuiz Augusto von Dentz 		return 0;
3113cf75ad8bSLuiz Augusto von Dentz 
3114cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3115cf75ad8bSLuiz Augusto von Dentz 
3116cf75ad8bSLuiz Augusto von Dentz 	if (enable) {
3117cf75ad8bSLuiz Augusto von Dentz 		type = PAGE_SCAN_TYPE_INTERLACED;
3118cf75ad8bSLuiz Augusto von Dentz 
3119cf75ad8bSLuiz Augusto von Dentz 		/* 160 msec page scan interval */
3120cf75ad8bSLuiz Augusto von Dentz 		cp.interval = cpu_to_le16(0x0100);
3121cf75ad8bSLuiz Augusto von Dentz 	} else {
3122cf75ad8bSLuiz Augusto von Dentz 		type = hdev->def_page_scan_type;
3123cf75ad8bSLuiz Augusto von Dentz 		cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3124cf75ad8bSLuiz Augusto von Dentz 	}
3125cf75ad8bSLuiz Augusto von Dentz 
3126cf75ad8bSLuiz Augusto von Dentz 	cp.window = cpu_to_le16(hdev->def_page_scan_window);
3127cf75ad8bSLuiz Augusto von Dentz 
3128cf75ad8bSLuiz Augusto von Dentz 	if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3129cf75ad8bSLuiz Augusto von Dentz 	    __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3130cf75ad8bSLuiz Augusto von Dentz 		err = __hci_cmd_sync_status(hdev,
3131cf75ad8bSLuiz Augusto von Dentz 					    HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
3132cf75ad8bSLuiz Augusto von Dentz 					    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3133cf75ad8bSLuiz Augusto von Dentz 		if (err)
3134cf75ad8bSLuiz Augusto von Dentz 			return err;
3135cf75ad8bSLuiz Augusto von Dentz 	}
3136cf75ad8bSLuiz Augusto von Dentz 
3137cf75ad8bSLuiz Augusto von Dentz 	if (hdev->page_scan_type != type)
3138cf75ad8bSLuiz Augusto von Dentz 		err = __hci_cmd_sync_status(hdev,
3139cf75ad8bSLuiz Augusto von Dentz 					    HCI_OP_WRITE_PAGE_SCAN_TYPE,
3140cf75ad8bSLuiz Augusto von Dentz 					    sizeof(type), &type,
3141cf75ad8bSLuiz Augusto von Dentz 					    HCI_CMD_TIMEOUT);
3142cf75ad8bSLuiz Augusto von Dentz 
3143cf75ad8bSLuiz Augusto von Dentz 	return err;
3144cf75ad8bSLuiz Augusto von Dentz }
3145cf75ad8bSLuiz Augusto von Dentz 
3146cf75ad8bSLuiz Augusto von Dentz static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3147cf75ad8bSLuiz Augusto von Dentz {
3148cf75ad8bSLuiz Augusto von Dentz 	struct bdaddr_list *b;
3149cf75ad8bSLuiz Augusto von Dentz 
3150cf75ad8bSLuiz Augusto von Dentz 	list_for_each_entry(b, &hdev->accept_list, list) {
3151cf75ad8bSLuiz Augusto von Dentz 		struct hci_conn *conn;
3152cf75ad8bSLuiz Augusto von Dentz 
3153cf75ad8bSLuiz Augusto von Dentz 		conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3154cf75ad8bSLuiz Augusto von Dentz 		if (!conn)
3155cf75ad8bSLuiz Augusto von Dentz 			return true;
3156cf75ad8bSLuiz Augusto von Dentz 
3157cf75ad8bSLuiz Augusto von Dentz 		if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3158cf75ad8bSLuiz Augusto von Dentz 			return true;
3159cf75ad8bSLuiz Augusto von Dentz 	}
3160cf75ad8bSLuiz Augusto von Dentz 
3161cf75ad8bSLuiz Augusto von Dentz 	return false;
3162cf75ad8bSLuiz Augusto von Dentz }
3163cf75ad8bSLuiz Augusto von Dentz 
3164cf75ad8bSLuiz Augusto von Dentz static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3165cf75ad8bSLuiz Augusto von Dentz {
3166cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3167cf75ad8bSLuiz Augusto von Dentz 					    sizeof(val), &val,
3168cf75ad8bSLuiz Augusto von Dentz 					    HCI_CMD_TIMEOUT);
3169cf75ad8bSLuiz Augusto von Dentz }
3170cf75ad8bSLuiz Augusto von Dentz 
3171451d95a9SBrian Gix int hci_update_scan_sync(struct hci_dev *hdev)
3172cf75ad8bSLuiz Augusto von Dentz {
3173cf75ad8bSLuiz Augusto von Dentz 	u8 scan;
3174cf75ad8bSLuiz Augusto von Dentz 
3175cf75ad8bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3176cf75ad8bSLuiz Augusto von Dentz 		return 0;
3177cf75ad8bSLuiz Augusto von Dentz 
3178cf75ad8bSLuiz Augusto von Dentz 	if (!hdev_is_powered(hdev))
3179cf75ad8bSLuiz Augusto von Dentz 		return 0;
3180cf75ad8bSLuiz Augusto von Dentz 
3181cf75ad8bSLuiz Augusto von Dentz 	if (mgmt_powering_down(hdev))
3182cf75ad8bSLuiz Augusto von Dentz 		return 0;
3183cf75ad8bSLuiz Augusto von Dentz 
3184cf75ad8bSLuiz Augusto von Dentz 	if (hdev->scanning_paused)
3185cf75ad8bSLuiz Augusto von Dentz 		return 0;
3186cf75ad8bSLuiz Augusto von Dentz 
3187cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3188cf75ad8bSLuiz Augusto von Dentz 	    disconnected_accept_list_entries(hdev))
3189cf75ad8bSLuiz Augusto von Dentz 		scan = SCAN_PAGE;
3190cf75ad8bSLuiz Augusto von Dentz 	else
3191cf75ad8bSLuiz Augusto von Dentz 		scan = SCAN_DISABLED;
3192cf75ad8bSLuiz Augusto von Dentz 
3193cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3194cf75ad8bSLuiz Augusto von Dentz 		scan |= SCAN_INQUIRY;
3195cf75ad8bSLuiz Augusto von Dentz 
3196cf75ad8bSLuiz Augusto von Dentz 	if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3197cf75ad8bSLuiz Augusto von Dentz 	    test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3198cf75ad8bSLuiz Augusto von Dentz 		return 0;
3199cf75ad8bSLuiz Augusto von Dentz 
3200cf75ad8bSLuiz Augusto von Dentz 	return hci_write_scan_enable_sync(hdev, scan);
3201cf75ad8bSLuiz Augusto von Dentz }
3202cf75ad8bSLuiz Augusto von Dentz 
32036f6ff38aSBrian Gix int hci_update_name_sync(struct hci_dev *hdev)
3204cf75ad8bSLuiz Augusto von Dentz {
3205cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_write_local_name cp;
3206cf75ad8bSLuiz Augusto von Dentz 
3207cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3208cf75ad8bSLuiz Augusto von Dentz 
3209cf75ad8bSLuiz Augusto von Dentz 	memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3210cf75ad8bSLuiz Augusto von Dentz 
3211cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3212cf75ad8bSLuiz Augusto von Dentz 					    sizeof(cp), &cp,
3213cf75ad8bSLuiz Augusto von Dentz 					    HCI_CMD_TIMEOUT);
3214cf75ad8bSLuiz Augusto von Dentz }
3215cf75ad8bSLuiz Augusto von Dentz 
3216cf75ad8bSLuiz Augusto von Dentz /* This function perform powered update HCI command sequence after the HCI init
3217cf75ad8bSLuiz Augusto von Dentz  * sequence which end up resetting all states, the sequence is as follows:
3218cf75ad8bSLuiz Augusto von Dentz  *
3219cf75ad8bSLuiz Augusto von Dentz  * HCI_SSP_ENABLED(Enable SSP)
3220cf75ad8bSLuiz Augusto von Dentz  * HCI_LE_ENABLED(Enable LE)
3221cf75ad8bSLuiz Augusto von Dentz  * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
3222cf75ad8bSLuiz Augusto von Dentz  * Update adv data)
3223cf75ad8bSLuiz Augusto von Dentz  * Enable Authentication
3224cf75ad8bSLuiz Augusto von Dentz  * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3225cf75ad8bSLuiz Augusto von Dentz  * Set Name -> Set EIR)
3226e411443cSLuiz Augusto von Dentz  * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address)
3227cf75ad8bSLuiz Augusto von Dentz  */
3228cf75ad8bSLuiz Augusto von Dentz int hci_powered_update_sync(struct hci_dev *hdev)
3229cf75ad8bSLuiz Augusto von Dentz {
3230cf75ad8bSLuiz Augusto von Dentz 	int err;
3231cf75ad8bSLuiz Augusto von Dentz 
3232cf75ad8bSLuiz Augusto von Dentz 	/* Register the available SMP channels (BR/EDR and LE) only when
3233cf75ad8bSLuiz Augusto von Dentz 	 * successfully powering on the controller. This late
3234cf75ad8bSLuiz Augusto von Dentz 	 * registration is required so that LE SMP can clearly decide if
3235cf75ad8bSLuiz Augusto von Dentz 	 * the public address or static address is used.
3236cf75ad8bSLuiz Augusto von Dentz 	 */
3237cf75ad8bSLuiz Augusto von Dentz 	smp_register(hdev);
3238cf75ad8bSLuiz Augusto von Dentz 
3239cf75ad8bSLuiz Augusto von Dentz 	err = hci_write_ssp_mode_sync(hdev, 0x01);
3240cf75ad8bSLuiz Augusto von Dentz 	if (err)
3241cf75ad8bSLuiz Augusto von Dentz 		return err;
3242cf75ad8bSLuiz Augusto von Dentz 
3243cf75ad8bSLuiz Augusto von Dentz 	err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3244cf75ad8bSLuiz Augusto von Dentz 	if (err)
3245cf75ad8bSLuiz Augusto von Dentz 		return err;
3246cf75ad8bSLuiz Augusto von Dentz 
3247cf75ad8bSLuiz Augusto von Dentz 	err = hci_powered_update_adv_sync(hdev);
3248cf75ad8bSLuiz Augusto von Dentz 	if (err)
3249cf75ad8bSLuiz Augusto von Dentz 		return err;
3250cf75ad8bSLuiz Augusto von Dentz 
3251cf75ad8bSLuiz Augusto von Dentz 	err = hci_write_auth_enable_sync(hdev);
3252cf75ad8bSLuiz Augusto von Dentz 	if (err)
3253cf75ad8bSLuiz Augusto von Dentz 		return err;
3254cf75ad8bSLuiz Augusto von Dentz 
3255cf75ad8bSLuiz Augusto von Dentz 	if (lmp_bredr_capable(hdev)) {
3256cf75ad8bSLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3257cf75ad8bSLuiz Augusto von Dentz 			hci_write_fast_connectable_sync(hdev, true);
3258cf75ad8bSLuiz Augusto von Dentz 		else
3259cf75ad8bSLuiz Augusto von Dentz 			hci_write_fast_connectable_sync(hdev, false);
3260cf75ad8bSLuiz Augusto von Dentz 		hci_update_scan_sync(hdev);
3261cf75ad8bSLuiz Augusto von Dentz 		hci_update_class_sync(hdev);
3262cf75ad8bSLuiz Augusto von Dentz 		hci_update_name_sync(hdev);
3263cf75ad8bSLuiz Augusto von Dentz 		hci_update_eir_sync(hdev);
3264cf75ad8bSLuiz Augusto von Dentz 	}
3265cf75ad8bSLuiz Augusto von Dentz 
3266e411443cSLuiz Augusto von Dentz 	/* If forcing static address is in use or there is no public
3267e411443cSLuiz Augusto von Dentz 	 * address use the static address as random address (but skip
3268e411443cSLuiz Augusto von Dentz 	 * the HCI command if the current random address is already the
3269e411443cSLuiz Augusto von Dentz 	 * static one.
3270e411443cSLuiz Augusto von Dentz 	 *
3271e411443cSLuiz Augusto von Dentz 	 * In case BR/EDR has been disabled on a dual-mode controller
3272e411443cSLuiz Augusto von Dentz 	 * and a static address has been configured, then use that
3273e411443cSLuiz Augusto von Dentz 	 * address instead of the public BR/EDR address.
3274e411443cSLuiz Augusto von Dentz 	 */
3275e411443cSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3276e411443cSLuiz Augusto von Dentz 	    (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3277e411443cSLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3278e411443cSLuiz Augusto von Dentz 		if (bacmp(&hdev->static_addr, BDADDR_ANY))
3279e411443cSLuiz Augusto von Dentz 			return hci_set_random_addr_sync(hdev,
3280e411443cSLuiz Augusto von Dentz 							&hdev->static_addr);
3281e411443cSLuiz Augusto von Dentz 	}
3282e411443cSLuiz Augusto von Dentz 
3283cf75ad8bSLuiz Augusto von Dentz 	return 0;
3284cf75ad8bSLuiz Augusto von Dentz }
3285cf75ad8bSLuiz Augusto von Dentz 
3286d0b13706SLuiz Augusto von Dentz /**
3287d0b13706SLuiz Augusto von Dentz  * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3288d0b13706SLuiz Augusto von Dentz  *				       (BD_ADDR) for a HCI device from
3289d0b13706SLuiz Augusto von Dentz  *				       a firmware node property.
3290d0b13706SLuiz Augusto von Dentz  * @hdev:	The HCI device
3291d0b13706SLuiz Augusto von Dentz  *
3292d0b13706SLuiz Augusto von Dentz  * Search the firmware node for 'local-bd-address'.
3293d0b13706SLuiz Augusto von Dentz  *
3294d0b13706SLuiz Augusto von Dentz  * All-zero BD addresses are rejected, because those could be properties
3295d0b13706SLuiz Augusto von Dentz  * that exist in the firmware tables, but were not updated by the firmware. For
3296d0b13706SLuiz Augusto von Dentz  * example, the DTS could define 'local-bd-address', with zero BD addresses.
3297d0b13706SLuiz Augusto von Dentz  */
3298d0b13706SLuiz Augusto von Dentz static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3299d0b13706SLuiz Augusto von Dentz {
3300d0b13706SLuiz Augusto von Dentz 	struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3301d0b13706SLuiz Augusto von Dentz 	bdaddr_t ba;
3302d0b13706SLuiz Augusto von Dentz 	int ret;
3303d0b13706SLuiz Augusto von Dentz 
3304d0b13706SLuiz Augusto von Dentz 	ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3305d0b13706SLuiz Augusto von Dentz 					    (u8 *)&ba, sizeof(ba));
3306d0b13706SLuiz Augusto von Dentz 	if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3307d0b13706SLuiz Augusto von Dentz 		return;
3308d0b13706SLuiz Augusto von Dentz 
3309d0b13706SLuiz Augusto von Dentz 	bacpy(&hdev->public_addr, &ba);
3310d0b13706SLuiz Augusto von Dentz }
3311d0b13706SLuiz Augusto von Dentz 
3312d0b13706SLuiz Augusto von Dentz struct hci_init_stage {
3313d0b13706SLuiz Augusto von Dentz 	int (*func)(struct hci_dev *hdev);
3314d0b13706SLuiz Augusto von Dentz };
3315d0b13706SLuiz Augusto von Dentz 
3316d0b13706SLuiz Augusto von Dentz /* Run init stage NULL terminated function table */
3317d0b13706SLuiz Augusto von Dentz static int hci_init_stage_sync(struct hci_dev *hdev,
3318d0b13706SLuiz Augusto von Dentz 			       const struct hci_init_stage *stage)
3319d0b13706SLuiz Augusto von Dentz {
3320d0b13706SLuiz Augusto von Dentz 	size_t i;
3321d0b13706SLuiz Augusto von Dentz 
3322d0b13706SLuiz Augusto von Dentz 	for (i = 0; stage[i].func; i++) {
3323d0b13706SLuiz Augusto von Dentz 		int err;
3324d0b13706SLuiz Augusto von Dentz 
3325d0b13706SLuiz Augusto von Dentz 		err = stage[i].func(hdev);
3326d0b13706SLuiz Augusto von Dentz 		if (err)
3327d0b13706SLuiz Augusto von Dentz 			return err;
3328d0b13706SLuiz Augusto von Dentz 	}
3329d0b13706SLuiz Augusto von Dentz 
3330d0b13706SLuiz Augusto von Dentz 	return 0;
3331d0b13706SLuiz Augusto von Dentz }
3332d0b13706SLuiz Augusto von Dentz 
3333d0b13706SLuiz Augusto von Dentz /* Read Local Version */
3334d0b13706SLuiz Augusto von Dentz static int hci_read_local_version_sync(struct hci_dev *hdev)
3335d0b13706SLuiz Augusto von Dentz {
3336d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3337d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3338d0b13706SLuiz Augusto von Dentz }
3339d0b13706SLuiz Augusto von Dentz 
3340d0b13706SLuiz Augusto von Dentz /* Read BD Address */
3341d0b13706SLuiz Augusto von Dentz static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3342d0b13706SLuiz Augusto von Dentz {
3343d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3344d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3345d0b13706SLuiz Augusto von Dentz }
3346d0b13706SLuiz Augusto von Dentz 
3347d0b13706SLuiz Augusto von Dentz #define HCI_INIT(_func) \
3348d0b13706SLuiz Augusto von Dentz { \
3349d0b13706SLuiz Augusto von Dentz 	.func = _func, \
3350d0b13706SLuiz Augusto von Dentz }
3351d0b13706SLuiz Augusto von Dentz 
3352d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init0[] = {
3353d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_VERSION */
3354d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_version_sync),
3355d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_BD_ADDR */
3356d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_bd_addr_sync),
3357d0b13706SLuiz Augusto von Dentz 	{}
3358d0b13706SLuiz Augusto von Dentz };
3359d0b13706SLuiz Augusto von Dentz 
3360d0b13706SLuiz Augusto von Dentz int hci_reset_sync(struct hci_dev *hdev)
3361d0b13706SLuiz Augusto von Dentz {
3362d0b13706SLuiz Augusto von Dentz 	int err;
3363d0b13706SLuiz Augusto von Dentz 
3364d0b13706SLuiz Augusto von Dentz 	set_bit(HCI_RESET, &hdev->flags);
3365d0b13706SLuiz Augusto von Dentz 
3366d0b13706SLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3367d0b13706SLuiz Augusto von Dentz 				    HCI_CMD_TIMEOUT);
3368d0b13706SLuiz Augusto von Dentz 	if (err)
3369d0b13706SLuiz Augusto von Dentz 		return err;
3370d0b13706SLuiz Augusto von Dentz 
3371d0b13706SLuiz Augusto von Dentz 	return 0;
3372d0b13706SLuiz Augusto von Dentz }
3373d0b13706SLuiz Augusto von Dentz 
3374d0b13706SLuiz Augusto von Dentz static int hci_init0_sync(struct hci_dev *hdev)
3375d0b13706SLuiz Augusto von Dentz {
3376d0b13706SLuiz Augusto von Dentz 	int err;
3377d0b13706SLuiz Augusto von Dentz 
3378d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
3379d0b13706SLuiz Augusto von Dentz 
3380d0b13706SLuiz Augusto von Dentz 	/* Reset */
3381d0b13706SLuiz Augusto von Dentz 	if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3382d0b13706SLuiz Augusto von Dentz 		err = hci_reset_sync(hdev);
3383d0b13706SLuiz Augusto von Dentz 		if (err)
3384d0b13706SLuiz Augusto von Dentz 			return err;
3385d0b13706SLuiz Augusto von Dentz 	}
3386d0b13706SLuiz Augusto von Dentz 
3387d0b13706SLuiz Augusto von Dentz 	return hci_init_stage_sync(hdev, hci_init0);
3388d0b13706SLuiz Augusto von Dentz }
3389d0b13706SLuiz Augusto von Dentz 
3390d0b13706SLuiz Augusto von Dentz static int hci_unconf_init_sync(struct hci_dev *hdev)
3391d0b13706SLuiz Augusto von Dentz {
3392d0b13706SLuiz Augusto von Dentz 	int err;
3393d0b13706SLuiz Augusto von Dentz 
3394d0b13706SLuiz Augusto von Dentz 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3395d0b13706SLuiz Augusto von Dentz 		return 0;
3396d0b13706SLuiz Augusto von Dentz 
3397d0b13706SLuiz Augusto von Dentz 	err = hci_init0_sync(hdev);
3398d0b13706SLuiz Augusto von Dentz 	if (err < 0)
3399d0b13706SLuiz Augusto von Dentz 		return err;
3400d0b13706SLuiz Augusto von Dentz 
3401d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_SETUP))
3402d0b13706SLuiz Augusto von Dentz 		hci_debugfs_create_basic(hdev);
3403d0b13706SLuiz Augusto von Dentz 
3404d0b13706SLuiz Augusto von Dentz 	return 0;
3405d0b13706SLuiz Augusto von Dentz }
3406d0b13706SLuiz Augusto von Dentz 
3407d0b13706SLuiz Augusto von Dentz /* Read Local Supported Features. */
3408d0b13706SLuiz Augusto von Dentz static int hci_read_local_features_sync(struct hci_dev *hdev)
3409d0b13706SLuiz Augusto von Dentz {
3410d0b13706SLuiz Augusto von Dentz 	 /* Not all AMP controllers support this command */
3411d0b13706SLuiz Augusto von Dentz 	if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
3412d0b13706SLuiz Augusto von Dentz 		return 0;
3413d0b13706SLuiz Augusto von Dentz 
3414d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3415d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3416d0b13706SLuiz Augusto von Dentz }
3417d0b13706SLuiz Augusto von Dentz 
3418d0b13706SLuiz Augusto von Dentz /* BR Controller init stage 1 command sequence */
3419d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage br_init1[] = {
3420d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_FEATURES */
3421d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_features_sync),
3422d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_VERSION */
3423d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_version_sync),
3424d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_BD_ADDR */
3425d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_bd_addr_sync),
3426d0b13706SLuiz Augusto von Dentz 	{}
3427d0b13706SLuiz Augusto von Dentz };
3428d0b13706SLuiz Augusto von Dentz 
3429d0b13706SLuiz Augusto von Dentz /* Read Local Commands */
3430d0b13706SLuiz Augusto von Dentz static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3431d0b13706SLuiz Augusto von Dentz {
3432d0b13706SLuiz Augusto von Dentz 	/* All Bluetooth 1.2 and later controllers should support the
3433d0b13706SLuiz Augusto von Dentz 	 * HCI command for reading the local supported commands.
3434d0b13706SLuiz Augusto von Dentz 	 *
3435d0b13706SLuiz Augusto von Dentz 	 * Unfortunately some controllers indicate Bluetooth 1.2 support,
3436d0b13706SLuiz Augusto von Dentz 	 * but do not have support for this command. If that is the case,
3437d0b13706SLuiz Augusto von Dentz 	 * the driver can quirk the behavior and skip reading the local
3438d0b13706SLuiz Augusto von Dentz 	 * supported commands.
3439d0b13706SLuiz Augusto von Dentz 	 */
3440d0b13706SLuiz Augusto von Dentz 	if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3441d0b13706SLuiz Augusto von Dentz 	    !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3442d0b13706SLuiz Augusto von Dentz 		return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3443d0b13706SLuiz Augusto von Dentz 					     0, NULL, HCI_CMD_TIMEOUT);
3444d0b13706SLuiz Augusto von Dentz 
3445d0b13706SLuiz Augusto von Dentz 	return 0;
3446d0b13706SLuiz Augusto von Dentz }
3447d0b13706SLuiz Augusto von Dentz 
3448d0b13706SLuiz Augusto von Dentz /* Read Local AMP Info */
3449d0b13706SLuiz Augusto von Dentz static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
3450d0b13706SLuiz Augusto von Dentz {
3451d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
3452d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3453d0b13706SLuiz Augusto von Dentz }
3454d0b13706SLuiz Augusto von Dentz 
3455d0b13706SLuiz Augusto von Dentz /* Read Data Blk size */
3456d0b13706SLuiz Augusto von Dentz static int hci_read_data_block_size_sync(struct hci_dev *hdev)
3457d0b13706SLuiz Augusto von Dentz {
3458d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
3459d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3460d0b13706SLuiz Augusto von Dentz }
3461d0b13706SLuiz Augusto von Dentz 
3462d0b13706SLuiz Augusto von Dentz /* Read Flow Control Mode */
3463d0b13706SLuiz Augusto von Dentz static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
3464d0b13706SLuiz Augusto von Dentz {
3465d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
3466d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3467d0b13706SLuiz Augusto von Dentz }
3468d0b13706SLuiz Augusto von Dentz 
3469d0b13706SLuiz Augusto von Dentz /* Read Location Data */
3470d0b13706SLuiz Augusto von Dentz static int hci_read_location_data_sync(struct hci_dev *hdev)
3471d0b13706SLuiz Augusto von Dentz {
3472d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
3473d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3474d0b13706SLuiz Augusto von Dentz }
3475d0b13706SLuiz Augusto von Dentz 
3476d0b13706SLuiz Augusto von Dentz /* AMP Controller init stage 1 command sequence */
3477d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage amp_init1[] = {
3478d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_VERSION */
3479d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_version_sync),
3480d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_COMMANDS */
3481d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_cmds_sync),
3482d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_AMP_INFO */
3483d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_amp_info_sync),
3484d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_DATA_BLOCK_SIZE */
3485d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_data_block_size_sync),
3486d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_FLOW_CONTROL_MODE */
3487d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_flow_control_mode_sync),
3488d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCATION_DATA */
3489d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_location_data_sync),
3490bce56405SSungwoo Kim 	{}
3491d0b13706SLuiz Augusto von Dentz };
3492d0b13706SLuiz Augusto von Dentz 
3493d0b13706SLuiz Augusto von Dentz static int hci_init1_sync(struct hci_dev *hdev)
3494d0b13706SLuiz Augusto von Dentz {
3495d0b13706SLuiz Augusto von Dentz 	int err;
3496d0b13706SLuiz Augusto von Dentz 
3497d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
3498d0b13706SLuiz Augusto von Dentz 
3499d0b13706SLuiz Augusto von Dentz 	/* Reset */
3500d0b13706SLuiz Augusto von Dentz 	if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3501d0b13706SLuiz Augusto von Dentz 		err = hci_reset_sync(hdev);
3502d0b13706SLuiz Augusto von Dentz 		if (err)
3503d0b13706SLuiz Augusto von Dentz 			return err;
3504d0b13706SLuiz Augusto von Dentz 	}
3505d0b13706SLuiz Augusto von Dentz 
3506d0b13706SLuiz Augusto von Dentz 	switch (hdev->dev_type) {
3507d0b13706SLuiz Augusto von Dentz 	case HCI_PRIMARY:
3508d0b13706SLuiz Augusto von Dentz 		hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3509d0b13706SLuiz Augusto von Dentz 		return hci_init_stage_sync(hdev, br_init1);
3510d0b13706SLuiz Augusto von Dentz 	case HCI_AMP:
3511d0b13706SLuiz Augusto von Dentz 		hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3512d0b13706SLuiz Augusto von Dentz 		return hci_init_stage_sync(hdev, amp_init1);
3513d0b13706SLuiz Augusto von Dentz 	default:
3514d0b13706SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
3515d0b13706SLuiz Augusto von Dentz 		break;
3516d0b13706SLuiz Augusto von Dentz 	}
3517d0b13706SLuiz Augusto von Dentz 
3518d0b13706SLuiz Augusto von Dentz 	return 0;
3519d0b13706SLuiz Augusto von Dentz }
3520d0b13706SLuiz Augusto von Dentz 
3521d0b13706SLuiz Augusto von Dentz /* AMP Controller init stage 2 command sequence */
3522d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage amp_init2[] = {
3523d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_FEATURES */
3524d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_features_sync),
3525bce56405SSungwoo Kim 	{}
3526d0b13706SLuiz Augusto von Dentz };
3527d0b13706SLuiz Augusto von Dentz 
3528d0b13706SLuiz Augusto von Dentz /* Read Buffer Size (ACL mtu, max pkt, etc.) */
3529d0b13706SLuiz Augusto von Dentz static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3530d0b13706SLuiz Augusto von Dentz {
3531d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3532d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3533d0b13706SLuiz Augusto von Dentz }
3534d0b13706SLuiz Augusto von Dentz 
3535d0b13706SLuiz Augusto von Dentz /* Read Class of Device */
3536d0b13706SLuiz Augusto von Dentz static int hci_read_dev_class_sync(struct hci_dev *hdev)
3537d0b13706SLuiz Augusto von Dentz {
3538d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3539d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3540d0b13706SLuiz Augusto von Dentz }
3541d0b13706SLuiz Augusto von Dentz 
3542d0b13706SLuiz Augusto von Dentz /* Read Local Name */
3543d0b13706SLuiz Augusto von Dentz static int hci_read_local_name_sync(struct hci_dev *hdev)
3544d0b13706SLuiz Augusto von Dentz {
3545d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3546d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3547d0b13706SLuiz Augusto von Dentz }
3548d0b13706SLuiz Augusto von Dentz 
3549d0b13706SLuiz Augusto von Dentz /* Read Voice Setting */
3550d0b13706SLuiz Augusto von Dentz static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3551d0b13706SLuiz Augusto von Dentz {
3552d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3553d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3554d0b13706SLuiz Augusto von Dentz }
3555d0b13706SLuiz Augusto von Dentz 
3556d0b13706SLuiz Augusto von Dentz /* Read Number of Supported IAC */
3557d0b13706SLuiz Augusto von Dentz static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3558d0b13706SLuiz Augusto von Dentz {
3559d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3560d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3561d0b13706SLuiz Augusto von Dentz }
3562d0b13706SLuiz Augusto von Dentz 
3563d0b13706SLuiz Augusto von Dentz /* Read Current IAC LAP */
3564d0b13706SLuiz Augusto von Dentz static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3565d0b13706SLuiz Augusto von Dentz {
3566d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3567d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3568d0b13706SLuiz Augusto von Dentz }
3569d0b13706SLuiz Augusto von Dentz 
3570d0b13706SLuiz Augusto von Dentz static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3571d0b13706SLuiz Augusto von Dentz 				     u8 cond_type, bdaddr_t *bdaddr,
3572d0b13706SLuiz Augusto von Dentz 				     u8 auto_accept)
3573d0b13706SLuiz Augusto von Dentz {
3574d0b13706SLuiz Augusto von Dentz 	struct hci_cp_set_event_filter cp;
3575d0b13706SLuiz Augusto von Dentz 
3576d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3577d0b13706SLuiz Augusto von Dentz 		return 0;
3578d0b13706SLuiz Augusto von Dentz 
35790eaecfb2SIsmael Ferreras Morezuelas 	if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
35800eaecfb2SIsmael Ferreras Morezuelas 		return 0;
35810eaecfb2SIsmael Ferreras Morezuelas 
3582d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3583d0b13706SLuiz Augusto von Dentz 	cp.flt_type = flt_type;
3584d0b13706SLuiz Augusto von Dentz 
3585d0b13706SLuiz Augusto von Dentz 	if (flt_type != HCI_FLT_CLEAR_ALL) {
3586d0b13706SLuiz Augusto von Dentz 		cp.cond_type = cond_type;
3587d0b13706SLuiz Augusto von Dentz 		bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3588d0b13706SLuiz Augusto von Dentz 		cp.addr_conn_flt.auto_accept = auto_accept;
3589d0b13706SLuiz Augusto von Dentz 	}
3590d0b13706SLuiz Augusto von Dentz 
3591d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3592d0b13706SLuiz Augusto von Dentz 				     flt_type == HCI_FLT_CLEAR_ALL ?
3593d0b13706SLuiz Augusto von Dentz 				     sizeof(cp.flt_type) : sizeof(cp), &cp,
3594d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
3595d0b13706SLuiz Augusto von Dentz }
3596d0b13706SLuiz Augusto von Dentz 
3597d0b13706SLuiz Augusto von Dentz static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3598d0b13706SLuiz Augusto von Dentz {
3599d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3600d0b13706SLuiz Augusto von Dentz 		return 0;
3601d0b13706SLuiz Augusto von Dentz 
36020eaecfb2SIsmael Ferreras Morezuelas 	/* In theory the state machine should not reach here unless
36030eaecfb2SIsmael Ferreras Morezuelas 	 * a hci_set_event_filter_sync() call succeeds, but we do
36040eaecfb2SIsmael Ferreras Morezuelas 	 * the check both for parity and as a future reminder.
36050eaecfb2SIsmael Ferreras Morezuelas 	 */
36060eaecfb2SIsmael Ferreras Morezuelas 	if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
36070eaecfb2SIsmael Ferreras Morezuelas 		return 0;
36080eaecfb2SIsmael Ferreras Morezuelas 
3609d0b13706SLuiz Augusto von Dentz 	return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3610d0b13706SLuiz Augusto von Dentz 					 BDADDR_ANY, 0x00);
3611d0b13706SLuiz Augusto von Dentz }
3612d0b13706SLuiz Augusto von Dentz 
3613d0b13706SLuiz Augusto von Dentz /* Connection accept timeout ~20 secs */
3614d0b13706SLuiz Augusto von Dentz static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3615d0b13706SLuiz Augusto von Dentz {
3616d0b13706SLuiz Augusto von Dentz 	__le16 param = cpu_to_le16(0x7d00);
3617d0b13706SLuiz Augusto von Dentz 
3618d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3619d0b13706SLuiz Augusto von Dentz 				     sizeof(param), &param, HCI_CMD_TIMEOUT);
3620d0b13706SLuiz Augusto von Dentz }
3621d0b13706SLuiz Augusto von Dentz 
3622d0b13706SLuiz Augusto von Dentz /* BR Controller init stage 2 command sequence */
3623d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage br_init2[] = {
3624d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_BUFFER_SIZE */
3625d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_buffer_size_sync),
3626d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_CLASS_OF_DEV */
3627d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_dev_class_sync),
3628d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_NAME */
3629d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_name_sync),
3630d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_VOICE_SETTING */
3631d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_voice_setting_sync),
3632d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_NUM_SUPPORTED_IAC */
3633d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_num_supported_iac_sync),
3634d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_CURRENT_IAC_LAP */
3635d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_current_iac_lap_sync),
3636d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_SET_EVENT_FLT */
3637d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_clear_event_filter_sync),
3638d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_CA_TIMEOUT */
3639d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_ca_timeout_sync),
3640d0b13706SLuiz Augusto von Dentz 	{}
3641d0b13706SLuiz Augusto von Dentz };
3642d0b13706SLuiz Augusto von Dentz 
3643d0b13706SLuiz Augusto von Dentz static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3644d0b13706SLuiz Augusto von Dentz {
3645d0b13706SLuiz Augusto von Dentz 	u8 mode = 0x01;
3646d0b13706SLuiz Augusto von Dentz 
3647d0b13706SLuiz Augusto von Dentz 	if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3648d0b13706SLuiz Augusto von Dentz 		return 0;
3649d0b13706SLuiz Augusto von Dentz 
3650d0b13706SLuiz Augusto von Dentz 	/* When SSP is available, then the host features page
3651d0b13706SLuiz Augusto von Dentz 	 * should also be available as well. However some
3652d0b13706SLuiz Augusto von Dentz 	 * controllers list the max_page as 0 as long as SSP
3653d0b13706SLuiz Augusto von Dentz 	 * has not been enabled. To achieve proper debugging
3654d0b13706SLuiz Augusto von Dentz 	 * output, force the minimum max_page to 1 at least.
3655d0b13706SLuiz Augusto von Dentz 	 */
3656d0b13706SLuiz Augusto von Dentz 	hdev->max_page = 0x01;
3657d0b13706SLuiz Augusto von Dentz 
3658d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3659d0b13706SLuiz Augusto von Dentz 				     sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3660d0b13706SLuiz Augusto von Dentz }
3661d0b13706SLuiz Augusto von Dentz 
3662d0b13706SLuiz Augusto von Dentz static int hci_write_eir_sync(struct hci_dev *hdev)
3663d0b13706SLuiz Augusto von Dentz {
3664d0b13706SLuiz Augusto von Dentz 	struct hci_cp_write_eir cp;
3665d0b13706SLuiz Augusto von Dentz 
3666d0b13706SLuiz Augusto von Dentz 	if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3667d0b13706SLuiz Augusto von Dentz 		return 0;
3668d0b13706SLuiz Augusto von Dentz 
3669d0b13706SLuiz Augusto von Dentz 	memset(hdev->eir, 0, sizeof(hdev->eir));
3670d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3671d0b13706SLuiz Augusto von Dentz 
3672d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3673d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
3674d0b13706SLuiz Augusto von Dentz }
3675d0b13706SLuiz Augusto von Dentz 
3676d0b13706SLuiz Augusto von Dentz static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3677d0b13706SLuiz Augusto von Dentz {
3678d0b13706SLuiz Augusto von Dentz 	u8 mode;
3679d0b13706SLuiz Augusto von Dentz 
3680d0b13706SLuiz Augusto von Dentz 	if (!lmp_inq_rssi_capable(hdev) &&
3681d0b13706SLuiz Augusto von Dentz 	    !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3682d0b13706SLuiz Augusto von Dentz 		return 0;
3683d0b13706SLuiz Augusto von Dentz 
3684d0b13706SLuiz Augusto von Dentz 	/* If Extended Inquiry Result events are supported, then
3685d0b13706SLuiz Augusto von Dentz 	 * they are clearly preferred over Inquiry Result with RSSI
3686d0b13706SLuiz Augusto von Dentz 	 * events.
3687d0b13706SLuiz Augusto von Dentz 	 */
3688d0b13706SLuiz Augusto von Dentz 	mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3689d0b13706SLuiz Augusto von Dentz 
3690d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3691d0b13706SLuiz Augusto von Dentz 				     sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3692d0b13706SLuiz Augusto von Dentz }
3693d0b13706SLuiz Augusto von Dentz 
3694d0b13706SLuiz Augusto von Dentz static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3695d0b13706SLuiz Augusto von Dentz {
3696d0b13706SLuiz Augusto von Dentz 	if (!lmp_inq_tx_pwr_capable(hdev))
3697d0b13706SLuiz Augusto von Dentz 		return 0;
3698d0b13706SLuiz Augusto von Dentz 
3699d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3700d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3701d0b13706SLuiz Augusto von Dentz }
3702d0b13706SLuiz Augusto von Dentz 
3703d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3704d0b13706SLuiz Augusto von Dentz {
3705d0b13706SLuiz Augusto von Dentz 	struct hci_cp_read_local_ext_features cp;
3706d0b13706SLuiz Augusto von Dentz 
3707d0b13706SLuiz Augusto von Dentz 	if (!lmp_ext_feat_capable(hdev))
3708d0b13706SLuiz Augusto von Dentz 		return 0;
3709d0b13706SLuiz Augusto von Dentz 
3710d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3711d0b13706SLuiz Augusto von Dentz 	cp.page = page;
3712d0b13706SLuiz Augusto von Dentz 
3713d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3714d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3715d0b13706SLuiz Augusto von Dentz }
3716d0b13706SLuiz Augusto von Dentz 
3717d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3718d0b13706SLuiz Augusto von Dentz {
3719d0b13706SLuiz Augusto von Dentz 	return hci_read_local_ext_features_sync(hdev, 0x01);
3720d0b13706SLuiz Augusto von Dentz }
3721d0b13706SLuiz Augusto von Dentz 
3722d0b13706SLuiz Augusto von Dentz /* HCI Controller init stage 2 command sequence */
3723d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init2[] = {
3724d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_COMMANDS */
3725d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_cmds_sync),
3726d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_SSP_MODE */
3727d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_ssp_mode_1_sync),
3728d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_EIR */
3729d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_eir_sync),
3730d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_INQUIRY_MODE */
3731d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_inquiry_mode_sync),
3732d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_INQ_RSP_TX_POWER */
3733d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3734d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_EXT_FEATURES */
3735d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_ext_features_1_sync),
3736d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_AUTH_ENABLE */
3737d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_auth_enable_sync),
3738d0b13706SLuiz Augusto von Dentz 	{}
3739d0b13706SLuiz Augusto von Dentz };
3740d0b13706SLuiz Augusto von Dentz 
3741d0b13706SLuiz Augusto von Dentz /* Read LE Buffer Size */
3742d0b13706SLuiz Augusto von Dentz static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3743d0b13706SLuiz Augusto von Dentz {
3744c1631dbcSLuiz Augusto von Dentz 	/* Use Read LE Buffer Size V2 if supported */
37453a4d29b6SLuiz Augusto von Dentz 	if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3746c1631dbcSLuiz Augusto von Dentz 		return __hci_cmd_sync_status(hdev,
3747c1631dbcSLuiz Augusto von Dentz 					     HCI_OP_LE_READ_BUFFER_SIZE_V2,
3748c1631dbcSLuiz Augusto von Dentz 					     0, NULL, HCI_CMD_TIMEOUT);
3749c1631dbcSLuiz Augusto von Dentz 
3750d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3751d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3752d0b13706SLuiz Augusto von Dentz }
3753d0b13706SLuiz Augusto von Dentz 
3754d0b13706SLuiz Augusto von Dentz /* Read LE Local Supported Features */
3755d0b13706SLuiz Augusto von Dentz static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3756d0b13706SLuiz Augusto von Dentz {
3757d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3758d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3759d0b13706SLuiz Augusto von Dentz }
3760d0b13706SLuiz Augusto von Dentz 
3761d0b13706SLuiz Augusto von Dentz /* Read LE Supported States */
3762d0b13706SLuiz Augusto von Dentz static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3763d0b13706SLuiz Augusto von Dentz {
3764d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3765d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3766d0b13706SLuiz Augusto von Dentz }
3767d0b13706SLuiz Augusto von Dentz 
3768d0b13706SLuiz Augusto von Dentz /* LE Controller init stage 2 command sequence */
3769d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init2[] = {
3770d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_LOCAL_FEATURES */
3771d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_local_features_sync),
37723a4d29b6SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_BUFFER_SIZE */
37733a4d29b6SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_buffer_size_sync),
3774d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_SUPPORTED_STATES */
3775d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_supported_states_sync),
3776d0b13706SLuiz Augusto von Dentz 	{}
3777d0b13706SLuiz Augusto von Dentz };
3778d0b13706SLuiz Augusto von Dentz 
3779d0b13706SLuiz Augusto von Dentz static int hci_init2_sync(struct hci_dev *hdev)
3780d0b13706SLuiz Augusto von Dentz {
3781d0b13706SLuiz Augusto von Dentz 	int err;
3782d0b13706SLuiz Augusto von Dentz 
3783d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
3784d0b13706SLuiz Augusto von Dentz 
3785d0b13706SLuiz Augusto von Dentz 	if (hdev->dev_type == HCI_AMP)
3786d0b13706SLuiz Augusto von Dentz 		return hci_init_stage_sync(hdev, amp_init2);
3787d0b13706SLuiz Augusto von Dentz 
378826afbd82SLuiz Augusto von Dentz 	err = hci_init_stage_sync(hdev, hci_init2);
378926afbd82SLuiz Augusto von Dentz 	if (err)
379026afbd82SLuiz Augusto von Dentz 		return err;
379126afbd82SLuiz Augusto von Dentz 
3792d0b13706SLuiz Augusto von Dentz 	if (lmp_bredr_capable(hdev)) {
3793d0b13706SLuiz Augusto von Dentz 		err = hci_init_stage_sync(hdev, br_init2);
3794d0b13706SLuiz Augusto von Dentz 		if (err)
3795d0b13706SLuiz Augusto von Dentz 			return err;
3796d0b13706SLuiz Augusto von Dentz 	} else {
3797d0b13706SLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3798d0b13706SLuiz Augusto von Dentz 	}
3799d0b13706SLuiz Augusto von Dentz 
3800d0b13706SLuiz Augusto von Dentz 	if (lmp_le_capable(hdev)) {
3801d0b13706SLuiz Augusto von Dentz 		err = hci_init_stage_sync(hdev, le_init2);
3802d0b13706SLuiz Augusto von Dentz 		if (err)
3803d0b13706SLuiz Augusto von Dentz 			return err;
3804d0b13706SLuiz Augusto von Dentz 		/* LE-only controllers have LE implicitly enabled */
3805d0b13706SLuiz Augusto von Dentz 		if (!lmp_bredr_capable(hdev))
3806d0b13706SLuiz Augusto von Dentz 			hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3807d0b13706SLuiz Augusto von Dentz 	}
3808d0b13706SLuiz Augusto von Dentz 
380926afbd82SLuiz Augusto von Dentz 	return 0;
3810d0b13706SLuiz Augusto von Dentz }
3811d0b13706SLuiz Augusto von Dentz 
3812d0b13706SLuiz Augusto von Dentz static int hci_set_event_mask_sync(struct hci_dev *hdev)
3813d0b13706SLuiz Augusto von Dentz {
3814d0b13706SLuiz Augusto von Dentz 	/* The second byte is 0xff instead of 0x9f (two reserved bits
3815d0b13706SLuiz Augusto von Dentz 	 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3816d0b13706SLuiz Augusto von Dentz 	 * command otherwise.
3817d0b13706SLuiz Augusto von Dentz 	 */
3818d0b13706SLuiz Augusto von Dentz 	u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3819d0b13706SLuiz Augusto von Dentz 
3820d0b13706SLuiz Augusto von Dentz 	/* CSR 1.1 dongles does not accept any bitfield so don't try to set
3821d0b13706SLuiz Augusto von Dentz 	 * any event mask for pre 1.2 devices.
3822d0b13706SLuiz Augusto von Dentz 	 */
3823d0b13706SLuiz Augusto von Dentz 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3824d0b13706SLuiz Augusto von Dentz 		return 0;
3825d0b13706SLuiz Augusto von Dentz 
3826d0b13706SLuiz Augusto von Dentz 	if (lmp_bredr_capable(hdev)) {
3827d0b13706SLuiz Augusto von Dentz 		events[4] |= 0x01; /* Flow Specification Complete */
3828182ee45dSLuiz Augusto von Dentz 
3829182ee45dSLuiz Augusto von Dentz 		/* Don't set Disconnect Complete when suspended as that
3830182ee45dSLuiz Augusto von Dentz 		 * would wakeup the host when disconnecting due to
3831182ee45dSLuiz Augusto von Dentz 		 * suspend.
3832182ee45dSLuiz Augusto von Dentz 		 */
3833182ee45dSLuiz Augusto von Dentz 		if (hdev->suspended)
3834182ee45dSLuiz Augusto von Dentz 			events[0] &= 0xef;
3835d0b13706SLuiz Augusto von Dentz 	} else {
3836d0b13706SLuiz Augusto von Dentz 		/* Use a different default for LE-only devices */
3837d0b13706SLuiz Augusto von Dentz 		memset(events, 0, sizeof(events));
3838d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x20; /* Command Complete */
3839d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x40; /* Command Status */
3840d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x80; /* Hardware Error */
3841d0b13706SLuiz Augusto von Dentz 
3842d0b13706SLuiz Augusto von Dentz 		/* If the controller supports the Disconnect command, enable
3843d0b13706SLuiz Augusto von Dentz 		 * the corresponding event. In addition enable packet flow
3844d0b13706SLuiz Augusto von Dentz 		 * control related events.
3845d0b13706SLuiz Augusto von Dentz 		 */
3846d0b13706SLuiz Augusto von Dentz 		if (hdev->commands[0] & 0x20) {
3847182ee45dSLuiz Augusto von Dentz 			/* Don't set Disconnect Complete when suspended as that
3848182ee45dSLuiz Augusto von Dentz 			 * would wakeup the host when disconnecting due to
3849182ee45dSLuiz Augusto von Dentz 			 * suspend.
3850182ee45dSLuiz Augusto von Dentz 			 */
3851182ee45dSLuiz Augusto von Dentz 			if (!hdev->suspended)
3852d0b13706SLuiz Augusto von Dentz 				events[0] |= 0x10; /* Disconnection Complete */
3853d0b13706SLuiz Augusto von Dentz 			events[2] |= 0x04; /* Number of Completed Packets */
3854d0b13706SLuiz Augusto von Dentz 			events[3] |= 0x02; /* Data Buffer Overflow */
3855d0b13706SLuiz Augusto von Dentz 		}
3856d0b13706SLuiz Augusto von Dentz 
3857d0b13706SLuiz Augusto von Dentz 		/* If the controller supports the Read Remote Version
3858d0b13706SLuiz Augusto von Dentz 		 * Information command, enable the corresponding event.
3859d0b13706SLuiz Augusto von Dentz 		 */
3860d0b13706SLuiz Augusto von Dentz 		if (hdev->commands[2] & 0x80)
3861d0b13706SLuiz Augusto von Dentz 			events[1] |= 0x08; /* Read Remote Version Information
3862d0b13706SLuiz Augusto von Dentz 					    * Complete
3863d0b13706SLuiz Augusto von Dentz 					    */
3864d0b13706SLuiz Augusto von Dentz 
3865d0b13706SLuiz Augusto von Dentz 		if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3866d0b13706SLuiz Augusto von Dentz 			events[0] |= 0x80; /* Encryption Change */
3867d0b13706SLuiz Augusto von Dentz 			events[5] |= 0x80; /* Encryption Key Refresh Complete */
3868d0b13706SLuiz Augusto von Dentz 		}
3869d0b13706SLuiz Augusto von Dentz 	}
3870d0b13706SLuiz Augusto von Dentz 
3871d0b13706SLuiz Augusto von Dentz 	if (lmp_inq_rssi_capable(hdev) ||
3872d0b13706SLuiz Augusto von Dentz 	    test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3873d0b13706SLuiz Augusto von Dentz 		events[4] |= 0x02; /* Inquiry Result with RSSI */
3874d0b13706SLuiz Augusto von Dentz 
3875d0b13706SLuiz Augusto von Dentz 	if (lmp_ext_feat_capable(hdev))
3876d0b13706SLuiz Augusto von Dentz 		events[4] |= 0x04; /* Read Remote Extended Features Complete */
3877d0b13706SLuiz Augusto von Dentz 
3878d0b13706SLuiz Augusto von Dentz 	if (lmp_esco_capable(hdev)) {
3879d0b13706SLuiz Augusto von Dentz 		events[5] |= 0x08; /* Synchronous Connection Complete */
3880d0b13706SLuiz Augusto von Dentz 		events[5] |= 0x10; /* Synchronous Connection Changed */
3881d0b13706SLuiz Augusto von Dentz 	}
3882d0b13706SLuiz Augusto von Dentz 
3883d0b13706SLuiz Augusto von Dentz 	if (lmp_sniffsubr_capable(hdev))
3884d0b13706SLuiz Augusto von Dentz 		events[5] |= 0x20; /* Sniff Subrating */
3885d0b13706SLuiz Augusto von Dentz 
3886d0b13706SLuiz Augusto von Dentz 	if (lmp_pause_enc_capable(hdev))
3887d0b13706SLuiz Augusto von Dentz 		events[5] |= 0x80; /* Encryption Key Refresh Complete */
3888d0b13706SLuiz Augusto von Dentz 
3889d0b13706SLuiz Augusto von Dentz 	if (lmp_ext_inq_capable(hdev))
3890d0b13706SLuiz Augusto von Dentz 		events[5] |= 0x40; /* Extended Inquiry Result */
3891d0b13706SLuiz Augusto von Dentz 
3892d0b13706SLuiz Augusto von Dentz 	if (lmp_no_flush_capable(hdev))
3893d0b13706SLuiz Augusto von Dentz 		events[7] |= 0x01; /* Enhanced Flush Complete */
3894d0b13706SLuiz Augusto von Dentz 
3895d0b13706SLuiz Augusto von Dentz 	if (lmp_lsto_capable(hdev))
3896d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x80; /* Link Supervision Timeout Changed */
3897d0b13706SLuiz Augusto von Dentz 
3898d0b13706SLuiz Augusto von Dentz 	if (lmp_ssp_capable(hdev)) {
3899d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x01;	/* IO Capability Request */
3900d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x02;	/* IO Capability Response */
3901d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x04;	/* User Confirmation Request */
3902d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x08;	/* User Passkey Request */
3903d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x10;	/* Remote OOB Data Request */
3904d0b13706SLuiz Augusto von Dentz 		events[6] |= 0x20;	/* Simple Pairing Complete */
3905d0b13706SLuiz Augusto von Dentz 		events[7] |= 0x04;	/* User Passkey Notification */
3906d0b13706SLuiz Augusto von Dentz 		events[7] |= 0x08;	/* Keypress Notification */
3907d0b13706SLuiz Augusto von Dentz 		events[7] |= 0x10;	/* Remote Host Supported
3908d0b13706SLuiz Augusto von Dentz 					 * Features Notification
3909d0b13706SLuiz Augusto von Dentz 					 */
3910d0b13706SLuiz Augusto von Dentz 	}
3911d0b13706SLuiz Augusto von Dentz 
3912d0b13706SLuiz Augusto von Dentz 	if (lmp_le_capable(hdev))
3913d0b13706SLuiz Augusto von Dentz 		events[7] |= 0x20;	/* LE Meta-Event */
3914d0b13706SLuiz Augusto von Dentz 
3915d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3916d0b13706SLuiz Augusto von Dentz 				     sizeof(events), events, HCI_CMD_TIMEOUT);
3917d0b13706SLuiz Augusto von Dentz }
3918d0b13706SLuiz Augusto von Dentz 
3919d0b13706SLuiz Augusto von Dentz static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3920d0b13706SLuiz Augusto von Dentz {
3921d0b13706SLuiz Augusto von Dentz 	struct hci_cp_read_stored_link_key cp;
3922d0b13706SLuiz Augusto von Dentz 
3923d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[6] & 0x20) ||
3924d0b13706SLuiz Augusto von Dentz 	    test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3925d0b13706SLuiz Augusto von Dentz 		return 0;
3926d0b13706SLuiz Augusto von Dentz 
3927d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3928d0b13706SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, BDADDR_ANY);
3929d0b13706SLuiz Augusto von Dentz 	cp.read_all = 0x01;
3930d0b13706SLuiz Augusto von Dentz 
3931d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3932d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3933d0b13706SLuiz Augusto von Dentz }
3934d0b13706SLuiz Augusto von Dentz 
3935d0b13706SLuiz Augusto von Dentz static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3936d0b13706SLuiz Augusto von Dentz {
3937d0b13706SLuiz Augusto von Dentz 	struct hci_cp_write_def_link_policy cp;
3938d0b13706SLuiz Augusto von Dentz 	u16 link_policy = 0;
3939d0b13706SLuiz Augusto von Dentz 
3940d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[5] & 0x10))
3941d0b13706SLuiz Augusto von Dentz 		return 0;
3942d0b13706SLuiz Augusto von Dentz 
3943d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
3944d0b13706SLuiz Augusto von Dentz 
3945d0b13706SLuiz Augusto von Dentz 	if (lmp_rswitch_capable(hdev))
3946d0b13706SLuiz Augusto von Dentz 		link_policy |= HCI_LP_RSWITCH;
3947d0b13706SLuiz Augusto von Dentz 	if (lmp_hold_capable(hdev))
3948d0b13706SLuiz Augusto von Dentz 		link_policy |= HCI_LP_HOLD;
3949d0b13706SLuiz Augusto von Dentz 	if (lmp_sniff_capable(hdev))
3950d0b13706SLuiz Augusto von Dentz 		link_policy |= HCI_LP_SNIFF;
3951d0b13706SLuiz Augusto von Dentz 	if (lmp_park_capable(hdev))
3952d0b13706SLuiz Augusto von Dentz 		link_policy |= HCI_LP_PARK;
3953d0b13706SLuiz Augusto von Dentz 
3954d0b13706SLuiz Augusto von Dentz 	cp.policy = cpu_to_le16(link_policy);
3955d0b13706SLuiz Augusto von Dentz 
3956d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3957d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3958d0b13706SLuiz Augusto von Dentz }
3959d0b13706SLuiz Augusto von Dentz 
3960d0b13706SLuiz Augusto von Dentz static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3961d0b13706SLuiz Augusto von Dentz {
3962d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[8] & 0x01))
3963d0b13706SLuiz Augusto von Dentz 		return 0;
3964d0b13706SLuiz Augusto von Dentz 
3965d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3966d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3967d0b13706SLuiz Augusto von Dentz }
3968d0b13706SLuiz Augusto von Dentz 
3969d0b13706SLuiz Augusto von Dentz static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3970d0b13706SLuiz Augusto von Dentz {
3971d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[18] & 0x04) ||
397242d7731eSIsmael Ferreras Morezuelas 	    !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
397342d7731eSIsmael Ferreras Morezuelas 	    test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3974d0b13706SLuiz Augusto von Dentz 		return 0;
3975d0b13706SLuiz Augusto von Dentz 
3976d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3977d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3978d0b13706SLuiz Augusto von Dentz }
3979d0b13706SLuiz Augusto von Dentz 
3980d0b13706SLuiz Augusto von Dentz static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3981d0b13706SLuiz Augusto von Dentz {
3982d0b13706SLuiz Augusto von Dentz 	/* Some older Broadcom based Bluetooth 1.2 controllers do not
3983d0b13706SLuiz Augusto von Dentz 	 * support the Read Page Scan Type command. Check support for
3984d0b13706SLuiz Augusto von Dentz 	 * this command in the bit mask of supported commands.
3985d0b13706SLuiz Augusto von Dentz 	 */
3986d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[13] & 0x01))
3987d0b13706SLuiz Augusto von Dentz 		return 0;
3988d0b13706SLuiz Augusto von Dentz 
3989d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3990d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
3991d0b13706SLuiz Augusto von Dentz }
3992d0b13706SLuiz Augusto von Dentz 
3993d0b13706SLuiz Augusto von Dentz /* Read features beyond page 1 if available */
3994d0b13706SLuiz Augusto von Dentz static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3995d0b13706SLuiz Augusto von Dentz {
3996d0b13706SLuiz Augusto von Dentz 	u8 page;
3997d0b13706SLuiz Augusto von Dentz 	int err;
3998d0b13706SLuiz Augusto von Dentz 
3999d0b13706SLuiz Augusto von Dentz 	if (!lmp_ext_feat_capable(hdev))
4000d0b13706SLuiz Augusto von Dentz 		return 0;
4001d0b13706SLuiz Augusto von Dentz 
4002d0b13706SLuiz Augusto von Dentz 	for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
4003d0b13706SLuiz Augusto von Dentz 	     page++) {
4004d0b13706SLuiz Augusto von Dentz 		err = hci_read_local_ext_features_sync(hdev, page);
4005d0b13706SLuiz Augusto von Dentz 		if (err)
4006d0b13706SLuiz Augusto von Dentz 			return err;
4007d0b13706SLuiz Augusto von Dentz 	}
4008d0b13706SLuiz Augusto von Dentz 
4009d0b13706SLuiz Augusto von Dentz 	return 0;
4010d0b13706SLuiz Augusto von Dentz }
4011d0b13706SLuiz Augusto von Dentz 
4012d0b13706SLuiz Augusto von Dentz /* HCI Controller init stage 3 command sequence */
4013d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init3[] = {
4014d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_SET_EVENT_MASK */
4015d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_set_event_mask_sync),
4016d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_STORED_LINK_KEY */
4017d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_stored_link_key_sync),
4018d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_DEF_LINK_POLICY */
4019d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_setup_link_policy_sync),
4020d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
4021d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_page_scan_activity_sync),
4022d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
4023d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_def_err_data_reporting_sync),
4024d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_PAGE_SCAN_TYPE */
4025d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_page_scan_type_sync),
4026d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_EXT_FEATURES */
4027d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_ext_features_all_sync),
4028d0b13706SLuiz Augusto von Dentz 	{}
4029d0b13706SLuiz Augusto von Dentz };
4030d0b13706SLuiz Augusto von Dentz 
4031d0b13706SLuiz Augusto von Dentz static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4032d0b13706SLuiz Augusto von Dentz {
4033d0b13706SLuiz Augusto von Dentz 	u8 events[8];
4034d0b13706SLuiz Augusto von Dentz 
4035d0b13706SLuiz Augusto von Dentz 	if (!lmp_le_capable(hdev))
4036d0b13706SLuiz Augusto von Dentz 		return 0;
4037d0b13706SLuiz Augusto von Dentz 
4038d0b13706SLuiz Augusto von Dentz 	memset(events, 0, sizeof(events));
4039d0b13706SLuiz Augusto von Dentz 
4040d0b13706SLuiz Augusto von Dentz 	if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4041d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x10;	/* LE Long Term Key Request */
4042d0b13706SLuiz Augusto von Dentz 
4043d0b13706SLuiz Augusto von Dentz 	/* If controller supports the Connection Parameters Request
4044d0b13706SLuiz Augusto von Dentz 	 * Link Layer Procedure, enable the corresponding event.
4045d0b13706SLuiz Augusto von Dentz 	 */
4046d0b13706SLuiz Augusto von Dentz 	if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4047d0b13706SLuiz Augusto von Dentz 		/* LE Remote Connection Parameter Request */
4048d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x20;
4049d0b13706SLuiz Augusto von Dentz 
4050d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the Data Length Extension
4051d0b13706SLuiz Augusto von Dentz 	 * feature, enable the corresponding event.
4052d0b13706SLuiz Augusto von Dentz 	 */
4053d0b13706SLuiz Augusto von Dentz 	if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4054d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x40;	/* LE Data Length Change */
4055d0b13706SLuiz Augusto von Dentz 
4056a56a1138SLuiz Augusto von Dentz 	/* If the controller supports LL Privacy feature or LE Extended Adv,
4057a56a1138SLuiz Augusto von Dentz 	 * enable the corresponding event.
4058d0b13706SLuiz Augusto von Dentz 	 */
4059a56a1138SLuiz Augusto von Dentz 	if (use_enhanced_conn_complete(hdev))
4060d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x02;	/* LE Enhanced Connection Complete */
4061d0b13706SLuiz Augusto von Dentz 
4062d0b13706SLuiz Augusto von Dentz 	/* If the controller supports Extended Scanner Filter
4063d0b13706SLuiz Augusto von Dentz 	 * Policies, enable the corresponding event.
4064d0b13706SLuiz Augusto von Dentz 	 */
4065d0b13706SLuiz Augusto von Dentz 	if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4066d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x04;	/* LE Direct Advertising Report */
4067d0b13706SLuiz Augusto von Dentz 
4068d0b13706SLuiz Augusto von Dentz 	/* If the controller supports Channel Selection Algorithm #2
4069d0b13706SLuiz Augusto von Dentz 	 * feature, enable the corresponding event.
4070d0b13706SLuiz Augusto von Dentz 	 */
4071d0b13706SLuiz Augusto von Dentz 	if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4072d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x08;	/* LE Channel Selection Algorithm */
4073d0b13706SLuiz Augusto von Dentz 
4074d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Set Scan Enable command,
4075d0b13706SLuiz Augusto von Dentz 	 * enable the corresponding advertising report event.
4076d0b13706SLuiz Augusto von Dentz 	 */
4077d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[26] & 0x08)
4078d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x02;	/* LE Advertising Report */
4079d0b13706SLuiz Augusto von Dentz 
4080d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Create Connection
4081d0b13706SLuiz Augusto von Dentz 	 * command, enable the corresponding event.
4082d0b13706SLuiz Augusto von Dentz 	 */
4083d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[26] & 0x10)
4084d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x01;	/* LE Connection Complete */
4085d0b13706SLuiz Augusto von Dentz 
4086d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Connection Update
4087d0b13706SLuiz Augusto von Dentz 	 * command, enable the corresponding event.
4088d0b13706SLuiz Augusto von Dentz 	 */
4089d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[27] & 0x04)
4090d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x04;	/* LE Connection Update Complete */
4091d0b13706SLuiz Augusto von Dentz 
4092d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Read Remote Used Features
4093d0b13706SLuiz Augusto von Dentz 	 * command, enable the corresponding event.
4094d0b13706SLuiz Augusto von Dentz 	 */
4095d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[27] & 0x20)
4096d0b13706SLuiz Augusto von Dentz 		/* LE Read Remote Used Features Complete */
4097d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x08;
4098d0b13706SLuiz Augusto von Dentz 
4099d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Read Local P-256
4100d0b13706SLuiz Augusto von Dentz 	 * Public Key command, enable the corresponding event.
4101d0b13706SLuiz Augusto von Dentz 	 */
4102d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[34] & 0x02)
4103d0b13706SLuiz Augusto von Dentz 		/* LE Read Local P-256 Public Key Complete */
4104d0b13706SLuiz Augusto von Dentz 		events[0] |= 0x80;
4105d0b13706SLuiz Augusto von Dentz 
4106d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Generate DHKey
4107d0b13706SLuiz Augusto von Dentz 	 * command, enable the corresponding event.
4108d0b13706SLuiz Augusto von Dentz 	 */
4109d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[34] & 0x04)
4110d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x01;	/* LE Generate DHKey Complete */
4111d0b13706SLuiz Augusto von Dentz 
4112d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Set Default PHY or
4113d0b13706SLuiz Augusto von Dentz 	 * LE Set PHY commands, enable the corresponding event.
4114d0b13706SLuiz Augusto von Dentz 	 */
4115d0b13706SLuiz Augusto von Dentz 	if (hdev->commands[35] & (0x20 | 0x40))
4116d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x08;        /* LE PHY Update Complete */
4117d0b13706SLuiz Augusto von Dentz 
4118d0b13706SLuiz Augusto von Dentz 	/* If the controller supports LE Set Extended Scan Parameters
4119d0b13706SLuiz Augusto von Dentz 	 * and LE Set Extended Scan Enable commands, enable the
4120d0b13706SLuiz Augusto von Dentz 	 * corresponding event.
4121d0b13706SLuiz Augusto von Dentz 	 */
4122d0b13706SLuiz Augusto von Dentz 	if (use_ext_scan(hdev))
4123d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x10;	/* LE Extended Advertising Report */
4124d0b13706SLuiz Augusto von Dentz 
4125d0b13706SLuiz Augusto von Dentz 	/* If the controller supports the LE Extended Advertising
4126d0b13706SLuiz Augusto von Dentz 	 * command, enable the corresponding event.
4127d0b13706SLuiz Augusto von Dentz 	 */
4128d0b13706SLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
4129d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x02;	/* LE Advertising Set Terminated */
4130d0b13706SLuiz Augusto von Dentz 
413126afbd82SLuiz Augusto von Dentz 	if (cis_capable(hdev)) {
413226afbd82SLuiz Augusto von Dentz 		events[3] |= 0x01;	/* LE CIS Established */
413326afbd82SLuiz Augusto von Dentz 		if (cis_peripheral_capable(hdev))
413426afbd82SLuiz Augusto von Dentz 			events[3] |= 0x02; /* LE CIS Request */
413526afbd82SLuiz Augusto von Dentz 	}
413626afbd82SLuiz Augusto von Dentz 
4137eca0ae4aSLuiz Augusto von Dentz 	if (bis_capable(hdev)) {
4138c33362a5SClaudia Draghicescu 		events[1] |= 0x20;	/* LE PA Report */
4139c33362a5SClaudia Draghicescu 		events[1] |= 0x40;	/* LE PA Sync Established */
4140eca0ae4aSLuiz Augusto von Dentz 		events[3] |= 0x04;	/* LE Create BIG Complete */
4141eca0ae4aSLuiz Augusto von Dentz 		events[3] |= 0x08;	/* LE Terminate BIG Complete */
4142eca0ae4aSLuiz Augusto von Dentz 		events[3] |= 0x10;	/* LE BIG Sync Established */
4143eca0ae4aSLuiz Augusto von Dentz 		events[3] |= 0x20;	/* LE BIG Sync Loss */
4144c33362a5SClaudia Draghicescu 		events[4] |= 0x02;	/* LE BIG Info Advertising Report */
4145eca0ae4aSLuiz Augusto von Dentz 	}
4146eca0ae4aSLuiz Augusto von Dentz 
4147d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4148d0b13706SLuiz Augusto von Dentz 				     sizeof(events), events, HCI_CMD_TIMEOUT);
4149d0b13706SLuiz Augusto von Dentz }
4150d0b13706SLuiz Augusto von Dentz 
4151d0b13706SLuiz Augusto von Dentz /* Read LE Advertising Channel TX Power */
4152d0b13706SLuiz Augusto von Dentz static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4153d0b13706SLuiz Augusto von Dentz {
4154d0b13706SLuiz Augusto von Dentz 	if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4155d0b13706SLuiz Augusto von Dentz 		/* HCI TS spec forbids mixing of legacy and extended
4156d0b13706SLuiz Augusto von Dentz 		 * advertising commands wherein READ_ADV_TX_POWER is
4157d0b13706SLuiz Augusto von Dentz 		 * also included. So do not call it if extended adv
4158d0b13706SLuiz Augusto von Dentz 		 * is supported otherwise controller will return
4159d0b13706SLuiz Augusto von Dentz 		 * COMMAND_DISALLOWED for extended commands.
4160d0b13706SLuiz Augusto von Dentz 		 */
4161d0b13706SLuiz Augusto von Dentz 		return __hci_cmd_sync_status(hdev,
4162d0b13706SLuiz Augusto von Dentz 					       HCI_OP_LE_READ_ADV_TX_POWER,
4163d0b13706SLuiz Augusto von Dentz 					       0, NULL, HCI_CMD_TIMEOUT);
4164d0b13706SLuiz Augusto von Dentz 	}
4165d0b13706SLuiz Augusto von Dentz 
4166d0b13706SLuiz Augusto von Dentz 	return 0;
4167d0b13706SLuiz Augusto von Dentz }
4168d0b13706SLuiz Augusto von Dentz 
4169d0b13706SLuiz Augusto von Dentz /* Read LE Min/Max Tx Power*/
4170d0b13706SLuiz Augusto von Dentz static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4171d0b13706SLuiz Augusto von Dentz {
4172d2f8114fSAditya Garg 	if (!(hdev->commands[38] & 0x80) ||
4173d2f8114fSAditya Garg 	    test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4174d0b13706SLuiz Augusto von Dentz 		return 0;
4175d0b13706SLuiz Augusto von Dentz 
4176d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4177d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
4178d0b13706SLuiz Augusto von Dentz }
4179d0b13706SLuiz Augusto von Dentz 
4180d0b13706SLuiz Augusto von Dentz /* Read LE Accept List Size */
4181d0b13706SLuiz Augusto von Dentz static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4182d0b13706SLuiz Augusto von Dentz {
4183d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[26] & 0x40))
4184d0b13706SLuiz Augusto von Dentz 		return 0;
4185d0b13706SLuiz Augusto von Dentz 
4186d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4187d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
4188d0b13706SLuiz Augusto von Dentz }
4189d0b13706SLuiz Augusto von Dentz 
4190d0b13706SLuiz Augusto von Dentz /* Clear LE Accept List */
4191d0b13706SLuiz Augusto von Dentz static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
4192d0b13706SLuiz Augusto von Dentz {
4193d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[26] & 0x80))
4194d0b13706SLuiz Augusto von Dentz 		return 0;
4195d0b13706SLuiz Augusto von Dentz 
4196d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
4197d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
4198d0b13706SLuiz Augusto von Dentz }
4199d0b13706SLuiz Augusto von Dentz 
4200d0b13706SLuiz Augusto von Dentz /* Read LE Resolving List Size */
4201d0b13706SLuiz Augusto von Dentz static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4202d0b13706SLuiz Augusto von Dentz {
4203d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[34] & 0x40))
4204d0b13706SLuiz Augusto von Dentz 		return 0;
4205d0b13706SLuiz Augusto von Dentz 
4206d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4207d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
4208d0b13706SLuiz Augusto von Dentz }
4209d0b13706SLuiz Augusto von Dentz 
4210d0b13706SLuiz Augusto von Dentz /* Clear LE Resolving List */
4211d0b13706SLuiz Augusto von Dentz static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4212d0b13706SLuiz Augusto von Dentz {
4213d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[34] & 0x20))
4214d0b13706SLuiz Augusto von Dentz 		return 0;
4215d0b13706SLuiz Augusto von Dentz 
4216d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4217d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
4218d0b13706SLuiz Augusto von Dentz }
4219d0b13706SLuiz Augusto von Dentz 
4220d0b13706SLuiz Augusto von Dentz /* Set RPA timeout */
4221d0b13706SLuiz Augusto von Dentz static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4222d0b13706SLuiz Augusto von Dentz {
4223d0b13706SLuiz Augusto von Dentz 	__le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4224d0b13706SLuiz Augusto von Dentz 
422591b6d02dSRaul Cheleguini 	if (!(hdev->commands[35] & 0x04) ||
422691b6d02dSRaul Cheleguini 	    test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4227d0b13706SLuiz Augusto von Dentz 		return 0;
4228d0b13706SLuiz Augusto von Dentz 
4229d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4230d0b13706SLuiz Augusto von Dentz 				     sizeof(timeout), &timeout,
4231d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
4232d0b13706SLuiz Augusto von Dentz }
4233d0b13706SLuiz Augusto von Dentz 
4234d0b13706SLuiz Augusto von Dentz /* Read LE Maximum Data Length */
4235d0b13706SLuiz Augusto von Dentz static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4236d0b13706SLuiz Augusto von Dentz {
4237d0b13706SLuiz Augusto von Dentz 	if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4238d0b13706SLuiz Augusto von Dentz 		return 0;
4239d0b13706SLuiz Augusto von Dentz 
4240d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4241d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
4242d0b13706SLuiz Augusto von Dentz }
4243d0b13706SLuiz Augusto von Dentz 
4244d0b13706SLuiz Augusto von Dentz /* Read LE Suggested Default Data Length */
4245d0b13706SLuiz Augusto von Dentz static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4246d0b13706SLuiz Augusto von Dentz {
4247d0b13706SLuiz Augusto von Dentz 	if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4248d0b13706SLuiz Augusto von Dentz 		return 0;
4249d0b13706SLuiz Augusto von Dentz 
4250d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4251d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
4252d0b13706SLuiz Augusto von Dentz }
4253d0b13706SLuiz Augusto von Dentz 
4254d0b13706SLuiz Augusto von Dentz /* Read LE Number of Supported Advertising Sets */
4255d0b13706SLuiz Augusto von Dentz static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4256d0b13706SLuiz Augusto von Dentz {
4257d0b13706SLuiz Augusto von Dentz 	if (!ext_adv_capable(hdev))
4258d0b13706SLuiz Augusto von Dentz 		return 0;
4259d0b13706SLuiz Augusto von Dentz 
4260d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev,
4261d0b13706SLuiz Augusto von Dentz 				     HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4262d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
4263d0b13706SLuiz Augusto von Dentz }
4264d0b13706SLuiz Augusto von Dentz 
4265d0b13706SLuiz Augusto von Dentz /* Write LE Host Supported */
4266d0b13706SLuiz Augusto von Dentz static int hci_set_le_support_sync(struct hci_dev *hdev)
4267d0b13706SLuiz Augusto von Dentz {
4268d0b13706SLuiz Augusto von Dentz 	struct hci_cp_write_le_host_supported cp;
4269d0b13706SLuiz Augusto von Dentz 
4270d0b13706SLuiz Augusto von Dentz 	/* LE-only devices do not support explicit enablement */
4271d0b13706SLuiz Augusto von Dentz 	if (!lmp_bredr_capable(hdev))
4272d0b13706SLuiz Augusto von Dentz 		return 0;
4273d0b13706SLuiz Augusto von Dentz 
4274d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4275d0b13706SLuiz Augusto von Dentz 
4276d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4277d0b13706SLuiz Augusto von Dentz 		cp.le = 0x01;
4278d0b13706SLuiz Augusto von Dentz 		cp.simul = 0x00;
4279d0b13706SLuiz Augusto von Dentz 	}
4280d0b13706SLuiz Augusto von Dentz 
4281d0b13706SLuiz Augusto von Dentz 	if (cp.le == lmp_host_le_capable(hdev))
4282d0b13706SLuiz Augusto von Dentz 		return 0;
4283d0b13706SLuiz Augusto von Dentz 
4284d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4285d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4286d0b13706SLuiz Augusto von Dentz }
4287d0b13706SLuiz Augusto von Dentz 
428826afbd82SLuiz Augusto von Dentz /* LE Set Host Feature */
428926afbd82SLuiz Augusto von Dentz static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
429026afbd82SLuiz Augusto von Dentz {
429126afbd82SLuiz Augusto von Dentz 	struct hci_cp_le_set_host_feature cp;
429226afbd82SLuiz Augusto von Dentz 
429326afbd82SLuiz Augusto von Dentz 	if (!iso_capable(hdev))
429426afbd82SLuiz Augusto von Dentz 		return 0;
429526afbd82SLuiz Augusto von Dentz 
429626afbd82SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
429726afbd82SLuiz Augusto von Dentz 
429826afbd82SLuiz Augusto von Dentz 	/* Isochronous Channels (Host Support) */
429926afbd82SLuiz Augusto von Dentz 	cp.bit_number = 32;
430026afbd82SLuiz Augusto von Dentz 	cp.bit_value = 1;
430126afbd82SLuiz Augusto von Dentz 
430226afbd82SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
430326afbd82SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
430426afbd82SLuiz Augusto von Dentz }
430526afbd82SLuiz Augusto von Dentz 
4306d0b13706SLuiz Augusto von Dentz /* LE Controller init stage 3 command sequence */
4307d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init3[] = {
4308d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_SET_EVENT_MASK */
4309d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_set_event_mask_sync),
4310d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_ADV_TX_POWER */
4311d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_adv_tx_power_sync),
4312d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_TRANSMIT_POWER */
4313d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_tx_power_sync),
4314d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4315d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_accept_list_size_sync),
4316d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4317d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_clear_accept_list_sync),
4318d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4319d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_resolv_list_size_sync),
4320d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_CLEAR_RESOLV_LIST */
4321d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_clear_resolv_list_sync),
4322d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_SET_RPA_TIMEOUT */
4323d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_set_rpa_timeout_sync),
4324d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_MAX_DATA_LEN */
4325d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_max_data_len_sync),
4326d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_DEF_DATA_LEN */
4327d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_def_data_len_sync),
4328d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4329d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4330d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4331d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_set_le_support_sync),
433226afbd82SLuiz Augusto von Dentz 	/* HCI_OP_LE_SET_HOST_FEATURE */
433326afbd82SLuiz Augusto von Dentz 	HCI_INIT(hci_le_set_host_feature_sync),
4334d0b13706SLuiz Augusto von Dentz 	{}
4335d0b13706SLuiz Augusto von Dentz };
4336d0b13706SLuiz Augusto von Dentz 
4337d0b13706SLuiz Augusto von Dentz static int hci_init3_sync(struct hci_dev *hdev)
4338d0b13706SLuiz Augusto von Dentz {
4339d0b13706SLuiz Augusto von Dentz 	int err;
4340d0b13706SLuiz Augusto von Dentz 
4341d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
4342d0b13706SLuiz Augusto von Dentz 
4343d0b13706SLuiz Augusto von Dentz 	err = hci_init_stage_sync(hdev, hci_init3);
4344d0b13706SLuiz Augusto von Dentz 	if (err)
4345d0b13706SLuiz Augusto von Dentz 		return err;
4346d0b13706SLuiz Augusto von Dentz 
4347d0b13706SLuiz Augusto von Dentz 	if (lmp_le_capable(hdev))
4348d0b13706SLuiz Augusto von Dentz 		return hci_init_stage_sync(hdev, le_init3);
4349d0b13706SLuiz Augusto von Dentz 
4350d0b13706SLuiz Augusto von Dentz 	return 0;
4351d0b13706SLuiz Augusto von Dentz }
4352d0b13706SLuiz Augusto von Dentz 
4353d0b13706SLuiz Augusto von Dentz static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4354d0b13706SLuiz Augusto von Dentz {
4355d0b13706SLuiz Augusto von Dentz 	struct hci_cp_delete_stored_link_key cp;
4356d0b13706SLuiz Augusto von Dentz 
4357d0b13706SLuiz Augusto von Dentz 	/* Some Broadcom based Bluetooth controllers do not support the
4358d0b13706SLuiz Augusto von Dentz 	 * Delete Stored Link Key command. They are clearly indicating its
4359d0b13706SLuiz Augusto von Dentz 	 * absence in the bit mask of supported commands.
4360d0b13706SLuiz Augusto von Dentz 	 *
4361d0b13706SLuiz Augusto von Dentz 	 * Check the supported commands and only if the command is marked
4362d0b13706SLuiz Augusto von Dentz 	 * as supported send it. If not supported assume that the controller
4363d0b13706SLuiz Augusto von Dentz 	 * does not have actual support for stored link keys which makes this
4364d0b13706SLuiz Augusto von Dentz 	 * command redundant anyway.
4365d0b13706SLuiz Augusto von Dentz 	 *
4366d0b13706SLuiz Augusto von Dentz 	 * Some controllers indicate that they support handling deleting
4367d0b13706SLuiz Augusto von Dentz 	 * stored link keys, but they don't. The quirk lets a driver
4368d0b13706SLuiz Augusto von Dentz 	 * just disable this command.
4369d0b13706SLuiz Augusto von Dentz 	 */
4370d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[6] & 0x80) ||
4371d0b13706SLuiz Augusto von Dentz 	    test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4372d0b13706SLuiz Augusto von Dentz 		return 0;
4373d0b13706SLuiz Augusto von Dentz 
4374d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4375d0b13706SLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, BDADDR_ANY);
4376d0b13706SLuiz Augusto von Dentz 	cp.delete_all = 0x01;
4377d0b13706SLuiz Augusto von Dentz 
4378d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4379d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4380d0b13706SLuiz Augusto von Dentz }
4381d0b13706SLuiz Augusto von Dentz 
4382d0b13706SLuiz Augusto von Dentz static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4383d0b13706SLuiz Augusto von Dentz {
4384d0b13706SLuiz Augusto von Dentz 	u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4385d0b13706SLuiz Augusto von Dentz 	bool changed = false;
4386d0b13706SLuiz Augusto von Dentz 
4387d0b13706SLuiz Augusto von Dentz 	/* Set event mask page 2 if the HCI command for it is supported */
4388d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[22] & 0x04))
4389d0b13706SLuiz Augusto von Dentz 		return 0;
4390d0b13706SLuiz Augusto von Dentz 
4391d0b13706SLuiz Augusto von Dentz 	/* If Connectionless Peripheral Broadcast central role is supported
4392d0b13706SLuiz Augusto von Dentz 	 * enable all necessary events for it.
4393d0b13706SLuiz Augusto von Dentz 	 */
4394d0b13706SLuiz Augusto von Dentz 	if (lmp_cpb_central_capable(hdev)) {
4395d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x40;	/* Triggered Clock Capture */
4396d0b13706SLuiz Augusto von Dentz 		events[1] |= 0x80;	/* Synchronization Train Complete */
43970feb8af0SZijun Hu 		events[2] |= 0x08;	/* Truncated Page Complete */
4398d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x20;	/* CPB Channel Map Change */
4399d0b13706SLuiz Augusto von Dentz 		changed = true;
4400d0b13706SLuiz Augusto von Dentz 	}
4401d0b13706SLuiz Augusto von Dentz 
4402d0b13706SLuiz Augusto von Dentz 	/* If Connectionless Peripheral Broadcast peripheral role is supported
4403d0b13706SLuiz Augusto von Dentz 	 * enable all necessary events for it.
4404d0b13706SLuiz Augusto von Dentz 	 */
4405d0b13706SLuiz Augusto von Dentz 	if (lmp_cpb_peripheral_capable(hdev)) {
4406d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x01;	/* Synchronization Train Received */
4407d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x02;	/* CPB Receive */
4408d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x04;	/* CPB Timeout */
44090feb8af0SZijun Hu 		events[2] |= 0x10;	/* Peripheral Page Response Timeout */
4410d0b13706SLuiz Augusto von Dentz 		changed = true;
4411d0b13706SLuiz Augusto von Dentz 	}
4412d0b13706SLuiz Augusto von Dentz 
4413d0b13706SLuiz Augusto von Dentz 	/* Enable Authenticated Payload Timeout Expired event if supported */
4414d0b13706SLuiz Augusto von Dentz 	if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4415d0b13706SLuiz Augusto von Dentz 		events[2] |= 0x80;
4416d0b13706SLuiz Augusto von Dentz 		changed = true;
4417d0b13706SLuiz Augusto von Dentz 	}
4418d0b13706SLuiz Augusto von Dentz 
4419d0b13706SLuiz Augusto von Dentz 	/* Some Broadcom based controllers indicate support for Set Event
4420d0b13706SLuiz Augusto von Dentz 	 * Mask Page 2 command, but then actually do not support it. Since
4421d0b13706SLuiz Augusto von Dentz 	 * the default value is all bits set to zero, the command is only
4422d0b13706SLuiz Augusto von Dentz 	 * required if the event mask has to be changed. In case no change
4423d0b13706SLuiz Augusto von Dentz 	 * to the event mask is needed, skip this command.
4424d0b13706SLuiz Augusto von Dentz 	 */
4425d0b13706SLuiz Augusto von Dentz 	if (!changed)
4426d0b13706SLuiz Augusto von Dentz 		return 0;
4427d0b13706SLuiz Augusto von Dentz 
4428d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4429d0b13706SLuiz Augusto von Dentz 				     sizeof(events), events, HCI_CMD_TIMEOUT);
4430d0b13706SLuiz Augusto von Dentz }
4431d0b13706SLuiz Augusto von Dentz 
4432d0b13706SLuiz Augusto von Dentz /* Read local codec list if the HCI command is supported */
4433d0b13706SLuiz Augusto von Dentz static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4434d0b13706SLuiz Augusto von Dentz {
4435828cea2bSChethan T N 	if (hdev->commands[45] & 0x04)
4436828cea2bSChethan T N 		hci_read_supported_codecs_v2(hdev);
4437828cea2bSChethan T N 	else if (hdev->commands[29] & 0x20)
4438828cea2bSChethan T N 		hci_read_supported_codecs(hdev);
4439d0b13706SLuiz Augusto von Dentz 
4440828cea2bSChethan T N 	return 0;
4441d0b13706SLuiz Augusto von Dentz }
4442d0b13706SLuiz Augusto von Dentz 
4443d0b13706SLuiz Augusto von Dentz /* Read local pairing options if the HCI command is supported */
4444d0b13706SLuiz Augusto von Dentz static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4445d0b13706SLuiz Augusto von Dentz {
4446d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[41] & 0x08))
4447d0b13706SLuiz Augusto von Dentz 		return 0;
4448d0b13706SLuiz Augusto von Dentz 
4449d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4450d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
4451d0b13706SLuiz Augusto von Dentz }
4452d0b13706SLuiz Augusto von Dentz 
4453d0b13706SLuiz Augusto von Dentz /* Get MWS transport configuration if the HCI command is supported */
4454d0b13706SLuiz Augusto von Dentz static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4455d0b13706SLuiz Augusto von Dentz {
4456ffcb0a44SSven Peter 	if (!mws_transport_config_capable(hdev))
4457d0b13706SLuiz Augusto von Dentz 		return 0;
4458d0b13706SLuiz Augusto von Dentz 
4459d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4460d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
4461d0b13706SLuiz Augusto von Dentz }
4462d0b13706SLuiz Augusto von Dentz 
4463d0b13706SLuiz Augusto von Dentz /* Check for Synchronization Train support */
4464d0b13706SLuiz Augusto von Dentz static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4465d0b13706SLuiz Augusto von Dentz {
4466d0b13706SLuiz Augusto von Dentz 	if (!lmp_sync_train_capable(hdev))
4467d0b13706SLuiz Augusto von Dentz 		return 0;
4468d0b13706SLuiz Augusto von Dentz 
4469d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4470d0b13706SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
4471d0b13706SLuiz Augusto von Dentz }
4472d0b13706SLuiz Augusto von Dentz 
4473d0b13706SLuiz Augusto von Dentz /* Enable Secure Connections if supported and configured */
4474d0b13706SLuiz Augusto von Dentz static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4475d0b13706SLuiz Augusto von Dentz {
4476d0b13706SLuiz Augusto von Dentz 	u8 support = 0x01;
4477d0b13706SLuiz Augusto von Dentz 
4478d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4479d0b13706SLuiz Augusto von Dentz 	    !bredr_sc_enabled(hdev))
4480d0b13706SLuiz Augusto von Dentz 		return 0;
4481d0b13706SLuiz Augusto von Dentz 
4482d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4483d0b13706SLuiz Augusto von Dentz 				     sizeof(support), &support,
4484d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
4485d0b13706SLuiz Augusto von Dentz }
4486d0b13706SLuiz Augusto von Dentz 
4487d0b13706SLuiz Augusto von Dentz /* Set erroneous data reporting if supported to the wideband speech
4488d0b13706SLuiz Augusto von Dentz  * setting value
4489d0b13706SLuiz Augusto von Dentz  */
4490d0b13706SLuiz Augusto von Dentz static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4491d0b13706SLuiz Augusto von Dentz {
4492d0b13706SLuiz Augusto von Dentz 	struct hci_cp_write_def_err_data_reporting cp;
4493d0b13706SLuiz Augusto von Dentz 	bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4494d0b13706SLuiz Augusto von Dentz 
4495d0b13706SLuiz Augusto von Dentz 	if (!(hdev->commands[18] & 0x08) ||
449642d7731eSIsmael Ferreras Morezuelas 	    !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
449742d7731eSIsmael Ferreras Morezuelas 	    test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4498d0b13706SLuiz Augusto von Dentz 		return 0;
4499d0b13706SLuiz Augusto von Dentz 
4500d0b13706SLuiz Augusto von Dentz 	if (enabled == hdev->err_data_reporting)
4501d0b13706SLuiz Augusto von Dentz 		return 0;
4502d0b13706SLuiz Augusto von Dentz 
4503d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4504d0b13706SLuiz Augusto von Dentz 	cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4505d0b13706SLuiz Augusto von Dentz 				ERR_DATA_REPORTING_DISABLED;
4506d0b13706SLuiz Augusto von Dentz 
4507d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4508d0b13706SLuiz Augusto von Dentz 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4509d0b13706SLuiz Augusto von Dentz }
4510d0b13706SLuiz Augusto von Dentz 
4511d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage hci_init4[] = {
4512d0b13706SLuiz Augusto von Dentz 	 /* HCI_OP_DELETE_STORED_LINK_KEY */
4513d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_delete_stored_link_key_sync),
4514d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4515d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_set_event_mask_page_2_sync),
4516d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_READ_LOCAL_CODECS */
4517d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_codecs_sync),
4518d0b13706SLuiz Augusto von Dentz 	 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4519d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_local_pairing_opts_sync),
4520d0b13706SLuiz Augusto von Dentz 	 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4521d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_get_mws_transport_config_sync),
4522d0b13706SLuiz Augusto von Dentz 	 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4523d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_read_sync_train_params_sync),
4524d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_SC_SUPPORT */
4525d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_write_sc_support_1_sync),
4526d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4527d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_set_err_data_report_sync),
4528d0b13706SLuiz Augusto von Dentz 	{}
4529d0b13706SLuiz Augusto von Dentz };
4530d0b13706SLuiz Augusto von Dentz 
4531d0b13706SLuiz Augusto von Dentz /* Set Suggested Default Data Length to maximum if supported */
4532d0b13706SLuiz Augusto von Dentz static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4533d0b13706SLuiz Augusto von Dentz {
4534d0b13706SLuiz Augusto von Dentz 	struct hci_cp_le_write_def_data_len cp;
4535d0b13706SLuiz Augusto von Dentz 
4536d0b13706SLuiz Augusto von Dentz 	if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4537d0b13706SLuiz Augusto von Dentz 		return 0;
4538d0b13706SLuiz Augusto von Dentz 
4539d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4540d0b13706SLuiz Augusto von Dentz 	cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4541d0b13706SLuiz Augusto von Dentz 	cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4542d0b13706SLuiz Augusto von Dentz 
4543d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4544d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4545d0b13706SLuiz Augusto von Dentz }
4546d0b13706SLuiz Augusto von Dentz 
4547288c9022SLuiz Augusto von Dentz /* Set Default PHY parameters if command is supported, enables all supported
4548288c9022SLuiz Augusto von Dentz  * PHYs according to the LE Features bits.
4549288c9022SLuiz Augusto von Dentz  */
4550d0b13706SLuiz Augusto von Dentz static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4551d0b13706SLuiz Augusto von Dentz {
4552d0b13706SLuiz Augusto von Dentz 	struct hci_cp_le_set_default_phy cp;
4553d0b13706SLuiz Augusto von Dentz 
4554288c9022SLuiz Augusto von Dentz 	if (!(hdev->commands[35] & 0x20)) {
4555288c9022SLuiz Augusto von Dentz 		/* If the command is not supported it means only 1M PHY is
4556288c9022SLuiz Augusto von Dentz 		 * supported.
4557288c9022SLuiz Augusto von Dentz 		 */
4558288c9022SLuiz Augusto von Dentz 		hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4559288c9022SLuiz Augusto von Dentz 		hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4560d0b13706SLuiz Augusto von Dentz 		return 0;
4561288c9022SLuiz Augusto von Dentz 	}
4562d0b13706SLuiz Augusto von Dentz 
4563d0b13706SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
4564d0b13706SLuiz Augusto von Dentz 	cp.all_phys = 0x00;
4565288c9022SLuiz Augusto von Dentz 	cp.tx_phys = HCI_LE_SET_PHY_1M;
4566288c9022SLuiz Augusto von Dentz 	cp.rx_phys = HCI_LE_SET_PHY_1M;
4567288c9022SLuiz Augusto von Dentz 
4568288c9022SLuiz Augusto von Dentz 	/* Enables 2M PHY if supported */
4569288c9022SLuiz Augusto von Dentz 	if (le_2m_capable(hdev)) {
4570288c9022SLuiz Augusto von Dentz 		cp.tx_phys |= HCI_LE_SET_PHY_2M;
4571288c9022SLuiz Augusto von Dentz 		cp.rx_phys |= HCI_LE_SET_PHY_2M;
4572288c9022SLuiz Augusto von Dentz 	}
4573288c9022SLuiz Augusto von Dentz 
4574288c9022SLuiz Augusto von Dentz 	/* Enables Coded PHY if supported */
4575288c9022SLuiz Augusto von Dentz 	if (le_coded_capable(hdev)) {
4576288c9022SLuiz Augusto von Dentz 		cp.tx_phys |= HCI_LE_SET_PHY_CODED;
4577288c9022SLuiz Augusto von Dentz 		cp.rx_phys |= HCI_LE_SET_PHY_CODED;
4578288c9022SLuiz Augusto von Dentz 	}
4579d0b13706SLuiz Augusto von Dentz 
4580d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4581d0b13706SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4582d0b13706SLuiz Augusto von Dentz }
4583d0b13706SLuiz Augusto von Dentz 
4584d0b13706SLuiz Augusto von Dentz static const struct hci_init_stage le_init4[] = {
4585d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4586d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_set_write_def_data_len_sync),
4587d0b13706SLuiz Augusto von Dentz 	/* HCI_OP_LE_SET_DEFAULT_PHY */
4588d0b13706SLuiz Augusto von Dentz 	HCI_INIT(hci_le_set_default_phy_sync),
4589d0b13706SLuiz Augusto von Dentz 	{}
4590d0b13706SLuiz Augusto von Dentz };
4591d0b13706SLuiz Augusto von Dentz 
4592d0b13706SLuiz Augusto von Dentz static int hci_init4_sync(struct hci_dev *hdev)
4593d0b13706SLuiz Augusto von Dentz {
4594d0b13706SLuiz Augusto von Dentz 	int err;
4595d0b13706SLuiz Augusto von Dentz 
4596d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
4597d0b13706SLuiz Augusto von Dentz 
4598d0b13706SLuiz Augusto von Dentz 	err = hci_init_stage_sync(hdev, hci_init4);
4599d0b13706SLuiz Augusto von Dentz 	if (err)
4600d0b13706SLuiz Augusto von Dentz 		return err;
4601d0b13706SLuiz Augusto von Dentz 
4602d0b13706SLuiz Augusto von Dentz 	if (lmp_le_capable(hdev))
4603d0b13706SLuiz Augusto von Dentz 		return hci_init_stage_sync(hdev, le_init4);
4604d0b13706SLuiz Augusto von Dentz 
4605d0b13706SLuiz Augusto von Dentz 	return 0;
4606d0b13706SLuiz Augusto von Dentz }
4607d0b13706SLuiz Augusto von Dentz 
4608d0b13706SLuiz Augusto von Dentz static int hci_init_sync(struct hci_dev *hdev)
4609d0b13706SLuiz Augusto von Dentz {
4610d0b13706SLuiz Augusto von Dentz 	int err;
4611d0b13706SLuiz Augusto von Dentz 
4612d0b13706SLuiz Augusto von Dentz 	err = hci_init1_sync(hdev);
4613d0b13706SLuiz Augusto von Dentz 	if (err < 0)
4614d0b13706SLuiz Augusto von Dentz 		return err;
4615d0b13706SLuiz Augusto von Dentz 
4616d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_SETUP))
4617d0b13706SLuiz Augusto von Dentz 		hci_debugfs_create_basic(hdev);
4618d0b13706SLuiz Augusto von Dentz 
4619d0b13706SLuiz Augusto von Dentz 	err = hci_init2_sync(hdev);
4620d0b13706SLuiz Augusto von Dentz 	if (err < 0)
4621d0b13706SLuiz Augusto von Dentz 		return err;
4622d0b13706SLuiz Augusto von Dentz 
4623d0b13706SLuiz Augusto von Dentz 	/* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
4624d0b13706SLuiz Augusto von Dentz 	 * BR/EDR/LE type controllers. AMP controllers only need the
4625d0b13706SLuiz Augusto von Dentz 	 * first two stages of init.
4626d0b13706SLuiz Augusto von Dentz 	 */
4627d0b13706SLuiz Augusto von Dentz 	if (hdev->dev_type != HCI_PRIMARY)
4628d0b13706SLuiz Augusto von Dentz 		return 0;
4629d0b13706SLuiz Augusto von Dentz 
4630d0b13706SLuiz Augusto von Dentz 	err = hci_init3_sync(hdev);
4631d0b13706SLuiz Augusto von Dentz 	if (err < 0)
4632d0b13706SLuiz Augusto von Dentz 		return err;
4633d0b13706SLuiz Augusto von Dentz 
4634d0b13706SLuiz Augusto von Dentz 	err = hci_init4_sync(hdev);
4635d0b13706SLuiz Augusto von Dentz 	if (err < 0)
4636d0b13706SLuiz Augusto von Dentz 		return err;
4637d0b13706SLuiz Augusto von Dentz 
4638d0b13706SLuiz Augusto von Dentz 	/* This function is only called when the controller is actually in
4639d0b13706SLuiz Augusto von Dentz 	 * configured state. When the controller is marked as unconfigured,
4640d0b13706SLuiz Augusto von Dentz 	 * this initialization procedure is not run.
4641d0b13706SLuiz Augusto von Dentz 	 *
4642d0b13706SLuiz Augusto von Dentz 	 * It means that it is possible that a controller runs through its
4643d0b13706SLuiz Augusto von Dentz 	 * setup phase and then discovers missing settings. If that is the
4644d0b13706SLuiz Augusto von Dentz 	 * case, then this function will not be called. It then will only
4645d0b13706SLuiz Augusto von Dentz 	 * be called during the config phase.
4646d0b13706SLuiz Augusto von Dentz 	 *
4647d0b13706SLuiz Augusto von Dentz 	 * So only when in setup phase or config phase, create the debugfs
4648d0b13706SLuiz Augusto von Dentz 	 * entries and register the SMP channels.
4649d0b13706SLuiz Augusto von Dentz 	 */
4650d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4651d0b13706SLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_CONFIG))
4652d0b13706SLuiz Augusto von Dentz 		return 0;
4653d0b13706SLuiz Augusto von Dentz 
4654fe2ccc6cSJohan Hovold 	if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4655fe2ccc6cSJohan Hovold 		return 0;
4656fe2ccc6cSJohan Hovold 
4657d0b13706SLuiz Augusto von Dentz 	hci_debugfs_create_common(hdev);
4658d0b13706SLuiz Augusto von Dentz 
4659d0b13706SLuiz Augusto von Dentz 	if (lmp_bredr_capable(hdev))
4660d0b13706SLuiz Augusto von Dentz 		hci_debugfs_create_bredr(hdev);
4661d0b13706SLuiz Augusto von Dentz 
4662d0b13706SLuiz Augusto von Dentz 	if (lmp_le_capable(hdev))
4663d0b13706SLuiz Augusto von Dentz 		hci_debugfs_create_le(hdev);
4664d0b13706SLuiz Augusto von Dentz 
4665d0b13706SLuiz Augusto von Dentz 	return 0;
4666d0b13706SLuiz Augusto von Dentz }
4667d0b13706SLuiz Augusto von Dentz 
46686b5c1cdaSLuiz Augusto von Dentz #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
46696b5c1cdaSLuiz Augusto von Dentz 
46706b5c1cdaSLuiz Augusto von Dentz static const struct {
46716b5c1cdaSLuiz Augusto von Dentz 	unsigned long quirk;
46726b5c1cdaSLuiz Augusto von Dentz 	const char *desc;
46736b5c1cdaSLuiz Augusto von Dentz } hci_broken_table[] = {
46746b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
46756b5c1cdaSLuiz Augusto von Dentz 			 "HCI Read Local Supported Commands not supported"),
46766b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(STORED_LINK_KEY,
46776b5c1cdaSLuiz Augusto von Dentz 			 "HCI Delete Stored Link Key command is advertised, "
46786b5c1cdaSLuiz Augusto von Dentz 			 "but not supported."),
467942d7731eSIsmael Ferreras Morezuelas 	HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
468042d7731eSIsmael Ferreras Morezuelas 			 "HCI Read Default Erroneous Data Reporting command is "
468142d7731eSIsmael Ferreras Morezuelas 			 "advertised, but not supported."),
46826b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
46836b5c1cdaSLuiz Augusto von Dentz 			 "HCI Read Transmit Power Level command is advertised, "
46846b5c1cdaSLuiz Augusto von Dentz 			 "but not supported."),
46856b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
46866b5c1cdaSLuiz Augusto von Dentz 			 "HCI Set Event Filter command not supported."),
46876b5c1cdaSLuiz Augusto von Dentz 	HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
46886b5c1cdaSLuiz Augusto von Dentz 			 "HCI Enhanced Setup Synchronous Connection command is "
468991b6d02dSRaul Cheleguini 			 "advertised, but not supported."),
469091b6d02dSRaul Cheleguini 	HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
469191b6d02dSRaul Cheleguini 			 "HCI LE Set Random Private Address Timeout command is "
46926b5c1cdaSLuiz Augusto von Dentz 			 "advertised, but not supported.")
46936b5c1cdaSLuiz Augusto von Dentz };
46946b5c1cdaSLuiz Augusto von Dentz 
46951bbf4023SLuiz Augusto von Dentz /* This function handles hdev setup stage:
46961bbf4023SLuiz Augusto von Dentz  *
46971bbf4023SLuiz Augusto von Dentz  * Calls hdev->setup
46981bbf4023SLuiz Augusto von Dentz  * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
46991bbf4023SLuiz Augusto von Dentz  */
47001bbf4023SLuiz Augusto von Dentz static int hci_dev_setup_sync(struct hci_dev *hdev)
4701d0b13706SLuiz Augusto von Dentz {
4702d0b13706SLuiz Augusto von Dentz 	int ret = 0;
47031bbf4023SLuiz Augusto von Dentz 	bool invalid_bdaddr;
47041bbf4023SLuiz Augusto von Dentz 	size_t i;
47051bbf4023SLuiz Augusto von Dentz 
47061bbf4023SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
47071bbf4023SLuiz Augusto von Dentz 	    !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
47081bbf4023SLuiz Augusto von Dentz 		return 0;
47091bbf4023SLuiz Augusto von Dentz 
47101bbf4023SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
47111bbf4023SLuiz Augusto von Dentz 
47121bbf4023SLuiz Augusto von Dentz 	hci_sock_dev_event(hdev, HCI_DEV_SETUP);
47131bbf4023SLuiz Augusto von Dentz 
47141bbf4023SLuiz Augusto von Dentz 	if (hdev->setup)
47151bbf4023SLuiz Augusto von Dentz 		ret = hdev->setup(hdev);
47161bbf4023SLuiz Augusto von Dentz 
47171bbf4023SLuiz Augusto von Dentz 	for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
47181bbf4023SLuiz Augusto von Dentz 		if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
47191bbf4023SLuiz Augusto von Dentz 			bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
47201bbf4023SLuiz Augusto von Dentz 	}
47211bbf4023SLuiz Augusto von Dentz 
47221bbf4023SLuiz Augusto von Dentz 	/* The transport driver can set the quirk to mark the
47231bbf4023SLuiz Augusto von Dentz 	 * BD_ADDR invalid before creating the HCI device or in
47241bbf4023SLuiz Augusto von Dentz 	 * its setup callback.
47251bbf4023SLuiz Augusto von Dentz 	 */
47266945795bSJohan Hovold 	invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
47276945795bSJohan Hovold 			 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
47281bbf4023SLuiz Augusto von Dentz 	if (!ret) {
47290cb73658SJohan Hovold 		if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
47300cb73658SJohan Hovold 		    !bacmp(&hdev->public_addr, BDADDR_ANY))
47311bbf4023SLuiz Augusto von Dentz 			hci_dev_get_bd_addr_from_property(hdev);
47321bbf4023SLuiz Augusto von Dentz 
47336945795bSJohan Hovold 		if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
47341bbf4023SLuiz Augusto von Dentz 		    hdev->set_bdaddr) {
47350cb73658SJohan Hovold 			ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
47361bbf4023SLuiz Augusto von Dentz 			if (!ret)
47371bbf4023SLuiz Augusto von Dentz 				invalid_bdaddr = false;
47381bbf4023SLuiz Augusto von Dentz 		}
47391bbf4023SLuiz Augusto von Dentz 	}
47401bbf4023SLuiz Augusto von Dentz 
47411bbf4023SLuiz Augusto von Dentz 	/* The transport driver can set these quirks before
47421bbf4023SLuiz Augusto von Dentz 	 * creating the HCI device or in its setup callback.
47431bbf4023SLuiz Augusto von Dentz 	 *
47441bbf4023SLuiz Augusto von Dentz 	 * For the invalid BD_ADDR quirk it is possible that
47451bbf4023SLuiz Augusto von Dentz 	 * it becomes a valid address if the bootloader does
47461bbf4023SLuiz Augusto von Dentz 	 * provide it (see above).
47471bbf4023SLuiz Augusto von Dentz 	 *
47481bbf4023SLuiz Augusto von Dentz 	 * In case any of them is set, the controller has to
47491bbf4023SLuiz Augusto von Dentz 	 * start up as unconfigured.
47501bbf4023SLuiz Augusto von Dentz 	 */
47511bbf4023SLuiz Augusto von Dentz 	if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
47521bbf4023SLuiz Augusto von Dentz 	    invalid_bdaddr)
47531bbf4023SLuiz Augusto von Dentz 		hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
47541bbf4023SLuiz Augusto von Dentz 
47551bbf4023SLuiz Augusto von Dentz 	/* For an unconfigured controller it is required to
47561bbf4023SLuiz Augusto von Dentz 	 * read at least the version information provided by
47571bbf4023SLuiz Augusto von Dentz 	 * the Read Local Version Information command.
47581bbf4023SLuiz Augusto von Dentz 	 *
47591bbf4023SLuiz Augusto von Dentz 	 * If the set_bdaddr driver callback is provided, then
47601bbf4023SLuiz Augusto von Dentz 	 * also the original Bluetooth public device address
47611bbf4023SLuiz Augusto von Dentz 	 * will be read using the Read BD Address command.
47621bbf4023SLuiz Augusto von Dentz 	 */
47631bbf4023SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
47641bbf4023SLuiz Augusto von Dentz 		return hci_unconf_init_sync(hdev);
47651bbf4023SLuiz Augusto von Dentz 
47661bbf4023SLuiz Augusto von Dentz 	return ret;
47671bbf4023SLuiz Augusto von Dentz }
47681bbf4023SLuiz Augusto von Dentz 
47691bbf4023SLuiz Augusto von Dentz /* This function handles hdev init stage:
47701bbf4023SLuiz Augusto von Dentz  *
47711bbf4023SLuiz Augusto von Dentz  * Calls hci_dev_setup_sync to perform setup stage
47721bbf4023SLuiz Augusto von Dentz  * Calls hci_init_sync to perform HCI command init sequence
47731bbf4023SLuiz Augusto von Dentz  */
47741bbf4023SLuiz Augusto von Dentz static int hci_dev_init_sync(struct hci_dev *hdev)
47751bbf4023SLuiz Augusto von Dentz {
47761bbf4023SLuiz Augusto von Dentz 	int ret;
47771bbf4023SLuiz Augusto von Dentz 
47781bbf4023SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
47791bbf4023SLuiz Augusto von Dentz 
47801bbf4023SLuiz Augusto von Dentz 	atomic_set(&hdev->cmd_cnt, 1);
47811bbf4023SLuiz Augusto von Dentz 	set_bit(HCI_INIT, &hdev->flags);
47821bbf4023SLuiz Augusto von Dentz 
47831bbf4023SLuiz Augusto von Dentz 	ret = hci_dev_setup_sync(hdev);
47841bbf4023SLuiz Augusto von Dentz 
47851bbf4023SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
47861bbf4023SLuiz Augusto von Dentz 		/* If public address change is configured, ensure that
47871bbf4023SLuiz Augusto von Dentz 		 * the address gets programmed. If the driver does not
47881bbf4023SLuiz Augusto von Dentz 		 * support changing the public address, fail the power
47891bbf4023SLuiz Augusto von Dentz 		 * on procedure.
47901bbf4023SLuiz Augusto von Dentz 		 */
47911bbf4023SLuiz Augusto von Dentz 		if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
47921bbf4023SLuiz Augusto von Dentz 		    hdev->set_bdaddr)
47931bbf4023SLuiz Augusto von Dentz 			ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
47941bbf4023SLuiz Augusto von Dentz 		else
47951bbf4023SLuiz Augusto von Dentz 			ret = -EADDRNOTAVAIL;
47961bbf4023SLuiz Augusto von Dentz 	}
47971bbf4023SLuiz Augusto von Dentz 
47981bbf4023SLuiz Augusto von Dentz 	if (!ret) {
47991bbf4023SLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
48001bbf4023SLuiz Augusto von Dentz 		    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
48011bbf4023SLuiz Augusto von Dentz 			ret = hci_init_sync(hdev);
48021bbf4023SLuiz Augusto von Dentz 			if (!ret && hdev->post_init)
48031bbf4023SLuiz Augusto von Dentz 				ret = hdev->post_init(hdev);
48041bbf4023SLuiz Augusto von Dentz 		}
48051bbf4023SLuiz Augusto von Dentz 	}
48061bbf4023SLuiz Augusto von Dentz 
48071bbf4023SLuiz Augusto von Dentz 	/* If the HCI Reset command is clearing all diagnostic settings,
48081bbf4023SLuiz Augusto von Dentz 	 * then they need to be reprogrammed after the init procedure
48091bbf4023SLuiz Augusto von Dentz 	 * completed.
48101bbf4023SLuiz Augusto von Dentz 	 */
48111bbf4023SLuiz Augusto von Dentz 	if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
48121bbf4023SLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
48131bbf4023SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
48141bbf4023SLuiz Augusto von Dentz 		ret = hdev->set_diag(hdev, true);
48151bbf4023SLuiz Augusto von Dentz 
48161bbf4023SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
48171bbf4023SLuiz Augusto von Dentz 		msft_do_open(hdev);
48181bbf4023SLuiz Augusto von Dentz 		aosp_do_open(hdev);
48191bbf4023SLuiz Augusto von Dentz 	}
48201bbf4023SLuiz Augusto von Dentz 
48211bbf4023SLuiz Augusto von Dentz 	clear_bit(HCI_INIT, &hdev->flags);
48221bbf4023SLuiz Augusto von Dentz 
48231bbf4023SLuiz Augusto von Dentz 	return ret;
48241bbf4023SLuiz Augusto von Dentz }
48251bbf4023SLuiz Augusto von Dentz 
48261bbf4023SLuiz Augusto von Dentz int hci_dev_open_sync(struct hci_dev *hdev)
48271bbf4023SLuiz Augusto von Dentz {
48281bbf4023SLuiz Augusto von Dentz 	int ret;
4829d0b13706SLuiz Augusto von Dentz 
4830d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
4831d0b13706SLuiz Augusto von Dentz 
4832d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4833d0b13706SLuiz Augusto von Dentz 		ret = -ENODEV;
4834d0b13706SLuiz Augusto von Dentz 		goto done;
4835d0b13706SLuiz Augusto von Dentz 	}
4836d0b13706SLuiz Augusto von Dentz 
4837d0b13706SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4838d0b13706SLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4839d0b13706SLuiz Augusto von Dentz 		/* Check for rfkill but allow the HCI setup stage to
4840d0b13706SLuiz Augusto von Dentz 		 * proceed (which in itself doesn't cause any RF activity).
4841d0b13706SLuiz Augusto von Dentz 		 */
4842d0b13706SLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4843d0b13706SLuiz Augusto von Dentz 			ret = -ERFKILL;
4844d0b13706SLuiz Augusto von Dentz 			goto done;
4845d0b13706SLuiz Augusto von Dentz 		}
4846d0b13706SLuiz Augusto von Dentz 
4847d0b13706SLuiz Augusto von Dentz 		/* Check for valid public address or a configured static
4848d0b13706SLuiz Augusto von Dentz 		 * random address, but let the HCI setup proceed to
4849d0b13706SLuiz Augusto von Dentz 		 * be able to determine if there is a public address
4850d0b13706SLuiz Augusto von Dentz 		 * or not.
4851d0b13706SLuiz Augusto von Dentz 		 *
4852d0b13706SLuiz Augusto von Dentz 		 * In case of user channel usage, it is not important
4853d0b13706SLuiz Augusto von Dentz 		 * if a public address or static random address is
4854d0b13706SLuiz Augusto von Dentz 		 * available.
4855d0b13706SLuiz Augusto von Dentz 		 *
4856d0b13706SLuiz Augusto von Dentz 		 * This check is only valid for BR/EDR controllers
4857d0b13706SLuiz Augusto von Dentz 		 * since AMP controllers do not have an address.
4858d0b13706SLuiz Augusto von Dentz 		 */
4859d0b13706SLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4860d0b13706SLuiz Augusto von Dentz 		    hdev->dev_type == HCI_PRIMARY &&
4861d0b13706SLuiz Augusto von Dentz 		    !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4862d0b13706SLuiz Augusto von Dentz 		    !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4863d0b13706SLuiz Augusto von Dentz 			ret = -EADDRNOTAVAIL;
4864d0b13706SLuiz Augusto von Dentz 			goto done;
4865d0b13706SLuiz Augusto von Dentz 		}
4866d0b13706SLuiz Augusto von Dentz 	}
4867d0b13706SLuiz Augusto von Dentz 
4868d0b13706SLuiz Augusto von Dentz 	if (test_bit(HCI_UP, &hdev->flags)) {
4869d0b13706SLuiz Augusto von Dentz 		ret = -EALREADY;
4870d0b13706SLuiz Augusto von Dentz 		goto done;
4871d0b13706SLuiz Augusto von Dentz 	}
4872d0b13706SLuiz Augusto von Dentz 
4873d0b13706SLuiz Augusto von Dentz 	if (hdev->open(hdev)) {
4874d0b13706SLuiz Augusto von Dentz 		ret = -EIO;
4875d0b13706SLuiz Augusto von Dentz 		goto done;
4876d0b13706SLuiz Augusto von Dentz 	}
4877d0b13706SLuiz Augusto von Dentz 
48789695ef87SAbhishek Pandit-Subedi 	hci_devcd_reset(hdev);
48799695ef87SAbhishek Pandit-Subedi 
4880d0b13706SLuiz Augusto von Dentz 	set_bit(HCI_RUNNING, &hdev->flags);
4881d0b13706SLuiz Augusto von Dentz 	hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4882d0b13706SLuiz Augusto von Dentz 
48831bbf4023SLuiz Augusto von Dentz 	ret = hci_dev_init_sync(hdev);
4884d0b13706SLuiz Augusto von Dentz 	if (!ret) {
4885d0b13706SLuiz Augusto von Dentz 		hci_dev_hold(hdev);
4886d0b13706SLuiz Augusto von Dentz 		hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4887d0b13706SLuiz Augusto von Dentz 		hci_adv_instances_set_rpa_expired(hdev, true);
4888d0b13706SLuiz Augusto von Dentz 		set_bit(HCI_UP, &hdev->flags);
4889d0b13706SLuiz Augusto von Dentz 		hci_sock_dev_event(hdev, HCI_DEV_UP);
4890d0b13706SLuiz Augusto von Dentz 		hci_leds_update_powered(hdev, true);
4891d0b13706SLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4892d0b13706SLuiz Augusto von Dentz 		    !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4893d0b13706SLuiz Augusto von Dentz 		    !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4894d0b13706SLuiz Augusto von Dentz 		    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4895d0b13706SLuiz Augusto von Dentz 		    hci_dev_test_flag(hdev, HCI_MGMT) &&
4896d0b13706SLuiz Augusto von Dentz 		    hdev->dev_type == HCI_PRIMARY) {
4897d0b13706SLuiz Augusto von Dentz 			ret = hci_powered_update_sync(hdev);
48986abf0daeSLuiz Augusto von Dentz 			mgmt_power_on(hdev, ret);
4899d0b13706SLuiz Augusto von Dentz 		}
4900d0b13706SLuiz Augusto von Dentz 	} else {
4901d0b13706SLuiz Augusto von Dentz 		/* Init failed, cleanup */
4902d0b13706SLuiz Augusto von Dentz 		flush_work(&hdev->tx_work);
4903d0b13706SLuiz Augusto von Dentz 
4904d0b13706SLuiz Augusto von Dentz 		/* Since hci_rx_work() is possible to awake new cmd_work
4905d0b13706SLuiz Augusto von Dentz 		 * it should be flushed first to avoid unexpected call of
4906d0b13706SLuiz Augusto von Dentz 		 * hci_cmd_work()
4907d0b13706SLuiz Augusto von Dentz 		 */
4908d0b13706SLuiz Augusto von Dentz 		flush_work(&hdev->rx_work);
4909d0b13706SLuiz Augusto von Dentz 		flush_work(&hdev->cmd_work);
4910d0b13706SLuiz Augusto von Dentz 
4911d0b13706SLuiz Augusto von Dentz 		skb_queue_purge(&hdev->cmd_q);
4912d0b13706SLuiz Augusto von Dentz 		skb_queue_purge(&hdev->rx_q);
4913d0b13706SLuiz Augusto von Dentz 
4914d0b13706SLuiz Augusto von Dentz 		if (hdev->flush)
4915d0b13706SLuiz Augusto von Dentz 			hdev->flush(hdev);
4916d0b13706SLuiz Augusto von Dentz 
4917d0b13706SLuiz Augusto von Dentz 		if (hdev->sent_cmd) {
491897dfaf07SArchie Pusaka 			cancel_delayed_work_sync(&hdev->cmd_timer);
4919d0b13706SLuiz Augusto von Dentz 			kfree_skb(hdev->sent_cmd);
4920d0b13706SLuiz Augusto von Dentz 			hdev->sent_cmd = NULL;
4921d0b13706SLuiz Augusto von Dentz 		}
4922d0b13706SLuiz Augusto von Dentz 
4923d0b13706SLuiz Augusto von Dentz 		clear_bit(HCI_RUNNING, &hdev->flags);
4924d0b13706SLuiz Augusto von Dentz 		hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4925d0b13706SLuiz Augusto von Dentz 
4926d0b13706SLuiz Augusto von Dentz 		hdev->close(hdev);
4927d0b13706SLuiz Augusto von Dentz 		hdev->flags &= BIT(HCI_RAW);
4928d0b13706SLuiz Augusto von Dentz 	}
4929d0b13706SLuiz Augusto von Dentz 
4930d0b13706SLuiz Augusto von Dentz done:
4931d0b13706SLuiz Augusto von Dentz 	return ret;
4932d0b13706SLuiz Augusto von Dentz }
4933d0b13706SLuiz Augusto von Dentz 
4934d0b13706SLuiz Augusto von Dentz /* This function requires the caller holds hdev->lock */
4935d0b13706SLuiz Augusto von Dentz static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4936d0b13706SLuiz Augusto von Dentz {
4937d0b13706SLuiz Augusto von Dentz 	struct hci_conn_params *p;
4938d0b13706SLuiz Augusto von Dentz 
4939d0b13706SLuiz Augusto von Dentz 	list_for_each_entry(p, &hdev->le_conn_params, list) {
4940195ef75eSPauli Virtanen 		hci_pend_le_list_del_init(p);
4941d0b13706SLuiz Augusto von Dentz 		if (p->conn) {
4942d0b13706SLuiz Augusto von Dentz 			hci_conn_drop(p->conn);
4943d0b13706SLuiz Augusto von Dentz 			hci_conn_put(p->conn);
4944d0b13706SLuiz Augusto von Dentz 			p->conn = NULL;
4945d0b13706SLuiz Augusto von Dentz 		}
4946d0b13706SLuiz Augusto von Dentz 	}
4947d0b13706SLuiz Augusto von Dentz 
4948d0b13706SLuiz Augusto von Dentz 	BT_DBG("All LE pending actions cleared");
4949d0b13706SLuiz Augusto von Dentz }
4950d0b13706SLuiz Augusto von Dentz 
49518dbc3e75SAbhishek Pandit-Subedi static int hci_dev_shutdown(struct hci_dev *hdev)
49528dbc3e75SAbhishek Pandit-Subedi {
49538dbc3e75SAbhishek Pandit-Subedi 	int err = 0;
49548dbc3e75SAbhishek Pandit-Subedi 	/* Similar to how we first do setup and then set the exclusive access
49558dbc3e75SAbhishek Pandit-Subedi 	 * bit for userspace, we must first unset userchannel and then clean up.
49568dbc3e75SAbhishek Pandit-Subedi 	 * Otherwise, the kernel can't properly use the hci channel to clean up
49578dbc3e75SAbhishek Pandit-Subedi 	 * the controller (some shutdown routines require sending additional
49588dbc3e75SAbhishek Pandit-Subedi 	 * commands to the controller for example).
49598dbc3e75SAbhishek Pandit-Subedi 	 */
49608dbc3e75SAbhishek Pandit-Subedi 	bool was_userchannel =
49618dbc3e75SAbhishek Pandit-Subedi 		hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
49628dbc3e75SAbhishek Pandit-Subedi 
49638dbc3e75SAbhishek Pandit-Subedi 	if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
49648dbc3e75SAbhishek Pandit-Subedi 	    test_bit(HCI_UP, &hdev->flags)) {
49658dbc3e75SAbhishek Pandit-Subedi 		/* Execute vendor specific shutdown routine */
49668dbc3e75SAbhishek Pandit-Subedi 		if (hdev->shutdown)
49678dbc3e75SAbhishek Pandit-Subedi 			err = hdev->shutdown(hdev);
49688dbc3e75SAbhishek Pandit-Subedi 	}
49698dbc3e75SAbhishek Pandit-Subedi 
49708dbc3e75SAbhishek Pandit-Subedi 	if (was_userchannel)
49718dbc3e75SAbhishek Pandit-Subedi 		hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
49728dbc3e75SAbhishek Pandit-Subedi 
49738dbc3e75SAbhishek Pandit-Subedi 	return err;
49748dbc3e75SAbhishek Pandit-Subedi }
49758dbc3e75SAbhishek Pandit-Subedi 
4976d0b13706SLuiz Augusto von Dentz int hci_dev_close_sync(struct hci_dev *hdev)
4977d0b13706SLuiz Augusto von Dentz {
4978d0b13706SLuiz Augusto von Dentz 	bool auto_off;
4979d0b13706SLuiz Augusto von Dentz 	int err = 0;
4980d0b13706SLuiz Augusto von Dentz 
4981d0b13706SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
4982d0b13706SLuiz Augusto von Dentz 
4983d0b13706SLuiz Augusto von Dentz 	cancel_delayed_work(&hdev->power_off);
4984d0b13706SLuiz Augusto von Dentz 	cancel_delayed_work(&hdev->ncmd_timer);
49858ffde2a7SBrian Gix 	cancel_delayed_work(&hdev->le_scan_disable);
498627d54b77SBrian Gix 	cancel_delayed_work(&hdev->le_scan_restart);
4987d0b13706SLuiz Augusto von Dentz 
4988d0b13706SLuiz Augusto von Dentz 	hci_request_cancel_all(hdev);
4989d0b13706SLuiz Augusto von Dentz 
4990c249ea9bSBrian Gix 	if (hdev->adv_instance_timeout) {
4991c249ea9bSBrian Gix 		cancel_delayed_work_sync(&hdev->adv_instance_expire);
4992c249ea9bSBrian Gix 		hdev->adv_instance_timeout = 0;
4993c249ea9bSBrian Gix 	}
4994c249ea9bSBrian Gix 
49958dbc3e75SAbhishek Pandit-Subedi 	err = hci_dev_shutdown(hdev);
4996d0b13706SLuiz Augusto von Dentz 
4997d0b13706SLuiz Augusto von Dentz 	if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4998d0b13706SLuiz Augusto von Dentz 		cancel_delayed_work_sync(&hdev->cmd_timer);
4999d0b13706SLuiz Augusto von Dentz 		return err;
5000d0b13706SLuiz Augusto von Dentz 	}
5001d0b13706SLuiz Augusto von Dentz 
5002d0b13706SLuiz Augusto von Dentz 	hci_leds_update_powered(hdev, false);
5003d0b13706SLuiz Augusto von Dentz 
5004d0b13706SLuiz Augusto von Dentz 	/* Flush RX and TX works */
5005d0b13706SLuiz Augusto von Dentz 	flush_work(&hdev->tx_work);
5006d0b13706SLuiz Augusto von Dentz 	flush_work(&hdev->rx_work);
5007d0b13706SLuiz Augusto von Dentz 
5008d0b13706SLuiz Augusto von Dentz 	if (hdev->discov_timeout > 0) {
5009d0b13706SLuiz Augusto von Dentz 		hdev->discov_timeout = 0;
5010d0b13706SLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5011d0b13706SLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
5012d0b13706SLuiz Augusto von Dentz 	}
5013d0b13706SLuiz Augusto von Dentz 
5014d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
5015d0b13706SLuiz Augusto von Dentz 		cancel_delayed_work(&hdev->service_cache);
5016d0b13706SLuiz Augusto von Dentz 
5017d0b13706SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_MGMT)) {
5018d0b13706SLuiz Augusto von Dentz 		struct adv_info *adv_instance;
5019d0b13706SLuiz Augusto von Dentz 
5020d0b13706SLuiz Augusto von Dentz 		cancel_delayed_work_sync(&hdev->rpa_expired);
5021d0b13706SLuiz Augusto von Dentz 
5022d0b13706SLuiz Augusto von Dentz 		list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5023d0b13706SLuiz Augusto von Dentz 			cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
5024d0b13706SLuiz Augusto von Dentz 	}
5025d0b13706SLuiz Augusto von Dentz 
5026d0b13706SLuiz Augusto von Dentz 	/* Avoid potential lockdep warnings from the *_flush() calls by
5027d0b13706SLuiz Augusto von Dentz 	 * ensuring the workqueue is empty up front.
5028d0b13706SLuiz Augusto von Dentz 	 */
5029d0b13706SLuiz Augusto von Dentz 	drain_workqueue(hdev->workqueue);
5030d0b13706SLuiz Augusto von Dentz 
5031d0b13706SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
5032d0b13706SLuiz Augusto von Dentz 
5033d0b13706SLuiz Augusto von Dentz 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5034d0b13706SLuiz Augusto von Dentz 
5035d0b13706SLuiz Augusto von Dentz 	auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5036d0b13706SLuiz Augusto von Dentz 
5037d0b13706SLuiz Augusto von Dentz 	if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
5038d0b13706SLuiz Augusto von Dentz 	    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5039d0b13706SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_MGMT))
5040d0b13706SLuiz Augusto von Dentz 		__mgmt_power_off(hdev);
5041d0b13706SLuiz Augusto von Dentz 
5042d0b13706SLuiz Augusto von Dentz 	hci_inquiry_cache_flush(hdev);
5043d0b13706SLuiz Augusto von Dentz 	hci_pend_le_actions_clear(hdev);
5044d0b13706SLuiz Augusto von Dentz 	hci_conn_hash_flush(hdev);
5045fa78d2d1SLin Ma 	/* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5046d0b13706SLuiz Augusto von Dentz 	smp_unregister(hdev);
5047fa78d2d1SLin Ma 	hci_dev_unlock(hdev);
5048d0b13706SLuiz Augusto von Dentz 
5049d0b13706SLuiz Augusto von Dentz 	hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5050d0b13706SLuiz Augusto von Dentz 
5051385315deSJesse Melhuish 	if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5052d0b13706SLuiz Augusto von Dentz 		aosp_do_close(hdev);
5053d0b13706SLuiz Augusto von Dentz 		msft_do_close(hdev);
5054385315deSJesse Melhuish 	}
5055d0b13706SLuiz Augusto von Dentz 
5056d0b13706SLuiz Augusto von Dentz 	if (hdev->flush)
5057d0b13706SLuiz Augusto von Dentz 		hdev->flush(hdev);
5058d0b13706SLuiz Augusto von Dentz 
5059d0b13706SLuiz Augusto von Dentz 	/* Reset device */
5060d0b13706SLuiz Augusto von Dentz 	skb_queue_purge(&hdev->cmd_q);
5061d0b13706SLuiz Augusto von Dentz 	atomic_set(&hdev->cmd_cnt, 1);
5062d0b13706SLuiz Augusto von Dentz 	if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5063d0b13706SLuiz Augusto von Dentz 	    !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5064d0b13706SLuiz Augusto von Dentz 		set_bit(HCI_INIT, &hdev->flags);
5065d0b13706SLuiz Augusto von Dentz 		hci_reset_sync(hdev);
5066d0b13706SLuiz Augusto von Dentz 		clear_bit(HCI_INIT, &hdev->flags);
5067d0b13706SLuiz Augusto von Dentz 	}
5068d0b13706SLuiz Augusto von Dentz 
5069d0b13706SLuiz Augusto von Dentz 	/* flush cmd  work */
5070d0b13706SLuiz Augusto von Dentz 	flush_work(&hdev->cmd_work);
5071d0b13706SLuiz Augusto von Dentz 
5072d0b13706SLuiz Augusto von Dentz 	/* Drop queues */
5073d0b13706SLuiz Augusto von Dentz 	skb_queue_purge(&hdev->rx_q);
5074d0b13706SLuiz Augusto von Dentz 	skb_queue_purge(&hdev->cmd_q);
5075d0b13706SLuiz Augusto von Dentz 	skb_queue_purge(&hdev->raw_q);
5076d0b13706SLuiz Augusto von Dentz 
5077d0b13706SLuiz Augusto von Dentz 	/* Drop last sent command */
5078d0b13706SLuiz Augusto von Dentz 	if (hdev->sent_cmd) {
5079d0b13706SLuiz Augusto von Dentz 		cancel_delayed_work_sync(&hdev->cmd_timer);
5080d0b13706SLuiz Augusto von Dentz 		kfree_skb(hdev->sent_cmd);
5081d0b13706SLuiz Augusto von Dentz 		hdev->sent_cmd = NULL;
5082d0b13706SLuiz Augusto von Dentz 	}
5083d0b13706SLuiz Augusto von Dentz 
5084d0b13706SLuiz Augusto von Dentz 	clear_bit(HCI_RUNNING, &hdev->flags);
5085d0b13706SLuiz Augusto von Dentz 	hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5086d0b13706SLuiz Augusto von Dentz 
5087d0b13706SLuiz Augusto von Dentz 	/* After this point our queues are empty and no tasks are scheduled. */
5088d0b13706SLuiz Augusto von Dentz 	hdev->close(hdev);
5089d0b13706SLuiz Augusto von Dentz 
5090d0b13706SLuiz Augusto von Dentz 	/* Clear flags */
5091d0b13706SLuiz Augusto von Dentz 	hdev->flags &= BIT(HCI_RAW);
5092d0b13706SLuiz Augusto von Dentz 	hci_dev_clear_volatile_flags(hdev);
5093d0b13706SLuiz Augusto von Dentz 
5094d0b13706SLuiz Augusto von Dentz 	/* Controller radio is available but is currently powered down */
5095d0b13706SLuiz Augusto von Dentz 	hdev->amp_status = AMP_STATUS_POWERED_DOWN;
5096d0b13706SLuiz Augusto von Dentz 
5097d0b13706SLuiz Augusto von Dentz 	memset(hdev->eir, 0, sizeof(hdev->eir));
5098d0b13706SLuiz Augusto von Dentz 	memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5099d0b13706SLuiz Augusto von Dentz 	bacpy(&hdev->random_addr, BDADDR_ANY);
5100d0b13706SLuiz Augusto von Dentz 
5101d0b13706SLuiz Augusto von Dentz 	hci_dev_put(hdev);
5102d0b13706SLuiz Augusto von Dentz 	return err;
5103d0b13706SLuiz Augusto von Dentz }
5104d0b13706SLuiz Augusto von Dentz 
5105cf75ad8bSLuiz Augusto von Dentz /* This function perform power on HCI command sequence as follows:
5106cf75ad8bSLuiz Augusto von Dentz  *
5107cf75ad8bSLuiz Augusto von Dentz  * If controller is already up (HCI_UP) performs hci_powered_update_sync
5108cf75ad8bSLuiz Augusto von Dentz  * sequence otherwise run hci_dev_open_sync which will follow with
5109cf75ad8bSLuiz Augusto von Dentz  * hci_powered_update_sync after the init sequence is completed.
5110cf75ad8bSLuiz Augusto von Dentz  */
5111cf75ad8bSLuiz Augusto von Dentz static int hci_power_on_sync(struct hci_dev *hdev)
5112cf75ad8bSLuiz Augusto von Dentz {
5113cf75ad8bSLuiz Augusto von Dentz 	int err;
5114cf75ad8bSLuiz Augusto von Dentz 
5115cf75ad8bSLuiz Augusto von Dentz 	if (test_bit(HCI_UP, &hdev->flags) &&
5116cf75ad8bSLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_MGMT) &&
5117cf75ad8bSLuiz Augusto von Dentz 	    hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5118cf75ad8bSLuiz Augusto von Dentz 		cancel_delayed_work(&hdev->power_off);
5119cf75ad8bSLuiz Augusto von Dentz 		return hci_powered_update_sync(hdev);
5120cf75ad8bSLuiz Augusto von Dentz 	}
5121cf75ad8bSLuiz Augusto von Dentz 
5122cf75ad8bSLuiz Augusto von Dentz 	err = hci_dev_open_sync(hdev);
5123cf75ad8bSLuiz Augusto von Dentz 	if (err < 0)
5124cf75ad8bSLuiz Augusto von Dentz 		return err;
5125cf75ad8bSLuiz Augusto von Dentz 
5126cf75ad8bSLuiz Augusto von Dentz 	/* During the HCI setup phase, a few error conditions are
5127cf75ad8bSLuiz Augusto von Dentz 	 * ignored and they need to be checked now. If they are still
5128cf75ad8bSLuiz Augusto von Dentz 	 * valid, it is important to return the device back off.
5129cf75ad8bSLuiz Augusto von Dentz 	 */
5130cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5131cf75ad8bSLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5132cf75ad8bSLuiz Augusto von Dentz 	    (hdev->dev_type == HCI_PRIMARY &&
5133cf75ad8bSLuiz Augusto von Dentz 	     !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5134cf75ad8bSLuiz Augusto von Dentz 	     !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5135cf75ad8bSLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5136cf75ad8bSLuiz Augusto von Dentz 		hci_dev_close_sync(hdev);
5137cf75ad8bSLuiz Augusto von Dentz 	} else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5138cf75ad8bSLuiz Augusto von Dentz 		queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5139cf75ad8bSLuiz Augusto von Dentz 				   HCI_AUTO_OFF_TIMEOUT);
5140cf75ad8bSLuiz Augusto von Dentz 	}
5141cf75ad8bSLuiz Augusto von Dentz 
5142cf75ad8bSLuiz Augusto von Dentz 	if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5143cf75ad8bSLuiz Augusto von Dentz 		/* For unconfigured devices, set the HCI_RAW flag
5144cf75ad8bSLuiz Augusto von Dentz 		 * so that userspace can easily identify them.
5145cf75ad8bSLuiz Augusto von Dentz 		 */
5146cf75ad8bSLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5147cf75ad8bSLuiz Augusto von Dentz 			set_bit(HCI_RAW, &hdev->flags);
5148cf75ad8bSLuiz Augusto von Dentz 
5149cf75ad8bSLuiz Augusto von Dentz 		/* For fully configured devices, this will send
5150cf75ad8bSLuiz Augusto von Dentz 		 * the Index Added event. For unconfigured devices,
5151cf75ad8bSLuiz Augusto von Dentz 		 * it will send Unconfigued Index Added event.
5152cf75ad8bSLuiz Augusto von Dentz 		 *
5153cf75ad8bSLuiz Augusto von Dentz 		 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
5154cf75ad8bSLuiz Augusto von Dentz 		 * and no event will be send.
5155cf75ad8bSLuiz Augusto von Dentz 		 */
5156cf75ad8bSLuiz Augusto von Dentz 		mgmt_index_added(hdev);
5157cf75ad8bSLuiz Augusto von Dentz 	} else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5158cf75ad8bSLuiz Augusto von Dentz 		/* When the controller is now configured, then it
5159cf75ad8bSLuiz Augusto von Dentz 		 * is important to clear the HCI_RAW flag.
5160cf75ad8bSLuiz Augusto von Dentz 		 */
5161cf75ad8bSLuiz Augusto von Dentz 		if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5162cf75ad8bSLuiz Augusto von Dentz 			clear_bit(HCI_RAW, &hdev->flags);
5163cf75ad8bSLuiz Augusto von Dentz 
5164cf75ad8bSLuiz Augusto von Dentz 		/* Powering on the controller with HCI_CONFIG set only
5165cf75ad8bSLuiz Augusto von Dentz 		 * happens with the transition from unconfigured to
5166cf75ad8bSLuiz Augusto von Dentz 		 * configured. This will send the Index Added event.
5167cf75ad8bSLuiz Augusto von Dentz 		 */
5168cf75ad8bSLuiz Augusto von Dentz 		mgmt_index_added(hdev);
5169cf75ad8bSLuiz Augusto von Dentz 	}
5170cf75ad8bSLuiz Augusto von Dentz 
5171cf75ad8bSLuiz Augusto von Dentz 	return 0;
5172cf75ad8bSLuiz Augusto von Dentz }
5173cf75ad8bSLuiz Augusto von Dentz 
5174cf75ad8bSLuiz Augusto von Dentz static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5175cf75ad8bSLuiz Augusto von Dentz {
5176cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_remote_name_req_cancel cp;
5177cf75ad8bSLuiz Augusto von Dentz 
5178cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
5179cf75ad8bSLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, addr);
5180cf75ad8bSLuiz Augusto von Dentz 
5181cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5182cf75ad8bSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5183cf75ad8bSLuiz Augusto von Dentz }
5184cf75ad8bSLuiz Augusto von Dentz 
5185abfeea47SLuiz Augusto von Dentz int hci_stop_discovery_sync(struct hci_dev *hdev)
5186cf75ad8bSLuiz Augusto von Dentz {
5187cf75ad8bSLuiz Augusto von Dentz 	struct discovery_state *d = &hdev->discovery;
5188cf75ad8bSLuiz Augusto von Dentz 	struct inquiry_entry *e;
5189cf75ad8bSLuiz Augusto von Dentz 	int err;
5190cf75ad8bSLuiz Augusto von Dentz 
5191cf75ad8bSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5192cf75ad8bSLuiz Augusto von Dentz 
5193cf75ad8bSLuiz Augusto von Dentz 	if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
5194cf75ad8bSLuiz Augusto von Dentz 		if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5195cf75ad8bSLuiz Augusto von Dentz 			err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5196cf75ad8bSLuiz Augusto von Dentz 						    0, NULL, HCI_CMD_TIMEOUT);
5197cf75ad8bSLuiz Augusto von Dentz 			if (err)
5198cf75ad8bSLuiz Augusto von Dentz 				return err;
5199cf75ad8bSLuiz Augusto von Dentz 		}
5200cf75ad8bSLuiz Augusto von Dentz 
5201cf75ad8bSLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5202cf75ad8bSLuiz Augusto von Dentz 			cancel_delayed_work(&hdev->le_scan_disable);
5203cf75ad8bSLuiz Augusto von Dentz 			cancel_delayed_work(&hdev->le_scan_restart);
5204cf75ad8bSLuiz Augusto von Dentz 
5205cf75ad8bSLuiz Augusto von Dentz 			err = hci_scan_disable_sync(hdev);
5206cf75ad8bSLuiz Augusto von Dentz 			if (err)
5207cf75ad8bSLuiz Augusto von Dentz 				return err;
5208cf75ad8bSLuiz Augusto von Dentz 		}
5209cf75ad8bSLuiz Augusto von Dentz 
5210cf75ad8bSLuiz Augusto von Dentz 	} else {
5211cf75ad8bSLuiz Augusto von Dentz 		err = hci_scan_disable_sync(hdev);
5212cf75ad8bSLuiz Augusto von Dentz 		if (err)
5213cf75ad8bSLuiz Augusto von Dentz 			return err;
5214cf75ad8bSLuiz Augusto von Dentz 	}
5215cf75ad8bSLuiz Augusto von Dentz 
5216abfeea47SLuiz Augusto von Dentz 	/* Resume advertising if it was paused */
5217abfeea47SLuiz Augusto von Dentz 	if (use_ll_privacy(hdev))
5218abfeea47SLuiz Augusto von Dentz 		hci_resume_advertising_sync(hdev);
5219abfeea47SLuiz Augusto von Dentz 
5220cf75ad8bSLuiz Augusto von Dentz 	/* No further actions needed for LE-only discovery */
5221cf75ad8bSLuiz Augusto von Dentz 	if (d->type == DISCOV_TYPE_LE)
5222cf75ad8bSLuiz Augusto von Dentz 		return 0;
5223cf75ad8bSLuiz Augusto von Dentz 
5224cf75ad8bSLuiz Augusto von Dentz 	if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5225cf75ad8bSLuiz Augusto von Dentz 		e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5226cf75ad8bSLuiz Augusto von Dentz 						     NAME_PENDING);
5227cf75ad8bSLuiz Augusto von Dentz 		if (!e)
5228cf75ad8bSLuiz Augusto von Dentz 			return 0;
5229cf75ad8bSLuiz Augusto von Dentz 
5230cf75ad8bSLuiz Augusto von Dentz 		return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5231cf75ad8bSLuiz Augusto von Dentz 	}
5232cf75ad8bSLuiz Augusto von Dentz 
5233cf75ad8bSLuiz Augusto von Dentz 	return 0;
5234cf75ad8bSLuiz Augusto von Dentz }
5235cf75ad8bSLuiz Augusto von Dentz 
5236cf75ad8bSLuiz Augusto von Dentz static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
5237cf75ad8bSLuiz Augusto von Dentz 					u8 reason)
5238cf75ad8bSLuiz Augusto von Dentz {
5239cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_disconn_phy_link cp;
5240cf75ad8bSLuiz Augusto von Dentz 
5241cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
5242cf75ad8bSLuiz Augusto von Dentz 	cp.phy_handle = HCI_PHY_HANDLE(handle);
5243cf75ad8bSLuiz Augusto von Dentz 	cp.reason = reason;
5244cf75ad8bSLuiz Augusto von Dentz 
5245cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
5246cf75ad8bSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5247cf75ad8bSLuiz Augusto von Dentz }
5248cf75ad8bSLuiz Augusto von Dentz 
5249cf75ad8bSLuiz Augusto von Dentz static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5250cf75ad8bSLuiz Augusto von Dentz 			       u8 reason)
5251cf75ad8bSLuiz Augusto von Dentz {
5252cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_disconnect cp;
5253cf75ad8bSLuiz Augusto von Dentz 
5254cf75ad8bSLuiz Augusto von Dentz 	if (conn->type == AMP_LINK)
5255cf75ad8bSLuiz Augusto von Dentz 		return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
5256cf75ad8bSLuiz Augusto von Dentz 
5257cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
5258cf75ad8bSLuiz Augusto von Dentz 	cp.handle = cpu_to_le16(conn->handle);
5259cf75ad8bSLuiz Augusto von Dentz 	cp.reason = reason;
5260cf75ad8bSLuiz Augusto von Dentz 
5261f5d13029SArchie Pusaka 	/* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5262f5d13029SArchie Pusaka 	 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5263f5d13029SArchie Pusaka 	 * used when suspending or powering off, where we don't want to wait
5264f5d13029SArchie Pusaka 	 * for the peer's response.
5265d0b13706SLuiz Augusto von Dentz 	 */
5266f5d13029SArchie Pusaka 	if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5267d0b13706SLuiz Augusto von Dentz 		return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5268d0b13706SLuiz Augusto von Dentz 						sizeof(cp), &cp,
5269d0b13706SLuiz Augusto von Dentz 						HCI_EV_DISCONN_COMPLETE,
5270d0b13706SLuiz Augusto von Dentz 						HCI_CMD_TIMEOUT, NULL);
5271d0b13706SLuiz Augusto von Dentz 
5272d0b13706SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5273d0b13706SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
5274cf75ad8bSLuiz Augusto von Dentz }
5275cf75ad8bSLuiz Augusto von Dentz 
5276cf75ad8bSLuiz Augusto von Dentz static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5277a13f316eSLuiz Augusto von Dentz 				      struct hci_conn *conn, u8 reason)
5278cf75ad8bSLuiz Augusto von Dentz {
5279a13f316eSLuiz Augusto von Dentz 	/* Return reason if scanning since the connection shall probably be
5280a13f316eSLuiz Augusto von Dentz 	 * cleanup directly.
5281a13f316eSLuiz Augusto von Dentz 	 */
5282cf75ad8bSLuiz Augusto von Dentz 	if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5283a13f316eSLuiz Augusto von Dentz 		return reason;
5284cf75ad8bSLuiz Augusto von Dentz 
5285a13f316eSLuiz Augusto von Dentz 	if (conn->role == HCI_ROLE_SLAVE ||
5286a13f316eSLuiz Augusto von Dentz 	    test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
5287b62e7220SLuiz Augusto von Dentz 		return 0;
5288b62e7220SLuiz Augusto von Dentz 
5289cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5290b62e7220SLuiz Augusto von Dentz 				     0, NULL, HCI_CMD_TIMEOUT);
5291cf75ad8bSLuiz Augusto von Dentz }
5292cf75ad8bSLuiz Augusto von Dentz 
5293a13f316eSLuiz Augusto von Dentz static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5294a13f316eSLuiz Augusto von Dentz 				   u8 reason)
5295cf75ad8bSLuiz Augusto von Dentz {
5296cf75ad8bSLuiz Augusto von Dentz 	if (conn->type == LE_LINK)
5297a13f316eSLuiz Augusto von Dentz 		return hci_le_connect_cancel_sync(hdev, conn, reason);
5298cf75ad8bSLuiz Augusto von Dentz 
529904a51d61SLuiz Augusto von Dentz 	if (conn->type == ISO_LINK) {
530004a51d61SLuiz Augusto von Dentz 		/* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
530104a51d61SLuiz Augusto von Dentz 		 * page 1857:
530204a51d61SLuiz Augusto von Dentz 		 *
530304a51d61SLuiz Augusto von Dentz 		 * If this command is issued for a CIS on the Central and the
530404a51d61SLuiz Augusto von Dentz 		 * CIS is successfully terminated before being established,
530504a51d61SLuiz Augusto von Dentz 		 * then an HCI_LE_CIS_Established event shall also be sent for
530604a51d61SLuiz Augusto von Dentz 		 * this CIS with the Status Operation Cancelled by Host (0x44).
530704a51d61SLuiz Augusto von Dentz 		 */
530804a51d61SLuiz Augusto von Dentz 		if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
530904a51d61SLuiz Augusto von Dentz 			return hci_disconnect_sync(hdev, conn, reason);
531004a51d61SLuiz Augusto von Dentz 
53112889bdd0SPauli Virtanen 		/* CIS with no Create CIS sent have nothing to cancel */
53122889bdd0SPauli Virtanen 		if (bacmp(&conn->dst, BDADDR_ANY))
53132889bdd0SPauli Virtanen 			return HCI_ERROR_LOCAL_HOST_TERM;
53142889bdd0SPauli Virtanen 
531504a51d61SLuiz Augusto von Dentz 		/* There is no way to cancel a BIS without terminating the BIG
531604a51d61SLuiz Augusto von Dentz 		 * which is done later on connection cleanup.
531704a51d61SLuiz Augusto von Dentz 		 */
531804a51d61SLuiz Augusto von Dentz 		return 0;
531904a51d61SLuiz Augusto von Dentz 	}
532004a51d61SLuiz Augusto von Dentz 
5321cf75ad8bSLuiz Augusto von Dentz 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5322cf75ad8bSLuiz Augusto von Dentz 		return 0;
5323cf75ad8bSLuiz Augusto von Dentz 
5324094e3639SLuiz Augusto von Dentz 	/* Wait for HCI_EV_CONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5325094e3639SLuiz Augusto von Dentz 	 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5326094e3639SLuiz Augusto von Dentz 	 * used when suspending or powering off, where we don't want to wait
5327094e3639SLuiz Augusto von Dentz 	 * for the peer's response.
5328094e3639SLuiz Augusto von Dentz 	 */
5329094e3639SLuiz Augusto von Dentz 	if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5330094e3639SLuiz Augusto von Dentz 		return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5331094e3639SLuiz Augusto von Dentz 						6, &conn->dst,
5332094e3639SLuiz Augusto von Dentz 						HCI_EV_CONN_COMPLETE,
5333094e3639SLuiz Augusto von Dentz 						HCI_CMD_TIMEOUT, NULL);
5334094e3639SLuiz Augusto von Dentz 
5335cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5336cf75ad8bSLuiz Augusto von Dentz 				     6, &conn->dst, HCI_CMD_TIMEOUT);
5337cf75ad8bSLuiz Augusto von Dentz }
5338cf75ad8bSLuiz Augusto von Dentz 
5339cf75ad8bSLuiz Augusto von Dentz static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5340cf75ad8bSLuiz Augusto von Dentz 			       u8 reason)
5341cf75ad8bSLuiz Augusto von Dentz {
5342cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_reject_sync_conn_req cp;
5343cf75ad8bSLuiz Augusto von Dentz 
5344cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
5345cf75ad8bSLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, &conn->dst);
5346cf75ad8bSLuiz Augusto von Dentz 	cp.reason = reason;
5347cf75ad8bSLuiz Augusto von Dentz 
5348cf75ad8bSLuiz Augusto von Dentz 	/* SCO rejection has its own limited set of
5349cf75ad8bSLuiz Augusto von Dentz 	 * allowed error values (0x0D-0x0F).
5350cf75ad8bSLuiz Augusto von Dentz 	 */
5351cf75ad8bSLuiz Augusto von Dentz 	if (reason < 0x0d || reason > 0x0f)
5352cf75ad8bSLuiz Augusto von Dentz 		cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5353cf75ad8bSLuiz Augusto von Dentz 
5354cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5355cf75ad8bSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5356cf75ad8bSLuiz Augusto von Dentz }
5357cf75ad8bSLuiz Augusto von Dentz 
535804a51d61SLuiz Augusto von Dentz static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
535904a51d61SLuiz Augusto von Dentz 				  u8 reason)
536004a51d61SLuiz Augusto von Dentz {
536104a51d61SLuiz Augusto von Dentz 	struct hci_cp_le_reject_cis cp;
536204a51d61SLuiz Augusto von Dentz 
536304a51d61SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
536404a51d61SLuiz Augusto von Dentz 	cp.handle = cpu_to_le16(conn->handle);
536504a51d61SLuiz Augusto von Dentz 	cp.reason = reason;
536604a51d61SLuiz Augusto von Dentz 
536704a51d61SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
536804a51d61SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
536904a51d61SLuiz Augusto von Dentz }
537004a51d61SLuiz Augusto von Dentz 
5371cf75ad8bSLuiz Augusto von Dentz static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5372cf75ad8bSLuiz Augusto von Dentz 				u8 reason)
5373cf75ad8bSLuiz Augusto von Dentz {
5374cf75ad8bSLuiz Augusto von Dentz 	struct hci_cp_reject_conn_req cp;
5375cf75ad8bSLuiz Augusto von Dentz 
537604a51d61SLuiz Augusto von Dentz 	if (conn->type == ISO_LINK)
537704a51d61SLuiz Augusto von Dentz 		return hci_le_reject_cis_sync(hdev, conn, reason);
537804a51d61SLuiz Augusto von Dentz 
5379cf75ad8bSLuiz Augusto von Dentz 	if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5380cf75ad8bSLuiz Augusto von Dentz 		return hci_reject_sco_sync(hdev, conn, reason);
5381cf75ad8bSLuiz Augusto von Dentz 
5382cf75ad8bSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
5383cf75ad8bSLuiz Augusto von Dentz 	bacpy(&cp.bdaddr, &conn->dst);
5384cf75ad8bSLuiz Augusto von Dentz 	cp.reason = reason;
5385cf75ad8bSLuiz Augusto von Dentz 
5386cf75ad8bSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5387cf75ad8bSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5388cf75ad8bSLuiz Augusto von Dentz }
5389cf75ad8bSLuiz Augusto von Dentz 
53901f7435c8SLuiz Augusto von Dentz int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5391cf75ad8bSLuiz Augusto von Dentz {
5392*5af1f84eSLuiz Augusto von Dentz 	int err = 0;
5393*5af1f84eSLuiz Augusto von Dentz 	u16 handle = conn->handle;
53949b3628d7SLuiz Augusto von Dentz 
5395cf75ad8bSLuiz Augusto von Dentz 	switch (conn->state) {
5396cf75ad8bSLuiz Augusto von Dentz 	case BT_CONNECTED:
5397cf75ad8bSLuiz Augusto von Dentz 	case BT_CONFIG:
5398*5af1f84eSLuiz Augusto von Dentz 		err = hci_disconnect_sync(hdev, conn, reason);
5399*5af1f84eSLuiz Augusto von Dentz 		break;
5400cf75ad8bSLuiz Augusto von Dentz 	case BT_CONNECT:
5401a13f316eSLuiz Augusto von Dentz 		err = hci_connect_cancel_sync(hdev, conn, reason);
5402*5af1f84eSLuiz Augusto von Dentz 		break;
5403cf75ad8bSLuiz Augusto von Dentz 	case BT_CONNECT2:
5404*5af1f84eSLuiz Augusto von Dentz 		err = hci_reject_conn_sync(hdev, conn, reason);
5405*5af1f84eSLuiz Augusto von Dentz 		break;
5406f777d882SIulia Tanasescu 	case BT_OPEN:
54072889bdd0SPauli Virtanen 	case BT_BOUND:
5408f777d882SIulia Tanasescu 		hci_dev_lock(hdev);
5409f777d882SIulia Tanasescu 		hci_conn_failed(conn, reason);
5410f777d882SIulia Tanasescu 		hci_dev_unlock(hdev);
54112889bdd0SPauli Virtanen 		return 0;
5412cf75ad8bSLuiz Augusto von Dentz 	default:
5413cf75ad8bSLuiz Augusto von Dentz 		conn->state = BT_CLOSED;
5414*5af1f84eSLuiz Augusto von Dentz 		return 0;
5415cf75ad8bSLuiz Augusto von Dentz 	}
5416cf75ad8bSLuiz Augusto von Dentz 
5417*5af1f84eSLuiz Augusto von Dentz 	/* Cleanup hci_conn object if it cannot be cancelled as it
5418*5af1f84eSLuiz Augusto von Dentz 	 * likelly means the controller and host stack are out of sync
5419*5af1f84eSLuiz Augusto von Dentz 	 * or in case of LE it was still scanning so it can be cleanup
5420*5af1f84eSLuiz Augusto von Dentz 	 * safely.
5421*5af1f84eSLuiz Augusto von Dentz 	 */
5422*5af1f84eSLuiz Augusto von Dentz 	if (err) {
5423*5af1f84eSLuiz Augusto von Dentz 		struct hci_conn *c;
5424*5af1f84eSLuiz Augusto von Dentz 
5425*5af1f84eSLuiz Augusto von Dentz 		/* Check if the connection hasn't been cleanup while waiting
5426*5af1f84eSLuiz Augusto von Dentz 		 * commands to complete.
5427*5af1f84eSLuiz Augusto von Dentz 		 */
5428*5af1f84eSLuiz Augusto von Dentz 		c = hci_conn_hash_lookup_handle(hdev, handle);
5429*5af1f84eSLuiz Augusto von Dentz 		if (!c || c != conn)
5430cf75ad8bSLuiz Augusto von Dentz 			return 0;
5431*5af1f84eSLuiz Augusto von Dentz 
5432*5af1f84eSLuiz Augusto von Dentz 		hci_dev_lock(hdev);
5433*5af1f84eSLuiz Augusto von Dentz 		hci_conn_failed(conn, err);
5434*5af1f84eSLuiz Augusto von Dentz 		hci_dev_unlock(hdev);
5435*5af1f84eSLuiz Augusto von Dentz 	}
5436*5af1f84eSLuiz Augusto von Dentz 
5437*5af1f84eSLuiz Augusto von Dentz 	return err;
5438cf75ad8bSLuiz Augusto von Dentz }
5439cf75ad8bSLuiz Augusto von Dentz 
5440182ee45dSLuiz Augusto von Dentz static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5441182ee45dSLuiz Augusto von Dentz {
5442182ee45dSLuiz Augusto von Dentz 	struct hci_conn *conn, *tmp;
5443182ee45dSLuiz Augusto von Dentz 	int err;
5444182ee45dSLuiz Augusto von Dentz 
5445182ee45dSLuiz Augusto von Dentz 	list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
5446182ee45dSLuiz Augusto von Dentz 		err = hci_abort_conn_sync(hdev, conn, reason);
5447182ee45dSLuiz Augusto von Dentz 		if (err)
5448182ee45dSLuiz Augusto von Dentz 			return err;
5449182ee45dSLuiz Augusto von Dentz 	}
5450182ee45dSLuiz Augusto von Dentz 
54518cd3c55cSTom Rix 	return 0;
5452182ee45dSLuiz Augusto von Dentz }
5453182ee45dSLuiz Augusto von Dentz 
5454cf75ad8bSLuiz Augusto von Dentz /* This function perform power off HCI command sequence as follows:
5455cf75ad8bSLuiz Augusto von Dentz  *
5456cf75ad8bSLuiz Augusto von Dentz  * Clear Advertising
5457cf75ad8bSLuiz Augusto von Dentz  * Stop Discovery
5458cf75ad8bSLuiz Augusto von Dentz  * Disconnect all connections
5459cf75ad8bSLuiz Augusto von Dentz  * hci_dev_close_sync
5460cf75ad8bSLuiz Augusto von Dentz  */
5461cf75ad8bSLuiz Augusto von Dentz static int hci_power_off_sync(struct hci_dev *hdev)
5462cf75ad8bSLuiz Augusto von Dentz {
5463cf75ad8bSLuiz Augusto von Dentz 	int err;
5464cf75ad8bSLuiz Augusto von Dentz 
5465cf75ad8bSLuiz Augusto von Dentz 	/* If controller is already down there is nothing to do */
5466cf75ad8bSLuiz Augusto von Dentz 	if (!test_bit(HCI_UP, &hdev->flags))
5467cf75ad8bSLuiz Augusto von Dentz 		return 0;
5468cf75ad8bSLuiz Augusto von Dentz 
5469cf75ad8bSLuiz Augusto von Dentz 	if (test_bit(HCI_ISCAN, &hdev->flags) ||
5470cf75ad8bSLuiz Augusto von Dentz 	    test_bit(HCI_PSCAN, &hdev->flags)) {
5471cf75ad8bSLuiz Augusto von Dentz 		err = hci_write_scan_enable_sync(hdev, 0x00);
5472cf75ad8bSLuiz Augusto von Dentz 		if (err)
5473cf75ad8bSLuiz Augusto von Dentz 			return err;
5474cf75ad8bSLuiz Augusto von Dentz 	}
5475cf75ad8bSLuiz Augusto von Dentz 
5476cf75ad8bSLuiz Augusto von Dentz 	err = hci_clear_adv_sync(hdev, NULL, false);
5477cf75ad8bSLuiz Augusto von Dentz 	if (err)
5478cf75ad8bSLuiz Augusto von Dentz 		return err;
5479cf75ad8bSLuiz Augusto von Dentz 
5480cf75ad8bSLuiz Augusto von Dentz 	err = hci_stop_discovery_sync(hdev);
5481cf75ad8bSLuiz Augusto von Dentz 	if (err)
5482cf75ad8bSLuiz Augusto von Dentz 		return err;
5483cf75ad8bSLuiz Augusto von Dentz 
5484182ee45dSLuiz Augusto von Dentz 	/* Terminated due to Power Off */
5485182ee45dSLuiz Augusto von Dentz 	err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5486182ee45dSLuiz Augusto von Dentz 	if (err)
5487182ee45dSLuiz Augusto von Dentz 		return err;
5488cf75ad8bSLuiz Augusto von Dentz 
5489cf75ad8bSLuiz Augusto von Dentz 	return hci_dev_close_sync(hdev);
5490cf75ad8bSLuiz Augusto von Dentz }
5491cf75ad8bSLuiz Augusto von Dentz 
5492cf75ad8bSLuiz Augusto von Dentz int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5493cf75ad8bSLuiz Augusto von Dentz {
5494cf75ad8bSLuiz Augusto von Dentz 	if (val)
5495cf75ad8bSLuiz Augusto von Dentz 		return hci_power_on_sync(hdev);
5496cf75ad8bSLuiz Augusto von Dentz 
5497cf75ad8bSLuiz Augusto von Dentz 	return hci_power_off_sync(hdev);
5498cf75ad8bSLuiz Augusto von Dentz }
5499abfeea47SLuiz Augusto von Dentz 
55002bd1b237SLuiz Augusto von Dentz static int hci_write_iac_sync(struct hci_dev *hdev)
55012bd1b237SLuiz Augusto von Dentz {
55022bd1b237SLuiz Augusto von Dentz 	struct hci_cp_write_current_iac_lap cp;
55032bd1b237SLuiz Augusto von Dentz 
55042bd1b237SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
55052bd1b237SLuiz Augusto von Dentz 		return 0;
55062bd1b237SLuiz Augusto von Dentz 
55072bd1b237SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
55082bd1b237SLuiz Augusto von Dentz 
55092bd1b237SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
55102bd1b237SLuiz Augusto von Dentz 		/* Limited discoverable mode */
55112bd1b237SLuiz Augusto von Dentz 		cp.num_iac = min_t(u8, hdev->num_iac, 2);
55122bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[0] = 0x00;	/* LIAC */
55132bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[1] = 0x8b;
55142bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[2] = 0x9e;
55152bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[3] = 0x33;	/* GIAC */
55162bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[4] = 0x8b;
55172bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[5] = 0x9e;
55182bd1b237SLuiz Augusto von Dentz 	} else {
55192bd1b237SLuiz Augusto von Dentz 		/* General discoverable mode */
55202bd1b237SLuiz Augusto von Dentz 		cp.num_iac = 1;
55212bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[0] = 0x33;	/* GIAC */
55222bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[1] = 0x8b;
55232bd1b237SLuiz Augusto von Dentz 		cp.iac_lap[2] = 0x9e;
55242bd1b237SLuiz Augusto von Dentz 	}
55252bd1b237SLuiz Augusto von Dentz 
55262bd1b237SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
55272bd1b237SLuiz Augusto von Dentz 				     (cp.num_iac * 3) + 1, &cp,
55282bd1b237SLuiz Augusto von Dentz 				     HCI_CMD_TIMEOUT);
55292bd1b237SLuiz Augusto von Dentz }
55302bd1b237SLuiz Augusto von Dentz 
55312bd1b237SLuiz Augusto von Dentz int hci_update_discoverable_sync(struct hci_dev *hdev)
55322bd1b237SLuiz Augusto von Dentz {
55332bd1b237SLuiz Augusto von Dentz 	int err = 0;
55342bd1b237SLuiz Augusto von Dentz 
55352bd1b237SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
55362bd1b237SLuiz Augusto von Dentz 		err = hci_write_iac_sync(hdev);
55372bd1b237SLuiz Augusto von Dentz 		if (err)
55382bd1b237SLuiz Augusto von Dentz 			return err;
55392bd1b237SLuiz Augusto von Dentz 
55402bd1b237SLuiz Augusto von Dentz 		err = hci_update_scan_sync(hdev);
55412bd1b237SLuiz Augusto von Dentz 		if (err)
55422bd1b237SLuiz Augusto von Dentz 			return err;
55432bd1b237SLuiz Augusto von Dentz 
55442bd1b237SLuiz Augusto von Dentz 		err = hci_update_class_sync(hdev);
55452bd1b237SLuiz Augusto von Dentz 		if (err)
55462bd1b237SLuiz Augusto von Dentz 			return err;
55472bd1b237SLuiz Augusto von Dentz 	}
55482bd1b237SLuiz Augusto von Dentz 
55492bd1b237SLuiz Augusto von Dentz 	/* Advertising instances don't use the global discoverable setting, so
55502bd1b237SLuiz Augusto von Dentz 	 * only update AD if advertising was enabled using Set Advertising.
55512bd1b237SLuiz Augusto von Dentz 	 */
55522bd1b237SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
55532bd1b237SLuiz Augusto von Dentz 		err = hci_update_adv_data_sync(hdev, 0x00);
55542bd1b237SLuiz Augusto von Dentz 		if (err)
55552bd1b237SLuiz Augusto von Dentz 			return err;
55562bd1b237SLuiz Augusto von Dentz 
55572bd1b237SLuiz Augusto von Dentz 		/* Discoverable mode affects the local advertising
55582bd1b237SLuiz Augusto von Dentz 		 * address in limited privacy mode.
55592bd1b237SLuiz Augusto von Dentz 		 */
55602bd1b237SLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
55612bd1b237SLuiz Augusto von Dentz 			if (ext_adv_capable(hdev))
55622bd1b237SLuiz Augusto von Dentz 				err = hci_start_ext_adv_sync(hdev, 0x00);
55632bd1b237SLuiz Augusto von Dentz 			else
55642bd1b237SLuiz Augusto von Dentz 				err = hci_enable_advertising_sync(hdev);
55652bd1b237SLuiz Augusto von Dentz 		}
55662bd1b237SLuiz Augusto von Dentz 	}
55672bd1b237SLuiz Augusto von Dentz 
55682bd1b237SLuiz Augusto von Dentz 	return err;
55692bd1b237SLuiz Augusto von Dentz }
55702bd1b237SLuiz Augusto von Dentz 
55712bd1b237SLuiz Augusto von Dentz static int update_discoverable_sync(struct hci_dev *hdev, void *data)
55722bd1b237SLuiz Augusto von Dentz {
55732bd1b237SLuiz Augusto von Dentz 	return hci_update_discoverable_sync(hdev);
55742bd1b237SLuiz Augusto von Dentz }
55752bd1b237SLuiz Augusto von Dentz 
55762bd1b237SLuiz Augusto von Dentz int hci_update_discoverable(struct hci_dev *hdev)
55772bd1b237SLuiz Augusto von Dentz {
55782bd1b237SLuiz Augusto von Dentz 	/* Only queue if it would have any effect */
55792bd1b237SLuiz Augusto von Dentz 	if (hdev_is_powered(hdev) &&
55802bd1b237SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
55812bd1b237SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
55822bd1b237SLuiz Augusto von Dentz 	    hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
55832bd1b237SLuiz Augusto von Dentz 		return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
55842bd1b237SLuiz Augusto von Dentz 					  NULL);
55852bd1b237SLuiz Augusto von Dentz 
55862bd1b237SLuiz Augusto von Dentz 	return 0;
55872bd1b237SLuiz Augusto von Dentz }
55882bd1b237SLuiz Augusto von Dentz 
5589f056a657SLuiz Augusto von Dentz int hci_update_connectable_sync(struct hci_dev *hdev)
5590f056a657SLuiz Augusto von Dentz {
5591f056a657SLuiz Augusto von Dentz 	int err;
5592f056a657SLuiz Augusto von Dentz 
5593f056a657SLuiz Augusto von Dentz 	err = hci_update_scan_sync(hdev);
5594f056a657SLuiz Augusto von Dentz 	if (err)
5595f056a657SLuiz Augusto von Dentz 		return err;
5596f056a657SLuiz Augusto von Dentz 
5597f056a657SLuiz Augusto von Dentz 	/* If BR/EDR is not enabled and we disable advertising as a
5598f056a657SLuiz Augusto von Dentz 	 * by-product of disabling connectable, we need to update the
5599f056a657SLuiz Augusto von Dentz 	 * advertising flags.
5600f056a657SLuiz Augusto von Dentz 	 */
5601f056a657SLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5602f056a657SLuiz Augusto von Dentz 		err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5603f056a657SLuiz Augusto von Dentz 
5604f056a657SLuiz Augusto von Dentz 	/* Update the advertising parameters if necessary */
5605f056a657SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5606f056a657SLuiz Augusto von Dentz 	    !list_empty(&hdev->adv_instances)) {
5607f056a657SLuiz Augusto von Dentz 		if (ext_adv_capable(hdev))
5608f056a657SLuiz Augusto von Dentz 			err = hci_start_ext_adv_sync(hdev,
5609f056a657SLuiz Augusto von Dentz 						     hdev->cur_adv_instance);
5610f056a657SLuiz Augusto von Dentz 		else
5611f056a657SLuiz Augusto von Dentz 			err = hci_enable_advertising_sync(hdev);
5612f056a657SLuiz Augusto von Dentz 
5613f056a657SLuiz Augusto von Dentz 		if (err)
5614f056a657SLuiz Augusto von Dentz 			return err;
5615f056a657SLuiz Augusto von Dentz 	}
5616f056a657SLuiz Augusto von Dentz 
5617f056a657SLuiz Augusto von Dentz 	return hci_update_passive_scan_sync(hdev);
5618f056a657SLuiz Augusto von Dentz }
5619f056a657SLuiz Augusto von Dentz 
5620abfeea47SLuiz Augusto von Dentz static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5621abfeea47SLuiz Augusto von Dentz {
5622abfeea47SLuiz Augusto von Dentz 	const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5623abfeea47SLuiz Augusto von Dentz 	const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5624abfeea47SLuiz Augusto von Dentz 	struct hci_cp_inquiry cp;
5625abfeea47SLuiz Augusto von Dentz 
5626abfeea47SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
5627abfeea47SLuiz Augusto von Dentz 
5628abfeea47SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_INQUIRY))
5629abfeea47SLuiz Augusto von Dentz 		return 0;
5630abfeea47SLuiz Augusto von Dentz 
5631abfeea47SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
5632abfeea47SLuiz Augusto von Dentz 	hci_inquiry_cache_flush(hdev);
5633abfeea47SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
5634abfeea47SLuiz Augusto von Dentz 
5635abfeea47SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
5636abfeea47SLuiz Augusto von Dentz 
5637abfeea47SLuiz Augusto von Dentz 	if (hdev->discovery.limited)
5638abfeea47SLuiz Augusto von Dentz 		memcpy(&cp.lap, liac, sizeof(cp.lap));
5639abfeea47SLuiz Augusto von Dentz 	else
5640abfeea47SLuiz Augusto von Dentz 		memcpy(&cp.lap, giac, sizeof(cp.lap));
5641abfeea47SLuiz Augusto von Dentz 
5642abfeea47SLuiz Augusto von Dentz 	cp.length = length;
5643abfeea47SLuiz Augusto von Dentz 
5644abfeea47SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5645abfeea47SLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5646abfeea47SLuiz Augusto von Dentz }
5647abfeea47SLuiz Augusto von Dentz 
5648abfeea47SLuiz Augusto von Dentz static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5649abfeea47SLuiz Augusto von Dentz {
5650abfeea47SLuiz Augusto von Dentz 	u8 own_addr_type;
5651abfeea47SLuiz Augusto von Dentz 	/* Accept list is not used for discovery */
5652abfeea47SLuiz Augusto von Dentz 	u8 filter_policy = 0x00;
5653abfeea47SLuiz Augusto von Dentz 	/* Default is to enable duplicates filter */
5654abfeea47SLuiz Augusto von Dentz 	u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5655abfeea47SLuiz Augusto von Dentz 	int err;
5656abfeea47SLuiz Augusto von Dentz 
5657abfeea47SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
5658abfeea47SLuiz Augusto von Dentz 
5659abfeea47SLuiz Augusto von Dentz 	/* If controller is scanning, it means the passive scanning is
5660abfeea47SLuiz Augusto von Dentz 	 * running. Thus, we should temporarily stop it in order to set the
5661abfeea47SLuiz Augusto von Dentz 	 * discovery scanning parameters.
5662abfeea47SLuiz Augusto von Dentz 	 */
5663abfeea47SLuiz Augusto von Dentz 	err = hci_scan_disable_sync(hdev);
5664abfeea47SLuiz Augusto von Dentz 	if (err) {
5665abfeea47SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5666abfeea47SLuiz Augusto von Dentz 		return err;
5667abfeea47SLuiz Augusto von Dentz 	}
5668abfeea47SLuiz Augusto von Dentz 
5669abfeea47SLuiz Augusto von Dentz 	cancel_interleave_scan(hdev);
5670abfeea47SLuiz Augusto von Dentz 
56713c44a431SZhengping Jiang 	/* Pause address resolution for active scan and stop advertising if
56723c44a431SZhengping Jiang 	 * privacy is enabled.
5673abfeea47SLuiz Augusto von Dentz 	 */
56743c44a431SZhengping Jiang 	err = hci_pause_addr_resolution(hdev);
56753c44a431SZhengping Jiang 	if (err)
5676abfeea47SLuiz Augusto von Dentz 		goto failed;
5677abfeea47SLuiz Augusto von Dentz 
5678abfeea47SLuiz Augusto von Dentz 	/* All active scans will be done with either a resolvable private
5679abfeea47SLuiz Augusto von Dentz 	 * address (when privacy feature has been enabled) or non-resolvable
5680abfeea47SLuiz Augusto von Dentz 	 * private address.
5681abfeea47SLuiz Augusto von Dentz 	 */
5682abfeea47SLuiz Augusto von Dentz 	err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5683abfeea47SLuiz Augusto von Dentz 					     &own_addr_type);
5684abfeea47SLuiz Augusto von Dentz 	if (err < 0)
5685abfeea47SLuiz Augusto von Dentz 		own_addr_type = ADDR_LE_DEV_PUBLIC;
5686abfeea47SLuiz Augusto von Dentz 
5687abfeea47SLuiz Augusto von Dentz 	if (hci_is_adv_monitoring(hdev)) {
5688abfeea47SLuiz Augusto von Dentz 		/* Duplicate filter should be disabled when some advertisement
5689abfeea47SLuiz Augusto von Dentz 		 * monitor is activated, otherwise AdvMon can only receive one
5690abfeea47SLuiz Augusto von Dentz 		 * advertisement for one peer(*) during active scanning, and
5691abfeea47SLuiz Augusto von Dentz 		 * might report loss to these peers.
5692abfeea47SLuiz Augusto von Dentz 		 *
5693abfeea47SLuiz Augusto von Dentz 		 * Note that different controllers have different meanings of
5694abfeea47SLuiz Augusto von Dentz 		 * |duplicate|. Some of them consider packets with the same
5695abfeea47SLuiz Augusto von Dentz 		 * address as duplicate, and others consider packets with the
5696abfeea47SLuiz Augusto von Dentz 		 * same address and the same RSSI as duplicate. Although in the
5697abfeea47SLuiz Augusto von Dentz 		 * latter case we don't need to disable duplicate filter, but
5698abfeea47SLuiz Augusto von Dentz 		 * it is common to have active scanning for a short period of
5699abfeea47SLuiz Augusto von Dentz 		 * time, the power impact should be neglectable.
5700abfeea47SLuiz Augusto von Dentz 		 */
5701abfeea47SLuiz Augusto von Dentz 		filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5702abfeea47SLuiz Augusto von Dentz 	}
5703abfeea47SLuiz Augusto von Dentz 
5704abfeea47SLuiz Augusto von Dentz 	err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5705abfeea47SLuiz Augusto von Dentz 				  hdev->le_scan_window_discovery,
5706abfeea47SLuiz Augusto von Dentz 				  own_addr_type, filter_policy, filter_dup);
5707abfeea47SLuiz Augusto von Dentz 	if (!err)
5708abfeea47SLuiz Augusto von Dentz 		return err;
5709abfeea47SLuiz Augusto von Dentz 
5710abfeea47SLuiz Augusto von Dentz failed:
5711abfeea47SLuiz Augusto von Dentz 	/* Resume advertising if it was paused */
5712abfeea47SLuiz Augusto von Dentz 	if (use_ll_privacy(hdev))
5713abfeea47SLuiz Augusto von Dentz 		hci_resume_advertising_sync(hdev);
5714abfeea47SLuiz Augusto von Dentz 
5715abfeea47SLuiz Augusto von Dentz 	/* Resume passive scanning */
5716abfeea47SLuiz Augusto von Dentz 	hci_update_passive_scan_sync(hdev);
5717abfeea47SLuiz Augusto von Dentz 	return err;
5718abfeea47SLuiz Augusto von Dentz }
5719abfeea47SLuiz Augusto von Dentz 
5720abfeea47SLuiz Augusto von Dentz static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5721abfeea47SLuiz Augusto von Dentz {
5722abfeea47SLuiz Augusto von Dentz 	int err;
5723abfeea47SLuiz Augusto von Dentz 
5724abfeea47SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
5725abfeea47SLuiz Augusto von Dentz 
5726abfeea47SLuiz Augusto von Dentz 	err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5727abfeea47SLuiz Augusto von Dentz 	if (err)
5728abfeea47SLuiz Augusto von Dentz 		return err;
5729abfeea47SLuiz Augusto von Dentz 
5730abfeea47SLuiz Augusto von Dentz 	return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5731abfeea47SLuiz Augusto von Dentz }
5732abfeea47SLuiz Augusto von Dentz 
5733abfeea47SLuiz Augusto von Dentz int hci_start_discovery_sync(struct hci_dev *hdev)
5734abfeea47SLuiz Augusto von Dentz {
5735abfeea47SLuiz Augusto von Dentz 	unsigned long timeout;
5736abfeea47SLuiz Augusto von Dentz 	int err;
5737abfeea47SLuiz Augusto von Dentz 
5738abfeea47SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5739abfeea47SLuiz Augusto von Dentz 
5740abfeea47SLuiz Augusto von Dentz 	switch (hdev->discovery.type) {
5741abfeea47SLuiz Augusto von Dentz 	case DISCOV_TYPE_BREDR:
5742abfeea47SLuiz Augusto von Dentz 		return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5743abfeea47SLuiz Augusto von Dentz 	case DISCOV_TYPE_INTERLEAVED:
5744abfeea47SLuiz Augusto von Dentz 		/* When running simultaneous discovery, the LE scanning time
5745abfeea47SLuiz Augusto von Dentz 		 * should occupy the whole discovery time sine BR/EDR inquiry
5746abfeea47SLuiz Augusto von Dentz 		 * and LE scanning are scheduled by the controller.
5747abfeea47SLuiz Augusto von Dentz 		 *
5748abfeea47SLuiz Augusto von Dentz 		 * For interleaving discovery in comparison, BR/EDR inquiry
5749abfeea47SLuiz Augusto von Dentz 		 * and LE scanning are done sequentially with separate
5750abfeea47SLuiz Augusto von Dentz 		 * timeouts.
5751abfeea47SLuiz Augusto von Dentz 		 */
5752abfeea47SLuiz Augusto von Dentz 		if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5753abfeea47SLuiz Augusto von Dentz 			     &hdev->quirks)) {
5754abfeea47SLuiz Augusto von Dentz 			timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5755abfeea47SLuiz Augusto von Dentz 			/* During simultaneous discovery, we double LE scan
5756abfeea47SLuiz Augusto von Dentz 			 * interval. We must leave some time for the controller
5757abfeea47SLuiz Augusto von Dentz 			 * to do BR/EDR inquiry.
5758abfeea47SLuiz Augusto von Dentz 			 */
5759abfeea47SLuiz Augusto von Dentz 			err = hci_start_interleaved_discovery_sync(hdev);
5760abfeea47SLuiz Augusto von Dentz 			break;
5761abfeea47SLuiz Augusto von Dentz 		}
5762abfeea47SLuiz Augusto von Dentz 
5763abfeea47SLuiz Augusto von Dentz 		timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5764abfeea47SLuiz Augusto von Dentz 		err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5765abfeea47SLuiz Augusto von Dentz 		break;
5766abfeea47SLuiz Augusto von Dentz 	case DISCOV_TYPE_LE:
5767abfeea47SLuiz Augusto von Dentz 		timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5768abfeea47SLuiz Augusto von Dentz 		err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5769abfeea47SLuiz Augusto von Dentz 		break;
5770abfeea47SLuiz Augusto von Dentz 	default:
5771abfeea47SLuiz Augusto von Dentz 		return -EINVAL;
5772abfeea47SLuiz Augusto von Dentz 	}
5773abfeea47SLuiz Augusto von Dentz 
5774abfeea47SLuiz Augusto von Dentz 	if (err)
5775abfeea47SLuiz Augusto von Dentz 		return err;
5776abfeea47SLuiz Augusto von Dentz 
5777abfeea47SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5778abfeea47SLuiz Augusto von Dentz 
5779abfeea47SLuiz Augusto von Dentz 	/* When service discovery is used and the controller has a
5780abfeea47SLuiz Augusto von Dentz 	 * strict duplicate filter, it is important to remember the
5781abfeea47SLuiz Augusto von Dentz 	 * start and duration of the scan. This is required for
5782abfeea47SLuiz Augusto von Dentz 	 * restarting scanning during the discovery phase.
5783abfeea47SLuiz Augusto von Dentz 	 */
5784abfeea47SLuiz Augusto von Dentz 	if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5785abfeea47SLuiz Augusto von Dentz 	    hdev->discovery.result_filtering) {
5786abfeea47SLuiz Augusto von Dentz 		hdev->discovery.scan_start = jiffies;
5787abfeea47SLuiz Augusto von Dentz 		hdev->discovery.scan_duration = timeout;
5788abfeea47SLuiz Augusto von Dentz 	}
5789abfeea47SLuiz Augusto von Dentz 
5790abfeea47SLuiz Augusto von Dentz 	queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5791abfeea47SLuiz Augusto von Dentz 			   timeout);
5792abfeea47SLuiz Augusto von Dentz 	return 0;
5793abfeea47SLuiz Augusto von Dentz }
5794182ee45dSLuiz Augusto von Dentz 
5795182ee45dSLuiz Augusto von Dentz static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5796182ee45dSLuiz Augusto von Dentz {
5797182ee45dSLuiz Augusto von Dentz 	switch (hci_get_adv_monitor_offload_ext(hdev)) {
5798182ee45dSLuiz Augusto von Dentz 	case HCI_ADV_MONITOR_EXT_MSFT:
5799182ee45dSLuiz Augusto von Dentz 		msft_suspend_sync(hdev);
5800182ee45dSLuiz Augusto von Dentz 		break;
5801182ee45dSLuiz Augusto von Dentz 	default:
5802182ee45dSLuiz Augusto von Dentz 		return;
5803182ee45dSLuiz Augusto von Dentz 	}
5804182ee45dSLuiz Augusto von Dentz }
5805182ee45dSLuiz Augusto von Dentz 
5806182ee45dSLuiz Augusto von Dentz /* This function disables discovery and mark it as paused */
5807182ee45dSLuiz Augusto von Dentz static int hci_pause_discovery_sync(struct hci_dev *hdev)
5808182ee45dSLuiz Augusto von Dentz {
5809182ee45dSLuiz Augusto von Dentz 	int old_state = hdev->discovery.state;
5810182ee45dSLuiz Augusto von Dentz 	int err;
5811182ee45dSLuiz Augusto von Dentz 
5812182ee45dSLuiz Augusto von Dentz 	/* If discovery already stopped/stopping/paused there nothing to do */
5813182ee45dSLuiz Augusto von Dentz 	if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
5814182ee45dSLuiz Augusto von Dentz 	    hdev->discovery_paused)
5815182ee45dSLuiz Augusto von Dentz 		return 0;
5816182ee45dSLuiz Augusto von Dentz 
5817182ee45dSLuiz Augusto von Dentz 	hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5818182ee45dSLuiz Augusto von Dentz 	err = hci_stop_discovery_sync(hdev);
5819182ee45dSLuiz Augusto von Dentz 	if (err)
5820182ee45dSLuiz Augusto von Dentz 		return err;
5821182ee45dSLuiz Augusto von Dentz 
5822182ee45dSLuiz Augusto von Dentz 	hdev->discovery_paused = true;
5823182ee45dSLuiz Augusto von Dentz 	hdev->discovery_old_state = old_state;
5824182ee45dSLuiz Augusto von Dentz 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5825182ee45dSLuiz Augusto von Dentz 
5826182ee45dSLuiz Augusto von Dentz 	return 0;
5827182ee45dSLuiz Augusto von Dentz }
5828182ee45dSLuiz Augusto von Dentz 
5829182ee45dSLuiz Augusto von Dentz static int hci_update_event_filter_sync(struct hci_dev *hdev)
5830182ee45dSLuiz Augusto von Dentz {
5831182ee45dSLuiz Augusto von Dentz 	struct bdaddr_list_with_flags *b;
5832182ee45dSLuiz Augusto von Dentz 	u8 scan = SCAN_DISABLED;
5833182ee45dSLuiz Augusto von Dentz 	bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5834182ee45dSLuiz Augusto von Dentz 	int err;
5835182ee45dSLuiz Augusto von Dentz 
5836182ee45dSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5837182ee45dSLuiz Augusto von Dentz 		return 0;
5838182ee45dSLuiz Augusto von Dentz 
58390eaecfb2SIsmael Ferreras Morezuelas 	/* Some fake CSR controllers lock up after setting this type of
58400eaecfb2SIsmael Ferreras Morezuelas 	 * filter, so avoid sending the request altogether.
58410eaecfb2SIsmael Ferreras Morezuelas 	 */
58420eaecfb2SIsmael Ferreras Morezuelas 	if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
58430eaecfb2SIsmael Ferreras Morezuelas 		return 0;
58440eaecfb2SIsmael Ferreras Morezuelas 
5845182ee45dSLuiz Augusto von Dentz 	/* Always clear event filter when starting */
5846182ee45dSLuiz Augusto von Dentz 	hci_clear_event_filter_sync(hdev);
5847182ee45dSLuiz Augusto von Dentz 
5848182ee45dSLuiz Augusto von Dentz 	list_for_each_entry(b, &hdev->accept_list, list) {
5849e1cff700SLinus Torvalds 		if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
5850182ee45dSLuiz Augusto von Dentz 			continue;
5851182ee45dSLuiz Augusto von Dentz 
5852182ee45dSLuiz Augusto von Dentz 		bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5853182ee45dSLuiz Augusto von Dentz 
5854182ee45dSLuiz Augusto von Dentz 		err =  hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5855182ee45dSLuiz Augusto von Dentz 						 HCI_CONN_SETUP_ALLOW_BDADDR,
5856182ee45dSLuiz Augusto von Dentz 						 &b->bdaddr,
5857182ee45dSLuiz Augusto von Dentz 						 HCI_CONN_SETUP_AUTO_ON);
5858182ee45dSLuiz Augusto von Dentz 		if (err)
5859182ee45dSLuiz Augusto von Dentz 			bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5860182ee45dSLuiz Augusto von Dentz 				   &b->bdaddr);
5861182ee45dSLuiz Augusto von Dentz 		else
5862182ee45dSLuiz Augusto von Dentz 			scan = SCAN_PAGE;
5863182ee45dSLuiz Augusto von Dentz 	}
5864182ee45dSLuiz Augusto von Dentz 
5865182ee45dSLuiz Augusto von Dentz 	if (scan && !scanning)
5866182ee45dSLuiz Augusto von Dentz 		hci_write_scan_enable_sync(hdev, scan);
5867182ee45dSLuiz Augusto von Dentz 	else if (!scan && scanning)
5868182ee45dSLuiz Augusto von Dentz 		hci_write_scan_enable_sync(hdev, scan);
5869182ee45dSLuiz Augusto von Dentz 
5870182ee45dSLuiz Augusto von Dentz 	return 0;
5871182ee45dSLuiz Augusto von Dentz }
5872182ee45dSLuiz Augusto von Dentz 
58733b420553SLuiz Augusto von Dentz /* This function disables scan (BR and LE) and mark it as paused */
58743b420553SLuiz Augusto von Dentz static int hci_pause_scan_sync(struct hci_dev *hdev)
58753b420553SLuiz Augusto von Dentz {
58763b420553SLuiz Augusto von Dentz 	if (hdev->scanning_paused)
58773b420553SLuiz Augusto von Dentz 		return 0;
58783b420553SLuiz Augusto von Dentz 
58793b420553SLuiz Augusto von Dentz 	/* Disable page scan if enabled */
58803b420553SLuiz Augusto von Dentz 	if (test_bit(HCI_PSCAN, &hdev->flags))
58813b420553SLuiz Augusto von Dentz 		hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
58823b420553SLuiz Augusto von Dentz 
58833b420553SLuiz Augusto von Dentz 	hci_scan_disable_sync(hdev);
58843b420553SLuiz Augusto von Dentz 
58853b420553SLuiz Augusto von Dentz 	hdev->scanning_paused = true;
58863b420553SLuiz Augusto von Dentz 
58873b420553SLuiz Augusto von Dentz 	return 0;
58883b420553SLuiz Augusto von Dentz }
58893b420553SLuiz Augusto von Dentz 
5890182ee45dSLuiz Augusto von Dentz /* This function performs the HCI suspend procedures in the follow order:
5891182ee45dSLuiz Augusto von Dentz  *
5892182ee45dSLuiz Augusto von Dentz  * Pause discovery (active scanning/inquiry)
5893182ee45dSLuiz Augusto von Dentz  * Pause Directed Advertising/Advertising
58943b420553SLuiz Augusto von Dentz  * Pause Scanning (passive scanning in case discovery was not active)
5895182ee45dSLuiz Augusto von Dentz  * Disconnect all connections
5896182ee45dSLuiz Augusto von Dentz  * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5897182ee45dSLuiz Augusto von Dentz  * otherwise:
5898182ee45dSLuiz Augusto von Dentz  * Update event mask (only set events that are allowed to wake up the host)
5899182ee45dSLuiz Augusto von Dentz  * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
5900182ee45dSLuiz Augusto von Dentz  * Update passive scanning (lower duty cycle)
5901182ee45dSLuiz Augusto von Dentz  * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
5902182ee45dSLuiz Augusto von Dentz  */
5903182ee45dSLuiz Augusto von Dentz int hci_suspend_sync(struct hci_dev *hdev)
5904182ee45dSLuiz Augusto von Dentz {
5905182ee45dSLuiz Augusto von Dentz 	int err;
5906182ee45dSLuiz Augusto von Dentz 
5907182ee45dSLuiz Augusto von Dentz 	/* If marked as suspended there nothing to do */
5908182ee45dSLuiz Augusto von Dentz 	if (hdev->suspended)
5909182ee45dSLuiz Augusto von Dentz 		return 0;
5910182ee45dSLuiz Augusto von Dentz 
5911182ee45dSLuiz Augusto von Dentz 	/* Mark device as suspended */
5912182ee45dSLuiz Augusto von Dentz 	hdev->suspended = true;
5913182ee45dSLuiz Augusto von Dentz 
5914182ee45dSLuiz Augusto von Dentz 	/* Pause discovery if not already stopped */
5915182ee45dSLuiz Augusto von Dentz 	hci_pause_discovery_sync(hdev);
5916182ee45dSLuiz Augusto von Dentz 
5917182ee45dSLuiz Augusto von Dentz 	/* Pause other advertisements */
5918182ee45dSLuiz Augusto von Dentz 	hci_pause_advertising_sync(hdev);
5919182ee45dSLuiz Augusto von Dentz 
5920182ee45dSLuiz Augusto von Dentz 	/* Suspend monitor filters */
5921182ee45dSLuiz Augusto von Dentz 	hci_suspend_monitor_sync(hdev);
5922182ee45dSLuiz Augusto von Dentz 
5923182ee45dSLuiz Augusto von Dentz 	/* Prevent disconnects from causing scanning to be re-enabled */
59243b420553SLuiz Augusto von Dentz 	hci_pause_scan_sync(hdev);
5925182ee45dSLuiz Augusto von Dentz 
5926123f6d3aSLuiz Augusto von Dentz 	if (hci_conn_count(hdev)) {
5927182ee45dSLuiz Augusto von Dentz 		/* Soft disconnect everything (power off) */
5928182ee45dSLuiz Augusto von Dentz 		err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5929182ee45dSLuiz Augusto von Dentz 		if (err) {
5930182ee45dSLuiz Augusto von Dentz 			/* Set state to BT_RUNNING so resume doesn't notify */
5931182ee45dSLuiz Augusto von Dentz 			hdev->suspend_state = BT_RUNNING;
5932182ee45dSLuiz Augusto von Dentz 			hci_resume_sync(hdev);
5933182ee45dSLuiz Augusto von Dentz 			return err;
5934182ee45dSLuiz Augusto von Dentz 		}
5935182ee45dSLuiz Augusto von Dentz 
5936123f6d3aSLuiz Augusto von Dentz 		/* Update event mask so only the allowed event can wakeup the
5937123f6d3aSLuiz Augusto von Dentz 		 * host.
5938123f6d3aSLuiz Augusto von Dentz 		 */
5939ef61b6eaSAbhishek Pandit-Subedi 		hci_set_event_mask_sync(hdev);
5940123f6d3aSLuiz Augusto von Dentz 	}
5941ef61b6eaSAbhishek Pandit-Subedi 
5942182ee45dSLuiz Augusto von Dentz 	/* Only configure accept list if disconnect succeeded and wake
5943182ee45dSLuiz Augusto von Dentz 	 * isn't being prevented.
5944182ee45dSLuiz Augusto von Dentz 	 */
5945182ee45dSLuiz Augusto von Dentz 	if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5946182ee45dSLuiz Augusto von Dentz 		hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5947182ee45dSLuiz Augusto von Dentz 		return 0;
5948182ee45dSLuiz Augusto von Dentz 	}
5949182ee45dSLuiz Augusto von Dentz 
5950182ee45dSLuiz Augusto von Dentz 	/* Unpause to take care of updating scanning params */
5951182ee45dSLuiz Augusto von Dentz 	hdev->scanning_paused = false;
5952182ee45dSLuiz Augusto von Dentz 
5953182ee45dSLuiz Augusto von Dentz 	/* Enable event filter for paired devices */
5954182ee45dSLuiz Augusto von Dentz 	hci_update_event_filter_sync(hdev);
5955182ee45dSLuiz Augusto von Dentz 
5956182ee45dSLuiz Augusto von Dentz 	/* Update LE passive scan if enabled */
5957182ee45dSLuiz Augusto von Dentz 	hci_update_passive_scan_sync(hdev);
5958182ee45dSLuiz Augusto von Dentz 
5959182ee45dSLuiz Augusto von Dentz 	/* Pause scan changes again. */
5960182ee45dSLuiz Augusto von Dentz 	hdev->scanning_paused = true;
5961182ee45dSLuiz Augusto von Dentz 
5962182ee45dSLuiz Augusto von Dentz 	hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5963182ee45dSLuiz Augusto von Dentz 
5964182ee45dSLuiz Augusto von Dentz 	return 0;
5965182ee45dSLuiz Augusto von Dentz }
5966182ee45dSLuiz Augusto von Dentz 
5967182ee45dSLuiz Augusto von Dentz /* This function resumes discovery */
5968182ee45dSLuiz Augusto von Dentz static int hci_resume_discovery_sync(struct hci_dev *hdev)
5969182ee45dSLuiz Augusto von Dentz {
5970182ee45dSLuiz Augusto von Dentz 	int err;
5971182ee45dSLuiz Augusto von Dentz 
5972182ee45dSLuiz Augusto von Dentz 	/* If discovery not paused there nothing to do */
5973182ee45dSLuiz Augusto von Dentz 	if (!hdev->discovery_paused)
5974182ee45dSLuiz Augusto von Dentz 		return 0;
5975182ee45dSLuiz Augusto von Dentz 
5976182ee45dSLuiz Augusto von Dentz 	hdev->discovery_paused = false;
5977182ee45dSLuiz Augusto von Dentz 
5978182ee45dSLuiz Augusto von Dentz 	hci_discovery_set_state(hdev, DISCOVERY_STARTING);
5979182ee45dSLuiz Augusto von Dentz 
5980182ee45dSLuiz Augusto von Dentz 	err = hci_start_discovery_sync(hdev);
5981182ee45dSLuiz Augusto von Dentz 
5982182ee45dSLuiz Augusto von Dentz 	hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
5983182ee45dSLuiz Augusto von Dentz 				DISCOVERY_FINDING);
5984182ee45dSLuiz Augusto von Dentz 
5985182ee45dSLuiz Augusto von Dentz 	return err;
5986182ee45dSLuiz Augusto von Dentz }
5987182ee45dSLuiz Augusto von Dentz 
5988182ee45dSLuiz Augusto von Dentz static void hci_resume_monitor_sync(struct hci_dev *hdev)
5989182ee45dSLuiz Augusto von Dentz {
5990182ee45dSLuiz Augusto von Dentz 	switch (hci_get_adv_monitor_offload_ext(hdev)) {
5991182ee45dSLuiz Augusto von Dentz 	case HCI_ADV_MONITOR_EXT_MSFT:
5992182ee45dSLuiz Augusto von Dentz 		msft_resume_sync(hdev);
5993182ee45dSLuiz Augusto von Dentz 		break;
5994182ee45dSLuiz Augusto von Dentz 	default:
5995182ee45dSLuiz Augusto von Dentz 		return;
5996182ee45dSLuiz Augusto von Dentz 	}
5997182ee45dSLuiz Augusto von Dentz }
5998182ee45dSLuiz Augusto von Dentz 
59993b420553SLuiz Augusto von Dentz /* This function resume scan and reset paused flag */
60003b420553SLuiz Augusto von Dentz static int hci_resume_scan_sync(struct hci_dev *hdev)
60013b420553SLuiz Augusto von Dentz {
60023b420553SLuiz Augusto von Dentz 	if (!hdev->scanning_paused)
60033b420553SLuiz Augusto von Dentz 		return 0;
60043b420553SLuiz Augusto von Dentz 
600568253f3cSZhengping Jiang 	hdev->scanning_paused = false;
600668253f3cSZhengping Jiang 
60073b420553SLuiz Augusto von Dentz 	hci_update_scan_sync(hdev);
60083b420553SLuiz Augusto von Dentz 
60093b420553SLuiz Augusto von Dentz 	/* Reset passive scanning to normal */
60103b420553SLuiz Augusto von Dentz 	hci_update_passive_scan_sync(hdev);
60113b420553SLuiz Augusto von Dentz 
60123b420553SLuiz Augusto von Dentz 	return 0;
60133b420553SLuiz Augusto von Dentz }
60143b420553SLuiz Augusto von Dentz 
6015182ee45dSLuiz Augusto von Dentz /* This function performs the HCI suspend procedures in the follow order:
6016182ee45dSLuiz Augusto von Dentz  *
6017182ee45dSLuiz Augusto von Dentz  * Restore event mask
6018182ee45dSLuiz Augusto von Dentz  * Clear event filter
6019182ee45dSLuiz Augusto von Dentz  * Update passive scanning (normal duty cycle)
6020182ee45dSLuiz Augusto von Dentz  * Resume Directed Advertising/Advertising
6021182ee45dSLuiz Augusto von Dentz  * Resume discovery (active scanning/inquiry)
6022182ee45dSLuiz Augusto von Dentz  */
6023182ee45dSLuiz Augusto von Dentz int hci_resume_sync(struct hci_dev *hdev)
6024182ee45dSLuiz Augusto von Dentz {
6025182ee45dSLuiz Augusto von Dentz 	/* If not marked as suspended there nothing to do */
6026182ee45dSLuiz Augusto von Dentz 	if (!hdev->suspended)
6027182ee45dSLuiz Augusto von Dentz 		return 0;
6028182ee45dSLuiz Augusto von Dentz 
6029182ee45dSLuiz Augusto von Dentz 	hdev->suspended = false;
6030182ee45dSLuiz Augusto von Dentz 
6031182ee45dSLuiz Augusto von Dentz 	/* Restore event mask */
6032182ee45dSLuiz Augusto von Dentz 	hci_set_event_mask_sync(hdev);
6033182ee45dSLuiz Augusto von Dentz 
6034182ee45dSLuiz Augusto von Dentz 	/* Clear any event filters and restore scan state */
6035182ee45dSLuiz Augusto von Dentz 	hci_clear_event_filter_sync(hdev);
6036182ee45dSLuiz Augusto von Dentz 
60373b420553SLuiz Augusto von Dentz 	/* Resume scanning */
60383b420553SLuiz Augusto von Dentz 	hci_resume_scan_sync(hdev);
6039182ee45dSLuiz Augusto von Dentz 
6040182ee45dSLuiz Augusto von Dentz 	/* Resume monitor filters */
6041182ee45dSLuiz Augusto von Dentz 	hci_resume_monitor_sync(hdev);
6042182ee45dSLuiz Augusto von Dentz 
6043182ee45dSLuiz Augusto von Dentz 	/* Resume other advertisements */
6044182ee45dSLuiz Augusto von Dentz 	hci_resume_advertising_sync(hdev);
6045182ee45dSLuiz Augusto von Dentz 
6046182ee45dSLuiz Augusto von Dentz 	/* Resume discovery */
6047182ee45dSLuiz Augusto von Dentz 	hci_resume_discovery_sync(hdev);
6048182ee45dSLuiz Augusto von Dentz 
6049182ee45dSLuiz Augusto von Dentz 	return 0;
6050182ee45dSLuiz Augusto von Dentz }
60518e8b92eeSLuiz Augusto von Dentz 
60528e8b92eeSLuiz Augusto von Dentz static bool conn_use_rpa(struct hci_conn *conn)
60538e8b92eeSLuiz Augusto von Dentz {
60548e8b92eeSLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
60558e8b92eeSLuiz Augusto von Dentz 
60568e8b92eeSLuiz Augusto von Dentz 	return hci_dev_test_flag(hdev, HCI_PRIVACY);
60578e8b92eeSLuiz Augusto von Dentz }
60588e8b92eeSLuiz Augusto von Dentz 
60598e8b92eeSLuiz Augusto von Dentz static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
60608e8b92eeSLuiz Augusto von Dentz 						struct hci_conn *conn)
60618e8b92eeSLuiz Augusto von Dentz {
60628e8b92eeSLuiz Augusto von Dentz 	struct hci_cp_le_set_ext_adv_params cp;
60638e8b92eeSLuiz Augusto von Dentz 	int err;
60648e8b92eeSLuiz Augusto von Dentz 	bdaddr_t random_addr;
60658e8b92eeSLuiz Augusto von Dentz 	u8 own_addr_type;
60668e8b92eeSLuiz Augusto von Dentz 
60678e8b92eeSLuiz Augusto von Dentz 	err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
60688e8b92eeSLuiz Augusto von Dentz 					     &own_addr_type);
60698e8b92eeSLuiz Augusto von Dentz 	if (err)
60708e8b92eeSLuiz Augusto von Dentz 		return err;
60718e8b92eeSLuiz Augusto von Dentz 
60728e8b92eeSLuiz Augusto von Dentz 	/* Set require_privacy to false so that the remote device has a
60738e8b92eeSLuiz Augusto von Dentz 	 * chance of identifying us.
60748e8b92eeSLuiz Augusto von Dentz 	 */
60758e8b92eeSLuiz Augusto von Dentz 	err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
60768e8b92eeSLuiz Augusto von Dentz 				     &own_addr_type, &random_addr);
60778e8b92eeSLuiz Augusto von Dentz 	if (err)
60788e8b92eeSLuiz Augusto von Dentz 		return err;
60798e8b92eeSLuiz Augusto von Dentz 
60808e8b92eeSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
60818e8b92eeSLuiz Augusto von Dentz 
60828e8b92eeSLuiz Augusto von Dentz 	cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
60838e8b92eeSLuiz Augusto von Dentz 	cp.channel_map = hdev->le_adv_channel_map;
60848e8b92eeSLuiz Augusto von Dentz 	cp.tx_power = HCI_TX_POWER_INVALID;
60858e8b92eeSLuiz Augusto von Dentz 	cp.primary_phy = HCI_ADV_PHY_1M;
60868e8b92eeSLuiz Augusto von Dentz 	cp.secondary_phy = HCI_ADV_PHY_1M;
60878e8b92eeSLuiz Augusto von Dentz 	cp.handle = 0x00; /* Use instance 0 for directed adv */
60888e8b92eeSLuiz Augusto von Dentz 	cp.own_addr_type = own_addr_type;
60898e8b92eeSLuiz Augusto von Dentz 	cp.peer_addr_type = conn->dst_type;
60908e8b92eeSLuiz Augusto von Dentz 	bacpy(&cp.peer_addr, &conn->dst);
60918e8b92eeSLuiz Augusto von Dentz 
60928e8b92eeSLuiz Augusto von Dentz 	/* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
60938e8b92eeSLuiz Augusto von Dentz 	 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
60948e8b92eeSLuiz Augusto von Dentz 	 * does not supports advertising data when the advertising set already
60958e8b92eeSLuiz Augusto von Dentz 	 * contains some, the controller shall return erroc code 'Invalid
60968e8b92eeSLuiz Augusto von Dentz 	 * HCI Command Parameters(0x12).
60978e8b92eeSLuiz Augusto von Dentz 	 * So it is required to remove adv set for handle 0x00. since we use
60988e8b92eeSLuiz Augusto von Dentz 	 * instance 0 for directed adv.
60998e8b92eeSLuiz Augusto von Dentz 	 */
61008e8b92eeSLuiz Augusto von Dentz 	err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
61018e8b92eeSLuiz Augusto von Dentz 	if (err)
61028e8b92eeSLuiz Augusto von Dentz 		return err;
61038e8b92eeSLuiz Augusto von Dentz 
61048e8b92eeSLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
61058e8b92eeSLuiz Augusto von Dentz 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
61068e8b92eeSLuiz Augusto von Dentz 	if (err)
61078e8b92eeSLuiz Augusto von Dentz 		return err;
61088e8b92eeSLuiz Augusto von Dentz 
61098e8b92eeSLuiz Augusto von Dentz 	/* Check if random address need to be updated */
61108e8b92eeSLuiz Augusto von Dentz 	if (own_addr_type == ADDR_LE_DEV_RANDOM &&
61118e8b92eeSLuiz Augusto von Dentz 	    bacmp(&random_addr, BDADDR_ANY) &&
61128e8b92eeSLuiz Augusto von Dentz 	    bacmp(&random_addr, &hdev->random_addr)) {
61138e8b92eeSLuiz Augusto von Dentz 		err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
61148e8b92eeSLuiz Augusto von Dentz 						       &random_addr);
61158e8b92eeSLuiz Augusto von Dentz 		if (err)
61168e8b92eeSLuiz Augusto von Dentz 			return err;
61178e8b92eeSLuiz Augusto von Dentz 	}
61188e8b92eeSLuiz Augusto von Dentz 
61198e8b92eeSLuiz Augusto von Dentz 	return hci_enable_ext_advertising_sync(hdev, 0x00);
61208e8b92eeSLuiz Augusto von Dentz }
61218e8b92eeSLuiz Augusto von Dentz 
61228e8b92eeSLuiz Augusto von Dentz static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
61238e8b92eeSLuiz Augusto von Dentz 					    struct hci_conn *conn)
61248e8b92eeSLuiz Augusto von Dentz {
61258e8b92eeSLuiz Augusto von Dentz 	struct hci_cp_le_set_adv_param cp;
61268e8b92eeSLuiz Augusto von Dentz 	u8 status;
61278e8b92eeSLuiz Augusto von Dentz 	u8 own_addr_type;
61288e8b92eeSLuiz Augusto von Dentz 	u8 enable;
61298e8b92eeSLuiz Augusto von Dentz 
61308e8b92eeSLuiz Augusto von Dentz 	if (ext_adv_capable(hdev))
61318e8b92eeSLuiz Augusto von Dentz 		return hci_le_ext_directed_advertising_sync(hdev, conn);
61328e8b92eeSLuiz Augusto von Dentz 
61338e8b92eeSLuiz Augusto von Dentz 	/* Clear the HCI_LE_ADV bit temporarily so that the
61348e8b92eeSLuiz Augusto von Dentz 	 * hci_update_random_address knows that it's safe to go ahead
61358e8b92eeSLuiz Augusto von Dentz 	 * and write a new random address. The flag will be set back on
61368e8b92eeSLuiz Augusto von Dentz 	 * as soon as the SET_ADV_ENABLE HCI command completes.
61378e8b92eeSLuiz Augusto von Dentz 	 */
61388e8b92eeSLuiz Augusto von Dentz 	hci_dev_clear_flag(hdev, HCI_LE_ADV);
61398e8b92eeSLuiz Augusto von Dentz 
61408e8b92eeSLuiz Augusto von Dentz 	/* Set require_privacy to false so that the remote device has a
61418e8b92eeSLuiz Augusto von Dentz 	 * chance of identifying us.
61428e8b92eeSLuiz Augusto von Dentz 	 */
61438e8b92eeSLuiz Augusto von Dentz 	status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
61448e8b92eeSLuiz Augusto von Dentz 						&own_addr_type);
61458e8b92eeSLuiz Augusto von Dentz 	if (status)
61468e8b92eeSLuiz Augusto von Dentz 		return status;
61478e8b92eeSLuiz Augusto von Dentz 
61488e8b92eeSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
61498e8b92eeSLuiz Augusto von Dentz 
61508e8b92eeSLuiz Augusto von Dentz 	/* Some controllers might reject command if intervals are not
61518e8b92eeSLuiz Augusto von Dentz 	 * within range for undirected advertising.
61528e8b92eeSLuiz Augusto von Dentz 	 * BCM20702A0 is known to be affected by this.
61538e8b92eeSLuiz Augusto von Dentz 	 */
61548e8b92eeSLuiz Augusto von Dentz 	cp.min_interval = cpu_to_le16(0x0020);
61558e8b92eeSLuiz Augusto von Dentz 	cp.max_interval = cpu_to_le16(0x0020);
61568e8b92eeSLuiz Augusto von Dentz 
61578e8b92eeSLuiz Augusto von Dentz 	cp.type = LE_ADV_DIRECT_IND;
61588e8b92eeSLuiz Augusto von Dentz 	cp.own_address_type = own_addr_type;
61598e8b92eeSLuiz Augusto von Dentz 	cp.direct_addr_type = conn->dst_type;
61608e8b92eeSLuiz Augusto von Dentz 	bacpy(&cp.direct_addr, &conn->dst);
61618e8b92eeSLuiz Augusto von Dentz 	cp.channel_map = hdev->le_adv_channel_map;
61628e8b92eeSLuiz Augusto von Dentz 
61638e8b92eeSLuiz Augusto von Dentz 	status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
61648e8b92eeSLuiz Augusto von Dentz 				       sizeof(cp), &cp, HCI_CMD_TIMEOUT);
61658e8b92eeSLuiz Augusto von Dentz 	if (status)
61668e8b92eeSLuiz Augusto von Dentz 		return status;
61678e8b92eeSLuiz Augusto von Dentz 
61688e8b92eeSLuiz Augusto von Dentz 	enable = 0x01;
61698e8b92eeSLuiz Augusto von Dentz 
61708e8b92eeSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
61718e8b92eeSLuiz Augusto von Dentz 				     sizeof(enable), &enable, HCI_CMD_TIMEOUT);
61728e8b92eeSLuiz Augusto von Dentz }
61738e8b92eeSLuiz Augusto von Dentz 
61748e8b92eeSLuiz Augusto von Dentz static void set_ext_conn_params(struct hci_conn *conn,
61758e8b92eeSLuiz Augusto von Dentz 				struct hci_cp_le_ext_conn_param *p)
61768e8b92eeSLuiz Augusto von Dentz {
61778e8b92eeSLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
61788e8b92eeSLuiz Augusto von Dentz 
61798e8b92eeSLuiz Augusto von Dentz 	memset(p, 0, sizeof(*p));
61808e8b92eeSLuiz Augusto von Dentz 
61818e8b92eeSLuiz Augusto von Dentz 	p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
61828e8b92eeSLuiz Augusto von Dentz 	p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
61838e8b92eeSLuiz Augusto von Dentz 	p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
61848e8b92eeSLuiz Augusto von Dentz 	p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
61858e8b92eeSLuiz Augusto von Dentz 	p->conn_latency = cpu_to_le16(conn->le_conn_latency);
61868e8b92eeSLuiz Augusto von Dentz 	p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
61878e8b92eeSLuiz Augusto von Dentz 	p->min_ce_len = cpu_to_le16(0x0000);
61888e8b92eeSLuiz Augusto von Dentz 	p->max_ce_len = cpu_to_le16(0x0000);
61898e8b92eeSLuiz Augusto von Dentz }
61908e8b92eeSLuiz Augusto von Dentz 
619189a0b8b9SLuiz Augusto von Dentz static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
619289a0b8b9SLuiz Augusto von Dentz 				       struct hci_conn *conn, u8 own_addr_type)
61938e8b92eeSLuiz Augusto von Dentz {
61948e8b92eeSLuiz Augusto von Dentz 	struct hci_cp_le_ext_create_conn *cp;
61958e8b92eeSLuiz Augusto von Dentz 	struct hci_cp_le_ext_conn_param *p;
61968e8b92eeSLuiz Augusto von Dentz 	u8 data[sizeof(*cp) + sizeof(*p) * 3];
61978e8b92eeSLuiz Augusto von Dentz 	u32 plen;
61988e8b92eeSLuiz Augusto von Dentz 
61998e8b92eeSLuiz Augusto von Dentz 	cp = (void *)data;
62008e8b92eeSLuiz Augusto von Dentz 	p = (void *)cp->data;
62018e8b92eeSLuiz Augusto von Dentz 
62028e8b92eeSLuiz Augusto von Dentz 	memset(cp, 0, sizeof(*cp));
62038e8b92eeSLuiz Augusto von Dentz 
62048e8b92eeSLuiz Augusto von Dentz 	bacpy(&cp->peer_addr, &conn->dst);
62058e8b92eeSLuiz Augusto von Dentz 	cp->peer_addr_type = conn->dst_type;
62068e8b92eeSLuiz Augusto von Dentz 	cp->own_addr_type = own_addr_type;
62078e8b92eeSLuiz Augusto von Dentz 
62088e8b92eeSLuiz Augusto von Dentz 	plen = sizeof(*cp);
62098e8b92eeSLuiz Augusto von Dentz 
62108e8b92eeSLuiz Augusto von Dentz 	if (scan_1m(hdev)) {
62118e8b92eeSLuiz Augusto von Dentz 		cp->phys |= LE_SCAN_PHY_1M;
62128e8b92eeSLuiz Augusto von Dentz 		set_ext_conn_params(conn, p);
62138e8b92eeSLuiz Augusto von Dentz 
62148e8b92eeSLuiz Augusto von Dentz 		p++;
62158e8b92eeSLuiz Augusto von Dentz 		plen += sizeof(*p);
62168e8b92eeSLuiz Augusto von Dentz 	}
62178e8b92eeSLuiz Augusto von Dentz 
62188e8b92eeSLuiz Augusto von Dentz 	if (scan_2m(hdev)) {
62198e8b92eeSLuiz Augusto von Dentz 		cp->phys |= LE_SCAN_PHY_2M;
62208e8b92eeSLuiz Augusto von Dentz 		set_ext_conn_params(conn, p);
62218e8b92eeSLuiz Augusto von Dentz 
62228e8b92eeSLuiz Augusto von Dentz 		p++;
62238e8b92eeSLuiz Augusto von Dentz 		plen += sizeof(*p);
62248e8b92eeSLuiz Augusto von Dentz 	}
62258e8b92eeSLuiz Augusto von Dentz 
62268e8b92eeSLuiz Augusto von Dentz 	if (scan_coded(hdev)) {
62278e8b92eeSLuiz Augusto von Dentz 		cp->phys |= LE_SCAN_PHY_CODED;
62288e8b92eeSLuiz Augusto von Dentz 		set_ext_conn_params(conn, p);
62298e8b92eeSLuiz Augusto von Dentz 
62308e8b92eeSLuiz Augusto von Dentz 		plen += sizeof(*p);
62318e8b92eeSLuiz Augusto von Dentz 	}
62328e8b92eeSLuiz Augusto von Dentz 
62336cd29ec6SLuiz Augusto von Dentz 	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
62346cd29ec6SLuiz Augusto von Dentz 					plen, data,
62356cd29ec6SLuiz Augusto von Dentz 					HCI_EV_LE_ENHANCED_CONN_COMPLETE,
6236a56a1138SLuiz Augusto von Dentz 					conn->conn_timeout, NULL);
62378e8b92eeSLuiz Augusto von Dentz }
62388e8b92eeSLuiz Augusto von Dentz 
62398e8b92eeSLuiz Augusto von Dentz int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
62408e8b92eeSLuiz Augusto von Dentz {
62418e8b92eeSLuiz Augusto von Dentz 	struct hci_cp_le_create_conn cp;
62428e8b92eeSLuiz Augusto von Dentz 	struct hci_conn_params *params;
62438e8b92eeSLuiz Augusto von Dentz 	u8 own_addr_type;
62448e8b92eeSLuiz Augusto von Dentz 	int err;
62458e8b92eeSLuiz Augusto von Dentz 
62468e8b92eeSLuiz Augusto von Dentz 	/* If requested to connect as peripheral use directed advertising */
62478e8b92eeSLuiz Augusto von Dentz 	if (conn->role == HCI_ROLE_SLAVE) {
624876d0685bSLuiz Augusto von Dentz 		/* If we're active scanning and simultaneous roles is not
624976d0685bSLuiz Augusto von Dentz 		 * enabled simply reject the attempt.
62508e8b92eeSLuiz Augusto von Dentz 		 */
62518e8b92eeSLuiz Augusto von Dentz 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
62524fc9857aSLuiz Augusto von Dentz 		    hdev->le_scan_type == LE_SCAN_ACTIVE &&
625376d0685bSLuiz Augusto von Dentz 		    !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
62548e8b92eeSLuiz Augusto von Dentz 			hci_conn_del(conn);
62558e8b92eeSLuiz Augusto von Dentz 			return -EBUSY;
62568e8b92eeSLuiz Augusto von Dentz 		}
62578e8b92eeSLuiz Augusto von Dentz 
62584fc9857aSLuiz Augusto von Dentz 		/* Pause advertising while doing directed advertising. */
62594fc9857aSLuiz Augusto von Dentz 		hci_pause_advertising_sync(hdev);
62604fc9857aSLuiz Augusto von Dentz 
62618e8b92eeSLuiz Augusto von Dentz 		err = hci_le_directed_advertising_sync(hdev, conn);
62628e8b92eeSLuiz Augusto von Dentz 		goto done;
62638e8b92eeSLuiz Augusto von Dentz 	}
62648e8b92eeSLuiz Augusto von Dentz 
626576d0685bSLuiz Augusto von Dentz 	/* Disable advertising if simultaneous roles is not in use. */
626676d0685bSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
62674fc9857aSLuiz Augusto von Dentz 		hci_pause_advertising_sync(hdev);
62684fc9857aSLuiz Augusto von Dentz 
62698e8b92eeSLuiz Augusto von Dentz 	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
62708e8b92eeSLuiz Augusto von Dentz 	if (params) {
62718e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_min_interval = params->conn_min_interval;
62728e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_max_interval = params->conn_max_interval;
62738e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_latency = params->conn_latency;
62748e8b92eeSLuiz Augusto von Dentz 		conn->le_supv_timeout = params->supervision_timeout;
62758e8b92eeSLuiz Augusto von Dentz 	} else {
62768e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_min_interval = hdev->le_conn_min_interval;
62778e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_max_interval = hdev->le_conn_max_interval;
62788e8b92eeSLuiz Augusto von Dentz 		conn->le_conn_latency = hdev->le_conn_latency;
62798e8b92eeSLuiz Augusto von Dentz 		conn->le_supv_timeout = hdev->le_supv_timeout;
62808e8b92eeSLuiz Augusto von Dentz 	}
62818e8b92eeSLuiz Augusto von Dentz 
62828e8b92eeSLuiz Augusto von Dentz 	/* If controller is scanning, we stop it since some controllers are
62838e8b92eeSLuiz Augusto von Dentz 	 * not able to scan and connect at the same time. Also set the
62848e8b92eeSLuiz Augusto von Dentz 	 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
62858e8b92eeSLuiz Augusto von Dentz 	 * handler for scan disabling knows to set the correct discovery
62868e8b92eeSLuiz Augusto von Dentz 	 * state.
62878e8b92eeSLuiz Augusto von Dentz 	 */
62888e8b92eeSLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
62898e8b92eeSLuiz Augusto von Dentz 		hci_scan_disable_sync(hdev);
62908e8b92eeSLuiz Augusto von Dentz 		hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
62918e8b92eeSLuiz Augusto von Dentz 	}
62928e8b92eeSLuiz Augusto von Dentz 
62938e8b92eeSLuiz Augusto von Dentz 	/* Update random address, but set require_privacy to false so
62948e8b92eeSLuiz Augusto von Dentz 	 * that we never connect with an non-resolvable address.
62958e8b92eeSLuiz Augusto von Dentz 	 */
62968e8b92eeSLuiz Augusto von Dentz 	err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
62978e8b92eeSLuiz Augusto von Dentz 					     &own_addr_type);
62988e8b92eeSLuiz Augusto von Dentz 	if (err)
62998e8b92eeSLuiz Augusto von Dentz 		goto done;
63008e8b92eeSLuiz Augusto von Dentz 
63018e8b92eeSLuiz Augusto von Dentz 	if (use_ext_conn(hdev)) {
63028e8b92eeSLuiz Augusto von Dentz 		err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
63038e8b92eeSLuiz Augusto von Dentz 		goto done;
63048e8b92eeSLuiz Augusto von Dentz 	}
63058e8b92eeSLuiz Augusto von Dentz 
63068e8b92eeSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
63078e8b92eeSLuiz Augusto von Dentz 
63088e8b92eeSLuiz Augusto von Dentz 	cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
63098e8b92eeSLuiz Augusto von Dentz 	cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
63108e8b92eeSLuiz Augusto von Dentz 
63118e8b92eeSLuiz Augusto von Dentz 	bacpy(&cp.peer_addr, &conn->dst);
63128e8b92eeSLuiz Augusto von Dentz 	cp.peer_addr_type = conn->dst_type;
63138e8b92eeSLuiz Augusto von Dentz 	cp.own_address_type = own_addr_type;
63148e8b92eeSLuiz Augusto von Dentz 	cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
63158e8b92eeSLuiz Augusto von Dentz 	cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
63168e8b92eeSLuiz Augusto von Dentz 	cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
63178e8b92eeSLuiz Augusto von Dentz 	cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
63188e8b92eeSLuiz Augusto von Dentz 	cp.min_ce_len = cpu_to_le16(0x0000);
63198e8b92eeSLuiz Augusto von Dentz 	cp.max_ce_len = cpu_to_le16(0x0000);
63208e8b92eeSLuiz Augusto von Dentz 
6321a56a1138SLuiz Augusto von Dentz 	/* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6322a56a1138SLuiz Augusto von Dentz 	 *
6323a56a1138SLuiz Augusto von Dentz 	 * If this event is unmasked and the HCI_LE_Connection_Complete event
6324a56a1138SLuiz Augusto von Dentz 	 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6325a56a1138SLuiz Augusto von Dentz 	 * sent when a new connection has been created.
6326a56a1138SLuiz Augusto von Dentz 	 */
63276cd29ec6SLuiz Augusto von Dentz 	err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6328a56a1138SLuiz Augusto von Dentz 				       sizeof(cp), &cp,
6329a56a1138SLuiz Augusto von Dentz 				       use_enhanced_conn_complete(hdev) ?
6330a56a1138SLuiz Augusto von Dentz 				       HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6331a56a1138SLuiz Augusto von Dentz 				       HCI_EV_LE_CONN_COMPLETE,
6332a56a1138SLuiz Augusto von Dentz 				       conn->conn_timeout, NULL);
63338e8b92eeSLuiz Augusto von Dentz 
63348e8b92eeSLuiz Augusto von Dentz done:
6335b62e7220SLuiz Augusto von Dentz 	if (err == -ETIMEDOUT)
6336a13f316eSLuiz Augusto von Dentz 		hci_le_connect_cancel_sync(hdev, conn, 0x00);
6337b62e7220SLuiz Augusto von Dentz 
63384fc9857aSLuiz Augusto von Dentz 	/* Re-enable advertising after the connection attempt is finished. */
63398e8b92eeSLuiz Augusto von Dentz 	hci_resume_advertising_sync(hdev);
63408e8b92eeSLuiz Augusto von Dentz 	return err;
63418e8b92eeSLuiz Augusto von Dentz }
634226afbd82SLuiz Augusto von Dentz 
63437f74563eSPauli Virtanen int hci_le_create_cis_sync(struct hci_dev *hdev)
6344c09b80beSLuiz Augusto von Dentz {
6345c09b80beSLuiz Augusto von Dentz 	struct {
6346c09b80beSLuiz Augusto von Dentz 		struct hci_cp_le_create_cis cp;
6347c09b80beSLuiz Augusto von Dentz 		struct hci_cis cis[0x1f];
6348c09b80beSLuiz Augusto von Dentz 	} cmd;
63497f74563eSPauli Virtanen 	struct hci_conn *conn;
63507f74563eSPauli Virtanen 	u8 cig = BT_ISO_QOS_CIG_UNSET;
6351c09b80beSLuiz Augusto von Dentz 
63527f74563eSPauli Virtanen 	/* The spec allows only one pending LE Create CIS command at a time. If
63537f74563eSPauli Virtanen 	 * the command is pending now, don't do anything. We check for pending
63547f74563eSPauli Virtanen 	 * connections after each CIS Established event.
6355c09b80beSLuiz Augusto von Dentz 	 *
6356c09b80beSLuiz Augusto von Dentz 	 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6357c09b80beSLuiz Augusto von Dentz 	 * page 2566:
6358c09b80beSLuiz Augusto von Dentz 	 *
6359c09b80beSLuiz Augusto von Dentz 	 * If the Host issues this command before all the
6360c09b80beSLuiz Augusto von Dentz 	 * HCI_LE_CIS_Established events from the previous use of the
6361c09b80beSLuiz Augusto von Dentz 	 * command have been generated, the Controller shall return the
6362c09b80beSLuiz Augusto von Dentz 	 * error code Command Disallowed (0x0C).
63637f74563eSPauli Virtanen 	 *
63647f74563eSPauli Virtanen 	 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
63657f74563eSPauli Virtanen 	 * page 2567:
63667f74563eSPauli Virtanen 	 *
63677f74563eSPauli Virtanen 	 * When the Controller receives the HCI_LE_Create_CIS command, the
63687f74563eSPauli Virtanen 	 * Controller sends the HCI_Command_Status event to the Host. An
63697f74563eSPauli Virtanen 	 * HCI_LE_CIS_Established event will be generated for each CIS when it
63707f74563eSPauli Virtanen 	 * is established or if it is disconnected or considered lost before
63717f74563eSPauli Virtanen 	 * being established; until all the events are generated, the command
63727f74563eSPauli Virtanen 	 * remains pending.
6373c09b80beSLuiz Augusto von Dentz 	 */
63747f74563eSPauli Virtanen 
63757f74563eSPauli Virtanen 	memset(&cmd, 0, sizeof(cmd));
63767f74563eSPauli Virtanen 
63777f74563eSPauli Virtanen 	hci_dev_lock(hdev);
63787f74563eSPauli Virtanen 
63797f74563eSPauli Virtanen 	rcu_read_lock();
63807f74563eSPauli Virtanen 
63817f74563eSPauli Virtanen 	/* Wait until previous Create CIS has completed */
63827f74563eSPauli Virtanen 	list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
63837f74563eSPauli Virtanen 		if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
63847f74563eSPauli Virtanen 			goto done;
63857f74563eSPauli Virtanen 	}
63867f74563eSPauli Virtanen 
63877f74563eSPauli Virtanen 	/* Find CIG with all CIS ready */
63887f74563eSPauli Virtanen 	list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
63897f74563eSPauli Virtanen 		struct hci_conn *link;
63907f74563eSPauli Virtanen 
63917f74563eSPauli Virtanen 		if (hci_conn_check_create_cis(conn))
63927f74563eSPauli Virtanen 			continue;
63937f74563eSPauli Virtanen 
63947f74563eSPauli Virtanen 		cig = conn->iso_qos.ucast.cig;
63957f74563eSPauli Virtanen 
63967f74563eSPauli Virtanen 		list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
63977f74563eSPauli Virtanen 			if (hci_conn_check_create_cis(link) > 0 &&
63987f74563eSPauli Virtanen 			    link->iso_qos.ucast.cig == cig &&
63997f74563eSPauli Virtanen 			    link->state != BT_CONNECTED) {
64007f74563eSPauli Virtanen 				cig = BT_ISO_QOS_CIG_UNSET;
64017f74563eSPauli Virtanen 				break;
64027f74563eSPauli Virtanen 			}
64037f74563eSPauli Virtanen 		}
64047f74563eSPauli Virtanen 
64057f74563eSPauli Virtanen 		if (cig != BT_ISO_QOS_CIG_UNSET)
64067f74563eSPauli Virtanen 			break;
64077f74563eSPauli Virtanen 	}
64087f74563eSPauli Virtanen 
64097f74563eSPauli Virtanen 	if (cig == BT_ISO_QOS_CIG_UNSET)
64107f74563eSPauli Virtanen 		goto done;
64117f74563eSPauli Virtanen 
64127f74563eSPauli Virtanen 	list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
64137f74563eSPauli Virtanen 		struct hci_cis *cis = &cmd.cis[cmd.cp.num_cis];
64147f74563eSPauli Virtanen 
64157f74563eSPauli Virtanen 		if (hci_conn_check_create_cis(conn) ||
64167f74563eSPauli Virtanen 		    conn->iso_qos.ucast.cig != cig)
64177f74563eSPauli Virtanen 			continue;
64187f74563eSPauli Virtanen 
64197f74563eSPauli Virtanen 		set_bit(HCI_CONN_CREATE_CIS, &conn->flags);
6420c09b80beSLuiz Augusto von Dentz 		cis->acl_handle = cpu_to_le16(conn->parent->handle);
6421c09b80beSLuiz Augusto von Dentz 		cis->cis_handle = cpu_to_le16(conn->handle);
6422c09b80beSLuiz Augusto von Dentz 		cmd.cp.num_cis++;
64237f74563eSPauli Virtanen 
64247f74563eSPauli Virtanen 		if (cmd.cp.num_cis >= ARRAY_SIZE(cmd.cis))
64257f74563eSPauli Virtanen 			break;
6426c09b80beSLuiz Augusto von Dentz 	}
6427c09b80beSLuiz Augusto von Dentz 
64287f74563eSPauli Virtanen done:
6429c09b80beSLuiz Augusto von Dentz 	rcu_read_unlock();
6430c09b80beSLuiz Augusto von Dentz 
6431c09b80beSLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
6432c09b80beSLuiz Augusto von Dentz 
6433c09b80beSLuiz Augusto von Dentz 	if (!cmd.cp.num_cis)
6434c09b80beSLuiz Augusto von Dentz 		return 0;
6435c09b80beSLuiz Augusto von Dentz 
6436c09b80beSLuiz Augusto von Dentz 	/* Wait for HCI_LE_CIS_Established */
6437c09b80beSLuiz Augusto von Dentz 	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6438c09b80beSLuiz Augusto von Dentz 					sizeof(cmd.cp) + sizeof(cmd.cis[0]) *
6439c09b80beSLuiz Augusto von Dentz 					cmd.cp.num_cis, &cmd,
6440c09b80beSLuiz Augusto von Dentz 					HCI_EVT_LE_CIS_ESTABLISHED,
6441c09b80beSLuiz Augusto von Dentz 					conn->conn_timeout, NULL);
6442c09b80beSLuiz Augusto von Dentz }
6443c09b80beSLuiz Augusto von Dentz 
644426afbd82SLuiz Augusto von Dentz int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
644526afbd82SLuiz Augusto von Dentz {
644626afbd82SLuiz Augusto von Dentz 	struct hci_cp_le_remove_cig cp;
644726afbd82SLuiz Augusto von Dentz 
644826afbd82SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
644926afbd82SLuiz Augusto von Dentz 	cp.cig_id = handle;
645026afbd82SLuiz Augusto von Dentz 
645126afbd82SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
645226afbd82SLuiz Augusto von Dentz 				     &cp, HCI_CMD_TIMEOUT);
645326afbd82SLuiz Augusto von Dentz }
6454eca0ae4aSLuiz Augusto von Dentz 
6455eca0ae4aSLuiz Augusto von Dentz int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6456eca0ae4aSLuiz Augusto von Dentz {
6457eca0ae4aSLuiz Augusto von Dentz 	struct hci_cp_le_big_term_sync cp;
6458eca0ae4aSLuiz Augusto von Dentz 
6459eca0ae4aSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
6460eca0ae4aSLuiz Augusto von Dentz 	cp.handle = handle;
6461eca0ae4aSLuiz Augusto von Dentz 
6462eca0ae4aSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6463eca0ae4aSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6464eca0ae4aSLuiz Augusto von Dentz }
6465eca0ae4aSLuiz Augusto von Dentz 
6466eca0ae4aSLuiz Augusto von Dentz int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6467eca0ae4aSLuiz Augusto von Dentz {
6468eca0ae4aSLuiz Augusto von Dentz 	struct hci_cp_le_pa_term_sync cp;
6469eca0ae4aSLuiz Augusto von Dentz 
6470eca0ae4aSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
6471eca0ae4aSLuiz Augusto von Dentz 	cp.handle = cpu_to_le16(handle);
6472eca0ae4aSLuiz Augusto von Dentz 
6473eca0ae4aSLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6474eca0ae4aSLuiz Augusto von Dentz 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6475eca0ae4aSLuiz Augusto von Dentz }
64763fe318eeSBrian Gix 
64773fe318eeSBrian Gix int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
64783fe318eeSBrian Gix 			   bool use_rpa, struct adv_info *adv_instance,
64793fe318eeSBrian Gix 			   u8 *own_addr_type, bdaddr_t *rand_addr)
64803fe318eeSBrian Gix {
64813fe318eeSBrian Gix 	int err;
64823fe318eeSBrian Gix 
64833fe318eeSBrian Gix 	bacpy(rand_addr, BDADDR_ANY);
64843fe318eeSBrian Gix 
64853fe318eeSBrian Gix 	/* If privacy is enabled use a resolvable private address. If
64863fe318eeSBrian Gix 	 * current RPA has expired then generate a new one.
64873fe318eeSBrian Gix 	 */
64883fe318eeSBrian Gix 	if (use_rpa) {
64893fe318eeSBrian Gix 		/* If Controller supports LL Privacy use own address type is
64903fe318eeSBrian Gix 		 * 0x03
64913fe318eeSBrian Gix 		 */
64923fe318eeSBrian Gix 		if (use_ll_privacy(hdev))
64933fe318eeSBrian Gix 			*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
64943fe318eeSBrian Gix 		else
64953fe318eeSBrian Gix 			*own_addr_type = ADDR_LE_DEV_RANDOM;
64963fe318eeSBrian Gix 
64973fe318eeSBrian Gix 		if (adv_instance) {
64983fe318eeSBrian Gix 			if (adv_rpa_valid(adv_instance))
64993fe318eeSBrian Gix 				return 0;
65003fe318eeSBrian Gix 		} else {
65013fe318eeSBrian Gix 			if (rpa_valid(hdev))
65023fe318eeSBrian Gix 				return 0;
65033fe318eeSBrian Gix 		}
65043fe318eeSBrian Gix 
65053fe318eeSBrian Gix 		err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
65063fe318eeSBrian Gix 		if (err < 0) {
65073fe318eeSBrian Gix 			bt_dev_err(hdev, "failed to generate new RPA");
65083fe318eeSBrian Gix 			return err;
65093fe318eeSBrian Gix 		}
65103fe318eeSBrian Gix 
65113fe318eeSBrian Gix 		bacpy(rand_addr, &hdev->rpa);
65123fe318eeSBrian Gix 
65133fe318eeSBrian Gix 		return 0;
65143fe318eeSBrian Gix 	}
65153fe318eeSBrian Gix 
65163fe318eeSBrian Gix 	/* In case of required privacy without resolvable private address,
65173fe318eeSBrian Gix 	 * use an non-resolvable private address. This is useful for
65183fe318eeSBrian Gix 	 * non-connectable advertising.
65193fe318eeSBrian Gix 	 */
65203fe318eeSBrian Gix 	if (require_privacy) {
65213fe318eeSBrian Gix 		bdaddr_t nrpa;
65223fe318eeSBrian Gix 
65233fe318eeSBrian Gix 		while (true) {
65243fe318eeSBrian Gix 			/* The non-resolvable private address is generated
65253fe318eeSBrian Gix 			 * from random six bytes with the two most significant
65263fe318eeSBrian Gix 			 * bits cleared.
65273fe318eeSBrian Gix 			 */
65283fe318eeSBrian Gix 			get_random_bytes(&nrpa, 6);
65293fe318eeSBrian Gix 			nrpa.b[5] &= 0x3f;
65303fe318eeSBrian Gix 
65313fe318eeSBrian Gix 			/* The non-resolvable private address shall not be
65323fe318eeSBrian Gix 			 * equal to the public address.
65333fe318eeSBrian Gix 			 */
65343fe318eeSBrian Gix 			if (bacmp(&hdev->bdaddr, &nrpa))
65353fe318eeSBrian Gix 				break;
65363fe318eeSBrian Gix 		}
65373fe318eeSBrian Gix 
65383fe318eeSBrian Gix 		*own_addr_type = ADDR_LE_DEV_RANDOM;
65393fe318eeSBrian Gix 		bacpy(rand_addr, &nrpa);
65403fe318eeSBrian Gix 
65413fe318eeSBrian Gix 		return 0;
65423fe318eeSBrian Gix 	}
65433fe318eeSBrian Gix 
65443fe318eeSBrian Gix 	/* No privacy so use a public address. */
65453fe318eeSBrian Gix 	*own_addr_type = ADDR_LE_DEV_PUBLIC;
65463fe318eeSBrian Gix 
65473fe318eeSBrian Gix 	return 0;
65483fe318eeSBrian Gix }
6549651cd3d6SBrian Gix 
6550651cd3d6SBrian Gix static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6551651cd3d6SBrian Gix {
65521ed8b37cSZhengchao Shao 	u8 instance = PTR_ERR(data);
6553651cd3d6SBrian Gix 
6554651cd3d6SBrian Gix 	return hci_update_adv_data_sync(hdev, instance);
6555651cd3d6SBrian Gix }
6556651cd3d6SBrian Gix 
6557651cd3d6SBrian Gix int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6558651cd3d6SBrian Gix {
65591ed8b37cSZhengchao Shao 	return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
65601ed8b37cSZhengchao Shao 				  ERR_PTR(instance), NULL);
6561651cd3d6SBrian Gix }
6562