V4L/DVB (8866): Add dummy FE to the Kconfig-file and fix it
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / firmware_class.c
blobc9c92b00fd555768730b731695bdbac1deeab1e9
1 /*
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.
8 */
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>
22 #include "base.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");
30 enum {
31 FW_STATUS_LOADING,
32 FW_STATUS_DONE,
33 FW_STATUS_ABORT,
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;
46 struct firmware *fw;
47 unsigned long status;
48 int alloc_size;
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;
58 #endif
60 static void
61 fw_load_abort(struct firmware_priv *fw_priv)
63 set_bit(FW_STATUS_ABORT, &fw_priv->status);
64 wmb();
65 complete(&fw_priv->completion);
68 static ssize_t
69 firmware_timeout_show(struct class *class, char *buf)
71 return sprintf(buf, "%d\n", loading_timeout);
74 /**
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'.
85 **/
86 static ssize_t
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)
91 loading_timeout = 0;
92 return count;
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))
104 return -ENOMEM;
105 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
106 return -ENOMEM;
108 return 0;
111 static struct class firmware_class = {
112 .name = "firmware",
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);
145 switch (loading) {
146 case 1:
147 mutex_lock(&fw_lock);
148 if (!fw_priv->fw) {
149 mutex_unlock(&fw_lock);
150 break;
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);
158 break;
159 case 0:
160 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
161 complete(&fw_priv->completion);
162 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
163 break;
165 /* fallthrough */
166 default:
167 printk(KERN_ERR "%s: unexpected value (%d)\n", __func__,
168 loading);
169 /* fallthrough */
170 case -1:
171 fw_load_abort(fw_priv);
172 break;
175 return count;
178 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
180 static ssize_t
181 firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
182 char *buffer, loff_t offset, size_t count)
184 struct device *dev = to_dev(kobj);
185 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
186 struct firmware *fw;
187 ssize_t ret_count;
189 mutex_lock(&fw_lock);
190 fw = fw_priv->fw;
191 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
192 ret_count = -ENODEV;
193 goto out;
195 ret_count = memory_read_from_buffer(buffer, count, &offset,
196 fw->data, fw->size);
197 out:
198 mutex_unlock(&fw_lock);
199 return ret_count;
202 static int
203 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
205 u8 *new_data;
206 int new_size = fw_priv->alloc_size;
208 if (min_size <= fw_priv->alloc_size)
209 return 0;
211 new_size = ALIGN(min_size, PAGE_SIZE);
212 new_data = vmalloc(new_size);
213 if (!new_data) {
214 printk(KERN_ERR "%s: unable to alloc buffer\n", __func__);
215 /* Make sure that we don't keep incomplete data */
216 fw_load_abort(fw_priv);
217 return -ENOMEM;
219 fw_priv->alloc_size = new_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);
226 return 0;
230 * firmware_data_write - write method for firmware
231 * @kobj: kobject for the device
232 * @bin_attr: bin_attr structure
233 * @buffer: buffer being written
234 * @offset: buffer offset for write in total data store area
235 * @count: buffer size
237 * Data written to the 'data' attribute will be later handed to
238 * the driver as a firmware image.
240 static ssize_t
241 firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
242 char *buffer, loff_t offset, size_t count)
244 struct device *dev = to_dev(kobj);
245 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
246 struct firmware *fw;
247 ssize_t retval;
249 if (!capable(CAP_SYS_RAWIO))
250 return -EPERM;
252 mutex_lock(&fw_lock);
253 fw = fw_priv->fw;
254 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
255 retval = -ENODEV;
256 goto out;
258 retval = fw_realloc_buffer(fw_priv, offset + count);
259 if (retval)
260 goto out;
262 memcpy((u8 *)fw->data + offset, buffer, count);
264 fw->size = max_t(size_t, offset + count, fw->size);
265 retval = count;
266 out:
267 mutex_unlock(&fw_lock);
268 return retval;
271 static struct bin_attribute firmware_attr_data_tmpl = {
272 .attr = {.name = "data", .mode = 0644},
273 .size = 0,
274 .read = firmware_data_read,
275 .write = firmware_data_write,
278 static void fw_dev_release(struct device *dev)
280 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
282 kfree(fw_priv);
283 kfree(dev);
285 module_put(THIS_MODULE);
288 static void
289 firmware_class_timeout(u_long data)
291 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
292 fw_load_abort(fw_priv);
295 static inline void fw_setup_device_id(struct device *f_dev, struct device *dev)
297 /* XXX warning we should watch out for name collisions */
298 strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE);
301 static int fw_register_device(struct device **dev_p, const char *fw_name,
302 struct device *device)
304 int retval;
305 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
306 GFP_KERNEL);
307 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
309 *dev_p = NULL;
311 if (!fw_priv || !f_dev) {
312 printk(KERN_ERR "%s: kmalloc failed\n", __func__);
313 retval = -ENOMEM;
314 goto error_kfree;
317 init_completion(&fw_priv->completion);
318 fw_priv->attr_data = firmware_attr_data_tmpl;
319 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
321 fw_priv->timeout.function = firmware_class_timeout;
322 fw_priv->timeout.data = (u_long) fw_priv;
323 init_timer(&fw_priv->timeout);
325 fw_setup_device_id(f_dev, device);
326 f_dev->parent = device;
327 f_dev->class = &firmware_class;
328 dev_set_drvdata(f_dev, fw_priv);
329 f_dev->uevent_suppress = 1;
330 retval = device_register(f_dev);
331 if (retval) {
332 printk(KERN_ERR "%s: device_register failed\n",
333 __func__);
334 goto error_kfree;
336 *dev_p = f_dev;
337 return 0;
339 error_kfree:
340 kfree(fw_priv);
341 kfree(f_dev);
342 return retval;
345 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
346 const char *fw_name, struct device *device,
347 int uevent)
349 struct device *f_dev;
350 struct firmware_priv *fw_priv;
351 int retval;
353 *dev_p = NULL;
354 retval = fw_register_device(&f_dev, fw_name, device);
355 if (retval)
356 goto out;
358 /* Need to pin this module until class device is destroyed */
359 __module_get(THIS_MODULE);
361 fw_priv = dev_get_drvdata(f_dev);
363 fw_priv->fw = fw;
364 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
365 if (retval) {
366 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
367 __func__);
368 goto error_unreg;
371 retval = device_create_file(f_dev, &dev_attr_loading);
372 if (retval) {
373 printk(KERN_ERR "%s: device_create_file failed\n",
374 __func__);
375 goto error_unreg;
378 if (uevent)
379 f_dev->uevent_suppress = 0;
380 *dev_p = f_dev;
381 goto out;
383 error_unreg:
384 device_unregister(f_dev);
385 out:
386 return retval;
389 static int
390 _request_firmware(const struct firmware **firmware_p, const char *name,
391 struct device *device, int uevent)
393 struct device *f_dev;
394 struct firmware_priv *fw_priv;
395 struct firmware *firmware;
396 struct builtin_fw *builtin;
397 int retval;
399 if (!firmware_p)
400 return -EINVAL;
402 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
403 if (!firmware) {
404 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
405 __func__);
406 retval = -ENOMEM;
407 goto out;
410 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
411 builtin++) {
412 if (strcmp(name, builtin->name))
413 continue;
414 printk(KERN_INFO "firmware: using built-in firmware %s\n",
415 name);
416 firmware->size = builtin->size;
417 firmware->data = builtin->data;
418 return 0;
421 if (uevent)
422 printk(KERN_INFO "firmware: requesting %s\n", name);
424 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
425 if (retval)
426 goto error_kfree_fw;
428 fw_priv = dev_get_drvdata(f_dev);
430 if (uevent) {
431 if (loading_timeout > 0) {
432 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
433 add_timer(&fw_priv->timeout);
436 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
437 wait_for_completion(&fw_priv->completion);
438 set_bit(FW_STATUS_DONE, &fw_priv->status);
439 del_timer_sync(&fw_priv->timeout);
440 } else
441 wait_for_completion(&fw_priv->completion);
443 mutex_lock(&fw_lock);
444 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
445 retval = -ENOENT;
446 release_firmware(fw_priv->fw);
447 *firmware_p = NULL;
449 fw_priv->fw = NULL;
450 mutex_unlock(&fw_lock);
451 device_unregister(f_dev);
452 goto out;
454 error_kfree_fw:
455 kfree(firmware);
456 *firmware_p = NULL;
457 out:
458 return retval;
462 * request_firmware: - send firmware request and wait for it
463 * @firmware_p: pointer to firmware image
464 * @name: name of firmware file
465 * @device: device for which firmware is being loaded
467 * @firmware_p will be used to return a firmware image by the name
468 * of @name for device @device.
470 * Should be called from user context where sleeping is allowed.
472 * @name will be used as $FIRMWARE in the uevent environment and
473 * should be distinctive enough not to be confused with any other
474 * firmware image for this or any other device.
477 request_firmware(const struct firmware **firmware_p, const char *name,
478 struct device *device)
480 int uevent = 1;
481 return _request_firmware(firmware_p, name, device, uevent);
485 * release_firmware: - release the resource associated with a firmware image
486 * @fw: firmware resource to release
488 void
489 release_firmware(const struct firmware *fw)
491 struct builtin_fw *builtin;
493 if (fw) {
494 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
495 builtin++) {
496 if (fw->data == builtin->data)
497 goto free_fw;
499 vfree(fw->data);
500 free_fw:
501 kfree(fw);
505 /* Async support */
506 struct firmware_work {
507 struct work_struct work;
508 struct module *module;
509 const char *name;
510 struct device *device;
511 void *context;
512 void (*cont)(const struct firmware *fw, void *context);
513 int uevent;
516 static int
517 request_firmware_work_func(void *arg)
519 struct firmware_work *fw_work = arg;
520 const struct firmware *fw;
521 int ret;
522 if (!arg) {
523 WARN_ON(1);
524 return 0;
526 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
527 fw_work->uevent);
528 if (ret < 0)
529 fw_work->cont(NULL, fw_work->context);
530 else {
531 fw_work->cont(fw, fw_work->context);
532 release_firmware(fw);
534 module_put(fw_work->module);
535 kfree(fw_work);
536 return ret;
540 * request_firmware_nowait: asynchronous version of request_firmware
541 * @module: module requesting the firmware
542 * @uevent: sends uevent to copy the firmware image if this flag
543 * is non-zero else the firmware copy must be done manually.
544 * @name: name of firmware file
545 * @device: device for which firmware is being loaded
546 * @context: will be passed over to @cont, and
547 * @fw may be %NULL if firmware request fails.
548 * @cont: function will be called asynchronously when the firmware
549 * request is over.
551 * Asynchronous variant of request_firmware() for contexts where
552 * it is not possible to sleep.
555 request_firmware_nowait(
556 struct module *module, int uevent,
557 const char *name, struct device *device, void *context,
558 void (*cont)(const struct firmware *fw, void *context))
560 struct task_struct *task;
561 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
562 GFP_ATOMIC);
564 if (!fw_work)
565 return -ENOMEM;
566 if (!try_module_get(module)) {
567 kfree(fw_work);
568 return -EFAULT;
571 *fw_work = (struct firmware_work) {
572 .module = module,
573 .name = name,
574 .device = device,
575 .context = context,
576 .cont = cont,
577 .uevent = uevent,
580 task = kthread_run(request_firmware_work_func, fw_work,
581 "firmware/%s", name);
583 if (IS_ERR(task)) {
584 fw_work->cont(NULL, fw_work->context);
585 module_put(fw_work->module);
586 kfree(fw_work);
587 return PTR_ERR(task);
589 return 0;
592 static int __init
593 firmware_class_init(void)
595 int error;
596 error = class_register(&firmware_class);
597 if (error) {
598 printk(KERN_ERR "%s: class_register failed\n", __func__);
599 return error;
601 error = class_create_file(&firmware_class, &class_attr_timeout);
602 if (error) {
603 printk(KERN_ERR "%s: class_create_file failed\n",
604 __func__);
605 class_unregister(&firmware_class);
607 return error;
610 static void __exit
611 firmware_class_exit(void)
613 class_unregister(&firmware_class);
616 fs_initcall(firmware_class_init);
617 module_exit(firmware_class_exit);
619 EXPORT_SYMBOL(release_firmware);
620 EXPORT_SYMBOL(request_firmware);
621 EXPORT_SYMBOL(request_firmware_nowait);