]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/wilc1000/linux_wlan.c
staging: wilc1000: removes wilc_dbg()
[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
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/semaphore.h>
27
28 static int dev_state_ev_handler(struct notifier_block *this,
29                                 unsigned long event, void *ptr);
30
31 static struct notifier_block g_dev_notifier = {
32         .notifier_call = dev_state_ev_handler
33 };
34
35 #define IRQ_WAIT        1
36 #define IRQ_NO_WAIT     0
37 static struct semaphore close_exit_sync;
38
39 static int wlan_deinit_locks(struct net_device *dev);
40 static void wlan_deinitialize_threads(struct net_device *dev);
41
42 static void linux_wlan_tx_complete(void *priv, int status);
43 static int  mac_init_fn(struct net_device *ndev);
44 static struct net_device_stats *mac_stats(struct net_device *dev);
45 static int  mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd);
46 static void wilc_set_multicast_list(struct net_device *dev);
47
48 bool wilc_enable_ps = true;
49
50 static const struct net_device_ops wilc_netdev_ops = {
51         .ndo_init = mac_init_fn,
52         .ndo_open = wilc_mac_open,
53         .ndo_stop = wilc_mac_close,
54         .ndo_start_xmit = wilc_mac_xmit,
55         .ndo_do_ioctl = mac_ioctl,
56         .ndo_get_stats = mac_stats,
57         .ndo_set_rx_mode  = wilc_set_multicast_list,
58
59 };
60
61 static int dev_state_ev_handler(struct notifier_block *this,
62                                 unsigned long event, void *ptr)
63 {
64         struct in_ifaddr *dev_iface = ptr;
65         struct wilc_priv *priv;
66         struct host_if_drv *hif_drv;
67         struct net_device *dev;
68         u8 *ip_addr_buf;
69         struct wilc_vif *vif;
70         u8 null_ip[4] = {0};
71         char wlan_dev_name[5] = "wlan0";
72
73         if (!dev_iface || !dev_iface->ifa_dev || !dev_iface->ifa_dev->dev)
74                 return NOTIFY_DONE;
75
76         if (memcmp(dev_iface->ifa_label, "wlan0", 5) &&
77             memcmp(dev_iface->ifa_label, "p2p0", 4))
78                 return NOTIFY_DONE;
79
80         dev  = (struct net_device *)dev_iface->ifa_dev->dev;
81         if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
82                 return NOTIFY_DONE;
83
84         priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
85         if (!priv)
86                 return NOTIFY_DONE;
87
88         hif_drv = (struct host_if_drv *)priv->hif_drv;
89         vif = netdev_priv(dev);
90         if (!vif || !hif_drv)
91                 return NOTIFY_DONE;
92
93         switch (event) {
94         case NETDEV_UP:
95                 if (vif->iftype == STATION_MODE || vif->iftype == CLIENT_MODE) {
96                         hif_drv->IFC_UP = 1;
97                         wilc_optaining_ip = false;
98                         del_timer(&wilc_during_ip_timer);
99                 }
100
101                 if (wilc_enable_ps)
102                         wilc_set_power_mgmt(vif, 1, 0);
103
104                 netdev_dbg(dev, "[%s] Up IP\n", dev_iface->ifa_label);
105
106                 ip_addr_buf = (char *)&dev_iface->ifa_address;
107                 netdev_dbg(dev, "IP add=%d:%d:%d:%d\n",
108                            ip_addr_buf[0], ip_addr_buf[1],
109                            ip_addr_buf[2], ip_addr_buf[3]);
110                 wilc_setup_ipaddress(vif, ip_addr_buf, vif->idx);
111
112                 break;
113
114         case NETDEV_DOWN:
115                 if (vif->iftype == STATION_MODE || vif->iftype == CLIENT_MODE) {
116                         hif_drv->IFC_UP = 0;
117                         wilc_optaining_ip = false;
118                 }
119
120                 if (memcmp(dev_iface->ifa_label, wlan_dev_name, 5) == 0)
121                         wilc_set_power_mgmt(vif, 0, 0);
122
123                 wilc_resolve_disconnect_aberration(vif);
124
125                 netdev_dbg(dev, "[%s] Down IP\n", dev_iface->ifa_label);
126
127                 ip_addr_buf = null_ip;
128                 netdev_dbg(dev, "IP add=%d:%d:%d:%d\n",
129                            ip_addr_buf[0], ip_addr_buf[1],
130                            ip_addr_buf[2], ip_addr_buf[3]);
131
132                 wilc_setup_ipaddress(vif, ip_addr_buf, vif->idx);
133
134                 break;
135
136         default:
137                 break;
138         }
139
140         return NOTIFY_DONE;
141 }
142
143 static irqreturn_t isr_uh_routine(int irq, void *user_data)
144 {
145         struct wilc_vif *vif;
146         struct wilc *wilc;
147         struct net_device *dev = user_data;
148
149         vif = netdev_priv(dev);
150         wilc = vif->wilc;
151
152         if (wilc->close) {
153                 netdev_err(dev, "Can't handle UH interrupt\n");
154                 return IRQ_HANDLED;
155         }
156         return IRQ_WAKE_THREAD;
157 }
158
159 static irqreturn_t isr_bh_routine(int irq, void *userdata)
160 {
161         struct wilc_vif *vif;
162         struct wilc *wilc;
163         struct net_device *dev = userdata;
164
165         vif = netdev_priv(userdata);
166         wilc = vif->wilc;
167
168         if (wilc->close) {
169                 netdev_err(dev, "Can't handle BH interrupt\n");
170                 return IRQ_HANDLED;
171         }
172
173         wilc_handle_isr(wilc);
174
175         return IRQ_HANDLED;
176 }
177
178 static int init_irq(struct net_device *dev)
179 {
180         int ret = 0;
181         struct wilc_vif *vif;
182         struct wilc *wl;
183
184         vif = netdev_priv(dev);
185         wl = vif->wilc;
186
187         if ((gpio_request(wl->gpio, "WILC_INTR") == 0) &&
188             (gpio_direction_input(wl->gpio) == 0)) {
189                 wl->dev_irq_num = gpio_to_irq(wl->gpio);
190         } else {
191                 ret = -1;
192                 netdev_err(dev, "could not obtain gpio for WILC_INTR\n");
193         }
194
195         if (ret != -1 && request_threaded_irq(wl->dev_irq_num,
196                                               isr_uh_routine,
197                                               isr_bh_routine,
198                                               IRQF_TRIGGER_LOW | IRQF_ONESHOT,
199                                               "WILC_IRQ", dev) < 0) {
200                 netdev_err(dev, "Failed to request IRQ GPIO: %d\n", wl->gpio);
201                 gpio_free(wl->gpio);
202                 ret = -1;
203         } else {
204                 netdev_dbg(dev,
205                            "IRQ request succeeded IRQ-NUM= %d on GPIO: %d\n",
206                            wl->dev_irq_num, wl->gpio);
207         }
208
209         return ret;
210 }
211
212 static void deinit_irq(struct net_device *dev)
213 {
214         struct wilc_vif *vif;
215         struct wilc *wilc;
216
217         vif = netdev_priv(dev);
218         wilc = vif->wilc;
219
220         /* Deintialize IRQ */
221         if (wilc->dev_irq_num) {
222                 free_irq(wilc->dev_irq_num, wilc);
223                 gpio_free(wilc->gpio);
224         }
225 }
226
227 int wilc_lock_timeout(struct wilc *nic, void *vp, u32 timeout)
228 {
229         /* FIXME: replace with mutex_lock or wait_for_completion */
230         int error = -1;
231
232         if (vp)
233                 error = down_timeout(vp,
234                                      msecs_to_jiffies(timeout));
235         return error;
236 }
237
238 void wilc_mac_indicate(struct wilc *wilc, int flag)
239 {
240         int status;
241
242         if (flag == WILC_MAC_INDICATE_STATUS) {
243                 wilc_wlan_cfg_get_val(WID_STATUS,
244                                       (unsigned char *)&status, 4);
245                 if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
246                         wilc->mac_status = status;
247                         up(&wilc->sync_event);
248                 } else {
249                         wilc->mac_status = status;
250                 }
251         }
252 }
253
254 static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
255 {
256         u8 *bssid, *bssid1;
257         int i = 0;
258
259         bssid = mac_header + 10;
260         bssid1 = mac_header + 4;
261
262         for (i = 0; i < wilc->vif_num; i++) {
263                 if (wilc->vif[i]->mode == STATION_MODE)
264                         if (!memcmp(bssid, wilc->vif[i]->bssid, ETH_ALEN))
265                                 return wilc->vif[i]->ndev;
266                 if (wilc->vif[i]->mode == AP_MODE)
267                         if (!memcmp(bssid1, wilc->vif[i]->bssid, ETH_ALEN))
268                                 return wilc->vif[i]->ndev;
269         }
270
271         return NULL;
272 }
273
274 int wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
275 {
276         int i = 0;
277         int ret = -1;
278         struct wilc_vif *vif;
279         struct wilc *wilc;
280
281         vif = netdev_priv(wilc_netdev);
282         wilc = vif->wilc;
283
284         for (i = 0; i < wilc->vif_num; i++)
285                 if (wilc->vif[i]->ndev == wilc_netdev) {
286                         memcpy(wilc->vif[i]->bssid, bssid, 6);
287                         wilc->vif[i]->mode = mode;
288                         ret = 0;
289                         break;
290                 }
291
292         return ret;
293 }
294
295 int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
296 {
297         u8 i = 0;
298         u8 null_bssid[6] = {0};
299         u8 ret_val = 0;
300
301         for (i = 0; i < wilc->vif_num; i++)
302                 if (memcmp(wilc->vif[i]->bssid, null_bssid, 6))
303                         ret_val++;
304
305         return ret_val;
306 }
307
308 #define USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
309
310 static int linux_wlan_txq_task(void *vp)
311 {
312         int ret, txq_count;
313         struct wilc_vif *vif;
314         struct wilc *wl;
315         struct net_device *dev = vp;
316 #if defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
317 #define TX_BACKOFF_WEIGHT_INCR_STEP (1)
318 #define TX_BACKOFF_WEIGHT_DECR_STEP (1)
319 #define TX_BACKOFF_WEIGHT_MAX (7)
320 #define TX_BACKOFF_WEIGHT_MIN (0)
321 #define TX_BACKOFF_WEIGHT_UNIT_MS (10)
322         int backoff_weight = TX_BACKOFF_WEIGHT_MIN;
323 #endif
324
325         vif = netdev_priv(dev);
326         wl = vif->wilc;
327
328         up(&wl->txq_thread_started);
329         while (1) {
330                 down(&wl->txq_event);
331
332                 if (wl->close) {
333                         up(&wl->txq_thread_started);
334
335                         while (!kthread_should_stop())
336                                 schedule();
337                         break;
338                 }
339 #if !defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
340                 ret = wilc_wlan_handle_txq(dev, &txq_count);
341 #else
342                 do {
343                         ret = wilc_wlan_handle_txq(dev, &txq_count);
344                         if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
345                                 if (netif_queue_stopped(wl->vif[0]->ndev))
346                                         netif_wake_queue(wl->vif[0]->ndev);
347                                 if (netif_queue_stopped(wl->vif[1]->ndev))
348                                         netif_wake_queue(wl->vif[1]->ndev);
349                         }
350
351                         if (ret == WILC_TX_ERR_NO_BUF) {
352                                 backoff_weight += TX_BACKOFF_WEIGHT_INCR_STEP;
353                                 if (backoff_weight > TX_BACKOFF_WEIGHT_MAX)
354                                         backoff_weight = TX_BACKOFF_WEIGHT_MAX;
355                         } else {
356                                 if (backoff_weight > TX_BACKOFF_WEIGHT_MIN) {
357                                         backoff_weight -= TX_BACKOFF_WEIGHT_DECR_STEP;
358                                         if (backoff_weight < TX_BACKOFF_WEIGHT_MIN)
359                                                 backoff_weight = TX_BACKOFF_WEIGHT_MIN;
360                                 }
361                         }
362                 } while (ret == WILC_TX_ERR_NO_BUF && !wl->close);
363 #endif
364         }
365         return 0;
366 }
367
368 int wilc_wlan_get_firmware(struct net_device *dev)
369 {
370         struct wilc_vif *vif;
371         struct wilc *wilc;
372         int chip_id, ret = 0;
373         const struct firmware *wilc_firmware;
374         char *firmware;
375
376         vif = netdev_priv(dev);
377         wilc = vif->wilc;
378
379         chip_id = wilc_get_chipid(wilc, false);
380
381         if (chip_id < 0x1003a0)
382                 firmware = FIRMWARE_1002;
383         else
384                 firmware = FIRMWARE_1003;
385
386         netdev_info(dev, "loading firmware %s\n", firmware);
387
388         if (!(&vif->ndev->dev))
389                 goto _fail_;
390
391         if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
392                 netdev_err(dev, "%s - firmare not available\n", firmware);
393                 ret = -1;
394                 goto _fail_;
395         }
396         wilc->firmware = wilc_firmware;
397
398 _fail_:
399
400         return ret;
401 }
402
403 static int linux_wlan_start_firmware(struct net_device *dev)
404 {
405         struct wilc_vif *vif;
406         struct wilc *wilc;
407         int ret = 0;
408
409         vif = netdev_priv(dev);
410         wilc = vif->wilc;
411
412         ret = wilc_wlan_start(wilc);
413         if (ret < 0)
414                 return ret;
415
416         ret = wilc_lock_timeout(wilc, &wilc->sync_event, 5000);
417         if (ret)
418                 return ret;
419
420         return 0;
421 }
422
423 static int wilc1000_firmware_download(struct net_device *dev)
424 {
425         struct wilc_vif *vif;
426         struct wilc *wilc;
427         int ret = 0;
428
429         vif = netdev_priv(dev);
430         wilc = vif->wilc;
431
432         if (!wilc->firmware) {
433                 netdev_err(dev, "Firmware buffer is NULL\n");
434                 return -ENOBUFS;
435         }
436
437         ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
438                                           wilc->firmware->size);
439         if (ret < 0)
440                 return ret;
441
442         release_firmware(wilc->firmware);
443         wilc->firmware = NULL;
444
445         netdev_dbg(dev, "Download Succeeded\n");
446
447         return 0;
448 }
449
450 static int linux_wlan_init_test_config(struct net_device *dev,
451                                        struct wilc_vif *vif)
452 {
453         unsigned char c_val[64];
454         unsigned char mac_add[] = {0x00, 0x80, 0xC2, 0x5E, 0xa2, 0xff};
455         struct wilc *wilc = vif->wilc;
456         struct wilc_priv *priv;
457         struct host_if_drv *hif_drv;
458
459         netdev_dbg(dev, "Start configuring Firmware\n");
460         priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
461         hif_drv = (struct host_if_drv *)priv->hif_drv;
462         netdev_dbg(dev, "Host = %p\n", hif_drv);
463         wilc_get_mac_address(vif, mac_add);
464
465         netdev_dbg(dev, "MAC address is : %pM\n", mac_add);
466         wilc_get_chipid(wilc, false);
467
468         *(int *)c_val = 1;
469
470         if (!wilc_wlan_cfg_set(vif, 1, WID_SET_DRV_HANDLER, c_val, 4, 0, 0))
471                 goto _fail_;
472
473         c_val[0] = 0;
474         if (!wilc_wlan_cfg_set(vif, 0, WID_PC_TEST_MODE, c_val, 1, 0, 0))
475                 goto _fail_;
476
477         c_val[0] = INFRASTRUCTURE;
478         if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, c_val, 1, 0, 0))
479                 goto _fail_;
480
481         c_val[0] = RATE_AUTO;
482         if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, c_val, 1, 0, 0))
483                 goto _fail_;
484
485         c_val[0] = G_MIXED_11B_2_MODE;
486         if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, c_val, 1, 0,
487                                0))
488                 goto _fail_;
489
490         c_val[0] = 1;
491         if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_CHANNEL, c_val, 1, 0, 0))
492                 goto _fail_;
493
494         c_val[0] = G_SHORT_PREAMBLE;
495         if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, c_val, 1, 0, 0))
496                 goto _fail_;
497
498         c_val[0] = AUTO_PROT;
499         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, c_val, 1, 0, 0))
500                 goto _fail_;
501
502         c_val[0] = ACTIVE_SCAN;
503         if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, c_val, 1, 0, 0))
504                 goto _fail_;
505
506         c_val[0] = SITE_SURVEY_OFF;
507         if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, c_val, 1, 0, 0))
508                 goto _fail_;
509
510         *((int *)c_val) = 0xffff;
511         if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, c_val, 2, 0, 0))
512                 goto _fail_;
513
514         *((int *)c_val) = 2346;
515         if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, c_val, 2, 0, 0))
516                 goto _fail_;
517
518         c_val[0] = 0;
519         if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, c_val, 1, 0, 0))
520                 goto _fail_;
521
522         c_val[0] = 1;
523         if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, c_val, 1, 0, 0))
524                 goto _fail_;
525
526         c_val[0] = NO_POWERSAVE;
527         if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, c_val, 1, 0, 0))
528                 goto _fail_;
529
530         c_val[0] = NO_SECURITY; /* NO_ENCRYPT, 0x79 */
531         if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, c_val, 1, 0, 0))
532                 goto _fail_;
533
534         c_val[0] = OPEN_SYSTEM;
535         if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, c_val, 1, 0, 0))
536                 goto _fail_;
537
538         strcpy(c_val, "123456790abcdef1234567890");
539         if (!wilc_wlan_cfg_set(vif, 0, WID_WEP_KEY_VALUE, c_val,
540                                (strlen(c_val) + 1), 0, 0))
541                 goto _fail_;
542
543         strcpy(c_val, "12345678");
544         if (!wilc_wlan_cfg_set(vif, 0, WID_11I_PSK, c_val, (strlen(c_val)), 0,
545                                0))
546                 goto _fail_;
547
548         strcpy(c_val, "password");
549         if (!wilc_wlan_cfg_set(vif, 0, WID_1X_KEY, c_val, (strlen(c_val) + 1),
550                                0, 0))
551                 goto _fail_;
552
553         c_val[0] = 192;
554         c_val[1] = 168;
555         c_val[2] = 1;
556         c_val[3] = 112;
557         if (!wilc_wlan_cfg_set(vif, 0, WID_1X_SERV_ADDR, c_val, 4, 0, 0))
558                 goto _fail_;
559
560         c_val[0] = 3;
561         if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, c_val, 1, 0, 0))
562                 goto _fail_;
563
564         c_val[0] = 3;
565         if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, c_val, 1, 0, 0))
566                 goto _fail_;
567
568         c_val[0] = NORMAL_ACK;
569         if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, c_val, 1, 0, 0))
570                 goto _fail_;
571
572         c_val[0] = 0;
573         if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, c_val, 1,
574                                0, 0))
575                 goto _fail_;
576
577         c_val[0] = 48;
578         if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, c_val, 1, 0,
579                                0))
580                 goto _fail_;
581
582         c_val[0] = 28;
583         if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, c_val, 1, 0,
584                                0))
585                 goto _fail_;
586
587         *((int *)c_val) = 100;
588         if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, c_val, 2, 0, 0))
589                 goto _fail_;
590
591         c_val[0] = REKEY_DISABLE;
592         if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, c_val, 1, 0, 0))
593                 goto _fail_;
594
595         *((int *)c_val) = 84600;
596         if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, c_val, 4, 0, 0))
597                 goto _fail_;
598
599         *((int *)c_val) = 500;
600         if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, c_val, 4, 0,
601                                0))
602                 goto _fail_;
603
604         c_val[0] = 1;
605         if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, c_val, 1, 0,
606                                0))
607                 goto _fail_;
608
609         c_val[0] = G_SELF_CTS_PROT;
610         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, c_val, 1, 0, 0))
611                 goto _fail_;
612
613         c_val[0] = 1;
614         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, c_val, 1, 0, 0))
615                 goto _fail_;
616
617         c_val[0] = HT_MIXED_MODE;
618         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, c_val, 1, 0,
619                                0))
620                 goto _fail_;
621
622         c_val[0] = 1;
623         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, c_val, 1, 0,
624                                0))
625                 goto _fail_;
626
627         memcpy(c_val, mac_add, 6);
628
629         if (!wilc_wlan_cfg_set(vif, 0, WID_MAC_ADDR, c_val, 6, 0, 0))
630                 goto _fail_;
631
632         c_val[0] = DETECT_PROTECT_REPORT;
633         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, c_val, 1,
634                                0, 0))
635                 goto _fail_;
636
637         c_val[0] = RTS_CTS_NONHT_PROT;
638         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, c_val, 1, 0, 0))
639                 goto _fail_;
640
641         c_val[0] = 0;
642         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, c_val, 1, 0,
643                                0))
644                 goto _fail_;
645
646         c_val[0] = MIMO_MODE;
647         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_SMPS_MODE, c_val, 1, 0, 0))
648                 goto _fail_;
649
650         c_val[0] = 7;
651         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, c_val, 1, 0,
652                                0))
653                 goto _fail_;
654
655         c_val[0] = 1;
656         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, c_val, 1,
657                                1, 1))
658                 goto _fail_;
659
660         return 0;
661
662 _fail_:
663         return -1;
664 }
665
666 void wilc1000_wlan_deinit(struct net_device *dev)
667 {
668         struct wilc_vif *vif;
669         struct wilc *wl;
670
671         vif = netdev_priv(dev);
672         wl = vif->wilc;
673
674         if (!wl) {
675                 netdev_err(dev, "wl is NULL\n");
676                 return;
677         }
678
679         if (wl->initialized)    {
680                 netdev_info(dev, "Deinitializing wilc1000...\n");
681
682                 if (!wl->dev_irq_num &&
683                     wl->hif_func->disable_interrupt) {
684                         mutex_lock(&wl->hif_cs);
685                         wl->hif_func->disable_interrupt(wl);
686                         mutex_unlock(&wl->hif_cs);
687                 }
688                 if (&wl->txq_event)
689                         up(&wl->txq_event);
690
691                 wlan_deinitialize_threads(dev);
692                 deinit_irq(dev);
693
694                 wilc_wlan_stop(wl);
695                 wilc_wlan_cleanup(dev);
696 #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
697                 if (!wl->dev_irq_num &&
698                     wl->hif_func->disable_interrupt) {
699                         mutex_lock(&wl->hif_cs);
700                         wl->hif_func->disable_interrupt(wl);
701                         mutex_unlock(&wl->hif_cs);
702                 }
703 #endif
704                 wlan_deinit_locks(dev);
705
706                 wl->initialized = false;
707
708                 netdev_dbg(dev, "wilc1000 deinitialization Done\n");
709         } else {
710                 netdev_dbg(dev, "wilc1000 is not initialized\n");
711         }
712 }
713
714 static int wlan_init_locks(struct net_device *dev)
715 {
716         struct wilc_vif *vif;
717         struct wilc *wl;
718
719         vif = netdev_priv(dev);
720         wl = vif->wilc;
721
722         mutex_init(&wl->hif_cs);
723         mutex_init(&wl->rxq_cs);
724
725         spin_lock_init(&wl->txq_spinlock);
726         sema_init(&wl->txq_add_to_head_cs, 1);
727
728         sema_init(&wl->txq_event, 0);
729
730         sema_init(&wl->cfg_event, 0);
731         sema_init(&wl->sync_event, 0);
732
733         sema_init(&wl->txq_thread_started, 0);
734
735         return 0;
736 }
737
738 static int wlan_deinit_locks(struct net_device *dev)
739 {
740         struct wilc_vif *vif;
741         struct wilc *wilc;
742
743         vif = netdev_priv(dev);
744         wilc = vif->wilc;
745
746         if (&wilc->hif_cs)
747                 mutex_destroy(&wilc->hif_cs);
748
749         if (&wilc->rxq_cs)
750                 mutex_destroy(&wilc->rxq_cs);
751
752         return 0;
753 }
754
755 static int wlan_initialize_threads(struct net_device *dev)
756 {
757         struct wilc_vif *vif;
758         struct wilc *wilc;
759
760         vif = netdev_priv(dev);
761         wilc = vif->wilc;
762
763         wilc->txq_thread = kthread_run(linux_wlan_txq_task, (void *)dev,
764                                      "K_TXQ_TASK");
765         if (!wilc->txq_thread) {
766                 netdev_err(dev, "couldn't create TXQ thread\n");
767                 wilc->close = 0;
768                 return -ENOBUFS;
769         }
770         down(&wilc->txq_thread_started);
771
772         return 0;
773 }
774
775 static void wlan_deinitialize_threads(struct net_device *dev)
776 {
777         struct wilc_vif *vif;
778         struct wilc *wl;
779
780         vif = netdev_priv(dev);
781         wl = vif->wilc;
782
783         wl->close = 1;
784
785         if (&wl->txq_event)
786                 up(&wl->txq_event);
787
788         if (wl->txq_thread) {
789                 kthread_stop(wl->txq_thread);
790                 wl->txq_thread = NULL;
791         }
792 }
793
794 int wilc1000_wlan_init(struct net_device *dev, struct wilc_vif *vif)
795 {
796         int ret = 0;
797         struct wilc *wl = vif->wilc;
798
799         if (!wl->initialized) {
800                 wl->mac_status = WILC_MAC_STATUS_INIT;
801                 wl->close = 0;
802
803                 wlan_init_locks(dev);
804
805                 ret = wilc_wlan_init(dev);
806                 if (ret < 0) {
807                         ret = -EIO;
808                         goto _fail_locks_;
809                 }
810
811                 if (wl->gpio >= 0 && init_irq(dev)) {
812                         ret = -EIO;
813                         goto _fail_locks_;
814                 }
815
816                 ret = wlan_initialize_threads(dev);
817                 if (ret < 0) {
818                         ret = -EIO;
819                         goto _fail_wilc_wlan_;
820                 }
821
822                 if (!wl->dev_irq_num &&
823                     wl->hif_func->enable_interrupt &&
824                     wl->hif_func->enable_interrupt(wl)) {
825                         ret = -EIO;
826                         goto _fail_irq_init_;
827                 }
828
829                 if (wilc_wlan_get_firmware(dev)) {
830                         ret = -EIO;
831                         goto _fail_irq_enable_;
832                 }
833
834                 ret = wilc1000_firmware_download(dev);
835                 if (ret < 0) {
836                         ret = -EIO;
837                         goto _fail_irq_enable_;
838                 }
839
840                 ret = linux_wlan_start_firmware(dev);
841                 if (ret < 0) {
842                         ret = -EIO;
843                         goto _fail_irq_enable_;
844                 }
845
846                 if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
847                         int size;
848                         char firmware_ver[20];
849
850                         size = wilc_wlan_cfg_get_val(WID_FIRMWARE_VERSION,
851                                                      firmware_ver,
852                                                      sizeof(firmware_ver));
853                         firmware_ver[size] = '\0';
854                         netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
855                 }
856                 ret = linux_wlan_init_test_config(dev, vif);
857
858                 if (ret < 0) {
859                         netdev_err(dev, "Failed to configure firmware\n");
860                         ret = -EIO;
861                         goto _fail_fw_start_;
862                 }
863
864                 wl->initialized = true;
865                 return 0;
866
867 _fail_fw_start_:
868                 wilc_wlan_stop(wl);
869
870 _fail_irq_enable_:
871                 if (!wl->dev_irq_num &&
872                     wl->hif_func->disable_interrupt)
873                         wl->hif_func->disable_interrupt(wl);
874 _fail_irq_init_:
875                 if (wl->dev_irq_num)
876                         deinit_irq(dev);
877
878                 wlan_deinitialize_threads(dev);
879 _fail_wilc_wlan_:
880                 wilc_wlan_cleanup(dev);
881 _fail_locks_:
882                 wlan_deinit_locks(dev);
883                 netdev_err(dev, "WLAN Iinitialization FAILED\n");
884         } else {
885                 netdev_dbg(dev, "wilc1000 already initialized\n");
886         }
887         return ret;
888 }
889
890 static int mac_init_fn(struct net_device *ndev)
891 {
892         netif_start_queue(ndev);
893         netif_stop_queue(ndev);
894
895         return 0;
896 }
897
898 int wilc_mac_open(struct net_device *ndev)
899 {
900         struct wilc_vif *vif;
901         struct wilc *wilc;
902
903         unsigned char mac_add[ETH_ALEN] = {0};
904         int ret = 0;
905         int i = 0;
906         struct wilc_priv *priv;
907         struct wilc *wl;
908
909         vif = netdev_priv(ndev);
910         wl = vif->wilc;
911
912         if (!wl || !wl->dev) {
913                 netdev_err(ndev, "wilc1000: SPI device not ready\n");
914                 return -ENODEV;
915         }
916
917         vif = netdev_priv(ndev);
918         wilc = vif->wilc;
919         priv = wiphy_priv(vif->ndev->ieee80211_ptr->wiphy);
920         netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
921
922         ret = wilc_init_host_int(ndev);
923         if (ret < 0)
924                 return ret;
925
926         ret = wilc1000_wlan_init(ndev, vif);
927         if (ret < 0) {
928                 wilc_deinit_host_int(ndev);
929                 return ret;
930         }
931
932         for (i = 0; i < wl->vif_num; i++) {
933                 if (ndev == wl->vif[i]->ndev) {
934                         if (vif->iftype == AP_MODE) {
935                                 wilc_set_wfi_drv_handler(vif,
936                                                          wilc_get_vif_idx(vif),
937                                                          0);
938                         } else if (!wilc_wlan_get_num_conn_ifcs(wilc)) {
939                                 wilc_set_wfi_drv_handler(vif,
940                                                          wilc_get_vif_idx(vif),
941                                                          wilc->open_ifcs);
942                         } else {
943                                 if (memcmp(wilc->vif[i ^ 1]->bssid,
944                                            wilc->vif[i ^ 1]->src_addr, 6))
945                                         wilc_set_wfi_drv_handler(vif,
946                                                          wilc_get_vif_idx(vif),
947                                                          0);
948                                 else
949                                         wilc_set_wfi_drv_handler(vif,
950                                                          wilc_get_vif_idx(vif),
951                                                          1);
952                         }
953                         wilc_set_operation_mode(vif, vif->iftype);
954
955                         wilc_get_mac_address(vif, mac_add);
956                         netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
957                         memcpy(wl->vif[i]->src_addr, mac_add, ETH_ALEN);
958
959                         break;
960                 }
961         }
962
963         memcpy(ndev->dev_addr, wl->vif[i]->src_addr, ETH_ALEN);
964
965         if (!is_valid_ether_addr(ndev->dev_addr)) {
966                 netdev_err(ndev, "Wrong MAC address\n");
967                 wilc_deinit_host_int(ndev);
968                 wilc1000_wlan_deinit(ndev);
969                 return -EINVAL;
970         }
971
972         wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
973                                  vif->ndev->ieee80211_ptr,
974                                  vif->g_struct_frame_reg[0].frame_type,
975                                  vif->g_struct_frame_reg[0].reg);
976         wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
977                                  vif->ndev->ieee80211_ptr,
978                                  vif->g_struct_frame_reg[1].frame_type,
979                                  vif->g_struct_frame_reg[1].reg);
980         netif_wake_queue(ndev);
981         wl->open_ifcs++;
982         vif->mac_opened = 1;
983         return 0;
984 }
985
986 static struct net_device_stats *mac_stats(struct net_device *dev)
987 {
988         struct wilc_vif *vif = netdev_priv(dev);
989
990         return &vif->netstats;
991 }
992
993 static void wilc_set_multicast_list(struct net_device *dev)
994 {
995         struct netdev_hw_addr *ha;
996         struct wilc_priv *priv;
997         struct host_if_drv *hif_drv;
998         struct wilc_vif *vif;
999         int i = 0;
1000
1001         priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
1002         vif = netdev_priv(dev);
1003         hif_drv = (struct host_if_drv *)priv->hif_drv;
1004
1005         if (dev->flags & IFF_PROMISC)
1006                 return;
1007
1008         if ((dev->flags & IFF_ALLMULTI) ||
1009             (dev->mc.count) > WILC_MULTICAST_TABLE_SIZE) {
1010                 wilc_setup_multicast_filter(vif, false, 0);
1011                 return;
1012         }
1013
1014         if ((dev->mc.count) == 0) {
1015                 wilc_setup_multicast_filter(vif, true, 0);
1016                 return;
1017         }
1018
1019         netdev_for_each_mc_addr(ha, dev) {
1020                 memcpy(wilc_multicast_mac_addr_list[i], ha->addr, ETH_ALEN);
1021                 netdev_dbg(dev, "Entry[%d]: %x:%x:%x:%x:%x:%x\n", i,
1022                            wilc_multicast_mac_addr_list[i][0],
1023                            wilc_multicast_mac_addr_list[i][1],
1024                            wilc_multicast_mac_addr_list[i][2],
1025                            wilc_multicast_mac_addr_list[i][3],
1026                            wilc_multicast_mac_addr_list[i][4],
1027                            wilc_multicast_mac_addr_list[i][5]);
1028                 i++;
1029         }
1030
1031         wilc_setup_multicast_filter(vif, true, (dev->mc.count));
1032 }
1033
1034 static void linux_wlan_tx_complete(void *priv, int status)
1035 {
1036         struct tx_complete_data *pv_data = priv;
1037
1038         dev_kfree_skb(pv_data->skb);
1039         kfree(pv_data);
1040 }
1041
1042 int wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
1043 {
1044         struct wilc_vif *vif;
1045         struct tx_complete_data *tx_data = NULL;
1046         int queue_count;
1047         char *udp_buf;
1048         struct iphdr *ih;
1049         struct ethhdr *eth_h;
1050         struct wilc *wilc;
1051
1052         vif = netdev_priv(ndev);
1053         wilc = vif->wilc;
1054
1055         if (skb->dev != ndev) {
1056                 netdev_err(ndev, "Packet not destined to this device\n");
1057                 return 0;
1058         }
1059
1060         tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
1061         if (!tx_data) {
1062                 dev_kfree_skb(skb);
1063                 netif_wake_queue(ndev);
1064                 return 0;
1065         }
1066
1067         tx_data->buff = skb->data;
1068         tx_data->size = skb->len;
1069         tx_data->skb  = skb;
1070
1071         eth_h = (struct ethhdr *)(skb->data);
1072         if (eth_h->h_proto == 0x8e88)
1073                 netdev_dbg(ndev, "EAPOL transmitted\n");
1074
1075         ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
1076
1077         udp_buf = (char *)ih + sizeof(struct iphdr);
1078         if ((udp_buf[1] == 68 && udp_buf[3] == 67) ||
1079             (udp_buf[1] == 67 && udp_buf[3] == 68))
1080                 netdev_dbg(ndev, "DHCP Message transmitted, type:%x %x %x\n",
1081                            udp_buf[248], udp_buf[249], udp_buf[250]);
1082
1083         vif->netstats.tx_packets++;
1084         vif->netstats.tx_bytes += tx_data->size;
1085         tx_data->bssid = wilc->vif[vif->idx]->bssid;
1086         queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
1087                                                 tx_data->buff, tx_data->size,
1088                                                 linux_wlan_tx_complete);
1089
1090         if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
1091                 netif_stop_queue(wilc->vif[0]->ndev);
1092                 netif_stop_queue(wilc->vif[1]->ndev);
1093         }
1094
1095         return 0;
1096 }
1097
1098 int wilc_mac_close(struct net_device *ndev)
1099 {
1100         struct wilc_priv *priv;
1101         struct wilc_vif *vif;
1102         struct host_if_drv *hif_drv;
1103         struct wilc *wl;
1104
1105         vif = netdev_priv(ndev);
1106
1107         if (!vif || !vif->ndev || !vif->ndev->ieee80211_ptr ||
1108             !vif->ndev->ieee80211_ptr->wiphy)
1109                 return 0;
1110
1111         priv = wiphy_priv(vif->ndev->ieee80211_ptr->wiphy);
1112         wl = vif->wilc;
1113
1114         if (!priv)
1115                 return 0;
1116
1117         hif_drv = (struct host_if_drv *)priv->hif_drv;
1118
1119         netdev_dbg(ndev, "Mac close\n");
1120
1121         if (!wl)
1122                 return 0;
1123
1124         if (!hif_drv)
1125                 return 0;
1126
1127         if ((wl->open_ifcs) > 0)
1128                 wl->open_ifcs--;
1129         else
1130                 return 0;
1131
1132         if (vif->ndev) {
1133                 netif_stop_queue(vif->ndev);
1134
1135                 wilc_deinit_host_int(vif->ndev);
1136         }
1137
1138         if (wl->open_ifcs == 0) {
1139                 netdev_dbg(ndev, "Deinitializing wilc1000\n");
1140                 wl->close = 1;
1141                 wilc1000_wlan_deinit(ndev);
1142                 WILC_WFI_deinit_mon_interface();
1143         }
1144
1145         up(&close_exit_sync);
1146         vif->mac_opened = 0;
1147
1148         return 0;
1149 }
1150
1151 static int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1152 {
1153         u8 *buff = NULL;
1154         s8 rssi;
1155         u32 size = 0, length = 0;
1156         struct wilc_vif *vif;
1157         struct wilc_priv *priv;
1158         s32 ret = 0;
1159         struct wilc *wilc;
1160
1161         vif = netdev_priv(ndev);
1162         wilc = vif->wilc;
1163
1164         if (!wilc->initialized)
1165                 return 0;
1166
1167         switch (cmd) {
1168         case SIOCSIWPRIV:
1169         {
1170                 struct iwreq *wrq = (struct iwreq *)req;
1171
1172                 size = wrq->u.data.length;
1173
1174                 if (size && wrq->u.data.pointer) {
1175                         buff = memdup_user(wrq->u.data.pointer,
1176                                            wrq->u.data.length);
1177                         if (IS_ERR(buff))
1178                                 return PTR_ERR(buff);
1179
1180                         if (strncasecmp(buff, "RSSI", length) == 0) {
1181                                 priv = wiphy_priv(vif->ndev->ieee80211_ptr->wiphy);
1182                                 ret = wilc_get_rssi(vif, &rssi);
1183                                 netdev_info(ndev, "RSSI :%d\n", rssi);
1184
1185                                 rssi += 5;
1186
1187                                 snprintf(buff, size, "rssi %d", rssi);
1188
1189                                 if (copy_to_user(wrq->u.data.pointer, buff, size)) {
1190                                         netdev_err(ndev, "failed to copy\n");
1191                                         ret = -EFAULT;
1192                                         goto done;
1193                                 }
1194                         }
1195                 }
1196         }
1197         break;
1198
1199         default:
1200         {
1201                 netdev_info(ndev, "Command - %d - has been received\n", cmd);
1202                 ret = -EOPNOTSUPP;
1203                 goto done;
1204         }
1205         }
1206
1207 done:
1208
1209         kfree(buff);
1210
1211         return ret;
1212 }
1213
1214 void wilc_frmw_to_linux(struct wilc *wilc, u8 *buff, u32 size, u32 pkt_offset)
1215 {
1216         unsigned int frame_len = 0;
1217         int stats;
1218         unsigned char *buff_to_send = NULL;
1219         struct sk_buff *skb;
1220         struct net_device *wilc_netdev;
1221         struct wilc_vif *vif;
1222
1223         if (!wilc)
1224                 return;
1225
1226         wilc_netdev = get_if_handler(wilc, buff);
1227         if (!wilc_netdev)
1228                 return;
1229
1230         buff += pkt_offset;
1231         vif = netdev_priv(wilc_netdev);
1232
1233         if (size > 0) {
1234                 frame_len = size;
1235                 buff_to_send = buff;
1236
1237                 skb = dev_alloc_skb(frame_len);
1238                 if (!skb)
1239                         return;
1240
1241                 skb->dev = wilc_netdev;
1242
1243                 memcpy(skb_put(skb, frame_len), buff_to_send, frame_len);
1244
1245                 skb->protocol = eth_type_trans(skb, wilc_netdev);
1246                 vif->netstats.rx_packets++;
1247                 vif->netstats.rx_bytes += frame_len;
1248                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1249                 stats = netif_rx(skb);
1250                 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
1251         }
1252 }
1253
1254 void WILC_WFI_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
1255 {
1256         int i = 0;
1257         struct wilc_vif *vif;
1258
1259         for (i = 0; i < wilc->vif_num; i++) {
1260                 vif = netdev_priv(wilc->vif[i]->ndev);
1261                 if (vif->monitor_flag) {
1262                         WILC_WFI_monitor_rx(buff, size);
1263                         return;
1264                 }
1265         }
1266
1267         vif = netdev_priv(wilc->vif[1]->ndev);
1268         if ((buff[0] == vif->g_struct_frame_reg[0].frame_type && vif->g_struct_frame_reg[0].reg) ||
1269             (buff[0] == vif->g_struct_frame_reg[1].frame_type && vif->g_struct_frame_reg[1].reg))
1270                 WILC_WFI_p2p_rx(wilc->vif[1]->ndev, buff, size);
1271 }
1272
1273 void wilc_netdev_cleanup(struct wilc *wilc)
1274 {
1275         int i = 0;
1276         struct wilc_vif *vif[NUM_CONCURRENT_IFC];
1277
1278         if (wilc && (wilc->vif[0]->ndev || wilc->vif[1]->ndev)) {
1279                 unregister_inetaddr_notifier(&g_dev_notifier);
1280
1281                 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
1282                         vif[i] = netdev_priv(wilc->vif[i]->ndev);
1283         }
1284
1285         if (wilc && wilc->firmware)
1286                 release_firmware(wilc->firmware);
1287
1288         if (wilc && (wilc->vif[0]->ndev || wilc->vif[1]->ndev)) {
1289                 wilc_lock_timeout(wilc, &close_exit_sync, 5 * 1000);
1290
1291                 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
1292                         if (wilc->vif[i]->ndev)
1293                                 if (vif[i]->mac_opened)
1294                                         wilc_mac_close(wilc->vif[i]->ndev);
1295
1296                 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1297                         unregister_netdev(wilc->vif[i]->ndev);
1298                         wilc_free_wiphy(wilc->vif[i]->ndev);
1299                         free_netdev(wilc->vif[i]->ndev);
1300                 }
1301         }
1302
1303         kfree(wilc);
1304 }
1305 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
1306
1307 int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
1308                      int gpio, const struct wilc_hif_func *ops)
1309 {
1310         int i, ret;
1311         struct wilc_vif *vif;
1312         struct net_device *ndev;
1313         struct wilc *wl;
1314
1315         sema_init(&close_exit_sync, 0);
1316
1317         wl = kzalloc(sizeof(*wl), GFP_KERNEL);
1318         if (!wl)
1319                 return -ENOMEM;
1320
1321         *wilc = wl;
1322         wl->io_type = io_type;
1323         wl->gpio = gpio;
1324         wl->hif_func = ops;
1325
1326         register_inetaddr_notifier(&g_dev_notifier);
1327
1328         for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1329                 ndev = alloc_etherdev(sizeof(struct wilc_vif));
1330                 if (!ndev)
1331                         return -ENOMEM;
1332
1333                 vif = netdev_priv(ndev);
1334                 memset(vif, 0, sizeof(struct wilc_vif));
1335
1336                 if (i == 0)
1337                         strcpy(ndev->name, "wlan%d");
1338                 else
1339                         strcpy(ndev->name, "p2p%d");
1340
1341                 vif->idx = wl->vif_num;
1342                 vif->wilc = *wilc;
1343                 wl->vif[i] = vif;
1344                 wl->vif[wl->vif_num]->ndev = ndev;
1345                 wl->vif_num++;
1346                 ndev->netdev_ops = &wilc_netdev_ops;
1347
1348                 {
1349                         struct wireless_dev *wdev;
1350
1351                         wdev = wilc_create_wiphy(ndev, dev);
1352
1353                         if (dev)
1354                                 SET_NETDEV_DEV(ndev, dev);
1355
1356                         if (!wdev) {
1357                                 netdev_err(ndev, "Can't register WILC Wiphy\n");
1358                                 return -1;
1359                         }
1360
1361                         vif->ndev->ieee80211_ptr = wdev;
1362                         vif->ndev->ml_priv = vif;
1363                         wdev->netdev = vif->ndev;
1364                         vif->netstats.rx_packets = 0;
1365                         vif->netstats.tx_packets = 0;
1366                         vif->netstats.rx_bytes = 0;
1367                         vif->netstats.tx_bytes = 0;
1368                 }
1369
1370                 ret = register_netdev(ndev);
1371                 if (ret)
1372                         return ret;
1373
1374                 vif->iftype = STATION_MODE;
1375                 vif->mac_opened = 0;
1376         }
1377
1378         return 0;
1379 }
1380 EXPORT_SYMBOL_GPL(wilc_netdev_init);
1381
1382 MODULE_LICENSE("GPL");