platform: x86: asus_acpi: world-writable procfs files
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / firmware_class.c
blobff911aef4d7d9da38a9134128260010663a6c8b7
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>
20 #include <linux/highmem.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;
44 struct completion completion;
45 struct bin_attribute attr_data;
46 struct firmware *fw;
47 unsigned long status;
48 struct page **pages;
49 int nr_pages;
50 int page_array_size;
51 const char *vdata;
52 struct timer_list timeout;
55 #ifdef CONFIG_FW_LOADER
56 extern struct builtin_fw __start_builtin_fw[];
57 extern struct builtin_fw __end_builtin_fw[];
58 #else /* Module case. Avoid ifdefs later; it'll all optimise out */
59 static struct builtin_fw *__start_builtin_fw;
60 static struct builtin_fw *__end_builtin_fw;
61 #endif
63 static void
64 fw_load_abort(struct firmware_priv *fw_priv)
66 set_bit(FW_STATUS_ABORT, &fw_priv->status);
67 wmb();
68 complete(&fw_priv->completion);
71 static ssize_t
72 firmware_timeout_show(struct class *class, char *buf)
74 return sprintf(buf, "%d\n", loading_timeout);
77 /**
78 * firmware_timeout_store - set number of seconds to wait for firmware
79 * @class: device class pointer
80 * @buf: buffer to scan for timeout value
81 * @count: number of bytes in @buf
83 * Sets the number of seconds to wait for the firmware. Once
84 * this expires an error will be returned to the driver and no
85 * firmware will be provided.
87 * Note: zero means 'wait forever'.
88 **/
89 static ssize_t
90 firmware_timeout_store(struct class *class, const char *buf, size_t count)
92 loading_timeout = simple_strtol(buf, NULL, 10);
93 if (loading_timeout < 0)
94 loading_timeout = 0;
95 return count;
98 static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
100 static void fw_dev_release(struct device *dev);
102 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
104 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
106 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
107 return -ENOMEM;
108 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
109 return -ENOMEM;
111 return 0;
114 static struct class firmware_class = {
115 .name = "firmware",
116 .dev_uevent = firmware_uevent,
117 .dev_release = fw_dev_release,
120 static ssize_t firmware_loading_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
123 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
124 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
125 return sprintf(buf, "%d\n", loading);
128 static void firmware_free_data(const struct firmware *fw)
130 int i;
131 vunmap(fw->data);
132 if (fw->pages) {
133 for (i = 0; i < PFN_UP(fw->size); i++)
134 __free_page(fw->pages[i]);
135 kfree(fw->pages);
139 /* Some architectures don't have PAGE_KERNEL_RO */
140 #ifndef PAGE_KERNEL_RO
141 #define PAGE_KERNEL_RO PAGE_KERNEL
142 #endif
144 * firmware_loading_store - set value in the 'loading' control file
145 * @dev: device pointer
146 * @attr: device attribute pointer
147 * @buf: buffer to scan for loading control value
148 * @count: number of bytes in @buf
150 * The relevant values are:
152 * 1: Start a load, discarding any previous partial load.
153 * 0: Conclude the load and hand the data to the driver code.
154 * -1: Conclude the load with an error and discard any written data.
156 static ssize_t firmware_loading_store(struct device *dev,
157 struct device_attribute *attr,
158 const char *buf, size_t count)
160 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
161 int loading = simple_strtol(buf, NULL, 10);
162 int i;
164 switch (loading) {
165 case 1:
166 mutex_lock(&fw_lock);
167 if (!fw_priv->fw) {
168 mutex_unlock(&fw_lock);
169 break;
171 firmware_free_data(fw_priv->fw);
172 memset(fw_priv->fw, 0, sizeof(struct firmware));
173 /* If the pages are not owned by 'struct firmware' */
174 for (i = 0; i < fw_priv->nr_pages; i++)
175 __free_page(fw_priv->pages[i]);
176 kfree(fw_priv->pages);
177 fw_priv->pages = NULL;
178 fw_priv->page_array_size = 0;
179 fw_priv->nr_pages = 0;
180 set_bit(FW_STATUS_LOADING, &fw_priv->status);
181 mutex_unlock(&fw_lock);
182 break;
183 case 0:
184 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
185 vunmap(fw_priv->fw->data);
186 fw_priv->fw->data = vmap(fw_priv->pages,
187 fw_priv->nr_pages,
188 0, PAGE_KERNEL_RO);
189 if (!fw_priv->fw->data) {
190 dev_err(dev, "%s: vmap() failed\n", __func__);
191 goto err;
193 /* Pages are now owned by 'struct firmware' */
194 fw_priv->fw->pages = fw_priv->pages;
195 fw_priv->pages = NULL;
197 fw_priv->page_array_size = 0;
198 fw_priv->nr_pages = 0;
199 complete(&fw_priv->completion);
200 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
201 break;
203 /* fallthrough */
204 default:
205 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
206 /* fallthrough */
207 case -1:
208 err:
209 fw_load_abort(fw_priv);
210 break;
213 return count;
216 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
218 static ssize_t
219 firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
220 char *buffer, loff_t offset, size_t count)
222 struct device *dev = to_dev(kobj);
223 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
224 struct firmware *fw;
225 ssize_t ret_count;
227 mutex_lock(&fw_lock);
228 fw = fw_priv->fw;
229 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
230 ret_count = -ENODEV;
231 goto out;
233 if (offset > fw->size) {
234 ret_count = 0;
235 goto out;
237 if (count > fw->size - offset)
238 count = fw->size - offset;
240 ret_count = count;
242 while (count) {
243 void *page_data;
244 int page_nr = offset >> PAGE_SHIFT;
245 int page_ofs = offset & (PAGE_SIZE-1);
246 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
248 page_data = kmap(fw_priv->pages[page_nr]);
250 memcpy(buffer, page_data + page_ofs, page_cnt);
252 kunmap(fw_priv->pages[page_nr]);
253 buffer += page_cnt;
254 offset += page_cnt;
255 count -= page_cnt;
257 out:
258 mutex_unlock(&fw_lock);
259 return ret_count;
262 static int
263 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
265 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
267 /* If the array of pages is too small, grow it... */
268 if (fw_priv->page_array_size < pages_needed) {
269 int new_array_size = max(pages_needed,
270 fw_priv->page_array_size * 2);
271 struct page **new_pages;
273 new_pages = kmalloc(new_array_size * sizeof(void *),
274 GFP_KERNEL);
275 if (!new_pages) {
276 fw_load_abort(fw_priv);
277 return -ENOMEM;
279 memcpy(new_pages, fw_priv->pages,
280 fw_priv->page_array_size * sizeof(void *));
281 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
282 (new_array_size - fw_priv->page_array_size));
283 kfree(fw_priv->pages);
284 fw_priv->pages = new_pages;
285 fw_priv->page_array_size = new_array_size;
288 while (fw_priv->nr_pages < pages_needed) {
289 fw_priv->pages[fw_priv->nr_pages] =
290 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
292 if (!fw_priv->pages[fw_priv->nr_pages]) {
293 fw_load_abort(fw_priv);
294 return -ENOMEM;
296 fw_priv->nr_pages++;
298 return 0;
302 * firmware_data_write - write method for firmware
303 * @kobj: kobject for the device
304 * @bin_attr: bin_attr structure
305 * @buffer: buffer being written
306 * @offset: buffer offset for write in total data store area
307 * @count: buffer size
309 * Data written to the 'data' attribute will be later handed to
310 * the driver as a firmware image.
312 static ssize_t
313 firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
314 char *buffer, loff_t offset, size_t count)
316 struct device *dev = to_dev(kobj);
317 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
318 struct firmware *fw;
319 ssize_t retval;
321 if (!capable(CAP_SYS_RAWIO))
322 return -EPERM;
324 mutex_lock(&fw_lock);
325 fw = fw_priv->fw;
326 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
327 retval = -ENODEV;
328 goto out;
330 retval = fw_realloc_buffer(fw_priv, offset + count);
331 if (retval)
332 goto out;
334 retval = count;
336 while (count) {
337 void *page_data;
338 int page_nr = offset >> PAGE_SHIFT;
339 int page_ofs = offset & (PAGE_SIZE - 1);
340 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
342 page_data = kmap(fw_priv->pages[page_nr]);
344 memcpy(page_data + page_ofs, buffer, page_cnt);
346 kunmap(fw_priv->pages[page_nr]);
347 buffer += page_cnt;
348 offset += page_cnt;
349 count -= page_cnt;
352 fw->size = max_t(size_t, offset, fw->size);
353 out:
354 mutex_unlock(&fw_lock);
355 return retval;
358 static struct bin_attribute firmware_attr_data_tmpl = {
359 .attr = {.name = "data", .mode = 0644},
360 .size = 0,
361 .read = firmware_data_read,
362 .write = firmware_data_write,
365 static void fw_dev_release(struct device *dev)
367 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
368 int i;
370 for (i = 0; i < fw_priv->nr_pages; i++)
371 __free_page(fw_priv->pages[i]);
372 kfree(fw_priv->pages);
373 kfree(fw_priv->fw_id);
374 kfree(fw_priv);
375 kfree(dev);
377 module_put(THIS_MODULE);
380 static void
381 firmware_class_timeout(u_long data)
383 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
384 fw_load_abort(fw_priv);
387 static int fw_register_device(struct device **dev_p, const char *fw_name,
388 struct device *device)
390 int retval;
391 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
392 GFP_KERNEL);
393 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
395 *dev_p = NULL;
397 if (!fw_priv || !f_dev) {
398 dev_err(device, "%s: kmalloc failed\n", __func__);
399 retval = -ENOMEM;
400 goto error_kfree;
403 init_completion(&fw_priv->completion);
404 fw_priv->attr_data = firmware_attr_data_tmpl;
405 fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
406 if (!fw_priv->fw_id) {
407 dev_err(device, "%s: Firmware name allocation failed\n",
408 __func__);
409 retval = -ENOMEM;
410 goto error_kfree;
413 fw_priv->timeout.function = firmware_class_timeout;
414 fw_priv->timeout.data = (u_long) fw_priv;
415 init_timer(&fw_priv->timeout);
417 dev_set_name(f_dev, "%s", dev_name(device));
418 f_dev->parent = device;
419 f_dev->class = &firmware_class;
420 dev_set_drvdata(f_dev, fw_priv);
421 dev_set_uevent_suppress(f_dev, 1);
422 retval = device_register(f_dev);
423 if (retval) {
424 dev_err(device, "%s: device_register failed\n", __func__);
425 put_device(f_dev);
426 return retval;
428 *dev_p = f_dev;
429 return 0;
431 error_kfree:
432 kfree(f_dev);
433 kfree(fw_priv);
434 return retval;
437 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
438 const char *fw_name, struct device *device,
439 int uevent)
441 struct device *f_dev;
442 struct firmware_priv *fw_priv;
443 int retval;
445 *dev_p = NULL;
446 retval = fw_register_device(&f_dev, fw_name, device);
447 if (retval)
448 goto out;
450 /* Need to pin this module until class device is destroyed */
451 __module_get(THIS_MODULE);
453 fw_priv = dev_get_drvdata(f_dev);
455 fw_priv->fw = fw;
456 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
457 if (retval) {
458 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
459 goto error_unreg;
462 retval = device_create_file(f_dev, &dev_attr_loading);
463 if (retval) {
464 dev_err(device, "%s: device_create_file failed\n", __func__);
465 goto error_unreg;
468 if (uevent)
469 dev_set_uevent_suppress(f_dev, 0);
470 *dev_p = f_dev;
471 goto out;
473 error_unreg:
474 device_unregister(f_dev);
475 out:
476 return retval;
479 static int
480 _request_firmware(const struct firmware **firmware_p, const char *name,
481 struct device *device, int uevent)
483 struct device *f_dev;
484 struct firmware_priv *fw_priv;
485 struct firmware *firmware;
486 struct builtin_fw *builtin;
487 int retval;
489 if (!firmware_p)
490 return -EINVAL;
492 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
493 if (!firmware) {
494 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
495 __func__);
496 retval = -ENOMEM;
497 goto out;
500 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
501 builtin++) {
502 if (strcmp(name, builtin->name))
503 continue;
504 dev_info(device, "firmware: using built-in firmware %s\n",
505 name);
506 firmware->size = builtin->size;
507 firmware->data = builtin->data;
508 return 0;
511 if (uevent)
512 dev_info(device, "firmware: requesting %s\n", name);
514 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
515 if (retval)
516 goto error_kfree_fw;
518 fw_priv = dev_get_drvdata(f_dev);
520 if (uevent) {
521 if (loading_timeout > 0) {
522 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
523 add_timer(&fw_priv->timeout);
526 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
527 wait_for_completion(&fw_priv->completion);
528 set_bit(FW_STATUS_DONE, &fw_priv->status);
529 del_timer_sync(&fw_priv->timeout);
530 } else
531 wait_for_completion(&fw_priv->completion);
533 mutex_lock(&fw_lock);
534 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
535 retval = -ENOENT;
536 release_firmware(fw_priv->fw);
537 *firmware_p = NULL;
539 fw_priv->fw = NULL;
540 mutex_unlock(&fw_lock);
541 device_unregister(f_dev);
542 goto out;
544 error_kfree_fw:
545 kfree(firmware);
546 *firmware_p = NULL;
547 out:
548 return retval;
552 * request_firmware: - send firmware request and wait for it
553 * @firmware_p: pointer to firmware image
554 * @name: name of firmware file
555 * @device: device for which firmware is being loaded
557 * @firmware_p will be used to return a firmware image by the name
558 * of @name for device @device.
560 * Should be called from user context where sleeping is allowed.
562 * @name will be used as $FIRMWARE in the uevent environment and
563 * should be distinctive enough not to be confused with any other
564 * firmware image for this or any other device.
567 request_firmware(const struct firmware **firmware_p, const char *name,
568 struct device *device)
570 int uevent = 1;
571 return _request_firmware(firmware_p, name, device, uevent);
575 * release_firmware: - release the resource associated with a firmware image
576 * @fw: firmware resource to release
578 void
579 release_firmware(const struct firmware *fw)
581 struct builtin_fw *builtin;
583 if (fw) {
584 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
585 builtin++) {
586 if (fw->data == builtin->data)
587 goto free_fw;
589 firmware_free_data(fw);
590 free_fw:
591 kfree(fw);
595 /* Async support */
596 struct firmware_work {
597 struct work_struct work;
598 struct module *module;
599 const char *name;
600 struct device *device;
601 void *context;
602 void (*cont)(const struct firmware *fw, void *context);
603 int uevent;
606 static int
607 request_firmware_work_func(void *arg)
609 struct firmware_work *fw_work = arg;
610 const struct firmware *fw;
611 int ret;
612 if (!arg) {
613 WARN_ON(1);
614 return 0;
616 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
617 fw_work->uevent);
619 fw_work->cont(fw, fw_work->context);
621 module_put(fw_work->module);
622 kfree(fw_work);
623 return ret;
627 * request_firmware_nowait: asynchronous version of request_firmware
628 * @module: module requesting the firmware
629 * @uevent: sends uevent to copy the firmware image if this flag
630 * is non-zero else the firmware copy must be done manually.
631 * @name: name of firmware file
632 * @device: device for which firmware is being loaded
633 * @gfp: allocation flags
634 * @context: will be passed over to @cont, and
635 * @fw may be %NULL if firmware request fails.
636 * @cont: function will be called asynchronously when the firmware
637 * request is over.
639 * Asynchronous variant of request_firmware() for user contexts where
640 * it is not possible to sleep for long time. It can't be called
641 * in atomic contexts.
644 request_firmware_nowait(
645 struct module *module, int uevent,
646 const char *name, struct device *device, gfp_t gfp, void *context,
647 void (*cont)(const struct firmware *fw, void *context))
649 struct task_struct *task;
650 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
651 gfp);
653 if (!fw_work)
654 return -ENOMEM;
655 if (!try_module_get(module)) {
656 kfree(fw_work);
657 return -EFAULT;
660 *fw_work = (struct firmware_work) {
661 .module = module,
662 .name = name,
663 .device = device,
664 .context = context,
665 .cont = cont,
666 .uevent = uevent,
669 task = kthread_run(request_firmware_work_func, fw_work,
670 "firmware/%s", name);
672 if (IS_ERR(task)) {
673 fw_work->cont(NULL, fw_work->context);
674 module_put(fw_work->module);
675 kfree(fw_work);
676 return PTR_ERR(task);
678 return 0;
681 static int __init
682 firmware_class_init(void)
684 int error;
685 error = class_register(&firmware_class);
686 if (error) {
687 printk(KERN_ERR "%s: class_register failed\n", __func__);
688 return error;
690 error = class_create_file(&firmware_class, &class_attr_timeout);
691 if (error) {
692 printk(KERN_ERR "%s: class_create_file failed\n",
693 __func__);
694 class_unregister(&firmware_class);
696 return error;
699 static void __exit
700 firmware_class_exit(void)
702 class_unregister(&firmware_class);
705 fs_initcall(firmware_class_init);
706 module_exit(firmware_class_exit);
708 EXPORT_SYMBOL(release_firmware);
709 EXPORT_SYMBOL(request_firmware);
710 EXPORT_SYMBOL(request_firmware_nowait);