libertas: add asynchronous firmware loading capability
authorDaniel Drake <dsd@laptop.org>
Mon, 16 Apr 2012 22:53:26 +0000 (23:53 +0100)
committerJohn W. Linville <linville@tuxdriver.com>
Tue, 17 Apr 2012 18:57:14 +0000 (14:57 -0400)
As described at
http://article.gmane.org/gmane.linux.kernel.wireless.general/86084
libertas is taking a long time to load because it loads firmware
during module loading.

Add a new API for interface drivers to load their firmware
asynchronously. The same semantics of the firmware table are followed
like before.

Interface drivers will be converted in follow-up patches, then we can
remove the old, synchronous firmware loading function.

Signed-off-by: Daniel Drake <dsd@laptop.org>
Acked-by: Dan Williams <dcbw@redhat.com>
Signed-off-by: John W. Linville <linville@tuxdriver.com>
drivers/net/wireless/libertas/decl.h
drivers/net/wireless/libertas/dev.h
drivers/net/wireless/libertas/firmware.c
drivers/net/wireless/libertas/main.c

index 2fb2e31733eeb7c3c51569cfb8a273f3e87b3242..84a3aa7ac5705c300e11fad5df4cccde1405101d 100644 (file)
@@ -18,6 +18,10 @@ struct lbs_fw_table {
        const char *fwname;
 };
 
+struct lbs_private;
+typedef void (*lbs_fw_cb)(struct lbs_private *priv, int ret,
+               const struct firmware *helper, const struct firmware *mainfw);
+
 struct lbs_private;
 struct sk_buff;
 struct net_device;
@@ -70,5 +74,9 @@ int lbs_get_firmware(struct device *dev, u32 card_model,
                        const struct lbs_fw_table *fw_table,
                        const struct firmware **helper,
                        const struct firmware **mainfw);
+int lbs_get_firmware_async(struct lbs_private *priv, struct device *device,
+                          u32 card_model, const struct lbs_fw_table *fw_table,
+                          lbs_fw_cb callback);
+void lbs_wait_for_firmware_load(struct lbs_private *priv);
 
 #endif
index f3fd447131c2c9d20ffa84b52a5ffff20ee7f233..672005430acab75e0b650bda1a490245e79d2167 100644 (file)
@@ -7,6 +7,7 @@
 #define _LBS_DEV_H_
 
 #include "defs.h"
+#include "decl.h"
 #include "host.h"
 
 #include <linux/kfifo.h>
@@ -180,6 +181,15 @@ struct lbs_private {
        wait_queue_head_t scan_q;
        /* Whether the scan was initiated internally and not by cfg80211 */
        bool internal_scan;
+
+       /* Firmware load */
+       u32 fw_model;
+       wait_queue_head_t fw_waitq;
+       struct device *fw_device;
+       const struct firmware *helper_fw;
+       const struct lbs_fw_table *fw_table;
+       const struct lbs_fw_table *fw_iter;
+       lbs_fw_cb fw_callback;
 };
 
 extern struct cmd_confirm_sleep confirm_sleep;
index 0c8c845b4901269b8ac50f70365741beece7b505..cd23f1a8c98aaa830851b61293d414116bd53cce 100644 (file)
  * Firmware loading and handling functions.
  */
 
+#include <linux/firmware.h>
 #include <linux/firmware.h>
 #include <linux/module.h>
 
+#include "dev.h"
 #include "decl.h"
 
