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