]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/base/firmware_class.c
kobject: Fix source code comment spelling
[mirror_ubuntu-artful-kernel.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>
abb139e7 24#include <linux/file.h>
1f2b7959 25#include <linux/list.h>
37276a51
ML
26#include <linux/async.h>
27#include <linux/pm.h>
07646d9c 28#include <linux/suspend.h>
ac39b3ea 29#include <linux/syscore_ops.h>
fe304143 30#include <linux/reboot.h>
37276a51 31
abb139e7
LT
32#include <generated/utsrelease.h>
33
37276a51 34#include "base.h"
1da177e4 35
87d37a4f 36MODULE_AUTHOR("Manuel Estrada Sainz");
1da177e4
LT
37MODULE_DESCRIPTION("Multi purpose firmware loading support");
38MODULE_LICENSE("GPL");
39
bcb9bd18
DT
40/* Builtin firmware support */
41
42#ifdef CONFIG_FW_LOADER
43
44extern struct builtin_fw __start_builtin_fw[];
45extern struct builtin_fw __end_builtin_fw[];
46
47static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
48{
49 struct builtin_fw *b_fw;
50
51 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
52 if (strcmp(name, b_fw->name) == 0) {
53 fw->size = b_fw->size;
54 fw->data = b_fw->data;
55 return true;
56 }
57 }
58
59 return false;
60}
61
62static bool fw_is_builtin_firmware(const struct firmware *fw)
63{
64 struct builtin_fw *b_fw;
65
66 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
67 if (fw->data == b_fw->data)
68 return true;
69
70 return false;
71}
72
73#else /* Module case - no builtin firmware support */
74
75static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
76{
77 return false;
78}
79
80static inline bool fw_is_builtin_firmware(const struct firmware *fw)
81{
82 return false;
83}
84#endif
85
1da177e4
LT
86enum {
87 FW_STATUS_LOADING,
88 FW_STATUS_DONE,
89 FW_STATUS_ABORT,
1da177e4
LT
90};
91
2f65168d 92static int loading_timeout = 60; /* In seconds */
1da177e4 93
9b78c1da
RW
94static inline long firmware_loading_timeout(void)
95{
96 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
97}
98
14c4bae7
TI
99/* firmware behavior options */
100#define FW_OPT_UEVENT (1U << 0)
101#define FW_OPT_NOWAIT (1U << 1)
68aeeaaa 102#ifdef CONFIG_FW_LOADER_USER_HELPER
14c4bae7 103#define FW_OPT_FALLBACK (1U << 2)
68aeeaaa
TI
104#else
105#define FW_OPT_FALLBACK 0
106#endif
14c4bae7 107
1f2b7959
ML
108struct firmware_cache {
109 /* firmware_buf instance will be added into the below list */
110 spinlock_t lock;
111 struct list_head head;
cfe016b1 112 int state;
37276a51 113
cfe016b1 114#ifdef CONFIG_PM_SLEEP
37276a51
ML
115 /*
116 * Names of firmware images which have been cached successfully
117 * will be added into the below list so that device uncache
118 * helper can trace which firmware images have been cached
119 * before.
120 */
121 spinlock_t name_lock;
122 struct list_head fw_names;
123
37276a51 124 struct delayed_work work;
07646d9c
ML
125
126 struct notifier_block pm_notify;
cfe016b1 127#endif
1f2b7959 128};
1da177e4 129
1244691c 130struct firmware_buf {
1f2b7959
ML
131 struct kref ref;
132 struct list_head list;
1da177e4 133 struct completion completion;
1f2b7959 134 struct firmware_cache *fwc;
1da177e4 135 unsigned long status;
65710cb6
ML
136 void *data;
137 size_t size;
7b1269f7 138#ifdef CONFIG_FW_LOADER_USER_HELPER
cd7239fa 139 bool is_paged_buf;
af5bc11e 140 bool need_uevent;
6e03a201
DW
141 struct page **pages;
142 int nr_pages;
143 int page_array_size;
fe304143 144 struct list_head pending_list;
7b1269f7 145#endif
1244691c
ML
146 char fw_id[];
147};
148
37276a51
ML
149struct fw_cache_entry {
150 struct list_head list;
151 char name[];
152};
153
f531f05a
ML
154struct fw_name_devm {
155 unsigned long magic;
156 char name[];
157};
158
1f2b7959
ML
159#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
160
ac39b3ea
ML
161#define FW_LOADER_NO_CACHE 0
162#define FW_LOADER_START_CACHE 1
163
164static int fw_cache_piggyback_on_request(const char *name);
165
1f2b7959
ML
166/* fw_lock could be moved to 'struct firmware_priv' but since it is just
167 * guarding for corner cases a global lock should be OK */
168static DEFINE_MUTEX(fw_lock);
169
170static struct firmware_cache fw_cache;
171
172static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
173 struct firmware_cache *fwc)
174{
175 struct firmware_buf *buf;
176
177 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
178
179 if (!buf)
180 return buf;
181
182 kref_init(&buf->ref);
183 strcpy(buf->fw_id, fw_name);
184 buf->fwc = fwc;
185 init_completion(&buf->completion);
fe304143
TI
186#ifdef CONFIG_FW_LOADER_USER_HELPER
187 INIT_LIST_HEAD(&buf->pending_list);
188#endif
1f2b7959
ML
189
190 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
191
192 return buf;
193}
194
2887b395
ML
195static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
196{
197 struct firmware_buf *tmp;
198 struct firmware_cache *fwc = &fw_cache;
199
200 list_for_each_entry(tmp, &fwc->head, list)
201 if (!strcmp(tmp->fw_id, fw_name))
202 return tmp;
203 return NULL;
204}
205
1f2b7959
ML
206static int fw_lookup_and_allocate_buf(const char *fw_name,
207 struct firmware_cache *fwc,
208 struct firmware_buf **buf)
209{
210 struct firmware_buf *tmp;
211
212 spin_lock(&fwc->lock);
2887b395
ML
213 tmp = __fw_lookup_buf(fw_name);
214 if (tmp) {
215 kref_get(&tmp->ref);
216 spin_unlock(&fwc->lock);
217 *buf = tmp;
218 return 1;
219 }
1f2b7959
ML
220 tmp = __allocate_fw_buf(fw_name, fwc);
221 if (tmp)
222 list_add(&tmp->list, &fwc->head);
223 spin_unlock(&fwc->lock);
224
225 *buf = tmp;
226
227 return tmp ? 0 : -ENOMEM;
228}
229
230static void __fw_free_buf(struct kref *ref)
231{
232 struct firmware_buf *buf = to_fwbuf(ref);
233 struct firmware_cache *fwc = buf->fwc;
1f2b7959
ML
234
235 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
236 __func__, buf->fw_id, buf, buf->data,
237 (unsigned int)buf->size);
238
1f2b7959
ML
239 list_del(&buf->list);
240 spin_unlock(&fwc->lock);
241
7b1269f7 242#ifdef CONFIG_FW_LOADER_USER_HELPER
cd7239fa 243 if (buf->is_paged_buf) {
7b1269f7 244 int i;
746058f4
ML
245 vunmap(buf->data);
246 for (i = 0; i < buf->nr_pages; i++)
247 __free_page(buf->pages[i]);
248 kfree(buf->pages);
249 } else
7b1269f7 250#endif
746058f4 251 vfree(buf->data);
1f2b7959
ML
252 kfree(buf);
253}
254
255static void fw_free_buf(struct firmware_buf *buf)
256{
bd9eb7fb
CL
257 struct firmware_cache *fwc = buf->fwc;
258 spin_lock(&fwc->lock);
259 if (!kref_put(&buf->ref, __fw_free_buf))
260 spin_unlock(&fwc->lock);
1f2b7959
ML
261}
262
746058f4 263/* direct firmware loading support */
27602842
ML
264static char fw_path_para[256];
265static const char * const fw_path[] = {
266 fw_path_para,
746058f4
ML
267 "/lib/firmware/updates/" UTS_RELEASE,
268 "/lib/firmware/updates",
269 "/lib/firmware/" UTS_RELEASE,
270 "/lib/firmware"
271};
272
27602842
ML
273/*
274 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
275 * from kernel command line because firmware_class is generally built in
276 * kernel instead of module.
277 */
278module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
279MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
280
746058f4 281/* Don't inline this: 'struct kstat' is biggish */
60dac5e2 282static noinline_for_stack long fw_file_size(struct file *file)
746058f4
ML
283{
284 struct kstat st;
3dadecce 285 if (vfs_getattr(&file->f_path, &st))
746058f4
ML
286 return -1;
287 if (!S_ISREG(st.mode))
288 return -1;
289 if (st.size != (long)st.size)
290 return -1;
291 return st.size;
292}
293
3e358ac2 294static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
746058f4
ML
295{
296 long size;
297 char *buf;
3e358ac2 298 int rc;
746058f4
ML
299
300 size = fw_file_size(file);
4adf07fb 301 if (size <= 0)
3e358ac2 302 return -EINVAL;
746058f4
ML
303 buf = vmalloc(size);
304 if (!buf)
3e358ac2
NH
305 return -ENOMEM;
306 rc = kernel_read(file, 0, buf, size);
307 if (rc != size) {
308 if (rc > 0)
309 rc = -EIO;
746058f4 310 vfree(buf);
3e358ac2 311 return rc;
746058f4
ML
312 }
313 fw_buf->data = buf;
314 fw_buf->size = size;
3e358ac2 315 return 0;
746058f4
ML
316}
317
3e358ac2 318static int fw_get_filesystem_firmware(struct device *device,
4e0c92d0 319 struct firmware_buf *buf)
746058f4
ML
320{
321 int i;
3e358ac2 322 int rc = -ENOENT;
746058f4
ML
323 char *path = __getname();
324
325 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
326 struct file *file;
27602842
ML
327
328 /* skip the unset customized path */
329 if (!fw_path[i][0])
330 continue;
331
746058f4
ML
332 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
333
334 file = filp_open(path, O_RDONLY, 0);
335 if (IS_ERR(file))
336 continue;
3e358ac2 337 rc = fw_read_file_contents(file, buf);
746058f4 338 fput(file);
3e358ac2
NH
339 if (rc)
340 dev_warn(device, "firmware, attempted to load %s, but failed with error %d\n",
341 path, rc);
342 else
746058f4
ML
343 break;
344 }
345 __putname(path);
4e0c92d0 346
3e358ac2 347 if (!rc) {
4e0c92d0
TI
348 dev_dbg(device, "firmware: direct-loading firmware %s\n",
349 buf->fw_id);
350 mutex_lock(&fw_lock);
351 set_bit(FW_STATUS_DONE, &buf->status);
352 complete_all(&buf->completion);
353 mutex_unlock(&fw_lock);
354 }
355
3e358ac2 356 return rc;
746058f4
ML
357}
358
7b1269f7
TI
359/* firmware holds the ownership of pages */
360static void firmware_free_data(const struct firmware *fw)
361{
362 /* Loaded directly? */
363 if (!fw->priv) {
364 vfree(fw->data);
365 return;
366 }
367 fw_free_buf(fw->priv);
368}
369
cd7239fa
TI
370/* store the pages buffer info firmware from buf */
371static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
372{
373 fw->priv = buf;
374#ifdef CONFIG_FW_LOADER_USER_HELPER
375 fw->pages = buf->pages;
376#endif
377 fw->size = buf->size;
378 fw->data = buf->data;
379
380 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
381 __func__, buf->fw_id, buf, buf->data,
382 (unsigned int)buf->size);
383}
384
385#ifdef CONFIG_PM_SLEEP
386static void fw_name_devm_release(struct device *dev, void *res)
387{
388 struct fw_name_devm *fwn = res;
389
390 if (fwn->magic == (unsigned long)&fw_cache)
391 pr_debug("%s: fw_name-%s devm-%p released\n",
392 __func__, fwn->name, res);
393}
394
395static int fw_devm_match(struct device *dev, void *res,
396 void *match_data)
397{
398 struct fw_name_devm *fwn = res;
399
400 return (fwn->magic == (unsigned long)&fw_cache) &&
401 !strcmp(fwn->name, match_data);
402}
403
404static struct fw_name_devm *fw_find_devm_name(struct device *dev,
405 const char *name)
406{
407 struct fw_name_devm *fwn;
408
409 fwn = devres_find(dev, fw_name_devm_release,
410 fw_devm_match, (void *)name);
411 return fwn;
412}
413
414/* add firmware name into devres list */
415static int fw_add_devm_name(struct device *dev, const char *name)
416{
417 struct fw_name_devm *fwn;
418
419 fwn = fw_find_devm_name(dev, name);
420 if (fwn)
421 return 1;
422
423 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
424 strlen(name) + 1, GFP_KERNEL);
425 if (!fwn)
426 return -ENOMEM;
427
428 fwn->magic = (unsigned long)&fw_cache;
429 strcpy(fwn->name, name);
430 devres_add(dev, fwn);
431
432 return 0;
433}
434#else
435static int fw_add_devm_name(struct device *dev, const char *name)
436{
437 return 0;
438}
439#endif
440
441
442/*
443 * user-mode helper code
444 */
7b1269f7 445#ifdef CONFIG_FW_LOADER_USER_HELPER
cd7239fa
TI
446struct firmware_priv {
447 struct delayed_work timeout_work;
448 bool nowait;
449 struct device dev;
450 struct firmware_buf *buf;
451 struct firmware *fw;
452};
7b1269f7 453
f8a4bd34
DT
454static struct firmware_priv *to_firmware_priv(struct device *dev)
455{
456 return container_of(dev, struct firmware_priv, dev);
457}
458
7068cb07 459static void __fw_load_abort(struct firmware_buf *buf)
1da177e4 460{
87597936
ML
461 /*
462 * There is a small window in which user can write to 'loading'
463 * between loading done and disappearance of 'loading'
464 */
465 if (test_bit(FW_STATUS_DONE, &buf->status))
466 return;
467
fe304143 468 list_del_init(&buf->pending_list);
1244691c 469 set_bit(FW_STATUS_ABORT, &buf->status);
1f2b7959 470 complete_all(&buf->completion);
1da177e4
LT
471}
472
7068cb07
GKH
473static void fw_load_abort(struct firmware_priv *fw_priv)
474{
475 struct firmware_buf *buf = fw_priv->buf;
476
477 __fw_load_abort(buf);
87597936
ML
478
479 /* avoid user action after loading abort */
480 fw_priv->buf = NULL;
1da177e4
LT
481}
482
807be03c
TI
483#define is_fw_load_aborted(buf) \
484 test_bit(FW_STATUS_ABORT, &(buf)->status)
485
fe304143
TI
486static LIST_HEAD(pending_fw_head);
487
488/* reboot notifier for avoid deadlock with usermode_lock */
489static int fw_shutdown_notify(struct notifier_block *unused1,
490 unsigned long unused2, void *unused3)
491{
492 mutex_lock(&fw_lock);
493 while (!list_empty(&pending_fw_head))
7068cb07 494 __fw_load_abort(list_first_entry(&pending_fw_head,
fe304143
TI
495 struct firmware_buf,
496 pending_list));
497 mutex_unlock(&fw_lock);
498 return NOTIFY_DONE;
499}
500
501static struct notifier_block fw_shutdown_nb = {
502 .notifier_call = fw_shutdown_notify,
503};
504
14adbe53
GKH
505static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
506 char *buf)
1da177e4
LT
507{
508 return sprintf(buf, "%d\n", loading_timeout);
509}
510
511/**
eb8e3179
RD
512 * firmware_timeout_store - set number of seconds to wait for firmware
513 * @class: device class pointer
e59817bf 514 * @attr: device attribute pointer
eb8e3179
RD
515 * @buf: buffer to scan for timeout value
516 * @count: number of bytes in @buf
517 *
1da177e4 518 * Sets the number of seconds to wait for the firmware. Once
eb8e3179 519 * this expires an error will be returned to the driver and no
1da177e4
LT
520 * firmware will be provided.
521 *
eb8e3179 522 * Note: zero means 'wait forever'.
1da177e4 523 **/
14adbe53
GKH
524static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
525 const char *buf, size_t count)
1da177e4
LT
526{
527 loading_timeout = simple_strtol(buf, NULL, 10);
b92eac01
SG
528 if (loading_timeout < 0)
529 loading_timeout = 0;
f8a4bd34 530
1da177e4
LT
531 return count;
532}
533
673fae90 534static struct class_attribute firmware_class_attrs[] = {
14adbe53 535 __ATTR_RW(timeout),
673fae90
DT
536 __ATTR_NULL
537};
1da177e4 538
1244691c
ML
539static void fw_dev_release(struct device *dev)
540{
541 struct firmware_priv *fw_priv = to_firmware_priv(dev);
65710cb6 542
673fae90 543 kfree(fw_priv);
673fae90 544}
1da177e4 545
7eff2e7a 546static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4 547{
f8a4bd34 548 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1da177e4 549
1244691c 550 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
1da177e4 551 return -ENOMEM;
7eff2e7a 552 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
6897089c 553 return -ENOMEM;
e9045f91
JB
554 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
555 return -ENOMEM;
1da177e4
LT
556
557 return 0;
558}
559
1b81d663
AB
560static struct class firmware_class = {
561 .name = "firmware",
673fae90 562 .class_attrs = firmware_class_attrs,
e55c8790
GKH
563 .dev_uevent = firmware_uevent,
564 .dev_release = fw_dev_release,
1b81d663
AB
565};
566
e55c8790
GKH
567static ssize_t firmware_loading_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
1da177e4 569{
f8a4bd34 570 struct firmware_priv *fw_priv = to_firmware_priv(dev);
87597936
ML
571 int loading = 0;
572
573 mutex_lock(&fw_lock);
574 if (fw_priv->buf)
575 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
576 mutex_unlock(&fw_lock);
f8a4bd34 577
1da177e4
LT
578 return sprintf(buf, "%d\n", loading);
579}
580
6e03a201
DW
581/* Some architectures don't have PAGE_KERNEL_RO */
582#ifndef PAGE_KERNEL_RO
583#define PAGE_KERNEL_RO PAGE_KERNEL
584#endif
253c9240
ML
585
586/* one pages buffer should be mapped/unmapped only once */
587static int fw_map_pages_buf(struct firmware_buf *buf)
588{
cd7239fa 589 if (!buf->is_paged_buf)
746058f4
ML
590 return 0;
591
253c9240
ML
592 if (buf->data)
593 vunmap(buf->data);
594 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
595 if (!buf->data)
596 return -ENOMEM;
597 return 0;
598}
599
1da177e4 600/**
eb8e3179 601 * firmware_loading_store - set value in the 'loading' control file
e55c8790 602 * @dev: device pointer
af9997e4 603 * @attr: device attribute pointer
eb8e3179
RD
604 * @buf: buffer to scan for loading control value
605 * @count: number of bytes in @buf
606 *
1da177e4
LT
607 * The relevant values are:
608 *
609 * 1: Start a load, discarding any previous partial load.
eb8e3179 610 * 0: Conclude the load and hand the data to the driver code.
1da177e4
LT
611 * -1: Conclude the load with an error and discard any written data.
612 **/
e55c8790
GKH
613static ssize_t firmware_loading_store(struct device *dev,
614 struct device_attribute *attr,
615 const char *buf, size_t count)
1da177e4 616{
f8a4bd34 617 struct firmware_priv *fw_priv = to_firmware_priv(dev);
87597936 618 struct firmware_buf *fw_buf;
1da177e4 619 int loading = simple_strtol(buf, NULL, 10);
6e03a201 620 int i;
1da177e4 621
eea915bb 622 mutex_lock(&fw_lock);
87597936 623 fw_buf = fw_priv->buf;
1244691c 624 if (!fw_buf)
eea915bb
NH
625 goto out;
626
1da177e4
LT
627 switch (loading) {
628 case 1:
65710cb6 629 /* discarding any previous partial load */
1244691c
ML
630 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
631 for (i = 0; i < fw_buf->nr_pages; i++)
632 __free_page(fw_buf->pages[i]);
633 kfree(fw_buf->pages);
634 fw_buf->pages = NULL;
635 fw_buf->page_array_size = 0;
636 fw_buf->nr_pages = 0;
637 set_bit(FW_STATUS_LOADING, &fw_buf->status);
28eefa75 638 }
1da177e4
LT
639 break;
640 case 0:
1244691c
ML
641 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
642 set_bit(FW_STATUS_DONE, &fw_buf->status);
643 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
253c9240
ML
644
645 /*
646 * Several loading requests may be pending on
647 * one same firmware buf, so let all requests
648 * see the mapped 'buf->data' once the loading
649 * is completed.
650 * */
651 fw_map_pages_buf(fw_buf);
fe304143 652 list_del_init(&fw_buf->pending_list);
1f2b7959 653 complete_all(&fw_buf->completion);
1da177e4
LT
654 break;
655 }
656 /* fallthrough */
657 default:
266a813c 658 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
1da177e4
LT
659 /* fallthrough */
660 case -1:
661 fw_load_abort(fw_priv);
662 break;
663 }
eea915bb
NH
664out:
665 mutex_unlock(&fw_lock);
1da177e4
LT
666 return count;
667}
668
e55c8790 669static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
1da177e4 670
f8a4bd34
DT
671static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
672 struct bin_attribute *bin_attr,
673 char *buffer, loff_t offset, size_t count)
1da177e4 674{
b0d1f807 675 struct device *dev = kobj_to_dev(kobj);
f8a4bd34 676 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 677 struct firmware_buf *buf;
f37e6617 678 ssize_t ret_count;
1da177e4 679
cad1e55d 680 mutex_lock(&fw_lock);
1244691c
ML
681 buf = fw_priv->buf;
682 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
1da177e4
LT
683 ret_count = -ENODEV;
684 goto out;
685 }
1244691c 686 if (offset > buf->size) {
308975fa
JS
687 ret_count = 0;
688 goto out;
689 }
1244691c
ML
690 if (count > buf->size - offset)
691 count = buf->size - offset;
6e03a201
DW
692
693 ret_count = count;
694
695 while (count) {
696 void *page_data;
697 int page_nr = offset >> PAGE_SHIFT;
698 int page_ofs = offset & (PAGE_SIZE-1);
699 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
700
1244691c 701 page_data = kmap(buf->pages[page_nr]);
6e03a201
DW
702
703 memcpy(buffer, page_data + page_ofs, page_cnt);
704
1244691c 705 kunmap(buf->pages[page_nr]);
6e03a201
DW
706 buffer += page_cnt;
707 offset += page_cnt;
708 count -= page_cnt;
709 }
1da177e4 710out:
cad1e55d 711 mutex_unlock(&fw_lock);
1da177e4
LT
712 return ret_count;
713}
eb8e3179 714
f8a4bd34 715static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
1da177e4 716{
1244691c 717 struct firmware_buf *buf = fw_priv->buf;
6e03a201
DW
718 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
719
720 /* If the array of pages is too small, grow it... */
1244691c 721 if (buf->page_array_size < pages_needed) {
6e03a201 722 int new_array_size = max(pages_needed,
1244691c 723 buf->page_array_size * 2);
6e03a201
DW
724 struct page **new_pages;
725
726 new_pages = kmalloc(new_array_size * sizeof(void *),
727 GFP_KERNEL);
728 if (!new_pages) {
729 fw_load_abort(fw_priv);
730 return -ENOMEM;
731 }
1244691c
ML
732 memcpy(new_pages, buf->pages,
733 buf->page_array_size * sizeof(void *));
734 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
735 (new_array_size - buf->page_array_size));
736 kfree(buf->pages);
737 buf->pages = new_pages;
738 buf->page_array_size = new_array_size;
6e03a201 739 }
1da177e4 740
1244691c
ML
741 while (buf->nr_pages < pages_needed) {
742 buf->pages[buf->nr_pages] =
6e03a201 743 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1da177e4 744
1244691c 745 if (!buf->pages[buf->nr_pages]) {
6e03a201
DW
746 fw_load_abort(fw_priv);
747 return -ENOMEM;
748 }
1244691c 749 buf->nr_pages++;
1da177e4 750 }
1da177e4
LT
751 return 0;
752}
753
754/**
eb8e3179 755 * firmware_data_write - write method for firmware
2c3c8bea 756 * @filp: open sysfs file
e55c8790 757 * @kobj: kobject for the device
42e61f4a 758 * @bin_attr: bin_attr structure
eb8e3179
RD
759 * @buffer: buffer being written
760 * @offset: buffer offset for write in total data store area
761 * @count: buffer size
1da177e4 762 *
eb8e3179 763 * Data written to the 'data' attribute will be later handed to
1da177e4
LT
764 * the driver as a firmware image.
765 **/
f8a4bd34
DT
766static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
767 struct bin_attribute *bin_attr,
768 char *buffer, loff_t offset, size_t count)
1da177e4 769{
b0d1f807 770 struct device *dev = kobj_to_dev(kobj);
f8a4bd34 771 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 772 struct firmware_buf *buf;
1da177e4
LT
773 ssize_t retval;
774
775 if (!capable(CAP_SYS_RAWIO))
776 return -EPERM;
b92eac01 777
cad1e55d 778 mutex_lock(&fw_lock);
1244691c
ML
779 buf = fw_priv->buf;
780 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
1da177e4
LT
781 retval = -ENODEV;
782 goto out;
783 }
65710cb6 784
1da177e4
LT
785 retval = fw_realloc_buffer(fw_priv, offset + count);
786 if (retval)
787 goto out;
788
1da177e4 789 retval = count;
6e03a201
DW
790
791 while (count) {
792 void *page_data;
793 int page_nr = offset >> PAGE_SHIFT;
794 int page_ofs = offset & (PAGE_SIZE - 1);
795 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
796
1244691c 797 page_data = kmap(buf->pages[page_nr]);
6e03a201
DW
798
799 memcpy(page_data + page_ofs, buffer, page_cnt);
800
1244691c 801 kunmap(buf->pages[page_nr]);
6e03a201
DW
802 buffer += page_cnt;
803 offset += page_cnt;
804 count -= page_cnt;
805 }
806
1244691c 807 buf->size = max_t(size_t, offset, buf->size);
1da177e4 808out:
cad1e55d 809 mutex_unlock(&fw_lock);
1da177e4
LT
810 return retval;
811}
eb8e3179 812
0983ca2d
DT
813static struct bin_attribute firmware_attr_data = {
814 .attr = { .name = "data", .mode = 0644 },
1da177e4
LT
815 .size = 0,
816 .read = firmware_data_read,
817 .write = firmware_data_write,
818};
819
ce2fcbd9 820static void firmware_class_timeout_work(struct work_struct *work)
1da177e4 821{
ce2fcbd9
CL
822 struct firmware_priv *fw_priv = container_of(work,
823 struct firmware_priv, timeout_work.work);
f8a4bd34 824
ce2fcbd9 825 mutex_lock(&fw_lock);
1da177e4 826 fw_load_abort(fw_priv);
ce2fcbd9 827 mutex_unlock(&fw_lock);
1da177e4
LT
828}
829
f8a4bd34 830static struct firmware_priv *
dddb5549 831fw_create_instance(struct firmware *firmware, const char *fw_name,
14c4bae7 832 struct device *device, unsigned int opt_flags)
1da177e4 833{
f8a4bd34
DT
834 struct firmware_priv *fw_priv;
835 struct device *f_dev;
1da177e4 836
1244691c 837 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
f8a4bd34 838 if (!fw_priv) {
266a813c 839 dev_err(device, "%s: kmalloc failed\n", __func__);
1244691c
ML
840 fw_priv = ERR_PTR(-ENOMEM);
841 goto exit;
842 }
843
14c4bae7 844 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
1f2b7959 845 fw_priv->fw = firmware;
ce2fcbd9
CL
846 INIT_DELAYED_WORK(&fw_priv->timeout_work,
847 firmware_class_timeout_work);
1da177e4 848
f8a4bd34
DT
849 f_dev = &fw_priv->dev;
850
851 device_initialize(f_dev);
99c2aa72 852 dev_set_name(f_dev, "%s", fw_name);
e55c8790
GKH
853 f_dev->parent = device;
854 f_dev->class = &firmware_class;
1244691c 855exit:
f8a4bd34 856 return fw_priv;
1da177e4
LT
857}
858
cd7239fa 859/* load a firmware via user helper */
14c4bae7
TI
860static int _request_firmware_load(struct firmware_priv *fw_priv,
861 unsigned int opt_flags, long timeout)
1f2b7959 862{
cd7239fa
TI
863 int retval = 0;
864 struct device *f_dev = &fw_priv->dev;
865 struct firmware_buf *buf = fw_priv->buf;
1f2b7959 866
cd7239fa
TI
867 /* fall back on userspace loading */
868 buf->is_paged_buf = true;
1f2b7959 869
cd7239fa 870 dev_set_uevent_suppress(f_dev, true);
f531f05a 871
cd7239fa
TI
872 retval = device_add(f_dev);
873 if (retval) {
874 dev_err(f_dev, "%s: device_register failed\n", __func__);
875 goto err_put_dev;
876 }
f531f05a 877
cd7239fa
TI
878 retval = device_create_bin_file(f_dev, &firmware_attr_data);
879 if (retval) {
880 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
881 goto err_del_dev;
882 }
f531f05a 883
1eeeef15
MB
884 mutex_lock(&fw_lock);
885 list_add(&buf->pending_list, &pending_fw_head);
886 mutex_unlock(&fw_lock);
887
cd7239fa
TI
888 retval = device_create_file(f_dev, &dev_attr_loading);
889 if (retval) {
1eeeef15
MB
890 mutex_lock(&fw_lock);
891 list_del_init(&buf->pending_list);
892 mutex_unlock(&fw_lock);
cd7239fa
TI
893 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
894 goto err_del_bin_attr;
895 }
f531f05a 896
14c4bae7 897 if (opt_flags & FW_OPT_UEVENT) {
af5bc11e 898 buf->need_uevent = true;
cd7239fa
TI
899 dev_set_uevent_suppress(f_dev, false);
900 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
901 if (timeout != MAX_SCHEDULE_TIMEOUT)
902 schedule_delayed_work(&fw_priv->timeout_work, timeout);
f531f05a 903
cd7239fa
TI
904 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
905 }
f531f05a 906
cd7239fa 907 wait_for_completion(&buf->completion);
f531f05a 908
cd7239fa 909 cancel_delayed_work_sync(&fw_priv->timeout_work);
f531f05a 910
cd7239fa
TI
911 device_remove_file(f_dev, &dev_attr_loading);
912err_del_bin_attr:
913 device_remove_bin_file(f_dev, &firmware_attr_data);
914err_del_dev:
915 device_del(f_dev);
916err_put_dev:
917 put_device(f_dev);
918 return retval;
f531f05a 919}
cd7239fa
TI
920
921static int fw_load_from_user_helper(struct firmware *firmware,
922 const char *name, struct device *device,
14c4bae7 923 unsigned int opt_flags, long timeout)
cfe016b1 924{
cd7239fa
TI
925 struct firmware_priv *fw_priv;
926
14c4bae7 927 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
cd7239fa
TI
928 if (IS_ERR(fw_priv))
929 return PTR_ERR(fw_priv);
930
931 fw_priv->buf = firmware->priv;
14c4bae7 932 return _request_firmware_load(fw_priv, opt_flags, timeout);
cfe016b1 933}
ddf1f064 934
5b7cb7a1 935#ifdef CONFIG_PM_SLEEP
ddf1f064
ML
936/* kill pending requests without uevent to avoid blocking suspend */
937static void kill_requests_without_uevent(void)
938{
939 struct firmware_buf *buf;
940 struct firmware_buf *next;
941
942 mutex_lock(&fw_lock);
943 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
944 if (!buf->need_uevent)
7068cb07 945 __fw_load_abort(buf);
ddf1f064
ML
946 }
947 mutex_unlock(&fw_lock);
948}
5b7cb7a1 949#endif
ddf1f064 950
cd7239fa
TI
951#else /* CONFIG_FW_LOADER_USER_HELPER */
952static inline int
953fw_load_from_user_helper(struct firmware *firmware, const char *name,
14c4bae7 954 struct device *device, unsigned int opt_flags,
cd7239fa
TI
955 long timeout)
956{
957 return -ENOENT;
958}
807be03c
TI
959
960/* No abort during direct loading */
961#define is_fw_load_aborted(buf) false
962
5b7cb7a1 963#ifdef CONFIG_PM_SLEEP
ddf1f064 964static inline void kill_requests_without_uevent(void) { }
5b7cb7a1 965#endif
ddf1f064 966
cd7239fa
TI
967#endif /* CONFIG_FW_LOADER_USER_HELPER */
968
f531f05a 969
4e0c92d0
TI
970/* wait until the shared firmware_buf becomes ready (or error) */
971static int sync_cached_firmware_buf(struct firmware_buf *buf)
1f2b7959 972{
4e0c92d0
TI
973 int ret = 0;
974
975 mutex_lock(&fw_lock);
976 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
807be03c 977 if (is_fw_load_aborted(buf)) {
4e0c92d0
TI
978 ret = -ENOENT;
979 break;
980 }
981 mutex_unlock(&fw_lock);
982 wait_for_completion(&buf->completion);
983 mutex_lock(&fw_lock);
984 }
985 mutex_unlock(&fw_lock);
986 return ret;
1f2b7959
ML
987}
988
4e0c92d0
TI
989/* prepare firmware and firmware_buf structs;
990 * return 0 if a firmware is already assigned, 1 if need to load one,
991 * or a negative error code
992 */
993static int
994_request_firmware_prepare(struct firmware **firmware_p, const char *name,
995 struct device *device)
1da177e4 996{
1da177e4 997 struct firmware *firmware;
1f2b7959
ML
998 struct firmware_buf *buf;
999 int ret;
1da177e4 1000
4aed0644 1001 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
1da177e4 1002 if (!firmware) {
266a813c
BH
1003 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1004 __func__);
4e0c92d0 1005 return -ENOMEM;
1da177e4 1006 }
1da177e4 1007
bcb9bd18 1008 if (fw_get_builtin_firmware(firmware, name)) {
6f18ff91 1009 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
4e0c92d0 1010 return 0; /* assigned */
5658c769
DW
1011 }
1012
1f2b7959 1013 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
4e0c92d0
TI
1014
1015 /*
1016 * bind with 'buf' now to avoid warning in failure path
1017 * of requesting firmware.
1018 */
1019 firmware->priv = buf;
1020
1021 if (ret > 0) {
1022 ret = sync_cached_firmware_buf(buf);
1023 if (!ret) {
1024 fw_set_page_data(buf, firmware);
1025 return 0; /* assigned */
1026 }
dddb5549 1027 }
811fa400 1028
4e0c92d0
TI
1029 if (ret < 0)
1030 return ret;
1031 return 1; /* need to load */
1032}
1033
e771d1aa 1034static int assign_firmware_buf(struct firmware *fw, struct device *device,
14c4bae7 1035 unsigned int opt_flags)
4e0c92d0
TI
1036{
1037 struct firmware_buf *buf = fw->priv;
1038
1f2b7959 1039 mutex_lock(&fw_lock);
807be03c 1040 if (!buf->size || is_fw_load_aborted(buf)) {
4e0c92d0
TI
1041 mutex_unlock(&fw_lock);
1042 return -ENOENT;
1f2b7959 1043 }
65710cb6 1044
4e0c92d0
TI
1045 /*
1046 * add firmware name into devres list so that we can auto cache
1047 * and uncache firmware for device.
1048 *
1049 * device may has been deleted already, but the problem
1050 * should be fixed in devres or driver core.
1051 */
14c4bae7
TI
1052 /* don't cache firmware handled without uevent */
1053 if (device && (opt_flags & FW_OPT_UEVENT))
4e0c92d0
TI
1054 fw_add_devm_name(device, buf->fw_id);
1055
1056 /*
1057 * After caching firmware image is started, let it piggyback
1058 * on request firmware.
1059 */
1060 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1061 if (fw_cache_piggyback_on_request(buf->fw_id))
1062 kref_get(&buf->ref);
1063 }
1064
1065 /* pass the pages buffer to driver at the last minute */
1066 fw_set_page_data(buf, fw);
1f2b7959 1067 mutex_unlock(&fw_lock);
4e0c92d0 1068 return 0;
65710cb6
ML
1069}
1070
4e0c92d0
TI
1071/* called from request_firmware() and request_firmware_work_func() */
1072static int
1073_request_firmware(const struct firmware **firmware_p, const char *name,
14c4bae7 1074 struct device *device, unsigned int opt_flags)
4e0c92d0
TI
1075{
1076 struct firmware *fw;
1077 long timeout;
1078 int ret;
1079
1080 if (!firmware_p)
1081 return -EINVAL;
1082
1083 ret = _request_firmware_prepare(&fw, name, device);
1084 if (ret <= 0) /* error or already assigned */
1085 goto out;
1086
1087 ret = 0;
1088 timeout = firmware_loading_timeout();
14c4bae7 1089 if (opt_flags & FW_OPT_NOWAIT) {
4e0c92d0
TI
1090 timeout = usermodehelper_read_lock_wait(timeout);
1091 if (!timeout) {
1092 dev_dbg(device, "firmware: %s loading timed out\n",
1093 name);
1094 ret = -EBUSY;
1095 goto out;
1096 }
1097 } else {
1098 ret = usermodehelper_read_trylock();
1099 if (WARN_ON(ret)) {
1100 dev_err(device, "firmware: %s will not be loaded\n",
1101 name);
1102 goto out;
1103 }
1104 }
1105
3e358ac2
NH
1106 ret = fw_get_filesystem_firmware(device, fw->priv);
1107 if (ret) {
14c4bae7 1108 if (opt_flags & FW_OPT_FALLBACK) {
bba3a87e
TI
1109 dev_warn(device,
1110 "Direct firmware load failed with error %d\n",
1111 ret);
1112 dev_warn(device, "Falling back to user helper\n");
1113 ret = fw_load_from_user_helper(fw, name, device,
14c4bae7 1114 opt_flags, timeout);
bba3a87e 1115 }
3e358ac2 1116 }
e771d1aa 1117
4e0c92d0 1118 if (!ret)
14c4bae7 1119 ret = assign_firmware_buf(fw, device, opt_flags);
4e0c92d0
TI
1120
1121 usermodehelper_read_unlock();
1122
1123 out:
1124 if (ret < 0) {
1125 release_firmware(fw);
1126 fw = NULL;
1127 }
1128
1129 *firmware_p = fw;
1130 return ret;
1131}
1132
6e3eaab0 1133/**
312c004d 1134 * request_firmware: - send firmware request and wait for it
eb8e3179
RD
1135 * @firmware_p: pointer to firmware image
1136 * @name: name of firmware file
1137 * @device: device for which firmware is being loaded
1138 *
1139 * @firmware_p will be used to return a firmware image by the name
6e3eaab0
AS
1140 * of @name for device @device.
1141 *
1142 * Should be called from user context where sleeping is allowed.
1143 *
312c004d 1144 * @name will be used as $FIRMWARE in the uevent environment and
6e3eaab0
AS
1145 * should be distinctive enough not to be confused with any other
1146 * firmware image for this or any other device.
0cfc1e1e
ML
1147 *
1148 * Caller must hold the reference count of @device.
6a927857
ML
1149 *
1150 * The function can be called safely inside device's suspend and
1151 * resume callback.
6e3eaab0
AS
1152 **/
1153int
1154request_firmware(const struct firmware **firmware_p, const char *name,
1155 struct device *device)
1156{
d6c8aa39
ML
1157 int ret;
1158
1159 /* Need to pin this module until return */
1160 __module_get(THIS_MODULE);
14c4bae7
TI
1161 ret = _request_firmware(firmware_p, name, device,
1162 FW_OPT_UEVENT | FW_OPT_FALLBACK);
d6c8aa39
ML
1163 module_put(THIS_MODULE);
1164 return ret;
6e3eaab0 1165}
f494513f 1166EXPORT_SYMBOL(request_firmware);
6e3eaab0 1167
bba3a87e
TI
1168#ifdef CONFIG_FW_LOADER_USER_HELPER
1169/**
1170 * request_firmware: - load firmware directly without usermode helper
1171 * @firmware_p: pointer to firmware image
1172 * @name: name of firmware file
1173 * @device: device for which firmware is being loaded
1174 *
1175 * This function works pretty much like request_firmware(), but this doesn't
1176 * fall back to usermode helper even if the firmware couldn't be loaded
1177 * directly from fs. Hence it's useful for loading optional firmwares, which
1178 * aren't always present, without extra long timeouts of udev.
1179 **/
1180int request_firmware_direct(const struct firmware **firmware_p,
1181 const char *name, struct device *device)
1182{
1183 int ret;
1184 __module_get(THIS_MODULE);
14c4bae7 1185 ret = _request_firmware(firmware_p, name, device, FW_OPT_UEVENT);
bba3a87e
TI
1186 module_put(THIS_MODULE);
1187 return ret;
1188}
1189EXPORT_SYMBOL_GPL(request_firmware_direct);
1190#endif
1191
1da177e4
LT
1192/**
1193 * release_firmware: - release the resource associated with a firmware image
eb8e3179 1194 * @fw: firmware resource to release
1da177e4 1195 **/
bcb9bd18 1196void release_firmware(const struct firmware *fw)
1da177e4
LT
1197{
1198 if (fw) {
bcb9bd18
DT
1199 if (!fw_is_builtin_firmware(fw))
1200 firmware_free_data(fw);
1da177e4
LT
1201 kfree(fw);
1202 }
1203}
f494513f 1204EXPORT_SYMBOL(release_firmware);
1da177e4 1205
1da177e4
LT
1206/* Async support */
1207struct firmware_work {
1208 struct work_struct work;
1209 struct module *module;
1210 const char *name;
1211 struct device *device;
1212 void *context;
1213 void (*cont)(const struct firmware *fw, void *context);
14c4bae7 1214 unsigned int opt_flags;
1da177e4
LT
1215};
1216
a36cf844 1217static void request_firmware_work_func(struct work_struct *work)
1da177e4 1218{
a36cf844 1219 struct firmware_work *fw_work;
1da177e4 1220 const struct firmware *fw;
f8a4bd34 1221
a36cf844 1222 fw_work = container_of(work, struct firmware_work, work);
811fa400 1223
4e0c92d0 1224 _request_firmware(&fw, fw_work->name, fw_work->device,
14c4bae7 1225 fw_work->opt_flags);
9ebfbd45 1226 fw_work->cont(fw, fw_work->context);
4e0c92d0 1227 put_device(fw_work->device); /* taken in request_firmware_nowait() */
9ebfbd45 1228
1da177e4
LT
1229 module_put(fw_work->module);
1230 kfree(fw_work);
1da177e4
LT
1231}
1232
1233/**
3c31f07a 1234 * request_firmware_nowait - asynchronous version of request_firmware
eb8e3179 1235 * @module: module requesting the firmware
312c004d 1236 * @uevent: sends uevent to copy the firmware image if this flag
eb8e3179
RD
1237 * is non-zero else the firmware copy must be done manually.
1238 * @name: name of firmware file
1239 * @device: device for which firmware is being loaded
9ebfbd45 1240 * @gfp: allocation flags
eb8e3179
RD
1241 * @context: will be passed over to @cont, and
1242 * @fw may be %NULL if firmware request fails.
1243 * @cont: function will be called asynchronously when the firmware
1244 * request is over.
1da177e4 1245 *
0cfc1e1e
ML
1246 * Caller must hold the reference count of @device.
1247 *
6f21a62a
ML
1248 * Asynchronous variant of request_firmware() for user contexts:
1249 * - sleep for as small periods as possible since it may
1250 * increase kernel boot time of built-in device drivers
1251 * requesting firmware in their ->probe() methods, if
1252 * @gfp is GFP_KERNEL.
1253 *
1254 * - can't sleep at all if @gfp is GFP_ATOMIC.
1da177e4
LT
1255 **/
1256int
1257request_firmware_nowait(
072fc8f0 1258 struct module *module, bool uevent,
9ebfbd45 1259 const char *name, struct device *device, gfp_t gfp, void *context,
1da177e4
LT
1260 void (*cont)(const struct firmware *fw, void *context))
1261{
f8a4bd34 1262 struct firmware_work *fw_work;
1da177e4 1263
f8a4bd34 1264 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
1da177e4
LT
1265 if (!fw_work)
1266 return -ENOMEM;
f8a4bd34
DT
1267
1268 fw_work->module = module;
1269 fw_work->name = name;
1270 fw_work->device = device;
1271 fw_work->context = context;
1272 fw_work->cont = cont;
14c4bae7
TI
1273 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
1274 (uevent ? FW_OPT_UEVENT : 0);
f8a4bd34 1275
1da177e4
LT
1276 if (!try_module_get(module)) {
1277 kfree(fw_work);
1278 return -EFAULT;
1279 }
1280
0cfc1e1e 1281 get_device(fw_work->device);
a36cf844
SB
1282 INIT_WORK(&fw_work->work, request_firmware_work_func);
1283 schedule_work(&fw_work->work);
1da177e4
LT
1284 return 0;
1285}
f494513f 1286EXPORT_SYMBOL(request_firmware_nowait);
1da177e4 1287
90f89081
ML
1288#ifdef CONFIG_PM_SLEEP
1289static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1290
2887b395
ML
1291/**
1292 * cache_firmware - cache one firmware image in kernel memory space
1293 * @fw_name: the firmware image name
1294 *
1295 * Cache firmware in kernel memory so that drivers can use it when
1296 * system isn't ready for them to request firmware image from userspace.
1297 * Once it returns successfully, driver can use request_firmware or its
1298 * nowait version to get the cached firmware without any interacting
1299 * with userspace
1300 *
1301 * Return 0 if the firmware image has been cached successfully
1302 * Return !0 otherwise
1303 *
1304 */
93232e46 1305static int cache_firmware(const char *fw_name)
2887b395
ML
1306{
1307 int ret;
1308 const struct firmware *fw;
1309
1310 pr_debug("%s: %s\n", __func__, fw_name);
1311
1312 ret = request_firmware(&fw, fw_name, NULL);
1313 if (!ret)
1314 kfree(fw);
1315
1316 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1317
1318 return ret;
1319}
1320
6a2c1234
ML
1321static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1322{
1323 struct firmware_buf *tmp;
1324 struct firmware_cache *fwc = &fw_cache;
1325
1326 spin_lock(&fwc->lock);
1327 tmp = __fw_lookup_buf(fw_name);
1328 spin_unlock(&fwc->lock);
1329
1330 return tmp;
1331}
1332
2887b395
ML
1333/**
1334 * uncache_firmware - remove one cached firmware image
1335 * @fw_name: the firmware image name
1336 *
1337 * Uncache one firmware image which has been cached successfully
1338 * before.
1339 *
1340 * Return 0 if the firmware cache has been removed successfully
1341 * Return !0 otherwise
1342 *
1343 */
93232e46 1344static int uncache_firmware(const char *fw_name)
2887b395
ML
1345{
1346 struct firmware_buf *buf;
1347 struct firmware fw;
1348
1349 pr_debug("%s: %s\n", __func__, fw_name);
1350
1351 if (fw_get_builtin_firmware(&fw, fw_name))
1352 return 0;
1353
1354 buf = fw_lookup_buf(fw_name);
1355 if (buf) {
1356 fw_free_buf(buf);
1357 return 0;
1358 }
1359
1360 return -EINVAL;
1361}
1362
37276a51
ML
1363static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1364{
1365 struct fw_cache_entry *fce;
1366
1367 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1368 if (!fce)
1369 goto exit;
1370
1371 strcpy(fce->name, name);
1372exit:
1373 return fce;
1374}
1375
373304fe 1376static int __fw_entry_found(const char *name)
ac39b3ea
ML
1377{
1378 struct firmware_cache *fwc = &fw_cache;
1379 struct fw_cache_entry *fce;
ac39b3ea 1380
ac39b3ea
ML
1381 list_for_each_entry(fce, &fwc->fw_names, list) {
1382 if (!strcmp(fce->name, name))
373304fe 1383 return 1;
ac39b3ea 1384 }
373304fe
ML
1385 return 0;
1386}
1387
1388static int fw_cache_piggyback_on_request(const char *name)
1389{
1390 struct firmware_cache *fwc = &fw_cache;
1391 struct fw_cache_entry *fce;
1392 int ret = 0;
1393
1394 spin_lock(&fwc->name_lock);
1395 if (__fw_entry_found(name))
1396 goto found;
ac39b3ea
ML
1397
1398 fce = alloc_fw_cache_entry(name);
1399 if (fce) {
1400 ret = 1;
1401 list_add(&fce->list, &fwc->fw_names);
1402 pr_debug("%s: fw: %s\n", __func__, name);
1403 }
1404found:
1405 spin_unlock(&fwc->name_lock);
1406 return ret;
1407}
1408
37276a51
ML
1409static void free_fw_cache_entry(struct fw_cache_entry *fce)
1410{
1411 kfree(fce);
1412}
1413
1414static void __async_dev_cache_fw_image(void *fw_entry,
1415 async_cookie_t cookie)
1416{
1417 struct fw_cache_entry *fce = fw_entry;
1418 struct firmware_cache *fwc = &fw_cache;
1419 int ret;
1420
1421 ret = cache_firmware(fce->name);
ac39b3ea
ML
1422 if (ret) {
1423 spin_lock(&fwc->name_lock);
1424 list_del(&fce->list);
1425 spin_unlock(&fwc->name_lock);
37276a51 1426
ac39b3ea
ML
1427 free_fw_cache_entry(fce);
1428 }
37276a51
ML
1429}
1430
1431/* called with dev->devres_lock held */
1432static void dev_create_fw_entry(struct device *dev, void *res,
1433 void *data)
1434{
1435 struct fw_name_devm *fwn = res;
1436 const char *fw_name = fwn->name;
1437 struct list_head *head = data;
1438 struct fw_cache_entry *fce;
1439
1440 fce = alloc_fw_cache_entry(fw_name);
1441 if (fce)
1442 list_add(&fce->list, head);
1443}
1444
1445static int devm_name_match(struct device *dev, void *res,
1446 void *match_data)
1447{
1448 struct fw_name_devm *fwn = res;
1449 return (fwn->magic == (unsigned long)match_data);
1450}
1451
ab6dd8e5 1452static void dev_cache_fw_image(struct device *dev, void *data)
37276a51
ML
1453{
1454 LIST_HEAD(todo);
1455 struct fw_cache_entry *fce;
1456 struct fw_cache_entry *fce_next;
1457 struct firmware_cache *fwc = &fw_cache;
1458
1459 devres_for_each_res(dev, fw_name_devm_release,
1460 devm_name_match, &fw_cache,
1461 dev_create_fw_entry, &todo);
1462
1463 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1464 list_del(&fce->list);
1465
1466 spin_lock(&fwc->name_lock);
373304fe
ML
1467 /* only one cache entry for one firmware */
1468 if (!__fw_entry_found(fce->name)) {
373304fe
ML
1469 list_add(&fce->list, &fwc->fw_names);
1470 } else {
1471 free_fw_cache_entry(fce);
1472 fce = NULL;
1473 }
37276a51
ML
1474 spin_unlock(&fwc->name_lock);
1475
373304fe 1476 if (fce)
d28d3882
ML
1477 async_schedule_domain(__async_dev_cache_fw_image,
1478 (void *)fce,
1479 &fw_cache_domain);
37276a51
ML
1480 }
1481}
1482
1483static void __device_uncache_fw_images(void)
1484{
1485 struct firmware_cache *fwc = &fw_cache;
1486 struct fw_cache_entry *fce;
1487
1488 spin_lock(&fwc->name_lock);
1489 while (!list_empty(&fwc->fw_names)) {
1490 fce = list_entry(fwc->fw_names.next,
1491 struct fw_cache_entry, list);
1492 list_del(&fce->list);
1493 spin_unlock(&fwc->name_lock);
1494
1495 uncache_firmware(fce->name);
1496 free_fw_cache_entry(fce);
1497
1498 spin_lock(&fwc->name_lock);
1499 }
1500 spin_unlock(&fwc->name_lock);
1501}
1502
1503/**
1504 * device_cache_fw_images - cache devices' firmware
1505 *
1506 * If one device called request_firmware or its nowait version
1507 * successfully before, the firmware names are recored into the
1508 * device's devres link list, so device_cache_fw_images can call
1509 * cache_firmware() to cache these firmwares for the device,
1510 * then the device driver can load its firmwares easily at
1511 * time when system is not ready to complete loading firmware.
1512 */
1513static void device_cache_fw_images(void)
1514{
1515 struct firmware_cache *fwc = &fw_cache;
ffe53f6f 1516 int old_timeout;
37276a51
ML
1517 DEFINE_WAIT(wait);
1518
1519 pr_debug("%s\n", __func__);
1520
373304fe
ML
1521 /* cancel uncache work */
1522 cancel_delayed_work_sync(&fwc->work);
1523
ffe53f6f
ML
1524 /*
1525 * use small loading timeout for caching devices' firmware
1526 * because all these firmware images have been loaded
1527 * successfully at lease once, also system is ready for
1528 * completing firmware loading now. The maximum size of
1529 * firmware in current distributions is about 2M bytes,
1530 * so 10 secs should be enough.
1531 */
1532 old_timeout = loading_timeout;
1533 loading_timeout = 10;
1534
ac39b3ea
ML
1535 mutex_lock(&fw_lock);
1536 fwc->state = FW_LOADER_START_CACHE;
ab6dd8e5 1537 dpm_for_each_dev(NULL, dev_cache_fw_image);
ac39b3ea 1538 mutex_unlock(&fw_lock);
37276a51
ML
1539
1540 /* wait for completion of caching firmware for all devices */
d28d3882 1541 async_synchronize_full_domain(&fw_cache_domain);
ffe53f6f
ML
1542
1543 loading_timeout = old_timeout;
37276a51
ML
1544}
1545
1546/**
1547 * device_uncache_fw_images - uncache devices' firmware
1548 *
1549 * uncache all firmwares which have been cached successfully
1550 * by device_uncache_fw_images earlier
1551 */
1552static void device_uncache_fw_images(void)
1553{
1554 pr_debug("%s\n", __func__);
1555 __device_uncache_fw_images();
1556}
1557
1558static void device_uncache_fw_images_work(struct work_struct *work)
1559{
1560 device_uncache_fw_images();
1561}
1562
1563/**
1564 * device_uncache_fw_images_delay - uncache devices firmwares
1565 * @delay: number of milliseconds to delay uncache device firmwares
1566 *
1567 * uncache all devices's firmwares which has been cached successfully
1568 * by device_cache_fw_images after @delay milliseconds.
1569 */
1570static void device_uncache_fw_images_delay(unsigned long delay)
1571{
1572 schedule_delayed_work(&fw_cache.work,
1573 msecs_to_jiffies(delay));
1574}
1575
07646d9c
ML
1576static int fw_pm_notify(struct notifier_block *notify_block,
1577 unsigned long mode, void *unused)
1578{
1579 switch (mode) {
1580 case PM_HIBERNATION_PREPARE:
1581 case PM_SUSPEND_PREPARE:
af5bc11e 1582 kill_requests_without_uevent();
07646d9c
ML
1583 device_cache_fw_images();
1584 break;
1585
1586 case PM_POST_SUSPEND:
1587 case PM_POST_HIBERNATION:
1588 case PM_POST_RESTORE:
ac39b3ea
ML
1589 /*
1590 * In case that system sleep failed and syscore_suspend is
1591 * not called.
1592 */
1593 mutex_lock(&fw_lock);
1594 fw_cache.state = FW_LOADER_NO_CACHE;
1595 mutex_unlock(&fw_lock);
1596
07646d9c
ML
1597 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1598 break;
1599 }
1600
1601 return 0;
1602}
07646d9c 1603
ac39b3ea
ML
1604/* stop caching firmware once syscore_suspend is reached */
1605static int fw_suspend(void)
1606{
1607 fw_cache.state = FW_LOADER_NO_CACHE;
1608 return 0;
1609}
1610
1611static struct syscore_ops fw_syscore_ops = {
1612 .suspend = fw_suspend,
1613};
cfe016b1
ML
1614#else
1615static int fw_cache_piggyback_on_request(const char *name)
1616{
1617 return 0;
1618}
1619#endif
ac39b3ea 1620
37276a51
ML
1621static void __init fw_cache_init(void)
1622{
1623 spin_lock_init(&fw_cache.lock);
1624 INIT_LIST_HEAD(&fw_cache.head);
cfe016b1 1625 fw_cache.state = FW_LOADER_NO_CACHE;
37276a51 1626
cfe016b1 1627#ifdef CONFIG_PM_SLEEP
37276a51
ML
1628 spin_lock_init(&fw_cache.name_lock);
1629 INIT_LIST_HEAD(&fw_cache.fw_names);
37276a51 1630
37276a51
ML
1631 INIT_DELAYED_WORK(&fw_cache.work,
1632 device_uncache_fw_images_work);
07646d9c
ML
1633
1634 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1635 register_pm_notifier(&fw_cache.pm_notify);
ac39b3ea
ML
1636
1637 register_syscore_ops(&fw_syscore_ops);
cfe016b1 1638#endif
37276a51
ML
1639}
1640
673fae90 1641static int __init firmware_class_init(void)
1da177e4 1642{
1f2b7959 1643 fw_cache_init();
7b1269f7 1644#ifdef CONFIG_FW_LOADER_USER_HELPER
fe304143 1645 register_reboot_notifier(&fw_shutdown_nb);
673fae90 1646 return class_register(&firmware_class);
7b1269f7
TI
1647#else
1648 return 0;
1649#endif
1da177e4 1650}
673fae90
DT
1651
1652static void __exit firmware_class_exit(void)
1da177e4 1653{
cfe016b1 1654#ifdef CONFIG_PM_SLEEP
ac39b3ea 1655 unregister_syscore_ops(&fw_syscore_ops);
07646d9c 1656 unregister_pm_notifier(&fw_cache.pm_notify);
cfe016b1 1657#endif
7b1269f7 1658#ifdef CONFIG_FW_LOADER_USER_HELPER
fe304143 1659 unregister_reboot_notifier(&fw_shutdown_nb);
1da177e4 1660 class_unregister(&firmware_class);
7b1269f7 1661#endif
1da177e4
LT
1662}
1663
a30a6a2c 1664fs_initcall(firmware_class_init);
1da177e4 1665module_exit(firmware_class_exit);