suspend.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_prepare_suspend - save state and prepare to suspend
  108. *
  109. * NOTE! Devices cannot detach at this point - not only do we
  110. * hold the device list semaphores over the whole prepare, but
  111. * the whole point is to do non-invasive preparatory work, not
  112. * the actual suspend.
  113. */
  114. int device_prepare_suspend(pm_message_t state)
  115. {
  116. int error = 0;
  117. struct device * dev;
  118. down(&dpm_sem);
  119. down(&dpm_list_sem);
  120. list_for_each_entry_reverse(dev, &dpm_active, power.entry) {
  121. if (!dev->bus || !dev->bus->suspend_prepare)
  122. continue;
  123. error = dev->bus->suspend_prepare(dev, state);
  124. if (error)
  125. break;
  126. }
  127. up(&dpm_list_sem);
  128. up(&dpm_sem);
  129. return error;
  130. }
  131. /**
  132. * device_suspend - Save state and stop all devices in system.
  133. * @state: Power state to put each device in.
  134. *
  135. * Walk the dpm_active list, call ->suspend() for each device, and move
  136. * it to the dpm_off list.
  137. *
  138. * (For historical reasons, if it returns -EAGAIN, that used to mean
  139. * that the device would be called again with interrupts disabled.
  140. * These days, we use the "suspend_late()" callback for that, so we
  141. * print a warning and consider it an error).
  142. *
  143. * If we get a different error, try and back out.
  144. *
  145. * If we hit a failure with any of the devices, call device_resume()
  146. * above to bring the suspended devices back to life.
  147. *
  148. */
  149. int device_suspend(pm_message_t state)
  150. {
  151. int error = 0;
  152. down(&dpm_sem);
  153. down(&dpm_list_sem);
  154. while (!list_empty(&dpm_active) && error == 0) {
  155. struct list_head * entry = dpm_active.prev;
  156. struct device * dev = to_device(entry);
  157. get_device(dev);
  158. up(&dpm_list_sem);
  159. error = suspend_device(dev, state);
  160. down(&dpm_list_sem);
  161. /* Check if the device got removed */
  162. if (!list_empty(&dev->power.entry)) {
  163. /* Move it to the dpm_off list */
  164. if (!error)
  165. list_move(&dev->power.entry, &dpm_off);
  166. }
  167. if (error)
  168. printk(KERN_ERR "Could not suspend device %s: "
  169. "error %d%s\n",
  170. kobject_name(&dev->kobj), error,
  171. error == -EAGAIN ? " (please convert to suspend_late)" : "");
  172. put_device(dev);
  173. }
  174. up(&dpm_list_sem);
  175. if (error)
  176. dpm_resume();
  177. up(&dpm_sem);
  178. return error;
  179. }
  180. EXPORT_SYMBOL_GPL(device_suspend);
  181. /**
  182. * device_power_down - Shut down special devices.
  183. * @state: Power state to enter.
  184. *
  185. * Walk the dpm_off_irq list, calling ->power_down() for each device that
  186. * couldn't power down the device with interrupts enabled. When we're
  187. * done, power down system devices.
  188. */
  189. int device_power_down(pm_message_t state)
  190. {
  191. int error = 0;
  192. struct device * dev;
  193. while (!list_empty(&dpm_off)) {
  194. struct list_head * entry = dpm_off.prev;
  195. dev = to_device(entry);
  196. error = suspend_device_late(dev, state);
  197. if (error)
  198. goto Error;
  199. list_move(&dev->power.entry, &dpm_off_irq);
  200. }
  201. error = sysdev_suspend(state);
  202. Done:
  203. return error;
  204. Error:
  205. printk(KERN_ERR "Could not power down device %s: "
  206. "error %d\n", kobject_name(&dev->kobj), error);
  207. dpm_power_up();
  208. goto Done;
  209. }
  210. EXPORT_SYMBOL_GPL(device_power_down);
  211. void __suspend_report_result(const char *function, void *fn, int ret)
  212. {
  213. if (ret) {
  214. printk(KERN_ERR "%s(): ", function);
  215. print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
  216. printk("%d\n", ret);
  217. }
  218. }
  219. EXPORT_SYMBOL_GPL(__suspend_report_result);