Commit 81d55332 authored by Uwe Kleine-König's avatar Uwe Kleine-König Committed by Jacek Anaszewski

leds: oneshot trigger: simplifications from core changes

The trigger core learned error handling for the activate callback and
can handle device attributes now. This allows simplifying the driver
considerably. Note that .deactivate() is only called when .activate()
succeeded, so the check for .activated can go away in .deactivate().

Also make use of module_led_trigger() and do some minor coding style
improvements.
Signed-off-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Acked-by: default avatarPavel Machek <pavel@ucw.cz>
Signed-off-by: default avatarJacek Anaszewski <jacek.anaszewski@gmail.com>
parent 823f786a
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as * it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. * published by the Free Software Foundation.
*
*/ */
#include <linux/module.h> #include <linux/module.h>
...@@ -29,8 +28,8 @@ struct oneshot_trig_data { ...@@ -29,8 +28,8 @@ struct oneshot_trig_data {
static ssize_t led_shot(struct device *dev, static ssize_t led_shot(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size) struct device_attribute *attr, const char *buf, size_t size)
{ {
struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev *led_cdev = led_trigger_get_led(dev);
struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data; struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
led_blink_set_oneshot(led_cdev, led_blink_set_oneshot(led_cdev,
&led_cdev->blink_delay_on, &led_cdev->blink_delay_off, &led_cdev->blink_delay_on, &led_cdev->blink_delay_off,
...@@ -42,8 +41,7 @@ static ssize_t led_shot(struct device *dev, ...@@ -42,8 +41,7 @@ static ssize_t led_shot(struct device *dev,
static ssize_t led_invert_show(struct device *dev, static ssize_t led_invert_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct led_classdev *led_cdev = dev_get_drvdata(dev); struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data;
return sprintf(buf, "%u\n", oneshot_data->invert); return sprintf(buf, "%u\n", oneshot_data->invert);
} }
...@@ -51,8 +49,8 @@ static ssize_t led_invert_show(struct device *dev, ...@@ -51,8 +49,8 @@ static ssize_t led_invert_show(struct device *dev,
static ssize_t led_invert_store(struct device *dev, static ssize_t led_invert_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size) struct device_attribute *attr, const char *buf, size_t size)
{ {
struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev *led_cdev = led_trigger_get_led(dev);
struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data; struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev);
unsigned long state; unsigned long state;
int ret; int ret;
...@@ -73,7 +71,7 @@ static ssize_t led_invert_store(struct device *dev, ...@@ -73,7 +71,7 @@ static ssize_t led_invert_store(struct device *dev,
static ssize_t led_delay_on_show(struct device *dev, static ssize_t led_delay_on_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev *led_cdev = led_trigger_get_led(dev);
return sprintf(buf, "%lu\n", led_cdev->blink_delay_on); return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
} }
...@@ -81,7 +79,7 @@ static ssize_t led_delay_on_show(struct device *dev, ...@@ -81,7 +79,7 @@ static ssize_t led_delay_on_show(struct device *dev,
static ssize_t led_delay_on_store(struct device *dev, static ssize_t led_delay_on_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size) struct device_attribute *attr, const char *buf, size_t size)
{ {
struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev *led_cdev = led_trigger_get_led(dev);
unsigned long state; unsigned long state;
int ret; int ret;
...@@ -93,10 +91,11 @@ static ssize_t led_delay_on_store(struct device *dev, ...@@ -93,10 +91,11 @@ static ssize_t led_delay_on_store(struct device *dev,
return size; return size;
} }
static ssize_t led_delay_off_show(struct device *dev, static ssize_t led_delay_off_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev *led_cdev = led_trigger_get_led(dev);
return sprintf(buf, "%lu\n", led_cdev->blink_delay_off); return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
} }
...@@ -104,7 +103,7 @@ static ssize_t led_delay_off_show(struct device *dev, ...@@ -104,7 +103,7 @@ static ssize_t led_delay_off_show(struct device *dev,
static ssize_t led_delay_off_store(struct device *dev, static ssize_t led_delay_off_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size) struct device_attribute *attr, const char *buf, size_t size)
{ {
struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev *led_cdev = led_trigger_get_led(dev);
unsigned long state; unsigned long state;
int ret; int ret;
...@@ -122,61 +121,36 @@ static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store); ...@@ -122,61 +121,36 @@ static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store); static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
static DEVICE_ATTR(shot, 0200, NULL, led_shot); static DEVICE_ATTR(shot, 0200, NULL, led_shot);
static struct attribute *oneshot_trig_attrs[] = {
&dev_attr_delay_on.attr,
&dev_attr_delay_off.attr,
&dev_attr_invert.attr,
&dev_attr_shot.attr,
NULL
};
ATTRIBUTE_GROUPS(oneshot_trig);
static int oneshot_trig_activate(struct led_classdev *led_cdev) static int oneshot_trig_activate(struct led_classdev *led_cdev)
{ {
struct oneshot_trig_data *oneshot_data; struct oneshot_trig_data *oneshot_data;
int rc;
oneshot_data = kzalloc(sizeof(*oneshot_data), GFP_KERNEL); oneshot_data = kzalloc(sizeof(*oneshot_data), GFP_KERNEL);
if (!oneshot_data) if (!oneshot_data)
return 0; return -ENOMEM;
led_cdev->trigger_data = oneshot_data; led_set_trigger_data(led_cdev, oneshot_data);
rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
if (rc)
goto err_out_trig_data;
rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
if (rc)
goto err_out_delayon;
rc = device_create_file(led_cdev->dev, &dev_attr_invert);
if (rc)
goto err_out_delayoff;
rc = device_create_file(led_cdev->dev, &dev_attr_shot);
if (rc)
goto err_out_invert;
led_cdev->blink_delay_on = DEFAULT_DELAY; led_cdev->blink_delay_on = DEFAULT_DELAY;
led_cdev->blink_delay_off = DEFAULT_DELAY; led_cdev->blink_delay_off = DEFAULT_DELAY;
led_cdev->activated = true;
return 0;
err_out_invert:
device_remove_file(led_cdev->dev, &dev_attr_invert);
err_out_delayoff:
device_remove_file(led_cdev->dev, &dev_attr_delay_off);
err_out_delayon:
device_remove_file(led_cdev->dev, &dev_attr_delay_on);
err_out_trig_data:
kfree(led_cdev->trigger_data);
return 0; return 0;
} }
static void oneshot_trig_deactivate(struct led_classdev *led_cdev) static void oneshot_trig_deactivate(struct led_classdev *led_cdev)
{ {
struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data; struct oneshot_trig_data *oneshot_data = led_get_trigger_data(led_cdev);
if (led_cdev->activated) {
device_remove_file(led_cdev->dev, &dev_attr_delay_on);
device_remove_file(led_cdev->dev, &dev_attr_delay_off);
device_remove_file(led_cdev->dev, &dev_attr_invert);
device_remove_file(led_cdev->dev, &dev_attr_shot);
kfree(oneshot_data); kfree(oneshot_data);
led_cdev->activated = false;
}
/* Stop blinking */ /* Stop blinking */
led_set_brightness(led_cdev, LED_OFF); led_set_brightness(led_cdev, LED_OFF);
...@@ -186,20 +160,9 @@ static struct led_trigger oneshot_led_trigger = { ...@@ -186,20 +160,9 @@ static struct led_trigger oneshot_led_trigger = {
.name = "oneshot", .name = "oneshot",
.activate = oneshot_trig_activate, .activate = oneshot_trig_activate,
.deactivate = oneshot_trig_deactivate, .deactivate = oneshot_trig_deactivate,
.groups = oneshot_trig_groups,
}; };
module_led_trigger(oneshot_led_trigger);
static int __init oneshot_trig_init(void)
{
return led_trigger_register(&oneshot_led_trigger);
}
static void __exit oneshot_trig_exit(void)
{
led_trigger_unregister(&oneshot_led_trigger);
}
module_init(oneshot_trig_init);
module_exit(oneshot_trig_exit);
MODULE_AUTHOR("Fabio Baltieri <fabio.baltieri@gmail.com>"); MODULE_AUTHOR("Fabio Baltieri <fabio.baltieri@gmail.com>");
MODULE_DESCRIPTION("One-shot LED trigger"); MODULE_DESCRIPTION("One-shot LED trigger");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment