]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/base/firmware_class.c
firmware loader: fix another compile warning with PM_SLEEP unset
[karo-tx-linux.git] / drivers / base / firmware_class.c
index 4b1f9265887f1048ff5a64cc14fa3d04964b519b..a439602ea9194f409286147d4e33b39cc1114cda 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/pm.h>
 #include <linux/suspend.h>
 #include <linux/syscore_ops.h>
+#include <linux/reboot.h>
 
 #include <generated/utsrelease.h>
 
@@ -127,9 +128,11 @@ struct firmware_buf {
        size_t size;
 #ifdef CONFIG_FW_LOADER_USER_HELPER
        bool is_paged_buf;
+       bool need_uevent;
        struct page **pages;
        int nr_pages;
        int page_array_size;
+       struct list_head pending_list;
 #endif
        char fw_id[];
 };
@@ -171,6 +174,9 @@ static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
        strcpy(buf->fw_id, fw_name);
        buf->fwc = fwc;
        init_completion(&buf->completion);
+#ifdef CONFIG_FW_LOADER_USER_HELPER
+       INIT_LIST_HEAD(&buf->pending_list);
+#endif
 
        pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
 
@@ -212,18 +218,6 @@ static int fw_lookup_and_allocate_buf(const char *fw_name,
        return tmp ? 0 : -ENOMEM;
 }
 
-static struct firmware_buf *fw_lookup_buf(const char *fw_name)
-{
-       struct firmware_buf *tmp;
-       struct firmware_cache *fwc = &fw_cache;
-
-       spin_lock(&fwc->lock);
-       tmp = __fw_lookup_buf(fw_name);
-       spin_unlock(&fwc->lock);
-
-       return tmp;
-}
-
 static void __fw_free_buf(struct kref *ref)
 {
        struct firmware_buf *buf = to_fwbuf(ref);
@@ -446,17 +440,52 @@ static struct firmware_priv *to_firmware_priv(struct device *dev)
        return container_of(dev, struct firmware_priv, dev);
 }
 
-static void fw_load_abort(struct firmware_priv *fw_priv)
+static void __fw_load_abort(struct firmware_buf *buf)
 {
-       struct firmware_buf *buf = fw_priv->buf;
+       /*
+        * There is a small window in which user can write to 'loading'
+        * between loading done and disappearance of 'loading'
+        */
+       if (test_bit(FW_STATUS_DONE, &buf->status))
+               return;
 
+       list_del_init(&buf->pending_list);
        set_bit(FW_STATUS_ABORT, &buf->status);
        complete_all(&buf->completion);
 }
 
+static void fw_load_abort(struct firmware_priv *fw_priv)
+{
+       struct firmware_buf *buf = fw_priv->buf;
+
+       __fw_load_abort(buf);
+
+       /* avoid user action after loading abort */
+       fw_priv->buf = NULL;
+}
+
 #define is_fw_load_aborted(buf)        \
        test_bit(FW_STATUS_ABORT, &(buf)->status)
 
+static LIST_HEAD(pending_fw_head);
+
+/* reboot notifier for avoid deadlock with usermode_lock */
+static int fw_shutdown_notify(struct notifier_block *unused1,
+                             unsigned long unused2, void *unused3)
+{
+       mutex_lock(&fw_lock);
+       while (!list_empty(&pending_fw_head))
+               __fw_load_abort(list_first_entry(&pending_fw_head,
+                                              struct firmware_buf,
+                                              pending_list));
+       mutex_unlock(&fw_lock);
+       return NOTIFY_DONE;
+}
+
+static struct notifier_block fw_shutdown_nb = {
+       .notifier_call = fw_shutdown_notify,
+};
+
 static ssize_t firmware_timeout_show(struct class *class,
                                     struct class_attribute *attr,
                                     char *buf)
@@ -499,8 +528,6 @@ static void fw_dev_release(struct device *dev)
        struct firmware_priv *fw_priv = to_firmware_priv(dev);
 
        kfree(fw_priv);
-
-       module_put(THIS_MODULE);
 }
 
 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
@@ -528,7 +555,12 @@ static ssize_t firmware_loading_show(struct device *dev,
                                     struct device_attribute *attr, char *buf)
 {
        struct firmware_priv *fw_priv = to_firmware_priv(dev);
-       int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
+       int loading = 0;
+
+       mutex_lock(&fw_lock);
+       if (fw_priv->buf)
+               loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
+       mutex_unlock(&fw_lock);
 
        return sprintf(buf, "%d\n", loading);
 }
