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