sysfs.c 4.7 KB

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