49b238a05a88a9e85c56c7a6d971e208afbec207
[pandora-kernel.git] / drivers / staging / wilc1000 / linux_wlan.c
1 #ifndef SIMULATION
2 #include "wilc_wfi_cfgoperations.h"
3 #include "linux_wlan_common.h"
4 #include "wilc_wlan_if.h"
5 #include "wilc_wlan.h"
6 #ifdef USE_WIRELESS
7 #include "wilc_wfi_cfgoperations.h"
8 #endif
9
10 #include "linux_wlan_common.h"
11
12 #include <linux/slab.h>
13 #include <linux/sched.h>
14 #include <linux/delay.h>
15 #include <linux/workqueue.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <asm/gpio.h>
19
20 #include <linux/kthread.h>
21 #include <linux/firmware.h>
22 #include <linux/delay.h>
23
24 #include <linux/init.h>
25 #include <linux/netdevice.h>
26 #ifdef DISABLE_PWRSAVE_AND_SCAN_DURING_IP
27 #include <linux/inetdevice.h>
28 #endif
29 #include <linux/etherdevice.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/skbuff.h>
33
34 #include <linux/version.h>
35 #include <linux/semaphore.h>
36
37 #ifdef WILC_SDIO
38 #include "linux_wlan_sdio.h"
39 #else
40 #include "linux_wlan_spi.h"
41 #endif
42
43 #ifdef WILC_FULLY_HOSTING_AP
44 #include "wilc_host_ap.h"
45 #endif
46
47 #ifdef STATIC_MACADDRESS /* brandy_0724 [[ */
48 #include <linux/vmalloc.h>
49 #include <linux/fs.h>
50 struct task_struct *wilc_mac_thread;
51 unsigned char mac_add[] = {0x00, 0x80, 0xC2, 0x5E, 0xa2, 0xb2};
52 #endif /* brandy_0724 ]] */
53
54 #if defined(CUSTOMER_PLATFORM)
55 /*
56  TODO : Write power control functions as customer platform.
57  */
58 #else
59
60  #define _linux_wlan_device_power_on()          {}
61  #define _linux_wlan_device_power_off()         {}
62
63  #define _linux_wlan_device_detection()         {}
64  #define _linux_wlan_device_removal()           {}
65 #endif
66
67 #ifdef DISABLE_PWRSAVE_AND_SCAN_DURING_IP
68 extern WILC_Bool g_obtainingIP;
69 #endif
70 extern WILC_Uint16 Set_machw_change_vir_if(WILC_Bool bValue);
71 extern void resolve_disconnect_aberration(void *drvHandler);
72 extern WILC_Uint8 gau8MulticastMacAddrList[WILC_MULTICAST_TABLE_SIZE][ETH_ALEN];
73 void wilc1000_wlan_deinit(linux_wlan_t *nic);
74 #ifdef DISABLE_PWRSAVE_AND_SCAN_DURING_IP
75 extern WILC_TimerHandle hDuringIpTimer;
76 #endif
77
78 static int linux_wlan_device_power(int on_off)
79 {
80         PRINT_D(INIT_DBG, "linux_wlan_device_power.. (%d)\n", on_off);
81
82         if (on_off) {
83                 _linux_wlan_device_power_on();
84         } else {
85                 _linux_wlan_device_power_off();
86         }
87
88         return 0;
89 }
90
91 static int linux_wlan_device_detection(int on_off)
92 {
93         PRINT_D(INIT_DBG, "linux_wlan_device_detection.. (%d)\n", on_off);
94
95 #ifdef WILC_SDIO
96         if (on_off) {
97                 _linux_wlan_device_detection();
98         } else {
99                 _linux_wlan_device_removal();
100         }
101 #endif
102
103         return 0;
104 }
105
106
107 #ifdef DISABLE_PWRSAVE_AND_SCAN_DURING_IP
108 static int dev_state_ev_handler(struct notifier_block *this, unsigned long event, void *ptr);
109
110 static struct notifier_block g_dev_notifier = {
111         .notifier_call = dev_state_ev_handler
112 };
113 #endif
114
115 #define wilc_wlan_deinit(nic)   { if (&g_linux_wlan->oup != NULL)        \
116                 if (g_linux_wlan->oup.wlan_cleanup != NULL) \
117                         g_linux_wlan->oup.wlan_cleanup(); }
118
119
120 #ifndef STA_FIRMWARE
121 #define STA_FIRMWARE    "wifi_firmware.bin"
122 #endif
123
124 #ifndef AP_FIRMWARE
125 #define AP_FIRMWARE             "wifi_firmware_ap.bin"
126 #endif
127
128 #ifndef P2P_CONCURRENCY_FIRMWARE
129 #define P2P_CONCURRENCY_FIRMWARE        "wifi_firmware_p2p_concurrency.bin"
130 #endif
131
132
133
134 typedef struct android_wifi_priv_cmd {
135         char *buf;
136         int used_len;
137         int total_len;
138 } android_wifi_priv_cmd;
139
140
141 #define IRQ_WAIT        1
142 #define IRQ_NO_WAIT     0
143 /*
144  *      to sync between mac_close and module exit.
145  *      don't initialize or de-initialize from init/deinitlocks
146  *      to be initialized from module wilc_netdev_init and
147  *      deinitialized from mdoule_exit
148  */
149 static struct semaphore close_exit_sync;
150 unsigned int int_rcvdU;
151 unsigned int int_rcvdB;
152 unsigned int int_clrd;
153
154 static int wlan_deinit_locks(linux_wlan_t *nic);
155 static void wlan_deinitialize_threads(linux_wlan_t *nic);
156 static void linux_wlan_lock(void *vp);
157 void linux_wlan_unlock(void *vp);
158 extern void WILC_WFI_monitor_rx(uint8_t *buff, uint32_t size);
159 extern void WILC_WFI_p2p_rx(struct net_device *dev, uint8_t *buff, uint32_t size);
160
161
162 static void *internal_alloc(uint32_t size, uint32_t flag);
163 static void linux_wlan_tx_complete(void *priv, int status);
164 void frmw_to_linux(uint8_t *buff, uint32_t size, uint32_t pkt_offset);
165 static int  mac_init_fn(struct net_device *ndev);
166 int  mac_xmit(struct sk_buff *skb, struct net_device *dev);
167 int  mac_open(struct net_device *ndev);
168 int  mac_close(struct net_device *ndev);
169 static struct net_device_stats *mac_stats(struct net_device *dev);
170 static int  mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd);
171 static void wilc_set_multicast_list(struct net_device *dev);
172
173
174
175 /*
176  * for now - in frmw_to_linux there should be private data to be passed to it
177  * and this data should be pointer to net device
178  */
179 linux_wlan_t *g_linux_wlan;
180 wilc_wlan_oup_t *gpstrWlanOps;
181 WILC_Bool bEnablePS = WILC_TRUE;
182
183 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
184 static const struct net_device_ops wilc_netdev_ops = {
185         .ndo_init = mac_init_fn,
186         .ndo_open = mac_open,
187         .ndo_stop = mac_close,
188         .ndo_start_xmit = mac_xmit,
189         .ndo_do_ioctl = mac_ioctl,
190         .ndo_get_stats = mac_stats,
191         .ndo_set_rx_mode  = wilc_set_multicast_list,
192
193 };
194 #define wilc_set_netdev_ops(ndev) do { (ndev)->netdev_ops = &wilc_netdev_ops; } while (0)
195 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
196
197 static const struct net_device_ops wilc_netdev_ops = {
198         .ndo_init = mac_init_fn,
199         .ndo_open = mac_open,
200         .ndo_stop = mac_close,
201         .ndo_start_xmit = mac_xmit,
202         .ndo_do_ioctl = mac_ioctl,
203         .ndo_get_stats = mac_stats,
204         .ndo_set_multicast_list = wilc_set_multicast_list,
205
206 };
207
208 #define wilc_set_netdev_ops(ndev) do { (ndev)->netdev_ops = &wilc_netdev_ops; } while (0)
209
210 #else
211
212 static void wilc_set_netdev_ops(struct net_device *ndev)
213 {
214
215         ndev->init = mac_init_fn;
216         ndev->open = mac_open;
217         ndev->stop = mac_close;
218         ndev->hard_start_xmit = mac_xmit;
219         ndev->do_ioctl = mac_ioctl;
220         ndev->get_stats = mac_stats;
221         ndev->set_multicast_list = wilc_set_multicast_list,
222 }
223
224 #endif
225 #ifdef DEBUG_MODE
226
227 extern volatile int timeNo;
228
229 #define DEGUG_BUFFER_LENGTH 1000
230 volatile int WatchDogdebuggerCounter;
231 char DebugBuffer[DEGUG_BUFFER_LENGTH + 20] = {0};
232 static char *ps8current = DebugBuffer;
233
234
235
236 void printk_later(const char *format, ...)
237 {
238         va_list args;
239         va_start (args, format);
240         ps8current += vsprintf (ps8current, format, args);
241         va_end (args);
242         if ((ps8current - DebugBuffer) > DEGUG_BUFFER_LENGTH) {
243                 ps8current = DebugBuffer;
244         }
245
246 }
247
248
249 void dump_logs()
250 {
251         if (DebugBuffer[0]) {
252                 DebugBuffer[DEGUG_BUFFER_LENGTH] = 0;
253                 PRINT_INFO(GENERIC_DBG, "early printed\n");
254                 PRINT_D(GENERIC_DBG, ps8current + 1);
255                 ps8current[1] = 0;
256                 PRINT_INFO(GENERIC_DBG, "latest printed\n");
257                 PRINT_D(GENERIC_DBG, DebugBuffer);
258                 DebugBuffer[0] = 0;
259                 ps8current = DebugBuffer;
260         }
261 }
262
263 void Reset_WatchDogdebugger()
264 {
265         WatchDogdebuggerCounter = 0;
266 }
267
268 static int DebuggingThreadTask(void *vp)
269 {
270         while (1) {
271                 while (!WatchDogdebuggerCounter) {
272                         PRINT_D(GENERIC_DBG, "Debug Thread Running %d\n", timeNo);
273                         WatchDogdebuggerCounter = 1;
274                         msleep(10000);
275                 }
276                 dump_logs();
277                 WatchDogdebuggerCounter = 0;
278         }
279 }
280
281
282 #endif
283
284
285 #ifdef DISABLE_PWRSAVE_AND_SCAN_DURING_IP
286 static int dev_state_ev_handler(struct notifier_block *this, unsigned long event, void *ptr)
287 {
288         struct in_ifaddr *dev_iface = (struct in_ifaddr *)ptr;
289         struct WILC_WFI_priv *priv;
290         tstrWILC_WFIDrv *pstrWFIDrv;
291         struct net_device *dev;
292         WILC_Uint8 *pIP_Add_buff;
293         WILC_Sint32 s32status = WILC_FAIL;
294         perInterface_wlan_t *nic;
295         WILC_Uint8 null_ip[4] = {0};
296         char wlan_dev_name[5] = "wlan0";
297
298         if (dev_iface == NULL || dev_iface->ifa_dev == NULL || dev_iface->ifa_dev->dev == NULL) {
299                 PRINT_D(GENERIC_DBG, "dev_iface = NULL\n");
300                 return NOTIFY_DONE;
301         }
302
303         if ((memcmp(dev_iface->ifa_label, "wlan0", 5)) && (memcmp(dev_iface->ifa_label, "p2p0", 4))) {
304                 PRINT_D(GENERIC_DBG, "Interface is neither WLAN0 nor P2P0\n");
305                 return NOTIFY_DONE;
306         }
307
308         dev  = (struct net_device *)dev_iface->ifa_dev->dev;
309         if (dev->ieee80211_ptr == NULL || dev->ieee80211_ptr->wiphy == NULL) {
310                 PRINT_D(GENERIC_DBG, "No Wireless registerd\n");
311                 return NOTIFY_DONE;
312         }
313         priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
314         if (priv == NULL) {
315                 PRINT_D(GENERIC_DBG, "No Wireless Priv\n");
316                 return NOTIFY_DONE;
317         }
318         pstrWFIDrv = (tstrWILC_WFIDrv *)priv->hWILCWFIDrv;
319         nic = netdev_priv(dev);
320         if (nic == NULL || pstrWFIDrv == NULL) {
321                 PRINT_D(GENERIC_DBG, "No Wireless Priv\n");
322                 return NOTIFY_DONE;
323         }
324
325         PRINT_INFO(GENERIC_DBG, "dev_state_ev_handler +++\n"); /* tony */
326
327         switch (event) {
328         case NETDEV_UP:
329                 PRINT_D(GENERIC_DBG, "dev_state_ev_handler event=NETDEV_UP %p\n", dev);       /* tony */
330
331                 PRINT_INFO(GENERIC_DBG, "\n ============== IP Address Obtained ===============\n\n");
332
333
334                 /*If we are in station mode or client mode*/
335                 if (nic->iftype == STATION_MODE || nic->iftype == CLIENT_MODE) {
336                         pstrWFIDrv->IFC_UP = 1;
337                         g_obtainingIP = WILC_FALSE;
338                         WILC_TimerStop(&hDuringIpTimer, WILC_NULL);
339                         PRINT_D(GENERIC_DBG, "IP obtained , enable scan\n");
340                 }
341
342
343
344                 if (bEnablePS    == WILC_TRUE)
345                         host_int_set_power_mgmt((WILC_WFIDrvHandle)pstrWFIDrv, 1, 0);
346
347                 PRINT_D(GENERIC_DBG, "[%s] Up IP\n", dev_iface->ifa_label);
348
349                 pIP_Add_buff = (char *) (&(dev_iface->ifa_address));
350                 PRINT_D(GENERIC_DBG, "IP add=%d:%d:%d:%d \n", pIP_Add_buff[0], pIP_Add_buff[1], pIP_Add_buff[2], pIP_Add_buff[3]);
351                 s32status = host_int_setup_ipaddress((WILC_WFIDrvHandle)pstrWFIDrv, pIP_Add_buff, nic->u8IfIdx);
352
353                 break;
354
355         case NETDEV_DOWN:
356                 PRINT_D(GENERIC_DBG, "dev_state_ev_handler event=NETDEV_DOWN %p\n", dev);               /* tony */
357
358                 PRINT_INFO(GENERIC_DBG, "\n ============== IP Address Released ===============\n\n");
359                 if (nic->iftype == STATION_MODE || nic->iftype == CLIENT_MODE) {
360                         pstrWFIDrv->IFC_UP = 0;
361                         g_obtainingIP = WILC_FALSE;
362                 }
363
364                 if (memcmp(dev_iface->ifa_label, wlan_dev_name, 5) == 0)
365                         host_int_set_power_mgmt((WILC_WFIDrvHandle)pstrWFIDrv, 0, 0);
366
367                 resolve_disconnect_aberration(pstrWFIDrv);
368
369
370                 PRINT_D(GENERIC_DBG, "[%s] Down IP\n", dev_iface->ifa_label);
371
372                 pIP_Add_buff = null_ip;
373                 PRINT_D(GENERIC_DBG, "IP add=%d:%d:%d:%d \n", pIP_Add_buff[0], pIP_Add_buff[1], pIP_Add_buff[2], pIP_Add_buff[3]);
374
375                 s32status = host_int_setup_ipaddress((WILC_WFIDrvHandle)pstrWFIDrv, pIP_Add_buff, nic->u8IfIdx);
376
377                 break;
378
379         default:
380                 PRINT_INFO(GENERIC_DBG, "dev_state_ev_handler event=default\n");        /* tony */
381                 PRINT_INFO(GENERIC_DBG, "[%s] unknown dev event: %lu\n", dev_iface->ifa_label, event);
382
383                 break;
384         }
385
386         return NOTIFY_DONE;
387
388 }
389 #endif
390
391 /*
392  *      Interrupt initialization and handling functions
393  */
394
395 void linux_wlan_enable_irq(void)
396 {
397
398 #if (RX_BH_TYPE != RX_BH_THREADED_IRQ)
399 #if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
400         PRINT_D(INT_DBG, "Enabling IRQ ...\n");
401         enable_irq(g_linux_wlan->dev_irq_num);
402 #endif
403 #endif
404 }
405
406 void linux_wlan_disable_irq(int wait)
407 {
408 #if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
409         if (wait) {
410                 PRINT_D(INT_DBG, "Disabling IRQ ...\n");
411                 disable_irq(g_linux_wlan->dev_irq_num);
412         } else {
413                 PRINT_D(INT_DBG, "Disabling IRQ ...\n");
414                 disable_irq_nosync(g_linux_wlan->dev_irq_num);
415         }
416 #endif
417 }
418
419 #if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
420 static irqreturn_t isr_uh_routine(int irq, void *user_data)
421 {
422
423
424         int_rcvdU++;
425 #if (RX_BH_TYPE != RX_BH_THREADED_IRQ)
426         linux_wlan_disable_irq(IRQ_NO_WAIT);
427 #endif
428         PRINT_D(INT_DBG, "Interrupt received UH\n");
429
430         /*While mac is closing cacncel the handling of any interrupts received*/
431         if (g_linux_wlan->close) {
432                 PRINT_ER("Driver is CLOSING: Can't handle UH interrupt\n");
433         #if (RX_BH_TYPE == RX_BH_THREADED_IRQ)
434                 return IRQ_HANDLED;
435         #else
436                 return IRQ_NONE;
437         #endif
438
439         }
440 #if (RX_BH_TYPE == RX_BH_WORK_QUEUE)
441         schedule_work(&g_linux_wlan->rx_work_queue);
442         return IRQ_HANDLED;
443 #elif (RX_BH_TYPE == RX_BH_KTHREAD)
444         linux_wlan_unlock(&g_linux_wlan->rx_sem);
445         return IRQ_HANDLED;
446 #elif (RX_BH_TYPE == RX_BH_THREADED_IRQ)
447         return IRQ_WAKE_THREAD;
448 #endif
449
450 }
451 #endif
452
453 #if (RX_BH_TYPE == RX_BH_WORK_QUEUE || RX_BH_TYPE == RX_BH_THREADED_IRQ)
454
455 #if (RX_BH_TYPE == RX_BH_THREADED_IRQ)
456 irqreturn_t isr_bh_routine(int irq, void *userdata)
457 {
458         linux_wlan_t *nic;
459         nic = (linux_wlan_t *)userdata;
460 #else
461 static void isr_bh_routine(struct work_struct *work)
462 {
463         perInterface_wlan_t *nic;
464         nic = (perInterface_wlan_t *)container_of(work, linux_wlan_t, rx_work_queue);
465 #endif
466
467         /*While mac is closing cacncel the handling of any interrupts received*/
468         if (g_linux_wlan->close) {
469                 PRINT_ER("Driver is CLOSING: Can't handle BH interrupt\n");
470         #if (RX_BH_TYPE == RX_BH_THREADED_IRQ)
471                 return IRQ_HANDLED;
472         #else
473                 return;
474         #endif
475
476
477
478         }
479
480         int_rcvdB++;
481         PRINT_D(INT_DBG, "Interrupt received BH\n");
482         if (g_linux_wlan->oup.wlan_handle_rx_isr != 0) {
483                 g_linux_wlan->oup.wlan_handle_rx_isr();
484         } else {
485                 PRINT_ER("wlan_handle_rx_isr() hasn't been initialized\n");
486         }
487
488
489 #if (RX_BH_TYPE == RX_BH_THREADED_IRQ)
490         return IRQ_HANDLED;
491 #endif
492 }
493 #elif (RX_BH_TYPE == RX_BH_KTHREAD)
494 static int isr_bh_routine(void *vp)
495 {
496         linux_wlan_t *nic;
497
498         nic = (linux_wlan_t *)vp;
499
500         while (1) {
501                 linux_wlan_lock(&nic->rx_sem);
502                 if (g_linux_wlan->close) {
503
504                         while (!kthread_should_stop())
505                                 schedule();
506
507                         break;
508                 }
509                 int_rcvdB++;
510                 PRINT_D(INT_DBG, "Interrupt received BH\n");
511                 if (g_linux_wlan->oup.wlan_handle_rx_isr != 0) {
512                         g_linux_wlan->oup.wlan_handle_rx_isr();
513                 } else {
514                         PRINT_ER("wlan_handle_rx_isr() hasn't been initialized\n");
515                 }
516         }
517
518         return 0;
519 }
520 #endif
521
522
523 #if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
524 static int init_irq(linux_wlan_t *p_nic)
525 {
526         int ret = 0;
527         linux_wlan_t *nic = p_nic;
528
529         /*initialize GPIO and register IRQ num*/
530         /*GPIO request*/
531         if ((gpio_request(GPIO_NUM, "WILC_INTR") == 0) &&
532             (gpio_direction_input(GPIO_NUM) == 0)) {
533 #if defined(CUSTOMER_PLATFORM)
534 /*
535  TODO : save the registerd irq number to the private wilc context in kernel.
536  *
537  * ex) nic->dev_irq_num = gpio_to_irq(GPIO_NUM);
538  */
539 #elif defined (NM73131_0_BOARD)
540                 nic->dev_irq_num = IRQ_WILC1000;
541 #elif defined (PANDA_BOARD)
542                 gpio_export(GPIO_NUM, 1);
543                 nic->dev_irq_num = OMAP_GPIO_IRQ(GPIO_NUM);
544                 irq_set_irq_type(nic->dev_irq_num, IRQ_TYPE_LEVEL_LOW);
545 #else
546                 nic->dev_irq_num = gpio_to_irq(GPIO_NUM);
547 #endif
548         } else {
549                 ret = -1;
550                 PRINT_ER("could not obtain gpio for WILC_INTR\n");
551         }
552
553
554 #if (RX_BH_TYPE == RX_BH_THREADED_IRQ)
555         if ((ret != -1) && (request_threaded_irq(nic->dev_irq_num, isr_uh_routine, isr_bh_routine,
556                                                   IRQF_TRIGGER_LOW | IRQF_ONESHOT,               /*Without IRQF_ONESHOT the uh will remain kicked in and dont gave a chance to bh*/
557                                                   "WILC_IRQ", nic)) < 0) {
558
559 #else
560         /*Request IRQ*/
561         if ((ret != -1) && (request_irq(nic->dev_irq_num, isr_uh_routine,
562                                         IRQF_TRIGGER_LOW, "WILC_IRQ", nic) < 0)) {
563
564 #endif
565                 PRINT_ER("Failed to request IRQ for GPIO: %d\n", GPIO_NUM);
566                 ret = -1;
567         } else {
568
569                 PRINT_D(INIT_DBG, "IRQ request succeeded IRQ-NUM= %d on GPIO: %d\n",
570                         nic->dev_irq_num, GPIO_NUM);
571         }
572
573         return ret;
574 }
575 #endif
576
577 static void deinit_irq(linux_wlan_t *nic)
578 {
579 #if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
580         /* Deintialize IRQ */
581         if (&nic->dev_irq_num != 0) {
582                 free_irq(nic->dev_irq_num, g_linux_wlan);
583
584                 gpio_free(GPIO_NUM);
585         }
586 #endif
587 }
588
589
590 /*
591  *      OS functions
592  */
593 static void linux_wlan_msleep(uint32_t msc)
594 {
595         if (msc <= 4000000) {
596                 WILC_Uint32 u32Temp = msc * 1000;
597 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35)
598                 usleep_range(u32Temp, u32Temp);
599 #else
600                 /* This is delay not sleep !!!, has to be changed*/
601                 msleep(msc);
602 #endif
603         } else {
604                 msleep(msc);
605         }
606 }
607
608 static void linux_wlan_atomic_msleep(uint32_t msc)
609 {
610         mdelay(msc);
611 }
612 static void linux_wlan_dbg(uint8_t *buff)
613 {
614         PRINT_D(INIT_DBG, "%d\n", *buff);
615 }
616
617 static void *linux_wlan_malloc_atomic(uint32_t sz)
618 {
619         char *pntr = NULL;
620         pntr = (char *)kmalloc(sz, GFP_ATOMIC);
621         PRINT_D(MEM_DBG, "Allocating %d bytes at address %p\n", sz, pntr);
622         return (void *)pntr;
623
624 }
625 static void *linux_wlan_malloc(uint32_t sz)
626 {
627         char *pntr = NULL;
628         pntr = (char *)kmalloc(sz, GFP_KERNEL);
629         PRINT_D(MEM_DBG, "Allocating %d bytes at address %p\n", sz, pntr);
630         return (void *)pntr;
631 }
632
633 void linux_wlan_free(void *vp)
634 {
635         if (vp != NULL) {
636                 PRINT_D(MEM_DBG, "Freeing %p\n", vp);
637                 kfree(vp);
638         }
639 }
640
641
642 static void *internal_alloc(uint32_t size, uint32_t flag)
643 {
644         char *pntr = NULL;
645         pntr = (char *)kmalloc(size, flag);
646         PRINT_D(MEM_DBG, "Allocating %d bytes at address %p\n", size, pntr);
647         return (void *)pntr;
648 }
649
650
651 static void linux_wlan_init_lock(char *lockName, void *plock, int count)
652 {
653         sema_init((struct semaphore *)plock, count);
654         PRINT_D(LOCK_DBG, "Initializing [%s][%p]\n", lockName, plock);
655
656 }
657
658 static void linux_wlan_deinit_lock(void *plock)
659 {
660         /* mutex_destroy((struct mutex*)plock); */
661 }
662
663 static void linux_wlan_lock(void *vp)
664 {
665         PRINT_D(LOCK_DBG, "Locking %p\n", vp);
666         if (vp != NULL) {
667                 while (down_interruptible((struct semaphore *) vp))
668                         ;
669         } else {
670                 PRINT_ER("Failed, mutex is NULL\n");
671         }
672 }
673
674 static int linux_wlan_lock_timeout(void *vp, WILC_Uint32 timeout)
675 {
676         int error = -1;
677         PRINT_D(LOCK_DBG, "Locking %p\n", vp);
678         if (vp != NULL) {
679                 error = down_timeout((struct semaphore *)vp, msecs_to_jiffies(timeout));
680         } else {
681                 PRINT_ER("Failed, mutex is NULL\n");
682         }
683         return error;
684 }
685
686 void linux_wlan_unlock(void *vp)
687 {
688         PRINT_D(LOCK_DBG, "Unlocking %p\n", vp);
689         if (vp != NULL) {
690                 up((struct semaphore *)vp);
691         } else {
692                 PRINT_ER("Failed, mutex is NULL\n");
693         }
694 }
695
696
697 static void linux_wlan_init_mutex(char *lockName, void *plock, int count)
698 {
699         mutex_init((struct mutex *)plock);
700         PRINT_D(LOCK_DBG, "Initializing mutex [%s][%p]\n", lockName, plock);
701
702 }
703
704 static void linux_wlan_deinit_mutex(void *plock)
705 {
706         mutex_destroy((struct mutex *)plock);
707 }
708
709 static void linux_wlan_lock_mutex(void *vp)
710 {
711         PRINT_D(LOCK_DBG, "Locking mutex %p\n", vp);
712         if (vp != NULL) {
713                 /*
714                  *      if(mutex_is_locked((struct mutex*)vp))
715                  *      {
716                  *              //PRINT_ER("Mutex already locked - %p \n",vp);
717                  *      }
718                  */
719                 mutex_lock((struct mutex *)vp);
720
721         } else {
722                 PRINT_ER("Failed, mutex is NULL\n");
723         }
724 }
725
726 static void linux_wlan_unlock_mutex(void *vp)
727 {
728         PRINT_D(LOCK_DBG, "Unlocking mutex %p\n", vp);
729         if (vp != NULL) {
730
731                 if (mutex_is_locked((struct mutex *)vp)) {
732                         mutex_unlock((struct mutex *)vp);
733                 } else {
734                         /* PRINT_ER("Mutex already unlocked  - %p\n",vp); */
735                 }
736
737         } else {
738                 PRINT_ER("Failed, mutex is NULL\n");
739         }
740 }
741
742
743 /*Added by Amr - BugID_4720*/
744 static void linux_wlan_init_spin_lock(char *lockName, void *plock, int count)
745 {
746         spin_lock_init((spinlock_t *)plock);
747         PRINT_D(SPIN_DEBUG, "Initializing mutex [%s][%p]\n", lockName, plock);
748
749 }
750
751 static void linux_wlan_deinit_spin_lock(void *plock)
752 {
753
754 }
755 static void linux_wlan_spin_lock(void *vp, unsigned long *flags)
756 {
757         unsigned long lflags;
758         PRINT_D(SPIN_DEBUG, "Lock spin %p\n", vp);
759         if (vp != NULL) {
760                 spin_lock_irqsave((spinlock_t *)vp, lflags);
761                 *flags = lflags;
762         } else {
763                 PRINT_ER("Failed, spin lock is NULL\n");
764         }
765 }
766 static void linux_wlan_spin_unlock(void *vp, unsigned long *flags)
767 {
768         unsigned long lflags = *flags;
769         PRINT_D(SPIN_DEBUG, "Unlock spin %p\n", vp);
770         if (vp != NULL) {
771                 spin_unlock_irqrestore((spinlock_t *)vp, lflags);
772                 *flags = lflags;
773         } else {
774                 PRINT_ER("Failed, spin lock is NULL\n");
775         }
776 }
777
778 static void linux_wlan_mac_indicate(int flag)
779 {
780         /*I have to do it that way becuase there is no mean to encapsulate device pointer
781          * as a parameter
782          */
783         linux_wlan_t *pd = g_linux_wlan;
784         int status;
785
786         if (flag == WILC_MAC_INDICATE_STATUS) {
787                 pd->oup.wlan_cfg_get_value(WID_STATUS, (unsigned char *)&status, 4);
788                 if (pd->mac_status == WILC_MAC_STATUS_INIT) {
789                         pd->mac_status = status;
790                         linux_wlan_unlock(&pd->sync_event);
791                 } else {
792                         pd->mac_status = status;
793                 }
794
795                 if (pd->mac_status == WILC_MAC_STATUS_CONNECT) {        /* Connect */
796 #if 0
797                         /**
798                          *      get the mac and bssid address
799                          **/
800                         PRINT_D(RX_DBG, "Calling cfg_get to get MAC_ADDR\n");
801                         pd->oup.wlan_cfg_get(1, WID_MAC_ADDR, 0);
802                         PRINT_D(RX_DBG, "Calling cfg_get to get BSSID\n");
803                         pd->oup.wlan_cfg_get(0, WID_BSSID, 1);
804
805                         /**
806                          *      get the value
807                          **/
808                         pd->oup.wlan_cfg_get_value(WID_MAC_ADDR, pd->eth_src_address, 6);
809                         pd->oup.wlan_cfg_get_value(WID_BSSID, pd->eth_dst_address, 6);
810
811                         PRINT_D(GENERIC_DBG, "Source Address = %s", pd->eth_src_address);
812                         PRINT_D(GENERIC_DBG, "Destiation Address = %s", pd->eth_dst_address);
813
814                         /**
815                          *      launch ndis
816                          **/
817 #endif
818                 }
819
820         } else if (flag == WILC_MAC_INDICATE_SCAN) {
821                 PRINT_D(GENERIC_DBG, "Scanning ...\n");
822
823         }
824
825 }
826
827 struct net_device *GetIfHandler(uint8_t *pMacHeader)
828 {
829         uint8_t *Bssid, *Bssid1;
830         int i = 0;
831
832         Bssid  = pMacHeader + 10;
833         Bssid1 = pMacHeader + 4;
834
835         for (i = 0; i < g_linux_wlan->u8NoIfcs; i++) {
836                 if (!memcmp(Bssid1, g_linux_wlan->strInterfaceInfo[i].aBSSID, ETH_ALEN) ||
837                     !memcmp(Bssid, g_linux_wlan->strInterfaceInfo[i].aBSSID, ETH_ALEN)) {
838                         return g_linux_wlan->strInterfaceInfo[i].wilc_netdev;
839                 }
840         }
841         PRINT_INFO(INIT_DBG, "Invalide handle\n");
842         for (i = 0; i < 25; i++) {
843                 PRINT_D(INIT_DBG, "%02x ", pMacHeader[i]);
844         }
845         Bssid  = pMacHeader + 18;
846         Bssid1 = pMacHeader + 12;
847         for (i = 0; i < g_linux_wlan->u8NoIfcs; i++) {
848                 if (!memcmp(Bssid1, g_linux_wlan->strInterfaceInfo[i].aBSSID, ETH_ALEN) ||
849                     !memcmp(Bssid, g_linux_wlan->strInterfaceInfo[i].aBSSID, ETH_ALEN)) {
850                         PRINT_D(INIT_DBG, "Ctx [%p]\n", g_linux_wlan->strInterfaceInfo[i].wilc_netdev);
851                         return g_linux_wlan->strInterfaceInfo[i].wilc_netdev;
852                 }
853         }
854         PRINT_INFO(INIT_DBG, "\n");
855         return NULL;
856 }
857
858 int linux_wlan_set_bssid(struct net_device *wilc_netdev, uint8_t *pBSSID)
859 {
860         int i = 0;
861         int ret = -1;
862
863         PRINT_D(INIT_DBG, "set bssid on[%p]\n", wilc_netdev);
864         for (i = 0; i < g_linux_wlan->u8NoIfcs; i++) {
865                 if (g_linux_wlan->strInterfaceInfo[i].wilc_netdev == wilc_netdev) {
866                         PRINT_D(INIT_DBG, "set bssid [%x][%x][%x]\n", pBSSID[0], pBSSID[1], pBSSID[2]);
867                         memcpy(g_linux_wlan->strInterfaceInfo[i].aBSSID, pBSSID, 6);
868                         ret = 0;
869                         break;
870                 }
871         }
872         return ret;
873 }
874
875 /*BugID_5213*/
876 /*Function to get number of connected interfaces*/
877 int linux_wlan_get_num_conn_ifcs(void)
878 {
879         uint8_t i = 0;
880         uint8_t null_bssid[6] = {0};
881         uint8_t ret_val = 0;
882
883         for (i = 0; i < g_linux_wlan->u8NoIfcs; i++) {
884                 if (memcmp(g_linux_wlan->strInterfaceInfo[i].aBSSID, null_bssid, 6)) {
885                         ret_val++;
886                 }
887         }
888         return ret_val;
889 }
890
891 static int linux_wlan_rxq_task(void *vp)
892 {
893
894         /* inform wilc1000_wlan_init that RXQ task is started. */
895         linux_wlan_unlock(&g_linux_wlan->rxq_thread_started);
896         while (1) {
897                 linux_wlan_lock(&g_linux_wlan->rxq_event);
898                 /* wait_for_completion(&g_linux_wlan->rxq_event); */
899
900                 if (g_linux_wlan->close) {
901                         /*Unlock the mutex in the mac_close function to indicate the exiting of the RX thread */
902                         linux_wlan_unlock(&g_linux_wlan->rxq_thread_started);
903
904                         while (!kthread_should_stop())
905                                 schedule();
906
907                         PRINT_D(RX_DBG, " RX thread stopped\n");
908                         break;
909                 }
910                 PRINT_D(RX_DBG, "Calling wlan_handle_rx_que()\n");
911
912                 g_linux_wlan->oup.wlan_handle_rx_que();
913         }
914         return 0;
915 }
916
917 #define USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
918
919 static int linux_wlan_txq_task(void *vp)
920 {
921         int ret, txq_count;
922
923 #if defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
924 #define TX_BACKOFF_WEIGHT_INCR_STEP (1)
925 #define TX_BACKOFF_WEIGHT_DECR_STEP (1)
926 #define TX_BACKOFF_WEIGHT_MAX (7)
927 #define TX_BACKOFF_WEIGHT_MIN (0)
928 #define TX_BACKOFF_WEIGHT_UNIT_MS (10)
929         int backoff_weight = TX_BACKOFF_WEIGHT_MIN;
930         signed long timeout;
931 #endif
932
933         /* inform wilc1000_wlan_init that TXQ task is started. */
934         linux_wlan_unlock(&g_linux_wlan->txq_thread_started);
935         while (1) {
936
937                 PRINT_D(TX_DBG, "txq_task Taking a nap :)\n");
938                 linux_wlan_lock(&g_linux_wlan->txq_event);
939                 /* wait_for_completion(&pd->txq_event); */
940                 PRINT_D(TX_DBG, "txq_task Who waked me up :$\n");
941
942                 if (g_linux_wlan->close) {
943                         /*Unlock the mutex in the mac_close function to indicate the exiting of the TX thread */
944                         linux_wlan_unlock(&g_linux_wlan->txq_thread_started);
945
946                         while (!kthread_should_stop())
947                                 schedule();
948
949                         PRINT_D(TX_DBG, "TX thread stopped\n");
950                         break;
951                 }
952                 PRINT_D(TX_DBG, "txq_task handle the sending packet and let me go to sleep.\n");
953 #if !defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
954                 g_linux_wlan->oup.wlan_handle_tx_que();
955 #else
956                 do {
957                         ret = g_linux_wlan->oup.wlan_handle_tx_que(&txq_count);
958                         if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD /* && netif_queue_stopped(pd->wilc_netdev)*/) {
959                                 PRINT_D(TX_DBG, "Waking up queue\n");
960                                 /* netif_wake_queue(pd->wilc_netdev); */
961                                 if (netif_queue_stopped(g_linux_wlan->strInterfaceInfo[0].wilc_netdev))
962                                         netif_wake_queue(g_linux_wlan->strInterfaceInfo[0].wilc_netdev);
963                                 if (netif_queue_stopped(g_linux_wlan->strInterfaceInfo[1].wilc_netdev))
964                                         netif_wake_queue(g_linux_wlan->strInterfaceInfo[1].wilc_netdev);
965                         }
966
967                         if (ret == WILC_TX_ERR_NO_BUF) { /* failed to allocate buffers in chip. */
968                                 timeout = msecs_to_jiffies(TX_BACKOFF_WEIGHT_UNIT_MS << backoff_weight);
969                                 do {
970                                         /* Back off from sending packets for some time. */
971                                         /* schedule_timeout will allow RX task to run and free buffers.*/
972                                         /* set_current_state(TASK_UNINTERRUPTIBLE); */
973                                         /* timeout = schedule_timeout(timeout); */
974                                         msleep(TX_BACKOFF_WEIGHT_UNIT_MS << backoff_weight);
975                                 } while (/*timeout*/ 0);
976                                 backoff_weight += TX_BACKOFF_WEIGHT_INCR_STEP;
977                                 if (backoff_weight > TX_BACKOFF_WEIGHT_MAX) {
978                                         backoff_weight = TX_BACKOFF_WEIGHT_MAX;
979                                 }
980                         } else {
981                                 if (backoff_weight > TX_BACKOFF_WEIGHT_MIN) {
982                                         backoff_weight -= TX_BACKOFF_WEIGHT_DECR_STEP;
983                                         if (backoff_weight < TX_BACKOFF_WEIGHT_MIN) {
984                                                 backoff_weight = TX_BACKOFF_WEIGHT_MIN;
985                                         }
986                                 }
987                         }
988                         /*TODO: drop packets after a certain time/number of retry count. */
989                 } while (ret == WILC_TX_ERR_NO_BUF && !g_linux_wlan->close); /* retry sending packets if no more buffers in chip. */
990 #endif
991         }
992         return 0;
993 }
994
995 static void linux_wlan_rx_complete(void)
996 {
997         PRINT_D(RX_DBG, "RX completed\n");
998 }
999
1000 int linux_wlan_get_firmware(perInterface_wlan_t *p_nic)
1001 {
1002
1003         perInterface_wlan_t *nic = p_nic;
1004         int ret = 0;
1005         const struct firmware *wilc_firmware;
1006         char *firmware;
1007
1008
1009         if (nic->iftype == AP_MODE)
1010                 firmware = AP_FIRMWARE;
1011         else if (nic->iftype == STATION_MODE)
1012                 firmware = STA_FIRMWARE;
1013
1014         /*BugID_5137*/
1015         else {
1016                 PRINT_D(INIT_DBG, "Get P2P_CONCURRENCY_FIRMWARE\n");
1017                 firmware = P2P_CONCURRENCY_FIRMWARE;
1018         }
1019
1020
1021
1022         if (nic == NULL) {
1023                 PRINT_ER("NIC is NULL\n");
1024                 goto _fail_;
1025         }
1026
1027         if (&nic->wilc_netdev->dev == NULL) {
1028                 PRINT_ER("&nic->wilc_netdev->dev  is NULL\n");
1029                 goto _fail_;
1030         }
1031
1032
1033         /*      the firmare should be located in /lib/firmware in
1034          *      root file system with the name specified above */
1035
1036 #ifdef WILC_SDIO
1037         if (request_firmware(&wilc_firmware, firmware, &g_linux_wlan->wilc_sdio_func->dev) != 0) {
1038                 PRINT_ER("%s - firmare not available\n", firmware);
1039                 ret = -1;
1040                 goto _fail_;
1041         }
1042 #else
1043         if (request_firmware(&wilc_firmware, firmware, &g_linux_wlan->wilc_spidev->dev) != 0) {
1044                 PRINT_ER("%s - firmare not available\n", firmware);
1045                 ret = -1;
1046                 goto _fail_;
1047         }
1048 #endif
1049         g_linux_wlan->wilc_firmware = wilc_firmware; /* Bug 4703 */
1050
1051 _fail_:
1052
1053         return ret;
1054
1055 }
1056
1057 #ifdef COMPLEMENT_BOOT
1058 int repeat_power_cycle(perInterface_wlan_t *nic);
1059 #endif
1060
1061 static int linux_wlan_start_firmware(perInterface_wlan_t *nic)
1062 {
1063
1064         static int timeout = 5;
1065         int ret = 0;
1066         /* start firmware */
1067         PRINT_D(INIT_DBG, "Starting Firmware ...\n");
1068         ret = g_linux_wlan->oup.wlan_start();
1069         if (ret < 0) {
1070                 PRINT_ER("Failed to start Firmware\n");
1071                 goto _fail_;
1072         }
1073
1074         /* wait for mac ready */
1075         PRINT_D(INIT_DBG, "Waiting for Firmware to get ready ...\n");
1076         ret = linux_wlan_lock_timeout(&g_linux_wlan->sync_event, 5000);
1077         if (ret) {
1078 #ifdef COMPLEMENT_BOOT
1079
1080                 if (timeout--) {
1081                         PRINT_D(INIT_DBG, "repeat power cycle[%d]", timeout);
1082                         ret = repeat_power_cycle(nic);
1083                 } else {
1084                         timeout = 5;
1085                         ret = -1;
1086                         goto _fail_;
1087                 }
1088 #endif
1089                 PRINT_D(INIT_DBG, "Firmware start timed out");
1090                 goto _fail_;
1091         }
1092         /*
1093          *      TODO: Driver shouoldn't wait forever for firmware to get started -
1094          *      in case of timeout this should be handled properly
1095          */
1096         PRINT_D(INIT_DBG, "Firmware successfully started\n");
1097
1098 _fail_:
1099         return ret;
1100 }
1101 static int linux_wlan_firmware_download(linux_wlan_t *p_nic)
1102 {
1103
1104         int ret = 0;
1105
1106         if (g_linux_wlan->wilc_firmware == NULL) {
1107                 PRINT_ER("Firmware buffer is NULL\n");
1108                 ret = -ENOBUFS;
1109                 goto _FAIL_;
1110         }
1111         /**
1112          *      do the firmware download
1113          **/
1114         PRINT_D(INIT_DBG, "Downloading Firmware ...\n");
1115         ret = g_linux_wlan->oup.wlan_firmware_download(g_linux_wlan->wilc_firmware->data, g_linux_wlan->wilc_firmware->size);
1116         if (ret < 0) {
1117                 goto _FAIL_;
1118         }
1119
1120         /* Freeing FW buffer */
1121         PRINT_D(INIT_DBG, "Freeing FW buffer ...\n");
1122         PRINT_D(INIT_DBG, "Releasing firmware\n");
1123         release_firmware(g_linux_wlan->wilc_firmware);
1124         g_linux_wlan->wilc_firmware = NULL;
1125
1126         PRINT_D(INIT_DBG, "Download Succeeded \n");
1127
1128 _FAIL_:
1129         return ret;
1130 }
1131
1132
1133 /* startup configuration - could be changed later using iconfig*/
1134 static int linux_wlan_init_test_config(struct net_device *dev, linux_wlan_t *p_nic)
1135 {
1136
1137         unsigned char c_val[64];
1138         #ifndef STATIC_MACADDRESS
1139         unsigned char mac_add[] = {0x00, 0x80, 0xC2, 0x5E, 0xa2, 0xff};
1140         #endif
1141         unsigned int chipid = 0;
1142
1143         /*BugID_5077*/
1144         struct WILC_WFI_priv *priv;
1145         tstrWILC_WFIDrv *pstrWFIDrv;
1146
1147         PRINT_D(TX_DBG, "Start configuring Firmware\n");
1148         #ifndef STATIC_MACADDRESS
1149         get_random_bytes(&mac_add[5], 1);
1150         get_random_bytes(&mac_add[4], 1);
1151         #endif
1152         priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
1153         pstrWFIDrv = (tstrWILC_WFIDrv *)priv->hWILCWFIDrv;
1154         PRINT_D(INIT_DBG, "Host = %x\n", (WILC_Uint32)pstrWFIDrv);
1155
1156         PRINT_D(INIT_DBG, "MAC address is : %02x-%02x-%02x-%02x-%02x-%02x\n", mac_add[0], mac_add[1], mac_add[2], mac_add[3], mac_add[4], mac_add[5]);
1157         chipid = wilc_get_chipid(0);
1158
1159
1160         if (g_linux_wlan->oup.wlan_cfg_set == NULL) {
1161                 PRINT_D(INIT_DBG, "Null p[ointer\n");
1162                 goto _fail_;
1163         }
1164
1165         *(int *)c_val = (WILC_Uint32)pstrWFIDrv;
1166
1167         if (!g_linux_wlan->oup.wlan_cfg_set(1, WID_SET_DRV_HANDLER, c_val, 4, 0, 0))
1168                 goto _fail_;
1169
1170         /*to tell fw that we are going to use PC test - WILC specific*/
1171         c_val[0] = 0;
1172         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_PC_TEST_MODE, c_val, 1, 0, 0))
1173                 goto _fail_;
1174
1175         c_val[0] = INFRASTRUCTURE;
1176         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_BSS_TYPE, c_val, 1, 0, 0))
1177                 goto _fail_;
1178
1179
1180         /* c_val[0] = RATE_AUTO; / * bug 4275: Enable autorate and limit it to 24Mbps * / */
1181         c_val[0] = RATE_AUTO;
1182         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_CURRENT_TX_RATE, c_val, 1, 0, 0))
1183                 goto _fail_;
1184
1185         c_val[0] = G_MIXED_11B_2_MODE;
1186         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11G_OPERATING_MODE, c_val, 1, 0, 0))
1187                 goto _fail_;
1188
1189         c_val[0] = 1;
1190         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_CURRENT_CHANNEL, c_val, 1, 0, 0))
1191                 goto _fail_;
1192
1193         c_val[0] = G_SHORT_PREAMBLE;
1194         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_PREAMBLE, c_val, 1, 0, 0))
1195                 goto _fail_;
1196
1197         c_val[0] = AUTO_PROT;
1198         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_PROT_MECH, c_val, 1, 0, 0))
1199                 goto _fail_;
1200
1201 #ifdef SWITCH_LOG_TERMINAL
1202         c_val[0] = AUTO_PROT;
1203         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_LOGTerminal_Switch, c_val, 1, 0, 0))
1204                 goto _fail_;
1205 #endif
1206
1207         c_val[0] = ACTIVE_SCAN;
1208         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_SCAN_TYPE, c_val, 1, 0, 0))
1209                 goto _fail_;
1210
1211         c_val[0] = SITE_SURVEY_OFF;
1212         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_SITE_SURVEY, c_val, 1, 0, 0))
1213                 goto _fail_;
1214
1215         *((int *)c_val) = 0xffff; /* Never use RTS-CTS */
1216         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_RTS_THRESHOLD, c_val, 2, 0, 0))
1217                 goto _fail_;
1218
1219         *((int *)c_val) = 2346;
1220         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_FRAG_THRESHOLD, c_val, 2, 0, 0))
1221                 goto _fail_;
1222
1223         /*  SSID                                                                 */
1224         /*  --------------------------------------------------------------       */
1225         /*  Configuration :   String with length less than 32 bytes              */
1226         /*  Values to set :   Any string with length less than 32 bytes          */
1227         /*                    ( In BSS Station Set SSID to "" (null string)      */
1228         /*                      to enable Broadcast SSID suppport )              */
1229         /*  --------------------------------------------------------------       */
1230 #ifndef USE_WIRELESS
1231         strcpy(c_val, "nwifi");
1232         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_SSID, c_val, (strlen(c_val) + 1), 0, 0))
1233                 goto _fail_;
1234 #endif
1235
1236         c_val[0] = 0;
1237         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_BCAST_SSID, c_val, 1, 0, 0))
1238                 goto _fail_;
1239
1240         c_val[0] = 1;
1241         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_QOS_ENABLE, c_val, 1, 0, 0))
1242                 goto _fail_;
1243
1244         c_val[0] = NO_POWERSAVE;
1245         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_POWER_MANAGEMENT, c_val, 1, 0, 0))
1246                 goto _fail_;
1247
1248         c_val[0] = NO_ENCRYPT; /* NO_ENCRYPT, 0x79 */
1249         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11I_MODE, c_val, 1, 0, 0))
1250                 goto _fail_;
1251
1252         c_val[0] = OPEN_SYSTEM;
1253         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_AUTH_TYPE, c_val, 1, 0, 0))
1254                 goto _fail_;
1255
1256         /*  WEP/802 11I Configuration                                            */
1257         /*  ------------------------------------------------------------------   */
1258         /*  Configuration : WEP Key                                              */
1259         /*  Values (0x)   : 5 byte for WEP40 and 13 bytes for WEP104             */
1260         /*                  In case more than 5 bytes are passed on for WEP 40   */
1261         /*                  only first 5 bytes will be used as the key           */
1262         /*  ------------------------------------------------------------------   */
1263
1264         strcpy(c_val, "123456790abcdef1234567890");
1265         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_WEP_KEY_VALUE, c_val, (strlen(c_val) + 1), 0, 0))
1266                 goto _fail_;
1267
1268         /*  WEP/802 11I Configuration                                            */
1269         /*  ------------------------------------------------------------------   */
1270         /*  Configuration : AES/TKIP WPA/RSNA Pre-Shared Key                     */
1271         /*  Values to set : Any string with length greater than equal to 8 bytes */
1272         /*                  and less than 64 bytes                               */
1273         /*  ------------------------------------------------------------------   */
1274         strcpy(c_val, "12345678");
1275         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11I_PSK, c_val, (strlen(c_val)), 0, 0))
1276                 goto _fail_;
1277
1278         /*  IEEE802.1X Key Configuration                                         */
1279         /*  ------------------------------------------------------------------   */
1280         /*  Configuration : Radius Server Access Secret Key                      */
1281         /*  Values to set : Any string with length greater than equal to 8 bytes */
1282         /*                  and less than 65 bytes                               */
1283         /*  ------------------------------------------------------------------   */
1284         strcpy(c_val, "password");
1285         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_1X_KEY, c_val, (strlen(c_val) + 1), 0, 0))
1286                 goto _fail_;
1287
1288         /*   IEEE802.1X Server Address Configuration                             */
1289         /*  ------------------------------------------------------------------   */
1290         /*  Configuration : Radius Server IP Address                             */
1291         /*  Values to set : Any valid IP Address                                 */
1292         /*  ------------------------------------------------------------------   */
1293         c_val[0] = 192;
1294         c_val[1] = 168;
1295         c_val[2] = 1;
1296         c_val[3] = 112;
1297         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_1X_SERV_ADDR, c_val, 4, 0, 0))
1298                 goto _fail_;
1299
1300         c_val[0] = 3;
1301         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_LISTEN_INTERVAL, c_val, 1, 0, 0))
1302                 goto _fail_;
1303
1304         c_val[0] = 3;
1305         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_DTIM_PERIOD, c_val, 1, 0, 0))
1306                 goto _fail_;
1307
1308         c_val[0] = NORMAL_ACK;
1309         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_ACK_POLICY, c_val, 1, 0, 0))
1310                 goto _fail_;
1311
1312         c_val[0] = 0;
1313         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_USER_CONTROL_ON_TX_POWER, c_val, 1, 0, 0))
1314                 goto _fail_;
1315
1316         c_val[0] = 48;
1317         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_TX_POWER_LEVEL_11A, c_val, 1, 0, 0))
1318                 goto _fail_;
1319
1320         c_val[0] = 28;
1321         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_TX_POWER_LEVEL_11B, c_val, 1, 0, 0))
1322                 goto _fail_;
1323
1324         /*  Beacon Interval                                                      */
1325         /*  -------------------------------------------------------------------- */
1326         /*  Configuration : Sets the beacon interval value                       */
1327         /*  Values to set : Any 16-bit value                                     */
1328         /*  -------------------------------------------------------------------- */
1329
1330         *((int *)c_val) = 100;
1331         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_BEACON_INTERVAL, c_val, 2, 0, 0))
1332                 goto _fail_;
1333
1334         c_val[0] = REKEY_DISABLE;
1335         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_REKEY_POLICY, c_val, 1, 0, 0))
1336                 goto _fail_;
1337
1338         /*  Rekey Time (s) (Used only when the Rekey policy is 2 or 4)           */
1339         /*  -------------------------------------------------------------------- */
1340         /*  Configuration : Sets the Rekey Time (s)                              */
1341         /*  Values to set : 32-bit value                                         */
1342         /*  -------------------------------------------------------------------- */
1343         *((int *)c_val) = 84600;
1344         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_REKEY_PERIOD, c_val, 4, 0, 0))
1345                 goto _fail_;
1346
1347         /*  Rekey Packet Count (in 1000s; used when Rekey Policy is 3)           */
1348         /*  -------------------------------------------------------------------- */
1349         /*  Configuration : Sets Rekey Group Packet count                        */
1350         /*  Values to set : 32-bit Value                                         */
1351         /*  -------------------------------------------------------------------- */
1352         *((int *)c_val) = 500;
1353         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_REKEY_PACKET_COUNT, c_val, 4, 0, 0))
1354                 goto _fail_;
1355
1356         c_val[0] = 1;
1357         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_SHORT_SLOT_ALLOWED, c_val, 1, 0, 0))
1358                 goto _fail_;
1359
1360         c_val[0] = G_SELF_CTS_PROT;
1361         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_ERP_PROT_TYPE, c_val, 1, 0, 0))
1362                 goto _fail_;
1363
1364         c_val[0] = 1;  /* Enable N */
1365         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_ENABLE, c_val, 1, 0, 0))
1366                 goto _fail_;
1367
1368         c_val[0] = HT_MIXED_MODE;
1369         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_OPERATING_MODE, c_val, 1, 0, 0))
1370                 goto _fail_;
1371
1372         c_val[0] = 1;   /* TXOP Prot disable in N mode: No RTS-CTS on TX A-MPDUs to save air-time. */
1373         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_TXOP_PROT_DISABLE, c_val, 1, 0, 0))
1374                 goto _fail_;
1375
1376         memcpy(c_val, mac_add, 6);
1377
1378         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_MAC_ADDR, c_val, 6, 0, 0))
1379                 goto _fail_;
1380
1381         /**
1382          *      AP only
1383          **/
1384         c_val[0] = DETECT_PROTECT_REPORT;
1385         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_OBSS_NONHT_DETECTION, c_val, 1, 0, 0))
1386                 goto _fail_;
1387
1388         c_val[0] = RTS_CTS_NONHT_PROT;
1389         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_HT_PROT_TYPE, c_val, 1, 0, 0))
1390                 goto _fail_;
1391
1392         c_val[0] = 0;
1393         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_RIFS_PROT_ENABLE, c_val, 1, 0, 0))
1394                 goto _fail_;
1395
1396         c_val[0] = MIMO_MODE;
1397         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_SMPS_MODE, c_val, 1, 0, 0))
1398                 goto _fail_;
1399
1400         c_val[0] = 7;
1401         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_CURRENT_TX_MCS, c_val, 1, 0, 0))
1402                 goto _fail_;
1403
1404         c_val[0] = 1; /* Enable N with immediate block ack. */
1405         if (!g_linux_wlan->oup.wlan_cfg_set(0, WID_11N_IMMEDIATE_BA_ENABLED, c_val, 1, 1, (WILC_Uint32)pstrWFIDrv))
1406                 goto _fail_;
1407
1408         return 0;
1409
1410 _fail_:
1411         return -1;
1412 }
1413
1414
1415 /**************************/
1416 void wilc1000_wlan_deinit(linux_wlan_t *nic)
1417 {
1418
1419         if (g_linux_wlan->wilc1000_initialized) {
1420
1421                 printk("Deinitializing wilc1000  ...\n");
1422
1423                 if (nic == NULL) {
1424                         PRINT_ER("nic is NULL\n");
1425                         return;
1426                 }
1427
1428 #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
1429                 /* johnny : remove */
1430                 PRINT_D(INIT_DBG, "skip wilc_bus_set_default_speed\n");
1431 #else
1432                 wilc_bus_set_default_speed();
1433 #endif
1434
1435                 PRINT_D(INIT_DBG, "Disabling IRQ\n");
1436                 #if (!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO)
1437                 linux_wlan_disable_irq(IRQ_WAIT);
1438                 #else
1439                   #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
1440
1441                   #else
1442                 linux_wlan_lock_mutex((void *)&g_linux_wlan->hif_cs);
1443                 disable_sdio_interrupt();
1444                 linux_wlan_unlock_mutex((void *)&g_linux_wlan->hif_cs);
1445                   #endif
1446                 #endif
1447
1448
1449                 /* not sure if the following unlocks are needed or not*/
1450                 if (&g_linux_wlan->rxq_event != NULL) {
1451                         linux_wlan_unlock(&g_linux_wlan->rxq_event);
1452                 }
1453
1454                 if (&g_linux_wlan->txq_event != NULL) {
1455                         linux_wlan_unlock(&g_linux_wlan->txq_event);
1456                 }
1457
1458
1459         #if (RX_BH_TYPE == RX_BH_WORK_QUEUE)
1460                 /*Removing the work struct from the linux kernel workqueue*/
1461                 if (&g_linux_wlan->rx_work_queue != NULL)
1462                         flush_work(&g_linux_wlan->rx_work_queue);
1463
1464         #elif (RX_BH_TYPE == RX_BH_KTHREAD)
1465                 /* if(&nic->rx_sem != NULL) */
1466                 /* linux_wlan_unlock(&nic->rx_sem); */
1467         #endif
1468
1469                 PRINT_D(INIT_DBG, "Deinitializing Threads\n");
1470                 wlan_deinitialize_threads(nic);
1471
1472                 PRINT_D(INIT_DBG, "Deinitializing IRQ\n");
1473                 deinit_irq(g_linux_wlan);
1474
1475
1476                 if (&g_linux_wlan->oup != NULL) {
1477                         if (g_linux_wlan->oup.wlan_stop != NULL)
1478                                 g_linux_wlan->oup.wlan_stop();
1479                 }
1480
1481                 PRINT_D(INIT_DBG, "Deinitializing WILC Wlan\n");
1482                 wilc_wlan_deinit(nic);
1483 #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
1484   #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
1485                 PRINT_D(INIT_DBG, "Disabling IRQ 2\n");
1486
1487                 linux_wlan_lock_mutex((void *)&g_linux_wlan->hif_cs);
1488                 disable_sdio_interrupt();
1489                 linux_wlan_unlock_mutex((void *)&g_linux_wlan->hif_cs);
1490   #endif
1491 #endif
1492
1493                 /*De-Initialize locks*/
1494                 PRINT_D(INIT_DBG, "Deinitializing Locks\n");
1495                 wlan_deinit_locks(g_linux_wlan);
1496
1497                 /* announce that wilc1000 is not initialized */
1498                 g_linux_wlan->wilc1000_initialized = 0;
1499
1500                 PRINT_D(INIT_DBG, "wilc1000 deinitialization Done\n");
1501
1502         } else {
1503                 PRINT_D(INIT_DBG, "wilc1000 is not initialized\n");
1504         }
1505         return;
1506 }
1507
1508 int wlan_init_locks(linux_wlan_t *p_nic)
1509 {
1510
1511         PRINT_D(INIT_DBG, "Initializing Locks ...\n");
1512
1513         /*initialize mutexes*/
1514         linux_wlan_init_mutex("hif_lock/hif_cs", &g_linux_wlan->hif_cs, 1);
1515         linux_wlan_init_mutex("rxq_lock/rxq_cs", &g_linux_wlan->rxq_cs, 1);
1516         linux_wlan_init_mutex("txq_lock/txq_cs", &g_linux_wlan->txq_cs, 1);
1517
1518         /*Added by Amr - BugID_4720*/
1519         linux_wlan_init_spin_lock("txq_spin_lock/txq_cs", &g_linux_wlan->txq_spinlock, 1);
1520
1521         /*Added by Amr - BugID_4720*/
1522         linux_wlan_init_lock("txq_add_to_head_lock/txq_cs", &g_linux_wlan->txq_add_to_head_cs, 1);
1523
1524         linux_wlan_init_lock("txq_wait/txq_event", &g_linux_wlan->txq_event, 0);
1525         linux_wlan_init_lock("rxq_wait/rxq_event", &g_linux_wlan->rxq_event, 0);
1526
1527         linux_wlan_init_lock("cfg_wait/cfg_event", &g_linux_wlan->cfg_event, 0);
1528         linux_wlan_init_lock("sync_event", &g_linux_wlan->sync_event, 0);
1529
1530         linux_wlan_init_lock("rxq_lock/rxq_started", &g_linux_wlan->rxq_thread_started, 0);
1531         linux_wlan_init_lock("rxq_lock/txq_started", &g_linux_wlan->txq_thread_started, 0);
1532
1533         #if (RX_BH_TYPE == RX_BH_KTHREAD)
1534         linux_wlan_init_lock("BH_SEM", &g_linux_wlan->rx_sem, 0);
1535         #endif
1536
1537         return 0;
1538 }
1539
1540 static int wlan_deinit_locks(linux_wlan_t *nic)
1541 {
1542         PRINT_D(INIT_DBG, "De-Initializing Locks\n");
1543
1544         if (&g_linux_wlan->hif_cs != NULL)
1545                 linux_wlan_deinit_mutex(&g_linux_wlan->hif_cs);
1546
1547         if (&g_linux_wlan->rxq_cs != NULL)
1548                 linux_wlan_deinit_mutex(&g_linux_wlan->rxq_cs);
1549
1550         if (&g_linux_wlan->txq_cs != NULL)
1551                 linux_wlan_deinit_mutex(&g_linux_wlan->txq_cs);
1552
1553         /*Added by Amr - BugID_4720*/
1554         if (&g_linux_wlan->txq_spinlock != NULL)
1555                 linux_wlan_deinit_spin_lock(&g_linux_wlan->txq_spinlock);
1556
1557         if (&g_linux_wlan->rxq_event != NULL)
1558                 linux_wlan_deinit_lock(&g_linux_wlan->rxq_event);
1559
1560         if (&g_linux_wlan->txq_event != NULL)
1561                 linux_wlan_deinit_lock(&g_linux_wlan->txq_event);
1562
1563         /*Added by Amr - BugID_4720*/
1564         if (&g_linux_wlan->txq_add_to_head_cs != NULL)
1565                 linux_wlan_deinit_lock(&g_linux_wlan->txq_add_to_head_cs);
1566
1567         if (&g_linux_wlan->rxq_thread_started != NULL)
1568                 linux_wlan_deinit_lock(&g_linux_wlan->rxq_thread_started);
1569
1570         if (&g_linux_wlan->txq_thread_started != NULL)
1571                 linux_wlan_deinit_lock(&g_linux_wlan->txq_thread_started);
1572
1573         if (&g_linux_wlan->cfg_event != NULL)
1574                 linux_wlan_deinit_lock(&g_linux_wlan->cfg_event);
1575
1576         if (&g_linux_wlan->sync_event != NULL)
1577                 linux_wlan_deinit_lock(&g_linux_wlan->sync_event);
1578
1579         return 0;
1580 }
1581 void linux_to_wlan(wilc_wlan_inp_t *nwi, linux_wlan_t *nic)
1582 {
1583
1584         PRINT_D(INIT_DBG, "Linux to Wlan services ...\n");
1585
1586         nwi->os_context.hif_critical_section = (void *)&g_linux_wlan->hif_cs;
1587         nwi->os_context.os_private = (void *)nic;
1588         nwi->os_context.tx_buffer_size = LINUX_TX_SIZE;
1589         nwi->os_context.txq_critical_section = (void *)&g_linux_wlan->txq_cs;
1590
1591         /*Added by Amr - BugID_4720*/
1592         nwi->os_context.txq_add_to_head_critical_section = (void *)&g_linux_wlan->txq_add_to_head_cs;
1593
1594         /*Added by Amr - BugID_4720*/
1595         nwi->os_context.txq_spin_lock = (void *)&g_linux_wlan->txq_spinlock;
1596
1597         nwi->os_context.txq_wait_event = (void *)&g_linux_wlan->txq_event;
1598
1599 #if defined (MEMORY_STATIC)
1600         nwi->os_context.rx_buffer_size = LINUX_RX_SIZE;
1601 #endif
1602         nwi->os_context.rxq_critical_section = (void *)&g_linux_wlan->rxq_cs;
1603         nwi->os_context.rxq_wait_event = (void *)&g_linux_wlan->rxq_event;
1604         nwi->os_context.cfg_wait_event = (void *)&g_linux_wlan->cfg_event;
1605
1606         nwi->os_func.os_sleep = linux_wlan_msleep;
1607         nwi->os_func.os_atomic_sleep = linux_wlan_atomic_msleep;
1608         nwi->os_func.os_debug = linux_wlan_dbg;
1609         nwi->os_func.os_malloc = linux_wlan_malloc;
1610         nwi->os_func.os_malloc_atomic = linux_wlan_malloc_atomic;
1611         nwi->os_func.os_free = linux_wlan_free;
1612         nwi->os_func.os_lock = linux_wlan_lock;
1613         nwi->os_func.os_unlock = linux_wlan_unlock;
1614         nwi->os_func.os_wait = linux_wlan_lock_timeout;
1615         nwi->os_func.os_signal = linux_wlan_unlock;
1616         nwi->os_func.os_enter_cs = linux_wlan_lock_mutex;
1617         nwi->os_func.os_leave_cs = linux_wlan_unlock_mutex;
1618
1619         /*Added by Amr - BugID_4720*/
1620         nwi->os_func.os_spin_lock = linux_wlan_spin_lock;
1621         nwi->os_func.os_spin_unlock = linux_wlan_spin_unlock;
1622
1623 #ifdef WILC_SDIO
1624         nwi->io_func.io_type = HIF_SDIO;
1625         nwi->io_func.io_init = linux_sdio_init;
1626         nwi->io_func.io_deinit = linux_sdio_deinit;
1627         nwi->io_func.u.sdio.sdio_cmd52 = linux_sdio_cmd52;
1628         nwi->io_func.u.sdio.sdio_cmd53 = linux_sdio_cmd53;
1629         nwi->io_func.u.sdio.sdio_set_max_speed = linux_sdio_set_max_speed;
1630         nwi->io_func.u.sdio.sdio_set_default_speed = linux_sdio_set_default_speed;
1631 #else
1632         nwi->io_func.io_type = HIF_SPI;
1633         nwi->io_func.io_init = linux_spi_init;
1634         nwi->io_func.io_deinit = linux_spi_deinit;
1635         nwi->io_func.u.spi.spi_tx = linux_spi_write;
1636         nwi->io_func.u.spi.spi_rx = linux_spi_read;
1637         nwi->io_func.u.spi.spi_trx = linux_spi_write_read;
1638         nwi->io_func.u.spi.spi_max_speed = linux_spi_set_max_speed;
1639 #endif
1640
1641         /*for now - to be revised*/
1642         #ifdef WILC_FULLY_HOSTING_AP
1643         /* incase of Fully hosted AP, all non cfg pkts are processed here*/
1644         nwi->net_func.rx_indicate = WILC_Process_rx_frame;
1645         #else
1646         nwi->net_func.rx_indicate = frmw_to_linux;
1647         #endif
1648         nwi->net_func.rx_complete = linux_wlan_rx_complete;
1649         nwi->indicate_func.mac_indicate = linux_wlan_mac_indicate;
1650 }
1651
1652 int wlan_initialize_threads(perInterface_wlan_t *nic)
1653 {
1654
1655         int ret = 0;
1656         PRINT_D(INIT_DBG, "Initializing Threads ...\n");
1657
1658 #if (RX_BH_TYPE == RX_BH_WORK_QUEUE)
1659         /*Initialize rx work queue task*/
1660         INIT_WORK(&g_linux_wlan->rx_work_queue, isr_bh_routine);
1661 #elif (RX_BH_TYPE == RX_BH_KTHREAD)
1662         PRINT_D(INIT_DBG, "Creating kthread for Rxq BH\n");
1663         g_linux_wlan->rx_bh_thread = kthread_run(isr_bh_routine, (void *)g_linux_wlan, "K_RXQ_BH");
1664         if (g_linux_wlan->rx_bh_thread == 0) {
1665                 PRINT_ER("couldn't create RX BH thread\n");
1666                 ret = -ENOBUFS;
1667                 goto _fail_;
1668         }
1669 #endif
1670
1671 #ifndef TCP_ENHANCEMENTS
1672         /* create rx task */
1673         PRINT_D(INIT_DBG, "Creating kthread for reception\n");
1674         g_linux_wlan->rxq_thread = kthread_run(linux_wlan_rxq_task, (void *)g_linux_wlan, "K_RXQ_TASK");
1675         if (g_linux_wlan->rxq_thread == 0) {
1676                 PRINT_ER("couldn't create RXQ thread\n");
1677                 ret = -ENOBUFS;
1678                 goto _fail_1;
1679         }
1680
1681         /* wait for RXQ task to start. */
1682         linux_wlan_lock(&g_linux_wlan->rxq_thread_started);
1683
1684 #endif
1685
1686         /* create tx task */
1687         PRINT_D(INIT_DBG, "Creating kthread for transmission\n");
1688         g_linux_wlan->txq_thread = kthread_run(linux_wlan_txq_task, (void *)g_linux_wlan, "K_TXQ_TASK");
1689         if (g_linux_wlan->txq_thread == 0) {
1690                 PRINT_ER("couldn't create TXQ thread\n");
1691                 ret = -ENOBUFS;
1692                 goto _fail_2;
1693         }
1694 #ifdef DEBUG_MODE
1695         PRINT_D(INIT_DBG, "Creating kthread for Debugging\n");
1696         g_linux_wlan->txq_thread = kthread_run(DebuggingThreadTask, (void *)g_linux_wlan, "DebugThread");
1697         if (g_linux_wlan->txq_thread == 0) {
1698                 PRINT_ER("couldn't create TXQ thread\n");
1699                 ret = -ENOBUFS;
1700                 goto _fail_2;
1701         }
1702 #endif
1703         /* wait for TXQ task to start. */
1704         linux_wlan_lock(&g_linux_wlan->txq_thread_started);
1705
1706         return 0;
1707
1708 _fail_2:
1709         /*De-Initialize 2nd thread*/
1710         g_linux_wlan->close = 1;
1711         linux_wlan_unlock(&g_linux_wlan->rxq_event);
1712         kthread_stop(g_linux_wlan->rxq_thread);
1713
1714 _fail_1:
1715         #if (RX_BH_TYPE == RX_BH_KTHREAD)
1716         /*De-Initialize 1st thread*/
1717         g_linux_wlan->close = 1;
1718         linux_wlan_unlock(&g_linux_wlan->rx_sem);
1719         kthread_stop(g_linux_wlan->rx_bh_thread);
1720 _fail_:
1721         #endif
1722         g_linux_wlan->close = 0;
1723         return ret;
1724 }
1725
1726 static void wlan_deinitialize_threads(linux_wlan_t *nic)
1727 {
1728
1729         g_linux_wlan->close = 1;
1730         PRINT_D(INIT_DBG, "Deinitializing Threads\n");
1731         if (&g_linux_wlan->rxq_event != NULL)
1732                 linux_wlan_unlock(&g_linux_wlan->rxq_event);
1733
1734
1735         if (g_linux_wlan->rxq_thread != NULL) {
1736                 kthread_stop(g_linux_wlan->rxq_thread);
1737                 g_linux_wlan->rxq_thread = NULL;
1738         }
1739
1740
1741         if (&g_linux_wlan->txq_event != NULL)
1742                 linux_wlan_unlock(&g_linux_wlan->txq_event);
1743
1744
1745         if (g_linux_wlan->txq_thread != NULL) {
1746                 kthread_stop(g_linux_wlan->txq_thread);
1747                 g_linux_wlan->txq_thread = NULL;
1748         }
1749
1750         #if (RX_BH_TYPE == RX_BH_KTHREAD)
1751         if (&g_linux_wlan->rx_sem != NULL)
1752                 linux_wlan_unlock(&g_linux_wlan->rx_sem);
1753
1754         if (g_linux_wlan->rx_bh_thread != NULL) {
1755                 kthread_stop(g_linux_wlan->rx_bh_thread);
1756                 g_linux_wlan->rx_bh_thread = NULL;
1757         }
1758         #endif
1759 }
1760
1761 #ifdef STATIC_MACADDRESS
1762 const char *path_string[] = {
1763         "/etc/wlan",
1764         "/data/wlan",
1765 };
1766
1767 static int linux_wlan_read_mac_addr(void *vp)
1768 {
1769         int ret = 0;
1770         struct file *fp = (struct file *)-ENOENT;
1771         mm_segment_t old_fs;
1772         loff_t pos = 0;
1773         int index;
1774         int array_size = sizeof(path_string) / sizeof(path_string[0]);
1775
1776         /* change to KERNEL_DS address limit */
1777         old_fs = get_fs();
1778         set_fs(KERNEL_DS);
1779
1780         for (index = 0; index < array_size; index++) {
1781                 fp = filp_open(path_string[index], O_WRONLY, 0640);
1782                 if (!fp) {
1783                         ret = -1;
1784                         goto exit;
1785                 }
1786
1787                 /*No such file or directory */
1788                 if (IS_ERR(fp) || !fp->f_op) {
1789                         get_random_bytes(&mac_add[3], 3);
1790                         /* open file to write */
1791                         fp = filp_open(path_string[index], O_WRONLY | O_CREAT, 0640);
1792
1793                         if (!fp || IS_ERR(fp)) {
1794                                 ret = -1;
1795                                 continue;
1796                         } else {
1797                                 /* write buf to file */
1798                                 fp->f_op->write(fp, mac_add, 6, &pos);
1799                                 break;
1800                         }
1801                 } else {
1802                         /* read file to buf */
1803                         fp->f_op->read(fp, mac_add, 6, &pos);
1804                         break;
1805                 }
1806         }
1807
1808         if (index == array_size) {
1809                 PRINT_ER("random MAC\n");
1810         }
1811
1812 exit:
1813         if (fp && !IS_ERR(fp)) {
1814                 filp_close(fp, NULL);
1815         }
1816
1817         set_fs(old_fs);
1818
1819         return ret;
1820 }
1821 #endif
1822
1823 #ifdef COMPLEMENT_BOOT
1824
1825 extern volatile int probe;
1826 extern uint8_t core_11b_ready(void);
1827
1828 #define READY_CHECK_THRESHOLD           30
1829 extern void wilc_wlan_global_reset(void);
1830 uint8_t wilc1000_prepare_11b_core(wilc_wlan_inp_t *nwi, wilc_wlan_oup_t *nwo, linux_wlan_t *nic)
1831 {
1832         uint8_t trials = 0;
1833         while ((core_11b_ready() && (READY_CHECK_THRESHOLD > (trials++)))) {
1834                 PRINT_D(INIT_DBG, "11b core not ready yet: %u\n", trials);
1835                 wilc_wlan_deinit(nic);
1836                 wilc_wlan_global_reset();
1837                 sdio_unregister_driver(&wilc_bus);
1838
1839                 linux_wlan_device_detection(0);
1840
1841                 mdelay(100);
1842
1843                 linux_wlan_device_detection(1);
1844
1845                 sdio_register_driver(&wilc_bus);
1846
1847                 while (!probe) {
1848                         msleep(100);
1849                 }
1850                 probe = 0;
1851                 g_linux_wlan->wilc_sdio_func = local_sdio_func;
1852                 linux_to_wlan(nwi, nic);
1853                 wilc_wlan_init(nwi, nwo);
1854         }
1855
1856         if (READY_CHECK_THRESHOLD <= trials)
1857                 return 1;
1858         else
1859                 return 0;
1860
1861 }
1862
1863 int repeat_power_cycle(perInterface_wlan_t *nic)
1864 {
1865         int ret = 0;
1866         wilc_wlan_inp_t nwi;
1867         wilc_wlan_oup_t nwo;
1868         sdio_unregister_driver(&wilc_bus);
1869
1870         linux_wlan_device_detection(0);
1871         linux_wlan_device_power(0);
1872         msleep(100);
1873         linux_wlan_device_power(1);
1874         msleep(80);
1875         linux_wlan_device_detection(1);
1876         msleep(20);
1877
1878         sdio_register_driver(&wilc_bus);
1879
1880         /* msleep(1000); */
1881         while (!probe) {
1882                 msleep(100);
1883         }
1884         probe = 0;
1885         g_linux_wlan->wilc_sdio_func = local_sdio_func;
1886         linux_to_wlan(&nwi, g_linux_wlan);
1887         ret = wilc_wlan_init(&nwi, &nwo);
1888
1889         g_linux_wlan->mac_status = WILC_MAC_STATUS_INIT;
1890         #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
1891         enable_sdio_interrupt();
1892         #endif
1893
1894         if (linux_wlan_get_firmware(nic)) {
1895                 PRINT_ER("Can't get firmware \n");
1896                 ret = -1;
1897                 goto __fail__;
1898         }
1899
1900         /*Download firmware*/
1901         ret = linux_wlan_firmware_download(g_linux_wlan);
1902         if (ret < 0) {
1903                 PRINT_ER("Failed to download firmware\n");
1904                 goto __fail__;
1905         }
1906         /* Start firmware*/
1907         ret = linux_wlan_start_firmware(nic);
1908         if (ret < 0) {
1909                 PRINT_ER("Failed to start firmware\n");
1910         }
1911 __fail__:
1912         return ret;
1913 }
1914 #endif
1915
1916 int wilc1000_wlan_init(struct net_device *dev, perInterface_wlan_t *p_nic)
1917 {
1918         wilc_wlan_inp_t nwi;
1919         wilc_wlan_oup_t nwo;
1920         perInterface_wlan_t *nic = p_nic;
1921         int ret = 0;
1922
1923         if (!g_linux_wlan->wilc1000_initialized) {
1924                 g_linux_wlan->mac_status = WILC_MAC_STATUS_INIT;
1925                 g_linux_wlan->close = 0;
1926                 g_linux_wlan->wilc1000_initialized = 0;
1927
1928                 wlan_init_locks(g_linux_wlan);
1929
1930 #ifdef STATIC_MACADDRESS
1931                 wilc_mac_thread = kthread_run(linux_wlan_read_mac_addr, NULL, "wilc_mac_thread");
1932                 if (wilc_mac_thread < 0) {
1933                         PRINT_ER("couldn't create Mac addr thread\n");
1934                 }
1935 #endif
1936
1937                 linux_to_wlan(&nwi, g_linux_wlan);
1938
1939                 ret = wilc_wlan_init(&nwi, &nwo);
1940                 if (ret < 0) {
1941                         PRINT_ER("Initializing WILC_Wlan FAILED\n");
1942                         ret = -EIO;
1943                         goto _fail_locks_;
1944                 }
1945                 memcpy(&g_linux_wlan->oup, &nwo, sizeof(wilc_wlan_oup_t));
1946
1947                 /*Save the oup structre into global pointer*/
1948                 gpstrWlanOps = &g_linux_wlan->oup;
1949
1950
1951                 ret = wlan_initialize_threads(nic);
1952                 if (ret < 0) {
1953                         PRINT_ER("Initializing Threads FAILED\n");
1954                         ret = -EIO;
1955                         goto _fail_wilc_wlan_;
1956                 }
1957
1958 #if (defined WILC_SDIO) && (defined COMPLEMENT_BOOT)
1959                 if (wilc1000_prepare_11b_core(&nwi, &nwo, g_linux_wlan)) {
1960                         PRINT_ER("11b Core is not ready\n");
1961                         ret = -EIO;
1962                         goto _fail_threads_;
1963                 }
1964 #endif
1965
1966 #if (!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO)
1967                 if (init_irq(g_linux_wlan)) {
1968                         PRINT_ER("couldn't initialize IRQ\n");
1969                         ret = -EIO;
1970                         goto _fail_threads_;
1971                 }
1972 #endif
1973
1974 #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
1975                 if (enable_sdio_interrupt()) {
1976                         PRINT_ER("couldn't initialize IRQ\n");
1977                         ret = -EIO;
1978                         goto _fail_irq_init_;
1979                 }
1980 #endif
1981
1982                 if (linux_wlan_get_firmware(nic)) {
1983                         PRINT_ER("Can't get firmware \n");
1984                         ret = -EIO;
1985                         goto _fail_irq_enable_;
1986                 }
1987
1988
1989                 /*Download firmware*/
1990                 ret = linux_wlan_firmware_download(g_linux_wlan);
1991                 if (ret < 0) {
1992                         PRINT_ER("Failed to download firmware\n");
1993                         ret = -EIO;
1994                         goto _fail_irq_enable_;
1995                 }
1996
1997                 /* Start firmware*/
1998                 ret = linux_wlan_start_firmware(nic);
1999                 if (ret < 0) {
2000                         PRINT_ER("Failed to start firmware\n");
2001                         ret = -EIO;
2002                         goto _fail_irq_enable_;
2003                 }
2004
2005                 wilc_bus_set_max_speed();
2006
2007                 if (g_linux_wlan->oup.wlan_cfg_get(1, WID_FIRMWARE_VERSION, 1, 0)) {
2008                         int size;
2009                         char Firmware_ver[20];
2010                         size = g_linux_wlan->oup.wlan_cfg_get_value(
2011                                         WID_FIRMWARE_VERSION,
2012                                         Firmware_ver, sizeof(Firmware_ver));
2013                         Firmware_ver[size] = '\0';
2014                         PRINT_D(INIT_DBG, "***** Firmware Ver = %s  *******\n", Firmware_ver);
2015                 }
2016                 /* Initialize firmware with default configuration */
2017                 ret = linux_wlan_init_test_config(dev, g_linux_wlan);
2018
2019                 if (ret < 0) {
2020                         PRINT_ER("Failed to configure firmware\n");
2021                         ret = -EIO;
2022                         goto _fail_fw_start_;
2023                 }
2024
2025                 g_linux_wlan->wilc1000_initialized = 1;
2026                 return 0; /*success*/
2027
2028
2029 _fail_fw_start_:
2030                 if (&g_linux_wlan->oup != NULL) {
2031                         if (g_linux_wlan->oup.wlan_stop != NULL)
2032                                 g_linux_wlan->oup.wlan_stop();
2033                 }
2034
2035 _fail_irq_enable_:
2036 #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
2037                 disable_sdio_interrupt();
2038 #endif
2039 _fail_irq_init_:
2040 #if (!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO)
2041                 deinit_irq(g_linux_wlan);
2042
2043 #endif
2044 _fail_threads_:
2045                 wlan_deinitialize_threads(g_linux_wlan);
2046 _fail_wilc_wlan_:
2047                 wilc_wlan_deinit(g_linux_wlan);
2048 _fail_locks_:
2049                 wlan_deinit_locks(g_linux_wlan);
2050                 PRINT_ER("WLAN Iinitialization FAILED\n");
2051         } else {
2052                 PRINT_D(INIT_DBG, "wilc1000 already initialized\n");
2053         }
2054         return ret;
2055 }
2056
2057
2058 /*
2059  *      - this function will be called automatically by OS when module inserted.
2060  */
2061
2062 #if !defined (NM73131_0_BOARD)
2063 int mac_init_fn(struct net_device *ndev)
2064 {
2065
2066         /*Why we do this !!!*/
2067         netif_start_queue(ndev); /* ma */
2068         netif_stop_queue(ndev); /* ma */
2069
2070         return 0;
2071 }
2072 #else
2073 int mac_init_fn(struct net_device *ndev)
2074 {
2075
2076         unsigned char mac_add[] = {0x00, 0x50, 0xc2, 0x5e, 0x10, 0x00};
2077         /* TODO: get MAC address whenever the source is EPROM - hardcoded and copy it to ndev*/
2078         memcpy(ndev->dev_addr, mac_add, 6);
2079
2080         if (!is_valid_ether_addr(ndev->dev_addr)) {
2081                 PRINT_ER("Error: Wrong MAC address\n");
2082                 return -EINVAL;
2083         }
2084
2085         return 0;
2086 }
2087 #endif
2088
2089
2090 void    WILC_WFI_frame_register(struct wiphy *wiphy, struct net_device *dev,
2091                                 u16 frame_type, bool reg);
2092
2093 /* This fn is called, when this device is setup using ifconfig */
2094 #if !defined (NM73131_0_BOARD)
2095 int mac_open(struct net_device *ndev)
2096 {
2097         perInterface_wlan_t *nic;
2098
2099         /*BugID_5213*/
2100         /*No need for setting mac address here anymore,*/
2101         /*Just set it in init_test_config()*/
2102         unsigned char mac_add[ETH_ALEN] = {0};
2103         int status;
2104         int ret = 0;
2105         int i = 0;
2106         struct WILC_WFI_priv *priv;
2107
2108         nic = netdev_priv(ndev);
2109         priv = wiphy_priv(nic->wilc_netdev->ieee80211_ptr->wiphy);
2110         PRINT_D(INIT_DBG, "MAC OPEN[%p]\n", ndev);
2111
2112         #ifdef USE_WIRELESS
2113         ret = WILC_WFI_InitHostInt(ndev);
2114         if (ret < 0) {
2115                 PRINT_ER("Failed to initialize host interface\n");
2116
2117                 return ret;
2118         }
2119         #endif
2120
2121         /*initialize platform*/
2122         PRINT_D(INIT_DBG, "*** re-init ***\n");
2123         ret = wilc1000_wlan_init(ndev, nic);
2124         if (ret < 0) {
2125                 PRINT_ER("Failed to initialize wilc1000\n");
2126                 WILC_WFI_DeInitHostInt(ndev);
2127                 return ret;
2128         }
2129
2130         Set_machw_change_vir_if(WILC_FALSE);
2131
2132         status = host_int_get_MacAddress(priv->hWILCWFIDrv, mac_add);
2133         PRINT_D(INIT_DBG, "Mac address: %x:%x:%x:%x:%x:%x\n", mac_add[0], mac_add[1], mac_add[2],
2134                 mac_add[3], mac_add[4], mac_add[5]);
2135
2136         /* loop through the NUM of supported devices and set the MAC address */
2137         for (i = 0; i < g_linux_wlan->u8NoIfcs; i++) {
2138                 if (ndev == g_linux_wlan->strInterfaceInfo[i].wilc_netdev) {
2139                         memcpy(g_linux_wlan->strInterfaceInfo[i].aSrcAddress, mac_add, ETH_ALEN);
2140                         g_linux_wlan->strInterfaceInfo[i].drvHandler = (WILC_Uint32)priv->hWILCWFIDrv;
2141                         break;
2142                 }
2143         }
2144
2145         /* TODO: get MAC address whenever the source is EPROM - hardcoded and copy it to ndev*/
2146         memcpy(ndev->dev_addr, g_linux_wlan->strInterfaceInfo[i].aSrcAddress, ETH_ALEN);
2147
2148         if (!is_valid_ether_addr(ndev->dev_addr)) {
2149                 PRINT_ER("Error: Wrong MAC address\n");
2150                 ret = -EINVAL;
2151                 goto _err_;
2152         }
2153
2154
2155         WILC_WFI_frame_register(nic->wilc_netdev->ieee80211_ptr->wiphy, nic->wilc_netdev,
2156                                 nic->g_struct_frame_reg[0].frame_type, nic->g_struct_frame_reg[0].reg);
2157         WILC_WFI_frame_register(nic->wilc_netdev->ieee80211_ptr->wiphy, nic->wilc_netdev,
2158                                 nic->g_struct_frame_reg[1].frame_type, nic->g_struct_frame_reg[1].reg);
2159         netif_wake_queue(ndev);
2160         g_linux_wlan->open_ifcs++;
2161         nic->mac_opened = 1;
2162         return 0;
2163
2164 _err_:
2165         WILC_WFI_DeInitHostInt(ndev);
2166         wilc1000_wlan_deinit(g_linux_wlan);
2167         return ret;
2168 }
2169 #else
2170 int mac_open(struct net_device *ndev)
2171 {
2172
2173         linux_wlan_t *nic;
2174         nic = netdev_priv(ndev);
2175
2176         /*initialize platform*/
2177         if (wilc1000_wlan_init(nic)) {
2178                 PRINT_ER("Failed to initialize platform\n");
2179                 return 1;
2180         }
2181         /* Start the network interface queue for this device */
2182         PRINT_D(INIT_DBG, "Starting netifQ\n");
2183         netif_start_queue(ndev);
2184 /*      linux_wlan_lock(&close_exit_sync); */
2185         return 0;
2186 }
2187 #endif
2188
2189 struct net_device_stats *mac_stats(struct net_device *dev)
2190 {
2191         perInterface_wlan_t *nic = netdev_priv(dev);
2192
2193
2194         return &nic->netstats;
2195 }
2196
2197 /* Setup the multicast filter */
2198 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
2199 static void wilc_set_multicast_list(struct net_device *dev)
2200 {
2201
2202         struct netdev_hw_addr *ha;
2203         struct WILC_WFI_priv *priv;
2204         tstrWILC_WFIDrv *pstrWFIDrv;
2205         int i = 0;
2206         priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
2207         pstrWFIDrv = (tstrWILC_WFIDrv *)priv->hWILCWFIDrv;
2208
2209
2210         if (!dev)
2211                 return;
2212
2213         PRINT_D(INIT_DBG, "Setting Multicast List with count = %d. \n", dev->mc.count);
2214
2215         if (dev->flags & IFF_PROMISC) {
2216                 /* Normally, we should configure the chip to retrive all packets
2217                  * but we don't wanna support this right now */
2218                 /* TODO: add promiscuous mode support */
2219                 PRINT_D(INIT_DBG, "Set promiscuous mode ON, retrive all packets \n");
2220                 return;
2221         }
2222
2223         /* If there's more addresses than we handle, get all multicast
2224          * packets and sort them out in software. */
2225         if ((dev->flags & IFF_ALLMULTI) || (dev->mc.count) > WILC_MULTICAST_TABLE_SIZE) {
2226                 PRINT_D(INIT_DBG, "Disable multicast filter, retrive all multicast packets\n");
2227                 /* get all multicast packets */
2228                 host_int_setup_multicast_filter((WILC_WFIDrvHandle)pstrWFIDrv, WILC_FALSE, 0);
2229                 return;
2230         }
2231
2232         /* No multicast?  Just get our own stuff */
2233         if ((dev->mc.count) == 0) {
2234                 PRINT_D(INIT_DBG, "Enable multicast filter, retrive directed packets only.\n");
2235                 host_int_setup_multicast_filter((WILC_WFIDrvHandle)pstrWFIDrv, WILC_TRUE, 0);
2236                 return;
2237         }
2238
2239         /* Store all of the multicast addresses in the hardware filter */
2240         netdev_for_each_mc_addr(ha, dev)
2241         {
2242                 WILC_memcpy(gau8MulticastMacAddrList[i], ha->addr, ETH_ALEN);
2243                 PRINT_D(INIT_DBG, "Entry[%d]: %x:%x:%x:%x:%x:%x\n", i,
2244                         gau8MulticastMacAddrList[i][0], gau8MulticastMacAddrList[i][1], gau8MulticastMacAddrList[i][2], gau8MulticastMacAddrList[i][3], gau8MulticastMacAddrList[i][4], gau8MulticastMacAddrList[i][5]);
2245                 i++;
2246         }
2247
2248         host_int_setup_multicast_filter((WILC_WFIDrvHandle)pstrWFIDrv, WILC_TRUE, (dev->mc.count));
2249
2250         return;
2251
2252 }
2253
2254 #else
2255
2256 static void wilc_set_multicast_list(struct net_device *dev)
2257 {
2258         /* BIG Warning, Beware : Uncompiled, untested... */
2259         struct dev_mc_list *mc_ptr;
2260         int i = 0;
2261
2262         if (!dev)
2263                 return;
2264
2265         PRINT_D(INIT_DBG, "Setting Multicast List. \n");
2266         PRINT_D(INIT_DBG, "dev->mc_count = %d\n", dev->mc_count);
2267
2268         if (dev->flags & IFF_PROMISC) {
2269                 /* Normally, we should configure the chip to retrive all packets
2270                  * but we don't wanna support this right now */
2271                 /* TODO: add promiscuous mode support */
2272                 PRINT_D(INIT_DBG, "Set promiscuous mode ON, retrive all packets \n");
2273                 return;
2274         }
2275
2276         /* If there's more addresses than we handle, get all multicast
2277          * packets and sort them out in software. */
2278         if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > WILC_MULTICAST_TABLE_SIZE)) {
2279                 PRINT_D(INIT_DBG, "Disable multicast filter, retrive all multicast packets\n");
2280                 host_int_setup_multicast_filter((WILC_WFIDrvHandle)gWFiDrvHandle, WILC_FALSE, 0);
2281                 return;
2282         }
2283
2284         /* No multicast?  Just get our own stuff */
2285         if (dev->mc_count == 0) {
2286                 PRINT_D(INIT_DBG, "Enable multicast filter, retrive directed packets only.\n");
2287                 host_int_setup_multicast_filter((WILC_WFIDrvHandle)gWFiDrvHandle, WILC_TRUE, 0);
2288                 return;
2289         }
2290
2291         /* Store all of the multicast addresses in the hardware filter */
2292
2293         for (mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next, i++) {
2294                 WILC_memcpy(gau8MulticastMacAddrList[i], mc_ptr->dmi_addr, ETH_ALEN)
2295                 i++;
2296         }
2297
2298         host_int_setup_multicast_filter((WILC_WFIDrvHandle)gWFiDrvHandle, WILC_TRUE, (dev->mc_count));
2299
2300 }
2301 #endif
2302
2303 static void linux_wlan_tx_complete(void *priv, int status)
2304 {
2305
2306         struct tx_complete_data *pv_data = (struct tx_complete_data *)priv;
2307         if (status == 1) {
2308                 PRINT_D(TX_DBG, "Packet sent successfully - Size = %d - Address = %p - SKB = %p\n", pv_data->size, pv_data->buff, pv_data->skb);
2309         } else {
2310                 PRINT_D(TX_DBG, "Couldn't send packet - Size = %d - Address = %p - SKB = %p\n", pv_data->size, pv_data->buff, pv_data->skb);
2311         }
2312         /* Free the SK Buffer, its work is done */
2313         dev_kfree_skb(pv_data->skb);
2314         linux_wlan_free(pv_data);
2315 }
2316
2317 int mac_xmit(struct sk_buff *skb, struct net_device *ndev)
2318 {
2319         perInterface_wlan_t *nic;
2320         struct tx_complete_data *tx_data = NULL;
2321         int QueueCount;
2322         char *pu8UdpBuffer;
2323         struct iphdr *ih;
2324         struct ethhdr *eth_h;
2325         nic = netdev_priv(ndev);
2326
2327         PRINT_D(INT_DBG, "\n========\n IntUH: %d - IntBH: %d - IntCld: %d \n========\n", int_rcvdU, int_rcvdB, int_clrd);
2328         PRINT_D(TX_DBG, "Sending packet just received from TCP/IP\n");
2329
2330         /* Stop the network interface queue */
2331         if (skb->dev != ndev) {
2332                 PRINT_ER("Packet not destined to this device\n");
2333                 return 0;
2334         }
2335
2336         tx_data = (struct tx_complete_data *)internal_alloc(sizeof(struct tx_complete_data), GFP_ATOMIC);
2337         if (tx_data == NULL) {
2338                 PRINT_ER("Failed to allocate memory for tx_data structure\n");
2339                 dev_kfree_skb(skb);
2340                 netif_wake_queue(ndev);
2341                 return 0;
2342         }
2343
2344         tx_data->buff = skb->data;
2345         tx_data->size = skb->len;
2346         tx_data->skb  = skb;
2347
2348         eth_h = (struct ethhdr *)(skb->data);
2349         if (eth_h->h_proto == 0x8e88) {
2350                 PRINT_D(INIT_DBG, "EAPOL transmitted\n");
2351         }
2352
2353         /*get source and dest ip addresses*/
2354         ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
2355
2356         pu8UdpBuffer = (char *)ih + sizeof(struct iphdr);
2357         if ((pu8UdpBuffer[1] == 68 && pu8UdpBuffer[3] == 67) || (pu8UdpBuffer[1] == 67 && pu8UdpBuffer[3] == 68)) {
2358                 PRINT_D(GENERIC_DBG, "DHCP Message transmitted, type:%x %x %x\n", pu8UdpBuffer[248], pu8UdpBuffer[249], pu8UdpBuffer[250]);
2359
2360         }
2361         PRINT_D(TX_DBG, "Sending packet - Size = %d - Address = %p - SKB = %p\n", tx_data->size, tx_data->buff, tx_data->skb);
2362
2363         /* Send packet to MAC HW - for now the tx_complete function will be just status
2364          * indicator. still not sure if I need to suspend host transmission till the tx_complete
2365          * function called or not?
2366          * allocated buffer will be freed in tx_complete function.
2367          */
2368         PRINT_D(TX_DBG, "Adding tx packet to TX Queue\n");
2369         nic->netstats.tx_packets++;
2370         nic->netstats.tx_bytes += tx_data->size;
2371         tx_data->pBssid = g_linux_wlan->strInterfaceInfo[nic->u8IfIdx].aBSSID;
2372         #ifndef WILC_FULLY_HOSTING_AP
2373         QueueCount = g_linux_wlan->oup.wlan_add_to_tx_que((void *)tx_data,
2374                                                           tx_data->buff,
2375                                                           tx_data->size,
2376                                                           linux_wlan_tx_complete);
2377         #else
2378         QueueCount = WILC_Xmit_data((void *)tx_data, HOST_TO_WLAN);
2379         #endif /* WILC_FULLY_HOSTING_AP */
2380
2381
2382         if (QueueCount > FLOW_CONTROL_UPPER_THRESHOLD) {
2383                 netif_stop_queue(g_linux_wlan->strInterfaceInfo[0].wilc_netdev);
2384                 netif_stop_queue(g_linux_wlan->strInterfaceInfo[1].wilc_netdev);
2385         }
2386
2387         return 0;
2388 }
2389
2390
2391 int mac_close(struct net_device *ndev)
2392 {
2393         struct WILC_WFI_priv *priv;
2394         perInterface_wlan_t *nic;
2395         tstrWILC_WFIDrv *pstrWFIDrv;
2396
2397         nic = netdev_priv(ndev);
2398
2399         if ((nic == NULL) || (nic->wilc_netdev == NULL) || (nic->wilc_netdev->ieee80211_ptr == NULL) || (nic->wilc_netdev->ieee80211_ptr->wiphy == NULL)) {
2400                 PRINT_ER("nic = NULL\n");
2401                 return 0;
2402         }
2403
2404         priv = wiphy_priv(nic->wilc_netdev->ieee80211_ptr->wiphy);
2405
2406         if (priv == NULL) {
2407                 PRINT_ER("priv = NULL\n");
2408                 return 0;
2409         }
2410
2411         pstrWFIDrv = (tstrWILC_WFIDrv *)priv->hWILCWFIDrv;
2412
2413
2414
2415         PRINT_D(GENERIC_DBG, "Mac close\n");
2416
2417         if (g_linux_wlan == NULL) {
2418                 PRINT_ER("g_linux_wlan = NULL\n");
2419                 return 0;
2420         }
2421
2422         if (pstrWFIDrv == NULL) {
2423                 PRINT_ER("pstrWFIDrv = NULL\n");
2424                 return 0;
2425         }
2426
2427         if ((g_linux_wlan->open_ifcs) > 0) {
2428                 g_linux_wlan->open_ifcs--;
2429         } else {
2430                 PRINT_ER("ERROR: MAC close called while number of opened interfaces is zero\n");
2431                 return 0;
2432         }
2433
2434         if (nic->wilc_netdev != NULL) {
2435                 /* Stop the network interface queue */
2436                 netif_stop_queue(nic->wilc_netdev);
2437
2438                 #ifdef USE_WIRELESS
2439                 WILC_WFI_DeInitHostInt(nic->wilc_netdev);
2440                 #endif
2441         }
2442
2443         if (g_linux_wlan->open_ifcs == 0) {
2444                 PRINT_D(GENERIC_DBG, "Deinitializing wilc1000\n");
2445                 g_linux_wlan->close = 1;
2446                 wilc1000_wlan_deinit(g_linux_wlan);
2447                 #ifdef USE_WIRELESS
2448                 #ifdef WILC_AP_EXTERNAL_MLME
2449                 WILC_WFI_deinit_mon_interface();
2450                 #endif
2451                 #endif
2452         }
2453
2454         linux_wlan_unlock(&close_exit_sync);
2455         nic->mac_opened = 0;
2456
2457         return 0;
2458 }
2459
2460
2461 int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
2462 {
2463
2464         WILC_Uint8 *buff = NULL;
2465         WILC_Sint8 rssi;
2466         WILC_Uint32 size = 0, length = 0;
2467         perInterface_wlan_t *nic;
2468         struct WILC_WFI_priv *priv;
2469         WILC_Sint32 s32Error = WILC_SUCCESS;
2470
2471
2472
2473         /* struct iwreq *wrq = (struct iwreq *) req;    // tony moved to case SIOCSIWPRIV */
2474         #ifdef USE_WIRELESS
2475         nic = netdev_priv(ndev);
2476
2477         if (!g_linux_wlan->wilc1000_initialized)
2478                 return 0;
2479
2480         #endif
2481
2482         switch (cmd) {
2483         /* [[ added by tony for SIOCDEVPRIVATE */
2484         case SIOCDEVPRIVATE + 1:
2485         {
2486                 android_wifi_priv_cmd priv_cmd;
2487
2488                 PRINT_INFO(GENERIC_DBG, "in SIOCDEVPRIVATE+1\n");
2489
2490                 if (copy_from_user(&priv_cmd, req->ifr_data, sizeof(android_wifi_priv_cmd))) {
2491                         s32Error = -EFAULT;
2492                         goto done;
2493                 }
2494
2495                 buff = kmalloc(priv_cmd.total_len, GFP_KERNEL);
2496                 if (!buff) {
2497                         s32Error = -ENOMEM;
2498                         goto done;
2499                 }
2500
2501                 if (copy_from_user(buff, priv_cmd.buf, priv_cmd.total_len)) {
2502                         s32Error = -EFAULT;
2503                         goto done;
2504                 }
2505
2506                 PRINT_INFO(GENERIC_DBG, "%s: Android private cmd \"%s\" on %s\n", __FUNCTION__, buff, req->ifr_name);
2507
2508                 if (strncasecmp(buff, "SCAN-ACTIVE", strlen("SCAN-ACTIVE")) == 0) {
2509                         PRINT_INFO(GENERIC_DBG, "%s, SCAN-ACTIVE command\n", __FUNCTION__);
2510                 } else if (strncasecmp(buff, "SCAN-PASSIVE", strlen("SCAN-PASSIVE")) == 0)  {
2511                         PRINT_INFO(GENERIC_DBG, "%s, SCAN-PASSIVE command\n", __FUNCTION__);
2512                 } else if (strncasecmp(buff, "RXFILTER-START", strlen("RXFILTER-START")) == 0)  {
2513                         PRINT_INFO(GENERIC_DBG, "%s, RXFILTER-START command\n", __FUNCTION__);
2514                 } else if (strncasecmp(buff, "RXFILTER-STOP", strlen("RXFILTER-STOP")) == 0)  {
2515                         PRINT_INFO(GENERIC_DBG, "%s, RXFILTER-STOP command\n", __FUNCTION__);
2516                 } else if (strncasecmp(buff, "RXFILTER-ADD", strlen("RXFILTER-ADD")) == 0)  {
2517                         int filter_num = *(buff + strlen("RXFILTER-ADD") + 1) - '0';
2518                         PRINT_INFO(GENERIC_DBG, "%s, RXFILTER-ADD command, filter_num=%d\n", __FUNCTION__, filter_num);
2519                 } else if (strncasecmp(buff, "RXFILTER-REMOVE", strlen("RXFILTER-REMOVE")) == 0)  {
2520                         int filter_num = *(buff + strlen("RXFILTER-REMOVE") + 1) - '0';
2521                         PRINT_INFO(GENERIC_DBG, "%s, RXFILTER-REMOVE command, filter_num=%d\n", __FUNCTION__, filter_num);
2522                 } else if (strncasecmp(buff, "BTCOEXSCAN-START", strlen("BTCOEXSCAN-START")) == 0)  {
2523                         PRINT_INFO(GENERIC_DBG, "%s, BTCOEXSCAN-START command\n", __FUNCTION__);
2524                 } else if (strncasecmp(buff, "BTCOEXSCAN-STOP", strlen("BTCOEXSCAN-STOP")) == 0)  {
2525                         PRINT_INFO(GENERIC_DBG, "%s, BTCOEXSCAN-STOP command\n", __FUNCTION__);
2526                 } else if (strncasecmp(buff, "BTCOEXMODE", strlen("BTCOEXMODE")) == 0)  {
2527                         PRINT_INFO(GENERIC_DBG, "%s, BTCOEXMODE command\n", __FUNCTION__);
2528                 } else if (strncasecmp(buff, "SETBAND", strlen("SETBAND")) == 0)  {
2529                         uint band = *(buff + strlen("SETBAND") + 1) - '0';
2530                         PRINT_INFO(GENERIC_DBG, "%s, SETBAND command, band=%d\n", __FUNCTION__, band);
2531                 } else if (strncasecmp(buff, "GETBAND", strlen("GETBAND")) == 0)  {
2532                         PRINT_INFO(GENERIC_DBG, "%s, GETBAND command\n", __FUNCTION__);
2533                 } else if (strncasecmp(buff, "COUNTRY", strlen("COUNTRY")) == 0)  {
2534                         char *country_code = buff + strlen("COUNTRY") + 1;
2535                         PRINT_INFO(GENERIC_DBG, "%s, COUNTRY command, country_code=%s\n", __FUNCTION__, country_code);
2536                 } else {
2537                         PRINT_INFO(GENERIC_DBG, "%s, Unknown command\n", __FUNCTION__);
2538                 }
2539         } break;
2540
2541         /* ]] 2013-06-24 */
2542         case SIOCSIWPRIV:
2543         {
2544                 struct iwreq *wrq = (struct iwreq *) req;               /* added by tony */
2545
2546                 size = wrq->u.data.length;
2547
2548                 if (size && wrq->u.data.pointer) {
2549                         buff = kmalloc(size, GFP_KERNEL);
2550                         if (!buff) {
2551                                 s32Error = -ENOMEM;
2552                                 goto done;
2553                         }
2554
2555                         if (copy_from_user
2556                                     (buff, wrq->u.data.pointer,
2557                                     wrq->u.data.length)) {
2558                                 s32Error = -EFAULT;
2559                                 goto done;
2560                         }
2561
2562                         if (strncasecmp(buff, "RSSI", length) == 0) {
2563
2564                                         #ifdef USE_WIRELESS
2565                                 priv = wiphy_priv(nic->wilc_netdev->ieee80211_ptr->wiphy);
2566                                 s32Error = host_int_get_rssi(priv->hWILCWFIDrv, &(rssi));
2567                                 if (s32Error)
2568                                         PRINT_ER("Failed to send get rssi param's message queue ");
2569                                         #endif
2570                                 PRINT_INFO(GENERIC_DBG, "RSSI :%d\n", rssi);
2571
2572                                 /*Rounding up the rssi negative value*/
2573                                 rssi += 5;
2574
2575                                 snprintf(buff, size, "rssi %d", rssi);
2576
2577                                 if (copy_to_user(wrq->u.data.pointer, buff, size)) {
2578                                         PRINT_ER("%s: failed to copy data to user buffer\n", __FUNCTION__);
2579                                         s32Error = -EFAULT;
2580                                         goto done;
2581                                 }
2582                         }
2583                 }
2584         }
2585         break;
2586
2587         default:
2588         {
2589                 PRINT_INFO(GENERIC_DBG, "Command - %d - has been received\n", cmd);
2590                 s32Error = -EOPNOTSUPP;
2591                 goto done;
2592         }
2593         }
2594
2595 done:
2596
2597         if (buff != NULL) {
2598                 kfree(buff);
2599         }
2600
2601         return s32Error;
2602 }
2603
2604 void frmw_to_linux(uint8_t *buff, uint32_t size, uint32_t pkt_offset)
2605 {
2606
2607         unsigned int frame_len = 0;
2608         int stats;
2609         unsigned char *buff_to_send = NULL;
2610         struct sk_buff *skb;
2611         char *pu8UdpBuffer;
2612         struct iphdr *ih;
2613         struct net_device *wilc_netdev;
2614         perInterface_wlan_t *nic;
2615
2616         wilc_netdev = GetIfHandler(buff);
2617         if (wilc_netdev == NULL)
2618                 return;
2619
2620         buff += pkt_offset;
2621         nic = netdev_priv(wilc_netdev);
2622
2623         if (size > 0) {
2624
2625                 frame_len = size;
2626                 buff_to_send = buff;
2627
2628
2629                 /* Need to send the packet up to the host, allocate a skb buffer */
2630                 skb = dev_alloc_skb(frame_len);
2631                 if (skb == NULL) {
2632                         PRINT_ER("Low memory - packet droped\n");
2633                         return;
2634                 }
2635
2636                 skb_reserve(skb, (unsigned int)skb->data & 0x3);
2637
2638                 if (g_linux_wlan == NULL || wilc_netdev == NULL) {
2639                         PRINT_ER("wilc_netdev in g_linux_wlan is NULL");
2640                 }
2641                 skb->dev = wilc_netdev;
2642
2643                 if (skb->dev == NULL) {
2644                         PRINT_ER("skb->dev is NULL\n");
2645                 }
2646
2647                 /*
2648                  * for(i=0;i<40;i++)
2649                  * {
2650                  *      if(i<frame_len)
2651                  *              WILC_PRINTF("buff_to_send[%d]=%2x\n",i,buff_to_send[i]);
2652                  *
2653                  * }*/
2654
2655                 /* skb_put(skb, frame_len); */
2656                 memcpy(skb_put(skb, frame_len), buff_to_send, frame_len);
2657
2658                 /* WILC_PRINTF("After MEM_CPY\n"); */
2659
2660                 /* nic = netdev_priv(wilc_netdev); */
2661
2662 #ifdef USE_WIRELESS
2663                 /*      if(nic->monitor_flag)
2664                  *      {
2665                  *              WILC_WFI_monitor_rx(nic->wilc_netdev,skb);
2666                  *              return;
2667                  *      }*/
2668 #endif
2669                 skb->protocol = eth_type_trans(skb, wilc_netdev);
2670                         #ifndef TCP_ENHANCEMENTS
2671                 /*get source and dest ip addresses*/
2672                 ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
2673
2674                 pu8UdpBuffer = (char *)ih + sizeof(struct iphdr);
2675                 if (buff_to_send[35] == 67 && buff_to_send[37] == 68) {
2676                         PRINT_D(RX_DBG, "DHCP Message received\n");
2677                 }
2678                 if (buff_to_send[12] == 0x88 && buff_to_send[13] == 0x8e)
2679                         PRINT_D(GENERIC_DBG, "eapol received\n");
2680                         #endif
2681                 /* Send the packet to the stack by giving it to the bridge */
2682                 nic->netstats.rx_packets++;
2683                 nic->netstats.rx_bytes += frame_len;
2684                 skb->ip_summed = CHECKSUM_UNNECESSARY;
2685                 stats = netif_rx(skb);
2686                 PRINT_D(RX_DBG, "netif_rx ret value is: %d\n", stats);
2687         }
2688                 #ifndef TCP_ENHANCEMENTS
2689         else {
2690                 PRINT_ER("Discard sending packet with len = %d\n", size);
2691         }
2692                 #endif
2693 }
2694
2695 void WILC_WFI_mgmt_rx(uint8_t *buff, uint32_t size)
2696 {
2697         int i = 0;
2698         perInterface_wlan_t *nic;
2699
2700         /*BugID_5450*/
2701         /*Pass the frame on the monitor interface, if any.*/
2702         /*Otherwise, pass it on p2p0 netdev, if registered on it*/
2703         for (i = 0; i < g_linux_wlan->u8NoIfcs; i++) {
2704                 nic = netdev_priv(g_linux_wlan->strInterfaceInfo[i].wilc_netdev);
2705                 if (nic->monitor_flag) {
2706                         WILC_WFI_monitor_rx(buff, size);
2707                         return;
2708                 }
2709         }
2710
2711         #ifdef WILC_P2P
2712         nic = netdev_priv(g_linux_wlan->strInterfaceInfo[1].wilc_netdev); /* p2p0 */
2713         if ((buff[0] == nic->g_struct_frame_reg[0].frame_type && nic->g_struct_frame_reg[0].reg) ||
2714             (buff[0] == nic->g_struct_frame_reg[1].frame_type && nic->g_struct_frame_reg[1].reg)) {
2715                 WILC_WFI_p2p_rx(g_linux_wlan->strInterfaceInfo[1].wilc_netdev, buff, size);
2716         }
2717         #endif
2718 }
2719
2720 int wilc_netdev_init(void)
2721 {
2722
2723         int i;
2724         perInterface_wlan_t *nic;
2725         struct net_device *ndev;
2726
2727         linux_wlan_init_lock("close_exit_sync", &close_exit_sync, 0);
2728
2729         /*create the common structure*/
2730         g_linux_wlan = (linux_wlan_t *)WILC_MALLOC(sizeof(linux_wlan_t));
2731         memset(g_linux_wlan, 0, sizeof(linux_wlan_t));
2732
2733         /*Reset interrupt count debug*/
2734         int_rcvdU = 0;
2735         int_rcvdB = 0;
2736         int_clrd = 0;
2737         #ifdef DISABLE_PWRSAVE_AND_SCAN_DURING_IP
2738         register_inetaddr_notifier(&g_dev_notifier);
2739         #endif
2740
2741         for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
2742                 /*allocate first ethernet device with perinterface_wlan_t as its private data*/
2743                 ndev = alloc_etherdev(sizeof(perInterface_wlan_t));
2744                 if (!ndev) {
2745                         PRINT_ER("Failed to allocate ethernet dev\n");
2746                         return -1;
2747                 }
2748
2749                 nic = netdev_priv(ndev);
2750                 memset(nic, 0, sizeof(perInterface_wlan_t));
2751
2752                 /*Name the Devices*/
2753                 if (i == 0) {
2754                 #if defined(NM73131)    /* tony, 2012-09-20 */
2755                         strcpy(ndev->name, "wilc_eth%d");
2756                 #elif defined(PLAT_CLM9722)                     /* rachel */
2757                         strcpy(ndev->name, "eth%d");
2758                 #else /* PANDA_BOARD, PLAT_ALLWINNER_A10, PLAT_ALLWINNER_A20, PLAT_ALLWINNER_A31, PLAT_AML8726_M3 or PLAT_WMS8304 */
2759                         strcpy(ndev->name, "wlan%d");
2760                 #endif
2761                 } else
2762                         strcpy(ndev->name, "p2p%d");
2763
2764                 nic->u8IfIdx = g_linux_wlan->u8NoIfcs;
2765                 nic->wilc_netdev = ndev;
2766                 g_linux_wlan->strInterfaceInfo[g_linux_wlan->u8NoIfcs].wilc_netdev = ndev;
2767                 g_linux_wlan->u8NoIfcs++;
2768                 wilc_set_netdev_ops(ndev);
2769
2770                 #ifdef USE_WIRELESS
2771                 {
2772                         struct wireless_dev *wdev;
2773                         /*Register WiFi*/
2774                         wdev = WILC_WFI_WiphyRegister(ndev);
2775
2776                         #ifdef WILC_SDIO
2777                         /* set netdev, tony */
2778                         SET_NETDEV_DEV(ndev, &local_sdio_func->dev);
2779                         #endif
2780
2781                         if (wdev == NULL) {
2782                                 PRINT_ER("Can't register WILC Wiphy\n");
2783                                 return -1;
2784                         }
2785
2786                         /*linking the wireless_dev structure with the netdevice*/
2787                         nic->wilc_netdev->ieee80211_ptr = wdev;
2788                         nic->wilc_netdev->ml_priv = nic;
2789                         wdev->netdev = nic->wilc_netdev;
2790                         nic->netstats.rx_packets = 0;
2791                         nic->netstats.tx_packets = 0;
2792                         nic->netstats.rx_bytes = 0;
2793                         nic->netstats.tx_bytes = 0;
2794
2795                 }
2796                 #endif
2797
2798
2799                 if (register_netdev(ndev)) {
2800                         PRINT_ER("Device couldn't be registered - %s\n", ndev->name);
2801                         return -1; /* ERROR */
2802                 }
2803
2804                 nic->iftype = STATION_MODE;
2805                 nic->mac_opened = 0;
2806
2807         }
2808
2809         #ifndef WILC_SDIO
2810         if (!linux_spi_init(&g_linux_wlan->wilc_spidev)) {
2811                 PRINT_ER("Can't initialize SPI \n");
2812                 return -1; /* ERROR */
2813         }
2814         g_linux_wlan->wilc_spidev = wilc_spi_dev;
2815         #else
2816         g_linux_wlan->wilc_sdio_func = local_sdio_func;
2817         #endif
2818
2819         return 0;
2820 }
2821
2822
2823 /*The 1st function called after module inserted*/
2824 static int __init init_wilc_driver(void)
2825 {
2826
2827
2828 #if defined (WILC_DEBUGFS)
2829         if (wilc_debugfs_init() < 0) {
2830                 PRINT_D(GENERIC_DBG, "fail to create debugfs for wilc driver\n");
2831                 return -1;
2832         }
2833 #endif
2834
2835         printk("IN INIT FUNCTION\n");
2836         printk("*** WILC1000 driver VERSION=[%s] FW_VER=[%s] ***\n", __DRIVER_VERSION__, __DRIVER_VERSION__);
2837
2838         linux_wlan_device_power(1);
2839         msleep(100);
2840         linux_wlan_device_detection(1);
2841
2842 #ifdef WILC_SDIO
2843         {
2844                 int ret;
2845
2846                 ret = sdio_register_driver(&wilc_bus);
2847                 if (ret < 0) {
2848                         PRINT_D(INIT_DBG, "init_wilc_driver: Failed register sdio driver\n");
2849                 }
2850
2851                 return ret;
2852         }
2853 #else
2854         PRINT_D(INIT_DBG, "Initializing netdev\n");
2855         if (wilc_netdev_init()) {
2856                 PRINT_ER("Couldn't initialize netdev\n");
2857         }
2858         return 0;
2859 #endif
2860 }
2861 late_initcall(init_wilc_driver);
2862
2863 static void __exit exit_wilc_driver(void)
2864 {
2865         int i = 0;
2866         perInterface_wlan_t *nic[NUM_CONCURRENT_IFC];
2867         #define CLOSE_TIMEOUT (12 * 1000)
2868
2869         if ((g_linux_wlan != NULL) && (((g_linux_wlan->strInterfaceInfo[0].wilc_netdev) != NULL)
2870                                        || ((g_linux_wlan->strInterfaceInfo[1].wilc_netdev) != NULL))) {
2871         #ifdef DISABLE_PWRSAVE_AND_SCAN_DURING_IP
2872                 unregister_inetaddr_notifier(&g_dev_notifier);
2873         #endif
2874
2875                 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
2876                         nic[i] = netdev_priv(g_linux_wlan->strInterfaceInfo[i].wilc_netdev);
2877                 }
2878         }
2879
2880
2881         if ((g_linux_wlan != NULL) && g_linux_wlan->wilc_firmware != NULL) {
2882                 release_firmware(g_linux_wlan->wilc_firmware);
2883                 g_linux_wlan->wilc_firmware = NULL;
2884         }
2885
2886
2887         if ((g_linux_wlan != NULL) && (((g_linux_wlan->strInterfaceInfo[0].wilc_netdev) != NULL)
2888                                        || ((g_linux_wlan->strInterfaceInfo[1].wilc_netdev) != NULL))) {
2889                 PRINT_D(INIT_DBG, "Waiting for mac_close ....\n");
2890
2891                 if (linux_wlan_lock_timeout(&close_exit_sync, CLOSE_TIMEOUT) < 0)
2892                         PRINT_D(INIT_DBG, "Closed TimedOUT\n");
2893                 else
2894                         PRINT_D(INIT_DBG, "mac_closed\n");
2895
2896
2897                 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
2898                         /* close all opened interfaces */
2899                         if (g_linux_wlan->strInterfaceInfo[i].wilc_netdev != NULL) {
2900                                 if (nic[i]->mac_opened) {
2901                                         mac_close(g_linux_wlan->strInterfaceInfo[i].wilc_netdev);
2902                                 }
2903                         }
2904                 }
2905                 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
2906                         PRINT_D(INIT_DBG, "Unregistering netdev %p \n", g_linux_wlan->strInterfaceInfo[i].wilc_netdev);
2907                         unregister_netdev(g_linux_wlan->strInterfaceInfo[i].wilc_netdev);
2908                         #ifdef USE_WIRELESS
2909                         PRINT_D(INIT_DBG, "Freeing Wiphy...\n");
2910                         WILC_WFI_WiphyFree(g_linux_wlan->strInterfaceInfo[i].wilc_netdev);
2911                         #endif
2912                         PRINT_D(INIT_DBG, "Freeing netdev...\n");
2913                         free_netdev(g_linux_wlan->strInterfaceInfo[i].wilc_netdev);
2914                 }
2915         }
2916
2917
2918 #ifdef USE_WIRELESS
2919 #ifdef WILC_AP_EXTERNAL_MLME
2920         /* Bug 4600 : WILC_WFI_deinit_mon_interface was already called at mac_close */
2921         /* WILC_WFI_deinit_mon_interface(); */
2922 #endif
2923 #endif
2924
2925         /* if(g_linux_wlan->open_ifcs==0) */
2926         {
2927         #ifndef WILC_SDIO
2928                 PRINT_D(INIT_DBG, "SPI unregsiter...\n");
2929                 spi_unregister_driver(&wilc_bus);
2930         #else
2931                 PRINT_D(INIT_DBG, "SDIO unregsiter...\n");
2932                 sdio_unregister_driver(&wilc_bus);
2933         #endif
2934
2935                 linux_wlan_deinit_lock(&close_exit_sync);
2936                 if (g_linux_wlan != NULL) {
2937                         WILC_FREE(g_linux_wlan);
2938                         g_linux_wlan = NULL;
2939                 }
2940                 printk("Module_exit Done.\n");
2941
2942 #if defined (WILC_DEBUGFS)
2943                 wilc_debugfs_remove();
2944 #endif
2945
2946                 linux_wlan_device_detection(0);
2947                 linux_wlan_device_power(0);
2948         }
2949 }
2950 module_exit(exit_wilc_driver);
2951
2952 MODULE_LICENSE("GPL");
2953 #endif