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);
1231 /* Advertising scan response data is handled in bluez.
1232 * This value will be updated only when application request the update
1233 * using adapter_set_scan_rsp_data()
1238 if (hdev->scan_rsp_data_len == len &&
1239 !memcmp(cp.data, hdev->scan_rsp_data, len))
1242 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1243 hdev->scan_rsp_data_len = len;
1247 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1248 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1251 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1253 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1256 if (ext_adv_capable(hdev))
1257 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1259 return __hci_set_scan_rsp_data_sync(hdev, instance);
1262 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1264 struct hci_cp_le_set_ext_adv_enable *cp;
1265 struct hci_cp_ext_adv_set *set;
1266 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1267 struct adv_info *adv;
1270 adv = hci_find_adv_instance(hdev, instance);
1273 /* If already enabled there is nothing to do */
1281 set = (void *)cp->data;
1283 memset(cp, 0, sizeof(*cp));
1286 cp->num_of_sets = 0x01;
1288 memset(set, 0, sizeof(*set));
1290 set->handle = instance;
1292 /* Set duration per instance since controller is responsible for
1295 if (adv && adv->timeout) {
1296 u16 duration = adv->timeout * MSEC_PER_SEC;
1298 /* Time = N * 10 ms */
1299 set->duration = cpu_to_le16(duration / 10);
1302 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1304 sizeof(*set) * cp->num_of_sets,
1305 data, HCI_CMD_TIMEOUT);
1308 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1312 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1316 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1320 return hci_enable_ext_advertising_sync(hdev, instance);
1323 static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1325 struct hci_cp_le_set_per_adv_enable cp;
1326 struct adv_info *adv = NULL;
1328 /* If periodic advertising already disabled there is nothing to do. */
1329 adv = hci_find_adv_instance(hdev, instance);
1330 if (!adv || !adv->periodic || !adv->enabled)
1333 memset(&cp, 0, sizeof(cp));
1336 cp.handle = instance;
1338 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1339 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1342 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1343 u16 min_interval, u16 max_interval)
1345 struct hci_cp_le_set_per_adv_params cp;
1347 memset(&cp, 0, sizeof(cp));
1350 min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1353 max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1355 cp.handle = instance;
1356 cp.min_interval = cpu_to_le16(min_interval);
1357 cp.max_interval = cpu_to_le16(max_interval);
1358 cp.periodic_properties = 0x0000;
1360 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1361 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1364 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1367 struct hci_cp_le_set_per_adv_data cp;
1368 u8 data[HCI_MAX_PER_AD_LENGTH];
1372 memset(&pdu, 0, sizeof(pdu));
1375 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1377 if (!adv || !adv->periodic)
1381 len = eir_create_per_adv_data(hdev, instance, pdu.data);
1383 pdu.cp.length = len;
1384 pdu.cp.handle = instance;
1385 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1387 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1388 sizeof(pdu.cp) + len, &pdu,
1392 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1394 struct hci_cp_le_set_per_adv_enable cp;
1395 struct adv_info *adv = NULL;
1397 /* If periodic advertising already enabled there is nothing to do. */
1398 adv = hci_find_adv_instance(hdev, instance);
1399 if (adv && adv->periodic && adv->enabled)
1402 memset(&cp, 0, sizeof(cp));
1405 cp.handle = instance;
1407 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1408 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1411 /* Checks if periodic advertising data contains a Basic Announcement and if it
1412 * does generates a Broadcast ID and add Broadcast Announcement.
1414 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1419 /* Skip if NULL adv as instance 0x00 is used for general purpose
1420 * advertising so it cannot used for the likes of Broadcast Announcement
1421 * as it can be overwritten at any point.
1426 /* Check if PA data doesn't contains a Basic Audio Announcement then
1427 * there is nothing to do.
1429 if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1433 /* Check if advertising data already has a Broadcast Announcement since
1434 * the process may want to control the Broadcast ID directly and in that
1435 * case the kernel shall no interfere.
1437 if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1441 /* Generate Broadcast ID */
1442 get_random_bytes(bid, sizeof(bid));
1443 eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1444 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1446 return hci_update_adv_data_sync(hdev, adv->instance);
1449 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1450 u8 *data, u32 flags, u16 min_interval,
1451 u16 max_interval, u16 sync_interval)
1453 struct adv_info *adv = NULL;
1457 hci_disable_per_advertising_sync(hdev, instance);
1460 adv = hci_find_adv_instance(hdev, instance);
1461 /* Create an instance if that could not be found */
1463 adv = hci_add_per_instance(hdev, instance, flags,
1468 return PTR_ERR(adv);
1469 adv->pending = false;
1474 /* Start advertising */
1475 err = hci_start_ext_adv_sync(hdev, instance);
1479 err = hci_adv_bcast_annoucement(hdev, adv);
1483 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1488 err = hci_set_per_adv_data_sync(hdev, instance);
1492 err = hci_enable_per_advertising_sync(hdev, instance);
1500 hci_remove_adv_instance(hdev, instance);
1505 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1509 if (ext_adv_capable(hdev))
1510 return hci_start_ext_adv_sync(hdev, instance);
1512 err = hci_update_adv_data_sync(hdev, instance);
1516 err = hci_update_scan_rsp_data_sync(hdev, instance);
1520 return hci_enable_advertising_sync(hdev);
1523 int hci_enable_advertising_sync(struct hci_dev *hdev)
1525 struct adv_info *adv_instance;
1526 struct hci_cp_le_set_adv_param cp;
1527 u8 own_addr_type, enable = 0x01;
1529 u16 adv_min_interval, adv_max_interval;
1533 if (ext_adv_capable(hdev))
1534 return hci_enable_ext_advertising_sync(hdev,
1535 hdev->cur_adv_instance);
1537 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1538 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1540 /* If the "connectable" instance flag was not set, then choose between
1541 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1543 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1544 mgmt_get_connectable(hdev);
1546 if (!is_advertising_allowed(hdev, connectable))
1549 status = hci_disable_advertising_sync(hdev);
1553 /* Clear the HCI_LE_ADV bit temporarily so that the
1554 * hci_update_random_address knows that it's safe to go ahead
1555 * and write a new random address. The flag will be set back on
1556 * as soon as the SET_ADV_ENABLE HCI command completes.
1558 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1560 /* Set require_privacy to true only when non-connectable
1561 * advertising is used. In that case it is fine to use a
1562 * non-resolvable private address.
1564 status = hci_update_random_address_sync(hdev, !connectable,
1565 adv_use_rpa(hdev, flags),
1570 memset(&cp, 0, sizeof(cp));
1573 adv_min_interval = adv_instance->min_interval;
1574 adv_max_interval = adv_instance->max_interval;
1576 adv_min_interval = hdev->le_adv_min_interval;
1577 adv_max_interval = hdev->le_adv_max_interval;
1581 cp.filter_policy = hdev->adv_filter_policy;
1582 cp.type = hdev->adv_type;
1586 cp.type = LE_ADV_IND;
1588 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1589 cp.type = LE_ADV_SCAN_IND;
1591 cp.type = LE_ADV_NONCONN_IND;
1593 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1594 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1595 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1596 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1600 cp.min_interval = cpu_to_le16(adv_min_interval);
1601 cp.max_interval = cpu_to_le16(adv_max_interval);
1602 cp.own_address_type = own_addr_type;
1603 cp.channel_map = hdev->le_adv_channel_map;
1605 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1606 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1610 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1611 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1614 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1616 return hci_enable_advertising_sync(hdev);
1619 int hci_enable_advertising(struct hci_dev *hdev)
1621 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1622 list_empty(&hdev->adv_instances))
1625 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1628 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1633 if (!ext_adv_capable(hdev))
1636 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1640 /* If request specifies an instance that doesn't exist, fail */
1641 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1644 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1645 sizeof(instance), &instance, 0,
1646 HCI_CMD_TIMEOUT, sk);
1649 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1651 struct adv_info *adv = data;
1655 instance = adv->instance;
1657 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1660 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1662 struct adv_info *adv = NULL;
1665 adv = hci_find_adv_instance(hdev, instance);
1670 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1673 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1675 struct hci_cp_le_term_big cp;
1677 memset(&cp, 0, sizeof(cp));
1681 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1682 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1685 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1688 struct hci_cp_le_set_ext_adv_data cp;
1689 u8 data[HCI_MAX_EXT_AD_LENGTH];
1692 struct adv_info *adv = NULL;
1695 memset(&pdu, 0, sizeof(pdu));
1698 adv = hci_find_adv_instance(hdev, instance);
1699 if (!adv || !adv->adv_data_changed)
1703 len = eir_create_adv_data(hdev, instance, pdu.data);
1705 pdu.cp.length = len;
1706 pdu.cp.handle = instance;
1707 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1708 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1710 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1711 sizeof(pdu.cp) + len, &pdu.cp,
1716 /* Update data if the command succeed */
1718 adv->adv_data_changed = false;
1720 memcpy(hdev->adv_data, pdu.data, len);
1721 hdev->adv_data_len = len;
1727 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1729 struct hci_cp_le_set_adv_data cp;
1732 memset(&cp, 0, sizeof(cp));
1734 len = eir_create_adv_data(hdev, instance, cp.data);
1736 /* There's nothing to do if the data hasn't changed */
1737 if (hdev->adv_data_len == len &&
1738 memcmp(cp.data, hdev->adv_data, len) == 0)
1741 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1742 hdev->adv_data_len = len;
1746 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1747 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1750 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1752 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1756 /* Bluez will handle the advertising data including the flag and tx
1757 * power. This value will be updated only when application request the
1758 * update using adapter_set_advertising_data().
1763 if (ext_adv_capable(hdev))
1764 return hci_set_ext_adv_data_sync(hdev, instance);
1766 return hci_set_adv_data_sync(hdev, instance);
1769 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1772 struct adv_info *adv = NULL;
1775 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1778 if (hdev->adv_instance_timeout)
1781 adv = hci_find_adv_instance(hdev, instance);
1785 /* A zero timeout means unlimited advertising. As long as there is
1786 * only one instance, duration should be ignored. We still set a timeout
1787 * in case further instances are being added later on.
1789 * If the remaining lifetime of the instance is more than the duration
1790 * then the timeout corresponds to the duration, otherwise it will be
1791 * reduced to the remaining instance lifetime.
1793 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1794 timeout = adv->duration;
1796 timeout = adv->remaining_time;
1798 /* The remaining time is being reduced unless the instance is being
1799 * advertised without time limit.
1802 adv->remaining_time = adv->remaining_time - timeout;
1804 /* Only use work for scheduling instances with legacy advertising */
1805 if (!ext_adv_capable(hdev)) {
1806 hdev->adv_instance_timeout = timeout;
1807 queue_delayed_work(hdev->req_workqueue,
1808 &hdev->adv_instance_expire,
1809 msecs_to_jiffies(timeout * 1000));
1812 /* If we're just re-scheduling the same instance again then do not
1813 * execute any HCI commands. This happens when a single instance is
1816 if (!force && hdev->cur_adv_instance == instance &&
1817 hci_dev_test_flag(hdev, HCI_LE_ADV))
1820 hdev->cur_adv_instance = instance;
1822 return hci_start_adv_sync(hdev, instance);
1825 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1829 if (!ext_adv_capable(hdev))
1832 /* Disable instance 0x00 to disable all instances */
1833 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1837 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1838 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1841 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1843 struct adv_info *adv, *n;
1846 if (ext_adv_capable(hdev))
1847 /* Remove all existing sets */
1848 err = hci_clear_adv_sets_sync(hdev, sk);
1849 if (ext_adv_capable(hdev))
1852 /* This is safe as long as there is no command send while the lock is
1857 /* Cleanup non-ext instances */
1858 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1859 u8 instance = adv->instance;
1862 if (!(force || adv->timeout))
1865 err = hci_remove_adv_instance(hdev, instance);
1867 mgmt_advertising_removed(sk, hdev, instance);
1870 hci_dev_unlock(hdev);
1875 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1880 /* If we use extended advertising, instance has to be removed first. */
1881 if (ext_adv_capable(hdev))
1882 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1883 if (ext_adv_capable(hdev))
1886 /* This is safe as long as there is no command send while the lock is
1891 err = hci_remove_adv_instance(hdev, instance);
1893 mgmt_advertising_removed(sk, hdev, instance);
1895 hci_dev_unlock(hdev);
1900 /* For a single instance:
1901 * - force == true: The instance will be removed even when its remaining
1902 * lifetime is not zero.
1903 * - force == false: the instance will be deactivated but kept stored unless
1904 * the remaining lifetime is zero.
1906 * For instance == 0x00:
1907 * - force == true: All instances will be removed regardless of their timeout
1909 * - force == false: Only instances that have a timeout will be removed.
1911 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1912 u8 instance, bool force)
1914 struct adv_info *next = NULL;
1917 /* Cancel any timeout concerning the removed instance(s). */
1918 if (!instance || hdev->cur_adv_instance == instance)
1919 cancel_adv_timeout(hdev);
1921 /* Get the next instance to advertise BEFORE we remove
1922 * the current one. This can be the same instance again
1923 * if there is only one instance.
1925 if (hdev->cur_adv_instance == instance)
1926 next = hci_get_next_instance(hdev, instance);
1929 err = hci_clear_adv_sync(hdev, sk, force);
1933 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1935 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1936 /* Don't advertise a removed instance. */
1937 if (next && next->instance == instance)
1940 err = hci_remove_adv_sync(hdev, instance, sk);
1946 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1949 if (next && !ext_adv_capable(hdev))
1950 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1955 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1957 struct hci_cp_read_rssi cp;
1960 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1961 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1964 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1966 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1967 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1970 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1972 struct hci_cp_read_tx_power cp;
1976 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1977 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1980 int hci_disable_advertising_sync(struct hci_dev *hdev)
1985 /* If controller is not advertising we are done. */
1986 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1989 if (ext_adv_capable(hdev))
1990 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1991 if (ext_adv_capable(hdev))
1994 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1995 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1998 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
2001 struct hci_cp_le_set_ext_scan_enable cp;
2003 memset(&cp, 0, sizeof(cp));
2006 if (hci_dev_test_flag(hdev, HCI_MESH))
2007 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2009 cp.filter_dup = filter_dup;
2011 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
2012 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2015 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2018 struct hci_cp_le_set_scan_enable cp;
2020 if (use_ext_scan(hdev))
2021 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2023 memset(&cp, 0, sizeof(cp));
2026 if (val && hci_dev_test_flag(hdev, HCI_MESH))
2027 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2029 cp.filter_dup = filter_dup;
2031 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2032 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2035 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2037 if (!use_ll_privacy(hdev))
2040 /* If controller is not/already resolving we are done. */
2041 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2044 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2045 sizeof(val), &val, HCI_CMD_TIMEOUT);
2048 static int hci_scan_disable_sync(struct hci_dev *hdev)
2052 /* If controller is not scanning we are done. */
2053 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2056 if (hdev->scanning_paused) {
2057 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2061 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2063 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2070 static bool scan_use_rpa(struct hci_dev *hdev)
2072 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2075 static void hci_start_interleave_scan(struct hci_dev *hdev)
2077 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2078 queue_delayed_work(hdev->req_workqueue,
2079 &hdev->interleave_scan, 0);
2082 static bool is_interleave_scanning(struct hci_dev *hdev)
2084 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2087 static void cancel_interleave_scan(struct hci_dev *hdev)
2089 bt_dev_dbg(hdev, "cancelling interleave scan");
2091 cancel_delayed_work_sync(&hdev->interleave_scan);
2093 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2096 /* Return true if interleave_scan wasn't started until exiting this function,
2097 * otherwise, return false
2099 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2101 /* Do interleaved scan only if all of the following are true:
2102 * - There is at least one ADV monitor
2103 * - At least one pending LE connection or one device to be scanned for
2104 * - Monitor offloading is not supported
2105 * If so, we should alternate between allowlist scan and one without
2106 * any filters to save power.
2108 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2109 !(list_empty(&hdev->pend_le_conns) &&
2110 list_empty(&hdev->pend_le_reports)) &&
2111 hci_get_adv_monitor_offload_ext(hdev) ==
2112 HCI_ADV_MONITOR_EXT_NONE;
2113 bool is_interleaving = is_interleave_scanning(hdev);
2115 if (use_interleaving && !is_interleaving) {
2116 hci_start_interleave_scan(hdev);
2117 bt_dev_dbg(hdev, "starting interleave scan");
2121 if (!use_interleaving && is_interleaving)
2122 cancel_interleave_scan(hdev);
2127 /* Removes connection to resolve list if needed.*/
2128 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2129 bdaddr_t *bdaddr, u8 bdaddr_type)
2131 struct hci_cp_le_del_from_resolv_list cp;
2132 struct bdaddr_list_with_irk *entry;
2134 if (!use_ll_privacy(hdev))
2137 /* Check if the IRK has been programmed */
2138 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2143 cp.bdaddr_type = bdaddr_type;
2144 bacpy(&cp.bdaddr, bdaddr);
2146 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2147 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2150 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2151 bdaddr_t *bdaddr, u8 bdaddr_type)
2153 struct hci_cp_le_del_from_accept_list cp;
2156 /* Check if device is on accept list before removing it */
2157 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2160 cp.bdaddr_type = bdaddr_type;
2161 bacpy(&cp.bdaddr, bdaddr);
2163 /* Ignore errors when removing from resolving list as that is likely
2164 * that the device was never added.
2166 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2168 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2169 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2171 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2175 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2181 struct conn_params {
2184 hci_conn_flags_t flags;
2188 /* Adds connection to resolve list if needed.
2189 * Setting params to NULL programs local hdev->irk
2191 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2192 struct conn_params *params)
2194 struct hci_cp_le_add_to_resolv_list cp;
2195 struct smp_irk *irk;
2196 struct bdaddr_list_with_irk *entry;
2197 struct hci_conn_params *p;
2199 if (!use_ll_privacy(hdev))
2202 /* Attempt to program local identity address, type and irk if params is
2206 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2209 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2210 memcpy(cp.peer_irk, hdev->irk, 16);
2214 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2218 /* Check if the IK has _not_ been programmed yet. */
2219 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2225 cp.bdaddr_type = params->addr_type;
2226 bacpy(&cp.bdaddr, ¶ms->addr);
2227 memcpy(cp.peer_irk, irk->val, 16);
2229 /* Default privacy mode is always Network */
2230 params->privacy_mode = HCI_NETWORK_PRIVACY;
2233 p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2234 ¶ms->addr, params->addr_type);
2236 p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2237 ¶ms->addr, params->addr_type);
2239 WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY);
2243 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2244 memcpy(cp.local_irk, hdev->irk, 16);
2246 memset(cp.local_irk, 0, 16);
2248 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2249 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2252 /* Set Device Privacy Mode. */
2253 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2254 struct conn_params *params)
2256 struct hci_cp_le_set_privacy_mode cp;
2257 struct smp_irk *irk;
2259 /* If device privacy mode has already been set there is nothing to do */
2260 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2263 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2264 * indicates that LL Privacy has been enabled and
2265 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2267 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2270 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2274 memset(&cp, 0, sizeof(cp));
2275 cp.bdaddr_type = irk->addr_type;
2276 bacpy(&cp.bdaddr, &irk->bdaddr);
2277 cp.mode = HCI_DEVICE_PRIVACY;
2279 /* Note: params->privacy_mode is not updated since it is a copy */
2281 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2282 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2285 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2286 * this attempts to program the device in the resolving list as well and
2287 * properly set the privacy mode.
2289 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2290 struct conn_params *params,
2293 struct hci_cp_le_add_to_accept_list cp;
2296 /* During suspend, only wakeable devices can be in acceptlist */
2297 if (hdev->suspended &&
2298 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
2301 /* Select filter policy to accept all advertising */
2302 if (*num_entries >= hdev->le_accept_list_size)
2305 /* Accept list can not be used with RPAs */
2306 if (!use_ll_privacy(hdev) &&
2307 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type))
2310 /* Attempt to program the device in the resolving list first to avoid
2311 * having to rollback in case it fails since the resolving list is
2312 * dynamic it can probably be smaller than the accept list.
2314 err = hci_le_add_resolve_list_sync(hdev, params);
2316 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2320 /* Set Privacy Mode */
2321 err = hci_le_set_privacy_mode_sync(hdev, params);
2323 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2327 /* Check if already in accept list */
2328 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
2333 cp.bdaddr_type = params->addr_type;
2334 bacpy(&cp.bdaddr, ¶ms->addr);
2336 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2337 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2339 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2340 /* Rollback the device from the resolving list */
2341 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2345 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2351 /* This function disables/pause all advertising instances */
2352 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2357 /* If already been paused there is nothing to do. */
2358 if (hdev->advertising_paused)
2361 bt_dev_dbg(hdev, "Pausing directed advertising");
2363 /* Stop directed advertising */
2364 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2366 /* When discoverable timeout triggers, then just make sure
2367 * the limited discoverable flag is cleared. Even in the case
2368 * of a timeout triggered from general discoverable, it is
2369 * safe to unconditionally clear the flag.
2371 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2372 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2373 hdev->discov_timeout = 0;
2376 bt_dev_dbg(hdev, "Pausing advertising instances");
2378 /* Call to disable any advertisements active on the controller.
2379 * This will succeed even if no advertisements are configured.
2381 err = hci_disable_advertising_sync(hdev);
2385 /* If we are using software rotation, pause the loop */
2386 if (!ext_adv_capable(hdev))
2387 cancel_adv_timeout(hdev);
2389 hdev->advertising_paused = true;
2390 hdev->advertising_old_state = old_state;
2395 /* This function enables all user advertising instances */
2396 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2398 struct adv_info *adv, *tmp;
2401 /* If advertising has not been paused there is nothing to do. */
2402 if (!hdev->advertising_paused)
2405 /* Resume directed advertising */
2406 hdev->advertising_paused = false;
2407 if (hdev->advertising_old_state) {
2408 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2409 hdev->advertising_old_state = 0;
2412 bt_dev_dbg(hdev, "Resuming advertising instances");
2414 if (ext_adv_capable(hdev)) {
2415 /* Call for each tracked instance to be re-enabled */
2416 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2417 err = hci_enable_ext_advertising_sync(hdev,
2422 /* If the instance cannot be resumed remove it */
2423 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2427 /* Schedule for most recent instance to be restarted and begin
2428 * the software rotation loop
2430 err = hci_schedule_adv_instance_sync(hdev,
2431 hdev->cur_adv_instance,
2435 hdev->advertising_paused = false;
2440 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2444 if (!use_ll_privacy(hdev))
2447 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2450 /* Cannot disable addr resolution if scanning is enabled or
2451 * when initiating an LE connection.
2453 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2454 hci_lookup_le_connect(hdev)) {
2455 bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2459 /* Cannot disable addr resolution if advertising is enabled. */
2460 err = hci_pause_advertising_sync(hdev);
2462 bt_dev_err(hdev, "Pause advertising failed: %d", err);
2466 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2468 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2471 /* Return if address resolution is disabled and RPA is not used. */
2472 if (!err && scan_use_rpa(hdev))
2475 hci_resume_advertising_sync(hdev);
2479 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2480 bool extended, struct sock *sk)
2482 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2483 HCI_OP_READ_LOCAL_OOB_DATA;
2485 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2488 static struct conn_params *conn_params_copy(struct list_head *list, size_t *n)
2490 struct hci_conn_params *params;
2491 struct conn_params *p;
2497 list_for_each_entry_rcu(params, list, action)
2503 p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL);
2510 list_for_each_entry_rcu(params, list, action) {
2511 /* Racing adds are handled in next scan update */
2515 /* No hdev->lock, but: addr, addr_type are immutable.
2516 * privacy_mode is only written by us or in
2517 * hci_cc_le_set_privacy_mode that we wait for.
2518 * We should be idempotent so MGMT updating flags
2519 * while we are processing is OK.
2521 bacpy(&p[i].addr, ¶ms->addr);
2522 p[i].addr_type = params->addr_type;
2523 p[i].flags = READ_ONCE(params->flags);
2524 p[i].privacy_mode = READ_ONCE(params->privacy_mode);
2534 /* Device must not be scanning when updating the accept list.
2536 * Update is done using the following sequence:
2538 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
2539 * Remove Devices From Accept List ->
2540 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
2541 * Add Devices to Accept List ->
2542 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
2543 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
2546 * In case of failure advertising shall be restored to its original state and
2547 * return would disable accept list since either accept or resolving list could
2548 * not be programmed.
2551 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2553 struct conn_params *params;
2554 struct bdaddr_list *b, *t;
2556 bool pend_conn, pend_report;
2561 /* Pause advertising if resolving list can be used as controllers
2562 * cannot accept resolving list modifications while advertising.
2564 if (use_ll_privacy(hdev)) {
2565 err = hci_pause_advertising_sync(hdev);
2567 bt_dev_err(hdev, "pause advertising failed: %d", err);
2572 /* Disable address resolution while reprogramming accept list since
2573 * devices that do have an IRK will be programmed in the resolving list
2574 * when LL Privacy is enabled.
2576 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2578 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2582 /* Go through the current accept list programmed into the
2583 * controller one by one and check if that address is connected or is
2584 * still in the list of pending connections or list of devices to
2585 * report. If not present in either list, then remove it from
2588 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2589 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2592 /* Pointers not dereferenced, no locks needed */
2593 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2596 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2600 /* If the device is not likely to connect or report,
2601 * remove it from the acceptlist.
2603 if (!pend_conn && !pend_report) {
2604 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2612 /* Since all no longer valid accept list entries have been
2613 * removed, walk through the list of pending connections
2614 * and ensure that any new device gets programmed into
2617 * If the list of the devices is larger than the list of
2618 * available accept list entries in the controller, then
2619 * just abort and return filer policy value to not use the
2622 * The list and params may be mutated while we wait for events,
2623 * so make a copy and iterate it.
2626 params = conn_params_copy(&hdev->pend_le_conns, &n);
2632 for (i = 0; i < n; ++i) {
2633 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2643 /* After adding all new pending connections, walk through
2644 * the list of pending reports and also add these to the
2645 * accept list if there is still space. Abort if space runs out.
2648 params = conn_params_copy(&hdev->pend_le_reports, &n);
2654 for (i = 0; i < n; ++i) {
2655 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2665 /* Use the allowlist unless the following conditions are all true:
2666 * - We are not currently suspending
2667 * - There are 1 or more ADV monitors registered and it's not offloaded
2668 * - Interleaved scanning is not currently using the allowlist
2670 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2671 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2672 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2676 filter_policy = err ? 0x00 : 0x01;
2678 /* Enable address resolution when LL Privacy is enabled. */
2679 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2681 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2683 /* Resume advertising if it was paused */
2684 if (use_ll_privacy(hdev))
2685 hci_resume_advertising_sync(hdev);
2687 /* Select filter policy to use accept list */
2688 return filter_policy;
2691 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2692 u16 interval, u16 window,
2693 u8 own_addr_type, u8 filter_policy)
2695 struct hci_cp_le_set_ext_scan_params *cp;
2696 struct hci_cp_le_scan_phy_params *phy;
2697 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2701 phy = (void *)cp->data;
2703 memset(data, 0, sizeof(data));
2705 cp->own_addr_type = own_addr_type;
2706 cp->filter_policy = filter_policy;
2708 if (scan_1m(hdev) || scan_2m(hdev)) {
2709 cp->scanning_phys |= LE_SCAN_PHY_1M;
2712 phy->interval = cpu_to_le16(interval);
2713 phy->window = cpu_to_le16(window);
2719 if (scan_coded(hdev)) {
2720 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2723 phy->interval = cpu_to_le16(interval);
2724 phy->window = cpu_to_le16(window);
2730 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2731 sizeof(*cp) + sizeof(*phy) * num_phy,
2732 data, HCI_CMD_TIMEOUT);
2735 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2736 u16 interval, u16 window,
2737 u8 own_addr_type, u8 filter_policy)
2739 struct hci_cp_le_set_scan_param cp;
2741 if (use_ext_scan(hdev))
2742 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2743 window, own_addr_type,
2746 memset(&cp, 0, sizeof(cp));
2748 cp.interval = cpu_to_le16(interval);
2749 cp.window = cpu_to_le16(window);
2750 cp.own_address_type = own_addr_type;
2751 cp.filter_policy = filter_policy;
2753 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2754 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2757 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2758 u16 window, u8 own_addr_type, u8 filter_policy,
2763 if (hdev->scanning_paused) {
2764 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2768 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2769 own_addr_type, filter_policy);
2773 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2776 static int hci_passive_scan_sync(struct hci_dev *hdev)
2780 u16 window, interval;
2781 u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2784 if (hdev->scanning_paused) {
2785 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2789 err = hci_scan_disable_sync(hdev);
2791 bt_dev_err(hdev, "disable scanning failed: %d", err);
2795 /* Set require_privacy to false since no SCAN_REQ are send
2796 * during passive scanning. Not using an non-resolvable address
2797 * here is important so that peer devices using direct
2798 * advertising with our address will be correctly reported
2799 * by the controller.
2801 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2805 if (hdev->enable_advmon_interleave_scan &&
2806 hci_update_interleaved_scan_sync(hdev))
2809 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2811 /* Adding or removing entries from the accept list must
2812 * happen before enabling scanning. The controller does
2813 * not allow accept list modification while scanning.
2815 filter_policy = hci_update_accept_list_sync(hdev);
2817 /* When the controller is using random resolvable addresses and
2818 * with that having LE privacy enabled, then controllers with
2819 * Extended Scanner Filter Policies support can now enable support
2820 * for handling directed advertising.
2822 * So instead of using filter polices 0x00 (no acceptlist)
2823 * and 0x01 (acceptlist enabled) use the new filter policies
2824 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2826 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2827 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2828 filter_policy |= 0x02;
2830 if (hdev->suspended) {
2831 window = hdev->le_scan_window_suspend;
2832 interval = hdev->le_scan_int_suspend;
2833 } else if (hci_is_le_conn_scanning(hdev)) {
2834 window = hdev->le_scan_window_connect;
2835 interval = hdev->le_scan_int_connect;
2836 } else if (hci_is_adv_monitoring(hdev)) {
2837 window = hdev->le_scan_window_adv_monitor;
2838 interval = hdev->le_scan_int_adv_monitor;
2840 window = hdev->le_scan_window;
2841 interval = hdev->le_scan_interval;
2844 /* Disable all filtering for Mesh */
2845 if (hci_dev_test_flag(hdev, HCI_MESH)) {
2847 filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
2850 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2852 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2853 own_addr_type, filter_policy, filter_dups);
2856 /* This function controls the passive scanning based on hdev->pend_le_conns
2857 * list. If there are pending LE connection we start the background scanning,
2858 * otherwise we stop it in the following sequence:
2860 * If there are devices to scan:
2862 * Disable Scanning -> Update Accept List ->
2863 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2864 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2871 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2875 if (!test_bit(HCI_UP, &hdev->flags) ||
2876 test_bit(HCI_INIT, &hdev->flags) ||
2877 hci_dev_test_flag(hdev, HCI_SETUP) ||
2878 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2879 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2880 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2883 /* No point in doing scanning if LE support hasn't been enabled */
2884 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2887 /* If discovery is active don't interfere with it */
2888 if (hdev->discovery.state != DISCOVERY_STOPPED)
2891 /* Reset RSSI and UUID filters when starting background scanning
2892 * since these filters are meant for service discovery only.
2894 * The Start Discovery and Start Service Discovery operations
2895 * ensure to set proper values for RSSI threshold and UUID
2896 * filter list. So it is safe to just reset them here.
2898 hci_discovery_filter_clear(hdev);
2900 bt_dev_dbg(hdev, "ADV monitoring is %s",
2901 hci_is_adv_monitoring(hdev) ? "on" : "off");
2903 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
2904 list_empty(&hdev->pend_le_conns) &&
2905 list_empty(&hdev->pend_le_reports) &&
2906 !hci_is_adv_monitoring(hdev) &&
2907 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2908 /* If there is no pending LE connections or devices
2909 * to be scanned for or no ADV monitors, we should stop the
2910 * background scanning.
2913 bt_dev_dbg(hdev, "stopping background scanning");
2915 err = hci_scan_disable_sync(hdev);
2917 bt_dev_err(hdev, "stop background scanning failed: %d",
2920 /* If there is at least one pending LE connection, we should
2921 * keep the background scan running.
2924 /* If controller is connecting, we should not start scanning
2925 * since some controllers are not able to scan and connect at
2928 if (hci_lookup_le_connect(hdev))
2931 bt_dev_dbg(hdev, "start background scanning");
2933 err = hci_passive_scan_sync(hdev);
2935 bt_dev_err(hdev, "start background scanning failed: %d",
2942 static int update_scan_sync(struct hci_dev *hdev, void *data)
2944 return hci_update_scan_sync(hdev);
2947 int hci_update_scan(struct hci_dev *hdev)
2949 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
2952 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2954 return hci_update_passive_scan_sync(hdev);
2957 int hci_update_passive_scan(struct hci_dev *hdev)
2959 /* Only queue if it would have any effect */
2960 if (!test_bit(HCI_UP, &hdev->flags) ||
2961 test_bit(HCI_INIT, &hdev->flags) ||
2962 hci_dev_test_flag(hdev, HCI_SETUP) ||
2963 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2964 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2965 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2968 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2971 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2975 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2978 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2979 sizeof(val), &val, HCI_CMD_TIMEOUT);
2983 hdev->features[1][0] |= LMP_HOST_SC;
2984 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2986 hdev->features[1][0] &= ~LMP_HOST_SC;
2987 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2994 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2998 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2999 lmp_host_ssp_capable(hdev))
3002 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
3003 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
3004 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3007 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3008 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3012 return hci_write_sc_support_sync(hdev, 0x01);
3015 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
3017 struct hci_cp_write_le_host_supported cp;
3019 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
3020 !lmp_bredr_capable(hdev))
3023 /* Check first if we already have the right host state
3024 * (host features set)
3026 if (le == lmp_host_le_capable(hdev) &&
3027 simul == lmp_host_le_br_capable(hdev))
3030 memset(&cp, 0, sizeof(cp));
3035 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3036 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3039 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3041 struct adv_info *adv, *tmp;
3044 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3047 /* If RPA Resolution has not been enable yet it means the
3048 * resolving list is empty and we should attempt to program the
3049 * local IRK in order to support using own_addr_type
3050 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
3052 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3053 hci_le_add_resolve_list_sync(hdev, NULL);
3054 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3057 /* Make sure the controller has a good default for
3058 * advertising data. This also applies to the case
3059 * where BR/EDR was toggled during the AUTO_OFF phase.
3061 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3062 list_empty(&hdev->adv_instances)) {
3063 if (ext_adv_capable(hdev)) {
3064 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3066 hci_update_scan_rsp_data_sync(hdev, 0x00);
3068 err = hci_update_adv_data_sync(hdev, 0x00);
3070 hci_update_scan_rsp_data_sync(hdev, 0x00);
3073 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3074 hci_enable_advertising_sync(hdev);
3077 /* Call for each tracked instance to be scheduled */
3078 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3079 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3084 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3088 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3089 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3092 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3093 sizeof(link_sec), &link_sec,
3097 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3099 struct hci_cp_write_page_scan_activity cp;
3103 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3106 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3109 memset(&cp, 0, sizeof(cp));
3112 type = PAGE_SCAN_TYPE_INTERLACED;
3114 /* 160 msec page scan interval */
3115 cp.interval = cpu_to_le16(0x0100);
3117 type = hdev->def_page_scan_type;
3118 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3121 cp.window = cpu_to_le16(hdev->def_page_scan_window);
3123 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3124 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3125 err = __hci_cmd_sync_status(hdev,
3126 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
3127 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3132 if (hdev->page_scan_type != type)
3133 err = __hci_cmd_sync_status(hdev,
3134 HCI_OP_WRITE_PAGE_SCAN_TYPE,
3135 sizeof(type), &type,
3141 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3143 struct bdaddr_list *b;
3145 list_for_each_entry(b, &hdev->accept_list, list) {
3146 struct hci_conn *conn;
3148 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3152 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3159 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3161 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3166 int hci_update_scan_sync(struct hci_dev *hdev)
3170 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3173 if (!hdev_is_powered(hdev))
3176 if (mgmt_powering_down(hdev))
3179 if (hdev->scanning_paused)
3182 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3183 disconnected_accept_list_entries(hdev))
3186 scan = SCAN_DISABLED;
3188 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3189 scan |= SCAN_INQUIRY;
3191 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3192 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3195 return hci_write_scan_enable_sync(hdev, scan);
3198 int hci_update_name_sync(struct hci_dev *hdev)
3200 struct hci_cp_write_local_name cp;
3202 memset(&cp, 0, sizeof(cp));
3204 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3206 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3211 /* This function perform powered update HCI command sequence after the HCI init
3212 * sequence which end up resetting all states, the sequence is as follows:
3214 * HCI_SSP_ENABLED(Enable SSP)
3215 * HCI_LE_ENABLED(Enable LE)
3216 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
3218 * Enable Authentication
3219 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3220 * Set Name -> Set EIR)
3221 * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address)
3223 int hci_powered_update_sync(struct hci_dev *hdev)
3227 /* Register the available SMP channels (BR/EDR and LE) only when
3228 * successfully powering on the controller. This late
3229 * registration is required so that LE SMP can clearly decide if
3230 * the public address or static address is used.
3234 err = hci_write_ssp_mode_sync(hdev, 0x01);
3238 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3242 err = hci_powered_update_adv_sync(hdev);
3246 err = hci_write_auth_enable_sync(hdev);
3250 if (lmp_bredr_capable(hdev)) {
3251 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3252 hci_write_fast_connectable_sync(hdev, true);
3254 hci_write_fast_connectable_sync(hdev, false);
3255 hci_update_scan_sync(hdev);
3256 hci_update_class_sync(hdev);
3257 hci_update_name_sync(hdev);
3258 hci_update_eir_sync(hdev);
3261 /* If forcing static address is in use or there is no public
3262 * address use the static address as random address (but skip
3263 * the HCI command if the current random address is already the
3266 * In case BR/EDR has been disabled on a dual-mode controller
3267 * and a static address has been configured, then use that
3268 * address instead of the public BR/EDR address.
3270 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3271 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3272 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3273 if (bacmp(&hdev->static_addr, BDADDR_ANY))
3274 return hci_set_random_addr_sync(hdev,
3275 &hdev->static_addr);
3282 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3283 * (BD_ADDR) for a HCI device from
3284 * a firmware node property.
3285 * @hdev: The HCI device
3287 * Search the firmware node for 'local-bd-address'.
3289 * All-zero BD addresses are rejected, because those could be properties
3290 * that exist in the firmware tables, but were not updated by the firmware. For
3291 * example, the DTS could define 'local-bd-address', with zero BD addresses.
3293 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3295 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3299 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3300 (u8 *)&ba, sizeof(ba));
3301 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3304 bacpy(&hdev->public_addr, &ba);
3307 struct hci_init_stage {
3308 int (*func)(struct hci_dev *hdev);
3311 /* Run init stage NULL terminated function table */
3312 static int hci_init_stage_sync(struct hci_dev *hdev,
3313 const struct hci_init_stage *stage)
3317 for (i = 0; stage[i].func; i++) {
3320 err = stage[i].func(hdev);
3328 /* Read Local Version */
3329 static int hci_read_local_version_sync(struct hci_dev *hdev)
3331 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3332 0, NULL, HCI_CMD_TIMEOUT);
3335 /* Read BD Address */
3336 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3338 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3339 0, NULL, HCI_CMD_TIMEOUT);
3342 #define HCI_INIT(_func) \
3347 static const struct hci_init_stage hci_init0[] = {
3348 /* HCI_OP_READ_LOCAL_VERSION */
3349 HCI_INIT(hci_read_local_version_sync),
3350 /* HCI_OP_READ_BD_ADDR */
3351 HCI_INIT(hci_read_bd_addr_sync),
3355 int hci_reset_sync(struct hci_dev *hdev)
3359 set_bit(HCI_RESET, &hdev->flags);
3361 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3369 static int hci_init0_sync(struct hci_dev *hdev)
3373 bt_dev_dbg(hdev, "");
3376 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3377 err = hci_reset_sync(hdev);
3382 return hci_init_stage_sync(hdev, hci_init0);
3385 static int hci_unconf_init_sync(struct hci_dev *hdev)
3389 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3392 err = hci_init0_sync(hdev);
3396 if (hci_dev_test_flag(hdev, HCI_SETUP))
3397 hci_debugfs_create_basic(hdev);
3402 /* Read Local Supported Features. */
3403 static int hci_read_local_features_sync(struct hci_dev *hdev)
3405 /* Not all AMP controllers support this command */
3406 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
3409 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3410 0, NULL, HCI_CMD_TIMEOUT);
3413 /* BR Controller init stage 1 command sequence */
3414 static const struct hci_init_stage br_init1[] = {
3415 /* HCI_OP_READ_LOCAL_FEATURES */
3416 HCI_INIT(hci_read_local_features_sync),
3417 /* HCI_OP_READ_LOCAL_VERSION */
3418 HCI_INIT(hci_read_local_version_sync),
3419 /* HCI_OP_READ_BD_ADDR */
3420 HCI_INIT(hci_read_bd_addr_sync),
3424 /* Read Local Commands */
3425 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3427 /* All Bluetooth 1.2 and later controllers should support the
3428 * HCI command for reading the local supported commands.
3430 * Unfortunately some controllers indicate Bluetooth 1.2 support,
3431 * but do not have support for this command. If that is the case,
3432 * the driver can quirk the behavior and skip reading the local
3433 * supported commands.
3435 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3436 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3437 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3438 0, NULL, HCI_CMD_TIMEOUT);
3443 /* Read Local AMP Info */
3444 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
3446 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
3447 0, NULL, HCI_CMD_TIMEOUT);
3450 /* Read Data Blk size */
3451 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
3453 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
3454 0, NULL, HCI_CMD_TIMEOUT);
3457 /* Read Flow Control Mode */
3458 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
3460 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
3461 0, NULL, HCI_CMD_TIMEOUT);
3464 /* Read Location Data */
3465 static int hci_read_location_data_sync(struct hci_dev *hdev)
3467 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
3468 0, NULL, HCI_CMD_TIMEOUT);
3471 /* AMP Controller init stage 1 command sequence */
3472 static const struct hci_init_stage amp_init1[] = {
3473 /* HCI_OP_READ_LOCAL_VERSION */
3474 HCI_INIT(hci_read_local_version_sync),
3475 /* HCI_OP_READ_LOCAL_COMMANDS */
3476 HCI_INIT(hci_read_local_cmds_sync),
3477 /* HCI_OP_READ_LOCAL_AMP_INFO */
3478 HCI_INIT(hci_read_local_amp_info_sync),
3479 /* HCI_OP_READ_DATA_BLOCK_SIZE */
3480 HCI_INIT(hci_read_data_block_size_sync),
3481 /* HCI_OP_READ_FLOW_CONTROL_MODE */
3482 HCI_INIT(hci_read_flow_control_mode_sync),
3483 /* HCI_OP_READ_LOCATION_DATA */
3484 HCI_INIT(hci_read_location_data_sync),
3488 static int hci_init1_sync(struct hci_dev *hdev)
3492 bt_dev_dbg(hdev, "");
3495 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3496 err = hci_reset_sync(hdev);
3501 switch (hdev->dev_type) {
3503 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3504 return hci_init_stage_sync(hdev, br_init1);
3506 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3507 return hci_init_stage_sync(hdev, amp_init1);
3509 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
3516 /* AMP Controller init stage 2 command sequence */
3517 static const struct hci_init_stage amp_init2[] = {
3518 /* HCI_OP_READ_LOCAL_FEATURES */
3519 HCI_INIT(hci_read_local_features_sync),
3523 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
3524 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3526 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3527 0, NULL, HCI_CMD_TIMEOUT);
3530 /* Read Class of Device */
3531 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3533 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3534 0, NULL, HCI_CMD_TIMEOUT);
3537 /* Read Local Name */
3538 static int hci_read_local_name_sync(struct hci_dev *hdev)
3540 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3541 0, NULL, HCI_CMD_TIMEOUT);
3544 /* Read Voice Setting */
3545 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3547 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3548 0, NULL, HCI_CMD_TIMEOUT);
3551 /* Read Number of Supported IAC */
3552 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3554 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3555 0, NULL, HCI_CMD_TIMEOUT);
3558 /* Read Current IAC LAP */
3559 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3561 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3562 0, NULL, HCI_CMD_TIMEOUT);
3565 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3566 u8 cond_type, bdaddr_t *bdaddr,
3569 struct hci_cp_set_event_filter cp;
3571 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3574 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3577 memset(&cp, 0, sizeof(cp));
3578 cp.flt_type = flt_type;
3580 if (flt_type != HCI_FLT_CLEAR_ALL) {
3581 cp.cond_type = cond_type;
3582 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3583 cp.addr_conn_flt.auto_accept = auto_accept;
3586 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3587 flt_type == HCI_FLT_CLEAR_ALL ?
3588 sizeof(cp.flt_type) : sizeof(cp), &cp,
3592 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3594 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3597 /* In theory the state machine should not reach here unless
3598 * a hci_set_event_filter_sync() call succeeds, but we do
3599 * the check both for parity and as a future reminder.
3601 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3604 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3608 /* Connection accept timeout ~20 secs */
3609 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3611 __le16 param = cpu_to_le16(0x7d00);
3613 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3614 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
3617 /* BR Controller init stage 2 command sequence */
3618 static const struct hci_init_stage br_init2[] = {
3619 /* HCI_OP_READ_BUFFER_SIZE */
3620 HCI_INIT(hci_read_buffer_size_sync),
3621 /* HCI_OP_READ_CLASS_OF_DEV */
3622 HCI_INIT(hci_read_dev_class_sync),
3623 /* HCI_OP_READ_LOCAL_NAME */
3624 HCI_INIT(hci_read_local_name_sync),
3625 /* HCI_OP_READ_VOICE_SETTING */
3626 HCI_INIT(hci_read_voice_setting_sync),
3627 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
3628 HCI_INIT(hci_read_num_supported_iac_sync),
3629 /* HCI_OP_READ_CURRENT_IAC_LAP */
3630 HCI_INIT(hci_read_current_iac_lap_sync),
3631 /* HCI_OP_SET_EVENT_FLT */
3632 HCI_INIT(hci_clear_event_filter_sync),
3633 /* HCI_OP_WRITE_CA_TIMEOUT */
3634 HCI_INIT(hci_write_ca_timeout_sync),
3638 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3642 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3645 /* When SSP is available, then the host features page
3646 * should also be available as well. However some
3647 * controllers list the max_page as 0 as long as SSP
3648 * has not been enabled. To achieve proper debugging
3649 * output, force the minimum max_page to 1 at least.
3651 hdev->max_page = 0x01;
3653 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3654 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3657 static int hci_write_eir_sync(struct hci_dev *hdev)
3659 struct hci_cp_write_eir cp;
3661 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3664 memset(hdev->eir, 0, sizeof(hdev->eir));
3665 memset(&cp, 0, sizeof(cp));
3667 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3671 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3675 if (!lmp_inq_rssi_capable(hdev) &&
3676 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3679 /* If Extended Inquiry Result events are supported, then
3680 * they are clearly preferred over Inquiry Result with RSSI
3683 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3685 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3686 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3689 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3691 if (!lmp_inq_tx_pwr_capable(hdev))
3694 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3695 0, NULL, HCI_CMD_TIMEOUT);
3698 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3700 struct hci_cp_read_local_ext_features cp;
3702 if (!lmp_ext_feat_capable(hdev))
3705 memset(&cp, 0, sizeof(cp));
3708 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3709 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3712 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3714 return hci_read_local_ext_features_sync(hdev, 0x01);
3717 /* HCI Controller init stage 2 command sequence */
3718 static const struct hci_init_stage hci_init2[] = {
3719 /* HCI_OP_READ_LOCAL_COMMANDS */
3720 HCI_INIT(hci_read_local_cmds_sync),
3721 /* HCI_OP_WRITE_SSP_MODE */
3722 HCI_INIT(hci_write_ssp_mode_1_sync),
3723 /* HCI_OP_WRITE_EIR */
3724 HCI_INIT(hci_write_eir_sync),
3725 /* HCI_OP_WRITE_INQUIRY_MODE */
3726 HCI_INIT(hci_write_inquiry_mode_sync),
3727 /* HCI_OP_READ_INQ_RSP_TX_POWER */
3728 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3729 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3730 HCI_INIT(hci_read_local_ext_features_1_sync),
3731 /* HCI_OP_WRITE_AUTH_ENABLE */
3732 HCI_INIT(hci_write_auth_enable_sync),
3736 /* Read LE Buffer Size */
3737 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3739 /* Use Read LE Buffer Size V2 if supported */
3740 if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3741 return __hci_cmd_sync_status(hdev,
3742 HCI_OP_LE_READ_BUFFER_SIZE_V2,
3743 0, NULL, HCI_CMD_TIMEOUT);
3745 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3746 0, NULL, HCI_CMD_TIMEOUT);
3749 /* Read LE Local Supported Features */
3750 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3752 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3753 0, NULL, HCI_CMD_TIMEOUT);
3756 /* Read LE Supported States */
3757 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3759 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3760 0, NULL, HCI_CMD_TIMEOUT);
3763 /* LE Controller init stage 2 command sequence */
3764 static const struct hci_init_stage le_init2[] = {
3765 /* HCI_OP_LE_READ_LOCAL_FEATURES */
3766 HCI_INIT(hci_le_read_local_features_sync),
3767 /* HCI_OP_LE_READ_BUFFER_SIZE */
3768 HCI_INIT(hci_le_read_buffer_size_sync),
3769 /* HCI_OP_LE_READ_SUPPORTED_STATES */
3770 HCI_INIT(hci_le_read_supported_states_sync),
3774 static int hci_init2_sync(struct hci_dev *hdev)
3778 bt_dev_dbg(hdev, "");
3780 if (hdev->dev_type == HCI_AMP)
3781 return hci_init_stage_sync(hdev, amp_init2);
3783 err = hci_init_stage_sync(hdev, hci_init2);
3787 if (lmp_bredr_capable(hdev)) {
3788 err = hci_init_stage_sync(hdev, br_init2);
3792 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3795 if (lmp_le_capable(hdev)) {
3796 err = hci_init_stage_sync(hdev, le_init2);
3799 /* LE-only controllers have LE implicitly enabled */
3800 if (!lmp_bredr_capable(hdev))
3801 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3807 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3809 /* The second byte is 0xff instead of 0x9f (two reserved bits
3810 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3811 * command otherwise.
3813 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3815 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3816 * any event mask for pre 1.2 devices.
3818 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3821 if (lmp_bredr_capable(hdev)) {
3822 events[4] |= 0x01; /* Flow Specification Complete */
3824 /* Don't set Disconnect Complete and mode change when
3825 * suspended as that would wakeup the host when disconnecting
3828 if (hdev->suspended) {
3833 /* Use a different default for LE-only devices */
3834 memset(events, 0, sizeof(events));
3835 events[1] |= 0x20; /* Command Complete */
3836 events[1] |= 0x40; /* Command Status */
3837 events[1] |= 0x80; /* Hardware Error */
3839 /* If the controller supports the Disconnect command, enable
3840 * the corresponding event. In addition enable packet flow
3841 * control related events.
3843 if (hdev->commands[0] & 0x20) {
3844 /* Don't set Disconnect Complete when suspended as that
3845 * would wakeup the host when disconnecting due to
3848 if (!hdev->suspended)
3849 events[0] |= 0x10; /* Disconnection Complete */
3850 events[2] |= 0x04; /* Number of Completed Packets */
3851 events[3] |= 0x02; /* Data Buffer Overflow */
3854 /* If the controller supports the Read Remote Version
3855 * Information command, enable the corresponding event.
3857 if (hdev->commands[2] & 0x80)
3858 events[1] |= 0x08; /* Read Remote Version Information
3862 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3863 events[0] |= 0x80; /* Encryption Change */
3864 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3868 if (lmp_inq_rssi_capable(hdev) ||
3869 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3870 events[4] |= 0x02; /* Inquiry Result with RSSI */
3872 if (lmp_ext_feat_capable(hdev))
3873 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3875 if (lmp_esco_capable(hdev)) {
3876 events[5] |= 0x08; /* Synchronous Connection Complete */
3877 events[5] |= 0x10; /* Synchronous Connection Changed */
3880 if (lmp_sniffsubr_capable(hdev))
3881 events[5] |= 0x20; /* Sniff Subrating */
3883 if (lmp_pause_enc_capable(hdev))
3884 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3886 if (lmp_ext_inq_capable(hdev))
3887 events[5] |= 0x40; /* Extended Inquiry Result */
3889 if (lmp_no_flush_capable(hdev))
3890 events[7] |= 0x01; /* Enhanced Flush Complete */
3892 if (lmp_lsto_capable(hdev))
3893 events[6] |= 0x80; /* Link Supervision Timeout Changed */
3895 if (lmp_ssp_capable(hdev)) {
3896 events[6] |= 0x01; /* IO Capability Request */
3897 events[6] |= 0x02; /* IO Capability Response */
3898 events[6] |= 0x04; /* User Confirmation Request */
3899 events[6] |= 0x08; /* User Passkey Request */
3900 events[6] |= 0x10; /* Remote OOB Data Request */
3901 events[6] |= 0x20; /* Simple Pairing Complete */
3902 events[7] |= 0x04; /* User Passkey Notification */
3903 events[7] |= 0x08; /* Keypress Notification */
3904 events[7] |= 0x10; /* Remote Host Supported
3905 * Features Notification
3909 if (lmp_le_capable(hdev))
3910 events[7] |= 0x20; /* LE Meta-Event */
3912 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3913 sizeof(events), events, HCI_CMD_TIMEOUT);
3916 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3918 struct hci_cp_read_stored_link_key cp;
3920 if (!(hdev->commands[6] & 0x20) ||
3921 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3924 memset(&cp, 0, sizeof(cp));
3925 bacpy(&cp.bdaddr, BDADDR_ANY);
3928 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3929 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3932 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3934 struct hci_cp_write_def_link_policy cp;
3935 u16 link_policy = 0;
3937 if (!(hdev->commands[5] & 0x10))
3940 memset(&cp, 0, sizeof(cp));
3942 if (lmp_rswitch_capable(hdev))
3943 link_policy |= HCI_LP_RSWITCH;
3944 if (lmp_hold_capable(hdev))
3945 link_policy |= HCI_LP_HOLD;
3946 if (lmp_sniff_capable(hdev))
3947 link_policy |= HCI_LP_SNIFF;
3948 if (lmp_park_capable(hdev))
3949 link_policy |= HCI_LP_PARK;
3951 cp.policy = cpu_to_le16(link_policy);
3953 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3954 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3957 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3959 if (!(hdev->commands[8] & 0x01))
3962 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3963 0, NULL, HCI_CMD_TIMEOUT);
3966 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3968 if (!(hdev->commands[18] & 0x04) ||
3969 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
3970 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3973 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3974 0, NULL, HCI_CMD_TIMEOUT);
3977 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3979 /* Some older Broadcom based Bluetooth 1.2 controllers do not
3980 * support the Read Page Scan Type command. Check support for
3981 * this command in the bit mask of supported commands.
3983 if (!(hdev->commands[13] & 0x01))
3986 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3987 0, NULL, HCI_CMD_TIMEOUT);
3990 /* Read features beyond page 1 if available */
3991 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3996 if (!lmp_ext_feat_capable(hdev))
3999 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
4001 err = hci_read_local_ext_features_sync(hdev, page);
4009 /* HCI Controller init stage 3 command sequence */
4010 static const struct hci_init_stage hci_init3[] = {
4011 /* HCI_OP_SET_EVENT_MASK */
4012 HCI_INIT(hci_set_event_mask_sync),
4013 /* HCI_OP_READ_STORED_LINK_KEY */
4014 HCI_INIT(hci_read_stored_link_key_sync),
4015 /* HCI_OP_WRITE_DEF_LINK_POLICY */
4016 HCI_INIT(hci_setup_link_policy_sync),
4017 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
4018 HCI_INIT(hci_read_page_scan_activity_sync),
4019 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
4020 HCI_INIT(hci_read_def_err_data_reporting_sync),
4021 /* HCI_OP_READ_PAGE_SCAN_TYPE */
4022 HCI_INIT(hci_read_page_scan_type_sync),
4023 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
4024 HCI_INIT(hci_read_local_ext_features_all_sync),
4028 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4032 if (!lmp_le_capable(hdev))
4035 memset(events, 0, sizeof(events));
4037 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4038 events[0] |= 0x10; /* LE Long Term Key Request */
4040 /* If controller supports the Connection Parameters Request
4041 * Link Layer Procedure, enable the corresponding event.
4043 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4044 /* LE Remote Connection Parameter Request */
4047 /* If the controller supports the Data Length Extension
4048 * feature, enable the corresponding event.
4050 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4051 events[0] |= 0x40; /* LE Data Length Change */
4053 /* If the controller supports LL Privacy feature or LE Extended Adv,
4054 * enable the corresponding event.
4056 if (use_enhanced_conn_complete(hdev))
4057 events[1] |= 0x02; /* LE Enhanced Connection Complete */
4059 /* If the controller supports Extended Scanner Filter
4060 * Policies, enable the corresponding event.
4062 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4063 events[1] |= 0x04; /* LE Direct Advertising Report */
4065 /* If the controller supports Channel Selection Algorithm #2
4066 * feature, enable the corresponding event.
4068 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4069 events[2] |= 0x08; /* LE Channel Selection Algorithm */
4071 /* If the controller supports the LE Set Scan Enable command,
4072 * enable the corresponding advertising report event.
4074 if (hdev->commands[26] & 0x08)
4075 events[0] |= 0x02; /* LE Advertising Report */
4077 /* If the controller supports the LE Create Connection
4078 * command, enable the corresponding event.
4080 if (hdev->commands[26] & 0x10)
4081 events[0] |= 0x01; /* LE Connection Complete */
4083 /* If the controller supports the LE Connection Update
4084 * command, enable the corresponding event.
4086 if (hdev->commands[27] & 0x04)
4087 events[0] |= 0x04; /* LE Connection Update Complete */
4089 /* If the controller supports the LE Read Remote Used Features
4090 * command, enable the corresponding event.
4092 if (hdev->commands[27] & 0x20)
4093 /* LE Read Remote Used Features Complete */
4096 /* If the controller supports the LE Read Local P-256
4097 * Public Key command, enable the corresponding event.
4099 if (hdev->commands[34] & 0x02)
4100 /* LE Read Local P-256 Public Key Complete */
4103 /* If the controller supports the LE Generate DHKey
4104 * command, enable the corresponding event.
4106 if (hdev->commands[34] & 0x04)
4107 events[1] |= 0x01; /* LE Generate DHKey Complete */
4109 /* If the controller supports the LE Set Default PHY or
4110 * LE Set PHY commands, enable the corresponding event.
4112 if (hdev->commands[35] & (0x20 | 0x40))
4113 events[1] |= 0x08; /* LE PHY Update Complete */
4115 /* If the controller supports LE Set Extended Scan Parameters
4116 * and LE Set Extended Scan Enable commands, enable the
4117 * corresponding event.
4119 if (use_ext_scan(hdev))
4120 events[1] |= 0x10; /* LE Extended Advertising Report */
4122 /* If the controller supports the LE Extended Advertising
4123 * command, enable the corresponding event.
4125 if (ext_adv_capable(hdev))
4126 events[2] |= 0x02; /* LE Advertising Set Terminated */
4128 if (cis_capable(hdev)) {
4129 events[3] |= 0x01; /* LE CIS Established */
4130 if (cis_peripheral_capable(hdev))
4131 events[3] |= 0x02; /* LE CIS Request */
4134 if (bis_capable(hdev)) {
4135 events[1] |= 0x20; /* LE PA Report */
4136 events[1] |= 0x40; /* LE PA Sync Established */
4137 events[3] |= 0x04; /* LE Create BIG Complete */
4138 events[3] |= 0x08; /* LE Terminate BIG Complete */
4139 events[3] |= 0x10; /* LE BIG Sync Established */
4140 events[3] |= 0x20; /* LE BIG Sync Loss */
4141 events[4] |= 0x02; /* LE BIG Info Advertising Report */
4144 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4145 sizeof(events), events, HCI_CMD_TIMEOUT);
4148 /* Read LE Advertising Channel TX Power */
4149 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4151 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4152 /* HCI TS spec forbids mixing of legacy and extended
4153 * advertising commands wherein READ_ADV_TX_POWER is
4154 * also included. So do not call it if extended adv
4155 * is supported otherwise controller will return
4156 * COMMAND_DISALLOWED for extended commands.
4158 return __hci_cmd_sync_status(hdev,
4159 HCI_OP_LE_READ_ADV_TX_POWER,
4160 0, NULL, HCI_CMD_TIMEOUT);
4166 /* Read LE Min/Max Tx Power*/
4167 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4169 if (!(hdev->commands[38] & 0x80) ||
4170 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4173 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4174 0, NULL, HCI_CMD_TIMEOUT);
4177 /* Read LE Accept List Size */
4178 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4180 if (!(hdev->commands[26] & 0x40))
4183 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4184 0, NULL, HCI_CMD_TIMEOUT);
4187 /* Clear LE Accept List */
4188 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
4190 if (!(hdev->commands[26] & 0x80))
4193 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
4197 /* Read LE Resolving List Size */
4198 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4200 if (!(hdev->commands[34] & 0x40))
4203 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4204 0, NULL, HCI_CMD_TIMEOUT);
4207 /* Clear LE Resolving List */
4208 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4210 if (!(hdev->commands[34] & 0x20))
4213 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4217 /* Set RPA timeout */
4218 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4220 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4222 if (!(hdev->commands[35] & 0x04) ||
4223 test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4226 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4227 sizeof(timeout), &timeout,
4231 /* Read LE Maximum Data Length */
4232 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4234 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4237 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4241 /* Read LE Suggested Default Data Length */
4242 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4244 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4247 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4251 /* Read LE Number of Supported Advertising Sets */
4252 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4254 if (!ext_adv_capable(hdev))
4257 return __hci_cmd_sync_status(hdev,
4258 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4259 0, NULL, HCI_CMD_TIMEOUT);
4262 /* Write LE Host Supported */
4263 static int hci_set_le_support_sync(struct hci_dev *hdev)
4265 struct hci_cp_write_le_host_supported cp;
4267 /* LE-only devices do not support explicit enablement */
4268 if (!lmp_bredr_capable(hdev))
4271 memset(&cp, 0, sizeof(cp));
4273 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4278 if (cp.le == lmp_host_le_capable(hdev))
4281 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4282 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4285 /* LE Set Host Feature */
4286 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4288 struct hci_cp_le_set_host_feature cp;
4290 if (!iso_capable(hdev))
4293 memset(&cp, 0, sizeof(cp));
4295 /* Isochronous Channels (Host Support) */
4299 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4300 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4303 /* LE Controller init stage 3 command sequence */
4304 static const struct hci_init_stage le_init3[] = {
4305 /* HCI_OP_LE_SET_EVENT_MASK */
4306 HCI_INIT(hci_le_set_event_mask_sync),
4307 /* HCI_OP_LE_READ_ADV_TX_POWER */
4308 HCI_INIT(hci_le_read_adv_tx_power_sync),
4309 /* HCI_OP_LE_READ_TRANSMIT_POWER */
4310 HCI_INIT(hci_le_read_tx_power_sync),
4311 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4312 HCI_INIT(hci_le_read_accept_list_size_sync),
4313 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4314 HCI_INIT(hci_le_clear_accept_list_sync),
4315 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4316 HCI_INIT(hci_le_read_resolv_list_size_sync),
4317 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
4318 HCI_INIT(hci_le_clear_resolv_list_sync),
4319 /* HCI_OP_LE_SET_RPA_TIMEOUT */
4320 HCI_INIT(hci_le_set_rpa_timeout_sync),
4321 /* HCI_OP_LE_READ_MAX_DATA_LEN */
4322 HCI_INIT(hci_le_read_max_data_len_sync),
4323 /* HCI_OP_LE_READ_DEF_DATA_LEN */
4324 HCI_INIT(hci_le_read_def_data_len_sync),
4325 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4326 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4327 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4328 HCI_INIT(hci_set_le_support_sync),
4329 /* HCI_OP_LE_SET_HOST_FEATURE */
4330 HCI_INIT(hci_le_set_host_feature_sync),
4334 static int hci_init3_sync(struct hci_dev *hdev)
4338 bt_dev_dbg(hdev, "");
4340 err = hci_init_stage_sync(hdev, hci_init3);
4344 if (lmp_le_capable(hdev))
4345 return hci_init_stage_sync(hdev, le_init3);
4350 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4352 struct hci_cp_delete_stored_link_key cp;
4354 /* Some Broadcom based Bluetooth controllers do not support the
4355 * Delete Stored Link Key command. They are clearly indicating its
4356 * absence in the bit mask of supported commands.
4358 * Check the supported commands and only if the command is marked
4359 * as supported send it. If not supported assume that the controller
4360 * does not have actual support for stored link keys which makes this
4361 * command redundant anyway.
4363 * Some controllers indicate that they support handling deleting
4364 * stored link keys, but they don't. The quirk lets a driver
4365 * just disable this command.
4367 if (!(hdev->commands[6] & 0x80) ||
4368 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4371 memset(&cp, 0, sizeof(cp));
4372 bacpy(&cp.bdaddr, BDADDR_ANY);
4373 cp.delete_all = 0x01;
4375 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4376 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4379 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4381 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4382 bool changed = false;
4384 /* Set event mask page 2 if the HCI command for it is supported */
4385 if (!(hdev->commands[22] & 0x04))
4388 /* If Connectionless Peripheral Broadcast central role is supported
4389 * enable all necessary events for it.
4391 if (lmp_cpb_central_capable(hdev)) {
4392 events[1] |= 0x40; /* Triggered Clock Capture */
4393 events[1] |= 0x80; /* Synchronization Train Complete */
4394 events[2] |= 0x08; /* Truncated Page Complete */
4395 events[2] |= 0x20; /* CPB Channel Map Change */
4399 /* If Connectionless Peripheral Broadcast peripheral role is supported
4400 * enable all necessary events for it.
4402 if (lmp_cpb_peripheral_capable(hdev)) {
4403 events[2] |= 0x01; /* Synchronization Train Received */
4404 events[2] |= 0x02; /* CPB Receive */
4405 events[2] |= 0x04; /* CPB Timeout */
4406 events[2] |= 0x10; /* Peripheral Page Response Timeout */
4410 /* Enable Authenticated Payload Timeout Expired event if supported */
4411 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4416 /* Some Broadcom based controllers indicate support for Set Event
4417 * Mask Page 2 command, but then actually do not support it. Since
4418 * the default value is all bits set to zero, the command is only
4419 * required if the event mask has to be changed. In case no change
4420 * to the event mask is needed, skip this command.
4425 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4426 sizeof(events), events, HCI_CMD_TIMEOUT);
4429 /* Read local codec list if the HCI command is supported */
4430 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4432 if (hdev->commands[45] & 0x04)
4433 hci_read_supported_codecs_v2(hdev);
4434 else if (hdev->commands[29] & 0x20)
4435 hci_read_supported_codecs(hdev);
4440 /* Read local pairing options if the HCI command is supported */
4441 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4443 if (!(hdev->commands[41] & 0x08))
4446 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4447 0, NULL, HCI_CMD_TIMEOUT);
4450 /* Get MWS transport configuration if the HCI command is supported */
4451 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4453 if (!mws_transport_config_capable(hdev))
4456 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4457 0, NULL, HCI_CMD_TIMEOUT);
4460 /* Check for Synchronization Train support */
4461 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4463 if (!lmp_sync_train_capable(hdev))
4466 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4467 0, NULL, HCI_CMD_TIMEOUT);
4470 /* Enable Secure Connections if supported and configured */
4471 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4475 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4476 !bredr_sc_enabled(hdev))
4479 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4480 sizeof(support), &support,
4484 /* Set erroneous data reporting if supported to the wideband speech
4487 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4489 struct hci_cp_write_def_err_data_reporting cp;
4490 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4492 if (!(hdev->commands[18] & 0x08) ||
4493 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4494 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4497 if (enabled == hdev->err_data_reporting)
4500 memset(&cp, 0, sizeof(cp));
4501 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4502 ERR_DATA_REPORTING_DISABLED;
4504 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4505 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4508 static const struct hci_init_stage hci_init4[] = {
4509 /* HCI_OP_DELETE_STORED_LINK_KEY */
4510 HCI_INIT(hci_delete_stored_link_key_sync),
4511 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4512 HCI_INIT(hci_set_event_mask_page_2_sync),
4513 /* HCI_OP_READ_LOCAL_CODECS */
4514 HCI_INIT(hci_read_local_codecs_sync),
4515 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4516 HCI_INIT(hci_read_local_pairing_opts_sync),
4517 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4518 HCI_INIT(hci_get_mws_transport_config_sync),
4519 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4520 HCI_INIT(hci_read_sync_train_params_sync),
4521 /* HCI_OP_WRITE_SC_SUPPORT */
4522 HCI_INIT(hci_write_sc_support_1_sync),
4523 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4524 HCI_INIT(hci_set_err_data_report_sync),
4528 /* Set Suggested Default Data Length to maximum if supported */
4529 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4531 struct hci_cp_le_write_def_data_len cp;
4533 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4536 memset(&cp, 0, sizeof(cp));
4537 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4538 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4540 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4541 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4544 /* Set Default PHY parameters if command is supported, enables all supported
4545 * PHYs according to the LE Features bits.
4547 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4549 struct hci_cp_le_set_default_phy cp;
4551 if (!(hdev->commands[35] & 0x20)) {
4552 /* If the command is not supported it means only 1M PHY is
4555 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4556 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4560 memset(&cp, 0, sizeof(cp));
4562 cp.tx_phys = HCI_LE_SET_PHY_1M;
4563 cp.rx_phys = HCI_LE_SET_PHY_1M;
4565 /* Enables 2M PHY if supported */
4566 if (le_2m_capable(hdev)) {
4567 cp.tx_phys |= HCI_LE_SET_PHY_2M;
4568 cp.rx_phys |= HCI_LE_SET_PHY_2M;
4571 /* Enables Coded PHY if supported */
4572 if (le_coded_capable(hdev)) {
4573 cp.tx_phys |= HCI_LE_SET_PHY_CODED;
4574 cp.rx_phys |= HCI_LE_SET_PHY_CODED;
4577 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4578 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4581 static const struct hci_init_stage le_init4[] = {
4582 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4583 HCI_INIT(hci_le_set_write_def_data_len_sync),
4584 /* HCI_OP_LE_SET_DEFAULT_PHY */
4585 HCI_INIT(hci_le_set_default_phy_sync),
4589 static int hci_init4_sync(struct hci_dev *hdev)
4593 bt_dev_dbg(hdev, "");
4595 err = hci_init_stage_sync(hdev, hci_init4);
4599 if (lmp_le_capable(hdev))
4600 return hci_init_stage_sync(hdev, le_init4);
4605 static int hci_init_sync(struct hci_dev *hdev)
4609 err = hci_init1_sync(hdev);
4613 if (hci_dev_test_flag(hdev, HCI_SETUP))
4614 hci_debugfs_create_basic(hdev);
4616 err = hci_init2_sync(hdev);
4620 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
4621 * BR/EDR/LE type controllers. AMP controllers only need the
4622 * first two stages of init.
4624 if (hdev->dev_type != HCI_PRIMARY)
4627 err = hci_init3_sync(hdev);
4631 err = hci_init4_sync(hdev);
4635 /* This function is only called when the controller is actually in
4636 * configured state. When the controller is marked as unconfigured,
4637 * this initialization procedure is not run.
4639 * It means that it is possible that a controller runs through its
4640 * setup phase and then discovers missing settings. If that is the
4641 * case, then this function will not be called. It then will only
4642 * be called during the config phase.
4644 * So only when in setup phase or config phase, create the debugfs
4645 * entries and register the SMP channels.
4647 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4648 !hci_dev_test_flag(hdev, HCI_CONFIG))
4651 if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4654 hci_debugfs_create_common(hdev);
4656 if (lmp_bredr_capable(hdev))
4657 hci_debugfs_create_bredr(hdev);
4659 if (lmp_le_capable(hdev))
4660 hci_debugfs_create_le(hdev);
4665 #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
4667 static const struct {
4668 unsigned long quirk;
4670 } hci_broken_table[] = {
4671 HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
4672 "HCI Read Local Supported Commands not supported"),
4673 HCI_QUIRK_BROKEN(STORED_LINK_KEY,
4674 "HCI Delete Stored Link Key command is advertised, "
4675 "but not supported."),
4676 HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
4677 "HCI Read Default Erroneous Data Reporting command is "
4678 "advertised, but not supported."),
4679 HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
4680 "HCI Read Transmit Power Level command is advertised, "
4681 "but not supported."),
4682 HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
4683 "HCI Set Event Filter command not supported."),
4684 HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
4685 "HCI Enhanced Setup Synchronous Connection command is "
4686 "advertised, but not supported."),
4687 HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
4688 "HCI LE Set Random Private Address Timeout command is "
4689 "advertised, but not supported."),
4690 HCI_QUIRK_BROKEN(LE_CODED,
4691 "HCI LE Coded PHY feature bit is set, "
4692 "but its usage is not supported.")
4695 /* This function handles hdev setup stage:
4698 * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
4700 static int hci_dev_setup_sync(struct hci_dev *hdev)
4703 bool invalid_bdaddr;
4706 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4707 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4710 bt_dev_dbg(hdev, "");
4712 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4715 ret = hdev->setup(hdev);
4717 for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
4718 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4719 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4722 /* The transport driver can set the quirk to mark the
4723 * BD_ADDR invalid before creating the HCI device or in
4724 * its setup callback.
4726 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
4727 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
4729 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4730 !bacmp(&hdev->public_addr, BDADDR_ANY))
4731 hci_dev_get_bd_addr_from_property(hdev);
4733 if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
4735 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4737 invalid_bdaddr = false;
4741 /* The transport driver can set these quirks before
4742 * creating the HCI device or in its setup callback.
4744 * For the invalid BD_ADDR quirk it is possible that
4745 * it becomes a valid address if the bootloader does
4746 * provide it (see above).
4748 * In case any of them is set, the controller has to
4749 * start up as unconfigured.
4751 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4753 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4755 /* For an unconfigured controller it is required to
4756 * read at least the version information provided by
4757 * the Read Local Version Information command.
4759 * If the set_bdaddr driver callback is provided, then
4760 * also the original Bluetooth public device address
4761 * will be read using the Read BD Address command.
4763 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4764 return hci_unconf_init_sync(hdev);
4769 /* This function handles hdev init stage:
4771 * Calls hci_dev_setup_sync to perform setup stage
4772 * Calls hci_init_sync to perform HCI command init sequence
4774 static int hci_dev_init_sync(struct hci_dev *hdev)
4778 bt_dev_dbg(hdev, "");
4780 atomic_set(&hdev->cmd_cnt, 1);
4781 set_bit(HCI_INIT, &hdev->flags);
4783 ret = hci_dev_setup_sync(hdev);
4785 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4786 /* If public address change is configured, ensure that
4787 * the address gets programmed. If the driver does not
4788 * support changing the public address, fail the power
4791 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4793 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4795 ret = -EADDRNOTAVAIL;
4799 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4800 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4801 ret = hci_init_sync(hdev);
4802 if (!ret && hdev->post_init)
4803 ret = hdev->post_init(hdev);
4807 /* If the HCI Reset command is clearing all diagnostic settings,
4808 * then they need to be reprogrammed after the init procedure
4811 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4812 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4813 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4814 ret = hdev->set_diag(hdev, true);
4816 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4821 clear_bit(HCI_INIT, &hdev->flags);
4826 int hci_dev_open_sync(struct hci_dev *hdev)
4830 bt_dev_dbg(hdev, "");
4832 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4837 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4838 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4839 /* Check for rfkill but allow the HCI setup stage to
4840 * proceed (which in itself doesn't cause any RF activity).
4842 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4847 /* Check for valid public address or a configured static
4848 * random address, but let the HCI setup proceed to
4849 * be able to determine if there is a public address
4852 * In case of user channel usage, it is not important
4853 * if a public address or static random address is
4856 * This check is only valid for BR/EDR controllers
4857 * since AMP controllers do not have an address.
4859 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4860 hdev->dev_type == HCI_PRIMARY &&
4861 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4862 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4863 ret = -EADDRNOTAVAIL;
4868 if (test_bit(HCI_UP, &hdev->flags)) {
4873 if (hdev->open(hdev)) {
4878 hci_devcd_reset(hdev);
4880 set_bit(HCI_RUNNING, &hdev->flags);
4881 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4883 ret = hci_dev_init_sync(hdev);
4886 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4887 hci_adv_instances_set_rpa_expired(hdev, true);
4888 set_bit(HCI_UP, &hdev->flags);
4889 hci_sock_dev_event(hdev, HCI_DEV_UP);
4890 hci_leds_update_powered(hdev, true);
4891 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4892 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4893 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4894 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4895 hci_dev_test_flag(hdev, HCI_MGMT) &&
4896 hdev->dev_type == HCI_PRIMARY) {
4897 ret = hci_powered_update_sync(hdev);
4898 mgmt_power_on(hdev, ret);
4901 /* Init failed, cleanup */
4902 flush_work(&hdev->tx_work);
4904 /* Since hci_rx_work() is possible to awake new cmd_work
4905 * it should be flushed first to avoid unexpected call of
4908 flush_work(&hdev->rx_work);
4909 flush_work(&hdev->cmd_work);
4911 skb_queue_purge(&hdev->cmd_q);
4912 skb_queue_purge(&hdev->rx_q);
4917 if (hdev->sent_cmd) {
4918 cancel_delayed_work_sync(&hdev->cmd_timer);
4919 kfree_skb(hdev->sent_cmd);
4920 hdev->sent_cmd = NULL;
4923 clear_bit(HCI_RUNNING, &hdev->flags);
4924 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4927 hdev->flags &= BIT(HCI_RAW);
4934 /* This function requires the caller holds hdev->lock */
4935 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4937 struct hci_conn_params *p;
4939 list_for_each_entry(p, &hdev->le_conn_params, list) {
4940 hci_pend_le_list_del_init(p);
4942 hci_conn_drop(p->conn);
4943 hci_conn_put(p->conn);
4948 BT_DBG("All LE pending actions cleared");
4951 static int hci_dev_shutdown(struct hci_dev *hdev)
4954 /* Similar to how we first do setup and then set the exclusive access
4955 * bit for userspace, we must first unset userchannel and then clean up.
4956 * Otherwise, the kernel can't properly use the hci channel to clean up
4957 * the controller (some shutdown routines require sending additional
4958 * commands to the controller for example).
4960 bool was_userchannel =
4961 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
4963 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4964 test_bit(HCI_UP, &hdev->flags)) {
4965 /* Execute vendor specific shutdown routine */
4967 err = hdev->shutdown(hdev);
4970 if (was_userchannel)
4971 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
4976 int hci_dev_close_sync(struct hci_dev *hdev)
4981 bt_dev_dbg(hdev, "");
4983 cancel_delayed_work(&hdev->power_off);
4984 cancel_delayed_work(&hdev->ncmd_timer);
4985 cancel_delayed_work(&hdev->le_scan_disable);
4986 cancel_delayed_work(&hdev->le_scan_restart);
4988 hci_request_cancel_all(hdev);
4990 if (hdev->adv_instance_timeout) {
4991 cancel_delayed_work_sync(&hdev->adv_instance_expire);
4992 hdev->adv_instance_timeout = 0;
4995 err = hci_dev_shutdown(hdev);
4997 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4998 cancel_delayed_work_sync(&hdev->cmd_timer);
5002 hci_leds_update_powered(hdev, false);
5004 /* Flush RX and TX works */
5005 flush_work(&hdev->tx_work);
5006 flush_work(&hdev->rx_work);
5008 if (hdev->discov_timeout > 0) {
5009 hdev->discov_timeout = 0;
5010 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5011 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
5014 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
5015 cancel_delayed_work(&hdev->service_cache);
5017 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
5018 struct adv_info *adv_instance;
5020 cancel_delayed_work_sync(&hdev->rpa_expired);
5022 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5023 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
5026 /* Avoid potential lockdep warnings from the *_flush() calls by
5027 * ensuring the workqueue is empty up front.
5029 drain_workqueue(hdev->workqueue);
5033 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5035 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5037 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
5038 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5039 hci_dev_test_flag(hdev, HCI_MGMT))
5040 __mgmt_power_off(hdev);
5042 hci_inquiry_cache_flush(hdev);
5043 hci_pend_le_actions_clear(hdev);
5044 hci_conn_hash_flush(hdev);
5045 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5046 smp_unregister(hdev);
5047 hci_dev_unlock(hdev);
5049 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5051 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5052 aosp_do_close(hdev);
5053 msft_do_close(hdev);
5060 skb_queue_purge(&hdev->cmd_q);
5061 atomic_set(&hdev->cmd_cnt, 1);
5062 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5063 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5064 set_bit(HCI_INIT, &hdev->flags);
5065 hci_reset_sync(hdev);
5066 clear_bit(HCI_INIT, &hdev->flags);
5069 /* flush cmd work */
5070 flush_work(&hdev->cmd_work);
5073 skb_queue_purge(&hdev->rx_q);
5074 skb_queue_purge(&hdev->cmd_q);
5075 skb_queue_purge(&hdev->raw_q);
5077 /* Drop last sent command */
5078 if (hdev->sent_cmd) {
5079 cancel_delayed_work_sync(&hdev->cmd_timer);
5080 kfree_skb(hdev->sent_cmd);
5081 hdev->sent_cmd = NULL;
5084 clear_bit(HCI_RUNNING, &hdev->flags);
5085 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5087 /* After this point our queues are empty and no tasks are scheduled. */
5091 hdev->flags &= BIT(HCI_RAW);
5092 hci_dev_clear_volatile_flags(hdev);
5094 /* Controller radio is available but is currently powered down */
5095 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
5097 memset(hdev->eir, 0, sizeof(hdev->eir));
5098 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5099 bacpy(&hdev->random_addr, BDADDR_ANY);
5100 hci_codec_list_clear(&hdev->local_codecs);
5106 /* This function perform power on HCI command sequence as follows:
5108 * If controller is already up (HCI_UP) performs hci_powered_update_sync
5109 * sequence otherwise run hci_dev_open_sync which will follow with
5110 * hci_powered_update_sync after the init sequence is completed.
5112 static int hci_power_on_sync(struct hci_dev *hdev)
5116 if (test_bit(HCI_UP, &hdev->flags) &&
5117 hci_dev_test_flag(hdev, HCI_MGMT) &&
5118 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5119 cancel_delayed_work(&hdev->power_off);
5120 return hci_powered_update_sync(hdev);
5123 err = hci_dev_open_sync(hdev);
5127 /* During the HCI setup phase, a few error conditions are
5128 * ignored and they need to be checked now. If they are still
5129 * valid, it is important to return the device back off.
5131 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5132 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5133 (hdev->dev_type == HCI_PRIMARY &&
5134 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5135 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5136 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5137 hci_dev_close_sync(hdev);
5138 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5139 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5140 HCI_AUTO_OFF_TIMEOUT);
5143 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5144 /* For unconfigured devices, set the HCI_RAW flag
5145 * so that userspace can easily identify them.
5147 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5148 set_bit(HCI_RAW, &hdev->flags);
5150 /* For fully configured devices, this will send
5151 * the Index Added event. For unconfigured devices,
5152 * it will send Unconfigued Index Added event.
5154 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
5155 * and no event will be send.
5157 mgmt_index_added(hdev);
5158 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5159 /* When the controller is now configured, then it
5160 * is important to clear the HCI_RAW flag.
5162 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5163 clear_bit(HCI_RAW, &hdev->flags);
5165 /* Powering on the controller with HCI_CONFIG set only
5166 * happens with the transition from unconfigured to
5167 * configured. This will send the Index Added event.
5169 mgmt_index_added(hdev);
5175 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5177 struct hci_cp_remote_name_req_cancel cp;
5179 memset(&cp, 0, sizeof(cp));
5180 bacpy(&cp.bdaddr, addr);
5182 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5183 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5186 int hci_stop_discovery_sync(struct hci_dev *hdev)
5188 struct discovery_state *d = &hdev->discovery;
5189 struct inquiry_entry *e;
5192 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5194 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
5195 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5196 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5197 0, NULL, HCI_CMD_TIMEOUT);
5202 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5203 cancel_delayed_work(&hdev->le_scan_disable);
5204 cancel_delayed_work(&hdev->le_scan_restart);
5206 err = hci_scan_disable_sync(hdev);
5212 err = hci_scan_disable_sync(hdev);
5217 /* Resume advertising if it was paused */
5218 if (use_ll_privacy(hdev))
5219 hci_resume_advertising_sync(hdev);
5221 /* No further actions needed for LE-only discovery */
5222 if (d->type == DISCOV_TYPE_LE)
5225 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5226 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5231 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5237 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
5240 struct hci_cp_disconn_phy_link cp;
5242 memset(&cp, 0, sizeof(cp));
5243 cp.phy_handle = HCI_PHY_HANDLE(handle);
5246 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
5247 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5250 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5253 struct hci_cp_disconnect cp;
5255 if (conn->type == AMP_LINK)
5256 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
5258 memset(&cp, 0, sizeof(cp));
5259 cp.handle = cpu_to_le16(conn->handle);
5262 /* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5263 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5264 * used when suspending or powering off, where we don't want to wait
5265 * for the peer's response.
5267 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5268 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5270 HCI_EV_DISCONN_COMPLETE,
5271 HCI_CMD_TIMEOUT, NULL);
5273 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5277 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5278 struct hci_conn *conn, u8 reason)
5280 /* Return reason if scanning since the connection shall probably be
5283 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5286 if (conn->role == HCI_ROLE_SLAVE ||
5287 test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
5290 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5291 0, NULL, HCI_CMD_TIMEOUT);
5294 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5297 if (conn->type == LE_LINK)
5298 return hci_le_connect_cancel_sync(hdev, conn, reason);
5300 if (conn->type == ISO_LINK) {
5301 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
5304 * If this command is issued for a CIS on the Central and the
5305 * CIS is successfully terminated before being established,
5306 * then an HCI_LE_CIS_Established event shall also be sent for
5307 * this CIS with the Status Operation Cancelled by Host (0x44).
5309 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
5310 return hci_disconnect_sync(hdev, conn, reason);
5312 /* CIS with no Create CIS sent have nothing to cancel */
5313 if (bacmp(&conn->dst, BDADDR_ANY))
5314 return HCI_ERROR_LOCAL_HOST_TERM;
5316 /* There is no way to cancel a BIS without terminating the BIG
5317 * which is done later on connection cleanup.
5322 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5325 /* Wait for HCI_EV_CONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5326 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5327 * used when suspending or powering off, where we don't want to wait
5328 * for the peer's response.
5330 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5331 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5333 HCI_EV_CONN_COMPLETE,
5334 HCI_CMD_TIMEOUT, NULL);
5336 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5337 6, &conn->dst, HCI_CMD_TIMEOUT);
5340 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5343 struct hci_cp_reject_sync_conn_req cp;
5345 memset(&cp, 0, sizeof(cp));
5346 bacpy(&cp.bdaddr, &conn->dst);
5349 /* SCO rejection has its own limited set of
5350 * allowed error values (0x0D-0x0F).
5352 if (reason < 0x0d || reason > 0x0f)
5353 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5355 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5356 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5359 static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
5362 struct hci_cp_le_reject_cis cp;
5364 memset(&cp, 0, sizeof(cp));
5365 cp.handle = cpu_to_le16(conn->handle);
5368 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
5369 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5372 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5375 struct hci_cp_reject_conn_req cp;
5377 if (conn->type == ISO_LINK)
5378 return hci_le_reject_cis_sync(hdev, conn, reason);
5380 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5381 return hci_reject_sco_sync(hdev, conn, reason);
5383 memset(&cp, 0, sizeof(cp));
5384 bacpy(&cp.bdaddr, &conn->dst);
5387 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5388 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5391 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5394 u16 handle = conn->handle;
5395 bool disconnect = false;
5398 switch (conn->state) {
5401 err = hci_disconnect_sync(hdev, conn, reason);
5404 err = hci_connect_cancel_sync(hdev, conn, reason);
5407 err = hci_reject_conn_sync(hdev, conn, reason);
5412 /* Cleanup bis or pa sync connections */
5413 if (test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags) ||
5414 test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags)) {
5415 hci_conn_failed(conn, reason);
5416 } else if (test_bit(HCI_CONN_PA_SYNC, &conn->flags) ||
5417 test_bit(HCI_CONN_BIG_SYNC, &conn->flags)) {
5418 conn->state = BT_CLOSED;
5419 hci_disconn_cfm(conn, reason);
5423 hci_dev_unlock(hdev);
5434 /* Check if the connection has been cleaned up concurrently */
5435 c = hci_conn_hash_lookup_handle(hdev, handle);
5436 if (!c || c != conn) {
5441 /* Cleanup hci_conn object if it cannot be cancelled as it
5442 * likelly means the controller and host stack are out of sync
5443 * or in case of LE it was still scanning so it can be cleanup
5447 conn->state = BT_CLOSED;
5448 hci_disconn_cfm(conn, reason);
5451 hci_conn_failed(conn, reason);
5455 hci_dev_unlock(hdev);
5459 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5461 struct list_head *head = &hdev->conn_hash.list;
5462 struct hci_conn *conn;
5465 while ((conn = list_first_or_null_rcu(head, struct hci_conn, list))) {
5466 /* Make sure the connection is not freed while unlocking */
5467 conn = hci_conn_get(conn);
5469 /* Disregard possible errors since hci_conn_del shall have been
5470 * called even in case of errors had occurred since it would
5471 * then cause hci_conn_failed to be called which calls
5472 * hci_conn_del internally.
5474 hci_abort_conn_sync(hdev, conn, reason);
5483 /* This function perform power off HCI command sequence as follows:
5487 * Disconnect all connections
5488 * hci_dev_close_sync
5490 static int hci_power_off_sync(struct hci_dev *hdev)
5494 /* If controller is already down there is nothing to do */
5495 if (!test_bit(HCI_UP, &hdev->flags))
5498 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5499 test_bit(HCI_PSCAN, &hdev->flags)) {
5500 err = hci_write_scan_enable_sync(hdev, 0x00);
5505 err = hci_clear_adv_sync(hdev, NULL, false);
5509 err = hci_stop_discovery_sync(hdev);
5513 /* Terminated due to Power Off */
5514 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5518 return hci_dev_close_sync(hdev);
5521 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5524 return hci_power_on_sync(hdev);
5526 return hci_power_off_sync(hdev);
5529 static int hci_write_iac_sync(struct hci_dev *hdev)
5531 struct hci_cp_write_current_iac_lap cp;
5533 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5536 memset(&cp, 0, sizeof(cp));
5538 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5539 /* Limited discoverable mode */
5540 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5541 cp.iac_lap[0] = 0x00; /* LIAC */
5542 cp.iac_lap[1] = 0x8b;
5543 cp.iac_lap[2] = 0x9e;
5544 cp.iac_lap[3] = 0x33; /* GIAC */
5545 cp.iac_lap[4] = 0x8b;
5546 cp.iac_lap[5] = 0x9e;
5548 /* General discoverable mode */
5550 cp.iac_lap[0] = 0x33; /* GIAC */
5551 cp.iac_lap[1] = 0x8b;
5552 cp.iac_lap[2] = 0x9e;
5555 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5556 (cp.num_iac * 3) + 1, &cp,
5560 int hci_update_discoverable_sync(struct hci_dev *hdev)
5564 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5565 err = hci_write_iac_sync(hdev);
5569 err = hci_update_scan_sync(hdev);
5573 err = hci_update_class_sync(hdev);
5578 /* Advertising instances don't use the global discoverable setting, so
5579 * only update AD if advertising was enabled using Set Advertising.
5581 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5582 err = hci_update_adv_data_sync(hdev, 0x00);
5586 /* Discoverable mode affects the local advertising
5587 * address in limited privacy mode.
5589 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5590 if (ext_adv_capable(hdev))
5591 err = hci_start_ext_adv_sync(hdev, 0x00);
5593 err = hci_enable_advertising_sync(hdev);
5600 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5602 return hci_update_discoverable_sync(hdev);
5605 int hci_update_discoverable(struct hci_dev *hdev)
5607 /* Only queue if it would have any effect */
5608 if (hdev_is_powered(hdev) &&
5609 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5610 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5611 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5612 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5618 int hci_update_connectable_sync(struct hci_dev *hdev)
5622 err = hci_update_scan_sync(hdev);
5626 /* If BR/EDR is not enabled and we disable advertising as a
5627 * by-product of disabling connectable, we need to update the
5628 * advertising flags.
5630 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5631 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5633 /* Update the advertising parameters if necessary */
5634 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5635 !list_empty(&hdev->adv_instances)) {
5636 if (ext_adv_capable(hdev))
5637 err = hci_start_ext_adv_sync(hdev,
5638 hdev->cur_adv_instance);
5640 err = hci_enable_advertising_sync(hdev);
5646 return hci_update_passive_scan_sync(hdev);
5649 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5651 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5652 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5653 struct hci_cp_inquiry cp;
5655 bt_dev_dbg(hdev, "");
5657 if (hci_dev_test_flag(hdev, HCI_INQUIRY))
5661 hci_inquiry_cache_flush(hdev);
5662 hci_dev_unlock(hdev);
5664 memset(&cp, 0, sizeof(cp));
5666 if (hdev->discovery.limited)
5667 memcpy(&cp.lap, liac, sizeof(cp.lap));
5669 memcpy(&cp.lap, giac, sizeof(cp.lap));
5673 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5674 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5677 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5680 /* Accept list is not used for discovery */
5681 u8 filter_policy = 0x00;
5682 /* Default is to enable duplicates filter */
5683 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5686 bt_dev_dbg(hdev, "");
5688 /* If controller is scanning, it means the passive scanning is
5689 * running. Thus, we should temporarily stop it in order to set the
5690 * discovery scanning parameters.
5692 err = hci_scan_disable_sync(hdev);
5694 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5698 cancel_interleave_scan(hdev);
5700 /* Pause address resolution for active scan and stop advertising if
5701 * privacy is enabled.
5703 err = hci_pause_addr_resolution(hdev);
5707 /* All active scans will be done with either a resolvable private
5708 * address (when privacy feature has been enabled) or non-resolvable
5711 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5714 own_addr_type = ADDR_LE_DEV_PUBLIC;
5716 if (hci_is_adv_monitoring(hdev)) {
5717 /* Duplicate filter should be disabled when some advertisement
5718 * monitor is activated, otherwise AdvMon can only receive one
5719 * advertisement for one peer(*) during active scanning, and
5720 * might report loss to these peers.
5722 * Note that different controllers have different meanings of
5723 * |duplicate|. Some of them consider packets with the same
5724 * address as duplicate, and others consider packets with the
5725 * same address and the same RSSI as duplicate. Although in the
5726 * latter case we don't need to disable duplicate filter, but
5727 * it is common to have active scanning for a short period of
5728 * time, the power impact should be neglectable.
5730 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5733 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5734 hdev->le_scan_window_discovery,
5735 own_addr_type, filter_policy, filter_dup);
5740 /* Resume advertising if it was paused */
5741 if (use_ll_privacy(hdev))
5742 hci_resume_advertising_sync(hdev);
5744 /* Resume passive scanning */
5745 hci_update_passive_scan_sync(hdev);
5749 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5753 bt_dev_dbg(hdev, "");
5755 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5759 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5762 int hci_start_discovery_sync(struct hci_dev *hdev)
5764 unsigned long timeout;
5767 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5769 switch (hdev->discovery.type) {
5770 case DISCOV_TYPE_BREDR:
5771 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5772 case DISCOV_TYPE_INTERLEAVED:
5773 /* When running simultaneous discovery, the LE scanning time
5774 * should occupy the whole discovery time sine BR/EDR inquiry
5775 * and LE scanning are scheduled by the controller.
5777 * For interleaving discovery in comparison, BR/EDR inquiry
5778 * and LE scanning are done sequentially with separate
5781 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5783 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5784 /* During simultaneous discovery, we double LE scan
5785 * interval. We must leave some time for the controller
5786 * to do BR/EDR inquiry.
5788 err = hci_start_interleaved_discovery_sync(hdev);
5792 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5793 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5795 case DISCOV_TYPE_LE:
5796 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5797 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5806 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5808 /* When service discovery is used and the controller has a
5809 * strict duplicate filter, it is important to remember the
5810 * start and duration of the scan. This is required for
5811 * restarting scanning during the discovery phase.
5813 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5814 hdev->discovery.result_filtering) {
5815 hdev->discovery.scan_start = jiffies;
5816 hdev->discovery.scan_duration = timeout;
5819 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5824 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5826 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5827 case HCI_ADV_MONITOR_EXT_MSFT:
5828 msft_suspend_sync(hdev);
5835 /* This function disables discovery and mark it as paused */
5836 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5838 int old_state = hdev->discovery.state;
5841 /* If discovery already stopped/stopping/paused there nothing to do */
5842 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
5843 hdev->discovery_paused)
5846 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5847 err = hci_stop_discovery_sync(hdev);
5851 hdev->discovery_paused = true;
5852 hdev->discovery_old_state = old_state;
5853 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5858 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5860 struct bdaddr_list_with_flags *b;
5861 u8 scan = SCAN_DISABLED;
5862 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5865 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5868 /* Some fake CSR controllers lock up after setting this type of
5869 * filter, so avoid sending the request altogether.
5871 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5874 /* Always clear event filter when starting */
5875 hci_clear_event_filter_sync(hdev);
5877 list_for_each_entry(b, &hdev->accept_list, list) {
5878 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
5881 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5883 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5884 HCI_CONN_SETUP_ALLOW_BDADDR,
5886 HCI_CONN_SETUP_AUTO_ON);
5888 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5894 if (scan && !scanning)
5895 hci_write_scan_enable_sync(hdev, scan);
5896 else if (!scan && scanning)
5897 hci_write_scan_enable_sync(hdev, scan);
5902 /* This function disables scan (BR and LE) and mark it as paused */
5903 static int hci_pause_scan_sync(struct hci_dev *hdev)
5905 if (hdev->scanning_paused)
5908 /* Disable page scan if enabled */
5909 if (test_bit(HCI_PSCAN, &hdev->flags))
5910 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
5912 hci_scan_disable_sync(hdev);
5914 hdev->scanning_paused = true;
5919 /* This function performs the HCI suspend procedures in the follow order:
5921 * Pause discovery (active scanning/inquiry)
5922 * Pause Directed Advertising/Advertising
5923 * Pause Scanning (passive scanning in case discovery was not active)
5924 * Disconnect all connections
5925 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5927 * Update event mask (only set events that are allowed to wake up the host)
5928 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
5929 * Update passive scanning (lower duty cycle)
5930 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
5932 int hci_suspend_sync(struct hci_dev *hdev)
5936 /* If marked as suspended there nothing to do */
5937 if (hdev->suspended)
5940 /* Mark device as suspended */
5941 hdev->suspended = true;
5943 /* Pause discovery if not already stopped */
5944 hci_pause_discovery_sync(hdev);
5946 /* Pause other advertisements */
5947 hci_pause_advertising_sync(hdev);
5949 /* Suspend monitor filters */
5950 hci_suspend_monitor_sync(hdev);
5952 /* Prevent disconnects from causing scanning to be re-enabled */
5953 hci_pause_scan_sync(hdev);
5955 if (hci_conn_count(hdev)) {
5956 /* Soft disconnect everything (power off) */
5957 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5959 /* Set state to BT_RUNNING so resume doesn't notify */
5960 hdev->suspend_state = BT_RUNNING;
5961 hci_resume_sync(hdev);
5965 /* Update event mask so only the allowed event can wakeup the
5968 hci_set_event_mask_sync(hdev);
5971 /* Only configure accept list if disconnect succeeded and wake
5972 * isn't being prevented.
5974 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5975 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5979 /* Unpause to take care of updating scanning params */
5980 hdev->scanning_paused = false;
5982 /* Enable event filter for paired devices */
5983 hci_update_event_filter_sync(hdev);
5985 /* Update LE passive scan if enabled */
5986 hci_update_passive_scan_sync(hdev);
5988 /* Pause scan changes again. */
5989 hdev->scanning_paused = true;
5991 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5996 /* This function resumes discovery */
5997 static int hci_resume_discovery_sync(struct hci_dev *hdev)
6001 /* If discovery not paused there nothing to do */
6002 if (!hdev->discovery_paused)
6005 hdev->discovery_paused = false;
6007 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6009 err = hci_start_discovery_sync(hdev);
6011 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
6017 static void hci_resume_monitor_sync(struct hci_dev *hdev)
6019 switch (hci_get_adv_monitor_offload_ext(hdev)) {
6020 case HCI_ADV_MONITOR_EXT_MSFT:
6021 msft_resume_sync(hdev);
6028 /* This function resume scan and reset paused flag */
6029 static int hci_resume_scan_sync(struct hci_dev *hdev)
6031 if (!hdev->scanning_paused)
6034 hdev->scanning_paused = false;
6036 hci_update_scan_sync(hdev);
6038 /* Reset passive scanning to normal */
6039 hci_update_passive_scan_sync(hdev);
6044 /* This function performs the HCI suspend procedures in the follow order:
6046 * Restore event mask
6047 * Clear event filter
6048 * Update passive scanning (normal duty cycle)
6049 * Resume Directed Advertising/Advertising
6050 * Resume discovery (active scanning/inquiry)
6052 int hci_resume_sync(struct hci_dev *hdev)
6054 /* If not marked as suspended there nothing to do */
6055 if (!hdev->suspended)
6058 hdev->suspended = false;
6060 /* Restore event mask */
6061 hci_set_event_mask_sync(hdev);
6063 /* Clear any event filters and restore scan state */
6064 hci_clear_event_filter_sync(hdev);
6066 /* Resume scanning */
6067 hci_resume_scan_sync(hdev);
6069 /* Resume monitor filters */
6070 hci_resume_monitor_sync(hdev);
6072 /* Resume other advertisements */
6073 hci_resume_advertising_sync(hdev);
6075 /* Resume discovery */
6076 hci_resume_discovery_sync(hdev);
6081 static bool conn_use_rpa(struct hci_conn *conn)
6083 struct hci_dev *hdev = conn->hdev;
6085 return hci_dev_test_flag(hdev, HCI_PRIVACY);
6088 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
6089 struct hci_conn *conn)
6091 struct hci_cp_le_set_ext_adv_params cp;
6093 bdaddr_t random_addr;
6096 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6101 /* Set require_privacy to false so that the remote device has a
6102 * chance of identifying us.
6104 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
6105 &own_addr_type, &random_addr);
6109 memset(&cp, 0, sizeof(cp));
6111 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
6112 cp.channel_map = hdev->le_adv_channel_map;
6113 cp.tx_power = HCI_TX_POWER_INVALID;
6114 cp.primary_phy = HCI_ADV_PHY_1M;
6115 cp.secondary_phy = HCI_ADV_PHY_1M;
6116 cp.handle = 0x00; /* Use instance 0 for directed adv */
6117 cp.own_addr_type = own_addr_type;
6118 cp.peer_addr_type = conn->dst_type;
6119 bacpy(&cp.peer_addr, &conn->dst);
6121 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
6122 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
6123 * does not supports advertising data when the advertising set already
6124 * contains some, the controller shall return erroc code 'Invalid
6125 * HCI Command Parameters(0x12).
6126 * So it is required to remove adv set for handle 0x00. since we use
6127 * instance 0 for directed adv.
6129 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
6133 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
6134 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6138 /* Check if random address need to be updated */
6139 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
6140 bacmp(&random_addr, BDADDR_ANY) &&
6141 bacmp(&random_addr, &hdev->random_addr)) {
6142 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
6148 return hci_enable_ext_advertising_sync(hdev, 0x00);
6151 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
6152 struct hci_conn *conn)
6154 struct hci_cp_le_set_adv_param cp;
6159 if (ext_adv_capable(hdev))
6160 return hci_le_ext_directed_advertising_sync(hdev, conn);
6162 /* Clear the HCI_LE_ADV bit temporarily so that the
6163 * hci_update_random_address knows that it's safe to go ahead
6164 * and write a new random address. The flag will be set back on
6165 * as soon as the SET_ADV_ENABLE HCI command completes.
6167 hci_dev_clear_flag(hdev, HCI_LE_ADV);
6169 /* Set require_privacy to false so that the remote device has a
6170 * chance of identifying us.
6172 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6177 memset(&cp, 0, sizeof(cp));
6179 /* Some controllers might reject command if intervals are not
6180 * within range for undirected advertising.
6181 * BCM20702A0 is known to be affected by this.
6183 cp.min_interval = cpu_to_le16(0x0020);
6184 cp.max_interval = cpu_to_le16(0x0020);
6186 cp.type = LE_ADV_DIRECT_IND;
6187 cp.own_address_type = own_addr_type;
6188 cp.direct_addr_type = conn->dst_type;
6189 bacpy(&cp.direct_addr, &conn->dst);
6190 cp.channel_map = hdev->le_adv_channel_map;
6192 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
6193 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6199 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
6200 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
6203 static void set_ext_conn_params(struct hci_conn *conn,
6204 struct hci_cp_le_ext_conn_param *p)
6206 struct hci_dev *hdev = conn->hdev;
6208 memset(p, 0, sizeof(*p));
6210 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6211 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6212 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6213 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6214 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
6215 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6216 p->min_ce_len = cpu_to_le16(0x0000);
6217 p->max_ce_len = cpu_to_le16(0x0000);
6220 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
6221 struct hci_conn *conn, u8 own_addr_type)
6223 struct hci_cp_le_ext_create_conn *cp;
6224 struct hci_cp_le_ext_conn_param *p;
6225 u8 data[sizeof(*cp) + sizeof(*p) * 3];
6229 p = (void *)cp->data;
6231 memset(cp, 0, sizeof(*cp));
6233 bacpy(&cp->peer_addr, &conn->dst);
6234 cp->peer_addr_type = conn->dst_type;
6235 cp->own_addr_type = own_addr_type;
6239 if (scan_1m(hdev)) {
6240 cp->phys |= LE_SCAN_PHY_1M;
6241 set_ext_conn_params(conn, p);
6247 if (scan_2m(hdev)) {
6248 cp->phys |= LE_SCAN_PHY_2M;
6249 set_ext_conn_params(conn, p);
6255 if (scan_coded(hdev)) {
6256 cp->phys |= LE_SCAN_PHY_CODED;
6257 set_ext_conn_params(conn, p);
6262 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6264 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
6265 conn->conn_timeout, NULL);
6268 int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
6270 struct hci_cp_le_create_conn cp;
6271 struct hci_conn_params *params;
6275 /* If requested to connect as peripheral use directed advertising */
6276 if (conn->role == HCI_ROLE_SLAVE) {
6277 /* If we're active scanning and simultaneous roles is not
6278 * enabled simply reject the attempt.
6280 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6281 hdev->le_scan_type == LE_SCAN_ACTIVE &&
6282 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6287 /* Pause advertising while doing directed advertising. */
6288 hci_pause_advertising_sync(hdev);
6290 err = hci_le_directed_advertising_sync(hdev, conn);
6294 /* Disable advertising if simultaneous roles is not in use. */
6295 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6296 hci_pause_advertising_sync(hdev);
6298 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6300 conn->le_conn_min_interval = params->conn_min_interval;
6301 conn->le_conn_max_interval = params->conn_max_interval;
6302 conn->le_conn_latency = params->conn_latency;
6303 conn->le_supv_timeout = params->supervision_timeout;
6305 conn->le_conn_min_interval = hdev->le_conn_min_interval;
6306 conn->le_conn_max_interval = hdev->le_conn_max_interval;
6307 conn->le_conn_latency = hdev->le_conn_latency;
6308 conn->le_supv_timeout = hdev->le_supv_timeout;
6311 /* If controller is scanning, we stop it since some controllers are
6312 * not able to scan and connect at the same time. Also set the
6313 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
6314 * handler for scan disabling knows to set the correct discovery
6317 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6318 hci_scan_disable_sync(hdev);
6319 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6322 /* Update random address, but set require_privacy to false so
6323 * that we never connect with an non-resolvable address.
6325 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6330 if (use_ext_conn(hdev)) {
6331 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6335 memset(&cp, 0, sizeof(cp));
6337 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6338 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6340 bacpy(&cp.peer_addr, &conn->dst);
6341 cp.peer_addr_type = conn->dst_type;
6342 cp.own_address_type = own_addr_type;
6343 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6344 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6345 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
6346 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6347 cp.min_ce_len = cpu_to_le16(0x0000);
6348 cp.max_ce_len = cpu_to_le16(0x0000);
6350 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6352 * If this event is unmasked and the HCI_LE_Connection_Complete event
6353 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6354 * sent when a new connection has been created.
6356 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6358 use_enhanced_conn_complete(hdev) ?
6359 HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6360 HCI_EV_LE_CONN_COMPLETE,
6361 conn->conn_timeout, NULL);
6364 if (err == -ETIMEDOUT)
6365 hci_le_connect_cancel_sync(hdev, conn, 0x00);
6367 /* Re-enable advertising after the connection attempt is finished. */
6368 hci_resume_advertising_sync(hdev);
6372 int hci_le_create_cis_sync(struct hci_dev *hdev)
6375 struct hci_cp_le_create_cis cp;
6376 struct hci_cis cis[0x1f];
6378 struct hci_conn *conn;
6379 u8 cig = BT_ISO_QOS_CIG_UNSET;
6381 /* The spec allows only one pending LE Create CIS command at a time. If
6382 * the command is pending now, don't do anything. We check for pending
6383 * connections after each CIS Established event.
6385 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6388 * If the Host issues this command before all the
6389 * HCI_LE_CIS_Established events from the previous use of the
6390 * command have been generated, the Controller shall return the
6391 * error code Command Disallowed (0x0C).
6393 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6396 * When the Controller receives the HCI_LE_Create_CIS command, the
6397 * Controller sends the HCI_Command_Status event to the Host. An
6398 * HCI_LE_CIS_Established event will be generated for each CIS when it
6399 * is established or if it is disconnected or considered lost before
6400 * being established; until all the events are generated, the command
6404 memset(&cmd, 0, sizeof(cmd));
6410 /* Wait until previous Create CIS has completed */
6411 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6412 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
6416 /* Find CIG with all CIS ready */
6417 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6418 struct hci_conn *link;
6420 if (hci_conn_check_create_cis(conn))
6423 cig = conn->iso_qos.ucast.cig;
6425 list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
6426 if (hci_conn_check_create_cis(link) > 0 &&
6427 link->iso_qos.ucast.cig == cig &&
6428 link->state != BT_CONNECTED) {
6429 cig = BT_ISO_QOS_CIG_UNSET;
6434 if (cig != BT_ISO_QOS_CIG_UNSET)
6438 if (cig == BT_ISO_QOS_CIG_UNSET)
6441 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6442 struct hci_cis *cis = &cmd.cis[cmd.cp.num_cis];
6444 if (hci_conn_check_create_cis(conn) ||
6445 conn->iso_qos.ucast.cig != cig)
6448 set_bit(HCI_CONN_CREATE_CIS, &conn->flags);
6449 cis->acl_handle = cpu_to_le16(conn->parent->handle);
6450 cis->cis_handle = cpu_to_le16(conn->handle);
6453 if (cmd.cp.num_cis >= ARRAY_SIZE(cmd.cis))
6460 hci_dev_unlock(hdev);
6462 if (!cmd.cp.num_cis)
6465 /* Wait for HCI_LE_CIS_Established */
6466 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6467 sizeof(cmd.cp) + sizeof(cmd.cis[0]) *
6468 cmd.cp.num_cis, &cmd,
6469 HCI_EVT_LE_CIS_ESTABLISHED,
6470 conn->conn_timeout, NULL);
6473 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6475 struct hci_cp_le_remove_cig cp;
6477 memset(&cp, 0, sizeof(cp));
6480 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6481 &cp, HCI_CMD_TIMEOUT);
6484 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6486 struct hci_cp_le_big_term_sync cp;
6488 memset(&cp, 0, sizeof(cp));
6491 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6492 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6495 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6497 struct hci_cp_le_pa_term_sync cp;
6499 memset(&cp, 0, sizeof(cp));
6500 cp.handle = cpu_to_le16(handle);
6502 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6503 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6506 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6507 bool use_rpa, struct adv_info *adv_instance,
6508 u8 *own_addr_type, bdaddr_t *rand_addr)
6512 bacpy(rand_addr, BDADDR_ANY);
6514 /* If privacy is enabled use a resolvable private address. If
6515 * current RPA has expired then generate a new one.
6518 /* If Controller supports LL Privacy use own address type is
6521 if (use_ll_privacy(hdev))
6522 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
6524 *own_addr_type = ADDR_LE_DEV_RANDOM;
6527 if (adv_rpa_valid(adv_instance))
6530 if (rpa_valid(hdev))
6534 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6536 bt_dev_err(hdev, "failed to generate new RPA");
6540 bacpy(rand_addr, &hdev->rpa);
6545 /* In case of required privacy without resolvable private address,
6546 * use an non-resolvable private address. This is useful for
6547 * non-connectable advertising.
6549 if (require_privacy) {
6553 /* The non-resolvable private address is generated
6554 * from random six bytes with the two most significant
6557 get_random_bytes(&nrpa, 6);
6560 /* The non-resolvable private address shall not be
6561 * equal to the public address.
6563 if (bacmp(&hdev->bdaddr, &nrpa))
6567 *own_addr_type = ADDR_LE_DEV_RANDOM;
6568 bacpy(rand_addr, &nrpa);
6573 /* No privacy so use a public address. */
6574 *own_addr_type = ADDR_LE_DEV_PUBLIC;
6579 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6581 u8 instance = PTR_UINT(data);
6583 return hci_update_adv_data_sync(hdev, instance);
6586 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6588 return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6589 UINT_PTR(instance), NULL);