Skip to content

Commit 4f40afc

Browse files
apanditholtmann
authored andcommitted
Bluetooth: Handle BR/EDR devices during suspend
To handle BR/EDR devices, we first disable page scan and disconnect all connected devices. Once that is complete, we add event filters (for devices that can wake the system) and re-enable page scan. Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent 9952d90 commit 4f40afc

5 files changed

Lines changed: 169 additions & 10 deletions

File tree

‎include/net/bluetooth/hci.h‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -942,10 +942,14 @@ struct hci_cp_sniff_subrate {
942942
#define HCI_OP_RESET 0x0c03
943943

944944
#define HCI_OP_SET_EVENT_FLT 0x0c05
945-
struct hci_cp_set_event_flt {
946-
__u8 flt_type;
947-
__u8 cond_type;
948-
__u8 condition[];
945+
#define HCI_SET_EVENT_FLT_SIZE 9
946+
struct hci_cp_set_event_filter {
947+
__u8 flt_type;
948+
__u8 cond_type;
949+
struct {
950+
bdaddr_t bdaddr;
951+
__u8 auto_accept;
952+
} __packed addr_conn_flt;
949953
} __packed;
950954

951955
/* Filter types */
@@ -959,8 +963,9 @@ struct hci_cp_set_event_flt {
959963
#define HCI_CONN_SETUP_ALLOW_BDADDR 0x02
960964

961965
/* CONN_SETUP Conditions */
962-
#define HCI_CONN_SETUP_AUTO_OFF 0x01
963-
#define HCI_CONN_SETUP_AUTO_ON 0x02
966+
#define HCI_CONN_SETUP_AUTO_OFF 0x01
967+
#define HCI_CONN_SETUP_AUTO_ON 0x02
968+
#define HCI_CONN_SETUP_AUTO_ON_WITH_RS 0x03
964969

965970
#define HCI_OP_READ_STORED_LINK_KEY 0x0c0d
966971
struct hci_cp_read_stored_link_key {

‎include/net/bluetooth/hci_core.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ struct discovery_state {
9191
#define SUSPEND_NOTIFIER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
9292

9393
enum suspend_tasks {
94+
SUSPEND_SCAN_DISABLE,
95+
SUSPEND_SCAN_ENABLE,
96+
SUSPEND_DISCONNECTING,
97+
9498
SUSPEND_POWERING_DOWN,
9599

96100
SUSPEND_PREPARE_NOTIFIER,
@@ -99,7 +103,8 @@ enum suspend_tasks {
99103

100104
enum suspended_state {
101105
BT_RUNNING = 0,
102-
BT_SUSPENDED,
106+
BT_SUSPEND_DISCONNECT,
107+
BT_SUSPEND_COMPLETE,
103108
};
104109

105110
struct hci_conn_hash {
@@ -409,6 +414,8 @@ struct hci_dev {
409414
struct work_struct suspend_prepare;
410415
enum suspended_state suspend_state_next;
411416
enum suspended_state suspend_state;
417+
bool scanning_paused;
418+
bool suspended;
412419

413420
wait_queue_head_t suspend_wait_q;
414421
DECLARE_BITMAP(suspend_tasks, __SUSPEND_NUM_TASKS);
@@ -418,6 +425,7 @@ struct hci_dev {
418425
struct list_head mgmt_pending;
419426
struct list_head blacklist;
420427
struct list_head whitelist;
428+
struct list_head wakeable;
421429
struct list_head uuids;
422430
struct list_head link_keys;
423431
struct list_head long_term_keys;

‎net/bluetooth/hci_core.c‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3325,16 +3325,31 @@ static int hci_suspend_notifier(struct notifier_block *nb, unsigned long action,
33253325
goto done;
33263326

33273327
if (action == PM_SUSPEND_PREPARE) {
3328-
hdev->suspend_state_next = BT_SUSPENDED;
3328+
/* Suspend consists of two actions:
3329+
* - First, disconnect everything and make the controller not
3330+
* connectable (disabling scanning)
3331+
* - Second, program event filter/whitelist and enable scan
3332+
*/
3333+
hdev->suspend_state_next = BT_SUSPEND_DISCONNECT;
33293334
set_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks);
33303335
queue_work(hdev->req_workqueue, &hdev->suspend_prepare);
3331-
33323336
ret = hci_suspend_wait_event(hdev);
3337+
3338+
/* If the disconnect portion failed, don't attempt to complete
3339+
* by configuring the whitelist. The suspend notifier will
3340+
* follow a cancelled suspend with a PM_POST_SUSPEND
3341+
* notification.
3342+
*/
3343+
if (!ret) {
3344+
hdev->suspend_state_next = BT_SUSPEND_COMPLETE;
3345+
set_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks);
3346+
queue_work(hdev->req_workqueue, &hdev->suspend_prepare);
3347+
ret = hci_suspend_wait_event(hdev);
3348+
}
33333349
} else if (action == PM_POST_SUSPEND) {
33343350
hdev->suspend_state_next = BT_RUNNING;
33353351
set_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks);
33363352
queue_work(hdev->req_workqueue, &hdev->suspend_prepare);
3337-
33383353
ret = hci_suspend_wait_event(hdev);
33393354
}
33403355

@@ -3399,6 +3414,7 @@ struct hci_dev *hci_alloc_dev(void)
33993414
INIT_LIST_HEAD(&hdev->mgmt_pending);
34003415
INIT_LIST_HEAD(&hdev->blacklist);
34013416
INIT_LIST_HEAD(&hdev->whitelist);
3417+
INIT_LIST_HEAD(&hdev->wakeable);
34023418
INIT_LIST_HEAD(&hdev->uuids);
34033419
INIT_LIST_HEAD(&hdev->link_keys);
34043420
INIT_LIST_HEAD(&hdev->long_term_keys);

‎net/bluetooth/hci_event.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,7 @@ static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
25052505
static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
25062506
{
25072507
struct hci_ev_conn_complete *ev = (void *) skb->data;
2508+
struct inquiry_entry *ie;
25082509
struct hci_conn *conn;
25092510

25102511
BT_DBG("%s", hdev->name);
@@ -2513,6 +2514,21 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
25132514

25142515
conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
25152516
if (!conn) {
2517+
/* Connection may not exist if auto-connected. Check the inquiry
2518+
* cache to see if we've already discovered this bdaddr before.
2519+
* If found and link is an ACL type, create a connection class
2520+
* automatically.
2521+
*/
2522+
ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2523+
if (ie && ev->link_type == ACL_LINK) {
2524+
conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2525+
HCI_ROLE_SLAVE);
2526+
if (!conn) {
2527+
bt_dev_err(hdev, "no memory for new conn");
2528+
goto unlock;
2529+
}
2530+
}
2531+
25162532
if (ev->link_type != SCO_LINK)
25172533
goto unlock;
25182534

@@ -2774,6 +2790,14 @@ static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
27742790
hci_disconn_cfm(conn, ev->reason);
27752791
hci_conn_del(conn);
27762792

2793+
/* The suspend notifier is waiting for all devices to disconnect so
2794+
* clear the bit from pending tasks and inform the wait queue.
2795+
*/
2796+
if (list_empty(&hdev->conn_hash.list) &&
2797+
test_and_clear_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks)) {
2798+
wake_up(&hdev->suspend_wait_q);
2799+
}
2800+
27772801
/* Re-enable advertising if necessary, since it might
27782802
* have been disabled by the connection. From the
27792803
* HCI_LE_Set_Advertise_Enable command description in

‎net/bluetooth/hci_request.c‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,15 +918,118 @@ static u8 get_adv_instance_scan_rsp_len(struct hci_dev *hdev, u8 instance)
918918
return adv_instance->scan_rsp_len;
919919
}
920920

921+
static void hci_req_clear_event_filter(struct hci_request *req)
922+
{
923+
struct hci_cp_set_event_filter f;
924+
925+
memset(&f, 0, sizeof(f));
926+
f.flt_type = HCI_FLT_CLEAR_ALL;
927+
hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &f);
928+
929+
/* Update page scan state (since we may have modified it when setting
930+
* the event filter).
931+
*/
932+
__hci_req_update_scan(req);
933+
}
934+
935+
static void hci_req_set_event_filter(struct hci_request *req)
936+
{
937+
struct bdaddr_list *b;
938+
struct hci_cp_set_event_filter f;
939+
struct hci_dev *hdev = req->hdev;
940+
u8 scan;
941+
942+
/* Always clear event filter when starting */
943+
hci_req_clear_event_filter(req);
944+
945+
list_for_each_entry(b, &hdev->wakeable, list) {
946+
memset(&f, 0, sizeof(f));
947+
bacpy(&f.addr_conn_flt.bdaddr, &b->bdaddr);
948+
f.flt_type = HCI_FLT_CONN_SETUP;
949+
f.cond_type = HCI_CONN_SETUP_ALLOW_BDADDR;
950+
f.addr_conn_flt.auto_accept = HCI_CONN_SETUP_AUTO_ON;
951+
952+
bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
953+
hci_req_add(req, HCI_OP_SET_EVENT_FLT, sizeof(f), &f);
954+
}
955+
956+
scan = !list_empty(&hdev->wakeable) ? SCAN_PAGE : SCAN_DISABLED;
957+
hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
958+
}
959+
960+
static void suspend_req_complete(struct hci_dev *hdev, u8 status, u16 opcode)
961+
{
962+
bt_dev_dbg(hdev, "Request complete opcode=0x%x, status=0x%x", opcode,
963+
status);
964+
if (test_and_clear_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks) ||
965+
test_and_clear_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks)) {
966+
wake_up(&hdev->suspend_wait_q);
967+
}
968+
}
969+
921970
/* Call with hci_dev_lock */
922971
void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next)
923972
{
973+
struct hci_conn *conn;
974+
struct hci_request req;
975+
u8 page_scan;
976+
int disconnect_counter;
977+
924978
if (next == hdev->suspend_state) {
925979
bt_dev_dbg(hdev, "Same state before and after: %d", next);
926980
goto done;
927981
}
928982

929983
hdev->suspend_state = next;
984+
hci_req_init(&req, hdev);
985+
986+
if (next == BT_SUSPEND_DISCONNECT) {
987+
/* Mark device as suspended */
988+
hdev->suspended = true;
989+
990+
/* Disable page scan */
991+
page_scan = SCAN_DISABLED;
992+
hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &page_scan);
993+
994+
/* Mark task needing completion */
995+
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
996+
997+
/* Prevent disconnects from causing scanning to be re-enabled */
998+
hdev->scanning_paused = true;
999+
1000+
/* Run commands before disconnecting */
1001+
hci_req_run(&req, suspend_req_complete);
1002+
1003+
disconnect_counter = 0;
1004+
/* Soft disconnect everything (power off) */
1005+
list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1006+
hci_disconnect(conn, HCI_ERROR_REMOTE_POWER_OFF);
1007+
disconnect_counter++;
1008+
}
1009+
1010+
if (disconnect_counter > 0) {
1011+
bt_dev_dbg(hdev,
1012+
"Had %d disconnects. Will wait on them",
1013+
disconnect_counter);
1014+
set_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks);
1015+
}
1016+
} else if (next == BT_SUSPEND_COMPLETE) {
1017+
/* Unpause to take care of updating scanning params */
1018+
hdev->scanning_paused = false;
1019+
/* Enable event filter for paired devices */
1020+
hci_req_set_event_filter(&req);
1021+
/* Pause scan changes again. */
1022+
hdev->scanning_paused = true;
1023+
hci_req_run(&req, suspend_req_complete);
1024+
} else {
1025+
hdev->suspended = false;
1026+
hdev->scanning_paused = false;
1027+
1028+
hci_req_clear_event_filter(&req);
1029+
hci_req_run(&req, suspend_req_complete);
1030+
}
1031+
1032+
hdev->suspend_state = next;
9301033

9311034
done:
9321035
clear_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks);
@@ -2030,6 +2133,9 @@ void __hci_req_update_scan(struct hci_request *req)
20302133
if (mgmt_powering_down(hdev))
20312134
return;
20322135

2136+
if (hdev->scanning_paused)
2137+
return;
2138+
20332139
if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
20342140
disconnected_whitelist_entries(hdev))
20352141
scan = SCAN_PAGE;

0 commit comments

Comments
 (0)