2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <vconf-keys.h>
31 #include "netsupplicant.h"
32 #include "wifi-firmware.h"
33 #include "network-statistics.h"
35 #define WLAN_DRIVER_SCRIPT "/usr/bin/wlan.sh"
36 #define WLAN_IFACE_NAME "wlan0"
38 #define WLAN_P2P_IFACE_NAME_TV "p2p0"
39 #define WLAN_P2P_IFACE_NAME_COMMON "wlan0"
40 #define WLAN_P2P_IFACE_NAME ((TIZEN_TV) ? (WLAN_P2P_IFACE_NAME_TV) : (WLAN_P2P_IFACE_NAME_COMMON))
41 #define QUAD_CPUS_COUNT 4
42 #define TEMP_BUFFER_LEN 100
43 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
45 static int __netconfig_sta_firmware_start(void)
48 const char *path = WLAN_DRIVER_SCRIPT;
49 char *const args[] = { "/usr/bin/wlan.sh", "start", NULL };
50 char *const envs[] = { NULL };
52 rv = netconfig_execute_file(path, args, envs);
56 rv = netconfig_interface_up(WLAN_IFACE_NAME);
60 DBG("Successfully loaded wireless device driver");
64 static int __netconfig_sta_firmware_stop(void)
67 const char *path = WLAN_DRIVER_SCRIPT;
68 char *const args[] = { "/usr/bin/wlan.sh", "stop", NULL };
69 char *const envs[] = { NULL };
71 /* Update statistics before driver remove */
72 netconfig_wifi_statistics_update_powered_off();
74 rv = netconfig_interface_down(WLAN_IFACE_NAME);
78 rv = netconfig_execute_file(path, args, envs);
82 DBG("Successfully removed wireless device driver");
86 static int __netconfig_p2p_firmware_start(void)
88 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
92 const char *path = WLAN_DRIVER_SCRIPT;
93 char *const args[] = { "/usr/bin/wlan.sh", "p2p", NULL };
94 char *const envs[] = { NULL };
96 rv = netconfig_execute_file(path, args, envs);
100 #if defined TIZEN_WLAN_USE_P2P_INTERFACE
101 rv = netconfig_interface_up(WLAN_P2P_IFACE_NAME);
106 DBG("Successfully loaded p2p device driver");
110 static int __netconfig_p2p_firmware_stop(void)
112 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
116 const char *path = WLAN_DRIVER_SCRIPT;
117 char *const args[] = { "/usr/bin/wlan.sh", "stop", NULL };
118 char *const envs[] = { NULL };
120 rv = netconfig_interface_down(WLAN_IFACE_NAME);
124 rv = netconfig_execute_file(path, args, envs);
128 DBG("Successfully removed p2p device driver");
132 static int __netconfig_softap_firmware_start(void)
134 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING))
138 const char *path = WLAN_DRIVER_SCRIPT;
139 char *const args[] = { "/usr/bin/wlan.sh", "softap", NULL };
140 char *const envs[] = { NULL };
142 rv = netconfig_execute_file(path, args, envs);
146 if (netconfig_interface_up(WLAN_IFACE_NAME) == FALSE)
149 DBG("Successfully loaded softap device driver");
153 static int __netconfig_softap_firmware_stop(void)
155 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING))
159 const char *path = WLAN_DRIVER_SCRIPT;
160 char *const args[] = { "/usr/bin/wlan.sh", "stop", NULL };
161 char *const envs[] = { NULL };
163 rv = netconfig_interface_down(WLAN_IFACE_NAME);
167 rv = netconfig_execute_file(path, args, envs);
171 DBG("Successfully removed softap device driver");
175 static int __netconfig_wifi_firmware_start(enum netconfig_wifi_firmware type)
177 if (emulator_is_emulated() == TRUE)
181 case NETCONFIG_WIFI_STA:
182 return __netconfig_sta_firmware_start();
183 case NETCONFIG_WIFI_P2P:
184 return __netconfig_p2p_firmware_start();
185 case NETCONFIG_WIFI_SOFTAP:
186 return __netconfig_softap_firmware_start();
194 static int __netconfig_wifi_firmware_stop(enum netconfig_wifi_firmware type)
196 if (emulator_is_emulated() == TRUE)
200 case NETCONFIG_WIFI_STA:
201 return __netconfig_sta_firmware_stop();
202 case NETCONFIG_WIFI_P2P:
203 return __netconfig_p2p_firmware_stop();
204 case NETCONFIG_WIFI_SOFTAP:
205 return __netconfig_softap_firmware_stop();
213 static int __netconfig_set_rps_cpus(void)
217 char t_buf[TEMP_BUFFER_LEN];
218 char r_buf[TEMP_BUFFER_LEN];
220 if (access(WIFI_MAC_ADD_PATH, F_OK) != 0) {
221 DBG("WiFi driver is not loaded... ");
224 DBG("WiFi driver loaded... ");
227 snprintf(t_buf, TEMP_BUFFER_LEN, "/sys/class/net/wlan0/queues/rx-0/rps_cpus");
228 DBG("Command : [%s]", t_buf);
231 while ((fd = open(t_buf, O_RDWR | O_CLOEXEC)) >= 0) {
233 count = read(fd, r_buf, 1);
240 DBG("read size = %d", count);
243 if (r_buf[0] == 'e') {
245 DBG("e is already written");
246 snprintf(t_buf, TEMP_BUFFER_LEN, "/sys/class/net/wlan0/queues/rx-%d/rps_cpus", curr);
247 DBG("Command : [%s]", t_buf);
253 if (lseek(fd, 0, SEEK_SET) < 0) {
259 count = write(fd, "e", 1);
266 DBG("write size = %d", count);
270 snprintf(t_buf, TEMP_BUFFER_LEN, "/sys/class/net/wlan0/queues/rx-%d/rps_cpus", curr);
271 DBG("Command : [%s]", t_buf);
277 int netconfig_wifi_firmware(enum netconfig_wifi_firmware type, gboolean enable)
280 static enum netconfig_wifi_firmware current_driver = NETCONFIG_WIFI_OFF;
281 enum netconfig_wifi_firmware alias = type;
283 DBG("Wi-Fi current firmware %d (type: %d %s)", current_driver, type,
284 enable == TRUE ? "enable" : "disable");
286 if (enable == FALSE) {
287 if (current_driver == NETCONFIG_WIFI_OFF) {
289 } else if (current_driver == alias) {
291 err = __netconfig_wifi_firmware_stop(type);
292 if (err < 0 && err != -EALREADY)
295 current_driver = NETCONFIG_WIFI_OFF;
303 if (current_driver > NETCONFIG_WIFI_OFF) {
304 if (current_driver == alias)
310 err = __netconfig_wifi_firmware_start(type);
312 DBG("Failed to execute script file");
314 current_driver = alias;
316 if (__netconfig_set_rps_cpus() < 0)
317 DBG("Failed to set rps_cpus");
322 gboolean handle_start(WifiFirmware *firmware, GDBusMethodInvocation *context, const gchar *device)
326 g_return_val_if_fail(firmware != NULL, TRUE);
328 DBG("Wi-Fi firmware start %s", device != NULL ? device : "null");
330 if (g_strcmp0("p2p", device) == 0)
331 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, TRUE);
332 else if (g_strcmp0("softap", device) == 0)
333 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, TRUE);
338 if (err == -EALREADY)
339 netconfig_error_already_exists(context);
340 else if (g_strcmp0("softap", device) == 0 && err == -EIO && netconfig_is_wifi_direct_on() == FALSE) {
341 if (netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, FALSE) == 0 && netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, TRUE) == 0) {
342 wifi_firmware_complete_start(firmware, context);
345 netconfig_error_wifi_driver_failed(context);
347 netconfig_error_wifi_driver_failed(context);
352 wifi_firmware_complete_start(firmware, context);
356 gboolean handle_stop(WifiFirmware *firmware, GDBusMethodInvocation *context, const gchar *device)
360 g_return_val_if_fail(firmware != NULL, TRUE);
362 DBG("Wi-Fi firmware stop %s", device != NULL ? device : "null");
364 if (g_strcmp0("p2p", device) == 0)
365 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, FALSE);
366 else if (g_strcmp0("softap", device) == 0)
367 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, FALSE);
372 if (err == -EALREADY)
373 netconfig_error_already_exists(context);
375 netconfig_error_wifi_driver_failed(context);
380 wifi_firmware_complete_stop(firmware, context);