Make sure omap cpufreq driver initializes after cpufreq framework and governors
[linux-ginger.git] / drivers / base / firmware_class.c
blob7376367bcb8053dec4951945f916d47c882fd59a
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 /* Some architectures don't have PAGE_KERNEL_RO */
129 #ifndef PAGE_KERNEL_RO
130 #define PAGE_KERNEL_RO PAGE_KERNEL
131 #endif
133 * firmware_loading_store - set value in the 'loading' control file
134 * @dev: device pointer
135 * @attr: device attribute pointer
136 * @buf: buffer to scan for loading control value
137 * @count: number of bytes in @buf
139 * The relevant values are:
141 * 1: Start a load, discarding any previous partial load.
142 * 0: Conclude the load and hand the data to the driver code.
143 * -1: Conclude the load with an error and discard any written data.
145 static ssize_t firmware_loading_store(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf, size_t count)
149 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
150 int loading = simple_strtol(buf, NULL, 10);
151 int i;
153 switch (loading) {
154 case 1:
155 mutex_lock(&fw_lock);
156 if (!fw_priv->fw) {
157 mutex_unlock(&fw_lock);
158 break;
160 vfree(fw_priv->fw->data);
161 fw_priv->fw->data = NULL;
162 for (i = 0; i < fw_priv->nr_pages; i++)
163 __free_page(fw_priv->pages[i]);
164 kfree(fw_priv->pages);
165 fw_priv->pages = NULL;
166 fw_priv->page_array_size = 0;
167 fw_priv->nr_pages = 0;
168 fw_priv->fw->size = 0;
169 set_bit(FW_STATUS_LOADING, &fw_priv->status);
170 mutex_unlock(&fw_lock);
171 break;
172 case 0:
173 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
174 vfree(fw_priv->fw->data);
175 fw_priv->fw->data = vmap(fw_priv->pages,
176 fw_priv->nr_pages,
177 0, PAGE_KERNEL_RO);
178 if (!fw_priv->fw->data) {
179 dev_err(dev, "%s: vmap() failed\n", __func__);
180 goto err;
182 /* Pages will be freed by vfree() */
183 fw_priv->page_array_size = 0;
184 fw_priv->nr_pages = 0;
185 complete(&fw_priv->completion);
186 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
187 break;
189 /* fallthrough */
190 default:
191 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
192 /* fallthrough */
193 case -1:
194 err:
195 fw_load_abort(fw_priv);
196 break;
199 return count;
202 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
204 static ssize_t
205 firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
206 char *buffer, loff_t offset, size_t count)
208 struct device *dev = to_dev(kobj);
209 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
210 struct firmware *fw;
211 ssize_t ret_count;
213 mutex_lock(&fw_lock);
214 fw = fw_priv->fw;
215 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
216 ret_count = -ENODEV;
217 goto out;
219 if (offset > fw->size) {
220 ret_count = 0;
221 goto out;
223 if (count > fw->size - offset)
224 count = fw->size - offset;
226 ret_count = count;
228 while (count) {
229 void *page_data;
230 int page_nr = offset >> PAGE_SHIFT;
231 int page_ofs = offset & (PAGE_SIZE-1);
232 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
234 page_data = kmap(fw_priv->pages[page_nr]);
236 memcpy(buffer, page_data + page_ofs, page_cnt);
238 kunmap(fw_priv->pages[page_nr]);
239 buffer += page_cnt;
240 offset += page_cnt;
241 count -= page_cnt;
243 out:
244 mutex_unlock(&fw_lock);
245 return ret_count;
248 static int
249 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
251 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
253 /* If the array of pages is too small, grow it... */
254 if (fw_priv->page_array_size < pages_needed) {
255 int new_array_size = max(pages_needed,
256 fw_priv->page_array_size * 2);
257 struct page **new_pages;
259 new_pages = kmalloc(new_array_size * sizeof(void *),
260 GFP_KERNEL);
261 if (!new_pages) {
262 fw_load_abort(fw_priv);
263 return -ENOMEM;
265 memcpy(new_pages, fw_priv->pages,
266 fw_priv->page_array_size * sizeof(void *));
267 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
268 (new_array_size - fw_priv->page_array_size));
269 kfree(fw_priv->pages);
270 fw_priv->pages = new_pages;
271 fw_priv->page_array_size = new_array_size;
274 while (fw_priv->nr_pages < pages_needed) {
275 fw_priv->pages[fw_priv->nr_pages] =
276 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
278 if (!fw_priv->pages[fw_priv->nr_pages]) {
279 fw_load_abort(fw_priv);
280 return -ENOMEM;
282 fw_priv->nr_pages++;
284 return 0;
288 * firmware_data_write - write method for firmware
289 * @kobj: kobject for the device
290 * @bin_attr: bin_attr structure
291 * @buffer: buffer being written
292 * @offset: buffer offset for write in total data store area
293 * @count: buffer size
295 * Data written to the 'data' attribute will be later handed to
296 * the driver as a firmware image.
298 static ssize_t
299 firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
300 char *buffer, loff_t offset, size_t count)
302 struct device *dev = to_dev(kobj);
303 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
304 struct firmware *fw;
305 ssize_t retval;
307 if (!capable(CAP_SYS_RAWIO))
308 return -EPERM;
310 mutex_lock(&fw_lock);
311 fw = fw_priv->fw;
312 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
313 retval = -ENODEV;
314 goto out;
316 retval = fw_realloc_buffer(fw_priv, offset + count);
317 if (retval)
318 goto out;
320 retval = count;
322 while (count) {
323 void *page_data;
324 int page_nr = offset >> PAGE_SHIFT;
325 int page_ofs = offset & (PAGE_SIZE - 1);
326 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
328 page_data = kmap(fw_priv->pages[page_nr]);
330 memcpy(page_data + page_ofs, buffer, page_cnt);
332 kunmap(fw_priv->pages[page_nr]);
333 buffer += page_cnt;
334 offset += page_cnt;
335 count -= page_cnt;
338 fw->size = max_t(size_t, offset, fw->size);
339 out:
340 mutex_unlock(&fw_lock);
341 return retval;
344 static struct bin_attribute firmware_attr_data_tmpl = {
345 .attr = {.name = "data", .mode = 0644},
346 .size = 0,
347 .read = firmware_data_read,
348 .write = firmware_data_write,
351 static void fw_dev_release(struct device *dev)
353 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
354 int i;
356 for (i = 0; i < fw_priv->nr_pages; i++)
357 __free_page(fw_priv->pages[i]);
358 kfree(fw_priv->pages);
359 kfree(fw_priv->fw_id);
360 kfree(fw_priv);
361 kfree(dev);
363 module_put(THIS_MODULE);
366 static void
367 firmware_class_timeout(u_long data)
369 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
370 fw_load_abort(fw_priv);
373 static int fw_register_device(struct device **dev_p, const char *fw_name,
374 struct device *device)
376 int retval;
377 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
378 GFP_KERNEL);
379 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
381 *dev_p = NULL;
383 if (!fw_priv || !f_dev) {
384 dev_err(device, "%s: kmalloc failed\n", __func__);
385 retval = -ENOMEM;
386 goto error_kfree;
389 init_completion(&fw_priv->completion);
390 fw_priv->attr_data = firmware_attr_data_tmpl;
391 fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
392 if (!fw_priv->fw_id) {
393 dev_err(device, "%s: Firmware name allocation failed\n",
394 __func__);
395 retval = -ENOMEM;
396 goto error_kfree;
399 fw_priv->timeout.function = firmware_class_timeout;
400 fw_priv->timeout.data = (u_long) fw_priv;
401 init_timer(&fw_priv->timeout);
403 dev_set_name(f_dev, "%s", dev_name(device));
404 f_dev->parent = device;
405 f_dev->class = &firmware_class;
406 dev_set_drvdata(f_dev, fw_priv);
407 dev_set_uevent_suppress(f_dev, 1);
408 retval = device_register(f_dev);
409 if (retval) {
410 dev_err(device, "%s: device_register failed\n", __func__);
411 put_device(f_dev);
412 return retval;
414 *dev_p = f_dev;
415 return 0;
417 error_kfree:
418 kfree(f_dev);
419 kfree(fw_priv);
420 return retval;
423 static int fw_setup_device(struct firmware *fw, struct device **dev_p,
424 const char *fw_name, struct device *device,
425 int uevent)
427 struct device *f_dev;
428 struct firmware_priv *fw_priv;
429 int retval;
431 *dev_p = NULL;
432 retval = fw_register_device(&f_dev, fw_name, device);
433 if (retval)
434 goto out;
436 /* Need to pin this module until class device is destroyed */
437 __module_get(THIS_MODULE);
439 fw_priv = dev_get_drvdata(f_dev);
441 fw_priv->fw = fw;
442 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
443 if (retval) {
444 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
445 goto error_unreg;
448 retval = device_create_file(f_dev, &dev_attr_loading);
449 if (retval) {
450 dev_err(device, "%s: device_create_file failed\n", __func__);
451 goto error_unreg;
454 if (uevent)
455 dev_set_uevent_suppress(f_dev, 0);
456 *dev_p = f_dev;
457 goto out;
459 error_unreg:
460 device_unregister(f_dev);
461 out:
462 return retval;
465 static int
466 _request_firmware(const struct firmware **firmware_p, const char *name,
467 struct device *device, int uevent)
469 struct device *f_dev;
470 struct firmware_priv *fw_priv;
471 struct firmware *firmware;
472 struct builtin_fw *builtin;
473 int retval;
475 if (!firmware_p)
476 return -EINVAL;
478 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
479 if (!firmware) {
480 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
481 __func__);
482 retval = -ENOMEM;
483 goto out;
486 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
487 builtin++) {
488 if (strcmp(name, builtin->name))
489 continue;
490 dev_info(device, "firmware: using built-in firmware %s\n",
491 name);
492 firmware->size = builtin->size;
493 firmware->data = builtin->data;
494 return 0;
497 if (uevent)
498 dev_info(device, "firmware: requesting %s\n", name);
500 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
501 if (retval)
502 goto error_kfree_fw;
504 fw_priv = dev_get_drvdata(f_dev);
506 if (uevent) {
507 if (loading_timeout > 0) {
508 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
509 add_timer(&fw_priv->timeout);
512 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
513 wait_for_completion(&fw_priv->completion);
514 set_bit(FW_STATUS_DONE, &fw_priv->status);
515 del_timer_sync(&fw_priv->timeout);
516 } else
517 wait_for_completion(&fw_priv->completion);
519 mutex_lock(&fw_lock);
520 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
521 retval = -ENOENT;
522 release_firmware(fw_priv->fw);
523 *firmware_p = NULL;
525 fw_priv->fw = NULL;
526 mutex_unlock(&fw_lock);
527 device_unregister(f_dev);
528 goto out;
530 error_kfree_fw:
531 kfree(firmware);
532 *firmware_p = NULL;
533 out:
534 return retval;
538 * request_firmware: - send firmware request and wait for it
539 * @firmware_p: pointer to firmware image
540 * @name: name of firmware file
541 * @device: device for which firmware is being loaded
543 * @firmware_p will be used to return a firmware image by the name
544 * of @name for device @device.
546 * Should be called from user context where sleeping is allowed.
548 * @name will be used as $FIRMWARE in the uevent environment and
549 * should be distinctive enough not to be confused with any other
550 * firmware image for this or any other device.
553 request_firmware(const struct firmware **firmware_p, const char *name,
554 struct device *device)
556 int uevent = 1;
557 return _request_firmware(firmware_p, name, device, uevent);
561 * release_firmware: - release the resource associated with a firmware image
562 * @fw: firmware resource to release
564 void
565 release_firmware(const struct firmware *fw)
567 struct builtin_fw *builtin;
569 if (fw) {
570 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
571 builtin++) {
572 if (fw->data == builtin->data)
573 goto free_fw;
575 vfree(fw->data);
576 free_fw:
577 kfree(fw);
581 /* Async support */
582 struct firmware_work {
583 struct work_struct work;
584 struct module *module;
585 const char *name;
586 struct device *device;
587 void *context;
588 void (*cont)(const struct firmware *fw, void *context);
589 int uevent;
592 static int
593 request_firmware_work_func(void *arg)
595 struct firmware_work *fw_work = arg;
596 const struct firmware *fw;
597 int ret;
598 if (!arg) {
599 WARN_ON(1);
600 return 0;
602 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
603 fw_work->uevent);
604 if (ret < 0)
605 fw_work->cont(NULL, fw_work->context);
606 else {
607 fw_work->cont(fw, fw_work->context);
608 release_firmware(fw);
610 module_put(fw_work->module);
611 kfree(fw_work);
612 return ret;
616 * request_firmware_nowait: asynchronous version of request_firmware
617 * @module: module requesting the firmware
618 * @uevent: sends uevent to copy the firmware image if this flag
619 * is non-zero else the firmware copy must be done manually.
620 * @name: name of firmware file
621 * @device: device for which firmware is being loaded
622 * @context: will be passed over to @cont, and
623 * @fw may be %NULL if firmware request fails.
624 * @cont: function will be called asynchronously when the firmware
625 * request is over.
627 * Asynchronous variant of request_firmware() for user contexts where
628 * it is not possible to sleep for long time. It can't be called
629 * in atomic contexts.
632 request_firmware_nowait(
633 struct module *module, int uevent,
634 const char *name, struct device *device, void *context,
635 void (*cont)(const struct firmware *fw, void *context))
637 struct task_struct *task;
638 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
639 GFP_ATOMIC);
641 if (!fw_work)
642 return -ENOMEM;
643 if (!try_module_get(module)) {
644 kfree(fw_work);
645 return -EFAULT;
648 *fw_work = (struct firmware_work) {
649 .module = module,
650 .name = name,
651 .device = device,
652 .context = context,
653 .cont = cont,
654 .uevent = uevent,
657 task = kthread_run(request_firmware_work_func, fw_work,
658 "firmware/%s", name);
660 if (IS_ERR(task)) {
661 fw_work->cont(NULL, fw_work->context);
662 module_put(fw_work->module);
663 kfree(fw_work);
664 return PTR_ERR(task);
666 return 0;
669 static int __init
670 firmware_class_init(void)
672 int error;
673 error = class_register(&firmware_class);
674 if (error) {
675 printk(KERN_ERR "%s: class_register failed\n", __func__);
676 return error;
678 error = class_create_file(&firmware_class, &class_attr_timeout);
679 if (error) {
680 printk(KERN_ERR "%s: class_create_file failed\n",
681 __func__);
682 class_unregister(&firmware_class);
684 return error;
687 static void __exit
688 firmware_class_exit(void)
690 class_unregister(&firmware_class);
693 fs_initcall(firmware_class_init);
694 module_exit(firmware_class_exit);
696 EXPORT_SYMBOL(release_firmware);
697 EXPORT_SYMBOL(request_firmware);
698 EXPORT_SYMBOL(request_firmware_nowait);