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;
35 /* Free the request command so it is not used as response */
36 kfree_skb(hdev->req_skb);
40 struct sock *sk = hci_skb_sk(skb);
42 /* Drop sk reference if set */
46 hdev->req_rsp = skb_get(skb);
49 wake_up_interruptible(&hdev->req_wait_q);
52 static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
53 u32 plen, const void *param,
56 int len = HCI_COMMAND_HDR_SIZE + plen;
57 struct hci_command_hdr *hdr;
60 skb = bt_skb_alloc(len, GFP_ATOMIC);
64 hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
65 hdr->opcode = cpu_to_le16(opcode);
69 skb_put_data(skb, param, plen);
71 bt_dev_dbg(hdev, "skb len %d", skb->len);
73 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
74 hci_skb_opcode(skb) = opcode;
76 /* Grab a reference if command needs to be associated with a sock (e.g.
77 * likely mgmt socket that initiated the command).
87 static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
88 const void *param, u8 event, struct sock *sk)
90 struct hci_dev *hdev = req->hdev;
93 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
95 /* If an error occurred during request building, there is no point in
96 * queueing the HCI command. We can simply return.
101 skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
103 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
109 if (skb_queue_empty(&req->cmd_q))
110 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
112 hci_skb_event(skb) = event;
114 skb_queue_tail(&req->cmd_q, skb);
117 static int hci_cmd_sync_run(struct hci_request *req)
119 struct hci_dev *hdev = req->hdev;
123 bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
125 /* If an error occurred during request building, remove all HCI
126 * commands queued on the HCI request queue.
129 skb_queue_purge(&req->cmd_q);
133 /* Do not allow empty requests */
134 if (skb_queue_empty(&req->cmd_q))
137 skb = skb_peek_tail(&req->cmd_q);
138 bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
139 bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
141 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
142 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
143 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
145 queue_work(hdev->workqueue, &hdev->cmd_work);
150 /* This function requires the caller holds hdev->req_lock. */
151 struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
152 const void *param, u8 event, u32 timeout,
155 struct hci_request req;
159 bt_dev_dbg(hdev, "Opcode 0x%4.4x", opcode);
161 hci_req_init(&req, hdev);
163 hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
165 hdev->req_status = HCI_REQ_PEND;
167 err = hci_cmd_sync_run(&req);
171 err = wait_event_interruptible_timeout(hdev->req_wait_q,
172 hdev->req_status != HCI_REQ_PEND,
175 if (err == -ERESTARTSYS)
176 return ERR_PTR(-EINTR);
178 switch (hdev->req_status) {
180 err = -bt_to_errno(hdev->req_result);
183 case HCI_REQ_CANCELED:
184 err = -hdev->req_result;
192 hdev->req_status = 0;
193 hdev->req_result = 0;
195 hdev->req_rsp = NULL;
197 bt_dev_dbg(hdev, "end: err %d", err);
206 EXPORT_SYMBOL(__hci_cmd_sync_sk);
208 /* This function requires the caller holds hdev->req_lock. */
209 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
210 const void *param, u32 timeout)
212 return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
214 EXPORT_SYMBOL(__hci_cmd_sync);
216 /* Send HCI command and wait for command complete event */
217 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
218 const void *param, u32 timeout)
222 if (!test_bit(HCI_UP, &hdev->flags))
223 return ERR_PTR(-ENETDOWN);
225 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
227 hci_req_sync_lock(hdev);
228 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
229 hci_req_sync_unlock(hdev);
233 EXPORT_SYMBOL(hci_cmd_sync);
235 /* This function requires the caller holds hdev->req_lock. */
236 struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
237 const void *param, u8 event, u32 timeout)
239 return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
242 EXPORT_SYMBOL(__hci_cmd_sync_ev);
244 /* This function requires the caller holds hdev->req_lock. */
245 int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
246 const void *param, u8 event, u32 timeout,
252 skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
255 bt_dev_err(hdev, "Opcode 0x%4.4x failed: %ld", opcode,
260 /* If command return a status event skb will be set to NULL as there are
261 * no parameters, in case of failure IS_ERR(skb) would have be set to
262 * the actual error would be found with PTR_ERR(skb).
267 status = skb->data[0];
273 EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
275 int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
276 const void *param, u32 timeout)
278 return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
281 EXPORT_SYMBOL(__hci_cmd_sync_status);
283 static void hci_cmd_sync_work(struct work_struct *work)
285 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
287 bt_dev_dbg(hdev, "");
289 /* Dequeue all entries and run them */
291 struct hci_cmd_sync_work_entry *entry;
293 mutex_lock(&hdev->cmd_sync_work_lock);
294 entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
295 struct hci_cmd_sync_work_entry,
298 list_del(&entry->list);
299 mutex_unlock(&hdev->cmd_sync_work_lock);
304 bt_dev_dbg(hdev, "entry %p", entry);
309 hci_req_sync_lock(hdev);
310 err = entry->func(hdev, entry->data);
312 entry->destroy(hdev, entry->data, err);
313 hci_req_sync_unlock(hdev);
320 static void hci_cmd_sync_cancel_work(struct work_struct *work)
322 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
324 cancel_delayed_work_sync(&hdev->cmd_timer);
325 cancel_delayed_work_sync(&hdev->ncmd_timer);
326 atomic_set(&hdev->cmd_cnt, 1);
328 wake_up_interruptible(&hdev->req_wait_q);
331 static int hci_scan_disable_sync(struct hci_dev *hdev);
332 static int scan_disable_sync(struct hci_dev *hdev, void *data)
334 return hci_scan_disable_sync(hdev);
337 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length);
338 static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data)
340 return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN);
343 static void le_scan_disable(struct work_struct *work)
345 struct hci_dev *hdev = container_of(work, struct hci_dev,
346 le_scan_disable.work);
349 bt_dev_dbg(hdev, "");
352 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
355 status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
357 bt_dev_err(hdev, "failed to disable LE scan: %d", status);
361 hdev->discovery.scan_start = 0;
363 /* If we were running LE only scan, change discovery state. If
364 * we were running both LE and BR/EDR inquiry simultaneously,
365 * and BR/EDR inquiry is already finished, stop discovery,
366 * otherwise BR/EDR inquiry will stop discovery when finished.
367 * If we will resolve remote device name, do not change
371 if (hdev->discovery.type == DISCOV_TYPE_LE)
374 if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
377 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
378 if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
379 hdev->discovery.state != DISCOVERY_RESOLVING)
385 status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
387 bt_dev_err(hdev, "inquiry failed: status %d", status);
394 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
397 hci_dev_unlock(hdev);
400 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
403 static int reenable_adv_sync(struct hci_dev *hdev, void *data)
405 bt_dev_dbg(hdev, "");
407 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
408 list_empty(&hdev->adv_instances))
411 if (hdev->cur_adv_instance) {
412 return hci_schedule_adv_instance_sync(hdev,
413 hdev->cur_adv_instance,
416 if (ext_adv_capable(hdev)) {
417 hci_start_ext_adv_sync(hdev, 0x00);
419 hci_update_adv_data_sync(hdev, 0x00);
420 hci_update_scan_rsp_data_sync(hdev, 0x00);
421 hci_enable_advertising_sync(hdev);
428 static void reenable_adv(struct work_struct *work)
430 struct hci_dev *hdev = container_of(work, struct hci_dev,
434 bt_dev_dbg(hdev, "");
438 status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
440 bt_dev_err(hdev, "failed to reenable ADV: %d", status);
442 hci_dev_unlock(hdev);
445 static void cancel_adv_timeout(struct hci_dev *hdev)
447 if (hdev->adv_instance_timeout) {
448 hdev->adv_instance_timeout = 0;
449 cancel_delayed_work(&hdev->adv_instance_expire);
453 /* For a single instance:
454 * - force == true: The instance will be removed even when its remaining
455 * lifetime is not zero.
456 * - force == false: the instance will be deactivated but kept stored unless
457 * the remaining lifetime is zero.
459 * For instance == 0x00:
460 * - force == true: All instances will be removed regardless of their timeout
462 * - force == false: Only instances that have a timeout will be removed.
464 int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
465 u8 instance, bool force)
467 struct adv_info *adv_instance, *n, *next_instance = NULL;
471 /* Cancel any timeout concerning the removed instance(s). */
472 if (!instance || hdev->cur_adv_instance == instance)
473 cancel_adv_timeout(hdev);
475 /* Get the next instance to advertise BEFORE we remove
476 * the current one. This can be the same instance again
477 * if there is only one instance.
479 if (instance && hdev->cur_adv_instance == instance)
480 next_instance = hci_get_next_instance(hdev, instance);
482 if (instance == 0x00) {
483 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
485 if (!(force || adv_instance->timeout))
488 rem_inst = adv_instance->instance;
489 err = hci_remove_adv_instance(hdev, rem_inst);
491 mgmt_advertising_removed(sk, hdev, rem_inst);
494 adv_instance = hci_find_adv_instance(hdev, instance);
496 if (force || (adv_instance && adv_instance->timeout &&
497 !adv_instance->remaining_time)) {
498 /* Don't advertise a removed instance. */
500 next_instance->instance == instance)
501 next_instance = NULL;
503 err = hci_remove_adv_instance(hdev, instance);
505 mgmt_advertising_removed(sk, hdev, instance);
509 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
512 if (next_instance && !ext_adv_capable(hdev))
513 return hci_schedule_adv_instance_sync(hdev,
514 next_instance->instance,
520 static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
522 u8 instance = *(u8 *)data;
526 hci_clear_adv_instance_sync(hdev, NULL, instance, false);
528 if (list_empty(&hdev->adv_instances))
529 return hci_disable_advertising_sync(hdev);
534 static void adv_timeout_expire(struct work_struct *work)
537 struct hci_dev *hdev = container_of(work, struct hci_dev,
538 adv_instance_expire.work);
540 bt_dev_dbg(hdev, "");
544 hdev->adv_instance_timeout = 0;
546 if (hdev->cur_adv_instance == 0x00)
549 inst_ptr = kmalloc(1, GFP_KERNEL);
553 *inst_ptr = hdev->cur_adv_instance;
554 hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
557 hci_dev_unlock(hdev);
560 void hci_cmd_sync_init(struct hci_dev *hdev)
562 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
563 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
564 mutex_init(&hdev->cmd_sync_work_lock);
565 mutex_init(&hdev->unregister_lock);
567 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
568 INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
569 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
570 INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
573 static void _hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
574 struct hci_cmd_sync_work_entry *entry,
578 entry->destroy(hdev, entry->data, err);
580 list_del(&entry->list);
584 void hci_cmd_sync_clear(struct hci_dev *hdev)
586 struct hci_cmd_sync_work_entry *entry, *tmp;
588 cancel_work_sync(&hdev->cmd_sync_work);
589 cancel_work_sync(&hdev->reenable_adv_work);
591 mutex_lock(&hdev->cmd_sync_work_lock);
592 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list)
593 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
594 mutex_unlock(&hdev->cmd_sync_work_lock);
597 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
599 bt_dev_dbg(hdev, "err 0x%2.2x", err);
601 if (hdev->req_status == HCI_REQ_PEND) {
602 hdev->req_result = err;
603 hdev->req_status = HCI_REQ_CANCELED;
605 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
608 EXPORT_SYMBOL(hci_cmd_sync_cancel);
610 /* Cancel ongoing command request synchronously:
612 * - Set result and mark status to HCI_REQ_CANCELED
613 * - Wakeup command sync thread
615 void hci_cmd_sync_cancel_sync(struct hci_dev *hdev, int err)
617 bt_dev_dbg(hdev, "err 0x%2.2x", err);
619 if (hdev->req_status == HCI_REQ_PEND) {
620 /* req_result is __u32 so error must be positive to be properly
623 hdev->req_result = err < 0 ? -err : err;
624 hdev->req_status = HCI_REQ_CANCELED;
626 wake_up_interruptible(&hdev->req_wait_q);
629 EXPORT_SYMBOL(hci_cmd_sync_cancel_sync);
631 /* Submit HCI command to be run in as cmd_sync_work:
633 * - hdev must _not_ be unregistered
635 int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
636 void *data, hci_cmd_sync_work_destroy_t destroy)
638 struct hci_cmd_sync_work_entry *entry;
641 mutex_lock(&hdev->unregister_lock);
642 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
647 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
654 entry->destroy = destroy;
656 mutex_lock(&hdev->cmd_sync_work_lock);
657 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
658 mutex_unlock(&hdev->cmd_sync_work_lock);
660 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
663 mutex_unlock(&hdev->unregister_lock);
666 EXPORT_SYMBOL(hci_cmd_sync_submit);
668 /* Queue HCI command:
670 * - hdev must be running
672 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
673 void *data, hci_cmd_sync_work_destroy_t destroy)
675 /* Only queue command if hdev is running which means it had been opened
676 * and is either on init phase or is already up.
678 if (!test_bit(HCI_RUNNING, &hdev->flags))
681 return hci_cmd_sync_submit(hdev, func, data, destroy);
683 EXPORT_SYMBOL(hci_cmd_sync_queue);
685 static struct hci_cmd_sync_work_entry *
686 _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
687 void *data, hci_cmd_sync_work_destroy_t destroy)
689 struct hci_cmd_sync_work_entry *entry, *tmp;
691 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
692 if (func && entry->func != func)
695 if (data && entry->data != data)
698 if (destroy && entry->destroy != destroy)
707 /* Queue HCI command entry once:
709 * - Lookup if an entry already exist and only if it doesn't creates a new entry
712 int hci_cmd_sync_queue_once(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
713 void *data, hci_cmd_sync_work_destroy_t destroy)
715 if (hci_cmd_sync_lookup_entry(hdev, func, data, destroy))
718 return hci_cmd_sync_queue(hdev, func, data, destroy);
720 EXPORT_SYMBOL(hci_cmd_sync_queue_once);
722 /* Lookup HCI command entry:
724 * - Return first entry that matches by function callback or data or
727 struct hci_cmd_sync_work_entry *
728 hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
729 void *data, hci_cmd_sync_work_destroy_t destroy)
731 struct hci_cmd_sync_work_entry *entry;
733 mutex_lock(&hdev->cmd_sync_work_lock);
734 entry = _hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
735 mutex_unlock(&hdev->cmd_sync_work_lock);
739 EXPORT_SYMBOL(hci_cmd_sync_lookup_entry);
741 /* Cancel HCI command entry */
742 void hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
743 struct hci_cmd_sync_work_entry *entry)
745 mutex_lock(&hdev->cmd_sync_work_lock);
746 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
747 mutex_unlock(&hdev->cmd_sync_work_lock);
749 EXPORT_SYMBOL(hci_cmd_sync_cancel_entry);
751 /* Dequeue one HCI command entry:
753 * - Lookup and cancel first entry that matches.
755 bool hci_cmd_sync_dequeue_once(struct hci_dev *hdev,
756 hci_cmd_sync_work_func_t func,
757 void *data, hci_cmd_sync_work_destroy_t destroy)
759 struct hci_cmd_sync_work_entry *entry;
761 entry = hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
765 hci_cmd_sync_cancel_entry(hdev, entry);
769 EXPORT_SYMBOL(hci_cmd_sync_dequeue_once);
771 /* Dequeue HCI command entry:
773 * - Lookup and cancel any entry that matches by function callback or data or
776 bool hci_cmd_sync_dequeue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
777 void *data, hci_cmd_sync_work_destroy_t destroy)
779 struct hci_cmd_sync_work_entry *entry;
782 mutex_lock(&hdev->cmd_sync_work_lock);
783 while ((entry = _hci_cmd_sync_lookup_entry(hdev, func, data,
785 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
788 mutex_unlock(&hdev->cmd_sync_work_lock);
792 EXPORT_SYMBOL(hci_cmd_sync_dequeue);
794 int hci_update_eir_sync(struct hci_dev *hdev)
796 struct hci_cp_write_eir cp;
798 bt_dev_dbg(hdev, "");
800 if (!hdev_is_powered(hdev))
803 if (!lmp_ext_inq_capable(hdev))
806 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
809 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
812 memset(&cp, 0, sizeof(cp));
814 eir_create(hdev, cp.data);
816 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
819 memcpy(hdev->eir, cp.data, sizeof(cp.data));
821 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
825 static u8 get_service_classes(struct hci_dev *hdev)
827 struct bt_uuid *uuid;
830 list_for_each_entry(uuid, &hdev->uuids, list)
831 val |= uuid->svc_hint;
836 int hci_update_class_sync(struct hci_dev *hdev)
840 bt_dev_dbg(hdev, "");
842 if (!hdev_is_powered(hdev))
845 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
848 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
851 cod[0] = hdev->minor_class;
852 cod[1] = hdev->major_class;
853 cod[2] = get_service_classes(hdev);
855 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
858 if (memcmp(cod, hdev->dev_class, 3) == 0)
861 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
862 sizeof(cod), cod, HCI_CMD_TIMEOUT);
865 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
867 /* If there is no connection we are OK to advertise. */
868 if (hci_conn_num(hdev, LE_LINK) == 0)
871 /* Check le_states if there is any connection in peripheral role. */
872 if (hdev->conn_hash.le_num_peripheral > 0) {
873 /* Peripheral connection state and non connectable mode
876 if (!connectable && !(hdev->le_states[2] & 0x10))
879 /* Peripheral connection state and connectable mode bit 38
880 * and scannable bit 21.
882 if (connectable && (!(hdev->le_states[4] & 0x40) ||
883 !(hdev->le_states[2] & 0x20)))
887 /* Check le_states if there is any connection in central role. */
888 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
889 /* Central connection state and non connectable mode bit 18. */
890 if (!connectable && !(hdev->le_states[2] & 0x02))
893 /* Central connection state and connectable mode bit 35 and
896 if (connectable && (!(hdev->le_states[4] & 0x08) ||
897 !(hdev->le_states[2] & 0x08)))
904 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
906 /* If privacy is not enabled don't use RPA */
907 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
910 /* If basic privacy mode is enabled use RPA */
911 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
914 /* If limited privacy mode is enabled don't use RPA if we're
915 * both discoverable and bondable.
917 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
918 hci_dev_test_flag(hdev, HCI_BONDABLE))
921 /* We're neither bondable nor discoverable in the limited
922 * privacy mode, therefore use RPA.
927 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
929 /* If we're advertising or initiating an LE connection we can't
930 * go ahead and change the random address at this time. This is
931 * because the eventual initiator address used for the
932 * subsequently created connection will be undefined (some
933 * controllers use the new address and others the one we had
934 * when the operation started).
936 * In this kind of scenario skip the update and let the random
937 * address be updated at the next cycle.
939 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
940 hci_lookup_le_connect(hdev)) {
941 bt_dev_dbg(hdev, "Deferring random address update");
942 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
946 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
947 6, rpa, HCI_CMD_TIMEOUT);
950 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
951 bool rpa, u8 *own_addr_type)
955 /* If privacy is enabled use a resolvable private address. If
956 * current RPA has expired or there is something else than
957 * the current RPA in use, then generate a new one.
960 /* If Controller supports LL Privacy use own address type is
963 if (use_ll_privacy(hdev))
964 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
966 *own_addr_type = ADDR_LE_DEV_RANDOM;
968 /* Check if RPA is valid */
972 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
974 bt_dev_err(hdev, "failed to generate new RPA");
978 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
985 /* In case of required privacy without resolvable private address,
986 * use an non-resolvable private address. This is useful for active
987 * scanning and non-connectable advertising.
989 if (require_privacy) {
993 /* The non-resolvable private address is generated
994 * from random six bytes with the two most significant
997 get_random_bytes(&nrpa, 6);
1000 /* The non-resolvable private address shall not be
1001 * equal to the public address.
1003 if (bacmp(&hdev->bdaddr, &nrpa))
1007 *own_addr_type = ADDR_LE_DEV_RANDOM;
1009 return hci_set_random_addr_sync(hdev, &nrpa);
1012 /* If forcing static address is in use or there is no public
1013 * address use the static address as random address (but skip
1014 * the HCI command if the current random address is already the
1017 * In case BR/EDR has been disabled on a dual-mode controller
1018 * and a static address has been configured, then use that
1019 * address instead of the public BR/EDR address.
1021 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
1022 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
1023 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
1024 bacmp(&hdev->static_addr, BDADDR_ANY))) {
1025 *own_addr_type = ADDR_LE_DEV_RANDOM;
1026 if (bacmp(&hdev->static_addr, &hdev->random_addr))
1027 return hci_set_random_addr_sync(hdev,
1028 &hdev->static_addr);
1032 /* Neither privacy nor static address is being used so use a
1035 *own_addr_type = ADDR_LE_DEV_PUBLIC;
1040 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1042 struct hci_cp_le_set_ext_adv_enable *cp;
1043 struct hci_cp_ext_adv_set *set;
1044 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1046 struct adv_info *adv = NULL;
1048 /* If request specifies an instance that doesn't exist, fail */
1050 adv = hci_find_adv_instance(hdev, instance);
1054 /* If not enabled there is nothing to do */
1059 memset(data, 0, sizeof(data));
1062 set = (void *)cp->data;
1064 /* Instance 0x00 indicates all advertising instances will be disabled */
1065 cp->num_of_sets = !!instance;
1068 set->handle = adv ? adv->handle : instance;
1070 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
1072 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1073 size, data, HCI_CMD_TIMEOUT);
1076 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1077 bdaddr_t *random_addr)
1079 struct hci_cp_le_set_adv_set_rand_addr cp;
1083 /* Instance 0x00 doesn't have an adv_info, instead it uses
1084 * hdev->random_addr to track its address so whenever it needs
1085 * to be updated this also set the random address since
1086 * hdev->random_addr is shared with scan state machine.
1088 err = hci_set_random_addr_sync(hdev, random_addr);
1093 memset(&cp, 0, sizeof(cp));
1095 cp.handle = instance;
1096 bacpy(&cp.bdaddr, random_addr);
1098 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1099 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1102 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1104 struct hci_cp_le_set_ext_adv_params cp;
1107 bdaddr_t random_addr;
1110 struct adv_info *adv;
1114 adv = hci_find_adv_instance(hdev, instance);
1121 /* Updating parameters of an active instance will return a
1122 * Command Disallowed error, so we must first disable the
1123 * instance if it is active.
1125 if (adv && !adv->pending) {
1126 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1131 flags = hci_adv_instance_flags(hdev, instance);
1133 /* If the "connectable" instance flag was not set, then choose between
1134 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1136 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1137 mgmt_get_connectable(hdev);
1139 if (!is_advertising_allowed(hdev, connectable))
1142 /* Set require_privacy to true only when non-connectable
1143 * advertising is used. In that case it is fine to use a
1144 * non-resolvable private address.
1146 err = hci_get_random_address(hdev, !connectable,
1147 adv_use_rpa(hdev, flags), adv,
1148 &own_addr_type, &random_addr);
1152 memset(&cp, 0, sizeof(cp));
1155 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
1156 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
1157 cp.tx_power = adv->tx_power;
1159 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1160 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1161 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
1164 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
1168 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
1170 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
1171 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1172 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
1174 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
1176 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
1179 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
1181 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
1184 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
1185 * contains the peer’s Identity Address and the Peer_Address_Type
1186 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
1187 * These parameters are used to locate the corresponding local IRK in
1188 * the resolving list; this IRK is used to generate their own address
1189 * used in the advertisement.
1191 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
1192 hci_copy_identity_address(hdev, &cp.peer_addr,
1193 &cp.peer_addr_type);
1195 cp.own_addr_type = own_addr_type;
1196 cp.channel_map = hdev->le_adv_channel_map;
1197 cp.handle = adv ? adv->handle : instance;
1199 if (flags & MGMT_ADV_FLAG_SEC_2M) {
1200 cp.primary_phy = HCI_ADV_PHY_1M;
1201 cp.secondary_phy = HCI_ADV_PHY_2M;
1202 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
1203 cp.primary_phy = HCI_ADV_PHY_CODED;
1204 cp.secondary_phy = HCI_ADV_PHY_CODED;
1206 /* In all other cases use 1M */
1207 cp.primary_phy = HCI_ADV_PHY_1M;
1208 cp.secondary_phy = HCI_ADV_PHY_1M;
1211 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1212 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1216 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
1217 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
1218 bacmp(&random_addr, BDADDR_ANY)) {
1219 /* Check if random address need to be updated */
1221 if (!bacmp(&random_addr, &adv->random_addr))
1224 if (!bacmp(&random_addr, &hdev->random_addr))
1228 return hci_set_adv_set_random_addr_sync(hdev, instance,
1235 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1237 DEFINE_FLEX(struct hci_cp_le_set_ext_scan_rsp_data, pdu, data, length,
1238 HCI_MAX_EXT_AD_LENGTH);
1240 struct adv_info *adv = NULL;
1244 adv = hci_find_adv_instance(hdev, instance);
1245 if (!adv || !adv->scan_rsp_changed)
1249 len = eir_create_scan_rsp(hdev, instance, pdu->data);
1251 pdu->handle = adv ? adv->handle : instance;
1253 pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
1254 pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1256 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1257 struct_size(pdu, data, len), pdu,
1263 adv->scan_rsp_changed = false;
1265 memcpy(hdev->scan_rsp_data, pdu->data, len);
1266 hdev->scan_rsp_data_len = len;
1272 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1274 struct hci_cp_le_set_scan_rsp_data cp;
1277 memset(&cp, 0, sizeof(cp));
1279 len = eir_create_scan_rsp(hdev, instance, cp.data);
1281 if (hdev->scan_rsp_data_len == len &&
1282 !memcmp(cp.data, hdev->scan_rsp_data, len))
1285 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1286 hdev->scan_rsp_data_len = len;
1290 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1291 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1294 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1296 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1299 if (ext_adv_capable(hdev))
1300 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1302 return __hci_set_scan_rsp_data_sync(hdev, instance);
1305 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1307 struct hci_cp_le_set_ext_adv_enable *cp;
1308 struct hci_cp_ext_adv_set *set;
1309 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1310 struct adv_info *adv;
1313 adv = hci_find_adv_instance(hdev, instance);
1316 /* If already enabled there is nothing to do */
1324 set = (void *)cp->data;
1326 memset(cp, 0, sizeof(*cp));
1329 cp->num_of_sets = 0x01;
1331 memset(set, 0, sizeof(*set));
1333 set->handle = adv ? adv->handle : instance;
1335 /* Set duration per instance since controller is responsible for
1338 if (adv && adv->timeout) {
1339 u16 duration = adv->timeout * MSEC_PER_SEC;
1341 /* Time = N * 10 ms */
1342 set->duration = cpu_to_le16(duration / 10);
1345 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1347 sizeof(*set) * cp->num_of_sets,
1348 data, HCI_CMD_TIMEOUT);
1351 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1355 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1359 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1363 return hci_enable_ext_advertising_sync(hdev, instance);
1366 int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1368 struct hci_cp_le_set_per_adv_enable cp;
1369 struct adv_info *adv = NULL;
1371 /* If periodic advertising already disabled there is nothing to do. */
1372 adv = hci_find_adv_instance(hdev, instance);
1373 if (!adv || !adv->periodic || !adv->enabled)
1376 memset(&cp, 0, sizeof(cp));
1379 cp.handle = instance;
1381 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1382 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1385 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1386 u16 min_interval, u16 max_interval)
1388 struct hci_cp_le_set_per_adv_params cp;
1390 memset(&cp, 0, sizeof(cp));
1393 min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1396 max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1398 cp.handle = instance;
1399 cp.min_interval = cpu_to_le16(min_interval);
1400 cp.max_interval = cpu_to_le16(max_interval);
1401 cp.periodic_properties = 0x0000;
1403 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1404 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1407 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1409 DEFINE_FLEX(struct hci_cp_le_set_per_adv_data, pdu, data, length,
1410 HCI_MAX_PER_AD_LENGTH);
1412 struct adv_info *adv = NULL;
1415 adv = hci_find_adv_instance(hdev, instance);
1416 if (!adv || !adv->periodic)
1420 len = eir_create_per_adv_data(hdev, instance, pdu->data);
1423 pdu->handle = adv ? adv->handle : instance;
1424 pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
1426 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1427 struct_size(pdu, data, len), pdu,
1431 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1433 struct hci_cp_le_set_per_adv_enable cp;
1434 struct adv_info *adv = NULL;
1436 /* If periodic advertising already enabled there is nothing to do. */
1437 adv = hci_find_adv_instance(hdev, instance);
1438 if (adv && adv->periodic && adv->enabled)
1441 memset(&cp, 0, sizeof(cp));
1444 cp.handle = instance;
1446 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1447 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1450 /* Checks if periodic advertising data contains a Basic Announcement and if it
1451 * does generates a Broadcast ID and add Broadcast Announcement.
1453 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1458 /* Skip if NULL adv as instance 0x00 is used for general purpose
1459 * advertising so it cannot used for the likes of Broadcast Announcement
1460 * as it can be overwritten at any point.
1465 /* Check if PA data doesn't contains a Basic Audio Announcement then
1466 * there is nothing to do.
1468 if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1472 /* Check if advertising data already has a Broadcast Announcement since
1473 * the process may want to control the Broadcast ID directly and in that
1474 * case the kernel shall no interfere.
1476 if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1480 /* Generate Broadcast ID */
1481 get_random_bytes(bid, sizeof(bid));
1482 eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1483 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1485 return hci_update_adv_data_sync(hdev, adv->instance);
1488 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1489 u8 *data, u32 flags, u16 min_interval,
1490 u16 max_interval, u16 sync_interval)
1492 struct adv_info *adv = NULL;
1496 hci_disable_per_advertising_sync(hdev, instance);
1499 adv = hci_find_adv_instance(hdev, instance);
1500 /* Create an instance if that could not be found */
1502 adv = hci_add_per_instance(hdev, instance, flags,
1507 return PTR_ERR(adv);
1508 adv->pending = false;
1513 /* Start advertising */
1514 err = hci_start_ext_adv_sync(hdev, instance);
1518 err = hci_adv_bcast_annoucement(hdev, adv);
1522 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1527 err = hci_set_per_adv_data_sync(hdev, instance);
1531 err = hci_enable_per_advertising_sync(hdev, instance);
1539 hci_remove_adv_instance(hdev, instance);
1544 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1548 if (ext_adv_capable(hdev))
1549 return hci_start_ext_adv_sync(hdev, instance);
1551 err = hci_update_adv_data_sync(hdev, instance);
1555 err = hci_update_scan_rsp_data_sync(hdev, instance);
1559 return hci_enable_advertising_sync(hdev);
1562 int hci_enable_advertising_sync(struct hci_dev *hdev)
1564 struct adv_info *adv_instance;
1565 struct hci_cp_le_set_adv_param cp;
1566 u8 own_addr_type, enable = 0x01;
1568 u16 adv_min_interval, adv_max_interval;
1572 if (ext_adv_capable(hdev))
1573 return hci_enable_ext_advertising_sync(hdev,
1574 hdev->cur_adv_instance);
1576 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1577 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1579 /* If the "connectable" instance flag was not set, then choose between
1580 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1582 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1583 mgmt_get_connectable(hdev);
1585 if (!is_advertising_allowed(hdev, connectable))
1588 status = hci_disable_advertising_sync(hdev);
1592 /* Clear the HCI_LE_ADV bit temporarily so that the
1593 * hci_update_random_address knows that it's safe to go ahead
1594 * and write a new random address. The flag will be set back on
1595 * as soon as the SET_ADV_ENABLE HCI command completes.
1597 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1599 /* Set require_privacy to true only when non-connectable
1600 * advertising is used. In that case it is fine to use a
1601 * non-resolvable private address.
1603 status = hci_update_random_address_sync(hdev, !connectable,
1604 adv_use_rpa(hdev, flags),
1609 memset(&cp, 0, sizeof(cp));
1612 adv_min_interval = adv_instance->min_interval;
1613 adv_max_interval = adv_instance->max_interval;
1615 adv_min_interval = hdev->le_adv_min_interval;
1616 adv_max_interval = hdev->le_adv_max_interval;
1620 cp.type = LE_ADV_IND;
1622 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1623 cp.type = LE_ADV_SCAN_IND;
1625 cp.type = LE_ADV_NONCONN_IND;
1627 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1628 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1629 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1630 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1634 cp.min_interval = cpu_to_le16(adv_min_interval);
1635 cp.max_interval = cpu_to_le16(adv_max_interval);
1636 cp.own_address_type = own_addr_type;
1637 cp.channel_map = hdev->le_adv_channel_map;
1639 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1640 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1644 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1645 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1648 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1650 return hci_enable_advertising_sync(hdev);
1653 int hci_enable_advertising(struct hci_dev *hdev)
1655 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1656 list_empty(&hdev->adv_instances))
1659 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1662 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1667 if (!ext_adv_capable(hdev))
1670 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1674 /* If request specifies an instance that doesn't exist, fail */
1675 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1678 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1679 sizeof(instance), &instance, 0,
1680 HCI_CMD_TIMEOUT, sk);
1683 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1685 struct adv_info *adv = data;
1689 instance = adv->instance;
1691 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1694 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1696 struct adv_info *adv = NULL;
1699 adv = hci_find_adv_instance(hdev, instance);
1704 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1707 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1709 struct hci_cp_le_term_big cp;
1711 memset(&cp, 0, sizeof(cp));
1715 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1716 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1719 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1721 DEFINE_FLEX(struct hci_cp_le_set_ext_adv_data, pdu, data, length,
1722 HCI_MAX_EXT_AD_LENGTH);
1724 struct adv_info *adv = NULL;
1728 adv = hci_find_adv_instance(hdev, instance);
1729 if (!adv || !adv->adv_data_changed)
1733 len = eir_create_adv_data(hdev, instance, pdu->data);
1736 pdu->handle = adv ? adv->handle : instance;
1737 pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
1738 pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1740 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1741 struct_size(pdu, data, len), pdu,
1746 /* Update data if the command succeed */
1748 adv->adv_data_changed = false;
1750 memcpy(hdev->adv_data, pdu->data, len);
1751 hdev->adv_data_len = len;
1757 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1759 struct hci_cp_le_set_adv_data cp;
1762 memset(&cp, 0, sizeof(cp));
1764 len = eir_create_adv_data(hdev, instance, cp.data);
1766 /* There's nothing to do if the data hasn't changed */
1767 if (hdev->adv_data_len == len &&
1768 memcmp(cp.data, hdev->adv_data, len) == 0)
1771 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1772 hdev->adv_data_len = len;
1776 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1777 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1780 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1782 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1785 if (ext_adv_capable(hdev))
1786 return hci_set_ext_adv_data_sync(hdev, instance);
1788 return hci_set_adv_data_sync(hdev, instance);
1791 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1794 struct adv_info *adv = NULL;
1797 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1800 if (hdev->adv_instance_timeout)
1803 adv = hci_find_adv_instance(hdev, instance);
1807 /* A zero timeout means unlimited advertising. As long as there is
1808 * only one instance, duration should be ignored. We still set a timeout
1809 * in case further instances are being added later on.
1811 * If the remaining lifetime of the instance is more than the duration
1812 * then the timeout corresponds to the duration, otherwise it will be
1813 * reduced to the remaining instance lifetime.
1815 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1816 timeout = adv->duration;
1818 timeout = adv->remaining_time;
1820 /* The remaining time is being reduced unless the instance is being
1821 * advertised without time limit.
1824 adv->remaining_time = adv->remaining_time - timeout;
1826 /* Only use work for scheduling instances with legacy advertising */
1827 if (!ext_adv_capable(hdev)) {
1828 hdev->adv_instance_timeout = timeout;
1829 queue_delayed_work(hdev->req_workqueue,
1830 &hdev->adv_instance_expire,
1831 msecs_to_jiffies(timeout * 1000));
1834 /* If we're just re-scheduling the same instance again then do not
1835 * execute any HCI commands. This happens when a single instance is
1838 if (!force && hdev->cur_adv_instance == instance &&
1839 hci_dev_test_flag(hdev, HCI_LE_ADV))
1842 hdev->cur_adv_instance = instance;
1844 return hci_start_adv_sync(hdev, instance);
1847 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1851 if (!ext_adv_capable(hdev))
1854 /* Disable instance 0x00 to disable all instances */
1855 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1859 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1860 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1863 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1865 struct adv_info *adv, *n;
1868 if (ext_adv_capable(hdev))
1869 /* Remove all existing sets */
1870 err = hci_clear_adv_sets_sync(hdev, sk);
1871 if (ext_adv_capable(hdev))
1874 /* This is safe as long as there is no command send while the lock is
1879 /* Cleanup non-ext instances */
1880 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1881 u8 instance = adv->instance;
1884 if (!(force || adv->timeout))
1887 err = hci_remove_adv_instance(hdev, instance);
1889 mgmt_advertising_removed(sk, hdev, instance);
1892 hci_dev_unlock(hdev);
1897 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1902 /* If we use extended advertising, instance has to be removed first. */
1903 if (ext_adv_capable(hdev))
1904 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1905 if (ext_adv_capable(hdev))
1908 /* This is safe as long as there is no command send while the lock is
1913 err = hci_remove_adv_instance(hdev, instance);
1915 mgmt_advertising_removed(sk, hdev, instance);
1917 hci_dev_unlock(hdev);
1922 /* For a single instance:
1923 * - force == true: The instance will be removed even when its remaining
1924 * lifetime is not zero.
1925 * - force == false: the instance will be deactivated but kept stored unless
1926 * the remaining lifetime is zero.
1928 * For instance == 0x00:
1929 * - force == true: All instances will be removed regardless of their timeout
1931 * - force == false: Only instances that have a timeout will be removed.
1933 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1934 u8 instance, bool force)
1936 struct adv_info *next = NULL;
1939 /* Cancel any timeout concerning the removed instance(s). */
1940 if (!instance || hdev->cur_adv_instance == instance)
1941 cancel_adv_timeout(hdev);
1943 /* Get the next instance to advertise BEFORE we remove
1944 * the current one. This can be the same instance again
1945 * if there is only one instance.
1947 if (hdev->cur_adv_instance == instance)
1948 next = hci_get_next_instance(hdev, instance);
1951 err = hci_clear_adv_sync(hdev, sk, force);
1955 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1957 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1958 /* Don't advertise a removed instance. */
1959 if (next && next->instance == instance)
1962 err = hci_remove_adv_sync(hdev, instance, sk);
1968 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1971 if (next && !ext_adv_capable(hdev))
1972 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1977 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1979 struct hci_cp_read_rssi cp;
1982 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1983 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1986 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1988 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1989 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1992 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1994 struct hci_cp_read_tx_power cp;
1998 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1999 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2002 int hci_disable_advertising_sync(struct hci_dev *hdev)
2007 /* If controller is not advertising we are done. */
2008 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
2011 if (ext_adv_capable(hdev))
2012 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
2013 if (ext_adv_capable(hdev))
2016 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
2017 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
2020 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
2023 struct hci_cp_le_set_ext_scan_enable cp;
2025 memset(&cp, 0, sizeof(cp));
2028 if (hci_dev_test_flag(hdev, HCI_MESH))
2029 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2031 cp.filter_dup = filter_dup;
2033 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
2034 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2037 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2040 struct hci_cp_le_set_scan_enable cp;
2042 if (use_ext_scan(hdev))
2043 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2045 memset(&cp, 0, sizeof(cp));
2048 if (val && hci_dev_test_flag(hdev, HCI_MESH))
2049 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2051 cp.filter_dup = filter_dup;
2053 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2054 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2057 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2059 if (!use_ll_privacy(hdev))
2062 /* If controller is not/already resolving we are done. */
2063 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2066 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2067 sizeof(val), &val, HCI_CMD_TIMEOUT);
2070 static int hci_scan_disable_sync(struct hci_dev *hdev)
2074 /* If controller is not scanning we are done. */
2075 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2078 if (hdev->scanning_paused) {
2079 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2083 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2085 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2092 static bool scan_use_rpa(struct hci_dev *hdev)
2094 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2097 static void hci_start_interleave_scan(struct hci_dev *hdev)
2099 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2100 queue_delayed_work(hdev->req_workqueue,
2101 &hdev->interleave_scan, 0);
2104 static bool is_interleave_scanning(struct hci_dev *hdev)
2106 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2109 static void cancel_interleave_scan(struct hci_dev *hdev)
2111 bt_dev_dbg(hdev, "cancelling interleave scan");
2113 cancel_delayed_work_sync(&hdev->interleave_scan);
2115 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2118 /* Return true if interleave_scan wasn't started until exiting this function,
2119 * otherwise, return false
2121 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2123 /* Do interleaved scan only if all of the following are true:
2124 * - There is at least one ADV monitor
2125 * - At least one pending LE connection or one device to be scanned for
2126 * - Monitor offloading is not supported
2127 * If so, we should alternate between allowlist scan and one without
2128 * any filters to save power.
2130 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2131 !(list_empty(&hdev->pend_le_conns) &&
2132 list_empty(&hdev->pend_le_reports)) &&
2133 hci_get_adv_monitor_offload_ext(hdev) ==
2134 HCI_ADV_MONITOR_EXT_NONE;
2135 bool is_interleaving = is_interleave_scanning(hdev);
2137 if (use_interleaving && !is_interleaving) {
2138 hci_start_interleave_scan(hdev);
2139 bt_dev_dbg(hdev, "starting interleave scan");
2143 if (!use_interleaving && is_interleaving)
2144 cancel_interleave_scan(hdev);
2149 /* Removes connection to resolve list if needed.*/
2150 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2151 bdaddr_t *bdaddr, u8 bdaddr_type)
2153 struct hci_cp_le_del_from_resolv_list cp;
2154 struct bdaddr_list_with_irk *entry;
2156 if (!use_ll_privacy(hdev))
2159 /* Check if the IRK has been programmed */
2160 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2165 cp.bdaddr_type = bdaddr_type;
2166 bacpy(&cp.bdaddr, bdaddr);
2168 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2169 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2172 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2173 bdaddr_t *bdaddr, u8 bdaddr_type)
2175 struct hci_cp_le_del_from_accept_list cp;
2178 /* Check if device is on accept list before removing it */
2179 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2182 cp.bdaddr_type = bdaddr_type;
2183 bacpy(&cp.bdaddr, bdaddr);
2185 /* Ignore errors when removing from resolving list as that is likely
2186 * that the device was never added.
2188 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2190 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2191 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2193 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2197 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2203 struct conn_params {
2206 hci_conn_flags_t flags;
2210 /* Adds connection to resolve list if needed.
2211 * Setting params to NULL programs local hdev->irk
2213 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2214 struct conn_params *params)
2216 struct hci_cp_le_add_to_resolv_list cp;
2217 struct smp_irk *irk;
2218 struct bdaddr_list_with_irk *entry;
2219 struct hci_conn_params *p;
2221 if (!use_ll_privacy(hdev))
2224 /* Attempt to program local identity address, type and irk if params is
2228 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2231 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2232 memcpy(cp.peer_irk, hdev->irk, 16);
2236 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2240 /* Check if the IK has _not_ been programmed yet. */
2241 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2247 cp.bdaddr_type = params->addr_type;
2248 bacpy(&cp.bdaddr, ¶ms->addr);
2249 memcpy(cp.peer_irk, irk->val, 16);
2251 /* Default privacy mode is always Network */
2252 params->privacy_mode = HCI_NETWORK_PRIVACY;
2255 p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2256 ¶ms->addr, params->addr_type);
2258 p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2259 ¶ms->addr, params->addr_type);
2261 WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY);
2265 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2266 memcpy(cp.local_irk, hdev->irk, 16);
2268 memset(cp.local_irk, 0, 16);
2270 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2271 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2274 /* Set Device Privacy Mode. */
2275 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2276 struct conn_params *params)
2278 struct hci_cp_le_set_privacy_mode cp;
2279 struct smp_irk *irk;
2281 /* If device privacy mode has already been set there is nothing to do */
2282 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2285 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2286 * indicates that LL Privacy has been enabled and
2287 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2289 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2292 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2296 memset(&cp, 0, sizeof(cp));
2297 cp.bdaddr_type = irk->addr_type;
2298 bacpy(&cp.bdaddr, &irk->bdaddr);
2299 cp.mode = HCI_DEVICE_PRIVACY;
2301 /* Note: params->privacy_mode is not updated since it is a copy */
2303 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2304 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2307 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2308 * this attempts to program the device in the resolving list as well and
2309 * properly set the privacy mode.
2311 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2312 struct conn_params *params,
2315 struct hci_cp_le_add_to_accept_list cp;
2318 /* During suspend, only wakeable devices can be in acceptlist */
2319 if (hdev->suspended &&
2320 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) {
2321 hci_le_del_accept_list_sync(hdev, ¶ms->addr,
2326 /* Select filter policy to accept all advertising */
2327 if (*num_entries >= hdev->le_accept_list_size)
2330 /* Accept list can not be used with RPAs */
2331 if (!use_ll_privacy(hdev) &&
2332 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type))
2335 /* Attempt to program the device in the resolving list first to avoid
2336 * having to rollback in case it fails since the resolving list is
2337 * dynamic it can probably be smaller than the accept list.
2339 err = hci_le_add_resolve_list_sync(hdev, params);
2341 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2345 /* Set Privacy Mode */
2346 err = hci_le_set_privacy_mode_sync(hdev, params);
2348 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2352 /* Check if already in accept list */
2353 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
2358 cp.bdaddr_type = params->addr_type;
2359 bacpy(&cp.bdaddr, ¶ms->addr);
2361 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2362 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2364 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2365 /* Rollback the device from the resolving list */
2366 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2370 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2376 /* This function disables/pause all advertising instances */
2377 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2382 /* If already been paused there is nothing to do. */
2383 if (hdev->advertising_paused)
2386 bt_dev_dbg(hdev, "Pausing directed advertising");
2388 /* Stop directed advertising */
2389 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2391 /* When discoverable timeout triggers, then just make sure
2392 * the limited discoverable flag is cleared. Even in the case
2393 * of a timeout triggered from general discoverable, it is
2394 * safe to unconditionally clear the flag.
2396 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2397 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2398 hdev->discov_timeout = 0;
2401 bt_dev_dbg(hdev, "Pausing advertising instances");
2403 /* Call to disable any advertisements active on the controller.
2404 * This will succeed even if no advertisements are configured.
2406 err = hci_disable_advertising_sync(hdev);
2410 /* If we are using software rotation, pause the loop */
2411 if (!ext_adv_capable(hdev))
2412 cancel_adv_timeout(hdev);
2414 hdev->advertising_paused = true;
2415 hdev->advertising_old_state = old_state;
2420 /* This function enables all user advertising instances */
2421 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2423 struct adv_info *adv, *tmp;
2426 /* If advertising has not been paused there is nothing to do. */
2427 if (!hdev->advertising_paused)
2430 /* Resume directed advertising */
2431 hdev->advertising_paused = false;
2432 if (hdev->advertising_old_state) {
2433 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2434 hdev->advertising_old_state = 0;
2437 bt_dev_dbg(hdev, "Resuming advertising instances");
2439 if (ext_adv_capable(hdev)) {
2440 /* Call for each tracked instance to be re-enabled */
2441 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2442 err = hci_enable_ext_advertising_sync(hdev,
2447 /* If the instance cannot be resumed remove it */
2448 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2452 /* Schedule for most recent instance to be restarted and begin
2453 * the software rotation loop
2455 err = hci_schedule_adv_instance_sync(hdev,
2456 hdev->cur_adv_instance,
2460 hdev->advertising_paused = false;
2465 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2469 if (!use_ll_privacy(hdev))
2472 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2475 /* Cannot disable addr resolution if scanning is enabled or
2476 * when initiating an LE connection.
2478 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2479 hci_lookup_le_connect(hdev)) {
2480 bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2484 /* Cannot disable addr resolution if advertising is enabled. */
2485 err = hci_pause_advertising_sync(hdev);
2487 bt_dev_err(hdev, "Pause advertising failed: %d", err);
2491 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2493 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2496 /* Return if address resolution is disabled and RPA is not used. */
2497 if (!err && scan_use_rpa(hdev))
2500 hci_resume_advertising_sync(hdev);
2504 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2505 bool extended, struct sock *sk)
2507 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2508 HCI_OP_READ_LOCAL_OOB_DATA;
2510 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2513 static struct conn_params *conn_params_copy(struct list_head *list, size_t *n)
2515 struct hci_conn_params *params;
2516 struct conn_params *p;
2522 list_for_each_entry_rcu(params, list, action)
2528 p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL);
2535 list_for_each_entry_rcu(params, list, action) {
2536 /* Racing adds are handled in next scan update */
2540 /* No hdev->lock, but: addr, addr_type are immutable.
2541 * privacy_mode is only written by us or in
2542 * hci_cc_le_set_privacy_mode that we wait for.
2543 * We should be idempotent so MGMT updating flags
2544 * while we are processing is OK.
2546 bacpy(&p[i].addr, ¶ms->addr);
2547 p[i].addr_type = params->addr_type;
2548 p[i].flags = READ_ONCE(params->flags);
2549 p[i].privacy_mode = READ_ONCE(params->privacy_mode);
2559 /* Clear LE Accept List */
2560 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
2562 if (!(hdev->commands[26] & 0x80))
2565 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
2569 /* Device must not be scanning when updating the accept list.
2571 * Update is done using the following sequence:
2573 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
2574 * Remove Devices From Accept List ->
2575 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
2576 * Add Devices to Accept List ->
2577 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
2578 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
2581 * In case of failure advertising shall be restored to its original state and
2582 * return would disable accept list since either accept or resolving list could
2583 * not be programmed.
2586 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2588 struct conn_params *params;
2589 struct bdaddr_list *b, *t;
2591 bool pend_conn, pend_report;
2596 /* Pause advertising if resolving list can be used as controllers
2597 * cannot accept resolving list modifications while advertising.
2599 if (use_ll_privacy(hdev)) {
2600 err = hci_pause_advertising_sync(hdev);
2602 bt_dev_err(hdev, "pause advertising failed: %d", err);
2607 /* Disable address resolution while reprogramming accept list since
2608 * devices that do have an IRK will be programmed in the resolving list
2609 * when LL Privacy is enabled.
2611 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2613 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2617 /* Force address filtering if PA Sync is in progress */
2618 if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2619 struct hci_cp_le_pa_create_sync *sent;
2621 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_PA_CREATE_SYNC);
2623 struct conn_params pa;
2625 memset(&pa, 0, sizeof(pa));
2627 bacpy(&pa.addr, &sent->addr);
2628 pa.addr_type = sent->addr_type;
2630 /* Clear first since there could be addresses left
2633 hci_le_clear_accept_list_sync(hdev);
2636 err = hci_le_add_accept_list_sync(hdev, &pa,
2642 /* Go through the current accept list programmed into the
2643 * controller one by one and check if that address is connected or is
2644 * still in the list of pending connections or list of devices to
2645 * report. If not present in either list, then remove it from
2648 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2649 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2652 /* Pointers not dereferenced, no locks needed */
2653 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2656 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2660 /* If the device is not likely to connect or report,
2661 * remove it from the acceptlist.
2663 if (!pend_conn && !pend_report) {
2664 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2672 /* Since all no longer valid accept list entries have been
2673 * removed, walk through the list of pending connections
2674 * and ensure that any new device gets programmed into
2677 * If the list of the devices is larger than the list of
2678 * available accept list entries in the controller, then
2679 * just abort and return filer policy value to not use the
2682 * The list and params may be mutated while we wait for events,
2683 * so make a copy and iterate it.
2686 params = conn_params_copy(&hdev->pend_le_conns, &n);
2692 for (i = 0; i < n; ++i) {
2693 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2703 /* After adding all new pending connections, walk through
2704 * the list of pending reports and also add these to the
2705 * accept list if there is still space. Abort if space runs out.
2708 params = conn_params_copy(&hdev->pend_le_reports, &n);
2714 for (i = 0; i < n; ++i) {
2715 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2725 /* Use the allowlist unless the following conditions are all true:
2726 * - We are not currently suspending
2727 * - There are 1 or more ADV monitors registered and it's not offloaded
2728 * - Interleaved scanning is not currently using the allowlist
2730 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2731 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2732 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2736 filter_policy = err ? 0x00 : 0x01;
2738 /* Enable address resolution when LL Privacy is enabled. */
2739 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2741 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2743 /* Resume advertising if it was paused */
2744 if (use_ll_privacy(hdev))
2745 hci_resume_advertising_sync(hdev);
2747 /* Select filter policy to use accept list */
2748 return filter_policy;
2751 static void hci_le_scan_phy_params(struct hci_cp_le_scan_phy_params *cp,
2752 u8 type, u16 interval, u16 window)
2755 cp->interval = cpu_to_le16(interval);
2756 cp->window = cpu_to_le16(window);
2759 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2760 u16 interval, u16 window,
2761 u8 own_addr_type, u8 filter_policy)
2763 struct hci_cp_le_set_ext_scan_params *cp;
2764 struct hci_cp_le_scan_phy_params *phy;
2765 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2769 phy = (void *)cp->data;
2771 memset(data, 0, sizeof(data));
2773 cp->own_addr_type = own_addr_type;
2774 cp->filter_policy = filter_policy;
2776 /* Check if PA Sync is in progress then select the PHY based on the
2779 if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2780 struct hci_cp_le_add_to_accept_list *sent;
2782 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
2784 struct hci_conn *conn;
2786 conn = hci_conn_hash_lookup_ba(hdev, ISO_LINK,
2789 struct bt_iso_qos *qos = &conn->iso_qos;
2791 if (qos->bcast.in.phy & BT_ISO_PHY_1M ||
2792 qos->bcast.in.phy & BT_ISO_PHY_2M) {
2793 cp->scanning_phys |= LE_SCAN_PHY_1M;
2794 hci_le_scan_phy_params(phy, type,
2801 if (qos->bcast.in.phy & BT_ISO_PHY_CODED) {
2802 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2803 hci_le_scan_phy_params(phy, type,
2816 if (scan_1m(hdev) || scan_2m(hdev)) {
2817 cp->scanning_phys |= LE_SCAN_PHY_1M;
2818 hci_le_scan_phy_params(phy, type, interval, window);
2823 if (scan_coded(hdev)) {
2824 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2825 hci_le_scan_phy_params(phy, type, interval * 3, window * 3);
2834 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2835 sizeof(*cp) + sizeof(*phy) * num_phy,
2836 data, HCI_CMD_TIMEOUT);
2839 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2840 u16 interval, u16 window,
2841 u8 own_addr_type, u8 filter_policy)
2843 struct hci_cp_le_set_scan_param cp;
2845 if (use_ext_scan(hdev))
2846 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2847 window, own_addr_type,
2850 memset(&cp, 0, sizeof(cp));
2852 cp.interval = cpu_to_le16(interval);
2853 cp.window = cpu_to_le16(window);
2854 cp.own_address_type = own_addr_type;
2855 cp.filter_policy = filter_policy;
2857 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2858 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2861 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2862 u16 window, u8 own_addr_type, u8 filter_policy,
2867 if (hdev->scanning_paused) {
2868 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2872 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2873 own_addr_type, filter_policy);
2877 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2880 static int hci_passive_scan_sync(struct hci_dev *hdev)
2884 u16 window, interval;
2885 u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2888 if (hdev->scanning_paused) {
2889 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2893 err = hci_scan_disable_sync(hdev);
2895 bt_dev_err(hdev, "disable scanning failed: %d", err);
2899 /* Set require_privacy to false since no SCAN_REQ are send
2900 * during passive scanning. Not using an non-resolvable address
2901 * here is important so that peer devices using direct
2902 * advertising with our address will be correctly reported
2903 * by the controller.
2905 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2909 if (hdev->enable_advmon_interleave_scan &&
2910 hci_update_interleaved_scan_sync(hdev))
2913 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2915 /* Adding or removing entries from the accept list must
2916 * happen before enabling scanning. The controller does
2917 * not allow accept list modification while scanning.
2919 filter_policy = hci_update_accept_list_sync(hdev);
2921 /* When the controller is using random resolvable addresses and
2922 * with that having LE privacy enabled, then controllers with
2923 * Extended Scanner Filter Policies support can now enable support
2924 * for handling directed advertising.
2926 * So instead of using filter polices 0x00 (no acceptlist)
2927 * and 0x01 (acceptlist enabled) use the new filter policies
2928 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2930 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2931 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2932 filter_policy |= 0x02;
2934 if (hdev->suspended) {
2935 window = hdev->le_scan_window_suspend;
2936 interval = hdev->le_scan_int_suspend;
2937 } else if (hci_is_le_conn_scanning(hdev)) {
2938 window = hdev->le_scan_window_connect;
2939 interval = hdev->le_scan_int_connect;
2940 } else if (hci_is_adv_monitoring(hdev)) {
2941 window = hdev->le_scan_window_adv_monitor;
2942 interval = hdev->le_scan_int_adv_monitor;
2944 window = hdev->le_scan_window;
2945 interval = hdev->le_scan_interval;
2948 /* Disable all filtering for Mesh */
2949 if (hci_dev_test_flag(hdev, HCI_MESH)) {
2951 filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
2954 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2956 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2957 own_addr_type, filter_policy, filter_dups);
2960 /* This function controls the passive scanning based on hdev->pend_le_conns
2961 * list. If there are pending LE connection we start the background scanning,
2962 * otherwise we stop it in the following sequence:
2964 * If there are devices to scan:
2966 * Disable Scanning -> Update Accept List ->
2967 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2968 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2975 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2979 if (!test_bit(HCI_UP, &hdev->flags) ||
2980 test_bit(HCI_INIT, &hdev->flags) ||
2981 hci_dev_test_flag(hdev, HCI_SETUP) ||
2982 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2983 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2984 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2987 /* No point in doing scanning if LE support hasn't been enabled */
2988 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2991 /* If discovery is active don't interfere with it */
2992 if (hdev->discovery.state != DISCOVERY_STOPPED)
2995 /* Reset RSSI and UUID filters when starting background scanning
2996 * since these filters are meant for service discovery only.
2998 * The Start Discovery and Start Service Discovery operations
2999 * ensure to set proper values for RSSI threshold and UUID
3000 * filter list. So it is safe to just reset them here.
3002 hci_discovery_filter_clear(hdev);
3004 bt_dev_dbg(hdev, "ADV monitoring is %s",
3005 hci_is_adv_monitoring(hdev) ? "on" : "off");
3007 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
3008 list_empty(&hdev->pend_le_conns) &&
3009 list_empty(&hdev->pend_le_reports) &&
3010 !hci_is_adv_monitoring(hdev) &&
3011 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
3012 /* If there is no pending LE connections or devices
3013 * to be scanned for or no ADV monitors, we should stop the
3014 * background scanning.
3017 bt_dev_dbg(hdev, "stopping background scanning");
3019 err = hci_scan_disable_sync(hdev);
3021 bt_dev_err(hdev, "stop background scanning failed: %d",
3024 /* If there is at least one pending LE connection, we should
3025 * keep the background scan running.
3028 /* If controller is connecting, we should not start scanning
3029 * since some controllers are not able to scan and connect at
3032 if (hci_lookup_le_connect(hdev))
3035 bt_dev_dbg(hdev, "start background scanning");
3037 err = hci_passive_scan_sync(hdev);
3039 bt_dev_err(hdev, "start background scanning failed: %d",
3046 static int update_scan_sync(struct hci_dev *hdev, void *data)
3048 return hci_update_scan_sync(hdev);
3051 int hci_update_scan(struct hci_dev *hdev)
3053 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
3056 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
3058 return hci_update_passive_scan_sync(hdev);
3061 int hci_update_passive_scan(struct hci_dev *hdev)
3063 /* Only queue if it would have any effect */
3064 if (!test_bit(HCI_UP, &hdev->flags) ||
3065 test_bit(HCI_INIT, &hdev->flags) ||
3066 hci_dev_test_flag(hdev, HCI_SETUP) ||
3067 hci_dev_test_flag(hdev, HCI_CONFIG) ||
3068 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
3069 hci_dev_test_flag(hdev, HCI_UNREGISTER))
3072 return hci_cmd_sync_queue_once(hdev, update_passive_scan_sync, NULL,
3076 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
3080 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
3083 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3084 sizeof(val), &val, HCI_CMD_TIMEOUT);
3088 hdev->features[1][0] |= LMP_HOST_SC;
3089 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
3091 hdev->features[1][0] &= ~LMP_HOST_SC;
3092 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
3099 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
3103 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3104 lmp_host_ssp_capable(hdev))
3107 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
3108 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
3109 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3112 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3113 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3117 return hci_write_sc_support_sync(hdev, 0x01);
3120 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
3122 struct hci_cp_write_le_host_supported cp;
3124 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
3125 !lmp_bredr_capable(hdev))
3128 /* Check first if we already have the right host state
3129 * (host features set)
3131 if (le == lmp_host_le_capable(hdev) &&
3132 simul == lmp_host_le_br_capable(hdev))
3135 memset(&cp, 0, sizeof(cp));
3140 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3141 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3144 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3146 struct adv_info *adv, *tmp;
3149 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3152 /* If RPA Resolution has not been enable yet it means the
3153 * resolving list is empty and we should attempt to program the
3154 * local IRK in order to support using own_addr_type
3155 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
3157 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3158 hci_le_add_resolve_list_sync(hdev, NULL);
3159 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3162 /* Make sure the controller has a good default for
3163 * advertising data. This also applies to the case
3164 * where BR/EDR was toggled during the AUTO_OFF phase.
3166 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3167 list_empty(&hdev->adv_instances)) {
3168 if (ext_adv_capable(hdev)) {
3169 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3171 hci_update_scan_rsp_data_sync(hdev, 0x00);
3173 err = hci_update_adv_data_sync(hdev, 0x00);
3175 hci_update_scan_rsp_data_sync(hdev, 0x00);
3178 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3179 hci_enable_advertising_sync(hdev);
3182 /* Call for each tracked instance to be scheduled */
3183 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3184 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3189 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3193 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3194 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3197 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3198 sizeof(link_sec), &link_sec,
3202 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3204 struct hci_cp_write_page_scan_activity cp;
3208 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3211 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3214 memset(&cp, 0, sizeof(cp));
3217 type = PAGE_SCAN_TYPE_INTERLACED;
3219 /* 160 msec page scan interval */
3220 cp.interval = cpu_to_le16(0x0100);
3222 type = hdev->def_page_scan_type;
3223 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3226 cp.window = cpu_to_le16(hdev->def_page_scan_window);
3228 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3229 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3230 err = __hci_cmd_sync_status(hdev,
3231 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
3232 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3237 if (hdev->page_scan_type != type)
3238 err = __hci_cmd_sync_status(hdev,
3239 HCI_OP_WRITE_PAGE_SCAN_TYPE,
3240 sizeof(type), &type,
3246 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3248 struct bdaddr_list *b;
3250 list_for_each_entry(b, &hdev->accept_list, list) {
3251 struct hci_conn *conn;
3253 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3257 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3264 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3266 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3271 int hci_update_scan_sync(struct hci_dev *hdev)
3275 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3278 if (!hdev_is_powered(hdev))
3281 if (mgmt_powering_down(hdev))
3284 if (hdev->scanning_paused)
3287 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3288 disconnected_accept_list_entries(hdev))
3291 scan = SCAN_DISABLED;
3293 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3294 scan |= SCAN_INQUIRY;
3296 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3297 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3300 return hci_write_scan_enable_sync(hdev, scan);
3303 int hci_update_name_sync(struct hci_dev *hdev)
3305 struct hci_cp_write_local_name cp;
3307 memset(&cp, 0, sizeof(cp));
3309 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3311 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3316 /* This function perform powered update HCI command sequence after the HCI init
3317 * sequence which end up resetting all states, the sequence is as follows:
3319 * HCI_SSP_ENABLED(Enable SSP)
3320 * HCI_LE_ENABLED(Enable LE)
3321 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
3323 * Enable Authentication
3324 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3325 * Set Name -> Set EIR)
3326 * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address)
3328 int hci_powered_update_sync(struct hci_dev *hdev)
3332 /* Register the available SMP channels (BR/EDR and LE) only when
3333 * successfully powering on the controller. This late
3334 * registration is required so that LE SMP can clearly decide if
3335 * the public address or static address is used.
3339 err = hci_write_ssp_mode_sync(hdev, 0x01);
3343 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3347 err = hci_powered_update_adv_sync(hdev);
3351 err = hci_write_auth_enable_sync(hdev);
3355 if (lmp_bredr_capable(hdev)) {
3356 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3357 hci_write_fast_connectable_sync(hdev, true);
3359 hci_write_fast_connectable_sync(hdev, false);
3360 hci_update_scan_sync(hdev);
3361 hci_update_class_sync(hdev);
3362 hci_update_name_sync(hdev);
3363 hci_update_eir_sync(hdev);
3366 /* If forcing static address is in use or there is no public
3367 * address use the static address as random address (but skip
3368 * the HCI command if the current random address is already the
3371 * In case BR/EDR has been disabled on a dual-mode controller
3372 * and a static address has been configured, then use that
3373 * address instead of the public BR/EDR address.
3375 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3376 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3377 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3378 if (bacmp(&hdev->static_addr, BDADDR_ANY))
3379 return hci_set_random_addr_sync(hdev,
3380 &hdev->static_addr);
3387 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3388 * (BD_ADDR) for a HCI device from
3389 * a firmware node property.
3390 * @hdev: The HCI device
3392 * Search the firmware node for 'local-bd-address'.
3394 * All-zero BD addresses are rejected, because those could be properties
3395 * that exist in the firmware tables, but were not updated by the firmware. For
3396 * example, the DTS could define 'local-bd-address', with zero BD addresses.
3398 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3400 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3404 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3405 (u8 *)&ba, sizeof(ba));
3406 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3409 if (test_bit(HCI_QUIRK_BDADDR_PROPERTY_BROKEN, &hdev->quirks))
3410 baswap(&hdev->public_addr, &ba);
3412 bacpy(&hdev->public_addr, &ba);
3415 struct hci_init_stage {
3416 int (*func)(struct hci_dev *hdev);
3419 /* Run init stage NULL terminated function table */
3420 static int hci_init_stage_sync(struct hci_dev *hdev,
3421 const struct hci_init_stage *stage)
3425 for (i = 0; stage[i].func; i++) {
3428 err = stage[i].func(hdev);
3436 /* Read Local Version */
3437 static int hci_read_local_version_sync(struct hci_dev *hdev)
3439 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3440 0, NULL, HCI_CMD_TIMEOUT);
3443 /* Read BD Address */
3444 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3446 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3447 0, NULL, HCI_CMD_TIMEOUT);
3450 #define HCI_INIT(_func) \
3455 static const struct hci_init_stage hci_init0[] = {
3456 /* HCI_OP_READ_LOCAL_VERSION */
3457 HCI_INIT(hci_read_local_version_sync),
3458 /* HCI_OP_READ_BD_ADDR */
3459 HCI_INIT(hci_read_bd_addr_sync),
3463 int hci_reset_sync(struct hci_dev *hdev)
3467 set_bit(HCI_RESET, &hdev->flags);
3469 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3477 static int hci_init0_sync(struct hci_dev *hdev)
3481 bt_dev_dbg(hdev, "");
3484 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3485 err = hci_reset_sync(hdev);
3490 return hci_init_stage_sync(hdev, hci_init0);
3493 static int hci_unconf_init_sync(struct hci_dev *hdev)
3497 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3500 err = hci_init0_sync(hdev);
3504 if (hci_dev_test_flag(hdev, HCI_SETUP))
3505 hci_debugfs_create_basic(hdev);
3510 /* Read Local Supported Features. */
3511 static int hci_read_local_features_sync(struct hci_dev *hdev)
3513 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3514 0, NULL, HCI_CMD_TIMEOUT);
3517 /* BR Controller init stage 1 command sequence */
3518 static const struct hci_init_stage br_init1[] = {
3519 /* HCI_OP_READ_LOCAL_FEATURES */
3520 HCI_INIT(hci_read_local_features_sync),
3521 /* HCI_OP_READ_LOCAL_VERSION */
3522 HCI_INIT(hci_read_local_version_sync),
3523 /* HCI_OP_READ_BD_ADDR */
3524 HCI_INIT(hci_read_bd_addr_sync),
3528 /* Read Local Commands */
3529 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3531 /* All Bluetooth 1.2 and later controllers should support the
3532 * HCI command for reading the local supported commands.
3534 * Unfortunately some controllers indicate Bluetooth 1.2 support,
3535 * but do not have support for this command. If that is the case,
3536 * the driver can quirk the behavior and skip reading the local
3537 * supported commands.
3539 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3540 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3541 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3542 0, NULL, HCI_CMD_TIMEOUT);
3547 static int hci_init1_sync(struct hci_dev *hdev)
3551 bt_dev_dbg(hdev, "");
3554 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3555 err = hci_reset_sync(hdev);
3560 return hci_init_stage_sync(hdev, br_init1);
3563 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
3564 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3566 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3567 0, NULL, HCI_CMD_TIMEOUT);
3570 /* Read Class of Device */
3571 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3573 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3574 0, NULL, HCI_CMD_TIMEOUT);
3577 /* Read Local Name */
3578 static int hci_read_local_name_sync(struct hci_dev *hdev)
3580 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3581 0, NULL, HCI_CMD_TIMEOUT);
3584 /* Read Voice Setting */
3585 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3587 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3588 0, NULL, HCI_CMD_TIMEOUT);
3591 /* Read Number of Supported IAC */
3592 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3594 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3595 0, NULL, HCI_CMD_TIMEOUT);
3598 /* Read Current IAC LAP */
3599 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3601 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3602 0, NULL, HCI_CMD_TIMEOUT);
3605 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3606 u8 cond_type, bdaddr_t *bdaddr,
3609 struct hci_cp_set_event_filter cp;
3611 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3614 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3617 memset(&cp, 0, sizeof(cp));
3618 cp.flt_type = flt_type;
3620 if (flt_type != HCI_FLT_CLEAR_ALL) {
3621 cp.cond_type = cond_type;
3622 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3623 cp.addr_conn_flt.auto_accept = auto_accept;
3626 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3627 flt_type == HCI_FLT_CLEAR_ALL ?
3628 sizeof(cp.flt_type) : sizeof(cp), &cp,
3632 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3634 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3637 /* In theory the state machine should not reach here unless
3638 * a hci_set_event_filter_sync() call succeeds, but we do
3639 * the check both for parity and as a future reminder.
3641 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3644 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3648 /* Connection accept timeout ~20 secs */
3649 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3651 __le16 param = cpu_to_le16(0x7d00);
3653 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3654 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
3657 /* BR Controller init stage 2 command sequence */
3658 static const struct hci_init_stage br_init2[] = {
3659 /* HCI_OP_READ_BUFFER_SIZE */
3660 HCI_INIT(hci_read_buffer_size_sync),
3661 /* HCI_OP_READ_CLASS_OF_DEV */
3662 HCI_INIT(hci_read_dev_class_sync),
3663 /* HCI_OP_READ_LOCAL_NAME */
3664 HCI_INIT(hci_read_local_name_sync),
3665 /* HCI_OP_READ_VOICE_SETTING */
3666 HCI_INIT(hci_read_voice_setting_sync),
3667 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
3668 HCI_INIT(hci_read_num_supported_iac_sync),
3669 /* HCI_OP_READ_CURRENT_IAC_LAP */
3670 HCI_INIT(hci_read_current_iac_lap_sync),
3671 /* HCI_OP_SET_EVENT_FLT */
3672 HCI_INIT(hci_clear_event_filter_sync),
3673 /* HCI_OP_WRITE_CA_TIMEOUT */
3674 HCI_INIT(hci_write_ca_timeout_sync),
3678 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3682 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3685 /* When SSP is available, then the host features page
3686 * should also be available as well. However some
3687 * controllers list the max_page as 0 as long as SSP
3688 * has not been enabled. To achieve proper debugging
3689 * output, force the minimum max_page to 1 at least.
3691 hdev->max_page = 0x01;
3693 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3694 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3697 static int hci_write_eir_sync(struct hci_dev *hdev)
3699 struct hci_cp_write_eir cp;
3701 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3704 memset(hdev->eir, 0, sizeof(hdev->eir));
3705 memset(&cp, 0, sizeof(cp));
3707 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3711 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3715 if (!lmp_inq_rssi_capable(hdev) &&
3716 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3719 /* If Extended Inquiry Result events are supported, then
3720 * they are clearly preferred over Inquiry Result with RSSI
3723 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3725 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3726 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3729 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3731 if (!lmp_inq_tx_pwr_capable(hdev))
3734 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3735 0, NULL, HCI_CMD_TIMEOUT);
3738 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3740 struct hci_cp_read_local_ext_features cp;
3742 if (!lmp_ext_feat_capable(hdev))
3745 memset(&cp, 0, sizeof(cp));
3748 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3749 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3752 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3754 return hci_read_local_ext_features_sync(hdev, 0x01);
3757 /* HCI Controller init stage 2 command sequence */
3758 static const struct hci_init_stage hci_init2[] = {
3759 /* HCI_OP_READ_LOCAL_COMMANDS */
3760 HCI_INIT(hci_read_local_cmds_sync),
3761 /* HCI_OP_WRITE_SSP_MODE */
3762 HCI_INIT(hci_write_ssp_mode_1_sync),
3763 /* HCI_OP_WRITE_EIR */
3764 HCI_INIT(hci_write_eir_sync),
3765 /* HCI_OP_WRITE_INQUIRY_MODE */
3766 HCI_INIT(hci_write_inquiry_mode_sync),
3767 /* HCI_OP_READ_INQ_RSP_TX_POWER */
3768 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3769 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3770 HCI_INIT(hci_read_local_ext_features_1_sync),
3771 /* HCI_OP_WRITE_AUTH_ENABLE */
3772 HCI_INIT(hci_write_auth_enable_sync),
3776 /* Read LE Buffer Size */
3777 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3779 /* Use Read LE Buffer Size V2 if supported */
3780 if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3781 return __hci_cmd_sync_status(hdev,
3782 HCI_OP_LE_READ_BUFFER_SIZE_V2,
3783 0, NULL, HCI_CMD_TIMEOUT);
3785 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3786 0, NULL, HCI_CMD_TIMEOUT);
3789 /* Read LE Local Supported Features */
3790 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3792 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3793 0, NULL, HCI_CMD_TIMEOUT);
3796 /* Read LE Supported States */
3797 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3799 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3800 0, NULL, HCI_CMD_TIMEOUT);
3803 /* LE Controller init stage 2 command sequence */
3804 static const struct hci_init_stage le_init2[] = {
3805 /* HCI_OP_LE_READ_LOCAL_FEATURES */
3806 HCI_INIT(hci_le_read_local_features_sync),
3807 /* HCI_OP_LE_READ_BUFFER_SIZE */
3808 HCI_INIT(hci_le_read_buffer_size_sync),
3809 /* HCI_OP_LE_READ_SUPPORTED_STATES */
3810 HCI_INIT(hci_le_read_supported_states_sync),
3814 static int hci_init2_sync(struct hci_dev *hdev)
3818 bt_dev_dbg(hdev, "");
3820 err = hci_init_stage_sync(hdev, hci_init2);
3824 if (lmp_bredr_capable(hdev)) {
3825 err = hci_init_stage_sync(hdev, br_init2);
3829 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3832 if (lmp_le_capable(hdev)) {
3833 err = hci_init_stage_sync(hdev, le_init2);
3836 /* LE-only controllers have LE implicitly enabled */
3837 if (!lmp_bredr_capable(hdev))
3838 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3844 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3846 /* The second byte is 0xff instead of 0x9f (two reserved bits
3847 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3848 * command otherwise.
3850 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3852 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3853 * any event mask for pre 1.2 devices.
3855 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3858 if (lmp_bredr_capable(hdev)) {
3859 events[4] |= 0x01; /* Flow Specification Complete */
3861 /* Don't set Disconnect Complete and mode change when
3862 * suspended as that would wakeup the host when disconnecting
3865 if (hdev->suspended) {
3870 /* Use a different default for LE-only devices */
3871 memset(events, 0, sizeof(events));
3872 events[1] |= 0x20; /* Command Complete */
3873 events[1] |= 0x40; /* Command Status */
3874 events[1] |= 0x80; /* Hardware Error */
3876 /* If the controller supports the Disconnect command, enable
3877 * the corresponding event. In addition enable packet flow
3878 * control related events.
3880 if (hdev->commands[0] & 0x20) {
3881 /* Don't set Disconnect Complete when suspended as that
3882 * would wakeup the host when disconnecting due to
3885 if (!hdev->suspended)
3886 events[0] |= 0x10; /* Disconnection Complete */
3887 events[2] |= 0x04; /* Number of Completed Packets */
3888 events[3] |= 0x02; /* Data Buffer Overflow */
3891 /* If the controller supports the Read Remote Version
3892 * Information command, enable the corresponding event.
3894 if (hdev->commands[2] & 0x80)
3895 events[1] |= 0x08; /* Read Remote Version Information
3899 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3900 events[0] |= 0x80; /* Encryption Change */
3901 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3905 if (lmp_inq_rssi_capable(hdev) ||
3906 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3907 events[4] |= 0x02; /* Inquiry Result with RSSI */
3909 if (lmp_ext_feat_capable(hdev))
3910 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3912 if (lmp_esco_capable(hdev)) {
3913 events[5] |= 0x08; /* Synchronous Connection Complete */
3914 events[5] |= 0x10; /* Synchronous Connection Changed */
3917 if (lmp_sniffsubr_capable(hdev))
3918 events[5] |= 0x20; /* Sniff Subrating */
3920 if (lmp_pause_enc_capable(hdev))
3921 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3923 if (lmp_ext_inq_capable(hdev))
3924 events[5] |= 0x40; /* Extended Inquiry Result */
3926 if (lmp_no_flush_capable(hdev))
3927 events[7] |= 0x01; /* Enhanced Flush Complete */
3929 if (lmp_lsto_capable(hdev))
3930 events[6] |= 0x80; /* Link Supervision Timeout Changed */
3932 if (lmp_ssp_capable(hdev)) {
3933 events[6] |= 0x01; /* IO Capability Request */
3934 events[6] |= 0x02; /* IO Capability Response */
3935 events[6] |= 0x04; /* User Confirmation Request */
3936 events[6] |= 0x08; /* User Passkey Request */
3937 events[6] |= 0x10; /* Remote OOB Data Request */
3938 events[6] |= 0x20; /* Simple Pairing Complete */
3939 events[7] |= 0x04; /* User Passkey Notification */
3940 events[7] |= 0x08; /* Keypress Notification */
3941 events[7] |= 0x10; /* Remote Host Supported
3942 * Features Notification
3946 if (lmp_le_capable(hdev))
3947 events[7] |= 0x20; /* LE Meta-Event */
3949 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3950 sizeof(events), events, HCI_CMD_TIMEOUT);
3953 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3955 struct hci_cp_read_stored_link_key cp;
3957 if (!(hdev->commands[6] & 0x20) ||
3958 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3961 memset(&cp, 0, sizeof(cp));
3962 bacpy(&cp.bdaddr, BDADDR_ANY);
3965 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3966 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3969 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3971 struct hci_cp_write_def_link_policy cp;
3972 u16 link_policy = 0;
3974 if (!(hdev->commands[5] & 0x10))
3977 memset(&cp, 0, sizeof(cp));
3979 if (lmp_rswitch_capable(hdev))
3980 link_policy |= HCI_LP_RSWITCH;
3981 if (lmp_hold_capable(hdev))
3982 link_policy |= HCI_LP_HOLD;
3983 if (lmp_sniff_capable(hdev))
3984 link_policy |= HCI_LP_SNIFF;
3985 if (lmp_park_capable(hdev))
3986 link_policy |= HCI_LP_PARK;
3988 cp.policy = cpu_to_le16(link_policy);
3990 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3991 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3994 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3996 if (!(hdev->commands[8] & 0x01))
3999 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
4000 0, NULL, HCI_CMD_TIMEOUT);
4003 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
4005 if (!(hdev->commands[18] & 0x04) ||
4006 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4007 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4010 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
4011 0, NULL, HCI_CMD_TIMEOUT);
4014 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
4016 /* Some older Broadcom based Bluetooth 1.2 controllers do not
4017 * support the Read Page Scan Type command. Check support for
4018 * this command in the bit mask of supported commands.
4020 if (!(hdev->commands[13] & 0x01))
4023 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
4024 0, NULL, HCI_CMD_TIMEOUT);
4027 /* Read features beyond page 1 if available */
4028 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
4033 if (!lmp_ext_feat_capable(hdev))
4036 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
4038 err = hci_read_local_ext_features_sync(hdev, page);
4046 /* HCI Controller init stage 3 command sequence */
4047 static const struct hci_init_stage hci_init3[] = {
4048 /* HCI_OP_SET_EVENT_MASK */
4049 HCI_INIT(hci_set_event_mask_sync),
4050 /* HCI_OP_READ_STORED_LINK_KEY */
4051 HCI_INIT(hci_read_stored_link_key_sync),
4052 /* HCI_OP_WRITE_DEF_LINK_POLICY */
4053 HCI_INIT(hci_setup_link_policy_sync),
4054 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
4055 HCI_INIT(hci_read_page_scan_activity_sync),
4056 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
4057 HCI_INIT(hci_read_def_err_data_reporting_sync),
4058 /* HCI_OP_READ_PAGE_SCAN_TYPE */
4059 HCI_INIT(hci_read_page_scan_type_sync),
4060 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
4061 HCI_INIT(hci_read_local_ext_features_all_sync),
4065 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4069 if (!lmp_le_capable(hdev))
4072 memset(events, 0, sizeof(events));
4074 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4075 events[0] |= 0x10; /* LE Long Term Key Request */
4077 /* If controller supports the Connection Parameters Request
4078 * Link Layer Procedure, enable the corresponding event.
4080 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4081 /* LE Remote Connection Parameter Request */
4084 /* If the controller supports the Data Length Extension
4085 * feature, enable the corresponding event.
4087 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4088 events[0] |= 0x40; /* LE Data Length Change */
4090 /* If the controller supports LL Privacy feature or LE Extended Adv,
4091 * enable the corresponding event.
4093 if (use_enhanced_conn_complete(hdev))
4094 events[1] |= 0x02; /* LE Enhanced Connection Complete */
4096 /* If the controller supports Extended Scanner Filter
4097 * Policies, enable the corresponding event.
4099 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4100 events[1] |= 0x04; /* LE Direct Advertising Report */
4102 /* If the controller supports Channel Selection Algorithm #2
4103 * feature, enable the corresponding event.
4105 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4106 events[2] |= 0x08; /* LE Channel Selection Algorithm */
4108 /* If the controller supports the LE Set Scan Enable command,
4109 * enable the corresponding advertising report event.
4111 if (hdev->commands[26] & 0x08)
4112 events[0] |= 0x02; /* LE Advertising Report */
4114 /* If the controller supports the LE Create Connection
4115 * command, enable the corresponding event.
4117 if (hdev->commands[26] & 0x10)
4118 events[0] |= 0x01; /* LE Connection Complete */
4120 /* If the controller supports the LE Connection Update
4121 * command, enable the corresponding event.
4123 if (hdev->commands[27] & 0x04)
4124 events[0] |= 0x04; /* LE Connection Update Complete */
4126 /* If the controller supports the LE Read Remote Used Features
4127 * command, enable the corresponding event.
4129 if (hdev->commands[27] & 0x20)
4130 /* LE Read Remote Used Features Complete */
4133 /* If the controller supports the LE Read Local P-256
4134 * Public Key command, enable the corresponding event.
4136 if (hdev->commands[34] & 0x02)
4137 /* LE Read Local P-256 Public Key Complete */
4140 /* If the controller supports the LE Generate DHKey
4141 * command, enable the corresponding event.
4143 if (hdev->commands[34] & 0x04)
4144 events[1] |= 0x01; /* LE Generate DHKey Complete */
4146 /* If the controller supports the LE Set Default PHY or
4147 * LE Set PHY commands, enable the corresponding event.
4149 if (hdev->commands[35] & (0x20 | 0x40))
4150 events[1] |= 0x08; /* LE PHY Update Complete */
4152 /* If the controller supports LE Set Extended Scan Parameters
4153 * and LE Set Extended Scan Enable commands, enable the
4154 * corresponding event.
4156 if (use_ext_scan(hdev))
4157 events[1] |= 0x10; /* LE Extended Advertising Report */
4159 /* If the controller supports the LE Extended Advertising
4160 * command, enable the corresponding event.
4162 if (ext_adv_capable(hdev))
4163 events[2] |= 0x02; /* LE Advertising Set Terminated */
4165 if (cis_capable(hdev)) {
4166 events[3] |= 0x01; /* LE CIS Established */
4167 if (cis_peripheral_capable(hdev))
4168 events[3] |= 0x02; /* LE CIS Request */
4171 if (bis_capable(hdev)) {
4172 events[1] |= 0x20; /* LE PA Report */
4173 events[1] |= 0x40; /* LE PA Sync Established */
4174 events[3] |= 0x04; /* LE Create BIG Complete */
4175 events[3] |= 0x08; /* LE Terminate BIG Complete */
4176 events[3] |= 0x10; /* LE BIG Sync Established */
4177 events[3] |= 0x20; /* LE BIG Sync Loss */
4178 events[4] |= 0x02; /* LE BIG Info Advertising Report */
4181 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4182 sizeof(events), events, HCI_CMD_TIMEOUT);
4185 /* Read LE Advertising Channel TX Power */
4186 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4188 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4189 /* HCI TS spec forbids mixing of legacy and extended
4190 * advertising commands wherein READ_ADV_TX_POWER is
4191 * also included. So do not call it if extended adv
4192 * is supported otherwise controller will return
4193 * COMMAND_DISALLOWED for extended commands.
4195 return __hci_cmd_sync_status(hdev,
4196 HCI_OP_LE_READ_ADV_TX_POWER,
4197 0, NULL, HCI_CMD_TIMEOUT);
4203 /* Read LE Min/Max Tx Power*/
4204 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4206 if (!(hdev->commands[38] & 0x80) ||
4207 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4210 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4211 0, NULL, HCI_CMD_TIMEOUT);
4214 /* Read LE Accept List Size */
4215 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4217 if (!(hdev->commands[26] & 0x40))
4220 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4221 0, NULL, HCI_CMD_TIMEOUT);
4224 /* Read LE Resolving List Size */
4225 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4227 if (!(hdev->commands[34] & 0x40))
4230 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4231 0, NULL, HCI_CMD_TIMEOUT);
4234 /* Clear LE Resolving List */
4235 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4237 if (!(hdev->commands[34] & 0x20))
4240 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4244 /* Set RPA timeout */
4245 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4247 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4249 if (!(hdev->commands[35] & 0x04) ||
4250 test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4253 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4254 sizeof(timeout), &timeout,
4258 /* Read LE Maximum Data Length */
4259 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4261 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4264 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4268 /* Read LE Suggested Default Data Length */
4269 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4271 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4274 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4278 /* Read LE Number of Supported Advertising Sets */
4279 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4281 if (!ext_adv_capable(hdev))
4284 return __hci_cmd_sync_status(hdev,
4285 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4286 0, NULL, HCI_CMD_TIMEOUT);
4289 /* Write LE Host Supported */
4290 static int hci_set_le_support_sync(struct hci_dev *hdev)
4292 struct hci_cp_write_le_host_supported cp;
4294 /* LE-only devices do not support explicit enablement */
4295 if (!lmp_bredr_capable(hdev))
4298 memset(&cp, 0, sizeof(cp));
4300 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4305 if (cp.le == lmp_host_le_capable(hdev))
4308 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4309 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4312 /* LE Set Host Feature */
4313 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4315 struct hci_cp_le_set_host_feature cp;
4317 if (!cis_capable(hdev))
4320 memset(&cp, 0, sizeof(cp));
4322 /* Connected Isochronous Channels (Host Support) */
4326 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4327 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4330 /* LE Controller init stage 3 command sequence */
4331 static const struct hci_init_stage le_init3[] = {
4332 /* HCI_OP_LE_SET_EVENT_MASK */
4333 HCI_INIT(hci_le_set_event_mask_sync),
4334 /* HCI_OP_LE_READ_ADV_TX_POWER */
4335 HCI_INIT(hci_le_read_adv_tx_power_sync),
4336 /* HCI_OP_LE_READ_TRANSMIT_POWER */
4337 HCI_INIT(hci_le_read_tx_power_sync),
4338 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4339 HCI_INIT(hci_le_read_accept_list_size_sync),
4340 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4341 HCI_INIT(hci_le_clear_accept_list_sync),
4342 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4343 HCI_INIT(hci_le_read_resolv_list_size_sync),
4344 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
4345 HCI_INIT(hci_le_clear_resolv_list_sync),
4346 /* HCI_OP_LE_SET_RPA_TIMEOUT */
4347 HCI_INIT(hci_le_set_rpa_timeout_sync),
4348 /* HCI_OP_LE_READ_MAX_DATA_LEN */
4349 HCI_INIT(hci_le_read_max_data_len_sync),
4350 /* HCI_OP_LE_READ_DEF_DATA_LEN */
4351 HCI_INIT(hci_le_read_def_data_len_sync),
4352 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4353 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4354 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4355 HCI_INIT(hci_set_le_support_sync),
4356 /* HCI_OP_LE_SET_HOST_FEATURE */
4357 HCI_INIT(hci_le_set_host_feature_sync),
4361 static int hci_init3_sync(struct hci_dev *hdev)
4365 bt_dev_dbg(hdev, "");
4367 err = hci_init_stage_sync(hdev, hci_init3);
4371 if (lmp_le_capable(hdev))
4372 return hci_init_stage_sync(hdev, le_init3);
4377 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4379 struct hci_cp_delete_stored_link_key cp;
4381 /* Some Broadcom based Bluetooth controllers do not support the
4382 * Delete Stored Link Key command. They are clearly indicating its
4383 * absence in the bit mask of supported commands.
4385 * Check the supported commands and only if the command is marked
4386 * as supported send it. If not supported assume that the controller
4387 * does not have actual support for stored link keys which makes this
4388 * command redundant anyway.
4390 * Some controllers indicate that they support handling deleting
4391 * stored link keys, but they don't. The quirk lets a driver
4392 * just disable this command.
4394 if (!(hdev->commands[6] & 0x80) ||
4395 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4398 memset(&cp, 0, sizeof(cp));
4399 bacpy(&cp.bdaddr, BDADDR_ANY);
4400 cp.delete_all = 0x01;
4402 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4403 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4406 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4408 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4409 bool changed = false;
4411 /* Set event mask page 2 if the HCI command for it is supported */
4412 if (!(hdev->commands[22] & 0x04))
4415 /* If Connectionless Peripheral Broadcast central role is supported
4416 * enable all necessary events for it.
4418 if (lmp_cpb_central_capable(hdev)) {
4419 events[1] |= 0x40; /* Triggered Clock Capture */
4420 events[1] |= 0x80; /* Synchronization Train Complete */
4421 events[2] |= 0x08; /* Truncated Page Complete */
4422 events[2] |= 0x20; /* CPB Channel Map Change */
4426 /* If Connectionless Peripheral Broadcast peripheral role is supported
4427 * enable all necessary events for it.
4429 if (lmp_cpb_peripheral_capable(hdev)) {
4430 events[2] |= 0x01; /* Synchronization Train Received */
4431 events[2] |= 0x02; /* CPB Receive */
4432 events[2] |= 0x04; /* CPB Timeout */
4433 events[2] |= 0x10; /* Peripheral Page Response Timeout */
4437 /* Enable Authenticated Payload Timeout Expired event if supported */
4438 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4443 /* Some Broadcom based controllers indicate support for Set Event
4444 * Mask Page 2 command, but then actually do not support it. Since
4445 * the default value is all bits set to zero, the command is only
4446 * required if the event mask has to be changed. In case no change
4447 * to the event mask is needed, skip this command.
4452 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4453 sizeof(events), events, HCI_CMD_TIMEOUT);
4456 /* Read local codec list if the HCI command is supported */
4457 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4459 if (hdev->commands[45] & 0x04)
4460 hci_read_supported_codecs_v2(hdev);
4461 else if (hdev->commands[29] & 0x20)
4462 hci_read_supported_codecs(hdev);
4467 /* Read local pairing options if the HCI command is supported */
4468 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4470 if (!(hdev->commands[41] & 0x08))
4473 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4474 0, NULL, HCI_CMD_TIMEOUT);
4477 /* Get MWS transport configuration if the HCI command is supported */
4478 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4480 if (!mws_transport_config_capable(hdev))
4483 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4484 0, NULL, HCI_CMD_TIMEOUT);
4487 /* Check for Synchronization Train support */
4488 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4490 if (!lmp_sync_train_capable(hdev))
4493 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4494 0, NULL, HCI_CMD_TIMEOUT);
4497 /* Enable Secure Connections if supported and configured */
4498 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4502 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4503 !bredr_sc_enabled(hdev))
4506 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4507 sizeof(support), &support,
4511 /* Set erroneous data reporting if supported to the wideband speech
4514 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4516 struct hci_cp_write_def_err_data_reporting cp;
4517 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4519 if (!(hdev->commands[18] & 0x08) ||
4520 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4521 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4524 if (enabled == hdev->err_data_reporting)
4527 memset(&cp, 0, sizeof(cp));
4528 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4529 ERR_DATA_REPORTING_DISABLED;
4531 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4532 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4535 static const struct hci_init_stage hci_init4[] = {
4536 /* HCI_OP_DELETE_STORED_LINK_KEY */
4537 HCI_INIT(hci_delete_stored_link_key_sync),
4538 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4539 HCI_INIT(hci_set_event_mask_page_2_sync),
4540 /* HCI_OP_READ_LOCAL_CODECS */
4541 HCI_INIT(hci_read_local_codecs_sync),
4542 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4543 HCI_INIT(hci_read_local_pairing_opts_sync),
4544 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4545 HCI_INIT(hci_get_mws_transport_config_sync),
4546 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4547 HCI_INIT(hci_read_sync_train_params_sync),
4548 /* HCI_OP_WRITE_SC_SUPPORT */
4549 HCI_INIT(hci_write_sc_support_1_sync),
4550 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4551 HCI_INIT(hci_set_err_data_report_sync),
4555 /* Set Suggested Default Data Length to maximum if supported */
4556 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4558 struct hci_cp_le_write_def_data_len cp;
4560 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4563 memset(&cp, 0, sizeof(cp));
4564 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4565 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4567 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4568 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4571 /* Set Default PHY parameters if command is supported, enables all supported
4572 * PHYs according to the LE Features bits.
4574 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4576 struct hci_cp_le_set_default_phy cp;
4578 if (!(hdev->commands[35] & 0x20)) {
4579 /* If the command is not supported it means only 1M PHY is
4582 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4583 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4587 memset(&cp, 0, sizeof(cp));
4589 cp.tx_phys = HCI_LE_SET_PHY_1M;
4590 cp.rx_phys = HCI_LE_SET_PHY_1M;
4592 /* Enables 2M PHY if supported */
4593 if (le_2m_capable(hdev)) {
4594 cp.tx_phys |= HCI_LE_SET_PHY_2M;
4595 cp.rx_phys |= HCI_LE_SET_PHY_2M;
4598 /* Enables Coded PHY if supported */
4599 if (le_coded_capable(hdev)) {
4600 cp.tx_phys |= HCI_LE_SET_PHY_CODED;
4601 cp.rx_phys |= HCI_LE_SET_PHY_CODED;
4604 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4605 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4608 static const struct hci_init_stage le_init4[] = {
4609 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4610 HCI_INIT(hci_le_set_write_def_data_len_sync),
4611 /* HCI_OP_LE_SET_DEFAULT_PHY */
4612 HCI_INIT(hci_le_set_default_phy_sync),
4616 static int hci_init4_sync(struct hci_dev *hdev)
4620 bt_dev_dbg(hdev, "");
4622 err = hci_init_stage_sync(hdev, hci_init4);
4626 if (lmp_le_capable(hdev))
4627 return hci_init_stage_sync(hdev, le_init4);
4632 static int hci_init_sync(struct hci_dev *hdev)
4636 err = hci_init1_sync(hdev);
4640 if (hci_dev_test_flag(hdev, HCI_SETUP))
4641 hci_debugfs_create_basic(hdev);
4643 err = hci_init2_sync(hdev);
4647 err = hci_init3_sync(hdev);
4651 err = hci_init4_sync(hdev);
4655 /* This function is only called when the controller is actually in
4656 * configured state. When the controller is marked as unconfigured,
4657 * this initialization procedure is not run.
4659 * It means that it is possible that a controller runs through its
4660 * setup phase and then discovers missing settings. If that is the
4661 * case, then this function will not be called. It then will only
4662 * be called during the config phase.
4664 * So only when in setup phase or config phase, create the debugfs
4665 * entries and register the SMP channels.
4667 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4668 !hci_dev_test_flag(hdev, HCI_CONFIG))
4671 if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4674 hci_debugfs_create_common(hdev);
4676 if (lmp_bredr_capable(hdev))
4677 hci_debugfs_create_bredr(hdev);
4679 if (lmp_le_capable(hdev))
4680 hci_debugfs_create_le(hdev);
4685 #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
4687 static const struct {
4688 unsigned long quirk;
4690 } hci_broken_table[] = {
4691 HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
4692 "HCI Read Local Supported Commands not supported"),
4693 HCI_QUIRK_BROKEN(STORED_LINK_KEY,
4694 "HCI Delete Stored Link Key command is advertised, "
4695 "but not supported."),
4696 HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
4697 "HCI Read Default Erroneous Data Reporting command is "
4698 "advertised, but not supported."),
4699 HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
4700 "HCI Read Transmit Power Level command is advertised, "
4701 "but not supported."),
4702 HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
4703 "HCI Set Event Filter command not supported."),
4704 HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
4705 "HCI Enhanced Setup Synchronous Connection command is "
4706 "advertised, but not supported."),
4707 HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
4708 "HCI LE Set Random Private Address Timeout command is "
4709 "advertised, but not supported."),
4710 HCI_QUIRK_BROKEN(LE_CODED,
4711 "HCI LE Coded PHY feature bit is set, "
4712 "but its usage is not supported.")
4715 /* This function handles hdev setup stage:
4718 * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
4720 static int hci_dev_setup_sync(struct hci_dev *hdev)
4723 bool invalid_bdaddr;
4726 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4727 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4730 bt_dev_dbg(hdev, "");
4732 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4735 ret = hdev->setup(hdev);
4737 for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
4738 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4739 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4742 /* The transport driver can set the quirk to mark the
4743 * BD_ADDR invalid before creating the HCI device or in
4744 * its setup callback.
4746 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
4747 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
4749 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4750 !bacmp(&hdev->public_addr, BDADDR_ANY))
4751 hci_dev_get_bd_addr_from_property(hdev);
4753 if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
4755 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4757 invalid_bdaddr = false;
4761 /* The transport driver can set these quirks before
4762 * creating the HCI device or in its setup callback.
4764 * For the invalid BD_ADDR quirk it is possible that
4765 * it becomes a valid address if the bootloader does
4766 * provide it (see above).
4768 * In case any of them is set, the controller has to
4769 * start up as unconfigured.
4771 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4773 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4775 /* For an unconfigured controller it is required to
4776 * read at least the version information provided by
4777 * the Read Local Version Information command.
4779 * If the set_bdaddr driver callback is provided, then
4780 * also the original Bluetooth public device address
4781 * will be read using the Read BD Address command.
4783 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4784 return hci_unconf_init_sync(hdev);
4789 /* This function handles hdev init stage:
4791 * Calls hci_dev_setup_sync to perform setup stage
4792 * Calls hci_init_sync to perform HCI command init sequence
4794 static int hci_dev_init_sync(struct hci_dev *hdev)
4798 bt_dev_dbg(hdev, "");
4800 atomic_set(&hdev->cmd_cnt, 1);
4801 set_bit(HCI_INIT, &hdev->flags);
4803 ret = hci_dev_setup_sync(hdev);
4805 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4806 /* If public address change is configured, ensure that
4807 * the address gets programmed. If the driver does not
4808 * support changing the public address, fail the power
4811 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4813 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4815 ret = -EADDRNOTAVAIL;
4819 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4820 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4821 ret = hci_init_sync(hdev);
4822 if (!ret && hdev->post_init)
4823 ret = hdev->post_init(hdev);
4827 /* If the HCI Reset command is clearing all diagnostic settings,
4828 * then they need to be reprogrammed after the init procedure
4831 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4832 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4833 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4834 ret = hdev->set_diag(hdev, true);
4836 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4841 clear_bit(HCI_INIT, &hdev->flags);
4846 int hci_dev_open_sync(struct hci_dev *hdev)
4850 bt_dev_dbg(hdev, "");
4852 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4857 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4858 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4859 /* Check for rfkill but allow the HCI setup stage to
4860 * proceed (which in itself doesn't cause any RF activity).
4862 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4867 /* Check for valid public address or a configured static
4868 * random address, but let the HCI setup proceed to
4869 * be able to determine if there is a public address
4872 * In case of user channel usage, it is not important
4873 * if a public address or static random address is
4876 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4877 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4878 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4879 ret = -EADDRNOTAVAIL;
4884 if (test_bit(HCI_UP, &hdev->flags)) {
4889 if (hdev->open(hdev)) {
4894 hci_devcd_reset(hdev);
4896 set_bit(HCI_RUNNING, &hdev->flags);
4897 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4899 ret = hci_dev_init_sync(hdev);
4902 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4903 hci_adv_instances_set_rpa_expired(hdev, true);
4904 set_bit(HCI_UP, &hdev->flags);
4905 hci_sock_dev_event(hdev, HCI_DEV_UP);
4906 hci_leds_update_powered(hdev, true);
4907 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4908 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4909 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4910 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4911 hci_dev_test_flag(hdev, HCI_MGMT)) {
4912 ret = hci_powered_update_sync(hdev);
4913 mgmt_power_on(hdev, ret);
4916 /* Init failed, cleanup */
4917 flush_work(&hdev->tx_work);
4919 /* Since hci_rx_work() is possible to awake new cmd_work
4920 * it should be flushed first to avoid unexpected call of
4923 flush_work(&hdev->rx_work);
4924 flush_work(&hdev->cmd_work);
4926 skb_queue_purge(&hdev->cmd_q);
4927 skb_queue_purge(&hdev->rx_q);
4932 if (hdev->sent_cmd) {
4933 cancel_delayed_work_sync(&hdev->cmd_timer);
4934 kfree_skb(hdev->sent_cmd);
4935 hdev->sent_cmd = NULL;
4938 if (hdev->req_skb) {
4939 kfree_skb(hdev->req_skb);
4940 hdev->req_skb = NULL;
4943 clear_bit(HCI_RUNNING, &hdev->flags);
4944 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4947 hdev->flags &= BIT(HCI_RAW);
4954 /* This function requires the caller holds hdev->lock */
4955 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4957 struct hci_conn_params *p;
4959 list_for_each_entry(p, &hdev->le_conn_params, list) {
4960 hci_pend_le_list_del_init(p);
4962 hci_conn_drop(p->conn);
4963 hci_conn_put(p->conn);
4968 BT_DBG("All LE pending actions cleared");
4971 static int hci_dev_shutdown(struct hci_dev *hdev)
4974 /* Similar to how we first do setup and then set the exclusive access
4975 * bit for userspace, we must first unset userchannel and then clean up.
4976 * Otherwise, the kernel can't properly use the hci channel to clean up
4977 * the controller (some shutdown routines require sending additional
4978 * commands to the controller for example).
4980 bool was_userchannel =
4981 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
4983 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4984 test_bit(HCI_UP, &hdev->flags)) {
4985 /* Execute vendor specific shutdown routine */
4987 err = hdev->shutdown(hdev);
4990 if (was_userchannel)
4991 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
4996 int hci_dev_close_sync(struct hci_dev *hdev)
5001 bt_dev_dbg(hdev, "");
5003 cancel_delayed_work(&hdev->power_off);
5004 cancel_delayed_work(&hdev->ncmd_timer);
5005 cancel_delayed_work(&hdev->le_scan_disable);
5007 hci_request_cancel_all(hdev);
5009 if (hdev->adv_instance_timeout) {
5010 cancel_delayed_work_sync(&hdev->adv_instance_expire);
5011 hdev->adv_instance_timeout = 0;
5014 err = hci_dev_shutdown(hdev);
5016 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
5017 cancel_delayed_work_sync(&hdev->cmd_timer);
5021 hci_leds_update_powered(hdev, false);
5023 /* Flush RX and TX works */
5024 flush_work(&hdev->tx_work);
5025 flush_work(&hdev->rx_work);
5027 if (hdev->discov_timeout > 0) {
5028 hdev->discov_timeout = 0;
5029 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5030 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
5033 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
5034 cancel_delayed_work(&hdev->service_cache);
5036 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
5037 struct adv_info *adv_instance;
5039 cancel_delayed_work_sync(&hdev->rpa_expired);
5041 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5042 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
5045 /* Avoid potential lockdep warnings from the *_flush() calls by
5046 * ensuring the workqueue is empty up front.
5048 drain_workqueue(hdev->workqueue);
5052 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5054 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5056 if (!auto_off && !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5057 hci_dev_test_flag(hdev, HCI_MGMT))
5058 __mgmt_power_off(hdev);
5060 hci_inquiry_cache_flush(hdev);
5061 hci_pend_le_actions_clear(hdev);
5062 hci_conn_hash_flush(hdev);
5063 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5064 smp_unregister(hdev);
5065 hci_dev_unlock(hdev);
5067 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5069 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5070 aosp_do_close(hdev);
5071 msft_do_close(hdev);
5078 skb_queue_purge(&hdev->cmd_q);
5079 atomic_set(&hdev->cmd_cnt, 1);
5080 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5081 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5082 set_bit(HCI_INIT, &hdev->flags);
5083 hci_reset_sync(hdev);
5084 clear_bit(HCI_INIT, &hdev->flags);
5087 /* flush cmd work */
5088 flush_work(&hdev->cmd_work);
5091 skb_queue_purge(&hdev->rx_q);
5092 skb_queue_purge(&hdev->cmd_q);
5093 skb_queue_purge(&hdev->raw_q);
5095 /* Drop last sent command */
5096 if (hdev->sent_cmd) {
5097 cancel_delayed_work_sync(&hdev->cmd_timer);
5098 kfree_skb(hdev->sent_cmd);
5099 hdev->sent_cmd = NULL;
5102 /* Drop last request */
5103 if (hdev->req_skb) {
5104 kfree_skb(hdev->req_skb);
5105 hdev->req_skb = NULL;
5108 clear_bit(HCI_RUNNING, &hdev->flags);
5109 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5111 /* After this point our queues are empty and no tasks are scheduled. */
5115 hdev->flags &= BIT(HCI_RAW);
5116 hci_dev_clear_volatile_flags(hdev);
5118 memset(hdev->eir, 0, sizeof(hdev->eir));
5119 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5120 bacpy(&hdev->random_addr, BDADDR_ANY);
5121 hci_codec_list_clear(&hdev->local_codecs);
5127 /* This function perform power on HCI command sequence as follows:
5129 * If controller is already up (HCI_UP) performs hci_powered_update_sync
5130 * sequence otherwise run hci_dev_open_sync which will follow with
5131 * hci_powered_update_sync after the init sequence is completed.
5133 static int hci_power_on_sync(struct hci_dev *hdev)
5137 if (test_bit(HCI_UP, &hdev->flags) &&
5138 hci_dev_test_flag(hdev, HCI_MGMT) &&
5139 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5140 cancel_delayed_work(&hdev->power_off);
5141 return hci_powered_update_sync(hdev);
5144 err = hci_dev_open_sync(hdev);
5148 /* During the HCI setup phase, a few error conditions are
5149 * ignored and they need to be checked now. If they are still
5150 * valid, it is important to return the device back off.
5152 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5153 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5154 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5155 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5156 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5157 hci_dev_close_sync(hdev);
5158 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5159 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5160 HCI_AUTO_OFF_TIMEOUT);
5163 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5164 /* For unconfigured devices, set the HCI_RAW flag
5165 * so that userspace can easily identify them.
5167 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5168 set_bit(HCI_RAW, &hdev->flags);
5170 /* For fully configured devices, this will send
5171 * the Index Added event. For unconfigured devices,
5172 * it will send Unconfigued Index Added event.
5174 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
5175 * and no event will be send.
5177 mgmt_index_added(hdev);
5178 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5179 /* When the controller is now configured, then it
5180 * is important to clear the HCI_RAW flag.
5182 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5183 clear_bit(HCI_RAW, &hdev->flags);
5185 /* Powering on the controller with HCI_CONFIG set only
5186 * happens with the transition from unconfigured to
5187 * configured. This will send the Index Added event.
5189 mgmt_index_added(hdev);
5195 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5197 struct hci_cp_remote_name_req_cancel cp;
5199 memset(&cp, 0, sizeof(cp));
5200 bacpy(&cp.bdaddr, addr);
5202 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5203 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5206 int hci_stop_discovery_sync(struct hci_dev *hdev)
5208 struct discovery_state *d = &hdev->discovery;
5209 struct inquiry_entry *e;
5212 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5214 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
5215 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5216 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5217 0, NULL, HCI_CMD_TIMEOUT);
5222 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5223 cancel_delayed_work(&hdev->le_scan_disable);
5225 err = hci_scan_disable_sync(hdev);
5231 err = hci_scan_disable_sync(hdev);
5236 /* Resume advertising if it was paused */
5237 if (use_ll_privacy(hdev))
5238 hci_resume_advertising_sync(hdev);
5240 /* No further actions needed for LE-only discovery */
5241 if (d->type == DISCOV_TYPE_LE)
5244 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5245 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5250 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5256 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5259 struct hci_cp_disconnect cp;
5261 if (test_bit(HCI_CONN_BIG_CREATED, &conn->flags)) {
5262 /* This is a BIS connection, hci_conn_del will
5263 * do the necessary cleanup.
5266 hci_conn_failed(conn, reason);
5267 hci_dev_unlock(hdev);
5272 memset(&cp, 0, sizeof(cp));
5273 cp.handle = cpu_to_le16(conn->handle);
5276 /* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5277 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5278 * used when suspending or powering off, where we don't want to wait
5279 * for the peer's response.
5281 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5282 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5284 HCI_EV_DISCONN_COMPLETE,
5285 HCI_CMD_TIMEOUT, NULL);
5287 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5291 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5292 struct hci_conn *conn, u8 reason)
5294 /* Return reason if scanning since the connection shall probably be
5297 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5300 if (conn->role == HCI_ROLE_SLAVE ||
5301 test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
5304 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5305 0, NULL, HCI_CMD_TIMEOUT);
5308 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5311 if (conn->type == LE_LINK)
5312 return hci_le_connect_cancel_sync(hdev, conn, reason);
5314 if (conn->type == ISO_LINK) {
5315 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
5318 * If this command is issued for a CIS on the Central and the
5319 * CIS is successfully terminated before being established,
5320 * then an HCI_LE_CIS_Established event shall also be sent for
5321 * this CIS with the Status Operation Cancelled by Host (0x44).
5323 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
5324 return hci_disconnect_sync(hdev, conn, reason);
5326 /* CIS with no Create CIS sent have nothing to cancel */
5327 if (bacmp(&conn->dst, BDADDR_ANY))
5328 return HCI_ERROR_LOCAL_HOST_TERM;
5330 /* There is no way to cancel a BIS without terminating the BIG
5331 * which is done later on connection cleanup.
5336 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5339 /* Wait for HCI_EV_CONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5340 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5341 * used when suspending or powering off, where we don't want to wait
5342 * for the peer's response.
5344 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5345 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5347 HCI_EV_CONN_COMPLETE,
5348 HCI_CMD_TIMEOUT, NULL);
5350 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5351 6, &conn->dst, HCI_CMD_TIMEOUT);
5354 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5357 struct hci_cp_reject_sync_conn_req cp;
5359 memset(&cp, 0, sizeof(cp));
5360 bacpy(&cp.bdaddr, &conn->dst);
5363 /* SCO rejection has its own limited set of
5364 * allowed error values (0x0D-0x0F).
5366 if (reason < 0x0d || reason > 0x0f)
5367 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5369 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5370 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5373 static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
5376 struct hci_cp_le_reject_cis cp;
5378 memset(&cp, 0, sizeof(cp));
5379 cp.handle = cpu_to_le16(conn->handle);
5382 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
5383 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5386 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5389 struct hci_cp_reject_conn_req cp;
5391 if (conn->type == ISO_LINK)
5392 return hci_le_reject_cis_sync(hdev, conn, reason);
5394 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5395 return hci_reject_sco_sync(hdev, conn, reason);
5397 memset(&cp, 0, sizeof(cp));
5398 bacpy(&cp.bdaddr, &conn->dst);
5401 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5402 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5405 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5408 u16 handle = conn->handle;
5409 bool disconnect = false;
5412 switch (conn->state) {
5415 err = hci_disconnect_sync(hdev, conn, reason);
5418 err = hci_connect_cancel_sync(hdev, conn, reason);
5421 err = hci_reject_conn_sync(hdev, conn, reason);
5433 /* Check if the connection has been cleaned up concurrently */
5434 c = hci_conn_hash_lookup_handle(hdev, handle);
5435 if (!c || c != conn) {
5440 /* Cleanup hci_conn object if it cannot be cancelled as it
5441 * likelly means the controller and host stack are out of sync
5442 * or in case of LE it was still scanning so it can be cleanup
5446 conn->state = BT_CLOSED;
5447 hci_disconn_cfm(conn, reason);
5450 hci_conn_failed(conn, reason);
5454 hci_dev_unlock(hdev);
5458 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5460 struct list_head *head = &hdev->conn_hash.list;
5461 struct hci_conn *conn;
5464 while ((conn = list_first_or_null_rcu(head, struct hci_conn, list))) {
5465 /* Make sure the connection is not freed while unlocking */
5466 conn = hci_conn_get(conn);
5468 /* Disregard possible errors since hci_conn_del shall have been
5469 * called even in case of errors had occurred since it would
5470 * then cause hci_conn_failed to be called which calls
5471 * hci_conn_del internally.
5473 hci_abort_conn_sync(hdev, conn, reason);
5482 /* This function perform power off HCI command sequence as follows:
5486 * Disconnect all connections
5487 * hci_dev_close_sync
5489 static int hci_power_off_sync(struct hci_dev *hdev)
5493 /* If controller is already down there is nothing to do */
5494 if (!test_bit(HCI_UP, &hdev->flags))
5497 hci_dev_set_flag(hdev, HCI_POWERING_DOWN);
5499 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5500 test_bit(HCI_PSCAN, &hdev->flags)) {
5501 err = hci_write_scan_enable_sync(hdev, 0x00);
5506 err = hci_clear_adv_sync(hdev, NULL, false);
5510 err = hci_stop_discovery_sync(hdev);
5514 /* Terminated due to Power Off */
5515 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5519 err = hci_dev_close_sync(hdev);
5522 hci_dev_clear_flag(hdev, HCI_POWERING_DOWN);
5526 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5529 return hci_power_on_sync(hdev);
5531 return hci_power_off_sync(hdev);
5534 static int hci_write_iac_sync(struct hci_dev *hdev)
5536 struct hci_cp_write_current_iac_lap cp;
5538 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5541 memset(&cp, 0, sizeof(cp));
5543 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5544 /* Limited discoverable mode */
5545 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5546 cp.iac_lap[0] = 0x00; /* LIAC */
5547 cp.iac_lap[1] = 0x8b;
5548 cp.iac_lap[2] = 0x9e;
5549 cp.iac_lap[3] = 0x33; /* GIAC */
5550 cp.iac_lap[4] = 0x8b;
5551 cp.iac_lap[5] = 0x9e;
5553 /* General discoverable mode */
5555 cp.iac_lap[0] = 0x33; /* GIAC */
5556 cp.iac_lap[1] = 0x8b;
5557 cp.iac_lap[2] = 0x9e;
5560 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5561 (cp.num_iac * 3) + 1, &cp,
5565 int hci_update_discoverable_sync(struct hci_dev *hdev)
5569 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5570 err = hci_write_iac_sync(hdev);
5574 err = hci_update_scan_sync(hdev);
5578 err = hci_update_class_sync(hdev);
5583 /* Advertising instances don't use the global discoverable setting, so
5584 * only update AD if advertising was enabled using Set Advertising.
5586 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5587 err = hci_update_adv_data_sync(hdev, 0x00);
5591 /* Discoverable mode affects the local advertising
5592 * address in limited privacy mode.
5594 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5595 if (ext_adv_capable(hdev))
5596 err = hci_start_ext_adv_sync(hdev, 0x00);
5598 err = hci_enable_advertising_sync(hdev);
5605 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5607 return hci_update_discoverable_sync(hdev);
5610 int hci_update_discoverable(struct hci_dev *hdev)
5612 /* Only queue if it would have any effect */
5613 if (hdev_is_powered(hdev) &&
5614 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5615 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5616 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5617 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5623 int hci_update_connectable_sync(struct hci_dev *hdev)
5627 err = hci_update_scan_sync(hdev);
5631 /* If BR/EDR is not enabled and we disable advertising as a
5632 * by-product of disabling connectable, we need to update the
5633 * advertising flags.
5635 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5636 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5638 /* Update the advertising parameters if necessary */
5639 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5640 !list_empty(&hdev->adv_instances)) {
5641 if (ext_adv_capable(hdev))
5642 err = hci_start_ext_adv_sync(hdev,
5643 hdev->cur_adv_instance);
5645 err = hci_enable_advertising_sync(hdev);
5651 return hci_update_passive_scan_sync(hdev);
5654 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5656 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5657 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5658 struct hci_cp_inquiry cp;
5660 bt_dev_dbg(hdev, "");
5662 if (test_bit(HCI_INQUIRY, &hdev->flags))
5666 hci_inquiry_cache_flush(hdev);
5667 hci_dev_unlock(hdev);
5669 memset(&cp, 0, sizeof(cp));
5671 if (hdev->discovery.limited)
5672 memcpy(&cp.lap, liac, sizeof(cp.lap));
5674 memcpy(&cp.lap, giac, sizeof(cp.lap));
5678 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5679 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5682 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5685 /* Accept list is not used for discovery */
5686 u8 filter_policy = 0x00;
5687 /* Default is to enable duplicates filter */
5688 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5691 bt_dev_dbg(hdev, "");
5693 /* If controller is scanning, it means the passive scanning is
5694 * running. Thus, we should temporarily stop it in order to set the
5695 * discovery scanning parameters.
5697 err = hci_scan_disable_sync(hdev);
5699 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5703 cancel_interleave_scan(hdev);
5705 /* Pause address resolution for active scan and stop advertising if
5706 * privacy is enabled.
5708 err = hci_pause_addr_resolution(hdev);
5712 /* All active scans will be done with either a resolvable private
5713 * address (when privacy feature has been enabled) or non-resolvable
5716 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5719 own_addr_type = ADDR_LE_DEV_PUBLIC;
5721 if (hci_is_adv_monitoring(hdev) ||
5722 (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5723 hdev->discovery.result_filtering)) {
5724 /* Duplicate filter should be disabled when some advertisement
5725 * monitor is activated, otherwise AdvMon can only receive one
5726 * advertisement for one peer(*) during active scanning, and
5727 * might report loss to these peers.
5729 * If controller does strict duplicate filtering and the
5730 * discovery requires result filtering disables controller based
5731 * filtering since that can cause reports that would match the
5732 * host filter to not be reported.
5734 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5737 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5738 hdev->le_scan_window_discovery,
5739 own_addr_type, filter_policy, filter_dup);
5744 /* Resume advertising if it was paused */
5745 if (use_ll_privacy(hdev))
5746 hci_resume_advertising_sync(hdev);
5748 /* Resume passive scanning */
5749 hci_update_passive_scan_sync(hdev);
5753 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5757 bt_dev_dbg(hdev, "");
5759 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5763 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5766 int hci_start_discovery_sync(struct hci_dev *hdev)
5768 unsigned long timeout;
5771 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5773 switch (hdev->discovery.type) {
5774 case DISCOV_TYPE_BREDR:
5775 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5776 case DISCOV_TYPE_INTERLEAVED:
5777 /* When running simultaneous discovery, the LE scanning time
5778 * should occupy the whole discovery time sine BR/EDR inquiry
5779 * and LE scanning are scheduled by the controller.
5781 * For interleaving discovery in comparison, BR/EDR inquiry
5782 * and LE scanning are done sequentially with separate
5785 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5787 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5788 /* During simultaneous discovery, we double LE scan
5789 * interval. We must leave some time for the controller
5790 * to do BR/EDR inquiry.
5792 err = hci_start_interleaved_discovery_sync(hdev);
5796 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5797 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5799 case DISCOV_TYPE_LE:
5800 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5801 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5810 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5812 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5817 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5819 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5820 case HCI_ADV_MONITOR_EXT_MSFT:
5821 msft_suspend_sync(hdev);
5828 /* This function disables discovery and mark it as paused */
5829 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5831 int old_state = hdev->discovery.state;
5834 /* If discovery already stopped/stopping/paused there nothing to do */
5835 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
5836 hdev->discovery_paused)
5839 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5840 err = hci_stop_discovery_sync(hdev);
5844 hdev->discovery_paused = true;
5845 hdev->discovery_old_state = old_state;
5846 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5851 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5853 struct bdaddr_list_with_flags *b;
5854 u8 scan = SCAN_DISABLED;
5855 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5858 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5861 /* Some fake CSR controllers lock up after setting this type of
5862 * filter, so avoid sending the request altogether.
5864 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5867 /* Always clear event filter when starting */
5868 hci_clear_event_filter_sync(hdev);
5870 list_for_each_entry(b, &hdev->accept_list, list) {
5871 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
5874 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5876 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5877 HCI_CONN_SETUP_ALLOW_BDADDR,
5879 HCI_CONN_SETUP_AUTO_ON);
5881 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5887 if (scan && !scanning)
5888 hci_write_scan_enable_sync(hdev, scan);
5889 else if (!scan && scanning)
5890 hci_write_scan_enable_sync(hdev, scan);
5895 /* This function disables scan (BR and LE) and mark it as paused */
5896 static int hci_pause_scan_sync(struct hci_dev *hdev)
5898 if (hdev->scanning_paused)
5901 /* Disable page scan if enabled */
5902 if (test_bit(HCI_PSCAN, &hdev->flags))
5903 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
5905 hci_scan_disable_sync(hdev);
5907 hdev->scanning_paused = true;
5912 /* This function performs the HCI suspend procedures in the follow order:
5914 * Pause discovery (active scanning/inquiry)
5915 * Pause Directed Advertising/Advertising
5916 * Pause Scanning (passive scanning in case discovery was not active)
5917 * Disconnect all connections
5918 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5920 * Update event mask (only set events that are allowed to wake up the host)
5921 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
5922 * Update passive scanning (lower duty cycle)
5923 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
5925 int hci_suspend_sync(struct hci_dev *hdev)
5929 /* If marked as suspended there nothing to do */
5930 if (hdev->suspended)
5933 /* Mark device as suspended */
5934 hdev->suspended = true;
5936 /* Pause discovery if not already stopped */
5937 hci_pause_discovery_sync(hdev);
5939 /* Pause other advertisements */
5940 hci_pause_advertising_sync(hdev);
5942 /* Suspend monitor filters */
5943 hci_suspend_monitor_sync(hdev);
5945 /* Prevent disconnects from causing scanning to be re-enabled */
5946 hci_pause_scan_sync(hdev);
5948 if (hci_conn_count(hdev)) {
5949 /* Soft disconnect everything (power off) */
5950 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5952 /* Set state to BT_RUNNING so resume doesn't notify */
5953 hdev->suspend_state = BT_RUNNING;
5954 hci_resume_sync(hdev);
5958 /* Update event mask so only the allowed event can wakeup the
5961 hci_set_event_mask_sync(hdev);
5964 /* Only configure accept list if disconnect succeeded and wake
5965 * isn't being prevented.
5967 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5968 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5972 /* Unpause to take care of updating scanning params */
5973 hdev->scanning_paused = false;
5975 /* Enable event filter for paired devices */
5976 hci_update_event_filter_sync(hdev);
5978 /* Update LE passive scan if enabled */
5979 hci_update_passive_scan_sync(hdev);
5981 /* Pause scan changes again. */
5982 hdev->scanning_paused = true;
5984 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5989 /* This function resumes discovery */
5990 static int hci_resume_discovery_sync(struct hci_dev *hdev)
5994 /* If discovery not paused there nothing to do */
5995 if (!hdev->discovery_paused)
5998 hdev->discovery_paused = false;
6000 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6002 err = hci_start_discovery_sync(hdev);
6004 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
6010 static void hci_resume_monitor_sync(struct hci_dev *hdev)
6012 switch (hci_get_adv_monitor_offload_ext(hdev)) {
6013 case HCI_ADV_MONITOR_EXT_MSFT:
6014 msft_resume_sync(hdev);
6021 /* This function resume scan and reset paused flag */
6022 static int hci_resume_scan_sync(struct hci_dev *hdev)
6024 if (!hdev->scanning_paused)
6027 hdev->scanning_paused = false;
6029 hci_update_scan_sync(hdev);
6031 /* Reset passive scanning to normal */
6032 hci_update_passive_scan_sync(hdev);
6037 /* This function performs the HCI suspend procedures in the follow order:
6039 * Restore event mask
6040 * Clear event filter
6041 * Update passive scanning (normal duty cycle)
6042 * Resume Directed Advertising/Advertising
6043 * Resume discovery (active scanning/inquiry)
6045 int hci_resume_sync(struct hci_dev *hdev)
6047 /* If not marked as suspended there nothing to do */
6048 if (!hdev->suspended)
6051 hdev->suspended = false;
6053 /* Restore event mask */
6054 hci_set_event_mask_sync(hdev);
6056 /* Clear any event filters and restore scan state */
6057 hci_clear_event_filter_sync(hdev);
6059 /* Resume scanning */
6060 hci_resume_scan_sync(hdev);
6062 /* Resume monitor filters */
6063 hci_resume_monitor_sync(hdev);
6065 /* Resume other advertisements */
6066 hci_resume_advertising_sync(hdev);
6068 /* Resume discovery */
6069 hci_resume_discovery_sync(hdev);
6074 static bool conn_use_rpa(struct hci_conn *conn)
6076 struct hci_dev *hdev = conn->hdev;
6078 return hci_dev_test_flag(hdev, HCI_PRIVACY);
6081 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
6082 struct hci_conn *conn)
6084 struct hci_cp_le_set_ext_adv_params cp;
6086 bdaddr_t random_addr;
6089 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6094 /* Set require_privacy to false so that the remote device has a
6095 * chance of identifying us.
6097 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
6098 &own_addr_type, &random_addr);
6102 memset(&cp, 0, sizeof(cp));
6104 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
6105 cp.channel_map = hdev->le_adv_channel_map;
6106 cp.tx_power = HCI_TX_POWER_INVALID;
6107 cp.primary_phy = HCI_ADV_PHY_1M;
6108 cp.secondary_phy = HCI_ADV_PHY_1M;
6109 cp.handle = 0x00; /* Use instance 0 for directed adv */
6110 cp.own_addr_type = own_addr_type;
6111 cp.peer_addr_type = conn->dst_type;
6112 bacpy(&cp.peer_addr, &conn->dst);
6114 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
6115 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
6116 * does not supports advertising data when the advertising set already
6117 * contains some, the controller shall return erroc code 'Invalid
6118 * HCI Command Parameters(0x12).
6119 * So it is required to remove adv set for handle 0x00. since we use
6120 * instance 0 for directed adv.
6122 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
6126 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
6127 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6131 /* Check if random address need to be updated */
6132 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
6133 bacmp(&random_addr, BDADDR_ANY) &&
6134 bacmp(&random_addr, &hdev->random_addr)) {
6135 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
6141 return hci_enable_ext_advertising_sync(hdev, 0x00);
6144 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
6145 struct hci_conn *conn)
6147 struct hci_cp_le_set_adv_param cp;
6152 if (ext_adv_capable(hdev))
6153 return hci_le_ext_directed_advertising_sync(hdev, conn);
6155 /* Clear the HCI_LE_ADV bit temporarily so that the
6156 * hci_update_random_address knows that it's safe to go ahead
6157 * and write a new random address. The flag will be set back on
6158 * as soon as the SET_ADV_ENABLE HCI command completes.
6160 hci_dev_clear_flag(hdev, HCI_LE_ADV);
6162 /* Set require_privacy to false so that the remote device has a
6163 * chance of identifying us.
6165 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6170 memset(&cp, 0, sizeof(cp));
6172 /* Some controllers might reject command if intervals are not
6173 * within range for undirected advertising.
6174 * BCM20702A0 is known to be affected by this.
6176 cp.min_interval = cpu_to_le16(0x0020);
6177 cp.max_interval = cpu_to_le16(0x0020);
6179 cp.type = LE_ADV_DIRECT_IND;
6180 cp.own_address_type = own_addr_type;
6181 cp.direct_addr_type = conn->dst_type;
6182 bacpy(&cp.direct_addr, &conn->dst);
6183 cp.channel_map = hdev->le_adv_channel_map;
6185 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
6186 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6192 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
6193 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
6196 static void set_ext_conn_params(struct hci_conn *conn,
6197 struct hci_cp_le_ext_conn_param *p)
6199 struct hci_dev *hdev = conn->hdev;
6201 memset(p, 0, sizeof(*p));
6203 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6204 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6205 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6206 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6207 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
6208 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6209 p->min_ce_len = cpu_to_le16(0x0000);
6210 p->max_ce_len = cpu_to_le16(0x0000);
6213 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
6214 struct hci_conn *conn, u8 own_addr_type)
6216 struct hci_cp_le_ext_create_conn *cp;
6217 struct hci_cp_le_ext_conn_param *p;
6218 u8 data[sizeof(*cp) + sizeof(*p) * 3];
6222 p = (void *)cp->data;
6224 memset(cp, 0, sizeof(*cp));
6226 bacpy(&cp->peer_addr, &conn->dst);
6227 cp->peer_addr_type = conn->dst_type;
6228 cp->own_addr_type = own_addr_type;
6232 if (scan_1m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_1M ||
6233 conn->le_adv_sec_phy == HCI_ADV_PHY_1M)) {
6234 cp->phys |= LE_SCAN_PHY_1M;
6235 set_ext_conn_params(conn, p);
6241 if (scan_2m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_2M ||
6242 conn->le_adv_sec_phy == HCI_ADV_PHY_2M)) {
6243 cp->phys |= LE_SCAN_PHY_2M;
6244 set_ext_conn_params(conn, p);
6250 if (scan_coded(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_CODED ||
6251 conn->le_adv_sec_phy == HCI_ADV_PHY_CODED)) {
6252 cp->phys |= LE_SCAN_PHY_CODED;
6253 set_ext_conn_params(conn, p);
6258 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6260 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
6261 conn->conn_timeout, NULL);
6264 static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
6266 struct hci_cp_le_create_conn cp;
6267 struct hci_conn_params *params;
6270 struct hci_conn *conn = data;
6272 if (!hci_conn_valid(hdev, conn))
6275 bt_dev_dbg(hdev, "conn %p", conn);
6277 clear_bit(HCI_CONN_SCANNING, &conn->flags);
6278 conn->state = BT_CONNECT;
6280 /* If requested to connect as peripheral use directed advertising */
6281 if (conn->role == HCI_ROLE_SLAVE) {
6282 /* If we're active scanning and simultaneous roles is not
6283 * enabled simply reject the attempt.
6285 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6286 hdev->le_scan_type == LE_SCAN_ACTIVE &&
6287 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6292 /* Pause advertising while doing directed advertising. */
6293 hci_pause_advertising_sync(hdev);
6295 err = hci_le_directed_advertising_sync(hdev, conn);
6299 /* Disable advertising if simultaneous roles is not in use. */
6300 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6301 hci_pause_advertising_sync(hdev);
6303 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6305 conn->le_conn_min_interval = params->conn_min_interval;
6306 conn->le_conn_max_interval = params->conn_max_interval;
6307 conn->le_conn_latency = params->conn_latency;
6308 conn->le_supv_timeout = params->supervision_timeout;
6310 conn->le_conn_min_interval = hdev->le_conn_min_interval;
6311 conn->le_conn_max_interval = hdev->le_conn_max_interval;
6312 conn->le_conn_latency = hdev->le_conn_latency;
6313 conn->le_supv_timeout = hdev->le_supv_timeout;
6316 /* If controller is scanning, we stop it since some controllers are
6317 * not able to scan and connect at the same time. Also set the
6318 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
6319 * handler for scan disabling knows to set the correct discovery
6322 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6323 hci_scan_disable_sync(hdev);
6324 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6327 /* Update random address, but set require_privacy to false so
6328 * that we never connect with an non-resolvable address.
6330 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6335 if (use_ext_conn(hdev)) {
6336 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6340 memset(&cp, 0, sizeof(cp));
6342 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6343 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6345 bacpy(&cp.peer_addr, &conn->dst);
6346 cp.peer_addr_type = conn->dst_type;
6347 cp.own_address_type = own_addr_type;
6348 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6349 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6350 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
6351 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6352 cp.min_ce_len = cpu_to_le16(0x0000);
6353 cp.max_ce_len = cpu_to_le16(0x0000);
6355 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6357 * If this event is unmasked and the HCI_LE_Connection_Complete event
6358 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6359 * sent when a new connection has been created.
6361 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6363 use_enhanced_conn_complete(hdev) ?
6364 HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6365 HCI_EV_LE_CONN_COMPLETE,
6366 conn->conn_timeout, NULL);
6369 if (err == -ETIMEDOUT)
6370 hci_le_connect_cancel_sync(hdev, conn, 0x00);
6372 /* Re-enable advertising after the connection attempt is finished. */
6373 hci_resume_advertising_sync(hdev);
6377 int hci_le_create_cis_sync(struct hci_dev *hdev)
6379 DEFINE_FLEX(struct hci_cp_le_create_cis, cmd, cis, num_cis, 0x1f);
6380 size_t aux_num_cis = 0;
6381 struct hci_conn *conn;
6382 u8 cig = BT_ISO_QOS_CIG_UNSET;
6384 /* The spec allows only one pending LE Create CIS command at a time. If
6385 * the command is pending now, don't do anything. We check for pending
6386 * connections after each CIS Established event.
6388 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6391 * If the Host issues this command before all the
6392 * HCI_LE_CIS_Established events from the previous use of the
6393 * command have been generated, the Controller shall return the
6394 * error code Command Disallowed (0x0C).
6396 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6399 * When the Controller receives the HCI_LE_Create_CIS command, the
6400 * Controller sends the HCI_Command_Status event to the Host. An
6401 * HCI_LE_CIS_Established event will be generated for each CIS when it
6402 * is established or if it is disconnected or considered lost before
6403 * being established; until all the events are generated, the command
6411 /* Wait until previous Create CIS has completed */
6412 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6413 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
6417 /* Find CIG with all CIS ready */
6418 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6419 struct hci_conn *link;
6421 if (hci_conn_check_create_cis(conn))
6424 cig = conn->iso_qos.ucast.cig;
6426 list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
6427 if (hci_conn_check_create_cis(link) > 0 &&
6428 link->iso_qos.ucast.cig == cig &&
6429 link->state != BT_CONNECTED) {
6430 cig = BT_ISO_QOS_CIG_UNSET;
6435 if (cig != BT_ISO_QOS_CIG_UNSET)
6439 if (cig == BT_ISO_QOS_CIG_UNSET)
6442 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6443 struct hci_cis *cis = &cmd->cis[aux_num_cis];
6445 if (hci_conn_check_create_cis(conn) ||
6446 conn->iso_qos.ucast.cig != cig)
6449 set_bit(HCI_CONN_CREATE_CIS, &conn->flags);
6450 cis->acl_handle = cpu_to_le16(conn->parent->handle);
6451 cis->cis_handle = cpu_to_le16(conn->handle);
6454 if (aux_num_cis >= cmd->num_cis)
6457 cmd->num_cis = aux_num_cis;
6462 hci_dev_unlock(hdev);
6467 /* Wait for HCI_LE_CIS_Established */
6468 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6469 struct_size(cmd, cis, cmd->num_cis),
6470 cmd, HCI_EVT_LE_CIS_ESTABLISHED,
6471 conn->conn_timeout, NULL);
6474 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6476 struct hci_cp_le_remove_cig cp;
6478 memset(&cp, 0, sizeof(cp));
6481 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6482 &cp, HCI_CMD_TIMEOUT);
6485 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6487 struct hci_cp_le_big_term_sync cp;
6489 memset(&cp, 0, sizeof(cp));
6492 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6493 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6496 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6498 struct hci_cp_le_pa_term_sync cp;
6500 memset(&cp, 0, sizeof(cp));
6501 cp.handle = cpu_to_le16(handle);
6503 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6504 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6507 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6508 bool use_rpa, struct adv_info *adv_instance,
6509 u8 *own_addr_type, bdaddr_t *rand_addr)
6513 bacpy(rand_addr, BDADDR_ANY);
6515 /* If privacy is enabled use a resolvable private address. If
6516 * current RPA has expired then generate a new one.
6519 /* If Controller supports LL Privacy use own address type is
6522 if (use_ll_privacy(hdev))
6523 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
6525 *own_addr_type = ADDR_LE_DEV_RANDOM;
6528 if (adv_rpa_valid(adv_instance))
6531 if (rpa_valid(hdev))
6535 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6537 bt_dev_err(hdev, "failed to generate new RPA");
6541 bacpy(rand_addr, &hdev->rpa);
6546 /* In case of required privacy without resolvable private address,
6547 * use an non-resolvable private address. This is useful for
6548 * non-connectable advertising.
6550 if (require_privacy) {
6554 /* The non-resolvable private address is generated
6555 * from random six bytes with the two most significant
6558 get_random_bytes(&nrpa, 6);
6561 /* The non-resolvable private address shall not be
6562 * equal to the public address.
6564 if (bacmp(&hdev->bdaddr, &nrpa))
6568 *own_addr_type = ADDR_LE_DEV_RANDOM;
6569 bacpy(rand_addr, &nrpa);
6574 /* No privacy so use a public address. */
6575 *own_addr_type = ADDR_LE_DEV_PUBLIC;
6580 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6582 u8 instance = PTR_UINT(data);
6584 return hci_update_adv_data_sync(hdev, instance);
6587 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6589 return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6590 UINT_PTR(instance), NULL);
6593 static int hci_acl_create_conn_sync(struct hci_dev *hdev, void *data)
6595 struct hci_conn *conn = data;
6596 struct inquiry_entry *ie;
6597 struct hci_cp_create_conn cp;
6600 if (!hci_conn_valid(hdev, conn))
6603 /* Many controllers disallow HCI Create Connection while it is doing
6604 * HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create
6605 * Connection. This may cause the MGMT discovering state to become false
6606 * without user space's request but it is okay since the MGMT Discovery
6607 * APIs do not promise that discovery should be done forever. Instead,
6608 * the user space monitors the status of MGMT discovering and it may
6609 * request for discovery again when this flag becomes false.
6611 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
6612 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 0,
6613 NULL, HCI_CMD_TIMEOUT);
6615 bt_dev_warn(hdev, "Failed to cancel inquiry %d", err);
6618 conn->state = BT_CONNECT;
6620 conn->role = HCI_ROLE_MASTER;
6624 conn->link_policy = hdev->link_policy;
6626 memset(&cp, 0, sizeof(cp));
6627 bacpy(&cp.bdaddr, &conn->dst);
6628 cp.pscan_rep_mode = 0x02;
6630 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
6632 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
6633 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
6634 cp.pscan_mode = ie->data.pscan_mode;
6635 cp.clock_offset = ie->data.clock_offset |
6636 cpu_to_le16(0x8000);
6639 memcpy(conn->dev_class, ie->data.dev_class, 3);
6642 cp.pkt_type = cpu_to_le16(conn->pkt_type);
6643 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
6644 cp.role_switch = 0x01;
6646 cp.role_switch = 0x00;
6648 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN,
6650 HCI_EV_CONN_COMPLETE,
6651 conn->conn_timeout, NULL);
6654 int hci_connect_acl_sync(struct hci_dev *hdev, struct hci_conn *conn)
6656 return hci_cmd_sync_queue_once(hdev, hci_acl_create_conn_sync, conn,
6660 static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err)
6662 struct hci_conn *conn = data;
6664 bt_dev_dbg(hdev, "err %d", err);
6666 if (err == -ECANCELED)
6671 if (!hci_conn_valid(hdev, conn))
6675 hci_connect_le_scan_cleanup(conn, 0x00);
6679 /* Check if connection is still pending */
6680 if (conn != hci_lookup_le_connect(hdev))
6683 /* Flush to make sure we send create conn cancel command if needed */
6684 flush_delayed_work(&conn->le_conn_timeout);
6685 hci_conn_failed(conn, bt_status(err));
6688 hci_dev_unlock(hdev);
6691 int hci_connect_le_sync(struct hci_dev *hdev, struct hci_conn *conn)
6693 return hci_cmd_sync_queue_once(hdev, hci_le_create_conn_sync, conn,
6694 create_le_conn_complete);
6697 int hci_cancel_connect_sync(struct hci_dev *hdev, struct hci_conn *conn)
6699 if (conn->state != BT_OPEN)
6702 switch (conn->type) {
6704 return !hci_cmd_sync_dequeue_once(hdev,
6705 hci_acl_create_conn_sync,
6708 return !hci_cmd_sync_dequeue_once(hdev, hci_le_create_conn_sync,
6709 conn, create_le_conn_complete);