@@ -570,12 +602,12 @@ static ssize_t firmware_loading_store(struct device *dev,
                                      const char *buf, size_t count)
 {
        struct firmware_priv *fw_priv = to_firmware_priv(dev);
-       struct firmware_buf *fw_buf = fw_priv->buf;
+       struct firmware_buf *fw_buf;
        int loading = simple_strtol(buf, NULL, 10);
        int i;
 
        mutex_lock(&fw_lock);
-
+       fw_buf = fw_priv->buf;
        if (!fw_buf)
                goto out;
 
@@ -604,6 +636,7 @@ static ssize_t firmware_loading_store(struct device *dev,
                         * is completed.
                         * */
                        fw_map_pages_buf(fw_buf);
+                       list_del_init(&fw_buf->pending_list);
                        complete_all(&fw_buf->completion);
                        break;
                }
@@ -777,10 +810,6 @@ static void firmware_class_timeout_work(struct work_struct *work)
                        struct firmware_priv, timeout_work.work);
 
        mutex_lock(&fw_lock);
-       if (test_bit(FW_STATUS_DONE, &(fw_priv->buf->status))) {
-               mutex_unlock(&fw_lock);
-               return;
-       }
        fw_load_abort(fw_priv);
        mutex_unlock(&fw_lock);
 }
@@ -827,9 +856,6 @@ static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
 
        dev_set_uevent_suppress(f_dev, true);
 
-       /* Need to pin this module until class device is destroyed */
-       __module_get(THIS_MODULE);
-
        retval = device_add(f_dev);
        if (retval) {
                dev_err(f_dev, "%s: device_register failed\n", __func__);
@@ -849,6 +875,7 @@ static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
        }
 
        if (uevent) {
+               buf->need_uevent = true;
                dev_set_uevent_suppress(f_dev, false);
                dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
                if (timeout != MAX_SCHEDULE_TIMEOUT)
@@ -857,12 +884,14 @@ static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
                kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
        }
 
+       mutex_lock(&fw_lock);
+       list_add(&buf->pending_list, &pending_fw_head);
+       mutex_unlock(&fw_lock);
+
        wait_for_completion(&buf->completion);
 
        cancel_delayed_work_sync(&fw_priv->timeout_work);
 
-       fw_priv->buf = NULL;
-
        device_remove_file(f_dev, &dev_attr_loading);
 err_del_bin_attr:
        device_remove_bin_file(f_dev, &firmware_attr_data);
@@ -886,6 +915,23 @@ static int fw_load_from_user_helper(struct firmware *firmware,
        fw_priv->buf = firmware->priv;
        return _request_firmware_load(fw_priv, uevent, timeout);
 }
