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