The trigger core learned error handling for the activate callback and to handle device attributes. This allows to simplify the driver considerably. Note that .deactivate is only called when .new_activate succeeded, so the check for activated can go away. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@xxxxxxxxxxxxxx> --- drivers/leds/trigger/ledtrig-transient.c | 63 ++++++++---------------- 1 file changed, 20 insertions(+), 43 deletions(-) diff --git a/drivers/leds/trigger/ledtrig-transient.c b/drivers/leds/trigger/ledtrig-transient.c index 9d1769073562..0cd90bda857b 100644 --- a/drivers/leds/trigger/ledtrig-transient.c +++ b/drivers/leds/trigger/ledtrig-transient.c @@ -152,68 +152,45 @@ static DEVICE_ATTR(duration, 0644, transient_duration_show, transient_duration_store); static DEVICE_ATTR(state, 0644, transient_state_show, transient_state_store); -static void transient_trig_activate(struct led_classdev *led_cdev) +static struct attribute *transient_trig_attrs[] = { + &dev_attr_activate.attr, + &dev_attr_duration.attr, + &dev_attr_state.attr, + NULL +}; +ATTRIBUTE_GROUPS(transient_trig); + +static int transient_trig_activate(struct led_classdev *led_cdev) { - int rc; struct transient_trig_data *tdata; tdata = kzalloc(sizeof(struct transient_trig_data), GFP_KERNEL); - if (!tdata) { - dev_err(led_cdev->dev, - "unable to allocate transient trigger\n"); - return; - } + if (!tdata) + return -ENOMEM; + led_cdev->trigger_data = tdata; tdata->led_cdev = led_cdev; - rc = device_create_file(led_cdev->dev, &dev_attr_activate); - if (rc) - goto err_out; - - rc = device_create_file(led_cdev->dev, &dev_attr_duration); - if (rc) - goto err_out_duration; - - rc = device_create_file(led_cdev->dev, &dev_attr_state); - if (rc) - goto err_out_state; - timer_setup(&tdata->timer, transient_timer_function, 0); - led_cdev->activated = true; - - return; - -err_out_state: - device_remove_file(led_cdev->dev, &dev_attr_duration); -err_out_duration: - device_remove_file(led_cdev->dev, &dev_attr_activate); -err_out: - dev_err(led_cdev->dev, "unable to register transient trigger\n"); - led_cdev->trigger_data = NULL; - kfree(tdata); + + return 0; } static void transient_trig_deactivate(struct led_classdev *led_cdev) { struct transient_trig_data *transient_data = led_cdev->trigger_data; - if (led_cdev->activated) { - del_timer_sync(&transient_data->timer); - led_set_brightness_nosleep(led_cdev, - transient_data->restore_state); - device_remove_file(led_cdev->dev, &dev_attr_activate); - device_remove_file(led_cdev->dev, &dev_attr_duration); - device_remove_file(led_cdev->dev, &dev_attr_state); - led_cdev->trigger_data = NULL; - led_cdev->activated = false; - kfree(transient_data); - } + del_timer_sync(&transient_data->timer); + led_set_brightness_nosleep(led_cdev, + transient_data->restore_state); + kfree(transient_data); } static struct led_trigger transient_trigger = { .name = "transient", - .activate = transient_trig_activate, + .new_activate = transient_trig_activate, .deactivate = transient_trig_deactivate, + .groups = transient_trig_groups, }; static int __init transient_trig_init(void) -- 2.17.0