ledtrig-timer.c 3.3 KB

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