sysfs.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * wakeup - Report/change current wakeup option for device
  9. *
  10. * Some devices support "wakeup" events, which are hardware signals
  11. * used to activate devices from suspended or low power states. Such
  12. * devices have one of three values for the sysfs power/wakeup file:
  13. *
  14. * + "enabled\n" to issue the events;
  15. * + "disabled\n" not to do so; or
  16. * + "\n" for temporary or permanent inability to issue wakeup.
  17. *
  18. * (For example, unconfigured USB devices can't issue wakeups.)
  19. *
  20. * Familiar examples of devices that can issue wakeup events include
  21. * keyboards and mice (both PS2 and USB styles), power buttons, modems,
  22. * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
  23. * will wake the entire system from a suspend state; others may just
  24. * wake up the device (if the system as a whole is already active).
  25. * Some wakeup events use normal IRQ lines; other use special out
  26. * of band signaling.
  27. *
  28. * It is the responsibility of device drivers to enable (or disable)
  29. * wakeup signaling as part of changing device power states, respecting
  30. * the policy choices provided through the driver model.
  31. *
  32. * Devices may not be able to generate wakeup events from all power
  33. * states. Also, the events may be ignored in some configurations;
  34. * for example, they might need help from other devices that aren't
  35. * active, or which may have wakeup disabled. Some drivers rely on
  36. * wakeup events internally (unless they are disabled), keeping
  37. * their hardware in low power modes whenever they're unused. This
  38. * saves runtime power, without requiring system-wide sleep states.
  39. */
  40. static const char enabled[] = "enabled";
  41. static const char disabled[] = "disabled";
  42. static ssize_t
  43. wake_show(struct device * dev, struct device_attribute *attr, char * buf)
  44. {
  45. return sprintf(buf, "%s\n", device_can_wakeup(dev)
  46. ? (device_may_wakeup(dev) ? enabled : disabled)
  47. : "");
  48. }
  49. static ssize_t
  50. wake_store(struct device * dev, struct device_attribute *attr,
  51. const char * buf, size_t n)
  52. {
  53. char *cp;
  54. int len = n;
  55. if (!device_can_wakeup(dev))
  56. return -EINVAL;
  57. cp = memchr(buf, '\n', n);
  58. if (cp)
  59. len = cp - buf;
  60. if (len == sizeof enabled - 1
  61. && strncmp(buf, enabled, sizeof enabled - 1) == 0)
  62. device_set_wakeup_enable(dev, 1);
  63. else if (len == sizeof disabled - 1
  64. && strncmp(buf, disabled, sizeof disabled - 1) == 0)
  65. device_set_wakeup_enable(dev, 0);
  66. else
  67. return -EINVAL;
  68. return n;
  69. }
  70. static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
  71. static struct attribute * power_attrs[] = {
  72. &dev_attr_wakeup.attr,
  73. NULL,
  74. };
  75. static struct attribute_group pm_attr_group = {
  76. .name = "power",
  77. .attrs = power_attrs,
  78. };
  79. int dpm_sysfs_add(struct device * dev)
  80. {
  81. return sysfs_create_group(&dev->kobj, &pm_attr_group);
  82. }
  83. void dpm_sysfs_remove(struct device * dev)
  84. {
  85. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  86. }