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