suspend.c 6.1 KB

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