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