[PATCH] NOMMU: make SYSV SHM nattch work correctly
[linux-2.6/linux-2.6-openrd.git] / drivers / base / firmware_class.c
blobc0a979a5074bbec75ef691cfe1f13b269c1332fb
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 #define to_dev(obj) container_of(obj, struct device, kobj)
26 MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
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,
34 FW_STATUS_READY,
35 FW_STATUS_READY_NOHOTPLUG,
38 static int loading_timeout = 60; /* In seconds */
40 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
41 * guarding for corner cases a global lock should be OK */
42 static DEFINE_MUTEX(fw_lock);
44 struct firmware_priv {
45 char fw_id[FIRMWARE_NAME_MAX];
46 struct completion completion;
47 struct bin_attribute attr_data;
48 struct firmware *fw;
49 unsigned long status;
50 int alloc_size;
51 struct timer_list timeout;
54 static void
55 fw_load_abort(struct firmware_priv *fw_priv)
57 set_bit(FW_STATUS_ABORT, &fw_priv->status);
58 wmb();
59 complete(&fw_priv->completion);
62 static ssize_t
63 firmware_timeout_show(struct class *class, char *buf)
65 return sprintf(buf, "%d\n", loading_timeout);
68 /**
69 * firmware_timeout_store - set number of seconds to wait for firmware
70 * @class: device class pointer
71 * @buf: buffer to scan for timeout value
72 * @count: number of bytes in @buf
74 * Sets the number of seconds to wait for the firmware. Once
75 * this expires an error will be returned to the driver and no
76 * firmware will be provided.
78 * Note: zero means 'wait forever'.
79 **/
80 static ssize_t
81 firmware_timeout_store(struct class *class, const char *buf, size_t count)
83 loading_timeout = simple_strtol(buf, NULL, 10);
84 if (loading_timeout < 0)
85 loading_timeout = 0;
86 return count;
89 static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
91 static void fw_dev_release(struct device *dev);
93 static int firmware_uevent(struct device *dev, char **envp, int num_envp,
94 char *buffer, int buffer_size)
96 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
97 int i = 0, len = 0;
99 if (!test_bit(FW_STATUS_READY, &fw_priv->status))
100 return -ENODEV;
102 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
103 "FIRMWARE=%s", fw_priv->fw_id))
104 return -ENOMEM;
105 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
106 "TIMEOUT=%i", loading_timeout))
107 return -ENOMEM;
108 envp[i] = NULL;
110 return 0;
113 static struct class firmware_class = {
114 .name = "firmware",
115 .dev_uevent = firmware_uevent,
116 .dev_release = fw_dev_release,
119 static ssize_t firmware_loading_show(struct device *dev,
120 struct device_attribute *attr, char *buf)
122 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
123 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
124 return sprintf(buf, "%d\n", loading);
128 * firmware_loading_store - set value in the 'loading' control file
129 * @dev: device pointer
130 * @attr: device attribute pointer
131 * @buf: buffer to scan for loading control value
132 * @count: number of bytes in @buf
134 * The relevant values are:
136 * 1: Start a load, discarding any previous partial load.
137 * 0: Conclude the load and hand the data to the driver code.
138 * -1: Conclude the load with an error and discard any written data.
140 static ssize_t firmware_loading_store(struct device *dev,
141 struct device_attribute *attr,
142 const char *buf, size_t count)
144 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
145 int loading = simple_strtol(buf, NULL, 10);
147 switch (loading) {
148 case 1:
149 mutex_lock(&fw_lock);
150 if (!fw_priv->fw) {
151 mutex_unlock(&fw_lock);
152 break;
154 vfree(fw_priv->fw->data);
155 fw_priv->fw->data = NULL;
156 fw_priv->fw->size = 0;
157 fw_priv->alloc_size = 0;
158 set_bit(FW_STATUS_LOADING, &fw_priv->status);
159 mutex_unlock(&fw_lock);
160 break;
161 case 0:
162 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
163 complete(&fw_priv->completion);
164 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
165 break;
167 /* fallthrough */
168 default:
169 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
170 loading);
171 /* fallthrough */
172 case -1:
173 fw_load_abort(fw_priv);
174 break;
177 return count;
180 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
182 static ssize_t
183 firmware_data_read(struct kobject *kobj,
184 char *buffer, loff_t offset, size_t count)
186 struct device *dev = to_dev(kobj);
187 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
188 struct firmware *fw;
189 ssize_t ret_count = count;
191 mutex_lock(&fw_lock);
192 fw = fw_priv->fw;
193 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
194 ret_count = -ENODEV;
195 goto out;
197 if (offset > fw->size) {
198 ret_count = 0;
199 goto out;
201 if (offset + ret_count > fw->size)
202 ret_count = fw->size - offset;
204 memcpy(buffer, fw->data + offset, ret_count);
205 out:
206 mutex_unlock(&fw_lock);
207 return ret_count;
210 static int
211 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
213 u8 *new_data;
214 int new_size = fw_priv->alloc_size;
216 if (min_size <= fw_priv->alloc_size)
217 return 0;
219 new_size = ALIGN(min_size, PAGE_SIZE);
220 new_data = vmalloc(new_size);
221 if (!new_data) {
222 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
223 /* Make sure that we don't keep incomplete data */
224 fw_load_abort(fw_priv);
225 return -ENOMEM;
227 fw_priv->alloc_size = new_size;
228 if (fw_priv->fw->data) {
229 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
230 vfree(fw_priv->fw->data);
232 fw_priv->fw->data = new_data;
233 BUG_ON(min_size > fw_priv->alloc_size);
234 return 0;
238 * firmware_data_write - write method for firmware
239 * @kobj: kobject for the device
240 * @buffer: buffer being written
241 * @offset: buffer offset for write in total data store area
242 * @count: buffer size
244 * Data written to the 'data' attribute will be later handed to
245 * the driver as a firmware image.
247 static ssize_t
248 firmware_data_write(struct kobject *kobj,
249 char *buffer, loff_t offset, size_t count)
251 struct device *dev = to_dev(kobj);
252 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
253 struct firmware *fw;
254 ssize_t retval;
256 if (!capable(CAP_SYS_RAWIO))
257 return -EPERM;
259 mutex_lock(&fw_lock);
260 fw = fw_priv->fw;
261 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
262 retval = -ENODEV;
263 goto out;
265 retval = fw_realloc_buffer(fw_priv, offset + count);
266 if (retval)
267 goto out;
269 memcpy(fw->data + offset, buffer, count);
271 fw->size = max_t(size_t, offset + count, fw->size);
272 retval = count;
273 out:
274 mutex_unlock(&fw_lock);
275 return retval;
278 static struct bin_attribute firmware_attr_data_tmpl = {
279 .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
280 .size = 0,
281 .read = firmware_data_read,
282 .write = firmware_data_write,
285 static void fw_dev_release(struct device *dev)
287 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
289 kfree(fw_priv);
290 kfree(dev);
292 module_put(THIS_MODULE);
295 static void
296 firmware_class_timeout(u_long data)
298 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
299 fw_load_abort(fw_priv);
302 static inline void fw_setup_device_id(struct device *f_dev, struct device *dev)
304 /* XXX warning we should watch out for name collisions */
305 strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE);
308 static int fw_register_device(struct device **dev_p, const char *fw_name,
309 struct device *device)
311 int retval;
312 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
313 GFP_KERNEL);
314 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
316 *dev_p = NULL;
318 if (!fw_priv || !f_dev) {
319 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
320 retval = -ENOMEM;
321 goto error_kfree;
324 init_completion(&fw_priv->completion);
325 fw_priv->attr_data = firmware_attr_data_tmpl;
326 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
328 fw_priv->timeout.function = firmware_class_timeout;
329 fw_priv->timeout.data = (u_long) fw_priv;
330 init_timer(&fw_priv->timeout);
332 fw_setup_device_id(f_dev, device);
333 f_dev->parent = device;
334 f_dev->class = &firmware_class;
335 dev_set_drvdata(f_dev, fw_priv);
336 retval = device_register(f_dev);
337 if (retval) {
338 printk(KERN_ERR "%s: device_register failed\n",
339 __FUNCTION__);
340 goto error_kfree;
342 *dev_p = f_dev;
343 return 0;
345 error_kfree:
346 kfree(fw_priv);
347 kfree(f_dev);
348 return retval;
351 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
352 const char *fw_name, struct device *device,
353 int uevent)
355 struct device *f_dev;
356 struct firmware_priv *fw_priv;
357 int retval;
359 *dev_p = NULL;
360 retval = fw_register_device(&f_dev, fw_name, device);
361 if (retval)
362 goto out;
364 /* Need to pin this module until class device is destroyed */
365 __module_get(THIS_MODULE);
367 fw_priv = dev_get_drvdata(f_dev);
369 fw_priv->fw = fw;
370 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
371 if (retval) {
372 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
373 __FUNCTION__);
374 goto error_unreg;
377 retval = device_create_file(f_dev, &dev_attr_loading);
378 if (retval) {
379 printk(KERN_ERR "%s: device_create_file failed\n",
380 __FUNCTION__);
381 goto error_unreg;
384 if (uevent)
385 set_bit(FW_STATUS_READY, &fw_priv->status);
386 else
387 set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status);
388 *dev_p = f_dev;
389 goto out;
391 error_unreg:
392 device_unregister(f_dev);
393 out:
394 return retval;
397 static int
398 _request_firmware(const struct firmware **firmware_p, const char *name,
399 struct device *device, int uevent)
401 struct device *f_dev;
402 struct firmware_priv *fw_priv;
403 struct firmware *firmware;
404 int retval;
406 if (!firmware_p)
407 return -EINVAL;
409 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
410 if (!firmware) {
411 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
412 __FUNCTION__);
413 retval = -ENOMEM;
414 goto out;
417 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
418 if (retval)
419 goto error_kfree_fw;
421 fw_priv = dev_get_drvdata(f_dev);
423 if (uevent) {
424 if (loading_timeout > 0) {
425 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
426 add_timer(&fw_priv->timeout);
429 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
430 wait_for_completion(&fw_priv->completion);
431 set_bit(FW_STATUS_DONE, &fw_priv->status);
432 del_timer_sync(&fw_priv->timeout);
433 } else
434 wait_for_completion(&fw_priv->completion);
436 mutex_lock(&fw_lock);
437 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
438 retval = -ENOENT;
439 release_firmware(fw_priv->fw);
440 *firmware_p = NULL;
442 fw_priv->fw = NULL;
443 mutex_unlock(&fw_lock);
444 device_unregister(f_dev);
445 goto out;
447 error_kfree_fw:
448 kfree(firmware);
449 *firmware_p = NULL;
450 out:
451 return retval;
455 * request_firmware: - send firmware request and wait for it
456 * @firmware_p: pointer to firmware image
457 * @name: name of firmware file
458 * @device: device for which firmware is being loaded
460 * @firmware_p will be used to return a firmware image by the name
461 * of @name for device @device.
463 * Should be called from user context where sleeping is allowed.
465 * @name will be used as $FIRMWARE in the uevent environment and
466 * should be distinctive enough not to be confused with any other
467 * firmware image for this or any other device.
470 request_firmware(const struct firmware **firmware_p, const char *name,
471 struct device *device)
473 int uevent = 1;
474 return _request_firmware(firmware_p, name, device, uevent);
478 * release_firmware: - release the resource associated with a firmware image
479 * @fw: firmware resource to release
481 void
482 release_firmware(const struct firmware *fw)
484 if (fw) {
485 vfree(fw->data);
486 kfree(fw);
490 /* Async support */
491 struct firmware_work {
492 struct work_struct work;
493 struct module *module;
494 const char *name;
495 struct device *device;
496 void *context;
497 void (*cont)(const struct firmware *fw, void *context);
498 int uevent;
501 static int
502 request_firmware_work_func(void *arg)
504 struct firmware_work *fw_work = arg;
505 const struct firmware *fw;
506 int ret;
507 if (!arg) {
508 WARN_ON(1);
509 return 0;
511 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
512 fw_work->uevent);
513 if (ret < 0)
514 fw_work->cont(NULL, fw_work->context);
515 else {
516 fw_work->cont(fw, fw_work->context);
517 release_firmware(fw);
519 module_put(fw_work->module);
520 kfree(fw_work);
521 return ret;
525 * request_firmware_nowait: asynchronous version of request_firmware
526 * @module: module requesting the firmware
527 * @uevent: sends uevent to copy the firmware image if this flag
528 * is non-zero else the firmware copy must be done manually.
529 * @name: name of firmware file
530 * @device: device for which firmware is being loaded
531 * @context: will be passed over to @cont, and
532 * @fw may be %NULL if firmware request fails.
533 * @cont: function will be called asynchronously when the firmware
534 * request is over.
536 * Asynchronous variant of request_firmware() for contexts where
537 * it is not possible to sleep.
540 request_firmware_nowait(
541 struct module *module, int uevent,
542 const char *name, struct device *device, void *context,
543 void (*cont)(const struct firmware *fw, void *context))
545 struct task_struct *task;
546 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
547 GFP_ATOMIC);
549 if (!fw_work)
550 return -ENOMEM;
551 if (!try_module_get(module)) {
552 kfree(fw_work);
553 return -EFAULT;
556 *fw_work = (struct firmware_work) {
557 .module = module,
558 .name = name,
559 .device = device,
560 .context = context,
561 .cont = cont,
562 .uevent = uevent,
565 task = kthread_run(request_firmware_work_func, fw_work,
566 "firmware/%s", name);
568 if (IS_ERR(task)) {
569 fw_work->cont(NULL, fw_work->context);
570 module_put(fw_work->module);
571 kfree(fw_work);
572 return PTR_ERR(task);
574 return 0;
577 static int __init
578 firmware_class_init(void)
580 int error;
581 error = class_register(&firmware_class);
582 if (error) {
583 printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
584 return error;
586 error = class_create_file(&firmware_class, &class_attr_timeout);
587 if (error) {
588 printk(KERN_ERR "%s: class_create_file failed\n",
589 __FUNCTION__);
590 class_unregister(&firmware_class);
592 return error;
595 static void __exit
596 firmware_class_exit(void)
598 class_unregister(&firmware_class);
601 fs_initcall(firmware_class_init);
602 module_exit(firmware_class_exit);
604 EXPORT_SYMBOL(release_firmware);
605 EXPORT_SYMBOL(request_firmware);
606 EXPORT_SYMBOL(request_firmware_nowait);