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
+= scnprintf(buf
+len
, PAGE_SIZE
- len
, "[none] ");
83 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
, "none ");
85 list_for_each_entry(trig
, &trigger_list
, next_trig
) {
86 if (led_cdev
->trigger
&& !strcmp(led_cdev
->trigger
->name
,
88 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
, "[%s] ",
91 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
, "%s ",
94 up_read(&led_cdev
->trigger_lock
);
95 up_read(&triggers_list_lock
);
97 len
+= scnprintf(len
+buf
, PAGE_SIZE
- len
, "\n");
100 EXPORT_SYMBOL_GPL(led_trigger_show
);
102 /* Caller must ensure led_cdev->trigger_lock held */
103 void led_trigger_set(struct led_classdev
*led_cdev
, struct led_trigger
*trig
)
110 name
= trig
? trig
->name
: "none";
111 event
= kasprintf(GFP_KERNEL
, "TRIGGER=%s", name
);
113 /* Remove any existing trigger */
114 if (led_cdev
->trigger
) {
115 write_lock_irqsave(&led_cdev
->trigger
->leddev_list_lock
, flags
);
116 list_del(&led_cdev
->trig_list
);
117 write_unlock_irqrestore(&led_cdev
->trigger
->leddev_list_lock
,
119 cancel_work_sync(&led_cdev
->set_brightness_work
);
120 led_stop_software_blink(led_cdev
);
121 if (led_cdev
->trigger
->deactivate
)
122 led_cdev
->trigger
->deactivate(led_cdev
);
123 led_cdev
->trigger
= NULL
;
124 led_set_brightness(led_cdev
, LED_OFF
);
127 write_lock_irqsave(&trig
->leddev_list_lock
, flags
);
128 list_add_tail(&led_cdev
->trig_list
, &trig
->led_cdevs
);
129 write_unlock_irqrestore(&trig
->leddev_list_lock
, flags
);
130 led_cdev
->trigger
= trig
;
132 trig
->activate(led_cdev
);
138 kobject_uevent_env(&led_cdev
->dev
->kobj
, KOBJ_CHANGE
, envp
);
142 EXPORT_SYMBOL_GPL(led_trigger_set
);
144 void led_trigger_remove(struct led_classdev
*led_cdev
)
146 down_write(&led_cdev
->trigger_lock
);
147 led_trigger_set(led_cdev
, NULL
);
148 up_write(&led_cdev
->trigger_lock
);
150 EXPORT_SYMBOL_GPL(led_trigger_remove
);
152 void led_trigger_set_default(struct led_classdev
*led_cdev
)
154 struct led_trigger
*trig
;
156 if (!led_cdev
->default_trigger
)
159 down_read(&triggers_list_lock
);
160 down_write(&led_cdev
->trigger_lock
);
161 list_for_each_entry(trig
, &trigger_list
, next_trig
) {
162 if (!strcmp(led_cdev
->default_trigger
, trig
->name
))
163 led_trigger_set(led_cdev
, trig
);
165 up_write(&led_cdev
->trigger_lock
);
166 up_read(&triggers_list_lock
);
168 EXPORT_SYMBOL_GPL(led_trigger_set_default
);
170 void led_trigger_rename_static(const char *name
, struct led_trigger
*trig
)
172 /* new name must be on a temporary string to prevent races */
173 BUG_ON(name
== trig
->name
);
175 down_write(&triggers_list_lock
);
176 /* this assumes that trig->name was originaly allocated to
177 * non constant storage */
178 strcpy((char *)trig
->name
, name
);
179 up_write(&triggers_list_lock
);
181 EXPORT_SYMBOL_GPL(led_trigger_rename_static
);
183 /* LED Trigger Interface */
185 int led_trigger_register(struct led_trigger
*trig
)
187 struct led_classdev
*led_cdev
;
188 struct led_trigger
*_trig
;
190 rwlock_init(&trig
->leddev_list_lock
);
191 INIT_LIST_HEAD(&trig
->led_cdevs
);
193 down_write(&triggers_list_lock
);
194 /* Make sure the trigger's name isn't already in use */
195 list_for_each_entry(_trig
, &trigger_list
, next_trig
) {
196 if (!strcmp(_trig
->name
, trig
->name
)) {
197 up_write(&triggers_list_lock
);
201 /* Add to the list of led triggers */
202 list_add_tail(&trig
->next_trig
, &trigger_list
);
203 up_write(&triggers_list_lock
);
205 /* Register with any LEDs that have this as a default trigger */
206 down_read(&leds_list_lock
);
207 list_for_each_entry(led_cdev
, &leds_list
, node
) {
208 down_write(&led_cdev
->trigger_lock
);
209 if (!led_cdev
->trigger
&& led_cdev
->default_trigger
&&
210 !strcmp(led_cdev
->default_trigger
, trig
->name
))
211 led_trigger_set(led_cdev
, trig
);
212 up_write(&led_cdev
->trigger_lock
);
214 up_read(&leds_list_lock
);
218 EXPORT_SYMBOL_GPL(led_trigger_register
);
220 void led_trigger_unregister(struct led_trigger
*trig
)
222 struct led_classdev
*led_cdev
;
224 if (list_empty_careful(&trig
->next_trig
))
227 /* Remove from the list of led triggers */
228 down_write(&triggers_list_lock
);
229 list_del_init(&trig
->next_trig
);
230 up_write(&triggers_list_lock
);
232 /* Remove anyone actively using this trigger */
233 down_read(&leds_list_lock
);
234 list_for_each_entry(led_cdev
, &leds_list
, node
) {
235 down_write(&led_cdev
->trigger_lock
);
236 if (led_cdev
->trigger
== trig
)
237 led_trigger_set(led_cdev
, NULL
);
238 up_write(&led_cdev
->trigger_lock
);
240 up_read(&leds_list_lock
);
242 EXPORT_SYMBOL_GPL(led_trigger_unregister
);
244 /* Simple LED Tigger Interface */
246 void led_trigger_event(struct led_trigger
*trig
,
247 enum led_brightness brightness
)
249 struct led_classdev
*led_cdev
;
254 read_lock(&trig
->leddev_list_lock
);
255 list_for_each_entry(led_cdev
, &trig
->led_cdevs
, trig_list
)
256 led_set_brightness(led_cdev
, brightness
);
257 read_unlock(&trig
->leddev_list_lock
);
259 EXPORT_SYMBOL_GPL(led_trigger_event
);
261 static void led_trigger_blink_setup(struct led_trigger
*trig
,
262 unsigned long *delay_on
,
263 unsigned long *delay_off
,
267 struct led_classdev
*led_cdev
;
272 read_lock(&trig
->leddev_list_lock
);
273 list_for_each_entry(led_cdev
, &trig
->led_cdevs
, trig_list
) {
275 led_blink_set_oneshot(led_cdev
, delay_on
, delay_off
,
278 led_blink_set(led_cdev
, delay_on
, delay_off
);
280 read_unlock(&trig
->leddev_list_lock
);
283 void led_trigger_blink(struct led_trigger
*trig
,
284 unsigned long *delay_on
,
285 unsigned long *delay_off
)
287 led_trigger_blink_setup(trig
, delay_on
, delay_off
, 0, 0);
289 EXPORT_SYMBOL_GPL(led_trigger_blink
);
291 void led_trigger_blink_oneshot(struct led_trigger
*trig
,
292 unsigned long *delay_on
,
293 unsigned long *delay_off
,
296 led_trigger_blink_setup(trig
, delay_on
, delay_off
, 1, invert
);
298 EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot
);
300 void led_trigger_register_simple(const char *name
, struct led_trigger
**tp
)
302 struct led_trigger
*trig
;
305 trig
= kzalloc(sizeof(struct led_trigger
), GFP_KERNEL
);
309 err
= led_trigger_register(trig
);
313 pr_warn("LED trigger %s failed to register (%d)\n",
317 pr_warn("LED trigger %s failed to register (no memory)\n",
322 EXPORT_SYMBOL_GPL(led_trigger_register_simple
);
324 void led_trigger_unregister_simple(struct led_trigger
*trig
)
327 led_trigger_unregister(trig
);
330 EXPORT_SYMBOL_GPL(led_trigger_unregister_simple
);
332 MODULE_AUTHOR("Richard Purdie");
333 MODULE_LICENSE("GPL");
334 MODULE_DESCRIPTION("LED Triggers Core");