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>
25 #define to_dev(obj) container_of(obj, struct device, kobj)
27 MODULE_AUTHOR("Manuel Estrada Sainz");
28 MODULE_DESCRIPTION("Multi purpose firmware loading support");
29 MODULE_LICENSE("GPL");
31 /* Builtin firmware support */
33 #ifdef CONFIG_FW_LOADER
35 extern struct builtin_fw __start_builtin_fw
[];
36 extern struct builtin_fw __end_builtin_fw
[];
38 static bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
40 struct builtin_fw
*b_fw
;
42 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
43 if (strcmp(name
, b_fw
->name
) == 0) {
44 fw
->size
= b_fw
->size
;
45 fw
->data
= b_fw
->data
;
53 static bool fw_is_builtin_firmware(const struct firmware
*fw
)
55 struct builtin_fw
*b_fw
;
57 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++)
58 if (fw
->data
== b_fw
->data
)
64 #else /* Module case - no builtin firmware support */
66 static inline bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
71 static inline bool fw_is_builtin_firmware(const struct firmware
*fw
)
83 static int loading_timeout
= 60; /* In seconds */
85 static inline long firmware_loading_timeout(void)
87 return loading_timeout
> 0 ? loading_timeout
* HZ
: MAX_SCHEDULE_TIMEOUT
;
90 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
91 * guarding for corner cases a global lock should be OK */
92 static DEFINE_MUTEX(fw_lock
);
94 struct firmware_priv
{
95 struct completion completion
;
101 struct timer_list timeout
;
107 static struct firmware_priv
*to_firmware_priv(struct device
*dev
)
109 return container_of(dev
, struct firmware_priv
, dev
);
112 static void fw_load_abort(struct firmware_priv
*fw_priv
)
114 set_bit(FW_STATUS_ABORT
, &fw_priv
->status
);
116 complete(&fw_priv
->completion
);
119 static ssize_t
firmware_timeout_show(struct class *class,
120 struct class_attribute
*attr
,
123 return sprintf(buf
, "%d\n", loading_timeout
);
127 * firmware_timeout_store - set number of seconds to wait for firmware
128 * @class: device class pointer
129 * @attr: device attribute pointer
130 * @buf: buffer to scan for timeout value
131 * @count: number of bytes in @buf
133 * Sets the number of seconds to wait for the firmware. Once
134 * this expires an error will be returned to the driver and no
135 * firmware will be provided.
137 * Note: zero means 'wait forever'.
139 static ssize_t
firmware_timeout_store(struct class *class,
140 struct class_attribute
*attr
,
141 const char *buf
, size_t count
)
143 loading_timeout
= simple_strtol(buf
, NULL
, 10);
144 if (loading_timeout
< 0)
150 static struct class_attribute firmware_class_attrs
[] = {
151 __ATTR(timeout
, S_IWUSR
| S_IRUGO
,
152 firmware_timeout_show
, firmware_timeout_store
),
156 static void fw_dev_release(struct device
*dev
)
158 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
161 for (i
= 0; i
< fw_priv
->nr_pages
; i
++)
162 __free_page(fw_priv
->pages
[i
]);
163 kfree(fw_priv
->pages
);
166 module_put(THIS_MODULE
);
169 static int firmware_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
171 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
173 if (add_uevent_var(env
, "FIRMWARE=%s", fw_priv
->fw_id
))
175 if (add_uevent_var(env
, "TIMEOUT=%i", loading_timeout
))
177 if (add_uevent_var(env
, "ASYNC=%d", fw_priv
->nowait
))
183 static struct class firmware_class
= {
185 .class_attrs
= firmware_class_attrs
,
186 .dev_uevent
= firmware_uevent
,
187 .dev_release
= fw_dev_release
,
190 static ssize_t
firmware_loading_show(struct device
*dev
,
191 struct device_attribute
*attr
, char *buf
)
193 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
194 int loading
= test_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
196 return sprintf(buf
, "%d\n", loading
);
199 static void firmware_free_data(const struct firmware
*fw
)
204 for (i
= 0; i
< PFN_UP(fw
->size
); i
++)
205 __free_page(fw
->pages
[i
]);
210 /* Some architectures don't have PAGE_KERNEL_RO */
211 #ifndef PAGE_KERNEL_RO
212 #define PAGE_KERNEL_RO PAGE_KERNEL
215 * firmware_loading_store - set value in the 'loading' control file
216 * @dev: device pointer
217 * @attr: device attribute pointer
218 * @buf: buffer to scan for loading control value
219 * @count: number of bytes in @buf
221 * The relevant values are:
223 * 1: Start a load, discarding any previous partial load.
224 * 0: Conclude the load and hand the data to the driver code.
225 * -1: Conclude the load with an error and discard any written data.
227 static ssize_t
firmware_loading_store(struct device
*dev
,
228 struct device_attribute
*attr
,
229 const char *buf
, size_t count
)
231 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
232 int loading
= simple_strtol(buf
, NULL
, 10);
235 mutex_lock(&fw_lock
);
242 firmware_free_data(fw_priv
->fw
);
243 memset(fw_priv
->fw
, 0, sizeof(struct firmware
));
244 /* If the pages are not owned by 'struct firmware' */
245 for (i
= 0; i
< fw_priv
->nr_pages
; i
++)
246 __free_page(fw_priv
->pages
[i
]);
247 kfree(fw_priv
->pages
);
248 fw_priv
->pages
= NULL
;
249 fw_priv
->page_array_size
= 0;
250 fw_priv
->nr_pages
= 0;
251 set_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
254 if (test_bit(FW_STATUS_LOADING
, &fw_priv
->status
)) {
255 vunmap(fw_priv
->fw
->data
);
256 fw_priv
->fw
->data
= vmap(fw_priv
->pages
,
259 if (!fw_priv
->fw
->data
) {
260 dev_err(dev
, "%s: vmap() failed\n", __func__
);
263 /* Pages are now owned by 'struct firmware' */
264 fw_priv
->fw
->pages
= fw_priv
->pages
;
265 fw_priv
->pages
= NULL
;
267 fw_priv
->page_array_size
= 0;
268 fw_priv
->nr_pages
= 0;
269 complete(&fw_priv
->completion
);
270 clear_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
275 dev_err(dev
, "%s: unexpected value (%d)\n", __func__
, loading
);
279 fw_load_abort(fw_priv
);
283 mutex_unlock(&fw_lock
);
287 static DEVICE_ATTR(loading
, 0644, firmware_loading_show
, firmware_loading_store
);
289 static ssize_t
firmware_data_read(struct file
*filp
, struct kobject
*kobj
,
290 struct bin_attribute
*bin_attr
,
291 char *buffer
, loff_t offset
, size_t count
)
293 struct device
*dev
= to_dev(kobj
);
294 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
298 mutex_lock(&fw_lock
);
300 if (!fw
|| test_bit(FW_STATUS_DONE
, &fw_priv
->status
)) {
304 if (offset
> fw
->size
) {
308 if (count
> fw
->size
- offset
)
309 count
= fw
->size
- offset
;
315 int page_nr
= offset
>> PAGE_SHIFT
;
316 int page_ofs
= offset
& (PAGE_SIZE
-1);
317 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
319 page_data
= kmap(fw_priv
->pages
[page_nr
]);
321 memcpy(buffer
, page_data
+ page_ofs
, page_cnt
);
323 kunmap(fw_priv
->pages
[page_nr
]);
329 mutex_unlock(&fw_lock
);
333 static int fw_realloc_buffer(struct firmware_priv
*fw_priv
, int min_size
)
335 int pages_needed
= ALIGN(min_size
, PAGE_SIZE
) >> PAGE_SHIFT
;
337 /* If the array of pages is too small, grow it... */
338 if (fw_priv
->page_array_size
< pages_needed
) {
339 int new_array_size
= max(pages_needed
,
340 fw_priv
->page_array_size
* 2);
341 struct page
**new_pages
;
343 new_pages
= kmalloc(new_array_size
* sizeof(void *),
346 fw_load_abort(fw_priv
);
349 memcpy(new_pages
, fw_priv
->pages
,
350 fw_priv
->page_array_size
* sizeof(void *));
351 memset(&new_pages
[fw_priv
->page_array_size
], 0, sizeof(void *) *
352 (new_array_size
- fw_priv
->page_array_size
));
353 kfree(fw_priv
->pages
);
354 fw_priv
->pages
= new_pages
;
355 fw_priv
->page_array_size
= new_array_size
;
358 while (fw_priv
->nr_pages
< pages_needed
) {
359 fw_priv
->pages
[fw_priv
->nr_pages
] =
360 alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
362 if (!fw_priv
->pages
[fw_priv
->nr_pages
]) {
363 fw_load_abort(fw_priv
);
372 * firmware_data_write - write method for firmware
373 * @filp: open sysfs file
374 * @kobj: kobject for the device
375 * @bin_attr: bin_attr structure
376 * @buffer: buffer being written
377 * @offset: buffer offset for write in total data store area
378 * @count: buffer size
380 * Data written to the 'data' attribute will be later handed to
381 * the driver as a firmware image.
383 static ssize_t
firmware_data_write(struct file
*filp
, struct kobject
*kobj
,
384 struct bin_attribute
*bin_attr
,
385 char *buffer
, loff_t offset
, size_t count
)
387 struct device
*dev
= to_dev(kobj
);
388 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
392 if (!capable(CAP_SYS_RAWIO
))
395 mutex_lock(&fw_lock
);
397 if (!fw
|| test_bit(FW_STATUS_DONE
, &fw_priv
->status
)) {
401 retval
= fw_realloc_buffer(fw_priv
, offset
+ count
);
409 int page_nr
= offset
>> PAGE_SHIFT
;
410 int page_ofs
= offset
& (PAGE_SIZE
- 1);
411 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
413 page_data
= kmap(fw_priv
->pages
[page_nr
]);
415 memcpy(page_data
+ page_ofs
, buffer
, page_cnt
);
417 kunmap(fw_priv
->pages
[page_nr
]);
423 fw
->size
= max_t(size_t, offset
, fw
->size
);
425 mutex_unlock(&fw_lock
);
429 static struct bin_attribute firmware_attr_data
= {
430 .attr
= { .name
= "data", .mode
= 0644 },
432 .read
= firmware_data_read
,
433 .write
= firmware_data_write
,
436 static void firmware_class_timeout(u_long data
)
438 struct firmware_priv
*fw_priv
= (struct firmware_priv
*) data
;
440 fw_load_abort(fw_priv
);
443 static struct firmware_priv
*
444 fw_create_instance(struct firmware
*firmware
, const char *fw_name
,
445 struct device
*device
, bool uevent
, bool nowait
)
447 struct firmware_priv
*fw_priv
;
448 struct device
*f_dev
;
450 fw_priv
= kzalloc(sizeof(*fw_priv
) + strlen(fw_name
) + 1 , GFP_KERNEL
);
452 dev_err(device
, "%s: kmalloc failed\n", __func__
);
453 return ERR_PTR(-ENOMEM
);
456 fw_priv
->fw
= firmware
;
457 fw_priv
->nowait
= nowait
;
458 strcpy(fw_priv
->fw_id
, fw_name
);
459 init_completion(&fw_priv
->completion
);
460 setup_timer(&fw_priv
->timeout
,
461 firmware_class_timeout
, (u_long
) fw_priv
);
463 f_dev
= &fw_priv
->dev
;
465 device_initialize(f_dev
);
466 dev_set_name(f_dev
, "%s", dev_name(device
));
467 f_dev
->parent
= device
;
468 f_dev
->class = &firmware_class
;
473 static struct firmware_priv
*
474 _request_firmware_prepare(const struct firmware
**firmware_p
, const char *name
,
475 struct device
*device
, bool uevent
, bool nowait
)
477 struct firmware
*firmware
;
478 struct firmware_priv
*fw_priv
;
481 return ERR_PTR(-EINVAL
);
483 *firmware_p
= firmware
= kzalloc(sizeof(*firmware
), GFP_KERNEL
);
485 dev_err(device
, "%s: kmalloc(struct firmware) failed\n",
487 return ERR_PTR(-ENOMEM
);
490 if (fw_get_builtin_firmware(firmware
, name
)) {
491 dev_dbg(device
, "firmware: using built-in firmware %s\n", name
);
495 fw_priv
= fw_create_instance(firmware
, name
, device
, uevent
, nowait
);
496 if (IS_ERR(fw_priv
)) {
497 release_firmware(firmware
);
503 static void _request_firmware_cleanup(const struct firmware
**firmware_p
)
505 release_firmware(*firmware_p
);
509 static int _request_firmware_load(struct firmware_priv
*fw_priv
, bool uevent
,
513 struct device
*f_dev
= &fw_priv
->dev
;
515 dev_set_uevent_suppress(f_dev
, true);
517 /* Need to pin this module until class device is destroyed */
518 __module_get(THIS_MODULE
);
520 retval
= device_add(f_dev
);
522 dev_err(f_dev
, "%s: device_register failed\n", __func__
);
526 retval
= device_create_bin_file(f_dev
, &firmware_attr_data
);
528 dev_err(f_dev
, "%s: sysfs_create_bin_file failed\n", __func__
);
532 retval
= device_create_file(f_dev
, &dev_attr_loading
);
534 dev_err(f_dev
, "%s: device_create_file failed\n", __func__
);
535 goto err_del_bin_attr
;
539 dev_set_uevent_suppress(f_dev
, false);
540 dev_dbg(f_dev
, "firmware: requesting %s\n", fw_priv
->fw_id
);
541 if (timeout
!= MAX_SCHEDULE_TIMEOUT
)
542 mod_timer(&fw_priv
->timeout
,
543 round_jiffies_up(jiffies
+ timeout
));
545 kobject_uevent(&fw_priv
->dev
.kobj
, KOBJ_ADD
);
548 wait_for_completion(&fw_priv
->completion
);
550 set_bit(FW_STATUS_DONE
, &fw_priv
->status
);
551 del_timer_sync(&fw_priv
->timeout
);
553 mutex_lock(&fw_lock
);
554 if (!fw_priv
->fw
->size
|| test_bit(FW_STATUS_ABORT
, &fw_priv
->status
))
557 mutex_unlock(&fw_lock
);
559 device_remove_file(f_dev
, &dev_attr_loading
);
561 device_remove_bin_file(f_dev
, &firmware_attr_data
);
570 * request_firmware: - send firmware request and wait for it
571 * @firmware_p: pointer to firmware image
572 * @name: name of firmware file
573 * @device: device for which firmware is being loaded
575 * @firmware_p will be used to return a firmware image by the name
576 * of @name for device @device.
578 * Should be called from user context where sleeping is allowed.
580 * @name will be used as $FIRMWARE in the uevent environment and
581 * should be distinctive enough not to be confused with any other
582 * firmware image for this or any other device.
585 request_firmware(const struct firmware
**firmware_p
, const char *name
,
586 struct device
*device
)
588 struct firmware_priv
*fw_priv
;
591 fw_priv
= _request_firmware_prepare(firmware_p
, name
, device
, true,
593 if (IS_ERR_OR_NULL(fw_priv
))
594 return PTR_RET(fw_priv
);
596 ret
= usermodehelper_read_trylock();
598 dev_err(device
, "firmware: %s will not be loaded\n", name
);
600 ret
= _request_firmware_load(fw_priv
, true,
601 firmware_loading_timeout());
602 usermodehelper_read_unlock();
605 _request_firmware_cleanup(firmware_p
);
611 * release_firmware: - release the resource associated with a firmware image
612 * @fw: firmware resource to release
614 void release_firmware(const struct firmware
*fw
)
617 if (!fw_is_builtin_firmware(fw
))
618 firmware_free_data(fw
);
624 struct firmware_work
{
625 struct work_struct work
;
626 struct module
*module
;
628 struct device
*device
;
630 void (*cont
)(const struct firmware
*fw
, void *context
);
634 static void request_firmware_work_func(struct work_struct
*work
)
636 struct firmware_work
*fw_work
;
637 const struct firmware
*fw
;
638 struct firmware_priv
*fw_priv
;
642 fw_work
= container_of(work
, struct firmware_work
, work
);
643 fw_priv
= _request_firmware_prepare(&fw
, fw_work
->name
, fw_work
->device
,
644 fw_work
->uevent
, true);
645 if (IS_ERR_OR_NULL(fw_priv
)) {
646 ret
= PTR_RET(fw_priv
);
650 timeout
= usermodehelper_read_lock_wait(firmware_loading_timeout());
652 ret
= _request_firmware_load(fw_priv
, fw_work
->uevent
, timeout
);
653 usermodehelper_read_unlock();
655 dev_dbg(fw_work
->device
, "firmware: %s loading timed out\n",
660 _request_firmware_cleanup(&fw
);
663 fw_work
->cont(fw
, fw_work
->context
);
665 module_put(fw_work
->module
);
670 * request_firmware_nowait - asynchronous version of request_firmware
671 * @module: module requesting the firmware
672 * @uevent: sends uevent to copy the firmware image if this flag
673 * is non-zero else the firmware copy must be done manually.
674 * @name: name of firmware file
675 * @device: device for which firmware is being loaded
676 * @gfp: allocation flags
677 * @context: will be passed over to @cont, and
678 * @fw may be %NULL if firmware request fails.
679 * @cont: function will be called asynchronously when the firmware
682 * Asynchronous variant of request_firmware() for user contexts where
683 * it is not possible to sleep for long time. It can't be called
684 * in atomic contexts.
687 request_firmware_nowait(
688 struct module
*module
, bool uevent
,
689 const char *name
, struct device
*device
, gfp_t gfp
, void *context
,
690 void (*cont
)(const struct firmware
*fw
, void *context
))
692 struct firmware_work
*fw_work
;
694 fw_work
= kzalloc(sizeof (struct firmware_work
), gfp
);
698 fw_work
->module
= module
;
699 fw_work
->name
= name
;
700 fw_work
->device
= device
;
701 fw_work
->context
= context
;
702 fw_work
->cont
= cont
;
703 fw_work
->uevent
= uevent
;
705 if (!try_module_get(module
)) {
710 INIT_WORK(&fw_work
->work
, request_firmware_work_func
);
711 schedule_work(&fw_work
->work
);
715 static int __init
firmware_class_init(void)
717 return class_register(&firmware_class
);
720 static void __exit
firmware_class_exit(void)
722 class_unregister(&firmware_class
);
725 fs_initcall(firmware_class_init
);
726 module_exit(firmware_class_exit
);
728 EXPORT_SYMBOL(release_firmware
);
729 EXPORT_SYMBOL(request_firmware
);
730 EXPORT_SYMBOL(request_firmware_nowait
);