sysfs.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * drivers/base/power/sysfs.c - sysfs entries for device PM
  3. */
  4. #include <linux/device.h>
  5. #include <linux/string.h>
  6. #include <linux/pm_runtime.h>
  7. #include <asm/atomic.h>
  8. #include "power.h"
  9. /*
  10. * control - Report/change current runtime PM setting of the device
  11. *
  12. * Runtime power management of a device can be blocked with the help of
  13. * this attribute. All devices have one of the following two values for
  14. * the power/control file:
  15. *
  16. * + "auto\n" to allow the device to be power managed at run time;
  17. * + "on\n" to prevent the device from being power managed at run time;
  18. *
  19. * The default for all devices is "auto", which means that devices may be
  20. * subject to automatic power management, depending on their drivers.
  21. * Changing this attribute to "on" prevents the driver from power managing
  22. * the device at run time. Doing that while the device is suspended causes
  23. * it to be woken up.
  24. *
  25. * wakeup - Report/change current wakeup option for device
  26. *
  27. * Some devices support "wakeup" events, which are hardware signals
  28. * used to activate devices from suspended or low power states. Such
  29. * devices have one of three values for the sysfs power/wakeup file:
  30. *
  31. * + "enabled\n" to issue the events;
  32. * + "disabled\n" not to do so; or
  33. * + "\n" for temporary or permanent inability to issue wakeup.
  34. *
  35. * (For example, unconfigured USB devices can't issue wakeups.)
  36. *
  37. * Familiar examples of devices that can issue wakeup events include
  38. * keyboards and mice (both PS2 and USB styles), power buttons, modems,
  39. * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
  40. * will wake the entire system from a suspend state; others may just
  41. * wake up the device (if the system as a whole is already active).
  42. * Some wakeup events use normal IRQ lines; other use special out
  43. * of band signaling.
  44. *
  45. * It is the responsibility of device drivers to enable (or disable)
  46. * wakeup signaling as part of changing device power states, respecting
  47. * the policy choices provided through the driver model.
  48. *
  49. * Devices may not be able to generate wakeup events from all power
  50. * states. Also, the events may be ignored in some configurations;
  51. * for example, they might need help from other devices that aren't
  52. * active, or which may have wakeup disabled. Some drivers rely on
  53. * wakeup events internally (unless they are disabled), keeping
  54. * their hardware in low power modes whenever they're unused. This
  55. * saves runtime power, without requiring system-wide sleep states.
  56. *
  57. * async - Report/change current async suspend setting for the device
  58. *
  59. * Asynchronous suspend and resume of the device during system-wide power
  60. * state transitions can be enabled by writing "enabled" to this file.
  61. * Analogously, if "disabled" is written to this file, the device will be
  62. * suspended and resumed synchronously.
  63. *
  64. * All devices have one of the following two values for power/async:
  65. *
  66. * + "enabled\n" to permit the asynchronous suspend/resume of the device;
  67. * + "disabled\n" to forbid it;
  68. *
  69. * NOTE: It generally is unsafe to permit the asynchronous suspend/resume
  70. * of a device unless it is certain that all of the PM dependencies of the
  71. * device are known to the PM core. However, for some devices this
  72. * attribute is set to "enabled" by bus type code or device drivers and in
  73. * that cases it should be safe to leave the default value.
  74. */
  75. static const char enabled[] = "enabled";
  76. static const char disabled[] = "disabled";
  77. #ifdef CONFIG_PM_RUNTIME
  78. static const char ctrl_auto[] = "auto";
  79. static const char ctrl_on[] = "on";
  80. static ssize_t control_show(struct device *dev, struct device_attribute *attr,
  81. char *buf)
  82. {
  83. return sprintf(buf, "%s\n",
  84. dev->power.runtime_auto ? ctrl_auto : ctrl_on);
  85. }
  86. static ssize_t control_store(struct device * dev, struct device_attribute *attr,
  87. const char * buf, size_t n)
  88. {
  89. char *cp;
  90. int len = n;
  91. cp = memchr(buf, '\n', n);
  92. if (cp)
  93. len = cp - buf;
  94. if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
  95. pm_runtime_allow(dev);
  96. else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
  97. pm_runtime_forbid(dev);
  98. else
  99. return -EINVAL;
  100. return n;
  101. }
  102. static DEVICE_ATTR(control, 0644, control_show, control_store);
  103. #endif
  104. static ssize_t
  105. wake_show(struct device * dev, struct device_attribute *attr, char * buf)
  106. {
  107. return sprintf(buf, "%s\n", device_can_wakeup(dev)
  108. ? (device_may_wakeup(dev) ? enabled : disabled)
  109. : "");
  110. }
  111. static ssize_t
  112. wake_store(struct device * dev, struct device_attribute *attr,
  113. const char * buf, size_t n)
  114. {
  115. char *cp;
  116. int len = n;
  117. if (!device_can_wakeup(dev))
  118. return -EINVAL;
  119. cp = memchr(buf, '\n', n);
  120. if (cp)
  121. len = cp - buf;
  122. if (len == sizeof enabled - 1
  123. && strncmp(buf, enabled, sizeof enabled - 1) == 0)
  124. device_set_wakeup_enable(dev, 1);
  125. else if (len == sizeof disabled - 1
  126. && strncmp(buf, disabled, sizeof disabled - 1) == 0)
  127. device_set_wakeup_enable(dev, 0);
  128. else
  129. return -EINVAL;
  130. return n;
  131. }
  132. static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
  133. #ifdef CONFIG_PM_ADVANCED_DEBUG
  134. #ifdef CONFIG_PM_RUNTIME
  135. static ssize_t rtpm_usagecount_show(struct device *dev,
  136. struct device_attribute *attr, char *buf)
  137. {
  138. return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
  139. }
  140. static ssize_t rtpm_children_show(struct device *dev,
  141. struct device_attribute *attr, char *buf)
  142. {
  143. return sprintf(buf, "%d\n", dev->power.ignore_children ?
  144. 0 : atomic_read(&dev->power.child_count));
  145. }
  146. static ssize_t rtpm_enabled_show(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
  150. return sprintf(buf, "disabled & forbidden\n");
  151. else if (dev->power.disable_depth)
  152. return sprintf(buf, "disabled\n");
  153. else if (dev->power.runtime_auto == false)
  154. return sprintf(buf, "forbidden\n");
  155. return sprintf(buf, "enabled\n");
  156. }
  157. static ssize_t rtpm_status_show(struct device *dev,
  158. struct device_attribute *attr, char *buf)
  159. {
  160. if (dev->power.runtime_error)
  161. return sprintf(buf, "error\n");
  162. switch (dev->power.runtime_status) {
  163. case RPM_SUSPENDED:
  164. return sprintf(buf, "suspended\n");
  165. case RPM_SUSPENDING:
  166. return sprintf(buf, "suspending\n");
  167. case RPM_RESUMING:
  168. return sprintf(buf, "resuming\n");
  169. case RPM_ACTIVE:
  170. return sprintf(buf, "active\n");
  171. }
  172. return -EIO;
  173. }
  174. static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
  175. static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
  176. static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
  177. static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
  178. #endif
  179. static ssize_t async_show(struct device *dev, struct device_attribute *attr,
  180. char *buf)
  181. {
  182. return sprintf(buf, "%s\n",
  183. device_async_suspend_enabled(dev) ? enabled : disabled);
  184. }
  185. static ssize_t async_store(struct device *dev, struct device_attribute *attr,
  186. const char *buf, size_t n)
  187. {
  188. char *cp;
  189. int len = n;
  190. cp = memchr(buf, '\n', n);
  191. if (cp)
  192. len = cp - buf;
  193. if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
  194. device_enable_async_suspend(dev);
  195. else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
  196. device_disable_async_suspend(dev);
  197. else
  198. return -EINVAL;
  199. return n;
  200. }
  201. static DEVICE_ATTR(async, 0644, async_show, async_store);
  202. #endif /* CONFIG_PM_ADVANCED_DEBUG */
  203. static struct attribute * power_attrs[] = {
  204. #ifdef CONFIG_PM_RUNTIME
  205. &dev_attr_control.attr,
  206. #endif
  207. &dev_attr_wakeup.attr,
  208. #ifdef CONFIG_PM_ADVANCED_DEBUG
  209. &dev_attr_async.attr,
  210. #ifdef CONFIG_PM_RUNTIME
  211. &dev_attr_runtime_usage.attr,
  212. &dev_attr_runtime_active_kids.attr,
  213. &dev_attr_runtime_status.attr,
  214. &dev_attr_runtime_enabled.attr,
  215. #endif
  216. #endif
  217. NULL,
  218. };
  219. static struct attribute_group pm_attr_group = {
  220. .name = "power",
  221. .attrs = power_attrs,
  222. };
  223. int dpm_sysfs_add(struct device * dev)
  224. {
  225. return sysfs_create_group(&dev->kobj, &pm_attr_group);
  226. }
  227. void dpm_sysfs_remove(struct device * dev)
  228. {
  229. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  230. }