sysfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "power.h"
  7. /**
  8. * state - Control current power state of device
  9. *
  10. * show() returns the current power state of the device. '0' indicates
  11. * the device is on. Other values (1-3) indicate the device is in a low
  12. * power state.
  13. *
  14. * store() sets the current power state, which is an integer value
  15. * between 0-3. If the device is on ('0'), and the value written is
  16. * greater than 0, then the device is placed directly into the low-power
  17. * state (via its driver's ->suspend() method).
  18. * If the device is currently in a low-power state, and the value is 0,
  19. * the device is powered back on (via the ->resume() method).
  20. * If the device is in a low-power state, and a different low-power state
  21. * is requested, the device is first resumed, then suspended into the new
  22. * low-power state.
  23. */
  24. static ssize_t state_show(struct device * dev, struct device_attribute *attr, char * buf)
  25. {
  26. return sprintf(buf, "%u\n", dev->power.power_state.event);
  27. }
  28. static ssize_t state_store(struct device * dev, struct device_attribute *attr, const char * buf, size_t n)
  29. {
  30. pm_message_t state;
  31. char * rest;
  32. int error = 0;
  33. state.event = simple_strtoul(buf, &rest, 10);
  34. if (*rest)
  35. return -EINVAL;
  36. if (state.event)
  37. error = dpm_runtime_suspend(dev, state);
  38. else
  39. dpm_runtime_resume(dev);
  40. return error ? error : n;
  41. }
  42. static DEVICE_ATTR(state, 0644, state_show, state_store);
  43. /*
  44. * wakeup - Report/change current wakeup option for device
  45. *
  46. * Some devices support "wakeup" events, which are hardware signals
  47. * used to activate devices from suspended or low power states. Such
  48. * devices have one of three values for the sysfs power/wakeup file:
  49. *
  50. * + "enabled\n" to issue the events;
  51. * + "disabled\n" not to do so; or
  52. * + "\n" for temporary or permanent inability to issue wakeup.
  53. *
  54. * (For example, unconfigured USB devices can't issue wakeups.)
  55. *
  56. * Familiar examples of devices that can issue wakeup events include
  57. * keyboards and mice (both PS2 and USB styles), power buttons, modems,
  58. * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
  59. * will wake the entire system from a suspend state; others may just
  60. * wake up the device (if the system as a whole is already active).
  61. * Some wakeup events use normal IRQ lines; other use special out
  62. * of band signaling.
  63. *
  64. * It is the responsibility of device drivers to enable (or disable)
  65. * wakeup signaling as part of changing device power states, respecting
  66. * the policy choices provided through the driver model.
  67. *
  68. * Devices may not be able to generate wakeup events from all power
  69. * states. Also, the events may be ignored in some configurations;
  70. * for example, they might need help from other devices that aren't
  71. * active, or which may have wakeup disabled. Some drivers rely on
  72. * wakeup events internally (unless they are disabled), keeping
  73. * their hardware in low power modes whenever they're unused. This
  74. * saves runtime power, without requiring system-wide sleep states.
  75. */
  76. static const char enabled[] = "enabled";
  77. static const char disabled[] = "disabled";
  78. static ssize_t
  79. wake_show(struct device * dev, struct device_attribute *attr, char * buf)
  80. {
  81. return sprintf(buf, "%s\n", device_can_wakeup(dev)
  82. ? (device_may_wakeup(dev) ? enabled : disabled)
  83. : "");
  84. }
  85. static ssize_t
  86. wake_store(struct device * dev, struct device_attribute *attr,
  87. const char * buf, size_t n)
  88. {
  89. char *cp;
  90. int len = n;
  91. if (!device_can_wakeup(dev))
  92. return -EINVAL;
  93. cp = memchr(buf, '\n', n);
  94. if (cp)
  95. len = cp - buf;
  96. if (len == sizeof enabled - 1
  97. && strncmp(buf, enabled, sizeof enabled - 1) == 0)
  98. device_set_wakeup_enable(dev, 1);
  99. else if (len == sizeof disabled - 1
  100. && strncmp(buf, disabled, sizeof disabled - 1) == 0)
  101. device_set_wakeup_enable(dev, 0);
  102. else
  103. return -EINVAL;
  104. return n;
  105. }
  106. static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
  107. static struct attribute * power_attrs[] = {
  108. &dev_attr_state.attr,
  109. &dev_attr_wakeup.attr,
  110. NULL,
  111. };
  112. static struct attribute_group pm_attr_group = {
  113. .name = "power",
  114. .attrs = power_attrs,
  115. };
  116. int dpm_sysfs_add(struct device * dev)
  117. {
  118. return sysfs_create_group(&dev->kobj, &pm_attr_group);
  119. }
  120. void dpm_sysfs_remove(struct device * dev)
  121. {
  122. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  123. }