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