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