suspend.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * suspend.c - Functions for putting devices to sleep.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Labs
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. */
  10. #include <linux/device.h>
  11. #include "power.h"
  12. extern int sysdev_suspend(pm_message_t state);
  13. /*
  14. * The entries in the dpm_active list are in a depth first order, simply
  15. * because children are guaranteed to be discovered after parents, and
  16. * are inserted at the back of the list on discovery.
  17. *
  18. * All list on the suspend path are done in reverse order, so we operate
  19. * on the leaves of the device tree (or forests, depending on how you want
  20. * to look at it ;) first. As nodes are removed from the back of the list,
  21. * they are inserted into the front of their destintation lists.
  22. *
  23. * Things are the reverse on the resume path - iterations are done in
  24. * forward order, and nodes are inserted at the back of their destination
  25. * lists. This way, the ancestors will be accessed before their descendents.
  26. */
  27. /**
  28. * suspend_device - Save state of one device.
  29. * @dev: Device.
  30. * @state: Power state device is entering.
  31. */
  32. int suspend_device(struct device * dev, pm_message_t state)
  33. {
  34. int error = 0;
  35. down(&dev->sem);
  36. if (dev->power.power_state) {
  37. dev_dbg(dev, "PM: suspend %d-->%d\n",
  38. dev->power.power_state, state);
  39. }
  40. if (dev->power.pm_parent
  41. && dev->power.pm_parent->power.power_state) {
  42. dev_err(dev,
  43. "PM: suspend %d->%d, parent %s already %d\n",
  44. dev->power.power_state, state,
  45. dev->power.pm_parent->bus_id,
  46. dev->power.pm_parent->power.power_state);
  47. }
  48. dev->power.prev_state = dev->power.power_state;
  49. if (dev->bus && dev->bus->suspend && !dev->power.power_state) {
  50. dev_dbg(dev, "suspending\n");
  51. error = dev->bus->suspend(dev, state);
  52. }
  53. up(&dev->sem);
  54. return error;
  55. }
  56. /**
  57. * device_suspend - Save state and stop all devices in system.
  58. * @state: Power state to put each device in.
  59. *
  60. * Walk the dpm_active list, call ->suspend() for each device, and move
  61. * it to dpm_off.
  62. * Check the return value for each. If it returns 0, then we move the
  63. * the device to the dpm_off list. If it returns -EAGAIN, we move it to
  64. * the dpm_off_irq list. If we get a different error, try and back out.
  65. *
  66. * If we hit a failure with any of the devices, call device_resume()
  67. * above to bring the suspended devices back to life.
  68. *
  69. */
  70. int device_suspend(pm_message_t state)
  71. {
  72. int error = 0;
  73. down(&dpm_sem);
  74. down(&dpm_list_sem);
  75. while (!list_empty(&dpm_active) && error == 0) {
  76. struct list_head * entry = dpm_active.prev;
  77. struct device * dev = to_device(entry);
  78. get_device(dev);
  79. up(&dpm_list_sem);
  80. error = suspend_device(dev, state);
  81. down(&dpm_list_sem);
  82. /* Check if the device got removed */
  83. if (!list_empty(&dev->power.entry)) {
  84. /* Move it to the dpm_off or dpm_off_irq list */
  85. if (!error) {
  86. list_del(&dev->power.entry);
  87. list_add(&dev->power.entry, &dpm_off);
  88. } else if (error == -EAGAIN) {
  89. list_del(&dev->power.entry);
  90. list_add(&dev->power.entry, &dpm_off_irq);
  91. error = 0;
  92. }
  93. }
  94. if (error)
  95. printk(KERN_ERR "Could not suspend device %s: "
  96. "error %d\n", kobject_name(&dev->kobj), error);
  97. put_device(dev);
  98. }
  99. up(&dpm_list_sem);
  100. if (error)
  101. dpm_resume();
  102. up(&dpm_sem);
  103. return error;
  104. }
  105. EXPORT_SYMBOL_GPL(device_suspend);
  106. /**
  107. * device_power_down - Shut down special devices.
  108. * @state: Power state to enter.
  109. *
  110. * Walk the dpm_off_irq list, calling ->power_down() for each device that
  111. * couldn't power down the device with interrupts enabled. When we're
  112. * done, power down system devices.
  113. */
  114. int device_power_down(pm_message_t state)
  115. {
  116. int error = 0;
  117. struct device * dev;
  118. list_for_each_entry_reverse(dev, &dpm_off_irq, power.entry) {
  119. if ((error = suspend_device(dev, state)))
  120. break;
  121. }
  122. if (error)
  123. goto Error;
  124. if ((error = sysdev_suspend(state)))
  125. goto Error;
  126. Done:
  127. return error;
  128. Error:
  129. printk(KERN_ERR "Could not power down device %s: "
  130. "error %d\n", kobject_name(&dev->kobj), error);
  131. dpm_power_up();
  132. goto Done;
  133. }
  134. EXPORT_SYMBOL_GPL(device_power_down);