libertas: remove lots of unused stuff
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / libertas / main.c
index 42b64b5ad08348cd979342f0a6653db08ef993d6..1eb0cb0a82b79c89d413bb537930fb6475c633cc 100644 (file)
@@ -6,7 +6,6 @@
 
 #include <linux/moduleparam.h>
 #include <linux/delay.h>
-#include <linux/freezer.h>
 #include <linux/etherdevice.h>
 #include <linux/netdevice.h>
 #include <linux/if_arp.h>
@@ -22,6 +21,7 @@
 #include "debugfs.h"
 #include "assoc.h"
 #include "join.h"
+#include "cmd.h"
 
 #define DRIVER_RELEASE_VERSION "323.p0"
 const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
@@ -216,13 +216,15 @@ u8 lbs_data_rate_to_fw_index(u32 rate)
 static ssize_t lbs_anycast_get(struct device *dev,
                struct device_attribute *attr, char * buf)
 {
+       struct lbs_private *priv = to_net_dev(dev)->priv;
        struct cmd_ds_mesh_access mesh_access;
+       int ret;
 
        memset(&mesh_access, 0, sizeof(mesh_access));
-       lbs_prepare_and_send_command(to_net_dev(dev)->priv,
-                       CMD_MESH_ACCESS,
-                       CMD_ACT_MESH_GET_ANYCAST,
-                       CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
+
+       ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
+       if (ret)
+               return ret;
 
        return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
 }
@@ -233,22 +235,27 @@ static ssize_t lbs_anycast_get(struct device *dev,
 static ssize_t lbs_anycast_set(struct device *dev,
                struct device_attribute *attr, const char * buf, size_t count)
 {
+       struct lbs_private *priv = to_net_dev(dev)->priv;
        struct cmd_ds_mesh_access mesh_access;
        uint32_t datum;
+       int ret;
 
        memset(&mesh_access, 0, sizeof(mesh_access));
        sscanf(buf, "%x", &datum);
        mesh_access.data[0] = cpu_to_le32(datum);
 
-       lbs_prepare_and_send_command((to_net_dev(dev))->priv,
-                       CMD_MESH_ACCESS,
-                       CMD_ACT_MESH_SET_ANYCAST,
-                       CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
+       ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
+       if (ret)
+               return ret;
+
        return strlen(buf);
 }
 
-int lbs_add_rtap(struct lbs_private *priv);
-void lbs_remove_rtap(struct lbs_private *priv);
+static int lbs_add_rtap(struct lbs_private *priv);
+static void lbs_remove_rtap(struct lbs_private *priv);
+static int lbs_add_mesh(struct lbs_private *priv);
+static void lbs_remove_mesh(struct lbs_private *priv);
+
 
 /**
  * Get function for sysfs attribute rtap
@@ -274,6 +281,8 @@ static ssize_t lbs_rtap_set(struct device *dev,
                if(priv->monitormode == monitor_mode)
                        return strlen(buf);
                if (priv->monitormode == LBS_MONITOR_OFF) {
+                       if (priv->infra_open || priv->mesh_open)
+                               return -EBUSY;
                        if (priv->mode == IW_MODE_INFRA)
                                lbs_send_deauthentication(priv);
                        else if (priv->mode == IW_MODE_ADHOC)
@@ -305,60 +314,62 @@ static ssize_t lbs_rtap_set(struct device *dev,
 }
 
 /**
- * lbs_rtap attribute to be exported per mshX interface
- * through sysfs (/sys/class/net/mshX/libertas-rtap)
+ * lbs_rtap attribute to be exported per ethX interface
+ * through sysfs (/sys/class/net/ethX/lbs_rtap)
  */
-static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get,
-               lbs_rtap_set );
+static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
 
 /**
- * anycast_mask attribute to be exported per mshX interface
- * through sysfs (/sys/class/net/mshX/anycast_mask)
+ * Get function for sysfs attribute mesh
  */
-static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
-
-static ssize_t lbs_autostart_enabled_get(struct device *dev,
+static ssize_t lbs_mesh_get(struct device *dev,
                struct device_attribute *attr, char * buf)
 {
-       struct cmd_ds_mesh_access mesh_access;
-
-       memset(&mesh_access, 0, sizeof(mesh_access));
-       lbs_prepare_and_send_command(to_net_dev(dev)->priv,
-                       CMD_MESH_ACCESS,
-                       CMD_ACT_MESH_GET_AUTOSTART_ENABLED,
-                       CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
-
-       return sprintf(buf, "%d\n", le32_to_cpu(mesh_access.data[0]));
+       struct lbs_private *priv = to_net_dev(dev)->priv;
+       return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
 }
 
-static ssize_t lbs_autostart_enabled_set(struct device *dev,
+/**
+ *  Set function for sysfs attribute mesh
+ */
+static ssize_t lbs_mesh_set(struct device *dev,
                struct device_attribute *attr, const char * buf, size_t count)
 {
-       struct cmd_ds_mesh_access mesh_access;
-       uint32_t datum;
-       struct lbs_private *priv = (to_net_dev(dev))->priv;
+       struct lbs_private *priv = to_net_dev(dev)->priv;
+       int enable;
        int ret;
 
-       memset(&mesh_access, 0, sizeof(mesh_access));
-       sscanf(buf, "%d", &datum);
-       mesh_access.data[0] = cpu_to_le32(datum);
+       sscanf(buf, "%x", &enable);
+       enable = !!enable;
+       if (enable == !!priv->mesh_dev)
+               return count;
 
-       ret = lbs_prepare_and_send_command(priv,
-                       CMD_MESH_ACCESS,
-                       CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
-                       CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
-       if (ret == 0)
-               priv->mesh_autostart_enabled = datum ? 1 : 0;
+       ret = lbs_mesh_config(priv, enable, priv->curbssparams.channel);
+       if (ret)
+               return ret;
 
-       return strlen(buf);
+       if (enable)
+               lbs_add_mesh(priv);
+       else
+               lbs_remove_mesh(priv);
+
+       return count;
 }
 
-static DEVICE_ATTR(autostart_enabled, 0644,
-               lbs_autostart_enabled_get, lbs_autostart_enabled_set);
+/**
+ * lbs_mesh attribute to be exported per ethX interface
+ * through sysfs (/sys/class/net/ethX/lbs_mesh)
+ */
+static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
+
+/**
+ * anycast_mask attribute to be exported per mshX interface
+ * through sysfs (/sys/class/net/mshX/anycast_mask)
+ */
+static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
 
 static struct attribute *lbs_mesh_sysfs_entries[] = {
        &dev_attr_anycast_mask.attr,
-       &dev_attr_autostart_enabled.attr,
        NULL,
 };
 
@@ -367,152 +378,91 @@ static struct attribute_group lbs_mesh_attr_group = {
 };
 
 /**
- *  @brief Check if the device can be open and wait if necessary.
+ *  @brief This function opens the ethX or mshX interface
  *
  *  @param dev     A pointer to net_device structure
- *  @return       0
- *
- * For USB adapter, on some systems the device open handler will be
- * called before FW ready. Use the following flag check and wait
- * function to work around the issue.
- *
- */
-static int pre_open_check(struct net_device *dev)
-{
-       struct lbs_private *priv = (struct lbs_private *) dev->priv;
-       int i = 0;
-
-       while (!priv->fw_ready && i < 20) {
-               i++;
-               msleep_interruptible(100);
-       }
-       if (!priv->fw_ready) {
-               lbs_pr_err("firmware not ready\n");
-               return -1;
-       }
-
-       return 0;
-}
-
-/**
- *  @brief This function opens the device
- *
- *  @param dev     A pointer to net_device structure
- *  @return       0
+ *  @return       0 or -EBUSY if monitor mode active
  */
 static int lbs_dev_open(struct net_device *dev)
 {
-       struct lbs_private *priv = (struct lbs_private *) dev->priv;
+       struct lbs_private *priv = (struct lbs_private *) dev->priv ;
+       int ret = 0;
 
        lbs_deb_enter(LBS_DEB_NET);
 
-       priv->open = 1;
+       spin_lock_irq(&priv->driver_lock);
 
-       if (priv->connect_status == LBS_CONNECTED)
-               netif_carrier_on(priv->dev);
-       else
-               netif_carrier_off(priv->dev);
-
-       if (priv->mesh_dev) {
-               if (priv->mesh_connect_status == LBS_CONNECTED)
-                       netif_carrier_on(priv->mesh_dev);
-               else
-                       netif_carrier_off(priv->mesh_dev);
+       if (priv->monitormode != LBS_MONITOR_OFF) {
+               ret = -EBUSY;
+               goto out;
        }
 
-       lbs_deb_leave(LBS_DEB_NET);
-       return 0;
-}
-/**
- *  @brief This function opens the mshX interface
- *
- *  @param dev     A pointer to net_device structure
- *  @return       0
- */
-static int lbs_mesh_open(struct net_device *dev)
-{
-       struct lbs_private *priv = (struct lbs_private *) dev->priv ;
+       if (dev == priv->mesh_dev) {
+               priv->mesh_open = 1;
+               priv->mesh_connect_status = LBS_CONNECTED;
+               netif_carrier_on(dev);
+       } else {
+               priv->infra_open = 1;
 
-       if (pre_open_check(dev) == -1)
-               return -1;
-       priv->mesh_open = 1 ;
-       netif_wake_queue(priv->mesh_dev);
+               if (priv->connect_status == LBS_CONNECTED)
+                       netif_carrier_on(dev);
+               else
+                       netif_carrier_off(dev);
+       }
 
-       priv->mesh_connect_status = LBS_CONNECTED;
+       if (!priv->tx_pending_len)
+               netif_wake_queue(dev);
+ out:
 
-       netif_carrier_on(priv->mesh_dev);
-       netif_wake_queue(priv->mesh_dev);
-       if (priv->infra_open == 0)
-               return lbs_dev_open(priv->dev) ;
-       return 0;
+       spin_unlock_irq(&priv->driver_lock);
+       lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
+       return ret;
 }
 
 /**
- *  @brief This function opens the ethX interface
+ *  @brief This function closes the mshX interface
  *
  *  @param dev     A pointer to net_device structure
  *  @return       0
  */
-static int lbs_open(struct net_device *dev)
+static int lbs_mesh_stop(struct net_device *dev)
 {
-       struct lbs_private *priv = (struct lbs_private *) dev->priv ;
+       struct lbs_private *priv = (struct lbs_private *) (dev->priv);
 
-       if(pre_open_check(dev) == -1)
-               return -1;
-       priv->infra_open = 1 ;
-       netif_wake_queue(priv->dev);
-       if (priv->open == 0)
-               return lbs_dev_open(priv->dev) ;
-       return 0;
-}
+       lbs_deb_enter(LBS_DEB_MESH);
+       spin_lock_irq(&priv->driver_lock);
 
-static int lbs_dev_close(struct net_device *dev)
-{
-       struct lbs_private *priv = dev->priv;
+       priv->mesh_open = 0;
+       priv->mesh_connect_status = LBS_DISCONNECTED;
 
-       lbs_deb_enter(LBS_DEB_NET);
+       netif_stop_queue(dev);
+       netif_carrier_off(dev);
 
-       netif_carrier_off(priv->dev);
-       priv->open = 0;
+       spin_unlock_irq(&priv->driver_lock);
 
-       lbs_deb_leave(LBS_DEB_NET);
+       lbs_deb_leave(LBS_DEB_MESH);
        return 0;
 }
 
-/**
- *  @brief This function closes the mshX interface
- *
- *  @param dev     A pointer to net_device structure
- *  @return       0
- */
-static int lbs_mesh_close(struct net_device *dev)
-{
-       struct lbs_private *priv = (struct lbs_private *) (dev->priv);
-
-       priv->mesh_open = 0;
-       netif_stop_queue(priv->mesh_dev);
-       if (priv->infra_open == 0)
-               return lbs_dev_close(dev);
-       else
-               return 0;
-}
-
 /**
  *  @brief This function closes the ethX interface
  *
  *  @param dev     A pointer to net_device structure
  *  @return       0
  */
-static int lbs_close(struct net_device *dev)
+static int lbs_eth_stop(struct net_device *dev)
 {
        struct lbs_private *priv = (struct lbs_private *) dev->priv;
 
-       netif_stop_queue(dev);
+       lbs_deb_enter(LBS_DEB_NET);
+
+       spin_lock_irq(&priv->driver_lock);
        priv->infra_open = 0;
-       if (priv->mesh_open == 0)
-               return lbs_dev_close(dev);
-       else
-               return 0;
+       netif_stop_queue(dev);
+       spin_unlock_irq(&priv->driver_lock);
+
+       lbs_deb_leave(LBS_DEB_NET);
+       return 0;
 }
 
 static void lbs_tx_timeout(struct net_device *dev)
@@ -533,6 +483,13 @@ static void lbs_tx_timeout(struct net_device *dev)
           to kick it somehow? */
        lbs_host_to_card_done(priv);
 
+       /* More often than not, this actually happens because the
+          firmware has crapped itself -- rather than just a very
+          busy medium. So send a harmless command, and if/when
+          _that_ times out, we'll kick it in the head. */
+       lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
+                                    0, 0, NULL);
+
        lbs_deb_leave(LBS_DEB_TX);
 }
 
@@ -540,25 +497,18 @@ void lbs_host_to_card_done(struct lbs_private *priv)
 {
        unsigned long flags;
 
+       lbs_deb_enter(LBS_DEB_THREAD);
+
        spin_lock_irqsave(&priv->driver_lock, flags);
 
        priv->dnld_sent = DNLD_RES_RECEIVED;
 
        /* Wake main thread if commands are pending */
-       if (!priv->cur_cmd)
+       if (!priv->cur_cmd || priv->tx_pending_len > 0)
                wake_up_interruptible(&priv->waitq);
 
-       /* Don't wake netif queues if we're in monitor mode and
-          a TX packet is already pending, or if there are commands
-          queued to be sent. */
-       if (!priv->currenttxskb && list_empty(&priv->cmdpendingq)) {
-               if (priv->dev && priv->connect_status == LBS_CONNECTED)
-                       netif_wake_queue(priv->dev);
-
-               if (priv->mesh_dev && priv->mesh_connect_status == LBS_CONNECTED)
-                       netif_wake_queue(priv->mesh_dev);
-       }
        spin_unlock_irqrestore(&priv->driver_lock, flags);
+       lbs_deb_leave(LBS_DEB_THREAD);
 }
 EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
 
@@ -572,6 +522,7 @@ static struct net_device_stats *lbs_get_stats(struct net_device *dev)
 {
        struct lbs_private *priv = (struct lbs_private *) dev->priv;
 
+       lbs_deb_enter(LBS_DEB_NET);
        return &priv->stats;
 }
 
@@ -624,53 +575,51 @@ static int lbs_copy_multicast_address(struct lbs_private *priv,
                memcpy(&priv->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
                mcptr = mcptr->next;
        }
-
        return i;
-
 }
 
 static void lbs_set_multicast_list(struct net_device *dev)
 {
        struct lbs_private *priv = dev->priv;
-       int oldpacketfilter;
+       int old_mac_control;
        DECLARE_MAC_BUF(mac);
 
        lbs_deb_enter(LBS_DEB_NET);
 
-       oldpacketfilter = priv->currentpacketfilter;
+       old_mac_control = priv->mac_control;
 
        if (dev->flags & IFF_PROMISC) {
                lbs_deb_net("enable promiscuous mode\n");
-               priv->currentpacketfilter |=
+               priv->mac_control |=
                    CMD_ACT_MAC_PROMISCUOUS_ENABLE;
-               priv->currentpacketfilter &=
+               priv->mac_control &=
                    ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
                      CMD_ACT_MAC_MULTICAST_ENABLE);
        } else {
                /* Multicast */
-               priv->currentpacketfilter &=
+               priv->mac_control &=
                    ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
 
                if (dev->flags & IFF_ALLMULTI || dev->mc_count >
                    MRVDRV_MAX_MULTICAST_LIST_SIZE) {
                        lbs_deb_net( "enabling all multicast\n");
-                       priv->currentpacketfilter |=
+                       priv->mac_control |=
                            CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
-                       priv->currentpacketfilter &=
+                       priv->mac_control &=
                            ~CMD_ACT_MAC_MULTICAST_ENABLE;
                } else {
-                       priv->currentpacketfilter &=
+                       priv->mac_control &=
                            ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
 
                        if (!dev->mc_count) {
                                lbs_deb_net("no multicast addresses, "
                                       "disabling multicast\n");
-                               priv->currentpacketfilter &=
+                               priv->mac_control &=
                                    ~CMD_ACT_MAC_MULTICAST_ENABLE;
                        } else {
                                int i;
 
-                               priv->currentpacketfilter |=
+                               priv->mac_control |=
                                    CMD_ACT_MAC_MULTICAST_ENABLE;
 
                                priv->nr_of_multicastmacaddr =
@@ -680,7 +629,7 @@ static void lbs_set_multicast_list(struct net_device *dev)
                                       dev->mc_count);
 
                                for (i = 0; i < dev->mc_count; i++) {
-                                       lbs_deb_net("Multicast address %d:%s\n",
+                                       lbs_deb_net("Multicast address %d: %s\n",
                                               i, print_mac(mac,
                                               priv->multicastlist[i]));
                                }
@@ -693,9 +642,8 @@ static void lbs_set_multicast_list(struct net_device *dev)
                }
        }
 
-       if (priv->currentpacketfilter != oldpacketfilter) {
-               lbs_set_mac_packet_filter(priv);
-       }
+       if (priv->mac_control != old_mac_control)
+               lbs_set_mac_control(priv);
 
        lbs_deb_leave(LBS_DEB_NET);
 }
@@ -719,8 +667,6 @@ static int lbs_thread(void *data)
 
        init_waitqueue_entry(&wait, current);
 
-       set_freezable();
-
        for (;;) {
                int shouldsleep;
 
@@ -731,14 +677,22 @@ static int lbs_thread(void *data)
                set_current_state(TASK_INTERRUPTIBLE);
                spin_lock_irq(&priv->driver_lock);
 
-               if (priv->surpriseremoved)
+               if (kthread_should_stop())
                        shouldsleep = 0;        /* Bye */
+               else if (priv->surpriseremoved)
+                       shouldsleep = 1;        /* We need to wait until we're _told_ to die */
                else if (priv->psstate == PS_STATE_SLEEP)
                        shouldsleep = 1;        /* Sleep mode. Nothing we can do till it wakes */
                else if (priv->intcounter)
                        shouldsleep = 0;        /* Interrupt pending. Deal with it now */
+               else if (priv->cmd_timed_out)
+                       shouldsleep = 0;        /* Command timed out. Recover */
+               else if (!priv->fw_ready)
+                       shouldsleep = 1;        /* Firmware not ready. We're waiting for it */
                else if (priv->dnld_sent)
                        shouldsleep = 1;        /* Something is en route to the device already */
+               else if (priv->tx_pending_len > 0)
+                       shouldsleep = 0;        /* We've a packet to send */
                else if (priv->cur_cmd)
                        shouldsleep = 1;        /* Can't send a command; one already running */
                else if (!list_empty(&priv->cmdpendingq))
@@ -760,17 +714,19 @@ static int lbs_thread(void *data)
 
                set_current_state(TASK_RUNNING);
                remove_wait_queue(&priv->waitq, &wait);
-               try_to_freeze();
 
                lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
                               priv->intcounter, priv->currenttxskb, priv->dnld_sent);
 
-               if (kthread_should_stop() || priv->surpriseremoved) {
-                       lbs_deb_thread("main-thread: break from main thread: surpriseremoved=0x%x\n",
-                                      priv->surpriseremoved);
+               if (kthread_should_stop()) {
+                       lbs_deb_thread("main-thread: break from main thread\n");
                        break;
                }
 
+               if (priv->surpriseremoved) {
+                       lbs_deb_thread("adapter removed; waiting to die...\n");
+                       continue;
+               }
 
                spin_lock_irq(&priv->driver_lock);
 
@@ -801,6 +757,26 @@ static int lbs_thread(void *data)
                        spin_lock_irq(&priv->driver_lock);
                }
 
+               if (priv->cmd_timed_out && priv->cur_cmd) {
+                       struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
+
+                       if (++priv->nr_retries > 10) {
+                               lbs_pr_info("Excessive timeouts submitting command %x\n",
+                                           le16_to_cpu(cmdnode->cmdbuf->command));
+                               lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
+                               priv->nr_retries = 0;
+                       } else {
+                               priv->cur_cmd = NULL;
+                               lbs_pr_info("requeueing command %x due to timeout (#%d)\n",
+                                           le16_to_cpu(cmdnode->cmdbuf->command), priv->nr_retries);
+
+                               /* Stick it back at the _top_ of the pending queue
+                                  for immediate resubmission */
+                               list_add(&cmdnode->list, &priv->cmdpendingq);
+                       }
+               }
+               priv->cmd_timed_out = 0;
+
                /* Any Card Event */
                if (priv->hisregcpy & MRVDRV_CARDEVENT) {
                        lbs_deb_thread("main-thread: Card Event Activity\n");
@@ -817,6 +793,9 @@ static int lbs_thread(void *data)
                } else
                        spin_unlock_irq(&priv->driver_lock);
 
+               if (!priv->fw_ready)
+                       continue;
+
                /* Check if we need to confirm Sleep Request received previously */
                if (priv->psstate == PS_STATE_PRE_SLEEP &&
                    !priv->dnld_sent && !priv->cur_cmd) {
@@ -824,7 +803,7 @@ static int lbs_thread(void *data)
                                lbs_deb_thread("main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p dnld_sent=%d cur_cmd=%p, confirm now\n",
                                               priv->intcounter, priv->currenttxskb, priv->dnld_sent, priv->cur_cmd);
 
-                               lbs_ps_confirm_sleep(priv, (u16) priv->psmode);
+                               lbs_ps_confirm_sleep(priv);
                        } else {
                                /* workaround for firmware sending
                                 * deauth/linkloss event immediately
@@ -852,6 +831,28 @@ static int lbs_thread(void *data)
                 */
                if (!list_empty(&priv->cmdpendingq))
                        wake_up_all(&priv->cmd_pending);
+
+               spin_lock_irq(&priv->driver_lock);
+               if (!priv->dnld_sent && priv->tx_pending_len > 0) {
+                       int ret = priv->hw_host_to_card(priv, MVMS_DAT,
+                                                       priv->tx_pending_buf,
+                                                       priv->tx_pending_len);
+                       if (ret) {
+                               lbs_deb_tx("host_to_card failed %d\n", ret);
+                               priv->dnld_sent = DNLD_RES_RECEIVED;
+                       }
+                       priv->tx_pending_len = 0;
+                       if (!priv->currenttxskb) {
+                               /* We can wake the queues immediately if we aren't
+                                  waiting for TX feedback */
+                               if (priv->connect_status == LBS_CONNECTED)
+                                       netif_wake_queue(priv->dev);
+                               if (priv->mesh_dev &&
+                                   priv->mesh_connect_status == LBS_CONNECTED)
+                                       netif_wake_queue(priv->mesh_dev);
+                       }
+               }
+               spin_unlock_irq(&priv->driver_lock);
        }
 
        del_timer(&priv->command_timer);
@@ -861,6 +862,64 @@ static int lbs_thread(void *data)
        return 0;
 }
 
+static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
+                               struct cmd_header *cmd)
+{
+       lbs_deb_enter(LBS_DEB_FW);
+
+       netif_device_detach(priv->dev);
+       if (priv->mesh_dev)
+               netif_device_detach(priv->mesh_dev);
+
+       priv->fw_ready = 0;
+       lbs_deb_leave(LBS_DEB_FW);
+       return 0;
+}
+
+int lbs_suspend(struct lbs_private *priv)
+{
+       struct cmd_header cmd;
+       int ret;
+
+       lbs_deb_enter(LBS_DEB_FW);
+
+       if (priv->wol_criteria == 0xffffffff) {
+               lbs_pr_info("Suspend attempt without configuring wake params!\n");
+               return -EINVAL;
+       }
+
+       memset(&cmd, 0, sizeof(cmd));
+
+       ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
+                       sizeof(cmd), lbs_suspend_callback, 0);
+       if (ret)
+               lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
+
+       lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(lbs_suspend);
+
+int lbs_resume(struct lbs_private *priv)
+{
+       lbs_deb_enter(LBS_DEB_FW);
+
+       priv->fw_ready = 1;
+
+       /* Firmware doesn't seem to give us RX packets any more
+          until we send it some command. Might as well update */
+       lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
+                                    0, 0, NULL);
+
+       netif_device_attach(priv->dev);
+       if (priv->mesh_dev)
+               netif_device_attach(priv->mesh_dev);
+
+       lbs_deb_leave(LBS_DEB_FW);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(lbs_resume);
+
 /**
  *  @brief This function downloads firmware image, gets
  *  HW spec from firmware and set basic parameters to
@@ -872,7 +931,6 @@ static int lbs_thread(void *data)
 static int lbs_setup_firmware(struct lbs_private *priv)
 {
        int ret = -1;
-       struct cmd_ds_mesh_access mesh_access;
 
        lbs_deb_enter(LBS_DEB_FW);
 
@@ -880,42 +938,20 @@ static int lbs_setup_firmware(struct lbs_private *priv)
         * Read MAC address from HW
         */
        memset(priv->current_addr, 0xff, ETH_ALEN);
-
-       ret = lbs_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
-                                   0, CMD_OPTION_WAITFORRSP, 0, NULL);
-
+       ret = lbs_update_hw_spec(priv);
        if (ret) {
                ret = -1;
                goto done;
        }
 
-       lbs_set_mac_packet_filter(priv);
-
-       /* Get the supported Data rates */
-       ret = lbs_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
-                                   CMD_ACT_GET_TX_RATE,
-                                   CMD_OPTION_WAITFORRSP, 0, NULL);
+       lbs_set_mac_control(priv);
 
-       if (ret) {
+       ret = lbs_get_data_rate(priv);
+       if (ret < 0) {
                ret = -1;
                goto done;
        }
 
-       /* Disable mesh autostart */
-       if (priv->mesh_dev) {
-               memset(&mesh_access, 0, sizeof(mesh_access));
-               mesh_access.data[0] = cpu_to_le32(0);
-               ret = lbs_prepare_and_send_command(priv,
-                               CMD_MESH_ACCESS,
-                               CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
-                               CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
-               if (ret) {
-                       ret = -1;
-                       goto done;
-               }
-               priv->mesh_autostart_enabled = 0;
-       }
-
        ret = 0;
 done:
        lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
@@ -929,44 +965,44 @@ done:
 static void command_timer_fn(unsigned long data)
 {
        struct lbs_private *priv = (struct lbs_private *)data;
-       struct cmd_ctrl_node *ptempnode;
-       struct cmd_ds_command *cmd;
        unsigned long flags;
 
-       ptempnode = priv->cur_cmd;
-       if (ptempnode == NULL) {
-               lbs_deb_fw("ptempnode empty\n");
-               return;
-       }
+       lbs_deb_enter(LBS_DEB_CMD);
+       spin_lock_irqsave(&priv->driver_lock, flags);
 
-       cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
-       if (!cmd) {
-               lbs_deb_fw("cmd is NULL\n");
-               return;
+       if (!priv->cur_cmd) {
+               lbs_pr_info("Command timer expired; no pending command\n");
+               goto out;
        }
 
-       lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
+       lbs_pr_info("Command %x timed out\n", le16_to_cpu(priv->cur_cmd->cmdbuf->command));
 
-       if (!priv->fw_ready)
-               return;
-
-       spin_lock_irqsave(&priv->driver_lock, flags);
-       priv->cur_cmd = NULL;
+       priv->cmd_timed_out = 1;
+       wake_up_interruptible(&priv->waitq);
+out:
        spin_unlock_irqrestore(&priv->driver_lock, flags);
+       lbs_deb_leave(LBS_DEB_CMD);
+}
 
-       lbs_deb_fw("re-sending same command because of timeout\n");
-       lbs_queue_cmd(priv, ptempnode, 0);
-
-       wake_up_interruptible(&priv->waitq);
+static void lbs_sync_channel_worker(struct work_struct *work)
+{
+       struct lbs_private *priv = container_of(work, struct lbs_private,
+               sync_channel);
 
-       return;
+       lbs_deb_enter(LBS_DEB_MAIN);
+       if (lbs_update_channel(priv))
+               lbs_pr_info("Channel synchronization failed.");
+       lbs_deb_leave(LBS_DEB_MAIN);
 }
 
+
 static int lbs_init_adapter(struct lbs_private *priv)
 {
        size_t bufsize;
        int i, ret = 0;
 
+       lbs_deb_enter(LBS_DEB_MAIN);
+
        /* Allocate buffer to store the BSSID list */
        bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
        priv->networks = kzalloc(bufsize, GFP_KERNEL);
@@ -999,7 +1035,7 @@ static int lbs_init_adapter(struct lbs_private *priv)
        priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
        priv->mode = IW_MODE_INFRA;
        priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
-       priv->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
+       priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
        priv->radioon = RADIO_ON;
        priv->auto_rate = 1;
        priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
@@ -1009,7 +1045,7 @@ static int lbs_init_adapter(struct lbs_private *priv)
        mutex_init(&priv->lock);
 
        setup_timer(&priv->command_timer, command_timer_fn,
-                   (unsigned long)priv);
+               (unsigned long)priv);
 
        INIT_LIST_HEAD(&priv->cmdfreeq);
        INIT_LIST_HEAD(&priv->cmdpendingq);
@@ -1024,20 +1060,21 @@ static int lbs_init_adapter(struct lbs_private *priv)
        }
 
 out:
+       lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
+
        return ret;
 }
 
 static void lbs_free_adapter(struct lbs_private *priv)
 {
-       lbs_deb_fw("free command buffer\n");
-       lbs_free_cmd_buffer(priv);
+       lbs_deb_enter(LBS_DEB_MAIN);
 
-       lbs_deb_fw("free command_timer\n");
+       lbs_free_cmd_buffer(priv);
        del_timer(&priv->command_timer);
-
-       lbs_deb_fw("free scan results table\n");
        kfree(priv->networks);
        priv->networks = NULL;
+
+       lbs_deb_leave(LBS_DEB_MAIN);
 }
 
 /**
@@ -1052,7 +1089,7 @@ struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
        struct net_device *dev = NULL;
        struct lbs_private *priv = NULL;
 
-       lbs_deb_enter(LBS_DEB_NET);
+       lbs_deb_enter(LBS_DEB_MAIN);
 
        /* Allocate an Ethernet device and register it */
        dev = alloc_etherdev(sizeof(struct lbs_private));
@@ -1073,9 +1110,9 @@ struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
        priv->infra_open = 0;
 
        /* Setup the OS Interface to our functions */
-       dev->open = lbs_open;
+       dev->open = lbs_dev_open;
        dev->hard_start_xmit = lbs_hard_start_xmit;
-       dev->stop = lbs_close;
+       dev->stop = lbs_eth_stop;
        dev->set_mac_address = lbs_set_mac_address;
        dev->tx_timeout = lbs_tx_timeout;
        dev->get_stats = lbs_get_stats;
@@ -1102,7 +1139,13 @@ struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
        priv->work_thread = create_singlethread_workqueue("lbs_worker");
        INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
        INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
-       INIT_WORK(&priv->sync_channel, lbs_sync_channel);
+       INIT_WORK(&priv->sync_channel, lbs_sync_channel_worker);
+
+       sprintf(priv->mesh_ssid, "mesh");
+       priv->mesh_ssid_len = 4;
+
+       priv->wol_criteria = 0xffffffff;
+       priv->wol_gpio = 0xff;
 
        goto done;
 
@@ -1112,7 +1155,7 @@ err_init_adapter:
        priv = NULL;
 
 done:
-       lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
+       lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
        return priv;
 }
 EXPORT_SYMBOL_GPL(lbs_add_card);
@@ -1125,10 +1168,10 @@ int lbs_remove_card(struct lbs_private *priv)
 
        lbs_deb_enter(LBS_DEB_MAIN);
 
+       lbs_remove_mesh(priv);
        lbs_remove_rtap(priv);
 
        dev = priv->dev;
-       device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
 
        cancel_delayed_work(&priv->scan_work);
        cancel_delayed_work(&priv->assoc_work);
@@ -1180,6 +1223,37 @@ int lbs_start_card(struct lbs_private *priv)
        if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
                lbs_pr_err("cannot register lbs_rtap attribute\n");
 
+       lbs_update_channel(priv);
+
+       /* 5.0.16p0 is known to NOT support any mesh */
+       if (priv->fwrelease > 0x05001000) {
+               /* Enable mesh, if supported, and work out which TLV it uses.
+                  0x100 + 291 is an unofficial value used in 5.110.20.pXX
+                  0x100 + 37 is the official value used in 5.110.21.pXX
+                  but we check them in that order because 20.pXX doesn't
+                  give an error -- it just silently fails. */
+
+               /* 5.110.20.pXX firmware will fail the command if the channel
+                  doesn't match the existing channel. But only if the TLV
+                  is correct. If the channel is wrong, _BOTH_ versions will
+                  give an error to 0x100+291, and allow 0x100+37 to succeed.
+                  It's just that 5.110.20.pXX will not have done anything
+                  useful */
+
+               priv->mesh_tlv = 0x100 + 291;
+               if (lbs_mesh_config(priv, 1, priv->curbssparams.channel)) {
+                       priv->mesh_tlv = 0x100 + 37;
+                       if (lbs_mesh_config(priv, 1, priv->curbssparams.channel))
+                               priv->mesh_tlv = 0;
+               }
+               if (priv->mesh_tlv) {
+                       lbs_add_mesh(priv);
+
+                       if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
+                               lbs_pr_err("cannot register lbs_mesh attribute\n");
+               }
+       }
+
        lbs_debugfs_init_one(priv, dev);
 
        lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
@@ -1206,10 +1280,14 @@ int lbs_stop_card(struct lbs_private *priv)
        netif_carrier_off(priv->dev);
 
        lbs_debugfs_remove_one(priv);
+       device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
+       if (priv->mesh_tlv)
+               device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
 
        /* Flush pending command nodes */
        spin_lock_irqsave(&priv->driver_lock, flags);
        list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
+               cmdnode->result = -ENOENT;
                cmdnode->cmdwaitqwoken = 1;
                wake_up_interruptible(&cmdnode->cmdwait_q);
        }
