ledtrig-timer.c 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * LED Kernel Timer Trigger
 *
 * Copyright 2005-2006 Openedhand Ltd.
 *
 * 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/device.h>
17
#include <linux/ctype.h>
18
#include <linux/slab.h>
19 20
#include <linux/leds.h>

21
static ssize_t led_delay_on_show(struct device *dev,
22
		struct device_attribute *attr, char *buf)
23
{
24
	struct led_classdev *led_cdev = led_trigger_get_led(dev);
25

26
	return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
27 28
}

29
static ssize_t led_delay_on_store(struct device *dev,
30
		struct device_attribute *attr, const char *buf, size_t size)
31
{
32
	struct led_classdev *led_cdev = led_trigger_get_led(dev);
S
Shuah Khan 已提交
33 34
	unsigned long state;
	ssize_t ret = -EINVAL;
35

S
Shuah Khan 已提交
36 37 38
	ret = kstrtoul(buf, 10, &state);
	if (ret)
		return ret;
39

S
Shuah Khan 已提交
40 41
	led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
	led_cdev->blink_delay_on = state;
42

S
Shuah Khan 已提交
43
	return size;
44 45
}

46
static ssize_t led_delay_off_show(struct device *dev,
47
		struct device_attribute *attr, char *buf)
48
{
49
	struct led_classdev *led_cdev = led_trigger_get_led(dev);
50

51
	return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
52 53
}

54
static ssize_t led_delay_off_store(struct device *dev,
55
		struct device_attribute *attr, const char *buf, size_t size)
56
{
57
	struct led_classdev *led_cdev = led_trigger_get_led(dev);
S
Shuah Khan 已提交
58 59
	unsigned long state;
	ssize_t ret = -EINVAL;
60

S
Shuah Khan 已提交
61 62 63
	ret = kstrtoul(buf, 10, &state);
	if (ret)
		return ret;
64

S
Shuah Khan 已提交
65 66
	led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
	led_cdev->blink_delay_off = state;
67

S
Shuah Khan 已提交
68
	return size;
69 70
}

71 72
static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
73

74 75 76 77 78 79 80
static struct attribute *timer_trig_attrs[] = {
	&dev_attr_delay_on.attr,
	&dev_attr_delay_off.attr,
	NULL
};
ATTRIBUTE_GROUPS(timer_trig);

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
static void pattern_init(struct led_classdev *led_cdev)
{
	u32 *pattern;
	unsigned int size = 0;

	pattern = led_get_default_pattern(led_cdev, &size);
	if (!pattern)
		return;

	if (size != 2) {
		dev_warn(led_cdev->dev,
			 "Expected 2 but got %u values for delays pattern\n",
			 size);
		goto out;
	}

	led_cdev->blink_delay_on = pattern[0];
	led_cdev->blink_delay_off = pattern[1];
	/* led_blink_set() called by caller */

out:
	kfree(pattern);
}

105
static int timer_trig_activate(struct led_classdev *led_cdev)
106
{
107 108 109 110 111 112 113 114 115
	if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) {
		pattern_init(led_cdev);
		/*
		 * Mark as initialized even on pattern_init() error because
		 * any consecutive call to it would produce the same error.
		 */
		led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER;
	}

116 117
	led_blink_set(led_cdev, &led_cdev->blink_delay_on,
		      &led_cdev->blink_delay_off);
118 119

	return 0;
120 121 122 123
}

static void timer_trig_deactivate(struct led_classdev *led_cdev)
{
124
	/* Stop blinking */
125
	led_set_brightness(led_cdev, LED_OFF);
126 127 128 129 130 131
}

static struct led_trigger timer_led_trigger = {
	.name     = "timer",
	.activate = timer_trig_activate,
	.deactivate = timer_trig_deactivate,
132
	.groups = timer_trig_groups,
133
};
134
module_led_trigger(timer_led_trigger);
135 136 137

MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
MODULE_DESCRIPTION("Timer LED trigger");
138
MODULE_LICENSE("GPL v2");