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