suspend.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. static inline char *suspend_verb(u32 event)
  30. {
  31. switch (event) {
  32. case PM_EVENT_SUSPEND: return "suspend";
  33. case PM_EVENT_FREEZE: return "freeze";
  34. default: return "(unknown suspend event)";
  35. }
  36. }
  37. /**
  38. * suspend_device - Save state of one device.
  39. * @dev: Device.
  40. * @state: Power state device is entering.
  41. */
  42. int suspend_device(struct device * dev, pm_message_t state)
  43. {
  44. int error = 0;
  45. down(&dev->sem);
  46. if (dev->power.power_state.event) {
  47. dev_dbg(dev, "PM: suspend %d-->%d\n",
  48. dev->power.power_state.event, state.event);
  49. }
  50. if (dev->power.pm_parent
  51. && dev->power.pm_parent->power.power_state.event) {
  52. dev_err(dev,
  53. "PM: suspend %d->%d, parent %s already %d\n",
  54. dev->power.power_state.event, state.event,
  55. dev->power.pm_parent->bus_id,
  56. dev->power.pm_parent->power.power_state.event);
  57. }
  58. dev->power.prev_state = dev->power.power_state;
  59. if (dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
  60. dev_dbg(dev, "%s%s\n",
  61. suspend_verb(state.event),
  62. ((state.event == PM_EVENT_SUSPEND)
  63. && device_may_wakeup(dev))
  64. ? ", may wakeup"
  65. : ""
  66. );
  67. error = dev->bus->suspend(dev, state);
  68. suspend_report_result(dev->bus->suspend, error);
  69. }
  70. up(&dev->sem);
  71. return error;
  72. }
  73. /**
  74. * device_suspend - Save state and stop all devices in system.
  75. * @state: Power state to put each device in.
  76. *
  77. * Walk the dpm_active list, call ->suspend() for each device, and move
  78. * it to dpm_off.
  79. * Check the return value for each. If it returns 0, then we move the
  80. * the device to the dpm_off list. If it returns -EAGAIN, we move it to
  81. * the dpm_off_irq list. If we get a different error, try and back out.
  82. *
  83. * If we hit a failure with any of the devices, call device_resume()
  84. * above to bring the suspended devices back to life.
  85. *
  86. */
  87. int device_suspend(pm_message_t state)
  88. {
  89. int error = 0;
  90. down(&dpm_sem);
  91. down(&dpm_list_sem);
  92. while (!list_empty(&dpm_active) && error == 0) {
  93. struct list_head * entry = dpm_active.prev;
  94. struct device * dev = to_device(entry);
  95. get_device(dev);
  96. up(&dpm_list_sem);
  97. error = suspend_device(dev, state);
  98. down(&dpm_list_sem);
  99. /* Check if the device got removed */
  100. if (!list_empty(&dev->power.entry)) {
  101. /* Move it to the dpm_off or dpm_off_irq list */
  102. if (!error)
  103. list_move(&dev->power.entry, &dpm_off);
  104. else if (error == -EAGAIN) {
  105. list_move(&dev->power.entry, &dpm_off_irq);
  106. error = 0;
  107. }
  108. }
  109. if (error)
  110. printk(KERN_ERR "Could not suspend device %s: "
  111. "error %d\n", kobject_name(&dev->kobj), error);
  112. put_device(dev);
  113. }
  114. up(&dpm_list_sem);
  115. if (error) {
  116. /* we failed... before resuming, bring back devices from
  117. * dpm_off_irq list back to main dpm_off list, we do want
  118. * to call resume() on them, in case they partially suspended
  119. * despite returning -EAGAIN
  120. */
  121. while (!list_empty(&dpm_off_irq)) {
  122. struct list_head * entry = dpm_off_irq.next;
  123. list_move(entry, &dpm_off);
  124. }
  125. dpm_resume();
  126. }
  127. up(&dpm_sem);
  128. return error;
  129. }
  130. EXPORT_SYMBOL_GPL(device_suspend);
  131. /**
  132. * device_power_down - Shut down special devices.
  133. * @state: Power state to enter.
  134. *
  135. * Walk the dpm_off_irq list, calling ->power_down() for each device that
  136. * couldn't power down the device with interrupts enabled. When we're
  137. * done, power down system devices.
  138. */
  139. int device_power_down(pm_message_t state)
  140. {
  141. int error = 0;
  142. struct device * dev;
  143. list_for_each_entry_reverse(dev, &dpm_off_irq, power.entry) {
  144. if ((error = suspend_device(dev, state)))
  145. break;
  146. }
  147. if (error)
  148. goto Error;
  149. if ((error = sysdev_suspend(state)))
  150. goto Error;
  151. Done:
  152. return error;
  153. Error:
  154. printk(KERN_ERR "Could not power down device %s: "
  155. "error %d\n", kobject_name(&dev->kobj), error);
  156. dpm_power_up();
  157. goto Done;
  158. }
  159. EXPORT_SYMBOL_GPL(device_power_down);
  160. void __suspend_report_result(const char *function, void *fn, int ret)
  161. {
  162. if (ret) {
  163. printk(KERN_ERR "%s(): ", function);
  164. print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
  165. printk("%d\n", ret);
  166. }
  167. }
  168. EXPORT_SYMBOL_GPL(__suspend_report_result);