xref: /OK3568_Linux_fs/kernel/net/bluetooth/hci_request.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun    BlueZ - Bluetooth protocol stack for Linux
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun    Copyright (C) 2014 Intel Corporation
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun    This program is free software; you can redistribute it and/or modify
7*4882a593Smuzhiyun    it under the terms of the GNU General Public License version 2 as
8*4882a593Smuzhiyun    published by the Free Software Foundation;
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11*4882a593Smuzhiyun    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*4882a593Smuzhiyun    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13*4882a593Smuzhiyun    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14*4882a593Smuzhiyun    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15*4882a593Smuzhiyun    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16*4882a593Smuzhiyun    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17*4882a593Smuzhiyun    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20*4882a593Smuzhiyun    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21*4882a593Smuzhiyun    SOFTWARE IS DISCLAIMED.
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include <linux/sched/signal.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include <net/bluetooth/bluetooth.h>
27*4882a593Smuzhiyun #include <net/bluetooth/hci_core.h>
28*4882a593Smuzhiyun #include <net/bluetooth/mgmt.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include "smp.h"
31*4882a593Smuzhiyun #include "hci_request.h"
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define HCI_REQ_DONE	  0
34*4882a593Smuzhiyun #define HCI_REQ_PEND	  1
35*4882a593Smuzhiyun #define HCI_REQ_CANCELED  2
36*4882a593Smuzhiyun 
hci_req_init(struct hci_request * req,struct hci_dev * hdev)37*4882a593Smuzhiyun void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	skb_queue_head_init(&req->cmd_q);
40*4882a593Smuzhiyun 	req->hdev = hdev;
41*4882a593Smuzhiyun 	req->err = 0;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
hci_req_purge(struct hci_request * req)44*4882a593Smuzhiyun void hci_req_purge(struct hci_request *req)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	skb_queue_purge(&req->cmd_q);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
hci_req_status_pend(struct hci_dev * hdev)49*4882a593Smuzhiyun bool hci_req_status_pend(struct hci_dev *hdev)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	return hdev->req_status == HCI_REQ_PEND;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
req_run(struct hci_request * req,hci_req_complete_t complete,hci_req_complete_skb_t complete_skb)54*4882a593Smuzhiyun static int req_run(struct hci_request *req, hci_req_complete_t complete,
55*4882a593Smuzhiyun 		   hci_req_complete_skb_t complete_skb)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
58*4882a593Smuzhiyun 	struct sk_buff *skb;
59*4882a593Smuzhiyun 	unsigned long flags;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	BT_DBG("length %u", skb_queue_len(&req->cmd_q));
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	/* If an error occurred during request building, remove all HCI
64*4882a593Smuzhiyun 	 * commands queued on the HCI request queue.
65*4882a593Smuzhiyun 	 */
66*4882a593Smuzhiyun 	if (req->err) {
67*4882a593Smuzhiyun 		skb_queue_purge(&req->cmd_q);
68*4882a593Smuzhiyun 		return req->err;
69*4882a593Smuzhiyun 	}
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	/* Do not allow empty requests */
72*4882a593Smuzhiyun 	if (skb_queue_empty(&req->cmd_q))
73*4882a593Smuzhiyun 		return -ENODATA;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	skb = skb_peek_tail(&req->cmd_q);
76*4882a593Smuzhiyun 	if (complete) {
77*4882a593Smuzhiyun 		bt_cb(skb)->hci.req_complete = complete;
78*4882a593Smuzhiyun 	} else if (complete_skb) {
79*4882a593Smuzhiyun 		bt_cb(skb)->hci.req_complete_skb = complete_skb;
80*4882a593Smuzhiyun 		bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	spin_lock_irqsave(&hdev->cmd_q.lock, flags);
84*4882a593Smuzhiyun 	skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
85*4882a593Smuzhiyun 	spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	queue_work(hdev->workqueue, &hdev->cmd_work);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
hci_req_run(struct hci_request * req,hci_req_complete_t complete)92*4882a593Smuzhiyun int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun 	return req_run(req, complete, NULL);
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
hci_req_run_skb(struct hci_request * req,hci_req_complete_skb_t complete)97*4882a593Smuzhiyun int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	return req_run(req, NULL, complete);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
hci_req_sync_complete(struct hci_dev * hdev,u8 result,u16 opcode,struct sk_buff * skb)102*4882a593Smuzhiyun static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
103*4882a593Smuzhiyun 				  struct sk_buff *skb)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	BT_DBG("%s result 0x%2.2x", hdev->name, result);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	if (hdev->req_status == HCI_REQ_PEND) {
108*4882a593Smuzhiyun 		hdev->req_result = result;
109*4882a593Smuzhiyun 		hdev->req_status = HCI_REQ_DONE;
110*4882a593Smuzhiyun 		if (skb)
111*4882a593Smuzhiyun 			hdev->req_skb = skb_get(skb);
112*4882a593Smuzhiyun 		wake_up_interruptible(&hdev->req_wait_q);
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
hci_req_sync_cancel(struct hci_dev * hdev,int err)116*4882a593Smuzhiyun void hci_req_sync_cancel(struct hci_dev *hdev, int err)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	BT_DBG("%s err 0x%2.2x", hdev->name, err);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	if (hdev->req_status == HCI_REQ_PEND) {
121*4882a593Smuzhiyun 		hdev->req_result = err;
122*4882a593Smuzhiyun 		hdev->req_status = HCI_REQ_CANCELED;
123*4882a593Smuzhiyun 		wake_up_interruptible(&hdev->req_wait_q);
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
__hci_cmd_sync_ev(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u8 event,u32 timeout)127*4882a593Smuzhiyun struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
128*4882a593Smuzhiyun 				  const void *param, u8 event, u32 timeout)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	struct hci_request req;
131*4882a593Smuzhiyun 	struct sk_buff *skb;
132*4882a593Smuzhiyun 	int err = 0;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	BT_DBG("%s", hdev->name);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	hci_req_init(&req, hdev);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	hci_req_add_ev(&req, opcode, plen, param, event);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	hdev->req_status = HCI_REQ_PEND;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	err = hci_req_run_skb(&req, hci_req_sync_complete);
143*4882a593Smuzhiyun 	if (err < 0)
144*4882a593Smuzhiyun 		return ERR_PTR(err);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	err = wait_event_interruptible_timeout(hdev->req_wait_q,
147*4882a593Smuzhiyun 			hdev->req_status != HCI_REQ_PEND, timeout);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	if (err == -ERESTARTSYS)
150*4882a593Smuzhiyun 		return ERR_PTR(-EINTR);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	switch (hdev->req_status) {
153*4882a593Smuzhiyun 	case HCI_REQ_DONE:
154*4882a593Smuzhiyun 		err = -bt_to_errno(hdev->req_result);
155*4882a593Smuzhiyun 		break;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	case HCI_REQ_CANCELED:
158*4882a593Smuzhiyun 		err = -hdev->req_result;
159*4882a593Smuzhiyun 		break;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	default:
162*4882a593Smuzhiyun 		err = -ETIMEDOUT;
163*4882a593Smuzhiyun 		break;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	hdev->req_status = hdev->req_result = 0;
167*4882a593Smuzhiyun 	skb = hdev->req_skb;
168*4882a593Smuzhiyun 	hdev->req_skb = NULL;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	BT_DBG("%s end: err %d", hdev->name, err);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	if (err < 0) {
173*4882a593Smuzhiyun 		kfree_skb(skb);
174*4882a593Smuzhiyun 		return ERR_PTR(err);
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	if (!skb)
178*4882a593Smuzhiyun 		return ERR_PTR(-ENODATA);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	return skb;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun EXPORT_SYMBOL(__hci_cmd_sync_ev);
183*4882a593Smuzhiyun 
__hci_cmd_sync(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)184*4882a593Smuzhiyun struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
185*4882a593Smuzhiyun 			       const void *param, u32 timeout)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun EXPORT_SYMBOL(__hci_cmd_sync);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /* Execute request and wait for completion. */
__hci_req_sync(struct hci_dev * hdev,int (* func)(struct hci_request * req,unsigned long opt),unsigned long opt,u32 timeout,u8 * hci_status)192*4882a593Smuzhiyun int __hci_req_sync(struct hci_dev *hdev, int (*func)(struct hci_request *req,
193*4882a593Smuzhiyun 						     unsigned long opt),
194*4882a593Smuzhiyun 		   unsigned long opt, u32 timeout, u8 *hci_status)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	struct hci_request req;
197*4882a593Smuzhiyun 	int err = 0;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	BT_DBG("%s start", hdev->name);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	hci_req_init(&req, hdev);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	hdev->req_status = HCI_REQ_PEND;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	err = func(&req, opt);
206*4882a593Smuzhiyun 	if (err) {
207*4882a593Smuzhiyun 		if (hci_status)
208*4882a593Smuzhiyun 			*hci_status = HCI_ERROR_UNSPECIFIED;
209*4882a593Smuzhiyun 		return err;
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	err = hci_req_run_skb(&req, hci_req_sync_complete);
213*4882a593Smuzhiyun 	if (err < 0) {
214*4882a593Smuzhiyun 		hdev->req_status = 0;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 		/* ENODATA means the HCI request command queue is empty.
217*4882a593Smuzhiyun 		 * This can happen when a request with conditionals doesn't
218*4882a593Smuzhiyun 		 * trigger any commands to be sent. This is normal behavior
219*4882a593Smuzhiyun 		 * and should not trigger an error return.
220*4882a593Smuzhiyun 		 */
221*4882a593Smuzhiyun 		if (err == -ENODATA) {
222*4882a593Smuzhiyun 			if (hci_status)
223*4882a593Smuzhiyun 				*hci_status = 0;
224*4882a593Smuzhiyun 			return 0;
225*4882a593Smuzhiyun 		}
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 		if (hci_status)
228*4882a593Smuzhiyun 			*hci_status = HCI_ERROR_UNSPECIFIED;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 		return err;
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	err = wait_event_interruptible_timeout(hdev->req_wait_q,
234*4882a593Smuzhiyun 			hdev->req_status != HCI_REQ_PEND, timeout);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	if (err == -ERESTARTSYS)
237*4882a593Smuzhiyun 		return -EINTR;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	switch (hdev->req_status) {
240*4882a593Smuzhiyun 	case HCI_REQ_DONE:
241*4882a593Smuzhiyun 		err = -bt_to_errno(hdev->req_result);
242*4882a593Smuzhiyun 		if (hci_status)
243*4882a593Smuzhiyun 			*hci_status = hdev->req_result;
244*4882a593Smuzhiyun 		break;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	case HCI_REQ_CANCELED:
247*4882a593Smuzhiyun 		err = -hdev->req_result;
248*4882a593Smuzhiyun 		if (hci_status)
249*4882a593Smuzhiyun 			*hci_status = HCI_ERROR_UNSPECIFIED;
250*4882a593Smuzhiyun 		break;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	default:
253*4882a593Smuzhiyun 		err = -ETIMEDOUT;
254*4882a593Smuzhiyun 		if (hci_status)
255*4882a593Smuzhiyun 			*hci_status = HCI_ERROR_UNSPECIFIED;
256*4882a593Smuzhiyun 		break;
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	kfree_skb(hdev->req_skb);
260*4882a593Smuzhiyun 	hdev->req_skb = NULL;
261*4882a593Smuzhiyun 	hdev->req_status = hdev->req_result = 0;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	BT_DBG("%s end: err %d", hdev->name, err);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	return err;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
hci_req_sync(struct hci_dev * hdev,int (* req)(struct hci_request * req,unsigned long opt),unsigned long opt,u32 timeout,u8 * hci_status)268*4882a593Smuzhiyun int hci_req_sync(struct hci_dev *hdev, int (*req)(struct hci_request *req,
269*4882a593Smuzhiyun 						  unsigned long opt),
270*4882a593Smuzhiyun 		 unsigned long opt, u32 timeout, u8 *hci_status)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun 	int ret;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	/* Serialize all requests */
275*4882a593Smuzhiyun 	hci_req_sync_lock(hdev);
276*4882a593Smuzhiyun 	/* check the state after obtaing the lock to protect the HCI_UP
277*4882a593Smuzhiyun 	 * against any races from hci_dev_do_close when the controller
278*4882a593Smuzhiyun 	 * gets removed.
279*4882a593Smuzhiyun 	 */
280*4882a593Smuzhiyun 	if (test_bit(HCI_UP, &hdev->flags))
281*4882a593Smuzhiyun 		ret = __hci_req_sync(hdev, req, opt, timeout, hci_status);
282*4882a593Smuzhiyun 	else
283*4882a593Smuzhiyun 		ret = -ENETDOWN;
284*4882a593Smuzhiyun 	hci_req_sync_unlock(hdev);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	return ret;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
hci_prepare_cmd(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param)289*4882a593Smuzhiyun struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen,
290*4882a593Smuzhiyun 				const void *param)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	int len = HCI_COMMAND_HDR_SIZE + plen;
293*4882a593Smuzhiyun 	struct hci_command_hdr *hdr;
294*4882a593Smuzhiyun 	struct sk_buff *skb;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	skb = bt_skb_alloc(len, GFP_ATOMIC);
297*4882a593Smuzhiyun 	if (!skb)
298*4882a593Smuzhiyun 		return NULL;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
301*4882a593Smuzhiyun 	hdr->opcode = cpu_to_le16(opcode);
302*4882a593Smuzhiyun 	hdr->plen   = plen;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	if (plen)
305*4882a593Smuzhiyun 		skb_put_data(skb, param, plen);
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	BT_DBG("skb len %d", skb->len);
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
310*4882a593Smuzhiyun 	hci_skb_opcode(skb) = opcode;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	return skb;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun /* Queue a command to an asynchronous HCI request */
hci_req_add_ev(struct hci_request * req,u16 opcode,u32 plen,const void * param,u8 event)316*4882a593Smuzhiyun void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
317*4882a593Smuzhiyun 		    const void *param, u8 event)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
320*4882a593Smuzhiyun 	struct sk_buff *skb;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	/* If an error occurred during request building, there is no point in
325*4882a593Smuzhiyun 	 * queueing the HCI command. We can simply return.
326*4882a593Smuzhiyun 	 */
327*4882a593Smuzhiyun 	if (req->err)
328*4882a593Smuzhiyun 		return;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	skb = hci_prepare_cmd(hdev, opcode, plen, param);
331*4882a593Smuzhiyun 	if (!skb) {
332*4882a593Smuzhiyun 		bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
333*4882a593Smuzhiyun 			   opcode);
334*4882a593Smuzhiyun 		req->err = -ENOMEM;
335*4882a593Smuzhiyun 		return;
336*4882a593Smuzhiyun 	}
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	if (skb_queue_empty(&req->cmd_q))
339*4882a593Smuzhiyun 		bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	bt_cb(skb)->hci.req_event = event;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	skb_queue_tail(&req->cmd_q, skb);
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
hci_req_add(struct hci_request * req,u16 opcode,u32 plen,const void * param)346*4882a593Smuzhiyun void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
347*4882a593Smuzhiyun 		 const void *param)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun 	hci_req_add_ev(req, opcode, plen, param, 0);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
__hci_req_write_fast_connectable(struct hci_request * req,bool enable)352*4882a593Smuzhiyun void __hci_req_write_fast_connectable(struct hci_request *req, bool enable)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
355*4882a593Smuzhiyun 	struct hci_cp_write_page_scan_activity acp;
356*4882a593Smuzhiyun 	u8 type;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
359*4882a593Smuzhiyun 		return;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
362*4882a593Smuzhiyun 		return;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	if (enable) {
365*4882a593Smuzhiyun 		type = PAGE_SCAN_TYPE_INTERLACED;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 		/* 160 msec page scan interval */
368*4882a593Smuzhiyun 		acp.interval = cpu_to_le16(0x0100);
369*4882a593Smuzhiyun 	} else {
370*4882a593Smuzhiyun 		type = hdev->def_page_scan_type;
371*4882a593Smuzhiyun 		acp.interval = cpu_to_le16(hdev->def_page_scan_int);
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	acp.window = cpu_to_le16(hdev->def_page_scan_window);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
377*4882a593Smuzhiyun 	    __cpu_to_le16(hdev->page_scan_window) != acp.window)
378*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
379*4882a593Smuzhiyun 			    sizeof(acp), &acp);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	if (hdev->page_scan_type != type)
382*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun /* This function controls the background scanning based on hdev->pend_le_conns
386*4882a593Smuzhiyun  * list. If there are pending LE connection we start the background scanning,
387*4882a593Smuzhiyun  * otherwise we stop it.
388*4882a593Smuzhiyun  *
389*4882a593Smuzhiyun  * This function requires the caller holds hdev->lock.
390*4882a593Smuzhiyun  */
__hci_update_background_scan(struct hci_request * req)391*4882a593Smuzhiyun static void __hci_update_background_scan(struct hci_request *req)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	if (!test_bit(HCI_UP, &hdev->flags) ||
396*4882a593Smuzhiyun 	    test_bit(HCI_INIT, &hdev->flags) ||
397*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_SETUP) ||
398*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_CONFIG) ||
399*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
400*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_UNREGISTER))
401*4882a593Smuzhiyun 		return;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	/* No point in doing scanning if LE support hasn't been enabled */
404*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
405*4882a593Smuzhiyun 		return;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	/* If discovery is active don't interfere with it */
408*4882a593Smuzhiyun 	if (hdev->discovery.state != DISCOVERY_STOPPED)
409*4882a593Smuzhiyun 		return;
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	/* Reset RSSI and UUID filters when starting background scanning
412*4882a593Smuzhiyun 	 * since these filters are meant for service discovery only.
413*4882a593Smuzhiyun 	 *
414*4882a593Smuzhiyun 	 * The Start Discovery and Start Service Discovery operations
415*4882a593Smuzhiyun 	 * ensure to set proper values for RSSI threshold and UUID
416*4882a593Smuzhiyun 	 * filter list. So it is safe to just reset them here.
417*4882a593Smuzhiyun 	 */
418*4882a593Smuzhiyun 	hci_discovery_filter_clear(hdev);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	BT_DBG("%s ADV monitoring is %s", hdev->name,
421*4882a593Smuzhiyun 	       hci_is_adv_monitoring(hdev) ? "on" : "off");
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (list_empty(&hdev->pend_le_conns) &&
424*4882a593Smuzhiyun 	    list_empty(&hdev->pend_le_reports) &&
425*4882a593Smuzhiyun 	    !hci_is_adv_monitoring(hdev)) {
426*4882a593Smuzhiyun 		/* If there is no pending LE connections or devices
427*4882a593Smuzhiyun 		 * to be scanned for or no ADV monitors, we should stop the
428*4882a593Smuzhiyun 		 * background scanning.
429*4882a593Smuzhiyun 		 */
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 		/* If controller is not scanning we are done. */
432*4882a593Smuzhiyun 		if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
433*4882a593Smuzhiyun 			return;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 		hci_req_add_le_scan_disable(req, false);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 		BT_DBG("%s stopping background scanning", hdev->name);
438*4882a593Smuzhiyun 	} else {
439*4882a593Smuzhiyun 		/* If there is at least one pending LE connection, we should
440*4882a593Smuzhiyun 		 * keep the background scan running.
441*4882a593Smuzhiyun 		 */
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 		/* If controller is connecting, we should not start scanning
444*4882a593Smuzhiyun 		 * since some controllers are not able to scan and connect at
445*4882a593Smuzhiyun 		 * the same time.
446*4882a593Smuzhiyun 		 */
447*4882a593Smuzhiyun 		if (hci_lookup_le_connect(hdev))
448*4882a593Smuzhiyun 			return;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 		/* If controller is currently scanning, we stop it to ensure we
451*4882a593Smuzhiyun 		 * don't miss any advertising (due to duplicates filter).
452*4882a593Smuzhiyun 		 */
453*4882a593Smuzhiyun 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
454*4882a593Smuzhiyun 			hci_req_add_le_scan_disable(req, false);
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 		hci_req_add_le_passive_scan(req);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 		BT_DBG("%s starting background scanning", hdev->name);
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun 
__hci_req_update_name(struct hci_request * req)462*4882a593Smuzhiyun void __hci_req_update_name(struct hci_request *req)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
465*4882a593Smuzhiyun 	struct hci_cp_write_local_name cp;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun #define PNP_INFO_SVCLASS_ID		0x1200
473*4882a593Smuzhiyun 
create_uuid16_list(struct hci_dev * hdev,u8 * data,ptrdiff_t len)474*4882a593Smuzhiyun static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun 	u8 *ptr = data, *uuids_start = NULL;
477*4882a593Smuzhiyun 	struct bt_uuid *uuid;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	if (len < 4)
480*4882a593Smuzhiyun 		return ptr;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	list_for_each_entry(uuid, &hdev->uuids, list) {
483*4882a593Smuzhiyun 		u16 uuid16;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 		if (uuid->size != 16)
486*4882a593Smuzhiyun 			continue;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 		uuid16 = get_unaligned_le16(&uuid->uuid[12]);
489*4882a593Smuzhiyun 		if (uuid16 < 0x1100)
490*4882a593Smuzhiyun 			continue;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 		if (uuid16 == PNP_INFO_SVCLASS_ID)
493*4882a593Smuzhiyun 			continue;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 		if (!uuids_start) {
496*4882a593Smuzhiyun 			uuids_start = ptr;
497*4882a593Smuzhiyun 			uuids_start[0] = 1;
498*4882a593Smuzhiyun 			uuids_start[1] = EIR_UUID16_ALL;
499*4882a593Smuzhiyun 			ptr += 2;
500*4882a593Smuzhiyun 		}
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 		/* Stop if not enough space to put next UUID */
503*4882a593Smuzhiyun 		if ((ptr - data) + sizeof(u16) > len) {
504*4882a593Smuzhiyun 			uuids_start[1] = EIR_UUID16_SOME;
505*4882a593Smuzhiyun 			break;
506*4882a593Smuzhiyun 		}
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 		*ptr++ = (uuid16 & 0x00ff);
509*4882a593Smuzhiyun 		*ptr++ = (uuid16 & 0xff00) >> 8;
510*4882a593Smuzhiyun 		uuids_start[0] += sizeof(uuid16);
511*4882a593Smuzhiyun 	}
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	return ptr;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun 
create_uuid32_list(struct hci_dev * hdev,u8 * data,ptrdiff_t len)516*4882a593Smuzhiyun static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun 	u8 *ptr = data, *uuids_start = NULL;
519*4882a593Smuzhiyun 	struct bt_uuid *uuid;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	if (len < 6)
522*4882a593Smuzhiyun 		return ptr;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	list_for_each_entry(uuid, &hdev->uuids, list) {
525*4882a593Smuzhiyun 		if (uuid->size != 32)
526*4882a593Smuzhiyun 			continue;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 		if (!uuids_start) {
529*4882a593Smuzhiyun 			uuids_start = ptr;
530*4882a593Smuzhiyun 			uuids_start[0] = 1;
531*4882a593Smuzhiyun 			uuids_start[1] = EIR_UUID32_ALL;
532*4882a593Smuzhiyun 			ptr += 2;
533*4882a593Smuzhiyun 		}
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 		/* Stop if not enough space to put next UUID */
536*4882a593Smuzhiyun 		if ((ptr - data) + sizeof(u32) > len) {
537*4882a593Smuzhiyun 			uuids_start[1] = EIR_UUID32_SOME;
538*4882a593Smuzhiyun 			break;
539*4882a593Smuzhiyun 		}
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 		memcpy(ptr, &uuid->uuid[12], sizeof(u32));
542*4882a593Smuzhiyun 		ptr += sizeof(u32);
543*4882a593Smuzhiyun 		uuids_start[0] += sizeof(u32);
544*4882a593Smuzhiyun 	}
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	return ptr;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
create_uuid128_list(struct hci_dev * hdev,u8 * data,ptrdiff_t len)549*4882a593Smuzhiyun static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun 	u8 *ptr = data, *uuids_start = NULL;
552*4882a593Smuzhiyun 	struct bt_uuid *uuid;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	if (len < 18)
555*4882a593Smuzhiyun 		return ptr;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	list_for_each_entry(uuid, &hdev->uuids, list) {
558*4882a593Smuzhiyun 		if (uuid->size != 128)
559*4882a593Smuzhiyun 			continue;
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 		if (!uuids_start) {
562*4882a593Smuzhiyun 			uuids_start = ptr;
563*4882a593Smuzhiyun 			uuids_start[0] = 1;
564*4882a593Smuzhiyun 			uuids_start[1] = EIR_UUID128_ALL;
565*4882a593Smuzhiyun 			ptr += 2;
566*4882a593Smuzhiyun 		}
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 		/* Stop if not enough space to put next UUID */
569*4882a593Smuzhiyun 		if ((ptr - data) + 16 > len) {
570*4882a593Smuzhiyun 			uuids_start[1] = EIR_UUID128_SOME;
571*4882a593Smuzhiyun 			break;
572*4882a593Smuzhiyun 		}
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 		memcpy(ptr, uuid->uuid, 16);
575*4882a593Smuzhiyun 		ptr += 16;
576*4882a593Smuzhiyun 		uuids_start[0] += 16;
577*4882a593Smuzhiyun 	}
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	return ptr;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun 
create_eir(struct hci_dev * hdev,u8 * data)582*4882a593Smuzhiyun static void create_eir(struct hci_dev *hdev, u8 *data)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun 	u8 *ptr = data;
585*4882a593Smuzhiyun 	size_t name_len;
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	name_len = strlen(hdev->dev_name);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	if (name_len > 0) {
590*4882a593Smuzhiyun 		/* EIR Data type */
591*4882a593Smuzhiyun 		if (name_len > 48) {
592*4882a593Smuzhiyun 			name_len = 48;
593*4882a593Smuzhiyun 			ptr[1] = EIR_NAME_SHORT;
594*4882a593Smuzhiyun 		} else
595*4882a593Smuzhiyun 			ptr[1] = EIR_NAME_COMPLETE;
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 		/* EIR Data length */
598*4882a593Smuzhiyun 		ptr[0] = name_len + 1;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 		memcpy(ptr + 2, hdev->dev_name, name_len);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 		ptr += (name_len + 2);
603*4882a593Smuzhiyun 	}
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
606*4882a593Smuzhiyun 		ptr[0] = 2;
607*4882a593Smuzhiyun 		ptr[1] = EIR_TX_POWER;
608*4882a593Smuzhiyun 		ptr[2] = (u8) hdev->inq_tx_power;
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 		ptr += 3;
611*4882a593Smuzhiyun 	}
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	if (hdev->devid_source > 0) {
614*4882a593Smuzhiyun 		ptr[0] = 9;
615*4882a593Smuzhiyun 		ptr[1] = EIR_DEVICE_ID;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 		put_unaligned_le16(hdev->devid_source, ptr + 2);
618*4882a593Smuzhiyun 		put_unaligned_le16(hdev->devid_vendor, ptr + 4);
619*4882a593Smuzhiyun 		put_unaligned_le16(hdev->devid_product, ptr + 6);
620*4882a593Smuzhiyun 		put_unaligned_le16(hdev->devid_version, ptr + 8);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 		ptr += 10;
623*4882a593Smuzhiyun 	}
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
626*4882a593Smuzhiyun 	ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
627*4882a593Smuzhiyun 	ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun 
__hci_req_update_eir(struct hci_request * req)630*4882a593Smuzhiyun void __hci_req_update_eir(struct hci_request *req)
631*4882a593Smuzhiyun {
632*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
633*4882a593Smuzhiyun 	struct hci_cp_write_eir cp;
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	if (!hdev_is_powered(hdev))
636*4882a593Smuzhiyun 		return;
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	if (!lmp_ext_inq_capable(hdev))
639*4882a593Smuzhiyun 		return;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
642*4882a593Smuzhiyun 		return;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
645*4882a593Smuzhiyun 		return;
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	memset(&cp, 0, sizeof(cp));
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	create_eir(hdev, cp.data);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
652*4882a593Smuzhiyun 		return;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	memcpy(hdev->eir, cp.data, sizeof(cp.data));
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun 
hci_req_add_le_scan_disable(struct hci_request * req,bool rpa_le_conn)659*4882a593Smuzhiyun void hci_req_add_le_scan_disable(struct hci_request *req, bool rpa_le_conn)
660*4882a593Smuzhiyun {
661*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	if (hdev->scanning_paused) {
664*4882a593Smuzhiyun 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
665*4882a593Smuzhiyun 		return;
666*4882a593Smuzhiyun 	}
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	if (use_ext_scan(hdev)) {
669*4882a593Smuzhiyun 		struct hci_cp_le_set_ext_scan_enable cp;
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 		memset(&cp, 0, sizeof(cp));
672*4882a593Smuzhiyun 		cp.enable = LE_SCAN_DISABLE;
673*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE, sizeof(cp),
674*4882a593Smuzhiyun 			    &cp);
675*4882a593Smuzhiyun 	} else {
676*4882a593Smuzhiyun 		struct hci_cp_le_set_scan_enable cp;
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 		memset(&cp, 0, sizeof(cp));
679*4882a593Smuzhiyun 		cp.enable = LE_SCAN_DISABLE;
680*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
681*4882a593Smuzhiyun 	}
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	/* Disable address resolution */
684*4882a593Smuzhiyun 	if (use_ll_privacy(hdev) &&
685*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
686*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION) && !rpa_le_conn) {
687*4882a593Smuzhiyun 		__u8 enable = 0x00;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, 1, &enable);
690*4882a593Smuzhiyun 	}
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun 
del_from_white_list(struct hci_request * req,bdaddr_t * bdaddr,u8 bdaddr_type)693*4882a593Smuzhiyun static void del_from_white_list(struct hci_request *req, bdaddr_t *bdaddr,
694*4882a593Smuzhiyun 				u8 bdaddr_type)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun 	struct hci_cp_le_del_from_white_list cp;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	cp.bdaddr_type = bdaddr_type;
699*4882a593Smuzhiyun 	bacpy(&cp.bdaddr, bdaddr);
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	bt_dev_dbg(req->hdev, "Remove %pMR (0x%x) from whitelist", &cp.bdaddr,
702*4882a593Smuzhiyun 		   cp.bdaddr_type);
703*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_LE_DEL_FROM_WHITE_LIST, sizeof(cp), &cp);
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	if (use_ll_privacy(req->hdev) &&
706*4882a593Smuzhiyun 	    hci_dev_test_flag(req->hdev, HCI_ENABLE_LL_PRIVACY)) {
707*4882a593Smuzhiyun 		struct smp_irk *irk;
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 		irk = hci_find_irk_by_addr(req->hdev, bdaddr, bdaddr_type);
710*4882a593Smuzhiyun 		if (irk) {
711*4882a593Smuzhiyun 			struct hci_cp_le_del_from_resolv_list cp;
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 			cp.bdaddr_type = bdaddr_type;
714*4882a593Smuzhiyun 			bacpy(&cp.bdaddr, bdaddr);
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
717*4882a593Smuzhiyun 				    sizeof(cp), &cp);
718*4882a593Smuzhiyun 		}
719*4882a593Smuzhiyun 	}
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun /* Adds connection to white list if needed. On error, returns -1. */
add_to_white_list(struct hci_request * req,struct hci_conn_params * params,u8 * num_entries,bool allow_rpa)723*4882a593Smuzhiyun static int add_to_white_list(struct hci_request *req,
724*4882a593Smuzhiyun 			     struct hci_conn_params *params, u8 *num_entries,
725*4882a593Smuzhiyun 			     bool allow_rpa)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun 	struct hci_cp_le_add_to_white_list cp;
728*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	/* Already in white list */
731*4882a593Smuzhiyun 	if (hci_bdaddr_list_lookup(&hdev->le_white_list, &params->addr,
732*4882a593Smuzhiyun 				   params->addr_type))
733*4882a593Smuzhiyun 		return 0;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	/* Select filter policy to accept all advertising */
736*4882a593Smuzhiyun 	if (*num_entries >= hdev->le_white_list_size)
737*4882a593Smuzhiyun 		return -1;
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	/* White list can not be used with RPAs */
740*4882a593Smuzhiyun 	if (!allow_rpa &&
741*4882a593Smuzhiyun 	    !hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
742*4882a593Smuzhiyun 	    hci_find_irk_by_addr(hdev, &params->addr, params->addr_type)) {
743*4882a593Smuzhiyun 		return -1;
744*4882a593Smuzhiyun 	}
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	/* During suspend, only wakeable devices can be in whitelist */
747*4882a593Smuzhiyun 	if (hdev->suspended && !hci_conn_test_flag(HCI_CONN_FLAG_REMOTE_WAKEUP,
748*4882a593Smuzhiyun 						   params->current_flags))
749*4882a593Smuzhiyun 		return 0;
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	*num_entries += 1;
752*4882a593Smuzhiyun 	cp.bdaddr_type = params->addr_type;
753*4882a593Smuzhiyun 	bacpy(&cp.bdaddr, &params->addr);
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	bt_dev_dbg(hdev, "Add %pMR (0x%x) to whitelist", &cp.bdaddr,
756*4882a593Smuzhiyun 		   cp.bdaddr_type);
757*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_LE_ADD_TO_WHITE_LIST, sizeof(cp), &cp);
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	if (use_ll_privacy(hdev) &&
760*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) {
761*4882a593Smuzhiyun 		struct smp_irk *irk;
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 		irk = hci_find_irk_by_addr(hdev, &params->addr,
764*4882a593Smuzhiyun 					   params->addr_type);
765*4882a593Smuzhiyun 		if (irk) {
766*4882a593Smuzhiyun 			struct hci_cp_le_add_to_resolv_list cp;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 			cp.bdaddr_type = params->addr_type;
769*4882a593Smuzhiyun 			bacpy(&cp.bdaddr, &params->addr);
770*4882a593Smuzhiyun 			memcpy(cp.peer_irk, irk->val, 16);
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 			if (hci_dev_test_flag(hdev, HCI_PRIVACY))
773*4882a593Smuzhiyun 				memcpy(cp.local_irk, hdev->irk, 16);
774*4882a593Smuzhiyun 			else
775*4882a593Smuzhiyun 				memset(cp.local_irk, 0, 16);
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_LE_ADD_TO_RESOLV_LIST,
778*4882a593Smuzhiyun 				    sizeof(cp), &cp);
779*4882a593Smuzhiyun 		}
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	return 0;
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun 
update_white_list(struct hci_request * req)785*4882a593Smuzhiyun static u8 update_white_list(struct hci_request *req)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
788*4882a593Smuzhiyun 	struct hci_conn_params *params;
789*4882a593Smuzhiyun 	struct bdaddr_list *b;
790*4882a593Smuzhiyun 	u8 num_entries = 0;
791*4882a593Smuzhiyun 	bool pend_conn, pend_report;
792*4882a593Smuzhiyun 	/* We allow whitelisting even with RPAs in suspend. In the worst case,
793*4882a593Smuzhiyun 	 * we won't be able to wake from devices that use the privacy1.2
794*4882a593Smuzhiyun 	 * features. Additionally, once we support privacy1.2 and IRK
795*4882a593Smuzhiyun 	 * offloading, we can update this to also check for those conditions.
796*4882a593Smuzhiyun 	 */
797*4882a593Smuzhiyun 	bool allow_rpa = hdev->suspended;
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	if (use_ll_privacy(hdev) &&
800*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
801*4882a593Smuzhiyun 		allow_rpa = true;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	/* Go through the current white list programmed into the
804*4882a593Smuzhiyun 	 * controller one by one and check if that address is still
805*4882a593Smuzhiyun 	 * in the list of pending connections or list of devices to
806*4882a593Smuzhiyun 	 * report. If not present in either list, then queue the
807*4882a593Smuzhiyun 	 * command to remove it from the controller.
808*4882a593Smuzhiyun 	 */
809*4882a593Smuzhiyun 	list_for_each_entry(b, &hdev->le_white_list, list) {
810*4882a593Smuzhiyun 		pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
811*4882a593Smuzhiyun 						      &b->bdaddr,
812*4882a593Smuzhiyun 						      b->bdaddr_type);
813*4882a593Smuzhiyun 		pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
814*4882a593Smuzhiyun 							&b->bdaddr,
815*4882a593Smuzhiyun 							b->bdaddr_type);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 		/* If the device is not likely to connect or report,
818*4882a593Smuzhiyun 		 * remove it from the whitelist.
819*4882a593Smuzhiyun 		 */
820*4882a593Smuzhiyun 		if (!pend_conn && !pend_report) {
821*4882a593Smuzhiyun 			del_from_white_list(req, &b->bdaddr, b->bdaddr_type);
822*4882a593Smuzhiyun 			continue;
823*4882a593Smuzhiyun 		}
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 		/* White list can not be used with RPAs */
826*4882a593Smuzhiyun 		if (!allow_rpa &&
827*4882a593Smuzhiyun 		    !hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
828*4882a593Smuzhiyun 		    hci_find_irk_by_addr(hdev, &b->bdaddr, b->bdaddr_type)) {
829*4882a593Smuzhiyun 			return 0x00;
830*4882a593Smuzhiyun 		}
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 		num_entries++;
833*4882a593Smuzhiyun 	}
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	/* Since all no longer valid white list entries have been
836*4882a593Smuzhiyun 	 * removed, walk through the list of pending connections
837*4882a593Smuzhiyun 	 * and ensure that any new device gets programmed into
838*4882a593Smuzhiyun 	 * the controller.
839*4882a593Smuzhiyun 	 *
840*4882a593Smuzhiyun 	 * If the list of the devices is larger than the list of
841*4882a593Smuzhiyun 	 * available white list entries in the controller, then
842*4882a593Smuzhiyun 	 * just abort and return filer policy value to not use the
843*4882a593Smuzhiyun 	 * white list.
844*4882a593Smuzhiyun 	 */
845*4882a593Smuzhiyun 	list_for_each_entry(params, &hdev->pend_le_conns, action) {
846*4882a593Smuzhiyun 		if (add_to_white_list(req, params, &num_entries, allow_rpa))
847*4882a593Smuzhiyun 			return 0x00;
848*4882a593Smuzhiyun 	}
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	/* After adding all new pending connections, walk through
851*4882a593Smuzhiyun 	 * the list of pending reports and also add these to the
852*4882a593Smuzhiyun 	 * white list if there is still space. Abort if space runs out.
853*4882a593Smuzhiyun 	 */
854*4882a593Smuzhiyun 	list_for_each_entry(params, &hdev->pend_le_reports, action) {
855*4882a593Smuzhiyun 		if (add_to_white_list(req, params, &num_entries, allow_rpa))
856*4882a593Smuzhiyun 			return 0x00;
857*4882a593Smuzhiyun 	}
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	/* Once the controller offloading of advertisement monitor is in place,
860*4882a593Smuzhiyun 	 * the if condition should include the support of MSFT extension
861*4882a593Smuzhiyun 	 * support. If suspend is ongoing, whitelist should be the default to
862*4882a593Smuzhiyun 	 * prevent waking by random advertisements.
863*4882a593Smuzhiyun 	 */
864*4882a593Smuzhiyun 	if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended)
865*4882a593Smuzhiyun 		return 0x00;
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 	/* Select filter policy to use white list */
868*4882a593Smuzhiyun 	return 0x01;
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun 
scan_use_rpa(struct hci_dev * hdev)871*4882a593Smuzhiyun static bool scan_use_rpa(struct hci_dev *hdev)
872*4882a593Smuzhiyun {
873*4882a593Smuzhiyun 	return hci_dev_test_flag(hdev, HCI_PRIVACY);
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun 
hci_req_start_scan(struct hci_request * req,u8 type,u16 interval,u16 window,u8 own_addr_type,u8 filter_policy,bool addr_resolv)876*4882a593Smuzhiyun static void hci_req_start_scan(struct hci_request *req, u8 type, u16 interval,
877*4882a593Smuzhiyun 			       u16 window, u8 own_addr_type, u8 filter_policy,
878*4882a593Smuzhiyun 			       bool addr_resolv)
879*4882a593Smuzhiyun {
880*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	if (hdev->scanning_paused) {
883*4882a593Smuzhiyun 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
884*4882a593Smuzhiyun 		return;
885*4882a593Smuzhiyun 	}
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	if (use_ll_privacy(hdev) &&
888*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
889*4882a593Smuzhiyun 	    addr_resolv) {
890*4882a593Smuzhiyun 		u8 enable = 0x01;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, 1, &enable);
893*4882a593Smuzhiyun 	}
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	/* Use ext scanning if set ext scan param and ext scan enable is
896*4882a593Smuzhiyun 	 * supported
897*4882a593Smuzhiyun 	 */
898*4882a593Smuzhiyun 	if (use_ext_scan(hdev)) {
899*4882a593Smuzhiyun 		struct hci_cp_le_set_ext_scan_params *ext_param_cp;
900*4882a593Smuzhiyun 		struct hci_cp_le_set_ext_scan_enable ext_enable_cp;
901*4882a593Smuzhiyun 		struct hci_cp_le_scan_phy_params *phy_params;
902*4882a593Smuzhiyun 		u8 data[sizeof(*ext_param_cp) + sizeof(*phy_params) * 2];
903*4882a593Smuzhiyun 		u32 plen;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 		ext_param_cp = (void *)data;
906*4882a593Smuzhiyun 		phy_params = (void *)ext_param_cp->data;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 		memset(ext_param_cp, 0, sizeof(*ext_param_cp));
909*4882a593Smuzhiyun 		ext_param_cp->own_addr_type = own_addr_type;
910*4882a593Smuzhiyun 		ext_param_cp->filter_policy = filter_policy;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 		plen = sizeof(*ext_param_cp);
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 		if (scan_1m(hdev) || scan_2m(hdev)) {
915*4882a593Smuzhiyun 			ext_param_cp->scanning_phys |= LE_SCAN_PHY_1M;
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 			memset(phy_params, 0, sizeof(*phy_params));
918*4882a593Smuzhiyun 			phy_params->type = type;
919*4882a593Smuzhiyun 			phy_params->interval = cpu_to_le16(interval);
920*4882a593Smuzhiyun 			phy_params->window = cpu_to_le16(window);
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 			plen += sizeof(*phy_params);
923*4882a593Smuzhiyun 			phy_params++;
924*4882a593Smuzhiyun 		}
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 		if (scan_coded(hdev)) {
927*4882a593Smuzhiyun 			ext_param_cp->scanning_phys |= LE_SCAN_PHY_CODED;
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 			memset(phy_params, 0, sizeof(*phy_params));
930*4882a593Smuzhiyun 			phy_params->type = type;
931*4882a593Smuzhiyun 			phy_params->interval = cpu_to_le16(interval);
932*4882a593Smuzhiyun 			phy_params->window = cpu_to_le16(window);
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 			plen += sizeof(*phy_params);
935*4882a593Smuzhiyun 			phy_params++;
936*4882a593Smuzhiyun 		}
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
939*4882a593Smuzhiyun 			    plen, ext_param_cp);
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 		memset(&ext_enable_cp, 0, sizeof(ext_enable_cp));
942*4882a593Smuzhiyun 		ext_enable_cp.enable = LE_SCAN_ENABLE;
943*4882a593Smuzhiyun 		ext_enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
946*4882a593Smuzhiyun 			    sizeof(ext_enable_cp), &ext_enable_cp);
947*4882a593Smuzhiyun 	} else {
948*4882a593Smuzhiyun 		struct hci_cp_le_set_scan_param param_cp;
949*4882a593Smuzhiyun 		struct hci_cp_le_set_scan_enable enable_cp;
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 		memset(&param_cp, 0, sizeof(param_cp));
952*4882a593Smuzhiyun 		param_cp.type = type;
953*4882a593Smuzhiyun 		param_cp.interval = cpu_to_le16(interval);
954*4882a593Smuzhiyun 		param_cp.window = cpu_to_le16(window);
955*4882a593Smuzhiyun 		param_cp.own_address_type = own_addr_type;
956*4882a593Smuzhiyun 		param_cp.filter_policy = filter_policy;
957*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
958*4882a593Smuzhiyun 			    &param_cp);
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 		memset(&enable_cp, 0, sizeof(enable_cp));
961*4882a593Smuzhiyun 		enable_cp.enable = LE_SCAN_ENABLE;
962*4882a593Smuzhiyun 		enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
963*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
964*4882a593Smuzhiyun 			    &enable_cp);
965*4882a593Smuzhiyun 	}
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun /* Returns true if an le connection is in the scanning state */
hci_is_le_conn_scanning(struct hci_dev * hdev)969*4882a593Smuzhiyun static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
970*4882a593Smuzhiyun {
971*4882a593Smuzhiyun 	struct hci_conn_hash *h = &hdev->conn_hash;
972*4882a593Smuzhiyun 	struct hci_conn  *c;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	rcu_read_lock();
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	list_for_each_entry_rcu(c, &h->list, list) {
977*4882a593Smuzhiyun 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
978*4882a593Smuzhiyun 		    test_bit(HCI_CONN_SCANNING, &c->flags)) {
979*4882a593Smuzhiyun 			rcu_read_unlock();
980*4882a593Smuzhiyun 			return true;
981*4882a593Smuzhiyun 		}
982*4882a593Smuzhiyun 	}
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	rcu_read_unlock();
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 	return false;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun /* Ensure to call hci_req_add_le_scan_disable() first to disable the
990*4882a593Smuzhiyun  * controller based address resolution to be able to reconfigure
991*4882a593Smuzhiyun  * resolving list.
992*4882a593Smuzhiyun  */
hci_req_add_le_passive_scan(struct hci_request * req)993*4882a593Smuzhiyun void hci_req_add_le_passive_scan(struct hci_request *req)
994*4882a593Smuzhiyun {
995*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
996*4882a593Smuzhiyun 	u8 own_addr_type;
997*4882a593Smuzhiyun 	u8 filter_policy;
998*4882a593Smuzhiyun 	u16 window, interval;
999*4882a593Smuzhiyun 	/* Background scanning should run with address resolution */
1000*4882a593Smuzhiyun 	bool addr_resolv = true;
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	if (hdev->scanning_paused) {
1003*4882a593Smuzhiyun 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
1004*4882a593Smuzhiyun 		return;
1005*4882a593Smuzhiyun 	}
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	/* Set require_privacy to false since no SCAN_REQ are send
1008*4882a593Smuzhiyun 	 * during passive scanning. Not using an non-resolvable address
1009*4882a593Smuzhiyun 	 * here is important so that peer devices using direct
1010*4882a593Smuzhiyun 	 * advertising with our address will be correctly reported
1011*4882a593Smuzhiyun 	 * by the controller.
1012*4882a593Smuzhiyun 	 */
1013*4882a593Smuzhiyun 	if (hci_update_random_address(req, false, scan_use_rpa(hdev),
1014*4882a593Smuzhiyun 				      &own_addr_type))
1015*4882a593Smuzhiyun 		return;
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 	/* Adding or removing entries from the white list must
1018*4882a593Smuzhiyun 	 * happen before enabling scanning. The controller does
1019*4882a593Smuzhiyun 	 * not allow white list modification while scanning.
1020*4882a593Smuzhiyun 	 */
1021*4882a593Smuzhiyun 	filter_policy = update_white_list(req);
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	/* When the controller is using random resolvable addresses and
1024*4882a593Smuzhiyun 	 * with that having LE privacy enabled, then controllers with
1025*4882a593Smuzhiyun 	 * Extended Scanner Filter Policies support can now enable support
1026*4882a593Smuzhiyun 	 * for handling directed advertising.
1027*4882a593Smuzhiyun 	 *
1028*4882a593Smuzhiyun 	 * So instead of using filter polices 0x00 (no whitelist)
1029*4882a593Smuzhiyun 	 * and 0x01 (whitelist enabled) use the new filter policies
1030*4882a593Smuzhiyun 	 * 0x02 (no whitelist) and 0x03 (whitelist enabled).
1031*4882a593Smuzhiyun 	 */
1032*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
1033*4882a593Smuzhiyun 	    (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
1034*4882a593Smuzhiyun 		filter_policy |= 0x02;
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 	if (hdev->suspended) {
1037*4882a593Smuzhiyun 		window = hdev->le_scan_window_suspend;
1038*4882a593Smuzhiyun 		interval = hdev->le_scan_int_suspend;
1039*4882a593Smuzhiyun 	} else if (hci_is_le_conn_scanning(hdev)) {
1040*4882a593Smuzhiyun 		window = hdev->le_scan_window_connect;
1041*4882a593Smuzhiyun 		interval = hdev->le_scan_int_connect;
1042*4882a593Smuzhiyun 	} else if (hci_is_adv_monitoring(hdev)) {
1043*4882a593Smuzhiyun 		window = hdev->le_scan_window_adv_monitor;
1044*4882a593Smuzhiyun 		interval = hdev->le_scan_int_adv_monitor;
1045*4882a593Smuzhiyun 	} else {
1046*4882a593Smuzhiyun 		window = hdev->le_scan_window;
1047*4882a593Smuzhiyun 		interval = hdev->le_scan_interval;
1048*4882a593Smuzhiyun 	}
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	bt_dev_dbg(hdev, "LE passive scan with whitelist = %d", filter_policy);
1051*4882a593Smuzhiyun 	hci_req_start_scan(req, LE_SCAN_PASSIVE, interval, window,
1052*4882a593Smuzhiyun 			   own_addr_type, filter_policy, addr_resolv);
1053*4882a593Smuzhiyun }
1054*4882a593Smuzhiyun 
get_adv_instance_scan_rsp_len(struct hci_dev * hdev,u8 instance)1055*4882a593Smuzhiyun static u8 get_adv_instance_scan_rsp_len(struct hci_dev *hdev, u8 instance)
1056*4882a593Smuzhiyun {
1057*4882a593Smuzhiyun 	struct adv_info *adv_instance;
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 	/* Instance 0x00 always set local name */
1060*4882a593Smuzhiyun 	if (instance == 0x00)
1061*4882a593Smuzhiyun 		return 1;
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun 	adv_instance = hci_find_adv_instance(hdev, instance);
1064*4882a593Smuzhiyun 	if (!adv_instance)
1065*4882a593Smuzhiyun 		return 0;
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	if (adv_instance->flags & MGMT_ADV_FLAG_APPEARANCE ||
1068*4882a593Smuzhiyun 	    adv_instance->flags & MGMT_ADV_FLAG_LOCAL_NAME)
1069*4882a593Smuzhiyun 		return 1;
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	return adv_instance->scan_rsp_len;
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun 
hci_req_clear_event_filter(struct hci_request * req)1074*4882a593Smuzhiyun static void hci_req_clear_event_filter(struct hci_request *req)
1075*4882a593Smuzhiyun {
1076*4882a593Smuzhiyun 	struct hci_cp_set_event_filter f;
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun 	memset(&f, 0, sizeof(f));
1079*4882a593Smuzhiyun 	f.flt_type = HCI_FLT_CLEAR_ALL;
1080*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &f);
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 	/* Update page scan state (since we may have modified it when setting
1083*4882a593Smuzhiyun 	 * the event filter).
1084*4882a593Smuzhiyun 	 */
1085*4882a593Smuzhiyun 	__hci_req_update_scan(req);
1086*4882a593Smuzhiyun }
1087*4882a593Smuzhiyun 
hci_req_set_event_filter(struct hci_request * req)1088*4882a593Smuzhiyun static void hci_req_set_event_filter(struct hci_request *req)
1089*4882a593Smuzhiyun {
1090*4882a593Smuzhiyun 	struct bdaddr_list_with_flags *b;
1091*4882a593Smuzhiyun 	struct hci_cp_set_event_filter f;
1092*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
1093*4882a593Smuzhiyun 	u8 scan = SCAN_DISABLED;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	/* Always clear event filter when starting */
1096*4882a593Smuzhiyun 	hci_req_clear_event_filter(req);
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	list_for_each_entry(b, &hdev->whitelist, list) {
1099*4882a593Smuzhiyun 		if (!hci_conn_test_flag(HCI_CONN_FLAG_REMOTE_WAKEUP,
1100*4882a593Smuzhiyun 					b->current_flags))
1101*4882a593Smuzhiyun 			continue;
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 		memset(&f, 0, sizeof(f));
1104*4882a593Smuzhiyun 		bacpy(&f.addr_conn_flt.bdaddr, &b->bdaddr);
1105*4882a593Smuzhiyun 		f.flt_type = HCI_FLT_CONN_SETUP;
1106*4882a593Smuzhiyun 		f.cond_type = HCI_CONN_SETUP_ALLOW_BDADDR;
1107*4882a593Smuzhiyun 		f.addr_conn_flt.auto_accept = HCI_CONN_SETUP_AUTO_ON;
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 		bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
1110*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_SET_EVENT_FLT, sizeof(f), &f);
1111*4882a593Smuzhiyun 		scan = SCAN_PAGE;
1112*4882a593Smuzhiyun 	}
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun 
hci_req_config_le_suspend_scan(struct hci_request * req)1117*4882a593Smuzhiyun static void hci_req_config_le_suspend_scan(struct hci_request *req)
1118*4882a593Smuzhiyun {
1119*4882a593Smuzhiyun 	/* Before changing params disable scan if enabled */
1120*4882a593Smuzhiyun 	if (hci_dev_test_flag(req->hdev, HCI_LE_SCAN))
1121*4882a593Smuzhiyun 		hci_req_add_le_scan_disable(req, false);
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun 	/* Configure params and enable scanning */
1124*4882a593Smuzhiyun 	hci_req_add_le_passive_scan(req);
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 	/* Block suspend notifier on response */
1127*4882a593Smuzhiyun 	set_bit(SUSPEND_SCAN_ENABLE, req->hdev->suspend_tasks);
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun 
cancel_adv_timeout(struct hci_dev * hdev)1130*4882a593Smuzhiyun static void cancel_adv_timeout(struct hci_dev *hdev)
1131*4882a593Smuzhiyun {
1132*4882a593Smuzhiyun 	if (hdev->adv_instance_timeout) {
1133*4882a593Smuzhiyun 		hdev->adv_instance_timeout = 0;
1134*4882a593Smuzhiyun 		cancel_delayed_work(&hdev->adv_instance_expire);
1135*4882a593Smuzhiyun 	}
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun /* This function requires the caller holds hdev->lock */
hci_suspend_adv_instances(struct hci_request * req)1139*4882a593Smuzhiyun static void hci_suspend_adv_instances(struct hci_request *req)
1140*4882a593Smuzhiyun {
1141*4882a593Smuzhiyun 	bt_dev_dbg(req->hdev, "Suspending advertising instances");
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	/* Call to disable any advertisements active on the controller.
1144*4882a593Smuzhiyun 	 * This will succeed even if no advertisements are configured.
1145*4882a593Smuzhiyun 	 */
1146*4882a593Smuzhiyun 	__hci_req_disable_advertising(req);
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	/* If we are using software rotation, pause the loop */
1149*4882a593Smuzhiyun 	if (!ext_adv_capable(req->hdev))
1150*4882a593Smuzhiyun 		cancel_adv_timeout(req->hdev);
1151*4882a593Smuzhiyun }
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun /* This function requires the caller holds hdev->lock */
hci_resume_adv_instances(struct hci_request * req)1154*4882a593Smuzhiyun static void hci_resume_adv_instances(struct hci_request *req)
1155*4882a593Smuzhiyun {
1156*4882a593Smuzhiyun 	struct adv_info *adv;
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	bt_dev_dbg(req->hdev, "Resuming advertising instances");
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	if (ext_adv_capable(req->hdev)) {
1161*4882a593Smuzhiyun 		/* Call for each tracked instance to be re-enabled */
1162*4882a593Smuzhiyun 		list_for_each_entry(adv, &req->hdev->adv_instances, list) {
1163*4882a593Smuzhiyun 			__hci_req_enable_ext_advertising(req,
1164*4882a593Smuzhiyun 							 adv->instance);
1165*4882a593Smuzhiyun 		}
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	} else {
1168*4882a593Smuzhiyun 		/* Schedule for most recent instance to be restarted and begin
1169*4882a593Smuzhiyun 		 * the software rotation loop
1170*4882a593Smuzhiyun 		 */
1171*4882a593Smuzhiyun 		__hci_req_schedule_adv_instance(req,
1172*4882a593Smuzhiyun 						req->hdev->cur_adv_instance,
1173*4882a593Smuzhiyun 						true);
1174*4882a593Smuzhiyun 	}
1175*4882a593Smuzhiyun }
1176*4882a593Smuzhiyun 
suspend_req_complete(struct hci_dev * hdev,u8 status,u16 opcode)1177*4882a593Smuzhiyun static void suspend_req_complete(struct hci_dev *hdev, u8 status, u16 opcode)
1178*4882a593Smuzhiyun {
1179*4882a593Smuzhiyun 	bt_dev_dbg(hdev, "Request complete opcode=0x%x, status=0x%x", opcode,
1180*4882a593Smuzhiyun 		   status);
1181*4882a593Smuzhiyun 	if (test_and_clear_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks) ||
1182*4882a593Smuzhiyun 	    test_and_clear_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks)) {
1183*4882a593Smuzhiyun 		wake_up(&hdev->suspend_wait_q);
1184*4882a593Smuzhiyun 	}
1185*4882a593Smuzhiyun }
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun /* Call with hci_dev_lock */
hci_req_prepare_suspend(struct hci_dev * hdev,enum suspended_state next)1188*4882a593Smuzhiyun void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next)
1189*4882a593Smuzhiyun {
1190*4882a593Smuzhiyun 	int old_state;
1191*4882a593Smuzhiyun 	struct hci_conn *conn;
1192*4882a593Smuzhiyun 	struct hci_request req;
1193*4882a593Smuzhiyun 	u8 page_scan;
1194*4882a593Smuzhiyun 	int disconnect_counter;
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 	if (next == hdev->suspend_state) {
1197*4882a593Smuzhiyun 		bt_dev_dbg(hdev, "Same state before and after: %d", next);
1198*4882a593Smuzhiyun 		goto done;
1199*4882a593Smuzhiyun 	}
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 	hdev->suspend_state = next;
1202*4882a593Smuzhiyun 	hci_req_init(&req, hdev);
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun 	if (next == BT_SUSPEND_DISCONNECT) {
1205*4882a593Smuzhiyun 		/* Mark device as suspended */
1206*4882a593Smuzhiyun 		hdev->suspended = true;
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun 		/* Pause discovery if not already stopped */
1209*4882a593Smuzhiyun 		old_state = hdev->discovery.state;
1210*4882a593Smuzhiyun 		if (old_state != DISCOVERY_STOPPED) {
1211*4882a593Smuzhiyun 			set_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks);
1212*4882a593Smuzhiyun 			hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1213*4882a593Smuzhiyun 			queue_work(hdev->req_workqueue, &hdev->discov_update);
1214*4882a593Smuzhiyun 		}
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 		hdev->discovery_paused = true;
1217*4882a593Smuzhiyun 		hdev->discovery_old_state = old_state;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 		/* Stop directed advertising */
1220*4882a593Smuzhiyun 		old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
1221*4882a593Smuzhiyun 		if (old_state) {
1222*4882a593Smuzhiyun 			set_bit(SUSPEND_PAUSE_ADVERTISING, hdev->suspend_tasks);
1223*4882a593Smuzhiyun 			cancel_delayed_work(&hdev->discov_off);
1224*4882a593Smuzhiyun 			queue_delayed_work(hdev->req_workqueue,
1225*4882a593Smuzhiyun 					   &hdev->discov_off, 0);
1226*4882a593Smuzhiyun 		}
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 		/* Pause other advertisements */
1229*4882a593Smuzhiyun 		if (hdev->adv_instance_cnt)
1230*4882a593Smuzhiyun 			hci_suspend_adv_instances(&req);
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 		hdev->advertising_paused = true;
1233*4882a593Smuzhiyun 		hdev->advertising_old_state = old_state;
1234*4882a593Smuzhiyun 		/* Disable page scan */
1235*4882a593Smuzhiyun 		page_scan = SCAN_DISABLED;
1236*4882a593Smuzhiyun 		hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &page_scan);
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 		/* Disable LE passive scan if enabled */
1239*4882a593Smuzhiyun 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
1240*4882a593Smuzhiyun 			hci_req_add_le_scan_disable(&req, false);
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 		/* Mark task needing completion */
1243*4882a593Smuzhiyun 		set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 		/* Prevent disconnects from causing scanning to be re-enabled */
1246*4882a593Smuzhiyun 		hdev->scanning_paused = true;
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun 		/* Run commands before disconnecting */
1249*4882a593Smuzhiyun 		hci_req_run(&req, suspend_req_complete);
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 		disconnect_counter = 0;
1252*4882a593Smuzhiyun 		/* Soft disconnect everything (power off) */
1253*4882a593Smuzhiyun 		list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1254*4882a593Smuzhiyun 			hci_disconnect(conn, HCI_ERROR_REMOTE_POWER_OFF);
1255*4882a593Smuzhiyun 			disconnect_counter++;
1256*4882a593Smuzhiyun 		}
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun 		if (disconnect_counter > 0) {
1259*4882a593Smuzhiyun 			bt_dev_dbg(hdev,
1260*4882a593Smuzhiyun 				   "Had %d disconnects. Will wait on them",
1261*4882a593Smuzhiyun 				   disconnect_counter);
1262*4882a593Smuzhiyun 			set_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks);
1263*4882a593Smuzhiyun 		}
1264*4882a593Smuzhiyun 	} else if (next == BT_SUSPEND_CONFIGURE_WAKE) {
1265*4882a593Smuzhiyun 		/* Unpause to take care of updating scanning params */
1266*4882a593Smuzhiyun 		hdev->scanning_paused = false;
1267*4882a593Smuzhiyun 		/* Enable event filter for paired devices */
1268*4882a593Smuzhiyun 		hci_req_set_event_filter(&req);
1269*4882a593Smuzhiyun 		/* Enable passive scan at lower duty cycle */
1270*4882a593Smuzhiyun 		hci_req_config_le_suspend_scan(&req);
1271*4882a593Smuzhiyun 		/* Pause scan changes again. */
1272*4882a593Smuzhiyun 		hdev->scanning_paused = true;
1273*4882a593Smuzhiyun 		hci_req_run(&req, suspend_req_complete);
1274*4882a593Smuzhiyun 	} else {
1275*4882a593Smuzhiyun 		hdev->suspended = false;
1276*4882a593Smuzhiyun 		hdev->scanning_paused = false;
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 		hci_req_clear_event_filter(&req);
1279*4882a593Smuzhiyun 		/* Reset passive/background scanning to normal */
1280*4882a593Smuzhiyun 		hci_req_config_le_suspend_scan(&req);
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 		/* Unpause directed advertising */
1283*4882a593Smuzhiyun 		hdev->advertising_paused = false;
1284*4882a593Smuzhiyun 		if (hdev->advertising_old_state) {
1285*4882a593Smuzhiyun 			set_bit(SUSPEND_UNPAUSE_ADVERTISING,
1286*4882a593Smuzhiyun 				hdev->suspend_tasks);
1287*4882a593Smuzhiyun 			hci_dev_set_flag(hdev, HCI_ADVERTISING);
1288*4882a593Smuzhiyun 			queue_work(hdev->req_workqueue,
1289*4882a593Smuzhiyun 				   &hdev->discoverable_update);
1290*4882a593Smuzhiyun 			hdev->advertising_old_state = 0;
1291*4882a593Smuzhiyun 		}
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 		/* Resume other advertisements */
1294*4882a593Smuzhiyun 		if (hdev->adv_instance_cnt)
1295*4882a593Smuzhiyun 			hci_resume_adv_instances(&req);
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 		/* Unpause discovery */
1298*4882a593Smuzhiyun 		hdev->discovery_paused = false;
1299*4882a593Smuzhiyun 		if (hdev->discovery_old_state != DISCOVERY_STOPPED &&
1300*4882a593Smuzhiyun 		    hdev->discovery_old_state != DISCOVERY_STOPPING) {
1301*4882a593Smuzhiyun 			set_bit(SUSPEND_UNPAUSE_DISCOVERY, hdev->suspend_tasks);
1302*4882a593Smuzhiyun 			hci_discovery_set_state(hdev, DISCOVERY_STARTING);
1303*4882a593Smuzhiyun 			queue_work(hdev->req_workqueue, &hdev->discov_update);
1304*4882a593Smuzhiyun 		}
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 		hci_req_run(&req, suspend_req_complete);
1307*4882a593Smuzhiyun 	}
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 	hdev->suspend_state = next;
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun done:
1312*4882a593Smuzhiyun 	clear_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks);
1313*4882a593Smuzhiyun 	wake_up(&hdev->suspend_wait_q);
1314*4882a593Smuzhiyun }
1315*4882a593Smuzhiyun 
get_cur_adv_instance_scan_rsp_len(struct hci_dev * hdev)1316*4882a593Smuzhiyun static u8 get_cur_adv_instance_scan_rsp_len(struct hci_dev *hdev)
1317*4882a593Smuzhiyun {
1318*4882a593Smuzhiyun 	u8 instance = hdev->cur_adv_instance;
1319*4882a593Smuzhiyun 	struct adv_info *adv_instance;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	/* Instance 0x00 always set local name */
1322*4882a593Smuzhiyun 	if (instance == 0x00)
1323*4882a593Smuzhiyun 		return 1;
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	adv_instance = hci_find_adv_instance(hdev, instance);
1326*4882a593Smuzhiyun 	if (!adv_instance)
1327*4882a593Smuzhiyun 		return 0;
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	/* TODO: Take into account the "appearance" and "local-name" flags here.
1330*4882a593Smuzhiyun 	 * These are currently being ignored as they are not supported.
1331*4882a593Smuzhiyun 	 */
1332*4882a593Smuzhiyun 	return adv_instance->scan_rsp_len;
1333*4882a593Smuzhiyun }
1334*4882a593Smuzhiyun 
__hci_req_disable_advertising(struct hci_request * req)1335*4882a593Smuzhiyun void __hci_req_disable_advertising(struct hci_request *req)
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun 	if (ext_adv_capable(req->hdev)) {
1338*4882a593Smuzhiyun 		__hci_req_disable_ext_adv_instance(req, 0x00);
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	} else {
1341*4882a593Smuzhiyun 		u8 enable = 0x00;
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1344*4882a593Smuzhiyun 	}
1345*4882a593Smuzhiyun }
1346*4882a593Smuzhiyun 
get_adv_instance_flags(struct hci_dev * hdev,u8 instance)1347*4882a593Smuzhiyun static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
1348*4882a593Smuzhiyun {
1349*4882a593Smuzhiyun 	u32 flags;
1350*4882a593Smuzhiyun 	struct adv_info *adv_instance;
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun 	if (instance == 0x00) {
1353*4882a593Smuzhiyun 		/* Instance 0 always manages the "Tx Power" and "Flags"
1354*4882a593Smuzhiyun 		 * fields
1355*4882a593Smuzhiyun 		 */
1356*4882a593Smuzhiyun 		flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 		/* For instance 0, the HCI_ADVERTISING_CONNECTABLE setting
1359*4882a593Smuzhiyun 		 * corresponds to the "connectable" instance flag.
1360*4882a593Smuzhiyun 		 */
1361*4882a593Smuzhiyun 		if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
1362*4882a593Smuzhiyun 			flags |= MGMT_ADV_FLAG_CONNECTABLE;
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 		if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
1365*4882a593Smuzhiyun 			flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
1366*4882a593Smuzhiyun 		else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
1367*4882a593Smuzhiyun 			flags |= MGMT_ADV_FLAG_DISCOV;
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 		return flags;
1370*4882a593Smuzhiyun 	}
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	adv_instance = hci_find_adv_instance(hdev, instance);
1373*4882a593Smuzhiyun 
1374*4882a593Smuzhiyun 	/* Return 0 when we got an invalid instance identifier. */
1375*4882a593Smuzhiyun 	if (!adv_instance)
1376*4882a593Smuzhiyun 		return 0;
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 	return adv_instance->flags;
1379*4882a593Smuzhiyun }
1380*4882a593Smuzhiyun 
adv_use_rpa(struct hci_dev * hdev,uint32_t flags)1381*4882a593Smuzhiyun static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
1382*4882a593Smuzhiyun {
1383*4882a593Smuzhiyun 	/* If privacy is not enabled don't use RPA */
1384*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
1385*4882a593Smuzhiyun 		return false;
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	/* If basic privacy mode is enabled use RPA */
1388*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
1389*4882a593Smuzhiyun 		return true;
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 	/* If limited privacy mode is enabled don't use RPA if we're
1392*4882a593Smuzhiyun 	 * both discoverable and bondable.
1393*4882a593Smuzhiyun 	 */
1394*4882a593Smuzhiyun 	if ((flags & MGMT_ADV_FLAG_DISCOV) &&
1395*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_BONDABLE))
1396*4882a593Smuzhiyun 		return false;
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	/* We're neither bondable nor discoverable in the limited
1399*4882a593Smuzhiyun 	 * privacy mode, therefore use RPA.
1400*4882a593Smuzhiyun 	 */
1401*4882a593Smuzhiyun 	return true;
1402*4882a593Smuzhiyun }
1403*4882a593Smuzhiyun 
is_advertising_allowed(struct hci_dev * hdev,bool connectable)1404*4882a593Smuzhiyun static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
1405*4882a593Smuzhiyun {
1406*4882a593Smuzhiyun 	/* If there is no connection we are OK to advertise. */
1407*4882a593Smuzhiyun 	if (hci_conn_num(hdev, LE_LINK) == 0)
1408*4882a593Smuzhiyun 		return true;
1409*4882a593Smuzhiyun 
1410*4882a593Smuzhiyun 	/* Check le_states if there is any connection in slave role. */
1411*4882a593Smuzhiyun 	if (hdev->conn_hash.le_num_slave > 0) {
1412*4882a593Smuzhiyun 		/* Slave connection state and non connectable mode bit 20. */
1413*4882a593Smuzhiyun 		if (!connectable && !(hdev->le_states[2] & 0x10))
1414*4882a593Smuzhiyun 			return false;
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 		/* Slave connection state and connectable mode bit 38
1417*4882a593Smuzhiyun 		 * and scannable bit 21.
1418*4882a593Smuzhiyun 		 */
1419*4882a593Smuzhiyun 		if (connectable && (!(hdev->le_states[4] & 0x40) ||
1420*4882a593Smuzhiyun 				    !(hdev->le_states[2] & 0x20)))
1421*4882a593Smuzhiyun 			return false;
1422*4882a593Smuzhiyun 	}
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 	/* Check le_states if there is any connection in master role. */
1425*4882a593Smuzhiyun 	if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_slave) {
1426*4882a593Smuzhiyun 		/* Master connection state and non connectable mode bit 18. */
1427*4882a593Smuzhiyun 		if (!connectable && !(hdev->le_states[2] & 0x02))
1428*4882a593Smuzhiyun 			return false;
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 		/* Master connection state and connectable mode bit 35 and
1431*4882a593Smuzhiyun 		 * scannable 19.
1432*4882a593Smuzhiyun 		 */
1433*4882a593Smuzhiyun 		if (connectable && (!(hdev->le_states[4] & 0x08) ||
1434*4882a593Smuzhiyun 				    !(hdev->le_states[2] & 0x08)))
1435*4882a593Smuzhiyun 			return false;
1436*4882a593Smuzhiyun 	}
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	return true;
1439*4882a593Smuzhiyun }
1440*4882a593Smuzhiyun 
__hci_req_enable_advertising(struct hci_request * req)1441*4882a593Smuzhiyun void __hci_req_enable_advertising(struct hci_request *req)
1442*4882a593Smuzhiyun {
1443*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
1444*4882a593Smuzhiyun 	struct hci_cp_le_set_adv_param cp;
1445*4882a593Smuzhiyun 	u8 own_addr_type, enable = 0x01;
1446*4882a593Smuzhiyun 	bool connectable;
1447*4882a593Smuzhiyun 	u16 adv_min_interval, adv_max_interval;
1448*4882a593Smuzhiyun 	u32 flags;
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	flags = get_adv_instance_flags(hdev, hdev->cur_adv_instance);
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun 	/* If the "connectable" instance flag was not set, then choose between
1453*4882a593Smuzhiyun 	 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1454*4882a593Smuzhiyun 	 */
1455*4882a593Smuzhiyun 	connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1456*4882a593Smuzhiyun 		      mgmt_get_connectable(hdev);
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun 	if (!is_advertising_allowed(hdev, connectable))
1459*4882a593Smuzhiyun 		return;
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_LE_ADV))
1462*4882a593Smuzhiyun 		__hci_req_disable_advertising(req);
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun 	/* Clear the HCI_LE_ADV bit temporarily so that the
1465*4882a593Smuzhiyun 	 * hci_update_random_address knows that it's safe to go ahead
1466*4882a593Smuzhiyun 	 * and write a new random address. The flag will be set back on
1467*4882a593Smuzhiyun 	 * as soon as the SET_ADV_ENABLE HCI command completes.
1468*4882a593Smuzhiyun 	 */
1469*4882a593Smuzhiyun 	hci_dev_clear_flag(hdev, HCI_LE_ADV);
1470*4882a593Smuzhiyun 
1471*4882a593Smuzhiyun 	/* Set require_privacy to true only when non-connectable
1472*4882a593Smuzhiyun 	 * advertising is used. In that case it is fine to use a
1473*4882a593Smuzhiyun 	 * non-resolvable private address.
1474*4882a593Smuzhiyun 	 */
1475*4882a593Smuzhiyun 	if (hci_update_random_address(req, !connectable,
1476*4882a593Smuzhiyun 				      adv_use_rpa(hdev, flags),
1477*4882a593Smuzhiyun 				      &own_addr_type) < 0)
1478*4882a593Smuzhiyun 		return;
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	memset(&cp, 0, sizeof(cp));
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 	if (connectable) {
1483*4882a593Smuzhiyun 		cp.type = LE_ADV_IND;
1484*4882a593Smuzhiyun 
1485*4882a593Smuzhiyun 		adv_min_interval = hdev->le_adv_min_interval;
1486*4882a593Smuzhiyun 		adv_max_interval = hdev->le_adv_max_interval;
1487*4882a593Smuzhiyun 	} else {
1488*4882a593Smuzhiyun 		if (get_cur_adv_instance_scan_rsp_len(hdev))
1489*4882a593Smuzhiyun 			cp.type = LE_ADV_SCAN_IND;
1490*4882a593Smuzhiyun 		else
1491*4882a593Smuzhiyun 			cp.type = LE_ADV_NONCONN_IND;
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 		if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1494*4882a593Smuzhiyun 		    hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1495*4882a593Smuzhiyun 			adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1496*4882a593Smuzhiyun 			adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1497*4882a593Smuzhiyun 		} else {
1498*4882a593Smuzhiyun 			adv_min_interval = hdev->le_adv_min_interval;
1499*4882a593Smuzhiyun 			adv_max_interval = hdev->le_adv_max_interval;
1500*4882a593Smuzhiyun 		}
1501*4882a593Smuzhiyun 	}
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	cp.min_interval = cpu_to_le16(adv_min_interval);
1504*4882a593Smuzhiyun 	cp.max_interval = cpu_to_le16(adv_max_interval);
1505*4882a593Smuzhiyun 	cp.own_address_type = own_addr_type;
1506*4882a593Smuzhiyun 	cp.channel_map = hdev->le_adv_channel_map;
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1511*4882a593Smuzhiyun }
1512*4882a593Smuzhiyun 
append_local_name(struct hci_dev * hdev,u8 * ptr,u8 ad_len)1513*4882a593Smuzhiyun u8 append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
1514*4882a593Smuzhiyun {
1515*4882a593Smuzhiyun 	size_t short_len;
1516*4882a593Smuzhiyun 	size_t complete_len;
1517*4882a593Smuzhiyun 
1518*4882a593Smuzhiyun 	/* no space left for name (+ NULL + type + len) */
1519*4882a593Smuzhiyun 	if ((HCI_MAX_AD_LENGTH - ad_len) < HCI_MAX_SHORT_NAME_LENGTH + 3)
1520*4882a593Smuzhiyun 		return ad_len;
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun 	/* use complete name if present and fits */
1523*4882a593Smuzhiyun 	complete_len = strlen(hdev->dev_name);
1524*4882a593Smuzhiyun 	if (complete_len && complete_len <= HCI_MAX_SHORT_NAME_LENGTH)
1525*4882a593Smuzhiyun 		return eir_append_data(ptr, ad_len, EIR_NAME_COMPLETE,
1526*4882a593Smuzhiyun 				       hdev->dev_name, complete_len + 1);
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 	/* use short name if present */
1529*4882a593Smuzhiyun 	short_len = strlen(hdev->short_name);
1530*4882a593Smuzhiyun 	if (short_len)
1531*4882a593Smuzhiyun 		return eir_append_data(ptr, ad_len, EIR_NAME_SHORT,
1532*4882a593Smuzhiyun 				       hdev->short_name, short_len + 1);
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	/* use shortened full name if present, we already know that name
1535*4882a593Smuzhiyun 	 * is longer then HCI_MAX_SHORT_NAME_LENGTH
1536*4882a593Smuzhiyun 	 */
1537*4882a593Smuzhiyun 	if (complete_len) {
1538*4882a593Smuzhiyun 		u8 name[HCI_MAX_SHORT_NAME_LENGTH + 1];
1539*4882a593Smuzhiyun 
1540*4882a593Smuzhiyun 		memcpy(name, hdev->dev_name, HCI_MAX_SHORT_NAME_LENGTH);
1541*4882a593Smuzhiyun 		name[HCI_MAX_SHORT_NAME_LENGTH] = '\0';
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun 		return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, name,
1544*4882a593Smuzhiyun 				       sizeof(name));
1545*4882a593Smuzhiyun 	}
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 	return ad_len;
1548*4882a593Smuzhiyun }
1549*4882a593Smuzhiyun 
append_appearance(struct hci_dev * hdev,u8 * ptr,u8 ad_len)1550*4882a593Smuzhiyun static u8 append_appearance(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
1551*4882a593Smuzhiyun {
1552*4882a593Smuzhiyun 	return eir_append_le16(ptr, ad_len, EIR_APPEARANCE, hdev->appearance);
1553*4882a593Smuzhiyun }
1554*4882a593Smuzhiyun 
create_default_scan_rsp_data(struct hci_dev * hdev,u8 * ptr)1555*4882a593Smuzhiyun static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
1556*4882a593Smuzhiyun {
1557*4882a593Smuzhiyun 	u8 scan_rsp_len = 0;
1558*4882a593Smuzhiyun 
1559*4882a593Smuzhiyun 	if (hdev->appearance) {
1560*4882a593Smuzhiyun 		scan_rsp_len = append_appearance(hdev, ptr, scan_rsp_len);
1561*4882a593Smuzhiyun 	}
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun 	return append_local_name(hdev, ptr, scan_rsp_len);
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun 
create_instance_scan_rsp_data(struct hci_dev * hdev,u8 instance,u8 * ptr)1566*4882a593Smuzhiyun static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 instance,
1567*4882a593Smuzhiyun 					u8 *ptr)
1568*4882a593Smuzhiyun {
1569*4882a593Smuzhiyun 	struct adv_info *adv_instance;
1570*4882a593Smuzhiyun 	u32 instance_flags;
1571*4882a593Smuzhiyun 	u8 scan_rsp_len = 0;
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun 	adv_instance = hci_find_adv_instance(hdev, instance);
1574*4882a593Smuzhiyun 	if (!adv_instance)
1575*4882a593Smuzhiyun 		return 0;
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun 	instance_flags = adv_instance->flags;
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	if ((instance_flags & MGMT_ADV_FLAG_APPEARANCE) && hdev->appearance) {
1580*4882a593Smuzhiyun 		scan_rsp_len = append_appearance(hdev, ptr, scan_rsp_len);
1581*4882a593Smuzhiyun 	}
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun 	memcpy(&ptr[scan_rsp_len], adv_instance->scan_rsp_data,
1584*4882a593Smuzhiyun 	       adv_instance->scan_rsp_len);
1585*4882a593Smuzhiyun 
1586*4882a593Smuzhiyun 	scan_rsp_len += adv_instance->scan_rsp_len;
1587*4882a593Smuzhiyun 
1588*4882a593Smuzhiyun 	if (instance_flags & MGMT_ADV_FLAG_LOCAL_NAME)
1589*4882a593Smuzhiyun 		scan_rsp_len = append_local_name(hdev, ptr, scan_rsp_len);
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	return scan_rsp_len;
1592*4882a593Smuzhiyun }
1593*4882a593Smuzhiyun 
__hci_req_update_scan_rsp_data(struct hci_request * req,u8 instance)1594*4882a593Smuzhiyun void __hci_req_update_scan_rsp_data(struct hci_request *req, u8 instance)
1595*4882a593Smuzhiyun {
1596*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
1597*4882a593Smuzhiyun 	u8 len;
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1600*4882a593Smuzhiyun 		return;
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 	if (ext_adv_capable(hdev)) {
1603*4882a593Smuzhiyun 		struct hci_cp_le_set_ext_scan_rsp_data cp;
1604*4882a593Smuzhiyun 
1605*4882a593Smuzhiyun 		memset(&cp, 0, sizeof(cp));
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 		if (instance)
1608*4882a593Smuzhiyun 			len = create_instance_scan_rsp_data(hdev, instance,
1609*4882a593Smuzhiyun 							    cp.data);
1610*4882a593Smuzhiyun 		else
1611*4882a593Smuzhiyun 			len = create_default_scan_rsp_data(hdev, cp.data);
1612*4882a593Smuzhiyun 
1613*4882a593Smuzhiyun 		if (hdev->scan_rsp_data_len == len &&
1614*4882a593Smuzhiyun 		    !memcmp(cp.data, hdev->scan_rsp_data, len))
1615*4882a593Smuzhiyun 			return;
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun 		memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1618*4882a593Smuzhiyun 		hdev->scan_rsp_data_len = len;
1619*4882a593Smuzhiyun 
1620*4882a593Smuzhiyun 		cp.handle = instance;
1621*4882a593Smuzhiyun 		cp.length = len;
1622*4882a593Smuzhiyun 		cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1623*4882a593Smuzhiyun 		cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, sizeof(cp),
1626*4882a593Smuzhiyun 			    &cp);
1627*4882a593Smuzhiyun 	} else {
1628*4882a593Smuzhiyun 		struct hci_cp_le_set_scan_rsp_data cp;
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 		memset(&cp, 0, sizeof(cp));
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun 		if (instance)
1633*4882a593Smuzhiyun 			len = create_instance_scan_rsp_data(hdev, instance,
1634*4882a593Smuzhiyun 							    cp.data);
1635*4882a593Smuzhiyun 		else
1636*4882a593Smuzhiyun 			len = create_default_scan_rsp_data(hdev, cp.data);
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun 		if (hdev->scan_rsp_data_len == len &&
1639*4882a593Smuzhiyun 		    !memcmp(cp.data, hdev->scan_rsp_data, len))
1640*4882a593Smuzhiyun 			return;
1641*4882a593Smuzhiyun 
1642*4882a593Smuzhiyun 		memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1643*4882a593Smuzhiyun 		hdev->scan_rsp_data_len = len;
1644*4882a593Smuzhiyun 
1645*4882a593Smuzhiyun 		cp.length = len;
1646*4882a593Smuzhiyun 
1647*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
1648*4882a593Smuzhiyun 	}
1649*4882a593Smuzhiyun }
1650*4882a593Smuzhiyun 
create_instance_adv_data(struct hci_dev * hdev,u8 instance,u8 * ptr)1651*4882a593Smuzhiyun static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
1652*4882a593Smuzhiyun {
1653*4882a593Smuzhiyun 	struct adv_info *adv_instance = NULL;
1654*4882a593Smuzhiyun 	u8 ad_len = 0, flags = 0;
1655*4882a593Smuzhiyun 	u32 instance_flags;
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun 	/* Return 0 when the current instance identifier is invalid. */
1658*4882a593Smuzhiyun 	if (instance) {
1659*4882a593Smuzhiyun 		adv_instance = hci_find_adv_instance(hdev, instance);
1660*4882a593Smuzhiyun 		if (!adv_instance)
1661*4882a593Smuzhiyun 			return 0;
1662*4882a593Smuzhiyun 	}
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	instance_flags = get_adv_instance_flags(hdev, instance);
1665*4882a593Smuzhiyun 
1666*4882a593Smuzhiyun 	/* If instance already has the flags set skip adding it once
1667*4882a593Smuzhiyun 	 * again.
1668*4882a593Smuzhiyun 	 */
1669*4882a593Smuzhiyun 	if (adv_instance && eir_get_data(adv_instance->adv_data,
1670*4882a593Smuzhiyun 					 adv_instance->adv_data_len, EIR_FLAGS,
1671*4882a593Smuzhiyun 					 NULL))
1672*4882a593Smuzhiyun 		goto skip_flags;
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun 	/* The Add Advertising command allows userspace to set both the general
1675*4882a593Smuzhiyun 	 * and limited discoverable flags.
1676*4882a593Smuzhiyun 	 */
1677*4882a593Smuzhiyun 	if (instance_flags & MGMT_ADV_FLAG_DISCOV)
1678*4882a593Smuzhiyun 		flags |= LE_AD_GENERAL;
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
1681*4882a593Smuzhiyun 		flags |= LE_AD_LIMITED;
1682*4882a593Smuzhiyun 
1683*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1684*4882a593Smuzhiyun 		flags |= LE_AD_NO_BREDR;
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun 	if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
1687*4882a593Smuzhiyun 		/* If a discovery flag wasn't provided, simply use the global
1688*4882a593Smuzhiyun 		 * settings.
1689*4882a593Smuzhiyun 		 */
1690*4882a593Smuzhiyun 		if (!flags)
1691*4882a593Smuzhiyun 			flags |= mgmt_get_adv_discov_flags(hdev);
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun 		/* If flags would still be empty, then there is no need to
1694*4882a593Smuzhiyun 		 * include the "Flags" AD field".
1695*4882a593Smuzhiyun 		 */
1696*4882a593Smuzhiyun 		if (flags) {
1697*4882a593Smuzhiyun 			ptr[0] = 0x02;
1698*4882a593Smuzhiyun 			ptr[1] = EIR_FLAGS;
1699*4882a593Smuzhiyun 			ptr[2] = flags;
1700*4882a593Smuzhiyun 
1701*4882a593Smuzhiyun 			ad_len += 3;
1702*4882a593Smuzhiyun 			ptr += 3;
1703*4882a593Smuzhiyun 		}
1704*4882a593Smuzhiyun 	}
1705*4882a593Smuzhiyun 
1706*4882a593Smuzhiyun skip_flags:
1707*4882a593Smuzhiyun 	if (adv_instance) {
1708*4882a593Smuzhiyun 		memcpy(ptr, adv_instance->adv_data,
1709*4882a593Smuzhiyun 		       adv_instance->adv_data_len);
1710*4882a593Smuzhiyun 		ad_len += adv_instance->adv_data_len;
1711*4882a593Smuzhiyun 		ptr += adv_instance->adv_data_len;
1712*4882a593Smuzhiyun 	}
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 	if (instance_flags & MGMT_ADV_FLAG_TX_POWER) {
1715*4882a593Smuzhiyun 		s8 adv_tx_power;
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun 		if (ext_adv_capable(hdev)) {
1718*4882a593Smuzhiyun 			if (adv_instance)
1719*4882a593Smuzhiyun 				adv_tx_power = adv_instance->tx_power;
1720*4882a593Smuzhiyun 			else
1721*4882a593Smuzhiyun 				adv_tx_power = hdev->adv_tx_power;
1722*4882a593Smuzhiyun 		} else {
1723*4882a593Smuzhiyun 			adv_tx_power = hdev->adv_tx_power;
1724*4882a593Smuzhiyun 		}
1725*4882a593Smuzhiyun 
1726*4882a593Smuzhiyun 		/* Provide Tx Power only if we can provide a valid value for it */
1727*4882a593Smuzhiyun 		if (adv_tx_power != HCI_TX_POWER_INVALID) {
1728*4882a593Smuzhiyun 			ptr[0] = 0x02;
1729*4882a593Smuzhiyun 			ptr[1] = EIR_TX_POWER;
1730*4882a593Smuzhiyun 			ptr[2] = (u8)adv_tx_power;
1731*4882a593Smuzhiyun 
1732*4882a593Smuzhiyun 			ad_len += 3;
1733*4882a593Smuzhiyun 			ptr += 3;
1734*4882a593Smuzhiyun 		}
1735*4882a593Smuzhiyun 	}
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun 	return ad_len;
1738*4882a593Smuzhiyun }
1739*4882a593Smuzhiyun 
__hci_req_update_adv_data(struct hci_request * req,u8 instance)1740*4882a593Smuzhiyun void __hci_req_update_adv_data(struct hci_request *req, u8 instance)
1741*4882a593Smuzhiyun {
1742*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
1743*4882a593Smuzhiyun 	u8 len;
1744*4882a593Smuzhiyun 
1745*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1746*4882a593Smuzhiyun 		return;
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 	if (ext_adv_capable(hdev)) {
1749*4882a593Smuzhiyun 		struct hci_cp_le_set_ext_adv_data cp;
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun 		memset(&cp, 0, sizeof(cp));
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun 		len = create_instance_adv_data(hdev, instance, cp.data);
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun 		/* There's nothing to do if the data hasn't changed */
1756*4882a593Smuzhiyun 		if (hdev->adv_data_len == len &&
1757*4882a593Smuzhiyun 		    memcmp(cp.data, hdev->adv_data, len) == 0)
1758*4882a593Smuzhiyun 			return;
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 		memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1761*4882a593Smuzhiyun 		hdev->adv_data_len = len;
1762*4882a593Smuzhiyun 
1763*4882a593Smuzhiyun 		cp.length = len;
1764*4882a593Smuzhiyun 		cp.handle = instance;
1765*4882a593Smuzhiyun 		cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1766*4882a593Smuzhiyun 		cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1767*4882a593Smuzhiyun 
1768*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_DATA, sizeof(cp), &cp);
1769*4882a593Smuzhiyun 	} else {
1770*4882a593Smuzhiyun 		struct hci_cp_le_set_adv_data cp;
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 		memset(&cp, 0, sizeof(cp));
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun 		len = create_instance_adv_data(hdev, instance, cp.data);
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 		/* There's nothing to do if the data hasn't changed */
1777*4882a593Smuzhiyun 		if (hdev->adv_data_len == len &&
1778*4882a593Smuzhiyun 		    memcmp(cp.data, hdev->adv_data, len) == 0)
1779*4882a593Smuzhiyun 			return;
1780*4882a593Smuzhiyun 
1781*4882a593Smuzhiyun 		memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1782*4882a593Smuzhiyun 		hdev->adv_data_len = len;
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun 		cp.length = len;
1785*4882a593Smuzhiyun 
1786*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
1787*4882a593Smuzhiyun 	}
1788*4882a593Smuzhiyun }
1789*4882a593Smuzhiyun 
hci_req_update_adv_data(struct hci_dev * hdev,u8 instance)1790*4882a593Smuzhiyun int hci_req_update_adv_data(struct hci_dev *hdev, u8 instance)
1791*4882a593Smuzhiyun {
1792*4882a593Smuzhiyun 	struct hci_request req;
1793*4882a593Smuzhiyun 
1794*4882a593Smuzhiyun 	hci_req_init(&req, hdev);
1795*4882a593Smuzhiyun 	__hci_req_update_adv_data(&req, instance);
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun 	return hci_req_run(&req, NULL);
1798*4882a593Smuzhiyun }
1799*4882a593Smuzhiyun 
enable_addr_resolution_complete(struct hci_dev * hdev,u8 status,u16 opcode)1800*4882a593Smuzhiyun static void enable_addr_resolution_complete(struct hci_dev *hdev, u8 status,
1801*4882a593Smuzhiyun 					    u16 opcode)
1802*4882a593Smuzhiyun {
1803*4882a593Smuzhiyun 	BT_DBG("%s status %u", hdev->name, status);
1804*4882a593Smuzhiyun }
1805*4882a593Smuzhiyun 
hci_req_disable_address_resolution(struct hci_dev * hdev)1806*4882a593Smuzhiyun void hci_req_disable_address_resolution(struct hci_dev *hdev)
1807*4882a593Smuzhiyun {
1808*4882a593Smuzhiyun 	struct hci_request req;
1809*4882a593Smuzhiyun 	__u8 enable = 0x00;
1810*4882a593Smuzhiyun 
1811*4882a593Smuzhiyun 	if (!use_ll_privacy(hdev) &&
1812*4882a593Smuzhiyun 	    !hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
1813*4882a593Smuzhiyun 		return;
1814*4882a593Smuzhiyun 
1815*4882a593Smuzhiyun 	hci_req_init(&req, hdev);
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun 	hci_req_add(&req, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, 1, &enable);
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun 	hci_req_run(&req, enable_addr_resolution_complete);
1820*4882a593Smuzhiyun }
1821*4882a593Smuzhiyun 
adv_enable_complete(struct hci_dev * hdev,u8 status,u16 opcode)1822*4882a593Smuzhiyun static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
1823*4882a593Smuzhiyun {
1824*4882a593Smuzhiyun 	BT_DBG("%s status %u", hdev->name, status);
1825*4882a593Smuzhiyun }
1826*4882a593Smuzhiyun 
hci_req_reenable_advertising(struct hci_dev * hdev)1827*4882a593Smuzhiyun void hci_req_reenable_advertising(struct hci_dev *hdev)
1828*4882a593Smuzhiyun {
1829*4882a593Smuzhiyun 	struct hci_request req;
1830*4882a593Smuzhiyun 
1831*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1832*4882a593Smuzhiyun 	    list_empty(&hdev->adv_instances))
1833*4882a593Smuzhiyun 		return;
1834*4882a593Smuzhiyun 
1835*4882a593Smuzhiyun 	hci_req_init(&req, hdev);
1836*4882a593Smuzhiyun 
1837*4882a593Smuzhiyun 	if (hdev->cur_adv_instance) {
1838*4882a593Smuzhiyun 		__hci_req_schedule_adv_instance(&req, hdev->cur_adv_instance,
1839*4882a593Smuzhiyun 						true);
1840*4882a593Smuzhiyun 	} else {
1841*4882a593Smuzhiyun 		if (ext_adv_capable(hdev)) {
1842*4882a593Smuzhiyun 			__hci_req_start_ext_adv(&req, 0x00);
1843*4882a593Smuzhiyun 		} else {
1844*4882a593Smuzhiyun 			__hci_req_update_adv_data(&req, 0x00);
1845*4882a593Smuzhiyun 			__hci_req_update_scan_rsp_data(&req, 0x00);
1846*4882a593Smuzhiyun 			__hci_req_enable_advertising(&req);
1847*4882a593Smuzhiyun 		}
1848*4882a593Smuzhiyun 	}
1849*4882a593Smuzhiyun 
1850*4882a593Smuzhiyun 	hci_req_run(&req, adv_enable_complete);
1851*4882a593Smuzhiyun }
1852*4882a593Smuzhiyun 
adv_timeout_expire(struct work_struct * work)1853*4882a593Smuzhiyun static void adv_timeout_expire(struct work_struct *work)
1854*4882a593Smuzhiyun {
1855*4882a593Smuzhiyun 	struct hci_dev *hdev = container_of(work, struct hci_dev,
1856*4882a593Smuzhiyun 					    adv_instance_expire.work);
1857*4882a593Smuzhiyun 
1858*4882a593Smuzhiyun 	struct hci_request req;
1859*4882a593Smuzhiyun 	u8 instance;
1860*4882a593Smuzhiyun 
1861*4882a593Smuzhiyun 	BT_DBG("%s", hdev->name);
1862*4882a593Smuzhiyun 
1863*4882a593Smuzhiyun 	hci_dev_lock(hdev);
1864*4882a593Smuzhiyun 
1865*4882a593Smuzhiyun 	hdev->adv_instance_timeout = 0;
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun 	instance = hdev->cur_adv_instance;
1868*4882a593Smuzhiyun 	if (instance == 0x00)
1869*4882a593Smuzhiyun 		goto unlock;
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun 	hci_req_init(&req, hdev);
1872*4882a593Smuzhiyun 
1873*4882a593Smuzhiyun 	hci_req_clear_adv_instance(hdev, NULL, &req, instance, false);
1874*4882a593Smuzhiyun 
1875*4882a593Smuzhiyun 	if (list_empty(&hdev->adv_instances))
1876*4882a593Smuzhiyun 		__hci_req_disable_advertising(&req);
1877*4882a593Smuzhiyun 
1878*4882a593Smuzhiyun 	hci_req_run(&req, NULL);
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun unlock:
1881*4882a593Smuzhiyun 	hci_dev_unlock(hdev);
1882*4882a593Smuzhiyun }
1883*4882a593Smuzhiyun 
hci_get_random_address(struct hci_dev * hdev,bool require_privacy,bool use_rpa,struct adv_info * adv_instance,u8 * own_addr_type,bdaddr_t * rand_addr)1884*4882a593Smuzhiyun int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
1885*4882a593Smuzhiyun 			   bool use_rpa, struct adv_info *adv_instance,
1886*4882a593Smuzhiyun 			   u8 *own_addr_type, bdaddr_t *rand_addr)
1887*4882a593Smuzhiyun {
1888*4882a593Smuzhiyun 	int err;
1889*4882a593Smuzhiyun 
1890*4882a593Smuzhiyun 	bacpy(rand_addr, BDADDR_ANY);
1891*4882a593Smuzhiyun 
1892*4882a593Smuzhiyun 	/* If privacy is enabled use a resolvable private address. If
1893*4882a593Smuzhiyun 	 * current RPA has expired then generate a new one.
1894*4882a593Smuzhiyun 	 */
1895*4882a593Smuzhiyun 	if (use_rpa) {
1896*4882a593Smuzhiyun 		int to;
1897*4882a593Smuzhiyun 
1898*4882a593Smuzhiyun 		/* If Controller supports LL Privacy use own address type is
1899*4882a593Smuzhiyun 		 * 0x03
1900*4882a593Smuzhiyun 		 */
1901*4882a593Smuzhiyun 		if (use_ll_privacy(hdev))
1902*4882a593Smuzhiyun 			*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
1903*4882a593Smuzhiyun 		else
1904*4882a593Smuzhiyun 			*own_addr_type = ADDR_LE_DEV_RANDOM;
1905*4882a593Smuzhiyun 
1906*4882a593Smuzhiyun 		if (adv_instance) {
1907*4882a593Smuzhiyun 			if (!adv_instance->rpa_expired &&
1908*4882a593Smuzhiyun 			    !bacmp(&adv_instance->random_addr, &hdev->rpa))
1909*4882a593Smuzhiyun 				return 0;
1910*4882a593Smuzhiyun 
1911*4882a593Smuzhiyun 			adv_instance->rpa_expired = false;
1912*4882a593Smuzhiyun 		} else {
1913*4882a593Smuzhiyun 			if (!hci_dev_test_and_clear_flag(hdev, HCI_RPA_EXPIRED) &&
1914*4882a593Smuzhiyun 			    !bacmp(&hdev->random_addr, &hdev->rpa))
1915*4882a593Smuzhiyun 				return 0;
1916*4882a593Smuzhiyun 		}
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun 		err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
1919*4882a593Smuzhiyun 		if (err < 0) {
1920*4882a593Smuzhiyun 			bt_dev_err(hdev, "failed to generate new RPA");
1921*4882a593Smuzhiyun 			return err;
1922*4882a593Smuzhiyun 		}
1923*4882a593Smuzhiyun 
1924*4882a593Smuzhiyun 		bacpy(rand_addr, &hdev->rpa);
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun 		to = msecs_to_jiffies(hdev->rpa_timeout * 1000);
1927*4882a593Smuzhiyun 		if (adv_instance)
1928*4882a593Smuzhiyun 			queue_delayed_work(hdev->workqueue,
1929*4882a593Smuzhiyun 					   &adv_instance->rpa_expired_cb, to);
1930*4882a593Smuzhiyun 		else
1931*4882a593Smuzhiyun 			queue_delayed_work(hdev->workqueue,
1932*4882a593Smuzhiyun 					   &hdev->rpa_expired, to);
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun 		return 0;
1935*4882a593Smuzhiyun 	}
1936*4882a593Smuzhiyun 
1937*4882a593Smuzhiyun 	/* In case of required privacy without resolvable private address,
1938*4882a593Smuzhiyun 	 * use an non-resolvable private address. This is useful for
1939*4882a593Smuzhiyun 	 * non-connectable advertising.
1940*4882a593Smuzhiyun 	 */
1941*4882a593Smuzhiyun 	if (require_privacy) {
1942*4882a593Smuzhiyun 		bdaddr_t nrpa;
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun 		while (true) {
1945*4882a593Smuzhiyun 			/* The non-resolvable private address is generated
1946*4882a593Smuzhiyun 			 * from random six bytes with the two most significant
1947*4882a593Smuzhiyun 			 * bits cleared.
1948*4882a593Smuzhiyun 			 */
1949*4882a593Smuzhiyun 			get_random_bytes(&nrpa, 6);
1950*4882a593Smuzhiyun 			nrpa.b[5] &= 0x3f;
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun 			/* The non-resolvable private address shall not be
1953*4882a593Smuzhiyun 			 * equal to the public address.
1954*4882a593Smuzhiyun 			 */
1955*4882a593Smuzhiyun 			if (bacmp(&hdev->bdaddr, &nrpa))
1956*4882a593Smuzhiyun 				break;
1957*4882a593Smuzhiyun 		}
1958*4882a593Smuzhiyun 
1959*4882a593Smuzhiyun 		*own_addr_type = ADDR_LE_DEV_RANDOM;
1960*4882a593Smuzhiyun 		bacpy(rand_addr, &nrpa);
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 		return 0;
1963*4882a593Smuzhiyun 	}
1964*4882a593Smuzhiyun 
1965*4882a593Smuzhiyun 	/* No privacy so use a public address. */
1966*4882a593Smuzhiyun 	*own_addr_type = ADDR_LE_DEV_PUBLIC;
1967*4882a593Smuzhiyun 
1968*4882a593Smuzhiyun 	return 0;
1969*4882a593Smuzhiyun }
1970*4882a593Smuzhiyun 
__hci_req_clear_ext_adv_sets(struct hci_request * req)1971*4882a593Smuzhiyun void __hci_req_clear_ext_adv_sets(struct hci_request *req)
1972*4882a593Smuzhiyun {
1973*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_LE_CLEAR_ADV_SETS, 0, NULL);
1974*4882a593Smuzhiyun }
1975*4882a593Smuzhiyun 
__hci_req_setup_ext_adv_instance(struct hci_request * req,u8 instance)1976*4882a593Smuzhiyun int __hci_req_setup_ext_adv_instance(struct hci_request *req, u8 instance)
1977*4882a593Smuzhiyun {
1978*4882a593Smuzhiyun 	struct hci_cp_le_set_ext_adv_params cp;
1979*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
1980*4882a593Smuzhiyun 	bool connectable;
1981*4882a593Smuzhiyun 	u32 flags;
1982*4882a593Smuzhiyun 	bdaddr_t random_addr;
1983*4882a593Smuzhiyun 	u8 own_addr_type;
1984*4882a593Smuzhiyun 	int err;
1985*4882a593Smuzhiyun 	struct adv_info *adv_instance;
1986*4882a593Smuzhiyun 	bool secondary_adv;
1987*4882a593Smuzhiyun 
1988*4882a593Smuzhiyun 	if (instance > 0) {
1989*4882a593Smuzhiyun 		adv_instance = hci_find_adv_instance(hdev, instance);
1990*4882a593Smuzhiyun 		if (!adv_instance)
1991*4882a593Smuzhiyun 			return -EINVAL;
1992*4882a593Smuzhiyun 	} else {
1993*4882a593Smuzhiyun 		adv_instance = NULL;
1994*4882a593Smuzhiyun 	}
1995*4882a593Smuzhiyun 
1996*4882a593Smuzhiyun 	flags = get_adv_instance_flags(hdev, instance);
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun 	/* If the "connectable" instance flag was not set, then choose between
1999*4882a593Smuzhiyun 	 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
2000*4882a593Smuzhiyun 	 */
2001*4882a593Smuzhiyun 	connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
2002*4882a593Smuzhiyun 		      mgmt_get_connectable(hdev);
2003*4882a593Smuzhiyun 
2004*4882a593Smuzhiyun 	if (!is_advertising_allowed(hdev, connectable))
2005*4882a593Smuzhiyun 		return -EPERM;
2006*4882a593Smuzhiyun 
2007*4882a593Smuzhiyun 	/* Set require_privacy to true only when non-connectable
2008*4882a593Smuzhiyun 	 * advertising is used. In that case it is fine to use a
2009*4882a593Smuzhiyun 	 * non-resolvable private address.
2010*4882a593Smuzhiyun 	 */
2011*4882a593Smuzhiyun 	err = hci_get_random_address(hdev, !connectable,
2012*4882a593Smuzhiyun 				     adv_use_rpa(hdev, flags), adv_instance,
2013*4882a593Smuzhiyun 				     &own_addr_type, &random_addr);
2014*4882a593Smuzhiyun 	if (err < 0)
2015*4882a593Smuzhiyun 		return err;
2016*4882a593Smuzhiyun 
2017*4882a593Smuzhiyun 	memset(&cp, 0, sizeof(cp));
2018*4882a593Smuzhiyun 
2019*4882a593Smuzhiyun 	/* In ext adv set param interval is 3 octets */
2020*4882a593Smuzhiyun 	hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
2021*4882a593Smuzhiyun 	hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
2022*4882a593Smuzhiyun 
2023*4882a593Smuzhiyun 	secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
2024*4882a593Smuzhiyun 
2025*4882a593Smuzhiyun 	if (connectable) {
2026*4882a593Smuzhiyun 		if (secondary_adv)
2027*4882a593Smuzhiyun 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
2028*4882a593Smuzhiyun 		else
2029*4882a593Smuzhiyun 			cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
2030*4882a593Smuzhiyun 	} else if (get_adv_instance_scan_rsp_len(hdev, instance)) {
2031*4882a593Smuzhiyun 		if (secondary_adv)
2032*4882a593Smuzhiyun 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
2033*4882a593Smuzhiyun 		else
2034*4882a593Smuzhiyun 			cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
2035*4882a593Smuzhiyun 	} else {
2036*4882a593Smuzhiyun 		if (secondary_adv)
2037*4882a593Smuzhiyun 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
2038*4882a593Smuzhiyun 		else
2039*4882a593Smuzhiyun 			cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
2040*4882a593Smuzhiyun 	}
2041*4882a593Smuzhiyun 
2042*4882a593Smuzhiyun 	cp.own_addr_type = own_addr_type;
2043*4882a593Smuzhiyun 	cp.channel_map = hdev->le_adv_channel_map;
2044*4882a593Smuzhiyun 	cp.tx_power = 127;
2045*4882a593Smuzhiyun 	cp.handle = instance;
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun 	if (flags & MGMT_ADV_FLAG_SEC_2M) {
2048*4882a593Smuzhiyun 		cp.primary_phy = HCI_ADV_PHY_1M;
2049*4882a593Smuzhiyun 		cp.secondary_phy = HCI_ADV_PHY_2M;
2050*4882a593Smuzhiyun 	} else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
2051*4882a593Smuzhiyun 		cp.primary_phy = HCI_ADV_PHY_CODED;
2052*4882a593Smuzhiyun 		cp.secondary_phy = HCI_ADV_PHY_CODED;
2053*4882a593Smuzhiyun 	} else {
2054*4882a593Smuzhiyun 		/* In all other cases use 1M */
2055*4882a593Smuzhiyun 		cp.primary_phy = HCI_ADV_PHY_1M;
2056*4882a593Smuzhiyun 		cp.secondary_phy = HCI_ADV_PHY_1M;
2057*4882a593Smuzhiyun 	}
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_PARAMS, sizeof(cp), &cp);
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 	if (own_addr_type == ADDR_LE_DEV_RANDOM &&
2062*4882a593Smuzhiyun 	    bacmp(&random_addr, BDADDR_ANY)) {
2063*4882a593Smuzhiyun 		struct hci_cp_le_set_adv_set_rand_addr cp;
2064*4882a593Smuzhiyun 
2065*4882a593Smuzhiyun 		/* Check if random address need to be updated */
2066*4882a593Smuzhiyun 		if (adv_instance) {
2067*4882a593Smuzhiyun 			if (!bacmp(&random_addr, &adv_instance->random_addr))
2068*4882a593Smuzhiyun 				return 0;
2069*4882a593Smuzhiyun 		} else {
2070*4882a593Smuzhiyun 			if (!bacmp(&random_addr, &hdev->random_addr))
2071*4882a593Smuzhiyun 				return 0;
2072*4882a593Smuzhiyun 		}
2073*4882a593Smuzhiyun 
2074*4882a593Smuzhiyun 		memset(&cp, 0, sizeof(cp));
2075*4882a593Smuzhiyun 
2076*4882a593Smuzhiyun 		cp.handle = instance;
2077*4882a593Smuzhiyun 		bacpy(&cp.bdaddr, &random_addr);
2078*4882a593Smuzhiyun 
2079*4882a593Smuzhiyun 		hci_req_add(req,
2080*4882a593Smuzhiyun 			    HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
2081*4882a593Smuzhiyun 			    sizeof(cp), &cp);
2082*4882a593Smuzhiyun 	}
2083*4882a593Smuzhiyun 
2084*4882a593Smuzhiyun 	return 0;
2085*4882a593Smuzhiyun }
2086*4882a593Smuzhiyun 
__hci_req_enable_ext_advertising(struct hci_request * req,u8 instance)2087*4882a593Smuzhiyun int __hci_req_enable_ext_advertising(struct hci_request *req, u8 instance)
2088*4882a593Smuzhiyun {
2089*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2090*4882a593Smuzhiyun 	struct hci_cp_le_set_ext_adv_enable *cp;
2091*4882a593Smuzhiyun 	struct hci_cp_ext_adv_set *adv_set;
2092*4882a593Smuzhiyun 	u8 data[sizeof(*cp) + sizeof(*adv_set) * 1];
2093*4882a593Smuzhiyun 	struct adv_info *adv_instance;
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun 	if (instance > 0) {
2096*4882a593Smuzhiyun 		adv_instance = hci_find_adv_instance(hdev, instance);
2097*4882a593Smuzhiyun 		if (!adv_instance)
2098*4882a593Smuzhiyun 			return -EINVAL;
2099*4882a593Smuzhiyun 	} else {
2100*4882a593Smuzhiyun 		adv_instance = NULL;
2101*4882a593Smuzhiyun 	}
2102*4882a593Smuzhiyun 
2103*4882a593Smuzhiyun 	cp = (void *) data;
2104*4882a593Smuzhiyun 	adv_set = (void *) cp->data;
2105*4882a593Smuzhiyun 
2106*4882a593Smuzhiyun 	memset(cp, 0, sizeof(*cp));
2107*4882a593Smuzhiyun 
2108*4882a593Smuzhiyun 	cp->enable = 0x01;
2109*4882a593Smuzhiyun 	cp->num_of_sets = 0x01;
2110*4882a593Smuzhiyun 
2111*4882a593Smuzhiyun 	memset(adv_set, 0, sizeof(*adv_set));
2112*4882a593Smuzhiyun 
2113*4882a593Smuzhiyun 	adv_set->handle = instance;
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun 	/* Set duration per instance since controller is responsible for
2116*4882a593Smuzhiyun 	 * scheduling it.
2117*4882a593Smuzhiyun 	 */
2118*4882a593Smuzhiyun 	if (adv_instance && adv_instance->timeout) {
2119*4882a593Smuzhiyun 		u16 duration = adv_instance->timeout * MSEC_PER_SEC;
2120*4882a593Smuzhiyun 
2121*4882a593Smuzhiyun 		/* Time = N * 10 ms */
2122*4882a593Smuzhiyun 		adv_set->duration = cpu_to_le16(duration / 10);
2123*4882a593Smuzhiyun 	}
2124*4882a593Smuzhiyun 
2125*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_ENABLE,
2126*4882a593Smuzhiyun 		    sizeof(*cp) + sizeof(*adv_set) * cp->num_of_sets,
2127*4882a593Smuzhiyun 		    data);
2128*4882a593Smuzhiyun 
2129*4882a593Smuzhiyun 	return 0;
2130*4882a593Smuzhiyun }
2131*4882a593Smuzhiyun 
__hci_req_disable_ext_adv_instance(struct hci_request * req,u8 instance)2132*4882a593Smuzhiyun int __hci_req_disable_ext_adv_instance(struct hci_request *req, u8 instance)
2133*4882a593Smuzhiyun {
2134*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2135*4882a593Smuzhiyun 	struct hci_cp_le_set_ext_adv_enable *cp;
2136*4882a593Smuzhiyun 	struct hci_cp_ext_adv_set *adv_set;
2137*4882a593Smuzhiyun 	u8 data[sizeof(*cp) + sizeof(*adv_set) * 1];
2138*4882a593Smuzhiyun 	u8 req_size;
2139*4882a593Smuzhiyun 
2140*4882a593Smuzhiyun 	/* If request specifies an instance that doesn't exist, fail */
2141*4882a593Smuzhiyun 	if (instance > 0 && !hci_find_adv_instance(hdev, instance))
2142*4882a593Smuzhiyun 		return -EINVAL;
2143*4882a593Smuzhiyun 
2144*4882a593Smuzhiyun 	memset(data, 0, sizeof(data));
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun 	cp = (void *)data;
2147*4882a593Smuzhiyun 	adv_set = (void *)cp->data;
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun 	/* Instance 0x00 indicates all advertising instances will be disabled */
2150*4882a593Smuzhiyun 	cp->num_of_sets = !!instance;
2151*4882a593Smuzhiyun 	cp->enable = 0x00;
2152*4882a593Smuzhiyun 
2153*4882a593Smuzhiyun 	adv_set->handle = instance;
2154*4882a593Smuzhiyun 
2155*4882a593Smuzhiyun 	req_size = sizeof(*cp) + sizeof(*adv_set) * cp->num_of_sets;
2156*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_ENABLE, req_size, data);
2157*4882a593Smuzhiyun 
2158*4882a593Smuzhiyun 	return 0;
2159*4882a593Smuzhiyun }
2160*4882a593Smuzhiyun 
__hci_req_remove_ext_adv_instance(struct hci_request * req,u8 instance)2161*4882a593Smuzhiyun int __hci_req_remove_ext_adv_instance(struct hci_request *req, u8 instance)
2162*4882a593Smuzhiyun {
2163*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun 	/* If request specifies an instance that doesn't exist, fail */
2166*4882a593Smuzhiyun 	if (instance > 0 && !hci_find_adv_instance(hdev, instance))
2167*4882a593Smuzhiyun 		return -EINVAL;
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_LE_REMOVE_ADV_SET, sizeof(instance), &instance);
2170*4882a593Smuzhiyun 
2171*4882a593Smuzhiyun 	return 0;
2172*4882a593Smuzhiyun }
2173*4882a593Smuzhiyun 
__hci_req_start_ext_adv(struct hci_request * req,u8 instance)2174*4882a593Smuzhiyun int __hci_req_start_ext_adv(struct hci_request *req, u8 instance)
2175*4882a593Smuzhiyun {
2176*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2177*4882a593Smuzhiyun 	struct adv_info *adv_instance = hci_find_adv_instance(hdev, instance);
2178*4882a593Smuzhiyun 	int err;
2179*4882a593Smuzhiyun 
2180*4882a593Smuzhiyun 	/* If instance isn't pending, the chip knows about it, and it's safe to
2181*4882a593Smuzhiyun 	 * disable
2182*4882a593Smuzhiyun 	 */
2183*4882a593Smuzhiyun 	if (adv_instance && !adv_instance->pending)
2184*4882a593Smuzhiyun 		__hci_req_disable_ext_adv_instance(req, instance);
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun 	err = __hci_req_setup_ext_adv_instance(req, instance);
2187*4882a593Smuzhiyun 	if (err < 0)
2188*4882a593Smuzhiyun 		return err;
2189*4882a593Smuzhiyun 
2190*4882a593Smuzhiyun 	__hci_req_update_scan_rsp_data(req, instance);
2191*4882a593Smuzhiyun 	__hci_req_enable_ext_advertising(req, instance);
2192*4882a593Smuzhiyun 
2193*4882a593Smuzhiyun 	return 0;
2194*4882a593Smuzhiyun }
2195*4882a593Smuzhiyun 
__hci_req_schedule_adv_instance(struct hci_request * req,u8 instance,bool force)2196*4882a593Smuzhiyun int __hci_req_schedule_adv_instance(struct hci_request *req, u8 instance,
2197*4882a593Smuzhiyun 				    bool force)
2198*4882a593Smuzhiyun {
2199*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2200*4882a593Smuzhiyun 	struct adv_info *adv_instance = NULL;
2201*4882a593Smuzhiyun 	u16 timeout;
2202*4882a593Smuzhiyun 
2203*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2204*4882a593Smuzhiyun 	    list_empty(&hdev->adv_instances))
2205*4882a593Smuzhiyun 		return -EPERM;
2206*4882a593Smuzhiyun 
2207*4882a593Smuzhiyun 	if (hdev->adv_instance_timeout)
2208*4882a593Smuzhiyun 		return -EBUSY;
2209*4882a593Smuzhiyun 
2210*4882a593Smuzhiyun 	adv_instance = hci_find_adv_instance(hdev, instance);
2211*4882a593Smuzhiyun 	if (!adv_instance)
2212*4882a593Smuzhiyun 		return -ENOENT;
2213*4882a593Smuzhiyun 
2214*4882a593Smuzhiyun 	/* A zero timeout means unlimited advertising. As long as there is
2215*4882a593Smuzhiyun 	 * only one instance, duration should be ignored. We still set a timeout
2216*4882a593Smuzhiyun 	 * in case further instances are being added later on.
2217*4882a593Smuzhiyun 	 *
2218*4882a593Smuzhiyun 	 * If the remaining lifetime of the instance is more than the duration
2219*4882a593Smuzhiyun 	 * then the timeout corresponds to the duration, otherwise it will be
2220*4882a593Smuzhiyun 	 * reduced to the remaining instance lifetime.
2221*4882a593Smuzhiyun 	 */
2222*4882a593Smuzhiyun 	if (adv_instance->timeout == 0 ||
2223*4882a593Smuzhiyun 	    adv_instance->duration <= adv_instance->remaining_time)
2224*4882a593Smuzhiyun 		timeout = adv_instance->duration;
2225*4882a593Smuzhiyun 	else
2226*4882a593Smuzhiyun 		timeout = adv_instance->remaining_time;
2227*4882a593Smuzhiyun 
2228*4882a593Smuzhiyun 	/* The remaining time is being reduced unless the instance is being
2229*4882a593Smuzhiyun 	 * advertised without time limit.
2230*4882a593Smuzhiyun 	 */
2231*4882a593Smuzhiyun 	if (adv_instance->timeout)
2232*4882a593Smuzhiyun 		adv_instance->remaining_time =
2233*4882a593Smuzhiyun 				adv_instance->remaining_time - timeout;
2234*4882a593Smuzhiyun 
2235*4882a593Smuzhiyun 	/* Only use work for scheduling instances with legacy advertising */
2236*4882a593Smuzhiyun 	if (!ext_adv_capable(hdev)) {
2237*4882a593Smuzhiyun 		hdev->adv_instance_timeout = timeout;
2238*4882a593Smuzhiyun 		queue_delayed_work(hdev->req_workqueue,
2239*4882a593Smuzhiyun 			   &hdev->adv_instance_expire,
2240*4882a593Smuzhiyun 			   msecs_to_jiffies(timeout * 1000));
2241*4882a593Smuzhiyun 	}
2242*4882a593Smuzhiyun 
2243*4882a593Smuzhiyun 	/* If we're just re-scheduling the same instance again then do not
2244*4882a593Smuzhiyun 	 * execute any HCI commands. This happens when a single instance is
2245*4882a593Smuzhiyun 	 * being advertised.
2246*4882a593Smuzhiyun 	 */
2247*4882a593Smuzhiyun 	if (!force && hdev->cur_adv_instance == instance &&
2248*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_LE_ADV))
2249*4882a593Smuzhiyun 		return 0;
2250*4882a593Smuzhiyun 
2251*4882a593Smuzhiyun 	hdev->cur_adv_instance = instance;
2252*4882a593Smuzhiyun 	if (ext_adv_capable(hdev)) {
2253*4882a593Smuzhiyun 		__hci_req_start_ext_adv(req, instance);
2254*4882a593Smuzhiyun 	} else {
2255*4882a593Smuzhiyun 		__hci_req_update_adv_data(req, instance);
2256*4882a593Smuzhiyun 		__hci_req_update_scan_rsp_data(req, instance);
2257*4882a593Smuzhiyun 		__hci_req_enable_advertising(req);
2258*4882a593Smuzhiyun 	}
2259*4882a593Smuzhiyun 
2260*4882a593Smuzhiyun 	return 0;
2261*4882a593Smuzhiyun }
2262*4882a593Smuzhiyun 
2263*4882a593Smuzhiyun /* For a single instance:
2264*4882a593Smuzhiyun  * - force == true: The instance will be removed even when its remaining
2265*4882a593Smuzhiyun  *   lifetime is not zero.
2266*4882a593Smuzhiyun  * - force == false: the instance will be deactivated but kept stored unless
2267*4882a593Smuzhiyun  *   the remaining lifetime is zero.
2268*4882a593Smuzhiyun  *
2269*4882a593Smuzhiyun  * For instance == 0x00:
2270*4882a593Smuzhiyun  * - force == true: All instances will be removed regardless of their timeout
2271*4882a593Smuzhiyun  *   setting.
2272*4882a593Smuzhiyun  * - force == false: Only instances that have a timeout will be removed.
2273*4882a593Smuzhiyun  */
hci_req_clear_adv_instance(struct hci_dev * hdev,struct sock * sk,struct hci_request * req,u8 instance,bool force)2274*4882a593Smuzhiyun void hci_req_clear_adv_instance(struct hci_dev *hdev, struct sock *sk,
2275*4882a593Smuzhiyun 				struct hci_request *req, u8 instance,
2276*4882a593Smuzhiyun 				bool force)
2277*4882a593Smuzhiyun {
2278*4882a593Smuzhiyun 	struct adv_info *adv_instance, *n, *next_instance = NULL;
2279*4882a593Smuzhiyun 	int err;
2280*4882a593Smuzhiyun 	u8 rem_inst;
2281*4882a593Smuzhiyun 
2282*4882a593Smuzhiyun 	/* Cancel any timeout concerning the removed instance(s). */
2283*4882a593Smuzhiyun 	if (!instance || hdev->cur_adv_instance == instance)
2284*4882a593Smuzhiyun 		cancel_adv_timeout(hdev);
2285*4882a593Smuzhiyun 
2286*4882a593Smuzhiyun 	/* Get the next instance to advertise BEFORE we remove
2287*4882a593Smuzhiyun 	 * the current one. This can be the same instance again
2288*4882a593Smuzhiyun 	 * if there is only one instance.
2289*4882a593Smuzhiyun 	 */
2290*4882a593Smuzhiyun 	if (instance && hdev->cur_adv_instance == instance)
2291*4882a593Smuzhiyun 		next_instance = hci_get_next_instance(hdev, instance);
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun 	if (instance == 0x00) {
2294*4882a593Smuzhiyun 		list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
2295*4882a593Smuzhiyun 					 list) {
2296*4882a593Smuzhiyun 			if (!(force || adv_instance->timeout))
2297*4882a593Smuzhiyun 				continue;
2298*4882a593Smuzhiyun 
2299*4882a593Smuzhiyun 			rem_inst = adv_instance->instance;
2300*4882a593Smuzhiyun 			err = hci_remove_adv_instance(hdev, rem_inst);
2301*4882a593Smuzhiyun 			if (!err)
2302*4882a593Smuzhiyun 				mgmt_advertising_removed(sk, hdev, rem_inst);
2303*4882a593Smuzhiyun 		}
2304*4882a593Smuzhiyun 	} else {
2305*4882a593Smuzhiyun 		adv_instance = hci_find_adv_instance(hdev, instance);
2306*4882a593Smuzhiyun 
2307*4882a593Smuzhiyun 		if (force || (adv_instance && adv_instance->timeout &&
2308*4882a593Smuzhiyun 			      !adv_instance->remaining_time)) {
2309*4882a593Smuzhiyun 			/* Don't advertise a removed instance. */
2310*4882a593Smuzhiyun 			if (next_instance &&
2311*4882a593Smuzhiyun 			    next_instance->instance == instance)
2312*4882a593Smuzhiyun 				next_instance = NULL;
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun 			err = hci_remove_adv_instance(hdev, instance);
2315*4882a593Smuzhiyun 			if (!err)
2316*4882a593Smuzhiyun 				mgmt_advertising_removed(sk, hdev, instance);
2317*4882a593Smuzhiyun 		}
2318*4882a593Smuzhiyun 	}
2319*4882a593Smuzhiyun 
2320*4882a593Smuzhiyun 	if (!req || !hdev_is_powered(hdev) ||
2321*4882a593Smuzhiyun 	    hci_dev_test_flag(hdev, HCI_ADVERTISING))
2322*4882a593Smuzhiyun 		return;
2323*4882a593Smuzhiyun 
2324*4882a593Smuzhiyun 	if (next_instance && !ext_adv_capable(hdev))
2325*4882a593Smuzhiyun 		__hci_req_schedule_adv_instance(req, next_instance->instance,
2326*4882a593Smuzhiyun 						false);
2327*4882a593Smuzhiyun }
2328*4882a593Smuzhiyun 
set_random_addr(struct hci_request * req,bdaddr_t * rpa)2329*4882a593Smuzhiyun static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
2330*4882a593Smuzhiyun {
2331*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2332*4882a593Smuzhiyun 
2333*4882a593Smuzhiyun 	/* If we're advertising or initiating an LE connection we can't
2334*4882a593Smuzhiyun 	 * go ahead and change the random address at this time. This is
2335*4882a593Smuzhiyun 	 * because the eventual initiator address used for the
2336*4882a593Smuzhiyun 	 * subsequently created connection will be undefined (some
2337*4882a593Smuzhiyun 	 * controllers use the new address and others the one we had
2338*4882a593Smuzhiyun 	 * when the operation started).
2339*4882a593Smuzhiyun 	 *
2340*4882a593Smuzhiyun 	 * In this kind of scenario skip the update and let the random
2341*4882a593Smuzhiyun 	 * address be updated at the next cycle.
2342*4882a593Smuzhiyun 	 */
2343*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
2344*4882a593Smuzhiyun 	    hci_lookup_le_connect(hdev)) {
2345*4882a593Smuzhiyun 		BT_DBG("Deferring random address update");
2346*4882a593Smuzhiyun 		hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
2347*4882a593Smuzhiyun 		return;
2348*4882a593Smuzhiyun 	}
2349*4882a593Smuzhiyun 
2350*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa);
2351*4882a593Smuzhiyun }
2352*4882a593Smuzhiyun 
hci_update_random_address(struct hci_request * req,bool require_privacy,bool use_rpa,u8 * own_addr_type)2353*4882a593Smuzhiyun int hci_update_random_address(struct hci_request *req, bool require_privacy,
2354*4882a593Smuzhiyun 			      bool use_rpa, u8 *own_addr_type)
2355*4882a593Smuzhiyun {
2356*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2357*4882a593Smuzhiyun 	int err;
2358*4882a593Smuzhiyun 
2359*4882a593Smuzhiyun 	/* If privacy is enabled use a resolvable private address. If
2360*4882a593Smuzhiyun 	 * current RPA has expired or there is something else than
2361*4882a593Smuzhiyun 	 * the current RPA in use, then generate a new one.
2362*4882a593Smuzhiyun 	 */
2363*4882a593Smuzhiyun 	if (use_rpa) {
2364*4882a593Smuzhiyun 		int to;
2365*4882a593Smuzhiyun 
2366*4882a593Smuzhiyun 		/* If Controller supports LL Privacy use own address type is
2367*4882a593Smuzhiyun 		 * 0x03
2368*4882a593Smuzhiyun 		 */
2369*4882a593Smuzhiyun 		if (use_ll_privacy(hdev))
2370*4882a593Smuzhiyun 			*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
2371*4882a593Smuzhiyun 		else
2372*4882a593Smuzhiyun 			*own_addr_type = ADDR_LE_DEV_RANDOM;
2373*4882a593Smuzhiyun 
2374*4882a593Smuzhiyun 		if (!hci_dev_test_and_clear_flag(hdev, HCI_RPA_EXPIRED) &&
2375*4882a593Smuzhiyun 		    !bacmp(&hdev->random_addr, &hdev->rpa))
2376*4882a593Smuzhiyun 			return 0;
2377*4882a593Smuzhiyun 
2378*4882a593Smuzhiyun 		err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
2379*4882a593Smuzhiyun 		if (err < 0) {
2380*4882a593Smuzhiyun 			bt_dev_err(hdev, "failed to generate new RPA");
2381*4882a593Smuzhiyun 			return err;
2382*4882a593Smuzhiyun 		}
2383*4882a593Smuzhiyun 
2384*4882a593Smuzhiyun 		set_random_addr(req, &hdev->rpa);
2385*4882a593Smuzhiyun 
2386*4882a593Smuzhiyun 		to = msecs_to_jiffies(hdev->rpa_timeout * 1000);
2387*4882a593Smuzhiyun 		queue_delayed_work(hdev->workqueue, &hdev->rpa_expired, to);
2388*4882a593Smuzhiyun 
2389*4882a593Smuzhiyun 		return 0;
2390*4882a593Smuzhiyun 	}
2391*4882a593Smuzhiyun 
2392*4882a593Smuzhiyun 	/* In case of required privacy without resolvable private address,
2393*4882a593Smuzhiyun 	 * use an non-resolvable private address. This is useful for active
2394*4882a593Smuzhiyun 	 * scanning and non-connectable advertising.
2395*4882a593Smuzhiyun 	 */
2396*4882a593Smuzhiyun 	if (require_privacy) {
2397*4882a593Smuzhiyun 		bdaddr_t nrpa;
2398*4882a593Smuzhiyun 
2399*4882a593Smuzhiyun 		while (true) {
2400*4882a593Smuzhiyun 			/* The non-resolvable private address is generated
2401*4882a593Smuzhiyun 			 * from random six bytes with the two most significant
2402*4882a593Smuzhiyun 			 * bits cleared.
2403*4882a593Smuzhiyun 			 */
2404*4882a593Smuzhiyun 			get_random_bytes(&nrpa, 6);
2405*4882a593Smuzhiyun 			nrpa.b[5] &= 0x3f;
2406*4882a593Smuzhiyun 
2407*4882a593Smuzhiyun 			/* The non-resolvable private address shall not be
2408*4882a593Smuzhiyun 			 * equal to the public address.
2409*4882a593Smuzhiyun 			 */
2410*4882a593Smuzhiyun 			if (bacmp(&hdev->bdaddr, &nrpa))
2411*4882a593Smuzhiyun 				break;
2412*4882a593Smuzhiyun 		}
2413*4882a593Smuzhiyun 
2414*4882a593Smuzhiyun 		*own_addr_type = ADDR_LE_DEV_RANDOM;
2415*4882a593Smuzhiyun 		set_random_addr(req, &nrpa);
2416*4882a593Smuzhiyun 		return 0;
2417*4882a593Smuzhiyun 	}
2418*4882a593Smuzhiyun 
2419*4882a593Smuzhiyun 	/* If forcing static address is in use or there is no public
2420*4882a593Smuzhiyun 	 * address use the static address as random address (but skip
2421*4882a593Smuzhiyun 	 * the HCI command if the current random address is already the
2422*4882a593Smuzhiyun 	 * static one.
2423*4882a593Smuzhiyun 	 *
2424*4882a593Smuzhiyun 	 * In case BR/EDR has been disabled on a dual-mode controller
2425*4882a593Smuzhiyun 	 * and a static address has been configured, then use that
2426*4882a593Smuzhiyun 	 * address instead of the public BR/EDR address.
2427*4882a593Smuzhiyun 	 */
2428*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
2429*4882a593Smuzhiyun 	    !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
2430*4882a593Smuzhiyun 	    (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
2431*4882a593Smuzhiyun 	     bacmp(&hdev->static_addr, BDADDR_ANY))) {
2432*4882a593Smuzhiyun 		*own_addr_type = ADDR_LE_DEV_RANDOM;
2433*4882a593Smuzhiyun 		if (bacmp(&hdev->static_addr, &hdev->random_addr))
2434*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
2435*4882a593Smuzhiyun 				    &hdev->static_addr);
2436*4882a593Smuzhiyun 		return 0;
2437*4882a593Smuzhiyun 	}
2438*4882a593Smuzhiyun 
2439*4882a593Smuzhiyun 	/* Neither privacy nor static address is being used so use a
2440*4882a593Smuzhiyun 	 * public address.
2441*4882a593Smuzhiyun 	 */
2442*4882a593Smuzhiyun 	*own_addr_type = ADDR_LE_DEV_PUBLIC;
2443*4882a593Smuzhiyun 
2444*4882a593Smuzhiyun 	return 0;
2445*4882a593Smuzhiyun }
2446*4882a593Smuzhiyun 
disconnected_whitelist_entries(struct hci_dev * hdev)2447*4882a593Smuzhiyun static bool disconnected_whitelist_entries(struct hci_dev *hdev)
2448*4882a593Smuzhiyun {
2449*4882a593Smuzhiyun 	struct bdaddr_list *b;
2450*4882a593Smuzhiyun 
2451*4882a593Smuzhiyun 	list_for_each_entry(b, &hdev->whitelist, list) {
2452*4882a593Smuzhiyun 		struct hci_conn *conn;
2453*4882a593Smuzhiyun 
2454*4882a593Smuzhiyun 		conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
2455*4882a593Smuzhiyun 		if (!conn)
2456*4882a593Smuzhiyun 			return true;
2457*4882a593Smuzhiyun 
2458*4882a593Smuzhiyun 		if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2459*4882a593Smuzhiyun 			return true;
2460*4882a593Smuzhiyun 	}
2461*4882a593Smuzhiyun 
2462*4882a593Smuzhiyun 	return false;
2463*4882a593Smuzhiyun }
2464*4882a593Smuzhiyun 
__hci_req_update_scan(struct hci_request * req)2465*4882a593Smuzhiyun void __hci_req_update_scan(struct hci_request *req)
2466*4882a593Smuzhiyun {
2467*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2468*4882a593Smuzhiyun 	u8 scan;
2469*4882a593Smuzhiyun 
2470*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2471*4882a593Smuzhiyun 		return;
2472*4882a593Smuzhiyun 
2473*4882a593Smuzhiyun 	if (!hdev_is_powered(hdev))
2474*4882a593Smuzhiyun 		return;
2475*4882a593Smuzhiyun 
2476*4882a593Smuzhiyun 	if (mgmt_powering_down(hdev))
2477*4882a593Smuzhiyun 		return;
2478*4882a593Smuzhiyun 
2479*4882a593Smuzhiyun 	if (hdev->scanning_paused)
2480*4882a593Smuzhiyun 		return;
2481*4882a593Smuzhiyun 
2482*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
2483*4882a593Smuzhiyun 	    disconnected_whitelist_entries(hdev))
2484*4882a593Smuzhiyun 		scan = SCAN_PAGE;
2485*4882a593Smuzhiyun 	else
2486*4882a593Smuzhiyun 		scan = SCAN_DISABLED;
2487*4882a593Smuzhiyun 
2488*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
2489*4882a593Smuzhiyun 		scan |= SCAN_INQUIRY;
2490*4882a593Smuzhiyun 
2491*4882a593Smuzhiyun 	if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
2492*4882a593Smuzhiyun 	    test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
2493*4882a593Smuzhiyun 		return;
2494*4882a593Smuzhiyun 
2495*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
2496*4882a593Smuzhiyun }
2497*4882a593Smuzhiyun 
update_scan(struct hci_request * req,unsigned long opt)2498*4882a593Smuzhiyun static int update_scan(struct hci_request *req, unsigned long opt)
2499*4882a593Smuzhiyun {
2500*4882a593Smuzhiyun 	hci_dev_lock(req->hdev);
2501*4882a593Smuzhiyun 	__hci_req_update_scan(req);
2502*4882a593Smuzhiyun 	hci_dev_unlock(req->hdev);
2503*4882a593Smuzhiyun 	return 0;
2504*4882a593Smuzhiyun }
2505*4882a593Smuzhiyun 
scan_update_work(struct work_struct * work)2506*4882a593Smuzhiyun static void scan_update_work(struct work_struct *work)
2507*4882a593Smuzhiyun {
2508*4882a593Smuzhiyun 	struct hci_dev *hdev = container_of(work, struct hci_dev, scan_update);
2509*4882a593Smuzhiyun 
2510*4882a593Smuzhiyun 	hci_req_sync(hdev, update_scan, 0, HCI_CMD_TIMEOUT, NULL);
2511*4882a593Smuzhiyun }
2512*4882a593Smuzhiyun 
connectable_update(struct hci_request * req,unsigned long opt)2513*4882a593Smuzhiyun static int connectable_update(struct hci_request *req, unsigned long opt)
2514*4882a593Smuzhiyun {
2515*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2516*4882a593Smuzhiyun 
2517*4882a593Smuzhiyun 	hci_dev_lock(hdev);
2518*4882a593Smuzhiyun 
2519*4882a593Smuzhiyun 	__hci_req_update_scan(req);
2520*4882a593Smuzhiyun 
2521*4882a593Smuzhiyun 	/* If BR/EDR is not enabled and we disable advertising as a
2522*4882a593Smuzhiyun 	 * by-product of disabling connectable, we need to update the
2523*4882a593Smuzhiyun 	 * advertising flags.
2524*4882a593Smuzhiyun 	 */
2525*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2526*4882a593Smuzhiyun 		__hci_req_update_adv_data(req, hdev->cur_adv_instance);
2527*4882a593Smuzhiyun 
2528*4882a593Smuzhiyun 	/* Update the advertising parameters if necessary */
2529*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2530*4882a593Smuzhiyun 	    !list_empty(&hdev->adv_instances)) {
2531*4882a593Smuzhiyun 		if (ext_adv_capable(hdev))
2532*4882a593Smuzhiyun 			__hci_req_start_ext_adv(req, hdev->cur_adv_instance);
2533*4882a593Smuzhiyun 		else
2534*4882a593Smuzhiyun 			__hci_req_enable_advertising(req);
2535*4882a593Smuzhiyun 	}
2536*4882a593Smuzhiyun 
2537*4882a593Smuzhiyun 	__hci_update_background_scan(req);
2538*4882a593Smuzhiyun 
2539*4882a593Smuzhiyun 	hci_dev_unlock(hdev);
2540*4882a593Smuzhiyun 
2541*4882a593Smuzhiyun 	return 0;
2542*4882a593Smuzhiyun }
2543*4882a593Smuzhiyun 
connectable_update_work(struct work_struct * work)2544*4882a593Smuzhiyun static void connectable_update_work(struct work_struct *work)
2545*4882a593Smuzhiyun {
2546*4882a593Smuzhiyun 	struct hci_dev *hdev = container_of(work, struct hci_dev,
2547*4882a593Smuzhiyun 					    connectable_update);
2548*4882a593Smuzhiyun 	u8 status;
2549*4882a593Smuzhiyun 
2550*4882a593Smuzhiyun 	hci_req_sync(hdev, connectable_update, 0, HCI_CMD_TIMEOUT, &status);
2551*4882a593Smuzhiyun 	mgmt_set_connectable_complete(hdev, status);
2552*4882a593Smuzhiyun }
2553*4882a593Smuzhiyun 
get_service_classes(struct hci_dev * hdev)2554*4882a593Smuzhiyun static u8 get_service_classes(struct hci_dev *hdev)
2555*4882a593Smuzhiyun {
2556*4882a593Smuzhiyun 	struct bt_uuid *uuid;
2557*4882a593Smuzhiyun 	u8 val = 0;
2558*4882a593Smuzhiyun 
2559*4882a593Smuzhiyun 	list_for_each_entry(uuid, &hdev->uuids, list)
2560*4882a593Smuzhiyun 		val |= uuid->svc_hint;
2561*4882a593Smuzhiyun 
2562*4882a593Smuzhiyun 	return val;
2563*4882a593Smuzhiyun }
2564*4882a593Smuzhiyun 
__hci_req_update_class(struct hci_request * req)2565*4882a593Smuzhiyun void __hci_req_update_class(struct hci_request *req)
2566*4882a593Smuzhiyun {
2567*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2568*4882a593Smuzhiyun 	u8 cod[3];
2569*4882a593Smuzhiyun 
2570*4882a593Smuzhiyun 	BT_DBG("%s", hdev->name);
2571*4882a593Smuzhiyun 
2572*4882a593Smuzhiyun 	if (!hdev_is_powered(hdev))
2573*4882a593Smuzhiyun 		return;
2574*4882a593Smuzhiyun 
2575*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2576*4882a593Smuzhiyun 		return;
2577*4882a593Smuzhiyun 
2578*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
2579*4882a593Smuzhiyun 		return;
2580*4882a593Smuzhiyun 
2581*4882a593Smuzhiyun 	cod[0] = hdev->minor_class;
2582*4882a593Smuzhiyun 	cod[1] = hdev->major_class;
2583*4882a593Smuzhiyun 	cod[2] = get_service_classes(hdev);
2584*4882a593Smuzhiyun 
2585*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
2586*4882a593Smuzhiyun 		cod[1] |= 0x20;
2587*4882a593Smuzhiyun 
2588*4882a593Smuzhiyun 	if (memcmp(cod, hdev->dev_class, 3) == 0)
2589*4882a593Smuzhiyun 		return;
2590*4882a593Smuzhiyun 
2591*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
2592*4882a593Smuzhiyun }
2593*4882a593Smuzhiyun 
write_iac(struct hci_request * req)2594*4882a593Smuzhiyun static void write_iac(struct hci_request *req)
2595*4882a593Smuzhiyun {
2596*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2597*4882a593Smuzhiyun 	struct hci_cp_write_current_iac_lap cp;
2598*4882a593Smuzhiyun 
2599*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
2600*4882a593Smuzhiyun 		return;
2601*4882a593Smuzhiyun 
2602*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
2603*4882a593Smuzhiyun 		/* Limited discoverable mode */
2604*4882a593Smuzhiyun 		cp.num_iac = min_t(u8, hdev->num_iac, 2);
2605*4882a593Smuzhiyun 		cp.iac_lap[0] = 0x00;	/* LIAC */
2606*4882a593Smuzhiyun 		cp.iac_lap[1] = 0x8b;
2607*4882a593Smuzhiyun 		cp.iac_lap[2] = 0x9e;
2608*4882a593Smuzhiyun 		cp.iac_lap[3] = 0x33;	/* GIAC */
2609*4882a593Smuzhiyun 		cp.iac_lap[4] = 0x8b;
2610*4882a593Smuzhiyun 		cp.iac_lap[5] = 0x9e;
2611*4882a593Smuzhiyun 	} else {
2612*4882a593Smuzhiyun 		/* General discoverable mode */
2613*4882a593Smuzhiyun 		cp.num_iac = 1;
2614*4882a593Smuzhiyun 		cp.iac_lap[0] = 0x33;	/* GIAC */
2615*4882a593Smuzhiyun 		cp.iac_lap[1] = 0x8b;
2616*4882a593Smuzhiyun 		cp.iac_lap[2] = 0x9e;
2617*4882a593Smuzhiyun 	}
2618*4882a593Smuzhiyun 
2619*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_WRITE_CURRENT_IAC_LAP,
2620*4882a593Smuzhiyun 		    (cp.num_iac * 3) + 1, &cp);
2621*4882a593Smuzhiyun }
2622*4882a593Smuzhiyun 
discoverable_update(struct hci_request * req,unsigned long opt)2623*4882a593Smuzhiyun static int discoverable_update(struct hci_request *req, unsigned long opt)
2624*4882a593Smuzhiyun {
2625*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2626*4882a593Smuzhiyun 
2627*4882a593Smuzhiyun 	hci_dev_lock(hdev);
2628*4882a593Smuzhiyun 
2629*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
2630*4882a593Smuzhiyun 		write_iac(req);
2631*4882a593Smuzhiyun 		__hci_req_update_scan(req);
2632*4882a593Smuzhiyun 		__hci_req_update_class(req);
2633*4882a593Smuzhiyun 	}
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun 	/* Advertising instances don't use the global discoverable setting, so
2636*4882a593Smuzhiyun 	 * only update AD if advertising was enabled using Set Advertising.
2637*4882a593Smuzhiyun 	 */
2638*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
2639*4882a593Smuzhiyun 		__hci_req_update_adv_data(req, 0x00);
2640*4882a593Smuzhiyun 
2641*4882a593Smuzhiyun 		/* Discoverable mode affects the local advertising
2642*4882a593Smuzhiyun 		 * address in limited privacy mode.
2643*4882a593Smuzhiyun 		 */
2644*4882a593Smuzhiyun 		if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
2645*4882a593Smuzhiyun 			if (ext_adv_capable(hdev))
2646*4882a593Smuzhiyun 				__hci_req_start_ext_adv(req, 0x00);
2647*4882a593Smuzhiyun 			else
2648*4882a593Smuzhiyun 				__hci_req_enable_advertising(req);
2649*4882a593Smuzhiyun 		}
2650*4882a593Smuzhiyun 	}
2651*4882a593Smuzhiyun 
2652*4882a593Smuzhiyun 	hci_dev_unlock(hdev);
2653*4882a593Smuzhiyun 
2654*4882a593Smuzhiyun 	return 0;
2655*4882a593Smuzhiyun }
2656*4882a593Smuzhiyun 
discoverable_update_work(struct work_struct * work)2657*4882a593Smuzhiyun static void discoverable_update_work(struct work_struct *work)
2658*4882a593Smuzhiyun {
2659*4882a593Smuzhiyun 	struct hci_dev *hdev = container_of(work, struct hci_dev,
2660*4882a593Smuzhiyun 					    discoverable_update);
2661*4882a593Smuzhiyun 	u8 status;
2662*4882a593Smuzhiyun 
2663*4882a593Smuzhiyun 	hci_req_sync(hdev, discoverable_update, 0, HCI_CMD_TIMEOUT, &status);
2664*4882a593Smuzhiyun 	mgmt_set_discoverable_complete(hdev, status);
2665*4882a593Smuzhiyun }
2666*4882a593Smuzhiyun 
__hci_abort_conn(struct hci_request * req,struct hci_conn * conn,u8 reason)2667*4882a593Smuzhiyun void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn,
2668*4882a593Smuzhiyun 		      u8 reason)
2669*4882a593Smuzhiyun {
2670*4882a593Smuzhiyun 	switch (conn->state) {
2671*4882a593Smuzhiyun 	case BT_CONNECTED:
2672*4882a593Smuzhiyun 	case BT_CONFIG:
2673*4882a593Smuzhiyun 		if (conn->type == AMP_LINK) {
2674*4882a593Smuzhiyun 			struct hci_cp_disconn_phy_link cp;
2675*4882a593Smuzhiyun 
2676*4882a593Smuzhiyun 			cp.phy_handle = HCI_PHY_HANDLE(conn->handle);
2677*4882a593Smuzhiyun 			cp.reason = reason;
2678*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_DISCONN_PHY_LINK, sizeof(cp),
2679*4882a593Smuzhiyun 				    &cp);
2680*4882a593Smuzhiyun 		} else {
2681*4882a593Smuzhiyun 			struct hci_cp_disconnect dc;
2682*4882a593Smuzhiyun 
2683*4882a593Smuzhiyun 			dc.handle = cpu_to_le16(conn->handle);
2684*4882a593Smuzhiyun 			dc.reason = reason;
2685*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
2686*4882a593Smuzhiyun 		}
2687*4882a593Smuzhiyun 
2688*4882a593Smuzhiyun 		conn->state = BT_DISCONN;
2689*4882a593Smuzhiyun 
2690*4882a593Smuzhiyun 		break;
2691*4882a593Smuzhiyun 	case BT_CONNECT:
2692*4882a593Smuzhiyun 		if (conn->type == LE_LINK) {
2693*4882a593Smuzhiyun 			if (test_bit(HCI_CONN_SCANNING, &conn->flags))
2694*4882a593Smuzhiyun 				break;
2695*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_LE_CREATE_CONN_CANCEL,
2696*4882a593Smuzhiyun 				    0, NULL);
2697*4882a593Smuzhiyun 		} else if (conn->type == ACL_LINK) {
2698*4882a593Smuzhiyun 			if (req->hdev->hci_ver < BLUETOOTH_VER_1_2)
2699*4882a593Smuzhiyun 				break;
2700*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_CREATE_CONN_CANCEL,
2701*4882a593Smuzhiyun 				    6, &conn->dst);
2702*4882a593Smuzhiyun 		}
2703*4882a593Smuzhiyun 		break;
2704*4882a593Smuzhiyun 	case BT_CONNECT2:
2705*4882a593Smuzhiyun 		if (conn->type == ACL_LINK) {
2706*4882a593Smuzhiyun 			struct hci_cp_reject_conn_req rej;
2707*4882a593Smuzhiyun 
2708*4882a593Smuzhiyun 			bacpy(&rej.bdaddr, &conn->dst);
2709*4882a593Smuzhiyun 			rej.reason = reason;
2710*4882a593Smuzhiyun 
2711*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_REJECT_CONN_REQ,
2712*4882a593Smuzhiyun 				    sizeof(rej), &rej);
2713*4882a593Smuzhiyun 		} else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
2714*4882a593Smuzhiyun 			struct hci_cp_reject_sync_conn_req rej;
2715*4882a593Smuzhiyun 
2716*4882a593Smuzhiyun 			bacpy(&rej.bdaddr, &conn->dst);
2717*4882a593Smuzhiyun 
2718*4882a593Smuzhiyun 			/* SCO rejection has its own limited set of
2719*4882a593Smuzhiyun 			 * allowed error values (0x0D-0x0F) which isn't
2720*4882a593Smuzhiyun 			 * compatible with most values passed to this
2721*4882a593Smuzhiyun 			 * function. To be safe hard-code one of the
2722*4882a593Smuzhiyun 			 * values that's suitable for SCO.
2723*4882a593Smuzhiyun 			 */
2724*4882a593Smuzhiyun 			rej.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
2725*4882a593Smuzhiyun 
2726*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_REJECT_SYNC_CONN_REQ,
2727*4882a593Smuzhiyun 				    sizeof(rej), &rej);
2728*4882a593Smuzhiyun 		}
2729*4882a593Smuzhiyun 		break;
2730*4882a593Smuzhiyun 	default:
2731*4882a593Smuzhiyun 		conn->state = BT_CLOSED;
2732*4882a593Smuzhiyun 		break;
2733*4882a593Smuzhiyun 	}
2734*4882a593Smuzhiyun }
2735*4882a593Smuzhiyun 
abort_conn_complete(struct hci_dev * hdev,u8 status,u16 opcode)2736*4882a593Smuzhiyun static void abort_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode)
2737*4882a593Smuzhiyun {
2738*4882a593Smuzhiyun 	if (status)
2739*4882a593Smuzhiyun 		BT_DBG("Failed to abort connection: status 0x%2.2x", status);
2740*4882a593Smuzhiyun }
2741*4882a593Smuzhiyun 
hci_abort_conn(struct hci_conn * conn,u8 reason)2742*4882a593Smuzhiyun int hci_abort_conn(struct hci_conn *conn, u8 reason)
2743*4882a593Smuzhiyun {
2744*4882a593Smuzhiyun 	struct hci_request req;
2745*4882a593Smuzhiyun 	int err;
2746*4882a593Smuzhiyun 
2747*4882a593Smuzhiyun 	hci_req_init(&req, conn->hdev);
2748*4882a593Smuzhiyun 
2749*4882a593Smuzhiyun 	__hci_abort_conn(&req, conn, reason);
2750*4882a593Smuzhiyun 
2751*4882a593Smuzhiyun 	err = hci_req_run(&req, abort_conn_complete);
2752*4882a593Smuzhiyun 	if (err && err != -ENODATA) {
2753*4882a593Smuzhiyun 		bt_dev_err(conn->hdev, "failed to run HCI request: err %d", err);
2754*4882a593Smuzhiyun 		return err;
2755*4882a593Smuzhiyun 	}
2756*4882a593Smuzhiyun 
2757*4882a593Smuzhiyun 	return 0;
2758*4882a593Smuzhiyun }
2759*4882a593Smuzhiyun 
update_bg_scan(struct hci_request * req,unsigned long opt)2760*4882a593Smuzhiyun static int update_bg_scan(struct hci_request *req, unsigned long opt)
2761*4882a593Smuzhiyun {
2762*4882a593Smuzhiyun 	hci_dev_lock(req->hdev);
2763*4882a593Smuzhiyun 	__hci_update_background_scan(req);
2764*4882a593Smuzhiyun 	hci_dev_unlock(req->hdev);
2765*4882a593Smuzhiyun 	return 0;
2766*4882a593Smuzhiyun }
2767*4882a593Smuzhiyun 
bg_scan_update(struct work_struct * work)2768*4882a593Smuzhiyun static void bg_scan_update(struct work_struct *work)
2769*4882a593Smuzhiyun {
2770*4882a593Smuzhiyun 	struct hci_dev *hdev = container_of(work, struct hci_dev,
2771*4882a593Smuzhiyun 					    bg_scan_update);
2772*4882a593Smuzhiyun 	struct hci_conn *conn;
2773*4882a593Smuzhiyun 	u8 status;
2774*4882a593Smuzhiyun 	int err;
2775*4882a593Smuzhiyun 
2776*4882a593Smuzhiyun 	err = hci_req_sync(hdev, update_bg_scan, 0, HCI_CMD_TIMEOUT, &status);
2777*4882a593Smuzhiyun 	if (!err)
2778*4882a593Smuzhiyun 		return;
2779*4882a593Smuzhiyun 
2780*4882a593Smuzhiyun 	hci_dev_lock(hdev);
2781*4882a593Smuzhiyun 
2782*4882a593Smuzhiyun 	conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
2783*4882a593Smuzhiyun 	if (conn)
2784*4882a593Smuzhiyun 		hci_le_conn_failed(conn, status);
2785*4882a593Smuzhiyun 
2786*4882a593Smuzhiyun 	hci_dev_unlock(hdev);
2787*4882a593Smuzhiyun }
2788*4882a593Smuzhiyun 
le_scan_disable(struct hci_request * req,unsigned long opt)2789*4882a593Smuzhiyun static int le_scan_disable(struct hci_request *req, unsigned long opt)
2790*4882a593Smuzhiyun {
2791*4882a593Smuzhiyun 	hci_req_add_le_scan_disable(req, false);
2792*4882a593Smuzhiyun 	return 0;
2793*4882a593Smuzhiyun }
2794*4882a593Smuzhiyun 
bredr_inquiry(struct hci_request * req,unsigned long opt)2795*4882a593Smuzhiyun static int bredr_inquiry(struct hci_request *req, unsigned long opt)
2796*4882a593Smuzhiyun {
2797*4882a593Smuzhiyun 	u8 length = opt;
2798*4882a593Smuzhiyun 	const u8 giac[3] = { 0x33, 0x8b, 0x9e };
2799*4882a593Smuzhiyun 	const u8 liac[3] = { 0x00, 0x8b, 0x9e };
2800*4882a593Smuzhiyun 	struct hci_cp_inquiry cp;
2801*4882a593Smuzhiyun 
2802*4882a593Smuzhiyun 	BT_DBG("%s", req->hdev->name);
2803*4882a593Smuzhiyun 
2804*4882a593Smuzhiyun 	hci_dev_lock(req->hdev);
2805*4882a593Smuzhiyun 	hci_inquiry_cache_flush(req->hdev);
2806*4882a593Smuzhiyun 	hci_dev_unlock(req->hdev);
2807*4882a593Smuzhiyun 
2808*4882a593Smuzhiyun 	memset(&cp, 0, sizeof(cp));
2809*4882a593Smuzhiyun 
2810*4882a593Smuzhiyun 	if (req->hdev->discovery.limited)
2811*4882a593Smuzhiyun 		memcpy(&cp.lap, liac, sizeof(cp.lap));
2812*4882a593Smuzhiyun 	else
2813*4882a593Smuzhiyun 		memcpy(&cp.lap, giac, sizeof(cp.lap));
2814*4882a593Smuzhiyun 
2815*4882a593Smuzhiyun 	cp.length = length;
2816*4882a593Smuzhiyun 
2817*4882a593Smuzhiyun 	hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
2818*4882a593Smuzhiyun 
2819*4882a593Smuzhiyun 	return 0;
2820*4882a593Smuzhiyun }
2821*4882a593Smuzhiyun 
le_scan_disable_work(struct work_struct * work)2822*4882a593Smuzhiyun static void le_scan_disable_work(struct work_struct *work)
2823*4882a593Smuzhiyun {
2824*4882a593Smuzhiyun 	struct hci_dev *hdev = container_of(work, struct hci_dev,
2825*4882a593Smuzhiyun 					    le_scan_disable.work);
2826*4882a593Smuzhiyun 	u8 status;
2827*4882a593Smuzhiyun 
2828*4882a593Smuzhiyun 	BT_DBG("%s", hdev->name);
2829*4882a593Smuzhiyun 
2830*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2831*4882a593Smuzhiyun 		return;
2832*4882a593Smuzhiyun 
2833*4882a593Smuzhiyun 	cancel_delayed_work(&hdev->le_scan_restart);
2834*4882a593Smuzhiyun 
2835*4882a593Smuzhiyun 	hci_req_sync(hdev, le_scan_disable, 0, HCI_CMD_TIMEOUT, &status);
2836*4882a593Smuzhiyun 	if (status) {
2837*4882a593Smuzhiyun 		bt_dev_err(hdev, "failed to disable LE scan: status 0x%02x",
2838*4882a593Smuzhiyun 			   status);
2839*4882a593Smuzhiyun 		return;
2840*4882a593Smuzhiyun 	}
2841*4882a593Smuzhiyun 
2842*4882a593Smuzhiyun 	hdev->discovery.scan_start = 0;
2843*4882a593Smuzhiyun 
2844*4882a593Smuzhiyun 	/* If we were running LE only scan, change discovery state. If
2845*4882a593Smuzhiyun 	 * we were running both LE and BR/EDR inquiry simultaneously,
2846*4882a593Smuzhiyun 	 * and BR/EDR inquiry is already finished, stop discovery,
2847*4882a593Smuzhiyun 	 * otherwise BR/EDR inquiry will stop discovery when finished.
2848*4882a593Smuzhiyun 	 * If we will resolve remote device name, do not change
2849*4882a593Smuzhiyun 	 * discovery state.
2850*4882a593Smuzhiyun 	 */
2851*4882a593Smuzhiyun 
2852*4882a593Smuzhiyun 	if (hdev->discovery.type == DISCOV_TYPE_LE)
2853*4882a593Smuzhiyun 		goto discov_stopped;
2854*4882a593Smuzhiyun 
2855*4882a593Smuzhiyun 	if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
2856*4882a593Smuzhiyun 		return;
2857*4882a593Smuzhiyun 
2858*4882a593Smuzhiyun 	if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
2859*4882a593Smuzhiyun 		if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
2860*4882a593Smuzhiyun 		    hdev->discovery.state != DISCOVERY_RESOLVING)
2861*4882a593Smuzhiyun 			goto discov_stopped;
2862*4882a593Smuzhiyun 
2863*4882a593Smuzhiyun 		return;
2864*4882a593Smuzhiyun 	}
2865*4882a593Smuzhiyun 
2866*4882a593Smuzhiyun 	hci_req_sync(hdev, bredr_inquiry, DISCOV_INTERLEAVED_INQUIRY_LEN,
2867*4882a593Smuzhiyun 		     HCI_CMD_TIMEOUT, &status);
2868*4882a593Smuzhiyun 	if (status) {
2869*4882a593Smuzhiyun 		bt_dev_err(hdev, "inquiry failed: status 0x%02x", status);
2870*4882a593Smuzhiyun 		goto discov_stopped;
2871*4882a593Smuzhiyun 	}
2872*4882a593Smuzhiyun 
2873*4882a593Smuzhiyun 	return;
2874*4882a593Smuzhiyun 
2875*4882a593Smuzhiyun discov_stopped:
2876*4882a593Smuzhiyun 	hci_dev_lock(hdev);
2877*4882a593Smuzhiyun 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2878*4882a593Smuzhiyun 	hci_dev_unlock(hdev);
2879*4882a593Smuzhiyun }
2880*4882a593Smuzhiyun 
le_scan_restart(struct hci_request * req,unsigned long opt)2881*4882a593Smuzhiyun static int le_scan_restart(struct hci_request *req, unsigned long opt)
2882*4882a593Smuzhiyun {
2883*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2884*4882a593Smuzhiyun 
2885*4882a593Smuzhiyun 	/* If controller is not scanning we are done. */
2886*4882a593Smuzhiyun 	if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2887*4882a593Smuzhiyun 		return 0;
2888*4882a593Smuzhiyun 
2889*4882a593Smuzhiyun 	if (hdev->scanning_paused) {
2890*4882a593Smuzhiyun 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
2891*4882a593Smuzhiyun 		return 0;
2892*4882a593Smuzhiyun 	}
2893*4882a593Smuzhiyun 
2894*4882a593Smuzhiyun 	hci_req_add_le_scan_disable(req, false);
2895*4882a593Smuzhiyun 
2896*4882a593Smuzhiyun 	if (use_ext_scan(hdev)) {
2897*4882a593Smuzhiyun 		struct hci_cp_le_set_ext_scan_enable ext_enable_cp;
2898*4882a593Smuzhiyun 
2899*4882a593Smuzhiyun 		memset(&ext_enable_cp, 0, sizeof(ext_enable_cp));
2900*4882a593Smuzhiyun 		ext_enable_cp.enable = LE_SCAN_ENABLE;
2901*4882a593Smuzhiyun 		ext_enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
2902*4882a593Smuzhiyun 
2903*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
2904*4882a593Smuzhiyun 			    sizeof(ext_enable_cp), &ext_enable_cp);
2905*4882a593Smuzhiyun 	} else {
2906*4882a593Smuzhiyun 		struct hci_cp_le_set_scan_enable cp;
2907*4882a593Smuzhiyun 
2908*4882a593Smuzhiyun 		memset(&cp, 0, sizeof(cp));
2909*4882a593Smuzhiyun 		cp.enable = LE_SCAN_ENABLE;
2910*4882a593Smuzhiyun 		cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
2911*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
2912*4882a593Smuzhiyun 	}
2913*4882a593Smuzhiyun 
2914*4882a593Smuzhiyun 	return 0;
2915*4882a593Smuzhiyun }
2916*4882a593Smuzhiyun 
le_scan_restart_work(struct work_struct * work)2917*4882a593Smuzhiyun static void le_scan_restart_work(struct work_struct *work)
2918*4882a593Smuzhiyun {
2919*4882a593Smuzhiyun 	struct hci_dev *hdev = container_of(work, struct hci_dev,
2920*4882a593Smuzhiyun 					    le_scan_restart.work);
2921*4882a593Smuzhiyun 	unsigned long timeout, duration, scan_start, now;
2922*4882a593Smuzhiyun 	u8 status;
2923*4882a593Smuzhiyun 
2924*4882a593Smuzhiyun 	BT_DBG("%s", hdev->name);
2925*4882a593Smuzhiyun 
2926*4882a593Smuzhiyun 	hci_req_sync(hdev, le_scan_restart, 0, HCI_CMD_TIMEOUT, &status);
2927*4882a593Smuzhiyun 	if (status) {
2928*4882a593Smuzhiyun 		bt_dev_err(hdev, "failed to restart LE scan: status %d",
2929*4882a593Smuzhiyun 			   status);
2930*4882a593Smuzhiyun 		return;
2931*4882a593Smuzhiyun 	}
2932*4882a593Smuzhiyun 
2933*4882a593Smuzhiyun 	hci_dev_lock(hdev);
2934*4882a593Smuzhiyun 
2935*4882a593Smuzhiyun 	if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
2936*4882a593Smuzhiyun 	    !hdev->discovery.scan_start)
2937*4882a593Smuzhiyun 		goto unlock;
2938*4882a593Smuzhiyun 
2939*4882a593Smuzhiyun 	/* When the scan was started, hdev->le_scan_disable has been queued
2940*4882a593Smuzhiyun 	 * after duration from scan_start. During scan restart this job
2941*4882a593Smuzhiyun 	 * has been canceled, and we need to queue it again after proper
2942*4882a593Smuzhiyun 	 * timeout, to make sure that scan does not run indefinitely.
2943*4882a593Smuzhiyun 	 */
2944*4882a593Smuzhiyun 	duration = hdev->discovery.scan_duration;
2945*4882a593Smuzhiyun 	scan_start = hdev->discovery.scan_start;
2946*4882a593Smuzhiyun 	now = jiffies;
2947*4882a593Smuzhiyun 	if (now - scan_start <= duration) {
2948*4882a593Smuzhiyun 		int elapsed;
2949*4882a593Smuzhiyun 
2950*4882a593Smuzhiyun 		if (now >= scan_start)
2951*4882a593Smuzhiyun 			elapsed = now - scan_start;
2952*4882a593Smuzhiyun 		else
2953*4882a593Smuzhiyun 			elapsed = ULONG_MAX - scan_start + now;
2954*4882a593Smuzhiyun 
2955*4882a593Smuzhiyun 		timeout = duration - elapsed;
2956*4882a593Smuzhiyun 	} else {
2957*4882a593Smuzhiyun 		timeout = 0;
2958*4882a593Smuzhiyun 	}
2959*4882a593Smuzhiyun 
2960*4882a593Smuzhiyun 	queue_delayed_work(hdev->req_workqueue,
2961*4882a593Smuzhiyun 			   &hdev->le_scan_disable, timeout);
2962*4882a593Smuzhiyun 
2963*4882a593Smuzhiyun unlock:
2964*4882a593Smuzhiyun 	hci_dev_unlock(hdev);
2965*4882a593Smuzhiyun }
2966*4882a593Smuzhiyun 
active_scan(struct hci_request * req,unsigned long opt)2967*4882a593Smuzhiyun static int active_scan(struct hci_request *req, unsigned long opt)
2968*4882a593Smuzhiyun {
2969*4882a593Smuzhiyun 	uint16_t interval = opt;
2970*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
2971*4882a593Smuzhiyun 	u8 own_addr_type;
2972*4882a593Smuzhiyun 	/* White list is not used for discovery */
2973*4882a593Smuzhiyun 	u8 filter_policy = 0x00;
2974*4882a593Smuzhiyun 	/* Discovery doesn't require controller address resolution */
2975*4882a593Smuzhiyun 	bool addr_resolv = false;
2976*4882a593Smuzhiyun 	int err;
2977*4882a593Smuzhiyun 
2978*4882a593Smuzhiyun 	BT_DBG("%s", hdev->name);
2979*4882a593Smuzhiyun 
2980*4882a593Smuzhiyun 	/* If controller is scanning, it means the background scanning is
2981*4882a593Smuzhiyun 	 * running. Thus, we should temporarily stop it in order to set the
2982*4882a593Smuzhiyun 	 * discovery scanning parameters.
2983*4882a593Smuzhiyun 	 */
2984*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
2985*4882a593Smuzhiyun 		hci_req_add_le_scan_disable(req, false);
2986*4882a593Smuzhiyun 
2987*4882a593Smuzhiyun 	/* All active scans will be done with either a resolvable private
2988*4882a593Smuzhiyun 	 * address (when privacy feature has been enabled) or non-resolvable
2989*4882a593Smuzhiyun 	 * private address.
2990*4882a593Smuzhiyun 	 */
2991*4882a593Smuzhiyun 	err = hci_update_random_address(req, true, scan_use_rpa(hdev),
2992*4882a593Smuzhiyun 					&own_addr_type);
2993*4882a593Smuzhiyun 	if (err < 0)
2994*4882a593Smuzhiyun 		own_addr_type = ADDR_LE_DEV_PUBLIC;
2995*4882a593Smuzhiyun 
2996*4882a593Smuzhiyun 	hci_req_start_scan(req, LE_SCAN_ACTIVE, interval,
2997*4882a593Smuzhiyun 			   hdev->le_scan_window_discovery, own_addr_type,
2998*4882a593Smuzhiyun 			   filter_policy, addr_resolv);
2999*4882a593Smuzhiyun 	return 0;
3000*4882a593Smuzhiyun }
3001*4882a593Smuzhiyun 
interleaved_discov(struct hci_request * req,unsigned long opt)3002*4882a593Smuzhiyun static int interleaved_discov(struct hci_request *req, unsigned long opt)
3003*4882a593Smuzhiyun {
3004*4882a593Smuzhiyun 	int err;
3005*4882a593Smuzhiyun 
3006*4882a593Smuzhiyun 	BT_DBG("%s", req->hdev->name);
3007*4882a593Smuzhiyun 
3008*4882a593Smuzhiyun 	err = active_scan(req, opt);
3009*4882a593Smuzhiyun 	if (err)
3010*4882a593Smuzhiyun 		return err;
3011*4882a593Smuzhiyun 
3012*4882a593Smuzhiyun 	return bredr_inquiry(req, DISCOV_BREDR_INQUIRY_LEN);
3013*4882a593Smuzhiyun }
3014*4882a593Smuzhiyun 
start_discovery(struct hci_dev * hdev,u8 * status)3015*4882a593Smuzhiyun static void start_discovery(struct hci_dev *hdev, u8 *status)
3016*4882a593Smuzhiyun {
3017*4882a593Smuzhiyun 	unsigned long timeout;
3018*4882a593Smuzhiyun 
3019*4882a593Smuzhiyun 	BT_DBG("%s type %u", hdev->name, hdev->discovery.type);
3020*4882a593Smuzhiyun 
3021*4882a593Smuzhiyun 	switch (hdev->discovery.type) {
3022*4882a593Smuzhiyun 	case DISCOV_TYPE_BREDR:
3023*4882a593Smuzhiyun 		if (!hci_dev_test_flag(hdev, HCI_INQUIRY))
3024*4882a593Smuzhiyun 			hci_req_sync(hdev, bredr_inquiry,
3025*4882a593Smuzhiyun 				     DISCOV_BREDR_INQUIRY_LEN, HCI_CMD_TIMEOUT,
3026*4882a593Smuzhiyun 				     status);
3027*4882a593Smuzhiyun 		return;
3028*4882a593Smuzhiyun 	case DISCOV_TYPE_INTERLEAVED:
3029*4882a593Smuzhiyun 		/* When running simultaneous discovery, the LE scanning time
3030*4882a593Smuzhiyun 		 * should occupy the whole discovery time sine BR/EDR inquiry
3031*4882a593Smuzhiyun 		 * and LE scanning are scheduled by the controller.
3032*4882a593Smuzhiyun 		 *
3033*4882a593Smuzhiyun 		 * For interleaving discovery in comparison, BR/EDR inquiry
3034*4882a593Smuzhiyun 		 * and LE scanning are done sequentially with separate
3035*4882a593Smuzhiyun 		 * timeouts.
3036*4882a593Smuzhiyun 		 */
3037*4882a593Smuzhiyun 		if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
3038*4882a593Smuzhiyun 			     &hdev->quirks)) {
3039*4882a593Smuzhiyun 			timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
3040*4882a593Smuzhiyun 			/* During simultaneous discovery, we double LE scan
3041*4882a593Smuzhiyun 			 * interval. We must leave some time for the controller
3042*4882a593Smuzhiyun 			 * to do BR/EDR inquiry.
3043*4882a593Smuzhiyun 			 */
3044*4882a593Smuzhiyun 			hci_req_sync(hdev, interleaved_discov,
3045*4882a593Smuzhiyun 				     hdev->le_scan_int_discovery * 2, HCI_CMD_TIMEOUT,
3046*4882a593Smuzhiyun 				     status);
3047*4882a593Smuzhiyun 			break;
3048*4882a593Smuzhiyun 		}
3049*4882a593Smuzhiyun 
3050*4882a593Smuzhiyun 		timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
3051*4882a593Smuzhiyun 		hci_req_sync(hdev, active_scan, hdev->le_scan_int_discovery,
3052*4882a593Smuzhiyun 			     HCI_CMD_TIMEOUT, status);
3053*4882a593Smuzhiyun 		break;
3054*4882a593Smuzhiyun 	case DISCOV_TYPE_LE:
3055*4882a593Smuzhiyun 		timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
3056*4882a593Smuzhiyun 		hci_req_sync(hdev, active_scan, hdev->le_scan_int_discovery,
3057*4882a593Smuzhiyun 			     HCI_CMD_TIMEOUT, status);
3058*4882a593Smuzhiyun 		break;
3059*4882a593Smuzhiyun 	default:
3060*4882a593Smuzhiyun 		*status = HCI_ERROR_UNSPECIFIED;
3061*4882a593Smuzhiyun 		return;
3062*4882a593Smuzhiyun 	}
3063*4882a593Smuzhiyun 
3064*4882a593Smuzhiyun 	if (*status)
3065*4882a593Smuzhiyun 		return;
3066*4882a593Smuzhiyun 
3067*4882a593Smuzhiyun 	BT_DBG("%s timeout %u ms", hdev->name, jiffies_to_msecs(timeout));
3068*4882a593Smuzhiyun 
3069*4882a593Smuzhiyun 	/* When service discovery is used and the controller has a
3070*4882a593Smuzhiyun 	 * strict duplicate filter, it is important to remember the
3071*4882a593Smuzhiyun 	 * start and duration of the scan. This is required for
3072*4882a593Smuzhiyun 	 * restarting scanning during the discovery phase.
3073*4882a593Smuzhiyun 	 */
3074*4882a593Smuzhiyun 	if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
3075*4882a593Smuzhiyun 		     hdev->discovery.result_filtering) {
3076*4882a593Smuzhiyun 		hdev->discovery.scan_start = jiffies;
3077*4882a593Smuzhiyun 		hdev->discovery.scan_duration = timeout;
3078*4882a593Smuzhiyun 	}
3079*4882a593Smuzhiyun 
3080*4882a593Smuzhiyun 	queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
3081*4882a593Smuzhiyun 			   timeout);
3082*4882a593Smuzhiyun }
3083*4882a593Smuzhiyun 
hci_req_stop_discovery(struct hci_request * req)3084*4882a593Smuzhiyun bool hci_req_stop_discovery(struct hci_request *req)
3085*4882a593Smuzhiyun {
3086*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
3087*4882a593Smuzhiyun 	struct discovery_state *d = &hdev->discovery;
3088*4882a593Smuzhiyun 	struct hci_cp_remote_name_req_cancel cp;
3089*4882a593Smuzhiyun 	struct inquiry_entry *e;
3090*4882a593Smuzhiyun 	bool ret = false;
3091*4882a593Smuzhiyun 
3092*4882a593Smuzhiyun 	BT_DBG("%s state %u", hdev->name, hdev->discovery.state);
3093*4882a593Smuzhiyun 
3094*4882a593Smuzhiyun 	if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
3095*4882a593Smuzhiyun 		if (test_bit(HCI_INQUIRY, &hdev->flags))
3096*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
3097*4882a593Smuzhiyun 
3098*4882a593Smuzhiyun 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
3099*4882a593Smuzhiyun 			cancel_delayed_work(&hdev->le_scan_disable);
3100*4882a593Smuzhiyun 			hci_req_add_le_scan_disable(req, false);
3101*4882a593Smuzhiyun 		}
3102*4882a593Smuzhiyun 
3103*4882a593Smuzhiyun 		ret = true;
3104*4882a593Smuzhiyun 	} else {
3105*4882a593Smuzhiyun 		/* Passive scanning */
3106*4882a593Smuzhiyun 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
3107*4882a593Smuzhiyun 			hci_req_add_le_scan_disable(req, false);
3108*4882a593Smuzhiyun 			ret = true;
3109*4882a593Smuzhiyun 		}
3110*4882a593Smuzhiyun 	}
3111*4882a593Smuzhiyun 
3112*4882a593Smuzhiyun 	/* No further actions needed for LE-only discovery */
3113*4882a593Smuzhiyun 	if (d->type == DISCOV_TYPE_LE)
3114*4882a593Smuzhiyun 		return ret;
3115*4882a593Smuzhiyun 
3116*4882a593Smuzhiyun 	if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
3117*4882a593Smuzhiyun 		e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
3118*4882a593Smuzhiyun 						     NAME_PENDING);
3119*4882a593Smuzhiyun 		if (!e)
3120*4882a593Smuzhiyun 			return ret;
3121*4882a593Smuzhiyun 
3122*4882a593Smuzhiyun 		bacpy(&cp.bdaddr, &e->data.bdaddr);
3123*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
3124*4882a593Smuzhiyun 			    &cp);
3125*4882a593Smuzhiyun 		ret = true;
3126*4882a593Smuzhiyun 	}
3127*4882a593Smuzhiyun 
3128*4882a593Smuzhiyun 	return ret;
3129*4882a593Smuzhiyun }
3130*4882a593Smuzhiyun 
stop_discovery(struct hci_request * req,unsigned long opt)3131*4882a593Smuzhiyun static int stop_discovery(struct hci_request *req, unsigned long opt)
3132*4882a593Smuzhiyun {
3133*4882a593Smuzhiyun 	hci_dev_lock(req->hdev);
3134*4882a593Smuzhiyun 	hci_req_stop_discovery(req);
3135*4882a593Smuzhiyun 	hci_dev_unlock(req->hdev);
3136*4882a593Smuzhiyun 
3137*4882a593Smuzhiyun 	return 0;
3138*4882a593Smuzhiyun }
3139*4882a593Smuzhiyun 
discov_update(struct work_struct * work)3140*4882a593Smuzhiyun static void discov_update(struct work_struct *work)
3141*4882a593Smuzhiyun {
3142*4882a593Smuzhiyun 	struct hci_dev *hdev = container_of(work, struct hci_dev,
3143*4882a593Smuzhiyun 					    discov_update);
3144*4882a593Smuzhiyun 	u8 status = 0;
3145*4882a593Smuzhiyun 
3146*4882a593Smuzhiyun 	switch (hdev->discovery.state) {
3147*4882a593Smuzhiyun 	case DISCOVERY_STARTING:
3148*4882a593Smuzhiyun 		start_discovery(hdev, &status);
3149*4882a593Smuzhiyun 		mgmt_start_discovery_complete(hdev, status);
3150*4882a593Smuzhiyun 		if (status)
3151*4882a593Smuzhiyun 			hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3152*4882a593Smuzhiyun 		else
3153*4882a593Smuzhiyun 			hci_discovery_set_state(hdev, DISCOVERY_FINDING);
3154*4882a593Smuzhiyun 		break;
3155*4882a593Smuzhiyun 	case DISCOVERY_STOPPING:
3156*4882a593Smuzhiyun 		hci_req_sync(hdev, stop_discovery, 0, HCI_CMD_TIMEOUT, &status);
3157*4882a593Smuzhiyun 		mgmt_stop_discovery_complete(hdev, status);
3158*4882a593Smuzhiyun 		if (!status)
3159*4882a593Smuzhiyun 			hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3160*4882a593Smuzhiyun 		break;
3161*4882a593Smuzhiyun 	case DISCOVERY_STOPPED:
3162*4882a593Smuzhiyun 	default:
3163*4882a593Smuzhiyun 		return;
3164*4882a593Smuzhiyun 	}
3165*4882a593Smuzhiyun }
3166*4882a593Smuzhiyun 
discov_off(struct work_struct * work)3167*4882a593Smuzhiyun static void discov_off(struct work_struct *work)
3168*4882a593Smuzhiyun {
3169*4882a593Smuzhiyun 	struct hci_dev *hdev = container_of(work, struct hci_dev,
3170*4882a593Smuzhiyun 					    discov_off.work);
3171*4882a593Smuzhiyun 
3172*4882a593Smuzhiyun 	BT_DBG("%s", hdev->name);
3173*4882a593Smuzhiyun 
3174*4882a593Smuzhiyun 	hci_dev_lock(hdev);
3175*4882a593Smuzhiyun 
3176*4882a593Smuzhiyun 	/* When discoverable timeout triggers, then just make sure
3177*4882a593Smuzhiyun 	 * the limited discoverable flag is cleared. Even in the case
3178*4882a593Smuzhiyun 	 * of a timeout triggered from general discoverable, it is
3179*4882a593Smuzhiyun 	 * safe to unconditionally clear the flag.
3180*4882a593Smuzhiyun 	 */
3181*4882a593Smuzhiyun 	hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
3182*4882a593Smuzhiyun 	hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
3183*4882a593Smuzhiyun 	hdev->discov_timeout = 0;
3184*4882a593Smuzhiyun 
3185*4882a593Smuzhiyun 	hci_dev_unlock(hdev);
3186*4882a593Smuzhiyun 
3187*4882a593Smuzhiyun 	hci_req_sync(hdev, discoverable_update, 0, HCI_CMD_TIMEOUT, NULL);
3188*4882a593Smuzhiyun 	mgmt_new_settings(hdev);
3189*4882a593Smuzhiyun }
3190*4882a593Smuzhiyun 
powered_update_hci(struct hci_request * req,unsigned long opt)3191*4882a593Smuzhiyun static int powered_update_hci(struct hci_request *req, unsigned long opt)
3192*4882a593Smuzhiyun {
3193*4882a593Smuzhiyun 	struct hci_dev *hdev = req->hdev;
3194*4882a593Smuzhiyun 	u8 link_sec;
3195*4882a593Smuzhiyun 
3196*4882a593Smuzhiyun 	hci_dev_lock(hdev);
3197*4882a593Smuzhiyun 
3198*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
3199*4882a593Smuzhiyun 	    !lmp_host_ssp_capable(hdev)) {
3200*4882a593Smuzhiyun 		u8 mode = 0x01;
3201*4882a593Smuzhiyun 
3202*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
3203*4882a593Smuzhiyun 
3204*4882a593Smuzhiyun 		if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
3205*4882a593Smuzhiyun 			u8 support = 0x01;
3206*4882a593Smuzhiyun 
3207*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT,
3208*4882a593Smuzhiyun 				    sizeof(support), &support);
3209*4882a593Smuzhiyun 		}
3210*4882a593Smuzhiyun 	}
3211*4882a593Smuzhiyun 
3212*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
3213*4882a593Smuzhiyun 	    lmp_bredr_capable(hdev)) {
3214*4882a593Smuzhiyun 		struct hci_cp_write_le_host_supported cp;
3215*4882a593Smuzhiyun 
3216*4882a593Smuzhiyun 		cp.le = 0x01;
3217*4882a593Smuzhiyun 		cp.simul = 0x00;
3218*4882a593Smuzhiyun 
3219*4882a593Smuzhiyun 		/* Check first if we already have the right
3220*4882a593Smuzhiyun 		 * host state (host features set)
3221*4882a593Smuzhiyun 		 */
3222*4882a593Smuzhiyun 		if (cp.le != lmp_host_le_capable(hdev) ||
3223*4882a593Smuzhiyun 		    cp.simul != lmp_host_le_br_capable(hdev))
3224*4882a593Smuzhiyun 			hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3225*4882a593Smuzhiyun 				    sizeof(cp), &cp);
3226*4882a593Smuzhiyun 	}
3227*4882a593Smuzhiyun 
3228*4882a593Smuzhiyun 	if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
3229*4882a593Smuzhiyun 		/* Make sure the controller has a good default for
3230*4882a593Smuzhiyun 		 * advertising data. This also applies to the case
3231*4882a593Smuzhiyun 		 * where BR/EDR was toggled during the AUTO_OFF phase.
3232*4882a593Smuzhiyun 		 */
3233*4882a593Smuzhiyun 		if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3234*4882a593Smuzhiyun 		    list_empty(&hdev->adv_instances)) {
3235*4882a593Smuzhiyun 			int err;
3236*4882a593Smuzhiyun 
3237*4882a593Smuzhiyun 			if (ext_adv_capable(hdev)) {
3238*4882a593Smuzhiyun 				err = __hci_req_setup_ext_adv_instance(req,
3239*4882a593Smuzhiyun 								       0x00);
3240*4882a593Smuzhiyun 				if (!err)
3241*4882a593Smuzhiyun 					__hci_req_update_scan_rsp_data(req,
3242*4882a593Smuzhiyun 								       0x00);
3243*4882a593Smuzhiyun 			} else {
3244*4882a593Smuzhiyun 				err = 0;
3245*4882a593Smuzhiyun 				__hci_req_update_adv_data(req, 0x00);
3246*4882a593Smuzhiyun 				__hci_req_update_scan_rsp_data(req, 0x00);
3247*4882a593Smuzhiyun 			}
3248*4882a593Smuzhiyun 
3249*4882a593Smuzhiyun 			if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
3250*4882a593Smuzhiyun 				if (!ext_adv_capable(hdev))
3251*4882a593Smuzhiyun 					__hci_req_enable_advertising(req);
3252*4882a593Smuzhiyun 				else if (!err)
3253*4882a593Smuzhiyun 					__hci_req_enable_ext_advertising(req,
3254*4882a593Smuzhiyun 									 0x00);
3255*4882a593Smuzhiyun 			}
3256*4882a593Smuzhiyun 		} else if (!list_empty(&hdev->adv_instances)) {
3257*4882a593Smuzhiyun 			struct adv_info *adv_instance;
3258*4882a593Smuzhiyun 
3259*4882a593Smuzhiyun 			adv_instance = list_first_entry(&hdev->adv_instances,
3260*4882a593Smuzhiyun 							struct adv_info, list);
3261*4882a593Smuzhiyun 			__hci_req_schedule_adv_instance(req,
3262*4882a593Smuzhiyun 							adv_instance->instance,
3263*4882a593Smuzhiyun 							true);
3264*4882a593Smuzhiyun 		}
3265*4882a593Smuzhiyun 	}
3266*4882a593Smuzhiyun 
3267*4882a593Smuzhiyun 	link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3268*4882a593Smuzhiyun 	if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
3269*4882a593Smuzhiyun 		hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE,
3270*4882a593Smuzhiyun 			    sizeof(link_sec), &link_sec);
3271*4882a593Smuzhiyun 
3272*4882a593Smuzhiyun 	if (lmp_bredr_capable(hdev)) {
3273*4882a593Smuzhiyun 		if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3274*4882a593Smuzhiyun 			__hci_req_write_fast_connectable(req, true);
3275*4882a593Smuzhiyun 		else
3276*4882a593Smuzhiyun 			__hci_req_write_fast_connectable(req, false);
3277*4882a593Smuzhiyun 		__hci_req_update_scan(req);
3278*4882a593Smuzhiyun 		__hci_req_update_class(req);
3279*4882a593Smuzhiyun 		__hci_req_update_name(req);
3280*4882a593Smuzhiyun 		__hci_req_update_eir(req);
3281*4882a593Smuzhiyun 	}
3282*4882a593Smuzhiyun 
3283*4882a593Smuzhiyun 	hci_dev_unlock(hdev);
3284*4882a593Smuzhiyun 	return 0;
3285*4882a593Smuzhiyun }
3286*4882a593Smuzhiyun 
__hci_req_hci_power_on(struct hci_dev * hdev)3287*4882a593Smuzhiyun int __hci_req_hci_power_on(struct hci_dev *hdev)
3288*4882a593Smuzhiyun {
3289*4882a593Smuzhiyun 	/* Register the available SMP channels (BR/EDR and LE) only when
3290*4882a593Smuzhiyun 	 * successfully powering on the controller. This late
3291*4882a593Smuzhiyun 	 * registration is required so that LE SMP can clearly decide if
3292*4882a593Smuzhiyun 	 * the public address or static address is used.
3293*4882a593Smuzhiyun 	 */
3294*4882a593Smuzhiyun 	smp_register(hdev);
3295*4882a593Smuzhiyun 
3296*4882a593Smuzhiyun 	return __hci_req_sync(hdev, powered_update_hci, 0, HCI_CMD_TIMEOUT,
3297*4882a593Smuzhiyun 			      NULL);
3298*4882a593Smuzhiyun }
3299*4882a593Smuzhiyun 
hci_request_setup(struct hci_dev * hdev)3300*4882a593Smuzhiyun void hci_request_setup(struct hci_dev *hdev)
3301*4882a593Smuzhiyun {
3302*4882a593Smuzhiyun 	INIT_WORK(&hdev->discov_update, discov_update);
3303*4882a593Smuzhiyun 	INIT_WORK(&hdev->bg_scan_update, bg_scan_update);
3304*4882a593Smuzhiyun 	INIT_WORK(&hdev->scan_update, scan_update_work);
3305*4882a593Smuzhiyun 	INIT_WORK(&hdev->connectable_update, connectable_update_work);
3306*4882a593Smuzhiyun 	INIT_WORK(&hdev->discoverable_update, discoverable_update_work);
3307*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&hdev->discov_off, discov_off);
3308*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
3309*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart_work);
3310*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
3311*4882a593Smuzhiyun }
3312*4882a593Smuzhiyun 
hci_request_cancel_all(struct hci_dev * hdev)3313*4882a593Smuzhiyun void hci_request_cancel_all(struct hci_dev *hdev)
3314*4882a593Smuzhiyun {
3315*4882a593Smuzhiyun 	hci_req_sync_cancel(hdev, ENODEV);
3316*4882a593Smuzhiyun 
3317*4882a593Smuzhiyun 	cancel_work_sync(&hdev->discov_update);
3318*4882a593Smuzhiyun 	cancel_work_sync(&hdev->bg_scan_update);
3319*4882a593Smuzhiyun 	cancel_work_sync(&hdev->scan_update);
3320*4882a593Smuzhiyun 	cancel_work_sync(&hdev->connectable_update);
3321*4882a593Smuzhiyun 	cancel_work_sync(&hdev->discoverable_update);
3322*4882a593Smuzhiyun 	cancel_delayed_work_sync(&hdev->discov_off);
3323*4882a593Smuzhiyun 	cancel_delayed_work_sync(&hdev->le_scan_disable);
3324*4882a593Smuzhiyun 	cancel_delayed_work_sync(&hdev->le_scan_restart);
3325*4882a593Smuzhiyun 
3326*4882a593Smuzhiyun 	if (hdev->adv_instance_timeout) {
3327*4882a593Smuzhiyun 		cancel_delayed_work_sync(&hdev->adv_instance_expire);
3328*4882a593Smuzhiyun 		hdev->adv_instance_timeout = 0;
3329*4882a593Smuzhiyun 	}
3330*4882a593Smuzhiyun }
3331