at v3.4-rc3 139 lines 3.3 kB view raw
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 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 22static 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 30static 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 int ret = -EINVAL; 35 char *after; 36 unsigned long state = simple_strtoul(buf, &after, 10); 37 size_t count = after - buf; 38 39 if (isspace(*after)) 40 count++; 41 42 if (count == size) { 43 led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off); 44 led_cdev->blink_delay_on = state; 45 ret = count; 46 } 47 48 return ret; 49} 50 51static ssize_t led_delay_off_show(struct device *dev, 52 struct device_attribute *attr, char *buf) 53{ 54 struct led_classdev *led_cdev = dev_get_drvdata(dev); 55 56 return sprintf(buf, "%lu\n", led_cdev->blink_delay_off); 57} 58 59static ssize_t led_delay_off_store(struct device *dev, 60 struct device_attribute *attr, const char *buf, size_t size) 61{ 62 struct led_classdev *led_cdev = dev_get_drvdata(dev); 63 int ret = -EINVAL; 64 char *after; 65 unsigned long state = simple_strtoul(buf, &after, 10); 66 size_t count = after - buf; 67 68 if (isspace(*after)) 69 count++; 70 71 if (count == size) { 72 led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state); 73 led_cdev->blink_delay_off = state; 74 ret = count; 75 } 76 77 return ret; 78} 79 80static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store); 81static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store); 82 83static void timer_trig_activate(struct led_classdev *led_cdev) 84{ 85 int rc; 86 87 led_cdev->trigger_data = NULL; 88 89 rc = device_create_file(led_cdev->dev, &dev_attr_delay_on); 90 if (rc) 91 return; 92 rc = device_create_file(led_cdev->dev, &dev_attr_delay_off); 93 if (rc) 94 goto err_out_delayon; 95 96 led_blink_set(led_cdev, &led_cdev->blink_delay_on, 97 &led_cdev->blink_delay_off); 98 99 led_cdev->trigger_data = (void *)1; 100 101 return; 102 103err_out_delayon: 104 device_remove_file(led_cdev->dev, &dev_attr_delay_on); 105} 106 107static void timer_trig_deactivate(struct led_classdev *led_cdev) 108{ 109 if (led_cdev->trigger_data) { 110 device_remove_file(led_cdev->dev, &dev_attr_delay_on); 111 device_remove_file(led_cdev->dev, &dev_attr_delay_off); 112 } 113 114 /* Stop blinking */ 115 led_brightness_set(led_cdev, LED_OFF); 116} 117 118static struct led_trigger timer_led_trigger = { 119 .name = "timer", 120 .activate = timer_trig_activate, 121 .deactivate = timer_trig_deactivate, 122}; 123 124static int __init timer_trig_init(void) 125{ 126 return led_trigger_register(&timer_led_trigger); 127} 128 129static void __exit timer_trig_exit(void) 130{ 131 led_trigger_unregister(&timer_led_trigger); 132} 133 134module_init(timer_trig_init); 135module_exit(timer_trig_exit); 136 137MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>"); 138MODULE_DESCRIPTION("Timer LED trigger"); 139MODULE_LICENSE("GPL");