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