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