blob: c077fb8a07a1c03afb9b4550f43e22c36290e168 [file] [log] [blame]
Raj Kushwaha41027212010-10-13 00:11:12 -07001/*
2 * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
3
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of Code Aurora Forum, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31#include <stdlib.h>
32#include <fcntl.h>
33#include <errno.h>
34#include <string.h>
35#include <stdio.h>
36
37#include <linux/if.h>
38#include <linux/wireless.h>
39#include <sys/ioctl.h>
40#include <sys/socket.h>
41#include <sys/stat.h>
42#include <sys/types.h>
43#include <sys/wait.h>
44#include <netinet/in.h>
45#include <arpa/inet.h>
46
47#define LOG_TAG "QCLDR-"
48
49#include "cutils/log.h"
50#include "cutils/memory.h"
51#include "cutils/misc.h"
52#include "cutils/properties.h"
53#include "private/android_filesystem_config.h"
54
55#include "qsap_api.h"
56#include "qsap.h"
57#include "libwpa_client/wpa_ctrl.h"
58
59#include <sys/system_properties.h>
60
61#ifndef WIFI_DRIVER_MODULE_PATH
62#define WIFI_DRIVER_MODULE_PATH "/system/lib/modules/wlan.ko"
63#endif
64
65#ifndef WIFI_DRIVER_MODULE_NAME
66#define WIFI_DRIVER_MODULE_NAME "wlan"
67#endif
68
Raj Kushwaha41027212010-10-13 00:11:12 -070069#ifdef WIFI_DRIVER_MODULE_ARG
70#undef WIFI_DRIVER_MODULE_ARG
71#endif
72
73#define WIFI_DRIVER_MODULE_ARG "con_mode=1"
74
Santosh Sajjan343ec712011-07-15 14:17:15 +053075/* WIFI_SDIO_IF_DRIVER_MODULE_NAME must be defined if sdioif driver required */
76#ifdef WIFI_SDIO_IF_DRIVER_MODULE_NAME
77
78#ifndef WIFI_SDIO_IF_DRIVER_MODULE_PATH
79#define WIFI_SDIO_IF_DRIVER_MODULE_PATH "/system/lib/modules/librasdioif.ko"
80#endif
81
82
83#ifndef WIFI_SDIO_IF_DRIVER_MODULE_ARG
84#define WIFI_SDIO_IF_DRIVER_MODULE_ARG ""
85#endif
86
87#endif
88
89
Raj Kushwaha41027212010-10-13 00:11:12 -070090extern int init_module(const char *name, u32, const s8 *);
91extern int delete_module(const char *name, int);
92
Raj Kushwahaae27f822010-11-14 17:47:11 -080093extern struct Command qsap_str[];
94
Raj Kushwaha41027212010-10-13 00:11:12 -070095static s32 check_driver_loaded( const s8 * tag)
96{
97 FILE *proc;
98 s8 line[126];
99
100 if ((proc = fopen("/proc/modules", "r")) == NULL) {
101 LOGW("Could not open %s: %s", "/proc/modules", strerror(errno));
102 return 0;
103 }
104
105 while ((fgets(line, sizeof(line), proc)) != NULL) {
106 if (strncmp(line, tag, strlen(tag)) == 0) {
107 fclose(proc);
108 return 1;
109 }
110 }
111
112 fclose(proc);
113
114 return 0;
115}
116
117static s32 insmod(const s8 *filename, const s8 *args, const s8 * tag)
118{
119#ifndef SDK_TEST
120 void *module;
121 s32 size;
122 s32 ret = 0;
123
124 if ( check_driver_loaded(tag) ) {
125 LOGE("Driver: %s already loaded\n", filename);
126 return ret;
127 }
128
129 LOGD("Loading Driver: %s %s\n", filename, args);
130
131 module = (void*)load_file(filename, (unsigned int*)&size);
132
133 if (!module) {
134 LOGE("Cannot load file: %s\n", filename);
135 return -1;
136 }
137
138 ret = init_module(module, size, args);
139
140 if ( ret ) {
141 LOGE("init_module (%s:%d) failed\n", filename, (int)size);
142 }
143
144 free(module);
145
146 return ret;
147#else
148 return 0;
149#endif
150}
151
152static s32 rmmod(const s8 *modname)
153{
154#ifndef SDK_TEST
155 s32 ret = 0;
156 s32 maxtry = 10;
157
158 while (maxtry-- > 0) {
159 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
160
161 if (ret < 0 && errno == EAGAIN){
162 usleep(50000);
163 } else {
164 break;
165 }
166 }
167
168 if (ret != 0) {
169 LOGD("Unable to unload driver module \"%s\": %s\n",
170 modname, strerror(errno));
171 }
172
173 return ret;
174#else
175 return 0;
176#endif
177}
178
179static const s8 SDIO_POLLING_ON[] = "/etc/init.qcom.sdio.sh 1";
180static const s8 SDIO_POLLING_OFF[] = "/etc/init.qcom.sdio.sh 0";
181
182s32 wifi_qsap_load_driver(void)
183{
184 s32 size;
185 s32 ret = 0;
186 s32 retry;
187
188 /* Unload the station mode driver first */
189 wifi_qsap_unload_wifi_sta_driver();
190
191 if (system(SDIO_POLLING_ON)) {
192 LOGE("Could not turn on the polling...");
193 }
194
Santosh Sajjan343ec712011-07-15 14:17:15 +0530195#ifdef WIFI_SDIO_IF_DRIVER_MODULE_NAME
196 ret = insmod(WIFI_SDIO_IF_DRIVER_MODULE_PATH, WIFI_SDIO_IF_DRIVER_MODULE_ARG, WIFI_SDIO_IF_DRIVER_MODULE_NAME " ");
Raj Kushwaha41027212010-10-13 00:11:12 -0700197
Santosh Sajjan343ec712011-07-15 14:17:15 +0530198 if ( ret != 0 ) {
199 LOGE("init_module failed sdioif\n");
200 ret = eERR_LOAD_FAILED_SDIOIF;
201 goto end;
Raj Kushwaha41027212010-10-13 00:11:12 -0700202 }
203
Santosh Sajjan343ec712011-07-15 14:17:15 +0530204 sched_yield();
205#endif
206
Raj Kushwaha41027212010-10-13 00:11:12 -0700207 ret = insmod(WIFI_DRIVER_MODULE_PATH, WIFI_DRIVER_MODULE_ARG, WIFI_DRIVER_MODULE_NAME " ");
208
209 if ( ret != 0 ) {
Santosh Sajjan343ec712011-07-15 14:17:15 +0530210#ifdef WIFI_SDIO_IF_DRIVER_MODULE_NAME
211 if ( check_driver_loaded(WIFI_SDIO_IF_DRIVER_MODULE_NAME " ") ) {
212 if ( rmmod(WIFI_SDIO_IF_DRIVER_MODULE_NAME) ) {
213 LOGE("Unable to unload the station mode librasdioif driver\n");
Raj Kushwaha41027212010-10-13 00:11:12 -0700214 }
215 }
Santosh Sajjan343ec712011-07-15 14:17:15 +0530216#endif
Raj Kushwaha41027212010-10-13 00:11:12 -0700217 LOGE("init_module failed libra_softap\n");
218 ret = eERR_LOAD_FAILED_SOFTAP;
219 goto end;
220 }
221
222 sched_yield();
223
224 ret = eSUCCESS;
225
226end:
227 if(system(SDIO_POLLING_OFF)) {
228 LOGE("Could not turn off the polling...");
229 }
230
231 return ret;
232}
233
Raj Kushwaha13eb75f2011-03-12 10:21:45 -0800234
235void qsap_send_module_down_indication(void)
236{
237 int s, ret;
238 struct iwreq wrq;
239
240 /*
241 * If the driver is loaded, ask it to broadcast a netlink message
242 * that it will be closing, so listeners can close their sockets.
243 *
244 */
245
246
247 /* Equivalent to: iwpriv wlan0 sendModuleInd */
248 if ((s = socket(PF_INET, SOCK_DGRAM, 0)) >= 0) {
249 strncpy(wrq.ifr_name, "wlan0", IFNAMSIZ);
250 wrq.u.data.length = 0; /* No Set arguments */
251 wrq.u.mode = 5; /* WE_MODULE_DOWN_IND sub-command */
252 ret = ioctl(s, (SIOCIWFIRSTPRIV + 1), &wrq);
Raj Kushwaha13eb75f2011-03-12 10:21:45 -0800253 if (ret < 0 ) {
Yunsen Wang26a9f6b2011-07-06 11:23:58 -0700254 strncpy(wrq.ifr_name, "softap.0", IFNAMSIZ);
255 ret = ioctl(s, (SIOCIWFIRSTPRIV + 1), &wrq);
256 if (ret < 0 ) {
257 LOGE("ioctl failed: %s", strerror(errno));
258 }
Raj Kushwaha13eb75f2011-03-12 10:21:45 -0800259 }
Yunsen Wang26a9f6b2011-07-06 11:23:58 -0700260 close(s);
Raj Kushwaha13eb75f2011-03-12 10:21:45 -0800261 sched_yield();
262 }
263 else {
264 LOGE("Socket open failed: %s", strerror(errno));
265 }
266}
267
Raj Kushwaha41027212010-10-13 00:11:12 -0700268s32 wifi_qsap_unload_wifi_sta_driver(void)
269{
270 s32 ret = 0;
271
272 if(system(SDIO_POLLING_ON)) {
273 LOGE("Could not turn on the polling...");
274 }
275
276 if ( check_driver_loaded(WIFI_DRIVER_MODULE_NAME " ") ) {
Raj Kushwaha13eb75f2011-03-12 10:21:45 -0800277 qsap_send_module_down_indication();
Raj Kushwaha41027212010-10-13 00:11:12 -0700278 if ( rmmod(WIFI_DRIVER_MODULE_NAME) ) {
279 LOGE("Unable to unload the station mode wifi driver...\n");
280 ret = 1;
281 goto end;
282 }
283 }
284
285 sched_yield();
286
Santosh Sajjan343ec712011-07-15 14:17:15 +0530287#ifdef WIFI_SDIO_IF_DRIVER_MODULE_NAME
Raj Kushwaha41027212010-10-13 00:11:12 -0700288 if ( check_driver_loaded(WIFI_SDIO_IF_DRIVER_MODULE_NAME " ") ) {
289 if ( rmmod(WIFI_SDIO_IF_DRIVER_MODULE_NAME) ) {
290 LOGE("Unable to unload the station mode librasdioif driver\n");
291 ret = 1;
292 goto end;
293 }
294 }
Santosh Sajjan343ec712011-07-15 14:17:15 +0530295#endif
Raj Kushwaha41027212010-10-13 00:11:12 -0700296
297end:
298 if(system(SDIO_POLLING_OFF)) {
299 LOGE("Could not turn off the polling...");
300 }
301 sched_yield();
302 return 0;
303}
304
305s32 wifi_qsap_unload_driver()
306{
307 s32 ret = eSUCCESS;
308
309 if(system(SDIO_POLLING_ON)) {
310 LOGE("Could not turn on the polling...");
311 }
312
313 if ( check_driver_loaded(WIFI_DRIVER_MODULE_NAME " ") ) {
Raj Kushwaha13eb75f2011-03-12 10:21:45 -0800314 qsap_send_module_down_indication();
Raj Kushwaha41027212010-10-13 00:11:12 -0700315 if ( rmmod(WIFI_DRIVER_MODULE_NAME) ) {
316 LOGE("Unable to unload the libra_softap driver\n");
317 ret = eERR_UNLOAD_FAILED_SOFTAP;
318 goto end;
319 }
320 }
321
322 sched_yield();
323
Santosh Sajjan343ec712011-07-15 14:17:15 +0530324#ifdef WIFI_SDIO_IF_DRIVER_MODULE_NAME
Raj Kushwaha41027212010-10-13 00:11:12 -0700325 if ( check_driver_loaded(WIFI_SDIO_IF_DRIVER_MODULE_NAME " ") ) {
326 if ( rmmod(WIFI_SDIO_IF_DRIVER_MODULE_NAME) ) {
327 LOGE("Unable to unload the librasdioif driver\n");
328 ret = eERR_UNLOAD_FAILED_SDIO;
329 goto end;
330 }
331 }
Santosh Sajjan343ec712011-07-15 14:17:15 +0530332#endif
333
Raj Kushwaha41027212010-10-13 00:11:12 -0700334end:
335 if(system(SDIO_POLLING_OFF)) {
336 LOGE("Could not turn off the polling...");
337 }
338
339 return ret;
340}
341
342s32 wifi_qsap_stop_bss(void)
343{
344#define QCIEEE80211_IOCTL_STOPBSS (SIOCIWFIRSTPRIV + 6)
345 s32 sock;
346 s32 ret = eERR_STOP_BSS;
347 s8 cmd[] = "stopbss";
348 s8 interface[128];
349 s8 *iface;
350 s32 len = 128;
351 struct iwreq wrq;
352 struct iw_priv_args *priv_ptr;
353
354 if(ENABLE != is_softap_enabled()) {
355 ret = eERR_BSS_NOT_STARTED;
356 return ret;
357 }
358
Raj Kushwahaae27f822010-11-14 17:47:11 -0800359 if(NULL == (iface = qsap_get_config_value(CONFIG_FILE, &qsap_str[STR_INTERFACE], interface, (u32*)&len))) {
Raj Kushwaha41027212010-10-13 00:11:12 -0700360 LOGE("%s :interface error \n", __func__);
361 return ret;
362 }
363
364 /* Issue the stopbss command to driver */
365 sock = socket(AF_INET, SOCK_DGRAM, 0);
366
367 if (sock < 0) {
368 LOGE("Failed to open socket");
369 return eERR_STOP_BSS;
370 }
371
372 strncpy(wrq.ifr_name, iface, sizeof(wrq.ifr_name));
373 wrq.u.data.length = sizeof(cmd);
374 wrq.u.data.pointer = cmd;
375 wrq.u.data.flags = 0;
376
377 ret = ioctl(sock, QCIEEE80211_IOCTL_STOPBSS, &wrq);
378
379 /* Here IOCTL is always returning non Zero: temporary fix untill driver is fixed*/
380 ret = 0;
381 close(sock);
382
383 if (ret) {
384 LOGE("IOCTL stopbss failed: %ld", ret);
385 ret = eERR_STOP_BSS;
386 } else {
387 LOGD("STOP BSS ISSUED");
388 ret = eSUCCESS;
389 }
390
391 sched_yield();
392 return ret;
393}
394
395s32 is_softap_enabled(void)
396{
397 s8 stat[32] = {0};
398
399 if ( property_get("init.svc.hostapd", stat, NULL) &&
400 (strcmp(stat, "running") == 0)) {
401 LOGD("HOSTAPD enabled \n");
402 return ENABLE;
403 }
404
405 LOGD("HOSTAPD disabled \n");
406 return DISABLE;
407}
408
409s32 commit(void)
410{
411#ifndef SDK_TEST
412 s32 ret = eERR_COMMIT;
413
414 if ( is_softap_enabled() ) {
415 /** Stop BSS */
416 if(eSUCCESS != (ret = wifi_qsap_stop_bss())) {
417 LOGE("%s: stop bss failed \n", __func__);
418 return ret;
419 }
420 sleep(1);
421 }
422
423 ret = wifi_qsap_start_softap();
424
425 if( eSUCCESS != ret )
426 wifi_qsap_unload_driver();
427
428 return ret;
429#else
430 return eSUCCESS;
431#endif
432}
433
434s32 wifi_qsap_start_softap()
435{
436 s32 retry = 4;
Raj Kushwahaae27f822010-11-14 17:47:11 -0800437 FILE * fp;
Raj Kushwaha41027212010-10-13 00:11:12 -0700438
439 LOGD("Starting Soft AP...\n");
440
Raj Kushwahaae27f822010-11-14 17:47:11 -0800441 /* Check if configuration files are present, if not create the default files */
442 check_for_configuration_files();
443
Raj Kushwaha41027212010-10-13 00:11:12 -0700444 /* Delete control interface if it was left over because of previous crash */
445 if ( !is_softap_enabled() ) {
446 qsap_del_ctrl_iface();
447 }
448
Raj Kushwahaae27f822010-11-14 17:47:11 -0800449 while(retry--) {
450 /* May be the configuration file is corrupted or not available, */
451 /* copy the default configuration file */
452 if ( retry == 1 )
453 wifi_qsap_reset_to_default(CONFIG_FILE, DEFAULT_CONFIG_FILE_PATH);
454
Raj Kushwaha41027212010-10-13 00:11:12 -0700455 /** Stop hostapd */
456 if(0 != property_set("ctl.start", "hostapd")) {
457 LOGE("failed \n");
458 continue;
459 }
460
461 sleep(1);
462
463 if ( is_softap_enabled() ) {
464 LOGD("success \n");
465 return eSUCCESS;
466 }
467 }
468
469 LOGE("Unable to start the SoftAP\n");
470 return eERR_START_SAP;
471}
472
473s32 wifi_qsap_stop_softap()
474{
475 s32 ret = eSUCCESS;
476
477 if ( is_softap_enabled() ) {
478 LOGD("Stopping BSS ..... ");
479
480 /** Stop the BSS */
481 if (eSUCCESS != (ret = wifi_qsap_stop_bss()) ) {
482 LOGE("failed \n");
483 return ret;
484 }
485 sleep(1);
486 }
487
488 return ret;
489}
490
491s32 wifi_qsap_reload_softap()
492{
493 s32 ret = eERR_RELOAD_SAP;
494
495 /** SDK API to reload the firmware */
496 if (eSUCCESS != (ret = wifi_qsap_stop_softap())) {
497 return ret;
498 }
499
500 if (eSUCCESS != (ret = wifi_qsap_unload_driver())) {
501 return ret;
502 }
503
504 usleep(500000);
505
506 if (eSUCCESS != (ret = wifi_qsap_load_driver())) {
507 return ret;
508 }
509
510 sleep(1);
511
512 if (eSUCCESS != (ret = wifi_qsap_start_softap())) {
513 wifi_qsap_unload_driver();
514 return ret;
515 }
516
517 return eSUCCESS;
518}