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
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 static ssize_t firmware_data_read(struct kobject *kobj,
318 struct bin_attribute *bin_attr,
319 char *buffer, loff_t offset, size_t count)
321 struct device *dev = to_dev(kobj);
322 struct firmware_priv *fw_priv = to_firmware_priv(dev);
326 mutex_lock(&fw_lock);
328 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
332 if (offset > fw->size) {
336 if (count > fw->size - offset)
337 count = fw->size - offset;
343 int page_nr = offset >> PAGE_SHIFT;
344 int page_ofs = offset & (PAGE_SIZE-1);
345 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
347 page_data = kmap(fw_priv->pages[page_nr]);
349 memcpy(buffer, page_data + page_ofs, page_cnt);
351 kunmap(fw_priv->pages[page_nr]);
357 mutex_unlock(&fw_lock);
361 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
363 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
365 /* If the array of pages is too small, grow it... */
366 if (fw_priv->page_array_size < pages_needed) {
367 int new_array_size = max(pages_needed,
368 fw_priv->page_array_size * 2);
369 struct page **new_pages;
371 new_pages = kmalloc(new_array_size * sizeof(void *),
374 fw_load_abort(fw_priv);
377 memcpy(new_pages, fw_priv->pages,
378 fw_priv->page_array_size * sizeof(void *));
379 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
380 (new_array_size - fw_priv->page_array_size));
381 kfree(fw_priv->pages);
382 fw_priv->pages = new_pages;
383 fw_priv->page_array_size = new_array_size;
386 while (fw_priv->nr_pages < pages_needed) {
387 fw_priv->pages[fw_priv->nr_pages] =
388 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
390 if (!fw_priv->pages[fw_priv->nr_pages]) {
391 fw_load_abort(fw_priv);
400 * firmware_data_write - write method for firmware
401 * @kobj: kobject for the device
402 * @bin_attr: bin_attr structure
403 * @buffer: buffer being written
404 * @offset: buffer offset for write in total data store area
405 * @count: buffer size
407 * Data written to the 'data' attribute will be later handed to
408 * the driver as a firmware image.
410 static ssize_t firmware_data_write(struct kobject *kobj,
411 struct bin_attribute *bin_attr,
412 char *buffer, loff_t offset, size_t count)
414 struct device *dev = to_dev(kobj);
415 struct firmware_priv *fw_priv = to_firmware_priv(dev);
419 if (!capable(CAP_SYS_RAWIO))
422 mutex_lock(&fw_lock);
424 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
428 retval = fw_realloc_buffer(fw_priv, offset + count);
436 int page_nr = offset >> PAGE_SHIFT;
437 int page_ofs = offset & (PAGE_SIZE - 1);
438 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
440 page_data = kmap(fw_priv->pages[page_nr]);
442 memcpy(page_data + page_ofs, buffer, page_cnt);
444 kunmap(fw_priv->pages[page_nr]);
450 fw->size = max_t(size_t, offset, fw->size);
452 mutex_unlock(&fw_lock);
456 static struct bin_attribute firmware_attr_data = {
457 .attr = { .name = "data", .mode = 0644 },
459 .read = firmware_data_read,
460 .write = firmware_data_write,
463 static void firmware_class_timeout(u_long data)
465 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
467 fw_load_abort(fw_priv);
470 static struct firmware_priv *
471 fw_create_instance(struct firmware *firmware, const char *fw_name,
472 struct device *device, bool uevent, bool nowait)
474 struct firmware_priv *fw_priv;
475 struct device *f_dev;
478 fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
480 dev_err(device, "%s: kmalloc failed\n", __func__);
485 fw_priv->fw = firmware;
486 fw_priv->nowait = nowait;
487 strcpy(fw_priv->fw_id, fw_name);
488 init_completion(&fw_priv->completion);
489 setup_timer(&fw_priv->timeout,
490 firmware_class_timeout, (u_long) fw_priv);
492 f_dev = &fw_priv->dev;
494 device_initialize(f_dev);
495 dev_set_name(f_dev, "%s", dev_name(device));
496 f_dev->parent = device;
497 f_dev->class = &firmware_class;
499 dev_set_uevent_suppress(f_dev, true);
501 /* Need to pin this module until class device is destroyed */
502 __module_get(THIS_MODULE);
504 error = device_add(f_dev);
506 dev_err(device, "%s: device_register failed\n", __func__);
510 error = device_create_bin_file(f_dev, &firmware_attr_data);
512 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
516 error = device_create_file(f_dev, &dev_attr_loading);
518 dev_err(device, "%s: device_create_file failed\n", __func__);
519 goto err_del_bin_attr;
523 dev_set_uevent_suppress(f_dev, false);
528 device_remove_bin_file(f_dev, &firmware_attr_data);
534 return ERR_PTR(error);
537 static void fw_destroy_instance(struct firmware_priv *fw_priv)
539 struct device *f_dev = &fw_priv->dev;
541 device_remove_file(f_dev, &dev_attr_loading);
542 device_remove_bin_file(f_dev, &firmware_attr_data);
543 device_unregister(f_dev);
546 static int _request_firmware(const struct firmware **firmware_p,
547 const char *name, struct device *device,
548 bool uevent, bool nowait)
550 struct firmware_priv *fw_priv;
551 struct firmware *firmware;
557 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
559 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
565 if (fw_get_builtin_firmware(firmware, name)) {
566 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
571 dev_dbg(device, "firmware: requesting %s\n", name);
573 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
574 if (IS_ERR(fw_priv)) {
575 retval = PTR_ERR(fw_priv);
580 if (loading_timeout > 0)
581 mod_timer(&fw_priv->timeout,
582 round_jiffies_up(jiffies +
583 loading_timeout * HZ));
585 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
588 wait_for_completion(&fw_priv->completion);
590 set_bit(FW_STATUS_DONE, &fw_priv->status);
591 del_timer_sync(&fw_priv->timeout);
593 mutex_lock(&fw_lock);
594 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
597 mutex_unlock(&fw_lock);
599 fw_destroy_instance(fw_priv);
603 release_firmware(firmware);
611 * request_firmware: - send firmware request and wait for it
612 * @firmware_p: pointer to firmware image
613 * @name: name of firmware file
614 * @device: device for which firmware is being loaded
616 * @firmware_p will be used to return a firmware image by the name
617 * of @name for device @device.
619 * Should be called from user context where sleeping is allowed.
621 * @name will be used as $FIRMWARE in the uevent environment and
622 * should be distinctive enough not to be confused with any other
623 * firmware image for this or any other device.
626 request_firmware(const struct firmware **firmware_p, const char *name,
627 struct device *device)
630 return _request_firmware(firmware_p, name, device, uevent, false);
634 * release_firmware: - release the resource associated with a firmware image
635 * @fw: firmware resource to release
637 void release_firmware(const struct firmware *fw)
640 if (!fw_is_builtin_firmware(fw))
641 firmware_free_data(fw);
647 struct firmware_work {
648 struct work_struct work;
649 struct module *module;
651 struct device *device;
653 void (*cont)(const struct firmware *fw, void *context);
657 static int request_firmware_work_func(void *arg)
659 struct firmware_work *fw_work = arg;
660 const struct firmware *fw;
668 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
669 fw_work->uevent, true);
670 fw_work->cont(fw, fw_work->context);
672 module_put(fw_work->module);
679 * request_firmware_nowait - asynchronous version of request_firmware
680 * @module: module requesting the firmware
681 * @uevent: sends uevent to copy the firmware image if this flag
682 * is non-zero else the firmware copy must be done manually.
683 * @name: name of firmware file
684 * @device: device for which firmware is being loaded
685 * @gfp: allocation flags
686 * @context: will be passed over to @cont, and
687 * @fw may be %NULL if firmware request fails.
688 * @cont: function will be called asynchronously when the firmware
691 * Asynchronous variant of request_firmware() for user contexts where
692 * it is not possible to sleep for long time. It can't be called
693 * in atomic contexts.
696 request_firmware_nowait(
697 struct module *module, int uevent,
698 const char *name, struct device *device, gfp_t gfp, void *context,
699 void (*cont)(const struct firmware *fw, void *context))
701 struct task_struct *task;
702 struct firmware_work *fw_work;
704 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
708 fw_work->module = module;
709 fw_work->name = name;
710 fw_work->device = device;
711 fw_work->context = context;
712 fw_work->cont = cont;
713 fw_work->uevent = uevent;
715 if (!try_module_get(module)) {
720 task = kthread_run(request_firmware_work_func, fw_work,
721 "firmware/%s", name);
723 fw_work->cont(NULL, fw_work->context);
724 module_put(fw_work->module);
726 return PTR_ERR(task);
732 static int __init firmware_class_init(void)
734 return class_register(&firmware_class);
737 static void __exit firmware_class_exit(void)
739 class_unregister(&firmware_class);
742 fs_initcall(firmware_class_init);
743 module_exit(firmware_class_exit);
745 EXPORT_SYMBOL(release_firmware);
746 EXPORT_SYMBOL(request_firmware);
747 EXPORT_SYMBOL(request_firmware_nowait);