[PATCH] libertas: remove adapter->{rx,tx}antenna
[safe/jmp/linux-2.6] / 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/moduleparam.h>
8 #include <linux/delay.h>
9 #include <linux/freezer.h>
10 #include <linux/etherdevice.h>
11 #include <linux/netdevice.h>
12 #include <linux/if_arp.h>
13 #include <linux/kthread.h>
14
15 #include <net/iw_handler.h>
16 #include <net/ieee80211.h>
17
18 #include "host.h"
19 #include "decl.h"
20 #include "dev.h"
21 #include "wext.h"
22 #include "debugfs.h"
23 #include "assoc.h"
24
25 #define DRIVER_RELEASE_VERSION "322.p1"
26 const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
27 #ifdef  DEBUG
28     "-dbg"
29 #endif
30     "";
31
32
33 /* Module parameters */
34 unsigned int libertas_debug = 0;
35 module_param(libertas_debug, int, 0644);
36 EXPORT_SYMBOL_GPL(libertas_debug);
37
38
39 #define WLAN_TX_PWR_DEFAULT             20      /*100mW */
40 #define WLAN_TX_PWR_US_DEFAULT          20      /*100mW */
41 #define WLAN_TX_PWR_JP_DEFAULT          16      /*50mW */
42 #define WLAN_TX_PWR_FR_DEFAULT          20      /*100mW */
43 #define WLAN_TX_PWR_EMEA_DEFAULT        20      /*100mW */
44
45 /* Format { channel, frequency (MHz), maxtxpower } */
46 /* band: 'B/G', region: USA FCC/Canada IC */
47 static struct chan_freq_power channel_freq_power_US_BG[] = {
48         {1, 2412, WLAN_TX_PWR_US_DEFAULT},
49         {2, 2417, WLAN_TX_PWR_US_DEFAULT},
50         {3, 2422, WLAN_TX_PWR_US_DEFAULT},
51         {4, 2427, WLAN_TX_PWR_US_DEFAULT},
52         {5, 2432, WLAN_TX_PWR_US_DEFAULT},
53         {6, 2437, WLAN_TX_PWR_US_DEFAULT},
54         {7, 2442, WLAN_TX_PWR_US_DEFAULT},
55         {8, 2447, WLAN_TX_PWR_US_DEFAULT},
56         {9, 2452, WLAN_TX_PWR_US_DEFAULT},
57         {10, 2457, WLAN_TX_PWR_US_DEFAULT},
58         {11, 2462, WLAN_TX_PWR_US_DEFAULT}
59 };
60
61 /* band: 'B/G', region: Europe ETSI */
62 static struct chan_freq_power channel_freq_power_EU_BG[] = {
63         {1, 2412, WLAN_TX_PWR_EMEA_DEFAULT},
64         {2, 2417, WLAN_TX_PWR_EMEA_DEFAULT},
65         {3, 2422, WLAN_TX_PWR_EMEA_DEFAULT},
66         {4, 2427, WLAN_TX_PWR_EMEA_DEFAULT},
67         {5, 2432, WLAN_TX_PWR_EMEA_DEFAULT},
68         {6, 2437, WLAN_TX_PWR_EMEA_DEFAULT},
69         {7, 2442, WLAN_TX_PWR_EMEA_DEFAULT},
70         {8, 2447, WLAN_TX_PWR_EMEA_DEFAULT},
71         {9, 2452, WLAN_TX_PWR_EMEA_DEFAULT},
72         {10, 2457, WLAN_TX_PWR_EMEA_DEFAULT},
73         {11, 2462, WLAN_TX_PWR_EMEA_DEFAULT},
74         {12, 2467, WLAN_TX_PWR_EMEA_DEFAULT},
75         {13, 2472, WLAN_TX_PWR_EMEA_DEFAULT}
76 };
77
78 /* band: 'B/G', region: Spain */
79 static struct chan_freq_power channel_freq_power_SPN_BG[] = {
80         {10, 2457, WLAN_TX_PWR_DEFAULT},
81         {11, 2462, WLAN_TX_PWR_DEFAULT}
82 };
83
84 /* band: 'B/G', region: France */
85 static struct chan_freq_power channel_freq_power_FR_BG[] = {
86         {10, 2457, WLAN_TX_PWR_FR_DEFAULT},
87         {11, 2462, WLAN_TX_PWR_FR_DEFAULT},
88         {12, 2467, WLAN_TX_PWR_FR_DEFAULT},
89         {13, 2472, WLAN_TX_PWR_FR_DEFAULT}
90 };
91
92 /* band: 'B/G', region: Japan */
93 static struct chan_freq_power channel_freq_power_JPN_BG[] = {
94         {1, 2412, WLAN_TX_PWR_JP_DEFAULT},
95         {2, 2417, WLAN_TX_PWR_JP_DEFAULT},
96         {3, 2422, WLAN_TX_PWR_JP_DEFAULT},
97         {4, 2427, WLAN_TX_PWR_JP_DEFAULT},
98         {5, 2432, WLAN_TX_PWR_JP_DEFAULT},
99         {6, 2437, WLAN_TX_PWR_JP_DEFAULT},
100         {7, 2442, WLAN_TX_PWR_JP_DEFAULT},
101         {8, 2447, WLAN_TX_PWR_JP_DEFAULT},
102         {9, 2452, WLAN_TX_PWR_JP_DEFAULT},
103         {10, 2457, WLAN_TX_PWR_JP_DEFAULT},
104         {11, 2462, WLAN_TX_PWR_JP_DEFAULT},
105         {12, 2467, WLAN_TX_PWR_JP_DEFAULT},
106         {13, 2472, WLAN_TX_PWR_JP_DEFAULT},
107         {14, 2484, WLAN_TX_PWR_JP_DEFAULT}
108 };
109
110 /**
111  * the structure for channel, frequency and power
112  */
113 struct region_cfp_table {
114         u8 region;
115         struct chan_freq_power *cfp_BG;
116         int cfp_no_BG;
117 };
118
119 /**
120  * the structure for the mapping between region and CFP
121  */
122 static struct region_cfp_table region_cfp_table[] = {
123         {0x10,                  /*US FCC */
124          channel_freq_power_US_BG,
125          sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
126          }
127         ,
128         {0x20,                  /*CANADA IC */
129          channel_freq_power_US_BG,
130          sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
131          }
132         ,
133         {0x30, /*EU*/ channel_freq_power_EU_BG,
134          sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
135          }
136         ,
137         {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
138          sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
139          }
140         ,
141         {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
142          sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
143          }
144         ,
145         {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
146          sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
147          }
148         ,
149 /*Add new region here */
150 };
151
152 /**
153  * the table to keep region code
154  */
155 u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
156     { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
157
158 /**
159  * 802.11b/g supported bitrates (in 500Kb/s units)
160  */
161 u8 libertas_bg_rates[MAX_RATES] =
162     { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
163 0x00, 0x00 };
164
165 /**
166  * FW rate table.  FW refers to rates by their index in this table, not by the
167  * rate value itself.  Values of 0x00 are
168  * reserved positions.
169  */
170 static u8 fw_data_rates[MAX_RATES] =
171     { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
172       0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
173 };
174
175 /**
176  *  @brief use index to get the data rate
177  *
178  *  @param idx                The index of data rate
179  *  @return                     data rate or 0
180  */
181 u32 libertas_fw_index_to_data_rate(u8 idx)
182 {
183         if (idx >= sizeof(fw_data_rates))
184                 idx = 0;
185         return fw_data_rates[idx];
186 }
187
188 /**
189  *  @brief use rate to get the index
190  *
191  *  @param rate                 data rate
192  *  @return                     index or 0
193  */
194 u8 libertas_data_rate_to_fw_index(u32 rate)
195 {
196         u8 i;
197
198         if (!rate)
199                 return 0;
200
201         for (i = 0; i < sizeof(fw_data_rates); i++) {
202                 if (rate == fw_data_rates[i])
203                         return i;
204         }
205         return 0;
206 }
207
208 /**
209  * Attributes exported through sysfs
210  */
211
212 /**
213  * @brief Get function for sysfs attribute anycast_mask
214  */
215 static ssize_t libertas_anycast_get(struct device * dev,
216                 struct device_attribute *attr, char * buf)
217 {
218         struct cmd_ds_mesh_access mesh_access;
219
220         memset(&mesh_access, 0, sizeof(mesh_access));
221         libertas_prepare_and_send_command(to_net_dev(dev)->priv,
222                         CMD_MESH_ACCESS,
223                         CMD_ACT_MESH_GET_ANYCAST,
224                         CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
225
226         return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
227 }
228
229 /**
230  * @brief Set function for sysfs attribute anycast_mask
231  */
232 static ssize_t libertas_anycast_set(struct device * dev,
233                 struct device_attribute *attr, const char * buf, size_t count)
234 {
235         struct cmd_ds_mesh_access mesh_access;
236         uint32_t datum;
237
238         memset(&mesh_access, 0, sizeof(mesh_access));
239         sscanf(buf, "%x", &datum);
240         mesh_access.data[0] = cpu_to_le32(datum);
241
242         libertas_prepare_and_send_command((to_net_dev(dev))->priv,
243                         CMD_MESH_ACCESS,
244                         CMD_ACT_MESH_SET_ANYCAST,
245                         CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
246         return strlen(buf);
247 }
248
249 /**
250  * anycast_mask attribute to be exported per mshX interface
251  * through sysfs (/sys/class/net/mshX/anycast_mask)
252  */
253 static DEVICE_ATTR(anycast_mask, 0644, libertas_anycast_get, libertas_anycast_set);
254
255 static ssize_t libertas_autostart_enabled_get(struct device * dev,
256                 struct device_attribute *attr, char * buf)
257 {
258         struct cmd_ds_mesh_access mesh_access;
259
260         memset(&mesh_access, 0, sizeof(mesh_access));
261         libertas_prepare_and_send_command(to_net_dev(dev)->priv,
262                         CMD_MESH_ACCESS,
263                         CMD_ACT_MESH_GET_AUTOSTART_ENABLED,
264                         CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
265
266         return sprintf(buf, "%d\n", le32_to_cpu(mesh_access.data[0]));
267 }
268
269 static ssize_t libertas_autostart_enabled_set(struct device * dev,
270                 struct device_attribute *attr, const char * buf, size_t count)
271 {
272         struct cmd_ds_mesh_access mesh_access;
273         uint32_t datum;
274
275         memset(&mesh_access, 0, sizeof(mesh_access));
276         sscanf(buf, "%d", &datum);
277         mesh_access.data[0] = cpu_to_le32(datum);
278
279         libertas_prepare_and_send_command((to_net_dev(dev))->priv,
280                         CMD_MESH_ACCESS,
281                         CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
282                         CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
283         return strlen(buf);
284 }
285
286 static DEVICE_ATTR(autostart_enabled, 0644,
287                 libertas_autostart_enabled_get, libertas_autostart_enabled_set);
288
289 static struct attribute *libertas_mesh_sysfs_entries[] = {
290         &dev_attr_anycast_mask.attr,
291         &dev_attr_autostart_enabled.attr,
292         NULL,
293 };
294
295 static struct attribute_group libertas_mesh_attr_group = {
296         .attrs = libertas_mesh_sysfs_entries,
297 };
298
299 /**
300  *  @brief Check if the device can be open and wait if necessary.
301  *
302  *  @param dev     A pointer to net_device structure
303  *  @return        0
304  *
305  * For USB adapter, on some systems the device open handler will be
306  * called before FW ready. Use the following flag check and wait
307  * function to work around the issue.
308  *
309  */
310 static int pre_open_check(struct net_device *dev)
311 {
312         wlan_private *priv = (wlan_private *) dev->priv;
313         wlan_adapter *adapter = priv->adapter;
314         int i = 0;
315
316         while (!adapter->fw_ready && i < 20) {
317                 i++;
318                 msleep_interruptible(100);
319         }
320         if (!adapter->fw_ready) {
321                 lbs_pr_err("firmware not ready\n");
322                 return -1;
323         }
324
325         return 0;
326 }
327
328 /**
329  *  @brief This function opens the device
330  *
331  *  @param dev     A pointer to net_device structure
332  *  @return        0
333  */
334 static int libertas_dev_open(struct net_device *dev)
335 {
336         wlan_private *priv = (wlan_private *) dev->priv;
337         wlan_adapter *adapter = priv->adapter;
338
339         lbs_deb_enter(LBS_DEB_NET);
340
341         priv->open = 1;
342
343         if (adapter->connect_status == LIBERTAS_CONNECTED) {
344                 netif_carrier_on(priv->dev);
345                 if (priv->mesh_dev)
346                         netif_carrier_on(priv->mesh_dev);
347         } else {
348                 netif_carrier_off(priv->dev);
349                 if (priv->mesh_dev)
350                         netif_carrier_off(priv->mesh_dev);
351         }
352
353         lbs_deb_leave(LBS_DEB_NET);
354         return 0;
355 }
356 /**
357  *  @brief This function opens the mshX interface
358  *
359  *  @param dev     A pointer to net_device structure
360  *  @return        0
361  */
362 static int libertas_mesh_open(struct net_device *dev)
363 {
364         wlan_private *priv = (wlan_private *) dev->priv ;
365
366         if (pre_open_check(dev) == -1)
367                 return -1;
368         priv->mesh_open = 1 ;
369         netif_wake_queue(priv->mesh_dev);
370         if (priv->infra_open == 0)
371                 return libertas_dev_open(priv->dev) ;
372         return 0;
373 }
374
375 /**
376  *  @brief This function opens the ethX interface
377  *
378  *  @param dev     A pointer to net_device structure
379  *  @return        0
380  */
381 static int libertas_open(struct net_device *dev)
382 {
383         wlan_private *priv = (wlan_private *) dev->priv ;
384
385         if(pre_open_check(dev) == -1)
386                 return -1;
387         priv->infra_open = 1 ;
388         netif_wake_queue(priv->dev);
389         if (priv->open == 0)
390                 return libertas_dev_open(priv->dev) ;
391         return 0;
392 }
393
394 static int libertas_dev_close(struct net_device *dev)
395 {
396         wlan_private *priv = dev->priv;
397
398         lbs_deb_enter(LBS_DEB_NET);
399
400         netif_carrier_off(priv->dev);
401         priv->open = 0;
402
403         lbs_deb_leave(LBS_DEB_NET);
404         return 0;
405 }
406
407 /**
408  *  @brief This function closes the mshX interface
409  *
410  *  @param dev     A pointer to net_device structure
411  *  @return        0
412  */
413 static int libertas_mesh_close(struct net_device *dev)
414 {
415         wlan_private *priv = (wlan_private *) (dev->priv);
416
417         priv->mesh_open = 0;
418         netif_stop_queue(priv->mesh_dev);
419         if (priv->infra_open == 0)
420                 return libertas_dev_close(dev);
421         else
422                 return 0;
423 }
424
425 /**
426  *  @brief This function closes the ethX interface
427  *
428  *  @param dev     A pointer to net_device structure
429  *  @return        0
430  */
431 static int libertas_close(struct net_device *dev)
432 {
433         wlan_private *priv = (wlan_private *) dev->priv;
434
435         netif_stop_queue(dev);
436         priv->infra_open = 0;
437         if (priv->mesh_open == 0)
438                 return libertas_dev_close(dev);
439         else
440                 return 0;
441 }
442
443
444 static int libertas_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
445 {
446         int ret = 0;
447         wlan_private *priv = dev->priv;
448
449         lbs_deb_enter(LBS_DEB_NET);
450
451         if (priv->dnld_sent || priv->adapter->TxLockFlag) {
452                 priv->stats.tx_dropped++;
453                 goto done;
454         }
455
456         netif_stop_queue(priv->dev);
457         if (priv->mesh_dev)
458                 netif_stop_queue(priv->mesh_dev);
459
460         if (libertas_process_tx(priv, skb) == 0)
461                 dev->trans_start = jiffies;
462 done:
463         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
464         return ret;
465 }
466
467 /**
468  * @brief Mark mesh packets and handover them to libertas_hard_start_xmit
469  *
470  */
471 static int libertas_mesh_pre_start_xmit(struct sk_buff *skb,
472                 struct net_device *dev)
473 {
474         wlan_private *priv = dev->priv;
475         int ret;
476
477         lbs_deb_enter(LBS_DEB_MESH);
478
479         SET_MESH_FRAME(skb);
480
481         ret = libertas_hard_start_xmit(skb, priv->dev);
482         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
483         return ret;
484 }
485
486 /**
487  * @brief Mark non-mesh packets and handover them to libertas_hard_start_xmit
488  *
489  */
490 static int libertas_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
491 {
492         int ret;
493
494         lbs_deb_enter(LBS_DEB_NET);
495
496         UNSET_MESH_FRAME(skb);
497
498         ret = libertas_hard_start_xmit(skb, dev);
499         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
500         return ret;
501 }
502
503 static void libertas_tx_timeout(struct net_device *dev)
504 {
505         wlan_private *priv = (wlan_private *) dev->priv;
506
507         lbs_deb_enter(LBS_DEB_TX);
508
509         lbs_pr_err("tx watch dog timeout\n");
510
511         priv->dnld_sent = DNLD_RES_RECEIVED;
512         dev->trans_start = jiffies;
513
514         if (priv->adapter->currenttxskb) {
515                 if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
516                         /* If we are here, we have not received feedback from
517                            the previous packet.  Assume TX_FAIL and move on. */
518                         priv->adapter->eventcause = 0x01000000;
519                         libertas_send_tx_feedback(priv);
520                 } else
521                         wake_up_interruptible(&priv->waitq);
522         } else if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
523                 netif_wake_queue(priv->dev);
524                 if (priv->mesh_dev)
525                         netif_wake_queue(priv->mesh_dev);
526         }
527
528         lbs_deb_leave(LBS_DEB_TX);
529 }
530
531 /**
532  *  @brief This function returns the network statistics
533  *
534  *  @param dev     A pointer to wlan_private structure
535  *  @return        A pointer to net_device_stats structure
536  */
537 static struct net_device_stats *libertas_get_stats(struct net_device *dev)
538 {
539         wlan_private *priv = (wlan_private *) dev->priv;
540
541         return &priv->stats;
542 }
543
544 static int libertas_set_mac_address(struct net_device *dev, void *addr)
545 {
546         int ret = 0;
547         wlan_private *priv = (wlan_private *) dev->priv;
548         wlan_adapter *adapter = priv->adapter;
549         struct sockaddr *phwaddr = addr;
550
551         lbs_deb_enter(LBS_DEB_NET);
552
553         /* In case it was called from the mesh device */
554         dev = priv->dev ;
555
556         memset(adapter->current_addr, 0, ETH_ALEN);
557
558         /* dev->dev_addr is 8 bytes */
559         lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
560
561         lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
562         memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
563
564         ret = libertas_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
565                                     CMD_ACT_SET,
566                                     CMD_OPTION_WAITFORRSP, 0, NULL);
567
568         if (ret) {
569                 lbs_deb_net("set MAC address failed\n");
570                 ret = -1;
571                 goto done;
572         }
573
574         lbs_deb_hex(LBS_DEB_NET, "adapter->macaddr", adapter->current_addr, ETH_ALEN);
575         memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
576         if (priv->mesh_dev)
577                 memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
578
579 done:
580         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
581         return ret;
582 }
583
584 static int libertas_copy_multicast_address(wlan_adapter * adapter,
585                                      struct net_device *dev)
586 {
587         int i = 0;
588         struct dev_mc_list *mcptr = dev->mc_list;
589
590         for (i = 0; i < dev->mc_count; i++) {
591                 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
592                 mcptr = mcptr->next;
593         }
594
595         return i;
596
597 }
598
599 static void libertas_set_multicast_list(struct net_device *dev)
600 {
601         wlan_private *priv = dev->priv;
602         wlan_adapter *adapter = priv->adapter;
603         int oldpacketfilter;
604
605         lbs_deb_enter(LBS_DEB_NET);
606
607         oldpacketfilter = adapter->currentpacketfilter;
608
609         if (dev->flags & IFF_PROMISC) {
610                 lbs_deb_net("enable promiscuous mode\n");
611                 adapter->currentpacketfilter |=
612                     CMD_ACT_MAC_PROMISCUOUS_ENABLE;
613                 adapter->currentpacketfilter &=
614                     ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
615                       CMD_ACT_MAC_MULTICAST_ENABLE);
616         } else {
617                 /* Multicast */
618                 adapter->currentpacketfilter &=
619                     ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
620
621                 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
622                     MRVDRV_MAX_MULTICAST_LIST_SIZE) {
623                         lbs_deb_net( "enabling all multicast\n");
624                         adapter->currentpacketfilter |=
625                             CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
626                         adapter->currentpacketfilter &=
627                             ~CMD_ACT_MAC_MULTICAST_ENABLE;
628                 } else {
629                         adapter->currentpacketfilter &=
630                             ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
631
632                         if (!dev->mc_count) {
633                                 lbs_deb_net("no multicast addresses, "
634                                        "disabling multicast\n");
635                                 adapter->currentpacketfilter &=
636                                     ~CMD_ACT_MAC_MULTICAST_ENABLE;
637                         } else {
638                                 int i;
639
640                                 adapter->currentpacketfilter |=
641                                     CMD_ACT_MAC_MULTICAST_ENABLE;
642
643                                 adapter->nr_of_multicastmacaddr =
644                                     libertas_copy_multicast_address(adapter, dev);
645
646                                 lbs_deb_net("multicast addresses: %d\n",
647                                        dev->mc_count);
648
649                                 for (i = 0; i < dev->mc_count; i++) {
650                                         lbs_deb_net("Multicast address %d:"
651                                                MAC_FMT "\n", i,
652                                                adapter->multicastlist[i][0],
653                                                adapter->multicastlist[i][1],
654                                                adapter->multicastlist[i][2],
655                                                adapter->multicastlist[i][3],
656                                                adapter->multicastlist[i][4],
657                                                adapter->multicastlist[i][5]);
658                                 }
659                                 /* send multicast addresses to firmware */
660                                 libertas_prepare_and_send_command(priv,
661                                                       CMD_MAC_MULTICAST_ADR,
662                                                       CMD_ACT_SET, 0, 0,
663                                                       NULL);
664                         }
665                 }
666         }
667
668         if (adapter->currentpacketfilter != oldpacketfilter) {
669                 libertas_set_mac_packet_filter(priv);
670         }
671
672         lbs_deb_leave(LBS_DEB_NET);
673 }
674
675 /**
676  *  @brief This function handles the major jobs in the WLAN driver.
677  *  It handles all events generated by firmware, RX data received
678  *  from firmware and TX data sent from kernel.
679  *
680  *  @param data    A pointer to wlan_thread structure
681  *  @return        0
682  */
683 static int libertas_thread(void *data)
684 {
685         struct net_device *dev = data;
686         wlan_private *priv = dev->priv;
687         wlan_adapter *adapter = priv->adapter;
688         wait_queue_t wait;
689         u8 ireg = 0;
690
691         lbs_deb_enter(LBS_DEB_THREAD);
692
693         init_waitqueue_entry(&wait, current);
694
695         for (;;) {
696                 lbs_deb_thread( "main-thread 111: intcounter=%d "
697                        "currenttxskb=%p dnld_sent=%d\n",
698                        adapter->intcounter,
699                        adapter->currenttxskb, priv->dnld_sent);
700
701                 add_wait_queue(&priv->waitq, &wait);
702                 set_current_state(TASK_INTERRUPTIBLE);
703                 spin_lock_irq(&adapter->driver_lock);
704                 if ((adapter->psstate == PS_STATE_SLEEP) ||
705                     (!adapter->intcounter
706                      && (priv->dnld_sent || adapter->cur_cmd ||
707                          list_empty(&adapter->cmdpendingq)))) {
708                         lbs_deb_thread(
709                                "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
710                                adapter->connect_status, adapter->intcounter,
711                                adapter->psmode, adapter->psstate);
712                         spin_unlock_irq(&adapter->driver_lock);
713                         schedule();
714                 } else
715                         spin_unlock_irq(&adapter->driver_lock);
716
717
718                 lbs_deb_thread(
719                        "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
720                        "dnld_sent=%d\n", adapter->intcounter,
721                        adapter->currenttxskb, priv->dnld_sent);
722
723                 set_current_state(TASK_RUNNING);
724                 remove_wait_queue(&priv->waitq, &wait);
725                 try_to_freeze();
726
727                 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
728                        "dnld_sent=%d\n",
729                        adapter->intcounter,
730                        adapter->currenttxskb, priv->dnld_sent);
731
732                 if (kthread_should_stop()
733                     || adapter->surpriseremoved) {
734                         lbs_deb_thread(
735                                "main-thread: break from main thread: surpriseremoved=0x%x\n",
736                                adapter->surpriseremoved);
737                         break;
738                 }
739
740
741                 spin_lock_irq(&adapter->driver_lock);
742                 if (adapter->intcounter) {
743                         u8 int_status;
744                         adapter->intcounter = 0;
745                         int_status = priv->hw_get_int_status(priv, &ireg);
746
747                         if (int_status) {
748                                 lbs_deb_thread(
749                                        "main-thread: reading HOST_INT_STATUS_REG failed\n");
750                                 spin_unlock_irq(&adapter->driver_lock);
751                                 continue;
752                         }
753                         adapter->hisregcpy |= ireg;
754                 }
755
756                 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
757                        "dnld_sent=%d\n",
758                        adapter->intcounter,
759                        adapter->currenttxskb, priv->dnld_sent);
760
761                 /* command response? */
762                 if (adapter->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
763                         lbs_deb_thread("main-thread: cmd response ready\n");
764
765                         adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
766                         spin_unlock_irq(&adapter->driver_lock);
767                         libertas_process_rx_command(priv);
768                         spin_lock_irq(&adapter->driver_lock);
769                 }
770
771                 /* Any Card Event */
772                 if (adapter->hisregcpy & MRVDRV_CARDEVENT) {
773                         lbs_deb_thread("main-thread: Card Event Activity\n");
774
775                         adapter->hisregcpy &= ~MRVDRV_CARDEVENT;
776
777                         if (priv->hw_read_event_cause(priv)) {
778                                 lbs_pr_alert(
779                                        "main-thread: hw_read_event_cause failed\n");
780                                 spin_unlock_irq(&adapter->driver_lock);
781                                 continue;
782                         }
783                         spin_unlock_irq(&adapter->driver_lock);
784                         libertas_process_event(priv);
785                 } else
786                         spin_unlock_irq(&adapter->driver_lock);
787
788                 /* Check if we need to confirm Sleep Request received previously */
789                 if (adapter->psstate == PS_STATE_PRE_SLEEP) {
790                         if (!priv->dnld_sent && !adapter->cur_cmd) {
791                                 if (adapter->connect_status ==
792                                     LIBERTAS_CONNECTED) {
793                                         lbs_deb_thread(
794                                                "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
795                                                "dnld_sent=%d cur_cmd=%p, confirm now\n",
796                                                adapter->intcounter,
797                                                adapter->currenttxskb,
798                                                priv->dnld_sent,
799                                                adapter->cur_cmd);
800
801                                         libertas_ps_confirm_sleep(priv,
802                                                        (u16) adapter->psmode);
803                                 } else {
804                                         /* workaround for firmware sending
805                                          * deauth/linkloss event immediately
806                                          * after sleep request, remove this
807                                          * after firmware fixes it
808                                          */
809                                         adapter->psstate = PS_STATE_AWAKE;
810                                         lbs_pr_alert(
811                                                "main-thread: ignore PS_SleepConfirm in non-connected state\n");
812                                 }
813                         }
814                 }
815
816                 /* The PS state is changed during processing of Sleep Request
817                  * event above
818                  */
819                 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
820                     (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
821                         continue;
822
823                 /* Execute the next command */
824                 if (!priv->dnld_sent && !priv->adapter->cur_cmd)
825                         libertas_execute_next_command(priv);
826
827                 /* Wake-up command waiters which can't sleep in
828                  * libertas_prepare_and_send_command
829                  */
830                 if (!adapter->nr_cmd_pending)
831                         wake_up_all(&adapter->cmd_pending);
832
833                 libertas_tx_runqueue(priv);
834         }
835
836         del_timer(&adapter->command_timer);
837         adapter->nr_cmd_pending = 0;
838         wake_up_all(&adapter->cmd_pending);
839
840         lbs_deb_leave(LBS_DEB_THREAD);
841         return 0;
842 }
843
844 /**
845  *  @brief This function downloads firmware image, gets
846  *  HW spec from firmware and set basic parameters to
847  *  firmware.
848  *
849  *  @param priv    A pointer to wlan_private structure
850  *  @return        0 or -1
851  */
852 static int wlan_setup_station_hw(wlan_private * priv)
853 {
854         int ret = -1;
855         wlan_adapter *adapter = priv->adapter;
856
857         lbs_deb_enter(LBS_DEB_FW);
858
859         ret = priv->hw_prog_firmware(priv);
860
861         if (ret) {
862                 lbs_deb_fw("bootloader in invalid state\n");
863                 ret = -1;
864                 goto done;
865         }
866
867         /*
868          * Read MAC address from HW
869          */
870         memset(adapter->current_addr, 0xff, ETH_ALEN);
871
872         ret = libertas_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
873                                     0, CMD_OPTION_WAITFORRSP, 0, NULL);
874
875         if (ret) {
876                 ret = -1;
877                 goto done;
878         }
879
880         libertas_set_mac_packet_filter(priv);
881
882         /* Get the supported Data rates */
883         ret = libertas_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
884                                     CMD_ACT_GET_TX_RATE,
885                                     CMD_OPTION_WAITFORRSP, 0, NULL);
886
887         if (ret) {
888                 ret = -1;
889                 goto done;
890         }
891
892         ret = 0;
893 done:
894         lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
895         return ret;
896 }
897
898 static void command_timer_fn(unsigned long data);
899
900 /**
901  *  This function handles the timeout of command sending.
902  *  It will re-send the same command again.
903  */
904 static void command_timer_fn(unsigned long data)
905 {
906         wlan_private *priv = (wlan_private *)data;
907         wlan_adapter *adapter = priv->adapter;
908         struct cmd_ctrl_node *ptempnode;
909         struct cmd_ds_command *cmd;
910         unsigned long flags;
911
912         ptempnode = adapter->cur_cmd;
913         if (ptempnode == NULL) {
914                 lbs_deb_fw("ptempnode empty\n");
915                 return;
916         }
917
918         cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
919         if (!cmd) {
920                 lbs_deb_fw("cmd is NULL\n");
921                 return;
922         }
923
924         lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
925
926         if (!adapter->fw_ready)
927                 return;
928
929         spin_lock_irqsave(&adapter->driver_lock, flags);
930         adapter->cur_cmd = NULL;
931         spin_unlock_irqrestore(&adapter->driver_lock, flags);
932
933         lbs_deb_fw("re-sending same command because of timeout\n");
934         libertas_queue_cmd(adapter, ptempnode, 0);
935
936         wake_up_interruptible(&priv->waitq);
937
938         return;
939 }
940
941 static void libertas_free_adapter(wlan_private * priv)
942 {
943         wlan_adapter *adapter = priv->adapter;
944
945         if (!adapter) {
946                 lbs_deb_fw("why double free adapter?\n");
947                 return;
948         }
949
950         lbs_deb_fw("free command buffer\n");
951         libertas_free_cmd_buffer(priv);
952
953         lbs_deb_fw("free command_timer\n");
954         del_timer(&adapter->command_timer);
955
956         lbs_deb_fw("free scan results table\n");
957         kfree(adapter->networks);
958         adapter->networks = NULL;
959
960         /* Free the adapter object itself */
961         lbs_deb_fw("free adapter\n");
962         kfree(adapter);
963         priv->adapter = NULL;
964 }
965
966 static int wlan_allocate_adapter(wlan_private * priv)
967 {
968         size_t bufsize;
969         wlan_adapter *adapter = priv->adapter;
970
971         /* Allocate buffer to store the BSSID list */
972         bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
973         adapter->networks = kzalloc(bufsize, GFP_KERNEL);
974         if (!adapter->networks) {
975                 lbs_pr_err("Out of memory allocating beacons\n");
976                 libertas_free_adapter(priv);
977                 return -ENOMEM;
978         }
979
980         /* Allocate the command buffers */
981         libertas_allocate_cmd_buffer(priv);
982
983         memset(&adapter->libertas_ps_confirm_sleep, 0, sizeof(struct PS_CMD_ConfirmSleep));
984         adapter->libertas_ps_confirm_sleep.seqnum = cpu_to_le16(++adapter->seqnum);
985         adapter->libertas_ps_confirm_sleep.command =
986             cpu_to_le16(CMD_802_11_PS_MODE);
987         adapter->libertas_ps_confirm_sleep.size =
988             cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
989         adapter->libertas_ps_confirm_sleep.result = 0;
990         adapter->libertas_ps_confirm_sleep.action =
991             cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
992
993         return 0;
994 }
995
996 static void wlan_init_adapter(wlan_private * priv)
997 {
998         wlan_adapter *adapter = priv->adapter;
999         int i;
1000
1001         adapter->scanprobes = 0;
1002
1003         adapter->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
1004         adapter->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
1005
1006         /* ATIM params */
1007         adapter->atimwindow = 0;
1008
1009         adapter->connect_status = LIBERTAS_DISCONNECTED;
1010         memset(adapter->current_addr, 0xff, ETH_ALEN);
1011
1012         /* scan type */
1013         adapter->scantype = CMD_SCAN_TYPE_ACTIVE;
1014
1015         /* scan mode */
1016         adapter->scanmode = CMD_BSS_TYPE_ANY;
1017
1018         /* 802.11 specific */
1019         adapter->secinfo.wep_enabled = 0;
1020         for (i = 0; i < sizeof(adapter->wep_keys) / sizeof(adapter->wep_keys[0]);
1021              i++)
1022                 memset(&adapter->wep_keys[i], 0, sizeof(struct enc_key));
1023         adapter->wep_tx_keyidx = 0;
1024         adapter->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1025         adapter->mode = IW_MODE_INFRA;
1026
1027         adapter->pending_assoc_req = NULL;
1028         adapter->in_progress_assoc_req = NULL;
1029
1030         /* Initialize scan result lists */
1031         INIT_LIST_HEAD(&adapter->network_free_list);
1032         INIT_LIST_HEAD(&adapter->network_list);
1033         for (i = 0; i < MAX_NETWORK_COUNT; i++) {
1034                 list_add_tail(&adapter->networks[i].list,
1035                               &adapter->network_free_list);
1036         }
1037
1038         mutex_init(&adapter->lock);
1039
1040         adapter->prescan = 1;
1041
1042         memset(&adapter->curbssparams, 0, sizeof(adapter->curbssparams));
1043         adapter->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
1044
1045         /* PnP and power profile */
1046         adapter->surpriseremoved = 0;
1047
1048         adapter->currentpacketfilter =
1049             CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
1050
1051         adapter->radioon = RADIO_ON;
1052
1053         adapter->auto_rate = 1;
1054         adapter->cur_rate = 0;
1055         adapter->adhoc_grate_enabled = 0;
1056
1057         adapter->beaconperiod = MRVDRV_BEACON_INTERVAL;
1058
1059         // set default capabilities
1060         adapter->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1061
1062         adapter->psmode = WLAN802_11POWERMODECAM;
1063         adapter->multipledtim = MRVDRV_DEFAULT_MULTIPLE_DTIM;
1064
1065         adapter->listeninterval = MRVDRV_DEFAULT_LISTEN_INTERVAL;
1066
1067         adapter->psstate = PS_STATE_FULL_POWER;
1068         adapter->needtowakeup = 0;
1069         adapter->locallisteninterval = 0;       /* default value in firmware will be used */
1070
1071         adapter->intcounter = 0;
1072
1073         adapter->currenttxskb = NULL;
1074         adapter->pkttxctrl = 0;
1075
1076         memset(&adapter->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
1077         adapter->tx_queue_idx = 0;
1078         spin_lock_init(&adapter->txqueue_lock);
1079
1080         return;
1081 }
1082
1083 static int libertas_init_fw(wlan_private * priv)
1084 {
1085         int ret = -1;
1086         wlan_adapter *adapter = priv->adapter;
1087
1088         lbs_deb_enter(LBS_DEB_FW);
1089
1090         /* Allocate adapter structure */
1091         if ((ret = wlan_allocate_adapter(priv)) != 0)
1092                 goto done;
1093
1094         /* init adapter structure */
1095         wlan_init_adapter(priv);
1096
1097         /* init timer etc. */
1098         setup_timer(&adapter->command_timer, command_timer_fn,
1099                         (unsigned long)priv);
1100
1101         /* download fimrware etc. */
1102         if ((ret = wlan_setup_station_hw(priv)) != 0) {
1103                 del_timer_sync(&adapter->command_timer);
1104                 goto done;
1105         }
1106
1107         /* init 802.11d */
1108         libertas_init_11d(priv);
1109
1110         ret = 0;
1111 done:
1112         lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
1113         return ret;
1114 }
1115
1116 /**
1117  * @brief This function adds the card. it will probe the
1118  * card, allocate the wlan_priv and initialize the device.
1119  *
1120  *  @param card    A pointer to card
1121  *  @return        A pointer to wlan_private structure
1122  */
1123 wlan_private *libertas_add_card(void *card, struct device *dmdev)
1124 {
1125         struct net_device *dev = NULL;
1126         wlan_private *priv = NULL;
1127
1128         lbs_deb_enter(LBS_DEB_NET);
1129
1130         /* Allocate an Ethernet device and register it */
1131         if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
1132                 lbs_pr_err("init ethX device failed\n");
1133                 return NULL;
1134         }
1135         priv = dev->priv;
1136
1137         /* allocate buffer for wlan_adapter */
1138         if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
1139                 lbs_pr_err("allocate buffer for wlan_adapter failed\n");
1140                 goto err_kzalloc;
1141         }
1142
1143         priv->dev = dev;
1144         priv->card = card;
1145         priv->mesh_open = 0;
1146         priv->infra_open = 0;
1147
1148         SET_MODULE_OWNER(dev);
1149
1150         /* Setup the OS Interface to our functions */
1151         dev->open = libertas_open;
1152         dev->hard_start_xmit = libertas_pre_start_xmit;
1153         dev->stop = libertas_close;
1154         dev->set_mac_address = libertas_set_mac_address;
1155         dev->tx_timeout = libertas_tx_timeout;
1156         dev->get_stats = libertas_get_stats;
1157         dev->watchdog_timeo = 5 * HZ;
1158         dev->ethtool_ops = &libertas_ethtool_ops;
1159 #ifdef  WIRELESS_EXT
1160         dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
1161 #endif
1162 #define NETIF_F_DYNALLOC 16
1163         dev->features |= NETIF_F_DYNALLOC;
1164         dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1165         dev->set_multicast_list = libertas_set_multicast_list;
1166
1167         SET_NETDEV_DEV(dev, dmdev);
1168
1169         INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
1170         INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
1171
1172         spin_lock_init(&priv->adapter->driver_lock);
1173         init_waitqueue_head(&priv->adapter->cmd_pending);
1174         priv->adapter->nr_cmd_pending = 0;
1175         goto done;
1176
1177 err_kzalloc:
1178         free_netdev(dev);
1179         priv = NULL;
1180 done:
1181         lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
1182         return priv;
1183 }
1184 EXPORT_SYMBOL_GPL(libertas_add_card);
1185
1186 int libertas_activate_card(wlan_private *priv)
1187 {
1188         struct net_device *dev = priv->dev;
1189         int ret = -1;
1190
1191         lbs_deb_enter(LBS_DEB_MAIN);
1192
1193         lbs_deb_thread("Starting main thread...\n");
1194         init_waitqueue_head(&priv->waitq);
1195         priv->main_thread = kthread_run(libertas_thread, dev, "libertas_main");
1196         if (IS_ERR(priv->main_thread)) {
1197                 lbs_deb_thread("Error creating main thread.\n");
1198                 goto done;
1199         }
1200
1201         priv->assoc_thread =
1202                 create_singlethread_workqueue("libertas_assoc");
1203         INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
1204         INIT_WORK(&priv->sync_channel, libertas_sync_channel);
1205
1206         /*
1207          * Register the device. Fillup the private data structure with
1208          * relevant information from the card and request for the required
1209          * IRQ.
1210          */
1211         if (priv->hw_register_dev(priv) < 0) {
1212                 lbs_pr_err("failed to register WLAN device\n");
1213                 goto err_registerdev;
1214         }
1215
1216         /* init FW and HW */
1217         if (libertas_init_fw(priv)) {
1218                 lbs_pr_err("firmware init failed\n");
1219                 goto err_registerdev;
1220         }
1221
1222         if (register_netdev(dev)) {
1223                 lbs_pr_err("cannot register ethX device\n");
1224                 goto err_init_fw;
1225         }
1226
1227         lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
1228
1229         libertas_debugfs_init_one(priv, dev);
1230
1231         ret = 0;
1232         goto done;
1233
1234 err_init_fw:
1235         priv->hw_unregister_dev(priv);
1236 err_registerdev:
1237         destroy_workqueue(priv->assoc_thread);
1238         /* Stop the thread servicing the interrupts */
1239         wake_up_interruptible(&priv->waitq);
1240         kthread_stop(priv->main_thread);
1241 done:
1242         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
1243         return ret;
1244 }
1245 EXPORT_SYMBOL_GPL(libertas_activate_card);
1246
1247
1248 /**
1249  * @brief This function adds mshX interface
1250  *
1251  *  @param priv    A pointer to the wlan_private structure
1252  *  @return        0 if successful, -X otherwise
1253  */
1254 int libertas_add_mesh(wlan_private *priv, struct device *dev)
1255 {
1256         struct net_device *mesh_dev = NULL;
1257         int ret = 0;
1258
1259         lbs_deb_enter(LBS_DEB_MESH);
1260
1261         /* Allocate a virtual mesh device */
1262         if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1263                 lbs_deb_mesh("init mshX device failed\n");
1264                 ret = -ENOMEM;
1265                 goto done;
1266         }
1267         mesh_dev->priv = priv;
1268         priv->mesh_dev = mesh_dev;
1269
1270         SET_MODULE_OWNER(mesh_dev);
1271
1272         mesh_dev->open = libertas_mesh_open;
1273         mesh_dev->hard_start_xmit = libertas_mesh_pre_start_xmit;
1274         mesh_dev->stop = libertas_mesh_close;
1275         mesh_dev->get_stats = libertas_get_stats;
1276         mesh_dev->set_mac_address = libertas_set_mac_address;
1277         mesh_dev->ethtool_ops = &libertas_ethtool_ops;
1278         memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1279                         sizeof(priv->dev->dev_addr));
1280
1281         SET_NETDEV_DEV(priv->mesh_dev, dev);
1282
1283 #ifdef  WIRELESS_EXT
1284         mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
1285 #endif
1286 #define NETIF_F_DYNALLOC 16
1287
1288         /* Register virtual mesh interface */
1289         ret = register_netdev(mesh_dev);
1290         if (ret) {
1291                 lbs_pr_err("cannot register mshX virtual interface\n");
1292                 goto err_free;
1293         }
1294
1295         ret = sysfs_create_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
1296         if (ret)
1297                 goto err_unregister;
1298
1299         /* Everything successful */
1300         ret = 0;
1301         goto done;
1302
1303 err_unregister:
1304         unregister_netdev(mesh_dev);
1305
1306 err_free:
1307         free_netdev(mesh_dev);
1308
1309 done:
1310         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1311         return ret;
1312 }
1313 EXPORT_SYMBOL_GPL(libertas_add_mesh);
1314
1315 static void wake_pending_cmdnodes(wlan_private *priv)
1316 {
1317         struct cmd_ctrl_node *cmdnode;
1318         unsigned long flags;
1319
1320         lbs_deb_enter(LBS_DEB_HOST);
1321
1322         spin_lock_irqsave(&priv->adapter->driver_lock, flags);
1323         list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
1324                 cmdnode->cmdwaitqwoken = 1;
1325                 wake_up_interruptible(&cmdnode->cmdwait_q);
1326         }
1327         spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
1328 }
1329
1330
1331 int libertas_remove_card(wlan_private *priv)
1332 {
1333         wlan_adapter *adapter;
1334         struct net_device *dev;
1335         union iwreq_data wrqu;
1336
1337         lbs_deb_enter(LBS_DEB_NET);
1338
1339         if (!priv)
1340                 goto out;
1341
1342         adapter = priv->adapter;
1343
1344         if (!adapter)
1345                 goto out;
1346
1347         dev = priv->dev;
1348
1349         netif_stop_queue(priv->dev);
1350         netif_carrier_off(priv->dev);
1351
1352         wake_pending_cmdnodes(priv);
1353
1354         unregister_netdev(dev);
1355
1356         cancel_delayed_work(&priv->assoc_work);
1357         destroy_workqueue(priv->assoc_thread);
1358
1359         if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
1360                 adapter->psmode = WLAN802_11POWERMODECAM;
1361                 libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1362         }
1363
1364         memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1365         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1366         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1367
1368         adapter->surpriseremoved = 1;
1369
1370         /* Stop the thread servicing the interrupts */
1371         kthread_stop(priv->main_thread);
1372
1373         libertas_debugfs_remove_one(priv);
1374
1375         lbs_deb_net("free adapter\n");
1376         libertas_free_adapter(priv);
1377
1378         lbs_deb_net("unregister finish\n");
1379
1380         priv->dev = NULL;
1381         free_netdev(dev);
1382
1383 out:
1384         lbs_deb_leave(LBS_DEB_NET);
1385         return 0;
1386 }
1387 EXPORT_SYMBOL_GPL(libertas_remove_card);
1388
1389
1390 void libertas_remove_mesh(wlan_private *priv)
1391 {
1392         struct net_device *mesh_dev;
1393
1394         lbs_deb_enter(LBS_DEB_NET);
1395
1396         if (!priv)
1397                 goto out;
1398
1399         mesh_dev = priv->mesh_dev;
1400
1401         netif_stop_queue(mesh_dev);
1402         netif_carrier_off(priv->mesh_dev);
1403
1404         sysfs_remove_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
1405         unregister_netdev(mesh_dev);
1406
1407         priv->mesh_dev = NULL ;
1408         free_netdev(mesh_dev);
1409
1410 out:
1411         lbs_deb_leave(LBS_DEB_NET);
1412 }
1413 EXPORT_SYMBOL_GPL(libertas_remove_mesh);
1414
1415 /**
1416  *  @brief This function finds the CFP in
1417  *  region_cfp_table based on region and band parameter.
1418  *
1419  *  @param region  The region code
1420  *  @param band    The band
1421  *  @param cfp_no  A pointer to CFP number
1422  *  @return        A pointer to CFP
1423  */
1424 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1425 {
1426         int i, end;
1427
1428         lbs_deb_enter(LBS_DEB_MAIN);
1429
1430         end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1431
1432         for (i = 0; i < end ; i++) {
1433                 lbs_deb_main("region_cfp_table[i].region=%d\n",
1434                         region_cfp_table[i].region);
1435                 if (region_cfp_table[i].region == region) {
1436                         *cfp_no = region_cfp_table[i].cfp_no_BG;
1437                         lbs_deb_leave(LBS_DEB_MAIN);
1438                         return region_cfp_table[i].cfp_BG;
1439                 }
1440         }
1441
1442         lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1443         return NULL;
1444 }
1445
1446 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1447 {
1448         wlan_adapter *adapter = priv->adapter;
1449         int ret = 0;
1450         int i = 0;
1451
1452         struct chan_freq_power *cfp;
1453         int cfp_no;
1454
1455         lbs_deb_enter(LBS_DEB_MAIN);
1456
1457         memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1458
1459         {
1460                 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1461                 if (cfp != NULL) {
1462                         adapter->region_channel[i].nrcfp = cfp_no;
1463                         adapter->region_channel[i].CFP = cfp;
1464                 } else {
1465                         lbs_deb_main("wrong region code %#x in band B/G\n",
1466                                region);
1467                         ret = -1;
1468                         goto out;
1469                 }
1470                 adapter->region_channel[i].valid = 1;
1471                 adapter->region_channel[i].region = region;
1472                 adapter->region_channel[i].band = band;
1473                 i++;
1474         }
1475 out:
1476         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1477         return ret;
1478 }
1479
1480 /**
1481  *  @brief This function handles the interrupt. it will change PS
1482  *  state if applicable. it will wake up main_thread to handle
1483  *  the interrupt event as well.
1484  *
1485  *  @param dev     A pointer to net_device structure
1486  *  @return        n/a
1487  */
1488 void libertas_interrupt(struct net_device *dev)
1489 {
1490         wlan_private *priv = dev->priv;
1491
1492         lbs_deb_enter(LBS_DEB_THREAD);
1493
1494         lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
1495                priv->adapter->intcounter);
1496
1497         priv->adapter->intcounter++;
1498
1499         if (priv->adapter->psstate == PS_STATE_SLEEP) {
1500                 priv->adapter->psstate = PS_STATE_AWAKE;
1501                 netif_wake_queue(dev);
1502                 if (priv->mesh_dev)
1503                         netif_wake_queue(priv->mesh_dev);
1504         }
1505
1506         wake_up_interruptible(&priv->waitq);
1507
1508         lbs_deb_leave(LBS_DEB_THREAD);
1509 }
1510 EXPORT_SYMBOL_GPL(libertas_interrupt);
1511
1512 int libertas_reset_device(wlan_private *priv)
1513 {
1514         int ret;
1515
1516         lbs_deb_enter(LBS_DEB_MAIN);
1517         ret = libertas_prepare_and_send_command(priv, CMD_802_11_RESET,
1518                                     CMD_ACT_HALT, 0, 0, NULL);
1519         msleep_interruptible(10);
1520
1521         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1522         return ret;
1523 }
1524 EXPORT_SYMBOL_GPL(libertas_reset_device);
1525
1526 static int libertas_init_module(void)
1527 {
1528         lbs_deb_enter(LBS_DEB_MAIN);
1529         libertas_debugfs_init();
1530         lbs_deb_leave(LBS_DEB_MAIN);
1531         return 0;
1532 }
1533
1534 static void libertas_exit_module(void)
1535 {
1536         lbs_deb_enter(LBS_DEB_MAIN);
1537
1538         libertas_debugfs_remove();
1539
1540         lbs_deb_leave(LBS_DEB_MAIN);
1541 }
1542
1543 module_init(libertas_init_module);
1544 module_exit(libertas_exit_module);
1545
1546 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1547 MODULE_AUTHOR("Marvell International Ltd.");
1548 MODULE_LICENSE("GPL");