ledtrig-timer.c 3.1 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 19
#include <linux/leds.h>

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

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

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

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

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

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

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

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

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

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

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

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

70 71
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);
72 73 74

static void timer_trig_activate(struct led_classdev *led_cdev)
{
75
	int rc;
76

77
	led_cdev->trigger_data = NULL;
78

79 80
	rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
	if (rc)
81
		return;
82 83 84
	rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
	if (rc)
		goto err_out_delayon;
85

86 87
	led_blink_set(led_cdev, &led_cdev->blink_delay_on,
		      &led_cdev->blink_delay_off);
88
	led_cdev->activated = true;
89

90 91 92
	return;

err_out_delayon:
93
	device_remove_file(led_cdev->dev, &dev_attr_delay_on);
94 95 96 97
}

static void timer_trig_deactivate(struct led_classdev *led_cdev)
{
98
	if (led_cdev->activated) {
99 100
		device_remove_file(led_cdev->dev, &dev_attr_delay_on);
		device_remove_file(led_cdev->dev, &dev_attr_delay_off);
101
		led_cdev->activated = false;
102
	}
103

104
	/* Stop blinking */
105
	led_set_brightness(led_cdev, LED_OFF);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
}

static struct led_trigger timer_led_trigger = {
	.name     = "timer",
	.activate = timer_trig_activate,
	.deactivate = timer_trig_deactivate,
};

static int __init timer_trig_init(void)
{
	return led_trigger_register(&timer_led_trigger);
}

static void __exit timer_trig_exit(void)
{
	led_trigger_unregister(&timer_led_trigger);
}

module_init(timer_trig_init);
module_exit(timer_trig_exit);

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