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