1 /*
2  * LED Kernel Timer Trigger
3  *
4  * Copyright 2005-2006 Openedhand Ltd.
5  *
6  * Author: Richard Purdie <rpurdie@openedhand.com>
7  *
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.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/ctype.h>
18 #include <linux/slab.h>
19 #include <linux/leds.h>
20 
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);
33 	unsigned long state;
34 	ssize_t ret = -EINVAL;
35 
36 	ret = kstrtoul(buf, 10, &state);
37 	if (ret)
38 		return ret;
39 
40 	led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
41 	led_cdev->blink_delay_on = state;
42 
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);
58 	unsigned long state;
59 	ssize_t ret = -EINVAL;
60 
61 	ret = kstrtoul(buf, 10, &state);
62 	if (ret)
63 		return ret;
64 
65 	led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
66 	led_cdev->blink_delay_off = state;
67 
68 	return size;
69 }
70 
71 static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
72 static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
73 
74 static struct attribute *timer_trig_attrs[] = {
75 	&dev_attr_delay_on.attr,
76 	&dev_attr_delay_off.attr,
77 	NULL
78 };
79 ATTRIBUTE_GROUPS(timer_trig);
80 
81 static void pattern_init(struct led_classdev *led_cdev)
82 {
83 	u32 *pattern;
84 	unsigned int size = 0;
85 
86 	pattern = led_get_default_pattern(led_cdev, &size);
87 	if (!pattern)
88 		return;
89 
90 	if (size != 2) {
91 		dev_warn(led_cdev->dev,
92 			 "Expected 2 but got %u values for delays pattern\n",
93 			 size);
94 		goto out;
95 	}
96 
97 	led_cdev->blink_delay_on = pattern[0];
98 	led_cdev->blink_delay_off = pattern[1];
99 	/* led_blink_set() called by caller */
100 
101 out:
102 	kfree(pattern);
103 }
104 
105 static int timer_trig_activate(struct led_classdev *led_cdev)
106 {
107 	if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) {
108 		pattern_init(led_cdev);
109 		/*
110 		 * Mark as initialized even on pattern_init() error because
111 		 * any consecutive call to it would produce the same error.
112 		 */
113 		led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER;
114 	}
115 
116 	led_blink_set(led_cdev, &led_cdev->blink_delay_on,
117 		      &led_cdev->blink_delay_off);
118 
119 	return 0;
120 }
121 
122 static void timer_trig_deactivate(struct led_classdev *led_cdev)
123 {
124 	/* Stop blinking */
125 	led_set_brightness(led_cdev, LED_OFF);
126 }
127 
128 static struct led_trigger timer_led_trigger = {
129 	.name     = "timer",
130 	.activate = timer_trig_activate,
131 	.deactivate = timer_trig_deactivate,
132 	.groups = timer_trig_groups,
133 };
134 module_led_trigger(timer_led_trigger);
135 
136 MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
137 MODULE_DESCRIPTION("Timer LED trigger");
138 MODULE_LICENSE("GPL v2");
139