Lines Matching refs:hdev

24 static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
27 bt_dev_dbg(hdev, "result 0x%2.2x", result);
29 if (hdev->req_status != HCI_REQ_PEND)
32 hdev->req_result = result;
33 hdev->req_status = HCI_REQ_DONE;
36 kfree_skb(hdev->req_skb);
37 hdev->req_skb = NULL;
46 hdev->req_rsp = skb_get(skb);
49 wake_up_interruptible(&hdev->req_wait_q);
52 static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
71 bt_dev_dbg(hdev, "skb len %d", skb->len);
90 struct hci_dev *hdev = req->hdev;
93 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
101 skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
103 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
119 struct hci_dev *hdev = req->hdev;
123 bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
141 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
142 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
143 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
145 queue_work(hdev->workqueue, &hdev->cmd_work);
150 /* This function requires the caller holds hdev->req_lock. */
151 struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
159 bt_dev_dbg(hdev, "Opcode 0x%4.4x", opcode);
161 hci_req_init(&req, hdev);
165 hdev->req_status = HCI_REQ_PEND;
171 err = wait_event_interruptible_timeout(hdev->req_wait_q,
172 hdev->req_status != HCI_REQ_PEND,
178 switch (hdev->req_status) {
180 err = -bt_to_errno(hdev->req_result);
184 err = -hdev->req_result;
192 hdev->req_status = 0;
193 hdev->req_result = 0;
194 skb = hdev->req_rsp;
195 hdev->req_rsp = NULL;
197 bt_dev_dbg(hdev, "end: err %d", err);
208 /* This function requires the caller holds hdev->req_lock. */
209 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
212 return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
217 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
222 if (!test_bit(HCI_UP, &hdev->flags))
225 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
227 hci_req_sync_lock(hdev);
228 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
229 hci_req_sync_unlock(hdev);
235 /* This function requires the caller holds hdev->req_lock. */
236 struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
239 return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
244 /* This function requires the caller holds hdev->req_lock. */
245 int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
252 skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
255 bt_dev_err(hdev, "Opcode 0x%4.4x failed: %ld", opcode,
275 int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
278 return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
285 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
287 bt_dev_dbg(hdev, "");
293 mutex_lock(&hdev->cmd_sync_work_lock);
294 entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
299 mutex_unlock(&hdev->cmd_sync_work_lock);
304 bt_dev_dbg(hdev, "entry %p", entry);
309 hci_req_sync_lock(hdev);
310 err = entry->func(hdev, entry->data);
312 entry->destroy(hdev, entry->data, err);
313 hci_req_sync_unlock(hdev);
322 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
324 cancel_delayed_work_sync(&hdev->cmd_timer);
325 cancel_delayed_work_sync(&hdev->ncmd_timer);
326 atomic_set(&hdev->cmd_cnt, 1);
328 wake_up_interruptible(&hdev->req_wait_q);
331 static int hci_scan_disable_sync(struct hci_dev *hdev);
332 static int scan_disable_sync(struct hci_dev *hdev, void *data)
334 return hci_scan_disable_sync(hdev);
337 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length);
338 static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data)
340 return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN);
345 struct hci_dev *hdev = container_of(work, struct hci_dev,
349 bt_dev_dbg(hdev, "");
350 hci_dev_lock(hdev);
352 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
355 status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
357 bt_dev_err(hdev, "failed to disable LE scan: %d", status);
361 hdev->discovery.scan_start = 0;
371 if (hdev->discovery.type == DISCOV_TYPE_LE)
374 if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
377 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
378 if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
379 hdev->discovery.state != DISCOVERY_RESOLVING)
385 status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
387 bt_dev_err(hdev, "inquiry failed: status %d", status);
394 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
397 hci_dev_unlock(hdev);
400 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
403 static int reenable_adv_sync(struct hci_dev *hdev, void *data)
405 bt_dev_dbg(hdev, "");
407 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
408 list_empty(&hdev->adv_instances))
411 if (hdev->cur_adv_instance) {
412 return hci_schedule_adv_instance_sync(hdev,
413 hdev->cur_adv_instance,
416 if (ext_adv_capable(hdev)) {
417 hci_start_ext_adv_sync(hdev, 0x00);
419 hci_update_adv_data_sync(hdev, 0x00);
420 hci_update_scan_rsp_data_sync(hdev, 0x00);
421 hci_enable_advertising_sync(hdev);
430 struct hci_dev *hdev = container_of(work, struct hci_dev,
434 bt_dev_dbg(hdev, "");
436 hci_dev_lock(hdev);
438 status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
440 bt_dev_err(hdev, "failed to reenable ADV: %d", status);
442 hci_dev_unlock(hdev);
445 static void cancel_adv_timeout(struct hci_dev *hdev)
447 if (hdev->adv_instance_timeout) {
448 hdev->adv_instance_timeout = 0;
449 cancel_delayed_work(&hdev->adv_instance_expire);
464 int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
472 if (!instance || hdev->cur_adv_instance == instance)
473 cancel_adv_timeout(hdev);
479 if (instance && hdev->cur_adv_instance == instance)
480 next_instance = hci_get_next_instance(hdev, instance);
483 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
489 err = hci_remove_adv_instance(hdev, rem_inst);
491 mgmt_advertising_removed(sk, hdev, rem_inst);
494 adv_instance = hci_find_adv_instance(hdev, instance);
503 err = hci_remove_adv_instance(hdev, instance);
505 mgmt_advertising_removed(sk, hdev, instance);
509 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
512 if (next_instance && !ext_adv_capable(hdev))
513 return hci_schedule_adv_instance_sync(hdev,
520 static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
526 hci_clear_adv_instance_sync(hdev, NULL, instance, false);
528 if (list_empty(&hdev->adv_instances))
529 return hci_disable_advertising_sync(hdev);
537 struct hci_dev *hdev = container_of(work, struct hci_dev,
540 bt_dev_dbg(hdev, "");
542 hci_dev_lock(hdev);
544 hdev->adv_instance_timeout = 0;
546 if (hdev->cur_adv_instance == 0x00)
553 *inst_ptr = hdev->cur_adv_instance;
554 hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
557 hci_dev_unlock(hdev);
560 void hci_cmd_sync_init(struct hci_dev *hdev)
562 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
563 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
564 mutex_init(&hdev->cmd_sync_work_lock);
565 mutex_init(&hdev->unregister_lock);
567 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
568 INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
569 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
570 INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
573 static void _hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
578 entry->destroy(hdev, entry->data, err);
584 void hci_cmd_sync_clear(struct hci_dev *hdev)
588 cancel_work_sync(&hdev->cmd_sync_work);
589 cancel_work_sync(&hdev->reenable_adv_work);
591 mutex_lock(&hdev->cmd_sync_work_lock);
592 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list)
593 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
594 mutex_unlock(&hdev->cmd_sync_work_lock);
597 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
599 bt_dev_dbg(hdev, "err 0x%2.2x", err);
601 if (hdev->req_status == HCI_REQ_PEND) {
602 hdev->req_result = err;
603 hdev->req_status = HCI_REQ_CANCELED;
605 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
615 void hci_cmd_sync_cancel_sync(struct hci_dev *hdev, int err)
617 bt_dev_dbg(hdev, "err 0x%2.2x", err);
619 if (hdev->req_status == HCI_REQ_PEND) {
623 hdev->req_result = err < 0 ? -err : err;
624 hdev->req_status = HCI_REQ_CANCELED;
626 wake_up_interruptible(&hdev->req_wait_q);
633 * - hdev must _not_ be unregistered
635 int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
641 mutex_lock(&hdev->unregister_lock);
642 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
656 mutex_lock(&hdev->cmd_sync_work_lock);
657 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
658 mutex_unlock(&hdev->cmd_sync_work_lock);
660 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
663 mutex_unlock(&hdev->unregister_lock);
670 * - hdev must be running
672 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
675 /* Only queue command if hdev is running which means it had been opened
678 if (!test_bit(HCI_RUNNING, &hdev->flags))
681 return hci_cmd_sync_submit(hdev, func, data, destroy);
686 _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
691 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
712 int hci_cmd_sync_queue_once(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
715 if (hci_cmd_sync_lookup_entry(hdev, func, data, destroy))
718 return hci_cmd_sync_queue(hdev, func, data, destroy);
728 hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
733 mutex_lock(&hdev->cmd_sync_work_lock);
734 entry = _hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
735 mutex_unlock(&hdev->cmd_sync_work_lock);
742 void hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
745 mutex_lock(&hdev->cmd_sync_work_lock);
746 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
747 mutex_unlock(&hdev->cmd_sync_work_lock);
755 bool hci_cmd_sync_dequeue_once(struct hci_dev *hdev,
761 entry = hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
765 hci_cmd_sync_cancel_entry(hdev, entry);
776 bool hci_cmd_sync_dequeue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
782 mutex_lock(&hdev->cmd_sync_work_lock);
783 while ((entry = _hci_cmd_sync_lookup_entry(hdev, func, data,
785 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
788 mutex_unlock(&hdev->cmd_sync_work_lock);
794 int hci_update_eir_sync(struct hci_dev *hdev)
798 bt_dev_dbg(hdev, "");
800 if (!hdev_is_powered(hdev))
803 if (!lmp_ext_inq_capable(hdev))
806 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
809 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
814 eir_create(hdev, cp.data);
816 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
819 memcpy(hdev->eir, cp.data, sizeof(cp.data));
821 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
825 static u8 get_service_classes(struct hci_dev *hdev)
830 list_for_each_entry(uuid, &hdev->uuids, list)
836 int hci_update_class_sync(struct hci_dev *hdev)
840 bt_dev_dbg(hdev, "");
842 if (!hdev_is_powered(hdev))
845 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
848 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
851 cod[0] = hdev->minor_class;
852 cod[1] = hdev->major_class;
853 cod[2] = get_service_classes(hdev);
855 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
858 if (memcmp(cod, hdev->dev_class, 3) == 0)
861 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
865 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
868 if (hci_conn_num(hdev, LE_LINK) == 0)
872 if (hdev->conn_hash.le_num_peripheral > 0) {
876 if (!connectable && !(hdev->le_states[2] & 0x10))
882 if (connectable && (!(hdev->le_states[4] & 0x40) ||
883 !(hdev->le_states[2] & 0x20)))
888 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
890 if (!connectable && !(hdev->le_states[2] & 0x02))
896 if (connectable && (!(hdev->le_states[4] & 0x08) ||
897 !(hdev->le_states[2] & 0x08)))
904 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
907 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
911 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
918 hci_dev_test_flag(hdev, HCI_BONDABLE))
927 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
939 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
940 hci_lookup_le_connect(hdev)) {
941 bt_dev_dbg(hdev, "Deferring random address update");
942 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
946 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
950 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
963 if (use_ll_privacy(hdev))
969 if (rpa_valid(hdev))
972 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
974 bt_dev_err(hdev, "failed to generate new RPA");
978 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
1003 if (bacmp(&hdev->bdaddr, &nrpa))
1009 return hci_set_random_addr_sync(hdev, &nrpa);
1021 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
1022 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
1023 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
1024 bacmp(&hdev->static_addr, BDADDR_ANY))) {
1026 if (bacmp(&hdev->static_addr, &hdev->random_addr))
1027 return hci_set_random_addr_sync(hdev,
1028 &hdev->static_addr);
1040 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1051 adv = hci_find_adv_instance(hdev, instance);
1073 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1077 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1085 * hdev->random_addr to track its address so whenever it needs
1087 * hdev->random_addr is shared with scan state machine.
1089 err = hci_set_random_addr_sync(hdev, random_addr);
1099 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1103 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1115 adv = hci_find_adv_instance(hdev, instance);
1127 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1132 flags = hci_adv_instance_flags(hdev, instance);
1138 mgmt_get_connectable(hdev);
1140 if (!is_advertising_allowed(hdev, connectable))
1147 err = hci_get_random_address(hdev, !connectable,
1148 adv_use_rpa(hdev, flags), adv,
1160 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1161 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1172 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1193 hci_copy_identity_address(hdev, &cp.peer_addr,
1197 cp.channel_map = hdev->le_adv_channel_map;
1212 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1225 if (!bacmp(&random_addr, &hdev->random_addr))
1229 return hci_set_adv_set_random_addr_sync(hdev, instance,
1236 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1249 adv = hci_find_adv_instance(hdev, instance);
1254 len = eir_create_scan_rsp(hdev, instance, pdu.data);
1261 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1270 memcpy(hdev->scan_rsp_data, pdu.data, len);
1271 hdev->scan_rsp_data_len = len;
1277 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1284 len = eir_create_scan_rsp(hdev, instance, cp.data);
1286 if (hdev->scan_rsp_data_len == len &&
1287 !memcmp(cp.data, hdev->scan_rsp_data, len))
1290 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1291 hdev->scan_rsp_data_len = len;
1295 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1299 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1301 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1304 if (ext_adv_capable(hdev))
1305 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1307 return __hci_set_scan_rsp_data_sync(hdev, instance);
1310 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1318 adv = hci_find_adv_instance(hdev, instance);
1350 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1356 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1360 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1364 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1368 return hci_enable_ext_advertising_sync(hdev, instance);
1371 int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1377 adv = hci_find_adv_instance(hdev, instance);
1386 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1390 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1408 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1412 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1423 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1429 len = eir_create_per_adv_data(hdev, instance, pdu.data);
1435 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1440 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1446 adv = hci_find_adv_instance(hdev, instance);
1455 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1462 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1492 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1494 return hci_update_adv_data_sync(hdev, adv->instance);
1497 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1505 hci_disable_per_advertising_sync(hdev, instance);
1508 adv = hci_find_adv_instance(hdev, instance);
1511 adv = hci_add_per_instance(hdev, instance, flags,
1523 err = hci_start_ext_adv_sync(hdev, instance);
1527 err = hci_adv_bcast_annoucement(hdev, adv);
1531 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1536 err = hci_set_per_adv_data_sync(hdev, instance);
1540 err = hci_enable_per_advertising_sync(hdev, instance);
1548 hci_remove_adv_instance(hdev, instance);
1553 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1557 if (ext_adv_capable(hdev))
1558 return hci_start_ext_adv_sync(hdev, instance);
1560 err = hci_update_adv_data_sync(hdev, instance);
1564 err = hci_update_scan_rsp_data_sync(hdev, instance);
1568 return hci_enable_advertising_sync(hdev);
1571 int hci_enable_advertising_sync(struct hci_dev *hdev)
1581 if (ext_adv_capable(hdev))
1582 return hci_enable_ext_advertising_sync(hdev,
1583 hdev->cur_adv_instance);
1585 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1586 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1592 mgmt_get_connectable(hdev);
1594 if (!is_advertising_allowed(hdev, connectable))
1597 status = hci_disable_advertising_sync(hdev);
1606 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1612 status = hci_update_random_address_sync(hdev, !connectable,
1613 adv_use_rpa(hdev, flags),
1624 adv_min_interval = hdev->le_adv_min_interval;
1625 adv_max_interval = hdev->le_adv_max_interval;
1631 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1636 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1637 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1646 cp.channel_map = hdev->le_adv_channel_map;
1648 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1653 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1657 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1659 return hci_enable_advertising_sync(hdev);
1662 int hci_enable_advertising(struct hci_dev *hdev)
1664 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1665 list_empty(&hdev->adv_instances))
1668 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1671 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1676 if (!ext_adv_capable(hdev))
1679 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1684 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1687 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1692 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1700 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1703 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1708 adv = hci_find_adv_instance(hdev, instance);
1713 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1716 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1724 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1728 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1741 adv = hci_find_adv_instance(hdev, instance);
1746 len = eir_create_adv_data(hdev, instance, pdu.data);
1753 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1763 memcpy(hdev->adv_data, pdu.data, len);
1764 hdev->adv_data_len = len;
1770 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1777 len = eir_create_adv_data(hdev, instance, cp.data);
1780 if (hdev->adv_data_len == len &&
1781 memcmp(cp.data, hdev->adv_data, len) == 0)
1784 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1785 hdev->adv_data_len = len;
1789 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1793 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1795 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1798 if (ext_adv_capable(hdev))
1799 return hci_set_ext_adv_data_sync(hdev, instance);
1801 return hci_set_adv_data_sync(hdev, instance);
1804 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1810 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1813 if (hdev->adv_instance_timeout)
1816 adv = hci_find_adv_instance(hdev, instance);
1840 if (!ext_adv_capable(hdev)) {
1841 hdev->adv_instance_timeout = timeout;
1842 queue_delayed_work(hdev->req_workqueue,
1843 &hdev->adv_instance_expire,
1851 if (!force && hdev->cur_adv_instance == instance &&
1852 hci_dev_test_flag(hdev, HCI_LE_ADV))
1855 hdev->cur_adv_instance = instance;
1857 return hci_start_adv_sync(hdev, instance);
1860 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1864 if (!ext_adv_capable(hdev))
1868 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1872 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1876 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1881 if (ext_adv_capable(hdev))
1883 err = hci_clear_adv_sets_sync(hdev, sk);
1884 if (ext_adv_capable(hdev))
1890 hci_dev_lock(hdev);
1893 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1900 err = hci_remove_adv_instance(hdev, instance);
1902 mgmt_advertising_removed(sk, hdev, instance);
1905 hci_dev_unlock(hdev);
1910 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1916 if (ext_adv_capable(hdev))
1917 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1918 if (ext_adv_capable(hdev))
1924 hci_dev_lock(hdev);
1926 err = hci_remove_adv_instance(hdev, instance);
1928 mgmt_advertising_removed(sk, hdev, instance);
1930 hci_dev_unlock(hdev);
1946 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1953 if (!instance || hdev->cur_adv_instance == instance)
1954 cancel_adv_timeout(hdev);
1960 if (hdev->cur_adv_instance == instance)
1961 next = hci_get_next_instance(hdev, instance);
1964 err = hci_clear_adv_sync(hdev, sk, force);
1968 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1975 err = hci_remove_adv_sync(hdev, instance, sk);
1981 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1984 if (next && !ext_adv_capable(hdev))
1985 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1990 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1995 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1999 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
2001 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
2005 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
2011 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
2015 int hci_disable_advertising_sync(struct hci_dev *hdev)
2021 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
2024 if (ext_adv_capable(hdev))
2025 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
2026 if (ext_adv_capable(hdev))
2029 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
2033 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
2041 if (hci_dev_test_flag(hdev, HCI_MESH))
2046 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
2050 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2055 if (use_ext_scan(hdev))
2056 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2061 if (val && hci_dev_test_flag(hdev, HCI_MESH))
2066 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2070 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2072 if (!use_ll_privacy(hdev))
2076 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2079 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2083 static int hci_scan_disable_sync(struct hci_dev *hdev)
2088 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2091 if (hdev->scanning_paused) {
2092 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2096 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2098 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2105 static bool scan_use_rpa(struct hci_dev *hdev)
2107 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2110 static void hci_start_interleave_scan(struct hci_dev *hdev)
2112 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2113 queue_delayed_work(hdev->req_workqueue,
2114 &hdev->interleave_scan, 0);
2117 static bool is_interleave_scanning(struct hci_dev *hdev)
2119 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2122 static void cancel_interleave_scan(struct hci_dev *hdev)
2124 bt_dev_dbg(hdev, "cancelling interleave scan");
2126 cancel_delayed_work_sync(&hdev->interleave_scan);
2128 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2134 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2143 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2144 !(list_empty(&hdev->pend_le_conns) &&
2145 list_empty(&hdev->pend_le_reports)) &&
2146 hci_get_adv_monitor_offload_ext(hdev) ==
2148 bool is_interleaving = is_interleave_scanning(hdev);
2151 hci_start_interleave_scan(hdev);
2152 bt_dev_dbg(hdev, "starting interleave scan");
2157 cancel_interleave_scan(hdev);
2163 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2169 if (!use_ll_privacy(hdev))
2173 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2181 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2185 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2192 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2201 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2203 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2206 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2210 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2224 * Setting params to NULL programs local hdev->irk
2226 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2234 if (!use_ll_privacy(hdev))
2241 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2244 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2245 memcpy(cp.peer_irk, hdev->irk, 16);
2249 irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
2254 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2268 p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2271 p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2278 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2279 memcpy(cp.local_irk, hdev->irk, 16);
2283 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2288 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2305 irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
2316 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2324 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2332 if (hdev->suspended &&
2334 hci_le_del_accept_list_sync(hdev, &params->addr,
2340 if (*num_entries >= hdev->le_accept_list_size)
2344 if (!use_ll_privacy(hdev) &&
2345 hci_find_irk_by_addr(hdev, &params->addr, params->addr_type))
2352 err = hci_le_add_resolve_list_sync(hdev, params);
2354 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2359 err = hci_le_set_privacy_mode_sync(hdev, params);
2361 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2366 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
2374 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2377 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2379 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2383 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2390 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2396 if (hdev->advertising_paused)
2399 bt_dev_dbg(hdev, "Pausing directed advertising");
2402 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2409 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2410 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2411 hdev->discov_timeout = 0;
2414 bt_dev_dbg(hdev, "Pausing advertising instances");
2419 err = hci_disable_advertising_sync(hdev);
2424 if (!ext_adv_capable(hdev))
2425 cancel_adv_timeout(hdev);
2427 hdev->advertising_paused = true;
2428 hdev->advertising_old_state = old_state;
2434 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2440 if (!hdev->advertising_paused)
2444 hdev->advertising_paused = false;
2445 if (hdev->advertising_old_state) {
2446 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2447 hdev->advertising_old_state = 0;
2450 bt_dev_dbg(hdev, "Resuming advertising instances");
2452 if (ext_adv_capable(hdev)) {
2454 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2455 err = hci_enable_ext_advertising_sync(hdev,
2461 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2468 err = hci_schedule_adv_instance_sync(hdev,
2469 hdev->cur_adv_instance,
2473 hdev->advertising_paused = false;
2478 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2482 if (!use_ll_privacy(hdev))
2485 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2491 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2492 hci_lookup_le_connect(hdev)) {
2493 bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2498 err = hci_pause_advertising_sync(hdev);
2500 bt_dev_err(hdev, "Pause advertising failed: %d", err);
2504 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2506 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2510 if (!err && scan_use_rpa(hdev))
2513 hci_resume_advertising_sync(hdev);
2517 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2523 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2553 /* No hdev->lock, but: addr, addr_type are immutable.
2573 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
2575 if (!(hdev->commands[26] & 0x80))
2578 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
2599 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2612 if (use_ll_privacy(hdev)) {
2613 err = hci_pause_advertising_sync(hdev);
2615 bt_dev_err(hdev, "pause advertising failed: %d", err);
2624 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2626 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2631 if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2634 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_PA_CREATE_SYNC);
2646 hci_le_clear_accept_list_sync(hdev);
2649 err = hci_le_add_accept_list_sync(hdev, &pa,
2661 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2662 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2666 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2669 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2677 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2699 params = conn_params_copy(&hdev->pend_le_conns, &n);
2706 err = hci_le_add_accept_list_sync(hdev, &params[i],
2721 params = conn_params_copy(&hdev->pend_le_reports, &n);
2728 err = hci_le_add_accept_list_sync(hdev, &params[i],
2743 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2744 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2745 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2752 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2754 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2757 if (use_ll_privacy(hdev))
2758 hci_resume_advertising_sync(hdev);
2772 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2792 if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2795 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
2799 conn = hci_conn_hash_lookup_ba(hdev, ISO_LINK,
2829 if (scan_1m(hdev) || scan_2m(hdev)) {
2836 if (scan_coded(hdev)) {
2847 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2852 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2858 if (use_ext_scan(hdev))
2859 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2870 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2874 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2880 if (hdev->scanning_paused) {
2881 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2885 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2890 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2893 static int hci_passive_scan_sync(struct hci_dev *hdev)
2901 if (hdev->scanning_paused) {
2902 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2906 err = hci_scan_disable_sync(hdev);
2908 bt_dev_err(hdev, "disable scanning failed: %d", err);
2918 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2922 if (hdev->enable_advmon_interleave_scan &&
2923 hci_update_interleaved_scan_sync(hdev))
2926 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2932 filter_policy = hci_update_accept_list_sync(hdev);
2943 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2944 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2947 if (hdev->suspended) {
2948 window = hdev->le_scan_window_suspend;
2949 interval = hdev->le_scan_int_suspend;
2950 } else if (hci_is_le_conn_scanning(hdev)) {
2951 window = hdev->le_scan_window_connect;
2952 interval = hdev->le_scan_int_connect;
2953 } else if (hci_is_adv_monitoring(hdev)) {
2954 window = hdev->le_scan_window_adv_monitor;
2955 interval = hdev->le_scan_int_adv_monitor;
2957 window = hdev->le_scan_window;
2958 interval = hdev->le_scan_interval;
2962 if (hci_dev_test_flag(hdev, HCI_MESH)) {
2967 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2969 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2973 /* This function controls the passive scanning based on hdev->pend_le_conns
2988 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2992 if (!test_bit(HCI_UP, &hdev->flags) ||
2993 test_bit(HCI_INIT, &hdev->flags) ||
2994 hci_dev_test_flag(hdev, HCI_SETUP) ||
2995 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2996 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2997 hci_dev_test_flag(hdev, HCI_UNREGISTER))
3001 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3005 if (hdev->discovery.state != DISCOVERY_STOPPED)
3015 hci_discovery_filter_clear(hdev);
3017 bt_dev_dbg(hdev, "ADV monitoring is %s",
3018 hci_is_adv_monitoring(hdev) ? "on" : "off");
3020 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
3021 list_empty(&hdev->pend_le_conns) &&
3022 list_empty(&hdev->pend_le_reports) &&
3023 !hci_is_adv_monitoring(hdev) &&
3024 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
3030 bt_dev_dbg(hdev, "stopping background scanning");
3032 err = hci_scan_disable_sync(hdev);
3034 bt_dev_err(hdev, "stop background scanning failed: %d",
3045 if (hci_lookup_le_connect(hdev))
3048 bt_dev_dbg(hdev, "start background scanning");
3050 err = hci_passive_scan_sync(hdev);
3052 bt_dev_err(hdev, "start background scanning failed: %d",
3059 static int update_scan_sync(struct hci_dev *hdev, void *data)
3061 return hci_update_scan_sync(hdev);
3064 int hci_update_scan(struct hci_dev *hdev)
3066 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
3069 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
3071 return hci_update_passive_scan_sync(hdev);
3074 int hci_update_passive_scan(struct hci_dev *hdev)
3077 if (!test_bit(HCI_UP, &hdev->flags) ||
3078 test_bit(HCI_INIT, &hdev->flags) ||
3079 hci_dev_test_flag(hdev, HCI_SETUP) ||
3080 hci_dev_test_flag(hdev, HCI_CONFIG) ||
3081 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
3082 hci_dev_test_flag(hdev, HCI_UNREGISTER))
3085 return hci_cmd_sync_queue_once(hdev, update_passive_scan_sync, NULL,
3089 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
3093 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
3096 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3101 hdev->features[1][0] |= LMP_HOST_SC;
3102 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
3104 hdev->features[1][0] &= ~LMP_HOST_SC;
3105 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
3112 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
3116 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3117 lmp_host_ssp_capable(hdev))
3120 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
3121 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
3125 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3130 return hci_write_sc_support_sync(hdev, 0x01);
3133 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
3137 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
3138 !lmp_bredr_capable(hdev))
3144 if (le == lmp_host_le_capable(hdev) &&
3145 simul == lmp_host_le_br_capable(hdev))
3153 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3157 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3162 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3170 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3171 hci_le_add_resolve_list_sync(hdev, NULL);
3172 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3179 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3180 list_empty(&hdev->adv_instances)) {
3181 if (ext_adv_capable(hdev)) {
3182 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3184 hci_update_scan_rsp_data_sync(hdev, 0x00);
3186 err = hci_update_adv_data_sync(hdev, 0x00);
3188 hci_update_scan_rsp_data_sync(hdev, 0x00);
3191 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3192 hci_enable_advertising_sync(hdev);
3196 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3197 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3202 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3206 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3207 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3210 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3215 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3221 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3224 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3235 type = hdev->def_page_scan_type;
3236 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3239 cp.window = cpu_to_le16(hdev->def_page_scan_window);
3241 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3242 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3243 err = __hci_cmd_sync_status(hdev,
3250 if (hdev->page_scan_type != type)
3251 err = __hci_cmd_sync_status(hdev,
3259 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3263 list_for_each_entry(b, &hdev->accept_list, list) {
3266 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3277 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3279 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3284 int hci_update_scan_sync(struct hci_dev *hdev)
3288 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3291 if (!hdev_is_powered(hdev))
3294 if (mgmt_powering_down(hdev))
3297 if (hdev->scanning_paused)
3300 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3301 disconnected_accept_list_entries(hdev))
3306 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3309 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3310 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3313 return hci_write_scan_enable_sync(hdev, scan);
3316 int hci_update_name_sync(struct hci_dev *hdev)
3322 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3324 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3341 int hci_powered_update_sync(struct hci_dev *hdev)
3350 smp_register(hdev);
3352 err = hci_write_ssp_mode_sync(hdev, 0x01);
3356 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3360 err = hci_powered_update_adv_sync(hdev);
3364 err = hci_write_auth_enable_sync(hdev);
3368 if (lmp_bredr_capable(hdev)) {
3369 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3370 hci_write_fast_connectable_sync(hdev, true);
3372 hci_write_fast_connectable_sync(hdev, false);
3373 hci_update_scan_sync(hdev);
3374 hci_update_class_sync(hdev);
3375 hci_update_name_sync(hdev);
3376 hci_update_eir_sync(hdev);
3388 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3389 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3390 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3391 if (bacmp(&hdev->static_addr, BDADDR_ANY))
3392 return hci_set_random_addr_sync(hdev,
3393 &hdev->static_addr);
3403 * @hdev: The HCI device
3411 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3413 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3422 if (test_bit(HCI_QUIRK_BDADDR_PROPERTY_BROKEN, &hdev->quirks))
3423 baswap(&hdev->public_addr, &ba);
3425 bacpy(&hdev->public_addr, &ba);
3429 int (*func)(struct hci_dev *hdev);
3433 static int hci_init_stage_sync(struct hci_dev *hdev,
3441 err = stage[i].func(hdev);
3450 static int hci_read_local_version_sync(struct hci_dev *hdev)
3452 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3457 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3459 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3476 int hci_reset_sync(struct hci_dev *hdev)
3480 set_bit(HCI_RESET, &hdev->flags);
3482 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3490 static int hci_init0_sync(struct hci_dev *hdev)
3494 bt_dev_dbg(hdev, "");
3497 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3498 err = hci_reset_sync(hdev);
3503 return hci_init_stage_sync(hdev, hci_init0);
3506 static int hci_unconf_init_sync(struct hci_dev *hdev)
3510 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3513 err = hci_init0_sync(hdev);
3517 if (hci_dev_test_flag(hdev, HCI_SETUP))
3518 hci_debugfs_create_basic(hdev);
3524 static int hci_read_local_features_sync(struct hci_dev *hdev)
3527 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
3530 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3546 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3556 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3557 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3558 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3565 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
3567 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
3572 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
3574 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
3579 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
3581 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
3586 static int hci_read_location_data_sync(struct hci_dev *hdev)
3588 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
3609 static int hci_init1_sync(struct hci_dev *hdev)
3613 bt_dev_dbg(hdev, "");
3616 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3617 err = hci_reset_sync(hdev);
3622 switch (hdev->dev_type) {
3624 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3625 return hci_init_stage_sync(hdev, br_init1);
3627 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3628 return hci_init_stage_sync(hdev, amp_init1);
3630 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
3645 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3647 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3652 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3654 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3659 static int hci_read_local_name_sync(struct hci_dev *hdev)
3661 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3666 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3668 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3673 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3675 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3680 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3682 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3686 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3692 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3695 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3707 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3713 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3715 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3722 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3725 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3730 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3734 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3759 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3763 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3772 hdev->max_page = 0x01;
3774 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3778 static int hci_write_eir_sync(struct hci_dev *hdev)
3782 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3785 memset(hdev->eir, 0, sizeof(hdev->eir));
3788 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3792 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3796 if (!lmp_inq_rssi_capable(hdev) &&
3797 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3804 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3806 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3810 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3812 if (!lmp_inq_tx_pwr_capable(hdev))
3815 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3819 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3823 if (!lmp_ext_feat_capable(hdev))
3829 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3833 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3835 return hci_read_local_ext_features_sync(hdev, 0x01);
3858 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3861 if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3862 return __hci_cmd_sync_status(hdev,
3866 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3871 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3873 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3878 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3880 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3895 static int hci_init2_sync(struct hci_dev *hdev)
3899 bt_dev_dbg(hdev, "");
3901 if (hdev->dev_type == HCI_AMP)
3902 return hci_init_stage_sync(hdev, amp_init2);
3904 err = hci_init_stage_sync(hdev, hci_init2);
3908 if (lmp_bredr_capable(hdev)) {
3909 err = hci_init_stage_sync(hdev, br_init2);
3913 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3916 if (lmp_le_capable(hdev)) {
3917 err = hci_init_stage_sync(hdev, le_init2);
3921 if (!lmp_bredr_capable(hdev))
3922 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3928 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3939 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3942 if (lmp_bredr_capable(hdev)) {
3949 if (hdev->suspended) {
3964 if (hdev->commands[0] & 0x20) {
3969 if (!hdev->suspended)
3978 if (hdev->commands[2] & 0x80)
3983 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3989 if (lmp_inq_rssi_capable(hdev) ||
3990 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3993 if (lmp_ext_feat_capable(hdev))
3996 if (lmp_esco_capable(hdev)) {
4001 if (lmp_sniffsubr_capable(hdev))
4004 if (lmp_pause_enc_capable(hdev))
4007 if (lmp_ext_inq_capable(hdev))
4010 if (lmp_no_flush_capable(hdev))
4013 if (lmp_lsto_capable(hdev))
4016 if (lmp_ssp_capable(hdev)) {
4030 if (lmp_le_capable(hdev))
4033 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
4037 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
4041 if (!(hdev->commands[6] & 0x20) ||
4042 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4049 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
4053 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
4058 if (!(hdev->commands[5] & 0x10))
4063 if (lmp_rswitch_capable(hdev))
4065 if (lmp_hold_capable(hdev))
4067 if (lmp_sniff_capable(hdev))
4069 if (lmp_park_capable(hdev))
4074 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
4078 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
4080 if (!(hdev->commands[8] & 0x01))
4083 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
4087 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
4089 if (!(hdev->commands[18] & 0x04) ||
4090 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4091 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4094 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
4098 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
4104 if (!(hdev->commands[13] & 0x01))
4107 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
4112 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
4117 if (!lmp_ext_feat_capable(hdev))
4120 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
4122 err = hci_read_local_ext_features_sync(hdev, page);
4149 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4153 if (!lmp_le_capable(hdev))
4158 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4164 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4171 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4177 if (use_enhanced_conn_complete(hdev))
4183 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4189 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4195 if (hdev->commands[26] & 0x08)
4201 if (hdev->commands[26] & 0x10)
4207 if (hdev->commands[27] & 0x04)
4213 if (hdev->commands[27] & 0x20)
4220 if (hdev->commands[34] & 0x02)
4227 if (hdev->commands[34] & 0x04)
4233 if (hdev->commands[35] & (0x20 | 0x40))
4240 if (use_ext_scan(hdev))
4246 if (ext_adv_capable(hdev))
4249 if (cis_capable(hdev)) {
4251 if (cis_peripheral_capable(hdev))
4255 if (bis_capable(hdev)) {
4265 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4270 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4272 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4279 return __hci_cmd_sync_status(hdev,
4288 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4290 if (!(hdev->commands[38] & 0x80) ||
4291 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4294 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4299 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4301 if (!(hdev->commands[26] & 0x40))
4304 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4309 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4311 if (!(hdev->commands[34] & 0x40))
4314 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4319 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4321 if (!(hdev->commands[34] & 0x20))
4324 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4329 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4331 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4333 if (!(hdev->commands[35] & 0x04) ||
4334 test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4337 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4343 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4345 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4348 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4353 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4355 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4358 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4363 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4365 if (!ext_adv_capable(hdev))
4368 return __hci_cmd_sync_status(hdev,
4374 static int hci_set_le_support_sync(struct hci_dev *hdev)
4379 if (!lmp_bredr_capable(hdev))
4384 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4389 if (cp.le == lmp_host_le_capable(hdev))
4392 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4397 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4401 if (!cis_capable(hdev))
4410 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4445 static int hci_init3_sync(struct hci_dev *hdev)
4449 bt_dev_dbg(hdev, "");
4451 err = hci_init_stage_sync(hdev, hci_init3);
4455 if (lmp_le_capable(hdev))
4456 return hci_init_stage_sync(hdev, le_init3);
4461 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4478 if (!(hdev->commands[6] & 0x80) ||
4479 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4486 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4490 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4496 if (!(hdev->commands[22] & 0x04))
4502 if (lmp_cpb_central_capable(hdev)) {
4513 if (lmp_cpb_peripheral_capable(hdev)) {
4522 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4536 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4541 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4543 if (hdev->commands[45] & 0x04)
4544 hci_read_supported_codecs_v2(hdev);
4545 else if (hdev->commands[29] & 0x20)
4546 hci_read_supported_codecs(hdev);
4552 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4554 if (!(hdev->commands[41] & 0x08))
4557 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4562 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4564 if (!mws_transport_config_capable(hdev))
4567 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4572 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4574 if (!lmp_sync_train_capable(hdev))
4577 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4582 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4586 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4587 !bredr_sc_enabled(hdev))
4590 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4598 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4601 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4603 if (!(hdev->commands[18] & 0x08) ||
4604 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4605 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4608 if (enabled == hdev->err_data_reporting)
4615 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4640 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4644 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4648 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4649 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4651 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4658 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4662 if (!(hdev->commands[35] & 0x20)) {
4666 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4667 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4677 if (le_2m_capable(hdev)) {
4683 if (le_coded_capable(hdev)) {
4688 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4700 static int hci_init4_sync(struct hci_dev *hdev)
4704 bt_dev_dbg(hdev, "");
4706 err = hci_init_stage_sync(hdev, hci_init4);
4710 if (lmp_le_capable(hdev))
4711 return hci_init_stage_sync(hdev, le_init4);
4716 static int hci_init_sync(struct hci_dev *hdev)
4720 err = hci_init1_sync(hdev);
4724 if (hci_dev_test_flag(hdev, HCI_SETUP))
4725 hci_debugfs_create_basic(hdev);
4727 err = hci_init2_sync(hdev);
4735 if (hdev->dev_type != HCI_PRIMARY)
4738 err = hci_init3_sync(hdev);
4742 err = hci_init4_sync(hdev);
4758 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4759 !hci_dev_test_flag(hdev, HCI_CONFIG))
4762 if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4765 hci_debugfs_create_common(hdev);
4767 if (lmp_bredr_capable(hdev))
4768 hci_debugfs_create_bredr(hdev);
4770 if (lmp_le_capable(hdev))
4771 hci_debugfs_create_le(hdev);
4806 /* This function handles hdev setup stage:
4808 * Calls hdev->setup
4811 static int hci_dev_setup_sync(struct hci_dev *hdev)
4817 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4818 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4821 bt_dev_dbg(hdev, "");
4823 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4825 if (hdev->setup)
4826 ret = hdev->setup(hdev);
4829 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4830 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4837 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
4838 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
4840 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4841 !bacmp(&hdev->public_addr, BDADDR_ANY))
4842 hci_dev_get_bd_addr_from_property(hdev);
4844 if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
4845 hdev->set_bdaddr) {
4846 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4862 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4864 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4874 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4875 return hci_unconf_init_sync(hdev);
4880 /* This function handles hdev init stage:
4885 static int hci_dev_init_sync(struct hci_dev *hdev)
4889 bt_dev_dbg(hdev, "");
4891 atomic_set(&hdev->cmd_cnt, 1);
4892 set_bit(HCI_INIT, &hdev->flags);
4894 ret = hci_dev_setup_sync(hdev);
4896 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4902 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4903 hdev->set_bdaddr)
4904 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4910 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4911 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4912 ret = hci_init_sync(hdev);
4913 if (!ret && hdev->post_init)
4914 ret = hdev->post_init(hdev);
4922 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4923 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4924 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4925 ret = hdev->set_diag(hdev, true);
4927 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4928 msft_do_open(hdev);
4929 aosp_do_open(hdev);
4932 clear_bit(HCI_INIT, &hdev->flags);
4937 int hci_dev_open_sync(struct hci_dev *hdev)
4941 bt_dev_dbg(hdev, "");
4943 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4948 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4949 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4953 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4970 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4971 hdev->dev_type == HCI_PRIMARY &&
4972 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4973 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4979 if (test_bit(HCI_UP, &hdev->flags)) {
4984 if (hdev->open(hdev)) {
4989 hci_devcd_reset(hdev);
4991 set_bit(HCI_RUNNING, &hdev->flags);
4992 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4994 ret = hci_dev_init_sync(hdev);
4996 hci_dev_hold(hdev);
4997 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4998 hci_adv_instances_set_rpa_expired(hdev, true);
4999 set_bit(HCI_UP, &hdev->flags);
5000 hci_sock_dev_event(hdev, HCI_DEV_UP);
5001 hci_leds_update_powered(hdev, true);
5002 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
5003 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
5004 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
5005 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5006 hci_dev_test_flag(hdev, HCI_MGMT) &&
5007 hdev->dev_type == HCI_PRIMARY) {
5008 ret = hci_powered_update_sync(hdev);
5009 mgmt_power_on(hdev, ret);
5013 flush_work(&hdev->tx_work);
5019 flush_work(&hdev->rx_work);
5020 flush_work(&hdev->cmd_work);
5022 skb_queue_purge(&hdev->cmd_q);
5023 skb_queue_purge(&hdev->rx_q);
5025 if (hdev->flush)
5026 hdev->flush(hdev);
5028 if (hdev->sent_cmd) {
5029 cancel_delayed_work_sync(&hdev->cmd_timer);
5030 kfree_skb(hdev->sent_cmd);
5031 hdev->sent_cmd = NULL;
5034 if (hdev->req_skb) {
5035 kfree_skb(hdev->req_skb);
5036 hdev->req_skb = NULL;
5039 clear_bit(HCI_RUNNING, &hdev->flags);
5040 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5042 hdev->close(hdev);
5043 hdev->flags &= BIT(HCI_RAW);
5050 /* This function requires the caller holds hdev->lock */
5051 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
5055 list_for_each_entry(p, &hdev->le_conn_params, list) {
5067 static int hci_dev_shutdown(struct hci_dev *hdev)
5077 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
5079 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
5080 test_bit(HCI_UP, &hdev->flags)) {
5082 if (hdev->shutdown)
5083 err = hdev->shutdown(hdev);
5087 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
5092 int hci_dev_close_sync(struct hci_dev *hdev)
5097 bt_dev_dbg(hdev, "");
5099 cancel_delayed_work(&hdev->power_off);
5100 cancel_delayed_work(&hdev->ncmd_timer);
5101 cancel_delayed_work(&hdev->le_scan_disable);
5103 hci_request_cancel_all(hdev);
5105 if (hdev->adv_instance_timeout) {
5106 cancel_delayed_work_sync(&hdev->adv_instance_expire);
5107 hdev->adv_instance_timeout = 0;
5110 err = hci_dev_shutdown(hdev);
5112 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
5113 cancel_delayed_work_sync(&hdev->cmd_timer);
5117 hci_leds_update_powered(hdev, false);
5120 flush_work(&hdev->tx_work);
5121 flush_work(&hdev->rx_work);
5123 if (hdev->discov_timeout > 0) {
5124 hdev->discov_timeout = 0;
5125 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5126 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
5129 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
5130 cancel_delayed_work(&hdev->service_cache);
5132 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
5135 cancel_delayed_work_sync(&hdev->rpa_expired);
5137 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5144 drain_workqueue(hdev->workqueue);
5146 hci_dev_lock(hdev);
5148 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5150 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5152 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
5153 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5154 hci_dev_test_flag(hdev, HCI_MGMT))
5155 __mgmt_power_off(hdev);
5157 hci_inquiry_cache_flush(hdev);
5158 hci_pend_le_actions_clear(hdev);
5159 hci_conn_hash_flush(hdev);
5160 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5161 smp_unregister(hdev);
5162 hci_dev_unlock(hdev);
5164 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5166 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5167 aosp_do_close(hdev);
5168 msft_do_close(hdev);
5171 if (hdev->flush)
5172 hdev->flush(hdev);
5175 skb_queue_purge(&hdev->cmd_q);
5176 atomic_set(&hdev->cmd_cnt, 1);
5177 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5178 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5179 set_bit(HCI_INIT, &hdev->flags);
5180 hci_reset_sync(hdev);
5181 clear_bit(HCI_INIT, &hdev->flags);
5185 flush_work(&hdev->cmd_work);
5188 skb_queue_purge(&hdev->rx_q);
5189 skb_queue_purge(&hdev->cmd_q);
5190 skb_queue_purge(&hdev->raw_q);
5193 if (hdev->sent_cmd) {
5194 cancel_delayed_work_sync(&hdev->cmd_timer);
5195 kfree_skb(hdev->sent_cmd);
5196 hdev->sent_cmd = NULL;
5200 if (hdev->req_skb) {
5201 kfree_skb(hdev->req_skb);
5202 hdev->req_skb = NULL;
5205 clear_bit(HCI_RUNNING, &hdev->flags);
5206 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5209 hdev->close(hdev);
5212 hdev->flags &= BIT(HCI_RAW);
5213 hci_dev_clear_volatile_flags(hdev);
5216 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
5218 memset(hdev->eir, 0, sizeof(hdev->eir));
5219 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5220 bacpy(&hdev->random_addr, BDADDR_ANY);
5221 hci_codec_list_clear(&hdev->local_codecs);
5223 hci_dev_put(hdev);
5233 static int hci_power_on_sync(struct hci_dev *hdev)
5237 if (test_bit(HCI_UP, &hdev->flags) &&
5238 hci_dev_test_flag(hdev, HCI_MGMT) &&
5239 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5240 cancel_delayed_work(&hdev->power_off);
5241 return hci_powered_update_sync(hdev);
5244 err = hci_dev_open_sync(hdev);
5252 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5253 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5254 (hdev->dev_type == HCI_PRIMARY &&
5255 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5256 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5257 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5258 hci_dev_close_sync(hdev);
5259 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5260 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5264 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5268 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5269 set_bit(HCI_RAW, &hdev->flags);
5278 mgmt_index_added(hdev);
5279 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5283 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5284 clear_bit(HCI_RAW, &hdev->flags);
5290 mgmt_index_added(hdev);
5296 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5303 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5307 int hci_stop_discovery_sync(struct hci_dev *hdev)
5309 struct discovery_state *d = &hdev->discovery;
5313 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5316 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5317 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5323 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5324 cancel_delayed_work(&hdev->le_scan_disable);
5326 err = hci_scan_disable_sync(hdev);
5332 err = hci_scan_disable_sync(hdev);
5338 if (use_ll_privacy(hdev))
5339 hci_resume_advertising_sync(hdev);
5346 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5351 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5357 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
5366 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
5370 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5376 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
5382 hci_dev_lock(hdev);
5384 hci_dev_unlock(hdev);
5399 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5404 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5408 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5421 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5425 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5429 return hci_le_connect_cancel_sync(hdev, conn, reason);
5441 return hci_disconnect_sync(hdev, conn, reason);
5453 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5462 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5467 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5471 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5486 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5490 static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
5499 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
5503 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5509 return hci_le_reject_cis_sync(hdev, conn, reason);
5512 return hci_reject_sco_sync(hdev, conn, reason);
5518 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5522 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5532 err = hci_disconnect_sync(hdev, conn, reason);
5535 err = hci_connect_cancel_sync(hdev, conn, reason);
5538 err = hci_reject_conn_sync(hdev, conn, reason);
5548 hci_dev_lock(hdev);
5551 c = hci_conn_hash_lookup_handle(hdev, handle);
5571 hci_dev_unlock(hdev);
5575 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5577 struct list_head *head = &hdev->conn_hash.list;
5590 hci_abort_conn_sync(hdev, conn, reason);
5606 static int hci_power_off_sync(struct hci_dev *hdev)
5611 if (!test_bit(HCI_UP, &hdev->flags))
5614 hci_dev_set_flag(hdev, HCI_POWERING_DOWN);
5616 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5617 test_bit(HCI_PSCAN, &hdev->flags)) {
5618 err = hci_write_scan_enable_sync(hdev, 0x00);
5623 err = hci_clear_adv_sync(hdev, NULL, false);
5627 err = hci_stop_discovery_sync(hdev);
5632 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5636 err = hci_dev_close_sync(hdev);
5639 hci_dev_clear_flag(hdev, HCI_POWERING_DOWN);
5643 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5646 return hci_power_on_sync(hdev);
5648 return hci_power_off_sync(hdev);
5651 static int hci_write_iac_sync(struct hci_dev *hdev)
5655 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5660 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5662 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5677 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5682 int hci_update_discoverable_sync(struct hci_dev *hdev)
5686 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5687 err = hci_write_iac_sync(hdev);
5691 err = hci_update_scan_sync(hdev);
5695 err = hci_update_class_sync(hdev);
5703 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5704 err = hci_update_adv_data_sync(hdev, 0x00);
5711 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5712 if (ext_adv_capable(hdev))
5713 err = hci_start_ext_adv_sync(hdev, 0x00);
5715 err = hci_enable_advertising_sync(hdev);
5722 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5724 return hci_update_discoverable_sync(hdev);
5727 int hci_update_discoverable(struct hci_dev *hdev)
5730 if (hdev_is_powered(hdev) &&
5731 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5732 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5733 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5734 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5740 int hci_update_connectable_sync(struct hci_dev *hdev)
5744 err = hci_update_scan_sync(hdev);
5752 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5753 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5756 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5757 !list_empty(&hdev->adv_instances)) {
5758 if (ext_adv_capable(hdev))
5759 err = hci_start_ext_adv_sync(hdev,
5760 hdev->cur_adv_instance);
5762 err = hci_enable_advertising_sync(hdev);
5768 return hci_update_passive_scan_sync(hdev);
5771 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5777 bt_dev_dbg(hdev, "");
5779 if (test_bit(HCI_INQUIRY, &hdev->flags))
5782 hci_dev_lock(hdev);
5783 hci_inquiry_cache_flush(hdev);
5784 hci_dev_unlock(hdev);
5788 if (hdev->discovery.limited)
5795 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5799 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5808 bt_dev_dbg(hdev, "");
5814 err = hci_scan_disable_sync(hdev);
5816 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5820 cancel_interleave_scan(hdev);
5825 err = hci_pause_addr_resolution(hdev);
5833 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5838 if (hci_is_adv_monitoring(hdev) ||
5839 (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5840 hdev->discovery.result_filtering)) {
5854 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5855 hdev->le_scan_window_discovery,
5862 if (use_ll_privacy(hdev))
5863 hci_resume_advertising_sync(hdev);
5866 hci_update_passive_scan_sync(hdev);
5870 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5874 bt_dev_dbg(hdev, "");
5876 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5880 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5883 int hci_start_discovery_sync(struct hci_dev *hdev)
5888 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5890 switch (hdev->discovery.type) {
5892 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5903 &hdev->quirks)) {
5909 err = hci_start_interleaved_discovery_sync(hdev);
5913 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5914 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5918 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5927 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5929 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5934 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5936 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5938 msft_suspend_sync(hdev);
5946 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5948 int old_state = hdev->discovery.state;
5953 hdev->discovery_paused)
5956 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5957 err = hci_stop_discovery_sync(hdev);
5961 hdev->discovery_paused = true;
5962 hdev->discovery_old_state = old_state;
5963 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5968 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5972 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5975 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5981 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5985 hci_clear_event_filter_sync(hdev);
5987 list_for_each_entry(b, &hdev->accept_list, list) {
5991 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5993 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5998 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
6005 hci_write_scan_enable_sync(hdev, scan);
6007 hci_write_scan_enable_sync(hdev, scan);
6013 static int hci_pause_scan_sync(struct hci_dev *hdev)
6015 if (hdev->scanning_paused)
6019 if (test_bit(HCI_PSCAN, &hdev->flags))
6020 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
6022 hci_scan_disable_sync(hdev);
6024 hdev->scanning_paused = true;
6035 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
6042 int hci_suspend_sync(struct hci_dev *hdev)
6047 if (hdev->suspended)
6051 hdev->suspended = true;
6054 hci_pause_discovery_sync(hdev);
6057 hci_pause_advertising_sync(hdev);
6060 hci_suspend_monitor_sync(hdev);
6063 hci_pause_scan_sync(hdev);
6065 if (hci_conn_count(hdev)) {
6067 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
6070 hdev->suspend_state = BT_RUNNING;
6071 hci_resume_sync(hdev);
6078 hci_set_event_mask_sync(hdev);
6084 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
6085 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
6090 hdev->scanning_paused = false;
6093 hci_update_event_filter_sync(hdev);
6096 hci_update_passive_scan_sync(hdev);
6099 hdev->scanning_paused = true;
6101 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
6107 static int hci_resume_discovery_sync(struct hci_dev *hdev)
6112 if (!hdev->discovery_paused)
6115 hdev->discovery_paused = false;
6117 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6119 err = hci_start_discovery_sync(hdev);
6121 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
6127 static void hci_resume_monitor_sync(struct hci_dev *hdev)
6129 switch (hci_get_adv_monitor_offload_ext(hdev)) {
6131 msft_resume_sync(hdev);
6139 static int hci_resume_scan_sync(struct hci_dev *hdev)
6141 if (!hdev->scanning_paused)
6144 hdev->scanning_paused = false;
6146 hci_update_scan_sync(hdev);
6149 hci_update_passive_scan_sync(hdev);
6162 int hci_resume_sync(struct hci_dev *hdev)
6165 if (!hdev->suspended)
6168 hdev->suspended = false;
6171 hci_set_event_mask_sync(hdev);
6174 hci_clear_event_filter_sync(hdev);
6177 hci_resume_scan_sync(hdev);
6180 hci_resume_monitor_sync(hdev);
6183 hci_resume_advertising_sync(hdev);
6186 hci_resume_discovery_sync(hdev);
6193 struct hci_dev *hdev = conn->hdev;
6195 return hci_dev_test_flag(hdev, HCI_PRIVACY);
6198 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
6206 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6214 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
6222 cp.channel_map = hdev->le_adv_channel_map;
6239 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
6243 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
6251 bacmp(&random_addr, &hdev->random_addr)) {
6252 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
6258 return hci_enable_ext_advertising_sync(hdev, 0x00);
6261 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
6269 if (ext_adv_capable(hdev))
6270 return hci_le_ext_directed_advertising_sync(hdev, conn);
6277 hci_dev_clear_flag(hdev, HCI_LE_ADV);
6282 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6300 cp.channel_map = hdev->le_adv_channel_map;
6302 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
6309 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
6316 struct hci_dev *hdev = conn->hdev;
6320 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6321 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6330 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
6349 if (scan_1m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_1M ||
6358 if (scan_2m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_2M ||
6367 if (scan_coded(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_CODED ||
6375 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6381 static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
6389 if (!hci_conn_valid(hdev, conn))
6392 bt_dev_dbg(hdev, "conn %p", conn);
6402 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6403 hdev->le_scan_type == LE_SCAN_ACTIVE &&
6404 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6410 hci_pause_advertising_sync(hdev);
6412 err = hci_le_directed_advertising_sync(hdev, conn);
6417 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6418 hci_pause_advertising_sync(hdev);
6420 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6427 conn->le_conn_min_interval = hdev->le_conn_min_interval;
6428 conn->le_conn_max_interval = hdev->le_conn_max_interval;
6429 conn->le_conn_latency = hdev->le_conn_latency;
6430 conn->le_supv_timeout = hdev->le_supv_timeout;
6439 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6440 hci_scan_disable_sync(hdev);
6441 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6447 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6452 if (use_ext_conn(hdev)) {
6453 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6459 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6460 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6478 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6480 use_enhanced_conn_complete(hdev) ?
6487 hci_le_connect_cancel_sync(hdev, conn, 0x00);
6490 hci_resume_advertising_sync(hdev);
6494 int hci_le_create_cis_sync(struct hci_dev *hdev)
6528 hci_dev_lock(hdev);
6533 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6539 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6547 list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
6563 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6582 hci_dev_unlock(hdev);
6588 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6595 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6602 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6606 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6613 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6617 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6624 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6628 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6643 if (use_ll_privacy(hdev))
6652 if (rpa_valid(hdev))
6656 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6658 bt_dev_err(hdev, "failed to generate new RPA");
6662 bacpy(rand_addr, &hdev->rpa);
6685 if (bacmp(&hdev->bdaddr, &nrpa))
6701 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6705 return hci_update_adv_data_sync(hdev, instance);
6708 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6710 return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6714 static int hci_acl_create_conn_sync(struct hci_dev *hdev, void *data)
6721 if (!hci_conn_valid(hdev, conn))
6732 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
6733 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 0,
6736 bt_dev_warn(hdev, "Failed to cancel inquiry %d", err);
6745 conn->link_policy = hdev->link_policy;
6751 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
6764 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
6769 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN,
6775 int hci_connect_acl_sync(struct hci_dev *hdev, struct hci_conn *conn)
6777 return hci_cmd_sync_queue_once(hdev, hci_acl_create_conn_sync, conn,
6781 static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err)
6785 bt_dev_dbg(hdev, "err %d", err);
6790 hci_dev_lock(hdev);
6792 if (!hci_conn_valid(hdev, conn))
6801 if (conn != hci_lookup_le_connect(hdev))
6809 hci_dev_unlock(hdev);
6812 int hci_connect_le_sync(struct hci_dev *hdev, struct hci_conn *conn)
6814 return hci_cmd_sync_queue_once(hdev, hci_le_create_conn_sync, conn,
6818 int hci_cancel_connect_sync(struct hci_dev *hdev, struct hci_conn *conn)
6825 return !hci_cmd_sync_dequeue_once(hdev,
6829 return !hci_cmd_sync_dequeue_once(hdev, hci_le_create_conn_sync,