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");
36 static int loading_timeout = 60; /* In seconds */
38 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
39 * guarding for corner cases a global lock should be OK */
40 static DEFINE_MUTEX(fw_lock);
42 struct firmware_priv {
44 struct completion completion;
45 struct bin_attribute attr_data;
52 struct timer_list timeout;
56 #ifdef CONFIG_FW_LOADER
57 extern struct builtin_fw __start_builtin_fw[];
58 extern struct builtin_fw __end_builtin_fw[];
59 #else /* Module case. Avoid ifdefs later; it'll all optimise out */
60 static struct builtin_fw *__start_builtin_fw;
61 static struct builtin_fw *__end_builtin_fw;
65 fw_load_abort(struct firmware_priv *fw_priv)
67 set_bit(FW_STATUS_ABORT, &fw_priv->status);
69 complete(&fw_priv->completion);
73 firmware_timeout_show(struct class *class,
74 struct class_attribute *attr,
77 return sprintf(buf, "%d\n", loading_timeout);
81 * firmware_timeout_store - set number of seconds to wait for firmware
82 * @class: device class pointer
83 * @attr: device attribute pointer
84 * @buf: buffer to scan for timeout value
85 * @count: number of bytes in @buf
87 * Sets the number of seconds to wait for the firmware. Once
88 * this expires an error will be returned to the driver and no
89 * firmware will be provided.
91 * Note: zero means 'wait forever'.
94 firmware_timeout_store(struct class *class,
95 struct class_attribute *attr,
96 const char *buf, size_t count)
98 loading_timeout = simple_strtol(buf, NULL, 10);
99 if (loading_timeout < 0)
104 static struct class_attribute firmware_class_attrs[] = {
105 __ATTR(timeout, S_IWUSR | S_IRUGO,
106 firmware_timeout_show, firmware_timeout_store),
110 static void fw_dev_release(struct device *dev)
112 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
115 for (i = 0; i < fw_priv->nr_pages; i++)
116 __free_page(fw_priv->pages[i]);
117 kfree(fw_priv->pages);
118 kfree(fw_priv->fw_id);
122 module_put(THIS_MODULE);
125 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
127 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
129 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
131 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
133 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
139 static struct class firmware_class = {
141 .class_attrs = firmware_class_attrs,
142 .dev_uevent = firmware_uevent,
143 .dev_release = fw_dev_release,
146 static ssize_t firmware_loading_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
149 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
150 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
151 return sprintf(buf, "%d\n", loading);
154 static void firmware_free_data(const struct firmware *fw)
159 for (i = 0; i < PFN_UP(fw->size); i++)
160 __free_page(fw->pages[i]);
165 /* Some architectures don't have PAGE_KERNEL_RO */
166 #ifndef PAGE_KERNEL_RO
167 #define PAGE_KERNEL_RO PAGE_KERNEL
170 * firmware_loading_store - set value in the 'loading' control file
171 * @dev: device pointer
172 * @attr: device attribute pointer
173 * @buf: buffer to scan for loading control value
174 * @count: number of bytes in @buf
176 * The relevant values are:
178 * 1: Start a load, discarding any previous partial load.
179 * 0: Conclude the load and hand the data to the driver code.
180 * -1: Conclude the load with an error and discard any written data.
182 static ssize_t firmware_loading_store(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf, size_t count)
186 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
187 int loading = simple_strtol(buf, NULL, 10);
192 mutex_lock(&fw_lock);
194 mutex_unlock(&fw_lock);
197 firmware_free_data(fw_priv->fw);
198 memset(fw_priv->fw, 0, sizeof(struct firmware));
199 /* If the pages are not owned by 'struct firmware' */
200 for (i = 0; i < fw_priv->nr_pages; i++)
201 __free_page(fw_priv->pages[i]);
202 kfree(fw_priv->pages);
203 fw_priv->pages = NULL;
204 fw_priv->page_array_size = 0;
205 fw_priv->nr_pages = 0;
206 set_bit(FW_STATUS_LOADING, &fw_priv->status);
207 mutex_unlock(&fw_lock);
210 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
211 vunmap(fw_priv->fw->data);
212 fw_priv->fw->data = vmap(fw_priv->pages,
215 if (!fw_priv->fw->data) {
216 dev_err(dev, "%s: vmap() failed\n", __func__);
219 /* Pages are now owned by 'struct firmware' */
220 fw_priv->fw->pages = fw_priv->pages;
221 fw_priv->pages = NULL;
223 fw_priv->page_array_size = 0;
224 fw_priv->nr_pages = 0;
225 complete(&fw_priv->completion);
226 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
231 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
235 fw_load_abort(fw_priv);
242 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
245 firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
246 char *buffer, loff_t offset, size_t count)
248 struct device *dev = to_dev(kobj);
249 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
253 mutex_lock(&fw_lock);
255 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
259 if (offset > fw->size) {
263 if (count > fw->size - offset)
264 count = fw->size - offset;
270 int page_nr = offset >> PAGE_SHIFT;
271 int page_ofs = offset & (PAGE_SIZE-1);
272 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
274 page_data = kmap(fw_priv->pages[page_nr]);
276 memcpy(buffer, page_data + page_ofs, page_cnt);
278 kunmap(fw_priv->pages[page_nr]);
284 mutex_unlock(&fw_lock);
289 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
291 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
293 /* If the array of pages is too small, grow it... */
294 if (fw_priv->page_array_size < pages_needed) {
295 int new_array_size = max(pages_needed,
296 fw_priv->page_array_size * 2);
297 struct page **new_pages;
299 new_pages = kmalloc(new_array_size * sizeof(void *),
302 fw_load_abort(fw_priv);
305 memcpy(new_pages, fw_priv->pages,
306 fw_priv->page_array_size * sizeof(void *));
307 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
308 (new_array_size - fw_priv->page_array_size));
309 kfree(fw_priv->pages);
310 fw_priv->pages = new_pages;
311 fw_priv->page_array_size = new_array_size;
314 while (fw_priv->nr_pages < pages_needed) {
315 fw_priv->pages[fw_priv->nr_pages] =
316 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
318 if (!fw_priv->pages[fw_priv->nr_pages]) {
319 fw_load_abort(fw_priv);
328 * firmware_data_write - write method for firmware
329 * @kobj: kobject for the device
330 * @bin_attr: bin_attr structure
331 * @buffer: buffer being written
332 * @offset: buffer offset for write in total data store area
333 * @count: buffer size
335 * Data written to the 'data' attribute will be later handed to
336 * the driver as a firmware image.
339 firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
340 char *buffer, loff_t offset, size_t count)
342 struct device *dev = to_dev(kobj);
343 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
347 if (!capable(CAP_SYS_RAWIO))
350 mutex_lock(&fw_lock);
352 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
356 retval = fw_realloc_buffer(fw_priv, offset + count);
364 int page_nr = offset >> PAGE_SHIFT;
365 int page_ofs = offset & (PAGE_SIZE - 1);
366 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
368 page_data = kmap(fw_priv->pages[page_nr]);
370 memcpy(page_data + page_ofs, buffer, page_cnt);
372 kunmap(fw_priv->pages[page_nr]);
378 fw->size = max_t(size_t, offset, fw->size);
380 mutex_unlock(&fw_lock);
384 static struct bin_attribute firmware_attr_data_tmpl = {
385 .attr = {.name = "data", .mode = 0644},
387 .read = firmware_data_read,
388 .write = firmware_data_write,
392 firmware_class_timeout(u_long data)
394 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
395 fw_load_abort(fw_priv);
398 static int fw_register_device(struct device **dev_p, const char *fw_name,
399 struct device *device)
402 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
404 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
408 if (!fw_priv || !f_dev) {
409 dev_err(device, "%s: kmalloc failed\n", __func__);
414 init_completion(&fw_priv->completion);
415 fw_priv->attr_data = firmware_attr_data_tmpl;
416 fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
417 if (!fw_priv->fw_id) {
418 dev_err(device, "%s: Firmware name allocation failed\n",
424 fw_priv->timeout.function = firmware_class_timeout;
425 fw_priv->timeout.data = (u_long) fw_priv;
426 init_timer(&fw_priv->timeout);
428 dev_set_name(f_dev, "%s", dev_name(device));
429 f_dev->parent = device;
430 f_dev->class = &firmware_class;
431 dev_set_drvdata(f_dev, fw_priv);
432 dev_set_uevent_suppress(f_dev, 1);
433 retval = device_register(f_dev);
435 dev_err(device, "%s: device_register failed\n", __func__);
448 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
449 const char *fw_name, struct device *device,
450 int uevent, bool nowait)
452 struct device *f_dev;
453 struct firmware_priv *fw_priv;
457 retval = fw_register_device(&f_dev, fw_name, device);
461 /* Need to pin this module until class device is destroyed */
462 __module_get(THIS_MODULE);
464 fw_priv = dev_get_drvdata(f_dev);
466 fw_priv->nowait = nowait;
469 sysfs_bin_attr_init(&fw_priv->attr_data);
470 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
472 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
476 retval = device_create_file(f_dev, &dev_attr_loading);
478 dev_err(device, "%s: device_create_file failed\n", __func__);
483 dev_set_uevent_suppress(f_dev, 0);
488 device_unregister(f_dev);
494 _request_firmware(const struct firmware **firmware_p, const char *name,
495 struct device *device, int uevent, bool nowait)
497 struct device *f_dev;
498 struct firmware_priv *fw_priv;
499 struct firmware *firmware;
500 struct builtin_fw *builtin;
506 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
508 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
514 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
516 if (strcmp(name, builtin->name))
518 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
519 firmware->size = builtin->size;
520 firmware->data = builtin->data;
525 dev_dbg(device, "firmware: requesting %s\n", name);
527 retval = fw_setup_device(firmware, &f_dev, name, device,
532 fw_priv = dev_get_drvdata(f_dev);
535 if (loading_timeout > 0) {
536 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
537 add_timer(&fw_priv->timeout);
540 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
541 wait_for_completion(&fw_priv->completion);
542 set_bit(FW_STATUS_DONE, &fw_priv->status);
543 del_timer_sync(&fw_priv->timeout);
545 wait_for_completion(&fw_priv->completion);
547 mutex_lock(&fw_lock);
548 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
550 release_firmware(fw_priv->fw);
554 mutex_unlock(&fw_lock);
555 device_unregister(f_dev);
566 * request_firmware: - send firmware request and wait for it
567 * @firmware_p: pointer to firmware image
568 * @name: name of firmware file
569 * @device: device for which firmware is being loaded
571 * @firmware_p will be used to return a firmware image by the name
572 * of @name for device @device.
574 * Should be called from user context where sleeping is allowed.
576 * @name will be used as $FIRMWARE in the uevent environment and
577 * should be distinctive enough not to be confused with any other
578 * firmware image for this or any other device.
581 request_firmware(const struct firmware **firmware_p, const char *name,
582 struct device *device)
585 return _request_firmware(firmware_p, name, device, uevent, false);
589 * release_firmware: - release the resource associated with a firmware image
590 * @fw: firmware resource to release
593 release_firmware(const struct firmware *fw)
595 struct builtin_fw *builtin;
598 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
600 if (fw->data == builtin->data)
603 firmware_free_data(fw);
610 struct firmware_work {
611 struct work_struct work;
612 struct module *module;
614 struct device *device;
616 void (*cont)(const struct firmware *fw, void *context);
621 request_firmware_work_func(void *arg)
623 struct firmware_work *fw_work = arg;
624 const struct firmware *fw;
630 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
631 fw_work->uevent, true);
633 fw_work->cont(fw, fw_work->context);
635 module_put(fw_work->module);
641 * request_firmware_nowait - asynchronous version of request_firmware
642 * @module: module requesting the firmware
643 * @uevent: sends uevent to copy the firmware image if this flag
644 * is non-zero else the firmware copy must be done manually.
645 * @name: name of firmware file
646 * @device: device for which firmware is being loaded
647 * @gfp: allocation flags
648 * @context: will be passed over to @cont, and
649 * @fw may be %NULL if firmware request fails.
650 * @cont: function will be called asynchronously when the firmware
653 * Asynchronous variant of request_firmware() for user contexts where
654 * it is not possible to sleep for long time. It can't be called
655 * in atomic contexts.
658 request_firmware_nowait(
659 struct module *module, int uevent,
660 const char *name, struct device *device, gfp_t gfp, void *context,
661 void (*cont)(const struct firmware *fw, void *context))
663 struct task_struct *task;
664 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
669 if (!try_module_get(module)) {
674 *fw_work = (struct firmware_work) {
683 task = kthread_run(request_firmware_work_func, fw_work,
684 "firmware/%s", name);
687 fw_work->cont(NULL, fw_work->context);
688 module_put(fw_work->module);
690 return PTR_ERR(task);
695 static int __init firmware_class_init(void)
697 return class_register(&firmware_class);
700 static void __exit firmware_class_exit(void)
702 class_unregister(&firmware_class);
705 fs_initcall(firmware_class_init);
706 module_exit(firmware_class_exit);
708 EXPORT_SYMBOL(release_firmware);
709 EXPORT_SYMBOL(request_firmware);
710 EXPORT_SYMBOL(request_firmware_nowait);