4 * Copyright 2005-2007 Openedhand Ltd.
6 * Author: 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.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/spinlock.h>
18 #include <linux/device.h>
19 #include <linux/timer.h>
20 #include <linux/rwsem.h>
21 #include <linux/leds.h>
22 #include <linux/slab.h>
26 * Nests outside led_cdev->trigger_lock
28 static DECLARE_RWSEM(triggers_list_lock
);
29 static LIST_HEAD(trigger_list
);
31 /* Used by LED Class */
33 ssize_t
led_trigger_store(struct device
*dev
, struct device_attribute
*attr
,
34 const char *buf
, size_t count
)
36 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
37 char trigger_name
[TRIG_NAME_MAX
];
38 struct led_trigger
*trig
;
41 trigger_name
[sizeof(trigger_name
) - 1] = '\0';
42 strncpy(trigger_name
, buf
, sizeof(trigger_name
) - 1);
43 len
= strlen(trigger_name
);
45 if (len
&& trigger_name
[len
- 1] == '\n')
46 trigger_name
[len
- 1] = '\0';
48 if (!strcmp(trigger_name
, "none")) {
49 led_trigger_remove(led_cdev
);
53 down_read(&triggers_list_lock
);
54 list_for_each_entry(trig
, &trigger_list
, next_trig
) {
55 if (!strcmp(trigger_name
, trig
->name
)) {
56 down_write(&led_cdev
->trigger_lock
);
57 led_trigger_set(led_cdev
, trig
);
58 up_write(&led_cdev
->trigger_lock
);
60 up_read(&triggers_list_lock
);
64 up_read(&triggers_list_lock
);
68 EXPORT_SYMBOL_GPL(led_trigger_store
);
70 ssize_t
led_trigger_show(struct device
*dev
, struct device_attribute
*attr
,
73 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
74 struct led_trigger
*trig
;
77 down_read(&triggers_list_lock
);
78 down_read(&led_cdev
->trigger_lock
);
80 if (!led_cdev
->trigger
)
81 len
+= sprintf(buf
+len
, "[none] ");
83 len
+= sprintf(buf
+len
, "none ");
85 list_for_each_entry(trig
, &trigger_list
, next_trig
) {
86 if (led_cdev
->trigger
&& !strcmp(led_cdev
->trigger
->name
,
88 len
+= sprintf(buf
+len
, "[%s] ", trig
->name
);
90 len
+= sprintf(buf
+len
, "%s ", trig
->name
);
92 up_read(&led_cdev
->trigger_lock
);
93 up_read(&triggers_list_lock
);
95 len
+= sprintf(len
+buf
, "\n");
98 EXPORT_SYMBOL_GPL(led_trigger_show
);
100 /* Caller must ensure led_cdev->trigger_lock held */
101 void led_trigger_set(struct led_classdev
*led_cdev
, struct led_trigger
*trig
)
108 name
= trig
? trig
->name
: "none";
109 event
= kasprintf(GFP_KERNEL
, "TRIGGER=%s", name
);
111 /* Remove any existing trigger */
112 if (led_cdev
->trigger
) {
113 write_lock_irqsave(&led_cdev
->trigger
->leddev_list_lock
, flags
);
114 list_del(&led_cdev
->trig_list
);
115 write_unlock_irqrestore(&led_cdev
->trigger
->leddev_list_lock
,
117 cancel_work_sync(&led_cdev
->set_brightness_work
);
118 led_stop_software_blink(led_cdev
);
119 if (led_cdev
->trigger
->deactivate
)
120 led_cdev
->trigger
->deactivate(led_cdev
);
121 led_cdev
->trigger
= NULL
;
122 led_set_brightness(led_cdev
, LED_OFF
);
125 write_lock_irqsave(&trig
->leddev_list_lock
, flags
);
126 list_add_tail(&led_cdev
->trig_list
, &trig
->led_cdevs
);
127 write_unlock_irqrestore(&trig
->leddev_list_lock
, flags
);
128 led_cdev
->trigger
= trig
;
130 trig
->activate(led_cdev
);
136 kobject_uevent_env(&led_cdev
->dev
->kobj
, KOBJ_CHANGE
, envp
);
140 EXPORT_SYMBOL_GPL(led_trigger_set
);
142 void led_trigger_remove(struct led_classdev
*led_cdev
)
144 down_write(&led_cdev
->trigger_lock
);
145 led_trigger_set(led_cdev
, NULL
);
146 up_write(&led_cdev
->trigger_lock
);
148 EXPORT_SYMBOL_GPL(led_trigger_remove
);
150 void led_trigger_set_default(struct led_classdev
*led_cdev
)
152 struct led_trigger
*trig
;
154 if (!led_cdev
->default_trigger
)
157 down_read(&triggers_list_lock
);
158 down_write(&led_cdev
->trigger_lock
);
159 list_for_each_entry(trig
, &trigger_list
, next_trig
) {
160 if (!strcmp(led_cdev
->default_trigger
, trig
->name
))
161 led_trigger_set(led_cdev
, trig
);
163 up_write(&led_cdev
->trigger_lock
);
164 up_read(&triggers_list_lock
);
166 EXPORT_SYMBOL_GPL(led_trigger_set_default
);
168 void led_trigger_rename_static(const char *name
, struct led_trigger
*trig
)
170 /* new name must be on a temporary string to prevent races */
171 BUG_ON(name
== trig
->name
);
173 down_write(&triggers_list_lock
);
174 /* this assumes that trig->name was originaly allocated to
175 * non constant storage */
176 strcpy((char *)trig
->name
, name
);
177 up_write(&triggers_list_lock
);
179 EXPORT_SYMBOL_GPL(led_trigger_rename_static
);
181 /* LED Trigger Interface */
183 int led_trigger_register(struct led_trigger
*trig
)
185 struct led_classdev
*led_cdev
;
186 struct led_trigger
*_trig
;
188 rwlock_init(&trig
->leddev_list_lock
);
189 INIT_LIST_HEAD(&trig
->led_cdevs
);
191 down_write(&triggers_list_lock
);
192 /* Make sure the trigger's name isn't already in use */
193 list_for_each_entry(_trig
, &trigger_list
, next_trig
) {
194 if (!strcmp(_trig
->name
, trig
->name
)) {
195 up_write(&triggers_list_lock
);
199 /* Add to the list of led triggers */
200 list_add_tail(&trig
->next_trig
, &trigger_list
);
201 up_write(&triggers_list_lock
);
203 /* Register with any LEDs that have this as a default trigger */
204 down_read(&leds_list_lock
);
205 list_for_each_entry(led_cdev
, &leds_list
, node
) {
206 down_write(&led_cdev
->trigger_lock
);
207 if (!led_cdev
->trigger
&& led_cdev
->default_trigger
&&
208 !strcmp(led_cdev
->default_trigger
, trig
->name
))
209 led_trigger_set(led_cdev
, trig
);
210 up_write(&led_cdev
->trigger_lock
);
212 up_read(&leds_list_lock
);
216 EXPORT_SYMBOL_GPL(led_trigger_register
);
218 void led_trigger_unregister(struct led_trigger
*trig
)
220 struct led_classdev
*led_cdev
;
222 if (list_empty_careful(&trig
->next_trig
))
225 /* Remove from the list of led triggers */
226 down_write(&triggers_list_lock
);
227 list_del_init(&trig
->next_trig
);
228 up_write(&triggers_list_lock
);
230 /* Remove anyone actively using this trigger */
231 down_read(&leds_list_lock
);
232 list_for_each_entry(led_cdev
, &leds_list
, node
) {
233 down_write(&led_cdev
->trigger_lock
);
234 if (led_cdev
->trigger
== trig
)
235 led_trigger_set(led_cdev
, NULL
);
236 up_write(&led_cdev
->trigger_lock
);
238 up_read(&leds_list_lock
);
240 EXPORT_SYMBOL_GPL(led_trigger_unregister
);
242 /* Simple LED Tigger Interface */
244 void led_trigger_event(struct led_trigger
*trig
,
245 enum led_brightness brightness
)
247 struct led_classdev
*led_cdev
;
252 read_lock(&trig
->leddev_list_lock
);
253 list_for_each_entry(led_cdev
, &trig
->led_cdevs
, trig_list
)
254 led_set_brightness(led_cdev
, brightness
);
255 read_unlock(&trig
->leddev_list_lock
);
257 EXPORT_SYMBOL_GPL(led_trigger_event
);
259 static void led_trigger_blink_setup(struct led_trigger
*trig
,
260 unsigned long *delay_on
,
261 unsigned long *delay_off
,
265 struct led_classdev
*led_cdev
;
270 read_lock(&trig
->leddev_list_lock
);
271 list_for_each_entry(led_cdev
, &trig
->led_cdevs
, trig_list
) {
273 led_blink_set_oneshot(led_cdev
, delay_on
, delay_off
,
276 led_blink_set(led_cdev
, delay_on
, delay_off
);
278 read_unlock(&trig
->leddev_list_lock
);
281 void led_trigger_blink(struct led_trigger
*trig
,
282 unsigned long *delay_on
,
283 unsigned long *delay_off
)
285 led_trigger_blink_setup(trig
, delay_on
, delay_off
, 0, 0);
287 EXPORT_SYMBOL_GPL(led_trigger_blink
);
289 void led_trigger_blink_oneshot(struct led_trigger
*trig
,
290 unsigned long *delay_on
,
291 unsigned long *delay_off
,
294 led_trigger_blink_setup(trig
, delay_on
, delay_off
, 1, invert
);
296 EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot
);
298 void led_trigger_register_simple(const char *name
, struct led_trigger
**tp
)
300 struct led_trigger
*trig
;
303 trig
= kzalloc(sizeof(struct led_trigger
), GFP_KERNEL
);
307 err
= led_trigger_register(trig
);
311 pr_warn("LED trigger %s failed to register (%d)\n",
315 pr_warn("LED trigger %s failed to register (no memory)\n",
320 EXPORT_SYMBOL_GPL(led_trigger_register_simple
);
322 void led_trigger_unregister_simple(struct led_trigger
*trig
)
325 led_trigger_unregister(trig
);
328 EXPORT_SYMBOL_GPL(led_trigger_unregister_simple
);
330 MODULE_AUTHOR("Richard Purdie");
331 MODULE_LICENSE("GPL");
332 MODULE_DESCRIPTION("LED Triggers Core");