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