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/init.h>
17 #include <linux/list.h>
18 #include <linux/spinlock.h>
19 #include <linux/device.h>
20 #include <linux/sysdev.h>
21 #include <linux/timer.h>
22 #include <linux/leds.h>
26 * Nests outside led_cdev->trigger_lock
28 static DEFINE_RWLOCK(triggers_list_lock
);
29 static LIST_HEAD(trigger_list
);
31 ssize_t
led_trigger_store(struct device
*dev
, struct device_attribute
*attr
,
32 const char *buf
, size_t count
)
34 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
35 char trigger_name
[TRIG_NAME_MAX
];
36 struct led_trigger
*trig
;
39 trigger_name
[sizeof(trigger_name
) - 1] = '\0';
40 strncpy(trigger_name
, buf
, sizeof(trigger_name
) - 1);
41 len
= strlen(trigger_name
);
43 if (len
&& trigger_name
[len
- 1] == '\n')
44 trigger_name
[len
- 1] = '\0';
46 if (!strcmp(trigger_name
, "none")) {
47 write_lock(&led_cdev
->trigger_lock
);
48 led_trigger_set(led_cdev
, NULL
);
49 write_unlock(&led_cdev
->trigger_lock
);
53 read_lock(&triggers_list_lock
);
54 list_for_each_entry(trig
, &trigger_list
, next_trig
) {
55 if (!strcmp(trigger_name
, trig
->name
)) {
56 write_lock(&led_cdev
->trigger_lock
);
57 led_trigger_set(led_cdev
, trig
);
58 write_unlock(&led_cdev
->trigger_lock
);
60 read_unlock(&triggers_list_lock
);
64 read_unlock(&triggers_list_lock
);
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 read_lock(&triggers_list_lock
);
78 read_lock(&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 read_unlock(&led_cdev
->trigger_lock
);
93 read_unlock(&triggers_list_lock
);
95 len
+= sprintf(len
+buf
, "\n");
99 void led_trigger_event(struct led_trigger
*trigger
,
100 enum led_brightness brightness
)
102 struct list_head
*entry
;
107 read_lock(&trigger
->leddev_list_lock
);
108 list_for_each(entry
, &trigger
->led_cdevs
) {
109 struct led_classdev
*led_cdev
;
111 led_cdev
= list_entry(entry
, struct led_classdev
, trig_list
);
112 led_set_brightness(led_cdev
, brightness
);
114 read_unlock(&trigger
->leddev_list_lock
);
117 /* Caller must ensure led_cdev->trigger_lock held */
118 void led_trigger_set(struct led_classdev
*led_cdev
, struct led_trigger
*trigger
)
122 /* Remove any existing trigger */
123 if (led_cdev
->trigger
) {
124 write_lock_irqsave(&led_cdev
->trigger
->leddev_list_lock
, flags
);
125 list_del(&led_cdev
->trig_list
);
126 write_unlock_irqrestore(&led_cdev
->trigger
->leddev_list_lock
, flags
);
127 if (led_cdev
->trigger
->deactivate
)
128 led_cdev
->trigger
->deactivate(led_cdev
);
129 led_set_brightness(led_cdev
, LED_OFF
);
132 write_lock_irqsave(&trigger
->leddev_list_lock
, flags
);
133 list_add_tail(&led_cdev
->trig_list
, &trigger
->led_cdevs
);
134 write_unlock_irqrestore(&trigger
->leddev_list_lock
, flags
);
135 if (trigger
->activate
)
136 trigger
->activate(led_cdev
);
138 led_cdev
->trigger
= trigger
;
141 void led_trigger_set_default(struct led_classdev
*led_cdev
)
143 struct led_trigger
*trig
;
145 if (!led_cdev
->default_trigger
)
148 read_lock(&triggers_list_lock
);
149 write_lock(&led_cdev
->trigger_lock
);
150 list_for_each_entry(trig
, &trigger_list
, next_trig
) {
151 if (!strcmp(led_cdev
->default_trigger
, trig
->name
))
152 led_trigger_set(led_cdev
, trig
);
154 write_unlock(&led_cdev
->trigger_lock
);
155 read_unlock(&triggers_list_lock
);
158 int led_trigger_register(struct led_trigger
*trigger
)
160 struct led_classdev
*led_cdev
;
162 rwlock_init(&trigger
->leddev_list_lock
);
163 INIT_LIST_HEAD(&trigger
->led_cdevs
);
165 /* Add to the list of led triggers */
166 write_lock(&triggers_list_lock
);
167 list_add_tail(&trigger
->next_trig
, &trigger_list
);
168 write_unlock(&triggers_list_lock
);
170 /* Register with any LEDs that have this as a default trigger */
171 read_lock(&leds_list_lock
);
172 list_for_each_entry(led_cdev
, &leds_list
, node
) {
173 write_lock(&led_cdev
->trigger_lock
);
174 if (!led_cdev
->trigger
&& led_cdev
->default_trigger
&&
175 !strcmp(led_cdev
->default_trigger
, trigger
->name
))
176 led_trigger_set(led_cdev
, trigger
);
177 write_unlock(&led_cdev
->trigger_lock
);
179 read_unlock(&leds_list_lock
);
184 void led_trigger_register_simple(const char *name
, struct led_trigger
**tp
)
186 struct led_trigger
*trigger
;
189 trigger
= kzalloc(sizeof(struct led_trigger
), GFP_KERNEL
);
192 trigger
->name
= name
;
193 err
= led_trigger_register(trigger
);
195 printk(KERN_WARNING
"LED trigger %s failed to register"
196 " (%d)\n", name
, err
);
198 printk(KERN_WARNING
"LED trigger %s failed to register"
199 " (no memory)\n", name
);
204 void led_trigger_unregister(struct led_trigger
*trigger
)
206 struct led_classdev
*led_cdev
;
208 /* Remove from the list of led triggers */
209 write_lock(&triggers_list_lock
);
210 list_del(&trigger
->next_trig
);
211 write_unlock(&triggers_list_lock
);
213 /* Remove anyone actively using this trigger */
214 read_lock(&leds_list_lock
);
215 list_for_each_entry(led_cdev
, &leds_list
, node
) {
216 write_lock(&led_cdev
->trigger_lock
);
217 if (led_cdev
->trigger
== trigger
)
218 led_trigger_set(led_cdev
, NULL
);
219 write_unlock(&led_cdev
->trigger_lock
);
221 read_unlock(&leds_list_lock
);
224 void led_trigger_unregister_simple(struct led_trigger
*trigger
)
227 led_trigger_unregister(trigger
);
231 /* Used by LED Class */
232 EXPORT_SYMBOL_GPL(led_trigger_set
);
233 EXPORT_SYMBOL_GPL(led_trigger_set_default
);
234 EXPORT_SYMBOL_GPL(led_trigger_show
);
235 EXPORT_SYMBOL_GPL(led_trigger_store
);
237 /* LED Trigger Interface */
238 EXPORT_SYMBOL_GPL(led_trigger_register
);
239 EXPORT_SYMBOL_GPL(led_trigger_unregister
);
241 /* Simple LED Tigger Interface */
242 EXPORT_SYMBOL_GPL(led_trigger_register_simple
);
243 EXPORT_SYMBOL_GPL(led_trigger_unregister_simple
);
244 EXPORT_SYMBOL_GPL(led_trigger_event
);
246 MODULE_AUTHOR("Richard Purdie");
247 MODULE_LICENSE("GPL");
248 MODULE_DESCRIPTION("LED Triggers Core");