suspend.c 5.4 KB

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