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