MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / base / firmware_class.c
blob6e17867db85ca8b13f626b02681720e1408d0323
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/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>
20 #include "base.h"
22 MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
23 MODULE_DESCRIPTION("Multi purpose firmware loading support");
24 MODULE_LICENSE("GPL");
26 enum {
27 FW_STATUS_LOADING,
28 FW_STATUS_DONE,
29 FW_STATUS_ABORT,
30 FW_STATUS_READY,
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;
43 struct firmware *fw;
44 unsigned long status;
45 int alloc_size;
46 struct timer_list timeout;
49 static inline void
50 fw_load_abort(struct firmware_priv *fw_priv)
52 set_bit(FW_STATUS_ABORT, &fw_priv->status);
53 wmb();
54 complete(&fw_priv->completion);
57 static ssize_t
58 firmware_timeout_show(struct class *class, char *buf)
60 return sprintf(buf, "%d\n", loading_timeout);
63 /**
64 * firmware_timeout_store:
65 * Description:
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'
72 **/
73 static ssize_t
74 firmware_timeout_store(struct class *class, const char *buf, size_t count)
76 loading_timeout = simple_strtol(buf, NULL, 10);
77 return count;
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 = {
87 .name = "firmware",
88 .hotplug = firmware_class_hotplug,
89 .release = fw_class_dev_release,
92 int
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);
97 int i = 0;
98 char *scratch = buffer;
100 if (!test_bit(FW_STATUS_READY, &fw_priv->status))
101 return -ENODEV;
103 if (buffer_size < (FIRMWARE_NAME_MAX + 10))
104 return -ENOMEM;
105 if (num_envp < 1)
106 return -ENOMEM;
108 envp[i++] = scratch;
109 scratch += sprintf(scratch, "FIRMWARE=%s", fw_priv->fw_id) + 1;
110 return 0;
113 static ssize_t
114 firmware_loading_show(struct class_device *class_dev, char *buf)
116 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
117 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
118 return sprintf(buf, "%d\n", loading);
122 * firmware_loading_store: - loading control file
123 * Description:
124 * The relevant values are:
126 * 1: Start a load, discarding any previous partial load.
127 * 0: Conclude the load and handle the data to the driver code.
128 * -1: Conclude the load with an error and discard any written data.
130 static ssize_t
131 firmware_loading_store(struct class_device *class_dev,
132 const char *buf, size_t count)
134 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
135 int loading = simple_strtol(buf, NULL, 10);
137 switch (loading) {
138 case 1:
139 down(&fw_lock);
140 vfree(fw_priv->fw->data);
141 fw_priv->fw->data = NULL;
142 fw_priv->fw->size = 0;
143 fw_priv->alloc_size = 0;
144 set_bit(FW_STATUS_LOADING, &fw_priv->status);
145 up(&fw_lock);
146 break;
147 case 0:
148 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
149 complete(&fw_priv->completion);
150 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
151 break;
153 /* fallthrough */
154 default:
155 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
156 loading);
157 /* fallthrough */
158 case -1:
159 fw_load_abort(fw_priv);
160 break;
163 return count;
166 static CLASS_DEVICE_ATTR(loading, 0644,
167 firmware_loading_show, firmware_loading_store);
169 static ssize_t
170 firmware_data_read(struct kobject *kobj,
171 char *buffer, loff_t offset, size_t count)
173 struct class_device *class_dev = to_class_dev(kobj);
174 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
175 struct firmware *fw;
176 ssize_t ret_count = count;
178 down(&fw_lock);
179 fw = fw_priv->fw;
180 if (test_bit(FW_STATUS_DONE, &fw_priv->status)) {
181 ret_count = -ENODEV;
182 goto out;
184 if (offset > fw->size) {
185 ret_count = 0;
186 goto out;
188 if (offset + ret_count > fw->size)
189 ret_count = fw->size - offset;
191 memcpy(buffer, fw->data + offset, ret_count);
192 out:
193 up(&fw_lock);
194 return ret_count;
196 static int
197 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
199 u8 *new_data;
201 if (min_size <= fw_priv->alloc_size)
202 return 0;
204 new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
205 if (!new_data) {
206 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
207 /* Make sure that we don't keep incomplete data */
208 fw_load_abort(fw_priv);
209 return -ENOMEM;
211 fw_priv->alloc_size += PAGE_SIZE;
212 if (fw_priv->fw->data) {
213 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
214 vfree(fw_priv->fw->data);
216 fw_priv->fw->data = new_data;
217 BUG_ON(min_size > fw_priv->alloc_size);
218 return 0;
222 * firmware_data_write:
224 * Description:
226 * Data written to the 'data' attribute will be later handled to
227 * the driver as a firmware image.
229 static ssize_t
230 firmware_data_write(struct kobject *kobj,
231 char *buffer, loff_t offset, size_t count)
233 struct class_device *class_dev = to_class_dev(kobj);
234 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
235 struct firmware *fw;
236 ssize_t retval;
238 if (!capable(CAP_SYS_RAWIO))
239 return -EPERM;
240 down(&fw_lock);
241 fw = fw_priv->fw;
242 if (test_bit(FW_STATUS_DONE, &fw_priv->status)) {
243 retval = -ENODEV;
244 goto out;
246 retval = fw_realloc_buffer(fw_priv, offset + count);
247 if (retval)
248 goto out;
250 memcpy(fw->data + offset, buffer, count);
252 fw->size = max_t(size_t, offset + count, fw->size);
253 retval = count;
254 out:
255 up(&fw_lock);
256 return retval;
258 static struct bin_attribute firmware_attr_data_tmpl = {
259 .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
260 .size = 0,
261 .read = firmware_data_read,
262 .write = firmware_data_write,
265 static void
266 fw_class_dev_release(struct class_device *class_dev)
268 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
270 kfree(fw_priv);
271 kfree(class_dev);
273 module_put(THIS_MODULE);
276 static void
277 firmware_class_timeout(u_long data)
279 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
280 fw_load_abort(fw_priv);
283 static inline void
284 fw_setup_class_device_id(struct class_device *class_dev, struct device *dev)
286 /* XXX warning we should watch out for name collisions */
287 strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE);
290 static int
291 fw_register_class_device(struct class_device **class_dev_p,
292 const char *fw_name, struct device *device)
294 int retval;
295 struct firmware_priv *fw_priv = kmalloc(sizeof (struct firmware_priv),
296 GFP_KERNEL);
297 struct class_device *class_dev = kmalloc(sizeof (struct class_device),
298 GFP_KERNEL);
300 *class_dev_p = NULL;
302 if (!fw_priv || !class_dev) {
303 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
304 retval = -ENOMEM;
305 goto error_kfree;
307 memset(fw_priv, 0, sizeof (*fw_priv));
308 memset(class_dev, 0, sizeof (*class_dev));
310 init_completion(&fw_priv->completion);
311 fw_priv->attr_data = firmware_attr_data_tmpl;
312 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
314 fw_priv->timeout.function = firmware_class_timeout;
315 fw_priv->timeout.data = (u_long) fw_priv;
316 init_timer(&fw_priv->timeout);
318 fw_setup_class_device_id(class_dev, device);
319 class_dev->dev = device;
320 class_dev->class = &firmware_class;
321 class_set_devdata(class_dev, fw_priv);
322 retval = class_device_register(class_dev);
323 if (retval) {
324 printk(KERN_ERR "%s: class_device_register failed\n",
325 __FUNCTION__);
326 goto error_kfree;
328 *class_dev_p = class_dev;
329 return 0;
331 error_kfree:
332 kfree(fw_priv);
333 kfree(class_dev);
334 return retval;
337 static int
338 fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
339 const char *fw_name, struct device *device)
341 struct class_device *class_dev;
342 struct firmware_priv *fw_priv;
343 int retval;
345 *class_dev_p = NULL;
346 retval = fw_register_class_device(&class_dev, fw_name, device);
347 if (retval)
348 goto out;
350 /* Need to pin this module until class device is destroyed */
351 __module_get(THIS_MODULE);
353 fw_priv = class_get_devdata(class_dev);
355 fw_priv->fw = fw;
356 retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data);
357 if (retval) {
358 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
359 __FUNCTION__);
360 goto error_unreg;
363 retval = class_device_create_file(class_dev,
364 &class_device_attr_loading);
365 if (retval) {
366 printk(KERN_ERR "%s: class_device_create_file failed\n",
367 __FUNCTION__);
368 goto error_unreg;
371 set_bit(FW_STATUS_READY, &fw_priv->status);
372 *class_dev_p = class_dev;
373 goto out;
375 error_unreg:
376 class_device_unregister(class_dev);
377 out:
378 return retval;
382 * request_firmware: - request firmware to hotplug and wait for it
383 * Description:
384 * @firmware will be used to return a firmware image by the name
385 * of @name for device @device.
387 * Should be called from user context where sleeping is allowed.
389 * @name will be use as $FIRMWARE in the hotplug environment and
390 * should be distinctive enough not to be confused with any other
391 * firmware image for this or any other device.
394 request_firmware(const struct firmware **firmware_p, const char *name,
395 struct device *device)
397 struct class_device *class_dev;
398 struct firmware_priv *fw_priv;
399 struct firmware *firmware;
400 int retval;
402 if (!firmware_p)
403 return -EINVAL;
405 *firmware_p = firmware = kmalloc(sizeof (struct firmware), GFP_KERNEL);
406 if (!firmware) {
407 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
408 __FUNCTION__);
409 retval = -ENOMEM;
410 goto out;
412 memset(firmware, 0, sizeof (*firmware));
414 retval = fw_setup_class_device(firmware, &class_dev, name, device);
415 if (retval)
416 goto error_kfree_fw;
418 fw_priv = class_get_devdata(class_dev);
420 if (loading_timeout) {
421 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
422 add_timer(&fw_priv->timeout);
425 kobject_hotplug("add", &class_dev->kobj);
426 wait_for_completion(&fw_priv->completion);
427 set_bit(FW_STATUS_DONE, &fw_priv->status);
429 del_timer_sync(&fw_priv->timeout);
431 down(&fw_lock);
432 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
433 retval = -ENOENT;
434 release_firmware(fw_priv->fw);
435 *firmware_p = NULL;
437 fw_priv->fw = NULL;
438 up(&fw_lock);
439 class_device_unregister(class_dev);
440 goto out;
442 error_kfree_fw:
443 kfree(firmware);
444 out:
445 return retval;
449 * release_firmware: - release the resource associated with a firmware image
451 void
452 release_firmware(const struct firmware *fw)
454 if (fw) {
455 vfree(fw->data);
456 kfree(fw);
461 * register_firmware: - provide a firmware image for later usage
463 * Description:
464 * Make sure that @data will be available by requesting firmware @name.
466 * Note: This will not be possible until some kind of persistence
467 * is available.
469 void
470 register_firmware(const char *name, const u8 *data, size_t size)
472 /* This is meaningless without firmware caching, so until we
473 * decide if firmware caching is reasonable just leave it as a
474 * noop */
477 /* Async support */
478 struct firmware_work {
479 struct work_struct work;
480 struct module *module;
481 const char *name;
482 struct device *device;
483 void *context;
484 void (*cont)(const struct firmware *fw, void *context);
487 static int
488 request_firmware_work_func(void *arg)
490 struct firmware_work *fw_work = arg;
491 const struct firmware *fw;
492 if (!arg) {
493 WARN_ON(1);
494 return 0;
496 daemonize("%s/%s", "firmware", fw_work->name);
497 request_firmware(&fw, fw_work->name, fw_work->device);
498 fw_work->cont(fw, fw_work->context);
499 release_firmware(fw);
500 module_put(fw_work->module);
501 kfree(fw_work);
502 return 0;
506 * request_firmware_nowait:
508 * Description:
509 * Asynchronous variant of request_firmware() for contexts where
510 * it is not possible to sleep.
512 * @cont will be called asynchronously when the firmware request is over.
514 * @context will be passed over to @cont.
516 * @fw may be %NULL if firmware request fails.
520 request_firmware_nowait(
521 struct module *module,
522 const char *name, struct device *device, void *context,
523 void (*cont)(const struct firmware *fw, void *context))
525 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
526 GFP_ATOMIC);
527 int ret;
529 if (!fw_work)
530 return -ENOMEM;
531 if (!try_module_get(module)) {
532 kfree(fw_work);
533 return -EFAULT;
536 *fw_work = (struct firmware_work) {
537 .module = module,
538 .name = name,
539 .device = device,
540 .context = context,
541 .cont = cont,
544 ret = kernel_thread(request_firmware_work_func, fw_work,
545 CLONE_FS | CLONE_FILES);
547 if (ret < 0) {
548 fw_work->cont(NULL, fw_work->context);
549 return ret;
551 return 0;
554 static int __init
555 firmware_class_init(void)
557 int error;
558 error = class_register(&firmware_class);
559 if (error) {
560 printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
561 return error;
563 error = class_create_file(&firmware_class, &class_attr_timeout);
564 if (error) {
565 printk(KERN_ERR "%s: class_create_file failed\n",
566 __FUNCTION__);
567 class_unregister(&firmware_class);
569 return error;
572 static void __exit
573 firmware_class_exit(void)
575 class_unregister(&firmware_class);
578 module_init(firmware_class_init);
579 module_exit(firmware_class_exit);
581 EXPORT_SYMBOL(release_firmware);
582 EXPORT_SYMBOL(request_firmware);
583 EXPORT_SYMBOL(request_firmware_nowait);
584 EXPORT_SYMBOL(register_firmware);
585 EXPORT_SYMBOL(firmware_class);