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"
37 #define QUAD_CPUS_COUNT 4
38 #define TEMP_BUFFER_LEN 100
39 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
41 static int __netconfig_sta_firmware_start(const char *interface_name)
44 const char *path = WLAN_DRIVER_SCRIPT;
45 char *const args[] = { "/usr/bin/wlan.sh", "start", (char *)interface_name, NULL };
46 char *const envs[] = { NULL };
48 rv = netconfig_execute_file(path, args, envs);
52 rv = netconfig_interface_up(interface_name);
56 DBG("Successfully loaded wireless device driver");
60 static int __netconfig_sta_firmware_stop(const char *interface_name)
63 const char *path = WLAN_DRIVER_SCRIPT;
64 char *const args[] = { "/usr/bin/wlan.sh", "stop", (char *)interface_name, NULL };
65 char *const envs[] = { NULL };
67 /* Update statistics before driver remove */
68 netconfig_wifi_statistics_update_powered_off();
70 rv = netconfig_interface_down(interface_name);
74 rv = netconfig_execute_file(path, args, envs);
78 DBG("Successfully removed wireless device driver");
82 static int __netconfig_p2p_firmware_start(const char *interface_name)
84 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
88 const char *path = WLAN_DRIVER_SCRIPT;
89 char *const args[] = { "/usr/bin/wlan.sh", "p2p", (char *)interface_name, NULL };
90 char *const envs[] = { NULL };
92 rv = netconfig_execute_file(path, args, envs);
96 #if defined TIZEN_WLAN_USE_P2P_INTERFACE
97 rv = netconfig_interface_up(interface_name);
102 DBG("Successfully loaded p2p device driver");
106 static int __netconfig_p2p_firmware_stop(const char *interface_name)
108 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
112 const char *path = WLAN_DRIVER_SCRIPT;
113 char *const args[] = { "/usr/bin/wlan.sh", "stop", (char *)interface_name, NULL };
114 char *const envs[] = { NULL };
116 rv = netconfig_interface_down(interface_name);
120 rv = netconfig_execute_file(path, args, envs);
124 DBG("Successfully removed p2p device driver");
128 static int __netconfig_softap_firmware_start(const char *interface_name)
130 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)
131 && !netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP))
135 const char *path = WLAN_DRIVER_SCRIPT;
136 char *const args[] = { "/usr/bin/wlan.sh", "softap", (char *)interface_name, NULL };
137 char *const envs[] = { NULL };
139 rv = netconfig_execute_file(path, args, envs);
143 if (netconfig_interface_up(interface_name) == FALSE)
146 DBG("Successfully loaded softap device driver");
150 static int __netconfig_softap_firmware_stop(const char *interface_name)
152 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)
153 && !netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP))
157 const char *path = WLAN_DRIVER_SCRIPT;
158 char *const args[] = { "/usr/bin/wlan.sh", "stop", (char *)interface_name, NULL };
159 char *const envs[] = { NULL };
161 rv = netconfig_interface_down(interface_name);
165 rv = netconfig_execute_file(path, args, envs);
169 DBG("Successfully removed softap device driver");
173 static int __netconfig_wifi_firmware_start(enum netconfig_wifi_firmware type,
174 const char *interface_name)
176 if (emulator_is_emulated() == TRUE)
180 case NETCONFIG_WIFI_STA:
181 return __netconfig_sta_firmware_start(interface_name);
182 case NETCONFIG_WIFI_P2P:
183 return __netconfig_p2p_firmware_start(interface_name);
184 case NETCONFIG_WIFI_SOFTAP:
185 return __netconfig_softap_firmware_start(interface_name);
193 static int __netconfig_wifi_firmware_stop(enum netconfig_wifi_firmware type,
194 const char *interface_name)
196 if (emulator_is_emulated() == TRUE)
200 case NETCONFIG_WIFI_STA:
201 return __netconfig_sta_firmware_stop(interface_name);
202 case NETCONFIG_WIFI_P2P:
203 return __netconfig_p2p_firmware_stop(interface_name);
204 case NETCONFIG_WIFI_SOFTAP:
205 return __netconfig_softap_firmware_stop(interface_name);
213 static int __netconfig_set_rps_cpus(const char *interface_name)
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,
228 "/sys/class/net/%s/queues/rx-0/rps_cpus",
230 DBG("Command : [%s]", t_buf);
233 while ((fd = open(t_buf, O_RDWR | O_CLOEXEC)) >= 0) {
235 count = read(fd, r_buf, 1);
242 DBG("read size = %zd", count);
245 if (r_buf[0] == 'e') {
247 DBG("e is already written");
248 snprintf(t_buf, TEMP_BUFFER_LEN,
249 "/sys/class/net/%s/queues/rx-%d/rps_cpus",
250 interface_name, curr);
251 DBG("Command : [%s]", t_buf);
257 if (lseek(fd, 0, SEEK_SET) < 0) {
263 count = write(fd, "e", 1);
270 DBG("write size = %zd", count);
274 snprintf(t_buf, TEMP_BUFFER_LEN,
275 "/sys/class/net/%s/queues/rx-%d/rps_cpus",
276 interface_name, curr);
277 DBG("Command : [%s]", t_buf);
283 int netconfig_wifi_firmware(enum netconfig_wifi_firmware type,
284 const char *interface_name, gboolean enable)
287 static enum netconfig_wifi_firmware current_driver = NETCONFIG_WIFI_OFF;
288 enum netconfig_wifi_firmware alias = type;
290 DBG("Wi-Fi current firmware %d (type: %d %s)", current_driver, type,
291 enable == TRUE ? "enable" : "disable");
293 if (enable == FALSE) {
294 if (current_driver == NETCONFIG_WIFI_OFF) {
296 } else if (current_driver == alias) {
298 err = __netconfig_wifi_firmware_stop(type, interface_name);
299 if (err < 0 && err != -EALREADY)
302 current_driver = NETCONFIG_WIFI_OFF;
310 if (current_driver > NETCONFIG_WIFI_OFF) {
311 if (current_driver == alias)
317 err = __netconfig_wifi_firmware_start(type, interface_name);
319 DBG("Failed to execute script file");
321 current_driver = alias;
323 if (__netconfig_set_rps_cpus(interface_name) < 0)
324 DBG("Failed to set rps_cpus");
329 gboolean handle_start(WifiFirmware *firmware, GDBusMethodInvocation *context,
330 const gchar *device, const gchar *ifname)
334 g_return_val_if_fail(firmware != NULL, TRUE);
336 DBG("Wi-Fi firmware start %s", device != NULL ? device : "null");
338 if (g_strcmp0("p2p", device) == 0)
339 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, ifname, TRUE);
340 else if (g_strcmp0("softap", device) == 0)
341 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, ifname, TRUE);
346 if (err == -EALREADY)
347 netconfig_error_already_exists(context);
348 else if (g_strcmp0("softap", device) == 0 &&
349 err == -EIO && netconfig_is_wifi_direct_on() == FALSE) {
350 if (netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, ifname, FALSE) == 0 &&
351 netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, ifname, TRUE) == 0) {
352 wifi_firmware_complete_start(firmware, context);
355 netconfig_error_wifi_driver_failed(context);
357 netconfig_error_wifi_driver_failed(context);
362 wifi_firmware_complete_start(firmware, context);
366 gboolean handle_stop(WifiFirmware *firmware, GDBusMethodInvocation *context,
367 const gchar *device, const gchar *ifname)
371 g_return_val_if_fail(firmware != NULL, TRUE);
373 DBG("Wi-Fi firmware stop %s", device != NULL ? device : "null");
375 if (g_strcmp0("p2p", device) == 0)
376 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, ifname, FALSE);
377 else if (g_strcmp0("softap", device) == 0)
378 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, ifname, FALSE);
383 if (err == -EALREADY)
384 netconfig_error_already_exists(context);
386 netconfig_error_wifi_driver_failed(context);
391 wifi_firmware_complete_stop(firmware, context);