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