@@ -1229,7 +1307,7 @@ EXPORT_SYMBOL_GPL(lbs_stop_card);
  *  @param priv    A pointer to the struct lbs_private structure
  *  @return       0 if successful, -X otherwise
  */
-int lbs_add_mesh(struct lbs_private *priv, struct device *dev)
+static int lbs_add_mesh(struct lbs_private *priv)
 {
        struct net_device *mesh_dev = NULL;
        int ret = 0;
@@ -1245,16 +1323,16 @@ int lbs_add_mesh(struct lbs_private *priv, struct device *dev)
        mesh_dev->priv = priv;
        priv->mesh_dev = mesh_dev;
 
-       mesh_dev->open = lbs_mesh_open;
+       mesh_dev->open = lbs_dev_open;
        mesh_dev->hard_start_xmit = lbs_hard_start_xmit;
-       mesh_dev->stop = lbs_mesh_close;
+       mesh_dev->stop = lbs_mesh_stop;
        mesh_dev->get_stats = lbs_get_stats;
        mesh_dev->set_mac_address = lbs_set_mac_address;
        mesh_dev->ethtool_ops = &lbs_ethtool_ops;
        memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
                        sizeof(priv->dev->dev_addr));
 
-       SET_NETDEV_DEV(priv->mesh_dev, dev);
+       SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
 
 #ifdef WIRELESS_EXT
        mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
