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