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