PM / Sleep: introduce dpm_for_each_dev
[linux-2.6-block.git] / drivers / base / firmware_class.c
CommitLineData
1da177e4
LT
1/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
87d37a4f 4 * Copyright (c) 2003 Manuel Estrada Sainz
1da177e4
LT
5 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
c59ede7b 10#include <linux/capability.h>
1da177e4
LT
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>
cad1e55d 18#include <linux/mutex.h>
a36cf844 19#include <linux/workqueue.h>
6e03a201 20#include <linux/highmem.h>
1da177e4 21#include <linux/firmware.h>
5a0e3ad6 22#include <linux/slab.h>
a36cf844 23#include <linux/sched.h>
1f2b7959 24#include <linux/list.h>
37276a51
ML
25#include <linux/async.h>
26#include <linux/pm.h>
07646d9c 27#include <linux/suspend.h>
37276a51
ML
28
29#include "base.h"
30#include "power/power.h"
1da177e4 31
87d37a4f 32MODULE_AUTHOR("Manuel Estrada Sainz");
1da177e4
LT
33MODULE_DESCRIPTION("Multi purpose firmware loading support");
34MODULE_LICENSE("GPL");
35
bcb9bd18
DT
36/* Builtin firmware support */
37
38#ifdef CONFIG_FW_LOADER
39
40extern struct builtin_fw __start_builtin_fw[];
41extern struct builtin_fw __end_builtin_fw[];
42
43static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
44{
45 struct builtin_fw *b_fw;
46
47 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
48 if (strcmp(name, b_fw->name) == 0) {
49 fw->size = b_fw->size;
50 fw->data = b_fw->data;
51 return true;
52 }
53 }
54
55 return false;
56}
57
58static bool fw_is_builtin_firmware(const struct firmware *fw)
59{
60 struct builtin_fw *b_fw;
61
62 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
63 if (fw->data == b_fw->data)
64 return true;
65
66 return false;
67}
68
69#else /* Module case - no builtin firmware support */
70
71static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
72{
73 return false;
74}
75
76static inline bool fw_is_builtin_firmware(const struct firmware *fw)
77{
78 return false;
79}
80#endif
81
1da177e4
LT
82enum {
83 FW_STATUS_LOADING,
84 FW_STATUS_DONE,
85 FW_STATUS_ABORT,
1da177e4
LT
86};
87
2f65168d 88static int loading_timeout = 60; /* In seconds */
1da177e4 89
9b78c1da
RW
90static inline long firmware_loading_timeout(void)
91{
92 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
93}
94
1f2b7959
ML
95struct firmware_cache {
96 /* firmware_buf instance will be added into the below list */
97 spinlock_t lock;
98 struct list_head head;
37276a51
ML
99
100 /*
101 * Names of firmware images which have been cached successfully
102 * will be added into the below list so that device uncache
103 * helper can trace which firmware images have been cached
104 * before.
105 */
106 spinlock_t name_lock;
107 struct list_head fw_names;
108
109 wait_queue_head_t wait_queue;
110 int cnt;
111 struct delayed_work work;
07646d9c
ML
112
113 struct notifier_block pm_notify;
1f2b7959 114};
1da177e4 115
1244691c 116struct firmware_buf {
1f2b7959
ML
117 struct kref ref;
118 struct list_head list;
1da177e4 119 struct completion completion;
1f2b7959 120 struct firmware_cache *fwc;
1da177e4 121 unsigned long status;
65710cb6
ML
122 void *data;
123 size_t size;
6e03a201
DW
124 struct page **pages;
125 int nr_pages;
126 int page_array_size;
1244691c
ML
127 char fw_id[];
128};
129
37276a51
ML
130struct fw_cache_entry {
131 struct list_head list;
132 char name[];
133};
134
1244691c 135struct firmware_priv {
1da177e4 136 struct timer_list timeout;
e9045f91 137 bool nowait;
1244691c
ML
138 struct device dev;
139 struct firmware_buf *buf;
1f2b7959 140 struct firmware *fw;
1da177e4
LT
141};
142
f531f05a
ML
143struct fw_name_devm {
144 unsigned long magic;
145 char name[];
146};
147
1f2b7959
ML
148#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
149
150/* fw_lock could be moved to 'struct firmware_priv' but since it is just
151 * guarding for corner cases a global lock should be OK */
152static DEFINE_MUTEX(fw_lock);
153
154static struct firmware_cache fw_cache;
155
156static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
157 struct firmware_cache *fwc)
158{
159 struct firmware_buf *buf;
160
161 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
162
163 if (!buf)
164 return buf;
165
166 kref_init(&buf->ref);
167 strcpy(buf->fw_id, fw_name);
168 buf->fwc = fwc;
169 init_completion(&buf->completion);
170
171 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
172
173 return buf;
174}
175
2887b395
ML
176static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
177{
178 struct firmware_buf *tmp;
179 struct firmware_cache *fwc = &fw_cache;
180
181 list_for_each_entry(tmp, &fwc->head, list)
182 if (!strcmp(tmp->fw_id, fw_name))
183 return tmp;
184 return NULL;
185}
186
1f2b7959
ML
187static int fw_lookup_and_allocate_buf(const char *fw_name,
188 struct firmware_cache *fwc,
189 struct firmware_buf **buf)
190{
191 struct firmware_buf *tmp;
192
193 spin_lock(&fwc->lock);
2887b395
ML
194 tmp = __fw_lookup_buf(fw_name);
195 if (tmp) {
196 kref_get(&tmp->ref);
197 spin_unlock(&fwc->lock);
198 *buf = tmp;
199 return 1;
200 }
1f2b7959
ML
201 tmp = __allocate_fw_buf(fw_name, fwc);
202 if (tmp)
203 list_add(&tmp->list, &fwc->head);
204 spin_unlock(&fwc->lock);
205
206 *buf = tmp;
207
208 return tmp ? 0 : -ENOMEM;
209}
210
2887b395
ML
211static struct firmware_buf *fw_lookup_buf(const char *fw_name)
212{
213 struct firmware_buf *tmp;
214 struct firmware_cache *fwc = &fw_cache;
215
216 spin_lock(&fwc->lock);
217 tmp = __fw_lookup_buf(fw_name);
218 spin_unlock(&fwc->lock);
219
220 return tmp;
221}
222
1f2b7959
ML
223static void __fw_free_buf(struct kref *ref)
224{
225 struct firmware_buf *buf = to_fwbuf(ref);
226 struct firmware_cache *fwc = buf->fwc;
227 int i;
228
229 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
230 __func__, buf->fw_id, buf, buf->data,
231 (unsigned int)buf->size);
232
233 spin_lock(&fwc->lock);
234 list_del(&buf->list);
235 spin_unlock(&fwc->lock);
236
237 vunmap(buf->data);
238 for (i = 0; i < buf->nr_pages; i++)
239 __free_page(buf->pages[i]);
240 kfree(buf->pages);
241 kfree(buf);
242}
243
244static void fw_free_buf(struct firmware_buf *buf)
245{
246 kref_put(&buf->ref, __fw_free_buf);
247}
248
f8a4bd34
DT
249static struct firmware_priv *to_firmware_priv(struct device *dev)
250{
251 return container_of(dev, struct firmware_priv, dev);
252}
253
254static void fw_load_abort(struct firmware_priv *fw_priv)
1da177e4 255{
1244691c
ML
256 struct firmware_buf *buf = fw_priv->buf;
257
258 set_bit(FW_STATUS_ABORT, &buf->status);
1f2b7959 259 complete_all(&buf->completion);
1da177e4
LT
260}
261
f8a4bd34
DT
262static ssize_t firmware_timeout_show(struct class *class,
263 struct class_attribute *attr,
264 char *buf)
1da177e4
LT
265{
266 return sprintf(buf, "%d\n", loading_timeout);
267}
268
269/**
eb8e3179
RD
270 * firmware_timeout_store - set number of seconds to wait for firmware
271 * @class: device class pointer
e59817bf 272 * @attr: device attribute pointer
eb8e3179
RD
273 * @buf: buffer to scan for timeout value
274 * @count: number of bytes in @buf
275 *
1da177e4 276 * Sets the number of seconds to wait for the firmware. Once
eb8e3179 277 * this expires an error will be returned to the driver and no
1da177e4
LT
278 * firmware will be provided.
279 *
eb8e3179 280 * Note: zero means 'wait forever'.
1da177e4 281 **/
f8a4bd34
DT
282static ssize_t firmware_timeout_store(struct class *class,
283 struct class_attribute *attr,
284 const char *buf, size_t count)
1da177e4
LT
285{
286 loading_timeout = simple_strtol(buf, NULL, 10);
b92eac01
SG
287 if (loading_timeout < 0)
288 loading_timeout = 0;
f8a4bd34 289
1da177e4
LT
290 return count;
291}
292
673fae90
DT
293static struct class_attribute firmware_class_attrs[] = {
294 __ATTR(timeout, S_IWUSR | S_IRUGO,
295 firmware_timeout_show, firmware_timeout_store),
296 __ATTR_NULL
297};
1da177e4 298
1244691c
ML
299static void fw_dev_release(struct device *dev)
300{
301 struct firmware_priv *fw_priv = to_firmware_priv(dev);
65710cb6 302
673fae90 303 kfree(fw_priv);
673fae90
DT
304
305 module_put(THIS_MODULE);
306}
1da177e4 307
7eff2e7a 308static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4 309{
f8a4bd34 310 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1da177e4 311
1244691c 312 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
1da177e4 313 return -ENOMEM;
7eff2e7a 314 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
6897089c 315 return -ENOMEM;
e9045f91
JB
316 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
317 return -ENOMEM;
1da177e4
LT
318
319 return 0;
320}
321
1b81d663
AB
322static struct class firmware_class = {
323 .name = "firmware",
673fae90 324 .class_attrs = firmware_class_attrs,
e55c8790
GKH
325 .dev_uevent = firmware_uevent,
326 .dev_release = fw_dev_release,
1b81d663
AB
327};
328
e55c8790
GKH
329static ssize_t firmware_loading_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
1da177e4 331{
f8a4bd34 332 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 333 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
f8a4bd34 334
1da177e4
LT
335 return sprintf(buf, "%d\n", loading);
336}
337
65710cb6 338/* firmware holds the ownership of pages */
dd336c55
DW
339static void firmware_free_data(const struct firmware *fw)
340{
1f2b7959
ML
341 WARN_ON(!fw->priv);
342 fw_free_buf(fw->priv);
dd336c55
DW
343}
344
6e03a201
DW
345/* Some architectures don't have PAGE_KERNEL_RO */
346#ifndef PAGE_KERNEL_RO
347#define PAGE_KERNEL_RO PAGE_KERNEL
348#endif
1da177e4 349/**
eb8e3179 350 * firmware_loading_store - set value in the 'loading' control file
e55c8790 351 * @dev: device pointer
af9997e4 352 * @attr: device attribute pointer
eb8e3179
RD
353 * @buf: buffer to scan for loading control value
354 * @count: number of bytes in @buf
355 *
1da177e4
LT
356 * The relevant values are:
357 *
358 * 1: Start a load, discarding any previous partial load.
eb8e3179 359 * 0: Conclude the load and hand the data to the driver code.
1da177e4
LT
360 * -1: Conclude the load with an error and discard any written data.
361 **/
e55c8790
GKH
362static ssize_t firmware_loading_store(struct device *dev,
363 struct device_attribute *attr,
364 const char *buf, size_t count)
1da177e4 365{
f8a4bd34 366 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 367 struct firmware_buf *fw_buf = fw_priv->buf;
1da177e4 368 int loading = simple_strtol(buf, NULL, 10);
6e03a201 369 int i;
1da177e4 370
eea915bb
NH
371 mutex_lock(&fw_lock);
372
1244691c 373 if (!fw_buf)
eea915bb
NH
374 goto out;
375
1da177e4
LT
376 switch (loading) {
377 case 1:
65710cb6 378 /* discarding any previous partial load */
1244691c
ML
379 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
380 for (i = 0; i < fw_buf->nr_pages; i++)
381 __free_page(fw_buf->pages[i]);
382 kfree(fw_buf->pages);
383 fw_buf->pages = NULL;
384 fw_buf->page_array_size = 0;
385 fw_buf->nr_pages = 0;
386 set_bit(FW_STATUS_LOADING, &fw_buf->status);
28eefa75 387 }
1da177e4
LT
388 break;
389 case 0:
1244691c
ML
390 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
391 set_bit(FW_STATUS_DONE, &fw_buf->status);
392 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
1f2b7959 393 complete_all(&fw_buf->completion);
1da177e4
LT
394 break;
395 }
396 /* fallthrough */
397 default:
266a813c 398 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
1da177e4
LT
399 /* fallthrough */
400 case -1:
401 fw_load_abort(fw_priv);
402 break;
403 }
eea915bb
NH
404out:
405 mutex_unlock(&fw_lock);
1da177e4
LT
406 return count;
407}
408
e55c8790 409static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
1da177e4 410
f8a4bd34
DT
411static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
412 struct bin_attribute *bin_attr,
413 char *buffer, loff_t offset, size_t count)
1da177e4 414{
b0d1f807 415 struct device *dev = kobj_to_dev(kobj);
f8a4bd34 416 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 417 struct firmware_buf *buf;
f37e6617 418 ssize_t ret_count;
1da177e4 419
cad1e55d 420 mutex_lock(&fw_lock);
1244691c
ML
421 buf = fw_priv->buf;
422 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
1da177e4
LT
423 ret_count = -ENODEV;
424 goto out;
425 }
1244691c 426 if (offset > buf->size) {
308975fa
JS
427 ret_count = 0;
428 goto out;
429 }
1244691c
ML
430 if (count > buf->size - offset)
431 count = buf->size - offset;
6e03a201
DW
432
433 ret_count = count;
434
435 while (count) {
436 void *page_data;
437 int page_nr = offset >> PAGE_SHIFT;
438 int page_ofs = offset & (PAGE_SIZE-1);
439 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
440
1244691c 441 page_data = kmap(buf->pages[page_nr]);
6e03a201
DW
442
443 memcpy(buffer, page_data + page_ofs, page_cnt);
444
1244691c 445 kunmap(buf->pages[page_nr]);
6e03a201
DW
446 buffer += page_cnt;
447 offset += page_cnt;
448 count -= page_cnt;
449 }
1da177e4 450out:
cad1e55d 451 mutex_unlock(&fw_lock);
1da177e4
LT
452 return ret_count;
453}
eb8e3179 454
f8a4bd34 455static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
1da177e4 456{
1244691c 457 struct firmware_buf *buf = fw_priv->buf;
6e03a201
DW
458 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
459
460 /* If the array of pages is too small, grow it... */
1244691c 461 if (buf->page_array_size < pages_needed) {
6e03a201 462 int new_array_size = max(pages_needed,
1244691c 463 buf->page_array_size * 2);
6e03a201
DW
464 struct page **new_pages;
465
466 new_pages = kmalloc(new_array_size * sizeof(void *),
467 GFP_KERNEL);
468 if (!new_pages) {
469 fw_load_abort(fw_priv);
470 return -ENOMEM;
471 }
1244691c
ML
472 memcpy(new_pages, buf->pages,
473 buf->page_array_size * sizeof(void *));
474 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
475 (new_array_size - buf->page_array_size));
476 kfree(buf->pages);
477 buf->pages = new_pages;
478 buf->page_array_size = new_array_size;
6e03a201 479 }
1da177e4 480
1244691c
ML
481 while (buf->nr_pages < pages_needed) {
482 buf->pages[buf->nr_pages] =
6e03a201 483 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1da177e4 484
1244691c 485 if (!buf->pages[buf->nr_pages]) {
6e03a201
DW
486 fw_load_abort(fw_priv);
487 return -ENOMEM;
488 }
1244691c 489 buf->nr_pages++;
1da177e4 490 }
1da177e4
LT
491 return 0;
492}
493
494/**
eb8e3179 495 * firmware_data_write - write method for firmware
2c3c8bea 496 * @filp: open sysfs file
e55c8790 497 * @kobj: kobject for the device
42e61f4a 498 * @bin_attr: bin_attr structure
eb8e3179
RD
499 * @buffer: buffer being written
500 * @offset: buffer offset for write in total data store area
501 * @count: buffer size
1da177e4 502 *
eb8e3179 503 * Data written to the 'data' attribute will be later handed to
1da177e4
LT
504 * the driver as a firmware image.
505 **/
f8a4bd34
DT
506static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
507 struct bin_attribute *bin_attr,
508 char *buffer, loff_t offset, size_t count)
1da177e4 509{
b0d1f807 510 struct device *dev = kobj_to_dev(kobj);
f8a4bd34 511 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 512 struct firmware_buf *buf;
1da177e4
LT
513 ssize_t retval;
514
515 if (!capable(CAP_SYS_RAWIO))
516 return -EPERM;
b92eac01 517
cad1e55d 518 mutex_lock(&fw_lock);
1244691c
ML
519 buf = fw_priv->buf;
520 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
1da177e4
LT
521 retval = -ENODEV;
522 goto out;
523 }
65710cb6 524
1da177e4
LT
525 retval = fw_realloc_buffer(fw_priv, offset + count);
526 if (retval)
527 goto out;
528
1da177e4 529 retval = count;
6e03a201
DW
530
531 while (count) {
532 void *page_data;
533 int page_nr = offset >> PAGE_SHIFT;
534 int page_ofs = offset & (PAGE_SIZE - 1);
535 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
536
1244691c 537 page_data = kmap(buf->pages[page_nr]);
6e03a201
DW
538
539 memcpy(page_data + page_ofs, buffer, page_cnt);
540
1244691c 541 kunmap(buf->pages[page_nr]);
6e03a201
DW
542 buffer += page_cnt;
543 offset += page_cnt;
544 count -= page_cnt;
545 }
546
1244691c 547 buf->size = max_t(size_t, offset, buf->size);
1da177e4 548out:
cad1e55d 549 mutex_unlock(&fw_lock);
1da177e4
LT
550 return retval;
551}
eb8e3179 552
0983ca2d
DT
553static struct bin_attribute firmware_attr_data = {
554 .attr = { .name = "data", .mode = 0644 },
1da177e4
LT
555 .size = 0,
556 .read = firmware_data_read,
557 .write = firmware_data_write,
558};
559
f8a4bd34 560static void firmware_class_timeout(u_long data)
1da177e4
LT
561{
562 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
f8a4bd34 563
1da177e4
LT
564 fw_load_abort(fw_priv);
565}
566
f8a4bd34 567static struct firmware_priv *
dddb5549 568fw_create_instance(struct firmware *firmware, const char *fw_name,
f8a4bd34 569 struct device *device, bool uevent, bool nowait)
1da177e4 570{
f8a4bd34
DT
571 struct firmware_priv *fw_priv;
572 struct device *f_dev;
1da177e4 573
1244691c 574 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
f8a4bd34 575 if (!fw_priv) {
266a813c 576 dev_err(device, "%s: kmalloc failed\n", __func__);
1244691c
ML
577 fw_priv = ERR_PTR(-ENOMEM);
578 goto exit;
579 }
580
f8a4bd34 581 fw_priv->nowait = nowait;
1f2b7959 582 fw_priv->fw = firmware;
f8a4bd34
DT
583 setup_timer(&fw_priv->timeout,
584 firmware_class_timeout, (u_long) fw_priv);
1da177e4 585
f8a4bd34
DT
586 f_dev = &fw_priv->dev;
587
588 device_initialize(f_dev);
99c2aa72 589 dev_set_name(f_dev, "%s", fw_name);
e55c8790
GKH
590 f_dev->parent = device;
591 f_dev->class = &firmware_class;
1244691c 592exit:
f8a4bd34 593 return fw_priv;
1da177e4
LT
594}
595
1f2b7959
ML
596/* one pages buffer is mapped/unmapped only once */
597static int fw_map_pages_buf(struct firmware_buf *buf)
598{
599 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
600 if (!buf->data)
601 return -ENOMEM;
602 return 0;
603}
604
605/* store the pages buffer info firmware from buf */
606static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
607{
608 fw->priv = buf;
609 fw->pages = buf->pages;
610 fw->size = buf->size;
611 fw->data = buf->data;
612
613 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
614 __func__, buf->fw_id, buf, buf->data,
615 (unsigned int)buf->size);
616}
617
f531f05a
ML
618static void fw_name_devm_release(struct device *dev, void *res)
619{
620 struct fw_name_devm *fwn = res;
621
622 if (fwn->magic == (unsigned long)&fw_cache)
623 pr_debug("%s: fw_name-%s devm-%p released\n",
624 __func__, fwn->name, res);
625}
626
627static int fw_devm_match(struct device *dev, void *res,
628 void *match_data)
629{
630 struct fw_name_devm *fwn = res;
631
632 return (fwn->magic == (unsigned long)&fw_cache) &&
633 !strcmp(fwn->name, match_data);
634}
635
636static struct fw_name_devm *fw_find_devm_name(struct device *dev,
637 const char *name)
638{
639 struct fw_name_devm *fwn;
640
641 fwn = devres_find(dev, fw_name_devm_release,
642 fw_devm_match, (void *)name);
643 return fwn;
644}
645
646/* add firmware name into devres list */
647static int fw_add_devm_name(struct device *dev, const char *name)
648{
649 struct fw_name_devm *fwn;
650
651 fwn = fw_find_devm_name(dev, name);
652 if (fwn)
653 return 1;
654
655 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
656 strlen(name) + 1, GFP_KERNEL);
657 if (!fwn)
658 return -ENOMEM;
659
660 fwn->magic = (unsigned long)&fw_cache;
661 strcpy(fwn->name, name);
662 devres_add(dev, fwn);
663
664 return 0;
665}
666
1f2b7959
ML
667static void _request_firmware_cleanup(const struct firmware **firmware_p)
668{
669 release_firmware(*firmware_p);
670 *firmware_p = NULL;
671}
672
dddb5549
SB
673static struct firmware_priv *
674_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
675 struct device *device, bool uevent, bool nowait)
1da177e4 676{
1da177e4 677 struct firmware *firmware;
1f2b7959
ML
678 struct firmware_priv *fw_priv = NULL;
679 struct firmware_buf *buf;
680 int ret;
1da177e4
LT
681
682 if (!firmware_p)
dddb5549 683 return ERR_PTR(-EINVAL);
1da177e4 684
4aed0644 685 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
1da177e4 686 if (!firmware) {
266a813c
BH
687 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
688 __func__);
dddb5549 689 return ERR_PTR(-ENOMEM);
1da177e4 690 }
1da177e4 691
bcb9bd18 692 if (fw_get_builtin_firmware(firmware, name)) {
6f18ff91 693 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
dddb5549 694 return NULL;
5658c769
DW
695 }
696
1f2b7959
ML
697 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
698 if (!ret)
699 fw_priv = fw_create_instance(firmware, name, device,
700 uevent, nowait);
701
702 if (IS_ERR(fw_priv) || ret < 0) {
703 kfree(firmware);
dddb5549 704 *firmware_p = NULL;
1f2b7959
ML
705 return ERR_PTR(-ENOMEM);
706 } else if (fw_priv) {
707 fw_priv->buf = buf;
708
709 /*
710 * bind with 'buf' now to avoid warning in failure path
711 * of requesting firmware.
712 */
713 firmware->priv = buf;
714 return fw_priv;
dddb5549 715 }
811fa400 716
1f2b7959
ML
717 /* share the cached buf, which is inprogessing or completed */
718 check_status:
719 mutex_lock(&fw_lock);
720 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
721 fw_priv = ERR_PTR(-ENOENT);
722 _request_firmware_cleanup(firmware_p);
723 goto exit;
724 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
725 fw_priv = NULL;
726 fw_set_page_data(buf, firmware);
727 goto exit;
728 }
729 mutex_unlock(&fw_lock);
730 wait_for_completion(&buf->completion);
731 goto check_status;
65710cb6 732
1f2b7959
ML
733exit:
734 mutex_unlock(&fw_lock);
735 return fw_priv;
65710cb6
ML
736}
737
dddb5549
SB
738static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
739 long timeout)
811fa400 740{
9b78c1da 741 int retval = 0;
dddb5549 742 struct device *f_dev = &fw_priv->dev;
1244691c 743 struct firmware_buf *buf = fw_priv->buf;
dddb5549
SB
744
745 dev_set_uevent_suppress(f_dev, true);
caca9510 746
dddb5549
SB
747 /* Need to pin this module until class device is destroyed */
748 __module_get(THIS_MODULE);
5658c769 749
dddb5549
SB
750 retval = device_add(f_dev);
751 if (retval) {
752 dev_err(f_dev, "%s: device_register failed\n", __func__);
753 goto err_put_dev;
754 }
755
756 retval = device_create_bin_file(f_dev, &firmware_attr_data);
757 if (retval) {
758 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
759 goto err_del_dev;
760 }
761
762 retval = device_create_file(f_dev, &dev_attr_loading);
763 if (retval) {
764 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
765 goto err_del_bin_attr;
766 }
1da177e4 767
312c004d 768 if (uevent) {
dddb5549 769 dev_set_uevent_suppress(f_dev, false);
1244691c 770 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
9b78c1da 771 if (timeout != MAX_SCHEDULE_TIMEOUT)
f8a4bd34 772 mod_timer(&fw_priv->timeout,
9b78c1da 773 round_jiffies_up(jiffies + timeout));
f8a4bd34
DT
774
775 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
776 }
777
1244691c 778 wait_for_completion(&buf->completion);
1da177e4 779
f8a4bd34 780 del_timer_sync(&fw_priv->timeout);
1da177e4 781
cad1e55d 782 mutex_lock(&fw_lock);
1244691c 783 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
1da177e4 784 retval = -ENOENT;
65710cb6 785
f531f05a
ML
786 /*
787 * add firmware name into devres list so that we can auto cache
788 * and uncache firmware for device.
789 *
790 * f_dev->parent may has been deleted already, but the problem
791 * should be fixed in devres or driver core.
792 */
793 if (!retval && f_dev->parent)
794 fw_add_devm_name(f_dev->parent, buf->fw_id);
795
65710cb6 796 if (!retval)
1f2b7959
ML
797 retval = fw_map_pages_buf(buf);
798
799 /* pass the pages buffer to driver at the last minute */
800 fw_set_page_data(buf, fw_priv->fw);
1244691c 801
1244691c 802 fw_priv->buf = NULL;
cad1e55d 803 mutex_unlock(&fw_lock);
1da177e4 804
dddb5549
SB
805 device_remove_file(f_dev, &dev_attr_loading);
806err_del_bin_attr:
807 device_remove_bin_file(f_dev, &firmware_attr_data);
808err_del_dev:
809 device_del(f_dev);
810err_put_dev:
811 put_device(f_dev);
1da177e4
LT
812 return retval;
813}
814
6e3eaab0 815/**
312c004d 816 * request_firmware: - send firmware request and wait for it
eb8e3179
RD
817 * @firmware_p: pointer to firmware image
818 * @name: name of firmware file
819 * @device: device for which firmware is being loaded
820 *
821 * @firmware_p will be used to return a firmware image by the name
6e3eaab0
AS
822 * of @name for device @device.
823 *
824 * Should be called from user context where sleeping is allowed.
825 *
312c004d 826 * @name will be used as $FIRMWARE in the uevent environment and
6e3eaab0
AS
827 * should be distinctive enough not to be confused with any other
828 * firmware image for this or any other device.
0cfc1e1e
ML
829 *
830 * Caller must hold the reference count of @device.
6e3eaab0
AS
831 **/
832int
833request_firmware(const struct firmware **firmware_p, const char *name,
834 struct device *device)
835{
dddb5549 836 struct firmware_priv *fw_priv;
811fa400
RW
837 int ret;
838
dddb5549
SB
839 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
840 false);
841 if (IS_ERR_OR_NULL(fw_priv))
842 return PTR_RET(fw_priv);
811fa400 843
9b78c1da
RW
844 ret = usermodehelper_read_trylock();
845 if (WARN_ON(ret)) {
846 dev_err(device, "firmware: %s will not be loaded\n", name);
847 } else {
dddb5549 848 ret = _request_firmware_load(fw_priv, true,
9b78c1da
RW
849 firmware_loading_timeout());
850 usermodehelper_read_unlock();
851 }
811fa400
RW
852 if (ret)
853 _request_firmware_cleanup(firmware_p);
854
855 return ret;
6e3eaab0
AS
856}
857
1da177e4
LT
858/**
859 * release_firmware: - release the resource associated with a firmware image
eb8e3179 860 * @fw: firmware resource to release
1da177e4 861 **/
bcb9bd18 862void release_firmware(const struct firmware *fw)
1da177e4
LT
863{
864 if (fw) {
bcb9bd18
DT
865 if (!fw_is_builtin_firmware(fw))
866 firmware_free_data(fw);
1da177e4
LT
867 kfree(fw);
868 }
869}
870
1da177e4
LT
871/* Async support */
872struct firmware_work {
873 struct work_struct work;
874 struct module *module;
875 const char *name;
876 struct device *device;
877 void *context;
878 void (*cont)(const struct firmware *fw, void *context);
072fc8f0 879 bool uevent;
1da177e4
LT
880};
881
a36cf844 882static void request_firmware_work_func(struct work_struct *work)
1da177e4 883{
a36cf844 884 struct firmware_work *fw_work;
1da177e4 885 const struct firmware *fw;
dddb5549 886 struct firmware_priv *fw_priv;
9b78c1da 887 long timeout;
113fab13 888 int ret;
f8a4bd34 889
a36cf844 890 fw_work = container_of(work, struct firmware_work, work);
dddb5549
SB
891 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
892 fw_work->uevent, true);
893 if (IS_ERR_OR_NULL(fw_priv)) {
894 ret = PTR_RET(fw_priv);
811fa400 895 goto out;
dddb5549 896 }
811fa400 897
9b78c1da
RW
898 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
899 if (timeout) {
dddb5549 900 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
9b78c1da
RW
901 usermodehelper_read_unlock();
902 } else {
903 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
904 fw_work->name);
905 ret = -EAGAIN;
906 }
811fa400
RW
907 if (ret)
908 _request_firmware_cleanup(&fw);
909
910 out:
9ebfbd45 911 fw_work->cont(fw, fw_work->context);
0cfc1e1e 912 put_device(fw_work->device);
9ebfbd45 913
1da177e4
LT
914 module_put(fw_work->module);
915 kfree(fw_work);
1da177e4
LT
916}
917
918/**
3c31f07a 919 * request_firmware_nowait - asynchronous version of request_firmware
eb8e3179 920 * @module: module requesting the firmware
312c004d 921 * @uevent: sends uevent to copy the firmware image if this flag
eb8e3179
RD
922 * is non-zero else the firmware copy must be done manually.
923 * @name: name of firmware file
924 * @device: device for which firmware is being loaded
9ebfbd45 925 * @gfp: allocation flags
eb8e3179
RD
926 * @context: will be passed over to @cont, and
927 * @fw may be %NULL if firmware request fails.
928 * @cont: function will be called asynchronously when the firmware
929 * request is over.
1da177e4 930 *
0cfc1e1e
ML
931 * Caller must hold the reference count of @device.
932 *
6f21a62a
ML
933 * Asynchronous variant of request_firmware() for user contexts:
934 * - sleep for as small periods as possible since it may
935 * increase kernel boot time of built-in device drivers
936 * requesting firmware in their ->probe() methods, if
937 * @gfp is GFP_KERNEL.
938 *
939 * - can't sleep at all if @gfp is GFP_ATOMIC.
1da177e4
LT
940 **/
941int
942request_firmware_nowait(
072fc8f0 943 struct module *module, bool uevent,
9ebfbd45 944 const char *name, struct device *device, gfp_t gfp, void *context,
1da177e4
LT
945 void (*cont)(const struct firmware *fw, void *context))
946{
f8a4bd34 947 struct firmware_work *fw_work;
1da177e4 948
f8a4bd34 949 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
1da177e4
LT
950 if (!fw_work)
951 return -ENOMEM;
f8a4bd34
DT
952
953 fw_work->module = module;
954 fw_work->name = name;
955 fw_work->device = device;
956 fw_work->context = context;
957 fw_work->cont = cont;
958 fw_work->uevent = uevent;
959
1da177e4
LT
960 if (!try_module_get(module)) {
961 kfree(fw_work);
962 return -EFAULT;
963 }
964
0cfc1e1e 965 get_device(fw_work->device);
a36cf844
SB
966 INIT_WORK(&fw_work->work, request_firmware_work_func);
967 schedule_work(&fw_work->work);
1da177e4
LT
968 return 0;
969}
970
2887b395
ML
971/**
972 * cache_firmware - cache one firmware image in kernel memory space
973 * @fw_name: the firmware image name
974 *
975 * Cache firmware in kernel memory so that drivers can use it when
976 * system isn't ready for them to request firmware image from userspace.
977 * Once it returns successfully, driver can use request_firmware or its
978 * nowait version to get the cached firmware without any interacting
979 * with userspace
980 *
981 * Return 0 if the firmware image has been cached successfully
982 * Return !0 otherwise
983 *
984 */
985int cache_firmware(const char *fw_name)
986{
987 int ret;
988 const struct firmware *fw;
989
990 pr_debug("%s: %s\n", __func__, fw_name);
991
992 ret = request_firmware(&fw, fw_name, NULL);
993 if (!ret)
994 kfree(fw);
995
996 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
997
998 return ret;
999}
1000
1001/**
1002 * uncache_firmware - remove one cached firmware image
1003 * @fw_name: the firmware image name
1004 *
1005 * Uncache one firmware image which has been cached successfully
1006 * before.
1007 *
1008 * Return 0 if the firmware cache has been removed successfully
1009 * Return !0 otherwise
1010 *
1011 */
1012int uncache_firmware(const char *fw_name)
1013{
1014 struct firmware_buf *buf;
1015 struct firmware fw;
1016
1017 pr_debug("%s: %s\n", __func__, fw_name);
1018
1019 if (fw_get_builtin_firmware(&fw, fw_name))
1020 return 0;
1021
1022 buf = fw_lookup_buf(fw_name);
1023 if (buf) {
1024 fw_free_buf(buf);
1025 return 0;
1026 }
1027
1028 return -EINVAL;
1029}
1030
37276a51
ML
1031static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1032{
1033 struct fw_cache_entry *fce;
1034
1035 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1036 if (!fce)
1037 goto exit;
1038
1039 strcpy(fce->name, name);
1040exit:
1041 return fce;
1042}
1043
1044static void free_fw_cache_entry(struct fw_cache_entry *fce)
1045{
1046 kfree(fce);
1047}
1048
1049static void __async_dev_cache_fw_image(void *fw_entry,
1050 async_cookie_t cookie)
1051{
1052 struct fw_cache_entry *fce = fw_entry;
1053 struct firmware_cache *fwc = &fw_cache;
1054 int ret;
1055
1056 ret = cache_firmware(fce->name);
1057 if (ret)
1058 goto free;
1059
1060 spin_lock(&fwc->name_lock);
1061 list_add(&fce->list, &fwc->fw_names);
1062 spin_unlock(&fwc->name_lock);
1063 goto drop_ref;
1064
1065free:
1066 free_fw_cache_entry(fce);
1067drop_ref:
1068 spin_lock(&fwc->name_lock);
1069 fwc->cnt--;
1070 spin_unlock(&fwc->name_lock);
1071
1072 wake_up(&fwc->wait_queue);
1073}
1074
1075/* called with dev->devres_lock held */
1076static void dev_create_fw_entry(struct device *dev, void *res,
1077 void *data)
1078{
1079 struct fw_name_devm *fwn = res;
1080 const char *fw_name = fwn->name;
1081 struct list_head *head = data;
1082 struct fw_cache_entry *fce;
1083
1084 fce = alloc_fw_cache_entry(fw_name);
1085 if (fce)
1086 list_add(&fce->list, head);
1087}
1088
1089static int devm_name_match(struct device *dev, void *res,
1090 void *match_data)
1091{
1092 struct fw_name_devm *fwn = res;
1093 return (fwn->magic == (unsigned long)match_data);
1094}
1095
1096static void dev_cache_fw_image(struct device *dev)
1097{
1098 LIST_HEAD(todo);
1099 struct fw_cache_entry *fce;
1100 struct fw_cache_entry *fce_next;
1101 struct firmware_cache *fwc = &fw_cache;
1102
1103 devres_for_each_res(dev, fw_name_devm_release,
1104 devm_name_match, &fw_cache,
1105 dev_create_fw_entry, &todo);
1106
1107 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1108 list_del(&fce->list);
1109
1110 spin_lock(&fwc->name_lock);
1111 fwc->cnt++;
1112 spin_unlock(&fwc->name_lock);
1113
1114 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1115 }
1116}
1117
1118static void __device_uncache_fw_images(void)
1119{
1120 struct firmware_cache *fwc = &fw_cache;
1121 struct fw_cache_entry *fce;
1122
1123 spin_lock(&fwc->name_lock);
1124 while (!list_empty(&fwc->fw_names)) {
1125 fce = list_entry(fwc->fw_names.next,
1126 struct fw_cache_entry, list);
1127 list_del(&fce->list);
1128 spin_unlock(&fwc->name_lock);
1129
1130 uncache_firmware(fce->name);
1131 free_fw_cache_entry(fce);
1132
1133 spin_lock(&fwc->name_lock);
1134 }
1135 spin_unlock(&fwc->name_lock);
1136}
1137
1138/**
1139 * device_cache_fw_images - cache devices' firmware
1140 *
1141 * If one device called request_firmware or its nowait version
1142 * successfully before, the firmware names are recored into the
1143 * device's devres link list, so device_cache_fw_images can call
1144 * cache_firmware() to cache these firmwares for the device,
1145 * then the device driver can load its firmwares easily at
1146 * time when system is not ready to complete loading firmware.
1147 */
1148static void device_cache_fw_images(void)
1149{
1150 struct firmware_cache *fwc = &fw_cache;
1151 struct device *dev;
ffe53f6f 1152 int old_timeout;
37276a51
ML
1153 DEFINE_WAIT(wait);
1154
1155 pr_debug("%s\n", __func__);
1156
ffe53f6f
ML
1157 /*
1158 * use small loading timeout for caching devices' firmware
1159 * because all these firmware images have been loaded
1160 * successfully at lease once, also system is ready for
1161 * completing firmware loading now. The maximum size of
1162 * firmware in current distributions is about 2M bytes,
1163 * so 10 secs should be enough.
1164 */
1165 old_timeout = loading_timeout;
1166 loading_timeout = 10;
1167
37276a51
ML
1168 device_pm_lock();
1169 list_for_each_entry(dev, &dpm_list, power.entry)
1170 dev_cache_fw_image(dev);
1171 device_pm_unlock();
1172
1173 /* wait for completion of caching firmware for all devices */
1174 spin_lock(&fwc->name_lock);
1175 for (;;) {
1176 prepare_to_wait(&fwc->wait_queue, &wait,
1177 TASK_UNINTERRUPTIBLE);
1178 if (!fwc->cnt)
1179 break;
1180
1181 spin_unlock(&fwc->name_lock);
1182
1183 schedule();
1184
1185 spin_lock(&fwc->name_lock);
1186 }
1187 spin_unlock(&fwc->name_lock);
1188 finish_wait(&fwc->wait_queue, &wait);
ffe53f6f
ML
1189
1190 loading_timeout = old_timeout;
37276a51
ML
1191}
1192
1193/**
1194 * device_uncache_fw_images - uncache devices' firmware
1195 *
1196 * uncache all firmwares which have been cached successfully
1197 * by device_uncache_fw_images earlier
1198 */
1199static void device_uncache_fw_images(void)
1200{
1201 pr_debug("%s\n", __func__);
1202 __device_uncache_fw_images();
1203}
1204
1205static void device_uncache_fw_images_work(struct work_struct *work)
1206{
1207 device_uncache_fw_images();
1208}
1209
1210/**
1211 * device_uncache_fw_images_delay - uncache devices firmwares
1212 * @delay: number of milliseconds to delay uncache device firmwares
1213 *
1214 * uncache all devices's firmwares which has been cached successfully
1215 * by device_cache_fw_images after @delay milliseconds.
1216 */
1217static void device_uncache_fw_images_delay(unsigned long delay)
1218{
1219 schedule_delayed_work(&fw_cache.work,
1220 msecs_to_jiffies(delay));
1221}
1222
07646d9c
ML
1223#ifdef CONFIG_PM
1224static int fw_pm_notify(struct notifier_block *notify_block,
1225 unsigned long mode, void *unused)
1226{
1227 switch (mode) {
1228 case PM_HIBERNATION_PREPARE:
1229 case PM_SUSPEND_PREPARE:
1230 device_cache_fw_images();
1231 break;
1232
1233 case PM_POST_SUSPEND:
1234 case PM_POST_HIBERNATION:
1235 case PM_POST_RESTORE:
1236 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1237 break;
1238 }
1239
1240 return 0;
1241}
1242#else
1243static int fw_pm_notify(struct notifier_block *notify_block,
1244 unsigned long mode, void *unused)
c08f6773
ML
1245{
1246 return 0;
1247}
07646d9c
ML
1248#endif
1249
37276a51
ML
1250static void __init fw_cache_init(void)
1251{
1252 spin_lock_init(&fw_cache.lock);
1253 INIT_LIST_HEAD(&fw_cache.head);
1254
1255 spin_lock_init(&fw_cache.name_lock);
1256 INIT_LIST_HEAD(&fw_cache.fw_names);
1257 fw_cache.cnt = 0;
1258
1259 init_waitqueue_head(&fw_cache.wait_queue);
1260 INIT_DELAYED_WORK(&fw_cache.work,
1261 device_uncache_fw_images_work);
07646d9c
ML
1262
1263 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1264 register_pm_notifier(&fw_cache.pm_notify);
37276a51
ML
1265}
1266
673fae90 1267static int __init firmware_class_init(void)
1da177e4 1268{
1f2b7959 1269 fw_cache_init();
673fae90 1270 return class_register(&firmware_class);
1da177e4 1271}
673fae90
DT
1272
1273static void __exit firmware_class_exit(void)
1da177e4 1274{
07646d9c 1275 unregister_pm_notifier(&fw_cache.pm_notify);
1da177e4
LT
1276 class_unregister(&firmware_class);
1277}
1278
a30a6a2c 1279fs_initcall(firmware_class_init);
1da177e4
LT
1280module_exit(firmware_class_exit);
1281
1282EXPORT_SYMBOL(release_firmware);
1283EXPORT_SYMBOL(request_firmware);
1284EXPORT_SYMBOL(request_firmware_nowait);
2887b395
ML
1285EXPORT_SYMBOL_GPL(cache_firmware);
1286EXPORT_SYMBOL_GPL(uncache_firmware);