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