suspend.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. case PM_EVENT_PRETHAW: return "prethaw";
  35. default: return "(unknown suspend event)";
  36. }
  37. }
  38. /**
  39. * suspend_device - Save state of one device.
  40. * @dev: Device.
  41. * @state: Power state device is entering.
  42. */
  43. int suspend_device(struct device * dev, pm_message_t state)
  44. {
  45. int error = 0;
  46. down(&dev->sem);
  47. if (dev->power.power_state.event) {
  48. dev_dbg(dev, "PM: suspend %d-->%d\n",
  49. dev->power.power_state.event, state.event);
  50. }
  51. if (dev->power.pm_parent
  52. && dev->power.pm_parent->power.power_state.event) {
  53. dev_err(dev,
  54. "PM: suspend %d->%d, parent %s already %d\n",
  55. dev->power.power_state.event, state.event,
  56. dev->power.pm_parent->bus_id,
  57. dev->power.pm_parent->power.power_state.event);
  58. }
  59. dev->power.prev_state = dev->power.power_state;
  60. if (dev->class && dev->class->suspend && !dev->power.power_state.event) {
  61. dev_dbg(dev, "class %s%s\n",
  62. suspend_verb(state.event),
  63. ((state.event == PM_EVENT_SUSPEND)
  64. && device_may_wakeup(dev))
  65. ? ", may wakeup"
  66. : ""
  67. );
  68. error = dev->class->suspend(dev, state);
  69. suspend_report_result(dev->class->suspend, error);
  70. }
  71. if (!error && dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
  72. dev_dbg(dev, "%s%s\n",
  73. suspend_verb(state.event),
  74. ((state.event == PM_EVENT_SUSPEND)
  75. && device_may_wakeup(dev))
  76. ? ", may wakeup"
  77. : ""
  78. );
  79. error = dev->bus->suspend(dev, state);
  80. suspend_report_result(dev->bus->suspend, error);
  81. }
  82. up(&dev->sem);
  83. return error;
  84. }
  85. /*
  86. * This is called with interrupts off, only a single CPU
  87. * running. We can't do down() on a semaphore (and we don't
  88. * need the protection)
  89. */
  90. static int suspend_device_late(struct device *dev, pm_message_t state)
  91. {
  92. int error = 0;
  93. if (dev->bus && dev->bus->suspend_late && !dev->power.power_state.event) {
  94. dev_dbg(dev, "LATE %s%s\n",
  95. suspend_verb(state.event),
  96. ((state.event == PM_EVENT_SUSPEND)
  97. && device_may_wakeup(dev))
  98. ? ", may wakeup"
  99. : ""
  100. );
  101. error = dev->bus->suspend_late(dev, state);
  102. suspend_report_result(dev->bus->suspend_late, error);
  103. }
  104. return error;
  105. }
  106. /**
  107. * device_suspend - Save state and stop all devices in system.
  108. * @state: Power state to put each device in.
  109. *
  110. * Walk the dpm_active list, call ->suspend() for each device, and move
  111. * it to the dpm_off list.
  112. *
  113. * (For historical reasons, if it returns -EAGAIN, that used to mean
  114. * that the device would be called again with interrupts disabled.
  115. * These days, we use the "suspend_late()" callback for that, so we
  116. * print a warning and consider it an error).
  117. *
  118. * If we get a different error, try and back out.
  119. *
  120. * If we hit a failure with any of the devices, call device_resume()
  121. * above to bring the suspended devices back to life.
  122. *
  123. */
  124. int device_suspend(pm_message_t state)
  125. {
  126. int error = 0;
  127. might_sleep();
  128. down(&dpm_sem);
  129. down(&dpm_list_sem);
  130. while (!list_empty(&dpm_active) && error == 0) {
  131. struct list_head * entry = dpm_active.prev;
  132. struct device * dev = to_device(entry);
  133. get_device(dev);
  134. up(&dpm_list_sem);
  135. error = suspend_device(dev, state);
  136. down(&dpm_list_sem);
  137. /* Check if the device got removed */
  138. if (!list_empty(&dev->power.entry)) {
  139. /* Move it to the dpm_off list */
  140. if (!error)
  141. list_move(&dev->power.entry, &dpm_off);
  142. }
  143. if (error)
  144. printk(KERN_ERR "Could not suspend device %s: "
  145. "error %d%s\n",
  146. kobject_name(&dev->kobj), error,
  147. error == -EAGAIN ? " (please convert to suspend_late)" : "");
  148. put_device(dev);
  149. }
  150. up(&dpm_list_sem);
  151. if (error)
  152. dpm_resume();
  153. up(&dpm_sem);
  154. return error;
  155. }
  156. EXPORT_SYMBOL_GPL(device_suspend);
  157. /**
  158. * device_power_down - Shut down special devices.
  159. * @state: Power state to enter.
  160. *
  161. * Walk the dpm_off_irq list, calling ->power_down() for each device that
  162. * couldn't power down the device with interrupts enabled. When we're
  163. * done, power down system devices.
  164. */
  165. int device_power_down(pm_message_t state)
  166. {
  167. int error = 0;
  168. struct device * dev;
  169. while (!list_empty(&dpm_off)) {
  170. struct list_head * entry = dpm_off.prev;
  171. dev = to_device(entry);
  172. error = suspend_device_late(dev, state);
  173. if (error)
  174. goto Error;
  175. list_move(&dev->power.entry, &dpm_off_irq);
  176. }
  177. error = sysdev_suspend(state);
  178. Done:
  179. return error;
  180. Error:
  181. printk(KERN_ERR "Could not power down device %s: "
  182. "error %d\n", kobject_name(&dev->kobj), error);
  183. dpm_power_up();
  184. goto Done;
  185. }
  186. EXPORT_SYMBOL_GPL(device_power_down);
  187. void __suspend_report_result(const char *function, void *fn, int ret)
  188. {
  189. if (ret) {
  190. printk(KERN_ERR "%s(): ", function);
  191. print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
  192. printk("%d\n", ret);
  193. }
  194. }
  195. EXPORT_SYMBOL_GPL(__suspend_report_result);