1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "wifi_hal/driver_tool.h"
18 
19 #include <android-base/logging.h>
20 #include <private/android_filesystem_config.h>
21 
22 #include "hardware_legacy/wifi.h"
23 
24 namespace android {
25 namespace wifi_hal {
26 
27 const int DriverTool::kFirmwareModeSta = WIFI_GET_FW_PATH_STA;
28 const int DriverTool::kFirmwareModeAp = WIFI_GET_FW_PATH_AP;
29 const int DriverTool::kFirmwareModeP2p = WIFI_GET_FW_PATH_P2P;
30 
TakeOwnershipOfFirmwareReload()31 bool DriverTool::TakeOwnershipOfFirmwareReload() {
32   if (!wifi_get_fw_path(kFirmwareModeSta) &&
33       !wifi_get_fw_path(kFirmwareModeAp) &&
34       !wifi_get_fw_path(kFirmwareModeP2p)) {
35     return true;  // HAL doesn't think we need to load firmware for any mode.
36   }
37 
38   if (chown(WIFI_DRIVER_FW_PATH_PARAM, AID_WIFI, AID_WIFI) != 0) {
39     PLOG(ERROR) << "Error changing ownership of '" << WIFI_DRIVER_FW_PATH_PARAM
40                 << "' to wifi:wifi";
41     return false;
42   }
43 
44   return true;
45 }
46 
LoadDriver()47 bool DriverTool::LoadDriver() {
48   return ::wifi_load_driver() == 0;
49 }
50 
UnloadDriver()51 bool DriverTool::UnloadDriver() {
52   return ::wifi_unload_driver() == 0;
53 }
54 
IsDriverLoaded()55 bool DriverTool::IsDriverLoaded() {
56   return ::wifi_unload_driver() != 0;
57 }
58 
IsFirmwareModeChangeNeeded(int mode)59 bool DriverTool::IsFirmwareModeChangeNeeded(int mode) {
60   return (wifi_get_fw_path(mode) != nullptr);
61 }
62 
ChangeFirmwareMode(int mode)63 bool DriverTool::ChangeFirmwareMode(int mode) {
64   const char* fwpath = wifi_get_fw_path(mode);
65   if (!fwpath) {
66     return true;  // HAL doesn't think we need to load firmware for this mode.
67   }
68   if (wifi_change_fw_path(fwpath) != 0) {
69     // Not all devices actually require firmware reloads, but
70     // failure to change the firmware path when it is defined is an error.
71     return false;
72   }
73   return true;
74 }
75 
76 }  // namespace wifi_hal
77 }  // namespace android
78