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