suspend.c 6.3 KB

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