@@ -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 */
922971void 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
9311034done :
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