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