+
+#ifdef CONFIG_PM_SLEEP
+/* kill pending requests without uevent to avoid blocking suspend */
+static void kill_requests_without_uevent(void)
+{
+       struct firmware_buf *buf;
+       struct firmware_buf *next;
+
+       mutex_lock(&fw_lock);
+       list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
+               if (!buf->need_uevent)
+                        __fw_load_abort(buf);
+       }
+       mutex_unlock(&fw_lock);
+}
+#endif
+
 #else /* CONFIG_FW_LOADER_USER_HELPER */
 static inline int
 fw_load_from_user_helper(struct firmware *firmware, const char *name,
@@ -898,6 +944,10 @@ fw_load_from_user_helper(struct firmware *firmware, const char *name,
 /* No abort during direct loading */
 #define is_fw_load_aborted(buf) false
 
+#ifdef CONFIG_PM_SLEEP
+static inline void kill_requests_without_uevent(void) { }
+#endif
+
 #endif /* CONFIG_FW_LOADER_USER_HELPER */
 
 
@@ -965,7 +1015,8 @@ _request_firmware_prepare(struct firmware **firmware_p, const char *name,
        return 1; /* need to load */
 }
 
-static int assign_firmware_buf(struct firmware *fw, struct device *device)
+static int assign_firmware_buf(struct firmware *fw, struct device *device,
+                               bool skip_cache)
 {
        struct firmware_buf *buf = fw->priv;
 
@@ -982,7 +1033,7 @@ static int assign_firmware_buf(struct firmware *fw, struct device *device)
         * device may has been deleted already, but the problem
         * should be fixed in devres or driver core.
         */
-       if (device)
+       if (device && !skip_cache)
                fw_add_devm_name(device, buf->fw_id);
 
        /*
@@ -1038,8 +1089,10 @@ _request_firmware(const struct firmware **firmware_p, const char *name,
        if (!fw_get_filesystem_firmware(device, fw->priv))
                ret = fw_load_from_user_helper(fw, name, device,
                                               uevent, nowait, timeout);
+
+       /* don't cache firmware handled without uevent */
        if (!ret)
-               ret = assign_firmware_buf(fw, device);
+               ret = assign_firmware_buf(fw, device, !uevent);
 
        usermodehelper_read_unlock();
 
@@ -1077,8 +1130,15 @@ int
 request_firmware(const struct firmware **firmware_p, const char *name,
                  struct device *device)
 {
-       return _request_firmware(firmware_p, name, device, true, false);
+       int ret;
+
+       /* Need to pin this module until return */
+       __module_get(THIS_MODULE);
+       ret = _request_firmware(firmware_p, name, device, true, false);
+       module_put(THIS_MODULE);
+       return ret;
 }
+EXPORT_SYMBOL(request_firmware);
 
 /**
  * release_firmware: - release the resource associated with a firmware image
@@ -1092,6 +1152,7 @@ void release_firmware(const struct firmware *fw)
                kfree(fw);
        }
 }
+EXPORT_SYMBOL(release_firmware);
 
 /* Async support */
 struct firmware_work {
@@ -1172,6 +1233,10 @@ request_firmware_nowait(
        schedule_work(&fw_work->work);
        return 0;
 }
+EXPORT_SYMBOL(request_firmware_nowait);
+
+#ifdef CONFIG_PM_SLEEP
+static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
 
 /**
  * cache_firmware - cache one firmware image in kernel memory space
@@ -1187,7 +1252,7 @@ request_firmware_nowait(
  * Return !0 otherwise
  *
  */
-int cache_firmware(const char *fw_name)
+static int cache_firmware(const char *fw_name)
 {
        int ret;
        const struct firmware *fw;
@@ -1203,6 +1268,18 @@ int cache_firmware(const char *fw_name)
        return ret;
 }
 
+static struct firmware_buf *fw_lookup_buf(const char *fw_name)
+{
+       struct firmware_buf *tmp;
+       struct firmware_cache *fwc = &fw_cache;
+
+       spin_lock(&fwc->lock);
+       tmp = __fw_lookup_buf(fw_name);
+       spin_unlock(&fwc->lock);
+
+       return tmp;
+}
+
 /**
  * uncache_firmware - remove one cached firmware image
  * @fw_name: the firmware image name
@@ -1214,7 +1291,7 @@ int cache_firmware(const char *fw_name)
  * Return !0 otherwise
  *
  */
-int uncache_firmware(const char *fw_name)
+static int uncache_firmware(const char *fw_name)
 {
        struct firmware_buf *buf;
        struct firmware fw;
@@ -1233,9 +1310,6 @@ int uncache_firmware(const char *fw_name)
        return -EINVAL;
 }
 
-#ifdef CONFIG_PM_SLEEP
-static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
-
 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
 {
        struct fw_cache_entry *fce;
@@ -1455,6 +1529,7 @@ static int fw_pm_notify(struct notifier_block *notify_block,
        switch (mode) {
        case PM_HIBERNATION_PREPARE:
        case PM_SUSPEND_PREPARE:
+               kill_requests_without_uevent();
                device_cache_fw_images();
                break;
 
@@ -1517,6 +1592,7 @@ static int __init firmware_class_init(void)
 {
        fw_cache_init();
 #ifdef CONFIG_FW_LOADER_USER_HELPER
+       register_reboot_notifier(&fw_shutdown_nb);
        return class_register(&firmware_class);
 #else
        return 0;
@@ -1530,15 +1606,10 @@ static void __exit firmware_class_exit(void)
        unregister_pm_notifier(&fw_cache.pm_notify);
 #endif
 #ifdef CONFIG_FW_LOADER_USER_HELPER
+       unregister_reboot_notifier(&fw_shutdown_nb);
        class_unregister(&firmware_class);
 #endif
 }
 
 fs_initcall(firmware_class_init);
 module_exit(firmware_class_exit);
-
-EXPORT_SYMBOL(release_firmware);
-EXPORT_SYMBOL(request_firmware);
-EXPORT_SYMBOL(request_firmware_nowait);
-EXPORT_SYMBOL_GPL(cache_firmware);
-EXPORT_SYMBOL_GPL(uncache_firmware);