sysfs.c 4.5 KB

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