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 bt_cb(skb)->hci.req_event = 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);
279 struct hci_cmd_sync_work_entry *entry;
280 hci_cmd_sync_work_func_t func;
281 hci_cmd_sync_work_destroy_t destroy;
284 bt_dev_dbg(hdev, "");
286 mutex_lock(&hdev->cmd_sync_work_lock);
287 entry = list_first_entry(&hdev->cmd_sync_work_list,
288 struct hci_cmd_sync_work_entry, list);
290 list_del(&entry->list);
293 destroy = entry->destroy;
300 mutex_unlock(&hdev->cmd_sync_work_lock);
305 hci_req_sync_lock(hdev);
307 err = func(hdev, data);
310 destroy(hdev, data, err);
312 hci_req_sync_unlock(hdev);
316 void hci_cmd_sync_init(struct hci_dev *hdev)
318 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
319 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
320 mutex_init(&hdev->cmd_sync_work_lock);
323 void hci_cmd_sync_clear(struct hci_dev *hdev)
325 struct hci_cmd_sync_work_entry *entry, *tmp;
327 cancel_work_sync(&hdev->cmd_sync_work);
329 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
331 entry->destroy(hdev, entry->data, -ECANCELED);
333 list_del(&entry->list);
338 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
340 bt_dev_dbg(hdev, "err 0x%2.2x", err);
342 if (hdev->req_status == HCI_REQ_PEND) {
343 hdev->req_result = err;
344 hdev->req_status = HCI_REQ_CANCELED;
346 cancel_delayed_work_sync(&hdev->cmd_timer);
347 cancel_delayed_work_sync(&hdev->ncmd_timer);
348 atomic_set(&hdev->cmd_cnt, 1);
350 wake_up_interruptible(&hdev->req_wait_q);
353 EXPORT_SYMBOL(hci_cmd_sync_cancel);
355 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
356 void *data, hci_cmd_sync_work_destroy_t destroy)
358 struct hci_cmd_sync_work_entry *entry;
360 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
366 entry->destroy = destroy;
368 mutex_lock(&hdev->cmd_sync_work_lock);
369 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
370 mutex_unlock(&hdev->cmd_sync_work_lock);
372 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
376 EXPORT_SYMBOL(hci_cmd_sync_queue);
378 int hci_update_eir_sync(struct hci_dev *hdev)
380 struct hci_cp_write_eir cp;
382 bt_dev_dbg(hdev, "");
384 if (!hdev_is_powered(hdev))
387 if (!lmp_ext_inq_capable(hdev))
390 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
393 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
396 memset(&cp, 0, sizeof(cp));
398 eir_create(hdev, cp.data);
400 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
403 memcpy(hdev->eir, cp.data, sizeof(cp.data));
405 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
409 static u8 get_service_classes(struct hci_dev *hdev)
411 struct bt_uuid *uuid;
414 list_for_each_entry(uuid, &hdev->uuids, list)
415 val |= uuid->svc_hint;
420 int hci_update_class_sync(struct hci_dev *hdev)
424 bt_dev_dbg(hdev, "");
426 if (!hdev_is_powered(hdev))
429 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
432 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
435 cod[0] = hdev->minor_class;
436 cod[1] = hdev->major_class;
437 cod[2] = get_service_classes(hdev);
439 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
442 if (memcmp(cod, hdev->dev_class, 3) == 0)
445 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
446 sizeof(cod), cod, HCI_CMD_TIMEOUT);
449 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
451 /* If there is no connection we are OK to advertise. */
452 if (hci_conn_num(hdev, LE_LINK) == 0)
455 /* Check le_states if there is any connection in peripheral role. */
456 if (hdev->conn_hash.le_num_peripheral > 0) {
457 /* Peripheral connection state and non connectable mode
460 if (!connectable && !(hdev->le_states[2] & 0x10))
463 /* Peripheral connection state and connectable mode bit 38
464 * and scannable bit 21.
466 if (connectable && (!(hdev->le_states[4] & 0x40) ||
467 !(hdev->le_states[2] & 0x20)))
471 /* Check le_states if there is any connection in central role. */
472 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
473 /* Central connection state and non connectable mode bit 18. */
474 if (!connectable && !(hdev->le_states[2] & 0x02))
477 /* Central connection state and connectable mode bit 35 and
480 if (connectable && (!(hdev->le_states[4] & 0x08) ||
481 !(hdev->le_states[2] & 0x08)))
488 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
490 /* If privacy is not enabled don't use RPA */
491 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
494 /* If basic privacy mode is enabled use RPA */
495 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
498 /* If limited privacy mode is enabled don't use RPA if we're
499 * both discoverable and bondable.
501 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
502 hci_dev_test_flag(hdev, HCI_BONDABLE))
505 /* We're neither bondable nor discoverable in the limited
506 * privacy mode, therefore use RPA.
511 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
513 /* If we're advertising or initiating an LE connection we can't
514 * go ahead and change the random address at this time. This is
515 * because the eventual initiator address used for the
516 * subsequently created connection will be undefined (some
517 * controllers use the new address and others the one we had
518 * when the operation started).
520 * In this kind of scenario skip the update and let the random
521 * address be updated at the next cycle.
523 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
524 hci_lookup_le_connect(hdev)) {
525 bt_dev_dbg(hdev, "Deferring random address update");
526 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
530 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
531 6, rpa, HCI_CMD_TIMEOUT);
534 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
535 bool rpa, u8 *own_addr_type)
539 /* If privacy is enabled use a resolvable private address. If
540 * current RPA has expired or there is something else than
541 * the current RPA in use, then generate a new one.
544 /* If Controller supports LL Privacy use own address type is
547 if (use_ll_privacy(hdev))
548 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
550 *own_addr_type = ADDR_LE_DEV_RANDOM;
552 /* Check if RPA is valid */
556 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
558 bt_dev_err(hdev, "failed to generate new RPA");
562 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
569 /* In case of required privacy without resolvable private address,
570 * use an non-resolvable private address. This is useful for active
571 * scanning and non-connectable advertising.
573 if (require_privacy) {
577 /* The non-resolvable private address is generated
578 * from random six bytes with the two most significant
581 get_random_bytes(&nrpa, 6);
584 /* The non-resolvable private address shall not be
585 * equal to the public address.
587 if (bacmp(&hdev->bdaddr, &nrpa))
591 *own_addr_type = ADDR_LE_DEV_RANDOM;
593 return hci_set_random_addr_sync(hdev, &nrpa);
596 /* If forcing static address is in use or there is no public
597 * address use the static address as random address (but skip
598 * the HCI command if the current random address is already the
601 * In case BR/EDR has been disabled on a dual-mode controller
602 * and a static address has been configured, then use that
603 * address instead of the public BR/EDR address.
605 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
606 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
607 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
608 bacmp(&hdev->static_addr, BDADDR_ANY))) {
609 *own_addr_type = ADDR_LE_DEV_RANDOM;
610 if (bacmp(&hdev->static_addr, &hdev->random_addr))
611 return hci_set_random_addr_sync(hdev,
616 /* Neither privacy nor static address is being used so use a
619 *own_addr_type = ADDR_LE_DEV_PUBLIC;
624 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
626 struct hci_cp_le_set_ext_adv_enable *cp;
627 struct hci_cp_ext_adv_set *set;
628 u8 data[sizeof(*cp) + sizeof(*set) * 1];
631 /* If request specifies an instance that doesn't exist, fail */
633 struct adv_info *adv;
635 adv = hci_find_adv_instance(hdev, instance);
639 /* If not enabled there is nothing to do */
644 memset(data, 0, sizeof(data));
647 set = (void *)cp->data;
649 /* Instance 0x00 indicates all advertising instances will be disabled */
650 cp->num_of_sets = !!instance;
653 set->handle = instance;
655 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
657 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
658 size, data, HCI_CMD_TIMEOUT);
661 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
662 bdaddr_t *random_addr)
664 struct hci_cp_le_set_adv_set_rand_addr cp;
668 /* Instance 0x00 doesn't have an adv_info, instead it uses
669 * hdev->random_addr to track its address so whenever it needs
670 * to be updated this also set the random address since
671 * hdev->random_addr is shared with scan state machine.
673 err = hci_set_random_addr_sync(hdev, random_addr);
678 memset(&cp, 0, sizeof(cp));
680 cp.handle = instance;
681 bacpy(&cp.bdaddr, random_addr);
683 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
684 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
687 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
689 struct hci_cp_le_set_ext_adv_params cp;
692 bdaddr_t random_addr;
695 struct adv_info *adv;
699 adv = hci_find_adv_instance(hdev, instance);
706 /* Updating parameters of an active instance will return a
707 * Command Disallowed error, so we must first disable the
708 * instance if it is active.
710 if (adv && !adv->pending) {
711 err = hci_disable_ext_adv_instance_sync(hdev, instance);
716 flags = hci_adv_instance_flags(hdev, instance);
718 /* If the "connectable" instance flag was not set, then choose between
719 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
721 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
722 mgmt_get_connectable(hdev);
724 if (!is_advertising_allowed(hdev, connectable))
727 /* Set require_privacy to true only when non-connectable
728 * advertising is used. In that case it is fine to use a
729 * non-resolvable private address.
731 err = hci_get_random_address(hdev, !connectable,
732 adv_use_rpa(hdev, flags), adv,
733 &own_addr_type, &random_addr);
737 memset(&cp, 0, sizeof(cp));
740 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
741 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
742 cp.tx_power = adv->tx_power;
744 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
745 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
746 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
749 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
753 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
755 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
756 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
757 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
759 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
761 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
764 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
766 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
769 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
770 * contains the peer’s Identity Address and the Peer_Address_Type
771 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
772 * These parameters are used to locate the corresponding local IRK in
773 * the resolving list; this IRK is used to generate their own address
774 * used in the advertisement.
776 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
777 hci_copy_identity_address(hdev, &cp.peer_addr,
780 cp.own_addr_type = own_addr_type;
781 cp.channel_map = hdev->le_adv_channel_map;
782 cp.handle = instance;
784 if (flags & MGMT_ADV_FLAG_SEC_2M) {
785 cp.primary_phy = HCI_ADV_PHY_1M;
786 cp.secondary_phy = HCI_ADV_PHY_2M;
787 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
788 cp.primary_phy = HCI_ADV_PHY_CODED;
789 cp.secondary_phy = HCI_ADV_PHY_CODED;
791 /* In all other cases use 1M */
792 cp.primary_phy = HCI_ADV_PHY_1M;
793 cp.secondary_phy = HCI_ADV_PHY_1M;
796 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
797 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
801 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
802 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
803 bacmp(&random_addr, BDADDR_ANY)) {
804 /* Check if random address need to be updated */
806 if (!bacmp(&random_addr, &adv->random_addr))
809 if (!bacmp(&random_addr, &hdev->random_addr))
813 return hci_set_adv_set_random_addr_sync(hdev, instance,
820 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
823 struct hci_cp_le_set_ext_scan_rsp_data cp;
824 u8 data[HCI_MAX_EXT_AD_LENGTH];
828 memset(&pdu, 0, sizeof(pdu));
830 len = eir_create_scan_rsp(hdev, instance, pdu.data);
832 if (hdev->scan_rsp_data_len == len &&
833 !memcmp(pdu.data, hdev->scan_rsp_data, len))
836 memcpy(hdev->scan_rsp_data, pdu.data, len);
837 hdev->scan_rsp_data_len = len;
839 pdu.cp.handle = instance;
841 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
842 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
844 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
845 sizeof(pdu.cp) + len, &pdu.cp,
849 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
851 struct hci_cp_le_set_scan_rsp_data cp;
854 memset(&cp, 0, sizeof(cp));
856 len = eir_create_scan_rsp(hdev, instance, cp.data);
858 if (hdev->scan_rsp_data_len == len &&
859 !memcmp(cp.data, hdev->scan_rsp_data, len))
862 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
863 hdev->scan_rsp_data_len = len;
867 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
868 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
871 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
873 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
876 if (ext_adv_capable(hdev))
877 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
879 return __hci_set_scan_rsp_data_sync(hdev, instance);
882 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
884 struct hci_cp_le_set_ext_adv_enable *cp;
885 struct hci_cp_ext_adv_set *set;
886 u8 data[sizeof(*cp) + sizeof(*set) * 1];
887 struct adv_info *adv;
890 adv = hci_find_adv_instance(hdev, instance);
893 /* If already enabled there is nothing to do */
901 set = (void *)cp->data;
903 memset(cp, 0, sizeof(*cp));
906 cp->num_of_sets = 0x01;
908 memset(set, 0, sizeof(*set));
910 set->handle = instance;
912 /* Set duration per instance since controller is responsible for
915 if (adv && adv->timeout) {
916 u16 duration = adv->timeout * MSEC_PER_SEC;
918 /* Time = N * 10 ms */
919 set->duration = cpu_to_le16(duration / 10);
922 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
924 sizeof(*set) * cp->num_of_sets,
925 data, HCI_CMD_TIMEOUT);
928 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
932 err = hci_setup_ext_adv_instance_sync(hdev, instance);
936 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
940 return hci_enable_ext_advertising_sync(hdev, instance);
943 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
947 if (ext_adv_capable(hdev))
948 return hci_start_ext_adv_sync(hdev, instance);
950 err = hci_update_adv_data_sync(hdev, instance);
954 err = hci_update_scan_rsp_data_sync(hdev, instance);
958 return hci_enable_advertising_sync(hdev);
961 int hci_enable_advertising_sync(struct hci_dev *hdev)
963 struct adv_info *adv_instance;
964 struct hci_cp_le_set_adv_param cp;
965 u8 own_addr_type, enable = 0x01;
967 u16 adv_min_interval, adv_max_interval;
971 if (ext_adv_capable(hdev))
972 return hci_enable_ext_advertising_sync(hdev,
973 hdev->cur_adv_instance);
975 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
976 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
978 /* If the "connectable" instance flag was not set, then choose between
979 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
981 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
982 mgmt_get_connectable(hdev);
984 if (!is_advertising_allowed(hdev, connectable))
987 status = hci_disable_advertising_sync(hdev);
991 /* Clear the HCI_LE_ADV bit temporarily so that the
992 * hci_update_random_address knows that it's safe to go ahead
993 * and write a new random address. The flag will be set back on
994 * as soon as the SET_ADV_ENABLE HCI command completes.
996 hci_dev_clear_flag(hdev, HCI_LE_ADV);
998 /* Set require_privacy to true only when non-connectable
999 * advertising is used. In that case it is fine to use a
1000 * non-resolvable private address.
1002 status = hci_update_random_address_sync(hdev, !connectable,
1003 adv_use_rpa(hdev, flags),
1008 memset(&cp, 0, sizeof(cp));
1011 adv_min_interval = adv_instance->min_interval;
1012 adv_max_interval = adv_instance->max_interval;
1014 adv_min_interval = hdev->le_adv_min_interval;
1015 adv_max_interval = hdev->le_adv_max_interval;
1019 cp.type = LE_ADV_IND;
1021 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1022 cp.type = LE_ADV_SCAN_IND;
1024 cp.type = LE_ADV_NONCONN_IND;
1026 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1027 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1028 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1029 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1033 cp.min_interval = cpu_to_le16(adv_min_interval);
1034 cp.max_interval = cpu_to_le16(adv_max_interval);
1035 cp.own_address_type = own_addr_type;
1036 cp.channel_map = hdev->le_adv_channel_map;
1038 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1039 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1043 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1044 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1047 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1049 return hci_enable_advertising_sync(hdev);
1052 int hci_enable_advertising(struct hci_dev *hdev)
1054 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1055 list_empty(&hdev->adv_instances))
1058 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1061 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1066 if (!ext_adv_capable(hdev))
1069 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1073 /* If request specifies an instance that doesn't exist, fail */
1074 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1077 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1078 sizeof(instance), &instance, 0,
1079 HCI_CMD_TIMEOUT, sk);
1082 static void cancel_adv_timeout(struct hci_dev *hdev)
1084 if (hdev->adv_instance_timeout) {
1085 hdev->adv_instance_timeout = 0;
1086 cancel_delayed_work(&hdev->adv_instance_expire);
1090 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1093 struct hci_cp_le_set_ext_adv_data cp;
1094 u8 data[HCI_MAX_EXT_AD_LENGTH];
1098 memset(&pdu, 0, sizeof(pdu));
1100 len = eir_create_adv_data(hdev, instance, pdu.data);
1102 /* There's nothing to do if the data hasn't changed */
1103 if (hdev->adv_data_len == len &&
1104 memcmp(pdu.data, hdev->adv_data, len) == 0)
1107 memcpy(hdev->adv_data, pdu.data, len);
1108 hdev->adv_data_len = len;
1110 pdu.cp.length = len;
1111 pdu.cp.handle = instance;
1112 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1113 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1115 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1116 sizeof(pdu.cp) + len, &pdu.cp,
1120 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1122 struct hci_cp_le_set_adv_data cp;
1125 memset(&cp, 0, sizeof(cp));
1127 len = eir_create_adv_data(hdev, instance, cp.data);
1129 /* There's nothing to do if the data hasn't changed */
1130 if (hdev->adv_data_len == len &&
1131 memcmp(cp.data, hdev->adv_data, len) == 0)
1134 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1135 hdev->adv_data_len = len;
1139 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1140 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1143 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1145 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1148 if (ext_adv_capable(hdev))
1149 return hci_set_ext_adv_data_sync(hdev, instance);
1151 return hci_set_adv_data_sync(hdev, instance);
1154 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1157 struct adv_info *adv = NULL;
1160 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1163 if (hdev->adv_instance_timeout)
1166 adv = hci_find_adv_instance(hdev, instance);
1170 /* A zero timeout means unlimited advertising. As long as there is
1171 * only one instance, duration should be ignored. We still set a timeout
1172 * in case further instances are being added later on.
1174 * If the remaining lifetime of the instance is more than the duration
1175 * then the timeout corresponds to the duration, otherwise it will be
1176 * reduced to the remaining instance lifetime.
1178 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1179 timeout = adv->duration;
1181 timeout = adv->remaining_time;
1183 /* The remaining time is being reduced unless the instance is being
1184 * advertised without time limit.
1187 adv->remaining_time = adv->remaining_time - timeout;
1189 /* Only use work for scheduling instances with legacy advertising */
1190 if (!ext_adv_capable(hdev)) {
1191 hdev->adv_instance_timeout = timeout;
1192 queue_delayed_work(hdev->req_workqueue,
1193 &hdev->adv_instance_expire,
1194 msecs_to_jiffies(timeout * 1000));
1197 /* If we're just re-scheduling the same instance again then do not
1198 * execute any HCI commands. This happens when a single instance is
1201 if (!force && hdev->cur_adv_instance == instance &&
1202 hci_dev_test_flag(hdev, HCI_LE_ADV))
1205 hdev->cur_adv_instance = instance;
1207 return hci_start_adv_sync(hdev, instance);
1210 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1214 if (!ext_adv_capable(hdev))
1217 /* Disable instance 0x00 to disable all instances */
1218 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1222 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1223 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1226 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1228 struct adv_info *adv, *n;
1230 if (ext_adv_capable(hdev))
1231 /* Remove all existing sets */
1232 return hci_clear_adv_sets_sync(hdev, sk);
1234 /* This is safe as long as there is no command send while the lock is
1239 /* Cleanup non-ext instances */
1240 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1241 u8 instance = adv->instance;
1244 if (!(force || adv->timeout))
1247 err = hci_remove_adv_instance(hdev, instance);
1249 mgmt_advertising_removed(sk, hdev, instance);
1252 hci_dev_unlock(hdev);
1257 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1262 /* If we use extended advertising, instance has to be removed first. */
1263 if (ext_adv_capable(hdev))
1264 return hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1266 /* This is safe as long as there is no command send while the lock is
1271 err = hci_remove_adv_instance(hdev, instance);
1273 mgmt_advertising_removed(sk, hdev, instance);
1275 hci_dev_unlock(hdev);
1280 /* For a single instance:
1281 * - force == true: The instance will be removed even when its remaining
1282 * lifetime is not zero.
1283 * - force == false: the instance will be deactivated but kept stored unless
1284 * the remaining lifetime is zero.
1286 * For instance == 0x00:
1287 * - force == true: All instances will be removed regardless of their timeout
1289 * - force == false: Only instances that have a timeout will be removed.
1291 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1292 u8 instance, bool force)
1294 struct adv_info *next = NULL;
1297 /* Cancel any timeout concerning the removed instance(s). */
1298 if (!instance || hdev->cur_adv_instance == instance)
1299 cancel_adv_timeout(hdev);
1301 /* Get the next instance to advertise BEFORE we remove
1302 * the current one. This can be the same instance again
1303 * if there is only one instance.
1305 if (hdev->cur_adv_instance == instance)
1306 next = hci_get_next_instance(hdev, instance);
1309 err = hci_clear_adv_sync(hdev, sk, force);
1313 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1315 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1316 /* Don't advertise a removed instance. */
1317 if (next && next->instance == instance)
1320 err = hci_remove_adv_sync(hdev, instance, sk);
1326 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1329 if (next && !ext_adv_capable(hdev))
1330 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1335 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1337 struct hci_cp_read_rssi cp;
1340 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1341 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1344 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1346 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1347 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1350 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1352 struct hci_cp_read_tx_power cp;
1356 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1357 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1360 int hci_disable_advertising_sync(struct hci_dev *hdev)
1364 /* If controller is not advertising we are done. */
1365 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1368 if (ext_adv_capable(hdev))
1369 return hci_disable_ext_adv_instance_sync(hdev, 0x00);
1371 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1372 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1375 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1378 struct hci_cp_le_set_ext_scan_enable cp;
1380 memset(&cp, 0, sizeof(cp));
1382 cp.filter_dup = filter_dup;
1384 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1385 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1388 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
1391 struct hci_cp_le_set_scan_enable cp;
1393 if (use_ext_scan(hdev))
1394 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
1396 memset(&cp, 0, sizeof(cp));
1398 cp.filter_dup = filter_dup;
1400 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
1401 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1404 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
1406 if (!use_ll_privacy(hdev))
1409 /* If controller is not/already resolving we are done. */
1410 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
1413 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
1414 sizeof(val), &val, HCI_CMD_TIMEOUT);
1417 static int hci_scan_disable_sync(struct hci_dev *hdev)
1421 /* If controller is not scanning we are done. */
1422 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
1425 if (hdev->scanning_paused) {
1426 bt_dev_dbg(hdev, "Scanning is paused for suspend");
1430 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
1432 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
1439 static bool scan_use_rpa(struct hci_dev *hdev)
1441 return hci_dev_test_flag(hdev, HCI_PRIVACY);
1444 static void hci_start_interleave_scan(struct hci_dev *hdev)
1446 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
1447 queue_delayed_work(hdev->req_workqueue,
1448 &hdev->interleave_scan, 0);
1451 static bool is_interleave_scanning(struct hci_dev *hdev)
1453 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
1456 static void cancel_interleave_scan(struct hci_dev *hdev)
1458 bt_dev_dbg(hdev, "cancelling interleave scan");
1460 cancel_delayed_work_sync(&hdev->interleave_scan);
1462 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
1465 /* Return true if interleave_scan wasn't started until exiting this function,
1466 * otherwise, return false
1468 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
1470 /* Do interleaved scan only if all of the following are true:
1471 * - There is at least one ADV monitor
1472 * - At least one pending LE connection or one device to be scanned for
1473 * - Monitor offloading is not supported
1474 * If so, we should alternate between allowlist scan and one without
1475 * any filters to save power.
1477 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
1478 !(list_empty(&hdev->pend_le_conns) &&
1479 list_empty(&hdev->pend_le_reports)) &&
1480 hci_get_adv_monitor_offload_ext(hdev) ==
1481 HCI_ADV_MONITOR_EXT_NONE;
1482 bool is_interleaving = is_interleave_scanning(hdev);
1484 if (use_interleaving && !is_interleaving) {
1485 hci_start_interleave_scan(hdev);
1486 bt_dev_dbg(hdev, "starting interleave scan");
1490 if (!use_interleaving && is_interleaving)
1491 cancel_interleave_scan(hdev);
1496 /* Removes connection to resolve list if needed.*/
1497 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
1498 bdaddr_t *bdaddr, u8 bdaddr_type)
1500 struct hci_cp_le_del_from_resolv_list cp;
1501 struct bdaddr_list_with_irk *entry;
1503 if (!use_ll_privacy(hdev))
1506 /* Check if the IRK has been programmed */
1507 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
1512 cp.bdaddr_type = bdaddr_type;
1513 bacpy(&cp.bdaddr, bdaddr);
1515 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
1516 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1519 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
1520 bdaddr_t *bdaddr, u8 bdaddr_type)
1522 struct hci_cp_le_del_from_accept_list cp;
1525 /* Check if device is on accept list before removing it */
1526 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
1529 cp.bdaddr_type = bdaddr_type;
1530 bacpy(&cp.bdaddr, bdaddr);
1532 /* Ignore errors when removing from resolving list as that is likely
1533 * that the device was never added.
1535 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
1537 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
1538 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1540 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
1544 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
1550 /* Adds connection to resolve list if needed.
1551 * Setting params to NULL programs local hdev->irk
1553 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
1554 struct hci_conn_params *params)
1556 struct hci_cp_le_add_to_resolv_list cp;
1557 struct smp_irk *irk;
1558 struct bdaddr_list_with_irk *entry;
1560 if (!use_ll_privacy(hdev))
1563 /* Attempt to program local identity address, type and irk if params is
1567 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
1570 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
1571 memcpy(cp.peer_irk, hdev->irk, 16);
1575 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
1579 /* Check if the IK has _not_ been programmed yet. */
1580 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
1586 cp.bdaddr_type = params->addr_type;
1587 bacpy(&cp.bdaddr, ¶ms->addr);
1588 memcpy(cp.peer_irk, irk->val, 16);
1591 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
1592 memcpy(cp.local_irk, hdev->irk, 16);
1594 memset(cp.local_irk, 0, 16);
1596 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
1597 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1600 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
1601 * this attempts to program the device in the resolving list as well.
1603 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
1604 struct hci_conn_params *params,
1607 struct hci_cp_le_add_to_accept_list cp;
1610 /* Already in accept list */
1611 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
1615 /* Select filter policy to accept all advertising */
1616 if (*num_entries >= hdev->le_accept_list_size)
1619 /* Accept list can not be used with RPAs */
1620 if (!use_ll_privacy(hdev) &&
1621 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type)) {
1625 /* During suspend, only wakeable devices can be in acceptlist */
1626 if (hdev->suspended && !hci_conn_test_flag(HCI_CONN_FLAG_REMOTE_WAKEUP,
1627 params->current_flags))
1630 /* Attempt to program the device in the resolving list first to avoid
1631 * having to rollback in case it fails since the resolving list is
1632 * dynamic it can probably be smaller than the accept list.
1634 err = hci_le_add_resolve_list_sync(hdev, params);
1636 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
1641 cp.bdaddr_type = params->addr_type;
1642 bacpy(&cp.bdaddr, ¶ms->addr);
1644 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
1645 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1647 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
1648 /* Rollback the device from the resolving list */
1649 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
1653 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
1659 /* This function disables/pause all advertising instances */
1660 static int hci_pause_advertising_sync(struct hci_dev *hdev)
1665 /* If there are no instances or advertising has already been paused
1666 * there is nothing to do.
1668 if (!hdev->adv_instance_cnt || hdev->advertising_paused)
1671 bt_dev_dbg(hdev, "Pausing directed advertising");
1673 /* Stop directed advertising */
1674 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
1676 /* When discoverable timeout triggers, then just make sure
1677 * the limited discoverable flag is cleared. Even in the case
1678 * of a timeout triggered from general discoverable, it is
1679 * safe to unconditionally clear the flag.
1681 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1682 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1683 hdev->discov_timeout = 0;
1686 bt_dev_dbg(hdev, "Pausing advertising instances");
1688 /* Call to disable any advertisements active on the controller.
1689 * This will succeed even if no advertisements are configured.
1691 err = hci_disable_advertising_sync(hdev);
1695 /* If we are using software rotation, pause the loop */
1696 if (!ext_adv_capable(hdev))
1697 cancel_adv_timeout(hdev);
1699 hdev->advertising_paused = true;
1700 hdev->advertising_old_state = old_state;
1705 /* This function enables all user advertising instances */
1706 static int hci_resume_advertising_sync(struct hci_dev *hdev)
1708 struct adv_info *adv, *tmp;
1711 /* If advertising has not been paused there is nothing to do. */
1712 if (!hdev->advertising_paused)
1715 /* Resume directed advertising */
1716 hdev->advertising_paused = false;
1717 if (hdev->advertising_old_state) {
1718 hci_dev_set_flag(hdev, HCI_ADVERTISING);
1719 hdev->advertising_old_state = 0;
1722 bt_dev_dbg(hdev, "Resuming advertising instances");
1724 if (ext_adv_capable(hdev)) {
1725 /* Call for each tracked instance to be re-enabled */
1726 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
1727 err = hci_enable_ext_advertising_sync(hdev,
1732 /* If the instance cannot be resumed remove it */
1733 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
1737 /* Schedule for most recent instance to be restarted and begin
1738 * the software rotation loop
1740 err = hci_schedule_adv_instance_sync(hdev,
1741 hdev->cur_adv_instance,
1745 hdev->advertising_paused = false;
1750 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
1751 bool extended, struct sock *sk)
1753 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
1754 HCI_OP_READ_LOCAL_OOB_DATA;
1756 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1759 /* Device must not be scanning when updating the accept list.
1761 * Update is done using the following sequence:
1763 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
1764 * Remove Devices From Accept List ->
1765 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
1766 * Add Devices to Accept List ->
1767 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
1768 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
1771 * In case of failure advertising shall be restored to its original state and
1772 * return would disable accept list since either accept or resolving list could
1773 * not be programmed.
1776 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
1778 struct hci_conn_params *params;
1779 struct bdaddr_list *b, *t;
1781 bool pend_conn, pend_report;
1784 /* Pause advertising if resolving list can be used as controllers are
1785 * cannot accept resolving list modifications while advertising.
1787 if (use_ll_privacy(hdev)) {
1788 err = hci_pause_advertising_sync(hdev);
1790 bt_dev_err(hdev, "pause advertising failed: %d", err);
1795 /* Disable address resolution while reprogramming accept list since
1796 * devices that do have an IRK will be programmed in the resolving list
1797 * when LL Privacy is enabled.
1799 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
1801 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
1805 /* Go through the current accept list programmed into the
1806 * controller one by one and check if that address is still
1807 * in the list of pending connections or list of devices to
1808 * report. If not present in either list, then remove it from
1811 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
1812 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
1815 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
1819 /* If the device is not likely to connect or report,
1820 * remove it from the acceptlist.
1822 if (!pend_conn && !pend_report) {
1823 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
1831 /* Since all no longer valid accept list entries have been
1832 * removed, walk through the list of pending connections
1833 * and ensure that any new device gets programmed into
1836 * If the list of the devices is larger than the list of
1837 * available accept list entries in the controller, then
1838 * just abort and return filer policy value to not use the
1841 list_for_each_entry(params, &hdev->pend_le_conns, action) {
1842 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
1847 /* After adding all new pending connections, walk through
1848 * the list of pending reports and also add these to the
1849 * accept list if there is still space. Abort if space runs out.
1851 list_for_each_entry(params, &hdev->pend_le_reports, action) {
1852 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
1857 /* Use the allowlist unless the following conditions are all true:
1858 * - We are not currently suspending
1859 * - There are 1 or more ADV monitors registered and it's not offloaded
1860 * - Interleaved scanning is not currently using the allowlist
1862 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
1863 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
1864 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
1868 /* Enable address resolution when LL Privacy is enabled. */
1869 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
1871 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
1873 /* Resume advertising if it was paused */
1874 if (use_ll_privacy(hdev))
1875 hci_resume_advertising_sync(hdev);
1877 /* Select filter policy to use accept list */
1878 return err ? 0x00 : 0x01;
1881 /* Returns true if an le connection is in the scanning state */
1882 static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
1884 struct hci_conn_hash *h = &hdev->conn_hash;
1889 list_for_each_entry_rcu(c, &h->list, list) {
1890 if (c->type == LE_LINK && c->state == BT_CONNECT &&
1891 test_bit(HCI_CONN_SCANNING, &c->flags)) {
1902 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
1903 u16 interval, u16 window,
1904 u8 own_addr_type, u8 filter_policy)
1906 struct hci_cp_le_set_ext_scan_params *cp;
1907 struct hci_cp_le_scan_phy_params *phy;
1908 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
1912 phy = (void *)cp->data;
1914 memset(data, 0, sizeof(data));
1916 cp->own_addr_type = own_addr_type;
1917 cp->filter_policy = filter_policy;
1919 if (scan_1m(hdev) || scan_2m(hdev)) {
1920 cp->scanning_phys |= LE_SCAN_PHY_1M;
1923 phy->interval = cpu_to_le16(interval);
1924 phy->window = cpu_to_le16(window);
1930 if (scan_coded(hdev)) {
1931 cp->scanning_phys |= LE_SCAN_PHY_CODED;
1934 phy->interval = cpu_to_le16(interval);
1935 phy->window = cpu_to_le16(window);
1941 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
1942 sizeof(*cp) + sizeof(*phy) * num_phy,
1943 data, HCI_CMD_TIMEOUT);
1946 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
1947 u16 interval, u16 window,
1948 u8 own_addr_type, u8 filter_policy)
1950 struct hci_cp_le_set_scan_param cp;
1952 if (use_ext_scan(hdev))
1953 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
1954 window, own_addr_type,
1957 memset(&cp, 0, sizeof(cp));
1959 cp.interval = cpu_to_le16(interval);
1960 cp.window = cpu_to_le16(window);
1961 cp.own_address_type = own_addr_type;
1962 cp.filter_policy = filter_policy;
1964 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
1965 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1968 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
1969 u16 window, u8 own_addr_type, u8 filter_policy,
1974 if (hdev->scanning_paused) {
1975 bt_dev_dbg(hdev, "Scanning is paused for suspend");
1979 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
1980 own_addr_type, filter_policy);
1984 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
1987 static int hci_passive_scan_sync(struct hci_dev *hdev)
1991 u16 window, interval;
1994 if (hdev->scanning_paused) {
1995 bt_dev_dbg(hdev, "Scanning is paused for suspend");
1999 err = hci_scan_disable_sync(hdev);
2001 bt_dev_err(hdev, "disable scanning failed: %d", err);
2005 /* Set require_privacy to false since no SCAN_REQ are send
2006 * during passive scanning. Not using an non-resolvable address
2007 * here is important so that peer devices using direct
2008 * advertising with our address will be correctly reported
2009 * by the controller.
2011 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2015 if (hdev->enable_advmon_interleave_scan &&
2016 hci_update_interleaved_scan_sync(hdev))
2019 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2021 /* Adding or removing entries from the accept list must
2022 * happen before enabling scanning. The controller does
2023 * not allow accept list modification while scanning.
2025 filter_policy = hci_update_accept_list_sync(hdev);
2027 /* When the controller is using random resolvable addresses and
2028 * with that having LE privacy enabled, then controllers with
2029 * Extended Scanner Filter Policies support can now enable support
2030 * for handling directed advertising.
2032 * So instead of using filter polices 0x00 (no acceptlist)
2033 * and 0x01 (acceptlist enabled) use the new filter policies
2034 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2036 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2037 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2038 filter_policy |= 0x02;
2040 if (hdev->suspended) {
2041 window = hdev->le_scan_window_suspend;
2042 interval = hdev->le_scan_int_suspend;
2043 } else if (hci_is_le_conn_scanning(hdev)) {
2044 window = hdev->le_scan_window_connect;
2045 interval = hdev->le_scan_int_connect;
2046 } else if (hci_is_adv_monitoring(hdev)) {
2047 window = hdev->le_scan_window_adv_monitor;
2048 interval = hdev->le_scan_int_adv_monitor;
2050 window = hdev->le_scan_window;
2051 interval = hdev->le_scan_interval;
2054 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2056 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2057 own_addr_type, filter_policy,
2058 LE_SCAN_FILTER_DUP_ENABLE);
2061 /* This function controls the passive scanning based on hdev->pend_le_conns
2062 * list. If there are pending LE connection we start the background scanning,
2063 * otherwise we stop it in the following sequence:
2065 * If there are devices to scan:
2067 * Disable Scanning -> Update Accept List ->
2068 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2069 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2076 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2080 if (!test_bit(HCI_UP, &hdev->flags) ||
2081 test_bit(HCI_INIT, &hdev->flags) ||
2082 hci_dev_test_flag(hdev, HCI_SETUP) ||
2083 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2084 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2085 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2088 /* No point in doing scanning if LE support hasn't been enabled */
2089 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2092 /* If discovery is active don't interfere with it */
2093 if (hdev->discovery.state != DISCOVERY_STOPPED)
2096 /* Reset RSSI and UUID filters when starting background scanning
2097 * since these filters are meant for service discovery only.
2099 * The Start Discovery and Start Service Discovery operations
2100 * ensure to set proper values for RSSI threshold and UUID
2101 * filter list. So it is safe to just reset them here.
2103 hci_discovery_filter_clear(hdev);
2105 bt_dev_dbg(hdev, "ADV monitoring is %s",
2106 hci_is_adv_monitoring(hdev) ? "on" : "off");
2108 if (list_empty(&hdev->pend_le_conns) &&
2109 list_empty(&hdev->pend_le_reports) &&
2110 !hci_is_adv_monitoring(hdev)) {
2111 /* If there is no pending LE connections or devices
2112 * to be scanned for or no ADV monitors, we should stop the
2113 * background scanning.
2116 bt_dev_dbg(hdev, "stopping background scanning");
2118 err = hci_scan_disable_sync(hdev);
2120 bt_dev_err(hdev, "stop background scanning failed: %d",
2123 /* If there is at least one pending LE connection, we should
2124 * keep the background scan running.
2127 /* If controller is connecting, we should not start scanning
2128 * since some controllers are not able to scan and connect at
2131 if (hci_lookup_le_connect(hdev))
2134 bt_dev_dbg(hdev, "start background scanning");
2136 err = hci_passive_scan_sync(hdev);
2138 bt_dev_err(hdev, "start background scanning failed: %d",
2145 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2147 return hci_update_passive_scan_sync(hdev);
2150 int hci_update_passive_scan(struct hci_dev *hdev)
2152 /* Only queue if it would have any effect */
2153 if (!test_bit(HCI_UP, &hdev->flags) ||
2154 test_bit(HCI_INIT, &hdev->flags) ||
2155 hci_dev_test_flag(hdev, HCI_SETUP) ||
2156 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2157 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2158 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2161 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2164 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2168 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2171 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2172 sizeof(val), &val, HCI_CMD_TIMEOUT);
2176 hdev->features[1][0] |= LMP_HOST_SC;
2177 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2179 hdev->features[1][0] &= ~LMP_HOST_SC;
2180 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2187 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2191 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2192 lmp_host_ssp_capable(hdev))
2195 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
2196 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2197 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2200 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2201 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2205 return hci_write_sc_support_sync(hdev, 0x01);
2208 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
2210 struct hci_cp_write_le_host_supported cp;
2212 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
2213 !lmp_bredr_capable(hdev))
2216 /* Check first if we already have the right host state
2217 * (host features set)
2219 if (le == lmp_host_le_capable(hdev) &&
2220 simul == lmp_host_le_br_capable(hdev))
2223 memset(&cp, 0, sizeof(cp));
2228 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
2229 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2232 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
2234 struct adv_info *adv, *tmp;
2237 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2240 /* If RPA Resolution has not been enable yet it means the
2241 * resolving list is empty and we should attempt to program the
2242 * local IRK in order to support using own_addr_type
2243 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
2245 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
2246 hci_le_add_resolve_list_sync(hdev, NULL);
2247 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2250 /* Make sure the controller has a good default for
2251 * advertising data. This also applies to the case
2252 * where BR/EDR was toggled during the AUTO_OFF phase.
2254 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2255 list_empty(&hdev->adv_instances)) {
2256 if (ext_adv_capable(hdev)) {
2257 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
2259 hci_update_scan_rsp_data_sync(hdev, 0x00);
2261 err = hci_update_adv_data_sync(hdev, 0x00);
2263 hci_update_scan_rsp_data_sync(hdev, 0x00);
2266 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2267 hci_enable_advertising_sync(hdev);
2270 /* Call for each tracked instance to be scheduled */
2271 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
2272 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
2277 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
2281 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
2282 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
2285 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
2286 sizeof(link_sec), &link_sec,
2290 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
2292 struct hci_cp_write_page_scan_activity cp;
2296 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2299 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
2302 memset(&cp, 0, sizeof(cp));
2305 type = PAGE_SCAN_TYPE_INTERLACED;
2307 /* 160 msec page scan interval */
2308 cp.interval = cpu_to_le16(0x0100);
2310 type = hdev->def_page_scan_type;
2311 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
2314 cp.window = cpu_to_le16(hdev->def_page_scan_window);
2316 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
2317 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
2318 err = __hci_cmd_sync_status(hdev,
2319 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
2320 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2325 if (hdev->page_scan_type != type)
2326 err = __hci_cmd_sync_status(hdev,
2327 HCI_OP_WRITE_PAGE_SCAN_TYPE,
2328 sizeof(type), &type,
2334 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
2336 struct bdaddr_list *b;
2338 list_for_each_entry(b, &hdev->accept_list, list) {
2339 struct hci_conn *conn;
2341 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
2345 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2352 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
2354 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
2359 int hci_update_scan_sync(struct hci_dev *hdev)
2363 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2366 if (!hdev_is_powered(hdev))
2369 if (mgmt_powering_down(hdev))
2372 if (hdev->scanning_paused)
2375 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
2376 disconnected_accept_list_entries(hdev))
2379 scan = SCAN_DISABLED;
2381 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
2382 scan |= SCAN_INQUIRY;
2384 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
2385 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
2388 return hci_write_scan_enable_sync(hdev, scan);
2391 int hci_update_name_sync(struct hci_dev *hdev)
2393 struct hci_cp_write_local_name cp;
2395 memset(&cp, 0, sizeof(cp));
2397 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
2399 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
2404 /* This function perform powered update HCI command sequence after the HCI init
2405 * sequence which end up resetting all states, the sequence is as follows:
2407 * HCI_SSP_ENABLED(Enable SSP)
2408 * HCI_LE_ENABLED(Enable LE)
2409 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
2411 * Enable Authentication
2412 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
2413 * Set Name -> Set EIR)
2415 int hci_powered_update_sync(struct hci_dev *hdev)
2419 /* Register the available SMP channels (BR/EDR and LE) only when
2420 * successfully powering on the controller. This late
2421 * registration is required so that LE SMP can clearly decide if
2422 * the public address or static address is used.
2426 err = hci_write_ssp_mode_sync(hdev, 0x01);
2430 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
2434 err = hci_powered_update_adv_sync(hdev);
2438 err = hci_write_auth_enable_sync(hdev);
2442 if (lmp_bredr_capable(hdev)) {
2443 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
2444 hci_write_fast_connectable_sync(hdev, true);
2446 hci_write_fast_connectable_sync(hdev, false);
2447 hci_update_scan_sync(hdev);
2448 hci_update_class_sync(hdev);
2449 hci_update_name_sync(hdev);
2450 hci_update_eir_sync(hdev);
2457 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
2458 * (BD_ADDR) for a HCI device from
2459 * a firmware node property.
2460 * @hdev: The HCI device
2462 * Search the firmware node for 'local-bd-address'.
2464 * All-zero BD addresses are rejected, because those could be properties
2465 * that exist in the firmware tables, but were not updated by the firmware. For
2466 * example, the DTS could define 'local-bd-address', with zero BD addresses.
2468 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
2470 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
2474 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
2475 (u8 *)&ba, sizeof(ba));
2476 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
2479 bacpy(&hdev->public_addr, &ba);
2482 struct hci_init_stage {
2483 int (*func)(struct hci_dev *hdev);
2486 /* Run init stage NULL terminated function table */
2487 static int hci_init_stage_sync(struct hci_dev *hdev,
2488 const struct hci_init_stage *stage)
2492 for (i = 0; stage[i].func; i++) {
2495 err = stage[i].func(hdev);
2503 /* Read Local Version */
2504 static int hci_read_local_version_sync(struct hci_dev *hdev)
2506 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
2507 0, NULL, HCI_CMD_TIMEOUT);
2510 /* Read BD Address */
2511 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
2513 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
2514 0, NULL, HCI_CMD_TIMEOUT);
2517 #define HCI_INIT(_func) \
2522 static const struct hci_init_stage hci_init0[] = {
2523 /* HCI_OP_READ_LOCAL_VERSION */
2524 HCI_INIT(hci_read_local_version_sync),
2525 /* HCI_OP_READ_BD_ADDR */
2526 HCI_INIT(hci_read_bd_addr_sync),
2530 int hci_reset_sync(struct hci_dev *hdev)
2534 set_bit(HCI_RESET, &hdev->flags);
2536 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
2544 static int hci_init0_sync(struct hci_dev *hdev)
2548 bt_dev_dbg(hdev, "");
2551 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
2552 err = hci_reset_sync(hdev);
2557 return hci_init_stage_sync(hdev, hci_init0);
2560 static int hci_unconf_init_sync(struct hci_dev *hdev)
2564 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
2567 err = hci_init0_sync(hdev);
2571 if (hci_dev_test_flag(hdev, HCI_SETUP))
2572 hci_debugfs_create_basic(hdev);
2577 /* Read Local Supported Features. */
2578 static int hci_read_local_features_sync(struct hci_dev *hdev)
2580 /* Not all AMP controllers support this command */
2581 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
2584 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
2585 0, NULL, HCI_CMD_TIMEOUT);
2588 /* BR Controller init stage 1 command sequence */
2589 static const struct hci_init_stage br_init1[] = {
2590 /* HCI_OP_READ_LOCAL_FEATURES */
2591 HCI_INIT(hci_read_local_features_sync),
2592 /* HCI_OP_READ_LOCAL_VERSION */
2593 HCI_INIT(hci_read_local_version_sync),
2594 /* HCI_OP_READ_BD_ADDR */
2595 HCI_INIT(hci_read_bd_addr_sync),
2599 /* Read Local Commands */
2600 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
2602 /* All Bluetooth 1.2 and later controllers should support the
2603 * HCI command for reading the local supported commands.
2605 * Unfortunately some controllers indicate Bluetooth 1.2 support,
2606 * but do not have support for this command. If that is the case,
2607 * the driver can quirk the behavior and skip reading the local
2608 * supported commands.
2610 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
2611 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
2612 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
2613 0, NULL, HCI_CMD_TIMEOUT);
2618 /* Read Local AMP Info */
2619 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
2621 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
2622 0, NULL, HCI_CMD_TIMEOUT);
2625 /* Read Data Blk size */
2626 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
2628 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
2629 0, NULL, HCI_CMD_TIMEOUT);
2632 /* Read Flow Control Mode */
2633 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
2635 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
2636 0, NULL, HCI_CMD_TIMEOUT);
2639 /* Read Location Data */
2640 static int hci_read_location_data_sync(struct hci_dev *hdev)
2642 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
2643 0, NULL, HCI_CMD_TIMEOUT);
2646 /* AMP Controller init stage 1 command sequence */
2647 static const struct hci_init_stage amp_init1[] = {
2648 /* HCI_OP_READ_LOCAL_VERSION */
2649 HCI_INIT(hci_read_local_version_sync),
2650 /* HCI_OP_READ_LOCAL_COMMANDS */
2651 HCI_INIT(hci_read_local_cmds_sync),
2652 /* HCI_OP_READ_LOCAL_AMP_INFO */
2653 HCI_INIT(hci_read_local_amp_info_sync),
2654 /* HCI_OP_READ_DATA_BLOCK_SIZE */
2655 HCI_INIT(hci_read_data_block_size_sync),
2656 /* HCI_OP_READ_FLOW_CONTROL_MODE */
2657 HCI_INIT(hci_read_flow_control_mode_sync),
2658 /* HCI_OP_READ_LOCATION_DATA */
2659 HCI_INIT(hci_read_location_data_sync),
2662 static int hci_init1_sync(struct hci_dev *hdev)
2666 bt_dev_dbg(hdev, "");
2669 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
2670 err = hci_reset_sync(hdev);
2675 switch (hdev->dev_type) {
2677 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
2678 return hci_init_stage_sync(hdev, br_init1);
2680 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
2681 return hci_init_stage_sync(hdev, amp_init1);
2683 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
2690 /* AMP Controller init stage 2 command sequence */
2691 static const struct hci_init_stage amp_init2[] = {
2692 /* HCI_OP_READ_LOCAL_FEATURES */
2693 HCI_INIT(hci_read_local_features_sync),
2696 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
2697 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
2699 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
2700 0, NULL, HCI_CMD_TIMEOUT);
2703 /* Read Class of Device */
2704 static int hci_read_dev_class_sync(struct hci_dev *hdev)
2706 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
2707 0, NULL, HCI_CMD_TIMEOUT);
2710 /* Read Local Name */
2711 static int hci_read_local_name_sync(struct hci_dev *hdev)
2713 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
2714 0, NULL, HCI_CMD_TIMEOUT);
2717 /* Read Voice Setting */
2718 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
2720 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
2721 0, NULL, HCI_CMD_TIMEOUT);
2724 /* Read Number of Supported IAC */
2725 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
2727 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
2728 0, NULL, HCI_CMD_TIMEOUT);
2731 /* Read Current IAC LAP */
2732 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
2734 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
2735 0, NULL, HCI_CMD_TIMEOUT);
2738 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
2739 u8 cond_type, bdaddr_t *bdaddr,
2742 struct hci_cp_set_event_filter cp;
2744 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2747 memset(&cp, 0, sizeof(cp));
2748 cp.flt_type = flt_type;
2750 if (flt_type != HCI_FLT_CLEAR_ALL) {
2751 cp.cond_type = cond_type;
2752 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
2753 cp.addr_conn_flt.auto_accept = auto_accept;
2756 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
2757 flt_type == HCI_FLT_CLEAR_ALL ?
2758 sizeof(cp.flt_type) : sizeof(cp), &cp,
2762 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
2764 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
2767 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
2771 /* Connection accept timeout ~20 secs */
2772 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
2774 __le16 param = cpu_to_le16(0x7d00);
2776 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
2777 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
2780 /* BR Controller init stage 2 command sequence */
2781 static const struct hci_init_stage br_init2[] = {
2782 /* HCI_OP_READ_BUFFER_SIZE */
2783 HCI_INIT(hci_read_buffer_size_sync),
2784 /* HCI_OP_READ_CLASS_OF_DEV */
2785 HCI_INIT(hci_read_dev_class_sync),
2786 /* HCI_OP_READ_LOCAL_NAME */
2787 HCI_INIT(hci_read_local_name_sync),
2788 /* HCI_OP_READ_VOICE_SETTING */
2789 HCI_INIT(hci_read_voice_setting_sync),
2790 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
2791 HCI_INIT(hci_read_num_supported_iac_sync),
2792 /* HCI_OP_READ_CURRENT_IAC_LAP */
2793 HCI_INIT(hci_read_current_iac_lap_sync),
2794 /* HCI_OP_SET_EVENT_FLT */
2795 HCI_INIT(hci_clear_event_filter_sync),
2796 /* HCI_OP_WRITE_CA_TIMEOUT */
2797 HCI_INIT(hci_write_ca_timeout_sync),
2801 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
2805 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2808 /* When SSP is available, then the host features page
2809 * should also be available as well. However some
2810 * controllers list the max_page as 0 as long as SSP
2811 * has not been enabled. To achieve proper debugging
2812 * output, force the minimum max_page to 1 at least.
2814 hdev->max_page = 0x01;
2816 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2817 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2820 static int hci_write_eir_sync(struct hci_dev *hdev)
2822 struct hci_cp_write_eir cp;
2824 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2827 memset(hdev->eir, 0, sizeof(hdev->eir));
2828 memset(&cp, 0, sizeof(cp));
2830 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
2834 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
2838 if (!lmp_inq_rssi_capable(hdev) &&
2839 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
2842 /* If Extended Inquiry Result events are supported, then
2843 * they are clearly preferred over Inquiry Result with RSSI
2846 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
2848 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
2849 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2852 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
2854 if (!lmp_inq_tx_pwr_capable(hdev))
2857 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
2858 0, NULL, HCI_CMD_TIMEOUT);
2861 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
2863 struct hci_cp_read_local_ext_features cp;
2865 if (!lmp_ext_feat_capable(hdev))
2868 memset(&cp, 0, sizeof(cp));
2871 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
2872 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2875 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
2877 return hci_read_local_ext_features_sync(hdev, 0x01);
2880 /* HCI Controller init stage 2 command sequence */
2881 static const struct hci_init_stage hci_init2[] = {
2882 /* HCI_OP_READ_LOCAL_COMMANDS */
2883 HCI_INIT(hci_read_local_cmds_sync),
2884 /* HCI_OP_WRITE_SSP_MODE */
2885 HCI_INIT(hci_write_ssp_mode_1_sync),
2886 /* HCI_OP_WRITE_EIR */
2887 HCI_INIT(hci_write_eir_sync),
2888 /* HCI_OP_WRITE_INQUIRY_MODE */
2889 HCI_INIT(hci_write_inquiry_mode_sync),
2890 /* HCI_OP_READ_INQ_RSP_TX_POWER */
2891 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
2892 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
2893 HCI_INIT(hci_read_local_ext_features_1_sync),
2894 /* HCI_OP_WRITE_AUTH_ENABLE */
2895 HCI_INIT(hci_write_auth_enable_sync),
2899 /* Read LE Buffer Size */
2900 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
2902 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
2903 0, NULL, HCI_CMD_TIMEOUT);
2906 /* Read LE Local Supported Features */
2907 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
2909 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
2910 0, NULL, HCI_CMD_TIMEOUT);
2913 /* Read LE Supported States */
2914 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
2916 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
2917 0, NULL, HCI_CMD_TIMEOUT);
2920 /* LE Controller init stage 2 command sequence */
2921 static const struct hci_init_stage le_init2[] = {
2922 /* HCI_OP_LE_READ_BUFFER_SIZE */
2923 HCI_INIT(hci_le_read_buffer_size_sync),
2924 /* HCI_OP_LE_READ_LOCAL_FEATURES */
2925 HCI_INIT(hci_le_read_local_features_sync),
2926 /* HCI_OP_LE_READ_SUPPORTED_STATES */
2927 HCI_INIT(hci_le_read_supported_states_sync),
2931 static int hci_init2_sync(struct hci_dev *hdev)
2935 bt_dev_dbg(hdev, "");
2937 if (hdev->dev_type == HCI_AMP)
2938 return hci_init_stage_sync(hdev, amp_init2);
2940 if (lmp_bredr_capable(hdev)) {
2941 err = hci_init_stage_sync(hdev, br_init2);
2945 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
2948 if (lmp_le_capable(hdev)) {
2949 err = hci_init_stage_sync(hdev, le_init2);
2952 /* LE-only controllers have LE implicitly enabled */
2953 if (!lmp_bredr_capable(hdev))
2954 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
2957 return hci_init_stage_sync(hdev, hci_init2);
2960 static int hci_set_event_mask_sync(struct hci_dev *hdev)
2962 /* The second byte is 0xff instead of 0x9f (two reserved bits
2963 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
2964 * command otherwise.
2966 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
2968 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
2969 * any event mask for pre 1.2 devices.
2971 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
2974 if (lmp_bredr_capable(hdev)) {
2975 events[4] |= 0x01; /* Flow Specification Complete */
2977 /* Don't set Disconnect Complete when suspended as that
2978 * would wakeup the host when disconnecting due to
2981 if (hdev->suspended)
2984 /* Use a different default for LE-only devices */
2985 memset(events, 0, sizeof(events));
2986 events[1] |= 0x20; /* Command Complete */
2987 events[1] |= 0x40; /* Command Status */
2988 events[1] |= 0x80; /* Hardware Error */
2990 /* If the controller supports the Disconnect command, enable
2991 * the corresponding event. In addition enable packet flow
2992 * control related events.
2994 if (hdev->commands[0] & 0x20) {
2995 /* Don't set Disconnect Complete when suspended as that
2996 * would wakeup the host when disconnecting due to
2999 if (!hdev->suspended)
3000 events[0] |= 0x10; /* Disconnection Complete */
3001 events[2] |= 0x04; /* Number of Completed Packets */
3002 events[3] |= 0x02; /* Data Buffer Overflow */
3005 /* If the controller supports the Read Remote Version
3006 * Information command, enable the corresponding event.
3008 if (hdev->commands[2] & 0x80)
3009 events[1] |= 0x08; /* Read Remote Version Information
3013 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3014 events[0] |= 0x80; /* Encryption Change */
3015 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3019 if (lmp_inq_rssi_capable(hdev) ||
3020 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3021 events[4] |= 0x02; /* Inquiry Result with RSSI */
3023 if (lmp_ext_feat_capable(hdev))
3024 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3026 if (lmp_esco_capable(hdev)) {
3027 events[5] |= 0x08; /* Synchronous Connection Complete */
3028 events[5] |= 0x10; /* Synchronous Connection Changed */
3031 if (lmp_sniffsubr_capable(hdev))
3032 events[5] |= 0x20; /* Sniff Subrating */
3034 if (lmp_pause_enc_capable(hdev))
3035 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3037 if (lmp_ext_inq_capable(hdev))
3038 events[5] |= 0x40; /* Extended Inquiry Result */
3040 if (lmp_no_flush_capable(hdev))
3041 events[7] |= 0x01; /* Enhanced Flush Complete */
3043 if (lmp_lsto_capable(hdev))
3044 events[6] |= 0x80; /* Link Supervision Timeout Changed */
3046 if (lmp_ssp_capable(hdev)) {
3047 events[6] |= 0x01; /* IO Capability Request */
3048 events[6] |= 0x02; /* IO Capability Response */
3049 events[6] |= 0x04; /* User Confirmation Request */
3050 events[6] |= 0x08; /* User Passkey Request */
3051 events[6] |= 0x10; /* Remote OOB Data Request */
3052 events[6] |= 0x20; /* Simple Pairing Complete */
3053 events[7] |= 0x04; /* User Passkey Notification */
3054 events[7] |= 0x08; /* Keypress Notification */
3055 events[7] |= 0x10; /* Remote Host Supported
3056 * Features Notification
3060 if (lmp_le_capable(hdev))
3061 events[7] |= 0x20; /* LE Meta-Event */
3063 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3064 sizeof(events), events, HCI_CMD_TIMEOUT);
3067 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3069 struct hci_cp_read_stored_link_key cp;
3071 if (!(hdev->commands[6] & 0x20) ||
3072 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3075 memset(&cp, 0, sizeof(cp));
3076 bacpy(&cp.bdaddr, BDADDR_ANY);
3079 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3080 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3083 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3085 struct hci_cp_write_def_link_policy cp;
3086 u16 link_policy = 0;
3088 if (!(hdev->commands[5] & 0x10))
3091 memset(&cp, 0, sizeof(cp));
3093 if (lmp_rswitch_capable(hdev))
3094 link_policy |= HCI_LP_RSWITCH;
3095 if (lmp_hold_capable(hdev))
3096 link_policy |= HCI_LP_HOLD;
3097 if (lmp_sniff_capable(hdev))
3098 link_policy |= HCI_LP_SNIFF;
3099 if (lmp_park_capable(hdev))
3100 link_policy |= HCI_LP_PARK;
3102 cp.policy = cpu_to_le16(link_policy);
3104 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3105 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3108 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3110 if (!(hdev->commands[8] & 0x01))
3113 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3114 0, NULL, HCI_CMD_TIMEOUT);
3117 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3119 if (!(hdev->commands[18] & 0x04) ||
3120 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3123 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3124 0, NULL, HCI_CMD_TIMEOUT);
3127 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3129 /* Some older Broadcom based Bluetooth 1.2 controllers do not
3130 * support the Read Page Scan Type command. Check support for
3131 * this command in the bit mask of supported commands.
3133 if (!(hdev->commands[13] & 0x01))
3136 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3137 0, NULL, HCI_CMD_TIMEOUT);
3140 /* Read features beyond page 1 if available */
3141 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3146 if (!lmp_ext_feat_capable(hdev))
3149 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3151 err = hci_read_local_ext_features_sync(hdev, page);
3159 /* HCI Controller init stage 3 command sequence */
3160 static const struct hci_init_stage hci_init3[] = {
3161 /* HCI_OP_SET_EVENT_MASK */
3162 HCI_INIT(hci_set_event_mask_sync),
3163 /* HCI_OP_READ_STORED_LINK_KEY */
3164 HCI_INIT(hci_read_stored_link_key_sync),
3165 /* HCI_OP_WRITE_DEF_LINK_POLICY */
3166 HCI_INIT(hci_setup_link_policy_sync),
3167 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
3168 HCI_INIT(hci_read_page_scan_activity_sync),
3169 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
3170 HCI_INIT(hci_read_def_err_data_reporting_sync),
3171 /* HCI_OP_READ_PAGE_SCAN_TYPE */
3172 HCI_INIT(hci_read_page_scan_type_sync),
3173 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3174 HCI_INIT(hci_read_local_ext_features_all_sync),
3178 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
3182 if (!lmp_le_capable(hdev))
3185 memset(events, 0, sizeof(events));
3187 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
3188 events[0] |= 0x10; /* LE Long Term Key Request */
3190 /* If controller supports the Connection Parameters Request
3191 * Link Layer Procedure, enable the corresponding event.
3193 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
3194 /* LE Remote Connection Parameter Request */
3197 /* If the controller supports the Data Length Extension
3198 * feature, enable the corresponding event.
3200 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
3201 events[0] |= 0x40; /* LE Data Length Change */
3203 /* If the controller supports LL Privacy feature, enable
3204 * the corresponding event.
3206 if (hdev->le_features[0] & HCI_LE_LL_PRIVACY)
3207 events[1] |= 0x02; /* LE Enhanced Connection Complete */
3209 /* If the controller supports Extended Scanner Filter
3210 * Policies, enable the corresponding event.
3212 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
3213 events[1] |= 0x04; /* LE Direct Advertising Report */
3215 /* If the controller supports Channel Selection Algorithm #2
3216 * feature, enable the corresponding event.
3218 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
3219 events[2] |= 0x08; /* LE Channel Selection Algorithm */
3221 /* If the controller supports the LE Set Scan Enable command,
3222 * enable the corresponding advertising report event.
3224 if (hdev->commands[26] & 0x08)
3225 events[0] |= 0x02; /* LE Advertising Report */
3227 /* If the controller supports the LE Create Connection
3228 * command, enable the corresponding event.
3230 if (hdev->commands[26] & 0x10)
3231 events[0] |= 0x01; /* LE Connection Complete */
3233 /* If the controller supports the LE Connection Update
3234 * command, enable the corresponding event.
3236 if (hdev->commands[27] & 0x04)
3237 events[0] |= 0x04; /* LE Connection Update Complete */
3239 /* If the controller supports the LE Read Remote Used Features
3240 * command, enable the corresponding event.
3242 if (hdev->commands[27] & 0x20)
3243 /* LE Read Remote Used Features Complete */
3246 /* If the controller supports the LE Read Local P-256
3247 * Public Key command, enable the corresponding event.
3249 if (hdev->commands[34] & 0x02)
3250 /* LE Read Local P-256 Public Key Complete */
3253 /* If the controller supports the LE Generate DHKey
3254 * command, enable the corresponding event.
3256 if (hdev->commands[34] & 0x04)
3257 events[1] |= 0x01; /* LE Generate DHKey Complete */
3259 /* If the controller supports the LE Set Default PHY or
3260 * LE Set PHY commands, enable the corresponding event.
3262 if (hdev->commands[35] & (0x20 | 0x40))
3263 events[1] |= 0x08; /* LE PHY Update Complete */
3265 /* If the controller supports LE Set Extended Scan Parameters
3266 * and LE Set Extended Scan Enable commands, enable the
3267 * corresponding event.
3269 if (use_ext_scan(hdev))
3270 events[1] |= 0x10; /* LE Extended Advertising Report */
3272 /* If the controller supports the LE Extended Advertising
3273 * command, enable the corresponding event.
3275 if (ext_adv_capable(hdev))
3276 events[2] |= 0x02; /* LE Advertising Set Terminated */
3278 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
3279 sizeof(events), events, HCI_CMD_TIMEOUT);
3282 /* Read LE Advertising Channel TX Power */
3283 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
3285 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
3286 /* HCI TS spec forbids mixing of legacy and extended
3287 * advertising commands wherein READ_ADV_TX_POWER is
3288 * also included. So do not call it if extended adv
3289 * is supported otherwise controller will return
3290 * COMMAND_DISALLOWED for extended commands.
3292 return __hci_cmd_sync_status(hdev,
3293 HCI_OP_LE_READ_ADV_TX_POWER,
3294 0, NULL, HCI_CMD_TIMEOUT);
3300 /* Read LE Min/Max Tx Power*/
3301 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
3303 if (!(hdev->commands[38] & 0x80) ||
3304 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
3307 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
3308 0, NULL, HCI_CMD_TIMEOUT);
3311 /* Read LE Accept List Size */
3312 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
3314 if (!(hdev->commands[26] & 0x40))
3317 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
3318 0, NULL, HCI_CMD_TIMEOUT);
3321 /* Clear LE Accept List */
3322 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
3324 if (!(hdev->commands[26] & 0x80))
3327 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
3331 /* Read LE Resolving List Size */
3332 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
3334 if (!(hdev->commands[34] & 0x40))
3337 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
3338 0, NULL, HCI_CMD_TIMEOUT);
3341 /* Clear LE Resolving List */
3342 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
3344 if (!(hdev->commands[34] & 0x20))
3347 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
3351 /* Set RPA timeout */
3352 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
3354 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
3356 if (!(hdev->commands[35] & 0x04))
3359 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
3360 sizeof(timeout), &timeout,
3364 /* Read LE Maximum Data Length */
3365 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
3367 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3370 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
3374 /* Read LE Suggested Default Data Length */
3375 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
3377 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3380 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
3384 /* Read LE Number of Supported Advertising Sets */
3385 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
3387 if (!ext_adv_capable(hdev))
3390 return __hci_cmd_sync_status(hdev,
3391 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
3392 0, NULL, HCI_CMD_TIMEOUT);
3395 /* Write LE Host Supported */
3396 static int hci_set_le_support_sync(struct hci_dev *hdev)
3398 struct hci_cp_write_le_host_supported cp;
3400 /* LE-only devices do not support explicit enablement */
3401 if (!lmp_bredr_capable(hdev))
3404 memset(&cp, 0, sizeof(cp));
3406 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
3411 if (cp.le == lmp_host_le_capable(hdev))
3414 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3415 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3418 /* LE Controller init stage 3 command sequence */
3419 static const struct hci_init_stage le_init3[] = {
3420 /* HCI_OP_LE_SET_EVENT_MASK */
3421 HCI_INIT(hci_le_set_event_mask_sync),
3422 /* HCI_OP_LE_READ_ADV_TX_POWER */
3423 HCI_INIT(hci_le_read_adv_tx_power_sync),
3424 /* HCI_OP_LE_READ_TRANSMIT_POWER */
3425 HCI_INIT(hci_le_read_tx_power_sync),
3426 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
3427 HCI_INIT(hci_le_read_accept_list_size_sync),
3428 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
3429 HCI_INIT(hci_le_clear_accept_list_sync),
3430 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
3431 HCI_INIT(hci_le_read_resolv_list_size_sync),
3432 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
3433 HCI_INIT(hci_le_clear_resolv_list_sync),
3434 /* HCI_OP_LE_SET_RPA_TIMEOUT */
3435 HCI_INIT(hci_le_set_rpa_timeout_sync),
3436 /* HCI_OP_LE_READ_MAX_DATA_LEN */
3437 HCI_INIT(hci_le_read_max_data_len_sync),
3438 /* HCI_OP_LE_READ_DEF_DATA_LEN */
3439 HCI_INIT(hci_le_read_def_data_len_sync),
3440 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
3441 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
3442 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
3443 HCI_INIT(hci_set_le_support_sync),
3447 static int hci_init3_sync(struct hci_dev *hdev)
3451 bt_dev_dbg(hdev, "");
3453 err = hci_init_stage_sync(hdev, hci_init3);
3457 if (lmp_le_capable(hdev))
3458 return hci_init_stage_sync(hdev, le_init3);
3463 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
3465 struct hci_cp_delete_stored_link_key cp;
3467 /* Some Broadcom based Bluetooth controllers do not support the
3468 * Delete Stored Link Key command. They are clearly indicating its
3469 * absence in the bit mask of supported commands.
3471 * Check the supported commands and only if the command is marked
3472 * as supported send it. If not supported assume that the controller
3473 * does not have actual support for stored link keys which makes this
3474 * command redundant anyway.
3476 * Some controllers indicate that they support handling deleting
3477 * stored link keys, but they don't. The quirk lets a driver
3478 * just disable this command.
3480 if (!(hdev->commands[6] & 0x80) ||
3481 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3484 memset(&cp, 0, sizeof(cp));
3485 bacpy(&cp.bdaddr, BDADDR_ANY);
3486 cp.delete_all = 0x01;
3488 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
3489 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3492 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
3494 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3495 bool changed = false;
3497 /* Set event mask page 2 if the HCI command for it is supported */
3498 if (!(hdev->commands[22] & 0x04))
3501 /* If Connectionless Peripheral Broadcast central role is supported
3502 * enable all necessary events for it.
3504 if (lmp_cpb_central_capable(hdev)) {
3505 events[1] |= 0x40; /* Triggered Clock Capture */
3506 events[1] |= 0x80; /* Synchronization Train Complete */
3507 events[2] |= 0x10; /* Peripheral Page Response Timeout */
3508 events[2] |= 0x20; /* CPB Channel Map Change */
3512 /* If Connectionless Peripheral Broadcast peripheral role is supported
3513 * enable all necessary events for it.
3515 if (lmp_cpb_peripheral_capable(hdev)) {
3516 events[2] |= 0x01; /* Synchronization Train Received */
3517 events[2] |= 0x02; /* CPB Receive */
3518 events[2] |= 0x04; /* CPB Timeout */
3519 events[2] |= 0x08; /* Truncated Page Complete */
3523 /* Enable Authenticated Payload Timeout Expired event if supported */
3524 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
3529 /* Some Broadcom based controllers indicate support for Set Event
3530 * Mask Page 2 command, but then actually do not support it. Since
3531 * the default value is all bits set to zero, the command is only
3532 * required if the event mask has to be changed. In case no change
3533 * to the event mask is needed, skip this command.
3538 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
3539 sizeof(events), events, HCI_CMD_TIMEOUT);
3542 /* Read local codec list if the HCI command is supported */
3543 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
3545 if (!(hdev->commands[29] & 0x20))
3548 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_CODECS, 0, NULL,
3552 /* Read local pairing options if the HCI command is supported */
3553 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
3555 if (!(hdev->commands[41] & 0x08))
3558 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
3559 0, NULL, HCI_CMD_TIMEOUT);
3562 /* Get MWS transport configuration if the HCI command is supported */
3563 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
3565 if (!(hdev->commands[30] & 0x08))
3568 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
3569 0, NULL, HCI_CMD_TIMEOUT);
3572 /* Check for Synchronization Train support */
3573 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
3575 if (!lmp_sync_train_capable(hdev))
3578 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
3579 0, NULL, HCI_CMD_TIMEOUT);
3582 /* Enable Secure Connections if supported and configured */
3583 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
3587 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3588 !bredr_sc_enabled(hdev))
3591 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3592 sizeof(support), &support,
3596 /* Set erroneous data reporting if supported to the wideband speech
3599 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
3601 struct hci_cp_write_def_err_data_reporting cp;
3602 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
3604 if (!(hdev->commands[18] & 0x08) ||
3605 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3608 if (enabled == hdev->err_data_reporting)
3611 memset(&cp, 0, sizeof(cp));
3612 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
3613 ERR_DATA_REPORTING_DISABLED;
3615 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
3616 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3619 static const struct hci_init_stage hci_init4[] = {
3620 /* HCI_OP_DELETE_STORED_LINK_KEY */
3621 HCI_INIT(hci_delete_stored_link_key_sync),
3622 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
3623 HCI_INIT(hci_set_event_mask_page_2_sync),
3624 /* HCI_OP_READ_LOCAL_CODECS */
3625 HCI_INIT(hci_read_local_codecs_sync),
3626 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
3627 HCI_INIT(hci_read_local_pairing_opts_sync),
3628 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
3629 HCI_INIT(hci_get_mws_transport_config_sync),
3630 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
3631 HCI_INIT(hci_read_sync_train_params_sync),
3632 /* HCI_OP_WRITE_SC_SUPPORT */
3633 HCI_INIT(hci_write_sc_support_1_sync),
3634 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
3635 HCI_INIT(hci_set_err_data_report_sync),
3639 /* Set Suggested Default Data Length to maximum if supported */
3640 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
3642 struct hci_cp_le_write_def_data_len cp;
3644 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3647 memset(&cp, 0, sizeof(cp));
3648 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
3649 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
3651 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
3652 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3655 /* Set Default PHY parameters if command is supported */
3656 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
3658 struct hci_cp_le_set_default_phy cp;
3660 if (!(hdev->commands[35] & 0x20))
3663 memset(&cp, 0, sizeof(cp));
3665 cp.tx_phys = hdev->le_tx_def_phys;
3666 cp.rx_phys = hdev->le_rx_def_phys;
3668 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
3669 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3672 static const struct hci_init_stage le_init4[] = {
3673 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
3674 HCI_INIT(hci_le_set_write_def_data_len_sync),
3675 /* HCI_OP_LE_SET_DEFAULT_PHY */
3676 HCI_INIT(hci_le_set_default_phy_sync),
3680 static int hci_init4_sync(struct hci_dev *hdev)
3684 bt_dev_dbg(hdev, "");
3686 err = hci_init_stage_sync(hdev, hci_init4);
3690 if (lmp_le_capable(hdev))
3691 return hci_init_stage_sync(hdev, le_init4);
3696 static int hci_init_sync(struct hci_dev *hdev)
3700 err = hci_init1_sync(hdev);
3704 if (hci_dev_test_flag(hdev, HCI_SETUP))
3705 hci_debugfs_create_basic(hdev);
3707 err = hci_init2_sync(hdev);
3711 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
3712 * BR/EDR/LE type controllers. AMP controllers only need the
3713 * first two stages of init.
3715 if (hdev->dev_type != HCI_PRIMARY)
3718 err = hci_init3_sync(hdev);
3722 err = hci_init4_sync(hdev);
3726 /* This function is only called when the controller is actually in
3727 * configured state. When the controller is marked as unconfigured,
3728 * this initialization procedure is not run.
3730 * It means that it is possible that a controller runs through its
3731 * setup phase and then discovers missing settings. If that is the
3732 * case, then this function will not be called. It then will only
3733 * be called during the config phase.
3735 * So only when in setup phase or config phase, create the debugfs
3736 * entries and register the SMP channels.
3738 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3739 !hci_dev_test_flag(hdev, HCI_CONFIG))
3742 hci_debugfs_create_common(hdev);
3744 if (lmp_bredr_capable(hdev))
3745 hci_debugfs_create_bredr(hdev);
3747 if (lmp_le_capable(hdev))
3748 hci_debugfs_create_le(hdev);
3753 int hci_dev_open_sync(struct hci_dev *hdev)
3757 bt_dev_dbg(hdev, "");
3759 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
3764 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3765 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
3766 /* Check for rfkill but allow the HCI setup stage to
3767 * proceed (which in itself doesn't cause any RF activity).
3769 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
3774 /* Check for valid public address or a configured static
3775 * random address, but let the HCI setup proceed to
3776 * be able to determine if there is a public address
3779 * In case of user channel usage, it is not important
3780 * if a public address or static random address is
3783 * This check is only valid for BR/EDR controllers
3784 * since AMP controllers do not have an address.
3786 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
3787 hdev->dev_type == HCI_PRIMARY &&
3788 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3789 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
3790 ret = -EADDRNOTAVAIL;
3795 if (test_bit(HCI_UP, &hdev->flags)) {
3800 if (hdev->open(hdev)) {
3805 set_bit(HCI_RUNNING, &hdev->flags);
3806 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
3808 atomic_set(&hdev->cmd_cnt, 1);
3809 set_bit(HCI_INIT, &hdev->flags);
3811 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
3812 test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) {
3813 bool invalid_bdaddr;
3815 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
3818 ret = hdev->setup(hdev);
3820 /* The transport driver can set the quirk to mark the
3821 * BD_ADDR invalid before creating the HCI device or in
3822 * its setup callback.
3824 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR,
3830 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) {
3831 if (!bacmp(&hdev->public_addr, BDADDR_ANY))
3832 hci_dev_get_bd_addr_from_property(hdev);
3834 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
3836 ret = hdev->set_bdaddr(hdev,
3837 &hdev->public_addr);
3839 /* If setting of the BD_ADDR from the device
3840 * property succeeds, then treat the address
3841 * as valid even if the invalid BD_ADDR
3842 * quirk indicates otherwise.
3845 invalid_bdaddr = false;
3850 /* The transport driver can set these quirks before
3851 * creating the HCI device or in its setup callback.
3853 * For the invalid BD_ADDR quirk it is possible that
3854 * it becomes a valid address if the bootloader does
3855 * provide it (see above).
3857 * In case any of them is set, the controller has to
3858 * start up as unconfigured.
3860 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
3862 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
3864 /* For an unconfigured controller it is required to
3865 * read at least the version information provided by
3866 * the Read Local Version Information command.
3868 * If the set_bdaddr driver callback is provided, then
3869 * also the original Bluetooth public device address
3870 * will be read using the Read BD Address command.
3872 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
3873 ret = hci_unconf_init_sync(hdev);
3876 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
3877 /* If public address change is configured, ensure that
3878 * the address gets programmed. If the driver does not
3879 * support changing the public address, fail the power
3882 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
3884 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
3886 ret = -EADDRNOTAVAIL;
3890 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
3891 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
3892 ret = hci_init_sync(hdev);
3893 if (!ret && hdev->post_init)
3894 ret = hdev->post_init(hdev);
3898 /* If the HCI Reset command is clearing all diagnostic settings,
3899 * then they need to be reprogrammed after the init procedure
3902 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
3903 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
3904 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
3905 ret = hdev->set_diag(hdev, true);
3907 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
3912 clear_bit(HCI_INIT, &hdev->flags);
3916 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
3917 hci_adv_instances_set_rpa_expired(hdev, true);
3918 set_bit(HCI_UP, &hdev->flags);
3919 hci_sock_dev_event(hdev, HCI_DEV_UP);
3920 hci_leds_update_powered(hdev, true);
3921 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3922 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
3923 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
3924 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
3925 hci_dev_test_flag(hdev, HCI_MGMT) &&
3926 hdev->dev_type == HCI_PRIMARY) {
3927 ret = hci_powered_update_sync(hdev);
3930 /* Init failed, cleanup */
3931 flush_work(&hdev->tx_work);
3933 /* Since hci_rx_work() is possible to awake new cmd_work
3934 * it should be flushed first to avoid unexpected call of
3937 flush_work(&hdev->rx_work);
3938 flush_work(&hdev->cmd_work);
3940 skb_queue_purge(&hdev->cmd_q);
3941 skb_queue_purge(&hdev->rx_q);
3946 if (hdev->sent_cmd) {
3947 kfree_skb(hdev->sent_cmd);
3948 hdev->sent_cmd = NULL;
3951 clear_bit(HCI_RUNNING, &hdev->flags);
3952 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
3955 hdev->flags &= BIT(HCI_RAW);
3962 /* This function requires the caller holds hdev->lock */
3963 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
3965 struct hci_conn_params *p;
3967 list_for_each_entry(p, &hdev->le_conn_params, list) {
3969 hci_conn_drop(p->conn);
3970 hci_conn_put(p->conn);
3973 list_del_init(&p->action);
3976 BT_DBG("All LE pending actions cleared");
3979 int hci_dev_close_sync(struct hci_dev *hdev)
3984 bt_dev_dbg(hdev, "");
3986 cancel_delayed_work(&hdev->power_off);
3987 cancel_delayed_work(&hdev->ncmd_timer);
3989 hci_request_cancel_all(hdev);
3991 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
3992 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
3993 test_bit(HCI_UP, &hdev->flags)) {
3994 /* Execute vendor specific shutdown routine */
3996 err = hdev->shutdown(hdev);
3999 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4000 cancel_delayed_work_sync(&hdev->cmd_timer);
4004 hci_leds_update_powered(hdev, false);
4006 /* Flush RX and TX works */
4007 flush_work(&hdev->tx_work);
4008 flush_work(&hdev->rx_work);
4010 if (hdev->discov_timeout > 0) {
4011 hdev->discov_timeout = 0;
4012 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4013 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
4016 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
4017 cancel_delayed_work(&hdev->service_cache);
4019 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4020 struct adv_info *adv_instance;
4022 cancel_delayed_work_sync(&hdev->rpa_expired);
4024 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
4025 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
4028 /* Avoid potential lockdep warnings from the *_flush() calls by
4029 * ensuring the workqueue is empty up front.
4031 drain_workqueue(hdev->workqueue);
4035 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4037 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
4039 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
4040 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4041 hci_dev_test_flag(hdev, HCI_MGMT))
4042 __mgmt_power_off(hdev);
4044 hci_inquiry_cache_flush(hdev);
4045 hci_pend_le_actions_clear(hdev);
4046 hci_conn_hash_flush(hdev);
4047 hci_dev_unlock(hdev);
4049 smp_unregister(hdev);
4051 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
4053 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4054 aosp_do_close(hdev);
4055 msft_do_close(hdev);
4062 skb_queue_purge(&hdev->cmd_q);
4063 atomic_set(&hdev->cmd_cnt, 1);
4064 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
4065 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4066 set_bit(HCI_INIT, &hdev->flags);
4067 hci_reset_sync(hdev);
4068 clear_bit(HCI_INIT, &hdev->flags);
4071 /* flush cmd work */
4072 flush_work(&hdev->cmd_work);
4075 skb_queue_purge(&hdev->rx_q);
4076 skb_queue_purge(&hdev->cmd_q);
4077 skb_queue_purge(&hdev->raw_q);
4079 /* Drop last sent command */
4080 if (hdev->sent_cmd) {
4081 cancel_delayed_work_sync(&hdev->cmd_timer);
4082 kfree_skb(hdev->sent_cmd);
4083 hdev->sent_cmd = NULL;
4086 clear_bit(HCI_RUNNING, &hdev->flags);
4087 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4089 /* After this point our queues are empty and no tasks are scheduled. */
4093 hdev->flags &= BIT(HCI_RAW);
4094 hci_dev_clear_volatile_flags(hdev);
4096 /* Controller radio is available but is currently powered down */
4097 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
4099 memset(hdev->eir, 0, sizeof(hdev->eir));
4100 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
4101 bacpy(&hdev->random_addr, BDADDR_ANY);
4107 /* This function perform power on HCI command sequence as follows:
4109 * If controller is already up (HCI_UP) performs hci_powered_update_sync
4110 * sequence otherwise run hci_dev_open_sync which will follow with
4111 * hci_powered_update_sync after the init sequence is completed.
4113 static int hci_power_on_sync(struct hci_dev *hdev)
4117 if (test_bit(HCI_UP, &hdev->flags) &&
4118 hci_dev_test_flag(hdev, HCI_MGMT) &&
4119 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
4120 cancel_delayed_work(&hdev->power_off);
4121 return hci_powered_update_sync(hdev);
4124 err = hci_dev_open_sync(hdev);
4128 /* During the HCI setup phase, a few error conditions are
4129 * ignored and they need to be checked now. If they are still
4130 * valid, it is important to return the device back off.
4132 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
4133 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
4134 (hdev->dev_type == HCI_PRIMARY &&
4135 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4136 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
4137 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
4138 hci_dev_close_sync(hdev);
4139 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
4140 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
4141 HCI_AUTO_OFF_TIMEOUT);
4144 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
4145 /* For unconfigured devices, set the HCI_RAW flag
4146 * so that userspace can easily identify them.
4148 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4149 set_bit(HCI_RAW, &hdev->flags);
4151 /* For fully configured devices, this will send
4152 * the Index Added event. For unconfigured devices,
4153 * it will send Unconfigued Index Added event.
4155 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
4156 * and no event will be send.
4158 mgmt_index_added(hdev);
4159 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
4160 /* When the controller is now configured, then it
4161 * is important to clear the HCI_RAW flag.
4163 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4164 clear_bit(HCI_RAW, &hdev->flags);
4166 /* Powering on the controller with HCI_CONFIG set only
4167 * happens with the transition from unconfigured to
4168 * configured. This will send the Index Added event.
4170 mgmt_index_added(hdev);
4176 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
4178 struct hci_cp_remote_name_req_cancel cp;
4180 memset(&cp, 0, sizeof(cp));
4181 bacpy(&cp.bdaddr, addr);
4183 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
4184 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4187 int hci_stop_discovery_sync(struct hci_dev *hdev)
4189 struct discovery_state *d = &hdev->discovery;
4190 struct inquiry_entry *e;
4193 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
4195 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
4196 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
4197 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
4198 0, NULL, HCI_CMD_TIMEOUT);
4203 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
4204 cancel_delayed_work(&hdev->le_scan_disable);
4205 cancel_delayed_work(&hdev->le_scan_restart);
4207 err = hci_scan_disable_sync(hdev);
4213 err = hci_scan_disable_sync(hdev);
4218 /* Resume advertising if it was paused */
4219 if (use_ll_privacy(hdev))
4220 hci_resume_advertising_sync(hdev);
4222 /* No further actions needed for LE-only discovery */
4223 if (d->type == DISCOV_TYPE_LE)
4226 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
4227 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
4232 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
4238 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
4241 struct hci_cp_disconn_phy_link cp;
4243 memset(&cp, 0, sizeof(cp));
4244 cp.phy_handle = HCI_PHY_HANDLE(handle);
4247 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
4248 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4251 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
4254 struct hci_cp_disconnect cp;
4256 if (conn->type == AMP_LINK)
4257 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
4259 memset(&cp, 0, sizeof(cp));
4260 cp.handle = cpu_to_le16(conn->handle);
4263 /* Wait for HCI_EV_DISCONN_COMPLETE not HCI_EV_CMD_STATUS when not
4266 if (!hdev->suspended)
4267 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
4269 HCI_EV_DISCONN_COMPLETE,
4270 HCI_CMD_TIMEOUT, NULL);
4272 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
4276 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
4277 struct hci_conn *conn)
4279 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
4282 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
4283 6, &conn->dst, HCI_CMD_TIMEOUT);
4286 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn)
4288 if (conn->type == LE_LINK)
4289 return hci_le_connect_cancel_sync(hdev, conn);
4291 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
4294 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
4295 6, &conn->dst, HCI_CMD_TIMEOUT);
4298 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
4301 struct hci_cp_reject_sync_conn_req cp;
4303 memset(&cp, 0, sizeof(cp));
4304 bacpy(&cp.bdaddr, &conn->dst);
4307 /* SCO rejection has its own limited set of
4308 * allowed error values (0x0D-0x0F).
4310 if (reason < 0x0d || reason > 0x0f)
4311 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
4313 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
4314 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4317 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
4320 struct hci_cp_reject_conn_req cp;
4322 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
4323 return hci_reject_sco_sync(hdev, conn, reason);
4325 memset(&cp, 0, sizeof(cp));
4326 bacpy(&cp.bdaddr, &conn->dst);
4329 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
4330 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4333 static int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
4336 switch (conn->state) {
4339 return hci_disconnect_sync(hdev, conn, reason);
4341 return hci_connect_cancel_sync(hdev, conn);
4343 return hci_reject_conn_sync(hdev, conn, reason);
4345 conn->state = BT_CLOSED;
4352 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
4354 struct hci_conn *conn, *tmp;
4357 list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
4358 err = hci_abort_conn_sync(hdev, conn, reason);
4366 /* This function perform power off HCI command sequence as follows:
4370 * Disconnect all connections
4371 * hci_dev_close_sync
4373 static int hci_power_off_sync(struct hci_dev *hdev)
4377 /* If controller is already down there is nothing to do */
4378 if (!test_bit(HCI_UP, &hdev->flags))
4381 if (test_bit(HCI_ISCAN, &hdev->flags) ||
4382 test_bit(HCI_PSCAN, &hdev->flags)) {
4383 err = hci_write_scan_enable_sync(hdev, 0x00);
4388 err = hci_clear_adv_sync(hdev, NULL, false);
4392 err = hci_stop_discovery_sync(hdev);
4396 /* Terminated due to Power Off */
4397 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
4401 return hci_dev_close_sync(hdev);
4404 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
4407 return hci_power_on_sync(hdev);
4409 return hci_power_off_sync(hdev);
4412 static int hci_write_iac_sync(struct hci_dev *hdev)
4414 struct hci_cp_write_current_iac_lap cp;
4416 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
4419 memset(&cp, 0, sizeof(cp));
4421 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
4422 /* Limited discoverable mode */
4423 cp.num_iac = min_t(u8, hdev->num_iac, 2);
4424 cp.iac_lap[0] = 0x00; /* LIAC */
4425 cp.iac_lap[1] = 0x8b;
4426 cp.iac_lap[2] = 0x9e;
4427 cp.iac_lap[3] = 0x33; /* GIAC */
4428 cp.iac_lap[4] = 0x8b;
4429 cp.iac_lap[5] = 0x9e;
4431 /* General discoverable mode */
4433 cp.iac_lap[0] = 0x33; /* GIAC */
4434 cp.iac_lap[1] = 0x8b;
4435 cp.iac_lap[2] = 0x9e;
4438 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
4439 (cp.num_iac * 3) + 1, &cp,
4443 int hci_update_discoverable_sync(struct hci_dev *hdev)
4447 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
4448 err = hci_write_iac_sync(hdev);
4452 err = hci_update_scan_sync(hdev);
4456 err = hci_update_class_sync(hdev);
4461 /* Advertising instances don't use the global discoverable setting, so
4462 * only update AD if advertising was enabled using Set Advertising.
4464 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
4465 err = hci_update_adv_data_sync(hdev, 0x00);
4469 /* Discoverable mode affects the local advertising
4470 * address in limited privacy mode.
4472 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
4473 if (ext_adv_capable(hdev))
4474 err = hci_start_ext_adv_sync(hdev, 0x00);
4476 err = hci_enable_advertising_sync(hdev);
4483 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
4485 return hci_update_discoverable_sync(hdev);
4488 int hci_update_discoverable(struct hci_dev *hdev)
4490 /* Only queue if it would have any effect */
4491 if (hdev_is_powered(hdev) &&
4492 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4493 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
4494 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
4495 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
4501 int hci_update_connectable_sync(struct hci_dev *hdev)
4505 err = hci_update_scan_sync(hdev);
4509 /* If BR/EDR is not enabled and we disable advertising as a
4510 * by-product of disabling connectable, we need to update the
4511 * advertising flags.
4513 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4514 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
4516 /* Update the advertising parameters if necessary */
4517 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4518 !list_empty(&hdev->adv_instances)) {
4519 if (ext_adv_capable(hdev))
4520 err = hci_start_ext_adv_sync(hdev,
4521 hdev->cur_adv_instance);
4523 err = hci_enable_advertising_sync(hdev);
4529 return hci_update_passive_scan_sync(hdev);
4532 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
4534 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
4535 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
4536 struct hci_cp_inquiry cp;
4538 bt_dev_dbg(hdev, "");
4540 if (hci_dev_test_flag(hdev, HCI_INQUIRY))
4544 hci_inquiry_cache_flush(hdev);
4545 hci_dev_unlock(hdev);
4547 memset(&cp, 0, sizeof(cp));
4549 if (hdev->discovery.limited)
4550 memcpy(&cp.lap, liac, sizeof(cp.lap));
4552 memcpy(&cp.lap, giac, sizeof(cp.lap));
4556 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
4557 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4560 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
4563 /* Accept list is not used for discovery */
4564 u8 filter_policy = 0x00;
4565 /* Default is to enable duplicates filter */
4566 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
4569 bt_dev_dbg(hdev, "");
4571 /* If controller is scanning, it means the passive scanning is
4572 * running. Thus, we should temporarily stop it in order to set the
4573 * discovery scanning parameters.
4575 err = hci_scan_disable_sync(hdev);
4577 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
4581 cancel_interleave_scan(hdev);
4583 /* Pause advertising since active scanning disables address resolution
4584 * which advertising depend on in order to generate its RPAs.
4586 if (use_ll_privacy(hdev)) {
4587 err = hci_pause_advertising_sync(hdev);
4589 bt_dev_err(hdev, "pause advertising failed: %d", err);
4594 /* Disable address resolution while doing active scanning since the
4595 * accept list shall not be used and all reports shall reach the host
4598 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
4600 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
4605 /* All active scans will be done with either a resolvable private
4606 * address (when privacy feature has been enabled) or non-resolvable
4609 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
4612 own_addr_type = ADDR_LE_DEV_PUBLIC;
4614 if (hci_is_adv_monitoring(hdev)) {
4615 /* Duplicate filter should be disabled when some advertisement
4616 * monitor is activated, otherwise AdvMon can only receive one
4617 * advertisement for one peer(*) during active scanning, and
4618 * might report loss to these peers.
4620 * Note that different controllers have different meanings of
4621 * |duplicate|. Some of them consider packets with the same
4622 * address as duplicate, and others consider packets with the
4623 * same address and the same RSSI as duplicate. Although in the
4624 * latter case we don't need to disable duplicate filter, but
4625 * it is common to have active scanning for a short period of
4626 * time, the power impact should be neglectable.
4628 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
4631 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
4632 hdev->le_scan_window_discovery,
4633 own_addr_type, filter_policy, filter_dup);
4638 /* Resume advertising if it was paused */
4639 if (use_ll_privacy(hdev))
4640 hci_resume_advertising_sync(hdev);
4642 /* Resume passive scanning */
4643 hci_update_passive_scan_sync(hdev);
4647 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
4651 bt_dev_dbg(hdev, "");
4653 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
4657 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
4660 int hci_start_discovery_sync(struct hci_dev *hdev)
4662 unsigned long timeout;
4665 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
4667 switch (hdev->discovery.type) {
4668 case DISCOV_TYPE_BREDR:
4669 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
4670 case DISCOV_TYPE_INTERLEAVED:
4671 /* When running simultaneous discovery, the LE scanning time
4672 * should occupy the whole discovery time sine BR/EDR inquiry
4673 * and LE scanning are scheduled by the controller.
4675 * For interleaving discovery in comparison, BR/EDR inquiry
4676 * and LE scanning are done sequentially with separate
4679 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
4681 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4682 /* During simultaneous discovery, we double LE scan
4683 * interval. We must leave some time for the controller
4684 * to do BR/EDR inquiry.
4686 err = hci_start_interleaved_discovery_sync(hdev);
4690 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
4691 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
4693 case DISCOV_TYPE_LE:
4694 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4695 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
4704 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
4706 /* When service discovery is used and the controller has a
4707 * strict duplicate filter, it is important to remember the
4708 * start and duration of the scan. This is required for
4709 * restarting scanning during the discovery phase.
4711 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
4712 hdev->discovery.result_filtering) {
4713 hdev->discovery.scan_start = jiffies;
4714 hdev->discovery.scan_duration = timeout;
4717 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
4722 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
4724 switch (hci_get_adv_monitor_offload_ext(hdev)) {
4725 case HCI_ADV_MONITOR_EXT_MSFT:
4726 msft_suspend_sync(hdev);
4733 /* This function disables discovery and mark it as paused */
4734 static int hci_pause_discovery_sync(struct hci_dev *hdev)
4736 int old_state = hdev->discovery.state;
4739 /* If discovery already stopped/stopping/paused there nothing to do */
4740 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
4741 hdev->discovery_paused)
4744 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
4745 err = hci_stop_discovery_sync(hdev);
4749 hdev->discovery_paused = true;
4750 hdev->discovery_old_state = old_state;
4751 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4756 static int hci_update_event_filter_sync(struct hci_dev *hdev)
4758 struct bdaddr_list_with_flags *b;
4759 u8 scan = SCAN_DISABLED;
4760 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
4763 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4766 /* Always clear event filter when starting */
4767 hci_clear_event_filter_sync(hdev);
4769 list_for_each_entry(b, &hdev->accept_list, list) {
4770 if (!hci_conn_test_flag(HCI_CONN_FLAG_REMOTE_WAKEUP,
4774 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
4776 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
4777 HCI_CONN_SETUP_ALLOW_BDADDR,
4779 HCI_CONN_SETUP_AUTO_ON);
4781 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
4787 if (scan && !scanning)
4788 hci_write_scan_enable_sync(hdev, scan);
4789 else if (!scan && scanning)
4790 hci_write_scan_enable_sync(hdev, scan);
4795 /* This function performs the HCI suspend procedures in the follow order:
4797 * Pause discovery (active scanning/inquiry)
4798 * Pause Directed Advertising/Advertising
4799 * Disconnect all connections
4800 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
4802 * Update event mask (only set events that are allowed to wake up the host)
4803 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
4804 * Update passive scanning (lower duty cycle)
4805 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
4807 int hci_suspend_sync(struct hci_dev *hdev)
4811 /* If marked as suspended there nothing to do */
4812 if (hdev->suspended)
4815 /* Mark device as suspended */
4816 hdev->suspended = true;
4818 /* Pause discovery if not already stopped */
4819 hci_pause_discovery_sync(hdev);
4821 /* Pause other advertisements */
4822 hci_pause_advertising_sync(hdev);
4824 /* Disable page scan if enabled */
4825 if (test_bit(HCI_PSCAN, &hdev->flags))
4826 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
4828 /* Suspend monitor filters */
4829 hci_suspend_monitor_sync(hdev);
4831 /* Prevent disconnects from causing scanning to be re-enabled */
4832 hdev->scanning_paused = true;
4834 /* Soft disconnect everything (power off) */
4835 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
4837 /* Set state to BT_RUNNING so resume doesn't notify */
4838 hdev->suspend_state = BT_RUNNING;
4839 hci_resume_sync(hdev);
4843 /* Only configure accept list if disconnect succeeded and wake
4844 * isn't being prevented.
4846 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
4847 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
4851 /* Unpause to take care of updating scanning params */
4852 hdev->scanning_paused = false;
4854 /* Update event mask so only the allowed event can wakeup the host */
4855 hci_set_event_mask_sync(hdev);
4857 /* Enable event filter for paired devices */
4858 hci_update_event_filter_sync(hdev);
4860 /* Update LE passive scan if enabled */
4861 hci_update_passive_scan_sync(hdev);
4863 /* Pause scan changes again. */
4864 hdev->scanning_paused = true;
4866 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
4871 /* This function resumes discovery */
4872 static int hci_resume_discovery_sync(struct hci_dev *hdev)
4876 /* If discovery not paused there nothing to do */
4877 if (!hdev->discovery_paused)
4880 hdev->discovery_paused = false;
4882 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4884 err = hci_start_discovery_sync(hdev);
4886 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
4892 static void hci_resume_monitor_sync(struct hci_dev *hdev)
4894 switch (hci_get_adv_monitor_offload_ext(hdev)) {
4895 case HCI_ADV_MONITOR_EXT_MSFT:
4896 msft_resume_sync(hdev);
4903 /* This function performs the HCI suspend procedures in the follow order:
4905 * Restore event mask
4906 * Clear event filter
4907 * Update passive scanning (normal duty cycle)
4908 * Resume Directed Advertising/Advertising
4909 * Resume discovery (active scanning/inquiry)
4911 int hci_resume_sync(struct hci_dev *hdev)
4913 /* If not marked as suspended there nothing to do */
4914 if (!hdev->suspended)
4917 hdev->suspended = false;
4918 hdev->scanning_paused = false;
4920 /* Restore event mask */
4921 hci_set_event_mask_sync(hdev);
4923 /* Clear any event filters and restore scan state */
4924 hci_clear_event_filter_sync(hdev);
4925 hci_update_scan_sync(hdev);
4927 /* Reset passive scanning to normal */
4928 hci_update_passive_scan_sync(hdev);
4930 /* Resume monitor filters */
4931 hci_resume_monitor_sync(hdev);
4933 /* Resume other advertisements */
4934 hci_resume_advertising_sync(hdev);
4936 /* Resume discovery */
4937 hci_resume_discovery_sync(hdev);