blob: 454fb0901f8211dc31ec6b86e6edac5bec774286 [file] [log] [blame]
Richard Purdiec3bc9952006-03-31 02:31:05 -08001/*
2 * LED Triggers Core
3 *
4 * Copyright 2005-2006 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <rpurdie@openedhand.com>
7 *
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.
11 *
12 */
13
Richard Purdiec3bc9952006-03-31 02:31:05 -080014#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>
23#include "leds.h"
24
25/*
26 * Nests outside led_cdev->trigger_lock
27 */
Ingo Molnar34af9462006-06-27 02:53:55 -070028static DEFINE_RWLOCK(triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080029static LIST_HEAD(trigger_list);
30
31ssize_t led_trigger_store(struct class_device *dev, const char *buf,
32 size_t count)
33{
34 struct led_classdev *led_cdev = class_get_devdata(dev);
35 char trigger_name[TRIG_NAME_MAX];
36 struct led_trigger *trig;
37 size_t len;
38
39 trigger_name[sizeof(trigger_name) - 1] = '\0';
40 strncpy(trigger_name, buf, sizeof(trigger_name) - 1);
41 len = strlen(trigger_name);
42
43 if (len && trigger_name[len - 1] == '\n')
44 trigger_name[len - 1] = '\0';
45
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);
50 return count;
51 }
52
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);
59
60 read_unlock(&triggers_list_lock);
61 return count;
62 }
63 }
64 read_unlock(&triggers_list_lock);
65
66 return -EINVAL;
67}
68
69
70ssize_t led_trigger_show(struct class_device *dev, char *buf)
71{
72 struct led_classdev *led_cdev = class_get_devdata(dev);
73 struct led_trigger *trig;
74 int len = 0;
75
76 read_lock(&triggers_list_lock);
77 read_lock(&led_cdev->trigger_lock);
78
79 if (!led_cdev->trigger)
80 len += sprintf(buf+len, "[none] ");
81 else
82 len += sprintf(buf+len, "none ");
83
84 list_for_each_entry(trig, &trigger_list, next_trig) {
85 if (led_cdev->trigger && !strcmp(led_cdev->trigger->name,
86 trig->name))
87 len += sprintf(buf+len, "[%s] ", trig->name);
88 else
89 len += sprintf(buf+len, "%s ", trig->name);
90 }
91 read_unlock(&led_cdev->trigger_lock);
92 read_unlock(&triggers_list_lock);
93
94 len += sprintf(len+buf, "\n");
95 return len;
96}
97
98void led_trigger_event(struct led_trigger *trigger,
99 enum led_brightness brightness)
100{
101 struct list_head *entry;
102
103 if (!trigger)
104 return;
105
106 read_lock(&trigger->leddev_list_lock);
107 list_for_each(entry, &trigger->led_cdevs) {
108 struct led_classdev *led_cdev;
109
110 led_cdev = list_entry(entry, struct led_classdev, trig_list);
111 led_set_brightness(led_cdev, brightness);
112 }
113 read_unlock(&trigger->leddev_list_lock);
114}
115
116/* Caller must ensure led_cdev->trigger_lock held */
117void led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trigger)
118{
119 unsigned long flags;
120
121 /* Remove any existing trigger */
122 if (led_cdev->trigger) {
123 write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
124 list_del(&led_cdev->trig_list);
125 write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, flags);
126 if (led_cdev->trigger->deactivate)
127 led_cdev->trigger->deactivate(led_cdev);
Paul Collinse3e5fc92006-09-30 23:27:41 -0700128 led_set_brightness(led_cdev, LED_OFF);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800129 }
130 if (trigger) {
131 write_lock_irqsave(&trigger->leddev_list_lock, flags);
132 list_add_tail(&led_cdev->trig_list, &trigger->led_cdevs);
133 write_unlock_irqrestore(&trigger->leddev_list_lock, flags);
134 if (trigger->activate)
135 trigger->activate(led_cdev);
136 }
137 led_cdev->trigger = trigger;
138}
139
140void led_trigger_set_default(struct led_classdev *led_cdev)
141{
142 struct led_trigger *trig;
143
144 if (!led_cdev->default_trigger)
145 return;
146
147 read_lock(&triggers_list_lock);
148 write_lock(&led_cdev->trigger_lock);
149 list_for_each_entry(trig, &trigger_list, next_trig) {
150 if (!strcmp(led_cdev->default_trigger, trig->name))
151 led_trigger_set(led_cdev, trig);
152 }
153 write_unlock(&led_cdev->trigger_lock);
154 read_unlock(&triggers_list_lock);
155}
156
157int led_trigger_register(struct led_trigger *trigger)
158{
159 struct led_classdev *led_cdev;
160
161 rwlock_init(&trigger->leddev_list_lock);
162 INIT_LIST_HEAD(&trigger->led_cdevs);
163
164 /* Add to the list of led triggers */
165 write_lock(&triggers_list_lock);
166 list_add_tail(&trigger->next_trig, &trigger_list);
167 write_unlock(&triggers_list_lock);
168
169 /* Register with any LEDs that have this as a default trigger */
170 read_lock(&leds_list_lock);
171 list_for_each_entry(led_cdev, &leds_list, node) {
172 write_lock(&led_cdev->trigger_lock);
173 if (!led_cdev->trigger && led_cdev->default_trigger &&
174 !strcmp(led_cdev->default_trigger, trigger->name))
175 led_trigger_set(led_cdev, trigger);
176 write_unlock(&led_cdev->trigger_lock);
177 }
178 read_unlock(&leds_list_lock);
179
180 return 0;
181}
182
183void led_trigger_register_simple(const char *name, struct led_trigger **tp)
184{
185 struct led_trigger *trigger;
186
187 trigger = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
188
189 if (trigger) {
190 trigger->name = name;
191 led_trigger_register(trigger);
192 }
193 *tp = trigger;
194}
195
196void led_trigger_unregister(struct led_trigger *trigger)
197{
198 struct led_classdev *led_cdev;
199
200 /* Remove from the list of led triggers */
201 write_lock(&triggers_list_lock);
202 list_del(&trigger->next_trig);
203 write_unlock(&triggers_list_lock);
204
205 /* Remove anyone actively using this trigger */
206 read_lock(&leds_list_lock);
207 list_for_each_entry(led_cdev, &leds_list, node) {
208 write_lock(&led_cdev->trigger_lock);
209 if (led_cdev->trigger == trigger)
210 led_trigger_set(led_cdev, NULL);
211 write_unlock(&led_cdev->trigger_lock);
212 }
213 read_unlock(&leds_list_lock);
214}
215
216void led_trigger_unregister_simple(struct led_trigger *trigger)
217{
218 led_trigger_unregister(trigger);
219 kfree(trigger);
220}
221
222/* Used by LED Class */
223EXPORT_SYMBOL_GPL(led_trigger_set);
224EXPORT_SYMBOL_GPL(led_trigger_set_default);
225EXPORT_SYMBOL_GPL(led_trigger_show);
226EXPORT_SYMBOL_GPL(led_trigger_store);
227
228/* LED Trigger Interface */
229EXPORT_SYMBOL_GPL(led_trigger_register);
230EXPORT_SYMBOL_GPL(led_trigger_unregister);
231
232/* Simple LED Tigger Interface */
233EXPORT_SYMBOL_GPL(led_trigger_register_simple);
234EXPORT_SYMBOL_GPL(led_trigger_unregister_simple);
235EXPORT_SYMBOL_GPL(led_trigger_event);
236
237MODULE_AUTHOR("Richard Purdie");
238MODULE_LICENSE("GPL");
239MODULE_DESCRIPTION("LED Triggers Core");