libertas: TX packet is radiotap iff it comes from rtap_dev
[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 static void lbs_tx_timeout(struct net_device *dev)
519 {
520         struct lbs_private *priv = (struct lbs_private *) dev->priv;
521
522         lbs_deb_enter(LBS_DEB_TX);
523
524         lbs_pr_err("tx watch dog timeout\n");
525
526         dev->trans_start = jiffies;
527
528         if (priv->currenttxskb) {
529                 priv->eventcause = 0x01000000;
530                 lbs_send_tx_feedback(priv);
531         }
532         /* XX: Shouldn't we also call into the hw-specific driver
533            to kick it somehow? */
534         lbs_host_to_card_done(priv);
535
536         lbs_deb_leave(LBS_DEB_TX);
537 }
538
539 void lbs_host_to_card_done(struct lbs_private *priv)
540 {
541         unsigned long flags;
542
543         spin_lock_irqsave(&priv->driver_lock, flags);
544
545         priv->dnld_sent = DNLD_RES_RECEIVED;
546
547         /* Wake main thread if commands are pending */
548         if (!priv->cur_cmd)
549                 wake_up_interruptible(&priv->waitq);
550
551         /* Don't wake netif queues if we're in monitor mode and
552            a TX packet is already pending, or if there are commands
553            queued to be sent. */
554         if (!priv->currenttxskb && list_empty(&priv->cmdpendingq)) {
555                 if (priv->dev && priv->connect_status == LBS_CONNECTED)
556                         netif_wake_queue(priv->dev);
557
558                 if (priv->mesh_dev && priv->mesh_connect_status == LBS_CONNECTED)
559                         netif_wake_queue(priv->mesh_dev);
560         }
561         spin_unlock_irqrestore(&priv->driver_lock, flags);
562 }
563 EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
564
565 /**
566  *  @brief This function returns the network statistics
567  *
568  *  @param dev     A pointer to struct lbs_private structure
569  *  @return        A pointer to net_device_stats structure
570  */
571 static struct net_device_stats *lbs_get_stats(struct net_device *dev)
572 {
573         struct lbs_private *priv = (struct lbs_private *) dev->priv;
574
575         return &priv->stats;
576 }
577
578 static int lbs_set_mac_address(struct net_device *dev, void *addr)
579 {
580         int ret = 0;
581         struct lbs_private *priv = (struct lbs_private *) dev->priv;
582         struct sockaddr *phwaddr = addr;
583
584         lbs_deb_enter(LBS_DEB_NET);
585
586         /* In case it was called from the mesh device */
587         dev = priv->dev ;
588
589         memset(priv->current_addr, 0, ETH_ALEN);
590
591         /* dev->dev_addr is 8 bytes */
592         lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
593
594         lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
595         memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
596
597         ret = lbs_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
598                                     CMD_ACT_SET,
599                                     CMD_OPTION_WAITFORRSP, 0, NULL);
600
601         if (ret) {
602                 lbs_deb_net("set MAC address failed\n");
603                 ret = -1;
604                 goto done;
605         }
606
607         lbs_deb_hex(LBS_DEB_NET, "priv->macaddr", priv->current_addr, ETH_ALEN);
608         memcpy(dev->dev_addr, priv->current_addr, ETH_ALEN);
609         if (priv->mesh_dev)
610                 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
611
612 done:
613         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
614         return ret;
615 }
616
617 static int lbs_copy_multicast_address(struct lbs_private *priv,
618                                      struct net_device *dev)
619 {
620         int i = 0;
621         struct dev_mc_list *mcptr = dev->mc_list;
622
623         for (i = 0; i < dev->mc_count; i++) {
624                 memcpy(&priv->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
625                 mcptr = mcptr->next;
626         }
627
628         return i;
629
630 }
631
632 static void lbs_set_multicast_list(struct net_device *dev)
633 {
634         struct lbs_private *priv = dev->priv;
635         int oldpacketfilter;
636         DECLARE_MAC_BUF(mac);
637
638         lbs_deb_enter(LBS_DEB_NET);
639
640         oldpacketfilter = priv->currentpacketfilter;
641
642         if (dev->flags & IFF_PROMISC) {
643                 lbs_deb_net("enable promiscuous mode\n");
644                 priv->currentpacketfilter |=
645                     CMD_ACT_MAC_PROMISCUOUS_ENABLE;
646                 priv->currentpacketfilter &=
647                     ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
648                       CMD_ACT_MAC_MULTICAST_ENABLE);
649         } else {
650                 /* Multicast */
651                 priv->currentpacketfilter &=
652                     ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
653
654                 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
655                     MRVDRV_MAX_MULTICAST_LIST_SIZE) {
656                         lbs_deb_net( "enabling all multicast\n");
657                         priv->currentpacketfilter |=
658                             CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
659                         priv->currentpacketfilter &=
660                             ~CMD_ACT_MAC_MULTICAST_ENABLE;
661                 } else {
662                         priv->currentpacketfilter &=
663                             ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
664
665                         if (!dev->mc_count) {
666                                 lbs_deb_net("no multicast addresses, "
667                                        "disabling multicast\n");
668                                 priv->currentpacketfilter &=
669                                     ~CMD_ACT_MAC_MULTICAST_ENABLE;
670                         } else {
671                                 int i;
672
673                                 priv->currentpacketfilter |=
674                                     CMD_ACT_MAC_MULTICAST_ENABLE;
675
676                                 priv->nr_of_multicastmacaddr =
677                                     lbs_copy_multicast_address(priv, dev);
678
679                                 lbs_deb_net("multicast addresses: %d\n",
680                                        dev->mc_count);
681
682                                 for (i = 0; i < dev->mc_count; i++) {
683                                         lbs_deb_net("Multicast address %d:%s\n",
684                                                i, print_mac(mac,
685                                                priv->multicastlist[i]));
686                                 }
687                                 /* send multicast addresses to firmware */
688                                 lbs_prepare_and_send_command(priv,
689                                                       CMD_MAC_MULTICAST_ADR,
690                                                       CMD_ACT_SET, 0, 0,
691                                                       NULL);
692                         }
693                 }
694         }
695
696         if (priv->currentpacketfilter != oldpacketfilter) {
697                 lbs_set_mac_packet_filter(priv);
698         }
699
700         lbs_deb_leave(LBS_DEB_NET);
701 }
702
703 /**
704  *  @brief This function handles the major jobs in the LBS driver.
705  *  It handles all events generated by firmware, RX data received
706  *  from firmware and TX data sent from kernel.
707  *
708  *  @param data    A pointer to lbs_thread structure
709  *  @return        0
710  */
711 static int lbs_thread(void *data)
712 {
713         struct net_device *dev = data;
714         struct lbs_private *priv = dev->priv;
715         wait_queue_t wait;
716         u8 ireg = 0;
717
718         lbs_deb_enter(LBS_DEB_THREAD);
719
720         init_waitqueue_entry(&wait, current);
721
722         set_freezable();
723
724         for (;;) {
725                 lbs_deb_thread( "main-thread 111: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
726                                 priv->intcounter, priv->currenttxskb, priv->dnld_sent);
727
728                 add_wait_queue(&priv->waitq, &wait);
729                 set_current_state(TASK_INTERRUPTIBLE);
730                 spin_lock_irq(&priv->driver_lock);
731
732                 if ((priv->psstate == PS_STATE_SLEEP) ||
733                     (!priv->intcounter && (priv->dnld_sent || priv->cur_cmd || list_empty(&priv->cmdpendingq)))) {
734                         lbs_deb_thread("main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
735                                        priv->connect_status, priv->intcounter,
736                                        priv->psmode, priv->psstate);
737                         spin_unlock_irq(&priv->driver_lock);
738                         schedule();
739                 } else
740                         spin_unlock_irq(&priv->driver_lock);
741
742                 lbs_deb_thread("main-thread 222 (waking up): intcounter=%d currenttxskb=%p dnld_sent=%d\n",
743                                priv->intcounter, priv->currenttxskb, priv->dnld_sent);
744
745                 set_current_state(TASK_RUNNING);
746                 remove_wait_queue(&priv->waitq, &wait);
747                 try_to_freeze();
748
749                 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
750                                priv->intcounter, priv->currenttxskb, priv->dnld_sent);
751
752                 if (kthread_should_stop() || priv->surpriseremoved) {
753                         lbs_deb_thread("main-thread: break from main thread: surpriseremoved=0x%x\n",
754                                        priv->surpriseremoved);
755                         break;
756                 }
757
758
759                 spin_lock_irq(&priv->driver_lock);
760
761                 if (priv->intcounter) {
762                         u8 int_status;
763
764                         priv->intcounter = 0;
765                         int_status = priv->hw_get_int_status(priv, &ireg);
766
767                         if (int_status) {
768                                 lbs_deb_thread("main-thread: reading HOST_INT_STATUS_REG failed\n");
769                                 spin_unlock_irq(&priv->driver_lock);
770                                 continue;
771                         }
772                         priv->hisregcpy |= ireg;
773                 }
774
775                 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
776                                priv->intcounter, priv->currenttxskb, priv->dnld_sent);
777
778                 /* command response? */
779                 if (priv->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
780                         lbs_deb_thread("main-thread: cmd response ready\n");
781
782                         priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
783                         spin_unlock_irq(&priv->driver_lock);
784                         lbs_process_rx_command(priv);
785                         spin_lock_irq(&priv->driver_lock);
786                 }
787
788                 /* Any Card Event */
789                 if (priv->hisregcpy & MRVDRV_CARDEVENT) {
790                         lbs_deb_thread("main-thread: Card Event Activity\n");
791
792                         priv->hisregcpy &= ~MRVDRV_CARDEVENT;
793
794                         if (priv->hw_read_event_cause(priv)) {
795                                 lbs_pr_alert("main-thread: hw_read_event_cause failed\n");
796                                 spin_unlock_irq(&priv->driver_lock);
797                                 continue;
798                         }
799                         spin_unlock_irq(&priv->driver_lock);
800                         lbs_process_event(priv);
801                 } else
802                         spin_unlock_irq(&priv->driver_lock);
803
804                 /* Check if we need to confirm Sleep Request received previously */
805                 if (priv->psstate == PS_STATE_PRE_SLEEP &&
806                     !priv->dnld_sent && !priv->cur_cmd) {
807                         if (priv->connect_status == LBS_CONNECTED) {
808                                 lbs_deb_thread("main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p dnld_sent=%d cur_cmd=%p, confirm now\n",
809                                                priv->intcounter, priv->currenttxskb, priv->dnld_sent, priv->cur_cmd);
810
811                                 lbs_ps_confirm_sleep(priv, (u16) priv->psmode);
812                         } else {
813                                 /* workaround for firmware sending
814                                  * deauth/linkloss event immediately
815                                  * after sleep request; remove this
816                                  * after firmware fixes it
817                                  */
818                                 priv->psstate = PS_STATE_AWAKE;
819                                 lbs_pr_alert("main-thread: ignore PS_SleepConfirm in non-connected state\n");
820                         }
821                 }
822
823                 /* The PS state is changed during processing of Sleep Request
824                  * event above
825                  */
826                 if ((priv->psstate == PS_STATE_SLEEP) ||
827                     (priv->psstate == PS_STATE_PRE_SLEEP))
828                         continue;
829
830                 /* Execute the next command */
831                 if (!priv->dnld_sent && !priv->cur_cmd)
832                         lbs_execute_next_command(priv);
833
834                 /* Wake-up command waiters which can't sleep in
835                  * lbs_prepare_and_send_command
836                  */
837                 if (!list_empty(&priv->cmdpendingq))
838                         wake_up_all(&priv->cmd_pending);
839         }
840
841         del_timer(&priv->command_timer);
842         wake_up_all(&priv->cmd_pending);
843
844         lbs_deb_leave(LBS_DEB_THREAD);
845         return 0;
846 }
847
848 /**
849  *  @brief This function downloads firmware image, gets
850  *  HW spec from firmware and set basic parameters to
851  *  firmware.
852  *
853  *  @param priv    A pointer to struct lbs_private structure
854  *  @return        0 or -1
855  */
856 static int lbs_setup_firmware(struct lbs_private *priv)
857 {
858         int ret = -1;
859         struct cmd_ds_mesh_access mesh_access;
860
861         lbs_deb_enter(LBS_DEB_FW);
862
863         /*
864          * Read MAC address from HW
865          */
866         memset(priv->current_addr, 0xff, ETH_ALEN);
867
868         ret = lbs_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
869                                     0, CMD_OPTION_WAITFORRSP, 0, NULL);
870
871         if (ret) {
872                 ret = -1;
873                 goto done;
874         }
875
876         lbs_set_mac_packet_filter(priv);
877
878         /* Get the supported Data rates */
879         ret = lbs_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
880                                     CMD_ACT_GET_TX_RATE,
881                                     CMD_OPTION_WAITFORRSP, 0, NULL);
882
883         if (ret) {
884                 ret = -1;
885                 goto done;
886         }
887
888         /* Disable mesh autostart */
889         if (priv->mesh_dev) {
890                 memset(&mesh_access, 0, sizeof(mesh_access));
891                 mesh_access.data[0] = cpu_to_le32(0);
892                 ret = lbs_prepare_and_send_command(priv,
893                                 CMD_MESH_ACCESS,
894                                 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
895                                 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
896                 if (ret) {
897                         ret = -1;
898                         goto done;
899                 }
900                 priv->mesh_autostart_enabled = 0;
901         }
902
903         ret = 0;
904 done:
905         lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
906         return ret;
907 }
908
909 /**
910  *  This function handles the timeout of command sending.
911  *  It will re-send the same command again.
912  */
913 static void command_timer_fn(unsigned long data)
914 {
915         struct lbs_private *priv = (struct lbs_private *)data;
916         struct cmd_ctrl_node *ptempnode;
917         struct cmd_ds_command *cmd;
918         unsigned long flags;
919
920         ptempnode = priv->cur_cmd;
921         if (ptempnode == NULL) {
922                 lbs_deb_fw("ptempnode empty\n");
923                 return;
924         }
925
926         cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
927         if (!cmd) {
928                 lbs_deb_fw("cmd is NULL\n");
929                 return;
930         }
931
932         lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
933
934         if (!priv->fw_ready)
935                 return;
936
937         spin_lock_irqsave(&priv->driver_lock, flags);
938         priv->cur_cmd = NULL;
939         spin_unlock_irqrestore(&priv->driver_lock, flags);
940
941         lbs_deb_fw("re-sending same command because of timeout\n");
942         lbs_queue_cmd(priv, ptempnode, 0);
943
944         wake_up_interruptible(&priv->waitq);
945
946         return;
947 }
948
949 static int lbs_init_adapter(struct lbs_private *priv)
950 {
951         size_t bufsize;
952         int i, ret = 0;
953
954         /* Allocate buffer to store the BSSID list */
955         bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
956         priv->networks = kzalloc(bufsize, GFP_KERNEL);
957         if (!priv->networks) {
958                 lbs_pr_err("Out of memory allocating beacons\n");
959                 ret = -1;
960                 goto out;
961         }
962
963         /* Initialize scan result lists */
964         INIT_LIST_HEAD(&priv->network_free_list);
965         INIT_LIST_HEAD(&priv->network_list);
966         for (i = 0; i < MAX_NETWORK_COUNT; i++) {
967                 list_add_tail(&priv->networks[i].list,
968                               &priv->network_free_list);
969         }
970
971         priv->lbs_ps_confirm_sleep.seqnum = cpu_to_le16(++priv->seqnum);
972         priv->lbs_ps_confirm_sleep.command =
973             cpu_to_le16(CMD_802_11_PS_MODE);
974         priv->lbs_ps_confirm_sleep.size =
975             cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
976         priv->lbs_ps_confirm_sleep.action =
977             cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
978
979         memset(priv->current_addr, 0xff, ETH_ALEN);
980
981         priv->connect_status = LBS_DISCONNECTED;
982         priv->mesh_connect_status = LBS_DISCONNECTED;
983         priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
984         priv->mode = IW_MODE_INFRA;
985         priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
986         priv->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
987         priv->radioon = RADIO_ON;
988         priv->auto_rate = 1;
989         priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
990         priv->psmode = LBS802_11POWERMODECAM;
991         priv->psstate = PS_STATE_FULL_POWER;
992
993         mutex_init(&priv->lock);
994
995         setup_timer(&priv->command_timer, command_timer_fn,
996                     (unsigned long)priv);
997
998         INIT_LIST_HEAD(&priv->cmdfreeq);
999         INIT_LIST_HEAD(&priv->cmdpendingq);
1000
1001         spin_lock_init(&priv->driver_lock);
1002         init_waitqueue_head(&priv->cmd_pending);
1003
1004         /* Allocate the command buffers */
1005         if (lbs_allocate_cmd_buffer(priv)) {
1006                 lbs_pr_err("Out of memory allocating command buffers\n");
1007                 ret = -1;
1008         }
1009
1010 out:
1011         return ret;
1012 }
1013
1014 static void lbs_free_adapter(struct lbs_private *priv)
1015 {
1016         lbs_deb_fw("free command buffer\n");
1017         lbs_free_cmd_buffer(priv);
1018
1019         lbs_deb_fw("free command_timer\n");
1020         del_timer(&priv->command_timer);
1021
1022         lbs_deb_fw("free scan results table\n");
1023         kfree(priv->networks);
1024         priv->networks = NULL;
1025 }
1026
1027 /**
1028  * @brief This function adds the card. it will probe the
1029  * card, allocate the lbs_priv and initialize the device.
1030  *
1031  *  @param card    A pointer to card
1032  *  @return        A pointer to struct lbs_private structure
1033  */
1034 struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
1035 {
1036         struct net_device *dev = NULL;
1037         struct lbs_private *priv = NULL;
1038
1039         lbs_deb_enter(LBS_DEB_NET);
1040
1041         /* Allocate an Ethernet device and register it */
1042         dev = alloc_etherdev(sizeof(struct lbs_private));
1043         if (!dev) {
1044                 lbs_pr_err("init ethX device failed\n");
1045                 goto done;
1046         }
1047         priv = dev->priv;
1048
1049         if (lbs_init_adapter(priv)) {
1050                 lbs_pr_err("failed to initialize adapter structure.\n");
1051                 goto err_init_adapter;
1052         }
1053
1054         priv->dev = dev;
1055         priv->card = card;
1056         priv->mesh_open = 0;
1057         priv->infra_open = 0;
1058
1059         /* Setup the OS Interface to our functions */
1060         dev->open = lbs_open;
1061         dev->hard_start_xmit = lbs_hard_start_xmit;
1062         dev->stop = lbs_close;
1063         dev->set_mac_address = lbs_set_mac_address;
1064         dev->tx_timeout = lbs_tx_timeout;
1065         dev->get_stats = lbs_get_stats;
1066         dev->watchdog_timeo = 5 * HZ;
1067         dev->ethtool_ops = &lbs_ethtool_ops;
1068 #ifdef  WIRELESS_EXT
1069         dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
1070 #endif
1071         dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1072         dev->set_multicast_list = lbs_set_multicast_list;
1073
1074         SET_NETDEV_DEV(dev, dmdev);
1075
1076         priv->rtap_net_dev = NULL;
1077
1078         lbs_deb_thread("Starting main thread...\n");
1079         init_waitqueue_head(&priv->waitq);
1080         priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
1081         if (IS_ERR(priv->main_thread)) {
1082                 lbs_deb_thread("Error creating main thread.\n");
1083                 goto err_init_adapter;
1084         }
1085
1086         priv->work_thread = create_singlethread_workqueue("lbs_worker");
1087         INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
1088         INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
1089         INIT_WORK(&priv->sync_channel, lbs_sync_channel);
1090
1091         goto done;
1092
1093 err_init_adapter:
1094         lbs_free_adapter(priv);
1095         free_netdev(dev);
1096         priv = NULL;
1097
1098 done:
1099         lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
1100         return priv;
1101 }
1102 EXPORT_SYMBOL_GPL(lbs_add_card);
1103
1104
1105 int lbs_remove_card(struct lbs_private *priv)
1106 {
1107         struct net_device *dev = priv->dev;
1108         union iwreq_data wrqu;
1109
1110         lbs_deb_enter(LBS_DEB_MAIN);
1111
1112         lbs_remove_rtap(priv);
1113
1114         dev = priv->dev;
1115         device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
1116
1117         cancel_delayed_work(&priv->scan_work);
1118         cancel_delayed_work(&priv->assoc_work);
1119         destroy_workqueue(priv->work_thread);
1120
1121         if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1122                 priv->psmode = LBS802_11POWERMODECAM;
1123                 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1124         }
1125
1126         memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1127         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1128         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1129
1130         /* Stop the thread servicing the interrupts */
1131         priv->surpriseremoved = 1;
1132         kthread_stop(priv->main_thread);
1133
1134         lbs_free_adapter(priv);
1135
1136         priv->dev = NULL;
1137         free_netdev(dev);
1138
1139         lbs_deb_leave(LBS_DEB_MAIN);
1140         return 0;
1141 }
1142 EXPORT_SYMBOL_GPL(lbs_remove_card);
1143
1144
1145 int lbs_start_card(struct lbs_private *priv)
1146 {
1147         struct net_device *dev = priv->dev;
1148         int ret = -1;
1149
1150         lbs_deb_enter(LBS_DEB_MAIN);
1151
1152         /* poke the firmware */
1153         ret = lbs_setup_firmware(priv);
1154         if (ret)
1155                 goto done;
1156
1157         /* init 802.11d */
1158         lbs_init_11d(priv);
1159
1160         if (register_netdev(dev)) {
1161                 lbs_pr_err("cannot register ethX device\n");
1162                 goto done;
1163         }
1164         if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
1165                 lbs_pr_err("cannot register lbs_rtap attribute\n");
1166
1167         lbs_debugfs_init_one(priv, dev);
1168
1169         lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
1170
1171         ret = 0;
1172
1173 done:
1174         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1175         return ret;
1176 }
1177 EXPORT_SYMBOL_GPL(lbs_start_card);
1178
1179
1180 int lbs_stop_card(struct lbs_private *priv)
1181 {
1182         struct net_device *dev = priv->dev;
1183         int ret = -1;
1184         struct cmd_ctrl_node *cmdnode;
1185         unsigned long flags;
1186
1187         lbs_deb_enter(LBS_DEB_MAIN);
1188
1189         netif_stop_queue(priv->dev);
1190         netif_carrier_off(priv->dev);
1191
1192         lbs_debugfs_remove_one(priv);
1193
1194         /* Flush pending command nodes */
1195         spin_lock_irqsave(&priv->driver_lock, flags);
1196         list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
1197                 cmdnode->cmdwaitqwoken = 1;
1198                 wake_up_interruptible(&cmdnode->cmdwait_q);
1199         }
1200         spin_unlock_irqrestore(&priv->driver_lock, flags);
1201
1202         unregister_netdev(dev);
1203
1204         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1205         return ret;
1206 }
1207 EXPORT_SYMBOL_GPL(lbs_stop_card);
1208
1209
1210 /**
1211  * @brief This function adds mshX interface
1212  *
1213  *  @param priv    A pointer to the struct lbs_private structure
1214  *  @return        0 if successful, -X otherwise
1215  */
1216 int lbs_add_mesh(struct lbs_private *priv, struct device *dev)
1217 {
1218         struct net_device *mesh_dev = NULL;
1219         int ret = 0;
1220
1221         lbs_deb_enter(LBS_DEB_MESH);
1222
1223         /* Allocate a virtual mesh device */
1224         if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1225                 lbs_deb_mesh("init mshX device failed\n");
1226                 ret = -ENOMEM;
1227                 goto done;
1228         }
1229         mesh_dev->priv = priv;
1230         priv->mesh_dev = mesh_dev;
1231
1232         mesh_dev->open = lbs_mesh_open;
1233         mesh_dev->hard_start_xmit = lbs_hard_start_xmit;
1234         mesh_dev->stop = lbs_mesh_close;
1235         mesh_dev->get_stats = lbs_get_stats;
1236         mesh_dev->set_mac_address = lbs_set_mac_address;
1237         mesh_dev->ethtool_ops = &lbs_ethtool_ops;
1238         memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1239                         sizeof(priv->dev->dev_addr));
1240
1241         SET_NETDEV_DEV(priv->mesh_dev, dev);
1242
1243 #ifdef  WIRELESS_EXT
1244         mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
1245 #endif
1246         /* Register virtual mesh interface */
1247         ret = register_netdev(mesh_dev);
1248         if (ret) {
1249                 lbs_pr_err("cannot register mshX virtual interface\n");
1250                 goto err_free;
1251         }
1252
1253         ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1254         if (ret)
1255                 goto err_unregister;
1256
1257         /* Everything successful */
1258         ret = 0;
1259         goto done;
1260
1261 err_unregister:
1262         unregister_netdev(mesh_dev);
1263
1264 err_free:
1265         free_netdev(mesh_dev);
1266
1267 done:
1268         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1269         return ret;
1270 }
1271 EXPORT_SYMBOL_GPL(lbs_add_mesh);
1272
1273
1274 void lbs_remove_mesh(struct lbs_private *priv)
1275 {
1276         struct net_device *mesh_dev;
1277
1278         lbs_deb_enter(LBS_DEB_MAIN);
1279
1280         if (!priv)
1281                 goto out;
1282
1283         mesh_dev = priv->mesh_dev;
1284
1285         netif_stop_queue(mesh_dev);
1286         netif_carrier_off(priv->mesh_dev);
1287
1288         sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1289         unregister_netdev(mesh_dev);
1290
1291         priv->mesh_dev = NULL ;
1292         free_netdev(mesh_dev);
1293
1294 out:
1295         lbs_deb_leave(LBS_DEB_MAIN);
1296 }
1297 EXPORT_SYMBOL_GPL(lbs_remove_mesh);
1298
1299 /**
1300  *  @brief This function finds the CFP in
1301  *  region_cfp_table based on region and band parameter.
1302  *
1303  *  @param region  The region code
1304  *  @param band    The band
1305  *  @param cfp_no  A pointer to CFP number
1306  *  @return        A pointer to CFP
1307  */
1308 struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1309 {
1310         int i, end;
1311
1312         lbs_deb_enter(LBS_DEB_MAIN);
1313
1314         end = ARRAY_SIZE(region_cfp_table);
1315
1316         for (i = 0; i < end ; i++) {
1317                 lbs_deb_main("region_cfp_table[i].region=%d\n",
1318                         region_cfp_table[i].region);
1319                 if (region_cfp_table[i].region == region) {
1320                         *cfp_no = region_cfp_table[i].cfp_no_BG;
1321                         lbs_deb_leave(LBS_DEB_MAIN);
1322                         return region_cfp_table[i].cfp_BG;
1323                 }
1324         }
1325
1326         lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1327         return NULL;
1328 }
1329
1330 int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
1331 {
1332         int ret = 0;
1333         int i = 0;
1334
1335         struct chan_freq_power *cfp;
1336         int cfp_no;
1337
1338         lbs_deb_enter(LBS_DEB_MAIN);
1339
1340         memset(priv->region_channel, 0, sizeof(priv->region_channel));
1341
1342         {
1343                 cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
1344                 if (cfp != NULL) {
1345                         priv->region_channel[i].nrcfp = cfp_no;
1346                         priv->region_channel[i].CFP = cfp;
1347                 } else {
1348                         lbs_deb_main("wrong region code %#x in band B/G\n",
1349                                region);
1350                         ret = -1;
1351                         goto out;
1352                 }
1353                 priv->region_channel[i].valid = 1;
1354                 priv->region_channel[i].region = region;
1355                 priv->region_channel[i].band = band;
1356                 i++;
1357         }
1358 out:
1359         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1360         return ret;
1361 }
1362
1363 /**
1364  *  @brief This function handles the interrupt. it will change PS
1365  *  state if applicable. it will wake up main_thread to handle
1366  *  the interrupt event as well.
1367  *
1368  *  @param dev     A pointer to net_device structure
1369  *  @return        n/a
1370  */
1371 void lbs_interrupt(struct net_device *dev)
1372 {
1373         struct lbs_private *priv = dev->priv;
1374
1375         lbs_deb_enter(LBS_DEB_THREAD);
1376
1377         lbs_deb_thread("lbs_interrupt: intcounter=%d\n",
1378                priv->intcounter);
1379
1380         priv->intcounter++;
1381
1382         if (priv->psstate == PS_STATE_SLEEP) {
1383                 priv->psstate = PS_STATE_AWAKE;
1384                 netif_wake_queue(dev);
1385                 if (priv->mesh_dev)
1386                         netif_wake_queue(priv->mesh_dev);
1387         }
1388
1389         wake_up_interruptible(&priv->waitq);
1390
1391         lbs_deb_leave(LBS_DEB_THREAD);
1392 }
1393 EXPORT_SYMBOL_GPL(lbs_interrupt);
1394
1395 int lbs_reset_device(struct lbs_private *priv)
1396 {
1397         int ret;
1398
1399         lbs_deb_enter(LBS_DEB_MAIN);
1400         ret = lbs_prepare_and_send_command(priv, CMD_802_11_RESET,
1401                                     CMD_ACT_HALT, 0, 0, NULL);
1402         msleep_interruptible(10);
1403
1404         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1405         return ret;
1406 }
1407 EXPORT_SYMBOL_GPL(lbs_reset_device);
1408
1409 static int __init lbs_init_module(void)
1410 {
1411         lbs_deb_enter(LBS_DEB_MAIN);
1412         lbs_debugfs_init();
1413         lbs_deb_leave(LBS_DEB_MAIN);
1414         return 0;
1415 }
1416
1417 static void __exit lbs_exit_module(void)
1418 {
1419         lbs_deb_enter(LBS_DEB_MAIN);
1420
1421         lbs_debugfs_remove();
1422
1423         lbs_deb_leave(LBS_DEB_MAIN);
1424 }
1425
1426 /*
1427  * rtap interface support fuctions
1428  */
1429
1430 static int lbs_rtap_open(struct net_device *dev)
1431 {
1432         netif_carrier_off(dev);
1433         netif_stop_queue(dev);
1434         return 0;
1435 }
1436
1437 static int lbs_rtap_stop(struct net_device *dev)
1438 {
1439         return 0;
1440 }
1441
1442 static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1443 {
1444         netif_stop_queue(dev);
1445         return -EOPNOTSUPP;
1446 }
1447
1448 static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
1449 {
1450         struct lbs_private *priv = dev->priv;
1451         return &priv->stats;
1452 }
1453
1454
1455 void lbs_remove_rtap(struct lbs_private *priv)
1456 {
1457         if (priv->rtap_net_dev == NULL)
1458                 return;
1459         unregister_netdev(priv->rtap_net_dev);
1460         free_netdev(priv->rtap_net_dev);
1461         priv->rtap_net_dev = NULL;
1462 }
1463
1464 int lbs_add_rtap(struct lbs_private *priv)
1465 {
1466         int rc = 0;
1467         struct net_device *rtap_dev;
1468
1469         if (priv->rtap_net_dev)
1470                 return -EPERM;
1471
1472         rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
1473         if (rtap_dev == NULL)
1474                 return -ENOMEM;
1475
1476         memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
1477         rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
1478         rtap_dev->open = lbs_rtap_open;
1479         rtap_dev->stop = lbs_rtap_stop;
1480         rtap_dev->get_stats = lbs_rtap_get_stats;
1481         rtap_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
1482         rtap_dev->set_multicast_list = lbs_set_multicast_list;
1483         rtap_dev->priv = priv;
1484
1485         rc = register_netdev(rtap_dev);
1486         if (rc) {
1487                 free_netdev(rtap_dev);
1488                 return rc;
1489         }
1490         priv->rtap_net_dev = rtap_dev;
1491
1492         return 0;
1493 }
1494
1495
1496 module_init(lbs_init_module);
1497 module_exit(lbs_exit_module);
1498
1499 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1500 MODULE_AUTHOR("Marvell International Ltd.");
1501 MODULE_LICENSE("GPL");