1 // SPDX-License-Identifier: GPL-2.0
3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2021 Intel Corporation
8 #include <linux/property.h>
10 #include <net/bluetooth/bluetooth.h>
11 #include <net/bluetooth/hci_core.h>
12 #include <net/bluetooth/mgmt.h>
14 #include "hci_request.h"
15 #include "hci_debugfs.h"
22 static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
25 bt_dev_dbg(hdev, "result 0x%2.2x", result);
27 if (hdev->req_status != HCI_REQ_PEND)
30 hdev->req_result = result;
31 hdev->req_status = HCI_REQ_DONE;
34 struct sock *sk = hci_skb_sk(skb);
36 /* Drop sk reference if set */
40 hdev->req_skb = skb_get(skb);
43 wake_up_interruptible(&hdev->req_wait_q);
46 static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
47 u32 plen, const void *param,
50 int len = HCI_COMMAND_HDR_SIZE + plen;
51 struct hci_command_hdr *hdr;
54 skb = bt_skb_alloc(len, GFP_ATOMIC);
58 hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
59 hdr->opcode = cpu_to_le16(opcode);
63 skb_put_data(skb, param, plen);
65 bt_dev_dbg(hdev, "skb len %d", skb->len);
67 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
68 hci_skb_opcode(skb) = opcode;
70 /* Grab a reference if command needs to be associated with a sock (e.g.
71 * likely mgmt socket that initiated the command).
81 static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
82 const void *param, u8 event, struct sock *sk)
84 struct hci_dev *hdev = req->hdev;
87 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
89 /* If an error occurred during request building, there is no point in
90 * queueing the HCI command. We can simply return.
95 skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
97 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
103 if (skb_queue_empty(&req->cmd_q))
104 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
106 hci_skb_event(skb) = event;
108 skb_queue_tail(&req->cmd_q, skb);
111 static int hci_cmd_sync_run(struct hci_request *req)
113 struct hci_dev *hdev = req->hdev;
117 bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
119 /* If an error occurred during request building, remove all HCI
120 * commands queued on the HCI request queue.
123 skb_queue_purge(&req->cmd_q);
127 /* Do not allow empty requests */
128 if (skb_queue_empty(&req->cmd_q))
131 skb = skb_peek_tail(&req->cmd_q);
132 bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
133 bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
135 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
136 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
137 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
139 queue_work(hdev->workqueue, &hdev->cmd_work);
144 /* This function requires the caller holds hdev->req_lock. */
145 struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
146 const void *param, u8 event, u32 timeout,
149 struct hci_request req;
153 bt_dev_dbg(hdev, "Opcode 0x%4x", opcode);
155 hci_req_init(&req, hdev);
157 hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
159 hdev->req_status = HCI_REQ_PEND;
161 err = hci_cmd_sync_run(&req);
165 err = wait_event_interruptible_timeout(hdev->req_wait_q,
166 hdev->req_status != HCI_REQ_PEND,
169 if (err == -ERESTARTSYS)
170 return ERR_PTR(-EINTR);
172 switch (hdev->req_status) {
174 err = -bt_to_errno(hdev->req_result);
177 case HCI_REQ_CANCELED:
178 err = -hdev->req_result;
186 hdev->req_status = 0;
187 hdev->req_result = 0;
189 hdev->req_skb = NULL;
191 bt_dev_dbg(hdev, "end: err %d", err);
200 EXPORT_SYMBOL(__hci_cmd_sync_sk);
202 /* This function requires the caller holds hdev->req_lock. */
203 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
204 const void *param, u32 timeout)
206 return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
208 EXPORT_SYMBOL(__hci_cmd_sync);
210 /* Send HCI command and wait for command complete event */
211 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
212 const void *param, u32 timeout)
216 if (!test_bit(HCI_UP, &hdev->flags))
217 return ERR_PTR(-ENETDOWN);
219 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
221 hci_req_sync_lock(hdev);
222 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
223 hci_req_sync_unlock(hdev);
227 EXPORT_SYMBOL(hci_cmd_sync);
229 /* This function requires the caller holds hdev->req_lock. */
230 struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
231 const void *param, u8 event, u32 timeout)
233 return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
236 EXPORT_SYMBOL(__hci_cmd_sync_ev);
238 /* This function requires the caller holds hdev->req_lock. */
239 int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
240 const void *param, u8 event, u32 timeout,
246 skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
248 bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode,
253 /* If command return a status event skb will be set to NULL as there are
254 * no parameters, in case of failure IS_ERR(skb) would have be set to
255 * the actual error would be found with PTR_ERR(skb).
260 status = skb->data[0];
266 EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
268 int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
269 const void *param, u32 timeout)
271 return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
274 EXPORT_SYMBOL(__hci_cmd_sync_status);
276 static void hci_cmd_sync_work(struct work_struct *work)
278 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
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 static void hci_cmd_sync_cancel_work(struct work_struct *work)
318 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
320 cancel_delayed_work_sync(&hdev->cmd_timer);
321 cancel_delayed_work_sync(&hdev->ncmd_timer);
322 atomic_set(&hdev->cmd_cnt, 1);
324 wake_up_interruptible(&hdev->req_wait_q);
327 void hci_cmd_sync_init(struct hci_dev *hdev)
329 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
330 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
331 mutex_init(&hdev->cmd_sync_work_lock);
333 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
336 void hci_cmd_sync_clear(struct hci_dev *hdev)
338 struct hci_cmd_sync_work_entry *entry, *tmp;
340 cancel_work_sync(&hdev->cmd_sync_work);
342 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
344 entry->destroy(hdev, entry->data, -ECANCELED);
346 list_del(&entry->list);
351 void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
353 bt_dev_dbg(hdev, "err 0x%2.2x", err);
355 if (hdev->req_status == HCI_REQ_PEND) {
356 hdev->req_result = err;
357 hdev->req_status = HCI_REQ_CANCELED;
359 cancel_delayed_work_sync(&hdev->cmd_timer);
360 cancel_delayed_work_sync(&hdev->ncmd_timer);
361 atomic_set(&hdev->cmd_cnt, 1);
363 wake_up_interruptible(&hdev->req_wait_q);
367 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
369 bt_dev_dbg(hdev, "err 0x%2.2x", err);
371 if (hdev->req_status == HCI_REQ_PEND) {
372 hdev->req_result = err;
373 hdev->req_status = HCI_REQ_CANCELED;
375 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
378 EXPORT_SYMBOL(hci_cmd_sync_cancel);
380 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
381 void *data, hci_cmd_sync_work_destroy_t destroy)
383 struct hci_cmd_sync_work_entry *entry;
385 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
391 entry->destroy = destroy;
393 mutex_lock(&hdev->cmd_sync_work_lock);
394 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
395 mutex_unlock(&hdev->cmd_sync_work_lock);
397 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
401 EXPORT_SYMBOL(hci_cmd_sync_queue);
403 int hci_update_eir_sync(struct hci_dev *hdev)
405 struct hci_cp_write_eir cp;
407 bt_dev_dbg(hdev, "");
409 if (!hdev_is_powered(hdev))
412 if (!lmp_ext_inq_capable(hdev))
415 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
418 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
421 memset(&cp, 0, sizeof(cp));
423 eir_create(hdev, cp.data);
425 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
428 memcpy(hdev->eir, cp.data, sizeof(cp.data));
430 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
434 static u8 get_service_classes(struct hci_dev *hdev)
436 struct bt_uuid *uuid;
439 list_for_each_entry(uuid, &hdev->uuids, list)
440 val |= uuid->svc_hint;
445 int hci_update_class_sync(struct hci_dev *hdev)
449 bt_dev_dbg(hdev, "");
451 if (!hdev_is_powered(hdev))
454 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
457 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
460 cod[0] = hdev->minor_class;
461 cod[1] = hdev->major_class;
462 cod[2] = get_service_classes(hdev);
464 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
467 if (memcmp(cod, hdev->dev_class, 3) == 0)
470 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
471 sizeof(cod), cod, HCI_CMD_TIMEOUT);
474 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
476 /* If there is no connection we are OK to advertise. */
477 if (hci_conn_num(hdev, LE_LINK) == 0)
480 /* Check le_states if there is any connection in peripheral role. */
481 if (hdev->conn_hash.le_num_peripheral > 0) {
482 /* Peripheral connection state and non connectable mode
485 if (!connectable && !(hdev->le_states[2] & 0x10))
488 /* Peripheral connection state and connectable mode bit 38
489 * and scannable bit 21.
491 if (connectable && (!(hdev->le_states[4] & 0x40) ||
492 !(hdev->le_states[2] & 0x20)))
496 /* Check le_states if there is any connection in central role. */
497 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
498 /* Central connection state and non connectable mode bit 18. */
499 if (!connectable && !(hdev->le_states[2] & 0x02))
502 /* Central connection state and connectable mode bit 35 and
505 if (connectable && (!(hdev->le_states[4] & 0x08) ||
506 !(hdev->le_states[2] & 0x08)))
513 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
515 /* If privacy is not enabled don't use RPA */
516 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
519 /* If basic privacy mode is enabled use RPA */
520 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
523 /* If limited privacy mode is enabled don't use RPA if we're
524 * both discoverable and bondable.
526 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
527 hci_dev_test_flag(hdev, HCI_BONDABLE))
530 /* We're neither bondable nor discoverable in the limited
531 * privacy mode, therefore use RPA.
536 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
538 /* If we're advertising or initiating an LE connection we can't
539 * go ahead and change the random address at this time. This is
540 * because the eventual initiator address used for the
541 * subsequently created connection will be undefined (some
542 * controllers use the new address and others the one we had
543 * when the operation started).
545 * In this kind of scenario skip the update and let the random
546 * address be updated at the next cycle.
548 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
549 hci_lookup_le_connect(hdev)) {
550 bt_dev_dbg(hdev, "Deferring random address update");
551 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
555 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
556 6, rpa, HCI_CMD_TIMEOUT);
559 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
560 bool rpa, u8 *own_addr_type)
564 /* If privacy is enabled use a resolvable private address. If
565 * current RPA has expired or there is something else than
566 * the current RPA in use, then generate a new one.
569 /* If Controller supports LL Privacy use own address type is
572 if (use_ll_privacy(hdev))
573 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
575 *own_addr_type = ADDR_LE_DEV_RANDOM;
577 /* Check if RPA is valid */
581 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
583 bt_dev_err(hdev, "failed to generate new RPA");
587 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
594 /* In case of required privacy without resolvable private address,
595 * use an non-resolvable private address. This is useful for active
596 * scanning and non-connectable advertising.
598 if (require_privacy) {
602 /* The non-resolvable private address is generated
603 * from random six bytes with the two most significant
606 get_random_bytes(&nrpa, 6);
609 /* The non-resolvable private address shall not be
610 * equal to the public address.
612 if (bacmp(&hdev->bdaddr, &nrpa))
616 *own_addr_type = ADDR_LE_DEV_RANDOM;
618 return hci_set_random_addr_sync(hdev, &nrpa);
621 /* If forcing static address is in use or there is no public
622 * address use the static address as random address (but skip
623 * the HCI command if the current random address is already the
626 * In case BR/EDR has been disabled on a dual-mode controller
627 * and a static address has been configured, then use that
628 * address instead of the public BR/EDR address.
630 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
631 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
632 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
633 bacmp(&hdev->static_addr, BDADDR_ANY))) {
634 *own_addr_type = ADDR_LE_DEV_RANDOM;
635 if (bacmp(&hdev->static_addr, &hdev->random_addr))
636 return hci_set_random_addr_sync(hdev,
641 /* Neither privacy nor static address is being used so use a
644 *own_addr_type = ADDR_LE_DEV_PUBLIC;
649 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
651 struct hci_cp_le_set_ext_adv_enable *cp;
652 struct hci_cp_ext_adv_set *set;
653 u8 data[sizeof(*cp) + sizeof(*set) * 1];
656 /* If request specifies an instance that doesn't exist, fail */
658 struct adv_info *adv;
660 adv = hci_find_adv_instance(hdev, instance);
664 /* If not enabled there is nothing to do */
669 memset(data, 0, sizeof(data));
672 set = (void *)cp->data;
674 /* Instance 0x00 indicates all advertising instances will be disabled */
675 cp->num_of_sets = !!instance;
678 set->handle = instance;
680 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
682 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
683 size, data, HCI_CMD_TIMEOUT);
686 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
687 bdaddr_t *random_addr)
689 struct hci_cp_le_set_adv_set_rand_addr cp;
693 /* Instance 0x00 doesn't have an adv_info, instead it uses
694 * hdev->random_addr to track its address so whenever it needs
695 * to be updated this also set the random address since
696 * hdev->random_addr is shared with scan state machine.
698 err = hci_set_random_addr_sync(hdev, random_addr);
703 memset(&cp, 0, sizeof(cp));
705 cp.handle = instance;
706 bacpy(&cp.bdaddr, random_addr);
708 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
709 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
712 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
714 struct hci_cp_le_set_ext_adv_params cp;
717 bdaddr_t random_addr;
720 struct adv_info *adv;
724 adv = hci_find_adv_instance(hdev, instance);
731 /* Updating parameters of an active instance will return a
732 * Command Disallowed error, so we must first disable the
733 * instance if it is active.
735 if (adv && !adv->pending) {
736 err = hci_disable_ext_adv_instance_sync(hdev, instance);
741 flags = hci_adv_instance_flags(hdev, instance);
743 /* If the "connectable" instance flag was not set, then choose between
744 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
746 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
747 mgmt_get_connectable(hdev);
749 if (!is_advertising_allowed(hdev, connectable))
752 /* Set require_privacy to true only when non-connectable
753 * advertising is used. In that case it is fine to use a
754 * non-resolvable private address.
756 err = hci_get_random_address(hdev, !connectable,
757 adv_use_rpa(hdev, flags), adv,
758 &own_addr_type, &random_addr);
762 memset(&cp, 0, sizeof(cp));
765 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
766 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
767 cp.tx_power = adv->tx_power;
769 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
770 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
771 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
774 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
778 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
780 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
781 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
782 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
784 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
786 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
789 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
791 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
794 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
795 * contains the peer’s Identity Address and the Peer_Address_Type
796 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
797 * These parameters are used to locate the corresponding local IRK in
798 * the resolving list; this IRK is used to generate their own address
799 * used in the advertisement.
801 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
802 hci_copy_identity_address(hdev, &cp.peer_addr,
805 cp.own_addr_type = own_addr_type;
806 cp.channel_map = hdev->le_adv_channel_map;
807 cp.handle = instance;
809 if (flags & MGMT_ADV_FLAG_SEC_2M) {
810 cp.primary_phy = HCI_ADV_PHY_1M;
811 cp.secondary_phy = HCI_ADV_PHY_2M;
812 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
813 cp.primary_phy = HCI_ADV_PHY_CODED;
814 cp.secondary_phy = HCI_ADV_PHY_CODED;
816 /* In all other cases use 1M */
817 cp.primary_phy = HCI_ADV_PHY_1M;
818 cp.secondary_phy = HCI_ADV_PHY_1M;
821 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
822 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
826 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
827 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
828 bacmp(&random_addr, BDADDR_ANY)) {
829 /* Check if random address need to be updated */
831 if (!bacmp(&random_addr, &adv->random_addr))
834 if (!bacmp(&random_addr, &hdev->random_addr))
838 return hci_set_adv_set_random_addr_sync(hdev, instance,
845 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
848 struct hci_cp_le_set_ext_scan_rsp_data cp;
849 u8 data[HCI_MAX_EXT_AD_LENGTH];
853 memset(&pdu, 0, sizeof(pdu));
855 len = eir_create_scan_rsp(hdev, instance, pdu.data);
857 if (hdev->scan_rsp_data_len == len &&
858 !memcmp(pdu.data, hdev->scan_rsp_data, len))
861 memcpy(hdev->scan_rsp_data, pdu.data, len);
862 hdev->scan_rsp_data_len = len;
864 pdu.cp.handle = instance;
866 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
867 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
869 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
870 sizeof(pdu.cp) + len, &pdu.cp,
874 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
876 struct hci_cp_le_set_scan_rsp_data cp;
879 memset(&cp, 0, sizeof(cp));
881 len = eir_create_scan_rsp(hdev, instance, cp.data);
883 if (hdev->scan_rsp_data_len == len &&
884 !memcmp(cp.data, hdev->scan_rsp_data, len))
887 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
888 hdev->scan_rsp_data_len = len;
892 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
893 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
896 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
898 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
901 if (ext_adv_capable(hdev))
902 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
904 return __hci_set_scan_rsp_data_sync(hdev, instance);
907 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
909 struct hci_cp_le_set_ext_adv_enable *cp;
910 struct hci_cp_ext_adv_set *set;
911 u8 data[sizeof(*cp) + sizeof(*set) * 1];
912 struct adv_info *adv;
915 adv = hci_find_adv_instance(hdev, instance);
918 /* If already enabled there is nothing to do */
926 set = (void *)cp->data;
928 memset(cp, 0, sizeof(*cp));
931 cp->num_of_sets = 0x01;
933 memset(set, 0, sizeof(*set));
935 set->handle = instance;
937 /* Set duration per instance since controller is responsible for
940 if (adv && adv->timeout) {
941 u16 duration = adv->timeout * MSEC_PER_SEC;
943 /* Time = N * 10 ms */
944 set->duration = cpu_to_le16(duration / 10);
947 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
949 sizeof(*set) * cp->num_of_sets,
950 data, HCI_CMD_TIMEOUT);
953 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
957 err = hci_setup_ext_adv_instance_sync(hdev, instance);
961 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
965 return hci_enable_ext_advertising_sync(hdev, instance);
968 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
972 if (ext_adv_capable(hdev))
973 return hci_start_ext_adv_sync(hdev, instance);
975 err = hci_update_adv_data_sync(hdev, instance);
979 err = hci_update_scan_rsp_data_sync(hdev, instance);
983 return hci_enable_advertising_sync(hdev);
986 int hci_enable_advertising_sync(struct hci_dev *hdev)
988 struct adv_info *adv_instance;
989 struct hci_cp_le_set_adv_param cp;
990 u8 own_addr_type, enable = 0x01;
992 u16 adv_min_interval, adv_max_interval;
996 if (ext_adv_capable(hdev))
997 return hci_enable_ext_advertising_sync(hdev,
998 hdev->cur_adv_instance);
1000 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1001 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1003 /* If the "connectable" instance flag was not set, then choose between
1004 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1006 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1007 mgmt_get_connectable(hdev);
1009 if (!is_advertising_allowed(hdev, connectable))
1012 status = hci_disable_advertising_sync(hdev);
1016 /* Clear the HCI_LE_ADV bit temporarily so that the
1017 * hci_update_random_address knows that it's safe to go ahead
1018 * and write a new random address. The flag will be set back on
1019 * as soon as the SET_ADV_ENABLE HCI command completes.
1021 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1023 /* Set require_privacy to true only when non-connectable
1024 * advertising is used. In that case it is fine to use a
1025 * non-resolvable private address.
1027 status = hci_update_random_address_sync(hdev, !connectable,
1028 adv_use_rpa(hdev, flags),
1033 memset(&cp, 0, sizeof(cp));
1036 adv_min_interval = adv_instance->min_interval;
1037 adv_max_interval = adv_instance->max_interval;
1039 adv_min_interval = hdev->le_adv_min_interval;
1040 adv_max_interval = hdev->le_adv_max_interval;
1044 cp.type = LE_ADV_IND;
1046 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1047 cp.type = LE_ADV_SCAN_IND;
1049 cp.type = LE_ADV_NONCONN_IND;
1051 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1052 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1053 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1054 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1058 cp.min_interval = cpu_to_le16(adv_min_interval);
1059 cp.max_interval = cpu_to_le16(adv_max_interval);
1060 cp.own_address_type = own_addr_type;
1061 cp.channel_map = hdev->le_adv_channel_map;
1063 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1064 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1068 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1069 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1072 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1074 return hci_enable_advertising_sync(hdev);
1077 int hci_enable_advertising(struct hci_dev *hdev)
1079 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1080 list_empty(&hdev->adv_instances))
1083 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1086 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1091 if (!ext_adv_capable(hdev))
1094 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1098 /* If request specifies an instance that doesn't exist, fail */
1099 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1102 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1103 sizeof(instance), &instance, 0,
1104 HCI_CMD_TIMEOUT, sk);
1107 static void cancel_adv_timeout(struct hci_dev *hdev)
1109 if (hdev->adv_instance_timeout) {
1110 hdev->adv_instance_timeout = 0;
1111 cancel_delayed_work(&hdev->adv_instance_expire);
1115 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1118 struct hci_cp_le_set_ext_adv_data cp;
1119 u8 data[HCI_MAX_EXT_AD_LENGTH];
1123 memset(&pdu, 0, sizeof(pdu));
1125 len = eir_create_adv_data(hdev, instance, pdu.data);
1127 /* There's nothing to do if the data hasn't changed */
1128 if (hdev->adv_data_len == len &&
1129 memcmp(pdu.data, hdev->adv_data, len) == 0)
1132 memcpy(hdev->adv_data, pdu.data, len);
1133 hdev->adv_data_len = len;
1135 pdu.cp.length = len;
1136 pdu.cp.handle = instance;
1137 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1138 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1140 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1141 sizeof(pdu.cp) + len, &pdu.cp,
1145 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1147 struct hci_cp_le_set_adv_data cp;
1150 memset(&cp, 0, sizeof(cp));
1152 len = eir_create_adv_data(hdev, instance, cp.data);
1154 /* There's nothing to do if the data hasn't changed */
1155 if (hdev->adv_data_len == len &&
1156 memcmp(cp.data, hdev->adv_data, len) == 0)
1159 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1160 hdev->adv_data_len = len;
1164 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1165 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1168 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1170 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1173 if (ext_adv_capable(hdev))
1174 return hci_set_ext_adv_data_sync(hdev, instance);
1176 return hci_set_adv_data_sync(hdev, instance);
1179 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1182 struct adv_info *adv = NULL;
1185 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1188 if (hdev->adv_instance_timeout)
1191 adv = hci_find_adv_instance(hdev, instance);
1195 /* A zero timeout means unlimited advertising. As long as there is
1196 * only one instance, duration should be ignored. We still set a timeout
1197 * in case further instances are being added later on.
1199 * If the remaining lifetime of the instance is more than the duration
1200 * then the timeout corresponds to the duration, otherwise it will be
1201 * reduced to the remaining instance lifetime.
1203 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1204 timeout = adv->duration;
1206 timeout = adv->remaining_time;
1208 /* The remaining time is being reduced unless the instance is being
1209 * advertised without time limit.
1212 adv->remaining_time = adv->remaining_time - timeout;
1214 /* Only use work for scheduling instances with legacy advertising */
1215 if (!ext_adv_capable(hdev)) {
1216 hdev->adv_instance_timeout = timeout;
1217 queue_delayed_work(hdev->req_workqueue,
1218 &hdev->adv_instance_expire,
1219 msecs_to_jiffies(timeout * 1000));
1222 /* If we're just re-scheduling the same instance again then do not
1223 * execute any HCI commands. This happens when a single instance is
1226 if (!force && hdev->cur_adv_instance == instance &&
1227 hci_dev_test_flag(hdev, HCI_LE_ADV))
1230 hdev->cur_adv_instance = instance;
1232 return hci_start_adv_sync(hdev, instance);
1235 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1239 if (!ext_adv_capable(hdev))
1242 /* Disable instance 0x00 to disable all instances */
1243 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1247 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1248 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1251 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1253 struct adv_info *adv, *n;
1255 if (ext_adv_capable(hdev))
1256 /* Remove all existing sets */
1257 return hci_clear_adv_sets_sync(hdev, sk);
1259 /* This is safe as long as there is no command send while the lock is
1264 /* Cleanup non-ext instances */
1265 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1266 u8 instance = adv->instance;
1269 if (!(force || adv->timeout))
1272 err = hci_remove_adv_instance(hdev, instance);
1274 mgmt_advertising_removed(sk, hdev, instance);
1277 hci_dev_unlock(hdev);
1282 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1287 /* If we use extended advertising, instance has to be removed first. */
1288 if (ext_adv_capable(hdev))
1289 return hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1291 /* This is safe as long as there is no command send while the lock is
1296 err = hci_remove_adv_instance(hdev, instance);
1298 mgmt_advertising_removed(sk, hdev, instance);
1300 hci_dev_unlock(hdev);
1305 /* For a single instance:
1306 * - force == true: The instance will be removed even when its remaining
1307 * lifetime is not zero.
1308 * - force == false: the instance will be deactivated but kept stored unless
1309 * the remaining lifetime is zero.
1311 * For instance == 0x00:
1312 * - force == true: All instances will be removed regardless of their timeout
1314 * - force == false: Only instances that have a timeout will be removed.
1316 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1317 u8 instance, bool force)
1319 struct adv_info *next = NULL;
1322 /* Cancel any timeout concerning the removed instance(s). */
1323 if (!instance || hdev->cur_adv_instance == instance)
1324 cancel_adv_timeout(hdev);
1326 /* Get the next instance to advertise BEFORE we remove
1327 * the current one. This can be the same instance again
1328 * if there is only one instance.
1330 if (hdev->cur_adv_instance == instance)
1331 next = hci_get_next_instance(hdev, instance);
1334 err = hci_clear_adv_sync(hdev, sk, force);
1338 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1340 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1341 /* Don't advertise a removed instance. */
1342 if (next && next->instance == instance)
1345 err = hci_remove_adv_sync(hdev, instance, sk);
1351 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1354 if (next && !ext_adv_capable(hdev))
1355 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1360 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1362 struct hci_cp_read_rssi cp;
1365 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1366 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1369 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1371 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1372 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1375 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1377 struct hci_cp_read_tx_power cp;
1381 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1382 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1385 int hci_disable_advertising_sync(struct hci_dev *hdev)
1389 /* If controller is not advertising we are done. */
1390 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1393 if (ext_adv_capable(hdev))
1394 return hci_disable_ext_adv_instance_sync(hdev, 0x00);
1396 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1397 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1400 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1403 struct hci_cp_le_set_ext_scan_enable cp;
1405 memset(&cp, 0, sizeof(cp));
1407 cp.filter_dup = filter_dup;
1409 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1410 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1413 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
1416 struct hci_cp_le_set_scan_enable cp;
1418 if (use_ext_scan(hdev))
1419 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
1421 memset(&cp, 0, sizeof(cp));
1423 cp.filter_dup = filter_dup;
1425 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
1426 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1429 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
1431 if (!use_ll_privacy(hdev))
1434 /* If controller is not/already resolving we are done. */
1435 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
1438 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
1439 sizeof(val), &val, HCI_CMD_TIMEOUT);
1442 static int hci_scan_disable_sync(struct hci_dev *hdev)
1446 /* If controller is not scanning we are done. */
1447 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
1450 if (hdev->scanning_paused) {
1451 bt_dev_dbg(hdev, "Scanning is paused for suspend");
1455 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
1457 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
1464 static bool scan_use_rpa(struct hci_dev *hdev)
1466 return hci_dev_test_flag(hdev, HCI_PRIVACY);
1469 static void hci_start_interleave_scan(struct hci_dev *hdev)
1471 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
1472 queue_delayed_work(hdev->req_workqueue,
1473 &hdev->interleave_scan, 0);
1476 static bool is_interleave_scanning(struct hci_dev *hdev)
1478 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
1481 static void cancel_interleave_scan(struct hci_dev *hdev)
1483 bt_dev_dbg(hdev, "cancelling interleave scan");
1485 cancel_delayed_work_sync(&hdev->interleave_scan);
1487 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
1490 /* Return true if interleave_scan wasn't started until exiting this function,
1491 * otherwise, return false
1493 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
1495 /* Do interleaved scan only if all of the following are true:
1496 * - There is at least one ADV monitor
1497 * - At least one pending LE connection or one device to be scanned for
1498 * - Monitor offloading is not supported
1499 * If so, we should alternate between allowlist scan and one without
1500 * any filters to save power.
1502 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
1503 !(list_empty(&hdev->pend_le_conns) &&
1504 list_empty(&hdev->pend_le_reports)) &&
1505 hci_get_adv_monitor_offload_ext(hdev) ==
1506 HCI_ADV_MONITOR_EXT_NONE;
1507 bool is_interleaving = is_interleave_scanning(hdev);
1509 if (use_interleaving && !is_interleaving) {
1510 hci_start_interleave_scan(hdev);
1511 bt_dev_dbg(hdev, "starting interleave scan");
1515 if (!use_interleaving && is_interleaving)
1516 cancel_interleave_scan(hdev);
1521 /* Removes connection to resolve list if needed.*/
1522 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
1523 bdaddr_t *bdaddr, u8 bdaddr_type)
1525 struct hci_cp_le_del_from_resolv_list cp;
1526 struct bdaddr_list_with_irk *entry;
1528 if (!use_ll_privacy(hdev))
1531 /* Check if the IRK has been programmed */
1532 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
1537 cp.bdaddr_type = bdaddr_type;
1538 bacpy(&cp.bdaddr, bdaddr);
1540 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
1541 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1544 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
1545 bdaddr_t *bdaddr, u8 bdaddr_type)
1547 struct hci_cp_le_del_from_accept_list cp;
1550 /* Check if device is on accept list before removing it */
1551 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
1554 cp.bdaddr_type = bdaddr_type;
1555 bacpy(&cp.bdaddr, bdaddr);
1557 /* Ignore errors when removing from resolving list as that is likely
1558 * that the device was never added.
1560 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
1562 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
1563 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1565 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
1569 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
1575 /* Adds connection to resolve list if needed.
1576 * Setting params to NULL programs local hdev->irk
1578 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
1579 struct hci_conn_params *params)
1581 struct hci_cp_le_add_to_resolv_list cp;
1582 struct smp_irk *irk;
1583 struct bdaddr_list_with_irk *entry;
1585 if (!use_ll_privacy(hdev))
1588 /* Attempt to program local identity address, type and irk if params is
1592 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
1595 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
1596 memcpy(cp.peer_irk, hdev->irk, 16);
1600 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
1604 /* Check if the IK has _not_ been programmed yet. */
1605 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
1611 cp.bdaddr_type = params->addr_type;
1612 bacpy(&cp.bdaddr, ¶ms->addr);
1613 memcpy(cp.peer_irk, irk->val, 16);
1616 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
1617 memcpy(cp.local_irk, hdev->irk, 16);
1619 memset(cp.local_irk, 0, 16);
1621 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
1622 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1625 /* Set Device Privacy Mode. */
1626 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
1627 struct hci_conn_params *params)
1629 struct hci_cp_le_set_privacy_mode cp;
1630 struct smp_irk *irk;
1632 /* If device privacy mode has already been set there is nothing to do */
1633 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
1636 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
1637 * indicates that LL Privacy has been enabled and
1638 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
1640 if (!test_bit(HCI_CONN_FLAG_DEVICE_PRIVACY, params->flags))
1643 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
1647 memset(&cp, 0, sizeof(cp));
1648 cp.bdaddr_type = irk->addr_type;
1649 bacpy(&cp.bdaddr, &irk->bdaddr);
1650 cp.mode = HCI_DEVICE_PRIVACY;
1652 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
1653 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1656 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
1657 * this attempts to program the device in the resolving list as well and
1658 * properly set the privacy mode.
1660 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
1661 struct hci_conn_params *params,
1664 struct hci_cp_le_add_to_accept_list cp;
1667 /* Select filter policy to accept all advertising */
1668 if (*num_entries >= hdev->le_accept_list_size)
1671 /* Accept list can not be used with RPAs */
1672 if (!use_ll_privacy(hdev) &&
1673 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type)) {
1677 /* During suspend, only wakeable devices can be in acceptlist */
1678 if (hdev->suspended &&
1679 !test_bit(HCI_CONN_FLAG_REMOTE_WAKEUP, params->flags))
1682 /* Attempt to program the device in the resolving list first to avoid
1683 * having to rollback in case it fails since the resolving list is
1684 * dynamic it can probably be smaller than the accept list.
1686 err = hci_le_add_resolve_list_sync(hdev, params);
1688 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
1692 /* Set Privacy Mode */
1693 err = hci_le_set_privacy_mode_sync(hdev, params);
1695 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
1699 /* Check if already in accept list */
1700 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
1705 cp.bdaddr_type = params->addr_type;
1706 bacpy(&cp.bdaddr, ¶ms->addr);
1708 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
1709 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1711 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
1712 /* Rollback the device from the resolving list */
1713 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
1717 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
1723 /* This function disables/pause all advertising instances */
1724 static int hci_pause_advertising_sync(struct hci_dev *hdev)
1729 /* If already been paused there is nothing to do. */
1730 if (hdev->advertising_paused)
1733 bt_dev_dbg(hdev, "Pausing directed advertising");
1735 /* Stop directed advertising */
1736 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
1738 /* When discoverable timeout triggers, then just make sure
1739 * the limited discoverable flag is cleared. Even in the case
1740 * of a timeout triggered from general discoverable, it is
1741 * safe to unconditionally clear the flag.
1743 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1744 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1745 hdev->discov_timeout = 0;
1748 bt_dev_dbg(hdev, "Pausing advertising instances");
1750 /* Call to disable any advertisements active on the controller.
1751 * This will succeed even if no advertisements are configured.
1753 err = hci_disable_advertising_sync(hdev);
1757 /* If we are using software rotation, pause the loop */
1758 if (!ext_adv_capable(hdev))
1759 cancel_adv_timeout(hdev);
1761 hdev->advertising_paused = true;
1762 hdev->advertising_old_state = old_state;
1767 /* This function enables all user advertising instances */
1768 static int hci_resume_advertising_sync(struct hci_dev *hdev)
1770 struct adv_info *adv, *tmp;
1773 /* If advertising has not been paused there is nothing to do. */
1774 if (!hdev->advertising_paused)
1777 /* Resume directed advertising */
1778 hdev->advertising_paused = false;
1779 if (hdev->advertising_old_state) {
1780 hci_dev_set_flag(hdev, HCI_ADVERTISING);
1781 hdev->advertising_old_state = 0;
1784 bt_dev_dbg(hdev, "Resuming advertising instances");
1786 if (ext_adv_capable(hdev)) {
1787 /* Call for each tracked instance to be re-enabled */
1788 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
1789 err = hci_enable_ext_advertising_sync(hdev,
1794 /* If the instance cannot be resumed remove it */
1795 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
1799 /* Schedule for most recent instance to be restarted and begin
1800 * the software rotation loop
1802 err = hci_schedule_adv_instance_sync(hdev,
1803 hdev->cur_adv_instance,
1807 hdev->advertising_paused = false;
1812 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
1813 bool extended, struct sock *sk)
1815 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
1816 HCI_OP_READ_LOCAL_OOB_DATA;
1818 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1821 /* Device must not be scanning when updating the accept list.
1823 * Update is done using the following sequence:
1825 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
1826 * Remove Devices From Accept List ->
1827 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
1828 * Add Devices to Accept List ->
1829 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
1830 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
1833 * In case of failure advertising shall be restored to its original state and
1834 * return would disable accept list since either accept or resolving list could
1835 * not be programmed.
1838 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
1840 struct hci_conn_params *params;
1841 struct bdaddr_list *b, *t;
1843 bool pend_conn, pend_report;
1846 /* Pause advertising if resolving list can be used as controllers are
1847 * cannot accept resolving list modifications while advertising.
1849 if (use_ll_privacy(hdev)) {
1850 err = hci_pause_advertising_sync(hdev);
1852 bt_dev_err(hdev, "pause advertising failed: %d", err);
1857 /* Disable address resolution while reprogramming accept list since
1858 * devices that do have an IRK will be programmed in the resolving list
1859 * when LL Privacy is enabled.
1861 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
1863 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
1867 /* Go through the current accept list programmed into the
1868 * controller one by one and check if that address is still
1869 * in the list of pending connections or list of devices to
1870 * report. If not present in either list, then remove it from
1873 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
1874 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
1877 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
1881 /* If the device is not likely to connect or report,
1882 * remove it from the acceptlist.
1884 if (!pend_conn && !pend_report) {
1885 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
1893 /* Since all no longer valid accept list entries have been
1894 * removed, walk through the list of pending connections
1895 * and ensure that any new device gets programmed into
1898 * If the list of the devices is larger than the list of
1899 * available accept list entries in the controller, then
1900 * just abort and return filer policy value to not use the
1903 list_for_each_entry(params, &hdev->pend_le_conns, action) {
1904 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
1909 /* After adding all new pending connections, walk through
1910 * the list of pending reports and also add these to the
1911 * accept list if there is still space. Abort if space runs out.
1913 list_for_each_entry(params, &hdev->pend_le_reports, action) {
1914 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
1919 /* Use the allowlist unless the following conditions are all true:
1920 * - We are not currently suspending
1921 * - There are 1 or more ADV monitors registered and it's not offloaded
1922 * - Interleaved scanning is not currently using the allowlist
1924 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
1925 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
1926 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
1930 /* Enable address resolution when LL Privacy is enabled. */
1931 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
1933 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
1935 /* Resume advertising if it was paused */
1936 if (use_ll_privacy(hdev))
1937 hci_resume_advertising_sync(hdev);
1939 /* Select filter policy to use accept list */
1940 return err ? 0x00 : 0x01;
1943 /* Returns true if an le connection is in the scanning state */
1944 static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
1946 struct hci_conn_hash *h = &hdev->conn_hash;
1951 list_for_each_entry_rcu(c, &h->list, list) {
1952 if (c->type == LE_LINK && c->state == BT_CONNECT &&
1953 test_bit(HCI_CONN_SCANNING, &c->flags)) {
1964 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
1965 u16 interval, u16 window,
1966 u8 own_addr_type, u8 filter_policy)
1968 struct hci_cp_le_set_ext_scan_params *cp;
1969 struct hci_cp_le_scan_phy_params *phy;
1970 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
1974 phy = (void *)cp->data;
1976 memset(data, 0, sizeof(data));
1978 cp->own_addr_type = own_addr_type;
1979 cp->filter_policy = filter_policy;
1981 if (scan_1m(hdev) || scan_2m(hdev)) {
1982 cp->scanning_phys |= LE_SCAN_PHY_1M;
1985 phy->interval = cpu_to_le16(interval);
1986 phy->window = cpu_to_le16(window);
1992 if (scan_coded(hdev)) {
1993 cp->scanning_phys |= LE_SCAN_PHY_CODED;
1996 phy->interval = cpu_to_le16(interval);
1997 phy->window = cpu_to_le16(window);
2003 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2004 sizeof(*cp) + sizeof(*phy) * num_phy,
2005 data, HCI_CMD_TIMEOUT);
2008 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2009 u16 interval, u16 window,
2010 u8 own_addr_type, u8 filter_policy)
2012 struct hci_cp_le_set_scan_param cp;
2014 if (use_ext_scan(hdev))
2015 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2016 window, own_addr_type,
2019 memset(&cp, 0, sizeof(cp));
2021 cp.interval = cpu_to_le16(interval);
2022 cp.window = cpu_to_le16(window);
2023 cp.own_address_type = own_addr_type;
2024 cp.filter_policy = filter_policy;
2026 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2027 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2030 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2031 u16 window, u8 own_addr_type, u8 filter_policy,
2036 if (hdev->scanning_paused) {
2037 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2041 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2042 own_addr_type, filter_policy);
2046 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2049 static int hci_passive_scan_sync(struct hci_dev *hdev)
2053 u16 window, interval;
2056 if (hdev->scanning_paused) {
2057 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2061 err = hci_scan_disable_sync(hdev);
2063 bt_dev_err(hdev, "disable scanning failed: %d", err);
2067 /* Set require_privacy to false since no SCAN_REQ are send
2068 * during passive scanning. Not using an non-resolvable address
2069 * here is important so that peer devices using direct
2070 * advertising with our address will be correctly reported
2071 * by the controller.
2073 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2077 if (hdev->enable_advmon_interleave_scan &&
2078 hci_update_interleaved_scan_sync(hdev))
2081 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2083 /* Adding or removing entries from the accept list must
2084 * happen before enabling scanning. The controller does
2085 * not allow accept list modification while scanning.
2087 filter_policy = hci_update_accept_list_sync(hdev);
2089 /* When the controller is using random resolvable addresses and
2090 * with that having LE privacy enabled, then controllers with
2091 * Extended Scanner Filter Policies support can now enable support
2092 * for handling directed advertising.
2094 * So instead of using filter polices 0x00 (no acceptlist)
2095 * and 0x01 (acceptlist enabled) use the new filter policies
2096 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2098 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2099 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2100 filter_policy |= 0x02;
2102 if (hdev->suspended) {
2103 window = hdev->le_scan_window_suspend;
2104 interval = hdev->le_scan_int_suspend;
2105 } else if (hci_is_le_conn_scanning(hdev)) {
2106 window = hdev->le_scan_window_connect;
2107 interval = hdev->le_scan_int_connect;
2108 } else if (hci_is_adv_monitoring(hdev)) {
2109 window = hdev->le_scan_window_adv_monitor;
2110 interval = hdev->le_scan_int_adv_monitor;
2112 window = hdev->le_scan_window;
2113 interval = hdev->le_scan_interval;
2116 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2118 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2119 own_addr_type, filter_policy,
2120 LE_SCAN_FILTER_DUP_ENABLE);
2123 /* This function controls the passive scanning based on hdev->pend_le_conns
2124 * list. If there are pending LE connection we start the background scanning,
2125 * otherwise we stop it in the following sequence:
2127 * If there are devices to scan:
2129 * Disable Scanning -> Update Accept List ->
2130 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2131 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2138 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2142 if (!test_bit(HCI_UP, &hdev->flags) ||
2143 test_bit(HCI_INIT, &hdev->flags) ||
2144 hci_dev_test_flag(hdev, HCI_SETUP) ||
2145 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2146 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2147 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2150 /* No point in doing scanning if LE support hasn't been enabled */
2151 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2154 /* If discovery is active don't interfere with it */
2155 if (hdev->discovery.state != DISCOVERY_STOPPED)
2158 /* Reset RSSI and UUID filters when starting background scanning
2159 * since these filters are meant for service discovery only.
2161 * The Start Discovery and Start Service Discovery operations
2162 * ensure to set proper values for RSSI threshold and UUID
2163 * filter list. So it is safe to just reset them here.
2165 hci_discovery_filter_clear(hdev);
2167 bt_dev_dbg(hdev, "ADV monitoring is %s",
2168 hci_is_adv_monitoring(hdev) ? "on" : "off");
2170 if (list_empty(&hdev->pend_le_conns) &&
2171 list_empty(&hdev->pend_le_reports) &&
2172 !hci_is_adv_monitoring(hdev)) {
2173 /* If there is no pending LE connections or devices
2174 * to be scanned for or no ADV monitors, we should stop the
2175 * background scanning.
2178 bt_dev_dbg(hdev, "stopping background scanning");
2180 err = hci_scan_disable_sync(hdev);
2182 bt_dev_err(hdev, "stop background scanning failed: %d",
2185 /* If there is at least one pending LE connection, we should
2186 * keep the background scan running.
2189 /* If controller is connecting, we should not start scanning
2190 * since some controllers are not able to scan and connect at
2193 if (hci_lookup_le_connect(hdev))
2196 bt_dev_dbg(hdev, "start background scanning");
2198 err = hci_passive_scan_sync(hdev);
2200 bt_dev_err(hdev, "start background scanning failed: %d",
2207 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2209 return hci_update_passive_scan_sync(hdev);
2212 int hci_update_passive_scan(struct hci_dev *hdev)
2214 /* Only queue if it would have any effect */
2215 if (!test_bit(HCI_UP, &hdev->flags) ||
2216 test_bit(HCI_INIT, &hdev->flags) ||
2217 hci_dev_test_flag(hdev, HCI_SETUP) ||
2218 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2219 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2220 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2223 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2226 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2230 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2233 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2234 sizeof(val), &val, HCI_CMD_TIMEOUT);
2238 hdev->features[1][0] |= LMP_HOST_SC;
2239 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2241 hdev->features[1][0] &= ~LMP_HOST_SC;
2242 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2249 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2253 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2254 lmp_host_ssp_capable(hdev))
2257 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
2258 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2259 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2262 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2263 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2267 return hci_write_sc_support_sync(hdev, 0x01);
2270 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
2272 struct hci_cp_write_le_host_supported cp;
2274 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
2275 !lmp_bredr_capable(hdev))
2278 /* Check first if we already have the right host state
2279 * (host features set)
2281 if (le == lmp_host_le_capable(hdev) &&
2282 simul == lmp_host_le_br_capable(hdev))
2285 memset(&cp, 0, sizeof(cp));
2290 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
2291 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2294 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
2296 struct adv_info *adv, *tmp;
2299 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2302 /* If RPA Resolution has not been enable yet it means the
2303 * resolving list is empty and we should attempt to program the
2304 * local IRK in order to support using own_addr_type
2305 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
2307 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
2308 hci_le_add_resolve_list_sync(hdev, NULL);
2309 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2312 /* Make sure the controller has a good default for
2313 * advertising data. This also applies to the case
2314 * where BR/EDR was toggled during the AUTO_OFF phase.
2316 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2317 list_empty(&hdev->adv_instances)) {
2318 if (ext_adv_capable(hdev)) {
2319 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
2321 hci_update_scan_rsp_data_sync(hdev, 0x00);
2323 err = hci_update_adv_data_sync(hdev, 0x00);
2325 hci_update_scan_rsp_data_sync(hdev, 0x00);
2328 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2329 hci_enable_advertising_sync(hdev);
2332 /* Call for each tracked instance to be scheduled */
2333 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
2334 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
2339 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
2343 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
2344 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
2347 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
2348 sizeof(link_sec), &link_sec,
2352 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
2354 struct hci_cp_write_page_scan_activity cp;
2358 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2361 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
2364 memset(&cp, 0, sizeof(cp));
2367 type = PAGE_SCAN_TYPE_INTERLACED;
2369 /* 160 msec page scan interval */
2370 cp.interval = cpu_to_le16(0x0100);
2372 type = hdev->def_page_scan_type;
2373 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
2376 cp.window = cpu_to_le16(hdev->def_page_scan_window);
2378 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
2379 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
2380 err = __hci_cmd_sync_status(hdev,
2381 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
2382 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2387 if (hdev->page_scan_type != type)
2388 err = __hci_cmd_sync_status(hdev,
2389 HCI_OP_WRITE_PAGE_SCAN_TYPE,
2390 sizeof(type), &type,
2396 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
2398 struct bdaddr_list *b;
2400 list_for_each_entry(b, &hdev->accept_list, list) {
2401 struct hci_conn *conn;
2403 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
2407 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2414 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
2416 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
2421 int hci_update_scan_sync(struct hci_dev *hdev)
2425 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2428 if (!hdev_is_powered(hdev))
2431 if (mgmt_powering_down(hdev))
2434 if (hdev->scanning_paused)
2437 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
2438 disconnected_accept_list_entries(hdev))
2441 scan = SCAN_DISABLED;
2443 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
2444 scan |= SCAN_INQUIRY;
2446 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
2447 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
2450 return hci_write_scan_enable_sync(hdev, scan);
2453 int hci_update_name_sync(struct hci_dev *hdev)
2455 struct hci_cp_write_local_name cp;
2457 memset(&cp, 0, sizeof(cp));
2459 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
2461 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
2466 /* This function perform powered update HCI command sequence after the HCI init
2467 * sequence which end up resetting all states, the sequence is as follows:
2469 * HCI_SSP_ENABLED(Enable SSP)
2470 * HCI_LE_ENABLED(Enable LE)
2471 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
2473 * Enable Authentication
2474 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
2475 * Set Name -> Set EIR)
2477 int hci_powered_update_sync(struct hci_dev *hdev)
2481 /* Register the available SMP channels (BR/EDR and LE) only when
2482 * successfully powering on the controller. This late
2483 * registration is required so that LE SMP can clearly decide if
2484 * the public address or static address is used.
2488 err = hci_write_ssp_mode_sync(hdev, 0x01);
2492 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
2496 err = hci_powered_update_adv_sync(hdev);
2500 err = hci_write_auth_enable_sync(hdev);
2504 if (lmp_bredr_capable(hdev)) {
2505 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
2506 hci_write_fast_connectable_sync(hdev, true);
2508 hci_write_fast_connectable_sync(hdev, false);
2509 hci_update_scan_sync(hdev);
2510 hci_update_class_sync(hdev);
2511 hci_update_name_sync(hdev);
2512 hci_update_eir_sync(hdev);
2519 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
2520 * (BD_ADDR) for a HCI device from
2521 * a firmware node property.
2522 * @hdev: The HCI device
2524 * Search the firmware node for 'local-bd-address'.
2526 * All-zero BD addresses are rejected, because those could be properties
2527 * that exist in the firmware tables, but were not updated by the firmware. For
2528 * example, the DTS could define 'local-bd-address', with zero BD addresses.
2530 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
2532 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
2536 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
2537 (u8 *)&ba, sizeof(ba));
2538 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
2541 bacpy(&hdev->public_addr, &ba);
2544 struct hci_init_stage {
2545 int (*func)(struct hci_dev *hdev);
2548 /* Run init stage NULL terminated function table */
2549 static int hci_init_stage_sync(struct hci_dev *hdev,
2550 const struct hci_init_stage *stage)
2554 for (i = 0; stage[i].func; i++) {
2557 err = stage[i].func(hdev);
2565 /* Read Local Version */
2566 static int hci_read_local_version_sync(struct hci_dev *hdev)
2568 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
2569 0, NULL, HCI_CMD_TIMEOUT);
2572 /* Read BD Address */
2573 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
2575 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
2576 0, NULL, HCI_CMD_TIMEOUT);
2579 #define HCI_INIT(_func) \
2584 static const struct hci_init_stage hci_init0[] = {
2585 /* HCI_OP_READ_LOCAL_VERSION */
2586 HCI_INIT(hci_read_local_version_sync),
2587 /* HCI_OP_READ_BD_ADDR */
2588 HCI_INIT(hci_read_bd_addr_sync),
2592 int hci_reset_sync(struct hci_dev *hdev)
2596 set_bit(HCI_RESET, &hdev->flags);
2598 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
2606 static int hci_init0_sync(struct hci_dev *hdev)
2610 bt_dev_dbg(hdev, "");
2613 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
2614 err = hci_reset_sync(hdev);
2619 return hci_init_stage_sync(hdev, hci_init0);
2622 static int hci_unconf_init_sync(struct hci_dev *hdev)
2626 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
2629 err = hci_init0_sync(hdev);
2633 if (hci_dev_test_flag(hdev, HCI_SETUP))
2634 hci_debugfs_create_basic(hdev);
2639 /* Read Local Supported Features. */
2640 static int hci_read_local_features_sync(struct hci_dev *hdev)
2642 /* Not all AMP controllers support this command */
2643 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
2646 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
2647 0, NULL, HCI_CMD_TIMEOUT);
2650 /* BR Controller init stage 1 command sequence */
2651 static const struct hci_init_stage br_init1[] = {
2652 /* HCI_OP_READ_LOCAL_FEATURES */
2653 HCI_INIT(hci_read_local_features_sync),
2654 /* HCI_OP_READ_LOCAL_VERSION */
2655 HCI_INIT(hci_read_local_version_sync),
2656 /* HCI_OP_READ_BD_ADDR */
2657 HCI_INIT(hci_read_bd_addr_sync),
2661 /* Read Local Commands */
2662 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
2664 /* All Bluetooth 1.2 and later controllers should support the
2665 * HCI command for reading the local supported commands.
2667 * Unfortunately some controllers indicate Bluetooth 1.2 support,
2668 * but do not have support for this command. If that is the case,
2669 * the driver can quirk the behavior and skip reading the local
2670 * supported commands.
2672 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
2673 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
2674 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
2675 0, NULL, HCI_CMD_TIMEOUT);
2680 /* Read Local AMP Info */
2681 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
2683 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
2684 0, NULL, HCI_CMD_TIMEOUT);
2687 /* Read Data Blk size */
2688 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
2690 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
2691 0, NULL, HCI_CMD_TIMEOUT);
2694 /* Read Flow Control Mode */
2695 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
2697 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
2698 0, NULL, HCI_CMD_TIMEOUT);
2701 /* Read Location Data */
2702 static int hci_read_location_data_sync(struct hci_dev *hdev)
2704 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
2705 0, NULL, HCI_CMD_TIMEOUT);
2708 /* AMP Controller init stage 1 command sequence */
2709 static const struct hci_init_stage amp_init1[] = {
2710 /* HCI_OP_READ_LOCAL_VERSION */
2711 HCI_INIT(hci_read_local_version_sync),
2712 /* HCI_OP_READ_LOCAL_COMMANDS */
2713 HCI_INIT(hci_read_local_cmds_sync),
2714 /* HCI_OP_READ_LOCAL_AMP_INFO */
2715 HCI_INIT(hci_read_local_amp_info_sync),
2716 /* HCI_OP_READ_DATA_BLOCK_SIZE */
2717 HCI_INIT(hci_read_data_block_size_sync),
2718 /* HCI_OP_READ_FLOW_CONTROL_MODE */
2719 HCI_INIT(hci_read_flow_control_mode_sync),
2720 /* HCI_OP_READ_LOCATION_DATA */
2721 HCI_INIT(hci_read_location_data_sync),
2724 static int hci_init1_sync(struct hci_dev *hdev)
2728 bt_dev_dbg(hdev, "");
2731 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
2732 err = hci_reset_sync(hdev);
2737 switch (hdev->dev_type) {
2739 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
2740 return hci_init_stage_sync(hdev, br_init1);
2742 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
2743 return hci_init_stage_sync(hdev, amp_init1);
2745 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
2752 /* AMP Controller init stage 2 command sequence */
2753 static const struct hci_init_stage amp_init2[] = {
2754 /* HCI_OP_READ_LOCAL_FEATURES */
2755 HCI_INIT(hci_read_local_features_sync),
2758 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
2759 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
2761 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
2762 0, NULL, HCI_CMD_TIMEOUT);
2765 /* Read Class of Device */
2766 static int hci_read_dev_class_sync(struct hci_dev *hdev)
2768 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
2769 0, NULL, HCI_CMD_TIMEOUT);
2772 /* Read Local Name */
2773 static int hci_read_local_name_sync(struct hci_dev *hdev)
2775 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
2776 0, NULL, HCI_CMD_TIMEOUT);
2779 /* Read Voice Setting */
2780 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
2782 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
2783 0, NULL, HCI_CMD_TIMEOUT);
2786 /* Read Number of Supported IAC */
2787 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
2789 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
2790 0, NULL, HCI_CMD_TIMEOUT);
2793 /* Read Current IAC LAP */
2794 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
2796 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
2797 0, NULL, HCI_CMD_TIMEOUT);
2800 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
2801 u8 cond_type, bdaddr_t *bdaddr,
2804 struct hci_cp_set_event_filter cp;
2806 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2809 memset(&cp, 0, sizeof(cp));
2810 cp.flt_type = flt_type;
2812 if (flt_type != HCI_FLT_CLEAR_ALL) {
2813 cp.cond_type = cond_type;
2814 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
2815 cp.addr_conn_flt.auto_accept = auto_accept;
2818 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
2819 flt_type == HCI_FLT_CLEAR_ALL ?
2820 sizeof(cp.flt_type) : sizeof(cp), &cp,
2824 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
2826 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
2829 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
2833 /* Connection accept timeout ~20 secs */
2834 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
2836 __le16 param = cpu_to_le16(0x7d00);
2838 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
2839 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
2842 /* BR Controller init stage 2 command sequence */
2843 static const struct hci_init_stage br_init2[] = {
2844 /* HCI_OP_READ_BUFFER_SIZE */
2845 HCI_INIT(hci_read_buffer_size_sync),
2846 /* HCI_OP_READ_CLASS_OF_DEV */
2847 HCI_INIT(hci_read_dev_class_sync),
2848 /* HCI_OP_READ_LOCAL_NAME */
2849 HCI_INIT(hci_read_local_name_sync),
2850 /* HCI_OP_READ_VOICE_SETTING */
2851 HCI_INIT(hci_read_voice_setting_sync),
2852 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
2853 HCI_INIT(hci_read_num_supported_iac_sync),
2854 /* HCI_OP_READ_CURRENT_IAC_LAP */
2855 HCI_INIT(hci_read_current_iac_lap_sync),
2856 /* HCI_OP_SET_EVENT_FLT */
2857 HCI_INIT(hci_clear_event_filter_sync),
2858 /* HCI_OP_WRITE_CA_TIMEOUT */
2859 HCI_INIT(hci_write_ca_timeout_sync),
2863 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
2867 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2870 /* When SSP is available, then the host features page
2871 * should also be available as well. However some
2872 * controllers list the max_page as 0 as long as SSP
2873 * has not been enabled. To achieve proper debugging
2874 * output, force the minimum max_page to 1 at least.
2876 hdev->max_page = 0x01;
2878 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2879 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2882 static int hci_write_eir_sync(struct hci_dev *hdev)
2884 struct hci_cp_write_eir cp;
2886 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2889 memset(hdev->eir, 0, sizeof(hdev->eir));
2890 memset(&cp, 0, sizeof(cp));
2892 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
2896 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
2900 if (!lmp_inq_rssi_capable(hdev) &&
2901 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
2904 /* If Extended Inquiry Result events are supported, then
2905 * they are clearly preferred over Inquiry Result with RSSI
2908 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
2910 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
2911 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2914 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
2916 if (!lmp_inq_tx_pwr_capable(hdev))
2919 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
2920 0, NULL, HCI_CMD_TIMEOUT);
2923 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
2925 struct hci_cp_read_local_ext_features cp;
2927 if (!lmp_ext_feat_capable(hdev))
2930 memset(&cp, 0, sizeof(cp));
2933 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
2934 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2937 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
2939 return hci_read_local_ext_features_sync(hdev, 0x01);
2942 /* HCI Controller init stage 2 command sequence */
2943 static const struct hci_init_stage hci_init2[] = {
2944 /* HCI_OP_READ_LOCAL_COMMANDS */
2945 HCI_INIT(hci_read_local_cmds_sync),
2946 /* HCI_OP_WRITE_SSP_MODE */
2947 HCI_INIT(hci_write_ssp_mode_1_sync),
2948 /* HCI_OP_WRITE_EIR */
2949 HCI_INIT(hci_write_eir_sync),
2950 /* HCI_OP_WRITE_INQUIRY_MODE */
2951 HCI_INIT(hci_write_inquiry_mode_sync),
2952 /* HCI_OP_READ_INQ_RSP_TX_POWER */
2953 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
2954 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
2955 HCI_INIT(hci_read_local_ext_features_1_sync),
2956 /* HCI_OP_WRITE_AUTH_ENABLE */
2957 HCI_INIT(hci_write_auth_enable_sync),
2961 /* Read LE Buffer Size */
2962 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
2964 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
2965 0, NULL, HCI_CMD_TIMEOUT);
2968 /* Read LE Local Supported Features */
2969 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
2971 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
2972 0, NULL, HCI_CMD_TIMEOUT);
2975 /* Read LE Supported States */
2976 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
2978 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
2979 0, NULL, HCI_CMD_TIMEOUT);
2982 /* LE Controller init stage 2 command sequence */
2983 static const struct hci_init_stage le_init2[] = {
2984 /* HCI_OP_LE_READ_BUFFER_SIZE */
2985 HCI_INIT(hci_le_read_buffer_size_sync),
2986 /* HCI_OP_LE_READ_LOCAL_FEATURES */
2987 HCI_INIT(hci_le_read_local_features_sync),
2988 /* HCI_OP_LE_READ_SUPPORTED_STATES */
2989 HCI_INIT(hci_le_read_supported_states_sync),
2993 static int hci_init2_sync(struct hci_dev *hdev)
2997 bt_dev_dbg(hdev, "");
2999 if (hdev->dev_type == HCI_AMP)
3000 return hci_init_stage_sync(hdev, amp_init2);
3002 if (lmp_bredr_capable(hdev)) {
3003 err = hci_init_stage_sync(hdev, br_init2);
3007 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3010 if (lmp_le_capable(hdev)) {
3011 err = hci_init_stage_sync(hdev, le_init2);
3014 /* LE-only controllers have LE implicitly enabled */
3015 if (!lmp_bredr_capable(hdev))
3016 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3019 return hci_init_stage_sync(hdev, hci_init2);
3022 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3024 /* The second byte is 0xff instead of 0x9f (two reserved bits
3025 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3026 * command otherwise.
3028 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3030 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3031 * any event mask for pre 1.2 devices.
3033 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3036 if (lmp_bredr_capable(hdev)) {
3037 events[4] |= 0x01; /* Flow Specification Complete */
3039 /* Don't set Disconnect Complete when suspended as that
3040 * would wakeup the host when disconnecting due to
3043 if (hdev->suspended)
3046 /* Use a different default for LE-only devices */
3047 memset(events, 0, sizeof(events));
3048 events[1] |= 0x20; /* Command Complete */
3049 events[1] |= 0x40; /* Command Status */
3050 events[1] |= 0x80; /* Hardware Error */
3052 /* If the controller supports the Disconnect command, enable
3053 * the corresponding event. In addition enable packet flow
3054 * control related events.
3056 if (hdev->commands[0] & 0x20) {
3057 /* Don't set Disconnect Complete when suspended as that
3058 * would wakeup the host when disconnecting due to
3061 if (!hdev->suspended)
3062 events[0] |= 0x10; /* Disconnection Complete */
3063 events[2] |= 0x04; /* Number of Completed Packets */
3064 events[3] |= 0x02; /* Data Buffer Overflow */
3067 /* If the controller supports the Read Remote Version
3068 * Information command, enable the corresponding event.
3070 if (hdev->commands[2] & 0x80)
3071 events[1] |= 0x08; /* Read Remote Version Information
3075 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3076 events[0] |= 0x80; /* Encryption Change */
3077 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3081 if (lmp_inq_rssi_capable(hdev) ||
3082 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3083 events[4] |= 0x02; /* Inquiry Result with RSSI */
3085 if (lmp_ext_feat_capable(hdev))
3086 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3088 if (lmp_esco_capable(hdev)) {
3089 events[5] |= 0x08; /* Synchronous Connection Complete */
3090 events[5] |= 0x10; /* Synchronous Connection Changed */
3093 if (lmp_sniffsubr_capable(hdev))
3094 events[5] |= 0x20; /* Sniff Subrating */
3096 if (lmp_pause_enc_capable(hdev))
3097 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3099 if (lmp_ext_inq_capable(hdev))
3100 events[5] |= 0x40; /* Extended Inquiry Result */
3102 if (lmp_no_flush_capable(hdev))
3103 events[7] |= 0x01; /* Enhanced Flush Complete */
3105 if (lmp_lsto_capable(hdev))
3106 events[6] |= 0x80; /* Link Supervision Timeout Changed */
3108 if (lmp_ssp_capable(hdev)) {
3109 events[6] |= 0x01; /* IO Capability Request */
3110 events[6] |= 0x02; /* IO Capability Response */
3111 events[6] |= 0x04; /* User Confirmation Request */
3112 events[6] |= 0x08; /* User Passkey Request */
3113 events[6] |= 0x10; /* Remote OOB Data Request */
3114 events[6] |= 0x20; /* Simple Pairing Complete */
3115 events[7] |= 0x04; /* User Passkey Notification */
3116 events[7] |= 0x08; /* Keypress Notification */
3117 events[7] |= 0x10; /* Remote Host Supported
3118 * Features Notification
3122 if (lmp_le_capable(hdev))
3123 events[7] |= 0x20; /* LE Meta-Event */
3125 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3126 sizeof(events), events, HCI_CMD_TIMEOUT);
3129 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3131 struct hci_cp_read_stored_link_key cp;
3133 if (!(hdev->commands[6] & 0x20) ||
3134 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3137 memset(&cp, 0, sizeof(cp));
3138 bacpy(&cp.bdaddr, BDADDR_ANY);
3141 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3142 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3145 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3147 struct hci_cp_write_def_link_policy cp;
3148 u16 link_policy = 0;
3150 if (!(hdev->commands[5] & 0x10))
3153 memset(&cp, 0, sizeof(cp));
3155 if (lmp_rswitch_capable(hdev))
3156 link_policy |= HCI_LP_RSWITCH;
3157 if (lmp_hold_capable(hdev))
3158 link_policy |= HCI_LP_HOLD;
3159 if (lmp_sniff_capable(hdev))
3160 link_policy |= HCI_LP_SNIFF;
3161 if (lmp_park_capable(hdev))
3162 link_policy |= HCI_LP_PARK;
3164 cp.policy = cpu_to_le16(link_policy);
3166 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3167 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3170 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3172 if (!(hdev->commands[8] & 0x01))
3175 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3176 0, NULL, HCI_CMD_TIMEOUT);
3179 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3181 if (!(hdev->commands[18] & 0x04) ||
3182 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3185 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3186 0, NULL, HCI_CMD_TIMEOUT);
3189 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3191 /* Some older Broadcom based Bluetooth 1.2 controllers do not
3192 * support the Read Page Scan Type command. Check support for
3193 * this command in the bit mask of supported commands.
3195 if (!(hdev->commands[13] & 0x01))
3198 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3199 0, NULL, HCI_CMD_TIMEOUT);
3202 /* Read features beyond page 1 if available */
3203 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3208 if (!lmp_ext_feat_capable(hdev))
3211 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3213 err = hci_read_local_ext_features_sync(hdev, page);
3221 /* HCI Controller init stage 3 command sequence */
3222 static const struct hci_init_stage hci_init3[] = {
3223 /* HCI_OP_SET_EVENT_MASK */
3224 HCI_INIT(hci_set_event_mask_sync),
3225 /* HCI_OP_READ_STORED_LINK_KEY */
3226 HCI_INIT(hci_read_stored_link_key_sync),
3227 /* HCI_OP_WRITE_DEF_LINK_POLICY */
3228 HCI_INIT(hci_setup_link_policy_sync),
3229 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
3230 HCI_INIT(hci_read_page_scan_activity_sync),
3231 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
3232 HCI_INIT(hci_read_def_err_data_reporting_sync),
3233 /* HCI_OP_READ_PAGE_SCAN_TYPE */
3234 HCI_INIT(hci_read_page_scan_type_sync),
3235 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3236 HCI_INIT(hci_read_local_ext_features_all_sync),
3240 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
3244 if (!lmp_le_capable(hdev))
3247 memset(events, 0, sizeof(events));
3249 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
3250 events[0] |= 0x10; /* LE Long Term Key Request */
3252 /* If controller supports the Connection Parameters Request
3253 * Link Layer Procedure, enable the corresponding event.
3255 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
3256 /* LE Remote Connection Parameter Request */
3259 /* If the controller supports the Data Length Extension
3260 * feature, enable the corresponding event.
3262 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
3263 events[0] |= 0x40; /* LE Data Length Change */
3265 /* If the controller supports LL Privacy feature, enable
3266 * the corresponding event.
3268 if (hdev->le_features[0] & HCI_LE_LL_PRIVACY)
3269 events[1] |= 0x02; /* LE Enhanced Connection Complete */
3271 /* If the controller supports Extended Scanner Filter
3272 * Policies, enable the corresponding event.
3274 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
3275 events[1] |= 0x04; /* LE Direct Advertising Report */
3277 /* If the controller supports Channel Selection Algorithm #2
3278 * feature, enable the corresponding event.
3280 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
3281 events[2] |= 0x08; /* LE Channel Selection Algorithm */
3283 /* If the controller supports the LE Set Scan Enable command,
3284 * enable the corresponding advertising report event.
3286 if (hdev->commands[26] & 0x08)
3287 events[0] |= 0x02; /* LE Advertising Report */
3289 /* If the controller supports the LE Create Connection
3290 * command, enable the corresponding event.
3292 if (hdev->commands[26] & 0x10)
3293 events[0] |= 0x01; /* LE Connection Complete */
3295 /* If the controller supports the LE Connection Update
3296 * command, enable the corresponding event.
3298 if (hdev->commands[27] & 0x04)
3299 events[0] |= 0x04; /* LE Connection Update Complete */
3301 /* If the controller supports the LE Read Remote Used Features
3302 * command, enable the corresponding event.
3304 if (hdev->commands[27] & 0x20)
3305 /* LE Read Remote Used Features Complete */
3308 /* If the controller supports the LE Read Local P-256
3309 * Public Key command, enable the corresponding event.
3311 if (hdev->commands[34] & 0x02)
3312 /* LE Read Local P-256 Public Key Complete */
3315 /* If the controller supports the LE Generate DHKey
3316 * command, enable the corresponding event.
3318 if (hdev->commands[34] & 0x04)
3319 events[1] |= 0x01; /* LE Generate DHKey Complete */
3321 /* If the controller supports the LE Set Default PHY or
3322 * LE Set PHY commands, enable the corresponding event.
3324 if (hdev->commands[35] & (0x20 | 0x40))
3325 events[1] |= 0x08; /* LE PHY Update Complete */
3327 /* If the controller supports LE Set Extended Scan Parameters
3328 * and LE Set Extended Scan Enable commands, enable the
3329 * corresponding event.
3331 if (use_ext_scan(hdev))
3332 events[1] |= 0x10; /* LE Extended Advertising Report */
3334 /* If the controller supports the LE Extended Advertising
3335 * command, enable the corresponding event.
3337 if (ext_adv_capable(hdev))
3338 events[2] |= 0x02; /* LE Advertising Set Terminated */
3340 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
3341 sizeof(events), events, HCI_CMD_TIMEOUT);
3344 /* Read LE Advertising Channel TX Power */
3345 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
3347 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
3348 /* HCI TS spec forbids mixing of legacy and extended
3349 * advertising commands wherein READ_ADV_TX_POWER is
3350 * also included. So do not call it if extended adv
3351 * is supported otherwise controller will return
3352 * COMMAND_DISALLOWED for extended commands.
3354 return __hci_cmd_sync_status(hdev,
3355 HCI_OP_LE_READ_ADV_TX_POWER,
3356 0, NULL, HCI_CMD_TIMEOUT);
3362 /* Read LE Min/Max Tx Power*/
3363 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
3365 if (!(hdev->commands[38] & 0x80) ||
3366 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
3369 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
3370 0, NULL, HCI_CMD_TIMEOUT);
3373 /* Read LE Accept List Size */
3374 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
3376 if (!(hdev->commands[26] & 0x40))
3379 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
3380 0, NULL, HCI_CMD_TIMEOUT);
3383 /* Clear LE Accept List */
3384 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
3386 if (!(hdev->commands[26] & 0x80))
3389 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
3393 /* Read LE Resolving List Size */
3394 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
3396 if (!(hdev->commands[34] & 0x40))
3399 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
3400 0, NULL, HCI_CMD_TIMEOUT);
3403 /* Clear LE Resolving List */
3404 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
3406 if (!(hdev->commands[34] & 0x20))
3409 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
3413 /* Set RPA timeout */
3414 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
3416 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
3418 if (!(hdev->commands[35] & 0x04))
3421 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
3422 sizeof(timeout), &timeout,
3426 /* Read LE Maximum Data Length */
3427 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
3429 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3432 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
3436 /* Read LE Suggested Default Data Length */
3437 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
3439 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3442 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
3446 /* Read LE Number of Supported Advertising Sets */
3447 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
3449 if (!ext_adv_capable(hdev))
3452 return __hci_cmd_sync_status(hdev,
3453 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
3454 0, NULL, HCI_CMD_TIMEOUT);
3457 /* Write LE Host Supported */
3458 static int hci_set_le_support_sync(struct hci_dev *hdev)
3460 struct hci_cp_write_le_host_supported cp;
3462 /* LE-only devices do not support explicit enablement */
3463 if (!lmp_bredr_capable(hdev))
3466 memset(&cp, 0, sizeof(cp));
3468 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
3473 if (cp.le == lmp_host_le_capable(hdev))
3476 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3477 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3480 /* LE Controller init stage 3 command sequence */
3481 static const struct hci_init_stage le_init3[] = {
3482 /* HCI_OP_LE_SET_EVENT_MASK */
3483 HCI_INIT(hci_le_set_event_mask_sync),
3484 /* HCI_OP_LE_READ_ADV_TX_POWER */
3485 HCI_INIT(hci_le_read_adv_tx_power_sync),
3486 /* HCI_OP_LE_READ_TRANSMIT_POWER */
3487 HCI_INIT(hci_le_read_tx_power_sync),
3488 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
3489 HCI_INIT(hci_le_read_accept_list_size_sync),
3490 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
3491 HCI_INIT(hci_le_clear_accept_list_sync),
3492 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
3493 HCI_INIT(hci_le_read_resolv_list_size_sync),
3494 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
3495 HCI_INIT(hci_le_clear_resolv_list_sync),
3496 /* HCI_OP_LE_SET_RPA_TIMEOUT */
3497 HCI_INIT(hci_le_set_rpa_timeout_sync),
3498 /* HCI_OP_LE_READ_MAX_DATA_LEN */
3499 HCI_INIT(hci_le_read_max_data_len_sync),
3500 /* HCI_OP_LE_READ_DEF_DATA_LEN */
3501 HCI_INIT(hci_le_read_def_data_len_sync),
3502 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
3503 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
3504 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
3505 HCI_INIT(hci_set_le_support_sync),
3509 static int hci_init3_sync(struct hci_dev *hdev)
3513 bt_dev_dbg(hdev, "");
3515 err = hci_init_stage_sync(hdev, hci_init3);
3519 if (lmp_le_capable(hdev))
3520 return hci_init_stage_sync(hdev, le_init3);
3525 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
3527 struct hci_cp_delete_stored_link_key cp;
3529 /* Some Broadcom based Bluetooth controllers do not support the
3530 * Delete Stored Link Key command. They are clearly indicating its
3531 * absence in the bit mask of supported commands.
3533 * Check the supported commands and only if the command is marked
3534 * as supported send it. If not supported assume that the controller
3535 * does not have actual support for stored link keys which makes this
3536 * command redundant anyway.
3538 * Some controllers indicate that they support handling deleting
3539 * stored link keys, but they don't. The quirk lets a driver
3540 * just disable this command.
3542 if (!(hdev->commands[6] & 0x80) ||
3543 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3546 memset(&cp, 0, sizeof(cp));
3547 bacpy(&cp.bdaddr, BDADDR_ANY);
3548 cp.delete_all = 0x01;
3550 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
3551 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3554 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
3556 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3557 bool changed = false;
3559 /* Set event mask page 2 if the HCI command for it is supported */
3560 if (!(hdev->commands[22] & 0x04))
3563 /* If Connectionless Peripheral Broadcast central role is supported
3564 * enable all necessary events for it.
3566 if (lmp_cpb_central_capable(hdev)) {
3567 events[1] |= 0x40; /* Triggered Clock Capture */
3568 events[1] |= 0x80; /* Synchronization Train Complete */
3569 events[2] |= 0x10; /* Peripheral Page Response Timeout */
3570 events[2] |= 0x20; /* CPB Channel Map Change */
3574 /* If Connectionless Peripheral Broadcast peripheral role is supported
3575 * enable all necessary events for it.
3577 if (lmp_cpb_peripheral_capable(hdev)) {
3578 events[2] |= 0x01; /* Synchronization Train Received */
3579 events[2] |= 0x02; /* CPB Receive */
3580 events[2] |= 0x04; /* CPB Timeout */
3581 events[2] |= 0x08; /* Truncated Page Complete */
3585 /* Enable Authenticated Payload Timeout Expired event if supported */
3586 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
3591 /* Some Broadcom based controllers indicate support for Set Event
3592 * Mask Page 2 command, but then actually do not support it. Since
3593 * the default value is all bits set to zero, the command is only
3594 * required if the event mask has to be changed. In case no change
3595 * to the event mask is needed, skip this command.
3600 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
3601 sizeof(events), events, HCI_CMD_TIMEOUT);
3604 /* Read local codec list if the HCI command is supported */
3605 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
3607 if (!(hdev->commands[29] & 0x20))
3610 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_CODECS, 0, NULL,
3614 /* Read local pairing options if the HCI command is supported */
3615 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
3617 if (!(hdev->commands[41] & 0x08))
3620 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
3621 0, NULL, HCI_CMD_TIMEOUT);
3624 /* Get MWS transport configuration if the HCI command is supported */
3625 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
3627 if (!(hdev->commands[30] & 0x08))
3630 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
3631 0, NULL, HCI_CMD_TIMEOUT);
3634 /* Check for Synchronization Train support */
3635 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
3637 if (!lmp_sync_train_capable(hdev))
3640 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
3641 0, NULL, HCI_CMD_TIMEOUT);
3644 /* Enable Secure Connections if supported and configured */
3645 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
3649 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3650 !bredr_sc_enabled(hdev))
3653 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3654 sizeof(support), &support,
3658 /* Set erroneous data reporting if supported to the wideband speech
3661 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
3663 struct hci_cp_write_def_err_data_reporting cp;
3664 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
3666 if (!(hdev->commands[18] & 0x08) ||
3667 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3670 if (enabled == hdev->err_data_reporting)
3673 memset(&cp, 0, sizeof(cp));
3674 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
3675 ERR_DATA_REPORTING_DISABLED;
3677 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
3678 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3681 static const struct hci_init_stage hci_init4[] = {
3682 /* HCI_OP_DELETE_STORED_LINK_KEY */
3683 HCI_INIT(hci_delete_stored_link_key_sync),
3684 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
3685 HCI_INIT(hci_set_event_mask_page_2_sync),
3686 /* HCI_OP_READ_LOCAL_CODECS */
3687 HCI_INIT(hci_read_local_codecs_sync),
3688 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
3689 HCI_INIT(hci_read_local_pairing_opts_sync),
3690 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
3691 HCI_INIT(hci_get_mws_transport_config_sync),
3692 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
3693 HCI_INIT(hci_read_sync_train_params_sync),
3694 /* HCI_OP_WRITE_SC_SUPPORT */
3695 HCI_INIT(hci_write_sc_support_1_sync),
3696 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
3697 HCI_INIT(hci_set_err_data_report_sync),
3701 /* Set Suggested Default Data Length to maximum if supported */
3702 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
3704 struct hci_cp_le_write_def_data_len cp;
3706 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3709 memset(&cp, 0, sizeof(cp));
3710 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
3711 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
3713 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
3714 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3717 /* Set Default PHY parameters if command is supported */
3718 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
3720 struct hci_cp_le_set_default_phy cp;
3722 if (!(hdev->commands[35] & 0x20))
3725 memset(&cp, 0, sizeof(cp));
3727 cp.tx_phys = hdev->le_tx_def_phys;
3728 cp.rx_phys = hdev->le_rx_def_phys;
3730 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
3731 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3734 static const struct hci_init_stage le_init4[] = {
3735 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
3736 HCI_INIT(hci_le_set_write_def_data_len_sync),
3737 /* HCI_OP_LE_SET_DEFAULT_PHY */
3738 HCI_INIT(hci_le_set_default_phy_sync),
3742 static int hci_init4_sync(struct hci_dev *hdev)
3746 bt_dev_dbg(hdev, "");
3748 err = hci_init_stage_sync(hdev, hci_init4);
3752 if (lmp_le_capable(hdev))
3753 return hci_init_stage_sync(hdev, le_init4);
3758 static int hci_init_sync(struct hci_dev *hdev)
3762 err = hci_init1_sync(hdev);
3766 if (hci_dev_test_flag(hdev, HCI_SETUP))
3767 hci_debugfs_create_basic(hdev);
3769 err = hci_init2_sync(hdev);
3773 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
3774 * BR/EDR/LE type controllers. AMP controllers only need the
3775 * first two stages of init.
3777 if (hdev->dev_type != HCI_PRIMARY)
3780 err = hci_init3_sync(hdev);
3784 err = hci_init4_sync(hdev);
3788 /* This function is only called when the controller is actually in
3789 * configured state. When the controller is marked as unconfigured,
3790 * this initialization procedure is not run.
3792 * It means that it is possible that a controller runs through its
3793 * setup phase and then discovers missing settings. If that is the
3794 * case, then this function will not be called. It then will only
3795 * be called during the config phase.
3797 * So only when in setup phase or config phase, create the debugfs
3798 * entries and register the SMP channels.
3800 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3801 !hci_dev_test_flag(hdev, HCI_CONFIG))
3804 hci_debugfs_create_common(hdev);
3806 if (lmp_bredr_capable(hdev))
3807 hci_debugfs_create_bredr(hdev);
3809 if (lmp_le_capable(hdev))
3810 hci_debugfs_create_le(hdev);
3815 int hci_dev_open_sync(struct hci_dev *hdev)
3819 bt_dev_dbg(hdev, "");
3821 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
3826 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3827 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
3828 /* Check for rfkill but allow the HCI setup stage to
3829 * proceed (which in itself doesn't cause any RF activity).
3831 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
3836 /* Check for valid public address or a configured static
3837 * random address, but let the HCI setup proceed to
3838 * be able to determine if there is a public address
3841 * In case of user channel usage, it is not important
3842 * if a public address or static random address is
3845 * This check is only valid for BR/EDR controllers
3846 * since AMP controllers do not have an address.
3848 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
3849 hdev->dev_type == HCI_PRIMARY &&
3850 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3851 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
3852 ret = -EADDRNOTAVAIL;
3857 if (test_bit(HCI_UP, &hdev->flags)) {
3862 if (hdev->open(hdev)) {
3867 set_bit(HCI_RUNNING, &hdev->flags);
3868 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
3870 atomic_set(&hdev->cmd_cnt, 1);
3871 set_bit(HCI_INIT, &hdev->flags);
3873 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
3874 test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) {
3875 bool invalid_bdaddr;
3877 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
3880 ret = hdev->setup(hdev);
3882 /* The transport driver can set the quirk to mark the
3883 * BD_ADDR invalid before creating the HCI device or in
3884 * its setup callback.
3886 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR,
3892 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) {
3893 if (!bacmp(&hdev->public_addr, BDADDR_ANY))
3894 hci_dev_get_bd_addr_from_property(hdev);
3896 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
3898 ret = hdev->set_bdaddr(hdev,
3899 &hdev->public_addr);
3901 /* If setting of the BD_ADDR from the device
3902 * property succeeds, then treat the address
3903 * as valid even if the invalid BD_ADDR
3904 * quirk indicates otherwise.
3907 invalid_bdaddr = false;
3912 /* The transport driver can set these quirks before
3913 * creating the HCI device or in its setup callback.
3915 * For the invalid BD_ADDR quirk it is possible that
3916 * it becomes a valid address if the bootloader does
3917 * provide it (see above).
3919 * In case any of them is set, the controller has to
3920 * start up as unconfigured.
3922 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
3924 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
3926 /* For an unconfigured controller it is required to
3927 * read at least the version information provided by
3928 * the Read Local Version Information command.
3930 * If the set_bdaddr driver callback is provided, then
3931 * also the original Bluetooth public device address
3932 * will be read using the Read BD Address command.
3934 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
3935 ret = hci_unconf_init_sync(hdev);
3938 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
3939 /* If public address change is configured, ensure that
3940 * the address gets programmed. If the driver does not
3941 * support changing the public address, fail the power
3944 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
3946 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
3948 ret = -EADDRNOTAVAIL;
3952 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
3953 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
3954 ret = hci_init_sync(hdev);
3955 if (!ret && hdev->post_init)
3956 ret = hdev->post_init(hdev);
3960 /* If the HCI Reset command is clearing all diagnostic settings,
3961 * then they need to be reprogrammed after the init procedure
3964 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
3965 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
3966 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
3967 ret = hdev->set_diag(hdev, true);
3969 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
3974 clear_bit(HCI_INIT, &hdev->flags);
3978 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
3979 hci_adv_instances_set_rpa_expired(hdev, true);
3980 set_bit(HCI_UP, &hdev->flags);
3981 hci_sock_dev_event(hdev, HCI_DEV_UP);
3982 hci_leds_update_powered(hdev, true);
3983 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3984 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
3985 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
3986 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
3987 hci_dev_test_flag(hdev, HCI_MGMT) &&
3988 hdev->dev_type == HCI_PRIMARY) {
3989 ret = hci_powered_update_sync(hdev);
3992 /* Init failed, cleanup */
3993 flush_work(&hdev->tx_work);
3995 /* Since hci_rx_work() is possible to awake new cmd_work
3996 * it should be flushed first to avoid unexpected call of
3999 flush_work(&hdev->rx_work);
4000 flush_work(&hdev->cmd_work);
4002 skb_queue_purge(&hdev->cmd_q);
4003 skb_queue_purge(&hdev->rx_q);
4008 if (hdev->sent_cmd) {
4009 kfree_skb(hdev->sent_cmd);
4010 hdev->sent_cmd = NULL;
4013 clear_bit(HCI_RUNNING, &hdev->flags);
4014 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4017 hdev->flags &= BIT(HCI_RAW);
4024 /* This function requires the caller holds hdev->lock */
4025 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4027 struct hci_conn_params *p;
4029 list_for_each_entry(p, &hdev->le_conn_params, list) {
4031 hci_conn_drop(p->conn);
4032 hci_conn_put(p->conn);
4035 list_del_init(&p->action);
4038 BT_DBG("All LE pending actions cleared");
4041 int hci_dev_close_sync(struct hci_dev *hdev)
4046 bt_dev_dbg(hdev, "");
4048 cancel_delayed_work(&hdev->power_off);
4049 cancel_delayed_work(&hdev->ncmd_timer);
4051 hci_request_cancel_all(hdev);
4053 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4054 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4055 test_bit(HCI_UP, &hdev->flags)) {
4056 /* Execute vendor specific shutdown routine */
4058 err = hdev->shutdown(hdev);
4061 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4062 cancel_delayed_work_sync(&hdev->cmd_timer);
4066 hci_leds_update_powered(hdev, false);
4068 /* Flush RX and TX works */
4069 flush_work(&hdev->tx_work);
4070 flush_work(&hdev->rx_work);
4072 if (hdev->discov_timeout > 0) {
4073 hdev->discov_timeout = 0;
4074 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4075 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
4078 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
4079 cancel_delayed_work(&hdev->service_cache);
4081 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4082 struct adv_info *adv_instance;
4084 cancel_delayed_work_sync(&hdev->rpa_expired);
4086 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
4087 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
4090 /* Avoid potential lockdep warnings from the *_flush() calls by
4091 * ensuring the workqueue is empty up front.
4093 drain_workqueue(hdev->workqueue);
4097 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4099 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
4101 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
4102 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4103 hci_dev_test_flag(hdev, HCI_MGMT))
4104 __mgmt_power_off(hdev);
4106 hci_inquiry_cache_flush(hdev);
4107 hci_pend_le_actions_clear(hdev);
4108 hci_conn_hash_flush(hdev);
4109 hci_dev_unlock(hdev);
4111 smp_unregister(hdev);
4113 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
4115 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4116 aosp_do_close(hdev);
4117 msft_do_close(hdev);
4124 skb_queue_purge(&hdev->cmd_q);
4125 atomic_set(&hdev->cmd_cnt, 1);
4126 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
4127 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4128 set_bit(HCI_INIT, &hdev->flags);
4129 hci_reset_sync(hdev);
4130 clear_bit(HCI_INIT, &hdev->flags);
4133 /* flush cmd work */
4134 flush_work(&hdev->cmd_work);
4137 skb_queue_purge(&hdev->rx_q);
4138 skb_queue_purge(&hdev->cmd_q);
4139 skb_queue_purge(&hdev->raw_q);
4141 /* Drop last sent command */
4142 if (hdev->sent_cmd) {
4143 cancel_delayed_work_sync(&hdev->cmd_timer);
4144 kfree_skb(hdev->sent_cmd);
4145 hdev->sent_cmd = NULL;
4148 clear_bit(HCI_RUNNING, &hdev->flags);
4149 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4151 /* After this point our queues are empty and no tasks are scheduled. */
4155 hdev->flags &= BIT(HCI_RAW);
4156 hci_dev_clear_volatile_flags(hdev);
4158 /* Controller radio is available but is currently powered down */
4159 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
4161 memset(hdev->eir, 0, sizeof(hdev->eir));
4162 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
4163 bacpy(&hdev->random_addr, BDADDR_ANY);
4169 /* This function perform power on HCI command sequence as follows:
4171 * If controller is already up (HCI_UP) performs hci_powered_update_sync
4172 * sequence otherwise run hci_dev_open_sync which will follow with
4173 * hci_powered_update_sync after the init sequence is completed.
4175 static int hci_power_on_sync(struct hci_dev *hdev)
4179 if (test_bit(HCI_UP, &hdev->flags) &&
4180 hci_dev_test_flag(hdev, HCI_MGMT) &&
4181 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
4182 cancel_delayed_work(&hdev->power_off);
4183 return hci_powered_update_sync(hdev);
4186 err = hci_dev_open_sync(hdev);
4190 /* During the HCI setup phase, a few error conditions are
4191 * ignored and they need to be checked now. If they are still
4192 * valid, it is important to return the device back off.
4194 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
4195 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
4196 (hdev->dev_type == HCI_PRIMARY &&
4197 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4198 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
4199 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
4200 hci_dev_close_sync(hdev);
4201 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
4202 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
4203 HCI_AUTO_OFF_TIMEOUT);
4206 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
4207 /* For unconfigured devices, set the HCI_RAW flag
4208 * so that userspace can easily identify them.
4210 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4211 set_bit(HCI_RAW, &hdev->flags);
4213 /* For fully configured devices, this will send
4214 * the Index Added event. For unconfigured devices,
4215 * it will send Unconfigued Index Added event.
4217 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
4218 * and no event will be send.
4220 mgmt_index_added(hdev);
4221 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
4222 /* When the controller is now configured, then it
4223 * is important to clear the HCI_RAW flag.
4225 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4226 clear_bit(HCI_RAW, &hdev->flags);
4228 /* Powering on the controller with HCI_CONFIG set only
4229 * happens with the transition from unconfigured to
4230 * configured. This will send the Index Added event.
4232 mgmt_index_added(hdev);
4238 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
4240 struct hci_cp_remote_name_req_cancel cp;
4242 memset(&cp, 0, sizeof(cp));
4243 bacpy(&cp.bdaddr, addr);
4245 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
4246 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4249 int hci_stop_discovery_sync(struct hci_dev *hdev)
4251 struct discovery_state *d = &hdev->discovery;
4252 struct inquiry_entry *e;
4255 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
4257 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
4258 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
4259 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
4260 0, NULL, HCI_CMD_TIMEOUT);
4265 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
4266 cancel_delayed_work(&hdev->le_scan_disable);
4267 cancel_delayed_work(&hdev->le_scan_restart);
4269 err = hci_scan_disable_sync(hdev);
4275 err = hci_scan_disable_sync(hdev);
4280 /* Resume advertising if it was paused */
4281 if (use_ll_privacy(hdev))
4282 hci_resume_advertising_sync(hdev);
4284 /* No further actions needed for LE-only discovery */
4285 if (d->type == DISCOV_TYPE_LE)
4288 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
4289 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
4294 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
4300 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
4303 struct hci_cp_disconn_phy_link cp;
4305 memset(&cp, 0, sizeof(cp));
4306 cp.phy_handle = HCI_PHY_HANDLE(handle);
4309 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
4310 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4313 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
4316 struct hci_cp_disconnect cp;
4318 if (conn->type == AMP_LINK)
4319 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
4321 memset(&cp, 0, sizeof(cp));
4322 cp.handle = cpu_to_le16(conn->handle);
4325 /* Wait for HCI_EV_DISCONN_COMPLETE not HCI_EV_CMD_STATUS when not
4328 if (!hdev->suspended)
4329 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
4331 HCI_EV_DISCONN_COMPLETE,
4332 HCI_CMD_TIMEOUT, NULL);
4334 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
4338 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
4339 struct hci_conn *conn)
4341 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
4344 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
4345 6, &conn->dst, HCI_CMD_TIMEOUT);
4348 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn)
4350 if (conn->type == LE_LINK)
4351 return hci_le_connect_cancel_sync(hdev, conn);
4353 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
4356 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
4357 6, &conn->dst, HCI_CMD_TIMEOUT);
4360 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
4363 struct hci_cp_reject_sync_conn_req cp;
4365 memset(&cp, 0, sizeof(cp));
4366 bacpy(&cp.bdaddr, &conn->dst);
4369 /* SCO rejection has its own limited set of
4370 * allowed error values (0x0D-0x0F).
4372 if (reason < 0x0d || reason > 0x0f)
4373 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
4375 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
4376 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4379 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
4382 struct hci_cp_reject_conn_req cp;
4384 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
4385 return hci_reject_sco_sync(hdev, conn, reason);
4387 memset(&cp, 0, sizeof(cp));
4388 bacpy(&cp.bdaddr, &conn->dst);
4391 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
4392 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4395 static int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
4398 switch (conn->state) {
4401 return hci_disconnect_sync(hdev, conn, reason);
4403 return hci_connect_cancel_sync(hdev, conn);
4405 return hci_reject_conn_sync(hdev, conn, reason);
4407 conn->state = BT_CLOSED;
4414 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
4416 struct hci_conn *conn, *tmp;
4419 list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
4420 err = hci_abort_conn_sync(hdev, conn, reason);
4428 /* This function perform power off HCI command sequence as follows:
4432 * Disconnect all connections
4433 * hci_dev_close_sync
4435 static int hci_power_off_sync(struct hci_dev *hdev)
4439 /* If controller is already down there is nothing to do */
4440 if (!test_bit(HCI_UP, &hdev->flags))
4443 if (test_bit(HCI_ISCAN, &hdev->flags) ||
4444 test_bit(HCI_PSCAN, &hdev->flags)) {
4445 err = hci_write_scan_enable_sync(hdev, 0x00);
4450 err = hci_clear_adv_sync(hdev, NULL, false);
4454 err = hci_stop_discovery_sync(hdev);
4458 /* Terminated due to Power Off */
4459 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
4463 return hci_dev_close_sync(hdev);
4466 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
4469 return hci_power_on_sync(hdev);
4471 return hci_power_off_sync(hdev);
4474 static int hci_write_iac_sync(struct hci_dev *hdev)
4476 struct hci_cp_write_current_iac_lap cp;
4478 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
4481 memset(&cp, 0, sizeof(cp));
4483 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
4484 /* Limited discoverable mode */
4485 cp.num_iac = min_t(u8, hdev->num_iac, 2);
4486 cp.iac_lap[0] = 0x00; /* LIAC */
4487 cp.iac_lap[1] = 0x8b;
4488 cp.iac_lap[2] = 0x9e;
4489 cp.iac_lap[3] = 0x33; /* GIAC */
4490 cp.iac_lap[4] = 0x8b;
4491 cp.iac_lap[5] = 0x9e;
4493 /* General discoverable mode */
4495 cp.iac_lap[0] = 0x33; /* GIAC */
4496 cp.iac_lap[1] = 0x8b;
4497 cp.iac_lap[2] = 0x9e;
4500 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
4501 (cp.num_iac * 3) + 1, &cp,
4505 int hci_update_discoverable_sync(struct hci_dev *hdev)
4509 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
4510 err = hci_write_iac_sync(hdev);
4514 err = hci_update_scan_sync(hdev);
4518 err = hci_update_class_sync(hdev);
4523 /* Advertising instances don't use the global discoverable setting, so
4524 * only update AD if advertising was enabled using Set Advertising.
4526 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
4527 err = hci_update_adv_data_sync(hdev, 0x00);
4531 /* Discoverable mode affects the local advertising
4532 * address in limited privacy mode.
4534 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
4535 if (ext_adv_capable(hdev))
4536 err = hci_start_ext_adv_sync(hdev, 0x00);
4538 err = hci_enable_advertising_sync(hdev);
4545 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
4547 return hci_update_discoverable_sync(hdev);
4550 int hci_update_discoverable(struct hci_dev *hdev)
4552 /* Only queue if it would have any effect */
4553 if (hdev_is_powered(hdev) &&
4554 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4555 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
4556 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
4557 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
4563 int hci_update_connectable_sync(struct hci_dev *hdev)
4567 err = hci_update_scan_sync(hdev);
4571 /* If BR/EDR is not enabled and we disable advertising as a
4572 * by-product of disabling connectable, we need to update the
4573 * advertising flags.
4575 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4576 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
4578 /* Update the advertising parameters if necessary */
4579 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4580 !list_empty(&hdev->adv_instances)) {
4581 if (ext_adv_capable(hdev))
4582 err = hci_start_ext_adv_sync(hdev,
4583 hdev->cur_adv_instance);
4585 err = hci_enable_advertising_sync(hdev);
4591 return hci_update_passive_scan_sync(hdev);
4594 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
4596 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
4597 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
4598 struct hci_cp_inquiry cp;
4600 bt_dev_dbg(hdev, "");
4602 if (hci_dev_test_flag(hdev, HCI_INQUIRY))
4606 hci_inquiry_cache_flush(hdev);
4607 hci_dev_unlock(hdev);
4609 memset(&cp, 0, sizeof(cp));
4611 if (hdev->discovery.limited)
4612 memcpy(&cp.lap, liac, sizeof(cp.lap));
4614 memcpy(&cp.lap, giac, sizeof(cp.lap));
4618 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
4619 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4622 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
4625 /* Accept list is not used for discovery */
4626 u8 filter_policy = 0x00;
4627 /* Default is to enable duplicates filter */
4628 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
4631 bt_dev_dbg(hdev, "");
4633 /* If controller is scanning, it means the passive scanning is
4634 * running. Thus, we should temporarily stop it in order to set the
4635 * discovery scanning parameters.
4637 err = hci_scan_disable_sync(hdev);
4639 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
4643 cancel_interleave_scan(hdev);
4645 /* Pause advertising since active scanning disables address resolution
4646 * which advertising depend on in order to generate its RPAs.
4648 if (use_ll_privacy(hdev)) {
4649 err = hci_pause_advertising_sync(hdev);
4651 bt_dev_err(hdev, "pause advertising failed: %d", err);
4656 /* Disable address resolution while doing active scanning since the
4657 * accept list shall not be used and all reports shall reach the host
4660 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
4662 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
4667 /* All active scans will be done with either a resolvable private
4668 * address (when privacy feature has been enabled) or non-resolvable
4671 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
4674 own_addr_type = ADDR_LE_DEV_PUBLIC;
4676 if (hci_is_adv_monitoring(hdev)) {
4677 /* Duplicate filter should be disabled when some advertisement
4678 * monitor is activated, otherwise AdvMon can only receive one
4679 * advertisement for one peer(*) during active scanning, and
4680 * might report loss to these peers.
4682 * Note that different controllers have different meanings of
4683 * |duplicate|. Some of them consider packets with the same
4684 * address as duplicate, and others consider packets with the
4685 * same address and the same RSSI as duplicate. Although in the
4686 * latter case we don't need to disable duplicate filter, but
4687 * it is common to have active scanning for a short period of
4688 * time, the power impact should be neglectable.
4690 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
4693 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
4694 hdev->le_scan_window_discovery,
4695 own_addr_type, filter_policy, filter_dup);
4700 /* Resume advertising if it was paused */
4701 if (use_ll_privacy(hdev))
4702 hci_resume_advertising_sync(hdev);
4704 /* Resume passive scanning */
4705 hci_update_passive_scan_sync(hdev);
4709 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
4713 bt_dev_dbg(hdev, "");
4715 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
4719 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
4722 int hci_start_discovery_sync(struct hci_dev *hdev)
4724 unsigned long timeout;
4727 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
4729 switch (hdev->discovery.type) {
4730 case DISCOV_TYPE_BREDR:
4731 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
4732 case DISCOV_TYPE_INTERLEAVED:
4733 /* When running simultaneous discovery, the LE scanning time
4734 * should occupy the whole discovery time sine BR/EDR inquiry
4735 * and LE scanning are scheduled by the controller.
4737 * For interleaving discovery in comparison, BR/EDR inquiry
4738 * and LE scanning are done sequentially with separate
4741 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
4743 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4744 /* During simultaneous discovery, we double LE scan
4745 * interval. We must leave some time for the controller
4746 * to do BR/EDR inquiry.
4748 err = hci_start_interleaved_discovery_sync(hdev);
4752 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
4753 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
4755 case DISCOV_TYPE_LE:
4756 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4757 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
4766 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
4768 /* When service discovery is used and the controller has a
4769 * strict duplicate filter, it is important to remember the
4770 * start and duration of the scan. This is required for
4771 * restarting scanning during the discovery phase.
4773 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
4774 hdev->discovery.result_filtering) {
4775 hdev->discovery.scan_start = jiffies;
4776 hdev->discovery.scan_duration = timeout;
4779 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
4784 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
4786 switch (hci_get_adv_monitor_offload_ext(hdev)) {
4787 case HCI_ADV_MONITOR_EXT_MSFT:
4788 msft_suspend_sync(hdev);
4795 /* This function disables discovery and mark it as paused */
4796 static int hci_pause_discovery_sync(struct hci_dev *hdev)
4798 int old_state = hdev->discovery.state;
4801 /* If discovery already stopped/stopping/paused there nothing to do */
4802 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
4803 hdev->discovery_paused)
4806 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
4807 err = hci_stop_discovery_sync(hdev);
4811 hdev->discovery_paused = true;
4812 hdev->discovery_old_state = old_state;
4813 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4818 static int hci_update_event_filter_sync(struct hci_dev *hdev)
4820 struct bdaddr_list_with_flags *b;
4821 u8 scan = SCAN_DISABLED;
4822 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
4825 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4828 /* Always clear event filter when starting */
4829 hci_clear_event_filter_sync(hdev);
4831 list_for_each_entry(b, &hdev->accept_list, list) {
4832 if (!test_bit(HCI_CONN_FLAG_REMOTE_WAKEUP, b->flags))
4835 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
4837 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
4838 HCI_CONN_SETUP_ALLOW_BDADDR,
4840 HCI_CONN_SETUP_AUTO_ON);
4842 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
4848 if (scan && !scanning)
4849 hci_write_scan_enable_sync(hdev, scan);
4850 else if (!scan && scanning)
4851 hci_write_scan_enable_sync(hdev, scan);
4856 /* This function performs the HCI suspend procedures in the follow order:
4858 * Pause discovery (active scanning/inquiry)
4859 * Pause Directed Advertising/Advertising
4860 * Disconnect all connections
4861 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
4863 * Update event mask (only set events that are allowed to wake up the host)
4864 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
4865 * Update passive scanning (lower duty cycle)
4866 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
4868 int hci_suspend_sync(struct hci_dev *hdev)
4872 /* If marked as suspended there nothing to do */
4873 if (hdev->suspended)
4876 /* Mark device as suspended */
4877 hdev->suspended = true;
4879 /* Pause discovery if not already stopped */
4880 hci_pause_discovery_sync(hdev);
4882 /* Pause other advertisements */
4883 hci_pause_advertising_sync(hdev);
4885 /* Disable page scan if enabled */
4886 if (test_bit(HCI_PSCAN, &hdev->flags))
4887 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
4889 /* Suspend monitor filters */
4890 hci_suspend_monitor_sync(hdev);
4892 /* Prevent disconnects from causing scanning to be re-enabled */
4893 hdev->scanning_paused = true;
4895 /* Soft disconnect everything (power off) */
4896 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
4898 /* Set state to BT_RUNNING so resume doesn't notify */
4899 hdev->suspend_state = BT_RUNNING;
4900 hci_resume_sync(hdev);
4904 /* Only configure accept list if disconnect succeeded and wake
4905 * isn't being prevented.
4907 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
4908 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
4912 /* Unpause to take care of updating scanning params */
4913 hdev->scanning_paused = false;
4915 /* Update event mask so only the allowed event can wakeup the host */
4916 hci_set_event_mask_sync(hdev);
4918 /* Enable event filter for paired devices */
4919 hci_update_event_filter_sync(hdev);
4921 /* Update LE passive scan if enabled */
4922 hci_update_passive_scan_sync(hdev);
4924 /* Pause scan changes again. */
4925 hdev->scanning_paused = true;
4927 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
4932 /* This function resumes discovery */
4933 static int hci_resume_discovery_sync(struct hci_dev *hdev)
4937 /* If discovery not paused there nothing to do */
4938 if (!hdev->discovery_paused)
4941 hdev->discovery_paused = false;
4943 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4945 err = hci_start_discovery_sync(hdev);
4947 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
4953 static void hci_resume_monitor_sync(struct hci_dev *hdev)
4955 switch (hci_get_adv_monitor_offload_ext(hdev)) {
4956 case HCI_ADV_MONITOR_EXT_MSFT:
4957 msft_resume_sync(hdev);
4964 /* This function performs the HCI suspend procedures in the follow order:
4966 * Restore event mask
4967 * Clear event filter
4968 * Update passive scanning (normal duty cycle)
4969 * Resume Directed Advertising/Advertising
4970 * Resume discovery (active scanning/inquiry)
4972 int hci_resume_sync(struct hci_dev *hdev)
4974 /* If not marked as suspended there nothing to do */
4975 if (!hdev->suspended)
4978 hdev->suspended = false;
4979 hdev->scanning_paused = false;
4981 /* Restore event mask */
4982 hci_set_event_mask_sync(hdev);
4984 /* Clear any event filters and restore scan state */
4985 hci_clear_event_filter_sync(hdev);
4986 hci_update_scan_sync(hdev);
4988 /* Reset passive scanning to normal */
4989 hci_update_passive_scan_sync(hdev);
4991 /* Resume monitor filters */
4992 hci_resume_monitor_sync(hdev);
4994 /* Resume other advertisements */
4995 hci_resume_advertising_sync(hdev);
4997 /* Resume discovery */
4998 hci_resume_discovery_sync(hdev);
5003 static bool conn_use_rpa(struct hci_conn *conn)
5005 struct hci_dev *hdev = conn->hdev;
5007 return hci_dev_test_flag(hdev, HCI_PRIVACY);
5010 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
5011 struct hci_conn *conn)
5013 struct hci_cp_le_set_ext_adv_params cp;
5015 bdaddr_t random_addr;
5018 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5023 /* Set require_privacy to false so that the remote device has a
5024 * chance of identifying us.
5026 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
5027 &own_addr_type, &random_addr);
5031 memset(&cp, 0, sizeof(cp));
5033 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
5034 cp.own_addr_type = own_addr_type;
5035 cp.channel_map = hdev->le_adv_channel_map;
5036 cp.tx_power = HCI_TX_POWER_INVALID;
5037 cp.primary_phy = HCI_ADV_PHY_1M;
5038 cp.secondary_phy = HCI_ADV_PHY_1M;
5039 cp.handle = 0x00; /* Use instance 0 for directed adv */
5040 cp.own_addr_type = own_addr_type;
5041 cp.peer_addr_type = conn->dst_type;
5042 bacpy(&cp.peer_addr, &conn->dst);
5044 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
5045 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
5046 * does not supports advertising data when the advertising set already
5047 * contains some, the controller shall return erroc code 'Invalid
5048 * HCI Command Parameters(0x12).
5049 * So it is required to remove adv set for handle 0x00. since we use
5050 * instance 0 for directed adv.
5052 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
5056 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
5057 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5061 /* Check if random address need to be updated */
5062 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
5063 bacmp(&random_addr, BDADDR_ANY) &&
5064 bacmp(&random_addr, &hdev->random_addr)) {
5065 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
5071 return hci_enable_ext_advertising_sync(hdev, 0x00);
5074 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
5075 struct hci_conn *conn)
5077 struct hci_cp_le_set_adv_param cp;
5082 if (ext_adv_capable(hdev))
5083 return hci_le_ext_directed_advertising_sync(hdev, conn);
5085 /* Clear the HCI_LE_ADV bit temporarily so that the
5086 * hci_update_random_address knows that it's safe to go ahead
5087 * and write a new random address. The flag will be set back on
5088 * as soon as the SET_ADV_ENABLE HCI command completes.
5090 hci_dev_clear_flag(hdev, HCI_LE_ADV);
5092 /* Set require_privacy to false so that the remote device has a
5093 * chance of identifying us.
5095 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5100 memset(&cp, 0, sizeof(cp));
5102 /* Some controllers might reject command if intervals are not
5103 * within range for undirected advertising.
5104 * BCM20702A0 is known to be affected by this.
5106 cp.min_interval = cpu_to_le16(0x0020);
5107 cp.max_interval = cpu_to_le16(0x0020);
5109 cp.type = LE_ADV_DIRECT_IND;
5110 cp.own_address_type = own_addr_type;
5111 cp.direct_addr_type = conn->dst_type;
5112 bacpy(&cp.direct_addr, &conn->dst);
5113 cp.channel_map = hdev->le_adv_channel_map;
5115 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
5116 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5122 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
5123 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
5126 static void set_ext_conn_params(struct hci_conn *conn,
5127 struct hci_cp_le_ext_conn_param *p)
5129 struct hci_dev *hdev = conn->hdev;
5131 memset(p, 0, sizeof(*p));
5133 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
5134 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
5135 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
5136 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
5137 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
5138 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
5139 p->min_ce_len = cpu_to_le16(0x0000);
5140 p->max_ce_len = cpu_to_le16(0x0000);
5143 int hci_le_ext_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5146 struct hci_cp_le_ext_create_conn *cp;
5147 struct hci_cp_le_ext_conn_param *p;
5148 u8 data[sizeof(*cp) + sizeof(*p) * 3];
5152 p = (void *)cp->data;
5154 memset(cp, 0, sizeof(*cp));
5156 bacpy(&cp->peer_addr, &conn->dst);
5157 cp->peer_addr_type = conn->dst_type;
5158 cp->own_addr_type = own_addr_type;
5162 if (scan_1m(hdev)) {
5163 cp->phys |= LE_SCAN_PHY_1M;
5164 set_ext_conn_params(conn, p);
5170 if (scan_2m(hdev)) {
5171 cp->phys |= LE_SCAN_PHY_2M;
5172 set_ext_conn_params(conn, p);
5178 if (scan_coded(hdev)) {
5179 cp->phys |= LE_SCAN_PHY_CODED;
5180 set_ext_conn_params(conn, p);
5185 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
5187 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
5188 HCI_CMD_TIMEOUT, NULL);
5191 int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
5193 struct hci_cp_le_create_conn cp;
5194 struct hci_conn_params *params;
5198 /* If requested to connect as peripheral use directed advertising */
5199 if (conn->role == HCI_ROLE_SLAVE) {
5200 /* If we're active scanning and simultaneous roles is not
5201 * enabled simply reject the attempt.
5203 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
5204 hdev->le_scan_type == LE_SCAN_ACTIVE &&
5205 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
5210 /* Pause advertising while doing directed advertising. */
5211 hci_pause_advertising_sync(hdev);
5213 err = hci_le_directed_advertising_sync(hdev, conn);
5217 /* Disable advertising if simultaneous roles is not in use. */
5218 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
5219 hci_pause_advertising_sync(hdev);
5221 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
5223 conn->le_conn_min_interval = params->conn_min_interval;
5224 conn->le_conn_max_interval = params->conn_max_interval;
5225 conn->le_conn_latency = params->conn_latency;
5226 conn->le_supv_timeout = params->supervision_timeout;
5228 conn->le_conn_min_interval = hdev->le_conn_min_interval;
5229 conn->le_conn_max_interval = hdev->le_conn_max_interval;
5230 conn->le_conn_latency = hdev->le_conn_latency;
5231 conn->le_supv_timeout = hdev->le_supv_timeout;
5234 /* If controller is scanning, we stop it since some controllers are
5235 * not able to scan and connect at the same time. Also set the
5236 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
5237 * handler for scan disabling knows to set the correct discovery
5240 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5241 hci_scan_disable_sync(hdev);
5242 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
5245 /* Update random address, but set require_privacy to false so
5246 * that we never connect with an non-resolvable address.
5248 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5253 if (use_ext_conn(hdev)) {
5254 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
5258 memset(&cp, 0, sizeof(cp));
5260 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
5261 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
5263 bacpy(&cp.peer_addr, &conn->dst);
5264 cp.peer_addr_type = conn->dst_type;
5265 cp.own_address_type = own_addr_type;
5266 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
5267 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
5268 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
5269 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
5270 cp.min_ce_len = cpu_to_le16(0x0000);
5271 cp.max_ce_len = cpu_to_le16(0x0000);
5273 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
5274 sizeof(cp), &cp, HCI_EV_LE_CONN_COMPLETE,
5275 HCI_CMD_TIMEOUT, NULL);
5278 /* Re-enable advertising after the connection attempt is finished. */
5279 hci_resume_advertising_sync(hdev);