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