4 * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
6 * Based on ledtrig-timer.c by Richard Purdie <rpurdie@openedhand.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/ctype.h>
18 #include <linux/slab.h>
19 #include <linux/leds.h>
22 #define DEFAULT_DELAY 100
24 struct oneshot_trig_data
{
28 static ssize_t
led_shot(struct device
*dev
,
29 struct device_attribute
*attr
, const char *buf
, size_t size
)
31 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
32 struct oneshot_trig_data
*oneshot_data
= led_trigger_get_drvdata(dev
);
34 led_blink_set_oneshot(led_cdev
,
35 &led_cdev
->blink_delay_on
, &led_cdev
->blink_delay_off
,
36 oneshot_data
->invert
);
38 /* content is ignored */
41 static ssize_t
led_invert_show(struct device
*dev
,
42 struct device_attribute
*attr
, char *buf
)
44 struct oneshot_trig_data
*oneshot_data
= led_trigger_get_drvdata(dev
);
46 return sprintf(buf
, "%u\n", oneshot_data
->invert
);
49 static ssize_t
led_invert_store(struct device
*dev
,
50 struct device_attribute
*attr
, const char *buf
, size_t size
)
52 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
53 struct oneshot_trig_data
*oneshot_data
= led_trigger_get_drvdata(dev
);
57 ret
= kstrtoul(buf
, 0, &state
);
61 oneshot_data
->invert
= !!state
;
63 if (oneshot_data
->invert
)
64 led_set_brightness_nosleep(led_cdev
, LED_FULL
);
66 led_set_brightness_nosleep(led_cdev
, LED_OFF
);
71 static ssize_t
led_delay_on_show(struct device
*dev
,
72 struct device_attribute
*attr
, char *buf
)
74 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
76 return sprintf(buf
, "%lu\n", led_cdev
->blink_delay_on
);
79 static ssize_t
led_delay_on_store(struct device
*dev
,
80 struct device_attribute
*attr
, const char *buf
, size_t size
)
82 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
86 ret
= kstrtoul(buf
, 0, &state
);
90 led_cdev
->blink_delay_on
= state
;
95 static ssize_t
led_delay_off_show(struct device
*dev
,
96 struct device_attribute
*attr
, char *buf
)
98 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
100 return sprintf(buf
, "%lu\n", led_cdev
->blink_delay_off
);
103 static ssize_t
led_delay_off_store(struct device
*dev
,
104 struct device_attribute
*attr
, const char *buf
, size_t size
)
106 struct led_classdev
*led_cdev
= led_trigger_get_led(dev
);
110 ret
= kstrtoul(buf
, 0, &state
);
114 led_cdev
->blink_delay_off
= state
;
119 static DEVICE_ATTR(delay_on
, 0644, led_delay_on_show
, led_delay_on_store
);
120 static DEVICE_ATTR(delay_off
, 0644, led_delay_off_show
, led_delay_off_store
);
121 static DEVICE_ATTR(invert
, 0644, led_invert_show
, led_invert_store
);
122 static DEVICE_ATTR(shot
, 0200, NULL
, led_shot
);
124 static struct attribute
*oneshot_trig_attrs
[] = {
125 &dev_attr_delay_on
.attr
,
126 &dev_attr_delay_off
.attr
,
127 &dev_attr_invert
.attr
,
131 ATTRIBUTE_GROUPS(oneshot_trig
);
133 static int oneshot_trig_activate(struct led_classdev
*led_cdev
)
135 struct oneshot_trig_data
*oneshot_data
;
137 oneshot_data
= kzalloc(sizeof(*oneshot_data
), GFP_KERNEL
);
141 led_set_trigger_data(led_cdev
, oneshot_data
);
143 led_cdev
->blink_delay_on
= DEFAULT_DELAY
;
144 led_cdev
->blink_delay_off
= DEFAULT_DELAY
;
149 static void oneshot_trig_deactivate(struct led_classdev
*led_cdev
)
151 struct oneshot_trig_data
*oneshot_data
= led_get_trigger_data(led_cdev
);
156 led_set_brightness(led_cdev
, LED_OFF
);
159 static struct led_trigger oneshot_led_trigger
= {
161 .activate
= oneshot_trig_activate
,
162 .deactivate
= oneshot_trig_deactivate
,
163 .groups
= oneshot_trig_groups
,
165 module_led_trigger(oneshot_led_trigger
);
167 MODULE_AUTHOR("Fabio Baltieri <fabio.baltieri@gmail.com>");
168 MODULE_DESCRIPTION("One-shot LED trigger");
169 MODULE_LICENSE("GPL v2");