sysfs.c 4.2 KB

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