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");
31 FW_STATUS_READY_NOHOTPLUG
,
34 static int loading_timeout
= 10; /* In seconds */
36 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
37 * guarding for corner cases a global lock should be OK */
38 static DECLARE_MUTEX(fw_lock
);
40 struct firmware_priv
{
41 char fw_id
[FIRMWARE_NAME_MAX
];
42 struct completion completion
;
43 struct bin_attribute attr_data
;
47 struct timer_list timeout
;
51 fw_load_abort(struct firmware_priv
*fw_priv
)
53 set_bit(FW_STATUS_ABORT
, &fw_priv
->status
);
55 complete(&fw_priv
->completion
);
59 firmware_timeout_show(struct class *class, char *buf
)
61 return sprintf(buf
, "%d\n", loading_timeout
);
65 * firmware_timeout_store:
67 * Sets the number of seconds to wait for the firmware. Once
68 * this expires an error will be return to the driver and no
69 * firmware will be provided.
71 * Note: zero means 'wait for ever'
75 firmware_timeout_store(struct class *class, const char *buf
, size_t count
)
77 loading_timeout
= simple_strtol(buf
, NULL
, 10);
78 if (loading_timeout
< 0)
83 static CLASS_ATTR(timeout
, 0644, firmware_timeout_show
, firmware_timeout_store
);
85 static void fw_class_dev_release(struct class_device
*class_dev
);
86 int firmware_class_hotplug(struct class_device
*dev
, char **envp
,
87 int num_envp
, char *buffer
, int buffer_size
);
89 static struct class firmware_class
= {
91 .hotplug
= firmware_class_hotplug
,
92 .release
= fw_class_dev_release
,
96 firmware_class_hotplug(struct class_device
*class_dev
, char **envp
,
97 int num_envp
, char *buffer
, int buffer_size
)
99 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
102 if (!test_bit(FW_STATUS_READY
, &fw_priv
->status
))
105 if (add_hotplug_env_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &len
,
106 "FIRMWARE=%s", fw_priv
->fw_id
))
108 if (add_hotplug_env_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &len
,
109 "TIMEOUT=%i", loading_timeout
))
118 firmware_loading_show(struct class_device
*class_dev
, char *buf
)
120 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
121 int loading
= test_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
122 return sprintf(buf
, "%d\n", loading
);
126 * firmware_loading_store: - loading control file
128 * The relevant values are:
130 * 1: Start a load, discarding any previous partial load.
131 * 0: Conclude the load and handle the data to the driver code.
132 * -1: Conclude the load with an error and discard any written data.
135 firmware_loading_store(struct class_device
*class_dev
,
136 const char *buf
, size_t count
)
138 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
139 int loading
= simple_strtol(buf
, NULL
, 10);
148 vfree(fw_priv
->fw
->data
);
149 fw_priv
->fw
->data
= NULL
;
150 fw_priv
->fw
->size
= 0;
151 fw_priv
->alloc_size
= 0;
152 set_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
156 if (test_bit(FW_STATUS_LOADING
, &fw_priv
->status
)) {
157 complete(&fw_priv
->completion
);
158 clear_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
163 printk(KERN_ERR
"%s: unexpected value (%d)\n", __FUNCTION__
,
167 fw_load_abort(fw_priv
);
174 static CLASS_DEVICE_ATTR(loading
, 0644,
175 firmware_loading_show
, firmware_loading_store
);
178 firmware_data_read(struct kobject
*kobj
,
179 char *buffer
, loff_t offset
, size_t count
)
181 struct class_device
*class_dev
= to_class_dev(kobj
);
182 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
184 ssize_t ret_count
= count
;
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
);
205 fw_realloc_buffer(struct firmware_priv
*fw_priv
, int min_size
)
209 if (min_size
<= fw_priv
->alloc_size
)
212 new_data
= vmalloc(fw_priv
->alloc_size
+ PAGE_SIZE
);
214 printk(KERN_ERR
"%s: unable to alloc buffer\n", __FUNCTION__
);
215 /* Make sure that we don't keep incomplete data */
216 fw_load_abort(fw_priv
);
219 fw_priv
->alloc_size
+= PAGE_SIZE
;
220 if (fw_priv
->fw
->data
) {
221 memcpy(new_data
, fw_priv
->fw
->data
, fw_priv
->fw
->size
);
222 vfree(fw_priv
->fw
->data
);
224 fw_priv
->fw
->data
= new_data
;
225 BUG_ON(min_size
> fw_priv
->alloc_size
);
230 * firmware_data_write:
234 * Data written to the 'data' attribute will be later handled to
235 * the driver as a firmware image.
238 firmware_data_write(struct kobject
*kobj
,
239 char *buffer
, loff_t offset
, size_t count
)
241 struct class_device
*class_dev
= to_class_dev(kobj
);
242 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
246 if (!capable(CAP_SYS_RAWIO
))
251 if (!fw
|| test_bit(FW_STATUS_DONE
, &fw_priv
->status
)) {
255 retval
= fw_realloc_buffer(fw_priv
, offset
+ count
);
259 memcpy(fw
->data
+ offset
, buffer
, count
);
261 fw
->size
= max_t(size_t, offset
+ count
, fw
->size
);
267 static struct bin_attribute firmware_attr_data_tmpl
= {
268 .attr
= {.name
= "data", .mode
= 0644, .owner
= THIS_MODULE
},
270 .read
= firmware_data_read
,
271 .write
= firmware_data_write
,
275 fw_class_dev_release(struct class_device
*class_dev
)
277 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
282 module_put(THIS_MODULE
);
286 firmware_class_timeout(u_long data
)
288 struct firmware_priv
*fw_priv
= (struct firmware_priv
*) data
;
289 fw_load_abort(fw_priv
);
293 fw_setup_class_device_id(struct class_device
*class_dev
, struct device
*dev
)
295 /* XXX warning we should watch out for name collisions */
296 strlcpy(class_dev
->class_id
, dev
->bus_id
, BUS_ID_SIZE
);
300 fw_register_class_device(struct class_device
**class_dev_p
,
301 const char *fw_name
, struct device
*device
)
304 struct firmware_priv
*fw_priv
= kmalloc(sizeof (struct firmware_priv
),
306 struct class_device
*class_dev
= kmalloc(sizeof (struct class_device
),
311 if (!fw_priv
|| !class_dev
) {
312 printk(KERN_ERR
"%s: kmalloc failed\n", __FUNCTION__
);
316 memset(fw_priv
, 0, sizeof (*fw_priv
));
317 memset(class_dev
, 0, sizeof (*class_dev
));
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_class_device_id(class_dev
, device
);
328 class_dev
->dev
= device
;
329 class_dev
->class = &firmware_class
;
330 class_set_devdata(class_dev
, fw_priv
);
331 retval
= class_device_register(class_dev
);
333 printk(KERN_ERR
"%s: class_device_register failed\n",
337 *class_dev_p
= class_dev
;
347 fw_setup_class_device(struct firmware
*fw
, struct class_device
**class_dev_p
,
348 const char *fw_name
, struct device
*device
, int hotplug
)
350 struct class_device
*class_dev
;
351 struct firmware_priv
*fw_priv
;
355 retval
= fw_register_class_device(&class_dev
, fw_name
, device
);
359 /* Need to pin this module until class device is destroyed */
360 __module_get(THIS_MODULE
);
362 fw_priv
= class_get_devdata(class_dev
);
365 retval
= sysfs_create_bin_file(&class_dev
->kobj
, &fw_priv
->attr_data
);
367 printk(KERN_ERR
"%s: sysfs_create_bin_file failed\n",
372 retval
= class_device_create_file(class_dev
,
373 &class_device_attr_loading
);
375 printk(KERN_ERR
"%s: class_device_create_file failed\n",
381 set_bit(FW_STATUS_READY
, &fw_priv
->status
);
383 set_bit(FW_STATUS_READY_NOHOTPLUG
, &fw_priv
->status
);
384 *class_dev_p
= class_dev
;
388 class_device_unregister(class_dev
);
394 _request_firmware(const struct firmware
**firmware_p
, const char *name
,
395 struct device
*device
, int hotplug
)
397 struct class_device
*class_dev
;
398 struct firmware_priv
*fw_priv
;
399 struct firmware
*firmware
;
405 *firmware_p
= firmware
= kmalloc(sizeof (struct firmware
), GFP_KERNEL
);
407 printk(KERN_ERR
"%s: kmalloc(struct firmware) failed\n",
412 memset(firmware
, 0, sizeof (*firmware
));
414 retval
= fw_setup_class_device(firmware
, &class_dev
, name
, device
,
419 fw_priv
= class_get_devdata(class_dev
);
422 if (loading_timeout
> 0) {
423 fw_priv
->timeout
.expires
= jiffies
+ loading_timeout
* HZ
;
424 add_timer(&fw_priv
->timeout
);
427 kobject_hotplug(&class_dev
->kobj
, KOBJ_ADD
);
428 wait_for_completion(&fw_priv
->completion
);
429 set_bit(FW_STATUS_DONE
, &fw_priv
->status
);
430 del_timer_sync(&fw_priv
->timeout
);
432 wait_for_completion(&fw_priv
->completion
);
435 if (!fw_priv
->fw
->size
|| test_bit(FW_STATUS_ABORT
, &fw_priv
->status
)) {
437 release_firmware(fw_priv
->fw
);
442 class_device_unregister(class_dev
);
453 * request_firmware: - request firmware to hotplug and wait for it
455 * @firmware will be used to return a firmware image by the name
456 * of @name for device @device.
458 * Should be called from user context where sleeping is allowed.
460 * @name will be use as $FIRMWARE in the hotplug environment and
461 * should be distinctive enough not to be confused with any other
462 * firmware image for this or any other device.
465 request_firmware(const struct firmware
**firmware_p
, const char *name
,
466 struct device
*device
)
469 return _request_firmware(firmware_p
, name
, device
, hotplug
);
473 * release_firmware: - release the resource associated with a firmware image
476 release_firmware(const struct firmware
*fw
)
485 * register_firmware: - provide a firmware image for later usage
488 * Make sure that @data will be available by requesting firmware @name.
490 * Note: This will not be possible until some kind of persistence
494 register_firmware(const char *name
, const u8
*data
, size_t size
)
496 /* This is meaningless without firmware caching, so until we
497 * decide if firmware caching is reasonable just leave it as a
502 struct firmware_work
{
503 struct work_struct work
;
504 struct module
*module
;
506 struct device
*device
;
508 void (*cont
)(const struct firmware
*fw
, void *context
);
513 request_firmware_work_func(void *arg
)
515 struct firmware_work
*fw_work
= arg
;
516 const struct firmware
*fw
;
521 daemonize("%s/%s", "firmware", fw_work
->name
);
522 _request_firmware(&fw
, fw_work
->name
, fw_work
->device
,
524 fw_work
->cont(fw
, fw_work
->context
);
525 release_firmware(fw
);
526 module_put(fw_work
->module
);
532 * request_firmware_nowait:
535 * Asynchronous variant of request_firmware() for contexts where
536 * it is not possible to sleep.
538 * @hotplug invokes hotplug event to copy the firmware image if this flag
539 * is non-zero else the firmware copy must be done manually.
541 * @cont will be called asynchronously when the firmware request is over.
543 * @context will be passed over to @cont.
545 * @fw may be %NULL if firmware request fails.
549 request_firmware_nowait(
550 struct module
*module
, int hotplug
,
551 const char *name
, struct device
*device
, void *context
,
552 void (*cont
)(const struct firmware
*fw
, void *context
))
554 struct firmware_work
*fw_work
= kmalloc(sizeof (struct firmware_work
),
560 if (!try_module_get(module
)) {
565 *fw_work
= (struct firmware_work
) {
574 ret
= kernel_thread(request_firmware_work_func
, fw_work
,
575 CLONE_FS
| CLONE_FILES
);
578 fw_work
->cont(NULL
, fw_work
->context
);
585 firmware_class_init(void)
588 error
= class_register(&firmware_class
);
590 printk(KERN_ERR
"%s: class_register failed\n", __FUNCTION__
);
593 error
= class_create_file(&firmware_class
, &class_attr_timeout
);
595 printk(KERN_ERR
"%s: class_create_file failed\n",
597 class_unregister(&firmware_class
);
603 firmware_class_exit(void)
605 class_unregister(&firmware_class
);
608 module_init(firmware_class_init
);
609 module_exit(firmware_class_exit
);
611 EXPORT_SYMBOL(release_firmware
);
612 EXPORT_SYMBOL(request_firmware
);
613 EXPORT_SYMBOL(request_firmware_nowait
);
614 EXPORT_SYMBOL(register_firmware
);