1 // SPDX-License-Identifier: GPL-2.0
3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2021 Intel Corporation
8 #include <linux/property.h>
10 #include <net/bluetooth/bluetooth.h>
11 #include <net/bluetooth/hci_core.h>
12 #include <net/bluetooth/mgmt.h>
14 #include "hci_request.h"
15 #include "hci_debugfs.h"
22 static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
25 bt_dev_dbg(hdev, "result 0x%2.2x", result);
27 if (hdev->req_status != HCI_REQ_PEND)
30 hdev->req_result = result;
31 hdev->req_status = HCI_REQ_DONE;
34 struct sock *sk = hci_skb_sk(skb);
36 /* Drop sk reference if set */
40 hdev->req_skb = skb_get(skb);
43 wake_up_interruptible(&hdev->req_wait_q);
46 static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
47 u32 plen, const void *param,
50 int len = HCI_COMMAND_HDR_SIZE + plen;
51 struct hci_command_hdr *hdr;
54 skb = bt_skb_alloc(len, GFP_ATOMIC);
58 hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
59 hdr->opcode = cpu_to_le16(opcode);
63 skb_put_data(skb, param, plen);
65 bt_dev_dbg(hdev, "skb len %d", skb->len);
67 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
68 hci_skb_opcode(skb) = opcode;
70 /* Grab a reference if command needs to be associated with a sock (e.g.
71 * likely mgmt socket that initiated the command).
81 static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
82 const void *param, u8 event, struct sock *sk)
84 struct hci_dev *hdev = req->hdev;
87 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
89 /* If an error occurred during request building, there is no point in
90 * queueing the HCI command. We can simply return.
95 skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
97 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
103 if (skb_queue_empty(&req->cmd_q))
104 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
106 hci_skb_event(skb) = event;
108 skb_queue_tail(&req->cmd_q, skb);
111 static int hci_cmd_sync_run(struct hci_request *req)
113 struct hci_dev *hdev = req->hdev;
117 bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
119 /* If an error occurred during request building, remove all HCI
120 * commands queued on the HCI request queue.
123 skb_queue_purge(&req->cmd_q);
127 /* Do not allow empty requests */
128 if (skb_queue_empty(&req->cmd_q))
131 skb = skb_peek_tail(&req->cmd_q);
132 bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
133 bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
135 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
136 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
137 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
139 queue_work(hdev->workqueue, &hdev->cmd_work);
144 /* This function requires the caller holds hdev->req_lock. */
145 struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
146 const void *param, u8 event, u32 timeout,
149 struct hci_request req;
153 bt_dev_dbg(hdev, "Opcode 0x%4x", opcode);
155 hci_req_init(&req, hdev);
157 hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
159 hdev->req_status = HCI_REQ_PEND;
161 err = hci_cmd_sync_run(&req);
165 err = wait_event_interruptible_timeout(hdev->req_wait_q,
166 hdev->req_status != HCI_REQ_PEND,
169 if (err == -ERESTARTSYS)
170 return ERR_PTR(-EINTR);
172 switch (hdev->req_status) {
174 err = -bt_to_errno(hdev->req_result);
177 case HCI_REQ_CANCELED:
178 err = -hdev->req_result;
186 hdev->req_status = 0;
187 hdev->req_result = 0;
189 hdev->req_skb = NULL;
191 bt_dev_dbg(hdev, "end: err %d", err);
200 EXPORT_SYMBOL(__hci_cmd_sync_sk);
202 /* This function requires the caller holds hdev->req_lock. */
203 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
204 const void *param, u32 timeout)
206 return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
208 EXPORT_SYMBOL(__hci_cmd_sync);
210 /* Send HCI command and wait for command complete event */
211 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
212 const void *param, u32 timeout)
216 if (!test_bit(HCI_UP, &hdev->flags))
217 return ERR_PTR(-ENETDOWN);
219 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
221 hci_req_sync_lock(hdev);
222 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
223 hci_req_sync_unlock(hdev);
227 EXPORT_SYMBOL(hci_cmd_sync);
229 /* This function requires the caller holds hdev->req_lock. */
230 struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
231 const void *param, u8 event, u32 timeout)
233 return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
236 EXPORT_SYMBOL(__hci_cmd_sync_ev);
238 /* This function requires the caller holds hdev->req_lock. */
239 int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
240 const void *param, u8 event, u32 timeout,
246 skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
248 bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode,
253 /* If command return a status event skb will be set to NULL as there are
254 * no parameters, in case of failure IS_ERR(skb) would have be set to
255 * the actual error would be found with PTR_ERR(skb).
260 status = skb->data[0];
266 EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
268 int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
269 const void *param, u32 timeout)
271 return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
274 EXPORT_SYMBOL(__hci_cmd_sync_status);
276 static void hci_cmd_sync_work(struct work_struct *work)
278 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
280 bt_dev_dbg(hdev, "");
282 /* Dequeue all entries and run them */
284 struct hci_cmd_sync_work_entry *entry;
286 mutex_lock(&hdev->cmd_sync_work_lock);
287 entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
288 struct hci_cmd_sync_work_entry,
291 list_del(&entry->list);
292 mutex_unlock(&hdev->cmd_sync_work_lock);
297 bt_dev_dbg(hdev, "entry %p", entry);
302 hci_req_sync_lock(hdev);
303 err = entry->func(hdev, entry->data);
305 entry->destroy(hdev, entry->data, err);
306 hci_req_sync_unlock(hdev);
313 static void hci_cmd_sync_cancel_work(struct work_struct *work)
315 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
317 cancel_delayed_work_sync(&hdev->cmd_timer);
318 cancel_delayed_work_sync(&hdev->ncmd_timer);
319 atomic_set(&hdev->cmd_cnt, 1);
321 wake_up_interruptible(&hdev->req_wait_q);
324 static int hci_scan_disable_sync(struct hci_dev *hdev);
325 static int scan_disable_sync(struct hci_dev *hdev, void *data)
327 return hci_scan_disable_sync(hdev);
330 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length);
331 static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data)
333 return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN);
336 static void le_scan_disable(struct work_struct *work)
338 struct hci_dev *hdev = container_of(work, struct hci_dev,
339 le_scan_disable.work);
342 bt_dev_dbg(hdev, "");
345 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
348 cancel_delayed_work(&hdev->le_scan_restart);
350 status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
352 bt_dev_err(hdev, "failed to disable LE scan: %d", status);
356 hdev->discovery.scan_start = 0;
358 /* If we were running LE only scan, change discovery state. If
359 * we were running both LE and BR/EDR inquiry simultaneously,
360 * and BR/EDR inquiry is already finished, stop discovery,
361 * otherwise BR/EDR inquiry will stop discovery when finished.
362 * If we will resolve remote device name, do not change
366 if (hdev->discovery.type == DISCOV_TYPE_LE)
369 if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
372 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
373 if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
374 hdev->discovery.state != DISCOVERY_RESOLVING)
380 status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
382 bt_dev_err(hdev, "inquiry failed: status %d", status);
389 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
392 hci_dev_unlock(hdev);
395 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
397 static int hci_le_scan_restart_sync(struct hci_dev *hdev)
399 /* If controller is not scanning we are done. */
400 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
403 if (hdev->scanning_paused) {
404 bt_dev_dbg(hdev, "Scanning is paused for suspend");
408 hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
409 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE,
410 LE_SCAN_FILTER_DUP_ENABLE);
413 static int le_scan_restart_sync(struct hci_dev *hdev, void *data)
415 return hci_le_scan_restart_sync(hdev);
418 static void le_scan_restart(struct work_struct *work)
420 struct hci_dev *hdev = container_of(work, struct hci_dev,
421 le_scan_restart.work);
422 unsigned long timeout, duration, scan_start, now;
425 bt_dev_dbg(hdev, "");
429 status = hci_cmd_sync_queue(hdev, le_scan_restart_sync, NULL, NULL);
431 bt_dev_err(hdev, "failed to restart LE scan: status %d",
436 if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
437 !hdev->discovery.scan_start)
440 /* When the scan was started, hdev->le_scan_disable has been queued
441 * after duration from scan_start. During scan restart this job
442 * has been canceled, and we need to queue it again after proper
443 * timeout, to make sure that scan does not run indefinitely.
445 duration = hdev->discovery.scan_duration;
446 scan_start = hdev->discovery.scan_start;
448 if (now - scan_start <= duration) {
451 if (now >= scan_start)
452 elapsed = now - scan_start;
454 elapsed = ULONG_MAX - scan_start + now;
456 timeout = duration - elapsed;
461 queue_delayed_work(hdev->req_workqueue,
462 &hdev->le_scan_disable, timeout);
465 hci_dev_unlock(hdev);
468 static int reenable_adv_sync(struct hci_dev *hdev, void *data)
470 bt_dev_dbg(hdev, "");
472 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
473 list_empty(&hdev->adv_instances))
476 if (hdev->cur_adv_instance) {
477 return hci_schedule_adv_instance_sync(hdev,
478 hdev->cur_adv_instance,
481 if (ext_adv_capable(hdev)) {
482 hci_start_ext_adv_sync(hdev, 0x00);
484 hci_update_adv_data_sync(hdev, 0x00);
485 hci_update_scan_rsp_data_sync(hdev, 0x00);
486 hci_enable_advertising_sync(hdev);
493 static void reenable_adv(struct work_struct *work)
495 struct hci_dev *hdev = container_of(work, struct hci_dev,
499 bt_dev_dbg(hdev, "");
503 status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
505 bt_dev_err(hdev, "failed to reenable ADV: %d", status);
507 hci_dev_unlock(hdev);
510 static void cancel_adv_timeout(struct hci_dev *hdev)
512 if (hdev->adv_instance_timeout) {
513 hdev->adv_instance_timeout = 0;
514 cancel_delayed_work(&hdev->adv_instance_expire);
518 /* For a single instance:
519 * - force == true: The instance will be removed even when its remaining
520 * lifetime is not zero.
521 * - force == false: the instance will be deactivated but kept stored unless
522 * the remaining lifetime is zero.
524 * For instance == 0x00:
525 * - force == true: All instances will be removed regardless of their timeout
527 * - force == false: Only instances that have a timeout will be removed.
529 int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
530 u8 instance, bool force)
532 struct adv_info *adv_instance, *n, *next_instance = NULL;
536 /* Cancel any timeout concerning the removed instance(s). */
537 if (!instance || hdev->cur_adv_instance == instance)
538 cancel_adv_timeout(hdev);
540 /* Get the next instance to advertise BEFORE we remove
541 * the current one. This can be the same instance again
542 * if there is only one instance.
544 if (instance && hdev->cur_adv_instance == instance)
545 next_instance = hci_get_next_instance(hdev, instance);
547 if (instance == 0x00) {
548 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
550 if (!(force || adv_instance->timeout))
553 rem_inst = adv_instance->instance;
554 err = hci_remove_adv_instance(hdev, rem_inst);
556 mgmt_advertising_removed(sk, hdev, rem_inst);
559 adv_instance = hci_find_adv_instance(hdev, instance);
561 if (force || (adv_instance && adv_instance->timeout &&
562 !adv_instance->remaining_time)) {
563 /* Don't advertise a removed instance. */
565 next_instance->instance == instance)
566 next_instance = NULL;
568 err = hci_remove_adv_instance(hdev, instance);
570 mgmt_advertising_removed(sk, hdev, instance);
574 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
577 if (next_instance && !ext_adv_capable(hdev))
578 return hci_schedule_adv_instance_sync(hdev,
579 next_instance->instance,
585 static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
587 u8 instance = *(u8 *)data;
591 hci_clear_adv_instance_sync(hdev, NULL, instance, false);
593 if (list_empty(&hdev->adv_instances))
594 return hci_disable_advertising_sync(hdev);
599 static void adv_timeout_expire(struct work_struct *work)
602 struct hci_dev *hdev = container_of(work, struct hci_dev,
603 adv_instance_expire.work);
605 bt_dev_dbg(hdev, "");
609 hdev->adv_instance_timeout = 0;
611 if (hdev->cur_adv_instance == 0x00)
614 inst_ptr = kmalloc(1, GFP_KERNEL);
618 *inst_ptr = hdev->cur_adv_instance;
619 hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
622 hci_dev_unlock(hdev);
625 void hci_cmd_sync_init(struct hci_dev *hdev)
627 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
628 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
629 mutex_init(&hdev->cmd_sync_work_lock);
631 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
632 INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
633 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
634 INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart);
635 INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
638 void hci_cmd_sync_clear(struct hci_dev *hdev)
640 struct hci_cmd_sync_work_entry *entry, *tmp;
642 cancel_work_sync(&hdev->cmd_sync_work);
643 cancel_work_sync(&hdev->reenable_adv_work);
645 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
647 entry->destroy(hdev, entry->data, -ECANCELED);
649 list_del(&entry->list);
654 void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
656 bt_dev_dbg(hdev, "err 0x%2.2x", err);
658 if (hdev->req_status == HCI_REQ_PEND) {
659 hdev->req_result = err;
660 hdev->req_status = HCI_REQ_CANCELED;
662 cancel_delayed_work_sync(&hdev->cmd_timer);
663 cancel_delayed_work_sync(&hdev->ncmd_timer);
664 atomic_set(&hdev->cmd_cnt, 1);
666 wake_up_interruptible(&hdev->req_wait_q);
670 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
672 bt_dev_dbg(hdev, "err 0x%2.2x", err);
674 if (hdev->req_status == HCI_REQ_PEND) {
675 hdev->req_result = err;
676 hdev->req_status = HCI_REQ_CANCELED;
678 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
681 EXPORT_SYMBOL(hci_cmd_sync_cancel);
683 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
684 void *data, hci_cmd_sync_work_destroy_t destroy)
686 struct hci_cmd_sync_work_entry *entry;
688 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
691 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
697 entry->destroy = destroy;
699 mutex_lock(&hdev->cmd_sync_work_lock);
700 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
701 mutex_unlock(&hdev->cmd_sync_work_lock);
703 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
707 EXPORT_SYMBOL(hci_cmd_sync_queue);
709 int hci_update_eir_sync(struct hci_dev *hdev)
711 struct hci_cp_write_eir cp;
713 bt_dev_dbg(hdev, "");
715 if (!hdev_is_powered(hdev))
718 if (!lmp_ext_inq_capable(hdev))
721 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
724 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
727 memset(&cp, 0, sizeof(cp));
729 eir_create(hdev, cp.data);
731 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
734 memcpy(hdev->eir, cp.data, sizeof(cp.data));
736 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
740 static u8 get_service_classes(struct hci_dev *hdev)
742 struct bt_uuid *uuid;
745 list_for_each_entry(uuid, &hdev->uuids, list)
746 val |= uuid->svc_hint;
751 int hci_update_class_sync(struct hci_dev *hdev)
755 bt_dev_dbg(hdev, "");
757 if (!hdev_is_powered(hdev))
760 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
763 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
766 cod[0] = hdev->minor_class;
767 cod[1] = hdev->major_class;
768 cod[2] = get_service_classes(hdev);
770 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
773 if (memcmp(cod, hdev->dev_class, 3) == 0)
776 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
777 sizeof(cod), cod, HCI_CMD_TIMEOUT);
780 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
782 /* If there is no connection we are OK to advertise. */
783 if (hci_conn_num(hdev, LE_LINK) == 0)
786 /* Check le_states if there is any connection in peripheral role. */
787 if (hdev->conn_hash.le_num_peripheral > 0) {
788 /* Peripheral connection state and non connectable mode
791 if (!connectable && !(hdev->le_states[2] & 0x10))
794 /* Peripheral connection state and connectable mode bit 38
795 * and scannable bit 21.
797 if (connectable && (!(hdev->le_states[4] & 0x40) ||
798 !(hdev->le_states[2] & 0x20)))
802 /* Check le_states if there is any connection in central role. */
803 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
804 /* Central connection state and non connectable mode bit 18. */
805 if (!connectable && !(hdev->le_states[2] & 0x02))
808 /* Central connection state and connectable mode bit 35 and
811 if (connectable && (!(hdev->le_states[4] & 0x08) ||
812 !(hdev->le_states[2] & 0x08)))
819 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
821 /* If privacy is not enabled don't use RPA */
822 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
825 /* If basic privacy mode is enabled use RPA */
826 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
829 /* If limited privacy mode is enabled don't use RPA if we're
830 * both discoverable and bondable.
832 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
833 hci_dev_test_flag(hdev, HCI_BONDABLE))
836 /* We're neither bondable nor discoverable in the limited
837 * privacy mode, therefore use RPA.
842 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
844 /* If we're advertising or initiating an LE connection we can't
845 * go ahead and change the random address at this time. This is
846 * because the eventual initiator address used for the
847 * subsequently created connection will be undefined (some
848 * controllers use the new address and others the one we had
849 * when the operation started).
851 * In this kind of scenario skip the update and let the random
852 * address be updated at the next cycle.
854 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
855 hci_lookup_le_connect(hdev)) {
856 bt_dev_dbg(hdev, "Deferring random address update");
857 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
861 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
862 6, rpa, HCI_CMD_TIMEOUT);
865 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
866 bool rpa, u8 *own_addr_type)
870 /* If privacy is enabled use a resolvable private address. If
871 * current RPA has expired or there is something else than
872 * the current RPA in use, then generate a new one.
875 /* If Controller supports LL Privacy use own address type is
878 if (use_ll_privacy(hdev))
879 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
881 *own_addr_type = ADDR_LE_DEV_RANDOM;
883 /* Check if RPA is valid */
887 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
889 bt_dev_err(hdev, "failed to generate new RPA");
893 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
900 /* In case of required privacy without resolvable private address,
901 * use an non-resolvable private address. This is useful for active
902 * scanning and non-connectable advertising.
904 if (require_privacy) {
908 /* The non-resolvable private address is generated
909 * from random six bytes with the two most significant
912 get_random_bytes(&nrpa, 6);
915 /* The non-resolvable private address shall not be
916 * equal to the public address.
918 if (bacmp(&hdev->bdaddr, &nrpa))
922 *own_addr_type = ADDR_LE_DEV_RANDOM;
924 return hci_set_random_addr_sync(hdev, &nrpa);
927 /* If forcing static address is in use or there is no public
928 * address use the static address as random address (but skip
929 * the HCI command if the current random address is already the
932 * In case BR/EDR has been disabled on a dual-mode controller
933 * and a static address has been configured, then use that
934 * address instead of the public BR/EDR address.
936 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
937 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
938 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
939 bacmp(&hdev->static_addr, BDADDR_ANY))) {
940 *own_addr_type = ADDR_LE_DEV_RANDOM;
941 if (bacmp(&hdev->static_addr, &hdev->random_addr))
942 return hci_set_random_addr_sync(hdev,
947 /* Neither privacy nor static address is being used so use a
950 *own_addr_type = ADDR_LE_DEV_PUBLIC;
955 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
957 struct hci_cp_le_set_ext_adv_enable *cp;
958 struct hci_cp_ext_adv_set *set;
959 u8 data[sizeof(*cp) + sizeof(*set) * 1];
962 /* If request specifies an instance that doesn't exist, fail */
964 struct adv_info *adv;
966 adv = hci_find_adv_instance(hdev, instance);
970 /* If not enabled there is nothing to do */
975 memset(data, 0, sizeof(data));
978 set = (void *)cp->data;
980 /* Instance 0x00 indicates all advertising instances will be disabled */
981 cp->num_of_sets = !!instance;
984 set->handle = instance;
986 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
988 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
989 size, data, HCI_CMD_TIMEOUT);
992 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
993 bdaddr_t *random_addr)
995 struct hci_cp_le_set_adv_set_rand_addr cp;
999 /* Instance 0x00 doesn't have an adv_info, instead it uses
1000 * hdev->random_addr to track its address so whenever it needs
1001 * to be updated this also set the random address since
1002 * hdev->random_addr is shared with scan state machine.
1004 err = hci_set_random_addr_sync(hdev, random_addr);
1009 memset(&cp, 0, sizeof(cp));
1011 cp.handle = instance;
1012 bacpy(&cp.bdaddr, random_addr);
1014 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1015 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1018 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1020 struct hci_cp_le_set_ext_adv_params cp;
1023 bdaddr_t random_addr;
1026 struct adv_info *adv;
1030 adv = hci_find_adv_instance(hdev, instance);
1037 /* Updating parameters of an active instance will return a
1038 * Command Disallowed error, so we must first disable the
1039 * instance if it is active.
1041 if (adv && !adv->pending) {
1042 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1047 flags = hci_adv_instance_flags(hdev, instance);
1049 /* If the "connectable" instance flag was not set, then choose between
1050 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1052 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1053 mgmt_get_connectable(hdev);
1055 if (!is_advertising_allowed(hdev, connectable))
1058 /* Set require_privacy to true only when non-connectable
1059 * advertising is used. In that case it is fine to use a
1060 * non-resolvable private address.
1062 err = hci_get_random_address(hdev, !connectable,
1063 adv_use_rpa(hdev, flags), adv,
1064 &own_addr_type, &random_addr);
1068 memset(&cp, 0, sizeof(cp));
1071 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
1072 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
1073 cp.tx_power = adv->tx_power;
1075 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1076 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1077 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
1080 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
1084 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
1086 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
1087 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1088 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
1090 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
1092 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
1095 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
1097 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
1100 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
1101 * contains the peer’s Identity Address and the Peer_Address_Type
1102 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
1103 * These parameters are used to locate the corresponding local IRK in
1104 * the resolving list; this IRK is used to generate their own address
1105 * used in the advertisement.
1107 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
1108 hci_copy_identity_address(hdev, &cp.peer_addr,
1109 &cp.peer_addr_type);
1111 cp.own_addr_type = own_addr_type;
1112 cp.channel_map = hdev->le_adv_channel_map;
1113 cp.handle = instance;
1115 if (flags & MGMT_ADV_FLAG_SEC_2M) {
1116 cp.primary_phy = HCI_ADV_PHY_1M;
1117 cp.secondary_phy = HCI_ADV_PHY_2M;
1118 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
1119 cp.primary_phy = HCI_ADV_PHY_CODED;
1120 cp.secondary_phy = HCI_ADV_PHY_CODED;
1122 /* In all other cases use 1M */
1123 cp.primary_phy = HCI_ADV_PHY_1M;
1124 cp.secondary_phy = HCI_ADV_PHY_1M;
1127 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1128 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1132 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
1133 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
1134 bacmp(&random_addr, BDADDR_ANY)) {
1135 /* Check if random address need to be updated */
1137 if (!bacmp(&random_addr, &adv->random_addr))
1140 if (!bacmp(&random_addr, &hdev->random_addr))
1144 return hci_set_adv_set_random_addr_sync(hdev, instance,
1151 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1154 struct hci_cp_le_set_ext_scan_rsp_data cp;
1155 u8 data[HCI_MAX_EXT_AD_LENGTH];
1158 struct adv_info *adv = NULL;
1161 memset(&pdu, 0, sizeof(pdu));
1164 adv = hci_find_adv_instance(hdev, instance);
1165 if (!adv || !adv->scan_rsp_changed)
1169 len = eir_create_scan_rsp(hdev, instance, pdu.data);
1171 pdu.cp.handle = instance;
1172 pdu.cp.length = len;
1173 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1174 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1176 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1177 sizeof(pdu.cp) + len, &pdu.cp,
1183 adv->scan_rsp_changed = false;
1185 memcpy(hdev->scan_rsp_data, pdu.data, len);
1186 hdev->scan_rsp_data_len = len;
1192 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1194 struct hci_cp_le_set_scan_rsp_data cp;
1197 memset(&cp, 0, sizeof(cp));
1199 len = eir_create_scan_rsp(hdev, instance, cp.data);
1201 if (hdev->scan_rsp_data_len == len &&
1202 !memcmp(cp.data, hdev->scan_rsp_data, len))
1205 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1206 hdev->scan_rsp_data_len = len;
1210 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1211 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1214 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1216 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1219 if (ext_adv_capable(hdev))
1220 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1222 return __hci_set_scan_rsp_data_sync(hdev, instance);
1225 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1227 struct hci_cp_le_set_ext_adv_enable *cp;
1228 struct hci_cp_ext_adv_set *set;
1229 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1230 struct adv_info *adv;
1233 adv = hci_find_adv_instance(hdev, instance);
1236 /* If already enabled there is nothing to do */
1244 set = (void *)cp->data;
1246 memset(cp, 0, sizeof(*cp));
1249 cp->num_of_sets = 0x01;
1251 memset(set, 0, sizeof(*set));
1253 set->handle = instance;
1255 /* Set duration per instance since controller is responsible for
1258 if (adv && adv->timeout) {
1259 u16 duration = adv->timeout * MSEC_PER_SEC;
1261 /* Time = N * 10 ms */
1262 set->duration = cpu_to_le16(duration / 10);
1265 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1267 sizeof(*set) * cp->num_of_sets,
1268 data, HCI_CMD_TIMEOUT);
1271 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1275 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1279 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1283 return hci_enable_ext_advertising_sync(hdev, instance);
1286 static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1288 struct hci_cp_le_set_per_adv_enable cp;
1290 /* If periodic advertising already disabled there is nothing to do. */
1291 if (!hci_dev_test_flag(hdev, HCI_LE_PER_ADV))
1294 memset(&cp, 0, sizeof(cp));
1297 cp.handle = instance;
1299 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1300 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1303 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1304 u16 min_interval, u16 max_interval)
1306 struct hci_cp_le_set_per_adv_params cp;
1308 memset(&cp, 0, sizeof(cp));
1311 min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1314 max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1316 cp.handle = instance;
1317 cp.min_interval = cpu_to_le16(min_interval);
1318 cp.max_interval = cpu_to_le16(max_interval);
1319 cp.periodic_properties = 0x0000;
1321 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1322 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1325 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1328 struct hci_cp_le_set_per_adv_data cp;
1329 u8 data[HCI_MAX_PER_AD_LENGTH];
1333 memset(&pdu, 0, sizeof(pdu));
1336 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1338 if (!adv || !adv->periodic)
1342 len = eir_create_per_adv_data(hdev, instance, pdu.data);
1344 pdu.cp.length = len;
1345 pdu.cp.handle = instance;
1346 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1348 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1349 sizeof(pdu.cp) + len, &pdu,
1353 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1355 struct hci_cp_le_set_per_adv_enable cp;
1357 /* If periodic advertising already enabled there is nothing to do. */
1358 if (hci_dev_test_flag(hdev, HCI_LE_PER_ADV))
1361 memset(&cp, 0, sizeof(cp));
1364 cp.handle = instance;
1366 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1367 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1370 /* Checks if periodic advertising data contains a Basic Announcement and if it
1371 * does generates a Broadcast ID and add Broadcast Announcement.
1373 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1378 /* Skip if NULL adv as instance 0x00 is used for general purpose
1379 * advertising so it cannot used for the likes of Broadcast Announcement
1380 * as it can be overwritten at any point.
1385 /* Check if PA data doesn't contains a Basic Audio Announcement then
1386 * there is nothing to do.
1388 if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1392 /* Check if advertising data already has a Broadcast Announcement since
1393 * the process may want to control the Broadcast ID directly and in that
1394 * case the kernel shall no interfere.
1396 if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1400 /* Generate Broadcast ID */
1401 get_random_bytes(bid, sizeof(bid));
1402 eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1403 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL);
1405 return hci_update_adv_data_sync(hdev, adv->instance);
1408 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len,
1409 u8 *data, u32 flags, u16 min_interval,
1410 u16 max_interval, u16 sync_interval)
1412 struct adv_info *adv = NULL;
1416 hci_disable_per_advertising_sync(hdev, instance);
1419 adv = hci_find_adv_instance(hdev, instance);
1420 /* Create an instance if that could not be found */
1422 adv = hci_add_per_instance(hdev, instance, flags,
1427 return PTR_ERR(adv);
1432 /* Only start advertising if instance 0 or if a dedicated instance has
1435 if (!adv || added) {
1436 err = hci_start_ext_adv_sync(hdev, instance);
1440 err = hci_adv_bcast_annoucement(hdev, adv);
1445 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1450 err = hci_set_per_adv_data_sync(hdev, instance);
1454 err = hci_enable_per_advertising_sync(hdev, instance);
1462 hci_remove_adv_instance(hdev, instance);
1467 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1471 if (ext_adv_capable(hdev))
1472 return hci_start_ext_adv_sync(hdev, instance);
1474 err = hci_update_adv_data_sync(hdev, instance);
1478 err = hci_update_scan_rsp_data_sync(hdev, instance);
1482 return hci_enable_advertising_sync(hdev);
1485 int hci_enable_advertising_sync(struct hci_dev *hdev)
1487 struct adv_info *adv_instance;
1488 struct hci_cp_le_set_adv_param cp;
1489 u8 own_addr_type, enable = 0x01;
1491 u16 adv_min_interval, adv_max_interval;
1495 if (ext_adv_capable(hdev))
1496 return hci_enable_ext_advertising_sync(hdev,
1497 hdev->cur_adv_instance);
1499 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1500 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1502 /* If the "connectable" instance flag was not set, then choose between
1503 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1505 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1506 mgmt_get_connectable(hdev);
1508 if (!is_advertising_allowed(hdev, connectable))
1511 status = hci_disable_advertising_sync(hdev);
1515 /* Clear the HCI_LE_ADV bit temporarily so that the
1516 * hci_update_random_address knows that it's safe to go ahead
1517 * and write a new random address. The flag will be set back on
1518 * as soon as the SET_ADV_ENABLE HCI command completes.
1520 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1522 /* Set require_privacy to true only when non-connectable
1523 * advertising is used. In that case it is fine to use a
1524 * non-resolvable private address.
1526 status = hci_update_random_address_sync(hdev, !connectable,
1527 adv_use_rpa(hdev, flags),
1532 memset(&cp, 0, sizeof(cp));
1535 adv_min_interval = adv_instance->min_interval;
1536 adv_max_interval = adv_instance->max_interval;
1538 adv_min_interval = hdev->le_adv_min_interval;
1539 adv_max_interval = hdev->le_adv_max_interval;
1543 cp.type = LE_ADV_IND;
1545 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1546 cp.type = LE_ADV_SCAN_IND;
1548 cp.type = LE_ADV_NONCONN_IND;
1550 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1551 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1552 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1553 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1557 cp.min_interval = cpu_to_le16(adv_min_interval);
1558 cp.max_interval = cpu_to_le16(adv_max_interval);
1559 cp.own_address_type = own_addr_type;
1560 cp.channel_map = hdev->le_adv_channel_map;
1562 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1563 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1567 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1568 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1571 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1573 return hci_enable_advertising_sync(hdev);
1576 int hci_enable_advertising(struct hci_dev *hdev)
1578 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1579 list_empty(&hdev->adv_instances))
1582 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1585 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1590 if (!ext_adv_capable(hdev))
1593 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1597 /* If request specifies an instance that doesn't exist, fail */
1598 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1601 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1602 sizeof(instance), &instance, 0,
1603 HCI_CMD_TIMEOUT, sk);
1606 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data)
1608 struct adv_info *adv = data;
1612 instance = adv->instance;
1614 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL);
1617 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance)
1619 struct adv_info *adv = NULL;
1622 adv = hci_find_adv_instance(hdev, instance);
1627 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL);
1630 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1632 struct hci_cp_le_term_big cp;
1634 memset(&cp, 0, sizeof(cp));
1638 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1639 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1642 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1645 struct hci_cp_le_set_ext_adv_data cp;
1646 u8 data[HCI_MAX_EXT_AD_LENGTH];
1649 struct adv_info *adv = NULL;
1652 memset(&pdu, 0, sizeof(pdu));
1655 adv = hci_find_adv_instance(hdev, instance);
1656 if (!adv || !adv->adv_data_changed)
1660 len = eir_create_adv_data(hdev, instance, pdu.data);
1662 pdu.cp.length = len;
1663 pdu.cp.handle = instance;
1664 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1665 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1667 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1668 sizeof(pdu.cp) + len, &pdu.cp,
1673 /* Update data if the command succeed */
1675 adv->adv_data_changed = false;
1677 memcpy(hdev->adv_data, pdu.data, len);
1678 hdev->adv_data_len = len;
1684 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1686 struct hci_cp_le_set_adv_data cp;
1689 memset(&cp, 0, sizeof(cp));
1691 len = eir_create_adv_data(hdev, instance, cp.data);
1693 /* There's nothing to do if the data hasn't changed */
1694 if (hdev->adv_data_len == len &&
1695 memcmp(cp.data, hdev->adv_data, len) == 0)
1698 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1699 hdev->adv_data_len = len;
1703 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1704 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1707 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1709 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1712 if (ext_adv_capable(hdev))
1713 return hci_set_ext_adv_data_sync(hdev, instance);
1715 return hci_set_adv_data_sync(hdev, instance);
1718 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1721 struct adv_info *adv = NULL;
1724 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1727 if (hdev->adv_instance_timeout)
1730 adv = hci_find_adv_instance(hdev, instance);
1734 /* A zero timeout means unlimited advertising. As long as there is
1735 * only one instance, duration should be ignored. We still set a timeout
1736 * in case further instances are being added later on.
1738 * If the remaining lifetime of the instance is more than the duration
1739 * then the timeout corresponds to the duration, otherwise it will be
1740 * reduced to the remaining instance lifetime.
1742 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1743 timeout = adv->duration;
1745 timeout = adv->remaining_time;
1747 /* The remaining time is being reduced unless the instance is being
1748 * advertised without time limit.
1751 adv->remaining_time = adv->remaining_time - timeout;
1753 /* Only use work for scheduling instances with legacy advertising */
1754 if (!ext_adv_capable(hdev)) {
1755 hdev->adv_instance_timeout = timeout;
1756 queue_delayed_work(hdev->req_workqueue,
1757 &hdev->adv_instance_expire,
1758 msecs_to_jiffies(timeout * 1000));
1761 /* If we're just re-scheduling the same instance again then do not
1762 * execute any HCI commands. This happens when a single instance is
1765 if (!force && hdev->cur_adv_instance == instance &&
1766 hci_dev_test_flag(hdev, HCI_LE_ADV))
1769 hdev->cur_adv_instance = instance;
1771 return hci_start_adv_sync(hdev, instance);
1774 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1778 if (!ext_adv_capable(hdev))
1781 /* Disable instance 0x00 to disable all instances */
1782 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1786 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1787 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1790 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1792 struct adv_info *adv, *n;
1795 if (ext_adv_capable(hdev))
1796 /* Remove all existing sets */
1797 err = hci_clear_adv_sets_sync(hdev, sk);
1798 if (ext_adv_capable(hdev))
1801 /* This is safe as long as there is no command send while the lock is
1806 /* Cleanup non-ext instances */
1807 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1808 u8 instance = adv->instance;
1811 if (!(force || adv->timeout))
1814 err = hci_remove_adv_instance(hdev, instance);
1816 mgmt_advertising_removed(sk, hdev, instance);
1819 hci_dev_unlock(hdev);
1824 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1829 /* If we use extended advertising, instance has to be removed first. */
1830 if (ext_adv_capable(hdev))
1831 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1832 if (ext_adv_capable(hdev))
1835 /* This is safe as long as there is no command send while the lock is
1840 err = hci_remove_adv_instance(hdev, instance);
1842 mgmt_advertising_removed(sk, hdev, instance);
1844 hci_dev_unlock(hdev);
1849 /* For a single instance:
1850 * - force == true: The instance will be removed even when its remaining
1851 * lifetime is not zero.
1852 * - force == false: the instance will be deactivated but kept stored unless
1853 * the remaining lifetime is zero.
1855 * For instance == 0x00:
1856 * - force == true: All instances will be removed regardless of their timeout
1858 * - force == false: Only instances that have a timeout will be removed.
1860 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1861 u8 instance, bool force)
1863 struct adv_info *next = NULL;
1866 /* Cancel any timeout concerning the removed instance(s). */
1867 if (!instance || hdev->cur_adv_instance == instance)
1868 cancel_adv_timeout(hdev);
1870 /* Get the next instance to advertise BEFORE we remove
1871 * the current one. This can be the same instance again
1872 * if there is only one instance.
1874 if (hdev->cur_adv_instance == instance)
1875 next = hci_get_next_instance(hdev, instance);
1878 err = hci_clear_adv_sync(hdev, sk, force);
1882 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1884 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1885 /* Don't advertise a removed instance. */
1886 if (next && next->instance == instance)
1889 err = hci_remove_adv_sync(hdev, instance, sk);
1895 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1898 if (next && !ext_adv_capable(hdev))
1899 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1904 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1906 struct hci_cp_read_rssi cp;
1909 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1910 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1913 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1915 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1916 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1919 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1921 struct hci_cp_read_tx_power cp;
1925 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1926 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1929 int hci_disable_advertising_sync(struct hci_dev *hdev)
1934 /* If controller is not advertising we are done. */
1935 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1938 if (ext_adv_capable(hdev))
1939 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1940 if (ext_adv_capable(hdev))
1943 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1944 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1947 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1950 struct hci_cp_le_set_ext_scan_enable cp;
1952 memset(&cp, 0, sizeof(cp));
1955 if (hci_dev_test_flag(hdev, HCI_MESH))
1956 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
1958 cp.filter_dup = filter_dup;
1960 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1961 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1964 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
1967 struct hci_cp_le_set_scan_enable cp;
1969 if (use_ext_scan(hdev))
1970 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
1972 memset(&cp, 0, sizeof(cp));
1975 if (val && hci_dev_test_flag(hdev, HCI_MESH))
1976 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
1978 cp.filter_dup = filter_dup;
1980 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
1981 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1984 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
1986 if (!use_ll_privacy(hdev))
1989 /* If controller is not/already resolving we are done. */
1990 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
1993 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
1994 sizeof(val), &val, HCI_CMD_TIMEOUT);
1997 static int hci_scan_disable_sync(struct hci_dev *hdev)
2001 /* If controller is not scanning we are done. */
2002 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2005 if (hdev->scanning_paused) {
2006 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2010 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2012 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2019 static bool scan_use_rpa(struct hci_dev *hdev)
2021 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2024 static void hci_start_interleave_scan(struct hci_dev *hdev)
2026 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2027 queue_delayed_work(hdev->req_workqueue,
2028 &hdev->interleave_scan, 0);
2031 static bool is_interleave_scanning(struct hci_dev *hdev)
2033 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
2036 static void cancel_interleave_scan(struct hci_dev *hdev)
2038 bt_dev_dbg(hdev, "cancelling interleave scan");
2040 cancel_delayed_work_sync(&hdev->interleave_scan);
2042 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2045 /* Return true if interleave_scan wasn't started until exiting this function,
2046 * otherwise, return false
2048 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2050 /* Do interleaved scan only if all of the following are true:
2051 * - There is at least one ADV monitor
2052 * - At least one pending LE connection or one device to be scanned for
2053 * - Monitor offloading is not supported
2054 * If so, we should alternate between allowlist scan and one without
2055 * any filters to save power.
2057 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2058 !(list_empty(&hdev->pend_le_conns) &&
2059 list_empty(&hdev->pend_le_reports)) &&
2060 hci_get_adv_monitor_offload_ext(hdev) ==
2061 HCI_ADV_MONITOR_EXT_NONE;
2062 bool is_interleaving = is_interleave_scanning(hdev);
2064 if (use_interleaving && !is_interleaving) {
2065 hci_start_interleave_scan(hdev);
2066 bt_dev_dbg(hdev, "starting interleave scan");
2070 if (!use_interleaving && is_interleaving)
2071 cancel_interleave_scan(hdev);
2076 /* Removes connection to resolve list if needed.*/
2077 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2078 bdaddr_t *bdaddr, u8 bdaddr_type)
2080 struct hci_cp_le_del_from_resolv_list cp;
2081 struct bdaddr_list_with_irk *entry;
2083 if (!use_ll_privacy(hdev))
2086 /* Check if the IRK has been programmed */
2087 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2092 cp.bdaddr_type = bdaddr_type;
2093 bacpy(&cp.bdaddr, bdaddr);
2095 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2096 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2099 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2100 bdaddr_t *bdaddr, u8 bdaddr_type)
2102 struct hci_cp_le_del_from_accept_list cp;
2105 /* Check if device is on accept list before removing it */
2106 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2109 cp.bdaddr_type = bdaddr_type;
2110 bacpy(&cp.bdaddr, bdaddr);
2112 /* Ignore errors when removing from resolving list as that is likely
2113 * that the device was never added.
2115 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2117 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2118 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2120 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2124 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2130 /* Adds connection to resolve list if needed.
2131 * Setting params to NULL programs local hdev->irk
2133 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2134 struct hci_conn_params *params)
2136 struct hci_cp_le_add_to_resolv_list cp;
2137 struct smp_irk *irk;
2138 struct bdaddr_list_with_irk *entry;
2140 if (!use_ll_privacy(hdev))
2143 /* Attempt to program local identity address, type and irk if params is
2147 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2150 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2151 memcpy(cp.peer_irk, hdev->irk, 16);
2155 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2159 /* Check if the IK has _not_ been programmed yet. */
2160 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2166 cp.bdaddr_type = params->addr_type;
2167 bacpy(&cp.bdaddr, ¶ms->addr);
2168 memcpy(cp.peer_irk, irk->val, 16);
2170 /* Default privacy mode is always Network */
2171 params->privacy_mode = HCI_NETWORK_PRIVACY;
2174 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2175 memcpy(cp.local_irk, hdev->irk, 16);
2177 memset(cp.local_irk, 0, 16);
2179 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2180 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2183 /* Set Device Privacy Mode. */
2184 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2185 struct hci_conn_params *params)
2187 struct hci_cp_le_set_privacy_mode cp;
2188 struct smp_irk *irk;
2190 /* If device privacy mode has already been set there is nothing to do */
2191 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2194 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2195 * indicates that LL Privacy has been enabled and
2196 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2198 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2201 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2205 memset(&cp, 0, sizeof(cp));
2206 cp.bdaddr_type = irk->addr_type;
2207 bacpy(&cp.bdaddr, &irk->bdaddr);
2208 cp.mode = HCI_DEVICE_PRIVACY;
2210 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2211 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2214 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2215 * this attempts to program the device in the resolving list as well and
2216 * properly set the privacy mode.
2218 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2219 struct hci_conn_params *params,
2222 struct hci_cp_le_add_to_accept_list cp;
2225 /* During suspend, only wakeable devices can be in acceptlist */
2226 if (hdev->suspended &&
2227 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
2230 /* Select filter policy to accept all advertising */
2231 if (*num_entries >= hdev->le_accept_list_size)
2234 /* Accept list can not be used with RPAs */
2235 if (!use_ll_privacy(hdev) &&
2236 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type))
2239 /* Attempt to program the device in the resolving list first to avoid
2240 * having to rollback in case it fails since the resolving list is
2241 * dynamic it can probably be smaller than the accept list.
2243 err = hci_le_add_resolve_list_sync(hdev, params);
2245 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2249 /* Set Privacy Mode */
2250 err = hci_le_set_privacy_mode_sync(hdev, params);
2252 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2256 /* Check if already in accept list */
2257 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
2262 cp.bdaddr_type = params->addr_type;
2263 bacpy(&cp.bdaddr, ¶ms->addr);
2265 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2266 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2268 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2269 /* Rollback the device from the resolving list */
2270 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2274 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2280 /* This function disables/pause all advertising instances */
2281 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2286 /* If already been paused there is nothing to do. */
2287 if (hdev->advertising_paused)
2290 bt_dev_dbg(hdev, "Pausing directed advertising");
2292 /* Stop directed advertising */
2293 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2295 /* When discoverable timeout triggers, then just make sure
2296 * the limited discoverable flag is cleared. Even in the case
2297 * of a timeout triggered from general discoverable, it is
2298 * safe to unconditionally clear the flag.
2300 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2301 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2302 hdev->discov_timeout = 0;
2305 bt_dev_dbg(hdev, "Pausing advertising instances");
2307 /* Call to disable any advertisements active on the controller.
2308 * This will succeed even if no advertisements are configured.
2310 err = hci_disable_advertising_sync(hdev);
2314 /* If we are using software rotation, pause the loop */
2315 if (!ext_adv_capable(hdev))
2316 cancel_adv_timeout(hdev);
2318 hdev->advertising_paused = true;
2319 hdev->advertising_old_state = old_state;
2324 /* This function enables all user advertising instances */
2325 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2327 struct adv_info *adv, *tmp;
2330 /* If advertising has not been paused there is nothing to do. */
2331 if (!hdev->advertising_paused)
2334 /* Resume directed advertising */
2335 hdev->advertising_paused = false;
2336 if (hdev->advertising_old_state) {
2337 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2338 hdev->advertising_old_state = 0;
2341 bt_dev_dbg(hdev, "Resuming advertising instances");
2343 if (ext_adv_capable(hdev)) {
2344 /* Call for each tracked instance to be re-enabled */
2345 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2346 err = hci_enable_ext_advertising_sync(hdev,
2351 /* If the instance cannot be resumed remove it */
2352 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2356 /* Schedule for most recent instance to be restarted and begin
2357 * the software rotation loop
2359 err = hci_schedule_adv_instance_sync(hdev,
2360 hdev->cur_adv_instance,
2364 hdev->advertising_paused = false;
2369 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2370 bool extended, struct sock *sk)
2372 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2373 HCI_OP_READ_LOCAL_OOB_DATA;
2375 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2378 /* Device must not be scanning when updating the accept list.
2380 * Update is done using the following sequence:
2382 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
2383 * Remove Devices From Accept List ->
2384 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
2385 * Add Devices to Accept List ->
2386 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
2387 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
2390 * In case of failure advertising shall be restored to its original state and
2391 * return would disable accept list since either accept or resolving list could
2392 * not be programmed.
2395 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2397 struct hci_conn_params *params;
2398 struct bdaddr_list *b, *t;
2400 bool pend_conn, pend_report;
2404 /* Pause advertising if resolving list can be used as controllers are
2405 * cannot accept resolving list modifications while advertising.
2407 if (use_ll_privacy(hdev)) {
2408 err = hci_pause_advertising_sync(hdev);
2410 bt_dev_err(hdev, "pause advertising failed: %d", err);
2415 /* Disable address resolution while reprogramming accept list since
2416 * devices that do have an IRK will be programmed in the resolving list
2417 * when LL Privacy is enabled.
2419 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2421 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2425 /* Go through the current accept list programmed into the
2426 * controller one by one and check if that address is connected or is
2427 * still in the list of pending connections or list of devices to
2428 * report. If not present in either list, then remove it from
2431 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2432 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2435 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2438 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2442 /* If the device is not likely to connect or report,
2443 * remove it from the acceptlist.
2445 if (!pend_conn && !pend_report) {
2446 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2454 /* Since all no longer valid accept list entries have been
2455 * removed, walk through the list of pending connections
2456 * and ensure that any new device gets programmed into
2459 * If the list of the devices is larger than the list of
2460 * available accept list entries in the controller, then
2461 * just abort and return filer policy value to not use the
2464 list_for_each_entry(params, &hdev->pend_le_conns, action) {
2465 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
2470 /* After adding all new pending connections, walk through
2471 * the list of pending reports and also add these to the
2472 * accept list if there is still space. Abort if space runs out.
2474 list_for_each_entry(params, &hdev->pend_le_reports, action) {
2475 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
2480 /* Use the allowlist unless the following conditions are all true:
2481 * - We are not currently suspending
2482 * - There are 1 or more ADV monitors registered and it's not offloaded
2483 * - Interleaved scanning is not currently using the allowlist
2485 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2486 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2487 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2491 filter_policy = err ? 0x00 : 0x01;
2493 /* Enable address resolution when LL Privacy is enabled. */
2494 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2496 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2498 /* Resume advertising if it was paused */
2499 if (use_ll_privacy(hdev))
2500 hci_resume_advertising_sync(hdev);
2502 /* Select filter policy to use accept list */
2503 return filter_policy;
2506 /* Returns true if an le connection is in the scanning state */
2507 static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
2509 struct hci_conn_hash *h = &hdev->conn_hash;
2514 list_for_each_entry_rcu(c, &h->list, list) {
2515 if (c->type == LE_LINK && c->state == BT_CONNECT &&
2516 test_bit(HCI_CONN_SCANNING, &c->flags)) {
2527 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2528 u16 interval, u16 window,
2529 u8 own_addr_type, u8 filter_policy)
2531 struct hci_cp_le_set_ext_scan_params *cp;
2532 struct hci_cp_le_scan_phy_params *phy;
2533 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2537 phy = (void *)cp->data;
2539 memset(data, 0, sizeof(data));
2541 cp->own_addr_type = own_addr_type;
2542 cp->filter_policy = filter_policy;
2544 if (scan_1m(hdev) || scan_2m(hdev)) {
2545 cp->scanning_phys |= LE_SCAN_PHY_1M;
2548 phy->interval = cpu_to_le16(interval);
2549 phy->window = cpu_to_le16(window);
2555 if (scan_coded(hdev)) {
2556 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2559 phy->interval = cpu_to_le16(interval);
2560 phy->window = cpu_to_le16(window);
2566 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2567 sizeof(*cp) + sizeof(*phy) * num_phy,
2568 data, HCI_CMD_TIMEOUT);
2571 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2572 u16 interval, u16 window,
2573 u8 own_addr_type, u8 filter_policy)
2575 struct hci_cp_le_set_scan_param cp;
2577 if (use_ext_scan(hdev))
2578 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2579 window, own_addr_type,
2582 memset(&cp, 0, sizeof(cp));
2584 cp.interval = cpu_to_le16(interval);
2585 cp.window = cpu_to_le16(window);
2586 cp.own_address_type = own_addr_type;
2587 cp.filter_policy = filter_policy;
2589 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2590 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2593 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2594 u16 window, u8 own_addr_type, u8 filter_policy,
2599 if (hdev->scanning_paused) {
2600 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2604 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2605 own_addr_type, filter_policy);
2609 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2612 static int hci_passive_scan_sync(struct hci_dev *hdev)
2616 u16 window, interval;
2617 u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2620 if (hdev->scanning_paused) {
2621 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2625 err = hci_scan_disable_sync(hdev);
2627 bt_dev_err(hdev, "disable scanning failed: %d", err);
2631 /* Set require_privacy to false since no SCAN_REQ are send
2632 * during passive scanning. Not using an non-resolvable address
2633 * here is important so that peer devices using direct
2634 * advertising with our address will be correctly reported
2635 * by the controller.
2637 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2641 if (hdev->enable_advmon_interleave_scan &&
2642 hci_update_interleaved_scan_sync(hdev))
2645 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
2647 /* Adding or removing entries from the accept list must
2648 * happen before enabling scanning. The controller does
2649 * not allow accept list modification while scanning.
2651 filter_policy = hci_update_accept_list_sync(hdev);
2653 /* When the controller is using random resolvable addresses and
2654 * with that having LE privacy enabled, then controllers with
2655 * Extended Scanner Filter Policies support can now enable support
2656 * for handling directed advertising.
2658 * So instead of using filter polices 0x00 (no acceptlist)
2659 * and 0x01 (acceptlist enabled) use the new filter policies
2660 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2662 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2663 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2664 filter_policy |= 0x02;
2666 if (hdev->suspended) {
2667 window = hdev->le_scan_window_suspend;
2668 interval = hdev->le_scan_int_suspend;
2669 } else if (hci_is_le_conn_scanning(hdev)) {
2670 window = hdev->le_scan_window_connect;
2671 interval = hdev->le_scan_int_connect;
2672 } else if (hci_is_adv_monitoring(hdev)) {
2673 window = hdev->le_scan_window_adv_monitor;
2674 interval = hdev->le_scan_int_adv_monitor;
2676 window = hdev->le_scan_window;
2677 interval = hdev->le_scan_interval;
2680 /* Disable all filtering for Mesh */
2681 if (hci_dev_test_flag(hdev, HCI_MESH)) {
2683 filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
2686 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2688 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
2689 own_addr_type, filter_policy, filter_dups);
2692 /* This function controls the passive scanning based on hdev->pend_le_conns
2693 * list. If there are pending LE connection we start the background scanning,
2694 * otherwise we stop it in the following sequence:
2696 * If there are devices to scan:
2698 * Disable Scanning -> Update Accept List ->
2699 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2700 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2707 int hci_update_passive_scan_sync(struct hci_dev *hdev)
2711 if (!test_bit(HCI_UP, &hdev->flags) ||
2712 test_bit(HCI_INIT, &hdev->flags) ||
2713 hci_dev_test_flag(hdev, HCI_SETUP) ||
2714 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2715 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2716 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2719 /* No point in doing scanning if LE support hasn't been enabled */
2720 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2723 /* If discovery is active don't interfere with it */
2724 if (hdev->discovery.state != DISCOVERY_STOPPED)
2727 /* Reset RSSI and UUID filters when starting background scanning
2728 * since these filters are meant for service discovery only.
2730 * The Start Discovery and Start Service Discovery operations
2731 * ensure to set proper values for RSSI threshold and UUID
2732 * filter list. So it is safe to just reset them here.
2734 hci_discovery_filter_clear(hdev);
2736 bt_dev_dbg(hdev, "ADV monitoring is %s",
2737 hci_is_adv_monitoring(hdev) ? "on" : "off");
2739 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
2740 list_empty(&hdev->pend_le_conns) &&
2741 list_empty(&hdev->pend_le_reports) &&
2742 !hci_is_adv_monitoring(hdev) &&
2743 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2744 /* If there is no pending LE connections or devices
2745 * to be scanned for or no ADV monitors, we should stop the
2746 * background scanning.
2749 bt_dev_dbg(hdev, "stopping background scanning");
2751 err = hci_scan_disable_sync(hdev);
2753 bt_dev_err(hdev, "stop background scanning failed: %d",
2756 /* If there is at least one pending LE connection, we should
2757 * keep the background scan running.
2760 /* If controller is connecting, we should not start scanning
2761 * since some controllers are not able to scan and connect at
2764 if (hci_lookup_le_connect(hdev))
2767 bt_dev_dbg(hdev, "start background scanning");
2769 err = hci_passive_scan_sync(hdev);
2771 bt_dev_err(hdev, "start background scanning failed: %d",
2778 static int update_scan_sync(struct hci_dev *hdev, void *data)
2780 return hci_update_scan_sync(hdev);
2783 int hci_update_scan(struct hci_dev *hdev)
2785 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
2788 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2790 return hci_update_passive_scan_sync(hdev);
2793 int hci_update_passive_scan(struct hci_dev *hdev)
2795 /* Only queue if it would have any effect */
2796 if (!test_bit(HCI_UP, &hdev->flags) ||
2797 test_bit(HCI_INIT, &hdev->flags) ||
2798 hci_dev_test_flag(hdev, HCI_SETUP) ||
2799 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2800 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2801 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2804 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2807 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
2811 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2814 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
2815 sizeof(val), &val, HCI_CMD_TIMEOUT);
2819 hdev->features[1][0] |= LMP_HOST_SC;
2820 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2822 hdev->features[1][0] &= ~LMP_HOST_SC;
2823 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2830 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
2834 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2835 lmp_host_ssp_capable(hdev))
2838 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
2839 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2840 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2843 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2844 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2848 return hci_write_sc_support_sync(hdev, 0x01);
2851 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
2853 struct hci_cp_write_le_host_supported cp;
2855 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
2856 !lmp_bredr_capable(hdev))
2859 /* Check first if we already have the right host state
2860 * (host features set)
2862 if (le == lmp_host_le_capable(hdev) &&
2863 simul == lmp_host_le_br_capable(hdev))
2866 memset(&cp, 0, sizeof(cp));
2871 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
2872 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2875 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
2877 struct adv_info *adv, *tmp;
2880 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2883 /* If RPA Resolution has not been enable yet it means the
2884 * resolving list is empty and we should attempt to program the
2885 * local IRK in order to support using own_addr_type
2886 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
2888 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
2889 hci_le_add_resolve_list_sync(hdev, NULL);
2890 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2893 /* Make sure the controller has a good default for
2894 * advertising data. This also applies to the case
2895 * where BR/EDR was toggled during the AUTO_OFF phase.
2897 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2898 list_empty(&hdev->adv_instances)) {
2899 if (ext_adv_capable(hdev)) {
2900 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
2902 hci_update_scan_rsp_data_sync(hdev, 0x00);
2904 err = hci_update_adv_data_sync(hdev, 0x00);
2906 hci_update_scan_rsp_data_sync(hdev, 0x00);
2909 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2910 hci_enable_advertising_sync(hdev);
2913 /* Call for each tracked instance to be scheduled */
2914 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
2915 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
2920 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
2924 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
2925 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
2928 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
2929 sizeof(link_sec), &link_sec,
2933 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
2935 struct hci_cp_write_page_scan_activity cp;
2939 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2942 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
2945 memset(&cp, 0, sizeof(cp));
2948 type = PAGE_SCAN_TYPE_INTERLACED;
2950 /* 160 msec page scan interval */
2951 cp.interval = cpu_to_le16(0x0100);
2953 type = hdev->def_page_scan_type;
2954 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
2957 cp.window = cpu_to_le16(hdev->def_page_scan_window);
2959 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
2960 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
2961 err = __hci_cmd_sync_status(hdev,
2962 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
2963 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2968 if (hdev->page_scan_type != type)
2969 err = __hci_cmd_sync_status(hdev,
2970 HCI_OP_WRITE_PAGE_SCAN_TYPE,
2971 sizeof(type), &type,
2977 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
2979 struct bdaddr_list *b;
2981 list_for_each_entry(b, &hdev->accept_list, list) {
2982 struct hci_conn *conn;
2984 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
2988 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2995 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
2997 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3002 int hci_update_scan_sync(struct hci_dev *hdev)
3006 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3009 if (!hdev_is_powered(hdev))
3012 if (mgmt_powering_down(hdev))
3015 if (hdev->scanning_paused)
3018 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3019 disconnected_accept_list_entries(hdev))
3022 scan = SCAN_DISABLED;
3024 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3025 scan |= SCAN_INQUIRY;
3027 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3028 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3031 return hci_write_scan_enable_sync(hdev, scan);
3034 int hci_update_name_sync(struct hci_dev *hdev)
3036 struct hci_cp_write_local_name cp;
3038 memset(&cp, 0, sizeof(cp));
3040 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3042 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3047 /* This function perform powered update HCI command sequence after the HCI init
3048 * sequence which end up resetting all states, the sequence is as follows:
3050 * HCI_SSP_ENABLED(Enable SSP)
3051 * HCI_LE_ENABLED(Enable LE)
3052 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
3054 * Enable Authentication
3055 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3056 * Set Name -> Set EIR)
3058 int hci_powered_update_sync(struct hci_dev *hdev)
3062 /* Register the available SMP channels (BR/EDR and LE) only when
3063 * successfully powering on the controller. This late
3064 * registration is required so that LE SMP can clearly decide if
3065 * the public address or static address is used.
3069 err = hci_write_ssp_mode_sync(hdev, 0x01);
3073 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3077 err = hci_powered_update_adv_sync(hdev);
3081 err = hci_write_auth_enable_sync(hdev);
3085 if (lmp_bredr_capable(hdev)) {
3086 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3087 hci_write_fast_connectable_sync(hdev, true);
3089 hci_write_fast_connectable_sync(hdev, false);
3090 hci_update_scan_sync(hdev);
3091 hci_update_class_sync(hdev);
3092 hci_update_name_sync(hdev);
3093 hci_update_eir_sync(hdev);
3100 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3101 * (BD_ADDR) for a HCI device from
3102 * a firmware node property.
3103 * @hdev: The HCI device
3105 * Search the firmware node for 'local-bd-address'.
3107 * All-zero BD addresses are rejected, because those could be properties
3108 * that exist in the firmware tables, but were not updated by the firmware. For
3109 * example, the DTS could define 'local-bd-address', with zero BD addresses.
3111 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3113 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3117 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3118 (u8 *)&ba, sizeof(ba));
3119 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3122 bacpy(&hdev->public_addr, &ba);
3125 struct hci_init_stage {
3126 int (*func)(struct hci_dev *hdev);
3129 /* Run init stage NULL terminated function table */
3130 static int hci_init_stage_sync(struct hci_dev *hdev,
3131 const struct hci_init_stage *stage)
3135 for (i = 0; stage[i].func; i++) {
3138 err = stage[i].func(hdev);
3146 /* Read Local Version */
3147 static int hci_read_local_version_sync(struct hci_dev *hdev)
3149 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3150 0, NULL, HCI_CMD_TIMEOUT);
3153 /* Read BD Address */
3154 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3156 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3157 0, NULL, HCI_CMD_TIMEOUT);
3160 #define HCI_INIT(_func) \
3165 static const struct hci_init_stage hci_init0[] = {
3166 /* HCI_OP_READ_LOCAL_VERSION */
3167 HCI_INIT(hci_read_local_version_sync),
3168 /* HCI_OP_READ_BD_ADDR */
3169 HCI_INIT(hci_read_bd_addr_sync),
3173 int hci_reset_sync(struct hci_dev *hdev)
3177 set_bit(HCI_RESET, &hdev->flags);
3179 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3187 static int hci_init0_sync(struct hci_dev *hdev)
3191 bt_dev_dbg(hdev, "");
3194 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3195 err = hci_reset_sync(hdev);
3200 return hci_init_stage_sync(hdev, hci_init0);
3203 static int hci_unconf_init_sync(struct hci_dev *hdev)
3207 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3210 err = hci_init0_sync(hdev);
3214 if (hci_dev_test_flag(hdev, HCI_SETUP))
3215 hci_debugfs_create_basic(hdev);
3220 /* Read Local Supported Features. */
3221 static int hci_read_local_features_sync(struct hci_dev *hdev)
3223 /* Not all AMP controllers support this command */
3224 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
3227 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3228 0, NULL, HCI_CMD_TIMEOUT);
3231 /* BR Controller init stage 1 command sequence */
3232 static const struct hci_init_stage br_init1[] = {
3233 /* HCI_OP_READ_LOCAL_FEATURES */
3234 HCI_INIT(hci_read_local_features_sync),
3235 /* HCI_OP_READ_LOCAL_VERSION */
3236 HCI_INIT(hci_read_local_version_sync),
3237 /* HCI_OP_READ_BD_ADDR */
3238 HCI_INIT(hci_read_bd_addr_sync),
3242 /* Read Local Commands */
3243 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3245 /* All Bluetooth 1.2 and later controllers should support the
3246 * HCI command for reading the local supported commands.
3248 * Unfortunately some controllers indicate Bluetooth 1.2 support,
3249 * but do not have support for this command. If that is the case,
3250 * the driver can quirk the behavior and skip reading the local
3251 * supported commands.
3253 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3254 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3255 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3256 0, NULL, HCI_CMD_TIMEOUT);
3261 /* Read Local AMP Info */
3262 static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
3264 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
3265 0, NULL, HCI_CMD_TIMEOUT);
3268 /* Read Data Blk size */
3269 static int hci_read_data_block_size_sync(struct hci_dev *hdev)
3271 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
3272 0, NULL, HCI_CMD_TIMEOUT);
3275 /* Read Flow Control Mode */
3276 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
3278 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
3279 0, NULL, HCI_CMD_TIMEOUT);
3282 /* Read Location Data */
3283 static int hci_read_location_data_sync(struct hci_dev *hdev)
3285 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
3286 0, NULL, HCI_CMD_TIMEOUT);
3289 /* AMP Controller init stage 1 command sequence */
3290 static const struct hci_init_stage amp_init1[] = {
3291 /* HCI_OP_READ_LOCAL_VERSION */
3292 HCI_INIT(hci_read_local_version_sync),
3293 /* HCI_OP_READ_LOCAL_COMMANDS */
3294 HCI_INIT(hci_read_local_cmds_sync),
3295 /* HCI_OP_READ_LOCAL_AMP_INFO */
3296 HCI_INIT(hci_read_local_amp_info_sync),
3297 /* HCI_OP_READ_DATA_BLOCK_SIZE */
3298 HCI_INIT(hci_read_data_block_size_sync),
3299 /* HCI_OP_READ_FLOW_CONTROL_MODE */
3300 HCI_INIT(hci_read_flow_control_mode_sync),
3301 /* HCI_OP_READ_LOCATION_DATA */
3302 HCI_INIT(hci_read_location_data_sync),
3305 static int hci_init1_sync(struct hci_dev *hdev)
3309 bt_dev_dbg(hdev, "");
3312 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3313 err = hci_reset_sync(hdev);
3318 switch (hdev->dev_type) {
3320 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3321 return hci_init_stage_sync(hdev, br_init1);
3323 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3324 return hci_init_stage_sync(hdev, amp_init1);
3326 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
3333 /* AMP Controller init stage 2 command sequence */
3334 static const struct hci_init_stage amp_init2[] = {
3335 /* HCI_OP_READ_LOCAL_FEATURES */
3336 HCI_INIT(hci_read_local_features_sync),
3339 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
3340 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3342 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3343 0, NULL, HCI_CMD_TIMEOUT);
3346 /* Read Class of Device */
3347 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3349 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3350 0, NULL, HCI_CMD_TIMEOUT);
3353 /* Read Local Name */
3354 static int hci_read_local_name_sync(struct hci_dev *hdev)
3356 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3357 0, NULL, HCI_CMD_TIMEOUT);
3360 /* Read Voice Setting */
3361 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3363 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3364 0, NULL, HCI_CMD_TIMEOUT);
3367 /* Read Number of Supported IAC */
3368 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3370 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3371 0, NULL, HCI_CMD_TIMEOUT);
3374 /* Read Current IAC LAP */
3375 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3377 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3378 0, NULL, HCI_CMD_TIMEOUT);
3381 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3382 u8 cond_type, bdaddr_t *bdaddr,
3385 struct hci_cp_set_event_filter cp;
3387 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3390 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3393 memset(&cp, 0, sizeof(cp));
3394 cp.flt_type = flt_type;
3396 if (flt_type != HCI_FLT_CLEAR_ALL) {
3397 cp.cond_type = cond_type;
3398 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3399 cp.addr_conn_flt.auto_accept = auto_accept;
3402 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3403 flt_type == HCI_FLT_CLEAR_ALL ?
3404 sizeof(cp.flt_type) : sizeof(cp), &cp,
3408 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3410 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3413 /* In theory the state machine should not reach here unless
3414 * a hci_set_event_filter_sync() call succeeds, but we do
3415 * the check both for parity and as a future reminder.
3417 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3420 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3424 /* Connection accept timeout ~20 secs */
3425 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3427 __le16 param = cpu_to_le16(0x7d00);
3429 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3430 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
3433 /* BR Controller init stage 2 command sequence */
3434 static const struct hci_init_stage br_init2[] = {
3435 /* HCI_OP_READ_BUFFER_SIZE */
3436 HCI_INIT(hci_read_buffer_size_sync),
3437 /* HCI_OP_READ_CLASS_OF_DEV */
3438 HCI_INIT(hci_read_dev_class_sync),
3439 /* HCI_OP_READ_LOCAL_NAME */
3440 HCI_INIT(hci_read_local_name_sync),
3441 /* HCI_OP_READ_VOICE_SETTING */
3442 HCI_INIT(hci_read_voice_setting_sync),
3443 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
3444 HCI_INIT(hci_read_num_supported_iac_sync),
3445 /* HCI_OP_READ_CURRENT_IAC_LAP */
3446 HCI_INIT(hci_read_current_iac_lap_sync),
3447 /* HCI_OP_SET_EVENT_FLT */
3448 HCI_INIT(hci_clear_event_filter_sync),
3449 /* HCI_OP_WRITE_CA_TIMEOUT */
3450 HCI_INIT(hci_write_ca_timeout_sync),
3454 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3458 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3461 /* When SSP is available, then the host features page
3462 * should also be available as well. However some
3463 * controllers list the max_page as 0 as long as SSP
3464 * has not been enabled. To achieve proper debugging
3465 * output, force the minimum max_page to 1 at least.
3467 hdev->max_page = 0x01;
3469 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3470 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3473 static int hci_write_eir_sync(struct hci_dev *hdev)
3475 struct hci_cp_write_eir cp;
3477 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3480 memset(hdev->eir, 0, sizeof(hdev->eir));
3481 memset(&cp, 0, sizeof(cp));
3483 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3487 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3491 if (!lmp_inq_rssi_capable(hdev) &&
3492 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3495 /* If Extended Inquiry Result events are supported, then
3496 * they are clearly preferred over Inquiry Result with RSSI
3499 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3501 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3502 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3505 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3507 if (!lmp_inq_tx_pwr_capable(hdev))
3510 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3511 0, NULL, HCI_CMD_TIMEOUT);
3514 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3516 struct hci_cp_read_local_ext_features cp;
3518 if (!lmp_ext_feat_capable(hdev))
3521 memset(&cp, 0, sizeof(cp));
3524 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3525 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3528 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3530 return hci_read_local_ext_features_sync(hdev, 0x01);
3533 /* HCI Controller init stage 2 command sequence */
3534 static const struct hci_init_stage hci_init2[] = {
3535 /* HCI_OP_READ_LOCAL_COMMANDS */
3536 HCI_INIT(hci_read_local_cmds_sync),
3537 /* HCI_OP_WRITE_SSP_MODE */
3538 HCI_INIT(hci_write_ssp_mode_1_sync),
3539 /* HCI_OP_WRITE_EIR */
3540 HCI_INIT(hci_write_eir_sync),
3541 /* HCI_OP_WRITE_INQUIRY_MODE */
3542 HCI_INIT(hci_write_inquiry_mode_sync),
3543 /* HCI_OP_READ_INQ_RSP_TX_POWER */
3544 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3545 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3546 HCI_INIT(hci_read_local_ext_features_1_sync),
3547 /* HCI_OP_WRITE_AUTH_ENABLE */
3548 HCI_INIT(hci_write_auth_enable_sync),
3552 /* Read LE Buffer Size */
3553 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3555 /* Use Read LE Buffer Size V2 if supported */
3556 if (hdev->commands[41] & 0x20)
3557 return __hci_cmd_sync_status(hdev,
3558 HCI_OP_LE_READ_BUFFER_SIZE_V2,
3559 0, NULL, HCI_CMD_TIMEOUT);
3561 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3562 0, NULL, HCI_CMD_TIMEOUT);
3565 /* Read LE Local Supported Features */
3566 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3568 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3569 0, NULL, HCI_CMD_TIMEOUT);
3572 /* Read LE Supported States */
3573 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3575 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3576 0, NULL, HCI_CMD_TIMEOUT);
3579 /* LE Controller init stage 2 command sequence */
3580 static const struct hci_init_stage le_init2[] = {
3581 /* HCI_OP_LE_READ_BUFFER_SIZE */
3582 HCI_INIT(hci_le_read_buffer_size_sync),
3583 /* HCI_OP_LE_READ_LOCAL_FEATURES */
3584 HCI_INIT(hci_le_read_local_features_sync),
3585 /* HCI_OP_LE_READ_SUPPORTED_STATES */
3586 HCI_INIT(hci_le_read_supported_states_sync),
3590 static int hci_init2_sync(struct hci_dev *hdev)
3594 bt_dev_dbg(hdev, "");
3596 if (hdev->dev_type == HCI_AMP)
3597 return hci_init_stage_sync(hdev, amp_init2);
3599 err = hci_init_stage_sync(hdev, hci_init2);
3603 if (lmp_bredr_capable(hdev)) {
3604 err = hci_init_stage_sync(hdev, br_init2);
3608 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3611 if (lmp_le_capable(hdev)) {
3612 err = hci_init_stage_sync(hdev, le_init2);
3615 /* LE-only controllers have LE implicitly enabled */
3616 if (!lmp_bredr_capable(hdev))
3617 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3623 static int hci_set_event_mask_sync(struct hci_dev *hdev)
3625 /* The second byte is 0xff instead of 0x9f (two reserved bits
3626 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3627 * command otherwise.
3629 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3631 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3632 * any event mask for pre 1.2 devices.
3634 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3637 if (lmp_bredr_capable(hdev)) {
3638 events[4] |= 0x01; /* Flow Specification Complete */
3640 /* Don't set Disconnect Complete when suspended as that
3641 * would wakeup the host when disconnecting due to
3644 if (hdev->suspended)
3647 /* Use a different default for LE-only devices */
3648 memset(events, 0, sizeof(events));
3649 events[1] |= 0x20; /* Command Complete */
3650 events[1] |= 0x40; /* Command Status */
3651 events[1] |= 0x80; /* Hardware Error */
3653 /* If the controller supports the Disconnect command, enable
3654 * the corresponding event. In addition enable packet flow
3655 * control related events.
3657 if (hdev->commands[0] & 0x20) {
3658 /* Don't set Disconnect Complete when suspended as that
3659 * would wakeup the host when disconnecting due to
3662 if (!hdev->suspended)
3663 events[0] |= 0x10; /* Disconnection Complete */
3664 events[2] |= 0x04; /* Number of Completed Packets */
3665 events[3] |= 0x02; /* Data Buffer Overflow */
3668 /* If the controller supports the Read Remote Version
3669 * Information command, enable the corresponding event.
3671 if (hdev->commands[2] & 0x80)
3672 events[1] |= 0x08; /* Read Remote Version Information
3676 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3677 events[0] |= 0x80; /* Encryption Change */
3678 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3682 if (lmp_inq_rssi_capable(hdev) ||
3683 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3684 events[4] |= 0x02; /* Inquiry Result with RSSI */
3686 if (lmp_ext_feat_capable(hdev))
3687 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3689 if (lmp_esco_capable(hdev)) {
3690 events[5] |= 0x08; /* Synchronous Connection Complete */
3691 events[5] |= 0x10; /* Synchronous Connection Changed */
3694 if (lmp_sniffsubr_capable(hdev))
3695 events[5] |= 0x20; /* Sniff Subrating */
3697 if (lmp_pause_enc_capable(hdev))
3698 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3700 if (lmp_ext_inq_capable(hdev))
3701 events[5] |= 0x40; /* Extended Inquiry Result */
3703 if (lmp_no_flush_capable(hdev))
3704 events[7] |= 0x01; /* Enhanced Flush Complete */
3706 if (lmp_lsto_capable(hdev))
3707 events[6] |= 0x80; /* Link Supervision Timeout Changed */
3709 if (lmp_ssp_capable(hdev)) {
3710 events[6] |= 0x01; /* IO Capability Request */
3711 events[6] |= 0x02; /* IO Capability Response */
3712 events[6] |= 0x04; /* User Confirmation Request */
3713 events[6] |= 0x08; /* User Passkey Request */
3714 events[6] |= 0x10; /* Remote OOB Data Request */
3715 events[6] |= 0x20; /* Simple Pairing Complete */
3716 events[7] |= 0x04; /* User Passkey Notification */
3717 events[7] |= 0x08; /* Keypress Notification */
3718 events[7] |= 0x10; /* Remote Host Supported
3719 * Features Notification
3723 if (lmp_le_capable(hdev))
3724 events[7] |= 0x20; /* LE Meta-Event */
3726 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3727 sizeof(events), events, HCI_CMD_TIMEOUT);
3730 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3732 struct hci_cp_read_stored_link_key cp;
3734 if (!(hdev->commands[6] & 0x20) ||
3735 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3738 memset(&cp, 0, sizeof(cp));
3739 bacpy(&cp.bdaddr, BDADDR_ANY);
3742 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3743 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3746 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3748 struct hci_cp_write_def_link_policy cp;
3749 u16 link_policy = 0;
3751 if (!(hdev->commands[5] & 0x10))
3754 memset(&cp, 0, sizeof(cp));
3756 if (lmp_rswitch_capable(hdev))
3757 link_policy |= HCI_LP_RSWITCH;
3758 if (lmp_hold_capable(hdev))
3759 link_policy |= HCI_LP_HOLD;
3760 if (lmp_sniff_capable(hdev))
3761 link_policy |= HCI_LP_SNIFF;
3762 if (lmp_park_capable(hdev))
3763 link_policy |= HCI_LP_PARK;
3765 cp.policy = cpu_to_le16(link_policy);
3767 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3768 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3771 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3773 if (!(hdev->commands[8] & 0x01))
3776 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3777 0, NULL, HCI_CMD_TIMEOUT);
3780 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3782 if (!(hdev->commands[18] & 0x04) ||
3783 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING))
3786 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3787 0, NULL, HCI_CMD_TIMEOUT);
3790 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3792 /* Some older Broadcom based Bluetooth 1.2 controllers do not
3793 * support the Read Page Scan Type command. Check support for
3794 * this command in the bit mask of supported commands.
3796 if (!(hdev->commands[13] & 0x01))
3799 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3800 0, NULL, HCI_CMD_TIMEOUT);
3803 /* Read features beyond page 1 if available */
3804 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3809 if (!lmp_ext_feat_capable(hdev))
3812 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3814 err = hci_read_local_ext_features_sync(hdev, page);
3822 /* HCI Controller init stage 3 command sequence */
3823 static const struct hci_init_stage hci_init3[] = {
3824 /* HCI_OP_SET_EVENT_MASK */
3825 HCI_INIT(hci_set_event_mask_sync),
3826 /* HCI_OP_READ_STORED_LINK_KEY */
3827 HCI_INIT(hci_read_stored_link_key_sync),
3828 /* HCI_OP_WRITE_DEF_LINK_POLICY */
3829 HCI_INIT(hci_setup_link_policy_sync),
3830 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
3831 HCI_INIT(hci_read_page_scan_activity_sync),
3832 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
3833 HCI_INIT(hci_read_def_err_data_reporting_sync),
3834 /* HCI_OP_READ_PAGE_SCAN_TYPE */
3835 HCI_INIT(hci_read_page_scan_type_sync),
3836 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3837 HCI_INIT(hci_read_local_ext_features_all_sync),
3841 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
3845 if (!lmp_le_capable(hdev))
3848 memset(events, 0, sizeof(events));
3850 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
3851 events[0] |= 0x10; /* LE Long Term Key Request */
3853 /* If controller supports the Connection Parameters Request
3854 * Link Layer Procedure, enable the corresponding event.
3856 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
3857 /* LE Remote Connection Parameter Request */
3860 /* If the controller supports the Data Length Extension
3861 * feature, enable the corresponding event.
3863 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
3864 events[0] |= 0x40; /* LE Data Length Change */
3866 /* If the controller supports LL Privacy feature or LE Extended Adv,
3867 * enable the corresponding event.
3869 if (use_enhanced_conn_complete(hdev))
3870 events[1] |= 0x02; /* LE Enhanced Connection Complete */
3872 /* If the controller supports Extended Scanner Filter
3873 * Policies, enable the corresponding event.
3875 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
3876 events[1] |= 0x04; /* LE Direct Advertising Report */
3878 /* If the controller supports Channel Selection Algorithm #2
3879 * feature, enable the corresponding event.
3881 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
3882 events[2] |= 0x08; /* LE Channel Selection Algorithm */
3884 /* If the controller supports the LE Set Scan Enable command,
3885 * enable the corresponding advertising report event.
3887 if (hdev->commands[26] & 0x08)
3888 events[0] |= 0x02; /* LE Advertising Report */
3890 /* If the controller supports the LE Create Connection
3891 * command, enable the corresponding event.
3893 if (hdev->commands[26] & 0x10)
3894 events[0] |= 0x01; /* LE Connection Complete */
3896 /* If the controller supports the LE Connection Update
3897 * command, enable the corresponding event.
3899 if (hdev->commands[27] & 0x04)
3900 events[0] |= 0x04; /* LE Connection Update Complete */
3902 /* If the controller supports the LE Read Remote Used Features
3903 * command, enable the corresponding event.
3905 if (hdev->commands[27] & 0x20)
3906 /* LE Read Remote Used Features Complete */
3909 /* If the controller supports the LE Read Local P-256
3910 * Public Key command, enable the corresponding event.
3912 if (hdev->commands[34] & 0x02)
3913 /* LE Read Local P-256 Public Key Complete */
3916 /* If the controller supports the LE Generate DHKey
3917 * command, enable the corresponding event.
3919 if (hdev->commands[34] & 0x04)
3920 events[1] |= 0x01; /* LE Generate DHKey Complete */
3922 /* If the controller supports the LE Set Default PHY or
3923 * LE Set PHY commands, enable the corresponding event.
3925 if (hdev->commands[35] & (0x20 | 0x40))
3926 events[1] |= 0x08; /* LE PHY Update Complete */
3928 /* If the controller supports LE Set Extended Scan Parameters
3929 * and LE Set Extended Scan Enable commands, enable the
3930 * corresponding event.
3932 if (use_ext_scan(hdev))
3933 events[1] |= 0x10; /* LE Extended Advertising Report */
3935 /* If the controller supports the LE Extended Advertising
3936 * command, enable the corresponding event.
3938 if (ext_adv_capable(hdev))
3939 events[2] |= 0x02; /* LE Advertising Set Terminated */
3941 if (cis_capable(hdev)) {
3942 events[3] |= 0x01; /* LE CIS Established */
3943 if (cis_peripheral_capable(hdev))
3944 events[3] |= 0x02; /* LE CIS Request */
3947 if (bis_capable(hdev)) {
3948 events[3] |= 0x04; /* LE Create BIG Complete */
3949 events[3] |= 0x08; /* LE Terminate BIG Complete */
3950 events[3] |= 0x10; /* LE BIG Sync Established */
3951 events[3] |= 0x20; /* LE BIG Sync Loss */
3954 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
3955 sizeof(events), events, HCI_CMD_TIMEOUT);
3958 /* Read LE Advertising Channel TX Power */
3959 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
3961 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
3962 /* HCI TS spec forbids mixing of legacy and extended
3963 * advertising commands wherein READ_ADV_TX_POWER is
3964 * also included. So do not call it if extended adv
3965 * is supported otherwise controller will return
3966 * COMMAND_DISALLOWED for extended commands.
3968 return __hci_cmd_sync_status(hdev,
3969 HCI_OP_LE_READ_ADV_TX_POWER,
3970 0, NULL, HCI_CMD_TIMEOUT);
3976 /* Read LE Min/Max Tx Power*/
3977 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
3979 if (!(hdev->commands[38] & 0x80) ||
3980 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
3983 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
3984 0, NULL, HCI_CMD_TIMEOUT);
3987 /* Read LE Accept List Size */
3988 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
3990 if (!(hdev->commands[26] & 0x40))
3993 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
3994 0, NULL, HCI_CMD_TIMEOUT);
3997 /* Clear LE Accept List */
3998 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
4000 if (!(hdev->commands[26] & 0x80))
4003 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
4007 /* Read LE Resolving List Size */
4008 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4010 if (!(hdev->commands[34] & 0x40))
4013 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4014 0, NULL, HCI_CMD_TIMEOUT);
4017 /* Clear LE Resolving List */
4018 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4020 if (!(hdev->commands[34] & 0x20))
4023 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4027 /* Set RPA timeout */
4028 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4030 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4032 if (!(hdev->commands[35] & 0x04))
4035 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4036 sizeof(timeout), &timeout,
4040 /* Read LE Maximum Data Length */
4041 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4043 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4046 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4050 /* Read LE Suggested Default Data Length */
4051 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4053 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4056 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4060 /* Read LE Number of Supported Advertising Sets */
4061 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4063 if (!ext_adv_capable(hdev))
4066 return __hci_cmd_sync_status(hdev,
4067 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4068 0, NULL, HCI_CMD_TIMEOUT);
4071 /* Write LE Host Supported */
4072 static int hci_set_le_support_sync(struct hci_dev *hdev)
4074 struct hci_cp_write_le_host_supported cp;
4076 /* LE-only devices do not support explicit enablement */
4077 if (!lmp_bredr_capable(hdev))
4080 memset(&cp, 0, sizeof(cp));
4082 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4087 if (cp.le == lmp_host_le_capable(hdev))
4090 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4091 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4094 /* LE Set Host Feature */
4095 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4097 struct hci_cp_le_set_host_feature cp;
4099 if (!iso_capable(hdev))
4102 memset(&cp, 0, sizeof(cp));
4104 /* Isochronous Channels (Host Support) */
4108 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4109 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4112 /* LE Controller init stage 3 command sequence */
4113 static const struct hci_init_stage le_init3[] = {
4114 /* HCI_OP_LE_SET_EVENT_MASK */
4115 HCI_INIT(hci_le_set_event_mask_sync),
4116 /* HCI_OP_LE_READ_ADV_TX_POWER */
4117 HCI_INIT(hci_le_read_adv_tx_power_sync),
4118 /* HCI_OP_LE_READ_TRANSMIT_POWER */
4119 HCI_INIT(hci_le_read_tx_power_sync),
4120 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4121 HCI_INIT(hci_le_read_accept_list_size_sync),
4122 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4123 HCI_INIT(hci_le_clear_accept_list_sync),
4124 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4125 HCI_INIT(hci_le_read_resolv_list_size_sync),
4126 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
4127 HCI_INIT(hci_le_clear_resolv_list_sync),
4128 /* HCI_OP_LE_SET_RPA_TIMEOUT */
4129 HCI_INIT(hci_le_set_rpa_timeout_sync),
4130 /* HCI_OP_LE_READ_MAX_DATA_LEN */
4131 HCI_INIT(hci_le_read_max_data_len_sync),
4132 /* HCI_OP_LE_READ_DEF_DATA_LEN */
4133 HCI_INIT(hci_le_read_def_data_len_sync),
4134 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4135 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4136 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4137 HCI_INIT(hci_set_le_support_sync),
4138 /* HCI_OP_LE_SET_HOST_FEATURE */
4139 HCI_INIT(hci_le_set_host_feature_sync),
4143 static int hci_init3_sync(struct hci_dev *hdev)
4147 bt_dev_dbg(hdev, "");
4149 err = hci_init_stage_sync(hdev, hci_init3);
4153 if (lmp_le_capable(hdev))
4154 return hci_init_stage_sync(hdev, le_init3);
4159 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4161 struct hci_cp_delete_stored_link_key cp;
4163 /* Some Broadcom based Bluetooth controllers do not support the
4164 * Delete Stored Link Key command. They are clearly indicating its
4165 * absence in the bit mask of supported commands.
4167 * Check the supported commands and only if the command is marked
4168 * as supported send it. If not supported assume that the controller
4169 * does not have actual support for stored link keys which makes this
4170 * command redundant anyway.
4172 * Some controllers indicate that they support handling deleting
4173 * stored link keys, but they don't. The quirk lets a driver
4174 * just disable this command.
4176 if (!(hdev->commands[6] & 0x80) ||
4177 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4180 memset(&cp, 0, sizeof(cp));
4181 bacpy(&cp.bdaddr, BDADDR_ANY);
4182 cp.delete_all = 0x01;
4184 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4185 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4188 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4190 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4191 bool changed = false;
4193 /* Set event mask page 2 if the HCI command for it is supported */
4194 if (!(hdev->commands[22] & 0x04))
4197 /* If Connectionless Peripheral Broadcast central role is supported
4198 * enable all necessary events for it.
4200 if (lmp_cpb_central_capable(hdev)) {
4201 events[1] |= 0x40; /* Triggered Clock Capture */
4202 events[1] |= 0x80; /* Synchronization Train Complete */
4203 events[2] |= 0x08; /* Truncated Page Complete */
4204 events[2] |= 0x20; /* CPB Channel Map Change */
4208 /* If Connectionless Peripheral Broadcast peripheral role is supported
4209 * enable all necessary events for it.
4211 if (lmp_cpb_peripheral_capable(hdev)) {
4212 events[2] |= 0x01; /* Synchronization Train Received */
4213 events[2] |= 0x02; /* CPB Receive */
4214 events[2] |= 0x04; /* CPB Timeout */
4215 events[2] |= 0x10; /* Peripheral Page Response Timeout */
4219 /* Enable Authenticated Payload Timeout Expired event if supported */
4220 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4225 /* Some Broadcom based controllers indicate support for Set Event
4226 * Mask Page 2 command, but then actually do not support it. Since
4227 * the default value is all bits set to zero, the command is only
4228 * required if the event mask has to be changed. In case no change
4229 * to the event mask is needed, skip this command.
4234 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4235 sizeof(events), events, HCI_CMD_TIMEOUT);
4238 /* Read local codec list if the HCI command is supported */
4239 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4241 if (!(hdev->commands[29] & 0x20))
4244 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_CODECS, 0, NULL,
4248 /* Read local pairing options if the HCI command is supported */
4249 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4251 if (!(hdev->commands[41] & 0x08))
4254 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4255 0, NULL, HCI_CMD_TIMEOUT);
4258 /* Get MWS transport configuration if the HCI command is supported */
4259 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4261 if (!(hdev->commands[30] & 0x08))
4264 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4265 0, NULL, HCI_CMD_TIMEOUT);
4268 /* Check for Synchronization Train support */
4269 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4271 if (!lmp_sync_train_capable(hdev))
4274 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4275 0, NULL, HCI_CMD_TIMEOUT);
4278 /* Enable Secure Connections if supported and configured */
4279 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4283 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4284 !bredr_sc_enabled(hdev))
4287 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4288 sizeof(support), &support,
4292 /* Set erroneous data reporting if supported to the wideband speech
4295 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4297 struct hci_cp_write_def_err_data_reporting cp;
4298 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4300 if (!(hdev->commands[18] & 0x08) ||
4301 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING))
4304 if (enabled == hdev->err_data_reporting)
4307 memset(&cp, 0, sizeof(cp));
4308 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4309 ERR_DATA_REPORTING_DISABLED;
4311 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4312 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4315 static const struct hci_init_stage hci_init4[] = {
4316 /* HCI_OP_DELETE_STORED_LINK_KEY */
4317 HCI_INIT(hci_delete_stored_link_key_sync),
4318 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4319 HCI_INIT(hci_set_event_mask_page_2_sync),
4320 /* HCI_OP_READ_LOCAL_CODECS */
4321 HCI_INIT(hci_read_local_codecs_sync),
4322 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4323 HCI_INIT(hci_read_local_pairing_opts_sync),
4324 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4325 HCI_INIT(hci_get_mws_transport_config_sync),
4326 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4327 HCI_INIT(hci_read_sync_train_params_sync),
4328 /* HCI_OP_WRITE_SC_SUPPORT */
4329 HCI_INIT(hci_write_sc_support_1_sync),
4330 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4331 HCI_INIT(hci_set_err_data_report_sync),
4335 /* Set Suggested Default Data Length to maximum if supported */
4336 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4338 struct hci_cp_le_write_def_data_len cp;
4340 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4343 memset(&cp, 0, sizeof(cp));
4344 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4345 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4347 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4348 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4351 /* Set Default PHY parameters if command is supported */
4352 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4354 struct hci_cp_le_set_default_phy cp;
4356 if (!(hdev->commands[35] & 0x20))
4359 memset(&cp, 0, sizeof(cp));
4361 cp.tx_phys = hdev->le_tx_def_phys;
4362 cp.rx_phys = hdev->le_rx_def_phys;
4364 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4365 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4368 static const struct hci_init_stage le_init4[] = {
4369 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4370 HCI_INIT(hci_le_set_write_def_data_len_sync),
4371 /* HCI_OP_LE_SET_DEFAULT_PHY */
4372 HCI_INIT(hci_le_set_default_phy_sync),
4376 static int hci_init4_sync(struct hci_dev *hdev)
4380 bt_dev_dbg(hdev, "");
4382 err = hci_init_stage_sync(hdev, hci_init4);
4386 if (lmp_le_capable(hdev))
4387 return hci_init_stage_sync(hdev, le_init4);
4392 static int hci_init_sync(struct hci_dev *hdev)
4396 err = hci_init1_sync(hdev);
4400 if (hci_dev_test_flag(hdev, HCI_SETUP))
4401 hci_debugfs_create_basic(hdev);
4403 err = hci_init2_sync(hdev);
4407 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
4408 * BR/EDR/LE type controllers. AMP controllers only need the
4409 * first two stages of init.
4411 if (hdev->dev_type != HCI_PRIMARY)
4414 err = hci_init3_sync(hdev);
4418 err = hci_init4_sync(hdev);
4422 /* This function is only called when the controller is actually in
4423 * configured state. When the controller is marked as unconfigured,
4424 * this initialization procedure is not run.
4426 * It means that it is possible that a controller runs through its
4427 * setup phase and then discovers missing settings. If that is the
4428 * case, then this function will not be called. It then will only
4429 * be called during the config phase.
4431 * So only when in setup phase or config phase, create the debugfs
4432 * entries and register the SMP channels.
4434 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4435 !hci_dev_test_flag(hdev, HCI_CONFIG))
4438 hci_debugfs_create_common(hdev);
4440 if (lmp_bredr_capable(hdev))
4441 hci_debugfs_create_bredr(hdev);
4443 if (lmp_le_capable(hdev))
4444 hci_debugfs_create_le(hdev);
4449 #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
4451 static const struct {
4452 unsigned long quirk;
4454 } hci_broken_table[] = {
4455 HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
4456 "HCI Read Local Supported Commands not supported"),
4457 HCI_QUIRK_BROKEN(STORED_LINK_KEY,
4458 "HCI Delete Stored Link Key command is advertised, "
4459 "but not supported."),
4460 HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
4461 "HCI Read Transmit Power Level command is advertised, "
4462 "but not supported."),
4463 HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
4464 "HCI Set Event Filter command not supported."),
4465 HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
4466 "HCI Enhanced Setup Synchronous Connection command is "
4467 "advertised, but not supported.")
4470 /* This function handles hdev setup stage:
4473 * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
4475 static int hci_dev_setup_sync(struct hci_dev *hdev)
4478 bool invalid_bdaddr;
4481 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4482 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4485 bt_dev_dbg(hdev, "");
4487 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4490 ret = hdev->setup(hdev);
4492 for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
4493 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4494 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4497 /* The transport driver can set the quirk to mark the
4498 * BD_ADDR invalid before creating the HCI device or in
4499 * its setup callback.
4501 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
4504 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) {
4505 if (!bacmp(&hdev->public_addr, BDADDR_ANY))
4506 hci_dev_get_bd_addr_from_property(hdev);
4508 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4510 ret = hdev->set_bdaddr(hdev,
4511 &hdev->public_addr);
4513 /* If setting of the BD_ADDR from the device
4514 * property succeeds, then treat the address
4515 * as valid even if the invalid BD_ADDR
4516 * quirk indicates otherwise.
4519 invalid_bdaddr = false;
4524 /* The transport driver can set these quirks before
4525 * creating the HCI device or in its setup callback.
4527 * For the invalid BD_ADDR quirk it is possible that
4528 * it becomes a valid address if the bootloader does
4529 * provide it (see above).
4531 * In case any of them is set, the controller has to
4532 * start up as unconfigured.
4534 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4536 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4538 /* For an unconfigured controller it is required to
4539 * read at least the version information provided by
4540 * the Read Local Version Information command.
4542 * If the set_bdaddr driver callback is provided, then
4543 * also the original Bluetooth public device address
4544 * will be read using the Read BD Address command.
4546 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4547 return hci_unconf_init_sync(hdev);
4552 /* This function handles hdev init stage:
4554 * Calls hci_dev_setup_sync to perform setup stage
4555 * Calls hci_init_sync to perform HCI command init sequence
4557 static int hci_dev_init_sync(struct hci_dev *hdev)
4561 bt_dev_dbg(hdev, "");
4563 atomic_set(&hdev->cmd_cnt, 1);
4564 set_bit(HCI_INIT, &hdev->flags);
4566 ret = hci_dev_setup_sync(hdev);
4568 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4569 /* If public address change is configured, ensure that
4570 * the address gets programmed. If the driver does not
4571 * support changing the public address, fail the power
4574 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4576 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4578 ret = -EADDRNOTAVAIL;
4582 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4583 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4584 ret = hci_init_sync(hdev);
4585 if (!ret && hdev->post_init)
4586 ret = hdev->post_init(hdev);
4590 /* If the HCI Reset command is clearing all diagnostic settings,
4591 * then they need to be reprogrammed after the init procedure
4594 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4595 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4596 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4597 ret = hdev->set_diag(hdev, true);
4599 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4604 clear_bit(HCI_INIT, &hdev->flags);
4609 int hci_dev_open_sync(struct hci_dev *hdev)
4613 bt_dev_dbg(hdev, "");
4615 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4620 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4621 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4622 /* Check for rfkill but allow the HCI setup stage to
4623 * proceed (which in itself doesn't cause any RF activity).
4625 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4630 /* Check for valid public address or a configured static
4631 * random address, but let the HCI setup proceed to
4632 * be able to determine if there is a public address
4635 * In case of user channel usage, it is not important
4636 * if a public address or static random address is
4639 * This check is only valid for BR/EDR controllers
4640 * since AMP controllers do not have an address.
4642 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4643 hdev->dev_type == HCI_PRIMARY &&
4644 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4645 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4646 ret = -EADDRNOTAVAIL;
4651 if (test_bit(HCI_UP, &hdev->flags)) {
4656 if (hdev->open(hdev)) {
4661 set_bit(HCI_RUNNING, &hdev->flags);
4662 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4664 ret = hci_dev_init_sync(hdev);
4667 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4668 hci_adv_instances_set_rpa_expired(hdev, true);
4669 set_bit(HCI_UP, &hdev->flags);
4670 hci_sock_dev_event(hdev, HCI_DEV_UP);
4671 hci_leds_update_powered(hdev, true);
4672 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4673 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4674 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4675 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4676 hci_dev_test_flag(hdev, HCI_MGMT) &&
4677 hdev->dev_type == HCI_PRIMARY) {
4678 ret = hci_powered_update_sync(hdev);
4679 mgmt_power_on(hdev, ret);
4682 /* Init failed, cleanup */
4683 flush_work(&hdev->tx_work);
4685 /* Since hci_rx_work() is possible to awake new cmd_work
4686 * it should be flushed first to avoid unexpected call of
4689 flush_work(&hdev->rx_work);
4690 flush_work(&hdev->cmd_work);
4692 skb_queue_purge(&hdev->cmd_q);
4693 skb_queue_purge(&hdev->rx_q);
4698 if (hdev->sent_cmd) {
4699 kfree_skb(hdev->sent_cmd);
4700 hdev->sent_cmd = NULL;
4703 clear_bit(HCI_RUNNING, &hdev->flags);
4704 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4707 hdev->flags &= BIT(HCI_RAW);
4714 /* This function requires the caller holds hdev->lock */
4715 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4717 struct hci_conn_params *p;
4719 list_for_each_entry(p, &hdev->le_conn_params, list) {
4721 hci_conn_drop(p->conn);
4722 hci_conn_put(p->conn);
4725 list_del_init(&p->action);
4728 BT_DBG("All LE pending actions cleared");
4731 static int hci_dev_shutdown(struct hci_dev *hdev)
4734 /* Similar to how we first do setup and then set the exclusive access
4735 * bit for userspace, we must first unset userchannel and then clean up.
4736 * Otherwise, the kernel can't properly use the hci channel to clean up
4737 * the controller (some shutdown routines require sending additional
4738 * commands to the controller for example).
4740 bool was_userchannel =
4741 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
4743 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4744 test_bit(HCI_UP, &hdev->flags)) {
4745 /* Execute vendor specific shutdown routine */
4747 err = hdev->shutdown(hdev);
4750 if (was_userchannel)
4751 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
4756 int hci_dev_close_sync(struct hci_dev *hdev)
4761 bt_dev_dbg(hdev, "");
4763 cancel_delayed_work(&hdev->power_off);
4764 cancel_delayed_work(&hdev->ncmd_timer);
4765 cancel_delayed_work(&hdev->le_scan_disable);
4766 cancel_delayed_work(&hdev->le_scan_restart);
4768 hci_request_cancel_all(hdev);
4770 if (hdev->adv_instance_timeout) {
4771 cancel_delayed_work_sync(&hdev->adv_instance_expire);
4772 hdev->adv_instance_timeout = 0;
4775 err = hci_dev_shutdown(hdev);
4777 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4778 cancel_delayed_work_sync(&hdev->cmd_timer);
4782 hci_leds_update_powered(hdev, false);
4784 /* Flush RX and TX works */
4785 flush_work(&hdev->tx_work);
4786 flush_work(&hdev->rx_work);
4788 if (hdev->discov_timeout > 0) {
4789 hdev->discov_timeout = 0;
4790 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4791 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
4794 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
4795 cancel_delayed_work(&hdev->service_cache);
4797 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4798 struct adv_info *adv_instance;
4800 cancel_delayed_work_sync(&hdev->rpa_expired);
4802 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
4803 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
4806 /* Avoid potential lockdep warnings from the *_flush() calls by
4807 * ensuring the workqueue is empty up front.
4809 drain_workqueue(hdev->workqueue);
4813 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4815 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
4817 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
4818 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4819 hci_dev_test_flag(hdev, HCI_MGMT))
4820 __mgmt_power_off(hdev);
4822 hci_inquiry_cache_flush(hdev);
4823 hci_pend_le_actions_clear(hdev);
4824 hci_conn_hash_flush(hdev);
4825 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
4826 smp_unregister(hdev);
4827 hci_dev_unlock(hdev);
4829 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
4831 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4832 aosp_do_close(hdev);
4833 msft_do_close(hdev);
4840 skb_queue_purge(&hdev->cmd_q);
4841 atomic_set(&hdev->cmd_cnt, 1);
4842 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
4843 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4844 set_bit(HCI_INIT, &hdev->flags);
4845 hci_reset_sync(hdev);
4846 clear_bit(HCI_INIT, &hdev->flags);
4849 /* flush cmd work */
4850 flush_work(&hdev->cmd_work);
4853 skb_queue_purge(&hdev->rx_q);
4854 skb_queue_purge(&hdev->cmd_q);
4855 skb_queue_purge(&hdev->raw_q);
4857 /* Drop last sent command */
4858 if (hdev->sent_cmd) {
4859 cancel_delayed_work_sync(&hdev->cmd_timer);
4860 kfree_skb(hdev->sent_cmd);
4861 hdev->sent_cmd = NULL;
4864 clear_bit(HCI_RUNNING, &hdev->flags);
4865 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4867 /* After this point our queues are empty and no tasks are scheduled. */
4871 hdev->flags &= BIT(HCI_RAW);
4872 hci_dev_clear_volatile_flags(hdev);
4874 /* Controller radio is available but is currently powered down */
4875 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
4877 memset(hdev->eir, 0, sizeof(hdev->eir));
4878 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
4879 bacpy(&hdev->random_addr, BDADDR_ANY);
4885 /* This function perform power on HCI command sequence as follows:
4887 * If controller is already up (HCI_UP) performs hci_powered_update_sync
4888 * sequence otherwise run hci_dev_open_sync which will follow with
4889 * hci_powered_update_sync after the init sequence is completed.
4891 static int hci_power_on_sync(struct hci_dev *hdev)
4895 if (test_bit(HCI_UP, &hdev->flags) &&
4896 hci_dev_test_flag(hdev, HCI_MGMT) &&
4897 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
4898 cancel_delayed_work(&hdev->power_off);
4899 return hci_powered_update_sync(hdev);
4902 err = hci_dev_open_sync(hdev);
4906 /* During the HCI setup phase, a few error conditions are
4907 * ignored and they need to be checked now. If they are still
4908 * valid, it is important to return the device back off.
4910 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
4911 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
4912 (hdev->dev_type == HCI_PRIMARY &&
4913 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4914 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
4915 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
4916 hci_dev_close_sync(hdev);
4917 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
4918 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
4919 HCI_AUTO_OFF_TIMEOUT);
4922 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
4923 /* For unconfigured devices, set the HCI_RAW flag
4924 * so that userspace can easily identify them.
4926 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4927 set_bit(HCI_RAW, &hdev->flags);
4929 /* For fully configured devices, this will send
4930 * the Index Added event. For unconfigured devices,
4931 * it will send Unconfigued Index Added event.
4933 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
4934 * and no event will be send.
4936 mgmt_index_added(hdev);
4937 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
4938 /* When the controller is now configured, then it
4939 * is important to clear the HCI_RAW flag.
4941 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4942 clear_bit(HCI_RAW, &hdev->flags);
4944 /* Powering on the controller with HCI_CONFIG set only
4945 * happens with the transition from unconfigured to
4946 * configured. This will send the Index Added event.
4948 mgmt_index_added(hdev);
4954 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
4956 struct hci_cp_remote_name_req_cancel cp;
4958 memset(&cp, 0, sizeof(cp));
4959 bacpy(&cp.bdaddr, addr);
4961 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
4962 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4965 int hci_stop_discovery_sync(struct hci_dev *hdev)
4967 struct discovery_state *d = &hdev->discovery;
4968 struct inquiry_entry *e;
4971 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
4973 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
4974 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
4975 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
4976 0, NULL, HCI_CMD_TIMEOUT);
4981 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
4982 cancel_delayed_work(&hdev->le_scan_disable);
4983 cancel_delayed_work(&hdev->le_scan_restart);
4985 err = hci_scan_disable_sync(hdev);
4991 err = hci_scan_disable_sync(hdev);
4996 /* Resume advertising if it was paused */
4997 if (use_ll_privacy(hdev))
4998 hci_resume_advertising_sync(hdev);
5000 /* No further actions needed for LE-only discovery */
5001 if (d->type == DISCOV_TYPE_LE)
5004 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5005 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5010 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5016 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
5019 struct hci_cp_disconn_phy_link cp;
5021 memset(&cp, 0, sizeof(cp));
5022 cp.phy_handle = HCI_PHY_HANDLE(handle);
5025 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
5026 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5029 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5032 struct hci_cp_disconnect cp;
5034 if (conn->type == AMP_LINK)
5035 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
5037 memset(&cp, 0, sizeof(cp));
5038 cp.handle = cpu_to_le16(conn->handle);
5041 /* Wait for HCI_EV_DISCONN_COMPLETE not HCI_EV_CMD_STATUS when not
5044 if (!hdev->suspended)
5045 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5047 HCI_EV_DISCONN_COMPLETE,
5048 HCI_CMD_TIMEOUT, NULL);
5050 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5054 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5055 struct hci_conn *conn)
5057 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5060 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5061 6, &conn->dst, HCI_CMD_TIMEOUT);
5064 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn)
5066 if (conn->type == LE_LINK)
5067 return hci_le_connect_cancel_sync(hdev, conn);
5069 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5072 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5073 6, &conn->dst, HCI_CMD_TIMEOUT);
5076 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5079 struct hci_cp_reject_sync_conn_req cp;
5081 memset(&cp, 0, sizeof(cp));
5082 bacpy(&cp.bdaddr, &conn->dst);
5085 /* SCO rejection has its own limited set of
5086 * allowed error values (0x0D-0x0F).
5088 if (reason < 0x0d || reason > 0x0f)
5089 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5091 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5092 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5095 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5098 struct hci_cp_reject_conn_req cp;
5100 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5101 return hci_reject_sco_sync(hdev, conn, reason);
5103 memset(&cp, 0, sizeof(cp));
5104 bacpy(&cp.bdaddr, &conn->dst);
5107 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5108 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5111 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5115 switch (conn->state) {
5118 return hci_disconnect_sync(hdev, conn, reason);
5120 err = hci_connect_cancel_sync(hdev, conn);
5121 /* Cleanup hci_conn object if it cannot be cancelled as it
5122 * likelly means the controller and host stack are out of sync.
5126 hci_conn_failed(conn, err);
5127 hci_dev_unlock(hdev);
5131 return hci_reject_conn_sync(hdev, conn, reason);
5133 conn->state = BT_CLOSED;
5140 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5142 struct hci_conn *conn, *tmp;
5145 list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
5146 err = hci_abort_conn_sync(hdev, conn, reason);
5154 /* This function perform power off HCI command sequence as follows:
5158 * Disconnect all connections
5159 * hci_dev_close_sync
5161 static int hci_power_off_sync(struct hci_dev *hdev)
5165 /* If controller is already down there is nothing to do */
5166 if (!test_bit(HCI_UP, &hdev->flags))
5169 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5170 test_bit(HCI_PSCAN, &hdev->flags)) {
5171 err = hci_write_scan_enable_sync(hdev, 0x00);
5176 err = hci_clear_adv_sync(hdev, NULL, false);
5180 err = hci_stop_discovery_sync(hdev);
5184 /* Terminated due to Power Off */
5185 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5189 return hci_dev_close_sync(hdev);
5192 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5195 return hci_power_on_sync(hdev);
5197 return hci_power_off_sync(hdev);
5200 static int hci_write_iac_sync(struct hci_dev *hdev)
5202 struct hci_cp_write_current_iac_lap cp;
5204 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5207 memset(&cp, 0, sizeof(cp));
5209 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5210 /* Limited discoverable mode */
5211 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5212 cp.iac_lap[0] = 0x00; /* LIAC */
5213 cp.iac_lap[1] = 0x8b;
5214 cp.iac_lap[2] = 0x9e;
5215 cp.iac_lap[3] = 0x33; /* GIAC */
5216 cp.iac_lap[4] = 0x8b;
5217 cp.iac_lap[5] = 0x9e;
5219 /* General discoverable mode */
5221 cp.iac_lap[0] = 0x33; /* GIAC */
5222 cp.iac_lap[1] = 0x8b;
5223 cp.iac_lap[2] = 0x9e;
5226 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5227 (cp.num_iac * 3) + 1, &cp,
5231 int hci_update_discoverable_sync(struct hci_dev *hdev)
5235 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5236 err = hci_write_iac_sync(hdev);
5240 err = hci_update_scan_sync(hdev);
5244 err = hci_update_class_sync(hdev);
5249 /* Advertising instances don't use the global discoverable setting, so
5250 * only update AD if advertising was enabled using Set Advertising.
5252 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5253 err = hci_update_adv_data_sync(hdev, 0x00);
5257 /* Discoverable mode affects the local advertising
5258 * address in limited privacy mode.
5260 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5261 if (ext_adv_capable(hdev))
5262 err = hci_start_ext_adv_sync(hdev, 0x00);
5264 err = hci_enable_advertising_sync(hdev);
5271 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5273 return hci_update_discoverable_sync(hdev);
5276 int hci_update_discoverable(struct hci_dev *hdev)
5278 /* Only queue if it would have any effect */
5279 if (hdev_is_powered(hdev) &&
5280 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5281 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5282 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5283 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5289 int hci_update_connectable_sync(struct hci_dev *hdev)
5293 err = hci_update_scan_sync(hdev);
5297 /* If BR/EDR is not enabled and we disable advertising as a
5298 * by-product of disabling connectable, we need to update the
5299 * advertising flags.
5301 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5302 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5304 /* Update the advertising parameters if necessary */
5305 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5306 !list_empty(&hdev->adv_instances)) {
5307 if (ext_adv_capable(hdev))
5308 err = hci_start_ext_adv_sync(hdev,
5309 hdev->cur_adv_instance);
5311 err = hci_enable_advertising_sync(hdev);
5317 return hci_update_passive_scan_sync(hdev);
5320 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
5322 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5323 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5324 struct hci_cp_inquiry cp;
5326 bt_dev_dbg(hdev, "");
5328 if (hci_dev_test_flag(hdev, HCI_INQUIRY))
5332 hci_inquiry_cache_flush(hdev);
5333 hci_dev_unlock(hdev);
5335 memset(&cp, 0, sizeof(cp));
5337 if (hdev->discovery.limited)
5338 memcpy(&cp.lap, liac, sizeof(cp.lap));
5340 memcpy(&cp.lap, giac, sizeof(cp.lap));
5344 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5345 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5348 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5351 /* Accept list is not used for discovery */
5352 u8 filter_policy = 0x00;
5353 /* Default is to enable duplicates filter */
5354 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5357 bt_dev_dbg(hdev, "");
5359 /* If controller is scanning, it means the passive scanning is
5360 * running. Thus, we should temporarily stop it in order to set the
5361 * discovery scanning parameters.
5363 err = hci_scan_disable_sync(hdev);
5365 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5369 cancel_interleave_scan(hdev);
5371 /* Pause advertising since active scanning disables address resolution
5372 * which advertising depend on in order to generate its RPAs.
5374 if (use_ll_privacy(hdev) && hci_dev_test_flag(hdev, HCI_PRIVACY)) {
5375 err = hci_pause_advertising_sync(hdev);
5377 bt_dev_err(hdev, "pause advertising failed: %d", err);
5382 /* Disable address resolution while doing active scanning since the
5383 * accept list shall not be used and all reports shall reach the host
5386 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
5388 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
5393 /* All active scans will be done with either a resolvable private
5394 * address (when privacy feature has been enabled) or non-resolvable
5397 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5400 own_addr_type = ADDR_LE_DEV_PUBLIC;
5402 if (hci_is_adv_monitoring(hdev)) {
5403 /* Duplicate filter should be disabled when some advertisement
5404 * monitor is activated, otherwise AdvMon can only receive one
5405 * advertisement for one peer(*) during active scanning, and
5406 * might report loss to these peers.
5408 * Note that different controllers have different meanings of
5409 * |duplicate|. Some of them consider packets with the same
5410 * address as duplicate, and others consider packets with the
5411 * same address and the same RSSI as duplicate. Although in the
5412 * latter case we don't need to disable duplicate filter, but
5413 * it is common to have active scanning for a short period of
5414 * time, the power impact should be neglectable.
5416 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5419 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5420 hdev->le_scan_window_discovery,
5421 own_addr_type, filter_policy, filter_dup);
5426 /* Resume advertising if it was paused */
5427 if (use_ll_privacy(hdev))
5428 hci_resume_advertising_sync(hdev);
5430 /* Resume passive scanning */
5431 hci_update_passive_scan_sync(hdev);
5435 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5439 bt_dev_dbg(hdev, "");
5441 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5445 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5448 int hci_start_discovery_sync(struct hci_dev *hdev)
5450 unsigned long timeout;
5453 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5455 switch (hdev->discovery.type) {
5456 case DISCOV_TYPE_BREDR:
5457 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
5458 case DISCOV_TYPE_INTERLEAVED:
5459 /* When running simultaneous discovery, the LE scanning time
5460 * should occupy the whole discovery time sine BR/EDR inquiry
5461 * and LE scanning are scheduled by the controller.
5463 * For interleaving discovery in comparison, BR/EDR inquiry
5464 * and LE scanning are done sequentially with separate
5467 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5469 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5470 /* During simultaneous discovery, we double LE scan
5471 * interval. We must leave some time for the controller
5472 * to do BR/EDR inquiry.
5474 err = hci_start_interleaved_discovery_sync(hdev);
5478 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5479 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5481 case DISCOV_TYPE_LE:
5482 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5483 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5492 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
5494 /* When service discovery is used and the controller has a
5495 * strict duplicate filter, it is important to remember the
5496 * start and duration of the scan. This is required for
5497 * restarting scanning during the discovery phase.
5499 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5500 hdev->discovery.result_filtering) {
5501 hdev->discovery.scan_start = jiffies;
5502 hdev->discovery.scan_duration = timeout;
5505 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
5510 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
5512 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5513 case HCI_ADV_MONITOR_EXT_MSFT:
5514 msft_suspend_sync(hdev);
5521 /* This function disables discovery and mark it as paused */
5522 static int hci_pause_discovery_sync(struct hci_dev *hdev)
5524 int old_state = hdev->discovery.state;
5527 /* If discovery already stopped/stopping/paused there nothing to do */
5528 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
5529 hdev->discovery_paused)
5532 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5533 err = hci_stop_discovery_sync(hdev);
5537 hdev->discovery_paused = true;
5538 hdev->discovery_old_state = old_state;
5539 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5544 static int hci_update_event_filter_sync(struct hci_dev *hdev)
5546 struct bdaddr_list_with_flags *b;
5547 u8 scan = SCAN_DISABLED;
5548 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
5551 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5554 /* Some fake CSR controllers lock up after setting this type of
5555 * filter, so avoid sending the request altogether.
5557 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
5560 /* Always clear event filter when starting */
5561 hci_clear_event_filter_sync(hdev);
5563 list_for_each_entry(b, &hdev->accept_list, list) {
5564 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
5567 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
5569 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
5570 HCI_CONN_SETUP_ALLOW_BDADDR,
5572 HCI_CONN_SETUP_AUTO_ON);
5574 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
5580 if (scan && !scanning)
5581 hci_write_scan_enable_sync(hdev, scan);
5582 else if (!scan && scanning)
5583 hci_write_scan_enable_sync(hdev, scan);
5588 /* This function disables scan (BR and LE) and mark it as paused */
5589 static int hci_pause_scan_sync(struct hci_dev *hdev)
5591 if (hdev->scanning_paused)
5594 /* Disable page scan if enabled */
5595 if (test_bit(HCI_PSCAN, &hdev->flags))
5596 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
5598 hci_scan_disable_sync(hdev);
5600 hdev->scanning_paused = true;
5605 /* This function performs the HCI suspend procedures in the follow order:
5607 * Pause discovery (active scanning/inquiry)
5608 * Pause Directed Advertising/Advertising
5609 * Pause Scanning (passive scanning in case discovery was not active)
5610 * Disconnect all connections
5611 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
5613 * Update event mask (only set events that are allowed to wake up the host)
5614 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
5615 * Update passive scanning (lower duty cycle)
5616 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
5618 int hci_suspend_sync(struct hci_dev *hdev)
5622 /* If marked as suspended there nothing to do */
5623 if (hdev->suspended)
5626 /* Mark device as suspended */
5627 hdev->suspended = true;
5629 /* Pause discovery if not already stopped */
5630 hci_pause_discovery_sync(hdev);
5632 /* Pause other advertisements */
5633 hci_pause_advertising_sync(hdev);
5635 /* Suspend monitor filters */
5636 hci_suspend_monitor_sync(hdev);
5638 /* Prevent disconnects from causing scanning to be re-enabled */
5639 hci_pause_scan_sync(hdev);
5641 if (hci_conn_count(hdev)) {
5642 /* Soft disconnect everything (power off) */
5643 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5645 /* Set state to BT_RUNNING so resume doesn't notify */
5646 hdev->suspend_state = BT_RUNNING;
5647 hci_resume_sync(hdev);
5651 /* Update event mask so only the allowed event can wakeup the
5654 hci_set_event_mask_sync(hdev);
5657 /* Only configure accept list if disconnect succeeded and wake
5658 * isn't being prevented.
5660 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5661 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5665 /* Unpause to take care of updating scanning params */
5666 hdev->scanning_paused = false;
5668 /* Enable event filter for paired devices */
5669 hci_update_event_filter_sync(hdev);
5671 /* Update LE passive scan if enabled */
5672 hci_update_passive_scan_sync(hdev);
5674 /* Pause scan changes again. */
5675 hdev->scanning_paused = true;
5677 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5682 /* This function resumes discovery */
5683 static int hci_resume_discovery_sync(struct hci_dev *hdev)
5687 /* If discovery not paused there nothing to do */
5688 if (!hdev->discovery_paused)
5691 hdev->discovery_paused = false;
5693 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
5695 err = hci_start_discovery_sync(hdev);
5697 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
5703 static void hci_resume_monitor_sync(struct hci_dev *hdev)
5705 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5706 case HCI_ADV_MONITOR_EXT_MSFT:
5707 msft_resume_sync(hdev);
5714 /* This function resume scan and reset paused flag */
5715 static int hci_resume_scan_sync(struct hci_dev *hdev)
5717 if (!hdev->scanning_paused)
5720 hdev->scanning_paused = false;
5722 hci_update_scan_sync(hdev);
5724 /* Reset passive scanning to normal */
5725 hci_update_passive_scan_sync(hdev);
5730 /* This function performs the HCI suspend procedures in the follow order:
5732 * Restore event mask
5733 * Clear event filter
5734 * Update passive scanning (normal duty cycle)
5735 * Resume Directed Advertising/Advertising
5736 * Resume discovery (active scanning/inquiry)
5738 int hci_resume_sync(struct hci_dev *hdev)
5740 /* If not marked as suspended there nothing to do */
5741 if (!hdev->suspended)
5744 hdev->suspended = false;
5746 /* Restore event mask */
5747 hci_set_event_mask_sync(hdev);
5749 /* Clear any event filters and restore scan state */
5750 hci_clear_event_filter_sync(hdev);
5752 /* Resume scanning */
5753 hci_resume_scan_sync(hdev);
5755 /* Resume monitor filters */
5756 hci_resume_monitor_sync(hdev);
5758 /* Resume other advertisements */
5759 hci_resume_advertising_sync(hdev);
5761 /* Resume discovery */
5762 hci_resume_discovery_sync(hdev);
5767 static bool conn_use_rpa(struct hci_conn *conn)
5769 struct hci_dev *hdev = conn->hdev;
5771 return hci_dev_test_flag(hdev, HCI_PRIVACY);
5774 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
5775 struct hci_conn *conn)
5777 struct hci_cp_le_set_ext_adv_params cp;
5779 bdaddr_t random_addr;
5782 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5787 /* Set require_privacy to false so that the remote device has a
5788 * chance of identifying us.
5790 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
5791 &own_addr_type, &random_addr);
5795 memset(&cp, 0, sizeof(cp));
5797 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
5798 cp.own_addr_type = own_addr_type;
5799 cp.channel_map = hdev->le_adv_channel_map;
5800 cp.tx_power = HCI_TX_POWER_INVALID;
5801 cp.primary_phy = HCI_ADV_PHY_1M;
5802 cp.secondary_phy = HCI_ADV_PHY_1M;
5803 cp.handle = 0x00; /* Use instance 0 for directed adv */
5804 cp.own_addr_type = own_addr_type;
5805 cp.peer_addr_type = conn->dst_type;
5806 bacpy(&cp.peer_addr, &conn->dst);
5808 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
5809 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
5810 * does not supports advertising data when the advertising set already
5811 * contains some, the controller shall return erroc code 'Invalid
5812 * HCI Command Parameters(0x12).
5813 * So it is required to remove adv set for handle 0x00. since we use
5814 * instance 0 for directed adv.
5816 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
5820 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
5821 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5825 /* Check if random address need to be updated */
5826 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
5827 bacmp(&random_addr, BDADDR_ANY) &&
5828 bacmp(&random_addr, &hdev->random_addr)) {
5829 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
5835 return hci_enable_ext_advertising_sync(hdev, 0x00);
5838 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
5839 struct hci_conn *conn)
5841 struct hci_cp_le_set_adv_param cp;
5846 if (ext_adv_capable(hdev))
5847 return hci_le_ext_directed_advertising_sync(hdev, conn);
5849 /* Clear the HCI_LE_ADV bit temporarily so that the
5850 * hci_update_random_address knows that it's safe to go ahead
5851 * and write a new random address. The flag will be set back on
5852 * as soon as the SET_ADV_ENABLE HCI command completes.
5854 hci_dev_clear_flag(hdev, HCI_LE_ADV);
5856 /* Set require_privacy to false so that the remote device has a
5857 * chance of identifying us.
5859 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5864 memset(&cp, 0, sizeof(cp));
5866 /* Some controllers might reject command if intervals are not
5867 * within range for undirected advertising.
5868 * BCM20702A0 is known to be affected by this.
5870 cp.min_interval = cpu_to_le16(0x0020);
5871 cp.max_interval = cpu_to_le16(0x0020);
5873 cp.type = LE_ADV_DIRECT_IND;
5874 cp.own_address_type = own_addr_type;
5875 cp.direct_addr_type = conn->dst_type;
5876 bacpy(&cp.direct_addr, &conn->dst);
5877 cp.channel_map = hdev->le_adv_channel_map;
5879 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
5880 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5886 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
5887 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
5890 static void set_ext_conn_params(struct hci_conn *conn,
5891 struct hci_cp_le_ext_conn_param *p)
5893 struct hci_dev *hdev = conn->hdev;
5895 memset(p, 0, sizeof(*p));
5897 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
5898 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
5899 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
5900 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
5901 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
5902 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
5903 p->min_ce_len = cpu_to_le16(0x0000);
5904 p->max_ce_len = cpu_to_le16(0x0000);
5907 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
5908 struct hci_conn *conn, u8 own_addr_type)
5910 struct hci_cp_le_ext_create_conn *cp;
5911 struct hci_cp_le_ext_conn_param *p;
5912 u8 data[sizeof(*cp) + sizeof(*p) * 3];
5916 p = (void *)cp->data;
5918 memset(cp, 0, sizeof(*cp));
5920 bacpy(&cp->peer_addr, &conn->dst);
5921 cp->peer_addr_type = conn->dst_type;
5922 cp->own_addr_type = own_addr_type;
5926 if (scan_1m(hdev)) {
5927 cp->phys |= LE_SCAN_PHY_1M;
5928 set_ext_conn_params(conn, p);
5934 if (scan_2m(hdev)) {
5935 cp->phys |= LE_SCAN_PHY_2M;
5936 set_ext_conn_params(conn, p);
5942 if (scan_coded(hdev)) {
5943 cp->phys |= LE_SCAN_PHY_CODED;
5944 set_ext_conn_params(conn, p);
5949 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
5951 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
5952 conn->conn_timeout, NULL);
5955 int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
5957 struct hci_cp_le_create_conn cp;
5958 struct hci_conn_params *params;
5962 /* If requested to connect as peripheral use directed advertising */
5963 if (conn->role == HCI_ROLE_SLAVE) {
5964 /* If we're active scanning and simultaneous roles is not
5965 * enabled simply reject the attempt.
5967 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
5968 hdev->le_scan_type == LE_SCAN_ACTIVE &&
5969 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
5974 /* Pause advertising while doing directed advertising. */
5975 hci_pause_advertising_sync(hdev);
5977 err = hci_le_directed_advertising_sync(hdev, conn);
5981 /* Disable advertising if simultaneous roles is not in use. */
5982 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
5983 hci_pause_advertising_sync(hdev);
5985 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
5987 conn->le_conn_min_interval = params->conn_min_interval;
5988 conn->le_conn_max_interval = params->conn_max_interval;
5989 conn->le_conn_latency = params->conn_latency;
5990 conn->le_supv_timeout = params->supervision_timeout;
5992 conn->le_conn_min_interval = hdev->le_conn_min_interval;
5993 conn->le_conn_max_interval = hdev->le_conn_max_interval;
5994 conn->le_conn_latency = hdev->le_conn_latency;
5995 conn->le_supv_timeout = hdev->le_supv_timeout;
5998 /* If controller is scanning, we stop it since some controllers are
5999 * not able to scan and connect at the same time. Also set the
6000 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
6001 * handler for scan disabling knows to set the correct discovery
6004 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6005 hci_scan_disable_sync(hdev);
6006 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6009 /* Update random address, but set require_privacy to false so
6010 * that we never connect with an non-resolvable address.
6012 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6017 if (use_ext_conn(hdev)) {
6018 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6022 memset(&cp, 0, sizeof(cp));
6024 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6025 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6027 bacpy(&cp.peer_addr, &conn->dst);
6028 cp.peer_addr_type = conn->dst_type;
6029 cp.own_address_type = own_addr_type;
6030 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6031 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6032 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
6033 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6034 cp.min_ce_len = cpu_to_le16(0x0000);
6035 cp.max_ce_len = cpu_to_le16(0x0000);
6037 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6039 * If this event is unmasked and the HCI_LE_Connection_Complete event
6040 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6041 * sent when a new connection has been created.
6043 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6045 use_enhanced_conn_complete(hdev) ?
6046 HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6047 HCI_EV_LE_CONN_COMPLETE,
6048 conn->conn_timeout, NULL);
6051 /* Re-enable advertising after the connection attempt is finished. */
6052 hci_resume_advertising_sync(hdev);
6056 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6058 struct hci_cp_le_remove_cig cp;
6060 memset(&cp, 0, sizeof(cp));
6063 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6064 &cp, HCI_CMD_TIMEOUT);
6067 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6069 struct hci_cp_le_big_term_sync cp;
6071 memset(&cp, 0, sizeof(cp));
6074 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6075 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6078 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6080 struct hci_cp_le_pa_term_sync cp;
6082 memset(&cp, 0, sizeof(cp));
6083 cp.handle = cpu_to_le16(handle);
6085 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6086 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6089 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6090 bool use_rpa, struct adv_info *adv_instance,
6091 u8 *own_addr_type, bdaddr_t *rand_addr)
6095 bacpy(rand_addr, BDADDR_ANY);
6097 /* If privacy is enabled use a resolvable private address. If
6098 * current RPA has expired then generate a new one.
6101 /* If Controller supports LL Privacy use own address type is
6104 if (use_ll_privacy(hdev))
6105 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
6107 *own_addr_type = ADDR_LE_DEV_RANDOM;
6110 if (adv_rpa_valid(adv_instance))
6113 if (rpa_valid(hdev))
6117 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6119 bt_dev_err(hdev, "failed to generate new RPA");
6123 bacpy(rand_addr, &hdev->rpa);
6128 /* In case of required privacy without resolvable private address,
6129 * use an non-resolvable private address. This is useful for
6130 * non-connectable advertising.
6132 if (require_privacy) {
6136 /* The non-resolvable private address is generated
6137 * from random six bytes with the two most significant
6140 get_random_bytes(&nrpa, 6);
6143 /* The non-resolvable private address shall not be
6144 * equal to the public address.
6146 if (bacmp(&hdev->bdaddr, &nrpa))
6150 *own_addr_type = ADDR_LE_DEV_RANDOM;
6151 bacpy(rand_addr, &nrpa);
6156 /* No privacy so use a public address. */
6157 *own_addr_type = ADDR_LE_DEV_PUBLIC;
6162 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6164 u8 instance = *(u8 *)data;
6168 return hci_update_adv_data_sync(hdev, instance);
6171 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6173 u8 *inst_ptr = kmalloc(1, GFP_KERNEL);
6178 *inst_ptr = instance;
6179 return hci_cmd_sync_queue(hdev, _update_adv_data_sync, inst_ptr, NULL);