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 #define to_dev(obj) container_of(obj, struct device, kobj)
37 MODULE_AUTHOR("Manuel Estrada Sainz");
38 MODULE_DESCRIPTION("Multi purpose firmware loading support");
39 MODULE_LICENSE("GPL");
41 static const char *fw_path[] = {
42 "/lib/firmware/updates/" UTS_RELEASE,
43 "/lib/firmware/updates",
44 "/lib/firmware/" UTS_RELEASE,
48 /* Don't inline this: 'struct kstat' is biggish */
49 static noinline long fw_file_size(struct file *file)
52 if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
54 if (!S_ISREG(st.mode))
56 if (st.size != (long)st.size)
61 static bool fw_read_file_contents(struct file *file, struct firmware *fw)
66 size = fw_file_size(file);
72 if (kernel_read(file, 0, buf, size) != size) {
81 static bool fw_get_filesystem_firmware(struct firmware *fw, const char *name)
85 char *path = __getname();
87 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
89 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], name);
91 file = filp_open(path, O_RDONLY, 0);
94 success = fw_read_file_contents(file, fw);
103 /* Builtin firmware support */
105 #ifdef CONFIG_FW_LOADER
107 extern struct builtin_fw __start_builtin_fw[];
108 extern struct builtin_fw __end_builtin_fw[];
110 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
112 struct builtin_fw *b_fw;
114 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
115 if (strcmp(name, b_fw->name) == 0) {
116 fw->size = b_fw->size;
117 fw->data = b_fw->data;
125 static bool fw_is_builtin_firmware(const struct firmware *fw)
127 struct builtin_fw *b_fw;
129 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
130 if (fw->data == b_fw->data)
136 #else /* Module case - no builtin firmware support */
138 static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
143 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
155 static int loading_timeout = 60; /* In seconds */
157 static inline long firmware_loading_timeout(void)
159 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
162 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
163 * guarding for corner cases a global lock should be OK */
164 static DEFINE_MUTEX(fw_lock);
166 struct firmware_priv {
167 struct completion completion;
169 unsigned long status;
173 struct timer_list timeout;
179 static struct firmware_priv *to_firmware_priv(struct device *dev)
181 return container_of(dev, struct firmware_priv, dev);
184 static void fw_load_abort(struct firmware_priv *fw_priv)
186 set_bit(FW_STATUS_ABORT, &fw_priv->status);
188 complete(&fw_priv->completion);
191 static ssize_t firmware_timeout_show(struct class *class,
192 struct class_attribute *attr,
195 return sprintf(buf, "%d\n", loading_timeout);
199 * firmware_timeout_store - set number of seconds to wait for firmware
200 * @class: device class pointer
201 * @attr: device attribute pointer
202 * @buf: buffer to scan for timeout value
203 * @count: number of bytes in @buf
205 * Sets the number of seconds to wait for the firmware. Once
206 * this expires an error will be returned to the driver and no
207 * firmware will be provided.
209 * Note: zero means 'wait forever'.
211 static ssize_t firmware_timeout_store(struct class *class,
212 struct class_attribute *attr,
213 const char *buf, size_t count)
215 loading_timeout = simple_strtol(buf, NULL, 10);
216 if (loading_timeout < 0)
222 static struct class_attribute firmware_class_attrs[] = {
223 __ATTR(timeout, S_IWUSR | S_IRUGO,
224 firmware_timeout_show, firmware_timeout_store),
228 static void fw_dev_release(struct device *dev)
230 struct firmware_priv *fw_priv = to_firmware_priv(dev);
233 for (i = 0; i < fw_priv->nr_pages; i++)
234 __free_page(fw_priv->pages[i]);
235 kfree(fw_priv->pages);
238 module_put(THIS_MODULE);
241 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
243 struct firmware_priv *fw_priv = to_firmware_priv(dev);
245 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
247 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
249 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
255 static struct class firmware_class = {
257 .class_attrs = firmware_class_attrs,
258 .dev_uevent = firmware_uevent,
259 .dev_release = fw_dev_release,
262 static ssize_t firmware_loading_show(struct device *dev,
263 struct device_attribute *attr, char *buf)
265 struct firmware_priv *fw_priv = to_firmware_priv(dev);
266 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
268 return sprintf(buf, "%d\n", loading);
271 static void firmware_free_data(const struct firmware *fw)
276 for (i = 0; i < PFN_UP(fw->size); i++)
277 __free_page(fw->pages[i]);
279 /* Loaded directly? */
285 /* Some architectures don't have PAGE_KERNEL_RO */
286 #ifndef PAGE_KERNEL_RO
287 #define PAGE_KERNEL_RO PAGE_KERNEL
290 * firmware_loading_store - set value in the 'loading' control file
291 * @dev: device pointer
292 * @attr: device attribute pointer
293 * @buf: buffer to scan for loading control value
294 * @count: number of bytes in @buf
296 * The relevant values are:
298 * 1: Start a load, discarding any previous partial load.
299 * 0: Conclude the load and hand the data to the driver code.
300 * -1: Conclude the load with an error and discard any written data.
302 static ssize_t firmware_loading_store(struct device *dev,
303 struct device_attribute *attr,
304 const char *buf, size_t count)
306 struct firmware_priv *fw_priv = to_firmware_priv(dev);
307 int loading = simple_strtol(buf, NULL, 10);
310 mutex_lock(&fw_lock);
317 firmware_free_data(fw_priv->fw);
318 memset(fw_priv->fw, 0, sizeof(struct firmware));
319 /* If the pages are not owned by 'struct firmware' */
320 for (i = 0; i < fw_priv->nr_pages; i++)
321 __free_page(fw_priv->pages[i]);
322 kfree(fw_priv->pages);
323 fw_priv->pages = NULL;
324 fw_priv->page_array_size = 0;
325 fw_priv->nr_pages = 0;
326 set_bit(FW_STATUS_LOADING, &fw_priv->status);
329 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
330 vunmap(fw_priv->fw->data);
331 fw_priv->fw->data = vmap(fw_priv->pages,
334 if (!fw_priv->fw->data) {
335 dev_err(dev, "%s: vmap() failed\n", __func__);
338 /* Pages are now owned by 'struct firmware' */
339 fw_priv->fw->pages = fw_priv->pages;
340 fw_priv->pages = NULL;
342 fw_priv->page_array_size = 0;
343 fw_priv->nr_pages = 0;
344 complete(&fw_priv->completion);
345 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
350 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
354 fw_load_abort(fw_priv);
358 mutex_unlock(&fw_lock);
362 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
364 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
365 struct bin_attribute *bin_attr,
366 char *buffer, loff_t offset, size_t count)
368 struct device *dev = to_dev(kobj);
369 struct firmware_priv *fw_priv = to_firmware_priv(dev);
373 mutex_lock(&fw_lock);
375 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
379 if (offset > fw->size) {
383 if (count > fw->size - offset)
384 count = fw->size - offset;
390 int page_nr = offset >> PAGE_SHIFT;
391 int page_ofs = offset & (PAGE_SIZE-1);
392 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
394 page_data = kmap(fw_priv->pages[page_nr]);
396 memcpy(buffer, page_data + page_ofs, page_cnt);
398 kunmap(fw_priv->pages[page_nr]);
404 mutex_unlock(&fw_lock);
408 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
410 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
412 /* If the array of pages is too small, grow it... */
413 if (fw_priv->page_array_size < pages_needed) {
414 int new_array_size = max(pages_needed,
415 fw_priv->page_array_size * 2);
416 struct page **new_pages;
418 new_pages = kmalloc(new_array_size * sizeof(void *),
421 fw_load_abort(fw_priv);
424 memcpy(new_pages, fw_priv->pages,
425 fw_priv->page_array_size * sizeof(void *));
426 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
427 (new_array_size - fw_priv->page_array_size));
428 kfree(fw_priv->pages);
429 fw_priv->pages = new_pages;
430 fw_priv->page_array_size = new_array_size;
433 while (fw_priv->nr_pages < pages_needed) {
434 fw_priv->pages[fw_priv->nr_pages] =
435 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
437 if (!fw_priv->pages[fw_priv->nr_pages]) {
438 fw_load_abort(fw_priv);
447 * firmware_data_write - write method for firmware
448 * @filp: open sysfs file
449 * @kobj: kobject for the device
450 * @bin_attr: bin_attr structure
451 * @buffer: buffer being written
452 * @offset: buffer offset for write in total data store area
453 * @count: buffer size
455 * Data written to the 'data' attribute will be later handed to
456 * the driver as a firmware image.
458 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
459 struct bin_attribute *bin_attr,
460 char *buffer, loff_t offset, size_t count)
462 struct device *dev = to_dev(kobj);
463 struct firmware_priv *fw_priv = to_firmware_priv(dev);
467 if (!capable(CAP_SYS_RAWIO))
470 mutex_lock(&fw_lock);
472 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
476 retval = fw_realloc_buffer(fw_priv, offset + count);
484 int page_nr = offset >> PAGE_SHIFT;
485 int page_ofs = offset & (PAGE_SIZE - 1);
486 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
488 page_data = kmap(fw_priv->pages[page_nr]);
490 memcpy(page_data + page_ofs, buffer, page_cnt);
492 kunmap(fw_priv->pages[page_nr]);
498 fw->size = max_t(size_t, offset, fw->size);
500 mutex_unlock(&fw_lock);
504 static struct bin_attribute firmware_attr_data = {
505 .attr = { .name = "data", .mode = 0644 },
507 .read = firmware_data_read,
508 .write = firmware_data_write,
511 static void firmware_class_timeout(u_long data)
513 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
515 fw_load_abort(fw_priv);
518 static struct firmware_priv *
519 fw_create_instance(struct firmware *firmware, const char *fw_name,
520 struct device *device, bool uevent, bool nowait)
522 struct firmware_priv *fw_priv;
523 struct device *f_dev;
525 fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
527 dev_err(device, "%s: kmalloc failed\n", __func__);
528 return ERR_PTR(-ENOMEM);
531 fw_priv->fw = firmware;
532 fw_priv->nowait = nowait;
533 strcpy(fw_priv->fw_id, fw_name);
534 init_completion(&fw_priv->completion);
535 setup_timer(&fw_priv->timeout,
536 firmware_class_timeout, (u_long) fw_priv);
538 f_dev = &fw_priv->dev;
540 device_initialize(f_dev);
541 dev_set_name(f_dev, "%s", dev_name(device));
542 f_dev->parent = device;
543 f_dev->class = &firmware_class;
548 static struct firmware_priv *
549 _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
550 struct device *device, bool uevent, bool nowait)
552 struct firmware *firmware;
553 struct firmware_priv *fw_priv;
556 return ERR_PTR(-EINVAL);
558 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
560 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
562 return ERR_PTR(-ENOMEM);
565 if (fw_get_builtin_firmware(firmware, name)) {
566 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
570 if (fw_get_filesystem_firmware(firmware, name)) {
571 dev_dbg(device, "firmware: direct-loading firmware %s\n", name);
575 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
576 if (IS_ERR(fw_priv)) {
577 release_firmware(firmware);
583 static void _request_firmware_cleanup(const struct firmware **firmware_p)
585 release_firmware(*firmware_p);
589 static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
593 struct device *f_dev = &fw_priv->dev;
595 dev_set_uevent_suppress(f_dev, true);
597 /* Need to pin this module until class device is destroyed */
598 __module_get(THIS_MODULE);
600 retval = device_add(f_dev);
602 dev_err(f_dev, "%s: device_register failed\n", __func__);
606 retval = device_create_bin_file(f_dev, &firmware_attr_data);
608 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
612 retval = device_create_file(f_dev, &dev_attr_loading);
614 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
615 goto err_del_bin_attr;
619 dev_set_uevent_suppress(f_dev, false);
620 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id);
621 if (timeout != MAX_SCHEDULE_TIMEOUT)
622 mod_timer(&fw_priv->timeout,
623 round_jiffies_up(jiffies + timeout));
625 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
628 wait_for_completion(&fw_priv->completion);
630 set_bit(FW_STATUS_DONE, &fw_priv->status);
631 del_timer_sync(&fw_priv->timeout);
633 mutex_lock(&fw_lock);
634 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
637 mutex_unlock(&fw_lock);
639 device_remove_file(f_dev, &dev_attr_loading);
641 device_remove_bin_file(f_dev, &firmware_attr_data);
650 * request_firmware: - send firmware request and wait for it
651 * @firmware_p: pointer to firmware image
652 * @name: name of firmware file
653 * @device: device for which firmware is being loaded
655 * @firmware_p will be used to return a firmware image by the name
656 * of @name for device @device.
658 * Should be called from user context where sleeping is allowed.
660 * @name will be used as $FIRMWARE in the uevent environment and
661 * should be distinctive enough not to be confused with any other
662 * firmware image for this or any other device.
665 request_firmware(const struct firmware **firmware_p, const char *name,
666 struct device *device)
668 struct firmware_priv *fw_priv;
671 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
673 if (IS_ERR_OR_NULL(fw_priv))
674 return PTR_RET(fw_priv);
676 ret = usermodehelper_read_trylock();
679 dev_err(device, "firmware: %s will not be loaded\n", name);
681 ret = _request_firmware_load(fw_priv, true,
682 firmware_loading_timeout());
683 usermodehelper_read_unlock();
686 _request_firmware_cleanup(firmware_p);
692 * release_firmware: - release the resource associated with a firmware image
693 * @fw: firmware resource to release
695 void release_firmware(const struct firmware *fw)
698 if (!fw_is_builtin_firmware(fw))
699 firmware_free_data(fw);
705 struct firmware_work {
706 struct work_struct work;
707 struct module *module;
709 struct device *device;
711 void (*cont)(const struct firmware *fw, void *context);
715 static void request_firmware_work_func(struct work_struct *work)
717 struct firmware_work *fw_work;
718 const struct firmware *fw;
719 struct firmware_priv *fw_priv;
723 fw_work = container_of(work, struct firmware_work, work);
724 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
725 fw_work->uevent, true);
726 if (IS_ERR_OR_NULL(fw_priv)) {
727 ret = PTR_RET(fw_priv);
731 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
733 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
734 usermodehelper_read_unlock();
736 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
741 _request_firmware_cleanup(&fw);
744 fw_work->cont(fw, fw_work->context);
746 module_put(fw_work->module);
751 * request_firmware_nowait - asynchronous version of request_firmware
752 * @module: module requesting the firmware
753 * @uevent: sends uevent to copy the firmware image if this flag
754 * is non-zero else the firmware copy must be done manually.
755 * @name: name of firmware file
756 * @device: device for which firmware is being loaded
757 * @gfp: allocation flags
758 * @context: will be passed over to @cont, and
759 * @fw may be %NULL if firmware request fails.
760 * @cont: function will be called asynchronously when the firmware
763 * Asynchronous variant of request_firmware() for user contexts where
764 * it is not possible to sleep for long time. It can't be called
765 * in atomic contexts.
768 request_firmware_nowait(
769 struct module *module, bool uevent,
770 const char *name, struct device *device, gfp_t gfp, void *context,
771 void (*cont)(const struct firmware *fw, void *context))
773 struct firmware_work *fw_work;
775 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
779 fw_work->module = module;
780 fw_work->name = name;
781 fw_work->device = device;
782 fw_work->context = context;
783 fw_work->cont = cont;
784 fw_work->uevent = uevent;
786 if (!try_module_get(module)) {
791 INIT_WORK(&fw_work->work, request_firmware_work_func);
792 schedule_work(&fw_work->work);
796 static int __init firmware_class_init(void)
798 return class_register(&firmware_class);
801 static void __exit firmware_class_exit(void)
803 class_unregister(&firmware_class);
806 fs_initcall(firmware_class_init);
807 module_exit(firmware_class_exit);
809 EXPORT_SYMBOL(release_firmware);
810 EXPORT_SYMBOL(request_firmware);
811 EXPORT_SYMBOL(request_firmware_nowait);