1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2005-2007 Openedhand Ltd.
7 * Author: Richard Purdie <rpurdie@openedhand.com>
10 #include <linux/export.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/spinlock.h>
14 #include <linux/device.h>
15 #include <linux/timer.h>
16 #include <linux/rwsem.h>
17 #include <linux/leds.h>
18 #include <linux/slab.h>
23 * Nests outside led_cdev->trigger_lock
25 static DECLARE_RWSEM(triggers_list_lock
);
26 LIST_HEAD(trigger_list
);
28 /* Used by LED Class */
30 ssize_t
led_trigger_write(struct file
*filp
, struct kobject
*kobj
,
31 struct bin_attribute
*bin_attr
, char *buf
,
32 loff_t pos
, size_t count
)
34 struct device
*dev
= kobj_to_dev(kobj
);
35 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
36 struct led_trigger
*trig
;
39 mutex_lock(&led_cdev
->led_access
);
41 if (led_sysfs_is_disabled(led_cdev
)) {
46 if (sysfs_streq(buf
, "none")) {
47 led_trigger_remove(led_cdev
);
51 down_read(&triggers_list_lock
);
52 list_for_each_entry(trig
, &trigger_list
, next_trig
) {
53 if (sysfs_streq(buf
, trig
->name
)) {
54 down_write(&led_cdev
->trigger_lock
);
55 led_trigger_set(led_cdev
, trig
);
56 up_write(&led_cdev
->trigger_lock
);
58 up_read(&triggers_list_lock
);
62 /* we come here only if buf matches no trigger */
64 up_read(&triggers_list_lock
);
67 mutex_unlock(&led_cdev
->led_access
);
70 EXPORT_SYMBOL_GPL(led_trigger_write
);
73 static int led_trigger_snprintf(char *buf
, ssize_t size
, const char *fmt
, ...)
80 i
= vsnprintf(NULL
, 0, fmt
, args
);
82 i
= vscnprintf(buf
, size
, fmt
, args
);
88 static int led_trigger_format(char *buf
, size_t size
,
89 struct led_classdev
*led_cdev
)
91 struct led_trigger
*trig
;
92 int len
= led_trigger_snprintf(buf
, size
, "%s",
93 led_cdev
->trigger
? "none" : "[none]");
95 list_for_each_entry(trig
, &trigger_list
, next_trig
) {
96 bool hit
= led_cdev
->trigger
&&
97 !strcmp(led_cdev
->trigger
->name
, trig
->name
);
99 len
+= led_trigger_snprintf(buf
+ len
, size
- len
,
100 " %s%s%s", hit
? "[" : "",
101 trig
->name
, hit
? "]" : "");
104 len
+= led_trigger_snprintf(buf
+ len
, size
- len
, "\n");
110 * It was stupid to create 10000 cpu triggers, but we are stuck with it now.
111 * Don't make that mistake again. We work around it here by creating binary
112 * attribute, which is not limited by length. This is _not_ good design, do not
115 ssize_t
led_trigger_read(struct file
*filp
, struct kobject
*kobj
,
116 struct bin_attribute
*attr
, char *buf
,
117 loff_t pos
, size_t count
)
119 struct device
*dev
= kobj_to_dev(kobj
);
120 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
124 down_read(&triggers_list_lock
);
125 down_read(&led_cdev
->trigger_lock
);
127 len
= led_trigger_format(NULL
, 0, led_cdev
);
128 data
= kvmalloc(len
+ 1, GFP_KERNEL
);
130 up_read(&led_cdev
->trigger_lock
);
131 up_read(&triggers_list_lock
);
134 len
= led_trigger_format(data
, len
+ 1, led_cdev
);
136 up_read(&led_cdev
->trigger_lock
);
137 up_read(&triggers_list_lock
);
139 len
= memory_read_from_buffer(buf
, count
, &pos
, data
, len
);
145 EXPORT_SYMBOL_GPL(led_trigger_read
);
147 /* Caller must ensure led_cdev->trigger_lock held */
148 int led_trigger_set(struct led_classdev
*led_cdev
, struct led_trigger
*trig
)
156 if (!led_cdev
->trigger
&& !trig
)
159 name
= trig
? trig
->name
: "none";
160 event
= kasprintf(GFP_KERNEL
, "TRIGGER=%s", name
);
162 /* Remove any existing trigger */
163 if (led_cdev
->trigger
) {
164 write_lock_irqsave(&led_cdev
->trigger
->leddev_list_lock
, flags
);
165 list_del(&led_cdev
->trig_list
);
166 write_unlock_irqrestore(&led_cdev
->trigger
->leddev_list_lock
,
168 cancel_work_sync(&led_cdev
->set_brightness_work
);
169 led_stop_software_blink(led_cdev
);
170 if (led_cdev
->trigger
->deactivate
)
171 led_cdev
->trigger
->deactivate(led_cdev
);
172 device_remove_groups(led_cdev
->dev
, led_cdev
->trigger
->groups
);
173 led_cdev
->trigger
= NULL
;
174 led_cdev
->trigger_data
= NULL
;
175 led_cdev
->activated
= false;
176 led_set_brightness(led_cdev
, LED_OFF
);
179 write_lock_irqsave(&trig
->leddev_list_lock
, flags
);
180 list_add_tail(&led_cdev
->trig_list
, &trig
->led_cdevs
);
181 write_unlock_irqrestore(&trig
->leddev_list_lock
, flags
);
182 led_cdev
->trigger
= trig
;
185 ret
= trig
->activate(led_cdev
);
192 ret
= device_add_groups(led_cdev
->dev
, trig
->groups
);
194 dev_err(led_cdev
->dev
, "Failed to add trigger attributes\n");
202 if (kobject_uevent_env(&led_cdev
->dev
->kobj
, KOBJ_CHANGE
, envp
))
203 dev_err(led_cdev
->dev
,
204 "%s: Error sending uevent\n", __func__
);
212 if (trig
->deactivate
)
213 trig
->deactivate(led_cdev
);
216 write_lock_irqsave(&led_cdev
->trigger
->leddev_list_lock
, flags
);
217 list_del(&led_cdev
->trig_list
);
218 write_unlock_irqrestore(&led_cdev
->trigger
->leddev_list_lock
, flags
);
219 led_cdev
->trigger
= NULL
;
220 led_cdev
->trigger_data
= NULL
;
221 led_set_brightness(led_cdev
, LED_OFF
);
226 EXPORT_SYMBOL_GPL(led_trigger_set
);
228 void led_trigger_remove(struct led_classdev
*led_cdev
)
230 down_write(&led_cdev
->trigger_lock
);
231 led_trigger_set(led_cdev
, NULL
);
232 up_write(&led_cdev
->trigger_lock
);
234 EXPORT_SYMBOL_GPL(led_trigger_remove
);
236 void led_trigger_set_default(struct led_classdev
*led_cdev
)
238 struct led_trigger
*trig
;
240 if (!led_cdev
->default_trigger
)
243 down_read(&triggers_list_lock
);
244 down_write(&led_cdev
->trigger_lock
);
245 list_for_each_entry(trig
, &trigger_list
, next_trig
) {
246 if (!strcmp(led_cdev
->default_trigger
, trig
->name
)) {
247 led_cdev
->flags
|= LED_INIT_DEFAULT_TRIGGER
;
248 led_trigger_set(led_cdev
, trig
);
252 up_write(&led_cdev
->trigger_lock
);
253 up_read(&triggers_list_lock
);
255 EXPORT_SYMBOL_GPL(led_trigger_set_default
);
257 void led_trigger_rename_static(const char *name
, struct led_trigger
*trig
)
259 /* new name must be on a temporary string to prevent races */
260 BUG_ON(name
== trig
->name
);
262 down_write(&triggers_list_lock
);
263 /* this assumes that trig->name was originaly allocated to
264 * non constant storage */
265 strcpy((char *)trig
->name
, name
);
266 up_write(&triggers_list_lock
);
268 EXPORT_SYMBOL_GPL(led_trigger_rename_static
);
270 /* LED Trigger Interface */
272 int led_trigger_register(struct led_trigger
*trig
)
274 struct led_classdev
*led_cdev
;
275 struct led_trigger
*_trig
;
277 rwlock_init(&trig
->leddev_list_lock
);
278 INIT_LIST_HEAD(&trig
->led_cdevs
);
280 down_write(&triggers_list_lock
);
281 /* Make sure the trigger's name isn't already in use */
282 list_for_each_entry(_trig
, &trigger_list
, next_trig
) {
283 if (!strcmp(_trig
->name
, trig
->name
)) {
284 up_write(&triggers_list_lock
);
288 /* Add to the list of led triggers */
289 list_add_tail(&trig
->next_trig
, &trigger_list
);
290 up_write(&triggers_list_lock
);
292 /* Register with any LEDs that have this as a default trigger */
293 down_read(&leds_list_lock
);
294 list_for_each_entry(led_cdev
, &leds_list
, node
) {
295 down_write(&led_cdev
->trigger_lock
);
296 if (!led_cdev
->trigger
&& led_cdev
->default_trigger
&&
297 !strcmp(led_cdev
->default_trigger
, trig
->name
)) {
298 led_cdev
->flags
|= LED_INIT_DEFAULT_TRIGGER
;
299 led_trigger_set(led_cdev
, trig
);
301 up_write(&led_cdev
->trigger_lock
);
303 up_read(&leds_list_lock
);
307 EXPORT_SYMBOL_GPL(led_trigger_register
);
309 void led_trigger_unregister(struct led_trigger
*trig
)
311 struct led_classdev
*led_cdev
;
313 if (list_empty_careful(&trig
->next_trig
))
316 /* Remove from the list of led triggers */
317 down_write(&triggers_list_lock
);
318 list_del_init(&trig
->next_trig
);
319 up_write(&triggers_list_lock
);
321 /* Remove anyone actively using this trigger */
322 down_read(&leds_list_lock
);
323 list_for_each_entry(led_cdev
, &leds_list
, node
) {
324 down_write(&led_cdev
->trigger_lock
);
325 if (led_cdev
->trigger
== trig
)
326 led_trigger_set(led_cdev
, NULL
);
327 up_write(&led_cdev
->trigger_lock
);
329 up_read(&leds_list_lock
);
331 EXPORT_SYMBOL_GPL(led_trigger_unregister
);
333 static void devm_led_trigger_release(struct device
*dev
, void *res
)
335 led_trigger_unregister(*(struct led_trigger
**)res
);
338 int devm_led_trigger_register(struct device
*dev
,
339 struct led_trigger
*trig
)
341 struct led_trigger
**dr
;
344 dr
= devres_alloc(devm_led_trigger_release
, sizeof(*dr
),
351 rc
= led_trigger_register(trig
);
359 EXPORT_SYMBOL_GPL(devm_led_trigger_register
);
361 /* Simple LED Tigger Interface */
363 void led_trigger_event(struct led_trigger
*trig
,
364 enum led_brightness brightness
)
366 struct led_classdev
*led_cdev
;
371 read_lock(&trig
->leddev_list_lock
);
372 list_for_each_entry(led_cdev
, &trig
->led_cdevs
, trig_list
)
373 led_set_brightness(led_cdev
, brightness
);
374 read_unlock(&trig
->leddev_list_lock
);
376 EXPORT_SYMBOL_GPL(led_trigger_event
);
378 static void led_trigger_blink_setup(struct led_trigger
*trig
,
379 unsigned long *delay_on
,
380 unsigned long *delay_off
,
384 struct led_classdev
*led_cdev
;
389 read_lock(&trig
->leddev_list_lock
);
390 list_for_each_entry(led_cdev
, &trig
->led_cdevs
, trig_list
) {
392 led_blink_set_oneshot(led_cdev
, delay_on
, delay_off
,
395 led_blink_set(led_cdev
, delay_on
, delay_off
);
397 read_unlock(&trig
->leddev_list_lock
);
400 void led_trigger_blink(struct led_trigger
*trig
,
401 unsigned long *delay_on
,
402 unsigned long *delay_off
)
404 led_trigger_blink_setup(trig
, delay_on
, delay_off
, 0, 0);
406 EXPORT_SYMBOL_GPL(led_trigger_blink
);
408 void led_trigger_blink_oneshot(struct led_trigger
*trig
,
409 unsigned long *delay_on
,
410 unsigned long *delay_off
,
413 led_trigger_blink_setup(trig
, delay_on
, delay_off
, 1, invert
);
415 EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot
);
417 void led_trigger_register_simple(const char *name
, struct led_trigger
**tp
)
419 struct led_trigger
*trig
;
422 trig
= kzalloc(sizeof(struct led_trigger
), GFP_KERNEL
);
426 err
= led_trigger_register(trig
);
430 pr_warn("LED trigger %s failed to register (%d)\n",
434 pr_warn("LED trigger %s failed to register (no memory)\n",
439 EXPORT_SYMBOL_GPL(led_trigger_register_simple
);
441 void led_trigger_unregister_simple(struct led_trigger
*trig
)
444 led_trigger_unregister(trig
);
447 EXPORT_SYMBOL_GPL(led_trigger_unregister_simple
);