led-class.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/sysdev.h>
  18. #include <linux/timer.h>
  19. #include <linux/err.h>
  20. #include <linux/ctype.h>
  21. #include <linux/leds.h>
  22. #include "leds.h"
  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 sprintf(buf, "%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 (*after && isspace(*after))
  46. count++;
  47. if (count == size) {
  48. ret = count;
  49. if (state == LED_OFF)
  50. led_trigger_remove(led_cdev);
  51. led_set_brightness(led_cdev, state);
  52. }
  53. return ret;
  54. }
  55. static ssize_t led_max_brightness_show(struct device *dev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  59. return sprintf(buf, "%u\n", led_cdev->max_brightness);
  60. }
  61. static DEVICE_ATTR(brightness, 0644, led_brightness_show, led_brightness_store);
  62. static DEVICE_ATTR(max_brightness, 0444, led_max_brightness_show, NULL);
  63. #ifdef CONFIG_LEDS_TRIGGERS
  64. static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store);
  65. #endif
  66. /**
  67. * led_classdev_suspend - suspend an led_classdev.
  68. * @led_cdev: the led_classdev to suspend.
  69. */
  70. void led_classdev_suspend(struct led_classdev *led_cdev)
  71. {
  72. led_cdev->flags |= LED_SUSPENDED;
  73. led_cdev->brightness_set(led_cdev, 0);
  74. }
  75. EXPORT_SYMBOL_GPL(led_classdev_suspend);
  76. /**
  77. * led_classdev_resume - resume an led_classdev.
  78. * @led_cdev: the led_classdev to resume.
  79. */
  80. void led_classdev_resume(struct led_classdev *led_cdev)
  81. {
  82. led_cdev->brightness_set(led_cdev, led_cdev->brightness);
  83. led_cdev->flags &= ~LED_SUSPENDED;
  84. }
  85. EXPORT_SYMBOL_GPL(led_classdev_resume);
  86. static int led_suspend(struct device *dev, pm_message_t state)
  87. {
  88. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  89. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  90. led_classdev_suspend(led_cdev);
  91. return 0;
  92. }
  93. static int led_resume(struct device *dev)
  94. {
  95. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  96. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  97. led_classdev_resume(led_cdev);
  98. return 0;
  99. }
  100. /**
  101. * led_classdev_register - register a new object of led_classdev class.
  102. * @parent: The device to register.
  103. * @led_cdev: the led_classdev structure for this device.
  104. */
  105. int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
  106. {
  107. int rc;
  108. led_cdev->dev = device_create(leds_class, parent, 0, led_cdev,
  109. "%s", led_cdev->name);
  110. if (IS_ERR(led_cdev->dev))
  111. return PTR_ERR(led_cdev->dev);
  112. /* register the attributes */
  113. rc = device_create_file(led_cdev->dev, &dev_attr_brightness);
  114. if (rc)
  115. goto err_out;
  116. #ifdef CONFIG_LEDS_TRIGGERS
  117. init_rwsem(&led_cdev->trigger_lock);
  118. #endif
  119. /* add to the list of leds */
  120. down_write(&leds_list_lock);
  121. list_add_tail(&led_cdev->node, &leds_list);
  122. up_write(&leds_list_lock);
  123. if (!led_cdev->max_brightness)
  124. led_cdev->max_brightness = LED_FULL;
  125. rc = device_create_file(led_cdev->dev, &dev_attr_max_brightness);
  126. if (rc)
  127. goto err_out_attr_max;
  128. led_update_brightness(led_cdev);
  129. #ifdef CONFIG_LEDS_TRIGGERS
  130. rc = device_create_file(led_cdev->dev, &dev_attr_trigger);
  131. if (rc)
  132. goto err_out_led_list;
  133. led_trigger_set_default(led_cdev);
  134. #endif
  135. printk(KERN_INFO "Registered led device: %s\n",
  136. led_cdev->name);
  137. return 0;
  138. #ifdef CONFIG_LEDS_TRIGGERS
  139. err_out_led_list:
  140. device_remove_file(led_cdev->dev, &dev_attr_max_brightness);
  141. #endif
  142. err_out_attr_max:
  143. device_remove_file(led_cdev->dev, &dev_attr_brightness);
  144. list_del(&led_cdev->node);
  145. err_out:
  146. device_unregister(led_cdev->dev);
  147. return rc;
  148. }
  149. EXPORT_SYMBOL_GPL(led_classdev_register);
  150. /**
  151. * led_classdev_unregister - unregisters a object of led_properties class.
  152. * @led_cdev: the led device to unregister
  153. *
  154. * Unregisters a previously registered via led_classdev_register object.
  155. */
  156. void led_classdev_unregister(struct led_classdev *led_cdev)
  157. {
  158. device_remove_file(led_cdev->dev, &dev_attr_max_brightness);
  159. device_remove_file(led_cdev->dev, &dev_attr_brightness);
  160. #ifdef CONFIG_LEDS_TRIGGERS
  161. device_remove_file(led_cdev->dev, &dev_attr_trigger);
  162. down_write(&led_cdev->trigger_lock);
  163. if (led_cdev->trigger)
  164. led_trigger_set(led_cdev, NULL);
  165. up_write(&led_cdev->trigger_lock);
  166. #endif
  167. device_unregister(led_cdev->dev);
  168. down_write(&leds_list_lock);
  169. list_del(&led_cdev->node);
  170. up_write(&leds_list_lock);
  171. }
  172. EXPORT_SYMBOL_GPL(led_classdev_unregister);
  173. static int __init leds_init(void)
  174. {
  175. leds_class = class_create(THIS_MODULE, "leds");
  176. if (IS_ERR(leds_class))
  177. return PTR_ERR(leds_class);
  178. leds_class->suspend = led_suspend;
  179. leds_class->resume = led_resume;
  180. return 0;
  181. }
  182. static void __exit leds_exit(void)
  183. {
  184. class_destroy(leds_class);
  185. }
  186. subsys_initcall(leds_init);
  187. module_exit(leds_exit);
  188. MODULE_AUTHOR("John Lenz, Richard Purdie");
  189. MODULE_LICENSE("GPL");
  190. MODULE_DESCRIPTION("LED Class Interface");