sysfs.c 3.0 KB

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