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