@@ -1284,33 +1362,25 @@ done:
        lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
        return ret;
 }
-EXPORT_SYMBOL_GPL(lbs_add_mesh);
 
-
-void lbs_remove_mesh(struct lbs_private *priv)
+static void lbs_remove_mesh(struct lbs_private *priv)
 {
        struct net_device *mesh_dev;
 
-       lbs_deb_enter(LBS_DEB_MAIN);
-
-       if (!priv)
-               goto out;
 
        mesh_dev = priv->mesh_dev;
+       if (!mesh_dev)
+               return;
 
+       lbs_deb_enter(LBS_DEB_MESH);
        netif_stop_queue(mesh_dev);
        netif_carrier_off(priv->mesh_dev);
-
        sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
        unregister_netdev(mesh_dev);
-
-       priv->mesh_dev = NULL ;
+       priv->mesh_dev = NULL;
        free_netdev(mesh_dev);
-
-out:
-       lbs_deb_leave(LBS_DEB_MAIN);
+       lbs_deb_leave(LBS_DEB_MESH);
 }
-EXPORT_SYMBOL_GPL(lbs_remove_mesh);
 
 /**
  *  @brief This function finds the CFP in
@@ -1321,7 +1391,7 @@ EXPORT_SYMBOL_GPL(lbs_remove_mesh);
  *  @param cfp_no  A pointer to CFP number
  *  @return       A pointer to CFP
  */
-struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
+struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
 {
        int i, end;
 
@@ -1355,22 +1425,20 @@ int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
 
        memset(priv->region_channel, 0, sizeof(priv->region_channel));
 
-       {
-               cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
-               if (cfp != NULL) {
-                       priv->region_channel[i].nrcfp = cfp_no;
-                       priv->region_channel[i].CFP = cfp;
-               } else {
-                       lbs_deb_main("wrong region code %#x in band B/G\n",
-                              region);
-                       ret = -1;
-                       goto out;
-               }
-               priv->region_channel[i].valid = 1;
-               priv->region_channel[i].region = region;
-               priv->region_channel[i].band = band;
-               i++;
+       cfp = lbs_get_region_cfp_table(region, &cfp_no);
+       if (cfp != NULL) {
+               priv->region_channel[i].nrcfp = cfp_no;
+               priv->region_channel[i].CFP = cfp;
+       } else {
+               lbs_deb_main("wrong region code %#x in band B/G\n",
+                      region);
+               ret = -1;
+               goto out;
        }
+       priv->region_channel[i].valid = 1;
+       priv->region_channel[i].region = region;
+       priv->region_channel[i].band = band;
+       i++;
 out:
        lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
        return ret;
@@ -1384,44 +1452,20 @@ out:
  *  @param dev     A pointer to net_device structure
  *  @return       n/a
  */
-void lbs_interrupt(struct net_device *dev)
+void lbs_interrupt(struct lbs_private *priv)
 {
-       struct lbs_private *priv = dev->priv;
-
        lbs_deb_enter(LBS_DEB_THREAD);
 
-       lbs_deb_thread("lbs_interrupt: intcounter=%d\n",
-              priv->intcounter);
-
+       lbs_deb_thread("lbs_interrupt: intcounter=%d\n", priv->intcounter);
        priv->intcounter++;
-
-       if (priv->psstate == PS_STATE_SLEEP) {
+       if (priv->psstate == PS_STATE_SLEEP)
                priv->psstate = PS_STATE_AWAKE;
-               netif_wake_queue(dev);
-               if (priv->mesh_dev)
-                       netif_wake_queue(priv->mesh_dev);
-       }
-
        wake_up_interruptible(&priv->waitq);
 
        lbs_deb_leave(LBS_DEB_THREAD);
 }
 EXPORT_SYMBOL_GPL(lbs_interrupt);
 
-int lbs_reset_device(struct lbs_private *priv)
-{
-       int ret;
-
-       lbs_deb_enter(LBS_DEB_MAIN);
-       ret = lbs_prepare_and_send_command(priv, CMD_802_11_RESET,
-                                   CMD_ACT_HALT, 0, 0, NULL);
-       msleep_interruptible(10);
-
-       lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
-       return ret;
-}
-EXPORT_SYMBOL_GPL(lbs_reset_device);
-
 static int __init lbs_init_module(void)
 {
        lbs_deb_enter(LBS_DEB_MAIN);
@@ -1433,9 +1477,7 @@ static int __init lbs_init_module(void)
 static void __exit lbs_exit_module(void)
 {
        lbs_deb_enter(LBS_DEB_MAIN);
-
        lbs_debugfs_remove();
-
        lbs_deb_leave(LBS_DEB_MAIN);
 }
 
@@ -1445,49 +1487,62 @@ static void __exit lbs_exit_module(void)
 
 static int lbs_rtap_open(struct net_device *dev)
 {
-        netif_carrier_off(dev);
-        netif_stop_queue(dev);
-        return 0;
+       /* Yes, _stop_ the queue. Because we don't support injection */
+       lbs_deb_enter(LBS_DEB_MAIN);
+       netif_carrier_off(dev);
+       netif_stop_queue(dev);
+       lbs_deb_leave(LBS_DEB_LEAVE);
+       return 0;
 }
 
 static int lbs_rtap_stop(struct net_device *dev)
 {
-        return 0;
+       lbs_deb_enter(LBS_DEB_MAIN);
+       lbs_deb_leave(LBS_DEB_MAIN);
+       return 0;
 }
 
 static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
 {
-        netif_stop_queue(dev);
-        return -EOPNOTSUPP;
+       netif_stop_queue(dev);
+       return NETDEV_TX_BUSY;
 }
 
 static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
 {
        struct lbs_private *priv = dev->priv;
+       lbs_deb_enter(LBS_DEB_NET);
        return &priv->stats;
 }
 
 
-void lbs_remove_rtap(struct lbs_private *priv)
+static void lbs_remove_rtap(struct lbs_private *priv)
 {
+       lbs_deb_enter(LBS_DEB_MAIN);
        if (priv->rtap_net_dev == NULL)
                return;
        unregister_netdev(priv->rtap_net_dev);
        free_netdev(priv->rtap_net_dev);
        priv->rtap_net_dev = NULL;
+       lbs_deb_leave(LBS_DEB_MAIN);
 }
 
-int lbs_add_rtap(struct lbs_private *priv)
+static int lbs_add_rtap(struct lbs_private *priv)
 {
-       int rc = 0;
+       int ret = 0;
        struct net_device *rtap_dev;
 
-       if (priv->rtap_net_dev)
-               return -EPERM;
+       lbs_deb_enter(LBS_DEB_MAIN);
+       if (priv->rtap_net_dev) {
+               ret = -EPERM;
+               goto out;
+       }
 
        rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
-       if (rtap_dev == NULL)
-               return -ENOMEM;
+       if (rtap_dev == NULL) {
+               ret = -ENOMEM;
+               goto out;
+       }
 
        memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
        rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
@@ -1498,14 +1553,16 @@ int lbs_add_rtap(struct lbs_private *priv)
        rtap_dev->set_multicast_list = lbs_set_multicast_list;
        rtap_dev->priv = priv;
 
-       rc = register_netdev(rtap_dev);
-       if (rc) {
+       ret = register_netdev(rtap_dev);
+       if (ret) {
                free_netdev(rtap_dev);
-               return rc;
+               goto out;
        }
        priv->rtap_net_dev = rtap_dev;
 
-       return 0;
+out:
+       lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
+       return ret;
 }