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