blob: c3734f10fdd55143da14b4bbd9f296db5e7ca43f [file] [log] [blame]
Richard Purdiec3bc9952006-03-31 02:31:05 -08001/*
2 * LED Triggers Core
3 *
Richard Purdief8a7c6f2007-07-08 23:19:31 +01004 * Copyright 2005-2007 Openedhand Ltd.
Richard Purdiec3bc9952006-03-31 02:31:05 -08005 *
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>
Richard Purdiec3bc9952006-03-31 02:31:05 -080016#include <linux/list.h>
17#include <linux/spinlock.h>
18#include <linux/device.h>
Richard Purdiec3bc9952006-03-31 02:31:05 -080019#include <linux/timer.h>
Richard Purdiedc472062007-11-10 13:29:04 +000020#include <linux/rwsem.h>
Richard Purdiec3bc9952006-03-31 02:31:05 -080021#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Richard Purdiec3bc9952006-03-31 02:31:05 -080023#include "leds.h"
24
25/*
26 * Nests outside led_cdev->trigger_lock
27 */
Richard Purdiedc472062007-11-10 13:29:04 +000028static DECLARE_RWSEM(triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080029static LIST_HEAD(trigger_list);
30
Németh Márton4d404fd2008-03-09 20:59:57 +000031 /* Used by LED Class */
32
Richard Purdief8a7c6f2007-07-08 23:19:31 +010033ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
34 const char *buf, size_t count)
Richard Purdiec3bc9952006-03-31 02:31:05 -080035{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010036 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdiec3bc9952006-03-31 02:31:05 -080037 char trigger_name[TRIG_NAME_MAX];
38 struct led_trigger *trig;
39 size_t len;
40
41 trigger_name[sizeof(trigger_name) - 1] = '\0';
42 strncpy(trigger_name, buf, sizeof(trigger_name) - 1);
43 len = strlen(trigger_name);
44
45 if (len && trigger_name[len - 1] == '\n')
46 trigger_name[len - 1] = '\0';
47
48 if (!strcmp(trigger_name, "none")) {
Németh Márton0013b232008-03-09 20:54:37 +000049 led_trigger_remove(led_cdev);
Richard Purdiec3bc9952006-03-31 02:31:05 -080050 return count;
51 }
52
Richard Purdiedc472062007-11-10 13:29:04 +000053 down_read(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080054 list_for_each_entry(trig, &trigger_list, next_trig) {
55 if (!strcmp(trigger_name, trig->name)) {
Richard Purdiedc472062007-11-10 13:29:04 +000056 down_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080057 led_trigger_set(led_cdev, trig);
Richard Purdiedc472062007-11-10 13:29:04 +000058 up_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080059
Richard Purdiedc472062007-11-10 13:29:04 +000060 up_read(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080061 return count;
62 }
63 }
Richard Purdiedc472062007-11-10 13:29:04 +000064 up_read(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080065
66 return -EINVAL;
67}
Németh Márton4d404fd2008-03-09 20:59:57 +000068EXPORT_SYMBOL_GPL(led_trigger_store);
Richard Purdiec3bc9952006-03-31 02:31:05 -080069
Richard Purdief8a7c6f2007-07-08 23:19:31 +010070ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
71 char *buf)
Richard Purdiec3bc9952006-03-31 02:31:05 -080072{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010073 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdiec3bc9952006-03-31 02:31:05 -080074 struct led_trigger *trig;
75 int len = 0;
76
Richard Purdiedc472062007-11-10 13:29:04 +000077 down_read(&triggers_list_lock);
78 down_read(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080079
80 if (!led_cdev->trigger)
81 len += sprintf(buf+len, "[none] ");
82 else
83 len += sprintf(buf+len, "none ");
84
85 list_for_each_entry(trig, &trigger_list, next_trig) {
86 if (led_cdev->trigger && !strcmp(led_cdev->trigger->name,
87 trig->name))
88 len += sprintf(buf+len, "[%s] ", trig->name);
89 else
90 len += sprintf(buf+len, "%s ", trig->name);
91 }
Richard Purdiedc472062007-11-10 13:29:04 +000092 up_read(&led_cdev->trigger_lock);
93 up_read(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080094
95 len += sprintf(len+buf, "\n");
96 return len;
97}
Németh Márton4d404fd2008-03-09 20:59:57 +000098EXPORT_SYMBOL_GPL(led_trigger_show);
Richard Purdiec3bc9952006-03-31 02:31:05 -080099
100/* Caller must ensure led_cdev->trigger_lock held */
Bryan Wueb202622012-04-19 11:46:33 +0800101void led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
Richard Purdiec3bc9952006-03-31 02:31:05 -0800102{
103 unsigned long flags;
Colin Cross52c47742012-08-27 09:31:49 +0800104 char *event = NULL;
105 char *envp[2];
106 const char *name;
107
108 name = trig ? trig->name : "none";
109 event = kasprintf(GFP_KERNEL, "TRIGGER=%s", name);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800110
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);
Németh Márton4d404fd2008-03-09 20:59:57 +0000115 write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock,
116 flags);
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800117 cancel_work_sync(&led_cdev->set_brightness_work);
118 led_stop_software_blink(led_cdev);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800119 if (led_cdev->trigger->deactivate)
120 led_cdev->trigger->deactivate(led_cdev);
Dmitry Baryshkovfe3025b2008-07-16 22:51:14 +0100121 led_cdev->trigger = NULL;
Shuah Khan19cd67e2012-06-14 04:34:30 +0800122 led_set_brightness(led_cdev, LED_OFF);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800123 }
Bryan Wueb202622012-04-19 11:46:33 +0800124 if (trig) {
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;
129 if (trig->activate)
130 trig->activate(led_cdev);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800131 }
Colin Cross52c47742012-08-27 09:31:49 +0800132
133 if (event) {
134 envp[0] = event;
135 envp[1] = NULL;
136 kobject_uevent_env(&led_cdev->dev->kobj, KOBJ_CHANGE, envp);
137 kfree(event);
138 }
Richard Purdiec3bc9952006-03-31 02:31:05 -0800139}
Németh Márton4d404fd2008-03-09 20:59:57 +0000140EXPORT_SYMBOL_GPL(led_trigger_set);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800141
Németh Márton0013b232008-03-09 20:54:37 +0000142void led_trigger_remove(struct led_classdev *led_cdev)
143{
144 down_write(&led_cdev->trigger_lock);
145 led_trigger_set(led_cdev, NULL);
146 up_write(&led_cdev->trigger_lock);
147}
Németh Márton4d404fd2008-03-09 20:59:57 +0000148EXPORT_SYMBOL_GPL(led_trigger_remove);
Németh Márton0013b232008-03-09 20:54:37 +0000149
Richard Purdiec3bc9952006-03-31 02:31:05 -0800150void led_trigger_set_default(struct led_classdev *led_cdev)
151{
152 struct led_trigger *trig;
153
154 if (!led_cdev->default_trigger)
155 return;
156
Richard Purdiedc472062007-11-10 13:29:04 +0000157 down_read(&triggers_list_lock);
158 down_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800159 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);
162 }
Richard Purdiedc472062007-11-10 13:29:04 +0000163 up_write(&led_cdev->trigger_lock);
164 up_read(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800165}
Németh Márton4d404fd2008-03-09 20:59:57 +0000166EXPORT_SYMBOL_GPL(led_trigger_set_default);
167
Fabio Baltieria8df7b12012-11-04 01:54:34 -0800168void led_trigger_rename_static(const char *name, struct led_trigger *trig)
169{
170 /* new name must be on a temporary string to prevent races */
171 BUG_ON(name == trig->name);
172
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);
178}
179EXPORT_SYMBOL_GPL(led_trigger_rename_static);
180
Németh Márton4d404fd2008-03-09 20:59:57 +0000181/* LED Trigger Interface */
Richard Purdiec3bc9952006-03-31 02:31:05 -0800182
Bryan Wueb202622012-04-19 11:46:33 +0800183int led_trigger_register(struct led_trigger *trig)
Richard Purdiec3bc9952006-03-31 02:31:05 -0800184{
185 struct led_classdev *led_cdev;
Bryan Wueb202622012-04-19 11:46:33 +0800186 struct led_trigger *_trig;
Richard Purdiec3bc9952006-03-31 02:31:05 -0800187
Bryan Wueb202622012-04-19 11:46:33 +0800188 rwlock_init(&trig->leddev_list_lock);
189 INIT_LIST_HEAD(&trig->led_cdevs);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800190
Richard Purdiedc472062007-11-10 13:29:04 +0000191 down_write(&triggers_list_lock);
Adam Nielsen700c6ea2009-02-18 08:18:04 +1000192 /* Make sure the trigger's name isn't already in use */
Bryan Wueb202622012-04-19 11:46:33 +0800193 list_for_each_entry(_trig, &trigger_list, next_trig) {
194 if (!strcmp(_trig->name, trig->name)) {
Adam Nielsen700c6ea2009-02-18 08:18:04 +1000195 up_write(&triggers_list_lock);
196 return -EEXIST;
197 }
198 }
199 /* Add to the list of led triggers */
Bryan Wueb202622012-04-19 11:46:33 +0800200 list_add_tail(&trig->next_trig, &trigger_list);
Richard Purdiedc472062007-11-10 13:29:04 +0000201 up_write(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800202
203 /* Register with any LEDs that have this as a default trigger */
Richard Purdie72f8da32007-12-31 23:09:44 +0000204 down_read(&leds_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800205 list_for_each_entry(led_cdev, &leds_list, node) {
Richard Purdiedc472062007-11-10 13:29:04 +0000206 down_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800207 if (!led_cdev->trigger && led_cdev->default_trigger &&
Bryan Wueb202622012-04-19 11:46:33 +0800208 !strcmp(led_cdev->default_trigger, trig->name))
209 led_trigger_set(led_cdev, trig);
Richard Purdiedc472062007-11-10 13:29:04 +0000210 up_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800211 }
Richard Purdie72f8da32007-12-31 23:09:44 +0000212 up_read(&leds_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800213
214 return 0;
215}
Németh Márton4d404fd2008-03-09 20:59:57 +0000216EXPORT_SYMBOL_GPL(led_trigger_register);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800217
Bryan Wueb202622012-04-19 11:46:33 +0800218void led_trigger_unregister(struct led_trigger *trig)
Richard Purdiec3bc9952006-03-31 02:31:05 -0800219{
220 struct led_classdev *led_cdev;
221
Sasha Levin14f57162014-04-04 10:01:13 -0700222 if (list_empty_careful(&trig->next_trig))
223 return;
224
Richard Purdiec3bc9952006-03-31 02:31:05 -0800225 /* Remove from the list of led triggers */
Richard Purdiedc472062007-11-10 13:29:04 +0000226 down_write(&triggers_list_lock);
Sasha Levin14f57162014-04-04 10:01:13 -0700227 list_del_init(&trig->next_trig);
Richard Purdiedc472062007-11-10 13:29:04 +0000228 up_write(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800229
230 /* Remove anyone actively using this trigger */
Richard Purdie72f8da32007-12-31 23:09:44 +0000231 down_read(&leds_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800232 list_for_each_entry(led_cdev, &leds_list, node) {
Richard Purdiedc472062007-11-10 13:29:04 +0000233 down_write(&led_cdev->trigger_lock);
Bryan Wueb202622012-04-19 11:46:33 +0800234 if (led_cdev->trigger == trig)
Richard Purdiec3bc9952006-03-31 02:31:05 -0800235 led_trigger_set(led_cdev, NULL);
Richard Purdiedc472062007-11-10 13:29:04 +0000236 up_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800237 }
Richard Purdie72f8da32007-12-31 23:09:44 +0000238 up_read(&leds_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800239}
Németh Márton4d404fd2008-03-09 20:59:57 +0000240EXPORT_SYMBOL_GPL(led_trigger_unregister);
241
242/* Simple LED Tigger Interface */
243
Bryan Wueb202622012-04-19 11:46:33 +0800244void led_trigger_event(struct led_trigger *trig,
Németh Márton4d404fd2008-03-09 20:59:57 +0000245 enum led_brightness brightness)
246{
ZHAO Gang11e043b2013-12-28 07:00:26 -0800247 struct led_classdev *led_cdev;
Németh Márton4d404fd2008-03-09 20:59:57 +0000248
Bryan Wueb202622012-04-19 11:46:33 +0800249 if (!trig)
Németh Márton4d404fd2008-03-09 20:59:57 +0000250 return;
251
Bryan Wueb202622012-04-19 11:46:33 +0800252 read_lock(&trig->leddev_list_lock);
ZHAO Gang11e043b2013-12-28 07:00:26 -0800253 list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list)
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800254 led_set_brightness(led_cdev, brightness);
Bryan Wueb202622012-04-19 11:46:33 +0800255 read_unlock(&trig->leddev_list_lock);
Németh Márton4d404fd2008-03-09 20:59:57 +0000256}
257EXPORT_SYMBOL_GPL(led_trigger_event);
258
Bryan Wu20c0e6b2012-06-15 22:15:27 +0800259static void led_trigger_blink_setup(struct led_trigger *trig,
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800260 unsigned long *delay_on,
261 unsigned long *delay_off,
262 int oneshot,
263 int invert)
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000264{
ZHAO Gang11e043b2013-12-28 07:00:26 -0800265 struct led_classdev *led_cdev;
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000266
Bryan Wueb202622012-04-19 11:46:33 +0800267 if (!trig)
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000268 return;
269
Bryan Wueb202622012-04-19 11:46:33 +0800270 read_lock(&trig->leddev_list_lock);
ZHAO Gang11e043b2013-12-28 07:00:26 -0800271 list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list) {
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800272 if (oneshot)
273 led_blink_set_oneshot(led_cdev, delay_on, delay_off,
274 invert);
275 else
276 led_blink_set(led_cdev, delay_on, delay_off);
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000277 }
Bryan Wueb202622012-04-19 11:46:33 +0800278 read_unlock(&trig->leddev_list_lock);
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000279}
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800280
281void led_trigger_blink(struct led_trigger *trig,
282 unsigned long *delay_on,
283 unsigned long *delay_off)
284{
285 led_trigger_blink_setup(trig, delay_on, delay_off, 0, 0);
286}
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000287EXPORT_SYMBOL_GPL(led_trigger_blink);
288
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800289void led_trigger_blink_oneshot(struct led_trigger *trig,
290 unsigned long *delay_on,
291 unsigned long *delay_off,
292 int invert)
293{
294 led_trigger_blink_setup(trig, delay_on, delay_off, 1, invert);
295}
296EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot);
297
Németh Márton4d404fd2008-03-09 20:59:57 +0000298void led_trigger_register_simple(const char *name, struct led_trigger **tp)
299{
Bryan Wueb202622012-04-19 11:46:33 +0800300 struct led_trigger *trig;
Németh Márton4d404fd2008-03-09 20:59:57 +0000301 int err;
302
Bryan Wueb202622012-04-19 11:46:33 +0800303 trig = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
Németh Márton4d404fd2008-03-09 20:59:57 +0000304
Bryan Wueb202622012-04-19 11:46:33 +0800305 if (trig) {
306 trig->name = name;
307 err = led_trigger_register(trig);
Masakazu Mokunocba4c2a2011-10-31 17:11:59 -0700308 if (err < 0) {
Bryan Wueb202622012-04-19 11:46:33 +0800309 kfree(trig);
310 trig = NULL;
Sachin Kamat09f5fe72012-11-25 13:00:44 +0530311 pr_warn("LED trigger %s failed to register (%d)\n",
312 name, err);
Masakazu Mokunocba4c2a2011-10-31 17:11:59 -0700313 }
Sachin Kamat09f5fe72012-11-25 13:00:44 +0530314 } else {
315 pr_warn("LED trigger %s failed to register (no memory)\n",
316 name);
317 }
Bryan Wueb202622012-04-19 11:46:33 +0800318 *tp = trig;
Németh Márton4d404fd2008-03-09 20:59:57 +0000319}
320EXPORT_SYMBOL_GPL(led_trigger_register_simple);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800321
Bryan Wueb202622012-04-19 11:46:33 +0800322void led_trigger_unregister_simple(struct led_trigger *trig)
Richard Purdiec3bc9952006-03-31 02:31:05 -0800323{
Bryan Wueb202622012-04-19 11:46:33 +0800324 if (trig)
325 led_trigger_unregister(trig);
326 kfree(trig);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800327}
Richard Purdiec3bc9952006-03-31 02:31:05 -0800328EXPORT_SYMBOL_GPL(led_trigger_unregister_simple);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800329
330MODULE_AUTHOR("Richard Purdie");
331MODULE_LICENSE("GPL");
332MODULE_DESCRIPTION("LED Triggers Core");