aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/firmware_class.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/base/firmware_class.c')
-rw-r--r--drivers/base/firmware_class.c206
1 files changed, 116 insertions, 90 deletions
diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c
index 985da11174e7..3f093b0dd217 100644
--- a/drivers/base/firmware_class.c
+++ b/drivers/base/firmware_class.c
@@ -27,6 +27,52 @@ MODULE_AUTHOR("Manuel Estrada Sainz");
27MODULE_DESCRIPTION("Multi purpose firmware loading support"); 27MODULE_DESCRIPTION("Multi purpose firmware loading support");
28MODULE_LICENSE("GPL"); 28MODULE_LICENSE("GPL");
29 29
30/* Builtin firmware support */
31
32#ifdef CONFIG_FW_LOADER
33
34extern struct builtin_fw __start_builtin_fw[];
35extern struct builtin_fw __end_builtin_fw[];
36
37static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
38{
39 struct builtin_fw *b_fw;
40
41 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
42 if (strcmp(name, b_fw->name) == 0) {
43 fw->size = b_fw->size;
44 fw->data = b_fw->data;
45 return true;
46 }
47 }
48
49 return false;
50}
51
52static bool fw_is_builtin_firmware(const struct firmware *fw)
53{
54 struct builtin_fw *b_fw;
55
56 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
57 if (fw->data == b_fw->data)
58 return true;
59
60 return false;
61}
62
63#else /* Module case - no builtin firmware support */
64
65static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
66{
67 return false;
68}
69
70static inline bool fw_is_builtin_firmware(const struct firmware *fw)
71{
72 return false;
73}
74#endif
75
30enum { 76enum {
31 FW_STATUS_LOADING, 77 FW_STATUS_LOADING,
32 FW_STATUS_DONE, 78 FW_STATUS_DONE,
@@ -40,7 +86,6 @@ static int loading_timeout = 60; /* In seconds */
40static DEFINE_MUTEX(fw_lock); 86static DEFINE_MUTEX(fw_lock);
41 87
42struct firmware_priv { 88struct firmware_priv {
43 char *fw_id;
44 struct completion completion; 89 struct completion completion;
45 struct bin_attribute attr_data; 90 struct bin_attribute attr_data;
46 struct firmware *fw; 91 struct firmware *fw;
@@ -48,18 +93,11 @@ struct firmware_priv {
48 struct page **pages; 93 struct page **pages;
49 int nr_pages; 94 int nr_pages;
50 int page_array_size; 95 int page_array_size;
51 const char *vdata;
52 struct timer_list timeout; 96 struct timer_list timeout;
97 bool nowait;
98 char fw_id[];
53}; 99};
54 100
55#ifdef CONFIG_FW_LOADER
56extern struct builtin_fw __start_builtin_fw[];
57extern struct builtin_fw __end_builtin_fw[];
58#else /* Module case. Avoid ifdefs later; it'll all optimise out */
59static struct builtin_fw *__start_builtin_fw;
60static struct builtin_fw *__end_builtin_fw;
61#endif
62
63static void 101static void
64fw_load_abort(struct firmware_priv *fw_priv) 102fw_load_abort(struct firmware_priv *fw_priv)
65{ 103{
@@ -100,9 +138,25 @@ firmware_timeout_store(struct class *class,
100 return count; 138 return count;
101} 139}
102 140
103static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store); 141static struct class_attribute firmware_class_attrs[] = {
142 __ATTR(timeout, S_IWUSR | S_IRUGO,
143 firmware_timeout_show, firmware_timeout_store),
144 __ATTR_NULL
145};
146
147static void fw_dev_release(struct device *dev)
148{
149 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
150 int i;
151
152 for (i = 0; i < fw_priv->nr_pages; i++)
153 __free_page(fw_priv->pages[i]);
154 kfree(fw_priv->pages);
155 kfree(fw_priv);
156 kfree(dev);
104 157
105static void fw_dev_release(struct device *dev); 158 module_put(THIS_MODULE);
159}
106 160
107static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) 161static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
108{ 162{
@@ -112,12 +166,15 @@ static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
112 return -ENOMEM; 166 return -ENOMEM;
113 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout)) 167 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
114 return -ENOMEM; 168 return -ENOMEM;
169 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
170 return -ENOMEM;
115 171
116 return 0; 172 return 0;
117} 173}
118 174
119static struct class firmware_class = { 175static struct class firmware_class = {
120 .name = "firmware", 176 .name = "firmware",
177 .class_attrs = firmware_class_attrs,
121 .dev_uevent = firmware_uevent, 178 .dev_uevent = firmware_uevent,
122 .dev_release = fw_dev_release, 179 .dev_release = fw_dev_release,
123}; 180};
@@ -130,6 +187,17 @@ static ssize_t firmware_loading_show(struct device *dev,
130 return sprintf(buf, "%d\n", loading); 187 return sprintf(buf, "%d\n", loading);
131} 188}
132 189
190static void firmware_free_data(const struct firmware *fw)
191{
192 int i;
193 vunmap(fw->data);
194 if (fw->pages) {
195 for (i = 0; i < PFN_UP(fw->size); i++)
196 __free_page(fw->pages[i]);
197 kfree(fw->pages);
198 }
199}
200
133/* Some architectures don't have PAGE_KERNEL_RO */ 201/* Some architectures don't have PAGE_KERNEL_RO */
134#ifndef PAGE_KERNEL_RO 202#ifndef PAGE_KERNEL_RO
135#define PAGE_KERNEL_RO PAGE_KERNEL 203#define PAGE_KERNEL_RO PAGE_KERNEL
@@ -162,21 +230,21 @@ static ssize_t firmware_loading_store(struct device *dev,
162 mutex_unlock(&fw_lock); 230 mutex_unlock(&fw_lock);
163 break; 231 break;
164 } 232 }
165 vfree(fw_priv->fw->data); 233 firmware_free_data(fw_priv->fw);
166 fw_priv->fw->data = NULL; 234 memset(fw_priv->fw, 0, sizeof(struct firmware));
235 /* If the pages are not owned by 'struct firmware' */
167 for (i = 0; i < fw_priv->nr_pages; i++) 236 for (i = 0; i < fw_priv->nr_pages; i++)
168 __free_page(fw_priv->pages[i]); 237 __free_page(fw_priv->pages[i]);
169 kfree(fw_priv->pages); 238 kfree(fw_priv->pages);
170 fw_priv->pages = NULL; 239 fw_priv->pages = NULL;
171 fw_priv->page_array_size = 0; 240 fw_priv->page_array_size = 0;
172 fw_priv->nr_pages = 0; 241 fw_priv->nr_pages = 0;
173 fw_priv->fw->size = 0;
174 set_bit(FW_STATUS_LOADING, &fw_priv->status); 242 set_bit(FW_STATUS_LOADING, &fw_priv->status);
175 mutex_unlock(&fw_lock); 243 mutex_unlock(&fw_lock);
176 break; 244 break;
177 case 0: 245 case 0:
178 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) { 246 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
179 vfree(fw_priv->fw->data); 247 vunmap(fw_priv->fw->data);
180 fw_priv->fw->data = vmap(fw_priv->pages, 248 fw_priv->fw->data = vmap(fw_priv->pages,
181 fw_priv->nr_pages, 249 fw_priv->nr_pages,
182 0, PAGE_KERNEL_RO); 250 0, PAGE_KERNEL_RO);
@@ -184,7 +252,10 @@ static ssize_t firmware_loading_store(struct device *dev,
184 dev_err(dev, "%s: vmap() failed\n", __func__); 252 dev_err(dev, "%s: vmap() failed\n", __func__);
185 goto err; 253 goto err;
186 } 254 }
187 /* Pages will be freed by vfree() */ 255 /* Pages are now owned by 'struct firmware' */
256 fw_priv->fw->pages = fw_priv->pages;
257 fw_priv->pages = NULL;
258
188 fw_priv->page_array_size = 0; 259 fw_priv->page_array_size = 0;
189 fw_priv->nr_pages = 0; 260 fw_priv->nr_pages = 0;
190 complete(&fw_priv->completion); 261 complete(&fw_priv->completion);
@@ -207,8 +278,9 @@ static ssize_t firmware_loading_store(struct device *dev,
207static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); 278static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
208 279
209static ssize_t 280static ssize_t
210firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr, 281firmware_data_read(struct file *filp, struct kobject *kobj,
211 char *buffer, loff_t offset, size_t count) 282 struct bin_attribute *bin_attr, char *buffer, loff_t offset,
283 size_t count)
212{ 284{
213 struct device *dev = to_dev(kobj); 285 struct device *dev = to_dev(kobj);
214 struct firmware_priv *fw_priv = dev_get_drvdata(dev); 286 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
@@ -291,6 +363,7 @@ fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
291 363
292/** 364/**
293 * firmware_data_write - write method for firmware 365 * firmware_data_write - write method for firmware
366 * @filp: open sysfs file
294 * @kobj: kobject for the device 367 * @kobj: kobject for the device
295 * @bin_attr: bin_attr structure 368 * @bin_attr: bin_attr structure
296 * @buffer: buffer being written 369 * @buffer: buffer being written
@@ -301,8 +374,9 @@ fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
301 * the driver as a firmware image. 374 * the driver as a firmware image.
302 **/ 375 **/
303static ssize_t 376static ssize_t
304firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr, 377firmware_data_write(struct file* filp, struct kobject *kobj,
305 char *buffer, loff_t offset, size_t count) 378 struct bin_attribute *bin_attr, char *buffer,
379 loff_t offset, size_t count)
306{ 380{
307 struct device *dev = to_dev(kobj); 381 struct device *dev = to_dev(kobj);
308 struct firmware_priv *fw_priv = dev_get_drvdata(dev); 382 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
@@ -353,21 +427,6 @@ static struct bin_attribute firmware_attr_data_tmpl = {
353 .write = firmware_data_write, 427 .write = firmware_data_write,
354}; 428};
355 429
356static void fw_dev_release(struct device *dev)
357{
358 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
359 int i;
360
361 for (i = 0; i < fw_priv->nr_pages; i++)
362 __free_page(fw_priv->pages[i]);
363 kfree(fw_priv->pages);
364 kfree(fw_priv->fw_id);
365 kfree(fw_priv);
366 kfree(dev);
367
368 module_put(THIS_MODULE);
369}
370
371static void 430static void
372firmware_class_timeout(u_long data) 431firmware_class_timeout(u_long data)
373{ 432{
@@ -379,8 +438,8 @@ static int fw_register_device(struct device **dev_p, const char *fw_name,
379 struct device *device) 438 struct device *device)
380{ 439{
381 int retval; 440 int retval;
382 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv), 441 struct firmware_priv *fw_priv =
383 GFP_KERNEL); 442 kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
384 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL); 443 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
385 444
386 *dev_p = NULL; 445 *dev_p = NULL;
@@ -391,16 +450,9 @@ static int fw_register_device(struct device **dev_p, const char *fw_name,
391 goto error_kfree; 450 goto error_kfree;
392 } 451 }
393 452
453 strcpy(fw_priv->fw_id, fw_name);
394 init_completion(&fw_priv->completion); 454 init_completion(&fw_priv->completion);
395 fw_priv->attr_data = firmware_attr_data_tmpl; 455 fw_priv->attr_data = firmware_attr_data_tmpl;
396 fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
397 if (!fw_priv->fw_id) {
398 dev_err(device, "%s: Firmware name allocation failed\n",
399 __func__);
400 retval = -ENOMEM;
401 goto error_kfree;
402 }
403
404 fw_priv->timeout.function = firmware_class_timeout; 456 fw_priv->timeout.function = firmware_class_timeout;
405 fw_priv->timeout.data = (u_long) fw_priv; 457 fw_priv->timeout.data = (u_long) fw_priv;
406 init_timer(&fw_priv->timeout); 458 init_timer(&fw_priv->timeout);
@@ -427,7 +479,7 @@ error_kfree:
427 479
428static int fw_setup_device(struct firmware *fw, struct device **dev_p, 480static int fw_setup_device(struct firmware *fw, struct device **dev_p,
429 const char *fw_name, struct device *device, 481 const char *fw_name, struct device *device,
430 int uevent) 482 int uevent, bool nowait)
431{ 483{
432 struct device *f_dev; 484 struct device *f_dev;
433 struct firmware_priv *fw_priv; 485 struct firmware_priv *fw_priv;
@@ -443,6 +495,8 @@ static int fw_setup_device(struct firmware *fw, struct device **dev_p,
443 495
444 fw_priv = dev_get_drvdata(f_dev); 496 fw_priv = dev_get_drvdata(f_dev);
445 497
498 fw_priv->nowait = nowait;
499
446 fw_priv->fw = fw; 500 fw_priv->fw = fw;
447 sysfs_bin_attr_init(&fw_priv->attr_data); 501 sysfs_bin_attr_init(&fw_priv->attr_data);
448 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data); 502 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
@@ -470,12 +524,11 @@ out:
470 524
471static int 525static int
472_request_firmware(const struct firmware **firmware_p, const char *name, 526_request_firmware(const struct firmware **firmware_p, const char *name,
473 struct device *device, int uevent) 527 struct device *device, int uevent, bool nowait)
474{ 528{
475 struct device *f_dev; 529 struct device *f_dev;
476 struct firmware_priv *fw_priv; 530 struct firmware_priv *fw_priv;
477 struct firmware *firmware; 531 struct firmware *firmware;
478 struct builtin_fw *builtin;
479 int retval; 532 int retval;
480 533
481 if (!firmware_p) 534 if (!firmware_p)
@@ -489,21 +542,16 @@ _request_firmware(const struct firmware **firmware_p, const char *name,
489 goto out; 542 goto out;
490 } 543 }
491 544
492 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw; 545 if (fw_get_builtin_firmware(firmware, name)) {
493 builtin++) { 546 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
494 if (strcmp(name, builtin->name))
495 continue;
496 dev_info(device, "firmware: using built-in firmware %s\n",
497 name);
498 firmware->size = builtin->size;
499 firmware->data = builtin->data;
500 return 0; 547 return 0;
501 } 548 }
502 549
503 if (uevent) 550 if (uevent)
504 dev_info(device, "firmware: requesting %s\n", name); 551 dev_dbg(device, "firmware: requesting %s\n", name);
505 552
506 retval = fw_setup_device(firmware, &f_dev, name, device, uevent); 553 retval = fw_setup_device(firmware, &f_dev, name, device,
554 uevent, nowait);
507 if (retval) 555 if (retval)
508 goto error_kfree_fw; 556 goto error_kfree_fw;
509 557
@@ -560,26 +608,18 @@ request_firmware(const struct firmware **firmware_p, const char *name,
560 struct device *device) 608 struct device *device)
561{ 609{
562 int uevent = 1; 610 int uevent = 1;
563 return _request_firmware(firmware_p, name, device, uevent); 611 return _request_firmware(firmware_p, name, device, uevent, false);
564} 612}
565 613
566/** 614/**
567 * release_firmware: - release the resource associated with a firmware image 615 * release_firmware: - release the resource associated with a firmware image
568 * @fw: firmware resource to release 616 * @fw: firmware resource to release
569 **/ 617 **/
570void 618void release_firmware(const struct firmware *fw)
571release_firmware(const struct firmware *fw)
572{ 619{
573 struct builtin_fw *builtin;
574
575 if (fw) { 620 if (fw) {
576 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw; 621 if (!fw_is_builtin_firmware(fw))
577 builtin++) { 622 firmware_free_data(fw);
578 if (fw->data == builtin->data)
579 goto free_fw;
580 }
581 vfree(fw->data);
582 free_fw:
583 kfree(fw); 623 kfree(fw);
584 } 624 }
585} 625}
@@ -606,7 +646,7 @@ request_firmware_work_func(void *arg)
606 return 0; 646 return 0;
607 } 647 }
608 ret = _request_firmware(&fw, fw_work->name, fw_work->device, 648 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
609 fw_work->uevent); 649 fw_work->uevent, true);
610 650
611 fw_work->cont(fw, fw_work->context); 651 fw_work->cont(fw, fw_work->context);
612 652
@@ -670,26 +710,12 @@ request_firmware_nowait(
670 return 0; 710 return 0;
671} 711}
672 712
673static int __init 713static int __init firmware_class_init(void)
674firmware_class_init(void)
675{ 714{
676 int error; 715 return class_register(&firmware_class);
677 error = class_register(&firmware_class);
678 if (error) {
679 printk(KERN_ERR "%s: class_register failed\n", __func__);
680 return error;
681 }
682 error = class_create_file(&firmware_class, &class_attr_timeout);
683 if (error) {
684 printk(KERN_ERR "%s: class_create_file failed\n",
685 __func__);
686 class_unregister(&firmware_class);
687 }
688 return error;
689
690} 716}
691static void __exit 717
692firmware_class_exit(void) 718static void __exit firmware_class_exit(void)
693{ 719{
694 class_unregister(&firmware_class); 720 class_unregister(&firmware_class);
695} 721}