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