ledtrig-timer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/device.h>
  17. #include <linux/ctype.h>
  18. #include <linux/leds.h>
  19. #include "leds.h"
  20. static ssize_t led_delay_on_show(struct device *dev,
  21. struct device_attribute *attr, char *buf)
  22. {
  23. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  24. return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
  25. }
  26. static ssize_t led_delay_on_store(struct device *dev,
  27. struct device_attribute *attr, const char *buf, size_t size)
  28. {
  29. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  30. int ret = -EINVAL;
  31. char *after;
  32. unsigned long state = simple_strtoul(buf, &after, 10);
  33. size_t count = after - buf;
  34. if (isspace(*after))
  35. count++;
  36. if (count == size) {
  37. led_blink_set(led_cdev, &state, &led_cdev->blink_delay_off);
  38. ret = count;
  39. }
  40. return ret;
  41. }
  42. static ssize_t led_delay_off_show(struct device *dev,
  43. struct device_attribute *attr, char *buf)
  44. {
  45. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  46. return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
  47. }
  48. static ssize_t led_delay_off_store(struct device *dev,
  49. struct device_attribute *attr, const char *buf, size_t size)
  50. {
  51. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  52. int ret = -EINVAL;
  53. char *after;
  54. unsigned long state = simple_strtoul(buf, &after, 10);
  55. size_t count = after - buf;
  56. if (isspace(*after))
  57. count++;
  58. if (count == size) {
  59. led_blink_set(led_cdev, &led_cdev->blink_delay_on, &state);
  60. ret = count;
  61. }
  62. return ret;
  63. }
  64. static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
  65. static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
  66. static void timer_trig_activate(struct led_classdev *led_cdev)
  67. {
  68. int rc;
  69. led_cdev->trigger_data = NULL;
  70. rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
  71. if (rc)
  72. return;
  73. rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
  74. if (rc)
  75. goto err_out_delayon;
  76. led_blink_set(led_cdev, &led_cdev->blink_delay_on,
  77. &led_cdev->blink_delay_off);
  78. led_cdev->trigger_data = (void *)1;
  79. return;
  80. err_out_delayon:
  81. device_remove_file(led_cdev->dev, &dev_attr_delay_on);
  82. }
  83. static void timer_trig_deactivate(struct led_classdev *led_cdev)
  84. {
  85. if (led_cdev->trigger_data) {
  86. device_remove_file(led_cdev->dev, &dev_attr_delay_on);
  87. device_remove_file(led_cdev->dev, &dev_attr_delay_off);
  88. }
  89. /* Stop blinking */
  90. led_brightness_set(led_cdev, LED_OFF);
  91. }
  92. static struct led_trigger timer_led_trigger = {
  93. .name = "timer",
  94. .activate = timer_trig_activate,
  95. .deactivate = timer_trig_deactivate,
  96. };
  97. static int __init timer_trig_init(void)
  98. {
  99. return led_trigger_register(&timer_led_trigger);
  100. }
  101. static void __exit timer_trig_exit(void)
  102. {
  103. led_trigger_unregister(&timer_led_trigger);
  104. }
  105. module_init(timer_trig_init);
  106. module_exit(timer_trig_exit);
  107. MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
  108. MODULE_DESCRIPTION("Timer LED trigger");
  109. MODULE_LICENSE("GPL");