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.
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/async.h>
28 #include <linux/suspend.h>
29 #include <linux/syscore_ops.h>
31 #include <generated/utsrelease.h>
35 MODULE_AUTHOR("Manuel Estrada Sainz");
36 MODULE_DESCRIPTION("Multi purpose firmware loading support");
37 MODULE_LICENSE("GPL");
39 static const char *fw_path
[] = {
40 "/lib/firmware/updates/" UTS_RELEASE
,
41 "/lib/firmware/updates",
42 "/lib/firmware/" UTS_RELEASE
,
46 /* Don't inline this: 'struct kstat' is biggish */
47 static noinline
long fw_file_size(struct file
*file
)
50 if (vfs_getattr(file
->f_path
.mnt
, file
->f_path
.dentry
, &st
))
52 if (!S_ISREG(st
.mode
))
54 if (st
.size
!= (long)st
.size
)
59 static bool fw_read_file_contents(struct file
*file
, struct firmware
*fw
)
64 size
= fw_file_size(file
);
70 if (kernel_read(file
, 0, buf
, size
) != size
) {
79 static bool fw_get_filesystem_firmware(struct firmware
*fw
, const char *name
)
83 char *path
= __getname();
85 for (i
= 0; i
< ARRAY_SIZE(fw_path
); i
++) {
87 snprintf(path
, PATH_MAX
, "%s/%s", fw_path
[i
], name
);
89 file
= filp_open(path
, O_RDONLY
, 0);
92 success
= fw_read_file_contents(file
, fw
);
101 /* Builtin firmware support */
103 #ifdef CONFIG_FW_LOADER
105 extern struct builtin_fw __start_builtin_fw
[];
106 extern struct builtin_fw __end_builtin_fw
[];
108 static bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
110 struct builtin_fw
*b_fw
;
112 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
113 if (strcmp(name
, b_fw
->name
) == 0) {
114 fw
->size
= b_fw
->size
;
115 fw
->data
= b_fw
->data
;
123 static bool fw_is_builtin_firmware(const struct firmware
*fw
)
125 struct builtin_fw
*b_fw
;
127 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++)
128 if (fw
->data
== b_fw
->data
)
134 #else /* Module case - no builtin firmware support */
136 static inline bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
141 static inline bool fw_is_builtin_firmware(const struct firmware
*fw
)
153 static int loading_timeout
= 60; /* In seconds */
155 static inline long firmware_loading_timeout(void)
157 return loading_timeout
> 0 ? loading_timeout
* HZ
: MAX_SCHEDULE_TIMEOUT
;
160 struct firmware_cache
{
161 /* firmware_buf instance will be added into the below list */
163 struct list_head head
;
166 #ifdef CONFIG_PM_SLEEP
168 * Names of firmware images which have been cached successfully
169 * will be added into the below list so that device uncache
170 * helper can trace which firmware images have been cached
173 spinlock_t name_lock
;
174 struct list_head fw_names
;
176 wait_queue_head_t wait_queue
;
178 struct delayed_work work
;
180 struct notifier_block pm_notify
;
184 struct firmware_buf
{
186 struct list_head list
;
187 struct completion completion
;
188 struct firmware_cache
*fwc
;
189 unsigned long status
;
198 struct fw_cache_entry
{
199 struct list_head list
;
203 struct firmware_priv
{
204 struct timer_list timeout
;
207 struct firmware_buf
*buf
;
211 struct fw_name_devm
{
216 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
218 #define FW_LOADER_NO_CACHE 0
219 #define FW_LOADER_START_CACHE 1
221 static int fw_cache_piggyback_on_request(const char *name
);
223 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
224 * guarding for corner cases a global lock should be OK */
225 static DEFINE_MUTEX(fw_lock
);
227 static struct firmware_cache fw_cache
;
229 static struct firmware_buf
*__allocate_fw_buf(const char *fw_name
,
230 struct firmware_cache
*fwc
)
232 struct firmware_buf
*buf
;
234 buf
= kzalloc(sizeof(*buf
) + strlen(fw_name
) + 1 , GFP_ATOMIC
);
239 kref_init(&buf
->ref
);
240 strcpy(buf
->fw_id
, fw_name
);
242 init_completion(&buf
->completion
);
244 pr_debug("%s: fw-%s buf=%p\n", __func__
, fw_name
, buf
);
249 static struct firmware_buf
*__fw_lookup_buf(const char *fw_name
)
251 struct firmware_buf
*tmp
;
252 struct firmware_cache
*fwc
= &fw_cache
;
254 list_for_each_entry(tmp
, &fwc
->head
, list
)
255 if (!strcmp(tmp
->fw_id
, fw_name
))
260 static int fw_lookup_and_allocate_buf(const char *fw_name
,
261 struct firmware_cache
*fwc
,
262 struct firmware_buf
**buf
)
264 struct firmware_buf
*tmp
;
266 spin_lock(&fwc
->lock
);
267 tmp
= __fw_lookup_buf(fw_name
);
270 spin_unlock(&fwc
->lock
);
274 tmp
= __allocate_fw_buf(fw_name
, fwc
);
276 list_add(&tmp
->list
, &fwc
->head
);
277 spin_unlock(&fwc
->lock
);
281 return tmp
? 0 : -ENOMEM
;
284 static struct firmware_buf
*fw_lookup_buf(const char *fw_name
)
286 struct firmware_buf
*tmp
;
287 struct firmware_cache
*fwc
= &fw_cache
;
289 spin_lock(&fwc
->lock
);
290 tmp
= __fw_lookup_buf(fw_name
);
291 spin_unlock(&fwc
->lock
);
296 static void __fw_free_buf(struct kref
*ref
)
298 struct firmware_buf
*buf
= to_fwbuf(ref
);
299 struct firmware_cache
*fwc
= buf
->fwc
;
302 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
303 __func__
, buf
->fw_id
, buf
, buf
->data
,
304 (unsigned int)buf
->size
);
306 spin_lock(&fwc
->lock
);
307 list_del(&buf
->list
);
308 spin_unlock(&fwc
->lock
);
311 for (i
= 0; i
< buf
->nr_pages
; i
++)
312 __free_page(buf
->pages
[i
]);
317 static void fw_free_buf(struct firmware_buf
*buf
)
319 kref_put(&buf
->ref
, __fw_free_buf
);
322 static struct firmware_priv
*to_firmware_priv(struct device
*dev
)
324 return container_of(dev
, struct firmware_priv
, dev
);
327 static void fw_load_abort(struct firmware_priv
*fw_priv
)
329 struct firmware_buf
*buf
= fw_priv
->buf
;
331 set_bit(FW_STATUS_ABORT
, &buf
->status
);
332 complete_all(&buf
->completion
);
335 static ssize_t
firmware_timeout_show(struct class *class,
336 struct class_attribute
*attr
,
339 return sprintf(buf
, "%d\n", loading_timeout
);
343 * firmware_timeout_store - set number of seconds to wait for firmware
344 * @class: device class pointer
345 * @attr: device attribute pointer
346 * @buf: buffer to scan for timeout value
347 * @count: number of bytes in @buf
349 * Sets the number of seconds to wait for the firmware. Once
350 * this expires an error will be returned to the driver and no
351 * firmware will be provided.
353 * Note: zero means 'wait forever'.
355 static ssize_t
firmware_timeout_store(struct class *class,
356 struct class_attribute
*attr
,
357 const char *buf
, size_t count
)
359 loading_timeout
= simple_strtol(buf
, NULL
, 10);
360 if (loading_timeout
< 0)
366 static struct class_attribute firmware_class_attrs
[] = {
367 __ATTR(timeout
, S_IWUSR
| S_IRUGO
,
368 firmware_timeout_show
, firmware_timeout_store
),
372 static void fw_dev_release(struct device
*dev
)
374 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
378 module_put(THIS_MODULE
);
381 static int firmware_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
383 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
385 if (add_uevent_var(env
, "FIRMWARE=%s", fw_priv
->buf
->fw_id
))
387 if (add_uevent_var(env
, "TIMEOUT=%i", loading_timeout
))
389 if (add_uevent_var(env
, "ASYNC=%d", fw_priv
->nowait
))
395 static struct class firmware_class
= {
397 .class_attrs
= firmware_class_attrs
,
398 .dev_uevent
= firmware_uevent
,
399 .dev_release
= fw_dev_release
,
402 static ssize_t
firmware_loading_show(struct device
*dev
,
403 struct device_attribute
*attr
, char *buf
)
405 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
406 int loading
= test_bit(FW_STATUS_LOADING
, &fw_priv
->buf
->status
);
408 return sprintf(buf
, "%d\n", loading
);
411 /* firmware holds the ownership of pages */
412 static void firmware_free_data(const struct firmware
*fw
)
414 /* Loaded directly? */
419 fw_free_buf(fw
->priv
);
422 /* Some architectures don't have PAGE_KERNEL_RO */
423 #ifndef PAGE_KERNEL_RO
424 #define PAGE_KERNEL_RO PAGE_KERNEL
427 * firmware_loading_store - set value in the 'loading' control file
428 * @dev: device pointer
429 * @attr: device attribute pointer
430 * @buf: buffer to scan for loading control value
431 * @count: number of bytes in @buf
433 * The relevant values are:
435 * 1: Start a load, discarding any previous partial load.
436 * 0: Conclude the load and hand the data to the driver code.
437 * -1: Conclude the load with an error and discard any written data.
439 static ssize_t
firmware_loading_store(struct device
*dev
,
440 struct device_attribute
*attr
,
441 const char *buf
, size_t count
)
443 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
444 struct firmware_buf
*fw_buf
= fw_priv
->buf
;
445 int loading
= simple_strtol(buf
, NULL
, 10);
448 mutex_lock(&fw_lock
);
455 /* discarding any previous partial load */
456 if (!test_bit(FW_STATUS_DONE
, &fw_buf
->status
)) {
457 for (i
= 0; i
< fw_buf
->nr_pages
; i
++)
458 __free_page(fw_buf
->pages
[i
]);
459 kfree(fw_buf
->pages
);
460 fw_buf
->pages
= NULL
;
461 fw_buf
->page_array_size
= 0;
462 fw_buf
->nr_pages
= 0;
463 set_bit(FW_STATUS_LOADING
, &fw_buf
->status
);
467 if (test_bit(FW_STATUS_LOADING
, &fw_buf
->status
)) {
468 set_bit(FW_STATUS_DONE
, &fw_buf
->status
);
469 clear_bit(FW_STATUS_LOADING
, &fw_buf
->status
);
470 complete_all(&fw_buf
->completion
);
475 dev_err(dev
, "%s: unexpected value (%d)\n", __func__
, loading
);
478 fw_load_abort(fw_priv
);
482 mutex_unlock(&fw_lock
);
486 static DEVICE_ATTR(loading
, 0644, firmware_loading_show
, firmware_loading_store
);
488 static ssize_t
firmware_data_read(struct file
*filp
, struct kobject
*kobj
,
489 struct bin_attribute
*bin_attr
,
490 char *buffer
, loff_t offset
, size_t count
)
492 struct device
*dev
= kobj_to_dev(kobj
);
493 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
494 struct firmware_buf
*buf
;
497 mutex_lock(&fw_lock
);
499 if (!buf
|| test_bit(FW_STATUS_DONE
, &buf
->status
)) {
503 if (offset
> buf
->size
) {
507 if (count
> buf
->size
- offset
)
508 count
= buf
->size
- offset
;
514 int page_nr
= offset
>> PAGE_SHIFT
;
515 int page_ofs
= offset
& (PAGE_SIZE
-1);
516 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
518 page_data
= kmap(buf
->pages
[page_nr
]);
520 memcpy(buffer
, page_data
+ page_ofs
, page_cnt
);
522 kunmap(buf
->pages
[page_nr
]);
528 mutex_unlock(&fw_lock
);
532 static int fw_realloc_buffer(struct firmware_priv
*fw_priv
, int min_size
)
534 struct firmware_buf
*buf
= fw_priv
->buf
;
535 int pages_needed
= ALIGN(min_size
, PAGE_SIZE
) >> PAGE_SHIFT
;
537 /* If the array of pages is too small, grow it... */
538 if (buf
->page_array_size
< pages_needed
) {
539 int new_array_size
= max(pages_needed
,
540 buf
->page_array_size
* 2);
541 struct page
**new_pages
;
543 new_pages
= kmalloc(new_array_size
* sizeof(void *),
546 fw_load_abort(fw_priv
);
549 memcpy(new_pages
, buf
->pages
,
550 buf
->page_array_size
* sizeof(void *));
551 memset(&new_pages
[buf
->page_array_size
], 0, sizeof(void *) *
552 (new_array_size
- buf
->page_array_size
));
554 buf
->pages
= new_pages
;
555 buf
->page_array_size
= new_array_size
;
558 while (buf
->nr_pages
< pages_needed
) {
559 buf
->pages
[buf
->nr_pages
] =
560 alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
562 if (!buf
->pages
[buf
->nr_pages
]) {
563 fw_load_abort(fw_priv
);
572 * firmware_data_write - write method for firmware
573 * @filp: open sysfs file
574 * @kobj: kobject for the device
575 * @bin_attr: bin_attr structure
576 * @buffer: buffer being written
577 * @offset: buffer offset for write in total data store area
578 * @count: buffer size
580 * Data written to the 'data' attribute will be later handed to
581 * the driver as a firmware image.
583 static ssize_t
firmware_data_write(struct file
*filp
, struct kobject
*kobj
,
584 struct bin_attribute
*bin_attr
,
585 char *buffer
, loff_t offset
, size_t count
)
587 struct device
*dev
= kobj_to_dev(kobj
);
588 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
589 struct firmware_buf
*buf
;
592 if (!capable(CAP_SYS_RAWIO
))
595 mutex_lock(&fw_lock
);
597 if (!buf
|| test_bit(FW_STATUS_DONE
, &buf
->status
)) {
602 retval
= fw_realloc_buffer(fw_priv
, offset
+ count
);
610 int page_nr
= offset
>> PAGE_SHIFT
;
611 int page_ofs
= offset
& (PAGE_SIZE
- 1);
612 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
614 page_data
= kmap(buf
->pages
[page_nr
]);
616 memcpy(page_data
+ page_ofs
, buffer
, page_cnt
);
618 kunmap(buf
->pages
[page_nr
]);
624 buf
->size
= max_t(size_t, offset
, buf
->size
);
626 mutex_unlock(&fw_lock
);
630 static struct bin_attribute firmware_attr_data
= {
631 .attr
= { .name
= "data", .mode
= 0644 },
633 .read
= firmware_data_read
,
634 .write
= firmware_data_write
,
637 static void firmware_class_timeout(u_long data
)
639 struct firmware_priv
*fw_priv
= (struct firmware_priv
*) data
;
641 fw_load_abort(fw_priv
);
644 static struct firmware_priv
*
645 fw_create_instance(struct firmware
*firmware
, const char *fw_name
,
646 struct device
*device
, bool uevent
, bool nowait
)
648 struct firmware_priv
*fw_priv
;
649 struct device
*f_dev
;
651 fw_priv
= kzalloc(sizeof(*fw_priv
), GFP_KERNEL
);
653 dev_err(device
, "%s: kmalloc failed\n", __func__
);
654 fw_priv
= ERR_PTR(-ENOMEM
);
658 fw_priv
->nowait
= nowait
;
659 fw_priv
->fw
= firmware
;
660 setup_timer(&fw_priv
->timeout
,
661 firmware_class_timeout
, (u_long
) fw_priv
);
663 f_dev
= &fw_priv
->dev
;
665 device_initialize(f_dev
);
666 dev_set_name(f_dev
, "%s", fw_name
);
667 f_dev
->parent
= device
;
668 f_dev
->class = &firmware_class
;
673 /* one pages buffer is mapped/unmapped only once */
674 static int fw_map_pages_buf(struct firmware_buf
*buf
)
676 buf
->data
= vmap(buf
->pages
, buf
->nr_pages
, 0, PAGE_KERNEL_RO
);
682 /* store the pages buffer info firmware from buf */
683 static void fw_set_page_data(struct firmware_buf
*buf
, struct firmware
*fw
)
686 fw
->pages
= buf
->pages
;
687 fw
->size
= buf
->size
;
688 fw
->data
= buf
->data
;
690 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
691 __func__
, buf
->fw_id
, buf
, buf
->data
,
692 (unsigned int)buf
->size
);
695 #ifdef CONFIG_PM_SLEEP
696 static void fw_name_devm_release(struct device
*dev
, void *res
)
698 struct fw_name_devm
*fwn
= res
;
700 if (fwn
->magic
== (unsigned long)&fw_cache
)
701 pr_debug("%s: fw_name-%s devm-%p released\n",
702 __func__
, fwn
->name
, res
);
705 static int fw_devm_match(struct device
*dev
, void *res
,
708 struct fw_name_devm
*fwn
= res
;
710 return (fwn
->magic
== (unsigned long)&fw_cache
) &&
711 !strcmp(fwn
->name
, match_data
);
714 static struct fw_name_devm
*fw_find_devm_name(struct device
*dev
,
717 struct fw_name_devm
*fwn
;
719 fwn
= devres_find(dev
, fw_name_devm_release
,
720 fw_devm_match
, (void *)name
);
724 /* add firmware name into devres list */
725 static int fw_add_devm_name(struct device
*dev
, const char *name
)
727 struct fw_name_devm
*fwn
;
729 fwn
= fw_find_devm_name(dev
, name
);
733 fwn
= devres_alloc(fw_name_devm_release
, sizeof(struct fw_name_devm
) +
734 strlen(name
) + 1, GFP_KERNEL
);
738 fwn
->magic
= (unsigned long)&fw_cache
;
739 strcpy(fwn
->name
, name
);
740 devres_add(dev
, fwn
);
745 static int fw_add_devm_name(struct device
*dev
, const char *name
)
751 static void _request_firmware_cleanup(const struct firmware
**firmware_p
)
753 release_firmware(*firmware_p
);
757 static struct firmware_priv
*
758 _request_firmware_prepare(const struct firmware
**firmware_p
, const char *name
,
759 struct device
*device
, bool uevent
, bool nowait
)
761 struct firmware
*firmware
;
762 struct firmware_priv
*fw_priv
= NULL
;
763 struct firmware_buf
*buf
;
767 return ERR_PTR(-EINVAL
);
769 *firmware_p
= firmware
= kzalloc(sizeof(*firmware
), GFP_KERNEL
);
771 dev_err(device
, "%s: kmalloc(struct firmware) failed\n",
773 return ERR_PTR(-ENOMEM
);
776 if (fw_get_builtin_firmware(firmware
, name
)) {
777 dev_dbg(device
, "firmware: using built-in firmware %s\n", name
);
781 if (fw_get_filesystem_firmware(firmware
, name
)) {
782 dev_dbg(device
, "firmware: direct-loading firmware %s\n", name
);
786 ret
= fw_lookup_and_allocate_buf(name
, &fw_cache
, &buf
);
788 fw_priv
= fw_create_instance(firmware
, name
, device
,
791 if (IS_ERR(fw_priv
) || ret
< 0) {
794 return ERR_PTR(-ENOMEM
);
795 } else if (fw_priv
) {
799 * bind with 'buf' now to avoid warning in failure path
800 * of requesting firmware.
802 firmware
->priv
= buf
;
806 /* share the cached buf, which is inprogessing or completed */
808 mutex_lock(&fw_lock
);
809 if (test_bit(FW_STATUS_ABORT
, &buf
->status
)) {
810 fw_priv
= ERR_PTR(-ENOENT
);
811 firmware
->priv
= buf
;
812 _request_firmware_cleanup(firmware_p
);
814 } else if (test_bit(FW_STATUS_DONE
, &buf
->status
)) {
816 fw_set_page_data(buf
, firmware
);
819 mutex_unlock(&fw_lock
);
820 wait_for_completion(&buf
->completion
);
824 mutex_unlock(&fw_lock
);
828 static int _request_firmware_load(struct firmware_priv
*fw_priv
, bool uevent
,
832 struct device
*f_dev
= &fw_priv
->dev
;
833 struct firmware_buf
*buf
= fw_priv
->buf
;
834 struct firmware_cache
*fwc
= &fw_cache
;
836 dev_set_uevent_suppress(f_dev
, true);
838 /* Need to pin this module until class device is destroyed */
839 __module_get(THIS_MODULE
);
841 retval
= device_add(f_dev
);
843 dev_err(f_dev
, "%s: device_register failed\n", __func__
);
847 retval
= device_create_bin_file(f_dev
, &firmware_attr_data
);
849 dev_err(f_dev
, "%s: sysfs_create_bin_file failed\n", __func__
);
853 retval
= device_create_file(f_dev
, &dev_attr_loading
);
855 dev_err(f_dev
, "%s: device_create_file failed\n", __func__
);
856 goto err_del_bin_attr
;
860 dev_set_uevent_suppress(f_dev
, false);
861 dev_dbg(f_dev
, "firmware: requesting %s\n", buf
->fw_id
);
862 if (timeout
!= MAX_SCHEDULE_TIMEOUT
)
863 mod_timer(&fw_priv
->timeout
,
864 round_jiffies_up(jiffies
+ timeout
));
866 kobject_uevent(&fw_priv
->dev
.kobj
, KOBJ_ADD
);
869 wait_for_completion(&buf
->completion
);
871 del_timer_sync(&fw_priv
->timeout
);
873 mutex_lock(&fw_lock
);
874 if (!buf
->size
|| test_bit(FW_STATUS_ABORT
, &buf
->status
))
878 * add firmware name into devres list so that we can auto cache
879 * and uncache firmware for device.
881 * f_dev->parent may has been deleted already, but the problem
882 * should be fixed in devres or driver core.
884 if (!retval
&& f_dev
->parent
)
885 fw_add_devm_name(f_dev
->parent
, buf
->fw_id
);
888 retval
= fw_map_pages_buf(buf
);
891 * After caching firmware image is started, let it piggyback
892 * on request firmware.
894 if (!retval
&& fwc
->state
== FW_LOADER_START_CACHE
) {
895 if (fw_cache_piggyback_on_request(buf
->fw_id
))
899 /* pass the pages buffer to driver at the last minute */
900 fw_set_page_data(buf
, fw_priv
->fw
);
903 mutex_unlock(&fw_lock
);
905 device_remove_file(f_dev
, &dev_attr_loading
);
907 device_remove_bin_file(f_dev
, &firmware_attr_data
);
916 * request_firmware: - send firmware request and wait for it
917 * @firmware_p: pointer to firmware image
918 * @name: name of firmware file
919 * @device: device for which firmware is being loaded
921 * @firmware_p will be used to return a firmware image by the name
922 * of @name for device @device.
924 * Should be called from user context where sleeping is allowed.
926 * @name will be used as $FIRMWARE in the uevent environment and
927 * should be distinctive enough not to be confused with any other
928 * firmware image for this or any other device.
930 * Caller must hold the reference count of @device.
933 request_firmware(const struct firmware
**firmware_p
, const char *name
,
934 struct device
*device
)
936 struct firmware_priv
*fw_priv
;
939 fw_priv
= _request_firmware_prepare(firmware_p
, name
, device
, true,
941 if (IS_ERR_OR_NULL(fw_priv
))
942 return PTR_RET(fw_priv
);
944 ret
= usermodehelper_read_trylock();
946 dev_err(device
, "firmware: %s will not be loaded\n", name
);
948 ret
= _request_firmware_load(fw_priv
, true,
949 firmware_loading_timeout());
950 usermodehelper_read_unlock();
953 _request_firmware_cleanup(firmware_p
);
959 * release_firmware: - release the resource associated with a firmware image
960 * @fw: firmware resource to release
962 void release_firmware(const struct firmware
*fw
)
965 if (!fw_is_builtin_firmware(fw
))
966 firmware_free_data(fw
);
972 struct firmware_work
{
973 struct work_struct work
;
974 struct module
*module
;
976 struct device
*device
;
978 void (*cont
)(const struct firmware
*fw
, void *context
);
982 static void request_firmware_work_func(struct work_struct
*work
)
984 struct firmware_work
*fw_work
;
985 const struct firmware
*fw
;
986 struct firmware_priv
*fw_priv
;
990 fw_work
= container_of(work
, struct firmware_work
, work
);
991 fw_priv
= _request_firmware_prepare(&fw
, fw_work
->name
, fw_work
->device
,
992 fw_work
->uevent
, true);
993 if (IS_ERR_OR_NULL(fw_priv
)) {
994 ret
= PTR_RET(fw_priv
);
998 timeout
= usermodehelper_read_lock_wait(firmware_loading_timeout());
1000 ret
= _request_firmware_load(fw_priv
, fw_work
->uevent
, timeout
);
1001 usermodehelper_read_unlock();
1003 dev_dbg(fw_work
->device
, "firmware: %s loading timed out\n",
1008 _request_firmware_cleanup(&fw
);
1011 fw_work
->cont(fw
, fw_work
->context
);
1012 put_device(fw_work
->device
);
1014 module_put(fw_work
->module
);
1019 * request_firmware_nowait - asynchronous version of request_firmware
1020 * @module: module requesting the firmware
1021 * @uevent: sends uevent to copy the firmware image if this flag
1022 * is non-zero else the firmware copy must be done manually.
1023 * @name: name of firmware file
1024 * @device: device for which firmware is being loaded
1025 * @gfp: allocation flags
1026 * @context: will be passed over to @cont, and
1027 * @fw may be %NULL if firmware request fails.
1028 * @cont: function will be called asynchronously when the firmware
1031 * Caller must hold the reference count of @device.
1033 * Asynchronous variant of request_firmware() for user contexts:
1034 * - sleep for as small periods as possible since it may
1035 * increase kernel boot time of built-in device drivers
1036 * requesting firmware in their ->probe() methods, if
1037 * @gfp is GFP_KERNEL.
1039 * - can't sleep at all if @gfp is GFP_ATOMIC.
1042 request_firmware_nowait(
1043 struct module
*module
, bool uevent
,
1044 const char *name
, struct device
*device
, gfp_t gfp
, void *context
,
1045 void (*cont
)(const struct firmware
*fw
, void *context
))
1047 struct firmware_work
*fw_work
;
1049 fw_work
= kzalloc(sizeof (struct firmware_work
), gfp
);
1053 fw_work
->module
= module
;
1054 fw_work
->name
= name
;
1055 fw_work
->device
= device
;
1056 fw_work
->context
= context
;
1057 fw_work
->cont
= cont
;
1058 fw_work
->uevent
= uevent
;
1060 if (!try_module_get(module
)) {
1065 get_device(fw_work
->device
);
1066 INIT_WORK(&fw_work
->work
, request_firmware_work_func
);
1067 schedule_work(&fw_work
->work
);
1072 * cache_firmware - cache one firmware image in kernel memory space
1073 * @fw_name: the firmware image name
1075 * Cache firmware in kernel memory so that drivers can use it when
1076 * system isn't ready for them to request firmware image from userspace.
1077 * Once it returns successfully, driver can use request_firmware or its
1078 * nowait version to get the cached firmware without any interacting
1081 * Return 0 if the firmware image has been cached successfully
1082 * Return !0 otherwise
1085 int cache_firmware(const char *fw_name
)
1088 const struct firmware
*fw
;
1090 pr_debug("%s: %s\n", __func__
, fw_name
);
1092 ret
= request_firmware(&fw
, fw_name
, NULL
);
1096 pr_debug("%s: %s ret=%d\n", __func__
, fw_name
, ret
);
1102 * uncache_firmware - remove one cached firmware image
1103 * @fw_name: the firmware image name
1105 * Uncache one firmware image which has been cached successfully
1108 * Return 0 if the firmware cache has been removed successfully
1109 * Return !0 otherwise
1112 int uncache_firmware(const char *fw_name
)
1114 struct firmware_buf
*buf
;
1117 pr_debug("%s: %s\n", __func__
, fw_name
);
1119 if (fw_get_builtin_firmware(&fw
, fw_name
))
1122 buf
= fw_lookup_buf(fw_name
);
1131 #ifdef CONFIG_PM_SLEEP
1132 static struct fw_cache_entry
*alloc_fw_cache_entry(const char *name
)
1134 struct fw_cache_entry
*fce
;
1136 fce
= kzalloc(sizeof(*fce
) + strlen(name
) + 1, GFP_ATOMIC
);
1140 strcpy(fce
->name
, name
);
1145 static int fw_cache_piggyback_on_request(const char *name
)
1147 struct firmware_cache
*fwc
= &fw_cache
;
1148 struct fw_cache_entry
*fce
;
1151 spin_lock(&fwc
->name_lock
);
1152 list_for_each_entry(fce
, &fwc
->fw_names
, list
) {
1153 if (!strcmp(fce
->name
, name
))
1157 fce
= alloc_fw_cache_entry(name
);
1160 list_add(&fce
->list
, &fwc
->fw_names
);
1161 pr_debug("%s: fw: %s\n", __func__
, name
);
1164 spin_unlock(&fwc
->name_lock
);
1168 static void free_fw_cache_entry(struct fw_cache_entry
*fce
)
1173 static void __async_dev_cache_fw_image(void *fw_entry
,
1174 async_cookie_t cookie
)
1176 struct fw_cache_entry
*fce
= fw_entry
;
1177 struct firmware_cache
*fwc
= &fw_cache
;
1180 ret
= cache_firmware(fce
->name
);
1182 spin_lock(&fwc
->name_lock
);
1183 list_del(&fce
->list
);
1184 spin_unlock(&fwc
->name_lock
);
1186 free_fw_cache_entry(fce
);
1189 spin_lock(&fwc
->name_lock
);
1191 spin_unlock(&fwc
->name_lock
);
1193 wake_up(&fwc
->wait_queue
);
1196 /* called with dev->devres_lock held */
1197 static void dev_create_fw_entry(struct device
*dev
, void *res
,
1200 struct fw_name_devm
*fwn
= res
;
1201 const char *fw_name
= fwn
->name
;
1202 struct list_head
*head
= data
;
1203 struct fw_cache_entry
*fce
;
1205 fce
= alloc_fw_cache_entry(fw_name
);
1207 list_add(&fce
->list
, head
);
1210 static int devm_name_match(struct device
*dev
, void *res
,
1213 struct fw_name_devm
*fwn
= res
;
1214 return (fwn
->magic
== (unsigned long)match_data
);
1217 static void dev_cache_fw_image(struct device
*dev
, void *data
)
1220 struct fw_cache_entry
*fce
;
1221 struct fw_cache_entry
*fce_next
;
1222 struct firmware_cache
*fwc
= &fw_cache
;
1224 devres_for_each_res(dev
, fw_name_devm_release
,
1225 devm_name_match
, &fw_cache
,
1226 dev_create_fw_entry
, &todo
);
1228 list_for_each_entry_safe(fce
, fce_next
, &todo
, list
) {
1229 list_del(&fce
->list
);
1231 spin_lock(&fwc
->name_lock
);
1233 list_add(&fce
->list
, &fwc
->fw_names
);
1234 spin_unlock(&fwc
->name_lock
);
1236 async_schedule(__async_dev_cache_fw_image
, (void *)fce
);
1240 static void __device_uncache_fw_images(void)
1242 struct firmware_cache
*fwc
= &fw_cache
;
1243 struct fw_cache_entry
*fce
;
1245 spin_lock(&fwc
->name_lock
);
1246 while (!list_empty(&fwc
->fw_names
)) {
1247 fce
= list_entry(fwc
->fw_names
.next
,
1248 struct fw_cache_entry
, list
);
1249 list_del(&fce
->list
);
1250 spin_unlock(&fwc
->name_lock
);
1252 uncache_firmware(fce
->name
);
1253 free_fw_cache_entry(fce
);
1255 spin_lock(&fwc
->name_lock
);
1257 spin_unlock(&fwc
->name_lock
);
1261 * device_cache_fw_images - cache devices' firmware
1263 * If one device called request_firmware or its nowait version
1264 * successfully before, the firmware names are recored into the
1265 * device's devres link list, so device_cache_fw_images can call
1266 * cache_firmware() to cache these firmwares for the device,
1267 * then the device driver can load its firmwares easily at
1268 * time when system is not ready to complete loading firmware.
1270 static void device_cache_fw_images(void)
1272 struct firmware_cache
*fwc
= &fw_cache
;
1276 pr_debug("%s\n", __func__
);
1279 * use small loading timeout for caching devices' firmware
1280 * because all these firmware images have been loaded
1281 * successfully at lease once, also system is ready for
1282 * completing firmware loading now. The maximum size of
1283 * firmware in current distributions is about 2M bytes,
1284 * so 10 secs should be enough.
1286 old_timeout
= loading_timeout
;
1287 loading_timeout
= 10;
1289 mutex_lock(&fw_lock
);
1290 fwc
->state
= FW_LOADER_START_CACHE
;
1291 dpm_for_each_dev(NULL
, dev_cache_fw_image
);
1292 mutex_unlock(&fw_lock
);
1294 /* wait for completion of caching firmware for all devices */
1295 spin_lock(&fwc
->name_lock
);
1297 prepare_to_wait(&fwc
->wait_queue
, &wait
,
1298 TASK_UNINTERRUPTIBLE
);
1302 spin_unlock(&fwc
->name_lock
);
1306 spin_lock(&fwc
->name_lock
);
1308 spin_unlock(&fwc
->name_lock
);
1309 finish_wait(&fwc
->wait_queue
, &wait
);
1311 loading_timeout
= old_timeout
;
1315 * device_uncache_fw_images - uncache devices' firmware
1317 * uncache all firmwares which have been cached successfully
1318 * by device_uncache_fw_images earlier
1320 static void device_uncache_fw_images(void)
1322 pr_debug("%s\n", __func__
);
1323 __device_uncache_fw_images();
1326 static void device_uncache_fw_images_work(struct work_struct
*work
)
1328 device_uncache_fw_images();
1332 * device_uncache_fw_images_delay - uncache devices firmwares
1333 * @delay: number of milliseconds to delay uncache device firmwares
1335 * uncache all devices's firmwares which has been cached successfully
1336 * by device_cache_fw_images after @delay milliseconds.
1338 static void device_uncache_fw_images_delay(unsigned long delay
)
1340 schedule_delayed_work(&fw_cache
.work
,
1341 msecs_to_jiffies(delay
));
1344 static int fw_pm_notify(struct notifier_block
*notify_block
,
1345 unsigned long mode
, void *unused
)
1348 case PM_HIBERNATION_PREPARE
:
1349 case PM_SUSPEND_PREPARE
:
1350 device_cache_fw_images();
1353 case PM_POST_SUSPEND
:
1354 case PM_POST_HIBERNATION
:
1355 case PM_POST_RESTORE
:
1357 * In case that system sleep failed and syscore_suspend is
1360 mutex_lock(&fw_lock
);
1361 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1362 mutex_unlock(&fw_lock
);
1364 device_uncache_fw_images_delay(10 * MSEC_PER_SEC
);
1371 /* stop caching firmware once syscore_suspend is reached */
1372 static int fw_suspend(void)
1374 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1378 static struct syscore_ops fw_syscore_ops
= {
1379 .suspend
= fw_suspend
,
1382 static int fw_cache_piggyback_on_request(const char *name
)
1388 static void __init
fw_cache_init(void)
1390 spin_lock_init(&fw_cache
.lock
);
1391 INIT_LIST_HEAD(&fw_cache
.head
);
1392 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1394 #ifdef CONFIG_PM_SLEEP
1395 spin_lock_init(&fw_cache
.name_lock
);
1396 INIT_LIST_HEAD(&fw_cache
.fw_names
);
1399 init_waitqueue_head(&fw_cache
.wait_queue
);
1400 INIT_DELAYED_WORK(&fw_cache
.work
,
1401 device_uncache_fw_images_work
);
1403 fw_cache
.pm_notify
.notifier_call
= fw_pm_notify
;
1404 register_pm_notifier(&fw_cache
.pm_notify
);
1406 register_syscore_ops(&fw_syscore_ops
);
1410 static int __init
firmware_class_init(void)
1413 return class_register(&firmware_class
);
1416 static void __exit
firmware_class_exit(void)
1418 #ifdef CONFIG_PM_SLEEP
1419 unregister_syscore_ops(&fw_syscore_ops
);
1420 unregister_pm_notifier(&fw_cache
.pm_notify
);
1422 class_unregister(&firmware_class
);
1425 fs_initcall(firmware_class_init
);
1426 module_exit(firmware_class_exit
);
1428 EXPORT_SYMBOL(release_firmware
);
1429 EXPORT_SYMBOL(request_firmware
);
1430 EXPORT_SYMBOL(request_firmware_nowait
);
1431 EXPORT_SYMBOL_GPL(cache_firmware
);
1432 EXPORT_SYMBOL_GPL(uncache_firmware
);