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;
52 #ifdef CONFIG_FW_LOADER
53 extern struct builtin_fw __start_builtin_fw[];
54 extern struct builtin_fw __end_builtin_fw[];
55 #else /* Module case. Avoid ifdefs later; it'll all optimise out */
56 static struct builtin_fw *__start_builtin_fw;
57 static struct builtin_fw *__end_builtin_fw;
61 fw_load_abort(struct firmware_priv *fw_priv)
63 set_bit(FW_STATUS_ABORT, &fw_priv->status);
65 complete(&fw_priv->completion);
69 firmware_timeout_show(struct class *class, char *buf)
71 return sprintf(buf, "%d\n", loading_timeout);
75 * firmware_timeout_store - set number of seconds to wait for firmware
76 * @class: device class pointer
77 * @buf: buffer to scan for timeout value
78 * @count: number of bytes in @buf
80 * Sets the number of seconds to wait for the firmware. Once
81 * this expires an error will be returned to the driver and no
82 * firmware will be provided.
84 * Note: zero means 'wait forever'.
87 firmware_timeout_store(struct class *class, const char *buf, size_t count)
89 loading_timeout = simple_strtol(buf, NULL, 10);
90 if (loading_timeout < 0)
95 static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
97 static void fw_dev_release(struct device *dev);
99 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
101 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
103 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
105 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
111 static struct class firmware_class = {
113 .dev_uevent = firmware_uevent,
114 .dev_release = fw_dev_release,
117 static ssize_t firmware_loading_show(struct device *dev,
118 struct device_attribute *attr, char *buf)
120 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
121 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
122 return sprintf(buf, "%d\n", loading);
126 * firmware_loading_store - set value in the 'loading' control file
127 * @dev: device pointer
128 * @attr: device attribute pointer
129 * @buf: buffer to scan for loading control value
130 * @count: number of bytes in @buf
132 * The relevant values are:
134 * 1: Start a load, discarding any previous partial load.
135 * 0: Conclude the load and hand the data to the driver code.
136 * -1: Conclude the load with an error and discard any written data.
138 static ssize_t firmware_loading_store(struct device *dev,
139 struct device_attribute *attr,
140 const char *buf, size_t count)
142 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
143 int loading = simple_strtol(buf, NULL, 10);
147 mutex_lock(&fw_lock);
149 mutex_unlock(&fw_lock);
152 vfree(fw_priv->fw->data);
153 fw_priv->fw->data = NULL;
154 fw_priv->fw->size = 0;
155 fw_priv->alloc_size = 0;
156 set_bit(FW_STATUS_LOADING, &fw_priv->status);
157 mutex_unlock(&fw_lock);
160 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
161 complete(&fw_priv->completion);
162 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
167 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
170 fw_load_abort(fw_priv);
177 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
180 firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
181 char *buffer, loff_t offset, size_t count)
183 struct device *dev = to_dev(kobj);
184 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
188 mutex_lock(&fw_lock);
190 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
194 ret_count = memory_read_from_buffer(buffer, count, &offset,
197 mutex_unlock(&fw_lock);
202 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
205 int new_size = fw_priv->alloc_size;
207 if (min_size <= fw_priv->alloc_size)
210 new_size = ALIGN(min_size, PAGE_SIZE);
211 new_data = vmalloc(new_size);
213 printk(KERN_ERR "%s: unable to alloc buffer\n", __func__);
214 /* Make sure that we don't keep incomplete data */
215 fw_load_abort(fw_priv);
218 fw_priv->alloc_size = new_size;
219 if (fw_priv->fw->data) {
220 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
221 vfree(fw_priv->fw->data);
223 fw_priv->fw->data = new_data;
224 BUG_ON(min_size > fw_priv->alloc_size);
229 * firmware_data_write - write method for firmware
230 * @kobj: kobject for the device
231 * @bin_attr: bin_attr structure
232 * @buffer: buffer being written
233 * @offset: buffer offset for write in total data store area
234 * @count: buffer size
236 * Data written to the 'data' attribute will be later handed to
237 * the driver as a firmware image.
240 firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
241 char *buffer, loff_t offset, size_t count)
243 struct device *dev = to_dev(kobj);
244 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
248 if (!capable(CAP_SYS_RAWIO))
251 mutex_lock(&fw_lock);
253 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
257 retval = fw_realloc_buffer(fw_priv, offset + count);
261 memcpy((u8 *)fw->data + offset, buffer, count);
263 fw->size = max_t(size_t, offset + count, fw->size);
266 mutex_unlock(&fw_lock);
270 static struct bin_attribute firmware_attr_data_tmpl = {
271 .attr = {.name = "data", .mode = 0644},
273 .read = firmware_data_read,
274 .write = firmware_data_write,
277 static void fw_dev_release(struct device *dev)
279 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
284 module_put(THIS_MODULE);
288 firmware_class_timeout(u_long data)
290 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
291 fw_load_abort(fw_priv);
294 static int fw_register_device(struct device **dev_p, const char *fw_name,
295 struct device *device)
298 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
300 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
304 if (!fw_priv || !f_dev) {
305 dev_err(device, "%s: kmalloc failed\n", __func__);
310 init_completion(&fw_priv->completion);
311 fw_priv->attr_data = firmware_attr_data_tmpl;
312 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
314 fw_priv->timeout.function = firmware_class_timeout;
315 fw_priv->timeout.data = (u_long) fw_priv;
316 init_timer(&fw_priv->timeout);
318 dev_set_name(f_dev, dev_name(device));
319 f_dev->parent = device;
320 f_dev->class = &firmware_class;
321 dev_set_drvdata(f_dev, fw_priv);
322 f_dev->uevent_suppress = 1;
323 retval = device_register(f_dev);
325 dev_err(device, "%s: device_register failed\n", __func__);
337 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
338 const char *fw_name, struct device *device,
341 struct device *f_dev;
342 struct firmware_priv *fw_priv;
346 retval = fw_register_device(&f_dev, fw_name, device);
350 /* Need to pin this module until class device is destroyed */
351 __module_get(THIS_MODULE);
353 fw_priv = dev_get_drvdata(f_dev);
356 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
358 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
362 retval = device_create_file(f_dev, &dev_attr_loading);
364 dev_err(device, "%s: device_create_file failed\n", __func__);
369 f_dev->uevent_suppress = 0;
374 device_unregister(f_dev);
380 _request_firmware(const struct firmware **firmware_p, const char *name,
381 struct device *device, int uevent)
383 struct device *f_dev;
384 struct firmware_priv *fw_priv;
385 struct firmware *firmware;
386 struct builtin_fw *builtin;
392 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
394 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
400 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
402 if (strcmp(name, builtin->name))
404 dev_info(device, "firmware: using built-in firmware %s\n",
406 firmware->size = builtin->size;
407 firmware->data = builtin->data;
412 dev_info(device, "firmware: requesting %s\n", name);
414 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
418 fw_priv = dev_get_drvdata(f_dev);
421 if (loading_timeout > 0) {
422 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
423 add_timer(&fw_priv->timeout);
426 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
427 wait_for_completion(&fw_priv->completion);
428 set_bit(FW_STATUS_DONE, &fw_priv->status);
429 del_timer_sync(&fw_priv->timeout);
431 wait_for_completion(&fw_priv->completion);
433 mutex_lock(&fw_lock);
434 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
436 release_firmware(fw_priv->fw);
440 mutex_unlock(&fw_lock);
441 device_unregister(f_dev);
452 * request_firmware: - send firmware request and wait for it
453 * @firmware_p: pointer to firmware image
454 * @name: name of firmware file
455 * @device: device for which firmware is being loaded
457 * @firmware_p will be used to return a firmware image by the name
458 * of @name for device @device.
460 * Should be called from user context where sleeping is allowed.
462 * @name will be used as $FIRMWARE in the uevent environment and
463 * should be distinctive enough not to be confused with any other
464 * firmware image for this or any other device.
467 request_firmware(const struct firmware **firmware_p, const char *name,
468 struct device *device)
471 return _request_firmware(firmware_p, name, device, uevent);
475 * release_firmware: - release the resource associated with a firmware image
476 * @fw: firmware resource to release
479 release_firmware(const struct firmware *fw)
481 struct builtin_fw *builtin;
484 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
486 if (fw->data == builtin->data)
496 struct firmware_work {
497 struct work_struct work;
498 struct module *module;
500 struct device *device;
502 void (*cont)(const struct firmware *fw, void *context);
507 request_firmware_work_func(void *arg)
509 struct firmware_work *fw_work = arg;
510 const struct firmware *fw;
516 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
519 fw_work->cont(NULL, fw_work->context);
521 fw_work->cont(fw, fw_work->context);
522 release_firmware(fw);
524 module_put(fw_work->module);
530 * request_firmware_nowait: asynchronous version of request_firmware
531 * @module: module requesting the firmware
532 * @uevent: sends uevent to copy the firmware image if this flag
533 * is non-zero else the firmware copy must be done manually.
534 * @name: name of firmware file
535 * @device: device for which firmware is being loaded
536 * @context: will be passed over to @cont, and
537 * @fw may be %NULL if firmware request fails.
538 * @cont: function will be called asynchronously when the firmware
541 * Asynchronous variant of request_firmware() for contexts where
542 * it is not possible to sleep.
545 request_firmware_nowait(
546 struct module *module, int uevent,
547 const char *name, struct device *device, void *context,
548 void (*cont)(const struct firmware *fw, void *context))
550 struct task_struct *task;
551 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
556 if (!try_module_get(module)) {
561 *fw_work = (struct firmware_work) {
570 task = kthread_run(request_firmware_work_func, fw_work,
571 "firmware/%s", name);
574 fw_work->cont(NULL, fw_work->context);
575 module_put(fw_work->module);
577 return PTR_ERR(task);
583 firmware_class_init(void)
586 error = class_register(&firmware_class);
588 printk(KERN_ERR "%s: class_register failed\n", __func__);
591 error = class_create_file(&firmware_class, &class_attr_timeout);
593 printk(KERN_ERR "%s: class_create_file failed\n",
595 class_unregister(&firmware_class);
601 firmware_class_exit(void)
603 class_unregister(&firmware_class);
606 fs_initcall(firmware_class_init);
607 module_exit(firmware_class_exit);
609 EXPORT_SYMBOL(release_firmware);
610 EXPORT_SYMBOL(request_firmware);
611 EXPORT_SYMBOL(request_firmware_nowait);