1 // SPDX-License-Identifier: GPL-2.0
3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2021 Intel Corporation
8 #include <linux/property.h>
10 #include <net/bluetooth/bluetooth.h>
11 #include <net/bluetooth/hci_core.h>
12 #include <net/bluetooth/mgmt.h>
14 #include "hci_request.h"
15 #include "hci_codec.h"
16 #include "hci_debugfs.h"
23 static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
26 bt_dev_dbg(hdev, "result 0x%2.2x", result);
28 if (hdev->req_status != HCI_REQ_PEND)
31 hdev->req_result = result;
32 hdev->req_status = HCI_REQ_DONE;
35 struct sock *sk = hci_skb_sk(skb);
37 /* Drop sk reference if set */
41 hdev->req_skb = skb_get(skb);
44 wake_up_interruptible(&hdev->req_wait_q);
47 static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
48 u32 plen, const void *param,
51 int len = HCI_COMMAND_HDR_SIZE + plen;
52 struct hci_command_hdr *hdr;
55 skb = bt_skb_alloc(len, GFP_ATOMIC);
59 hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
60 hdr->opcode = cpu_to_le16(opcode);
64 skb_put_data(skb, param, plen);
66 bt_dev_dbg(hdev, "skb len %d", skb->len);
68 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
69 hci_skb_opcode(skb) = opcode;
71 /* Grab a reference if command needs to be associated with a sock (e.g.
72 * likely mgmt socket that initiated the command).
82 static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
83 const void *param, u8 event, struct sock *sk)
85 struct hci_dev *hdev = req->hdev;
88 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
90 /* If an error occurred during request building, there is no point in
91 * queueing the HCI command. We can simply return.
96 skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
98 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
104 if (skb_queue_empty(&req->cmd_q))
105 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
107 hci_skb_event(skb) = event;
109 skb_queue_tail(&req->cmd_q, skb);
112 static int hci_cmd_sync_run(struct hci_request *req)
114 struct hci_dev *hdev = req->hdev;
118 bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
120 /* If an error occurred during request building, remove all HCI
121 * commands queued on the HCI request queue.
124 skb_queue_purge(&req->cmd_q);
128 /* Do not allow empty requests */
129 if (skb_queue_empty(&req->cmd_q))
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;
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);
140 queue_work(hdev->workqueue, &hdev->cmd_work);
145 /* This function requires the caller holds hdev->req_lock. */
146 struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
147 const void *param, u8 event, u32 timeout,
150 struct hci_request req;
154 bt_dev_dbg(hdev, "Opcode 0x%4x", opcode);
156 hci_req_init(&req, hdev);
158 hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
160 hdev->req_status = HCI_REQ_PEND;
162 err = hci_cmd_sync_run(&req);
166 err = wait_event_interruptible_timeout(hdev->req_wait_q,
167 hdev->req_status != HCI_REQ_PEND,
170 if (err == -ERESTARTSYS)
171 return ERR_PTR(-EINTR);
173 switch (hdev->req_status) {
175 err = -bt_to_errno(hdev->req_result);
178 case HCI_REQ_CANCELED:
179 err = -hdev->req_result;
187 hdev->req_status = 0;
188 hdev->req_result = 0;
190 hdev->req_skb = NULL;
192 bt_dev_dbg(hdev, "end: err %d", err);
201 EXPORT_SYMBOL(__hci_cmd_sync_sk);
203 /* This function requires the caller holds hdev->req_lock. */
204 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
205 const void *param, u32 timeout)
207 return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
209 EXPORT_SYMBOL(__hci_cmd_sync);
211 /* Send HCI command and wait for command complete event */
212 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
213 const void *param, u32 timeout)
217 if (!test_bit(HCI_UP, &hdev->flags))
218 return ERR_PTR(-ENETDOWN);
220 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
222 hci_req_sync_lock(hdev);
223 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
224 hci_req_sync_unlock(hdev);
228 EXPORT_SYMBOL(hci_cmd_sync);
230 /* This function requires the caller holds hdev->req_lock. */
231 struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
232 const void *param, u8 event, u32 timeout)
234 return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
237 EXPORT_SYMBOL(__hci_cmd_sync_ev);
239 /* This function requires the caller holds hdev->req_lock. */
240 int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
241 const void *param, u8 event, u32 timeout,
247 skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
250 bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode,
255 /* If command return a status event skb will be set to NULL as there are
256 * no parameters, in case of failure IS_ERR(skb) would have be set to
257 * the actual error would be found with PTR_ERR(skb).
262 status = skb->data[0];
268 EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
270 int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
271 const void *param, u32 timeout)
273 return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
276 EXPORT_SYMBOL(__hci_cmd_sync_status);
278 static void hci_cmd_sync_work(struct work_struct *work)
280 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
282 bt_dev_dbg(hdev, "");
284 /* Dequeue all entries and run them */
286 struct hci_cmd_sync_work_entry *entry;
288 mutex_lock(&hdev->cmd_sync_work_lock);
289 entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
290 struct hci_cmd_sync_work_entry,
293 list_del(&entry->list);
294 mutex_unlock(&hdev->cmd_sync_work_lock);
299 bt_dev_dbg(hdev, "entry %p", entry);
304 hci_req_sync_lock(hdev);
305 err = entry->func(hdev, entry->data);
307 entry->destroy(hdev, entry->data, err);
308 hci_req_sync_unlock(hdev);
315 static void hci_cmd_sync_cancel_work(struct work_struct *work)
317 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
319 cancel_delayed_work_sync(&hdev->cmd_timer);
320 cancel_delayed_work_sync(&hdev->ncmd_timer);
321 atomic_set(&hdev->cmd_cnt, 1);
323 wake_up_interruptible(&hdev->req_wait_q);
326 static int hci_scan_disable_sync(struct hci_dev *hdev);
327 static int scan_disable_sync(struct hci_dev *hdev, void *data)
329 return hci_scan_disable_sync(hdev);
332 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length);
333 static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data)
335 return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN);
338 static void le_scan_disable(struct work_struct *work)
340 struct hci_dev *hdev = container_of(work, struct hci_dev,
341 le_scan_disable.work);
344 bt_dev_dbg(hdev, "");
347 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
350 cancel_delayed_work(&hdev->le_scan_restart);
352 status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
354 bt_dev_err(hdev, "failed to disable LE scan: %d", status);
358 hdev->discovery.scan_start = 0;
360 /* If we were running LE only scan, change discovery state. If
361 * we were running both LE and BR/EDR inquiry simultaneously,
362 * and BR/EDR inquiry is already finished, stop discovery,
363 * otherwise BR/EDR inquiry will stop discovery when finished.
364 * If we will resolve remote device name, do not change
368 if (hdev->discovery.type == DISCOV_TYPE_LE)
371 if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
374 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
375 if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
376 hdev->discovery.state != DISCOVERY_RESOLVING)
382 status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
384 bt_dev_err(hdev, "inquiry failed: status %d", status);
391 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
394 hci_dev_unlock(hdev);
397 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
399 static int hci_le_scan_restart_sync(struct hci_dev *hdev)
401 /* If controller is not scanning we are done. */
402 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
405 if (hdev->scanning_paused) {
406 bt_dev_dbg(hdev, "Scanning is paused for suspend");
410 hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
411 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE,
412 LE_SCAN_FILTER_DUP_ENABLE);
415 static int le_scan_restart_sync(struct hci_dev *hdev, void *data)
417 return hci_le_scan_restart_sync(hdev);
420 static void le_scan_restart(struct work_struct *work)
422 struct hci_dev *hdev = container_of(work, struct hci_dev,
423 le_scan_restart.work);
424 unsigned long timeout, duration, scan_start, now;
427 bt_dev_dbg(hdev, "");
431 status = hci_cmd_sync_queue(hdev, le_scan_restart_sync, NULL, NULL);
433 bt_dev_err(hdev, "failed to restart LE scan: status %d",
438 if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
439 !hdev->discovery.scan_start)
442 /* When the scan was started, hdev->le_scan_disable has been queued
443 * after duration from scan_start. During scan restart this job
444 * has been canceled, and we need to queue it again after proper
445 * timeout, to make sure that scan does not run indefinitely.
447 duration = hdev->discovery.scan_duration;
448 scan_start = hdev->discovery.scan_start;
450 if (now - scan_start <= duration) {
453 if (now >= scan_start)
454 elapsed = now - scan_start;
456 elapsed = ULONG_MAX - scan_start + now;
458 timeout = duration - elapsed;
463 queue_delayed_work(hdev->req_workqueue,
464 &hdev->le_scan_disable, timeout);
467 hci_dev_unlock(hdev);
470 static int reenable_adv_sync(struct hci_dev *hdev, void *data)
472 bt_dev_dbg(hdev, "");
474 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
475 list_empty(&hdev->adv_instances))
478 if (hdev->cur_adv_instance) {
479 return hci_schedule_adv_instance_sync(hdev,
480 hdev->cur_adv_instance,
483 if (ext_adv_capable(hdev)) {
484 hci_start_ext_adv_sync(hdev, 0x00);
486 hci_update_adv_data_sync(hdev, 0x00);
487 hci_update_scan_rsp_data_sync(hdev, 0x00);
488 hci_enable_advertising_sync(hdev);
495 static void reenable_adv(struct work_struct *work)
497 struct hci_dev *hdev = container_of(work, struct hci_dev,
501 bt_dev_dbg(hdev, "");
505 status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
507 bt_dev_err(hdev, "failed to reenable ADV: %d", status);
509 hci_dev_unlock(hdev);
512 static void cancel_adv_timeout(struct hci_dev *hdev)
514 if (hdev->adv_instance_timeout) {
515 hdev->adv_instance_timeout = 0;
516 cancel_delayed_work(&hdev->adv_instance_expire);
520 /* For a single instance:
521 * - force == true: The instance will be removed even when its remaining
522 * lifetime is not zero.
523 * - force == false: the instance will be deactivated but kept stored unless
524 * the remaining lifetime is zero.
526 * For instance == 0x00:
527 * - force == true: All instances will be removed regardless of their timeout
529 * - force == false: Only instances that have a timeout will be removed.
531 int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
532 u8 instance, bool force)
534 struct adv_info *adv_instance, *n, *next_instance = NULL;
538 /* Cancel any timeout concerning the removed instance(s). */
539 if (!instance || hdev->cur_adv_instance == instance)
540 cancel_adv_timeout(hdev);
542 /* Get the next instance to advertise BEFORE we remove
543 * the current one. This can be the same instance again
544 * if there is only one instance.
546 if (instance && hdev->cur_adv_instance == instance)
547 next_instance = hci_get_next_instance(hdev, instance);
549 if (instance == 0x00) {
550 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
552 if (!(force || adv_instance->timeout))
555 rem_inst = adv_instance->instance;
556 err = hci_remove_adv_instance(hdev, rem_inst);
558 mgmt_advertising_removed(sk, hdev, rem_inst);
561 adv_instance = hci_find_adv_instance(hdev, instance);
563 if (force || (adv_instance && adv_instance->timeout &&
564 !adv_instance->remaining_time)) {
565 /* Don't advertise a removed instance. */
567 next_instance->instance == instance)
568 next_instance = NULL;
570 err = hci_remove_adv_instance(hdev, instance);
572 mgmt_advertising_removed(sk, hdev, instance);
576 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
579 if (next_instance && !ext_adv_capable(hdev))
580 return hci_schedule_adv_instance_sync(hdev,
581 next_instance->instance,
587 static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
589 u8 instance = *(u8 *)data;
593 hci_clear_adv_instance_sync(hdev, NULL, instance, false);
595 if (list_empty(&hdev->adv_instances))
596 return hci_disable_advertising_sync(hdev);
601 static void adv_timeout_expire(struct work_struct *work)
604 struct hci_dev *hdev = container_of(work, struct hci_dev,
605 adv_instance_expire.work);
607 bt_dev_dbg(hdev, "");
611 hdev->adv_instance_timeout = 0;
613 if (hdev->cur_adv_instance == 0x00)
616 inst_ptr = kmalloc(1, GFP_KERNEL);
620 *inst_ptr = hdev->cur_adv_instance;
621 hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
624 hci_dev_unlock(hdev);
627 void hci_cmd_sync_init(struct hci_dev *hdev)
629 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
630 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
631 mutex_init(&hdev->cmd_sync_work_lock);
632 mutex_init(&hdev->unregister_lock);
634 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
635 INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
636 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
637 INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart);
638 INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
641 void hci_cmd_sync_clear(struct hci_dev *hdev)
643 struct hci_cmd_sync_work_entry *entry, *tmp;
645 cancel_work_sync(&hdev->cmd_sync_work);
646 cancel_work_sync(&hdev->reenable_adv_work);
648 mutex_lock(&hdev->cmd_sync_work_lock);
649 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
651 entry->destroy(hdev, entry->data, -ECANCELED);
653 list_del(&entry->list);
656 mutex_unlock(&hdev->cmd_sync_work_lock);
659 void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
661 bt_dev_dbg(hdev, "err 0x%2.2x", err);
663 if (hdev->req_status == HCI_REQ_PEND) {
664 hdev->req_result = err;
665 hdev->req_status = HCI_REQ_CANCELED;
667 cancel_delayed_work_sync(&hdev->cmd_timer);
668 cancel_delayed_work_sync(&hdev->ncmd_timer);
669 atomic_set(&hdev->cmd_cnt, 1);
671 wake_up_interruptible(&hdev->req_wait_q);
675 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
677 bt_dev_dbg(hdev, "err 0x%2.2x", err);
679 if (hdev->req_status == HCI_REQ_PEND) {
680 hdev->req_result = err;
681 hdev->req_status = HCI_REQ_CANCELED;
683 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
686 EXPORT_SYMBOL(hci_cmd_sync_cancel);
688 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
689 void *data, hci_cmd_sync_work_destroy_t destroy)
691 struct hci_cmd_sync_work_entry *entry;
694 mutex_lock(&hdev->unregister_lock);
695 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
700 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
707 entry->destroy = destroy;
709 mutex_lock(&hdev->cmd_sync_work_lock);
710 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
711 mutex_unlock(&hdev->cmd_sync_work_lock);
713 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
716 mutex_unlock(&hdev->unregister_lock);
719 EXPORT_SYMBOL(hci_cmd_sync_queue);
721 int hci_update_eir_sync(struct hci_dev *hdev)
723 struct hci_cp_write_eir cp;
725 bt_dev_dbg(hdev, "");
727 if (!hdev_is_powered(hdev))
730 if (!lmp_ext_inq_capable(hdev))
733 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
736 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
739 memset(&cp, 0, sizeof(cp));
741 eir_create(hdev, cp.data);
743 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
746 memcpy(hdev->eir, cp.data, sizeof(cp.data));
748 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
752 static u8 get_service_classes(struct hci_dev *hdev)
754 struct bt_uuid *uuid;
757 list_for_each_entry(uuid, &hdev->uuids, list)
758 val |= uuid->svc_hint;
763 int hci_update_class_sync(struct hci_dev *hdev)
767 bt_dev_dbg(hdev, "");
769 if (!hdev_is_powered(hdev))
772 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
775 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
778 cod[0] = hdev->minor_class;
779 cod[1] = hdev->major_class;
780 cod[2] = get_service_classes(hdev);
782 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
785 if (memcmp(cod, hdev->dev_class, 3) == 0)
788 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
789 sizeof(cod), cod, HCI_CMD_TIMEOUT);
792 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
794 /* If there is no connection we are OK to advertise. */
795 if (hci_conn_num(hdev, LE_LINK) == 0)
798 /* Check le_states if there is any connection in peripheral role. */
799 if (hdev->conn_hash.le_num_peripheral > 0) {
800 /* Peripheral connection state and non connectable mode
803 if (!connectable && !(hdev->le_states[2] & 0x10))
806 /* Peripheral connection state and connectable mode bit 38
807 * and scannable bit 21.
809 if (connectable && (!(hdev->le_states[4] & 0x40) ||
810 !(hdev->le_states[2] & 0x20)))
814 /* Check le_states if there is any connection in central role. */
815 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
816 /* Central connection state and non connectable mode bit 18. */
817 if (!connectable && !(hdev->le_states[2] & 0x02))
820 /* Central connection state and connectable mode bit 35 and
823 if (connectable && (!(hdev->le_states[4] & 0x08) ||
824 !(hdev->le_states[2] & 0x08)))
831 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
833 /* If privacy is not enabled don't use RPA */
834 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
837 /* If basic privacy mode is enabled use RPA */
838 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
841 /* If limited privacy mode is enabled don't use RPA if we're
842 * both discoverable and bondable.
844 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
845 hci_dev_test_flag(hdev, HCI_BONDABLE))
848 /* We're neither bondable nor discoverable in the limited
849 * privacy mode, therefore use RPA.
854 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
856 /* If we're advertising or initiating an LE connection we can't
857 * go ahead and change the random address at this time. This is
858 * because the eventual initiator address used for the
859 * subsequently created connection will be undefined (some
860 * controllers use the new address and others the one we had
861 * when the operation started).
863 * In this kind of scenario skip the update and let the random
864 * address be updated at the next cycle.
866 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
867 hci_lookup_le_connect(hdev)) {
868 bt_dev_dbg(hdev, "Deferring random address update");
869 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
873 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
874 6, rpa, HCI_CMD_TIMEOUT);
877 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
878 bool rpa, u8 *own_addr_type)
882 /* If privacy is enabled use a resolvable private address. If
883 * current RPA has expired or there is something else than
884 * the current RPA in use, then generate a new one.
887 /* If Controller supports LL Privacy use own address type is
890 if (use_ll_privacy(hdev))
891 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
893 *own_addr_type = ADDR_LE_DEV_RANDOM;
895 /* Check if RPA is valid */
899 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
901 bt_dev_err(hdev, "failed to generate new RPA");
905 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
912 /* In case of required privacy without resolvable private address,
913 * use an non-resolvable private address. This is useful for active
914 * scanning and non-connectable advertising.
916 if (require_privacy) {
920 /* The non-resolvable private address is generated
921 * from random six bytes with the two most significant
924 get_random_bytes(&nrpa, 6);
927 /* The non-resolvable private address shall not be
928 * equal to the public address.
930 if (bacmp(&hdev->bdaddr, &nrpa))
934 *own_addr_type = ADDR_LE_DEV_RANDOM;
936 return hci_set_random_addr_sync(hdev, &nrpa);
939 /* If forcing static address is in use or there is no public
940 * address use the static address as random address (but skip
941 * the HCI command if the current random address is already the
944 * In case BR/EDR has been disabled on a dual-mode controller
945 * and a static address has been configured, then use that
946 * address instead of the public BR/EDR address.
948 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
949 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
950 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
951 bacmp(&hdev->static_addr, BDADDR_ANY))) {
952 *own_addr_type = ADDR_LE_DEV_RANDOM;
953 if (bacmp(&hdev->static_addr, &hdev->random_addr))
954 return hci_set_random_addr_sync(hdev,
959 /* Neither privacy nor static address is being used so use a
962 *own_addr_type = ADDR_LE_DEV_PUBLIC;
967 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
969 struct hci_cp_le_set_ext_adv_enable *cp;
970 struct hci_cp_ext_adv_set *set;
971 u8 data[sizeof(*cp) + sizeof(*set) * 1];
974 /* If request specifies an instance that doesn't exist, fail */
976 struct adv_info *adv;
978 adv = hci_find_adv_instance(hdev, instance);
982 /* If not enabled there is nothing to do */
987 memset(data, 0, sizeof(data));
990 set = (void *)cp->data;
992 /* Instance 0x00 indicates all advertising instances will be disabled */
993 cp->num_of_sets = !!instance;
996 set->handle = instance;
998 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
1000 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1001 size, data, HCI_CMD_TIMEOUT);
1004 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1005 bdaddr_t *random_addr)
1007 struct hci_cp_le_set_adv_set_rand_addr cp;
1011 /* Instance 0x00 doesn't have an adv_info, instead it uses
1012 * hdev->random_addr to track its address so whenever it needs
1013 * to be updated this also set the random address since
1014 * hdev->random_addr is shared with scan state machine.
1016 err = hci_set_random_addr_sync(hdev, random_addr);
1021 memset(&cp, 0, sizeof(cp));
1023 cp.handle = instance;
1024 bacpy(&cp.bdaddr, random_addr);
1026 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1027 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1030 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1032 struct hci_cp_le_set_ext_adv_params cp;
1035 bdaddr_t random_addr;
1038 struct adv_info *adv;
1042 adv = hci_find_adv_instance(hdev, instance);
1049 /* Updating parameters of an active instance will return a
1050 * Command Disallowed error, so we must first disable the
1051 * instance if it is active.
1053 if (adv && !adv->pending) {
1054 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1059 flags = hci_adv_instance_flags(hdev, instance);
1061 /* If the "connectable" instance flag was not set, then choose between
1062 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1064 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1065 mgmt_get_connectable(hdev);
1067 if (!is_advertising_allowed(hdev, connectable))
1070 /* Set require_privacy to true only when non-connectable
1071 * advertising is used. In that case it is fine to use a
1072 * non-resolvable private address.
1074 err = hci_get_random_address(hdev, !connectable,
1075 adv_use_rpa(hdev, flags), adv,
1076 &own_addr_type, &random_addr);
1080 memset(&cp, 0, sizeof(cp));
1083 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
1084 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
1085 cp.tx_power = adv->tx_power;
1087 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1088 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1089 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
1092 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
1096 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
1098 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
1099 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1100 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
1102 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
1104 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
1107 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
1109 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
1112 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
1113 * contains the peer’s Identity Address and the Peer_Address_Type
1114 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
1115 * These parameters are used to locate the corresponding local IRK in
1116 * the resolving list; this IRK is used to generate their own address
1117 * used in the advertisement.
1119 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
1120 hci_copy_identity_address(hdev, &cp.peer_addr,
1121 &cp.peer_addr_type);
1123 cp.own_addr_type = own_addr_type;
1124 cp.channel_map = hdev->le_adv_channel_map;
1125 cp.handle = instance;
1127 if (flags & MGMT_ADV_FLAG_SEC_2M) {
1128 cp.primary_phy = HCI_ADV_PHY_1M;
1129 cp.secondary_phy = HCI_ADV_PHY_2M;
1130 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
1131 cp.primary_phy = HCI_ADV_PHY_CODED;
1132 cp.secondary_phy = HCI_ADV_PHY_CODED;
1134 /* In all other cases use 1M */
1135 cp.primary_phy = HCI_ADV_PHY_1M;
1136 cp.secondary_phy = HCI_ADV_PHY_1M;
1139 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1140 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1144 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
1145 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
1146 bacmp(&random_addr, BDADDR_ANY)) {
1147 /* Check if random address need to be updated */
1149 if (!bacmp(&random_addr, &adv->random_addr))
1152 if (!bacmp(&random_addr, &hdev->random_addr))
1156 return hci_set_adv_set_random_addr_sync(hdev, instance,
1163 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1166 struct hci_cp_le_set_ext_scan_rsp_data cp;
1167 u8 data[HCI_MAX_EXT_AD_LENGTH];
1170 struct adv_info *adv = NULL;
1173 memset(&pdu, 0, sizeof(pdu));
1176 adv = hci_find_adv_instance(hdev, instance);
1177 if (!adv || !adv->scan_rsp_changed)
1181 len = eir_create_scan_rsp(hdev, instance, pdu.data);
1183 pdu.cp.handle = instance;
1184 pdu.cp.length = len;
1185 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1186 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1188 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1189 sizeof(pdu.cp) + len, &pdu.cp,
1195 adv->scan_rsp_changed = false;
1197 memcpy(hdev->scan_rsp_data, pdu.data, len);
1198 hdev->scan_rsp_data_len = len;
1204 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1206 struct hci_cp_le_set_scan_rsp_data cp;
1209 memset(&cp, 0, sizeof(cp));
1211 len = eir_create_scan_rsp(hdev, instance, cp.data);
1214 /* Advertising scan response data is handled in bluez.
1215 * This value will be updated only when application request the update
1216 * using adapter_set_scan_rsp_data()
1221 if (hdev->scan_rsp_data_len == len &&
1222 !memcmp(cp.data, hdev->scan_rsp_data, len))
1225 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1226 hdev->scan_rsp_data_len = len;
1230 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1231 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1234 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1236 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1239 if (ext_adv_capable(hdev))
1240 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1242 return __hci_set_scan_rsp_data_sync(hdev, instance);
1245 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1247 struct hci_cp_le_set_ext_adv_enable *cp;
1248 struct hci_cp_ext_adv_set *set;
1249 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1250 struct adv_info *adv;
1253 adv = hci_find_adv_instance(hdev, instance);
1256 /* If already enabled there is nothing to do */
1264 set = (void *)cp->data;
1266 memset(cp, 0, sizeof(*cp));
1269 cp->num_of_sets = 0x01;
1271 memset(set, 0, sizeof(*set));
1273 set->handle = instance;
1275 /* Set duration per instance since controller is responsible for
1278 if (adv && adv->timeout) {
1279 u16 duration = adv->timeout * MSEC_PER_SEC;
1281 /* Time = N * 10 ms */
1282 set->duration = cpu_to_le16(duration / 10);
1285 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1287 sizeof(*set) * cp->num_of_sets,
1288 data, HCI_CMD_TIMEOUT);
1291 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1295 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1299 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1303 return hci_enable_ext_advertising_sync(hdev, instance);
1306 static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1308 struct hci_cp_le_set_per_adv_enable cp;
1310 /* If periodic advertising already disabled there is nothing to do. */
1311 if (!hci_dev_test_flag(hdev, HCI_LE_PER_ADV))
1314 memset(&cp, 0, sizeof(cp));
1317 cp.handle = instance;
1319 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1320 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1323 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1324 u16 min_interval, u16 max_interval)
1326 struct hci_cp_le_set_per_adv_params cp;
1328 memset(&cp, 0, sizeof(cp));
1331 min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1334 max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1336 cp.handle = instance;
1337 cp.min_interval = cpu_to_le16(min_interval);
1338 cp.max_interval = cpu_to_le16(max_interval);
1339 cp.periodic_properties = 0x0000;
1341 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1342 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1345 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1348 struct hci_cp_le_set_per_adv_data cp;
1349 u8 data[HCI_MAX_PER_AD_LENGTH];
1353 memset(&pdu, 0, sizeof(pdu));
1356 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1358 if (!adv || !adv->periodic)
1362 len = eir_create_per_adv_data(hdev, instance, pdu.data);
1364 pdu.cp.length = len;
1365 pdu.cp.handle = instance;
1366 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1368 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1369 sizeof(pdu.cp) + len, &pdu,
1373 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1375 struct hci_cp_le_set_per_adv_enable cp;
1377 /* If periodic advertising already enabled there is nothing to do. */
1378 if (hci_dev_test_flag(hdev, HCI_LE_PER_ADV))
1381 memset(&cp, 0, sizeof(cp));
1384 cp.handle = instance;
1386 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1387 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1390 /* Checks if periodic advertising data contains a Basic Announcement and if it
1391 * does generates a Broadcast ID and add Broadcast Announcement.
1393 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1398 /* Skip if NULL adv as instance 0x00 is used for general purpose
1399 * advertising so it cannot used for the likes of Broadcast Announcement
1400 * as it can be overwritten at any point.
1405 /* Check if PA data doesn't contains a Basic Audio Announcement then
1406 * there is nothing to do.
1408 if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1412 /* Check if advertising data already has a Broadcast Announcement since
1413 * the process may want to control the Broadcast ID directly and in that
1414 * case the kernel shall no interfere.
1416 if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1420 /* Generate Broadcast ID */
1421 get_random_bytes(bid, sizeof(bid));
1422 eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1423 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1425 return hci_update_adv_data_sync(hdev, adv->instance);
1428 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1429 u8 *data, u32 flags, u16 min_interval,
1430 u16 max_interval, u16 sync_interval)
1432 struct adv_info *adv = NULL;
1436 hci_disable_per_advertising_sync(hdev, instance);
1439 adv = hci_find_adv_instance(hdev, instance);
1440 /* Create an instance if that could not be found */
1442 adv = hci_add_per_instance(hdev, instance, flags,
1447 return PTR_ERR(adv);
1452 /* Only start advertising if instance 0 or if a dedicated instance has
1455 if (!adv || added) {
1456 err = hci_start_ext_adv_sync(hdev, instance);
1460 err = hci_adv_bcast_annoucement(hdev, adv);
1465 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1470 err = hci_set_per_adv_data_sync(hdev, instance);
1474 err = hci_enable_per_advertising_sync(hdev, instance);
1482 hci_remove_adv_instance(hdev, instance);
1487 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1491 if (ext_adv_capable(hdev))
1492 return hci_start_ext_adv_sync(hdev, instance);
1494 err = hci_update_adv_data_sync(hdev, instance);
1498 err = hci_update_scan_rsp_data_sync(hdev, instance);
1502 return hci_enable_advertising_sync(hdev);
1505 int hci_enable_advertising_sync(struct hci_dev *hdev)
1507 struct adv_info *adv_instance;
1508 struct hci_cp_le_set_adv_param cp;
1509 u8 own_addr_type, enable = 0x01;
1511 u16 adv_min_interval, adv_max_interval;
1515 if (ext_adv_capable(hdev))
1516 return hci_enable_ext_advertising_sync(hdev,
1517 hdev->cur_adv_instance);
1519 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1520 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1522 /* If the "connectable" instance flag was not set, then choose between
1523 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1525 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1526 mgmt_get_connectable(hdev);
1528 if (!is_advertising_allowed(hdev, connectable))
1531 status = hci_disable_advertising_sync(hdev);
1535 /* Clear the HCI_LE_ADV bit temporarily so that the
1536 * hci_update_random_address knows that it's safe to go ahead
1537 * and write a new random address. The flag will be set back on
1538 * as soon as the SET_ADV_ENABLE HCI command completes.
1540 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1542 /* Set require_privacy to true only when non-connectable
1543 * advertising is used. In that case it is fine to use a
1544 * non-resolvable private address.
1546 status = hci_update_random_address_sync(hdev, !connectable,
1547 adv_use_rpa(hdev, flags),
1552 memset(&cp, 0, sizeof(cp));
1555 adv_min_interval = adv_instance->min_interval;
1556 adv_max_interval = adv_instance->max_interval;
1558 adv_min_interval = hdev->le_adv_min_interval;
1559 adv_max_interval = hdev->le_adv_max_interval;
1563 cp.filter_policy = hdev->adv_filter_policy;
1564 cp.type = hdev->adv_type;
1568 cp.type = LE_ADV_IND;
1570 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1571 cp.type = LE_ADV_SCAN_IND;
1573 cp.type = LE_ADV_NONCONN_IND;
1575 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1576 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1577 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1578 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1582 cp.min_interval = cpu_to_le16(adv_min_interval);
1583 cp.max_interval = cpu_to_le16(adv_max_interval);
1584 cp.own_address_type = own_addr_type;
1585 cp.channel_map = hdev->le_adv_channel_map;
1587 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1588 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1592 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1593 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1596 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1598 return hci_enable_advertising_sync(hdev);
1601 int hci_enable_advertising(struct hci_dev *hdev)
1603 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1604 list_empty(&hdev->adv_instances))
1607 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1610 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1615 if (!ext_adv_capable(hdev))
1618 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1622 /* If request specifies an instance that doesn't exist, fail */
1623 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1626 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1627 sizeof(instance), &instance, 0,
1628 HCI_CMD_TIMEOUT, sk);
1631 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1633 struct adv_info *adv = data;
1637 instance = adv->instance;
1639 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1642 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1644 struct adv_info *adv = NULL;
1647 adv = hci_find_adv_instance(hdev, instance);
1652 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1655 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1657 struct hci_cp_le_term_big cp;
1659 memset(&cp, 0, sizeof(cp));
1663 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1664 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1667 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1670 struct hci_cp_le_set_ext_adv_data cp;
1671 u8 data[HCI_MAX_EXT_AD_LENGTH];
1674 struct adv_info *adv = NULL;
1677 memset(&pdu, 0, sizeof(pdu));
1680 adv = hci_find_adv_instance(hdev, instance);
1681 if (!adv || !adv->adv_data_changed)
1685 len = eir_create_adv_data(hdev, instance, pdu.data);
1687 pdu.cp.length = len;
1688 pdu.cp.handle = instance;
1689 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1690 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1692 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1693 sizeof(pdu.cp) + len, &pdu.cp,
1698 /* Update data if the command succeed */
1700 adv->adv_data_changed = false;
1702 memcpy(hdev->adv_data, pdu.data, len);
1703 hdev->adv_data_len = len;
1709 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1711 struct hci_cp_le_set_adv_data cp;
1714 memset(&cp, 0, sizeof(cp));
1716 len = eir_create_adv_data(hdev, instance, cp.data);
1718 /* There's nothing to do if the data hasn't changed */
1719 if (hdev->adv_data_len == len &&
1720 memcmp(cp.data, hdev->adv_data, len) == 0)
1723 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1724 hdev->adv_data_len = len;
1728 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1729 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1732 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1734 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1738 /* Bluez will handle the advertising data including the flag and tx
1739 * power. This value will be updated only when application request the
1740 * update using adapter_set_advertising_data().
1745 if (ext_adv_capable(hdev))
1746 return hci_set_ext_adv_data_sync(hdev, instance);
1748 return hci_set_adv_data_sync(hdev, instance);
1751 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1754 struct adv_info *adv = NULL;
1757 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1760 if (hdev->adv_instance_timeout)
1763 adv = hci_find_adv_instance(hdev, instance);
1767 /* A zero timeout means unlimited advertising. As long as there is
1768 * only one instance, duration should be ignored. We still set a timeout
1769 * in case further instances are being added later on.
1771 * If the remaining lifetime of the instance is more than the duration
1772 * then the timeout corresponds to the duration, otherwise it will be
1773 * reduced to the remaining instance lifetime.
1775 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1776 timeout = adv->duration;
1778 timeout = adv->remaining_time;
1780 /* The remaining time is being reduced unless the instance is being
1781 * advertised without time limit.
1784 adv->remaining_time = adv->remaining_time - timeout;
1786 /* Only use work for scheduling instances with legacy advertising */
1787 if (!ext_adv_capable(hdev)) {
1788 hdev->adv_instance_timeout = timeout;
1789 queue_delayed_work(hdev->req_workqueue,
1790 &hdev->adv_instance_expire,
1791 msecs_to_jiffies(timeout * 1000));
1794 /* If we're just re-scheduling the same instance again then do not
1795 * execute any HCI commands. This happens when a single instance is
1798 if (!force && hdev->cur_adv_instance == instance &&
1799 hci_dev_test_flag(hdev, HCI_LE_ADV))
1802 hdev->cur_adv_instance = instance;
1804 return hci_start_adv_sync(hdev, instance);
1807 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1811 if (!ext_adv_capable(hdev))
1814 /* Disable instance 0x00 to disable all instances */
1815 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1819 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1820 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1823 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1825 struct adv_info *adv, *n;
1828 if (ext_adv_capable(hdev))
1829 /* Remove all existing sets */
1830 err = hci_clear_adv_sets_sync(hdev, sk);
1831 if (ext_adv_capable(hdev))
1834 /* This is safe as long as there is no command send while the lock is
1839 /* Cleanup non-ext instances */
1840 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1841 u8 instance = adv->instance;
1844 if (!(force || adv->timeout))
1847 err = hci_remove_adv_instance(hdev, instance);
1849 mgmt_advertising_removed(sk, hdev, instance);
1852 hci_dev_unlock(hdev);
1857 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1862 /* If we use extended advertising, instance has to be removed first. */
1863 if (ext_adv_capable(hdev))
1864 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1865 if (ext_adv_capable(hdev))
1868 /* This is safe as long as there is no command send while the lock is
1873 err = hci_remove_adv_instance(hdev, instance);
1875 mgmt_advertising_removed(sk, hdev, instance);
1877 hci_dev_unlock(hdev);
1882 /* For a single instance:
1883 * - force == true: The instance will be removed even when its remaining
1884 * lifetime is not zero.
1885 * - force == false: the instance will be deactivated but kept stored unless
1886 * the remaining lifetime is zero.
1888 * For instance == 0x00:
1889 * - force == true: All instances will be removed regardless of their timeout
1891 * - force == false: Only instances that have a timeout will be removed.
1893 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1894 u8 instance, bool force)
1896 struct adv_info *next = NULL;
1899 /* Cancel any timeout concerning the removed instance(s). */
1900 if (!instance || hdev->cur_adv_instance == instance)
1901 cancel_adv_timeout(hdev);
1903 /* Get the next instance to advertise BEFORE we remove
1904 * the current one. This can be the same instance again
1905 * if there is only one instance.
1907 if (hdev->cur_adv_instance == instance)
1908 next = hci_get_next_instance(hdev, instance);
1911 err = hci_clear_adv_sync(hdev, sk, force);
1915 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1917 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1918 /* Don't advertise a removed instance. */
1919 if (next && next->instance == instance)
1922 err = hci_remove_adv_sync(hdev, instance, sk);
1928 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1931 if (next && !ext_adv_capable(hdev))
1932 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1937 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1939 struct hci_cp_read_rssi cp;
1942 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1943 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1946 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1948 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1949 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1952 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1954 struct hci_cp_read_tx_power cp;
1958 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1959 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1962 int hci_disable_advertising_sync(struct hci_dev *hdev)
1967 /* If controller is not advertising we are done. */
1968 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1971 if (ext_adv_capable(hdev))
1972 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1973 if (ext_adv_capable(hdev))
1976 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1977 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1980 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1983 struct hci_cp_le_set_ext_scan_enable cp;
1985 memset(&cp, 0, sizeof(cp));
1988 if (hci_dev_test_flag(hdev, HCI_MESH))
1989 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
1991 cp.filter_dup = filter_dup;
1993 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1994 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1997 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2000 struct hci_cp_le_set_scan_enable cp;
2002 if (use_ext_scan(hdev))
2003 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2005 memset(&cp, 0, sizeof(cp));
2008 if (val && hci_dev_test_flag(hdev, HCI_MESH))
2009 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2011 cp.filter_dup = filter_dup;
2013 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2014 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2017 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2019 if (!use_ll_privacy(hdev))
2022 /* If controller is not/already resolving we are done. */
2023 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2026 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2027 sizeof(val), &val, HCI_CMD_TIMEOUT);
2030 static int hci_scan_disable_sync(struct hci_dev *hdev)
2034 /* If controller is not scanning we are done. */
2035 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2038 if (hdev->scanning_paused) {
2039 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2043 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2045 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2052 static bool scan_use_rpa(struct hci_dev *hdev)
2054 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2057 static void hci_start_interleave_scan(struct hci_dev *hdev)
2059 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2060 queue_delayed_work(hdev->req_workqueue,
2061 &hdev->interleave_scan, 0);
2064 static bool is_interleave_scanning(struct hci_dev *hdev)
2066 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2069 static void cancel_interleave_scan(struct hci_dev *hdev)
2071 bt_dev_dbg(hdev, "cancelling interleave scan");
2073 cancel_delayed_work_sync(&hdev->interleave_scan);
2075 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2078 /* Return true if interleave_scan wasn't started until exiting this function,
2079 * otherwise, return false
2081 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2083 /* Do interleaved scan only if all of the following are true:
2084 * - There is at least one ADV monitor
2085 * - At least one pending LE connection or one device to be scanned for
2086 * - Monitor offloading is not supported
2087 * If so, we should alternate between allowlist scan and one without
2088 * any filters to save power.
2090 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2091 !(list_empty(&hdev->pend_le_conns) &&
2092 list_empty(&hdev->pend_le_reports)) &&
2093 hci_get_adv_monitor_offload_ext(hdev) ==
2094 HCI_ADV_MONITOR_EXT_NONE;
2095 bool is_interleaving = is_interleave_scanning(hdev);
2097 if (use_interleaving && !is_interleaving) {
2098 hci_start_interleave_scan(hdev);
2099 bt_dev_dbg(hdev, "starting interleave scan");
2103 if (!use_interleaving && is_interleaving)
2104 cancel_interleave_scan(hdev);
2109 /* Removes connection to resolve list if needed.*/
2110 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2111 bdaddr_t *bdaddr, u8 bdaddr_type)
2113 struct hci_cp_le_del_from_resolv_list cp;
2114 struct bdaddr_list_with_irk *entry;
2116 if (!use_ll_privacy(hdev))
2119 /* Check if the IRK has been programmed */
2120 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2125 cp.bdaddr_type = bdaddr_type;
2126 bacpy(&cp.bdaddr, bdaddr);
2128 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2129 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2132 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2133 bdaddr_t *bdaddr, u8 bdaddr_type)
2135 struct hci_cp_le_del_from_accept_list cp;
2138 /* Check if device is on accept list before removing it */
2139 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2142 cp.bdaddr_type = bdaddr_type;
2143 bacpy(&cp.bdaddr, bdaddr);
2145 /* Ignore errors when removing from resolving list as that is likely
2146 * that the device was never added.
2148 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2150 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2151 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2153 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2157 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2163 /* Adds connection to resolve list if needed.
2164 * Setting params to NULL programs local hdev->irk
2166 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2167 struct hci_conn_params *params)
2169 struct hci_cp_le_add_to_resolv_list cp;
2170 struct smp_irk *irk;
2171 struct bdaddr_list_with_irk *entry;
2173 if (!use_ll_privacy(hdev))
2176 /* Attempt to program local identity address, type and irk if params is
2180 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2183 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2184 memcpy(cp.peer_irk, hdev->irk, 16);
2188 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2192 /* Check if the IK has _not_ been programmed yet. */
2193 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2199 cp.bdaddr_type = params->addr_type;
2200 bacpy(&cp.bdaddr, ¶ms->addr);
2201 memcpy(cp.peer_irk, irk->val, 16);
2203 /* Default privacy mode is always Network */
2204 params->privacy_mode = HCI_NETWORK_PRIVACY;
2207 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2208 memcpy(cp.local_irk, hdev->irk, 16);
2210 memset(cp.local_irk, 0, 16);
2212 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2213 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2216 /* Set Device Privacy Mode. */
2217 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2218 struct hci_conn_params *params)
2220 struct hci_cp_le_set_privacy_mode cp;
2221 struct smp_irk *irk;
2223 /* If device privacy mode has already been set there is nothing to do */
2224 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2227 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2228 * indicates that LL Privacy has been enabled and
2229 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2231 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2234 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2238 memset(&cp, 0, sizeof(cp));
2239 cp.bdaddr_type = irk->addr_type;
2240 bacpy(&cp.bdaddr, &irk->bdaddr);
2241 cp.mode = HCI_DEVICE_PRIVACY;
2243 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2244 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2247 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2248 * this attempts to program the device in the resolving list as well and
2249 * properly set the privacy mode.
2251 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2252 struct hci_conn_params *params,
2255 struct hci_cp_le_add_to_accept_list cp;
2258 /* During suspend, only wakeable devices can be in acceptlist */
2259 if (hdev->suspended &&
2260 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
2263 /* Select filter policy to accept all advertising */
2264 if (*num_entries >= hdev->le_accept_list_size)
2267 /* Accept list can not be used with RPAs */
2268 if (!use_ll_privacy(hdev) &&
2269 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type))
2272 /* Attempt to program the device in the resolving list first to avoid
2273 * having to rollback in case it fails since the resolving list is
2274 * dynamic it can probably be smaller than the accept list.
2276 err = hci_le_add_resolve_list_sync(hdev, params);
2278 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2282 /* Set Privacy Mode */
2283 err = hci_le_set_privacy_mode_sync(hdev, params);
2285 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2289 /* Check if already in accept list */
2290 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
2295 cp.bdaddr_type = params->addr_type;
2296 bacpy(&cp.bdaddr, ¶ms->addr);
2298 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2299 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2301 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2302 /* Rollback the device from the resolving list */
2303 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2307 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2313 /* This function disables/pause all advertising instances */
2314 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2319 /* If already been paused there is nothing to do. */
2320 if (hdev->advertising_paused)
2323 bt_dev_dbg(hdev, "Pausing directed advertising");
2325 /* Stop directed advertising */
2326 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2328 /* When discoverable timeout triggers, then just make sure
2329 * the limited discoverable flag is cleared. Even in the case
2330 * of a timeout triggered from general discoverable, it is
2331 * safe to unconditionally clear the flag.
2333 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2334 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2335 hdev->discov_timeout = 0;
2338 bt_dev_dbg(hdev, "Pausing advertising instances");
2340 /* Call to disable any advertisements active on the controller.
2341 * This will succeed even if no advertisements are configured.
2343 err = hci_disable_advertising_sync(hdev);
2347 /* If we are using software rotation, pause the loop */
2348 if (!ext_adv_capable(hdev))
2349 cancel_adv_timeout(hdev);
2351 hdev->advertising_paused = true;
2352 hdev->advertising_old_state = old_state;
2357 /* This function enables all user advertising instances */
2358 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2360 struct adv_info *adv, *tmp;
2363 /* If advertising has not been paused there is nothing to do. */
2364 if (!hdev->advertising_paused)
2367 /* Resume directed advertising */
2368 hdev->advertising_paused = false;
2369 if (hdev->advertising_old_state) {
2370 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2371 hdev->advertising_old_state = 0;
2374 bt_dev_dbg(hdev, "Resuming advertising instances");
2376 if (ext_adv_capable(hdev)) {
2377 /* Call for each tracked instance to be re-enabled */
2378 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2379 err = hci_enable_ext_advertising_sync(hdev,
2384 /* If the instance cannot be resumed remove it */
2385 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2389 /* Schedule for most recent instance to be restarted and begin
2390 * the software rotation loop
2392 err = hci_schedule_adv_instance_sync(hdev,
2393 hdev->cur_adv_instance,
2397 hdev->advertising_paused = false;
2402 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2406 if (!use_ll_privacy(hdev))
2409 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2412 /* Cannot disable addr resolution if scanning is enabled or
2413 * when initiating an LE connection.
2415 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2416 hci_lookup_le_connect(hdev)) {
2417 bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2421 /* Cannot disable addr resolution if advertising is enabled. */
2422 err = hci_pause_advertising_sync(hdev);
2424 bt_dev_err(hdev, "Pause advertising failed: %d", err);
2428 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2430 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2433 /* Return if address resolution is disabled and RPA is not used. */
2434 if (!err && scan_use_rpa(hdev))
2437 hci_resume_advertising_sync(hdev);
2441 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2442 bool extended, struct sock *sk)
2444 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2445 HCI_OP_READ_LOCAL_OOB_DATA;
2447 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2450 /* Device must not be scanning when updating the accept list.
2452 * Update is done using the following sequence:
2454 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
2455 * Remove Devices From Accept List ->
2456 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
2457 * Add Devices to Accept List ->
2458 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
2459 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
2462 * In case of failure advertising shall be restored to its original state and
2463 * return would disable accept list since either accept or resolving list could
2464 * not be programmed.
2467 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2469 struct hci_conn_params *params;
2470 struct bdaddr_list *b, *t;
2472 bool pend_conn, pend_report;
2476 /* Pause advertising if resolving list can be used as controllers
2477 * cannot accept resolving list modifications while advertising.
2479 if (use_ll_privacy(hdev)) {
2480 err = hci_pause_advertising_sync(hdev);
2482 bt_dev_err(hdev, "pause advertising failed: %d", err);
2487 /* Disable address resolution while reprogramming accept list since
2488 * devices that do have an IRK will be programmed in the resolving list
2489 * when LL Privacy is enabled.
2491 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2493 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2497 /* Go through the current accept list programmed into the
2498 * controller one by one and check if that address is connected or is
2499 * still in the list of pending connections or list of devices to
2500 * report. If not present in either list, then remove it from
2503 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2504 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2507 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2510 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2514 /* If the device is not likely to connect or report,
2515 * remove it from the acceptlist.
2517 if (!pend_conn && !pend_report) {
2518 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
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
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
2536 list_for_each_entry(params, &hdev->pend_le_conns, action) {
2537 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
2542 /* After adding all new pending connections, walk through
2543 * the list of pending reports and also add these to the
2544 * accept list if there is still space. Abort if space runs out.
2546 list_for_each_entry(params, &hdev->pend_le_reports, action) {
2547 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
2552 /* Use the allowlist unless the following conditions are all true:
2553 * - We are not currently suspending
2554 * - There are 1 or more ADV monitors registered and it's not offloaded
2555 * - Interleaved scanning is not currently using the allowlist
2557 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2558 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2559 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2563 filter_policy = err ? 0x00 : 0x01;
2565 /* Enable address resolution when LL Privacy is enabled. */
2566 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2568 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2570 /* Resume advertising if it was paused */
2571 if (use_ll_privacy(hdev))
2572 hci_resume_advertising_sync(hdev);
2574 /* Select filter policy to use accept list */
2575 return filter_policy;
2578 /* Returns true if an le connection is in the scanning state */
2579 static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
2581 struct hci_conn_hash *h = &hdev->conn_hash;
2586 list_for_each_entry_rcu(c, &h->list, list) {
2587 if (c->type == LE_LINK && c->state == BT_CONNECT &&
2588 test_bit(HCI_CONN_SCANNING, &c->flags)) {
2599 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2600 u16 interval, u16 window,
2601 u8 own_addr_type, u8 filter_policy)
2603 struct hci_cp_le_set_ext_scan_params *cp;
2604 struct hci_cp_le_scan_phy_params *phy;
2605 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2609 phy = (void *)cp->data;
2611 memset(data, 0, sizeof(data));
2613 cp->own_addr_type = own_addr_type;
2614 cp->filter_policy = filter_policy;
2616 if (scan_1m(hdev) || scan_2m(hdev)) {
2617 cp->scanning_phys |= LE_SCAN_PHY_1M;
2620 phy->interval = cpu_to_le16(interval);
2621 phy->window = cpu_to_le16(window);
2627 if (scan_coded(hdev)) {
2628 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2631 phy->interval = cpu_to_le16(interval);
2632 phy->window = cpu_to_le16(window);
2638 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2639 sizeof(*cp) + sizeof(*phy) * num_phy,
2640 data, HCI_CMD_TIMEOUT);
2643 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2644 u16 interval, u16 window,
2645 u8 own_addr_type, u8 filter_policy)
2647 struct hci_cp_le_set_scan_param cp;
2649 if (use_ext_scan(hdev))
2650 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2651 window, own_addr_type,
2654 memset(&cp, 0, sizeof(cp));
2656 cp.interval = cpu_to_le16(interval);
2657 cp.window = cpu_to_le16(window);
2658 cp.own_address_type = own_addr_type;
2659 cp.filter_policy = filter_policy;
2661 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2662 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2665 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2666 u16 window, u8 own_addr_type, u8 filter_policy,
2671 if (hdev->scanning_paused) {
2672 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2676 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2677 own_addr_type, filter_policy);
2681 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2684 static int hci_passive_scan_sync(struct hci_dev *hdev)
2688 u16 window, interval;
2689 u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2692 if (hdev->scanning_paused) {
2693 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2697 err = hci_scan_disable_sync(hdev);
2699 bt_dev_err(hdev, "disable scanning failed: %d", err);
2703 /* Set require_privacy to false since no SCAN_REQ are send
2704 * during passive scanning. Not using an non-resolvable address
2705 * here is important so that peer devices using direct
2706 * advertising with our address will be correctly reported
2707 * by the controller.
2709 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2713 if (hdev->enable_advmon_interleave_scan &&
2714 hci_update_interleaved_scan_sync(hdev))
2717 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2719 /* Adding or removing entries from the accept list must
2720 * happen before enabling scanning. The controller does
2721 * not allow accept list modification while scanning.
2723 filter_policy = hci_update_accept_list_sync(hdev);
2725 /* When the controller is using random resolvable addresses and
2726 * with that having LE privacy enabled, then controllers with
2727 * Extended Scanner Filter Policies support can now enable support
2728 * for handling directed advertising.
2730 * So instead of using filter polices 0x00 (no acceptlist)
2731 * and 0x01 (acceptlist enabled) use the new filter policies
2732 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2734 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2735 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2736 filter_policy |= 0x02;
2738 if (hdev->suspended) {
2739 window = hdev->le_scan_window_suspend;
2740 interval = hdev->le_scan_int_suspend;
2741 } else if (hci_is_le_conn_scanning(hdev)) {
2742 window = hdev->le_scan_window_connect;
2743 interval = hdev->le_scan_int_connect;
2744 } else if (hci_is_adv_monitoring(hdev)) {
2745 window = hdev->le_scan_window_adv_monitor;
2746 interval = hdev->le_scan_int_adv_monitor;
2748 window = hdev->le_scan_window;
2749 interval = hdev->le_scan_interval;
2752 /* Disable all filtering for Mesh */
2753 if (hci_dev_test_flag(hdev, HCI_MESH)) {
2755 filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
2758 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2760 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2761 own_addr_type, filter_policy, filter_dups);
2764 /* This function controls the passive scanning based on hdev->pend_le_conns
2765 * list. If there are pending LE connection we start the background scanning,
2766 * otherwise we stop it in the following sequence:
2768 * If there are devices to scan:
2770 * Disable Scanning -> Update Accept List ->
2771 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2772 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2779 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2783 if (!test_bit(HCI_UP, &hdev->flags) ||
2784 test_bit(HCI_INIT, &hdev->flags) ||
2785 hci_dev_test_flag(hdev, HCI_SETUP) ||
2786 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2787 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2788 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2791 /* No point in doing scanning if LE support hasn't been enabled */
2792 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2795 /* If discovery is active don't interfere with it */
2796 if (hdev->discovery.state != DISCOVERY_STOPPED)
2799 /* Reset RSSI and UUID filters when starting background scanning
2800 * since these filters are meant for service discovery only.
2802 * The Start Discovery and Start Service Discovery operations
2803 * ensure to set proper values for RSSI threshold and UUID
2804 * filter list. So it is safe to just reset them here.
2806 hci_discovery_filter_clear(hdev);
2808 bt_dev_dbg(hdev, "ADV monitoring is %s",
2809 hci_is_adv_monitoring(hdev) ? "on" : "off");
2811 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
2812 list_empty(&hdev->pend_le_conns) &&
2813 list_empty(&hdev->pend_le_reports) &&
2814 !hci_is_adv_monitoring(hdev) &&
2815 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2816 /* If there is no pending LE connections or devices
2817 * to be scanned for or no ADV monitors, we should stop the
2818 * background scanning.
2821 bt_dev_dbg(hdev, "stopping background scanning");
2823 err = hci_scan_disable_sync(hdev);
2825 bt_dev_err(hdev, "stop background scanning failed: %d",
2828 /* If there is at least one pending LE connection, we should
2829 * keep the background scan running.
2832 /* If controller is connecting, we should not start scanning
2833 * since some controllers are not able to scan and connect at
2836 if (hci_lookup_le_connect(hdev))
2839 bt_dev_dbg(hdev, "start background scanning");
2841 err = hci_passive_scan_sync(hdev);
2843 bt_dev_err(hdev, "start background scanning failed: %d",
2850 static int update_scan_sync(struct hci_dev *hdev, void *data)
2852 return hci_update_scan_sync(hdev);
2855 int hci_update_scan(struct hci_dev *hdev)
2857 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
2860 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2862 return hci_update_passive_scan_sync(hdev);
2865 int hci_update_passive_scan(struct hci_dev *hdev)
2867 /* Only queue if it would have any effect */
2868 if (!test_bit(HCI_UP, &hdev->flags) ||
2869 test_bit(HCI_INIT, &hdev->flags) ||
2870 hci_dev_test_flag(hdev, HCI_SETUP) ||
2871 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2872 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2873 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2876 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2879 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2883 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2886 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2887 sizeof(val), &val, HCI_CMD_TIMEOUT);
2891 hdev->features[1][0] |= LMP_HOST_SC;
2892 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2894 hdev->features[1][0] &= ~LMP_HOST_SC;
2895 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2902 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2906 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2907 lmp_host_ssp_capable(hdev))
2910 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
2911 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2912 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2915 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2916 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2920 return hci_write_sc_support_sync(hdev, 0x01);
2923 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
2925 struct hci_cp_write_le_host_supported cp;
2927 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
2928 !lmp_bredr_capable(hdev))
2931 /* Check first if we already have the right host state
2932 * (host features set)
2934 if (le == lmp_host_le_capable(hdev) &&
2935 simul == lmp_host_le_br_capable(hdev))
2938 memset(&cp, 0, sizeof(cp));
2943 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
2944 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2947 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
2949 struct adv_info *adv, *tmp;
2952 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2955 /* If RPA Resolution has not been enable yet it means the
2956 * resolving list is empty and we should attempt to program the
2957 * local IRK in order to support using own_addr_type
2958 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
2960 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
2961 hci_le_add_resolve_list_sync(hdev, NULL);
2962 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2965 /* Make sure the controller has a good default for
2966 * advertising data. This also applies to the case
2967 * where BR/EDR was toggled during the AUTO_OFF phase.
2969 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2970 list_empty(&hdev->adv_instances)) {
2971 if (ext_adv_capable(hdev)) {
2972 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
2974 hci_update_scan_rsp_data_sync(hdev, 0x00);
2976 err = hci_update_adv_data_sync(hdev, 0x00);
2978 hci_update_scan_rsp_data_sync(hdev, 0x00);
2981 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2982 hci_enable_advertising_sync(hdev);
2985 /* Call for each tracked instance to be scheduled */
2986 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
2987 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
2992 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
2996 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
2997 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3000 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3001 sizeof(link_sec), &link_sec,
3005 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3007 struct hci_cp_write_page_scan_activity cp;
3011 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3014 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3017 memset(&cp, 0, sizeof(cp));
3020 type = PAGE_SCAN_TYPE_INTERLACED;
3022 /* 160 msec page scan interval */
3023 cp.interval = cpu_to_le16(0x0100);
3025 type = hdev->def_page_scan_type;
3026 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3029 cp.window = cpu_to_le16(hdev->def_page_scan_window);
3031 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3032 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3033 err = __hci_cmd_sync_status(hdev,
3034 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
3035 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3040 if (hdev->page_scan_type != type)
3041 err = __hci_cmd_sync_status(hdev,
3042 HCI_OP_WRITE_PAGE_SCAN_TYPE,
3043 sizeof(type), &type,
3049 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3051 struct bdaddr_list *b;
3053 list_for_each_entry(b, &hdev->accept_list, list) {
3054 struct hci_conn *conn;
3056 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3060 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3067 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3069 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3074 int hci_update_scan_sync(struct hci_dev *hdev)
3078 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3081 if (!hdev_is_powered(hdev))
3084 if (mgmt_powering_down(hdev))
3087 if (hdev->scanning_paused)
3090 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3091 disconnected_accept_list_entries(hdev))
3094 scan = SCAN_DISABLED;
3096 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3097 scan |= SCAN_INQUIRY;
3099 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3100 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3103 return hci_write_scan_enable_sync(hdev, scan);
3106 int hci_update_name_sync(struct hci_dev *hdev)
3108 struct hci_cp_write_local_name cp;
3110 memset(&cp, 0, sizeof(cp));
3112 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3114 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3119 /* This function perform powered update HCI command sequence after the HCI init
3120 * sequence which end up resetting all states, the sequence is as follows:
3122 * HCI_SSP_ENABLED(Enable SSP)
3123 * HCI_LE_ENABLED(Enable LE)
3124 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
3126 * Enable Authentication
3127 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3128 * Set Name -> Set EIR)
3130 int hci_powered_update_sync(struct hci_dev *hdev)
3134 /* Register the available SMP channels (BR/EDR and LE) only when
3135 * successfully powering on the controller. This late
3136 * registration is required so that LE SMP can clearly decide if
3137 * the public address or static address is used.
3141 err = hci_write_ssp_mode_sync(hdev, 0x01);
3145 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3149 err = hci_powered_update_adv_sync(hdev);
3153 err = hci_write_auth_enable_sync(hdev);
3157 if (lmp_bredr_capable(hdev)) {
3158 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3159 hci_write_fast_connectable_sync(hdev, true);
3161 hci_write_fast_connectable_sync(hdev, false);
3162 hci_update_scan_sync(hdev);
3163 hci_update_class_sync(hdev);
3164 hci_update_name_sync(hdev);
3165 hci_update_eir_sync(hdev);
3172 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3173 * (BD_ADDR) for a HCI device from
3174 * a firmware node property.
3175 * @hdev: The HCI device
3177 * Search the firmware node for 'local-bd-address'.
3179 * All-zero BD addresses are rejected, because those could be properties
3180 * that exist in the firmware tables, but were not updated by the firmware. For
3181 * example, the DTS could define 'local-bd-address', with zero BD addresses.
3183 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3185 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3189 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3190 (u8 *)&ba, sizeof(ba));
3191 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3194 bacpy(&hdev->public_addr, &ba);
3197 struct hci_init_stage {
3198 int (*func)(struct hci_dev *hdev);
3201 /* Run init stage NULL terminated function table */
3202 static int hci_init_stage_sync(struct hci_dev *hdev,
3203 const struct hci_init_stage *stage)
3207 for (i = 0; stage[i].func; i++) {
3210 err = stage[i].func(hdev);
3218 /* Read Local Version */
3219 static int hci_read_local_version_sync(struct hci_dev *hdev)
3221 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3222 0, NULL, HCI_CMD_TIMEOUT);
3225 /* Read BD Address */
3226 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3228 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3229 0, NULL, HCI_CMD_TIMEOUT);
3232 #define HCI_INIT(_func) \
3237 static const struct hci_init_stage hci_init0[] = {
3238 /* HCI_OP_READ_LOCAL_VERSION */
3239 HCI_INIT(hci_read_local_version_sync),
3240 /* HCI_OP_READ_BD_ADDR */
3241 HCI_INIT(hci_read_bd_addr_sync),
3245 int hci_reset_sync(struct hci_dev *hdev)
3249 set_bit(HCI_RESET, &hdev->flags);
3251 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3259 static int hci_init0_sync(struct hci_dev *hdev)
3263 bt_dev_dbg(hdev, "");
3266 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3267 err = hci_reset_sync(hdev);
3272 return hci_init_stage_sync(hdev, hci_init0);
3275 static int hci_unconf_init_sync(struct hci_dev *hdev)
3279 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3282 err = hci_init0_sync(hdev);
3286 if (hci_dev_test_flag(hdev, HCI_SETUP))
3287 hci_debugfs_create_basic(hdev);
3292 /* Read Local Supported Features. */
3293 static int hci_read_local_features_sync(struct hci_dev *hdev)
3295 /* Not all AMP controllers support this command */
3296 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
3299 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3300 0, NULL, HCI_CMD_TIMEOUT);
3303 /* BR Controller init stage 1 command sequence */
3304 static const struct hci_init_stage br_init1[] = {
3305 /* HCI_OP_READ_LOCAL_FEATURES */
3306 HCI_INIT(hci_read_local_features_sync),
3307 /* HCI_OP_READ_LOCAL_VERSION */
3308 HCI_INIT(hci_read_local_version_sync),
3309 /* HCI_OP_READ_BD_ADDR */
3310 HCI_INIT(hci_read_bd_addr_sync),
3314 /* Read Local Commands */
3315 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3317 /* All Bluetooth 1.2 and later controllers should support the
3318 * HCI command for reading the local supported commands.
3320 * Unfortunately some controllers indicate Bluetooth 1.2 support,
3321 * but do not have support for this command. If that is the case,
3322 * the driver can quirk the behavior and skip reading the local
3323 * supported commands.
3325 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3326 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3327 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3328 0, NULL, HCI_CMD_TIMEOUT);
3333 /* Read Local AMP Info */
3334 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
3336 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
3337 0, NULL, HCI_CMD_TIMEOUT);
3340 /* Read Data Blk size */
3341 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
3343 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
3344 0, NULL, HCI_CMD_TIMEOUT);
3347 /* Read Flow Control Mode */
3348 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
3350 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
3351 0, NULL, HCI_CMD_TIMEOUT);
3354 /* Read Location Data */
3355 static int hci_read_location_data_sync(struct hci_dev *hdev)
3357 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
3358 0, NULL, HCI_CMD_TIMEOUT);
3361 /* AMP Controller init stage 1 command sequence */
3362 static const struct hci_init_stage amp_init1[] = {
3363 /* HCI_OP_READ_LOCAL_VERSION */
3364 HCI_INIT(hci_read_local_version_sync),
3365 /* HCI_OP_READ_LOCAL_COMMANDS */
3366 HCI_INIT(hci_read_local_cmds_sync),
3367 /* HCI_OP_READ_LOCAL_AMP_INFO */
3368 HCI_INIT(hci_read_local_amp_info_sync),
3369 /* HCI_OP_READ_DATA_BLOCK_SIZE */
3370 HCI_INIT(hci_read_data_block_size_sync),
3371 /* HCI_OP_READ_FLOW_CONTROL_MODE */
3372 HCI_INIT(hci_read_flow_control_mode_sync),
3373 /* HCI_OP_READ_LOCATION_DATA */
3374 HCI_INIT(hci_read_location_data_sync),
3378 static int hci_init1_sync(struct hci_dev *hdev)
3382 bt_dev_dbg(hdev, "");
3385 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3386 err = hci_reset_sync(hdev);
3391 switch (hdev->dev_type) {
3393 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3394 return hci_init_stage_sync(hdev, br_init1);
3396 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3397 return hci_init_stage_sync(hdev, amp_init1);
3399 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
3406 /* AMP Controller init stage 2 command sequence */
3407 static const struct hci_init_stage amp_init2[] = {
3408 /* HCI_OP_READ_LOCAL_FEATURES */
3409 HCI_INIT(hci_read_local_features_sync),
3413 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
3414 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3416 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3417 0, NULL, HCI_CMD_TIMEOUT);
3420 /* Read Class of Device */
3421 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3423 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3424 0, NULL, HCI_CMD_TIMEOUT);
3427 /* Read Local Name */
3428 static int hci_read_local_name_sync(struct hci_dev *hdev)
3430 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3431 0, NULL, HCI_CMD_TIMEOUT);
3434 /* Read Voice Setting */
3435 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3437 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3438 0, NULL, HCI_CMD_TIMEOUT);
3441 /* Read Number of Supported IAC */
3442 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3444 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3445 0, NULL, HCI_CMD_TIMEOUT);
3448 /* Read Current IAC LAP */
3449 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3451 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3452 0, NULL, HCI_CMD_TIMEOUT);
3455 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3456 u8 cond_type, bdaddr_t *bdaddr,
3459 struct hci_cp_set_event_filter cp;
3461 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3464 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3467 memset(&cp, 0, sizeof(cp));
3468 cp.flt_type = flt_type;
3470 if (flt_type != HCI_FLT_CLEAR_ALL) {
3471 cp.cond_type = cond_type;
3472 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3473 cp.addr_conn_flt.auto_accept = auto_accept;
3476 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3477 flt_type == HCI_FLT_CLEAR_ALL ?
3478 sizeof(cp.flt_type) : sizeof(cp), &cp,
3482 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3484 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3487 /* In theory the state machine should not reach here unless
3488 * a hci_set_event_filter_sync() call succeeds, but we do
3489 * the check both for parity and as a future reminder.
3491 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3494 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3498 /* Connection accept timeout ~20 secs */
3499 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3501 __le16 param = cpu_to_le16(0x7d00);
3503 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3504 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
3507 /* BR Controller init stage 2 command sequence */
3508 static const struct hci_init_stage br_init2[] = {
3509 /* HCI_OP_READ_BUFFER_SIZE */
3510 HCI_INIT(hci_read_buffer_size_sync),
3511 /* HCI_OP_READ_CLASS_OF_DEV */
3512 HCI_INIT(hci_read_dev_class_sync),
3513 /* HCI_OP_READ_LOCAL_NAME */
3514 HCI_INIT(hci_read_local_name_sync),
3515 /* HCI_OP_READ_VOICE_SETTING */
3516 HCI_INIT(hci_read_voice_setting_sync),
3517 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
3518 HCI_INIT(hci_read_num_supported_iac_sync),
3519 /* HCI_OP_READ_CURRENT_IAC_LAP */
3520 HCI_INIT(hci_read_current_iac_lap_sync),
3521 /* HCI_OP_SET_EVENT_FLT */
3522 HCI_INIT(hci_clear_event_filter_sync),
3523 /* HCI_OP_WRITE_CA_TIMEOUT */
3524 HCI_INIT(hci_write_ca_timeout_sync),
3528 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3532 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3535 /* When SSP is available, then the host features page
3536 * should also be available as well. However some
3537 * controllers list the max_page as 0 as long as SSP
3538 * has not been enabled. To achieve proper debugging
3539 * output, force the minimum max_page to 1 at least.
3541 hdev->max_page = 0x01;
3543 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3544 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3547 static int hci_write_eir_sync(struct hci_dev *hdev)
3549 struct hci_cp_write_eir cp;
3551 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3554 memset(hdev->eir, 0, sizeof(hdev->eir));
3555 memset(&cp, 0, sizeof(cp));
3557 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3561 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3565 if (!lmp_inq_rssi_capable(hdev) &&
3566 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3569 /* If Extended Inquiry Result events are supported, then
3570 * they are clearly preferred over Inquiry Result with RSSI
3573 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3575 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3576 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3579 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3581 if (!lmp_inq_tx_pwr_capable(hdev))
3584 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3585 0, NULL, HCI_CMD_TIMEOUT);
3588 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3590 struct hci_cp_read_local_ext_features cp;
3592 if (!lmp_ext_feat_capable(hdev))
3595 memset(&cp, 0, sizeof(cp));
3598 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3599 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3602 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3604 return hci_read_local_ext_features_sync(hdev, 0x01);
3607 /* HCI Controller init stage 2 command sequence */
3608 static const struct hci_init_stage hci_init2[] = {
3609 /* HCI_OP_READ_LOCAL_COMMANDS */
3610 HCI_INIT(hci_read_local_cmds_sync),
3611 /* HCI_OP_WRITE_SSP_MODE */
3612 HCI_INIT(hci_write_ssp_mode_1_sync),
3613 /* HCI_OP_WRITE_EIR */
3614 HCI_INIT(hci_write_eir_sync),
3615 /* HCI_OP_WRITE_INQUIRY_MODE */
3616 HCI_INIT(hci_write_inquiry_mode_sync),
3617 /* HCI_OP_READ_INQ_RSP_TX_POWER */
3618 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3619 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3620 HCI_INIT(hci_read_local_ext_features_1_sync),
3621 /* HCI_OP_WRITE_AUTH_ENABLE */
3622 HCI_INIT(hci_write_auth_enable_sync),
3626 /* Read LE Buffer Size */
3627 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3629 /* Use Read LE Buffer Size V2 if supported */
3630 if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3631 return __hci_cmd_sync_status(hdev,
3632 HCI_OP_LE_READ_BUFFER_SIZE_V2,
3633 0, NULL, HCI_CMD_TIMEOUT);
3635 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3636 0, NULL, HCI_CMD_TIMEOUT);
3639 /* Read LE Local Supported Features */
3640 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3642 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3643 0, NULL, HCI_CMD_TIMEOUT);
3646 /* Read LE Supported States */
3647 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3649 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3650 0, NULL, HCI_CMD_TIMEOUT);
3653 /* LE Controller init stage 2 command sequence */
3654 static const struct hci_init_stage le_init2[] = {
3655 /* HCI_OP_LE_READ_LOCAL_FEATURES */
3656 HCI_INIT(hci_le_read_local_features_sync),
3657 /* HCI_OP_LE_READ_BUFFER_SIZE */
3658 HCI_INIT(hci_le_read_buffer_size_sync),
3659 /* HCI_OP_LE_READ_SUPPORTED_STATES */
3660 HCI_INIT(hci_le_read_supported_states_sync),
3664 static int hci_init2_sync(struct hci_dev *hdev)
3668 bt_dev_dbg(hdev, "");
3670 if (hdev->dev_type == HCI_AMP)
3671 return hci_init_stage_sync(hdev, amp_init2);
3673 err = hci_init_stage_sync(hdev, hci_init2);
3677 if (lmp_bredr_capable(hdev)) {
3678 err = hci_init_stage_sync(hdev, br_init2);
3682 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3685 if (lmp_le_capable(hdev)) {
3686 err = hci_init_stage_sync(hdev, le_init2);
3689 /* LE-only controllers have LE implicitly enabled */
3690 if (!lmp_bredr_capable(hdev))
3691 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3697 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3699 /* The second byte is 0xff instead of 0x9f (two reserved bits
3700 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3701 * command otherwise.
3703 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3705 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3706 * any event mask for pre 1.2 devices.
3708 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3711 if (lmp_bredr_capable(hdev)) {
3712 events[4] |= 0x01; /* Flow Specification Complete */
3714 /* Don't set Disconnect Complete when suspended as that
3715 * would wakeup the host when disconnecting due to
3718 if (hdev->suspended)
3721 /* Use a different default for LE-only devices */
3722 memset(events, 0, sizeof(events));
3723 events[1] |= 0x20; /* Command Complete */
3724 events[1] |= 0x40; /* Command Status */
3725 events[1] |= 0x80; /* Hardware Error */
3727 /* If the controller supports the Disconnect command, enable
3728 * the corresponding event. In addition enable packet flow
3729 * control related events.
3731 if (hdev->commands[0] & 0x20) {
3732 /* Don't set Disconnect Complete when suspended as that
3733 * would wakeup the host when disconnecting due to
3736 if (!hdev->suspended)
3737 events[0] |= 0x10; /* Disconnection Complete */
3738 events[2] |= 0x04; /* Number of Completed Packets */
3739 events[3] |= 0x02; /* Data Buffer Overflow */
3742 /* If the controller supports the Read Remote Version
3743 * Information command, enable the corresponding event.
3745 if (hdev->commands[2] & 0x80)
3746 events[1] |= 0x08; /* Read Remote Version Information
3750 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3751 events[0] |= 0x80; /* Encryption Change */
3752 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3756 if (lmp_inq_rssi_capable(hdev) ||
3757 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3758 events[4] |= 0x02; /* Inquiry Result with RSSI */
3760 if (lmp_ext_feat_capable(hdev))
3761 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3763 if (lmp_esco_capable(hdev)) {
3764 events[5] |= 0x08; /* Synchronous Connection Complete */
3765 events[5] |= 0x10; /* Synchronous Connection Changed */
3768 if (lmp_sniffsubr_capable(hdev))
3769 events[5] |= 0x20; /* Sniff Subrating */
3771 if (lmp_pause_enc_capable(hdev))
3772 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3774 if (lmp_ext_inq_capable(hdev))
3775 events[5] |= 0x40; /* Extended Inquiry Result */
3777 if (lmp_no_flush_capable(hdev))
3778 events[7] |= 0x01; /* Enhanced Flush Complete */
3780 if (lmp_lsto_capable(hdev))
3781 events[6] |= 0x80; /* Link Supervision Timeout Changed */
3783 if (lmp_ssp_capable(hdev)) {
3784 events[6] |= 0x01; /* IO Capability Request */
3785 events[6] |= 0x02; /* IO Capability Response */
3786 events[6] |= 0x04; /* User Confirmation Request */
3787 events[6] |= 0x08; /* User Passkey Request */
3788 events[6] |= 0x10; /* Remote OOB Data Request */
3789 events[6] |= 0x20; /* Simple Pairing Complete */
3790 events[7] |= 0x04; /* User Passkey Notification */
3791 events[7] |= 0x08; /* Keypress Notification */
3792 events[7] |= 0x10; /* Remote Host Supported
3793 * Features Notification
3797 if (lmp_le_capable(hdev))
3798 events[7] |= 0x20; /* LE Meta-Event */
3800 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3801 sizeof(events), events, HCI_CMD_TIMEOUT);
3804 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3806 struct hci_cp_read_stored_link_key cp;
3808 if (!(hdev->commands[6] & 0x20) ||
3809 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3812 memset(&cp, 0, sizeof(cp));
3813 bacpy(&cp.bdaddr, BDADDR_ANY);
3816 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3817 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3820 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3822 struct hci_cp_write_def_link_policy cp;
3823 u16 link_policy = 0;
3825 if (!(hdev->commands[5] & 0x10))
3828 memset(&cp, 0, sizeof(cp));
3830 if (lmp_rswitch_capable(hdev))
3831 link_policy |= HCI_LP_RSWITCH;
3832 if (lmp_hold_capable(hdev))
3833 link_policy |= HCI_LP_HOLD;
3834 if (lmp_sniff_capable(hdev))
3835 link_policy |= HCI_LP_SNIFF;
3836 if (lmp_park_capable(hdev))
3837 link_policy |= HCI_LP_PARK;
3839 cp.policy = cpu_to_le16(link_policy);
3841 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3842 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3845 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3847 if (!(hdev->commands[8] & 0x01))
3850 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3851 0, NULL, HCI_CMD_TIMEOUT);
3854 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3856 if (!(hdev->commands[18] & 0x04) ||
3857 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
3858 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3861 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3862 0, NULL, HCI_CMD_TIMEOUT);
3865 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3867 /* Some older Broadcom based Bluetooth 1.2 controllers do not
3868 * support the Read Page Scan Type command. Check support for
3869 * this command in the bit mask of supported commands.
3871 if (!(hdev->commands[13] & 0x01))
3874 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3875 0, NULL, HCI_CMD_TIMEOUT);
3878 /* Read features beyond page 1 if available */
3879 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3884 if (!lmp_ext_feat_capable(hdev))
3887 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3889 err = hci_read_local_ext_features_sync(hdev, page);
3897 /* HCI Controller init stage 3 command sequence */
3898 static const struct hci_init_stage hci_init3[] = {
3899 /* HCI_OP_SET_EVENT_MASK */
3900 HCI_INIT(hci_set_event_mask_sync),
3901 /* HCI_OP_READ_STORED_LINK_KEY */
3902 HCI_INIT(hci_read_stored_link_key_sync),
3903 /* HCI_OP_WRITE_DEF_LINK_POLICY */
3904 HCI_INIT(hci_setup_link_policy_sync),
3905 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
3906 HCI_INIT(hci_read_page_scan_activity_sync),
3907 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
3908 HCI_INIT(hci_read_def_err_data_reporting_sync),
3909 /* HCI_OP_READ_PAGE_SCAN_TYPE */
3910 HCI_INIT(hci_read_page_scan_type_sync),
3911 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3912 HCI_INIT(hci_read_local_ext_features_all_sync),
3916 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
3920 if (!lmp_le_capable(hdev))
3923 memset(events, 0, sizeof(events));
3925 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
3926 events[0] |= 0x10; /* LE Long Term Key Request */
3928 /* If controller supports the Connection Parameters Request
3929 * Link Layer Procedure, enable the corresponding event.
3931 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
3932 /* LE Remote Connection Parameter Request */
3935 /* If the controller supports the Data Length Extension
3936 * feature, enable the corresponding event.
3938 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
3939 events[0] |= 0x40; /* LE Data Length Change */
3941 /* If the controller supports LL Privacy feature or LE Extended Adv,
3942 * enable the corresponding event.
3944 if (use_enhanced_conn_complete(hdev))
3945 events[1] |= 0x02; /* LE Enhanced Connection Complete */
3947 /* If the controller supports Extended Scanner Filter
3948 * Policies, enable the corresponding event.
3950 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
3951 events[1] |= 0x04; /* LE Direct Advertising Report */
3953 /* If the controller supports Channel Selection Algorithm #2
3954 * feature, enable the corresponding event.
3956 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
3957 events[2] |= 0x08; /* LE Channel Selection Algorithm */
3959 /* If the controller supports the LE Set Scan Enable command,
3960 * enable the corresponding advertising report event.
3962 if (hdev->commands[26] & 0x08)
3963 events[0] |= 0x02; /* LE Advertising Report */
3965 /* If the controller supports the LE Create Connection
3966 * command, enable the corresponding event.
3968 if (hdev->commands[26] & 0x10)
3969 events[0] |= 0x01; /* LE Connection Complete */
3971 /* If the controller supports the LE Connection Update
3972 * command, enable the corresponding event.
3974 if (hdev->commands[27] & 0x04)
3975 events[0] |= 0x04; /* LE Connection Update Complete */
3977 /* If the controller supports the LE Read Remote Used Features
3978 * command, enable the corresponding event.
3980 if (hdev->commands[27] & 0x20)
3981 /* LE Read Remote Used Features Complete */
3984 /* If the controller supports the LE Read Local P-256
3985 * Public Key command, enable the corresponding event.
3987 if (hdev->commands[34] & 0x02)
3988 /* LE Read Local P-256 Public Key Complete */
3991 /* If the controller supports the LE Generate DHKey
3992 * command, enable the corresponding event.
3994 if (hdev->commands[34] & 0x04)
3995 events[1] |= 0x01; /* LE Generate DHKey Complete */
3997 /* If the controller supports the LE Set Default PHY or
3998 * LE Set PHY commands, enable the corresponding event.
4000 if (hdev->commands[35] & (0x20 | 0x40))
4001 events[1] |= 0x08; /* LE PHY Update Complete */
4003 /* If the controller supports LE Set Extended Scan Parameters
4004 * and LE Set Extended Scan Enable commands, enable the
4005 * corresponding event.
4007 if (use_ext_scan(hdev))
4008 events[1] |= 0x10; /* LE Extended Advertising Report */
4010 /* If the controller supports the LE Extended Advertising
4011 * command, enable the corresponding event.
4013 if (ext_adv_capable(hdev))
4014 events[2] |= 0x02; /* LE Advertising Set Terminated */
4016 if (cis_capable(hdev)) {
4017 events[3] |= 0x01; /* LE CIS Established */
4018 if (cis_peripheral_capable(hdev))
4019 events[3] |= 0x02; /* LE CIS Request */
4022 if (bis_capable(hdev)) {
4023 events[3] |= 0x04; /* LE Create BIG Complete */
4024 events[3] |= 0x08; /* LE Terminate BIG Complete */
4025 events[3] |= 0x10; /* LE BIG Sync Established */
4026 events[3] |= 0x20; /* LE BIG Sync Loss */
4029 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4030 sizeof(events), events, HCI_CMD_TIMEOUT);
4033 /* Read LE Advertising Channel TX Power */
4034 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4036 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4037 /* HCI TS spec forbids mixing of legacy and extended
4038 * advertising commands wherein READ_ADV_TX_POWER is
4039 * also included. So do not call it if extended adv
4040 * is supported otherwise controller will return
4041 * COMMAND_DISALLOWED for extended commands.
4043 return __hci_cmd_sync_status(hdev,
4044 HCI_OP_LE_READ_ADV_TX_POWER,
4045 0, NULL, HCI_CMD_TIMEOUT);
4051 /* Read LE Min/Max Tx Power*/
4052 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4054 if (!(hdev->commands[38] & 0x80) ||
4055 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4058 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4059 0, NULL, HCI_CMD_TIMEOUT);
4062 /* Read LE Accept List Size */
4063 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4065 if (!(hdev->commands[26] & 0x40))
4068 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4069 0, NULL, HCI_CMD_TIMEOUT);
4072 /* Clear LE Accept List */
4073 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
4075 if (!(hdev->commands[26] & 0x80))
4078 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
4082 /* Read LE Resolving List Size */
4083 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4085 if (!(hdev->commands[34] & 0x40))
4088 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4089 0, NULL, HCI_CMD_TIMEOUT);
4092 /* Clear LE Resolving List */
4093 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4095 if (!(hdev->commands[34] & 0x20))
4098 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4102 /* Set RPA timeout */
4103 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4105 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4107 if (!(hdev->commands[35] & 0x04) ||
4108 test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4111 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4112 sizeof(timeout), &timeout,
4116 /* Read LE Maximum Data Length */
4117 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4119 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4122 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4126 /* Read LE Suggested Default Data Length */
4127 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4129 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4132 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4136 /* Read LE Number of Supported Advertising Sets */
4137 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4139 if (!ext_adv_capable(hdev))
4142 return __hci_cmd_sync_status(hdev,
4143 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4144 0, NULL, HCI_CMD_TIMEOUT);
4147 /* Write LE Host Supported */
4148 static int hci_set_le_support_sync(struct hci_dev *hdev)
4150 struct hci_cp_write_le_host_supported cp;
4152 /* LE-only devices do not support explicit enablement */
4153 if (!lmp_bredr_capable(hdev))
4156 memset(&cp, 0, sizeof(cp));
4158 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4163 if (cp.le == lmp_host_le_capable(hdev))
4166 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4167 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4170 /* LE Set Host Feature */
4171 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4173 struct hci_cp_le_set_host_feature cp;
4175 if (!iso_capable(hdev))
4178 memset(&cp, 0, sizeof(cp));
4180 /* Isochronous Channels (Host Support) */
4184 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4185 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4188 /* LE Controller init stage 3 command sequence */
4189 static const struct hci_init_stage le_init3[] = {
4190 /* HCI_OP_LE_SET_EVENT_MASK */
4191 HCI_INIT(hci_le_set_event_mask_sync),
4192 /* HCI_OP_LE_READ_ADV_TX_POWER */
4193 HCI_INIT(hci_le_read_adv_tx_power_sync),
4194 /* HCI_OP_LE_READ_TRANSMIT_POWER */
4195 HCI_INIT(hci_le_read_tx_power_sync),
4196 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4197 HCI_INIT(hci_le_read_accept_list_size_sync),
4198 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4199 HCI_INIT(hci_le_clear_accept_list_sync),
4200 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4201 HCI_INIT(hci_le_read_resolv_list_size_sync),
4202 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
4203 HCI_INIT(hci_le_clear_resolv_list_sync),
4204 /* HCI_OP_LE_SET_RPA_TIMEOUT */
4205 HCI_INIT(hci_le_set_rpa_timeout_sync),
4206 /* HCI_OP_LE_READ_MAX_DATA_LEN */
4207 HCI_INIT(hci_le_read_max_data_len_sync),
4208 /* HCI_OP_LE_READ_DEF_DATA_LEN */
4209 HCI_INIT(hci_le_read_def_data_len_sync),
4210 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4211 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4212 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4213 HCI_INIT(hci_set_le_support_sync),
4214 /* HCI_OP_LE_SET_HOST_FEATURE */
4215 HCI_INIT(hci_le_set_host_feature_sync),
4219 static int hci_init3_sync(struct hci_dev *hdev)
4223 bt_dev_dbg(hdev, "");
4225 err = hci_init_stage_sync(hdev, hci_init3);
4229 if (lmp_le_capable(hdev))
4230 return hci_init_stage_sync(hdev, le_init3);
4235 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4237 struct hci_cp_delete_stored_link_key cp;
4239 /* Some Broadcom based Bluetooth controllers do not support the
4240 * Delete Stored Link Key command. They are clearly indicating its
4241 * absence in the bit mask of supported commands.
4243 * Check the supported commands and only if the command is marked
4244 * as supported send it. If not supported assume that the controller
4245 * does not have actual support for stored link keys which makes this
4246 * command redundant anyway.
4248 * Some controllers indicate that they support handling deleting
4249 * stored link keys, but they don't. The quirk lets a driver
4250 * just disable this command.
4252 if (!(hdev->commands[6] & 0x80) ||
4253 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4256 memset(&cp, 0, sizeof(cp));
4257 bacpy(&cp.bdaddr, BDADDR_ANY);
4258 cp.delete_all = 0x01;
4260 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4261 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4264 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4266 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4267 bool changed = false;
4269 /* Set event mask page 2 if the HCI command for it is supported */
4270 if (!(hdev->commands[22] & 0x04))
4273 /* If Connectionless Peripheral Broadcast central role is supported
4274 * enable all necessary events for it.
4276 if (lmp_cpb_central_capable(hdev)) {
4277 events[1] |= 0x40; /* Triggered Clock Capture */
4278 events[1] |= 0x80; /* Synchronization Train Complete */
4279 events[2] |= 0x08; /* Truncated Page Complete */
4280 events[2] |= 0x20; /* CPB Channel Map Change */
4284 /* If Connectionless Peripheral Broadcast peripheral role is supported
4285 * enable all necessary events for it.
4287 if (lmp_cpb_peripheral_capable(hdev)) {
4288 events[2] |= 0x01; /* Synchronization Train Received */
4289 events[2] |= 0x02; /* CPB Receive */
4290 events[2] |= 0x04; /* CPB Timeout */
4291 events[2] |= 0x10; /* Peripheral Page Response Timeout */
4295 /* Enable Authenticated Payload Timeout Expired event if supported */
4296 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4301 /* Some Broadcom based controllers indicate support for Set Event
4302 * Mask Page 2 command, but then actually do not support it. Since
4303 * the default value is all bits set to zero, the command is only
4304 * required if the event mask has to be changed. In case no change
4305 * to the event mask is needed, skip this command.
4310 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4311 sizeof(events), events, HCI_CMD_TIMEOUT);
4314 /* Read local codec list if the HCI command is supported */
4315 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4317 if (hdev->commands[45] & 0x04)
4318 hci_read_supported_codecs_v2(hdev);
4319 else if (hdev->commands[29] & 0x20)
4320 hci_read_supported_codecs(hdev);
4325 /* Read local pairing options if the HCI command is supported */
4326 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4328 if (!(hdev->commands[41] & 0x08))
4331 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4332 0, NULL, HCI_CMD_TIMEOUT);
4335 /* Get MWS transport configuration if the HCI command is supported */
4336 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4338 if (!mws_transport_config_capable(hdev))
4341 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4342 0, NULL, HCI_CMD_TIMEOUT);
4345 /* Check for Synchronization Train support */
4346 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4348 if (!lmp_sync_train_capable(hdev))
4351 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4352 0, NULL, HCI_CMD_TIMEOUT);
4355 /* Enable Secure Connections if supported and configured */
4356 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4360 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4361 !bredr_sc_enabled(hdev))
4364 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4365 sizeof(support), &support,
4369 /* Set erroneous data reporting if supported to the wideband speech
4372 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4374 struct hci_cp_write_def_err_data_reporting cp;
4375 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4377 if (!(hdev->commands[18] & 0x08) ||
4378 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4379 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4382 if (enabled == hdev->err_data_reporting)
4385 memset(&cp, 0, sizeof(cp));
4386 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4387 ERR_DATA_REPORTING_DISABLED;
4389 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4390 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4393 static const struct hci_init_stage hci_init4[] = {
4394 /* HCI_OP_DELETE_STORED_LINK_KEY */
4395 HCI_INIT(hci_delete_stored_link_key_sync),
4396 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4397 HCI_INIT(hci_set_event_mask_page_2_sync),
4398 /* HCI_OP_READ_LOCAL_CODECS */
4399 HCI_INIT(hci_read_local_codecs_sync),
4400 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4401 HCI_INIT(hci_read_local_pairing_opts_sync),
4402 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4403 HCI_INIT(hci_get_mws_transport_config_sync),
4404 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4405 HCI_INIT(hci_read_sync_train_params_sync),
4406 /* HCI_OP_WRITE_SC_SUPPORT */
4407 HCI_INIT(hci_write_sc_support_1_sync),
4408 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4409 HCI_INIT(hci_set_err_data_report_sync),
4413 /* Set Suggested Default Data Length to maximum if supported */
4414 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4416 struct hci_cp_le_write_def_data_len cp;
4418 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4421 memset(&cp, 0, sizeof(cp));
4422 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4423 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4425 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4426 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4429 /* Set Default PHY parameters if command is supported */
4430 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4432 struct hci_cp_le_set_default_phy cp;
4434 if (!(hdev->commands[35] & 0x20))
4437 memset(&cp, 0, sizeof(cp));
4439 cp.tx_phys = hdev->le_tx_def_phys;
4440 cp.rx_phys = hdev->le_rx_def_phys;
4442 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4443 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4446 static const struct hci_init_stage le_init4[] = {
4447 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4448 HCI_INIT(hci_le_set_write_def_data_len_sync),
4449 /* HCI_OP_LE_SET_DEFAULT_PHY */
4450 HCI_INIT(hci_le_set_default_phy_sync),
4454 static int hci_init4_sync(struct hci_dev *hdev)
4458 bt_dev_dbg(hdev, "");
4460 err = hci_init_stage_sync(hdev, hci_init4);
4464 if (lmp_le_capable(hdev))
4465 return hci_init_stage_sync(hdev, le_init4);
4470 static int hci_init_sync(struct hci_dev *hdev)
4474 err = hci_init1_sync(hdev);
4478 if (hci_dev_test_flag(hdev, HCI_SETUP))
4479 hci_debugfs_create_basic(hdev);
4481 err = hci_init2_sync(hdev);
4485 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
4486 * BR/EDR/LE type controllers. AMP controllers only need the
4487 * first two stages of init.
4489 if (hdev->dev_type != HCI_PRIMARY)
4492 err = hci_init3_sync(hdev);
4496 err = hci_init4_sync(hdev);
4500 /* This function is only called when the controller is actually in
4501 * configured state. When the controller is marked as unconfigured,
4502 * this initialization procedure is not run.
4504 * It means that it is possible that a controller runs through its
4505 * setup phase and then discovers missing settings. If that is the
4506 * case, then this function will not be called. It then will only
4507 * be called during the config phase.
4509 * So only when in setup phase or config phase, create the debugfs
4510 * entries and register the SMP channels.
4512 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4513 !hci_dev_test_flag(hdev, HCI_CONFIG))
4516 if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4519 hci_debugfs_create_common(hdev);
4521 if (lmp_bredr_capable(hdev))
4522 hci_debugfs_create_bredr(hdev);
4524 if (lmp_le_capable(hdev))
4525 hci_debugfs_create_le(hdev);
4530 #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
4532 static const struct {
4533 unsigned long quirk;
4535 } hci_broken_table[] = {
4536 HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
4537 "HCI Read Local Supported Commands not supported"),
4538 HCI_QUIRK_BROKEN(STORED_LINK_KEY,
4539 "HCI Delete Stored Link Key command is advertised, "
4540 "but not supported."),
4541 HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
4542 "HCI Read Default Erroneous Data Reporting command is "
4543 "advertised, but not supported."),
4544 HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
4545 "HCI Read Transmit Power Level command is advertised, "
4546 "but not supported."),
4547 HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
4548 "HCI Set Event Filter command not supported."),
4549 HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
4550 "HCI Enhanced Setup Synchronous Connection command is "
4551 "advertised, but not supported."),
4552 HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
4553 "HCI LE Set Random Private Address Timeout command is "
4554 "advertised, but not supported.")
4557 /* This function handles hdev setup stage:
4560 * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
4562 static int hci_dev_setup_sync(struct hci_dev *hdev)
4565 bool invalid_bdaddr;
4568 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4569 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4572 bt_dev_dbg(hdev, "");
4574 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4577 ret = hdev->setup(hdev);
4579 for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
4580 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4581 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4584 /* The transport driver can set the quirk to mark the
4585 * BD_ADDR invalid before creating the HCI device or in
4586 * its setup callback.
4588 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
4591 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4592 !bacmp(&hdev->public_addr, BDADDR_ANY))
4593 hci_dev_get_bd_addr_from_property(hdev);
4595 if ((invalid_bdaddr ||
4596 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) &&
4597 bacmp(&hdev->public_addr, BDADDR_ANY) &&
4599 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4601 invalid_bdaddr = false;
4605 /* The transport driver can set these quirks before
4606 * creating the HCI device or in its setup callback.
4608 * For the invalid BD_ADDR quirk it is possible that
4609 * it becomes a valid address if the bootloader does
4610 * provide it (see above).
4612 * In case any of them is set, the controller has to
4613 * start up as unconfigured.
4615 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4617 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4619 /* For an unconfigured controller it is required to
4620 * read at least the version information provided by
4621 * the Read Local Version Information command.
4623 * If the set_bdaddr driver callback is provided, then
4624 * also the original Bluetooth public device address
4625 * will be read using the Read BD Address command.
4627 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4628 return hci_unconf_init_sync(hdev);
4633 /* This function handles hdev init stage:
4635 * Calls hci_dev_setup_sync to perform setup stage
4636 * Calls hci_init_sync to perform HCI command init sequence
4638 static int hci_dev_init_sync(struct hci_dev *hdev)
4642 bt_dev_dbg(hdev, "");
4644 atomic_set(&hdev->cmd_cnt, 1);
4645 set_bit(HCI_INIT, &hdev->flags);
4647 ret = hci_dev_setup_sync(hdev);
4649 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4650 /* If public address change is configured, ensure that
4651 * the address gets programmed. If the driver does not
4652 * support changing the public address, fail the power
4655 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4657 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4659 ret = -EADDRNOTAVAIL;
4663 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4664 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4665 ret = hci_init_sync(hdev);
4666 if (!ret && hdev->post_init)
4667 ret = hdev->post_init(hdev);
4671 /* If the HCI Reset command is clearing all diagnostic settings,
4672 * then they need to be reprogrammed after the init procedure
4675 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4676 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4677 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4678 ret = hdev->set_diag(hdev, true);
4680 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4685 clear_bit(HCI_INIT, &hdev->flags);
4690 int hci_dev_open_sync(struct hci_dev *hdev)
4694 bt_dev_dbg(hdev, "");
4696 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4701 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4702 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4703 /* Check for rfkill but allow the HCI setup stage to
4704 * proceed (which in itself doesn't cause any RF activity).
4706 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4711 /* Check for valid public address or a configured static
4712 * random address, but let the HCI setup proceed to
4713 * be able to determine if there is a public address
4716 * In case of user channel usage, it is not important
4717 * if a public address or static random address is
4720 * This check is only valid for BR/EDR controllers
4721 * since AMP controllers do not have an address.
4723 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4724 hdev->dev_type == HCI_PRIMARY &&
4725 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4726 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4727 ret = -EADDRNOTAVAIL;
4732 if (test_bit(HCI_UP, &hdev->flags)) {
4737 if (hdev->open(hdev)) {
4742 set_bit(HCI_RUNNING, &hdev->flags);
4743 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4745 ret = hci_dev_init_sync(hdev);
4748 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4749 hci_adv_instances_set_rpa_expired(hdev, true);
4750 set_bit(HCI_UP, &hdev->flags);
4751 hci_sock_dev_event(hdev, HCI_DEV_UP);
4752 hci_leds_update_powered(hdev, true);
4753 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4754 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4755 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4756 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4757 hci_dev_test_flag(hdev, HCI_MGMT) &&
4758 hdev->dev_type == HCI_PRIMARY) {
4759 ret = hci_powered_update_sync(hdev);
4760 mgmt_power_on(hdev, ret);
4763 /* Init failed, cleanup */
4764 flush_work(&hdev->tx_work);
4766 /* Since hci_rx_work() is possible to awake new cmd_work
4767 * it should be flushed first to avoid unexpected call of
4770 flush_work(&hdev->rx_work);
4771 flush_work(&hdev->cmd_work);
4773 skb_queue_purge(&hdev->cmd_q);
4774 skb_queue_purge(&hdev->rx_q);
4779 if (hdev->sent_cmd) {
4780 cancel_delayed_work_sync(&hdev->cmd_timer);
4781 kfree_skb(hdev->sent_cmd);
4782 hdev->sent_cmd = NULL;
4785 clear_bit(HCI_RUNNING, &hdev->flags);
4786 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4789 hdev->flags &= BIT(HCI_RAW);
4796 /* This function requires the caller holds hdev->lock */
4797 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4799 struct hci_conn_params *p;
4801 list_for_each_entry(p, &hdev->le_conn_params, list) {
4803 hci_conn_drop(p->conn);
4804 hci_conn_put(p->conn);
4807 list_del_init(&p->action);
4810 BT_DBG("All LE pending actions cleared");
4813 static int hci_dev_shutdown(struct hci_dev *hdev)
4816 /* Similar to how we first do setup and then set the exclusive access
4817 * bit for userspace, we must first unset userchannel and then clean up.
4818 * Otherwise, the kernel can't properly use the hci channel to clean up
4819 * the controller (some shutdown routines require sending additional
4820 * commands to the controller for example).
4822 bool was_userchannel =
4823 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
4825 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4826 test_bit(HCI_UP, &hdev->flags)) {
4827 /* Execute vendor specific shutdown routine */
4829 err = hdev->shutdown(hdev);
4832 if (was_userchannel)
4833 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
4838 int hci_dev_close_sync(struct hci_dev *hdev)
4843 bt_dev_dbg(hdev, "");
4845 cancel_delayed_work(&hdev->power_off);
4846 cancel_delayed_work(&hdev->ncmd_timer);
4847 cancel_delayed_work(&hdev->le_scan_disable);
4848 cancel_delayed_work(&hdev->le_scan_restart);
4850 hci_request_cancel_all(hdev);
4852 if (hdev->adv_instance_timeout) {
4853 cancel_delayed_work_sync(&hdev->adv_instance_expire);
4854 hdev->adv_instance_timeout = 0;
4857 err = hci_dev_shutdown(hdev);
4859 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4860 cancel_delayed_work_sync(&hdev->cmd_timer);
4864 hci_leds_update_powered(hdev, false);
4866 /* Flush RX and TX works */
4867 flush_work(&hdev->tx_work);
4868 flush_work(&hdev->rx_work);
4870 if (hdev->discov_timeout > 0) {
4871 hdev->discov_timeout = 0;
4872 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4873 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
4876 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
4877 cancel_delayed_work(&hdev->service_cache);
4879 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4880 struct adv_info *adv_instance;
4882 cancel_delayed_work_sync(&hdev->rpa_expired);
4884 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
4885 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
4888 /* Avoid potential lockdep warnings from the *_flush() calls by
4889 * ensuring the workqueue is empty up front.
4891 drain_workqueue(hdev->workqueue);
4895 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4897 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
4899 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
4900 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4901 hci_dev_test_flag(hdev, HCI_MGMT))
4902 __mgmt_power_off(hdev);
4904 hci_inquiry_cache_flush(hdev);
4905 hci_pend_le_actions_clear(hdev);
4906 hci_conn_hash_flush(hdev);
4907 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
4908 smp_unregister(hdev);
4909 hci_dev_unlock(hdev);
4911 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
4913 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4914 aosp_do_close(hdev);
4915 msft_do_close(hdev);
4922 skb_queue_purge(&hdev->cmd_q);
4923 atomic_set(&hdev->cmd_cnt, 1);
4924 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
4925 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4926 set_bit(HCI_INIT, &hdev->flags);
4927 hci_reset_sync(hdev);
4928 clear_bit(HCI_INIT, &hdev->flags);
4931 /* flush cmd work */
4932 flush_work(&hdev->cmd_work);
4935 skb_queue_purge(&hdev->rx_q);
4936 skb_queue_purge(&hdev->cmd_q);
4937 skb_queue_purge(&hdev->raw_q);
4939 /* Drop last sent command */
4940 if (hdev->sent_cmd) {
4941 cancel_delayed_work_sync(&hdev->cmd_timer);
4942 kfree_skb(hdev->sent_cmd);
4943 hdev->sent_cmd = NULL;
4946 clear_bit(HCI_RUNNING, &hdev->flags);
4947 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4949 /* After this point our queues are empty and no tasks are scheduled. */
4953 hdev->flags &= BIT(HCI_RAW);
4954 hci_dev_clear_volatile_flags(hdev);
4956 /* Controller radio is available but is currently powered down */
4957 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
4959 memset(hdev->eir, 0, sizeof(hdev->eir));
4960 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
4961 bacpy(&hdev->random_addr, BDADDR_ANY);
4967 /* This function perform power on HCI command sequence as follows:
4969 * If controller is already up (HCI_UP) performs hci_powered_update_sync
4970 * sequence otherwise run hci_dev_open_sync which will follow with
4971 * hci_powered_update_sync after the init sequence is completed.
4973 static int hci_power_on_sync(struct hci_dev *hdev)
4977 if (test_bit(HCI_UP, &hdev->flags) &&
4978 hci_dev_test_flag(hdev, HCI_MGMT) &&
4979 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
4980 cancel_delayed_work(&hdev->power_off);
4981 return hci_powered_update_sync(hdev);
4984 err = hci_dev_open_sync(hdev);
4988 /* During the HCI setup phase, a few error conditions are
4989 * ignored and they need to be checked now. If they are still
4990 * valid, it is important to return the device back off.
4992 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
4993 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
4994 (hdev->dev_type == HCI_PRIMARY &&
4995 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4996 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
4997 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
4998 hci_dev_close_sync(hdev);
4999 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5000 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5001 HCI_AUTO_OFF_TIMEOUT);
5004 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5005 /* For unconfigured devices, set the HCI_RAW flag
5006 * so that userspace can easily identify them.
5008 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5009 set_bit(HCI_RAW, &hdev->flags);
5011 /* For fully configured devices, this will send
5012 * the Index Added event. For unconfigured devices,
5013 * it will send Unconfigued Index Added event.
5015 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
5016 * and no event will be send.
5018 mgmt_index_added(hdev);
5019 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5020 /* When the controller is now configured, then it
5021 * is important to clear the HCI_RAW flag.
5023 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5024 clear_bit(HCI_RAW, &hdev->flags);
5026 /* Powering on the controller with HCI_CONFIG set only
5027 * happens with the transition from unconfigured to
5028 * configured. This will send the Index Added event.
5030 mgmt_index_added(hdev);
5036 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5038 struct hci_cp_remote_name_req_cancel cp;
5040 memset(&cp, 0, sizeof(cp));
5041 bacpy(&cp.bdaddr, addr);
5043 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5044 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5047 int hci_stop_discovery_sync(struct hci_dev *hdev)
5049 struct discovery_state *d = &hdev->discovery;
5050 struct inquiry_entry *e;
5053 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5055 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
5056 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5057 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5058 0, NULL, HCI_CMD_TIMEOUT);
5063 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5064 cancel_delayed_work(&hdev->le_scan_disable);
5065 cancel_delayed_work(&hdev->le_scan_restart);
5067 err = hci_scan_disable_sync(hdev);
5073 err = hci_scan_disable_sync(hdev);
5078 /* Resume advertising if it was paused */
5079 if (use_ll_privacy(hdev))
5080 hci_resume_advertising_sync(hdev);
5082 /* No further actions needed for LE-only discovery */
5083 if (d->type == DISCOV_TYPE_LE)
5086 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5087 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5092 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5098 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
5101 struct hci_cp_disconn_phy_link cp;
5103 memset(&cp, 0, sizeof(cp));
5104 cp.phy_handle = HCI_PHY_HANDLE(handle);
5107 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
5108 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5111 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5114 struct hci_cp_disconnect cp;
5116 if (conn->type == AMP_LINK)
5117 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
5119 memset(&cp, 0, sizeof(cp));
5120 cp.handle = cpu_to_le16(conn->handle);
5123 /* Wait for HCI_EV_DISCONN_COMPLETE not HCI_EV_CMD_STATUS when not
5126 if (!hdev->suspended)
5127 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5129 HCI_EV_DISCONN_COMPLETE,
5130 HCI_CMD_TIMEOUT, NULL);
5132 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5136 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5137 struct hci_conn *conn)
5139 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5142 if (test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
5145 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5146 0, NULL, HCI_CMD_TIMEOUT);
5149 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn)
5151 if (conn->type == LE_LINK)
5152 return hci_le_connect_cancel_sync(hdev, conn);
5154 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5157 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5158 6, &conn->dst, HCI_CMD_TIMEOUT);
5161 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5164 struct hci_cp_reject_sync_conn_req cp;
5166 memset(&cp, 0, sizeof(cp));
5167 bacpy(&cp.bdaddr, &conn->dst);
5170 /* SCO rejection has its own limited set of
5171 * allowed error values (0x0D-0x0F).
5173 if (reason < 0x0d || reason > 0x0f)
5174 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5176 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5177 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5180 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5183 struct hci_cp_reject_conn_req cp;
5185 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5186 return hci_reject_sco_sync(hdev, conn, reason);
5188 memset(&cp, 0, sizeof(cp));
5189 bacpy(&cp.bdaddr, &conn->dst);
5192 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5193 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5196 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5200 switch (conn->state) {
5203 return hci_disconnect_sync(hdev, conn, reason);
5205 err = hci_connect_cancel_sync(hdev, conn);
5206 /* Cleanup hci_conn object if it cannot be cancelled as it
5207 * likelly means the controller and host stack are out of sync.
5211 hci_conn_failed(conn, err);
5212 hci_dev_unlock(hdev);
5216 return hci_reject_conn_sync(hdev, conn, reason);
5218 conn->state = BT_CLOSED;
5225 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5227 struct hci_conn *conn, *tmp;
5230 list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
5231 err = hci_abort_conn_sync(hdev, conn, reason);
5239 /* This function perform power off HCI command sequence as follows:
5243 * Disconnect all connections
5244 * hci_dev_close_sync
5246 static int hci_power_off_sync(struct hci_dev *hdev)
5250 /* If controller is already down there is nothing to do */
5251 if (!test_bit(HCI_UP, &hdev->flags))
5254 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5255 test_bit(HCI_PSCAN, &hdev->flags)) {
5256 err = hci_write_scan_enable_sync(hdev, 0x00);
5261 err = hci_clear_adv_sync(hdev, NULL, false);
5265 err = hci_stop_discovery_sync(hdev);
5269 /* Terminated due to Power Off */
5270 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5274 return hci_dev_close_sync(hdev);
5277 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5280 return hci_power_on_sync(hdev);
5282 return hci_power_off_sync(hdev);
5285 static int hci_write_iac_sync(struct hci_dev *hdev)
5287 struct hci_cp_write_current_iac_lap cp;
5289 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5292 memset(&cp, 0, sizeof(cp));
5294 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5295 /* Limited discoverable mode */
5296 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5297 cp.iac_lap[0] = 0x00; /* LIAC */
5298 cp.iac_lap[1] = 0x8b;
5299 cp.iac_lap[2] = 0x9e;
5300 cp.iac_lap[3] = 0x33; /* GIAC */
5301 cp.iac_lap[4] = 0x8b;
5302 cp.iac_lap[5] = 0x9e;
5304 /* General discoverable mode */
5306 cp.iac_lap[0] = 0x33; /* GIAC */
5307 cp.iac_lap[1] = 0x8b;
5308 cp.iac_lap[2] = 0x9e;
5311 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5312 (cp.num_iac * 3) + 1, &cp,
5316 int hci_update_discoverable_sync(struct hci_dev *hdev)
5320 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5321 err = hci_write_iac_sync(hdev);
5325 err = hci_update_scan_sync(hdev);
5329 err = hci_update_class_sync(hdev);
5334 /* Advertising instances don't use the global discoverable setting, so
5335 * only update AD if advertising was enabled using Set Advertising.
5337 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5338 err = hci_update_adv_data_sync(hdev, 0x00);
5342 /* Discoverable mode affects the local advertising
5343 * address in limited privacy mode.
5345 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5346 if (ext_adv_capable(hdev))
5347 err = hci_start_ext_adv_sync(hdev, 0x00);
5349 err = hci_enable_advertising_sync(hdev);
5356 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5358 return hci_update_discoverable_sync(hdev);
5361 int hci_update_discoverable(struct hci_dev *hdev)
5363 /* Only queue if it would have any effect */
5364 if (hdev_is_powered(hdev) &&
5365 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5366 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5367 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5368 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5374 int hci_update_connectable_sync(struct hci_dev *hdev)
5378 err = hci_update_scan_sync(hdev);
5382 /* If BR/EDR is not enabled and we disable advertising as a
5383 * by-product of disabling connectable, we need to update the
5384 * advertising flags.
5386 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5387 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5389 /* Update the advertising parameters if necessary */
5390 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5391 !list_empty(&hdev->adv_instances)) {
5392 if (ext_adv_capable(hdev))
5393 err = hci_start_ext_adv_sync(hdev,
5394 hdev->cur_adv_instance);
5396 err = hci_enable_advertising_sync(hdev);
5402 return hci_update_passive_scan_sync(hdev);
5405 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5407 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5408 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5409 struct hci_cp_inquiry cp;
5411 bt_dev_dbg(hdev, "");
5413 if (hci_dev_test_flag(hdev, HCI_INQUIRY))
5417 hci_inquiry_cache_flush(hdev);
5418 hci_dev_unlock(hdev);
5420 memset(&cp, 0, sizeof(cp));
5422 if (hdev->discovery.limited)
5423 memcpy(&cp.lap, liac, sizeof(cp.lap));
5425 memcpy(&cp.lap, giac, sizeof(cp.lap));
5429 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5430 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5433 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5436 /* Accept list is not used for discovery */
5437 u8 filter_policy = 0x00;
5438 /* Default is to enable duplicates filter */
5439 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5442 bt_dev_dbg(hdev, "");
5444 /* If controller is scanning, it means the passive scanning is
5445 * running. Thus, we should temporarily stop it in order to set the
5446 * discovery scanning parameters.
5448 err = hci_scan_disable_sync(hdev);
5450 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5454 cancel_interleave_scan(hdev);
5456 /* Pause address resolution for active scan and stop advertising if
5457 * privacy is enabled.
5459 err = hci_pause_addr_resolution(hdev);
5463 /* All active scans will be done with either a resolvable private
5464 * address (when privacy feature has been enabled) or non-resolvable
5467 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5470 own_addr_type = ADDR_LE_DEV_PUBLIC;
5472 if (hci_is_adv_monitoring(hdev)) {
5473 /* Duplicate filter should be disabled when some advertisement
5474 * monitor is activated, otherwise AdvMon can only receive one
5475 * advertisement for one peer(*) during active scanning, and
5476 * might report loss to these peers.
5478 * Note that different controllers have different meanings of
5479 * |duplicate|. Some of them consider packets with the same
5480 * address as duplicate, and others consider packets with the
5481 * same address and the same RSSI as duplicate. Although in the
5482 * latter case we don't need to disable duplicate filter, but
5483 * it is common to have active scanning for a short period of
5484 * time, the power impact should be neglectable.
5486 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5489 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5490 hdev->le_scan_window_discovery,
5491 own_addr_type, filter_policy, filter_dup);
5496 /* Resume advertising if it was paused */
5497 if (use_ll_privacy(hdev))
5498 hci_resume_advertising_sync(hdev);
5500 /* Resume passive scanning */
5501 hci_update_passive_scan_sync(hdev);
5505 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5509 bt_dev_dbg(hdev, "");
5511 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5515 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5518 int hci_start_discovery_sync(struct hci_dev *hdev)
5520 unsigned long timeout;
5523 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5525 switch (hdev->discovery.type) {
5526 case DISCOV_TYPE_BREDR:
5527 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5528 case DISCOV_TYPE_INTERLEAVED:
5529 /* When running simultaneous discovery, the LE scanning time
5530 * should occupy the whole discovery time sine BR/EDR inquiry
5531 * and LE scanning are scheduled by the controller.
5533 * For interleaving discovery in comparison, BR/EDR inquiry
5534 * and LE scanning are done sequentially with separate
5537 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5539 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5540 /* During simultaneous discovery, we double LE scan
5541 * interval. We must leave some time for the controller
5542 * to do BR/EDR inquiry.
5544 err = hci_start_interleaved_discovery_sync(hdev);
5548 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5549 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5551 case DISCOV_TYPE_LE:
5552 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5553 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5562 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5564 /* When service discovery is used and the controller has a
5565 * strict duplicate filter, it is important to remember the
5566 * start and duration of the scan. This is required for
5567 * restarting scanning during the discovery phase.
5569 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5570 hdev->discovery.result_filtering) {
5571 hdev->discovery.scan_start = jiffies;
5572 hdev->discovery.scan_duration = timeout;
5575 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5580 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5582 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5583 case HCI_ADV_MONITOR_EXT_MSFT:
5584 msft_suspend_sync(hdev);
5591 /* This function disables discovery and mark it as paused */
5592 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5594 int old_state = hdev->discovery.state;
5597 /* If discovery already stopped/stopping/paused there nothing to do */
5598 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
5599 hdev->discovery_paused)
5602 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5603 err = hci_stop_discovery_sync(hdev);
5607 hdev->discovery_paused = true;
5608 hdev->discovery_old_state = old_state;
5609 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5614 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5616 struct bdaddr_list_with_flags *b;
5617 u8 scan = SCAN_DISABLED;
5618 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5621 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5624 /* Some fake CSR controllers lock up after setting this type of
5625 * filter, so avoid sending the request altogether.
5627 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5630 /* Always clear event filter when starting */
5631 hci_clear_event_filter_sync(hdev);
5633 list_for_each_entry(b, &hdev->accept_list, list) {
5634 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
5637 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5639 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5640 HCI_CONN_SETUP_ALLOW_BDADDR,
5642 HCI_CONN_SETUP_AUTO_ON);
5644 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5650 if (scan && !scanning)
5651 hci_write_scan_enable_sync(hdev, scan);
5652 else if (!scan && scanning)
5653 hci_write_scan_enable_sync(hdev, scan);
5658 /* This function disables scan (BR and LE) and mark it as paused */
5659 static int hci_pause_scan_sync(struct hci_dev *hdev)
5661 if (hdev->scanning_paused)
5664 /* Disable page scan if enabled */
5665 if (test_bit(HCI_PSCAN, &hdev->flags))
5666 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
5668 hci_scan_disable_sync(hdev);
5670 hdev->scanning_paused = true;
5675 /* This function performs the HCI suspend procedures in the follow order:
5677 * Pause discovery (active scanning/inquiry)
5678 * Pause Directed Advertising/Advertising
5679 * Pause Scanning (passive scanning in case discovery was not active)
5680 * Disconnect all connections
5681 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5683 * Update event mask (only set events that are allowed to wake up the host)
5684 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
5685 * Update passive scanning (lower duty cycle)
5686 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
5688 int hci_suspend_sync(struct hci_dev *hdev)
5692 /* If marked as suspended there nothing to do */
5693 if (hdev->suspended)
5696 /* Mark device as suspended */
5697 hdev->suspended = true;
5699 /* Pause discovery if not already stopped */
5700 hci_pause_discovery_sync(hdev);
5702 /* Pause other advertisements */
5703 hci_pause_advertising_sync(hdev);
5705 /* Suspend monitor filters */
5706 hci_suspend_monitor_sync(hdev);
5708 /* Prevent disconnects from causing scanning to be re-enabled */
5709 hci_pause_scan_sync(hdev);
5711 if (hci_conn_count(hdev)) {
5712 /* Soft disconnect everything (power off) */
5713 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5715 /* Set state to BT_RUNNING so resume doesn't notify */
5716 hdev->suspend_state = BT_RUNNING;
5717 hci_resume_sync(hdev);
5721 /* Update event mask so only the allowed event can wakeup the
5724 hci_set_event_mask_sync(hdev);
5727 /* Only configure accept list if disconnect succeeded and wake
5728 * isn't being prevented.
5730 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5731 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5735 /* Unpause to take care of updating scanning params */
5736 hdev->scanning_paused = false;
5738 /* Enable event filter for paired devices */
5739 hci_update_event_filter_sync(hdev);
5741 /* Update LE passive scan if enabled */
5742 hci_update_passive_scan_sync(hdev);
5744 /* Pause scan changes again. */
5745 hdev->scanning_paused = true;
5747 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5752 /* This function resumes discovery */
5753 static int hci_resume_discovery_sync(struct hci_dev *hdev)
5757 /* If discovery not paused there nothing to do */
5758 if (!hdev->discovery_paused)
5761 hdev->discovery_paused = false;
5763 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
5765 err = hci_start_discovery_sync(hdev);
5767 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
5773 static void hci_resume_monitor_sync(struct hci_dev *hdev)
5775 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5776 case HCI_ADV_MONITOR_EXT_MSFT:
5777 msft_resume_sync(hdev);
5784 /* This function resume scan and reset paused flag */
5785 static int hci_resume_scan_sync(struct hci_dev *hdev)
5787 if (!hdev->scanning_paused)
5790 hdev->scanning_paused = false;
5792 hci_update_scan_sync(hdev);
5794 /* Reset passive scanning to normal */
5795 hci_update_passive_scan_sync(hdev);
5800 /* This function performs the HCI suspend procedures in the follow order:
5802 * Restore event mask
5803 * Clear event filter
5804 * Update passive scanning (normal duty cycle)
5805 * Resume Directed Advertising/Advertising
5806 * Resume discovery (active scanning/inquiry)
5808 int hci_resume_sync(struct hci_dev *hdev)
5810 /* If not marked as suspended there nothing to do */
5811 if (!hdev->suspended)
5814 hdev->suspended = false;
5816 /* Restore event mask */
5817 hci_set_event_mask_sync(hdev);
5819 /* Clear any event filters and restore scan state */
5820 hci_clear_event_filter_sync(hdev);
5822 /* Resume scanning */
5823 hci_resume_scan_sync(hdev);
5825 /* Resume monitor filters */
5826 hci_resume_monitor_sync(hdev);
5828 /* Resume other advertisements */
5829 hci_resume_advertising_sync(hdev);
5831 /* Resume discovery */
5832 hci_resume_discovery_sync(hdev);
5837 static bool conn_use_rpa(struct hci_conn *conn)
5839 struct hci_dev *hdev = conn->hdev;
5841 return hci_dev_test_flag(hdev, HCI_PRIVACY);
5844 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
5845 struct hci_conn *conn)
5847 struct hci_cp_le_set_ext_adv_params cp;
5849 bdaddr_t random_addr;
5852 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5857 /* Set require_privacy to false so that the remote device has a
5858 * chance of identifying us.
5860 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
5861 &own_addr_type, &random_addr);
5865 memset(&cp, 0, sizeof(cp));
5867 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
5868 cp.own_addr_type = own_addr_type;
5869 cp.channel_map = hdev->le_adv_channel_map;
5870 cp.tx_power = HCI_TX_POWER_INVALID;
5871 cp.primary_phy = HCI_ADV_PHY_1M;
5872 cp.secondary_phy = HCI_ADV_PHY_1M;
5873 cp.handle = 0x00; /* Use instance 0 for directed adv */
5874 cp.own_addr_type = own_addr_type;
5875 cp.peer_addr_type = conn->dst_type;
5876 bacpy(&cp.peer_addr, &conn->dst);
5878 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
5879 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
5880 * does not supports advertising data when the advertising set already
5881 * contains some, the controller shall return erroc code 'Invalid
5882 * HCI Command Parameters(0x12).
5883 * So it is required to remove adv set for handle 0x00. since we use
5884 * instance 0 for directed adv.
5886 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
5890 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
5891 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5895 /* Check if random address need to be updated */
5896 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
5897 bacmp(&random_addr, BDADDR_ANY) &&
5898 bacmp(&random_addr, &hdev->random_addr)) {
5899 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
5905 return hci_enable_ext_advertising_sync(hdev, 0x00);
5908 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
5909 struct hci_conn *conn)
5911 struct hci_cp_le_set_adv_param cp;
5916 if (ext_adv_capable(hdev))
5917 return hci_le_ext_directed_advertising_sync(hdev, conn);
5919 /* Clear the HCI_LE_ADV bit temporarily so that the
5920 * hci_update_random_address knows that it's safe to go ahead
5921 * and write a new random address. The flag will be set back on
5922 * as soon as the SET_ADV_ENABLE HCI command completes.
5924 hci_dev_clear_flag(hdev, HCI_LE_ADV);
5926 /* Set require_privacy to false so that the remote device has a
5927 * chance of identifying us.
5929 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5934 memset(&cp, 0, sizeof(cp));
5936 /* Some controllers might reject command if intervals are not
5937 * within range for undirected advertising.
5938 * BCM20702A0 is known to be affected by this.
5940 cp.min_interval = cpu_to_le16(0x0020);
5941 cp.max_interval = cpu_to_le16(0x0020);
5943 cp.type = LE_ADV_DIRECT_IND;
5944 cp.own_address_type = own_addr_type;
5945 cp.direct_addr_type = conn->dst_type;
5946 bacpy(&cp.direct_addr, &conn->dst);
5947 cp.channel_map = hdev->le_adv_channel_map;
5949 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
5950 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5956 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
5957 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
5960 static void set_ext_conn_params(struct hci_conn *conn,
5961 struct hci_cp_le_ext_conn_param *p)
5963 struct hci_dev *hdev = conn->hdev;
5965 memset(p, 0, sizeof(*p));
5967 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
5968 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
5969 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
5970 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
5971 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
5972 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
5973 p->min_ce_len = cpu_to_le16(0x0000);
5974 p->max_ce_len = cpu_to_le16(0x0000);
5977 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
5978 struct hci_conn *conn, u8 own_addr_type)
5980 struct hci_cp_le_ext_create_conn *cp;
5981 struct hci_cp_le_ext_conn_param *p;
5982 u8 data[sizeof(*cp) + sizeof(*p) * 3];
5986 p = (void *)cp->data;
5988 memset(cp, 0, sizeof(*cp));
5990 bacpy(&cp->peer_addr, &conn->dst);
5991 cp->peer_addr_type = conn->dst_type;
5992 cp->own_addr_type = own_addr_type;
5996 if (scan_1m(hdev)) {
5997 cp->phys |= LE_SCAN_PHY_1M;
5998 set_ext_conn_params(conn, p);
6004 if (scan_2m(hdev)) {
6005 cp->phys |= LE_SCAN_PHY_2M;
6006 set_ext_conn_params(conn, p);
6012 if (scan_coded(hdev)) {
6013 cp->phys |= LE_SCAN_PHY_CODED;
6014 set_ext_conn_params(conn, p);
6019 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6021 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
6022 conn->conn_timeout, NULL);
6025 int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
6027 struct hci_cp_le_create_conn cp;
6028 struct hci_conn_params *params;
6032 /* If requested to connect as peripheral use directed advertising */
6033 if (conn->role == HCI_ROLE_SLAVE) {
6034 /* If we're active scanning and simultaneous roles is not
6035 * enabled simply reject the attempt.
6037 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6038 hdev->le_scan_type == LE_SCAN_ACTIVE &&
6039 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6044 /* Pause advertising while doing directed advertising. */
6045 hci_pause_advertising_sync(hdev);
6047 err = hci_le_directed_advertising_sync(hdev, conn);
6051 /* Disable advertising if simultaneous roles is not in use. */
6052 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6053 hci_pause_advertising_sync(hdev);
6055 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6057 conn->le_conn_min_interval = params->conn_min_interval;
6058 conn->le_conn_max_interval = params->conn_max_interval;
6059 conn->le_conn_latency = params->conn_latency;
6060 conn->le_supv_timeout = params->supervision_timeout;
6062 conn->le_conn_min_interval = hdev->le_conn_min_interval;
6063 conn->le_conn_max_interval = hdev->le_conn_max_interval;
6064 conn->le_conn_latency = hdev->le_conn_latency;
6065 conn->le_supv_timeout = hdev->le_supv_timeout;
6068 /* If controller is scanning, we stop it since some controllers are
6069 * not able to scan and connect at the same time. Also set the
6070 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
6071 * handler for scan disabling knows to set the correct discovery
6074 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6075 hci_scan_disable_sync(hdev);
6076 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6079 /* Update random address, but set require_privacy to false so
6080 * that we never connect with an non-resolvable address.
6082 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6087 if (use_ext_conn(hdev)) {
6088 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6092 memset(&cp, 0, sizeof(cp));
6094 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6095 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6097 bacpy(&cp.peer_addr, &conn->dst);
6098 cp.peer_addr_type = conn->dst_type;
6099 cp.own_address_type = own_addr_type;
6100 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6101 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6102 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
6103 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6104 cp.min_ce_len = cpu_to_le16(0x0000);
6105 cp.max_ce_len = cpu_to_le16(0x0000);
6107 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6109 * If this event is unmasked and the HCI_LE_Connection_Complete event
6110 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6111 * sent when a new connection has been created.
6113 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6115 use_enhanced_conn_complete(hdev) ?
6116 HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6117 HCI_EV_LE_CONN_COMPLETE,
6118 conn->conn_timeout, NULL);
6121 if (err == -ETIMEDOUT)
6122 hci_le_connect_cancel_sync(hdev, conn);
6124 /* Re-enable advertising after the connection attempt is finished. */
6125 hci_resume_advertising_sync(hdev);
6129 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6131 struct hci_cp_le_remove_cig cp;
6133 memset(&cp, 0, sizeof(cp));
6136 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6137 &cp, HCI_CMD_TIMEOUT);
6140 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6142 struct hci_cp_le_big_term_sync cp;
6144 memset(&cp, 0, sizeof(cp));
6147 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6148 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6151 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6153 struct hci_cp_le_pa_term_sync cp;
6155 memset(&cp, 0, sizeof(cp));
6156 cp.handle = cpu_to_le16(handle);
6158 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6159 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6162 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6163 bool use_rpa, struct adv_info *adv_instance,
6164 u8 *own_addr_type, bdaddr_t *rand_addr)
6168 bacpy(rand_addr, BDADDR_ANY);
6170 /* If privacy is enabled use a resolvable private address. If
6171 * current RPA has expired then generate a new one.
6174 /* If Controller supports LL Privacy use own address type is
6177 if (use_ll_privacy(hdev))
6178 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
6180 *own_addr_type = ADDR_LE_DEV_RANDOM;
6183 if (adv_rpa_valid(adv_instance))
6186 if (rpa_valid(hdev))
6190 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6192 bt_dev_err(hdev, "failed to generate new RPA");
6196 bacpy(rand_addr, &hdev->rpa);
6201 /* In case of required privacy without resolvable private address,
6202 * use an non-resolvable private address. This is useful for
6203 * non-connectable advertising.
6205 if (require_privacy) {
6209 /* The non-resolvable private address is generated
6210 * from random six bytes with the two most significant
6213 get_random_bytes(&nrpa, 6);
6216 /* The non-resolvable private address shall not be
6217 * equal to the public address.
6219 if (bacmp(&hdev->bdaddr, &nrpa))
6223 *own_addr_type = ADDR_LE_DEV_RANDOM;
6224 bacpy(rand_addr, &nrpa);
6229 /* No privacy so use a public address. */
6230 *own_addr_type = ADDR_LE_DEV_PUBLIC;
6235 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6237 u8 instance = PTR_ERR(data);
6239 return hci_update_adv_data_sync(hdev, instance);
6242 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6244 return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6245 ERR_PTR(instance), NULL);