suspend.c 5.8 KB

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