Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ledtrig-timer.c
Go to the documentation of this file.
1 /*
2  * LED Kernel Timer Trigger
3  *
4  * Copyright 2005-2006 Openedhand Ltd.
5  *
6  * Author: Richard Purdie <[email protected]>
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 
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/ctype.h>
19 #include <linux/leds.h>
20 #include "leds.h"
21 
22 static ssize_t led_delay_on_show(struct device *dev,
23  struct device_attribute *attr, char *buf)
24 {
25  struct led_classdev *led_cdev = dev_get_drvdata(dev);
26 
27  return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
28 }
29 
30 static ssize_t led_delay_on_store(struct device *dev,
31  struct device_attribute *attr, const char *buf, size_t size)
32 {
33  struct led_classdev *led_cdev = dev_get_drvdata(dev);
34  unsigned long state;
35  ssize_t ret = -EINVAL;
36 
37  ret = kstrtoul(buf, 10, &state);
38  if (ret)
39  return ret;
40 
41  led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
42  led_cdev->blink_delay_on = state;
43 
44  return size;
45 }
46 
47 static ssize_t led_delay_off_show(struct device *dev,
48  struct device_attribute *attr, char *buf)
49 {
50  struct led_classdev *led_cdev = dev_get_drvdata(dev);
51 
52  return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
53 }
54 
55 static ssize_t led_delay_off_store(struct device *dev,
56  struct device_attribute *attr, const char *buf, size_t size)
57 {
58  struct led_classdev *led_cdev = dev_get_drvdata(dev);
59  unsigned long state;
60  ssize_t ret = -EINVAL;
61 
62  ret = kstrtoul(buf, 10, &state);
63  if (ret)
64  return ret;
65 
66  led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
67  led_cdev->blink_delay_off = state;
68 
69  return size;
70 }
71 
72 static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
73 static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
74 
75 static void timer_trig_activate(struct led_classdev *led_cdev)
76 {
77  int rc;
78 
79  led_cdev->trigger_data = NULL;
80 
81  rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
82  if (rc)
83  return;
84  rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
85  if (rc)
86  goto err_out_delayon;
87 
88  led_blink_set(led_cdev, &led_cdev->blink_delay_on,
89  &led_cdev->blink_delay_off);
90  led_cdev->activated = true;
91 
92  return;
93 
94 err_out_delayon:
95  device_remove_file(led_cdev->dev, &dev_attr_delay_on);
96 }
97 
98 static void timer_trig_deactivate(struct led_classdev *led_cdev)
99 {
100  if (led_cdev->activated) {
101  device_remove_file(led_cdev->dev, &dev_attr_delay_on);
102  device_remove_file(led_cdev->dev, &dev_attr_delay_off);
103  led_cdev->activated = false;
104  }
105 
106  /* Stop blinking */
107  led_set_brightness(led_cdev, LED_OFF);
108 }
109 
110 static struct led_trigger timer_led_trigger = {
111  .name = "timer",
112  .activate = timer_trig_activate,
113  .deactivate = timer_trig_deactivate,
114 };
115 
116 static int __init timer_trig_init(void)
117 {
118  return led_trigger_register(&timer_led_trigger);
119 }
120 
121 static void __exit timer_trig_exit(void)
122 {
123  led_trigger_unregister(&timer_led_trigger);
124 }
125 
126 module_init(timer_trig_init);
127 module_exit(timer_trig_exit);
128 
129 MODULE_AUTHOR("Richard Purdie <[email protected]>");
130 MODULE_DESCRIPTION("Timer LED trigger");
131 MODULE_LICENSE("GPL");