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>
20 #include <linux/firmware.h>
23 MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
24 MODULE_DESCRIPTION("Multi purpose firmware loading support");
25 MODULE_LICENSE("GPL");
32 FW_STATUS_READY_NOHOTPLUG
,
35 static int loading_timeout
= 10; /* In seconds */
37 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
38 * guarding for corner cases a global lock should be OK */
39 static DEFINE_MUTEX(fw_lock
);
41 struct firmware_priv
{
42 char fw_id
[FIRMWARE_NAME_MAX
];
43 struct completion completion
;
44 struct bin_attribute attr_data
;
48 struct timer_list timeout
;
52 fw_load_abort(struct firmware_priv
*fw_priv
)
54 set_bit(FW_STATUS_ABORT
, &fw_priv
->status
);
56 complete(&fw_priv
->completion
);
60 firmware_timeout_show(struct class *class, char *buf
)
62 return sprintf(buf
, "%d\n", loading_timeout
);
66 * firmware_timeout_store - set number of seconds to wait for firmware
67 * @class: device class pointer
68 * @buf: buffer to scan for timeout value
69 * @count: number of bytes in @buf
71 * Sets the number of seconds to wait for the firmware. Once
72 * this expires an error will be returned to the driver and no
73 * firmware will be provided.
75 * Note: zero means 'wait forever'.
78 firmware_timeout_store(struct class *class, const char *buf
, size_t count
)
80 loading_timeout
= simple_strtol(buf
, NULL
, 10);
81 if (loading_timeout
< 0)
86 static CLASS_ATTR(timeout
, 0644, firmware_timeout_show
, firmware_timeout_store
);
88 static void fw_class_dev_release(struct class_device
*class_dev
);
90 static int firmware_class_uevent(struct class_device
*class_dev
, char **envp
,
91 int num_envp
, char *buffer
, int buffer_size
)
93 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
96 if (!test_bit(FW_STATUS_READY
, &fw_priv
->status
))
99 if (add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &len
,
100 "FIRMWARE=%s", fw_priv
->fw_id
))
102 if (add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &len
,
103 "TIMEOUT=%i", loading_timeout
))
110 static struct class firmware_class
= {
112 .uevent
= firmware_class_uevent
,
113 .release
= fw_class_dev_release
,
117 firmware_loading_show(struct class_device
*class_dev
, char *buf
)
119 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
120 int loading
= test_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
121 return sprintf(buf
, "%d\n", loading
);
125 * firmware_loading_store - set value in the 'loading' control file
126 * @class_dev: class_device pointer
127 * @buf: buffer to scan for loading control value
128 * @count: number of bytes in @buf
130 * The relevant values are:
132 * 1: Start a load, discarding any previous partial load.
133 * 0: Conclude the load and hand the data to the driver code.
134 * -1: Conclude the load with an error and discard any written data.
137 firmware_loading_store(struct class_device
*class_dev
,
138 const char *buf
, size_t count
)
140 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
141 int loading
= simple_strtol(buf
, NULL
, 10);
145 mutex_lock(&fw_lock
);
147 mutex_unlock(&fw_lock
);
150 vfree(fw_priv
->fw
->data
);
151 fw_priv
->fw
->data
= NULL
;
152 fw_priv
->fw
->size
= 0;
153 fw_priv
->alloc_size
= 0;
154 set_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
155 mutex_unlock(&fw_lock
);
158 if (test_bit(FW_STATUS_LOADING
, &fw_priv
->status
)) {
159 complete(&fw_priv
->completion
);
160 clear_bit(FW_STATUS_LOADING
, &fw_priv
->status
);
165 printk(KERN_ERR
"%s: unexpected value (%d)\n", __FUNCTION__
,
169 fw_load_abort(fw_priv
);
176 static CLASS_DEVICE_ATTR(loading
, 0644,
177 firmware_loading_show
, firmware_loading_store
);
180 firmware_data_read(struct kobject
*kobj
,
181 char *buffer
, loff_t offset
, size_t count
)
183 struct class_device
*class_dev
= to_class_dev(kobj
);
184 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
186 ssize_t ret_count
= count
;
188 mutex_lock(&fw_lock
);
190 if (!fw
|| test_bit(FW_STATUS_DONE
, &fw_priv
->status
)) {
194 if (offset
> fw
->size
) {
198 if (offset
+ ret_count
> fw
->size
)
199 ret_count
= fw
->size
- offset
;
201 memcpy(buffer
, fw
->data
+ offset
, ret_count
);
203 mutex_unlock(&fw_lock
);
208 fw_realloc_buffer(struct firmware_priv
*fw_priv
, int min_size
)
211 int new_size
= fw_priv
->alloc_size
;
213 if (min_size
<= fw_priv
->alloc_size
)
216 new_size
= ALIGN(min_size
, PAGE_SIZE
);
217 new_data
= vmalloc(new_size
);
219 printk(KERN_ERR
"%s: unable to alloc buffer\n", __FUNCTION__
);
220 /* Make sure that we don't keep incomplete data */
221 fw_load_abort(fw_priv
);
224 fw_priv
->alloc_size
= new_size
;
225 if (fw_priv
->fw
->data
) {
226 memcpy(new_data
, fw_priv
->fw
->data
, fw_priv
->fw
->size
);
227 vfree(fw_priv
->fw
->data
);
229 fw_priv
->fw
->data
= new_data
;
230 BUG_ON(min_size
> fw_priv
->alloc_size
);
235 * firmware_data_write - write method for firmware
236 * @kobj: kobject for the class_device
237 * @buffer: buffer being written
238 * @offset: buffer offset for write in total data store area
239 * @count: buffer size
241 * Data written to the 'data' attribute will be later handed to
242 * the driver as a firmware image.
245 firmware_data_write(struct kobject
*kobj
,
246 char *buffer
, loff_t offset
, size_t count
)
248 struct class_device
*class_dev
= to_class_dev(kobj
);
249 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
253 if (!capable(CAP_SYS_RAWIO
))
256 mutex_lock(&fw_lock
);
258 if (!fw
|| test_bit(FW_STATUS_DONE
, &fw_priv
->status
)) {
262 retval
= fw_realloc_buffer(fw_priv
, offset
+ count
);
266 memcpy(fw
->data
+ offset
, buffer
, count
);
268 fw
->size
= max_t(size_t, offset
+ count
, fw
->size
);
271 mutex_unlock(&fw_lock
);
275 static struct bin_attribute firmware_attr_data_tmpl
= {
276 .attr
= {.name
= "data", .mode
= 0644, .owner
= THIS_MODULE
},
278 .read
= firmware_data_read
,
279 .write
= firmware_data_write
,
283 fw_class_dev_release(struct class_device
*class_dev
)
285 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
290 module_put(THIS_MODULE
);
294 firmware_class_timeout(u_long data
)
296 struct firmware_priv
*fw_priv
= (struct firmware_priv
*) data
;
297 fw_load_abort(fw_priv
);
301 fw_setup_class_device_id(struct class_device
*class_dev
, struct device
*dev
)
303 /* XXX warning we should watch out for name collisions */
304 strlcpy(class_dev
->class_id
, dev
->bus_id
, BUS_ID_SIZE
);
308 fw_register_class_device(struct class_device
**class_dev_p
,
309 const char *fw_name
, struct device
*device
)
312 struct firmware_priv
*fw_priv
= kzalloc(sizeof(*fw_priv
),
314 struct class_device
*class_dev
= kzalloc(sizeof(*class_dev
),
319 if (!fw_priv
|| !class_dev
) {
320 printk(KERN_ERR
"%s: kmalloc failed\n", __FUNCTION__
);
325 init_completion(&fw_priv
->completion
);
326 fw_priv
->attr_data
= firmware_attr_data_tmpl
;
327 strlcpy(fw_priv
->fw_id
, fw_name
, FIRMWARE_NAME_MAX
);
329 fw_priv
->timeout
.function
= firmware_class_timeout
;
330 fw_priv
->timeout
.data
= (u_long
) fw_priv
;
331 init_timer(&fw_priv
->timeout
);
333 fw_setup_class_device_id(class_dev
, device
);
334 class_dev
->dev
= device
;
335 class_dev
->class = &firmware_class
;
336 class_set_devdata(class_dev
, fw_priv
);
337 retval
= class_device_register(class_dev
);
339 printk(KERN_ERR
"%s: class_device_register failed\n",
343 *class_dev_p
= class_dev
;
353 fw_setup_class_device(struct firmware
*fw
, struct class_device
**class_dev_p
,
354 const char *fw_name
, struct device
*device
, int uevent
)
356 struct class_device
*class_dev
;
357 struct firmware_priv
*fw_priv
;
361 retval
= fw_register_class_device(&class_dev
, fw_name
, device
);
365 /* Need to pin this module until class device is destroyed */
366 __module_get(THIS_MODULE
);
368 fw_priv
= class_get_devdata(class_dev
);
371 retval
= sysfs_create_bin_file(&class_dev
->kobj
, &fw_priv
->attr_data
);
373 printk(KERN_ERR
"%s: sysfs_create_bin_file failed\n",
378 retval
= class_device_create_file(class_dev
,
379 &class_device_attr_loading
);
381 printk(KERN_ERR
"%s: class_device_create_file failed\n",
387 set_bit(FW_STATUS_READY
, &fw_priv
->status
);
389 set_bit(FW_STATUS_READY_NOHOTPLUG
, &fw_priv
->status
);
390 *class_dev_p
= class_dev
;
394 class_device_unregister(class_dev
);
400 _request_firmware(const struct firmware
**firmware_p
, const char *name
,
401 struct device
*device
, int uevent
)
403 struct class_device
*class_dev
;
404 struct firmware_priv
*fw_priv
;
405 struct firmware
*firmware
;
411 *firmware_p
= firmware
= kzalloc(sizeof(*firmware
), GFP_KERNEL
);
413 printk(KERN_ERR
"%s: kmalloc(struct firmware) failed\n",
419 retval
= fw_setup_class_device(firmware
, &class_dev
, name
, device
,
424 fw_priv
= class_get_devdata(class_dev
);
427 if (loading_timeout
> 0) {
428 fw_priv
->timeout
.expires
= jiffies
+ loading_timeout
* HZ
;
429 add_timer(&fw_priv
->timeout
);
432 kobject_uevent(&class_dev
->kobj
, KOBJ_ADD
);
433 wait_for_completion(&fw_priv
->completion
);
434 set_bit(FW_STATUS_DONE
, &fw_priv
->status
);
435 del_timer_sync(&fw_priv
->timeout
);
437 wait_for_completion(&fw_priv
->completion
);
439 mutex_lock(&fw_lock
);
440 if (!fw_priv
->fw
->size
|| test_bit(FW_STATUS_ABORT
, &fw_priv
->status
)) {
442 release_firmware(fw_priv
->fw
);
446 mutex_unlock(&fw_lock
);
447 class_device_unregister(class_dev
);
458 * request_firmware: - send firmware request and wait for it
459 * @firmware_p: pointer to firmware image
460 * @name: name of firmware file
461 * @device: device for which firmware is being loaded
463 * @firmware_p will be used to return a firmware image by the name
464 * of @name for device @device.
466 * Should be called from user context where sleeping is allowed.
468 * @name will be used as $FIRMWARE in the uevent environment and
469 * should be distinctive enough not to be confused with any other
470 * firmware image for this or any other device.
473 request_firmware(const struct firmware
**firmware_p
, const char *name
,
474 struct device
*device
)
477 return _request_firmware(firmware_p
, name
, device
, uevent
);
481 * release_firmware: - release the resource associated with a firmware image
482 * @fw: firmware resource to release
485 release_firmware(const struct firmware
*fw
)
494 struct firmware_work
{
495 struct work_struct work
;
496 struct module
*module
;
498 struct device
*device
;
500 void (*cont
)(const struct firmware
*fw
, void *context
);
505 request_firmware_work_func(void *arg
)
507 struct firmware_work
*fw_work
= arg
;
508 const struct firmware
*fw
;
514 daemonize("%s/%s", "firmware", fw_work
->name
);
515 ret
= _request_firmware(&fw
, fw_work
->name
, fw_work
->device
,
518 fw_work
->cont(NULL
, fw_work
->context
);
520 fw_work
->cont(fw
, fw_work
->context
);
521 release_firmware(fw
);
523 module_put(fw_work
->module
);
529 * request_firmware_nowait: asynchronous version of request_firmware
530 * @module: module requesting the firmware
531 * @uevent: sends uevent to copy the firmware image if this flag
532 * is non-zero else the firmware copy must be done manually.
533 * @name: name of firmware file
534 * @device: device for which firmware is being loaded
535 * @context: will be passed over to @cont, and
536 * @fw may be %NULL if firmware request fails.
537 * @cont: function will be called asynchronously when the firmware
540 * Asynchronous variant of request_firmware() for contexts where
541 * it is not possible to sleep.
544 request_firmware_nowait(
545 struct module
*module
, int uevent
,
546 const char *name
, struct device
*device
, void *context
,
547 void (*cont
)(const struct firmware
*fw
, void *context
))
549 struct firmware_work
*fw_work
= kmalloc(sizeof (struct firmware_work
),
555 if (!try_module_get(module
)) {
560 *fw_work
= (struct firmware_work
) {
569 ret
= kernel_thread(request_firmware_work_func
, fw_work
,
570 CLONE_FS
| CLONE_FILES
);
573 fw_work
->cont(NULL
, fw_work
->context
);
574 module_put(fw_work
->module
);
582 firmware_class_init(void)
585 error
= class_register(&firmware_class
);
587 printk(KERN_ERR
"%s: class_register failed\n", __FUNCTION__
);
590 error
= class_create_file(&firmware_class
, &class_attr_timeout
);
592 printk(KERN_ERR
"%s: class_create_file failed\n",
594 class_unregister(&firmware_class
);
600 firmware_class_exit(void)
602 class_unregister(&firmware_class
);
605 module_init(firmware_class_init
);
606 module_exit(firmware_class_exit
);
608 EXPORT_SYMBOL(release_firmware
);
609 EXPORT_SYMBOL(request_firmware
);
610 EXPORT_SYMBOL(request_firmware_nowait
);