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.c208
1 files changed, 108 insertions, 100 deletions
diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c
index 6c9387d646ec..5401814c874d 100644
--- a/drivers/base/firmware_class.c
+++ b/drivers/base/firmware_class.c
@@ -16,10 +16,11 @@
16#include <linux/interrupt.h> 16#include <linux/interrupt.h>
17#include <linux/bitops.h> 17#include <linux/bitops.h>
18#include <linux/mutex.h> 18#include <linux/mutex.h>
19#include <linux/kthread.h> 19#include <linux/workqueue.h>
20#include <linux/highmem.h> 20#include <linux/highmem.h>
21#include <linux/firmware.h> 21#include <linux/firmware.h>
22#include <linux/slab.h> 22#include <linux/slab.h>
23#include <linux/sched.h>
23 24
24#define to_dev(obj) container_of(obj, struct device, kobj) 25#define to_dev(obj) container_of(obj, struct device, kobj)
25 26
@@ -81,6 +82,11 @@ enum {
81 82
82static int loading_timeout = 60; /* In seconds */ 83static int loading_timeout = 60; /* In seconds */
83 84
85static inline long firmware_loading_timeout(void)
86{
87 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
88}
89
84/* fw_lock could be moved to 'struct firmware_priv' but since it is just 90/* fw_lock could be moved to 'struct firmware_priv' but since it is just
85 * guarding for corner cases a global lock should be OK */ 91 * guarding for corner cases a global lock should be OK */
86static DEFINE_MUTEX(fw_lock); 92static DEFINE_MUTEX(fw_lock);
@@ -440,13 +446,11 @@ fw_create_instance(struct firmware *firmware, const char *fw_name,
440{ 446{
441 struct firmware_priv *fw_priv; 447 struct firmware_priv *fw_priv;
442 struct device *f_dev; 448 struct device *f_dev;
443 int error;
444 449
445 fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL); 450 fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
446 if (!fw_priv) { 451 if (!fw_priv) {
447 dev_err(device, "%s: kmalloc failed\n", __func__); 452 dev_err(device, "%s: kmalloc failed\n", __func__);
448 error = -ENOMEM; 453 return ERR_PTR(-ENOMEM);
449 goto err_out;
450 } 454 }
451 455
452 fw_priv->fw = firmware; 456 fw_priv->fw = firmware;
@@ -463,98 +467,80 @@ fw_create_instance(struct firmware *firmware, const char *fw_name,
463 f_dev->parent = device; 467 f_dev->parent = device;
464 f_dev->class = &firmware_class; 468 f_dev->class = &firmware_class;
465 469
466 dev_set_uevent_suppress(f_dev, true);
467
468 /* Need to pin this module until class device is destroyed */
469 __module_get(THIS_MODULE);
470
471 error = device_add(f_dev);
472 if (error) {
473 dev_err(device, "%s: device_register failed\n", __func__);
474 goto err_put_dev;
475 }
476
477 error = device_create_bin_file(f_dev, &firmware_attr_data);
478 if (error) {
479 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
480 goto err_del_dev;
481 }
482
483 error = device_create_file(f_dev, &dev_attr_loading);
484 if (error) {
485 dev_err(device, "%s: device_create_file failed\n", __func__);
486 goto err_del_bin_attr;
487 }
488
489 if (uevent)
490 dev_set_uevent_suppress(f_dev, false);
491
492 return fw_priv; 470 return fw_priv;
493
494err_del_bin_attr:
495 device_remove_bin_file(f_dev, &firmware_attr_data);
496err_del_dev:
497 device_del(f_dev);
498err_put_dev:
499 put_device(f_dev);
500err_out:
501 return ERR_PTR(error);
502} 471}
503 472
504static void fw_destroy_instance(struct firmware_priv *fw_priv) 473static struct firmware_priv *
505{ 474_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
506 struct device *f_dev = &fw_priv->dev; 475 struct device *device, bool uevent, bool nowait)
507
508 device_remove_file(f_dev, &dev_attr_loading);
509 device_remove_bin_file(f_dev, &firmware_attr_data);
510 device_unregister(f_dev);
511}
512
513static int _request_firmware(const struct firmware **firmware_p,
514 const char *name, struct device *device,
515 bool uevent, bool nowait)
516{ 476{
517 struct firmware_priv *fw_priv;
518 struct firmware *firmware; 477 struct firmware *firmware;
519 int retval = 0; 478 struct firmware_priv *fw_priv;
520 479
521 if (!firmware_p) 480 if (!firmware_p)
522 return -EINVAL; 481 return ERR_PTR(-EINVAL);
523 482
524 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL); 483 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
525 if (!firmware) { 484 if (!firmware) {
526 dev_err(device, "%s: kmalloc(struct firmware) failed\n", 485 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
527 __func__); 486 __func__);
528 return -ENOMEM; 487 return ERR_PTR(-ENOMEM);
529 } 488 }
530 489
531 if (fw_get_builtin_firmware(firmware, name)) { 490 if (fw_get_builtin_firmware(firmware, name)) {
532 dev_dbg(device, "firmware: using built-in firmware %s\n", name); 491 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
533 return 0; 492 return NULL;
493 }
494
495 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
496 if (IS_ERR(fw_priv)) {
497 release_firmware(firmware);
498 *firmware_p = NULL;
534 } 499 }
500 return fw_priv;
501}
535 502
536 read_lock_usermodehelper(); 503static void _request_firmware_cleanup(const struct firmware **firmware_p)
504{
505 release_firmware(*firmware_p);
506 *firmware_p = NULL;
507}
537 508
538 if (WARN_ON(usermodehelper_is_disabled())) { 509static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
539 dev_err(device, "firmware: %s will not be loaded\n", name); 510 long timeout)
540 retval = -EBUSY; 511{
541 goto out; 512 int retval = 0;
513 struct device *f_dev = &fw_priv->dev;
514
515 dev_set_uevent_suppress(f_dev, true);
516
517 /* Need to pin this module until class device is destroyed */
518 __module_get(THIS_MODULE);
519
520 retval = device_add(f_dev);
521 if (retval) {
522 dev_err(f_dev, "%s: device_register failed\n", __func__);
523 goto err_put_dev;
542 } 524 }
543 525
544 if (uevent) 526 retval = device_create_bin_file(f_dev, &firmware_attr_data);
545 dev_dbg(device, "firmware: requesting %s\n", name); 527 if (retval) {
528 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
529 goto err_del_dev;
530 }
546 531
547 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait); 532 retval = device_create_file(f_dev, &dev_attr_loading);
548 if (IS_ERR(fw_priv)) { 533 if (retval) {
549 retval = PTR_ERR(fw_priv); 534 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
550 goto out; 535 goto err_del_bin_attr;
551 } 536 }
552 537
553 if (uevent) { 538 if (uevent) {
554 if (loading_timeout > 0) 539 dev_set_uevent_suppress(f_dev, false);
540 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id);
541 if (timeout != MAX_SCHEDULE_TIMEOUT)
555 mod_timer(&fw_priv->timeout, 542 mod_timer(&fw_priv->timeout,
556 round_jiffies_up(jiffies + 543 round_jiffies_up(jiffies + timeout));
557 loading_timeout * HZ));
558 544
559 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD); 545 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
560 } 546 }
@@ -570,16 +556,13 @@ static int _request_firmware(const struct firmware **firmware_p,
570 fw_priv->fw = NULL; 556 fw_priv->fw = NULL;
571 mutex_unlock(&fw_lock); 557 mutex_unlock(&fw_lock);
572 558
573 fw_destroy_instance(fw_priv); 559 device_remove_file(f_dev, &dev_attr_loading);
574 560err_del_bin_attr:
575out: 561 device_remove_bin_file(f_dev, &firmware_attr_data);
576 read_unlock_usermodehelper(); 562err_del_dev:
577 563 device_del(f_dev);
578 if (retval) { 564err_put_dev:
579 release_firmware(firmware); 565 put_device(f_dev);
580 *firmware_p = NULL;
581 }
582
583 return retval; 566 return retval;
584} 567}
585 568
@@ -602,7 +585,26 @@ int
602request_firmware(const struct firmware **firmware_p, const char *name, 585request_firmware(const struct firmware **firmware_p, const char *name,
603 struct device *device) 586 struct device *device)
604{ 587{
605 return _request_firmware(firmware_p, name, device, true, false); 588 struct firmware_priv *fw_priv;
589 int ret;
590
591 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
592 false);
593 if (IS_ERR_OR_NULL(fw_priv))
594 return PTR_RET(fw_priv);
595
596 ret = usermodehelper_read_trylock();
597 if (WARN_ON(ret)) {
598 dev_err(device, "firmware: %s will not be loaded\n", name);
599 } else {
600 ret = _request_firmware_load(fw_priv, true,
601 firmware_loading_timeout());
602 usermodehelper_read_unlock();
603 }
604 if (ret)
605 _request_firmware_cleanup(firmware_p);
606
607 return ret;
606} 608}
607 609
608/** 610/**
@@ -629,25 +631,39 @@ struct firmware_work {
629 bool uevent; 631 bool uevent;
630}; 632};
631 633
632static int request_firmware_work_func(void *arg) 634static void request_firmware_work_func(struct work_struct *work)
633{ 635{
634 struct firmware_work *fw_work = arg; 636 struct firmware_work *fw_work;
635 const struct firmware *fw; 637 const struct firmware *fw;
638 struct firmware_priv *fw_priv;
639 long timeout;
636 int ret; 640 int ret;
637 641
638 if (!arg) { 642 fw_work = container_of(work, struct firmware_work, work);
639 WARN_ON(1); 643 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
640 return 0; 644 fw_work->uevent, true);
645 if (IS_ERR_OR_NULL(fw_priv)) {
646 ret = PTR_RET(fw_priv);
647 goto out;
648 }
649
650 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
651 if (timeout) {
652 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
653 usermodehelper_read_unlock();
654 } else {
655 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
656 fw_work->name);
657 ret = -EAGAIN;
641 } 658 }
659 if (ret)
660 _request_firmware_cleanup(&fw);
642 661
643 ret = _request_firmware(&fw, fw_work->name, fw_work->device, 662 out:
644 fw_work->uevent, true);
645 fw_work->cont(fw, fw_work->context); 663 fw_work->cont(fw, fw_work->context);
646 664
647 module_put(fw_work->module); 665 module_put(fw_work->module);
648 kfree(fw_work); 666 kfree(fw_work);
649
650 return ret;
651} 667}
652 668
653/** 669/**
@@ -673,7 +689,6 @@ request_firmware_nowait(
673 const char *name, struct device *device, gfp_t gfp, void *context, 689 const char *name, struct device *device, gfp_t gfp, void *context,
674 void (*cont)(const struct firmware *fw, void *context)) 690 void (*cont)(const struct firmware *fw, void *context))
675{ 691{
676 struct task_struct *task;
677 struct firmware_work *fw_work; 692 struct firmware_work *fw_work;
678 693
679 fw_work = kzalloc(sizeof (struct firmware_work), gfp); 694 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
@@ -692,15 +707,8 @@ request_firmware_nowait(
692 return -EFAULT; 707 return -EFAULT;
693 } 708 }
694 709
695 task = kthread_run(request_firmware_work_func, fw_work, 710 INIT_WORK(&fw_work->work, request_firmware_work_func);
696 "firmware/%s", name); 711 schedule_work(&fw_work->work);
697 if (IS_ERR(task)) {
698 fw_work->cont(NULL, fw_work->context);
699 module_put(fw_work->module);
700 kfree(fw_work);
701 return PTR_ERR(task);
702 }
703
704 return 0; 712 return 0;
705} 713}
706 714