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