Linux 4.19.133
[linux/fpc-iii.git] / drivers / leds / led-triggers.c
blob005b839f6eb9efc5f8646b0fdc7cfcb26e53e693
1 /*
2 * LED Triggers Core
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/export.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>
23 #include "leds.h"
26 * Nests outside led_cdev->trigger_lock
28 static DECLARE_RWSEM(triggers_list_lock);
29 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 struct led_trigger *trig;
38 int ret = count;
40 mutex_lock(&led_cdev->led_access);
42 if (led_sysfs_is_disabled(led_cdev)) {
43 ret = -EBUSY;
44 goto unlock;
47 if (sysfs_streq(buf, "none")) {
48 led_trigger_remove(led_cdev);
49 goto unlock;
52 down_read(&triggers_list_lock);
53 list_for_each_entry(trig, &trigger_list, next_trig) {
54 if (sysfs_streq(buf, trig->name)) {
55 down_write(&led_cdev->trigger_lock);
56 led_trigger_set(led_cdev, trig);
57 up_write(&led_cdev->trigger_lock);
59 up_read(&triggers_list_lock);
60 goto unlock;
63 /* we come here only if buf matches no trigger */
64 ret = -EINVAL;
65 up_read(&triggers_list_lock);
67 unlock:
68 mutex_unlock(&led_cdev->led_access);
69 return ret;
71 EXPORT_SYMBOL_GPL(led_trigger_store);
73 ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
74 char *buf)
76 struct led_classdev *led_cdev = dev_get_drvdata(dev);
77 struct led_trigger *trig;
78 int len = 0;
80 down_read(&triggers_list_lock);
81 down_read(&led_cdev->trigger_lock);
83 if (!led_cdev->trigger)
84 len += scnprintf(buf+len, PAGE_SIZE - len, "[none] ");
85 else
86 len += scnprintf(buf+len, PAGE_SIZE - len, "none ");
88 list_for_each_entry(trig, &trigger_list, next_trig) {
89 if (led_cdev->trigger && !strcmp(led_cdev->trigger->name,
90 trig->name))
91 len += scnprintf(buf+len, PAGE_SIZE - len, "[%s] ",
92 trig->name);
93 else
94 len += scnprintf(buf+len, PAGE_SIZE - len, "%s ",
95 trig->name);
97 up_read(&led_cdev->trigger_lock);
98 up_read(&triggers_list_lock);
100 len += scnprintf(len+buf, PAGE_SIZE - len, "\n");
101 return len;
103 EXPORT_SYMBOL_GPL(led_trigger_show);
105 /* Caller must ensure led_cdev->trigger_lock held */
106 int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
108 unsigned long flags;
109 char *event = NULL;
110 char *envp[2];
111 const char *name;
112 int ret;
114 if (!led_cdev->trigger && !trig)
115 return 0;
117 name = trig ? trig->name : "none";
118 event = kasprintf(GFP_KERNEL, "TRIGGER=%s", name);
120 /* Remove any existing trigger */
121 if (led_cdev->trigger) {
122 write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
123 list_del(&led_cdev->trig_list);
124 write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock,
125 flags);
126 cancel_work_sync(&led_cdev->set_brightness_work);
127 led_stop_software_blink(led_cdev);
128 if (led_cdev->trigger->deactivate)
129 led_cdev->trigger->deactivate(led_cdev);
130 device_remove_groups(led_cdev->dev, led_cdev->trigger->groups);
131 led_cdev->trigger = NULL;
132 led_cdev->trigger_data = NULL;
133 led_cdev->activated = false;
134 led_set_brightness(led_cdev, LED_OFF);
136 if (trig) {
137 write_lock_irqsave(&trig->leddev_list_lock, flags);
138 list_add_tail(&led_cdev->trig_list, &trig->led_cdevs);
139 write_unlock_irqrestore(&trig->leddev_list_lock, flags);
140 led_cdev->trigger = trig;
142 if (trig->activate)
143 ret = trig->activate(led_cdev);
144 else
145 ret = 0;
147 if (ret)
148 goto err_activate;
150 ret = device_add_groups(led_cdev->dev, trig->groups);
151 if (ret) {
152 dev_err(led_cdev->dev, "Failed to add trigger attributes\n");
153 goto err_add_groups;
157 if (event) {
158 envp[0] = event;
159 envp[1] = NULL;
160 if (kobject_uevent_env(&led_cdev->dev->kobj, KOBJ_CHANGE, envp))
161 dev_err(led_cdev->dev,
162 "%s: Error sending uevent\n", __func__);
163 kfree(event);
166 return 0;
168 err_add_groups:
170 if (trig->deactivate)
171 trig->deactivate(led_cdev);
172 err_activate:
174 write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
175 list_del(&led_cdev->trig_list);
176 write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, flags);
177 led_cdev->trigger = NULL;
178 led_cdev->trigger_data = NULL;
179 led_set_brightness(led_cdev, LED_OFF);
180 kfree(event);
182 return ret;
184 EXPORT_SYMBOL_GPL(led_trigger_set);
186 void led_trigger_remove(struct led_classdev *led_cdev)
188 down_write(&led_cdev->trigger_lock);
189 led_trigger_set(led_cdev, NULL);
190 up_write(&led_cdev->trigger_lock);
192 EXPORT_SYMBOL_GPL(led_trigger_remove);
194 void led_trigger_set_default(struct led_classdev *led_cdev)
196 struct led_trigger *trig;
198 if (!led_cdev->default_trigger)
199 return;
201 down_read(&triggers_list_lock);
202 down_write(&led_cdev->trigger_lock);
203 list_for_each_entry(trig, &trigger_list, next_trig) {
204 if (!strcmp(led_cdev->default_trigger, trig->name))
205 led_trigger_set(led_cdev, trig);
207 up_write(&led_cdev->trigger_lock);
208 up_read(&triggers_list_lock);
210 EXPORT_SYMBOL_GPL(led_trigger_set_default);
212 void led_trigger_rename_static(const char *name, struct led_trigger *trig)
214 /* new name must be on a temporary string to prevent races */
215 BUG_ON(name == trig->name);
217 down_write(&triggers_list_lock);
218 /* this assumes that trig->name was originaly allocated to
219 * non constant storage */
220 strcpy((char *)trig->name, name);
221 up_write(&triggers_list_lock);
223 EXPORT_SYMBOL_GPL(led_trigger_rename_static);
225 /* LED Trigger Interface */
227 int led_trigger_register(struct led_trigger *trig)
229 struct led_classdev *led_cdev;
230 struct led_trigger *_trig;
232 rwlock_init(&trig->leddev_list_lock);
233 INIT_LIST_HEAD(&trig->led_cdevs);
235 down_write(&triggers_list_lock);
236 /* Make sure the trigger's name isn't already in use */
237 list_for_each_entry(_trig, &trigger_list, next_trig) {
238 if (!strcmp(_trig->name, trig->name)) {
239 up_write(&triggers_list_lock);
240 return -EEXIST;
243 /* Add to the list of led triggers */
244 list_add_tail(&trig->next_trig, &trigger_list);
245 up_write(&triggers_list_lock);
247 /* Register with any LEDs that have this as a default trigger */
248 down_read(&leds_list_lock);
249 list_for_each_entry(led_cdev, &leds_list, node) {
250 down_write(&led_cdev->trigger_lock);
251 if (!led_cdev->trigger && led_cdev->default_trigger &&
252 !strcmp(led_cdev->default_trigger, trig->name))
253 led_trigger_set(led_cdev, trig);
254 up_write(&led_cdev->trigger_lock);
256 up_read(&leds_list_lock);
258 return 0;
260 EXPORT_SYMBOL_GPL(led_trigger_register);
262 void led_trigger_unregister(struct led_trigger *trig)
264 struct led_classdev *led_cdev;
266 if (list_empty_careful(&trig->next_trig))
267 return;
269 /* Remove from the list of led triggers */
270 down_write(&triggers_list_lock);
271 list_del_init(&trig->next_trig);
272 up_write(&triggers_list_lock);
274 /* Remove anyone actively using this trigger */
275 down_read(&leds_list_lock);
276 list_for_each_entry(led_cdev, &leds_list, node) {
277 down_write(&led_cdev->trigger_lock);
278 if (led_cdev->trigger == trig)
279 led_trigger_set(led_cdev, NULL);
280 up_write(&led_cdev->trigger_lock);
282 up_read(&leds_list_lock);
284 EXPORT_SYMBOL_GPL(led_trigger_unregister);
286 static void devm_led_trigger_release(struct device *dev, void *res)
288 led_trigger_unregister(*(struct led_trigger **)res);
291 int devm_led_trigger_register(struct device *dev,
292 struct led_trigger *trig)
294 struct led_trigger **dr;
295 int rc;
297 dr = devres_alloc(devm_led_trigger_release, sizeof(*dr),
298 GFP_KERNEL);
299 if (!dr)
300 return -ENOMEM;
302 *dr = trig;
304 rc = led_trigger_register(trig);
305 if (rc)
306 devres_free(dr);
307 else
308 devres_add(dev, dr);
310 return rc;
312 EXPORT_SYMBOL_GPL(devm_led_trigger_register);
314 /* Simple LED Tigger Interface */
316 void led_trigger_event(struct led_trigger *trig,
317 enum led_brightness brightness)
319 struct led_classdev *led_cdev;
321 if (!trig)
322 return;
324 read_lock(&trig->leddev_list_lock);
325 list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list)
326 led_set_brightness(led_cdev, brightness);
327 read_unlock(&trig->leddev_list_lock);
329 EXPORT_SYMBOL_GPL(led_trigger_event);
331 static void led_trigger_blink_setup(struct led_trigger *trig,
332 unsigned long *delay_on,
333 unsigned long *delay_off,
334 int oneshot,
335 int invert)
337 struct led_classdev *led_cdev;
339 if (!trig)
340 return;
342 read_lock(&trig->leddev_list_lock);
343 list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list) {
344 if (oneshot)
345 led_blink_set_oneshot(led_cdev, delay_on, delay_off,
346 invert);
347 else
348 led_blink_set(led_cdev, delay_on, delay_off);
350 read_unlock(&trig->leddev_list_lock);
353 void led_trigger_blink(struct led_trigger *trig,
354 unsigned long *delay_on,
355 unsigned long *delay_off)
357 led_trigger_blink_setup(trig, delay_on, delay_off, 0, 0);
359 EXPORT_SYMBOL_GPL(led_trigger_blink);
361 void led_trigger_blink_oneshot(struct led_trigger *trig,
362 unsigned long *delay_on,
363 unsigned long *delay_off,
364 int invert)
366 led_trigger_blink_setup(trig, delay_on, delay_off, 1, invert);
368 EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot);
370 void led_trigger_register_simple(const char *name, struct led_trigger **tp)
372 struct led_trigger *trig;
373 int err;
375 trig = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
377 if (trig) {
378 trig->name = name;
379 err = led_trigger_register(trig);
380 if (err < 0) {
381 kfree(trig);
382 trig = NULL;
383 pr_warn("LED trigger %s failed to register (%d)\n",
384 name, err);
386 } else {
387 pr_warn("LED trigger %s failed to register (no memory)\n",
388 name);
390 *tp = trig;
392 EXPORT_SYMBOL_GPL(led_trigger_register_simple);
394 void led_trigger_unregister_simple(struct led_trigger *trig)
396 if (trig)
397 led_trigger_unregister(trig);
398 kfree(trig);
400 EXPORT_SYMBOL_GPL(led_trigger_unregister_simple);