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