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 cancel_delayed_work(&hdev->le_scan_restart);
357 status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
359 bt_dev_err(hdev, "failed to disable LE scan: %d", status);
363 hdev->discovery.scan_start = 0;
373 if (hdev->discovery.type == DISCOV_TYPE_LE)
376 if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
379 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
380 if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
381 hdev->discovery.state != DISCOVERY_RESOLVING)
387 status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
389 bt_dev_err(hdev, "inquiry failed: status %d", status);
396 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
399 hci_dev_unlock(hdev);
402 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
404 static int hci_le_scan_restart_sync(struct hci_dev *hdev)
407 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
410 if (hdev->scanning_paused) {
411 bt_dev_dbg(hdev, "Scanning is paused for suspend");
415 hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
416 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE,
422 struct hci_dev *hdev = container_of(work, struct hci_dev,
427 bt_dev_dbg(hdev, "");
429 status = hci_le_scan_restart_sync(hdev);
431 bt_dev_err(hdev, "failed to restart LE scan: status %d",
436 hci_dev_lock(hdev);
438 if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
439 !hdev->discovery.scan_start)
442 /* When the scan was started, hdev->le_scan_disable has been queued
447 duration = hdev->discovery.scan_duration;
448 scan_start = hdev->discovery.scan_start;
463 queue_delayed_work(hdev->req_workqueue,
464 &hdev->le_scan_disable, timeout);
467 hci_dev_unlock(hdev);
470 static int reenable_adv_sync(struct hci_dev *hdev, void *data)
472 bt_dev_dbg(hdev, "");
474 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
475 list_empty(&hdev->adv_instances))
478 if (hdev->cur_adv_instance) {
479 return hci_schedule_adv_instance_sync(hdev,
480 hdev->cur_adv_instance,
483 if (ext_adv_capable(hdev)) {
484 hci_start_ext_adv_sync(hdev, 0x00);
486 hci_update_adv_data_sync(hdev, 0x00);
487 hci_update_scan_rsp_data_sync(hdev, 0x00);
488 hci_enable_advertising_sync(hdev);
497 struct hci_dev *hdev = container_of(work, struct hci_dev,
501 bt_dev_dbg(hdev, "");
503 hci_dev_lock(hdev);
505 status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
507 bt_dev_err(hdev, "failed to reenable ADV: %d", status);
509 hci_dev_unlock(hdev);
512 static void cancel_adv_timeout(struct hci_dev *hdev)
514 if (hdev->adv_instance_timeout) {
515 hdev->adv_instance_timeout = 0;
516 cancel_delayed_work(&hdev->adv_instance_expire);
531 int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
539 if (!instance || hdev->cur_adv_instance == instance)
540 cancel_adv_timeout(hdev);
546 if (instance && hdev->cur_adv_instance == instance)
547 next_instance = hci_get_next_instance(hdev, instance);
550 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
556 err = hci_remove_adv_instance(hdev, rem_inst);
558 mgmt_advertising_removed(sk, hdev, rem_inst);
561 adv_instance = hci_find_adv_instance(hdev, instance);
570 err = hci_remove_adv_instance(hdev, instance);
572 mgmt_advertising_removed(sk, hdev, instance);
576 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
579 if (next_instance && !ext_adv_capable(hdev))
580 return hci_schedule_adv_instance_sync(hdev,
587 static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
593 hci_clear_adv_instance_sync(hdev, NULL, instance, false);
595 if (list_empty(&hdev->adv_instances))
596 return hci_disable_advertising_sync(hdev);
604 struct hci_dev *hdev = container_of(work, struct hci_dev,
607 bt_dev_dbg(hdev, "");
609 hci_dev_lock(hdev);
611 hdev->adv_instance_timeout = 0;
613 if (hdev->cur_adv_instance == 0x00)
620 *inst_ptr = hdev->cur_adv_instance;
621 hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
624 hci_dev_unlock(hdev);
627 void hci_cmd_sync_init(struct hci_dev *hdev)
629 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
630 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
631 mutex_init(&hdev->cmd_sync_work_lock);
632 mutex_init(&hdev->unregister_lock);
634 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
635 INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
636 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
637 INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart);
638 INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
641 void hci_cmd_sync_clear(struct hci_dev *hdev)
645 cancel_work_sync(&hdev->cmd_sync_work);
646 cancel_work_sync(&hdev->reenable_adv_work);
648 mutex_lock(&hdev->cmd_sync_work_lock);
649 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
651 entry->destroy(hdev, entry->data, -ECANCELED);
656 mutex_unlock(&hdev->cmd_sync_work_lock);
659 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
661 bt_dev_dbg(hdev, "err 0x%2.2x", err);
663 if (hdev->req_status == HCI_REQ_PEND) {
664 hdev->req_result = err;
665 hdev->req_status = HCI_REQ_CANCELED;
667 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
677 void hci_cmd_sync_cancel_sync(struct hci_dev *hdev, int err)
679 bt_dev_dbg(hdev, "err 0x%2.2x", err);
681 if (hdev->req_status == HCI_REQ_PEND) {
682 hdev->req_result = err;
683 hdev->req_status = HCI_REQ_CANCELED;
685 wake_up_interruptible(&hdev->req_wait_q);
692 * - hdev must _not_ be unregistered
694 int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
700 mutex_lock(&hdev->unregister_lock);
701 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
715 mutex_lock(&hdev->cmd_sync_work_lock);
716 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
717 mutex_unlock(&hdev->cmd_sync_work_lock);
719 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
722 mutex_unlock(&hdev->unregister_lock);
729 * - hdev must be running
731 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
734 /* Only queue command if hdev is running which means it had been opened
737 if (!test_bit(HCI_RUNNING, &hdev->flags))
740 return hci_cmd_sync_submit(hdev, func, data, destroy);
744 int hci_update_eir_sync(struct hci_dev *hdev)
748 bt_dev_dbg(hdev, "");
750 if (!hdev_is_powered(hdev))
753 if (!lmp_ext_inq_capable(hdev))
756 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
759 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
764 eir_create(hdev, cp.data);
766 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
769 memcpy(hdev->eir, cp.data, sizeof(cp.data));
771 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
775 static u8 get_service_classes(struct hci_dev *hdev)
780 list_for_each_entry(uuid, &hdev->uuids, list)
786 int hci_update_class_sync(struct hci_dev *hdev)
790 bt_dev_dbg(hdev, "");
792 if (!hdev_is_powered(hdev))
795 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
798 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
801 cod[0] = hdev->minor_class;
802 cod[1] = hdev->major_class;
803 cod[2] = get_service_classes(hdev);
805 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
808 if (memcmp(cod, hdev->dev_class, 3) == 0)
811 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
815 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
818 if (hci_conn_num(hdev, LE_LINK) == 0)
822 if (hdev->conn_hash.le_num_peripheral > 0) {
826 if (!connectable && !(hdev->le_states[2] & 0x10))
832 if (connectable && (!(hdev->le_states[4] & 0x40) ||
833 !(hdev->le_states[2] & 0x20)))
838 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
840 if (!connectable && !(hdev->le_states[2] & 0x02))
846 if (connectable && (!(hdev->le_states[4] & 0x08) ||
847 !(hdev->le_states[2] & 0x08)))
854 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
857 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
861 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
868 hci_dev_test_flag(hdev, HCI_BONDABLE))
877 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
889 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
890 hci_lookup_le_connect(hdev)) {
891 bt_dev_dbg(hdev, "Deferring random address update");
892 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
896 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
900 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
913 if (use_ll_privacy(hdev))
919 if (rpa_valid(hdev))
922 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
924 bt_dev_err(hdev, "failed to generate new RPA");
928 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
953 if (bacmp(&hdev->bdaddr, &nrpa))
959 return hci_set_random_addr_sync(hdev, &nrpa);
971 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
972 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
973 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
974 bacmp(&hdev->static_addr, BDADDR_ANY))) {
976 if (bacmp(&hdev->static_addr, &hdev->random_addr))
977 return hci_set_random_addr_sync(hdev,
978 &hdev->static_addr);
990 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1001 adv = hci_find_adv_instance(hdev, instance);
1023 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1027 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1035 * hdev->random_addr to track its address so whenever it needs
1037 * hdev->random_addr is shared with scan state machine.
1039 err = hci_set_random_addr_sync(hdev, random_addr);
1049 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1053 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1065 adv = hci_find_adv_instance(hdev, instance);
1077 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1082 flags = hci_adv_instance_flags(hdev, instance);
1088 mgmt_get_connectable(hdev);
1090 if (!is_advertising_allowed(hdev, connectable))
1097 err = hci_get_random_address(hdev, !connectable,
1098 adv_use_rpa(hdev, flags), adv,
1110 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1111 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1122 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1143 hci_copy_identity_address(hdev, &cp.peer_addr,
1147 cp.channel_map = hdev->le_adv_channel_map;
1162 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1175 if (!bacmp(&random_addr, &hdev->random_addr))
1179 return hci_set_adv_set_random_addr_sync(hdev, instance,
1186 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1199 adv = hci_find_adv_instance(hdev, instance);
1204 len = eir_create_scan_rsp(hdev, instance, pdu.data);
1211 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1220 memcpy(hdev->scan_rsp_data, pdu.data, len);
1221 hdev->scan_rsp_data_len = len;
1227 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1234 len = eir_create_scan_rsp(hdev, instance, cp.data);
1236 if (hdev->scan_rsp_data_len == len &&
1237 !memcmp(cp.data, hdev->scan_rsp_data, len))
1240 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1241 hdev->scan_rsp_data_len = len;
1245 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1249 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1251 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1254 if (ext_adv_capable(hdev))
1255 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1257 return __hci_set_scan_rsp_data_sync(hdev, instance);
1260 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1268 adv = hci_find_adv_instance(hdev, instance);
1300 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1306 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1310 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1314 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1318 return hci_enable_ext_advertising_sync(hdev, instance);
1321 static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1327 adv = hci_find_adv_instance(hdev, instance);
1336 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1340 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1358 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1362 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1373 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1379 len = eir_create_per_adv_data(hdev, instance, pdu.data);
1385 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1390 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1396 adv = hci_find_adv_instance(hdev, instance);
1405 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1412 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1442 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1444 return hci_update_adv_data_sync(hdev, adv->instance);
1447 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1455 hci_disable_per_advertising_sync(hdev, instance);
1458 adv = hci_find_adv_instance(hdev, instance);
1461 adv = hci_add_per_instance(hdev, instance, flags,
1473 err = hci_start_ext_adv_sync(hdev, instance);
1477 err = hci_adv_bcast_annoucement(hdev, adv);
1481 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1486 err = hci_set_per_adv_data_sync(hdev, instance);
1490 err = hci_enable_per_advertising_sync(hdev, instance);
1498 hci_remove_adv_instance(hdev, instance);
1503 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1507 if (ext_adv_capable(hdev))
1508 return hci_start_ext_adv_sync(hdev, instance);
1510 err = hci_update_adv_data_sync(hdev, instance);
1514 err = hci_update_scan_rsp_data_sync(hdev, instance);
1518 return hci_enable_advertising_sync(hdev);
1521 int hci_enable_advertising_sync(struct hci_dev *hdev)
1531 if (ext_adv_capable(hdev))
1532 return hci_enable_ext_advertising_sync(hdev,
1533 hdev->cur_adv_instance);
1535 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1536 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1542 mgmt_get_connectable(hdev);
1544 if (!is_advertising_allowed(hdev, connectable))
1547 status = hci_disable_advertising_sync(hdev);
1556 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1562 status = hci_update_random_address_sync(hdev, !connectable,
1563 adv_use_rpa(hdev, flags),
1574 adv_min_interval = hdev->le_adv_min_interval;
1575 adv_max_interval = hdev->le_adv_max_interval;
1581 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1586 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1587 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1596 cp.channel_map = hdev->le_adv_channel_map;
1598 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1603 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1607 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1609 return hci_enable_advertising_sync(hdev);
1612 int hci_enable_advertising(struct hci_dev *hdev)
1614 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1615 list_empty(&hdev->adv_instances))
1618 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1621 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1626 if (!ext_adv_capable(hdev))
1629 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1634 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1637 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1642 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1650 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1653 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1658 adv = hci_find_adv_instance(hdev, instance);
1663 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1666 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1674 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1678 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1691 adv = hci_find_adv_instance(hdev, instance);
1696 len = eir_create_adv_data(hdev, instance, pdu.data);
1703 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1713 memcpy(hdev->adv_data, pdu.data, len);
1714 hdev->adv_data_len = len;
1720 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1727 len = eir_create_adv_data(hdev, instance, cp.data);
1730 if (hdev->adv_data_len == len &&
1731 memcmp(cp.data, hdev->adv_data, len) == 0)
1734 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1735 hdev->adv_data_len = len;
1739 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1743 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1745 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1748 if (ext_adv_capable(hdev))
1749 return hci_set_ext_adv_data_sync(hdev, instance);
1751 return hci_set_adv_data_sync(hdev, instance);
1754 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1760 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1763 if (hdev->adv_instance_timeout)
1766 adv = hci_find_adv_instance(hdev, instance);
1790 if (!ext_adv_capable(hdev)) {
1791 hdev->adv_instance_timeout = timeout;
1792 queue_delayed_work(hdev->req_workqueue,
1793 &hdev->adv_instance_expire,
1801 if (!force && hdev->cur_adv_instance == instance &&
1802 hci_dev_test_flag(hdev, HCI_LE_ADV))
1805 hdev->cur_adv_instance = instance;
1807 return hci_start_adv_sync(hdev, instance);
1810 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1814 if (!ext_adv_capable(hdev))
1818 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1822 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1826 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1831 if (ext_adv_capable(hdev))
1833 err = hci_clear_adv_sets_sync(hdev, sk);
1834 if (ext_adv_capable(hdev))
1840 hci_dev_lock(hdev);
1843 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1850 err = hci_remove_adv_instance(hdev, instance);
1852 mgmt_advertising_removed(sk, hdev, instance);
1855 hci_dev_unlock(hdev);
1860 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1866 if (ext_adv_capable(hdev))
1867 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1868 if (ext_adv_capable(hdev))
1874 hci_dev_lock(hdev);
1876 err = hci_remove_adv_instance(hdev, instance);
1878 mgmt_advertising_removed(sk, hdev, instance);
1880 hci_dev_unlock(hdev);
1896 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1903 if (!instance || hdev->cur_adv_instance == instance)
1904 cancel_adv_timeout(hdev);
1910 if (hdev->cur_adv_instance == instance)
1911 next = hci_get_next_instance(hdev, instance);
1914 err = hci_clear_adv_sync(hdev, sk, force);
1918 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1925 err = hci_remove_adv_sync(hdev, instance, sk);
1931 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1934 if (next && !ext_adv_capable(hdev))
1935 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1940 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1945 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1949 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1951 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1955 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1961 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1965 int hci_disable_advertising_sync(struct hci_dev *hdev)
1971 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1974 if (ext_adv_capable(hdev))
1975 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1976 if (ext_adv_capable(hdev))
1979 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1983 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1991 if (hci_dev_test_flag(hdev, HCI_MESH))
1996 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
2000 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2005 if (use_ext_scan(hdev))
2006 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2011 if (val && hci_dev_test_flag(hdev, HCI_MESH))
2016 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2020 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2022 if (!use_ll_privacy(hdev))
2026 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2029 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2033 static int hci_scan_disable_sync(struct hci_dev *hdev)
2038 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2041 if (hdev->scanning_paused) {
2042 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2046 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2048 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2055 static bool scan_use_rpa(struct hci_dev *hdev)
2057 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2060 static void hci_start_interleave_scan(struct hci_dev *hdev)
2062 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2063 queue_delayed_work(hdev->req_workqueue,
2064 &hdev->interleave_scan, 0);
2067 static bool is_interleave_scanning(struct hci_dev *hdev)
2069 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2072 static void cancel_interleave_scan(struct hci_dev *hdev)
2074 bt_dev_dbg(hdev, "cancelling interleave scan");
2076 cancel_delayed_work_sync(&hdev->interleave_scan);
2078 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2084 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2093 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2094 !(list_empty(&hdev->pend_le_conns) &&
2095 list_empty(&hdev->pend_le_reports)) &&
2096 hci_get_adv_monitor_offload_ext(hdev) ==
2098 bool is_interleaving = is_interleave_scanning(hdev);
2101 hci_start_interleave_scan(hdev);
2102 bt_dev_dbg(hdev, "starting interleave scan");
2107 cancel_interleave_scan(hdev);
2113 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2119 if (!use_ll_privacy(hdev))
2123 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2131 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2135 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2142 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2151 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2153 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2156 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2160 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2174 * Setting params to NULL programs local hdev->irk
2176 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2184 if (!use_ll_privacy(hdev))
2191 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2194 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2195 memcpy(cp.peer_irk, hdev->irk, 16);
2199 irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
2204 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2218 p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2221 p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2228 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2229 memcpy(cp.local_irk, hdev->irk, 16);
2233 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2238 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2255 irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
2266 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2274 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2282 if (hdev->suspended &&
2284 hci_le_del_accept_list_sync(hdev, &params->addr,
2290 if (*num_entries >= hdev->le_accept_list_size)
2294 if (!use_ll_privacy(hdev) &&
2295 hci_find_irk_by_addr(hdev, &params->addr, params->addr_type))
2302 err = hci_le_add_resolve_list_sync(hdev, params);
2304 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2309 err = hci_le_set_privacy_mode_sync(hdev, params);
2311 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2316 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
2324 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2327 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2329 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2333 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2340 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2346 if (hdev->advertising_paused)
2349 bt_dev_dbg(hdev, "Pausing directed advertising");
2352 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2359 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2360 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2361 hdev->discov_timeout = 0;
2364 bt_dev_dbg(hdev, "Pausing advertising instances");
2369 err = hci_disable_advertising_sync(hdev);
2374 if (!ext_adv_capable(hdev))
2375 cancel_adv_timeout(hdev);
2377 hdev->advertising_paused = true;
2378 hdev->advertising_old_state = old_state;
2384 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2390 if (!hdev->advertising_paused)
2394 hdev->advertising_paused = false;
2395 if (hdev->advertising_old_state) {
2396 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2397 hdev->advertising_old_state = 0;
2400 bt_dev_dbg(hdev, "Resuming advertising instances");
2402 if (ext_adv_capable(hdev)) {
2404 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2405 err = hci_enable_ext_advertising_sync(hdev,
2411 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2418 err = hci_schedule_adv_instance_sync(hdev,
2419 hdev->cur_adv_instance,
2423 hdev->advertising_paused = false;
2428 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2432 if (!use_ll_privacy(hdev))
2435 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2441 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2442 hci_lookup_le_connect(hdev)) {
2443 bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2448 err = hci_pause_advertising_sync(hdev);
2450 bt_dev_err(hdev, "Pause advertising failed: %d", err);
2454 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2456 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2460 if (!err && scan_use_rpa(hdev))
2463 hci_resume_advertising_sync(hdev);
2467 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2473 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2503 /* No hdev->lock, but: addr, addr_type are immutable.
2539 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2552 if (use_ll_privacy(hdev)) {
2553 err = hci_pause_advertising_sync(hdev);
2555 bt_dev_err(hdev, "pause advertising failed: %d", err);
2564 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2566 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2576 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2577 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2581 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2584 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2592 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2614 params = conn_params_copy(&hdev->pend_le_conns, &n);
2621 err = hci_le_add_accept_list_sync(hdev, &params[i],
2636 params = conn_params_copy(&hdev->pend_le_reports, &n);
2643 err = hci_le_add_accept_list_sync(hdev, &params[i],
2658 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2659 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2660 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2667 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2669 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2672 if (use_ll_privacy(hdev))
2673 hci_resume_advertising_sync(hdev);
2679 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2696 if (scan_1m(hdev) || scan_2m(hdev)) {
2707 if (scan_coded(hdev)) {
2718 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2723 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2729 if (use_ext_scan(hdev))
2730 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2741 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2745 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2751 if (hdev->scanning_paused) {
2752 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2756 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2761 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2764 static int hci_passive_scan_sync(struct hci_dev *hdev)
2772 if (hdev->scanning_paused) {
2773 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2777 err = hci_scan_disable_sync(hdev);
2779 bt_dev_err(hdev, "disable scanning failed: %d", err);
2789 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2793 if (hdev->enable_advmon_interleave_scan &&
2794 hci_update_interleaved_scan_sync(hdev))
2797 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2803 filter_policy = hci_update_accept_list_sync(hdev);
2814 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2815 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2818 if (hdev->suspended) {
2819 window = hdev->le_scan_window_suspend;
2820 interval = hdev->le_scan_int_suspend;
2821 } else if (hci_is_le_conn_scanning(hdev)) {
2822 window = hdev->le_scan_window_connect;
2823 interval = hdev->le_scan_int_connect;
2824 } else if (hci_is_adv_monitoring(hdev)) {
2825 window = hdev->le_scan_window_adv_monitor;
2826 interval = hdev->le_scan_int_adv_monitor;
2828 window = hdev->le_scan_window;
2829 interval = hdev->le_scan_interval;
2833 if (hci_dev_test_flag(hdev, HCI_MESH)) {
2838 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2840 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2844 /* This function controls the passive scanning based on hdev->pend_le_conns
2859 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2863 if (!test_bit(HCI_UP, &hdev->flags) ||
2864 test_bit(HCI_INIT, &hdev->flags) ||
2865 hci_dev_test_flag(hdev, HCI_SETUP) ||
2866 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2867 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2868 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2872 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2876 if (hdev->discovery.state != DISCOVERY_STOPPED)
2886 hci_discovery_filter_clear(hdev);
2888 bt_dev_dbg(hdev, "ADV monitoring is %s",
2889 hci_is_adv_monitoring(hdev) ? "on" : "off");
2891 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
2892 list_empty(&hdev->pend_le_conns) &&
2893 list_empty(&hdev->pend_le_reports) &&
2894 !hci_is_adv_monitoring(hdev) &&
2895 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2901 bt_dev_dbg(hdev, "stopping background scanning");
2903 err = hci_scan_disable_sync(hdev);
2905 bt_dev_err(hdev, "stop background scanning failed: %d",
2916 if (hci_lookup_le_connect(hdev))
2919 bt_dev_dbg(hdev, "start background scanning");
2921 err = hci_passive_scan_sync(hdev);
2923 bt_dev_err(hdev, "start background scanning failed: %d",
2930 static int update_scan_sync(struct hci_dev *hdev, void *data)
2932 return hci_update_scan_sync(hdev);
2935 int hci_update_scan(struct hci_dev *hdev)
2937 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
2940 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2942 return hci_update_passive_scan_sync(hdev);
2945 int hci_update_passive_scan(struct hci_dev *hdev)
2948 if (!test_bit(HCI_UP, &hdev->flags) ||
2949 test_bit(HCI_INIT, &hdev->flags) ||
2950 hci_dev_test_flag(hdev, HCI_SETUP) ||
2951 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2952 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2953 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2956 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2959 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2963 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2966 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2971 hdev->features[1][0] |= LMP_HOST_SC;
2972 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2974 hdev->features[1][0] &= ~LMP_HOST_SC;
2975 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2982 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2986 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2987 lmp_host_ssp_capable(hdev))
2990 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
2991 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2995 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3000 return hci_write_sc_support_sync(hdev, 0x01);
3003 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
3007 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
3008 !lmp_bredr_capable(hdev))
3014 if (le == lmp_host_le_capable(hdev) &&
3015 simul == lmp_host_le_br_capable(hdev))
3023 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3027 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3032 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3040 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3041 hci_le_add_resolve_list_sync(hdev, NULL);
3042 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3049 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3050 list_empty(&hdev->adv_instances)) {
3051 if (ext_adv_capable(hdev)) {
3052 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3054 hci_update_scan_rsp_data_sync(hdev, 0x00);
3056 err = hci_update_adv_data_sync(hdev, 0x00);
3058 hci_update_scan_rsp_data_sync(hdev, 0x00);
3061 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3062 hci_enable_advertising_sync(hdev);
3066 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3067 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3072 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3076 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3077 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3080 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3085 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3091 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3094 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3105 type = hdev->def_page_scan_type;
3106 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3109 cp.window = cpu_to_le16(hdev->def_page_scan_window);
3111 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3112 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3113 err = __hci_cmd_sync_status(hdev,
3120 if (hdev->page_scan_type != type)
3121 err = __hci_cmd_sync_status(hdev,
3129 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3133 list_for_each_entry(b, &hdev->accept_list, list) {
3136 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3147 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3149 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3154 int hci_update_scan_sync(struct hci_dev *hdev)
3158 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3161 if (!hdev_is_powered(hdev))
3164 if (mgmt_powering_down(hdev))
3167 if (hdev->scanning_paused)
3170 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3171 disconnected_accept_list_entries(hdev))
3176 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3179 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3180 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3183 return hci_write_scan_enable_sync(hdev, scan);
3186 int hci_update_name_sync(struct hci_dev *hdev)
3192 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3194 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3211 int hci_powered_update_sync(struct hci_dev *hdev)
3220 smp_register(hdev);
3222 err = hci_write_ssp_mode_sync(hdev, 0x01);
3226 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3230 err = hci_powered_update_adv_sync(hdev);
3234 err = hci_write_auth_enable_sync(hdev);
3238 if (lmp_bredr_capable(hdev)) {
3239 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3240 hci_write_fast_connectable_sync(hdev, true);
3242 hci_write_fast_connectable_sync(hdev, false);
3243 hci_update_scan_sync(hdev);
3244 hci_update_class_sync(hdev);
3245 hci_update_name_sync(hdev);
3246 hci_update_eir_sync(hdev);
3258 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3259 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3260 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3261 if (bacmp(&hdev->static_addr, BDADDR_ANY))
3262 return hci_set_random_addr_sync(hdev,
3263 &hdev->static_addr);
3273 * @hdev: The HCI device
3281 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3283 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3292 bacpy(&hdev->public_addr, &ba);
3296 int (*func)(struct hci_dev *hdev);
3300 static int hci_init_stage_sync(struct hci_dev *hdev,
3308 err = stage[i].func(hdev);
3317 static int hci_read_local_version_sync(struct hci_dev *hdev)
3319 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3324 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3326 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3343 int hci_reset_sync(struct hci_dev *hdev)
3347 set_bit(HCI_RESET, &hdev->flags);
3349 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3357 static int hci_init0_sync(struct hci_dev *hdev)
3361 bt_dev_dbg(hdev, "");
3364 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3365 err = hci_reset_sync(hdev);
3370 return hci_init_stage_sync(hdev, hci_init0);
3373 static int hci_unconf_init_sync(struct hci_dev *hdev)
3377 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3380 err = hci_init0_sync(hdev);
3384 if (hci_dev_test_flag(hdev, HCI_SETUP))
3385 hci_debugfs_create_basic(hdev);
3391 static int hci_read_local_features_sync(struct hci_dev *hdev)
3394 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
3397 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3413 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3423 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3424 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3425 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3432 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
3434 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
3439 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
3441 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
3446 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
3448 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
3453 static int hci_read_location_data_sync(struct hci_dev *hdev)
3455 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
3476 static int hci_init1_sync(struct hci_dev *hdev)
3480 bt_dev_dbg(hdev, "");
3483 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3484 err = hci_reset_sync(hdev);
3489 switch (hdev->dev_type) {
3491 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3492 return hci_init_stage_sync(hdev, br_init1);
3494 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3495 return hci_init_stage_sync(hdev, amp_init1);
3497 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
3512 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3514 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3519 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3521 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3526 static int hci_read_local_name_sync(struct hci_dev *hdev)
3528 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3533 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3535 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3540 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3542 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3547 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3549 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3553 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3559 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3562 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3574 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3580 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3582 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3589 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3592 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3597 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3601 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3626 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3630 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3639 hdev->max_page = 0x01;
3641 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3645 static int hci_write_eir_sync(struct hci_dev *hdev)
3649 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3652 memset(hdev->eir, 0, sizeof(hdev->eir));
3655 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3659 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3663 if (!lmp_inq_rssi_capable(hdev) &&
3664 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3671 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3673 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3677 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3679 if (!lmp_inq_tx_pwr_capable(hdev))
3682 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3686 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3690 if (!lmp_ext_feat_capable(hdev))
3696 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3700 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3702 return hci_read_local_ext_features_sync(hdev, 0x01);
3725 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3728 if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3729 return __hci_cmd_sync_status(hdev,
3733 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3738 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3740 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3745 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3747 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3762 static int hci_init2_sync(struct hci_dev *hdev)
3766 bt_dev_dbg(hdev, "");
3768 if (hdev->dev_type == HCI_AMP)
3769 return hci_init_stage_sync(hdev, amp_init2);
3771 err = hci_init_stage_sync(hdev, hci_init2);
3775 if (lmp_bredr_capable(hdev)) {
3776 err = hci_init_stage_sync(hdev, br_init2);
3780 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3783 if (lmp_le_capable(hdev)) {
3784 err = hci_init_stage_sync(hdev, le_init2);
3788 if (!lmp_bredr_capable(hdev))
3789 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3795 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3806 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3809 if (lmp_bredr_capable(hdev)) {
3816 if (hdev->suspended) {
3831 if (hdev->commands[0] & 0x20) {
3836 if (!hdev->suspended)
3845 if (hdev->commands[2] & 0x80)
3850 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3856 if (lmp_inq_rssi_capable(hdev) ||
3857 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3860 if (lmp_ext_feat_capable(hdev))
3863 if (lmp_esco_capable(hdev)) {
3868 if (lmp_sniffsubr_capable(hdev))
3871 if (lmp_pause_enc_capable(hdev))
3874 if (lmp_ext_inq_capable(hdev))
3877 if (lmp_no_flush_capable(hdev))
3880 if (lmp_lsto_capable(hdev))
3883 if (lmp_ssp_capable(hdev)) {
3897 if (lmp_le_capable(hdev))
3900 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3904 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3908 if (!(hdev->commands[6] & 0x20) ||
3909 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3916 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3920 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3925 if (!(hdev->commands[5] & 0x10))
3930 if (lmp_rswitch_capable(hdev))
3932 if (lmp_hold_capable(hdev))
3934 if (lmp_sniff_capable(hdev))
3936 if (lmp_park_capable(hdev))
3941 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3945 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3947 if (!(hdev->commands[8] & 0x01))
3950 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3954 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3956 if (!(hdev->commands[18] & 0x04) ||
3957 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
3958 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
3961 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3965 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3971 if (!(hdev->commands[13] & 0x01))
3974 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3979 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3984 if (!lmp_ext_feat_capable(hdev))
3987 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3989 err = hci_read_local_ext_features_sync(hdev, page);
4016 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4020 if (!lmp_le_capable(hdev))
4025 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4031 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4038 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4044 if (use_enhanced_conn_complete(hdev))
4050 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4056 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4062 if (hdev->commands[26] & 0x08)
4068 if (hdev->commands[26] & 0x10)
4074 if (hdev->commands[27] & 0x04)
4080 if (hdev->commands[27] & 0x20)
4087 if (hdev->commands[34] & 0x02)
4094 if (hdev->commands[34] & 0x04)
4100 if (hdev->commands[35] & (0x20 | 0x40))
4107 if (use_ext_scan(hdev))
4113 if (ext_adv_capable(hdev))
4116 if (cis_capable(hdev)) {
4118 if (cis_peripheral_capable(hdev))
4122 if (bis_capable(hdev)) {
4132 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4137 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4139 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4146 return __hci_cmd_sync_status(hdev,
4155 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4157 if (!(hdev->commands[38] & 0x80) ||
4158 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4161 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4166 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4168 if (!(hdev->commands[26] & 0x40))
4171 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4176 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
4178 if (!(hdev->commands[26] & 0x80))
4181 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
4186 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4188 if (!(hdev->commands[34] & 0x40))
4191 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4196 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4198 if (!(hdev->commands[34] & 0x20))
4201 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4206 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4208 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4210 if (!(hdev->commands[35] & 0x04) ||
4211 test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4214 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4220 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4222 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4225 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4230 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4232 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4235 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4240 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4242 if (!ext_adv_capable(hdev))
4245 return __hci_cmd_sync_status(hdev,
4251 static int hci_set_le_support_sync(struct hci_dev *hdev)
4256 if (!lmp_bredr_capable(hdev))
4261 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4266 if (cp.le == lmp_host_le_capable(hdev))
4269 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4274 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4278 if (!iso_capable(hdev))
4287 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4322 static int hci_init3_sync(struct hci_dev *hdev)
4326 bt_dev_dbg(hdev, "");
4328 err = hci_init_stage_sync(hdev, hci_init3);
4332 if (lmp_le_capable(hdev))
4333 return hci_init_stage_sync(hdev, le_init3);
4338 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4355 if (!(hdev->commands[6] & 0x80) ||
4356 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4363 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4367 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4373 if (!(hdev->commands[22] & 0x04))
4379 if (lmp_cpb_central_capable(hdev)) {
4390 if (lmp_cpb_peripheral_capable(hdev)) {
4399 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4413 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4418 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4420 if (hdev->commands[45] & 0x04)
4421 hci_read_supported_codecs_v2(hdev);
4422 else if (hdev->commands[29] & 0x20)
4423 hci_read_supported_codecs(hdev);
4429 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4431 if (!(hdev->commands[41] & 0x08))
4434 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4439 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4441 if (!mws_transport_config_capable(hdev))
4444 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4449 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4451 if (!lmp_sync_train_capable(hdev))
4454 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4459 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4463 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4464 !bredr_sc_enabled(hdev))
4467 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4475 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4478 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4480 if (!(hdev->commands[18] & 0x08) ||
4481 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4482 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4485 if (enabled == hdev->err_data_reporting)
4492 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4517 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4521 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4525 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4526 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4528 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4535 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4539 if (!(hdev->commands[35] & 0x20)) {
4543 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4544 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4554 if (le_2m_capable(hdev)) {
4560 if (le_coded_capable(hdev)) {
4565 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4577 static int hci_init4_sync(struct hci_dev *hdev)
4581 bt_dev_dbg(hdev, "");
4583 err = hci_init_stage_sync(hdev, hci_init4);
4587 if (lmp_le_capable(hdev))
4588 return hci_init_stage_sync(hdev, le_init4);
4593 static int hci_init_sync(struct hci_dev *hdev)
4597 err = hci_init1_sync(hdev);
4601 if (hci_dev_test_flag(hdev, HCI_SETUP))
4602 hci_debugfs_create_basic(hdev);
4604 err = hci_init2_sync(hdev);
4612 if (hdev->dev_type != HCI_PRIMARY)
4615 err = hci_init3_sync(hdev);
4619 err = hci_init4_sync(hdev);
4635 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4636 !hci_dev_test_flag(hdev, HCI_CONFIG))
4639 if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4642 hci_debugfs_create_common(hdev);
4644 if (lmp_bredr_capable(hdev))
4645 hci_debugfs_create_bredr(hdev);
4647 if (lmp_le_capable(hdev))
4648 hci_debugfs_create_le(hdev);
4683 /* This function handles hdev setup stage:
4685 * Calls hdev->setup
4688 static int hci_dev_setup_sync(struct hci_dev *hdev)
4694 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4695 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4698 bt_dev_dbg(hdev, "");
4700 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4702 if (hdev->setup)
4703 ret = hdev->setup(hdev);
4706 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4707 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4714 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
4715 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
4717 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4718 !bacmp(&hdev->public_addr, BDADDR_ANY))
4719 hci_dev_get_bd_addr_from_property(hdev);
4721 if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
4722 hdev->set_bdaddr) {
4723 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4739 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4741 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4751 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4752 return hci_unconf_init_sync(hdev);
4757 /* This function handles hdev init stage:
4762 static int hci_dev_init_sync(struct hci_dev *hdev)
4766 bt_dev_dbg(hdev, "");
4768 atomic_set(&hdev->cmd_cnt, 1);
4769 set_bit(HCI_INIT, &hdev->flags);
4771 ret = hci_dev_setup_sync(hdev);
4773 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4779 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4780 hdev->set_bdaddr)
4781 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4787 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4788 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4789 ret = hci_init_sync(hdev);
4790 if (!ret && hdev->post_init)
4791 ret = hdev->post_init(hdev);
4799 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4800 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4801 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4802 ret = hdev->set_diag(hdev, true);
4804 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4805 msft_do_open(hdev);
4806 aosp_do_open(hdev);
4809 clear_bit(HCI_INIT, &hdev->flags);
4814 int hci_dev_open_sync(struct hci_dev *hdev)
4818 bt_dev_dbg(hdev, "");
4820 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4825 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4826 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4830 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4847 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4848 hdev->dev_type == HCI_PRIMARY &&
4849 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4850 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4856 if (test_bit(HCI_UP, &hdev->flags)) {
4861 if (hdev->open(hdev)) {
4866 hci_devcd_reset(hdev);
4868 set_bit(HCI_RUNNING, &hdev->flags);
4869 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4871 ret = hci_dev_init_sync(hdev);
4873 hci_dev_hold(hdev);
4874 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4875 hci_adv_instances_set_rpa_expired(hdev, true);
4876 set_bit(HCI_UP, &hdev->flags);
4877 hci_sock_dev_event(hdev, HCI_DEV_UP);
4878 hci_leds_update_powered(hdev, true);
4879 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4880 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4881 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4882 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4883 hci_dev_test_flag(hdev, HCI_MGMT) &&
4884 hdev->dev_type == HCI_PRIMARY) {
4885 ret = hci_powered_update_sync(hdev);
4886 mgmt_power_on(hdev, ret);
4890 flush_work(&hdev->tx_work);
4896 flush_work(&hdev->rx_work);
4897 flush_work(&hdev->cmd_work);
4899 skb_queue_purge(&hdev->cmd_q);
4900 skb_queue_purge(&hdev->rx_q);
4902 if (hdev->flush)
4903 hdev->flush(hdev);
4905 if (hdev->sent_cmd) {
4906 cancel_delayed_work_sync(&hdev->cmd_timer);
4907 kfree_skb(hdev->sent_cmd);
4908 hdev->sent_cmd = NULL;
4911 if (hdev->req_skb) {
4912 kfree_skb(hdev->req_skb);
4913 hdev->req_skb = NULL;
4916 clear_bit(HCI_RUNNING, &hdev->flags);
4917 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4919 hdev->close(hdev);
4920 hdev->flags &= BIT(HCI_RAW);
4927 /* This function requires the caller holds hdev->lock */
4928 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4932 list_for_each_entry(p, &hdev->le_conn_params, list) {
4944 static int hci_dev_shutdown(struct hci_dev *hdev)
4954 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
4956 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4957 test_bit(HCI_UP, &hdev->flags)) {
4959 if (hdev->shutdown)
4960 err = hdev->shutdown(hdev);
4964 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
4969 int hci_dev_close_sync(struct hci_dev *hdev)
4974 bt_dev_dbg(hdev, "");
4976 cancel_delayed_work(&hdev->power_off);
4977 cancel_delayed_work(&hdev->ncmd_timer);
4978 cancel_delayed_work(&hdev->le_scan_disable);
4979 cancel_delayed_work(&hdev->le_scan_restart);
4981 hci_request_cancel_all(hdev);
4983 if (hdev->adv_instance_timeout) {
4984 cancel_delayed_work_sync(&hdev->adv_instance_expire);
4985 hdev->adv_instance_timeout = 0;
4988 err = hci_dev_shutdown(hdev);
4990 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4991 cancel_delayed_work_sync(&hdev->cmd_timer);
4995 hci_leds_update_powered(hdev, false);
4998 flush_work(&hdev->tx_work);
4999 flush_work(&hdev->rx_work);
5001 if (hdev->discov_timeout > 0) {
5002 hdev->discov_timeout = 0;
5003 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5004 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
5007 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
5008 cancel_delayed_work(&hdev->service_cache);
5010 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
5013 cancel_delayed_work_sync(&hdev->rpa_expired);
5015 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5022 drain_workqueue(hdev->workqueue);
5024 hci_dev_lock(hdev);
5026 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5028 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5030 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
5031 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5032 hci_dev_test_flag(hdev, HCI_MGMT))
5033 __mgmt_power_off(hdev);
5035 hci_inquiry_cache_flush(hdev);
5036 hci_pend_le_actions_clear(hdev);
5037 hci_conn_hash_flush(hdev);
5038 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5039 smp_unregister(hdev);
5040 hci_dev_unlock(hdev);
5042 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5044 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5045 aosp_do_close(hdev);
5046 msft_do_close(hdev);
5049 if (hdev->flush)
5050 hdev->flush(hdev);
5053 skb_queue_purge(&hdev->cmd_q);
5054 atomic_set(&hdev->cmd_cnt, 1);
5055 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5056 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5057 set_bit(HCI_INIT, &hdev->flags);
5058 hci_reset_sync(hdev);
5059 clear_bit(HCI_INIT, &hdev->flags);
5063 flush_work(&hdev->cmd_work);
5066 skb_queue_purge(&hdev->rx_q);
5067 skb_queue_purge(&hdev->cmd_q);
5068 skb_queue_purge(&hdev->raw_q);
5071 if (hdev->sent_cmd) {
5072 cancel_delayed_work_sync(&hdev->cmd_timer);
5073 kfree_skb(hdev->sent_cmd);
5074 hdev->sent_cmd = NULL;
5078 if (hdev->req_skb) {
5079 kfree_skb(hdev->req_skb);
5080 hdev->req_skb = NULL;
5083 clear_bit(HCI_RUNNING, &hdev->flags);
5084 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5087 hdev->close(hdev);
5090 hdev->flags &= BIT(HCI_RAW);
5091 hci_dev_clear_volatile_flags(hdev);
5094 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
5096 memset(hdev->eir, 0, sizeof(hdev->eir));
5097 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5098 bacpy(&hdev->random_addr, BDADDR_ANY);
5099 hci_codec_list_clear(&hdev->local_codecs);
5101 hci_dev_put(hdev);
5111 static int hci_power_on_sync(struct hci_dev *hdev)
5115 if (test_bit(HCI_UP, &hdev->flags) &&
5116 hci_dev_test_flag(hdev, HCI_MGMT) &&
5117 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5118 cancel_delayed_work(&hdev->power_off);
5119 return hci_powered_update_sync(hdev);
5122 err = hci_dev_open_sync(hdev);
5130 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5131 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5132 (hdev->dev_type == HCI_PRIMARY &&
5133 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5134 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5135 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5136 hci_dev_close_sync(hdev);
5137 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5138 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5142 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5146 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5147 set_bit(HCI_RAW, &hdev->flags);
5156 mgmt_index_added(hdev);
5157 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5161 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5162 clear_bit(HCI_RAW, &hdev->flags);
5168 mgmt_index_added(hdev);
5174 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5181 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5185 int hci_stop_discovery_sync(struct hci_dev *hdev)
5187 struct discovery_state *d = &hdev->discovery;
5191 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5194 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5195 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5201 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5202 cancel_delayed_work(&hdev->le_scan_disable);
5203 cancel_delayed_work(&hdev->le_scan_restart);
5205 err = hci_scan_disable_sync(hdev);
5211 err = hci_scan_disable_sync(hdev);
5217 if (use_ll_privacy(hdev))
5218 hci_resume_advertising_sync(hdev);
5225 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5230 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5236 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
5245 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
5249 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5255 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
5267 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5272 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5276 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5289 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5293 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5297 return hci_le_connect_cancel_sync(hdev, conn, reason);
5309 return hci_disconnect_sync(hdev, conn, reason);
5321 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5330 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5335 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5339 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5354 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5358 static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
5367 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
5371 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5377 return hci_le_reject_cis_sync(hdev, conn, reason);
5380 return hci_reject_sco_sync(hdev, conn, reason);
5386 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5390 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5400 err = hci_disconnect_sync(hdev, conn, reason);
5403 err = hci_connect_cancel_sync(hdev, conn, reason);
5406 err = hci_reject_conn_sync(hdev, conn, reason);
5409 hci_dev_lock(hdev);
5422 hci_dev_unlock(hdev);
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 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5498 test_bit(HCI_PSCAN, &hdev->flags)) {
5499 err = hci_write_scan_enable_sync(hdev, 0x00);
5504 err = hci_clear_adv_sync(hdev, NULL, false);
5508 err = hci_stop_discovery_sync(hdev);
5513 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5517 return hci_dev_close_sync(hdev);
5520 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5523 return hci_power_on_sync(hdev);
5525 return hci_power_off_sync(hdev);
5528 static int hci_write_iac_sync(struct hci_dev *hdev)
5532 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5537 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5539 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5554 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5559 int hci_update_discoverable_sync(struct hci_dev *hdev)
5563 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5564 err = hci_write_iac_sync(hdev);
5568 err = hci_update_scan_sync(hdev);
5572 err = hci_update_class_sync(hdev);
5580 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5581 err = hci_update_adv_data_sync(hdev, 0x00);
5588 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5589 if (ext_adv_capable(hdev))
5590 err = hci_start_ext_adv_sync(hdev, 0x00);
5592 err = hci_enable_advertising_sync(hdev);
5599 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5601 return hci_update_discoverable_sync(hdev);
5604 int hci_update_discoverable(struct hci_dev *hdev)
5607 if (hdev_is_powered(hdev) &&
5608 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5609 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5610 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5611 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5617 int hci_update_connectable_sync(struct hci_dev *hdev)
5621 err = hci_update_scan_sync(hdev);
5629 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5630 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5633 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5634 !list_empty(&hdev->adv_instances)) {
5635 if (ext_adv_capable(hdev))
5636 err = hci_start_ext_adv_sync(hdev,
5637 hdev->cur_adv_instance);
5639 err = hci_enable_advertising_sync(hdev);
5645 return hci_update_passive_scan_sync(hdev);
5648 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5654 bt_dev_dbg(hdev, "");
5656 if (test_bit(HCI_INQUIRY, &hdev->flags))
5659 hci_dev_lock(hdev);
5660 hci_inquiry_cache_flush(hdev);
5661 hci_dev_unlock(hdev);
5665 if (hdev->discovery.limited)
5672 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5676 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5685 bt_dev_dbg(hdev, "");
5691 err = hci_scan_disable_sync(hdev);
5693 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5697 cancel_interleave_scan(hdev);
5702 err = hci_pause_addr_resolution(hdev);
5710 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5715 if (hci_is_adv_monitoring(hdev)) {
5732 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5733 hdev->le_scan_window_discovery,
5740 if (use_ll_privacy(hdev))
5741 hci_resume_advertising_sync(hdev);
5744 hci_update_passive_scan_sync(hdev);
5748 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5752 bt_dev_dbg(hdev, "");
5754 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5758 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5761 int hci_start_discovery_sync(struct hci_dev *hdev)
5766 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5768 switch (hdev->discovery.type) {
5770 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5781 &hdev->quirks)) {
5787 err = hci_start_interleaved_discovery_sync(hdev);
5791 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5792 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5796 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5805 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5812 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5813 hdev->discovery.result_filtering) {
5814 hdev->discovery.scan_start = jiffies;
5815 hdev->discovery.scan_duration = timeout;
5818 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5823 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5825 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5827 msft_suspend_sync(hdev);
5835 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5837 int old_state = hdev->discovery.state;
5842 hdev->discovery_paused)
5845 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5846 err = hci_stop_discovery_sync(hdev);
5850 hdev->discovery_paused = true;
5851 hdev->discovery_old_state = old_state;
5852 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5857 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5861 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5864 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5870 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5874 hci_clear_event_filter_sync(hdev);
5876 list_for_each_entry(b, &hdev->accept_list, list) {
5880 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5882 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5887 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5894 hci_write_scan_enable_sync(hdev, scan);
5896 hci_write_scan_enable_sync(hdev, scan);
5902 static int hci_pause_scan_sync(struct hci_dev *hdev)
5904 if (hdev->scanning_paused)
5908 if (test_bit(HCI_PSCAN, &hdev->flags))
5909 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
5911 hci_scan_disable_sync(hdev);
5913 hdev->scanning_paused = true;
5924 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5931 int hci_suspend_sync(struct hci_dev *hdev)
5936 if (hdev->suspended)
5940 hdev->suspended = true;
5943 hci_pause_discovery_sync(hdev);
5946 hci_pause_advertising_sync(hdev);
5949 hci_suspend_monitor_sync(hdev);
5952 hci_pause_scan_sync(hdev);
5954 if (hci_conn_count(hdev)) {
5956 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5959 hdev->suspend_state = BT_RUNNING;
5960 hci_resume_sync(hdev);
5967 hci_set_event_mask_sync(hdev);
5973 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5974 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5979 hdev->scanning_paused = false;
5982 hci_update_event_filter_sync(hdev);
5985 hci_update_passive_scan_sync(hdev);
5988 hdev->scanning_paused = true;
5990 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5996 static int hci_resume_discovery_sync(struct hci_dev *hdev)
6001 if (!hdev->discovery_paused)
6004 hdev->discovery_paused = false;
6006 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6008 err = hci_start_discovery_sync(hdev);
6010 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
6016 static void hci_resume_monitor_sync(struct hci_dev *hdev)
6018 switch (hci_get_adv_monitor_offload_ext(hdev)) {
6020 msft_resume_sync(hdev);
6028 static int hci_resume_scan_sync(struct hci_dev *hdev)
6030 if (!hdev->scanning_paused)
6033 hdev->scanning_paused = false;
6035 hci_update_scan_sync(hdev);
6038 hci_update_passive_scan_sync(hdev);
6051 int hci_resume_sync(struct hci_dev *hdev)
6054 if (!hdev->suspended)
6057 hdev->suspended = false;
6060 hci_set_event_mask_sync(hdev);
6063 hci_clear_event_filter_sync(hdev);
6066 hci_resume_scan_sync(hdev);
6069 hci_resume_monitor_sync(hdev);
6072 hci_resume_advertising_sync(hdev);
6075 hci_resume_discovery_sync(hdev);
6082 struct hci_dev *hdev = conn->hdev;
6084 return hci_dev_test_flag(hdev, HCI_PRIVACY);
6087 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
6095 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6103 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
6111 cp.channel_map = hdev->le_adv_channel_map;
6128 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
6132 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
6140 bacmp(&random_addr, &hdev->random_addr)) {
6141 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
6147 return hci_enable_ext_advertising_sync(hdev, 0x00);
6150 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
6158 if (ext_adv_capable(hdev))
6159 return hci_le_ext_directed_advertising_sync(hdev, conn);
6166 hci_dev_clear_flag(hdev, HCI_LE_ADV);
6171 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6189 cp.channel_map = hdev->le_adv_channel_map;
6191 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
6198 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
6205 struct hci_dev *hdev = conn->hdev;
6209 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6210 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6219 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
6238 if (scan_1m(hdev)) {
6246 if (scan_2m(hdev)) {
6254 if (scan_coded(hdev)) {
6261 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6267 int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
6279 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6280 hdev->le_scan_type == LE_SCAN_ACTIVE &&
6281 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6287 hci_pause_advertising_sync(hdev);
6289 err = hci_le_directed_advertising_sync(hdev, conn);
6294 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6295 hci_pause_advertising_sync(hdev);
6297 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6304 conn->le_conn_min_interval = hdev->le_conn_min_interval;
6305 conn->le_conn_max_interval = hdev->le_conn_max_interval;
6306 conn->le_conn_latency = hdev->le_conn_latency;
6307 conn->le_supv_timeout = hdev->le_supv_timeout;
6316 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6317 hci_scan_disable_sync(hdev);
6318 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6324 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6329 if (use_ext_conn(hdev)) {
6330 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6336 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6337 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6355 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6357 use_enhanced_conn_complete(hdev) ?
6364 hci_le_connect_cancel_sync(hdev, conn, 0x00);
6367 hci_resume_advertising_sync(hdev);
6371 int hci_le_create_cis_sync(struct hci_dev *hdev)
6405 hci_dev_lock(hdev);
6410 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6416 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6424 list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
6440 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6459 hci_dev_unlock(hdev);
6465 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6472 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6479 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6483 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6490 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6494 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6501 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6505 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6520 if (use_ll_privacy(hdev))
6529 if (rpa_valid(hdev))
6533 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6535 bt_dev_err(hdev, "failed to generate new RPA");
6539 bacpy(rand_addr, &hdev->rpa);
6562 if (bacmp(&hdev->bdaddr, &nrpa))
6578 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6582 return hci_update_adv_data_sync(hdev, instance);
6585 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6587 return hci_cmd_sync_queue(hdev, _update_adv_data_sync,