Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 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 | |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 14 | #include <linux/module.h> |
Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 15 | #include <linux/jiffies.h> |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/sysdev.h> |
| 22 | #include <linux/timer.h> |
Richard Purdie | 3dc7b82 | 2006-05-15 09:44:17 -0700 | [diff] [blame] | 23 | #include <linux/ctype.h> |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 24 | #include <linux/leds.h> |
| 25 | #include "leds.h" |
| 26 | |
| 27 | struct timer_trig_data { |
| 28 | unsigned long delay_on; /* milliseconds on */ |
| 29 | unsigned long delay_off; /* milliseconds off */ |
| 30 | struct timer_list timer; |
| 31 | }; |
| 32 | |
| 33 | static void led_timer_function(unsigned long data) |
| 34 | { |
| 35 | struct led_classdev *led_cdev = (struct led_classdev *) data; |
| 36 | struct timer_trig_data *timer_data = led_cdev->trigger_data; |
| 37 | unsigned long brightness = LED_OFF; |
| 38 | unsigned long delay = timer_data->delay_off; |
| 39 | |
| 40 | if (!timer_data->delay_on || !timer_data->delay_off) { |
| 41 | led_set_brightness(led_cdev, LED_OFF); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | if (!led_cdev->brightness) { |
| 46 | brightness = LED_FULL; |
| 47 | delay = timer_data->delay_on; |
| 48 | } |
| 49 | |
| 50 | led_set_brightness(led_cdev, brightness); |
| 51 | |
| 52 | mod_timer(&timer_data->timer, jiffies + msecs_to_jiffies(delay)); |
| 53 | } |
| 54 | |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 55 | static ssize_t led_delay_on_show(struct device *dev, |
| 56 | struct device_attribute *attr, char *buf) |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 57 | { |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 58 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 59 | struct timer_trig_data *timer_data = led_cdev->trigger_data; |
| 60 | |
| 61 | sprintf(buf, "%lu\n", timer_data->delay_on); |
| 62 | |
| 63 | return strlen(buf) + 1; |
| 64 | } |
| 65 | |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 66 | static ssize_t led_delay_on_store(struct device *dev, |
| 67 | struct device_attribute *attr, const char *buf, size_t size) |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 68 | { |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 69 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 70 | struct timer_trig_data *timer_data = led_cdev->trigger_data; |
| 71 | int ret = -EINVAL; |
| 72 | char *after; |
| 73 | unsigned long state = simple_strtoul(buf, &after, 10); |
Richard Purdie | 3dc7b82 | 2006-05-15 09:44:17 -0700 | [diff] [blame] | 74 | size_t count = after - buf; |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 75 | |
Richard Purdie | 3dc7b82 | 2006-05-15 09:44:17 -0700 | [diff] [blame] | 76 | if (*after && isspace(*after)) |
| 77 | count++; |
| 78 | |
| 79 | if (count == size) { |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 80 | timer_data->delay_on = state; |
| 81 | mod_timer(&timer_data->timer, jiffies + 1); |
Richard Purdie | 3dc7b82 | 2006-05-15 09:44:17 -0700 | [diff] [blame] | 82 | ret = count; |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 88 | static ssize_t led_delay_off_show(struct device *dev, |
| 89 | struct device_attribute *attr, char *buf) |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 90 | { |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 91 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 92 | struct timer_trig_data *timer_data = led_cdev->trigger_data; |
| 93 | |
| 94 | sprintf(buf, "%lu\n", timer_data->delay_off); |
| 95 | |
| 96 | return strlen(buf) + 1; |
| 97 | } |
| 98 | |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 99 | static ssize_t led_delay_off_store(struct device *dev, |
| 100 | struct device_attribute *attr, const char *buf, size_t size) |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 101 | { |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 102 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 103 | struct timer_trig_data *timer_data = led_cdev->trigger_data; |
| 104 | int ret = -EINVAL; |
| 105 | char *after; |
| 106 | unsigned long state = simple_strtoul(buf, &after, 10); |
Richard Purdie | 3dc7b82 | 2006-05-15 09:44:17 -0700 | [diff] [blame] | 107 | size_t count = after - buf; |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 108 | |
Richard Purdie | 3dc7b82 | 2006-05-15 09:44:17 -0700 | [diff] [blame] | 109 | if (*after && isspace(*after)) |
| 110 | count++; |
| 111 | |
| 112 | if (count == size) { |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 113 | timer_data->delay_off = state; |
| 114 | mod_timer(&timer_data->timer, jiffies + 1); |
Richard Purdie | 3dc7b82 | 2006-05-15 09:44:17 -0700 | [diff] [blame] | 115 | ret = count; |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 121 | static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store); |
| 122 | static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store); |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 123 | |
| 124 | static void timer_trig_activate(struct led_classdev *led_cdev) |
| 125 | { |
| 126 | struct timer_trig_data *timer_data; |
Jeff Garzik | 12fda16 | 2006-10-17 00:10:20 -0700 | [diff] [blame] | 127 | int rc; |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 128 | |
| 129 | timer_data = kzalloc(sizeof(struct timer_trig_data), GFP_KERNEL); |
| 130 | if (!timer_data) |
| 131 | return; |
| 132 | |
| 133 | led_cdev->trigger_data = timer_data; |
| 134 | |
| 135 | init_timer(&timer_data->timer); |
| 136 | timer_data->timer.function = led_timer_function; |
| 137 | timer_data->timer.data = (unsigned long) led_cdev; |
| 138 | |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 139 | rc = device_create_file(led_cdev->dev, &dev_attr_delay_on); |
| 140 | if (rc) |
| 141 | goto err_out; |
| 142 | rc = device_create_file(led_cdev->dev, &dev_attr_delay_off); |
| 143 | if (rc) |
| 144 | goto err_out_delayon; |
Jeff Garzik | 12fda16 | 2006-10-17 00:10:20 -0700 | [diff] [blame] | 145 | |
| 146 | return; |
| 147 | |
| 148 | err_out_delayon: |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 149 | device_remove_file(led_cdev->dev, &dev_attr_delay_on); |
Jeff Garzik | 12fda16 | 2006-10-17 00:10:20 -0700 | [diff] [blame] | 150 | err_out: |
| 151 | led_cdev->trigger_data = NULL; |
| 152 | kfree(timer_data); |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void timer_trig_deactivate(struct led_classdev *led_cdev) |
| 156 | { |
| 157 | struct timer_trig_data *timer_data = led_cdev->trigger_data; |
| 158 | |
| 159 | if (timer_data) { |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame^] | 160 | device_remove_file(led_cdev->dev, &dev_attr_delay_on); |
| 161 | device_remove_file(led_cdev->dev, &dev_attr_delay_off); |
Richard Purdie | 6655c6f | 2006-03-31 02:31:07 -0800 | [diff] [blame] | 162 | del_timer_sync(&timer_data->timer); |
| 163 | kfree(timer_data); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static struct led_trigger timer_led_trigger = { |
| 168 | .name = "timer", |
| 169 | .activate = timer_trig_activate, |
| 170 | .deactivate = timer_trig_deactivate, |
| 171 | }; |
| 172 | |
| 173 | static int __init timer_trig_init(void) |
| 174 | { |
| 175 | return led_trigger_register(&timer_led_trigger); |
| 176 | } |
| 177 | |
| 178 | static void __exit timer_trig_exit(void) |
| 179 | { |
| 180 | led_trigger_unregister(&timer_led_trigger); |
| 181 | } |
| 182 | |
| 183 | module_init(timer_trig_init); |
| 184 | module_exit(timer_trig_exit); |
| 185 | |
| 186 | MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>"); |
| 187 | MODULE_DESCRIPTION("Timer LED trigger"); |
| 188 | MODULE_LICENSE("GPL"); |