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