led-class.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/ctype.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/leds.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/timer.h>
  22. #include <uapi/linux/uleds.h>
  23. #include "leds.h"
  24. static struct class *leds_class;
  25. static ssize_t brightness_show(struct device *dev,
  26. struct device_attribute *attr, char *buf)
  27. {
  28. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  29. /* no lock needed for this */
  30. led_update_brightness(led_cdev);
  31. return sprintf(buf, "%u\n", led_cdev->brightness);
  32. }
  33. static ssize_t brightness_store(struct device *dev,
  34. struct device_attribute *attr, const char *buf, size_t size)
  35. {
  36. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  37. unsigned long state;
  38. ssize_t ret;
  39. mutex_lock(&led_cdev->led_access);
  40. if (led_sysfs_is_disabled(led_cdev)) {
  41. ret = -EBUSY;
  42. goto unlock;
  43. }
  44. ret = kstrtoul(buf, 10, &state);
  45. if (ret)
  46. goto unlock;
  47. if (state == LED_OFF)
  48. led_trigger_remove(led_cdev);
  49. led_set_brightness(led_cdev, state);
  50. ret = size;
  51. unlock:
  52. mutex_unlock(&led_cdev->led_access);
  53. return ret;
  54. }
  55. static DEVICE_ATTR_RW(brightness);
  56. static ssize_t max_brightness_show(struct device *dev,
  57. struct device_attribute *attr, char *buf)
  58. {
  59. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  60. return sprintf(buf, "%u\n", led_cdev->max_brightness);
  61. }
  62. static DEVICE_ATTR_RO(max_brightness);
  63. #ifdef CONFIG_LEDS_TRIGGERS
  64. static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store);
  65. static struct attribute *led_trigger_attrs[] = {
  66. &dev_attr_trigger.attr,
  67. NULL,
  68. };
  69. static const struct attribute_group led_trigger_group = {
  70. .attrs = led_trigger_attrs,
  71. };
  72. #endif
  73. static struct attribute *led_class_attrs[] = {
  74. &dev_attr_brightness.attr,
  75. &dev_attr_max_brightness.attr,
  76. NULL,
  77. };
  78. static const struct attribute_group led_group = {
  79. .attrs = led_class_attrs,
  80. };
  81. static const struct attribute_group *led_groups[] = {
  82. &led_group,
  83. #ifdef CONFIG_LEDS_TRIGGERS
  84. &led_trigger_group,
  85. #endif
  86. NULL,
  87. };
  88. #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
  89. static ssize_t brightness_hw_changed_show(struct device *dev,
  90. struct device_attribute *attr, char *buf)
  91. {
  92. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  93. if (led_cdev->brightness_hw_changed == -1)
  94. return -ENODATA;
  95. return sprintf(buf, "%u\n", led_cdev->brightness_hw_changed);
  96. }
  97. static DEVICE_ATTR_RO(brightness_hw_changed);
  98. static int led_add_brightness_hw_changed(struct led_classdev *led_cdev)
  99. {
  100. struct device *dev = led_cdev->dev;
  101. int ret;
  102. ret = device_create_file(dev, &dev_attr_brightness_hw_changed);
  103. if (ret) {
  104. dev_err(dev, "Error creating brightness_hw_changed\n");
  105. return ret;
  106. }
  107. led_cdev->brightness_hw_changed_kn =
  108. sysfs_get_dirent(dev->kobj.sd, "brightness_hw_changed");
  109. if (!led_cdev->brightness_hw_changed_kn) {
  110. dev_err(dev, "Error getting brightness_hw_changed kn\n");
  111. device_remove_file(dev, &dev_attr_brightness_hw_changed);
  112. return -ENXIO;
  113. }
  114. return 0;
  115. }
  116. static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev)
  117. {
  118. sysfs_put(led_cdev->brightness_hw_changed_kn);
  119. device_remove_file(led_cdev->dev, &dev_attr_brightness_hw_changed);
  120. }
  121. void led_classdev_notify_brightness_hw_changed(struct led_classdev *led_cdev,
  122. enum led_brightness brightness)
  123. {
  124. if (WARN_ON(!led_cdev->brightness_hw_changed_kn))
  125. return;
  126. led_cdev->brightness_hw_changed = brightness;
  127. sysfs_notify_dirent(led_cdev->brightness_hw_changed_kn);
  128. }
  129. EXPORT_SYMBOL_GPL(led_classdev_notify_brightness_hw_changed);
  130. #else
  131. static int led_add_brightness_hw_changed(struct led_classdev *led_cdev)
  132. {
  133. return 0;
  134. }
  135. static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev)
  136. {
  137. }
  138. #endif
  139. /**
  140. * led_classdev_suspend - suspend an led_classdev.
  141. * @led_cdev: the led_classdev to suspend.
  142. */
  143. void led_classdev_suspend(struct led_classdev *led_cdev)
  144. {
  145. led_cdev->flags |= LED_SUSPENDED;
  146. led_set_brightness_nopm(led_cdev, 0);
  147. }
  148. EXPORT_SYMBOL_GPL(led_classdev_suspend);
  149. /**
  150. * led_classdev_resume - resume an led_classdev.
  151. * @led_cdev: the led_classdev to resume.
  152. */
  153. void led_classdev_resume(struct led_classdev *led_cdev)
  154. {
  155. led_set_brightness_nopm(led_cdev, led_cdev->brightness);
  156. if (led_cdev->flash_resume)
  157. led_cdev->flash_resume(led_cdev);
  158. led_cdev->flags &= ~LED_SUSPENDED;
  159. }
  160. EXPORT_SYMBOL_GPL(led_classdev_resume);
  161. #ifdef CONFIG_PM_SLEEP
  162. static int led_suspend(struct device *dev)
  163. {
  164. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  165. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  166. led_classdev_suspend(led_cdev);
  167. return 0;
  168. }
  169. static int led_resume(struct device *dev)
  170. {
  171. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  172. if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
  173. led_classdev_resume(led_cdev);
  174. return 0;
  175. }
  176. #endif
  177. static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume);
  178. static int match_name(struct device *dev, const void *data)
  179. {
  180. if (!dev_name(dev))
  181. return 0;
  182. return !strcmp(dev_name(dev), (char *)data);
  183. }
  184. static int led_classdev_next_name(const char *init_name, char *name,
  185. size_t len)
  186. {
  187. unsigned int i = 0;
  188. int ret = 0;
  189. struct device *dev;
  190. strlcpy(name, init_name, len);
  191. while ((ret < len) &&
  192. (dev = class_find_device(leds_class, NULL, name, match_name))) {
  193. put_device(dev);
  194. ret = snprintf(name, len, "%s_%u", init_name, ++i);
  195. }
  196. if (ret >= len)
  197. return -ENOMEM;
  198. return i;
  199. }
  200. /**
  201. * led_classdev_register - register a new object of led_classdev class.
  202. * @parent: The device to register.
  203. * @led_cdev: the led_classdev structure for this device.
  204. */
  205. int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
  206. {
  207. char name[LED_MAX_NAME_SIZE];
  208. int ret;
  209. ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
  210. if (ret < 0)
  211. return ret;
  212. led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
  213. led_cdev, led_cdev->groups, "%s", name);
  214. if (IS_ERR(led_cdev->dev))
  215. return PTR_ERR(led_cdev->dev);
  216. if (ret)
  217. dev_warn(parent, "Led %s renamed to %s due to name collision",
  218. led_cdev->name, dev_name(led_cdev->dev));
  219. if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) {
  220. ret = led_add_brightness_hw_changed(led_cdev);
  221. if (ret) {
  222. device_unregister(led_cdev->dev);
  223. return ret;
  224. }
  225. }
  226. led_cdev->work_flags = 0;
  227. #ifdef CONFIG_LEDS_TRIGGERS
  228. init_rwsem(&led_cdev->trigger_lock);
  229. #endif
  230. #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
  231. led_cdev->brightness_hw_changed = -1;
  232. #endif
  233. mutex_init(&led_cdev->led_access);
  234. /* add to the list of leds */
  235. down_write(&leds_list_lock);
  236. list_add_tail(&led_cdev->node, &leds_list);
  237. up_write(&leds_list_lock);
  238. if (!led_cdev->max_brightness)
  239. led_cdev->max_brightness = LED_FULL;
  240. led_update_brightness(led_cdev);
  241. led_init_core(led_cdev);
  242. #ifdef CONFIG_LEDS_TRIGGERS
  243. led_trigger_set_default(led_cdev);
  244. #endif
  245. dev_dbg(parent, "Registered led device: %s\n",
  246. led_cdev->name);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL_GPL(led_classdev_register);
  250. /**
  251. * led_classdev_unregister - unregisters a object of led_properties class.
  252. * @led_cdev: the led device to unregister
  253. *
  254. * Unregisters a previously registered via led_classdev_register object.
  255. */
  256. void led_classdev_unregister(struct led_classdev *led_cdev)
  257. {
  258. #ifdef CONFIG_LEDS_TRIGGERS
  259. down_write(&led_cdev->trigger_lock);
  260. if (led_cdev->trigger)
  261. led_trigger_set(led_cdev, NULL);
  262. up_write(&led_cdev->trigger_lock);
  263. #endif
  264. led_cdev->flags |= LED_UNREGISTERING;
  265. /* Stop blinking */
  266. led_stop_software_blink(led_cdev);
  267. led_set_brightness(led_cdev, LED_OFF);
  268. flush_work(&led_cdev->set_brightness_work);
  269. if (led_cdev->flags & LED_BRIGHT_HW_CHANGED)
  270. led_remove_brightness_hw_changed(led_cdev);
  271. device_unregister(led_cdev->dev);
  272. down_write(&leds_list_lock);
  273. list_del(&led_cdev->node);
  274. up_write(&leds_list_lock);
  275. mutex_destroy(&led_cdev->led_access);
  276. }
  277. EXPORT_SYMBOL_GPL(led_classdev_unregister);
  278. static void devm_led_classdev_release(struct device *dev, void *res)
  279. {
  280. led_classdev_unregister(*(struct led_classdev **)res);
  281. }
  282. /**
  283. * devm_led_classdev_register - resource managed led_classdev_register()
  284. * @parent: The device to register.
  285. * @led_cdev: the led_classdev structure for this device.
  286. */
  287. int devm_led_classdev_register(struct device *parent,
  288. struct led_classdev *led_cdev)
  289. {
  290. struct led_classdev **dr;
  291. int rc;
  292. dr = devres_alloc(devm_led_classdev_release, sizeof(*dr), GFP_KERNEL);
  293. if (!dr)
  294. return -ENOMEM;
  295. rc = led_classdev_register(parent, led_cdev);
  296. if (rc) {
  297. devres_free(dr);
  298. return rc;
  299. }
  300. *dr = led_cdev;
  301. devres_add(parent, dr);
  302. return 0;
  303. }
  304. EXPORT_SYMBOL_GPL(devm_led_classdev_register);
  305. static int devm_led_classdev_match(struct device *dev, void *res, void *data)
  306. {
  307. struct led_cdev **p = res;
  308. if (WARN_ON(!p || !*p))
  309. return 0;
  310. return *p == data;
  311. }
  312. /**
  313. * devm_led_classdev_unregister() - resource managed led_classdev_unregister()
  314. * @parent: The device to unregister.
  315. * @led_cdev: the led_classdev structure for this device.
  316. */
  317. void devm_led_classdev_unregister(struct device *dev,
  318. struct led_classdev *led_cdev)
  319. {
  320. WARN_ON(devres_release(dev,
  321. devm_led_classdev_release,
  322. devm_led_classdev_match, led_cdev));
  323. }
  324. EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
  325. static int __init leds_init(void)
  326. {
  327. leds_class = class_create(THIS_MODULE, "leds");
  328. if (IS_ERR(leds_class))
  329. return PTR_ERR(leds_class);
  330. leds_class->pm = &leds_class_dev_pm_ops;
  331. leds_class->dev_groups = led_groups;
  332. return 0;
  333. }
  334. static void __exit leds_exit(void)
  335. {
  336. class_destroy(leds_class);
  337. }
  338. subsys_initcall(leds_init);
  339. module_exit(leds_exit);
  340. MODULE_AUTHOR("John Lenz, Richard Purdie");
  341. MODULE_LICENSE("GPL");
  342. MODULE_DESCRIPTION("LED Class Interface");