]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/base/firmware_class.c
firmware loader: fix compile warning
[karo-tx-linux.git] / drivers / base / firmware_class.c
index 4b1f9265887f1048ff5a64cc14fa3d04964b519b..47e3a228072ddc7324a4b5f281230ae34aa8e7f0 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);
 
@@ -446,10 +452,9 @@ 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;
-
+       list_del_init(&buf->pending_list);
        set_bit(FW_STATUS_ABORT, &buf->status);
        complete_all(&buf->completion);
 }
@@ -457,6 +462,25 @@ static void fw_load_abort(struct firmware_priv *fw_priv)
 #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)
@@ -604,6 +628,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;
                }
@@ -612,7 +637,7 @@ static ssize_t firmware_loading_store(struct device *dev,
                dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
                /* fallthrough */
        case -1:
-               fw_load_abort(fw_priv);
+               fw_load_abort(fw_buf);
                break;
        }
 out:
@@ -680,7 +705,7 @@ static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
                new_pages = kmalloc(new_array_size * sizeof(void *),
                                    GFP_KERNEL);
                if (!new_pages) {
-                       fw_load_abort(fw_priv);
+                       fw_load_abort(buf);
                        return -ENOMEM;
                }
                memcpy(new_pages, buf->pages,
@@ -697,7 +722,7 @@ static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
                        alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
 
                if (!buf->pages[buf->nr_pages]) {
-                       fw_load_abort(fw_priv);
+                       fw_load_abort(buf);
                        return -ENOMEM;
                }
                buf->nr_pages++;
@@ -781,7 +806,7 @@ static void firmware_class_timeout_work(struct work_struct *work)
                mutex_unlock(&fw_lock);
                return;
        }
-       fw_load_abort(fw_priv);
+       fw_load_abort(fw_priv->buf);
        mutex_unlock(&fw_lock);
 }
 
@@ -849,6 +874,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,6 +883,10 @@ 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);
@@ -886,6 +916,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 +945,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 +1016,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 +1034,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 +1090,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();
 
@@ -1079,6 +1133,7 @@ request_firmware(const struct firmware **firmware_p, const char *name,
 {
        return _request_firmware(firmware_p, name, device, true, false);
 }
+EXPORT_SYMBOL(request_firmware);
 
 /**
  * release_firmware: - release the resource associated with a firmware image
@@ -1092,6 +1147,7 @@ void release_firmware(const struct firmware *fw)
                kfree(fw);
        }
 }
+EXPORT_SYMBOL(release_firmware);
 
 /* Async support */
 struct firmware_work {
@@ -1172,6 +1228,7 @@ request_firmware_nowait(
        schedule_work(&fw_work->work);
        return 0;
 }
+EXPORT_SYMBOL(request_firmware_nowait);
 
 /**
  * cache_firmware - cache one firmware image in kernel memory space
@@ -1202,6 +1259,7 @@ int cache_firmware(const char *fw_name)
 
        return ret;
 }
+EXPORT_SYMBOL_GPL(cache_firmware);
 
 /**
  * uncache_firmware - remove one cached firmware image
@@ -1232,6 +1290,7 @@ int uncache_firmware(const char *fw_name)
 
        return -EINVAL;
 }
+EXPORT_SYMBOL_GPL(uncache_firmware);
 
 #ifdef CONFIG_PM_SLEEP
 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
@@ -1455,6 +1514,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 +1577,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 +1591,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);