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 hdev->req_result = err;
621 hdev->req_status = HCI_REQ_CANCELED;
623 wake_up_interruptible(&hdev->req_wait_q);
626 EXPORT_SYMBOL(hci_cmd_sync_cancel_sync);
628 /* Submit HCI command to be run in as cmd_sync_work:
630 * - hdev must _not_ be unregistered
632 int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
633 void *data, hci_cmd_sync_work_destroy_t destroy)
635 struct hci_cmd_sync_work_entry *entry;
638 mutex_lock(&hdev->unregister_lock);
639 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
644 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
651 entry->destroy = destroy;
653 mutex_lock(&hdev->cmd_sync_work_lock);
654 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
655 mutex_unlock(&hdev->cmd_sync_work_lock);
657 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
660 mutex_unlock(&hdev->unregister_lock);
663 EXPORT_SYMBOL(hci_cmd_sync_submit);
665 /* Queue HCI command:
667 * - hdev must be running
669 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
670 void *data, hci_cmd_sync_work_destroy_t destroy)
672 /* Only queue command if hdev is running which means it had been opened
673 * and is either on init phase or is already up.
675 if (!test_bit(HCI_RUNNING, &hdev->flags))
678 return hci_cmd_sync_submit(hdev, func, data, destroy);
680 EXPORT_SYMBOL(hci_cmd_sync_queue);
682 static struct hci_cmd_sync_work_entry *
683 _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
684 void *data, hci_cmd_sync_work_destroy_t destroy)
686 struct hci_cmd_sync_work_entry *entry, *tmp;
688 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
689 if (func && entry->func != func)
692 if (data && entry->data != data)
695 if (destroy && entry->destroy != destroy)
704 /* Queue HCI command entry once:
706 * - Lookup if an entry already exist and only if it doesn't creates a new entry
709 int hci_cmd_sync_queue_once(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
710 void *data, hci_cmd_sync_work_destroy_t destroy)
712 if (hci_cmd_sync_lookup_entry(hdev, func, data, destroy))
715 return hci_cmd_sync_queue(hdev, func, data, destroy);
717 EXPORT_SYMBOL(hci_cmd_sync_queue_once);
719 /* Lookup HCI command entry:
721 * - Return first entry that matches by function callback or data or
724 struct hci_cmd_sync_work_entry *
725 hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
726 void *data, hci_cmd_sync_work_destroy_t destroy)
728 struct hci_cmd_sync_work_entry *entry;
730 mutex_lock(&hdev->cmd_sync_work_lock);
731 entry = _hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
732 mutex_unlock(&hdev->cmd_sync_work_lock);
736 EXPORT_SYMBOL(hci_cmd_sync_lookup_entry);
738 /* Cancel HCI command entry */
739 void hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
740 struct hci_cmd_sync_work_entry *entry)
742 mutex_lock(&hdev->cmd_sync_work_lock);
743 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
744 mutex_unlock(&hdev->cmd_sync_work_lock);
746 EXPORT_SYMBOL(hci_cmd_sync_cancel_entry);
748 /* Dequeue one HCI command entry:
750 * - Lookup and cancel first entry that matches.
752 bool hci_cmd_sync_dequeue_once(struct hci_dev *hdev,
753 hci_cmd_sync_work_func_t func,
754 void *data, hci_cmd_sync_work_destroy_t destroy)
756 struct hci_cmd_sync_work_entry *entry;
758 entry = hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
762 hci_cmd_sync_cancel_entry(hdev, entry);
766 EXPORT_SYMBOL(hci_cmd_sync_dequeue_once);
768 /* Dequeue HCI command entry:
770 * - Lookup and cancel any entry that matches by function callback or data or
773 bool hci_cmd_sync_dequeue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
774 void *data, hci_cmd_sync_work_destroy_t destroy)
776 struct hci_cmd_sync_work_entry *entry;
779 mutex_lock(&hdev->cmd_sync_work_lock);
780 while ((entry = _hci_cmd_sync_lookup_entry(hdev, func, data,
782 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
785 mutex_unlock(&hdev->cmd_sync_work_lock);
789 EXPORT_SYMBOL(hci_cmd_sync_dequeue);
791 int hci_update_eir_sync(struct hci_dev *hdev)
793 struct hci_cp_write_eir cp;
795 bt_dev_dbg(hdev, "");
797 if (!hdev_is_powered(hdev))
800 if (!lmp_ext_inq_capable(hdev))
803 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
806 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
809 memset(&cp, 0, sizeof(cp));
811 eir_create(hdev, cp.data);
813 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
816 memcpy(hdev->eir, cp.data, sizeof(cp.data));
818 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
822 static u8 get_service_classes(struct hci_dev *hdev)
824 struct bt_uuid *uuid;
827 list_for_each_entry(uuid, &hdev->uuids, list)
828 val |= uuid->svc_hint;
833 int hci_update_class_sync(struct hci_dev *hdev)
837 bt_dev_dbg(hdev, "");
839 if (!hdev_is_powered(hdev))
842 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
845 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
848 cod[0] = hdev->minor_class;
849 cod[1] = hdev->major_class;
850 cod[2] = get_service_classes(hdev);
852 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
855 if (memcmp(cod, hdev->dev_class, 3) == 0)
858 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
859 sizeof(cod), cod, HCI_CMD_TIMEOUT);
862 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
864 /* If there is no connection we are OK to advertise. */
865 if (hci_conn_num(hdev, LE_LINK) == 0)
868 /* Check le_states if there is any connection in peripheral role. */
869 if (hdev->conn_hash.le_num_peripheral > 0) {
870 /* Peripheral connection state and non connectable mode
873 if (!connectable && !(hdev->le_states[2] & 0x10))
876 /* Peripheral connection state and connectable mode bit 38
877 * and scannable bit 21.
879 if (connectable && (!(hdev->le_states[4] & 0x40) ||
880 !(hdev->le_states[2] & 0x20)))
884 /* Check le_states if there is any connection in central role. */
885 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
886 /* Central connection state and non connectable mode bit 18. */
887 if (!connectable && !(hdev->le_states[2] & 0x02))
890 /* Central connection state and connectable mode bit 35 and
893 if (connectable && (!(hdev->le_states[4] & 0x08) ||
894 !(hdev->le_states[2] & 0x08)))
901 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
903 /* If privacy is not enabled don't use RPA */
904 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
907 /* If basic privacy mode is enabled use RPA */
908 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
911 /* If limited privacy mode is enabled don't use RPA if we're
912 * both discoverable and bondable.
914 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
915 hci_dev_test_flag(hdev, HCI_BONDABLE))
918 /* We're neither bondable nor discoverable in the limited
919 * privacy mode, therefore use RPA.
924 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
926 /* If we're advertising or initiating an LE connection we can't
927 * go ahead and change the random address at this time. This is
928 * because the eventual initiator address used for the
929 * subsequently created connection will be undefined (some
930 * controllers use the new address and others the one we had
931 * when the operation started).
933 * In this kind of scenario skip the update and let the random
934 * address be updated at the next cycle.
936 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
937 hci_lookup_le_connect(hdev)) {
938 bt_dev_dbg(hdev, "Deferring random address update");
939 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
943 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
944 6, rpa, HCI_CMD_TIMEOUT);
947 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
948 bool rpa, u8 *own_addr_type)
952 /* If privacy is enabled use a resolvable private address. If
953 * current RPA has expired or there is something else than
954 * the current RPA in use, then generate a new one.
957 /* If Controller supports LL Privacy use own address type is
960 if (use_ll_privacy(hdev))
961 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
963 *own_addr_type = ADDR_LE_DEV_RANDOM;
965 /* Check if RPA is valid */
969 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
971 bt_dev_err(hdev, "failed to generate new RPA");
975 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
982 /* In case of required privacy without resolvable private address,
983 * use an non-resolvable private address. This is useful for active
984 * scanning and non-connectable advertising.
986 if (require_privacy) {
990 /* The non-resolvable private address is generated
991 * from random six bytes with the two most significant
994 get_random_bytes(&nrpa, 6);
997 /* The non-resolvable private address shall not be
998 * equal to the public address.
1000 if (bacmp(&hdev->bdaddr, &nrpa))
1004 *own_addr_type = ADDR_LE_DEV_RANDOM;
1006 return hci_set_random_addr_sync(hdev, &nrpa);
1009 /* If forcing static address is in use or there is no public
1010 * address use the static address as random address (but skip
1011 * the HCI command if the current random address is already the
1014 * In case BR/EDR has been disabled on a dual-mode controller
1015 * and a static address has been configured, then use that
1016 * address instead of the public BR/EDR address.
1018 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
1019 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
1020 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
1021 bacmp(&hdev->static_addr, BDADDR_ANY))) {
1022 *own_addr_type = ADDR_LE_DEV_RANDOM;
1023 if (bacmp(&hdev->static_addr, &hdev->random_addr))
1024 return hci_set_random_addr_sync(hdev,
1025 &hdev->static_addr);
1029 /* Neither privacy nor static address is being used so use a
1032 *own_addr_type = ADDR_LE_DEV_PUBLIC;
1037 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1039 struct hci_cp_le_set_ext_adv_enable *cp;
1040 struct hci_cp_ext_adv_set *set;
1041 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1044 /* If request specifies an instance that doesn't exist, fail */
1046 struct adv_info *adv;
1048 adv = hci_find_adv_instance(hdev, instance);
1052 /* If not enabled there is nothing to do */
1057 memset(data, 0, sizeof(data));
1060 set = (void *)cp->data;
1062 /* Instance 0x00 indicates all advertising instances will be disabled */
1063 cp->num_of_sets = !!instance;
1066 set->handle = instance;
1068 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
1070 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1071 size, data, HCI_CMD_TIMEOUT);
1074 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1075 bdaddr_t *random_addr)
1077 struct hci_cp_le_set_adv_set_rand_addr cp;
1081 /* Instance 0x00 doesn't have an adv_info, instead it uses
1082 * hdev->random_addr to track its address so whenever it needs
1083 * to be updated this also set the random address since
1084 * hdev->random_addr is shared with scan state machine.
1086 err = hci_set_random_addr_sync(hdev, random_addr);
1091 memset(&cp, 0, sizeof(cp));
1093 cp.handle = instance;
1094 bacpy(&cp.bdaddr, random_addr);
1096 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1097 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1100 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1102 struct hci_cp_le_set_ext_adv_params cp;
1105 bdaddr_t random_addr;
1108 struct adv_info *adv;
1112 adv = hci_find_adv_instance(hdev, instance);
1119 /* Updating parameters of an active instance will return a
1120 * Command Disallowed error, so we must first disable the
1121 * instance if it is active.
1123 if (adv && !adv->pending) {
1124 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1129 flags = hci_adv_instance_flags(hdev, instance);
1131 /* If the "connectable" instance flag was not set, then choose between
1132 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1134 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1135 mgmt_get_connectable(hdev);
1137 if (!is_advertising_allowed(hdev, connectable))
1140 /* Set require_privacy to true only when non-connectable
1141 * advertising is used. In that case it is fine to use a
1142 * non-resolvable private address.
1144 err = hci_get_random_address(hdev, !connectable,
1145 adv_use_rpa(hdev, flags), adv,
1146 &own_addr_type, &random_addr);
1150 memset(&cp, 0, sizeof(cp));
1153 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
1154 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
1155 cp.tx_power = adv->tx_power;
1157 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1158 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1159 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
1162 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
1166 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
1168 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
1169 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1170 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
1172 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
1174 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
1177 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
1179 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
1182 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
1183 * contains the peer’s Identity Address and the Peer_Address_Type
1184 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
1185 * These parameters are used to locate the corresponding local IRK in
1186 * the resolving list; this IRK is used to generate their own address
1187 * used in the advertisement.
1189 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
1190 hci_copy_identity_address(hdev, &cp.peer_addr,
1191 &cp.peer_addr_type);
1193 cp.own_addr_type = own_addr_type;
1194 cp.channel_map = hdev->le_adv_channel_map;
1195 cp.handle = instance;
1197 if (flags & MGMT_ADV_FLAG_SEC_2M) {
1198 cp.primary_phy = HCI_ADV_PHY_1M;
1199 cp.secondary_phy = HCI_ADV_PHY_2M;
1200 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
1201 cp.primary_phy = HCI_ADV_PHY_CODED;
1202 cp.secondary_phy = HCI_ADV_PHY_CODED;
1204 /* In all other cases use 1M */
1205 cp.primary_phy = HCI_ADV_PHY_1M;
1206 cp.secondary_phy = HCI_ADV_PHY_1M;
1209 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1210 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1214 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
1215 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
1216 bacmp(&random_addr, BDADDR_ANY)) {
1217 /* Check if random address need to be updated */
1219 if (!bacmp(&random_addr, &adv->random_addr))
1222 if (!bacmp(&random_addr, &hdev->random_addr))
1226 return hci_set_adv_set_random_addr_sync(hdev, instance,
1233 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1236 struct hci_cp_le_set_ext_scan_rsp_data cp;
1237 u8 data[HCI_MAX_EXT_AD_LENGTH];
1240 struct adv_info *adv = NULL;
1243 memset(&pdu, 0, sizeof(pdu));
1246 adv = hci_find_adv_instance(hdev, instance);
1247 if (!adv || !adv->scan_rsp_changed)
1251 len = eir_create_scan_rsp(hdev, instance, pdu.data);
1253 pdu.cp.handle = instance;
1254 pdu.cp.length = len;
1255 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1256 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1258 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1259 sizeof(pdu.cp) + len, &pdu.cp,
1265 adv->scan_rsp_changed = false;
1267 memcpy(hdev->scan_rsp_data, pdu.data, len);
1268 hdev->scan_rsp_data_len = len;
1274 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1276 struct hci_cp_le_set_scan_rsp_data cp;
1279 memset(&cp, 0, sizeof(cp));
1281 len = eir_create_scan_rsp(hdev, instance, cp.data);
1283 if (hdev->scan_rsp_data_len == len &&
1284 !memcmp(cp.data, hdev->scan_rsp_data, len))
1287 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1288 hdev->scan_rsp_data_len = len;
1292 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1293 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1296 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1298 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1301 if (ext_adv_capable(hdev))
1302 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1304 return __hci_set_scan_rsp_data_sync(hdev, instance);
1307 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1309 struct hci_cp_le_set_ext_adv_enable *cp;
1310 struct hci_cp_ext_adv_set *set;
1311 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1312 struct adv_info *adv;
1315 adv = hci_find_adv_instance(hdev, instance);
1318 /* If already enabled there is nothing to do */
1326 set = (void *)cp->data;
1328 memset(cp, 0, sizeof(*cp));
1331 cp->num_of_sets = 0x01;
1333 memset(set, 0, sizeof(*set));
1335 set->handle = instance;
1337 /* Set duration per instance since controller is responsible for
1340 if (adv && adv->timeout) {
1341 u16 duration = adv->timeout * MSEC_PER_SEC;
1343 /* Time = N * 10 ms */
1344 set->duration = cpu_to_le16(duration / 10);
1347 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1349 sizeof(*set) * cp->num_of_sets,
1350 data, HCI_CMD_TIMEOUT);
1353 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1357 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1361 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1365 return hci_enable_ext_advertising_sync(hdev, instance);
1368 int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1370 struct hci_cp_le_set_per_adv_enable cp;
1371 struct adv_info *adv = NULL;
1373 /* If periodic advertising already disabled there is nothing to do. */
1374 adv = hci_find_adv_instance(hdev, instance);
1375 if (!adv || !adv->periodic || !adv->enabled)
1378 memset(&cp, 0, sizeof(cp));
1381 cp.handle = instance;
1383 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1384 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1387 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1388 u16 min_interval, u16 max_interval)
1390 struct hci_cp_le_set_per_adv_params cp;
1392 memset(&cp, 0, sizeof(cp));
1395 min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1398 max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1400 cp.handle = instance;
1401 cp.min_interval = cpu_to_le16(min_interval);
1402 cp.max_interval = cpu_to_le16(max_interval);
1403 cp.periodic_properties = 0x0000;
1405 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1406 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1409 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1412 struct hci_cp_le_set_per_adv_data cp;
1413 u8 data[HCI_MAX_PER_AD_LENGTH];
1417 memset(&pdu, 0, sizeof(pdu));
1420 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1422 if (!adv || !adv->periodic)
1426 len = eir_create_per_adv_data(hdev, instance, pdu.data);
1428 pdu.cp.length = len;
1429 pdu.cp.handle = instance;
1430 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1432 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1433 sizeof(pdu.cp) + len, &pdu,
1437 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1439 struct hci_cp_le_set_per_adv_enable cp;
1440 struct adv_info *adv = NULL;
1442 /* If periodic advertising already enabled there is nothing to do. */
1443 adv = hci_find_adv_instance(hdev, instance);
1444 if (adv && adv->periodic && adv->enabled)
1447 memset(&cp, 0, sizeof(cp));
1450 cp.handle = instance;
1452 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1453 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1456 /* Checks if periodic advertising data contains a Basic Announcement and if it
1457 * does generates a Broadcast ID and add Broadcast Announcement.
1459 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1464 /* Skip if NULL adv as instance 0x00 is used for general purpose
1465 * advertising so it cannot used for the likes of Broadcast Announcement
1466 * as it can be overwritten at any point.
1471 /* Check if PA data doesn't contains a Basic Audio Announcement then
1472 * there is nothing to do.
1474 if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1478 /* Check if advertising data already has a Broadcast Announcement since
1479 * the process may want to control the Broadcast ID directly and in that
1480 * case the kernel shall no interfere.
1482 if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1486 /* Generate Broadcast ID */
1487 get_random_bytes(bid, sizeof(bid));
1488 eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1489 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1491 return hci_update_adv_data_sync(hdev, adv->instance);
1494 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1495 u8 *data, u32 flags, u16 min_interval,
1496 u16 max_interval, u16 sync_interval)
1498 struct adv_info *adv = NULL;
1502 hci_disable_per_advertising_sync(hdev, instance);
1505 adv = hci_find_adv_instance(hdev, instance);
1506 /* Create an instance if that could not be found */
1508 adv = hci_add_per_instance(hdev, instance, flags,
1513 return PTR_ERR(adv);
1514 adv->pending = false;
1519 /* Start advertising */
1520 err = hci_start_ext_adv_sync(hdev, instance);
1524 err = hci_adv_bcast_annoucement(hdev, adv);
1528 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1533 err = hci_set_per_adv_data_sync(hdev, instance);
1537 err = hci_enable_per_advertising_sync(hdev, instance);
1545 hci_remove_adv_instance(hdev, instance);
1550 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1554 if (ext_adv_capable(hdev))
1555 return hci_start_ext_adv_sync(hdev, instance);
1557 err = hci_update_adv_data_sync(hdev, instance);
1561 err = hci_update_scan_rsp_data_sync(hdev, instance);
1565 return hci_enable_advertising_sync(hdev);
1568 int hci_enable_advertising_sync(struct hci_dev *hdev)
1570 struct adv_info *adv_instance;
1571 struct hci_cp_le_set_adv_param cp;
1572 u8 own_addr_type, enable = 0x01;
1574 u16 adv_min_interval, adv_max_interval;
1578 if (ext_adv_capable(hdev))
1579 return hci_enable_ext_advertising_sync(hdev,
1580 hdev->cur_adv_instance);
1582 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1583 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1585 /* If the "connectable" instance flag was not set, then choose between
1586 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1588 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1589 mgmt_get_connectable(hdev);
1591 if (!is_advertising_allowed(hdev, connectable))
1594 status = hci_disable_advertising_sync(hdev);
1598 /* Clear the HCI_LE_ADV bit temporarily so that the
1599 * hci_update_random_address knows that it's safe to go ahead
1600 * and write a new random address. The flag will be set back on
1601 * as soon as the SET_ADV_ENABLE HCI command completes.
1603 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1605 /* Set require_privacy to true only when non-connectable
1606 * advertising is used. In that case it is fine to use a
1607 * non-resolvable private address.
1609 status = hci_update_random_address_sync(hdev, !connectable,
1610 adv_use_rpa(hdev, flags),
1615 memset(&cp, 0, sizeof(cp));
1618 adv_min_interval = adv_instance->min_interval;
1619 adv_max_interval = adv_instance->max_interval;
1621 adv_min_interval = hdev->le_adv_min_interval;
1622 adv_max_interval = hdev->le_adv_max_interval;
1626 cp.type = LE_ADV_IND;
1628 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1629 cp.type = LE_ADV_SCAN_IND;
1631 cp.type = LE_ADV_NONCONN_IND;
1633 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1634 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1635 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1636 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1640 cp.min_interval = cpu_to_le16(adv_min_interval);
1641 cp.max_interval = cpu_to_le16(adv_max_interval);
1642 cp.own_address_type = own_addr_type;
1643 cp.channel_map = hdev->le_adv_channel_map;
1645 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1646 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1650 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1651 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1654 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1656 return hci_enable_advertising_sync(hdev);
1659 int hci_enable_advertising(struct hci_dev *hdev)
1661 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1662 list_empty(&hdev->adv_instances))
1665 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1668 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1673 if (!ext_adv_capable(hdev))
1676 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1680 /* If request specifies an instance that doesn't exist, fail */
1681 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1684 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1685 sizeof(instance), &instance, 0,
1686 HCI_CMD_TIMEOUT, sk);
1689 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1691 struct adv_info *adv = data;
1695 instance = adv->instance;
1697 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1700 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1702 struct adv_info *adv = NULL;
1705 adv = hci_find_adv_instance(hdev, instance);
1710 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1713 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1715 struct hci_cp_le_term_big cp;
1717 memset(&cp, 0, sizeof(cp));
1721 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1722 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1725 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1728 struct hci_cp_le_set_ext_adv_data cp;
1729 u8 data[HCI_MAX_EXT_AD_LENGTH];
1732 struct adv_info *adv = NULL;
1735 memset(&pdu, 0, sizeof(pdu));
1738 adv = hci_find_adv_instance(hdev, instance);
1739 if (!adv || !adv->adv_data_changed)
1743 len = eir_create_adv_data(hdev, instance, pdu.data);
1745 pdu.cp.length = len;
1746 pdu.cp.handle = instance;
1747 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1748 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1750 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1751 sizeof(pdu.cp) + len, &pdu.cp,
1756 /* Update data if the command succeed */
1758 adv->adv_data_changed = false;
1760 memcpy(hdev->adv_data, pdu.data, len);
1761 hdev->adv_data_len = len;
1767 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1769 struct hci_cp_le_set_adv_data cp;
1772 memset(&cp, 0, sizeof(cp));
1774 len = eir_create_adv_data(hdev, instance, cp.data);
1776 /* There's nothing to do if the data hasn't changed */
1777 if (hdev->adv_data_len == len &&
1778 memcmp(cp.data, hdev->adv_data, len) == 0)
1781 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1782 hdev->adv_data_len = len;
1786 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1787 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1790 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1792 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1795 if (ext_adv_capable(hdev))
1796 return hci_set_ext_adv_data_sync(hdev, instance);
1798 return hci_set_adv_data_sync(hdev, instance);
1801 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1804 struct adv_info *adv = NULL;
1807 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1810 if (hdev->adv_instance_timeout)
1813 adv = hci_find_adv_instance(hdev, instance);
1817 /* A zero timeout means unlimited advertising. As long as there is
1818 * only one instance, duration should be ignored. We still set a timeout
1819 * in case further instances are being added later on.
1821 * If the remaining lifetime of the instance is more than the duration
1822 * then the timeout corresponds to the duration, otherwise it will be
1823 * reduced to the remaining instance lifetime.
1825 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1826 timeout = adv->duration;
1828 timeout = adv->remaining_time;
1830 /* The remaining time is being reduced unless the instance is being
1831 * advertised without time limit.
1834 adv->remaining_time = adv->remaining_time - timeout;
1836 /* Only use work for scheduling instances with legacy advertising */
1837 if (!ext_adv_capable(hdev)) {
1838 hdev->adv_instance_timeout = timeout;
1839 queue_delayed_work(hdev->req_workqueue,
1840 &hdev->adv_instance_expire,
1841 msecs_to_jiffies(timeout * 1000));
1844 /* If we're just re-scheduling the same instance again then do not
1845 * execute any HCI commands. This happens when a single instance is
1848 if (!force && hdev->cur_adv_instance == instance &&
1849 hci_dev_test_flag(hdev, HCI_LE_ADV))
1852 hdev->cur_adv_instance = instance;
1854 return hci_start_adv_sync(hdev, instance);
1857 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1861 if (!ext_adv_capable(hdev))
1864 /* Disable instance 0x00 to disable all instances */
1865 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1869 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1870 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1873 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1875 struct adv_info *adv, *n;
1878 if (ext_adv_capable(hdev))
1879 /* Remove all existing sets */
1880 err = hci_clear_adv_sets_sync(hdev, sk);
1881 if (ext_adv_capable(hdev))
1884 /* This is safe as long as there is no command send while the lock is
1889 /* Cleanup non-ext instances */
1890 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1891 u8 instance = adv->instance;
1894 if (!(force || adv->timeout))
1897 err = hci_remove_adv_instance(hdev, instance);
1899 mgmt_advertising_removed(sk, hdev, instance);
1902 hci_dev_unlock(hdev);
1907 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1912 /* If we use extended advertising, instance has to be removed first. */
1913 if (ext_adv_capable(hdev))
1914 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1915 if (ext_adv_capable(hdev))
1918 /* This is safe as long as there is no command send while the lock is
1923 err = hci_remove_adv_instance(hdev, instance);
1925 mgmt_advertising_removed(sk, hdev, instance);
1927 hci_dev_unlock(hdev);
1932 /* For a single instance:
1933 * - force == true: The instance will be removed even when its remaining
1934 * lifetime is not zero.
1935 * - force == false: the instance will be deactivated but kept stored unless
1936 * the remaining lifetime is zero.
1938 * For instance == 0x00:
1939 * - force == true: All instances will be removed regardless of their timeout
1941 * - force == false: Only instances that have a timeout will be removed.
1943 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1944 u8 instance, bool force)
1946 struct adv_info *next = NULL;
1949 /* Cancel any timeout concerning the removed instance(s). */
1950 if (!instance || hdev->cur_adv_instance == instance)
1951 cancel_adv_timeout(hdev);
1953 /* Get the next instance to advertise BEFORE we remove
1954 * the current one. This can be the same instance again
1955 * if there is only one instance.
1957 if (hdev->cur_adv_instance == instance)
1958 next = hci_get_next_instance(hdev, instance);
1961 err = hci_clear_adv_sync(hdev, sk, force);
1965 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1967 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1968 /* Don't advertise a removed instance. */
1969 if (next && next->instance == instance)
1972 err = hci_remove_adv_sync(hdev, instance, sk);
1978 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1981 if (next && !ext_adv_capable(hdev))
1982 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1987 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1989 struct hci_cp_read_rssi cp;
1992 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1993 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1996 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1998 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1999 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
2002 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
2004 struct hci_cp_read_tx_power cp;
2008 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
2009 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2012 int hci_disable_advertising_sync(struct hci_dev *hdev)
2017 /* If controller is not advertising we are done. */
2018 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
2021 if (ext_adv_capable(hdev))
2022 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
2023 if (ext_adv_capable(hdev))
2026 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
2027 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
2030 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
2033 struct hci_cp_le_set_ext_scan_enable cp;
2035 memset(&cp, 0, sizeof(cp));
2038 if (hci_dev_test_flag(hdev, HCI_MESH))
2039 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2041 cp.filter_dup = filter_dup;
2043 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
2044 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2047 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2050 struct hci_cp_le_set_scan_enable cp;
2052 if (use_ext_scan(hdev))
2053 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2055 memset(&cp, 0, sizeof(cp));
2058 if (val && hci_dev_test_flag(hdev, HCI_MESH))
2059 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2061 cp.filter_dup = filter_dup;
2063 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2064 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2067 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2069 if (!use_ll_privacy(hdev))
2072 /* If controller is not/already resolving we are done. */
2073 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2076 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2077 sizeof(val), &val, HCI_CMD_TIMEOUT);
2080 static int hci_scan_disable_sync(struct hci_dev *hdev)
2084 /* If controller is not scanning we are done. */
2085 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2088 if (hdev->scanning_paused) {
2089 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2093 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2095 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2102 static bool scan_use_rpa(struct hci_dev *hdev)
2104 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2107 static void hci_start_interleave_scan(struct hci_dev *hdev)
2109 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2110 queue_delayed_work(hdev->req_workqueue,
2111 &hdev->interleave_scan, 0);
2114 static bool is_interleave_scanning(struct hci_dev *hdev)
2116 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2119 static void cancel_interleave_scan(struct hci_dev *hdev)
2121 bt_dev_dbg(hdev, "cancelling interleave scan");
2123 cancel_delayed_work_sync(&hdev->interleave_scan);
2125 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2128 /* Return true if interleave_scan wasn't started until exiting this function,
2129 * otherwise, return false
2131 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2133 /* Do interleaved scan only if all of the following are true:
2134 * - There is at least one ADV monitor
2135 * - At least one pending LE connection or one device to be scanned for
2136 * - Monitor offloading is not supported
2137 * If so, we should alternate between allowlist scan and one without
2138 * any filters to save power.
2140 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2141 !(list_empty(&hdev->pend_le_conns) &&
2142 list_empty(&hdev->pend_le_reports)) &&
2143 hci_get_adv_monitor_offload_ext(hdev) ==
2144 HCI_ADV_MONITOR_EXT_NONE;
2145 bool is_interleaving = is_interleave_scanning(hdev);
2147 if (use_interleaving && !is_interleaving) {
2148 hci_start_interleave_scan(hdev);
2149 bt_dev_dbg(hdev, "starting interleave scan");
2153 if (!use_interleaving && is_interleaving)
2154 cancel_interleave_scan(hdev);
2159 /* Removes connection to resolve list if needed.*/
2160 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2161 bdaddr_t *bdaddr, u8 bdaddr_type)
2163 struct hci_cp_le_del_from_resolv_list cp;
2164 struct bdaddr_list_with_irk *entry;
2166 if (!use_ll_privacy(hdev))
2169 /* Check if the IRK has been programmed */
2170 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2175 cp.bdaddr_type = bdaddr_type;
2176 bacpy(&cp.bdaddr, bdaddr);
2178 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2179 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2182 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2183 bdaddr_t *bdaddr, u8 bdaddr_type)
2185 struct hci_cp_le_del_from_accept_list cp;
2188 /* Check if device is on accept list before removing it */
2189 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2192 cp.bdaddr_type = bdaddr_type;
2193 bacpy(&cp.bdaddr, bdaddr);
2195 /* Ignore errors when removing from resolving list as that is likely
2196 * that the device was never added.
2198 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2200 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2201 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2203 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2207 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2213 struct conn_params {
2216 hci_conn_flags_t flags;
2220 /* Adds connection to resolve list if needed.
2221 * Setting params to NULL programs local hdev->irk
2223 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2224 struct conn_params *params)
2226 struct hci_cp_le_add_to_resolv_list cp;
2227 struct smp_irk *irk;
2228 struct bdaddr_list_with_irk *entry;
2229 struct hci_conn_params *p;
2231 if (!use_ll_privacy(hdev))
2234 /* Attempt to program local identity address, type and irk if params is
2238 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2241 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2242 memcpy(cp.peer_irk, hdev->irk, 16);
2246 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2250 /* Check if the IK has _not_ been programmed yet. */
2251 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2257 cp.bdaddr_type = params->addr_type;
2258 bacpy(&cp.bdaddr, ¶ms->addr);
2259 memcpy(cp.peer_irk, irk->val, 16);
2261 /* Default privacy mode is always Network */
2262 params->privacy_mode = HCI_NETWORK_PRIVACY;
2265 p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2266 ¶ms->addr, params->addr_type);
2268 p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2269 ¶ms->addr, params->addr_type);
2271 WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY);
2275 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2276 memcpy(cp.local_irk, hdev->irk, 16);
2278 memset(cp.local_irk, 0, 16);
2280 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2281 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2284 /* Set Device Privacy Mode. */
2285 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2286 struct conn_params *params)
2288 struct hci_cp_le_set_privacy_mode cp;
2289 struct smp_irk *irk;
2291 /* If device privacy mode has already been set there is nothing to do */
2292 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2295 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2296 * indicates that LL Privacy has been enabled and
2297 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2299 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2302 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2306 memset(&cp, 0, sizeof(cp));
2307 cp.bdaddr_type = irk->addr_type;
2308 bacpy(&cp.bdaddr, &irk->bdaddr);
2309 cp.mode = HCI_DEVICE_PRIVACY;
2311 /* Note: params->privacy_mode is not updated since it is a copy */
2313 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2314 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2317 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2318 * this attempts to program the device in the resolving list as well and
2319 * properly set the privacy mode.
2321 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2322 struct conn_params *params,
2325 struct hci_cp_le_add_to_accept_list cp;
2328 /* During suspend, only wakeable devices can be in acceptlist */
2329 if (hdev->suspended &&
2330 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) {
2331 hci_le_del_accept_list_sync(hdev, ¶ms->addr,
2336 /* Select filter policy to accept all advertising */
2337 if (*num_entries >= hdev->le_accept_list_size)
2340 /* Accept list can not be used with RPAs */
2341 if (!use_ll_privacy(hdev) &&
2342 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type))
2345 /* Attempt to program the device in the resolving list first to avoid
2346 * having to rollback in case it fails since the resolving list is
2347 * dynamic it can probably be smaller than the accept list.
2349 err = hci_le_add_resolve_list_sync(hdev, params);
2351 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2355 /* Set Privacy Mode */
2356 err = hci_le_set_privacy_mode_sync(hdev, params);
2358 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2362 /* Check if already in accept list */
2363 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
2368 cp.bdaddr_type = params->addr_type;
2369 bacpy(&cp.bdaddr, ¶ms->addr);
2371 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2372 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2374 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2375 /* Rollback the device from the resolving list */
2376 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2380 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2386 /* This function disables/pause all advertising instances */
2387 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2392 /* If already been paused there is nothing to do. */
2393 if (hdev->advertising_paused)
2396 bt_dev_dbg(hdev, "Pausing directed advertising");
2398 /* Stop directed advertising */
2399 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2401 /* When discoverable timeout triggers, then just make sure
2402 * the limited discoverable flag is cleared. Even in the case
2403 * of a timeout triggered from general discoverable, it is
2404 * safe to unconditionally clear the flag.
2406 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2407 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2408 hdev->discov_timeout = 0;
2411 bt_dev_dbg(hdev, "Pausing advertising instances");
2413 /* Call to disable any advertisements active on the controller.
2414 * This will succeed even if no advertisements are configured.
2416 err = hci_disable_advertising_sync(hdev);
2420 /* If we are using software rotation, pause the loop */
2421 if (!ext_adv_capable(hdev))
2422 cancel_adv_timeout(hdev);
2424 hdev->advertising_paused = true;
2425 hdev->advertising_old_state = old_state;
2430 /* This function enables all user advertising instances */
2431 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2433 struct adv_info *adv, *tmp;
2436 /* If advertising has not been paused there is nothing to do. */
2437 if (!hdev->advertising_paused)
2440 /* Resume directed advertising */
2441 hdev->advertising_paused = false;
2442 if (hdev->advertising_old_state) {
2443 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2444 hdev->advertising_old_state = 0;
2447 bt_dev_dbg(hdev, "Resuming advertising instances");
2449 if (ext_adv_capable(hdev)) {
2450 /* Call for each tracked instance to be re-enabled */
2451 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2452 err = hci_enable_ext_advertising_sync(hdev,
2457 /* If the instance cannot be resumed remove it */
2458 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2462 /* Schedule for most recent instance to be restarted and begin
2463 * the software rotation loop
2465 err = hci_schedule_adv_instance_sync(hdev,
2466 hdev->cur_adv_instance,
2470 hdev->advertising_paused = false;
2475 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2479 if (!use_ll_privacy(hdev))
2482 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2485 /* Cannot disable addr resolution if scanning is enabled or
2486 * when initiating an LE connection.
2488 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2489 hci_lookup_le_connect(hdev)) {
2490 bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2494 /* Cannot disable addr resolution if advertising is enabled. */
2495 err = hci_pause_advertising_sync(hdev);
2497 bt_dev_err(hdev, "Pause advertising failed: %d", err);
2501 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2503 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2506 /* Return if address resolution is disabled and RPA is not used. */
2507 if (!err && scan_use_rpa(hdev))
2510 hci_resume_advertising_sync(hdev);
2514 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2515 bool extended, struct sock *sk)
2517 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2518 HCI_OP_READ_LOCAL_OOB_DATA;
2520 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2523 static struct conn_params *conn_params_copy(struct list_head *list, size_t *n)
2525 struct hci_conn_params *params;
2526 struct conn_params *p;
2532 list_for_each_entry_rcu(params, list, action)
2538 p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL);
2545 list_for_each_entry_rcu(params, list, action) {
2546 /* Racing adds are handled in next scan update */
2550 /* No hdev->lock, but: addr, addr_type are immutable.
2551 * privacy_mode is only written by us or in
2552 * hci_cc_le_set_privacy_mode that we wait for.
2553 * We should be idempotent so MGMT updating flags
2554 * while we are processing is OK.
2556 bacpy(&p[i].addr, ¶ms->addr);
2557 p[i].addr_type = params->addr_type;
2558 p[i].flags = READ_ONCE(params->flags);
2559 p[i].privacy_mode = READ_ONCE(params->privacy_mode);
2569 /* Clear LE Accept List */
2570 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
2572 if (!(hdev->commands[26] & 0x80))
2575 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
2579 /* Device must not be scanning when updating the accept list.
2581 * Update is done using the following sequence:
2583 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
2584 * Remove Devices From Accept List ->
2585 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
2586 * Add Devices to Accept List ->
2587 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
2588 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
2591 * In case of failure advertising shall be restored to its original state and
2592 * return would disable accept list since either accept or resolving list could
2593 * not be programmed.
2596 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2598 struct conn_params *params;
2599 struct bdaddr_list *b, *t;
2601 bool pend_conn, pend_report;
2606 /* Pause advertising if resolving list can be used as controllers
2607 * cannot accept resolving list modifications while advertising.
2609 if (use_ll_privacy(hdev)) {
2610 err = hci_pause_advertising_sync(hdev);
2612 bt_dev_err(hdev, "pause advertising failed: %d", err);
2617 /* Disable address resolution while reprogramming accept list since
2618 * devices that do have an IRK will be programmed in the resolving list
2619 * when LL Privacy is enabled.
2621 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2623 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2627 /* Force address filtering if PA Sync is in progress */
2628 if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2629 struct hci_cp_le_pa_create_sync *sent;
2631 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_PA_CREATE_SYNC);
2633 struct conn_params pa;
2635 memset(&pa, 0, sizeof(pa));
2637 bacpy(&pa.addr, &sent->addr);
2638 pa.addr_type = sent->addr_type;
2640 /* Clear first since there could be addresses left
2643 hci_le_clear_accept_list_sync(hdev);
2646 err = hci_le_add_accept_list_sync(hdev, &pa,
2652 /* Go through the current accept list programmed into the
2653 * controller one by one and check if that address is connected or is
2654 * still in the list of pending connections or list of devices to
2655 * report. If not present in either list, then remove it from
2658 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2659 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2662 /* Pointers not dereferenced, no locks needed */
2663 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2666 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2670 /* If the device is not likely to connect or report,
2671 * remove it from the acceptlist.
2673 if (!pend_conn && !pend_report) {
2674 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2682 /* Since all no longer valid accept list entries have been
2683 * removed, walk through the list of pending connections
2684 * and ensure that any new device gets programmed into
2687 * If the list of the devices is larger than the list of
2688 * available accept list entries in the controller, then
2689 * just abort and return filer policy value to not use the
2692 * The list and params may be mutated while we wait for events,
2693 * so make a copy and iterate it.
2696 params = conn_params_copy(&hdev->pend_le_conns, &n);
2702 for (i = 0; i < n; ++i) {
2703 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2713 /* After adding all new pending connections, walk through
2714 * the list of pending reports and also add these to the
2715 * accept list if there is still space. Abort if space runs out.
2718 params = conn_params_copy(&hdev->pend_le_reports, &n);
2724 for (i = 0; i < n; ++i) {
2725 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2735 /* Use the allowlist unless the following conditions are all true:
2736 * - We are not currently suspending
2737 * - There are 1 or more ADV monitors registered and it's not offloaded
2738 * - Interleaved scanning is not currently using the allowlist
2740 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2741 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2742 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2746 filter_policy = err ? 0x00 : 0x01;
2748 /* Enable address resolution when LL Privacy is enabled. */
2749 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2751 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2753 /* Resume advertising if it was paused */
2754 if (use_ll_privacy(hdev))
2755 hci_resume_advertising_sync(hdev);
2757 /* Select filter policy to use accept list */
2758 return filter_policy;
2761 static void hci_le_scan_phy_params(struct hci_cp_le_scan_phy_params *cp,
2762 u8 type, u16 interval, u16 window)
2765 cp->interval = cpu_to_le16(interval);
2766 cp->window = cpu_to_le16(window);
2769 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2770 u16 interval, u16 window,
2771 u8 own_addr_type, u8 filter_policy)
2773 struct hci_cp_le_set_ext_scan_params *cp;
2774 struct hci_cp_le_scan_phy_params *phy;
2775 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2779 phy = (void *)cp->data;
2781 memset(data, 0, sizeof(data));
2783 cp->own_addr_type = own_addr_type;
2784 cp->filter_policy = filter_policy;
2786 /* Check if PA Sync is in progress then select the PHY based on the
2789 if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2790 struct hci_cp_le_add_to_accept_list *sent;
2792 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
2794 struct hci_conn *conn;
2796 conn = hci_conn_hash_lookup_ba(hdev, ISO_LINK,
2799 struct bt_iso_qos *qos = &conn->iso_qos;
2801 if (qos->bcast.in.phy & BT_ISO_PHY_1M ||
2802 qos->bcast.in.phy & BT_ISO_PHY_2M) {
2803 cp->scanning_phys |= LE_SCAN_PHY_1M;
2804 hci_le_scan_phy_params(phy, type,
2811 if (qos->bcast.in.phy & BT_ISO_PHY_CODED) {
2812 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2813 hci_le_scan_phy_params(phy, type,
2826 if (scan_1m(hdev) || scan_2m(hdev)) {
2827 cp->scanning_phys |= LE_SCAN_PHY_1M;
2828 hci_le_scan_phy_params(phy, type, interval, window);
2833 if (scan_coded(hdev)) {
2834 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2835 hci_le_scan_phy_params(phy, type, interval, window);
2844 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2845 sizeof(*cp) + sizeof(*phy) * num_phy,
2846 data, HCI_CMD_TIMEOUT);
2849 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2850 u16 interval, u16 window,
2851 u8 own_addr_type, u8 filter_policy)
2853 struct hci_cp_le_set_scan_param cp;
2855 if (use_ext_scan(hdev))
2856 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2857 window, own_addr_type,
2860 memset(&cp, 0, sizeof(cp));
2862 cp.interval = cpu_to_le16(interval);
2863 cp.window = cpu_to_le16(window);
2864 cp.own_address_type = own_addr_type;
2865 cp.filter_policy = filter_policy;
2867 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2868 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2871 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2872 u16 window, u8 own_addr_type, u8 filter_policy,
2877 if (hdev->scanning_paused) {
2878 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2882 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2883 own_addr_type, filter_policy);
2887 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2890 static int hci_passive_scan_sync(struct hci_dev *hdev)
2894 u16 window, interval;
2895 u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2898 if (hdev->scanning_paused) {
2899 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2903 err = hci_scan_disable_sync(hdev);
2905 bt_dev_err(hdev, "disable scanning failed: %d", err);
2909 /* Set require_privacy to false since no SCAN_REQ are send
2910 * during passive scanning. Not using an non-resolvable address
2911 * here is important so that peer devices using direct
2912 * advertising with our address will be correctly reported
2913 * by the controller.
2915 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2919 if (hdev->enable_advmon_interleave_scan &&
2920 hci_update_interleaved_scan_sync(hdev))
2923 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2925 /* Adding or removing entries from the accept list must
2926 * happen before enabling scanning. The controller does
2927 * not allow accept list modification while scanning.
2929 filter_policy = hci_update_accept_list_sync(hdev);
2931 /* When the controller is using random resolvable addresses and
2932 * with that having LE privacy enabled, then controllers with
2933 * Extended Scanner Filter Policies support can now enable support
2934 * for handling directed advertising.
2936 * So instead of using filter polices 0x00 (no acceptlist)
2937 * and 0x01 (acceptlist enabled) use the new filter policies
2938 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2940 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2941 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2942 filter_policy |= 0x02;
2944 if (hdev->suspended) {
2945 window = hdev->le_scan_window_suspend;
2946 interval = hdev->le_scan_int_suspend;
2947 } else if (hci_is_le_conn_scanning(hdev)) {
2948 window = hdev->le_scan_window_connect;
2949 interval = hdev->le_scan_int_connect;
2950 } else if (hci_is_adv_monitoring(hdev)) {
2951 window = hdev->le_scan_window_adv_monitor;
2952 interval = hdev->le_scan_int_adv_monitor;
2954 window = hdev->le_scan_window;
2955 interval = hdev->le_scan_interval;
2958 /* Disable all filtering for Mesh */
2959 if (hci_dev_test_flag(hdev, HCI_MESH)) {
2961 filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
2964 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2966 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2967 own_addr_type, filter_policy, filter_dups);
2970 /* This function controls the passive scanning based on hdev->pend_le_conns
2971 * list. If there are pending LE connection we start the background scanning,
2972 * otherwise we stop it in the following sequence:
2974 * If there are devices to scan:
2976 * Disable Scanning -> Update Accept List ->
2977 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2978 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2985 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2989 if (!test_bit(HCI_UP, &hdev->flags) ||
2990 test_bit(HCI_INIT, &hdev->flags) ||
2991 hci_dev_test_flag(hdev, HCI_SETUP) ||
2992 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2993 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2994 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2997 /* No point in doing scanning if LE support hasn't been enabled */
2998 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3001 /* If discovery is active don't interfere with it */
3002 if (hdev->discovery.state != DISCOVERY_STOPPED)
3005 /* Reset RSSI and UUID filters when starting background scanning
3006 * since these filters are meant for service discovery only.
3008 * The Start Discovery and Start Service Discovery operations
3009 * ensure to set proper values for RSSI threshold and UUID
3010 * filter list. So it is safe to just reset them here.
3012 hci_discovery_filter_clear(hdev);
3014 bt_dev_dbg(hdev, "ADV monitoring is %s",
3015 hci_is_adv_monitoring(hdev) ? "on" : "off");
3017 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
3018 list_empty(&hdev->pend_le_conns) &&
3019 list_empty(&hdev->pend_le_reports) &&
3020 !hci_is_adv_monitoring(hdev) &&
3021 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
3022 /* If there is no pending LE connections or devices
3023 * to be scanned for or no ADV monitors, we should stop the
3024 * background scanning.
3027 bt_dev_dbg(hdev, "stopping background scanning");
3029 err = hci_scan_disable_sync(hdev);
3031 bt_dev_err(hdev, "stop background scanning failed: %d",
3034 /* If there is at least one pending LE connection, we should
3035 * keep the background scan running.
3038 /* If controller is connecting, we should not start scanning
3039 * since some controllers are not able to scan and connect at
3042 if (hci_lookup_le_connect(hdev))
3045 bt_dev_dbg(hdev, "start background scanning");
3047 err = hci_passive_scan_sync(hdev);
3049 bt_dev_err(hdev, "start background scanning failed: %d",
3056 static int update_scan_sync(struct hci_dev *hdev, void *data)
3058 return hci_update_scan_sync(hdev);
3061 int hci_update_scan(struct hci_dev *hdev)
3063 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
3066 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
3068 return hci_update_passive_scan_sync(hdev);
3071 int hci_update_passive_scan(struct hci_dev *hdev)
3073 /* Only queue if it would have any effect */
3074 if (!test_bit(HCI_UP, &hdev->flags) ||
3075 test_bit(HCI_INIT, &hdev->flags) ||
3076 hci_dev_test_flag(hdev, HCI_SETUP) ||
3077 hci_dev_test_flag(hdev, HCI_CONFIG) ||
3078 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
3079 hci_dev_test_flag(hdev, HCI_UNREGISTER))
3082 return hci_cmd_sync_queue_once(hdev, update_passive_scan_sync, NULL,
3086 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
3090 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
3093 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3094 sizeof(val), &val, HCI_CMD_TIMEOUT);
3098 hdev->features[1][0] |= LMP_HOST_SC;
3099 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
3101 hdev->features[1][0] &= ~LMP_HOST_SC;
3102 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
3109 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
3113 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3114 lmp_host_ssp_capable(hdev))
3117 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
3118 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
3119 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3122 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3123 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3127 return hci_write_sc_support_sync(hdev, 0x01);
3130 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
3132 struct hci_cp_write_le_host_supported cp;
3134 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
3135 !lmp_bredr_capable(hdev))
3138 /* Check first if we already have the right host state
3139 * (host features set)
3141 if (le == lmp_host_le_capable(hdev) &&
3142 simul == lmp_host_le_br_capable(hdev))
3145 memset(&cp, 0, sizeof(cp));
3150 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3151 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3154 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3156 struct adv_info *adv, *tmp;
3159 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3162 /* If RPA Resolution has not been enable yet it means the
3163 * resolving list is empty and we should attempt to program the
3164 * local IRK in order to support using own_addr_type
3165 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
3167 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3168 hci_le_add_resolve_list_sync(hdev, NULL);
3169 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3172 /* Make sure the controller has a good default for
3173 * advertising data. This also applies to the case
3174 * where BR/EDR was toggled during the AUTO_OFF phase.
3176 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3177 list_empty(&hdev->adv_instances)) {
3178 if (ext_adv_capable(hdev)) {
3179 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3181 hci_update_scan_rsp_data_sync(hdev, 0x00);
3183 err = hci_update_adv_data_sync(hdev, 0x00);
3185 hci_update_scan_rsp_data_sync(hdev, 0x00);
3188 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3189 hci_enable_advertising_sync(hdev);
3192 /* Call for each tracked instance to be scheduled */
3193 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3194 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3199 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3203 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3204 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3207 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3208 sizeof(link_sec), &link_sec,
3212 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3214 struct hci_cp_write_page_scan_activity cp;
3218 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3221 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3224 memset(&cp, 0, sizeof(cp));
3227 type = PAGE_SCAN_TYPE_INTERLACED;
3229 /* 160 msec page scan interval */
3230 cp.interval = cpu_to_le16(0x0100);
3232 type = hdev->def_page_scan_type;
3233 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3236 cp.window = cpu_to_le16(hdev->def_page_scan_window);
3238 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3239 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3240 err = __hci_cmd_sync_status(hdev,
3241 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
3242 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3247 if (hdev->page_scan_type != type)
3248 err = __hci_cmd_sync_status(hdev,
3249 HCI_OP_WRITE_PAGE_SCAN_TYPE,
3250 sizeof(type), &type,
3256 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3258 struct bdaddr_list *b;
3260 list_for_each_entry(b, &hdev->accept_list, list) {
3261 struct hci_conn *conn;
3263 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3267 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3274 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3276 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3281 int hci_update_scan_sync(struct hci_dev *hdev)
3285 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3288 if (!hdev_is_powered(hdev))
3291 if (mgmt_powering_down(hdev))
3294 if (hdev->scanning_paused)
3297 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3298 disconnected_accept_list_entries(hdev))
3301 scan = SCAN_DISABLED;
3303 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3304 scan |= SCAN_INQUIRY;
3306 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3307 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3310 return hci_write_scan_enable_sync(hdev, scan);
3313 int hci_update_name_sync(struct hci_dev *hdev)
3315 struct hci_cp_write_local_name cp;
3317 memset(&cp, 0, sizeof(cp));
3319 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3321 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3326 /* This function perform powered update HCI command sequence after the HCI init
3327 * sequence which end up resetting all states, the sequence is as follows:
3329 * HCI_SSP_ENABLED(Enable SSP)
3330 * HCI_LE_ENABLED(Enable LE)
3331 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
3333 * Enable Authentication
3334 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3335 * Set Name -> Set EIR)
3336 * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address)
3338 int hci_powered_update_sync(struct hci_dev *hdev)
3342 /* Register the available SMP channels (BR/EDR and LE) only when
3343 * successfully powering on the controller. This late
3344 * registration is required so that LE SMP can clearly decide if
3345 * the public address or static address is used.
3349 err = hci_write_ssp_mode_sync(hdev, 0x01);
3353 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3357 err = hci_powered_update_adv_sync(hdev);
3361 err = hci_write_auth_enable_sync(hdev);
3365 if (lmp_bredr_capable(hdev)) {
3366 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3367 hci_write_fast_connectable_sync(hdev, true);
3369 hci_write_fast_connectable_sync(hdev, false);
3370 hci_update_scan_sync(hdev);
3371 hci_update_class_sync(hdev);
3372 hci_update_name_sync(hdev);
3373 hci_update_eir_sync(hdev);
3376 /* If forcing static address is in use or there is no public
3377 * address use the static address as random address (but skip
3378 * the HCI command if the current random address is already the
3381 * In case BR/EDR has been disabled on a dual-mode controller
3382 * and a static address has been configured, then use that
3383 * address instead of the public BR/EDR address.
3385 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3386 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3387 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3388 if (bacmp(&hdev->static_addr, BDADDR_ANY))
3389 return hci_set_random_addr_sync(hdev,
3390 &hdev->static_addr);
3397 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3398 * (BD_ADDR) for a HCI device from
3399 * a firmware node property.
3400 * @hdev: The HCI device
3402 * Search the firmware node for 'local-bd-address'.
3404 * All-zero BD addresses are rejected, because those could be properties
3405 * that exist in the firmware tables, but were not updated by the firmware. For
3406 * example, the DTS could define 'local-bd-address', with zero BD addresses.
3408 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3410 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3414 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3415 (u8 *)&ba, sizeof(ba));
3416 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3419 bacpy(&hdev->public_addr, &ba);
3422 struct hci_init_stage {
3423 int (*func)(struct hci_dev *hdev);
3426 /* Run init stage NULL terminated function table */
3427 static int hci_init_stage_sync(struct hci_dev *hdev,
3428 const struct hci_init_stage *stage)
3432 for (i = 0; stage[i].func; i++) {
3435 err = stage[i].func(hdev);
3443 /* Read Local Version */
3444 static int hci_read_local_version_sync(struct hci_dev *hdev)
3446 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3447 0, NULL, HCI_CMD_TIMEOUT);
3450 /* Read BD Address */
3451 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3453 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3454 0, NULL, HCI_CMD_TIMEOUT);
3457 #define HCI_INIT(_func) \
3462 static const struct hci_init_stage hci_init0[] = {
3463 /* HCI_OP_READ_LOCAL_VERSION */
3464 HCI_INIT(hci_read_local_version_sync),
3465 /* HCI_OP_READ_BD_ADDR */
3466 HCI_INIT(hci_read_bd_addr_sync),
3470 int hci_reset_sync(struct hci_dev *hdev)
3474 set_bit(HCI_RESET, &hdev->flags);
3476 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3484 static int hci_init0_sync(struct hci_dev *hdev)
3488 bt_dev_dbg(hdev, "");
3491 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3492 err = hci_reset_sync(hdev);
3497 return hci_init_stage_sync(hdev, hci_init0);
3500 static int hci_unconf_init_sync(struct hci_dev *hdev)
3504 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3507 err = hci_init0_sync(hdev);
3511 if (hci_dev_test_flag(hdev, HCI_SETUP))
3512 hci_debugfs_create_basic(hdev);
3517 /* Read Local Supported Features. */
3518 static int hci_read_local_features_sync(struct hci_dev *hdev)
3520 /* Not all AMP controllers support this command */
3521 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
3524 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3525 0, NULL, HCI_CMD_TIMEOUT);
3528 /* BR Controller init stage 1 command sequence */
3529 static const struct hci_init_stage br_init1[] = {
3530 /* HCI_OP_READ_LOCAL_FEATURES */
3531 HCI_INIT(hci_read_local_features_sync),
3532 /* HCI_OP_READ_LOCAL_VERSION */
3533 HCI_INIT(hci_read_local_version_sync),
3534 /* HCI_OP_READ_BD_ADDR */
3535 HCI_INIT(hci_read_bd_addr_sync),
3539 /* Read Local Commands */
3540 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3542 /* All Bluetooth 1.2 and later controllers should support the
3543 * HCI command for reading the local supported commands.
3545 * Unfortunately some controllers indicate Bluetooth 1.2 support,
3546 * but do not have support for this command. If that is the case,
3547 * the driver can quirk the behavior and skip reading the local
3548 * supported commands.
3550 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3551 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3552 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3553 0, NULL, HCI_CMD_TIMEOUT);
3558 /* Read Local AMP Info */
3559 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
3561 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
3562 0, NULL, HCI_CMD_TIMEOUT);
3565 /* Read Data Blk size */
3566 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
3568 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
3569 0, NULL, HCI_CMD_TIMEOUT);
3572 /* Read Flow Control Mode */
3573 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
3575 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
3576 0, NULL, HCI_CMD_TIMEOUT);
3579 /* Read Location Data */
3580 static int hci_read_location_data_sync(struct hci_dev *hdev)
3582 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
3583 0, NULL, HCI_CMD_TIMEOUT);
3586 /* AMP Controller init stage 1 command sequence */
3587 static const struct hci_init_stage amp_init1[] = {
3588 /* HCI_OP_READ_LOCAL_VERSION */
3589 HCI_INIT(hci_read_local_version_sync),
3590 /* HCI_OP_READ_LOCAL_COMMANDS */
3591 HCI_INIT(hci_read_local_cmds_sync),
3592 /* HCI_OP_READ_LOCAL_AMP_INFO */
3593 HCI_INIT(hci_read_local_amp_info_sync),
3594 /* HCI_OP_READ_DATA_BLOCK_SIZE */
3595 HCI_INIT(hci_read_data_block_size_sync),
3596 /* HCI_OP_READ_FLOW_CONTROL_MODE */
3597 HCI_INIT(hci_read_flow_control_mode_sync),
3598 /* HCI_OP_READ_LOCATION_DATA */
3599 HCI_INIT(hci_read_location_data_sync),
3603 static int hci_init1_sync(struct hci_dev *hdev)
3607 bt_dev_dbg(hdev, "");
3610 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3611 err = hci_reset_sync(hdev);
3616 switch (hdev->dev_type) {
3618 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3619 return hci_init_stage_sync(hdev, br_init1);
3621 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3622 return hci_init_stage_sync(hdev, amp_init1);
3624 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
3631 /* AMP Controller init stage 2 command sequence */
3632 static const struct hci_init_stage amp_init2[] = {
3633 /* HCI_OP_READ_LOCAL_FEATURES */
3634 HCI_INIT(hci_read_local_features_sync),
3638 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
3639 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3641 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3642 0, NULL, HCI_CMD_TIMEOUT);
3645 /* Read Class of Device */
3646 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3648 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3649 0, NULL, HCI_CMD_TIMEOUT);
3652 /* Read Local Name */
3653 static int hci_read_local_name_sync(struct hci_dev *hdev)
3655 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3656 0, NULL, HCI_CMD_TIMEOUT);
3659 /* Read Voice Setting */
3660 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3662 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3663 0, NULL, HCI_CMD_TIMEOUT);
3666 /* Read Number of Supported IAC */
3667 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3669 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3670 0, NULL, HCI_CMD_TIMEOUT);
3673 /* Read Current IAC LAP */
3674 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3676 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3677 0, NULL, HCI_CMD_TIMEOUT);
3680 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3681 u8 cond_type, bdaddr_t *bdaddr,
3684 struct hci_cp_set_event_filter cp;
3686 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3689 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3692 memset(&cp, 0, sizeof(cp));
3693 cp.flt_type = flt_type;
3695 if (flt_type != HCI_FLT_CLEAR_ALL) {
3696 cp.cond_type = cond_type;
3697 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3698 cp.addr_conn_flt.auto_accept = auto_accept;
3701 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3702 flt_type == HCI_FLT_CLEAR_ALL ?
3703 sizeof(cp.flt_type) : sizeof(cp), &cp,
3707 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3709 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3712 /* In theory the state machine should not reach here unless
3713 * a hci_set_event_filter_sync() call succeeds, but we do
3714 * the check both for parity and as a future reminder.
3716 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3719 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3723 /* Connection accept timeout ~20 secs */
3724 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3726 __le16 param = cpu_to_le16(0x7d00);
3728 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3729 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
3732 /* BR Controller init stage 2 command sequence */
3733 static const struct hci_init_stage br_init2[] = {
3734 /* HCI_OP_READ_BUFFER_SIZE */
3735 HCI_INIT(hci_read_buffer_size_sync),
3736 /* HCI_OP_READ_CLASS_OF_DEV */
3737 HCI_INIT(hci_read_dev_class_sync),
3738 /* HCI_OP_READ_LOCAL_NAME */
3739 HCI_INIT(hci_read_local_name_sync),
3740 /* HCI_OP_READ_VOICE_SETTING */
3741 HCI_INIT(hci_read_voice_setting_sync),
3742 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
3743 HCI_INIT(hci_read_num_supported_iac_sync),
3744 /* HCI_OP_READ_CURRENT_IAC_LAP */
3745 HCI_INIT(hci_read_current_iac_lap_sync),
3746 /* HCI_OP_SET_EVENT_FLT */
3747 HCI_INIT(hci_clear_event_filter_sync),
3748 /* HCI_OP_WRITE_CA_TIMEOUT */
3749 HCI_INIT(hci_write_ca_timeout_sync),
3753 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3757 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3760 /* When SSP is available, then the host features page
3761 * should also be available as well. However some
3762 * controllers list the max_page as 0 as long as SSP
3763 * has not been enabled. To achieve proper debugging
3764 * output, force the minimum max_page to 1 at least.
3766 hdev->max_page = 0x01;
3768 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3769 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3772 static int hci_write_eir_sync(struct hci_dev *hdev)
3774 struct hci_cp_write_eir cp;
3776 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3779 memset(hdev->eir, 0, sizeof(hdev->eir));
3780 memset(&cp, 0, sizeof(cp));
3782 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3786 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3790 if (!lmp_inq_rssi_capable(hdev) &&
3791 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3794 /* If Extended Inquiry Result events are supported, then
3795 * they are clearly preferred over Inquiry Result with RSSI
3798 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3800 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3801 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3804 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3806 if (!lmp_inq_tx_pwr_capable(hdev))
3809 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3810 0, NULL, HCI_CMD_TIMEOUT);
3813 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3815 struct hci_cp_read_local_ext_features cp;
3817 if (!lmp_ext_feat_capable(hdev))
3820 memset(&cp, 0, sizeof(cp));
3823 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3824 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3827 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3829 return hci_read_local_ext_features_sync(hdev, 0x01);
3832 /* HCI Controller init stage 2 command sequence */
3833 static const struct hci_init_stage hci_init2[] = {
3834 /* HCI_OP_READ_LOCAL_COMMANDS */
3835 HCI_INIT(hci_read_local_cmds_sync),
3836 /* HCI_OP_WRITE_SSP_MODE */
3837 HCI_INIT(hci_write_ssp_mode_1_sync),
3838 /* HCI_OP_WRITE_EIR */
3839 HCI_INIT(hci_write_eir_sync),
3840 /* HCI_OP_WRITE_INQUIRY_MODE */
3841 HCI_INIT(hci_write_inquiry_mode_sync),
3842 /* HCI_OP_READ_INQ_RSP_TX_POWER */
3843 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3844 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3845 HCI_INIT(hci_read_local_ext_features_1_sync),
3846 /* HCI_OP_WRITE_AUTH_ENABLE */
3847 HCI_INIT(hci_write_auth_enable_sync),
3851 /* Read LE Buffer Size */
3852 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3854 /* Use Read LE Buffer Size V2 if supported */
3855 if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3856 return __hci_cmd_sync_status(hdev,
3857 HCI_OP_LE_READ_BUFFER_SIZE_V2,
3858 0, NULL, HCI_CMD_TIMEOUT);
3860 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3861 0, NULL, HCI_CMD_TIMEOUT);
3864 /* Read LE Local Supported Features */
3865 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3867 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3868 0, NULL, HCI_CMD_TIMEOUT);
3871 /* Read LE Supported States */
3872 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3874 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3875 0, NULL, HCI_CMD_TIMEOUT);
3878 /* LE Controller init stage 2 command sequence */
3879 static const struct hci_init_stage le_init2[] = {
3880 /* HCI_OP_LE_READ_LOCAL_FEATURES */
3881 HCI_INIT(hci_le_read_local_features_sync),
3882 /* HCI_OP_LE_READ_BUFFER_SIZE */
3883 HCI_INIT(hci_le_read_buffer_size_sync),
3884 /* HCI_OP_LE_READ_SUPPORTED_STATES */
3885 HCI_INIT(hci_le_read_supported_states_sync),
3889 static int hci_init2_sync(struct hci_dev *hdev)
3893 bt_dev_dbg(hdev, "");
3895 if (hdev->dev_type == HCI_AMP)
3896 return hci_init_stage_sync(hdev, amp_init2);
3898 err = hci_init_stage_sync(hdev, hci_init2);
3902 if (lmp_bredr_capable(hdev)) {
3903 err = hci_init_stage_sync(hdev, br_init2);
3907 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3910 if (lmp_le_capable(hdev)) {
3911 err = hci_init_stage_sync(hdev, le_init2);
3914 /* LE-only controllers have LE implicitly enabled */
3915 if (!lmp_bredr_capable(hdev))
3916 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3922 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3924 /* The second byte is 0xff instead of 0x9f (two reserved bits
3925 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3926 * command otherwise.
3928 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3930 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3931 * any event mask for pre 1.2 devices.
3933 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3936 if (lmp_bredr_capable(hdev)) {
3937 events[4] |= 0x01; /* Flow Specification Complete */
3939 /* Don't set Disconnect Complete and mode change when
3940 * suspended as that would wakeup the host when disconnecting
3943 if (hdev->suspended) {
3948 /* Use a different default for LE-only devices */
3949 memset(events, 0, sizeof(events));
3950 events[1] |= 0x20; /* Command Complete */
3951 events[1] |= 0x40; /* Command Status */
3952 events[1] |= 0x80; /* Hardware Error */
3954 /* If the controller supports the Disconnect command, enable
3955 * the corresponding event. In addition enable packet flow
3956 * control related events.
3958 if (hdev->commands[0] & 0x20) {
3959 /* Don't set Disconnect Complete when suspended as that
3960 * would wakeup the host when disconnecting due to
3963 if (!hdev->suspended)
3964 events[0] |= 0x10; /* Disconnection Complete */
3965 events[2] |= 0x04; /* Number of Completed Packets */
3966 events[3] |= 0x02; /* Data Buffer Overflow */
3969 /* If the controller supports the Read Remote Version
3970 * Information command, enable the corresponding event.
3972 if (hdev->commands[2] & 0x80)
3973 events[1] |= 0x08; /* Read Remote Version Information
3977 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3978 events[0] |= 0x80; /* Encryption Change */
3979 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3983 if (lmp_inq_rssi_capable(hdev) ||
3984 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3985 events[4] |= 0x02; /* Inquiry Result with RSSI */
3987 if (lmp_ext_feat_capable(hdev))
3988 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3990 if (lmp_esco_capable(hdev)) {
3991 events[5] |= 0x08; /* Synchronous Connection Complete */
3992 events[5] |= 0x10; /* Synchronous Connection Changed */
3995 if (lmp_sniffsubr_capable(hdev))
3996 events[5] |= 0x20; /* Sniff Subrating */
3998 if (lmp_pause_enc_capable(hdev))
3999 events[5] |= 0x80; /* Encryption Key Refresh Complete */
4001 if (lmp_ext_inq_capable(hdev))
4002 events[5] |= 0x40; /* Extended Inquiry Result */
4004 if (lmp_no_flush_capable(hdev))
4005 events[7] |= 0x01; /* Enhanced Flush Complete */
4007 if (lmp_lsto_capable(hdev))
4008 events[6] |= 0x80; /* Link Supervision Timeout Changed */
4010 if (lmp_ssp_capable(hdev)) {
4011 events[6] |= 0x01; /* IO Capability Request */
4012 events[6] |= 0x02; /* IO Capability Response */
4013 events[6] |= 0x04; /* User Confirmation Request */
4014 events[6] |= 0x08; /* User Passkey Request */
4015 events[6] |= 0x10; /* Remote OOB Data Request */
4016 events[6] |= 0x20; /* Simple Pairing Complete */
4017 events[7] |= 0x04; /* User Passkey Notification */
4018 events[7] |= 0x08; /* Keypress Notification */
4019 events[7] |= 0x10; /* Remote Host Supported
4020 * Features Notification
4024 if (lmp_le_capable(hdev))
4025 events[7] |= 0x20; /* LE Meta-Event */
4027 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
4028 sizeof(events), events, HCI_CMD_TIMEOUT);
4031 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
4033 struct hci_cp_read_stored_link_key cp;
4035 if (!(hdev->commands[6] & 0x20) ||
4036 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4039 memset(&cp, 0, sizeof(cp));
4040 bacpy(&cp.bdaddr, BDADDR_ANY);
4043 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
4044 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4047 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
4049 struct hci_cp_write_def_link_policy cp;
4050 u16 link_policy = 0;
4052 if (!(hdev->commands[5] & 0x10))
4055 memset(&cp, 0, sizeof(cp));
4057 if (lmp_rswitch_capable(hdev))
4058 link_policy |= HCI_LP_RSWITCH;
4059 if (lmp_hold_capable(hdev))
4060 link_policy |= HCI_LP_HOLD;
4061 if (lmp_sniff_capable(hdev))
4062 link_policy |= HCI_LP_SNIFF;
4063 if (lmp_park_capable(hdev))
4064 link_policy |= HCI_LP_PARK;
4066 cp.policy = cpu_to_le16(link_policy);
4068 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
4069 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4072 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
4074 if (!(hdev->commands[8] & 0x01))
4077 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
4078 0, NULL, HCI_CMD_TIMEOUT);
4081 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
4083 if (!(hdev->commands[18] & 0x04) ||
4084 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4085 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4088 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
4089 0, NULL, HCI_CMD_TIMEOUT);
4092 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
4094 /* Some older Broadcom based Bluetooth 1.2 controllers do not
4095 * support the Read Page Scan Type command. Check support for
4096 * this command in the bit mask of supported commands.
4098 if (!(hdev->commands[13] & 0x01))
4101 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
4102 0, NULL, HCI_CMD_TIMEOUT);
4105 /* Read features beyond page 1 if available */
4106 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
4111 if (!lmp_ext_feat_capable(hdev))
4114 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
4116 err = hci_read_local_ext_features_sync(hdev, page);
4124 /* HCI Controller init stage 3 command sequence */
4125 static const struct hci_init_stage hci_init3[] = {
4126 /* HCI_OP_SET_EVENT_MASK */
4127 HCI_INIT(hci_set_event_mask_sync),
4128 /* HCI_OP_READ_STORED_LINK_KEY */
4129 HCI_INIT(hci_read_stored_link_key_sync),
4130 /* HCI_OP_WRITE_DEF_LINK_POLICY */
4131 HCI_INIT(hci_setup_link_policy_sync),
4132 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
4133 HCI_INIT(hci_read_page_scan_activity_sync),
4134 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
4135 HCI_INIT(hci_read_def_err_data_reporting_sync),
4136 /* HCI_OP_READ_PAGE_SCAN_TYPE */
4137 HCI_INIT(hci_read_page_scan_type_sync),
4138 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
4139 HCI_INIT(hci_read_local_ext_features_all_sync),
4143 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4147 if (!lmp_le_capable(hdev))
4150 memset(events, 0, sizeof(events));
4152 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4153 events[0] |= 0x10; /* LE Long Term Key Request */
4155 /* If controller supports the Connection Parameters Request
4156 * Link Layer Procedure, enable the corresponding event.
4158 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4159 /* LE Remote Connection Parameter Request */
4162 /* If the controller supports the Data Length Extension
4163 * feature, enable the corresponding event.
4165 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4166 events[0] |= 0x40; /* LE Data Length Change */
4168 /* If the controller supports LL Privacy feature or LE Extended Adv,
4169 * enable the corresponding event.
4171 if (use_enhanced_conn_complete(hdev))
4172 events[1] |= 0x02; /* LE Enhanced Connection Complete */
4174 /* If the controller supports Extended Scanner Filter
4175 * Policies, enable the corresponding event.
4177 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4178 events[1] |= 0x04; /* LE Direct Advertising Report */
4180 /* If the controller supports Channel Selection Algorithm #2
4181 * feature, enable the corresponding event.
4183 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4184 events[2] |= 0x08; /* LE Channel Selection Algorithm */
4186 /* If the controller supports the LE Set Scan Enable command,
4187 * enable the corresponding advertising report event.
4189 if (hdev->commands[26] & 0x08)
4190 events[0] |= 0x02; /* LE Advertising Report */
4192 /* If the controller supports the LE Create Connection
4193 * command, enable the corresponding event.
4195 if (hdev->commands[26] & 0x10)
4196 events[0] |= 0x01; /* LE Connection Complete */
4198 /* If the controller supports the LE Connection Update
4199 * command, enable the corresponding event.
4201 if (hdev->commands[27] & 0x04)
4202 events[0] |= 0x04; /* LE Connection Update Complete */
4204 /* If the controller supports the LE Read Remote Used Features
4205 * command, enable the corresponding event.
4207 if (hdev->commands[27] & 0x20)
4208 /* LE Read Remote Used Features Complete */
4211 /* If the controller supports the LE Read Local P-256
4212 * Public Key command, enable the corresponding event.
4214 if (hdev->commands[34] & 0x02)
4215 /* LE Read Local P-256 Public Key Complete */
4218 /* If the controller supports the LE Generate DHKey
4219 * command, enable the corresponding event.
4221 if (hdev->commands[34] & 0x04)
4222 events[1] |= 0x01; /* LE Generate DHKey Complete */
4224 /* If the controller supports the LE Set Default PHY or
4225 * LE Set PHY commands, enable the corresponding event.
4227 if (hdev->commands[35] & (0x20 | 0x40))
4228 events[1] |= 0x08; /* LE PHY Update Complete */
4230 /* If the controller supports LE Set Extended Scan Parameters
4231 * and LE Set Extended Scan Enable commands, enable the
4232 * corresponding event.
4234 if (use_ext_scan(hdev))
4235 events[1] |= 0x10; /* LE Extended Advertising Report */
4237 /* If the controller supports the LE Extended Advertising
4238 * command, enable the corresponding event.
4240 if (ext_adv_capable(hdev))
4241 events[2] |= 0x02; /* LE Advertising Set Terminated */
4243 if (cis_capable(hdev)) {
4244 events[3] |= 0x01; /* LE CIS Established */
4245 if (cis_peripheral_capable(hdev))
4246 events[3] |= 0x02; /* LE CIS Request */
4249 if (bis_capable(hdev)) {
4250 events[1] |= 0x20; /* LE PA Report */
4251 events[1] |= 0x40; /* LE PA Sync Established */
4252 events[3] |= 0x04; /* LE Create BIG Complete */
4253 events[3] |= 0x08; /* LE Terminate BIG Complete */
4254 events[3] |= 0x10; /* LE BIG Sync Established */
4255 events[3] |= 0x20; /* LE BIG Sync Loss */
4256 events[4] |= 0x02; /* LE BIG Info Advertising Report */
4259 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4260 sizeof(events), events, HCI_CMD_TIMEOUT);
4263 /* Read LE Advertising Channel TX Power */
4264 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4266 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4267 /* HCI TS spec forbids mixing of legacy and extended
4268 * advertising commands wherein READ_ADV_TX_POWER is
4269 * also included. So do not call it if extended adv
4270 * is supported otherwise controller will return
4271 * COMMAND_DISALLOWED for extended commands.
4273 return __hci_cmd_sync_status(hdev,
4274 HCI_OP_LE_READ_ADV_TX_POWER,
4275 0, NULL, HCI_CMD_TIMEOUT);
4281 /* Read LE Min/Max Tx Power*/
4282 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4284 if (!(hdev->commands[38] & 0x80) ||
4285 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4288 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4289 0, NULL, HCI_CMD_TIMEOUT);
4292 /* Read LE Accept List Size */
4293 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4295 if (!(hdev->commands[26] & 0x40))
4298 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4299 0, NULL, HCI_CMD_TIMEOUT);
4302 /* Read LE Resolving List Size */
4303 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4305 if (!(hdev->commands[34] & 0x40))
4308 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4309 0, NULL, HCI_CMD_TIMEOUT);
4312 /* Clear LE Resolving List */
4313 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4315 if (!(hdev->commands[34] & 0x20))
4318 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4322 /* Set RPA timeout */
4323 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4325 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4327 if (!(hdev->commands[35] & 0x04) ||
4328 test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4331 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4332 sizeof(timeout), &timeout,
4336 /* Read LE Maximum Data Length */
4337 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4339 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4342 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4346 /* Read LE Suggested Default Data Length */
4347 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4349 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4352 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4356 /* Read LE Number of Supported Advertising Sets */
4357 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4359 if (!ext_adv_capable(hdev))
4362 return __hci_cmd_sync_status(hdev,
4363 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4364 0, NULL, HCI_CMD_TIMEOUT);
4367 /* Write LE Host Supported */
4368 static int hci_set_le_support_sync(struct hci_dev *hdev)
4370 struct hci_cp_write_le_host_supported cp;
4372 /* LE-only devices do not support explicit enablement */
4373 if (!lmp_bredr_capable(hdev))
4376 memset(&cp, 0, sizeof(cp));
4378 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4383 if (cp.le == lmp_host_le_capable(hdev))
4386 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4387 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4390 /* LE Set Host Feature */
4391 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4393 struct hci_cp_le_set_host_feature cp;
4395 if (!cis_capable(hdev))
4398 memset(&cp, 0, sizeof(cp));
4400 /* Connected Isochronous Channels (Host Support) */
4404 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4405 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4408 /* LE Controller init stage 3 command sequence */
4409 static const struct hci_init_stage le_init3[] = {
4410 /* HCI_OP_LE_SET_EVENT_MASK */
4411 HCI_INIT(hci_le_set_event_mask_sync),
4412 /* HCI_OP_LE_READ_ADV_TX_POWER */
4413 HCI_INIT(hci_le_read_adv_tx_power_sync),
4414 /* HCI_OP_LE_READ_TRANSMIT_POWER */
4415 HCI_INIT(hci_le_read_tx_power_sync),
4416 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4417 HCI_INIT(hci_le_read_accept_list_size_sync),
4418 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4419 HCI_INIT(hci_le_clear_accept_list_sync),
4420 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4421 HCI_INIT(hci_le_read_resolv_list_size_sync),
4422 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
4423 HCI_INIT(hci_le_clear_resolv_list_sync),
4424 /* HCI_OP_LE_SET_RPA_TIMEOUT */
4425 HCI_INIT(hci_le_set_rpa_timeout_sync),
4426 /* HCI_OP_LE_READ_MAX_DATA_LEN */
4427 HCI_INIT(hci_le_read_max_data_len_sync),
4428 /* HCI_OP_LE_READ_DEF_DATA_LEN */
4429 HCI_INIT(hci_le_read_def_data_len_sync),
4430 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4431 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4432 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4433 HCI_INIT(hci_set_le_support_sync),
4434 /* HCI_OP_LE_SET_HOST_FEATURE */
4435 HCI_INIT(hci_le_set_host_feature_sync),
4439 static int hci_init3_sync(struct hci_dev *hdev)
4443 bt_dev_dbg(hdev, "");
4445 err = hci_init_stage_sync(hdev, hci_init3);
4449 if (lmp_le_capable(hdev))
4450 return hci_init_stage_sync(hdev, le_init3);
4455 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4457 struct hci_cp_delete_stored_link_key cp;
4459 /* Some Broadcom based Bluetooth controllers do not support the
4460 * Delete Stored Link Key command. They are clearly indicating its
4461 * absence in the bit mask of supported commands.
4463 * Check the supported commands and only if the command is marked
4464 * as supported send it. If not supported assume that the controller
4465 * does not have actual support for stored link keys which makes this
4466 * command redundant anyway.
4468 * Some controllers indicate that they support handling deleting
4469 * stored link keys, but they don't. The quirk lets a driver
4470 * just disable this command.
4472 if (!(hdev->commands[6] & 0x80) ||
4473 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4476 memset(&cp, 0, sizeof(cp));
4477 bacpy(&cp.bdaddr, BDADDR_ANY);
4478 cp.delete_all = 0x01;
4480 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4481 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4484 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4486 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4487 bool changed = false;
4489 /* Set event mask page 2 if the HCI command for it is supported */
4490 if (!(hdev->commands[22] & 0x04))
4493 /* If Connectionless Peripheral Broadcast central role is supported
4494 * enable all necessary events for it.
4496 if (lmp_cpb_central_capable(hdev)) {
4497 events[1] |= 0x40; /* Triggered Clock Capture */
4498 events[1] |= 0x80; /* Synchronization Train Complete */
4499 events[2] |= 0x08; /* Truncated Page Complete */
4500 events[2] |= 0x20; /* CPB Channel Map Change */
4504 /* If Connectionless Peripheral Broadcast peripheral role is supported
4505 * enable all necessary events for it.
4507 if (lmp_cpb_peripheral_capable(hdev)) {
4508 events[2] |= 0x01; /* Synchronization Train Received */
4509 events[2] |= 0x02; /* CPB Receive */
4510 events[2] |= 0x04; /* CPB Timeout */
4511 events[2] |= 0x10; /* Peripheral Page Response Timeout */
4515 /* Enable Authenticated Payload Timeout Expired event if supported */
4516 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4521 /* Some Broadcom based controllers indicate support for Set Event
4522 * Mask Page 2 command, but then actually do not support it. Since
4523 * the default value is all bits set to zero, the command is only
4524 * required if the event mask has to be changed. In case no change
4525 * to the event mask is needed, skip this command.
4530 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4531 sizeof(events), events, HCI_CMD_TIMEOUT);
4534 /* Read local codec list if the HCI command is supported */
4535 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4537 if (hdev->commands[45] & 0x04)
4538 hci_read_supported_codecs_v2(hdev);
4539 else if (hdev->commands[29] & 0x20)
4540 hci_read_supported_codecs(hdev);
4545 /* Read local pairing options if the HCI command is supported */
4546 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4548 if (!(hdev->commands[41] & 0x08))
4551 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4552 0, NULL, HCI_CMD_TIMEOUT);
4555 /* Get MWS transport configuration if the HCI command is supported */
4556 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4558 if (!mws_transport_config_capable(hdev))
4561 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4562 0, NULL, HCI_CMD_TIMEOUT);
4565 /* Check for Synchronization Train support */
4566 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4568 if (!lmp_sync_train_capable(hdev))
4571 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4572 0, NULL, HCI_CMD_TIMEOUT);
4575 /* Enable Secure Connections if supported and configured */
4576 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4580 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4581 !bredr_sc_enabled(hdev))
4584 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4585 sizeof(support), &support,
4589 /* Set erroneous data reporting if supported to the wideband speech
4592 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4594 struct hci_cp_write_def_err_data_reporting cp;
4595 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4597 if (!(hdev->commands[18] & 0x08) ||
4598 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4599 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4602 if (enabled == hdev->err_data_reporting)
4605 memset(&cp, 0, sizeof(cp));
4606 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4607 ERR_DATA_REPORTING_DISABLED;
4609 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4610 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4613 static const struct hci_init_stage hci_init4[] = {
4614 /* HCI_OP_DELETE_STORED_LINK_KEY */
4615 HCI_INIT(hci_delete_stored_link_key_sync),
4616 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4617 HCI_INIT(hci_set_event_mask_page_2_sync),
4618 /* HCI_OP_READ_LOCAL_CODECS */
4619 HCI_INIT(hci_read_local_codecs_sync),
4620 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4621 HCI_INIT(hci_read_local_pairing_opts_sync),
4622 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4623 HCI_INIT(hci_get_mws_transport_config_sync),
4624 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4625 HCI_INIT(hci_read_sync_train_params_sync),
4626 /* HCI_OP_WRITE_SC_SUPPORT */
4627 HCI_INIT(hci_write_sc_support_1_sync),
4628 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4629 HCI_INIT(hci_set_err_data_report_sync),
4633 /* Set Suggested Default Data Length to maximum if supported */
4634 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4636 struct hci_cp_le_write_def_data_len cp;
4638 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4641 memset(&cp, 0, sizeof(cp));
4642 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4643 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4645 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4646 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4649 /* Set Default PHY parameters if command is supported, enables all supported
4650 * PHYs according to the LE Features bits.
4652 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4654 struct hci_cp_le_set_default_phy cp;
4656 if (!(hdev->commands[35] & 0x20)) {
4657 /* If the command is not supported it means only 1M PHY is
4660 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4661 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4665 memset(&cp, 0, sizeof(cp));
4667 cp.tx_phys = HCI_LE_SET_PHY_1M;
4668 cp.rx_phys = HCI_LE_SET_PHY_1M;
4670 /* Enables 2M PHY if supported */
4671 if (le_2m_capable(hdev)) {
4672 cp.tx_phys |= HCI_LE_SET_PHY_2M;
4673 cp.rx_phys |= HCI_LE_SET_PHY_2M;
4676 /* Enables Coded PHY if supported */
4677 if (le_coded_capable(hdev)) {
4678 cp.tx_phys |= HCI_LE_SET_PHY_CODED;
4679 cp.rx_phys |= HCI_LE_SET_PHY_CODED;
4682 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4683 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4686 static const struct hci_init_stage le_init4[] = {
4687 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4688 HCI_INIT(hci_le_set_write_def_data_len_sync),
4689 /* HCI_OP_LE_SET_DEFAULT_PHY */
4690 HCI_INIT(hci_le_set_default_phy_sync),
4694 static int hci_init4_sync(struct hci_dev *hdev)
4698 bt_dev_dbg(hdev, "");
4700 err = hci_init_stage_sync(hdev, hci_init4);
4704 if (lmp_le_capable(hdev))
4705 return hci_init_stage_sync(hdev, le_init4);
4710 static int hci_init_sync(struct hci_dev *hdev)
4714 err = hci_init1_sync(hdev);
4718 if (hci_dev_test_flag(hdev, HCI_SETUP))
4719 hci_debugfs_create_basic(hdev);
4721 err = hci_init2_sync(hdev);
4725 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
4726 * BR/EDR/LE type controllers. AMP controllers only need the
4727 * first two stages of init.
4729 if (hdev->dev_type != HCI_PRIMARY)
4732 err = hci_init3_sync(hdev);
4736 err = hci_init4_sync(hdev);
4740 /* This function is only called when the controller is actually in
4741 * configured state. When the controller is marked as unconfigured,
4742 * this initialization procedure is not run.
4744 * It means that it is possible that a controller runs through its
4745 * setup phase and then discovers missing settings. If that is the
4746 * case, then this function will not be called. It then will only
4747 * be called during the config phase.
4749 * So only when in setup phase or config phase, create the debugfs
4750 * entries and register the SMP channels.
4752 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4753 !hci_dev_test_flag(hdev, HCI_CONFIG))
4756 if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4759 hci_debugfs_create_common(hdev);
4761 if (lmp_bredr_capable(hdev))
4762 hci_debugfs_create_bredr(hdev);
4764 if (lmp_le_capable(hdev))
4765 hci_debugfs_create_le(hdev);
4770 #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
4772 static const struct {
4773 unsigned long quirk;
4775 } hci_broken_table[] = {
4776 HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
4777 "HCI Read Local Supported Commands not supported"),
4778 HCI_QUIRK_BROKEN(STORED_LINK_KEY,
4779 "HCI Delete Stored Link Key command is advertised, "
4780 "but not supported."),
4781 HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
4782 "HCI Read Default Erroneous Data Reporting command is "
4783 "advertised, but not supported."),
4784 HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
4785 "HCI Read Transmit Power Level command is advertised, "
4786 "but not supported."),
4787 HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
4788 "HCI Set Event Filter command not supported."),
4789 HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
4790 "HCI Enhanced Setup Synchronous Connection command is "
4791 "advertised, but not supported."),
4792 HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
4793 "HCI LE Set Random Private Address Timeout command is "
4794 "advertised, but not supported."),
4795 HCI_QUIRK_BROKEN(LE_CODED,
4796 "HCI LE Coded PHY feature bit is set, "
4797 "but its usage is not supported.")
4800 /* This function handles hdev setup stage:
4803 * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
4805 static int hci_dev_setup_sync(struct hci_dev *hdev)
4808 bool invalid_bdaddr;
4811 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4812 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4815 bt_dev_dbg(hdev, "");
4817 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4820 ret = hdev->setup(hdev);
4822 for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
4823 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4824 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4827 /* The transport driver can set the quirk to mark the
4828 * BD_ADDR invalid before creating the HCI device or in
4829 * its setup callback.
4831 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
4832 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
4834 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4835 !bacmp(&hdev->public_addr, BDADDR_ANY))
4836 hci_dev_get_bd_addr_from_property(hdev);
4838 if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
4840 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4842 invalid_bdaddr = false;
4846 /* The transport driver can set these quirks before
4847 * creating the HCI device or in its setup callback.
4849 * For the invalid BD_ADDR quirk it is possible that
4850 * it becomes a valid address if the bootloader does
4851 * provide it (see above).
4853 * In case any of them is set, the controller has to
4854 * start up as unconfigured.
4856 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4858 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4860 /* For an unconfigured controller it is required to
4861 * read at least the version information provided by
4862 * the Read Local Version Information command.
4864 * If the set_bdaddr driver callback is provided, then
4865 * also the original Bluetooth public device address
4866 * will be read using the Read BD Address command.
4868 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4869 return hci_unconf_init_sync(hdev);
4874 /* This function handles hdev init stage:
4876 * Calls hci_dev_setup_sync to perform setup stage
4877 * Calls hci_init_sync to perform HCI command init sequence
4879 static int hci_dev_init_sync(struct hci_dev *hdev)
4883 bt_dev_dbg(hdev, "");
4885 atomic_set(&hdev->cmd_cnt, 1);
4886 set_bit(HCI_INIT, &hdev->flags);
4888 ret = hci_dev_setup_sync(hdev);
4890 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4891 /* If public address change is configured, ensure that
4892 * the address gets programmed. If the driver does not
4893 * support changing the public address, fail the power
4896 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4898 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4900 ret = -EADDRNOTAVAIL;
4904 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4905 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4906 ret = hci_init_sync(hdev);
4907 if (!ret && hdev->post_init)
4908 ret = hdev->post_init(hdev);
4912 /* If the HCI Reset command is clearing all diagnostic settings,
4913 * then they need to be reprogrammed after the init procedure
4916 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4917 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4918 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4919 ret = hdev->set_diag(hdev, true);
4921 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4926 clear_bit(HCI_INIT, &hdev->flags);
4931 int hci_dev_open_sync(struct hci_dev *hdev)
4935 bt_dev_dbg(hdev, "");
4937 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4942 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4943 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4944 /* Check for rfkill but allow the HCI setup stage to
4945 * proceed (which in itself doesn't cause any RF activity).
4947 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4952 /* Check for valid public address or a configured static
4953 * random address, but let the HCI setup proceed to
4954 * be able to determine if there is a public address
4957 * In case of user channel usage, it is not important
4958 * if a public address or static random address is
4961 * This check is only valid for BR/EDR controllers
4962 * since AMP controllers do not have an address.
4964 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4965 hdev->dev_type == HCI_PRIMARY &&
4966 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4967 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4968 ret = -EADDRNOTAVAIL;
4973 if (test_bit(HCI_UP, &hdev->flags)) {
4978 if (hdev->open(hdev)) {
4983 hci_devcd_reset(hdev);
4985 set_bit(HCI_RUNNING, &hdev->flags);
4986 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4988 ret = hci_dev_init_sync(hdev);
4991 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4992 hci_adv_instances_set_rpa_expired(hdev, true);
4993 set_bit(HCI_UP, &hdev->flags);
4994 hci_sock_dev_event(hdev, HCI_DEV_UP);
4995 hci_leds_update_powered(hdev, true);
4996 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4997 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4998 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4999 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5000 hci_dev_test_flag(hdev, HCI_MGMT) &&
5001 hdev->dev_type == HCI_PRIMARY) {
5002 ret = hci_powered_update_sync(hdev);
5003 mgmt_power_on(hdev, ret);
5006 /* Init failed, cleanup */
5007 flush_work(&hdev->tx_work);
5009 /* Since hci_rx_work() is possible to awake new cmd_work
5010 * it should be flushed first to avoid unexpected call of
5013 flush_work(&hdev->rx_work);
5014 flush_work(&hdev->cmd_work);
5016 skb_queue_purge(&hdev->cmd_q);
5017 skb_queue_purge(&hdev->rx_q);
5022 if (hdev->sent_cmd) {
5023 cancel_delayed_work_sync(&hdev->cmd_timer);
5024 kfree_skb(hdev->sent_cmd);
5025 hdev->sent_cmd = NULL;
5028 if (hdev->req_skb) {
5029 kfree_skb(hdev->req_skb);
5030 hdev->req_skb = NULL;
5033 clear_bit(HCI_RUNNING, &hdev->flags);
5034 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5037 hdev->flags &= BIT(HCI_RAW);
5044 /* This function requires the caller holds hdev->lock */
5045 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
5047 struct hci_conn_params *p;
5049 list_for_each_entry(p, &hdev->le_conn_params, list) {
5050 hci_pend_le_list_del_init(p);
5052 hci_conn_drop(p->conn);
5053 hci_conn_put(p->conn);
5058 BT_DBG("All LE pending actions cleared");
5061 static int hci_dev_shutdown(struct hci_dev *hdev)
5064 /* Similar to how we first do setup and then set the exclusive access
5065 * bit for userspace, we must first unset userchannel and then clean up.
5066 * Otherwise, the kernel can't properly use the hci channel to clean up
5067 * the controller (some shutdown routines require sending additional
5068 * commands to the controller for example).
5070 bool was_userchannel =
5071 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
5073 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
5074 test_bit(HCI_UP, &hdev->flags)) {
5075 /* Execute vendor specific shutdown routine */
5077 err = hdev->shutdown(hdev);
5080 if (was_userchannel)
5081 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
5086 int hci_dev_close_sync(struct hci_dev *hdev)
5091 bt_dev_dbg(hdev, "");
5093 cancel_delayed_work(&hdev->power_off);
5094 cancel_delayed_work(&hdev->ncmd_timer);
5095 cancel_delayed_work(&hdev->le_scan_disable);
5097 hci_request_cancel_all(hdev);
5099 if (hdev->adv_instance_timeout) {
5100 cancel_delayed_work_sync(&hdev->adv_instance_expire);
5101 hdev->adv_instance_timeout = 0;
5104 err = hci_dev_shutdown(hdev);
5106 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
5107 cancel_delayed_work_sync(&hdev->cmd_timer);
5111 hci_leds_update_powered(hdev, false);
5113 /* Flush RX and TX works */
5114 flush_work(&hdev->tx_work);
5115 flush_work(&hdev->rx_work);
5117 if (hdev->discov_timeout > 0) {
5118 hdev->discov_timeout = 0;
5119 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5120 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
5123 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
5124 cancel_delayed_work(&hdev->service_cache);
5126 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
5127 struct adv_info *adv_instance;
5129 cancel_delayed_work_sync(&hdev->rpa_expired);
5131 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5132 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
5135 /* Avoid potential lockdep warnings from the *_flush() calls by
5136 * ensuring the workqueue is empty up front.
5138 drain_workqueue(hdev->workqueue);
5142 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5144 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5146 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
5147 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5148 hci_dev_test_flag(hdev, HCI_MGMT))
5149 __mgmt_power_off(hdev);
5151 hci_inquiry_cache_flush(hdev);
5152 hci_pend_le_actions_clear(hdev);
5153 hci_conn_hash_flush(hdev);
5154 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5155 smp_unregister(hdev);
5156 hci_dev_unlock(hdev);
5158 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5160 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5161 aosp_do_close(hdev);
5162 msft_do_close(hdev);
5169 skb_queue_purge(&hdev->cmd_q);
5170 atomic_set(&hdev->cmd_cnt, 1);
5171 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5172 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5173 set_bit(HCI_INIT, &hdev->flags);
5174 hci_reset_sync(hdev);
5175 clear_bit(HCI_INIT, &hdev->flags);
5178 /* flush cmd work */
5179 flush_work(&hdev->cmd_work);
5182 skb_queue_purge(&hdev->rx_q);
5183 skb_queue_purge(&hdev->cmd_q);
5184 skb_queue_purge(&hdev->raw_q);
5186 /* Drop last sent command */
5187 if (hdev->sent_cmd) {
5188 cancel_delayed_work_sync(&hdev->cmd_timer);
5189 kfree_skb(hdev->sent_cmd);
5190 hdev->sent_cmd = NULL;
5193 /* Drop last request */
5194 if (hdev->req_skb) {
5195 kfree_skb(hdev->req_skb);
5196 hdev->req_skb = NULL;
5199 clear_bit(HCI_RUNNING, &hdev->flags);
5200 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5202 /* After this point our queues are empty and no tasks are scheduled. */
5206 hdev->flags &= BIT(HCI_RAW);
5207 hci_dev_clear_volatile_flags(hdev);
5209 /* Controller radio is available but is currently powered down */
5210 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
5212 memset(hdev->eir, 0, sizeof(hdev->eir));
5213 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5214 bacpy(&hdev->random_addr, BDADDR_ANY);
5215 hci_codec_list_clear(&hdev->local_codecs);
5221 /* This function perform power on HCI command sequence as follows:
5223 * If controller is already up (HCI_UP) performs hci_powered_update_sync
5224 * sequence otherwise run hci_dev_open_sync which will follow with
5225 * hci_powered_update_sync after the init sequence is completed.
5227 static int hci_power_on_sync(struct hci_dev *hdev)
5231 if (test_bit(HCI_UP, &hdev->flags) &&
5232 hci_dev_test_flag(hdev, HCI_MGMT) &&
5233 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5234 cancel_delayed_work(&hdev->power_off);
5235 return hci_powered_update_sync(hdev);
5238 err = hci_dev_open_sync(hdev);
5242 /* During the HCI setup phase, a few error conditions are
5243 * ignored and they need to be checked now. If they are still
5244 * valid, it is important to return the device back off.
5246 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5247 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5248 (hdev->dev_type == HCI_PRIMARY &&
5249 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5250 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5251 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5252 hci_dev_close_sync(hdev);
5253 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5254 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5255 HCI_AUTO_OFF_TIMEOUT);
5258 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5259 /* For unconfigured devices, set the HCI_RAW flag
5260 * so that userspace can easily identify them.
5262 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5263 set_bit(HCI_RAW, &hdev->flags);
5265 /* For fully configured devices, this will send
5266 * the Index Added event. For unconfigured devices,
5267 * it will send Unconfigued Index Added event.
5269 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
5270 * and no event will be send.
5272 mgmt_index_added(hdev);
5273 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5274 /* When the controller is now configured, then it
5275 * is important to clear the HCI_RAW flag.
5277 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5278 clear_bit(HCI_RAW, &hdev->flags);
5280 /* Powering on the controller with HCI_CONFIG set only
5281 * happens with the transition from unconfigured to
5282 * configured. This will send the Index Added event.
5284 mgmt_index_added(hdev);
5290 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5292 struct hci_cp_remote_name_req_cancel cp;
5294 memset(&cp, 0, sizeof(cp));
5295 bacpy(&cp.bdaddr, addr);
5297 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5298 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5301 int hci_stop_discovery_sync(struct hci_dev *hdev)
5303 struct discovery_state *d = &hdev->discovery;
5304 struct inquiry_entry *e;
5307 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5309 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
5310 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5311 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5312 0, NULL, HCI_CMD_TIMEOUT);
5317 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5318 cancel_delayed_work(&hdev->le_scan_disable);
5320 err = hci_scan_disable_sync(hdev);
5326 err = hci_scan_disable_sync(hdev);
5331 /* Resume advertising if it was paused */
5332 if (use_ll_privacy(hdev))
5333 hci_resume_advertising_sync(hdev);
5335 /* No further actions needed for LE-only discovery */
5336 if (d->type == DISCOV_TYPE_LE)
5339 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5340 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5345 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5351 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
5354 struct hci_cp_disconn_phy_link cp;
5356 memset(&cp, 0, sizeof(cp));
5357 cp.phy_handle = HCI_PHY_HANDLE(handle);
5360 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
5361 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5364 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5367 struct hci_cp_disconnect cp;
5369 if (conn->type == AMP_LINK)
5370 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
5372 if (test_bit(HCI_CONN_BIG_CREATED, &conn->flags)) {
5373 /* This is a BIS connection, hci_conn_del will
5374 * do the necessary cleanup.
5377 hci_conn_failed(conn, reason);
5378 hci_dev_unlock(hdev);
5383 memset(&cp, 0, sizeof(cp));
5384 cp.handle = cpu_to_le16(conn->handle);
5387 /* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5388 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5389 * used when suspending or powering off, where we don't want to wait
5390 * for the peer's response.
5392 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5393 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5395 HCI_EV_DISCONN_COMPLETE,
5396 HCI_CMD_TIMEOUT, NULL);
5398 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5402 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5403 struct hci_conn *conn, u8 reason)
5405 /* Return reason if scanning since the connection shall probably be
5408 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5411 if (conn->role == HCI_ROLE_SLAVE ||
5412 test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
5415 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5416 0, NULL, HCI_CMD_TIMEOUT);
5419 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5422 if (conn->type == LE_LINK)
5423 return hci_le_connect_cancel_sync(hdev, conn, reason);
5425 if (conn->type == ISO_LINK) {
5426 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
5429 * If this command is issued for a CIS on the Central and the
5430 * CIS is successfully terminated before being established,
5431 * then an HCI_LE_CIS_Established event shall also be sent for
5432 * this CIS with the Status Operation Cancelled by Host (0x44).
5434 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
5435 return hci_disconnect_sync(hdev, conn, reason);
5437 /* CIS with no Create CIS sent have nothing to cancel */
5438 if (bacmp(&conn->dst, BDADDR_ANY))
5439 return HCI_ERROR_LOCAL_HOST_TERM;
5441 /* There is no way to cancel a BIS without terminating the BIG
5442 * which is done later on connection cleanup.
5447 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5450 /* Wait for HCI_EV_CONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5451 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5452 * used when suspending or powering off, where we don't want to wait
5453 * for the peer's response.
5455 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5456 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5458 HCI_EV_CONN_COMPLETE,
5459 HCI_CMD_TIMEOUT, NULL);
5461 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5462 6, &conn->dst, HCI_CMD_TIMEOUT);
5465 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5468 struct hci_cp_reject_sync_conn_req cp;
5470 memset(&cp, 0, sizeof(cp));
5471 bacpy(&cp.bdaddr, &conn->dst);
5474 /* SCO rejection has its own limited set of
5475 * allowed error values (0x0D-0x0F).
5477 if (reason < 0x0d || reason > 0x0f)
5478 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5480 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5481 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5484 static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
5487 struct hci_cp_le_reject_cis cp;
5489 memset(&cp, 0, sizeof(cp));
5490 cp.handle = cpu_to_le16(conn->handle);
5493 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
5494 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5497 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5500 struct hci_cp_reject_conn_req cp;
5502 if (conn->type == ISO_LINK)
5503 return hci_le_reject_cis_sync(hdev, conn, reason);
5505 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5506 return hci_reject_sco_sync(hdev, conn, reason);
5508 memset(&cp, 0, sizeof(cp));
5509 bacpy(&cp.bdaddr, &conn->dst);
5512 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5513 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5516 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5519 u16 handle = conn->handle;
5520 bool disconnect = false;
5523 switch (conn->state) {
5526 err = hci_disconnect_sync(hdev, conn, reason);
5529 err = hci_connect_cancel_sync(hdev, conn, reason);
5532 err = hci_reject_conn_sync(hdev, conn, reason);
5544 /* Check if the connection has been cleaned up concurrently */
5545 c = hci_conn_hash_lookup_handle(hdev, handle);
5546 if (!c || c != conn) {
5551 /* Cleanup hci_conn object if it cannot be cancelled as it
5552 * likelly means the controller and host stack are out of sync
5553 * or in case of LE it was still scanning so it can be cleanup
5557 conn->state = BT_CLOSED;
5558 hci_disconn_cfm(conn, reason);
5561 hci_conn_failed(conn, reason);
5565 hci_dev_unlock(hdev);
5569 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5571 struct list_head *head = &hdev->conn_hash.list;
5572 struct hci_conn *conn;
5575 while ((conn = list_first_or_null_rcu(head, struct hci_conn, list))) {
5576 /* Make sure the connection is not freed while unlocking */
5577 conn = hci_conn_get(conn);
5579 /* Disregard possible errors since hci_conn_del shall have been
5580 * called even in case of errors had occurred since it would
5581 * then cause hci_conn_failed to be called which calls
5582 * hci_conn_del internally.
5584 hci_abort_conn_sync(hdev, conn, reason);
5593 /* This function perform power off HCI command sequence as follows:
5597 * Disconnect all connections
5598 * hci_dev_close_sync
5600 static int hci_power_off_sync(struct hci_dev *hdev)
5604 /* If controller is already down there is nothing to do */
5605 if (!test_bit(HCI_UP, &hdev->flags))
5608 hci_dev_set_flag(hdev, HCI_POWERING_DOWN);
5610 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5611 test_bit(HCI_PSCAN, &hdev->flags)) {
5612 err = hci_write_scan_enable_sync(hdev, 0x00);
5617 err = hci_clear_adv_sync(hdev, NULL, false);
5621 err = hci_stop_discovery_sync(hdev);
5625 /* Terminated due to Power Off */
5626 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5630 err = hci_dev_close_sync(hdev);
5633 hci_dev_clear_flag(hdev, HCI_POWERING_DOWN);
5637 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5640 return hci_power_on_sync(hdev);
5642 return hci_power_off_sync(hdev);
5645 static int hci_write_iac_sync(struct hci_dev *hdev)
5647 struct hci_cp_write_current_iac_lap cp;
5649 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5652 memset(&cp, 0, sizeof(cp));
5654 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5655 /* Limited discoverable mode */
5656 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5657 cp.iac_lap[0] = 0x00; /* LIAC */
5658 cp.iac_lap[1] = 0x8b;
5659 cp.iac_lap[2] = 0x9e;
5660 cp.iac_lap[3] = 0x33; /* GIAC */
5661 cp.iac_lap[4] = 0x8b;
5662 cp.iac_lap[5] = 0x9e;
5664 /* General discoverable mode */
5666 cp.iac_lap[0] = 0x33; /* GIAC */
5667 cp.iac_lap[1] = 0x8b;
5668 cp.iac_lap[2] = 0x9e;
5671 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5672 (cp.num_iac * 3) + 1, &cp,
5676 int hci_update_discoverable_sync(struct hci_dev *hdev)
5680 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5681 err = hci_write_iac_sync(hdev);
5685 err = hci_update_scan_sync(hdev);
5689 err = hci_update_class_sync(hdev);
5694 /* Advertising instances don't use the global discoverable setting, so
5695 * only update AD if advertising was enabled using Set Advertising.
5697 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5698 err = hci_update_adv_data_sync(hdev, 0x00);
5702 /* Discoverable mode affects the local advertising
5703 * address in limited privacy mode.
5705 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5706 if (ext_adv_capable(hdev))
5707 err = hci_start_ext_adv_sync(hdev, 0x00);
5709 err = hci_enable_advertising_sync(hdev);
5716 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5718 return hci_update_discoverable_sync(hdev);
5721 int hci_update_discoverable(struct hci_dev *hdev)
5723 /* Only queue if it would have any effect */
5724 if (hdev_is_powered(hdev) &&
5725 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5726 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5727 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5728 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5734 int hci_update_connectable_sync(struct hci_dev *hdev)
5738 err = hci_update_scan_sync(hdev);
5742 /* If BR/EDR is not enabled and we disable advertising as a
5743 * by-product of disabling connectable, we need to update the
5744 * advertising flags.
5746 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5747 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5749 /* Update the advertising parameters if necessary */
5750 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5751 !list_empty(&hdev->adv_instances)) {
5752 if (ext_adv_capable(hdev))
5753 err = hci_start_ext_adv_sync(hdev,
5754 hdev->cur_adv_instance);
5756 err = hci_enable_advertising_sync(hdev);
5762 return hci_update_passive_scan_sync(hdev);
5765 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5767 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5768 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5769 struct hci_cp_inquiry cp;
5771 bt_dev_dbg(hdev, "");
5773 if (test_bit(HCI_INQUIRY, &hdev->flags))
5777 hci_inquiry_cache_flush(hdev);
5778 hci_dev_unlock(hdev);
5780 memset(&cp, 0, sizeof(cp));
5782 if (hdev->discovery.limited)
5783 memcpy(&cp.lap, liac, sizeof(cp.lap));
5785 memcpy(&cp.lap, giac, sizeof(cp.lap));
5789 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5790 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5793 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5796 /* Accept list is not used for discovery */
5797 u8 filter_policy = 0x00;
5798 /* Default is to enable duplicates filter */
5799 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5802 bt_dev_dbg(hdev, "");
5804 /* If controller is scanning, it means the passive scanning is
5805 * running. Thus, we should temporarily stop it in order to set the
5806 * discovery scanning parameters.
5808 err = hci_scan_disable_sync(hdev);
5810 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5814 cancel_interleave_scan(hdev);
5816 /* Pause address resolution for active scan and stop advertising if
5817 * privacy is enabled.
5819 err = hci_pause_addr_resolution(hdev);
5823 /* All active scans will be done with either a resolvable private
5824 * address (when privacy feature has been enabled) or non-resolvable
5827 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5830 own_addr_type = ADDR_LE_DEV_PUBLIC;
5832 if (hci_is_adv_monitoring(hdev) ||
5833 (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5834 hdev->discovery.result_filtering)) {
5835 /* Duplicate filter should be disabled when some advertisement
5836 * monitor is activated, otherwise AdvMon can only receive one
5837 * advertisement for one peer(*) during active scanning, and
5838 * might report loss to these peers.
5840 * If controller does strict duplicate filtering and the
5841 * discovery requires result filtering disables controller based
5842 * filtering since that can cause reports that would match the
5843 * host filter to not be reported.
5845 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5848 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5849 hdev->le_scan_window_discovery,
5850 own_addr_type, filter_policy, filter_dup);
5855 /* Resume advertising if it was paused */
5856 if (use_ll_privacy(hdev))
5857 hci_resume_advertising_sync(hdev);
5859 /* Resume passive scanning */
5860 hci_update_passive_scan_sync(hdev);
5864 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5868 bt_dev_dbg(hdev, "");
5870 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5874 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5877 int hci_start_discovery_sync(struct hci_dev *hdev)
5879 unsigned long timeout;
5882 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5884 switch (hdev->discovery.type) {
5885 case DISCOV_TYPE_BREDR:
5886 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5887 case DISCOV_TYPE_INTERLEAVED:
5888 /* When running simultaneous discovery, the LE scanning time
5889 * should occupy the whole discovery time sine BR/EDR inquiry
5890 * and LE scanning are scheduled by the controller.
5892 * For interleaving discovery in comparison, BR/EDR inquiry
5893 * and LE scanning are done sequentially with separate
5896 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5898 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5899 /* During simultaneous discovery, we double LE scan
5900 * interval. We must leave some time for the controller
5901 * to do BR/EDR inquiry.
5903 err = hci_start_interleaved_discovery_sync(hdev);
5907 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5908 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5910 case DISCOV_TYPE_LE:
5911 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5912 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5921 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5923 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5928 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5930 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5931 case HCI_ADV_MONITOR_EXT_MSFT:
5932 msft_suspend_sync(hdev);
5939 /* This function disables discovery and mark it as paused */
5940 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5942 int old_state = hdev->discovery.state;
5945 /* If discovery already stopped/stopping/paused there nothing to do */
5946 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
5947 hdev->discovery_paused)
5950 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5951 err = hci_stop_discovery_sync(hdev);
5955 hdev->discovery_paused = true;
5956 hdev->discovery_old_state = old_state;
5957 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5962 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5964 struct bdaddr_list_with_flags *b;
5965 u8 scan = SCAN_DISABLED;
5966 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5969 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5972 /* Some fake CSR controllers lock up after setting this type of
5973 * filter, so avoid sending the request altogether.
5975 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5978 /* Always clear event filter when starting */
5979 hci_clear_event_filter_sync(hdev);
5981 list_for_each_entry(b, &hdev->accept_list, list) {
5982 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
5985 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5987 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5988 HCI_CONN_SETUP_ALLOW_BDADDR,
5990 HCI_CONN_SETUP_AUTO_ON);
5992 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5998 if (scan && !scanning)
5999 hci_write_scan_enable_sync(hdev, scan);
6000 else if (!scan && scanning)
6001 hci_write_scan_enable_sync(hdev, scan);
6006 /* This function disables scan (BR and LE) and mark it as paused */
6007 static int hci_pause_scan_sync(struct hci_dev *hdev)
6009 if (hdev->scanning_paused)
6012 /* Disable page scan if enabled */
6013 if (test_bit(HCI_PSCAN, &hdev->flags))
6014 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
6016 hci_scan_disable_sync(hdev);
6018 hdev->scanning_paused = true;
6023 /* This function performs the HCI suspend procedures in the follow order:
6025 * Pause discovery (active scanning/inquiry)
6026 * Pause Directed Advertising/Advertising
6027 * Pause Scanning (passive scanning in case discovery was not active)
6028 * Disconnect all connections
6029 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
6031 * Update event mask (only set events that are allowed to wake up the host)
6032 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
6033 * Update passive scanning (lower duty cycle)
6034 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
6036 int hci_suspend_sync(struct hci_dev *hdev)
6040 /* If marked as suspended there nothing to do */
6041 if (hdev->suspended)
6044 /* Mark device as suspended */
6045 hdev->suspended = true;
6047 /* Pause discovery if not already stopped */
6048 hci_pause_discovery_sync(hdev);
6050 /* Pause other advertisements */
6051 hci_pause_advertising_sync(hdev);
6053 /* Suspend monitor filters */
6054 hci_suspend_monitor_sync(hdev);
6056 /* Prevent disconnects from causing scanning to be re-enabled */
6057 hci_pause_scan_sync(hdev);
6059 if (hci_conn_count(hdev)) {
6060 /* Soft disconnect everything (power off) */
6061 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
6063 /* Set state to BT_RUNNING so resume doesn't notify */
6064 hdev->suspend_state = BT_RUNNING;
6065 hci_resume_sync(hdev);
6069 /* Update event mask so only the allowed event can wakeup the
6072 hci_set_event_mask_sync(hdev);
6075 /* Only configure accept list if disconnect succeeded and wake
6076 * isn't being prevented.
6078 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
6079 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
6083 /* Unpause to take care of updating scanning params */
6084 hdev->scanning_paused = false;
6086 /* Enable event filter for paired devices */
6087 hci_update_event_filter_sync(hdev);
6089 /* Update LE passive scan if enabled */
6090 hci_update_passive_scan_sync(hdev);
6092 /* Pause scan changes again. */
6093 hdev->scanning_paused = true;
6095 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
6100 /* This function resumes discovery */
6101 static int hci_resume_discovery_sync(struct hci_dev *hdev)
6105 /* If discovery not paused there nothing to do */
6106 if (!hdev->discovery_paused)
6109 hdev->discovery_paused = false;
6111 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6113 err = hci_start_discovery_sync(hdev);
6115 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
6121 static void hci_resume_monitor_sync(struct hci_dev *hdev)
6123 switch (hci_get_adv_monitor_offload_ext(hdev)) {
6124 case HCI_ADV_MONITOR_EXT_MSFT:
6125 msft_resume_sync(hdev);
6132 /* This function resume scan and reset paused flag */
6133 static int hci_resume_scan_sync(struct hci_dev *hdev)
6135 if (!hdev->scanning_paused)
6138 hdev->scanning_paused = false;
6140 hci_update_scan_sync(hdev);
6142 /* Reset passive scanning to normal */
6143 hci_update_passive_scan_sync(hdev);
6148 /* This function performs the HCI suspend procedures in the follow order:
6150 * Restore event mask
6151 * Clear event filter
6152 * Update passive scanning (normal duty cycle)
6153 * Resume Directed Advertising/Advertising
6154 * Resume discovery (active scanning/inquiry)
6156 int hci_resume_sync(struct hci_dev *hdev)
6158 /* If not marked as suspended there nothing to do */
6159 if (!hdev->suspended)
6162 hdev->suspended = false;
6164 /* Restore event mask */
6165 hci_set_event_mask_sync(hdev);
6167 /* Clear any event filters and restore scan state */
6168 hci_clear_event_filter_sync(hdev);
6170 /* Resume scanning */
6171 hci_resume_scan_sync(hdev);
6173 /* Resume monitor filters */
6174 hci_resume_monitor_sync(hdev);
6176 /* Resume other advertisements */
6177 hci_resume_advertising_sync(hdev);
6179 /* Resume discovery */
6180 hci_resume_discovery_sync(hdev);
6185 static bool conn_use_rpa(struct hci_conn *conn)
6187 struct hci_dev *hdev = conn->hdev;
6189 return hci_dev_test_flag(hdev, HCI_PRIVACY);
6192 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
6193 struct hci_conn *conn)
6195 struct hci_cp_le_set_ext_adv_params cp;
6197 bdaddr_t random_addr;
6200 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6205 /* Set require_privacy to false so that the remote device has a
6206 * chance of identifying us.
6208 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
6209 &own_addr_type, &random_addr);
6213 memset(&cp, 0, sizeof(cp));
6215 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
6216 cp.channel_map = hdev->le_adv_channel_map;
6217 cp.tx_power = HCI_TX_POWER_INVALID;
6218 cp.primary_phy = HCI_ADV_PHY_1M;
6219 cp.secondary_phy = HCI_ADV_PHY_1M;
6220 cp.handle = 0x00; /* Use instance 0 for directed adv */
6221 cp.own_addr_type = own_addr_type;
6222 cp.peer_addr_type = conn->dst_type;
6223 bacpy(&cp.peer_addr, &conn->dst);
6225 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
6226 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
6227 * does not supports advertising data when the advertising set already
6228 * contains some, the controller shall return erroc code 'Invalid
6229 * HCI Command Parameters(0x12).
6230 * So it is required to remove adv set for handle 0x00. since we use
6231 * instance 0 for directed adv.
6233 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
6237 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
6238 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6242 /* Check if random address need to be updated */
6243 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
6244 bacmp(&random_addr, BDADDR_ANY) &&
6245 bacmp(&random_addr, &hdev->random_addr)) {
6246 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
6252 return hci_enable_ext_advertising_sync(hdev, 0x00);
6255 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
6256 struct hci_conn *conn)
6258 struct hci_cp_le_set_adv_param cp;
6263 if (ext_adv_capable(hdev))
6264 return hci_le_ext_directed_advertising_sync(hdev, conn);
6266 /* Clear the HCI_LE_ADV bit temporarily so that the
6267 * hci_update_random_address knows that it's safe to go ahead
6268 * and write a new random address. The flag will be set back on
6269 * as soon as the SET_ADV_ENABLE HCI command completes.
6271 hci_dev_clear_flag(hdev, HCI_LE_ADV);
6273 /* Set require_privacy to false so that the remote device has a
6274 * chance of identifying us.
6276 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6281 memset(&cp, 0, sizeof(cp));
6283 /* Some controllers might reject command if intervals are not
6284 * within range for undirected advertising.
6285 * BCM20702A0 is known to be affected by this.
6287 cp.min_interval = cpu_to_le16(0x0020);
6288 cp.max_interval = cpu_to_le16(0x0020);
6290 cp.type = LE_ADV_DIRECT_IND;
6291 cp.own_address_type = own_addr_type;
6292 cp.direct_addr_type = conn->dst_type;
6293 bacpy(&cp.direct_addr, &conn->dst);
6294 cp.channel_map = hdev->le_adv_channel_map;
6296 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
6297 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6303 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
6304 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
6307 static void set_ext_conn_params(struct hci_conn *conn,
6308 struct hci_cp_le_ext_conn_param *p)
6310 struct hci_dev *hdev = conn->hdev;
6312 memset(p, 0, sizeof(*p));
6314 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6315 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6316 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6317 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6318 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
6319 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6320 p->min_ce_len = cpu_to_le16(0x0000);
6321 p->max_ce_len = cpu_to_le16(0x0000);
6324 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
6325 struct hci_conn *conn, u8 own_addr_type)
6327 struct hci_cp_le_ext_create_conn *cp;
6328 struct hci_cp_le_ext_conn_param *p;
6329 u8 data[sizeof(*cp) + sizeof(*p) * 3];
6333 p = (void *)cp->data;
6335 memset(cp, 0, sizeof(*cp));
6337 bacpy(&cp->peer_addr, &conn->dst);
6338 cp->peer_addr_type = conn->dst_type;
6339 cp->own_addr_type = own_addr_type;
6343 if (scan_1m(hdev)) {
6344 cp->phys |= LE_SCAN_PHY_1M;
6345 set_ext_conn_params(conn, p);
6351 if (scan_2m(hdev)) {
6352 cp->phys |= LE_SCAN_PHY_2M;
6353 set_ext_conn_params(conn, p);
6359 if (scan_coded(hdev)) {
6360 cp->phys |= LE_SCAN_PHY_CODED;
6361 set_ext_conn_params(conn, p);
6366 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6368 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
6369 conn->conn_timeout, NULL);
6372 static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
6374 struct hci_cp_le_create_conn cp;
6375 struct hci_conn_params *params;
6378 struct hci_conn *conn = data;
6380 if (!hci_conn_valid(hdev, conn))
6383 bt_dev_dbg(hdev, "conn %p", conn);
6385 clear_bit(HCI_CONN_SCANNING, &conn->flags);
6386 conn->state = BT_CONNECT;
6388 /* If requested to connect as peripheral use directed advertising */
6389 if (conn->role == HCI_ROLE_SLAVE) {
6390 /* If we're active scanning and simultaneous roles is not
6391 * enabled simply reject the attempt.
6393 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6394 hdev->le_scan_type == LE_SCAN_ACTIVE &&
6395 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6400 /* Pause advertising while doing directed advertising. */
6401 hci_pause_advertising_sync(hdev);
6403 err = hci_le_directed_advertising_sync(hdev, conn);
6407 /* Disable advertising if simultaneous roles is not in use. */
6408 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6409 hci_pause_advertising_sync(hdev);
6411 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6413 conn->le_conn_min_interval = params->conn_min_interval;
6414 conn->le_conn_max_interval = params->conn_max_interval;
6415 conn->le_conn_latency = params->conn_latency;
6416 conn->le_supv_timeout = params->supervision_timeout;
6418 conn->le_conn_min_interval = hdev->le_conn_min_interval;
6419 conn->le_conn_max_interval = hdev->le_conn_max_interval;
6420 conn->le_conn_latency = hdev->le_conn_latency;
6421 conn->le_supv_timeout = hdev->le_supv_timeout;
6424 /* If controller is scanning, we stop it since some controllers are
6425 * not able to scan and connect at the same time. Also set the
6426 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
6427 * handler for scan disabling knows to set the correct discovery
6430 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6431 hci_scan_disable_sync(hdev);
6432 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6435 /* Update random address, but set require_privacy to false so
6436 * that we never connect with an non-resolvable address.
6438 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6443 if (use_ext_conn(hdev)) {
6444 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6448 memset(&cp, 0, sizeof(cp));
6450 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6451 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6453 bacpy(&cp.peer_addr, &conn->dst);
6454 cp.peer_addr_type = conn->dst_type;
6455 cp.own_address_type = own_addr_type;
6456 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6457 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6458 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
6459 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6460 cp.min_ce_len = cpu_to_le16(0x0000);
6461 cp.max_ce_len = cpu_to_le16(0x0000);
6463 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6465 * If this event is unmasked and the HCI_LE_Connection_Complete event
6466 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6467 * sent when a new connection has been created.
6469 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6471 use_enhanced_conn_complete(hdev) ?
6472 HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6473 HCI_EV_LE_CONN_COMPLETE,
6474 conn->conn_timeout, NULL);
6477 if (err == -ETIMEDOUT)
6478 hci_le_connect_cancel_sync(hdev, conn, 0x00);
6480 /* Re-enable advertising after the connection attempt is finished. */
6481 hci_resume_advertising_sync(hdev);
6485 int hci_le_create_cis_sync(struct hci_dev *hdev)
6488 struct hci_cp_le_create_cis cp;
6489 struct hci_cis cis[0x1f];
6491 struct hci_conn *conn;
6492 u8 cig = BT_ISO_QOS_CIG_UNSET;
6494 /* The spec allows only one pending LE Create CIS command at a time. If
6495 * the command is pending now, don't do anything. We check for pending
6496 * connections after each CIS Established event.
6498 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6501 * If the Host issues this command before all the
6502 * HCI_LE_CIS_Established events from the previous use of the
6503 * command have been generated, the Controller shall return the
6504 * error code Command Disallowed (0x0C).
6506 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6509 * When the Controller receives the HCI_LE_Create_CIS command, the
6510 * Controller sends the HCI_Command_Status event to the Host. An
6511 * HCI_LE_CIS_Established event will be generated for each CIS when it
6512 * is established or if it is disconnected or considered lost before
6513 * being established; until all the events are generated, the command
6517 memset(&cmd, 0, sizeof(cmd));
6523 /* Wait until previous Create CIS has completed */
6524 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6525 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
6529 /* Find CIG with all CIS ready */
6530 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6531 struct hci_conn *link;
6533 if (hci_conn_check_create_cis(conn))
6536 cig = conn->iso_qos.ucast.cig;
6538 list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
6539 if (hci_conn_check_create_cis(link) > 0 &&
6540 link->iso_qos.ucast.cig == cig &&
6541 link->state != BT_CONNECTED) {
6542 cig = BT_ISO_QOS_CIG_UNSET;
6547 if (cig != BT_ISO_QOS_CIG_UNSET)
6551 if (cig == BT_ISO_QOS_CIG_UNSET)
6554 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6555 struct hci_cis *cis = &cmd.cis[cmd.cp.num_cis];
6557 if (hci_conn_check_create_cis(conn) ||
6558 conn->iso_qos.ucast.cig != cig)
6561 set_bit(HCI_CONN_CREATE_CIS, &conn->flags);
6562 cis->acl_handle = cpu_to_le16(conn->parent->handle);
6563 cis->cis_handle = cpu_to_le16(conn->handle);
6566 if (cmd.cp.num_cis >= ARRAY_SIZE(cmd.cis))
6573 hci_dev_unlock(hdev);
6575 if (!cmd.cp.num_cis)
6578 /* Wait for HCI_LE_CIS_Established */
6579 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6580 sizeof(cmd.cp) + sizeof(cmd.cis[0]) *
6581 cmd.cp.num_cis, &cmd,
6582 HCI_EVT_LE_CIS_ESTABLISHED,
6583 conn->conn_timeout, NULL);
6586 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6588 struct hci_cp_le_remove_cig cp;
6590 memset(&cp, 0, sizeof(cp));
6593 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6594 &cp, HCI_CMD_TIMEOUT);
6597 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6599 struct hci_cp_le_big_term_sync cp;
6601 memset(&cp, 0, sizeof(cp));
6604 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6605 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6608 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6610 struct hci_cp_le_pa_term_sync cp;
6612 memset(&cp, 0, sizeof(cp));
6613 cp.handle = cpu_to_le16(handle);
6615 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6616 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6619 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6620 bool use_rpa, struct adv_info *adv_instance,
6621 u8 *own_addr_type, bdaddr_t *rand_addr)
6625 bacpy(rand_addr, BDADDR_ANY);
6627 /* If privacy is enabled use a resolvable private address. If
6628 * current RPA has expired then generate a new one.
6631 /* If Controller supports LL Privacy use own address type is
6634 if (use_ll_privacy(hdev))
6635 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
6637 *own_addr_type = ADDR_LE_DEV_RANDOM;
6640 if (adv_rpa_valid(adv_instance))
6643 if (rpa_valid(hdev))
6647 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6649 bt_dev_err(hdev, "failed to generate new RPA");
6653 bacpy(rand_addr, &hdev->rpa);
6658 /* In case of required privacy without resolvable private address,
6659 * use an non-resolvable private address. This is useful for
6660 * non-connectable advertising.
6662 if (require_privacy) {
6666 /* The non-resolvable private address is generated
6667 * from random six bytes with the two most significant
6670 get_random_bytes(&nrpa, 6);
6673 /* The non-resolvable private address shall not be
6674 * equal to the public address.
6676 if (bacmp(&hdev->bdaddr, &nrpa))
6680 *own_addr_type = ADDR_LE_DEV_RANDOM;
6681 bacpy(rand_addr, &nrpa);
6686 /* No privacy so use a public address. */
6687 *own_addr_type = ADDR_LE_DEV_PUBLIC;
6692 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6694 u8 instance = PTR_UINT(data);
6696 return hci_update_adv_data_sync(hdev, instance);
6699 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6701 return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6702 UINT_PTR(instance), NULL);
6705 static int hci_acl_create_conn_sync(struct hci_dev *hdev, void *data)
6707 struct hci_conn *conn = data;
6708 struct inquiry_entry *ie;
6709 struct hci_cp_create_conn cp;
6712 if (!hci_conn_valid(hdev, conn))
6715 /* Many controllers disallow HCI Create Connection while it is doing
6716 * HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create
6717 * Connection. This may cause the MGMT discovering state to become false
6718 * without user space's request but it is okay since the MGMT Discovery
6719 * APIs do not promise that discovery should be done forever. Instead,
6720 * the user space monitors the status of MGMT discovering and it may
6721 * request for discovery again when this flag becomes false.
6723 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
6724 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 0,
6725 NULL, HCI_CMD_TIMEOUT);
6727 bt_dev_warn(hdev, "Failed to cancel inquiry %d", err);
6730 conn->state = BT_CONNECT;
6732 conn->role = HCI_ROLE_MASTER;
6736 conn->link_policy = hdev->link_policy;
6738 memset(&cp, 0, sizeof(cp));
6739 bacpy(&cp.bdaddr, &conn->dst);
6740 cp.pscan_rep_mode = 0x02;
6742 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
6744 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
6745 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
6746 cp.pscan_mode = ie->data.pscan_mode;
6747 cp.clock_offset = ie->data.clock_offset |
6748 cpu_to_le16(0x8000);
6751 memcpy(conn->dev_class, ie->data.dev_class, 3);
6754 cp.pkt_type = cpu_to_le16(conn->pkt_type);
6755 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
6756 cp.role_switch = 0x01;
6758 cp.role_switch = 0x00;
6760 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN,
6762 HCI_EV_CONN_COMPLETE,
6763 conn->conn_timeout, NULL);
6766 int hci_connect_acl_sync(struct hci_dev *hdev, struct hci_conn *conn)
6768 return hci_cmd_sync_queue_once(hdev, hci_acl_create_conn_sync, conn,
6772 static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err)
6774 struct hci_conn *conn = data;
6776 bt_dev_dbg(hdev, "err %d", err);
6778 if (err == -ECANCELED)
6783 if (!hci_conn_valid(hdev, conn))
6787 hci_connect_le_scan_cleanup(conn, 0x00);
6791 /* Check if connection is still pending */
6792 if (conn != hci_lookup_le_connect(hdev))
6795 /* Flush to make sure we send create conn cancel command if needed */
6796 flush_delayed_work(&conn->le_conn_timeout);
6797 hci_conn_failed(conn, bt_status(err));
6800 hci_dev_unlock(hdev);
6803 int hci_connect_le_sync(struct hci_dev *hdev, struct hci_conn *conn)
6805 return hci_cmd_sync_queue_once(hdev, hci_le_create_conn_sync, conn,
6806 create_le_conn_complete);
6809 int hci_cancel_connect_sync(struct hci_dev *hdev, struct hci_conn *conn)
6811 if (conn->state != BT_OPEN)
6814 switch (conn->type) {
6816 return !hci_cmd_sync_dequeue_once(hdev,
6817 hci_acl_create_conn_sync,
6820 return !hci_cmd_sync_dequeue_once(hdev, hci_le_create_conn_sync,
6821 conn, create_le_conn_complete);