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