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