suspend.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.event) {
  37. dev_dbg(dev, "PM: suspend %d-->%d\n",
  38. dev->power.power_state.event, state.event);
  39. }
  40. if (dev->power.pm_parent
  41. && dev->power.pm_parent->power.power_state.event) {
  42. dev_err(dev,
  43. "PM: suspend %d->%d, parent %s already %d\n",
  44. dev->power.power_state.event, state.event,
  45. dev->power.pm_parent->bus_id,
  46. dev->power.pm_parent->power.power_state.event);
  47. }
  48. dev->power.prev_state = dev->power.power_state;
  49. if (dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
  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. /* we failed... before resuming, bring back devices from
  102. * dpm_off_irq list back to main dpm_off list, we do want
  103. * to call resume() on them, in case they partially suspended
  104. * despite returning -EAGAIN
  105. */
  106. while (!list_empty(&dpm_off_irq)) {
  107. struct list_head * entry = dpm_off_irq.next;
  108. list_del(entry);
  109. list_add(entry, &dpm_off);
  110. }
  111. dpm_resume();
  112. }
  113. up(&dpm_sem);
  114. return error;
  115. }
  116. EXPORT_SYMBOL_GPL(device_suspend);
  117. /**
  118. * device_power_down - Shut down special devices.
  119. * @state: Power state to enter.
  120. *
  121. * Walk the dpm_off_irq list, calling ->power_down() for each device that
  122. * couldn't power down the device with interrupts enabled. When we're
  123. * done, power down system devices.
  124. */
  125. int device_power_down(pm_message_t state)
  126. {
  127. int error = 0;
  128. struct device * dev;
  129. list_for_each_entry_reverse(dev, &dpm_off_irq, power.entry) {
  130. if ((error = suspend_device(dev, state)))
  131. break;
  132. }
  133. if (error)
  134. goto Error;
  135. if ((error = sysdev_suspend(state)))
  136. goto Error;
  137. Done:
  138. return error;
  139. Error:
  140. printk(KERN_ERR "Could not power down device %s: "
  141. "error %d\n", kobject_name(&dev->kobj), error);
  142. dpm_power_up();
  143. goto Done;
  144. }
  145. EXPORT_SYMBOL_GPL(device_power_down);