sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / base / firmware_class.c
blob4497d263209fb861e08e1650ddf654a98f2102e3
1 /*
2 * firmware_class.c - Multi purpose firmware loading support
4 * Copyright (c) 2003 Manuel Estrada Sainz
6 * Please see Documentation/firmware_class/ for more information.
8 */
10 #include <linux/capability.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/timer.h>
15 #include <linux/vmalloc.h>
16 #include <linux/interrupt.h>
17 #include <linux/bitops.h>
18 #include <linux/mutex.h>
19 #include <linux/workqueue.h>
20 #include <linux/highmem.h>
21 #include <linux/firmware.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/file.h>
25 #include <linux/list.h>
26 #include <linux/fs.h>
27 #include <linux/async.h>
28 #include <linux/pm.h>
29 #include <linux/suspend.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/reboot.h>
32 #include <linux/security.h>
33 #include <linux/swait.h>
35 #include <generated/utsrelease.h>
37 #include "base.h"
39 MODULE_AUTHOR("Manuel Estrada Sainz");
40 MODULE_DESCRIPTION("Multi purpose firmware loading support");
41 MODULE_LICENSE("GPL");
43 /* Builtin firmware support */
45 #ifdef CONFIG_FW_LOADER
47 extern struct builtin_fw __start_builtin_fw[];
48 extern struct builtin_fw __end_builtin_fw[];
50 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
51 void *buf, size_t size)
53 struct builtin_fw *b_fw;
55 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
56 if (strcmp(name, b_fw->name) == 0) {
57 fw->size = b_fw->size;
58 fw->data = b_fw->data;
60 if (buf && fw->size <= size)
61 memcpy(buf, fw->data, fw->size);
62 return true;
66 return false;
69 static bool fw_is_builtin_firmware(const struct firmware *fw)
71 struct builtin_fw *b_fw;
73 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
74 if (fw->data == b_fw->data)
75 return true;
77 return false;
80 #else /* Module case - no builtin firmware support */
82 static inline bool fw_get_builtin_firmware(struct firmware *fw,
83 const char *name, void *buf,
84 size_t size)
86 return false;
89 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
91 return false;
93 #endif
95 enum fw_status {
96 FW_STATUS_UNKNOWN,
97 FW_STATUS_LOADING,
98 FW_STATUS_DONE,
99 FW_STATUS_ABORTED,
102 static int loading_timeout = 60; /* In seconds */
104 static inline long firmware_loading_timeout(void)
106 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
110 * Concurrent request_firmware() for the same firmware need to be
111 * serialized. struct fw_state is simple state machine which hold the
112 * state of the firmware loading.
114 struct fw_state {
115 struct swait_queue_head wq;
116 enum fw_status status;
119 static void fw_state_init(struct fw_state *fw_st)
121 init_swait_queue_head(&fw_st->wq);
122 fw_st->status = FW_STATUS_UNKNOWN;
125 static inline bool __fw_state_is_done(enum fw_status status)
127 return status == FW_STATUS_DONE || status == FW_STATUS_ABORTED;
130 static int __fw_state_wait_common(struct fw_state *fw_st, long timeout)
132 long ret;
134 ret = swait_event_interruptible_timeout(fw_st->wq,
135 __fw_state_is_done(READ_ONCE(fw_st->status)),
136 timeout);
137 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
138 return -ENOENT;
139 if (!ret)
140 return -ETIMEDOUT;
142 return ret < 0 ? ret : 0;
145 static void __fw_state_set(struct fw_state *fw_st,
146 enum fw_status status)
148 WRITE_ONCE(fw_st->status, status);
150 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
151 swake_up(&fw_st->wq);
154 #define fw_state_start(fw_st) \
155 __fw_state_set(fw_st, FW_STATUS_LOADING)
156 #define fw_state_done(fw_st) \
157 __fw_state_set(fw_st, FW_STATUS_DONE)
158 #define fw_state_wait(fw_st) \
159 __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT)
161 #ifndef CONFIG_FW_LOADER_USER_HELPER
163 #define fw_state_is_aborted(fw_st) false
165 #else /* CONFIG_FW_LOADER_USER_HELPER */
167 static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
169 return fw_st->status == status;
172 #define fw_state_aborted(fw_st) \
173 __fw_state_set(fw_st, FW_STATUS_ABORTED)
174 #define fw_state_is_done(fw_st) \
175 __fw_state_check(fw_st, FW_STATUS_DONE)
176 #define fw_state_is_loading(fw_st) \
177 __fw_state_check(fw_st, FW_STATUS_LOADING)
178 #define fw_state_is_aborted(fw_st) \
179 __fw_state_check(fw_st, FW_STATUS_ABORTED)
180 #define fw_state_wait_timeout(fw_st, timeout) \
181 __fw_state_wait_common(fw_st, timeout)
183 #endif /* CONFIG_FW_LOADER_USER_HELPER */
185 /* firmware behavior options */
186 #define FW_OPT_UEVENT (1U << 0)
187 #define FW_OPT_NOWAIT (1U << 1)
188 #ifdef CONFIG_FW_LOADER_USER_HELPER
189 #define FW_OPT_USERHELPER (1U << 2)
190 #else
191 #define FW_OPT_USERHELPER 0
192 #endif
193 #ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
194 #define FW_OPT_FALLBACK FW_OPT_USERHELPER
195 #else
196 #define FW_OPT_FALLBACK 0
197 #endif
198 #define FW_OPT_NO_WARN (1U << 3)
199 #define FW_OPT_NOCACHE (1U << 4)
201 struct firmware_cache {
202 /* firmware_buf instance will be added into the below list */
203 spinlock_t lock;
204 struct list_head head;
205 int state;
207 #ifdef CONFIG_PM_SLEEP
209 * Names of firmware images which have been cached successfully
210 * will be added into the below list so that device uncache
211 * helper can trace which firmware images have been cached
212 * before.
214 spinlock_t name_lock;
215 struct list_head fw_names;
217 struct delayed_work work;
219 struct notifier_block pm_notify;
220 #endif
223 struct firmware_buf {
224 struct kref ref;
225 struct list_head list;
226 struct firmware_cache *fwc;
227 struct fw_state fw_st;
228 void *data;
229 size_t size;
230 size_t allocated_size;
231 #ifdef CONFIG_FW_LOADER_USER_HELPER
232 bool is_paged_buf;
233 bool need_uevent;
234 struct page **pages;
235 int nr_pages;
236 int page_array_size;
237 struct list_head pending_list;
238 #endif
239 const char *fw_id;
242 struct fw_cache_entry {
243 struct list_head list;
244 const char *name;
247 struct fw_name_devm {
248 unsigned long magic;
249 const char *name;
252 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
254 #define FW_LOADER_NO_CACHE 0
255 #define FW_LOADER_START_CACHE 1
257 static int fw_cache_piggyback_on_request(const char *name);
259 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
260 * guarding for corner cases a global lock should be OK */
261 static DEFINE_MUTEX(fw_lock);
263 static struct firmware_cache fw_cache;
265 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
266 struct firmware_cache *fwc,
267 void *dbuf, size_t size)
269 struct firmware_buf *buf;
271 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
272 if (!buf)
273 return NULL;
275 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
276 if (!buf->fw_id) {
277 kfree(buf);
278 return NULL;
281 kref_init(&buf->ref);
282 buf->fwc = fwc;
283 buf->data = dbuf;
284 buf->allocated_size = size;
285 fw_state_init(&buf->fw_st);
286 #ifdef CONFIG_FW_LOADER_USER_HELPER
287 INIT_LIST_HEAD(&buf->pending_list);
288 #endif
290 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
292 return buf;
295 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
297 struct firmware_buf *tmp;
298 struct firmware_cache *fwc = &fw_cache;
300 list_for_each_entry(tmp, &fwc->head, list)
301 if (!strcmp(tmp->fw_id, fw_name))
302 return tmp;
303 return NULL;
306 static int fw_lookup_and_allocate_buf(const char *fw_name,
307 struct firmware_cache *fwc,
308 struct firmware_buf **buf, void *dbuf,
309 size_t size)
311 struct firmware_buf *tmp;
313 spin_lock(&fwc->lock);
314 tmp = __fw_lookup_buf(fw_name);
315 if (tmp) {
316 kref_get(&tmp->ref);
317 spin_unlock(&fwc->lock);
318 *buf = tmp;
319 return 1;
321 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
322 if (tmp)
323 list_add(&tmp->list, &fwc->head);
324 spin_unlock(&fwc->lock);
326 *buf = tmp;
328 return tmp ? 0 : -ENOMEM;
331 static void __fw_free_buf(struct kref *ref)
332 __releases(&fwc->lock)
334 struct firmware_buf *buf = to_fwbuf(ref);
335 struct firmware_cache *fwc = buf->fwc;
337 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
338 __func__, buf->fw_id, buf, buf->data,
339 (unsigned int)buf->size);
341 list_del(&buf->list);
342 spin_unlock(&fwc->lock);
344 #ifdef CONFIG_FW_LOADER_USER_HELPER
345 if (buf->is_paged_buf) {
346 int i;
347 vunmap(buf->data);
348 for (i = 0; i < buf->nr_pages; i++)
349 __free_page(buf->pages[i]);
350 vfree(buf->pages);
351 } else
352 #endif
353 if (!buf->allocated_size)
354 vfree(buf->data);
355 kfree_const(buf->fw_id);
356 kfree(buf);
359 static void fw_free_buf(struct firmware_buf *buf)
361 struct firmware_cache *fwc = buf->fwc;
362 spin_lock(&fwc->lock);
363 if (!kref_put(&buf->ref, __fw_free_buf))
364 spin_unlock(&fwc->lock);
367 /* direct firmware loading support */
368 static char fw_path_para[256];
369 static const char * const fw_path[] = {
370 fw_path_para,
371 "/lib/firmware/updates/" UTS_RELEASE,
372 "/lib/firmware/updates",
373 "/lib/firmware/" UTS_RELEASE,
374 "/lib/firmware"
378 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
379 * from kernel command line because firmware_class is generally built in
380 * kernel instead of module.
382 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
383 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
385 static int
386 fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
388 loff_t size;
389 int i, len;
390 int rc = -ENOENT;
391 char *path;
392 enum kernel_read_file_id id = READING_FIRMWARE;
393 size_t msize = INT_MAX;
395 /* Already populated data member means we're loading into a buffer */
396 if (buf->data) {
397 id = READING_FIRMWARE_PREALLOC_BUFFER;
398 msize = buf->allocated_size;
401 path = __getname();
402 if (!path)
403 return -ENOMEM;
405 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
406 /* skip the unset customized path */
407 if (!fw_path[i][0])
408 continue;
410 len = snprintf(path, PATH_MAX, "%s/%s",
411 fw_path[i], buf->fw_id);
412 if (len >= PATH_MAX) {
413 rc = -ENAMETOOLONG;
414 break;
417 buf->size = 0;
418 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
419 id);
420 if (rc) {
421 if (rc == -ENOENT)
422 dev_dbg(device, "loading %s failed with error %d\n",
423 path, rc);
424 else
425 dev_warn(device, "loading %s failed with error %d\n",
426 path, rc);
427 continue;
429 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
430 buf->size = size;
431 fw_state_done(&buf->fw_st);
432 break;
434 __putname(path);
436 return rc;
439 /* firmware holds the ownership of pages */
440 static void firmware_free_data(const struct firmware *fw)
442 /* Loaded directly? */
443 if (!fw->priv) {
444 vfree(fw->data);
445 return;
447 fw_free_buf(fw->priv);
450 /* store the pages buffer info firmware from buf */
451 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
453 fw->priv = buf;
454 #ifdef CONFIG_FW_LOADER_USER_HELPER
455 fw->pages = buf->pages;
456 #endif
457 fw->size = buf->size;
458 fw->data = buf->data;
460 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
461 __func__, buf->fw_id, buf, buf->data,
462 (unsigned int)buf->size);
465 #ifdef CONFIG_PM_SLEEP
466 static void fw_name_devm_release(struct device *dev, void *res)
468 struct fw_name_devm *fwn = res;
470 if (fwn->magic == (unsigned long)&fw_cache)
471 pr_debug("%s: fw_name-%s devm-%p released\n",
472 __func__, fwn->name, res);
473 kfree_const(fwn->name);
476 static int fw_devm_match(struct device *dev, void *res,
477 void *match_data)
479 struct fw_name_devm *fwn = res;
481 return (fwn->magic == (unsigned long)&fw_cache) &&
482 !strcmp(fwn->name, match_data);
485 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
486 const char *name)
488 struct fw_name_devm *fwn;
490 fwn = devres_find(dev, fw_name_devm_release,
491 fw_devm_match, (void *)name);
492 return fwn;
495 /* add firmware name into devres list */
496 static int fw_add_devm_name(struct device *dev, const char *name)
498 struct fw_name_devm *fwn;
500 fwn = fw_find_devm_name(dev, name);
501 if (fwn)
502 return 1;
504 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
505 GFP_KERNEL);
506 if (!fwn)
507 return -ENOMEM;
508 fwn->name = kstrdup_const(name, GFP_KERNEL);
509 if (!fwn->name) {
510 devres_free(fwn);
511 return -ENOMEM;
514 fwn->magic = (unsigned long)&fw_cache;
515 devres_add(dev, fwn);
517 return 0;
519 #else
520 static int fw_add_devm_name(struct device *dev, const char *name)
522 return 0;
524 #endif
528 * user-mode helper code
530 #ifdef CONFIG_FW_LOADER_USER_HELPER
531 struct firmware_priv {
532 bool nowait;
533 struct device dev;
534 struct firmware_buf *buf;
535 struct firmware *fw;
538 static struct firmware_priv *to_firmware_priv(struct device *dev)
540 return container_of(dev, struct firmware_priv, dev);
543 static void __fw_load_abort(struct firmware_buf *buf)
546 * There is a small window in which user can write to 'loading'
547 * between loading done and disappearance of 'loading'
549 if (fw_state_is_done(&buf->fw_st))
550 return;
552 list_del_init(&buf->pending_list);
553 fw_state_aborted(&buf->fw_st);
556 static void fw_load_abort(struct firmware_priv *fw_priv)
558 struct firmware_buf *buf = fw_priv->buf;
560 __fw_load_abort(buf);
562 /* avoid user action after loading abort */
563 fw_priv->buf = NULL;
566 static LIST_HEAD(pending_fw_head);
568 /* reboot notifier for avoid deadlock with usermode_lock */
569 static int fw_shutdown_notify(struct notifier_block *unused1,
570 unsigned long unused2, void *unused3)
572 mutex_lock(&fw_lock);
573 while (!list_empty(&pending_fw_head))
574 __fw_load_abort(list_first_entry(&pending_fw_head,
575 struct firmware_buf,
576 pending_list));
577 mutex_unlock(&fw_lock);
578 return NOTIFY_DONE;
581 static struct notifier_block fw_shutdown_nb = {
582 .notifier_call = fw_shutdown_notify,
585 static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
586 char *buf)
588 return sprintf(buf, "%d\n", loading_timeout);
592 * firmware_timeout_store - set number of seconds to wait for firmware
593 * @class: device class pointer
594 * @attr: device attribute pointer
595 * @buf: buffer to scan for timeout value
596 * @count: number of bytes in @buf
598 * Sets the number of seconds to wait for the firmware. Once
599 * this expires an error will be returned to the driver and no
600 * firmware will be provided.
602 * Note: zero means 'wait forever'.
604 static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
605 const char *buf, size_t count)
607 loading_timeout = simple_strtol(buf, NULL, 10);
608 if (loading_timeout < 0)
609 loading_timeout = 0;
611 return count;
613 static CLASS_ATTR_RW(timeout);
615 static struct attribute *firmware_class_attrs[] = {
616 &class_attr_timeout.attr,
617 NULL,
619 ATTRIBUTE_GROUPS(firmware_class);
621 static void fw_dev_release(struct device *dev)
623 struct firmware_priv *fw_priv = to_firmware_priv(dev);
625 kfree(fw_priv);
628 static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
630 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
631 return -ENOMEM;
632 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
633 return -ENOMEM;
634 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
635 return -ENOMEM;
637 return 0;
640 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
642 struct firmware_priv *fw_priv = to_firmware_priv(dev);
643 int err = 0;
645 mutex_lock(&fw_lock);
646 if (fw_priv->buf)
647 err = do_firmware_uevent(fw_priv, env);
648 mutex_unlock(&fw_lock);
649 return err;
652 static struct class firmware_class = {
653 .name = "firmware",
654 .class_groups = firmware_class_groups,
655 .dev_uevent = firmware_uevent,
656 .dev_release = fw_dev_release,
659 static ssize_t firmware_loading_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
662 struct firmware_priv *fw_priv = to_firmware_priv(dev);
663 int loading = 0;
665 mutex_lock(&fw_lock);
666 if (fw_priv->buf)
667 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
668 mutex_unlock(&fw_lock);
670 return sprintf(buf, "%d\n", loading);
673 /* Some architectures don't have PAGE_KERNEL_RO */
674 #ifndef PAGE_KERNEL_RO
675 #define PAGE_KERNEL_RO PAGE_KERNEL
676 #endif
678 /* one pages buffer should be mapped/unmapped only once */
679 static int fw_map_pages_buf(struct firmware_buf *buf)
681 if (!buf->is_paged_buf)
682 return 0;
684 vunmap(buf->data);
685 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
686 if (!buf->data)
687 return -ENOMEM;
688 return 0;
692 * firmware_loading_store - set value in the 'loading' control file
693 * @dev: device pointer
694 * @attr: device attribute pointer
695 * @buf: buffer to scan for loading control value
696 * @count: number of bytes in @buf
698 * The relevant values are:
700 * 1: Start a load, discarding any previous partial load.
701 * 0: Conclude the load and hand the data to the driver code.
702 * -1: Conclude the load with an error and discard any written data.
704 static ssize_t firmware_loading_store(struct device *dev,
705 struct device_attribute *attr,
706 const char *buf, size_t count)
708 struct firmware_priv *fw_priv = to_firmware_priv(dev);
709 struct firmware_buf *fw_buf;
710 ssize_t written = count;
711 int loading = simple_strtol(buf, NULL, 10);
712 int i;
714 mutex_lock(&fw_lock);
715 fw_buf = fw_priv->buf;
716 if (!fw_buf)
717 goto out;
719 switch (loading) {
720 case 1:
721 /* discarding any previous partial load */
722 if (!fw_state_is_done(&fw_buf->fw_st)) {
723 for (i = 0; i < fw_buf->nr_pages; i++)
724 __free_page(fw_buf->pages[i]);
725 vfree(fw_buf->pages);
726 fw_buf->pages = NULL;
727 fw_buf->page_array_size = 0;
728 fw_buf->nr_pages = 0;
729 fw_state_start(&fw_buf->fw_st);
731 break;
732 case 0:
733 if (fw_state_is_loading(&fw_buf->fw_st)) {
734 int rc;
737 * Several loading requests may be pending on
738 * one same firmware buf, so let all requests
739 * see the mapped 'buf->data' once the loading
740 * is completed.
741 * */
742 rc = fw_map_pages_buf(fw_buf);
743 if (rc)
744 dev_err(dev, "%s: map pages failed\n",
745 __func__);
746 else
747 rc = security_kernel_post_read_file(NULL,
748 fw_buf->data, fw_buf->size,
749 READING_FIRMWARE);
752 * Same logic as fw_load_abort, only the DONE bit
753 * is ignored and we set ABORT only on failure.
755 list_del_init(&fw_buf->pending_list);
756 if (rc) {
757 fw_state_aborted(&fw_buf->fw_st);
758 written = rc;
759 } else {
760 fw_state_done(&fw_buf->fw_st);
762 break;
764 /* fallthrough */
765 default:
766 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
767 /* fallthrough */
768 case -1:
769 fw_load_abort(fw_priv);
770 break;
772 out:
773 mutex_unlock(&fw_lock);
774 return written;
777 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
779 static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
780 loff_t offset, size_t count, bool read)
782 if (read)
783 memcpy(buffer, buf->data + offset, count);
784 else
785 memcpy(buf->data + offset, buffer, count);
788 static void firmware_rw(struct firmware_buf *buf, char *buffer,
789 loff_t offset, size_t count, bool read)
791 while (count) {
792 void *page_data;
793 int page_nr = offset >> PAGE_SHIFT;
794 int page_ofs = offset & (PAGE_SIZE-1);
795 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
797 page_data = kmap(buf->pages[page_nr]);
799 if (read)
800 memcpy(buffer, page_data + page_ofs, page_cnt);
801 else
802 memcpy(page_data + page_ofs, buffer, page_cnt);
804 kunmap(buf->pages[page_nr]);
805 buffer += page_cnt;
806 offset += page_cnt;
807 count -= page_cnt;
811 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
812 struct bin_attribute *bin_attr,
813 char *buffer, loff_t offset, size_t count)
815 struct device *dev = kobj_to_dev(kobj);
816 struct firmware_priv *fw_priv = to_firmware_priv(dev);
817 struct firmware_buf *buf;
818 ssize_t ret_count;
820 mutex_lock(&fw_lock);
821 buf = fw_priv->buf;
822 if (!buf || fw_state_is_done(&buf->fw_st)) {
823 ret_count = -ENODEV;
824 goto out;
826 if (offset > buf->size) {
827 ret_count = 0;
828 goto out;
830 if (count > buf->size - offset)
831 count = buf->size - offset;
833 ret_count = count;
835 if (buf->data)
836 firmware_rw_buf(buf, buffer, offset, count, true);
837 else
838 firmware_rw(buf, buffer, offset, count, true);
840 out:
841 mutex_unlock(&fw_lock);
842 return ret_count;
845 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
847 struct firmware_buf *buf = fw_priv->buf;
848 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
850 /* If the array of pages is too small, grow it... */
851 if (buf->page_array_size < pages_needed) {
852 int new_array_size = max(pages_needed,
853 buf->page_array_size * 2);
854 struct page **new_pages;
856 new_pages = vmalloc(new_array_size * sizeof(void *));
857 if (!new_pages) {
858 fw_load_abort(fw_priv);
859 return -ENOMEM;
861 memcpy(new_pages, buf->pages,
862 buf->page_array_size * sizeof(void *));
863 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
864 (new_array_size - buf->page_array_size));
865 vfree(buf->pages);
866 buf->pages = new_pages;
867 buf->page_array_size = new_array_size;
870 while (buf->nr_pages < pages_needed) {
871 buf->pages[buf->nr_pages] =
872 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
874 if (!buf->pages[buf->nr_pages]) {
875 fw_load_abort(fw_priv);
876 return -ENOMEM;
878 buf->nr_pages++;
880 return 0;
884 * firmware_data_write - write method for firmware
885 * @filp: open sysfs file
886 * @kobj: kobject for the device
887 * @bin_attr: bin_attr structure
888 * @buffer: buffer being written
889 * @offset: buffer offset for write in total data store area
890 * @count: buffer size
892 * Data written to the 'data' attribute will be later handed to
893 * the driver as a firmware image.
895 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
896 struct bin_attribute *bin_attr,
897 char *buffer, loff_t offset, size_t count)
899 struct device *dev = kobj_to_dev(kobj);
900 struct firmware_priv *fw_priv = to_firmware_priv(dev);
901 struct firmware_buf *buf;
902 ssize_t retval;
904 if (!capable(CAP_SYS_RAWIO))
905 return -EPERM;
907 mutex_lock(&fw_lock);
908 buf = fw_priv->buf;
909 if (!buf || fw_state_is_done(&buf->fw_st)) {
910 retval = -ENODEV;
911 goto out;
914 if (buf->data) {
915 if (offset + count > buf->allocated_size) {
916 retval = -ENOMEM;
917 goto out;
919 firmware_rw_buf(buf, buffer, offset, count, false);
920 retval = count;
921 } else {
922 retval = fw_realloc_buffer(fw_priv, offset + count);
923 if (retval)
924 goto out;
926 retval = count;
927 firmware_rw(buf, buffer, offset, count, false);
930 buf->size = max_t(size_t, offset + count, buf->size);
931 out:
932 mutex_unlock(&fw_lock);
933 return retval;
936 static struct bin_attribute firmware_attr_data = {
937 .attr = { .name = "data", .mode = 0644 },
938 .size = 0,
939 .read = firmware_data_read,
940 .write = firmware_data_write,
943 static struct attribute *fw_dev_attrs[] = {
944 &dev_attr_loading.attr,
945 NULL
948 static struct bin_attribute *fw_dev_bin_attrs[] = {
949 &firmware_attr_data,
950 NULL
953 static const struct attribute_group fw_dev_attr_group = {
954 .attrs = fw_dev_attrs,
955 .bin_attrs = fw_dev_bin_attrs,
958 static const struct attribute_group *fw_dev_attr_groups[] = {
959 &fw_dev_attr_group,
960 NULL
963 static struct firmware_priv *
964 fw_create_instance(struct firmware *firmware, const char *fw_name,
965 struct device *device, unsigned int opt_flags)
967 struct firmware_priv *fw_priv;
968 struct device *f_dev;
970 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
971 if (!fw_priv) {
972 fw_priv = ERR_PTR(-ENOMEM);
973 goto exit;
976 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
977 fw_priv->fw = firmware;
978 f_dev = &fw_priv->dev;
980 device_initialize(f_dev);
981 dev_set_name(f_dev, "%s", fw_name);
982 f_dev->parent = device;
983 f_dev->class = &firmware_class;
984 f_dev->groups = fw_dev_attr_groups;
985 exit:
986 return fw_priv;
989 /* load a firmware via user helper */
990 static int _request_firmware_load(struct firmware_priv *fw_priv,
991 unsigned int opt_flags, long timeout)
993 int retval = 0;
994 struct device *f_dev = &fw_priv->dev;
995 struct firmware_buf *buf = fw_priv->buf;
997 /* fall back on userspace loading */
998 if (!buf->data)
999 buf->is_paged_buf = true;
1001 dev_set_uevent_suppress(f_dev, true);
1003 retval = device_add(f_dev);
1004 if (retval) {
1005 dev_err(f_dev, "%s: device_register failed\n", __func__);
1006 goto err_put_dev;
1009 mutex_lock(&fw_lock);
1010 list_add(&buf->pending_list, &pending_fw_head);
1011 mutex_unlock(&fw_lock);
1013 if (opt_flags & FW_OPT_UEVENT) {
1014 buf->need_uevent = true;
1015 dev_set_uevent_suppress(f_dev, false);
1016 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
1017 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
1018 } else {
1019 timeout = MAX_JIFFY_OFFSET;
1022 retval = fw_state_wait_timeout(&buf->fw_st, timeout);
1023 if (retval < 0) {
1024 mutex_lock(&fw_lock);
1025 fw_load_abort(fw_priv);
1026 mutex_unlock(&fw_lock);
1029 if (fw_state_is_aborted(&buf->fw_st))
1030 retval = -EAGAIN;
1031 else if (buf->is_paged_buf && !buf->data)
1032 retval = -ENOMEM;
1034 device_del(f_dev);
1035 err_put_dev:
1036 put_device(f_dev);
1037 return retval;
1040 static int fw_load_from_user_helper(struct firmware *firmware,
1041 const char *name, struct device *device,
1042 unsigned int opt_flags, long timeout)
1044 struct firmware_priv *fw_priv;
1046 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
1047 if (IS_ERR(fw_priv))
1048 return PTR_ERR(fw_priv);
1050 fw_priv->buf = firmware->priv;
1051 return _request_firmware_load(fw_priv, opt_flags, timeout);
1054 #ifdef CONFIG_PM_SLEEP
1055 /* kill pending requests without uevent to avoid blocking suspend */
1056 static void kill_requests_without_uevent(void)
1058 struct firmware_buf *buf;
1059 struct firmware_buf *next;
1061 mutex_lock(&fw_lock);
1062 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
1063 if (!buf->need_uevent)
1064 __fw_load_abort(buf);
1066 mutex_unlock(&fw_lock);
1068 #endif
1070 #else /* CONFIG_FW_LOADER_USER_HELPER */
1071 static inline int
1072 fw_load_from_user_helper(struct firmware *firmware, const char *name,
1073 struct device *device, unsigned int opt_flags,
1074 long timeout)
1076 return -ENOENT;
1079 #ifdef CONFIG_PM_SLEEP
1080 static inline void kill_requests_without_uevent(void) { }
1081 #endif
1083 #endif /* CONFIG_FW_LOADER_USER_HELPER */
1085 /* prepare firmware and firmware_buf structs;
1086 * return 0 if a firmware is already assigned, 1 if need to load one,
1087 * or a negative error code
1089 static int
1090 _request_firmware_prepare(struct firmware **firmware_p, const char *name,
1091 struct device *device, void *dbuf, size_t size)
1093 struct firmware *firmware;
1094 struct firmware_buf *buf;
1095 int ret;
1097 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
1098 if (!firmware) {
1099 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1100 __func__);
1101 return -ENOMEM;
1104 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
1105 dev_dbg(device, "using built-in %s\n", name);
1106 return 0; /* assigned */
1109 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
1112 * bind with 'buf' now to avoid warning in failure path
1113 * of requesting firmware.
1115 firmware->priv = buf;
1117 if (ret > 0) {
1118 ret = fw_state_wait(&buf->fw_st);
1119 if (!ret) {
1120 fw_set_page_data(buf, firmware);
1121 return 0; /* assigned */
1125 if (ret < 0)
1126 return ret;
1127 return 1; /* need to load */
1130 static int assign_firmware_buf(struct firmware *fw, struct device *device,
1131 unsigned int opt_flags)
1133 struct firmware_buf *buf = fw->priv;
1135 mutex_lock(&fw_lock);
1136 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
1137 mutex_unlock(&fw_lock);
1138 return -ENOENT;
1142 * add firmware name into devres list so that we can auto cache
1143 * and uncache firmware for device.
1145 * device may has been deleted already, but the problem
1146 * should be fixed in devres or driver core.
1148 /* don't cache firmware handled without uevent */
1149 if (device && (opt_flags & FW_OPT_UEVENT) &&
1150 !(opt_flags & FW_OPT_NOCACHE))
1151 fw_add_devm_name(device, buf->fw_id);
1154 * After caching firmware image is started, let it piggyback
1155 * on request firmware.
1157 if (!(opt_flags & FW_OPT_NOCACHE) &&
1158 buf->fwc->state == FW_LOADER_START_CACHE) {
1159 if (fw_cache_piggyback_on_request(buf->fw_id))
1160 kref_get(&buf->ref);
1163 /* pass the pages buffer to driver at the last minute */
1164 fw_set_page_data(buf, fw);
1165 mutex_unlock(&fw_lock);
1166 return 0;
1169 /* called from request_firmware() and request_firmware_work_func() */
1170 static int
1171 _request_firmware(const struct firmware **firmware_p, const char *name,
1172 struct device *device, void *buf, size_t size,
1173 unsigned int opt_flags)
1175 struct firmware *fw = NULL;
1176 long timeout;
1177 int ret;
1179 if (!firmware_p)
1180 return -EINVAL;
1182 if (!name || name[0] == '\0') {
1183 ret = -EINVAL;
1184 goto out;
1187 ret = _request_firmware_prepare(&fw, name, device, buf, size);
1188 if (ret <= 0) /* error or already assigned */
1189 goto out;
1191 ret = 0;
1192 timeout = firmware_loading_timeout();
1193 if (opt_flags & FW_OPT_NOWAIT) {
1194 timeout = usermodehelper_read_lock_wait(timeout);
1195 if (!timeout) {
1196 dev_dbg(device, "firmware: %s loading timed out\n",
1197 name);
1198 ret = -EBUSY;
1199 goto out;
1201 } else {
1202 ret = usermodehelper_read_trylock();
1203 if (WARN_ON(ret)) {
1204 dev_err(device, "firmware: %s will not be loaded\n",
1205 name);
1206 goto out;
1210 ret = fw_get_filesystem_firmware(device, fw->priv);
1211 if (ret) {
1212 if (!(opt_flags & FW_OPT_NO_WARN))
1213 dev_warn(device,
1214 "Direct firmware load for %s failed with error %d\n",
1215 name, ret);
1216 if (opt_flags & FW_OPT_USERHELPER) {
1217 dev_warn(device, "Falling back to user helper\n");
1218 ret = fw_load_from_user_helper(fw, name, device,
1219 opt_flags, timeout);
1223 if (!ret)
1224 ret = assign_firmware_buf(fw, device, opt_flags);
1226 usermodehelper_read_unlock();
1228 out:
1229 if (ret < 0) {
1230 release_firmware(fw);
1231 fw = NULL;
1234 *firmware_p = fw;
1235 return ret;
1239 * request_firmware: - send firmware request and wait for it
1240 * @firmware_p: pointer to firmware image
1241 * @name: name of firmware file
1242 * @device: device for which firmware is being loaded
1244 * @firmware_p will be used to return a firmware image by the name
1245 * of @name for device @device.
1247 * Should be called from user context where sleeping is allowed.
1249 * @name will be used as $FIRMWARE in the uevent environment and
1250 * should be distinctive enough not to be confused with any other
1251 * firmware image for this or any other device.
1253 * Caller must hold the reference count of @device.
1255 * The function can be called safely inside device's suspend and
1256 * resume callback.
1259 request_firmware(const struct firmware **firmware_p, const char *name,
1260 struct device *device)
1262 int ret;
1264 /* Need to pin this module until return */
1265 __module_get(THIS_MODULE);
1266 ret = _request_firmware(firmware_p, name, device, NULL, 0,
1267 FW_OPT_UEVENT | FW_OPT_FALLBACK);
1268 module_put(THIS_MODULE);
1269 return ret;
1271 EXPORT_SYMBOL(request_firmware);
1274 * request_firmware_direct: - load firmware directly without usermode helper
1275 * @firmware_p: pointer to firmware image
1276 * @name: name of firmware file
1277 * @device: device for which firmware is being loaded
1279 * This function works pretty much like request_firmware(), but this doesn't
1280 * fall back to usermode helper even if the firmware couldn't be loaded
1281 * directly from fs. Hence it's useful for loading optional firmwares, which
1282 * aren't always present, without extra long timeouts of udev.
1284 int request_firmware_direct(const struct firmware **firmware_p,
1285 const char *name, struct device *device)
1287 int ret;
1289 __module_get(THIS_MODULE);
1290 ret = _request_firmware(firmware_p, name, device, NULL, 0,
1291 FW_OPT_UEVENT | FW_OPT_NO_WARN);
1292 module_put(THIS_MODULE);
1293 return ret;
1295 EXPORT_SYMBOL_GPL(request_firmware_direct);
1298 * request_firmware_into_buf - load firmware into a previously allocated buffer
1299 * @firmware_p: pointer to firmware image
1300 * @name: name of firmware file
1301 * @device: device for which firmware is being loaded and DMA region allocated
1302 * @buf: address of buffer to load firmware into
1303 * @size: size of buffer
1305 * This function works pretty much like request_firmware(), but it doesn't
1306 * allocate a buffer to hold the firmware data. Instead, the firmware
1307 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1308 * data member is pointed at @buf.
1310 * This function doesn't cache firmware either.
1313 request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1314 struct device *device, void *buf, size_t size)
1316 int ret;
1318 __module_get(THIS_MODULE);
1319 ret = _request_firmware(firmware_p, name, device, buf, size,
1320 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1321 FW_OPT_NOCACHE);
1322 module_put(THIS_MODULE);
1323 return ret;
1325 EXPORT_SYMBOL(request_firmware_into_buf);
1328 * release_firmware: - release the resource associated with a firmware image
1329 * @fw: firmware resource to release
1331 void release_firmware(const struct firmware *fw)
1333 if (fw) {
1334 if (!fw_is_builtin_firmware(fw))
1335 firmware_free_data(fw);
1336 kfree(fw);
1339 EXPORT_SYMBOL(release_firmware);
1341 /* Async support */
1342 struct firmware_work {
1343 struct work_struct work;
1344 struct module *module;
1345 const char *name;
1346 struct device *device;
1347 void *context;
1348 void (*cont)(const struct firmware *fw, void *context);
1349 unsigned int opt_flags;
1352 static void request_firmware_work_func(struct work_struct *work)
1354 struct firmware_work *fw_work;
1355 const struct firmware *fw;
1357 fw_work = container_of(work, struct firmware_work, work);
1359 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
1360 fw_work->opt_flags);
1361 fw_work->cont(fw, fw_work->context);
1362 put_device(fw_work->device); /* taken in request_firmware_nowait() */
1364 module_put(fw_work->module);
1365 kfree_const(fw_work->name);
1366 kfree(fw_work);
1370 * request_firmware_nowait - asynchronous version of request_firmware
1371 * @module: module requesting the firmware
1372 * @uevent: sends uevent to copy the firmware image if this flag
1373 * is non-zero else the firmware copy must be done manually.
1374 * @name: name of firmware file
1375 * @device: device for which firmware is being loaded
1376 * @gfp: allocation flags
1377 * @context: will be passed over to @cont, and
1378 * @fw may be %NULL if firmware request fails.
1379 * @cont: function will be called asynchronously when the firmware
1380 * request is over.
1382 * Caller must hold the reference count of @device.
1384 * Asynchronous variant of request_firmware() for user contexts:
1385 * - sleep for as small periods as possible since it may
1386 * increase kernel boot time of built-in device drivers
1387 * requesting firmware in their ->probe() methods, if
1388 * @gfp is GFP_KERNEL.
1390 * - can't sleep at all if @gfp is GFP_ATOMIC.
1393 request_firmware_nowait(
1394 struct module *module, bool uevent,
1395 const char *name, struct device *device, gfp_t gfp, void *context,
1396 void (*cont)(const struct firmware *fw, void *context))
1398 struct firmware_work *fw_work;
1400 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
1401 if (!fw_work)
1402 return -ENOMEM;
1404 fw_work->module = module;
1405 fw_work->name = kstrdup_const(name, gfp);
1406 if (!fw_work->name) {
1407 kfree(fw_work);
1408 return -ENOMEM;
1410 fw_work->device = device;
1411 fw_work->context = context;
1412 fw_work->cont = cont;
1413 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
1414 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
1416 if (!try_module_get(module)) {
1417 kfree_const(fw_work->name);
1418 kfree(fw_work);
1419 return -EFAULT;
1422 get_device(fw_work->device);
1423 INIT_WORK(&fw_work->work, request_firmware_work_func);
1424 schedule_work(&fw_work->work);
1425 return 0;
1427 EXPORT_SYMBOL(request_firmware_nowait);
1429 #ifdef CONFIG_PM_SLEEP
1430 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1433 * cache_firmware - cache one firmware image in kernel memory space
1434 * @fw_name: the firmware image name
1436 * Cache firmware in kernel memory so that drivers can use it when
1437 * system isn't ready for them to request firmware image from userspace.
1438 * Once it returns successfully, driver can use request_firmware or its
1439 * nowait version to get the cached firmware without any interacting
1440 * with userspace
1442 * Return 0 if the firmware image has been cached successfully
1443 * Return !0 otherwise
1446 static int cache_firmware(const char *fw_name)
1448 int ret;
1449 const struct firmware *fw;
1451 pr_debug("%s: %s\n", __func__, fw_name);
1453 ret = request_firmware(&fw, fw_name, NULL);
1454 if (!ret)
1455 kfree(fw);
1457 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1459 return ret;
1462 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1464 struct firmware_buf *tmp;
1465 struct firmware_cache *fwc = &fw_cache;
1467 spin_lock(&fwc->lock);
1468 tmp = __fw_lookup_buf(fw_name);
1469 spin_unlock(&fwc->lock);
1471 return tmp;
1475 * uncache_firmware - remove one cached firmware image
1476 * @fw_name: the firmware image name
1478 * Uncache one firmware image which has been cached successfully
1479 * before.
1481 * Return 0 if the firmware cache has been removed successfully
1482 * Return !0 otherwise
1485 static int uncache_firmware(const char *fw_name)
1487 struct firmware_buf *buf;
1488 struct firmware fw;
1490 pr_debug("%s: %s\n", __func__, fw_name);
1492 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
1493 return 0;
1495 buf = fw_lookup_buf(fw_name);
1496 if (buf) {
1497 fw_free_buf(buf);
1498 return 0;
1501 return -EINVAL;
1504 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1506 struct fw_cache_entry *fce;
1508 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
1509 if (!fce)
1510 goto exit;
1512 fce->name = kstrdup_const(name, GFP_ATOMIC);
1513 if (!fce->name) {
1514 kfree(fce);
1515 fce = NULL;
1516 goto exit;
1518 exit:
1519 return fce;
1522 static int __fw_entry_found(const char *name)
1524 struct firmware_cache *fwc = &fw_cache;
1525 struct fw_cache_entry *fce;
1527 list_for_each_entry(fce, &fwc->fw_names, list) {
1528 if (!strcmp(fce->name, name))
1529 return 1;
1531 return 0;
1534 static int fw_cache_piggyback_on_request(const char *name)
1536 struct firmware_cache *fwc = &fw_cache;
1537 struct fw_cache_entry *fce;
1538 int ret = 0;
1540 spin_lock(&fwc->name_lock);
1541 if (__fw_entry_found(name))
1542 goto found;
1544 fce = alloc_fw_cache_entry(name);
1545 if (fce) {
1546 ret = 1;
1547 list_add(&fce->list, &fwc->fw_names);
1548 pr_debug("%s: fw: %s\n", __func__, name);
1550 found:
1551 spin_unlock(&fwc->name_lock);
1552 return ret;
1555 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1557 kfree_const(fce->name);
1558 kfree(fce);
1561 static void __async_dev_cache_fw_image(void *fw_entry,
1562 async_cookie_t cookie)
1564 struct fw_cache_entry *fce = fw_entry;
1565 struct firmware_cache *fwc = &fw_cache;
1566 int ret;
1568 ret = cache_firmware(fce->name);
1569 if (ret) {
1570 spin_lock(&fwc->name_lock);
1571 list_del(&fce->list);
1572 spin_unlock(&fwc->name_lock);
1574 free_fw_cache_entry(fce);
1578 /* called with dev->devres_lock held */
1579 static void dev_create_fw_entry(struct device *dev, void *res,
1580 void *data)
1582 struct fw_name_devm *fwn = res;
1583 const char *fw_name = fwn->name;
1584 struct list_head *head = data;
1585 struct fw_cache_entry *fce;
1587 fce = alloc_fw_cache_entry(fw_name);
1588 if (fce)
1589 list_add(&fce->list, head);
1592 static int devm_name_match(struct device *dev, void *res,
1593 void *match_data)
1595 struct fw_name_devm *fwn = res;
1596 return (fwn->magic == (unsigned long)match_data);
1599 static void dev_cache_fw_image(struct device *dev, void *data)
1601 LIST_HEAD(todo);
1602 struct fw_cache_entry *fce;
1603 struct fw_cache_entry *fce_next;
1604 struct firmware_cache *fwc = &fw_cache;
1606 devres_for_each_res(dev, fw_name_devm_release,
1607 devm_name_match, &fw_cache,
1608 dev_create_fw_entry, &todo);
1610 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1611 list_del(&fce->list);
1613 spin_lock(&fwc->name_lock);
1614 /* only one cache entry for one firmware */
1615 if (!__fw_entry_found(fce->name)) {
1616 list_add(&fce->list, &fwc->fw_names);
1617 } else {
1618 free_fw_cache_entry(fce);
1619 fce = NULL;
1621 spin_unlock(&fwc->name_lock);
1623 if (fce)
1624 async_schedule_domain(__async_dev_cache_fw_image,
1625 (void *)fce,
1626 &fw_cache_domain);
1630 static void __device_uncache_fw_images(void)
1632 struct firmware_cache *fwc = &fw_cache;
1633 struct fw_cache_entry *fce;
1635 spin_lock(&fwc->name_lock);
1636 while (!list_empty(&fwc->fw_names)) {
1637 fce = list_entry(fwc->fw_names.next,
1638 struct fw_cache_entry, list);
1639 list_del(&fce->list);
1640 spin_unlock(&fwc->name_lock);
1642 uncache_firmware(fce->name);
1643 free_fw_cache_entry(fce);
1645 spin_lock(&fwc->name_lock);
1647 spin_unlock(&fwc->name_lock);
1651 * device_cache_fw_images - cache devices' firmware
1653 * If one device called request_firmware or its nowait version
1654 * successfully before, the firmware names are recored into the
1655 * device's devres link list, so device_cache_fw_images can call
1656 * cache_firmware() to cache these firmwares for the device,
1657 * then the device driver can load its firmwares easily at
1658 * time when system is not ready to complete loading firmware.
1660 static void device_cache_fw_images(void)
1662 struct firmware_cache *fwc = &fw_cache;
1663 int old_timeout;
1664 DEFINE_WAIT(wait);
1666 pr_debug("%s\n", __func__);
1668 /* cancel uncache work */
1669 cancel_delayed_work_sync(&fwc->work);
1672 * use small loading timeout for caching devices' firmware
1673 * because all these firmware images have been loaded
1674 * successfully at lease once, also system is ready for
1675 * completing firmware loading now. The maximum size of
1676 * firmware in current distributions is about 2M bytes,
1677 * so 10 secs should be enough.
1679 old_timeout = loading_timeout;
1680 loading_timeout = 10;
1682 mutex_lock(&fw_lock);
1683 fwc->state = FW_LOADER_START_CACHE;
1684 dpm_for_each_dev(NULL, dev_cache_fw_image);
1685 mutex_unlock(&fw_lock);
1687 /* wait for completion of caching firmware for all devices */
1688 async_synchronize_full_domain(&fw_cache_domain);
1690 loading_timeout = old_timeout;
1694 * device_uncache_fw_images - uncache devices' firmware
1696 * uncache all firmwares which have been cached successfully
1697 * by device_uncache_fw_images earlier
1699 static void device_uncache_fw_images(void)
1701 pr_debug("%s\n", __func__);
1702 __device_uncache_fw_images();
1705 static void device_uncache_fw_images_work(struct work_struct *work)
1707 device_uncache_fw_images();
1711 * device_uncache_fw_images_delay - uncache devices firmwares
1712 * @delay: number of milliseconds to delay uncache device firmwares
1714 * uncache all devices's firmwares which has been cached successfully
1715 * by device_cache_fw_images after @delay milliseconds.
1717 static void device_uncache_fw_images_delay(unsigned long delay)
1719 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1720 msecs_to_jiffies(delay));
1723 static int fw_pm_notify(struct notifier_block *notify_block,
1724 unsigned long mode, void *unused)
1726 switch (mode) {
1727 case PM_HIBERNATION_PREPARE:
1728 case PM_SUSPEND_PREPARE:
1729 case PM_RESTORE_PREPARE:
1730 kill_requests_without_uevent();
1731 device_cache_fw_images();
1732 break;
1734 case PM_POST_SUSPEND:
1735 case PM_POST_HIBERNATION:
1736 case PM_POST_RESTORE:
1738 * In case that system sleep failed and syscore_suspend is
1739 * not called.
1741 mutex_lock(&fw_lock);
1742 fw_cache.state = FW_LOADER_NO_CACHE;
1743 mutex_unlock(&fw_lock);
1745 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1746 break;
1749 return 0;
1752 /* stop caching firmware once syscore_suspend is reached */
1753 static int fw_suspend(void)
1755 fw_cache.state = FW_LOADER_NO_CACHE;
1756 return 0;
1759 static struct syscore_ops fw_syscore_ops = {
1760 .suspend = fw_suspend,
1762 #else
1763 static int fw_cache_piggyback_on_request(const char *name)
1765 return 0;
1767 #endif
1769 static void __init fw_cache_init(void)
1771 spin_lock_init(&fw_cache.lock);
1772 INIT_LIST_HEAD(&fw_cache.head);
1773 fw_cache.state = FW_LOADER_NO_CACHE;
1775 #ifdef CONFIG_PM_SLEEP
1776 spin_lock_init(&fw_cache.name_lock);
1777 INIT_LIST_HEAD(&fw_cache.fw_names);
1779 INIT_DELAYED_WORK(&fw_cache.work,
1780 device_uncache_fw_images_work);
1782 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1783 register_pm_notifier(&fw_cache.pm_notify);
1785 register_syscore_ops(&fw_syscore_ops);
1786 #endif
1789 static int __init firmware_class_init(void)
1791 fw_cache_init();
1792 #ifdef CONFIG_FW_LOADER_USER_HELPER
1793 register_reboot_notifier(&fw_shutdown_nb);
1794 return class_register(&firmware_class);
1795 #else
1796 return 0;
1797 #endif
1800 static void __exit firmware_class_exit(void)
1802 #ifdef CONFIG_PM_SLEEP
1803 unregister_syscore_ops(&fw_syscore_ops);
1804 unregister_pm_notifier(&fw_cache.pm_notify);
1805 #endif
1806 #ifdef CONFIG_FW_LOADER_USER_HELPER
1807 unregister_reboot_notifier(&fw_shutdown_nb);
1808 class_unregister(&firmware_class);
1809 #endif
1812 fs_initcall(firmware_class_init);
1813 module_exit(firmware_class_exit);