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