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