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