led-class.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * LED Class Core
  3. *
  4. * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
  5. * Copyright (C) 2005-2007 Richard Purdie <rpurdie@openedhand.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/list.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/device.h>
  17. #include <linux/timer.h>
  18. #include <linux/err.h>
  19. #include <linux/ctype.h>
  20. #include <linux/leds.h>
  21. #include "leds.h"
  22. #define LED_BUFF_SIZE 50
  23. static struct class *leds_class;
  24. static void led_update_brightness(struct led_classdev *led_cdev)
  25. {
  26. if (led_cdev->brightness_get)
  27. led_cdev->brightness = led_cdev->brightness_get(led_cdev);
  28. }
  29. static ssize_t led_brightness_show(struct device *dev,
  30. struct device_attribute *attr, char *buf)
  31. {
  32. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  33. /* no lock needed for this */
  34. led_update_brightness(led_cdev);
  35. return snprintf(buf, LED_BUFF_SIZE, "%u\n", led_cdev->brightness);
  36. }
  37. static ssize_t led_brightness_store(struct device *dev,
  38. struct device_attribute *attr, const char *buf, size_t size)
  39. {
  40. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  41. ssize_t ret = -EINVAL;
  42. char *after;
  43. unsigned long state = simple_strtoul(buf, &after, 10);
  44. size_t count = after - buf;
  45. if (isspace(*after))
  46. count++;
  47. if (count == size) {
  48. ret = count;
  49. if (state == LED_OFF)
  50. led_trigger_set_default(led_cdev);
  51. led_set_brightness(led_cdev, state);
  52. }
  53. return ret;
  54. }
  55. static ssize_t led_max_brightness_store(struct device *dev,
  56. struct device_attribute *attr, const char *buf, size_t size)
  57. {
  58. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  59. ssize_t ret = -EINVAL;
  60. unsigned long state = 0;
  61. ret = strict_strtoul(buf, 10, &state);
  62. if (!ret) {
  63. ret = size;
  64. if (state > LED_FULL)
  65. state = LED_FULL;
  66. led_cdev->max_brightness = state;
  67. led_set_brightness(led_cdev, led_cdev->brightness);
  68. }
  69. return ret;
  70. }
  71. static ssize_t led_max_brightness_show(struct device *dev,
  72. struct device_attribute *attr, char *buf)
  73. {
  74. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  75. return snprintf(buf, LED_BUFF_SIZE, "%u\n", led_cdev->max_brightness);
  76. }
  77. static struct device_attribute led_class_attrs[] = {
  78. __ATTR(brightness, 0644, led_brightness_show, led_brightness_store),
  79. __ATTR(max_brightness, 0644, led_max_brightness_show,
  80. led_max_brightness_store),
  81. #ifdef CONFIG_LEDS_TRIGGERS
  82. __ATTR(trigger, 0644, led_trigger_show, led_trigger_store),
  83. #endif
  84. __ATTR_NULL,
  85. };
  86. static void led_timer_function(unsigned long data)
  87. {
  88. struct led_classdev *led_cdev = (void *)data;
  89. unsigned long brightness;
  90. unsigned long delay;
  91. if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
  92. led_set_brightness(led_cdev, LED_OFF);
  93. return;
  94. }
  95. brightness = led_get_brightness(led_cdev);
  96. if (!brightness) {
  97. /* Time to switch the LED on. */
  98. brightness = led_cdev->blink_brightness;
  99. delay = led_cdev->blink_delay_on;
  100. } else {
  101. /* Store the current brightness value to be able
  102. * to restore it when the delay_off period is over.
  103. */
  104. led_cdev->blink_brightness = brightness;
  105. brightness = LED_OFF;
  106. delay = led_cdev->blink_delay_off;
  107. }
  108. led_set_brightness(led_cdev, brightness);
  109. mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
  110. }
  111. /**
  112. * led_classdev_suspend - suspend an led_classdev.
  113. * @led_cdev: the led_classdev to suspend.
  114. */
  115. void led_classdev_suspend(struct led_classdev *led_cdev)
  116. {
  117. led_cdev->flags |= LED_SUSPENDED;
  118. led_cdev->brightness_set(led_cdev, 0);
  119. }
  120. EXPORT_SYMBOL_GPL(led_classdev_suspend);
  121. /**
  122. * led_classdev_resume - resume an led_classdev.
  123. * @led_cdev: the led_classdev to resume.
  124. */
  125. void led_classdev_resume(struct led_classdev *led_cdev)
  126. {
  127. led_cdev->brightness_set(led_cdev, led_cdev->brightness);
  128. led_cdev->flags &= ~LED_SUSPENDED;
  129. }
  130. EXPORT_SYMBOL_GPL(led_classdev_resume);
  131. static int led_suspend(struct device *dev, pm_message_t state)
  132. {
  133. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  134. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  135. led_classdev_suspend(led_cdev);
  136. return 0;
  137. }
  138. static int led_resume(struct device *dev)
  139. {
  140. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  141. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  142. led_classdev_resume(led_cdev);
  143. return 0;
  144. }
  145. /**
  146. * led_classdev_register - register a new object of led_classdev class.
  147. * @parent: The device to register.
  148. * @led_cdev: the led_classdev structure for this device.
  149. */
  150. int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
  151. {
  152. led_cdev->dev = device_create(leds_class, parent, 0, led_cdev,
  153. "%s", led_cdev->name);
  154. if (IS_ERR(led_cdev->dev))
  155. return PTR_ERR(led_cdev->dev);
  156. #ifdef CONFIG_LEDS_TRIGGERS
  157. init_rwsem(&led_cdev->trigger_lock);
  158. #endif
  159. /* add to the list of leds */
  160. down_write(&leds_list_lock);
  161. list_add_tail(&led_cdev->node, &leds_list);
  162. up_write(&leds_list_lock);
  163. if (!led_cdev->max_brightness)
  164. led_cdev->max_brightness = LED_FULL;
  165. led_update_brightness(led_cdev);
  166. init_timer(&led_cdev->blink_timer);
  167. led_cdev->blink_timer.function = led_timer_function;
  168. led_cdev->blink_timer.data = (unsigned long)led_cdev;
  169. #ifdef CONFIG_LEDS_TRIGGERS
  170. led_trigger_set_default(led_cdev);
  171. #endif
  172. printk(KERN_DEBUG "Registered led device: %s\n",
  173. led_cdev->name);
  174. return 0;
  175. }
  176. EXPORT_SYMBOL_GPL(led_classdev_register);
  177. /**
  178. * led_classdev_unregister - unregisters a object of led_properties class.
  179. * @led_cdev: the led device to unregister
  180. *
  181. * Unregisters a previously registered via led_classdev_register object.
  182. */
  183. void led_classdev_unregister(struct led_classdev *led_cdev)
  184. {
  185. #ifdef CONFIG_LEDS_TRIGGERS
  186. down_write(&led_cdev->trigger_lock);
  187. if (led_cdev->trigger)
  188. led_trigger_set(led_cdev, NULL);
  189. up_write(&led_cdev->trigger_lock);
  190. #endif
  191. /* Stop blinking */
  192. led_brightness_set(led_cdev, LED_OFF);
  193. device_unregister(led_cdev->dev);
  194. down_write(&leds_list_lock);
  195. list_del(&led_cdev->node);
  196. up_write(&leds_list_lock);
  197. }
  198. EXPORT_SYMBOL_GPL(led_classdev_unregister);
  199. static int __init leds_init(void)
  200. {
  201. leds_class = class_create(THIS_MODULE, "leds");
  202. if (IS_ERR(leds_class))
  203. return PTR_ERR(leds_class);
  204. leds_class->suspend = led_suspend;
  205. leds_class->resume = led_resume;
  206. leds_class->dev_attrs = led_class_attrs;
  207. return 0;
  208. }
  209. static void __exit leds_exit(void)
  210. {
  211. class_destroy(leds_class);
  212. }
  213. subsys_initcall(leds_init);
  214. module_exit(leds_exit);
  215. MODULE_AUTHOR("John Lenz, Richard Purdie");
  216. MODULE_LICENSE("GPL");
  217. MODULE_DESCRIPTION("LED Class Interface");