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