Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / libertas / main.c
1 /**
2   * This file contains the major functions in WLAN
3   * driver. It includes init, exit, open, close and main
4   * thread etc..
5   */
6
7 #include <linux/delay.h>
8 #include <linux/freezer.h>
9 #include <linux/etherdevice.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12
13 #include <net/iw_handler.h>
14
15 #include "host.h"
16 #include "sbi.h"
17 #include "decl.h"
18 #include "dev.h"
19 #include "fw.h"
20 #include "wext.h"
21 #include "debugfs.h"
22 #include "assoc.h"
23
24 #define DRIVER_RELEASE_VERSION "320.p0"
25 const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
26 #ifdef  DEBUG
27     "-dbg"
28 #endif
29     "";
30
31 #ifdef ENABLE_PM
32 static struct pm_dev *wlan_pm_dev = NULL;
33 #endif
34
35 #define WLAN_TX_PWR_DEFAULT             20      /*100mW */
36 #define WLAN_TX_PWR_US_DEFAULT          20      /*100mW */
37 #define WLAN_TX_PWR_JP_DEFAULT          16      /*50mW */
38 #define WLAN_TX_PWR_FR_DEFAULT          20      /*100mW */
39 #define WLAN_TX_PWR_EMEA_DEFAULT        20      /*100mW */
40
41 /* Format { channel, frequency (MHz), maxtxpower } */
42 /* band: 'B/G', region: USA FCC/Canada IC */
43 static struct chan_freq_power channel_freq_power_US_BG[] = {
44         {1, 2412, WLAN_TX_PWR_US_DEFAULT},
45         {2, 2417, WLAN_TX_PWR_US_DEFAULT},
46         {3, 2422, WLAN_TX_PWR_US_DEFAULT},
47         {4, 2427, WLAN_TX_PWR_US_DEFAULT},
48         {5, 2432, WLAN_TX_PWR_US_DEFAULT},
49         {6, 2437, WLAN_TX_PWR_US_DEFAULT},
50         {7, 2442, WLAN_TX_PWR_US_DEFAULT},
51         {8, 2447, WLAN_TX_PWR_US_DEFAULT},
52         {9, 2452, WLAN_TX_PWR_US_DEFAULT},
53         {10, 2457, WLAN_TX_PWR_US_DEFAULT},
54         {11, 2462, WLAN_TX_PWR_US_DEFAULT}
55 };
56
57 /* band: 'B/G', region: Europe ETSI */
58 static struct chan_freq_power channel_freq_power_EU_BG[] = {
59         {1, 2412, WLAN_TX_PWR_EMEA_DEFAULT},
60         {2, 2417, WLAN_TX_PWR_EMEA_DEFAULT},
61         {3, 2422, WLAN_TX_PWR_EMEA_DEFAULT},
62         {4, 2427, WLAN_TX_PWR_EMEA_DEFAULT},
63         {5, 2432, WLAN_TX_PWR_EMEA_DEFAULT},
64         {6, 2437, WLAN_TX_PWR_EMEA_DEFAULT},
65         {7, 2442, WLAN_TX_PWR_EMEA_DEFAULT},
66         {8, 2447, WLAN_TX_PWR_EMEA_DEFAULT},
67         {9, 2452, WLAN_TX_PWR_EMEA_DEFAULT},
68         {10, 2457, WLAN_TX_PWR_EMEA_DEFAULT},
69         {11, 2462, WLAN_TX_PWR_EMEA_DEFAULT},
70         {12, 2467, WLAN_TX_PWR_EMEA_DEFAULT},
71         {13, 2472, WLAN_TX_PWR_EMEA_DEFAULT}
72 };
73
74 /* band: 'B/G', region: Spain */
75 static struct chan_freq_power channel_freq_power_SPN_BG[] = {
76         {10, 2457, WLAN_TX_PWR_DEFAULT},
77         {11, 2462, WLAN_TX_PWR_DEFAULT}
78 };
79
80 /* band: 'B/G', region: France */
81 static struct chan_freq_power channel_freq_power_FR_BG[] = {
82         {10, 2457, WLAN_TX_PWR_FR_DEFAULT},
83         {11, 2462, WLAN_TX_PWR_FR_DEFAULT},
84         {12, 2467, WLAN_TX_PWR_FR_DEFAULT},
85         {13, 2472, WLAN_TX_PWR_FR_DEFAULT}
86 };
87
88 /* band: 'B/G', region: Japan */
89 static struct chan_freq_power channel_freq_power_JPN_BG[] = {
90         {1, 2412, WLAN_TX_PWR_JP_DEFAULT},
91         {2, 2417, WLAN_TX_PWR_JP_DEFAULT},
92         {3, 2422, WLAN_TX_PWR_JP_DEFAULT},
93         {4, 2427, WLAN_TX_PWR_JP_DEFAULT},
94         {5, 2432, WLAN_TX_PWR_JP_DEFAULT},
95         {6, 2437, WLAN_TX_PWR_JP_DEFAULT},
96         {7, 2442, WLAN_TX_PWR_JP_DEFAULT},
97         {8, 2447, WLAN_TX_PWR_JP_DEFAULT},
98         {9, 2452, WLAN_TX_PWR_JP_DEFAULT},
99         {10, 2457, WLAN_TX_PWR_JP_DEFAULT},
100         {11, 2462, WLAN_TX_PWR_JP_DEFAULT},
101         {12, 2467, WLAN_TX_PWR_JP_DEFAULT},
102         {13, 2472, WLAN_TX_PWR_JP_DEFAULT},
103         {14, 2484, WLAN_TX_PWR_JP_DEFAULT}
104 };
105
106 /**
107  * the structure for channel, frequency and power
108  */
109 struct region_cfp_table {
110         u8 region;
111         struct chan_freq_power *cfp_BG;
112         int cfp_no_BG;
113 };
114
115 /**
116  * the structure for the mapping between region and CFP
117  */
118 static struct region_cfp_table region_cfp_table[] = {
119         {0x10,                  /*US FCC */
120          channel_freq_power_US_BG,
121          sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
122          }
123         ,
124         {0x20,                  /*CANADA IC */
125          channel_freq_power_US_BG,
126          sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
127          }
128         ,
129         {0x30, /*EU*/ channel_freq_power_EU_BG,
130          sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
131          }
132         ,
133         {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
134          sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
135          }
136         ,
137         {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
138          sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
139          }
140         ,
141         {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
142          sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
143          }
144         ,
145 /*Add new region here */
146 };
147
148 /**
149  * the rates supported by the card
150  */
151 u8 libertas_wlan_data_rates[WLAN_SUPPORTED_RATES] =
152     { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
153         0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
154 };
155
156 /**
157  * the rates supported
158  */
159 u8 libertas_supported_rates[G_SUPPORTED_RATES] =
160     { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
161 0 };
162
163 /**
164  * the rates supported for ad-hoc G mode
165  */
166 u8 libertas_adhoc_rates_g[G_SUPPORTED_RATES] =
167     { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
168 0 };
169
170 /**
171  * the rates supported for ad-hoc B mode
172  */
173 u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
174
175 /**
176  * the global variable of a pointer to wlan_private
177  * structure variable
178  */
179 static wlan_private *wlanpriv = NULL;
180
181 #define MAX_DEVS 5
182 static struct net_device *libertas_devs[MAX_DEVS];
183 static int libertas_found = 0;
184
185 /**
186  * the table to keep region code
187  */
188 u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
189     { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
190
191 static u8 *default_fw_name = "usb8388.bin";
192
193 /**
194  * Attributes exported through sysfs
195  */
196
197 /**
198  * @brief Get function for sysfs attribute libertas_mpp
199  */
200 static ssize_t libertas_mpp_get(struct device * dev,
201                 struct device_attribute *attr, char * buf) {
202         struct cmd_ds_mesh_access mesh_access;
203
204         memset(&mesh_access, 0, sizeof(mesh_access));
205         libertas_prepare_and_send_command(to_net_dev(dev)->priv,
206                         cmd_mesh_access,
207                         cmd_act_mesh_get_mpp,
208                         cmd_option_waitforrsp, 0, (void *)&mesh_access);
209
210         return snprintf(buf, 3, "%d\n", mesh_access.data[0]);
211 }
212
213 /**
214  * @brief Set function for sysfs attribute libertas_mpp
215  */
216 static ssize_t libertas_mpp_set(struct device * dev,
217                 struct device_attribute *attr, const char * buf, size_t count) {
218         struct cmd_ds_mesh_access mesh_access;
219
220
221         memset(&mesh_access, 0, sizeof(mesh_access));
222         sscanf(buf, "%d", &(mesh_access.data[0]));
223         libertas_prepare_and_send_command((to_net_dev(dev))->priv,
224                         cmd_mesh_access,
225                         cmd_act_mesh_set_mpp,
226                         cmd_option_waitforrsp, 0, (void *)&mesh_access);
227         return strlen(buf);
228 }
229
230 /**
231  * libertas_mpp attribute to be exported per mshX interface
232  * through sysfs (/sys/class/net/mshX/libertas-mpp)
233  */
234 static DEVICE_ATTR(libertas_mpp, 0644, libertas_mpp_get,
235                 libertas_mpp_set );
236
237 /**
238  *  @brief Check if the device can be open and wait if necessary.
239  *
240  *  @param dev     A pointer to net_device structure
241  *  @return        0
242  *
243  * For USB adapter, on some systems the device open handler will be
244  * called before FW ready. Use the following flag check and wait
245  * function to work around the issue.
246  *
247  */
248 static int pre_open_check(struct net_device *dev) {
249         wlan_private *priv = (wlan_private *) dev->priv;
250         wlan_adapter *adapter = priv->adapter;
251         int i = 0;
252
253         while (!adapter->fw_ready && i < 20) {
254                 i++;
255                 msleep_interruptible(100);
256         }
257         if (!adapter->fw_ready) {
258                 lbs_pr_info("FW not ready, pre_open_check() return failure\n");
259                 LEAVE();
260                 return -1;
261         }
262
263         return 0;
264 }
265
266 /**
267  *  @brief This function opens the device
268  *
269  *  @param dev     A pointer to net_device structure
270  *  @return        0
271  */
272 static int wlan_dev_open(struct net_device *dev)
273 {
274         wlan_private *priv = (wlan_private *) dev->priv;
275         wlan_adapter *adapter = priv->adapter;
276
277         ENTER();
278
279
280         priv->open = 1;
281
282         if (adapter->connect_status == libertas_connected) {
283                 netif_carrier_on(priv->wlan_dev.netdev);
284         } else
285                 netif_carrier_off(priv->wlan_dev.netdev);
286
287         LEAVE();
288         return 0;
289 }
290 /**
291  *  @brief This function opens the mshX interface
292  *
293  *  @param dev     A pointer to net_device structure
294  *  @return        0
295  */
296 static int mesh_open(struct net_device *dev)
297 {
298         wlan_private *priv = (wlan_private *) dev->priv ;
299
300         if(pre_open_check(dev) == -1)
301                 return -1;
302         priv->mesh_open = 1 ;
303         netif_start_queue(priv->mesh_dev);
304         if (priv->infra_open == 0)
305                 return wlan_dev_open(priv->wlan_dev.netdev) ;
306         return 0;
307 }
308
309 /**
310  *  @brief This function opens the ethX interface
311  *
312  *  @param dev     A pointer to net_device structure
313  *  @return        0
314  */
315 static int wlan_open(struct net_device *dev)
316 {
317         wlan_private *priv = (wlan_private *) dev->priv ;
318
319         if(pre_open_check(dev) == -1)
320                 return -1;
321         priv->infra_open = 1 ;
322         netif_wake_queue(priv->wlan_dev.netdev);
323         if (priv->open == 0)
324                 return wlan_dev_open(priv->wlan_dev.netdev) ;
325         return 0;
326 }
327
328 static int wlan_dev_close(struct net_device *dev)
329 {
330         wlan_private *priv = dev->priv;
331
332         ENTER();
333
334         netif_carrier_off(priv->wlan_dev.netdev);
335         priv->open = 0;
336
337         LEAVE();
338         return 0;
339 }
340
341 /**
342  *  @brief This function closes the mshX interface
343  *
344  *  @param dev     A pointer to net_device structure
345  *  @return        0
346  */
347 static int mesh_close(struct net_device *dev)
348 {
349         wlan_private *priv = (wlan_private *) (dev->priv);
350
351         priv->mesh_open = 0;
352         netif_stop_queue(priv->mesh_dev);
353         if (priv->infra_open == 0)
354                 return wlan_dev_close( ((wlan_private *) dev->priv)->wlan_dev.netdev) ;
355         else
356                 return 0;
357 }
358
359 /**
360  *  @brief This function closes the ethX interface
361  *
362  *  @param dev     A pointer to net_device structure
363  *  @return        0
364  */
365 static int wlan_close(struct net_device *dev) {
366         wlan_private *priv = (wlan_private *) dev->priv;
367
368         netif_stop_queue(priv->wlan_dev.netdev);
369         priv->infra_open = 0;
370         if (priv->mesh_open == 0)
371                 return wlan_dev_close( ((wlan_private *) dev->priv)->wlan_dev.netdev) ;
372         else
373                 return 0;
374 }
375
376
377 #ifdef ENABLE_PM
378
379 /**
380  *  @brief This function is a callback function. it is called by
381  *  kernel to enter or exit power saving mode.
382  *
383  *  @param pmdev   A pointer to pm_dev
384  *  @param pmreq   pm_request_t
385  *  @param pmdata  A pointer to pmdata
386  *  @return        0 or -1
387  */
388 static int wlan_pm_callback(struct pm_dev *pmdev, pm_request_t pmreq,
389                             void *pmdata)
390 {
391         wlan_private *priv = wlanpriv;
392         wlan_adapter *adapter = priv->adapter;
393         struct net_device *dev = priv->wlan_dev.netdev;
394
395         lbs_pr_debug(1, "WPRM_PM_CALLBACK: pmreq = %d.\n", pmreq);
396
397         switch (pmreq) {
398         case PM_SUSPEND:
399                 lbs_pr_debug(1, "WPRM_PM_CALLBACK: enter PM_SUSPEND.\n");
400
401                 /* in associated mode */
402                 if (adapter->connect_status == libertas_connected) {
403                         if ((adapter->psstate != PS_STATE_SLEEP)
404                             ) {
405                                 lbs_pr_debug(1,
406                                        "wlan_pm_callback: can't enter sleep mode\n");
407                                 return -1;
408                         } else {
409
410                                 /*
411                                  * Detach the network interface
412                                  * if the network is running
413                                  */
414                                 if (netif_running(dev)) {
415                                         netif_device_detach(dev);
416                                         lbs_pr_debug(1,
417                                                "netif_device_detach().\n");
418                                 }
419                                 libertas_sbi_suspend(priv);
420                         }
421                         break;
422                 }
423
424                 /* in non associated mode */
425
426                 /*
427                  * Detach the network interface
428                  * if the network is running
429                  */
430                 if (netif_running(dev))
431                         netif_device_detach(dev);
432
433                 /*
434                  * Storing and restoring of the regs be taken care
435                  * at the driver rest will be done at wlan driver
436                  * this makes driver independent of the card
437                  */
438
439                 libertas_sbi_suspend(priv);
440
441                 break;
442
443         case PM_RESUME:
444                 /* in associated mode */
445                 if (adapter->connect_status == libertas_connected) {
446                         {
447                                 /*
448                                  * Bring the inteface up first
449                                  * This case should not happen still ...
450                                  */
451                                 libertas_sbi_resume(priv);
452
453                                 /*
454                                  * Attach the network interface
455                                  * if the network is running
456                                  */
457                                 if (netif_running(dev)) {
458                                         netif_device_attach(dev);
459                                         lbs_pr_debug(1,
460                                                "after netif_device_attach().\n");
461                                 }
462                                 lbs_pr_debug(1,
463                                        "After netif attach, in associated mode.\n");
464                         }
465                         break;
466                 }
467
468                 /* in non associated mode */
469
470                 /*
471                  * Bring the inteface up first
472                  * This case should not happen still ...
473                  */
474
475                 libertas_sbi_resume(priv);
476
477                 if (netif_running(dev))
478                         netif_device_attach(dev);
479
480                 lbs_pr_debug(1, "after netif attach, in NON associated mode.\n");
481                 break;
482         }
483
484         return 0;
485 }
486 #endif                          /* ENABLE_PM */
487
488 static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
489 {
490         int ret = 0;
491         wlan_private *priv = dev->priv;
492
493         ENTER();
494
495         if (priv->wlan_dev.dnld_sent || priv->adapter->TxLockFlag) {
496                 priv->stats.tx_dropped++;
497                 goto done;
498         }
499
500         netif_stop_queue(priv->wlan_dev.netdev);
501
502         if (libertas_process_tx(priv, skb) == 0)
503                 dev->trans_start = jiffies;
504 done:
505         LEAVE();
506         return ret;
507 }
508
509 /**
510  * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
511  *
512  */
513 static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
514 {
515         wlan_private *priv = dev->priv;
516         ENTER();
517         SET_MESH_FRAME(skb);
518         LEAVE();
519
520         return wlan_hard_start_xmit(skb, priv->wlan_dev.netdev);
521 }
522
523 /**
524  * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
525  *
526  */
527 static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev) {
528         ENTER();
529         UNSET_MESH_FRAME(skb);
530         LEAVE();
531         return wlan_hard_start_xmit(skb, dev);
532 }
533
534 static void wlan_tx_timeout(struct net_device *dev)
535 {
536         wlan_private *priv = (wlan_private *) dev->priv;
537
538         ENTER();
539
540         lbs_pr_err("tx watch dog timeout!\n");
541
542         priv->wlan_dev.dnld_sent = DNLD_RES_RECEIVED;
543         dev->trans_start = jiffies;
544
545         if (priv->adapter->currenttxskb) {
546                 if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
547                         /* If we are here, we have not received feedback from
548                            the previous packet.  Assume TX_FAIL and move on. */
549                         priv->adapter->eventcause = 0x01000000;
550                         libertas_send_tx_feedback(priv);
551                 } else
552                         wake_up_interruptible(&priv->mainthread.waitq);
553         } else if (priv->adapter->connect_status == libertas_connected)
554                 netif_wake_queue(priv->wlan_dev.netdev);
555
556         LEAVE();
557 }
558
559 /**
560  *  @brief This function returns the network statistics
561  *
562  *  @param dev     A pointer to wlan_private structure
563  *  @return        A pointer to net_device_stats structure
564  */
565 static struct net_device_stats *wlan_get_stats(struct net_device *dev)
566 {
567         wlan_private *priv = (wlan_private *) dev->priv;
568
569         return &priv->stats;
570 }
571
572 static int wlan_set_mac_address(struct net_device *dev, void *addr)
573 {
574         int ret = 0;
575         wlan_private *priv = (wlan_private *) dev->priv;
576         wlan_adapter *adapter = priv->adapter;
577         struct sockaddr *phwaddr = addr;
578
579         ENTER();
580
581         memset(adapter->current_addr, 0, ETH_ALEN);
582
583         /* dev->dev_addr is 8 bytes */
584         lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
585
586         lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
587         memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
588
589         ret = libertas_prepare_and_send_command(priv, cmd_802_11_mac_address,
590                                     cmd_act_set,
591                                     cmd_option_waitforrsp, 0, NULL);
592
593         if (ret) {
594                 lbs_pr_debug(1, "set mac address failed.\n");
595                 ret = -1;
596                 goto done;
597         }
598
599         lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
600         memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
601         memcpy(((wlan_private *) dev->priv)->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
602
603 done:
604         LEAVE();
605         return ret;
606 }
607
608 static int wlan_copy_multicast_address(wlan_adapter * adapter,
609                                      struct net_device *dev)
610 {
611         int i = 0;
612         struct dev_mc_list *mcptr = dev->mc_list;
613
614         for (i = 0; i < dev->mc_count; i++) {
615                 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
616                 mcptr = mcptr->next;
617         }
618
619         return i;
620
621 }
622
623 static void wlan_set_multicast_list(struct net_device *dev)
624 {
625         wlan_private *priv = dev->priv;
626         wlan_adapter *adapter = priv->adapter;
627         int oldpacketfilter;
628
629         ENTER();
630
631         oldpacketfilter = adapter->currentpacketfilter;
632
633         if (dev->flags & IFF_PROMISC) {
634                 lbs_pr_debug(1, "enable Promiscuous mode\n");
635                 adapter->currentpacketfilter |=
636                     cmd_act_mac_promiscuous_enable;
637                 adapter->currentpacketfilter &=
638                     ~(cmd_act_mac_all_multicast_enable |
639                       cmd_act_mac_multicast_enable);
640         } else {
641                 /* Multicast */
642                 adapter->currentpacketfilter &=
643                     ~cmd_act_mac_promiscuous_enable;
644
645                 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
646                     MRVDRV_MAX_MULTICAST_LIST_SIZE) {
647                         lbs_pr_debug(1, "Enabling All Multicast!\n");
648                         adapter->currentpacketfilter |=
649                             cmd_act_mac_all_multicast_enable;
650                         adapter->currentpacketfilter &=
651                             ~cmd_act_mac_multicast_enable;
652                 } else {
653                         adapter->currentpacketfilter &=
654                             ~cmd_act_mac_all_multicast_enable;
655
656                         if (!dev->mc_count) {
657                                 lbs_pr_debug(1, "No multicast addresses - "
658                                        "disabling multicast!\n");
659                                 adapter->currentpacketfilter &=
660                                     ~cmd_act_mac_multicast_enable;
661                         } else {
662                                 int i;
663
664                                 adapter->currentpacketfilter |=
665                                     cmd_act_mac_multicast_enable;
666
667                                 adapter->nr_of_multicastmacaddr =
668                                     wlan_copy_multicast_address(adapter, dev);
669
670                                 lbs_pr_debug(1, "Multicast addresses: %d\n",
671                                        dev->mc_count);
672
673                                 for (i = 0; i < dev->mc_count; i++) {
674                                         lbs_pr_debug(1, "Multicast address %d:"
675                                                "%x %x %x %x %x %x\n", i,
676                                                adapter->multicastlist[i][0],
677                                                adapter->multicastlist[i][1],
678                                                adapter->multicastlist[i][2],
679                                                adapter->multicastlist[i][3],
680                                                adapter->multicastlist[i][4],
681                                                adapter->multicastlist[i][5]);
682                                 }
683                                 /* set multicast addresses to firmware */
684                                 libertas_prepare_and_send_command(priv,
685                                                       cmd_mac_multicast_adr,
686                                                       cmd_act_set, 0, 0,
687                                                       NULL);
688                         }
689                 }
690         }
691
692         if (adapter->currentpacketfilter != oldpacketfilter) {
693                 libertas_set_mac_packet_filter(priv);
694         }
695
696         LEAVE();
697 }
698
699 /**
700  *  @brief This function hanldes the major job in WLAN driver.
701  *  it handles the event generated by firmware, rx data received
702  *  from firmware and tx data sent from kernel.
703  *
704  *  @param data    A pointer to wlan_thread structure
705  *  @return        0
706  */
707 static int wlan_service_main_thread(void *data)
708 {
709         struct wlan_thread *thread = data;
710         wlan_private *priv = thread->priv;
711         wlan_adapter *adapter = priv->adapter;
712         wait_queue_t wait;
713         u8 ireg = 0;
714
715         ENTER();
716
717         wlan_activate_thread(thread);
718
719         init_waitqueue_entry(&wait, current);
720
721         for (;;) {
722                 lbs_pr_debug(1, "main-thread 111: intcounter=%d "
723                        "currenttxskb=%p dnld_sent=%d\n",
724                        adapter->intcounter,
725                        adapter->currenttxskb, priv->wlan_dev.dnld_sent);
726
727                 add_wait_queue(&thread->waitq, &wait);
728                 set_current_state(TASK_INTERRUPTIBLE);
729                 spin_lock_irq(&adapter->driver_lock);
730                 if ((adapter->psstate == PS_STATE_SLEEP) ||
731                     (!adapter->intcounter
732                      && (priv->wlan_dev.dnld_sent || adapter->cur_cmd ||
733                          list_empty(&adapter->cmdpendingq)))) {
734                         lbs_pr_debug(1,
735                                "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
736                                adapter->connect_status, adapter->intcounter,
737                                adapter->psmode, adapter->psstate);
738                         spin_unlock_irq(&adapter->driver_lock);
739                         schedule();
740                 } else
741                         spin_unlock_irq(&adapter->driver_lock);
742
743
744                 lbs_pr_debug(1,
745                        "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
746                        "dnld_sent=%d\n", adapter->intcounter,
747                        adapter->currenttxskb, priv->wlan_dev.dnld_sent);
748
749                 set_current_state(TASK_RUNNING);
750                 remove_wait_queue(&thread->waitq, &wait);
751                 try_to_freeze();
752
753                 lbs_pr_debug(1, "main-thread 333: intcounter=%d currenttxskb=%p "
754                        "dnld_sent=%d\n",
755                        adapter->intcounter,
756                        adapter->currenttxskb, priv->wlan_dev.dnld_sent);
757
758                 if (kthread_should_stop()
759                     || adapter->surpriseremoved) {
760                         lbs_pr_debug(1,
761                                "main-thread: break from main thread: surpriseremoved=0x%x\n",
762                                adapter->surpriseremoved);
763                         break;
764                 }
765
766
767                 spin_lock_irq(&adapter->driver_lock);
768                 if (adapter->intcounter) {
769                         u8 int_status;
770                         adapter->intcounter = 0;
771                         int_status = libertas_sbi_get_int_status(priv, &ireg);
772
773                         if (int_status) {
774                                 lbs_pr_debug(1,
775                                        "main-thread: reading HOST_INT_STATUS_REG failed\n");
776                                 spin_unlock_irq(&adapter->driver_lock);
777                                 continue;
778                         }
779                         adapter->hisregcpy |= ireg;
780                 }
781
782                 lbs_pr_debug(1, "main-thread 444: intcounter=%d currenttxskb=%p "
783                        "dnld_sent=%d\n",
784                        adapter->intcounter,
785                        adapter->currenttxskb, priv->wlan_dev.dnld_sent);
786
787                 /* command response? */
788                 if (adapter->hisregcpy & his_cmdupldrdy) {
789                         lbs_pr_debug(1, "main-thread: cmd response ready.\n");
790
791                         adapter->hisregcpy &= ~his_cmdupldrdy;
792                         spin_unlock_irq(&adapter->driver_lock);
793                         libertas_process_rx_command(priv);
794                         spin_lock_irq(&adapter->driver_lock);
795                 }
796
797                 /* Any Card Event */
798                 if (adapter->hisregcpy & his_cardevent) {
799                         lbs_pr_debug(1, "main-thread: Card Event Activity.\n");
800
801                         adapter->hisregcpy &= ~his_cardevent;
802
803                         if (libertas_sbi_read_event_cause(priv)) {
804                                 lbs_pr_alert(
805                                        "main-thread: libertas_sbi_read_event_cause failed.\n");
806                                 spin_unlock_irq(&adapter->driver_lock);
807                                 continue;
808                         }
809                         spin_unlock_irq(&adapter->driver_lock);
810                         libertas_process_event(priv);
811                 } else
812                         spin_unlock_irq(&adapter->driver_lock);
813
814                 /* Check if we need to confirm Sleep Request received previously */
815                 if (adapter->psstate == PS_STATE_PRE_SLEEP) {
816                         if (!priv->wlan_dev.dnld_sent && !adapter->cur_cmd) {
817                                 if (adapter->connect_status ==
818                                     libertas_connected) {
819                                         lbs_pr_debug(1,
820                                                "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
821                                                "dnld_sent=%d cur_cmd=%p, confirm now\n",
822                                                adapter->intcounter,
823                                                adapter->currenttxskb,
824                                                priv->wlan_dev.dnld_sent,
825                                                adapter->cur_cmd);
826
827                                         libertas_ps_confirm_sleep(priv,
828                                                        (u16) adapter->psmode);
829                                 } else {
830                                         /* workaround for firmware sending
831                                          * deauth/linkloss event immediately
832                                          * after sleep request, remove this
833                                          * after firmware fixes it
834                                          */
835                                         adapter->psstate = PS_STATE_AWAKE;
836                                         lbs_pr_alert(
837                                                "main-thread: ignore PS_SleepConfirm in non-connected state\n");
838                                 }
839                         }
840                 }
841
842                 /* The PS state is changed during processing of Sleep Request
843                  * event above
844                  */
845                 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
846                     (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
847                         continue;
848
849                 /* Execute the next command */
850                 if (!priv->wlan_dev.dnld_sent && !priv->adapter->cur_cmd)
851                         libertas_execute_next_command(priv);
852
853                 /* Wake-up command waiters which can't sleep in
854                  * libertas_prepare_and_send_command
855                  */
856                 if (!adapter->nr_cmd_pending)
857                         wake_up_all(&adapter->cmd_pending);
858
859                 libertas_tx_runqueue(priv);
860         }
861
862         del_timer(&adapter->command_timer);
863         adapter->nr_cmd_pending = 0;
864         wake_up_all(&adapter->cmd_pending);
865         wlan_deactivate_thread(thread);
866
867         LEAVE();
868         return 0;
869 }
870
871 /**
872  * @brief This function adds the card. it will probe the
873  * card, allocate the wlan_priv and initialize the device.
874  *
875  *  @param card    A pointer to card
876  *  @return        A pointer to wlan_private structure
877  */
878 wlan_private *wlan_add_card(void *card)
879 {
880         struct net_device *dev = NULL;
881         struct net_device *mesh_dev = NULL;
882         wlan_private *priv = NULL;
883
884         ENTER();
885
886         /* Allocate an Ethernet device and register it */
887         if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
888                 lbs_pr_alert( "Init ethernet device failed!\n");
889                 return NULL;
890         }
891
892         priv = dev->priv;
893
894         /* allocate buffer for wlan_adapter */
895         if (!(priv->adapter = kmalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
896                 lbs_pr_alert( "Allocate buffer for wlan_adapter failed!\n");
897                 goto err_kmalloc;
898         }
899
900         /* Allocate a virtual mesh device */
901         if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
902                 lbs_pr_debug(1, "Init ethernet device failed!\n");
903                 return NULL;
904         }
905
906         /* Both intervaces share the priv structure */
907         mesh_dev->priv = priv;
908
909         /* init wlan_adapter */
910         memset(priv->adapter, 0, sizeof(wlan_adapter));
911
912         priv->wlan_dev.netdev = dev;
913         priv->wlan_dev.card = card;
914         priv->mesh_open = 0;
915         priv->infra_open = 0;
916         priv->mesh_dev = mesh_dev;
917         wlanpriv = priv;
918
919         SET_MODULE_OWNER(dev);
920         SET_MODULE_OWNER(mesh_dev);
921
922         /* Setup the OS Interface to our functions */
923         dev->open = wlan_open;
924         dev->hard_start_xmit = wlan_pre_start_xmit;
925         dev->stop = wlan_close;
926         dev->do_ioctl = libertas_do_ioctl;
927         dev->set_mac_address = wlan_set_mac_address;
928         mesh_dev->open = mesh_open;
929         mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
930         mesh_dev->stop = mesh_close;
931         mesh_dev->do_ioctl = libertas_do_ioctl;
932         memcpy(mesh_dev->dev_addr, wlanpriv->wlan_dev.netdev->dev_addr,
933                         sizeof(wlanpriv->wlan_dev.netdev->dev_addr));
934
935 #define WLAN_WATCHDOG_TIMEOUT   (5 * HZ)
936
937         dev->tx_timeout = wlan_tx_timeout;
938         dev->get_stats = wlan_get_stats;
939         dev->watchdog_timeo = WLAN_WATCHDOG_TIMEOUT;
940         dev->ethtool_ops = &libertas_ethtool_ops;
941         mesh_dev->get_stats = wlan_get_stats;
942         mesh_dev->ethtool_ops = &libertas_ethtool_ops;
943
944 #ifdef  WIRELESS_EXT
945         dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
946         mesh_dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
947 #endif
948 #define NETIF_F_DYNALLOC 16
949         dev->features |= NETIF_F_DYNALLOC;
950         dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
951         dev->set_multicast_list = wlan_set_multicast_list;
952
953         INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
954         INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
955
956         spin_lock_init(&priv->adapter->driver_lock);
957         init_waitqueue_head(&priv->adapter->cmd_pending);
958         priv->adapter->nr_cmd_pending = 0;
959
960         lbs_pr_debug(1, "Starting kthread...\n");
961         priv->mainthread.priv = priv;
962         wlan_create_thread(wlan_service_main_thread,
963                            &priv->mainthread, "wlan_main_service");
964
965         priv->assoc_thread =
966                 create_singlethread_workqueue("libertas_assoc");
967         INIT_DELAYED_WORK(&priv->assoc_work, wlan_association_worker);
968
969         /*
970          * Register the device. Fillup the private data structure with
971          * relevant information from the card and request for the required
972          * IRQ.
973          */
974         if (libertas_sbi_register_dev(priv) < 0) {
975                 lbs_pr_info("failed to register wlan device!\n");
976                 goto err_registerdev;
977         }
978
979         /* init FW and HW */
980         if (libertas_init_fw(priv)) {
981                 lbs_pr_debug(1, "Firmware Init failed\n");
982                 goto err_registerdev;
983         }
984
985         if (register_netdev(dev)) {
986                 lbs_pr_err("Cannot register network device!\n");
987                 goto err_init_fw;
988         }
989
990         /* Register virtual mesh interface */
991         if (register_netdev(mesh_dev)) {
992                 lbs_pr_info("Cannot register mesh virtual interface!\n");
993                 goto err_init_fw;
994         }
995
996         lbs_pr_info("%s: Marvell Wlan 802.11 adapter ", dev->name);
997
998         libertas_debugfs_init_one(priv, dev);
999
1000         if (libertas_found == MAX_DEVS)
1001                 goto err_init_fw;
1002         libertas_devs[libertas_found] = dev;
1003         libertas_found++;
1004 #ifdef ENABLE_PM
1005         if (!(wlan_pm_dev = pm_register(PM_UNKNOWN_DEV, 0, wlan_pm_callback)))
1006                 lbs_pr_alert( "failed to register PM callback\n");
1007 #endif
1008         if (device_create_file(&(mesh_dev->dev), &dev_attr_libertas_mpp))
1009                 goto err_create_file;
1010
1011         LEAVE();
1012         return priv;
1013
1014 err_create_file:
1015         device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
1016 err_init_fw:
1017         libertas_sbi_unregister_dev(priv);
1018 err_registerdev:
1019         destroy_workqueue(priv->assoc_thread);
1020         /* Stop the thread servicing the interrupts */
1021         wake_up_interruptible(&priv->mainthread.waitq);
1022         wlan_terminate_thread(&priv->mainthread);
1023         kfree(priv->adapter);
1024 err_kmalloc:
1025         free_netdev(dev);
1026         free_netdev(mesh_dev);
1027         wlanpriv = NULL;
1028
1029         LEAVE();
1030         return NULL;
1031 }
1032
1033 static void wake_pending_cmdnodes(wlan_private *priv)
1034 {
1035         struct cmd_ctrl_node *cmdnode;
1036         unsigned long flags;
1037
1038         spin_lock_irqsave(&priv->adapter->driver_lock, flags);
1039         list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
1040                 cmdnode->cmdwaitqwoken = 1;
1041                 wake_up_interruptible(&cmdnode->cmdwait_q);
1042         }
1043         spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
1044 }
1045
1046
1047 int wlan_remove_card(void *card)
1048 {
1049         wlan_private *priv = libertas_sbi_get_priv(card);
1050         wlan_adapter *adapter;
1051         struct net_device *dev;
1052         struct net_device *mesh_dev;
1053         union iwreq_data wrqu;
1054         int i;
1055
1056         ENTER();
1057
1058         if (!priv) {
1059                 LEAVE();
1060                 return 0;
1061         }
1062
1063         adapter = priv->adapter;
1064
1065         if (!adapter) {
1066                 LEAVE();
1067                 return 0;
1068         }
1069
1070         dev = priv->wlan_dev.netdev;
1071         mesh_dev = priv->mesh_dev;
1072
1073         netif_stop_queue(mesh_dev);
1074         netif_stop_queue(priv->wlan_dev.netdev);
1075         netif_carrier_off(priv->wlan_dev.netdev);
1076
1077         wake_pending_cmdnodes(priv);
1078
1079         device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
1080         unregister_netdev(mesh_dev);
1081         unregister_netdev(dev);
1082
1083         cancel_delayed_work(&priv->assoc_work);
1084         destroy_workqueue(priv->assoc_thread);
1085
1086         if (adapter->psmode == wlan802_11powermodemax_psp) {
1087                 adapter->psmode = wlan802_11powermodecam;
1088                 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
1089         }
1090
1091         memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1092         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1093         wireless_send_event(priv->wlan_dev.netdev, SIOCGIWAP, &wrqu, NULL);
1094
1095 #ifdef ENABLE_PM
1096         pm_unregister(wlan_pm_dev);
1097 #endif
1098
1099         adapter->surpriseremoved = 1;
1100
1101         /* Stop the thread servicing the interrupts */
1102         wlan_terminate_thread(&priv->mainthread);
1103
1104         libertas_debugfs_remove_one(priv);
1105
1106         lbs_pr_debug(1, "Free adapter\n");
1107         libertas_free_adapter(priv);
1108
1109         for (i = 0; i<libertas_found; i++) {
1110                 if (libertas_devs[i]==priv->wlan_dev.netdev) {
1111                         libertas_devs[i] = libertas_devs[--libertas_found];
1112                         libertas_devs[libertas_found] = NULL ;
1113                         break ;
1114                 }
1115         }
1116
1117         lbs_pr_debug(1, "Unregister finish\n");
1118
1119         priv->wlan_dev.netdev = NULL;
1120         priv->mesh_dev = NULL ;
1121         free_netdev(mesh_dev);
1122         free_netdev(dev);
1123         wlanpriv = NULL;
1124
1125         LEAVE();
1126         return 0;
1127 }
1128
1129 /**
1130  *  @brief This function finds the CFP in
1131  *  region_cfp_table based on region and band parameter.
1132  *
1133  *  @param region  The region code
1134  *  @param band    The band
1135  *  @param cfp_no  A pointer to CFP number
1136  *  @return        A pointer to CFP
1137  */
1138 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1139 {
1140         int i, end;
1141
1142         ENTER();
1143
1144         end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1145
1146         for (i = 0; i < end ; i++) {
1147                 lbs_pr_debug(1, "region_cfp_table[i].region=%d\n",
1148                         region_cfp_table[i].region);
1149                 if (region_cfp_table[i].region == region) {
1150                         *cfp_no = region_cfp_table[i].cfp_no_BG;
1151                         LEAVE();
1152                         return region_cfp_table[i].cfp_BG;
1153                 }
1154         }
1155
1156         LEAVE();
1157         return NULL;
1158 }
1159
1160 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1161 {
1162         wlan_adapter *adapter = priv->adapter;
1163         int i = 0;
1164
1165         struct chan_freq_power *cfp;
1166         int cfp_no;
1167
1168         ENTER();
1169
1170         memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1171
1172         {
1173                 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1174                 if (cfp != NULL) {
1175                         adapter->region_channel[i].nrcfp = cfp_no;
1176                         adapter->region_channel[i].CFP = cfp;
1177                 } else {
1178                         lbs_pr_debug(1, "wrong region code %#x in band B-G\n",
1179                                region);
1180                         return -1;
1181                 }
1182                 adapter->region_channel[i].valid = 1;
1183                 adapter->region_channel[i].region = region;
1184                 adapter->region_channel[i].band = band;
1185                 i++;
1186         }
1187         LEAVE();
1188         return 0;
1189 }
1190
1191 /**
1192  *  @brief This function handles the interrupt. it will change PS
1193  *  state if applicable. it will wake up main_thread to handle
1194  *  the interrupt event as well.
1195  *
1196  *  @param dev     A pointer to net_device structure
1197  *  @return        n/a
1198  */
1199 void libertas_interrupt(struct net_device *dev)
1200 {
1201         wlan_private *priv = dev->priv;
1202
1203         ENTER();
1204
1205         lbs_pr_debug(1, "libertas_interrupt: intcounter=%d\n",
1206                priv->adapter->intcounter);
1207
1208         priv->adapter->intcounter++;
1209
1210         if (priv->adapter->psstate == PS_STATE_SLEEP) {
1211                 priv->adapter->psstate = PS_STATE_AWAKE;
1212                 netif_wake_queue(dev);
1213         }
1214
1215         wake_up_interruptible(&priv->mainthread.waitq);
1216
1217         LEAVE();
1218 }
1219
1220 static int wlan_init_module(void)
1221 {
1222         int ret = 0;
1223
1224         ENTER();
1225
1226         if (libertas_fw_name == NULL) {
1227                 libertas_fw_name = default_fw_name;
1228         }
1229
1230         libertas_debugfs_init();
1231
1232         if (libertas_sbi_register()) {
1233                 ret = -1;
1234                 libertas_debugfs_remove();
1235                 goto done;
1236         }
1237
1238 done:
1239         LEAVE();
1240         return ret;
1241 }
1242
1243 static void wlan_cleanup_module(void)
1244 {
1245         int i;
1246
1247         ENTER();
1248
1249         for (i = 0; i<libertas_found; i++) {
1250                 wlan_private *priv = libertas_devs[i]->priv;
1251                 reset_device(priv);
1252         }
1253
1254         libertas_sbi_unregister();
1255         libertas_debugfs_remove();
1256
1257         LEAVE();
1258 }
1259
1260 module_init(wlan_init_module);
1261 module_exit(wlan_cleanup_module);
1262
1263 MODULE_DESCRIPTION("M-WLAN Driver");
1264 MODULE_AUTHOR("Marvell International Ltd.");
1265 MODULE_LICENSE("GPL");