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 MODULE_AUTHOR("Manuel Estrada Sainz");
26 MODULE_DESCRIPTION("Multi purpose firmware loading support");
27 MODULE_LICENSE("GPL");
29 /* Builtin firmware support */
31 #ifdef CONFIG_FW_LOADER
33 extern struct builtin_fw __start_builtin_fw[];
34 extern struct builtin_fw __end_builtin_fw[];
36 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
38 struct builtin_fw *b_fw;
40 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
41 if (strcmp(name, b_fw->name) == 0) {
42 fw->size = b_fw->size;
43 fw->data = b_fw->data;
51 static bool fw_is_builtin_firmware(const struct firmware *fw)
53 struct builtin_fw *b_fw;
55 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
56 if (fw->data == b_fw->data)
62 #else /* Module case - no builtin firmware support */
64 static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
69 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
81 static int loading_timeout = 60; /* In seconds */
83 static inline long firmware_loading_timeout(void)
85 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
88 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
89 * guarding for corner cases a global lock should be OK */
90 static DEFINE_MUTEX(fw_lock);
92 struct firmware_priv {
93 struct completion completion;
99 struct timer_list timeout;
105 static struct firmware_priv *to_firmware_priv(struct device *dev)
107 return container_of(dev, struct firmware_priv, dev);
110 static void fw_load_abort(struct firmware_priv *fw_priv)
112 set_bit(FW_STATUS_ABORT, &fw_priv->status);
114 complete(&fw_priv->completion);
117 static ssize_t firmware_timeout_show(struct class *class,
118 struct class_attribute *attr,
121 return sprintf(buf, "%d\n", loading_timeout);
125 * firmware_timeout_store - set number of seconds to wait for firmware
126 * @class: device class pointer
127 * @attr: device attribute pointer
128 * @buf: buffer to scan for timeout value
129 * @count: number of bytes in @buf
131 * Sets the number of seconds to wait for the firmware. Once
132 * this expires an error will be returned to the driver and no
133 * firmware will be provided.
135 * Note: zero means 'wait forever'.
137 static ssize_t firmware_timeout_store(struct class *class,
138 struct class_attribute *attr,
139 const char *buf, size_t count)
141 loading_timeout = simple_strtol(buf, NULL, 10);
142 if (loading_timeout < 0)
148 static struct class_attribute firmware_class_attrs[] = {
149 __ATTR(timeout, S_IWUSR | S_IRUGO,
150 firmware_timeout_show, firmware_timeout_store),
154 static void fw_dev_release(struct device *dev)
156 struct firmware_priv *fw_priv = to_firmware_priv(dev);
159 for (i = 0; i < fw_priv->nr_pages; i++)
160 __free_page(fw_priv->pages[i]);
161 kfree(fw_priv->pages);
164 module_put(THIS_MODULE);
167 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
169 struct firmware_priv *fw_priv = to_firmware_priv(dev);
171 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
173 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
175 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
181 static struct class firmware_class = {
183 .class_attrs = firmware_class_attrs,
184 .dev_uevent = firmware_uevent,
185 .dev_release = fw_dev_release,
188 static ssize_t firmware_loading_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
191 struct firmware_priv *fw_priv = to_firmware_priv(dev);
192 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
194 return sprintf(buf, "%d\n", loading);
197 static void firmware_free_data(const struct firmware *fw)
202 for (i = 0; i < PFN_UP(fw->size); i++)
203 __free_page(fw->pages[i]);
208 /* Some architectures don't have PAGE_KERNEL_RO */
209 #ifndef PAGE_KERNEL_RO
210 #define PAGE_KERNEL_RO PAGE_KERNEL
213 * firmware_loading_store - set value in the 'loading' control file
214 * @dev: device pointer
215 * @attr: device attribute pointer
216 * @buf: buffer to scan for loading control value
217 * @count: number of bytes in @buf
219 * The relevant values are:
221 * 1: Start a load, discarding any previous partial load.
222 * 0: Conclude the load and hand the data to the driver code.
223 * -1: Conclude the load with an error and discard any written data.
225 static ssize_t firmware_loading_store(struct device *dev,
226 struct device_attribute *attr,
227 const char *buf, size_t count)
229 struct firmware_priv *fw_priv = to_firmware_priv(dev);
230 int loading = simple_strtol(buf, NULL, 10);
233 mutex_lock(&fw_lock);
240 firmware_free_data(fw_priv->fw);
241 memset(fw_priv->fw, 0, sizeof(struct firmware));
242 /* If the pages are not owned by 'struct firmware' */
243 for (i = 0; i < fw_priv->nr_pages; i++)
244 __free_page(fw_priv->pages[i]);
245 kfree(fw_priv->pages);
246 fw_priv->pages = NULL;
247 fw_priv->page_array_size = 0;
248 fw_priv->nr_pages = 0;
249 set_bit(FW_STATUS_LOADING, &fw_priv->status);
252 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
253 vunmap(fw_priv->fw->data);
254 fw_priv->fw->data = vmap(fw_priv->pages,
257 if (!fw_priv->fw->data) {
258 dev_err(dev, "%s: vmap() failed\n", __func__);
261 /* Pages are now owned by 'struct firmware' */
262 fw_priv->fw->pages = fw_priv->pages;
263 fw_priv->pages = NULL;
265 fw_priv->page_array_size = 0;
266 fw_priv->nr_pages = 0;
267 complete(&fw_priv->completion);
268 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
273 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
277 fw_load_abort(fw_priv);
281 mutex_unlock(&fw_lock);
285 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
287 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
288 struct bin_attribute *bin_attr,
289 char *buffer, loff_t offset, size_t count)
291 struct device *dev = kobj_to_dev(kobj);
292 struct firmware_priv *fw_priv = to_firmware_priv(dev);
296 mutex_lock(&fw_lock);
298 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
302 if (offset > fw->size) {
306 if (count > fw->size - offset)
307 count = fw->size - offset;
313 int page_nr = offset >> PAGE_SHIFT;
314 int page_ofs = offset & (PAGE_SIZE-1);
315 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
317 page_data = kmap(fw_priv->pages[page_nr]);
319 memcpy(buffer, page_data + page_ofs, page_cnt);
321 kunmap(fw_priv->pages[page_nr]);
327 mutex_unlock(&fw_lock);
331 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
333 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
335 /* If the array of pages is too small, grow it... */
336 if (fw_priv->page_array_size < pages_needed) {
337 int new_array_size = max(pages_needed,
338 fw_priv->page_array_size * 2);
339 struct page **new_pages;
341 new_pages = kmalloc(new_array_size * sizeof(void *),
344 fw_load_abort(fw_priv);
347 memcpy(new_pages, fw_priv->pages,
348 fw_priv->page_array_size * sizeof(void *));
349 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
350 (new_array_size - fw_priv->page_array_size));
351 kfree(fw_priv->pages);
352 fw_priv->pages = new_pages;
353 fw_priv->page_array_size = new_array_size;
356 while (fw_priv->nr_pages < pages_needed) {
357 fw_priv->pages[fw_priv->nr_pages] =
358 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
360 if (!fw_priv->pages[fw_priv->nr_pages]) {
361 fw_load_abort(fw_priv);
370 * firmware_data_write - write method for firmware
371 * @filp: open sysfs file
372 * @kobj: kobject for the device
373 * @bin_attr: bin_attr structure
374 * @buffer: buffer being written
375 * @offset: buffer offset for write in total data store area
376 * @count: buffer size
378 * Data written to the 'data' attribute will be later handed to
379 * the driver as a firmware image.
381 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
382 struct bin_attribute *bin_attr,
383 char *buffer, loff_t offset, size_t count)
385 struct device *dev = kobj_to_dev(kobj);
386 struct firmware_priv *fw_priv = to_firmware_priv(dev);
390 if (!capable(CAP_SYS_RAWIO))
393 mutex_lock(&fw_lock);
395 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
399 retval = fw_realloc_buffer(fw_priv, offset + count);
407 int page_nr = offset >> PAGE_SHIFT;
408 int page_ofs = offset & (PAGE_SIZE - 1);
409 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
411 page_data = kmap(fw_priv->pages[page_nr]);
413 memcpy(page_data + page_ofs, buffer, page_cnt);
415 kunmap(fw_priv->pages[page_nr]);
421 fw->size = max_t(size_t, offset, fw->size);
423 mutex_unlock(&fw_lock);
427 static struct bin_attribute firmware_attr_data = {
428 .attr = { .name = "data", .mode = 0644 },
430 .read = firmware_data_read,
431 .write = firmware_data_write,
434 static void firmware_class_timeout(u_long data)
436 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
438 fw_load_abort(fw_priv);
441 static struct firmware_priv *
442 fw_create_instance(struct firmware *firmware, const char *fw_name,
443 struct device *device, bool uevent, bool nowait)
445 struct firmware_priv *fw_priv;
446 struct device *f_dev;
448 fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
450 dev_err(device, "%s: kmalloc failed\n", __func__);
451 return ERR_PTR(-ENOMEM);
454 fw_priv->fw = firmware;
455 fw_priv->nowait = nowait;
456 strcpy(fw_priv->fw_id, fw_name);
457 init_completion(&fw_priv->completion);
458 setup_timer(&fw_priv->timeout,
459 firmware_class_timeout, (u_long) fw_priv);
461 f_dev = &fw_priv->dev;
463 device_initialize(f_dev);
464 dev_set_name(f_dev, "%s", dev_name(device));
465 f_dev->parent = device;
466 f_dev->class = &firmware_class;
471 static struct firmware_priv *
472 _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
473 struct device *device, bool uevent, bool nowait)
475 struct firmware *firmware;
476 struct firmware_priv *fw_priv;
479 return ERR_PTR(-EINVAL);
481 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
483 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
485 return ERR_PTR(-ENOMEM);
488 if (fw_get_builtin_firmware(firmware, name)) {
489 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
493 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
494 if (IS_ERR(fw_priv)) {
495 release_firmware(firmware);
501 static void _request_firmware_cleanup(const struct firmware **firmware_p)
503 release_firmware(*firmware_p);
507 static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
511 struct device *f_dev = &fw_priv->dev;
513 dev_set_uevent_suppress(f_dev, true);
515 /* Need to pin this module until class device is destroyed */
516 __module_get(THIS_MODULE);
518 retval = device_add(f_dev);
520 dev_err(f_dev, "%s: device_register failed\n", __func__);
524 retval = device_create_bin_file(f_dev, &firmware_attr_data);
526 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
530 retval = device_create_file(f_dev, &dev_attr_loading);
532 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
533 goto err_del_bin_attr;
537 dev_set_uevent_suppress(f_dev, false);
538 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id);
539 if (timeout != MAX_SCHEDULE_TIMEOUT)
540 mod_timer(&fw_priv->timeout,
541 round_jiffies_up(jiffies + timeout));
543 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
546 wait_for_completion(&fw_priv->completion);
548 set_bit(FW_STATUS_DONE, &fw_priv->status);
549 del_timer_sync(&fw_priv->timeout);
551 mutex_lock(&fw_lock);
552 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
555 mutex_unlock(&fw_lock);
557 device_remove_file(f_dev, &dev_attr_loading);
559 device_remove_bin_file(f_dev, &firmware_attr_data);
568 * request_firmware: - send firmware request and wait for it
569 * @firmware_p: pointer to firmware image
570 * @name: name of firmware file
571 * @device: device for which firmware is being loaded
573 * @firmware_p will be used to return a firmware image by the name
574 * of @name for device @device.
576 * Should be called from user context where sleeping is allowed.
578 * @name will be used as $FIRMWARE in the uevent environment and
579 * should be distinctive enough not to be confused with any other
580 * firmware image for this or any other device.
583 request_firmware(const struct firmware **firmware_p, const char *name,
584 struct device *device)
586 struct firmware_priv *fw_priv;
589 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
591 if (IS_ERR_OR_NULL(fw_priv))
592 return PTR_RET(fw_priv);
594 ret = usermodehelper_read_trylock();
596 dev_err(device, "firmware: %s will not be loaded\n", name);
598 ret = _request_firmware_load(fw_priv, true,
599 firmware_loading_timeout());
600 usermodehelper_read_unlock();
603 _request_firmware_cleanup(firmware_p);
609 * release_firmware: - release the resource associated with a firmware image
610 * @fw: firmware resource to release
612 void release_firmware(const struct firmware *fw)
615 if (!fw_is_builtin_firmware(fw))
616 firmware_free_data(fw);
622 struct firmware_work {
623 struct work_struct work;
624 struct module *module;
626 struct device *device;
628 void (*cont)(const struct firmware *fw, void *context);
632 static void request_firmware_work_func(struct work_struct *work)
634 struct firmware_work *fw_work;
635 const struct firmware *fw;
636 struct firmware_priv *fw_priv;
640 fw_work = container_of(work, struct firmware_work, work);
641 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
642 fw_work->uevent, true);
643 if (IS_ERR_OR_NULL(fw_priv)) {
644 ret = PTR_RET(fw_priv);
648 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
650 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
651 usermodehelper_read_unlock();
653 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
658 _request_firmware_cleanup(&fw);
661 fw_work->cont(fw, fw_work->context);
663 module_put(fw_work->module);
668 * request_firmware_nowait - asynchronous version of request_firmware
669 * @module: module requesting the firmware
670 * @uevent: sends uevent to copy the firmware image if this flag
671 * is non-zero else the firmware copy must be done manually.
672 * @name: name of firmware file
673 * @device: device for which firmware is being loaded
674 * @gfp: allocation flags
675 * @context: will be passed over to @cont, and
676 * @fw may be %NULL if firmware request fails.
677 * @cont: function will be called asynchronously when the firmware
680 * Asynchronous variant of request_firmware() for user contexts where
681 * it is not possible to sleep for long time. It can't be called
682 * in atomic contexts.
685 request_firmware_nowait(
686 struct module *module, bool uevent,
687 const char *name, struct device *device, gfp_t gfp, void *context,
688 void (*cont)(const struct firmware *fw, void *context))
690 struct firmware_work *fw_work;
692 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
696 fw_work->module = module;
697 fw_work->name = name;
698 fw_work->device = device;
699 fw_work->context = context;
700 fw_work->cont = cont;
701 fw_work->uevent = uevent;
703 if (!try_module_get(module)) {
708 INIT_WORK(&fw_work->work, request_firmware_work_func);
709 schedule_work(&fw_work->work);
713 static int __init firmware_class_init(void)
715 return class_register(&firmware_class);
718 static void __exit firmware_class_exit(void)
720 class_unregister(&firmware_class);
723 fs_initcall(firmware_class_init);
724 module_exit(firmware_class_exit);
726 EXPORT_SYMBOL(release_firmware);
727 EXPORT_SYMBOL(request_firmware);
728 EXPORT_SYMBOL(request_firmware_nowait);