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