blob: 97fe13f7f07c85d6fa2d07efd3f04afab932ec46 [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,
31};
32
33static int loading_timeout = 10; /* In seconds */
34
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 */
37static DECLARE_MUTEX(fw_lock);
38
39struct 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;
47};
48
49static inline void
50fw_load_abort(struct firmware_priv *fw_priv)
51{
52 set_bit(FW_STATUS_ABORT, &fw_priv->status);
53 wmb();
54 complete(&fw_priv->completion);
55}
56
57static ssize_t
58firmware_timeout_show(struct class *class, char *buf)
59{
60 return sprintf(buf, "%d\n", loading_timeout);
61}
62
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.
69 *
70 * Note: zero means 'wait for ever'
71 *
72 **/
73static ssize_t
74firmware_timeout_store(struct class *class, const char *buf, size_t count)
75{
76 loading_timeout = simple_strtol(buf, NULL, 10);
77 return count;
78}
79
80static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
81
82static void fw_class_dev_release(struct class_device *class_dev);
83int firmware_class_hotplug(struct class_device *dev, char **envp,
84 int num_envp, char *buffer, int buffer_size);
85
86static struct class firmware_class = {
87 .name = "firmware",
88 .hotplug = firmware_class_hotplug,
89 .release = fw_class_dev_release,
90};
91
92int
93firmware_class_hotplug(struct class_device *class_dev, char **envp,
94 int num_envp, char *buffer, int buffer_size)
95{
96 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
97 int i = 0, len = 0;
98
99 if (!test_bit(FW_STATUS_READY, &fw_priv->status))
100 return -ENODEV;
101
102 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
103 "FIRMWARE=%s", fw_priv->fw_id))
104 return -ENOMEM;
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700105 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
106 "TIMEOUT=%i", loading_timeout))
107 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 envp[i] = NULL;
110
111 return 0;
112}
113
114static ssize_t
115firmware_loading_show(struct class_device *class_dev, char *buf)
116{
117 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
118 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
119 return sprintf(buf, "%d\n", loading);
120}
121
122/**
123 * firmware_loading_store: - loading control file
124 * Description:
125 * The relevant values are:
126 *
127 * 1: Start a load, discarding any previous partial load.
128 * 0: Conclude the load and handle the data to the driver code.
129 * -1: Conclude the load with an error and discard any written data.
130 **/
131static ssize_t
132firmware_loading_store(struct class_device *class_dev,
133 const char *buf, size_t count)
134{
135 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
136 int loading = simple_strtol(buf, NULL, 10);
137
138 switch (loading) {
139 case 1:
140 down(&fw_lock);
141 vfree(fw_priv->fw->data);
142 fw_priv->fw->data = NULL;
143 fw_priv->fw->size = 0;
144 fw_priv->alloc_size = 0;
145 set_bit(FW_STATUS_LOADING, &fw_priv->status);
146 up(&fw_lock);
147 break;
148 case 0:
149 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
150 complete(&fw_priv->completion);
151 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
152 break;
153 }
154 /* fallthrough */
155 default:
156 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
157 loading);
158 /* fallthrough */
159 case -1:
160 fw_load_abort(fw_priv);
161 break;
162 }
163
164 return count;
165}
166
167static CLASS_DEVICE_ATTR(loading, 0644,
168 firmware_loading_show, firmware_loading_store);
169
170static ssize_t
171firmware_data_read(struct kobject *kobj,
172 char *buffer, loff_t offset, size_t count)
173{
174 struct class_device *class_dev = to_class_dev(kobj);
175 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
176 struct firmware *fw;
177 ssize_t ret_count = count;
178
179 down(&fw_lock);
180 fw = fw_priv->fw;
181 if (test_bit(FW_STATUS_DONE, &fw_priv->status)) {
182 ret_count = -ENODEV;
183 goto out;
184 }
185 if (offset > fw->size) {
186 ret_count = 0;
187 goto out;
188 }
189 if (offset + ret_count > fw->size)
190 ret_count = fw->size - offset;
191
192 memcpy(buffer, fw->data + offset, ret_count);
193out:
194 up(&fw_lock);
195 return ret_count;
196}
197static int
198fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
199{
200 u8 *new_data;
201
202 if (min_size <= fw_priv->alloc_size)
203 return 0;
204
205 new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
206 if (!new_data) {
207 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
208 /* Make sure that we don't keep incomplete data */
209 fw_load_abort(fw_priv);
210 return -ENOMEM;
211 }
212 fw_priv->alloc_size += PAGE_SIZE;
213 if (fw_priv->fw->data) {
214 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
215 vfree(fw_priv->fw->data);
216 }
217 fw_priv->fw->data = new_data;
218 BUG_ON(min_size > fw_priv->alloc_size);
219 return 0;
220}
221
222/**
223 * firmware_data_write:
224 *
225 * Description:
226 *
227 * Data written to the 'data' attribute will be later handled to
228 * the driver as a firmware image.
229 **/
230static ssize_t
231firmware_data_write(struct kobject *kobj,
232 char *buffer, loff_t offset, size_t count)
233{
234 struct class_device *class_dev = to_class_dev(kobj);
235 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
236 struct firmware *fw;
237 ssize_t retval;
238
239 if (!capable(CAP_SYS_RAWIO))
240 return -EPERM;
241 down(&fw_lock);
242 fw = fw_priv->fw;
243 if (test_bit(FW_STATUS_DONE, &fw_priv->status)) {
244 retval = -ENODEV;
245 goto out;
246 }
247 retval = fw_realloc_buffer(fw_priv, offset + count);
248 if (retval)
249 goto out;
250
251 memcpy(fw->data + offset, buffer, count);
252
253 fw->size = max_t(size_t, offset + count, fw->size);
254 retval = count;
255out:
256 up(&fw_lock);
257 return retval;
258}
259static struct bin_attribute firmware_attr_data_tmpl = {
260 .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
261 .size = 0,
262 .read = firmware_data_read,
263 .write = firmware_data_write,
264};
265
266static void
267fw_class_dev_release(struct class_device *class_dev)
268{
269 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
270
271 kfree(fw_priv);
272 kfree(class_dev);
273
274 module_put(THIS_MODULE);
275}
276
277static void
278firmware_class_timeout(u_long data)
279{
280 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
281 fw_load_abort(fw_priv);
282}
283
284static inline void
285fw_setup_class_device_id(struct class_device *class_dev, struct device *dev)
286{
287 /* XXX warning we should watch out for name collisions */
288 strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE);
289}
290
291static int
292fw_register_class_device(struct class_device **class_dev_p,
293 const char *fw_name, struct device *device)
294{
295 int retval;
296 struct firmware_priv *fw_priv = kmalloc(sizeof (struct firmware_priv),
297 GFP_KERNEL);
298 struct class_device *class_dev = kmalloc(sizeof (struct class_device),
299 GFP_KERNEL);
300
301 *class_dev_p = NULL;
302
303 if (!fw_priv || !class_dev) {
304 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
305 retval = -ENOMEM;
306 goto error_kfree;
307 }
308 memset(fw_priv, 0, sizeof (*fw_priv));
309 memset(class_dev, 0, sizeof (*class_dev));
310
311 init_completion(&fw_priv->completion);
312 fw_priv->attr_data = firmware_attr_data_tmpl;
313 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
314
315 fw_priv->timeout.function = firmware_class_timeout;
316 fw_priv->timeout.data = (u_long) fw_priv;
317 init_timer(&fw_priv->timeout);
318
319 fw_setup_class_device_id(class_dev, device);
320 class_dev->dev = device;
321 class_dev->class = &firmware_class;
322 class_set_devdata(class_dev, fw_priv);
323 retval = class_device_register(class_dev);
324 if (retval) {
325 printk(KERN_ERR "%s: class_device_register failed\n",
326 __FUNCTION__);
327 goto error_kfree;
328 }
329 *class_dev_p = class_dev;
330 return 0;
331
332error_kfree:
333 kfree(fw_priv);
334 kfree(class_dev);
335 return retval;
336}
337
338static int
339fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
340 const char *fw_name, struct device *device)
341{
342 struct class_device *class_dev;
343 struct firmware_priv *fw_priv;
344 int retval;
345
346 *class_dev_p = NULL;
347 retval = fw_register_class_device(&class_dev, fw_name, device);
348 if (retval)
349 goto out;
350
351 /* Need to pin this module until class device is destroyed */
352 __module_get(THIS_MODULE);
353
354 fw_priv = class_get_devdata(class_dev);
355
356 fw_priv->fw = fw;
357 retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data);
358 if (retval) {
359 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
360 __FUNCTION__);
361 goto error_unreg;
362 }
363
364 retval = class_device_create_file(class_dev,
365 &class_device_attr_loading);
366 if (retval) {
367 printk(KERN_ERR "%s: class_device_create_file failed\n",
368 __FUNCTION__);
369 goto error_unreg;
370 }
371
372 set_bit(FW_STATUS_READY, &fw_priv->status);
373 *class_dev_p = class_dev;
374 goto out;
375
376error_unreg:
377 class_device_unregister(class_dev);
378out:
379 return retval;
380}
381
382/**
383 * request_firmware: - request firmware to hotplug and wait for it
384 * Description:
385 * @firmware will be used to return a firmware image by the name
386 * of @name for device @device.
387 *
388 * Should be called from user context where sleeping is allowed.
389 *
390 * @name will be use as $FIRMWARE in the hotplug environment and
391 * should be distinctive enough not to be confused with any other
392 * firmware image for this or any other device.
393 **/
394int
395request_firmware(const struct firmware **firmware_p, const char *name,
396 struct device *device)
397{
398 struct class_device *class_dev;
399 struct firmware_priv *fw_priv;
400 struct firmware *firmware;
401 int retval;
402
403 if (!firmware_p)
404 return -EINVAL;
405
406 *firmware_p = firmware = kmalloc(sizeof (struct firmware), GFP_KERNEL);
407 if (!firmware) {
408 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
409 __FUNCTION__);
410 retval = -ENOMEM;
411 goto out;
412 }
413 memset(firmware, 0, sizeof (*firmware));
414
415 retval = fw_setup_class_device(firmware, &class_dev, name, device);
416 if (retval)
417 goto error_kfree_fw;
418
419 fw_priv = class_get_devdata(class_dev);
420
421 if (loading_timeout) {
422 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
423 add_timer(&fw_priv->timeout);
424 }
425
426 kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
427 wait_for_completion(&fw_priv->completion);
428 set_bit(FW_STATUS_DONE, &fw_priv->status);
429
430 del_timer_sync(&fw_priv->timeout);
431
432 down(&fw_lock);
433 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
434 retval = -ENOENT;
435 release_firmware(fw_priv->fw);
436 *firmware_p = NULL;
437 }
438 fw_priv->fw = NULL;
439 up(&fw_lock);
440 class_device_unregister(class_dev);
441 goto out;
442
443error_kfree_fw:
444 kfree(firmware);
445 *firmware_p = NULL;
446out:
447 return retval;
448}
449
450/**
451 * release_firmware: - release the resource associated with a firmware image
452 **/
453void
454release_firmware(const struct firmware *fw)
455{
456 if (fw) {
457 vfree(fw->data);
458 kfree(fw);
459 }
460}
461
462/**
463 * register_firmware: - provide a firmware image for later usage
464 *
465 * Description:
466 * Make sure that @data will be available by requesting firmware @name.
467 *
468 * Note: This will not be possible until some kind of persistence
469 * is available.
470 **/
471void
472register_firmware(const char *name, const u8 *data, size_t size)
473{
474 /* This is meaningless without firmware caching, so until we
475 * decide if firmware caching is reasonable just leave it as a
476 * noop */
477}
478
479/* Async support */
480struct firmware_work {
481 struct work_struct work;
482 struct module *module;
483 const char *name;
484 struct device *device;
485 void *context;
486 void (*cont)(const struct firmware *fw, void *context);
487};
488
489static int
490request_firmware_work_func(void *arg)
491{
492 struct firmware_work *fw_work = arg;
493 const struct firmware *fw;
494 if (!arg) {
495 WARN_ON(1);
496 return 0;
497 }
498 daemonize("%s/%s", "firmware", fw_work->name);
499 request_firmware(&fw, fw_work->name, fw_work->device);
500 fw_work->cont(fw, fw_work->context);
501 release_firmware(fw);
502 module_put(fw_work->module);
503 kfree(fw_work);
504 return 0;
505}
506
507/**
508 * request_firmware_nowait:
509 *
510 * Description:
511 * Asynchronous variant of request_firmware() for contexts where
512 * it is not possible to sleep.
513 *
514 * @cont will be called asynchronously when the firmware request is over.
515 *
516 * @context will be passed over to @cont.
517 *
518 * @fw may be %NULL if firmware request fails.
519 *
520 **/
521int
522request_firmware_nowait(
523 struct module *module,
524 const char *name, struct device *device, void *context,
525 void (*cont)(const struct firmware *fw, void *context))
526{
527 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
528 GFP_ATOMIC);
529 int ret;
530
531 if (!fw_work)
532 return -ENOMEM;
533 if (!try_module_get(module)) {
534 kfree(fw_work);
535 return -EFAULT;
536 }
537
538 *fw_work = (struct firmware_work) {
539 .module = module,
540 .name = name,
541 .device = device,
542 .context = context,
543 .cont = cont,
544 };
545
546 ret = kernel_thread(request_firmware_work_func, fw_work,
547 CLONE_FS | CLONE_FILES);
548
549 if (ret < 0) {
550 fw_work->cont(NULL, fw_work->context);
551 return ret;
552 }
553 return 0;
554}
555
556static int __init
557firmware_class_init(void)
558{
559 int error;
560 error = class_register(&firmware_class);
561 if (error) {
562 printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
563 return error;
564 }
565 error = class_create_file(&firmware_class, &class_attr_timeout);
566 if (error) {
567 printk(KERN_ERR "%s: class_create_file failed\n",
568 __FUNCTION__);
569 class_unregister(&firmware_class);
570 }
571 return error;
572
573}
574static void __exit
575firmware_class_exit(void)
576{
577 class_unregister(&firmware_class);
578}
579
580module_init(firmware_class_init);
581module_exit(firmware_class_exit);
582
583EXPORT_SYMBOL(release_firmware);
584EXPORT_SYMBOL(request_firmware);
585EXPORT_SYMBOL(request_firmware_nowait);
586EXPORT_SYMBOL(register_firmware);