History

Bluetooth: hci_sync: Rework hci_suspend_notifier

This makes hci_suspend_notifier use the hci_*_sync which can be executed synchronously which is allowed in the suspend_notifier and simplifies a lot of the handling since the status of each command can be checked inline so no other work need to be scheduled thus can be performed without using of a state machine.

Luiz Augusto von Dentz committed 5 years ago

Commit 182ee451 parent d0b137010 files · +650 −638Browse files

Unified · Split

10 files
  1. include/net/bluetooth/hci_core.h +0 −4
  2. include/net/bluetooth/hci_sync.h +3 −0
  3. net/bluetooth/hci_conn.c +0 −10
  4. net/bluetooth/hci_core.c +12 −94
  5. net/bluetooth/hci_event.c +53 −18
  6. net/bluetooth/hci_request.c +0 −237
  7. net/bluetooth/hci_sync.c +278 −16
  8. net/bluetooth/mgmt.c +1 −38
  9. net/bluetooth/msft.c +292 −217
  10. net/bluetooth/msft.h +11 −4
include/net/bluetooth/hci_core.h+0−4
@@ -523,7 +523,6 @@ struct hci_dev { · 522 unmodified lines
523523 bool advertising_paused;
524524
525525 struct notifier_block suspend_notifier;
526− struct work_struct suspend_prepare;
527526 enum suspended_state suspend_state_next;
528527 enum suspended_state suspend_state;
529528 bool scanning_paused;
@@ -532,9 +531,6 @@ struct hci_dev { · 2 unmodified lines
532531 bdaddr_t wake_addr;
533532 u8 wake_addr_type;
534533
535− wait_queue_head_t suspend_wait_q;
536− DECLARE_BITMAP(suspend_tasks, __SUSPEND_NUM_TASKS);
537−
538534 struct hci_conn_hash conn_hash;
539535
540536 struct list_head mgmt_pending;
include/net/bluetooth/hci_sync.h+3−0
@@ -92,3 +92,6 @@ int hci_set_powered_sync(struct hci_dev *hdev, u8 val); · 91 unmodified lines
9292
9393 int hci_start_discovery_sync(struct hci_dev *hdev);
9494 int hci_stop_discovery_sync(struct hci_dev *hdev);
95+
96+int hci_suspend_sync(struct hci_dev *hdev);
97+int hci_resume_sync(struct hci_dev *hdev);
net/bluetooth/hci_conn.c+0−10
@@ -900,16 +900,6 @@ void hci_le_conn_failed(struct hci_conn *conn, u8 status) · 899 unmodified lines
900900
901901 hci_conn_del(conn);
902902
903− /* The suspend notifier is waiting for all devices to disconnect and an
904− * LE connect cancel will result in an hci_le_conn_failed. Once the last
905− * connection is deleted, we should also wake the suspend queue to
906− * complete suspend operations.
907− */
908− if (list_empty(&hdev->conn_hash.list) &&
909− test_and_clear_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks)) {
910− wake_up(&hdev->suspend_wait_q);
911− }
912−
913903 /* Since we may have temporarily stopped the background scanning in
914904 * favor of connection establishment, we should restart it.
915905 */
net/bluetooth/hci_core.c+12−94
@@ -2374,61 +2374,6 @@ void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr, · 2373 unmodified lines
23742374 }
23752375 }
23762376
2377−static void hci_suspend_clear_tasks(struct hci_dev *hdev)
2378−{
2379− int i;
2380−
2381− for (i = 0; i < __SUSPEND_NUM_TASKS; i++)
2382− clear_bit(i, hdev->suspend_tasks);
2383−
2384− wake_up(&hdev->suspend_wait_q);
2385−}
2386−
2387−static int hci_suspend_wait_event(struct hci_dev *hdev)
2388−{
2389−#define WAKE_COND \
2390− (find_first_bit(hdev->suspend_tasks, __SUSPEND_NUM_TASKS) == \
2391− __SUSPEND_NUM_TASKS)
2392−
2393− int i;
2394− int ret = wait_event_timeout(hdev->suspend_wait_q,
2395− WAKE_COND, SUSPEND_NOTIFIER_TIMEOUT);
2396−
2397− if (ret == 0) {
2398− bt_dev_err(hdev, "Timed out waiting for suspend events");
2399− for (i = 0; i < __SUSPEND_NUM_TASKS; ++i) {
2400− if (test_bit(i, hdev->suspend_tasks))
2401− bt_dev_err(hdev, "Suspend timeout bit: %d", i);
2402− clear_bit(i, hdev->suspend_tasks);
2403− }
2404−
2405− ret = -ETIMEDOUT;
2406− } else {
2407− ret = 0;
2408− }
2409−
2410− return ret;
2411−}
2412−
2413−static void hci_prepare_suspend(struct work_struct *work)
2414−{
2415− struct hci_dev *hdev =
2416− container_of(work, struct hci_dev, suspend_prepare);
2417−
2418− hci_dev_lock(hdev);
2419− hci_req_prepare_suspend(hdev, hdev->suspend_state_next);
2420− hci_dev_unlock(hdev);
2421−}
2422−
2423−static int hci_change_suspend_state(struct hci_dev *hdev,
2424− enum suspended_state next)
2425−{
2426− hdev->suspend_state_next = next;
2427− set_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks);
2428− queue_work(hdev->req_workqueue, &hdev->suspend_prepare);
2429− return hci_suspend_wait_event(hdev);
2430−}
2431−
24322377 static void hci_clear_wake_reason(struct hci_dev *hdev)
24332378 {
24342379 hci_dev_lock(hdev);
@@ -2565,7 +2510,6 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) · 130 unmodified lines
25652510 INIT_WORK(&hdev->tx_work, hci_tx_work);
25662511 INIT_WORK(&hdev->power_on, hci_power_on);
25672512 INIT_WORK(&hdev->error_reset, hci_error_reset);
2568− INIT_WORK(&hdev->suspend_prepare, hci_prepare_suspend);
25692513
25702514 hci_cmd_sync_init(hdev);
25712515
@@ -2576,7 +2520,6 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) · 4 unmodified lines
25762520 skb_queue_head_init(&hdev->raw_q);
25772521
25782522 init_waitqueue_head(&hdev->req_wait_q);
2579− init_waitqueue_head(&hdev->suspend_wait_q);
25802523
25812524 INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout);
25822525 INIT_DELAYED_WORK(&hdev->ncmd_timer, hci_ncmd_timeout);
@@ -2729,11 +2672,8 @@ void hci_unregister_dev(struct hci_dev *hdev) · 146 unmodified lines
27292672
27302673 hci_cmd_sync_clear(hdev);
27312674
2732− if (!test_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks)) {
2733− hci_suspend_clear_tasks(hdev);
2675+ if (!test_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks))
27342676 unregister_pm_notifier(&hdev->suspend_notifier);
2735− cancel_work_sync(&hdev->suspend_prepare);
2736− }
27372677
27382678 msft_unregister(hdev);
27392679
@@ -2800,7 +2740,6 @@ EXPORT_SYMBOL(hci_release_dev); · 60 unmodified lines
28002740 int hci_suspend_dev(struct hci_dev *hdev)
28012741 {
28022742 int ret;
2803− u8 state = BT_RUNNING;
28042743
28052744 bt_dev_dbg(hdev, "");
28062745
@@ -2809,40 +2748,17 @@ int hci_suspend_dev(struct hci_dev *hdev) · 2 unmodified lines
28092748 hci_dev_test_flag(hdev, HCI_UNREGISTER))
28102749 return 0;
28112750
2812− /* If powering down, wait for completion. */
2813− if (mgmt_powering_down(hdev)) {
2814− set_bit(SUSPEND_POWERING_DOWN, hdev->suspend_tasks);
2815− ret = hci_suspend_wait_event(hdev);
2816− if (ret)
2817− goto done;
2818− }
2819−
2820− /* Suspend consists of two actions:
2821− * - First, disconnect everything and make the controller not
2822− * connectable (disabling scanning)
2823− * - Second, program event filter/accept list and enable scan
2824− */
2825− ret = hci_change_suspend_state(hdev, BT_SUSPEND_DISCONNECT);
2826− if (ret)
2827− goto clear;
2828−
2829− state = BT_SUSPEND_DISCONNECT;
2751+ /* If powering down don't attempt to suspend */
2752+ if (mgmt_powering_down(hdev))
2753+ return 0;
28302754
2831− /* Only configure accept list if device may wakeup. */
2832− if (hdev->wakeup && hdev->wakeup(hdev)) {
2833− ret = hci_change_suspend_state(hdev, BT_SUSPEND_CONFIGURE_WAKE);
2834− if (!ret)
2835− state = BT_SUSPEND_CONFIGURE_WAKE;
2836− }
2755+ hci_req_sync_lock(hdev);
2756+ ret = hci_suspend_sync(hdev);
2757+ hci_req_sync_unlock(hdev);
28372758
2838−clear:
28392759 hci_clear_wake_reason(hdev);
2840− mgmt_suspending(hdev, state);
2760+ mgmt_suspending(hdev, hdev->suspend_state);
28412761
2842−done:
2843− /* We always allow suspend even if suspend preparation failed and
2844− * attempt to recover in resume.
2845− */
28462762 hci_sock_dev_event(hdev, HCI_DEV_SUSPEND);
28472763 return ret;
28482764 }
@@ -2864,10 +2780,12 @@ int hci_resume_dev(struct hci_dev *hdev) · 15 unmodified lines
28642780 if (mgmt_powering_down(hdev))
28652781 return 0;
28662782
2867− ret = hci_change_suspend_state(hdev, BT_RUNNING);
2783+ hci_req_sync_lock(hdev);
2784+ ret = hci_resume_sync(hdev);
2785+ hci_req_sync_unlock(hdev);
28682786
28692787 mgmt_resuming(hdev, hdev->wake_reason, &hdev->wake_addr,
2870− hdev->wake_addr_type);
2788+ hdev->wake_addr_type);
28712789
28722790 hci_sock_dev_event(hdev, HCI_DEV_RESUME);
28732791 return ret;
net/bluetooth/hci_event.c+53−18
@@ -2414,9 +2414,14 @@ static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status) · 2413 unmodified lines
24142414 static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
24152415 {
24162416 struct hci_cp_disconnect *cp;
2417+ struct hci_conn_params *params;
24172418 struct hci_conn *conn;
2419+ bool mgmt_conn;
24182420
2419− if (!status)
2421+ /* Wait for HCI_EV_DISCONN_COMPLETE if status 0x00 and not suspended
2422+ * otherwise cleanup the connection immediately.
2423+ */
2424+ if (!status && !hdev->suspended)
24202425 return;
24212426
24222427 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
@@ -2426,7 +2431,10 @@ static void hci_cs_disconnect(struct hci_dev *hdev, u8 status) · 3 unmodified lines
24262431 hci_dev_lock(hdev);
24272432
24282433 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2429− if (conn) {
2434+ if (!conn)
2435+ goto unlock;
2436+
2437+ if (status) {
24302438 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
24312439 conn->dst_type, status);
24322440
@@ -2435,14 +2443,48 @@ static void hci_cs_disconnect(struct hci_dev *hdev, u8 status) · 2 unmodified lines
24352443 hci_enable_advertising(hdev);
24362444 }
24372445
2438− /* If the disconnection failed for any reason, the upper layer
2439− * does not retry to disconnect in current implementation.
2440− * Hence, we need to do some basic cleanup here and re-enable
2441− * advertising if necessary.
2442− */
2443− hci_conn_del(conn);
2446+ goto done;
24442447 }
24452448
2449+ mgmt_conn = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
2450+
2451+ if (conn->type == ACL_LINK) {
2452+ if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2453+ hci_remove_link_key(hdev, &conn->dst);
2454+ }
2455+
2456+ params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2457+ if (params) {
2458+ switch (params->auto_connect) {
2459+ case HCI_AUTO_CONN_LINK_LOSS:
2460+ if (cp->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2461+ break;
2462+ fallthrough;
2463+
2464+ case HCI_AUTO_CONN_DIRECT:
2465+ case HCI_AUTO_CONN_ALWAYS:
2466+ list_del_init(&params->action);
2467+ list_add(&params->action, &hdev->pend_le_conns);
2468+ break;
2469+
2470+ default:
2471+ break;
2472+ }
2473+ }
2474+
2475+ mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2476+ cp->reason, mgmt_conn);
2477+
2478+ hci_disconn_cfm(conn, cp->reason);
2479+
2480+done:
2481+ /* If the disconnection failed for any reason, the upper layer
2482+ * does not retry to disconnect in current implementation.
2483+ * Hence, we need to do some basic cleanup here and re-enable
2484+ * advertising if necessary.
2485+ */
2486+ hci_conn_del(conn);
2487+unlock:
24462488 hci_dev_unlock(hdev);
24472489 }
24482490
@@ -3047,14 +3089,6 @@ static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) · 598 unmodified lines
30473089
30483090 hci_conn_del(conn);
30493091
3050− /* The suspend notifier is waiting for all devices to disconnect so
3051− * clear the bit from pending tasks and inform the wait queue.
3052− */
3053− if (list_empty(&hdev->conn_hash.list) &&
3054− test_and_clear_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks)) {
3055− wake_up(&hdev->suspend_wait_q);
3056− }
3057−
30583092 unlock:
30593093 hci_dev_unlock(hdev);
30603094 }
@@ -5575,8 +5609,9 @@ static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev, · 2514 unmodified lines
55755609 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
55765610 return NULL;
55775611
5578− /* Ignore if the device is blocked */
5579− if (hci_bdaddr_list_lookup(&hdev->reject_list, addr, addr_type))
5612+ /* Ignore if the device is blocked or hdev is suspended */
5613+ if (hci_bdaddr_list_lookup(&hdev->reject_list, addr, addr_type) ||
5614+ hdev->suspended)
55805615 return NULL;
55815616
55825617 /* Most controller will fail if we try to create new connections
net/bluetooth/hci_request.c+0−237
@@ -492,9 +492,6 @@ void hci_req_add_le_scan_disable(struct hci_request *req, bool rpa_le_conn) · 491 unmodified lines
492492 return;
493493 }
494494
495− if (hdev->suspended)
496− set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
497−
498495 if (use_ext_scan(hdev)) {
499496 struct hci_cp_le_set_ext_scan_enable cp;
500497
@@ -868,8 +865,6 @@ void hci_req_add_le_passive_scan(struct hci_request *req) · 367 unmodified lines
868865 if (hdev->suspended) {
869866 window = hdev->le_scan_window_suspend;
870867 interval = hdev->le_scan_int_suspend;
871−
872− set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks);
873868 } else if (hci_is_le_conn_scanning(hdev)) {
874869 window = hdev->le_scan_window_connect;
875870 interval = hdev->le_scan_int_connect;
@@ -902,59 +897,6 @@ void hci_req_add_le_passive_scan(struct hci_request *req) · 26 unmodified lines
902897 addr_resolv);
903898 }
904899
905−static void hci_req_clear_event_filter(struct hci_request *req)
906−{
907− struct hci_cp_set_event_filter f;
908−
909− if (!hci_dev_test_flag(req->hdev, HCI_BREDR_ENABLED))
910− return;
911−
912− if (hci_dev_test_flag(req->hdev, HCI_EVENT_FILTER_CONFIGURED)) {
913− memset(&f, 0, sizeof(f));
914− f.flt_type = HCI_FLT_CLEAR_ALL;
915− hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &f);
916− }
917−}
918−
919−static void hci_req_set_event_filter(struct hci_request *req)
920−{
921− struct bdaddr_list_with_flags *b;
922− struct hci_cp_set_event_filter f;
923− struct hci_dev *hdev = req->hdev;
924− u8 scan = SCAN_DISABLED;
925− bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
926−
927− if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
928− return;
929−
930− /* Always clear event filter when starting */
931− hci_req_clear_event_filter(req);
932−
933− list_for_each_entry(b, &hdev->accept_list, list) {
934− if (!hci_conn_test_flag(HCI_CONN_FLAG_REMOTE_WAKEUP,
935− b->current_flags))
936− continue;
937−
938− memset(&f, 0, sizeof(f));
939− bacpy(&f.addr_conn_flt.bdaddr, &b->bdaddr);
940− f.flt_type = HCI_FLT_CONN_SETUP;
941− f.cond_type = HCI_CONN_SETUP_ALLOW_BDADDR;
942− f.addr_conn_flt.auto_accept = HCI_CONN_SETUP_AUTO_ON;
943−
944− bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
945− hci_req_add(req, HCI_OP_SET_EVENT_FLT, sizeof(f), &f);
946− scan = SCAN_PAGE;
947− }
948−
949− if (scan && !scanning) {
950− set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks);
951− hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
952− } else if (!scan && scanning) {
953− set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
954− hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
955− }
956−}
957−
958900 static void cancel_adv_timeout(struct hci_dev *hdev)
959901 {
960902 if (hdev->adv_instance_timeout) {
@@ -1013,185 +955,6 @@ int hci_req_resume_adv_instances(struct hci_dev *hdev) · 52 unmodified lines
1013955 return hci_req_run(&req, NULL);
1014956 }
1015957
1016−static void suspend_req_complete(struct hci_dev *hdev, u8 status, u16 opcode)
1017−{
1018− bt_dev_dbg(hdev, "Request complete opcode=0x%x, status=0x%x", opcode,
1019− status);
1020− if (test_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks) ||
1021− test_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks)) {
1022− clear_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks);
1023− clear_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
1024− wake_up(&hdev->suspend_wait_q);
1025− }
1026−
1027− if (test_bit(SUSPEND_SET_ADV_FILTER, hdev->suspend_tasks)) {
1028− clear_bit(SUSPEND_SET_ADV_FILTER, hdev->suspend_tasks);
1029− wake_up(&hdev->suspend_wait_q);
1030− }
1031−}
1032−
1033−static void hci_req_prepare_adv_monitor_suspend(struct hci_request *req,
1034− bool suspending)
1035−{
1036− struct hci_dev *hdev = req->hdev;
1037−
1038− switch (hci_get_adv_monitor_offload_ext(hdev)) {
1039− case HCI_ADV_MONITOR_EXT_MSFT:
1040− if (suspending)
1041− msft_suspend(hdev);
1042− else
1043− msft_resume(hdev);
1044− break;
1045− default:
1046− return;
1047− }
1048−
1049− /* No need to block when enabling since it's on resume path */
1050− if (hdev->suspended && suspending)
1051− set_bit(SUSPEND_SET_ADV_FILTER, hdev->suspend_tasks);
1052−}
1053−
1054−/* Call with hci_dev_lock */
1055−void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next)
1056−{
1057− int old_state;
1058− struct hci_conn *conn;
1059− struct hci_request req;
1060− u8 page_scan;
1061− int disconnect_counter;
1062−
1063− if (next == hdev->suspend_state) {
1064− bt_dev_dbg(hdev, "Same state before and after: %d", next);
1065− goto done;
1066− }
1067−
1068− hdev->suspend_state = next;
1069− hci_req_init(&req, hdev);
1070−
1071− if (next == BT_SUSPEND_DISCONNECT) {
1072− /* Mark device as suspended */
1073− hdev->suspended = true;
1074−
1075− /* Pause discovery if not already stopped */
1076− old_state = hdev->discovery.state;
1077− if (old_state != DISCOVERY_STOPPED) {
1078− set_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks);
1079− hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1080− queue_work(hdev->req_workqueue, &hdev->discov_update);
1081− }
1082−
1083− hdev->discovery_paused = true;
1084− hdev->discovery_old_state = old_state;
1085−
1086− /* Stop directed advertising */
1087− old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
1088− if (old_state) {
1089− set_bit(SUSPEND_PAUSE_ADVERTISING, hdev->suspend_tasks);
1090− cancel_delayed_work(&hdev->discov_off);
1091− queue_delayed_work(hdev->req_workqueue,
1092− &hdev->discov_off, 0);
1093− }
1094−
1095− /* Pause other advertisements */
1096− if (hdev->adv_instance_cnt)
1097− __hci_req_pause_adv_instances(&req);
1098−
1099− hdev->advertising_paused = true;
1100− hdev->advertising_old_state = old_state;
1101−
1102− /* Disable page scan if enabled */
1103− if (test_bit(HCI_PSCAN, &hdev->flags)) {
1104− page_scan = SCAN_DISABLED;
1105− hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1,
1106− &page_scan);
1107− set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
1108− }
1109−
1110− /* Disable LE passive scan if enabled */
1111− if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
1112− cancel_interleave_scan(hdev);
1113− hci_req_add_le_scan_disable(&req, false);
1114− }
1115−
1116− /* Disable advertisement filters */
1117− hci_req_prepare_adv_monitor_suspend(&req, true);
1118−
1119− /* Prevent disconnects from causing scanning to be re-enabled */
1120− hdev->scanning_paused = true;
1121−
1122− /* Run commands before disconnecting */
1123− hci_req_run(&req, suspend_req_complete);
1124−
1125− disconnect_counter = 0;
1126− /* Soft disconnect everything (power off) */
1127− list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1128− hci_disconnect(conn, HCI_ERROR_REMOTE_POWER_OFF);
1129− disconnect_counter++;
1130− }
1131−
1132− if (disconnect_counter > 0) {
1133− bt_dev_dbg(hdev,
1134− "Had %d disconnects. Will wait on them",
1135− disconnect_counter);
1136− set_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks);
1137− }
1138− } else if (next == BT_SUSPEND_CONFIGURE_WAKE) {
1139− /* Unpause to take care of updating scanning params */
1140− hdev->scanning_paused = false;
1141− /* Enable event filter for paired devices */
1142− hci_req_set_event_filter(&req);
1143− /* Enable passive scan at lower duty cycle */
1144− __hci_update_background_scan(&req);
1145− /* Pause scan changes again. */
1146− hdev->scanning_paused = true;
1147− hci_req_run(&req, suspend_req_complete);
1148− } else {
1149− hdev->suspended = false;
1150− hdev->scanning_paused = false;
1151−
1152− /* Clear any event filters and restore scan state */
1153− hci_req_clear_event_filter(&req);
1154− __hci_req_update_scan(&req);
1155−
1156− /* Reset passive/background scanning to normal */
1157− __hci_update_background_scan(&req);
1158− /* Enable all of the advertisement filters */
1159− hci_req_prepare_adv_monitor_suspend(&req, false);
1160−
1161− /* Unpause directed advertising */
1162− hdev->advertising_paused = false;
1163− if (hdev->advertising_old_state) {
1164− set_bit(SUSPEND_UNPAUSE_ADVERTISING,
1165− hdev->suspend_tasks);
1166− hci_dev_set_flag(hdev, HCI_ADVERTISING);
1167− queue_work(hdev->req_workqueue,
1168− &hdev->discoverable_update);
1169− hdev->advertising_old_state = 0;
1170− }
1171−
1172− /* Resume other advertisements */
1173− if (hdev->adv_instance_cnt)
1174− __hci_req_resume_adv_instances(&req);
1175−
1176− /* Unpause discovery */
1177− hdev->discovery_paused = false;
1178− if (hdev->discovery_old_state != DISCOVERY_STOPPED &&
1179− hdev->discovery_old_state != DISCOVERY_STOPPING) {
1180− set_bit(SUSPEND_UNPAUSE_DISCOVERY, hdev->suspend_tasks);
1181− hci_discovery_set_state(hdev, DISCOVERY_STARTING);
1182− queue_work(hdev->req_workqueue, &hdev->discov_update);
1183− }
1184−
1185− hci_req_run(&req, suspend_req_complete);
1186− }
1187−
1188− hdev->suspend_state = next;
1189−
1190−done:
1191− clear_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks);
1192− wake_up(&hdev->suspend_wait_q);
1193−}
1194−
1195958 static bool adv_cur_instance_is_scannable(struct hci_dev *hdev)
1196959 {
1197960 return hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance);
net/bluetooth/hci_sync.c+278−16
@@ -1410,9 +1410,6 @@ int hci_scan_disable_sync(struct hci_dev *hdev) · 1409 unmodified lines
14101410 return 0;
14111411 }
14121412
1413− if (hdev->suspended)
1414− set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
1415−
14161413 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
14171414 if (err) {
14181415 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
@@ -1642,10 +1639,11 @@ static int hci_le_add_accept_list_sync(struct hci_dev *hdev, · 223 unmodified lines
16421639 return 0;
16431640 }
16441641
1645−/* This function disables all advertising instances (including 0x00) */
1642+/* This function disables/pause all advertising instances */
16461643 static int hci_pause_advertising_sync(struct hci_dev *hdev)
16471644 {
16481645 int err;
1646+ int old_state;
16491647
16501648 /* If there are no instances or advertising has already been paused
16511649 * there is nothing to do.
@@ -1653,6 +1651,21 @@ static int hci_pause_advertising_sync(struct hci_dev *hdev) · 1 unmodified line
16531651 if (!hdev->adv_instance_cnt || hdev->advertising_paused)
16541652 return 0;
16551653
1654+ bt_dev_dbg(hdev, "Pausing directed advertising");
1655+
1656+ /* Stop directed advertising */
1657+ old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
1658+ if (old_state) {
1659+ /* When discoverable timeout triggers, then just make sure
1660+ * the limited discoverable flag is cleared. Even in the case
1661+ * of a timeout triggered from general discoverable, it is
1662+ * safe to unconditionally clear the flag.
1663+ */
1664+ hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1665+ hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1666+ hdev->discov_timeout = 0;
1667+ }
1668+
16561669 bt_dev_dbg(hdev, "Pausing advertising instances");
16571670
16581671 /* Call to disable any advertisements active on the controller.
@@ -1667,11 +1680,12 @@ static int hci_pause_advertising_sync(struct hci_dev *hdev) · 8 unmodified lines
16671680 cancel_adv_timeout(hdev);
16681681
16691682 hdev->advertising_paused = true;
1683+ hdev->advertising_old_state = old_state;
16701684
16711685 return 0;
16721686 }
16731687
1674−/* This function enables all user advertising instances (excluding 0x00) */
1688+/* This function enables all user advertising instances */
16751689 static int hci_resume_advertising_sync(struct hci_dev *hdev)
16761690 {
16771691 struct adv_info *adv, *tmp;
@@ -1681,6 +1695,14 @@ static int hci_resume_advertising_sync(struct hci_dev *hdev) · 3 unmodified lines
16811695 if (!hdev->advertising_paused)
16821696 return 0;
16831697
1698+ /* Resume directed advertising */
1699+ hdev->advertising_paused = false;
1700+ if (hdev->advertising_old_state) {
1701+ hci_dev_set_flag(hdev, HCI_ADVERTISING);
1702+ queue_work(hdev->req_workqueue, &hdev->discoverable_update);
1703+ hdev->advertising_old_state = 0;
1704+ }
1705+
16841706 bt_dev_dbg(hdev, "Resuming advertising instances");
16851707
16861708 if (ext_adv_capable(hdev)) {
@@ -2002,8 +2024,6 @@ int hci_passive_scan_sync(struct hci_dev *hdev) · 315 unmodified lines
20022024 if (hdev->suspended) {
20032025 window = hdev->le_scan_window_suspend;
20042026 interval = hdev->le_scan_int_suspend;
2005−
2006− set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks);
20072027 } else if (hci_is_le_conn_scanning(hdev)) {
20082028 window = hdev->le_scan_window_connect;
20092029 interval = hdev->le_scan_int_connect;
@@ -2937,6 +2957,13 @@ static int hci_set_event_mask_sync(struct hci_dev *hdev) · 927 unmodified lines
29372957
29382958 if (lmp_bredr_capable(hdev)) {
29392959 events[4] |= 0x01; /* Flow Specification Complete */
2960+
2961+ /* Don't set Disconnect Complete when suspended as that
2962+ * would wakeup the host when disconnecting due to
2963+ * suspend.
2964+ */
2965+ if (hdev->suspended)
2966+ events[0] &= 0xef;
29402967 } else {
29412968 /* Use a different default for LE-only devices */
29422969 memset(events, 0, sizeof(events));
@@ -2949,7 +2976,12 @@ static int hci_set_event_mask_sync(struct hci_dev *hdev) · 6 unmodified lines
29492976 * control related events.
29502977 */
29512978 if (hdev->commands[0] & 0x20) {
2952− events[0] |= 0x10; /* Disconnection Complete */
2979+ /* Don't set Disconnect Complete when suspended as that
2980+ * would wakeup the host when disconnecting due to
2981+ * suspend.
2982+ */
2983+ if (!hdev->suspended)
2984+ events[0] |= 0x10; /* Disconnection Complete */
29532985 events[2] |= 0x04; /* Number of Completed Packets */
29542986 events[3] |= 0x02; /* Data Buffer Overflow */
29552987 }
@@ -4033,9 +4065,6 @@ int hci_dev_close_sync(struct hci_dev *hdev) · 1077 unmodified lines
40334065 clear_bit(HCI_RUNNING, &hdev->flags);
40344066 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
40354067
4036− if (test_and_clear_bit(SUSPEND_POWERING_DOWN, hdev->suspend_tasks))
4037− wake_up(&hdev->suspend_wait_q);
4038−
40394068 /* After this point our queues are empty and no tasks are scheduled. */
40404069 hdev->close(hdev);
40414070
@@ -4299,6 +4328,20 @@ static int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, · 257 unmodified lines
42994328 return 0;
43004329 }
43014330
4331+static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
4332+{
4333+ struct hci_conn *conn, *tmp;
4334+ int err;
4335+
4336+ list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
4337+ err = hci_abort_conn_sync(hdev, conn, reason);
4338+ if (err)
4339+ return err;
4340+ }
4341+
4342+ return err;
4343+}
4344+
43024345 /* This function perform power off HCI command sequence as follows:
43034346 *
43044347 * Clear Advertising
@@ -4308,7 +4351,6 @@ static int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, · 3 unmodified lines
43084351 */
43094352 static int hci_power_off_sync(struct hci_dev *hdev)
43104353 {
4311− struct hci_conn *conn;
43124354 int err;
43134355
43144356 /* If controller is already down there is nothing to do */
@@ -4330,10 +4372,10 @@ static int hci_power_off_sync(struct hci_dev *hdev) · 15 unmodified lines
43304372 if (err)
43314373 return err;
43324374
4333− list_for_each_entry(conn, &hdev->conn_hash.list, list) {
4334− /* 0x15 == Terminated due to Power Off */
4335− hci_abort_conn_sync(hdev, conn, 0x15);
4336− }
4375+ /* Terminated due to Power Off */
4376+ err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
4377+ if (err)
4378+ return err;
43374379
43384380 return hci_dev_close_sync(hdev);
43394381 }
@@ -4535,3 +4577,223 @@ int hci_start_discovery_sync(struct hci_dev *hdev) · 195 unmodified lines
45354577 timeout);
45364578 return 0;
45374579 }
4580+
4581+static void hci_suspend_monitor_sync(struct hci_dev *hdev)
4582+{
4583+ switch (hci_get_adv_monitor_offload_ext(hdev)) {
4584+ case HCI_ADV_MONITOR_EXT_MSFT:
4585+ msft_suspend_sync(hdev);
4586+ break;
4587+ default:
4588+ return;
4589+ }
4590+}
4591+
4592+/* This function disables discovery and mark it as paused */
4593+static int hci_pause_discovery_sync(struct hci_dev *hdev)
4594+{
4595+ int old_state = hdev->discovery.state;
4596+ int err;
4597+
4598+ /* If discovery already stopped/stopping/paused there nothing to do */
4599+ if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
4600+ hdev->discovery_paused)
4601+ return 0;
4602+
4603+ hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
4604+ err = hci_stop_discovery_sync(hdev);
4605+ if (err)
4606+ return err;
4607+
4608+ hdev->discovery_paused = true;
4609+ hdev->discovery_old_state = old_state;
4610+ hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4611+
4612+ return 0;
4613+}
4614+
4615+static int hci_update_event_filter_sync(struct hci_dev *hdev)
4616+{
4617+ struct bdaddr_list_with_flags *b;
4618+ u8 scan = SCAN_DISABLED;
4619+ bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
4620+ int err;
4621+
4622+ if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4623+ return 0;
4624+
4625+ /* Always clear event filter when starting */
4626+ hci_clear_event_filter_sync(hdev);
4627+
4628+ list_for_each_entry(b, &hdev->accept_list, list) {
4629+ if (!hci_conn_test_flag(HCI_CONN_FLAG_REMOTE_WAKEUP,
4630+ b->current_flags))
4631+ continue;
4632+
4633+ bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
4634+
4635+ err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
4636+ HCI_CONN_SETUP_ALLOW_BDADDR,
4637+ &b->bdaddr,
4638+ HCI_CONN_SETUP_AUTO_ON);
4639+ if (err)
4640+ bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
4641+ &b->bdaddr);
4642+ else
4643+ scan = SCAN_PAGE;
4644+ }
4645+
4646+ if (scan && !scanning)
4647+ hci_write_scan_enable_sync(hdev, scan);
4648+ else if (!scan && scanning)
4649+ hci_write_scan_enable_sync(hdev, scan);
4650+
4651+ return 0;
4652+}
4653+
4654+/* This function performs the HCI suspend procedures in the follow order:
4655+ *
4656+ * Pause discovery (active scanning/inquiry)
4657+ * Pause Directed Advertising/Advertising
4658+ * Disconnect all connections
4659+ * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
4660+ * otherwise:
4661+ * Update event mask (only set events that are allowed to wake up the host)
4662+ * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
4663+ * Update passive scanning (lower duty cycle)
4664+ * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
4665+ */
4666+int hci_suspend_sync(struct hci_dev *hdev)
4667+{
4668+ int err;
4669+
4670+ /* If marked as suspended there nothing to do */
4671+ if (hdev->suspended)
4672+ return 0;
4673+
4674+ /* Mark device as suspended */
4675+ hdev->suspended = true;
4676+
4677+ /* Pause discovery if not already stopped */
4678+ hci_pause_discovery_sync(hdev);
4679+
4680+ /* Pause other advertisements */
4681+ hci_pause_advertising_sync(hdev);
4682+
4683+ /* Disable page scan if enabled */
4684+ if (test_bit(HCI_PSCAN, &hdev->flags))
4685+ hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
4686+
4687+ /* Suspend monitor filters */
4688+ hci_suspend_monitor_sync(hdev);
4689+
4690+ /* Prevent disconnects from causing scanning to be re-enabled */
4691+ hdev->scanning_paused = true;
4692+
4693+ /* Soft disconnect everything (power off) */
4694+ err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
4695+ if (err) {
4696+ /* Set state to BT_RUNNING so resume doesn't notify */
4697+ hdev->suspend_state = BT_RUNNING;
4698+ hci_resume_sync(hdev);
4699+ return err;
4700+ }
4701+
4702+ /* Only configure accept list if disconnect succeeded and wake
4703+ * isn't being prevented.
4704+ */
4705+ if (!hdev->wakeup || !hdev->wakeup(hdev)) {
4706+ hdev->suspend_state = BT_SUSPEND_DISCONNECT;
4707+ return 0;
4708+ }
4709+
4710+ /* Unpause to take care of updating scanning params */
4711+ hdev->scanning_paused = false;
4712+
4713+ /* Update event mask so only the allowed event can wakeup the host */
4714+ hci_set_event_mask_sync(hdev);
4715+
4716+ /* Enable event filter for paired devices */
4717+ hci_update_event_filter_sync(hdev);
4718+
4719+ /* Update LE passive scan if enabled */
4720+ hci_update_passive_scan_sync(hdev);
4721+
4722+ /* Pause scan changes again. */
4723+ hdev->scanning_paused = true;
4724+
4725+ hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
4726+
4727+ return 0;
4728+}
4729+
4730+/* This function resumes discovery */
4731+static int hci_resume_discovery_sync(struct hci_dev *hdev)
4732+{
4733+ int err;
4734+
4735+ /* If discovery not paused there nothing to do */
4736+ if (!hdev->discovery_paused)
4737+ return 0;
4738+
4739+ hdev->discovery_paused = false;
4740+
4741+ hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4742+
4743+ err = hci_start_discovery_sync(hdev);
4744+
4745+ hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
4746+ DISCOVERY_FINDING);
4747+
4748+ return err;
4749+}
4750+
4751+static void hci_resume_monitor_sync(struct hci_dev *hdev)
4752+{
4753+ switch (hci_get_adv_monitor_offload_ext(hdev)) {
4754+ case HCI_ADV_MONITOR_EXT_MSFT:
4755+ msft_resume_sync(hdev);
4756+ break;
4757+ default:
4758+ return;
4759+ }
4760+}
4761+
4762+/* This function performs the HCI suspend procedures in the follow order:
4763+ *
4764+ * Restore event mask
4765+ * Clear event filter
4766+ * Update passive scanning (normal duty cycle)
4767+ * Resume Directed Advertising/Advertising
4768+ * Resume discovery (active scanning/inquiry)
4769+ */
4770+int hci_resume_sync(struct hci_dev *hdev)
4771+{
4772+ /* If not marked as suspended there nothing to do */
4773+ if (!hdev->suspended)
4774+ return 0;
4775+
4776+ hdev->suspended = false;
4777+ hdev->scanning_paused = false;
4778+
4779+ /* Restore event mask */
4780+ hci_set_event_mask_sync(hdev);
4781+
4782+ /* Clear any event filters and restore scan state */
4783+ hci_clear_event_filter_sync(hdev);
4784+ hci_update_scan_sync(hdev);
4785+
4786+ /* Reset passive scanning to normal */
4787+ hci_update_passive_scan_sync(hdev);
4788+
4789+ /* Resume monitor filters */
4790+ hci_resume_monitor_sync(hdev);
4791+
4792+ /* Resume other advertisements */
4793+ hci_resume_advertising_sync(hdev);
4794+
4795+ /* Resume discovery */
4796+ hci_resume_discovery_sync(hdev);
4797+
4798+ return 0;
4799+}
net/bluetooth/mgmt.c+1−38
@@ -5171,13 +5171,6 @@ void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) · 5170 unmodified lines
51715171 }
51725172
51735173 hci_dev_unlock(hdev);
5174−
5175− /* Handle suspend notifier */
5176− if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY,
5177− hdev->suspend_tasks)) {
5178− bt_dev_dbg(hdev, "Unpaused discovery");
5179− wake_up(&hdev->suspend_wait_q);
5180− }
51815174 }
51825175
51835176 static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type,
@@ -5217,14 +5210,7 @@ static void start_discovery_complete(struct hci_dev *hdev, void *data, int err) · 33 unmodified lines
52175210 cmd->param, 1);
52185211 mgmt_pending_free(cmd);
52195212
5220− /* Handle suspend notifier */
5221− if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY,
5222− hdev->suspend_tasks)) {
5223− bt_dev_dbg(hdev, "Unpaused discovery");
5224− wake_up(&hdev->suspend_wait_q);
5225− }
5226−
5227− hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
5213+ hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED:
52285214 DISCOVERY_FINDING);
52295215 }
52305216
@@ -5446,12 +5432,6 @@ void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) · 215 unmodified lines
54465432 }
54475433
54485434 hci_dev_unlock(hdev);
5449−
5450− /* Handle suspend notifier */
5451− if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) {
5452− bt_dev_dbg(hdev, "Paused discovery");
5453− wake_up(&hdev->suspend_wait_q);
5454− }
54555435 }
54565436
54575437 static void stop_discovery_complete(struct hci_dev *hdev, void *data, int err)
@@ -5464,12 +5444,6 @@ static void stop_discovery_complete(struct hci_dev *hdev, void *data, int err) · 6 unmodified lines
54645444 cmd->param, 1);
54655445 mgmt_pending_free(cmd);
54665446
5467− /* Handle suspend notifier */
5468− if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) {
5469− bt_dev_dbg(hdev, "Paused discovery");
5470− wake_up(&hdev->suspend_wait_q);
5471− }
5472−
54735447 if (!err)
54745448 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
54755449 }
@@ -5709,17 +5683,6 @@ static void set_advertising_complete(struct hci_dev *hdev, void *data, int err) · 233 unmodified lines
57095683 if (match.sk)
57105684 sock_put(match.sk);
57115685
5712− /* Handle suspend notifier */
5713− if (test_and_clear_bit(SUSPEND_PAUSE_ADVERTISING,
5714− hdev->suspend_tasks)) {
5715− bt_dev_dbg(hdev, "Paused advertising");
5716− wake_up(&hdev->suspend_wait_q);
5717− } else if (test_and_clear_bit(SUSPEND_UNPAUSE_ADVERTISING,
5718− hdev->suspend_tasks)) {
5719− bt_dev_dbg(hdev, "Unpaused advertising");
5720− wake_up(&hdev->suspend_wait_q);
5721− }
5722−
57235686 /* If "Set Advertising" was just disabled and instance advertising was
57245687 * set up earlier, then re-enable multi-instance advertising.
57255688 */
net/bluetooth/msft.c+292−217
@@ -93,7 +93,7 @@ struct msft_data { · 92 unmodified lines
9393 struct list_head handle_map;
9494 __u16 pending_add_handle;
9595 __u16 pending_remove_handle;
96− __u8 reregistering;
96+ __u8 resuming;
9797 __u8 suspending;
9898 __u8 filter_enabled;
9999 };
@@ -156,7 +156,6 @@ failed: · 56 unmodified lines
156156 return false;
157157 }
158158
159−/* This function requires the caller holds hdev->lock */
160159 static void reregister_monitor(struct hci_dev *hdev, int handle)
161160 {
162161 struct adv_monitor *monitor;
@@ -166,8 +165,8 @@ static void reregister_monitor(struct hci_dev *hdev, int handle) · 3 unmodified lines
166165 while (1) {
167166 monitor = idr_get_next(&hdev->adv_monitors_idr, &handle);
168167 if (!monitor) {
169− /* All monitors have been reregistered */
170− msft->reregistering = false;
168+ /* All monitors have been resumed */
169+ msft->resuming = false;
171170 hci_update_passive_scan(hdev);
172171 return;
173172 }
@@ -185,67 +184,317 @@ static void reregister_monitor(struct hci_dev *hdev, int handle) · 11 unmodified lines
185184 }
186185 }
187186
188−/* This function requires the caller holds hdev->lock */
189−static void remove_monitor_on_suspend(struct hci_dev *hdev, int handle)
187+/* is_mgmt = true matches the handle exposed to userspace via mgmt.
188+ * is_mgmt = false matches the handle used by the msft controller.
189+ * This function requires the caller holds hdev->lock
190+ */
191+static struct msft_monitor_advertisement_handle_data *msft_find_handle_data
192+ (struct hci_dev *hdev, u16 handle, bool is_mgmt)
193+{
194+ struct msft_monitor_advertisement_handle_data *entry;
195+ struct msft_data *msft = hdev->msft_data;
196+
197+ list_for_each_entry(entry, &msft->handle_map, list) {
198+ if (is_mgmt && entry->mgmt_handle == handle)
199+ return entry;
200+ if (!is_mgmt && entry->msft_handle == handle)
201+ return entry;
202+ }
203+
204+ return NULL;
205+}
206+
207+static void msft_le_monitor_advertisement_cb(struct hci_dev *hdev,
208+ u8 status, u16 opcode,
209+ struct sk_buff *skb)
210+{
211+ struct msft_rp_le_monitor_advertisement *rp;
212+ struct adv_monitor *monitor;
213+ struct msft_monitor_advertisement_handle_data *handle_data;
214+ struct msft_data *msft = hdev->msft_data;
215+
216+ hci_dev_lock(hdev);
217+
218+ monitor = idr_find(&hdev->adv_monitors_idr, msft->pending_add_handle);
219+ if (!monitor) {
220+ bt_dev_err(hdev, "msft add advmon: monitor %u is not found!",
221+ msft->pending_add_handle);
222+ status = HCI_ERROR_UNSPECIFIED;
223+ goto unlock;
224+ }
225+
226+ if (status)
227+ goto unlock;
228+
229+ rp = (struct msft_rp_le_monitor_advertisement *)skb->data;
230+ if (skb->len < sizeof(*rp)) {
231+ status = HCI_ERROR_UNSPECIFIED;
232+ goto unlock;
233+ }
234+
235+ handle_data = kmalloc(sizeof(*handle_data), GFP_KERNEL);
236+ if (!handle_data) {
237+ status = HCI_ERROR_UNSPECIFIED;
238+ goto unlock;
239+ }
240+
241+ handle_data->mgmt_handle = monitor->handle;
242+ handle_data->msft_handle = rp->handle;
243+ INIT_LIST_HEAD(&handle_data->list);
244+ list_add(&handle_data->list, &msft->handle_map);
245+
246+ monitor->state = ADV_MONITOR_STATE_OFFLOADED;
247+
248+unlock:
249+ if (status && monitor)
250+ hci_free_adv_monitor(hdev, monitor);
251+
252+ hci_dev_unlock(hdev);
253+
254+ if (!msft->resuming)
255+ hci_add_adv_patterns_monitor_complete(hdev, status);
256+}
257+
258+static void msft_le_cancel_monitor_advertisement_cb(struct hci_dev *hdev,
259+ u8 status, u16 opcode,
260+ struct sk_buff *skb)
190261 {
262+ struct msft_cp_le_cancel_monitor_advertisement *cp;
263+ struct msft_rp_le_cancel_monitor_advertisement *rp;
191264 struct adv_monitor *monitor;
265+ struct msft_monitor_advertisement_handle_data *handle_data;
192266 struct msft_data *msft = hdev->msft_data;
193267 int err;
268+ bool pending;
194269
195− while (1) {
196− monitor = idr_get_next(&hdev->adv_monitors_idr, &handle);
197− if (!monitor) {
198− /* All monitors have been removed */
199− msft->suspending = false;
200− hci_update_background_scan(hdev);
270+ if (status)
271+ goto done;
272+
273+ rp = (struct msft_rp_le_cancel_monitor_advertisement *)skb->data;
274+ if (skb->len < sizeof(*rp)) {
275+ status = HCI_ERROR_UNSPECIFIED;
276+ goto done;
277+ }
278+
279+ hci_dev_lock(hdev);
280+
281+ cp = hci_sent_cmd_data(hdev, hdev->msft_opcode);
282+ handle_data = msft_find_handle_data(hdev, cp->handle, false);
283+
284+ if (handle_data) {
285+ monitor = idr_find(&hdev->adv_monitors_idr,
286+ handle_data->mgmt_handle);
287+
288+ if (monitor && monitor->state == ADV_MONITOR_STATE_OFFLOADED)
289+ monitor->state = ADV_MONITOR_STATE_REGISTERED;
290+
291+ /* Do not free the monitor if it is being removed due to
292+ * suspend. It will be re-monitored on resume.
293+ */
294+ if (monitor && !msft->suspending)
295+ hci_free_adv_monitor(hdev, monitor);
296+
297+ list_del(&handle_data->list);
298+ kfree(handle_data);
299+ }
300+
301+ /* If remove all monitors is required, we need to continue the process
302+ * here because the earlier it was paused when waiting for the
303+ * response from controller.
304+ */
305+ if (msft->pending_remove_handle == 0) {
306+ pending = hci_remove_all_adv_monitor(hdev, &err);
307+ if (pending) {
308+ hci_dev_unlock(hdev);
201309 return;
202310 }
203311
204− msft->pending_remove_handle = (u16)handle;
205− err = __msft_remove_monitor(hdev, monitor, handle);
312+ if (err)
313+ status = HCI_ERROR_UNSPECIFIED;
314+ }
206315
207− /* If success, return and wait for monitor removed callback */
208− if (!err)
209− return;
316+ hci_dev_unlock(hdev);
317+
318+done:
319+ if (!msft->suspending)
320+ hci_remove_adv_monitor_complete(hdev, status);
321+}
322+
323+static int msft_remove_monitor_sync(struct hci_dev *hdev,
324+ struct adv_monitor *monitor)
325+{
326+ struct msft_cp_le_cancel_monitor_advertisement cp;
327+ struct msft_monitor_advertisement_handle_data *handle_data;
328+ struct sk_buff *skb;
329+ u8 status;
330+
331+ handle_data = msft_find_handle_data(hdev, monitor->handle, true);
332+
333+ /* If no matched handle, just remove without telling controller */
334+ if (!handle_data)
335+ return -ENOENT;
336+
337+ cp.sub_opcode = MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT;
338+ cp.handle = handle_data->msft_handle;
339+
340+ skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
341+ HCI_CMD_TIMEOUT);
342+ if (IS_ERR(skb))
343+ return PTR_ERR(skb);
344+
345+ status = skb->data[0];
346+ skb_pull(skb, 1);
347+
348+ msft_le_cancel_monitor_advertisement_cb(hdev, status, hdev->msft_opcode,
349+ skb);
350+
351+ return status;
352+}
353+
354+/* This function requires the caller holds hci_req_sync_lock */
355+int msft_suspend_sync(struct hci_dev *hdev)
356+{
357+ struct msft_data *msft = hdev->msft_data;
358+ struct adv_monitor *monitor;
359+ int handle = 0;
360+
361+ if (!msft || !msft_monitor_supported(hdev))
362+ return 0;
363+
364+ msft->suspending = true;
365+
366+ while (1) {
367+ monitor = idr_get_next(&hdev->adv_monitors_idr, &handle);
368+ if (!monitor)
369+ break;
370+
371+ msft_remove_monitor_sync(hdev, monitor);
210372
211− /* Otherwise free the monitor and keep removing */
212− hci_free_adv_monitor(hdev, monitor);
213373 handle++;
214374 }
375+
376+ /* All monitors have been removed */
377+ msft->suspending = false;
378+
379+ return 0;
215380 }
216381
217−/* This function requires the caller holds hdev->lock */
218−void msft_suspend(struct hci_dev *hdev)
382+static bool msft_monitor_rssi_valid(struct adv_monitor *monitor)
219383 {
220− struct msft_data *msft = hdev->msft_data;
384+ struct adv_rssi_thresholds *r = &monitor->rssi;
221385
222− if (!msft)
223− return;
386+ if (r->high_threshold < MSFT_RSSI_THRESHOLD_VALUE_MIN ||
387+ r->high_threshold > MSFT_RSSI_THRESHOLD_VALUE_MAX ||
388+ r->low_threshold < MSFT_RSSI_THRESHOLD_VALUE_MIN ||
389+ r->low_threshold > MSFT_RSSI_THRESHOLD_VALUE_MAX)
390+ return false;
224391
225− if (msft_monitor_supported(hdev)) {
226− msft->suspending = true;
227− /* Quitely remove all monitors on suspend to avoid waking up
228− * the system.
229− */
230− remove_monitor_on_suspend(hdev, 0);
392+ /* High_threshold_timeout is not supported,
393+ * once high_threshold is reached, events are immediately reported.
394+ */
395+ if (r->high_threshold_timeout != 0)
396+ return false;
397+
398+ if (r->low_threshold_timeout > MSFT_RSSI_LOW_TIMEOUT_MAX)
399+ return false;
400+
401+ /* Sampling period from 0x00 to 0xFF are all allowed */
402+ return true;
403+}
404+
405+static bool msft_monitor_pattern_valid(struct adv_monitor *monitor)
406+{
407+ return msft_monitor_rssi_valid(monitor);
408+ /* No additional check needed for pattern-based monitor */
409+}
410+
411+static int msft_add_monitor_sync(struct hci_dev *hdev,
412+ struct adv_monitor *monitor)
413+{
414+ struct msft_cp_le_monitor_advertisement *cp;
415+ struct msft_le_monitor_advertisement_pattern_data *pattern_data;
416+ struct msft_le_monitor_advertisement_pattern *pattern;
417+ struct adv_pattern *entry;
418+ size_t total_size = sizeof(*cp) + sizeof(*pattern_data);
419+ ptrdiff_t offset = 0;
420+ u8 pattern_count = 0;
421+ struct sk_buff *skb;
422+ u8 status;
423+
424+ if (!msft_monitor_pattern_valid(monitor))
425+ return -EINVAL;
426+
427+ list_for_each_entry(entry, &monitor->patterns, list) {
428+ pattern_count++;
429+ total_size += sizeof(*pattern) + entry->length;
231430 }
431+
432+ cp = kmalloc(total_size, GFP_KERNEL);
433+ if (!cp)
434+ return -ENOMEM;
435+
436+ cp->sub_opcode = MSFT_OP_LE_MONITOR_ADVERTISEMENT;
437+ cp->rssi_high = monitor->rssi.high_threshold;
438+ cp->rssi_low = monitor->rssi.low_threshold;
439+ cp->rssi_low_interval = (u8)monitor->rssi.low_threshold_timeout;
440+ cp->rssi_sampling_period = monitor->rssi.sampling_period;
441+
442+ cp->cond_type = MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN;
443+
444+ pattern_data = (void *)cp->data;
445+ pattern_data->count = pattern_count;
446+
447+ list_for_each_entry(entry, &monitor->patterns, list) {
448+ pattern = (void *)(pattern_data->data + offset);
449+ /* the length also includes data_type and offset */
450+ pattern->length = entry->length + 2;
451+ pattern->data_type = entry->ad_type;
452+ pattern->start_byte = entry->offset;
453+ memcpy(pattern->pattern, entry->value, entry->length);
454+ offset += sizeof(*pattern) + entry->length;
455+ }
456+
457+ skb = __hci_cmd_sync(hdev, hdev->msft_opcode, total_size, cp,
458+ HCI_CMD_TIMEOUT);
459+ kfree(cp);
460+
461+ if (IS_ERR(skb))
462+ return PTR_ERR(skb);
463+
464+ status = skb->data[0];
465+ skb_pull(skb, 1);
466+
467+ msft_le_monitor_advertisement_cb(hdev, status, hdev->msft_opcode, skb);
468+
469+ return status;
232470 }
233471
234−/* This function requires the caller holds hdev->lock */
235−void msft_resume(struct hci_dev *hdev)
472+/* This function requires the caller holds hci_req_sync_lock */
473+int msft_resume_sync(struct hci_dev *hdev)
236474 {
237475 struct msft_data *msft = hdev->msft_data;
476+ struct adv_monitor *monitor;
477+ int handle = 0;
238478
239− if (!msft)
240− return;
479+ if (!msft || !msft_monitor_supported(hdev))
480+ return 0;
241481
242− if (msft_monitor_supported(hdev)) {
243− msft->reregistering = true;
244− /* Monitors are removed on suspend, so we need to add all
245− * monitors on resume.
246− */
247− reregister_monitor(hdev, 0);
482+ msft->resuming = true;
483+
484+ while (1) {
485+ monitor = idr_get_next(&hdev->adv_monitors_idr, &handle);
486+ if (!monitor)
487+ break;
488+
489+ msft_add_monitor_sync(hdev, monitor);
490+
491+ handle++;
248492 }
493+
494+ /* All monitors have been resumed */
495+ msft->resuming = false;
496+
497+ return 0;
249498 }
250499
251500 void msft_do_open(struct hci_dev *hdev)
@@ -275,7 +524,7 @@ void msft_do_open(struct hci_dev *hdev) · 23 unmodified lines
275524 }
276525
277526 if (msft_monitor_supported(hdev)) {
278− msft->reregistering = true;
527+ msft->resuming = true;
279528 msft_set_filter_enable(hdev, true);
280529 /* Monitors get removed on power off, so we need to explicitly
281530 * tell the controller to re-monitor.
@@ -381,151 +630,6 @@ __u64 msft_get_features(struct hci_dev *hdev) · 99 unmodified lines
381630 return msft ? msft->features : 0;
382631 }
383632
384−/* is_mgmt = true matches the handle exposed to userspace via mgmt.
385− * is_mgmt = false matches the handle used by the msft controller.
386− * This function requires the caller holds hdev->lock
387− */
388−static struct msft_monitor_advertisement_handle_data *msft_find_handle_data
389− (struct hci_dev *hdev, u16 handle, bool is_mgmt)
390−{
391− struct msft_monitor_advertisement_handle_data *entry;
392− struct msft_data *msft = hdev->msft_data;
393−
394− list_for_each_entry(entry, &msft->handle_map, list) {
395− if (is_mgmt && entry->mgmt_handle == handle)
396− return entry;
397− if (!is_mgmt && entry->msft_handle == handle)
398− return entry;
399− }
400−
401− return NULL;
402−}
403−
404−static void msft_le_monitor_advertisement_cb(struct hci_dev *hdev,
405− u8 status, u16 opcode,
406− struct sk_buff *skb)
407−{
408− struct msft_rp_le_monitor_advertisement *rp;
409− struct adv_monitor *monitor;
410− struct msft_monitor_advertisement_handle_data *handle_data;
411− struct msft_data *msft = hdev->msft_data;
412−
413− hci_dev_lock(hdev);
414−
415− monitor = idr_find(&hdev->adv_monitors_idr, msft->pending_add_handle);
416− if (!monitor) {
417− bt_dev_err(hdev, "msft add advmon: monitor %u is not found!",
418− msft->pending_add_handle);
419− status = HCI_ERROR_UNSPECIFIED;
420− goto unlock;
421− }
422−
423− if (status)
424− goto unlock;
425−
426− rp = (struct msft_rp_le_monitor_advertisement *)skb->data;
427− if (skb->len < sizeof(*rp)) {
428− status = HCI_ERROR_UNSPECIFIED;
429− goto unlock;
430− }
431−
432− handle_data = kmalloc(sizeof(*handle_data), GFP_KERNEL);
433− if (!handle_data) {
434− status = HCI_ERROR_UNSPECIFIED;
435− goto unlock;
436− }
437−
438− handle_data->mgmt_handle = monitor->handle;
439− handle_data->msft_handle = rp->handle;
440− INIT_LIST_HEAD(&handle_data->list);
441− list_add(&handle_data->list, &msft->handle_map);
442−
443− monitor->state = ADV_MONITOR_STATE_OFFLOADED;
444−
445−unlock:
446− if (status && monitor)
447− hci_free_adv_monitor(hdev, monitor);
448−
449− /* If in restart/reregister sequence, keep registering. */
450− if (msft->reregistering)
451− reregister_monitor(hdev, msft->pending_add_handle + 1);
452−
453− hci_dev_unlock(hdev);
454−
455− if (!msft->reregistering)
456− hci_add_adv_patterns_monitor_complete(hdev, status);
457−}
458−
459−static void msft_le_cancel_monitor_advertisement_cb(struct hci_dev *hdev,
460− u8 status, u16 opcode,
461− struct sk_buff *skb)
462−{
463− struct msft_cp_le_cancel_monitor_advertisement *cp;
464− struct msft_rp_le_cancel_monitor_advertisement *rp;
465− struct adv_monitor *monitor;
466− struct msft_monitor_advertisement_handle_data *handle_data;
467− struct msft_data *msft = hdev->msft_data;
468− int err;
469− bool pending;
470−
471− if (status)
472− goto done;
473−
474− rp = (struct msft_rp_le_cancel_monitor_advertisement *)skb->data;
475− if (skb->len < sizeof(*rp)) {
476− status = HCI_ERROR_UNSPECIFIED;
477− goto done;
478− }
479−
480− hci_dev_lock(hdev);
481−
482− cp = hci_sent_cmd_data(hdev, hdev->msft_opcode);
483− handle_data = msft_find_handle_data(hdev, cp->handle, false);
484−
485− if (handle_data) {
486− monitor = idr_find(&hdev->adv_monitors_idr,
487− handle_data->mgmt_handle);
488−
489− if (monitor && monitor->state == ADV_MONITOR_STATE_OFFLOADED)
490− monitor->state = ADV_MONITOR_STATE_REGISTERED;
491−
492− /* Do not free the monitor if it is being removed due to
493− * suspend. It will be re-monitored on resume.
494− */
495− if (monitor && !msft->suspending)
496− hci_free_adv_monitor(hdev, monitor);
497−
498− list_del(&handle_data->list);
499− kfree(handle_data);
500− }
501−
502− /* If in suspend/remove sequence, keep removing. */
503− if (msft->suspending)
504− remove_monitor_on_suspend(hdev,
505− msft->pending_remove_handle + 1);
506−
507− /* If remove all monitors is required, we need to continue the process
508− * here because the earlier it was paused when waiting for the
509− * response from controller.
510− */
511− if (msft->pending_remove_handle == 0) {
512− pending = hci_remove_all_adv_monitor(hdev, &err);
513− if (pending) {
514− hci_dev_unlock(hdev);
515− return;
516− }
517−
518− if (err)
519− status = HCI_ERROR_UNSPECIFIED;
520− }
521−
522− hci_dev_unlock(hdev);
523−
524−done:
525− if (!msft->suspending)
526− hci_remove_adv_monitor_complete(hdev, status);
527−}
528−
529633 static void msft_le_set_advertisement_filter_enable_cb(struct hci_dev *hdev,
530634 u8 status, u16 opcode,
531635 struct sk_buff *skb)
@@ -560,35 +664,6 @@ static void msft_le_set_advertisement_filter_enable_cb(struct hci_dev *hdev, · 28 unmodified lines
560664 hci_dev_unlock(hdev);
561665 }
562666
563−static bool msft_monitor_rssi_valid(struct adv_monitor *monitor)
564−{
565− struct adv_rssi_thresholds *r = &monitor->rssi;
566−
567− if (r->high_threshold < MSFT_RSSI_THRESHOLD_VALUE_MIN ||
568− r->high_threshold > MSFT_RSSI_THRESHOLD_VALUE_MAX ||
569− r->low_threshold < MSFT_RSSI_THRESHOLD_VALUE_MIN ||
570− r->low_threshold > MSFT_RSSI_THRESHOLD_VALUE_MAX)
571− return false;
572−
573− /* High_threshold_timeout is not supported,
574− * once high_threshold is reached, events are immediately reported.
575− */
576− if (r->high_threshold_timeout != 0)
577− return false;
578−
579− if (r->low_threshold_timeout > MSFT_RSSI_LOW_TIMEOUT_MAX)
580− return false;
581−
582− /* Sampling period from 0x00 to 0xFF are all allowed */
583− return true;
584−}
585−
586−static bool msft_monitor_pattern_valid(struct adv_monitor *monitor)
587−{
588− return msft_monitor_rssi_valid(monitor);
589− /* No additional check needed for pattern-based monitor */
590−}
591−
592667 /* This function requires the caller holds hdev->lock */
593668 static int __msft_add_monitor_pattern(struct hci_dev *hdev,
594669 struct adv_monitor *monitor)
@@ -656,7 +731,7 @@ int msft_add_monitor_pattern(struct hci_dev *hdev, struct adv_monitor *monitor) · 61 unmodified lines
656731 if (!msft)
657732 return -EOPNOTSUPP;
658733
659− if (msft->reregistering || msft->suspending)
734+ if (msft->resuming || msft->suspending)
660735 return -EBUSY;
661736
662737 return __msft_add_monitor_pattern(hdev, monitor);
@@ -700,7 +775,7 @@ int msft_remove_monitor(struct hci_dev *hdev, struct adv_monitor *monitor, · 37 unmodified lines
700775 if (!msft)
701776 return -EOPNOTSUPP;
702777
703− if (msft->reregistering || msft->suspending)
778+ if (msft->resuming || msft->suspending)
704779 return -EBUSY;
705780
706781 return __msft_remove_monitor(hdev, monitor, handle);
net/bluetooth/msft.h+11−4
@@ -24,8 +24,8 @@ int msft_remove_monitor(struct hci_dev *hdev, struct adv_monitor *monitor, · 23 unmodified lines
2424 u16 handle);
2525 void msft_req_add_set_filter_enable(struct hci_request *req, bool enable);
2626 int msft_set_filter_enable(struct hci_dev *hdev, bool enable);
27−void msft_suspend(struct hci_dev *hdev);
28−void msft_resume(struct hci_dev *hdev);
27+int msft_suspend_sync(struct hci_dev *hdev);
28+int msft_resume_sync(struct hci_dev *hdev);
2929 bool msft_curve_validity(struct hci_dev *hdev);
3030
3131 #else
@@ -61,8 +61,15 @@ static inline int msft_set_filter_enable(struct hci_dev *hdev, bool enable) · 29 unmodified lines
6161 return -EOPNOTSUPP;
6262 }
6363
64−static inline void msft_suspend(struct hci_dev *hdev) {}
65−static inline void msft_resume(struct hci_dev *hdev) {}
64+static inline int msft_suspend_sync(struct hci_dev *hdev)
65+{
66+ return -EOPNOTSUPP;
67+}
68+
69+static inline int msft_resume_sync(struct hci_dev *hdev)
70+{
71+ return -EOPNOTSUPP;
72+}
6673
6774 static inline bool msft_curve_validity(struct hci_dev *hdev)
6875 {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t