Skip to content

Commit ec60aaa

Browse files
committed
ACPI: button: Switch over to devres-based resource management
Switch over the ACPI button driver to devres-based resource management by making the following changes: * Use devm_kzalloc() for allocating button object memory. * Use devm_input_allocate_device() for allocating the input class device object. * Turn acpi_lid_remove_fs() into a devm cleanup action added by devm_acpi_lid_add_fs() which is a new wrapper around acpi_lid_add_fs(). * Add devm_acpi_button_init_wakeup() for initializing the wakeup source and make it add a custom devm action that will automatically remove the wakeup source registered by it. * Turn acpi_button_remove_event_handler() into a devm cleanup action added by devm_acpi_button_add_event_handler() which is a new wrapper around acpi_button_add_event_handler(). No intentional functional impact. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Link: https://patch.msgid.link/2283436.Mh6RI2rZIc@rafael.j.wysocki [ rjw: Rebased and removed unnecessary input device parent assignment ] Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 0da41a4 commit ec60aaa

1 file changed

Lines changed: 58 additions & 47 deletions

File tree

drivers/acpi/button.c

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,9 @@ static int acpi_lid_add_fs(struct acpi_button *button)
340340
return -ENODEV;
341341
}
342342

343-
static void acpi_lid_remove_fs(struct acpi_button *button)
343+
static void acpi_lid_remove_fs(void *data)
344344
{
345+
struct acpi_button *button = data;
345346
struct acpi_device *device = button->adev;
346347

347348
remove_proc_entry(ACPI_BUTTON_FILE_STATE,
@@ -355,6 +356,17 @@ static void acpi_lid_remove_fs(struct acpi_button *button)
355356
acpi_button_dir = NULL;
356357
}
357358

359+
static int devm_acpi_lid_add_fs(struct device *dev, struct acpi_button *button)
360+
{
361+
int ret;
362+
363+
ret = acpi_lid_add_fs(button);
364+
if (ret)
365+
return ret;
366+
367+
return devm_add_action_or_reset(dev, acpi_lid_remove_fs, button);
368+
}
369+
358370
static acpi_handle saved_lid_handle;
359371
static DEFINE_MUTEX(acpi_lid_lock);
360372

@@ -530,8 +542,20 @@ static acpi_notify_handler acpi_button_notify_handler(struct acpi_button *button
530542
return acpi_button_notify;
531543
}
532544

533-
static void acpi_button_remove_event_handler(struct acpi_button *button)
545+
static void acpi_button_wakeup_cleanup(void *data)
546+
{
547+
device_init_wakeup(data, false);
548+
}
549+
550+
static int devm_acpi_button_init_wakeup(struct device *dev)
534551
{
552+
device_init_wakeup(dev, true);
553+
return devm_add_action_or_reset(dev, acpi_button_wakeup_cleanup, dev);
554+
}
555+
556+
static void acpi_button_remove_event_handler(void *data)
557+
{
558+
struct acpi_button *button = data;
535559
struct acpi_device *adev = button->adev;
536560

537561
switch (adev->device_type) {
@@ -608,6 +632,19 @@ static int acpi_button_add_event_handler(struct acpi_button *button)
608632
return 0;
609633
}
610634

635+
static int devm_acpi_button_add_event_handler(struct device *dev,
636+
struct acpi_button *button)
637+
{
638+
int ret;
639+
640+
ret = acpi_button_add_event_handler(button);
641+
if (ret)
642+
return ret;
643+
644+
return devm_add_action_or_reset(dev, acpi_button_remove_event_handler,
645+
button);
646+
}
647+
611648
static int acpi_button_probe(struct platform_device *pdev)
612649
{
613650
struct device *dev = &pdev->dev;
@@ -627,19 +664,18 @@ static int acpi_button_probe(struct platform_device *pdev)
627664
lid_init_state == ACPI_BUTTON_LID_INIT_DISABLED)
628665
return -ENODEV;
629666

630-
button = kzalloc_obj(struct acpi_button);
667+
button = devm_kzalloc(dev, sizeof(*button), GFP_KERNEL);
631668
if (!button)
632669
return -ENOMEM;
633670

634671
platform_set_drvdata(pdev, button);
635672

636673
button->dev = dev;
637674
button->adev = device;
638-
input = input_allocate_device();
639-
if (!input) {
640-
error = -ENOMEM;
641-
goto err_free_button;
642-
}
675+
input = devm_input_allocate_device(dev);
676+
if (!input)
677+
return -ENOMEM;
678+
643679
button->input = input;
644680
button->type = button_type;
645681

@@ -651,11 +687,10 @@ static int acpi_button_probe(struct platform_device *pdev)
651687
input_set_capability(input, EV_SW, SW_LID);
652688
input->open = acpi_lid_input_open;
653689

654-
error = acpi_lid_add_fs(button);
655-
if (error) {
656-
input_free_device(input);
657-
goto err_free_button;
658-
}
690+
error = devm_acpi_lid_add_fs(dev, button);
691+
if (error)
692+
return error;
693+
659694
break;
660695

661696
case ACPI_BUTTON_TYPE_POWER:
@@ -674,9 +709,7 @@ static int acpi_button_probe(struct platform_device *pdev)
674709
break;
675710

676711
default:
677-
input_free_device(input);
678-
error = dev_err_probe(dev, -ENODEV, "Unrecognized button type\n");
679-
goto err_free_button;
712+
return dev_err_probe(dev, -ENODEV, "Unrecognized button type\n");
680713
}
681714

682715
snprintf(button->phys, sizeof(button->phys), "%s/button/input0",
@@ -685,20 +718,19 @@ static int acpi_button_probe(struct platform_device *pdev)
685718
input->phys = button->phys;
686719
input->id.bustype = BUS_HOST;
687720
input->id.product = button_type;
688-
input->dev.parent = dev;
689721

690722
input_set_drvdata(input, button);
691723
error = input_register_device(input);
692-
if (error) {
693-
input_free_device(input);
694-
goto err_remove_fs;
695-
}
724+
if (error)
725+
return error;
696726

697-
device_init_wakeup(button->dev, true);
727+
error = devm_acpi_button_init_wakeup(dev);
728+
if (error)
729+
return error;
698730

699-
error = acpi_button_add_event_handler(button);
731+
error = devm_acpi_button_add_event_handler(dev, button);
700732
if (error)
701-
goto err_input_unregister;
733+
return error;
702734

703735
if (button_type == ACPI_BUTTON_TYPE_LID) {
704736
/*
@@ -709,37 +741,16 @@ static int acpi_button_probe(struct platform_device *pdev)
709741
}
710742

711743
pr_info("%s [%s]\n", input->name, acpi_device_bid(device));
712-
return 0;
713744

714-
err_input_unregister:
715-
device_init_wakeup(button->dev, false);
716-
input_unregister_device(input);
717-
err_remove_fs:
718-
if (button_type == ACPI_BUTTON_TYPE_LID)
719-
acpi_lid_remove_fs(button);
720-
721-
err_free_button:
722-
kfree(button);
723-
return error;
745+
return 0;
724746
}
725747

726748
static void acpi_button_remove(struct platform_device *pdev)
727749
{
728750
struct acpi_button *button = platform_get_drvdata(pdev);
729-
struct acpi_device *adev = button->adev;
730-
731-
if (button->type == ACPI_BUTTON_TYPE_LID)
732-
acpi_lid_forget(adev);
733-
734-
acpi_button_remove_event_handler(button);
735751

736-
device_init_wakeup(button->dev, false);
737-
738-
input_unregister_device(button->input);
739752
if (button->type == ACPI_BUTTON_TYPE_LID)
740-
acpi_lid_remove_fs(button);
741-
742-
kfree(button);
753+
acpi_lid_forget(button->adev);
743754
}
744755

745756
static int param_set_lid_init_state(const char *val,

0 commit comments

Comments
 (0)