sysfs.c 6.1 KB

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