blob: d87c9d02f786df2570385faea517e54e883c5b30 [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
Esben Haabendalfff26f82011-05-24 17:13:22 -070094 led_blink_set(led_cdev, &led_cdev->blink_delay_on,
95 &led_cdev->blink_delay_off);
96
Johannes Berg5ada28b2010-11-11 14:05:21 -080097 led_cdev->trigger_data = (void *)1;
Márton Németh4c791412007-10-31 15:07:12 +010098
Jeff Garzik12fda162006-10-17 00:10:20 -070099 return;
100
101err_out_delayon:
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100102 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800103}
104
105static void timer_trig_deactivate(struct led_classdev *led_cdev)
106{
Johannes Berg5ada28b2010-11-11 14:05:21 -0800107 if (led_cdev->trigger_data) {
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100108 device_remove_file(led_cdev->dev, &dev_attr_delay_on);
109 device_remove_file(led_cdev->dev, &dev_attr_delay_off);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800110 }
Rodolfo Giomettice70a242009-01-08 19:50:15 +0000111
Johannes Berg5ada28b2010-11-11 14:05:21 -0800112 /* Stop blinking */
113 led_brightness_set(led_cdev, LED_OFF);
Richard Purdie6655c6f2006-03-31 02:31:07 -0800114}
115
116static struct led_trigger timer_led_trigger = {
117 .name = "timer",
118 .activate = timer_trig_activate,
119 .deactivate = timer_trig_deactivate,
120};
121
122static int __init timer_trig_init(void)
123{
124 return led_trigger_register(&timer_led_trigger);
125}
126
127static void __exit timer_trig_exit(void)
128{
129 led_trigger_unregister(&timer_led_trigger);
130}
131
132module_init(timer_trig_init);
133module_exit(timer_trig_exit);
134
135MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
136MODULE_DESCRIPTION("Timer LED trigger");
137MODULE_LICENSE("GPL");