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%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%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 int le_scan_restart_sync(struct hci_dev *hdev, void *data)
418 return hci_le_scan_restart_sync(hdev);
421 static void le_scan_restart(struct work_struct *work)
423 struct hci_dev *hdev = container_of(work, struct hci_dev,
424 le_scan_restart.work);
425 unsigned long timeout, duration, scan_start, now;
428 bt_dev_dbg(hdev, "");
432 status = hci_cmd_sync_queue(hdev, le_scan_restart_sync, NULL, NULL);
434 bt_dev_err(hdev, "failed to restart LE scan: status %d",
439 if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
440 !hdev->discovery.scan_start)
443 /* When the scan was started, hdev->le_scan_disable has been queued
444 * after duration from scan_start. During scan restart this job
445 * has been canceled, and we need to queue it again after proper
446 * timeout, to make sure that scan does not run indefinitely.
448 duration = hdev->discovery.scan_duration;
449 scan_start = hdev->discovery.scan_start;
451 if (now - scan_start <= duration) {
454 if (now >= scan_start)
455 elapsed = now - scan_start;
457 elapsed = ULONG_MAX - scan_start + now;
459 timeout = duration - elapsed;
464 queue_delayed_work(hdev->req_workqueue,
465 &hdev->le_scan_disable, timeout);
468 hci_dev_unlock(hdev);
471 static int reenable_adv_sync(struct hci_dev *hdev, void *data)
473 bt_dev_dbg(hdev, "");
475 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
476 list_empty(&hdev->adv_instances))
479 if (hdev->cur_adv_instance) {
480 return hci_schedule_adv_instance_sync(hdev,
481 hdev->cur_adv_instance,
484 if (ext_adv_capable(hdev)) {
485 hci_start_ext_adv_sync(hdev, 0x00);
487 hci_update_adv_data_sync(hdev, 0x00);
488 hci_update_scan_rsp_data_sync(hdev, 0x00);
489 hci_enable_advertising_sync(hdev);
496 static void reenable_adv(struct work_struct *work)
498 struct hci_dev *hdev = container_of(work, struct hci_dev,
502 bt_dev_dbg(hdev, "");
506 status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
508 bt_dev_err(hdev, "failed to reenable ADV: %d", status);
510 hci_dev_unlock(hdev);
513 static void cancel_adv_timeout(struct hci_dev *hdev)
515 if (hdev->adv_instance_timeout) {
516 hdev->adv_instance_timeout = 0;
517 cancel_delayed_work(&hdev->adv_instance_expire);
521 /* For a single instance:
522 * - force == true: The instance will be removed even when its remaining
523 * lifetime is not zero.
524 * - force == false: the instance will be deactivated but kept stored unless
525 * the remaining lifetime is zero.
527 * For instance == 0x00:
528 * - force == true: All instances will be removed regardless of their timeout
530 * - force == false: Only instances that have a timeout will be removed.
532 int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
533 u8 instance, bool force)
535 struct adv_info *adv_instance, *n, *next_instance = NULL;
539 /* Cancel any timeout concerning the removed instance(s). */
540 if (!instance || hdev->cur_adv_instance == instance)
541 cancel_adv_timeout(hdev);
543 /* Get the next instance to advertise BEFORE we remove
544 * the current one. This can be the same instance again
545 * if there is only one instance.
547 if (instance && hdev->cur_adv_instance == instance)
548 next_instance = hci_get_next_instance(hdev, instance);
550 if (instance == 0x00) {
551 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
553 if (!(force || adv_instance->timeout))
556 rem_inst = adv_instance->instance;
557 err = hci_remove_adv_instance(hdev, rem_inst);
559 mgmt_advertising_removed(sk, hdev, rem_inst);
562 adv_instance = hci_find_adv_instance(hdev, instance);
564 if (force || (adv_instance && adv_instance->timeout &&
565 !adv_instance->remaining_time)) {
566 /* Don't advertise a removed instance. */
568 next_instance->instance == instance)
569 next_instance = NULL;
571 err = hci_remove_adv_instance(hdev, instance);
573 mgmt_advertising_removed(sk, hdev, instance);
577 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
580 if (next_instance && !ext_adv_capable(hdev))
581 return hci_schedule_adv_instance_sync(hdev,
582 next_instance->instance,
588 static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
590 u8 instance = *(u8 *)data;
594 hci_clear_adv_instance_sync(hdev, NULL, instance, false);
596 if (list_empty(&hdev->adv_instances))
597 return hci_disable_advertising_sync(hdev);
602 static void adv_timeout_expire(struct work_struct *work)
605 struct hci_dev *hdev = container_of(work, struct hci_dev,
606 adv_instance_expire.work);
608 bt_dev_dbg(hdev, "");
612 hdev->adv_instance_timeout = 0;
614 if (hdev->cur_adv_instance == 0x00)
617 inst_ptr = kmalloc(1, GFP_KERNEL);
621 *inst_ptr = hdev->cur_adv_instance;
622 hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
625 hci_dev_unlock(hdev);
628 void hci_cmd_sync_init(struct hci_dev *hdev)
630 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
631 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
632 mutex_init(&hdev->cmd_sync_work_lock);
633 mutex_init(&hdev->unregister_lock);
635 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
636 INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
637 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
638 INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart);
639 INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
642 void hci_cmd_sync_clear(struct hci_dev *hdev)
644 struct hci_cmd_sync_work_entry *entry, *tmp;
646 cancel_work_sync(&hdev->cmd_sync_work);
647 cancel_work_sync(&hdev->reenable_adv_work);
649 mutex_lock(&hdev->cmd_sync_work_lock);
650 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
652 entry->destroy(hdev, entry->data, -ECANCELED);
654 list_del(&entry->list);
657 mutex_unlock(&hdev->cmd_sync_work_lock);
660 void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
662 bt_dev_dbg(hdev, "err 0x%2.2x", err);
664 if (hdev->req_status == HCI_REQ_PEND) {
665 hdev->req_result = err;
666 hdev->req_status = HCI_REQ_CANCELED;
668 cancel_delayed_work_sync(&hdev->cmd_timer);
669 cancel_delayed_work_sync(&hdev->ncmd_timer);
670 atomic_set(&hdev->cmd_cnt, 1);
672 wake_up_interruptible(&hdev->req_wait_q);
676 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
678 bt_dev_dbg(hdev, "err 0x%2.2x", err);
680 if (hdev->req_status == HCI_REQ_PEND) {
681 hdev->req_result = err;
682 hdev->req_status = HCI_REQ_CANCELED;
684 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
687 EXPORT_SYMBOL(hci_cmd_sync_cancel);
689 /* Submit HCI command to be run in as cmd_sync_work:
691 * - hdev must _not_ be unregistered
693 int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
694 void *data, hci_cmd_sync_work_destroy_t destroy)
696 struct hci_cmd_sync_work_entry *entry;
699 mutex_lock(&hdev->unregister_lock);
700 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
705 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
712 entry->destroy = destroy;
714 mutex_lock(&hdev->cmd_sync_work_lock);
715 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
716 mutex_unlock(&hdev->cmd_sync_work_lock);
718 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
721 mutex_unlock(&hdev->unregister_lock);
724 EXPORT_SYMBOL(hci_cmd_sync_submit);
726 /* Queue HCI command:
728 * - hdev must be running
730 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
731 void *data, hci_cmd_sync_work_destroy_t destroy)
733 /* Only queue command if hdev is running which means it had been opened
734 * and is either on init phase or is already up.
736 if (!test_bit(HCI_RUNNING, &hdev->flags))
739 return hci_cmd_sync_submit(hdev, func, data, destroy);
741 EXPORT_SYMBOL(hci_cmd_sync_queue);
743 int hci_update_eir_sync(struct hci_dev *hdev)
745 struct hci_cp_write_eir cp;
747 bt_dev_dbg(hdev, "");
749 if (!hdev_is_powered(hdev))
752 if (!lmp_ext_inq_capable(hdev))
755 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
758 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
761 memset(&cp, 0, sizeof(cp));
763 eir_create(hdev, cp.data);
765 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
768 memcpy(hdev->eir, cp.data, sizeof(cp.data));
770 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
774 static u8 get_service_classes(struct hci_dev *hdev)
776 struct bt_uuid *uuid;
779 list_for_each_entry(uuid, &hdev->uuids, list)
780 val |= uuid->svc_hint;
785 int hci_update_class_sync(struct hci_dev *hdev)
789 bt_dev_dbg(hdev, "");
791 if (!hdev_is_powered(hdev))
794 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
797 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
800 cod[0] = hdev->minor_class;
801 cod[1] = hdev->major_class;
802 cod[2] = get_service_classes(hdev);
804 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
807 if (memcmp(cod, hdev->dev_class, 3) == 0)
810 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
811 sizeof(cod), cod, HCI_CMD_TIMEOUT);
814 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
816 /* If there is no connection we are OK to advertise. */
817 if (hci_conn_num(hdev, LE_LINK) == 0)
820 /* Check le_states if there is any connection in peripheral role. */
821 if (hdev->conn_hash.le_num_peripheral > 0) {
822 /* Peripheral connection state and non connectable mode
825 if (!connectable && !(hdev->le_states[2] & 0x10))
828 /* Peripheral connection state and connectable mode bit 38
829 * and scannable bit 21.
831 if (connectable && (!(hdev->le_states[4] & 0x40) ||
832 !(hdev->le_states[2] & 0x20)))
836 /* Check le_states if there is any connection in central role. */
837 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
838 /* Central connection state and non connectable mode bit 18. */
839 if (!connectable && !(hdev->le_states[2] & 0x02))
842 /* Central connection state and connectable mode bit 35 and
845 if (connectable && (!(hdev->le_states[4] & 0x08) ||
846 !(hdev->le_states[2] & 0x08)))
853 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
855 /* If privacy is not enabled don't use RPA */
856 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
859 /* If basic privacy mode is enabled use RPA */
860 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
863 /* If limited privacy mode is enabled don't use RPA if we're
864 * both discoverable and bondable.
866 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
867 hci_dev_test_flag(hdev, HCI_BONDABLE))
870 /* We're neither bondable nor discoverable in the limited
871 * privacy mode, therefore use RPA.
876 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
878 /* If we're advertising or initiating an LE connection we can't
879 * go ahead and change the random address at this time. This is
880 * because the eventual initiator address used for the
881 * subsequently created connection will be undefined (some
882 * controllers use the new address and others the one we had
883 * when the operation started).
885 * In this kind of scenario skip the update and let the random
886 * address be updated at the next cycle.
888 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
889 hci_lookup_le_connect(hdev)) {
890 bt_dev_dbg(hdev, "Deferring random address update");
891 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
895 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
896 6, rpa, HCI_CMD_TIMEOUT);
899 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
900 bool rpa, u8 *own_addr_type)
904 /* If privacy is enabled use a resolvable private address. If
905 * current RPA has expired or there is something else than
906 * the current RPA in use, then generate a new one.
909 /* If Controller supports LL Privacy use own address type is
912 if (use_ll_privacy(hdev))
913 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
915 *own_addr_type = ADDR_LE_DEV_RANDOM;
917 /* Check if RPA is valid */
921 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
923 bt_dev_err(hdev, "failed to generate new RPA");
927 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
934 /* In case of required privacy without resolvable private address,
935 * use an non-resolvable private address. This is useful for active
936 * scanning and non-connectable advertising.
938 if (require_privacy) {
942 /* The non-resolvable private address is generated
943 * from random six bytes with the two most significant
946 get_random_bytes(&nrpa, 6);
949 /* The non-resolvable private address shall not be
950 * equal to the public address.
952 if (bacmp(&hdev->bdaddr, &nrpa))
956 *own_addr_type = ADDR_LE_DEV_RANDOM;
958 return hci_set_random_addr_sync(hdev, &nrpa);
961 /* If forcing static address is in use or there is no public
962 * address use the static address as random address (but skip
963 * the HCI command if the current random address is already the
966 * In case BR/EDR has been disabled on a dual-mode controller
967 * and a static address has been configured, then use that
968 * address instead of the public BR/EDR address.
970 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
971 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
972 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
973 bacmp(&hdev->static_addr, BDADDR_ANY))) {
974 *own_addr_type = ADDR_LE_DEV_RANDOM;
975 if (bacmp(&hdev->static_addr, &hdev->random_addr))
976 return hci_set_random_addr_sync(hdev,
981 /* Neither privacy nor static address is being used so use a
984 *own_addr_type = ADDR_LE_DEV_PUBLIC;
989 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
991 struct hci_cp_le_set_ext_adv_enable *cp;
992 struct hci_cp_ext_adv_set *set;
993 u8 data[sizeof(*cp) + sizeof(*set) * 1];
996 /* If request specifies an instance that doesn't exist, fail */
998 struct adv_info *adv;
1000 adv = hci_find_adv_instance(hdev, instance);
1004 /* If not enabled there is nothing to do */
1009 memset(data, 0, sizeof(data));
1012 set = (void *)cp->data;
1014 /* Instance 0x00 indicates all advertising instances will be disabled */
1015 cp->num_of_sets = !!instance;
1018 set->handle = instance;
1020 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
1022 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1023 size, data, HCI_CMD_TIMEOUT);
1026 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1027 bdaddr_t *random_addr)
1029 struct hci_cp_le_set_adv_set_rand_addr cp;
1033 /* Instance 0x00 doesn't have an adv_info, instead it uses
1034 * hdev->random_addr to track its address so whenever it needs
1035 * to be updated this also set the random address since
1036 * hdev->random_addr is shared with scan state machine.
1038 err = hci_set_random_addr_sync(hdev, random_addr);
1043 memset(&cp, 0, sizeof(cp));
1045 cp.handle = instance;
1046 bacpy(&cp.bdaddr, random_addr);
1048 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1049 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1052 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1054 struct hci_cp_le_set_ext_adv_params cp;
1057 bdaddr_t random_addr;
1060 struct adv_info *adv;
1064 adv = hci_find_adv_instance(hdev, instance);
1071 /* Updating parameters of an active instance will return a
1072 * Command Disallowed error, so we must first disable the
1073 * instance if it is active.
1075 if (adv && !adv->pending) {
1076 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1081 flags = hci_adv_instance_flags(hdev, instance);
1083 /* If the "connectable" instance flag was not set, then choose between
1084 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1086 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1087 mgmt_get_connectable(hdev);
1089 if (!is_advertising_allowed(hdev, connectable))
1092 /* Set require_privacy to true only when non-connectable
1093 * advertising is used. In that case it is fine to use a
1094 * non-resolvable private address.
1096 err = hci_get_random_address(hdev, !connectable,
1097 adv_use_rpa(hdev, flags), adv,
1098 &own_addr_type, &random_addr);
1102 memset(&cp, 0, sizeof(cp));
1105 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
1106 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
1107 cp.tx_power = adv->tx_power;
1109 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1110 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1111 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
1114 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
1118 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
1120 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
1121 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1122 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
1124 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
1126 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
1129 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
1131 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
1134 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
1135 * contains the peer’s Identity Address and the Peer_Address_Type
1136 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
1137 * These parameters are used to locate the corresponding local IRK in
1138 * the resolving list; this IRK is used to generate their own address
1139 * used in the advertisement.
1141 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
1142 hci_copy_identity_address(hdev, &cp.peer_addr,
1143 &cp.peer_addr_type);
1145 cp.own_addr_type = own_addr_type;
1146 cp.channel_map = hdev->le_adv_channel_map;
1147 cp.handle = instance;
1149 if (flags & MGMT_ADV_FLAG_SEC_2M) {
1150 cp.primary_phy = HCI_ADV_PHY_1M;
1151 cp.secondary_phy = HCI_ADV_PHY_2M;
1152 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
1153 cp.primary_phy = HCI_ADV_PHY_CODED;
1154 cp.secondary_phy = HCI_ADV_PHY_CODED;
1156 /* In all other cases use 1M */
1157 cp.primary_phy = HCI_ADV_PHY_1M;
1158 cp.secondary_phy = HCI_ADV_PHY_1M;
1161 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1162 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1166 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
1167 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
1168 bacmp(&random_addr, BDADDR_ANY)) {
1169 /* Check if random address need to be updated */
1171 if (!bacmp(&random_addr, &adv->random_addr))
1174 if (!bacmp(&random_addr, &hdev->random_addr))
1178 return hci_set_adv_set_random_addr_sync(hdev, instance,
1185 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1188 struct hci_cp_le_set_ext_scan_rsp_data cp;
1189 u8 data[HCI_MAX_EXT_AD_LENGTH];
1192 struct adv_info *adv = NULL;
1195 memset(&pdu, 0, sizeof(pdu));
1198 adv = hci_find_adv_instance(hdev, instance);
1199 if (!adv || !adv->scan_rsp_changed)
1203 len = eir_create_scan_rsp(hdev, instance, pdu.data);
1205 pdu.cp.handle = instance;
1206 pdu.cp.length = len;
1207 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1208 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1210 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1211 sizeof(pdu.cp) + len, &pdu.cp,
1217 adv->scan_rsp_changed = false;
1219 memcpy(hdev->scan_rsp_data, pdu.data, len);
1220 hdev->scan_rsp_data_len = len;
1226 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1228 struct hci_cp_le_set_scan_rsp_data cp;
1231 memset(&cp, 0, sizeof(cp));
1233 len = eir_create_scan_rsp(hdev, instance, cp.data);
1235 if (hdev->scan_rsp_data_len == len &&
1236 !memcmp(cp.data, hdev->scan_rsp_data, len))
1239 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1240 hdev->scan_rsp_data_len = len;
1244 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1245 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1248 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1250 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1253 if (ext_adv_capable(hdev))
1254 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1256 return __hci_set_scan_rsp_data_sync(hdev, instance);
1259 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1261 struct hci_cp_le_set_ext_adv_enable *cp;
1262 struct hci_cp_ext_adv_set *set;
1263 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1264 struct adv_info *adv;
1267 adv = hci_find_adv_instance(hdev, instance);
1270 /* If already enabled there is nothing to do */
1278 set = (void *)cp->data;
1280 memset(cp, 0, sizeof(*cp));
1283 cp->num_of_sets = 0x01;
1285 memset(set, 0, sizeof(*set));
1287 set->handle = instance;
1289 /* Set duration per instance since controller is responsible for
1292 if (adv && adv->timeout) {
1293 u16 duration = adv->timeout * MSEC_PER_SEC;
1295 /* Time = N * 10 ms */
1296 set->duration = cpu_to_le16(duration / 10);
1299 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1301 sizeof(*set) * cp->num_of_sets,
1302 data, HCI_CMD_TIMEOUT);
1305 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1309 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1313 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1317 return hci_enable_ext_advertising_sync(hdev, instance);
1320 static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1322 struct hci_cp_le_set_per_adv_enable cp;
1323 struct adv_info *adv = NULL;
1325 /* If periodic advertising already disabled there is nothing to do. */
1326 adv = hci_find_adv_instance(hdev, instance);
1327 if (!adv || !adv->periodic || !adv->enabled)
1330 memset(&cp, 0, sizeof(cp));
1333 cp.handle = instance;
1335 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1336 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1339 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1340 u16 min_interval, u16 max_interval)
1342 struct hci_cp_le_set_per_adv_params cp;
1344 memset(&cp, 0, sizeof(cp));
1347 min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1350 max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1352 cp.handle = instance;
1353 cp.min_interval = cpu_to_le16(min_interval);
1354 cp.max_interval = cpu_to_le16(max_interval);
1355 cp.periodic_properties = 0x0000;
1357 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1358 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1361 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1364 struct hci_cp_le_set_per_adv_data cp;
1365 u8 data[HCI_MAX_PER_AD_LENGTH];
1369 memset(&pdu, 0, sizeof(pdu));
1372 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1374 if (!adv || !adv->periodic)
1378 len = eir_create_per_adv_data(hdev, instance, pdu.data);
1380 pdu.cp.length = len;
1381 pdu.cp.handle = instance;
1382 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1384 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1385 sizeof(pdu.cp) + len, &pdu,
1389 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1391 struct hci_cp_le_set_per_adv_enable cp;
1392 struct adv_info *adv = NULL;
1394 /* If periodic advertising already enabled there is nothing to do. */
1395 adv = hci_find_adv_instance(hdev, instance);
1396 if (adv && adv->periodic && adv->enabled)
1399 memset(&cp, 0, sizeof(cp));
1402 cp.handle = instance;
1404 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1405 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1408 /* Checks if periodic advertising data contains a Basic Announcement and if it
1409 * does generates a Broadcast ID and add Broadcast Announcement.
1411 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1416 /* Skip if NULL adv as instance 0x00 is used for general purpose
1417 * advertising so it cannot used for the likes of Broadcast Announcement
1418 * as it can be overwritten at any point.
1423 /* Check if PA data doesn't contains a Basic Audio Announcement then
1424 * there is nothing to do.
1426 if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1430 /* Check if advertising data already has a Broadcast Announcement since
1431 * the process may want to control the Broadcast ID directly and in that
1432 * case the kernel shall no interfere.
1434 if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1438 /* Generate Broadcast ID */
1439 get_random_bytes(bid, sizeof(bid));
1440 eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1441 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1443 return hci_update_adv_data_sync(hdev, adv->instance);
1446 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1447 u8 *data, u32 flags, u16 min_interval,
1448 u16 max_interval, u16 sync_interval)
1450 struct adv_info *adv = NULL;
1454 hci_disable_per_advertising_sync(hdev, instance);
1457 adv = hci_find_adv_instance(hdev, instance);
1458 /* Create an instance if that could not be found */
1460 adv = hci_add_per_instance(hdev, instance, flags,
1465 return PTR_ERR(adv);
1466 adv->pending = false;
1471 /* Start advertising */
1472 err = hci_start_ext_adv_sync(hdev, instance);
1476 err = hci_adv_bcast_annoucement(hdev, adv);
1480 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1485 err = hci_set_per_adv_data_sync(hdev, instance);
1489 err = hci_enable_per_advertising_sync(hdev, instance);
1497 hci_remove_adv_instance(hdev, instance);
1502 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1506 if (ext_adv_capable(hdev))
1507 return hci_start_ext_adv_sync(hdev, instance);
1509 err = hci_update_adv_data_sync(hdev, instance);
1513 err = hci_update_scan_rsp_data_sync(hdev, instance);
1517 return hci_enable_advertising_sync(hdev);
1520 int hci_enable_advertising_sync(struct hci_dev *hdev)
1522 struct adv_info *adv_instance;
1523 struct hci_cp_le_set_adv_param cp;
1524 u8 own_addr_type, enable = 0x01;
1526 u16 adv_min_interval, adv_max_interval;
1530 if (ext_adv_capable(hdev))
1531 return hci_enable_ext_advertising_sync(hdev,
1532 hdev->cur_adv_instance);
1534 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1535 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1537 /* If the "connectable" instance flag was not set, then choose between
1538 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1540 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1541 mgmt_get_connectable(hdev);
1543 if (!is_advertising_allowed(hdev, connectable))
1546 status = hci_disable_advertising_sync(hdev);
1550 /* Clear the HCI_LE_ADV bit temporarily so that the
1551 * hci_update_random_address knows that it's safe to go ahead
1552 * and write a new random address. The flag will be set back on
1553 * as soon as the SET_ADV_ENABLE HCI command completes.
1555 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1557 /* Set require_privacy to true only when non-connectable
1558 * advertising is used. In that case it is fine to use a
1559 * non-resolvable private address.
1561 status = hci_update_random_address_sync(hdev, !connectable,
1562 adv_use_rpa(hdev, flags),
1567 memset(&cp, 0, sizeof(cp));
1570 adv_min_interval = adv_instance->min_interval;
1571 adv_max_interval = adv_instance->max_interval;
1573 adv_min_interval = hdev->le_adv_min_interval;
1574 adv_max_interval = hdev->le_adv_max_interval;
1578 cp.type = LE_ADV_IND;
1580 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1581 cp.type = LE_ADV_SCAN_IND;
1583 cp.type = LE_ADV_NONCONN_IND;
1585 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1586 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1587 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1588 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1592 cp.min_interval = cpu_to_le16(adv_min_interval);
1593 cp.max_interval = cpu_to_le16(adv_max_interval);
1594 cp.own_address_type = own_addr_type;
1595 cp.channel_map = hdev->le_adv_channel_map;
1597 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1598 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1602 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1603 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1606 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1608 return hci_enable_advertising_sync(hdev);
1611 int hci_enable_advertising(struct hci_dev *hdev)
1613 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1614 list_empty(&hdev->adv_instances))
1617 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1620 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1625 if (!ext_adv_capable(hdev))
1628 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1632 /* If request specifies an instance that doesn't exist, fail */
1633 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1636 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1637 sizeof(instance), &instance, 0,
1638 HCI_CMD_TIMEOUT, sk);
1641 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1643 struct adv_info *adv = data;
1647 instance = adv->instance;
1649 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1652 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1654 struct adv_info *adv = NULL;
1657 adv = hci_find_adv_instance(hdev, instance);
1662 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1665 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1667 struct hci_cp_le_term_big cp;
1669 memset(&cp, 0, sizeof(cp));
1673 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1674 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1677 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1680 struct hci_cp_le_set_ext_adv_data cp;
1681 u8 data[HCI_MAX_EXT_AD_LENGTH];
1684 struct adv_info *adv = NULL;
1687 memset(&pdu, 0, sizeof(pdu));
1690 adv = hci_find_adv_instance(hdev, instance);
1691 if (!adv || !adv->adv_data_changed)
1695 len = eir_create_adv_data(hdev, instance, pdu.data);
1697 pdu.cp.length = len;
1698 pdu.cp.handle = instance;
1699 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1700 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1702 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1703 sizeof(pdu.cp) + len, &pdu.cp,
1708 /* Update data if the command succeed */
1710 adv->adv_data_changed = false;
1712 memcpy(hdev->adv_data, pdu.data, len);
1713 hdev->adv_data_len = len;
1719 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1721 struct hci_cp_le_set_adv_data cp;
1724 memset(&cp, 0, sizeof(cp));
1726 len = eir_create_adv_data(hdev, instance, cp.data);
1728 /* There's nothing to do if the data hasn't changed */
1729 if (hdev->adv_data_len == len &&
1730 memcmp(cp.data, hdev->adv_data, len) == 0)
1733 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1734 hdev->adv_data_len = len;
1738 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1739 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1742 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1744 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1747 if (ext_adv_capable(hdev))
1748 return hci_set_ext_adv_data_sync(hdev, instance);
1750 return hci_set_adv_data_sync(hdev, instance);
1753 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1756 struct adv_info *adv = NULL;
1759 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1762 if (hdev->adv_instance_timeout)
1765 adv = hci_find_adv_instance(hdev, instance);
1769 /* A zero timeout means unlimited advertising. As long as there is
1770 * only one instance, duration should be ignored. We still set a timeout
1771 * in case further instances are being added later on.
1773 * If the remaining lifetime of the instance is more than the duration
1774 * then the timeout corresponds to the duration, otherwise it will be
1775 * reduced to the remaining instance lifetime.
1777 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1778 timeout = adv->duration;
1780 timeout = adv->remaining_time;
1782 /* The remaining time is being reduced unless the instance is being
1783 * advertised without time limit.
1786 adv->remaining_time = adv->remaining_time - timeout;
1788 /* Only use work for scheduling instances with legacy advertising */
1789 if (!ext_adv_capable(hdev)) {
1790 hdev->adv_instance_timeout = timeout;
1791 queue_delayed_work(hdev->req_workqueue,
1792 &hdev->adv_instance_expire,
1793 msecs_to_jiffies(timeout * 1000));
1796 /* If we're just re-scheduling the same instance again then do not
1797 * execute any HCI commands. This happens when a single instance is
1800 if (!force && hdev->cur_adv_instance == instance &&
1801 hci_dev_test_flag(hdev, HCI_LE_ADV))
1804 hdev->cur_adv_instance = instance;
1806 return hci_start_adv_sync(hdev, instance);
1809 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1813 if (!ext_adv_capable(hdev))
1816 /* Disable instance 0x00 to disable all instances */
1817 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1821 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1822 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1825 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1827 struct adv_info *adv, *n;
1830 if (ext_adv_capable(hdev))
1831 /* Remove all existing sets */
1832 err = hci_clear_adv_sets_sync(hdev, sk);
1833 if (ext_adv_capable(hdev))
1836 /* This is safe as long as there is no command send while the lock is
1841 /* Cleanup non-ext instances */
1842 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1843 u8 instance = adv->instance;
1846 if (!(force || adv->timeout))
1849 err = hci_remove_adv_instance(hdev, instance);
1851 mgmt_advertising_removed(sk, hdev, instance);
1854 hci_dev_unlock(hdev);
1859 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1864 /* If we use extended advertising, instance has to be removed first. */
1865 if (ext_adv_capable(hdev))
1866 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1867 if (ext_adv_capable(hdev))
1870 /* This is safe as long as there is no command send while the lock is
1875 err = hci_remove_adv_instance(hdev, instance);
1877 mgmt_advertising_removed(sk, hdev, instance);
1879 hci_dev_unlock(hdev);
1884 /* For a single instance:
1885 * - force == true: The instance will be removed even when its remaining
1886 * lifetime is not zero.
1887 * - force == false: the instance will be deactivated but kept stored unless
1888 * the remaining lifetime is zero.
1890 * For instance == 0x00:
1891 * - force == true: All instances will be removed regardless of their timeout
1893 * - force == false: Only instances that have a timeout will be removed.
1895 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1896 u8 instance, bool force)
1898 struct adv_info *next = NULL;
1901 /* Cancel any timeout concerning the removed instance(s). */
1902 if (!instance || hdev->cur_adv_instance == instance)
1903 cancel_adv_timeout(hdev);
1905 /* Get the next instance to advertise BEFORE we remove
1906 * the current one. This can be the same instance again
1907 * if there is only one instance.
1909 if (hdev->cur_adv_instance == instance)
1910 next = hci_get_next_instance(hdev, instance);
1913 err = hci_clear_adv_sync(hdev, sk, force);
1917 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1919 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1920 /* Don't advertise a removed instance. */
1921 if (next && next->instance == instance)
1924 err = hci_remove_adv_sync(hdev, instance, sk);
1930 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1933 if (next && !ext_adv_capable(hdev))
1934 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1939 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1941 struct hci_cp_read_rssi cp;
1944 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1945 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1948 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1950 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1951 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1954 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1956 struct hci_cp_read_tx_power cp;
1960 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1961 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1964 int hci_disable_advertising_sync(struct hci_dev *hdev)
1969 /* If controller is not advertising we are done. */
1970 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1973 if (ext_adv_capable(hdev))
1974 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1975 if (ext_adv_capable(hdev))
1978 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1979 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1982 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1985 struct hci_cp_le_set_ext_scan_enable cp;
1987 memset(&cp, 0, sizeof(cp));
1990 if (hci_dev_test_flag(hdev, HCI_MESH))
1991 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
1993 cp.filter_dup = filter_dup;
1995 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1996 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1999 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2002 struct hci_cp_le_set_scan_enable cp;
2004 if (use_ext_scan(hdev))
2005 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2007 memset(&cp, 0, sizeof(cp));
2010 if (val && hci_dev_test_flag(hdev, HCI_MESH))
2011 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2013 cp.filter_dup = filter_dup;
2015 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2016 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2019 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2021 if (!use_ll_privacy(hdev))
2024 /* If controller is not/already resolving we are done. */
2025 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2028 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2029 sizeof(val), &val, HCI_CMD_TIMEOUT);
2032 static int hci_scan_disable_sync(struct hci_dev *hdev)
2036 /* If controller is not scanning we are done. */
2037 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2040 if (hdev->scanning_paused) {
2041 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2045 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2047 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2054 static bool scan_use_rpa(struct hci_dev *hdev)
2056 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2059 static void hci_start_interleave_scan(struct hci_dev *hdev)
2061 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2062 queue_delayed_work(hdev->req_workqueue,
2063 &hdev->interleave_scan, 0);
2066 static bool is_interleave_scanning(struct hci_dev *hdev)
2068 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2071 static void cancel_interleave_scan(struct hci_dev *hdev)
2073 bt_dev_dbg(hdev, "cancelling interleave scan");
2075 cancel_delayed_work_sync(&hdev->interleave_scan);
2077 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2080 /* Return true if interleave_scan wasn't started until exiting this function,
2081 * otherwise, return false
2083 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2085 /* Do interleaved scan only if all of the following are true:
2086 * - There is at least one ADV monitor
2087 * - At least one pending LE connection or one device to be scanned for
2088 * - Monitor offloading is not supported
2089 * If so, we should alternate between allowlist scan and one without
2090 * any filters to save power.
2092 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2093 !(list_empty(&hdev->pend_le_conns) &&
2094 list_empty(&hdev->pend_le_reports)) &&
2095 hci_get_adv_monitor_offload_ext(hdev) ==
2096 HCI_ADV_MONITOR_EXT_NONE;
2097 bool is_interleaving = is_interleave_scanning(hdev);
2099 if (use_interleaving && !is_interleaving) {
2100 hci_start_interleave_scan(hdev);
2101 bt_dev_dbg(hdev, "starting interleave scan");
2105 if (!use_interleaving && is_interleaving)
2106 cancel_interleave_scan(hdev);
2111 /* Removes connection to resolve list if needed.*/
2112 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2113 bdaddr_t *bdaddr, u8 bdaddr_type)
2115 struct hci_cp_le_del_from_resolv_list cp;
2116 struct bdaddr_list_with_irk *entry;
2118 if (!use_ll_privacy(hdev))
2121 /* Check if the IRK has been programmed */
2122 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2127 cp.bdaddr_type = bdaddr_type;
2128 bacpy(&cp.bdaddr, bdaddr);
2130 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2131 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2134 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2135 bdaddr_t *bdaddr, u8 bdaddr_type)
2137 struct hci_cp_le_del_from_accept_list cp;
2140 /* Check if device is on accept list before removing it */
2141 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2144 cp.bdaddr_type = bdaddr_type;
2145 bacpy(&cp.bdaddr, bdaddr);
2147 /* Ignore errors when removing from resolving list as that is likely
2148 * that the device was never added.
2150 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2152 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2153 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2155 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2159 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2165 struct conn_params {
2168 hci_conn_flags_t flags;
2172 /* Adds connection to resolve list if needed.
2173 * Setting params to NULL programs local hdev->irk
2175 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2176 struct conn_params *params)
2178 struct hci_cp_le_add_to_resolv_list cp;
2179 struct smp_irk *irk;
2180 struct bdaddr_list_with_irk *entry;
2181 struct hci_conn_params *p;
2183 if (!use_ll_privacy(hdev))
2186 /* Attempt to program local identity address, type and irk if params is
2190 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2193 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2194 memcpy(cp.peer_irk, hdev->irk, 16);
2198 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2202 /* Check if the IK has _not_ been programmed yet. */
2203 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2209 cp.bdaddr_type = params->addr_type;
2210 bacpy(&cp.bdaddr, ¶ms->addr);
2211 memcpy(cp.peer_irk, irk->val, 16);
2213 /* Default privacy mode is always Network */
2214 params->privacy_mode = HCI_NETWORK_PRIVACY;
2217 p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2218 ¶ms->addr, params->addr_type);
2220 p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2221 ¶ms->addr, params->addr_type);
2223 WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY);
2227 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2228 memcpy(cp.local_irk, hdev->irk, 16);
2230 memset(cp.local_irk, 0, 16);
2232 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2233 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2236 /* Set Device Privacy Mode. */
2237 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2238 struct conn_params *params)
2240 struct hci_cp_le_set_privacy_mode cp;
2241 struct smp_irk *irk;
2243 /* If device privacy mode has already been set there is nothing to do */
2244 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2247 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2248 * indicates that LL Privacy has been enabled and
2249 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2251 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2254 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2258 memset(&cp, 0, sizeof(cp));
2259 cp.bdaddr_type = irk->addr_type;
2260 bacpy(&cp.bdaddr, &irk->bdaddr);
2261 cp.mode = HCI_DEVICE_PRIVACY;
2263 /* Note: params->privacy_mode is not updated since it is a copy */
2265 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2266 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2269 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2270 * this attempts to program the device in the resolving list as well and
2271 * properly set the privacy mode.
2273 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2274 struct conn_params *params,
2277 struct hci_cp_le_add_to_accept_list cp;
2280 /* During suspend, only wakeable devices can be in acceptlist */
2281 if (hdev->suspended &&
2282 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
2285 /* Select filter policy to accept all advertising */
2286 if (*num_entries >= hdev->le_accept_list_size)
2289 /* Accept list can not be used with RPAs */
2290 if (!use_ll_privacy(hdev) &&
2291 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type))
2294 /* Attempt to program the device in the resolving list first to avoid
2295 * having to rollback in case it fails since the resolving list is
2296 * dynamic it can probably be smaller than the accept list.
2298 err = hci_le_add_resolve_list_sync(hdev, params);
2300 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2304 /* Set Privacy Mode */
2305 err = hci_le_set_privacy_mode_sync(hdev, params);
2307 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2311 /* Check if already in accept list */
2312 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
2317 cp.bdaddr_type = params->addr_type;
2318 bacpy(&cp.bdaddr, ¶ms->addr);
2320 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2321 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2323 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2324 /* Rollback the device from the resolving list */
2325 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2329 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2335 /* This function disables/pause all advertising instances */
2336 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2341 /* If already been paused there is nothing to do. */
2342 if (hdev->advertising_paused)
2345 bt_dev_dbg(hdev, "Pausing directed advertising");
2347 /* Stop directed advertising */
2348 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2350 /* When discoverable timeout triggers, then just make sure
2351 * the limited discoverable flag is cleared. Even in the case
2352 * of a timeout triggered from general discoverable, it is
2353 * safe to unconditionally clear the flag.
2355 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2356 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2357 hdev->discov_timeout = 0;
2360 bt_dev_dbg(hdev, "Pausing advertising instances");
2362 /* Call to disable any advertisements active on the controller.
2363 * This will succeed even if no advertisements are configured.
2365 err = hci_disable_advertising_sync(hdev);
2369 /* If we are using software rotation, pause the loop */
2370 if (!ext_adv_capable(hdev))
2371 cancel_adv_timeout(hdev);
2373 hdev->advertising_paused = true;
2374 hdev->advertising_old_state = old_state;
2379 /* This function enables all user advertising instances */
2380 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2382 struct adv_info *adv, *tmp;
2385 /* If advertising has not been paused there is nothing to do. */
2386 if (!hdev->advertising_paused)
2389 /* Resume directed advertising */
2390 hdev->advertising_paused = false;
2391 if (hdev->advertising_old_state) {
2392 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2393 hdev->advertising_old_state = 0;
2396 bt_dev_dbg(hdev, "Resuming advertising instances");
2398 if (ext_adv_capable(hdev)) {
2399 /* Call for each tracked instance to be re-enabled */
2400 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2401 err = hci_enable_ext_advertising_sync(hdev,
2406 /* If the instance cannot be resumed remove it */
2407 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2411 /* Schedule for most recent instance to be restarted and begin
2412 * the software rotation loop
2414 err = hci_schedule_adv_instance_sync(hdev,
2415 hdev->cur_adv_instance,
2419 hdev->advertising_paused = false;
2424 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2428 if (!use_ll_privacy(hdev))
2431 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2434 /* Cannot disable addr resolution if scanning is enabled or
2435 * when initiating an LE connection.
2437 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2438 hci_lookup_le_connect(hdev)) {
2439 bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2443 /* Cannot disable addr resolution if advertising is enabled. */
2444 err = hci_pause_advertising_sync(hdev);
2446 bt_dev_err(hdev, "Pause advertising failed: %d", err);
2450 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2452 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2455 /* Return if address resolution is disabled and RPA is not used. */
2456 if (!err && scan_use_rpa(hdev))
2459 hci_resume_advertising_sync(hdev);
2463 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2464 bool extended, struct sock *sk)
2466 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2467 HCI_OP_READ_LOCAL_OOB_DATA;
2469 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2472 static struct conn_params *conn_params_copy(struct list_head *list, size_t *n)
2474 struct hci_conn_params *params;
2475 struct conn_params *p;
2481 list_for_each_entry_rcu(params, list, action)
2487 p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL);
2494 list_for_each_entry_rcu(params, list, action) {
2495 /* Racing adds are handled in next scan update */
2499 /* No hdev->lock, but: addr, addr_type are immutable.
2500 * privacy_mode is only written by us or in
2501 * hci_cc_le_set_privacy_mode that we wait for.
2502 * We should be idempotent so MGMT updating flags
2503 * while we are processing is OK.
2505 bacpy(&p[i].addr, ¶ms->addr);
2506 p[i].addr_type = params->addr_type;
2507 p[i].flags = READ_ONCE(params->flags);
2508 p[i].privacy_mode = READ_ONCE(params->privacy_mode);
2518 /* Device must not be scanning when updating the accept list.
2520 * Update is done using the following sequence:
2522 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
2523 * Remove Devices From Accept List ->
2524 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
2525 * Add Devices to Accept List ->
2526 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
2527 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
2530 * In case of failure advertising shall be restored to its original state and
2531 * return would disable accept list since either accept or resolving list could
2532 * not be programmed.
2535 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2537 struct conn_params *params;
2538 struct bdaddr_list *b, *t;
2540 bool pend_conn, pend_report;
2545 /* Pause advertising if resolving list can be used as controllers
2546 * cannot accept resolving list modifications while advertising.
2548 if (use_ll_privacy(hdev)) {
2549 err = hci_pause_advertising_sync(hdev);
2551 bt_dev_err(hdev, "pause advertising failed: %d", err);
2556 /* Disable address resolution while reprogramming accept list since
2557 * devices that do have an IRK will be programmed in the resolving list
2558 * when LL Privacy is enabled.
2560 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2562 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2566 /* Go through the current accept list programmed into the
2567 * controller one by one and check if that address is connected or is
2568 * still in the list of pending connections or list of devices to
2569 * report. If not present in either list, then remove it from
2572 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2573 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2576 /* Pointers not dereferenced, no locks needed */
2577 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2580 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2584 /* If the device is not likely to connect or report,
2585 * remove it from the acceptlist.
2587 if (!pend_conn && !pend_report) {
2588 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2596 /* Since all no longer valid accept list entries have been
2597 * removed, walk through the list of pending connections
2598 * and ensure that any new device gets programmed into
2601 * If the list of the devices is larger than the list of
2602 * available accept list entries in the controller, then
2603 * just abort and return filer policy value to not use the
2606 * The list and params may be mutated while we wait for events,
2607 * so make a copy and iterate it.
2610 params = conn_params_copy(&hdev->pend_le_conns, &n);
2616 for (i = 0; i < n; ++i) {
2617 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2627 /* After adding all new pending connections, walk through
2628 * the list of pending reports and also add these to the
2629 * accept list if there is still space. Abort if space runs out.
2632 params = conn_params_copy(&hdev->pend_le_reports, &n);
2638 for (i = 0; i < n; ++i) {
2639 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2649 /* Use the allowlist unless the following conditions are all true:
2650 * - We are not currently suspending
2651 * - There are 1 or more ADV monitors registered and it's not offloaded
2652 * - Interleaved scanning is not currently using the allowlist
2654 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2655 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2656 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2660 filter_policy = err ? 0x00 : 0x01;
2662 /* Enable address resolution when LL Privacy is enabled. */
2663 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2665 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2667 /* Resume advertising if it was paused */
2668 if (use_ll_privacy(hdev))
2669 hci_resume_advertising_sync(hdev);
2671 /* Select filter policy to use accept list */
2672 return filter_policy;
2675 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2676 u16 interval, u16 window,
2677 u8 own_addr_type, u8 filter_policy)
2679 struct hci_cp_le_set_ext_scan_params *cp;
2680 struct hci_cp_le_scan_phy_params *phy;
2681 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2685 phy = (void *)cp->data;
2687 memset(data, 0, sizeof(data));
2689 cp->own_addr_type = own_addr_type;
2690 cp->filter_policy = filter_policy;
2692 if (scan_1m(hdev) || scan_2m(hdev)) {
2693 cp->scanning_phys |= LE_SCAN_PHY_1M;
2696 phy->interval = cpu_to_le16(interval);
2697 phy->window = cpu_to_le16(window);
2703 if (scan_coded(hdev)) {
2704 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2707 phy->interval = cpu_to_le16(interval);
2708 phy->window = cpu_to_le16(window);
2714 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2715 sizeof(*cp) + sizeof(*phy) * num_phy,
2716 data, HCI_CMD_TIMEOUT);
2719 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2720 u16 interval, u16 window,
2721 u8 own_addr_type, u8 filter_policy)
2723 struct hci_cp_le_set_scan_param cp;
2725 if (use_ext_scan(hdev))
2726 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2727 window, own_addr_type,
2730 memset(&cp, 0, sizeof(cp));
2732 cp.interval = cpu_to_le16(interval);
2733 cp.window = cpu_to_le16(window);
2734 cp.own_address_type = own_addr_type;
2735 cp.filter_policy = filter_policy;
2737 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2738 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2741 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2742 u16 window, u8 own_addr_type, u8 filter_policy,
2747 if (hdev->scanning_paused) {
2748 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2752 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2753 own_addr_type, filter_policy);
2757 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2760 static int hci_passive_scan_sync(struct hci_dev *hdev)
2764 u16 window, interval;
2765 u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2768 if (hdev->scanning_paused) {
2769 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2773 err = hci_scan_disable_sync(hdev);
2775 bt_dev_err(hdev, "disable scanning failed: %d", err);
2779 /* Set require_privacy to false since no SCAN_REQ are send
2780 * during passive scanning. Not using an non-resolvable address
2781 * here is important so that peer devices using direct
2782 * advertising with our address will be correctly reported
2783 * by the controller.
2785 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2789 if (hdev->enable_advmon_interleave_scan &&
2790 hci_update_interleaved_scan_sync(hdev))
2793 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2795 /* Adding or removing entries from the accept list must
2796 * happen before enabling scanning. The controller does
2797 * not allow accept list modification while scanning.
2799 filter_policy = hci_update_accept_list_sync(hdev);
2801 /* When the controller is using random resolvable addresses and
2802 * with that having LE privacy enabled, then controllers with
2803 * Extended Scanner Filter Policies support can now enable support
2804 * for handling directed advertising.
2806 * So instead of using filter polices 0x00 (no acceptlist)
2807 * and 0x01 (acceptlist enabled) use the new filter policies
2808 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2810 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2811 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2812 filter_policy |= 0x02;
2814 if (hdev->suspended) {
2815 window = hdev->le_scan_window_suspend;
2816 interval = hdev->le_scan_int_suspend;
2817 } else if (hci_is_le_conn_scanning(hdev)) {
2818 window = hdev->le_scan_window_connect;
2819 interval = hdev->le_scan_int_connect;
2820 } else if (hci_is_adv_monitoring(hdev)) {
2821 window = hdev->le_scan_window_adv_monitor;
2822 interval = hdev->le_scan_int_adv_monitor;
2824 window = hdev->le_scan_window;
2825 interval = hdev->le_scan_interval;
2828 /* Disable all filtering for Mesh */
2829 if (hci_dev_test_flag(hdev, HCI_MESH)) {
2831 filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
2834 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2836 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2837 own_addr_type, filter_policy, filter_dups);
2840 /* This function controls the passive scanning based on hdev->pend_le_conns
2841 * list. If there are pending LE connection we start the background scanning,
2842 * otherwise we stop it in the following sequence:
2844 * If there are devices to scan:
2846 * Disable Scanning -> Update Accept List ->
2847 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2848 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2855 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2859 if (!test_bit(HCI_UP, &hdev->flags) ||
2860 test_bit(HCI_INIT, &hdev->flags) ||
2861 hci_dev_test_flag(hdev, HCI_SETUP) ||
2862 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2863 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2864 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2867 /* No point in doing scanning if LE support hasn't been enabled */
2868 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2871 /* If discovery is active don't interfere with it */
2872 if (hdev->discovery.state != DISCOVERY_STOPPED)
2875 /* Reset RSSI and UUID filters when starting background scanning
2876 * since these filters are meant for service discovery only.
2878 * The Start Discovery and Start Service Discovery operations
2879 * ensure to set proper values for RSSI threshold and UUID
2880 * filter list. So it is safe to just reset them here.
2882 hci_discovery_filter_clear(hdev);
2884 bt_dev_dbg(hdev, "ADV monitoring is %s",
2885 hci_is_adv_monitoring(hdev) ? "on" : "off");
2887 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
2888 list_empty(&hdev->pend_le_conns) &&
2889 list_empty(&hdev->pend_le_reports) &&
2890 !hci_is_adv_monitoring(hdev) &&
2891 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2892 /* If there is no pending LE connections or devices
2893 * to be scanned for or no ADV monitors, we should stop the
2894 * background scanning.
2897 bt_dev_dbg(hdev, "stopping background scanning");
2899 err = hci_scan_disable_sync(hdev);
2901 bt_dev_err(hdev, "stop background scanning failed: %d",
2904 /* If there is at least one pending LE connection, we should
2905 * keep the background scan running.
2908 /* If controller is connecting, we should not start scanning
2909 * since some controllers are not able to scan and connect at
2912 if (hci_lookup_le_connect(hdev))
2915 bt_dev_dbg(hdev, "start background scanning");
2917 err = hci_passive_scan_sync(hdev);
2919 bt_dev_err(hdev, "start background scanning failed: %d",
2926 static int update_scan_sync(struct hci_dev *hdev, void *data)
2928 return hci_update_scan_sync(hdev);
2931 int hci_update_scan(struct hci_dev *hdev)
2933 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
2936 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2938 return hci_update_passive_scan_sync(hdev);
2941 int hci_update_passive_scan(struct hci_dev *hdev)
2943 /* Only queue if it would have any effect */
2944 if (!test_bit(HCI_UP, &hdev->flags) ||
2945 test_bit(HCI_INIT, &hdev->flags) ||
2946 hci_dev_test_flag(hdev, HCI_SETUP) ||
2947 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2948 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2949 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2952 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2955 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2959 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2962 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2963 sizeof(val), &val, HCI_CMD_TIMEOUT);
2967 hdev->features[1][0] |= LMP_HOST_SC;
2968 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2970 hdev->features[1][0] &= ~LMP_HOST_SC;
2971 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2978 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2982 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2983 lmp_host_ssp_capable(hdev))
2986 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
2987 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2988 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2991 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2992 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2996 return hci_write_sc_support_sync(hdev, 0x01);
2999 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
3001 struct hci_cp_write_le_host_supported cp;
3003 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
3004 !lmp_bredr_capable(hdev))
3007 /* Check first if we already have the right host state
3008 * (host features set)
3010 if (le == lmp_host_le_capable(hdev) &&
3011 simul == lmp_host_le_br_capable(hdev))
3014 memset(&cp, 0, sizeof(cp));
3019 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3020 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3023 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3025 struct adv_info *adv, *tmp;
3028 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3031 /* If RPA Resolution has not been enable yet it means the
3032 * resolving list is empty and we should attempt to program the
3033 * local IRK in order to support using own_addr_type
3034 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
3036 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3037 hci_le_add_resolve_list_sync(hdev, NULL);
3038 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3041 /* Make sure the controller has a good default for
3042 * advertising data. This also applies to the case
3043 * where BR/EDR was toggled during the AUTO_OFF phase.
3045 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3046 list_empty(&hdev->adv_instances)) {
3047 if (ext_adv_capable(hdev)) {
3048 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3050 hci_update_scan_rsp_data_sync(hdev, 0x00);
3052 err = hci_update_adv_data_sync(hdev, 0x00);
3054 hci_update_scan_rsp_data_sync(hdev, 0x00);
3057 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3058 hci_enable_advertising_sync(hdev);
3061 /* Call for each tracked instance to be scheduled */
3062 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3063 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3068 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3072 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3073 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3076 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3077 sizeof(link_sec), &link_sec,
3081 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3083 struct hci_cp_write_page_scan_activity cp;
3087 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3090 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3093 memset(&cp, 0, sizeof(cp));
3096 type = PAGE_SCAN_TYPE_INTERLACED;
3098 /* 160 msec page scan interval */
3099 cp.interval = cpu_to_le16(0x0100);
3101 type = hdev->def_page_scan_type;
3102 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3105 cp.window = cpu_to_le16(hdev->def_page_scan_window);
3107 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3108 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3109 err = __hci_cmd_sync_status(hdev,
3110 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
3111 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3116 if (hdev->page_scan_type != type)
3117 err = __hci_cmd_sync_status(hdev,
3118 HCI_OP_WRITE_PAGE_SCAN_TYPE,
3119 sizeof(type), &type,
3125 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3127 struct bdaddr_list *b;
3129 list_for_each_entry(b, &hdev->accept_list, list) {
3130 struct hci_conn *conn;
3132 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3136 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3143 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3145 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3150 int hci_update_scan_sync(struct hci_dev *hdev)
3154 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3157 if (!hdev_is_powered(hdev))
3160 if (mgmt_powering_down(hdev))
3163 if (hdev->scanning_paused)
3166 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3167 disconnected_accept_list_entries(hdev))
3170 scan = SCAN_DISABLED;
3172 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3173 scan |= SCAN_INQUIRY;
3175 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3176 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3179 return hci_write_scan_enable_sync(hdev, scan);
3182 int hci_update_name_sync(struct hci_dev *hdev)
3184 struct hci_cp_write_local_name cp;
3186 memset(&cp, 0, sizeof(cp));
3188 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3190 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3195 /* This function perform powered update HCI command sequence after the HCI init
3196 * sequence which end up resetting all states, the sequence is as follows:
3198 * HCI_SSP_ENABLED(Enable SSP)
3199 * HCI_LE_ENABLED(Enable LE)
3200 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
3202 * Enable Authentication
3203 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3204 * Set Name -> Set EIR)
3205 * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address)
3207 int hci_powered_update_sync(struct hci_dev *hdev)
3211 /* Register the available SMP channels (BR/EDR and LE) only when
3212 * successfully powering on the controller. This late
3213 * registration is required so that LE SMP can clearly decide if
3214 * the public address or static address is used.
3218 err = hci_write_ssp_mode_sync(hdev, 0x01);
3222 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3226 err = hci_powered_update_adv_sync(hdev);
3230 err = hci_write_auth_enable_sync(hdev);
3234 if (lmp_bredr_capable(hdev)) {
3235 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3236 hci_write_fast_connectable_sync(hdev, true);
3238 hci_write_fast_connectable_sync(hdev, false);
3239 hci_update_scan_sync(hdev);
3240 hci_update_class_sync(hdev);
3241 hci_update_name_sync(hdev);
3242 hci_update_eir_sync(hdev);
3245 /* If forcing static address is in use or there is no public
3246 * address use the static address as random address (but skip
3247 * the HCI command if the current random address is already the
3250 * In case BR/EDR has been disabled on a dual-mode controller
3251 * and a static address has been configured, then use that
3252 * address instead of the public BR/EDR address.
3254 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3255 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3256 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3257 if (bacmp(&hdev->static_addr, BDADDR_ANY))
3258 return hci_set_random_addr_sync(hdev,
3259 &hdev->static_addr);
3266 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3267 * (BD_ADDR) for a HCI device from
3268 * a firmware node property.
3269 * @hdev: The HCI device
3271 * Search the firmware node for 'local-bd-address'.
3273 * All-zero BD addresses are rejected, because those could be properties
3274 * that exist in the firmware tables, but were not updated by the firmware. For
3275 * example, the DTS could define 'local-bd-address', with zero BD addresses.
3277 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3279 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3283 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3284 (u8 *)&ba, sizeof(ba));
3285 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3288 bacpy(&hdev->public_addr, &ba);
3291 struct hci_init_stage {
3292 int (*func)(struct hci_dev *hdev);
3295 /* Run init stage NULL terminated function table */
3296 static int hci_init_stage_sync(struct hci_dev *hdev,
3297 const struct hci_init_stage *stage)
3301 for (i = 0; stage[i].func; i++) {
3304 err = stage[i].func(hdev);
3312 /* Read Local Version */
3313 static int hci_read_local_version_sync(struct hci_dev *hdev)
3315 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3316 0, NULL, HCI_CMD_TIMEOUT);
3319 /* Read BD Address */
3320 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3322 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3323 0, NULL, HCI_CMD_TIMEOUT);
3326 #define HCI_INIT(_func) \
3331 static const struct hci_init_stage hci_init0[] = {
3332 /* HCI_OP_READ_LOCAL_VERSION */
3333 HCI_INIT(hci_read_local_version_sync),
3334 /* HCI_OP_READ_BD_ADDR */
3335 HCI_INIT(hci_read_bd_addr_sync),
3339 int hci_reset_sync(struct hci_dev *hdev)
3343 set_bit(HCI_RESET, &hdev->flags);
3345 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3353 static int hci_init0_sync(struct hci_dev *hdev)
3357 bt_dev_dbg(hdev, "");
3360 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3361 err = hci_reset_sync(hdev);
3366 return hci_init_stage_sync(hdev, hci_init0);
3369 static int hci_unconf_init_sync(struct hci_dev *hdev)
3373 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3376 err = hci_init0_sync(hdev);
3380 if (hci_dev_test_flag(hdev, HCI_SETUP))
3381 hci_debugfs_create_basic(hdev);
3386 /* Read Local Supported Features. */
3387 static int hci_read_local_features_sync(struct hci_dev *hdev)
3389 /* Not all AMP controllers support this command */
3390 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
3393 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3394 0, NULL, HCI_CMD_TIMEOUT);
3397 /* BR Controller init stage 1 command sequence */
3398 static const struct hci_init_stage br_init1[] = {
3399 /* HCI_OP_READ_LOCAL_FEATURES */
3400 HCI_INIT(hci_read_local_features_sync),
3401 /* HCI_OP_READ_LOCAL_VERSION */
3402 HCI_INIT(hci_read_local_version_sync),
3403 /* HCI_OP_READ_BD_ADDR */
3404 HCI_INIT(hci_read_bd_addr_sync),
3408 /* Read Local Commands */
3409 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3411 /* All Bluetooth 1.2 and later controllers should support the
3412 * HCI command for reading the local supported commands.
3414 * Unfortunately some controllers indicate Bluetooth 1.2 support,
3415 * but do not have support for this command. If that is the case,
3416 * the driver can quirk the behavior and skip reading the local
3417 * supported commands.
3419 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3420 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3421 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3422 0, NULL, HCI_CMD_TIMEOUT);
3427 /* Read Local AMP Info */
3428 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
3430 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
3431 0, NULL, HCI_CMD_TIMEOUT);
3434 /* Read Data Blk size */
3435 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
3437 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
3438 0, NULL, HCI_CMD_TIMEOUT);
3441 /* Read Flow Control Mode */
3442 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
3444 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
3445 0, NULL, HCI_CMD_TIMEOUT);
3448 /* Read Location Data */
3449 static int hci_read_location_data_sync(struct hci_dev *hdev)
3451 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
3452 0, NULL, HCI_CMD_TIMEOUT);
3455 /* AMP Controller init stage 1 command sequence */
3456 static const struct hci_init_stage amp_init1[] = {
3457 /* HCI_OP_READ_LOCAL_VERSION */
3458 HCI_INIT(hci_read_local_version_sync),
3459 /* HCI_OP_READ_LOCAL_COMMANDS */
3460 HCI_INIT(hci_read_local_cmds_sync),
3461 /* HCI_OP_READ_LOCAL_AMP_INFO */
3462 HCI_INIT(hci_read_local_amp_info_sync),
3463 /* HCI_OP_READ_DATA_BLOCK_SIZE */
3464 HCI_INIT(hci_read_data_block_size_sync),
3465 /* HCI_OP_READ_FLOW_CONTROL_MODE */
3466 HCI_INIT(hci_read_flow_control_mode_sync),
3467 /* HCI_OP_READ_LOCATION_DATA */
3468 HCI_INIT(hci_read_location_data_sync),
3472 static int hci_init1_sync(struct hci_dev *hdev)
3476 bt_dev_dbg(hdev, "");
3479 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3480 err = hci_reset_sync(hdev);
3485 switch (hdev->dev_type) {
3487 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3488 return hci_init_stage_sync(hdev, br_init1);
3490 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3491 return hci_init_stage_sync(hdev, amp_init1);
3493 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
3500 /* AMP Controller init stage 2 command sequence */
3501 static const struct hci_init_stage amp_init2[] = {
3502 /* HCI_OP_READ_LOCAL_FEATURES */
3503 HCI_INIT(hci_read_local_features_sync),
3507 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
3508 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3510 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3511 0, NULL, HCI_CMD_TIMEOUT);
3514 /* Read Class of Device */
3515 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3517 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3518 0, NULL, HCI_CMD_TIMEOUT);
3521 /* Read Local Name */
3522 static int hci_read_local_name_sync(struct hci_dev *hdev)
3524 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3525 0, NULL, HCI_CMD_TIMEOUT);
3528 /* Read Voice Setting */
3529 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3531 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3532 0, NULL, HCI_CMD_TIMEOUT);
3535 /* Read Number of Supported IAC */
3536 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3538 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3539 0, NULL, HCI_CMD_TIMEOUT);
3542 /* Read Current IAC LAP */
3543 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3545 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3546 0, NULL, HCI_CMD_TIMEOUT);
3549 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3550 u8 cond_type, bdaddr_t *bdaddr,
3553 struct hci_cp_set_event_filter cp;
3555 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3558 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3561 memset(&cp, 0, sizeof(cp));
3562 cp.flt_type = flt_type;
3564 if (flt_type != HCI_FLT_CLEAR_ALL) {
3565 cp.cond_type = cond_type;
3566 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3567 cp.addr_conn_flt.auto_accept = auto_accept;
3570 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3571 flt_type == HCI_FLT_CLEAR_ALL ?
3572 sizeof(cp.flt_type) : sizeof(cp), &cp,
3576 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3578 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3581 /* In theory the state machine should not reach here unless
3582 * a hci_set_event_filter_sync() call succeeds, but we do
3583 * the check both for parity and as a future reminder.
3585 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3588 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3592 /* Connection accept timeout ~20 secs */
3593 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3595 __le16 param = cpu_to_le16(0x7d00);
3597 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3598 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
3601 /* BR Controller init stage 2 command sequence */
3602 static const struct hci_init_stage br_init2[] = {
3603 /* HCI_OP_READ_BUFFER_SIZE */
3604 HCI_INIT(hci_read_buffer_size_sync),
3605 /* HCI_OP_READ_CLASS_OF_DEV */
3606 HCI_INIT(hci_read_dev_class_sync),
3607 /* HCI_OP_READ_LOCAL_NAME */
3608 HCI_INIT(hci_read_local_name_sync),
3609 /* HCI_OP_READ_VOICE_SETTING */
3610 HCI_INIT(hci_read_voice_setting_sync),
3611 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
3612 HCI_INIT(hci_read_num_supported_iac_sync),
3613 /* HCI_OP_READ_CURRENT_IAC_LAP */
3614 HCI_INIT(hci_read_current_iac_lap_sync),
3615 /* HCI_OP_SET_EVENT_FLT */
3616 HCI_INIT(hci_clear_event_filter_sync),
3617 /* HCI_OP_WRITE_CA_TIMEOUT */
3618 HCI_INIT(hci_write_ca_timeout_sync),
3622 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3626 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3629 /* When SSP is available, then the host features page
3630 * should also be available as well. However some
3631 * controllers list the max_page as 0 as long as SSP
3632 * has not been enabled. To achieve proper debugging
3633 * output, force the minimum max_page to 1 at least.
3635 hdev->max_page = 0x01;
3637 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3638 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3641 static int hci_write_eir_sync(struct hci_dev *hdev)
3643 struct hci_cp_write_eir cp;
3645 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3648 memset(hdev->eir, 0, sizeof(hdev->eir));
3649 memset(&cp, 0, sizeof(cp));
3651 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3655 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3659 if (!lmp_inq_rssi_capable(hdev) &&
3660 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3663 /* If Extended Inquiry Result events are supported, then
3664 * they are clearly preferred over Inquiry Result with RSSI
3667 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3669 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3670 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3673 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3675 if (!lmp_inq_tx_pwr_capable(hdev))
3678 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3679 0, NULL, HCI_CMD_TIMEOUT);
3682 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3684 struct hci_cp_read_local_ext_features cp;
3686 if (!lmp_ext_feat_capable(hdev))
3689 memset(&cp, 0, sizeof(cp));
3692 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3693 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3696 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3698 return hci_read_local_ext_features_sync(hdev, 0x01);
3701 /* HCI Controller init stage 2 command sequence */
3702 static const struct hci_init_stage hci_init2[] = {
3703 /* HCI_OP_READ_LOCAL_COMMANDS */
3704 HCI_INIT(hci_read_local_cmds_sync),
3705 /* HCI_OP_WRITE_SSP_MODE */
3706 HCI_INIT(hci_write_ssp_mode_1_sync),
3707 /* HCI_OP_WRITE_EIR */
3708 HCI_INIT(hci_write_eir_sync),
3709 /* HCI_OP_WRITE_INQUIRY_MODE */
3710 HCI_INIT(hci_write_inquiry_mode_sync),
3711 /* HCI_OP_READ_INQ_RSP_TX_POWER */
3712 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3713 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3714 HCI_INIT(hci_read_local_ext_features_1_sync),
3715 /* HCI_OP_WRITE_AUTH_ENABLE */
3716 HCI_INIT(hci_write_auth_enable_sync),
3720 /* Read LE Buffer Size */
3721 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3723 /* Use Read LE Buffer Size V2 if supported */
3724 if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3725 return __hci_cmd_sync_status(hdev,
3726 HCI_OP_LE_READ_BUFFER_SIZE_V2,
3727 0, NULL, HCI_CMD_TIMEOUT);
3729 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3730 0, NULL, HCI_CMD_TIMEOUT);
3733 /* Read LE Local Supported Features */
3734 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3736 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3737 0, NULL, HCI_CMD_TIMEOUT);
3740 /* Read LE Supported States */
3741 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3743 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3744 0, NULL, HCI_CMD_TIMEOUT);
3747 /* LE Controller init stage 2 command sequence */
3748 static const struct hci_init_stage le_init2[] = {
3749 /* HCI_OP_LE_READ_LOCAL_FEATURES */
3750 HCI_INIT(hci_le_read_local_features_sync),
3751 /* HCI_OP_LE_READ_BUFFER_SIZE */
3752 HCI_INIT(hci_le_read_buffer_size_sync),
3753 /* HCI_OP_LE_READ_SUPPORTED_STATES */
3754 HCI_INIT(hci_le_read_supported_states_sync),
3758 static int hci_init2_sync(struct hci_dev *hdev)
3762 bt_dev_dbg(hdev, "");
3764 if (hdev->dev_type == HCI_AMP)
3765 return hci_init_stage_sync(hdev, amp_init2);
3767 err = hci_init_stage_sync(hdev, hci_init2);
3771 if (lmp_bredr_capable(hdev)) {
3772 err = hci_init_stage_sync(hdev, br_init2);
3776 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3779 if (lmp_le_capable(hdev)) {
3780 err = hci_init_stage_sync(hdev, le_init2);
3783 /* LE-only controllers have LE implicitly enabled */
3784 if (!lmp_bredr_capable(hdev))
3785 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3791 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3793 /* The second byte is 0xff instead of 0x9f (two reserved bits
3794 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3795 * command otherwise.
3797 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3799 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3800 * any event mask for pre 1.2 devices.
3802 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3805 if (lmp_bredr_capable(hdev)) {
3806 events[4] |= 0x01; /* Flow Specification Complete */
3808 /* Don't set Disconnect Complete when suspended as that
3809 * would wakeup the host when disconnecting due to
3812 if (hdev->suspended)
3815 /* Use a different default for LE-only devices */
3816 memset(events, 0, sizeof(events));
3817 events[1] |= 0x20; /* Command Complete */
3818 events[1] |= 0x40; /* Command Status */
3819 events[1] |= 0x80; /* Hardware Error */
3821 /* If the controller supports the Disconnect command, enable
3822 * the corresponding event. In addition enable packet flow
3823 * control related events.
3825 if (hdev->commands[0] & 0x20) {
3826 /* Don't set Disconnect Complete when suspended as that
3827 * would wakeup the host when disconnecting due to
3830 if (!hdev->suspended)
3831 events[0] |= 0x10; /* Disconnection Complete */
3832 events[2] |= 0x04; /* Number of Completed Packets */
3833 events[3] |= 0x02; /* Data Buffer Overflow */
3836 /* If the controller supports the Read Remote Version
3837 * Information command, enable the corresponding event.
3839 if (hdev->commands[2] & 0x80)
3840 events[1] |= 0x08; /* Read Remote Version Information
3844 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3845 events[0] |= 0x80; /* Encryption Change */
3846 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3850 if (lmp_inq_rssi_capable(hdev) ||
3851 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3852 events[4] |= 0x02; /* Inquiry Result with RSSI */
3854 if (lmp_ext_feat_capable(hdev))
3855 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3857 if (lmp_esco_capable(hdev)) {
3858 events[5] |= 0x08; /* Synchronous Connection Complete */
3859 events[5] |= 0x10; /* Synchronous Connection Changed */
3862 if (lmp_sniffsubr_capable(hdev))
3863 events[5] |= 0x20; /* Sniff Subrating */
3865 if (lmp_pause_enc_capable(hdev))
3866 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3868 if (lmp_ext_inq_capable(hdev))
3869 events[5] |= 0x40; /* Extended Inquiry Result */
3871 if (lmp_no_flush_capable(hdev))
3872 events[7] |= 0x01; /* Enhanced Flush Complete */
3874 if (lmp_lsto_capable(hdev))
3875 events[6] |= 0x80; /* Link Supervision Timeout Changed */
3877 if (lmp_ssp_capable(hdev)) {
3878 events[6] |= 0x01; /* IO Capability Request */
3879 events[6] |= 0x02; /* IO Capability Response */
3880 events[6] |= 0x04; /* User Confirmation Request */
3881 events[6] |= 0x08; /* User Passkey Request */
3882 events[6] |= 0x10; /* Remote OOB Data Request */
3883 events[6] |= 0x20; /* Simple Pairing Complete */
3884 events[7] |= 0x04; /* User Passkey Notification */
3885 events[7] |= 0x08; /* Keypress Notification */
3886 events[7] |= 0x10; /* Remote Host Supported
3887 * Features Notification
3891 if (lmp_le_capable(hdev))
3892 events[7] |= 0x20; /* LE Meta-Event */
3894 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3895 sizeof(events), events, HCI_CMD_TIMEOUT);
3898 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3900 struct hci_cp_read_stored_link_key cp;
3902 if (!(hdev->commands[6] & 0x20) ||
3903 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3906 memset(&cp, 0, sizeof(cp));
3907 bacpy(&cp.bdaddr, BDADDR_ANY);
3910 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3911 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3914 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3916 struct hci_cp_write_def_link_policy cp;
3917 u16 link_policy = 0;
3919 if (!(hdev->commands[5] & 0x10))
3922 memset(&cp, 0, sizeof(cp));
3924 if (lmp_rswitch_capable(hdev))
3925 link_policy |= HCI_LP_RSWITCH;
3926 if (lmp_hold_capable(hdev))
3927 link_policy |= HCI_LP_HOLD;
3928 if (lmp_sniff_capable(hdev))
3929 link_policy |= HCI_LP_SNIFF;
3930 if (lmp_park_capable(hdev))
3931 link_policy |= HCI_LP_PARK;
3933 cp.policy = cpu_to_le16(link_policy);
3935 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3936 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3939 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3941 if (!(hdev->commands[8] & 0x01))
3944 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3945 0, NULL, HCI_CMD_TIMEOUT);
3948 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3950 if (!(hdev->commands[18] & 0x04) ||
3951 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
3952 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3955 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3956 0, NULL, HCI_CMD_TIMEOUT);
3959 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3961 /* Some older Broadcom based Bluetooth 1.2 controllers do not
3962 * support the Read Page Scan Type command. Check support for
3963 * this command in the bit mask of supported commands.
3965 if (!(hdev->commands[13] & 0x01))
3968 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3969 0, NULL, HCI_CMD_TIMEOUT);
3972 /* Read features beyond page 1 if available */
3973 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3978 if (!lmp_ext_feat_capable(hdev))
3981 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3983 err = hci_read_local_ext_features_sync(hdev, page);
3991 /* HCI Controller init stage 3 command sequence */
3992 static const struct hci_init_stage hci_init3[] = {
3993 /* HCI_OP_SET_EVENT_MASK */
3994 HCI_INIT(hci_set_event_mask_sync),
3995 /* HCI_OP_READ_STORED_LINK_KEY */
3996 HCI_INIT(hci_read_stored_link_key_sync),
3997 /* HCI_OP_WRITE_DEF_LINK_POLICY */
3998 HCI_INIT(hci_setup_link_policy_sync),
3999 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
4000 HCI_INIT(hci_read_page_scan_activity_sync),
4001 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
4002 HCI_INIT(hci_read_def_err_data_reporting_sync),
4003 /* HCI_OP_READ_PAGE_SCAN_TYPE */
4004 HCI_INIT(hci_read_page_scan_type_sync),
4005 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
4006 HCI_INIT(hci_read_local_ext_features_all_sync),
4010 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4014 if (!lmp_le_capable(hdev))
4017 memset(events, 0, sizeof(events));
4019 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4020 events[0] |= 0x10; /* LE Long Term Key Request */
4022 /* If controller supports the Connection Parameters Request
4023 * Link Layer Procedure, enable the corresponding event.
4025 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4026 /* LE Remote Connection Parameter Request */
4029 /* If the controller supports the Data Length Extension
4030 * feature, enable the corresponding event.
4032 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4033 events[0] |= 0x40; /* LE Data Length Change */
4035 /* If the controller supports LL Privacy feature or LE Extended Adv,
4036 * enable the corresponding event.
4038 if (use_enhanced_conn_complete(hdev))
4039 events[1] |= 0x02; /* LE Enhanced Connection Complete */
4041 /* If the controller supports Extended Scanner Filter
4042 * Policies, enable the corresponding event.
4044 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4045 events[1] |= 0x04; /* LE Direct Advertising Report */
4047 /* If the controller supports Channel Selection Algorithm #2
4048 * feature, enable the corresponding event.
4050 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4051 events[2] |= 0x08; /* LE Channel Selection Algorithm */
4053 /* If the controller supports the LE Set Scan Enable command,
4054 * enable the corresponding advertising report event.
4056 if (hdev->commands[26] & 0x08)
4057 events[0] |= 0x02; /* LE Advertising Report */
4059 /* If the controller supports the LE Create Connection
4060 * command, enable the corresponding event.
4062 if (hdev->commands[26] & 0x10)
4063 events[0] |= 0x01; /* LE Connection Complete */
4065 /* If the controller supports the LE Connection Update
4066 * command, enable the corresponding event.
4068 if (hdev->commands[27] & 0x04)
4069 events[0] |= 0x04; /* LE Connection Update Complete */
4071 /* If the controller supports the LE Read Remote Used Features
4072 * command, enable the corresponding event.
4074 if (hdev->commands[27] & 0x20)
4075 /* LE Read Remote Used Features Complete */
4078 /* If the controller supports the LE Read Local P-256
4079 * Public Key command, enable the corresponding event.
4081 if (hdev->commands[34] & 0x02)
4082 /* LE Read Local P-256 Public Key Complete */
4085 /* If the controller supports the LE Generate DHKey
4086 * command, enable the corresponding event.
4088 if (hdev->commands[34] & 0x04)
4089 events[1] |= 0x01; /* LE Generate DHKey Complete */
4091 /* If the controller supports the LE Set Default PHY or
4092 * LE Set PHY commands, enable the corresponding event.
4094 if (hdev->commands[35] & (0x20 | 0x40))
4095 events[1] |= 0x08; /* LE PHY Update Complete */
4097 /* If the controller supports LE Set Extended Scan Parameters
4098 * and LE Set Extended Scan Enable commands, enable the
4099 * corresponding event.
4101 if (use_ext_scan(hdev))
4102 events[1] |= 0x10; /* LE Extended Advertising Report */
4104 /* If the controller supports the LE Extended Advertising
4105 * command, enable the corresponding event.
4107 if (ext_adv_capable(hdev))
4108 events[2] |= 0x02; /* LE Advertising Set Terminated */
4110 if (cis_capable(hdev)) {
4111 events[3] |= 0x01; /* LE CIS Established */
4112 if (cis_peripheral_capable(hdev))
4113 events[3] |= 0x02; /* LE CIS Request */
4116 if (bis_capable(hdev)) {
4117 events[1] |= 0x20; /* LE PA Report */
4118 events[1] |= 0x40; /* LE PA Sync Established */
4119 events[3] |= 0x04; /* LE Create BIG Complete */
4120 events[3] |= 0x08; /* LE Terminate BIG Complete */
4121 events[3] |= 0x10; /* LE BIG Sync Established */
4122 events[3] |= 0x20; /* LE BIG Sync Loss */
4123 events[4] |= 0x02; /* LE BIG Info Advertising Report */
4126 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4127 sizeof(events), events, HCI_CMD_TIMEOUT);
4130 /* Read LE Advertising Channel TX Power */
4131 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4133 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4134 /* HCI TS spec forbids mixing of legacy and extended
4135 * advertising commands wherein READ_ADV_TX_POWER is
4136 * also included. So do not call it if extended adv
4137 * is supported otherwise controller will return
4138 * COMMAND_DISALLOWED for extended commands.
4140 return __hci_cmd_sync_status(hdev,
4141 HCI_OP_LE_READ_ADV_TX_POWER,
4142 0, NULL, HCI_CMD_TIMEOUT);
4148 /* Read LE Min/Max Tx Power*/
4149 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4151 if (!(hdev->commands[38] & 0x80) ||
4152 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4155 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4156 0, NULL, HCI_CMD_TIMEOUT);
4159 /* Read LE Accept List Size */
4160 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4162 if (!(hdev->commands[26] & 0x40))
4165 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4166 0, NULL, HCI_CMD_TIMEOUT);
4169 /* Clear LE Accept List */
4170 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
4172 if (!(hdev->commands[26] & 0x80))
4175 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
4179 /* Read LE Resolving List Size */
4180 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4182 if (!(hdev->commands[34] & 0x40))
4185 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4186 0, NULL, HCI_CMD_TIMEOUT);
4189 /* Clear LE Resolving List */
4190 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4192 if (!(hdev->commands[34] & 0x20))
4195 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4199 /* Set RPA timeout */
4200 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4202 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4204 if (!(hdev->commands[35] & 0x04) ||
4205 test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4208 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4209 sizeof(timeout), &timeout,
4213 /* Read LE Maximum Data Length */
4214 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4216 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4219 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4223 /* Read LE Suggested Default Data Length */
4224 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4226 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4229 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4233 /* Read LE Number of Supported Advertising Sets */
4234 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4236 if (!ext_adv_capable(hdev))
4239 return __hci_cmd_sync_status(hdev,
4240 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4241 0, NULL, HCI_CMD_TIMEOUT);
4244 /* Write LE Host Supported */
4245 static int hci_set_le_support_sync(struct hci_dev *hdev)
4247 struct hci_cp_write_le_host_supported cp;
4249 /* LE-only devices do not support explicit enablement */
4250 if (!lmp_bredr_capable(hdev))
4253 memset(&cp, 0, sizeof(cp));
4255 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4260 if (cp.le == lmp_host_le_capable(hdev))
4263 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4264 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4267 /* LE Set Host Feature */
4268 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4270 struct hci_cp_le_set_host_feature cp;
4272 if (!iso_capable(hdev))
4275 memset(&cp, 0, sizeof(cp));
4277 /* Isochronous Channels (Host Support) */
4281 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4282 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4285 /* LE Controller init stage 3 command sequence */
4286 static const struct hci_init_stage le_init3[] = {
4287 /* HCI_OP_LE_SET_EVENT_MASK */
4288 HCI_INIT(hci_le_set_event_mask_sync),
4289 /* HCI_OP_LE_READ_ADV_TX_POWER */
4290 HCI_INIT(hci_le_read_adv_tx_power_sync),
4291 /* HCI_OP_LE_READ_TRANSMIT_POWER */
4292 HCI_INIT(hci_le_read_tx_power_sync),
4293 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4294 HCI_INIT(hci_le_read_accept_list_size_sync),
4295 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4296 HCI_INIT(hci_le_clear_accept_list_sync),
4297 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4298 HCI_INIT(hci_le_read_resolv_list_size_sync),
4299 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
4300 HCI_INIT(hci_le_clear_resolv_list_sync),
4301 /* HCI_OP_LE_SET_RPA_TIMEOUT */
4302 HCI_INIT(hci_le_set_rpa_timeout_sync),
4303 /* HCI_OP_LE_READ_MAX_DATA_LEN */
4304 HCI_INIT(hci_le_read_max_data_len_sync),
4305 /* HCI_OP_LE_READ_DEF_DATA_LEN */
4306 HCI_INIT(hci_le_read_def_data_len_sync),
4307 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4308 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4309 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4310 HCI_INIT(hci_set_le_support_sync),
4311 /* HCI_OP_LE_SET_HOST_FEATURE */
4312 HCI_INIT(hci_le_set_host_feature_sync),
4316 static int hci_init3_sync(struct hci_dev *hdev)
4320 bt_dev_dbg(hdev, "");
4322 err = hci_init_stage_sync(hdev, hci_init3);
4326 if (lmp_le_capable(hdev))
4327 return hci_init_stage_sync(hdev, le_init3);
4332 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4334 struct hci_cp_delete_stored_link_key cp;
4336 /* Some Broadcom based Bluetooth controllers do not support the
4337 * Delete Stored Link Key command. They are clearly indicating its
4338 * absence in the bit mask of supported commands.
4340 * Check the supported commands and only if the command is marked
4341 * as supported send it. If not supported assume that the controller
4342 * does not have actual support for stored link keys which makes this
4343 * command redundant anyway.
4345 * Some controllers indicate that they support handling deleting
4346 * stored link keys, but they don't. The quirk lets a driver
4347 * just disable this command.
4349 if (!(hdev->commands[6] & 0x80) ||
4350 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4353 memset(&cp, 0, sizeof(cp));
4354 bacpy(&cp.bdaddr, BDADDR_ANY);
4355 cp.delete_all = 0x01;
4357 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4358 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4361 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4363 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4364 bool changed = false;
4366 /* Set event mask page 2 if the HCI command for it is supported */
4367 if (!(hdev->commands[22] & 0x04))
4370 /* If Connectionless Peripheral Broadcast central role is supported
4371 * enable all necessary events for it.
4373 if (lmp_cpb_central_capable(hdev)) {
4374 events[1] |= 0x40; /* Triggered Clock Capture */
4375 events[1] |= 0x80; /* Synchronization Train Complete */
4376 events[2] |= 0x08; /* Truncated Page Complete */
4377 events[2] |= 0x20; /* CPB Channel Map Change */
4381 /* If Connectionless Peripheral Broadcast peripheral role is supported
4382 * enable all necessary events for it.
4384 if (lmp_cpb_peripheral_capable(hdev)) {
4385 events[2] |= 0x01; /* Synchronization Train Received */
4386 events[2] |= 0x02; /* CPB Receive */
4387 events[2] |= 0x04; /* CPB Timeout */
4388 events[2] |= 0x10; /* Peripheral Page Response Timeout */
4392 /* Enable Authenticated Payload Timeout Expired event if supported */
4393 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4398 /* Some Broadcom based controllers indicate support for Set Event
4399 * Mask Page 2 command, but then actually do not support it. Since
4400 * the default value is all bits set to zero, the command is only
4401 * required if the event mask has to be changed. In case no change
4402 * to the event mask is needed, skip this command.
4407 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4408 sizeof(events), events, HCI_CMD_TIMEOUT);
4411 /* Read local codec list if the HCI command is supported */
4412 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4414 if (hdev->commands[45] & 0x04)
4415 hci_read_supported_codecs_v2(hdev);
4416 else if (hdev->commands[29] & 0x20)
4417 hci_read_supported_codecs(hdev);
4422 /* Read local pairing options if the HCI command is supported */
4423 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4425 if (!(hdev->commands[41] & 0x08))
4428 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4429 0, NULL, HCI_CMD_TIMEOUT);
4432 /* Get MWS transport configuration if the HCI command is supported */
4433 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4435 if (!mws_transport_config_capable(hdev))
4438 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4439 0, NULL, HCI_CMD_TIMEOUT);
4442 /* Check for Synchronization Train support */
4443 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4445 if (!lmp_sync_train_capable(hdev))
4448 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4449 0, NULL, HCI_CMD_TIMEOUT);
4452 /* Enable Secure Connections if supported and configured */
4453 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4457 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4458 !bredr_sc_enabled(hdev))
4461 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4462 sizeof(support), &support,
4466 /* Set erroneous data reporting if supported to the wideband speech
4469 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4471 struct hci_cp_write_def_err_data_reporting cp;
4472 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4474 if (!(hdev->commands[18] & 0x08) ||
4475 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4476 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4479 if (enabled == hdev->err_data_reporting)
4482 memset(&cp, 0, sizeof(cp));
4483 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4484 ERR_DATA_REPORTING_DISABLED;
4486 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4487 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4490 static const struct hci_init_stage hci_init4[] = {
4491 /* HCI_OP_DELETE_STORED_LINK_KEY */
4492 HCI_INIT(hci_delete_stored_link_key_sync),
4493 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4494 HCI_INIT(hci_set_event_mask_page_2_sync),
4495 /* HCI_OP_READ_LOCAL_CODECS */
4496 HCI_INIT(hci_read_local_codecs_sync),
4497 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4498 HCI_INIT(hci_read_local_pairing_opts_sync),
4499 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4500 HCI_INIT(hci_get_mws_transport_config_sync),
4501 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4502 HCI_INIT(hci_read_sync_train_params_sync),
4503 /* HCI_OP_WRITE_SC_SUPPORT */
4504 HCI_INIT(hci_write_sc_support_1_sync),
4505 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4506 HCI_INIT(hci_set_err_data_report_sync),
4510 /* Set Suggested Default Data Length to maximum if supported */
4511 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4513 struct hci_cp_le_write_def_data_len cp;
4515 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4518 memset(&cp, 0, sizeof(cp));
4519 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4520 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4522 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4523 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4526 /* Set Default PHY parameters if command is supported, enables all supported
4527 * PHYs according to the LE Features bits.
4529 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4531 struct hci_cp_le_set_default_phy cp;
4533 if (!(hdev->commands[35] & 0x20)) {
4534 /* If the command is not supported it means only 1M PHY is
4537 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4538 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4542 memset(&cp, 0, sizeof(cp));
4544 cp.tx_phys = HCI_LE_SET_PHY_1M;
4545 cp.rx_phys = HCI_LE_SET_PHY_1M;
4547 /* Enables 2M PHY if supported */
4548 if (le_2m_capable(hdev)) {
4549 cp.tx_phys |= HCI_LE_SET_PHY_2M;
4550 cp.rx_phys |= HCI_LE_SET_PHY_2M;
4553 /* Enables Coded PHY if supported */
4554 if (le_coded_capable(hdev)) {
4555 cp.tx_phys |= HCI_LE_SET_PHY_CODED;
4556 cp.rx_phys |= HCI_LE_SET_PHY_CODED;
4559 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4560 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4563 static const struct hci_init_stage le_init4[] = {
4564 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4565 HCI_INIT(hci_le_set_write_def_data_len_sync),
4566 /* HCI_OP_LE_SET_DEFAULT_PHY */
4567 HCI_INIT(hci_le_set_default_phy_sync),
4571 static int hci_init4_sync(struct hci_dev *hdev)
4575 bt_dev_dbg(hdev, "");
4577 err = hci_init_stage_sync(hdev, hci_init4);
4581 if (lmp_le_capable(hdev))
4582 return hci_init_stage_sync(hdev, le_init4);
4587 static int hci_init_sync(struct hci_dev *hdev)
4591 err = hci_init1_sync(hdev);
4595 if (hci_dev_test_flag(hdev, HCI_SETUP))
4596 hci_debugfs_create_basic(hdev);
4598 err = hci_init2_sync(hdev);
4602 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
4603 * BR/EDR/LE type controllers. AMP controllers only need the
4604 * first two stages of init.
4606 if (hdev->dev_type != HCI_PRIMARY)
4609 err = hci_init3_sync(hdev);
4613 err = hci_init4_sync(hdev);
4617 /* This function is only called when the controller is actually in
4618 * configured state. When the controller is marked as unconfigured,
4619 * this initialization procedure is not run.
4621 * It means that it is possible that a controller runs through its
4622 * setup phase and then discovers missing settings. If that is the
4623 * case, then this function will not be called. It then will only
4624 * be called during the config phase.
4626 * So only when in setup phase or config phase, create the debugfs
4627 * entries and register the SMP channels.
4629 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4630 !hci_dev_test_flag(hdev, HCI_CONFIG))
4633 if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4636 hci_debugfs_create_common(hdev);
4638 if (lmp_bredr_capable(hdev))
4639 hci_debugfs_create_bredr(hdev);
4641 if (lmp_le_capable(hdev))
4642 hci_debugfs_create_le(hdev);
4647 #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
4649 static const struct {
4650 unsigned long quirk;
4652 } hci_broken_table[] = {
4653 HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
4654 "HCI Read Local Supported Commands not supported"),
4655 HCI_QUIRK_BROKEN(STORED_LINK_KEY,
4656 "HCI Delete Stored Link Key command is advertised, "
4657 "but not supported."),
4658 HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
4659 "HCI Read Default Erroneous Data Reporting command is "
4660 "advertised, but not supported."),
4661 HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
4662 "HCI Read Transmit Power Level command is advertised, "
4663 "but not supported."),
4664 HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
4665 "HCI Set Event Filter command not supported."),
4666 HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
4667 "HCI Enhanced Setup Synchronous Connection command is "
4668 "advertised, but not supported."),
4669 HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
4670 "HCI LE Set Random Private Address Timeout command is "
4671 "advertised, but not supported."),
4672 HCI_QUIRK_BROKEN(LE_CODED,
4673 "HCI LE Coded PHY feature bit is set, "
4674 "but its usage is not supported.")
4677 /* This function handles hdev setup stage:
4680 * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
4682 static int hci_dev_setup_sync(struct hci_dev *hdev)
4685 bool invalid_bdaddr;
4688 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4689 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4692 bt_dev_dbg(hdev, "");
4694 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4697 ret = hdev->setup(hdev);
4699 for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
4700 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4701 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4704 /* The transport driver can set the quirk to mark the
4705 * BD_ADDR invalid before creating the HCI device or in
4706 * its setup callback.
4708 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
4709 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
4711 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4712 !bacmp(&hdev->public_addr, BDADDR_ANY))
4713 hci_dev_get_bd_addr_from_property(hdev);
4715 if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
4717 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4719 invalid_bdaddr = false;
4723 /* The transport driver can set these quirks before
4724 * creating the HCI device or in its setup callback.
4726 * For the invalid BD_ADDR quirk it is possible that
4727 * it becomes a valid address if the bootloader does
4728 * provide it (see above).
4730 * In case any of them is set, the controller has to
4731 * start up as unconfigured.
4733 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4735 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4737 /* For an unconfigured controller it is required to
4738 * read at least the version information provided by
4739 * the Read Local Version Information command.
4741 * If the set_bdaddr driver callback is provided, then
4742 * also the original Bluetooth public device address
4743 * will be read using the Read BD Address command.
4745 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4746 return hci_unconf_init_sync(hdev);
4751 /* This function handles hdev init stage:
4753 * Calls hci_dev_setup_sync to perform setup stage
4754 * Calls hci_init_sync to perform HCI command init sequence
4756 static int hci_dev_init_sync(struct hci_dev *hdev)
4760 bt_dev_dbg(hdev, "");
4762 atomic_set(&hdev->cmd_cnt, 1);
4763 set_bit(HCI_INIT, &hdev->flags);
4765 ret = hci_dev_setup_sync(hdev);
4767 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4768 /* If public address change is configured, ensure that
4769 * the address gets programmed. If the driver does not
4770 * support changing the public address, fail the power
4773 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4775 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4777 ret = -EADDRNOTAVAIL;
4781 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4782 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4783 ret = hci_init_sync(hdev);
4784 if (!ret && hdev->post_init)
4785 ret = hdev->post_init(hdev);
4789 /* If the HCI Reset command is clearing all diagnostic settings,
4790 * then they need to be reprogrammed after the init procedure
4793 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4794 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4795 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4796 ret = hdev->set_diag(hdev, true);
4798 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4803 clear_bit(HCI_INIT, &hdev->flags);
4808 int hci_dev_open_sync(struct hci_dev *hdev)
4812 bt_dev_dbg(hdev, "");
4814 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4819 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4820 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4821 /* Check for rfkill but allow the HCI setup stage to
4822 * proceed (which in itself doesn't cause any RF activity).
4824 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4829 /* Check for valid public address or a configured static
4830 * random address, but let the HCI setup proceed to
4831 * be able to determine if there is a public address
4834 * In case of user channel usage, it is not important
4835 * if a public address or static random address is
4838 * This check is only valid for BR/EDR controllers
4839 * since AMP controllers do not have an address.
4841 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4842 hdev->dev_type == HCI_PRIMARY &&
4843 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4844 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4845 ret = -EADDRNOTAVAIL;
4850 if (test_bit(HCI_UP, &hdev->flags)) {
4855 if (hdev->open(hdev)) {
4860 hci_devcd_reset(hdev);
4862 set_bit(HCI_RUNNING, &hdev->flags);
4863 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4865 ret = hci_dev_init_sync(hdev);
4868 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4869 hci_adv_instances_set_rpa_expired(hdev, true);
4870 set_bit(HCI_UP, &hdev->flags);
4871 hci_sock_dev_event(hdev, HCI_DEV_UP);
4872 hci_leds_update_powered(hdev, true);
4873 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4874 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4875 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4876 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4877 hci_dev_test_flag(hdev, HCI_MGMT) &&
4878 hdev->dev_type == HCI_PRIMARY) {
4879 ret = hci_powered_update_sync(hdev);
4880 mgmt_power_on(hdev, ret);
4883 /* Init failed, cleanup */
4884 flush_work(&hdev->tx_work);
4886 /* Since hci_rx_work() is possible to awake new cmd_work
4887 * it should be flushed first to avoid unexpected call of
4890 flush_work(&hdev->rx_work);
4891 flush_work(&hdev->cmd_work);
4893 skb_queue_purge(&hdev->cmd_q);
4894 skb_queue_purge(&hdev->rx_q);
4899 if (hdev->sent_cmd) {
4900 cancel_delayed_work_sync(&hdev->cmd_timer);
4901 kfree_skb(hdev->sent_cmd);
4902 hdev->sent_cmd = NULL;
4905 clear_bit(HCI_RUNNING, &hdev->flags);
4906 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4909 hdev->flags &= BIT(HCI_RAW);
4916 /* This function requires the caller holds hdev->lock */
4917 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4919 struct hci_conn_params *p;
4921 list_for_each_entry(p, &hdev->le_conn_params, list) {
4922 hci_pend_le_list_del_init(p);
4924 hci_conn_drop(p->conn);
4925 hci_conn_put(p->conn);
4930 BT_DBG("All LE pending actions cleared");
4933 static int hci_dev_shutdown(struct hci_dev *hdev)
4936 /* Similar to how we first do setup and then set the exclusive access
4937 * bit for userspace, we must first unset userchannel and then clean up.
4938 * Otherwise, the kernel can't properly use the hci channel to clean up
4939 * the controller (some shutdown routines require sending additional
4940 * commands to the controller for example).
4942 bool was_userchannel =
4943 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
4945 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4946 test_bit(HCI_UP, &hdev->flags)) {
4947 /* Execute vendor specific shutdown routine */
4949 err = hdev->shutdown(hdev);
4952 if (was_userchannel)
4953 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
4958 int hci_dev_close_sync(struct hci_dev *hdev)
4963 bt_dev_dbg(hdev, "");
4965 cancel_delayed_work(&hdev->power_off);
4966 cancel_delayed_work(&hdev->ncmd_timer);
4967 cancel_delayed_work(&hdev->le_scan_disable);
4968 cancel_delayed_work(&hdev->le_scan_restart);
4970 hci_request_cancel_all(hdev);
4972 if (hdev->adv_instance_timeout) {
4973 cancel_delayed_work_sync(&hdev->adv_instance_expire);
4974 hdev->adv_instance_timeout = 0;
4977 err = hci_dev_shutdown(hdev);
4979 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4980 cancel_delayed_work_sync(&hdev->cmd_timer);
4984 hci_leds_update_powered(hdev, false);
4986 /* Flush RX and TX works */
4987 flush_work(&hdev->tx_work);
4988 flush_work(&hdev->rx_work);
4990 if (hdev->discov_timeout > 0) {
4991 hdev->discov_timeout = 0;
4992 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4993 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
4996 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
4997 cancel_delayed_work(&hdev->service_cache);
4999 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
5000 struct adv_info *adv_instance;
5002 cancel_delayed_work_sync(&hdev->rpa_expired);
5004 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5005 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
5008 /* Avoid potential lockdep warnings from the *_flush() calls by
5009 * ensuring the workqueue is empty up front.
5011 drain_workqueue(hdev->workqueue);
5015 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5017 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5019 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
5020 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5021 hci_dev_test_flag(hdev, HCI_MGMT))
5022 __mgmt_power_off(hdev);
5024 hci_inquiry_cache_flush(hdev);
5025 hci_pend_le_actions_clear(hdev);
5026 hci_conn_hash_flush(hdev);
5027 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5028 smp_unregister(hdev);
5029 hci_dev_unlock(hdev);
5031 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5033 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5034 aosp_do_close(hdev);
5035 msft_do_close(hdev);
5042 skb_queue_purge(&hdev->cmd_q);
5043 atomic_set(&hdev->cmd_cnt, 1);
5044 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5045 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5046 set_bit(HCI_INIT, &hdev->flags);
5047 hci_reset_sync(hdev);
5048 clear_bit(HCI_INIT, &hdev->flags);
5051 /* flush cmd work */
5052 flush_work(&hdev->cmd_work);
5055 skb_queue_purge(&hdev->rx_q);
5056 skb_queue_purge(&hdev->cmd_q);
5057 skb_queue_purge(&hdev->raw_q);
5059 /* Drop last sent command */
5060 if (hdev->sent_cmd) {
5061 cancel_delayed_work_sync(&hdev->cmd_timer);
5062 kfree_skb(hdev->sent_cmd);
5063 hdev->sent_cmd = NULL;
5066 clear_bit(HCI_RUNNING, &hdev->flags);
5067 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5069 /* After this point our queues are empty and no tasks are scheduled. */
5073 hdev->flags &= BIT(HCI_RAW);
5074 hci_dev_clear_volatile_flags(hdev);
5076 /* Controller radio is available but is currently powered down */
5077 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
5079 memset(hdev->eir, 0, sizeof(hdev->eir));
5080 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5081 bacpy(&hdev->random_addr, BDADDR_ANY);
5087 /* This function perform power on HCI command sequence as follows:
5089 * If controller is already up (HCI_UP) performs hci_powered_update_sync
5090 * sequence otherwise run hci_dev_open_sync which will follow with
5091 * hci_powered_update_sync after the init sequence is completed.
5093 static int hci_power_on_sync(struct hci_dev *hdev)
5097 if (test_bit(HCI_UP, &hdev->flags) &&
5098 hci_dev_test_flag(hdev, HCI_MGMT) &&
5099 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5100 cancel_delayed_work(&hdev->power_off);
5101 return hci_powered_update_sync(hdev);
5104 err = hci_dev_open_sync(hdev);
5108 /* During the HCI setup phase, a few error conditions are
5109 * ignored and they need to be checked now. If they are still
5110 * valid, it is important to return the device back off.
5112 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5113 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5114 (hdev->dev_type == HCI_PRIMARY &&
5115 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5116 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5117 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5118 hci_dev_close_sync(hdev);
5119 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5120 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5121 HCI_AUTO_OFF_TIMEOUT);
5124 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5125 /* For unconfigured devices, set the HCI_RAW flag
5126 * so that userspace can easily identify them.
5128 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5129 set_bit(HCI_RAW, &hdev->flags);
5131 /* For fully configured devices, this will send
5132 * the Index Added event. For unconfigured devices,
5133 * it will send Unconfigued Index Added event.
5135 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
5136 * and no event will be send.
5138 mgmt_index_added(hdev);
5139 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5140 /* When the controller is now configured, then it
5141 * is important to clear the HCI_RAW flag.
5143 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5144 clear_bit(HCI_RAW, &hdev->flags);
5146 /* Powering on the controller with HCI_CONFIG set only
5147 * happens with the transition from unconfigured to
5148 * configured. This will send the Index Added event.
5150 mgmt_index_added(hdev);
5156 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5158 struct hci_cp_remote_name_req_cancel cp;
5160 memset(&cp, 0, sizeof(cp));
5161 bacpy(&cp.bdaddr, addr);
5163 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5164 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5167 int hci_stop_discovery_sync(struct hci_dev *hdev)
5169 struct discovery_state *d = &hdev->discovery;
5170 struct inquiry_entry *e;
5173 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5175 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
5176 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5177 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5178 0, NULL, HCI_CMD_TIMEOUT);
5183 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5184 cancel_delayed_work(&hdev->le_scan_disable);
5185 cancel_delayed_work(&hdev->le_scan_restart);
5187 err = hci_scan_disable_sync(hdev);
5193 err = hci_scan_disable_sync(hdev);
5198 /* Resume advertising if it was paused */
5199 if (use_ll_privacy(hdev))
5200 hci_resume_advertising_sync(hdev);
5202 /* No further actions needed for LE-only discovery */
5203 if (d->type == DISCOV_TYPE_LE)
5206 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5207 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5212 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5218 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
5221 struct hci_cp_disconn_phy_link cp;
5223 memset(&cp, 0, sizeof(cp));
5224 cp.phy_handle = HCI_PHY_HANDLE(handle);
5227 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
5228 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5231 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5234 struct hci_cp_disconnect cp;
5236 if (conn->type == AMP_LINK)
5237 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
5239 memset(&cp, 0, sizeof(cp));
5240 cp.handle = cpu_to_le16(conn->handle);
5243 /* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5244 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5245 * used when suspending or powering off, where we don't want to wait
5246 * for the peer's response.
5248 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5249 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5251 HCI_EV_DISCONN_COMPLETE,
5252 HCI_CMD_TIMEOUT, NULL);
5254 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5258 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5259 struct hci_conn *conn, u8 reason)
5261 /* Return reason if scanning since the connection shall probably be
5264 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5267 if (conn->role == HCI_ROLE_SLAVE ||
5268 test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
5271 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5272 0, NULL, HCI_CMD_TIMEOUT);
5275 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5278 if (conn->type == LE_LINK)
5279 return hci_le_connect_cancel_sync(hdev, conn, reason);
5281 if (conn->type == ISO_LINK) {
5282 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
5285 * If this command is issued for a CIS on the Central and the
5286 * CIS is successfully terminated before being established,
5287 * then an HCI_LE_CIS_Established event shall also be sent for
5288 * this CIS with the Status Operation Cancelled by Host (0x44).
5290 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
5291 return hci_disconnect_sync(hdev, conn, reason);
5293 /* CIS with no Create CIS sent have nothing to cancel */
5294 if (bacmp(&conn->dst, BDADDR_ANY))
5295 return HCI_ERROR_LOCAL_HOST_TERM;
5297 /* There is no way to cancel a BIS without terminating the BIG
5298 * which is done later on connection cleanup.
5303 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5306 /* Wait for HCI_EV_CONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5307 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5308 * used when suspending or powering off, where we don't want to wait
5309 * for the peer's response.
5311 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5312 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5314 HCI_EV_CONN_COMPLETE,
5315 HCI_CMD_TIMEOUT, NULL);
5317 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5318 6, &conn->dst, HCI_CMD_TIMEOUT);
5321 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5324 struct hci_cp_reject_sync_conn_req cp;
5326 memset(&cp, 0, sizeof(cp));
5327 bacpy(&cp.bdaddr, &conn->dst);
5330 /* SCO rejection has its own limited set of
5331 * allowed error values (0x0D-0x0F).
5333 if (reason < 0x0d || reason > 0x0f)
5334 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5336 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5337 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5340 static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
5343 struct hci_cp_le_reject_cis cp;
5345 memset(&cp, 0, sizeof(cp));
5346 cp.handle = cpu_to_le16(conn->handle);
5349 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
5350 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5353 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5356 struct hci_cp_reject_conn_req cp;
5358 if (conn->type == ISO_LINK)
5359 return hci_le_reject_cis_sync(hdev, conn, reason);
5361 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5362 return hci_reject_sco_sync(hdev, conn, reason);
5364 memset(&cp, 0, sizeof(cp));
5365 bacpy(&cp.bdaddr, &conn->dst);
5368 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5369 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5372 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5375 u16 handle = conn->handle;
5378 switch (conn->state) {
5381 err = hci_disconnect_sync(hdev, conn, reason);
5384 err = hci_connect_cancel_sync(hdev, conn, reason);
5387 err = hci_reject_conn_sync(hdev, conn, reason);
5392 /* Cleanup bis or pa sync connections */
5393 if (test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags) ||
5394 test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags)) {
5395 hci_conn_failed(conn, reason);
5396 } else if (test_bit(HCI_CONN_PA_SYNC, &conn->flags) ||
5397 test_bit(HCI_CONN_BIG_SYNC, &conn->flags)) {
5398 conn->state = BT_CLOSED;
5399 hci_disconn_cfm(conn, reason);
5403 hci_dev_unlock(hdev);
5407 hci_conn_failed(conn, reason);
5408 hci_dev_unlock(hdev);
5412 conn->state = BT_CLOSED;
5413 hci_disconn_cfm(conn, reason);
5415 hci_dev_unlock(hdev);
5421 /* Check if the connection hasn't been cleanup while waiting
5422 * commands to complete.
5424 c = hci_conn_hash_lookup_handle(hdev, handle);
5425 if (!c || c != conn) {
5430 /* Cleanup hci_conn object if it cannot be cancelled as it
5431 * likelly means the controller and host stack are out of sync
5432 * or in case of LE it was still scanning so it can be cleanup
5435 hci_conn_failed(conn, reason);
5438 hci_dev_unlock(hdev);
5442 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5444 struct list_head *head = &hdev->conn_hash.list;
5445 struct hci_conn *conn;
5448 while ((conn = list_first_or_null_rcu(head, struct hci_conn, list))) {
5449 /* Make sure the connection is not freed while unlocking */
5450 conn = hci_conn_get(conn);
5452 /* Disregard possible errors since hci_conn_del shall have been
5453 * called even in case of errors had occurred since it would
5454 * then cause hci_conn_failed to be called which calls
5455 * hci_conn_del internally.
5457 hci_abort_conn_sync(hdev, conn, reason);
5466 /* This function perform power off HCI command sequence as follows:
5470 * Disconnect all connections
5471 * hci_dev_close_sync
5473 static int hci_power_off_sync(struct hci_dev *hdev)
5477 /* If controller is already down there is nothing to do */
5478 if (!test_bit(HCI_UP, &hdev->flags))
5481 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5482 test_bit(HCI_PSCAN, &hdev->flags)) {
5483 err = hci_write_scan_enable_sync(hdev, 0x00);
5488 err = hci_clear_adv_sync(hdev, NULL, false);
5492 err = hci_stop_discovery_sync(hdev);
5496 /* Terminated due to Power Off */
5497 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5501 return hci_dev_close_sync(hdev);
5504 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5507 return hci_power_on_sync(hdev);
5509 return hci_power_off_sync(hdev);
5512 static int hci_write_iac_sync(struct hci_dev *hdev)
5514 struct hci_cp_write_current_iac_lap cp;
5516 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5519 memset(&cp, 0, sizeof(cp));
5521 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5522 /* Limited discoverable mode */
5523 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5524 cp.iac_lap[0] = 0x00; /* LIAC */
5525 cp.iac_lap[1] = 0x8b;
5526 cp.iac_lap[2] = 0x9e;
5527 cp.iac_lap[3] = 0x33; /* GIAC */
5528 cp.iac_lap[4] = 0x8b;
5529 cp.iac_lap[5] = 0x9e;
5531 /* General discoverable mode */
5533 cp.iac_lap[0] = 0x33; /* GIAC */
5534 cp.iac_lap[1] = 0x8b;
5535 cp.iac_lap[2] = 0x9e;
5538 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5539 (cp.num_iac * 3) + 1, &cp,
5543 int hci_update_discoverable_sync(struct hci_dev *hdev)
5547 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5548 err = hci_write_iac_sync(hdev);
5552 err = hci_update_scan_sync(hdev);
5556 err = hci_update_class_sync(hdev);
5561 /* Advertising instances don't use the global discoverable setting, so
5562 * only update AD if advertising was enabled using Set Advertising.
5564 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5565 err = hci_update_adv_data_sync(hdev, 0x00);
5569 /* Discoverable mode affects the local advertising
5570 * address in limited privacy mode.
5572 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5573 if (ext_adv_capable(hdev))
5574 err = hci_start_ext_adv_sync(hdev, 0x00);
5576 err = hci_enable_advertising_sync(hdev);
5583 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5585 return hci_update_discoverable_sync(hdev);
5588 int hci_update_discoverable(struct hci_dev *hdev)
5590 /* Only queue if it would have any effect */
5591 if (hdev_is_powered(hdev) &&
5592 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5593 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5594 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5595 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5601 int hci_update_connectable_sync(struct hci_dev *hdev)
5605 err = hci_update_scan_sync(hdev);
5609 /* If BR/EDR is not enabled and we disable advertising as a
5610 * by-product of disabling connectable, we need to update the
5611 * advertising flags.
5613 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5614 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5616 /* Update the advertising parameters if necessary */
5617 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5618 !list_empty(&hdev->adv_instances)) {
5619 if (ext_adv_capable(hdev))
5620 err = hci_start_ext_adv_sync(hdev,
5621 hdev->cur_adv_instance);
5623 err = hci_enable_advertising_sync(hdev);
5629 return hci_update_passive_scan_sync(hdev);
5632 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5634 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5635 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5636 struct hci_cp_inquiry cp;
5638 bt_dev_dbg(hdev, "");
5640 if (hci_dev_test_flag(hdev, HCI_INQUIRY))
5644 hci_inquiry_cache_flush(hdev);
5645 hci_dev_unlock(hdev);
5647 memset(&cp, 0, sizeof(cp));
5649 if (hdev->discovery.limited)
5650 memcpy(&cp.lap, liac, sizeof(cp.lap));
5652 memcpy(&cp.lap, giac, sizeof(cp.lap));
5656 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5657 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5660 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5663 /* Accept list is not used for discovery */
5664 u8 filter_policy = 0x00;
5665 /* Default is to enable duplicates filter */
5666 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5669 bt_dev_dbg(hdev, "");
5671 /* If controller is scanning, it means the passive scanning is
5672 * running. Thus, we should temporarily stop it in order to set the
5673 * discovery scanning parameters.
5675 err = hci_scan_disable_sync(hdev);
5677 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5681 cancel_interleave_scan(hdev);
5683 /* Pause address resolution for active scan and stop advertising if
5684 * privacy is enabled.
5686 err = hci_pause_addr_resolution(hdev);
5690 /* All active scans will be done with either a resolvable private
5691 * address (when privacy feature has been enabled) or non-resolvable
5694 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5697 own_addr_type = ADDR_LE_DEV_PUBLIC;
5699 if (hci_is_adv_monitoring(hdev)) {
5700 /* Duplicate filter should be disabled when some advertisement
5701 * monitor is activated, otherwise AdvMon can only receive one
5702 * advertisement for one peer(*) during active scanning, and
5703 * might report loss to these peers.
5705 * Note that different controllers have different meanings of
5706 * |duplicate|. Some of them consider packets with the same
5707 * address as duplicate, and others consider packets with the
5708 * same address and the same RSSI as duplicate. Although in the
5709 * latter case we don't need to disable duplicate filter, but
5710 * it is common to have active scanning for a short period of
5711 * time, the power impact should be neglectable.
5713 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5716 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5717 hdev->le_scan_window_discovery,
5718 own_addr_type, filter_policy, filter_dup);
5723 /* Resume advertising if it was paused */
5724 if (use_ll_privacy(hdev))
5725 hci_resume_advertising_sync(hdev);
5727 /* Resume passive scanning */
5728 hci_update_passive_scan_sync(hdev);
5732 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5736 bt_dev_dbg(hdev, "");
5738 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5742 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5745 int hci_start_discovery_sync(struct hci_dev *hdev)
5747 unsigned long timeout;
5750 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5752 switch (hdev->discovery.type) {
5753 case DISCOV_TYPE_BREDR:
5754 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5755 case DISCOV_TYPE_INTERLEAVED:
5756 /* When running simultaneous discovery, the LE scanning time
5757 * should occupy the whole discovery time sine BR/EDR inquiry
5758 * and LE scanning are scheduled by the controller.
5760 * For interleaving discovery in comparison, BR/EDR inquiry
5761 * and LE scanning are done sequentially with separate
5764 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5766 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5767 /* During simultaneous discovery, we double LE scan
5768 * interval. We must leave some time for the controller
5769 * to do BR/EDR inquiry.
5771 err = hci_start_interleaved_discovery_sync(hdev);
5775 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5776 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5778 case DISCOV_TYPE_LE:
5779 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5780 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5789 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5791 /* When service discovery is used and the controller has a
5792 * strict duplicate filter, it is important to remember the
5793 * start and duration of the scan. This is required for
5794 * restarting scanning during the discovery phase.
5796 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5797 hdev->discovery.result_filtering) {
5798 hdev->discovery.scan_start = jiffies;
5799 hdev->discovery.scan_duration = timeout;
5802 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5807 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5809 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5810 case HCI_ADV_MONITOR_EXT_MSFT:
5811 msft_suspend_sync(hdev);
5818 /* This function disables discovery and mark it as paused */
5819 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5821 int old_state = hdev->discovery.state;
5824 /* If discovery already stopped/stopping/paused there nothing to do */
5825 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
5826 hdev->discovery_paused)
5829 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5830 err = hci_stop_discovery_sync(hdev);
5834 hdev->discovery_paused = true;
5835 hdev->discovery_old_state = old_state;
5836 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5841 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5843 struct bdaddr_list_with_flags *b;
5844 u8 scan = SCAN_DISABLED;
5845 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5848 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5851 /* Some fake CSR controllers lock up after setting this type of
5852 * filter, so avoid sending the request altogether.
5854 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5857 /* Always clear event filter when starting */
5858 hci_clear_event_filter_sync(hdev);
5860 list_for_each_entry(b, &hdev->accept_list, list) {
5861 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
5864 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5866 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5867 HCI_CONN_SETUP_ALLOW_BDADDR,
5869 HCI_CONN_SETUP_AUTO_ON);
5871 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5877 if (scan && !scanning)
5878 hci_write_scan_enable_sync(hdev, scan);
5879 else if (!scan && scanning)
5880 hci_write_scan_enable_sync(hdev, scan);
5885 /* This function disables scan (BR and LE) and mark it as paused */
5886 static int hci_pause_scan_sync(struct hci_dev *hdev)
5888 if (hdev->scanning_paused)
5891 /* Disable page scan if enabled */
5892 if (test_bit(HCI_PSCAN, &hdev->flags))
5893 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
5895 hci_scan_disable_sync(hdev);
5897 hdev->scanning_paused = true;
5902 /* This function performs the HCI suspend procedures in the follow order:
5904 * Pause discovery (active scanning/inquiry)
5905 * Pause Directed Advertising/Advertising
5906 * Pause Scanning (passive scanning in case discovery was not active)
5907 * Disconnect all connections
5908 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5910 * Update event mask (only set events that are allowed to wake up the host)
5911 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
5912 * Update passive scanning (lower duty cycle)
5913 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
5915 int hci_suspend_sync(struct hci_dev *hdev)
5919 /* If marked as suspended there nothing to do */
5920 if (hdev->suspended)
5923 /* Mark device as suspended */
5924 hdev->suspended = true;
5926 /* Pause discovery if not already stopped */
5927 hci_pause_discovery_sync(hdev);
5929 /* Pause other advertisements */
5930 hci_pause_advertising_sync(hdev);
5932 /* Suspend monitor filters */
5933 hci_suspend_monitor_sync(hdev);
5935 /* Prevent disconnects from causing scanning to be re-enabled */
5936 hci_pause_scan_sync(hdev);
5938 if (hci_conn_count(hdev)) {
5939 /* Soft disconnect everything (power off) */
5940 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5942 /* Set state to BT_RUNNING so resume doesn't notify */
5943 hdev->suspend_state = BT_RUNNING;
5944 hci_resume_sync(hdev);
5948 /* Update event mask so only the allowed event can wakeup the
5951 hci_set_event_mask_sync(hdev);
5954 /* Only configure accept list if disconnect succeeded and wake
5955 * isn't being prevented.
5957 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5958 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5962 /* Unpause to take care of updating scanning params */
5963 hdev->scanning_paused = false;
5965 /* Enable event filter for paired devices */
5966 hci_update_event_filter_sync(hdev);
5968 /* Update LE passive scan if enabled */
5969 hci_update_passive_scan_sync(hdev);
5971 /* Pause scan changes again. */
5972 hdev->scanning_paused = true;
5974 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5979 /* This function resumes discovery */
5980 static int hci_resume_discovery_sync(struct hci_dev *hdev)
5984 /* If discovery not paused there nothing to do */
5985 if (!hdev->discovery_paused)
5988 hdev->discovery_paused = false;
5990 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
5992 err = hci_start_discovery_sync(hdev);
5994 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
6000 static void hci_resume_monitor_sync(struct hci_dev *hdev)
6002 switch (hci_get_adv_monitor_offload_ext(hdev)) {
6003 case HCI_ADV_MONITOR_EXT_MSFT:
6004 msft_resume_sync(hdev);
6011 /* This function resume scan and reset paused flag */
6012 static int hci_resume_scan_sync(struct hci_dev *hdev)
6014 if (!hdev->scanning_paused)
6017 hdev->scanning_paused = false;
6019 hci_update_scan_sync(hdev);
6021 /* Reset passive scanning to normal */
6022 hci_update_passive_scan_sync(hdev);
6027 /* This function performs the HCI suspend procedures in the follow order:
6029 * Restore event mask
6030 * Clear event filter
6031 * Update passive scanning (normal duty cycle)
6032 * Resume Directed Advertising/Advertising
6033 * Resume discovery (active scanning/inquiry)
6035 int hci_resume_sync(struct hci_dev *hdev)
6037 /* If not marked as suspended there nothing to do */
6038 if (!hdev->suspended)
6041 hdev->suspended = false;
6043 /* Restore event mask */
6044 hci_set_event_mask_sync(hdev);
6046 /* Clear any event filters and restore scan state */
6047 hci_clear_event_filter_sync(hdev);
6049 /* Resume scanning */
6050 hci_resume_scan_sync(hdev);
6052 /* Resume monitor filters */
6053 hci_resume_monitor_sync(hdev);
6055 /* Resume other advertisements */
6056 hci_resume_advertising_sync(hdev);
6058 /* Resume discovery */
6059 hci_resume_discovery_sync(hdev);
6064 static bool conn_use_rpa(struct hci_conn *conn)
6066 struct hci_dev *hdev = conn->hdev;
6068 return hci_dev_test_flag(hdev, HCI_PRIVACY);
6071 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
6072 struct hci_conn *conn)
6074 struct hci_cp_le_set_ext_adv_params cp;
6076 bdaddr_t random_addr;
6079 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6084 /* Set require_privacy to false so that the remote device has a
6085 * chance of identifying us.
6087 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
6088 &own_addr_type, &random_addr);
6092 memset(&cp, 0, sizeof(cp));
6094 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
6095 cp.channel_map = hdev->le_adv_channel_map;
6096 cp.tx_power = HCI_TX_POWER_INVALID;
6097 cp.primary_phy = HCI_ADV_PHY_1M;
6098 cp.secondary_phy = HCI_ADV_PHY_1M;
6099 cp.handle = 0x00; /* Use instance 0 for directed adv */
6100 cp.own_addr_type = own_addr_type;
6101 cp.peer_addr_type = conn->dst_type;
6102 bacpy(&cp.peer_addr, &conn->dst);
6104 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
6105 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
6106 * does not supports advertising data when the advertising set already
6107 * contains some, the controller shall return erroc code 'Invalid
6108 * HCI Command Parameters(0x12).
6109 * So it is required to remove adv set for handle 0x00. since we use
6110 * instance 0 for directed adv.
6112 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
6116 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
6117 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6121 /* Check if random address need to be updated */
6122 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
6123 bacmp(&random_addr, BDADDR_ANY) &&
6124 bacmp(&random_addr, &hdev->random_addr)) {
6125 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
6131 return hci_enable_ext_advertising_sync(hdev, 0x00);
6134 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
6135 struct hci_conn *conn)
6137 struct hci_cp_le_set_adv_param cp;
6142 if (ext_adv_capable(hdev))
6143 return hci_le_ext_directed_advertising_sync(hdev, conn);
6145 /* Clear the HCI_LE_ADV bit temporarily so that the
6146 * hci_update_random_address knows that it's safe to go ahead
6147 * and write a new random address. The flag will be set back on
6148 * as soon as the SET_ADV_ENABLE HCI command completes.
6150 hci_dev_clear_flag(hdev, HCI_LE_ADV);
6152 /* Set require_privacy to false so that the remote device has a
6153 * chance of identifying us.
6155 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6160 memset(&cp, 0, sizeof(cp));
6162 /* Some controllers might reject command if intervals are not
6163 * within range for undirected advertising.
6164 * BCM20702A0 is known to be affected by this.
6166 cp.min_interval = cpu_to_le16(0x0020);
6167 cp.max_interval = cpu_to_le16(0x0020);
6169 cp.type = LE_ADV_DIRECT_IND;
6170 cp.own_address_type = own_addr_type;
6171 cp.direct_addr_type = conn->dst_type;
6172 bacpy(&cp.direct_addr, &conn->dst);
6173 cp.channel_map = hdev->le_adv_channel_map;
6175 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
6176 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6182 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
6183 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
6186 static void set_ext_conn_params(struct hci_conn *conn,
6187 struct hci_cp_le_ext_conn_param *p)
6189 struct hci_dev *hdev = conn->hdev;
6191 memset(p, 0, sizeof(*p));
6193 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6194 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6195 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6196 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6197 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
6198 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6199 p->min_ce_len = cpu_to_le16(0x0000);
6200 p->max_ce_len = cpu_to_le16(0x0000);
6203 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
6204 struct hci_conn *conn, u8 own_addr_type)
6206 struct hci_cp_le_ext_create_conn *cp;
6207 struct hci_cp_le_ext_conn_param *p;
6208 u8 data[sizeof(*cp) + sizeof(*p) * 3];
6212 p = (void *)cp->data;
6214 memset(cp, 0, sizeof(*cp));
6216 bacpy(&cp->peer_addr, &conn->dst);
6217 cp->peer_addr_type = conn->dst_type;
6218 cp->own_addr_type = own_addr_type;
6222 if (scan_1m(hdev)) {
6223 cp->phys |= LE_SCAN_PHY_1M;
6224 set_ext_conn_params(conn, p);
6230 if (scan_2m(hdev)) {
6231 cp->phys |= LE_SCAN_PHY_2M;
6232 set_ext_conn_params(conn, p);
6238 if (scan_coded(hdev)) {
6239 cp->phys |= LE_SCAN_PHY_CODED;
6240 set_ext_conn_params(conn, p);
6245 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6247 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
6248 conn->conn_timeout, NULL);
6251 int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
6253 struct hci_cp_le_create_conn cp;
6254 struct hci_conn_params *params;
6258 /* If requested to connect as peripheral use directed advertising */
6259 if (conn->role == HCI_ROLE_SLAVE) {
6260 /* If we're active scanning and simultaneous roles is not
6261 * enabled simply reject the attempt.
6263 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6264 hdev->le_scan_type == LE_SCAN_ACTIVE &&
6265 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6270 /* Pause advertising while doing directed advertising. */
6271 hci_pause_advertising_sync(hdev);
6273 err = hci_le_directed_advertising_sync(hdev, conn);
6277 /* Disable advertising if simultaneous roles is not in use. */
6278 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6279 hci_pause_advertising_sync(hdev);
6281 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6283 conn->le_conn_min_interval = params->conn_min_interval;
6284 conn->le_conn_max_interval = params->conn_max_interval;
6285 conn->le_conn_latency = params->conn_latency;
6286 conn->le_supv_timeout = params->supervision_timeout;
6288 conn->le_conn_min_interval = hdev->le_conn_min_interval;
6289 conn->le_conn_max_interval = hdev->le_conn_max_interval;
6290 conn->le_conn_latency = hdev->le_conn_latency;
6291 conn->le_supv_timeout = hdev->le_supv_timeout;
6294 /* If controller is scanning, we stop it since some controllers are
6295 * not able to scan and connect at the same time. Also set the
6296 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
6297 * handler for scan disabling knows to set the correct discovery
6300 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6301 hci_scan_disable_sync(hdev);
6302 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6305 /* Update random address, but set require_privacy to false so
6306 * that we never connect with an non-resolvable address.
6308 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6313 if (use_ext_conn(hdev)) {
6314 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6318 memset(&cp, 0, sizeof(cp));
6320 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6321 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6323 bacpy(&cp.peer_addr, &conn->dst);
6324 cp.peer_addr_type = conn->dst_type;
6325 cp.own_address_type = own_addr_type;
6326 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6327 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6328 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
6329 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6330 cp.min_ce_len = cpu_to_le16(0x0000);
6331 cp.max_ce_len = cpu_to_le16(0x0000);
6333 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6335 * If this event is unmasked and the HCI_LE_Connection_Complete event
6336 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6337 * sent when a new connection has been created.
6339 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6341 use_enhanced_conn_complete(hdev) ?
6342 HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6343 HCI_EV_LE_CONN_COMPLETE,
6344 conn->conn_timeout, NULL);
6347 if (err == -ETIMEDOUT)
6348 hci_le_connect_cancel_sync(hdev, conn, 0x00);
6350 /* Re-enable advertising after the connection attempt is finished. */
6351 hci_resume_advertising_sync(hdev);
6355 int hci_le_create_cis_sync(struct hci_dev *hdev)
6358 struct hci_cp_le_create_cis cp;
6359 struct hci_cis cis[0x1f];
6361 struct hci_conn *conn;
6362 u8 cig = BT_ISO_QOS_CIG_UNSET;
6364 /* The spec allows only one pending LE Create CIS command at a time. If
6365 * the command is pending now, don't do anything. We check for pending
6366 * connections after each CIS Established event.
6368 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6371 * If the Host issues this command before all the
6372 * HCI_LE_CIS_Established events from the previous use of the
6373 * command have been generated, the Controller shall return the
6374 * error code Command Disallowed (0x0C).
6376 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6379 * When the Controller receives the HCI_LE_Create_CIS command, the
6380 * Controller sends the HCI_Command_Status event to the Host. An
6381 * HCI_LE_CIS_Established event will be generated for each CIS when it
6382 * is established or if it is disconnected or considered lost before
6383 * being established; until all the events are generated, the command
6387 memset(&cmd, 0, sizeof(cmd));
6393 /* Wait until previous Create CIS has completed */
6394 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6395 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
6399 /* Find CIG with all CIS ready */
6400 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6401 struct hci_conn *link;
6403 if (hci_conn_check_create_cis(conn))
6406 cig = conn->iso_qos.ucast.cig;
6408 list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
6409 if (hci_conn_check_create_cis(link) > 0 &&
6410 link->iso_qos.ucast.cig == cig &&
6411 link->state != BT_CONNECTED) {
6412 cig = BT_ISO_QOS_CIG_UNSET;
6417 if (cig != BT_ISO_QOS_CIG_UNSET)
6421 if (cig == BT_ISO_QOS_CIG_UNSET)
6424 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6425 struct hci_cis *cis = &cmd.cis[cmd.cp.num_cis];
6427 if (hci_conn_check_create_cis(conn) ||
6428 conn->iso_qos.ucast.cig != cig)
6431 set_bit(HCI_CONN_CREATE_CIS, &conn->flags);
6432 cis->acl_handle = cpu_to_le16(conn->parent->handle);
6433 cis->cis_handle = cpu_to_le16(conn->handle);
6436 if (cmd.cp.num_cis >= ARRAY_SIZE(cmd.cis))
6443 hci_dev_unlock(hdev);
6445 if (!cmd.cp.num_cis)
6448 /* Wait for HCI_LE_CIS_Established */
6449 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6450 sizeof(cmd.cp) + sizeof(cmd.cis[0]) *
6451 cmd.cp.num_cis, &cmd,
6452 HCI_EVT_LE_CIS_ESTABLISHED,
6453 conn->conn_timeout, NULL);
6456 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6458 struct hci_cp_le_remove_cig cp;
6460 memset(&cp, 0, sizeof(cp));
6463 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6464 &cp, HCI_CMD_TIMEOUT);
6467 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6469 struct hci_cp_le_big_term_sync cp;
6471 memset(&cp, 0, sizeof(cp));
6474 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6475 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6478 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6480 struct hci_cp_le_pa_term_sync cp;
6482 memset(&cp, 0, sizeof(cp));
6483 cp.handle = cpu_to_le16(handle);
6485 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6486 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6489 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6490 bool use_rpa, struct adv_info *adv_instance,
6491 u8 *own_addr_type, bdaddr_t *rand_addr)
6495 bacpy(rand_addr, BDADDR_ANY);
6497 /* If privacy is enabled use a resolvable private address. If
6498 * current RPA has expired then generate a new one.
6501 /* If Controller supports LL Privacy use own address type is
6504 if (use_ll_privacy(hdev))
6505 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
6507 *own_addr_type = ADDR_LE_DEV_RANDOM;
6510 if (adv_rpa_valid(adv_instance))
6513 if (rpa_valid(hdev))
6517 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6519 bt_dev_err(hdev, "failed to generate new RPA");
6523 bacpy(rand_addr, &hdev->rpa);
6528 /* In case of required privacy without resolvable private address,
6529 * use an non-resolvable private address. This is useful for
6530 * non-connectable advertising.
6532 if (require_privacy) {
6536 /* The non-resolvable private address is generated
6537 * from random six bytes with the two most significant
6540 get_random_bytes(&nrpa, 6);
6543 /* The non-resolvable private address shall not be
6544 * equal to the public address.
6546 if (bacmp(&hdev->bdaddr, &nrpa))
6550 *own_addr_type = ADDR_LE_DEV_RANDOM;
6551 bacpy(rand_addr, &nrpa);
6556 /* No privacy so use a public address. */
6557 *own_addr_type = ADDR_LE_DEV_PUBLIC;
6562 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6564 u8 instance = PTR_UINT(data);
6566 return hci_update_adv_data_sync(hdev, instance);
6569 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6571 return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6572 UINT_PTR(instance), NULL);