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>
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 {
43 char fw_id[FIRMWARE_NAME_MAX];
44 struct completion completion;
45 struct bin_attribute attr_data;
52 struct timer_list timeout;
55 #ifdef CONFIG_FW_LOADER
56 extern struct builtin_fw __start_builtin_fw[];
57 extern struct builtin_fw __end_builtin_fw[];
58 #else /* Module case. Avoid ifdefs later; it'll all optimise out */
59 static struct builtin_fw *__start_builtin_fw;
60 static struct builtin_fw *__end_builtin_fw;
64 fw_load_abort(struct firmware_priv *fw_priv)
66 set_bit(FW_STATUS_ABORT, &fw_priv->status);
68 complete(&fw_priv->completion);
72 firmware_timeout_show(struct class *class, char *buf)
74 return sprintf(buf, "%d\n", loading_timeout);
78 * firmware_timeout_store - set number of seconds to wait for firmware
79 * @class: device class pointer
80 * @buf: buffer to scan for timeout value
81 * @count: number of bytes in @buf
83 * Sets the number of seconds to wait for the firmware. Once
84 * this expires an error will be returned to the driver and no
85 * firmware will be provided.
87 * Note: zero means 'wait forever'.
90 firmware_timeout_store(struct class *class, const char *buf, size_t count)
92 loading_timeout = simple_strtol(buf, NULL, 10);
93 if (loading_timeout < 0)
98 static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
100 static void fw_dev_release(struct device *dev);
102 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
104 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
106 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
108 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
114 static struct class firmware_class = {
116 .dev_uevent = firmware_uevent,
117 .dev_release = fw_dev_release,
120 static ssize_t firmware_loading_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
123 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
124 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
125 return sprintf(buf, "%d\n", loading);
128 /* Some architectures don't have PAGE_KERNEL_RO */
129 #ifndef PAGE_KERNEL_RO
130 #define PAGE_KERNEL_RO PAGE_KERNEL
133 * firmware_loading_store - set value in the 'loading' control file
134 * @dev: device pointer
135 * @attr: device attribute pointer
136 * @buf: buffer to scan for loading control value
137 * @count: number of bytes in @buf
139 * The relevant values are:
141 * 1: Start a load, discarding any previous partial load.
142 * 0: Conclude the load and hand the data to the driver code.
143 * -1: Conclude the load with an error and discard any written data.
145 static ssize_t firmware_loading_store(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf, size_t count)
149 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
150 int loading = simple_strtol(buf, NULL, 10);
155 mutex_lock(&fw_lock);
157 mutex_unlock(&fw_lock);
160 vfree(fw_priv->fw->data);
161 fw_priv->fw->data = NULL;
162 for (i = 0; i < fw_priv->nr_pages; i++)
163 __free_page(fw_priv->pages[i]);
164 kfree(fw_priv->pages);
165 fw_priv->pages = NULL;
166 fw_priv->page_array_size = 0;
167 fw_priv->nr_pages = 0;
168 fw_priv->fw->size = 0;
169 set_bit(FW_STATUS_LOADING, &fw_priv->status);
170 mutex_unlock(&fw_lock);
173 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
174 vfree(fw_priv->fw->data);
175 fw_priv->fw->data = vmap(fw_priv->pages,
178 if (!fw_priv->fw->data) {
179 dev_err(dev, "%s: vmap() failed\n", __func__);
182 /* Pages will be freed by vfree() */
183 fw_priv->pages = NULL;
184 fw_priv->page_array_size = 0;
185 fw_priv->nr_pages = 0;
186 complete(&fw_priv->completion);
187 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
192 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
196 fw_load_abort(fw_priv);
203 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
206 firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
207 char *buffer, loff_t offset, size_t count)
209 struct device *dev = to_dev(kobj);
210 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
214 mutex_lock(&fw_lock);
216 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
220 if (offset > fw->size)
222 if (count > fw->size - offset)
223 count = fw->size - offset;
229 int page_nr = offset >> PAGE_SHIFT;
230 int page_ofs = offset & (PAGE_SIZE-1);
231 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
233 page_data = kmap(fw_priv->pages[page_nr]);
235 memcpy(buffer, page_data + page_ofs, page_cnt);
237 kunmap(fw_priv->pages[page_nr]);
243 mutex_unlock(&fw_lock);
248 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
250 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
252 /* If the array of pages is too small, grow it... */
253 if (fw_priv->page_array_size < pages_needed) {
254 int new_array_size = max(pages_needed,
255 fw_priv->page_array_size * 2);
256 struct page **new_pages;
258 new_pages = kmalloc(new_array_size * sizeof(void *),
261 fw_load_abort(fw_priv);
264 memcpy(new_pages, fw_priv->pages,
265 fw_priv->page_array_size * sizeof(void *));
266 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
267 (new_array_size - fw_priv->page_array_size));
268 kfree(fw_priv->pages);
269 fw_priv->pages = new_pages;
270 fw_priv->page_array_size = new_array_size;
273 while (fw_priv->nr_pages < pages_needed) {
274 fw_priv->pages[fw_priv->nr_pages] =
275 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
277 if (!fw_priv->pages[fw_priv->nr_pages]) {
278 fw_load_abort(fw_priv);
287 * firmware_data_write - write method for firmware
288 * @kobj: kobject for the device
289 * @bin_attr: bin_attr structure
290 * @buffer: buffer being written
291 * @offset: buffer offset for write in total data store area
292 * @count: buffer size
294 * Data written to the 'data' attribute will be later handed to
295 * the driver as a firmware image.
298 firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
299 char *buffer, loff_t offset, size_t count)
301 struct device *dev = to_dev(kobj);
302 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
306 if (!capable(CAP_SYS_RAWIO))
309 mutex_lock(&fw_lock);
311 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
315 retval = fw_realloc_buffer(fw_priv, offset + count);
323 int page_nr = offset >> PAGE_SHIFT;
324 int page_ofs = offset & (PAGE_SIZE - 1);
325 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
327 page_data = kmap(fw_priv->pages[page_nr]);
329 memcpy(page_data + page_ofs, buffer, page_cnt);
331 kunmap(fw_priv->pages[page_nr]);
337 fw->size = max_t(size_t, offset, fw->size);
339 mutex_unlock(&fw_lock);
343 static struct bin_attribute firmware_attr_data_tmpl = {
344 .attr = {.name = "data", .mode = 0644},
346 .read = firmware_data_read,
347 .write = firmware_data_write,
350 static void fw_dev_release(struct device *dev)
352 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
355 for (i = 0; i < fw_priv->nr_pages; i++)
356 __free_page(fw_priv->pages[i]);
357 kfree(fw_priv->pages);
361 module_put(THIS_MODULE);
365 firmware_class_timeout(u_long data)
367 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
368 fw_load_abort(fw_priv);
371 static int fw_register_device(struct device **dev_p, const char *fw_name,
372 struct device *device)
375 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
377 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
381 if (!fw_priv || !f_dev) {
382 dev_err(device, "%s: kmalloc failed\n", __func__);
387 init_completion(&fw_priv->completion);
388 fw_priv->attr_data = firmware_attr_data_tmpl;
389 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
391 fw_priv->timeout.function = firmware_class_timeout;
392 fw_priv->timeout.data = (u_long) fw_priv;
393 init_timer(&fw_priv->timeout);
395 dev_set_name(f_dev, dev_name(device));
396 f_dev->parent = device;
397 f_dev->class = &firmware_class;
398 dev_set_drvdata(f_dev, fw_priv);
399 dev_set_uevent_suppress(f_dev, 1);
400 retval = device_register(f_dev);
402 dev_err(device, "%s: device_register failed\n", __func__);
414 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
415 const char *fw_name, struct device *device,
418 struct device *f_dev;
419 struct firmware_priv *fw_priv;
423 retval = fw_register_device(&f_dev, fw_name, device);
427 /* Need to pin this module until class device is destroyed */
428 __module_get(THIS_MODULE);
430 fw_priv = dev_get_drvdata(f_dev);
433 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
435 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
439 retval = device_create_file(f_dev, &dev_attr_loading);
441 dev_err(device, "%s: device_create_file failed\n", __func__);
446 dev_set_uevent_suppress(f_dev, 0);
451 device_unregister(f_dev);
457 _request_firmware(const struct firmware **firmware_p, const char *name,
458 struct device *device, int uevent)
460 struct device *f_dev;
461 struct firmware_priv *fw_priv;
462 struct firmware *firmware;
463 struct builtin_fw *builtin;
469 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
471 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
477 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
479 if (strcmp(name, builtin->name))
481 dev_info(device, "firmware: using built-in firmware %s\n",
483 firmware->size = builtin->size;
484 firmware->data = builtin->data;
489 dev_info(device, "firmware: requesting %s\n", name);
491 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
495 fw_priv = dev_get_drvdata(f_dev);
498 if (loading_timeout > 0) {
499 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
500 add_timer(&fw_priv->timeout);
503 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
504 wait_for_completion(&fw_priv->completion);
505 set_bit(FW_STATUS_DONE, &fw_priv->status);
506 del_timer_sync(&fw_priv->timeout);
508 wait_for_completion(&fw_priv->completion);
510 mutex_lock(&fw_lock);
511 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
513 release_firmware(fw_priv->fw);
517 mutex_unlock(&fw_lock);
518 device_unregister(f_dev);
529 * request_firmware: - send firmware request and wait for it
530 * @firmware_p: pointer to firmware image
531 * @name: name of firmware file
532 * @device: device for which firmware is being loaded
534 * @firmware_p will be used to return a firmware image by the name
535 * of @name for device @device.
537 * Should be called from user context where sleeping is allowed.
539 * @name will be used as $FIRMWARE in the uevent environment and
540 * should be distinctive enough not to be confused with any other
541 * firmware image for this or any other device.
544 request_firmware(const struct firmware **firmware_p, const char *name,
545 struct device *device)
548 return _request_firmware(firmware_p, name, device, uevent);
552 * release_firmware: - release the resource associated with a firmware image
553 * @fw: firmware resource to release
556 release_firmware(const struct firmware *fw)
558 struct builtin_fw *builtin;
561 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
563 if (fw->data == builtin->data)
573 struct firmware_work {
574 struct work_struct work;
575 struct module *module;
577 struct device *device;
579 void (*cont)(const struct firmware *fw, void *context);
584 request_firmware_work_func(void *arg)
586 struct firmware_work *fw_work = arg;
587 const struct firmware *fw;
593 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
596 fw_work->cont(NULL, fw_work->context);
598 fw_work->cont(fw, fw_work->context);
599 release_firmware(fw);
601 module_put(fw_work->module);
607 * request_firmware_nowait: asynchronous version of request_firmware
608 * @module: module requesting the firmware
609 * @uevent: sends uevent to copy the firmware image if this flag
610 * is non-zero else the firmware copy must be done manually.
611 * @name: name of firmware file
612 * @device: device for which firmware is being loaded
613 * @context: will be passed over to @cont, and
614 * @fw may be %NULL if firmware request fails.
615 * @cont: function will be called asynchronously when the firmware
618 * Asynchronous variant of request_firmware() for contexts where
619 * it is not possible to sleep.
622 request_firmware_nowait(
623 struct module *module, int uevent,
624 const char *name, struct device *device, void *context,
625 void (*cont)(const struct firmware *fw, void *context))
627 struct task_struct *task;
628 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
633 if (!try_module_get(module)) {
638 *fw_work = (struct firmware_work) {
647 task = kthread_run(request_firmware_work_func, fw_work,
648 "firmware/%s", name);
651 fw_work->cont(NULL, fw_work->context);
652 module_put(fw_work->module);
654 return PTR_ERR(task);
660 firmware_class_init(void)
663 error = class_register(&firmware_class);
665 printk(KERN_ERR "%s: class_register failed\n", __func__);
668 error = class_create_file(&firmware_class, &class_attr_timeout);
670 printk(KERN_ERR "%s: class_create_file failed\n",
672 class_unregister(&firmware_class);
678 firmware_class_exit(void)
680 class_unregister(&firmware_class);
683 fs_initcall(firmware_class_init);
684 module_exit(firmware_class_exit);
686 EXPORT_SYMBOL(release_firmware);
687 EXPORT_SYMBOL(request_firmware);
688 EXPORT_SYMBOL(request_firmware_nowait);