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>
32 #include "netsupplicant.h"
33 #include "wifi-firmware.h"
34 #include "network-statistics.h"
36 #define QUAD_CPUS_COUNT 4
37 #define TEMP_BUFFER_LEN 100
38 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
42 enum netconfig_wifi_firmware type;
45 static GSList *wifi_driver_list;
47 static void __netconfig_wifi_driver_free(gpointer data)
49 wifi_driver_s *driver = (wifi_driver_s *) data;
51 DBG("Remove wifi driver [%d:%s]", driver->type, driver->interface_name);
53 g_free(driver->interface_name);
57 static gint __netconfig_cmp_wifi_driver(gconstpointer a, gconstpointer b)
59 wifi_driver_s *drv_a = (wifi_driver_s *)a;
60 wifi_driver_s *drv_b = (wifi_driver_s *)b;
62 if (g_strcmp0(drv_a->interface_name, drv_b->interface_name))
68 static GSList * __netconfig_is_wifi_driver_in_list(
69 enum netconfig_wifi_firmware type, const char *interface_name)
71 wifi_driver_s driver = { .type = type, .interface_name = (char *)interface_name };
72 GSList *found = g_slist_find_custom(wifi_driver_list, &driver,
73 __netconfig_cmp_wifi_driver);
78 static gboolean __netconfig_add_wifi_driver_to_list(
79 enum netconfig_wifi_firmware type, const char *interface_name)
82 wifi_driver_s *driver;
84 found = __netconfig_is_wifi_driver_in_list(type, interface_name);
88 driver = g_try_new0(wifi_driver_s, 1);
90 ERR("g_try_new0 failed!!!");
94 driver->interface_name = g_strdup(interface_name);
95 if (!driver->interface_name) {
96 ERR("g_strdup failed!!!");
103 wifi_driver_list = g_slist_prepend(wifi_driver_list, driver);
107 static gboolean __netconfig_remove_wifi_driver_from_list(
108 enum netconfig_wifi_firmware type, const char *interface_name)
112 found = __netconfig_is_wifi_driver_in_list(type, interface_name);
116 __netconfig_wifi_driver_free(found->data);
117 wifi_driver_list = g_slist_delete_link(wifi_driver_list, found);
122 static int __netconfig_sta_firmware_start(const char *interface_name)
126 rv = hal_wifi_get_backend();
128 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
132 rv = hal_wifi_sta_start(interface_name);
134 DBG("hal_wifi_sta_start() failed, ret: %d", rv);
138 rv = netconfig_interface_up(interface_name);
142 DBG("Successfully loaded wireless device driver");
146 static int __netconfig_sta_firmware_stop(const char *interface_name)
150 rv = netconfig_interface_down(interface_name);
154 rv = hal_wifi_get_backend();
156 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
160 rv = hal_wifi_stop(interface_name);
162 DBG("hal_wifi_stop() failed, ret: %d", rv);
163 netconfig_interface_up(interface_name);
167 DBG("Successfully removed wireless device driver");
171 static int __netconfig_p2p_firmware_start(const char *interface_name)
173 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
178 rv = hal_wifi_get_backend();
180 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
184 rv = hal_wifi_p2p_start(interface_name);
186 DBG("hal_wifi_p2p_start() failed, ret: %d", rv);
190 #if defined TIZEN_WLAN_USE_P2P_INTERFACE
191 rv = netconfig_interface_up(interface_name);
196 DBG("Successfully loaded p2p device driver");
200 static int __netconfig_p2p_firmware_stop(const char *interface_name)
202 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
207 rv = netconfig_interface_down(interface_name);
211 rv = hal_wifi_get_backend();
213 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
217 rv = hal_wifi_stop(interface_name);
219 DBG("hal_wifi_stop() failed, ret: %d", rv);
220 netconfig_interface_up(interface_name);
224 DBG("Successfully removed p2p device driver");
228 static int __netconfig_softap_firmware_start(const char *interface_name)
230 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)
231 && !netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP))
236 rv = hal_wifi_get_backend();
238 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
242 rv = hal_wifi_softap_start(interface_name);
244 DBG("hal_wifi_softap_start() failed, ret: %d", rv);
248 if (netconfig_interface_up(interface_name) == FALSE)
251 DBG("Successfully loaded softap device driver");
255 static int __netconfig_softap_firmware_stop(const char *interface_name)
257 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)
258 && !netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP))
263 rv = netconfig_interface_down(interface_name);
267 rv = hal_wifi_get_backend();
269 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
273 rv = hal_wifi_stop(interface_name);
275 DBG("hal_wifi_stop() failed, ret: %d", rv);
276 netconfig_interface_up(interface_name);
280 DBG("Successfully removed softap device driver");
284 static int __netconfig_wifi_firmware_start(enum netconfig_wifi_firmware type,
285 const char *interface_name)
287 if (emulator_is_emulated() == TRUE)
291 case NETCONFIG_WIFI_STA:
292 return __netconfig_sta_firmware_start(interface_name);
293 case NETCONFIG_WIFI_P2P:
294 return __netconfig_p2p_firmware_start(interface_name);
295 case NETCONFIG_WIFI_SOFTAP:
296 return __netconfig_softap_firmware_start(interface_name);
304 static int __netconfig_wifi_firmware_stop(enum netconfig_wifi_firmware type,
305 const char *interface_name)
307 if (emulator_is_emulated() == TRUE)
311 case NETCONFIG_WIFI_STA:
312 return __netconfig_sta_firmware_stop(interface_name);
313 case NETCONFIG_WIFI_P2P:
314 return __netconfig_p2p_firmware_stop(interface_name);
315 case NETCONFIG_WIFI_SOFTAP:
316 return __netconfig_softap_firmware_stop(interface_name);
324 static int __netconfig_set_rps_cpus(const char *interface_name)
328 char t_buf[TEMP_BUFFER_LEN];
329 char r_buf[TEMP_BUFFER_LEN];
331 if (access(WIFI_MAC_ADD_PATH, F_OK) != 0) {
332 DBG("WiFi driver is not loaded... ");
335 DBG("WiFi driver loaded... ");
338 snprintf(t_buf, TEMP_BUFFER_LEN,
339 "/sys/class/net/%s/queues/rx-0/rps_cpus",
341 DBG("Command : [%s]", t_buf);
344 while ((fd = open(t_buf, O_RDWR | O_CLOEXEC)) >= 0) {
346 count = read(fd, r_buf, 1);
353 DBG("read size = %zd", count);
356 if (r_buf[0] == 'e') {
358 DBG("e is already written");
359 snprintf(t_buf, TEMP_BUFFER_LEN,
360 "/sys/class/net/%s/queues/rx-%d/rps_cpus",
361 interface_name, curr);
362 DBG("Command : [%s]", t_buf);
368 if (lseek(fd, 0, SEEK_SET) < 0) {
374 count = write(fd, "e", 1);
381 DBG("write size = %zd", count);
385 snprintf(t_buf, TEMP_BUFFER_LEN,
386 "/sys/class/net/%s/queues/rx-%d/rps_cpus",
387 interface_name, curr);
388 DBG("Command : [%s]", t_buf);
394 int netconfig_wifi_firmware_get_mac(const char *interface_name, char **mac)
398 rv = hal_wifi_get_backend();
400 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
404 rv = hal_wifi_get_mac(interface_name, mac);
406 DBG("hal_wifi_get_mac() failed, ret: %d", rv);
410 DBG("Successfully loaded default wifi mac address: %s", *mac);
414 int netconfig_wifi_firmware(enum netconfig_wifi_firmware type,
415 const char *interface_name, gboolean enable)
419 DBG("Wi-Fi firmware (type: %d %s) for %s", type, enable == TRUE ? "enable" : "disable",
420 interface_name ? interface_name : "");
422 GSList *found = __netconfig_is_wifi_driver_in_list(type, interface_name);
424 if (enable == FALSE) {
425 wifi_driver_s *driver;
430 driver = (wifi_driver_s *) found->data;
432 if (type != driver->type)
435 err = __netconfig_wifi_firmware_stop(type, interface_name);
436 if (err < 0 && err != -EALREADY)
439 __netconfig_remove_wifi_driver_from_list(type, interface_name);
445 wifi_driver_s *driver = (wifi_driver_s *) found->data;
447 DBG("Wi-Fi interface (%s) already in use", interface_name);
449 if (type == driver->type)
455 err = __netconfig_wifi_firmware_start(type, interface_name);
457 DBG("Failed to execute script file");
459 __netconfig_add_wifi_driver_to_list(type, interface_name);
461 if (__netconfig_set_rps_cpus(interface_name) < 0)
462 DBG("Failed to set rps_cpus");
467 gboolean handle_start(WifiFirmware *firmware, GDBusMethodInvocation *context,
468 const gchar *device, const gchar *ifname)
472 g_return_val_if_fail(firmware != NULL, TRUE);
474 DBG("Wi-Fi firmware start %s", device != NULL ? device : "null");
476 if (g_strcmp0("p2p", device) == 0)
477 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, ifname, TRUE);
478 else if (g_strcmp0("softap", device) == 0)
479 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, ifname, TRUE);
484 if (err == -EALREADY)
485 netconfig_error_already_exists(context);
486 else if (g_strcmp0("softap", device) == 0 &&
487 err == -EIO && netconfig_is_wifi_direct_on() == FALSE) {
488 if (netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, ifname, FALSE) == 0 &&
489 netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, ifname, TRUE) == 0) {
490 wifi_firmware_complete_start(firmware, context);
493 netconfig_error_wifi_driver_failed(context);
495 netconfig_error_wifi_driver_failed(context);
500 wifi_firmware_complete_start(firmware, context);
504 gboolean handle_stop(WifiFirmware *firmware, GDBusMethodInvocation *context,
505 const gchar *device, const gchar *ifname)
509 g_return_val_if_fail(firmware != NULL, TRUE);
511 DBG("Wi-Fi firmware stop %s", device != NULL ? device : "null");
513 if (g_strcmp0("p2p", device) == 0)
514 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, ifname, FALSE);
515 else if (g_strcmp0("softap", device) == 0)
516 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, ifname, FALSE);
521 if (err == -EALREADY)
522 netconfig_error_already_exists(context);
524 netconfig_error_wifi_driver_failed(context);
529 wifi_firmware_complete_stop(firmware, context);