libertas: kill struct lbs_adapter
[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 #include "join.h"
25
26 #define DRIVER_RELEASE_VERSION "323.p0"
27 const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
28 #ifdef  DEBUG
29     "-dbg"
30 #endif
31     "";
32
33
34 /* Module parameters */
35 unsigned int lbs_debug;
36 EXPORT_SYMBOL_GPL(lbs_debug);
37 module_param_named(libertas_debug, lbs_debug, int, 0644);
38
39
40 #define LBS_TX_PWR_DEFAULT              20      /*100mW */
41 #define LBS_TX_PWR_US_DEFAULT           20      /*100mW */
42 #define LBS_TX_PWR_JP_DEFAULT           16      /*50mW */
43 #define LBS_TX_PWR_FR_DEFAULT           20      /*100mW */
44 #define LBS_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, LBS_TX_PWR_US_DEFAULT},
50         {2, 2417, LBS_TX_PWR_US_DEFAULT},
51         {3, 2422, LBS_TX_PWR_US_DEFAULT},
52         {4, 2427, LBS_TX_PWR_US_DEFAULT},
53         {5, 2432, LBS_TX_PWR_US_DEFAULT},
54         {6, 2437, LBS_TX_PWR_US_DEFAULT},
55         {7, 2442, LBS_TX_PWR_US_DEFAULT},
56         {8, 2447, LBS_TX_PWR_US_DEFAULT},
57         {9, 2452, LBS_TX_PWR_US_DEFAULT},
58         {10, 2457, LBS_TX_PWR_US_DEFAULT},
59         {11, 2462, LBS_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, LBS_TX_PWR_EMEA_DEFAULT},
65         {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
66         {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
67         {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
68         {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
69         {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
70         {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
71         {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
72         {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
73         {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
74         {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
75         {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
76         {13, 2472, LBS_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, LBS_TX_PWR_DEFAULT},
82         {11, 2462, LBS_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, LBS_TX_PWR_FR_DEFAULT},
88         {11, 2462, LBS_TX_PWR_FR_DEFAULT},
89         {12, 2467, LBS_TX_PWR_FR_DEFAULT},
90         {13, 2472, LBS_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, LBS_TX_PWR_JP_DEFAULT},
96         {2, 2417, LBS_TX_PWR_JP_DEFAULT},
97         {3, 2422, LBS_TX_PWR_JP_DEFAULT},
98         {4, 2427, LBS_TX_PWR_JP_DEFAULT},
99         {5, 2432, LBS_TX_PWR_JP_DEFAULT},
100         {6, 2437, LBS_TX_PWR_JP_DEFAULT},
101         {7, 2442, LBS_TX_PWR_JP_DEFAULT},
102         {8, 2447, LBS_TX_PWR_JP_DEFAULT},
103         {9, 2452, LBS_TX_PWR_JP_DEFAULT},
104         {10, 2457, LBS_TX_PWR_JP_DEFAULT},
105         {11, 2462, LBS_TX_PWR_JP_DEFAULT},
106         {12, 2467, LBS_TX_PWR_JP_DEFAULT},
107         {13, 2472, LBS_TX_PWR_JP_DEFAULT},
108         {14, 2484, LBS_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          ARRAY_SIZE(channel_freq_power_US_BG),
127          }
128         ,
129         {0x20,                  /*CANADA IC */
130          channel_freq_power_US_BG,
131          ARRAY_SIZE(channel_freq_power_US_BG),
132          }
133         ,
134         {0x30, /*EU*/ channel_freq_power_EU_BG,
135          ARRAY_SIZE(channel_freq_power_EU_BG),
136          }
137         ,
138         {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
139          ARRAY_SIZE(channel_freq_power_SPN_BG),
140          }
141         ,
142         {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
143          ARRAY_SIZE(channel_freq_power_FR_BG),
144          }
145         ,
146         {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
147          ARRAY_SIZE(channel_freq_power_JPN_BG),
148          }
149         ,
150 /*Add new region here */
151 };
152
153 /**
154  * the table to keep region code
155  */
156 u16 lbs_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 lbs_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 lbs_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 lbs_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 lbs_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         lbs_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 lbs_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         lbs_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 lbs_add_rtap(struct lbs_private *priv);
251 void lbs_remove_rtap(struct lbs_private *priv);
252
253 /**
254  * Get function for sysfs attribute rtap
255  */
256 static ssize_t lbs_rtap_get(struct device *dev,
257                 struct device_attribute *attr, char * buf)
258 {
259         struct lbs_private *priv = to_net_dev(dev)->priv;
260         return snprintf(buf, 5, "0x%X\n", priv->monitormode);
261 }
262
263 /**
264  *  Set function for sysfs attribute rtap
265  */
266 static ssize_t lbs_rtap_set(struct device *dev,
267                 struct device_attribute *attr, const char * buf, size_t count)
268 {
269         int monitor_mode;
270         struct lbs_private *priv = to_net_dev(dev)->priv;
271
272         sscanf(buf, "%x", &monitor_mode);
273         if (monitor_mode != LBS_MONITOR_OFF) {
274                 if(priv->monitormode == monitor_mode)
275                         return strlen(buf);
276                 if (priv->monitormode == LBS_MONITOR_OFF) {
277                         if (priv->mode == IW_MODE_INFRA)
278                                 lbs_send_deauthentication(priv);
279                         else if (priv->mode == IW_MODE_ADHOC)
280                                 lbs_stop_adhoc_network(priv);
281                         lbs_add_rtap(priv);
282                 }
283                 priv->monitormode = monitor_mode;
284         }
285
286         else {
287                 if (priv->monitormode == LBS_MONITOR_OFF)
288                         return strlen(buf);
289                 priv->monitormode = LBS_MONITOR_OFF;
290                 lbs_remove_rtap(priv);
291
292                 if (priv->currenttxskb) {
293                         dev_kfree_skb_any(priv->currenttxskb);
294                         priv->currenttxskb = NULL;
295                 }
296
297                 /* Wake queues, command thread, etc. */
298                 lbs_host_to_card_done(priv);
299         }
300
301         lbs_prepare_and_send_command(priv,
302                         CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
303                         CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
304         return strlen(buf);
305 }
306
307 /**
308  * lbs_rtap attribute to be exported per mshX interface
309  * through sysfs (/sys/class/net/mshX/libertas-rtap)
310  */
311 static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get,
312                 lbs_rtap_set );
313
314 /**
315  * anycast_mask attribute to be exported per mshX interface
316  * through sysfs (/sys/class/net/mshX/anycast_mask)
317  */
318 static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
319
320 static ssize_t lbs_autostart_enabled_get(struct device *dev,
321                 struct device_attribute *attr, char * buf)
322 {
323         struct cmd_ds_mesh_access mesh_access;
324
325         memset(&mesh_access, 0, sizeof(mesh_access));
326         lbs_prepare_and_send_command(to_net_dev(dev)->priv,
327                         CMD_MESH_ACCESS,
328                         CMD_ACT_MESH_GET_AUTOSTART_ENABLED,
329                         CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
330
331         return sprintf(buf, "%d\n", le32_to_cpu(mesh_access.data[0]));
332 }
333
334 static ssize_t lbs_autostart_enabled_set(struct device *dev,
335                 struct device_attribute *attr, const char * buf, size_t count)
336 {
337         struct cmd_ds_mesh_access mesh_access;
338         uint32_t datum;
339         struct lbs_private *priv = (to_net_dev(dev))->priv;
340         int ret;
341
342         memset(&mesh_access, 0, sizeof(mesh_access));
343         sscanf(buf, "%d", &datum);
344         mesh_access.data[0] = cpu_to_le32(datum);
345
346         ret = lbs_prepare_and_send_command(priv,
347                         CMD_MESH_ACCESS,
348                         CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
349                         CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
350         if (ret == 0)
351                 priv->mesh_autostart_enabled = datum ? 1 : 0;
352
353         return strlen(buf);
354 }
355
356 static DEVICE_ATTR(autostart_enabled, 0644,
357                 lbs_autostart_enabled_get, lbs_autostart_enabled_set);
358
359 static struct attribute *lbs_mesh_sysfs_entries[] = {
360         &dev_attr_anycast_mask.attr,
361         &dev_attr_autostart_enabled.attr,
362         NULL,
363 };
364
365 static struct attribute_group lbs_mesh_attr_group = {
366         .attrs = lbs_mesh_sysfs_entries,
367 };
368
369 /**
370  *  @brief Check if the device can be open and wait if necessary.
371  *
372  *  @param dev     A pointer to net_device structure
373  *  @return        0
374  *
375  * For USB adapter, on some systems the device open handler will be
376  * called before FW ready. Use the following flag check and wait
377  * function to work around the issue.
378  *
379  */
380 static int pre_open_check(struct net_device *dev)
381 {
382         struct lbs_private *priv = (struct lbs_private *) dev->priv;
383         int i = 0;
384
385         while (!priv->fw_ready && i < 20) {
386                 i++;
387                 msleep_interruptible(100);
388         }
389         if (!priv->fw_ready) {
390                 lbs_pr_err("firmware not ready\n");
391                 return -1;
392         }
393
394         return 0;
395 }
396
397 /**
398  *  @brief This function opens the device
399  *
400  *  @param dev     A pointer to net_device structure
401  *  @return        0
402  */
403 static int lbs_dev_open(struct net_device *dev)
404 {
405         struct lbs_private *priv = (struct lbs_private *) dev->priv;
406
407         lbs_deb_enter(LBS_DEB_NET);
408
409         priv->open = 1;
410
411         if (priv->connect_status == LBS_CONNECTED)
412                 netif_carrier_on(priv->dev);
413         else
414                 netif_carrier_off(priv->dev);
415
416         if (priv->mesh_dev) {
417                 if (priv->mesh_connect_status == LBS_CONNECTED)
418                         netif_carrier_on(priv->mesh_dev);
419                 else
420                         netif_carrier_off(priv->mesh_dev);
421         }
422
423         lbs_deb_leave(LBS_DEB_NET);
424         return 0;
425 }
426 /**
427  *  @brief This function opens the mshX interface
428  *
429  *  @param dev     A pointer to net_device structure
430  *  @return        0
431  */
432 static int lbs_mesh_open(struct net_device *dev)
433 {
434         struct lbs_private *priv = (struct lbs_private *) dev->priv ;
435
436         if (pre_open_check(dev) == -1)
437                 return -1;
438         priv->mesh_open = 1 ;
439         netif_wake_queue(priv->mesh_dev);
440
441         priv->mesh_connect_status = LBS_CONNECTED;
442
443         netif_carrier_on(priv->mesh_dev);
444         netif_wake_queue(priv->mesh_dev);
445         if (priv->infra_open == 0)
446                 return lbs_dev_open(priv->dev) ;
447         return 0;
448 }
449
450 /**
451  *  @brief This function opens the ethX interface
452  *
453  *  @param dev     A pointer to net_device structure
454  *  @return        0
455  */
456 static int lbs_open(struct net_device *dev)
457 {
458         struct lbs_private *priv = (struct lbs_private *) dev->priv ;
459
460         if(pre_open_check(dev) == -1)
461                 return -1;
462         priv->infra_open = 1 ;
463         netif_wake_queue(priv->dev);
464         if (priv->open == 0)
465                 return lbs_dev_open(priv->dev) ;
466         return 0;
467 }
468
469 static int lbs_dev_close(struct net_device *dev)
470 {
471         struct lbs_private *priv = dev->priv;
472
473         lbs_deb_enter(LBS_DEB_NET);
474
475         netif_carrier_off(priv->dev);
476         priv->open = 0;
477
478         lbs_deb_leave(LBS_DEB_NET);
479         return 0;
480 }
481
482 /**
483  *  @brief This function closes the mshX interface
484  *
485  *  @param dev     A pointer to net_device structure
486  *  @return        0
487  */
488 static int lbs_mesh_close(struct net_device *dev)
489 {
490         struct lbs_private *priv = (struct lbs_private *) (dev->priv);
491
492         priv->mesh_open = 0;
493         netif_stop_queue(priv->mesh_dev);
494         if (priv->infra_open == 0)
495                 return lbs_dev_close(dev);
496         else
497                 return 0;
498 }
499
500 /**
501  *  @brief This function closes the ethX interface
502  *
503  *  @param dev     A pointer to net_device structure
504  *  @return        0
505  */
506 static int lbs_close(struct net_device *dev)
507 {
508         struct lbs_private *priv = (struct lbs_private *) dev->priv;
509
510         netif_stop_queue(dev);
511         priv->infra_open = 0;
512         if (priv->mesh_open == 0)
513                 return lbs_dev_close(dev);
514         else
515                 return 0;
516 }
517
518
519 static int lbs_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
520 {
521         int ret = 0;
522         struct lbs_private *priv = dev->priv;
523
524         lbs_deb_enter(LBS_DEB_TX);
525
526         /* We could return NETDEV_TX_BUSY here, but I'd actually 
527            like to get the point where we can BUG() */
528         if (priv->dnld_sent) {
529                 lbs_pr_err("%s while dnld_sent\n", __func__);
530                 priv->stats.tx_dropped++;
531                 goto done;
532         }
533         if (priv->currenttxskb) {
534                 lbs_pr_err("%s while TX skb pending\n", __func__);
535                 priv->stats.tx_dropped++;
536                 goto done;
537         }
538
539         netif_stop_queue(priv->dev);
540         if (priv->mesh_dev)
541                 netif_stop_queue(priv->mesh_dev);
542
543         if (lbs_process_tx(priv, skb) == 0)
544                 dev->trans_start = jiffies;
545 done:
546         lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
547         return ret;
548 }
549
550 /**
551  * @brief Mark mesh packets and handover them to lbs_hard_start_xmit
552  *
553  */
554 static int lbs_mesh_pre_start_xmit(struct sk_buff *skb,
555                 struct net_device *dev)
556 {
557         struct lbs_private *priv = dev->priv;
558         int ret;
559
560         lbs_deb_enter(LBS_DEB_MESH);
561         if (priv->monitormode != LBS_MONITOR_OFF) {
562                 netif_stop_queue(dev);
563                 return -EOPNOTSUPP;
564         }
565
566         SET_MESH_FRAME(skb);
567
568         ret = lbs_hard_start_xmit(skb, priv->mesh_dev);
569         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
570         return ret;
571 }
572
573 /**
574  * @brief Mark non-mesh packets and handover them to lbs_hard_start_xmit
575  *
576  */
577 static int lbs_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
578 {
579         struct lbs_private *priv = dev->priv;
580         int ret;
581
582         lbs_deb_enter(LBS_DEB_TX);
583
584         if (priv->monitormode != LBS_MONITOR_OFF) {
585                 netif_stop_queue(dev);
586                 return -EOPNOTSUPP;
587         }
588
589         UNSET_MESH_FRAME(skb);
590
591         ret = lbs_hard_start_xmit(skb, dev);
592         lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
593         return ret;
594 }
595
596 static void lbs_tx_timeout(struct net_device *dev)
597 {
598         struct lbs_private *priv = (struct lbs_private *) dev->priv;
599
600         lbs_deb_enter(LBS_DEB_TX);
601
602         lbs_pr_err("tx watch dog timeout\n");
603
604         priv->dnld_sent = DNLD_RES_RECEIVED;
605         dev->trans_start = jiffies;
606
607         if (priv->currenttxskb) {
608                 if (priv->monitormode != LBS_MONITOR_OFF) {
609                         /* If we are here, we have not received feedback from
610                            the previous packet.  Assume TX_FAIL and move on. */
611                         priv->eventcause = 0x01000000;
612                         lbs_send_tx_feedback(priv);
613                 } else
614                         wake_up_interruptible(&priv->waitq);
615         } else if (dev == priv->dev) {
616                 if (priv->connect_status == LBS_CONNECTED)
617                         netif_wake_queue(priv->dev);
618
619         } else if (dev == priv->mesh_dev) {
620                 if (priv->mesh_connect_status == LBS_CONNECTED)
621                         netif_wake_queue(priv->mesh_dev);
622         }
623
624         lbs_deb_leave(LBS_DEB_TX);
625 }
626
627 void lbs_host_to_card_done(struct lbs_private *priv)
628 {
629
630         priv->dnld_sent = DNLD_RES_RECEIVED;
631
632         /* Wake main thread if commands are pending */
633         if (!priv->cur_cmd)
634                 wake_up_interruptible(&priv->waitq);
635
636         /* Don't wake netif queues if we're in monitor mode and
637            a TX packet is already pending. */
638         if (priv->currenttxskb)
639                 return;
640
641         if (priv->dev && priv->connect_status == LBS_CONNECTED)
642                 netif_wake_queue(priv->dev);
643
644         if (priv->mesh_dev && priv->mesh_connect_status == LBS_CONNECTED)
645                 netif_wake_queue(priv->mesh_dev);
646 }
647 EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
648
649 /**
650  *  @brief This function returns the network statistics
651  *
652  *  @param dev     A pointer to struct lbs_private structure
653  *  @return        A pointer to net_device_stats structure
654  */
655 static struct net_device_stats *lbs_get_stats(struct net_device *dev)
656 {
657         struct lbs_private *priv = (struct lbs_private *) dev->priv;
658
659         return &priv->stats;
660 }
661
662 static int lbs_set_mac_address(struct net_device *dev, void *addr)
663 {
664         int ret = 0;
665         struct lbs_private *priv = (struct lbs_private *) dev->priv;
666         struct sockaddr *phwaddr = addr;
667
668         lbs_deb_enter(LBS_DEB_NET);
669
670         /* In case it was called from the mesh device */
671         dev = priv->dev ;
672
673         memset(priv->current_addr, 0, ETH_ALEN);
674
675         /* dev->dev_addr is 8 bytes */
676         lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
677
678         lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
679         memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
680
681         ret = lbs_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
682                                     CMD_ACT_SET,
683                                     CMD_OPTION_WAITFORRSP, 0, NULL);
684
685         if (ret) {
686                 lbs_deb_net("set MAC address failed\n");
687                 ret = -1;
688                 goto done;
689         }
690
691         lbs_deb_hex(LBS_DEB_NET, "priv->macaddr", priv->current_addr, ETH_ALEN);
692         memcpy(dev->dev_addr, priv->current_addr, ETH_ALEN);
693         if (priv->mesh_dev)
694                 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
695
696 done:
697         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
698         return ret;
699 }
700
701 static int lbs_copy_multicast_address(struct lbs_private *priv,
702                                      struct net_device *dev)
703 {
704         int i = 0;
705         struct dev_mc_list *mcptr = dev->mc_list;
706
707         for (i = 0; i < dev->mc_count; i++) {
708                 memcpy(&priv->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
709                 mcptr = mcptr->next;
710         }
711
712         return i;
713
714 }
715
716 static void lbs_set_multicast_list(struct net_device *dev)
717 {
718         struct lbs_private *priv = dev->priv;
719         int oldpacketfilter;
720         DECLARE_MAC_BUF(mac);
721
722         lbs_deb_enter(LBS_DEB_NET);
723
724         oldpacketfilter = priv->currentpacketfilter;
725
726         if (dev->flags & IFF_PROMISC) {
727                 lbs_deb_net("enable promiscuous mode\n");
728                 priv->currentpacketfilter |=
729                     CMD_ACT_MAC_PROMISCUOUS_ENABLE;
730                 priv->currentpacketfilter &=
731                     ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
732                       CMD_ACT_MAC_MULTICAST_ENABLE);
733         } else {
734                 /* Multicast */
735                 priv->currentpacketfilter &=
736                     ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
737
738                 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
739                     MRVDRV_MAX_MULTICAST_LIST_SIZE) {
740                         lbs_deb_net( "enabling all multicast\n");
741                         priv->currentpacketfilter |=
742                             CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
743                         priv->currentpacketfilter &=
744                             ~CMD_ACT_MAC_MULTICAST_ENABLE;
745                 } else {
746                         priv->currentpacketfilter &=
747                             ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
748
749                         if (!dev->mc_count) {
750                                 lbs_deb_net("no multicast addresses, "
751                                        "disabling multicast\n");
752                                 priv->currentpacketfilter &=
753                                     ~CMD_ACT_MAC_MULTICAST_ENABLE;
754                         } else {
755                                 int i;
756
757                                 priv->currentpacketfilter |=
758                                     CMD_ACT_MAC_MULTICAST_ENABLE;
759
760                                 priv->nr_of_multicastmacaddr =
761                                     lbs_copy_multicast_address(priv, dev);
762
763                                 lbs_deb_net("multicast addresses: %d\n",
764                                        dev->mc_count);
765
766                                 for (i = 0; i < dev->mc_count; i++) {
767                                         lbs_deb_net("Multicast address %d:%s\n",
768                                                i, print_mac(mac,
769                                                priv->multicastlist[i]));
770                                 }
771                                 /* send multicast addresses to firmware */
772                                 lbs_prepare_and_send_command(priv,
773                                                       CMD_MAC_MULTICAST_ADR,
774                                                       CMD_ACT_SET, 0, 0,
775                                                       NULL);
776                         }
777                 }
778         }
779
780         if (priv->currentpacketfilter != oldpacketfilter) {
781                 lbs_set_mac_packet_filter(priv);
782         }
783
784         lbs_deb_leave(LBS_DEB_NET);
785 }
786
787 /**
788  *  @brief This function handles the major jobs in the LBS driver.
789  *  It handles all events generated by firmware, RX data received
790  *  from firmware and TX data sent from kernel.
791  *
792  *  @param data    A pointer to lbs_thread structure
793  *  @return        0
794  */
795 static int lbs_thread(void *data)
796 {
797         struct net_device *dev = data;
798         struct lbs_private *priv = dev->priv;
799         wait_queue_t wait;
800         u8 ireg = 0;
801
802         lbs_deb_enter(LBS_DEB_THREAD);
803
804         init_waitqueue_entry(&wait, current);
805
806         set_freezable();
807
808         for (;;) {
809                 lbs_deb_thread( "main-thread 111: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
810                                 priv->intcounter, priv->currenttxskb, priv->dnld_sent);
811
812                 add_wait_queue(&priv->waitq, &wait);
813                 set_current_state(TASK_INTERRUPTIBLE);
814                 spin_lock_irq(&priv->driver_lock);
815
816                 if ((priv->psstate == PS_STATE_SLEEP) ||
817                     (!priv->intcounter && (priv->dnld_sent || priv->cur_cmd || list_empty(&priv->cmdpendingq)))) {
818                         lbs_deb_thread("main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
819                                        priv->connect_status, priv->intcounter,
820                                        priv->psmode, priv->psstate);
821                         spin_unlock_irq(&priv->driver_lock);
822                         schedule();
823                 } else
824                         spin_unlock_irq(&priv->driver_lock);
825
826                 lbs_deb_thread("main-thread 222 (waking up): intcounter=%d currenttxskb=%p dnld_sent=%d\n",
827                                priv->intcounter, priv->currenttxskb, priv->dnld_sent);
828
829                 set_current_state(TASK_RUNNING);
830                 remove_wait_queue(&priv->waitq, &wait);
831                 try_to_freeze();
832
833                 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
834                                priv->intcounter, priv->currenttxskb, priv->dnld_sent);
835
836                 if (kthread_should_stop() || priv->surpriseremoved) {
837                         lbs_deb_thread("main-thread: break from main thread: surpriseremoved=0x%x\n",
838                                        priv->surpriseremoved);
839                         break;
840                 }
841
842
843                 spin_lock_irq(&priv->driver_lock);
844
845                 if (priv->intcounter) {
846                         u8 int_status;
847
848                         priv->intcounter = 0;
849                         int_status = priv->hw_get_int_status(priv, &ireg);
850
851                         if (int_status) {
852                                 lbs_deb_thread("main-thread: reading HOST_INT_STATUS_REG failed\n");
853                                 spin_unlock_irq(&priv->driver_lock);
854                                 continue;
855                         }
856                         priv->hisregcpy |= ireg;
857                 }
858
859                 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
860                                priv->intcounter, priv->currenttxskb, priv->dnld_sent);
861
862                 /* command response? */
863                 if (priv->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
864                         lbs_deb_thread("main-thread: cmd response ready\n");
865
866                         priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
867                         spin_unlock_irq(&priv->driver_lock);
868                         lbs_process_rx_command(priv);
869                         spin_lock_irq(&priv->driver_lock);
870                 }
871
872                 /* Any Card Event */
873                 if (priv->hisregcpy & MRVDRV_CARDEVENT) {
874                         lbs_deb_thread("main-thread: Card Event Activity\n");
875
876                         priv->hisregcpy &= ~MRVDRV_CARDEVENT;
877
878                         if (priv->hw_read_event_cause(priv)) {
879                                 lbs_pr_alert("main-thread: hw_read_event_cause failed\n");
880                                 spin_unlock_irq(&priv->driver_lock);
881                                 continue;
882                         }
883                         spin_unlock_irq(&priv->driver_lock);
884                         lbs_process_event(priv);
885                 } else
886                         spin_unlock_irq(&priv->driver_lock);
887
888                 /* Check if we need to confirm Sleep Request received previously */
889                 if (priv->psstate == PS_STATE_PRE_SLEEP &&
890                     !priv->dnld_sent && !priv->cur_cmd) {
891                         if (priv->connect_status == LBS_CONNECTED) {
892                                 lbs_deb_thread("main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p dnld_sent=%d cur_cmd=%p, confirm now\n",
893                                                priv->intcounter, priv->currenttxskb, priv->dnld_sent, priv->cur_cmd);
894
895                                 lbs_ps_confirm_sleep(priv, (u16) priv->psmode);
896                         } else {
897                                 /* workaround for firmware sending
898                                  * deauth/linkloss event immediately
899                                  * after sleep request; remove this
900                                  * after firmware fixes it
901                                  */
902                                 priv->psstate = PS_STATE_AWAKE;
903                                 lbs_pr_alert("main-thread: ignore PS_SleepConfirm in non-connected state\n");
904                         }
905                 }
906
907                 /* The PS state is changed during processing of Sleep Request
908                  * event above
909                  */
910                 if ((priv->psstate == PS_STATE_SLEEP) ||
911                     (priv->psstate == PS_STATE_PRE_SLEEP))
912                         continue;
913
914                 /* Execute the next command */
915                 if (!priv->dnld_sent && !priv->cur_cmd)
916                         lbs_execute_next_command(priv);
917
918                 /* Wake-up command waiters which can't sleep in
919                  * lbs_prepare_and_send_command
920                  */
921                 if (!list_empty(&priv->cmdpendingq))
922                         wake_up_all(&priv->cmd_pending);
923
924                 lbs_tx_runqueue(priv);
925         }
926
927         del_timer(&priv->command_timer);
928         wake_up_all(&priv->cmd_pending);
929
930         lbs_deb_leave(LBS_DEB_THREAD);
931         return 0;
932 }
933
934 /**
935  *  @brief This function downloads firmware image, gets
936  *  HW spec from firmware and set basic parameters to
937  *  firmware.
938  *
939  *  @param priv    A pointer to struct lbs_private structure
940  *  @return        0 or -1
941  */
942 static int lbs_setup_firmware(struct lbs_private *priv)
943 {
944         int ret = -1;
945         struct cmd_ds_mesh_access mesh_access;
946
947         lbs_deb_enter(LBS_DEB_FW);
948
949         /*
950          * Read MAC address from HW
951          */
952         memset(priv->current_addr, 0xff, ETH_ALEN);
953
954         ret = lbs_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
955                                     0, CMD_OPTION_WAITFORRSP, 0, NULL);
956
957         if (ret) {
958                 ret = -1;
959                 goto done;
960         }
961
962         lbs_set_mac_packet_filter(priv);
963
964         /* Get the supported Data rates */
965         ret = lbs_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
966                                     CMD_ACT_GET_TX_RATE,
967                                     CMD_OPTION_WAITFORRSP, 0, NULL);
968
969         if (ret) {
970                 ret = -1;
971                 goto done;
972         }
973
974         /* Disable mesh autostart */
975         if (priv->mesh_dev) {
976                 memset(&mesh_access, 0, sizeof(mesh_access));
977                 mesh_access.data[0] = cpu_to_le32(0);
978                 ret = lbs_prepare_and_send_command(priv,
979                                 CMD_MESH_ACCESS,
980                                 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
981                                 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
982                 if (ret) {
983                         ret = -1;
984                         goto done;
985                 }
986                 priv->mesh_autostart_enabled = 0;
987         }
988
989         ret = 0;
990 done:
991         lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
992         return ret;
993 }
994
995 /**
996  *  This function handles the timeout of command sending.
997  *  It will re-send the same command again.
998  */
999 static void command_timer_fn(unsigned long data)
1000 {
1001         struct lbs_private *priv = (struct lbs_private *)data;
1002         struct cmd_ctrl_node *ptempnode;
1003         struct cmd_ds_command *cmd;
1004         unsigned long flags;
1005
1006         ptempnode = priv->cur_cmd;
1007         if (ptempnode == NULL) {
1008                 lbs_deb_fw("ptempnode empty\n");
1009                 return;
1010         }
1011
1012         cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
1013         if (!cmd) {
1014                 lbs_deb_fw("cmd is NULL\n");
1015                 return;
1016         }
1017
1018         lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
1019
1020         if (!priv->fw_ready)
1021                 return;
1022
1023         spin_lock_irqsave(&priv->driver_lock, flags);
1024         priv->cur_cmd = NULL;
1025         spin_unlock_irqrestore(&priv->driver_lock, flags);
1026
1027         lbs_deb_fw("re-sending same command because of timeout\n");
1028         lbs_queue_cmd(priv, ptempnode, 0);
1029
1030         wake_up_interruptible(&priv->waitq);
1031
1032         return;
1033 }
1034
1035 static int lbs_init_adapter(struct lbs_private *priv)
1036 {
1037         size_t bufsize;
1038         int i, ret = 0;
1039
1040         /* Allocate buffer to store the BSSID list */
1041         bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
1042         priv->networks = kzalloc(bufsize, GFP_KERNEL);
1043         if (!priv->networks) {
1044                 lbs_pr_err("Out of memory allocating beacons\n");
1045                 ret = -1;
1046                 goto out;
1047         }
1048
1049         /* Initialize scan result lists */
1050         INIT_LIST_HEAD(&priv->network_free_list);
1051         INIT_LIST_HEAD(&priv->network_list);
1052         for (i = 0; i < MAX_NETWORK_COUNT; i++) {
1053                 list_add_tail(&priv->networks[i].list,
1054                               &priv->network_free_list);
1055         }
1056
1057         priv->lbs_ps_confirm_sleep.seqnum = cpu_to_le16(++priv->seqnum);
1058         priv->lbs_ps_confirm_sleep.command =
1059             cpu_to_le16(CMD_802_11_PS_MODE);
1060         priv->lbs_ps_confirm_sleep.size =
1061             cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
1062         priv->lbs_ps_confirm_sleep.action =
1063             cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
1064
1065         memset(priv->current_addr, 0xff, ETH_ALEN);
1066
1067         priv->connect_status = LBS_DISCONNECTED;
1068         priv->mesh_connect_status = LBS_DISCONNECTED;
1069         priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1070         priv->mode = IW_MODE_INFRA;
1071         priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
1072         priv->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
1073         priv->radioon = RADIO_ON;
1074         priv->auto_rate = 1;
1075         priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1076         priv->psmode = LBS802_11POWERMODECAM;
1077         priv->psstate = PS_STATE_FULL_POWER;
1078
1079         mutex_init(&priv->lock);
1080
1081         memset(&priv->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
1082         priv->tx_queue_idx = 0;
1083         spin_lock_init(&priv->txqueue_lock);
1084
1085         setup_timer(&priv->command_timer, command_timer_fn,
1086                     (unsigned long)priv);
1087
1088         INIT_LIST_HEAD(&priv->cmdfreeq);
1089         INIT_LIST_HEAD(&priv->cmdpendingq);
1090
1091         spin_lock_init(&priv->driver_lock);
1092         init_waitqueue_head(&priv->cmd_pending);
1093
1094         /* Allocate the command buffers */
1095         if (lbs_allocate_cmd_buffer(priv)) {
1096                 lbs_pr_err("Out of memory allocating command buffers\n");
1097                 ret = -1;
1098         }
1099
1100 out:
1101         return ret;
1102 }
1103
1104 static void lbs_free_adapter(struct lbs_private *priv)
1105 {
1106         lbs_deb_fw("free command buffer\n");
1107         lbs_free_cmd_buffer(priv);
1108
1109         lbs_deb_fw("free command_timer\n");
1110         del_timer(&priv->command_timer);
1111
1112         lbs_deb_fw("free scan results table\n");
1113         kfree(priv->networks);
1114         priv->networks = NULL;
1115 }
1116
1117 /**
1118  * @brief This function adds the card. it will probe the
1119  * card, allocate the lbs_priv and initialize the device.
1120  *
1121  *  @param card    A pointer to card
1122  *  @return        A pointer to struct lbs_private structure
1123  */
1124 struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
1125 {
1126         struct net_device *dev = NULL;
1127         struct lbs_private *priv = NULL;
1128
1129         lbs_deb_enter(LBS_DEB_NET);
1130
1131         /* Allocate an Ethernet device and register it */
1132         dev = alloc_etherdev(sizeof(struct lbs_private));
1133         if (!dev) {
1134                 lbs_pr_err("init ethX device failed\n");
1135                 goto done;
1136         }
1137         priv = dev->priv;
1138
1139         if (lbs_init_adapter(priv)) {
1140                 lbs_pr_err("failed to initialize adapter structure.\n");
1141                 goto err_init_adapter;
1142         }
1143
1144         priv->dev = dev;
1145         priv->card = card;
1146         priv->mesh_open = 0;
1147         priv->infra_open = 0;
1148
1149         /* Setup the OS Interface to our functions */
1150         dev->open = lbs_open;
1151         dev->hard_start_xmit = lbs_pre_start_xmit;
1152         dev->stop = lbs_close;
1153         dev->set_mac_address = lbs_set_mac_address;
1154         dev->tx_timeout = lbs_tx_timeout;
1155         dev->get_stats = lbs_get_stats;
1156         dev->watchdog_timeo = 5 * HZ;
1157         dev->ethtool_ops = &lbs_ethtool_ops;
1158 #ifdef  WIRELESS_EXT
1159         dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
1160 #endif
1161         dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1162         dev->set_multicast_list = lbs_set_multicast_list;
1163
1164         SET_NETDEV_DEV(dev, dmdev);
1165
1166         priv->rtap_net_dev = NULL;
1167
1168         lbs_deb_thread("Starting main thread...\n");
1169         init_waitqueue_head(&priv->waitq);
1170         priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
1171         if (IS_ERR(priv->main_thread)) {
1172                 lbs_deb_thread("Error creating main thread.\n");
1173                 goto err_init_adapter;
1174         }
1175
1176         priv->work_thread = create_singlethread_workqueue("lbs_worker");
1177         INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
1178         INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
1179         INIT_WORK(&priv->sync_channel, lbs_sync_channel);
1180
1181         goto done;
1182
1183 err_init_adapter:
1184         lbs_free_adapter(priv);
1185         free_netdev(dev);
1186         priv = NULL;
1187
1188 done:
1189         lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
1190         return priv;
1191 }
1192 EXPORT_SYMBOL_GPL(lbs_add_card);
1193
1194
1195 int lbs_remove_card(struct lbs_private *priv)
1196 {
1197         struct net_device *dev = priv->dev;
1198         union iwreq_data wrqu;
1199
1200         lbs_deb_enter(LBS_DEB_MAIN);
1201
1202         lbs_remove_rtap(priv);
1203
1204         dev = priv->dev;
1205         device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
1206
1207         cancel_delayed_work(&priv->scan_work);
1208         cancel_delayed_work(&priv->assoc_work);
1209         destroy_workqueue(priv->work_thread);
1210
1211         if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1212                 priv->psmode = LBS802_11POWERMODECAM;
1213                 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1214         }
1215
1216         memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1217         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1218         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1219
1220         /* Stop the thread servicing the interrupts */
1221         priv->surpriseremoved = 1;
1222         kthread_stop(priv->main_thread);
1223
1224         lbs_free_adapter(priv);
1225
1226         priv->dev = NULL;
1227         free_netdev(dev);
1228
1229         lbs_deb_leave(LBS_DEB_MAIN);
1230         return 0;
1231 }
1232 EXPORT_SYMBOL_GPL(lbs_remove_card);
1233
1234
1235 int lbs_start_card(struct lbs_private *priv)
1236 {
1237         struct net_device *dev = priv->dev;
1238         int ret = -1;
1239
1240         lbs_deb_enter(LBS_DEB_MAIN);
1241
1242         /* poke the firmware */
1243         ret = lbs_setup_firmware(priv);
1244         if (ret)
1245                 goto done;
1246
1247         /* init 802.11d */
1248         lbs_init_11d(priv);
1249
1250         if (register_netdev(dev)) {
1251                 lbs_pr_err("cannot register ethX device\n");
1252                 goto done;
1253         }
1254         if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
1255                 lbs_pr_err("cannot register lbs_rtap attribute\n");
1256
1257         lbs_debugfs_init_one(priv, dev);
1258
1259         lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
1260
1261         ret = 0;
1262
1263 done:
1264         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1265         return ret;
1266 }
1267 EXPORT_SYMBOL_GPL(lbs_start_card);
1268
1269
1270 int lbs_stop_card(struct lbs_private *priv)
1271 {
1272         struct net_device *dev = priv->dev;
1273         int ret = -1;
1274         struct cmd_ctrl_node *cmdnode;
1275         unsigned long flags;
1276
1277         lbs_deb_enter(LBS_DEB_MAIN);
1278
1279         netif_stop_queue(priv->dev);
1280         netif_carrier_off(priv->dev);
1281
1282         lbs_debugfs_remove_one(priv);
1283
1284         /* Flush pending command nodes */
1285         spin_lock_irqsave(&priv->driver_lock, flags);
1286         list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
1287                 cmdnode->cmdwaitqwoken = 1;
1288                 wake_up_interruptible(&cmdnode->cmdwait_q);
1289         }
1290         spin_unlock_irqrestore(&priv->driver_lock, flags);
1291
1292         unregister_netdev(dev);
1293
1294         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1295         return ret;
1296 }
1297 EXPORT_SYMBOL_GPL(lbs_stop_card);
1298
1299
1300 /**
1301  * @brief This function adds mshX interface
1302  *
1303  *  @param priv    A pointer to the struct lbs_private structure
1304  *  @return        0 if successful, -X otherwise
1305  */
1306 int lbs_add_mesh(struct lbs_private *priv, struct device *dev)
1307 {
1308         struct net_device *mesh_dev = NULL;
1309         int ret = 0;
1310
1311         lbs_deb_enter(LBS_DEB_MESH);
1312
1313         /* Allocate a virtual mesh device */
1314         if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1315                 lbs_deb_mesh("init mshX device failed\n");
1316                 ret = -ENOMEM;
1317                 goto done;
1318         }
1319         mesh_dev->priv = priv;
1320         priv->mesh_dev = mesh_dev;
1321
1322         mesh_dev->open = lbs_mesh_open;
1323         mesh_dev->hard_start_xmit = lbs_mesh_pre_start_xmit;
1324         mesh_dev->stop = lbs_mesh_close;
1325         mesh_dev->get_stats = lbs_get_stats;
1326         mesh_dev->set_mac_address = lbs_set_mac_address;
1327         mesh_dev->ethtool_ops = &lbs_ethtool_ops;
1328         memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1329                         sizeof(priv->dev->dev_addr));
1330
1331         SET_NETDEV_DEV(priv->mesh_dev, dev);
1332
1333 #ifdef  WIRELESS_EXT
1334         mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
1335 #endif
1336         /* Register virtual mesh interface */
1337         ret = register_netdev(mesh_dev);
1338         if (ret) {
1339                 lbs_pr_err("cannot register mshX virtual interface\n");
1340                 goto err_free;
1341         }
1342
1343         ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1344         if (ret)
1345                 goto err_unregister;
1346
1347         /* Everything successful */
1348         ret = 0;
1349         goto done;
1350
1351 err_unregister:
1352         unregister_netdev(mesh_dev);
1353
1354 err_free:
1355         free_netdev(mesh_dev);
1356
1357 done:
1358         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1359         return ret;
1360 }
1361 EXPORT_SYMBOL_GPL(lbs_add_mesh);
1362
1363
1364 void lbs_remove_mesh(struct lbs_private *priv)
1365 {
1366         struct net_device *mesh_dev;
1367
1368         lbs_deb_enter(LBS_DEB_MAIN);
1369
1370         if (!priv)
1371                 goto out;
1372
1373         mesh_dev = priv->mesh_dev;
1374
1375         netif_stop_queue(mesh_dev);
1376         netif_carrier_off(priv->mesh_dev);
1377
1378         sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1379         unregister_netdev(mesh_dev);
1380
1381         priv->mesh_dev = NULL ;
1382         free_netdev(mesh_dev);
1383
1384 out:
1385         lbs_deb_leave(LBS_DEB_MAIN);
1386 }
1387 EXPORT_SYMBOL_GPL(lbs_remove_mesh);
1388
1389 /**
1390  *  @brief This function finds the CFP in
1391  *  region_cfp_table based on region and band parameter.
1392  *
1393  *  @param region  The region code
1394  *  @param band    The band
1395  *  @param cfp_no  A pointer to CFP number
1396  *  @return        A pointer to CFP
1397  */
1398 struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1399 {
1400         int i, end;
1401
1402         lbs_deb_enter(LBS_DEB_MAIN);
1403
1404         end = ARRAY_SIZE(region_cfp_table);
1405
1406         for (i = 0; i < end ; i++) {
1407                 lbs_deb_main("region_cfp_table[i].region=%d\n",
1408                         region_cfp_table[i].region);
1409                 if (region_cfp_table[i].region == region) {
1410                         *cfp_no = region_cfp_table[i].cfp_no_BG;
1411                         lbs_deb_leave(LBS_DEB_MAIN);
1412                         return region_cfp_table[i].cfp_BG;
1413                 }
1414         }
1415
1416         lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1417         return NULL;
1418 }
1419
1420 int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
1421 {
1422         int ret = 0;
1423         int i = 0;
1424
1425         struct chan_freq_power *cfp;
1426         int cfp_no;
1427
1428         lbs_deb_enter(LBS_DEB_MAIN);
1429
1430         memset(priv->region_channel, 0, sizeof(priv->region_channel));
1431
1432         {
1433                 cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
1434                 if (cfp != NULL) {
1435                         priv->region_channel[i].nrcfp = cfp_no;
1436                         priv->region_channel[i].CFP = cfp;
1437                 } else {
1438                         lbs_deb_main("wrong region code %#x in band B/G\n",
1439                                region);
1440                         ret = -1;
1441                         goto out;
1442                 }
1443                 priv->region_channel[i].valid = 1;
1444                 priv->region_channel[i].region = region;
1445                 priv->region_channel[i].band = band;
1446                 i++;
1447         }
1448 out:
1449         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1450         return ret;
1451 }
1452
1453 /**
1454  *  @brief This function handles the interrupt. it will change PS
1455  *  state if applicable. it will wake up main_thread to handle
1456  *  the interrupt event as well.
1457  *
1458  *  @param dev     A pointer to net_device structure
1459  *  @return        n/a
1460  */
1461 void lbs_interrupt(struct net_device *dev)
1462 {
1463         struct lbs_private *priv = dev->priv;
1464
1465         lbs_deb_enter(LBS_DEB_THREAD);
1466
1467         lbs_deb_thread("lbs_interrupt: intcounter=%d\n",
1468                priv->intcounter);
1469
1470         priv->intcounter++;
1471
1472         if (priv->psstate == PS_STATE_SLEEP) {
1473                 priv->psstate = PS_STATE_AWAKE;
1474                 netif_wake_queue(dev);
1475                 if (priv->mesh_dev)
1476                         netif_wake_queue(priv->mesh_dev);
1477         }
1478
1479         wake_up_interruptible(&priv->waitq);
1480
1481         lbs_deb_leave(LBS_DEB_THREAD);
1482 }
1483 EXPORT_SYMBOL_GPL(lbs_interrupt);
1484
1485 int lbs_reset_device(struct lbs_private *priv)
1486 {
1487         int ret;
1488
1489         lbs_deb_enter(LBS_DEB_MAIN);
1490         ret = lbs_prepare_and_send_command(priv, CMD_802_11_RESET,
1491                                     CMD_ACT_HALT, 0, 0, NULL);
1492         msleep_interruptible(10);
1493
1494         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1495         return ret;
1496 }
1497 EXPORT_SYMBOL_GPL(lbs_reset_device);
1498
1499 static int __init lbs_init_module(void)
1500 {
1501         lbs_deb_enter(LBS_DEB_MAIN);
1502         lbs_debugfs_init();
1503         lbs_deb_leave(LBS_DEB_MAIN);
1504         return 0;
1505 }
1506
1507 static void __exit lbs_exit_module(void)
1508 {
1509         lbs_deb_enter(LBS_DEB_MAIN);
1510
1511         lbs_debugfs_remove();
1512
1513         lbs_deb_leave(LBS_DEB_MAIN);
1514 }
1515
1516 /*
1517  * rtap interface support fuctions
1518  */
1519
1520 static int lbs_rtap_open(struct net_device *dev)
1521 {
1522         netif_carrier_off(dev);
1523         netif_stop_queue(dev);
1524         return 0;
1525 }
1526
1527 static int lbs_rtap_stop(struct net_device *dev)
1528 {
1529         return 0;
1530 }
1531
1532 static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1533 {
1534         netif_stop_queue(dev);
1535         return -EOPNOTSUPP;
1536 }
1537
1538 static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
1539 {
1540         struct lbs_private *priv = dev->priv;
1541         return &priv->ieee->stats;
1542 }
1543
1544
1545 void lbs_remove_rtap(struct lbs_private *priv)
1546 {
1547         if (priv->rtap_net_dev == NULL)
1548                 return;
1549         unregister_netdev(priv->rtap_net_dev);
1550         free_ieee80211(priv->rtap_net_dev);
1551         priv->rtap_net_dev = NULL;
1552 }
1553
1554 int lbs_add_rtap(struct lbs_private *priv)
1555 {
1556         int rc = 0;
1557
1558         if (priv->rtap_net_dev)
1559                 return -EPERM;
1560
1561         priv->rtap_net_dev = alloc_ieee80211(0);
1562         if (priv->rtap_net_dev == NULL)
1563                 return -ENOMEM;
1564
1565
1566         priv->ieee = netdev_priv(priv->rtap_net_dev);
1567
1568         strcpy(priv->rtap_net_dev->name, "rtap%d");
1569
1570         priv->rtap_net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
1571         priv->rtap_net_dev->open = lbs_rtap_open;
1572         priv->rtap_net_dev->stop = lbs_rtap_stop;
1573         priv->rtap_net_dev->get_stats = lbs_rtap_get_stats;
1574         priv->rtap_net_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
1575         priv->rtap_net_dev->set_multicast_list = lbs_set_multicast_list;
1576         priv->rtap_net_dev->priv = priv;
1577
1578         priv->ieee->iw_mode = IW_MODE_MONITOR;
1579
1580         rc = register_netdev(priv->rtap_net_dev);
1581         if (rc) {
1582                 free_ieee80211(priv->rtap_net_dev);
1583                 priv->rtap_net_dev = NULL;
1584                 return rc;
1585         }
1586
1587         return 0;
1588 }
1589
1590
1591 module_init(lbs_init_module);
1592 module_exit(lbs_exit_module);
1593
1594 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1595 MODULE_AUTHOR("Marvell International Ltd.");
1596 MODULE_LICENSE("GPL");