• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * BlueZ - Bluetooth protocol stack for Linux
4  *
5  * Copyright (C) 2021 Intel Corporation
6  */
7 
8 #include <linux/property.h>
9 
10 #include <net/bluetooth/bluetooth.h>
11 #include <net/bluetooth/hci_core.h>
12 #include <net/bluetooth/mgmt.h>
13 
14 #include "hci_request.h"
15 #include "hci_codec.h"
16 #include "hci_debugfs.h"
17 #include "smp.h"
18 #include "eir.h"
19 #include "msft.h"
20 #include "aosp.h"
21 #include "leds.h"
22 
hci_cmd_sync_complete(struct hci_dev * hdev,u8 result,u16 opcode,struct sk_buff * skb)23 static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
24 				  struct sk_buff *skb)
25 {
26 	bt_dev_dbg(hdev, "result 0x%2.2x", result);
27 
28 	if (hdev->req_status != HCI_REQ_PEND)
29 		return;
30 
31 	hdev->req_result = result;
32 	hdev->req_status = HCI_REQ_DONE;
33 
34 	if (skb) {
35 		struct sock *sk = hci_skb_sk(skb);
36 
37 		/* Drop sk reference if set */
38 		if (sk)
39 			sock_put(sk);
40 
41 		hdev->req_skb = skb_get(skb);
42 	}
43 
44 	wake_up_interruptible(&hdev->req_wait_q);
45 }
46 
hci_cmd_sync_alloc(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,struct sock * sk)47 static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
48 					  u32 plen, const void *param,
49 					  struct sock *sk)
50 {
51 	int len = HCI_COMMAND_HDR_SIZE + plen;
52 	struct hci_command_hdr *hdr;
53 	struct sk_buff *skb;
54 
55 	skb = bt_skb_alloc(len, GFP_ATOMIC);
56 	if (!skb)
57 		return NULL;
58 
59 	hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
60 	hdr->opcode = cpu_to_le16(opcode);
61 	hdr->plen   = plen;
62 
63 	if (plen)
64 		skb_put_data(skb, param, plen);
65 
66 	bt_dev_dbg(hdev, "skb len %d", skb->len);
67 
68 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
69 	hci_skb_opcode(skb) = opcode;
70 
71 	/* Grab a reference if command needs to be associated with a sock (e.g.
72 	 * likely mgmt socket that initiated the command).
73 	 */
74 	if (sk) {
75 		hci_skb_sk(skb) = sk;
76 		sock_hold(sk);
77 	}
78 
79 	return skb;
80 }
81 
hci_cmd_sync_add(struct hci_request * req,u16 opcode,u32 plen,const void * param,u8 event,struct sock * sk)82 static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
83 			     const void *param, u8 event, struct sock *sk)
84 {
85 	struct hci_dev *hdev = req->hdev;
86 	struct sk_buff *skb;
87 
88 	bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
89 
90 	/* If an error occurred during request building, there is no point in
91 	 * queueing the HCI command. We can simply return.
92 	 */
93 	if (req->err)
94 		return;
95 
96 	skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
97 	if (!skb) {
98 		bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
99 			   opcode);
100 		req->err = -ENOMEM;
101 		return;
102 	}
103 
104 	if (skb_queue_empty(&req->cmd_q))
105 		bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
106 
107 	hci_skb_event(skb) = event;
108 
109 	skb_queue_tail(&req->cmd_q, skb);
110 }
111 
hci_cmd_sync_run(struct hci_request * req)112 static int hci_cmd_sync_run(struct hci_request *req)
113 {
114 	struct hci_dev *hdev = req->hdev;
115 	struct sk_buff *skb;
116 	unsigned long flags;
117 
118 	bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
119 
120 	/* If an error occurred during request building, remove all HCI
121 	 * commands queued on the HCI request queue.
122 	 */
123 	if (req->err) {
124 		skb_queue_purge(&req->cmd_q);
125 		return req->err;
126 	}
127 
128 	/* Do not allow empty requests */
129 	if (skb_queue_empty(&req->cmd_q))
130 		return -ENODATA;
131 
132 	skb = skb_peek_tail(&req->cmd_q);
133 	bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
134 	bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
135 
136 	spin_lock_irqsave(&hdev->cmd_q.lock, flags);
137 	skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
138 	spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
139 
140 	queue_work(hdev->workqueue, &hdev->cmd_work);
141 
142 	return 0;
143 }
144 
145 /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync_sk(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u8 event,u32 timeout,struct sock * sk)146 struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
147 				  const void *param, u8 event, u32 timeout,
148 				  struct sock *sk)
149 {
150 	struct hci_request req;
151 	struct sk_buff *skb;
152 	int err = 0;
153 
154 	bt_dev_dbg(hdev, "Opcode 0x%4.4x", opcode);
155 
156 	hci_req_init(&req, hdev);
157 
158 	hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
159 
160 	hdev->req_status = HCI_REQ_PEND;
161 
162 	err = hci_cmd_sync_run(&req);
163 	if (err < 0)
164 		return ERR_PTR(err);
165 
166 	err = wait_event_interruptible_timeout(hdev->req_wait_q,
167 					       hdev->req_status != HCI_REQ_PEND,
168 					       timeout);
169 
170 	if (err == -ERESTARTSYS)
171 		return ERR_PTR(-EINTR);
172 
173 	switch (hdev->req_status) {
174 	case HCI_REQ_DONE:
175 		err = -bt_to_errno(hdev->req_result);
176 		break;
177 
178 	case HCI_REQ_CANCELED:
179 		err = -hdev->req_result;
180 		break;
181 
182 	default:
183 		err = -ETIMEDOUT;
184 		break;
185 	}
186 
187 	hdev->req_status = 0;
188 	hdev->req_result = 0;
189 	skb = hdev->req_skb;
190 	hdev->req_skb = NULL;
191 
192 	bt_dev_dbg(hdev, "end: err %d", err);
193 
194 	if (err < 0) {
195 		kfree_skb(skb);
196 		return ERR_PTR(err);
197 	}
198 
199 	return skb;
200 }
201 EXPORT_SYMBOL(__hci_cmd_sync_sk);
202 
203 /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)204 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
205 			       const void *param, u32 timeout)
206 {
207 	return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
208 }
209 EXPORT_SYMBOL(__hci_cmd_sync);
210 
211 /* Send HCI command and wait for command complete event */
hci_cmd_sync(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)212 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
213 			     const void *param, u32 timeout)
214 {
215 	struct sk_buff *skb;
216 
217 	if (!test_bit(HCI_UP, &hdev->flags))
218 		return ERR_PTR(-ENETDOWN);
219 
220 	bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
221 
222 	hci_req_sync_lock(hdev);
223 	skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
224 	hci_req_sync_unlock(hdev);
225 
226 	return skb;
227 }
228 EXPORT_SYMBOL(hci_cmd_sync);
229 
230 /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync_ev(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u8 event,u32 timeout)231 struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
232 				  const void *param, u8 event, u32 timeout)
233 {
234 	return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
235 				 NULL);
236 }
237 EXPORT_SYMBOL(__hci_cmd_sync_ev);
238 
239 /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync_status_sk(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u8 event,u32 timeout,struct sock * sk)240 int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
241 			     const void *param, u8 event, u32 timeout,
242 			     struct sock *sk)
243 {
244 	struct sk_buff *skb;
245 	u8 status;
246 
247 	skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
248 	if (IS_ERR(skb)) {
249 		if (!event)
250 			bt_dev_err(hdev, "Opcode 0x%4.4x failed: %ld", opcode,
251 				   PTR_ERR(skb));
252 		return PTR_ERR(skb);
253 	}
254 
255 	/* If command return a status event skb will be set to NULL as there are
256 	 * no parameters, in case of failure IS_ERR(skb) would have be set to
257 	 * the actual error would be found with PTR_ERR(skb).
258 	 */
259 	if (!skb)
260 		return 0;
261 
262 	status = skb->data[0];
263 
264 	kfree_skb(skb);
265 
266 	return status;
267 }
268 EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
269 
__hci_cmd_sync_status(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)270 int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
271 			  const void *param, u32 timeout)
272 {
273 	return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
274 					NULL);
275 }
276 EXPORT_SYMBOL(__hci_cmd_sync_status);
277 
hci_cmd_sync_work(struct work_struct * work)278 static void hci_cmd_sync_work(struct work_struct *work)
279 {
280 	struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
281 
282 	bt_dev_dbg(hdev, "");
283 
284 	/* Dequeue all entries and run them */
285 	while (1) {
286 		struct hci_cmd_sync_work_entry *entry;
287 
288 		mutex_lock(&hdev->cmd_sync_work_lock);
289 		entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
290 						 struct hci_cmd_sync_work_entry,
291 						 list);
292 		if (entry)
293 			list_del(&entry->list);
294 		mutex_unlock(&hdev->cmd_sync_work_lock);
295 
296 		if (!entry)
297 			break;
298 
299 		bt_dev_dbg(hdev, "entry %p", entry);
300 
301 		if (entry->func) {
302 			int err;
303 
304 			hci_req_sync_lock(hdev);
305 			err = entry->func(hdev, entry->data);
306 			if (entry->destroy)
307 				entry->destroy(hdev, entry->data, err);
308 			hci_req_sync_unlock(hdev);
309 		}
310 
311 		kfree(entry);
312 	}
313 }
314 
hci_cmd_sync_cancel_work(struct work_struct * work)315 static void hci_cmd_sync_cancel_work(struct work_struct *work)
316 {
317 	struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
318 
319 	cancel_delayed_work_sync(&hdev->cmd_timer);
320 	cancel_delayed_work_sync(&hdev->ncmd_timer);
321 	atomic_set(&hdev->cmd_cnt, 1);
322 
323 	wake_up_interruptible(&hdev->req_wait_q);
324 }
325 
326 static int hci_scan_disable_sync(struct hci_dev *hdev);
scan_disable_sync(struct hci_dev * hdev,void * data)327 static int scan_disable_sync(struct hci_dev *hdev, void *data)
328 {
329 	return hci_scan_disable_sync(hdev);
330 }
331 
332 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length);
interleaved_inquiry_sync(struct hci_dev * hdev,void * data)333 static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data)
334 {
335 	return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN);
336 }
337 
le_scan_disable(struct work_struct * work)338 static void le_scan_disable(struct work_struct *work)
339 {
340 	struct hci_dev *hdev = container_of(work, struct hci_dev,
341 					    le_scan_disable.work);
342 	int status;
343 
344 	bt_dev_dbg(hdev, "");
345 	hci_dev_lock(hdev);
346 
347 	if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
348 		goto _return;
349 
350 	cancel_delayed_work(&hdev->le_scan_restart);
351 
352 	status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
353 	if (status) {
354 		bt_dev_err(hdev, "failed to disable LE scan: %d", status);
355 		goto _return;
356 	}
357 
358 	hdev->discovery.scan_start = 0;
359 
360 	/* If we were running LE only scan, change discovery state. If
361 	 * we were running both LE and BR/EDR inquiry simultaneously,
362 	 * and BR/EDR inquiry is already finished, stop discovery,
363 	 * otherwise BR/EDR inquiry will stop discovery when finished.
364 	 * If we will resolve remote device name, do not change
365 	 * discovery state.
366 	 */
367 
368 	if (hdev->discovery.type == DISCOV_TYPE_LE)
369 		goto discov_stopped;
370 
371 	if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
372 		goto _return;
373 
374 	if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
375 		if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
376 		    hdev->discovery.state != DISCOVERY_RESOLVING)
377 			goto discov_stopped;
378 
379 		goto _return;
380 	}
381 
382 	status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
383 	if (status) {
384 		bt_dev_err(hdev, "inquiry failed: status %d", status);
385 		goto discov_stopped;
386 	}
387 
388 	goto _return;
389 
390 discov_stopped:
391 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
392 
393 _return:
394 	hci_dev_unlock(hdev);
395 }
396 
397 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
398 				       u8 filter_dup);
hci_le_scan_restart_sync(struct hci_dev * hdev)399 static int hci_le_scan_restart_sync(struct hci_dev *hdev)
400 {
401 	/* If controller is not scanning we are done. */
402 	if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
403 		return 0;
404 
405 	if (hdev->scanning_paused) {
406 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
407 		return 0;
408 	}
409 
410 	hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
411 	return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE,
412 					   LE_SCAN_FILTER_DUP_ENABLE);
413 }
414 
le_scan_restart(struct work_struct * work)415 static void le_scan_restart(struct work_struct *work)
416 {
417 	struct hci_dev *hdev = container_of(work, struct hci_dev,
418 					    le_scan_restart.work);
419 	unsigned long timeout, duration, scan_start, now;
420 	int status;
421 
422 	bt_dev_dbg(hdev, "");
423 
424 	status = hci_le_scan_restart_sync(hdev);
425 	if (status) {
426 		bt_dev_err(hdev, "failed to restart LE scan: status %d",
427 			   status);
428 		return;
429 	}
430 
431 	hci_dev_lock(hdev);
432 
433 	if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
434 	    !hdev->discovery.scan_start)
435 		goto unlock;
436 
437 	/* When the scan was started, hdev->le_scan_disable has been queued
438 	 * after duration from scan_start. During scan restart this job
439 	 * has been canceled, and we need to queue it again after proper
440 	 * timeout, to make sure that scan does not run indefinitely.
441 	 */
442 	duration = hdev->discovery.scan_duration;
443 	scan_start = hdev->discovery.scan_start;
444 	now = jiffies;
445 	if (now - scan_start <= duration) {
446 		int elapsed;
447 
448 		if (now >= scan_start)
449 			elapsed = now - scan_start;
450 		else
451 			elapsed = ULONG_MAX - scan_start + now;
452 
453 		timeout = duration - elapsed;
454 	} else {
455 		timeout = 0;
456 	}
457 
458 	queue_delayed_work(hdev->req_workqueue,
459 			   &hdev->le_scan_disable, timeout);
460 
461 unlock:
462 	hci_dev_unlock(hdev);
463 }
464 
reenable_adv_sync(struct hci_dev * hdev,void * data)465 static int reenable_adv_sync(struct hci_dev *hdev, void *data)
466 {
467 	bt_dev_dbg(hdev, "");
468 
469 	if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
470 	    list_empty(&hdev->adv_instances))
471 		return 0;
472 
473 	if (hdev->cur_adv_instance) {
474 		return hci_schedule_adv_instance_sync(hdev,
475 						      hdev->cur_adv_instance,
476 						      true);
477 	} else {
478 		if (ext_adv_capable(hdev)) {
479 			hci_start_ext_adv_sync(hdev, 0x00);
480 		} else {
481 			hci_update_adv_data_sync(hdev, 0x00);
482 			hci_update_scan_rsp_data_sync(hdev, 0x00);
483 			hci_enable_advertising_sync(hdev);
484 		}
485 	}
486 
487 	return 0;
488 }
489 
reenable_adv(struct work_struct * work)490 static void reenable_adv(struct work_struct *work)
491 {
492 	struct hci_dev *hdev = container_of(work, struct hci_dev,
493 					    reenable_adv_work);
494 	int status;
495 
496 	bt_dev_dbg(hdev, "");
497 
498 	hci_dev_lock(hdev);
499 
500 	status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
501 	if (status)
502 		bt_dev_err(hdev, "failed to reenable ADV: %d", status);
503 
504 	hci_dev_unlock(hdev);
505 }
506 
cancel_adv_timeout(struct hci_dev * hdev)507 static void cancel_adv_timeout(struct hci_dev *hdev)
508 {
509 	if (hdev->adv_instance_timeout) {
510 		hdev->adv_instance_timeout = 0;
511 		cancel_delayed_work(&hdev->adv_instance_expire);
512 	}
513 }
514 
515 /* For a single instance:
516  * - force == true: The instance will be removed even when its remaining
517  *   lifetime is not zero.
518  * - force == false: the instance will be deactivated but kept stored unless
519  *   the remaining lifetime is zero.
520  *
521  * For instance == 0x00:
522  * - force == true: All instances will be removed regardless of their timeout
523  *   setting.
524  * - force == false: Only instances that have a timeout will be removed.
525  */
hci_clear_adv_instance_sync(struct hci_dev * hdev,struct sock * sk,u8 instance,bool force)526 int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
527 				u8 instance, bool force)
528 {
529 	struct adv_info *adv_instance, *n, *next_instance = NULL;
530 	int err;
531 	u8 rem_inst;
532 
533 	/* Cancel any timeout concerning the removed instance(s). */
534 	if (!instance || hdev->cur_adv_instance == instance)
535 		cancel_adv_timeout(hdev);
536 
537 	/* Get the next instance to advertise BEFORE we remove
538 	 * the current one. This can be the same instance again
539 	 * if there is only one instance.
540 	 */
541 	if (instance && hdev->cur_adv_instance == instance)
542 		next_instance = hci_get_next_instance(hdev, instance);
543 
544 	if (instance == 0x00) {
545 		list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
546 					 list) {
547 			if (!(force || adv_instance->timeout))
548 				continue;
549 
550 			rem_inst = adv_instance->instance;
551 			err = hci_remove_adv_instance(hdev, rem_inst);
552 			if (!err)
553 				mgmt_advertising_removed(sk, hdev, rem_inst);
554 		}
555 	} else {
556 		adv_instance = hci_find_adv_instance(hdev, instance);
557 
558 		if (force || (adv_instance && adv_instance->timeout &&
559 			      !adv_instance->remaining_time)) {
560 			/* Don't advertise a removed instance. */
561 			if (next_instance &&
562 			    next_instance->instance == instance)
563 				next_instance = NULL;
564 
565 			err = hci_remove_adv_instance(hdev, instance);
566 			if (!err)
567 				mgmt_advertising_removed(sk, hdev, instance);
568 		}
569 	}
570 
571 	if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
572 		return 0;
573 
574 	if (next_instance && !ext_adv_capable(hdev))
575 		return hci_schedule_adv_instance_sync(hdev,
576 						      next_instance->instance,
577 						      false);
578 
579 	return 0;
580 }
581 
adv_timeout_expire_sync(struct hci_dev * hdev,void * data)582 static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
583 {
584 	u8 instance = *(u8 *)data;
585 
586 	kfree(data);
587 
588 	hci_clear_adv_instance_sync(hdev, NULL, instance, false);
589 
590 	if (list_empty(&hdev->adv_instances))
591 		return hci_disable_advertising_sync(hdev);
592 
593 	return 0;
594 }
595 
adv_timeout_expire(struct work_struct * work)596 static void adv_timeout_expire(struct work_struct *work)
597 {
598 	u8 *inst_ptr;
599 	struct hci_dev *hdev = container_of(work, struct hci_dev,
600 					    adv_instance_expire.work);
601 
602 	bt_dev_dbg(hdev, "");
603 
604 	hci_dev_lock(hdev);
605 
606 	hdev->adv_instance_timeout = 0;
607 
608 	if (hdev->cur_adv_instance == 0x00)
609 		goto unlock;
610 
611 	inst_ptr = kmalloc(1, GFP_KERNEL);
612 	if (!inst_ptr)
613 		goto unlock;
614 
615 	*inst_ptr = hdev->cur_adv_instance;
616 	hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
617 
618 unlock:
619 	hci_dev_unlock(hdev);
620 }
621 
hci_cmd_sync_init(struct hci_dev * hdev)622 void hci_cmd_sync_init(struct hci_dev *hdev)
623 {
624 	INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
625 	INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
626 	mutex_init(&hdev->cmd_sync_work_lock);
627 	mutex_init(&hdev->unregister_lock);
628 
629 	INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
630 	INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
631 	INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
632 	INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart);
633 	INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
634 }
635 
hci_cmd_sync_clear(struct hci_dev * hdev)636 void hci_cmd_sync_clear(struct hci_dev *hdev)
637 {
638 	struct hci_cmd_sync_work_entry *entry, *tmp;
639 
640 	cancel_work_sync(&hdev->cmd_sync_work);
641 	cancel_work_sync(&hdev->reenable_adv_work);
642 
643 	mutex_lock(&hdev->cmd_sync_work_lock);
644 	list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
645 		if (entry->destroy)
646 			entry->destroy(hdev, entry->data, -ECANCELED);
647 
648 		list_del(&entry->list);
649 		kfree(entry);
650 	}
651 	mutex_unlock(&hdev->cmd_sync_work_lock);
652 }
653 
__hci_cmd_sync_cancel(struct hci_dev * hdev,int err)654 void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
655 {
656 	bt_dev_dbg(hdev, "err 0x%2.2x", err);
657 
658 	if (hdev->req_status == HCI_REQ_PEND) {
659 		hdev->req_result = err;
660 		hdev->req_status = HCI_REQ_CANCELED;
661 
662 		cancel_delayed_work_sync(&hdev->cmd_timer);
663 		cancel_delayed_work_sync(&hdev->ncmd_timer);
664 		atomic_set(&hdev->cmd_cnt, 1);
665 
666 		wake_up_interruptible(&hdev->req_wait_q);
667 	}
668 }
669 
hci_cmd_sync_cancel(struct hci_dev * hdev,int err)670 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
671 {
672 	bt_dev_dbg(hdev, "err 0x%2.2x", err);
673 
674 	if (hdev->req_status == HCI_REQ_PEND) {
675 		hdev->req_result = err;
676 		hdev->req_status = HCI_REQ_CANCELED;
677 
678 		queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
679 	}
680 }
681 EXPORT_SYMBOL(hci_cmd_sync_cancel);
682 
hci_cmd_sync_queue(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)683 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
684 		       void *data, hci_cmd_sync_work_destroy_t destroy)
685 {
686 	struct hci_cmd_sync_work_entry *entry;
687 	int err = 0;
688 
689 	mutex_lock(&hdev->unregister_lock);
690 	if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
691 		err = -ENODEV;
692 		goto unlock;
693 	}
694 
695 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
696 	if (!entry) {
697 		err = -ENOMEM;
698 		goto unlock;
699 	}
700 	entry->func = func;
701 	entry->data = data;
702 	entry->destroy = destroy;
703 
704 	mutex_lock(&hdev->cmd_sync_work_lock);
705 	list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
706 	mutex_unlock(&hdev->cmd_sync_work_lock);
707 
708 	queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
709 
710 unlock:
711 	mutex_unlock(&hdev->unregister_lock);
712 	return err;
713 }
714 EXPORT_SYMBOL(hci_cmd_sync_queue);
715 
hci_update_eir_sync(struct hci_dev * hdev)716 int hci_update_eir_sync(struct hci_dev *hdev)
717 {
718 	struct hci_cp_write_eir cp;
719 
720 	bt_dev_dbg(hdev, "");
721 
722 	if (!hdev_is_powered(hdev))
723 		return 0;
724 
725 	if (!lmp_ext_inq_capable(hdev))
726 		return 0;
727 
728 	if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
729 		return 0;
730 
731 	if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
732 		return 0;
733 
734 	memset(&cp, 0, sizeof(cp));
735 
736 	eir_create(hdev, cp.data);
737 
738 	if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
739 		return 0;
740 
741 	memcpy(hdev->eir, cp.data, sizeof(cp.data));
742 
743 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
744 				     HCI_CMD_TIMEOUT);
745 }
746 
get_service_classes(struct hci_dev * hdev)747 static u8 get_service_classes(struct hci_dev *hdev)
748 {
749 	struct bt_uuid *uuid;
750 	u8 val = 0;
751 
752 	list_for_each_entry(uuid, &hdev->uuids, list)
753 		val |= uuid->svc_hint;
754 
755 	return val;
756 }
757 
hci_update_class_sync(struct hci_dev * hdev)758 int hci_update_class_sync(struct hci_dev *hdev)
759 {
760 	u8 cod[3];
761 
762 	bt_dev_dbg(hdev, "");
763 
764 	if (!hdev_is_powered(hdev))
765 		return 0;
766 
767 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
768 		return 0;
769 
770 	if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
771 		return 0;
772 
773 	cod[0] = hdev->minor_class;
774 	cod[1] = hdev->major_class;
775 	cod[2] = get_service_classes(hdev);
776 
777 	if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
778 		cod[1] |= 0x20;
779 
780 	if (memcmp(cod, hdev->dev_class, 3) == 0)
781 		return 0;
782 
783 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
784 				     sizeof(cod), cod, HCI_CMD_TIMEOUT);
785 }
786 
is_advertising_allowed(struct hci_dev * hdev,bool connectable)787 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
788 {
789 	/* If there is no connection we are OK to advertise. */
790 	if (hci_conn_num(hdev, LE_LINK) == 0)
791 		return true;
792 
793 	/* Check le_states if there is any connection in peripheral role. */
794 	if (hdev->conn_hash.le_num_peripheral > 0) {
795 		/* Peripheral connection state and non connectable mode
796 		 * bit 20.
797 		 */
798 		if (!connectable && !(hdev->le_states[2] & 0x10))
799 			return false;
800 
801 		/* Peripheral connection state and connectable mode bit 38
802 		 * and scannable bit 21.
803 		 */
804 		if (connectable && (!(hdev->le_states[4] & 0x40) ||
805 				    !(hdev->le_states[2] & 0x20)))
806 			return false;
807 	}
808 
809 	/* Check le_states if there is any connection in central role. */
810 	if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
811 		/* Central connection state and non connectable mode bit 18. */
812 		if (!connectable && !(hdev->le_states[2] & 0x02))
813 			return false;
814 
815 		/* Central connection state and connectable mode bit 35 and
816 		 * scannable 19.
817 		 */
818 		if (connectable && (!(hdev->le_states[4] & 0x08) ||
819 				    !(hdev->le_states[2] & 0x08)))
820 			return false;
821 	}
822 
823 	return true;
824 }
825 
adv_use_rpa(struct hci_dev * hdev,uint32_t flags)826 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
827 {
828 	/* If privacy is not enabled don't use RPA */
829 	if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
830 		return false;
831 
832 	/* If basic privacy mode is enabled use RPA */
833 	if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
834 		return true;
835 
836 	/* If limited privacy mode is enabled don't use RPA if we're
837 	 * both discoverable and bondable.
838 	 */
839 	if ((flags & MGMT_ADV_FLAG_DISCOV) &&
840 	    hci_dev_test_flag(hdev, HCI_BONDABLE))
841 		return false;
842 
843 	/* We're neither bondable nor discoverable in the limited
844 	 * privacy mode, therefore use RPA.
845 	 */
846 	return true;
847 }
848 
hci_set_random_addr_sync(struct hci_dev * hdev,bdaddr_t * rpa)849 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
850 {
851 	/* If we're advertising or initiating an LE connection we can't
852 	 * go ahead and change the random address at this time. This is
853 	 * because the eventual initiator address used for the
854 	 * subsequently created connection will be undefined (some
855 	 * controllers use the new address and others the one we had
856 	 * when the operation started).
857 	 *
858 	 * In this kind of scenario skip the update and let the random
859 	 * address be updated at the next cycle.
860 	 */
861 	if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
862 	    hci_lookup_le_connect(hdev)) {
863 		bt_dev_dbg(hdev, "Deferring random address update");
864 		hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
865 		return 0;
866 	}
867 
868 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
869 				     6, rpa, HCI_CMD_TIMEOUT);
870 }
871 
hci_update_random_address_sync(struct hci_dev * hdev,bool require_privacy,bool rpa,u8 * own_addr_type)872 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
873 				   bool rpa, u8 *own_addr_type)
874 {
875 	int err;
876 
877 	/* If privacy is enabled use a resolvable private address. If
878 	 * current RPA has expired or there is something else than
879 	 * the current RPA in use, then generate a new one.
880 	 */
881 	if (rpa) {
882 		/* If Controller supports LL Privacy use own address type is
883 		 * 0x03
884 		 */
885 		if (use_ll_privacy(hdev))
886 			*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
887 		else
888 			*own_addr_type = ADDR_LE_DEV_RANDOM;
889 
890 		/* Check if RPA is valid */
891 		if (rpa_valid(hdev))
892 			return 0;
893 
894 		err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
895 		if (err < 0) {
896 			bt_dev_err(hdev, "failed to generate new RPA");
897 			return err;
898 		}
899 
900 		err = hci_set_random_addr_sync(hdev, &hdev->rpa);
901 		if (err)
902 			return err;
903 
904 		return 0;
905 	}
906 
907 	/* In case of required privacy without resolvable private address,
908 	 * use an non-resolvable private address. This is useful for active
909 	 * scanning and non-connectable advertising.
910 	 */
911 	if (require_privacy) {
912 		bdaddr_t nrpa;
913 
914 		while (true) {
915 			/* The non-resolvable private address is generated
916 			 * from random six bytes with the two most significant
917 			 * bits cleared.
918 			 */
919 			get_random_bytes(&nrpa, 6);
920 			nrpa.b[5] &= 0x3f;
921 
922 			/* The non-resolvable private address shall not be
923 			 * equal to the public address.
924 			 */
925 			if (bacmp(&hdev->bdaddr, &nrpa))
926 				break;
927 		}
928 
929 		*own_addr_type = ADDR_LE_DEV_RANDOM;
930 
931 		return hci_set_random_addr_sync(hdev, &nrpa);
932 	}
933 
934 	/* If forcing static address is in use or there is no public
935 	 * address use the static address as random address (but skip
936 	 * the HCI command if the current random address is already the
937 	 * static one.
938 	 *
939 	 * In case BR/EDR has been disabled on a dual-mode controller
940 	 * and a static address has been configured, then use that
941 	 * address instead of the public BR/EDR address.
942 	 */
943 	if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
944 	    !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
945 	    (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
946 	     bacmp(&hdev->static_addr, BDADDR_ANY))) {
947 		*own_addr_type = ADDR_LE_DEV_RANDOM;
948 		if (bacmp(&hdev->static_addr, &hdev->random_addr))
949 			return hci_set_random_addr_sync(hdev,
950 							&hdev->static_addr);
951 		return 0;
952 	}
953 
954 	/* Neither privacy nor static address is being used so use a
955 	 * public address.
956 	 */
957 	*own_addr_type = ADDR_LE_DEV_PUBLIC;
958 
959 	return 0;
960 }
961 
hci_disable_ext_adv_instance_sync(struct hci_dev * hdev,u8 instance)962 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
963 {
964 	struct hci_cp_le_set_ext_adv_enable *cp;
965 	struct hci_cp_ext_adv_set *set;
966 	u8 data[sizeof(*cp) + sizeof(*set) * 1];
967 	u8 size;
968 
969 	/* If request specifies an instance that doesn't exist, fail */
970 	if (instance > 0) {
971 		struct adv_info *adv;
972 
973 		adv = hci_find_adv_instance(hdev, instance);
974 		if (!adv)
975 			return -EINVAL;
976 
977 		/* If not enabled there is nothing to do */
978 		if (!adv->enabled)
979 			return 0;
980 	}
981 
982 	memset(data, 0, sizeof(data));
983 
984 	cp = (void *)data;
985 	set = (void *)cp->data;
986 
987 	/* Instance 0x00 indicates all advertising instances will be disabled */
988 	cp->num_of_sets = !!instance;
989 	cp->enable = 0x00;
990 
991 	set->handle = instance;
992 
993 	size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
994 
995 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
996 				     size, data, HCI_CMD_TIMEOUT);
997 }
998 
hci_set_adv_set_random_addr_sync(struct hci_dev * hdev,u8 instance,bdaddr_t * random_addr)999 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1000 					    bdaddr_t *random_addr)
1001 {
1002 	struct hci_cp_le_set_adv_set_rand_addr cp;
1003 	int err;
1004 
1005 	if (!instance) {
1006 		/* Instance 0x00 doesn't have an adv_info, instead it uses
1007 		 * hdev->random_addr to track its address so whenever it needs
1008 		 * to be updated this also set the random address since
1009 		 * hdev->random_addr is shared with scan state machine.
1010 		 */
1011 		err = hci_set_random_addr_sync(hdev, random_addr);
1012 		if (err)
1013 			return err;
1014 	}
1015 
1016 	memset(&cp, 0, sizeof(cp));
1017 
1018 	cp.handle = instance;
1019 	bacpy(&cp.bdaddr, random_addr);
1020 
1021 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1022 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1023 }
1024 
hci_setup_ext_adv_instance_sync(struct hci_dev * hdev,u8 instance)1025 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1026 {
1027 	struct hci_cp_le_set_ext_adv_params cp;
1028 	bool connectable;
1029 	u32 flags;
1030 	bdaddr_t random_addr;
1031 	u8 own_addr_type;
1032 	int err;
1033 	struct adv_info *adv;
1034 	bool secondary_adv;
1035 
1036 	if (instance > 0) {
1037 		adv = hci_find_adv_instance(hdev, instance);
1038 		if (!adv)
1039 			return -EINVAL;
1040 	} else {
1041 		adv = NULL;
1042 	}
1043 
1044 	/* Updating parameters of an active instance will return a
1045 	 * Command Disallowed error, so we must first disable the
1046 	 * instance if it is active.
1047 	 */
1048 	if (adv && !adv->pending) {
1049 		err = hci_disable_ext_adv_instance_sync(hdev, instance);
1050 		if (err)
1051 			return err;
1052 	}
1053 
1054 	flags = hci_adv_instance_flags(hdev, instance);
1055 
1056 	/* If the "connectable" instance flag was not set, then choose between
1057 	 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1058 	 */
1059 	connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1060 		      mgmt_get_connectable(hdev);
1061 
1062 	if (!is_advertising_allowed(hdev, connectable))
1063 		return -EPERM;
1064 
1065 	/* Set require_privacy to true only when non-connectable
1066 	 * advertising is used. In that case it is fine to use a
1067 	 * non-resolvable private address.
1068 	 */
1069 	err = hci_get_random_address(hdev, !connectable,
1070 				     adv_use_rpa(hdev, flags), adv,
1071 				     &own_addr_type, &random_addr);
1072 	if (err < 0)
1073 		return err;
1074 
1075 	memset(&cp, 0, sizeof(cp));
1076 
1077 	if (adv) {
1078 		hci_cpu_to_le24(adv->min_interval, cp.min_interval);
1079 		hci_cpu_to_le24(adv->max_interval, cp.max_interval);
1080 		cp.tx_power = adv->tx_power;
1081 	} else {
1082 		hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1083 		hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1084 		cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
1085 	}
1086 
1087 	secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
1088 
1089 	if (connectable) {
1090 		if (secondary_adv)
1091 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
1092 		else
1093 			cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
1094 	} else if (hci_adv_instance_is_scannable(hdev, instance) ||
1095 		   (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
1096 		if (secondary_adv)
1097 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
1098 		else
1099 			cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
1100 	} else {
1101 		if (secondary_adv)
1102 			cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
1103 		else
1104 			cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
1105 	}
1106 
1107 	/* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
1108 	 * contains the peer’s Identity Address and the Peer_Address_Type
1109 	 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
1110 	 * These parameters are used to locate the corresponding local IRK in
1111 	 * the resolving list; this IRK is used to generate their own address
1112 	 * used in the advertisement.
1113 	 */
1114 	if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
1115 		hci_copy_identity_address(hdev, &cp.peer_addr,
1116 					  &cp.peer_addr_type);
1117 
1118 	cp.own_addr_type = own_addr_type;
1119 	cp.channel_map = hdev->le_adv_channel_map;
1120 	cp.handle = instance;
1121 
1122 	if (flags & MGMT_ADV_FLAG_SEC_2M) {
1123 		cp.primary_phy = HCI_ADV_PHY_1M;
1124 		cp.secondary_phy = HCI_ADV_PHY_2M;
1125 	} else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
1126 		cp.primary_phy = HCI_ADV_PHY_CODED;
1127 		cp.secondary_phy = HCI_ADV_PHY_CODED;
1128 	} else {
1129 		/* In all other cases use 1M */
1130 		cp.primary_phy = HCI_ADV_PHY_1M;
1131 		cp.secondary_phy = HCI_ADV_PHY_1M;
1132 	}
1133 
1134 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1135 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1136 	if (err)
1137 		return err;
1138 
1139 	if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
1140 	     own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
1141 	    bacmp(&random_addr, BDADDR_ANY)) {
1142 		/* Check if random address need to be updated */
1143 		if (adv) {
1144 			if (!bacmp(&random_addr, &adv->random_addr))
1145 				return 0;
1146 		} else {
1147 			if (!bacmp(&random_addr, &hdev->random_addr))
1148 				return 0;
1149 		}
1150 
1151 		return hci_set_adv_set_random_addr_sync(hdev, instance,
1152 							&random_addr);
1153 	}
1154 
1155 	return 0;
1156 }
1157 
hci_set_ext_scan_rsp_data_sync(struct hci_dev * hdev,u8 instance)1158 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1159 {
1160 	struct {
1161 		struct hci_cp_le_set_ext_scan_rsp_data cp;
1162 		u8 data[HCI_MAX_EXT_AD_LENGTH];
1163 	} pdu;
1164 	u8 len;
1165 	struct adv_info *adv = NULL;
1166 	int err;
1167 
1168 	memset(&pdu, 0, sizeof(pdu));
1169 
1170 	if (instance) {
1171 		adv = hci_find_adv_instance(hdev, instance);
1172 		if (!adv || !adv->scan_rsp_changed)
1173 			return 0;
1174 	}
1175 
1176 	len = eir_create_scan_rsp(hdev, instance, pdu.data);
1177 
1178 	pdu.cp.handle = instance;
1179 	pdu.cp.length = len;
1180 	pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1181 	pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1182 
1183 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1184 				    sizeof(pdu.cp) + len, &pdu.cp,
1185 				    HCI_CMD_TIMEOUT);
1186 	if (err)
1187 		return err;
1188 
1189 	if (adv) {
1190 		adv->scan_rsp_changed = false;
1191 	} else {
1192 		memcpy(hdev->scan_rsp_data, pdu.data, len);
1193 		hdev->scan_rsp_data_len = len;
1194 	}
1195 
1196 	return 0;
1197 }
1198 
__hci_set_scan_rsp_data_sync(struct hci_dev * hdev,u8 instance)1199 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1200 {
1201 	struct hci_cp_le_set_scan_rsp_data cp;
1202 	u8 len;
1203 
1204 	memset(&cp, 0, sizeof(cp));
1205 
1206 	len = eir_create_scan_rsp(hdev, instance, cp.data);
1207 
1208 	if (hdev->scan_rsp_data_len == len &&
1209 	    !memcmp(cp.data, hdev->scan_rsp_data, len))
1210 		return 0;
1211 
1212 	memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1213 	hdev->scan_rsp_data_len = len;
1214 
1215 	cp.length = len;
1216 
1217 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1218 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1219 }
1220 
hci_update_scan_rsp_data_sync(struct hci_dev * hdev,u8 instance)1221 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1222 {
1223 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1224 		return 0;
1225 
1226 	if (ext_adv_capable(hdev))
1227 		return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1228 
1229 	return __hci_set_scan_rsp_data_sync(hdev, instance);
1230 }
1231 
hci_enable_ext_advertising_sync(struct hci_dev * hdev,u8 instance)1232 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1233 {
1234 	struct hci_cp_le_set_ext_adv_enable *cp;
1235 	struct hci_cp_ext_adv_set *set;
1236 	u8 data[sizeof(*cp) + sizeof(*set) * 1];
1237 	struct adv_info *adv;
1238 
1239 	if (instance > 0) {
1240 		adv = hci_find_adv_instance(hdev, instance);
1241 		if (!adv)
1242 			return -EINVAL;
1243 		/* If already enabled there is nothing to do */
1244 		if (adv->enabled)
1245 			return 0;
1246 	} else {
1247 		adv = NULL;
1248 	}
1249 
1250 	cp = (void *)data;
1251 	set = (void *)cp->data;
1252 
1253 	memset(cp, 0, sizeof(*cp));
1254 
1255 	cp->enable = 0x01;
1256 	cp->num_of_sets = 0x01;
1257 
1258 	memset(set, 0, sizeof(*set));
1259 
1260 	set->handle = instance;
1261 
1262 	/* Set duration per instance since controller is responsible for
1263 	 * scheduling it.
1264 	 */
1265 	if (adv && adv->timeout) {
1266 		u16 duration = adv->timeout * MSEC_PER_SEC;
1267 
1268 		/* Time = N * 10 ms */
1269 		set->duration = cpu_to_le16(duration / 10);
1270 	}
1271 
1272 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1273 				     sizeof(*cp) +
1274 				     sizeof(*set) * cp->num_of_sets,
1275 				     data, HCI_CMD_TIMEOUT);
1276 }
1277 
hci_start_ext_adv_sync(struct hci_dev * hdev,u8 instance)1278 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1279 {
1280 	int err;
1281 
1282 	err = hci_setup_ext_adv_instance_sync(hdev, instance);
1283 	if (err)
1284 		return err;
1285 
1286 	err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1287 	if (err)
1288 		return err;
1289 
1290 	return hci_enable_ext_advertising_sync(hdev, instance);
1291 }
1292 
hci_disable_per_advertising_sync(struct hci_dev * hdev,u8 instance)1293 static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1294 {
1295 	struct hci_cp_le_set_per_adv_enable cp;
1296 
1297 	/* If periodic advertising already disabled there is nothing to do. */
1298 	if (!hci_dev_test_flag(hdev, HCI_LE_PER_ADV))
1299 		return 0;
1300 
1301 	memset(&cp, 0, sizeof(cp));
1302 
1303 	cp.enable = 0x00;
1304 	cp.handle = instance;
1305 
1306 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1307 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1308 }
1309 
hci_set_per_adv_params_sync(struct hci_dev * hdev,u8 instance,u16 min_interval,u16 max_interval)1310 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1311 				       u16 min_interval, u16 max_interval)
1312 {
1313 	struct hci_cp_le_set_per_adv_params cp;
1314 
1315 	memset(&cp, 0, sizeof(cp));
1316 
1317 	if (!min_interval)
1318 		min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1319 
1320 	if (!max_interval)
1321 		max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1322 
1323 	cp.handle = instance;
1324 	cp.min_interval = cpu_to_le16(min_interval);
1325 	cp.max_interval = cpu_to_le16(max_interval);
1326 	cp.periodic_properties = 0x0000;
1327 
1328 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1329 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1330 }
1331 
hci_set_per_adv_data_sync(struct hci_dev * hdev,u8 instance)1332 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1333 {
1334 	struct {
1335 		struct hci_cp_le_set_per_adv_data cp;
1336 		u8 data[HCI_MAX_PER_AD_LENGTH];
1337 	} pdu;
1338 	u8 len;
1339 
1340 	memset(&pdu, 0, sizeof(pdu));
1341 
1342 	if (instance) {
1343 		struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1344 
1345 		if (!adv || !adv->periodic)
1346 			return 0;
1347 	}
1348 
1349 	len = eir_create_per_adv_data(hdev, instance, pdu.data);
1350 
1351 	pdu.cp.length = len;
1352 	pdu.cp.handle = instance;
1353 	pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1354 
1355 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1356 				     sizeof(pdu.cp) + len, &pdu,
1357 				     HCI_CMD_TIMEOUT);
1358 }
1359 
hci_enable_per_advertising_sync(struct hci_dev * hdev,u8 instance)1360 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1361 {
1362 	struct hci_cp_le_set_per_adv_enable cp;
1363 
1364 	/* If periodic advertising already enabled there is nothing to do. */
1365 	if (hci_dev_test_flag(hdev, HCI_LE_PER_ADV))
1366 		return 0;
1367 
1368 	memset(&cp, 0, sizeof(cp));
1369 
1370 	cp.enable = 0x01;
1371 	cp.handle = instance;
1372 
1373 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1374 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1375 }
1376 
1377 /* Checks if periodic advertising data contains a Basic Announcement and if it
1378  * does generates a Broadcast ID and add Broadcast Announcement.
1379  */
hci_adv_bcast_annoucement(struct hci_dev * hdev,struct adv_info * adv)1380 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1381 {
1382 	u8 bid[3];
1383 	u8 ad[4 + 3];
1384 
1385 	/* Skip if NULL adv as instance 0x00 is used for general purpose
1386 	 * advertising so it cannot used for the likes of Broadcast Announcement
1387 	 * as it can be overwritten at any point.
1388 	 */
1389 	if (!adv)
1390 		return 0;
1391 
1392 	/* Check if PA data doesn't contains a Basic Audio Announcement then
1393 	 * there is nothing to do.
1394 	 */
1395 	if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1396 				  0x1851, NULL))
1397 		return 0;
1398 
1399 	/* Check if advertising data already has a Broadcast Announcement since
1400 	 * the process may want to control the Broadcast ID directly and in that
1401 	 * case the kernel shall no interfere.
1402 	 */
1403 	if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1404 				 NULL))
1405 		return 0;
1406 
1407 	/* Generate Broadcast ID */
1408 	get_random_bytes(bid, sizeof(bid));
1409 	eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1410 	hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1411 
1412 	return hci_update_adv_data_sync(hdev, adv->instance);
1413 }
1414 
hci_start_per_adv_sync(struct hci_dev * hdev,u8 instance,u8 data_len,u8 * data,u32 flags,u16 min_interval,u16 max_interval,u16 sync_interval)1415 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1416 			   u8 *data, u32 flags, u16 min_interval,
1417 			   u16 max_interval, u16 sync_interval)
1418 {
1419 	struct adv_info *adv = NULL;
1420 	int err;
1421 	bool added = false;
1422 
1423 	hci_disable_per_advertising_sync(hdev, instance);
1424 
1425 	if (instance) {
1426 		adv = hci_find_adv_instance(hdev, instance);
1427 		/* Create an instance if that could not be found */
1428 		if (!adv) {
1429 			adv = hci_add_per_instance(hdev, instance, flags,
1430 						   data_len, data,
1431 						   sync_interval,
1432 						   sync_interval);
1433 			if (IS_ERR(adv))
1434 				return PTR_ERR(adv);
1435 			added = true;
1436 		}
1437 	}
1438 
1439 	/* Only start advertising if instance 0 or if a dedicated instance has
1440 	 * been added.
1441 	 */
1442 	if (!adv || added) {
1443 		err = hci_start_ext_adv_sync(hdev, instance);
1444 		if (err < 0)
1445 			goto fail;
1446 
1447 		err = hci_adv_bcast_annoucement(hdev, adv);
1448 		if (err < 0)
1449 			goto fail;
1450 	}
1451 
1452 	err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1453 					  max_interval);
1454 	if (err < 0)
1455 		goto fail;
1456 
1457 	err = hci_set_per_adv_data_sync(hdev, instance);
1458 	if (err < 0)
1459 		goto fail;
1460 
1461 	err = hci_enable_per_advertising_sync(hdev, instance);
1462 	if (err < 0)
1463 		goto fail;
1464 
1465 	return 0;
1466 
1467 fail:
1468 	if (added)
1469 		hci_remove_adv_instance(hdev, instance);
1470 
1471 	return err;
1472 }
1473 
hci_start_adv_sync(struct hci_dev * hdev,u8 instance)1474 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1475 {
1476 	int err;
1477 
1478 	if (ext_adv_capable(hdev))
1479 		return hci_start_ext_adv_sync(hdev, instance);
1480 
1481 	err = hci_update_adv_data_sync(hdev, instance);
1482 	if (err)
1483 		return err;
1484 
1485 	err = hci_update_scan_rsp_data_sync(hdev, instance);
1486 	if (err)
1487 		return err;
1488 
1489 	return hci_enable_advertising_sync(hdev);
1490 }
1491 
hci_enable_advertising_sync(struct hci_dev * hdev)1492 int hci_enable_advertising_sync(struct hci_dev *hdev)
1493 {
1494 	struct adv_info *adv_instance;
1495 	struct hci_cp_le_set_adv_param cp;
1496 	u8 own_addr_type, enable = 0x01;
1497 	bool connectable;
1498 	u16 adv_min_interval, adv_max_interval;
1499 	u32 flags;
1500 	u8 status;
1501 
1502 	if (ext_adv_capable(hdev))
1503 		return hci_enable_ext_advertising_sync(hdev,
1504 						       hdev->cur_adv_instance);
1505 
1506 	flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1507 	adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1508 
1509 	/* If the "connectable" instance flag was not set, then choose between
1510 	 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1511 	 */
1512 	connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1513 		      mgmt_get_connectable(hdev);
1514 
1515 	if (!is_advertising_allowed(hdev, connectable))
1516 		return -EINVAL;
1517 
1518 	status = hci_disable_advertising_sync(hdev);
1519 	if (status)
1520 		return status;
1521 
1522 	/* Clear the HCI_LE_ADV bit temporarily so that the
1523 	 * hci_update_random_address knows that it's safe to go ahead
1524 	 * and write a new random address. The flag will be set back on
1525 	 * as soon as the SET_ADV_ENABLE HCI command completes.
1526 	 */
1527 	hci_dev_clear_flag(hdev, HCI_LE_ADV);
1528 
1529 	/* Set require_privacy to true only when non-connectable
1530 	 * advertising is used. In that case it is fine to use a
1531 	 * non-resolvable private address.
1532 	 */
1533 	status = hci_update_random_address_sync(hdev, !connectable,
1534 						adv_use_rpa(hdev, flags),
1535 						&own_addr_type);
1536 	if (status)
1537 		return status;
1538 
1539 	memset(&cp, 0, sizeof(cp));
1540 
1541 	if (adv_instance) {
1542 		adv_min_interval = adv_instance->min_interval;
1543 		adv_max_interval = adv_instance->max_interval;
1544 	} else {
1545 		adv_min_interval = hdev->le_adv_min_interval;
1546 		adv_max_interval = hdev->le_adv_max_interval;
1547 	}
1548 
1549 	if (connectable) {
1550 		cp.type = LE_ADV_IND;
1551 	} else {
1552 		if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1553 			cp.type = LE_ADV_SCAN_IND;
1554 		else
1555 			cp.type = LE_ADV_NONCONN_IND;
1556 
1557 		if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1558 		    hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1559 			adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1560 			adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1561 		}
1562 	}
1563 
1564 	cp.min_interval = cpu_to_le16(adv_min_interval);
1565 	cp.max_interval = cpu_to_le16(adv_max_interval);
1566 	cp.own_address_type = own_addr_type;
1567 	cp.channel_map = hdev->le_adv_channel_map;
1568 
1569 	status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1570 				       sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1571 	if (status)
1572 		return status;
1573 
1574 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1575 				     sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1576 }
1577 
enable_advertising_sync(struct hci_dev * hdev,void * data)1578 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1579 {
1580 	return hci_enable_advertising_sync(hdev);
1581 }
1582 
hci_enable_advertising(struct hci_dev * hdev)1583 int hci_enable_advertising(struct hci_dev *hdev)
1584 {
1585 	if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1586 	    list_empty(&hdev->adv_instances))
1587 		return 0;
1588 
1589 	return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1590 }
1591 
hci_remove_ext_adv_instance_sync(struct hci_dev * hdev,u8 instance,struct sock * sk)1592 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1593 				     struct sock *sk)
1594 {
1595 	int err;
1596 
1597 	if (!ext_adv_capable(hdev))
1598 		return 0;
1599 
1600 	err = hci_disable_ext_adv_instance_sync(hdev, instance);
1601 	if (err)
1602 		return err;
1603 
1604 	/* If request specifies an instance that doesn't exist, fail */
1605 	if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1606 		return -EINVAL;
1607 
1608 	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1609 					sizeof(instance), &instance, 0,
1610 					HCI_CMD_TIMEOUT, sk);
1611 }
1612 
remove_ext_adv_sync(struct hci_dev * hdev,void * data)1613 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1614 {
1615 	struct adv_info *adv = data;
1616 	u8 instance = 0;
1617 
1618 	if (adv)
1619 		instance = adv->instance;
1620 
1621 	return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1622 }
1623 
hci_remove_ext_adv_instance(struct hci_dev * hdev,u8 instance)1624 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1625 {
1626 	struct adv_info *adv = NULL;
1627 
1628 	if (instance) {
1629 		adv = hci_find_adv_instance(hdev, instance);
1630 		if (!adv)
1631 			return -EINVAL;
1632 	}
1633 
1634 	return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1635 }
1636 
hci_le_terminate_big_sync(struct hci_dev * hdev,u8 handle,u8 reason)1637 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1638 {
1639 	struct hci_cp_le_term_big cp;
1640 
1641 	memset(&cp, 0, sizeof(cp));
1642 	cp.handle = handle;
1643 	cp.reason = reason;
1644 
1645 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1646 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1647 }
1648 
hci_set_ext_adv_data_sync(struct hci_dev * hdev,u8 instance)1649 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1650 {
1651 	struct {
1652 		struct hci_cp_le_set_ext_adv_data cp;
1653 		u8 data[HCI_MAX_EXT_AD_LENGTH];
1654 	} pdu;
1655 	u8 len;
1656 	struct adv_info *adv = NULL;
1657 	int err;
1658 
1659 	memset(&pdu, 0, sizeof(pdu));
1660 
1661 	if (instance) {
1662 		adv = hci_find_adv_instance(hdev, instance);
1663 		if (!adv || !adv->adv_data_changed)
1664 			return 0;
1665 	}
1666 
1667 	len = eir_create_adv_data(hdev, instance, pdu.data);
1668 
1669 	pdu.cp.length = len;
1670 	pdu.cp.handle = instance;
1671 	pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1672 	pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1673 
1674 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1675 				    sizeof(pdu.cp) + len, &pdu.cp,
1676 				    HCI_CMD_TIMEOUT);
1677 	if (err)
1678 		return err;
1679 
1680 	/* Update data if the command succeed */
1681 	if (adv) {
1682 		adv->adv_data_changed = false;
1683 	} else {
1684 		memcpy(hdev->adv_data, pdu.data, len);
1685 		hdev->adv_data_len = len;
1686 	}
1687 
1688 	return 0;
1689 }
1690 
hci_set_adv_data_sync(struct hci_dev * hdev,u8 instance)1691 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1692 {
1693 	struct hci_cp_le_set_adv_data cp;
1694 	u8 len;
1695 
1696 	memset(&cp, 0, sizeof(cp));
1697 
1698 	len = eir_create_adv_data(hdev, instance, cp.data);
1699 
1700 	/* There's nothing to do if the data hasn't changed */
1701 	if (hdev->adv_data_len == len &&
1702 	    memcmp(cp.data, hdev->adv_data, len) == 0)
1703 		return 0;
1704 
1705 	memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1706 	hdev->adv_data_len = len;
1707 
1708 	cp.length = len;
1709 
1710 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1711 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1712 }
1713 
hci_update_adv_data_sync(struct hci_dev * hdev,u8 instance)1714 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1715 {
1716 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1717 		return 0;
1718 
1719 	if (ext_adv_capable(hdev))
1720 		return hci_set_ext_adv_data_sync(hdev, instance);
1721 
1722 	return hci_set_adv_data_sync(hdev, instance);
1723 }
1724 
hci_schedule_adv_instance_sync(struct hci_dev * hdev,u8 instance,bool force)1725 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1726 				   bool force)
1727 {
1728 	struct adv_info *adv = NULL;
1729 	u16 timeout;
1730 
1731 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1732 		return -EPERM;
1733 
1734 	if (hdev->adv_instance_timeout)
1735 		return -EBUSY;
1736 
1737 	adv = hci_find_adv_instance(hdev, instance);
1738 	if (!adv)
1739 		return -ENOENT;
1740 
1741 	/* A zero timeout means unlimited advertising. As long as there is
1742 	 * only one instance, duration should be ignored. We still set a timeout
1743 	 * in case further instances are being added later on.
1744 	 *
1745 	 * If the remaining lifetime of the instance is more than the duration
1746 	 * then the timeout corresponds to the duration, otherwise it will be
1747 	 * reduced to the remaining instance lifetime.
1748 	 */
1749 	if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1750 		timeout = adv->duration;
1751 	else
1752 		timeout = adv->remaining_time;
1753 
1754 	/* The remaining time is being reduced unless the instance is being
1755 	 * advertised without time limit.
1756 	 */
1757 	if (adv->timeout)
1758 		adv->remaining_time = adv->remaining_time - timeout;
1759 
1760 	/* Only use work for scheduling instances with legacy advertising */
1761 	if (!ext_adv_capable(hdev)) {
1762 		hdev->adv_instance_timeout = timeout;
1763 		queue_delayed_work(hdev->req_workqueue,
1764 				   &hdev->adv_instance_expire,
1765 				   msecs_to_jiffies(timeout * 1000));
1766 	}
1767 
1768 	/* If we're just re-scheduling the same instance again then do not
1769 	 * execute any HCI commands. This happens when a single instance is
1770 	 * being advertised.
1771 	 */
1772 	if (!force && hdev->cur_adv_instance == instance &&
1773 	    hci_dev_test_flag(hdev, HCI_LE_ADV))
1774 		return 0;
1775 
1776 	hdev->cur_adv_instance = instance;
1777 
1778 	return hci_start_adv_sync(hdev, instance);
1779 }
1780 
hci_clear_adv_sets_sync(struct hci_dev * hdev,struct sock * sk)1781 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1782 {
1783 	int err;
1784 
1785 	if (!ext_adv_capable(hdev))
1786 		return 0;
1787 
1788 	/* Disable instance 0x00 to disable all instances */
1789 	err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1790 	if (err)
1791 		return err;
1792 
1793 	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1794 					0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1795 }
1796 
hci_clear_adv_sync(struct hci_dev * hdev,struct sock * sk,bool force)1797 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1798 {
1799 	struct adv_info *adv, *n;
1800 	int err = 0;
1801 
1802 	if (ext_adv_capable(hdev))
1803 		/* Remove all existing sets */
1804 		err = hci_clear_adv_sets_sync(hdev, sk);
1805 	if (ext_adv_capable(hdev))
1806 		return err;
1807 
1808 	/* This is safe as long as there is no command send while the lock is
1809 	 * held.
1810 	 */
1811 	hci_dev_lock(hdev);
1812 
1813 	/* Cleanup non-ext instances */
1814 	list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1815 		u8 instance = adv->instance;
1816 		int err;
1817 
1818 		if (!(force || adv->timeout))
1819 			continue;
1820 
1821 		err = hci_remove_adv_instance(hdev, instance);
1822 		if (!err)
1823 			mgmt_advertising_removed(sk, hdev, instance);
1824 	}
1825 
1826 	hci_dev_unlock(hdev);
1827 
1828 	return 0;
1829 }
1830 
hci_remove_adv_sync(struct hci_dev * hdev,u8 instance,struct sock * sk)1831 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1832 			       struct sock *sk)
1833 {
1834 	int err = 0;
1835 
1836 	/* If we use extended advertising, instance has to be removed first. */
1837 	if (ext_adv_capable(hdev))
1838 		err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1839 	if (ext_adv_capable(hdev))
1840 		return err;
1841 
1842 	/* This is safe as long as there is no command send while the lock is
1843 	 * held.
1844 	 */
1845 	hci_dev_lock(hdev);
1846 
1847 	err = hci_remove_adv_instance(hdev, instance);
1848 	if (!err)
1849 		mgmt_advertising_removed(sk, hdev, instance);
1850 
1851 	hci_dev_unlock(hdev);
1852 
1853 	return err;
1854 }
1855 
1856 /* For a single instance:
1857  * - force == true: The instance will be removed even when its remaining
1858  *   lifetime is not zero.
1859  * - force == false: the instance will be deactivated but kept stored unless
1860  *   the remaining lifetime is zero.
1861  *
1862  * For instance == 0x00:
1863  * - force == true: All instances will be removed regardless of their timeout
1864  *   setting.
1865  * - force == false: Only instances that have a timeout will be removed.
1866  */
hci_remove_advertising_sync(struct hci_dev * hdev,struct sock * sk,u8 instance,bool force)1867 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1868 				u8 instance, bool force)
1869 {
1870 	struct adv_info *next = NULL;
1871 	int err;
1872 
1873 	/* Cancel any timeout concerning the removed instance(s). */
1874 	if (!instance || hdev->cur_adv_instance == instance)
1875 		cancel_adv_timeout(hdev);
1876 
1877 	/* Get the next instance to advertise BEFORE we remove
1878 	 * the current one. This can be the same instance again
1879 	 * if there is only one instance.
1880 	 */
1881 	if (hdev->cur_adv_instance == instance)
1882 		next = hci_get_next_instance(hdev, instance);
1883 
1884 	if (!instance) {
1885 		err = hci_clear_adv_sync(hdev, sk, force);
1886 		if (err)
1887 			return err;
1888 	} else {
1889 		struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1890 
1891 		if (force || (adv && adv->timeout && !adv->remaining_time)) {
1892 			/* Don't advertise a removed instance. */
1893 			if (next && next->instance == instance)
1894 				next = NULL;
1895 
1896 			err = hci_remove_adv_sync(hdev, instance, sk);
1897 			if (err)
1898 				return err;
1899 		}
1900 	}
1901 
1902 	if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1903 		return 0;
1904 
1905 	if (next && !ext_adv_capable(hdev))
1906 		hci_schedule_adv_instance_sync(hdev, next->instance, false);
1907 
1908 	return 0;
1909 }
1910 
hci_read_rssi_sync(struct hci_dev * hdev,__le16 handle)1911 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1912 {
1913 	struct hci_cp_read_rssi cp;
1914 
1915 	cp.handle = handle;
1916 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1917 					sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1918 }
1919 
hci_read_clock_sync(struct hci_dev * hdev,struct hci_cp_read_clock * cp)1920 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1921 {
1922 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1923 					sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1924 }
1925 
hci_read_tx_power_sync(struct hci_dev * hdev,__le16 handle,u8 type)1926 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1927 {
1928 	struct hci_cp_read_tx_power cp;
1929 
1930 	cp.handle = handle;
1931 	cp.type = type;
1932 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1933 					sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1934 }
1935 
hci_disable_advertising_sync(struct hci_dev * hdev)1936 int hci_disable_advertising_sync(struct hci_dev *hdev)
1937 {
1938 	u8 enable = 0x00;
1939 	int err = 0;
1940 
1941 	/* If controller is not advertising we are done. */
1942 	if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1943 		return 0;
1944 
1945 	if (ext_adv_capable(hdev))
1946 		err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1947 	if (ext_adv_capable(hdev))
1948 		return err;
1949 
1950 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1951 				     sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1952 }
1953 
hci_le_set_ext_scan_enable_sync(struct hci_dev * hdev,u8 val,u8 filter_dup)1954 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1955 					   u8 filter_dup)
1956 {
1957 	struct hci_cp_le_set_ext_scan_enable cp;
1958 
1959 	memset(&cp, 0, sizeof(cp));
1960 	cp.enable = val;
1961 
1962 	if (hci_dev_test_flag(hdev, HCI_MESH))
1963 		cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
1964 	else
1965 		cp.filter_dup = filter_dup;
1966 
1967 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1968 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1969 }
1970 
hci_le_set_scan_enable_sync(struct hci_dev * hdev,u8 val,u8 filter_dup)1971 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
1972 				       u8 filter_dup)
1973 {
1974 	struct hci_cp_le_set_scan_enable cp;
1975 
1976 	if (use_ext_scan(hdev))
1977 		return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
1978 
1979 	memset(&cp, 0, sizeof(cp));
1980 	cp.enable = val;
1981 
1982 	if (val && hci_dev_test_flag(hdev, HCI_MESH))
1983 		cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
1984 	else
1985 		cp.filter_dup = filter_dup;
1986 
1987 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
1988 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1989 }
1990 
hci_le_set_addr_resolution_enable_sync(struct hci_dev * hdev,u8 val)1991 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
1992 {
1993 	if (!use_ll_privacy(hdev))
1994 		return 0;
1995 
1996 	/* If controller is not/already resolving we are done. */
1997 	if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
1998 		return 0;
1999 
2000 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2001 				     sizeof(val), &val, HCI_CMD_TIMEOUT);
2002 }
2003 
hci_scan_disable_sync(struct hci_dev * hdev)2004 static int hci_scan_disable_sync(struct hci_dev *hdev)
2005 {
2006 	int err;
2007 
2008 	/* If controller is not scanning we are done. */
2009 	if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2010 		return 0;
2011 
2012 	if (hdev->scanning_paused) {
2013 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
2014 		return 0;
2015 	}
2016 
2017 	err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2018 	if (err) {
2019 		bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2020 		return err;
2021 	}
2022 
2023 	return err;
2024 }
2025 
scan_use_rpa(struct hci_dev * hdev)2026 static bool scan_use_rpa(struct hci_dev *hdev)
2027 {
2028 	return hci_dev_test_flag(hdev, HCI_PRIVACY);
2029 }
2030 
hci_start_interleave_scan(struct hci_dev * hdev)2031 static void hci_start_interleave_scan(struct hci_dev *hdev)
2032 {
2033 	hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2034 	queue_delayed_work(hdev->req_workqueue,
2035 			   &hdev->interleave_scan, 0);
2036 }
2037 
is_interleave_scanning(struct hci_dev * hdev)2038 static bool is_interleave_scanning(struct hci_dev *hdev)
2039 {
2040 	return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2041 }
2042 
cancel_interleave_scan(struct hci_dev * hdev)2043 static void cancel_interleave_scan(struct hci_dev *hdev)
2044 {
2045 	bt_dev_dbg(hdev, "cancelling interleave scan");
2046 
2047 	cancel_delayed_work_sync(&hdev->interleave_scan);
2048 
2049 	hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2050 }
2051 
2052 /* Return true if interleave_scan wasn't started until exiting this function,
2053  * otherwise, return false
2054  */
hci_update_interleaved_scan_sync(struct hci_dev * hdev)2055 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2056 {
2057 	/* Do interleaved scan only if all of the following are true:
2058 	 * - There is at least one ADV monitor
2059 	 * - At least one pending LE connection or one device to be scanned for
2060 	 * - Monitor offloading is not supported
2061 	 * If so, we should alternate between allowlist scan and one without
2062 	 * any filters to save power.
2063 	 */
2064 	bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2065 				!(list_empty(&hdev->pend_le_conns) &&
2066 				  list_empty(&hdev->pend_le_reports)) &&
2067 				hci_get_adv_monitor_offload_ext(hdev) ==
2068 				    HCI_ADV_MONITOR_EXT_NONE;
2069 	bool is_interleaving = is_interleave_scanning(hdev);
2070 
2071 	if (use_interleaving && !is_interleaving) {
2072 		hci_start_interleave_scan(hdev);
2073 		bt_dev_dbg(hdev, "starting interleave scan");
2074 		return true;
2075 	}
2076 
2077 	if (!use_interleaving && is_interleaving)
2078 		cancel_interleave_scan(hdev);
2079 
2080 	return false;
2081 }
2082 
2083 /* Removes connection to resolve list if needed.*/
hci_le_del_resolve_list_sync(struct hci_dev * hdev,bdaddr_t * bdaddr,u8 bdaddr_type)2084 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2085 					bdaddr_t *bdaddr, u8 bdaddr_type)
2086 {
2087 	struct hci_cp_le_del_from_resolv_list cp;
2088 	struct bdaddr_list_with_irk *entry;
2089 
2090 	if (!use_ll_privacy(hdev))
2091 		return 0;
2092 
2093 	/* Check if the IRK has been programmed */
2094 	entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2095 						bdaddr_type);
2096 	if (!entry)
2097 		return 0;
2098 
2099 	cp.bdaddr_type = bdaddr_type;
2100 	bacpy(&cp.bdaddr, bdaddr);
2101 
2102 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2103 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2104 }
2105 
hci_le_del_accept_list_sync(struct hci_dev * hdev,bdaddr_t * bdaddr,u8 bdaddr_type)2106 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2107 				       bdaddr_t *bdaddr, u8 bdaddr_type)
2108 {
2109 	struct hci_cp_le_del_from_accept_list cp;
2110 	int err;
2111 
2112 	/* Check if device is on accept list before removing it */
2113 	if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2114 		return 0;
2115 
2116 	cp.bdaddr_type = bdaddr_type;
2117 	bacpy(&cp.bdaddr, bdaddr);
2118 
2119 	/* Ignore errors when removing from resolving list as that is likely
2120 	 * that the device was never added.
2121 	 */
2122 	hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2123 
2124 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2125 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2126 	if (err) {
2127 		bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2128 		return err;
2129 	}
2130 
2131 	bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2132 		   cp.bdaddr_type);
2133 
2134 	return 0;
2135 }
2136 
2137 struct conn_params {
2138 	bdaddr_t addr;
2139 	u8 addr_type;
2140 	hci_conn_flags_t flags;
2141 	u8 privacy_mode;
2142 };
2143 
2144 /* Adds connection to resolve list if needed.
2145  * Setting params to NULL programs local hdev->irk
2146  */
hci_le_add_resolve_list_sync(struct hci_dev * hdev,struct conn_params * params)2147 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2148 					struct conn_params *params)
2149 {
2150 	struct hci_cp_le_add_to_resolv_list cp;
2151 	struct smp_irk *irk;
2152 	struct bdaddr_list_with_irk *entry;
2153 	struct hci_conn_params *p;
2154 
2155 	if (!use_ll_privacy(hdev))
2156 		return 0;
2157 
2158 	/* Attempt to program local identity address, type and irk if params is
2159 	 * NULL.
2160 	 */
2161 	if (!params) {
2162 		if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2163 			return 0;
2164 
2165 		hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2166 		memcpy(cp.peer_irk, hdev->irk, 16);
2167 		goto done;
2168 	}
2169 
2170 	irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
2171 	if (!irk)
2172 		return 0;
2173 
2174 	/* Check if the IK has _not_ been programmed yet. */
2175 	entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2176 						&params->addr,
2177 						params->addr_type);
2178 	if (entry)
2179 		return 0;
2180 
2181 	cp.bdaddr_type = params->addr_type;
2182 	bacpy(&cp.bdaddr, &params->addr);
2183 	memcpy(cp.peer_irk, irk->val, 16);
2184 
2185 	/* Default privacy mode is always Network */
2186 	params->privacy_mode = HCI_NETWORK_PRIVACY;
2187 
2188 	rcu_read_lock();
2189 	p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2190 				      &params->addr, params->addr_type);
2191 	if (!p)
2192 		p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2193 					      &params->addr, params->addr_type);
2194 	if (p)
2195 		WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY);
2196 	rcu_read_unlock();
2197 
2198 done:
2199 	if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2200 		memcpy(cp.local_irk, hdev->irk, 16);
2201 	else
2202 		memset(cp.local_irk, 0, 16);
2203 
2204 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2205 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2206 }
2207 
2208 /* Set Device Privacy Mode. */
hci_le_set_privacy_mode_sync(struct hci_dev * hdev,struct conn_params * params)2209 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2210 					struct conn_params *params)
2211 {
2212 	struct hci_cp_le_set_privacy_mode cp;
2213 	struct smp_irk *irk;
2214 
2215 	/* If device privacy mode has already been set there is nothing to do */
2216 	if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2217 		return 0;
2218 
2219 	/* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2220 	 * indicates that LL Privacy has been enabled and
2221 	 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2222 	 */
2223 	if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2224 		return 0;
2225 
2226 	irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
2227 	if (!irk)
2228 		return 0;
2229 
2230 	memset(&cp, 0, sizeof(cp));
2231 	cp.bdaddr_type = irk->addr_type;
2232 	bacpy(&cp.bdaddr, &irk->bdaddr);
2233 	cp.mode = HCI_DEVICE_PRIVACY;
2234 
2235 	/* Note: params->privacy_mode is not updated since it is a copy */
2236 
2237 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2238 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2239 }
2240 
2241 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2242  * this attempts to program the device in the resolving list as well and
2243  * properly set the privacy mode.
2244  */
hci_le_add_accept_list_sync(struct hci_dev * hdev,struct conn_params * params,u8 * num_entries)2245 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2246 				       struct conn_params *params,
2247 				       u8 *num_entries)
2248 {
2249 	struct hci_cp_le_add_to_accept_list cp;
2250 	int err;
2251 
2252 	/* During suspend, only wakeable devices can be in acceptlist */
2253 	if (hdev->suspended &&
2254 	    !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
2255 		return 0;
2256 
2257 	/* Select filter policy to accept all advertising */
2258 	if (*num_entries >= hdev->le_accept_list_size)
2259 		return -ENOSPC;
2260 
2261 	/* Accept list can not be used with RPAs */
2262 	if (!use_ll_privacy(hdev) &&
2263 	    hci_find_irk_by_addr(hdev, &params->addr, params->addr_type))
2264 		return -EINVAL;
2265 
2266 	/* Attempt to program the device in the resolving list first to avoid
2267 	 * having to rollback in case it fails since the resolving list is
2268 	 * dynamic it can probably be smaller than the accept list.
2269 	 */
2270 	err = hci_le_add_resolve_list_sync(hdev, params);
2271 	if (err) {
2272 		bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2273 		return err;
2274 	}
2275 
2276 	/* Set Privacy Mode */
2277 	err = hci_le_set_privacy_mode_sync(hdev, params);
2278 	if (err) {
2279 		bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2280 		return err;
2281 	}
2282 
2283 	/* Check if already in accept list */
2284 	if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
2285 				   params->addr_type))
2286 		return 0;
2287 
2288 	*num_entries += 1;
2289 	cp.bdaddr_type = params->addr_type;
2290 	bacpy(&cp.bdaddr, &params->addr);
2291 
2292 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2293 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2294 	if (err) {
2295 		bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2296 		/* Rollback the device from the resolving list */
2297 		hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2298 		return err;
2299 	}
2300 
2301 	bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2302 		   cp.bdaddr_type);
2303 
2304 	return 0;
2305 }
2306 
2307 /* This function disables/pause all advertising instances */
hci_pause_advertising_sync(struct hci_dev * hdev)2308 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2309 {
2310 	int err;
2311 	int old_state;
2312 
2313 	/* If already been paused there is nothing to do. */
2314 	if (hdev->advertising_paused)
2315 		return 0;
2316 
2317 	bt_dev_dbg(hdev, "Pausing directed advertising");
2318 
2319 	/* Stop directed advertising */
2320 	old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2321 	if (old_state) {
2322 		/* When discoverable timeout triggers, then just make sure
2323 		 * the limited discoverable flag is cleared. Even in the case
2324 		 * of a timeout triggered from general discoverable, it is
2325 		 * safe to unconditionally clear the flag.
2326 		 */
2327 		hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2328 		hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2329 		hdev->discov_timeout = 0;
2330 	}
2331 
2332 	bt_dev_dbg(hdev, "Pausing advertising instances");
2333 
2334 	/* Call to disable any advertisements active on the controller.
2335 	 * This will succeed even if no advertisements are configured.
2336 	 */
2337 	err = hci_disable_advertising_sync(hdev);
2338 	if (err)
2339 		return err;
2340 
2341 	/* If we are using software rotation, pause the loop */
2342 	if (!ext_adv_capable(hdev))
2343 		cancel_adv_timeout(hdev);
2344 
2345 	hdev->advertising_paused = true;
2346 	hdev->advertising_old_state = old_state;
2347 
2348 	return 0;
2349 }
2350 
2351 /* This function enables all user advertising instances */
hci_resume_advertising_sync(struct hci_dev * hdev)2352 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2353 {
2354 	struct adv_info *adv, *tmp;
2355 	int err;
2356 
2357 	/* If advertising has not been paused there is nothing  to do. */
2358 	if (!hdev->advertising_paused)
2359 		return 0;
2360 
2361 	/* Resume directed advertising */
2362 	hdev->advertising_paused = false;
2363 	if (hdev->advertising_old_state) {
2364 		hci_dev_set_flag(hdev, HCI_ADVERTISING);
2365 		hdev->advertising_old_state = 0;
2366 	}
2367 
2368 	bt_dev_dbg(hdev, "Resuming advertising instances");
2369 
2370 	if (ext_adv_capable(hdev)) {
2371 		/* Call for each tracked instance to be re-enabled */
2372 		list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2373 			err = hci_enable_ext_advertising_sync(hdev,
2374 							      adv->instance);
2375 			if (!err)
2376 				continue;
2377 
2378 			/* If the instance cannot be resumed remove it */
2379 			hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2380 							 NULL);
2381 		}
2382 	} else {
2383 		/* Schedule for most recent instance to be restarted and begin
2384 		 * the software rotation loop
2385 		 */
2386 		err = hci_schedule_adv_instance_sync(hdev,
2387 						     hdev->cur_adv_instance,
2388 						     true);
2389 	}
2390 
2391 	hdev->advertising_paused = false;
2392 
2393 	return err;
2394 }
2395 
hci_pause_addr_resolution(struct hci_dev * hdev)2396 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2397 {
2398 	int err;
2399 
2400 	if (!use_ll_privacy(hdev))
2401 		return 0;
2402 
2403 	if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2404 		return 0;
2405 
2406 	/* Cannot disable addr resolution if scanning is enabled or
2407 	 * when initiating an LE connection.
2408 	 */
2409 	if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2410 	    hci_lookup_le_connect(hdev)) {
2411 		bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2412 		return -EPERM;
2413 	}
2414 
2415 	/* Cannot disable addr resolution if advertising is enabled. */
2416 	err = hci_pause_advertising_sync(hdev);
2417 	if (err) {
2418 		bt_dev_err(hdev, "Pause advertising failed: %d", err);
2419 		return err;
2420 	}
2421 
2422 	err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2423 	if (err)
2424 		bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2425 			   err);
2426 
2427 	/* Return if address resolution is disabled and RPA is not used. */
2428 	if (!err && scan_use_rpa(hdev))
2429 		return err;
2430 
2431 	hci_resume_advertising_sync(hdev);
2432 	return err;
2433 }
2434 
hci_read_local_oob_data_sync(struct hci_dev * hdev,bool extended,struct sock * sk)2435 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2436 					     bool extended, struct sock *sk)
2437 {
2438 	u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2439 					HCI_OP_READ_LOCAL_OOB_DATA;
2440 
2441 	return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2442 }
2443 
conn_params_copy(struct list_head * list,size_t * n)2444 static struct conn_params *conn_params_copy(struct list_head *list, size_t *n)
2445 {
2446 	struct hci_conn_params *params;
2447 	struct conn_params *p;
2448 	size_t i;
2449 
2450 	rcu_read_lock();
2451 
2452 	i = 0;
2453 	list_for_each_entry_rcu(params, list, action)
2454 		++i;
2455 	*n = i;
2456 
2457 	rcu_read_unlock();
2458 
2459 	p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL);
2460 	if (!p)
2461 		return NULL;
2462 
2463 	rcu_read_lock();
2464 
2465 	i = 0;
2466 	list_for_each_entry_rcu(params, list, action) {
2467 		/* Racing adds are handled in next scan update */
2468 		if (i >= *n)
2469 			break;
2470 
2471 		/* No hdev->lock, but: addr, addr_type are immutable.
2472 		 * privacy_mode is only written by us or in
2473 		 * hci_cc_le_set_privacy_mode that we wait for.
2474 		 * We should be idempotent so MGMT updating flags
2475 		 * while we are processing is OK.
2476 		 */
2477 		bacpy(&p[i].addr, &params->addr);
2478 		p[i].addr_type = params->addr_type;
2479 		p[i].flags = READ_ONCE(params->flags);
2480 		p[i].privacy_mode = READ_ONCE(params->privacy_mode);
2481 		++i;
2482 	}
2483 
2484 	rcu_read_unlock();
2485 
2486 	*n = i;
2487 	return p;
2488 }
2489 
2490 /* Device must not be scanning when updating the accept list.
2491  *
2492  * Update is done using the following sequence:
2493  *
2494  * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
2495  * Remove Devices From Accept List ->
2496  * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
2497  * Add Devices to Accept List ->
2498  * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
2499  * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
2500  * Enable Scanning
2501  *
2502  * In case of failure advertising shall be restored to its original state and
2503  * return would disable accept list since either accept or resolving list could
2504  * not be programmed.
2505  *
2506  */
hci_update_accept_list_sync(struct hci_dev * hdev)2507 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2508 {
2509 	struct conn_params *params;
2510 	struct bdaddr_list *b, *t;
2511 	u8 num_entries = 0;
2512 	bool pend_conn, pend_report;
2513 	u8 filter_policy;
2514 	size_t i, n;
2515 	int err;
2516 
2517 	/* Pause advertising if resolving list can be used as controllers
2518 	 * cannot accept resolving list modifications while advertising.
2519 	 */
2520 	if (use_ll_privacy(hdev)) {
2521 		err = hci_pause_advertising_sync(hdev);
2522 		if (err) {
2523 			bt_dev_err(hdev, "pause advertising failed: %d", err);
2524 			return 0x00;
2525 		}
2526 	}
2527 
2528 	/* Disable address resolution while reprogramming accept list since
2529 	 * devices that do have an IRK will be programmed in the resolving list
2530 	 * when LL Privacy is enabled.
2531 	 */
2532 	err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2533 	if (err) {
2534 		bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2535 		goto done;
2536 	}
2537 
2538 	/* Go through the current accept list programmed into the
2539 	 * controller one by one and check if that address is connected or is
2540 	 * still in the list of pending connections or list of devices to
2541 	 * report. If not present in either list, then remove it from
2542 	 * the controller.
2543 	 */
2544 	list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2545 		if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2546 			continue;
2547 
2548 		/* Pointers not dereferenced, no locks needed */
2549 		pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2550 						      &b->bdaddr,
2551 						      b->bdaddr_type);
2552 		pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2553 							&b->bdaddr,
2554 							b->bdaddr_type);
2555 
2556 		/* If the device is not likely to connect or report,
2557 		 * remove it from the acceptlist.
2558 		 */
2559 		if (!pend_conn && !pend_report) {
2560 			hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2561 						    b->bdaddr_type);
2562 			continue;
2563 		}
2564 
2565 		num_entries++;
2566 	}
2567 
2568 	/* Since all no longer valid accept list entries have been
2569 	 * removed, walk through the list of pending connections
2570 	 * and ensure that any new device gets programmed into
2571 	 * the controller.
2572 	 *
2573 	 * If the list of the devices is larger than the list of
2574 	 * available accept list entries in the controller, then
2575 	 * just abort and return filer policy value to not use the
2576 	 * accept list.
2577 	 *
2578 	 * The list and params may be mutated while we wait for events,
2579 	 * so make a copy and iterate it.
2580 	 */
2581 
2582 	params = conn_params_copy(&hdev->pend_le_conns, &n);
2583 	if (!params) {
2584 		err = -ENOMEM;
2585 		goto done;
2586 	}
2587 
2588 	for (i = 0; i < n; ++i) {
2589 		err = hci_le_add_accept_list_sync(hdev, &params[i],
2590 						  &num_entries);
2591 		if (err) {
2592 			kvfree(params);
2593 			goto done;
2594 		}
2595 	}
2596 
2597 	kvfree(params);
2598 
2599 	/* After adding all new pending connections, walk through
2600 	 * the list of pending reports and also add these to the
2601 	 * accept list if there is still space. Abort if space runs out.
2602 	 */
2603 
2604 	params = conn_params_copy(&hdev->pend_le_reports, &n);
2605 	if (!params) {
2606 		err = -ENOMEM;
2607 		goto done;
2608 	}
2609 
2610 	for (i = 0; i < n; ++i) {
2611 		err = hci_le_add_accept_list_sync(hdev, &params[i],
2612 						  &num_entries);
2613 		if (err) {
2614 			kvfree(params);
2615 			goto done;
2616 		}
2617 	}
2618 
2619 	kvfree(params);
2620 
2621 	/* Use the allowlist unless the following conditions are all true:
2622 	 * - We are not currently suspending
2623 	 * - There are 1 or more ADV monitors registered and it's not offloaded
2624 	 * - Interleaved scanning is not currently using the allowlist
2625 	 */
2626 	if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2627 	    hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2628 	    hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2629 		err = -EINVAL;
2630 
2631 done:
2632 	filter_policy = err ? 0x00 : 0x01;
2633 
2634 	/* Enable address resolution when LL Privacy is enabled. */
2635 	err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2636 	if (err)
2637 		bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2638 
2639 	/* Resume advertising if it was paused */
2640 	if (use_ll_privacy(hdev))
2641 		hci_resume_advertising_sync(hdev);
2642 
2643 	/* Select filter policy to use accept list */
2644 	return filter_policy;
2645 }
2646 
2647 /* Returns true if an le connection is in the scanning state */
hci_is_le_conn_scanning(struct hci_dev * hdev)2648 static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
2649 {
2650 	struct hci_conn_hash *h = &hdev->conn_hash;
2651 	struct hci_conn  *c;
2652 
2653 	rcu_read_lock();
2654 
2655 	list_for_each_entry_rcu(c, &h->list, list) {
2656 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
2657 		    test_bit(HCI_CONN_SCANNING, &c->flags)) {
2658 			rcu_read_unlock();
2659 			return true;
2660 		}
2661 	}
2662 
2663 	rcu_read_unlock();
2664 
2665 	return false;
2666 }
2667 
hci_le_set_ext_scan_param_sync(struct hci_dev * hdev,u8 type,u16 interval,u16 window,u8 own_addr_type,u8 filter_policy)2668 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2669 					  u16 interval, u16 window,
2670 					  u8 own_addr_type, u8 filter_policy)
2671 {
2672 	struct hci_cp_le_set_ext_scan_params *cp;
2673 	struct hci_cp_le_scan_phy_params *phy;
2674 	u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2675 	u8 num_phy = 0;
2676 
2677 	cp = (void *)data;
2678 	phy = (void *)cp->data;
2679 
2680 	memset(data, 0, sizeof(data));
2681 
2682 	cp->own_addr_type = own_addr_type;
2683 	cp->filter_policy = filter_policy;
2684 
2685 	if (scan_1m(hdev) || scan_2m(hdev)) {
2686 		cp->scanning_phys |= LE_SCAN_PHY_1M;
2687 
2688 		phy->type = type;
2689 		phy->interval = cpu_to_le16(interval);
2690 		phy->window = cpu_to_le16(window);
2691 
2692 		num_phy++;
2693 		phy++;
2694 	}
2695 
2696 	if (scan_coded(hdev)) {
2697 		cp->scanning_phys |= LE_SCAN_PHY_CODED;
2698 
2699 		phy->type = type;
2700 		phy->interval = cpu_to_le16(interval);
2701 		phy->window = cpu_to_le16(window);
2702 
2703 		num_phy++;
2704 		phy++;
2705 	}
2706 
2707 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2708 				     sizeof(*cp) + sizeof(*phy) * num_phy,
2709 				     data, HCI_CMD_TIMEOUT);
2710 }
2711 
hci_le_set_scan_param_sync(struct hci_dev * hdev,u8 type,u16 interval,u16 window,u8 own_addr_type,u8 filter_policy)2712 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2713 				      u16 interval, u16 window,
2714 				      u8 own_addr_type, u8 filter_policy)
2715 {
2716 	struct hci_cp_le_set_scan_param cp;
2717 
2718 	if (use_ext_scan(hdev))
2719 		return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2720 						      window, own_addr_type,
2721 						      filter_policy);
2722 
2723 	memset(&cp, 0, sizeof(cp));
2724 	cp.type = type;
2725 	cp.interval = cpu_to_le16(interval);
2726 	cp.window = cpu_to_le16(window);
2727 	cp.own_address_type = own_addr_type;
2728 	cp.filter_policy = filter_policy;
2729 
2730 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2731 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2732 }
2733 
hci_start_scan_sync(struct hci_dev * hdev,u8 type,u16 interval,u16 window,u8 own_addr_type,u8 filter_policy,u8 filter_dup)2734 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2735 			       u16 window, u8 own_addr_type, u8 filter_policy,
2736 			       u8 filter_dup)
2737 {
2738 	int err;
2739 
2740 	if (hdev->scanning_paused) {
2741 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
2742 		return 0;
2743 	}
2744 
2745 	err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2746 					 own_addr_type, filter_policy);
2747 	if (err)
2748 		return err;
2749 
2750 	return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2751 }
2752 
hci_passive_scan_sync(struct hci_dev * hdev)2753 static int hci_passive_scan_sync(struct hci_dev *hdev)
2754 {
2755 	u8 own_addr_type;
2756 	u8 filter_policy;
2757 	u16 window, interval;
2758 	u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2759 	int err;
2760 
2761 	if (hdev->scanning_paused) {
2762 		bt_dev_dbg(hdev, "Scanning is paused for suspend");
2763 		return 0;
2764 	}
2765 
2766 	err = hci_scan_disable_sync(hdev);
2767 	if (err) {
2768 		bt_dev_err(hdev, "disable scanning failed: %d", err);
2769 		return err;
2770 	}
2771 
2772 	/* Set require_privacy to false since no SCAN_REQ are send
2773 	 * during passive scanning. Not using an non-resolvable address
2774 	 * here is important so that peer devices using direct
2775 	 * advertising with our address will be correctly reported
2776 	 * by the controller.
2777 	 */
2778 	if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2779 					   &own_addr_type))
2780 		return 0;
2781 
2782 	if (hdev->enable_advmon_interleave_scan &&
2783 	    hci_update_interleaved_scan_sync(hdev))
2784 		return 0;
2785 
2786 	bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2787 
2788 	/* Adding or removing entries from the accept list must
2789 	 * happen before enabling scanning. The controller does
2790 	 * not allow accept list modification while scanning.
2791 	 */
2792 	filter_policy = hci_update_accept_list_sync(hdev);
2793 
2794 	/* When the controller is using random resolvable addresses and
2795 	 * with that having LE privacy enabled, then controllers with
2796 	 * Extended Scanner Filter Policies support can now enable support
2797 	 * for handling directed advertising.
2798 	 *
2799 	 * So instead of using filter polices 0x00 (no acceptlist)
2800 	 * and 0x01 (acceptlist enabled) use the new filter policies
2801 	 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2802 	 */
2803 	if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2804 	    (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2805 		filter_policy |= 0x02;
2806 
2807 	if (hdev->suspended) {
2808 		window = hdev->le_scan_window_suspend;
2809 		interval = hdev->le_scan_int_suspend;
2810 	} else if (hci_is_le_conn_scanning(hdev)) {
2811 		window = hdev->le_scan_window_connect;
2812 		interval = hdev->le_scan_int_connect;
2813 	} else if (hci_is_adv_monitoring(hdev)) {
2814 		window = hdev->le_scan_window_adv_monitor;
2815 		interval = hdev->le_scan_int_adv_monitor;
2816 	} else {
2817 		window = hdev->le_scan_window;
2818 		interval = hdev->le_scan_interval;
2819 	}
2820 
2821 	/* Disable all filtering for Mesh */
2822 	if (hci_dev_test_flag(hdev, HCI_MESH)) {
2823 		filter_policy = 0;
2824 		filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
2825 	}
2826 
2827 	bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2828 
2829 	return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2830 				   own_addr_type, filter_policy, filter_dups);
2831 }
2832 
2833 /* This function controls the passive scanning based on hdev->pend_le_conns
2834  * list. If there are pending LE connection we start the background scanning,
2835  * otherwise we stop it in the following sequence:
2836  *
2837  * If there are devices to scan:
2838  *
2839  * Disable Scanning -> Update Accept List ->
2840  * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2841  * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2842  * Enable Scanning
2843  *
2844  * Otherwise:
2845  *
2846  * Disable Scanning
2847  */
hci_update_passive_scan_sync(struct hci_dev * hdev)2848 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2849 {
2850 	int err;
2851 
2852 	if (!test_bit(HCI_UP, &hdev->flags) ||
2853 	    test_bit(HCI_INIT, &hdev->flags) ||
2854 	    hci_dev_test_flag(hdev, HCI_SETUP) ||
2855 	    hci_dev_test_flag(hdev, HCI_CONFIG) ||
2856 	    hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2857 	    hci_dev_test_flag(hdev, HCI_UNREGISTER))
2858 		return 0;
2859 
2860 	/* No point in doing scanning if LE support hasn't been enabled */
2861 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2862 		return 0;
2863 
2864 	/* If discovery is active don't interfere with it */
2865 	if (hdev->discovery.state != DISCOVERY_STOPPED)
2866 		return 0;
2867 
2868 	/* Reset RSSI and UUID filters when starting background scanning
2869 	 * since these filters are meant for service discovery only.
2870 	 *
2871 	 * The Start Discovery and Start Service Discovery operations
2872 	 * ensure to set proper values for RSSI threshold and UUID
2873 	 * filter list. So it is safe to just reset them here.
2874 	 */
2875 	hci_discovery_filter_clear(hdev);
2876 
2877 	bt_dev_dbg(hdev, "ADV monitoring is %s",
2878 		   hci_is_adv_monitoring(hdev) ? "on" : "off");
2879 
2880 	if (!hci_dev_test_flag(hdev, HCI_MESH) &&
2881 	    list_empty(&hdev->pend_le_conns) &&
2882 	    list_empty(&hdev->pend_le_reports) &&
2883 	    !hci_is_adv_monitoring(hdev) &&
2884 	    !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2885 		/* If there is no pending LE connections or devices
2886 		 * to be scanned for or no ADV monitors, we should stop the
2887 		 * background scanning.
2888 		 */
2889 
2890 		bt_dev_dbg(hdev, "stopping background scanning");
2891 
2892 		err = hci_scan_disable_sync(hdev);
2893 		if (err)
2894 			bt_dev_err(hdev, "stop background scanning failed: %d",
2895 				   err);
2896 	} else {
2897 		/* If there is at least one pending LE connection, we should
2898 		 * keep the background scan running.
2899 		 */
2900 
2901 		/* If controller is connecting, we should not start scanning
2902 		 * since some controllers are not able to scan and connect at
2903 		 * the same time.
2904 		 */
2905 		if (hci_lookup_le_connect(hdev))
2906 			return 0;
2907 
2908 		bt_dev_dbg(hdev, "start background scanning");
2909 
2910 		err = hci_passive_scan_sync(hdev);
2911 		if (err)
2912 			bt_dev_err(hdev, "start background scanning failed: %d",
2913 				   err);
2914 	}
2915 
2916 	return err;
2917 }
2918 
update_scan_sync(struct hci_dev * hdev,void * data)2919 static int update_scan_sync(struct hci_dev *hdev, void *data)
2920 {
2921 	return hci_update_scan_sync(hdev);
2922 }
2923 
hci_update_scan(struct hci_dev * hdev)2924 int hci_update_scan(struct hci_dev *hdev)
2925 {
2926 	return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
2927 }
2928 
update_passive_scan_sync(struct hci_dev * hdev,void * data)2929 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2930 {
2931 	return hci_update_passive_scan_sync(hdev);
2932 }
2933 
hci_update_passive_scan(struct hci_dev * hdev)2934 int hci_update_passive_scan(struct hci_dev *hdev)
2935 {
2936 	/* Only queue if it would have any effect */
2937 	if (!test_bit(HCI_UP, &hdev->flags) ||
2938 	    test_bit(HCI_INIT, &hdev->flags) ||
2939 	    hci_dev_test_flag(hdev, HCI_SETUP) ||
2940 	    hci_dev_test_flag(hdev, HCI_CONFIG) ||
2941 	    hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2942 	    hci_dev_test_flag(hdev, HCI_UNREGISTER))
2943 		return 0;
2944 
2945 	return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2946 }
2947 
hci_write_sc_support_sync(struct hci_dev * hdev,u8 val)2948 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2949 {
2950 	int err;
2951 
2952 	if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2953 		return 0;
2954 
2955 	err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2956 				    sizeof(val), &val, HCI_CMD_TIMEOUT);
2957 
2958 	if (!err) {
2959 		if (val) {
2960 			hdev->features[1][0] |= LMP_HOST_SC;
2961 			hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2962 		} else {
2963 			hdev->features[1][0] &= ~LMP_HOST_SC;
2964 			hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2965 		}
2966 	}
2967 
2968 	return err;
2969 }
2970 
hci_write_ssp_mode_sync(struct hci_dev * hdev,u8 mode)2971 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2972 {
2973 	int err;
2974 
2975 	if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2976 	    lmp_host_ssp_capable(hdev))
2977 		return 0;
2978 
2979 	if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
2980 		__hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2981 				      sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2982 	}
2983 
2984 	err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2985 				    sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2986 	if (err)
2987 		return err;
2988 
2989 	return hci_write_sc_support_sync(hdev, 0x01);
2990 }
2991 
hci_write_le_host_supported_sync(struct hci_dev * hdev,u8 le,u8 simul)2992 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
2993 {
2994 	struct hci_cp_write_le_host_supported cp;
2995 
2996 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
2997 	    !lmp_bredr_capable(hdev))
2998 		return 0;
2999 
3000 	/* Check first if we already have the right host state
3001 	 * (host features set)
3002 	 */
3003 	if (le == lmp_host_le_capable(hdev) &&
3004 	    simul == lmp_host_le_br_capable(hdev))
3005 		return 0;
3006 
3007 	memset(&cp, 0, sizeof(cp));
3008 
3009 	cp.le = le;
3010 	cp.simul = simul;
3011 
3012 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3013 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3014 }
3015 
hci_powered_update_adv_sync(struct hci_dev * hdev)3016 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3017 {
3018 	struct adv_info *adv, *tmp;
3019 	int err;
3020 
3021 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3022 		return 0;
3023 
3024 	/* If RPA Resolution has not been enable yet it means the
3025 	 * resolving list is empty and we should attempt to program the
3026 	 * local IRK in order to support using own_addr_type
3027 	 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
3028 	 */
3029 	if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3030 		hci_le_add_resolve_list_sync(hdev, NULL);
3031 		hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3032 	}
3033 
3034 	/* Make sure the controller has a good default for
3035 	 * advertising data. This also applies to the case
3036 	 * where BR/EDR was toggled during the AUTO_OFF phase.
3037 	 */
3038 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3039 	    list_empty(&hdev->adv_instances)) {
3040 		if (ext_adv_capable(hdev)) {
3041 			err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3042 			if (!err)
3043 				hci_update_scan_rsp_data_sync(hdev, 0x00);
3044 		} else {
3045 			err = hci_update_adv_data_sync(hdev, 0x00);
3046 			if (!err)
3047 				hci_update_scan_rsp_data_sync(hdev, 0x00);
3048 		}
3049 
3050 		if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3051 			hci_enable_advertising_sync(hdev);
3052 	}
3053 
3054 	/* Call for each tracked instance to be scheduled */
3055 	list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3056 		hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3057 
3058 	return 0;
3059 }
3060 
hci_write_auth_enable_sync(struct hci_dev * hdev)3061 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3062 {
3063 	u8 link_sec;
3064 
3065 	link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3066 	if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3067 		return 0;
3068 
3069 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3070 				     sizeof(link_sec), &link_sec,
3071 				     HCI_CMD_TIMEOUT);
3072 }
3073 
hci_write_fast_connectable_sync(struct hci_dev * hdev,bool enable)3074 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3075 {
3076 	struct hci_cp_write_page_scan_activity cp;
3077 	u8 type;
3078 	int err = 0;
3079 
3080 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3081 		return 0;
3082 
3083 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3084 		return 0;
3085 
3086 	memset(&cp, 0, sizeof(cp));
3087 
3088 	if (enable) {
3089 		type = PAGE_SCAN_TYPE_INTERLACED;
3090 
3091 		/* 160 msec page scan interval */
3092 		cp.interval = cpu_to_le16(0x0100);
3093 	} else {
3094 		type = hdev->def_page_scan_type;
3095 		cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3096 	}
3097 
3098 	cp.window = cpu_to_le16(hdev->def_page_scan_window);
3099 
3100 	if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3101 	    __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3102 		err = __hci_cmd_sync_status(hdev,
3103 					    HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
3104 					    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3105 		if (err)
3106 			return err;
3107 	}
3108 
3109 	if (hdev->page_scan_type != type)
3110 		err = __hci_cmd_sync_status(hdev,
3111 					    HCI_OP_WRITE_PAGE_SCAN_TYPE,
3112 					    sizeof(type), &type,
3113 					    HCI_CMD_TIMEOUT);
3114 
3115 	return err;
3116 }
3117 
disconnected_accept_list_entries(struct hci_dev * hdev)3118 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3119 {
3120 	struct bdaddr_list *b;
3121 
3122 	list_for_each_entry(b, &hdev->accept_list, list) {
3123 		struct hci_conn *conn;
3124 
3125 		conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3126 		if (!conn)
3127 			return true;
3128 
3129 		if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3130 			return true;
3131 	}
3132 
3133 	return false;
3134 }
3135 
hci_write_scan_enable_sync(struct hci_dev * hdev,u8 val)3136 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3137 {
3138 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3139 					    sizeof(val), &val,
3140 					    HCI_CMD_TIMEOUT);
3141 }
3142 
hci_update_scan_sync(struct hci_dev * hdev)3143 int hci_update_scan_sync(struct hci_dev *hdev)
3144 {
3145 	u8 scan;
3146 
3147 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3148 		return 0;
3149 
3150 	if (!hdev_is_powered(hdev))
3151 		return 0;
3152 
3153 	if (mgmt_powering_down(hdev))
3154 		return 0;
3155 
3156 	if (hdev->scanning_paused)
3157 		return 0;
3158 
3159 	if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3160 	    disconnected_accept_list_entries(hdev))
3161 		scan = SCAN_PAGE;
3162 	else
3163 		scan = SCAN_DISABLED;
3164 
3165 	if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3166 		scan |= SCAN_INQUIRY;
3167 
3168 	if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3169 	    test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3170 		return 0;
3171 
3172 	return hci_write_scan_enable_sync(hdev, scan);
3173 }
3174 
hci_update_name_sync(struct hci_dev * hdev)3175 int hci_update_name_sync(struct hci_dev *hdev)
3176 {
3177 	struct hci_cp_write_local_name cp;
3178 
3179 	memset(&cp, 0, sizeof(cp));
3180 
3181 	memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3182 
3183 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3184 					    sizeof(cp), &cp,
3185 					    HCI_CMD_TIMEOUT);
3186 }
3187 
3188 /* This function perform powered update HCI command sequence after the HCI init
3189  * sequence which end up resetting all states, the sequence is as follows:
3190  *
3191  * HCI_SSP_ENABLED(Enable SSP)
3192  * HCI_LE_ENABLED(Enable LE)
3193  * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
3194  * Update adv data)
3195  * Enable Authentication
3196  * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3197  * Set Name -> Set EIR)
3198  */
hci_powered_update_sync(struct hci_dev * hdev)3199 int hci_powered_update_sync(struct hci_dev *hdev)
3200 {
3201 	int err;
3202 
3203 	/* Register the available SMP channels (BR/EDR and LE) only when
3204 	 * successfully powering on the controller. This late
3205 	 * registration is required so that LE SMP can clearly decide if
3206 	 * the public address or static address is used.
3207 	 */
3208 	smp_register(hdev);
3209 
3210 	err = hci_write_ssp_mode_sync(hdev, 0x01);
3211 	if (err)
3212 		return err;
3213 
3214 	err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3215 	if (err)
3216 		return err;
3217 
3218 	err = hci_powered_update_adv_sync(hdev);
3219 	if (err)
3220 		return err;
3221 
3222 	err = hci_write_auth_enable_sync(hdev);
3223 	if (err)
3224 		return err;
3225 
3226 	if (lmp_bredr_capable(hdev)) {
3227 		if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3228 			hci_write_fast_connectable_sync(hdev, true);
3229 		else
3230 			hci_write_fast_connectable_sync(hdev, false);
3231 		hci_update_scan_sync(hdev);
3232 		hci_update_class_sync(hdev);
3233 		hci_update_name_sync(hdev);
3234 		hci_update_eir_sync(hdev);
3235 	}
3236 
3237 	return 0;
3238 }
3239 
3240 /**
3241  * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3242  *				       (BD_ADDR) for a HCI device from
3243  *				       a firmware node property.
3244  * @hdev:	The HCI device
3245  *
3246  * Search the firmware node for 'local-bd-address'.
3247  *
3248  * All-zero BD addresses are rejected, because those could be properties
3249  * that exist in the firmware tables, but were not updated by the firmware. For
3250  * example, the DTS could define 'local-bd-address', with zero BD addresses.
3251  */
hci_dev_get_bd_addr_from_property(struct hci_dev * hdev)3252 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3253 {
3254 	struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3255 	bdaddr_t ba;
3256 	int ret;
3257 
3258 	ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3259 					    (u8 *)&ba, sizeof(ba));
3260 	if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3261 		return;
3262 
3263 	bacpy(&hdev->public_addr, &ba);
3264 }
3265 
3266 struct hci_init_stage {
3267 	int (*func)(struct hci_dev *hdev);
3268 };
3269 
3270 /* Run init stage NULL terminated function table */
hci_init_stage_sync(struct hci_dev * hdev,const struct hci_init_stage * stage)3271 static int hci_init_stage_sync(struct hci_dev *hdev,
3272 			       const struct hci_init_stage *stage)
3273 {
3274 	size_t i;
3275 
3276 	for (i = 0; stage[i].func; i++) {
3277 		int err;
3278 
3279 		err = stage[i].func(hdev);
3280 		if (err)
3281 			return err;
3282 	}
3283 
3284 	return 0;
3285 }
3286 
3287 /* Read Local Version */
hci_read_local_version_sync(struct hci_dev * hdev)3288 static int hci_read_local_version_sync(struct hci_dev *hdev)
3289 {
3290 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3291 				     0, NULL, HCI_CMD_TIMEOUT);
3292 }
3293 
3294 /* Read BD Address */
hci_read_bd_addr_sync(struct hci_dev * hdev)3295 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3296 {
3297 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3298 				     0, NULL, HCI_CMD_TIMEOUT);
3299 }
3300 
3301 #define HCI_INIT(_func) \
3302 { \
3303 	.func = _func, \
3304 }
3305 
3306 static const struct hci_init_stage hci_init0[] = {
3307 	/* HCI_OP_READ_LOCAL_VERSION */
3308 	HCI_INIT(hci_read_local_version_sync),
3309 	/* HCI_OP_READ_BD_ADDR */
3310 	HCI_INIT(hci_read_bd_addr_sync),
3311 	{}
3312 };
3313 
hci_reset_sync(struct hci_dev * hdev)3314 int hci_reset_sync(struct hci_dev *hdev)
3315 {
3316 	int err;
3317 
3318 	set_bit(HCI_RESET, &hdev->flags);
3319 
3320 	err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3321 				    HCI_CMD_TIMEOUT);
3322 	if (err)
3323 		return err;
3324 
3325 	return 0;
3326 }
3327 
hci_init0_sync(struct hci_dev * hdev)3328 static int hci_init0_sync(struct hci_dev *hdev)
3329 {
3330 	int err;
3331 
3332 	bt_dev_dbg(hdev, "");
3333 
3334 	/* Reset */
3335 	if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3336 		err = hci_reset_sync(hdev);
3337 		if (err)
3338 			return err;
3339 	}
3340 
3341 	return hci_init_stage_sync(hdev, hci_init0);
3342 }
3343 
hci_unconf_init_sync(struct hci_dev * hdev)3344 static int hci_unconf_init_sync(struct hci_dev *hdev)
3345 {
3346 	int err;
3347 
3348 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3349 		return 0;
3350 
3351 	err = hci_init0_sync(hdev);
3352 	if (err < 0)
3353 		return err;
3354 
3355 	if (hci_dev_test_flag(hdev, HCI_SETUP))
3356 		hci_debugfs_create_basic(hdev);
3357 
3358 	return 0;
3359 }
3360 
3361 /* Read Local Supported Features. */
hci_read_local_features_sync(struct hci_dev * hdev)3362 static int hci_read_local_features_sync(struct hci_dev *hdev)
3363 {
3364 	 /* Not all AMP controllers support this command */
3365 	if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
3366 		return 0;
3367 
3368 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3369 				     0, NULL, HCI_CMD_TIMEOUT);
3370 }
3371 
3372 /* BR Controller init stage 1 command sequence */
3373 static const struct hci_init_stage br_init1[] = {
3374 	/* HCI_OP_READ_LOCAL_FEATURES */
3375 	HCI_INIT(hci_read_local_features_sync),
3376 	/* HCI_OP_READ_LOCAL_VERSION */
3377 	HCI_INIT(hci_read_local_version_sync),
3378 	/* HCI_OP_READ_BD_ADDR */
3379 	HCI_INIT(hci_read_bd_addr_sync),
3380 	{}
3381 };
3382 
3383 /* Read Local Commands */
hci_read_local_cmds_sync(struct hci_dev * hdev)3384 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3385 {
3386 	/* All Bluetooth 1.2 and later controllers should support the
3387 	 * HCI command for reading the local supported commands.
3388 	 *
3389 	 * Unfortunately some controllers indicate Bluetooth 1.2 support,
3390 	 * but do not have support for this command. If that is the case,
3391 	 * the driver can quirk the behavior and skip reading the local
3392 	 * supported commands.
3393 	 */
3394 	if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3395 	    !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3396 		return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3397 					     0, NULL, HCI_CMD_TIMEOUT);
3398 
3399 	return 0;
3400 }
3401 
3402 /* Read Local AMP Info */
hci_read_local_amp_info_sync(struct hci_dev * hdev)3403 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
3404 {
3405 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
3406 				     0, NULL, HCI_CMD_TIMEOUT);
3407 }
3408 
3409 /* Read Data Blk size */
hci_read_data_block_size_sync(struct hci_dev * hdev)3410 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
3411 {
3412 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
3413 				     0, NULL, HCI_CMD_TIMEOUT);
3414 }
3415 
3416 /* Read Flow Control Mode */
hci_read_flow_control_mode_sync(struct hci_dev * hdev)3417 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
3418 {
3419 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
3420 				     0, NULL, HCI_CMD_TIMEOUT);
3421 }
3422 
3423 /* Read Location Data */
hci_read_location_data_sync(struct hci_dev * hdev)3424 static int hci_read_location_data_sync(struct hci_dev *hdev)
3425 {
3426 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
3427 				     0, NULL, HCI_CMD_TIMEOUT);
3428 }
3429 
3430 /* AMP Controller init stage 1 command sequence */
3431 static const struct hci_init_stage amp_init1[] = {
3432 	/* HCI_OP_READ_LOCAL_VERSION */
3433 	HCI_INIT(hci_read_local_version_sync),
3434 	/* HCI_OP_READ_LOCAL_COMMANDS */
3435 	HCI_INIT(hci_read_local_cmds_sync),
3436 	/* HCI_OP_READ_LOCAL_AMP_INFO */
3437 	HCI_INIT(hci_read_local_amp_info_sync),
3438 	/* HCI_OP_READ_DATA_BLOCK_SIZE */
3439 	HCI_INIT(hci_read_data_block_size_sync),
3440 	/* HCI_OP_READ_FLOW_CONTROL_MODE */
3441 	HCI_INIT(hci_read_flow_control_mode_sync),
3442 	/* HCI_OP_READ_LOCATION_DATA */
3443 	HCI_INIT(hci_read_location_data_sync),
3444 	{}
3445 };
3446 
hci_init1_sync(struct hci_dev * hdev)3447 static int hci_init1_sync(struct hci_dev *hdev)
3448 {
3449 	int err;
3450 
3451 	bt_dev_dbg(hdev, "");
3452 
3453 	/* Reset */
3454 	if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3455 		err = hci_reset_sync(hdev);
3456 		if (err)
3457 			return err;
3458 	}
3459 
3460 	switch (hdev->dev_type) {
3461 	case HCI_PRIMARY:
3462 		hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3463 		return hci_init_stage_sync(hdev, br_init1);
3464 	case HCI_AMP:
3465 		hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3466 		return hci_init_stage_sync(hdev, amp_init1);
3467 	default:
3468 		bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
3469 		break;
3470 	}
3471 
3472 	return 0;
3473 }
3474 
3475 /* AMP Controller init stage 2 command sequence */
3476 static const struct hci_init_stage amp_init2[] = {
3477 	/* HCI_OP_READ_LOCAL_FEATURES */
3478 	HCI_INIT(hci_read_local_features_sync),
3479 	{}
3480 };
3481 
3482 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
hci_read_buffer_size_sync(struct hci_dev * hdev)3483 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3484 {
3485 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3486 				     0, NULL, HCI_CMD_TIMEOUT);
3487 }
3488 
3489 /* Read Class of Device */
hci_read_dev_class_sync(struct hci_dev * hdev)3490 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3491 {
3492 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3493 				     0, NULL, HCI_CMD_TIMEOUT);
3494 }
3495 
3496 /* Read Local Name */
hci_read_local_name_sync(struct hci_dev * hdev)3497 static int hci_read_local_name_sync(struct hci_dev *hdev)
3498 {
3499 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3500 				     0, NULL, HCI_CMD_TIMEOUT);
3501 }
3502 
3503 /* Read Voice Setting */
hci_read_voice_setting_sync(struct hci_dev * hdev)3504 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3505 {
3506 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3507 				     0, NULL, HCI_CMD_TIMEOUT);
3508 }
3509 
3510 /* Read Number of Supported IAC */
hci_read_num_supported_iac_sync(struct hci_dev * hdev)3511 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3512 {
3513 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3514 				     0, NULL, HCI_CMD_TIMEOUT);
3515 }
3516 
3517 /* Read Current IAC LAP */
hci_read_current_iac_lap_sync(struct hci_dev * hdev)3518 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3519 {
3520 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3521 				     0, NULL, HCI_CMD_TIMEOUT);
3522 }
3523 
hci_set_event_filter_sync(struct hci_dev * hdev,u8 flt_type,u8 cond_type,bdaddr_t * bdaddr,u8 auto_accept)3524 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3525 				     u8 cond_type, bdaddr_t *bdaddr,
3526 				     u8 auto_accept)
3527 {
3528 	struct hci_cp_set_event_filter cp;
3529 
3530 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3531 		return 0;
3532 
3533 	if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3534 		return 0;
3535 
3536 	memset(&cp, 0, sizeof(cp));
3537 	cp.flt_type = flt_type;
3538 
3539 	if (flt_type != HCI_FLT_CLEAR_ALL) {
3540 		cp.cond_type = cond_type;
3541 		bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3542 		cp.addr_conn_flt.auto_accept = auto_accept;
3543 	}
3544 
3545 	return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3546 				     flt_type == HCI_FLT_CLEAR_ALL ?
3547 				     sizeof(cp.flt_type) : sizeof(cp), &cp,
3548 				     HCI_CMD_TIMEOUT);
3549 }
3550 
hci_clear_event_filter_sync(struct hci_dev * hdev)3551 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3552 {
3553 	if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3554 		return 0;
3555 
3556 	/* In theory the state machine should not reach here unless
3557 	 * a hci_set_event_filter_sync() call succeeds, but we do
3558 	 * the check both for parity and as a future reminder.
3559 	 */
3560 	if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3561 		return 0;
3562 
3563 	return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3564 					 BDADDR_ANY, 0x00);
3565 }
3566 
3567 /* Connection accept timeout ~20 secs */
hci_write_ca_timeout_sync(struct hci_dev * hdev)3568 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3569 {
3570 	__le16 param = cpu_to_le16(0x7d00);
3571 
3572 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3573 				     sizeof(param), &param, HCI_CMD_TIMEOUT);
3574 }
3575 
3576 /* BR Controller init stage 2 command sequence */
3577 static const struct hci_init_stage br_init2[] = {
3578 	/* HCI_OP_READ_BUFFER_SIZE */
3579 	HCI_INIT(hci_read_buffer_size_sync),
3580 	/* HCI_OP_READ_CLASS_OF_DEV */
3581 	HCI_INIT(hci_read_dev_class_sync),
3582 	/* HCI_OP_READ_LOCAL_NAME */
3583 	HCI_INIT(hci_read_local_name_sync),
3584 	/* HCI_OP_READ_VOICE_SETTING */
3585 	HCI_INIT(hci_read_voice_setting_sync),
3586 	/* HCI_OP_READ_NUM_SUPPORTED_IAC */
3587 	HCI_INIT(hci_read_num_supported_iac_sync),
3588 	/* HCI_OP_READ_CURRENT_IAC_LAP */
3589 	HCI_INIT(hci_read_current_iac_lap_sync),
3590 	/* HCI_OP_SET_EVENT_FLT */
3591 	HCI_INIT(hci_clear_event_filter_sync),
3592 	/* HCI_OP_WRITE_CA_TIMEOUT */
3593 	HCI_INIT(hci_write_ca_timeout_sync),
3594 	{}
3595 };
3596 
hci_write_ssp_mode_1_sync(struct hci_dev * hdev)3597 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3598 {
3599 	u8 mode = 0x01;
3600 
3601 	if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3602 		return 0;
3603 
3604 	/* When SSP is available, then the host features page
3605 	 * should also be available as well. However some
3606 	 * controllers list the max_page as 0 as long as SSP
3607 	 * has not been enabled. To achieve proper debugging
3608 	 * output, force the minimum max_page to 1 at least.
3609 	 */
3610 	hdev->max_page = 0x01;
3611 
3612 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3613 				     sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3614 }
3615 
hci_write_eir_sync(struct hci_dev * hdev)3616 static int hci_write_eir_sync(struct hci_dev *hdev)
3617 {
3618 	struct hci_cp_write_eir cp;
3619 
3620 	if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3621 		return 0;
3622 
3623 	memset(hdev->eir, 0, sizeof(hdev->eir));
3624 	memset(&cp, 0, sizeof(cp));
3625 
3626 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3627 				     HCI_CMD_TIMEOUT);
3628 }
3629 
hci_write_inquiry_mode_sync(struct hci_dev * hdev)3630 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3631 {
3632 	u8 mode;
3633 
3634 	if (!lmp_inq_rssi_capable(hdev) &&
3635 	    !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3636 		return 0;
3637 
3638 	/* If Extended Inquiry Result events are supported, then
3639 	 * they are clearly preferred over Inquiry Result with RSSI
3640 	 * events.
3641 	 */
3642 	mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3643 
3644 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3645 				     sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3646 }
3647 
hci_read_inq_rsp_tx_power_sync(struct hci_dev * hdev)3648 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3649 {
3650 	if (!lmp_inq_tx_pwr_capable(hdev))
3651 		return 0;
3652 
3653 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3654 				     0, NULL, HCI_CMD_TIMEOUT);
3655 }
3656 
hci_read_local_ext_features_sync(struct hci_dev * hdev,u8 page)3657 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3658 {
3659 	struct hci_cp_read_local_ext_features cp;
3660 
3661 	if (!lmp_ext_feat_capable(hdev))
3662 		return 0;
3663 
3664 	memset(&cp, 0, sizeof(cp));
3665 	cp.page = page;
3666 
3667 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3668 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3669 }
3670 
hci_read_local_ext_features_1_sync(struct hci_dev * hdev)3671 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3672 {
3673 	return hci_read_local_ext_features_sync(hdev, 0x01);
3674 }
3675 
3676 /* HCI Controller init stage 2 command sequence */
3677 static const struct hci_init_stage hci_init2[] = {
3678 	/* HCI_OP_READ_LOCAL_COMMANDS */
3679 	HCI_INIT(hci_read_local_cmds_sync),
3680 	/* HCI_OP_WRITE_SSP_MODE */
3681 	HCI_INIT(hci_write_ssp_mode_1_sync),
3682 	/* HCI_OP_WRITE_EIR */
3683 	HCI_INIT(hci_write_eir_sync),
3684 	/* HCI_OP_WRITE_INQUIRY_MODE */
3685 	HCI_INIT(hci_write_inquiry_mode_sync),
3686 	/* HCI_OP_READ_INQ_RSP_TX_POWER */
3687 	HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3688 	/* HCI_OP_READ_LOCAL_EXT_FEATURES */
3689 	HCI_INIT(hci_read_local_ext_features_1_sync),
3690 	/* HCI_OP_WRITE_AUTH_ENABLE */
3691 	HCI_INIT(hci_write_auth_enable_sync),
3692 	{}
3693 };
3694 
3695 /* Read LE Buffer Size */
hci_le_read_buffer_size_sync(struct hci_dev * hdev)3696 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3697 {
3698 	/* Use Read LE Buffer Size V2 if supported */
3699 	if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3700 		return __hci_cmd_sync_status(hdev,
3701 					     HCI_OP_LE_READ_BUFFER_SIZE_V2,
3702 					     0, NULL, HCI_CMD_TIMEOUT);
3703 
3704 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3705 				     0, NULL, HCI_CMD_TIMEOUT);
3706 }
3707 
3708 /* Read LE Local Supported Features */
hci_le_read_local_features_sync(struct hci_dev * hdev)3709 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3710 {
3711 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3712 				     0, NULL, HCI_CMD_TIMEOUT);
3713 }
3714 
3715 /* Read LE Supported States */
hci_le_read_supported_states_sync(struct hci_dev * hdev)3716 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3717 {
3718 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3719 				     0, NULL, HCI_CMD_TIMEOUT);
3720 }
3721 
3722 /* LE Controller init stage 2 command sequence */
3723 static const struct hci_init_stage le_init2[] = {
3724 	/* HCI_OP_LE_READ_LOCAL_FEATURES */
3725 	HCI_INIT(hci_le_read_local_features_sync),
3726 	/* HCI_OP_LE_READ_BUFFER_SIZE */
3727 	HCI_INIT(hci_le_read_buffer_size_sync),
3728 	/* HCI_OP_LE_READ_SUPPORTED_STATES */
3729 	HCI_INIT(hci_le_read_supported_states_sync),
3730 	{}
3731 };
3732 
hci_init2_sync(struct hci_dev * hdev)3733 static int hci_init2_sync(struct hci_dev *hdev)
3734 {
3735 	int err;
3736 
3737 	bt_dev_dbg(hdev, "");
3738 
3739 	if (hdev->dev_type == HCI_AMP)
3740 		return hci_init_stage_sync(hdev, amp_init2);
3741 
3742 	err = hci_init_stage_sync(hdev, hci_init2);
3743 	if (err)
3744 		return err;
3745 
3746 	if (lmp_bredr_capable(hdev)) {
3747 		err = hci_init_stage_sync(hdev, br_init2);
3748 		if (err)
3749 			return err;
3750 	} else {
3751 		hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3752 	}
3753 
3754 	if (lmp_le_capable(hdev)) {
3755 		err = hci_init_stage_sync(hdev, le_init2);
3756 		if (err)
3757 			return err;
3758 		/* LE-only controllers have LE implicitly enabled */
3759 		if (!lmp_bredr_capable(hdev))
3760 			hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3761 	}
3762 
3763 	return 0;
3764 }
3765 
hci_set_event_mask_sync(struct hci_dev * hdev)3766 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3767 {
3768 	/* The second byte is 0xff instead of 0x9f (two reserved bits
3769 	 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3770 	 * command otherwise.
3771 	 */
3772 	u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3773 
3774 	/* CSR 1.1 dongles does not accept any bitfield so don't try to set
3775 	 * any event mask for pre 1.2 devices.
3776 	 */
3777 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3778 		return 0;
3779 
3780 	if (lmp_bredr_capable(hdev)) {
3781 		events[4] |= 0x01; /* Flow Specification Complete */
3782 
3783 		/* Don't set Disconnect Complete and mode change when
3784 		 * suspended as that would wakeup the host when disconnecting
3785 		 * due to suspend.
3786 		 */
3787 		if (hdev->suspended) {
3788 			events[0] &= 0xef;
3789 			events[2] &= 0xf7;
3790 		}
3791 	} else {
3792 		/* Use a different default for LE-only devices */
3793 		memset(events, 0, sizeof(events));
3794 		events[1] |= 0x20; /* Command Complete */
3795 		events[1] |= 0x40; /* Command Status */
3796 		events[1] |= 0x80; /* Hardware Error */
3797 
3798 		/* If the controller supports the Disconnect command, enable
3799 		 * the corresponding event. In addition enable packet flow
3800 		 * control related events.
3801 		 */
3802 		if (hdev->commands[0] & 0x20) {
3803 			/* Don't set Disconnect Complete when suspended as that
3804 			 * would wakeup the host when disconnecting due to
3805 			 * suspend.
3806 			 */
3807 			if (!hdev->suspended)
3808 				events[0] |= 0x10; /* Disconnection Complete */
3809 			events[2] |= 0x04; /* Number of Completed Packets */
3810 			events[3] |= 0x02; /* Data Buffer Overflow */
3811 		}
3812 
3813 		/* If the controller supports the Read Remote Version
3814 		 * Information command, enable the corresponding event.
3815 		 */
3816 		if (hdev->commands[2] & 0x80)
3817 			events[1] |= 0x08; /* Read Remote Version Information
3818 					    * Complete
3819 					    */
3820 
3821 		if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3822 			events[0] |= 0x80; /* Encryption Change */
3823 			events[5] |= 0x80; /* Encryption Key Refresh Complete */
3824 		}
3825 	}
3826 
3827 	if (lmp_inq_rssi_capable(hdev) ||
3828 	    test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3829 		events[4] |= 0x02; /* Inquiry Result with RSSI */
3830 
3831 	if (lmp_ext_feat_capable(hdev))
3832 		events[4] |= 0x04; /* Read Remote Extended Features Complete */
3833 
3834 	if (lmp_esco_capable(hdev)) {
3835 		events[5] |= 0x08; /* Synchronous Connection Complete */
3836 		events[5] |= 0x10; /* Synchronous Connection Changed */
3837 	}
3838 
3839 	if (lmp_sniffsubr_capable(hdev))
3840 		events[5] |= 0x20; /* Sniff Subrating */
3841 
3842 	if (lmp_pause_enc_capable(hdev))
3843 		events[5] |= 0x80; /* Encryption Key Refresh Complete */
3844 
3845 	if (lmp_ext_inq_capable(hdev))
3846 		events[5] |= 0x40; /* Extended Inquiry Result */
3847 
3848 	if (lmp_no_flush_capable(hdev))
3849 		events[7] |= 0x01; /* Enhanced Flush Complete */
3850 
3851 	if (lmp_lsto_capable(hdev))
3852 		events[6] |= 0x80; /* Link Supervision Timeout Changed */
3853 
3854 	if (lmp_ssp_capable(hdev)) {
3855 		events[6] |= 0x01;	/* IO Capability Request */
3856 		events[6] |= 0x02;	/* IO Capability Response */
3857 		events[6] |= 0x04;	/* User Confirmation Request */
3858 		events[6] |= 0x08;	/* User Passkey Request */
3859 		events[6] |= 0x10;	/* Remote OOB Data Request */
3860 		events[6] |= 0x20;	/* Simple Pairing Complete */
3861 		events[7] |= 0x04;	/* User Passkey Notification */
3862 		events[7] |= 0x08;	/* Keypress Notification */
3863 		events[7] |= 0x10;	/* Remote Host Supported
3864 					 * Features Notification
3865 					 */
3866 	}
3867 
3868 	if (lmp_le_capable(hdev))
3869 		events[7] |= 0x20;	/* LE Meta-Event */
3870 
3871 	return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3872 				     sizeof(events), events, HCI_CMD_TIMEOUT);
3873 }
3874 
hci_read_stored_link_key_sync(struct hci_dev * hdev)3875 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3876 {
3877 	struct hci_cp_read_stored_link_key cp;
3878 
3879 	if (!(hdev->commands[6] & 0x20) ||
3880 	    test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3881 		return 0;
3882 
3883 	memset(&cp, 0, sizeof(cp));
3884 	bacpy(&cp.bdaddr, BDADDR_ANY);
3885 	cp.read_all = 0x01;
3886 
3887 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3888 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3889 }
3890 
hci_setup_link_policy_sync(struct hci_dev * hdev)3891 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3892 {
3893 	struct hci_cp_write_def_link_policy cp;
3894 	u16 link_policy = 0;
3895 
3896 	if (!(hdev->commands[5] & 0x10))
3897 		return 0;
3898 
3899 	memset(&cp, 0, sizeof(cp));
3900 
3901 	if (lmp_rswitch_capable(hdev))
3902 		link_policy |= HCI_LP_RSWITCH;
3903 	if (lmp_hold_capable(hdev))
3904 		link_policy |= HCI_LP_HOLD;
3905 	if (lmp_sniff_capable(hdev))
3906 		link_policy |= HCI_LP_SNIFF;
3907 	if (lmp_park_capable(hdev))
3908 		link_policy |= HCI_LP_PARK;
3909 
3910 	cp.policy = cpu_to_le16(link_policy);
3911 
3912 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3913 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3914 }
3915 
hci_read_page_scan_activity_sync(struct hci_dev * hdev)3916 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3917 {
3918 	if (!(hdev->commands[8] & 0x01))
3919 		return 0;
3920 
3921 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3922 				     0, NULL, HCI_CMD_TIMEOUT);
3923 }
3924 
hci_read_def_err_data_reporting_sync(struct hci_dev * hdev)3925 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3926 {
3927 	if (!(hdev->commands[18] & 0x04) ||
3928 	    !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
3929 	    test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3930 		return 0;
3931 
3932 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3933 				     0, NULL, HCI_CMD_TIMEOUT);
3934 }
3935 
hci_read_page_scan_type_sync(struct hci_dev * hdev)3936 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3937 {
3938 	/* Some older Broadcom based Bluetooth 1.2 controllers do not
3939 	 * support the Read Page Scan Type command. Check support for
3940 	 * this command in the bit mask of supported commands.
3941 	 */
3942 	if (!(hdev->commands[13] & 0x01))
3943 		return 0;
3944 
3945 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3946 				     0, NULL, HCI_CMD_TIMEOUT);
3947 }
3948 
3949 /* Read features beyond page 1 if available */
hci_read_local_ext_features_all_sync(struct hci_dev * hdev)3950 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3951 {
3952 	u8 page;
3953 	int err;
3954 
3955 	if (!lmp_ext_feat_capable(hdev))
3956 		return 0;
3957 
3958 	for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3959 	     page++) {
3960 		err = hci_read_local_ext_features_sync(hdev, page);
3961 		if (err)
3962 			return err;
3963 	}
3964 
3965 	return 0;
3966 }
3967 
3968 /* HCI Controller init stage 3 command sequence */
3969 static const struct hci_init_stage hci_init3[] = {
3970 	/* HCI_OP_SET_EVENT_MASK */
3971 	HCI_INIT(hci_set_event_mask_sync),
3972 	/* HCI_OP_READ_STORED_LINK_KEY */
3973 	HCI_INIT(hci_read_stored_link_key_sync),
3974 	/* HCI_OP_WRITE_DEF_LINK_POLICY */
3975 	HCI_INIT(hci_setup_link_policy_sync),
3976 	/* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
3977 	HCI_INIT(hci_read_page_scan_activity_sync),
3978 	/* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
3979 	HCI_INIT(hci_read_def_err_data_reporting_sync),
3980 	/* HCI_OP_READ_PAGE_SCAN_TYPE */
3981 	HCI_INIT(hci_read_page_scan_type_sync),
3982 	/* HCI_OP_READ_LOCAL_EXT_FEATURES */
3983 	HCI_INIT(hci_read_local_ext_features_all_sync),
3984 	{}
3985 };
3986 
hci_le_set_event_mask_sync(struct hci_dev * hdev)3987 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
3988 {
3989 	u8 events[8];
3990 
3991 	if (!lmp_le_capable(hdev))
3992 		return 0;
3993 
3994 	memset(events, 0, sizeof(events));
3995 
3996 	if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
3997 		events[0] |= 0x10;	/* LE Long Term Key Request */
3998 
3999 	/* If controller supports the Connection Parameters Request
4000 	 * Link Layer Procedure, enable the corresponding event.
4001 	 */
4002 	if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4003 		/* LE Remote Connection Parameter Request */
4004 		events[0] |= 0x20;
4005 
4006 	/* If the controller supports the Data Length Extension
4007 	 * feature, enable the corresponding event.
4008 	 */
4009 	if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4010 		events[0] |= 0x40;	/* LE Data Length Change */
4011 
4012 	/* If the controller supports LL Privacy feature or LE Extended Adv,
4013 	 * enable the corresponding event.
4014 	 */
4015 	if (use_enhanced_conn_complete(hdev))
4016 		events[1] |= 0x02;	/* LE Enhanced Connection Complete */
4017 
4018 	/* If the controller supports Extended Scanner Filter
4019 	 * Policies, enable the corresponding event.
4020 	 */
4021 	if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4022 		events[1] |= 0x04;	/* LE Direct Advertising Report */
4023 
4024 	/* If the controller supports Channel Selection Algorithm #2
4025 	 * feature, enable the corresponding event.
4026 	 */
4027 	if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4028 		events[2] |= 0x08;	/* LE Channel Selection Algorithm */
4029 
4030 	/* If the controller supports the LE Set Scan Enable command,
4031 	 * enable the corresponding advertising report event.
4032 	 */
4033 	if (hdev->commands[26] & 0x08)
4034 		events[0] |= 0x02;	/* LE Advertising Report */
4035 
4036 	/* If the controller supports the LE Create Connection
4037 	 * command, enable the corresponding event.
4038 	 */
4039 	if (hdev->commands[26] & 0x10)
4040 		events[0] |= 0x01;	/* LE Connection Complete */
4041 
4042 	/* If the controller supports the LE Connection Update
4043 	 * command, enable the corresponding event.
4044 	 */
4045 	if (hdev->commands[27] & 0x04)
4046 		events[0] |= 0x04;	/* LE Connection Update Complete */
4047 
4048 	/* If the controller supports the LE Read Remote Used Features
4049 	 * command, enable the corresponding event.
4050 	 */
4051 	if (hdev->commands[27] & 0x20)
4052 		/* LE Read Remote Used Features Complete */
4053 		events[0] |= 0x08;
4054 
4055 	/* If the controller supports the LE Read Local P-256
4056 	 * Public Key command, enable the corresponding event.
4057 	 */
4058 	if (hdev->commands[34] & 0x02)
4059 		/* LE Read Local P-256 Public Key Complete */
4060 		events[0] |= 0x80;
4061 
4062 	/* If the controller supports the LE Generate DHKey
4063 	 * command, enable the corresponding event.
4064 	 */
4065 	if (hdev->commands[34] & 0x04)
4066 		events[1] |= 0x01;	/* LE Generate DHKey Complete */
4067 
4068 	/* If the controller supports the LE Set Default PHY or
4069 	 * LE Set PHY commands, enable the corresponding event.
4070 	 */
4071 	if (hdev->commands[35] & (0x20 | 0x40))
4072 		events[1] |= 0x08;        /* LE PHY Update Complete */
4073 
4074 	/* If the controller supports LE Set Extended Scan Parameters
4075 	 * and LE Set Extended Scan Enable commands, enable the
4076 	 * corresponding event.
4077 	 */
4078 	if (use_ext_scan(hdev))
4079 		events[1] |= 0x10;	/* LE Extended Advertising Report */
4080 
4081 	/* If the controller supports the LE Extended Advertising
4082 	 * command, enable the corresponding event.
4083 	 */
4084 	if (ext_adv_capable(hdev))
4085 		events[2] |= 0x02;	/* LE Advertising Set Terminated */
4086 
4087 	if (cis_capable(hdev)) {
4088 		events[3] |= 0x01;	/* LE CIS Established */
4089 		if (cis_peripheral_capable(hdev))
4090 			events[3] |= 0x02; /* LE CIS Request */
4091 	}
4092 
4093 	if (bis_capable(hdev)) {
4094 		events[3] |= 0x04;	/* LE Create BIG Complete */
4095 		events[3] |= 0x08;	/* LE Terminate BIG Complete */
4096 		events[3] |= 0x10;	/* LE BIG Sync Established */
4097 		events[3] |= 0x20;	/* LE BIG Sync Loss */
4098 	}
4099 
4100 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4101 				     sizeof(events), events, HCI_CMD_TIMEOUT);
4102 }
4103 
4104 /* Read LE Advertising Channel TX Power */
hci_le_read_adv_tx_power_sync(struct hci_dev * hdev)4105 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4106 {
4107 	if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4108 		/* HCI TS spec forbids mixing of legacy and extended
4109 		 * advertising commands wherein READ_ADV_TX_POWER is
4110 		 * also included. So do not call it if extended adv
4111 		 * is supported otherwise controller will return
4112 		 * COMMAND_DISALLOWED for extended commands.
4113 		 */
4114 		return __hci_cmd_sync_status(hdev,
4115 					       HCI_OP_LE_READ_ADV_TX_POWER,
4116 					       0, NULL, HCI_CMD_TIMEOUT);
4117 	}
4118 
4119 	return 0;
4120 }
4121 
4122 /* Read LE Min/Max Tx Power*/
hci_le_read_tx_power_sync(struct hci_dev * hdev)4123 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4124 {
4125 	if (!(hdev->commands[38] & 0x80) ||
4126 	    test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4127 		return 0;
4128 
4129 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4130 				     0, NULL, HCI_CMD_TIMEOUT);
4131 }
4132 
4133 /* Read LE Accept List Size */
hci_le_read_accept_list_size_sync(struct hci_dev * hdev)4134 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4135 {
4136 	if (!(hdev->commands[26] & 0x40))
4137 		return 0;
4138 
4139 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4140 				     0, NULL, HCI_CMD_TIMEOUT);
4141 }
4142 
4143 /* Clear LE Accept List */
hci_le_clear_accept_list_sync(struct hci_dev * hdev)4144 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
4145 {
4146 	if (!(hdev->commands[26] & 0x80))
4147 		return 0;
4148 
4149 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
4150 				     HCI_CMD_TIMEOUT);
4151 }
4152 
4153 /* Read LE Resolving List Size */
hci_le_read_resolv_list_size_sync(struct hci_dev * hdev)4154 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4155 {
4156 	if (!(hdev->commands[34] & 0x40))
4157 		return 0;
4158 
4159 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4160 				     0, NULL, HCI_CMD_TIMEOUT);
4161 }
4162 
4163 /* Clear LE Resolving List */
hci_le_clear_resolv_list_sync(struct hci_dev * hdev)4164 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4165 {
4166 	if (!(hdev->commands[34] & 0x20))
4167 		return 0;
4168 
4169 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4170 				     HCI_CMD_TIMEOUT);
4171 }
4172 
4173 /* Set RPA timeout */
hci_le_set_rpa_timeout_sync(struct hci_dev * hdev)4174 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4175 {
4176 	__le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4177 
4178 	if (!(hdev->commands[35] & 0x04) ||
4179 	    test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4180 		return 0;
4181 
4182 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4183 				     sizeof(timeout), &timeout,
4184 				     HCI_CMD_TIMEOUT);
4185 }
4186 
4187 /* Read LE Maximum Data Length */
hci_le_read_max_data_len_sync(struct hci_dev * hdev)4188 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4189 {
4190 	if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4191 		return 0;
4192 
4193 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4194 				     HCI_CMD_TIMEOUT);
4195 }
4196 
4197 /* Read LE Suggested Default Data Length */
hci_le_read_def_data_len_sync(struct hci_dev * hdev)4198 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4199 {
4200 	if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4201 		return 0;
4202 
4203 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4204 				     HCI_CMD_TIMEOUT);
4205 }
4206 
4207 /* Read LE Number of Supported Advertising Sets */
hci_le_read_num_support_adv_sets_sync(struct hci_dev * hdev)4208 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4209 {
4210 	if (!ext_adv_capable(hdev))
4211 		return 0;
4212 
4213 	return __hci_cmd_sync_status(hdev,
4214 				     HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4215 				     0, NULL, HCI_CMD_TIMEOUT);
4216 }
4217 
4218 /* Write LE Host Supported */
hci_set_le_support_sync(struct hci_dev * hdev)4219 static int hci_set_le_support_sync(struct hci_dev *hdev)
4220 {
4221 	struct hci_cp_write_le_host_supported cp;
4222 
4223 	/* LE-only devices do not support explicit enablement */
4224 	if (!lmp_bredr_capable(hdev))
4225 		return 0;
4226 
4227 	memset(&cp, 0, sizeof(cp));
4228 
4229 	if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4230 		cp.le = 0x01;
4231 		cp.simul = 0x00;
4232 	}
4233 
4234 	if (cp.le == lmp_host_le_capable(hdev))
4235 		return 0;
4236 
4237 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4238 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4239 }
4240 
4241 /* LE Set Host Feature */
hci_le_set_host_feature_sync(struct hci_dev * hdev)4242 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4243 {
4244 	struct hci_cp_le_set_host_feature cp;
4245 
4246 	if (!iso_capable(hdev))
4247 		return 0;
4248 
4249 	memset(&cp, 0, sizeof(cp));
4250 
4251 	/* Isochronous Channels (Host Support) */
4252 	cp.bit_number = 32;
4253 	cp.bit_value = 1;
4254 
4255 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4256 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4257 }
4258 
4259 /* LE Controller init stage 3 command sequence */
4260 static const struct hci_init_stage le_init3[] = {
4261 	/* HCI_OP_LE_SET_EVENT_MASK */
4262 	HCI_INIT(hci_le_set_event_mask_sync),
4263 	/* HCI_OP_LE_READ_ADV_TX_POWER */
4264 	HCI_INIT(hci_le_read_adv_tx_power_sync),
4265 	/* HCI_OP_LE_READ_TRANSMIT_POWER */
4266 	HCI_INIT(hci_le_read_tx_power_sync),
4267 	/* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4268 	HCI_INIT(hci_le_read_accept_list_size_sync),
4269 	/* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4270 	HCI_INIT(hci_le_clear_accept_list_sync),
4271 	/* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4272 	HCI_INIT(hci_le_read_resolv_list_size_sync),
4273 	/* HCI_OP_LE_CLEAR_RESOLV_LIST */
4274 	HCI_INIT(hci_le_clear_resolv_list_sync),
4275 	/* HCI_OP_LE_SET_RPA_TIMEOUT */
4276 	HCI_INIT(hci_le_set_rpa_timeout_sync),
4277 	/* HCI_OP_LE_READ_MAX_DATA_LEN */
4278 	HCI_INIT(hci_le_read_max_data_len_sync),
4279 	/* HCI_OP_LE_READ_DEF_DATA_LEN */
4280 	HCI_INIT(hci_le_read_def_data_len_sync),
4281 	/* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4282 	HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4283 	/* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4284 	HCI_INIT(hci_set_le_support_sync),
4285 	/* HCI_OP_LE_SET_HOST_FEATURE */
4286 	HCI_INIT(hci_le_set_host_feature_sync),
4287 	{}
4288 };
4289 
hci_init3_sync(struct hci_dev * hdev)4290 static int hci_init3_sync(struct hci_dev *hdev)
4291 {
4292 	int err;
4293 
4294 	bt_dev_dbg(hdev, "");
4295 
4296 	err = hci_init_stage_sync(hdev, hci_init3);
4297 	if (err)
4298 		return err;
4299 
4300 	if (lmp_le_capable(hdev))
4301 		return hci_init_stage_sync(hdev, le_init3);
4302 
4303 	return 0;
4304 }
4305 
hci_delete_stored_link_key_sync(struct hci_dev * hdev)4306 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4307 {
4308 	struct hci_cp_delete_stored_link_key cp;
4309 
4310 	/* Some Broadcom based Bluetooth controllers do not support the
4311 	 * Delete Stored Link Key command. They are clearly indicating its
4312 	 * absence in the bit mask of supported commands.
4313 	 *
4314 	 * Check the supported commands and only if the command is marked
4315 	 * as supported send it. If not supported assume that the controller
4316 	 * does not have actual support for stored link keys which makes this
4317 	 * command redundant anyway.
4318 	 *
4319 	 * Some controllers indicate that they support handling deleting
4320 	 * stored link keys, but they don't. The quirk lets a driver
4321 	 * just disable this command.
4322 	 */
4323 	if (!(hdev->commands[6] & 0x80) ||
4324 	    test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4325 		return 0;
4326 
4327 	memset(&cp, 0, sizeof(cp));
4328 	bacpy(&cp.bdaddr, BDADDR_ANY);
4329 	cp.delete_all = 0x01;
4330 
4331 	return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4332 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4333 }
4334 
hci_set_event_mask_page_2_sync(struct hci_dev * hdev)4335 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4336 {
4337 	u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4338 	bool changed = false;
4339 
4340 	/* Set event mask page 2 if the HCI command for it is supported */
4341 	if (!(hdev->commands[22] & 0x04))
4342 		return 0;
4343 
4344 	/* If Connectionless Peripheral Broadcast central role is supported
4345 	 * enable all necessary events for it.
4346 	 */
4347 	if (lmp_cpb_central_capable(hdev)) {
4348 		events[1] |= 0x40;	/* Triggered Clock Capture */
4349 		events[1] |= 0x80;	/* Synchronization Train Complete */
4350 		events[2] |= 0x08;	/* Truncated Page Complete */
4351 		events[2] |= 0x20;	/* CPB Channel Map Change */
4352 		changed = true;
4353 	}
4354 
4355 	/* If Connectionless Peripheral Broadcast peripheral role is supported
4356 	 * enable all necessary events for it.
4357 	 */
4358 	if (lmp_cpb_peripheral_capable(hdev)) {
4359 		events[2] |= 0x01;	/* Synchronization Train Received */
4360 		events[2] |= 0x02;	/* CPB Receive */
4361 		events[2] |= 0x04;	/* CPB Timeout */
4362 		events[2] |= 0x10;	/* Peripheral Page Response Timeout */
4363 		changed = true;
4364 	}
4365 
4366 	/* Enable Authenticated Payload Timeout Expired event if supported */
4367 	if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4368 		events[2] |= 0x80;
4369 		changed = true;
4370 	}
4371 
4372 	/* Some Broadcom based controllers indicate support for Set Event
4373 	 * Mask Page 2 command, but then actually do not support it. Since
4374 	 * the default value is all bits set to zero, the command is only
4375 	 * required if the event mask has to be changed. In case no change
4376 	 * to the event mask is needed, skip this command.
4377 	 */
4378 	if (!changed)
4379 		return 0;
4380 
4381 	return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4382 				     sizeof(events), events, HCI_CMD_TIMEOUT);
4383 }
4384 
4385 /* Read local codec list if the HCI command is supported */
hci_read_local_codecs_sync(struct hci_dev * hdev)4386 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4387 {
4388 	if (hdev->commands[45] & 0x04)
4389 		hci_read_supported_codecs_v2(hdev);
4390 	else if (hdev->commands[29] & 0x20)
4391 		hci_read_supported_codecs(hdev);
4392 
4393 	return 0;
4394 }
4395 
4396 /* Read local pairing options if the HCI command is supported */
hci_read_local_pairing_opts_sync(struct hci_dev * hdev)4397 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4398 {
4399 	if (!(hdev->commands[41] & 0x08))
4400 		return 0;
4401 
4402 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4403 				     0, NULL, HCI_CMD_TIMEOUT);
4404 }
4405 
4406 /* Get MWS transport configuration if the HCI command is supported */
hci_get_mws_transport_config_sync(struct hci_dev * hdev)4407 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4408 {
4409 	if (!mws_transport_config_capable(hdev))
4410 		return 0;
4411 
4412 	return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4413 				     0, NULL, HCI_CMD_TIMEOUT);
4414 }
4415 
4416 /* Check for Synchronization Train support */
hci_read_sync_train_params_sync(struct hci_dev * hdev)4417 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4418 {
4419 	if (!lmp_sync_train_capable(hdev))
4420 		return 0;
4421 
4422 	return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4423 				     0, NULL, HCI_CMD_TIMEOUT);
4424 }
4425 
4426 /* Enable Secure Connections if supported and configured */
hci_write_sc_support_1_sync(struct hci_dev * hdev)4427 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4428 {
4429 	u8 support = 0x01;
4430 
4431 	if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4432 	    !bredr_sc_enabled(hdev))
4433 		return 0;
4434 
4435 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4436 				     sizeof(support), &support,
4437 				     HCI_CMD_TIMEOUT);
4438 }
4439 
4440 /* Set erroneous data reporting if supported to the wideband speech
4441  * setting value
4442  */
hci_set_err_data_report_sync(struct hci_dev * hdev)4443 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4444 {
4445 	struct hci_cp_write_def_err_data_reporting cp;
4446 	bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4447 
4448 	if (!(hdev->commands[18] & 0x08) ||
4449 	    !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4450 	    test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4451 		return 0;
4452 
4453 	if (enabled == hdev->err_data_reporting)
4454 		return 0;
4455 
4456 	memset(&cp, 0, sizeof(cp));
4457 	cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4458 				ERR_DATA_REPORTING_DISABLED;
4459 
4460 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4461 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4462 }
4463 
4464 static const struct hci_init_stage hci_init4[] = {
4465 	 /* HCI_OP_DELETE_STORED_LINK_KEY */
4466 	HCI_INIT(hci_delete_stored_link_key_sync),
4467 	/* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4468 	HCI_INIT(hci_set_event_mask_page_2_sync),
4469 	/* HCI_OP_READ_LOCAL_CODECS */
4470 	HCI_INIT(hci_read_local_codecs_sync),
4471 	 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4472 	HCI_INIT(hci_read_local_pairing_opts_sync),
4473 	 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4474 	HCI_INIT(hci_get_mws_transport_config_sync),
4475 	 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4476 	HCI_INIT(hci_read_sync_train_params_sync),
4477 	/* HCI_OP_WRITE_SC_SUPPORT */
4478 	HCI_INIT(hci_write_sc_support_1_sync),
4479 	/* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4480 	HCI_INIT(hci_set_err_data_report_sync),
4481 	{}
4482 };
4483 
4484 /* Set Suggested Default Data Length to maximum if supported */
hci_le_set_write_def_data_len_sync(struct hci_dev * hdev)4485 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4486 {
4487 	struct hci_cp_le_write_def_data_len cp;
4488 
4489 	if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4490 		return 0;
4491 
4492 	memset(&cp, 0, sizeof(cp));
4493 	cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4494 	cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4495 
4496 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4497 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4498 }
4499 
4500 /* Set Default PHY parameters if command is supported */
hci_le_set_default_phy_sync(struct hci_dev * hdev)4501 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4502 {
4503 	struct hci_cp_le_set_default_phy cp;
4504 
4505 	if (!(hdev->commands[35] & 0x20))
4506 		return 0;
4507 
4508 	memset(&cp, 0, sizeof(cp));
4509 	cp.all_phys = 0x00;
4510 	cp.tx_phys = hdev->le_tx_def_phys;
4511 	cp.rx_phys = hdev->le_rx_def_phys;
4512 
4513 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4514 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4515 }
4516 
4517 static const struct hci_init_stage le_init4[] = {
4518 	/* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4519 	HCI_INIT(hci_le_set_write_def_data_len_sync),
4520 	/* HCI_OP_LE_SET_DEFAULT_PHY */
4521 	HCI_INIT(hci_le_set_default_phy_sync),
4522 	{}
4523 };
4524 
hci_init4_sync(struct hci_dev * hdev)4525 static int hci_init4_sync(struct hci_dev *hdev)
4526 {
4527 	int err;
4528 
4529 	bt_dev_dbg(hdev, "");
4530 
4531 	err = hci_init_stage_sync(hdev, hci_init4);
4532 	if (err)
4533 		return err;
4534 
4535 	if (lmp_le_capable(hdev))
4536 		return hci_init_stage_sync(hdev, le_init4);
4537 
4538 	return 0;
4539 }
4540 
hci_init_sync(struct hci_dev * hdev)4541 static int hci_init_sync(struct hci_dev *hdev)
4542 {
4543 	int err;
4544 
4545 	err = hci_init1_sync(hdev);
4546 	if (err < 0)
4547 		return err;
4548 
4549 	if (hci_dev_test_flag(hdev, HCI_SETUP))
4550 		hci_debugfs_create_basic(hdev);
4551 
4552 	err = hci_init2_sync(hdev);
4553 	if (err < 0)
4554 		return err;
4555 
4556 	/* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
4557 	 * BR/EDR/LE type controllers. AMP controllers only need the
4558 	 * first two stages of init.
4559 	 */
4560 	if (hdev->dev_type != HCI_PRIMARY)
4561 		return 0;
4562 
4563 	err = hci_init3_sync(hdev);
4564 	if (err < 0)
4565 		return err;
4566 
4567 	err = hci_init4_sync(hdev);
4568 	if (err < 0)
4569 		return err;
4570 
4571 	/* This function is only called when the controller is actually in
4572 	 * configured state. When the controller is marked as unconfigured,
4573 	 * this initialization procedure is not run.
4574 	 *
4575 	 * It means that it is possible that a controller runs through its
4576 	 * setup phase and then discovers missing settings. If that is the
4577 	 * case, then this function will not be called. It then will only
4578 	 * be called during the config phase.
4579 	 *
4580 	 * So only when in setup phase or config phase, create the debugfs
4581 	 * entries and register the SMP channels.
4582 	 */
4583 	if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4584 	    !hci_dev_test_flag(hdev, HCI_CONFIG))
4585 		return 0;
4586 
4587 	if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4588 		return 0;
4589 
4590 	hci_debugfs_create_common(hdev);
4591 
4592 	if (lmp_bredr_capable(hdev))
4593 		hci_debugfs_create_bredr(hdev);
4594 
4595 	if (lmp_le_capable(hdev))
4596 		hci_debugfs_create_le(hdev);
4597 
4598 	return 0;
4599 }
4600 
4601 #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
4602 
4603 static const struct {
4604 	unsigned long quirk;
4605 	const char *desc;
4606 } hci_broken_table[] = {
4607 	HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
4608 			 "HCI Read Local Supported Commands not supported"),
4609 	HCI_QUIRK_BROKEN(STORED_LINK_KEY,
4610 			 "HCI Delete Stored Link Key command is advertised, "
4611 			 "but not supported."),
4612 	HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
4613 			 "HCI Read Default Erroneous Data Reporting command is "
4614 			 "advertised, but not supported."),
4615 	HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
4616 			 "HCI Read Transmit Power Level command is advertised, "
4617 			 "but not supported."),
4618 	HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
4619 			 "HCI Set Event Filter command not supported."),
4620 	HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
4621 			 "HCI Enhanced Setup Synchronous Connection command is "
4622 			 "advertised, but not supported."),
4623 	HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
4624 			 "HCI LE Set Random Private Address Timeout command is "
4625 			 "advertised, but not supported.")
4626 };
4627 
4628 /* This function handles hdev setup stage:
4629  *
4630  * Calls hdev->setup
4631  * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
4632  */
hci_dev_setup_sync(struct hci_dev * hdev)4633 static int hci_dev_setup_sync(struct hci_dev *hdev)
4634 {
4635 	int ret = 0;
4636 	bool invalid_bdaddr;
4637 	size_t i;
4638 
4639 	if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4640 	    !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4641 		return 0;
4642 
4643 	bt_dev_dbg(hdev, "");
4644 
4645 	hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4646 
4647 	if (hdev->setup)
4648 		ret = hdev->setup(hdev);
4649 
4650 	for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
4651 		if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4652 			bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4653 	}
4654 
4655 	/* The transport driver can set the quirk to mark the
4656 	 * BD_ADDR invalid before creating the HCI device or in
4657 	 * its setup callback.
4658 	 */
4659 	invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
4660 
4661 	if (!ret) {
4662 		if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4663 		    !bacmp(&hdev->public_addr, BDADDR_ANY))
4664 			hci_dev_get_bd_addr_from_property(hdev);
4665 
4666 		if ((invalid_bdaddr ||
4667 		     test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) &&
4668 		    bacmp(&hdev->public_addr, BDADDR_ANY) &&
4669 		    hdev->set_bdaddr) {
4670 			ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4671 			if (!ret)
4672 				invalid_bdaddr = false;
4673 		}
4674 	}
4675 
4676 	/* The transport driver can set these quirks before
4677 	 * creating the HCI device or in its setup callback.
4678 	 *
4679 	 * For the invalid BD_ADDR quirk it is possible that
4680 	 * it becomes a valid address if the bootloader does
4681 	 * provide it (see above).
4682 	 *
4683 	 * In case any of them is set, the controller has to
4684 	 * start up as unconfigured.
4685 	 */
4686 	if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4687 	    invalid_bdaddr)
4688 		hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4689 
4690 	/* For an unconfigured controller it is required to
4691 	 * read at least the version information provided by
4692 	 * the Read Local Version Information command.
4693 	 *
4694 	 * If the set_bdaddr driver callback is provided, then
4695 	 * also the original Bluetooth public device address
4696 	 * will be read using the Read BD Address command.
4697 	 */
4698 	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4699 		return hci_unconf_init_sync(hdev);
4700 
4701 	return ret;
4702 }
4703 
4704 /* This function handles hdev init stage:
4705  *
4706  * Calls hci_dev_setup_sync to perform setup stage
4707  * Calls hci_init_sync to perform HCI command init sequence
4708  */
hci_dev_init_sync(struct hci_dev * hdev)4709 static int hci_dev_init_sync(struct hci_dev *hdev)
4710 {
4711 	int ret;
4712 
4713 	bt_dev_dbg(hdev, "");
4714 
4715 	atomic_set(&hdev->cmd_cnt, 1);
4716 	set_bit(HCI_INIT, &hdev->flags);
4717 
4718 	ret = hci_dev_setup_sync(hdev);
4719 
4720 	if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4721 		/* If public address change is configured, ensure that
4722 		 * the address gets programmed. If the driver does not
4723 		 * support changing the public address, fail the power
4724 		 * on procedure.
4725 		 */
4726 		if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4727 		    hdev->set_bdaddr)
4728 			ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4729 		else
4730 			ret = -EADDRNOTAVAIL;
4731 	}
4732 
4733 	if (!ret) {
4734 		if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4735 		    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4736 			ret = hci_init_sync(hdev);
4737 			if (!ret && hdev->post_init)
4738 				ret = hdev->post_init(hdev);
4739 		}
4740 	}
4741 
4742 	/* If the HCI Reset command is clearing all diagnostic settings,
4743 	 * then they need to be reprogrammed after the init procedure
4744 	 * completed.
4745 	 */
4746 	if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4747 	    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4748 	    hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4749 		ret = hdev->set_diag(hdev, true);
4750 
4751 	if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4752 		msft_do_open(hdev);
4753 		aosp_do_open(hdev);
4754 	}
4755 
4756 	clear_bit(HCI_INIT, &hdev->flags);
4757 
4758 	return ret;
4759 }
4760 
hci_dev_open_sync(struct hci_dev * hdev)4761 int hci_dev_open_sync(struct hci_dev *hdev)
4762 {
4763 	int ret;
4764 
4765 	bt_dev_dbg(hdev, "");
4766 
4767 	if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4768 		ret = -ENODEV;
4769 		goto done;
4770 	}
4771 
4772 	if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4773 	    !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4774 		/* Check for rfkill but allow the HCI setup stage to
4775 		 * proceed (which in itself doesn't cause any RF activity).
4776 		 */
4777 		if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4778 			ret = -ERFKILL;
4779 			goto done;
4780 		}
4781 
4782 		/* Check for valid public address or a configured static
4783 		 * random address, but let the HCI setup proceed to
4784 		 * be able to determine if there is a public address
4785 		 * or not.
4786 		 *
4787 		 * In case of user channel usage, it is not important
4788 		 * if a public address or static random address is
4789 		 * available.
4790 		 *
4791 		 * This check is only valid for BR/EDR controllers
4792 		 * since AMP controllers do not have an address.
4793 		 */
4794 		if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4795 		    hdev->dev_type == HCI_PRIMARY &&
4796 		    !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4797 		    !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4798 			ret = -EADDRNOTAVAIL;
4799 			goto done;
4800 		}
4801 	}
4802 
4803 	if (test_bit(HCI_UP, &hdev->flags)) {
4804 		ret = -EALREADY;
4805 		goto done;
4806 	}
4807 
4808 	if (hdev->open(hdev)) {
4809 		ret = -EIO;
4810 		goto done;
4811 	}
4812 
4813 	set_bit(HCI_RUNNING, &hdev->flags);
4814 	hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4815 
4816 	ret = hci_dev_init_sync(hdev);
4817 	if (!ret) {
4818 		hci_dev_hold(hdev);
4819 		hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4820 		hci_adv_instances_set_rpa_expired(hdev, true);
4821 		set_bit(HCI_UP, &hdev->flags);
4822 		hci_sock_dev_event(hdev, HCI_DEV_UP);
4823 		hci_leds_update_powered(hdev, true);
4824 		if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4825 		    !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4826 		    !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4827 		    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4828 		    hci_dev_test_flag(hdev, HCI_MGMT) &&
4829 		    hdev->dev_type == HCI_PRIMARY) {
4830 			ret = hci_powered_update_sync(hdev);
4831 			mgmt_power_on(hdev, ret);
4832 		}
4833 	} else {
4834 		/* Init failed, cleanup */
4835 		flush_work(&hdev->tx_work);
4836 
4837 		/* Since hci_rx_work() is possible to awake new cmd_work
4838 		 * it should be flushed first to avoid unexpected call of
4839 		 * hci_cmd_work()
4840 		 */
4841 		flush_work(&hdev->rx_work);
4842 		flush_work(&hdev->cmd_work);
4843 
4844 		skb_queue_purge(&hdev->cmd_q);
4845 		skb_queue_purge(&hdev->rx_q);
4846 
4847 		if (hdev->flush)
4848 			hdev->flush(hdev);
4849 
4850 		if (hdev->sent_cmd) {
4851 			cancel_delayed_work_sync(&hdev->cmd_timer);
4852 			kfree_skb(hdev->sent_cmd);
4853 			hdev->sent_cmd = NULL;
4854 		}
4855 
4856 		clear_bit(HCI_RUNNING, &hdev->flags);
4857 		hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4858 
4859 		hdev->close(hdev);
4860 		hdev->flags &= BIT(HCI_RAW);
4861 	}
4862 
4863 done:
4864 	return ret;
4865 }
4866 
4867 /* This function requires the caller holds hdev->lock */
hci_pend_le_actions_clear(struct hci_dev * hdev)4868 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4869 {
4870 	struct hci_conn_params *p;
4871 
4872 	list_for_each_entry(p, &hdev->le_conn_params, list) {
4873 		hci_pend_le_list_del_init(p);
4874 		if (p->conn) {
4875 			hci_conn_drop(p->conn);
4876 			hci_conn_put(p->conn);
4877 			p->conn = NULL;
4878 		}
4879 	}
4880 
4881 	BT_DBG("All LE pending actions cleared");
4882 }
4883 
hci_dev_shutdown(struct hci_dev * hdev)4884 static int hci_dev_shutdown(struct hci_dev *hdev)
4885 {
4886 	int err = 0;
4887 	/* Similar to how we first do setup and then set the exclusive access
4888 	 * bit for userspace, we must first unset userchannel and then clean up.
4889 	 * Otherwise, the kernel can't properly use the hci channel to clean up
4890 	 * the controller (some shutdown routines require sending additional
4891 	 * commands to the controller for example).
4892 	 */
4893 	bool was_userchannel =
4894 		hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
4895 
4896 	if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4897 	    test_bit(HCI_UP, &hdev->flags)) {
4898 		/* Execute vendor specific shutdown routine */
4899 		if (hdev->shutdown)
4900 			err = hdev->shutdown(hdev);
4901 	}
4902 
4903 	if (was_userchannel)
4904 		hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
4905 
4906 	return err;
4907 }
4908 
hci_dev_close_sync(struct hci_dev * hdev)4909 int hci_dev_close_sync(struct hci_dev *hdev)
4910 {
4911 	bool auto_off;
4912 	int err = 0;
4913 
4914 	bt_dev_dbg(hdev, "");
4915 
4916 	cancel_delayed_work(&hdev->power_off);
4917 	cancel_delayed_work(&hdev->ncmd_timer);
4918 	cancel_delayed_work(&hdev->le_scan_disable);
4919 	cancel_delayed_work(&hdev->le_scan_restart);
4920 
4921 	hci_request_cancel_all(hdev);
4922 
4923 	if (hdev->adv_instance_timeout) {
4924 		cancel_delayed_work_sync(&hdev->adv_instance_expire);
4925 		hdev->adv_instance_timeout = 0;
4926 	}
4927 
4928 	err = hci_dev_shutdown(hdev);
4929 
4930 	if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4931 		cancel_delayed_work_sync(&hdev->cmd_timer);
4932 		return err;
4933 	}
4934 
4935 	hci_leds_update_powered(hdev, false);
4936 
4937 	/* Flush RX and TX works */
4938 	flush_work(&hdev->tx_work);
4939 	flush_work(&hdev->rx_work);
4940 
4941 	if (hdev->discov_timeout > 0) {
4942 		hdev->discov_timeout = 0;
4943 		hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4944 		hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
4945 	}
4946 
4947 	if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
4948 		cancel_delayed_work(&hdev->service_cache);
4949 
4950 	if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4951 		struct adv_info *adv_instance;
4952 
4953 		cancel_delayed_work_sync(&hdev->rpa_expired);
4954 
4955 		list_for_each_entry(adv_instance, &hdev->adv_instances, list)
4956 			cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
4957 	}
4958 
4959 	/* Avoid potential lockdep warnings from the *_flush() calls by
4960 	 * ensuring the workqueue is empty up front.
4961 	 */
4962 	drain_workqueue(hdev->workqueue);
4963 
4964 	hci_dev_lock(hdev);
4965 
4966 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4967 
4968 	auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
4969 
4970 	if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
4971 	    !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4972 	    hci_dev_test_flag(hdev, HCI_MGMT))
4973 		__mgmt_power_off(hdev);
4974 
4975 	hci_inquiry_cache_flush(hdev);
4976 	hci_pend_le_actions_clear(hdev);
4977 	hci_conn_hash_flush(hdev);
4978 	/* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
4979 	smp_unregister(hdev);
4980 	hci_dev_unlock(hdev);
4981 
4982 	hci_sock_dev_event(hdev, HCI_DEV_DOWN);
4983 
4984 	if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4985 		aosp_do_close(hdev);
4986 		msft_do_close(hdev);
4987 	}
4988 
4989 	if (hdev->flush)
4990 		hdev->flush(hdev);
4991 
4992 	/* Reset device */
4993 	skb_queue_purge(&hdev->cmd_q);
4994 	atomic_set(&hdev->cmd_cnt, 1);
4995 	if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
4996 	    !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4997 		set_bit(HCI_INIT, &hdev->flags);
4998 		hci_reset_sync(hdev);
4999 		clear_bit(HCI_INIT, &hdev->flags);
5000 	}
5001 
5002 	/* flush cmd  work */
5003 	flush_work(&hdev->cmd_work);
5004 
5005 	/* Drop queues */
5006 	skb_queue_purge(&hdev->rx_q);
5007 	skb_queue_purge(&hdev->cmd_q);
5008 	skb_queue_purge(&hdev->raw_q);
5009 
5010 	/* Drop last sent command */
5011 	if (hdev->sent_cmd) {
5012 		cancel_delayed_work_sync(&hdev->cmd_timer);
5013 		kfree_skb(hdev->sent_cmd);
5014 		hdev->sent_cmd = NULL;
5015 	}
5016 
5017 	clear_bit(HCI_RUNNING, &hdev->flags);
5018 	hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5019 
5020 	/* After this point our queues are empty and no tasks are scheduled. */
5021 	hdev->close(hdev);
5022 
5023 	/* Clear flags */
5024 	hdev->flags &= BIT(HCI_RAW);
5025 	hci_dev_clear_volatile_flags(hdev);
5026 
5027 	/* Controller radio is available but is currently powered down */
5028 	hdev->amp_status = AMP_STATUS_POWERED_DOWN;
5029 
5030 	memset(hdev->eir, 0, sizeof(hdev->eir));
5031 	memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5032 	bacpy(&hdev->random_addr, BDADDR_ANY);
5033 	hci_codec_list_clear(&hdev->local_codecs);
5034 
5035 	hci_dev_put(hdev);
5036 	return err;
5037 }
5038 
5039 /* This function perform power on HCI command sequence as follows:
5040  *
5041  * If controller is already up (HCI_UP) performs hci_powered_update_sync
5042  * sequence otherwise run hci_dev_open_sync which will follow with
5043  * hci_powered_update_sync after the init sequence is completed.
5044  */
hci_power_on_sync(struct hci_dev * hdev)5045 static int hci_power_on_sync(struct hci_dev *hdev)
5046 {
5047 	int err;
5048 
5049 	if (test_bit(HCI_UP, &hdev->flags) &&
5050 	    hci_dev_test_flag(hdev, HCI_MGMT) &&
5051 	    hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5052 		cancel_delayed_work(&hdev->power_off);
5053 		return hci_powered_update_sync(hdev);
5054 	}
5055 
5056 	err = hci_dev_open_sync(hdev);
5057 	if (err < 0)
5058 		return err;
5059 
5060 	/* During the HCI setup phase, a few error conditions are
5061 	 * ignored and they need to be checked now. If they are still
5062 	 * valid, it is important to return the device back off.
5063 	 */
5064 	if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5065 	    hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5066 	    (hdev->dev_type == HCI_PRIMARY &&
5067 	     !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5068 	     !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5069 		hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5070 		hci_dev_close_sync(hdev);
5071 	} else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5072 		queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5073 				   HCI_AUTO_OFF_TIMEOUT);
5074 	}
5075 
5076 	if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5077 		/* For unconfigured devices, set the HCI_RAW flag
5078 		 * so that userspace can easily identify them.
5079 		 */
5080 		if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5081 			set_bit(HCI_RAW, &hdev->flags);
5082 
5083 		/* For fully configured devices, this will send
5084 		 * the Index Added event. For unconfigured devices,
5085 		 * it will send Unconfigued Index Added event.
5086 		 *
5087 		 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
5088 		 * and no event will be send.
5089 		 */
5090 		mgmt_index_added(hdev);
5091 	} else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5092 		/* When the controller is now configured, then it
5093 		 * is important to clear the HCI_RAW flag.
5094 		 */
5095 		if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5096 			clear_bit(HCI_RAW, &hdev->flags);
5097 
5098 		/* Powering on the controller with HCI_CONFIG set only
5099 		 * happens with the transition from unconfigured to
5100 		 * configured. This will send the Index Added event.
5101 		 */
5102 		mgmt_index_added(hdev);
5103 	}
5104 
5105 	return 0;
5106 }
5107 
hci_remote_name_cancel_sync(struct hci_dev * hdev,bdaddr_t * addr)5108 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5109 {
5110 	struct hci_cp_remote_name_req_cancel cp;
5111 
5112 	memset(&cp, 0, sizeof(cp));
5113 	bacpy(&cp.bdaddr, addr);
5114 
5115 	return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5116 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5117 }
5118 
hci_stop_discovery_sync(struct hci_dev * hdev)5119 int hci_stop_discovery_sync(struct hci_dev *hdev)
5120 {
5121 	struct discovery_state *d = &hdev->discovery;
5122 	struct inquiry_entry *e;
5123 	int err;
5124 
5125 	bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5126 
5127 	if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
5128 		if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5129 			err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5130 						    0, NULL, HCI_CMD_TIMEOUT);
5131 			if (err)
5132 				return err;
5133 		}
5134 
5135 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5136 			cancel_delayed_work(&hdev->le_scan_disable);
5137 			cancel_delayed_work(&hdev->le_scan_restart);
5138 
5139 			err = hci_scan_disable_sync(hdev);
5140 			if (err)
5141 				return err;
5142 		}
5143 
5144 	} else {
5145 		err = hci_scan_disable_sync(hdev);
5146 		if (err)
5147 			return err;
5148 	}
5149 
5150 	/* Resume advertising if it was paused */
5151 	if (use_ll_privacy(hdev))
5152 		hci_resume_advertising_sync(hdev);
5153 
5154 	/* No further actions needed for LE-only discovery */
5155 	if (d->type == DISCOV_TYPE_LE)
5156 		return 0;
5157 
5158 	if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5159 		e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5160 						     NAME_PENDING);
5161 		if (!e)
5162 			return 0;
5163 
5164 		return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5165 	}
5166 
5167 	return 0;
5168 }
5169 
hci_disconnect_phy_link_sync(struct hci_dev * hdev,u16 handle,u8 reason)5170 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
5171 					u8 reason)
5172 {
5173 	struct hci_cp_disconn_phy_link cp;
5174 
5175 	memset(&cp, 0, sizeof(cp));
5176 	cp.phy_handle = HCI_PHY_HANDLE(handle);
5177 	cp.reason = reason;
5178 
5179 	return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
5180 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5181 }
5182 
hci_disconnect_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5183 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5184 			       u8 reason)
5185 {
5186 	struct hci_cp_disconnect cp;
5187 
5188 	if (conn->type == AMP_LINK)
5189 		return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
5190 
5191 	memset(&cp, 0, sizeof(cp));
5192 	cp.handle = cpu_to_le16(conn->handle);
5193 	cp.reason = reason;
5194 
5195 	/* Wait for HCI_EV_DISCONN_COMPLETE not HCI_EV_CMD_STATUS when not
5196 	 * suspending.
5197 	 */
5198 	if (!hdev->suspended)
5199 		return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5200 						sizeof(cp), &cp,
5201 						HCI_EV_DISCONN_COMPLETE,
5202 						HCI_CMD_TIMEOUT, NULL);
5203 
5204 	return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5205 				     HCI_CMD_TIMEOUT);
5206 }
5207 
hci_le_connect_cancel_sync(struct hci_dev * hdev,struct hci_conn * conn)5208 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5209 				      struct hci_conn *conn)
5210 {
5211 	if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5212 		return 0;
5213 
5214 	if (test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
5215 		return 0;
5216 
5217 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5218 				     0, NULL, HCI_CMD_TIMEOUT);
5219 }
5220 
hci_connect_cancel_sync(struct hci_dev * hdev,struct hci_conn * conn)5221 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn)
5222 {
5223 	if (conn->type == LE_LINK)
5224 		return hci_le_connect_cancel_sync(hdev, conn);
5225 
5226 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5227 		return 0;
5228 
5229 	return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5230 				     6, &conn->dst, HCI_CMD_TIMEOUT);
5231 }
5232 
hci_reject_sco_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5233 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5234 			       u8 reason)
5235 {
5236 	struct hci_cp_reject_sync_conn_req cp;
5237 
5238 	memset(&cp, 0, sizeof(cp));
5239 	bacpy(&cp.bdaddr, &conn->dst);
5240 	cp.reason = reason;
5241 
5242 	/* SCO rejection has its own limited set of
5243 	 * allowed error values (0x0D-0x0F).
5244 	 */
5245 	if (reason < 0x0d || reason > 0x0f)
5246 		cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5247 
5248 	return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5249 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5250 }
5251 
hci_reject_conn_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5252 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5253 				u8 reason)
5254 {
5255 	struct hci_cp_reject_conn_req cp;
5256 
5257 	if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5258 		return hci_reject_sco_sync(hdev, conn, reason);
5259 
5260 	memset(&cp, 0, sizeof(cp));
5261 	bacpy(&cp.bdaddr, &conn->dst);
5262 	cp.reason = reason;
5263 
5264 	return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5265 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5266 }
5267 
hci_abort_conn_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5268 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5269 {
5270 	int err;
5271 
5272 	switch (conn->state) {
5273 	case BT_CONNECTED:
5274 	case BT_CONFIG:
5275 		return hci_disconnect_sync(hdev, conn, reason);
5276 	case BT_CONNECT:
5277 		err = hci_connect_cancel_sync(hdev, conn);
5278 		/* Cleanup hci_conn object if it cannot be cancelled as it
5279 		 * likelly means the controller and host stack are out of sync.
5280 		 */
5281 		if (err) {
5282 			hci_dev_lock(hdev);
5283 			hci_conn_failed(conn, err);
5284 			hci_dev_unlock(hdev);
5285 		}
5286 		return err;
5287 	case BT_CONNECT2:
5288 		return hci_reject_conn_sync(hdev, conn, reason);
5289 	default:
5290 		conn->state = BT_CLOSED;
5291 		break;
5292 	}
5293 
5294 	return 0;
5295 }
5296 
hci_disconnect_all_sync(struct hci_dev * hdev,u8 reason)5297 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5298 {
5299 	struct hci_conn *conn, *tmp;
5300 	int err;
5301 
5302 	list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
5303 		err = hci_abort_conn_sync(hdev, conn, reason);
5304 		if (err)
5305 			return err;
5306 	}
5307 
5308 	return 0;
5309 }
5310 
5311 /* This function perform power off HCI command sequence as follows:
5312  *
5313  * Clear Advertising
5314  * Stop Discovery
5315  * Disconnect all connections
5316  * hci_dev_close_sync
5317  */
hci_power_off_sync(struct hci_dev * hdev)5318 static int hci_power_off_sync(struct hci_dev *hdev)
5319 {
5320 	int err;
5321 
5322 	/* If controller is already down there is nothing to do */
5323 	if (!test_bit(HCI_UP, &hdev->flags))
5324 		return 0;
5325 
5326 	if (test_bit(HCI_ISCAN, &hdev->flags) ||
5327 	    test_bit(HCI_PSCAN, &hdev->flags)) {
5328 		err = hci_write_scan_enable_sync(hdev, 0x00);
5329 		if (err)
5330 			return err;
5331 	}
5332 
5333 	err = hci_clear_adv_sync(hdev, NULL, false);
5334 	if (err)
5335 		return err;
5336 
5337 	err = hci_stop_discovery_sync(hdev);
5338 	if (err)
5339 		return err;
5340 
5341 	/* Terminated due to Power Off */
5342 	err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5343 	if (err)
5344 		return err;
5345 
5346 	return hci_dev_close_sync(hdev);
5347 }
5348 
hci_set_powered_sync(struct hci_dev * hdev,u8 val)5349 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5350 {
5351 	if (val)
5352 		return hci_power_on_sync(hdev);
5353 
5354 	return hci_power_off_sync(hdev);
5355 }
5356 
hci_write_iac_sync(struct hci_dev * hdev)5357 static int hci_write_iac_sync(struct hci_dev *hdev)
5358 {
5359 	struct hci_cp_write_current_iac_lap cp;
5360 
5361 	if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5362 		return 0;
5363 
5364 	memset(&cp, 0, sizeof(cp));
5365 
5366 	if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5367 		/* Limited discoverable mode */
5368 		cp.num_iac = min_t(u8, hdev->num_iac, 2);
5369 		cp.iac_lap[0] = 0x00;	/* LIAC */
5370 		cp.iac_lap[1] = 0x8b;
5371 		cp.iac_lap[2] = 0x9e;
5372 		cp.iac_lap[3] = 0x33;	/* GIAC */
5373 		cp.iac_lap[4] = 0x8b;
5374 		cp.iac_lap[5] = 0x9e;
5375 	} else {
5376 		/* General discoverable mode */
5377 		cp.num_iac = 1;
5378 		cp.iac_lap[0] = 0x33;	/* GIAC */
5379 		cp.iac_lap[1] = 0x8b;
5380 		cp.iac_lap[2] = 0x9e;
5381 	}
5382 
5383 	return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5384 				     (cp.num_iac * 3) + 1, &cp,
5385 				     HCI_CMD_TIMEOUT);
5386 }
5387 
hci_update_discoverable_sync(struct hci_dev * hdev)5388 int hci_update_discoverable_sync(struct hci_dev *hdev)
5389 {
5390 	int err = 0;
5391 
5392 	if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5393 		err = hci_write_iac_sync(hdev);
5394 		if (err)
5395 			return err;
5396 
5397 		err = hci_update_scan_sync(hdev);
5398 		if (err)
5399 			return err;
5400 
5401 		err = hci_update_class_sync(hdev);
5402 		if (err)
5403 			return err;
5404 	}
5405 
5406 	/* Advertising instances don't use the global discoverable setting, so
5407 	 * only update AD if advertising was enabled using Set Advertising.
5408 	 */
5409 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5410 		err = hci_update_adv_data_sync(hdev, 0x00);
5411 		if (err)
5412 			return err;
5413 
5414 		/* Discoverable mode affects the local advertising
5415 		 * address in limited privacy mode.
5416 		 */
5417 		if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5418 			if (ext_adv_capable(hdev))
5419 				err = hci_start_ext_adv_sync(hdev, 0x00);
5420 			else
5421 				err = hci_enable_advertising_sync(hdev);
5422 		}
5423 	}
5424 
5425 	return err;
5426 }
5427 
update_discoverable_sync(struct hci_dev * hdev,void * data)5428 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5429 {
5430 	return hci_update_discoverable_sync(hdev);
5431 }
5432 
hci_update_discoverable(struct hci_dev * hdev)5433 int hci_update_discoverable(struct hci_dev *hdev)
5434 {
5435 	/* Only queue if it would have any effect */
5436 	if (hdev_is_powered(hdev) &&
5437 	    hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5438 	    hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5439 	    hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5440 		return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5441 					  NULL);
5442 
5443 	return 0;
5444 }
5445 
hci_update_connectable_sync(struct hci_dev * hdev)5446 int hci_update_connectable_sync(struct hci_dev *hdev)
5447 {
5448 	int err;
5449 
5450 	err = hci_update_scan_sync(hdev);
5451 	if (err)
5452 		return err;
5453 
5454 	/* If BR/EDR is not enabled and we disable advertising as a
5455 	 * by-product of disabling connectable, we need to update the
5456 	 * advertising flags.
5457 	 */
5458 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5459 		err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5460 
5461 	/* Update the advertising parameters if necessary */
5462 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5463 	    !list_empty(&hdev->adv_instances)) {
5464 		if (ext_adv_capable(hdev))
5465 			err = hci_start_ext_adv_sync(hdev,
5466 						     hdev->cur_adv_instance);
5467 		else
5468 			err = hci_enable_advertising_sync(hdev);
5469 
5470 		if (err)
5471 			return err;
5472 	}
5473 
5474 	return hci_update_passive_scan_sync(hdev);
5475 }
5476 
hci_inquiry_sync(struct hci_dev * hdev,u8 length)5477 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5478 {
5479 	const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5480 	const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5481 	struct hci_cp_inquiry cp;
5482 
5483 	bt_dev_dbg(hdev, "");
5484 
5485 	if (hci_dev_test_flag(hdev, HCI_INQUIRY))
5486 		return 0;
5487 
5488 	hci_dev_lock(hdev);
5489 	hci_inquiry_cache_flush(hdev);
5490 	hci_dev_unlock(hdev);
5491 
5492 	memset(&cp, 0, sizeof(cp));
5493 
5494 	if (hdev->discovery.limited)
5495 		memcpy(&cp.lap, liac, sizeof(cp.lap));
5496 	else
5497 		memcpy(&cp.lap, giac, sizeof(cp.lap));
5498 
5499 	cp.length = length;
5500 
5501 	return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5502 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5503 }
5504 
hci_active_scan_sync(struct hci_dev * hdev,uint16_t interval)5505 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5506 {
5507 	u8 own_addr_type;
5508 	/* Accept list is not used for discovery */
5509 	u8 filter_policy = 0x00;
5510 	/* Default is to enable duplicates filter */
5511 	u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5512 	int err;
5513 
5514 	bt_dev_dbg(hdev, "");
5515 
5516 	/* If controller is scanning, it means the passive scanning is
5517 	 * running. Thus, we should temporarily stop it in order to set the
5518 	 * discovery scanning parameters.
5519 	 */
5520 	err = hci_scan_disable_sync(hdev);
5521 	if (err) {
5522 		bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5523 		return err;
5524 	}
5525 
5526 	cancel_interleave_scan(hdev);
5527 
5528 	/* Pause address resolution for active scan and stop advertising if
5529 	 * privacy is enabled.
5530 	 */
5531 	err = hci_pause_addr_resolution(hdev);
5532 	if (err)
5533 		goto failed;
5534 
5535 	/* All active scans will be done with either a resolvable private
5536 	 * address (when privacy feature has been enabled) or non-resolvable
5537 	 * private address.
5538 	 */
5539 	err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5540 					     &own_addr_type);
5541 	if (err < 0)
5542 		own_addr_type = ADDR_LE_DEV_PUBLIC;
5543 
5544 	if (hci_is_adv_monitoring(hdev)) {
5545 		/* Duplicate filter should be disabled when some advertisement
5546 		 * monitor is activated, otherwise AdvMon can only receive one
5547 		 * advertisement for one peer(*) during active scanning, and
5548 		 * might report loss to these peers.
5549 		 *
5550 		 * Note that different controllers have different meanings of
5551 		 * |duplicate|. Some of them consider packets with the same
5552 		 * address as duplicate, and others consider packets with the
5553 		 * same address and the same RSSI as duplicate. Although in the
5554 		 * latter case we don't need to disable duplicate filter, but
5555 		 * it is common to have active scanning for a short period of
5556 		 * time, the power impact should be neglectable.
5557 		 */
5558 		filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5559 	}
5560 
5561 	err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5562 				  hdev->le_scan_window_discovery,
5563 				  own_addr_type, filter_policy, filter_dup);
5564 	if (!err)
5565 		return err;
5566 
5567 failed:
5568 	/* Resume advertising if it was paused */
5569 	if (use_ll_privacy(hdev))
5570 		hci_resume_advertising_sync(hdev);
5571 
5572 	/* Resume passive scanning */
5573 	hci_update_passive_scan_sync(hdev);
5574 	return err;
5575 }
5576 
hci_start_interleaved_discovery_sync(struct hci_dev * hdev)5577 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5578 {
5579 	int err;
5580 
5581 	bt_dev_dbg(hdev, "");
5582 
5583 	err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5584 	if (err)
5585 		return err;
5586 
5587 	return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5588 }
5589 
hci_start_discovery_sync(struct hci_dev * hdev)5590 int hci_start_discovery_sync(struct hci_dev *hdev)
5591 {
5592 	unsigned long timeout;
5593 	int err;
5594 
5595 	bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5596 
5597 	switch (hdev->discovery.type) {
5598 	case DISCOV_TYPE_BREDR:
5599 		return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5600 	case DISCOV_TYPE_INTERLEAVED:
5601 		/* When running simultaneous discovery, the LE scanning time
5602 		 * should occupy the whole discovery time sine BR/EDR inquiry
5603 		 * and LE scanning are scheduled by the controller.
5604 		 *
5605 		 * For interleaving discovery in comparison, BR/EDR inquiry
5606 		 * and LE scanning are done sequentially with separate
5607 		 * timeouts.
5608 		 */
5609 		if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5610 			     &hdev->quirks)) {
5611 			timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5612 			/* During simultaneous discovery, we double LE scan
5613 			 * interval. We must leave some time for the controller
5614 			 * to do BR/EDR inquiry.
5615 			 */
5616 			err = hci_start_interleaved_discovery_sync(hdev);
5617 			break;
5618 		}
5619 
5620 		timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5621 		err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5622 		break;
5623 	case DISCOV_TYPE_LE:
5624 		timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5625 		err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5626 		break;
5627 	default:
5628 		return -EINVAL;
5629 	}
5630 
5631 	if (err)
5632 		return err;
5633 
5634 	bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5635 
5636 	/* When service discovery is used and the controller has a
5637 	 * strict duplicate filter, it is important to remember the
5638 	 * start and duration of the scan. This is required for
5639 	 * restarting scanning during the discovery phase.
5640 	 */
5641 	if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5642 	    hdev->discovery.result_filtering) {
5643 		hdev->discovery.scan_start = jiffies;
5644 		hdev->discovery.scan_duration = timeout;
5645 	}
5646 
5647 	queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5648 			   timeout);
5649 	return 0;
5650 }
5651 
hci_suspend_monitor_sync(struct hci_dev * hdev)5652 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5653 {
5654 	switch (hci_get_adv_monitor_offload_ext(hdev)) {
5655 	case HCI_ADV_MONITOR_EXT_MSFT:
5656 		msft_suspend_sync(hdev);
5657 		break;
5658 	default:
5659 		return;
5660 	}
5661 }
5662 
5663 /* This function disables discovery and mark it as paused */
hci_pause_discovery_sync(struct hci_dev * hdev)5664 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5665 {
5666 	int old_state = hdev->discovery.state;
5667 	int err;
5668 
5669 	/* If discovery already stopped/stopping/paused there nothing to do */
5670 	if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
5671 	    hdev->discovery_paused)
5672 		return 0;
5673 
5674 	hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5675 	err = hci_stop_discovery_sync(hdev);
5676 	if (err)
5677 		return err;
5678 
5679 	hdev->discovery_paused = true;
5680 	hdev->discovery_old_state = old_state;
5681 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5682 
5683 	return 0;
5684 }
5685 
hci_update_event_filter_sync(struct hci_dev * hdev)5686 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5687 {
5688 	struct bdaddr_list_with_flags *b;
5689 	u8 scan = SCAN_DISABLED;
5690 	bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5691 	int err;
5692 
5693 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5694 		return 0;
5695 
5696 	/* Some fake CSR controllers lock up after setting this type of
5697 	 * filter, so avoid sending the request altogether.
5698 	 */
5699 	if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5700 		return 0;
5701 
5702 	/* Always clear event filter when starting */
5703 	hci_clear_event_filter_sync(hdev);
5704 
5705 	list_for_each_entry(b, &hdev->accept_list, list) {
5706 		if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
5707 			continue;
5708 
5709 		bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5710 
5711 		err =  hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5712 						 HCI_CONN_SETUP_ALLOW_BDADDR,
5713 						 &b->bdaddr,
5714 						 HCI_CONN_SETUP_AUTO_ON);
5715 		if (err)
5716 			bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5717 				   &b->bdaddr);
5718 		else
5719 			scan = SCAN_PAGE;
5720 	}
5721 
5722 	if (scan && !scanning)
5723 		hci_write_scan_enable_sync(hdev, scan);
5724 	else if (!scan && scanning)
5725 		hci_write_scan_enable_sync(hdev, scan);
5726 
5727 	return 0;
5728 }
5729 
5730 /* This function disables scan (BR and LE) and mark it as paused */
hci_pause_scan_sync(struct hci_dev * hdev)5731 static int hci_pause_scan_sync(struct hci_dev *hdev)
5732 {
5733 	if (hdev->scanning_paused)
5734 		return 0;
5735 
5736 	/* Disable page scan if enabled */
5737 	if (test_bit(HCI_PSCAN, &hdev->flags))
5738 		hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
5739 
5740 	hci_scan_disable_sync(hdev);
5741 
5742 	hdev->scanning_paused = true;
5743 
5744 	return 0;
5745 }
5746 
5747 /* This function performs the HCI suspend procedures in the follow order:
5748  *
5749  * Pause discovery (active scanning/inquiry)
5750  * Pause Directed Advertising/Advertising
5751  * Pause Scanning (passive scanning in case discovery was not active)
5752  * Disconnect all connections
5753  * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5754  * otherwise:
5755  * Update event mask (only set events that are allowed to wake up the host)
5756  * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
5757  * Update passive scanning (lower duty cycle)
5758  * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
5759  */
hci_suspend_sync(struct hci_dev * hdev)5760 int hci_suspend_sync(struct hci_dev *hdev)
5761 {
5762 	int err;
5763 
5764 	/* If marked as suspended there nothing to do */
5765 	if (hdev->suspended)
5766 		return 0;
5767 
5768 	/* Mark device as suspended */
5769 	hdev->suspended = true;
5770 
5771 	/* Pause discovery if not already stopped */
5772 	hci_pause_discovery_sync(hdev);
5773 
5774 	/* Pause other advertisements */
5775 	hci_pause_advertising_sync(hdev);
5776 
5777 	/* Suspend monitor filters */
5778 	hci_suspend_monitor_sync(hdev);
5779 
5780 	/* Prevent disconnects from causing scanning to be re-enabled */
5781 	hci_pause_scan_sync(hdev);
5782 
5783 	if (hci_conn_count(hdev)) {
5784 		/* Soft disconnect everything (power off) */
5785 		err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5786 		if (err) {
5787 			/* Set state to BT_RUNNING so resume doesn't notify */
5788 			hdev->suspend_state = BT_RUNNING;
5789 			hci_resume_sync(hdev);
5790 			return err;
5791 		}
5792 
5793 		/* Update event mask so only the allowed event can wakeup the
5794 		 * host.
5795 		 */
5796 		hci_set_event_mask_sync(hdev);
5797 	}
5798 
5799 	/* Only configure accept list if disconnect succeeded and wake
5800 	 * isn't being prevented.
5801 	 */
5802 	if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5803 		hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5804 		return 0;
5805 	}
5806 
5807 	/* Unpause to take care of updating scanning params */
5808 	hdev->scanning_paused = false;
5809 
5810 	/* Enable event filter for paired devices */
5811 	hci_update_event_filter_sync(hdev);
5812 
5813 	/* Update LE passive scan if enabled */
5814 	hci_update_passive_scan_sync(hdev);
5815 
5816 	/* Pause scan changes again. */
5817 	hdev->scanning_paused = true;
5818 
5819 	hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5820 
5821 	return 0;
5822 }
5823 
5824 /* This function resumes discovery */
hci_resume_discovery_sync(struct hci_dev * hdev)5825 static int hci_resume_discovery_sync(struct hci_dev *hdev)
5826 {
5827 	int err;
5828 
5829 	/* If discovery not paused there nothing to do */
5830 	if (!hdev->discovery_paused)
5831 		return 0;
5832 
5833 	hdev->discovery_paused = false;
5834 
5835 	hci_discovery_set_state(hdev, DISCOVERY_STARTING);
5836 
5837 	err = hci_start_discovery_sync(hdev);
5838 
5839 	hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
5840 				DISCOVERY_FINDING);
5841 
5842 	return err;
5843 }
5844 
hci_resume_monitor_sync(struct hci_dev * hdev)5845 static void hci_resume_monitor_sync(struct hci_dev *hdev)
5846 {
5847 	switch (hci_get_adv_monitor_offload_ext(hdev)) {
5848 	case HCI_ADV_MONITOR_EXT_MSFT:
5849 		msft_resume_sync(hdev);
5850 		break;
5851 	default:
5852 		return;
5853 	}
5854 }
5855 
5856 /* This function resume scan and reset paused flag */
hci_resume_scan_sync(struct hci_dev * hdev)5857 static int hci_resume_scan_sync(struct hci_dev *hdev)
5858 {
5859 	if (!hdev->scanning_paused)
5860 		return 0;
5861 
5862 	hdev->scanning_paused = false;
5863 
5864 	hci_update_scan_sync(hdev);
5865 
5866 	/* Reset passive scanning to normal */
5867 	hci_update_passive_scan_sync(hdev);
5868 
5869 	return 0;
5870 }
5871 
5872 /* This function performs the HCI suspend procedures in the follow order:
5873  *
5874  * Restore event mask
5875  * Clear event filter
5876  * Update passive scanning (normal duty cycle)
5877  * Resume Directed Advertising/Advertising
5878  * Resume discovery (active scanning/inquiry)
5879  */
hci_resume_sync(struct hci_dev * hdev)5880 int hci_resume_sync(struct hci_dev *hdev)
5881 {
5882 	/* If not marked as suspended there nothing to do */
5883 	if (!hdev->suspended)
5884 		return 0;
5885 
5886 	hdev->suspended = false;
5887 
5888 	/* Restore event mask */
5889 	hci_set_event_mask_sync(hdev);
5890 
5891 	/* Clear any event filters and restore scan state */
5892 	hci_clear_event_filter_sync(hdev);
5893 
5894 	/* Resume scanning */
5895 	hci_resume_scan_sync(hdev);
5896 
5897 	/* Resume monitor filters */
5898 	hci_resume_monitor_sync(hdev);
5899 
5900 	/* Resume other advertisements */
5901 	hci_resume_advertising_sync(hdev);
5902 
5903 	/* Resume discovery */
5904 	hci_resume_discovery_sync(hdev);
5905 
5906 	return 0;
5907 }
5908 
conn_use_rpa(struct hci_conn * conn)5909 static bool conn_use_rpa(struct hci_conn *conn)
5910 {
5911 	struct hci_dev *hdev = conn->hdev;
5912 
5913 	return hci_dev_test_flag(hdev, HCI_PRIVACY);
5914 }
5915 
hci_le_ext_directed_advertising_sync(struct hci_dev * hdev,struct hci_conn * conn)5916 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
5917 						struct hci_conn *conn)
5918 {
5919 	struct hci_cp_le_set_ext_adv_params cp;
5920 	int err;
5921 	bdaddr_t random_addr;
5922 	u8 own_addr_type;
5923 
5924 	err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5925 					     &own_addr_type);
5926 	if (err)
5927 		return err;
5928 
5929 	/* Set require_privacy to false so that the remote device has a
5930 	 * chance of identifying us.
5931 	 */
5932 	err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
5933 				     &own_addr_type, &random_addr);
5934 	if (err)
5935 		return err;
5936 
5937 	memset(&cp, 0, sizeof(cp));
5938 
5939 	cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
5940 	cp.own_addr_type = own_addr_type;
5941 	cp.channel_map = hdev->le_adv_channel_map;
5942 	cp.tx_power = HCI_TX_POWER_INVALID;
5943 	cp.primary_phy = HCI_ADV_PHY_1M;
5944 	cp.secondary_phy = HCI_ADV_PHY_1M;
5945 	cp.handle = 0x00; /* Use instance 0 for directed adv */
5946 	cp.own_addr_type = own_addr_type;
5947 	cp.peer_addr_type = conn->dst_type;
5948 	bacpy(&cp.peer_addr, &conn->dst);
5949 
5950 	/* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
5951 	 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
5952 	 * does not supports advertising data when the advertising set already
5953 	 * contains some, the controller shall return erroc code 'Invalid
5954 	 * HCI Command Parameters(0x12).
5955 	 * So it is required to remove adv set for handle 0x00. since we use
5956 	 * instance 0 for directed adv.
5957 	 */
5958 	err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
5959 	if (err)
5960 		return err;
5961 
5962 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
5963 				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5964 	if (err)
5965 		return err;
5966 
5967 	/* Check if random address need to be updated */
5968 	if (own_addr_type == ADDR_LE_DEV_RANDOM &&
5969 	    bacmp(&random_addr, BDADDR_ANY) &&
5970 	    bacmp(&random_addr, &hdev->random_addr)) {
5971 		err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
5972 						       &random_addr);
5973 		if (err)
5974 			return err;
5975 	}
5976 
5977 	return hci_enable_ext_advertising_sync(hdev, 0x00);
5978 }
5979 
hci_le_directed_advertising_sync(struct hci_dev * hdev,struct hci_conn * conn)5980 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
5981 					    struct hci_conn *conn)
5982 {
5983 	struct hci_cp_le_set_adv_param cp;
5984 	u8 status;
5985 	u8 own_addr_type;
5986 	u8 enable;
5987 
5988 	if (ext_adv_capable(hdev))
5989 		return hci_le_ext_directed_advertising_sync(hdev, conn);
5990 
5991 	/* Clear the HCI_LE_ADV bit temporarily so that the
5992 	 * hci_update_random_address knows that it's safe to go ahead
5993 	 * and write a new random address. The flag will be set back on
5994 	 * as soon as the SET_ADV_ENABLE HCI command completes.
5995 	 */
5996 	hci_dev_clear_flag(hdev, HCI_LE_ADV);
5997 
5998 	/* Set require_privacy to false so that the remote device has a
5999 	 * chance of identifying us.
6000 	 */
6001 	status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6002 						&own_addr_type);
6003 	if (status)
6004 		return status;
6005 
6006 	memset(&cp, 0, sizeof(cp));
6007 
6008 	/* Some controllers might reject command if intervals are not
6009 	 * within range for undirected advertising.
6010 	 * BCM20702A0 is known to be affected by this.
6011 	 */
6012 	cp.min_interval = cpu_to_le16(0x0020);
6013 	cp.max_interval = cpu_to_le16(0x0020);
6014 
6015 	cp.type = LE_ADV_DIRECT_IND;
6016 	cp.own_address_type = own_addr_type;
6017 	cp.direct_addr_type = conn->dst_type;
6018 	bacpy(&cp.direct_addr, &conn->dst);
6019 	cp.channel_map = hdev->le_adv_channel_map;
6020 
6021 	status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
6022 				       sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6023 	if (status)
6024 		return status;
6025 
6026 	enable = 0x01;
6027 
6028 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
6029 				     sizeof(enable), &enable, HCI_CMD_TIMEOUT);
6030 }
6031 
set_ext_conn_params(struct hci_conn * conn,struct hci_cp_le_ext_conn_param * p)6032 static void set_ext_conn_params(struct hci_conn *conn,
6033 				struct hci_cp_le_ext_conn_param *p)
6034 {
6035 	struct hci_dev *hdev = conn->hdev;
6036 
6037 	memset(p, 0, sizeof(*p));
6038 
6039 	p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6040 	p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6041 	p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6042 	p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6043 	p->conn_latency = cpu_to_le16(conn->le_conn_latency);
6044 	p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6045 	p->min_ce_len = cpu_to_le16(0x0000);
6046 	p->max_ce_len = cpu_to_le16(0x0000);
6047 }
6048 
hci_le_ext_create_conn_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 own_addr_type)6049 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
6050 				       struct hci_conn *conn, u8 own_addr_type)
6051 {
6052 	struct hci_cp_le_ext_create_conn *cp;
6053 	struct hci_cp_le_ext_conn_param *p;
6054 	u8 data[sizeof(*cp) + sizeof(*p) * 3];
6055 	u32 plen;
6056 
6057 	cp = (void *)data;
6058 	p = (void *)cp->data;
6059 
6060 	memset(cp, 0, sizeof(*cp));
6061 
6062 	bacpy(&cp->peer_addr, &conn->dst);
6063 	cp->peer_addr_type = conn->dst_type;
6064 	cp->own_addr_type = own_addr_type;
6065 
6066 	plen = sizeof(*cp);
6067 
6068 	if (scan_1m(hdev)) {
6069 		cp->phys |= LE_SCAN_PHY_1M;
6070 		set_ext_conn_params(conn, p);
6071 
6072 		p++;
6073 		plen += sizeof(*p);
6074 	}
6075 
6076 	if (scan_2m(hdev)) {
6077 		cp->phys |= LE_SCAN_PHY_2M;
6078 		set_ext_conn_params(conn, p);
6079 
6080 		p++;
6081 		plen += sizeof(*p);
6082 	}
6083 
6084 	if (scan_coded(hdev)) {
6085 		cp->phys |= LE_SCAN_PHY_CODED;
6086 		set_ext_conn_params(conn, p);
6087 
6088 		plen += sizeof(*p);
6089 	}
6090 
6091 	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6092 					plen, data,
6093 					HCI_EV_LE_ENHANCED_CONN_COMPLETE,
6094 					conn->conn_timeout, NULL);
6095 }
6096 
hci_le_create_conn_sync(struct hci_dev * hdev,struct hci_conn * conn)6097 int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
6098 {
6099 	struct hci_cp_le_create_conn cp;
6100 	struct hci_conn_params *params;
6101 	u8 own_addr_type;
6102 	int err;
6103 
6104 	/* If requested to connect as peripheral use directed advertising */
6105 	if (conn->role == HCI_ROLE_SLAVE) {
6106 		/* If we're active scanning and simultaneous roles is not
6107 		 * enabled simply reject the attempt.
6108 		 */
6109 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6110 		    hdev->le_scan_type == LE_SCAN_ACTIVE &&
6111 		    !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6112 			hci_conn_del(conn);
6113 			return -EBUSY;
6114 		}
6115 
6116 		/* Pause advertising while doing directed advertising. */
6117 		hci_pause_advertising_sync(hdev);
6118 
6119 		err = hci_le_directed_advertising_sync(hdev, conn);
6120 		goto done;
6121 	}
6122 
6123 	/* Disable advertising if simultaneous roles is not in use. */
6124 	if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6125 		hci_pause_advertising_sync(hdev);
6126 
6127 	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6128 	if (params) {
6129 		conn->le_conn_min_interval = params->conn_min_interval;
6130 		conn->le_conn_max_interval = params->conn_max_interval;
6131 		conn->le_conn_latency = params->conn_latency;
6132 		conn->le_supv_timeout = params->supervision_timeout;
6133 	} else {
6134 		conn->le_conn_min_interval = hdev->le_conn_min_interval;
6135 		conn->le_conn_max_interval = hdev->le_conn_max_interval;
6136 		conn->le_conn_latency = hdev->le_conn_latency;
6137 		conn->le_supv_timeout = hdev->le_supv_timeout;
6138 	}
6139 
6140 	/* If controller is scanning, we stop it since some controllers are
6141 	 * not able to scan and connect at the same time. Also set the
6142 	 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
6143 	 * handler for scan disabling knows to set the correct discovery
6144 	 * state.
6145 	 */
6146 	if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6147 		hci_scan_disable_sync(hdev);
6148 		hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6149 	}
6150 
6151 	/* Update random address, but set require_privacy to false so
6152 	 * that we never connect with an non-resolvable address.
6153 	 */
6154 	err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6155 					     &own_addr_type);
6156 	if (err)
6157 		goto done;
6158 
6159 	if (use_ext_conn(hdev)) {
6160 		err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6161 		goto done;
6162 	}
6163 
6164 	memset(&cp, 0, sizeof(cp));
6165 
6166 	cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6167 	cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6168 
6169 	bacpy(&cp.peer_addr, &conn->dst);
6170 	cp.peer_addr_type = conn->dst_type;
6171 	cp.own_address_type = own_addr_type;
6172 	cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6173 	cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6174 	cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
6175 	cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6176 	cp.min_ce_len = cpu_to_le16(0x0000);
6177 	cp.max_ce_len = cpu_to_le16(0x0000);
6178 
6179 	/* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6180 	 *
6181 	 * If this event is unmasked and the HCI_LE_Connection_Complete event
6182 	 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6183 	 * sent when a new connection has been created.
6184 	 */
6185 	err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6186 				       sizeof(cp), &cp,
6187 				       use_enhanced_conn_complete(hdev) ?
6188 				       HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6189 				       HCI_EV_LE_CONN_COMPLETE,
6190 				       conn->conn_timeout, NULL);
6191 
6192 done:
6193 	if (err == -ETIMEDOUT)
6194 		hci_le_connect_cancel_sync(hdev, conn);
6195 
6196 	/* Re-enable advertising after the connection attempt is finished. */
6197 	hci_resume_advertising_sync(hdev);
6198 	return err;
6199 }
6200 
hci_le_remove_cig_sync(struct hci_dev * hdev,u8 handle)6201 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6202 {
6203 	struct hci_cp_le_remove_cig cp;
6204 
6205 	memset(&cp, 0, sizeof(cp));
6206 	cp.cig_id = handle;
6207 
6208 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6209 				     &cp, HCI_CMD_TIMEOUT);
6210 }
6211 
hci_le_big_terminate_sync(struct hci_dev * hdev,u8 handle)6212 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6213 {
6214 	struct hci_cp_le_big_term_sync cp;
6215 
6216 	memset(&cp, 0, sizeof(cp));
6217 	cp.handle = handle;
6218 
6219 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6220 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6221 }
6222 
hci_le_pa_terminate_sync(struct hci_dev * hdev,u16 handle)6223 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6224 {
6225 	struct hci_cp_le_pa_term_sync cp;
6226 
6227 	memset(&cp, 0, sizeof(cp));
6228 	cp.handle = cpu_to_le16(handle);
6229 
6230 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6231 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6232 }
6233 
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)6234 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6235 			   bool use_rpa, struct adv_info *adv_instance,
6236 			   u8 *own_addr_type, bdaddr_t *rand_addr)
6237 {
6238 	int err;
6239 
6240 	bacpy(rand_addr, BDADDR_ANY);
6241 
6242 	/* If privacy is enabled use a resolvable private address. If
6243 	 * current RPA has expired then generate a new one.
6244 	 */
6245 	if (use_rpa) {
6246 		/* If Controller supports LL Privacy use own address type is
6247 		 * 0x03
6248 		 */
6249 		if (use_ll_privacy(hdev))
6250 			*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
6251 		else
6252 			*own_addr_type = ADDR_LE_DEV_RANDOM;
6253 
6254 		if (adv_instance) {
6255 			if (adv_rpa_valid(adv_instance))
6256 				return 0;
6257 		} else {
6258 			if (rpa_valid(hdev))
6259 				return 0;
6260 		}
6261 
6262 		err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6263 		if (err < 0) {
6264 			bt_dev_err(hdev, "failed to generate new RPA");
6265 			return err;
6266 		}
6267 
6268 		bacpy(rand_addr, &hdev->rpa);
6269 
6270 		return 0;
6271 	}
6272 
6273 	/* In case of required privacy without resolvable private address,
6274 	 * use an non-resolvable private address. This is useful for
6275 	 * non-connectable advertising.
6276 	 */
6277 	if (require_privacy) {
6278 		bdaddr_t nrpa;
6279 
6280 		while (true) {
6281 			/* The non-resolvable private address is generated
6282 			 * from random six bytes with the two most significant
6283 			 * bits cleared.
6284 			 */
6285 			get_random_bytes(&nrpa, 6);
6286 			nrpa.b[5] &= 0x3f;
6287 
6288 			/* The non-resolvable private address shall not be
6289 			 * equal to the public address.
6290 			 */
6291 			if (bacmp(&hdev->bdaddr, &nrpa))
6292 				break;
6293 		}
6294 
6295 		*own_addr_type = ADDR_LE_DEV_RANDOM;
6296 		bacpy(rand_addr, &nrpa);
6297 
6298 		return 0;
6299 	}
6300 
6301 	/* No privacy so use a public address. */
6302 	*own_addr_type = ADDR_LE_DEV_PUBLIC;
6303 
6304 	return 0;
6305 }
6306 
_update_adv_data_sync(struct hci_dev * hdev,void * data)6307 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6308 {
6309 	u8 instance = PTR_ERR(data);
6310 
6311 	return hci_update_adv_data_sync(hdev, instance);
6312 }
6313 
hci_update_adv_data(struct hci_dev * hdev,u8 instance)6314 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6315 {
6316 	return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6317 				  ERR_PTR(instance), NULL);
6318 }
6319