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