led-triggers.c 7.8 KB
Newer Older
1 2 3
/*
 * LED Triggers Core
 *
4
 * Copyright 2005-2007 Openedhand Ltd.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Author: Richard Purdie <rpurdie@openedhand.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/device.h>
#include <linux/timer.h>
21
#include <linux/rwsem.h>
22
#include <linux/leds.h>
23
#include <linux/slab.h>
24 25 26 27 28
#include "leds.h"

/*
 * Nests outside led_cdev->trigger_lock
 */
29
static DECLARE_RWSEM(triggers_list_lock);
30 31
static LIST_HEAD(trigger_list);

32 33
 /* Used by LED Class */

34 35
ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
36
{
37
	struct led_classdev *led_cdev = dev_get_drvdata(dev);
38 39 40 41 42 43 44 45 46 47 48 49
	char trigger_name[TRIG_NAME_MAX];
	struct led_trigger *trig;
	size_t len;

	trigger_name[sizeof(trigger_name) - 1] = '\0';
	strncpy(trigger_name, buf, sizeof(trigger_name) - 1);
	len = strlen(trigger_name);

	if (len && trigger_name[len - 1] == '\n')
		trigger_name[len - 1] = '\0';

	if (!strcmp(trigger_name, "none")) {
50
		led_trigger_remove(led_cdev);
51 52 53
		return count;
	}

54
	down_read(&triggers_list_lock);
55 56
	list_for_each_entry(trig, &trigger_list, next_trig) {
		if (!strcmp(trigger_name, trig->name)) {
57
			down_write(&led_cdev->trigger_lock);
58
			led_trigger_set(led_cdev, trig);
59
			up_write(&led_cdev->trigger_lock);
60

61
			up_read(&triggers_list_lock);
62 63 64
			return count;
		}
	}
65
	up_read(&triggers_list_lock);
66 67 68

	return -EINVAL;
}
69
EXPORT_SYMBOL_GPL(led_trigger_store);
70

71 72
ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
		char *buf)
73
{
74
	struct led_classdev *led_cdev = dev_get_drvdata(dev);
75 76 77
	struct led_trigger *trig;
	int len = 0;

78 79
	down_read(&triggers_list_lock);
	down_read(&led_cdev->trigger_lock);
80 81 82 83 84 85 86 87 88 89 90 91 92

	if (!led_cdev->trigger)
		len += sprintf(buf+len, "[none] ");
	else
		len += sprintf(buf+len, "none ");

	list_for_each_entry(trig, &trigger_list, next_trig) {
		if (led_cdev->trigger && !strcmp(led_cdev->trigger->name,
							trig->name))
			len += sprintf(buf+len, "[%s] ", trig->name);
		else
			len += sprintf(buf+len, "%s ", trig->name);
	}
93 94
	up_read(&led_cdev->trigger_lock);
	up_read(&triggers_list_lock);
95 96 97 98

	len += sprintf(len+buf, "\n");
	return len;
}
99
EXPORT_SYMBOL_GPL(led_trigger_show);
100 101

/* Caller must ensure led_cdev->trigger_lock held */
102
void led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
103 104 105 106 107 108 109
{
	unsigned long flags;

	/* Remove any existing trigger */
	if (led_cdev->trigger) {
		write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
		list_del(&led_cdev->trig_list);
110 111
		write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock,
			flags);
112 113
		cancel_work_sync(&led_cdev->set_brightness_work);
		led_stop_software_blink(led_cdev);
114 115
		if (led_cdev->trigger->deactivate)
			led_cdev->trigger->deactivate(led_cdev);
116
		led_cdev->trigger = NULL;
117
		led_set_brightness(led_cdev, LED_OFF);
118
	}
119 120 121 122 123 124 125
	if (trig) {
		write_lock_irqsave(&trig->leddev_list_lock, flags);
		list_add_tail(&led_cdev->trig_list, &trig->led_cdevs);
		write_unlock_irqrestore(&trig->leddev_list_lock, flags);
		led_cdev->trigger = trig;
		if (trig->activate)
			trig->activate(led_cdev);
126 127
	}
}
128
EXPORT_SYMBOL_GPL(led_trigger_set);
129

130 131 132 133 134 135
void led_trigger_remove(struct led_classdev *led_cdev)
{
	down_write(&led_cdev->trigger_lock);
	led_trigger_set(led_cdev, NULL);
	up_write(&led_cdev->trigger_lock);
}
136
EXPORT_SYMBOL_GPL(led_trigger_remove);
137

138 139 140 141 142 143 144
void led_trigger_set_default(struct led_classdev *led_cdev)
{
	struct led_trigger *trig;

	if (!led_cdev->default_trigger)
		return;

145 146
	down_read(&triggers_list_lock);
	down_write(&led_cdev->trigger_lock);
147 148 149 150
	list_for_each_entry(trig, &trigger_list, next_trig) {
		if (!strcmp(led_cdev->default_trigger, trig->name))
			led_trigger_set(led_cdev, trig);
	}
151 152
	up_write(&led_cdev->trigger_lock);
	up_read(&triggers_list_lock);
153
}
154 155 156
EXPORT_SYMBOL_GPL(led_trigger_set_default);

/* LED Trigger Interface */
157

