blob: b09bcbeade9c433c56365204b4848690bf08a366 [file] [log] [blame]
Richard Purdie6655c6f2006-03-31 02:31:07 -08001/*
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 Purdie6655c6f2006-03-31 02:31:07 -080014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
Richard Purdie6655c6f2006-03-31 02:31:07 -080017#include <linux/device.h>
Richard Purdie3dc7b822006-05-15 09:44:17 -070018#include <linux/ctype.h>
Richard Purdie6655c6f2006-03-31 02:31:07 -080019#include <linux/leds.h>
20#include "leds.h"
21
Németh Márton4d404fd2008-03-09 20:59:57 +000022static ssize_t led_delay_on_show(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010023 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -080024{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010025 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080026
Johannes Berg5ada28b2010-11-11 14:05:21 -080027 return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
Richard Purdie6655c6f2006-03-31 02:31:07 -080028}
29
Németh Márton4d404fd2008-03-09 20:59:57 +000030static ssize_t led_delay_on_store(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010031 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -080032{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010033 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080034 int ret = -EINVAL;
35 char *after;
36 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -070037 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -080038
André Goddard Rosae7d28602009-12-14 18:01:06 -080039 if (isspace(*after))
Richard Purdie3dc7b822006-05-15 09:44:17 -070040 count++;
41
42 if (count == size) {
Johannes Berg5ada28b2010-11-11 14:05:21 -080043 led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
Richard Purdie3dc7b822006-05-15 09:44:17 -070044 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -080045 }
46
47 return ret;
48}
49
Németh Márton4d404fd2008-03-09 20:59:57 +000050static ssize_t led_delay_off_show(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010051 struct device_attribute *attr, char *buf)
Richard Purdie6655c6f2006-03-31 02:31:07 -080052{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010053 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080054
Johannes Berg5ada28b2010-11-11 14:05:21 -080055 return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -080056}
57
Németh Márton4d404fd2008-03-09 20:59:57 +000058static ssize_t led_delay_off_store(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010059 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdie6655c6f2006-03-31 02:31:07 -080060{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010061 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdie6655c6f2006-03-31 02:31:07 -080062 int ret = -EINVAL;
63 char *after;
64 unsigned long state = simple_strtoul(buf, &after, 10);
Richard Purdie3dc7b822006-05-15 09:44:17 -070065 size_t count = after - buf;
Richard Purdie6655c6f2006-03-31 02:31:07 -080066
André Goddard Rosae7d28602009-12-14 18:01:06 -080067 if (isspace(*after))
Richard Purdie3dc7b822006-05-15 09:44:17 -070068 count++;
69
70 if (count == size) {
Johannes Berg5ada28b2010-11-11 14:05:21 -080071 led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
Richard Purdie3dc7b822006-05-15 09:44:17 -070072 ret = count;
Richard Purdie6655c6f2006-03-31 02:31:07 -080073 }
74
75 return ret;
76}
77
Richard Purdief8a7c6f2007-07-08 23:19:31 +010078static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
79static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
Richard Purdie6655c6f2006-03-31 02:31:07 -080080
81static void timer_trig_activate(struct led_classdev *led_cdev)
82{
Jeff Garzik12fda162006-10-17 00:10:20 -070083 int rc;
Richard Purdie6655c6f2006-03-31 02:31:07 -080084
Johannes Berg5ada28b2010-11-11 14:05:21 -080085 led_cdev->trigger_data = NULL;
Richard Purdie6655c6f2006-03-31 02:31:07 -080086
Richard Purdief8a7c6f2007-07-08 23:19:31 +010087 rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
88 if (rc)
Johannes Berg5ada28b2010-11-11 14:05:21 -080089 return;
Richard Purdief8a7c6f2007-07-08 23:19:31 +010090 rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
91 if (rc)
92 goto err_out_delayon;
Jeff Garzik12fda162006-10-17 00:10:20 -070093
Johannes Berg5ada28b2010-11-11 14:05:21 -080094 led_cdev->trigger_data = (void *)1;
Márton Németh4c791412007-10-31 15:07:12 +010095
Jeff Garzik12fda162006-10-17 00:10:20 -070096 return;
97
98err_out_delayon:
Richard Purdief8a7c6f2007-07-08 23:19:31 +010099 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800100}
101
102static void timer_trig_deactivate(struct led_classdev *led_cdev)
103{
Johannes Berg5ada28b2010-11-11 14:05:21 -0800104 if (led_cdev->trigger_data) {
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100105 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
106 device_remove_file(led_cdev->dev, &dev_attr_delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800107 }
Rodolfo Giomettice70a242009-01-08 19:50:15 +0000108
Johannes Berg5ada28b2010-11-11 14:05:21 -0800109 /* Stop blinking */
110 led_brightness_set(led_cdev, LED_OFF);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800111}
112
113static struct led_trigger timer_led_trigger = {
114 .name = "timer",
115 .activate = timer_trig_activate,
116 .deactivate = timer_trig_deactivate,
117};
118
119static int __init timer_trig_init(void)
120{
121 return led_trigger_register(&timer_led_trigger);
122}
123
124static void __exit timer_trig_exit(void)
125{
126 led_trigger_unregister(&timer_led_trigger);
127}
128
129module_init(timer_trig_init);
130module_exit(timer_trig_exit);
131
132MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
133MODULE_DESCRIPTION("Timer LED trigger");
134MODULE_LICENSE("GPL");