1 // SPDX-License-Identifier: GPL-2.0
3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2021 Intel Corporation
8 #include <linux/property.h>
10 #include <net/bluetooth/bluetooth.h>
11 #include <net/bluetooth/hci_core.h>
12 #include <net/bluetooth/mgmt.h>
14 #include "hci_request.h"
15 #include "hci_debugfs.h"
22 static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
25 bt_dev_dbg(hdev, "result 0x%2.2x", result);
27 if (hdev->req_status != HCI_REQ_PEND)
30 hdev->req_result = result;
31 hdev->req_status = HCI_REQ_DONE;
34 struct sock *sk = hci_skb_sk(skb);
36 /* Drop sk reference if set */
40 hdev->req_skb = skb_get(skb);
43 wake_up_interruptible(&hdev->req_wait_q);
46 static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
47 u32 plen, const void *param,
50 int len = HCI_COMMAND_HDR_SIZE + plen;
51 struct hci_command_hdr *hdr;
54 skb = bt_skb_alloc(len, GFP_ATOMIC);
58 hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
59 hdr->opcode = cpu_to_le16(opcode);
63 skb_put_data(skb, param, plen);
65 bt_dev_dbg(hdev, "skb len %d", skb->len);
67 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
68 hci_skb_opcode(skb) = opcode;
70 /* Grab a reference if command needs to be associated with a sock (e.g.
71 * likely mgmt socket that initiated the command).
81 static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
82 const void *param, u8 event, struct sock *sk)
84 struct hci_dev *hdev = req->hdev;
87 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
89 /* If an error occurred during request building, there is no point in
90 * queueing the HCI command. We can simply return.
95 skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
97 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
103 if (skb_queue_empty(&req->cmd_q))
104 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
106 hci_skb_event(skb) = event;
108 skb_queue_tail(&req->cmd_q, skb);
111 static int hci_cmd_sync_run(struct hci_request *req)
113 struct hci_dev *hdev = req->hdev;
117 bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
119 /* If an error occurred during request building, remove all HCI
120 * commands queued on the HCI request queue.
123 skb_queue_purge(&req->cmd_q);
127 /* Do not allow empty requests */
128 if (skb_queue_empty(&req->cmd_q))
131 skb = skb_peek_tail(&req->cmd_q);
132 bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
133 bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
135 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
136 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
137 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
139 queue_work(hdev->workqueue, &hdev->cmd_work);
144 /* This function requires the caller holds hdev->req_lock. */
145 struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
146 const void *param, u8 event, u32 timeout,
149 struct hci_request req;
153 bt_dev_dbg(hdev, "Opcode 0x%4x", opcode);
155 hci_req_init(&req, hdev);
157 hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
159 hdev->req_status = HCI_REQ_PEND;
161 err = hci_cmd_sync_run(&req);
165 err = wait_event_interruptible_timeout(hdev->req_wait_q,
166 hdev->req_status != HCI_REQ_PEND,
169 if (err == -ERESTARTSYS)
170 return ERR_PTR(-EINTR);
172 switch (hdev->req_status) {
174 err = -bt_to_errno(hdev->req_result);
177 case HCI_REQ_CANCELED:
178 err = -hdev->req_result;
186 hdev->req_status = 0;
187 hdev->req_result = 0;
189 hdev->req_skb = NULL;
191 bt_dev_dbg(hdev, "end: err %d", err);
200 EXPORT_SYMBOL(__hci_cmd_sync_sk);
202 /* This function requires the caller holds hdev->req_lock. */
203 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
204 const void *param, u32 timeout)
206 return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
208 EXPORT_SYMBOL(__hci_cmd_sync);
210 /* Send HCI command and wait for command complete event */
211 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
212 const void *param, u32 timeout)
216 if (!test_bit(HCI_UP, &hdev->flags))
217 return ERR_PTR(-ENETDOWN);
219 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
221 hci_req_sync_lock(hdev);
222 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
223 hci_req_sync_unlock(hdev);
227 EXPORT_SYMBOL(hci_cmd_sync);
229 /* This function requires the caller holds hdev->req_lock. */
230 struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
231 const void *param, u8 event, u32 timeout)
233 return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
236 EXPORT_SYMBOL(__hci_cmd_sync_ev);
238 /* This function requires the caller holds hdev->req_lock. */
239 int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
240 const void *param, u8 event, u32 timeout,
246 skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
248 bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode,
253 /* If command return a status event skb will be set to NULL as there are
254 * no parameters, in case of failure IS_ERR(skb) would have be set to
255 * the actual error would be found with PTR_ERR(skb).
260 status = skb->data[0];
266 EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
268 int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
269 const void *param, u32 timeout)
271 return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
274 EXPORT_SYMBOL(__hci_cmd_sync_status);
276 static void hci_cmd_sync_work(struct work_struct *work)
278 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
280 bt_dev_dbg(hdev, "");
282 /* Dequeue all entries and run them */
284 struct hci_cmd_sync_work_entry *entry;
286 mutex_lock(&hdev->cmd_sync_work_lock);
287 entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
288 struct hci_cmd_sync_work_entry,
291 list_del(&entry->list);
292 mutex_unlock(&hdev->cmd_sync_work_lock);
297 bt_dev_dbg(hdev, "entry %p", entry);
302 hci_req_sync_lock(hdev);
303 err = entry->func(hdev, entry->data);
305 entry->destroy(hdev, entry->data, err);
306 hci_req_sync_unlock(hdev);
313 static void hci_cmd_sync_cancel_work(struct work_struct *work)
315 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
317 cancel_delayed_work_sync(&hdev->cmd_timer);
318 cancel_delayed_work_sync(&hdev->ncmd_timer);
319 atomic_set(&hdev->cmd_cnt, 1);
321 wake_up_interruptible(&hdev->req_wait_q);
324 void hci_cmd_sync_init(struct hci_dev *hdev)
326 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
327 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
328 mutex_init(&hdev->cmd_sync_work_lock);
330 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
333 void hci_cmd_sync_clear(struct hci_dev *hdev)
335 struct hci_cmd_sync_work_entry *entry, *tmp;
337 cancel_work_sync(&hdev->cmd_sync_work);
339 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
341 entry->destroy(hdev, entry->data, -ECANCELED);
343 list_del(&entry->list);
348 void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
350 bt_dev_dbg(hdev, "err 0x%2.2x", err);
352 if (hdev->req_status == HCI_REQ_PEND) {
353 hdev->req_result = err;
354 hdev->req_status = HCI_REQ_CANCELED;
356 cancel_delayed_work_sync(&hdev->cmd_timer);
357 cancel_delayed_work_sync(&hdev->ncmd_timer);
358 atomic_set(&hdev->cmd_cnt, 1);
360 wake_up_interruptible(&hdev->req_wait_q);
364 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
366 bt_dev_dbg(hdev, "err 0x%2.2x", err);
368 if (hdev->req_status == HCI_REQ_PEND) {
369 hdev->req_result = err;
370 hdev->req_status = HCI_REQ_CANCELED;
372 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
375 EXPORT_SYMBOL(hci_cmd_sync_cancel);
377 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
378 void *data, hci_cmd_sync_work_destroy_t destroy)
380 struct hci_cmd_sync_work_entry *entry;
382 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
385 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
391 entry->destroy = destroy;
393 mutex_lock(&hdev->cmd_sync_work_lock);
394 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
395 mutex_unlock(&hdev->cmd_sync_work_lock);
397 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
401 EXPORT_SYMBOL(hci_cmd_sync_queue);
403 int hci_update_eir_sync(struct hci_dev *hdev)
405 struct hci_cp_write_eir cp;
407 bt_dev_dbg(hdev, "");
409 if (!hdev_is_powered(hdev))
412 if (!lmp_ext_inq_capable(hdev))
415 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
418 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
421 memset(&cp, 0, sizeof(cp));
423 eir_create(hdev, cp.data);
425 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
428 memcpy(hdev->eir, cp.data, sizeof(cp.data));
430 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
434 static u8 get_service_classes(struct hci_dev *hdev)
436 struct bt_uuid *uuid;
439 list_for_each_entry(uuid, &hdev->uuids, list)
440 val |= uuid->svc_hint;
445 int hci_update_class_sync(struct hci_dev *hdev)
449 bt_dev_dbg(hdev, "");
451 if (!hdev_is_powered(hdev))
454 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
457 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
460 cod[0] = hdev->minor_class;
461 cod[1] = hdev->major_class;
462 cod[2] = get_service_classes(hdev);
464 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
467 if (memcmp(cod, hdev->dev_class, 3) == 0)
470 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
471 sizeof(cod), cod, HCI_CMD_TIMEOUT);
474 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
476 /* If there is no connection we are OK to advertise. */
477 if (hci_conn_num(hdev, LE_LINK) == 0)
480 /* Check le_states if there is any connection in peripheral role. */
481 if (hdev->conn_hash.le_num_peripheral > 0) {
482 /* Peripheral connection state and non connectable mode
485 if (!connectable && !(hdev->le_states[2] & 0x10))
488 /* Peripheral connection state and connectable mode bit 38
489 * and scannable bit 21.
491 if (connectable && (!(hdev->le_states[4] & 0x40) ||
492 !(hdev->le_states[2] & 0x20)))
496 /* Check le_states if there is any connection in central role. */
497 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
498 /* Central connection state and non connectable mode bit 18. */
499 if (!connectable && !(hdev->le_states[2] & 0x02))
502 /* Central connection state and connectable mode bit 35 and
505 if (connectable && (!(hdev->le_states[4] & 0x08) ||
506 !(hdev->le_states[2] & 0x08)))
513 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
515 /* If privacy is not enabled don't use RPA */
516 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
519 /* If basic privacy mode is enabled use RPA */
520 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
523 /* If limited privacy mode is enabled don't use RPA if we're
524 * both discoverable and bondable.
526 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
527 hci_dev_test_flag(hdev, HCI_BONDABLE))
530 /* We're neither bondable nor discoverable in the limited
531 * privacy mode, therefore use RPA.
536 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
538 /* If we're advertising or initiating an LE connection we can't
539 * go ahead and change the random address at this time. This is
540 * because the eventual initiator address used for the
541 * subsequently created connection will be undefined (some
542 * controllers use the new address and others the one we had
543 * when the operation started).
545 * In this kind of scenario skip the update and let the random
546 * address be updated at the next cycle.
548 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
549 hci_lookup_le_connect(hdev)) {
550 bt_dev_dbg(hdev, "Deferring random address update");
551 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
555 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
556 6, rpa, HCI_CMD_TIMEOUT);
559 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
560 bool rpa, u8 *own_addr_type)
564 /* If privacy is enabled use a resolvable private address. If
565 * current RPA has expired or there is something else than
566 * the current RPA in use, then generate a new one.
569 /* If Controller supports LL Privacy use own address type is
572 if (use_ll_privacy(hdev))
573 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
575 *own_addr_type = ADDR_LE_DEV_RANDOM;
577 /* Check if RPA is valid */
581 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
583 bt_dev_err(hdev, "failed to generate new RPA");
587 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
594 /* In case of required privacy without resolvable private address,
595 * use an non-resolvable private address. This is useful for active
596 * scanning and non-connectable advertising.
598 if (require_privacy) {
602 /* The non-resolvable private address is generated
603 * from random six bytes with the two most significant
606 get_random_bytes(&nrpa, 6);
609 /* The non-resolvable private address shall not be
610 * equal to the public address.
612 if (bacmp(&hdev->bdaddr, &nrpa))
616 *own_addr_type = ADDR_LE_DEV_RANDOM;
618 return hci_set_random_addr_sync(hdev, &nrpa);
621 /* If forcing static address is in use or there is no public
622 * address use the static address as random address (but skip
623 * the HCI command if the current random address is already the
626 * In case BR/EDR has been disabled on a dual-mode controller
627 * and a static address has been configured, then use that
628 * address instead of the public BR/EDR address.
630 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
631 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
632 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
633 bacmp(&hdev->static_addr, BDADDR_ANY))) {
634 *own_addr_type = ADDR_LE_DEV_RANDOM;
635 if (bacmp(&hdev->static_addr, &hdev->random_addr))
636 return hci_set_random_addr_sync(hdev,
641 /* Neither privacy nor static address is being used so use a
644 *own_addr_type = ADDR_LE_DEV_PUBLIC;
649 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
651 struct hci_cp_le_set_ext_adv_enable *cp;
652 struct hci_cp_ext_adv_set *set;
653 u8 data[sizeof(*cp) + sizeof(*set) * 1];
656 /* If request specifies an instance that doesn't exist, fail */
658 struct adv_info *adv;
660 adv = hci_find_adv_instance(hdev, instance);
664 /* If not enabled there is nothing to do */
669 memset(data, 0, sizeof(data));
672 set = (void *)cp->data;
674 /* Instance 0x00 indicates all advertising instances will be disabled */
675 cp->num_of_sets = !!instance;
678 set->handle = instance;
680 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
682 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
683 size, data, HCI_CMD_TIMEOUT);
686 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
687 bdaddr_t *random_addr)
689 struct hci_cp_le_set_adv_set_rand_addr cp;
693 /* Instance 0x00 doesn't have an adv_info, instead it uses
694 * hdev->random_addr to track its address so whenever it needs
695 * to be updated this also set the random address since
696 * hdev->random_addr is shared with scan state machine.
698 err = hci_set_random_addr_sync(hdev, random_addr);
703 memset(&cp, 0, sizeof(cp));
705 cp.handle = instance;
706 bacpy(&cp.bdaddr, random_addr);
708 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
709 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
712 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
714 struct hci_cp_le_set_ext_adv_params cp;
717 bdaddr_t random_addr;
720 struct adv_info *adv;
724 adv = hci_find_adv_instance(hdev, instance);
731 /* Updating parameters of an active instance will return a
732 * Command Disallowed error, so we must first disable the
733 * instance if it is active.
735 if (adv && !adv->pending) {
736 err = hci_disable_ext_adv_instance_sync(hdev, instance);
741 flags = hci_adv_instance_flags(hdev, instance);
743 /* If the "connectable" instance flag was not set, then choose between
744 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
746 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
747 mgmt_get_connectable(hdev);
749 if (!is_advertising_allowed(hdev, connectable))
752 /* Set require_privacy to true only when non-connectable
753 * advertising is used. In that case it is fine to use a
754 * non-resolvable private address.
756 err = hci_get_random_address(hdev, !connectable,
757 adv_use_rpa(hdev, flags), adv,
758 &own_addr_type, &random_addr);
762 memset(&cp, 0, sizeof(cp));
765 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
766 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
767 cp.tx_power = adv->tx_power;
769 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
770 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
771 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
774 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
778 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
780 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
781 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
782 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
784 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
786 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
789 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
791 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
794 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
795 * contains the peer’s Identity Address and the Peer_Address_Type
796 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
797 * These parameters are used to locate the corresponding local IRK in
798 * the resolving list; this IRK is used to generate their own address
799 * used in the advertisement.
801 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
802 hci_copy_identity_address(hdev, &cp.peer_addr,
805 cp.own_addr_type = own_addr_type;
806 cp.channel_map = hdev->le_adv_channel_map;
807 cp.handle = instance;
809 if (flags & MGMT_ADV_FLAG_SEC_2M) {
810 cp.primary_phy = HCI_ADV_PHY_1M;
811 cp.secondary_phy = HCI_ADV_PHY_2M;
812 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
813 cp.primary_phy = HCI_ADV_PHY_CODED;
814 cp.secondary_phy = HCI_ADV_PHY_CODED;
816 /* In all other cases use 1M */
817 cp.primary_phy = HCI_ADV_PHY_1M;
818 cp.secondary_phy = HCI_ADV_PHY_1M;
821 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
822 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
826 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
827 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
828 bacmp(&random_addr, BDADDR_ANY)) {
829 /* Check if random address need to be updated */
831 if (!bacmp(&random_addr, &adv->random_addr))
834 if (!bacmp(&random_addr, &hdev->random_addr))
838 return hci_set_adv_set_random_addr_sync(hdev, instance,
845 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
848 struct hci_cp_le_set_ext_scan_rsp_data cp;
849 u8 data[HCI_MAX_EXT_AD_LENGTH];
852 struct adv_info *adv = NULL;
855 memset(&pdu, 0, sizeof(pdu));
858 adv = hci_find_adv_instance(hdev, instance);
859 if (!adv || !adv->scan_rsp_changed)
863 len = eir_create_scan_rsp(hdev, instance, pdu.data);
865 pdu.cp.handle = instance;
867 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
868 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
870 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
871 sizeof(pdu.cp) + len, &pdu.cp,
877 adv->scan_rsp_changed = false;
879 memcpy(hdev->scan_rsp_data, pdu.data, len);
880 hdev->scan_rsp_data_len = len;
886 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
888 struct hci_cp_le_set_scan_rsp_data cp;
891 memset(&cp, 0, sizeof(cp));
893 len = eir_create_scan_rsp(hdev, instance, cp.data);
895 if (hdev->scan_rsp_data_len == len &&
896 !memcmp(cp.data, hdev->scan_rsp_data, len))
899 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
900 hdev->scan_rsp_data_len = len;
904 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
905 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
908 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
910 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
913 if (ext_adv_capable(hdev))
914 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
916 return __hci_set_scan_rsp_data_sync(hdev, instance);
919 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
921 struct hci_cp_le_set_ext_adv_enable *cp;
922 struct hci_cp_ext_adv_set *set;
923 u8 data[sizeof(*cp) + sizeof(*set) * 1];
924 struct adv_info *adv;
927 adv = hci_find_adv_instance(hdev, instance);
930 /* If already enabled there is nothing to do */
938 set = (void *)cp->data;
940 memset(cp, 0, sizeof(*cp));
943 cp->num_of_sets = 0x01;
945 memset(set, 0, sizeof(*set));
947 set->handle = instance;
949 /* Set duration per instance since controller is responsible for
952 if (adv && adv->timeout) {
953 u16 duration = adv->timeout * MSEC_PER_SEC;
955 /* Time = N * 10 ms */
956 set->duration = cpu_to_le16(duration / 10);
959 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
961 sizeof(*set) * cp->num_of_sets,
962 data, HCI_CMD_TIMEOUT);
965 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
969 err = hci_setup_ext_adv_instance_sync(hdev, instance);
973 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
977 return hci_enable_ext_advertising_sync(hdev, instance);
980 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
984 if (ext_adv_capable(hdev))
985 return hci_start_ext_adv_sync(hdev, instance);
987 err = hci_update_adv_data_sync(hdev, instance);
991 err = hci_update_scan_rsp_data_sync(hdev, instance);
995 return hci_enable_advertising_sync(hdev);
998 int hci_enable_advertising_sync(struct hci_dev *hdev)
1000 struct adv_info *adv_instance;
1001 struct hci_cp_le_set_adv_param cp;
1002 u8 own_addr_type, enable = 0x01;
1004 u16 adv_min_interval, adv_max_interval;
1008 if (ext_adv_capable(hdev))
1009 return hci_enable_ext_advertising_sync(hdev,
1010 hdev->cur_adv_instance);
1012 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1013 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1015 /* If the "connectable" instance flag was not set, then choose between
1016 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1018 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1019 mgmt_get_connectable(hdev);
1021 if (!is_advertising_allowed(hdev, connectable))
1024 status = hci_disable_advertising_sync(hdev);
1028 /* Clear the HCI_LE_ADV bit temporarily so that the
1029 * hci_update_random_address knows that it's safe to go ahead
1030 * and write a new random address. The flag will be set back on
1031 * as soon as the SET_ADV_ENABLE HCI command completes.
1033 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1035 /* Set require_privacy to true only when non-connectable
1036 * advertising is used. In that case it is fine to use a
1037 * non-resolvable private address.
1039 status = hci_update_random_address_sync(hdev, !connectable,
1040 adv_use_rpa(hdev, flags),
1045 memset(&cp, 0, sizeof(cp));
1048 adv_min_interval = adv_instance->min_interval;
1049 adv_max_interval = adv_instance->max_interval;
1051 adv_min_interval = hdev->le_adv_min_interval;
1052 adv_max_interval = hdev->le_adv_max_interval;
1056 cp.type = LE_ADV_IND;
1058 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1059 cp.type = LE_ADV_SCAN_IND;
1061 cp.type = LE_ADV_NONCONN_IND;
1063 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1064 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1065 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1066 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1070 cp.min_interval = cpu_to_le16(adv_min_interval);
1071 cp.max_interval = cpu_to_le16(adv_max_interval);
1072 cp.own_address_type = own_addr_type;
1073 cp.channel_map = hdev->le_adv_channel_map;
1075 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1076 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1080 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1081 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1084 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1086 return hci_enable_advertising_sync(hdev);
1089 int hci_enable_advertising(struct hci_dev *hdev)
1091 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1092 list_empty(&hdev->adv_instances))
1095 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1098 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1103 if (!ext_adv_capable(hdev))
1106 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1110 /* If request specifies an instance that doesn't exist, fail */
1111 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1114 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1115 sizeof(instance), &instance, 0,
1116 HCI_CMD_TIMEOUT, sk);
1119 static void cancel_adv_timeout(struct hci_dev *hdev)
1121 if (hdev->adv_instance_timeout) {
1122 hdev->adv_instance_timeout = 0;
1123 cancel_delayed_work(&hdev->adv_instance_expire);
1127 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1130 struct hci_cp_le_set_ext_adv_data cp;
1131 u8 data[HCI_MAX_EXT_AD_LENGTH];
1134 struct adv_info *adv = NULL;
1137 memset(&pdu, 0, sizeof(pdu));
1140 adv = hci_find_adv_instance(hdev, instance);
1141 if (!adv || !adv->adv_data_changed)
1145 len = eir_create_adv_data(hdev, instance, pdu.data);
1147 pdu.cp.length = len;
1148 pdu.cp.handle = instance;
1149 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1150 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1152 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1153 sizeof(pdu.cp) + len, &pdu.cp,
1158 /* Update data if the command succeed */
1160 adv->adv_data_changed = false;
1162 memcpy(hdev->adv_data, pdu.data, len);
1163 hdev->adv_data_len = len;
1169 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1171 struct hci_cp_le_set_adv_data cp;
1174 memset(&cp, 0, sizeof(cp));
1176 len = eir_create_adv_data(hdev, instance, cp.data);
1178 /* There's nothing to do if the data hasn't changed */
1179 if (hdev->adv_data_len == len &&
1180 memcmp(cp.data, hdev->adv_data, len) == 0)
1183 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1184 hdev->adv_data_len = len;
1188 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1189 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1192 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1194 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1197 if (ext_adv_capable(hdev))
1198 return hci_set_ext_adv_data_sync(hdev, instance);
1200 return hci_set_adv_data_sync(hdev, instance);
1203 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1206 struct adv_info *adv = NULL;
1209 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1212 if (hdev->adv_instance_timeout)
1215 adv = hci_find_adv_instance(hdev, instance);
1219 /* A zero timeout means unlimited advertising. As long as there is
1220 * only one instance, duration should be ignored. We still set a timeout
1221 * in case further instances are being added later on.
1223 * If the remaining lifetime of the instance is more than the duration
1224 * then the timeout corresponds to the duration, otherwise it will be
1225 * reduced to the remaining instance lifetime.
1227 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1228 timeout = adv->duration;
1230 timeout = adv->remaining_time;
1232 /* The remaining time is being reduced unless the instance is being
1233 * advertised without time limit.
1236 adv->remaining_time = adv->remaining_time - timeout;
1238 /* Only use work for scheduling instances with legacy advertising */
1239 if (!ext_adv_capable(hdev)) {
1240 hdev->adv_instance_timeout = timeout;
1241 queue_delayed_work(hdev->req_workqueue,
1242 &hdev->adv_instance_expire,
1243 msecs_to_jiffies(timeout * 1000));
1246 /* If we're just re-scheduling the same instance again then do not
1247 * execute any HCI commands. This happens when a single instance is
1250 if (!force && hdev->cur_adv_instance == instance &&
1251 hci_dev_test_flag(hdev, HCI_LE_ADV))
1254 hdev->cur_adv_instance = instance;
1256 return hci_start_adv_sync(hdev, instance);
1259 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1263 if (!ext_adv_capable(hdev))
1266 /* Disable instance 0x00 to disable all instances */
1267 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1271 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1272 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1275 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1277 struct adv_info *adv, *n;
1279 if (ext_adv_capable(hdev))
1280 /* Remove all existing sets */
1281 return hci_clear_adv_sets_sync(hdev, sk);
1283 /* This is safe as long as there is no command send while the lock is
1288 /* Cleanup non-ext instances */
1289 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1290 u8 instance = adv->instance;
1293 if (!(force || adv->timeout))
1296 err = hci_remove_adv_instance(hdev, instance);
1298 mgmt_advertising_removed(sk, hdev, instance);
1301 hci_dev_unlock(hdev);
1306 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1311 /* If we use extended advertising, instance has to be removed first. */
1312 if (ext_adv_capable(hdev))
1313 return hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1315 /* This is safe as long as there is no command send while the lock is
1320 err = hci_remove_adv_instance(hdev, instance);
1322 mgmt_advertising_removed(sk, hdev, instance);
1324 hci_dev_unlock(hdev);
1329 /* For a single instance:
1330 * - force == true: The instance will be removed even when its remaining
1331 * lifetime is not zero.
1332 * - force == false: the instance will be deactivated but kept stored unless
1333 * the remaining lifetime is zero.
1335 * For instance == 0x00:
1336 * - force == true: All instances will be removed regardless of their timeout
1338 * - force == false: Only instances that have a timeout will be removed.
1340 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1341 u8 instance, bool force)
1343 struct adv_info *next = NULL;
1346 /* Cancel any timeout concerning the removed instance(s). */
1347 if (!instance || hdev->cur_adv_instance == instance)
1348 cancel_adv_timeout(hdev);
1350 /* Get the next instance to advertise BEFORE we remove
1351 * the current one. This can be the same instance again
1352 * if there is only one instance.
1354 if (hdev->cur_adv_instance == instance)
1355 next = hci_get_next_instance(hdev, instance);
1358 err = hci_clear_adv_sync(hdev, sk, force);
1362 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1364 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1365 /* Don't advertise a removed instance. */
1366 if (next && next->instance == instance)
1369 err = hci_remove_adv_sync(hdev, instance, sk);
1375 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1378 if (next && !ext_adv_capable(hdev))
1379 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1384 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1386 struct hci_cp_read_rssi cp;
1389 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1390 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1393 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1395 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1396 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1399 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1401 struct hci_cp_read_tx_power cp;
1405 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1406 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1409 int hci_disable_advertising_sync(struct hci_dev *hdev)
1413 /* If controller is not advertising we are done. */
1414 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1417 if (ext_adv_capable(hdev))
1418 return hci_disable_ext_adv_instance_sync(hdev, 0x00);
1420 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1421 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1424 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1427 struct hci_cp_le_set_ext_scan_enable cp;
1429 memset(&cp, 0, sizeof(cp));
1431 cp.filter_dup = filter_dup;
1433 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1434 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1437 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
1440 struct hci_cp_le_set_scan_enable cp;
1442 if (use_ext_scan(hdev))
1443 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
1445 memset(&cp, 0, sizeof(cp));
1447 cp.filter_dup = filter_dup;
1449 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
1450 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1453 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
1455 if (!use_ll_privacy(hdev))
1458 /* If controller is not/already resolving we are done. */
1459 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
1462 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
1463 sizeof(val), &val, HCI_CMD_TIMEOUT);
1466 static int hci_scan_disable_sync(struct hci_dev *hdev)
1470 /* If controller is not scanning we are done. */
1471 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
1474 if (hdev->scanning_paused) {
1475 bt_dev_dbg(hdev, "Scanning is paused for suspend");
1479 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
1481 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
1488 static bool scan_use_rpa(struct hci_dev *hdev)
1490 return hci_dev_test_flag(hdev, HCI_PRIVACY);
1493 static void hci_start_interleave_scan(struct hci_dev *hdev)
1495 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
1496 queue_delayed_work(hdev->req_workqueue,
1497 &hdev->interleave_scan, 0);
1500 static bool is_interleave_scanning(struct hci_dev *hdev)
1502 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
1505 static void cancel_interleave_scan(struct hci_dev *hdev)
1507 bt_dev_dbg(hdev, "cancelling interleave scan");
1509 cancel_delayed_work_sync(&hdev->interleave_scan);
1511 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
1514 /* Return true if interleave_scan wasn't started until exiting this function,
1515 * otherwise, return false
1517 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
1519 /* Do interleaved scan only if all of the following are true:
1520 * - There is at least one ADV monitor
1521 * - At least one pending LE connection or one device to be scanned for
1522 * - Monitor offloading is not supported
1523 * If so, we should alternate between allowlist scan and one without
1524 * any filters to save power.
1526 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
1527 !(list_empty(&hdev->pend_le_conns) &&
1528 list_empty(&hdev->pend_le_reports)) &&
1529 hci_get_adv_monitor_offload_ext(hdev) ==
1530 HCI_ADV_MONITOR_EXT_NONE;
1531 bool is_interleaving = is_interleave_scanning(hdev);
1533 if (use_interleaving && !is_interleaving) {
1534 hci_start_interleave_scan(hdev);
1535 bt_dev_dbg(hdev, "starting interleave scan");
1539 if (!use_interleaving && is_interleaving)
1540 cancel_interleave_scan(hdev);
1545 /* Removes connection to resolve list if needed.*/
1546 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
1547 bdaddr_t *bdaddr, u8 bdaddr_type)
1549 struct hci_cp_le_del_from_resolv_list cp;
1550 struct bdaddr_list_with_irk *entry;
1552 if (!use_ll_privacy(hdev))
1555 /* Check if the IRK has been programmed */
1556 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
1561 cp.bdaddr_type = bdaddr_type;
1562 bacpy(&cp.bdaddr, bdaddr);
1564 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
1565 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1568 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
1569 bdaddr_t *bdaddr, u8 bdaddr_type)
1571 struct hci_cp_le_del_from_accept_list cp;
1574 /* Check if device is on accept list before removing it */
1575 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
1578 cp.bdaddr_type = bdaddr_type;
1579 bacpy(&cp.bdaddr, bdaddr);
1581 /* Ignore errors when removing from resolving list as that is likely
1582 * that the device was never added.
1584 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
1586 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
1587 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1589 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
1593 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
1599 /* Adds connection to resolve list if needed.
1600 * Setting params to NULL programs local hdev->irk
1602 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
1603 struct hci_conn_params *params)
1605 struct hci_cp_le_add_to_resolv_list cp;
1606 struct smp_irk *irk;
1607 struct bdaddr_list_with_irk *entry;
1609 if (!use_ll_privacy(hdev))
1612 /* Attempt to program local identity address, type and irk if params is
1616 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
1619 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
1620 memcpy(cp.peer_irk, hdev->irk, 16);
1624 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
1628 /* Check if the IK has _not_ been programmed yet. */
1629 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
1635 cp.bdaddr_type = params->addr_type;
1636 bacpy(&cp.bdaddr, ¶ms->addr);
1637 memcpy(cp.peer_irk, irk->val, 16);
1639 /* Default privacy mode is always Network */
1640 params->privacy_mode = HCI_NETWORK_PRIVACY;
1643 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
1644 memcpy(cp.local_irk, hdev->irk, 16);
1646 memset(cp.local_irk, 0, 16);
1648 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
1649 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1652 /* Set Device Privacy Mode. */
1653 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
1654 struct hci_conn_params *params)
1656 struct hci_cp_le_set_privacy_mode cp;
1657 struct smp_irk *irk;
1659 /* If device privacy mode has already been set there is nothing to do */
1660 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
1663 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
1664 * indicates that LL Privacy has been enabled and
1665 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
1667 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
1670 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
1674 memset(&cp, 0, sizeof(cp));
1675 cp.bdaddr_type = irk->addr_type;
1676 bacpy(&cp.bdaddr, &irk->bdaddr);
1677 cp.mode = HCI_DEVICE_PRIVACY;
1679 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
1680 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1683 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
1684 * this attempts to program the device in the resolving list as well and
1685 * properly set the privacy mode.
1687 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
1688 struct hci_conn_params *params,
1691 struct hci_cp_le_add_to_accept_list cp;
1694 /* During suspend, only wakeable devices can be in acceptlist */
1695 if (hdev->suspended &&
1696 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
1699 /* Select filter policy to accept all advertising */
1700 if (*num_entries >= hdev->le_accept_list_size)
1703 /* Accept list can not be used with RPAs */
1704 if (!use_ll_privacy(hdev) &&
1705 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type))
1708 /* Attempt to program the device in the resolving list first to avoid
1709 * having to rollback in case it fails since the resolving list is
1710 * dynamic it can probably be smaller than the accept list.
1712 err = hci_le_add_resolve_list_sync(hdev, params);
1714 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
1718 /* Set Privacy Mode */
1719 err = hci_le_set_privacy_mode_sync(hdev, params);
1721 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
1725 /* Check if already in accept list */
1726 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
1731 cp.bdaddr_type = params->addr_type;
1732 bacpy(&cp.bdaddr, ¶ms->addr);
1734 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
1735 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1737 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
1738 /* Rollback the device from the resolving list */
1739 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
1743 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
1749 /* This function disables/pause all advertising instances */
1750 static int hci_pause_advertising_sync(struct hci_dev *hdev)
1755 /* If already been paused there is nothing to do. */
1756 if (hdev->advertising_paused)
1759 bt_dev_dbg(hdev, "Pausing directed advertising");
1761 /* Stop directed advertising */
1762 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
1764 /* When discoverable timeout triggers, then just make sure
1765 * the limited discoverable flag is cleared. Even in the case
1766 * of a timeout triggered from general discoverable, it is
1767 * safe to unconditionally clear the flag.
1769 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1770 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1771 hdev->discov_timeout = 0;
1774 bt_dev_dbg(hdev, "Pausing advertising instances");
1776 /* Call to disable any advertisements active on the controller.
1777 * This will succeed even if no advertisements are configured.
1779 err = hci_disable_advertising_sync(hdev);
1783 /* If we are using software rotation, pause the loop */
1784 if (!ext_adv_capable(hdev))
1785 cancel_adv_timeout(hdev);
1787 hdev->advertising_paused = true;
1788 hdev->advertising_old_state = old_state;
1793 /* This function enables all user advertising instances */
1794 static int hci_resume_advertising_sync(struct hci_dev *hdev)
1796 struct adv_info *adv, *tmp;
1799 /* If advertising has not been paused there is nothing to do. */
1800 if (!hdev->advertising_paused)
1803 /* Resume directed advertising */
1804 hdev->advertising_paused = false;
1805 if (hdev->advertising_old_state) {
1806 hci_dev_set_flag(hdev, HCI_ADVERTISING);
1807 hdev->advertising_old_state = 0;
1810 bt_dev_dbg(hdev, "Resuming advertising instances");
1812 if (ext_adv_capable(hdev)) {
1813 /* Call for each tracked instance to be re-enabled */
1814 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
1815 err = hci_enable_ext_advertising_sync(hdev,
1820 /* If the instance cannot be resumed remove it */
1821 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
1825 /* Schedule for most recent instance to be restarted and begin
1826 * the software rotation loop
1828 err = hci_schedule_adv_instance_sync(hdev,
1829 hdev->cur_adv_instance,
1833 hdev->advertising_paused = false;
1838 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
1839 bool extended, struct sock *sk)
1841 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
1842 HCI_OP_READ_LOCAL_OOB_DATA;
1844 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1847 /* Device must not be scanning when updating the accept list.
1849 * Update is done using the following sequence:
1851 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
1852 * Remove Devices From Accept List ->
1853 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
1854 * Add Devices to Accept List ->
1855 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
1856 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
1859 * In case of failure advertising shall be restored to its original state and
1860 * return would disable accept list since either accept or resolving list could
1861 * not be programmed.
1864 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
1866 struct hci_conn_params *params;
1867 struct bdaddr_list *b, *t;
1869 bool pend_conn, pend_report;
1873 /* Pause advertising if resolving list can be used as controllers are
1874 * cannot accept resolving list modifications while advertising.
1876 if (use_ll_privacy(hdev)) {
1877 err = hci_pause_advertising_sync(hdev);
1879 bt_dev_err(hdev, "pause advertising failed: %d", err);
1884 /* Disable address resolution while reprogramming accept list since
1885 * devices that do have an IRK will be programmed in the resolving list
1886 * when LL Privacy is enabled.
1888 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
1890 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
1894 /* Go through the current accept list programmed into the
1895 * controller one by one and check if that address is connected or is
1896 * still in the list of pending connections or list of devices to
1897 * report. If not present in either list, then remove it from
1900 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
1901 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
1904 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
1907 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
1911 /* If the device is not likely to connect or report,
1912 * remove it from the acceptlist.
1914 if (!pend_conn && !pend_report) {
1915 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
1923 /* Since all no longer valid accept list entries have been
1924 * removed, walk through the list of pending connections
1925 * and ensure that any new device gets programmed into
1928 * If the list of the devices is larger than the list of
1929 * available accept list entries in the controller, then
1930 * just abort and return filer policy value to not use the
1933 list_for_each_entry(params, &hdev->pend_le_conns, action) {
1934 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
1939 /* After adding all new pending connections, walk through
1940 * the list of pending reports and also add these to the
1941 * accept list if there is still space. Abort if space runs out.
1943 list_for_each_entry(params, &hdev->pend_le_reports, action) {
1944 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
1949 /* Use the allowlist unless the following conditions are all true:
1950 * - We are not currently suspending
1951 * - There are 1 or more ADV monitors registered and it's not offloaded
1952 * - Interleaved scanning is not currently using the allowlist
1954 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
1955 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
1956 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
1960 filter_policy = err ? 0x00 : 0x01;
1962 /* Enable address resolution when LL Privacy is enabled. */
1963 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
1965 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
1967 /* Resume advertising if it was paused */
1968 if (use_ll_privacy(hdev))
1969 hci_resume_advertising_sync(hdev);
1971 /* Select filter policy to use accept list */
1972 return filter_policy;
1975 /* Returns true if an le connection is in the scanning state */
1976 static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
1978 struct hci_conn_hash *h = &hdev->conn_hash;
1983 list_for_each_entry_rcu(c, &h->list, list) {
1984 if (c->type == LE_LINK && c->state == BT_CONNECT &&
1985 test_bit(HCI_CONN_SCANNING, &c->flags)) {
1996 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
1997 u16 interval, u16 window,
1998 u8 own_addr_type, u8 filter_policy)
2000 struct hci_cp_le_set_ext_scan_params *cp;
2001 struct hci_cp_le_scan_phy_params *phy;
2002 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2006 phy = (void *)cp->data;
2008 memset(data, 0, sizeof(data));
2010 cp->own_addr_type = own_addr_type;
2011 cp->filter_policy = filter_policy;
2013 if (scan_1m(hdev) || scan_2m(hdev)) {
2014 cp->scanning_phys |= LE_SCAN_PHY_1M;
2017 phy->interval = cpu_to_le16(interval);
2018 phy->window = cpu_to_le16(window);
2024 if (scan_coded(hdev)) {
2025 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2028 phy->interval = cpu_to_le16(interval);
2029 phy->window = cpu_to_le16(window);
2035 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2036 sizeof(*cp) + sizeof(*phy) * num_phy,
2037 data, HCI_CMD_TIMEOUT);
2040 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2041 u16 interval, u16 window,
2042 u8 own_addr_type, u8 filter_policy)
2044 struct hci_cp_le_set_scan_param cp;
2046 if (use_ext_scan(hdev))
2047 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2048 window, own_addr_type,
2051 memset(&cp, 0, sizeof(cp));
2053 cp.interval = cpu_to_le16(interval);
2054 cp.window = cpu_to_le16(window);
2055 cp.own_address_type = own_addr_type;
2056 cp.filter_policy = filter_policy;
2058 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2059 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2062 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2063 u16 window, u8 own_addr_type, u8 filter_policy,
2068 if (hdev->scanning_paused) {
2069 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2073 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2074 own_addr_type, filter_policy);
2078 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2081 static int hci_passive_scan_sync(struct hci_dev *hdev)
2085 u16 window, interval;
2088 if (hdev->scanning_paused) {
2089 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2093 err = hci_scan_disable_sync(hdev);
2095 bt_dev_err(hdev, "disable scanning failed: %d", err);
2099 /* Set require_privacy to false since no SCAN_REQ are send
2100 * during passive scanning. Not using an non-resolvable address
2101 * here is important so that peer devices using direct
2102 * advertising with our address will be correctly reported
2103 * by the controller.
2105 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2109 if (hdev->enable_advmon_interleave_scan &&
2110 hci_update_interleaved_scan_sync(hdev))
2113 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2115 /* Adding or removing entries from the accept list must
2116 * happen before enabling scanning. The controller does
2117 * not allow accept list modification while scanning.
2119 filter_policy = hci_update_accept_list_sync(hdev);
2121 /* When the controller is using random resolvable addresses and
2122 * with that having LE privacy enabled, then controllers with
2123 * Extended Scanner Filter Policies support can now enable support
2124 * for handling directed advertising.
2126 * So instead of using filter polices 0x00 (no acceptlist)
2127 * and 0x01 (acceptlist enabled) use the new filter policies
2128 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2130 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2131 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2132 filter_policy |= 0x02;
2134 if (hdev->suspended) {
2135 window = hdev->le_scan_window_suspend;
2136 interval = hdev->le_scan_int_suspend;
2137 } else if (hci_is_le_conn_scanning(hdev)) {
2138 window = hdev->le_scan_window_connect;
2139 interval = hdev->le_scan_int_connect;
2140 } else if (hci_is_adv_monitoring(hdev)) {
2141 window = hdev->le_scan_window_adv_monitor;
2142 interval = hdev->le_scan_int_adv_monitor;
2144 window = hdev->le_scan_window;
2145 interval = hdev->le_scan_interval;
2148 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2150 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2151 own_addr_type, filter_policy,
2152 LE_SCAN_FILTER_DUP_ENABLE);
2155 /* This function controls the passive scanning based on hdev->pend_le_conns
2156 * list. If there are pending LE connection we start the background scanning,
2157 * otherwise we stop it in the following sequence:
2159 * If there are devices to scan:
2161 * Disable Scanning -> Update Accept List ->
2162 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2163 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2170 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2174 if (!test_bit(HCI_UP, &hdev->flags) ||
2175 test_bit(HCI_INIT, &hdev->flags) ||
2176 hci_dev_test_flag(hdev, HCI_SETUP) ||
2177 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2178 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2179 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2182 /* No point in doing scanning if LE support hasn't been enabled */
2183 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2186 /* If discovery is active don't interfere with it */
2187 if (hdev->discovery.state != DISCOVERY_STOPPED)
2190 /* Reset RSSI and UUID filters when starting background scanning
2191 * since these filters are meant for service discovery only.
2193 * The Start Discovery and Start Service Discovery operations
2194 * ensure to set proper values for RSSI threshold and UUID
2195 * filter list. So it is safe to just reset them here.
2197 hci_discovery_filter_clear(hdev);
2199 bt_dev_dbg(hdev, "ADV monitoring is %s",
2200 hci_is_adv_monitoring(hdev) ? "on" : "off");
2202 if (list_empty(&hdev->pend_le_conns) &&
2203 list_empty(&hdev->pend_le_reports) &&
2204 !hci_is_adv_monitoring(hdev)) {
2205 /* If there is no pending LE connections or devices
2206 * to be scanned for or no ADV monitors, we should stop the
2207 * background scanning.
2210 bt_dev_dbg(hdev, "stopping background scanning");
2212 err = hci_scan_disable_sync(hdev);
2214 bt_dev_err(hdev, "stop background scanning failed: %d",
2217 /* If there is at least one pending LE connection, we should
2218 * keep the background scan running.
2221 /* If controller is connecting, we should not start scanning
2222 * since some controllers are not able to scan and connect at
2225 if (hci_lookup_le_connect(hdev))
2228 bt_dev_dbg(hdev, "start background scanning");
2230 err = hci_passive_scan_sync(hdev);
2232 bt_dev_err(hdev, "start background scanning failed: %d",
2239 static int update_scan_sync(struct hci_dev *hdev, void *data)
2241 return hci_update_scan_sync(hdev);
2244 int hci_update_scan(struct hci_dev *hdev)
2246 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
2249 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2251 return hci_update_passive_scan_sync(hdev);
2254 int hci_update_passive_scan(struct hci_dev *hdev)
2256 /* Only queue if it would have any effect */
2257 if (!test_bit(HCI_UP, &hdev->flags) ||
2258 test_bit(HCI_INIT, &hdev->flags) ||
2259 hci_dev_test_flag(hdev, HCI_SETUP) ||
2260 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2261 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2262 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2265 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2268 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2272 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2275 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2276 sizeof(val), &val, HCI_CMD_TIMEOUT);
2280 hdev->features[1][0] |= LMP_HOST_SC;
2281 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2283 hdev->features[1][0] &= ~LMP_HOST_SC;
2284 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2291 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2295 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2296 lmp_host_ssp_capable(hdev))
2299 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
2300 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2301 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2304 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2305 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2309 return hci_write_sc_support_sync(hdev, 0x01);
2312 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
2314 struct hci_cp_write_le_host_supported cp;
2316 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
2317 !lmp_bredr_capable(hdev))
2320 /* Check first if we already have the right host state
2321 * (host features set)
2323 if (le == lmp_host_le_capable(hdev) &&
2324 simul == lmp_host_le_br_capable(hdev))
2327 memset(&cp, 0, sizeof(cp));
2332 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
2333 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2336 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
2338 struct adv_info *adv, *tmp;
2341 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2344 /* If RPA Resolution has not been enable yet it means the
2345 * resolving list is empty and we should attempt to program the
2346 * local IRK in order to support using own_addr_type
2347 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
2349 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
2350 hci_le_add_resolve_list_sync(hdev, NULL);
2351 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2354 /* Make sure the controller has a good default for
2355 * advertising data. This also applies to the case
2356 * where BR/EDR was toggled during the AUTO_OFF phase.
2358 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2359 list_empty(&hdev->adv_instances)) {
2360 if (ext_adv_capable(hdev)) {
2361 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
2363 hci_update_scan_rsp_data_sync(hdev, 0x00);
2365 err = hci_update_adv_data_sync(hdev, 0x00);
2367 hci_update_scan_rsp_data_sync(hdev, 0x00);
2370 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2371 hci_enable_advertising_sync(hdev);
2374 /* Call for each tracked instance to be scheduled */
2375 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
2376 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
2381 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
2385 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
2386 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
2389 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
2390 sizeof(link_sec), &link_sec,
2394 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
2396 struct hci_cp_write_page_scan_activity cp;
2400 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2403 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
2406 memset(&cp, 0, sizeof(cp));
2409 type = PAGE_SCAN_TYPE_INTERLACED;
2411 /* 160 msec page scan interval */
2412 cp.interval = cpu_to_le16(0x0100);
2414 type = hdev->def_page_scan_type;
2415 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
2418 cp.window = cpu_to_le16(hdev->def_page_scan_window);
2420 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
2421 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
2422 err = __hci_cmd_sync_status(hdev,
2423 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
2424 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2429 if (hdev->page_scan_type != type)
2430 err = __hci_cmd_sync_status(hdev,
2431 HCI_OP_WRITE_PAGE_SCAN_TYPE,
2432 sizeof(type), &type,
2438 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
2440 struct bdaddr_list *b;
2442 list_for_each_entry(b, &hdev->accept_list, list) {
2443 struct hci_conn *conn;
2445 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
2449 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2456 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
2458 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
2463 int hci_update_scan_sync(struct hci_dev *hdev)
2467 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2470 if (!hdev_is_powered(hdev))
2473 if (mgmt_powering_down(hdev))
2476 if (hdev->scanning_paused)
2479 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
2480 disconnected_accept_list_entries(hdev))
2483 scan = SCAN_DISABLED;
2485 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
2486 scan |= SCAN_INQUIRY;
2488 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
2489 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
2492 return hci_write_scan_enable_sync(hdev, scan);
2495 int hci_update_name_sync(struct hci_dev *hdev)
2497 struct hci_cp_write_local_name cp;
2499 memset(&cp, 0, sizeof(cp));
2501 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
2503 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
2508 /* This function perform powered update HCI command sequence after the HCI init
2509 * sequence which end up resetting all states, the sequence is as follows:
2511 * HCI_SSP_ENABLED(Enable SSP)
2512 * HCI_LE_ENABLED(Enable LE)
2513 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
2515 * Enable Authentication
2516 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
2517 * Set Name -> Set EIR)
2519 int hci_powered_update_sync(struct hci_dev *hdev)
2523 /* Register the available SMP channels (BR/EDR and LE) only when
2524 * successfully powering on the controller. This late
2525 * registration is required so that LE SMP can clearly decide if
2526 * the public address or static address is used.
2530 err = hci_write_ssp_mode_sync(hdev, 0x01);
2534 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
2538 err = hci_powered_update_adv_sync(hdev);
2542 err = hci_write_auth_enable_sync(hdev);
2546 if (lmp_bredr_capable(hdev)) {
2547 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
2548 hci_write_fast_connectable_sync(hdev, true);
2550 hci_write_fast_connectable_sync(hdev, false);
2551 hci_update_scan_sync(hdev);
2552 hci_update_class_sync(hdev);
2553 hci_update_name_sync(hdev);
2554 hci_update_eir_sync(hdev);
2561 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
2562 * (BD_ADDR) for a HCI device from
2563 * a firmware node property.
2564 * @hdev: The HCI device
2566 * Search the firmware node for 'local-bd-address'.
2568 * All-zero BD addresses are rejected, because those could be properties
2569 * that exist in the firmware tables, but were not updated by the firmware. For
2570 * example, the DTS could define 'local-bd-address', with zero BD addresses.
2572 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
2574 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
2578 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
2579 (u8 *)&ba, sizeof(ba));
2580 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
2583 bacpy(&hdev->public_addr, &ba);
2586 struct hci_init_stage {
2587 int (*func)(struct hci_dev *hdev);
2590 /* Run init stage NULL terminated function table */
2591 static int hci_init_stage_sync(struct hci_dev *hdev,
2592 const struct hci_init_stage *stage)
2596 for (i = 0; stage[i].func; i++) {
2599 err = stage[i].func(hdev);
2607 /* Read Local Version */
2608 static int hci_read_local_version_sync(struct hci_dev *hdev)
2610 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
2611 0, NULL, HCI_CMD_TIMEOUT);
2614 /* Read BD Address */
2615 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
2617 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
2618 0, NULL, HCI_CMD_TIMEOUT);
2621 #define HCI_INIT(_func) \
2626 static const struct hci_init_stage hci_init0[] = {
2627 /* HCI_OP_READ_LOCAL_VERSION */
2628 HCI_INIT(hci_read_local_version_sync),
2629 /* HCI_OP_READ_BD_ADDR */
2630 HCI_INIT(hci_read_bd_addr_sync),
2634 int hci_reset_sync(struct hci_dev *hdev)
2638 set_bit(HCI_RESET, &hdev->flags);
2640 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
2648 static int hci_init0_sync(struct hci_dev *hdev)
2652 bt_dev_dbg(hdev, "");
2655 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
2656 err = hci_reset_sync(hdev);
2661 return hci_init_stage_sync(hdev, hci_init0);
2664 static int hci_unconf_init_sync(struct hci_dev *hdev)
2668 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
2671 err = hci_init0_sync(hdev);
2675 if (hci_dev_test_flag(hdev, HCI_SETUP))
2676 hci_debugfs_create_basic(hdev);
2681 /* Read Local Supported Features. */
2682 static int hci_read_local_features_sync(struct hci_dev *hdev)
2684 /* Not all AMP controllers support this command */
2685 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
2688 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
2689 0, NULL, HCI_CMD_TIMEOUT);
2692 /* BR Controller init stage 1 command sequence */
2693 static const struct hci_init_stage br_init1[] = {
2694 /* HCI_OP_READ_LOCAL_FEATURES */
2695 HCI_INIT(hci_read_local_features_sync),
2696 /* HCI_OP_READ_LOCAL_VERSION */
2697 HCI_INIT(hci_read_local_version_sync),
2698 /* HCI_OP_READ_BD_ADDR */
2699 HCI_INIT(hci_read_bd_addr_sync),
2703 /* Read Local Commands */
2704 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
2706 /* All Bluetooth 1.2 and later controllers should support the
2707 * HCI command for reading the local supported commands.
2709 * Unfortunately some controllers indicate Bluetooth 1.2 support,
2710 * but do not have support for this command. If that is the case,
2711 * the driver can quirk the behavior and skip reading the local
2712 * supported commands.
2714 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
2715 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
2716 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
2717 0, NULL, HCI_CMD_TIMEOUT);
2722 /* Read Local AMP Info */
2723 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
2725 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
2726 0, NULL, HCI_CMD_TIMEOUT);
2729 /* Read Data Blk size */
2730 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
2732 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
2733 0, NULL, HCI_CMD_TIMEOUT);
2736 /* Read Flow Control Mode */
2737 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
2739 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
2740 0, NULL, HCI_CMD_TIMEOUT);
2743 /* Read Location Data */
2744 static int hci_read_location_data_sync(struct hci_dev *hdev)
2746 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
2747 0, NULL, HCI_CMD_TIMEOUT);
2750 /* AMP Controller init stage 1 command sequence */
2751 static const struct hci_init_stage amp_init1[] = {
2752 /* HCI_OP_READ_LOCAL_VERSION */
2753 HCI_INIT(hci_read_local_version_sync),
2754 /* HCI_OP_READ_LOCAL_COMMANDS */
2755 HCI_INIT(hci_read_local_cmds_sync),
2756 /* HCI_OP_READ_LOCAL_AMP_INFO */
2757 HCI_INIT(hci_read_local_amp_info_sync),
2758 /* HCI_OP_READ_DATA_BLOCK_SIZE */
2759 HCI_INIT(hci_read_data_block_size_sync),
2760 /* HCI_OP_READ_FLOW_CONTROL_MODE */
2761 HCI_INIT(hci_read_flow_control_mode_sync),
2762 /* HCI_OP_READ_LOCATION_DATA */
2763 HCI_INIT(hci_read_location_data_sync),
2766 static int hci_init1_sync(struct hci_dev *hdev)
2770 bt_dev_dbg(hdev, "");
2773 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
2774 err = hci_reset_sync(hdev);
2779 switch (hdev->dev_type) {
2781 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
2782 return hci_init_stage_sync(hdev, br_init1);
2784 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
2785 return hci_init_stage_sync(hdev, amp_init1);
2787 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
2794 /* AMP Controller init stage 2 command sequence */
2795 static const struct hci_init_stage amp_init2[] = {
2796 /* HCI_OP_READ_LOCAL_FEATURES */
2797 HCI_INIT(hci_read_local_features_sync),
2800 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
2801 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
2803 /* Use Read LE Buffer Size V2 if supported */
2804 if (hdev->commands[41] & 0x20)
2805 return __hci_cmd_sync_status(hdev,
2806 HCI_OP_LE_READ_BUFFER_SIZE_V2,
2807 0, NULL, HCI_CMD_TIMEOUT);
2809 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
2810 0, NULL, HCI_CMD_TIMEOUT);
2813 /* Read Class of Device */
2814 static int hci_read_dev_class_sync(struct hci_dev *hdev)
2816 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
2817 0, NULL, HCI_CMD_TIMEOUT);
2820 /* Read Local Name */
2821 static int hci_read_local_name_sync(struct hci_dev *hdev)
2823 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
2824 0, NULL, HCI_CMD_TIMEOUT);
2827 /* Read Voice Setting */
2828 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
2830 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
2831 0, NULL, HCI_CMD_TIMEOUT);
2834 /* Read Number of Supported IAC */
2835 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
2837 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
2838 0, NULL, HCI_CMD_TIMEOUT);
2841 /* Read Current IAC LAP */
2842 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
2844 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
2845 0, NULL, HCI_CMD_TIMEOUT);
2848 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
2849 u8 cond_type, bdaddr_t *bdaddr,
2852 struct hci_cp_set_event_filter cp;
2854 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2857 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
2860 memset(&cp, 0, sizeof(cp));
2861 cp.flt_type = flt_type;
2863 if (flt_type != HCI_FLT_CLEAR_ALL) {
2864 cp.cond_type = cond_type;
2865 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
2866 cp.addr_conn_flt.auto_accept = auto_accept;
2869 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
2870 flt_type == HCI_FLT_CLEAR_ALL ?
2871 sizeof(cp.flt_type) : sizeof(cp), &cp,
2875 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
2877 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
2880 /* In theory the state machine should not reach here unless
2881 * a hci_set_event_filter_sync() call succeeds, but we do
2882 * the check both for parity and as a future reminder.
2884 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
2887 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
2891 /* Connection accept timeout ~20 secs */
2892 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
2894 __le16 param = cpu_to_le16(0x7d00);
2896 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
2897 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
2900 /* BR Controller init stage 2 command sequence */
2901 static const struct hci_init_stage br_init2[] = {
2902 /* HCI_OP_READ_BUFFER_SIZE */
2903 HCI_INIT(hci_read_buffer_size_sync),
2904 /* HCI_OP_READ_CLASS_OF_DEV */
2905 HCI_INIT(hci_read_dev_class_sync),
2906 /* HCI_OP_READ_LOCAL_NAME */
2907 HCI_INIT(hci_read_local_name_sync),
2908 /* HCI_OP_READ_VOICE_SETTING */
2909 HCI_INIT(hci_read_voice_setting_sync),
2910 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
2911 HCI_INIT(hci_read_num_supported_iac_sync),
2912 /* HCI_OP_READ_CURRENT_IAC_LAP */
2913 HCI_INIT(hci_read_current_iac_lap_sync),
2914 /* HCI_OP_SET_EVENT_FLT */
2915 HCI_INIT(hci_clear_event_filter_sync),
2916 /* HCI_OP_WRITE_CA_TIMEOUT */
2917 HCI_INIT(hci_write_ca_timeout_sync),
2921 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
2925 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2928 /* When SSP is available, then the host features page
2929 * should also be available as well. However some
2930 * controllers list the max_page as 0 as long as SSP
2931 * has not been enabled. To achieve proper debugging
2932 * output, force the minimum max_page to 1 at least.
2934 hdev->max_page = 0x01;
2936 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2937 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2940 static int hci_write_eir_sync(struct hci_dev *hdev)
2942 struct hci_cp_write_eir cp;
2944 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2947 memset(hdev->eir, 0, sizeof(hdev->eir));
2948 memset(&cp, 0, sizeof(cp));
2950 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
2954 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
2958 if (!lmp_inq_rssi_capable(hdev) &&
2959 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
2962 /* If Extended Inquiry Result events are supported, then
2963 * they are clearly preferred over Inquiry Result with RSSI
2966 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
2968 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
2969 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2972 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
2974 if (!lmp_inq_tx_pwr_capable(hdev))
2977 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
2978 0, NULL, HCI_CMD_TIMEOUT);
2981 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
2983 struct hci_cp_read_local_ext_features cp;
2985 if (!lmp_ext_feat_capable(hdev))
2988 memset(&cp, 0, sizeof(cp));
2991 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
2992 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2995 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
2997 return hci_read_local_ext_features_sync(hdev, 0x01);
3000 /* HCI Controller init stage 2 command sequence */
3001 static const struct hci_init_stage hci_init2[] = {
3002 /* HCI_OP_READ_LOCAL_COMMANDS */
3003 HCI_INIT(hci_read_local_cmds_sync),
3004 /* HCI_OP_WRITE_SSP_MODE */
3005 HCI_INIT(hci_write_ssp_mode_1_sync),
3006 /* HCI_OP_WRITE_EIR */
3007 HCI_INIT(hci_write_eir_sync),
3008 /* HCI_OP_WRITE_INQUIRY_MODE */
3009 HCI_INIT(hci_write_inquiry_mode_sync),
3010 /* HCI_OP_READ_INQ_RSP_TX_POWER */
3011 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3012 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3013 HCI_INIT(hci_read_local_ext_features_1_sync),
3014 /* HCI_OP_WRITE_AUTH_ENABLE */
3015 HCI_INIT(hci_write_auth_enable_sync),
3019 /* Read LE Buffer Size */
3020 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3022 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3023 0, NULL, HCI_CMD_TIMEOUT);
3026 /* Read LE Local Supported Features */
3027 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3029 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3030 0, NULL, HCI_CMD_TIMEOUT);
3033 /* Read LE Supported States */
3034 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3036 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3037 0, NULL, HCI_CMD_TIMEOUT);
3040 /* LE Controller init stage 2 command sequence */
3041 static const struct hci_init_stage le_init2[] = {
3042 /* HCI_OP_LE_READ_BUFFER_SIZE */
3043 HCI_INIT(hci_le_read_buffer_size_sync),
3044 /* HCI_OP_LE_READ_LOCAL_FEATURES */
3045 HCI_INIT(hci_le_read_local_features_sync),
3046 /* HCI_OP_LE_READ_SUPPORTED_STATES */
3047 HCI_INIT(hci_le_read_supported_states_sync),
3051 static int hci_init2_sync(struct hci_dev *hdev)
3055 bt_dev_dbg(hdev, "");
3057 if (hdev->dev_type == HCI_AMP)
3058 return hci_init_stage_sync(hdev, amp_init2);
3060 err = hci_init_stage_sync(hdev, hci_init2);
3064 if (lmp_bredr_capable(hdev)) {
3065 err = hci_init_stage_sync(hdev, br_init2);
3069 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3072 if (lmp_le_capable(hdev)) {
3073 err = hci_init_stage_sync(hdev, le_init2);
3076 /* LE-only controllers have LE implicitly enabled */
3077 if (!lmp_bredr_capable(hdev))
3078 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3084 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3086 /* The second byte is 0xff instead of 0x9f (two reserved bits
3087 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3088 * command otherwise.
3090 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3092 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3093 * any event mask for pre 1.2 devices.
3095 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3098 if (lmp_bredr_capable(hdev)) {
3099 events[4] |= 0x01; /* Flow Specification Complete */
3101 /* Don't set Disconnect Complete when suspended as that
3102 * would wakeup the host when disconnecting due to
3105 if (hdev->suspended)
3108 /* Use a different default for LE-only devices */
3109 memset(events, 0, sizeof(events));
3110 events[1] |= 0x20; /* Command Complete */
3111 events[1] |= 0x40; /* Command Status */
3112 events[1] |= 0x80; /* Hardware Error */
3114 /* If the controller supports the Disconnect command, enable
3115 * the corresponding event. In addition enable packet flow
3116 * control related events.
3118 if (hdev->commands[0] & 0x20) {
3119 /* Don't set Disconnect Complete when suspended as that
3120 * would wakeup the host when disconnecting due to
3123 if (!hdev->suspended)
3124 events[0] |= 0x10; /* Disconnection Complete */
3125 events[2] |= 0x04; /* Number of Completed Packets */
3126 events[3] |= 0x02; /* Data Buffer Overflow */
3129 /* If the controller supports the Read Remote Version
3130 * Information command, enable the corresponding event.
3132 if (hdev->commands[2] & 0x80)
3133 events[1] |= 0x08; /* Read Remote Version Information
3137 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3138 events[0] |= 0x80; /* Encryption Change */
3139 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3143 if (lmp_inq_rssi_capable(hdev) ||
3144 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3145 events[4] |= 0x02; /* Inquiry Result with RSSI */
3147 if (lmp_ext_feat_capable(hdev))
3148 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3150 if (lmp_esco_capable(hdev)) {
3151 events[5] |= 0x08; /* Synchronous Connection Complete */
3152 events[5] |= 0x10; /* Synchronous Connection Changed */
3155 if (lmp_sniffsubr_capable(hdev))
3156 events[5] |= 0x20; /* Sniff Subrating */
3158 if (lmp_pause_enc_capable(hdev))
3159 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3161 if (lmp_ext_inq_capable(hdev))
3162 events[5] |= 0x40; /* Extended Inquiry Result */
3164 if (lmp_no_flush_capable(hdev))
3165 events[7] |= 0x01; /* Enhanced Flush Complete */
3167 if (lmp_lsto_capable(hdev))
3168 events[6] |= 0x80; /* Link Supervision Timeout Changed */
3170 if (lmp_ssp_capable(hdev)) {
3171 events[6] |= 0x01; /* IO Capability Request */
3172 events[6] |= 0x02; /* IO Capability Response */
3173 events[6] |= 0x04; /* User Confirmation Request */
3174 events[6] |= 0x08; /* User Passkey Request */
3175 events[6] |= 0x10; /* Remote OOB Data Request */
3176 events[6] |= 0x20; /* Simple Pairing Complete */
3177 events[7] |= 0x04; /* User Passkey Notification */
3178 events[7] |= 0x08; /* Keypress Notification */
3179 events[7] |= 0x10; /* Remote Host Supported
3180 * Features Notification
3184 if (lmp_le_capable(hdev))
3185 events[7] |= 0x20; /* LE Meta-Event */
3187 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3188 sizeof(events), events, HCI_CMD_TIMEOUT);
3191 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3193 struct hci_cp_read_stored_link_key cp;
3195 if (!(hdev->commands[6] & 0x20) ||
3196 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3199 memset(&cp, 0, sizeof(cp));
3200 bacpy(&cp.bdaddr, BDADDR_ANY);
3203 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3204 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3207 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3209 struct hci_cp_write_def_link_policy cp;
3210 u16 link_policy = 0;
3212 if (!(hdev->commands[5] & 0x10))
3215 memset(&cp, 0, sizeof(cp));
3217 if (lmp_rswitch_capable(hdev))
3218 link_policy |= HCI_LP_RSWITCH;
3219 if (lmp_hold_capable(hdev))
3220 link_policy |= HCI_LP_HOLD;
3221 if (lmp_sniff_capable(hdev))
3222 link_policy |= HCI_LP_SNIFF;
3223 if (lmp_park_capable(hdev))
3224 link_policy |= HCI_LP_PARK;
3226 cp.policy = cpu_to_le16(link_policy);
3228 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3229 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3232 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3234 if (!(hdev->commands[8] & 0x01))
3237 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3238 0, NULL, HCI_CMD_TIMEOUT);
3241 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3243 if (!(hdev->commands[18] & 0x04) ||
3244 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING))
3247 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3248 0, NULL, HCI_CMD_TIMEOUT);
3251 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3253 /* Some older Broadcom based Bluetooth 1.2 controllers do not
3254 * support the Read Page Scan Type command. Check support for
3255 * this command in the bit mask of supported commands.
3257 if (!(hdev->commands[13] & 0x01))
3260 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3261 0, NULL, HCI_CMD_TIMEOUT);
3264 /* Read features beyond page 1 if available */
3265 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3270 if (!lmp_ext_feat_capable(hdev))
3273 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3275 err = hci_read_local_ext_features_sync(hdev, page);
3283 /* HCI Controller init stage 3 command sequence */
3284 static const struct hci_init_stage hci_init3[] = {
3285 /* HCI_OP_SET_EVENT_MASK */
3286 HCI_INIT(hci_set_event_mask_sync),
3287 /* HCI_OP_READ_STORED_LINK_KEY */
3288 HCI_INIT(hci_read_stored_link_key_sync),
3289 /* HCI_OP_WRITE_DEF_LINK_POLICY */
3290 HCI_INIT(hci_setup_link_policy_sync),
3291 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
3292 HCI_INIT(hci_read_page_scan_activity_sync),
3293 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
3294 HCI_INIT(hci_read_def_err_data_reporting_sync),
3295 /* HCI_OP_READ_PAGE_SCAN_TYPE */
3296 HCI_INIT(hci_read_page_scan_type_sync),
3297 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3298 HCI_INIT(hci_read_local_ext_features_all_sync),
3302 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
3306 if (!lmp_le_capable(hdev))
3309 memset(events, 0, sizeof(events));
3311 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
3312 events[0] |= 0x10; /* LE Long Term Key Request */
3314 /* If controller supports the Connection Parameters Request
3315 * Link Layer Procedure, enable the corresponding event.
3317 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
3318 /* LE Remote Connection Parameter Request */
3321 /* If the controller supports the Data Length Extension
3322 * feature, enable the corresponding event.
3324 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
3325 events[0] |= 0x40; /* LE Data Length Change */
3327 /* If the controller supports LL Privacy feature or LE Extended Adv,
3328 * enable the corresponding event.
3330 if (use_enhanced_conn_complete(hdev))
3331 events[1] |= 0x02; /* LE Enhanced Connection Complete */
3333 /* If the controller supports Extended Scanner Filter
3334 * Policies, enable the corresponding event.
3336 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
3337 events[1] |= 0x04; /* LE Direct Advertising Report */
3339 /* If the controller supports Channel Selection Algorithm #2
3340 * feature, enable the corresponding event.
3342 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
3343 events[2] |= 0x08; /* LE Channel Selection Algorithm */
3345 /* If the controller supports the LE Set Scan Enable command,
3346 * enable the corresponding advertising report event.
3348 if (hdev->commands[26] & 0x08)
3349 events[0] |= 0x02; /* LE Advertising Report */
3351 /* If the controller supports the LE Create Connection
3352 * command, enable the corresponding event.
3354 if (hdev->commands[26] & 0x10)
3355 events[0] |= 0x01; /* LE Connection Complete */
3357 /* If the controller supports the LE Connection Update
3358 * command, enable the corresponding event.
3360 if (hdev->commands[27] & 0x04)
3361 events[0] |= 0x04; /* LE Connection Update Complete */
3363 /* If the controller supports the LE Read Remote Used Features
3364 * command, enable the corresponding event.
3366 if (hdev->commands[27] & 0x20)
3367 /* LE Read Remote Used Features Complete */
3370 /* If the controller supports the LE Read Local P-256
3371 * Public Key command, enable the corresponding event.
3373 if (hdev->commands[34] & 0x02)
3374 /* LE Read Local P-256 Public Key Complete */
3377 /* If the controller supports the LE Generate DHKey
3378 * command, enable the corresponding event.
3380 if (hdev->commands[34] & 0x04)
3381 events[1] |= 0x01; /* LE Generate DHKey Complete */
3383 /* If the controller supports the LE Set Default PHY or
3384 * LE Set PHY commands, enable the corresponding event.
3386 if (hdev->commands[35] & (0x20 | 0x40))
3387 events[1] |= 0x08; /* LE PHY Update Complete */
3389 /* If the controller supports LE Set Extended Scan Parameters
3390 * and LE Set Extended Scan Enable commands, enable the
3391 * corresponding event.
3393 if (use_ext_scan(hdev))
3394 events[1] |= 0x10; /* LE Extended Advertising Report */
3396 /* If the controller supports the LE Extended Advertising
3397 * command, enable the corresponding event.
3399 if (ext_adv_capable(hdev))
3400 events[2] |= 0x02; /* LE Advertising Set Terminated */
3402 if (cis_capable(hdev)) {
3403 events[3] |= 0x01; /* LE CIS Established */
3404 if (cis_peripheral_capable(hdev))
3405 events[3] |= 0x02; /* LE CIS Request */
3408 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
3409 sizeof(events), events, HCI_CMD_TIMEOUT);
3412 /* Read LE Advertising Channel TX Power */
3413 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
3415 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
3416 /* HCI TS spec forbids mixing of legacy and extended
3417 * advertising commands wherein READ_ADV_TX_POWER is
3418 * also included. So do not call it if extended adv
3419 * is supported otherwise controller will return
3420 * COMMAND_DISALLOWED for extended commands.
3422 return __hci_cmd_sync_status(hdev,
3423 HCI_OP_LE_READ_ADV_TX_POWER,
3424 0, NULL, HCI_CMD_TIMEOUT);
3430 /* Read LE Min/Max Tx Power*/
3431 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
3433 if (!(hdev->commands[38] & 0x80) ||
3434 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
3437 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
3438 0, NULL, HCI_CMD_TIMEOUT);
3441 /* Read LE Accept List Size */
3442 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
3444 if (!(hdev->commands[26] & 0x40))
3447 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
3448 0, NULL, HCI_CMD_TIMEOUT);
3451 /* Clear LE Accept List */
3452 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
3454 if (!(hdev->commands[26] & 0x80))
3457 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
3461 /* Read LE Resolving List Size */
3462 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
3464 if (!(hdev->commands[34] & 0x40))
3467 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
3468 0, NULL, HCI_CMD_TIMEOUT);
3471 /* Clear LE Resolving List */
3472 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
3474 if (!(hdev->commands[34] & 0x20))
3477 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
3481 /* Set RPA timeout */
3482 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
3484 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
3486 if (!(hdev->commands[35] & 0x04))
3489 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
3490 sizeof(timeout), &timeout,
3494 /* Read LE Maximum Data Length */
3495 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
3497 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3500 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
3504 /* Read LE Suggested Default Data Length */
3505 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
3507 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3510 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
3514 /* Read LE Number of Supported Advertising Sets */
3515 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
3517 if (!ext_adv_capable(hdev))
3520 return __hci_cmd_sync_status(hdev,
3521 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
3522 0, NULL, HCI_CMD_TIMEOUT);
3525 /* Write LE Host Supported */
3526 static int hci_set_le_support_sync(struct hci_dev *hdev)
3528 struct hci_cp_write_le_host_supported cp;
3530 /* LE-only devices do not support explicit enablement */
3531 if (!lmp_bredr_capable(hdev))
3534 memset(&cp, 0, sizeof(cp));
3536 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
3541 if (cp.le == lmp_host_le_capable(hdev))
3544 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3545 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3548 /* LE Set Host Feature */
3549 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
3551 struct hci_cp_le_set_host_feature cp;
3553 if (!iso_capable(hdev))
3556 memset(&cp, 0, sizeof(cp));
3558 /* Isochronous Channels (Host Support) */
3562 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
3563 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3566 /* LE Controller init stage 3 command sequence */
3567 static const struct hci_init_stage le_init3[] = {
3568 /* HCI_OP_LE_SET_EVENT_MASK */
3569 HCI_INIT(hci_le_set_event_mask_sync),
3570 /* HCI_OP_LE_READ_ADV_TX_POWER */
3571 HCI_INIT(hci_le_read_adv_tx_power_sync),
3572 /* HCI_OP_LE_READ_TRANSMIT_POWER */
3573 HCI_INIT(hci_le_read_tx_power_sync),
3574 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
3575 HCI_INIT(hci_le_read_accept_list_size_sync),
3576 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
3577 HCI_INIT(hci_le_clear_accept_list_sync),
3578 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
3579 HCI_INIT(hci_le_read_resolv_list_size_sync),
3580 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
3581 HCI_INIT(hci_le_clear_resolv_list_sync),
3582 /* HCI_OP_LE_SET_RPA_TIMEOUT */
3583 HCI_INIT(hci_le_set_rpa_timeout_sync),
3584 /* HCI_OP_LE_READ_MAX_DATA_LEN */
3585 HCI_INIT(hci_le_read_max_data_len_sync),
3586 /* HCI_OP_LE_READ_DEF_DATA_LEN */
3587 HCI_INIT(hci_le_read_def_data_len_sync),
3588 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
3589 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
3590 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
3591 HCI_INIT(hci_set_le_support_sync),
3592 /* HCI_OP_LE_SET_HOST_FEATURE */
3593 HCI_INIT(hci_le_set_host_feature_sync),
3597 static int hci_init3_sync(struct hci_dev *hdev)
3601 bt_dev_dbg(hdev, "");
3603 err = hci_init_stage_sync(hdev, hci_init3);
3607 if (lmp_le_capable(hdev))
3608 return hci_init_stage_sync(hdev, le_init3);
3613 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
3615 struct hci_cp_delete_stored_link_key cp;
3617 /* Some Broadcom based Bluetooth controllers do not support the
3618 * Delete Stored Link Key command. They are clearly indicating its
3619 * absence in the bit mask of supported commands.
3621 * Check the supported commands and only if the command is marked
3622 * as supported send it. If not supported assume that the controller
3623 * does not have actual support for stored link keys which makes this
3624 * command redundant anyway.
3626 * Some controllers indicate that they support handling deleting
3627 * stored link keys, but they don't. The quirk lets a driver
3628 * just disable this command.
3630 if (!(hdev->commands[6] & 0x80) ||
3631 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3634 memset(&cp, 0, sizeof(cp));
3635 bacpy(&cp.bdaddr, BDADDR_ANY);
3636 cp.delete_all = 0x01;
3638 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
3639 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3642 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
3644 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3645 bool changed = false;
3647 /* Set event mask page 2 if the HCI command for it is supported */
3648 if (!(hdev->commands[22] & 0x04))
3651 /* If Connectionless Peripheral Broadcast central role is supported
3652 * enable all necessary events for it.
3654 if (lmp_cpb_central_capable(hdev)) {
3655 events[1] |= 0x40; /* Triggered Clock Capture */
3656 events[1] |= 0x80; /* Synchronization Train Complete */
3657 events[2] |= 0x08; /* Truncated Page Complete */
3658 events[2] |= 0x20; /* CPB Channel Map Change */
3662 /* If Connectionless Peripheral Broadcast peripheral role is supported
3663 * enable all necessary events for it.
3665 if (lmp_cpb_peripheral_capable(hdev)) {
3666 events[2] |= 0x01; /* Synchronization Train Received */
3667 events[2] |= 0x02; /* CPB Receive */
3668 events[2] |= 0x04; /* CPB Timeout */
3669 events[2] |= 0x10; /* Peripheral Page Response Timeout */
3673 /* Enable Authenticated Payload Timeout Expired event if supported */
3674 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
3679 /* Some Broadcom based controllers indicate support for Set Event
3680 * Mask Page 2 command, but then actually do not support it. Since
3681 * the default value is all bits set to zero, the command is only
3682 * required if the event mask has to be changed. In case no change
3683 * to the event mask is needed, skip this command.
3688 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
3689 sizeof(events), events, HCI_CMD_TIMEOUT);
3692 /* Read local codec list if the HCI command is supported */
3693 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
3695 if (!(hdev->commands[29] & 0x20))
3698 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_CODECS, 0, NULL,
3702 /* Read local pairing options if the HCI command is supported */
3703 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
3705 if (!(hdev->commands[41] & 0x08))
3708 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
3709 0, NULL, HCI_CMD_TIMEOUT);
3712 /* Get MWS transport configuration if the HCI command is supported */
3713 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
3715 if (!(hdev->commands[30] & 0x08))
3718 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
3719 0, NULL, HCI_CMD_TIMEOUT);
3722 /* Check for Synchronization Train support */
3723 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
3725 if (!lmp_sync_train_capable(hdev))
3728 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
3729 0, NULL, HCI_CMD_TIMEOUT);
3732 /* Enable Secure Connections if supported and configured */
3733 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
3737 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3738 !bredr_sc_enabled(hdev))
3741 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3742 sizeof(support), &support,
3746 /* Set erroneous data reporting if supported to the wideband speech
3749 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
3751 struct hci_cp_write_def_err_data_reporting cp;
3752 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
3754 if (!(hdev->commands[18] & 0x08) ||
3755 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING))
3758 if (enabled == hdev->err_data_reporting)
3761 memset(&cp, 0, sizeof(cp));
3762 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
3763 ERR_DATA_REPORTING_DISABLED;
3765 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
3766 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3769 static const struct hci_init_stage hci_init4[] = {
3770 /* HCI_OP_DELETE_STORED_LINK_KEY */
3771 HCI_INIT(hci_delete_stored_link_key_sync),
3772 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
3773 HCI_INIT(hci_set_event_mask_page_2_sync),
3774 /* HCI_OP_READ_LOCAL_CODECS */
3775 HCI_INIT(hci_read_local_codecs_sync),
3776 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
3777 HCI_INIT(hci_read_local_pairing_opts_sync),
3778 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
3779 HCI_INIT(hci_get_mws_transport_config_sync),
3780 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
3781 HCI_INIT(hci_read_sync_train_params_sync),
3782 /* HCI_OP_WRITE_SC_SUPPORT */
3783 HCI_INIT(hci_write_sc_support_1_sync),
3784 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
3785 HCI_INIT(hci_set_err_data_report_sync),
3789 /* Set Suggested Default Data Length to maximum if supported */
3790 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
3792 struct hci_cp_le_write_def_data_len cp;
3794 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3797 memset(&cp, 0, sizeof(cp));
3798 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
3799 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
3801 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
3802 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3805 /* Set Default PHY parameters if command is supported */
3806 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
3808 struct hci_cp_le_set_default_phy cp;
3810 if (!(hdev->commands[35] & 0x20))
3813 memset(&cp, 0, sizeof(cp));
3815 cp.tx_phys = hdev->le_tx_def_phys;
3816 cp.rx_phys = hdev->le_rx_def_phys;
3818 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
3819 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3822 static const struct hci_init_stage le_init4[] = {
3823 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
3824 HCI_INIT(hci_le_set_write_def_data_len_sync),
3825 /* HCI_OP_LE_SET_DEFAULT_PHY */
3826 HCI_INIT(hci_le_set_default_phy_sync),
3830 static int hci_init4_sync(struct hci_dev *hdev)
3834 bt_dev_dbg(hdev, "");
3836 err = hci_init_stage_sync(hdev, hci_init4);
3840 if (lmp_le_capable(hdev))
3841 return hci_init_stage_sync(hdev, le_init4);
3846 static int hci_init_sync(struct hci_dev *hdev)
3850 err = hci_init1_sync(hdev);
3854 if (hci_dev_test_flag(hdev, HCI_SETUP))
3855 hci_debugfs_create_basic(hdev);
3857 err = hci_init2_sync(hdev);
3861 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
3862 * BR/EDR/LE type controllers. AMP controllers only need the
3863 * first two stages of init.
3865 if (hdev->dev_type != HCI_PRIMARY)
3868 err = hci_init3_sync(hdev);
3872 err = hci_init4_sync(hdev);
3876 /* This function is only called when the controller is actually in
3877 * configured state. When the controller is marked as unconfigured,
3878 * this initialization procedure is not run.
3880 * It means that it is possible that a controller runs through its
3881 * setup phase and then discovers missing settings. If that is the
3882 * case, then this function will not be called. It then will only
3883 * be called during the config phase.
3885 * So only when in setup phase or config phase, create the debugfs
3886 * entries and register the SMP channels.
3888 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3889 !hci_dev_test_flag(hdev, HCI_CONFIG))
3892 hci_debugfs_create_common(hdev);
3894 if (lmp_bredr_capable(hdev))
3895 hci_debugfs_create_bredr(hdev);
3897 if (lmp_le_capable(hdev))
3898 hci_debugfs_create_le(hdev);
3903 #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
3905 static const struct {
3906 unsigned long quirk;
3908 } hci_broken_table[] = {
3909 HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
3910 "HCI Read Local Supported Commands not supported"),
3911 HCI_QUIRK_BROKEN(STORED_LINK_KEY,
3912 "HCI Delete Stored Link Key command is advertised, "
3913 "but not supported."),
3914 HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
3915 "HCI Read Transmit Power Level command is advertised, "
3916 "but not supported."),
3917 HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
3918 "HCI Set Event Filter command not supported."),
3919 HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
3920 "HCI Enhanced Setup Synchronous Connection command is "
3921 "advertised, but not supported.")
3924 /* This function handles hdev setup stage:
3927 * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
3929 static int hci_dev_setup_sync(struct hci_dev *hdev)
3932 bool invalid_bdaddr;
3935 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3936 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
3939 bt_dev_dbg(hdev, "");
3941 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
3944 ret = hdev->setup(hdev);
3946 for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
3947 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
3948 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
3951 /* The transport driver can set the quirk to mark the
3952 * BD_ADDR invalid before creating the HCI device or in
3953 * its setup callback.
3955 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
3958 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) {
3959 if (!bacmp(&hdev->public_addr, BDADDR_ANY))
3960 hci_dev_get_bd_addr_from_property(hdev);
3962 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
3964 ret = hdev->set_bdaddr(hdev,
3965 &hdev->public_addr);
3967 /* If setting of the BD_ADDR from the device
3968 * property succeeds, then treat the address
3969 * as valid even if the invalid BD_ADDR
3970 * quirk indicates otherwise.
3973 invalid_bdaddr = false;
3978 /* The transport driver can set these quirks before
3979 * creating the HCI device or in its setup callback.
3981 * For the invalid BD_ADDR quirk it is possible that
3982 * it becomes a valid address if the bootloader does
3983 * provide it (see above).
3985 * In case any of them is set, the controller has to
3986 * start up as unconfigured.
3988 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
3990 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
3992 /* For an unconfigured controller it is required to
3993 * read at least the version information provided by
3994 * the Read Local Version Information command.
3996 * If the set_bdaddr driver callback is provided, then
3997 * also the original Bluetooth public device address
3998 * will be read using the Read BD Address command.
4000 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4001 return hci_unconf_init_sync(hdev);
4006 /* This function handles hdev init stage:
4008 * Calls hci_dev_setup_sync to perform setup stage
4009 * Calls hci_init_sync to perform HCI command init sequence
4011 static int hci_dev_init_sync(struct hci_dev *hdev)
4015 bt_dev_dbg(hdev, "");
4017 atomic_set(&hdev->cmd_cnt, 1);
4018 set_bit(HCI_INIT, &hdev->flags);
4020 ret = hci_dev_setup_sync(hdev);
4022 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4023 /* If public address change is configured, ensure that
4024 * the address gets programmed. If the driver does not
4025 * support changing the public address, fail the power
4028 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4030 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4032 ret = -EADDRNOTAVAIL;
4036 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4037 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4038 ret = hci_init_sync(hdev);
4039 if (!ret && hdev->post_init)
4040 ret = hdev->post_init(hdev);
4044 /* If the HCI Reset command is clearing all diagnostic settings,
4045 * then they need to be reprogrammed after the init procedure
4048 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4049 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4050 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4051 ret = hdev->set_diag(hdev, true);
4053 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4058 clear_bit(HCI_INIT, &hdev->flags);
4063 int hci_dev_open_sync(struct hci_dev *hdev)
4067 bt_dev_dbg(hdev, "");
4069 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4074 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4075 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4076 /* Check for rfkill but allow the HCI setup stage to
4077 * proceed (which in itself doesn't cause any RF activity).
4079 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4084 /* Check for valid public address or a configured static
4085 * random address, but let the HCI setup proceed to
4086 * be able to determine if there is a public address
4089 * In case of user channel usage, it is not important
4090 * if a public address or static random address is
4093 * This check is only valid for BR/EDR controllers
4094 * since AMP controllers do not have an address.
4096 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4097 hdev->dev_type == HCI_PRIMARY &&
4098 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4099 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4100 ret = -EADDRNOTAVAIL;
4105 if (test_bit(HCI_UP, &hdev->flags)) {
4110 if (hdev->open(hdev)) {
4115 set_bit(HCI_RUNNING, &hdev->flags);
4116 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4118 ret = hci_dev_init_sync(hdev);
4121 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4122 hci_adv_instances_set_rpa_expired(hdev, true);
4123 set_bit(HCI_UP, &hdev->flags);
4124 hci_sock_dev_event(hdev, HCI_DEV_UP);
4125 hci_leds_update_powered(hdev, true);
4126 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4127 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4128 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4129 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4130 hci_dev_test_flag(hdev, HCI_MGMT) &&
4131 hdev->dev_type == HCI_PRIMARY) {
4132 ret = hci_powered_update_sync(hdev);
4135 /* Init failed, cleanup */
4136 flush_work(&hdev->tx_work);
4138 /* Since hci_rx_work() is possible to awake new cmd_work
4139 * it should be flushed first to avoid unexpected call of
4142 flush_work(&hdev->rx_work);
4143 flush_work(&hdev->cmd_work);
4145 skb_queue_purge(&hdev->cmd_q);
4146 skb_queue_purge(&hdev->rx_q);
4151 if (hdev->sent_cmd) {
4152 kfree_skb(hdev->sent_cmd);
4153 hdev->sent_cmd = NULL;
4156 clear_bit(HCI_RUNNING, &hdev->flags);
4157 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4160 hdev->flags &= BIT(HCI_RAW);
4167 /* This function requires the caller holds hdev->lock */
4168 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4170 struct hci_conn_params *p;
4172 list_for_each_entry(p, &hdev->le_conn_params, list) {
4174 hci_conn_drop(p->conn);
4175 hci_conn_put(p->conn);
4178 list_del_init(&p->action);
4181 BT_DBG("All LE pending actions cleared");
4184 int hci_dev_close_sync(struct hci_dev *hdev)
4189 bt_dev_dbg(hdev, "");
4191 cancel_delayed_work(&hdev->power_off);
4192 cancel_delayed_work(&hdev->ncmd_timer);
4194 hci_request_cancel_all(hdev);
4196 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4197 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4198 test_bit(HCI_UP, &hdev->flags)) {
4199 /* Execute vendor specific shutdown routine */
4201 err = hdev->shutdown(hdev);
4204 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4205 cancel_delayed_work_sync(&hdev->cmd_timer);
4209 hci_leds_update_powered(hdev, false);
4211 /* Flush RX and TX works */
4212 flush_work(&hdev->tx_work);
4213 flush_work(&hdev->rx_work);
4215 if (hdev->discov_timeout > 0) {
4216 hdev->discov_timeout = 0;
4217 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4218 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
4221 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
4222 cancel_delayed_work(&hdev->service_cache);
4224 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4225 struct adv_info *adv_instance;
4227 cancel_delayed_work_sync(&hdev->rpa_expired);
4229 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
4230 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
4233 /* Avoid potential lockdep warnings from the *_flush() calls by
4234 * ensuring the workqueue is empty up front.
4236 drain_workqueue(hdev->workqueue);
4240 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4242 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
4244 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
4245 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4246 hci_dev_test_flag(hdev, HCI_MGMT))
4247 __mgmt_power_off(hdev);
4249 hci_inquiry_cache_flush(hdev);
4250 hci_pend_le_actions_clear(hdev);
4251 hci_conn_hash_flush(hdev);
4252 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
4253 smp_unregister(hdev);
4254 hci_dev_unlock(hdev);
4256 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
4258 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4259 aosp_do_close(hdev);
4260 msft_do_close(hdev);
4267 skb_queue_purge(&hdev->cmd_q);
4268 atomic_set(&hdev->cmd_cnt, 1);
4269 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
4270 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4271 set_bit(HCI_INIT, &hdev->flags);
4272 hci_reset_sync(hdev);
4273 clear_bit(HCI_INIT, &hdev->flags);
4276 /* flush cmd work */
4277 flush_work(&hdev->cmd_work);
4280 skb_queue_purge(&hdev->rx_q);
4281 skb_queue_purge(&hdev->cmd_q);
4282 skb_queue_purge(&hdev->raw_q);
4284 /* Drop last sent command */
4285 if (hdev->sent_cmd) {
4286 cancel_delayed_work_sync(&hdev->cmd_timer);
4287 kfree_skb(hdev->sent_cmd);
4288 hdev->sent_cmd = NULL;
4291 clear_bit(HCI_RUNNING, &hdev->flags);
4292 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4294 /* After this point our queues are empty and no tasks are scheduled. */
4298 hdev->flags &= BIT(HCI_RAW);
4299 hci_dev_clear_volatile_flags(hdev);
4301 /* Controller radio is available but is currently powered down */
4302 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
4304 memset(hdev->eir, 0, sizeof(hdev->eir));
4305 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
4306 bacpy(&hdev->random_addr, BDADDR_ANY);
4312 /* This function perform power on HCI command sequence as follows:
4314 * If controller is already up (HCI_UP) performs hci_powered_update_sync
4315 * sequence otherwise run hci_dev_open_sync which will follow with
4316 * hci_powered_update_sync after the init sequence is completed.
4318 static int hci_power_on_sync(struct hci_dev *hdev)
4322 if (test_bit(HCI_UP, &hdev->flags) &&
4323 hci_dev_test_flag(hdev, HCI_MGMT) &&
4324 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
4325 cancel_delayed_work(&hdev->power_off);
4326 return hci_powered_update_sync(hdev);
4329 err = hci_dev_open_sync(hdev);
4333 /* During the HCI setup phase, a few error conditions are
4334 * ignored and they need to be checked now. If they are still
4335 * valid, it is important to return the device back off.
4337 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
4338 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
4339 (hdev->dev_type == HCI_PRIMARY &&
4340 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4341 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
4342 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
4343 hci_dev_close_sync(hdev);
4344 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
4345 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
4346 HCI_AUTO_OFF_TIMEOUT);
4349 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
4350 /* For unconfigured devices, set the HCI_RAW flag
4351 * so that userspace can easily identify them.
4353 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4354 set_bit(HCI_RAW, &hdev->flags);
4356 /* For fully configured devices, this will send
4357 * the Index Added event. For unconfigured devices,
4358 * it will send Unconfigued Index Added event.
4360 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
4361 * and no event will be send.
4363 mgmt_index_added(hdev);
4364 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
4365 /* When the controller is now configured, then it
4366 * is important to clear the HCI_RAW flag.
4368 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4369 clear_bit(HCI_RAW, &hdev->flags);
4371 /* Powering on the controller with HCI_CONFIG set only
4372 * happens with the transition from unconfigured to
4373 * configured. This will send the Index Added event.
4375 mgmt_index_added(hdev);
4381 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
4383 struct hci_cp_remote_name_req_cancel cp;
4385 memset(&cp, 0, sizeof(cp));
4386 bacpy(&cp.bdaddr, addr);
4388 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
4389 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4392 int hci_stop_discovery_sync(struct hci_dev *hdev)
4394 struct discovery_state *d = &hdev->discovery;
4395 struct inquiry_entry *e;
4398 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
4400 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
4401 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
4402 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
4403 0, NULL, HCI_CMD_TIMEOUT);
4408 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
4409 cancel_delayed_work(&hdev->le_scan_disable);
4410 cancel_delayed_work(&hdev->le_scan_restart);
4412 err = hci_scan_disable_sync(hdev);
4418 err = hci_scan_disable_sync(hdev);
4423 /* Resume advertising if it was paused */
4424 if (use_ll_privacy(hdev))
4425 hci_resume_advertising_sync(hdev);
4427 /* No further actions needed for LE-only discovery */
4428 if (d->type == DISCOV_TYPE_LE)
4431 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
4432 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
4437 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
4443 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
4446 struct hci_cp_disconn_phy_link cp;
4448 memset(&cp, 0, sizeof(cp));
4449 cp.phy_handle = HCI_PHY_HANDLE(handle);
4452 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
4453 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4456 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
4459 struct hci_cp_disconnect cp;
4461 if (conn->type == AMP_LINK)
4462 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
4464 memset(&cp, 0, sizeof(cp));
4465 cp.handle = cpu_to_le16(conn->handle);
4468 /* Wait for HCI_EV_DISCONN_COMPLETE not HCI_EV_CMD_STATUS when not
4471 if (!hdev->suspended)
4472 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
4474 HCI_EV_DISCONN_COMPLETE,
4475 HCI_CMD_TIMEOUT, NULL);
4477 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
4481 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
4482 struct hci_conn *conn)
4484 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
4487 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
4488 6, &conn->dst, HCI_CMD_TIMEOUT);
4491 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn)
4493 if (conn->type == LE_LINK)
4494 return hci_le_connect_cancel_sync(hdev, conn);
4496 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
4499 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
4500 6, &conn->dst, HCI_CMD_TIMEOUT);
4503 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
4506 struct hci_cp_reject_sync_conn_req cp;
4508 memset(&cp, 0, sizeof(cp));
4509 bacpy(&cp.bdaddr, &conn->dst);
4512 /* SCO rejection has its own limited set of
4513 * allowed error values (0x0D-0x0F).
4515 if (reason < 0x0d || reason > 0x0f)
4516 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
4518 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
4519 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4522 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
4525 struct hci_cp_reject_conn_req cp;
4527 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
4528 return hci_reject_sco_sync(hdev, conn, reason);
4530 memset(&cp, 0, sizeof(cp));
4531 bacpy(&cp.bdaddr, &conn->dst);
4534 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
4535 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4538 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
4542 switch (conn->state) {
4545 return hci_disconnect_sync(hdev, conn, reason);
4547 err = hci_connect_cancel_sync(hdev, conn);
4548 /* Cleanup hci_conn object if it cannot be cancelled as it
4549 * likelly means the controller and host stack are out of sync.
4552 hci_conn_failed(conn, err);
4556 return hci_reject_conn_sync(hdev, conn, reason);
4558 conn->state = BT_CLOSED;
4565 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
4567 struct hci_conn *conn, *tmp;
4570 list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
4571 err = hci_abort_conn_sync(hdev, conn, reason);
4579 /* This function perform power off HCI command sequence as follows:
4583 * Disconnect all connections
4584 * hci_dev_close_sync
4586 static int hci_power_off_sync(struct hci_dev *hdev)
4590 /* If controller is already down there is nothing to do */
4591 if (!test_bit(HCI_UP, &hdev->flags))
4594 if (test_bit(HCI_ISCAN, &hdev->flags) ||
4595 test_bit(HCI_PSCAN, &hdev->flags)) {
4596 err = hci_write_scan_enable_sync(hdev, 0x00);
4601 err = hci_clear_adv_sync(hdev, NULL, false);
4605 err = hci_stop_discovery_sync(hdev);
4609 /* Terminated due to Power Off */
4610 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
4614 return hci_dev_close_sync(hdev);
4617 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
4620 return hci_power_on_sync(hdev);
4622 return hci_power_off_sync(hdev);
4625 static int hci_write_iac_sync(struct hci_dev *hdev)
4627 struct hci_cp_write_current_iac_lap cp;
4629 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
4632 memset(&cp, 0, sizeof(cp));
4634 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
4635 /* Limited discoverable mode */
4636 cp.num_iac = min_t(u8, hdev->num_iac, 2);
4637 cp.iac_lap[0] = 0x00; /* LIAC */
4638 cp.iac_lap[1] = 0x8b;
4639 cp.iac_lap[2] = 0x9e;
4640 cp.iac_lap[3] = 0x33; /* GIAC */
4641 cp.iac_lap[4] = 0x8b;
4642 cp.iac_lap[5] = 0x9e;
4644 /* General discoverable mode */
4646 cp.iac_lap[0] = 0x33; /* GIAC */
4647 cp.iac_lap[1] = 0x8b;
4648 cp.iac_lap[2] = 0x9e;
4651 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
4652 (cp.num_iac * 3) + 1, &cp,
4656 int hci_update_discoverable_sync(struct hci_dev *hdev)
4660 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
4661 err = hci_write_iac_sync(hdev);
4665 err = hci_update_scan_sync(hdev);
4669 err = hci_update_class_sync(hdev);
4674 /* Advertising instances don't use the global discoverable setting, so
4675 * only update AD if advertising was enabled using Set Advertising.
4677 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
4678 err = hci_update_adv_data_sync(hdev, 0x00);
4682 /* Discoverable mode affects the local advertising
4683 * address in limited privacy mode.
4685 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
4686 if (ext_adv_capable(hdev))
4687 err = hci_start_ext_adv_sync(hdev, 0x00);
4689 err = hci_enable_advertising_sync(hdev);
4696 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
4698 return hci_update_discoverable_sync(hdev);
4701 int hci_update_discoverable(struct hci_dev *hdev)
4703 /* Only queue if it would have any effect */
4704 if (hdev_is_powered(hdev) &&
4705 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4706 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
4707 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
4708 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
4714 int hci_update_connectable_sync(struct hci_dev *hdev)
4718 err = hci_update_scan_sync(hdev);
4722 /* If BR/EDR is not enabled and we disable advertising as a
4723 * by-product of disabling connectable, we need to update the
4724 * advertising flags.
4726 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4727 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
4729 /* Update the advertising parameters if necessary */
4730 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4731 !list_empty(&hdev->adv_instances)) {
4732 if (ext_adv_capable(hdev))
4733 err = hci_start_ext_adv_sync(hdev,
4734 hdev->cur_adv_instance);
4736 err = hci_enable_advertising_sync(hdev);
4742 return hci_update_passive_scan_sync(hdev);
4745 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
4747 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
4748 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
4749 struct hci_cp_inquiry cp;
4751 bt_dev_dbg(hdev, "");
4753 if (hci_dev_test_flag(hdev, HCI_INQUIRY))
4757 hci_inquiry_cache_flush(hdev);
4758 hci_dev_unlock(hdev);
4760 memset(&cp, 0, sizeof(cp));
4762 if (hdev->discovery.limited)
4763 memcpy(&cp.lap, liac, sizeof(cp.lap));
4765 memcpy(&cp.lap, giac, sizeof(cp.lap));
4769 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
4770 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4773 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
4776 /* Accept list is not used for discovery */
4777 u8 filter_policy = 0x00;
4778 /* Default is to enable duplicates filter */
4779 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
4782 bt_dev_dbg(hdev, "");
4784 /* If controller is scanning, it means the passive scanning is
4785 * running. Thus, we should temporarily stop it in order to set the
4786 * discovery scanning parameters.
4788 err = hci_scan_disable_sync(hdev);
4790 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
4794 cancel_interleave_scan(hdev);
4796 /* Pause advertising since active scanning disables address resolution
4797 * which advertising depend on in order to generate its RPAs.
4799 if (use_ll_privacy(hdev)) {
4800 err = hci_pause_advertising_sync(hdev);
4802 bt_dev_err(hdev, "pause advertising failed: %d", err);
4807 /* Disable address resolution while doing active scanning since the
4808 * accept list shall not be used and all reports shall reach the host
4811 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
4813 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
4818 /* All active scans will be done with either a resolvable private
4819 * address (when privacy feature has been enabled) or non-resolvable
4822 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
4825 own_addr_type = ADDR_LE_DEV_PUBLIC;
4827 if (hci_is_adv_monitoring(hdev)) {
4828 /* Duplicate filter should be disabled when some advertisement
4829 * monitor is activated, otherwise AdvMon can only receive one
4830 * advertisement for one peer(*) during active scanning, and
4831 * might report loss to these peers.
4833 * Note that different controllers have different meanings of
4834 * |duplicate|. Some of them consider packets with the same
4835 * address as duplicate, and others consider packets with the
4836 * same address and the same RSSI as duplicate. Although in the
4837 * latter case we don't need to disable duplicate filter, but
4838 * it is common to have active scanning for a short period of
4839 * time, the power impact should be neglectable.
4841 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
4844 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
4845 hdev->le_scan_window_discovery,
4846 own_addr_type, filter_policy, filter_dup);
4851 /* Resume advertising if it was paused */
4852 if (use_ll_privacy(hdev))
4853 hci_resume_advertising_sync(hdev);
4855 /* Resume passive scanning */
4856 hci_update_passive_scan_sync(hdev);
4860 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
4864 bt_dev_dbg(hdev, "");
4866 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
4870 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
4873 int hci_start_discovery_sync(struct hci_dev *hdev)
4875 unsigned long timeout;
4878 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
4880 switch (hdev->discovery.type) {
4881 case DISCOV_TYPE_BREDR:
4882 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
4883 case DISCOV_TYPE_INTERLEAVED:
4884 /* When running simultaneous discovery, the LE scanning time
4885 * should occupy the whole discovery time sine BR/EDR inquiry
4886 * and LE scanning are scheduled by the controller.
4888 * For interleaving discovery in comparison, BR/EDR inquiry
4889 * and LE scanning are done sequentially with separate
4892 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
4894 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4895 /* During simultaneous discovery, we double LE scan
4896 * interval. We must leave some time for the controller
4897 * to do BR/EDR inquiry.
4899 err = hci_start_interleaved_discovery_sync(hdev);
4903 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
4904 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
4906 case DISCOV_TYPE_LE:
4907 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4908 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
4917 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
4919 /* When service discovery is used and the controller has a
4920 * strict duplicate filter, it is important to remember the
4921 * start and duration of the scan. This is required for
4922 * restarting scanning during the discovery phase.
4924 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
4925 hdev->discovery.result_filtering) {
4926 hdev->discovery.scan_start = jiffies;
4927 hdev->discovery.scan_duration = timeout;
4930 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
4935 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
4937 switch (hci_get_adv_monitor_offload_ext(hdev)) {
4938 case HCI_ADV_MONITOR_EXT_MSFT:
4939 msft_suspend_sync(hdev);
4946 /* This function disables discovery and mark it as paused */
4947 static int hci_pause_discovery_sync(struct hci_dev *hdev)
4949 int old_state = hdev->discovery.state;
4952 /* If discovery already stopped/stopping/paused there nothing to do */
4953 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
4954 hdev->discovery_paused)
4957 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
4958 err = hci_stop_discovery_sync(hdev);
4962 hdev->discovery_paused = true;
4963 hdev->discovery_old_state = old_state;
4964 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4969 static int hci_update_event_filter_sync(struct hci_dev *hdev)
4971 struct bdaddr_list_with_flags *b;
4972 u8 scan = SCAN_DISABLED;
4973 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
4976 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4979 /* Some fake CSR controllers lock up after setting this type of
4980 * filter, so avoid sending the request altogether.
4982 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
4985 /* Always clear event filter when starting */
4986 hci_clear_event_filter_sync(hdev);
4988 list_for_each_entry(b, &hdev->accept_list, list) {
4989 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
4992 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
4994 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
4995 HCI_CONN_SETUP_ALLOW_BDADDR,
4997 HCI_CONN_SETUP_AUTO_ON);
4999 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5005 if (scan && !scanning)
5006 hci_write_scan_enable_sync(hdev, scan);
5007 else if (!scan && scanning)
5008 hci_write_scan_enable_sync(hdev, scan);
5013 /* This function disables scan (BR and LE) and mark it as paused */
5014 static int hci_pause_scan_sync(struct hci_dev *hdev)
5016 if (hdev->scanning_paused)
5019 /* Disable page scan if enabled */
5020 if (test_bit(HCI_PSCAN, &hdev->flags))
5021 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
5023 hci_scan_disable_sync(hdev);
5025 hdev->scanning_paused = true;
5030 /* This function performs the HCI suspend procedures in the follow order:
5032 * Pause discovery (active scanning/inquiry)
5033 * Pause Directed Advertising/Advertising
5034 * Pause Scanning (passive scanning in case discovery was not active)
5035 * Disconnect all connections
5036 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5038 * Update event mask (only set events that are allowed to wake up the host)
5039 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
5040 * Update passive scanning (lower duty cycle)
5041 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
5043 int hci_suspend_sync(struct hci_dev *hdev)
5047 /* If marked as suspended there nothing to do */
5048 if (hdev->suspended)
5051 /* Mark device as suspended */
5052 hdev->suspended = true;
5054 /* Pause discovery if not already stopped */
5055 hci_pause_discovery_sync(hdev);
5057 /* Pause other advertisements */
5058 hci_pause_advertising_sync(hdev);
5060 /* Suspend monitor filters */
5061 hci_suspend_monitor_sync(hdev);
5063 /* Prevent disconnects from causing scanning to be re-enabled */
5064 hci_pause_scan_sync(hdev);
5066 /* Soft disconnect everything (power off) */
5067 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5069 /* Set state to BT_RUNNING so resume doesn't notify */
5070 hdev->suspend_state = BT_RUNNING;
5071 hci_resume_sync(hdev);
5075 /* Only configure accept list if disconnect succeeded and wake
5076 * isn't being prevented.
5078 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5079 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5083 /* Unpause to take care of updating scanning params */
5084 hdev->scanning_paused = false;
5086 /* Update event mask so only the allowed event can wakeup the host */
5087 hci_set_event_mask_sync(hdev);
5089 /* Enable event filter for paired devices */
5090 hci_update_event_filter_sync(hdev);
5092 /* Update LE passive scan if enabled */
5093 hci_update_passive_scan_sync(hdev);
5095 /* Pause scan changes again. */
5096 hdev->scanning_paused = true;
5098 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5103 /* This function resumes discovery */
5104 static int hci_resume_discovery_sync(struct hci_dev *hdev)
5108 /* If discovery not paused there nothing to do */
5109 if (!hdev->discovery_paused)
5112 hdev->discovery_paused = false;
5114 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
5116 err = hci_start_discovery_sync(hdev);
5118 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
5124 static void hci_resume_monitor_sync(struct hci_dev *hdev)
5126 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5127 case HCI_ADV_MONITOR_EXT_MSFT:
5128 msft_resume_sync(hdev);
5135 /* This function resume scan and reset paused flag */
5136 static int hci_resume_scan_sync(struct hci_dev *hdev)
5138 if (!hdev->scanning_paused)
5141 hdev->scanning_paused = false;
5143 hci_update_scan_sync(hdev);
5145 /* Reset passive scanning to normal */
5146 hci_update_passive_scan_sync(hdev);
5151 /* This function performs the HCI suspend procedures in the follow order:
5153 * Restore event mask
5154 * Clear event filter
5155 * Update passive scanning (normal duty cycle)
5156 * Resume Directed Advertising/Advertising
5157 * Resume discovery (active scanning/inquiry)
5159 int hci_resume_sync(struct hci_dev *hdev)
5161 /* If not marked as suspended there nothing to do */
5162 if (!hdev->suspended)
5165 hdev->suspended = false;
5167 /* Restore event mask */
5168 hci_set_event_mask_sync(hdev);
5170 /* Clear any event filters and restore scan state */
5171 hci_clear_event_filter_sync(hdev);
5173 /* Resume scanning */
5174 hci_resume_scan_sync(hdev);
5176 /* Resume monitor filters */
5177 hci_resume_monitor_sync(hdev);
5179 /* Resume other advertisements */
5180 hci_resume_advertising_sync(hdev);
5182 /* Resume discovery */
5183 hci_resume_discovery_sync(hdev);
5188 static bool conn_use_rpa(struct hci_conn *conn)
5190 struct hci_dev *hdev = conn->hdev;
5192 return hci_dev_test_flag(hdev, HCI_PRIVACY);
5195 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
5196 struct hci_conn *conn)
5198 struct hci_cp_le_set_ext_adv_params cp;
5200 bdaddr_t random_addr;
5203 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5208 /* Set require_privacy to false so that the remote device has a
5209 * chance of identifying us.
5211 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
5212 &own_addr_type, &random_addr);
5216 memset(&cp, 0, sizeof(cp));
5218 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
5219 cp.own_addr_type = own_addr_type;
5220 cp.channel_map = hdev->le_adv_channel_map;
5221 cp.tx_power = HCI_TX_POWER_INVALID;
5222 cp.primary_phy = HCI_ADV_PHY_1M;
5223 cp.secondary_phy = HCI_ADV_PHY_1M;
5224 cp.handle = 0x00; /* Use instance 0 for directed adv */
5225 cp.own_addr_type = own_addr_type;
5226 cp.peer_addr_type = conn->dst_type;
5227 bacpy(&cp.peer_addr, &conn->dst);
5229 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
5230 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
5231 * does not supports advertising data when the advertising set already
5232 * contains some, the controller shall return erroc code 'Invalid
5233 * HCI Command Parameters(0x12).
5234 * So it is required to remove adv set for handle 0x00. since we use
5235 * instance 0 for directed adv.
5237 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
5241 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
5242 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5246 /* Check if random address need to be updated */
5247 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
5248 bacmp(&random_addr, BDADDR_ANY) &&
5249 bacmp(&random_addr, &hdev->random_addr)) {
5250 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
5256 return hci_enable_ext_advertising_sync(hdev, 0x00);
5259 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
5260 struct hci_conn *conn)
5262 struct hci_cp_le_set_adv_param cp;
5267 if (ext_adv_capable(hdev))
5268 return hci_le_ext_directed_advertising_sync(hdev, conn);
5270 /* Clear the HCI_LE_ADV bit temporarily so that the
5271 * hci_update_random_address knows that it's safe to go ahead
5272 * and write a new random address. The flag will be set back on
5273 * as soon as the SET_ADV_ENABLE HCI command completes.
5275 hci_dev_clear_flag(hdev, HCI_LE_ADV);
5277 /* Set require_privacy to false so that the remote device has a
5278 * chance of identifying us.
5280 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5285 memset(&cp, 0, sizeof(cp));
5287 /* Some controllers might reject command if intervals are not
5288 * within range for undirected advertising.
5289 * BCM20702A0 is known to be affected by this.
5291 cp.min_interval = cpu_to_le16(0x0020);
5292 cp.max_interval = cpu_to_le16(0x0020);
5294 cp.type = LE_ADV_DIRECT_IND;
5295 cp.own_address_type = own_addr_type;
5296 cp.direct_addr_type = conn->dst_type;
5297 bacpy(&cp.direct_addr, &conn->dst);
5298 cp.channel_map = hdev->le_adv_channel_map;
5300 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
5301 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5307 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
5308 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
5311 static void set_ext_conn_params(struct hci_conn *conn,
5312 struct hci_cp_le_ext_conn_param *p)
5314 struct hci_dev *hdev = conn->hdev;
5316 memset(p, 0, sizeof(*p));
5318 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
5319 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
5320 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
5321 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
5322 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
5323 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
5324 p->min_ce_len = cpu_to_le16(0x0000);
5325 p->max_ce_len = cpu_to_le16(0x0000);
5328 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
5329 struct hci_conn *conn, u8 own_addr_type)
5331 struct hci_cp_le_ext_create_conn *cp;
5332 struct hci_cp_le_ext_conn_param *p;
5333 u8 data[sizeof(*cp) + sizeof(*p) * 3];
5337 p = (void *)cp->data;
5339 memset(cp, 0, sizeof(*cp));
5341 bacpy(&cp->peer_addr, &conn->dst);
5342 cp->peer_addr_type = conn->dst_type;
5343 cp->own_addr_type = own_addr_type;
5347 if (scan_1m(hdev)) {
5348 cp->phys |= LE_SCAN_PHY_1M;
5349 set_ext_conn_params(conn, p);
5355 if (scan_2m(hdev)) {
5356 cp->phys |= LE_SCAN_PHY_2M;
5357 set_ext_conn_params(conn, p);
5363 if (scan_coded(hdev)) {
5364 cp->phys |= LE_SCAN_PHY_CODED;
5365 set_ext_conn_params(conn, p);
5370 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
5372 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
5373 conn->conn_timeout, NULL);
5376 int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
5378 struct hci_cp_le_create_conn cp;
5379 struct hci_conn_params *params;
5383 /* If requested to connect as peripheral use directed advertising */
5384 if (conn->role == HCI_ROLE_SLAVE) {
5385 /* If we're active scanning and simultaneous roles is not
5386 * enabled simply reject the attempt.
5388 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
5389 hdev->le_scan_type == LE_SCAN_ACTIVE &&
5390 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
5395 /* Pause advertising while doing directed advertising. */
5396 hci_pause_advertising_sync(hdev);
5398 err = hci_le_directed_advertising_sync(hdev, conn);
5402 /* Disable advertising if simultaneous roles is not in use. */
5403 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
5404 hci_pause_advertising_sync(hdev);
5406 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
5408 conn->le_conn_min_interval = params->conn_min_interval;
5409 conn->le_conn_max_interval = params->conn_max_interval;
5410 conn->le_conn_latency = params->conn_latency;
5411 conn->le_supv_timeout = params->supervision_timeout;
5413 conn->le_conn_min_interval = hdev->le_conn_min_interval;
5414 conn->le_conn_max_interval = hdev->le_conn_max_interval;
5415 conn->le_conn_latency = hdev->le_conn_latency;
5416 conn->le_supv_timeout = hdev->le_supv_timeout;
5419 /* If controller is scanning, we stop it since some controllers are
5420 * not able to scan and connect at the same time. Also set the
5421 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
5422 * handler for scan disabling knows to set the correct discovery
5425 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5426 hci_scan_disable_sync(hdev);
5427 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
5430 /* Update random address, but set require_privacy to false so
5431 * that we never connect with an non-resolvable address.
5433 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5438 if (use_ext_conn(hdev)) {
5439 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
5443 memset(&cp, 0, sizeof(cp));
5445 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
5446 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
5448 bacpy(&cp.peer_addr, &conn->dst);
5449 cp.peer_addr_type = conn->dst_type;
5450 cp.own_address_type = own_addr_type;
5451 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
5452 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
5453 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
5454 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
5455 cp.min_ce_len = cpu_to_le16(0x0000);
5456 cp.max_ce_len = cpu_to_le16(0x0000);
5458 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
5460 * If this event is unmasked and the HCI_LE_Connection_Complete event
5461 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
5462 * sent when a new connection has been created.
5464 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
5466 use_enhanced_conn_complete(hdev) ?
5467 HCI_EV_LE_ENHANCED_CONN_COMPLETE :
5468 HCI_EV_LE_CONN_COMPLETE,
5469 conn->conn_timeout, NULL);
5472 /* Re-enable advertising after the connection attempt is finished. */
5473 hci_resume_advertising_sync(hdev);
5477 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
5479 struct hci_cp_le_remove_cig cp;
5481 memset(&cp, 0, sizeof(cp));
5484 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
5485 &cp, HCI_CMD_TIMEOUT);