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 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
85 * guarding for corner cases a global lock should be OK */
86 static DEFINE_MUTEX(fw_lock);
88 struct firmware_priv {
90 struct completion completion;
91 struct bin_attribute attr_data;
98 struct timer_list timeout;
103 fw_load_abort(struct firmware_priv *fw_priv)
105 set_bit(FW_STATUS_ABORT, &fw_priv->status);
107 complete(&fw_priv->completion);
111 firmware_timeout_show(struct class *class,
112 struct class_attribute *attr,
115 return sprintf(buf, "%d\n", loading_timeout);
119 * firmware_timeout_store - set number of seconds to wait for firmware
120 * @class: device class pointer
121 * @attr: device attribute pointer
122 * @buf: buffer to scan for timeout value
123 * @count: number of bytes in @buf
125 * Sets the number of seconds to wait for the firmware. Once
126 * this expires an error will be returned to the driver and no
127 * firmware will be provided.
129 * Note: zero means 'wait forever'.
132 firmware_timeout_store(struct class *class,
133 struct class_attribute *attr,
134 const char *buf, size_t count)
136 loading_timeout = simple_strtol(buf, NULL, 10);
137 if (loading_timeout < 0)
142 static struct class_attribute firmware_class_attrs[] = {
143 __ATTR(timeout, S_IWUSR | S_IRUGO,
144 firmware_timeout_show, firmware_timeout_store),
148 static void fw_dev_release(struct device *dev)
150 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
153 for (i = 0; i < fw_priv->nr_pages; i++)
154 __free_page(fw_priv->pages[i]);
155 kfree(fw_priv->pages);
156 kfree(fw_priv->fw_id);
160 module_put(THIS_MODULE);
163 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
165 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
167 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
169 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
171 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
177 static struct class firmware_class = {
179 .class_attrs = firmware_class_attrs,
180 .dev_uevent = firmware_uevent,
181 .dev_release = fw_dev_release,
184 static ssize_t firmware_loading_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
187 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
188 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
189 return sprintf(buf, "%d\n", loading);
192 static void firmware_free_data(const struct firmware *fw)
197 for (i = 0; i < PFN_UP(fw->size); i++)
198 __free_page(fw->pages[i]);
203 /* Some architectures don't have PAGE_KERNEL_RO */
204 #ifndef PAGE_KERNEL_RO
205 #define PAGE_KERNEL_RO PAGE_KERNEL
208 * firmware_loading_store - set value in the 'loading' control file
209 * @dev: device pointer
210 * @attr: device attribute pointer
211 * @buf: buffer to scan for loading control value
212 * @count: number of bytes in @buf
214 * The relevant values are:
216 * 1: Start a load, discarding any previous partial load.
217 * 0: Conclude the load and hand the data to the driver code.
218 * -1: Conclude the load with an error and discard any written data.
220 static ssize_t firmware_loading_store(struct device *dev,
221 struct device_attribute *attr,
222 const char *buf, size_t count)
224 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
225 int loading = simple_strtol(buf, NULL, 10);
230 mutex_lock(&fw_lock);
232 mutex_unlock(&fw_lock);
235 firmware_free_data(fw_priv->fw);
236 memset(fw_priv->fw, 0, sizeof(struct firmware));
237 /* If the pages are not owned by 'struct firmware' */
238 for (i = 0; i < fw_priv->nr_pages; i++)
239 __free_page(fw_priv->pages[i]);
240 kfree(fw_priv->pages);
241 fw_priv->pages = NULL;
242 fw_priv->page_array_size = 0;
243 fw_priv->nr_pages = 0;
244 set_bit(FW_STATUS_LOADING, &fw_priv->status);
245 mutex_unlock(&fw_lock);
248 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
249 vunmap(fw_priv->fw->data);
250 fw_priv->fw->data = vmap(fw_priv->pages,
253 if (!fw_priv->fw->data) {
254 dev_err(dev, "%s: vmap() failed\n", __func__);
257 /* Pages are now owned by 'struct firmware' */
258 fw_priv->fw->pages = fw_priv->pages;
259 fw_priv->pages = NULL;
261 fw_priv->page_array_size = 0;
262 fw_priv->nr_pages = 0;
263 complete(&fw_priv->completion);
264 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
269 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
273 fw_load_abort(fw_priv);
280 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
283 firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
284 char *buffer, loff_t offset, size_t count)
286 struct device *dev = to_dev(kobj);
287 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
291 mutex_lock(&fw_lock);
293 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
297 if (offset > fw->size) {
301 if (count > fw->size - offset)
302 count = fw->size - offset;
308 int page_nr = offset >> PAGE_SHIFT;
309 int page_ofs = offset & (PAGE_SIZE-1);
310 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
312 page_data = kmap(fw_priv->pages[page_nr]);
314 memcpy(buffer, page_data + page_ofs, page_cnt);
316 kunmap(fw_priv->pages[page_nr]);
322 mutex_unlock(&fw_lock);
327 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
329 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
331 /* If the array of pages is too small, grow it... */
332 if (fw_priv->page_array_size < pages_needed) {
333 int new_array_size = max(pages_needed,
334 fw_priv->page_array_size * 2);
335 struct page **new_pages;
337 new_pages = kmalloc(new_array_size * sizeof(void *),
340 fw_load_abort(fw_priv);
343 memcpy(new_pages, fw_priv->pages,
344 fw_priv->page_array_size * sizeof(void *));
345 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
346 (new_array_size - fw_priv->page_array_size));
347 kfree(fw_priv->pages);
348 fw_priv->pages = new_pages;
349 fw_priv->page_array_size = new_array_size;
352 while (fw_priv->nr_pages < pages_needed) {
353 fw_priv->pages[fw_priv->nr_pages] =
354 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
356 if (!fw_priv->pages[fw_priv->nr_pages]) {
357 fw_load_abort(fw_priv);
366 * firmware_data_write - write method for firmware
367 * @kobj: kobject for the device
368 * @bin_attr: bin_attr structure
369 * @buffer: buffer being written
370 * @offset: buffer offset for write in total data store area
371 * @count: buffer size
373 * Data written to the 'data' attribute will be later handed to
374 * the driver as a firmware image.
377 firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
378 char *buffer, loff_t offset, size_t count)
380 struct device *dev = to_dev(kobj);
381 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
385 if (!capable(CAP_SYS_RAWIO))
388 mutex_lock(&fw_lock);
390 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
394 retval = fw_realloc_buffer(fw_priv, offset + count);
402 int page_nr = offset >> PAGE_SHIFT;
403 int page_ofs = offset & (PAGE_SIZE - 1);
404 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
406 page_data = kmap(fw_priv->pages[page_nr]);
408 memcpy(page_data + page_ofs, buffer, page_cnt);
410 kunmap(fw_priv->pages[page_nr]);
416 fw->size = max_t(size_t, offset, fw->size);
418 mutex_unlock(&fw_lock);
422 static struct bin_attribute firmware_attr_data_tmpl = {
423 .attr = {.name = "data", .mode = 0644},
425 .read = firmware_data_read,
426 .write = firmware_data_write,
430 firmware_class_timeout(u_long data)
432 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
433 fw_load_abort(fw_priv);
436 static int fw_register_device(struct device **dev_p, const char *fw_name,
437 struct device *device)
440 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
442 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
446 if (!fw_priv || !f_dev) {
447 dev_err(device, "%s: kmalloc failed\n", __func__);
452 init_completion(&fw_priv->completion);
453 fw_priv->attr_data = firmware_attr_data_tmpl;
454 fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
455 if (!fw_priv->fw_id) {
456 dev_err(device, "%s: Firmware name allocation failed\n",
462 fw_priv->timeout.function = firmware_class_timeout;
463 fw_priv->timeout.data = (u_long) fw_priv;
464 init_timer(&fw_priv->timeout);
466 dev_set_name(f_dev, "%s", dev_name(device));
467 f_dev->parent = device;
468 f_dev->class = &firmware_class;
469 dev_set_drvdata(f_dev, fw_priv);
470 dev_set_uevent_suppress(f_dev, 1);
471 retval = device_register(f_dev);
473 dev_err(device, "%s: device_register failed\n", __func__);
486 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
487 const char *fw_name, struct device *device,
488 int uevent, bool nowait)
490 struct device *f_dev;
491 struct firmware_priv *fw_priv;
495 retval = fw_register_device(&f_dev, fw_name, device);
499 /* Need to pin this module until class device is destroyed */
500 __module_get(THIS_MODULE);
502 fw_priv = dev_get_drvdata(f_dev);
504 fw_priv->nowait = nowait;
507 sysfs_bin_attr_init(&fw_priv->attr_data);
508 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
510 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
514 retval = device_create_file(f_dev, &dev_attr_loading);
516 dev_err(device, "%s: device_create_file failed\n", __func__);
521 dev_set_uevent_suppress(f_dev, 0);
526 device_unregister(f_dev);
532 _request_firmware(const struct firmware **firmware_p, const char *name,
533 struct device *device, int uevent, bool nowait)
535 struct device *f_dev;
536 struct firmware_priv *fw_priv;
537 struct firmware *firmware;
543 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
545 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
551 if (fw_get_builtin_firmware(firmware, name)) {
552 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
557 dev_dbg(device, "firmware: requesting %s\n", name);
559 retval = fw_setup_device(firmware, &f_dev, name, device,
564 fw_priv = dev_get_drvdata(f_dev);
567 if (loading_timeout > 0) {
568 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
569 add_timer(&fw_priv->timeout);
572 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
573 wait_for_completion(&fw_priv->completion);
574 set_bit(FW_STATUS_DONE, &fw_priv->status);
575 del_timer_sync(&fw_priv->timeout);
577 wait_for_completion(&fw_priv->completion);
579 mutex_lock(&fw_lock);
580 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
582 release_firmware(fw_priv->fw);
586 mutex_unlock(&fw_lock);
587 device_unregister(f_dev);
598 * request_firmware: - send firmware request and wait for it
599 * @firmware_p: pointer to firmware image
600 * @name: name of firmware file
601 * @device: device for which firmware is being loaded
603 * @firmware_p will be used to return a firmware image by the name
604 * of @name for device @device.
606 * Should be called from user context where sleeping is allowed.
608 * @name will be used as $FIRMWARE in the uevent environment and
609 * should be distinctive enough not to be confused with any other
610 * firmware image for this or any other device.
613 request_firmware(const struct firmware **firmware_p, const char *name,
614 struct device *device)
617 return _request_firmware(firmware_p, name, device, uevent, false);
621 * release_firmware: - release the resource associated with a firmware image
622 * @fw: firmware resource to release
624 void release_firmware(const struct firmware *fw)
627 if (!fw_is_builtin_firmware(fw))
628 firmware_free_data(fw);
634 struct firmware_work {
635 struct work_struct work;
636 struct module *module;
638 struct device *device;
640 void (*cont)(const struct firmware *fw, void *context);
645 request_firmware_work_func(void *arg)
647 struct firmware_work *fw_work = arg;
648 const struct firmware *fw;
654 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
655 fw_work->uevent, true);
657 fw_work->cont(fw, fw_work->context);
659 module_put(fw_work->module);
665 * request_firmware_nowait - asynchronous version of request_firmware
666 * @module: module requesting the firmware
667 * @uevent: sends uevent to copy the firmware image if this flag
668 * is non-zero else the firmware copy must be done manually.
669 * @name: name of firmware file
670 * @device: device for which firmware is being loaded
671 * @gfp: allocation flags
672 * @context: will be passed over to @cont, and
673 * @fw may be %NULL if firmware request fails.
674 * @cont: function will be called asynchronously when the firmware
677 * Asynchronous variant of request_firmware() for user contexts where
678 * it is not possible to sleep for long time. It can't be called
679 * in atomic contexts.
682 request_firmware_nowait(
683 struct module *module, int uevent,
684 const char *name, struct device *device, gfp_t gfp, void *context,
685 void (*cont)(const struct firmware *fw, void *context))
687 struct task_struct *task;
688 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
693 if (!try_module_get(module)) {
698 *fw_work = (struct firmware_work) {
707 task = kthread_run(request_firmware_work_func, fw_work,
708 "firmware/%s", name);
711 fw_work->cont(NULL, fw_work->context);
712 module_put(fw_work->module);
714 return PTR_ERR(task);
719 static int __init firmware_class_init(void)
721 return class_register(&firmware_class);
724 static void __exit firmware_class_exit(void)
726 class_unregister(&firmware_class);
729 fs_initcall(firmware_class_init);
730 module_exit(firmware_class_exit);
732 EXPORT_SYMBOL(release_firmware);
733 EXPORT_SYMBOL(request_firmware);
734 EXPORT_SYMBOL(request_firmware_nowait);