1 // SPDX-License-Identifier: GPL-2.0
3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2021 Intel Corporation
9 #include <linux/property.h>
11 #include <net/bluetooth/bluetooth.h>
12 #include <net/bluetooth/hci_core.h>
13 #include <net/bluetooth/mgmt.h>
15 #include "hci_request.h"
16 #include "hci_codec.h"
17 #include "hci_debugfs.h"
24 static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
27 bt_dev_dbg(hdev, "result 0x%2.2x", result);
29 if (hdev->req_status != HCI_REQ_PEND)
32 hdev->req_result = result;
33 hdev->req_status = HCI_REQ_DONE;
36 struct sock *sk = hci_skb_sk(skb);
38 /* Drop sk reference if set */
42 hdev->req_skb = skb_get(skb);
45 wake_up_interruptible(&hdev->req_wait_q);
48 static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
49 u32 plen, const void *param,
52 int len = HCI_COMMAND_HDR_SIZE + plen;
53 struct hci_command_hdr *hdr;
56 skb = bt_skb_alloc(len, GFP_ATOMIC);
60 hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
61 hdr->opcode = cpu_to_le16(opcode);
65 skb_put_data(skb, param, plen);
67 bt_dev_dbg(hdev, "skb len %d", skb->len);
69 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
70 hci_skb_opcode(skb) = opcode;
72 /* Grab a reference if command needs to be associated with a sock (e.g.
73 * likely mgmt socket that initiated the command).
83 static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
84 const void *param, u8 event, struct sock *sk)
86 struct hci_dev *hdev = req->hdev;
89 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
91 /* If an error occurred during request building, there is no point in
92 * queueing the HCI command. We can simply return.
97 skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
99 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
105 if (skb_queue_empty(&req->cmd_q))
106 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
108 hci_skb_event(skb) = event;
110 skb_queue_tail(&req->cmd_q, skb);
113 static int hci_cmd_sync_run(struct hci_request *req)
115 struct hci_dev *hdev = req->hdev;
119 bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
121 /* If an error occurred during request building, remove all HCI
122 * commands queued on the HCI request queue.
125 skb_queue_purge(&req->cmd_q);
129 /* Do not allow empty requests */
130 if (skb_queue_empty(&req->cmd_q))
133 skb = skb_peek_tail(&req->cmd_q);
134 bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
135 bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
137 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
138 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
139 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
141 queue_work(hdev->workqueue, &hdev->cmd_work);
146 /* This function requires the caller holds hdev->req_lock. */
147 struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
148 const void *param, u8 event, u32 timeout,
151 struct hci_request req;
155 bt_dev_dbg(hdev, "Opcode 0x%4.4x", opcode);
157 hci_req_init(&req, hdev);
159 hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
161 hdev->req_status = HCI_REQ_PEND;
163 err = hci_cmd_sync_run(&req);
167 err = wait_event_interruptible_timeout(hdev->req_wait_q,
168 hdev->req_status != HCI_REQ_PEND,
171 if (err == -ERESTARTSYS)
172 return ERR_PTR(-EINTR);
174 switch (hdev->req_status) {
176 err = -bt_to_errno(hdev->req_result);
179 case HCI_REQ_CANCELED:
180 err = -hdev->req_result;
188 hdev->req_status = 0;
189 hdev->req_result = 0;
191 hdev->req_skb = NULL;
193 bt_dev_dbg(hdev, "end: err %d", err);
202 EXPORT_SYMBOL(__hci_cmd_sync_sk);
204 /* This function requires the caller holds hdev->req_lock. */
205 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
206 const void *param, u32 timeout)
208 return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
210 EXPORT_SYMBOL(__hci_cmd_sync);
212 /* Send HCI command and wait for command complete event */
213 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
214 const void *param, u32 timeout)
218 if (!test_bit(HCI_UP, &hdev->flags))
219 return ERR_PTR(-ENETDOWN);
221 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
223 hci_req_sync_lock(hdev);
224 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
225 hci_req_sync_unlock(hdev);
229 EXPORT_SYMBOL(hci_cmd_sync);
231 /* This function requires the caller holds hdev->req_lock. */
232 struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
233 const void *param, u8 event, u32 timeout)
235 return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
238 EXPORT_SYMBOL(__hci_cmd_sync_ev);
240 /* This function requires the caller holds hdev->req_lock. */
241 int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
242 const void *param, u8 event, u32 timeout,
248 skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
251 bt_dev_err(hdev, "Opcode 0x%4.4x failed: %ld", opcode,
256 /* If command return a status event skb will be set to NULL as there are
257 * no parameters, in case of failure IS_ERR(skb) would have be set to
258 * the actual error would be found with PTR_ERR(skb).
263 status = skb->data[0];
269 EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
271 int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
272 const void *param, u32 timeout)
274 return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
277 EXPORT_SYMBOL(__hci_cmd_sync_status);
279 static void hci_cmd_sync_work(struct work_struct *work)
281 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
283 bt_dev_dbg(hdev, "");
285 /* Dequeue all entries and run them */
287 struct hci_cmd_sync_work_entry *entry;
289 mutex_lock(&hdev->cmd_sync_work_lock);
290 entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
291 struct hci_cmd_sync_work_entry,
294 list_del(&entry->list);
295 mutex_unlock(&hdev->cmd_sync_work_lock);
300 bt_dev_dbg(hdev, "entry %p", entry);
305 hci_req_sync_lock(hdev);
306 err = entry->func(hdev, entry->data);
308 entry->destroy(hdev, entry->data, err);
309 hci_req_sync_unlock(hdev);
316 static void hci_cmd_sync_cancel_work(struct work_struct *work)
318 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
320 cancel_delayed_work_sync(&hdev->cmd_timer);
321 cancel_delayed_work_sync(&hdev->ncmd_timer);
322 atomic_set(&hdev->cmd_cnt, 1);
324 wake_up_interruptible(&hdev->req_wait_q);
327 static int hci_scan_disable_sync(struct hci_dev *hdev);
328 static int scan_disable_sync(struct hci_dev *hdev, void *data)
330 return hci_scan_disable_sync(hdev);
333 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length);
334 static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data)
336 return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN);
339 static void le_scan_disable(struct work_struct *work)
341 struct hci_dev *hdev = container_of(work, struct hci_dev,
342 le_scan_disable.work);
345 bt_dev_dbg(hdev, "");
348 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
351 cancel_delayed_work(&hdev->le_scan_restart);
353 status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
355 bt_dev_err(hdev, "failed to disable LE scan: %d", status);
359 hdev->discovery.scan_start = 0;
361 /* If we were running LE only scan, change discovery state. If
362 * we were running both LE and BR/EDR inquiry simultaneously,
363 * and BR/EDR inquiry is already finished, stop discovery,
364 * otherwise BR/EDR inquiry will stop discovery when finished.
365 * If we will resolve remote device name, do not change
369 if (hdev->discovery.type == DISCOV_TYPE_LE)
372 if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
375 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
376 if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
377 hdev->discovery.state != DISCOVERY_RESOLVING)
383 status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
385 bt_dev_err(hdev, "inquiry failed: status %d", status);
392 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
395 hci_dev_unlock(hdev);
398 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
400 static int hci_le_scan_restart_sync(struct hci_dev *hdev)
402 /* If controller is not scanning we are done. */
403 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
406 if (hdev->scanning_paused) {
407 bt_dev_dbg(hdev, "Scanning is paused for suspend");
411 hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
412 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE,
413 LE_SCAN_FILTER_DUP_ENABLE);
416 static void le_scan_restart(struct work_struct *work)
418 struct hci_dev *hdev = container_of(work, struct hci_dev,
419 le_scan_restart.work);
420 unsigned long timeout, duration, scan_start, now;
423 bt_dev_dbg(hdev, "");
425 status = hci_le_scan_restart_sync(hdev);
427 bt_dev_err(hdev, "failed to restart LE scan: status %d",
434 if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
435 !hdev->discovery.scan_start)
438 /* When the scan was started, hdev->le_scan_disable has been queued
439 * after duration from scan_start. During scan restart this job
440 * has been canceled, and we need to queue it again after proper
441 * timeout, to make sure that scan does not run indefinitely.
443 duration = hdev->discovery.scan_duration;
444 scan_start = hdev->discovery.scan_start;
446 if (now - scan_start <= duration) {
449 if (now >= scan_start)
450 elapsed = now - scan_start;
452 elapsed = ULONG_MAX - scan_start + now;
454 timeout = duration - elapsed;
459 queue_delayed_work(hdev->req_workqueue,
460 &hdev->le_scan_disable, timeout);
463 hci_dev_unlock(hdev);
466 static int reenable_adv_sync(struct hci_dev *hdev, void *data)
468 bt_dev_dbg(hdev, "");
470 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
471 list_empty(&hdev->adv_instances))
474 if (hdev->cur_adv_instance) {
475 return hci_schedule_adv_instance_sync(hdev,
476 hdev->cur_adv_instance,
479 if (ext_adv_capable(hdev)) {
480 hci_start_ext_adv_sync(hdev, 0x00);
482 hci_update_adv_data_sync(hdev, 0x00);
483 hci_update_scan_rsp_data_sync(hdev, 0x00);
484 hci_enable_advertising_sync(hdev);
491 static void reenable_adv(struct work_struct *work)
493 struct hci_dev *hdev = container_of(work, struct hci_dev,
497 bt_dev_dbg(hdev, "");
501 status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
503 bt_dev_err(hdev, "failed to reenable ADV: %d", status);
505 hci_dev_unlock(hdev);
508 static void cancel_adv_timeout(struct hci_dev *hdev)
510 if (hdev->adv_instance_timeout) {
511 hdev->adv_instance_timeout = 0;
512 cancel_delayed_work(&hdev->adv_instance_expire);
516 /* For a single instance:
517 * - force == true: The instance will be removed even when its remaining
518 * lifetime is not zero.
519 * - force == false: the instance will be deactivated but kept stored unless
520 * the remaining lifetime is zero.
522 * For instance == 0x00:
523 * - force == true: All instances will be removed regardless of their timeout
525 * - force == false: Only instances that have a timeout will be removed.
527 int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
528 u8 instance, bool force)
530 struct adv_info *adv_instance, *n, *next_instance = NULL;
534 /* Cancel any timeout concerning the removed instance(s). */
535 if (!instance || hdev->cur_adv_instance == instance)
536 cancel_adv_timeout(hdev);
538 /* Get the next instance to advertise BEFORE we remove
539 * the current one. This can be the same instance again
540 * if there is only one instance.
542 if (instance && hdev->cur_adv_instance == instance)
543 next_instance = hci_get_next_instance(hdev, instance);
545 if (instance == 0x00) {
546 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
548 if (!(force || adv_instance->timeout))
551 rem_inst = adv_instance->instance;
552 err = hci_remove_adv_instance(hdev, rem_inst);
554 mgmt_advertising_removed(sk, hdev, rem_inst);
557 adv_instance = hci_find_adv_instance(hdev, instance);
559 if (force || (adv_instance && adv_instance->timeout &&
560 !adv_instance->remaining_time)) {
561 /* Don't advertise a removed instance. */
563 next_instance->instance == instance)
564 next_instance = NULL;
566 err = hci_remove_adv_instance(hdev, instance);
568 mgmt_advertising_removed(sk, hdev, instance);
572 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
575 if (next_instance && !ext_adv_capable(hdev))
576 return hci_schedule_adv_instance_sync(hdev,
577 next_instance->instance,
583 static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
585 u8 instance = *(u8 *)data;
589 hci_clear_adv_instance_sync(hdev, NULL, instance, false);
591 if (list_empty(&hdev->adv_instances))
592 return hci_disable_advertising_sync(hdev);
597 static void adv_timeout_expire(struct work_struct *work)
600 struct hci_dev *hdev = container_of(work, struct hci_dev,
601 adv_instance_expire.work);
603 bt_dev_dbg(hdev, "");
607 hdev->adv_instance_timeout = 0;
609 if (hdev->cur_adv_instance == 0x00)
612 inst_ptr = kmalloc(1, GFP_KERNEL);
616 *inst_ptr = hdev->cur_adv_instance;
617 hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
620 hci_dev_unlock(hdev);
623 void hci_cmd_sync_init(struct hci_dev *hdev)
625 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
626 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
627 mutex_init(&hdev->cmd_sync_work_lock);
628 mutex_init(&hdev->unregister_lock);
630 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
631 INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
632 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
633 INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart);
634 INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
637 void hci_cmd_sync_clear(struct hci_dev *hdev)
639 struct hci_cmd_sync_work_entry *entry, *tmp;
641 cancel_work_sync(&hdev->cmd_sync_work);
642 cancel_work_sync(&hdev->reenable_adv_work);
644 mutex_lock(&hdev->cmd_sync_work_lock);
645 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
647 entry->destroy(hdev, entry->data, -ECANCELED);
649 list_del(&entry->list);
652 mutex_unlock(&hdev->cmd_sync_work_lock);
655 void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
657 bt_dev_dbg(hdev, "err 0x%2.2x", err);
659 if (hdev->req_status == HCI_REQ_PEND) {
660 hdev->req_result = err;
661 hdev->req_status = HCI_REQ_CANCELED;
663 cancel_delayed_work_sync(&hdev->cmd_timer);
664 cancel_delayed_work_sync(&hdev->ncmd_timer);
665 atomic_set(&hdev->cmd_cnt, 1);
667 wake_up_interruptible(&hdev->req_wait_q);
671 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
673 bt_dev_dbg(hdev, "err 0x%2.2x", err);
675 if (hdev->req_status == HCI_REQ_PEND) {
676 hdev->req_result = err;
677 hdev->req_status = HCI_REQ_CANCELED;
679 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
682 EXPORT_SYMBOL(hci_cmd_sync_cancel);
684 /* Submit HCI command to be run in as cmd_sync_work:
686 * - hdev must _not_ be unregistered
688 int hci_cmd_sync_submit(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_submit);
721 /* Queue HCI command:
723 * - hdev must be running
725 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
726 void *data, hci_cmd_sync_work_destroy_t destroy)
728 /* Only queue command if hdev is running which means it had been opened
729 * and is either on init phase or is already up.
731 if (!test_bit(HCI_RUNNING, &hdev->flags))
734 return hci_cmd_sync_submit(hdev, func, data, destroy);
736 EXPORT_SYMBOL(hci_cmd_sync_queue);
738 int hci_update_eir_sync(struct hci_dev *hdev)
740 struct hci_cp_write_eir cp;
742 bt_dev_dbg(hdev, "");
744 if (!hdev_is_powered(hdev))
747 if (!lmp_ext_inq_capable(hdev))
750 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
753 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
756 memset(&cp, 0, sizeof(cp));
758 eir_create(hdev, cp.data);
760 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
763 memcpy(hdev->eir, cp.data, sizeof(cp.data));
765 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
769 static u8 get_service_classes(struct hci_dev *hdev)
771 struct bt_uuid *uuid;
774 list_for_each_entry(uuid, &hdev->uuids, list)
775 val |= uuid->svc_hint;
780 int hci_update_class_sync(struct hci_dev *hdev)
784 bt_dev_dbg(hdev, "");
786 if (!hdev_is_powered(hdev))
789 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
792 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
795 cod[0] = hdev->minor_class;
796 cod[1] = hdev->major_class;
797 cod[2] = get_service_classes(hdev);
799 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
802 if (memcmp(cod, hdev->dev_class, 3) == 0)
805 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
806 sizeof(cod), cod, HCI_CMD_TIMEOUT);
809 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
811 /* If there is no connection we are OK to advertise. */
812 if (hci_conn_num(hdev, LE_LINK) == 0)
815 /* Check le_states if there is any connection in peripheral role. */
816 if (hdev->conn_hash.le_num_peripheral > 0) {
817 /* Peripheral connection state and non connectable mode
820 if (!connectable && !(hdev->le_states[2] & 0x10))
823 /* Peripheral connection state and connectable mode bit 38
824 * and scannable bit 21.
826 if (connectable && (!(hdev->le_states[4] & 0x40) ||
827 !(hdev->le_states[2] & 0x20)))
831 /* Check le_states if there is any connection in central role. */
832 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
833 /* Central connection state and non connectable mode bit 18. */
834 if (!connectable && !(hdev->le_states[2] & 0x02))
837 /* Central connection state and connectable mode bit 35 and
840 if (connectable && (!(hdev->le_states[4] & 0x08) ||
841 !(hdev->le_states[2] & 0x08)))
848 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
850 /* If privacy is not enabled don't use RPA */
851 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
854 /* If basic privacy mode is enabled use RPA */
855 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
858 /* If limited privacy mode is enabled don't use RPA if we're
859 * both discoverable and bondable.
861 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
862 hci_dev_test_flag(hdev, HCI_BONDABLE))
865 /* We're neither bondable nor discoverable in the limited
866 * privacy mode, therefore use RPA.
871 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
873 /* If we're advertising or initiating an LE connection we can't
874 * go ahead and change the random address at this time. This is
875 * because the eventual initiator address used for the
876 * subsequently created connection will be undefined (some
877 * controllers use the new address and others the one we had
878 * when the operation started).
880 * In this kind of scenario skip the update and let the random
881 * address be updated at the next cycle.
883 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
884 hci_lookup_le_connect(hdev)) {
885 bt_dev_dbg(hdev, "Deferring random address update");
886 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
890 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
891 6, rpa, HCI_CMD_TIMEOUT);
894 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
895 bool rpa, u8 *own_addr_type)
899 /* If privacy is enabled use a resolvable private address. If
900 * current RPA has expired or there is something else than
901 * the current RPA in use, then generate a new one.
904 /* If Controller supports LL Privacy use own address type is
907 if (use_ll_privacy(hdev))
908 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
910 *own_addr_type = ADDR_LE_DEV_RANDOM;
912 /* Check if RPA is valid */
916 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
918 bt_dev_err(hdev, "failed to generate new RPA");
922 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
929 /* In case of required privacy without resolvable private address,
930 * use an non-resolvable private address. This is useful for active
931 * scanning and non-connectable advertising.
933 if (require_privacy) {
937 /* The non-resolvable private address is generated
938 * from random six bytes with the two most significant
941 get_random_bytes(&nrpa, 6);
944 /* The non-resolvable private address shall not be
945 * equal to the public address.
947 if (bacmp(&hdev->bdaddr, &nrpa))
951 *own_addr_type = ADDR_LE_DEV_RANDOM;
953 return hci_set_random_addr_sync(hdev, &nrpa);
956 /* If forcing static address is in use or there is no public
957 * address use the static address as random address (but skip
958 * the HCI command if the current random address is already the
961 * In case BR/EDR has been disabled on a dual-mode controller
962 * and a static address has been configured, then use that
963 * address instead of the public BR/EDR address.
965 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
966 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
967 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
968 bacmp(&hdev->static_addr, BDADDR_ANY))) {
969 *own_addr_type = ADDR_LE_DEV_RANDOM;
970 if (bacmp(&hdev->static_addr, &hdev->random_addr))
971 return hci_set_random_addr_sync(hdev,
976 /* Neither privacy nor static address is being used so use a
979 *own_addr_type = ADDR_LE_DEV_PUBLIC;
984 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
986 struct hci_cp_le_set_ext_adv_enable *cp;
987 struct hci_cp_ext_adv_set *set;
988 u8 data[sizeof(*cp) + sizeof(*set) * 1];
991 /* If request specifies an instance that doesn't exist, fail */
993 struct adv_info *adv;
995 adv = hci_find_adv_instance(hdev, instance);
999 /* If not enabled there is nothing to do */
1004 memset(data, 0, sizeof(data));
1007 set = (void *)cp->data;
1009 /* Instance 0x00 indicates all advertising instances will be disabled */
1010 cp->num_of_sets = !!instance;
1013 set->handle = instance;
1015 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
1017 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1018 size, data, HCI_CMD_TIMEOUT);
1021 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1022 bdaddr_t *random_addr)
1024 struct hci_cp_le_set_adv_set_rand_addr cp;
1028 /* Instance 0x00 doesn't have an adv_info, instead it uses
1029 * hdev->random_addr to track its address so whenever it needs
1030 * to be updated this also set the random address since
1031 * hdev->random_addr is shared with scan state machine.
1033 err = hci_set_random_addr_sync(hdev, random_addr);
1038 memset(&cp, 0, sizeof(cp));
1040 cp.handle = instance;
1041 bacpy(&cp.bdaddr, random_addr);
1043 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1044 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1047 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1049 struct hci_cp_le_set_ext_adv_params cp;
1052 bdaddr_t random_addr;
1055 struct adv_info *adv;
1059 adv = hci_find_adv_instance(hdev, instance);
1066 /* Updating parameters of an active instance will return a
1067 * Command Disallowed error, so we must first disable the
1068 * instance if it is active.
1070 if (adv && !adv->pending) {
1071 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1076 flags = hci_adv_instance_flags(hdev, instance);
1078 /* If the "connectable" instance flag was not set, then choose between
1079 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1081 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1082 mgmt_get_connectable(hdev);
1084 if (!is_advertising_allowed(hdev, connectable))
1087 /* Set require_privacy to true only when non-connectable
1088 * advertising is used. In that case it is fine to use a
1089 * non-resolvable private address.
1091 err = hci_get_random_address(hdev, !connectable,
1092 adv_use_rpa(hdev, flags), adv,
1093 &own_addr_type, &random_addr);
1097 memset(&cp, 0, sizeof(cp));
1100 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
1101 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
1102 cp.tx_power = adv->tx_power;
1104 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1105 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1106 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
1109 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
1113 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
1115 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
1116 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1117 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
1119 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
1121 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
1124 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
1126 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
1129 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
1130 * contains the peer’s Identity Address and the Peer_Address_Type
1131 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
1132 * These parameters are used to locate the corresponding local IRK in
1133 * the resolving list; this IRK is used to generate their own address
1134 * used in the advertisement.
1136 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
1137 hci_copy_identity_address(hdev, &cp.peer_addr,
1138 &cp.peer_addr_type);
1140 cp.own_addr_type = own_addr_type;
1141 cp.channel_map = hdev->le_adv_channel_map;
1142 cp.handle = instance;
1144 if (flags & MGMT_ADV_FLAG_SEC_2M) {
1145 cp.primary_phy = HCI_ADV_PHY_1M;
1146 cp.secondary_phy = HCI_ADV_PHY_2M;
1147 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
1148 cp.primary_phy = HCI_ADV_PHY_CODED;
1149 cp.secondary_phy = HCI_ADV_PHY_CODED;
1151 /* In all other cases use 1M */
1152 cp.primary_phy = HCI_ADV_PHY_1M;
1153 cp.secondary_phy = HCI_ADV_PHY_1M;
1156 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1157 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1161 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
1162 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
1163 bacmp(&random_addr, BDADDR_ANY)) {
1164 /* Check if random address need to be updated */
1166 if (!bacmp(&random_addr, &adv->random_addr))
1169 if (!bacmp(&random_addr, &hdev->random_addr))
1173 return hci_set_adv_set_random_addr_sync(hdev, instance,
1180 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1183 struct hci_cp_le_set_ext_scan_rsp_data cp;
1184 u8 data[HCI_MAX_EXT_AD_LENGTH];
1187 struct adv_info *adv = NULL;
1190 memset(&pdu, 0, sizeof(pdu));
1193 adv = hci_find_adv_instance(hdev, instance);
1194 if (!adv || !adv->scan_rsp_changed)
1198 len = eir_create_scan_rsp(hdev, instance, pdu.data);
1200 pdu.cp.handle = instance;
1201 pdu.cp.length = len;
1202 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1203 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1205 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1206 sizeof(pdu.cp) + len, &pdu.cp,
1212 adv->scan_rsp_changed = false;
1214 memcpy(hdev->scan_rsp_data, pdu.data, len);
1215 hdev->scan_rsp_data_len = len;
1221 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1223 struct hci_cp_le_set_scan_rsp_data cp;
1226 memset(&cp, 0, sizeof(cp));
1228 len = eir_create_scan_rsp(hdev, instance, cp.data);
1230 if (hdev->scan_rsp_data_len == len &&
1231 !memcmp(cp.data, hdev->scan_rsp_data, len))
1234 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1235 hdev->scan_rsp_data_len = len;
1239 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1240 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1243 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1245 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1248 if (ext_adv_capable(hdev))
1249 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1251 return __hci_set_scan_rsp_data_sync(hdev, instance);
1254 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1256 struct hci_cp_le_set_ext_adv_enable *cp;
1257 struct hci_cp_ext_adv_set *set;
1258 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1259 struct adv_info *adv;
1262 adv = hci_find_adv_instance(hdev, instance);
1265 /* If already enabled there is nothing to do */
1273 set = (void *)cp->data;
1275 memset(cp, 0, sizeof(*cp));
1278 cp->num_of_sets = 0x01;
1280 memset(set, 0, sizeof(*set));
1282 set->handle = instance;
1284 /* Set duration per instance since controller is responsible for
1287 if (adv && adv->timeout) {
1288 u16 duration = adv->timeout * MSEC_PER_SEC;
1290 /* Time = N * 10 ms */
1291 set->duration = cpu_to_le16(duration / 10);
1294 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1296 sizeof(*set) * cp->num_of_sets,
1297 data, HCI_CMD_TIMEOUT);
1300 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1304 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1308 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1312 return hci_enable_ext_advertising_sync(hdev, instance);
1315 static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1317 struct hci_cp_le_set_per_adv_enable cp;
1318 struct adv_info *adv = NULL;
1320 /* If periodic advertising already disabled there is nothing to do. */
1321 adv = hci_find_adv_instance(hdev, instance);
1322 if (!adv || !adv->periodic || !adv->enabled)
1325 memset(&cp, 0, sizeof(cp));
1328 cp.handle = instance;
1330 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1331 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1334 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1335 u16 min_interval, u16 max_interval)
1337 struct hci_cp_le_set_per_adv_params cp;
1339 memset(&cp, 0, sizeof(cp));
1342 min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1345 max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1347 cp.handle = instance;
1348 cp.min_interval = cpu_to_le16(min_interval);
1349 cp.max_interval = cpu_to_le16(max_interval);
1350 cp.periodic_properties = 0x0000;
1352 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1353 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1356 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1359 struct hci_cp_le_set_per_adv_data cp;
1360 u8 data[HCI_MAX_PER_AD_LENGTH];
1364 memset(&pdu, 0, sizeof(pdu));
1367 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1369 if (!adv || !adv->periodic)
1373 len = eir_create_per_adv_data(hdev, instance, pdu.data);
1375 pdu.cp.length = len;
1376 pdu.cp.handle = instance;
1377 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1379 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1380 sizeof(pdu.cp) + len, &pdu,
1384 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1386 struct hci_cp_le_set_per_adv_enable cp;
1387 struct adv_info *adv = NULL;
1389 /* If periodic advertising already enabled there is nothing to do. */
1390 adv = hci_find_adv_instance(hdev, instance);
1391 if (adv && adv->periodic && adv->enabled)
1394 memset(&cp, 0, sizeof(cp));
1397 cp.handle = instance;
1399 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1400 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1403 /* Checks if periodic advertising data contains a Basic Announcement and if it
1404 * does generates a Broadcast ID and add Broadcast Announcement.
1406 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1411 /* Skip if NULL adv as instance 0x00 is used for general purpose
1412 * advertising so it cannot used for the likes of Broadcast Announcement
1413 * as it can be overwritten at any point.
1418 /* Check if PA data doesn't contains a Basic Audio Announcement then
1419 * there is nothing to do.
1421 if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1425 /* Check if advertising data already has a Broadcast Announcement since
1426 * the process may want to control the Broadcast ID directly and in that
1427 * case the kernel shall no interfere.
1429 if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1433 /* Generate Broadcast ID */
1434 get_random_bytes(bid, sizeof(bid));
1435 eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1436 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1438 return hci_update_adv_data_sync(hdev, adv->instance);
1441 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1442 u8 *data, u32 flags, u16 min_interval,
1443 u16 max_interval, u16 sync_interval)
1445 struct adv_info *adv = NULL;
1449 hci_disable_per_advertising_sync(hdev, instance);
1452 adv = hci_find_adv_instance(hdev, instance);
1453 /* Create an instance if that could not be found */
1455 adv = hci_add_per_instance(hdev, instance, flags,
1460 return PTR_ERR(adv);
1461 adv->pending = false;
1466 /* Start advertising */
1467 err = hci_start_ext_adv_sync(hdev, instance);
1471 err = hci_adv_bcast_annoucement(hdev, adv);
1475 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1480 err = hci_set_per_adv_data_sync(hdev, instance);
1484 err = hci_enable_per_advertising_sync(hdev, instance);
1492 hci_remove_adv_instance(hdev, instance);
1497 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1501 if (ext_adv_capable(hdev))
1502 return hci_start_ext_adv_sync(hdev, instance);
1504 err = hci_update_adv_data_sync(hdev, instance);
1508 err = hci_update_scan_rsp_data_sync(hdev, instance);
1512 return hci_enable_advertising_sync(hdev);
1515 int hci_enable_advertising_sync(struct hci_dev *hdev)
1517 struct adv_info *adv_instance;
1518 struct hci_cp_le_set_adv_param cp;
1519 u8 own_addr_type, enable = 0x01;
1521 u16 adv_min_interval, adv_max_interval;
1525 if (ext_adv_capable(hdev))
1526 return hci_enable_ext_advertising_sync(hdev,
1527 hdev->cur_adv_instance);
1529 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1530 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1532 /* If the "connectable" instance flag was not set, then choose between
1533 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1535 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1536 mgmt_get_connectable(hdev);
1538 if (!is_advertising_allowed(hdev, connectable))
1541 status = hci_disable_advertising_sync(hdev);
1545 /* Clear the HCI_LE_ADV bit temporarily so that the
1546 * hci_update_random_address knows that it's safe to go ahead
1547 * and write a new random address. The flag will be set back on
1548 * as soon as the SET_ADV_ENABLE HCI command completes.
1550 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1552 /* Set require_privacy to true only when non-connectable
1553 * advertising is used. In that case it is fine to use a
1554 * non-resolvable private address.
1556 status = hci_update_random_address_sync(hdev, !connectable,
1557 adv_use_rpa(hdev, flags),
1562 memset(&cp, 0, sizeof(cp));
1565 adv_min_interval = adv_instance->min_interval;
1566 adv_max_interval = adv_instance->max_interval;
1568 adv_min_interval = hdev->le_adv_min_interval;
1569 adv_max_interval = hdev->le_adv_max_interval;
1573 cp.type = LE_ADV_IND;
1575 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1576 cp.type = LE_ADV_SCAN_IND;
1578 cp.type = LE_ADV_NONCONN_IND;
1580 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1581 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1582 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1583 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1587 cp.min_interval = cpu_to_le16(adv_min_interval);
1588 cp.max_interval = cpu_to_le16(adv_max_interval);
1589 cp.own_address_type = own_addr_type;
1590 cp.channel_map = hdev->le_adv_channel_map;
1592 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1593 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1597 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1598 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1601 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1603 return hci_enable_advertising_sync(hdev);
1606 int hci_enable_advertising(struct hci_dev *hdev)
1608 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1609 list_empty(&hdev->adv_instances))
1612 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1615 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1620 if (!ext_adv_capable(hdev))
1623 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1627 /* If request specifies an instance that doesn't exist, fail */
1628 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1631 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1632 sizeof(instance), &instance, 0,
1633 HCI_CMD_TIMEOUT, sk);
1636 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1638 struct adv_info *adv = data;
1642 instance = adv->instance;
1644 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1647 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1649 struct adv_info *adv = NULL;
1652 adv = hci_find_adv_instance(hdev, instance);
1657 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1660 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1662 struct hci_cp_le_term_big cp;
1664 memset(&cp, 0, sizeof(cp));
1668 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1669 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1672 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1675 struct hci_cp_le_set_ext_adv_data cp;
1676 u8 data[HCI_MAX_EXT_AD_LENGTH];
1679 struct adv_info *adv = NULL;
1682 memset(&pdu, 0, sizeof(pdu));
1685 adv = hci_find_adv_instance(hdev, instance);
1686 if (!adv || !adv->adv_data_changed)
1690 len = eir_create_adv_data(hdev, instance, pdu.data);
1692 pdu.cp.length = len;
1693 pdu.cp.handle = instance;
1694 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1695 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1697 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1698 sizeof(pdu.cp) + len, &pdu.cp,
1703 /* Update data if the command succeed */
1705 adv->adv_data_changed = false;
1707 memcpy(hdev->adv_data, pdu.data, len);
1708 hdev->adv_data_len = len;
1714 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1716 struct hci_cp_le_set_adv_data cp;
1719 memset(&cp, 0, sizeof(cp));
1721 len = eir_create_adv_data(hdev, instance, cp.data);
1723 /* There's nothing to do if the data hasn't changed */
1724 if (hdev->adv_data_len == len &&
1725 memcmp(cp.data, hdev->adv_data, len) == 0)
1728 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1729 hdev->adv_data_len = len;
1733 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1734 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1737 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1739 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1742 if (ext_adv_capable(hdev))
1743 return hci_set_ext_adv_data_sync(hdev, instance);
1745 return hci_set_adv_data_sync(hdev, instance);
1748 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1751 struct adv_info *adv = NULL;
1754 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1757 if (hdev->adv_instance_timeout)
1760 adv = hci_find_adv_instance(hdev, instance);
1764 /* A zero timeout means unlimited advertising. As long as there is
1765 * only one instance, duration should be ignored. We still set a timeout
1766 * in case further instances are being added later on.
1768 * If the remaining lifetime of the instance is more than the duration
1769 * then the timeout corresponds to the duration, otherwise it will be
1770 * reduced to the remaining instance lifetime.
1772 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1773 timeout = adv->duration;
1775 timeout = adv->remaining_time;
1777 /* The remaining time is being reduced unless the instance is being
1778 * advertised without time limit.
1781 adv->remaining_time = adv->remaining_time - timeout;
1783 /* Only use work for scheduling instances with legacy advertising */
1784 if (!ext_adv_capable(hdev)) {
1785 hdev->adv_instance_timeout = timeout;
1786 queue_delayed_work(hdev->req_workqueue,
1787 &hdev->adv_instance_expire,
1788 msecs_to_jiffies(timeout * 1000));
1791 /* If we're just re-scheduling the same instance again then do not
1792 * execute any HCI commands. This happens when a single instance is
1795 if (!force && hdev->cur_adv_instance == instance &&
1796 hci_dev_test_flag(hdev, HCI_LE_ADV))
1799 hdev->cur_adv_instance = instance;
1801 return hci_start_adv_sync(hdev, instance);
1804 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1808 if (!ext_adv_capable(hdev))
1811 /* Disable instance 0x00 to disable all instances */
1812 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1816 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1817 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1820 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1822 struct adv_info *adv, *n;
1825 if (ext_adv_capable(hdev))
1826 /* Remove all existing sets */
1827 err = hci_clear_adv_sets_sync(hdev, sk);
1828 if (ext_adv_capable(hdev))
1831 /* This is safe as long as there is no command send while the lock is
1836 /* Cleanup non-ext instances */
1837 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1838 u8 instance = adv->instance;
1841 if (!(force || adv->timeout))
1844 err = hci_remove_adv_instance(hdev, instance);
1846 mgmt_advertising_removed(sk, hdev, instance);
1849 hci_dev_unlock(hdev);
1854 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1859 /* If we use extended advertising, instance has to be removed first. */
1860 if (ext_adv_capable(hdev))
1861 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1862 if (ext_adv_capable(hdev))
1865 /* This is safe as long as there is no command send while the lock is
1870 err = hci_remove_adv_instance(hdev, instance);
1872 mgmt_advertising_removed(sk, hdev, instance);
1874 hci_dev_unlock(hdev);
1879 /* For a single instance:
1880 * - force == true: The instance will be removed even when its remaining
1881 * lifetime is not zero.
1882 * - force == false: the instance will be deactivated but kept stored unless
1883 * the remaining lifetime is zero.
1885 * For instance == 0x00:
1886 * - force == true: All instances will be removed regardless of their timeout
1888 * - force == false: Only instances that have a timeout will be removed.
1890 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1891 u8 instance, bool force)
1893 struct adv_info *next = NULL;
1896 /* Cancel any timeout concerning the removed instance(s). */
1897 if (!instance || hdev->cur_adv_instance == instance)
1898 cancel_adv_timeout(hdev);
1900 /* Get the next instance to advertise BEFORE we remove
1901 * the current one. This can be the same instance again
1902 * if there is only one instance.
1904 if (hdev->cur_adv_instance == instance)
1905 next = hci_get_next_instance(hdev, instance);
1908 err = hci_clear_adv_sync(hdev, sk, force);
1912 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1914 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1915 /* Don't advertise a removed instance. */
1916 if (next && next->instance == instance)
1919 err = hci_remove_adv_sync(hdev, instance, sk);
1925 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1928 if (next && !ext_adv_capable(hdev))
1929 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1934 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1936 struct hci_cp_read_rssi cp;
1939 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1940 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1943 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1945 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1946 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1949 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1951 struct hci_cp_read_tx_power cp;
1955 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1956 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1959 int hci_disable_advertising_sync(struct hci_dev *hdev)
1964 /* If controller is not advertising we are done. */
1965 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1968 if (ext_adv_capable(hdev))
1969 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1970 if (ext_adv_capable(hdev))
1973 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1974 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1977 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1980 struct hci_cp_le_set_ext_scan_enable cp;
1982 memset(&cp, 0, sizeof(cp));
1985 if (hci_dev_test_flag(hdev, HCI_MESH))
1986 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
1988 cp.filter_dup = filter_dup;
1990 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1991 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1994 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
1997 struct hci_cp_le_set_scan_enable cp;
1999 if (use_ext_scan(hdev))
2000 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2002 memset(&cp, 0, sizeof(cp));
2005 if (val && hci_dev_test_flag(hdev, HCI_MESH))
2006 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2008 cp.filter_dup = filter_dup;
2010 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2011 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2014 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2016 if (!use_ll_privacy(hdev))
2019 /* If controller is not/already resolving we are done. */
2020 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2023 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2024 sizeof(val), &val, HCI_CMD_TIMEOUT);
2027 static int hci_scan_disable_sync(struct hci_dev *hdev)
2031 /* If controller is not scanning we are done. */
2032 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2035 if (hdev->scanning_paused) {
2036 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2040 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2042 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2049 static bool scan_use_rpa(struct hci_dev *hdev)
2051 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2054 static void hci_start_interleave_scan(struct hci_dev *hdev)
2056 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2057 queue_delayed_work(hdev->req_workqueue,
2058 &hdev->interleave_scan, 0);
2061 static bool is_interleave_scanning(struct hci_dev *hdev)
2063 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2066 static void cancel_interleave_scan(struct hci_dev *hdev)
2068 bt_dev_dbg(hdev, "cancelling interleave scan");
2070 cancel_delayed_work_sync(&hdev->interleave_scan);
2072 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2075 /* Return true if interleave_scan wasn't started until exiting this function,
2076 * otherwise, return false
2078 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2080 /* Do interleaved scan only if all of the following are true:
2081 * - There is at least one ADV monitor
2082 * - At least one pending LE connection or one device to be scanned for
2083 * - Monitor offloading is not supported
2084 * If so, we should alternate between allowlist scan and one without
2085 * any filters to save power.
2087 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2088 !(list_empty(&hdev->pend_le_conns) &&
2089 list_empty(&hdev->pend_le_reports)) &&
2090 hci_get_adv_monitor_offload_ext(hdev) ==
2091 HCI_ADV_MONITOR_EXT_NONE;
2092 bool is_interleaving = is_interleave_scanning(hdev);
2094 if (use_interleaving && !is_interleaving) {
2095 hci_start_interleave_scan(hdev);
2096 bt_dev_dbg(hdev, "starting interleave scan");
2100 if (!use_interleaving && is_interleaving)
2101 cancel_interleave_scan(hdev);
2106 /* Removes connection to resolve list if needed.*/
2107 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2108 bdaddr_t *bdaddr, u8 bdaddr_type)
2110 struct hci_cp_le_del_from_resolv_list cp;
2111 struct bdaddr_list_with_irk *entry;
2113 if (!use_ll_privacy(hdev))
2116 /* Check if the IRK has been programmed */
2117 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2122 cp.bdaddr_type = bdaddr_type;
2123 bacpy(&cp.bdaddr, bdaddr);
2125 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2126 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2129 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2130 bdaddr_t *bdaddr, u8 bdaddr_type)
2132 struct hci_cp_le_del_from_accept_list cp;
2135 /* Check if device is on accept list before removing it */
2136 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2139 cp.bdaddr_type = bdaddr_type;
2140 bacpy(&cp.bdaddr, bdaddr);
2142 /* Ignore errors when removing from resolving list as that is likely
2143 * that the device was never added.
2145 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2147 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2148 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2150 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2154 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2160 struct conn_params {
2163 hci_conn_flags_t flags;
2167 /* Adds connection to resolve list if needed.
2168 * Setting params to NULL programs local hdev->irk
2170 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2171 struct conn_params *params)
2173 struct hci_cp_le_add_to_resolv_list cp;
2174 struct smp_irk *irk;
2175 struct bdaddr_list_with_irk *entry;
2176 struct hci_conn_params *p;
2178 if (!use_ll_privacy(hdev))
2181 /* Attempt to program local identity address, type and irk if params is
2185 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2188 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2189 memcpy(cp.peer_irk, hdev->irk, 16);
2193 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2197 /* Check if the IK has _not_ been programmed yet. */
2198 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2204 cp.bdaddr_type = params->addr_type;
2205 bacpy(&cp.bdaddr, ¶ms->addr);
2206 memcpy(cp.peer_irk, irk->val, 16);
2208 /* Default privacy mode is always Network */
2209 params->privacy_mode = HCI_NETWORK_PRIVACY;
2212 p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2213 ¶ms->addr, params->addr_type);
2215 p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2216 ¶ms->addr, params->addr_type);
2218 WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY);
2222 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2223 memcpy(cp.local_irk, hdev->irk, 16);
2225 memset(cp.local_irk, 0, 16);
2227 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2228 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2231 /* Set Device Privacy Mode. */
2232 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2233 struct conn_params *params)
2235 struct hci_cp_le_set_privacy_mode cp;
2236 struct smp_irk *irk;
2238 /* If device privacy mode has already been set there is nothing to do */
2239 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2242 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2243 * indicates that LL Privacy has been enabled and
2244 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2246 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2249 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2253 memset(&cp, 0, sizeof(cp));
2254 cp.bdaddr_type = irk->addr_type;
2255 bacpy(&cp.bdaddr, &irk->bdaddr);
2256 cp.mode = HCI_DEVICE_PRIVACY;
2258 /* Note: params->privacy_mode is not updated since it is a copy */
2260 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2261 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2264 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2265 * this attempts to program the device in the resolving list as well and
2266 * properly set the privacy mode.
2268 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2269 struct conn_params *params,
2272 struct hci_cp_le_add_to_accept_list cp;
2275 /* During suspend, only wakeable devices can be in acceptlist */
2276 if (hdev->suspended &&
2277 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
2280 /* Select filter policy to accept all advertising */
2281 if (*num_entries >= hdev->le_accept_list_size)
2284 /* Accept list can not be used with RPAs */
2285 if (!use_ll_privacy(hdev) &&
2286 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type))
2289 /* Attempt to program the device in the resolving list first to avoid
2290 * having to rollback in case it fails since the resolving list is
2291 * dynamic it can probably be smaller than the accept list.
2293 err = hci_le_add_resolve_list_sync(hdev, params);
2295 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2299 /* Set Privacy Mode */
2300 err = hci_le_set_privacy_mode_sync(hdev, params);
2302 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2306 /* Check if already in accept list */
2307 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
2312 cp.bdaddr_type = params->addr_type;
2313 bacpy(&cp.bdaddr, ¶ms->addr);
2315 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2316 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2318 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2319 /* Rollback the device from the resolving list */
2320 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2324 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2330 /* This function disables/pause all advertising instances */
2331 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2336 /* If already been paused there is nothing to do. */
2337 if (hdev->advertising_paused)
2340 bt_dev_dbg(hdev, "Pausing directed advertising");
2342 /* Stop directed advertising */
2343 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2345 /* When discoverable timeout triggers, then just make sure
2346 * the limited discoverable flag is cleared. Even in the case
2347 * of a timeout triggered from general discoverable, it is
2348 * safe to unconditionally clear the flag.
2350 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2351 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2352 hdev->discov_timeout = 0;
2355 bt_dev_dbg(hdev, "Pausing advertising instances");
2357 /* Call to disable any advertisements active on the controller.
2358 * This will succeed even if no advertisements are configured.
2360 err = hci_disable_advertising_sync(hdev);
2364 /* If we are using software rotation, pause the loop */
2365 if (!ext_adv_capable(hdev))
2366 cancel_adv_timeout(hdev);
2368 hdev->advertising_paused = true;
2369 hdev->advertising_old_state = old_state;
2374 /* This function enables all user advertising instances */
2375 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2377 struct adv_info *adv, *tmp;
2380 /* If advertising has not been paused there is nothing to do. */
2381 if (!hdev->advertising_paused)
2384 /* Resume directed advertising */
2385 hdev->advertising_paused = false;
2386 if (hdev->advertising_old_state) {
2387 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2388 hdev->advertising_old_state = 0;
2391 bt_dev_dbg(hdev, "Resuming advertising instances");
2393 if (ext_adv_capable(hdev)) {
2394 /* Call for each tracked instance to be re-enabled */
2395 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2396 err = hci_enable_ext_advertising_sync(hdev,
2401 /* If the instance cannot be resumed remove it */
2402 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2406 /* Schedule for most recent instance to be restarted and begin
2407 * the software rotation loop
2409 err = hci_schedule_adv_instance_sync(hdev,
2410 hdev->cur_adv_instance,
2414 hdev->advertising_paused = false;
2419 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2423 if (!use_ll_privacy(hdev))
2426 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2429 /* Cannot disable addr resolution if scanning is enabled or
2430 * when initiating an LE connection.
2432 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2433 hci_lookup_le_connect(hdev)) {
2434 bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2438 /* Cannot disable addr resolution if advertising is enabled. */
2439 err = hci_pause_advertising_sync(hdev);
2441 bt_dev_err(hdev, "Pause advertising failed: %d", err);
2445 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2447 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2450 /* Return if address resolution is disabled and RPA is not used. */
2451 if (!err && scan_use_rpa(hdev))
2454 hci_resume_advertising_sync(hdev);
2458 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2459 bool extended, struct sock *sk)
2461 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2462 HCI_OP_READ_LOCAL_OOB_DATA;
2464 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2467 static struct conn_params *conn_params_copy(struct list_head *list, size_t *n)
2469 struct hci_conn_params *params;
2470 struct conn_params *p;
2476 list_for_each_entry_rcu(params, list, action)
2482 p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL);
2489 list_for_each_entry_rcu(params, list, action) {
2490 /* Racing adds are handled in next scan update */
2494 /* No hdev->lock, but: addr, addr_type are immutable.
2495 * privacy_mode is only written by us or in
2496 * hci_cc_le_set_privacy_mode that we wait for.
2497 * We should be idempotent so MGMT updating flags
2498 * while we are processing is OK.
2500 bacpy(&p[i].addr, ¶ms->addr);
2501 p[i].addr_type = params->addr_type;
2502 p[i].flags = READ_ONCE(params->flags);
2503 p[i].privacy_mode = READ_ONCE(params->privacy_mode);
2513 /* Device must not be scanning when updating the accept list.
2515 * Update is done using the following sequence:
2517 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
2518 * Remove Devices From Accept List ->
2519 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
2520 * Add Devices to Accept List ->
2521 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
2522 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
2525 * In case of failure advertising shall be restored to its original state and
2526 * return would disable accept list since either accept or resolving list could
2527 * not be programmed.
2530 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2532 struct conn_params *params;
2533 struct bdaddr_list *b, *t;
2535 bool pend_conn, pend_report;
2540 /* Pause advertising if resolving list can be used as controllers
2541 * cannot accept resolving list modifications while advertising.
2543 if (use_ll_privacy(hdev)) {
2544 err = hci_pause_advertising_sync(hdev);
2546 bt_dev_err(hdev, "pause advertising failed: %d", err);
2551 /* Disable address resolution while reprogramming accept list since
2552 * devices that do have an IRK will be programmed in the resolving list
2553 * when LL Privacy is enabled.
2555 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2557 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2561 /* Go through the current accept list programmed into the
2562 * controller one by one and check if that address is connected or is
2563 * still in the list of pending connections or list of devices to
2564 * report. If not present in either list, then remove it from
2567 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2568 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2571 /* Pointers not dereferenced, no locks needed */
2572 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2575 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2579 /* If the device is not likely to connect or report,
2580 * remove it from the acceptlist.
2582 if (!pend_conn && !pend_report) {
2583 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2591 /* Since all no longer valid accept list entries have been
2592 * removed, walk through the list of pending connections
2593 * and ensure that any new device gets programmed into
2596 * If the list of the devices is larger than the list of
2597 * available accept list entries in the controller, then
2598 * just abort and return filer policy value to not use the
2601 * The list and params may be mutated while we wait for events,
2602 * so make a copy and iterate it.
2605 params = conn_params_copy(&hdev->pend_le_conns, &n);
2611 for (i = 0; i < n; ++i) {
2612 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2622 /* After adding all new pending connections, walk through
2623 * the list of pending reports and also add these to the
2624 * accept list if there is still space. Abort if space runs out.
2627 params = conn_params_copy(&hdev->pend_le_reports, &n);
2633 for (i = 0; i < n; ++i) {
2634 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2644 /* Use the allowlist unless the following conditions are all true:
2645 * - We are not currently suspending
2646 * - There are 1 or more ADV monitors registered and it's not offloaded
2647 * - Interleaved scanning is not currently using the allowlist
2649 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2650 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2651 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2655 filter_policy = err ? 0x00 : 0x01;
2657 /* Enable address resolution when LL Privacy is enabled. */
2658 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2660 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2662 /* Resume advertising if it was paused */
2663 if (use_ll_privacy(hdev))
2664 hci_resume_advertising_sync(hdev);
2666 /* Select filter policy to use accept list */
2667 return filter_policy;
2670 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2671 u16 interval, u16 window,
2672 u8 own_addr_type, u8 filter_policy)
2674 struct hci_cp_le_set_ext_scan_params *cp;
2675 struct hci_cp_le_scan_phy_params *phy;
2676 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2680 phy = (void *)cp->data;
2682 memset(data, 0, sizeof(data));
2684 cp->own_addr_type = own_addr_type;
2685 cp->filter_policy = filter_policy;
2687 if (scan_1m(hdev) || scan_2m(hdev)) {
2688 cp->scanning_phys |= LE_SCAN_PHY_1M;
2691 phy->interval = cpu_to_le16(interval);
2692 phy->window = cpu_to_le16(window);
2698 if (scan_coded(hdev)) {
2699 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2702 phy->interval = cpu_to_le16(interval);
2703 phy->window = cpu_to_le16(window);
2709 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2710 sizeof(*cp) + sizeof(*phy) * num_phy,
2711 data, HCI_CMD_TIMEOUT);
2714 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2715 u16 interval, u16 window,
2716 u8 own_addr_type, u8 filter_policy)
2718 struct hci_cp_le_set_scan_param cp;
2720 if (use_ext_scan(hdev))
2721 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2722 window, own_addr_type,
2725 memset(&cp, 0, sizeof(cp));
2727 cp.interval = cpu_to_le16(interval);
2728 cp.window = cpu_to_le16(window);
2729 cp.own_address_type = own_addr_type;
2730 cp.filter_policy = filter_policy;
2732 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2733 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2736 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2737 u16 window, u8 own_addr_type, u8 filter_policy,
2742 if (hdev->scanning_paused) {
2743 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2747 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2748 own_addr_type, filter_policy);
2752 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2755 static int hci_passive_scan_sync(struct hci_dev *hdev)
2759 u16 window, interval;
2760 u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2763 if (hdev->scanning_paused) {
2764 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2768 err = hci_scan_disable_sync(hdev);
2770 bt_dev_err(hdev, "disable scanning failed: %d", err);
2774 /* Set require_privacy to false since no SCAN_REQ are send
2775 * during passive scanning. Not using an non-resolvable address
2776 * here is important so that peer devices using direct
2777 * advertising with our address will be correctly reported
2778 * by the controller.
2780 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2784 if (hdev->enable_advmon_interleave_scan &&
2785 hci_update_interleaved_scan_sync(hdev))
2788 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2790 /* Adding or removing entries from the accept list must
2791 * happen before enabling scanning. The controller does
2792 * not allow accept list modification while scanning.
2794 filter_policy = hci_update_accept_list_sync(hdev);
2796 /* When the controller is using random resolvable addresses and
2797 * with that having LE privacy enabled, then controllers with
2798 * Extended Scanner Filter Policies support can now enable support
2799 * for handling directed advertising.
2801 * So instead of using filter polices 0x00 (no acceptlist)
2802 * and 0x01 (acceptlist enabled) use the new filter policies
2803 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2805 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2806 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2807 filter_policy |= 0x02;
2809 if (hdev->suspended) {
2810 window = hdev->le_scan_window_suspend;
2811 interval = hdev->le_scan_int_suspend;
2812 } else if (hci_is_le_conn_scanning(hdev)) {
2813 window = hdev->le_scan_window_connect;
2814 interval = hdev->le_scan_int_connect;
2815 } else if (hci_is_adv_monitoring(hdev)) {
2816 window = hdev->le_scan_window_adv_monitor;
2817 interval = hdev->le_scan_int_adv_monitor;
2819 window = hdev->le_scan_window;
2820 interval = hdev->le_scan_interval;
2823 /* Disable all filtering for Mesh */
2824 if (hci_dev_test_flag(hdev, HCI_MESH)) {
2826 filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
2829 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2831 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2832 own_addr_type, filter_policy, filter_dups);
2835 /* This function controls the passive scanning based on hdev->pend_le_conns
2836 * list. If there are pending LE connection we start the background scanning,
2837 * otherwise we stop it in the following sequence:
2839 * If there are devices to scan:
2841 * Disable Scanning -> Update Accept List ->
2842 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2843 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2850 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2854 if (!test_bit(HCI_UP, &hdev->flags) ||
2855 test_bit(HCI_INIT, &hdev->flags) ||
2856 hci_dev_test_flag(hdev, HCI_SETUP) ||
2857 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2858 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2859 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2862 /* No point in doing scanning if LE support hasn't been enabled */
2863 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2866 /* If discovery is active don't interfere with it */
2867 if (hdev->discovery.state != DISCOVERY_STOPPED)
2870 /* Reset RSSI and UUID filters when starting background scanning
2871 * since these filters are meant for service discovery only.
2873 * The Start Discovery and Start Service Discovery operations
2874 * ensure to set proper values for RSSI threshold and UUID
2875 * filter list. So it is safe to just reset them here.
2877 hci_discovery_filter_clear(hdev);
2879 bt_dev_dbg(hdev, "ADV monitoring is %s",
2880 hci_is_adv_monitoring(hdev) ? "on" : "off");
2882 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
2883 list_empty(&hdev->pend_le_conns) &&
2884 list_empty(&hdev->pend_le_reports) &&
2885 !hci_is_adv_monitoring(hdev) &&
2886 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2887 /* If there is no pending LE connections or devices
2888 * to be scanned for or no ADV monitors, we should stop the
2889 * background scanning.
2892 bt_dev_dbg(hdev, "stopping background scanning");
2894 err = hci_scan_disable_sync(hdev);
2896 bt_dev_err(hdev, "stop background scanning failed: %d",
2899 /* If there is at least one pending LE connection, we should
2900 * keep the background scan running.
2903 /* If controller is connecting, we should not start scanning
2904 * since some controllers are not able to scan and connect at
2907 if (hci_lookup_le_connect(hdev))
2910 bt_dev_dbg(hdev, "start background scanning");
2912 err = hci_passive_scan_sync(hdev);
2914 bt_dev_err(hdev, "start background scanning failed: %d",
2921 static int update_scan_sync(struct hci_dev *hdev, void *data)
2923 return hci_update_scan_sync(hdev);
2926 int hci_update_scan(struct hci_dev *hdev)
2928 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
2931 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2933 return hci_update_passive_scan_sync(hdev);
2936 int hci_update_passive_scan(struct hci_dev *hdev)
2938 /* Only queue if it would have any effect */
2939 if (!test_bit(HCI_UP, &hdev->flags) ||
2940 test_bit(HCI_INIT, &hdev->flags) ||
2941 hci_dev_test_flag(hdev, HCI_SETUP) ||
2942 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2943 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2944 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2947 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2950 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2954 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2957 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2958 sizeof(val), &val, HCI_CMD_TIMEOUT);
2962 hdev->features[1][0] |= LMP_HOST_SC;
2963 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2965 hdev->features[1][0] &= ~LMP_HOST_SC;
2966 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2973 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2977 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2978 lmp_host_ssp_capable(hdev))
2981 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
2982 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2983 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2986 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2987 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2991 return hci_write_sc_support_sync(hdev, 0x01);
2994 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
2996 struct hci_cp_write_le_host_supported cp;
2998 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
2999 !lmp_bredr_capable(hdev))
3002 /* Check first if we already have the right host state
3003 * (host features set)
3005 if (le == lmp_host_le_capable(hdev) &&
3006 simul == lmp_host_le_br_capable(hdev))
3009 memset(&cp, 0, sizeof(cp));
3014 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3015 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3018 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3020 struct adv_info *adv, *tmp;
3023 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3026 /* If RPA Resolution has not been enable yet it means the
3027 * resolving list is empty and we should attempt to program the
3028 * local IRK in order to support using own_addr_type
3029 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
3031 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3032 hci_le_add_resolve_list_sync(hdev, NULL);
3033 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3036 /* Make sure the controller has a good default for
3037 * advertising data. This also applies to the case
3038 * where BR/EDR was toggled during the AUTO_OFF phase.
3040 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3041 list_empty(&hdev->adv_instances)) {
3042 if (ext_adv_capable(hdev)) {
3043 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3045 hci_update_scan_rsp_data_sync(hdev, 0x00);
3047 err = hci_update_adv_data_sync(hdev, 0x00);
3049 hci_update_scan_rsp_data_sync(hdev, 0x00);
3052 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3053 hci_enable_advertising_sync(hdev);
3056 /* Call for each tracked instance to be scheduled */
3057 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3058 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3063 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3067 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3068 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3071 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3072 sizeof(link_sec), &link_sec,
3076 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3078 struct hci_cp_write_page_scan_activity cp;
3082 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3085 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3088 memset(&cp, 0, sizeof(cp));
3091 type = PAGE_SCAN_TYPE_INTERLACED;
3093 /* 160 msec page scan interval */
3094 cp.interval = cpu_to_le16(0x0100);
3096 type = hdev->def_page_scan_type;
3097 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3100 cp.window = cpu_to_le16(hdev->def_page_scan_window);
3102 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3103 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3104 err = __hci_cmd_sync_status(hdev,
3105 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
3106 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3111 if (hdev->page_scan_type != type)
3112 err = __hci_cmd_sync_status(hdev,
3113 HCI_OP_WRITE_PAGE_SCAN_TYPE,
3114 sizeof(type), &type,
3120 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3122 struct bdaddr_list *b;
3124 list_for_each_entry(b, &hdev->accept_list, list) {
3125 struct hci_conn *conn;
3127 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3131 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3138 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3140 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3145 int hci_update_scan_sync(struct hci_dev *hdev)
3149 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3152 if (!hdev_is_powered(hdev))
3155 if (mgmt_powering_down(hdev))
3158 if (hdev->scanning_paused)
3161 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3162 disconnected_accept_list_entries(hdev))
3165 scan = SCAN_DISABLED;
3167 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3168 scan |= SCAN_INQUIRY;
3170 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3171 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3174 return hci_write_scan_enable_sync(hdev, scan);
3177 int hci_update_name_sync(struct hci_dev *hdev)
3179 struct hci_cp_write_local_name cp;
3181 memset(&cp, 0, sizeof(cp));
3183 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3185 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3190 /* This function perform powered update HCI command sequence after the HCI init
3191 * sequence which end up resetting all states, the sequence is as follows:
3193 * HCI_SSP_ENABLED(Enable SSP)
3194 * HCI_LE_ENABLED(Enable LE)
3195 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
3197 * Enable Authentication
3198 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3199 * Set Name -> Set EIR)
3200 * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address)
3202 int hci_powered_update_sync(struct hci_dev *hdev)
3206 /* Register the available SMP channels (BR/EDR and LE) only when
3207 * successfully powering on the controller. This late
3208 * registration is required so that LE SMP can clearly decide if
3209 * the public address or static address is used.
3213 err = hci_write_ssp_mode_sync(hdev, 0x01);
3217 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3221 err = hci_powered_update_adv_sync(hdev);
3225 err = hci_write_auth_enable_sync(hdev);
3229 if (lmp_bredr_capable(hdev)) {
3230 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3231 hci_write_fast_connectable_sync(hdev, true);
3233 hci_write_fast_connectable_sync(hdev, false);
3234 hci_update_scan_sync(hdev);
3235 hci_update_class_sync(hdev);
3236 hci_update_name_sync(hdev);
3237 hci_update_eir_sync(hdev);
3240 /* If forcing static address is in use or there is no public
3241 * address use the static address as random address (but skip
3242 * the HCI command if the current random address is already the
3245 * In case BR/EDR has been disabled on a dual-mode controller
3246 * and a static address has been configured, then use that
3247 * address instead of the public BR/EDR address.
3249 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3250 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3251 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3252 if (bacmp(&hdev->static_addr, BDADDR_ANY))
3253 return hci_set_random_addr_sync(hdev,
3254 &hdev->static_addr);
3261 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3262 * (BD_ADDR) for a HCI device from
3263 * a firmware node property.
3264 * @hdev: The HCI device
3266 * Search the firmware node for 'local-bd-address'.
3268 * All-zero BD addresses are rejected, because those could be properties
3269 * that exist in the firmware tables, but were not updated by the firmware. For
3270 * example, the DTS could define 'local-bd-address', with zero BD addresses.
3272 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3274 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3278 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3279 (u8 *)&ba, sizeof(ba));
3280 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3283 bacpy(&hdev->public_addr, &ba);
3286 struct hci_init_stage {
3287 int (*func)(struct hci_dev *hdev);
3290 /* Run init stage NULL terminated function table */
3291 static int hci_init_stage_sync(struct hci_dev *hdev,
3292 const struct hci_init_stage *stage)
3296 for (i = 0; stage[i].func; i++) {
3299 err = stage[i].func(hdev);
3307 /* Read Local Version */
3308 static int hci_read_local_version_sync(struct hci_dev *hdev)
3310 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3311 0, NULL, HCI_CMD_TIMEOUT);
3314 /* Read BD Address */
3315 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3317 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3318 0, NULL, HCI_CMD_TIMEOUT);
3321 #define HCI_INIT(_func) \
3326 static const struct hci_init_stage hci_init0[] = {
3327 /* HCI_OP_READ_LOCAL_VERSION */
3328 HCI_INIT(hci_read_local_version_sync),
3329 /* HCI_OP_READ_BD_ADDR */
3330 HCI_INIT(hci_read_bd_addr_sync),
3334 int hci_reset_sync(struct hci_dev *hdev)
3338 set_bit(HCI_RESET, &hdev->flags);
3340 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3348 static int hci_init0_sync(struct hci_dev *hdev)
3352 bt_dev_dbg(hdev, "");
3355 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3356 err = hci_reset_sync(hdev);
3361 return hci_init_stage_sync(hdev, hci_init0);
3364 static int hci_unconf_init_sync(struct hci_dev *hdev)
3368 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3371 err = hci_init0_sync(hdev);
3375 if (hci_dev_test_flag(hdev, HCI_SETUP))
3376 hci_debugfs_create_basic(hdev);
3381 /* Read Local Supported Features. */
3382 static int hci_read_local_features_sync(struct hci_dev *hdev)
3384 /* Not all AMP controllers support this command */
3385 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
3388 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3389 0, NULL, HCI_CMD_TIMEOUT);
3392 /* BR Controller init stage 1 command sequence */
3393 static const struct hci_init_stage br_init1[] = {
3394 /* HCI_OP_READ_LOCAL_FEATURES */
3395 HCI_INIT(hci_read_local_features_sync),
3396 /* HCI_OP_READ_LOCAL_VERSION */
3397 HCI_INIT(hci_read_local_version_sync),
3398 /* HCI_OP_READ_BD_ADDR */
3399 HCI_INIT(hci_read_bd_addr_sync),
3403 /* Read Local Commands */
3404 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3406 /* All Bluetooth 1.2 and later controllers should support the
3407 * HCI command for reading the local supported commands.
3409 * Unfortunately some controllers indicate Bluetooth 1.2 support,
3410 * but do not have support for this command. If that is the case,
3411 * the driver can quirk the behavior and skip reading the local
3412 * supported commands.
3414 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3415 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3416 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3417 0, NULL, HCI_CMD_TIMEOUT);
3422 /* Read Local AMP Info */
3423 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
3425 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
3426 0, NULL, HCI_CMD_TIMEOUT);
3429 /* Read Data Blk size */
3430 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
3432 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
3433 0, NULL, HCI_CMD_TIMEOUT);
3436 /* Read Flow Control Mode */
3437 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
3439 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
3440 0, NULL, HCI_CMD_TIMEOUT);
3443 /* Read Location Data */
3444 static int hci_read_location_data_sync(struct hci_dev *hdev)
3446 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
3447 0, NULL, HCI_CMD_TIMEOUT);
3450 /* AMP Controller init stage 1 command sequence */
3451 static const struct hci_init_stage amp_init1[] = {
3452 /* HCI_OP_READ_LOCAL_VERSION */
3453 HCI_INIT(hci_read_local_version_sync),
3454 /* HCI_OP_READ_LOCAL_COMMANDS */
3455 HCI_INIT(hci_read_local_cmds_sync),
3456 /* HCI_OP_READ_LOCAL_AMP_INFO */
3457 HCI_INIT(hci_read_local_amp_info_sync),
3458 /* HCI_OP_READ_DATA_BLOCK_SIZE */
3459 HCI_INIT(hci_read_data_block_size_sync),
3460 /* HCI_OP_READ_FLOW_CONTROL_MODE */
3461 HCI_INIT(hci_read_flow_control_mode_sync),
3462 /* HCI_OP_READ_LOCATION_DATA */
3463 HCI_INIT(hci_read_location_data_sync),
3467 static int hci_init1_sync(struct hci_dev *hdev)
3471 bt_dev_dbg(hdev, "");
3474 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3475 err = hci_reset_sync(hdev);
3480 switch (hdev->dev_type) {
3482 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3483 return hci_init_stage_sync(hdev, br_init1);
3485 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3486 return hci_init_stage_sync(hdev, amp_init1);
3488 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
3495 /* AMP Controller init stage 2 command sequence */
3496 static const struct hci_init_stage amp_init2[] = {
3497 /* HCI_OP_READ_LOCAL_FEATURES */
3498 HCI_INIT(hci_read_local_features_sync),
3502 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
3503 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3505 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3506 0, NULL, HCI_CMD_TIMEOUT);
3509 /* Read Class of Device */
3510 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3512 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3513 0, NULL, HCI_CMD_TIMEOUT);
3516 /* Read Local Name */
3517 static int hci_read_local_name_sync(struct hci_dev *hdev)
3519 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3520 0, NULL, HCI_CMD_TIMEOUT);
3523 /* Read Voice Setting */
3524 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3526 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3527 0, NULL, HCI_CMD_TIMEOUT);
3530 /* Read Number of Supported IAC */
3531 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3533 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3534 0, NULL, HCI_CMD_TIMEOUT);
3537 /* Read Current IAC LAP */
3538 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3540 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3541 0, NULL, HCI_CMD_TIMEOUT);
3544 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3545 u8 cond_type, bdaddr_t *bdaddr,
3548 struct hci_cp_set_event_filter cp;
3550 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3553 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3556 memset(&cp, 0, sizeof(cp));
3557 cp.flt_type = flt_type;
3559 if (flt_type != HCI_FLT_CLEAR_ALL) {
3560 cp.cond_type = cond_type;
3561 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3562 cp.addr_conn_flt.auto_accept = auto_accept;
3565 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3566 flt_type == HCI_FLT_CLEAR_ALL ?
3567 sizeof(cp.flt_type) : sizeof(cp), &cp,
3571 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3573 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3576 /* In theory the state machine should not reach here unless
3577 * a hci_set_event_filter_sync() call succeeds, but we do
3578 * the check both for parity and as a future reminder.
3580 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3583 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3587 /* Connection accept timeout ~20 secs */
3588 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3590 __le16 param = cpu_to_le16(0x7d00);
3592 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3593 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
3596 /* BR Controller init stage 2 command sequence */
3597 static const struct hci_init_stage br_init2[] = {
3598 /* HCI_OP_READ_BUFFER_SIZE */
3599 HCI_INIT(hci_read_buffer_size_sync),
3600 /* HCI_OP_READ_CLASS_OF_DEV */
3601 HCI_INIT(hci_read_dev_class_sync),
3602 /* HCI_OP_READ_LOCAL_NAME */
3603 HCI_INIT(hci_read_local_name_sync),
3604 /* HCI_OP_READ_VOICE_SETTING */
3605 HCI_INIT(hci_read_voice_setting_sync),
3606 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
3607 HCI_INIT(hci_read_num_supported_iac_sync),
3608 /* HCI_OP_READ_CURRENT_IAC_LAP */
3609 HCI_INIT(hci_read_current_iac_lap_sync),
3610 /* HCI_OP_SET_EVENT_FLT */
3611 HCI_INIT(hci_clear_event_filter_sync),
3612 /* HCI_OP_WRITE_CA_TIMEOUT */
3613 HCI_INIT(hci_write_ca_timeout_sync),
3617 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3621 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3624 /* When SSP is available, then the host features page
3625 * should also be available as well. However some
3626 * controllers list the max_page as 0 as long as SSP
3627 * has not been enabled. To achieve proper debugging
3628 * output, force the minimum max_page to 1 at least.
3630 hdev->max_page = 0x01;
3632 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3633 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3636 static int hci_write_eir_sync(struct hci_dev *hdev)
3638 struct hci_cp_write_eir cp;
3640 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3643 memset(hdev->eir, 0, sizeof(hdev->eir));
3644 memset(&cp, 0, sizeof(cp));
3646 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3650 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3654 if (!lmp_inq_rssi_capable(hdev) &&
3655 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3658 /* If Extended Inquiry Result events are supported, then
3659 * they are clearly preferred over Inquiry Result with RSSI
3662 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3664 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3665 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3668 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3670 if (!lmp_inq_tx_pwr_capable(hdev))
3673 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3674 0, NULL, HCI_CMD_TIMEOUT);
3677 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3679 struct hci_cp_read_local_ext_features cp;
3681 if (!lmp_ext_feat_capable(hdev))
3684 memset(&cp, 0, sizeof(cp));
3687 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3688 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3691 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3693 return hci_read_local_ext_features_sync(hdev, 0x01);
3696 /* HCI Controller init stage 2 command sequence */
3697 static const struct hci_init_stage hci_init2[] = {
3698 /* HCI_OP_READ_LOCAL_COMMANDS */
3699 HCI_INIT(hci_read_local_cmds_sync),
3700 /* HCI_OP_WRITE_SSP_MODE */
3701 HCI_INIT(hci_write_ssp_mode_1_sync),
3702 /* HCI_OP_WRITE_EIR */
3703 HCI_INIT(hci_write_eir_sync),
3704 /* HCI_OP_WRITE_INQUIRY_MODE */
3705 HCI_INIT(hci_write_inquiry_mode_sync),
3706 /* HCI_OP_READ_INQ_RSP_TX_POWER */
3707 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3708 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3709 HCI_INIT(hci_read_local_ext_features_1_sync),
3710 /* HCI_OP_WRITE_AUTH_ENABLE */
3711 HCI_INIT(hci_write_auth_enable_sync),
3715 /* Read LE Buffer Size */
3716 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3718 /* Use Read LE Buffer Size V2 if supported */
3719 if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3720 return __hci_cmd_sync_status(hdev,
3721 HCI_OP_LE_READ_BUFFER_SIZE_V2,
3722 0, NULL, HCI_CMD_TIMEOUT);
3724 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3725 0, NULL, HCI_CMD_TIMEOUT);
3728 /* Read LE Local Supported Features */
3729 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3731 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3732 0, NULL, HCI_CMD_TIMEOUT);
3735 /* Read LE Supported States */
3736 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3738 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3739 0, NULL, HCI_CMD_TIMEOUT);
3742 /* LE Controller init stage 2 command sequence */
3743 static const struct hci_init_stage le_init2[] = {
3744 /* HCI_OP_LE_READ_LOCAL_FEATURES */
3745 HCI_INIT(hci_le_read_local_features_sync),
3746 /* HCI_OP_LE_READ_BUFFER_SIZE */
3747 HCI_INIT(hci_le_read_buffer_size_sync),
3748 /* HCI_OP_LE_READ_SUPPORTED_STATES */
3749 HCI_INIT(hci_le_read_supported_states_sync),
3753 static int hci_init2_sync(struct hci_dev *hdev)
3757 bt_dev_dbg(hdev, "");
3759 if (hdev->dev_type == HCI_AMP)
3760 return hci_init_stage_sync(hdev, amp_init2);
3762 err = hci_init_stage_sync(hdev, hci_init2);
3766 if (lmp_bredr_capable(hdev)) {
3767 err = hci_init_stage_sync(hdev, br_init2);
3771 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3774 if (lmp_le_capable(hdev)) {
3775 err = hci_init_stage_sync(hdev, le_init2);
3778 /* LE-only controllers have LE implicitly enabled */
3779 if (!lmp_bredr_capable(hdev))
3780 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3786 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3788 /* The second byte is 0xff instead of 0x9f (two reserved bits
3789 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3790 * command otherwise.
3792 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3794 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3795 * any event mask for pre 1.2 devices.
3797 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3800 if (lmp_bredr_capable(hdev)) {
3801 events[4] |= 0x01; /* Flow Specification Complete */
3803 /* Don't set Disconnect Complete when suspended as that
3804 * would wakeup the host when disconnecting due to
3807 if (hdev->suspended)
3810 /* Use a different default for LE-only devices */
3811 memset(events, 0, sizeof(events));
3812 events[1] |= 0x20; /* Command Complete */
3813 events[1] |= 0x40; /* Command Status */
3814 events[1] |= 0x80; /* Hardware Error */
3816 /* If the controller supports the Disconnect command, enable
3817 * the corresponding event. In addition enable packet flow
3818 * control related events.
3820 if (hdev->commands[0] & 0x20) {
3821 /* Don't set Disconnect Complete when suspended as that
3822 * would wakeup the host when disconnecting due to
3825 if (!hdev->suspended)
3826 events[0] |= 0x10; /* Disconnection Complete */
3827 events[2] |= 0x04; /* Number of Completed Packets */
3828 events[3] |= 0x02; /* Data Buffer Overflow */
3831 /* If the controller supports the Read Remote Version
3832 * Information command, enable the corresponding event.
3834 if (hdev->commands[2] & 0x80)
3835 events[1] |= 0x08; /* Read Remote Version Information
3839 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3840 events[0] |= 0x80; /* Encryption Change */
3841 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3845 if (lmp_inq_rssi_capable(hdev) ||
3846 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3847 events[4] |= 0x02; /* Inquiry Result with RSSI */
3849 if (lmp_ext_feat_capable(hdev))
3850 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3852 if (lmp_esco_capable(hdev)) {
3853 events[5] |= 0x08; /* Synchronous Connection Complete */
3854 events[5] |= 0x10; /* Synchronous Connection Changed */
3857 if (lmp_sniffsubr_capable(hdev))
3858 events[5] |= 0x20; /* Sniff Subrating */
3860 if (lmp_pause_enc_capable(hdev))
3861 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3863 if (lmp_ext_inq_capable(hdev))
3864 events[5] |= 0x40; /* Extended Inquiry Result */
3866 if (lmp_no_flush_capable(hdev))
3867 events[7] |= 0x01; /* Enhanced Flush Complete */
3869 if (lmp_lsto_capable(hdev))
3870 events[6] |= 0x80; /* Link Supervision Timeout Changed */
3872 if (lmp_ssp_capable(hdev)) {
3873 events[6] |= 0x01; /* IO Capability Request */
3874 events[6] |= 0x02; /* IO Capability Response */
3875 events[6] |= 0x04; /* User Confirmation Request */
3876 events[6] |= 0x08; /* User Passkey Request */
3877 events[6] |= 0x10; /* Remote OOB Data Request */
3878 events[6] |= 0x20; /* Simple Pairing Complete */
3879 events[7] |= 0x04; /* User Passkey Notification */
3880 events[7] |= 0x08; /* Keypress Notification */
3881 events[7] |= 0x10; /* Remote Host Supported
3882 * Features Notification
3886 if (lmp_le_capable(hdev))
3887 events[7] |= 0x20; /* LE Meta-Event */
3889 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3890 sizeof(events), events, HCI_CMD_TIMEOUT);
3893 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3895 struct hci_cp_read_stored_link_key cp;
3897 if (!(hdev->commands[6] & 0x20) ||
3898 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3901 memset(&cp, 0, sizeof(cp));
3902 bacpy(&cp.bdaddr, BDADDR_ANY);
3905 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3906 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3909 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3911 struct hci_cp_write_def_link_policy cp;
3912 u16 link_policy = 0;
3914 if (!(hdev->commands[5] & 0x10))
3917 memset(&cp, 0, sizeof(cp));
3919 if (lmp_rswitch_capable(hdev))
3920 link_policy |= HCI_LP_RSWITCH;
3921 if (lmp_hold_capable(hdev))
3922 link_policy |= HCI_LP_HOLD;
3923 if (lmp_sniff_capable(hdev))
3924 link_policy |= HCI_LP_SNIFF;
3925 if (lmp_park_capable(hdev))
3926 link_policy |= HCI_LP_PARK;
3928 cp.policy = cpu_to_le16(link_policy);
3930 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3931 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3934 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3936 if (!(hdev->commands[8] & 0x01))
3939 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3940 0, NULL, HCI_CMD_TIMEOUT);
3943 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3945 if (!(hdev->commands[18] & 0x04) ||
3946 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
3947 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3950 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3951 0, NULL, HCI_CMD_TIMEOUT);
3954 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3956 /* Some older Broadcom based Bluetooth 1.2 controllers do not
3957 * support the Read Page Scan Type command. Check support for
3958 * this command in the bit mask of supported commands.
3960 if (!(hdev->commands[13] & 0x01))
3963 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3964 0, NULL, HCI_CMD_TIMEOUT);
3967 /* Read features beyond page 1 if available */
3968 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3973 if (!lmp_ext_feat_capable(hdev))
3976 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3978 err = hci_read_local_ext_features_sync(hdev, page);
3986 /* HCI Controller init stage 3 command sequence */
3987 static const struct hci_init_stage hci_init3[] = {
3988 /* HCI_OP_SET_EVENT_MASK */
3989 HCI_INIT(hci_set_event_mask_sync),
3990 /* HCI_OP_READ_STORED_LINK_KEY */
3991 HCI_INIT(hci_read_stored_link_key_sync),
3992 /* HCI_OP_WRITE_DEF_LINK_POLICY */
3993 HCI_INIT(hci_setup_link_policy_sync),
3994 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
3995 HCI_INIT(hci_read_page_scan_activity_sync),
3996 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
3997 HCI_INIT(hci_read_def_err_data_reporting_sync),
3998 /* HCI_OP_READ_PAGE_SCAN_TYPE */
3999 HCI_INIT(hci_read_page_scan_type_sync),
4000 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
4001 HCI_INIT(hci_read_local_ext_features_all_sync),
4005 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4009 if (!lmp_le_capable(hdev))
4012 memset(events, 0, sizeof(events));
4014 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4015 events[0] |= 0x10; /* LE Long Term Key Request */
4017 /* If controller supports the Connection Parameters Request
4018 * Link Layer Procedure, enable the corresponding event.
4020 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4021 /* LE Remote Connection Parameter Request */
4024 /* If the controller supports the Data Length Extension
4025 * feature, enable the corresponding event.
4027 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4028 events[0] |= 0x40; /* LE Data Length Change */
4030 /* If the controller supports LL Privacy feature or LE Extended Adv,
4031 * enable the corresponding event.
4033 if (use_enhanced_conn_complete(hdev))
4034 events[1] |= 0x02; /* LE Enhanced Connection Complete */
4036 /* If the controller supports Extended Scanner Filter
4037 * Policies, enable the corresponding event.
4039 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4040 events[1] |= 0x04; /* LE Direct Advertising Report */
4042 /* If the controller supports Channel Selection Algorithm #2
4043 * feature, enable the corresponding event.
4045 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4046 events[2] |= 0x08; /* LE Channel Selection Algorithm */
4048 /* If the controller supports the LE Set Scan Enable command,
4049 * enable the corresponding advertising report event.
4051 if (hdev->commands[26] & 0x08)
4052 events[0] |= 0x02; /* LE Advertising Report */
4054 /* If the controller supports the LE Create Connection
4055 * command, enable the corresponding event.
4057 if (hdev->commands[26] & 0x10)
4058 events[0] |= 0x01; /* LE Connection Complete */
4060 /* If the controller supports the LE Connection Update
4061 * command, enable the corresponding event.
4063 if (hdev->commands[27] & 0x04)
4064 events[0] |= 0x04; /* LE Connection Update Complete */
4066 /* If the controller supports the LE Read Remote Used Features
4067 * command, enable the corresponding event.
4069 if (hdev->commands[27] & 0x20)
4070 /* LE Read Remote Used Features Complete */
4073 /* If the controller supports the LE Read Local P-256
4074 * Public Key command, enable the corresponding event.
4076 if (hdev->commands[34] & 0x02)
4077 /* LE Read Local P-256 Public Key Complete */
4080 /* If the controller supports the LE Generate DHKey
4081 * command, enable the corresponding event.
4083 if (hdev->commands[34] & 0x04)
4084 events[1] |= 0x01; /* LE Generate DHKey Complete */
4086 /* If the controller supports the LE Set Default PHY or
4087 * LE Set PHY commands, enable the corresponding event.
4089 if (hdev->commands[35] & (0x20 | 0x40))
4090 events[1] |= 0x08; /* LE PHY Update Complete */
4092 /* If the controller supports LE Set Extended Scan Parameters
4093 * and LE Set Extended Scan Enable commands, enable the
4094 * corresponding event.
4096 if (use_ext_scan(hdev))
4097 events[1] |= 0x10; /* LE Extended Advertising Report */
4099 /* If the controller supports the LE Extended Advertising
4100 * command, enable the corresponding event.
4102 if (ext_adv_capable(hdev))
4103 events[2] |= 0x02; /* LE Advertising Set Terminated */
4105 if (cis_capable(hdev)) {
4106 events[3] |= 0x01; /* LE CIS Established */
4107 if (cis_peripheral_capable(hdev))
4108 events[3] |= 0x02; /* LE CIS Request */
4111 if (bis_capable(hdev)) {
4112 events[1] |= 0x20; /* LE PA Report */
4113 events[1] |= 0x40; /* LE PA Sync Established */
4114 events[3] |= 0x04; /* LE Create BIG Complete */
4115 events[3] |= 0x08; /* LE Terminate BIG Complete */
4116 events[3] |= 0x10; /* LE BIG Sync Established */
4117 events[3] |= 0x20; /* LE BIG Sync Loss */
4118 events[4] |= 0x02; /* LE BIG Info Advertising Report */
4121 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4122 sizeof(events), events, HCI_CMD_TIMEOUT);
4125 /* Read LE Advertising Channel TX Power */
4126 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4128 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4129 /* HCI TS spec forbids mixing of legacy and extended
4130 * advertising commands wherein READ_ADV_TX_POWER is
4131 * also included. So do not call it if extended adv
4132 * is supported otherwise controller will return
4133 * COMMAND_DISALLOWED for extended commands.
4135 return __hci_cmd_sync_status(hdev,
4136 HCI_OP_LE_READ_ADV_TX_POWER,
4137 0, NULL, HCI_CMD_TIMEOUT);
4143 /* Read LE Min/Max Tx Power*/
4144 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4146 if (!(hdev->commands[38] & 0x80) ||
4147 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4150 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4151 0, NULL, HCI_CMD_TIMEOUT);
4154 /* Read LE Accept List Size */
4155 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4157 if (!(hdev->commands[26] & 0x40))
4160 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4161 0, NULL, HCI_CMD_TIMEOUT);
4164 /* Clear LE Accept List */
4165 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
4167 if (!(hdev->commands[26] & 0x80))
4170 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
4174 /* Read LE Resolving List Size */
4175 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4177 if (!(hdev->commands[34] & 0x40))
4180 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4181 0, NULL, HCI_CMD_TIMEOUT);
4184 /* Clear LE Resolving List */
4185 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4187 if (!(hdev->commands[34] & 0x20))
4190 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4194 /* Set RPA timeout */
4195 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4197 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4199 if (!(hdev->commands[35] & 0x04) ||
4200 test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4203 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4204 sizeof(timeout), &timeout,
4208 /* Read LE Maximum Data Length */
4209 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4211 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4214 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4218 /* Read LE Suggested Default Data Length */
4219 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4221 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4224 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4228 /* Read LE Number of Supported Advertising Sets */
4229 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4231 if (!ext_adv_capable(hdev))
4234 return __hci_cmd_sync_status(hdev,
4235 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4236 0, NULL, HCI_CMD_TIMEOUT);
4239 /* Write LE Host Supported */
4240 static int hci_set_le_support_sync(struct hci_dev *hdev)
4242 struct hci_cp_write_le_host_supported cp;
4244 /* LE-only devices do not support explicit enablement */
4245 if (!lmp_bredr_capable(hdev))
4248 memset(&cp, 0, sizeof(cp));
4250 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4255 if (cp.le == lmp_host_le_capable(hdev))
4258 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4259 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4262 /* LE Set Host Feature */
4263 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4265 struct hci_cp_le_set_host_feature cp;
4267 if (!iso_capable(hdev))
4270 memset(&cp, 0, sizeof(cp));
4272 /* Isochronous Channels (Host Support) */
4276 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4277 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4280 /* LE Controller init stage 3 command sequence */
4281 static const struct hci_init_stage le_init3[] = {
4282 /* HCI_OP_LE_SET_EVENT_MASK */
4283 HCI_INIT(hci_le_set_event_mask_sync),
4284 /* HCI_OP_LE_READ_ADV_TX_POWER */
4285 HCI_INIT(hci_le_read_adv_tx_power_sync),
4286 /* HCI_OP_LE_READ_TRANSMIT_POWER */
4287 HCI_INIT(hci_le_read_tx_power_sync),
4288 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4289 HCI_INIT(hci_le_read_accept_list_size_sync),
4290 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4291 HCI_INIT(hci_le_clear_accept_list_sync),
4292 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4293 HCI_INIT(hci_le_read_resolv_list_size_sync),
4294 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
4295 HCI_INIT(hci_le_clear_resolv_list_sync),
4296 /* HCI_OP_LE_SET_RPA_TIMEOUT */
4297 HCI_INIT(hci_le_set_rpa_timeout_sync),
4298 /* HCI_OP_LE_READ_MAX_DATA_LEN */
4299 HCI_INIT(hci_le_read_max_data_len_sync),
4300 /* HCI_OP_LE_READ_DEF_DATA_LEN */
4301 HCI_INIT(hci_le_read_def_data_len_sync),
4302 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4303 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4304 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4305 HCI_INIT(hci_set_le_support_sync),
4306 /* HCI_OP_LE_SET_HOST_FEATURE */
4307 HCI_INIT(hci_le_set_host_feature_sync),
4311 static int hci_init3_sync(struct hci_dev *hdev)
4315 bt_dev_dbg(hdev, "");
4317 err = hci_init_stage_sync(hdev, hci_init3);
4321 if (lmp_le_capable(hdev))
4322 return hci_init_stage_sync(hdev, le_init3);
4327 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4329 struct hci_cp_delete_stored_link_key cp;
4331 /* Some Broadcom based Bluetooth controllers do not support the
4332 * Delete Stored Link Key command. They are clearly indicating its
4333 * absence in the bit mask of supported commands.
4335 * Check the supported commands and only if the command is marked
4336 * as supported send it. If not supported assume that the controller
4337 * does not have actual support for stored link keys which makes this
4338 * command redundant anyway.
4340 * Some controllers indicate that they support handling deleting
4341 * stored link keys, but they don't. The quirk lets a driver
4342 * just disable this command.
4344 if (!(hdev->commands[6] & 0x80) ||
4345 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4348 memset(&cp, 0, sizeof(cp));
4349 bacpy(&cp.bdaddr, BDADDR_ANY);
4350 cp.delete_all = 0x01;
4352 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4353 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4356 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4358 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4359 bool changed = false;
4361 /* Set event mask page 2 if the HCI command for it is supported */
4362 if (!(hdev->commands[22] & 0x04))
4365 /* If Connectionless Peripheral Broadcast central role is supported
4366 * enable all necessary events for it.
4368 if (lmp_cpb_central_capable(hdev)) {
4369 events[1] |= 0x40; /* Triggered Clock Capture */
4370 events[1] |= 0x80; /* Synchronization Train Complete */
4371 events[2] |= 0x08; /* Truncated Page Complete */
4372 events[2] |= 0x20; /* CPB Channel Map Change */
4376 /* If Connectionless Peripheral Broadcast peripheral role is supported
4377 * enable all necessary events for it.
4379 if (lmp_cpb_peripheral_capable(hdev)) {
4380 events[2] |= 0x01; /* Synchronization Train Received */
4381 events[2] |= 0x02; /* CPB Receive */
4382 events[2] |= 0x04; /* CPB Timeout */
4383 events[2] |= 0x10; /* Peripheral Page Response Timeout */
4387 /* Enable Authenticated Payload Timeout Expired event if supported */
4388 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4393 /* Some Broadcom based controllers indicate support for Set Event
4394 * Mask Page 2 command, but then actually do not support it. Since
4395 * the default value is all bits set to zero, the command is only
4396 * required if the event mask has to be changed. In case no change
4397 * to the event mask is needed, skip this command.
4402 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4403 sizeof(events), events, HCI_CMD_TIMEOUT);
4406 /* Read local codec list if the HCI command is supported */
4407 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4409 if (hdev->commands[45] & 0x04)
4410 hci_read_supported_codecs_v2(hdev);
4411 else if (hdev->commands[29] & 0x20)
4412 hci_read_supported_codecs(hdev);
4417 /* Read local pairing options if the HCI command is supported */
4418 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4420 if (!(hdev->commands[41] & 0x08))
4423 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4424 0, NULL, HCI_CMD_TIMEOUT);
4427 /* Get MWS transport configuration if the HCI command is supported */
4428 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4430 if (!mws_transport_config_capable(hdev))
4433 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4434 0, NULL, HCI_CMD_TIMEOUT);
4437 /* Check for Synchronization Train support */
4438 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4440 if (!lmp_sync_train_capable(hdev))
4443 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4444 0, NULL, HCI_CMD_TIMEOUT);
4447 /* Enable Secure Connections if supported and configured */
4448 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4452 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4453 !bredr_sc_enabled(hdev))
4456 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4457 sizeof(support), &support,
4461 /* Set erroneous data reporting if supported to the wideband speech
4464 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4466 struct hci_cp_write_def_err_data_reporting cp;
4467 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4469 if (!(hdev->commands[18] & 0x08) ||
4470 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4471 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4474 if (enabled == hdev->err_data_reporting)
4477 memset(&cp, 0, sizeof(cp));
4478 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4479 ERR_DATA_REPORTING_DISABLED;
4481 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4482 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4485 static const struct hci_init_stage hci_init4[] = {
4486 /* HCI_OP_DELETE_STORED_LINK_KEY */
4487 HCI_INIT(hci_delete_stored_link_key_sync),
4488 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4489 HCI_INIT(hci_set_event_mask_page_2_sync),
4490 /* HCI_OP_READ_LOCAL_CODECS */
4491 HCI_INIT(hci_read_local_codecs_sync),
4492 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4493 HCI_INIT(hci_read_local_pairing_opts_sync),
4494 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4495 HCI_INIT(hci_get_mws_transport_config_sync),
4496 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4497 HCI_INIT(hci_read_sync_train_params_sync),
4498 /* HCI_OP_WRITE_SC_SUPPORT */
4499 HCI_INIT(hci_write_sc_support_1_sync),
4500 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4501 HCI_INIT(hci_set_err_data_report_sync),
4505 /* Set Suggested Default Data Length to maximum if supported */
4506 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4508 struct hci_cp_le_write_def_data_len cp;
4510 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4513 memset(&cp, 0, sizeof(cp));
4514 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4515 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4517 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4518 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4521 /* Set Default PHY parameters if command is supported, enables all supported
4522 * PHYs according to the LE Features bits.
4524 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4526 struct hci_cp_le_set_default_phy cp;
4528 if (!(hdev->commands[35] & 0x20)) {
4529 /* If the command is not supported it means only 1M PHY is
4532 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4533 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4537 memset(&cp, 0, sizeof(cp));
4539 cp.tx_phys = HCI_LE_SET_PHY_1M;
4540 cp.rx_phys = HCI_LE_SET_PHY_1M;
4542 /* Enables 2M PHY if supported */
4543 if (le_2m_capable(hdev)) {
4544 cp.tx_phys |= HCI_LE_SET_PHY_2M;
4545 cp.rx_phys |= HCI_LE_SET_PHY_2M;
4548 /* Enables Coded PHY if supported */
4549 if (le_coded_capable(hdev)) {
4550 cp.tx_phys |= HCI_LE_SET_PHY_CODED;
4551 cp.rx_phys |= HCI_LE_SET_PHY_CODED;
4554 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4555 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4558 static const struct hci_init_stage le_init4[] = {
4559 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4560 HCI_INIT(hci_le_set_write_def_data_len_sync),
4561 /* HCI_OP_LE_SET_DEFAULT_PHY */
4562 HCI_INIT(hci_le_set_default_phy_sync),
4566 static int hci_init4_sync(struct hci_dev *hdev)
4570 bt_dev_dbg(hdev, "");
4572 err = hci_init_stage_sync(hdev, hci_init4);
4576 if (lmp_le_capable(hdev))
4577 return hci_init_stage_sync(hdev, le_init4);
4582 static int hci_init_sync(struct hci_dev *hdev)
4586 err = hci_init1_sync(hdev);
4590 if (hci_dev_test_flag(hdev, HCI_SETUP))
4591 hci_debugfs_create_basic(hdev);
4593 err = hci_init2_sync(hdev);
4597 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
4598 * BR/EDR/LE type controllers. AMP controllers only need the
4599 * first two stages of init.
4601 if (hdev->dev_type != HCI_PRIMARY)
4604 err = hci_init3_sync(hdev);
4608 err = hci_init4_sync(hdev);
4612 /* This function is only called when the controller is actually in
4613 * configured state. When the controller is marked as unconfigured,
4614 * this initialization procedure is not run.
4616 * It means that it is possible that a controller runs through its
4617 * setup phase and then discovers missing settings. If that is the
4618 * case, then this function will not be called. It then will only
4619 * be called during the config phase.
4621 * So only when in setup phase or config phase, create the debugfs
4622 * entries and register the SMP channels.
4624 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4625 !hci_dev_test_flag(hdev, HCI_CONFIG))
4628 if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4631 hci_debugfs_create_common(hdev);
4633 if (lmp_bredr_capable(hdev))
4634 hci_debugfs_create_bredr(hdev);
4636 if (lmp_le_capable(hdev))
4637 hci_debugfs_create_le(hdev);
4642 #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
4644 static const struct {
4645 unsigned long quirk;
4647 } hci_broken_table[] = {
4648 HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
4649 "HCI Read Local Supported Commands not supported"),
4650 HCI_QUIRK_BROKEN(STORED_LINK_KEY,
4651 "HCI Delete Stored Link Key command is advertised, "
4652 "but not supported."),
4653 HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
4654 "HCI Read Default Erroneous Data Reporting command is "
4655 "advertised, but not supported."),
4656 HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
4657 "HCI Read Transmit Power Level command is advertised, "
4658 "but not supported."),
4659 HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
4660 "HCI Set Event Filter command not supported."),
4661 HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
4662 "HCI Enhanced Setup Synchronous Connection command is "
4663 "advertised, but not supported."),
4664 HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
4665 "HCI LE Set Random Private Address Timeout command is "
4666 "advertised, but not supported."),
4667 HCI_QUIRK_BROKEN(LE_CODED,
4668 "HCI LE Coded PHY feature bit is set, "
4669 "but its usage is not supported.")
4672 /* This function handles hdev setup stage:
4675 * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
4677 static int hci_dev_setup_sync(struct hci_dev *hdev)
4680 bool invalid_bdaddr;
4683 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4684 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4687 bt_dev_dbg(hdev, "");
4689 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4692 ret = hdev->setup(hdev);
4694 for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
4695 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4696 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4699 /* The transport driver can set the quirk to mark the
4700 * BD_ADDR invalid before creating the HCI device or in
4701 * its setup callback.
4703 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
4704 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
4706 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4707 !bacmp(&hdev->public_addr, BDADDR_ANY))
4708 hci_dev_get_bd_addr_from_property(hdev);
4710 if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
4712 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4714 invalid_bdaddr = false;
4718 /* The transport driver can set these quirks before
4719 * creating the HCI device or in its setup callback.
4721 * For the invalid BD_ADDR quirk it is possible that
4722 * it becomes a valid address if the bootloader does
4723 * provide it (see above).
4725 * In case any of them is set, the controller has to
4726 * start up as unconfigured.
4728 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4730 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4732 /* For an unconfigured controller it is required to
4733 * read at least the version information provided by
4734 * the Read Local Version Information command.
4736 * If the set_bdaddr driver callback is provided, then
4737 * also the original Bluetooth public device address
4738 * will be read using the Read BD Address command.
4740 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4741 return hci_unconf_init_sync(hdev);
4746 /* This function handles hdev init stage:
4748 * Calls hci_dev_setup_sync to perform setup stage
4749 * Calls hci_init_sync to perform HCI command init sequence
4751 static int hci_dev_init_sync(struct hci_dev *hdev)
4755 bt_dev_dbg(hdev, "");
4757 atomic_set(&hdev->cmd_cnt, 1);
4758 set_bit(HCI_INIT, &hdev->flags);
4760 ret = hci_dev_setup_sync(hdev);
4762 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4763 /* If public address change is configured, ensure that
4764 * the address gets programmed. If the driver does not
4765 * support changing the public address, fail the power
4768 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4770 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4772 ret = -EADDRNOTAVAIL;
4776 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4777 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4778 ret = hci_init_sync(hdev);
4779 if (!ret && hdev->post_init)
4780 ret = hdev->post_init(hdev);
4784 /* If the HCI Reset command is clearing all diagnostic settings,
4785 * then they need to be reprogrammed after the init procedure
4788 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4789 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4790 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4791 ret = hdev->set_diag(hdev, true);
4793 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4798 clear_bit(HCI_INIT, &hdev->flags);
4803 int hci_dev_open_sync(struct hci_dev *hdev)
4807 bt_dev_dbg(hdev, "");
4809 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4814 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4815 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4816 /* Check for rfkill but allow the HCI setup stage to
4817 * proceed (which in itself doesn't cause any RF activity).
4819 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4824 /* Check for valid public address or a configured static
4825 * random address, but let the HCI setup proceed to
4826 * be able to determine if there is a public address
4829 * In case of user channel usage, it is not important
4830 * if a public address or static random address is
4833 * This check is only valid for BR/EDR controllers
4834 * since AMP controllers do not have an address.
4836 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4837 hdev->dev_type == HCI_PRIMARY &&
4838 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4839 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4840 ret = -EADDRNOTAVAIL;
4845 if (test_bit(HCI_UP, &hdev->flags)) {
4850 if (hdev->open(hdev)) {
4855 hci_devcd_reset(hdev);
4857 set_bit(HCI_RUNNING, &hdev->flags);
4858 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4860 ret = hci_dev_init_sync(hdev);
4863 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4864 hci_adv_instances_set_rpa_expired(hdev, true);
4865 set_bit(HCI_UP, &hdev->flags);
4866 hci_sock_dev_event(hdev, HCI_DEV_UP);
4867 hci_leds_update_powered(hdev, true);
4868 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4869 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4870 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4871 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4872 hci_dev_test_flag(hdev, HCI_MGMT) &&
4873 hdev->dev_type == HCI_PRIMARY) {
4874 ret = hci_powered_update_sync(hdev);
4875 mgmt_power_on(hdev, ret);
4878 /* Init failed, cleanup */
4879 flush_work(&hdev->tx_work);
4881 /* Since hci_rx_work() is possible to awake new cmd_work
4882 * it should be flushed first to avoid unexpected call of
4885 flush_work(&hdev->rx_work);
4886 flush_work(&hdev->cmd_work);
4888 skb_queue_purge(&hdev->cmd_q);
4889 skb_queue_purge(&hdev->rx_q);
4894 if (hdev->sent_cmd) {
4895 cancel_delayed_work_sync(&hdev->cmd_timer);
4896 kfree_skb(hdev->sent_cmd);
4897 hdev->sent_cmd = NULL;
4900 clear_bit(HCI_RUNNING, &hdev->flags);
4901 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4904 hdev->flags &= BIT(HCI_RAW);
4911 /* This function requires the caller holds hdev->lock */
4912 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4914 struct hci_conn_params *p;
4916 list_for_each_entry(p, &hdev->le_conn_params, list) {
4917 hci_pend_le_list_del_init(p);
4919 hci_conn_drop(p->conn);
4920 hci_conn_put(p->conn);
4925 BT_DBG("All LE pending actions cleared");
4928 static int hci_dev_shutdown(struct hci_dev *hdev)
4931 /* Similar to how we first do setup and then set the exclusive access
4932 * bit for userspace, we must first unset userchannel and then clean up.
4933 * Otherwise, the kernel can't properly use the hci channel to clean up
4934 * the controller (some shutdown routines require sending additional
4935 * commands to the controller for example).
4937 bool was_userchannel =
4938 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
4940 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4941 test_bit(HCI_UP, &hdev->flags)) {
4942 /* Execute vendor specific shutdown routine */
4944 err = hdev->shutdown(hdev);
4947 if (was_userchannel)
4948 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
4953 int hci_dev_close_sync(struct hci_dev *hdev)
4958 bt_dev_dbg(hdev, "");
4960 cancel_delayed_work(&hdev->power_off);
4961 cancel_delayed_work(&hdev->ncmd_timer);
4962 cancel_delayed_work(&hdev->le_scan_disable);
4963 cancel_delayed_work(&hdev->le_scan_restart);
4965 hci_request_cancel_all(hdev);
4967 if (hdev->adv_instance_timeout) {
4968 cancel_delayed_work_sync(&hdev->adv_instance_expire);
4969 hdev->adv_instance_timeout = 0;
4972 err = hci_dev_shutdown(hdev);
4974 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4975 cancel_delayed_work_sync(&hdev->cmd_timer);
4979 hci_leds_update_powered(hdev, false);
4981 /* Flush RX and TX works */
4982 flush_work(&hdev->tx_work);
4983 flush_work(&hdev->rx_work);
4985 if (hdev->discov_timeout > 0) {
4986 hdev->discov_timeout = 0;
4987 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4988 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
4991 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
4992 cancel_delayed_work(&hdev->service_cache);
4994 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4995 struct adv_info *adv_instance;
4997 cancel_delayed_work_sync(&hdev->rpa_expired);
4999 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5000 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
5003 /* Avoid potential lockdep warnings from the *_flush() calls by
5004 * ensuring the workqueue is empty up front.
5006 drain_workqueue(hdev->workqueue);
5010 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5012 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5014 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
5015 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5016 hci_dev_test_flag(hdev, HCI_MGMT))
5017 __mgmt_power_off(hdev);
5019 hci_inquiry_cache_flush(hdev);
5020 hci_pend_le_actions_clear(hdev);
5021 hci_conn_hash_flush(hdev);
5022 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5023 smp_unregister(hdev);
5024 hci_dev_unlock(hdev);
5026 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5028 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5029 aosp_do_close(hdev);
5030 msft_do_close(hdev);
5037 skb_queue_purge(&hdev->cmd_q);
5038 atomic_set(&hdev->cmd_cnt, 1);
5039 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5040 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5041 set_bit(HCI_INIT, &hdev->flags);
5042 hci_reset_sync(hdev);
5043 clear_bit(HCI_INIT, &hdev->flags);
5046 /* flush cmd work */
5047 flush_work(&hdev->cmd_work);
5050 skb_queue_purge(&hdev->rx_q);
5051 skb_queue_purge(&hdev->cmd_q);
5052 skb_queue_purge(&hdev->raw_q);
5054 /* Drop last sent command */
5055 if (hdev->sent_cmd) {
5056 cancel_delayed_work_sync(&hdev->cmd_timer);
5057 kfree_skb(hdev->sent_cmd);
5058 hdev->sent_cmd = NULL;
5061 clear_bit(HCI_RUNNING, &hdev->flags);
5062 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5064 /* After this point our queues are empty and no tasks are scheduled. */
5068 hdev->flags &= BIT(HCI_RAW);
5069 hci_dev_clear_volatile_flags(hdev);
5071 /* Controller radio is available but is currently powered down */
5072 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
5074 memset(hdev->eir, 0, sizeof(hdev->eir));
5075 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5076 bacpy(&hdev->random_addr, BDADDR_ANY);
5077 hci_codec_list_clear(&hdev->local_codecs);
5083 /* This function perform power on HCI command sequence as follows:
5085 * If controller is already up (HCI_UP) performs hci_powered_update_sync
5086 * sequence otherwise run hci_dev_open_sync which will follow with
5087 * hci_powered_update_sync after the init sequence is completed.
5089 static int hci_power_on_sync(struct hci_dev *hdev)
5093 if (test_bit(HCI_UP, &hdev->flags) &&
5094 hci_dev_test_flag(hdev, HCI_MGMT) &&
5095 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5096 cancel_delayed_work(&hdev->power_off);
5097 return hci_powered_update_sync(hdev);
5100 err = hci_dev_open_sync(hdev);
5104 /* During the HCI setup phase, a few error conditions are
5105 * ignored and they need to be checked now. If they are still
5106 * valid, it is important to return the device back off.
5108 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5109 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5110 (hdev->dev_type == HCI_PRIMARY &&
5111 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5112 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5113 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5114 hci_dev_close_sync(hdev);
5115 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5116 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5117 HCI_AUTO_OFF_TIMEOUT);
5120 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5121 /* For unconfigured devices, set the HCI_RAW flag
5122 * so that userspace can easily identify them.
5124 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5125 set_bit(HCI_RAW, &hdev->flags);
5127 /* For fully configured devices, this will send
5128 * the Index Added event. For unconfigured devices,
5129 * it will send Unconfigued Index Added event.
5131 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
5132 * and no event will be send.
5134 mgmt_index_added(hdev);
5135 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5136 /* When the controller is now configured, then it
5137 * is important to clear the HCI_RAW flag.
5139 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5140 clear_bit(HCI_RAW, &hdev->flags);
5142 /* Powering on the controller with HCI_CONFIG set only
5143 * happens with the transition from unconfigured to
5144 * configured. This will send the Index Added event.
5146 mgmt_index_added(hdev);
5152 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5154 struct hci_cp_remote_name_req_cancel cp;
5156 memset(&cp, 0, sizeof(cp));
5157 bacpy(&cp.bdaddr, addr);
5159 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5160 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5163 int hci_stop_discovery_sync(struct hci_dev *hdev)
5165 struct discovery_state *d = &hdev->discovery;
5166 struct inquiry_entry *e;
5169 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5171 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
5172 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5173 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5174 0, NULL, HCI_CMD_TIMEOUT);
5179 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5180 cancel_delayed_work(&hdev->le_scan_disable);
5181 cancel_delayed_work(&hdev->le_scan_restart);
5183 err = hci_scan_disable_sync(hdev);
5189 err = hci_scan_disable_sync(hdev);
5194 /* Resume advertising if it was paused */
5195 if (use_ll_privacy(hdev))
5196 hci_resume_advertising_sync(hdev);
5198 /* No further actions needed for LE-only discovery */
5199 if (d->type == DISCOV_TYPE_LE)
5202 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5203 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5208 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5214 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
5217 struct hci_cp_disconn_phy_link cp;
5219 memset(&cp, 0, sizeof(cp));
5220 cp.phy_handle = HCI_PHY_HANDLE(handle);
5223 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
5224 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5227 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5230 struct hci_cp_disconnect cp;
5232 if (conn->type == AMP_LINK)
5233 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
5235 memset(&cp, 0, sizeof(cp));
5236 cp.handle = cpu_to_le16(conn->handle);
5239 /* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5240 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5241 * used when suspending or powering off, where we don't want to wait
5242 * for the peer's response.
5244 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5245 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5247 HCI_EV_DISCONN_COMPLETE,
5248 HCI_CMD_TIMEOUT, NULL);
5250 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5254 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5255 struct hci_conn *conn, u8 reason)
5257 /* Return reason if scanning since the connection shall probably be
5260 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5263 if (conn->role == HCI_ROLE_SLAVE ||
5264 test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
5267 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5268 0, NULL, HCI_CMD_TIMEOUT);
5271 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5274 if (conn->type == LE_LINK)
5275 return hci_le_connect_cancel_sync(hdev, conn, reason);
5277 if (conn->type == ISO_LINK) {
5278 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
5281 * If this command is issued for a CIS on the Central and the
5282 * CIS is successfully terminated before being established,
5283 * then an HCI_LE_CIS_Established event shall also be sent for
5284 * this CIS with the Status Operation Cancelled by Host (0x44).
5286 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
5287 return hci_disconnect_sync(hdev, conn, reason);
5289 /* CIS with no Create CIS sent have nothing to cancel */
5290 if (bacmp(&conn->dst, BDADDR_ANY))
5291 return HCI_ERROR_LOCAL_HOST_TERM;
5293 /* There is no way to cancel a BIS without terminating the BIG
5294 * which is done later on connection cleanup.
5299 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5302 /* Wait for HCI_EV_CONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5303 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5304 * used when suspending or powering off, where we don't want to wait
5305 * for the peer's response.
5307 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5308 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5310 HCI_EV_CONN_COMPLETE,
5311 HCI_CMD_TIMEOUT, NULL);
5313 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5314 6, &conn->dst, HCI_CMD_TIMEOUT);
5317 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5320 struct hci_cp_reject_sync_conn_req cp;
5322 memset(&cp, 0, sizeof(cp));
5323 bacpy(&cp.bdaddr, &conn->dst);
5326 /* SCO rejection has its own limited set of
5327 * allowed error values (0x0D-0x0F).
5329 if (reason < 0x0d || reason > 0x0f)
5330 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5332 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5333 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5336 static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
5339 struct hci_cp_le_reject_cis cp;
5341 memset(&cp, 0, sizeof(cp));
5342 cp.handle = cpu_to_le16(conn->handle);
5345 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
5346 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5349 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5352 struct hci_cp_reject_conn_req cp;
5354 if (conn->type == ISO_LINK)
5355 return hci_le_reject_cis_sync(hdev, conn, reason);
5357 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5358 return hci_reject_sco_sync(hdev, conn, reason);
5360 memset(&cp, 0, sizeof(cp));
5361 bacpy(&cp.bdaddr, &conn->dst);
5364 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5365 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5368 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5371 u16 handle = conn->handle;
5372 bool disconnect = false;
5375 switch (conn->state) {
5378 err = hci_disconnect_sync(hdev, conn, reason);
5381 err = hci_connect_cancel_sync(hdev, conn, reason);
5384 err = hci_reject_conn_sync(hdev, conn, reason);
5389 /* Cleanup bis or pa sync connections */
5390 if (test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags) ||
5391 test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags)) {
5392 hci_conn_failed(conn, reason);
5393 } else if (test_bit(HCI_CONN_PA_SYNC, &conn->flags) ||
5394 test_bit(HCI_CONN_BIG_SYNC, &conn->flags)) {
5395 conn->state = BT_CLOSED;
5396 hci_disconn_cfm(conn, reason);
5400 hci_dev_unlock(hdev);
5411 /* Check if the connection has been cleaned up concurrently */
5412 c = hci_conn_hash_lookup_handle(hdev, handle);
5413 if (!c || c != conn) {
5418 /* Cleanup hci_conn object if it cannot be cancelled as it
5419 * likelly means the controller and host stack are out of sync
5420 * or in case of LE it was still scanning so it can be cleanup
5424 conn->state = BT_CLOSED;
5425 hci_disconn_cfm(conn, reason);
5428 hci_conn_failed(conn, reason);
5432 hci_dev_unlock(hdev);
5436 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5438 struct list_head *head = &hdev->conn_hash.list;
5439 struct hci_conn *conn;
5442 while ((conn = list_first_or_null_rcu(head, struct hci_conn, list))) {
5443 /* Make sure the connection is not freed while unlocking */
5444 conn = hci_conn_get(conn);
5446 /* Disregard possible errors since hci_conn_del shall have been
5447 * called even in case of errors had occurred since it would
5448 * then cause hci_conn_failed to be called which calls
5449 * hci_conn_del internally.
5451 hci_abort_conn_sync(hdev, conn, reason);
5460 /* This function perform power off HCI command sequence as follows:
5464 * Disconnect all connections
5465 * hci_dev_close_sync
5467 static int hci_power_off_sync(struct hci_dev *hdev)
5471 /* If controller is already down there is nothing to do */
5472 if (!test_bit(HCI_UP, &hdev->flags))
5475 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5476 test_bit(HCI_PSCAN, &hdev->flags)) {
5477 err = hci_write_scan_enable_sync(hdev, 0x00);
5482 err = hci_clear_adv_sync(hdev, NULL, false);
5486 err = hci_stop_discovery_sync(hdev);
5490 /* Terminated due to Power Off */
5491 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5495 return hci_dev_close_sync(hdev);
5498 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5501 return hci_power_on_sync(hdev);
5503 return hci_power_off_sync(hdev);
5506 static int hci_write_iac_sync(struct hci_dev *hdev)
5508 struct hci_cp_write_current_iac_lap cp;
5510 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5513 memset(&cp, 0, sizeof(cp));
5515 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5516 /* Limited discoverable mode */
5517 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5518 cp.iac_lap[0] = 0x00; /* LIAC */
5519 cp.iac_lap[1] = 0x8b;
5520 cp.iac_lap[2] = 0x9e;
5521 cp.iac_lap[3] = 0x33; /* GIAC */
5522 cp.iac_lap[4] = 0x8b;
5523 cp.iac_lap[5] = 0x9e;
5525 /* General discoverable mode */
5527 cp.iac_lap[0] = 0x33; /* GIAC */
5528 cp.iac_lap[1] = 0x8b;
5529 cp.iac_lap[2] = 0x9e;
5532 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5533 (cp.num_iac * 3) + 1, &cp,
5537 int hci_update_discoverable_sync(struct hci_dev *hdev)
5541 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5542 err = hci_write_iac_sync(hdev);
5546 err = hci_update_scan_sync(hdev);
5550 err = hci_update_class_sync(hdev);
5555 /* Advertising instances don't use the global discoverable setting, so
5556 * only update AD if advertising was enabled using Set Advertising.
5558 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5559 err = hci_update_adv_data_sync(hdev, 0x00);
5563 /* Discoverable mode affects the local advertising
5564 * address in limited privacy mode.
5566 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5567 if (ext_adv_capable(hdev))
5568 err = hci_start_ext_adv_sync(hdev, 0x00);
5570 err = hci_enable_advertising_sync(hdev);
5577 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5579 return hci_update_discoverable_sync(hdev);
5582 int hci_update_discoverable(struct hci_dev *hdev)
5584 /* Only queue if it would have any effect */
5585 if (hdev_is_powered(hdev) &&
5586 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5587 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5588 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5589 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5595 int hci_update_connectable_sync(struct hci_dev *hdev)
5599 err = hci_update_scan_sync(hdev);
5603 /* If BR/EDR is not enabled and we disable advertising as a
5604 * by-product of disabling connectable, we need to update the
5605 * advertising flags.
5607 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5608 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5610 /* Update the advertising parameters if necessary */
5611 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5612 !list_empty(&hdev->adv_instances)) {
5613 if (ext_adv_capable(hdev))
5614 err = hci_start_ext_adv_sync(hdev,
5615 hdev->cur_adv_instance);
5617 err = hci_enable_advertising_sync(hdev);
5623 return hci_update_passive_scan_sync(hdev);
5626 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5628 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5629 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5630 struct hci_cp_inquiry cp;
5632 bt_dev_dbg(hdev, "");
5634 if (hci_dev_test_flag(hdev, HCI_INQUIRY))
5638 hci_inquiry_cache_flush(hdev);
5639 hci_dev_unlock(hdev);
5641 memset(&cp, 0, sizeof(cp));
5643 if (hdev->discovery.limited)
5644 memcpy(&cp.lap, liac, sizeof(cp.lap));
5646 memcpy(&cp.lap, giac, sizeof(cp.lap));
5650 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5651 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5654 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5657 /* Accept list is not used for discovery */
5658 u8 filter_policy = 0x00;
5659 /* Default is to enable duplicates filter */
5660 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5663 bt_dev_dbg(hdev, "");
5665 /* If controller is scanning, it means the passive scanning is
5666 * running. Thus, we should temporarily stop it in order to set the
5667 * discovery scanning parameters.
5669 err = hci_scan_disable_sync(hdev);
5671 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5675 cancel_interleave_scan(hdev);
5677 /* Pause address resolution for active scan and stop advertising if
5678 * privacy is enabled.
5680 err = hci_pause_addr_resolution(hdev);
5684 /* All active scans will be done with either a resolvable private
5685 * address (when privacy feature has been enabled) or non-resolvable
5688 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5691 own_addr_type = ADDR_LE_DEV_PUBLIC;
5693 if (hci_is_adv_monitoring(hdev)) {
5694 /* Duplicate filter should be disabled when some advertisement
5695 * monitor is activated, otherwise AdvMon can only receive one
5696 * advertisement for one peer(*) during active scanning, and
5697 * might report loss to these peers.
5699 * Note that different controllers have different meanings of
5700 * |duplicate|. Some of them consider packets with the same
5701 * address as duplicate, and others consider packets with the
5702 * same address and the same RSSI as duplicate. Although in the
5703 * latter case we don't need to disable duplicate filter, but
5704 * it is common to have active scanning for a short period of
5705 * time, the power impact should be neglectable.
5707 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5710 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5711 hdev->le_scan_window_discovery,
5712 own_addr_type, filter_policy, filter_dup);
5717 /* Resume advertising if it was paused */
5718 if (use_ll_privacy(hdev))
5719 hci_resume_advertising_sync(hdev);
5721 /* Resume passive scanning */
5722 hci_update_passive_scan_sync(hdev);
5726 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5730 bt_dev_dbg(hdev, "");
5732 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5736 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5739 int hci_start_discovery_sync(struct hci_dev *hdev)
5741 unsigned long timeout;
5744 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5746 switch (hdev->discovery.type) {
5747 case DISCOV_TYPE_BREDR:
5748 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5749 case DISCOV_TYPE_INTERLEAVED:
5750 /* When running simultaneous discovery, the LE scanning time
5751 * should occupy the whole discovery time sine BR/EDR inquiry
5752 * and LE scanning are scheduled by the controller.
5754 * For interleaving discovery in comparison, BR/EDR inquiry
5755 * and LE scanning are done sequentially with separate
5758 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5760 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5761 /* During simultaneous discovery, we double LE scan
5762 * interval. We must leave some time for the controller
5763 * to do BR/EDR inquiry.
5765 err = hci_start_interleaved_discovery_sync(hdev);
5769 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5770 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5772 case DISCOV_TYPE_LE:
5773 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5774 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5783 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5785 /* When service discovery is used and the controller has a
5786 * strict duplicate filter, it is important to remember the
5787 * start and duration of the scan. This is required for
5788 * restarting scanning during the discovery phase.
5790 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5791 hdev->discovery.result_filtering) {
5792 hdev->discovery.scan_start = jiffies;
5793 hdev->discovery.scan_duration = timeout;
5796 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5801 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5803 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5804 case HCI_ADV_MONITOR_EXT_MSFT:
5805 msft_suspend_sync(hdev);
5812 /* This function disables discovery and mark it as paused */
5813 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5815 int old_state = hdev->discovery.state;
5818 /* If discovery already stopped/stopping/paused there nothing to do */
5819 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
5820 hdev->discovery_paused)
5823 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5824 err = hci_stop_discovery_sync(hdev);
5828 hdev->discovery_paused = true;
5829 hdev->discovery_old_state = old_state;
5830 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5835 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5837 struct bdaddr_list_with_flags *b;
5838 u8 scan = SCAN_DISABLED;
5839 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5842 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5845 /* Some fake CSR controllers lock up after setting this type of
5846 * filter, so avoid sending the request altogether.
5848 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5851 /* Always clear event filter when starting */
5852 hci_clear_event_filter_sync(hdev);
5854 list_for_each_entry(b, &hdev->accept_list, list) {
5855 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
5858 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5860 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5861 HCI_CONN_SETUP_ALLOW_BDADDR,
5863 HCI_CONN_SETUP_AUTO_ON);
5865 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5871 if (scan && !scanning)
5872 hci_write_scan_enable_sync(hdev, scan);
5873 else if (!scan && scanning)
5874 hci_write_scan_enable_sync(hdev, scan);
5879 /* This function disables scan (BR and LE) and mark it as paused */
5880 static int hci_pause_scan_sync(struct hci_dev *hdev)
5882 if (hdev->scanning_paused)
5885 /* Disable page scan if enabled */
5886 if (test_bit(HCI_PSCAN, &hdev->flags))
5887 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
5889 hci_scan_disable_sync(hdev);
5891 hdev->scanning_paused = true;
5896 /* This function performs the HCI suspend procedures in the follow order:
5898 * Pause discovery (active scanning/inquiry)
5899 * Pause Directed Advertising/Advertising
5900 * Pause Scanning (passive scanning in case discovery was not active)
5901 * Disconnect all connections
5902 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5904 * Update event mask (only set events that are allowed to wake up the host)
5905 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
5906 * Update passive scanning (lower duty cycle)
5907 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
5909 int hci_suspend_sync(struct hci_dev *hdev)
5913 /* If marked as suspended there nothing to do */
5914 if (hdev->suspended)
5917 /* Mark device as suspended */
5918 hdev->suspended = true;
5920 /* Pause discovery if not already stopped */
5921 hci_pause_discovery_sync(hdev);
5923 /* Pause other advertisements */
5924 hci_pause_advertising_sync(hdev);
5926 /* Suspend monitor filters */
5927 hci_suspend_monitor_sync(hdev);
5929 /* Prevent disconnects from causing scanning to be re-enabled */
5930 hci_pause_scan_sync(hdev);
5932 if (hci_conn_count(hdev)) {
5933 /* Soft disconnect everything (power off) */
5934 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5936 /* Set state to BT_RUNNING so resume doesn't notify */
5937 hdev->suspend_state = BT_RUNNING;
5938 hci_resume_sync(hdev);
5942 /* Update event mask so only the allowed event can wakeup the
5945 hci_set_event_mask_sync(hdev);
5948 /* Only configure accept list if disconnect succeeded and wake
5949 * isn't being prevented.
5951 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5952 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5956 /* Unpause to take care of updating scanning params */
5957 hdev->scanning_paused = false;
5959 /* Enable event filter for paired devices */
5960 hci_update_event_filter_sync(hdev);
5962 /* Update LE passive scan if enabled */
5963 hci_update_passive_scan_sync(hdev);
5965 /* Pause scan changes again. */
5966 hdev->scanning_paused = true;
5968 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5973 /* This function resumes discovery */
5974 static int hci_resume_discovery_sync(struct hci_dev *hdev)
5978 /* If discovery not paused there nothing to do */
5979 if (!hdev->discovery_paused)
5982 hdev->discovery_paused = false;
5984 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
5986 err = hci_start_discovery_sync(hdev);
5988 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
5994 static void hci_resume_monitor_sync(struct hci_dev *hdev)
5996 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5997 case HCI_ADV_MONITOR_EXT_MSFT:
5998 msft_resume_sync(hdev);
6005 /* This function resume scan and reset paused flag */
6006 static int hci_resume_scan_sync(struct hci_dev *hdev)
6008 if (!hdev->scanning_paused)
6011 hdev->scanning_paused = false;
6013 hci_update_scan_sync(hdev);
6015 /* Reset passive scanning to normal */
6016 hci_update_passive_scan_sync(hdev);
6021 /* This function performs the HCI suspend procedures in the follow order:
6023 * Restore event mask
6024 * Clear event filter
6025 * Update passive scanning (normal duty cycle)
6026 * Resume Directed Advertising/Advertising
6027 * Resume discovery (active scanning/inquiry)
6029 int hci_resume_sync(struct hci_dev *hdev)
6031 /* If not marked as suspended there nothing to do */
6032 if (!hdev->suspended)
6035 hdev->suspended = false;
6037 /* Restore event mask */
6038 hci_set_event_mask_sync(hdev);
6040 /* Clear any event filters and restore scan state */
6041 hci_clear_event_filter_sync(hdev);
6043 /* Resume scanning */
6044 hci_resume_scan_sync(hdev);
6046 /* Resume monitor filters */
6047 hci_resume_monitor_sync(hdev);
6049 /* Resume other advertisements */
6050 hci_resume_advertising_sync(hdev);
6052 /* Resume discovery */
6053 hci_resume_discovery_sync(hdev);
6058 static bool conn_use_rpa(struct hci_conn *conn)
6060 struct hci_dev *hdev = conn->hdev;
6062 return hci_dev_test_flag(hdev, HCI_PRIVACY);
6065 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
6066 struct hci_conn *conn)
6068 struct hci_cp_le_set_ext_adv_params cp;
6070 bdaddr_t random_addr;
6073 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6078 /* Set require_privacy to false so that the remote device has a
6079 * chance of identifying us.
6081 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
6082 &own_addr_type, &random_addr);
6086 memset(&cp, 0, sizeof(cp));
6088 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
6089 cp.channel_map = hdev->le_adv_channel_map;
6090 cp.tx_power = HCI_TX_POWER_INVALID;
6091 cp.primary_phy = HCI_ADV_PHY_1M;
6092 cp.secondary_phy = HCI_ADV_PHY_1M;
6093 cp.handle = 0x00; /* Use instance 0 for directed adv */
6094 cp.own_addr_type = own_addr_type;
6095 cp.peer_addr_type = conn->dst_type;
6096 bacpy(&cp.peer_addr, &conn->dst);
6098 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
6099 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
6100 * does not supports advertising data when the advertising set already
6101 * contains some, the controller shall return erroc code 'Invalid
6102 * HCI Command Parameters(0x12).
6103 * So it is required to remove adv set for handle 0x00. since we use
6104 * instance 0 for directed adv.
6106 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
6110 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
6111 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6115 /* Check if random address need to be updated */
6116 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
6117 bacmp(&random_addr, BDADDR_ANY) &&
6118 bacmp(&random_addr, &hdev->random_addr)) {
6119 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
6125 return hci_enable_ext_advertising_sync(hdev, 0x00);
6128 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
6129 struct hci_conn *conn)
6131 struct hci_cp_le_set_adv_param cp;
6136 if (ext_adv_capable(hdev))
6137 return hci_le_ext_directed_advertising_sync(hdev, conn);
6139 /* Clear the HCI_LE_ADV bit temporarily so that the
6140 * hci_update_random_address knows that it's safe to go ahead
6141 * and write a new random address. The flag will be set back on
6142 * as soon as the SET_ADV_ENABLE HCI command completes.
6144 hci_dev_clear_flag(hdev, HCI_LE_ADV);
6146 /* Set require_privacy to false so that the remote device has a
6147 * chance of identifying us.
6149 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6154 memset(&cp, 0, sizeof(cp));
6156 /* Some controllers might reject command if intervals are not
6157 * within range for undirected advertising.
6158 * BCM20702A0 is known to be affected by this.
6160 cp.min_interval = cpu_to_le16(0x0020);
6161 cp.max_interval = cpu_to_le16(0x0020);
6163 cp.type = LE_ADV_DIRECT_IND;
6164 cp.own_address_type = own_addr_type;
6165 cp.direct_addr_type = conn->dst_type;
6166 bacpy(&cp.direct_addr, &conn->dst);
6167 cp.channel_map = hdev->le_adv_channel_map;
6169 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
6170 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6176 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
6177 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
6180 static void set_ext_conn_params(struct hci_conn *conn,
6181 struct hci_cp_le_ext_conn_param *p)
6183 struct hci_dev *hdev = conn->hdev;
6185 memset(p, 0, sizeof(*p));
6187 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6188 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6189 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6190 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6191 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
6192 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6193 p->min_ce_len = cpu_to_le16(0x0000);
6194 p->max_ce_len = cpu_to_le16(0x0000);
6197 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
6198 struct hci_conn *conn, u8 own_addr_type)
6200 struct hci_cp_le_ext_create_conn *cp;
6201 struct hci_cp_le_ext_conn_param *p;
6202 u8 data[sizeof(*cp) + sizeof(*p) * 3];
6206 p = (void *)cp->data;
6208 memset(cp, 0, sizeof(*cp));
6210 bacpy(&cp->peer_addr, &conn->dst);
6211 cp->peer_addr_type = conn->dst_type;
6212 cp->own_addr_type = own_addr_type;
6216 if (scan_1m(hdev)) {
6217 cp->phys |= LE_SCAN_PHY_1M;
6218 set_ext_conn_params(conn, p);
6224 if (scan_2m(hdev)) {
6225 cp->phys |= LE_SCAN_PHY_2M;
6226 set_ext_conn_params(conn, p);
6232 if (scan_coded(hdev)) {
6233 cp->phys |= LE_SCAN_PHY_CODED;
6234 set_ext_conn_params(conn, p);
6239 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6241 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
6242 conn->conn_timeout, NULL);
6245 int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
6247 struct hci_cp_le_create_conn cp;
6248 struct hci_conn_params *params;
6252 /* If requested to connect as peripheral use directed advertising */
6253 if (conn->role == HCI_ROLE_SLAVE) {
6254 /* If we're active scanning and simultaneous roles is not
6255 * enabled simply reject the attempt.
6257 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6258 hdev->le_scan_type == LE_SCAN_ACTIVE &&
6259 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6264 /* Pause advertising while doing directed advertising. */
6265 hci_pause_advertising_sync(hdev);
6267 err = hci_le_directed_advertising_sync(hdev, conn);
6271 /* Disable advertising if simultaneous roles is not in use. */
6272 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6273 hci_pause_advertising_sync(hdev);
6275 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6277 conn->le_conn_min_interval = params->conn_min_interval;
6278 conn->le_conn_max_interval = params->conn_max_interval;
6279 conn->le_conn_latency = params->conn_latency;
6280 conn->le_supv_timeout = params->supervision_timeout;
6282 conn->le_conn_min_interval = hdev->le_conn_min_interval;
6283 conn->le_conn_max_interval = hdev->le_conn_max_interval;
6284 conn->le_conn_latency = hdev->le_conn_latency;
6285 conn->le_supv_timeout = hdev->le_supv_timeout;
6288 /* If controller is scanning, we stop it since some controllers are
6289 * not able to scan and connect at the same time. Also set the
6290 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
6291 * handler for scan disabling knows to set the correct discovery
6294 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6295 hci_scan_disable_sync(hdev);
6296 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6299 /* Update random address, but set require_privacy to false so
6300 * that we never connect with an non-resolvable address.
6302 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6307 if (use_ext_conn(hdev)) {
6308 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6312 memset(&cp, 0, sizeof(cp));
6314 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6315 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6317 bacpy(&cp.peer_addr, &conn->dst);
6318 cp.peer_addr_type = conn->dst_type;
6319 cp.own_address_type = own_addr_type;
6320 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6321 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6322 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
6323 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6324 cp.min_ce_len = cpu_to_le16(0x0000);
6325 cp.max_ce_len = cpu_to_le16(0x0000);
6327 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6329 * If this event is unmasked and the HCI_LE_Connection_Complete event
6330 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6331 * sent when a new connection has been created.
6333 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6335 use_enhanced_conn_complete(hdev) ?
6336 HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6337 HCI_EV_LE_CONN_COMPLETE,
6338 conn->conn_timeout, NULL);
6341 if (err == -ETIMEDOUT)
6342 hci_le_connect_cancel_sync(hdev, conn, 0x00);
6344 /* Re-enable advertising after the connection attempt is finished. */
6345 hci_resume_advertising_sync(hdev);
6349 int hci_le_create_cis_sync(struct hci_dev *hdev)
6352 struct hci_cp_le_create_cis cp;
6353 struct hci_cis cis[0x1f];
6355 struct hci_conn *conn;
6356 u8 cig = BT_ISO_QOS_CIG_UNSET;
6358 /* The spec allows only one pending LE Create CIS command at a time. If
6359 * the command is pending now, don't do anything. We check for pending
6360 * connections after each CIS Established event.
6362 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6365 * If the Host issues this command before all the
6366 * HCI_LE_CIS_Established events from the previous use of the
6367 * command have been generated, the Controller shall return the
6368 * error code Command Disallowed (0x0C).
6370 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6373 * When the Controller receives the HCI_LE_Create_CIS command, the
6374 * Controller sends the HCI_Command_Status event to the Host. An
6375 * HCI_LE_CIS_Established event will be generated for each CIS when it
6376 * is established or if it is disconnected or considered lost before
6377 * being established; until all the events are generated, the command
6381 memset(&cmd, 0, sizeof(cmd));
6387 /* Wait until previous Create CIS has completed */
6388 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6389 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
6393 /* Find CIG with all CIS ready */
6394 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6395 struct hci_conn *link;
6397 if (hci_conn_check_create_cis(conn))
6400 cig = conn->iso_qos.ucast.cig;
6402 list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
6403 if (hci_conn_check_create_cis(link) > 0 &&
6404 link->iso_qos.ucast.cig == cig &&
6405 link->state != BT_CONNECTED) {
6406 cig = BT_ISO_QOS_CIG_UNSET;
6411 if (cig != BT_ISO_QOS_CIG_UNSET)
6415 if (cig == BT_ISO_QOS_CIG_UNSET)
6418 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6419 struct hci_cis *cis = &cmd.cis[cmd.cp.num_cis];
6421 if (hci_conn_check_create_cis(conn) ||
6422 conn->iso_qos.ucast.cig != cig)
6425 set_bit(HCI_CONN_CREATE_CIS, &conn->flags);
6426 cis->acl_handle = cpu_to_le16(conn->parent->handle);
6427 cis->cis_handle = cpu_to_le16(conn->handle);
6430 if (cmd.cp.num_cis >= ARRAY_SIZE(cmd.cis))
6437 hci_dev_unlock(hdev);
6439 if (!cmd.cp.num_cis)
6442 /* Wait for HCI_LE_CIS_Established */
6443 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6444 sizeof(cmd.cp) + sizeof(cmd.cis[0]) *
6445 cmd.cp.num_cis, &cmd,
6446 HCI_EVT_LE_CIS_ESTABLISHED,
6447 conn->conn_timeout, NULL);
6450 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6452 struct hci_cp_le_remove_cig cp;
6454 memset(&cp, 0, sizeof(cp));
6457 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6458 &cp, HCI_CMD_TIMEOUT);
6461 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6463 struct hci_cp_le_big_term_sync cp;
6465 memset(&cp, 0, sizeof(cp));
6468 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6469 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6472 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6474 struct hci_cp_le_pa_term_sync cp;
6476 memset(&cp, 0, sizeof(cp));
6477 cp.handle = cpu_to_le16(handle);
6479 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6480 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6483 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6484 bool use_rpa, struct adv_info *adv_instance,
6485 u8 *own_addr_type, bdaddr_t *rand_addr)
6489 bacpy(rand_addr, BDADDR_ANY);
6491 /* If privacy is enabled use a resolvable private address. If
6492 * current RPA has expired then generate a new one.
6495 /* If Controller supports LL Privacy use own address type is
6498 if (use_ll_privacy(hdev))
6499 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
6501 *own_addr_type = ADDR_LE_DEV_RANDOM;
6504 if (adv_rpa_valid(adv_instance))
6507 if (rpa_valid(hdev))
6511 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6513 bt_dev_err(hdev, "failed to generate new RPA");
6517 bacpy(rand_addr, &hdev->rpa);
6522 /* In case of required privacy without resolvable private address,
6523 * use an non-resolvable private address. This is useful for
6524 * non-connectable advertising.
6526 if (require_privacy) {
6530 /* The non-resolvable private address is generated
6531 * from random six bytes with the two most significant
6534 get_random_bytes(&nrpa, 6);
6537 /* The non-resolvable private address shall not be
6538 * equal to the public address.
6540 if (bacmp(&hdev->bdaddr, &nrpa))
6544 *own_addr_type = ADDR_LE_DEV_RANDOM;
6545 bacpy(rand_addr, &nrpa);
6550 /* No privacy so use a public address. */
6551 *own_addr_type = ADDR_LE_DEV_PUBLIC;
6556 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6558 u8 instance = PTR_UINT(data);
6560 return hci_update_adv_data_sync(hdev, instance);
6563 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6565 return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6566 UINT_PTR(instance), NULL);