+static void load_next_firmware_from_table(struct lbs_private *private);
+
+static void lbs_fw_loaded(struct lbs_private *priv, int ret,
+       const struct firmware *helper, const struct firmware *mainfw)
+{
+       unsigned long flags;
+
+       lbs_deb_fw("firmware load complete, code %d\n", ret);
+
+       /* User must free helper/mainfw */
+       priv->fw_callback(priv, ret, helper, mainfw);
+
+       spin_lock_irqsave(&priv->driver_lock, flags);
+       priv->fw_callback = NULL;
+       wake_up(&priv->fw_waitq);
+       spin_unlock_irqrestore(&priv->driver_lock, flags);
+}
+
+static void do_load_firmware(struct lbs_private *priv, const char *name,
+       void (*cb)(const struct firmware *fw, void *context))
+{
+       int ret;
+
+       lbs_deb_fw("Requesting %s\n", name);
+       ret = request_firmware_nowait(THIS_MODULE, true, name,
+                       priv->fw_device, GFP_KERNEL, priv, cb);
+       if (ret) {
+               lbs_deb_fw("request_firmware_nowait error %d\n", ret);
+               lbs_fw_loaded(priv, ret, NULL, NULL);
+       }
+}
+
+static void main_firmware_cb(const struct firmware *firmware, void *context)
+{
+       struct lbs_private *priv = context;
+
+       if (!firmware) {
+               /* Failed to find firmware: try next table entry */
+               load_next_firmware_from_table(priv);
+               return;
+       }
+
+       /* Firmware found! */
+       lbs_fw_loaded(priv, 0, priv->helper_fw, firmware);
+}
+
+static void helper_firmware_cb(const struct firmware *firmware, void *context)
+{
+       struct lbs_private *priv = context;
+
+       if (!firmware) {
+               /* Failed to find firmware: try next table entry */
+               load_next_firmware_from_table(priv);
+               return;
+       }
+
+       /* Firmware found! */
+       if (priv->fw_iter->fwname) {
+               priv->helper_fw = firmware;
+               do_load_firmware(priv, priv->fw_iter->fwname, main_firmware_cb);
+       } else {
+               /* No main firmware needed for this helper --> success! */
+               lbs_fw_loaded(priv, 0, firmware, NULL);
+       }
+}
+
+static void load_next_firmware_from_table(struct lbs_private *priv)
+{
+       const struct lbs_fw_table *iter;
+
+       if (!priv->fw_iter)
+               iter = priv->fw_table;
+       else
+               iter = ++priv->fw_iter;
+
+       if (priv->helper_fw) {
+               release_firmware(priv->helper_fw);
+               priv->helper_fw = NULL;
+       }
+
+next:
+       if (!iter->helper) {
+               /* End of table hit. */
+               lbs_fw_loaded(priv, -ENOENT, NULL, NULL);
+               return;
+       }
+
+       if (iter->model != priv->fw_model) {
+               iter++;
+               goto next;
+       }
+
+       priv->fw_iter = iter;
+       do_load_firmware(priv, iter->helper, helper_firmware_cb);
+}
+
+void lbs_wait_for_firmware_load(struct lbs_private *priv)
+{
+       wait_event(priv->fw_waitq, priv->fw_callback == NULL);
+}
+
+/**
+ *  lbs_get_firmware_async - Retrieves firmware asynchronously. Can load
+ *  either a helper firmware and a main firmware (2-stage), or just the helper.
+ *
+ *  @priv:      Pointer to lbs_private instance
+ *  @dev:      A pointer to &device structure
+ *  @card_model: Bus-specific card model ID used to filter firmware table
+ *             elements
+ *  @fw_table: Table of firmware file names and device model numbers
+ *             terminated by an entry with a NULL helper name
+ *     @callback: User callback to invoke when firmware load succeeds or fails.
+ */
+int lbs_get_firmware_async(struct lbs_private *priv, struct device *device,
+                           u32 card_model, const struct lbs_fw_table *fw_table,
+                           lbs_fw_cb callback)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&priv->driver_lock, flags);
+       if (priv->fw_callback) {
+               lbs_deb_fw("firmware load already in progress\n");
+               spin_unlock_irqrestore(&priv->driver_lock, flags);
+               return -EBUSY;
+       }
+
+       priv->fw_device = device;
+       priv->fw_callback = callback;
+       priv->fw_table = fw_table;
+       priv->fw_iter = NULL;
+       priv->fw_model = card_model;
+       spin_unlock_irqrestore(&priv->driver_lock, flags);
+
+       lbs_deb_fw("Starting async firmware load\n");
+       load_next_firmware_from_table(priv);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(lbs_get_firmware_async);
+
 /**
  *  lbs_get_firmware - Retrieves two-stage firmware
  *
  *  @helper:   On success, the helper firmware; caller must free
  *  @mainfw:   On success, the main firmware; caller must free
  *
+ * Deprecated: use lbs_get_firmware_async() instead.
+ *
  *  returns:           0 on success, non-zero on failure
  */
 int lbs_get_firmware(struct device *dev, u32 card_model,
index 7eaf992775ae7e0e23d748685b7658e2121e65b4..e96ee0aa8439420954b4f78a8571ab161eb50145 100644 (file)
@@ -878,6 +878,7 @@ static int lbs_init_adapter(struct lbs_private *priv)
        priv->is_host_sleep_configured = 0;
        priv->is_host_sleep_activated = 0;
        init_waitqueue_head(&priv->host_sleep_q);
+       init_waitqueue_head(&priv->fw_waitq);
        mutex_init(&priv->lock);
 
        setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
@@ -1037,6 +1038,8 @@ void lbs_remove_card(struct lbs_private *priv)
        if (priv->wiphy_registered)
                lbs_scan_deinit(priv);
 
+       lbs_wait_for_firmware_load(priv);
+
        /* worker thread destruction blocks on the in-flight command which
         * should have been cleared already in lbs_stop_card().
         */