sysfs.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. static struct attribute * power_attrs[] = {
  43. &dev_attr_state.attr,
  44. NULL,
  45. };
  46. static struct attribute_group pm_attr_group = {
  47. .name = "power",
  48. .attrs = power_attrs,
  49. };
  50. int dpm_sysfs_add(struct device * dev)
  51. {
  52. return sysfs_create_group(&dev->kobj, &pm_attr_group);
  53. }
  54. void dpm_sysfs_remove(struct device * dev)
  55. {
  56. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  57. }