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 compat_firmware_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
35 extern struct builtin_fw __start_builtin_fw[];
36 extern struct builtin_fw __end_builtin_fw[];
38 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
40 struct builtin_fw *b_fw;
42 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
43 if (strcmp(name, b_fw->name) == 0) {
44 fw->size = b_fw->size;
45 fw->data = b_fw->data;
53 static bool fw_is_builtin_firmware(const struct firmware *fw)
55 struct builtin_fw *b_fw;
57 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
58 if (fw->data == b_fw->data)
64 #else /* Module case - no builtin firmware support */
66 static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
71 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
83 static int loading_timeout = 60; /* In seconds */
85 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
86 * guarding for corner cases a global lock should be OK */
87 static DEFINE_MUTEX(fw_lock);
89 struct firmware_priv {
90 struct completion completion;
96 struct timer_list timeout;
102 static struct firmware_priv *to_firmware_priv(struct device *dev)
104 return container_of(dev, struct firmware_priv, dev);
107 static void fw_load_abort(struct firmware_priv *fw_priv)
109 set_bit(FW_STATUS_ABORT, &fw_priv->status);
111 complete(&fw_priv->completion);
114 static ssize_t firmware_timeout_show(struct class *class,
117 return sprintf(buf, "%d\n", loading_timeout);
121 * firmware_timeout_store - set number of seconds to wait for firmware
122 * @class: device class pointer
123 * @buf: buffer to scan for timeout value
124 * @count: number of bytes in @buf
126 * Sets the number of seconds to wait for the firmware. Once
127 * this expires an error will be returned to the driver and no
128 * firmware will be provided.
130 * Note: zero means 'wait forever'.
132 static ssize_t firmware_timeout_store(struct class *class,
133 const char *buf, size_t count)
135 loading_timeout = simple_strtol(buf, NULL, 10);
136 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 = to_firmware_priv(dev);
153 for (i = 0; i < fw_priv->nr_pages; i++)
154 __free_page(fw_priv->pages[i]);
155 kfree(fw_priv->pages);
158 module_put(THIS_MODULE);
161 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24))
162 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
164 struct firmware_priv *fw_priv = to_firmware_priv(dev);
166 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
168 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
170 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
176 static int firmware_uevent(struct device *dev, char **envp,
177 int num_envp, char *buf, int size)
179 struct firmware_priv *fw_priv = to_firmware_priv(dev);
180 int error, len = 0, i = 0;
182 error = add_uevent_var(envp, num_envp, &i,
184 "FIRMWARE=%s", fw_priv->fw_id);
188 error = add_uevent_var(envp, num_envp, &i,
190 "TIMEOUT=%i", loading_timeout);
193 error = add_uevent_var(envp, num_envp, &i,
195 "ASYNC=%i", fw_priv->nowait);
206 static struct class firmware_class = {
207 .name = "compat_firmware",
208 .class_attrs = firmware_class_attrs,
209 .dev_uevent = firmware_uevent,
210 .dev_release = fw_dev_release,
213 static ssize_t firmware_loading_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
216 struct firmware_priv *fw_priv = to_firmware_priv(dev);
217 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
219 return sprintf(buf, "%d\n", loading);
222 static void firmware_free_data(const struct firmware *fw)
224 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
228 for (i = 0; i < PFN_UP(fw->size); i++)
229 __free_page(fw->pages[i]);
237 /* Some architectures don't have PAGE_KERNEL_RO */
238 #ifndef PAGE_KERNEL_RO
239 #define PAGE_KERNEL_RO PAGE_KERNEL
242 * firmware_loading_store - set value in the 'loading' control file
243 * @dev: device pointer
244 * @buf: buffer to scan for loading control value
245 * @count: number of bytes in @buf
247 * The relevant values are:
249 * 1: Start a load, discarding any previous partial load.
250 * 0: Conclude the load and hand the data to the driver code.
251 * -1: Conclude the load with an error and discard any written data.
253 static ssize_t firmware_loading_store(struct device *dev,
254 struct device_attribute *attr,
255 const char *buf, size_t count)
257 struct firmware_priv *fw_priv = to_firmware_priv(dev);
258 int loading = simple_strtol(buf, NULL, 10);
263 mutex_lock(&fw_lock);
265 mutex_unlock(&fw_lock);
268 firmware_free_data(fw_priv->fw);
269 memset(fw_priv->fw, 0, sizeof(struct firmware));
270 /* If the pages are not owned by 'struct firmware' */
271 for (i = 0; i < fw_priv->nr_pages; i++)
272 __free_page(fw_priv->pages[i]);
273 kfree(fw_priv->pages);
274 fw_priv->pages = NULL;
275 fw_priv->page_array_size = 0;
276 fw_priv->nr_pages = 0;
277 set_bit(FW_STATUS_LOADING, &fw_priv->status);
278 mutex_unlock(&fw_lock);
281 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
282 vunmap(fw_priv->fw->data);
283 fw_priv->fw->data = vmap(fw_priv->pages,
286 if (!fw_priv->fw->data) {
287 dev_err(dev, "%s: vmap() failed\n", __func__);
290 /* Pages are now owned by 'struct firmware' */
291 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
292 fw_priv->fw->pages = fw_priv->pages;
293 fw_priv->pages = NULL;
296 fw_priv->page_array_size = 0;
297 fw_priv->nr_pages = 0;
298 complete(&fw_priv->completion);
299 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
304 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
308 fw_load_abort(fw_priv);
315 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
317 #if defined(CONFIG_COMPAT_FIRMWARE_DATA_RW_NEEDS_FILP)
318 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
319 struct bin_attribute *bin_attr,
320 char *buffer, loff_t offset, size_t count)
322 static ssize_t firmware_data_read(struct kobject *kobj,
323 struct bin_attribute *bin_attr,
324 char *buffer, loff_t offset, size_t count)
327 struct device *dev = compat_firmware_to_dev(kobj);
328 struct firmware_priv *fw_priv = to_firmware_priv(dev);
332 mutex_lock(&fw_lock);
334 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
338 if (offset > fw->size) {
342 if (count > fw->size - offset)
343 count = fw->size - offset;
349 int page_nr = offset >> PAGE_SHIFT;
350 int page_ofs = offset & (PAGE_SIZE-1);
351 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
353 page_data = kmap(fw_priv->pages[page_nr]);
355 memcpy(buffer, page_data + page_ofs, page_cnt);
357 kunmap(fw_priv->pages[page_nr]);
363 mutex_unlock(&fw_lock);
367 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
369 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
371 /* If the array of pages is too small, grow it... */
372 if (fw_priv->page_array_size < pages_needed) {
373 int new_array_size = max(pages_needed,
374 fw_priv->page_array_size * 2);
375 struct page **new_pages;
377 new_pages = kmalloc(new_array_size * sizeof(void *),
380 fw_load_abort(fw_priv);
383 memcpy(new_pages, fw_priv->pages,
384 fw_priv->page_array_size * sizeof(void *));
385 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
386 (new_array_size - fw_priv->page_array_size));
387 kfree(fw_priv->pages);
388 fw_priv->pages = new_pages;
389 fw_priv->page_array_size = new_array_size;
392 while (fw_priv->nr_pages < pages_needed) {
393 fw_priv->pages[fw_priv->nr_pages] =
394 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
396 if (!fw_priv->pages[fw_priv->nr_pages]) {
397 fw_load_abort(fw_priv);
406 * firmware_data_write - write method for firmware
407 * @kobj: kobject for the device
408 * @bin_attr: bin_attr structure
409 * @buffer: buffer being written
410 * @offset: buffer offset for write in total data store area
411 * @count: buffer size
413 * Data written to the 'data' attribute will be later handed to
414 * the driver as a firmware image.
416 #if defined(CONFIG_COMPAT_FIRMWARE_DATA_RW_NEEDS_FILP)
417 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
418 struct bin_attribute *bin_attr,
419 char *buffer, loff_t offset, size_t count)
421 static ssize_t firmware_data_write(struct kobject *kobj,
422 struct bin_attribute *bin_attr,
423 char *buffer, loff_t offset, size_t count)
426 struct device *dev = compat_firmware_to_dev(kobj);
427 struct firmware_priv *fw_priv = to_firmware_priv(dev);
431 if (!capable(CAP_SYS_RAWIO))
434 mutex_lock(&fw_lock);
436 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
440 retval = fw_realloc_buffer(fw_priv, offset + count);
448 int page_nr = offset >> PAGE_SHIFT;
449 int page_ofs = offset & (PAGE_SIZE - 1);
450 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
452 page_data = kmap(fw_priv->pages[page_nr]);
454 memcpy(page_data + page_ofs, buffer, page_cnt);
456 kunmap(fw_priv->pages[page_nr]);
462 fw->size = max_t(size_t, offset, fw->size);
464 mutex_unlock(&fw_lock);
468 static struct bin_attribute firmware_attr_data = {
469 .attr = { .name = "data", .mode = 0644 },
471 .read = firmware_data_read,
472 .write = firmware_data_write,
475 static void firmware_class_timeout(u_long data)
477 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
479 fw_load_abort(fw_priv);
482 static struct firmware_priv *
483 fw_create_instance(struct firmware *firmware, const char *fw_name,
484 struct device *device, bool uevent, bool nowait)
486 struct firmware_priv *fw_priv;
487 struct device *f_dev;
490 fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
492 dev_err(device, "%s: kmalloc failed\n", __func__);
497 fw_priv->fw = firmware;
498 fw_priv->nowait = nowait;
499 strcpy(fw_priv->fw_id, fw_name);
500 init_completion(&fw_priv->completion);
501 setup_timer(&fw_priv->timeout,
502 firmware_class_timeout, (u_long) fw_priv);
504 f_dev = &fw_priv->dev;
506 device_initialize(f_dev);
507 dev_set_name(f_dev, "%s", dev_name(device));
508 f_dev->parent = device;
509 f_dev->class = &firmware_class;
511 dev_set_uevent_suppress(f_dev, true);
513 /* Need to pin this module until class device is destroyed */
514 __module_get(THIS_MODULE);
516 error = device_add(f_dev);
518 dev_err(device, "%s: device_register failed\n", __func__);
522 error = device_create_bin_file(f_dev, &firmware_attr_data);
524 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
528 error = device_create_file(f_dev, &dev_attr_loading);
530 dev_err(device, "%s: device_create_file failed\n", __func__);
531 goto err_del_bin_attr;
535 dev_set_uevent_suppress(f_dev, false);
540 device_remove_bin_file(f_dev, &firmware_attr_data);
546 return ERR_PTR(error);
549 static void fw_destroy_instance(struct firmware_priv *fw_priv)
551 struct device *f_dev = &fw_priv->dev;
553 device_remove_file(f_dev, &dev_attr_loading);
554 device_remove_bin_file(f_dev, &firmware_attr_data);
555 device_unregister(f_dev);
558 static int _request_firmware(const struct firmware **firmware_p,
559 const char *name, struct device *device,
560 bool uevent, bool nowait)
562 struct firmware_priv *fw_priv;
563 struct firmware *firmware;
569 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
571 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
577 if (fw_get_builtin_firmware(firmware, name)) {
578 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
583 dev_dbg(device, "firmware: requesting %s\n", name);
585 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
586 if (IS_ERR(fw_priv)) {
587 retval = PTR_ERR(fw_priv);
592 if (loading_timeout > 0)
593 mod_timer(&fw_priv->timeout,
594 round_jiffies_up(jiffies +
595 loading_timeout * HZ));
597 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
600 wait_for_completion(&fw_priv->completion);
602 set_bit(FW_STATUS_DONE, &fw_priv->status);
603 del_timer_sync(&fw_priv->timeout);
605 mutex_lock(&fw_lock);
606 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
609 mutex_unlock(&fw_lock);
611 fw_destroy_instance(fw_priv);
615 release_firmware(firmware);
623 * request_firmware: - send firmware request and wait for it
624 * @firmware_p: pointer to firmware image
625 * @name: name of firmware file
626 * @device: device for which firmware is being loaded
628 * @firmware_p will be used to return a firmware image by the name
629 * of @name for device @device.
631 * Should be called from user context where sleeping is allowed.
633 * @name will be used as $FIRMWARE in the uevent environment and
634 * should be distinctive enough not to be confused with any other
635 * firmware image for this or any other device.
638 request_firmware(const struct firmware **firmware_p, const char *name,
639 struct device *device)
642 return _request_firmware(firmware_p, name, device, uevent, false);
646 * release_firmware: - release the resource associated with a firmware image
647 * @fw: firmware resource to release
649 void release_firmware(const struct firmware *fw)
652 if (!fw_is_builtin_firmware(fw))
653 firmware_free_data(fw);
659 struct firmware_work {
660 struct work_struct work;
661 struct module *module;
663 struct device *device;
665 void (*cont)(const struct firmware *fw, void *context);
669 static int request_firmware_work_func(void *arg)
671 struct firmware_work *fw_work = arg;
672 const struct firmware *fw;
680 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
681 fw_work->uevent, true);
682 fw_work->cont(fw, fw_work->context);
684 module_put(fw_work->module);
691 * request_firmware_nowait - asynchronous version of request_firmware
692 * @module: module requesting the firmware
693 * @uevent: sends uevent to copy the firmware image if this flag
694 * is non-zero else the firmware copy must be done manually.
695 * @name: name of firmware file
696 * @device: device for which firmware is being loaded
697 * @gfp: allocation flags
698 * @context: will be passed over to @cont, and
699 * @fw may be %NULL if firmware request fails.
700 * @cont: function will be called asynchronously when the firmware
703 * Asynchronous variant of request_firmware() for user contexts where
704 * it is not possible to sleep for long time. It can't be called
705 * in atomic contexts.
708 request_firmware_nowait(
709 struct module *module, int uevent,
710 const char *name, struct device *device, gfp_t gfp, void *context,
711 void (*cont)(const struct firmware *fw, void *context))
713 struct task_struct *task;
714 struct firmware_work *fw_work;
716 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
720 fw_work->module = module;
721 fw_work->name = name;
722 fw_work->device = device;
723 fw_work->context = context;
724 fw_work->cont = cont;
725 fw_work->uevent = uevent;
727 if (!try_module_get(module)) {
732 task = kthread_run(request_firmware_work_func, fw_work,
733 "firmware/%s", name);
735 fw_work->cont(NULL, fw_work->context);
736 module_put(fw_work->module);
738 return PTR_ERR(task);
744 static int __init firmware_class_init(void)
746 return class_register(&firmware_class);
749 static void __exit firmware_class_exit(void)
751 class_unregister(&firmware_class);
754 fs_initcall(firmware_class_init);
755 module_exit(firmware_class_exit);
757 EXPORT_SYMBOL_GPL(release_firmware);
758 EXPORT_SYMBOL_GPL(request_firmware);
759 EXPORT_SYMBOL_GPL(request_firmware_nowait);