Skip to content

Commit 4867bd0

Browse files
apanditholtmann
authored andcommitted
Bluetooth: Pause discovery and advertising during suspend
To prevent spurious wake ups, we disable any discovery or advertising when we enter suspend and restore it when we exit suspend. While paused, we disable any management requests to modify discovery or advertising. Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent dd522a7 commit 4867bd0

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

‎include/net/bluetooth/hci_core.h‎

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

9393
enum suspend_tasks {
94+
SUSPEND_PAUSE_DISCOVERY,
95+
SUSPEND_UNPAUSE_DISCOVERY,
96+
97+
SUSPEND_PAUSE_ADVERTISING,
98+
SUSPEND_UNPAUSE_ADVERTISING,
99+
94100
SUSPEND_SCAN_DISABLE,
95101
SUSPEND_SCAN_ENABLE,
96102
SUSPEND_DISCONNECTING,
@@ -410,6 +416,11 @@ struct hci_dev {
410416

411417
struct discovery_state discovery;
412418

419+
int discovery_old_state;
420+
bool discovery_paused;
421+
int advertising_old_state;
422+
bool advertising_paused;
423+
413424
struct notifier_block suspend_notifier;
414425
struct work_struct suspend_prepare;
415426
enum suspended_state suspend_state_next;

‎net/bluetooth/hci_request.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ static void suspend_req_complete(struct hci_dev *hdev, u8 status, u16 opcode)
10211021
/* Call with hci_dev_lock */
10221022
void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next)
10231023
{
1024+
int old_state;
10241025
struct hci_conn *conn;
10251026
struct hci_request req;
10261027
u8 page_scan;
@@ -1038,6 +1039,28 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next)
10381039
/* Mark device as suspended */
10391040
hdev->suspended = true;
10401041

1042+
/* Pause discovery if not already stopped */
1043+
old_state = hdev->discovery.state;
1044+
if (old_state != DISCOVERY_STOPPED) {
1045+
set_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks);
1046+
hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1047+
queue_work(hdev->req_workqueue, &hdev->discov_update);
1048+
}
1049+
1050+
hdev->discovery_paused = true;
1051+
hdev->discovery_old_state = old_state;
1052+
1053+
/* Stop advertising */
1054+
old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
1055+
if (old_state) {
1056+
set_bit(SUSPEND_PAUSE_ADVERTISING, hdev->suspend_tasks);
1057+
cancel_delayed_work(&hdev->discov_off);
1058+
queue_delayed_work(hdev->req_workqueue,
1059+
&hdev->discov_off, 0);
1060+
}
1061+
1062+
hdev->advertising_paused = true;
1063+
hdev->advertising_old_state = old_state;
10411064
/* Disable page scan */
10421065
page_scan = SCAN_DISABLED;
10431066
hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &page_scan);
@@ -1084,6 +1107,27 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next)
10841107
hci_req_clear_event_filter(&req);
10851108
/* Reset passive/background scanning to normal */
10861109
hci_req_config_le_suspend_scan(&req);
1110+
1111+
/* Unpause advertising */
1112+
hdev->advertising_paused = false;
1113+
if (hdev->advertising_old_state) {
1114+
set_bit(SUSPEND_UNPAUSE_ADVERTISING,
1115+
hdev->suspend_tasks);
1116+
hci_dev_set_flag(hdev, HCI_ADVERTISING);
1117+
queue_work(hdev->req_workqueue,
1118+
&hdev->discoverable_update);
1119+
hdev->advertising_old_state = 0;
1120+
}
1121+
1122+
/* Unpause discovery */
1123+
hdev->discovery_paused = false;
1124+
if (hdev->discovery_old_state != DISCOVERY_STOPPED &&
1125+
hdev->discovery_old_state != DISCOVERY_STOPPING) {
1126+
set_bit(SUSPEND_UNPAUSE_DISCOVERY, hdev->suspend_tasks);
1127+
hci_discovery_set_state(hdev, DISCOVERY_STARTING);
1128+
queue_work(hdev->req_workqueue, &hdev->discov_update);
1129+
}
1130+
10871131
hci_req_run(&req, suspend_req_complete);
10881132
}
10891133

‎net/bluetooth/mgmt.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,12 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
13901390
goto failed;
13911391
}
13921392

1393+
if (hdev->advertising_paused) {
1394+
err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1395+
MGMT_STATUS_BUSY);
1396+
goto failed;
1397+
}
1398+
13931399
if (!hdev_is_powered(hdev)) {
13941400
bool changed = false;
13951401

@@ -3929,6 +3935,13 @@ void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status)
39293935
}
39303936

39313937
hci_dev_unlock(hdev);
3938+
3939+
/* Handle suspend notifier */
3940+
if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY,
3941+
hdev->suspend_tasks)) {
3942+
bt_dev_dbg(hdev, "Unpaused discovery");
3943+
wake_up(&hdev->suspend_wait_q);
3944+
}
39323945
}
39333946

39343947
static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type,
@@ -3990,6 +4003,13 @@ static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev,
39904003
goto failed;
39914004
}
39924005

4006+
/* Can't start discovery when it is paused */
4007+
if (hdev->discovery_paused) {
4008+
err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
4009+
&cp->type, sizeof(cp->type));
4010+
goto failed;
4011+
}
4012+
39934013
/* Clear the discovery filter first to free any previously
39944014
* allocated memory for the UUID list.
39954015
*/
@@ -4157,6 +4177,12 @@ void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status)
41574177
}
41584178

41594179
hci_dev_unlock(hdev);
4180+
4181+
/* Handle suspend notifier */
4182+
if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) {
4183+
bt_dev_dbg(hdev, "Paused discovery");
4184+
wake_up(&hdev->suspend_wait_q);
4185+
}
41604186
}
41614187

41624188
static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
@@ -4388,6 +4414,17 @@ static void set_advertising_complete(struct hci_dev *hdev, u8 status,
43884414
if (match.sk)
43894415
sock_put(match.sk);
43904416

4417+
/* Handle suspend notifier */
4418+
if (test_and_clear_bit(SUSPEND_PAUSE_ADVERTISING,
4419+
hdev->suspend_tasks)) {
4420+
bt_dev_dbg(hdev, "Paused advertising");
4421+
wake_up(&hdev->suspend_wait_q);
4422+
} else if (test_and_clear_bit(SUSPEND_UNPAUSE_ADVERTISING,
4423+
hdev->suspend_tasks)) {
4424+
bt_dev_dbg(hdev, "Unpaused advertising");
4425+
wake_up(&hdev->suspend_wait_q);
4426+
}
4427+
43914428
/* If "Set Advertising" was just disabled and instance advertising was
43924429
* set up earlier, then re-enable multi-instance advertising.
43934430
*/
@@ -4439,6 +4476,10 @@ static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
44394476
return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
44404477
MGMT_STATUS_INVALID_PARAMS);
44414478

4479+
if (hdev->advertising_paused)
4480+
return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4481+
MGMT_STATUS_BUSY);
4482+
44424483
hci_dev_lock(hdev);
44434484

44444485
val = !!cp->val;

0 commit comments

Comments
 (0)