11e26fb1090b6fa2f0d71b47ae89454ebdf33670
[firefly-linux-kernel-4.4.55.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
6 #include <linux/slab.h>
7 #include <linux/sched.h>
8 #include <linux/delay.h>
9 #include <linux/workqueue.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/gpio.h>
13
14 #include <linux/kthread.h>
15 #include <linux/firmware.h>
16 #include <linux/delay.h>
17
18 #include <linux/init.h>
19 #include <linux/netdevice.h>
20 #include <linux/inetdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/skbuff.h>
25
26 #include <linux/version.h>
27 #include <linux/semaphore.h>
28
29 #ifdef WILC_SDIO
30 #include "linux_wlan_sdio.h"
31 #else
32 #include "linux_wlan_spi.h"
33 #endif
34
35 #if defined(CUSTOMER_PLATFORM)
36 /*
37  TODO : Write power control functions as customer platform.
38  */
39 #else
40
41  #define _linux_wlan_device_power_on()          {}
42  #define _linux_wlan_device_power_off()         {}
43
44  #define _linux_wlan_device_detection()         {}
45  #define _linux_wlan_device_removal()           {}
46 #endif
47
48 extern bool g_obtainingIP;
49 extern u16 Set_machw_change_vir_if(bool bValue);
50 extern void resolve_disconnect_aberration(void *drvHandler);
51 extern u8 gau8MulticastMacAddrList[WILC_MULTICAST_TABLE_SIZE][ETH_ALEN];
52 void wilc1000_wlan_deinit(struct wilc *nic);
53 extern struct timer_list hDuringIpTimer;
54
55 static int linux_wlan_device_power(int on_off)
56 {
57         PRINT_D(INIT_DBG, "linux_wlan_device_power.. (%d)\n", on_off);
58
59         if (on_off) {
60                 _linux_wlan_device_power_on();
61         } else {
62                 _linux_wlan_device_power_off();
63         }
64
65         return 0;
66 }
67
68 static int linux_wlan_device_detection(int on_off)
69 {
70         PRINT_D(INIT_DBG, "linux_wlan_device_detection.. (%d)\n", on_off);
71
72 #ifdef WILC_SDIO
73         if (on_off) {
74                 _linux_wlan_device_detection();
75         } else {
76                 _linux_wlan_device_removal();
77         }
78 #endif
79
80         return 0;
81 }
82
83 static int dev_state_ev_handler(struct notifier_block *this, unsigned long event, void *ptr);
84
85 static struct notifier_block g_dev_notifier = {
86         .notifier_call = dev_state_ev_handler
87 };
88
89 #define IRQ_WAIT        1
90 #define IRQ_NO_WAIT     0
91 /*
92  *      to sync between mac_close and module exit.
93  *      don't initialize or de-initialize from init/deinitlocks
94  *      to be initialized from module wilc_netdev_init and
95  *      deinitialized from mdoule_exit
96  */
97 static struct semaphore close_exit_sync;
98
99 static int wlan_deinit_locks(struct wilc *nic);
100 static void wlan_deinitialize_threads(struct wilc *nic);
101 extern void WILC_WFI_monitor_rx(u8 *buff, u32 size);
102 extern void WILC_WFI_p2p_rx(struct net_device *dev, u8 *buff, u32 size);
103
104 static void linux_wlan_tx_complete(void *priv, int status);
105 static int  mac_init_fn(struct net_device *ndev);
106 int  mac_xmit(struct sk_buff *skb, struct net_device *dev);
107 int  mac_open(struct net_device *ndev);
108 int  mac_close(struct net_device *ndev);
109 static struct net_device_stats *mac_stats(struct net_device *dev);
110 static int  mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd);
111 static void wilc_set_multicast_list(struct net_device *dev);
112
113 /*
114  * for now - in frmw_to_linux there should be private data to be passed to it
115  * and this data should be pointer to net device
116  */
117 struct wilc *g_linux_wlan;
118 bool bEnablePS = true;
119
120 static const struct net_device_ops wilc_netdev_ops = {
121         .ndo_init = mac_init_fn,
122         .ndo_open = mac_open,
123         .ndo_stop = mac_close,
124         .ndo_start_xmit = mac_xmit,
125         .ndo_do_ioctl = mac_ioctl,
126         .ndo_get_stats = mac_stats,
127         .ndo_set_rx_mode  = wilc_set_multicast_list,
128
129 };
130
131 static int dev_state_ev_handler(struct notifier_block *this, unsigned long event, void *ptr)
132 {
133         struct in_ifaddr *dev_iface = (struct in_ifaddr *)ptr;
134         struct wilc_priv *priv;
135         struct host_if_drv *pstrWFIDrv;
136         struct net_device *dev;
137         u8 *pIP_Add_buff;
138         perInterface_wlan_t *nic;
139         u8 null_ip[4] = {0};
140         char wlan_dev_name[5] = "wlan0";
141
142         if (dev_iface == NULL || dev_iface->ifa_dev == NULL || dev_iface->ifa_dev->dev == NULL) {
143                 PRINT_D(GENERIC_DBG, "dev_iface = NULL\n");
144                 return NOTIFY_DONE;
145         }
146
147         if ((memcmp(dev_iface->ifa_label, "wlan0", 5)) && (memcmp(dev_iface->ifa_label, "p2p0", 4))) {
148                 PRINT_D(GENERIC_DBG, "Interface is neither WLAN0 nor P2P0\n");
149                 return NOTIFY_DONE;
150         }
151
152         dev  = (struct net_device *)dev_iface->ifa_dev->dev;
153         if (dev->ieee80211_ptr == NULL || dev->ieee80211_ptr->wiphy == NULL) {
154                 PRINT_D(GENERIC_DBG, "No Wireless registerd\n");
155                 return NOTIFY_DONE;
156         }
157         priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
158         if (priv == NULL) {
159                 PRINT_D(GENERIC_DBG, "No Wireless Priv\n");
160                 return NOTIFY_DONE;
161         }
162         pstrWFIDrv = (struct host_if_drv *)priv->hWILCWFIDrv;
163         nic = netdev_priv(dev);
164         if (nic == NULL || pstrWFIDrv == NULL) {
165                 PRINT_D(GENERIC_DBG, "No Wireless Priv\n");
166                 return NOTIFY_DONE;
167         }
168
169         PRINT_INFO(GENERIC_DBG, "dev_state_ev_handler +++\n"); /* tony */
170
171         switch (event) {
172         case NETDEV_UP:
173                 PRINT_D(GENERIC_DBG, "dev_state_ev_handler event=NETDEV_UP %p\n", dev);       /* tony */
174
175                 PRINT_INFO(GENERIC_DBG, "\n ============== IP Address Obtained ===============\n\n");
176
177                 /*If we are in station mode or client mode*/
178                 if (nic->iftype == STATION_MODE || nic->iftype == CLIENT_MODE) {
179                         pstrWFIDrv->IFC_UP = 1;
180                         g_obtainingIP = false;
181                         del_timer(&hDuringIpTimer);
182                         PRINT_D(GENERIC_DBG, "IP obtained , enable scan\n");
183                 }
184
185                 if (bEnablePS)
186                         host_int_set_power_mgmt(pstrWFIDrv, 1, 0);
187
188                 PRINT_D(GENERIC_DBG, "[%s] Up IP\n", dev_iface->ifa_label);
189
190                 pIP_Add_buff = (char *) (&(dev_iface->ifa_address));
191                 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]);
192                 host_int_setup_ipaddress(pstrWFIDrv, pIP_Add_buff, nic->u8IfIdx);
193
194                 break;
195
196         case NETDEV_DOWN:
197                 PRINT_D(GENERIC_DBG, "dev_state_ev_handler event=NETDEV_DOWN %p\n", dev);               /* tony */
198
199                 PRINT_INFO(GENERIC_DBG, "\n ============== IP Address Released ===============\n\n");
200                 if (nic->iftype == STATION_MODE || nic->iftype == CLIENT_MODE) {
201                         pstrWFIDrv->IFC_UP = 0;
202                         g_obtainingIP = false;
203                 }
204
205                 if (memcmp(dev_iface->ifa_label, wlan_dev_name, 5) == 0)
206                         host_int_set_power_mgmt(pstrWFIDrv, 0, 0);
207
208                 resolve_disconnect_aberration(pstrWFIDrv);
209
210                 PRINT_D(GENERIC_DBG, "[%s] Down IP\n", dev_iface->ifa_label);
211
212                 pIP_Add_buff = null_ip;
213                 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]);
214
215                 host_int_setup_ipaddress(pstrWFIDrv, pIP_Add_buff, nic->u8IfIdx);
216
217                 break;
218
219         default:
220                 PRINT_INFO(GENERIC_DBG, "dev_state_ev_handler event=default\n");        /* tony */
221                 PRINT_INFO(GENERIC_DBG, "[%s] unknown dev event: %lu\n", dev_iface->ifa_label, event);
222
223                 break;
224         }
225
226         return NOTIFY_DONE;
227
228 }
229
230 #if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
231 static irqreturn_t isr_uh_routine(int irq, void *user_data)
232 {
233         PRINT_D(INT_DBG, "Interrupt received UH\n");
234
235         /*While mac is closing cacncel the handling of any interrupts received*/
236         if (g_linux_wlan->close) {
237                 PRINT_ER("Driver is CLOSING: Can't handle UH interrupt\n");
238                 return IRQ_HANDLED;
239         }
240         return IRQ_WAKE_THREAD;
241 }
242 #endif
243
244 irqreturn_t isr_bh_routine(int irq, void *userdata)
245 {
246         struct wilc *nic;
247
248         nic = (struct wilc *)userdata;
249
250         /*While mac is closing cacncel the handling of any interrupts received*/
251         if (g_linux_wlan->close) {
252                 PRINT_ER("Driver is CLOSING: Can't handle BH interrupt\n");
253                 return IRQ_HANDLED;
254         }
255
256         PRINT_D(INT_DBG, "Interrupt received BH\n");
257         wilc_handle_isr();
258
259         return IRQ_HANDLED;
260 }
261
262 #if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
263 static int init_irq(struct wilc *p_nic)
264 {
265         int ret = 0;
266         struct wilc *nic = p_nic;
267
268         /*initialize GPIO and register IRQ num*/
269         /*GPIO request*/
270         if ((gpio_request(GPIO_NUM, "WILC_INTR") == 0) &&
271             (gpio_direction_input(GPIO_NUM) == 0)) {
272 #if defined(CUSTOMER_PLATFORM)
273 /*
274  TODO : save the registerd irq number to the private wilc context in kernel.
275  *
276  * ex) nic->dev_irq_num = gpio_to_irq(GPIO_NUM);
277  */
278 #else
279                 nic->dev_irq_num = gpio_to_irq(GPIO_NUM);
280 #endif
281         } else {
282                 ret = -1;
283                 PRINT_ER("could not obtain gpio for WILC_INTR\n");
284         }
285
286         if ((ret != -1) && (request_threaded_irq(nic->dev_irq_num, isr_uh_routine, isr_bh_routine,
287                                                   IRQF_TRIGGER_LOW | IRQF_ONESHOT,               /*Without IRQF_ONESHOT the uh will remain kicked in and dont gave a chance to bh*/
288                                                   "WILC_IRQ", nic)) < 0) {
289
290                 PRINT_ER("Failed to request IRQ for GPIO: %d\n", GPIO_NUM);
291                 ret = -1;
292         } else {
293
294                 PRINT_D(INIT_DBG, "IRQ request succeeded IRQ-NUM= %d on GPIO: %d\n",
295                         nic->dev_irq_num, GPIO_NUM);
296         }
297
298         return ret;
299 }
300 #endif
301
302 static void deinit_irq(struct wilc *nic)
303 {
304 #if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
305         /* Deintialize IRQ */
306         if (&nic->dev_irq_num != 0) {
307                 free_irq(nic->dev_irq_num, g_linux_wlan);
308
309                 gpio_free(GPIO_NUM);
310         }
311 #endif
312 }
313
314 /*
315  *      OS functions
316  */
317 void linux_wlan_dbg(u8 *buff)
318 {
319         PRINT_D(INIT_DBG, "%d\n", *buff);
320 }
321
322 int linux_wlan_lock_timeout(void *vp, u32 timeout)
323 {
324         int error = -1;
325
326         PRINT_D(LOCK_DBG, "Locking %p\n", vp);
327         if (vp != NULL)
328                 error = down_timeout((struct semaphore *)vp, msecs_to_jiffies(timeout));
329         else
330                 PRINT_ER("Failed, mutex is NULL\n");
331         return error;
332 }
333
334 void linux_wlan_mac_indicate(int flag)
335 {
336         /*I have to do it that way becuase there is no mean to encapsulate device pointer
337          * as a parameter
338          */
339         struct wilc *pd = g_linux_wlan;
340         int status;
341
342         if (flag == WILC_MAC_INDICATE_STATUS) {
343                 wilc_wlan_cfg_get_val(WID_STATUS, (unsigned char *)&status, 4);
344                 if (pd->mac_status == WILC_MAC_STATUS_INIT) {
345                         pd->mac_status = status;
346                         up(&pd->sync_event);
347                 } else {
348                         pd->mac_status = status;
349                 }
350
351                 if (pd->mac_status == WILC_MAC_STATUS_CONNECT) {        /* Connect */
352                 }
353
354         } else if (flag == WILC_MAC_INDICATE_SCAN) {
355                 PRINT_D(GENERIC_DBG, "Scanning ...\n");
356
357         }
358
359 }
360
361 struct net_device *GetIfHandler(u8 *pMacHeader)
362 {
363         u8 *Bssid, *Bssid1;
364         int i = 0;
365
366         Bssid  = pMacHeader + 10;
367         Bssid1 = pMacHeader + 4;
368
369         for (i = 0; i < g_linux_wlan->vif_num; i++)
370                 if (!memcmp(Bssid1, g_linux_wlan->vif[i].bssid, ETH_ALEN) ||
371                     !memcmp(Bssid, g_linux_wlan->vif[i].bssid, ETH_ALEN))
372                         return g_linux_wlan->vif[i].wilc_netdev;
373
374         PRINT_INFO(INIT_DBG, "Invalide handle\n");
375         for (i = 0; i < 25; i++)
376                 PRINT_D(INIT_DBG, "%02x ", pMacHeader[i]);
377         Bssid  = pMacHeader + 18;
378         Bssid1 = pMacHeader + 12;
379         for (i = 0; i < g_linux_wlan->vif_num; i++)
380                 if (!memcmp(Bssid1, g_linux_wlan->vif[i].bssid, ETH_ALEN) ||
381                     !memcmp(Bssid, g_linux_wlan->vif[i].bssid, ETH_ALEN))
382                         return g_linux_wlan->vif[i].wilc_netdev;
383
384         PRINT_INFO(INIT_DBG, "\n");
385         return NULL;
386 }
387
388 int linux_wlan_set_bssid(struct net_device *wilc_netdev, u8 *pBSSID)
389 {
390         int i = 0;
391         int ret = -1;
392
393         for (i = 0; i < g_linux_wlan->vif_num; i++)
394                 if (g_linux_wlan->vif[i].wilc_netdev == wilc_netdev) {
395                         memcpy(g_linux_wlan->vif[i].bssid, pBSSID, 6);
396                         ret = 0;
397                         break;
398                 }
399
400         return ret;
401 }
402
403 /*Function to get number of connected interfaces*/
404 int linux_wlan_get_num_conn_ifcs(void)
405 {
406         u8 i = 0;
407         u8 null_bssid[6] = {0};
408         u8 ret_val = 0;
409
410         for (i = 0; i < g_linux_wlan->vif_num; i++)
411                 if (memcmp(g_linux_wlan->vif[i].bssid, null_bssid, 6))
412                         ret_val++;
413
414         return ret_val;
415 }
416
417 #define USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
418
419 static int linux_wlan_txq_task(void *vp)
420 {
421         int ret, txq_count;
422
423 #if defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
424 #define TX_BACKOFF_WEIGHT_INCR_STEP (1)
425 #define TX_BACKOFF_WEIGHT_DECR_STEP (1)
426 #define TX_BACKOFF_WEIGHT_MAX (7)
427 #define TX_BACKOFF_WEIGHT_MIN (0)
428 #define TX_BACKOFF_WEIGHT_UNIT_MS (10)
429         int backoff_weight = TX_BACKOFF_WEIGHT_MIN;
430 #endif
431
432         /* inform wilc1000_wlan_init that TXQ task is started. */
433         up(&g_linux_wlan->txq_thread_started);
434         while (1) {
435
436                 PRINT_D(TX_DBG, "txq_task Taking a nap :)\n");
437                 down(&g_linux_wlan->txq_event);
438                 /* wait_for_completion(&pd->txq_event); */
439                 PRINT_D(TX_DBG, "txq_task Who waked me up :$\n");
440
441                 if (g_linux_wlan->close) {
442                         /*Unlock the mutex in the mac_close function to indicate the exiting of the TX thread */
443                         up(&g_linux_wlan->txq_thread_started);
444
445                         while (!kthread_should_stop())
446                                 schedule();
447
448                         PRINT_D(TX_DBG, "TX thread stopped\n");
449                         break;
450                 }
451                 PRINT_D(TX_DBG, "txq_task handle the sending packet and let me go to sleep.\n");
452 #if !defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
453                 ret = wilc_wlan_handle_txq(&txq_count);
454 #else
455                 do {
456                         ret = wilc_wlan_handle_txq(&txq_count);
457                         if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD /* && netif_queue_stopped(pd->wilc_netdev)*/) {
458                                 PRINT_D(TX_DBG, "Waking up queue\n");
459                                 /* netif_wake_queue(pd->wilc_netdev); */
460                                 if (netif_queue_stopped(g_linux_wlan->vif[0].wilc_netdev))
461                                         netif_wake_queue(g_linux_wlan->vif[0].wilc_netdev);
462                                 if (netif_queue_stopped(g_linux_wlan->vif[1].wilc_netdev))
463                                         netif_wake_queue(g_linux_wlan->vif[1].wilc_netdev);
464                         }
465
466                         if (ret == WILC_TX_ERR_NO_BUF) { /* failed to allocate buffers in chip. */
467                                 do {
468                                         /* Back off from sending packets for some time. */
469                                         /* schedule_timeout will allow RX task to run and free buffers.*/
470                                         /* set_current_state(TASK_UNINTERRUPTIBLE); */
471                                         /* timeout = schedule_timeout(timeout); */
472                                         msleep(TX_BACKOFF_WEIGHT_UNIT_MS << backoff_weight);
473                                 } while (/*timeout*/ 0);
474                                 backoff_weight += TX_BACKOFF_WEIGHT_INCR_STEP;
475                                 if (backoff_weight > TX_BACKOFF_WEIGHT_MAX)
476                                         backoff_weight = TX_BACKOFF_WEIGHT_MAX;
477                         } else {
478                                 if (backoff_weight > TX_BACKOFF_WEIGHT_MIN) {
479                                         backoff_weight -= TX_BACKOFF_WEIGHT_DECR_STEP;
480                                         if (backoff_weight < TX_BACKOFF_WEIGHT_MIN)
481                                                 backoff_weight = TX_BACKOFF_WEIGHT_MIN;
482                                 }
483                         }
484                         /*TODO: drop packets after a certain time/number of retry count. */
485                 } while (ret == WILC_TX_ERR_NO_BUF && !g_linux_wlan->close); /* retry sending packets if no more buffers in chip. */
486 #endif
487         }
488         return 0;
489 }
490
491 void linux_wlan_rx_complete(void)
492 {
493         PRINT_D(RX_DBG, "RX completed\n");
494 }
495
496 int linux_wlan_get_firmware(perInterface_wlan_t *p_nic)
497 {
498
499         perInterface_wlan_t *nic = p_nic;
500         int ret = 0;
501         const struct firmware *wilc_firmware;
502         char *firmware;
503
504         if (nic->iftype == AP_MODE)
505                 firmware = AP_FIRMWARE;
506         else if (nic->iftype == STATION_MODE)
507                 firmware = STA_FIRMWARE;
508
509         else {
510                 PRINT_D(INIT_DBG, "Get P2P_CONCURRENCY_FIRMWARE\n");
511                 firmware = P2P_CONCURRENCY_FIRMWARE;
512         }
513
514         if (nic == NULL) {
515                 PRINT_ER("NIC is NULL\n");
516                 goto _fail_;
517         }
518
519         if (&nic->wilc_netdev->dev == NULL) {
520                 PRINT_ER("&nic->wilc_netdev->dev  is NULL\n");
521                 goto _fail_;
522         }
523
524         /*      the firmare should be located in /lib/firmware in
525          *      root file system with the name specified above */
526
527 #ifdef WILC_SDIO
528         if (request_firmware(&wilc_firmware, firmware, &g_linux_wlan->wilc_sdio_func->dev) != 0) {
529                 PRINT_ER("%s - firmare not available\n", firmware);
530                 ret = -1;
531                 goto _fail_;
532         }
533 #else
534         if (request_firmware(&wilc_firmware, firmware, &g_linux_wlan->wilc_spidev->dev) != 0) {
535                 PRINT_ER("%s - firmare not available\n", firmware);
536                 ret = -1;
537                 goto _fail_;
538         }
539 #endif
540         g_linux_wlan->wilc_firmware = wilc_firmware;
541
542 _fail_:
543
544         return ret;
545
546 }
547
548 #ifdef COMPLEMENT_BOOT
549 int repeat_power_cycle(perInterface_wlan_t *nic);
550 #endif
551
552 static int linux_wlan_start_firmware(perInterface_wlan_t *nic)
553 {
554
555         int ret = 0;
556         /* start firmware */
557         PRINT_D(INIT_DBG, "Starting Firmware ...\n");
558         ret = wilc_wlan_start();
559         if (ret < 0) {
560                 PRINT_ER("Failed to start Firmware\n");
561                 goto _fail_;
562         }
563
564         /* wait for mac ready */
565         PRINT_D(INIT_DBG, "Waiting for Firmware to get ready ...\n");
566         ret = linux_wlan_lock_timeout(&g_linux_wlan->sync_event, 5000);
567         if (ret) {
568 #ifdef COMPLEMENT_BOOT
569                 static int timeout = 5;
570
571                 if (timeout--) {
572                         PRINT_D(INIT_DBG, "repeat power cycle[%d]", timeout);
573                         ret = repeat_power_cycle(nic);
574                 } else {
575                         timeout = 5;
576                         ret = -1;
577                         goto _fail_;
578                 }
579 #endif
580                 PRINT_D(INIT_DBG, "Firmware start timed out");
581                 goto _fail_;
582         }
583         /*
584          *      TODO: Driver shouoldn't wait forever for firmware to get started -
585          *      in case of timeout this should be handled properly
586          */
587         PRINT_D(INIT_DBG, "Firmware successfully started\n");
588
589 _fail_:
590         return ret;
591 }
592 static int linux_wlan_firmware_download(struct wilc *p_nic)
593 {
594
595         int ret = 0;
596
597         if (g_linux_wlan->wilc_firmware == NULL) {
598                 PRINT_ER("Firmware buffer is NULL\n");
599                 ret = -ENOBUFS;
600                 goto _FAIL_;
601         }
602         /**
603          *      do the firmware download
604          **/
605         PRINT_D(INIT_DBG, "Downloading Firmware ...\n");
606         ret = wilc_wlan_firmware_download(g_linux_wlan->wilc_firmware->data,
607                                           g_linux_wlan->wilc_firmware->size);
608         if (ret < 0)
609                 goto _FAIL_;
610
611         /* Freeing FW buffer */
612         PRINT_D(INIT_DBG, "Freeing FW buffer ...\n");
613         PRINT_D(INIT_DBG, "Releasing firmware\n");
614         release_firmware(g_linux_wlan->wilc_firmware);
615         g_linux_wlan->wilc_firmware = NULL;
616
617         PRINT_D(INIT_DBG, "Download Succeeded\n");
618
619 _FAIL_:
620         return ret;
621 }
622
623 /* startup configuration - could be changed later using iconfig*/
624 static int linux_wlan_init_test_config(struct net_device *dev, struct wilc *p_nic)
625 {
626
627         unsigned char c_val[64];
628         unsigned char mac_add[] = {0x00, 0x80, 0xC2, 0x5E, 0xa2, 0xff};
629
630         struct wilc_priv *priv;
631         struct host_if_drv *pstrWFIDrv;
632
633         PRINT_D(TX_DBG, "Start configuring Firmware\n");
634         get_random_bytes(&mac_add[5], 1);
635         get_random_bytes(&mac_add[4], 1);
636         priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
637         pstrWFIDrv = (struct host_if_drv *)priv->hWILCWFIDrv;
638         PRINT_D(INIT_DBG, "Host = %p\n", pstrWFIDrv);
639
640         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]);
641         wilc_get_chipid(0);
642
643         *(int *)c_val = 1;
644
645         if (!wilc_wlan_cfg_set(1, WID_SET_DRV_HANDLER, c_val, 4, 0, 0))
646                 goto _fail_;
647
648         /*to tell fw that we are going to use PC test - WILC specific*/
649         c_val[0] = 0;
650         if (!wilc_wlan_cfg_set(0, WID_PC_TEST_MODE, c_val, 1, 0, 0))
651                 goto _fail_;
652
653         c_val[0] = INFRASTRUCTURE;
654         if (!wilc_wlan_cfg_set(0, WID_BSS_TYPE, c_val, 1, 0, 0))
655                 goto _fail_;
656
657         /* c_val[0] = RATE_AUTO; */
658         c_val[0] = RATE_AUTO;
659         if (!wilc_wlan_cfg_set(0, WID_CURRENT_TX_RATE, c_val, 1, 0, 0))
660                 goto _fail_;
661
662         c_val[0] = G_MIXED_11B_2_MODE;
663         if (!wilc_wlan_cfg_set(0, WID_11G_OPERATING_MODE, c_val, 1, 0, 0))
664                 goto _fail_;
665
666         c_val[0] = 1;
667         if (!wilc_wlan_cfg_set(0, WID_CURRENT_CHANNEL, c_val, 1, 0, 0))
668                 goto _fail_;
669
670         c_val[0] = G_SHORT_PREAMBLE;
671         if (!wilc_wlan_cfg_set(0, WID_PREAMBLE, c_val, 1, 0, 0))
672                 goto _fail_;
673
674         c_val[0] = AUTO_PROT;
675         if (!wilc_wlan_cfg_set(0, WID_11N_PROT_MECH, c_val, 1, 0, 0))
676                 goto _fail_;
677
678         c_val[0] = ACTIVE_SCAN;
679         if (!wilc_wlan_cfg_set(0, WID_SCAN_TYPE, c_val, 1, 0, 0))
680                 goto _fail_;
681
682         c_val[0] = SITE_SURVEY_OFF;
683         if (!wilc_wlan_cfg_set(0, WID_SITE_SURVEY, c_val, 1, 0, 0))
684                 goto _fail_;
685
686         *((int *)c_val) = 0xffff; /* Never use RTS-CTS */
687         if (!wilc_wlan_cfg_set(0, WID_RTS_THRESHOLD, c_val, 2, 0, 0))
688                 goto _fail_;
689
690         *((int *)c_val) = 2346;
691         if (!wilc_wlan_cfg_set(0, WID_FRAG_THRESHOLD, c_val, 2, 0, 0))
692                 goto _fail_;
693
694         /*  SSID                                                                 */
695         /*  --------------------------------------------------------------       */
696         /*  Configuration :   String with length less than 32 bytes              */
697         /*  Values to set :   Any string with length less than 32 bytes          */
698         /*                    ( In BSS Station Set SSID to "" (null string)      */
699         /*                      to enable Broadcast SSID suppport )              */
700         /*  --------------------------------------------------------------       */
701         c_val[0] = 0;
702         if (!wilc_wlan_cfg_set(0, WID_BCAST_SSID, c_val, 1, 0, 0))
703                 goto _fail_;
704
705         c_val[0] = 1;
706         if (!wilc_wlan_cfg_set(0, WID_QOS_ENABLE, c_val, 1, 0, 0))
707                 goto _fail_;
708
709         c_val[0] = NO_POWERSAVE;
710         if (!wilc_wlan_cfg_set(0, WID_POWER_MANAGEMENT, c_val, 1, 0, 0))
711                 goto _fail_;
712
713         c_val[0] = NO_ENCRYPT; /* NO_ENCRYPT, 0x79 */
714         if (!wilc_wlan_cfg_set(0, WID_11I_MODE, c_val, 1, 0, 0))
715                 goto _fail_;
716
717         c_val[0] = OPEN_SYSTEM;
718         if (!wilc_wlan_cfg_set(0, WID_AUTH_TYPE, c_val, 1, 0, 0))
719                 goto _fail_;
720
721         /*  WEP/802 11I Configuration                                            */
722         /*  ------------------------------------------------------------------   */
723         /*  Configuration : WEP Key                                              */
724         /*  Values (0x)   : 5 byte for WEP40 and 13 bytes for WEP104             */
725         /*                  In case more than 5 bytes are passed on for WEP 40   */
726         /*                  only first 5 bytes will be used as the key           */
727         /*  ------------------------------------------------------------------   */
728
729         strcpy(c_val, "123456790abcdef1234567890");
730         if (!wilc_wlan_cfg_set(0, WID_WEP_KEY_VALUE, c_val, (strlen(c_val) + 1), 0, 0))
731                 goto _fail_;
732
733         /*  WEP/802 11I Configuration                                            */
734         /*  ------------------------------------------------------------------   */
735         /*  Configuration : AES/TKIP WPA/RSNA Pre-Shared Key                     */
736         /*  Values to set : Any string with length greater than equal to 8 bytes */
737         /*                  and less than 64 bytes                               */
738         /*  ------------------------------------------------------------------   */
739         strcpy(c_val, "12345678");
740         if (!wilc_wlan_cfg_set(0, WID_11I_PSK, c_val, (strlen(c_val)), 0, 0))
741                 goto _fail_;
742
743         /*  IEEE802.1X Key Configuration                                         */
744         /*  ------------------------------------------------------------------   */
745         /*  Configuration : Radius Server Access Secret Key                      */
746         /*  Values to set : Any string with length greater than equal to 8 bytes */
747         /*                  and less than 65 bytes                               */
748         /*  ------------------------------------------------------------------   */
749         strcpy(c_val, "password");
750         if (!wilc_wlan_cfg_set(0, WID_1X_KEY, c_val, (strlen(c_val) + 1), 0, 0))
751                 goto _fail_;
752
753         /*   IEEE802.1X Server Address Configuration                             */
754         /*  ------------------------------------------------------------------   */
755         /*  Configuration : Radius Server IP Address                             */
756         /*  Values to set : Any valid IP Address                                 */
757         /*  ------------------------------------------------------------------   */
758         c_val[0] = 192;
759         c_val[1] = 168;
760         c_val[2] = 1;
761         c_val[3] = 112;
762         if (!wilc_wlan_cfg_set(0, WID_1X_SERV_ADDR, c_val, 4, 0, 0))
763                 goto _fail_;
764
765         c_val[0] = 3;
766         if (!wilc_wlan_cfg_set(0, WID_LISTEN_INTERVAL, c_val, 1, 0, 0))
767                 goto _fail_;
768
769         c_val[0] = 3;
770         if (!wilc_wlan_cfg_set(0, WID_DTIM_PERIOD, c_val, 1, 0, 0))
771                 goto _fail_;
772
773         c_val[0] = NORMAL_ACK;
774         if (!wilc_wlan_cfg_set(0, WID_ACK_POLICY, c_val, 1, 0, 0))
775                 goto _fail_;
776
777         c_val[0] = 0;
778         if (!wilc_wlan_cfg_set(0, WID_USER_CONTROL_ON_TX_POWER, c_val, 1, 0, 0))
779                 goto _fail_;
780
781         c_val[0] = 48;
782         if (!wilc_wlan_cfg_set(0, WID_TX_POWER_LEVEL_11A, c_val, 1, 0, 0))
783                 goto _fail_;
784
785         c_val[0] = 28;
786         if (!wilc_wlan_cfg_set(0, WID_TX_POWER_LEVEL_11B, c_val, 1, 0, 0))
787                 goto _fail_;
788
789         /*  Beacon Interval                                                      */
790         /*  -------------------------------------------------------------------- */
791         /*  Configuration : Sets the beacon interval value                       */
792         /*  Values to set : Any 16-bit value                                     */
793         /*  -------------------------------------------------------------------- */
794
795         *((int *)c_val) = 100;
796         if (!wilc_wlan_cfg_set(0, WID_BEACON_INTERVAL, c_val, 2, 0, 0))
797                 goto _fail_;
798
799         c_val[0] = REKEY_DISABLE;
800         if (!wilc_wlan_cfg_set(0, WID_REKEY_POLICY, c_val, 1, 0, 0))
801                 goto _fail_;
802
803         /*  Rekey Time (s) (Used only when the Rekey policy is 2 or 4)           */
804         /*  -------------------------------------------------------------------- */
805         /*  Configuration : Sets the Rekey Time (s)                              */
806         /*  Values to set : 32-bit value                                         */
807         /*  -------------------------------------------------------------------- */
808         *((int *)c_val) = 84600;
809         if (!wilc_wlan_cfg_set(0, WID_REKEY_PERIOD, c_val, 4, 0, 0))
810                 goto _fail_;
811
812         /*  Rekey Packet Count (in 1000s; used when Rekey Policy is 3)           */
813         /*  -------------------------------------------------------------------- */
814         /*  Configuration : Sets Rekey Group Packet count                        */
815         /*  Values to set : 32-bit Value                                         */
816         /*  -------------------------------------------------------------------- */
817         *((int *)c_val) = 500;
818         if (!wilc_wlan_cfg_set(0, WID_REKEY_PACKET_COUNT, c_val, 4, 0, 0))
819                 goto _fail_;
820
821         c_val[0] = 1;
822         if (!wilc_wlan_cfg_set(0, WID_SHORT_SLOT_ALLOWED, c_val, 1, 0, 0))
823                 goto _fail_;
824
825         c_val[0] = G_SELF_CTS_PROT;
826         if (!wilc_wlan_cfg_set(0, WID_11N_ERP_PROT_TYPE, c_val, 1, 0, 0))
827                 goto _fail_;
828
829         c_val[0] = 1;  /* Enable N */
830         if (!wilc_wlan_cfg_set(0, WID_11N_ENABLE, c_val, 1, 0, 0))
831                 goto _fail_;
832
833         c_val[0] = HT_MIXED_MODE;
834         if (!wilc_wlan_cfg_set(0, WID_11N_OPERATING_MODE, c_val, 1, 0, 0))
835                 goto _fail_;
836
837         c_val[0] = 1;   /* TXOP Prot disable in N mode: No RTS-CTS on TX A-MPDUs to save air-time. */
838         if (!wilc_wlan_cfg_set(0, WID_11N_TXOP_PROT_DISABLE, c_val, 1, 0, 0))
839                 goto _fail_;
840
841         memcpy(c_val, mac_add, 6);
842
843         if (!wilc_wlan_cfg_set(0, WID_MAC_ADDR, c_val, 6, 0, 0))
844                 goto _fail_;
845
846         /**
847          *      AP only
848          **/
849         c_val[0] = DETECT_PROTECT_REPORT;
850         if (!wilc_wlan_cfg_set(0, WID_11N_OBSS_NONHT_DETECTION, c_val, 1, 0, 0))
851                 goto _fail_;
852
853         c_val[0] = RTS_CTS_NONHT_PROT;
854         if (!wilc_wlan_cfg_set(0, WID_11N_HT_PROT_TYPE, c_val, 1, 0, 0))
855                 goto _fail_;
856
857         c_val[0] = 0;
858         if (!wilc_wlan_cfg_set(0, WID_11N_RIFS_PROT_ENABLE, c_val, 1, 0, 0))
859                 goto _fail_;
860
861         c_val[0] = MIMO_MODE;
862         if (!wilc_wlan_cfg_set(0, WID_11N_SMPS_MODE, c_val, 1, 0, 0))
863                 goto _fail_;
864
865         c_val[0] = 7;
866         if (!wilc_wlan_cfg_set(0, WID_11N_CURRENT_TX_MCS, c_val, 1, 0, 0))
867                 goto _fail_;
868
869         c_val[0] = 1; /* Enable N with immediate block ack. */
870         if (!wilc_wlan_cfg_set(0, WID_11N_IMMEDIATE_BA_ENABLED, c_val, 1, 1, 1))
871                 goto _fail_;
872
873         return 0;
874
875 _fail_:
876         return -1;
877 }
878
879 /**************************/
880 void wilc1000_wlan_deinit(struct wilc *nic)
881 {
882         if (g_linux_wlan->initialized)  {
883                 printk("Deinitializing wilc1000  ...\n");
884
885                 if (nic == NULL) {
886                         PRINT_ER("nic is NULL\n");
887                         return;
888                 }
889
890 #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
891                 /* johnny : remove */
892                 PRINT_D(INIT_DBG, "skip wilc_bus_set_default_speed\n");
893 #else
894                 wilc_bus_set_default_speed();
895 #endif
896
897                 PRINT_D(INIT_DBG, "Disabling IRQ\n");
898 #ifdef WILC_SDIO
899                 mutex_lock(&g_linux_wlan->hif_cs);
900                 disable_sdio_interrupt();
901                 mutex_unlock(&g_linux_wlan->hif_cs);
902 #endif
903                 if (&g_linux_wlan->txq_event != NULL)
904                         up(&g_linux_wlan->txq_event);
905
906                 PRINT_D(INIT_DBG, "Deinitializing Threads\n");
907                 wlan_deinitialize_threads(nic);
908
909                 PRINT_D(INIT_DBG, "Deinitializing IRQ\n");
910                 deinit_irq(g_linux_wlan);
911
912                 wilc_wlan_stop();
913
914                 PRINT_D(INIT_DBG, "Deinitializing WILC Wlan\n");
915                 wilc_wlan_cleanup();
916 #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
917   #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
918                 PRINT_D(INIT_DBG, "Disabling IRQ 2\n");
919
920                 mutex_lock(&g_linux_wlan->hif_cs);
921                 disable_sdio_interrupt();
922                 mutex_unlock(&g_linux_wlan->hif_cs);
923   #endif
924 #endif
925
926                 /*De-Initialize locks*/
927                 PRINT_D(INIT_DBG, "Deinitializing Locks\n");
928                 wlan_deinit_locks(g_linux_wlan);
929
930                 /* announce that wilc1000 is not initialized */
931                 g_linux_wlan->initialized = false;
932
933                 PRINT_D(INIT_DBG, "wilc1000 deinitialization Done\n");
934
935         } else {
936                 PRINT_D(INIT_DBG, "wilc1000 is not initialized\n");
937         }
938 }
939
940 int wlan_init_locks(struct wilc *p_nic)
941 {
942
943         PRINT_D(INIT_DBG, "Initializing Locks ...\n");
944
945         mutex_init(&g_linux_wlan->hif_cs);
946         mutex_init(&g_linux_wlan->rxq_cs);
947
948         spin_lock_init(&g_linux_wlan->txq_spinlock);
949         sema_init(&g_linux_wlan->txq_add_to_head_cs, 1);
950
951         sema_init(&g_linux_wlan->txq_event, 0);
952
953         sema_init(&g_linux_wlan->cfg_event, 0);
954         sema_init(&g_linux_wlan->sync_event, 0);
955
956         sema_init(&g_linux_wlan->txq_thread_started, 0);
957
958         return 0;
959 }
960
961 static int wlan_deinit_locks(struct wilc *nic)
962 {
963         PRINT_D(INIT_DBG, "De-Initializing Locks\n");
964
965         if (&g_linux_wlan->hif_cs != NULL)
966                 mutex_destroy(&g_linux_wlan->hif_cs);
967
968         if (&g_linux_wlan->rxq_cs != NULL)
969                 mutex_destroy(&g_linux_wlan->rxq_cs);
970
971         return 0;
972 }
973 void linux_to_wlan(wilc_wlan_inp_t *nwi, struct wilc *nic)
974 {
975
976         PRINT_D(INIT_DBG, "Linux to Wlan services ...\n");
977
978         nwi->os_context.os_private = (void *)nic;
979
980 #ifdef WILC_SDIO
981         nwi->io_func.io_type = HIF_SDIO;
982         nwi->io_func.io_init = linux_sdio_init;
983         nwi->io_func.io_deinit = linux_sdio_deinit;
984         nwi->io_func.u.sdio.sdio_cmd52 = linux_sdio_cmd52;
985         nwi->io_func.u.sdio.sdio_cmd53 = linux_sdio_cmd53;
986         nwi->io_func.u.sdio.sdio_set_max_speed = linux_sdio_set_max_speed;
987         nwi->io_func.u.sdio.sdio_set_default_speed = linux_sdio_set_default_speed;
988 #else
989         nwi->io_func.io_type = HIF_SPI;
990         nwi->io_func.io_init = linux_spi_init;
991         nwi->io_func.io_deinit = linux_spi_deinit;
992         nwi->io_func.u.spi.spi_tx = linux_spi_write;
993         nwi->io_func.u.spi.spi_rx = linux_spi_read;
994         nwi->io_func.u.spi.spi_trx = linux_spi_write_read;
995         nwi->io_func.u.spi.spi_max_speed = linux_spi_set_max_speed;
996 #endif
997 }
998
999 int wlan_initialize_threads(perInterface_wlan_t *nic)
1000 {
1001
1002         int ret = 0;
1003
1004         PRINT_D(INIT_DBG, "Initializing Threads ...\n");
1005
1006         /* create tx task */
1007         PRINT_D(INIT_DBG, "Creating kthread for transmission\n");
1008         g_linux_wlan->txq_thread = kthread_run(linux_wlan_txq_task, (void *)g_linux_wlan, "K_TXQ_TASK");
1009         if (g_linux_wlan->txq_thread == NULL) {
1010                 PRINT_ER("couldn't create TXQ thread\n");
1011                 ret = -ENOBUFS;
1012                 goto _fail_2;
1013         }
1014         /* wait for TXQ task to start. */
1015         down(&g_linux_wlan->txq_thread_started);
1016
1017         return 0;
1018
1019 _fail_2:
1020         /*De-Initialize 2nd thread*/
1021         g_linux_wlan->close = 1;
1022
1023         g_linux_wlan->close = 0;
1024         return ret;
1025 }
1026
1027 static void wlan_deinitialize_threads(struct wilc *nic)
1028 {
1029
1030         g_linux_wlan->close = 1;
1031         PRINT_D(INIT_DBG, "Deinitializing Threads\n");
1032
1033         if (&g_linux_wlan->txq_event != NULL)
1034                 up(&g_linux_wlan->txq_event);
1035
1036         if (g_linux_wlan->txq_thread != NULL) {
1037                 kthread_stop(g_linux_wlan->txq_thread);
1038                 g_linux_wlan->txq_thread = NULL;
1039         }
1040 }
1041
1042 #ifdef COMPLEMENT_BOOT
1043
1044 extern volatile int probe;
1045 extern u8 core_11b_ready(void);
1046
1047 #define READY_CHECK_THRESHOLD           30
1048 extern void wilc_wlan_global_reset(void);
1049 u8 wilc1000_prepare_11b_core(wilc_wlan_inp_t *nwi, struct wilc *nic)
1050 {
1051         u8 trials = 0;
1052
1053         while ((core_11b_ready() && (READY_CHECK_THRESHOLD > (trials++)))) {
1054                 PRINT_D(INIT_DBG, "11b core not ready yet: %u\n", trials);
1055                 wilc_wlan_cleanup();
1056                 wilc_wlan_global_reset();
1057                 sdio_unregister_driver(&wilc_bus);
1058
1059                 linux_wlan_device_detection(0);
1060
1061                 mdelay(100);
1062
1063                 linux_wlan_device_detection(1);
1064
1065                 sdio_register_driver(&wilc_bus);
1066
1067                 while (!probe)
1068                         msleep(100);
1069                 probe = 0;
1070                 g_linux_wlan->wilc_sdio_func = local_sdio_func;
1071                 linux_to_wlan(nwi, nic);
1072                 wilc_wlan_init(nwi);
1073         }
1074
1075         if (READY_CHECK_THRESHOLD <= trials)
1076                 return 1;
1077         else
1078                 return 0;
1079
1080 }
1081
1082 int repeat_power_cycle(perInterface_wlan_t *nic)
1083 {
1084         int ret = 0;
1085         wilc_wlan_inp_t nwi;
1086
1087         sdio_unregister_driver(&wilc_bus);
1088
1089         linux_wlan_device_detection(0);
1090         linux_wlan_device_power(0);
1091         msleep(100);
1092         linux_wlan_device_power(1);
1093         msleep(80);
1094         linux_wlan_device_detection(1);
1095         msleep(20);
1096
1097         sdio_register_driver(&wilc_bus);
1098
1099         /* msleep(1000); */
1100         while (!probe)
1101                 msleep(100);
1102         probe = 0;
1103         g_linux_wlan->wilc_sdio_func = local_sdio_func;
1104         linux_to_wlan(&nwi, g_linux_wlan);
1105         ret = wilc_wlan_init(&nwi);
1106
1107         g_linux_wlan->mac_status = WILC_MAC_STATUS_INIT;
1108         #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
1109         enable_sdio_interrupt();
1110         #endif
1111
1112         if (linux_wlan_get_firmware(nic)) {
1113                 PRINT_ER("Can't get firmware\n");
1114                 ret = -1;
1115                 goto __fail__;
1116         }
1117
1118         /*Download firmware*/
1119         ret = linux_wlan_firmware_download(g_linux_wlan);
1120         if (ret < 0) {
1121                 PRINT_ER("Failed to download firmware\n");
1122                 goto __fail__;
1123         }
1124         /* Start firmware*/
1125         ret = linux_wlan_start_firmware(nic);
1126         if (ret < 0)
1127                 PRINT_ER("Failed to start firmware\n");
1128 __fail__:
1129         return ret;
1130 }
1131 #endif
1132
1133 int wilc1000_wlan_init(struct net_device *dev, perInterface_wlan_t *p_nic)
1134 {
1135         wilc_wlan_inp_t nwi;
1136         perInterface_wlan_t *nic = p_nic;
1137         int ret = 0;
1138
1139         if (!g_linux_wlan->initialized) {
1140                 g_linux_wlan->mac_status = WILC_MAC_STATUS_INIT;
1141                 g_linux_wlan->close = 0;
1142
1143                 wlan_init_locks(g_linux_wlan);
1144
1145                 linux_to_wlan(&nwi, g_linux_wlan);
1146
1147                 ret = wilc_wlan_init(&nwi);
1148                 if (ret < 0) {
1149                         PRINT_ER("Initializing WILC_Wlan FAILED\n");
1150                         ret = -EIO;
1151                         goto _fail_locks_;
1152                 }
1153
1154                 ret = wlan_initialize_threads(nic);
1155                 if (ret < 0) {
1156                         PRINT_ER("Initializing Threads FAILED\n");
1157                         ret = -EIO;
1158                         goto _fail_wilc_wlan_;
1159                 }
1160
1161 #if (defined WILC_SDIO) && (defined COMPLEMENT_BOOT)
1162                 if (wilc1000_prepare_11b_core(&nwi, g_linux_wlan)) {
1163                         PRINT_ER("11b Core is not ready\n");
1164                         ret = -EIO;
1165                         goto _fail_threads_;
1166                 }
1167 #endif
1168
1169 #if (!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO)
1170                 if (init_irq(g_linux_wlan)) {
1171                         PRINT_ER("couldn't initialize IRQ\n");
1172                         ret = -EIO;
1173                         goto _fail_threads_;
1174                 }
1175 #endif
1176
1177 #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
1178                 if (enable_sdio_interrupt()) {
1179                         PRINT_ER("couldn't initialize IRQ\n");
1180                         ret = -EIO;
1181                         goto _fail_irq_init_;
1182                 }
1183 #endif
1184
1185                 if (linux_wlan_get_firmware(nic)) {
1186                         PRINT_ER("Can't get firmware\n");
1187                         ret = -EIO;
1188                         goto _fail_irq_enable_;
1189                 }
1190
1191                 /*Download firmware*/
1192                 ret = linux_wlan_firmware_download(g_linux_wlan);
1193                 if (ret < 0) {
1194                         PRINT_ER("Failed to download firmware\n");
1195                         ret = -EIO;
1196                         goto _fail_irq_enable_;
1197                 }
1198
1199                 /* Start firmware*/
1200                 ret = linux_wlan_start_firmware(nic);
1201                 if (ret < 0) {
1202                         PRINT_ER("Failed to start firmware\n");
1203                         ret = -EIO;
1204                         goto _fail_irq_enable_;
1205                 }
1206
1207                 wilc_bus_set_max_speed();
1208
1209                 if (wilc_wlan_cfg_get(1, WID_FIRMWARE_VERSION, 1, 0)) {
1210                         int size;
1211                         char Firmware_ver[20];
1212
1213                         size = wilc_wlan_cfg_get_val(
1214                                         WID_FIRMWARE_VERSION,
1215                                         Firmware_ver, sizeof(Firmware_ver));
1216                         Firmware_ver[size] = '\0';
1217                         PRINT_D(INIT_DBG, "***** Firmware Ver = %s  *******\n", Firmware_ver);
1218                 }
1219                 /* Initialize firmware with default configuration */
1220                 ret = linux_wlan_init_test_config(dev, g_linux_wlan);
1221
1222                 if (ret < 0) {
1223                         PRINT_ER("Failed to configure firmware\n");
1224                         ret = -EIO;
1225                         goto _fail_fw_start_;
1226                 }
1227
1228                 g_linux_wlan->initialized = true;
1229                 return 0; /*success*/
1230
1231 _fail_fw_start_:
1232                 wilc_wlan_stop();
1233
1234 _fail_irq_enable_:
1235 #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
1236                 disable_sdio_interrupt();
1237 _fail_irq_init_:
1238 #endif
1239 #if (!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO)
1240                 deinit_irq(g_linux_wlan);
1241
1242 #endif
1243 _fail_threads_:
1244                 wlan_deinitialize_threads(g_linux_wlan);
1245 _fail_wilc_wlan_:
1246                 wilc_wlan_cleanup();
1247 _fail_locks_:
1248                 wlan_deinit_locks(g_linux_wlan);
1249                 PRINT_ER("WLAN Iinitialization FAILED\n");
1250         } else {
1251                 PRINT_D(INIT_DBG, "wilc1000 already initialized\n");
1252         }
1253         return ret;
1254 }
1255
1256 /*
1257  *      - this function will be called automatically by OS when module inserted.
1258  */
1259
1260 int mac_init_fn(struct net_device *ndev)
1261 {
1262
1263         /*Why we do this !!!*/
1264         netif_start_queue(ndev); /* ma */
1265         netif_stop_queue(ndev); /* ma */
1266
1267         return 0;
1268 }
1269
1270 /* This fn is called, when this device is setup using ifconfig */
1271 int mac_open(struct net_device *ndev)
1272 {
1273         perInterface_wlan_t *nic;
1274
1275         /*No need for setting mac address here anymore,*/
1276         /*Just set it in init_test_config()*/
1277         unsigned char mac_add[ETH_ALEN] = {0};
1278         int ret = 0;
1279         int i = 0;
1280         struct wilc_priv *priv;
1281
1282 #ifdef WILC_SPI
1283         if (!g_linux_wlan || !g_linux_wlan->wilc_spidev) {
1284                 netdev_err(ndev, "wilc1000: SPI device not ready\n");
1285                 return -ENODEV;
1286         }
1287 #endif
1288         nic = netdev_priv(ndev);
1289         priv = wiphy_priv(nic->wilc_netdev->ieee80211_ptr->wiphy);
1290         PRINT_D(INIT_DBG, "MAC OPEN[%p]\n", ndev);
1291
1292         ret = wilc_init_host_int(ndev);
1293         if (ret < 0) {
1294                 PRINT_ER("Failed to initialize host interface\n");
1295
1296                 return ret;
1297         }
1298
1299         /*initialize platform*/
1300         PRINT_D(INIT_DBG, "*** re-init ***\n");
1301         ret = wilc1000_wlan_init(ndev, nic);
1302         if (ret < 0) {
1303                 PRINT_ER("Failed to initialize wilc1000\n");
1304                 wilc_deinit_host_int(ndev);
1305                 return ret;
1306         }
1307
1308         Set_machw_change_vir_if(false);
1309
1310         host_int_get_MacAddress(priv->hWILCWFIDrv, mac_add);
1311         PRINT_D(INIT_DBG, "Mac address: %pM\n", mac_add);
1312
1313         /* loop through the NUM of supported devices and set the MAC address */
1314         for (i = 0; i < g_linux_wlan->vif_num; i++) {
1315                 if (ndev == g_linux_wlan->vif[i].wilc_netdev) {
1316                         memcpy(g_linux_wlan->vif[i].src_addr, mac_add, ETH_ALEN);
1317                         g_linux_wlan->vif[i].hif_drv = priv->hWILCWFIDrv;
1318                         break;
1319                 }
1320         }
1321
1322         /* TODO: get MAC address whenever the source is EPROM - hardcoded and copy it to ndev*/
1323         memcpy(ndev->dev_addr, g_linux_wlan->vif[i].src_addr, ETH_ALEN);
1324
1325         if (!is_valid_ether_addr(ndev->dev_addr)) {
1326                 PRINT_ER("Error: Wrong MAC address\n");
1327                 ret = -EINVAL;
1328                 goto _err_;
1329         }
1330
1331         wilc_mgmt_frame_register(nic->wilc_netdev->ieee80211_ptr->wiphy, nic->wilc_netdev->ieee80211_ptr,
1332                                  nic->g_struct_frame_reg[0].frame_type, nic->g_struct_frame_reg[0].reg);
1333         wilc_mgmt_frame_register(nic->wilc_netdev->ieee80211_ptr->wiphy, nic->wilc_netdev->ieee80211_ptr,
1334                                  nic->g_struct_frame_reg[1].frame_type, nic->g_struct_frame_reg[1].reg);
1335         netif_wake_queue(ndev);
1336         g_linux_wlan->open_ifcs++;
1337         nic->mac_opened = 1;
1338         return 0;
1339
1340 _err_:
1341         wilc_deinit_host_int(ndev);
1342         wilc1000_wlan_deinit(g_linux_wlan);
1343         return ret;
1344 }
1345
1346 struct net_device_stats *mac_stats(struct net_device *dev)
1347 {
1348         perInterface_wlan_t *nic = netdev_priv(dev);
1349
1350         return &nic->netstats;
1351 }
1352
1353 /* Setup the multicast filter */
1354 static void wilc_set_multicast_list(struct net_device *dev)
1355 {
1356
1357         struct netdev_hw_addr *ha;
1358         struct wilc_priv *priv;
1359         struct host_if_drv *pstrWFIDrv;
1360         int i = 0;
1361
1362         priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
1363         pstrWFIDrv = (struct host_if_drv *)priv->hWILCWFIDrv;
1364
1365         if (!dev)
1366                 return;
1367
1368         PRINT_D(INIT_DBG, "Setting Multicast List with count = %d.\n", dev->mc.count);
1369
1370         if (dev->flags & IFF_PROMISC) {
1371                 /* Normally, we should configure the chip to retrive all packets
1372                  * but we don't wanna support this right now */
1373                 /* TODO: add promiscuous mode support */
1374                 PRINT_D(INIT_DBG, "Set promiscuous mode ON, retrive all packets\n");
1375                 return;
1376         }
1377
1378         /* If there's more addresses than we handle, get all multicast
1379          * packets and sort them out in software. */
1380         if ((dev->flags & IFF_ALLMULTI) || (dev->mc.count) > WILC_MULTICAST_TABLE_SIZE) {
1381                 PRINT_D(INIT_DBG, "Disable multicast filter, retrive all multicast packets\n");
1382                 /* get all multicast packets */
1383                 host_int_setup_multicast_filter(pstrWFIDrv, false, 0);
1384                 return;
1385         }
1386
1387         /* No multicast?  Just get our own stuff */
1388         if ((dev->mc.count) == 0) {
1389                 PRINT_D(INIT_DBG, "Enable multicast filter, retrive directed packets only.\n");
1390                 host_int_setup_multicast_filter(pstrWFIDrv, true, 0);
1391                 return;
1392         }
1393
1394         /* Store all of the multicast addresses in the hardware filter */
1395         netdev_for_each_mc_addr(ha, dev)
1396         {
1397                 memcpy(gau8MulticastMacAddrList[i], ha->addr, ETH_ALEN);
1398                 PRINT_D(INIT_DBG, "Entry[%d]: %x:%x:%x:%x:%x:%x\n", i,
1399                         gau8MulticastMacAddrList[i][0], gau8MulticastMacAddrList[i][1], gau8MulticastMacAddrList[i][2], gau8MulticastMacAddrList[i][3], gau8MulticastMacAddrList[i][4], gau8MulticastMacAddrList[i][5]);
1400                 i++;
1401         }
1402
1403         host_int_setup_multicast_filter(pstrWFIDrv, true, (dev->mc.count));
1404
1405         return;
1406
1407 }
1408
1409 static void linux_wlan_tx_complete(void *priv, int status)
1410 {
1411
1412         struct tx_complete_data *pv_data = (struct tx_complete_data *)priv;
1413
1414         if (status == 1)
1415                 PRINT_D(TX_DBG, "Packet sent successfully - Size = %d - Address = %p - SKB = %p\n", pv_data->size, pv_data->buff, pv_data->skb);
1416         else
1417                 PRINT_D(TX_DBG, "Couldn't send packet - Size = %d - Address = %p - SKB = %p\n", pv_data->size, pv_data->buff, pv_data->skb);
1418         /* Free the SK Buffer, its work is done */
1419         dev_kfree_skb(pv_data->skb);
1420         kfree(pv_data);
1421 }
1422
1423 int mac_xmit(struct sk_buff *skb, struct net_device *ndev)
1424 {
1425         perInterface_wlan_t *nic;
1426         struct tx_complete_data *tx_data = NULL;
1427         int QueueCount;
1428         char *pu8UdpBuffer;
1429         struct iphdr *ih;
1430         struct ethhdr *eth_h;
1431
1432         nic = netdev_priv(ndev);
1433
1434         PRINT_D(TX_DBG, "Sending packet just received from TCP/IP\n");
1435
1436         /* Stop the network interface queue */
1437         if (skb->dev != ndev) {
1438                 PRINT_ER("Packet not destined to this device\n");
1439                 return 0;
1440         }
1441
1442         tx_data = kmalloc(sizeof(struct tx_complete_data), GFP_ATOMIC);
1443         if (tx_data == NULL) {
1444                 PRINT_ER("Failed to allocate memory for tx_data structure\n");
1445                 dev_kfree_skb(skb);
1446                 netif_wake_queue(ndev);
1447                 return 0;
1448         }
1449
1450         tx_data->buff = skb->data;
1451         tx_data->size = skb->len;
1452         tx_data->skb  = skb;
1453
1454         eth_h = (struct ethhdr *)(skb->data);
1455         if (eth_h->h_proto == 0x8e88)
1456                 PRINT_D(INIT_DBG, "EAPOL transmitted\n");
1457
1458         /*get source and dest ip addresses*/
1459         ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
1460
1461         pu8UdpBuffer = (char *)ih + sizeof(struct iphdr);
1462         if ((pu8UdpBuffer[1] == 68 && pu8UdpBuffer[3] == 67) || (pu8UdpBuffer[1] == 67 && pu8UdpBuffer[3] == 68))
1463                 PRINT_D(GENERIC_DBG, "DHCP Message transmitted, type:%x %x %x\n", pu8UdpBuffer[248], pu8UdpBuffer[249], pu8UdpBuffer[250]);
1464
1465         PRINT_D(TX_DBG, "Sending packet - Size = %d - Address = %p - SKB = %p\n", tx_data->size, tx_data->buff, tx_data->skb);
1466
1467         /* Send packet to MAC HW - for now the tx_complete function will be just status
1468          * indicator. still not sure if I need to suspend host transmission till the tx_complete
1469          * function called or not?
1470          * allocated buffer will be freed in tx_complete function.
1471          */
1472         PRINT_D(TX_DBG, "Adding tx packet to TX Queue\n");
1473         nic->netstats.tx_packets++;
1474         nic->netstats.tx_bytes += tx_data->size;
1475         tx_data->pBssid = g_linux_wlan->vif[nic->u8IfIdx].bssid;
1476         QueueCount = wilc_wlan_txq_add_net_pkt((void *)tx_data, tx_data->buff,
1477                                                tx_data->size,
1478                                                linux_wlan_tx_complete);
1479
1480         if (QueueCount > FLOW_CONTROL_UPPER_THRESHOLD) {
1481                 netif_stop_queue(g_linux_wlan->vif[0].wilc_netdev);
1482                 netif_stop_queue(g_linux_wlan->vif[1].wilc_netdev);
1483         }
1484
1485         return 0;
1486 }
1487
1488 int mac_close(struct net_device *ndev)
1489 {
1490         struct wilc_priv *priv;
1491         perInterface_wlan_t *nic;
1492         struct host_if_drv *pstrWFIDrv;
1493
1494         nic = netdev_priv(ndev);
1495
1496         if ((nic == NULL) || (nic->wilc_netdev == NULL) || (nic->wilc_netdev->ieee80211_ptr == NULL) || (nic->wilc_netdev->ieee80211_ptr->wiphy == NULL)) {
1497                 PRINT_ER("nic = NULL\n");
1498                 return 0;
1499         }
1500
1501         priv = wiphy_priv(nic->wilc_netdev->ieee80211_ptr->wiphy);
1502
1503         if (priv == NULL) {
1504                 PRINT_ER("priv = NULL\n");
1505                 return 0;
1506         }
1507
1508         pstrWFIDrv = (struct host_if_drv *)priv->hWILCWFIDrv;
1509
1510         PRINT_D(GENERIC_DBG, "Mac close\n");
1511
1512         if (g_linux_wlan == NULL) {
1513                 PRINT_ER("g_linux_wlan = NULL\n");
1514                 return 0;
1515         }
1516
1517         if (pstrWFIDrv == NULL) {
1518                 PRINT_ER("pstrWFIDrv = NULL\n");
1519                 return 0;
1520         }
1521
1522         if ((g_linux_wlan->open_ifcs) > 0) {
1523                 g_linux_wlan->open_ifcs--;
1524         } else {
1525                 PRINT_ER("ERROR: MAC close called while number of opened interfaces is zero\n");
1526                 return 0;
1527         }
1528
1529         if (nic->wilc_netdev != NULL) {
1530                 /* Stop the network interface queue */
1531                 netif_stop_queue(nic->wilc_netdev);
1532
1533                 wilc_deinit_host_int(nic->wilc_netdev);
1534         }
1535
1536         if (g_linux_wlan->open_ifcs == 0) {
1537                 PRINT_D(GENERIC_DBG, "Deinitializing wilc1000\n");
1538                 g_linux_wlan->close = 1;
1539                 wilc1000_wlan_deinit(g_linux_wlan);
1540                 WILC_WFI_deinit_mon_interface();
1541         }
1542
1543         up(&close_exit_sync);
1544         nic->mac_opened = 0;
1545
1546         return 0;
1547 }
1548
1549 int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1550 {
1551
1552         u8 *buff = NULL;
1553         s8 rssi;
1554         u32 size = 0, length = 0;
1555         perInterface_wlan_t *nic;
1556         struct wilc_priv *priv;
1557         s32 s32Error = 0;
1558
1559         /* struct iwreq *wrq = (struct iwreq *) req;    // tony moved to case SIOCSIWPRIV */
1560         nic = netdev_priv(ndev);
1561
1562         if (!g_linux_wlan->initialized)
1563                 return 0;
1564
1565         switch (cmd) {
1566
1567         /* ]] 2013-06-24 */
1568         case SIOCSIWPRIV:
1569         {
1570                 struct iwreq *wrq = (struct iwreq *) req;               /* added by tony */
1571
1572                 size = wrq->u.data.length;
1573
1574                 if (size && wrq->u.data.pointer) {
1575
1576                         buff = memdup_user(wrq->u.data.pointer, wrq->u.data.length);
1577                         if (IS_ERR(buff))
1578                                 return PTR_ERR(buff);
1579
1580                         if (strncasecmp(buff, "RSSI", length) == 0) {
1581                                 priv = wiphy_priv(nic->wilc_netdev->ieee80211_ptr->wiphy);
1582                                 s32Error = host_int_get_rssi(priv->hWILCWFIDrv, &(rssi));
1583                                 if (s32Error)
1584                                         PRINT_ER("Failed to send get rssi param's message queue ");
1585                                 PRINT_INFO(GENERIC_DBG, "RSSI :%d\n", rssi);
1586
1587                                 /*Rounding up the rssi negative value*/
1588                                 rssi += 5;
1589
1590                                 snprintf(buff, size, "rssi %d", rssi);
1591
1592                                 if (copy_to_user(wrq->u.data.pointer, buff, size)) {
1593                                         PRINT_ER("%s: failed to copy data to user buffer\n", __func__);
1594                                         s32Error = -EFAULT;
1595                                         goto done;
1596                                 }
1597                         }
1598                 }
1599         }
1600         break;
1601
1602         default:
1603         {
1604                 PRINT_INFO(GENERIC_DBG, "Command - %d - has been received\n", cmd);
1605                 s32Error = -EOPNOTSUPP;
1606                 goto done;
1607         }
1608         }
1609
1610 done:
1611
1612         kfree(buff);
1613
1614         return s32Error;
1615 }
1616
1617 void frmw_to_linux(u8 *buff, u32 size, u32 pkt_offset)
1618 {
1619
1620         unsigned int frame_len = 0;
1621         int stats;
1622         unsigned char *buff_to_send = NULL;
1623         struct sk_buff *skb;
1624         struct net_device *wilc_netdev;
1625         perInterface_wlan_t *nic;
1626
1627         wilc_netdev = GetIfHandler(buff);
1628         if (wilc_netdev == NULL)
1629                 return;
1630
1631         buff += pkt_offset;
1632         nic = netdev_priv(wilc_netdev);
1633
1634         if (size > 0) {
1635
1636                 frame_len = size;
1637                 buff_to_send = buff;
1638
1639                 /* Need to send the packet up to the host, allocate a skb buffer */
1640                 skb = dev_alloc_skb(frame_len);
1641                 if (skb == NULL) {
1642                         PRINT_ER("Low memory - packet droped\n");
1643                         return;
1644                 }
1645
1646                 if (g_linux_wlan == NULL || wilc_netdev == NULL)
1647                         PRINT_ER("wilc_netdev in g_linux_wlan is NULL");
1648                 skb->dev = wilc_netdev;
1649
1650                 if (skb->dev == NULL)
1651                         PRINT_ER("skb->dev is NULL\n");
1652
1653                 /*
1654                  * for(i=0;i<40;i++)
1655                  * {
1656                  *      if(i<frame_len)
1657                  *              WILC_PRINTF("buff_to_send[%d]=%2x\n",i,buff_to_send[i]);
1658                  *
1659                  * }*/
1660
1661                 /* skb_put(skb, frame_len); */
1662                 memcpy(skb_put(skb, frame_len), buff_to_send, frame_len);
1663
1664                 /* WILC_PRINTF("After MEM_CPY\n"); */
1665
1666                 /* nic = netdev_priv(wilc_netdev); */
1667
1668                 skb->protocol = eth_type_trans(skb, wilc_netdev);
1669                 /* Send the packet to the stack by giving it to the bridge */
1670                 nic->netstats.rx_packets++;
1671                 nic->netstats.rx_bytes += frame_len;
1672                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1673                 stats = netif_rx(skb);
1674                 PRINT_D(RX_DBG, "netif_rx ret value is: %d\n", stats);
1675         }
1676 }
1677
1678 void WILC_WFI_mgmt_rx(u8 *buff, u32 size)
1679 {
1680         int i = 0;
1681         perInterface_wlan_t *nic;
1682
1683         /*Pass the frame on the monitor interface, if any.*/
1684         /*Otherwise, pass it on p2p0 netdev, if registered on it*/
1685         for (i = 0; i < g_linux_wlan->vif_num; i++) {
1686                 nic = netdev_priv(g_linux_wlan->vif[i].wilc_netdev);
1687                 if (nic->monitor_flag) {
1688                         WILC_WFI_monitor_rx(buff, size);
1689                         return;
1690                 }
1691         }
1692
1693         nic = netdev_priv(g_linux_wlan->vif[1].wilc_netdev); /* p2p0 */
1694         if ((buff[0] == nic->g_struct_frame_reg[0].frame_type && nic->g_struct_frame_reg[0].reg) ||
1695             (buff[0] == nic->g_struct_frame_reg[1].frame_type && nic->g_struct_frame_reg[1].reg))
1696                 WILC_WFI_p2p_rx(g_linux_wlan->vif[1].wilc_netdev, buff, size);
1697 }
1698
1699 int wilc_netdev_init(void)
1700 {
1701
1702         int i;
1703         perInterface_wlan_t *nic;
1704         struct net_device *ndev;
1705
1706         sema_init(&close_exit_sync, 0);
1707
1708         /*create the common structure*/
1709         g_linux_wlan = kzalloc(sizeof(*g_linux_wlan), GFP_KERNEL);
1710         if (!g_linux_wlan)
1711                 return -ENOMEM;
1712
1713         register_inetaddr_notifier(&g_dev_notifier);
1714
1715         for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1716                 /*allocate first ethernet device with perinterface_wlan_t as its private data*/
1717                 ndev = alloc_etherdev(sizeof(perInterface_wlan_t));
1718                 if (!ndev) {
1719                         PRINT_ER("Failed to allocate ethernet dev\n");
1720                         return -1;
1721                 }
1722
1723                 nic = netdev_priv(ndev);
1724                 memset(nic, 0, sizeof(perInterface_wlan_t));
1725
1726                 /*Name the Devices*/
1727                 if (i == 0) {
1728                 #if defined(NM73131)    /* tony, 2012-09-20 */
1729                         strcpy(ndev->name, "wilc_eth%d");
1730                 #elif defined(PLAT_CLM9722)                     /* rachel */
1731                         strcpy(ndev->name, "eth%d");
1732                 #else /* PANDA_BOARD, PLAT_ALLWINNER_A10, PLAT_ALLWINNER_A20, PLAT_ALLWINNER_A31, PLAT_AML8726_M3 or PLAT_WMS8304 */
1733                         strcpy(ndev->name, "wlan%d");
1734                 #endif
1735                 } else
1736                         strcpy(ndev->name, "p2p%d");
1737
1738                 nic->u8IfIdx = g_linux_wlan->vif_num;
1739                 nic->wilc_netdev = ndev;
1740                 g_linux_wlan->vif[g_linux_wlan->vif_num].wilc_netdev = ndev;
1741                 g_linux_wlan->vif_num++;
1742                 ndev->netdev_ops = &wilc_netdev_ops;
1743
1744                 {
1745                         struct wireless_dev *wdev;
1746                         /*Register WiFi*/
1747                         wdev = wilc_create_wiphy(ndev);
1748
1749                         #ifdef WILC_SDIO
1750                         /* set netdev, tony */
1751                         SET_NETDEV_DEV(ndev, &local_sdio_func->dev);
1752                         #endif
1753
1754                         if (wdev == NULL) {
1755                                 PRINT_ER("Can't register WILC Wiphy\n");
1756                                 return -1;
1757                         }
1758
1759                         /*linking the wireless_dev structure with the netdevice*/
1760                         nic->wilc_netdev->ieee80211_ptr = wdev;
1761                         nic->wilc_netdev->ml_priv = nic;
1762                         wdev->netdev = nic->wilc_netdev;
1763                         nic->netstats.rx_packets = 0;
1764                         nic->netstats.tx_packets = 0;
1765                         nic->netstats.rx_bytes = 0;
1766                         nic->netstats.tx_bytes = 0;
1767
1768                 }
1769
1770                 if (register_netdev(ndev)) {
1771                         PRINT_ER("Device couldn't be registered - %s\n", ndev->name);
1772                         return -1; /* ERROR */
1773                 }
1774
1775                 nic->iftype = STATION_MODE;
1776                 nic->mac_opened = 0;
1777
1778         }
1779
1780         #ifndef WILC_SDIO
1781         if (!linux_spi_init(&g_linux_wlan->wilc_spidev)) {
1782                 PRINT_ER("Can't initialize SPI\n");
1783                 return -1; /* ERROR */
1784         }
1785         g_linux_wlan->wilc_spidev = wilc_spi_dev;
1786         #else
1787         g_linux_wlan->wilc_sdio_func = local_sdio_func;
1788         #endif
1789
1790         return 0;
1791 }
1792
1793 /*The 1st function called after module inserted*/
1794 static int __init init_wilc_driver(void)
1795 {
1796 #if defined(WILC_DEBUGFS)
1797         if (wilc_debugfs_init() < 0) {
1798                 PRINT_D(GENERIC_DBG, "fail to create debugfs for wilc driver\n");
1799                 return -1;
1800         }
1801 #endif
1802
1803         printk("IN INIT FUNCTION\n");
1804         printk("*** WILC1000 driver VERSION=[10.2] FW_VER=[10.2] ***\n");
1805
1806         linux_wlan_device_power(1);
1807         msleep(100);
1808         linux_wlan_device_detection(1);
1809
1810 #ifdef WILC_SDIO
1811         {
1812                 int ret;
1813
1814                 ret = sdio_register_driver(&wilc_bus);
1815                 if (ret < 0)
1816                         PRINT_D(INIT_DBG, "init_wilc_driver: Failed register sdio driver\n");
1817
1818                 return ret;
1819         }
1820 #else
1821         PRINT_D(INIT_DBG, "Initializing netdev\n");
1822         if (wilc_netdev_init())
1823                 PRINT_ER("Couldn't initialize netdev\n");
1824         return 0;
1825 #endif
1826 }
1827 late_initcall(init_wilc_driver);
1828
1829 static void __exit exit_wilc_driver(void)
1830 {
1831         int i = 0;
1832         perInterface_wlan_t *nic[NUM_CONCURRENT_IFC] = {NULL,};
1833         #define CLOSE_TIMEOUT (12 * 1000)
1834
1835         if ((g_linux_wlan != NULL) && (((g_linux_wlan->vif[0].wilc_netdev) != NULL)
1836                                        || ((g_linux_wlan->vif[1].wilc_netdev) != NULL))) {
1837                 unregister_inetaddr_notifier(&g_dev_notifier);
1838
1839                 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
1840                         nic[i] = netdev_priv(g_linux_wlan->vif[i].wilc_netdev);
1841         }
1842
1843         if ((g_linux_wlan != NULL) && g_linux_wlan->wilc_firmware != NULL) {
1844                 release_firmware(g_linux_wlan->wilc_firmware);
1845                 g_linux_wlan->wilc_firmware = NULL;
1846         }
1847
1848         if ((g_linux_wlan != NULL) && (((g_linux_wlan->vif[0].wilc_netdev) != NULL)
1849                                        || ((g_linux_wlan->vif[1].wilc_netdev) != NULL))) {
1850                 PRINT_D(INIT_DBG, "Waiting for mac_close ....\n");
1851
1852                 if (linux_wlan_lock_timeout(&close_exit_sync, CLOSE_TIMEOUT) < 0)
1853                         PRINT_D(INIT_DBG, "Closed TimedOUT\n");
1854                 else
1855                         PRINT_D(INIT_DBG, "mac_closed\n");
1856
1857                 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1858                         /* close all opened interfaces */
1859                         if (g_linux_wlan->vif[i].wilc_netdev != NULL) {
1860                                 if (nic[i]->mac_opened)
1861                                         mac_close(g_linux_wlan->vif[i].wilc_netdev);
1862                         }
1863                 }
1864                 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1865                         PRINT_D(INIT_DBG, "Unregistering netdev %p\n", g_linux_wlan->vif[i].wilc_netdev);
1866                         unregister_netdev(g_linux_wlan->vif[i].wilc_netdev);
1867                         PRINT_D(INIT_DBG, "Freeing Wiphy...\n");
1868                         wilc_free_wiphy(g_linux_wlan->vif[i].wilc_netdev);
1869                         PRINT_D(INIT_DBG, "Freeing netdev...\n");
1870                         free_netdev(g_linux_wlan->vif[i].wilc_netdev);
1871                 }
1872         }
1873
1874 #ifndef WILC_SDIO
1875         PRINT_D(INIT_DBG, "SPI unregsiter...\n");
1876         spi_unregister_driver(&wilc_bus);
1877 #else
1878         PRINT_D(INIT_DBG, "SDIO unregsiter...\n");
1879         sdio_unregister_driver(&wilc_bus);
1880 #endif
1881
1882         kfree(g_linux_wlan);
1883         g_linux_wlan = NULL;
1884         printk("Module_exit Done.\n");
1885
1886 #if defined(WILC_DEBUGFS)
1887         wilc_debugfs_remove();
1888 #endif
1889
1890         linux_wlan_device_detection(0);
1891         linux_wlan_device_power(0);
1892 }
1893 module_exit(exit_wilc_driver);
1894
1895 MODULE_LICENSE("GPL");