sysfs.c 9.2 KB

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