bb9e2db7c5fff91517938ed50e289de62f04eee5
[cascardo/linux.git] / drivers / base / firmware_class.c
1 /*
2  * firmware_class.c - Multi purpose firmware loading support
3  *
4  * Copyright (c) 2003 Manuel Estrada Sainz
5  *
6  * Please see Documentation/firmware_class/ for more information.
7  *
8  */
9
10 #include <linux/capability.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/timer.h>
15 #include <linux/vmalloc.h>
16 #include <linux/interrupt.h>
17 #include <linux/bitops.h>
18 #include <linux/mutex.h>
19 #include <linux/workqueue.h>
20 #include <linux/highmem.h>
21 #include <linux/firmware.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/file.h>
25 #include <linux/list.h>
26 #include <linux/async.h>
27 #include <linux/pm.h>
28 #include <linux/suspend.h>
29 #include <linux/syscore_ops.h>
30
31 #include <generated/utsrelease.h>
32
33 #include "base.h"
34
35 #define to_dev(obj) container_of(obj, struct device, kobj)
36
37 MODULE_AUTHOR("Manuel Estrada Sainz");
38 MODULE_DESCRIPTION("Multi purpose firmware loading support");
39 MODULE_LICENSE("GPL");
40
41 static const char *fw_path[] = {
42         "/lib/firmware/updates/" UTS_RELEASE,
43         "/lib/firmware/updates",
44         "/lib/firmware/" UTS_RELEASE,
45         "/lib/firmware"
46 };
47
48 /* Don't inline this: 'struct kstat' is biggish */
49 static noinline long fw_file_size(struct file *file)
50 {
51         struct kstat st;
52         if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
53                 return -1;
54         if (!S_ISREG(st.mode))
55                 return -1;
56         if (st.size != (long)st.size)
57                 return -1;
58         return st.size;
59 }
60
61 static bool fw_read_file_contents(struct file *file, struct firmware *fw)
62 {
63         long size;
64         char *buf;
65
66         size = fw_file_size(file);
67         if (size < 0)
68                 return false;
69         buf = vmalloc(size);
70         if (!buf)
71                 return false;
72         if (kernel_read(file, 0, buf, size) != size) {
73                 vfree(buf);
74                 return false;
75         }
76         fw->data = buf;
77         fw->size = size;
78         return true;
79 }
80
81 static bool fw_get_filesystem_firmware(struct firmware *fw, const char *name)
82 {
83         int i;
84         bool success = false;
85         char *path = __getname();
86
87         for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
88                 struct file *file;
89                 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], name);
90
91                 file = filp_open(path, O_RDONLY, 0);
92                 if (IS_ERR(file))
93                         continue;
94                 success = fw_read_file_contents(file, fw);
95                 fput(file);
96                 if (success)
97                         break;
98         }
99         __putname(path);
100         return success;
101 }
102
103 /* Builtin firmware support */
104
105 #ifdef CONFIG_FW_LOADER
106
107 extern struct builtin_fw __start_builtin_fw[];
108 extern struct builtin_fw __end_builtin_fw[];
109
110 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
111 {
112         struct builtin_fw *b_fw;
113
114         for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
115                 if (strcmp(name, b_fw->name) == 0) {
116                         fw->size = b_fw->size;
117                         fw->data = b_fw->data;
118                         return true;
119                 }
120         }
121
122         return false;
123 }
124
125 static bool fw_is_builtin_firmware(const struct firmware *fw)
126 {
127         struct builtin_fw *b_fw;
128
129         for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
130                 if (fw->data == b_fw->data)
131                         return true;
132
133         return false;
134 }
135
136 #else /* Module case - no builtin firmware support */
137
138 static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
139 {
140         return false;
141 }
142
143 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
144 {
145         return false;
146 }
147 #endif
148
149 enum {
150         FW_STATUS_LOADING,
151         FW_STATUS_DONE,
152         FW_STATUS_ABORT,
153 };
154
155 static int loading_timeout = 60;        /* In seconds */
156
157 static inline long firmware_loading_timeout(void)
158 {
159         return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
160 }
161
162 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
163  * guarding for corner cases a global lock should be OK */
164 static DEFINE_MUTEX(fw_lock);
165
166 struct firmware_priv {
167         struct completion completion;
168         struct firmware *fw;
169         unsigned long status;
170         struct page **pages;
171         int nr_pages;
172         int page_array_size;
173         struct timer_list timeout;
174         struct device dev;
175         bool nowait;
176         char fw_id[];
177 };
178
179 static struct firmware_priv *to_firmware_priv(struct device *dev)
180 {
181         return container_of(dev, struct firmware_priv, dev);
182 }
183
184 static void fw_load_abort(struct firmware_priv *fw_priv)
185 {
186         set_bit(FW_STATUS_ABORT, &fw_priv->status);
187         wmb();
188         complete(&fw_priv->completion);
189 }
190
191 static ssize_t firmware_timeout_show(struct class *class,
192                                      struct class_attribute *attr,
193                                      char *buf)
194 {
195         return sprintf(buf, "%d\n", loading_timeout);
196 }
197
198 /**
199  * firmware_timeout_store - set number of seconds to wait for firmware
200  * @class: device class pointer
201  * @attr: device attribute pointer
202  * @buf: buffer to scan for timeout value
203  * @count: number of bytes in @buf
204  *
205  *      Sets the number of seconds to wait for the firmware.  Once
206  *      this expires an error will be returned to the driver and no
207  *      firmware will be provided.
208  *
209  *      Note: zero means 'wait forever'.
210  **/
211 static ssize_t firmware_timeout_store(struct class *class,
212                                       struct class_attribute *attr,
213                                       const char *buf, size_t count)
214 {
215         loading_timeout = simple_strtol(buf, NULL, 10);
216         if (loading_timeout < 0)
217                 loading_timeout = 0;
218
219         return count;
220 }
221
222 static struct class_attribute firmware_class_attrs[] = {
223         __ATTR(timeout, S_IWUSR | S_IRUGO,
224                 firmware_timeout_show, firmware_timeout_store),
225         __ATTR_NULL
226 };
227
228 static void fw_dev_release(struct device *dev)
229 {
230         struct firmware_priv *fw_priv = to_firmware_priv(dev);
231         int i;
232
233         for (i = 0; i < fw_priv->nr_pages; i++)
234                 __free_page(fw_priv->pages[i]);
235         kfree(fw_priv->pages);
236         kfree(fw_priv);
237
238         module_put(THIS_MODULE);
239 }
240
241 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
242 {
243         struct firmware_priv *fw_priv = to_firmware_priv(dev);
244
245         if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
246                 return -ENOMEM;
247         if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
248                 return -ENOMEM;
249         if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
250                 return -ENOMEM;
251
252         return 0;
253 }
254
255 static struct class firmware_class = {
256         .name           = "firmware",
257         .class_attrs    = firmware_class_attrs,
258         .dev_uevent     = firmware_uevent,
259         .dev_release    = fw_dev_release,
260 };
261
262 static ssize_t firmware_loading_show(struct device *dev,
263                                      struct device_attribute *attr, char *buf)
264 {
265         struct firmware_priv *fw_priv = to_firmware_priv(dev);
266         int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
267
268         return sprintf(buf, "%d\n", loading);
269 }
270
271 static void firmware_free_data(const struct firmware *fw)
272 {
273         int i;
274         /* Loaded directly? */
275         if (!fw->priv) {
276                 vfree(fw->data);
277                 return;
278         }
279         vunmap(fw->data);
280         if (fw->pages) {
281                 for (i = 0; i < PFN_UP(fw->size); i++)
282                         __free_page(fw->pages[i]);
283                 kfree(fw->pages);
284         }
285 }
286
287 /* Some architectures don't have PAGE_KERNEL_RO */
288 #ifndef PAGE_KERNEL_RO
289 #define PAGE_KERNEL_RO PAGE_KERNEL
290 #endif
291 /**
292  * firmware_loading_store - set value in the 'loading' control file
293  * @dev: device pointer
294  * @attr: device attribute pointer
295  * @buf: buffer to scan for loading control value
296  * @count: number of bytes in @buf
297  *
298  *      The relevant values are:
299  *
300  *       1: Start a load, discarding any previous partial load.
301  *       0: Conclude the load and hand the data to the driver code.
302  *      -1: Conclude the load with an error and discard any written data.
303  **/
304 static ssize_t firmware_loading_store(struct device *dev,
305                                       struct device_attribute *attr,
306                                       const char *buf, size_t count)
307 {
308         struct firmware_priv *fw_priv = to_firmware_priv(dev);
309         int loading = simple_strtol(buf, NULL, 10);
310         int i;
311
312         mutex_lock(&fw_lock);
313
314         if (!fw_priv->fw)
315                 goto out;
316
317         switch (loading) {
318         case 1:
319                 firmware_free_data(fw_priv->fw);
320                 memset(fw_priv->fw, 0, sizeof(struct firmware));
321                 /* If the pages are not owned by 'struct firmware' */
322                 for (i = 0; i < fw_priv->nr_pages; i++)
323                         __free_page(fw_priv->pages[i]);
324                 kfree(fw_priv->pages);
325                 fw_priv->pages = NULL;
326                 fw_priv->page_array_size = 0;
327                 fw_priv->nr_pages = 0;
328                 set_bit(FW_STATUS_LOADING, &fw_priv->status);
329                 break;
330         case 0:
331                 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
332                         vunmap(fw_priv->fw->data);
333                         fw_priv->fw->data = vmap(fw_priv->pages,
334                                                  fw_priv->nr_pages,
335                                                  0, PAGE_KERNEL_RO);
336                         if (!fw_priv->fw->data) {
337                                 dev_err(dev, "%s: vmap() failed\n", __func__);
338                                 goto err;
339                         }
340                         /* Pages are now owned by 'struct firmware' */
341                         fw_priv->fw->pages = fw_priv->pages;
342                         fw_priv->pages = NULL;
343
344                         fw_priv->page_array_size = 0;
345                         fw_priv->nr_pages = 0;
346                         complete(&fw_priv->completion);
347                         clear_bit(FW_STATUS_LOADING, &fw_priv->status);
348                         break;
349                 }
350                 /* fallthrough */
351         default:
352                 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
353                 /* fallthrough */
354         case -1:
355         err:
356                 fw_load_abort(fw_priv);
357                 break;
358         }
359 out:
360         mutex_unlock(&fw_lock);
361         return count;
362 }
363
364 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
365
366 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
367                                   struct bin_attribute *bin_attr,
368                                   char *buffer, loff_t offset, size_t count)
369 {
370         struct device *dev = to_dev(kobj);
371         struct firmware_priv *fw_priv = to_firmware_priv(dev);
372         struct firmware *fw;
373         ssize_t ret_count;
374
375         mutex_lock(&fw_lock);
376         fw = fw_priv->fw;
377         if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
378                 ret_count = -ENODEV;
379                 goto out;
380         }
381         if (offset > fw->size) {
382                 ret_count = 0;
383                 goto out;
384         }
385         if (count > fw->size - offset)
386                 count = fw->size - offset;
387
388         ret_count = count;
389
390         while (count) {
391                 void *page_data;
392                 int page_nr = offset >> PAGE_SHIFT;
393                 int page_ofs = offset & (PAGE_SIZE-1);
394                 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
395
396                 page_data = kmap(fw_priv->pages[page_nr]);
397
398                 memcpy(buffer, page_data + page_ofs, page_cnt);
399
400                 kunmap(fw_priv->pages[page_nr]);
401                 buffer += page_cnt;
402                 offset += page_cnt;
403                 count -= page_cnt;
404         }
405 out:
406         mutex_unlock(&fw_lock);
407         return ret_count;
408 }
409
410 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
411 {
412         int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
413
414         /* If the array of pages is too small, grow it... */
415         if (fw_priv->page_array_size < pages_needed) {
416                 int new_array_size = max(pages_needed,
417                                          fw_priv->page_array_size * 2);
418                 struct page **new_pages;
419
420                 new_pages = kmalloc(new_array_size * sizeof(void *),
421                                     GFP_KERNEL);
422                 if (!new_pages) {
423                         fw_load_abort(fw_priv);
424                         return -ENOMEM;
425                 }
426                 memcpy(new_pages, fw_priv->pages,
427                        fw_priv->page_array_size * sizeof(void *));
428                 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
429                        (new_array_size - fw_priv->page_array_size));
430                 kfree(fw_priv->pages);
431                 fw_priv->pages = new_pages;
432                 fw_priv->page_array_size = new_array_size;
433         }
434
435         while (fw_priv->nr_pages < pages_needed) {
436                 fw_priv->pages[fw_priv->nr_pages] =
437                         alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
438
439                 if (!fw_priv->pages[fw_priv->nr_pages]) {
440                         fw_load_abort(fw_priv);
441                         return -ENOMEM;
442                 }
443                 fw_priv->nr_pages++;
444         }
445         return 0;
446 }
447
448 /**
449  * firmware_data_write - write method for firmware
450  * @filp: open sysfs file
451  * @kobj: kobject for the device
452  * @bin_attr: bin_attr structure
453  * @buffer: buffer being written
454  * @offset: buffer offset for write in total data store area
455  * @count: buffer size
456  *
457  *      Data written to the 'data' attribute will be later handed to
458  *      the driver as a firmware image.
459  **/
460 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
461                                    struct bin_attribute *bin_attr,
462                                    char *buffer, loff_t offset, size_t count)
463 {
464         struct device *dev = to_dev(kobj);
465         struct firmware_priv *fw_priv = to_firmware_priv(dev);
466         struct firmware *fw;
467         ssize_t retval;
468
469         if (!capable(CAP_SYS_RAWIO))
470                 return -EPERM;
471
472         mutex_lock(&fw_lock);
473         fw = fw_priv->fw;
474         if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
475                 retval = -ENODEV;
476                 goto out;
477         }
478         retval = fw_realloc_buffer(fw_priv, offset + count);
479         if (retval)
480                 goto out;
481
482         retval = count;
483
484         while (count) {
485                 void *page_data;
486                 int page_nr = offset >> PAGE_SHIFT;
487                 int page_ofs = offset & (PAGE_SIZE - 1);
488                 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
489
490                 page_data = kmap(fw_priv->pages[page_nr]);
491
492                 memcpy(page_data + page_ofs, buffer, page_cnt);
493
494                 kunmap(fw_priv->pages[page_nr]);
495                 buffer += page_cnt;
496                 offset += page_cnt;
497                 count -= page_cnt;
498         }
499
500         fw->size = max_t(size_t, offset, fw->size);
501 out:
502         mutex_unlock(&fw_lock);
503         return retval;
504 }
505
506 static struct bin_attribute firmware_attr_data = {
507         .attr = { .name = "data", .mode = 0644 },
508         .size = 0,
509         .read = firmware_data_read,
510         .write = firmware_data_write,
511 };
512
513 static void firmware_class_timeout(u_long data)
514 {
515         struct firmware_priv *fw_priv = (struct firmware_priv *) data;
516
517         fw_load_abort(fw_priv);
518 }
519
520 static struct firmware_priv *
521 fw_create_instance(struct firmware *firmware, const char *fw_name,
522                    struct device *device, bool uevent, bool nowait)
523 {
524         struct firmware_priv *fw_priv;
525         struct device *f_dev;
526
527         fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
528         if (!fw_priv) {
529                 dev_err(device, "%s: kmalloc failed\n", __func__);
530                 return ERR_PTR(-ENOMEM);
531         }
532
533         fw_priv->fw = firmware;
534         fw_priv->nowait = nowait;
535         strcpy(fw_priv->fw_id, fw_name);
536         init_completion(&fw_priv->completion);
537         setup_timer(&fw_priv->timeout,
538                     firmware_class_timeout, (u_long) fw_priv);
539
540         f_dev = &fw_priv->dev;
541
542         device_initialize(f_dev);
543         dev_set_name(f_dev, "%s", dev_name(device));
544         f_dev->parent = device;
545         f_dev->class = &firmware_class;
546
547         return fw_priv;
548 }
549
550 static struct firmware_priv *
551 _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
552                           struct device *device, bool uevent, bool nowait)
553 {
554         struct firmware *firmware;
555         struct firmware_priv *fw_priv;
556
557         if (!firmware_p)
558                 return ERR_PTR(-EINVAL);
559
560         *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
561         if (!firmware) {
562                 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
563                         __func__);
564                 return ERR_PTR(-ENOMEM);
565         }
566
567         if (fw_get_builtin_firmware(firmware, name)) {
568                 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
569                 return NULL;
570         }
571
572         if (fw_get_filesystem_firmware(firmware, name)) {
573                 dev_dbg(device, "firmware: direct-loading firmware %s\n", name);
574                 return NULL;
575         }
576
577         fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
578         if (IS_ERR(fw_priv)) {
579                 release_firmware(firmware);
580                 *firmware_p = NULL;
581         }
582         return fw_priv;
583 }
584
585 static void _request_firmware_cleanup(const struct firmware **firmware_p)
586 {
587         release_firmware(*firmware_p);
588         *firmware_p = NULL;
589 }
590
591 static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
592                                   long timeout)
593 {
594         int retval = 0;
595         struct device *f_dev = &fw_priv->dev;
596
597         dev_set_uevent_suppress(f_dev, true);
598
599         /* Need to pin this module until class device is destroyed */
600         __module_get(THIS_MODULE);
601
602         retval = device_add(f_dev);
603         if (retval) {
604                 dev_err(f_dev, "%s: device_register failed\n", __func__);
605                 goto err_put_dev;
606         }
607
608         retval = device_create_bin_file(f_dev, &firmware_attr_data);
609         if (retval) {
610                 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
611                 goto err_del_dev;
612         }
613
614         retval = device_create_file(f_dev, &dev_attr_loading);
615         if (retval) {
616                 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
617                 goto err_del_bin_attr;
618         }
619
620         if (uevent) {
621                 dev_set_uevent_suppress(f_dev, false);
622                 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id);
623                 if (timeout != MAX_SCHEDULE_TIMEOUT)
624                         mod_timer(&fw_priv->timeout,
625                                   round_jiffies_up(jiffies + timeout));
626
627                 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
628         }
629
630         wait_for_completion(&fw_priv->completion);
631
632         set_bit(FW_STATUS_DONE, &fw_priv->status);
633         del_timer_sync(&fw_priv->timeout);
634
635         mutex_lock(&fw_lock);
636         if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
637                 retval = -ENOENT;
638         fw_priv->fw = NULL;
639         mutex_unlock(&fw_lock);
640
641         device_remove_file(f_dev, &dev_attr_loading);
642 err_del_bin_attr:
643         device_remove_bin_file(f_dev, &firmware_attr_data);
644 err_del_dev:
645         device_del(f_dev);
646 err_put_dev:
647         put_device(f_dev);
648         return retval;
649 }
650
651 /**
652  * request_firmware: - send firmware request and wait for it
653  * @firmware_p: pointer to firmware image
654  * @name: name of firmware file
655  * @device: device for which firmware is being loaded
656  *
657  *      @firmware_p will be used to return a firmware image by the name
658  *      of @name for device @device.
659  *
660  *      Should be called from user context where sleeping is allowed.
661  *
662  *      @name will be used as $FIRMWARE in the uevent environment and
663  *      should be distinctive enough not to be confused with any other
664  *      firmware image for this or any other device.
665  **/
666 int
667 request_firmware(const struct firmware **firmware_p, const char *name,
668                  struct device *device)
669 {
670         struct firmware_priv *fw_priv;
671         int ret;
672
673         fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
674                                             false);
675         if (IS_ERR_OR_NULL(fw_priv))
676                 return PTR_RET(fw_priv);
677
678         ret = usermodehelper_read_trylock();
679         if (ret) {
680                 kfree(fw_priv);
681                 dev_err(device, "firmware: %s will not be loaded\n", name);
682         } else {
683                 ret = _request_firmware_load(fw_priv, true,
684                                         firmware_loading_timeout());
685                 usermodehelper_read_unlock();
686         }
687         if (ret)
688                 _request_firmware_cleanup(firmware_p);
689
690         return ret;
691 }
692
693 /**
694  * release_firmware: - release the resource associated with a firmware image
695  * @fw: firmware resource to release
696  **/
697 void release_firmware(const struct firmware *fw)
698 {
699         if (fw) {
700                 if (!fw_is_builtin_firmware(fw))
701                         firmware_free_data(fw);
702                 kfree(fw);
703         }
704 }
705
706 /* Async support */
707 struct firmware_work {
708         struct work_struct work;
709         struct module *module;
710         const char *name;
711         struct device *device;
712         void *context;
713         void (*cont)(const struct firmware *fw, void *context);
714         bool uevent;
715 };
716
717 static void request_firmware_work_func(struct work_struct *work)
718 {
719         struct firmware_work *fw_work;
720         const struct firmware *fw;
721         struct firmware_priv *fw_priv;
722         long timeout;
723         int ret;
724
725         fw_work = container_of(work, struct firmware_work, work);
726         fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
727                         fw_work->uevent, true);
728         if (IS_ERR_OR_NULL(fw_priv)) {
729                 ret = PTR_RET(fw_priv);
730                 goto out;
731         }
732
733         timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
734         if (timeout) {
735                 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
736                 usermodehelper_read_unlock();
737         } else {
738                 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
739                         fw_work->name);
740                 ret = -EAGAIN;
741         }
742         if (ret)
743                 _request_firmware_cleanup(&fw);
744
745  out:
746         fw_work->cont(fw, fw_work->context);
747
748         module_put(fw_work->module);
749         kfree(fw_work);
750 }
751
752 /**
753  * request_firmware_nowait - asynchronous version of request_firmware
754  * @module: module requesting the firmware
755  * @uevent: sends uevent to copy the firmware image if this flag
756  *      is non-zero else the firmware copy must be done manually.
757  * @name: name of firmware file
758  * @device: device for which firmware is being loaded
759  * @gfp: allocation flags
760  * @context: will be passed over to @cont, and
761  *      @fw may be %NULL if firmware request fails.
762  * @cont: function will be called asynchronously when the firmware
763  *      request is over.
764  *
765  *      Asynchronous variant of request_firmware() for user contexts where
766  *      it is not possible to sleep for long time. It can't be called
767  *      in atomic contexts.
768  **/
769 int
770 request_firmware_nowait(
771         struct module *module, bool uevent,
772         const char *name, struct device *device, gfp_t gfp, void *context,
773         void (*cont)(const struct firmware *fw, void *context))
774 {
775         struct firmware_work *fw_work;
776
777         fw_work = kzalloc(sizeof (struct firmware_work), gfp);
778         if (!fw_work)
779                 return -ENOMEM;
780
781         fw_work->module = module;
782         fw_work->name = name;
783         fw_work->device = device;
784         fw_work->context = context;
785         fw_work->cont = cont;
786         fw_work->uevent = uevent;
787
788         if (!try_module_get(module)) {
789                 kfree(fw_work);
790                 return -EFAULT;
791         }
792
793         INIT_WORK(&fw_work->work, request_firmware_work_func);
794         schedule_work(&fw_work->work);
795         return 0;
796 }
797
798 static int __init firmware_class_init(void)
799 {
800         return class_register(&firmware_class);
801 }
802
803 static void __exit firmware_class_exit(void)
804 {
805         class_unregister(&firmware_class);
806 }
807
808 fs_initcall(firmware_class_init);
809 module_exit(firmware_class_exit);
810
811 EXPORT_SYMBOL(release_firmware);
812 EXPORT_SYMBOL(request_firmware);
813 EXPORT_SYMBOL(request_firmware_nowait);