2 * firmware_class.c - Multi purpose firmware loading support
4 * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org>
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 <ranty@debian.org>");
27 MODULE_DESCRIPTION("Multi purpose firmware loading support");
28 MODULE_LICENSE("GPL");
35 FW_STATUS_READY_NOHOTPLUG,
38 static int loading_timeout = 60; /* In seconds */
40 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
41 * guarding for corner cases a global lock should be OK */
42 static DEFINE_MUTEX(fw_lock);
44 struct firmware_priv {
45 char fw_id[FIRMWARE_NAME_MAX];
46 struct completion completion;
47 struct bin_attribute attr_data;
51 struct timer_list timeout;
55 fw_load_abort(struct firmware_priv *fw_priv)
57 set_bit(FW_STATUS_ABORT, &fw_priv->status);
59 complete(&fw_priv->completion);
63 firmware_timeout_show(struct class *class, char *buf)
65 return sprintf(buf, "%d\n", loading_timeout);
69 * firmware_timeout_store - set number of seconds to wait for firmware
70 * @class: device class pointer
71 * @buf: buffer to scan for timeout value
72 * @count: number of bytes in @buf
74 * Sets the number of seconds to wait for the firmware. Once
75 * this expires an error will be returned to the driver and no
76 * firmware will be provided.
78 * Note: zero means 'wait forever'.
81 firmware_timeout_store(struct class *class, const char *buf, size_t count)
83 loading_timeout = simple_strtol(buf, NULL, 10);
84 if (loading_timeout < 0)
89 static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
91 static void fw_dev_release(struct device *dev);
93 static int firmware_uevent(struct device *dev, char **envp, int num_envp,
94 char *buffer, int buffer_size)
96 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
99 if (!test_bit(FW_STATUS_READY, &fw_priv->status))
102 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
103 "FIRMWARE=%s", fw_priv->fw_id))
105 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
106 "TIMEOUT=%i", loading_timeout))
113 static struct class firmware_class = {
115 .dev_uevent = firmware_uevent,
116 .dev_release = fw_dev_release,
119 static ssize_t firmware_loading_show(struct device *dev,
120 struct device_attribute *attr, char *buf)
122 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
123 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
124 return sprintf(buf, "%d\n", loading);
128 * firmware_loading_store - set value in the 'loading' control file
129 * @dev: device pointer
130 * @attr: device attribute pointer
131 * @buf: buffer to scan for loading control value
132 * @count: number of bytes in @buf
134 * The relevant values are:
136 * 1: Start a load, discarding any previous partial load.
137 * 0: Conclude the load and hand the data to the driver code.
138 * -1: Conclude the load with an error and discard any written data.
140 static ssize_t firmware_loading_store(struct device *dev,
141 struct device_attribute *attr,
142 const char *buf, size_t count)
144 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
145 int loading = simple_strtol(buf, NULL, 10);
149 mutex_lock(&fw_lock);
151 mutex_unlock(&fw_lock);
154 vfree(fw_priv->fw->data);
155 fw_priv->fw->data = NULL;
156 fw_priv->fw->size = 0;
157 fw_priv->alloc_size = 0;
158 set_bit(FW_STATUS_LOADING, &fw_priv->status);
159 mutex_unlock(&fw_lock);
162 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
163 complete(&fw_priv->completion);
164 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
169 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
173 fw_load_abort(fw_priv);
180 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
183 firmware_data_read(struct kobject *kobj,
184 char *buffer, loff_t offset, size_t count)
186 struct device *dev = to_dev(kobj);
187 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
189 ssize_t ret_count = count;
191 mutex_lock(&fw_lock);
193 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
197 if (offset > fw->size) {
201 if (offset + ret_count > fw->size)
202 ret_count = fw->size - offset;
204 memcpy(buffer, fw->data + offset, ret_count);
206 mutex_unlock(&fw_lock);
211 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
214 int new_size = fw_priv->alloc_size;
216 if (min_size <= fw_priv->alloc_size)
219 new_size = ALIGN(min_size, PAGE_SIZE);
220 new_data = vmalloc(new_size);
222 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
223 /* Make sure that we don't keep incomplete data */
224 fw_load_abort(fw_priv);
227 fw_priv->alloc_size = new_size;
228 if (fw_priv->fw->data) {
229 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
230 vfree(fw_priv->fw->data);
232 fw_priv->fw->data = new_data;
233 BUG_ON(min_size > fw_priv->alloc_size);
238 * firmware_data_write - write method for firmware
239 * @kobj: kobject for the device
240 * @buffer: buffer being written
241 * @offset: buffer offset for write in total data store area
242 * @count: buffer size
244 * Data written to the 'data' attribute will be later handed to
245 * the driver as a firmware image.
248 firmware_data_write(struct kobject *kobj,
249 char *buffer, loff_t offset, size_t count)
251 struct device *dev = to_dev(kobj);
252 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
256 if (!capable(CAP_SYS_RAWIO))
259 mutex_lock(&fw_lock);
261 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
265 retval = fw_realloc_buffer(fw_priv, offset + count);
269 memcpy(fw->data + offset, buffer, count);
271 fw->size = max_t(size_t, offset + count, fw->size);
274 mutex_unlock(&fw_lock);
278 static struct bin_attribute firmware_attr_data_tmpl = {
279 .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
281 .read = firmware_data_read,
282 .write = firmware_data_write,
285 static void fw_dev_release(struct device *dev)
287 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
292 module_put(THIS_MODULE);
296 firmware_class_timeout(u_long data)
298 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
299 fw_load_abort(fw_priv);
302 static inline void fw_setup_device_id(struct device *f_dev, struct device *dev)
304 /* XXX warning we should watch out for name collisions */
305 strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE);
308 static int fw_register_device(struct device **dev_p, const char *fw_name,
309 struct device *device)
312 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
314 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
318 if (!fw_priv || !f_dev) {
319 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
324 init_completion(&fw_priv->completion);
325 fw_priv->attr_data = firmware_attr_data_tmpl;
326 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
328 fw_priv->timeout.function = firmware_class_timeout;
329 fw_priv->timeout.data = (u_long) fw_priv;
330 init_timer(&fw_priv->timeout);
332 fw_setup_device_id(f_dev, device);
333 f_dev->parent = device;
334 f_dev->class = &firmware_class;
335 dev_set_drvdata(f_dev, fw_priv);
336 retval = device_register(f_dev);
338 printk(KERN_ERR "%s: device_register failed\n",
351 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
352 const char *fw_name, struct device *device,
355 struct device *f_dev;
356 struct firmware_priv *fw_priv;
360 retval = fw_register_device(&f_dev, fw_name, device);
364 /* Need to pin this module until class device is destroyed */
365 __module_get(THIS_MODULE);
367 fw_priv = dev_get_drvdata(f_dev);
370 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
372 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
377 retval = device_create_file(f_dev, &dev_attr_loading);
379 printk(KERN_ERR "%s: device_create_file failed\n",
385 set_bit(FW_STATUS_READY, &fw_priv->status);
387 set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status);
392 device_unregister(f_dev);
398 _request_firmware(const struct firmware **firmware_p, const char *name,
399 struct device *device, int uevent)
401 struct device *f_dev;
402 struct firmware_priv *fw_priv;
403 struct firmware *firmware;
409 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
411 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
417 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
421 fw_priv = dev_get_drvdata(f_dev);
424 if (loading_timeout > 0) {
425 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
426 add_timer(&fw_priv->timeout);
429 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
430 wait_for_completion(&fw_priv->completion);
431 set_bit(FW_STATUS_DONE, &fw_priv->status);
432 del_timer_sync(&fw_priv->timeout);
434 wait_for_completion(&fw_priv->completion);
436 mutex_lock(&fw_lock);
437 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
439 release_firmware(fw_priv->fw);
443 mutex_unlock(&fw_lock);
444 device_unregister(f_dev);
455 * request_firmware: - send firmware request and wait for it
456 * @firmware_p: pointer to firmware image
457 * @name: name of firmware file
458 * @device: device for which firmware is being loaded
460 * @firmware_p will be used to return a firmware image by the name
461 * of @name for device @device.
463 * Should be called from user context where sleeping is allowed.
465 * @name will be used as $FIRMWARE in the uevent environment and
466 * should be distinctive enough not to be confused with any other
467 * firmware image for this or any other device.
470 request_firmware(const struct firmware **firmware_p, const char *name,
471 struct device *device)
474 return _request_firmware(firmware_p, name, device, uevent);
478 * release_firmware: - release the resource associated with a firmware image
479 * @fw: firmware resource to release
482 release_firmware(const struct firmware *fw)
491 struct firmware_work {
492 struct work_struct work;
493 struct module *module;
495 struct device *device;
497 void (*cont)(const struct firmware *fw, void *context);
502 request_firmware_work_func(void *arg)
504 struct firmware_work *fw_work = arg;
505 const struct firmware *fw;
511 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
514 fw_work->cont(NULL, fw_work->context);
516 fw_work->cont(fw, fw_work->context);
517 release_firmware(fw);
519 module_put(fw_work->module);
525 * request_firmware_nowait: asynchronous version of request_firmware
526 * @module: module requesting the firmware
527 * @uevent: sends uevent to copy the firmware image if this flag
528 * is non-zero else the firmware copy must be done manually.
529 * @name: name of firmware file
530 * @device: device for which firmware is being loaded
531 * @context: will be passed over to @cont, and
532 * @fw may be %NULL if firmware request fails.
533 * @cont: function will be called asynchronously when the firmware
536 * Asynchronous variant of request_firmware() for contexts where
537 * it is not possible to sleep.
540 request_firmware_nowait(
541 struct module *module, int uevent,
542 const char *name, struct device *device, void *context,
543 void (*cont)(const struct firmware *fw, void *context))
545 struct task_struct *task;
546 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
551 if (!try_module_get(module)) {
556 *fw_work = (struct firmware_work) {
565 task = kthread_run(request_firmware_work_func, fw_work,
566 "firmware/%s", name);
569 fw_work->cont(NULL, fw_work->context);
570 module_put(fw_work->module);
572 return PTR_ERR(task);
578 firmware_class_init(void)
581 error = class_register(&firmware_class);
583 printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
586 error = class_create_file(&firmware_class, &class_attr_timeout);
588 printk(KERN_ERR "%s: class_create_file failed\n",
590 class_unregister(&firmware_class);
596 firmware_class_exit(void)
598 class_unregister(&firmware_class);
601 fs_initcall(firmware_class_init);
602 module_exit(firmware_class_exit);
604 EXPORT_SYMBOL(release_firmware);
605 EXPORT_SYMBOL(request_firmware);
606 EXPORT_SYMBOL(request_firmware_nowait);