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