suspend.c 4.7 KB

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