Lines Matching defs: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)
1050 adv = hci_find_adv_instance(hdev, instance);
1072 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1076 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1084 * hdev->random_addr to track its address so whenever it needs
1086 * hdev->random_addr is shared with scan state machine.
1088 err = hci_set_random_addr_sync(hdev, random_addr);
1098 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1102 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1114 adv = hci_find_adv_instance(hdev, instance);
1126 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1131 flags = hci_adv_instance_flags(hdev, instance);
1137 mgmt_get_connectable(hdev);
1139 if (!is_advertising_allowed(hdev, connectable))
1146 err = hci_get_random_address(hdev, !connectable,
1147 adv_use_rpa(hdev, flags), adv,
1159 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1160 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1171 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1192 hci_copy_identity_address(hdev, &cp.peer_addr,
1196 cp.channel_map = hdev->le_adv_channel_map;
1211 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1224 if (!bacmp(&random_addr, &hdev->random_addr))
1228 return hci_set_adv_set_random_addr_sync(hdev, instance,
1235 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1244 adv = hci_find_adv_instance(hdev, instance);
1249 len = eir_create_scan_rsp(hdev, instance, pdu->data);
1256 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1265 memcpy(hdev->scan_rsp_data, pdu->data, len);
1266 hdev->scan_rsp_data_len = len;
1272 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1279 len = eir_create_scan_rsp(hdev, instance, cp.data);
1281 if (hdev->scan_rsp_data_len == len &&
1282 !memcmp(cp.data, hdev->scan_rsp_data, len))
1285 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1286 hdev->scan_rsp_data_len = len;
1290 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1294 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1296 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1299 if (ext_adv_capable(hdev))
1300 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1302 return __hci_set_scan_rsp_data_sync(hdev, instance);
1305 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1313 adv = hci_find_adv_instance(hdev, instance);
1345 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1351 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1355 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1359 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1363 return hci_enable_ext_advertising_sync(hdev, instance);
1366 int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1372 adv = hci_find_adv_instance(hdev, instance);
1381 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1385 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1403 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1407 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1415 adv = hci_find_adv_instance(hdev, instance);
1420 len = eir_create_per_adv_data(hdev, instance, pdu->data);
1426 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1431 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1437 adv = hci_find_adv_instance(hdev, instance);
1446 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1453 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1483 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1485 return hci_update_adv_data_sync(hdev, adv->instance);
1488 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1496 hci_disable_per_advertising_sync(hdev, instance);
1499 adv = hci_find_adv_instance(hdev, instance);
1502 adv = hci_add_per_instance(hdev, instance, flags,
1514 err = hci_start_ext_adv_sync(hdev, instance);
1518 err = hci_adv_bcast_annoucement(hdev, adv);
1522 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1527 err = hci_set_per_adv_data_sync(hdev, instance);
1531 err = hci_enable_per_advertising_sync(hdev, instance);
1539 hci_remove_adv_instance(hdev, instance);
1544 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1548 if (ext_adv_capable(hdev))
1549 return hci_start_ext_adv_sync(hdev, instance);
1551 err = hci_update_adv_data_sync(hdev, instance);
1555 err = hci_update_scan_rsp_data_sync(hdev, instance);
1559 return hci_enable_advertising_sync(hdev);
1562 int hci_enable_advertising_sync(struct hci_dev *hdev)
1572 if (ext_adv_capable(hdev))
1573 return hci_enable_ext_advertising_sync(hdev,
1574 hdev->cur_adv_instance);
1576 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1577 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1583 mgmt_get_connectable(hdev);
1585 if (!is_advertising_allowed(hdev, connectable))
1588 status = hci_disable_advertising_sync(hdev);
1597 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1603 status = hci_update_random_address_sync(hdev, !connectable,
1604 adv_use_rpa(hdev, flags),
1615 adv_min_interval = hdev->le_adv_min_interval;
1616 adv_max_interval = hdev->le_adv_max_interval;
1622 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1627 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1628 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1637 cp.channel_map = hdev->le_adv_channel_map;
1639 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1644 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1648 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1650 return hci_enable_advertising_sync(hdev);
1653 int hci_enable_advertising(struct hci_dev *hdev)
1655 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1656 list_empty(&hdev->adv_instances))
1659 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1662 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1667 if (!ext_adv_capable(hdev))
1670 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1675 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1678 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1683 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1691 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1694 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1699 adv = hci_find_adv_instance(hdev, instance);
1704 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1707 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1715 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1719 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1728 adv = hci_find_adv_instance(hdev, instance);
1733 len = eir_create_adv_data(hdev, instance, pdu->data);
1740 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1750 memcpy(hdev->adv_data, pdu->data, len);
1751 hdev->adv_data_len = len;
1757 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1764 len = eir_create_adv_data(hdev, instance, cp.data);
1767 if (hdev->adv_data_len == len &&
1768 memcmp(cp.data, hdev->adv_data, len) == 0)
1771 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1772 hdev->adv_data_len = len;
1776 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1780 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1782 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1785 if (ext_adv_capable(hdev))
1786 return hci_set_ext_adv_data_sync(hdev, instance);
1788 return hci_set_adv_data_sync(hdev, instance);
1791 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1797 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1800 if (hdev->adv_instance_timeout)
1803 adv = hci_find_adv_instance(hdev, instance);
1827 if (!ext_adv_capable(hdev)) {
1828 hdev->adv_instance_timeout = timeout;
1829 queue_delayed_work(hdev->req_workqueue,
1830 &hdev->adv_instance_expire,
1838 if (!force && hdev->cur_adv_instance == instance &&
1839 hci_dev_test_flag(hdev, HCI_LE_ADV))
1842 hdev->cur_adv_instance = instance;
1844 return hci_start_adv_sync(hdev, instance);
1847 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1851 if (!ext_adv_capable(hdev))
1855 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1859 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1863 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1868 if (ext_adv_capable(hdev))
1870 err = hci_clear_adv_sets_sync(hdev, sk);
1871 if (ext_adv_capable(hdev))
1877 hci_dev_lock(hdev);
1880 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1887 err = hci_remove_adv_instance(hdev, instance);
1889 mgmt_advertising_removed(sk, hdev, instance);
1892 hci_dev_unlock(hdev);
1897 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1903 if (ext_adv_capable(hdev))
1904 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1905 if (ext_adv_capable(hdev))
1911 hci_dev_lock(hdev);
1913 err = hci_remove_adv_instance(hdev, instance);
1915 mgmt_advertising_removed(sk, hdev, instance);
1917 hci_dev_unlock(hdev);
1933 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1940 if (!instance || hdev->cur_adv_instance == instance)
1941 cancel_adv_timeout(hdev);
1947 if (hdev->cur_adv_instance == instance)
1948 next = hci_get_next_instance(hdev, instance);
1951 err = hci_clear_adv_sync(hdev, sk, force);
1955 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1962 err = hci_remove_adv_sync(hdev, instance, sk);
1968 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1971 if (next && !ext_adv_capable(hdev))
1972 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1977 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1982 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1986 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1988 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1992 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1998 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
2002 int hci_disable_advertising_sync(struct hci_dev *hdev)
2008 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
2011 if (ext_adv_capable(hdev))
2012 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
2013 if (ext_adv_capable(hdev))
2016 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
2020 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
2028 if (hci_dev_test_flag(hdev, HCI_MESH))
2033 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
2037 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2042 if (use_ext_scan(hdev))
2043 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2048 if (val && hci_dev_test_flag(hdev, HCI_MESH))
2053 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2057 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2059 if (!use_ll_privacy(hdev))
2063 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2066 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2070 static int hci_scan_disable_sync(struct hci_dev *hdev)
2075 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2078 if (hdev->scanning_paused) {
2079 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2083 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2085 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2092 static bool scan_use_rpa(struct hci_dev *hdev)
2094 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2097 static void hci_start_interleave_scan(struct hci_dev *hdev)
2099 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2100 queue_delayed_work(hdev->req_workqueue,
2101 &hdev->interleave_scan, 0);
2104 static bool is_interleave_scanning(struct hci_dev *hdev)
2106 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2109 static void cancel_interleave_scan(struct hci_dev *hdev)
2111 bt_dev_dbg(hdev, "cancelling interleave scan");
2113 cancel_delayed_work_sync(&hdev->interleave_scan);
2115 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2121 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2130 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2131 !(list_empty(&hdev->pend_le_conns) &&
2132 list_empty(&hdev->pend_le_reports)) &&
2133 hci_get_adv_monitor_offload_ext(hdev) ==
2135 bool is_interleaving = is_interleave_scanning(hdev);
2138 hci_start_interleave_scan(hdev);
2139 bt_dev_dbg(hdev, "starting interleave scan");
2144 cancel_interleave_scan(hdev);
2150 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2156 if (!use_ll_privacy(hdev))
2160 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2168 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2172 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2179 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2188 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2190 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2193 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2197 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2211 * Setting params to NULL programs local hdev->irk
2213 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2221 if (!use_ll_privacy(hdev))
2228 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2231 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2232 memcpy(cp.peer_irk, hdev->irk, 16);
2236 irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
2241 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2255 p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2258 p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2265 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2266 memcpy(cp.local_irk, hdev->irk, 16);
2270 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2275 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2292 irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
2303 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2311 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2319 if (hdev->suspended &&
2321 hci_le_del_accept_list_sync(hdev, &params->addr,
2327 if (*num_entries >= hdev->le_accept_list_size)
2331 if (!use_ll_privacy(hdev) &&
2332 hci_find_irk_by_addr(hdev, &params->addr, params->addr_type))
2339 err = hci_le_add_resolve_list_sync(hdev, params);
2341 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2346 err = hci_le_set_privacy_mode_sync(hdev, params);
2348 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2353 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
2361 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2364 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2366 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2370 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2377 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2383 if (hdev->advertising_paused)
2386 bt_dev_dbg(hdev, "Pausing directed advertising");
2389 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2396 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2397 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2398 hdev->discov_timeout = 0;
2401 bt_dev_dbg(hdev, "Pausing advertising instances");
2406 err = hci_disable_advertising_sync(hdev);
2411 if (!ext_adv_capable(hdev))
2412 cancel_adv_timeout(hdev);
2414 hdev->advertising_paused = true;
2415 hdev->advertising_old_state = old_state;
2421 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2427 if (!hdev->advertising_paused)
2431 hdev->advertising_paused = false;
2432 if (hdev->advertising_old_state) {
2433 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2434 hdev->advertising_old_state = 0;
2437 bt_dev_dbg(hdev, "Resuming advertising instances");
2439 if (ext_adv_capable(hdev)) {
2441 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2442 err = hci_enable_ext_advertising_sync(hdev,
2448 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2455 err = hci_schedule_adv_instance_sync(hdev,
2456 hdev->cur_adv_instance,
2460 hdev->advertising_paused = false;
2465 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2469 if (!use_ll_privacy(hdev))
2472 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2478 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2479 hci_lookup_le_connect(hdev)) {
2480 bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2485 err = hci_pause_advertising_sync(hdev);
2487 bt_dev_err(hdev, "Pause advertising failed: %d", err);
2491 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2493 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2497 if (!err && scan_use_rpa(hdev))
2500 hci_resume_advertising_sync(hdev);
2504 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2510 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2540 /* No hdev->lock, but: addr, addr_type are immutable.
2560 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
2562 if (!(hdev->commands[26] & 0x80))
2565 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
2586 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2599 if (use_ll_privacy(hdev)) {
2600 err = hci_pause_advertising_sync(hdev);
2602 bt_dev_err(hdev, "pause advertising failed: %d", err);
2611 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2613 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2618 if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2621 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_PA_CREATE_SYNC);
2633 hci_le_clear_accept_list_sync(hdev);
2636 err = hci_le_add_accept_list_sync(hdev, &pa,
2648 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2649 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2653 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2656 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2664 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2686 params = conn_params_copy(&hdev->pend_le_conns, &n);
2693 err = hci_le_add_accept_list_sync(hdev, &params[i],
2708 params = conn_params_copy(&hdev->pend_le_reports, &n);
2715 err = hci_le_add_accept_list_sync(hdev, &params[i],
2730 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2731 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2732 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2739 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2741 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2744 if (use_ll_privacy(hdev))
2745 hci_resume_advertising_sync(hdev);
2759 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2779 if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2782 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
2786 conn = hci_conn_hash_lookup_ba(hdev, ISO_LINK,
2816 if (scan_1m(hdev) || scan_2m(hdev)) {
2823 if (scan_coded(hdev)) {
2834 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2839 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2845 if (use_ext_scan(hdev))
2846 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2857 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2861 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2867 if (hdev->scanning_paused) {
2868 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2872 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2877 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2880 static int hci_passive_scan_sync(struct hci_dev *hdev)
2888 if (hdev->scanning_paused) {
2889 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2893 err = hci_scan_disable_sync(hdev);
2895 bt_dev_err(hdev, "disable scanning failed: %d", err);
2905 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2909 if (hdev->enable_advmon_interleave_scan &&
2910 hci_update_interleaved_scan_sync(hdev))
2913 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2919 filter_policy = hci_update_accept_list_sync(hdev);
2930 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2931 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2934 if (hdev->suspended) {
2935 window = hdev->le_scan_window_suspend;
2936 interval = hdev->le_scan_int_suspend;
2937 } else if (hci_is_le_conn_scanning(hdev)) {
2938 window = hdev->le_scan_window_connect;
2939 interval = hdev->le_scan_int_connect;
2940 } else if (hci_is_adv_monitoring(hdev)) {
2941 window = hdev->le_scan_window_adv_monitor;
2942 interval = hdev->le_scan_int_adv_monitor;
2944 window = hdev->le_scan_window;
2945 interval = hdev->le_scan_interval;
2949 if (hci_dev_test_flag(hdev, HCI_MESH)) {
2954 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2956 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2960 /* This function controls the passive scanning based on hdev->pend_le_conns
2975 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2979 if (!test_bit(HCI_UP, &hdev->flags) ||
2980 test_bit(HCI_INIT, &hdev->flags) ||
2981 hci_dev_test_flag(hdev, HCI_SETUP) ||
2982 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2983 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2984 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2988 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2992 if (hdev->discovery.state != DISCOVERY_STOPPED)
3002 hci_discovery_filter_clear(hdev);
3004 bt_dev_dbg(hdev, "ADV monitoring is %s",
3005 hci_is_adv_monitoring(hdev) ? "on" : "off");
3007 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
3008 list_empty(&hdev->pend_le_conns) &&
3009 list_empty(&hdev->pend_le_reports) &&
3010 !hci_is_adv_monitoring(hdev) &&
3011 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
3017 bt_dev_dbg(hdev, "stopping background scanning");
3019 err = hci_scan_disable_sync(hdev);
3021 bt_dev_err(hdev, "stop background scanning failed: %d",
3032 if (hci_lookup_le_connect(hdev))
3035 bt_dev_dbg(hdev, "start background scanning");
3037 err = hci_passive_scan_sync(hdev);
3039 bt_dev_err(hdev, "start background scanning failed: %d",
3046 static int update_scan_sync(struct hci_dev *hdev, void *data)
3048 return hci_update_scan_sync(hdev);
3051 int hci_update_scan(struct hci_dev *hdev)
3053 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
3056 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
3058 return hci_update_passive_scan_sync(hdev);
3061 int hci_update_passive_scan(struct hci_dev *hdev)
3064 if (!test_bit(HCI_UP, &hdev->flags) ||
3065 test_bit(HCI_INIT, &hdev->flags) ||
3066 hci_dev_test_flag(hdev, HCI_SETUP) ||
3067 hci_dev_test_flag(hdev, HCI_CONFIG) ||
3068 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
3069 hci_dev_test_flag(hdev, HCI_UNREGISTER))
3072 return hci_cmd_sync_queue_once(hdev, update_passive_scan_sync, NULL,
3076 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
3080 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
3083 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3088 hdev->features[1][0] |= LMP_HOST_SC;
3089 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
3091 hdev->features[1][0] &= ~LMP_HOST_SC;
3092 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
3099 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
3103 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3104 lmp_host_ssp_capable(hdev))
3107 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
3108 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
3112 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3117 return hci_write_sc_support_sync(hdev, 0x01);
3120 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
3124 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
3125 !lmp_bredr_capable(hdev))
3131 if (le == lmp_host_le_capable(hdev) &&
3132 simul == lmp_host_le_br_capable(hdev))
3140 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3144 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3149 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3157 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3158 hci_le_add_resolve_list_sync(hdev, NULL);
3159 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3166 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3167 list_empty(&hdev->adv_instances)) {
3168 if (ext_adv_capable(hdev)) {
3169 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3171 hci_update_scan_rsp_data_sync(hdev, 0x00);
3173 err = hci_update_adv_data_sync(hdev, 0x00);
3175 hci_update_scan_rsp_data_sync(hdev, 0x00);
3178 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3179 hci_enable_advertising_sync(hdev);
3183 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3184 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3189 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3193 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3194 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3197 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3202 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3208 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3211 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3222 type = hdev->def_page_scan_type;
3223 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3226 cp.window = cpu_to_le16(hdev->def_page_scan_window);
3228 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3229 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3230 err = __hci_cmd_sync_status(hdev,
3237 if (hdev->page_scan_type != type)
3238 err = __hci_cmd_sync_status(hdev,
3246 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3250 list_for_each_entry(b, &hdev->accept_list, list) {
3253 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3264 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3266 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3271 int hci_update_scan_sync(struct hci_dev *hdev)
3275 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3278 if (!hdev_is_powered(hdev))
3281 if (mgmt_powering_down(hdev))
3284 if (hdev->scanning_paused)
3287 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3288 disconnected_accept_list_entries(hdev))
3293 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3296 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3297 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3300 return hci_write_scan_enable_sync(hdev, scan);
3303 int hci_update_name_sync(struct hci_dev *hdev)
3309 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3311 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3328 int hci_powered_update_sync(struct hci_dev *hdev)
3337 smp_register(hdev);
3339 err = hci_write_ssp_mode_sync(hdev, 0x01);
3343 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3347 err = hci_powered_update_adv_sync(hdev);
3351 err = hci_write_auth_enable_sync(hdev);
3355 if (lmp_bredr_capable(hdev)) {
3356 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3357 hci_write_fast_connectable_sync(hdev, true);
3359 hci_write_fast_connectable_sync(hdev, false);
3360 hci_update_scan_sync(hdev);
3361 hci_update_class_sync(hdev);
3362 hci_update_name_sync(hdev);
3363 hci_update_eir_sync(hdev);
3375 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3376 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3377 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3378 if (bacmp(&hdev->static_addr, BDADDR_ANY))
3379 return hci_set_random_addr_sync(hdev,
3380 &hdev->static_addr);
3390 * @hdev: The HCI device
3398 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3400 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3409 if (test_bit(HCI_QUIRK_BDADDR_PROPERTY_BROKEN, &hdev->quirks))
3410 baswap(&hdev->public_addr, &ba);
3412 bacpy(&hdev->public_addr, &ba);
3416 int (*func)(struct hci_dev *hdev);
3420 static int hci_init_stage_sync(struct hci_dev *hdev,
3428 err = stage[i].func(hdev);
3437 static int hci_read_local_version_sync(struct hci_dev *hdev)
3439 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3444 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3446 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3463 int hci_reset_sync(struct hci_dev *hdev)
3467 set_bit(HCI_RESET, &hdev->flags);
3469 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3477 static int hci_init0_sync(struct hci_dev *hdev)
3481 bt_dev_dbg(hdev, "");
3484 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3485 err = hci_reset_sync(hdev);
3490 return hci_init_stage_sync(hdev, hci_init0);
3493 static int hci_unconf_init_sync(struct hci_dev *hdev)
3497 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3500 err = hci_init0_sync(hdev);
3504 if (hci_dev_test_flag(hdev, HCI_SETUP))
3505 hci_debugfs_create_basic(hdev);
3511 static int hci_read_local_features_sync(struct hci_dev *hdev)
3513 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3529 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3539 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3540 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3541 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3547 static int hci_init1_sync(struct hci_dev *hdev)
3551 bt_dev_dbg(hdev, "");
3554 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3555 err = hci_reset_sync(hdev);
3560 return hci_init_stage_sync(hdev, br_init1);
3564 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3566 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3571 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3573 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3578 static int hci_read_local_name_sync(struct hci_dev *hdev)
3580 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3585 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3587 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3592 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3594 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3599 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3601 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3605 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3611 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3614 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3626 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3632 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3634 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3641 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3644 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3649 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3653 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3678 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3682 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3691 hdev->max_page = 0x01;
3693 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3697 static int hci_write_eir_sync(struct hci_dev *hdev)
3701 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3704 memset(hdev->eir, 0, sizeof(hdev->eir));
3707 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3711 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3715 if (!lmp_inq_rssi_capable(hdev) &&
3716 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3723 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3725 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3729 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3731 if (!lmp_inq_tx_pwr_capable(hdev))
3734 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3738 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3742 if (!lmp_ext_feat_capable(hdev))
3748 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3752 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3754 return hci_read_local_ext_features_sync(hdev, 0x01);
3777 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3780 if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3781 return __hci_cmd_sync_status(hdev,
3785 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3790 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3792 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3797 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3799 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3814 static int hci_init2_sync(struct hci_dev *hdev)
3818 bt_dev_dbg(hdev, "");
3820 err = hci_init_stage_sync(hdev, hci_init2);
3824 if (lmp_bredr_capable(hdev)) {
3825 err = hci_init_stage_sync(hdev, br_init2);
3829 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3832 if (lmp_le_capable(hdev)) {
3833 err = hci_init_stage_sync(hdev, le_init2);
3837 if (!lmp_bredr_capable(hdev))
3838 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3844 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3855 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3858 if (lmp_bredr_capable(hdev)) {
3865 if (hdev->suspended) {
3880 if (hdev->commands[0] & 0x20) {
3885 if (!hdev->suspended)
3894 if (hdev->commands[2] & 0x80)
3899 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3905 if (lmp_inq_rssi_capable(hdev) ||
3906 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3909 if (lmp_ext_feat_capable(hdev))
3912 if (lmp_esco_capable(hdev)) {
3917 if (lmp_sniffsubr_capable(hdev))
3920 if (lmp_pause_enc_capable(hdev))
3923 if (lmp_ext_inq_capable(hdev))
3926 if (lmp_no_flush_capable(hdev))
3929 if (lmp_lsto_capable(hdev))
3932 if (lmp_ssp_capable(hdev)) {
3946 if (lmp_le_capable(hdev))
3949 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3953 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3957 if (!(hdev->commands[6] & 0x20) ||
3958 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3965 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3969 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3974 if (!(hdev->commands[5] & 0x10))
3979 if (lmp_rswitch_capable(hdev))
3981 if (lmp_hold_capable(hdev))
3983 if (lmp_sniff_capable(hdev))
3985 if (lmp_park_capable(hdev))
3990 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3994 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3996 if (!(hdev->commands[8] & 0x01))
3999 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
4003 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
4005 if (!(hdev->commands[18] & 0x04) ||
4006 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4007 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4010 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
4014 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
4020 if (!(hdev->commands[13] & 0x01))
4023 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
4028 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
4033 if (!lmp_ext_feat_capable(hdev))
4036 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
4038 err = hci_read_local_ext_features_sync(hdev, page);
4065 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4069 if (!lmp_le_capable(hdev))
4074 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4080 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4087 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4093 if (use_enhanced_conn_complete(hdev))
4099 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4105 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4111 if (hdev->commands[26] & 0x08)
4117 if (hdev->commands[26] & 0x10)
4123 if (hdev->commands[27] & 0x04)
4129 if (hdev->commands[27] & 0x20)
4136 if (hdev->commands[34] & 0x02)
4143 if (hdev->commands[34] & 0x04)
4149 if (hdev->commands[35] & (0x20 | 0x40))
4156 if (use_ext_scan(hdev))
4162 if (ext_adv_capable(hdev))
4165 if (cis_capable(hdev)) {
4167 if (cis_peripheral_capable(hdev))
4171 if (bis_capable(hdev)) {
4181 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4186 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4188 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4195 return __hci_cmd_sync_status(hdev,
4204 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4206 if (!(hdev->commands[38] & 0x80) ||
4207 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4210 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4215 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4217 if (!(hdev->commands[26] & 0x40))
4220 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4225 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4227 if (!(hdev->commands[34] & 0x40))
4230 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4235 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4237 if (!(hdev->commands[34] & 0x20))
4240 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4245 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4247 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4249 if (!(hdev->commands[35] & 0x04) ||
4250 test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4253 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4259 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4261 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4264 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4269 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4271 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4274 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4279 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4281 if (!ext_adv_capable(hdev))
4284 return __hci_cmd_sync_status(hdev,
4290 static int hci_set_le_support_sync(struct hci_dev *hdev)
4295 if (!lmp_bredr_capable(hdev))
4300 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4305 if (cp.le == lmp_host_le_capable(hdev))
4308 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4313 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4317 if (!cis_capable(hdev))
4326 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4361 static int hci_init3_sync(struct hci_dev *hdev)
4365 bt_dev_dbg(hdev, "");
4367 err = hci_init_stage_sync(hdev, hci_init3);
4371 if (lmp_le_capable(hdev))
4372 return hci_init_stage_sync(hdev, le_init3);
4377 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4394 if (!(hdev->commands[6] & 0x80) ||
4395 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4402 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4406 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4412 if (!(hdev->commands[22] & 0x04))
4418 if (lmp_cpb_central_capable(hdev)) {
4429 if (lmp_cpb_peripheral_capable(hdev)) {
4438 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4452 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4457 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4459 if (hdev->commands[45] & 0x04)
4460 hci_read_supported_codecs_v2(hdev);
4461 else if (hdev->commands[29] & 0x20)
4462 hci_read_supported_codecs(hdev);
4468 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4470 if (!(hdev->commands[41] & 0x08))
4473 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4478 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4480 if (!mws_transport_config_capable(hdev))
4483 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4488 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4490 if (!lmp_sync_train_capable(hdev))
4493 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4498 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4502 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4503 !bredr_sc_enabled(hdev))
4506 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4514 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4517 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4519 if (!(hdev->commands[18] & 0x08) ||
4520 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4521 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4524 if (enabled == hdev->err_data_reporting)
4531 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4556 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4560 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4564 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4565 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4567 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4574 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4578 if (!(hdev->commands[35] & 0x20)) {
4582 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4583 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4593 if (le_2m_capable(hdev)) {
4599 if (le_coded_capable(hdev)) {
4604 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4616 static int hci_init4_sync(struct hci_dev *hdev)
4620 bt_dev_dbg(hdev, "");
4622 err = hci_init_stage_sync(hdev, hci_init4);
4626 if (lmp_le_capable(hdev))
4627 return hci_init_stage_sync(hdev, le_init4);
4632 static int hci_init_sync(struct hci_dev *hdev)
4636 err = hci_init1_sync(hdev);
4640 if (hci_dev_test_flag(hdev, HCI_SETUP))
4641 hci_debugfs_create_basic(hdev);
4643 err = hci_init2_sync(hdev);
4647 err = hci_init3_sync(hdev);
4651 err = hci_init4_sync(hdev);
4667 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4668 !hci_dev_test_flag(hdev, HCI_CONFIG))
4671 if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4674 hci_debugfs_create_common(hdev);
4676 if (lmp_bredr_capable(hdev))
4677 hci_debugfs_create_bredr(hdev);
4679 if (lmp_le_capable(hdev))
4680 hci_debugfs_create_le(hdev);
4715 /* This function handles hdev setup stage:
4717 * Calls hdev->setup
4720 static int hci_dev_setup_sync(struct hci_dev *hdev)
4726 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4727 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4730 bt_dev_dbg(hdev, "");
4732 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4734 if (hdev->setup)
4735 ret = hdev->setup(hdev);
4738 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4739 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4746 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
4747 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
4749 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4750 !bacmp(&hdev->public_addr, BDADDR_ANY))
4751 hci_dev_get_bd_addr_from_property(hdev);
4753 if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
4754 hdev->set_bdaddr) {
4755 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4771 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4773 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4783 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4784 return hci_unconf_init_sync(hdev);
4789 /* This function handles hdev init stage:
4794 static int hci_dev_init_sync(struct hci_dev *hdev)
4798 bt_dev_dbg(hdev, "");
4800 atomic_set(&hdev->cmd_cnt, 1);
4801 set_bit(HCI_INIT, &hdev->flags);
4803 ret = hci_dev_setup_sync(hdev);
4805 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4811 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4812 hdev->set_bdaddr)
4813 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4819 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4820 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4821 ret = hci_init_sync(hdev);
4822 if (!ret && hdev->post_init)
4823 ret = hdev->post_init(hdev);
4831 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4832 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4833 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4834 ret = hdev->set_diag(hdev, true);
4836 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4837 msft_do_open(hdev);
4838 aosp_do_open(hdev);
4841 clear_bit(HCI_INIT, &hdev->flags);
4846 int hci_dev_open_sync(struct hci_dev *hdev)
4850 bt_dev_dbg(hdev, "");
4852 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4857 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4858 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4862 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4876 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4877 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4878 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4884 if (test_bit(HCI_UP, &hdev->flags)) {
4889 if (hdev->open(hdev)) {
4894 hci_devcd_reset(hdev);
4896 set_bit(HCI_RUNNING, &hdev->flags);
4897 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4899 ret = hci_dev_init_sync(hdev);
4901 hci_dev_hold(hdev);
4902 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4903 hci_adv_instances_set_rpa_expired(hdev, true);
4904 set_bit(HCI_UP, &hdev->flags);
4905 hci_sock_dev_event(hdev, HCI_DEV_UP);
4906 hci_leds_update_powered(hdev, true);
4907 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4908 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4909 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4910 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4911 hci_dev_test_flag(hdev, HCI_MGMT)) {
4912 ret = hci_powered_update_sync(hdev);
4913 mgmt_power_on(hdev, ret);
4917 flush_work(&hdev->tx_work);
4923 flush_work(&hdev->rx_work);
4924 flush_work(&hdev->cmd_work);
4926 skb_queue_purge(&hdev->cmd_q);
4927 skb_queue_purge(&hdev->rx_q);
4929 if (hdev->flush)
4930 hdev->flush(hdev);
4932 if (hdev->sent_cmd) {
4933 cancel_delayed_work_sync(&hdev->cmd_timer);
4934 kfree_skb(hdev->sent_cmd);
4935 hdev->sent_cmd = NULL;
4938 if (hdev->req_skb) {
4939 kfree_skb(hdev->req_skb);
4940 hdev->req_skb = NULL;
4943 clear_bit(HCI_RUNNING, &hdev->flags);
4944 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4946 hdev->close(hdev);
4947 hdev->flags &= BIT(HCI_RAW);
4954 /* This function requires the caller holds hdev->lock */
4955 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4959 list_for_each_entry(p, &hdev->le_conn_params, list) {
4971 static int hci_dev_shutdown(struct hci_dev *hdev)
4981 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
4983 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4984 test_bit(HCI_UP, &hdev->flags)) {
4986 if (hdev->shutdown)
4987 err = hdev->shutdown(hdev);
4991 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
4996 int hci_dev_close_sync(struct hci_dev *hdev)
5001 bt_dev_dbg(hdev, "");
5003 cancel_delayed_work(&hdev->power_off);
5004 cancel_delayed_work(&hdev->ncmd_timer);
5005 cancel_delayed_work(&hdev->le_scan_disable);
5007 hci_request_cancel_all(hdev);
5009 if (hdev->adv_instance_timeout) {
5010 cancel_delayed_work_sync(&hdev->adv_instance_expire);
5011 hdev->adv_instance_timeout = 0;
5014 err = hci_dev_shutdown(hdev);
5016 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
5017 cancel_delayed_work_sync(&hdev->cmd_timer);
5021 hci_leds_update_powered(hdev, false);
5024 flush_work(&hdev->tx_work);
5025 flush_work(&hdev->rx_work);
5027 if (hdev->discov_timeout > 0) {
5028 hdev->discov_timeout = 0;
5029 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5030 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
5033 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
5034 cancel_delayed_work(&hdev->service_cache);
5036 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
5039 cancel_delayed_work_sync(&hdev->rpa_expired);
5041 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5048 drain_workqueue(hdev->workqueue);
5050 hci_dev_lock(hdev);
5052 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5054 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5056 if (!auto_off && !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5057 hci_dev_test_flag(hdev, HCI_MGMT))
5058 __mgmt_power_off(hdev);
5060 hci_inquiry_cache_flush(hdev);
5061 hci_pend_le_actions_clear(hdev);
5062 hci_conn_hash_flush(hdev);
5063 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5064 smp_unregister(hdev);
5065 hci_dev_unlock(hdev);
5067 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5069 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5070 aosp_do_close(hdev);
5071 msft_do_close(hdev);
5074 if (hdev->flush)
5075 hdev->flush(hdev);
5078 skb_queue_purge(&hdev->cmd_q);
5079 atomic_set(&hdev->cmd_cnt, 1);
5080 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5081 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5082 set_bit(HCI_INIT, &hdev->flags);
5083 hci_reset_sync(hdev);
5084 clear_bit(HCI_INIT, &hdev->flags);
5088 flush_work(&hdev->cmd_work);
5091 skb_queue_purge(&hdev->rx_q);
5092 skb_queue_purge(&hdev->cmd_q);
5093 skb_queue_purge(&hdev->raw_q);
5096 if (hdev->sent_cmd) {
5097 cancel_delayed_work_sync(&hdev->cmd_timer);
5098 kfree_skb(hdev->sent_cmd);
5099 hdev->sent_cmd = NULL;
5103 if (hdev->req_skb) {
5104 kfree_skb(hdev->req_skb);
5105 hdev->req_skb = NULL;
5108 clear_bit(HCI_RUNNING, &hdev->flags);
5109 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5112 hdev->close(hdev);
5115 hdev->flags &= BIT(HCI_RAW);
5116 hci_dev_clear_volatile_flags(hdev);
5118 memset(hdev->eir, 0, sizeof(hdev->eir));
5119 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5120 bacpy(&hdev->random_addr, BDADDR_ANY);
5121 hci_codec_list_clear(&hdev->local_codecs);
5123 hci_dev_put(hdev);
5133 static int hci_power_on_sync(struct hci_dev *hdev)
5137 if (test_bit(HCI_UP, &hdev->flags) &&
5138 hci_dev_test_flag(hdev, HCI_MGMT) &&
5139 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5140 cancel_delayed_work(&hdev->power_off);
5141 return hci_powered_update_sync(hdev);
5144 err = hci_dev_open_sync(hdev);
5152 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5153 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5154 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5155 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5156 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5157 hci_dev_close_sync(hdev);
5158 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5159 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5163 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5167 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5168 set_bit(HCI_RAW, &hdev->flags);
5177 mgmt_index_added(hdev);
5178 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5182 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5183 clear_bit(HCI_RAW, &hdev->flags);
5189 mgmt_index_added(hdev);
5195 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5202 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5206 int hci_stop_discovery_sync(struct hci_dev *hdev)
5208 struct discovery_state *d = &hdev->discovery;
5212 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5215 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5216 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5222 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5223 cancel_delayed_work(&hdev->le_scan_disable);
5225 err = hci_scan_disable_sync(hdev);
5231 err = hci_scan_disable_sync(hdev);
5237 if (use_ll_privacy(hdev))
5238 hci_resume_advertising_sync(hdev);
5245 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5250 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5256 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5265 hci_dev_lock(hdev);
5267 hci_dev_unlock(hdev);
5282 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5287 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5291 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5304 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5308 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5312 return hci_le_connect_cancel_sync(hdev, conn, reason);
5324 return hci_disconnect_sync(hdev, conn, reason);
5336 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5345 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5350 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5354 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5369 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5373 static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
5382 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
5386 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5392 return hci_le_reject_cis_sync(hdev, conn, reason);
5395 return hci_reject_sco_sync(hdev, conn, reason);
5401 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5405 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5415 err = hci_disconnect_sync(hdev, conn, reason);
5418 err = hci_connect_cancel_sync(hdev, conn, reason);
5421 err = hci_reject_conn_sync(hdev, conn, reason);
5431 hci_dev_lock(hdev);
5434 c = hci_conn_hash_lookup_handle(hdev, handle);
5454 hci_dev_unlock(hdev);
5458 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5460 struct list_head *head = &hdev->conn_hash.list;
5473 hci_abort_conn_sync(hdev, conn, reason);
5489 static int hci_power_off_sync(struct hci_dev *hdev)
5494 if (!test_bit(HCI_UP, &hdev->flags))
5497 hci_dev_set_flag(hdev, HCI_POWERING_DOWN);
5499 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5500 test_bit(HCI_PSCAN, &hdev->flags)) {
5501 err = hci_write_scan_enable_sync(hdev, 0x00);
5506 err = hci_clear_adv_sync(hdev, NULL, false);
5510 err = hci_stop_discovery_sync(hdev);
5515 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5519 err = hci_dev_close_sync(hdev);
5522 hci_dev_clear_flag(hdev, HCI_POWERING_DOWN);
5526 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5529 return hci_power_on_sync(hdev);
5531 return hci_power_off_sync(hdev);
5534 static int hci_write_iac_sync(struct hci_dev *hdev)
5538 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5543 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5545 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5560 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5565 int hci_update_discoverable_sync(struct hci_dev *hdev)
5569 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5570 err = hci_write_iac_sync(hdev);
5574 err = hci_update_scan_sync(hdev);
5578 err = hci_update_class_sync(hdev);
5586 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5587 err = hci_update_adv_data_sync(hdev, 0x00);
5594 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5595 if (ext_adv_capable(hdev))
5596 err = hci_start_ext_adv_sync(hdev, 0x00);
5598 err = hci_enable_advertising_sync(hdev);
5605 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5607 return hci_update_discoverable_sync(hdev);
5610 int hci_update_discoverable(struct hci_dev *hdev)
5613 if (hdev_is_powered(hdev) &&
5614 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5615 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5616 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5617 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5623 int hci_update_connectable_sync(struct hci_dev *hdev)
5627 err = hci_update_scan_sync(hdev);
5635 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5636 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5639 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5640 !list_empty(&hdev->adv_instances)) {
5641 if (ext_adv_capable(hdev))
5642 err = hci_start_ext_adv_sync(hdev,
5643 hdev->cur_adv_instance);
5645 err = hci_enable_advertising_sync(hdev);
5651 return hci_update_passive_scan_sync(hdev);
5654 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5660 bt_dev_dbg(hdev, "");
5662 if (test_bit(HCI_INQUIRY, &hdev->flags))
5665 hci_dev_lock(hdev);
5666 hci_inquiry_cache_flush(hdev);
5667 hci_dev_unlock(hdev);
5671 if (hdev->discovery.limited)
5678 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5682 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5691 bt_dev_dbg(hdev, "");
5697 err = hci_scan_disable_sync(hdev);
5699 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5703 cancel_interleave_scan(hdev);
5708 err = hci_pause_addr_resolution(hdev);
5716 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5721 if (hci_is_adv_monitoring(hdev) ||
5722 (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5723 hdev->discovery.result_filtering)) {
5737 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5738 hdev->le_scan_window_discovery,
5745 if (use_ll_privacy(hdev))
5746 hci_resume_advertising_sync(hdev);
5749 hci_update_passive_scan_sync(hdev);
5753 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5757 bt_dev_dbg(hdev, "");
5759 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5763 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5766 int hci_start_discovery_sync(struct hci_dev *hdev)
5771 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5773 switch (hdev->discovery.type) {
5775 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5786 &hdev->quirks)) {
5792 err = hci_start_interleaved_discovery_sync(hdev);
5796 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5797 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5801 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5810 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5812 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5817 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5819 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5821 msft_suspend_sync(hdev);
5829 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5831 int old_state = hdev->discovery.state;
5836 hdev->discovery_paused)
5839 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5840 err = hci_stop_discovery_sync(hdev);
5844 hdev->discovery_paused = true;
5845 hdev->discovery_old_state = old_state;
5846 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5851 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5855 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5858 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5864 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5868 hci_clear_event_filter_sync(hdev);
5870 list_for_each_entry(b, &hdev->accept_list, list) {
5874 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5876 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5881 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5888 hci_write_scan_enable_sync(hdev, scan);
5890 hci_write_scan_enable_sync(hdev, scan);
5896 static int hci_pause_scan_sync(struct hci_dev *hdev)
5898 if (hdev->scanning_paused)
5902 if (test_bit(HCI_PSCAN, &hdev->flags))
5903 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
5905 hci_scan_disable_sync(hdev);
5907 hdev->scanning_paused = true;
5918 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5925 int hci_suspend_sync(struct hci_dev *hdev)
5930 if (hdev->suspended)
5934 hdev->suspended = true;
5937 hci_pause_discovery_sync(hdev);
5940 hci_pause_advertising_sync(hdev);
5943 hci_suspend_monitor_sync(hdev);
5946 hci_pause_scan_sync(hdev);
5948 if (hci_conn_count(hdev)) {
5950 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5953 hdev->suspend_state = BT_RUNNING;
5954 hci_resume_sync(hdev);
5961 hci_set_event_mask_sync(hdev);
5967 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5968 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5973 hdev->scanning_paused = false;
5976 hci_update_event_filter_sync(hdev);
5979 hci_update_passive_scan_sync(hdev);
5982 hdev->scanning_paused = true;
5984 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5990 static int hci_resume_discovery_sync(struct hci_dev *hdev)
5995 if (!hdev->discovery_paused)
5998 hdev->discovery_paused = false;
6000 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6002 err = hci_start_discovery_sync(hdev);
6004 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
6010 static void hci_resume_monitor_sync(struct hci_dev *hdev)
6012 switch (hci_get_adv_monitor_offload_ext(hdev)) {
6014 msft_resume_sync(hdev);
6022 static int hci_resume_scan_sync(struct hci_dev *hdev)
6024 if (!hdev->scanning_paused)
6027 hdev->scanning_paused = false;
6029 hci_update_scan_sync(hdev);
6032 hci_update_passive_scan_sync(hdev);
6045 int hci_resume_sync(struct hci_dev *hdev)
6048 if (!hdev->suspended)
6051 hdev->suspended = false;
6054 hci_set_event_mask_sync(hdev);
6057 hci_clear_event_filter_sync(hdev);
6060 hci_resume_scan_sync(hdev);
6063 hci_resume_monitor_sync(hdev);
6066 hci_resume_advertising_sync(hdev);
6069 hci_resume_discovery_sync(hdev);
6076 struct hci_dev *hdev = conn->hdev;
6078 return hci_dev_test_flag(hdev, HCI_PRIVACY);
6081 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
6089 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6097 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
6105 cp.channel_map = hdev->le_adv_channel_map;
6122 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
6126 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
6134 bacmp(&random_addr, &hdev->random_addr)) {
6135 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
6141 return hci_enable_ext_advertising_sync(hdev, 0x00);
6144 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
6152 if (ext_adv_capable(hdev))
6153 return hci_le_ext_directed_advertising_sync(hdev, conn);
6160 hci_dev_clear_flag(hdev, HCI_LE_ADV);
6165 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6183 cp.channel_map = hdev->le_adv_channel_map;
6185 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
6192 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
6199 struct hci_dev *hdev = conn->hdev;
6203 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6204 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6213 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
6232 if (scan_1m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_1M ||
6241 if (scan_2m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_2M ||
6250 if (scan_coded(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_CODED ||
6258 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6264 static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
6272 if (!hci_conn_valid(hdev, conn))
6275 bt_dev_dbg(hdev, "conn %p", conn);
6285 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6286 hdev->le_scan_type == LE_SCAN_ACTIVE &&
6287 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6293 hci_pause_advertising_sync(hdev);
6295 err = hci_le_directed_advertising_sync(hdev, conn);
6300 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6301 hci_pause_advertising_sync(hdev);
6303 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6310 conn->le_conn_min_interval = hdev->le_conn_min_interval;
6311 conn->le_conn_max_interval = hdev->le_conn_max_interval;
6312 conn->le_conn_latency = hdev->le_conn_latency;
6313 conn->le_supv_timeout = hdev->le_supv_timeout;
6322 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6323 hci_scan_disable_sync(hdev);
6324 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6330 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6335 if (use_ext_conn(hdev)) {
6336 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6342 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6343 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6361 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6363 use_enhanced_conn_complete(hdev) ?
6370 hci_le_connect_cancel_sync(hdev, conn, 0x00);
6373 hci_resume_advertising_sync(hdev);
6377 int hci_le_create_cis_sync(struct hci_dev *hdev)
6407 hci_dev_lock(hdev);
6412 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6418 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6426 list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
6442 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6462 hci_dev_unlock(hdev);
6468 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6474 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6481 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6485 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6492 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6496 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6503 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6507 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6522 if (use_ll_privacy(hdev))
6531 if (rpa_valid(hdev))
6535 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6537 bt_dev_err(hdev, "failed to generate new RPA");
6541 bacpy(rand_addr, &hdev->rpa);
6564 if (bacmp(&hdev->bdaddr, &nrpa))
6580 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6584 return hci_update_adv_data_sync(hdev, instance);
6587 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6589 return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6593 static int hci_acl_create_conn_sync(struct hci_dev *hdev, void *data)
6600 if (!hci_conn_valid(hdev, conn))
6611 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
6612 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 0,
6615 bt_dev_warn(hdev, "Failed to cancel inquiry %d", err);
6624 conn->link_policy = hdev->link_policy;
6630 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
6643 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
6648 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN,
6654 int hci_connect_acl_sync(struct hci_dev *hdev, struct hci_conn *conn)
6656 return hci_cmd_sync_queue_once(hdev, hci_acl_create_conn_sync, conn,
6660 static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err)
6664 bt_dev_dbg(hdev, "err %d", err);
6669 hci_dev_lock(hdev);
6671 if (!hci_conn_valid(hdev, conn))
6680 if (conn != hci_lookup_le_connect(hdev))
6688 hci_dev_unlock(hdev);
6691 int hci_connect_le_sync(struct hci_dev *hdev, struct hci_conn *conn)
6693 return hci_cmd_sync_queue_once(hdev, hci_le_create_conn_sync, conn,
6697 int hci_cancel_connect_sync(struct hci_dev *hdev, struct hci_conn *conn)
6704 return !hci_cmd_sync_dequeue_once(hdev,
6708 return !hci_cmd_sync_dequeue_once(hdev, hci_le_create_conn_sync,