158
int led_trigger_register(struct led_trigger *trig)
159 160
{
	struct led_classdev *led_cdev;
161
	struct led_trigger *_trig;
162

163 164
	rwlock_init(&trig->leddev_list_lock);
	INIT_LIST_HEAD(&trig->led_cdevs);
165

166
	down_write(&triggers_list_lock);
167
	/* Make sure the trigger's name isn't already in use */
168 169
	list_for_each_entry(_trig, &trigger_list, next_trig) {
		if (!strcmp(_trig->name, trig->name)) {
170 171 172 173 174
			up_write(&triggers_list_lock);
			return -EEXIST;
		}
	}
	/* Add to the list of led triggers */
175
	list_add_tail(&trig->next_trig, &trigger_list);
176
	up_write(&triggers_list_lock);
177 178

	/* Register with any LEDs that have this as a default trigger */
179
	down_read(&leds_list_lock);
180
	list_for_each_entry(led_cdev, &leds_list, node) {
181
		down_write(&led_cdev->trigger_lock);
182
		if (!led_cdev->trigger && led_cdev->default_trigger &&
183 184
			    !strcmp(led_cdev->default_trigger, trig->name))
			led_trigger_set(led_cdev, trig);
185
		up_write(&led_cdev->trigger_lock);
186
	}
187
	up_read(&leds_list_lock);
188 189 190

	return 0;
}
191
EXPORT_SYMBOL_GPL(led_trigger_register);
192

193
void led_trigger_unregister(struct led_trigger *trig)
194 195 196 197
{
	struct led_classdev *led_cdev;

	/* Remove from the list of led triggers */
198
	down_write(&triggers_list_lock);
199
	list_del(&trig->next_trig);
200
	up_write(&triggers_list_lock);
201 202

	/* Remove anyone actively using this trigger */
203
	down_read(&leds_list_lock);
204
	list_for_each_entry(led_cdev, &leds_list, node) {
205
		down_write(&led_cdev->trigger_lock);
206
		if (led_cdev->trigger == trig)
207
			led_trigger_set(led_cdev, NULL);
208
		up_write(&led_cdev->trigger_lock);
209
	}
210
	up_read(&leds_list_lock);
211
}
212
EXPORT_SYMBOL_GPL(led_trigger_unregister);
213

214 215
/* Simple LED Tigger Interface */

216
void led_trigger_event(struct led_trigger *trig,
217
			enum led_brightness brightness)
218
{
219 220
	struct list_head *entry;

221
	if (!trig)
222 223
		return;

224 225
	read_lock(&trig->leddev_list_lock);
	list_for_each(entry, &trig->led_cdevs) {
226 227 228
		struct led_classdev *led_cdev;

		led_cdev = list_entry(entry, struct led_classdev, trig_list);
229
		led_set_brightness(led_cdev, brightness);
230
	}
231
	read_unlock(&trig->leddev_list_lock);
232
}
233
EXPORT_SYMBOL_GPL(led_trigger_event);
234

235
static void led_trigger_blink_setup(struct led_trigger *trig,
236 237 238 239
			     unsigned long *delay_on,
			     unsigned long *delay_off,
			     int oneshot,
			     int invert)
240 241 242
{
	struct list_head *entry;

243
	if (!trig)
244 245
		return;

246 247
	read_lock(&trig->leddev_list_lock);
	list_for_each(entry, &trig->led_cdevs) {
248 249 250
		struct led_classdev *led_cdev;

		led_cdev = list_entry(entry, struct led_classdev, trig_list);
251 252 253 254 255
		if (oneshot)
			led_blink_set_oneshot(led_cdev, delay_on, delay_off,
					      invert);
		else
			led_blink_set(led_cdev, delay_on, delay_off);
256
	}
257
	read_unlock(&trig->leddev_list_lock);
258
}
259 260 261 262 263 264 265

void led_trigger_blink(struct led_trigger *trig,
		       unsigned long *delay_on,
		       unsigned long *delay_off)
{
	led_trigger_blink_setup(trig, delay_on, delay_off, 0, 0);
}
266 267
EXPORT_SYMBOL_GPL(led_trigger_blink);

268 269 270 271 272 273 274 275 276
void led_trigger_blink_oneshot(struct led_trigger *trig,
			       unsigned long *delay_on,
			       unsigned long *delay_off,
			       int invert)
{
	led_trigger_blink_setup(trig, delay_on, delay_off, 1, invert);
}
EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot);

277 278
void led_trigger_register_simple(const char *name, struct led_trigger **tp)
{
279
	struct led_trigger *trig;
280
	int err;
281

282
	trig = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
283

284 285 286
	if (trig) {
		trig->name = name;
		err = led_trigger_register(trig);
287
		if (err < 0) {
288 289
			kfree(trig);
			trig = NULL;
290 291
			printk(KERN_WARNING "LED trigger %s failed to register"
				" (%d)\n", name, err);
292
		}
293 294 295 296
	} else
		printk(KERN_WARNING "LED trigger %s failed to register"
			" (no memory)\n", name);

297
	*tp = trig;
298
}
299
EXPORT_SYMBOL_GPL(led_trigger_register_simple);
300

301
void led_trigger_unregister_simple(struct led_trigger *trig)
302
{
303 304 305
	if (trig)
		led_trigger_unregister(trig);
	kfree(trig);
306
}
307 308 309 310 311
EXPORT_SYMBOL_GPL(led_trigger_unregister_simple);

MODULE_AUTHOR("Richard Purdie");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("LED Triggers Core");