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/kthread.h>
20 #include <linux/highmem.h>
21 #include <linux/firmware.h>
22 #include <linux/slab.h>
24 #define to_dev(obj) container_of(obj, struct device, kobj)
26 MODULE_AUTHOR("Manuel Estrada Sainz");
27 MODULE_DESCRIPTION("Multi purpose firmware loading support");
28 MODULE_LICENSE("GPL");
30 /* Builtin firmware support */
32 #ifdef CONFIG_FW_LOADER
34 extern struct builtin_fw __start_builtin_fw
[];
35 extern struct builtin_fw __end_builtin_fw
[];
37 static bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
39 struct builtin_fw
*b_fw
;
41 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
42 if (strcmp(name
, b_fw
->name
) == 0) {
43 fw
->size
= b_fw
->size
;
44 fw
->data
= b_fw
->data
;
52 static bool fw_is_builtin_firmware(const struct firmware
*fw
)
54 struct builtin_fw
*b_fw
;
56 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++)
57 if (fw
->data
== b_fw
->data
)
63 #else /* Module case - no builtin firmware support */
65 static inline bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
70 static inline bool fw_is_builtin_firmware(const struct firmware
*fw
)
82 static int loading_timeout
= 60; /* In seconds */
84 static inline long firmware_loading_timeout(void)
86 return loading_timeout
> 0 ? loading_timeout
* HZ
: MAX_SCHEDULE_TIMEOUT
;
89 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
90 * guarding for corner cases a global lock should be OK */
91 static DEFINE_MUTEX(fw_lock
);
93 struct firmware_priv
{
94 struct completion completion
;
100 struct timer_list timeout
;
106 static struct firmware_priv
*to_firmware_priv(struct device
*dev
)
108 return container_of(dev
, struct firmware_priv
, dev
);
111 static void fw_load_abort(struct firmware_priv
*fw_priv
)
113 set_bit(FW_STATUS_ABORT
, &fw_priv
->status
);
115 complete(&fw_priv
->completion
);
118 static ssize_t
firmware_timeout_show(struct class *class,
119 struct class_attribute
*attr
,
122 return sprintf(buf
, "%d\n", loading_timeout
);
126 * firmware_timeout_store - set number of seconds to wait for firmware
127 * @class: device class pointer
128 * @attr: device attribute pointer
129 * @buf: buffer to scan for timeout value
130 * @count: number of bytes in @buf
132 * Sets the number of seconds to wait for the firmware. Once
133 * this expires an error will be returned to the driver and no
134 * firmware will be provided.
136 * Note: zero means 'wait forever'.
138 static ssize_t
firmware_timeout_store(struct class *class,
139 struct class_attribute
*attr
,
140 const char *buf
, size_t count
)
142 loading_timeout
= simple_strtol(buf
, NULL
, 10);
143 if (loading_timeout
< 0)
149 static struct class_attribute firmware_class_attrs
[] = {
150 __ATTR(timeout
, S_IWUSR
| S_IRUGO
,
151 firmware_timeout_show
, firmware_timeout_store
),
155 static void fw_dev_release(struct device
*dev
)
157 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
160 for (i
= 0; i
< fw_priv
->nr_pages
; i
++)
161 __free_page(fw_priv
->pages
[i
]);
162 kfree(fw_priv
->pages
);
165 module_put(THIS_MODULE
);
168 static int firmware_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
170 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
172 if (add_uevent_var(env
, "FIRMWARE=%s", fw_priv
->fw_id
))
174 if (add_uevent_var(env
, "TIMEOUT=%i", loading_timeout
))
176 if (add_uevent_var(env
, "ASYNC=%d", fw_priv
->nowait
))
182 static struct class firmware_class
= {
184 .class_attrs
= firmware_class_attrs
,
185 .dev_uevent
= firmware_uevent
,
186 .dev_release
= fw_dev_release
,
189 static ssize_t
firmware_loading_show(struct device
*dev
,
190 struct device_attribute
*attr
, char *buf
)
192 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
193 int loading
= test_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
195 return sprintf(buf
, "%d\n", loading
);
198 static void firmware_free_data(const struct firmware
*fw
)
203 for (i
= 0; i
< PFN_UP(fw
->size
); i
++)
204 __free_page(fw
->pages
[i
]);
209 /* Some architectures don't have PAGE_KERNEL_RO */
210 #ifndef PAGE_KERNEL_RO
211 #define PAGE_KERNEL_RO PAGE_KERNEL
214 * firmware_loading_store - set value in the 'loading' control file
215 * @dev: device pointer
216 * @attr: device attribute pointer
217 * @buf: buffer to scan for loading control value
218 * @count: number of bytes in @buf
220 * The relevant values are:
222 * 1: Start a load, discarding any previous partial load.
223 * 0: Conclude the load and hand the data to the driver code.
224 * -1: Conclude the load with an error and discard any written data.
226 static ssize_t
firmware_loading_store(struct device
*dev
,
227 struct device_attribute
*attr
,
228 const char *buf
, size_t count
)
230 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
231 int loading
= simple_strtol(buf
, NULL
, 10);
234 mutex_lock(&fw_lock
);
241 firmware_free_data(fw_priv
->fw
);
242 memset(fw_priv
->fw
, 0, sizeof(struct firmware
));
243 /* If the pages are not owned by 'struct firmware' */
244 for (i
= 0; i
< fw_priv
->nr_pages
; i
++)
245 __free_page(fw_priv
->pages
[i
]);
246 kfree(fw_priv
->pages
);
247 fw_priv
->pages
= NULL
;
248 fw_priv
->page_array_size
= 0;
249 fw_priv
->nr_pages
= 0;
250 set_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
253 if (test_bit(FW_STATUS_LOADING
, &fw_priv
->status
)) {
254 vunmap(fw_priv
->fw
->data
);
255 fw_priv
->fw
->data
= vmap(fw_priv
->pages
,
258 if (!fw_priv
->fw
->data
) {
259 dev_err(dev
, "%s: vmap() failed\n", __func__
);
262 /* Pages are now owned by 'struct firmware' */
263 fw_priv
->fw
->pages
= fw_priv
->pages
;
264 fw_priv
->pages
= NULL
;
266 fw_priv
->page_array_size
= 0;
267 fw_priv
->nr_pages
= 0;
268 complete(&fw_priv
->completion
);
269 clear_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
274 dev_err(dev
, "%s: unexpected value (%d)\n", __func__
, loading
);
278 fw_load_abort(fw_priv
);
282 mutex_unlock(&fw_lock
);
286 static DEVICE_ATTR(loading
, 0644, firmware_loading_show
, firmware_loading_store
);
288 static ssize_t
firmware_data_read(struct file
*filp
, struct kobject
*kobj
,
289 struct bin_attribute
*bin_attr
,
290 char *buffer
, loff_t offset
, size_t count
)
292 struct device
*dev
= to_dev(kobj
);
293 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
297 mutex_lock(&fw_lock
);
299 if (!fw
|| test_bit(FW_STATUS_DONE
, &fw_priv
->status
)) {
303 if (offset
> fw
->size
) {
307 if (count
> fw
->size
- offset
)
308 count
= fw
->size
- offset
;
314 int page_nr
= offset
>> PAGE_SHIFT
;
315 int page_ofs
= offset
& (PAGE_SIZE
-1);
316 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
318 page_data
= kmap(fw_priv
->pages
[page_nr
]);
320 memcpy(buffer
, page_data
+ page_ofs
, page_cnt
);
322 kunmap(fw_priv
->pages
[page_nr
]);
328 mutex_unlock(&fw_lock
);
332 static int fw_realloc_buffer(struct firmware_priv
*fw_priv
, int min_size
)
334 int pages_needed
= ALIGN(min_size
, PAGE_SIZE
) >> PAGE_SHIFT
;
336 /* If the array of pages is too small, grow it... */
337 if (fw_priv
->page_array_size
< pages_needed
) {
338 int new_array_size
= max(pages_needed
,
339 fw_priv
->page_array_size
* 2);
340 struct page
**new_pages
;
342 new_pages
= kmalloc(new_array_size
* sizeof(void *),
345 fw_load_abort(fw_priv
);
348 memcpy(new_pages
, fw_priv
->pages
,
349 fw_priv
->page_array_size
* sizeof(void *));
350 memset(&new_pages
[fw_priv
->page_array_size
], 0, sizeof(void *) *
351 (new_array_size
- fw_priv
->page_array_size
));
352 kfree(fw_priv
->pages
);
353 fw_priv
->pages
= new_pages
;
354 fw_priv
->page_array_size
= new_array_size
;
357 while (fw_priv
->nr_pages
< pages_needed
) {
358 fw_priv
->pages
[fw_priv
->nr_pages
] =
359 alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
361 if (!fw_priv
->pages
[fw_priv
->nr_pages
]) {
362 fw_load_abort(fw_priv
);
371 * firmware_data_write - write method for firmware
372 * @filp: open sysfs file
373 * @kobj: kobject for the device
374 * @bin_attr: bin_attr structure
375 * @buffer: buffer being written
376 * @offset: buffer offset for write in total data store area
377 * @count: buffer size
379 * Data written to the 'data' attribute will be later handed to
380 * the driver as a firmware image.
382 static ssize_t
firmware_data_write(struct file
*filp
, struct kobject
*kobj
,
383 struct bin_attribute
*bin_attr
,
384 char *buffer
, loff_t offset
, size_t count
)
386 struct device
*dev
= to_dev(kobj
);
387 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
391 if (!capable(CAP_SYS_RAWIO
))
394 mutex_lock(&fw_lock
);
396 if (!fw
|| test_bit(FW_STATUS_DONE
, &fw_priv
->status
)) {
400 retval
= fw_realloc_buffer(fw_priv
, offset
+ count
);
408 int page_nr
= offset
>> PAGE_SHIFT
;
409 int page_ofs
= offset
& (PAGE_SIZE
- 1);
410 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
412 page_data
= kmap(fw_priv
->pages
[page_nr
]);
414 memcpy(page_data
+ page_ofs
, buffer
, page_cnt
);
416 kunmap(fw_priv
->pages
[page_nr
]);
422 fw
->size
= max_t(size_t, offset
, fw
->size
);
424 mutex_unlock(&fw_lock
);
428 static struct bin_attribute firmware_attr_data
= {
429 .attr
= { .name
= "data", .mode
= 0644 },
431 .read
= firmware_data_read
,
432 .write
= firmware_data_write
,
435 static void firmware_class_timeout(u_long data
)
437 struct firmware_priv
*fw_priv
= (struct firmware_priv
*) data
;
439 fw_load_abort(fw_priv
);
442 static struct firmware_priv
*
443 fw_create_instance(const struct firmware
*firmware
, const char *fw_name
,
444 struct device
*device
, bool uevent
, bool nowait
)
446 struct firmware_priv
*fw_priv
;
447 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__
);
457 fw_priv
->fw
= (struct firmware
*)firmware
;
458 fw_priv
->nowait
= nowait
;
459 strcpy(fw_priv
->fw_id
, fw_name
);
460 init_completion(&fw_priv
->completion
);
461 setup_timer(&fw_priv
->timeout
,
462 firmware_class_timeout
, (u_long
) fw_priv
);
464 f_dev
= &fw_priv
->dev
;
466 device_initialize(f_dev
);
467 dev_set_name(f_dev
, "%s", dev_name(device
));
468 f_dev
->parent
= device
;
469 f_dev
->class = &firmware_class
;
471 dev_set_uevent_suppress(f_dev
, true);
473 /* Need to pin this module until class device is destroyed */
474 __module_get(THIS_MODULE
);
476 error
= device_add(f_dev
);
478 dev_err(device
, "%s: device_register failed\n", __func__
);
482 error
= device_create_bin_file(f_dev
, &firmware_attr_data
);
484 dev_err(device
, "%s: sysfs_create_bin_file failed\n", __func__
);
488 error
= device_create_file(f_dev
, &dev_attr_loading
);
490 dev_err(device
, "%s: device_create_file failed\n", __func__
);
491 goto err_del_bin_attr
;
495 dev_set_uevent_suppress(f_dev
, false);
500 device_remove_bin_file(f_dev
, &firmware_attr_data
);
506 return ERR_PTR(error
);
509 static void fw_destroy_instance(struct firmware_priv
*fw_priv
)
511 struct device
*f_dev
= &fw_priv
->dev
;
513 device_remove_file(f_dev
, &dev_attr_loading
);
514 device_remove_bin_file(f_dev
, &firmware_attr_data
);
515 device_unregister(f_dev
);
518 static int _request_firmware_prepare(const struct firmware
**firmware_p
,
519 const char *name
, struct device
*device
)
521 struct firmware
*firmware
;
526 *firmware_p
= firmware
= kzalloc(sizeof(*firmware
), GFP_KERNEL
);
528 dev_err(device
, "%s: kmalloc(struct firmware) failed\n",
533 if (fw_get_builtin_firmware(firmware
, name
)) {
534 dev_dbg(device
, "firmware: using built-in firmware %s\n", name
);
541 static void _request_firmware_cleanup(const struct firmware
**firmware_p
)
543 release_firmware(*firmware_p
);
547 static int _request_firmware(const struct firmware
*firmware
,
548 const char *name
, struct device
*device
,
549 bool uevent
, bool nowait
, long timeout
)
551 struct firmware_priv
*fw_priv
;
555 dev_dbg(device
, "firmware: requesting %s\n", name
);
557 fw_priv
= fw_create_instance(firmware
, name
, device
, uevent
, nowait
);
559 return PTR_ERR(fw_priv
);
562 if (timeout
!= MAX_SCHEDULE_TIMEOUT
)
563 mod_timer(&fw_priv
->timeout
,
564 round_jiffies_up(jiffies
+ timeout
));
566 kobject_uevent(&fw_priv
->dev
.kobj
, KOBJ_ADD
);
569 wait_for_completion(&fw_priv
->completion
);
571 set_bit(FW_STATUS_DONE
, &fw_priv
->status
);
572 del_timer_sync(&fw_priv
->timeout
);
574 mutex_lock(&fw_lock
);
575 if (!fw_priv
->fw
->size
|| test_bit(FW_STATUS_ABORT
, &fw_priv
->status
))
578 mutex_unlock(&fw_lock
);
580 fw_destroy_instance(fw_priv
);
585 * request_firmware: - send firmware request and wait for it
586 * @firmware_p: pointer to firmware image
587 * @name: name of firmware file
588 * @device: device for which firmware is being loaded
590 * @firmware_p will be used to return a firmware image by the name
591 * of @name for device @device.
593 * Should be called from user context where sleeping is allowed.
595 * @name will be used as $FIRMWARE in the uevent environment and
596 * should be distinctive enough not to be confused with any other
597 * firmware image for this or any other device.
600 request_firmware(const struct firmware
**firmware_p
, const char *name
,
601 struct device
*device
)
605 ret
= _request_firmware_prepare(firmware_p
, name
, device
);
609 ret
= usermodehelper_read_trylock();
611 dev_err(device
, "firmware: %s will not be loaded\n", name
);
613 ret
= _request_firmware(*firmware_p
, name
, device
, true, false,
614 firmware_loading_timeout());
615 usermodehelper_read_unlock();
618 _request_firmware_cleanup(firmware_p
);
624 * release_firmware: - release the resource associated with a firmware image
625 * @fw: firmware resource to release
627 void release_firmware(const struct firmware
*fw
)
630 if (!fw_is_builtin_firmware(fw
))
631 firmware_free_data(fw
);
637 struct firmware_work
{
638 struct work_struct work
;
639 struct module
*module
;
641 struct device
*device
;
643 void (*cont
)(const struct firmware
*fw
, void *context
);
647 static int request_firmware_work_func(void *arg
)
649 struct firmware_work
*fw_work
= arg
;
650 const struct firmware
*fw
;
659 ret
= _request_firmware_prepare(&fw
, fw_work
->name
, fw_work
->device
);
663 timeout
= usermodehelper_read_lock_wait(firmware_loading_timeout());
665 ret
= _request_firmware(fw
, fw_work
->name
, fw_work
->device
,
666 fw_work
->uevent
, true, timeout
);
667 usermodehelper_read_unlock();
669 dev_dbg(fw_work
->device
, "firmware: %s loading timed out\n",
674 _request_firmware_cleanup(&fw
);
677 fw_work
->cont(fw
, fw_work
->context
);
679 module_put(fw_work
->module
);
686 * request_firmware_nowait - asynchronous version of request_firmware
687 * @module: module requesting the firmware
688 * @uevent: sends uevent to copy the firmware image if this flag
689 * is non-zero else the firmware copy must be done manually.
690 * @name: name of firmware file
691 * @device: device for which firmware is being loaded
692 * @gfp: allocation flags
693 * @context: will be passed over to @cont, and
694 * @fw may be %NULL if firmware request fails.
695 * @cont: function will be called asynchronously when the firmware
698 * Asynchronous variant of request_firmware() for user contexts where
699 * it is not possible to sleep for long time. It can't be called
700 * in atomic contexts.
703 request_firmware_nowait(
704 struct module
*module
, bool uevent
,
705 const char *name
, struct device
*device
, gfp_t gfp
, void *context
,
706 void (*cont
)(const struct firmware
*fw
, void *context
))
708 struct task_struct
*task
;
709 struct firmware_work
*fw_work
;
711 fw_work
= kzalloc(sizeof (struct firmware_work
), gfp
);
715 fw_work
->module
= module
;
716 fw_work
->name
= name
;
717 fw_work
->device
= device
;
718 fw_work
->context
= context
;
719 fw_work
->cont
= cont
;
720 fw_work
->uevent
= uevent
;
722 if (!try_module_get(module
)) {
727 task
= kthread_run(request_firmware_work_func
, fw_work
,
728 "firmware/%s", name
);
730 fw_work
->cont(NULL
, fw_work
->context
);
731 module_put(fw_work
->module
);
733 return PTR_ERR(task
);
739 static int __init
firmware_class_init(void)
741 return class_register(&firmware_class
);
744 static void __exit
firmware_class_exit(void)
746 class_unregister(&firmware_class
);
749 fs_initcall(firmware_class_init
);
750 module_exit(firmware_class_exit
);
752 EXPORT_SYMBOL(release_firmware
);
753 EXPORT_SYMBOL(request_firmware
);
754 EXPORT_SYMBOL(request_firmware_nowait
);