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/device.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/timer.h>
14 #include <linux/vmalloc.h>
15 #include <linux/interrupt.h>
16 #include <linux/bitops.h>
17 #include <asm/semaphore.h>
19 #include <linux/firmware.h>
22 MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
23 MODULE_DESCRIPTION("Multi purpose firmware loading support");
24 MODULE_LICENSE("GPL");
33 static int loading_timeout = 10; /* In seconds */
35 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
36 * guarding for corner cases a global lock should be OK */
37 static DECLARE_MUTEX(fw_lock);
39 struct firmware_priv {
40 char fw_id[FIRMWARE_NAME_MAX];
41 struct completion completion;
42 struct bin_attribute attr_data;
46 struct timer_list timeout;
50 fw_load_abort(struct firmware_priv *fw_priv)
52 set_bit(FW_STATUS_ABORT, &fw_priv->status);
54 complete(&fw_priv->completion);
58 firmware_timeout_show(struct class *class, char *buf)
60 return sprintf(buf, "%d\n", loading_timeout);
64 * firmware_timeout_store:
66 * Sets the number of seconds to wait for the firmware. Once
67 * this expires an error will be return to the driver and no
68 * firmware will be provided.
70 * Note: zero means 'wait for ever'
74 firmware_timeout_store(struct class *class, const char *buf, size_t count)
76 loading_timeout = simple_strtol(buf, NULL, 10);
80 static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
82 static void fw_class_dev_release(struct class_device *class_dev);
83 int firmware_class_hotplug(struct class_device *dev, char **envp,
84 int num_envp, char *buffer, int buffer_size);
86 static struct class firmware_class = {
88 .hotplug = firmware_class_hotplug,
89 .release = fw_class_dev_release,
93 firmware_class_hotplug(struct class_device *class_dev, char **envp,
94 int num_envp, char *buffer, int buffer_size)
96 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
99 if (!test_bit(FW_STATUS_READY, &fw_priv->status))
102 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
103 "FIRMWARE=%s", fw_priv->fw_id))
112 firmware_loading_show(struct class_device *class_dev, char *buf)
114 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
115 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
116 return sprintf(buf, "%d\n", loading);
120 * firmware_loading_store: - loading control file
122 * The relevant values are:
124 * 1: Start a load, discarding any previous partial load.
125 * 0: Conclude the load and handle the data to the driver code.
126 * -1: Conclude the load with an error and discard any written data.
129 firmware_loading_store(struct class_device *class_dev,
130 const char *buf, size_t count)
132 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
133 int loading = simple_strtol(buf, NULL, 10);
138 vfree(fw_priv->fw->data);
139 fw_priv->fw->data = NULL;
140 fw_priv->fw->size = 0;
141 fw_priv->alloc_size = 0;
142 set_bit(FW_STATUS_LOADING, &fw_priv->status);
146 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
147 complete(&fw_priv->completion);
148 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
153 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
157 fw_load_abort(fw_priv);
164 static CLASS_DEVICE_ATTR(loading, 0644,
165 firmware_loading_show, firmware_loading_store);
168 firmware_data_read(struct kobject *kobj,
169 char *buffer, loff_t offset, size_t count)
171 struct class_device *class_dev = to_class_dev(kobj);
172 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
174 ssize_t ret_count = count;
178 if (test_bit(FW_STATUS_DONE, &fw_priv->status)) {
182 if (offset > fw->size) {
186 if (offset + ret_count > fw->size)
187 ret_count = fw->size - offset;
189 memcpy(buffer, fw->data + offset, ret_count);
195 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
199 if (min_size <= fw_priv->alloc_size)
202 new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
204 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
205 /* Make sure that we don't keep incomplete data */
206 fw_load_abort(fw_priv);
209 fw_priv->alloc_size += PAGE_SIZE;
210 if (fw_priv->fw->data) {
211 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
212 vfree(fw_priv->fw->data);
214 fw_priv->fw->data = new_data;
215 BUG_ON(min_size > fw_priv->alloc_size);
220 * firmware_data_write:
224 * Data written to the 'data' attribute will be later handled to
225 * the driver as a firmware image.
228 firmware_data_write(struct kobject *kobj,
229 char *buffer, loff_t offset, size_t count)
231 struct class_device *class_dev = to_class_dev(kobj);
232 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
236 if (!capable(CAP_SYS_RAWIO))
240 if (test_bit(FW_STATUS_DONE, &fw_priv->status)) {
244 retval = fw_realloc_buffer(fw_priv, offset + count);
248 memcpy(fw->data + offset, buffer, count);
250 fw->size = max_t(size_t, offset + count, fw->size);
256 static struct bin_attribute firmware_attr_data_tmpl = {
257 .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
259 .read = firmware_data_read,
260 .write = firmware_data_write,
264 fw_class_dev_release(struct class_device *class_dev)
266 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
271 module_put(THIS_MODULE);
275 firmware_class_timeout(u_long data)
277 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
278 fw_load_abort(fw_priv);
282 fw_setup_class_device_id(struct class_device *class_dev, struct device *dev)
284 /* XXX warning we should watch out for name collisions */
285 strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE);
289 fw_register_class_device(struct class_device **class_dev_p,
290 const char *fw_name, struct device *device)
293 struct firmware_priv *fw_priv = kmalloc(sizeof (struct firmware_priv),
295 struct class_device *class_dev = kmalloc(sizeof (struct class_device),
300 if (!fw_priv || !class_dev) {
301 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
305 memset(fw_priv, 0, sizeof (*fw_priv));
306 memset(class_dev, 0, sizeof (*class_dev));
308 init_completion(&fw_priv->completion);
309 fw_priv->attr_data = firmware_attr_data_tmpl;
310 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
312 fw_priv->timeout.function = firmware_class_timeout;
313 fw_priv->timeout.data = (u_long) fw_priv;
314 init_timer(&fw_priv->timeout);
316 fw_setup_class_device_id(class_dev, device);
317 class_dev->dev = device;
318 class_dev->class = &firmware_class;
319 class_set_devdata(class_dev, fw_priv);
320 retval = class_device_register(class_dev);
322 printk(KERN_ERR "%s: class_device_register failed\n",
326 *class_dev_p = class_dev;
336 fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
337 const char *fw_name, struct device *device)
339 struct class_device *class_dev;
340 struct firmware_priv *fw_priv;
344 retval = fw_register_class_device(&class_dev, fw_name, device);
348 /* Need to pin this module until class device is destroyed */
349 __module_get(THIS_MODULE);
351 fw_priv = class_get_devdata(class_dev);
354 retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data);
356 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
361 retval = class_device_create_file(class_dev,
362 &class_device_attr_loading);
364 printk(KERN_ERR "%s: class_device_create_file failed\n",
369 set_bit(FW_STATUS_READY, &fw_priv->status);
370 *class_dev_p = class_dev;
374 class_device_unregister(class_dev);
380 * request_firmware: - request firmware to hotplug and wait for it
382 * @firmware will be used to return a firmware image by the name
383 * of @name for device @device.
385 * Should be called from user context where sleeping is allowed.
387 * @name will be use as $FIRMWARE in the hotplug environment and
388 * should be distinctive enough not to be confused with any other
389 * firmware image for this or any other device.
392 request_firmware(const struct firmware **firmware_p, const char *name,
393 struct device *device)
395 struct class_device *class_dev;
396 struct firmware_priv *fw_priv;
397 struct firmware *firmware;
403 *firmware_p = firmware = kmalloc(sizeof (struct firmware), GFP_KERNEL);
405 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
410 memset(firmware, 0, sizeof (*firmware));
412 retval = fw_setup_class_device(firmware, &class_dev, name, device);
416 fw_priv = class_get_devdata(class_dev);
418 if (loading_timeout) {
419 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
420 add_timer(&fw_priv->timeout);
423 kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
424 wait_for_completion(&fw_priv->completion);
425 set_bit(FW_STATUS_DONE, &fw_priv->status);
427 del_timer_sync(&fw_priv->timeout);
430 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
432 release_firmware(fw_priv->fw);
437 class_device_unregister(class_dev);
448 * release_firmware: - release the resource associated with a firmware image
451 release_firmware(const struct firmware *fw)
460 * register_firmware: - provide a firmware image for later usage
463 * Make sure that @data will be available by requesting firmware @name.
465 * Note: This will not be possible until some kind of persistence
469 register_firmware(const char *name, const u8 *data, size_t size)
471 /* This is meaningless without firmware caching, so until we
472 * decide if firmware caching is reasonable just leave it as a
477 struct firmware_work {
478 struct work_struct work;
479 struct module *module;
481 struct device *device;
483 void (*cont)(const struct firmware *fw, void *context);
487 request_firmware_work_func(void *arg)
489 struct firmware_work *fw_work = arg;
490 const struct firmware *fw;
495 daemonize("%s/%s", "firmware", fw_work->name);
496 request_firmware(&fw, fw_work->name, fw_work->device);
497 fw_work->cont(fw, fw_work->context);
498 release_firmware(fw);
499 module_put(fw_work->module);
505 * request_firmware_nowait:
508 * Asynchronous variant of request_firmware() for contexts where
509 * it is not possible to sleep.
511 * @cont will be called asynchronously when the firmware request is over.
513 * @context will be passed over to @cont.
515 * @fw may be %NULL if firmware request fails.
519 request_firmware_nowait(
520 struct module *module,
521 const char *name, struct device *device, void *context,
522 void (*cont)(const struct firmware *fw, void *context))
524 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
530 if (!try_module_get(module)) {
535 *fw_work = (struct firmware_work) {
543 ret = kernel_thread(request_firmware_work_func, fw_work,
544 CLONE_FS | CLONE_FILES);
547 fw_work->cont(NULL, fw_work->context);
554 firmware_class_init(void)
557 error = class_register(&firmware_class);
559 printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
562 error = class_create_file(&firmware_class, &class_attr_timeout);
564 printk(KERN_ERR "%s: class_create_file failed\n",
566 class_unregister(&firmware_class);
572 firmware_class_exit(void)
574 class_unregister(&firmware_class);
577 module_init(firmware_class_init);
578 module_exit(firmware_class_exit);
580 EXPORT_SYMBOL(release_firmware);
581 EXPORT_SYMBOL(request_firmware);
582 EXPORT_SYMBOL(request_firmware_nowait);
583 EXPORT_SYMBOL(register_firmware);