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