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