sysfs.c 4.8 KB

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