65c60666685b0dac79ff2f70735f2e696e9e8d11
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / base / firmware_class.c
1 /*
2  * firmware_class.c - Multi purpose firmware loading support
3  *
4  * Copyright (c) 2003 Manuel Estrada Sainz
5  *
6  * Please see Documentation/firmware_class/ for more information.
7  *
8  */
9
10 #include <linux/capability.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/timer.h>
15 #include <linux/vmalloc.h>
16 #include <linux/interrupt.h>
17 #include <linux/bitops.h>
18 #include <linux/mutex.h>
19 #include <linux/workqueue.h>
20 #include <linux/highmem.h>
21 #include <linux/firmware.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/list.h>
25
26 MODULE_AUTHOR("Manuel Estrada Sainz");
27 MODULE_DESCRIPTION("Multi purpose firmware loading support");
28 MODULE_LICENSE("GPL");
29
30 /* Builtin firmware support */
31
32 #ifdef CONFIG_FW_LOADER
33
34 extern struct builtin_fw __start_builtin_fw[];
35 extern struct builtin_fw __end_builtin_fw[];
36
37 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
38 {
39         struct builtin_fw *b_fw;
40
41         for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
42                 if (strcmp(name, b_fw->name) == 0) {
43                         fw->size = b_fw->size;
44                         fw->data = b_fw->data;
45                         return true;
46                 }
47         }
48
49         return false;
50 }
51
52 static bool fw_is_builtin_firmware(const struct firmware *fw)
53 {
54         struct builtin_fw *b_fw;
55
56         for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
57                 if (fw->data == b_fw->data)
58                         return true;
59
60         return false;
61 }
62
63 #else /* Module case - no builtin firmware support */
64
65 static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
66 {
67         return false;
68 }
69
70 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
71 {
72         return false;
73 }
74 #endif
75
76 enum {
77         FW_STATUS_LOADING,
78         FW_STATUS_DONE,
79         FW_STATUS_ABORT,
80 };
81
82 static int loading_timeout = 60;        /* In seconds */
83
84 static inline long firmware_loading_timeout(void)
85 {
86         return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
87 }
88
89 struct firmware_cache {
90         /* firmware_buf instance will be added into the below list */
91         spinlock_t lock;
92         struct list_head head;
93 };
94
95 struct firmware_buf {
96         struct kref ref;
97         struct list_head list;
98         struct completion completion;
99         struct firmware_cache *fwc;
100         unsigned long status;
101         void *data;
102         size_t size;
103         struct page **pages;
104         int nr_pages;
105         int page_array_size;
106         char fw_id[];
107 };
108
109 struct firmware_priv {
110         struct timer_list timeout;
111         bool nowait;
112         struct device dev;
113         struct firmware_buf *buf;
114         struct firmware *fw;
115 };
116
117 struct fw_name_devm {
118         unsigned long magic;
119         char name[];
120 };
121
122 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
123
124 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
125  * guarding for corner cases a global lock should be OK */
126 static DEFINE_MUTEX(fw_lock);
127
128 static struct firmware_cache fw_cache;
129
130 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
131                                               struct firmware_cache *fwc)
132 {
133         struct firmware_buf *buf;
134
135         buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
136
137         if (!buf)
138                 return buf;
139
140         kref_init(&buf->ref);
141         strcpy(buf->fw_id, fw_name);
142         buf->fwc = fwc;
143         init_completion(&buf->completion);
144
145         pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
146
147         return buf;
148 }
149
150 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
151 {
152         struct firmware_buf *tmp;
153         struct firmware_cache *fwc = &fw_cache;
154
155         list_for_each_entry(tmp, &fwc->head, list)
156                 if (!strcmp(tmp->fw_id, fw_name))
157                         return tmp;
158         return NULL;
159 }
160
161 static int fw_lookup_and_allocate_buf(const char *fw_name,
162                                       struct firmware_cache *fwc,
163                                       struct firmware_buf **buf)
164 {
165         struct firmware_buf *tmp;
166
167         spin_lock(&fwc->lock);
168         tmp = __fw_lookup_buf(fw_name);
169         if (tmp) {
170                 kref_get(&tmp->ref);
171                 spin_unlock(&fwc->lock);
172                 *buf = tmp;
173                 return 1;
174         }
175         tmp = __allocate_fw_buf(fw_name, fwc);
176         if (tmp)
177                 list_add(&tmp->list, &fwc->head);
178         spin_unlock(&fwc->lock);
179
180         *buf = tmp;
181
182         return tmp ? 0 : -ENOMEM;
183 }
184
185 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
186 {
187         struct firmware_buf *tmp;
188         struct firmware_cache *fwc = &fw_cache;
189
190         spin_lock(&fwc->lock);
191         tmp = __fw_lookup_buf(fw_name);
192         spin_unlock(&fwc->lock);
193
194         return tmp;
195 }
196
197 static void __fw_free_buf(struct kref *ref)
198 {
199         struct firmware_buf *buf = to_fwbuf(ref);
200         struct firmware_cache *fwc = buf->fwc;
201         int i;
202
203         pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
204                  __func__, buf->fw_id, buf, buf->data,
205                  (unsigned int)buf->size);
206
207         spin_lock(&fwc->lock);
208         list_del(&buf->list);
209         spin_unlock(&fwc->lock);
210
211         vunmap(buf->data);
212         for (i = 0; i < buf->nr_pages; i++)
213                 __free_page(buf->pages[i]);
214         kfree(buf->pages);
215         kfree(buf);
216 }
217
218 static void fw_free_buf(struct firmware_buf *buf)
219 {
220         kref_put(&buf->ref, __fw_free_buf);
221 }
222
223 static void __init fw_cache_init(void)
224 {
225         spin_lock_init(&fw_cache.lock);
226         INIT_LIST_HEAD(&fw_cache.head);
227 }
228
229 static struct firmware_priv *to_firmware_priv(struct device *dev)
230 {
231         return container_of(dev, struct firmware_priv, dev);
232 }
233
234 static void fw_load_abort(struct firmware_priv *fw_priv)
235 {
236         struct firmware_buf *buf = fw_priv->buf;
237
238         set_bit(FW_STATUS_ABORT, &buf->status);
239         complete_all(&buf->completion);
240 }
241
242 static ssize_t firmware_timeout_show(struct class *class,
243                                      struct class_attribute *attr,
244                                      char *buf)
245 {
246         return sprintf(buf, "%d\n", loading_timeout);
247 }
248
249 /**
250  * firmware_timeout_store - set number of seconds to wait for firmware
251  * @class: device class pointer
252  * @attr: device attribute pointer
253  * @buf: buffer to scan for timeout value
254  * @count: number of bytes in @buf
255  *
256  *      Sets the number of seconds to wait for the firmware.  Once
257  *      this expires an error will be returned to the driver and no
258  *      firmware will be provided.
259  *
260  *      Note: zero means 'wait forever'.
261  **/
262 static ssize_t firmware_timeout_store(struct class *class,
263                                       struct class_attribute *attr,
264                                       const char *buf, size_t count)
265 {
266         loading_timeout = simple_strtol(buf, NULL, 10);
267         if (loading_timeout < 0)
268                 loading_timeout = 0;
269
270         return count;
271 }
272
273 static struct class_attribute firmware_class_attrs[] = {
274         __ATTR(timeout, S_IWUSR | S_IRUGO,
275                 firmware_timeout_show, firmware_timeout_store),
276         __ATTR_NULL
277 };
278
279 static void fw_dev_release(struct device *dev)
280 {
281         struct firmware_priv *fw_priv = to_firmware_priv(dev);
282
283         kfree(fw_priv);
284
285         module_put(THIS_MODULE);
286 }
287
288 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
289 {
290         struct firmware_priv *fw_priv = to_firmware_priv(dev);
291
292         if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
293                 return -ENOMEM;
294         if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
295                 return -ENOMEM;
296         if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
297                 return -ENOMEM;
298
299         return 0;
300 }
301
302 static struct class firmware_class = {
303         .name           = "firmware",
304         .class_attrs    = firmware_class_attrs,
305         .dev_uevent     = firmware_uevent,
306         .dev_release    = fw_dev_release,
307 };
308
309 static ssize_t firmware_loading_show(struct device *dev,
310                                      struct device_attribute *attr, char *buf)
311 {
312         struct firmware_priv *fw_priv = to_firmware_priv(dev);
313         int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
314
315         return sprintf(buf, "%d\n", loading);
316 }
317
318 /* firmware holds the ownership of pages */
319 static void firmware_free_data(const struct firmware *fw)
320 {
321         WARN_ON(!fw->priv);
322         fw_free_buf(fw->priv);
323 }
324
325 /* Some architectures don't have PAGE_KERNEL_RO */
326 #ifndef PAGE_KERNEL_RO
327 #define PAGE_KERNEL_RO PAGE_KERNEL
328 #endif
329 /**
330  * firmware_loading_store - set value in the 'loading' control file
331  * @dev: device pointer
332  * @attr: device attribute pointer
333  * @buf: buffer to scan for loading control value
334  * @count: number of bytes in @buf
335  *
336  *      The relevant values are:
337  *
338  *       1: Start a load, discarding any previous partial load.
339  *       0: Conclude the load and hand the data to the driver code.
340  *      -1: Conclude the load with an error and discard any written data.
341  **/
342 static ssize_t firmware_loading_store(struct device *dev,
343                                       struct device_attribute *attr,
344                                       const char *buf, size_t count)
345 {
346         struct firmware_priv *fw_priv = to_firmware_priv(dev);
347         struct firmware_buf *fw_buf = fw_priv->buf;
348         int loading = simple_strtol(buf, NULL, 10);
349         int i;
350
351         mutex_lock(&fw_lock);
352
353         if (!fw_buf)
354                 goto out;
355
356         switch (loading) {
357         case 1:
358                 /* discarding any previous partial load */
359                 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
360                         for (i = 0; i < fw_buf->nr_pages; i++)
361                                 __free_page(fw_buf->pages[i]);
362                         kfree(fw_buf->pages);
363                         fw_buf->pages = NULL;
364                         fw_buf->page_array_size = 0;
365                         fw_buf->nr_pages = 0;
366                         set_bit(FW_STATUS_LOADING, &fw_buf->status);
367                 }
368                 break;
369         case 0:
370                 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
371                         set_bit(FW_STATUS_DONE, &fw_buf->status);
372                         clear_bit(FW_STATUS_LOADING, &fw_buf->status);
373                         complete_all(&fw_buf->completion);
374                         break;
375                 }
376                 /* fallthrough */
377         default:
378                 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
379                 /* fallthrough */
380         case -1:
381                 fw_load_abort(fw_priv);
382                 break;
383         }
384 out:
385         mutex_unlock(&fw_lock);
386         return count;
387 }
388
389 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
390
391 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
392                                   struct bin_attribute *bin_attr,
393                                   char *buffer, loff_t offset, size_t count)
394 {
395         struct device *dev = kobj_to_dev(kobj);
396         struct firmware_priv *fw_priv = to_firmware_priv(dev);
397         struct firmware_buf *buf;
398         ssize_t ret_count;
399
400         mutex_lock(&fw_lock);
401         buf = fw_priv->buf;
402         if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
403                 ret_count = -ENODEV;
404                 goto out;
405         }
406         if (offset > buf->size) {
407                 ret_count = 0;
408                 goto out;
409         }
410         if (count > buf->size - offset)
411                 count = buf->size - offset;
412
413         ret_count = count;
414
415         while (count) {
416                 void *page_data;
417                 int page_nr = offset >> PAGE_SHIFT;
418                 int page_ofs = offset & (PAGE_SIZE-1);
419                 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
420
421                 page_data = kmap(buf->pages[page_nr]);
422
423                 memcpy(buffer, page_data + page_ofs, page_cnt);
424
425                 kunmap(buf->pages[page_nr]);
426                 buffer += page_cnt;
427                 offset += page_cnt;
428                 count -= page_cnt;
429         }
430 out:
431         mutex_unlock(&fw_lock);
432         return ret_count;
433 }
434
435 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
436 {
437         struct firmware_buf *buf = fw_priv->buf;
438         int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
439
440         /* If the array of pages is too small, grow it... */
441         if (buf->page_array_size < pages_needed) {
442                 int new_array_size = max(pages_needed,
443                                          buf->page_array_size * 2);
444                 struct page **new_pages;
445
446                 new_pages = kmalloc(new_array_size * sizeof(void *),
447                                     GFP_KERNEL);
448                 if (!new_pages) {
449                         fw_load_abort(fw_priv);
450                         return -ENOMEM;
451                 }
452                 memcpy(new_pages, buf->pages,
453                        buf->page_array_size * sizeof(void *));
454                 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
455                        (new_array_size - buf->page_array_size));
456                 kfree(buf->pages);
457                 buf->pages = new_pages;
458                 buf->page_array_size = new_array_size;
459         }
460
461         while (buf->nr_pages < pages_needed) {
462                 buf->pages[buf->nr_pages] =
463                         alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
464
465                 if (!buf->pages[buf->nr_pages]) {
466                         fw_load_abort(fw_priv);
467                         return -ENOMEM;
468                 }
469                 buf->nr_pages++;
470         }
471         return 0;
472 }
473
474 /**
475  * firmware_data_write - write method for firmware
476  * @filp: open sysfs file
477  * @kobj: kobject for the device
478  * @bin_attr: bin_attr structure
479  * @buffer: buffer being written
480  * @offset: buffer offset for write in total data store area
481  * @count: buffer size
482  *
483  *      Data written to the 'data' attribute will be later handed to
484  *      the driver as a firmware image.
485  **/
486 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
487                                    struct bin_attribute *bin_attr,
488                                    char *buffer, loff_t offset, size_t count)
489 {
490         struct device *dev = kobj_to_dev(kobj);
491         struct firmware_priv *fw_priv = to_firmware_priv(dev);
492         struct firmware_buf *buf;
493         ssize_t retval;
494
495         if (!capable(CAP_SYS_RAWIO))
496                 return -EPERM;
497
498         mutex_lock(&fw_lock);
499         buf = fw_priv->buf;
500         if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
501                 retval = -ENODEV;
502                 goto out;
503         }
504
505         retval = fw_realloc_buffer(fw_priv, offset + count);
506         if (retval)
507                 goto out;
508
509         retval = count;
510
511         while (count) {
512                 void *page_data;
513                 int page_nr = offset >> PAGE_SHIFT;
514                 int page_ofs = offset & (PAGE_SIZE - 1);
515                 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
516
517                 page_data = kmap(buf->pages[page_nr]);
518
519                 memcpy(page_data + page_ofs, buffer, page_cnt);
520
521                 kunmap(buf->pages[page_nr]);
522                 buffer += page_cnt;
523                 offset += page_cnt;
524                 count -= page_cnt;
525         }
526
527         buf->size = max_t(size_t, offset, buf->size);
528 out:
529         mutex_unlock(&fw_lock);
530         return retval;
531 }
532
533 static struct bin_attribute firmware_attr_data = {
534         .attr = { .name = "data", .mode = 0644 },
535         .size = 0,
536         .read = firmware_data_read,
537         .write = firmware_data_write,
538 };
539
540 static void firmware_class_timeout(u_long data)
541 {
542         struct firmware_priv *fw_priv = (struct firmware_priv *) data;
543
544         fw_load_abort(fw_priv);
545 }
546
547 static struct firmware_priv *
548 fw_create_instance(struct firmware *firmware, const char *fw_name,
549                    struct device *device, bool uevent, bool nowait)
550 {
551         struct firmware_priv *fw_priv;
552         struct device *f_dev;
553
554         fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
555         if (!fw_priv) {
556                 dev_err(device, "%s: kmalloc failed\n", __func__);
557                 fw_priv = ERR_PTR(-ENOMEM);
558                 goto exit;
559         }
560
561         fw_priv->nowait = nowait;
562         fw_priv->fw = firmware;
563         setup_timer(&fw_priv->timeout,
564                     firmware_class_timeout, (u_long) fw_priv);
565
566         f_dev = &fw_priv->dev;
567
568         device_initialize(f_dev);
569         dev_set_name(f_dev, "%s", fw_name);
570         f_dev->parent = device;
571         f_dev->class = &firmware_class;
572 exit:
573         return fw_priv;
574 }
575
576 /* one pages buffer is mapped/unmapped only once */
577 static int fw_map_pages_buf(struct firmware_buf *buf)
578 {
579         buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
580         if (!buf->data)
581                 return -ENOMEM;
582         return 0;
583 }
584
585 /* store the pages buffer info firmware from buf */
586 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
587 {
588         fw->priv = buf;
589         fw->pages = buf->pages;
590         fw->size = buf->size;
591         fw->data = buf->data;
592
593         pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
594                  __func__, buf->fw_id, buf, buf->data,
595                  (unsigned int)buf->size);
596 }
597
598 static void fw_name_devm_release(struct device *dev, void *res)
599 {
600         struct fw_name_devm *fwn = res;
601
602         if (fwn->magic == (unsigned long)&fw_cache)
603                 pr_debug("%s: fw_name-%s devm-%p released\n",
604                                 __func__, fwn->name, res);
605 }
606
607 static int fw_devm_match(struct device *dev, void *res,
608                 void *match_data)
609 {
610         struct fw_name_devm *fwn = res;
611
612         return (fwn->magic == (unsigned long)&fw_cache) &&
613                 !strcmp(fwn->name, match_data);
614 }
615
616 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
617                 const char *name)
618 {
619         struct fw_name_devm *fwn;
620
621         fwn = devres_find(dev, fw_name_devm_release,
622                           fw_devm_match, (void *)name);
623         return fwn;
624 }
625
626 /* add firmware name into devres list */
627 static int fw_add_devm_name(struct device *dev, const char *name)
628 {
629         struct fw_name_devm *fwn;
630
631         fwn = fw_find_devm_name(dev, name);
632         if (fwn)
633                 return 1;
634
635         fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
636                            strlen(name) + 1, GFP_KERNEL);
637         if (!fwn)
638                 return -ENOMEM;
639
640         fwn->magic = (unsigned long)&fw_cache;
641         strcpy(fwn->name, name);
642         devres_add(dev, fwn);
643
644         return 0;
645 }
646
647 static void _request_firmware_cleanup(const struct firmware **firmware_p)
648 {
649         release_firmware(*firmware_p);
650         *firmware_p = NULL;
651 }
652
653 static struct firmware_priv *
654 _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
655                           struct device *device, bool uevent, bool nowait)
656 {
657         struct firmware *firmware;
658         struct firmware_priv *fw_priv = NULL;
659         struct firmware_buf *buf;
660         int ret;
661
662         if (!firmware_p)
663                 return ERR_PTR(-EINVAL);
664
665         *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
666         if (!firmware) {
667                 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
668                         __func__);
669                 return ERR_PTR(-ENOMEM);
670         }
671
672         if (fw_get_builtin_firmware(firmware, name)) {
673                 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
674                 return NULL;
675         }
676
677         ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
678         if (!ret)
679                 fw_priv = fw_create_instance(firmware, name, device,
680                                 uevent, nowait);
681
682         if (IS_ERR(fw_priv) || ret < 0) {
683                 kfree(firmware);
684                 *firmware_p = NULL;
685                 return ERR_PTR(-ENOMEM);
686         } else if (fw_priv) {
687                 fw_priv->buf = buf;
688
689                 /*
690                  * bind with 'buf' now to avoid warning in failure path
691                  * of requesting firmware.
692                  */
693                 firmware->priv = buf;
694                 return fw_priv;
695         }
696
697         /* share the cached buf, which is inprogessing or completed */
698  check_status:
699         mutex_lock(&fw_lock);
700         if (test_bit(FW_STATUS_ABORT, &buf->status)) {
701                 fw_priv = ERR_PTR(-ENOENT);
702                 _request_firmware_cleanup(firmware_p);
703                 goto exit;
704         } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
705                 fw_priv = NULL;
706                 fw_set_page_data(buf, firmware);
707                 goto exit;
708         }
709         mutex_unlock(&fw_lock);
710         wait_for_completion(&buf->completion);
711         goto check_status;
712
713 exit:
714         mutex_unlock(&fw_lock);
715         return fw_priv;
716 }
717
718 static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
719                                   long timeout)
720 {
721         int retval = 0;
722         struct device *f_dev = &fw_priv->dev;
723         struct firmware_buf *buf = fw_priv->buf;
724
725         dev_set_uevent_suppress(f_dev, true);
726
727         /* Need to pin this module until class device is destroyed */
728         __module_get(THIS_MODULE);
729
730         retval = device_add(f_dev);
731         if (retval) {
732                 dev_err(f_dev, "%s: device_register failed\n", __func__);
733                 goto err_put_dev;
734         }
735
736         retval = device_create_bin_file(f_dev, &firmware_attr_data);
737         if (retval) {
738                 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
739                 goto err_del_dev;
740         }
741
742         retval = device_create_file(f_dev, &dev_attr_loading);
743         if (retval) {
744                 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
745                 goto err_del_bin_attr;
746         }
747
748         if (uevent) {
749                 dev_set_uevent_suppress(f_dev, false);
750                 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
751                 if (timeout != MAX_SCHEDULE_TIMEOUT)
752                         mod_timer(&fw_priv->timeout,
753                                   round_jiffies_up(jiffies + timeout));
754
755                 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
756         }
757
758         wait_for_completion(&buf->completion);
759
760         del_timer_sync(&fw_priv->timeout);
761
762         mutex_lock(&fw_lock);
763         if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
764                 retval = -ENOENT;
765
766         /*
767          * add firmware name into devres list so that we can auto cache
768          * and uncache firmware for device.
769          *
770          * f_dev->parent may has been deleted already, but the problem
771          * should be fixed in devres or driver core.
772          */
773         if (!retval && f_dev->parent)
774                 fw_add_devm_name(f_dev->parent, buf->fw_id);
775
776         if (!retval)
777                 retval = fw_map_pages_buf(buf);
778
779         /* pass the pages buffer to driver at the last minute */
780         fw_set_page_data(buf, fw_priv->fw);
781
782         fw_priv->buf = NULL;
783         mutex_unlock(&fw_lock);
784
785         device_remove_file(f_dev, &dev_attr_loading);
786 err_del_bin_attr:
787         device_remove_bin_file(f_dev, &firmware_attr_data);
788 err_del_dev:
789         device_del(f_dev);
790 err_put_dev:
791         put_device(f_dev);
792         return retval;
793 }
794
795 /**
796  * request_firmware: - send firmware request and wait for it
797  * @firmware_p: pointer to firmware image
798  * @name: name of firmware file
799  * @device: device for which firmware is being loaded
800  *
801  *      @firmware_p will be used to return a firmware image by the name
802  *      of @name for device @device.
803  *
804  *      Should be called from user context where sleeping is allowed.
805  *
806  *      @name will be used as $FIRMWARE in the uevent environment and
807  *      should be distinctive enough not to be confused with any other
808  *      firmware image for this or any other device.
809  *
810  *      Caller must hold the reference count of @device.
811  **/
812 int
813 request_firmware(const struct firmware **firmware_p, const char *name,
814                  struct device *device)
815 {
816         struct firmware_priv *fw_priv;
817         int ret;
818
819         fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
820                                             false);
821         if (IS_ERR_OR_NULL(fw_priv))
822                 return PTR_RET(fw_priv);
823
824         ret = usermodehelper_read_trylock();
825         if (WARN_ON(ret)) {
826                 dev_err(device, "firmware: %s will not be loaded\n", name);
827         } else {
828                 ret = _request_firmware_load(fw_priv, true,
829                                         firmware_loading_timeout());
830                 usermodehelper_read_unlock();
831         }
832         if (ret)
833                 _request_firmware_cleanup(firmware_p);
834
835         return ret;
836 }
837
838 /**
839  * release_firmware: - release the resource associated with a firmware image
840  * @fw: firmware resource to release
841  **/
842 void release_firmware(const struct firmware *fw)
843 {
844         if (fw) {
845                 if (!fw_is_builtin_firmware(fw))
846                         firmware_free_data(fw);
847                 kfree(fw);
848         }
849 }
850
851 /* Async support */
852 struct firmware_work {
853         struct work_struct work;
854         struct module *module;
855         const char *name;
856         struct device *device;
857         void *context;
858         void (*cont)(const struct firmware *fw, void *context);
859         bool uevent;
860 };
861
862 static void request_firmware_work_func(struct work_struct *work)
863 {
864         struct firmware_work *fw_work;
865         const struct firmware *fw;
866         struct firmware_priv *fw_priv;
867         long timeout;
868         int ret;
869
870         fw_work = container_of(work, struct firmware_work, work);
871         fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
872                         fw_work->uevent, true);
873         if (IS_ERR_OR_NULL(fw_priv)) {
874                 ret = PTR_RET(fw_priv);
875                 goto out;
876         }
877
878         timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
879         if (timeout) {
880                 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
881                 usermodehelper_read_unlock();
882         } else {
883                 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
884                         fw_work->name);
885                 ret = -EAGAIN;
886         }
887         if (ret)
888                 _request_firmware_cleanup(&fw);
889
890  out:
891         fw_work->cont(fw, fw_work->context);
892         put_device(fw_work->device);
893
894         module_put(fw_work->module);
895         kfree(fw_work);
896 }
897
898 /**
899  * request_firmware_nowait - asynchronous version of request_firmware
900  * @module: module requesting the firmware
901  * @uevent: sends uevent to copy the firmware image if this flag
902  *      is non-zero else the firmware copy must be done manually.
903  * @name: name of firmware file
904  * @device: device for which firmware is being loaded
905  * @gfp: allocation flags
906  * @context: will be passed over to @cont, and
907  *      @fw may be %NULL if firmware request fails.
908  * @cont: function will be called asynchronously when the firmware
909  *      request is over.
910  *
911  *      Caller must hold the reference count of @device.
912  *
913  *      Asynchronous variant of request_firmware() for user contexts:
914  *              - sleep for as small periods as possible since it may
915  *              increase kernel boot time of built-in device drivers
916  *              requesting firmware in their ->probe() methods, if
917  *              @gfp is GFP_KERNEL.
918  *
919  *              - can't sleep at all if @gfp is GFP_ATOMIC.
920  **/
921 int
922 request_firmware_nowait(
923         struct module *module, bool uevent,
924         const char *name, struct device *device, gfp_t gfp, void *context,
925         void (*cont)(const struct firmware *fw, void *context))
926 {
927         struct firmware_work *fw_work;
928
929         fw_work = kzalloc(sizeof (struct firmware_work), gfp);
930         if (!fw_work)
931                 return -ENOMEM;
932
933         fw_work->module = module;
934         fw_work->name = name;
935         fw_work->device = device;
936         fw_work->context = context;
937         fw_work->cont = cont;
938         fw_work->uevent = uevent;
939
940         if (!try_module_get(module)) {
941                 kfree(fw_work);
942                 return -EFAULT;
943         }
944
945         get_device(fw_work->device);
946         INIT_WORK(&fw_work->work, request_firmware_work_func);
947         schedule_work(&fw_work->work);
948         return 0;
949 }
950
951 /**
952  * cache_firmware - cache one firmware image in kernel memory space
953  * @fw_name: the firmware image name
954  *
955  * Cache firmware in kernel memory so that drivers can use it when
956  * system isn't ready for them to request firmware image from userspace.
957  * Once it returns successfully, driver can use request_firmware or its
958  * nowait version to get the cached firmware without any interacting
959  * with userspace
960  *
961  * Return 0 if the firmware image has been cached successfully
962  * Return !0 otherwise
963  *
964  */
965 int cache_firmware(const char *fw_name)
966 {
967         int ret;
968         const struct firmware *fw;
969
970         pr_debug("%s: %s\n", __func__, fw_name);
971
972         ret = request_firmware(&fw, fw_name, NULL);
973         if (!ret)
974                 kfree(fw);
975
976         pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
977
978         return ret;
979 }
980
981 /**
982  * uncache_firmware - remove one cached firmware image
983  * @fw_name: the firmware image name
984  *
985  * Uncache one firmware image which has been cached successfully
986  * before.
987  *
988  * Return 0 if the firmware cache has been removed successfully
989  * Return !0 otherwise
990  *
991  */
992 int uncache_firmware(const char *fw_name)
993 {
994         struct firmware_buf *buf;
995         struct firmware fw;
996
997         pr_debug("%s: %s\n", __func__, fw_name);
998
999         if (fw_get_builtin_firmware(&fw, fw_name))
1000                 return 0;
1001
1002         buf = fw_lookup_buf(fw_name);
1003         if (buf) {
1004                 fw_free_buf(buf);
1005                 return 0;
1006         }
1007
1008         return -EINVAL;
1009 }
1010
1011 static int __init firmware_class_init(void)
1012 {
1013         fw_cache_init();
1014         return class_register(&firmware_class);
1015 }
1016
1017 static void __exit firmware_class_exit(void)
1018 {
1019         class_unregister(&firmware_class);
1020 }
1021
1022 fs_initcall(firmware_class_init);
1023 module_exit(firmware_class_exit);
1024
1025 EXPORT_SYMBOL(release_firmware);
1026 EXPORT_SYMBOL(request_firmware);
1027 EXPORT_SYMBOL(request_firmware_nowait);
1028 EXPORT_SYMBOL_GPL(cache_firmware);
1029 EXPORT_SYMBOL_GPL(uncache_firmware);