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