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