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