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>
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;
49 struct timer_list timeout;
53 fw_load_abort(struct firmware_priv *fw_priv)
55 set_bit(FW_STATUS_ABORT, &fw_priv->status);
57 complete(&fw_priv->completion);
61 firmware_timeout_show(struct class *class, char *buf)
63 return sprintf(buf, "%d\n", loading_timeout);
67 * firmware_timeout_store - set number of seconds to wait for firmware
68 * @class: device class pointer
69 * @buf: buffer to scan for timeout value
70 * @count: number of bytes in @buf
72 * Sets the number of seconds to wait for the firmware. Once
73 * this expires an error will be returned to the driver and no
74 * firmware will be provided.
76 * Note: zero means 'wait forever'.
79 firmware_timeout_store(struct class *class, const char *buf, size_t count)
81 loading_timeout = simple_strtol(buf, NULL, 10);
82 if (loading_timeout < 0)
87 static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
89 static void fw_dev_release(struct device *dev);
91 static int firmware_uevent(struct device *dev, char **envp, int num_envp,
92 char *buffer, int buffer_size)
94 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
97 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
98 "FIRMWARE=%s", fw_priv->fw_id))
100 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
101 "TIMEOUT=%i", loading_timeout))
108 static struct class firmware_class = {
110 .dev_uevent = firmware_uevent,
111 .dev_release = fw_dev_release,
114 static ssize_t firmware_loading_show(struct device *dev,
115 struct device_attribute *attr, char *buf)
117 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
118 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
119 return sprintf(buf, "%d\n", loading);
123 * firmware_loading_store - set value in the 'loading' control file
124 * @dev: device pointer
125 * @attr: device attribute pointer
126 * @buf: buffer to scan for loading control value
127 * @count: number of bytes in @buf
129 * The relevant values are:
131 * 1: Start a load, discarding any previous partial load.
132 * 0: Conclude the load and hand the data to the driver code.
133 * -1: Conclude the load with an error and discard any written data.
135 static ssize_t firmware_loading_store(struct device *dev,
136 struct device_attribute *attr,
137 const char *buf, size_t count)
139 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
140 int loading = simple_strtol(buf, NULL, 10);
144 mutex_lock(&fw_lock);
146 mutex_unlock(&fw_lock);
149 vfree(fw_priv->fw->data);
150 fw_priv->fw->data = NULL;
151 fw_priv->fw->size = 0;
152 fw_priv->alloc_size = 0;
153 set_bit(FW_STATUS_LOADING, &fw_priv->status);
154 mutex_unlock(&fw_lock);
157 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
158 complete(&fw_priv->completion);
159 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
164 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
168 fw_load_abort(fw_priv);
175 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
178 firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
179 char *buffer, loff_t offset, size_t count)
181 struct device *dev = to_dev(kobj);
182 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
184 ssize_t ret_count = count;
186 mutex_lock(&fw_lock);
188 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
192 if (offset > fw->size) {
196 if (offset + ret_count > fw->size)
197 ret_count = fw->size - offset;
199 memcpy(buffer, fw->data + offset, ret_count);
201 mutex_unlock(&fw_lock);
206 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
209 int new_size = fw_priv->alloc_size;
211 if (min_size <= fw_priv->alloc_size)
214 new_size = ALIGN(min_size, PAGE_SIZE);
215 new_data = vmalloc(new_size);
217 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
218 /* Make sure that we don't keep incomplete data */
219 fw_load_abort(fw_priv);
222 fw_priv->alloc_size = new_size;
223 if (fw_priv->fw->data) {
224 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
225 vfree(fw_priv->fw->data);
227 fw_priv->fw->data = new_data;
228 BUG_ON(min_size > fw_priv->alloc_size);
233 * firmware_data_write - write method for firmware
234 * @kobj: kobject for the device
235 * @buffer: buffer being written
236 * @offset: buffer offset for write in total data store area
237 * @count: buffer size
239 * Data written to the 'data' attribute will be later handed to
240 * the driver as a firmware image.
243 firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
244 char *buffer, loff_t offset, size_t count)
246 struct device *dev = to_dev(kobj);
247 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
251 if (!capable(CAP_SYS_RAWIO))
254 mutex_lock(&fw_lock);
256 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
260 retval = fw_realloc_buffer(fw_priv, offset + count);
264 memcpy(fw->data + offset, buffer, count);
266 fw->size = max_t(size_t, offset + count, fw->size);
269 mutex_unlock(&fw_lock);
273 static struct bin_attribute firmware_attr_data_tmpl = {
274 .attr = {.name = "data", .mode = 0644},
276 .read = firmware_data_read,
277 .write = firmware_data_write,
280 static void fw_dev_release(struct device *dev)
282 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
287 module_put(THIS_MODULE);
291 firmware_class_timeout(u_long data)
293 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
294 fw_load_abort(fw_priv);
297 static inline void fw_setup_device_id(struct device *f_dev, struct device *dev)
299 /* XXX warning we should watch out for name collisions */
300 strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE);
303 static int fw_register_device(struct device **dev_p, const char *fw_name,
304 struct device *device)
307 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
309 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
313 if (!fw_priv || !f_dev) {
314 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
319 init_completion(&fw_priv->completion);
320 fw_priv->attr_data = firmware_attr_data_tmpl;
321 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
323 fw_priv->timeout.function = firmware_class_timeout;
324 fw_priv->timeout.data = (u_long) fw_priv;
325 init_timer(&fw_priv->timeout);
327 fw_setup_device_id(f_dev, device);
328 f_dev->parent = device;
329 f_dev->class = &firmware_class;
330 dev_set_drvdata(f_dev, fw_priv);
331 f_dev->uevent_suppress = 1;
332 retval = device_register(f_dev);
334 printk(KERN_ERR "%s: device_register failed\n",
347 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
348 const char *fw_name, struct device *device,
351 struct device *f_dev;
352 struct firmware_priv *fw_priv;
356 retval = fw_register_device(&f_dev, fw_name, device);
360 /* Need to pin this module until class device is destroyed */
361 __module_get(THIS_MODULE);
363 fw_priv = dev_get_drvdata(f_dev);
366 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
368 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
373 retval = device_create_file(f_dev, &dev_attr_loading);
375 printk(KERN_ERR "%s: device_create_file failed\n",
381 f_dev->uevent_suppress = 0;
386 device_unregister(f_dev);
392 _request_firmware(const struct firmware **firmware_p, const char *name,
393 struct device *device, int uevent)
395 struct device *f_dev;
396 struct firmware_priv *fw_priv;
397 struct firmware *firmware;
403 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
405 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
411 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
415 fw_priv = dev_get_drvdata(f_dev);
418 if (loading_timeout > 0) {
419 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
420 add_timer(&fw_priv->timeout);
423 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
424 wait_for_completion(&fw_priv->completion);
425 set_bit(FW_STATUS_DONE, &fw_priv->status);
426 del_timer_sync(&fw_priv->timeout);
428 wait_for_completion(&fw_priv->completion);
430 mutex_lock(&fw_lock);
431 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
433 release_firmware(fw_priv->fw);
437 mutex_unlock(&fw_lock);
438 device_unregister(f_dev);
449 * request_firmware: - send firmware request and wait for it
450 * @firmware_p: pointer to firmware image
451 * @name: name of firmware file
452 * @device: device for which firmware is being loaded
454 * @firmware_p will be used to return a firmware image by the name
455 * of @name for device @device.
457 * Should be called from user context where sleeping is allowed.
459 * @name will be used as $FIRMWARE in the uevent environment and
460 * should be distinctive enough not to be confused with any other
461 * firmware image for this or any other device.
464 request_firmware(const struct firmware **firmware_p, const char *name,
465 struct device *device)
468 return _request_firmware(firmware_p, name, device, uevent);
472 * release_firmware: - release the resource associated with a firmware image
473 * @fw: firmware resource to release
476 release_firmware(const struct firmware *fw)
485 struct firmware_work {
486 struct work_struct work;
487 struct module *module;
489 struct device *device;
491 void (*cont)(const struct firmware *fw, void *context);
496 request_firmware_work_func(void *arg)
498 struct firmware_work *fw_work = arg;
499 const struct firmware *fw;
505 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
508 fw_work->cont(NULL, fw_work->context);
510 fw_work->cont(fw, fw_work->context);
511 release_firmware(fw);
513 module_put(fw_work->module);
519 * request_firmware_nowait: asynchronous version of request_firmware
520 * @module: module requesting the firmware
521 * @uevent: sends uevent to copy the firmware image if this flag
522 * is non-zero else the firmware copy must be done manually.
523 * @name: name of firmware file
524 * @device: device for which firmware is being loaded
525 * @context: will be passed over to @cont, and
526 * @fw may be %NULL if firmware request fails.
527 * @cont: function will be called asynchronously when the firmware
530 * Asynchronous variant of request_firmware() for contexts where
531 * it is not possible to sleep.
534 request_firmware_nowait(
535 struct module *module, int uevent,
536 const char *name, struct device *device, void *context,
537 void (*cont)(const struct firmware *fw, void *context))
539 struct task_struct *task;
540 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
545 if (!try_module_get(module)) {
550 *fw_work = (struct firmware_work) {
559 task = kthread_run(request_firmware_work_func, fw_work,
560 "firmware/%s", name);
563 fw_work->cont(NULL, fw_work->context);
564 module_put(fw_work->module);
566 return PTR_ERR(task);
572 firmware_class_init(void)
575 error = class_register(&firmware_class);
577 printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
580 error = class_create_file(&firmware_class, &class_attr_timeout);
582 printk(KERN_ERR "%s: class_create_file failed\n",
584 class_unregister(&firmware_class);
590 firmware_class_exit(void)
592 class_unregister(&firmware_class);
595 fs_initcall(firmware_class_init);
596 module_exit(firmware_class_exit);
598 EXPORT_SYMBOL(release_firmware);
599 EXPORT_SYMBOL(request_firmware);
600 EXPORT_SYMBOL(request_firmware_nowait);