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 {
89 struct completion completion;
90 struct bin_attribute attr_data;
96 struct timer_list timeout;
102 fw_load_abort(struct firmware_priv *fw_priv)
104 set_bit(FW_STATUS_ABORT, &fw_priv->status);
106 complete(&fw_priv->completion);
110 firmware_timeout_show(struct class *class,
111 struct class_attribute *attr,
114 return sprintf(buf, "%d\n", loading_timeout);
118 * firmware_timeout_store - set number of seconds to wait for firmware
119 * @class: device class pointer
120 * @attr: device attribute pointer
121 * @buf: buffer to scan for timeout value
122 * @count: number of bytes in @buf
124 * Sets the number of seconds to wait for the firmware. Once
125 * this expires an error will be returned to the driver and no
126 * firmware will be provided.
128 * Note: zero means 'wait forever'.
131 firmware_timeout_store(struct class *class,
132 struct class_attribute *attr,
133 const char *buf, size_t count)
135 loading_timeout = simple_strtol(buf, NULL, 10);
136 if (loading_timeout < 0)
141 static struct class_attribute firmware_class_attrs[] = {
142 __ATTR(timeout, S_IWUSR | S_IRUGO,
143 firmware_timeout_show, firmware_timeout_store),
147 static void fw_dev_release(struct device *dev)
149 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
152 for (i = 0; i < fw_priv->nr_pages; i++)
153 __free_page(fw_priv->pages[i]);
154 kfree(fw_priv->pages);
158 module_put(THIS_MODULE);
161 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
163 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
165 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
167 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
169 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
175 static struct class firmware_class = {
177 .class_attrs = firmware_class_attrs,
178 .dev_uevent = firmware_uevent,
179 .dev_release = fw_dev_release,
182 static ssize_t firmware_loading_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
185 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
186 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
187 return sprintf(buf, "%d\n", loading);
190 static void firmware_free_data(const struct firmware *fw)
195 for (i = 0; i < PFN_UP(fw->size); i++)
196 __free_page(fw->pages[i]);
201 /* Some architectures don't have PAGE_KERNEL_RO */
202 #ifndef PAGE_KERNEL_RO
203 #define PAGE_KERNEL_RO PAGE_KERNEL
206 * firmware_loading_store - set value in the 'loading' control file
207 * @dev: device pointer
208 * @attr: device attribute pointer
209 * @buf: buffer to scan for loading control value
210 * @count: number of bytes in @buf
212 * The relevant values are:
214 * 1: Start a load, discarding any previous partial load.
215 * 0: Conclude the load and hand the data to the driver code.
216 * -1: Conclude the load with an error and discard any written data.
218 static ssize_t firmware_loading_store(struct device *dev,
219 struct device_attribute *attr,
220 const char *buf, size_t count)
222 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
223 int loading = simple_strtol(buf, NULL, 10);
228 mutex_lock(&fw_lock);
230 mutex_unlock(&fw_lock);
233 firmware_free_data(fw_priv->fw);
234 memset(fw_priv->fw, 0, sizeof(struct firmware));
235 /* If the pages are not owned by 'struct firmware' */
236 for (i = 0; i < fw_priv->nr_pages; i++)
237 __free_page(fw_priv->pages[i]);
238 kfree(fw_priv->pages);
239 fw_priv->pages = NULL;
240 fw_priv->page_array_size = 0;
241 fw_priv->nr_pages = 0;
242 set_bit(FW_STATUS_LOADING, &fw_priv->status);
243 mutex_unlock(&fw_lock);
246 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
247 vunmap(fw_priv->fw->data);
248 fw_priv->fw->data = vmap(fw_priv->pages,
251 if (!fw_priv->fw->data) {
252 dev_err(dev, "%s: vmap() failed\n", __func__);
255 /* Pages are now owned by 'struct firmware' */
256 fw_priv->fw->pages = fw_priv->pages;
257 fw_priv->pages = NULL;
259 fw_priv->page_array_size = 0;
260 fw_priv->nr_pages = 0;
261 complete(&fw_priv->completion);
262 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
267 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
271 fw_load_abort(fw_priv);
278 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
281 firmware_data_read(struct file *filp, struct kobject *kobj,
282 struct bin_attribute *bin_attr, char *buffer, loff_t offset,
285 struct device *dev = to_dev(kobj);
286 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
290 mutex_lock(&fw_lock);
292 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
296 if (offset > fw->size) {
300 if (count > fw->size - offset)
301 count = fw->size - offset;
307 int page_nr = offset >> PAGE_SHIFT;
308 int page_ofs = offset & (PAGE_SIZE-1);
309 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
311 page_data = kmap(fw_priv->pages[page_nr]);
313 memcpy(buffer, page_data + page_ofs, page_cnt);
315 kunmap(fw_priv->pages[page_nr]);
321 mutex_unlock(&fw_lock);
326 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
328 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
330 /* If the array of pages is too small, grow it... */
331 if (fw_priv->page_array_size < pages_needed) {
332 int new_array_size = max(pages_needed,
333 fw_priv->page_array_size * 2);
334 struct page **new_pages;
336 new_pages = kmalloc(new_array_size * sizeof(void *),
339 fw_load_abort(fw_priv);
342 memcpy(new_pages, fw_priv->pages,
343 fw_priv->page_array_size * sizeof(void *));
344 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
345 (new_array_size - fw_priv->page_array_size));
346 kfree(fw_priv->pages);
347 fw_priv->pages = new_pages;
348 fw_priv->page_array_size = new_array_size;
351 while (fw_priv->nr_pages < pages_needed) {
352 fw_priv->pages[fw_priv->nr_pages] =
353 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
355 if (!fw_priv->pages[fw_priv->nr_pages]) {
356 fw_load_abort(fw_priv);
365 * firmware_data_write - write method for firmware
366 * @filp: open sysfs file
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 file* filp, struct kobject *kobj,
378 struct bin_attribute *bin_attr, char *buffer,
379 loff_t offset, size_t count)
381 struct device *dev = to_dev(kobj);
382 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
386 if (!capable(CAP_SYS_RAWIO))
389 mutex_lock(&fw_lock);
391 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
395 retval = fw_realloc_buffer(fw_priv, offset + count);
403 int page_nr = offset >> PAGE_SHIFT;
404 int page_ofs = offset & (PAGE_SIZE - 1);
405 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
407 page_data = kmap(fw_priv->pages[page_nr]);
409 memcpy(page_data + page_ofs, buffer, page_cnt);
411 kunmap(fw_priv->pages[page_nr]);
417 fw->size = max_t(size_t, offset, fw->size);
419 mutex_unlock(&fw_lock);
423 static struct bin_attribute firmware_attr_data_tmpl = {
424 .attr = {.name = "data", .mode = 0644},
426 .read = firmware_data_read,
427 .write = firmware_data_write,
431 firmware_class_timeout(u_long data)
433 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
434 fw_load_abort(fw_priv);
437 static int fw_register_device(struct device **dev_p, const char *fw_name,
438 struct device *device)
441 struct firmware_priv *fw_priv =
442 kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
443 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
447 if (!fw_priv || !f_dev) {
448 dev_err(device, "%s: kmalloc failed\n", __func__);
453 strcpy(fw_priv->fw_id, fw_name);
454 init_completion(&fw_priv->completion);
455 fw_priv->attr_data = firmware_attr_data_tmpl;
456 fw_priv->timeout.function = firmware_class_timeout;
457 fw_priv->timeout.data = (u_long) fw_priv;
458 init_timer(&fw_priv->timeout);
460 dev_set_name(f_dev, "%s", dev_name(device));
461 f_dev->parent = device;
462 f_dev->class = &firmware_class;
463 dev_set_drvdata(f_dev, fw_priv);
464 dev_set_uevent_suppress(f_dev, 1);
465 retval = device_register(f_dev);
467 dev_err(device, "%s: device_register failed\n", __func__);
480 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
481 const char *fw_name, struct device *device,
482 int uevent, bool nowait)
484 struct device *f_dev;
485 struct firmware_priv *fw_priv;
489 retval = fw_register_device(&f_dev, fw_name, device);
493 /* Need to pin this module until class device is destroyed */
494 __module_get(THIS_MODULE);
496 fw_priv = dev_get_drvdata(f_dev);
498 fw_priv->nowait = nowait;
501 sysfs_bin_attr_init(&fw_priv->attr_data);
502 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
504 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
508 retval = device_create_file(f_dev, &dev_attr_loading);
510 dev_err(device, "%s: device_create_file failed\n", __func__);
515 dev_set_uevent_suppress(f_dev, 0);
520 device_unregister(f_dev);
526 _request_firmware(const struct firmware **firmware_p, const char *name,
527 struct device *device, int uevent, bool nowait)
529 struct device *f_dev;
530 struct firmware_priv *fw_priv;
531 struct firmware *firmware;
537 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
539 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
545 if (fw_get_builtin_firmware(firmware, name)) {
546 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
551 dev_dbg(device, "firmware: requesting %s\n", name);
553 retval = fw_setup_device(firmware, &f_dev, name, device,
558 fw_priv = dev_get_drvdata(f_dev);
561 if (loading_timeout > 0) {
562 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
563 add_timer(&fw_priv->timeout);
566 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
567 wait_for_completion(&fw_priv->completion);
568 set_bit(FW_STATUS_DONE, &fw_priv->status);
569 del_timer_sync(&fw_priv->timeout);
571 wait_for_completion(&fw_priv->completion);
573 mutex_lock(&fw_lock);
574 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
576 release_firmware(fw_priv->fw);
580 mutex_unlock(&fw_lock);
581 device_unregister(f_dev);
592 * request_firmware: - send firmware request and wait for it
593 * @firmware_p: pointer to firmware image
594 * @name: name of firmware file
595 * @device: device for which firmware is being loaded
597 * @firmware_p will be used to return a firmware image by the name
598 * of @name for device @device.
600 * Should be called from user context where sleeping is allowed.
602 * @name will be used as $FIRMWARE in the uevent environment and
603 * should be distinctive enough not to be confused with any other
604 * firmware image for this or any other device.
607 request_firmware(const struct firmware **firmware_p, const char *name,
608 struct device *device)
611 return _request_firmware(firmware_p, name, device, uevent, false);
615 * release_firmware: - release the resource associated with a firmware image
616 * @fw: firmware resource to release
618 void release_firmware(const struct firmware *fw)
621 if (!fw_is_builtin_firmware(fw))
622 firmware_free_data(fw);
628 struct firmware_work {
629 struct work_struct work;
630 struct module *module;
632 struct device *device;
634 void (*cont)(const struct firmware *fw, void *context);
639 request_firmware_work_func(void *arg)
641 struct firmware_work *fw_work = arg;
642 const struct firmware *fw;
648 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
649 fw_work->uevent, true);
651 fw_work->cont(fw, fw_work->context);
653 module_put(fw_work->module);
659 * request_firmware_nowait - asynchronous version of request_firmware
660 * @module: module requesting the firmware
661 * @uevent: sends uevent to copy the firmware image if this flag
662 * is non-zero else the firmware copy must be done manually.
663 * @name: name of firmware file
664 * @device: device for which firmware is being loaded
665 * @gfp: allocation flags
666 * @context: will be passed over to @cont, and
667 * @fw may be %NULL if firmware request fails.
668 * @cont: function will be called asynchronously when the firmware
671 * Asynchronous variant of request_firmware() for user contexts where
672 * it is not possible to sleep for long time. It can't be called
673 * in atomic contexts.
676 request_firmware_nowait(
677 struct module *module, int uevent,
678 const char *name, struct device *device, gfp_t gfp, void *context,
679 void (*cont)(const struct firmware *fw, void *context))
681 struct task_struct *task;
682 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
687 if (!try_module_get(module)) {
692 *fw_work = (struct firmware_work) {
701 task = kthread_run(request_firmware_work_func, fw_work,
702 "firmware/%s", name);
705 fw_work->cont(NULL, fw_work->context);
706 module_put(fw_work->module);
708 return PTR_ERR(task);
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);