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