ledtrig-timer.c 3.1 KB

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