Skip to content

Commit dd522a7

Browse files
apanditholtmann
authored andcommitted
Bluetooth: Handle LE devices during suspend
To handle LE devices, we must first disable passive scanning and disconnect all connected devices. Once that is complete, we update the whitelist and re-enable scanning Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent 4f40afc commit dd522a7

2 files changed

Lines changed: 113 additions & 54 deletions

File tree

‎include/net/bluetooth/hci_core.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ struct hci_conn_params {
607607

608608
struct hci_conn *conn;
609609
bool explicit_connect;
610+
bool wakeable;
610611
};
611612

612613
extern struct list_head hci_dev_list;

‎net/bluetooth/hci_request.c‎

Lines changed: 112 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#define HCI_REQ_PEND 1
3535
#define HCI_REQ_CANCELED 2
3636

37+
#define LE_SUSPEND_SCAN_WINDOW 0x0012
38+
#define LE_SUSPEND_SCAN_INTERVAL 0x0060
39+
3740
void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
3841
{
3942
skb_queue_head_init(&req->cmd_q);
@@ -654,6 +657,11 @@ void hci_req_add_le_scan_disable(struct hci_request *req)
654657
{
655658
struct hci_dev *hdev = req->hdev;
656659

660+
if (hdev->scanning_paused) {
661+
bt_dev_dbg(hdev, "Scanning is paused for suspend");
662+
return;
663+
}
664+
657665
if (use_ext_scan(hdev)) {
658666
struct hci_cp_le_set_ext_scan_enable cp;
659667

@@ -670,23 +678,70 @@ void hci_req_add_le_scan_disable(struct hci_request *req)
670678
}
671679
}
672680

673-
static void add_to_white_list(struct hci_request *req,
674-
struct hci_conn_params *params)
681+
static void del_from_white_list(struct hci_request *req, bdaddr_t *bdaddr,
682+
u8 bdaddr_type)
683+
{
684+
struct hci_cp_le_del_from_white_list cp;
685+
686+
cp.bdaddr_type = bdaddr_type;
687+
bacpy(&cp.bdaddr, bdaddr);
688+
689+
bt_dev_dbg(req->hdev, "Remove %pMR (0x%x) from whitelist", &cp.bdaddr,
690+
cp.bdaddr_type);
691+
hci_req_add(req, HCI_OP_LE_DEL_FROM_WHITE_LIST, sizeof(cp), &cp);
692+
}
693+
694+
/* Adds connection to white list if needed. On error, returns -1. */
695+
static int add_to_white_list(struct hci_request *req,
696+
struct hci_conn_params *params, u8 *num_entries,
697+
bool allow_rpa)
675698
{
676699
struct hci_cp_le_add_to_white_list cp;
700+
struct hci_dev *hdev = req->hdev;
701+
702+
/* Already in white list */
703+
if (hci_bdaddr_list_lookup(&hdev->le_white_list, &params->addr,
704+
params->addr_type))
705+
return 0;
706+
707+
/* Select filter policy to accept all advertising */
708+
if (*num_entries >= hdev->le_white_list_size)
709+
return -1;
677710

711+
/* White list can not be used with RPAs */
712+
if (!allow_rpa &&
713+
hci_find_irk_by_addr(hdev, &params->addr, params->addr_type)) {
714+
return -1;
715+
}
716+
717+
/* During suspend, only wakeable devices can be in whitelist */
718+
if (hdev->suspended && !params->wakeable)
719+
return 0;
720+
721+
*num_entries += 1;
678722
cp.bdaddr_type = params->addr_type;
679723
bacpy(&cp.bdaddr, &params->addr);
680724

725+
bt_dev_dbg(hdev, "Add %pMR (0x%x) to whitelist", &cp.bdaddr,
726+
cp.bdaddr_type);
681727
hci_req_add(req, HCI_OP_LE_ADD_TO_WHITE_LIST, sizeof(cp), &cp);
728+
729+
return 0;
682730
}
683731

684732
static u8 update_white_list(struct hci_request *req)
685733
{
686734
struct hci_dev *hdev = req->hdev;
687735
struct hci_conn_params *params;
688736
struct bdaddr_list *b;
689-
uint8_t white_list_entries = 0;
737+
u8 num_entries = 0;
738+
bool pend_conn, pend_report;
739+
/* We allow whitelisting even with RPAs in suspend. In the worst case,
740+
* we won't be able to wake from devices that use the privacy1.2
741+
* features. Additionally, once we support privacy1.2 and IRK
742+
* offloading, we can update this to also check for those conditions.
743+
*/
744+
bool allow_rpa = hdev->suspended;
690745

691746
/* Go through the current white list programmed into the
692747
* controller one by one and check if that address is still
@@ -695,29 +750,28 @@ static u8 update_white_list(struct hci_request *req)
695750
* command to remove it from the controller.
696751
*/
697752
list_for_each_entry(b, &hdev->le_white_list, list) {
698-
/* If the device is neither in pend_le_conns nor
699-
* pend_le_reports then remove it from the whitelist.
753+
pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
754+
&b->bdaddr,
755+
b->bdaddr_type);
756+
pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
757+
&b->bdaddr,
758+
b->bdaddr_type);
759+
760+
/* If the device is not likely to connect or report,
761+
* remove it from the whitelist.
700762
*/
701-
if (!hci_pend_le_action_lookup(&hdev->pend_le_conns,
702-
&b->bdaddr, b->bdaddr_type) &&
703-
!hci_pend_le_action_lookup(&hdev->pend_le_reports,
704-
&b->bdaddr, b->bdaddr_type)) {
705-
struct hci_cp_le_del_from_white_list cp;
706-
707-
cp.bdaddr_type = b->bdaddr_type;
708-
bacpy(&cp.bdaddr, &b->bdaddr);
709-
710-
hci_req_add(req, HCI_OP_LE_DEL_FROM_WHITE_LIST,
711-
sizeof(cp), &cp);
763+
if (!pend_conn && !pend_report) {
764+
del_from_white_list(req, &b->bdaddr, b->bdaddr_type);
712765
continue;
713766
}
714767

715-
if (hci_find_irk_by_addr(hdev, &b->bdaddr, b->bdaddr_type)) {
716-
/* White list can not be used with RPAs */
768+
/* White list can not be used with RPAs */
769+
if (!allow_rpa &&
770+
hci_find_irk_by_addr(hdev, &b->bdaddr, b->bdaddr_type)) {
717771
return 0x00;
718772
}
719773

720-
white_list_entries++;
774+
num_entries++;
721775
}
722776

723777
/* Since all no longer valid white list entries have been
@@ -731,47 +785,17 @@ static u8 update_white_list(struct hci_request *req)
731785
* white list.
732786
*/
733787
list_for_each_entry(params, &hdev->pend_le_conns, action) {
734-
if (hci_bdaddr_list_lookup(&hdev->le_white_list,
735-
&params->addr, params->addr_type))
736-
continue;
737-
738-
if (white_list_entries >= hdev->le_white_list_size) {
739-
/* Select filter policy to accept all advertising */
788+
if (add_to_white_list(req, params, &num_entries, allow_rpa))
740789
return 0x00;
741-
}
742-
743-
if (hci_find_irk_by_addr(hdev, &params->addr,
744-
params->addr_type)) {
745-
/* White list can not be used with RPAs */
746-
return 0x00;
747-
}
748-
749-
white_list_entries++;
750-
add_to_white_list(req, params);
751790
}
752791

753792
/* After adding all new pending connections, walk through
754793
* the list of pending reports and also add these to the
755-
* white list if there is still space.
794+
* white list if there is still space. Abort if space runs out.
756795
*/
757796
list_for_each_entry(params, &hdev->pend_le_reports, action) {
758-
if (hci_bdaddr_list_lookup(&hdev->le_white_list,
759-
&params->addr, params->addr_type))
760-
continue;
761-
762-
if (white_list_entries >= hdev->le_white_list_size) {
763-
/* Select filter policy to accept all advertising */
797+
if (add_to_white_list(req, params, &num_entries, allow_rpa))
764798
return 0x00;
765-
}
766-
767-
if (hci_find_irk_by_addr(hdev, &params->addr,
768-
params->addr_type)) {
769-
/* White list can not be used with RPAs */
770-
return 0x00;
771-
}
772-
773-
white_list_entries++;
774-
add_to_white_list(req, params);
775799
}
776800

777801
/* Select filter policy to use white list */
@@ -866,6 +890,12 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
866890
struct hci_dev *hdev = req->hdev;
867891
u8 own_addr_type;
868892
u8 filter_policy;
893+
u8 window, interval;
894+
895+
if (hdev->scanning_paused) {
896+
bt_dev_dbg(hdev, "Scanning is paused for suspend");
897+
return;
898+
}
869899

870900
/* Set require_privacy to false since no SCAN_REQ are send
871901
* during passive scanning. Not using an non-resolvable address
@@ -896,8 +926,17 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
896926
(hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
897927
filter_policy |= 0x02;
898928

899-
hci_req_start_scan(req, LE_SCAN_PASSIVE, hdev->le_scan_interval,
900-
hdev->le_scan_window, own_addr_type, filter_policy);
929+
if (hdev->suspended) {
930+
window = LE_SUSPEND_SCAN_WINDOW;
931+
interval = LE_SUSPEND_SCAN_INTERVAL;
932+
} else {
933+
window = hdev->le_scan_window;
934+
interval = hdev->le_scan_interval;
935+
}
936+
937+
bt_dev_dbg(hdev, "LE passive scan with whitelist = %d", filter_policy);
938+
hci_req_start_scan(req, LE_SCAN_PASSIVE, interval, window,
939+
own_addr_type, filter_policy);
901940
}
902941

903942
static u8 get_adv_instance_scan_rsp_len(struct hci_dev *hdev, u8 instance)
@@ -957,6 +996,18 @@ static void hci_req_set_event_filter(struct hci_request *req)
957996
hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
958997
}
959998

999+
static void hci_req_config_le_suspend_scan(struct hci_request *req)
1000+
{
1001+
/* Can't change params without disabling first */
1002+
hci_req_add_le_scan_disable(req);
1003+
1004+
/* Configure params and enable scanning */
1005+
hci_req_add_le_passive_scan(req);
1006+
1007+
/* Block suspend notifier on response */
1008+
set_bit(SUSPEND_SCAN_ENABLE, req->hdev->suspend_tasks);
1009+
}
1010+
9601011
static void suspend_req_complete(struct hci_dev *hdev, u8 status, u16 opcode)
9611012
{
9621013
bt_dev_dbg(hdev, "Request complete opcode=0x%x, status=0x%x", opcode,
@@ -991,6 +1042,9 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next)
9911042
page_scan = SCAN_DISABLED;
9921043
hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &page_scan);
9931044

1045+
/* Disable LE passive scan */
1046+
hci_req_add_le_scan_disable(&req);
1047+
9941048
/* Mark task needing completion */
9951049
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
9961050

@@ -1018,6 +1072,8 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next)
10181072
hdev->scanning_paused = false;
10191073
/* Enable event filter for paired devices */
10201074
hci_req_set_event_filter(&req);
1075+
/* Enable passive scan at lower duty cycle */
1076+
hci_req_config_le_suspend_scan(&req);
10211077
/* Pause scan changes again. */
10221078
hdev->scanning_paused = true;
10231079
hci_req_run(&req, suspend_req_complete);
@@ -1026,6 +1082,8 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next)
10261082
hdev->scanning_paused = false;
10271083

10281084
hci_req_clear_event_filter(&req);
1085+
/* Reset passive/background scanning to normal */
1086+
hci_req_config_le_suspend_scan(&req);
10291087
hci_req_run(&req, suspend_req_complete);
10301088
}
10311089

0 commit comments

Comments
 (0)