blob: 4acb2c5733c3b46710d6321b886d16b8704f1d1c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
4 * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org>
5 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
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>
18
19#include <linux/firmware.h>
20#include "base.h"
21
22MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
23MODULE_DESCRIPTION("Multi purpose firmware loading support");
24MODULE_LICENSE("GPL");
25
26enum {
27 FW_STATUS_LOADING,
28 FW_STATUS_DONE,
29 FW_STATUS_ABORT,
30 FW_STATUS_READY,
Abhay Salunke6e3eaab2005-09-06 15:17:13 -070031 FW_STATUS_READY_NOHOTPLUG,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
34static int loading_timeout = 10; /* In seconds */
35
36/* fw_lock could be moved to 'struct firmware_priv' but since it is just
37 * guarding for corner cases a global lock should be OK */
38static DECLARE_MUTEX(fw_lock);
39
40struct firmware_priv {
41 char fw_id[FIRMWARE_NAME_MAX];
42 struct completion completion;
43 struct bin_attribute attr_data;
44 struct firmware *fw;
45 unsigned long status;
46 int alloc_size;
47 struct timer_list timeout;
48};
49
50static inline void
51fw_load_abort(struct firmware_priv *fw_priv)
52{
53 set_bit(FW_STATUS_ABORT, &fw_priv->status);
54 wmb();
55 complete(&fw_priv->completion);
56}
57
58static ssize_t
59firmware_timeout_show(struct class *class, char *buf)
60{
61 return sprintf(buf, "%d\n", loading_timeout);
62}
63
64/**
65 * firmware_timeout_store:
66 * Description:
67 * Sets the number of seconds to wait for the firmware. Once
68 * this expires an error will be return to the driver and no
69 * firmware will be provided.
70 *
71 * Note: zero means 'wait for ever'
72 *
73 **/
74static ssize_t
75firmware_timeout_store(struct class *class, const char *buf, size_t count)
76{
77 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -070078 if (loading_timeout < 0)
79 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return count;
81}
82
83static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
84
85static void fw_class_dev_release(struct class_device *class_dev);
86int firmware_class_hotplug(struct class_device *dev, char **envp,
87 int num_envp, char *buffer, int buffer_size);
88
89static struct class firmware_class = {
90 .name = "firmware",
91 .hotplug = firmware_class_hotplug,
92 .release = fw_class_dev_release,
93};
94
95int
96firmware_class_hotplug(struct class_device *class_dev, char **envp,
97 int num_envp, char *buffer, int buffer_size)
98{
99 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
100 int i = 0, len = 0;
101
102 if (!test_bit(FW_STATUS_READY, &fw_priv->status))
103 return -ENODEV;
104
105 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
106 "FIRMWARE=%s", fw_priv->fw_id))
107 return -ENOMEM;
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700108 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
109 "TIMEOUT=%i", loading_timeout))
110 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 envp[i] = NULL;
113
114 return 0;
115}
116
117static ssize_t
118firmware_loading_show(struct class_device *class_dev, char *buf)
119{
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);
123}
124
125/**
126 * firmware_loading_store: - loading control file
127 * Description:
128 * The relevant values are:
129 *
130 * 1: Start a load, discarding any previous partial load.
131 * 0: Conclude the load and handle the data to the driver code.
132 * -1: Conclude the load with an error and discard any written data.
133 **/
134static ssize_t
135firmware_loading_store(struct class_device *class_dev,
136 const char *buf, size_t count)
137{
138 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
139 int loading = simple_strtol(buf, NULL, 10);
140
141 switch (loading) {
142 case 1:
143 down(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700144 if (!fw_priv->fw) {
145 up(&fw_lock);
146 break;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 vfree(fw_priv->fw->data);
149 fw_priv->fw->data = NULL;
150 fw_priv->fw->size = 0;
151 fw_priv->alloc_size = 0;
152 set_bit(FW_STATUS_LOADING, &fw_priv->status);
153 up(&fw_lock);
154 break;
155 case 0:
156 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
157 complete(&fw_priv->completion);
158 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
159 break;
160 }
161 /* fallthrough */
162 default:
163 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
164 loading);
165 /* fallthrough */
166 case -1:
167 fw_load_abort(fw_priv);
168 break;
169 }
170
171 return count;
172}
173
174static CLASS_DEVICE_ATTR(loading, 0644,
175 firmware_loading_show, firmware_loading_store);
176
177static ssize_t
178firmware_data_read(struct kobject *kobj,
179 char *buffer, loff_t offset, size_t count)
180{
181 struct class_device *class_dev = to_class_dev(kobj);
182 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
183 struct firmware *fw;
184 ssize_t ret_count = count;
185
186 down(&fw_lock);
187 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700188 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 ret_count = -ENODEV;
190 goto out;
191 }
192 if (offset > fw->size) {
193 ret_count = 0;
194 goto out;
195 }
196 if (offset + ret_count > fw->size)
197 ret_count = fw->size - offset;
198
199 memcpy(buffer, fw->data + offset, ret_count);
200out:
201 up(&fw_lock);
202 return ret_count;
203}
204static int
205fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
206{
207 u8 *new_data;
208
209 if (min_size <= fw_priv->alloc_size)
210 return 0;
211
212 new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
213 if (!new_data) {
214 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
215 /* Make sure that we don't keep incomplete data */
216 fw_load_abort(fw_priv);
217 return -ENOMEM;
218 }
219 fw_priv->alloc_size += PAGE_SIZE;
220 if (fw_priv->fw->data) {
221 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
222 vfree(fw_priv->fw->data);
223 }
224 fw_priv->fw->data = new_data;
225 BUG_ON(min_size > fw_priv->alloc_size);
226 return 0;
227}
228
229/**
230 * firmware_data_write:
231 *
232 * Description:
233 *
234 * Data written to the 'data' attribute will be later handled to
235 * the driver as a firmware image.
236 **/
237static ssize_t
238firmware_data_write(struct kobject *kobj,
239 char *buffer, loff_t offset, size_t count)
240{
241 struct class_device *class_dev = to_class_dev(kobj);
242 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
243 struct firmware *fw;
244 ssize_t retval;
245
246 if (!capable(CAP_SYS_RAWIO))
247 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 down(&fw_lock);
250 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700251 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 retval = -ENODEV;
253 goto out;
254 }
255 retval = fw_realloc_buffer(fw_priv, offset + count);
256 if (retval)
257 goto out;
258
259 memcpy(fw->data + offset, buffer, count);
260
261 fw->size = max_t(size_t, offset + count, fw->size);
262 retval = count;
263out:
264 up(&fw_lock);
265 return retval;
266}
267static struct bin_attribute firmware_attr_data_tmpl = {
268 .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
269 .size = 0,
270 .read = firmware_data_read,
271 .write = firmware_data_write,
272};
273
274static void
275fw_class_dev_release(struct class_device *class_dev)
276{
277 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
278
279 kfree(fw_priv);
280 kfree(class_dev);
281
282 module_put(THIS_MODULE);
283}
284
285static void
286firmware_class_timeout(u_long data)
287{
288 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
289 fw_load_abort(fw_priv);
290}
291
292static inline void
293fw_setup_class_device_id(struct class_device *class_dev, struct device *dev)
294{
295 /* XXX warning we should watch out for name collisions */
296 strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE);
297}
298
299static int
300fw_register_class_device(struct class_device **class_dev_p,
301 const char *fw_name, struct device *device)
302{
303 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700304 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 GFP_KERNEL);
Jiri Slaby4aed0642005-09-13 01:25:01 -0700306 struct class_device *class_dev = kzalloc(sizeof(*class_dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 GFP_KERNEL);
308
309 *class_dev_p = NULL;
310
311 if (!fw_priv || !class_dev) {
312 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
313 retval = -ENOMEM;
314 goto error_kfree;
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 init_completion(&fw_priv->completion);
318 fw_priv->attr_data = firmware_attr_data_tmpl;
319 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
320
321 fw_priv->timeout.function = firmware_class_timeout;
322 fw_priv->timeout.data = (u_long) fw_priv;
323 init_timer(&fw_priv->timeout);
324
325 fw_setup_class_device_id(class_dev, device);
326 class_dev->dev = device;
327 class_dev->class = &firmware_class;
328 class_set_devdata(class_dev, fw_priv);
329 retval = class_device_register(class_dev);
330 if (retval) {
331 printk(KERN_ERR "%s: class_device_register failed\n",
332 __FUNCTION__);
333 goto error_kfree;
334 }
335 *class_dev_p = class_dev;
336 return 0;
337
338error_kfree:
339 kfree(fw_priv);
340 kfree(class_dev);
341 return retval;
342}
343
344static int
345fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700346 const char *fw_name, struct device *device, int hotplug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct class_device *class_dev;
349 struct firmware_priv *fw_priv;
350 int retval;
351
352 *class_dev_p = NULL;
353 retval = fw_register_class_device(&class_dev, fw_name, device);
354 if (retval)
355 goto out;
356
357 /* Need to pin this module until class device is destroyed */
358 __module_get(THIS_MODULE);
359
360 fw_priv = class_get_devdata(class_dev);
361
362 fw_priv->fw = fw;
363 retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data);
364 if (retval) {
365 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
366 __FUNCTION__);
367 goto error_unreg;
368 }
369
370 retval = class_device_create_file(class_dev,
371 &class_device_attr_loading);
372 if (retval) {
373 printk(KERN_ERR "%s: class_device_create_file failed\n",
374 __FUNCTION__);
375 goto error_unreg;
376 }
377
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700378 if (hotplug)
379 set_bit(FW_STATUS_READY, &fw_priv->status);
380 else
381 set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 *class_dev_p = class_dev;
383 goto out;
384
385error_unreg:
386 class_device_unregister(class_dev);
387out:
388 return retval;
389}
390
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700391static int
392_request_firmware(const struct firmware **firmware_p, const char *name,
393 struct device *device, int hotplug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct class_device *class_dev;
396 struct firmware_priv *fw_priv;
397 struct firmware *firmware;
398 int retval;
399
400 if (!firmware_p)
401 return -EINVAL;
402
Jiri Slaby4aed0642005-09-13 01:25:01 -0700403 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!firmware) {
405 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
406 __FUNCTION__);
407 retval = -ENOMEM;
408 goto out;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700411 retval = fw_setup_class_device(firmware, &class_dev, name, device,
412 hotplug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (retval)
414 goto error_kfree_fw;
415
416 fw_priv = class_get_devdata(class_dev);
417
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700418 if (hotplug) {
419 if (loading_timeout > 0) {
420 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
421 add_timer(&fw_priv->timeout);
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700424 kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
425 wait_for_completion(&fw_priv->completion);
426 set_bit(FW_STATUS_DONE, &fw_priv->status);
427 del_timer_sync(&fw_priv->timeout);
428 } else
429 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
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;
436 }
437 fw_priv->fw = NULL;
438 up(&fw_lock);
439 class_device_unregister(class_dev);
440 goto out;
441
442error_kfree_fw:
443 kfree(firmware);
444 *firmware_p = NULL;
445out:
446 return retval;
447}
448
449/**
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700450 * request_firmware: - request firmware to hotplug and wait for it
451 * Description:
452 * @firmware will be used to return a firmware image by the name
453 * of @name for device @device.
454 *
455 * Should be called from user context where sleeping is allowed.
456 *
457 * @name will be use as $FIRMWARE in the hotplug environment and
458 * should be distinctive enough not to be confused with any other
459 * firmware image for this or any other device.
460 **/
461int
462request_firmware(const struct firmware **firmware_p, const char *name,
463 struct device *device)
464{
465 int hotplug = 1;
466 return _request_firmware(firmware_p, name, device, hotplug);
467}
468
469/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * release_firmware: - release the resource associated with a firmware image
471 **/
472void
473release_firmware(const struct firmware *fw)
474{
475 if (fw) {
476 vfree(fw->data);
477 kfree(fw);
478 }
479}
480
481/**
482 * register_firmware: - provide a firmware image for later usage
483 *
484 * Description:
485 * Make sure that @data will be available by requesting firmware @name.
486 *
487 * Note: This will not be possible until some kind of persistence
488 * is available.
489 **/
490void
491register_firmware(const char *name, const u8 *data, size_t size)
492{
493 /* This is meaningless without firmware caching, so until we
494 * decide if firmware caching is reasonable just leave it as a
495 * noop */
496}
497
498/* Async support */
499struct firmware_work {
500 struct work_struct work;
501 struct module *module;
502 const char *name;
503 struct device *device;
504 void *context;
505 void (*cont)(const struct firmware *fw, void *context);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700506 int hotplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507};
508
509static int
510request_firmware_work_func(void *arg)
511{
512 struct firmware_work *fw_work = arg;
513 const struct firmware *fw;
514 if (!arg) {
515 WARN_ON(1);
516 return 0;
517 }
518 daemonize("%s/%s", "firmware", fw_work->name);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700519 _request_firmware(&fw, fw_work->name, fw_work->device,
520 fw_work->hotplug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 fw_work->cont(fw, fw_work->context);
522 release_firmware(fw);
523 module_put(fw_work->module);
524 kfree(fw_work);
525 return 0;
526}
527
528/**
529 * request_firmware_nowait:
530 *
531 * Description:
532 * Asynchronous variant of request_firmware() for contexts where
533 * it is not possible to sleep.
534 *
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700535 * @hotplug invokes hotplug event to copy the firmware image if this flag
536 * is non-zero else the firmware copy must be done manually.
537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * @cont will be called asynchronously when the firmware request is over.
539 *
540 * @context will be passed over to @cont.
541 *
542 * @fw may be %NULL if firmware request fails.
543 *
544 **/
545int
546request_firmware_nowait(
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700547 struct module *module, int hotplug,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 const char *name, struct device *device, void *context,
549 void (*cont)(const struct firmware *fw, void *context))
550{
551 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
552 GFP_ATOMIC);
553 int ret;
554
555 if (!fw_work)
556 return -ENOMEM;
557 if (!try_module_get(module)) {
558 kfree(fw_work);
559 return -EFAULT;
560 }
561
562 *fw_work = (struct firmware_work) {
563 .module = module,
564 .name = name,
565 .device = device,
566 .context = context,
567 .cont = cont,
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700568 .hotplug = hotplug,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 };
570
571 ret = kernel_thread(request_firmware_work_func, fw_work,
572 CLONE_FS | CLONE_FILES);
573
574 if (ret < 0) {
575 fw_work->cont(NULL, fw_work->context);
576 return ret;
577 }
578 return 0;
579}
580
581static int __init
582firmware_class_init(void)
583{
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;
589 }
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);
595 }
596 return error;
597
598}
599static void __exit
600firmware_class_exit(void)
601{
602 class_unregister(&firmware_class);
603}
604
605module_init(firmware_class_init);
606module_exit(firmware_class_exit);
607
608EXPORT_SYMBOL(release_firmware);
609EXPORT_SYMBOL(request_firmware);
610EXPORT_SYMBOL(request_firmware_nowait);
611EXPORT_SYMBOL(register_firmware);