Re: [PATCH 1/1] leds: add "kickable" LED trigger

From: Jonas Bonn
Date: Sun Apr 15 2012 - 18:37:35 EST


Hmm... I think I messed up the --in-reply-to parameter to git
send-email. This is in reply to the
"LEDS-One-Shot-Timer-Trigger-implementation" thread.

Sorry about that.

/Jonas

On 16 April 2012 00:34, Jonas Bonn <jonas@xxxxxxxxxxxx> wrote:
>
> This LED trigger allows userspace to "kick" the LED so that it illuminates
> for a short period of time. ÂThat period is currently hard-coded to
> 200 ms, but that can be easily fixed by adding a sysfs attribute for
> the illumination time.
>
> The original motivation for this trigger was to provide a way for
> userspace to provide an activity indicator for data sent/received on a
> serial bus, along the lines of a network activity indicator on a NIC.
>
> Signed-off-by: Jonas Bonn <jonas@xxxxxxxxxxxx>
> ---
>
> Hi,
>
> I just stumbled across this mail thread today. ÂI've got this trigger
> that we've been using in another project that seems to fit the bill
> here. ÂIt should just be a matter of adding a sysfs attribute to set
> the "illumination" duration to get what you need for the vibrator interface.
>
> The interface is simple enough. ÂYou set the LED (vibrator) trigger to
> "kickable" and then you get a file "kick" in the sysfs directory for
> the led. ÂAny time you write to that file, the illumination timer is
> reset so that the LED shines for the next 200ms. ÂIf the LED is not
> kicked within 200 ms, the LED is extinguished.
>
> Feel free to modify this to fit your needs.
>
> Caveat: ÂI haven't even compile tested this on a recent kernel... we've
> been using this on a 2.6.32 kernel. ÂAs far as I know, though, the LED
> interfaces haven't changed recently, so this is probably usable as it.
>
> Best regards,
> Jonas
>
> Âdrivers/leds/ledtrig-kickable.c | Â111 +++++++++++++++++++++++++++++++++++++++
> Â1 files changed, 111 insertions(+), 0 deletions(-)
> Âcreate mode 100644 drivers/leds/ledtrig-kickable.c
>
> diff --git a/drivers/leds/ledtrig-kickable.c b/drivers/leds/ledtrig-kickable.c
> new file mode 100644
> index 0000000..50699b1
> --- /dev/null
> +++ b/drivers/leds/ledtrig-kickable.c
> @@ -0,0 +1,111 @@
> +/*
> + * LED Kernel "Kickable" Trigger
> + *
> + * Copyright 2012 Jonas Bonn <jonas@xxxxxxxxxxxx>
> + *
> + * 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
> + * published by the Free Software Foundation.
> + *
> + * This is a simple trigger that provides a file 'kick' for the LED's
> + * userspace interface. ÂWriting anything to the 'kick' file causes the
> + * LED to illuminate for 200 ms. ÂEverytime the LED is 'kicked', its
> + * timer is reset to a full 200 ms.
> + *
> + * This can be used as an activity indicator for userspace processes.
> + *
> + */
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/init.h>
> +#include <linux/device.h>
> +#include <linux/ctype.h>
> +#include <linux/leds.h>
> +#include "leds.h"
> +
> +struct kickable_trig_data {
> + Â Â Â struct timer_list timer;
> +};
> +
> +static void led_kickable_function(unsigned long data)
> +{
> + Â Â Â struct led_classdev *led_cdev = (struct led_classdev *) data;
> +
> + Â Â Â led_set_brightness(led_cdev, LED_OFF);
> +}
> +
> +static ssize_t kick_store(struct device *dev,
> + Â Â Â Â Â Â Â struct device_attribute *attr, const char *buf, size_t size)
> +{
> + Â Â Â struct led_classdev *led_cdev = dev_get_drvdata(dev);
> + Â Â Â struct kickable_trig_data *kdata = led_cdev->trigger_data;
> + Â Â Â unsigned long delay = 0;
> +
> + Â Â Â delay = msecs_to_jiffies(200);
> +
> + Â Â Â mod_timer(&kdata->timer, jiffies + delay);
> +
> + Â Â Â led_set_brightness(led_cdev, LED_FULL);
> +
> + Â Â Â return size;
> +}
> +
> +static DEVICE_ATTR(kick, 0200, NULL, kick_store);
> +
> +static void kickable_trig_activate(struct led_classdev *led_cdev)
> +{
> + Â Â Â struct kickable_trig_data *kickable_data;
> + Â Â Â int rc;
> +
> + Â Â Â kickable_data = kzalloc(sizeof(*kickable_data), GFP_KERNEL);
> + Â Â Â if (!kickable_data)
> + Â Â Â Â Â Â Â return;
> +
> + Â Â Â led_cdev->trigger_data = kickable_data;
> + Â Â Â setup_timer(&kickable_data->timer,
> + Â Â Â Â Â Â Â Â Â led_kickable_function, (unsigned long) led_cdev);
> +
> +
> + Â Â Â rc = device_create_file(led_cdev->dev, &dev_attr_kick);
> + Â Â Â if (rc)
> + Â Â Â Â Â Â Â return;
> +
> + Â Â Â led_set_brightness(led_cdev, LED_OFF);
> +}
> +
> +static void kickable_trig_deactivate(struct led_classdev *led_cdev)
> +{
> + Â Â Â struct kickable_trig_data *kdata = led_cdev->trigger_data;
> +
> + Â Â Â if (kdata) {
> + Â Â Â Â Â Â Â del_timer_sync(&kdata->timer);
> + Â Â Â Â Â Â Â kfree(kdata);
> + Â Â Â Â Â Â Â device_remove_file(led_cdev->dev, &dev_attr_kick);
> + Â Â Â }
> +
> + Â Â Â led_set_brightness(led_cdev, LED_OFF);
> +}
> +
> +static struct led_trigger kickable_led_trigger = {
> +    .name   = "kickable",
> + Â Â Â .activate = kickable_trig_activate,
> + Â Â Â .deactivate = kickable_trig_deactivate,
> +};
> +
> +static int __init kickable_trig_init(void)
> +{
> + Â Â Â return led_trigger_register(&kickable_led_trigger);
> +}
> +
> +static void __exit kickable_trig_exit(void)
> +{
> + Â Â Â led_trigger_unregister(&kickable_led_trigger);
> +}
> +
> +module_init(kickable_trig_init);
> +module_exit(kickable_trig_exit);
> +
> +MODULE_AUTHOR("Jonas Bonn <jonas@xxxxxxxxxxxx>");
> +MODULE_DESCRIPTION("Kickable LED trigger");
> +MODULE_LICENSE("GPL");
> --
> 1.7.0.4
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at Âhttp://vger.kernel.org/majordomo-info.html
> Please read the FAQ at Âhttp://www.tux.org/lkml/



--
Jonas Bonn
Stockholm, Sweden
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/