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>
33 #include "netsupplicant.h"
34 #include "wifi-firmware.h"
35 #include "network-statistics.h"
37 #define QUAD_CPUS_COUNT 4
38 #define TEMP_BUFFER_LEN 100
39 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
43 enum netconfig_wifi_firmware type;
46 static GSList *wifi_driver_list;
48 static void __netconfig_wifi_driver_free(gpointer data)
50 wifi_driver_s *driver = (wifi_driver_s *) data;
52 DBG("Remove wifi driver [%d:%s]", driver->type, driver->interface_name);
54 g_free(driver->interface_name);
58 static gint __netconfig_cmp_wifi_driver(gconstpointer a, gconstpointer b)
60 wifi_driver_s *drv_a = (wifi_driver_s *)a;
61 wifi_driver_s *drv_b = (wifi_driver_s *)b;
63 if (g_strcmp0(drv_a->interface_name, drv_b->interface_name))
69 static GSList * __netconfig_is_wifi_driver_in_list(
70 enum netconfig_wifi_firmware type, const char *interface_name)
72 wifi_driver_s driver = { .type = type, .interface_name = (char *)interface_name };
73 GSList *found = g_slist_find_custom(wifi_driver_list, &driver,
74 __netconfig_cmp_wifi_driver);
79 static gboolean __netconfig_add_wifi_driver_to_list(
80 enum netconfig_wifi_firmware type, const char *interface_name)
83 wifi_driver_s *driver;
85 found = __netconfig_is_wifi_driver_in_list(type, interface_name);
89 driver = g_try_new0(wifi_driver_s, 1);
91 ERR("g_try_new0 failed!!!");
95 driver->interface_name = g_strdup(interface_name);
96 if (!driver->interface_name) {
97 ERR("g_strdup failed!!!");
104 wifi_driver_list = g_slist_prepend(wifi_driver_list, driver);
108 static gboolean __netconfig_remove_wifi_driver_from_list(
109 enum netconfig_wifi_firmware type, const char *interface_name)
113 found = __netconfig_is_wifi_driver_in_list(type, interface_name);
117 __netconfig_wifi_driver_free(found->data);
118 wifi_driver_list = g_slist_delete_link(wifi_driver_list, found);
123 static int __netconfig_sta_firmware_start(const char *interface_name)
128 if_found = wifi_check_interface(interface_name);
129 rv = hal_wifi_get_backend();
130 if (rv < 0 && !if_found) {
131 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
135 rv = hal_wifi_sta_start(interface_name);
136 if (rv < 0 && !if_found) {
137 DBG("hal_wifi_sta_start() failed, ret: %d", rv);
141 rv = netconfig_interface_up(interface_name);
145 DBG("Successfully loaded wireless device driver");
149 static int __netconfig_sta_firmware_stop(const char *interface_name)
153 rv = netconfig_interface_down(interface_name);
157 rv = hal_wifi_get_backend();
159 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
163 rv = hal_wifi_stop(interface_name);
165 DBG("hal_wifi_stop() failed, ret: %d", rv);
166 netconfig_interface_up(interface_name);
170 DBG("Successfully removed wireless device driver");
174 static int __netconfig_p2p_firmware_start(const char *interface_name)
176 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
182 if_found = wifi_check_interface(interface_name);
183 rv = hal_wifi_get_backend();
184 if (rv < 0 && !if_found) {
185 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
189 rv = hal_wifi_p2p_start(interface_name);
190 if (rv < 0 && !if_found) {
191 DBG("hal_wifi_p2p_start() failed, ret: %d", rv);
195 #if defined TIZEN_WLAN_USE_P2P_INTERFACE
196 rv = netconfig_interface_up(interface_name);
201 DBG("Successfully loaded p2p device driver");
205 static int __netconfig_p2p_firmware_stop(const char *interface_name)
207 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
212 rv = netconfig_interface_down(interface_name);
216 rv = hal_wifi_get_backend();
218 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
222 rv = hal_wifi_stop(interface_name);
224 DBG("hal_wifi_stop() failed, ret: %d", rv);
225 netconfig_interface_up(interface_name);
229 DBG("Successfully removed p2p device driver");
233 static int __netconfig_softap_firmware_start(const char *interface_name)
235 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)
236 && !netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP))
242 if_found = wifi_check_interface(interface_name);
243 rv = hal_wifi_get_backend();
244 if (rv < 0 && !if_found) {
245 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
249 rv = hal_wifi_softap_start(interface_name);
250 if (rv < 0 && !if_found) {
251 DBG("hal_wifi_softap_start() failed, ret: %d", rv);
255 if (netconfig_interface_up(interface_name) == FALSE)
258 DBG("Successfully loaded softap device driver");
262 static int __netconfig_softap_firmware_stop(const char *interface_name)
264 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)
265 && !netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP))
270 rv = netconfig_interface_down(interface_name);
274 rv = hal_wifi_get_backend();
276 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
280 rv = hal_wifi_stop(interface_name);
282 DBG("hal_wifi_stop() failed, ret: %d", rv);
283 netconfig_interface_up(interface_name);
287 DBG("Successfully removed softap device driver");
291 static int __netconfig_wifi_firmware_start(enum netconfig_wifi_firmware type,
292 const char *interface_name)
294 if (emulator_is_emulated() == TRUE)
298 case NETCONFIG_WIFI_STA:
299 return __netconfig_sta_firmware_start(interface_name);
300 case NETCONFIG_WIFI_P2P:
301 return __netconfig_p2p_firmware_start(interface_name);
302 case NETCONFIG_WIFI_SOFTAP:
303 return __netconfig_softap_firmware_start(interface_name);
311 static int __netconfig_wifi_firmware_stop(enum netconfig_wifi_firmware type,
312 const char *interface_name)
314 if (emulator_is_emulated() == TRUE)
318 case NETCONFIG_WIFI_STA:
319 return __netconfig_sta_firmware_stop(interface_name);
320 case NETCONFIG_WIFI_P2P:
321 return __netconfig_p2p_firmware_stop(interface_name);
322 case NETCONFIG_WIFI_SOFTAP:
323 return __netconfig_softap_firmware_stop(interface_name);
331 static int __netconfig_set_rps_cpus(const char *interface_name)
335 char t_buf[TEMP_BUFFER_LEN];
336 char r_buf[TEMP_BUFFER_LEN];
338 if (access(WIFI_MAC_ADD_PATH, F_OK) != 0) {
339 DBG("WiFi driver is not loaded... ");
342 DBG("WiFi driver loaded... ");
345 snprintf(t_buf, TEMP_BUFFER_LEN,
346 "/sys/class/net/%s/queues/rx-0/rps_cpus",
348 DBG("Command : [%s]", t_buf);
351 while ((fd = open(t_buf, O_RDWR | O_CLOEXEC)) >= 0) {
353 count = read(fd, r_buf, 1);
360 DBG("read size = %zd", count);
363 if (r_buf[0] == 'e') {
365 DBG("e is already written");
366 snprintf(t_buf, TEMP_BUFFER_LEN,
367 "/sys/class/net/%s/queues/rx-%d/rps_cpus",
368 interface_name, curr);
369 DBG("Command : [%s]", t_buf);
375 if (lseek(fd, 0, SEEK_SET) < 0) {
381 count = write(fd, "e", 1);
388 DBG("write size = %zd", count);
392 snprintf(t_buf, TEMP_BUFFER_LEN,
393 "/sys/class/net/%s/queues/rx-%d/rps_cpus",
394 interface_name, curr);
395 DBG("Command : [%s]", t_buf);
401 int netconfig_wifi_firmware_get_mac(const char *interface_name, char **mac)
405 rv = hal_wifi_get_backend();
407 DBG("hal_wifi_get_backend() failed, ret: %d", rv);
411 rv = hal_wifi_get_mac(interface_name, mac);
413 DBG("hal_wifi_get_mac() failed, ret: %d", rv);
417 DBG("Successfully loaded default wifi mac address: %s", *mac);
421 int netconfig_wifi_firmware(enum netconfig_wifi_firmware type,
422 const char *interface_name, gboolean enable)
426 DBG("Wi-Fi firmware (type: %d %s) for %s", type, enable == TRUE ? "enable" : "disable",
427 interface_name ? interface_name : "");
429 GSList *found = __netconfig_is_wifi_driver_in_list(type, interface_name);
431 if (enable == FALSE) {
432 wifi_driver_s *driver;
437 driver = (wifi_driver_s *) found->data;
439 if (type != driver->type)
442 err = __netconfig_wifi_firmware_stop(type, interface_name);
443 if (err < 0 && err != -EALREADY)
446 __netconfig_remove_wifi_driver_from_list(type, interface_name);
452 wifi_driver_s *driver = (wifi_driver_s *) found->data;
454 DBG("Wi-Fi interface (%s) already in use", interface_name);
456 if (type == driver->type)
462 err = __netconfig_wifi_firmware_start(type, interface_name);
464 DBG("Failed to execute script file");
466 __netconfig_add_wifi_driver_to_list(type, interface_name);
468 if (__netconfig_set_rps_cpus(interface_name) < 0)
469 DBG("Failed to set rps_cpus");
474 gboolean handle_start(WifiFirmware *firmware, GDBusMethodInvocation *context,
475 const gchar *device, const gchar *ifname)
479 g_return_val_if_fail(firmware != NULL, TRUE);
481 DBG("Wi-Fi firmware start %s", device != NULL ? device : "null");
483 if (g_strcmp0("p2p", device) == 0)
484 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, ifname, TRUE);
485 else if (g_strcmp0("softap", device) == 0)
486 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, ifname, TRUE);
491 if (err == -EALREADY)
492 netconfig_error_already_exists(context);
493 else if (g_strcmp0("softap", device) == 0 &&
494 err == -EIO && netconfig_is_wifi_direct_on() == FALSE) {
495 if (netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, ifname, FALSE) == 0 &&
496 netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, ifname, TRUE) == 0) {
497 wifi_firmware_complete_start(firmware, context);
500 netconfig_error_wifi_driver_failed(context);
502 netconfig_error_wifi_driver_failed(context);
507 wifi_firmware_complete_start(firmware, context);
511 gboolean handle_stop(WifiFirmware *firmware, GDBusMethodInvocation *context,
512 const gchar *device, const gchar *ifname)
516 g_return_val_if_fail(firmware != NULL, TRUE);
518 DBG("Wi-Fi firmware stop %s", device != NULL ? device : "null");
520 if (g_strcmp0("p2p", device) == 0)
521 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, ifname, FALSE);
522 else if (g_strcmp0("softap", device) == 0)
523 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, ifname, FALSE);
528 if (err == -EALREADY)
529 netconfig_error_already_exists(context);
531 netconfig_error_wifi_driver_failed(context);
536 wifi_firmware_complete_stop(firmware, context);