[PATCH] kthread: drivers/base/firmware_class.c
[linux-2.6/libata-dev.git] / drivers / base / firmware_class.c
blob14615694ae9aa3e707435f2f04a1c6dde4586c5c
1 /*
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.
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 MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
25 MODULE_DESCRIPTION("Multi purpose firmware loading support");
26 MODULE_LICENSE("GPL");
28 enum {
29 FW_STATUS_LOADING,
30 FW_STATUS_DONE,
31 FW_STATUS_ABORT,
32 FW_STATUS_READY,
33 FW_STATUS_READY_NOHOTPLUG,
36 static int loading_timeout = 10; /* 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 static void
53 fw_load_abort(struct firmware_priv *fw_priv)
55 set_bit(FW_STATUS_ABORT, &fw_priv->status);
56 wmb();
57 complete(&fw_priv->completion);
60 static ssize_t
61 firmware_timeout_show(struct class *class, char *buf)
63 return sprintf(buf, "%d\n", loading_timeout);
66 /**
67 * firmware_timeout_store - set number of seconds to wait for firmware
68 * @class: device class pointer
69 * @buf: buffer to scan for timeout value
70 * @count: number of bytes in @buf
72 * Sets the number of seconds to wait for the firmware. Once
73 * this expires an error will be returned to the driver and no
74 * firmware will be provided.
76 * Note: zero means 'wait forever'.
77 **/
78 static ssize_t
79 firmware_timeout_store(struct class *class, const char *buf, size_t count)
81 loading_timeout = simple_strtol(buf, NULL, 10);
82 if (loading_timeout < 0)
83 loading_timeout = 0;
84 return count;
87 static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
89 static void fw_class_dev_release(struct class_device *class_dev);
91 static int firmware_class_uevent(struct class_device *class_dev, char **envp,
92 int num_envp, char *buffer, int buffer_size)
94 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
95 int i = 0, len = 0;
97 if (!test_bit(FW_STATUS_READY, &fw_priv->status))
98 return -ENODEV;
100 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
101 "FIRMWARE=%s", fw_priv->fw_id))
102 return -ENOMEM;
103 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
104 "TIMEOUT=%i", loading_timeout))
105 return -ENOMEM;
106 envp[i] = NULL;
108 return 0;
111 static struct class firmware_class = {
112 .name = "firmware",
113 .uevent = firmware_class_uevent,
114 .release = fw_class_dev_release,
117 static ssize_t
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 - set value in the 'loading' control file
127 * @class_dev: class_device pointer
128 * @buf: buffer to scan for loading control value
129 * @count: number of bytes in @buf
131 * The relevant values are:
133 * 1: Start a load, discarding any previous partial load.
134 * 0: Conclude the load and hand the data to the driver code.
135 * -1: Conclude the load with an error and discard any written data.
137 static ssize_t
138 firmware_loading_store(struct class_device *class_dev,
139 const char *buf, size_t count)
141 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
142 int loading = simple_strtol(buf, NULL, 10);
144 switch (loading) {
145 case 1:
146 mutex_lock(&fw_lock);
147 if (!fw_priv->fw) {
148 mutex_unlock(&fw_lock);
149 break;
151 vfree(fw_priv->fw->data);
152 fw_priv->fw->data = NULL;
153 fw_priv->fw->size = 0;
154 fw_priv->alloc_size = 0;
155 set_bit(FW_STATUS_LOADING, &fw_priv->status);
156 mutex_unlock(&fw_lock);
157 break;
158 case 0:
159 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
160 complete(&fw_priv->completion);
161 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
162 break;
164 /* fallthrough */
165 default:
166 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
167 loading);
168 /* fallthrough */
169 case -1:
170 fw_load_abort(fw_priv);
171 break;
174 return count;
177 static CLASS_DEVICE_ATTR(loading, 0644,
178 firmware_loading_show, firmware_loading_store);
180 static ssize_t
181 firmware_data_read(struct kobject *kobj,
182 char *buffer, loff_t offset, size_t count)
184 struct class_device *class_dev = to_class_dev(kobj);
185 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
186 struct firmware *fw;
187 ssize_t ret_count = 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 if (offset > fw->size) {
196 ret_count = 0;
197 goto out;
199 if (offset + ret_count > fw->size)
200 ret_count = fw->size - offset;
202 memcpy(buffer, fw->data + offset, ret_count);
203 out:
204 mutex_unlock(&fw_lock);
205 return ret_count;
208 static int
209 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
211 u8 *new_data;
212 int new_size = fw_priv->alloc_size;
214 if (min_size <= fw_priv->alloc_size)
215 return 0;
217 new_size = ALIGN(min_size, PAGE_SIZE);
218 new_data = vmalloc(new_size);
219 if (!new_data) {
220 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
221 /* Make sure that we don't keep incomplete data */
222 fw_load_abort(fw_priv);
223 return -ENOMEM;
225 fw_priv->alloc_size = new_size;
226 if (fw_priv->fw->data) {
227 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
228 vfree(fw_priv->fw->data);
230 fw_priv->fw->data = new_data;
231 BUG_ON(min_size > fw_priv->alloc_size);
232 return 0;
236 * firmware_data_write - write method for firmware
237 * @kobj: kobject for the class_device
238 * @buffer: buffer being written
239 * @offset: buffer offset for write in total data store area
240 * @count: buffer size
242 * Data written to the 'data' attribute will be later handed to
243 * the driver as a firmware image.
245 static ssize_t
246 firmware_data_write(struct kobject *kobj,
247 char *buffer, loff_t offset, size_t count)
249 struct class_device *class_dev = to_class_dev(kobj);
250 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
251 struct firmware *fw;
252 ssize_t retval;
254 if (!capable(CAP_SYS_RAWIO))
255 return -EPERM;
257 mutex_lock(&fw_lock);
258 fw = fw_priv->fw;
259 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
260 retval = -ENODEV;
261 goto out;
263 retval = fw_realloc_buffer(fw_priv, offset + count);
264 if (retval)
265 goto out;
267 memcpy(fw->data + offset, buffer, count);
269 fw->size = max_t(size_t, offset + count, fw->size);
270 retval = count;
271 out:
272 mutex_unlock(&fw_lock);
273 return retval;
276 static struct bin_attribute firmware_attr_data_tmpl = {
277 .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
278 .size = 0,
279 .read = firmware_data_read,
280 .write = firmware_data_write,
283 static void
284 fw_class_dev_release(struct class_device *class_dev)
286 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
288 kfree(fw_priv);
289 kfree(class_dev);
291 module_put(THIS_MODULE);
294 static void
295 firmware_class_timeout(u_long data)
297 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
298 fw_load_abort(fw_priv);
301 static inline void
302 fw_setup_class_device_id(struct class_device *class_dev, struct device *dev)
304 /* XXX warning we should watch out for name collisions */
305 strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE);
308 static int
309 fw_register_class_device(struct class_device **class_dev_p,
310 const char *fw_name, struct device *device)
312 int retval;
313 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
314 GFP_KERNEL);
315 struct class_device *class_dev = kzalloc(sizeof(*class_dev),
316 GFP_KERNEL);
318 *class_dev_p = NULL;
320 if (!fw_priv || !class_dev) {
321 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
322 retval = -ENOMEM;
323 goto error_kfree;
326 init_completion(&fw_priv->completion);
327 fw_priv->attr_data = firmware_attr_data_tmpl;
328 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
330 fw_priv->timeout.function = firmware_class_timeout;
331 fw_priv->timeout.data = (u_long) fw_priv;
332 init_timer(&fw_priv->timeout);
334 fw_setup_class_device_id(class_dev, device);
335 class_dev->dev = device;
336 class_dev->class = &firmware_class;
337 class_set_devdata(class_dev, fw_priv);
338 retval = class_device_register(class_dev);
339 if (retval) {
340 printk(KERN_ERR "%s: class_device_register failed\n",
341 __FUNCTION__);
342 goto error_kfree;
344 *class_dev_p = class_dev;
345 return 0;
347 error_kfree:
348 kfree(fw_priv);
349 kfree(class_dev);
350 return retval;
353 static int
354 fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
355 const char *fw_name, struct device *device, int uevent)
357 struct class_device *class_dev;
358 struct firmware_priv *fw_priv;
359 int retval;
361 *class_dev_p = NULL;
362 retval = fw_register_class_device(&class_dev, fw_name, device);
363 if (retval)
364 goto out;
366 /* Need to pin this module until class device is destroyed */
367 __module_get(THIS_MODULE);
369 fw_priv = class_get_devdata(class_dev);
371 fw_priv->fw = fw;
372 retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data);
373 if (retval) {
374 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
375 __FUNCTION__);
376 goto error_unreg;
379 retval = class_device_create_file(class_dev,
380 &class_device_attr_loading);
381 if (retval) {
382 printk(KERN_ERR "%s: class_device_create_file failed\n",
383 __FUNCTION__);
384 goto error_unreg;
387 if (uevent)
388 set_bit(FW_STATUS_READY, &fw_priv->status);
389 else
390 set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status);
391 *class_dev_p = class_dev;
392 goto out;
394 error_unreg:
395 class_device_unregister(class_dev);
396 out:
397 return retval;
400 static int
401 _request_firmware(const struct firmware **firmware_p, const char *name,
402 struct device *device, int uevent)
404 struct class_device *class_dev;
405 struct firmware_priv *fw_priv;
406 struct firmware *firmware;
407 int retval;
409 if (!firmware_p)
410 return -EINVAL;
412 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
413 if (!firmware) {
414 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
415 __FUNCTION__);
416 retval = -ENOMEM;
417 goto out;
420 retval = fw_setup_class_device(firmware, &class_dev, name, device,
421 uevent);
422 if (retval)
423 goto error_kfree_fw;
425 fw_priv = class_get_devdata(class_dev);
427 if (uevent) {
428 if (loading_timeout > 0) {
429 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
430 add_timer(&fw_priv->timeout);
433 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
434 wait_for_completion(&fw_priv->completion);
435 set_bit(FW_STATUS_DONE, &fw_priv->status);
436 del_timer_sync(&fw_priv->timeout);
437 } else
438 wait_for_completion(&fw_priv->completion);
440 mutex_lock(&fw_lock);
441 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
442 retval = -ENOENT;
443 release_firmware(fw_priv->fw);
444 *firmware_p = NULL;
446 fw_priv->fw = NULL;
447 mutex_unlock(&fw_lock);
448 class_device_unregister(class_dev);
449 goto out;
451 error_kfree_fw:
452 kfree(firmware);
453 *firmware_p = NULL;
454 out:
455 return retval;
459 * request_firmware: - send firmware request and wait for it
460 * @firmware_p: pointer to firmware image
461 * @name: name of firmware file
462 * @device: device for which firmware is being loaded
464 * @firmware_p will be used to return a firmware image by the name
465 * of @name for device @device.
467 * Should be called from user context where sleeping is allowed.
469 * @name will be used as $FIRMWARE in the uevent environment and
470 * should be distinctive enough not to be confused with any other
471 * firmware image for this or any other device.
474 request_firmware(const struct firmware **firmware_p, const char *name,
475 struct device *device)
477 int uevent = 1;
478 return _request_firmware(firmware_p, name, device, uevent);
482 * release_firmware: - release the resource associated with a firmware image
483 * @fw: firmware resource to release
485 void
486 release_firmware(const struct firmware *fw)
488 if (fw) {
489 vfree(fw->data);
490 kfree(fw);
494 /* Async support */
495 struct firmware_work {
496 struct work_struct work;
497 struct module *module;
498 const char *name;
499 struct device *device;
500 void *context;
501 void (*cont)(const struct firmware *fw, void *context);
502 int uevent;
505 static int
506 request_firmware_work_func(void *arg)
508 struct firmware_work *fw_work = arg;
509 const struct firmware *fw;
510 int ret;
511 if (!arg) {
512 WARN_ON(1);
513 return 0;
515 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
516 fw_work->uevent);
517 if (ret < 0)
518 fw_work->cont(NULL, fw_work->context);
519 else {
520 fw_work->cont(fw, fw_work->context);
521 release_firmware(fw);
523 module_put(fw_work->module);
524 kfree(fw_work);
525 return ret;
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
538 * request is over.
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 task_struct *task;
550 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
551 GFP_ATOMIC);
553 if (!fw_work)
554 return -ENOMEM;
555 if (!try_module_get(module)) {
556 kfree(fw_work);
557 return -EFAULT;
560 *fw_work = (struct firmware_work) {
561 .module = module,
562 .name = name,
563 .device = device,
564 .context = context,
565 .cont = cont,
566 .uevent = uevent,
569 task = kthread_run(request_firmware_work_func, fw_work,
570 "firmware/%s", name);
572 if (IS_ERR(task)) {
573 fw_work->cont(NULL, fw_work->context);
574 module_put(fw_work->module);
575 kfree(fw_work);
576 return PTR_ERR(task);
578 return 0;
581 static int __init
582 firmware_class_init(void)
584 int error;
585 error = class_register(&firmware_class);
586 if (error) {
587 printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
588 return error;
590 error = class_create_file(&firmware_class, &class_attr_timeout);
591 if (error) {
592 printk(KERN_ERR "%s: class_create_file failed\n",
593 __FUNCTION__);
594 class_unregister(&firmware_class);
596 return error;
599 static void __exit
600 firmware_class_exit(void)
602 class_unregister(&firmware_class);
605 fs_initcall(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);