OSDN Git Service

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