suspend.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/vt_kern.h>
  11. #include <linux/device.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/pm.h>
  14. #include "../base.h"
  15. #include "power.h"
  16. /*
  17. * The entries in the dpm_active list are in a depth first order, simply
  18. * because children are guaranteed to be discovered after parents, and
  19. * are inserted at the back of the list on discovery.
  20. *
  21. * All list on the suspend path are done in reverse order, so we operate
  22. * on the leaves of the device tree (or forests, depending on how you want
  23. * to look at it ;) first. As nodes are removed from the back of the list,
  24. * they are inserted into the front of their destintation lists.
  25. *
  26. * Things are the reverse on the resume path - iterations are done in
  27. * forward order, and nodes are inserted at the back of their destination
  28. * lists. This way, the ancestors will be accessed before their descendents.
  29. */
  30. /**
  31. * suspend_device - Save state of one device.
  32. * @dev: Device.
  33. * @state: Power state device is entering.
  34. */
  35. int suspend_device(struct device * dev, pm_message_t state)
  36. {
  37. int error = 0;
  38. down(&dev->sem);
  39. if (dev->power.power_state.event) {
  40. dev_dbg(dev, "PM: suspend %d-->%d\n",
  41. dev->power.power_state.event, state.event);
  42. }
  43. if (dev->power.pm_parent
  44. && dev->power.pm_parent->power.power_state.event) {
  45. dev_err(dev,
  46. "PM: suspend %d->%d, parent %s already %d\n",
  47. dev->power.power_state.event, state.event,
  48. dev->power.pm_parent->bus_id,
  49. dev->power.pm_parent->power.power_state.event);
  50. }
  51. dev->power.prev_state = dev->power.power_state;
  52. if (dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
  53. dev_dbg(dev, "suspending\n");
  54. error = dev->bus->suspend(dev, state);
  55. suspend_report_result(dev->bus->suspend, error);
  56. }
  57. up(&dev->sem);
  58. return error;
  59. }
  60. /**
  61. * device_suspend - Save state and stop all devices in system.
  62. * @state: Power state to put each device in.
  63. *
  64. * Walk the dpm_active list, call ->suspend() for each device, and move
  65. * it to dpm_off.
  66. * Check the return value for each. If it returns 0, then we move the
  67. * the device to the dpm_off list. If it returns -EAGAIN, we move it to
  68. * the dpm_off_irq list. If we get a different error, try and back out.
  69. *
  70. * If we hit a failure with any of the devices, call device_resume()
  71. * above to bring the suspended devices back to life.
  72. *
  73. */
  74. int device_suspend(pm_message_t state)
  75. {
  76. int error = 0;
  77. if (!is_console_suspend_safe())
  78. return -EINVAL;
  79. down(&dpm_sem);
  80. down(&dpm_list_sem);
  81. while (!list_empty(&dpm_active) && error == 0) {
  82. struct list_head * entry = dpm_active.prev;
  83. struct device * dev = to_device(entry);
  84. get_device(dev);
  85. up(&dpm_list_sem);
  86. error = suspend_device(dev, state);
  87. down(&dpm_list_sem);
  88. /* Check if the device got removed */
  89. if (!list_empty(&dev->power.entry)) {
  90. /* Move it to the dpm_off or dpm_off_irq list */
  91. if (!error) {
  92. list_del(&dev->power.entry);
  93. list_add(&dev->power.entry, &dpm_off);
  94. } else if (error == -EAGAIN) {
  95. list_del(&dev->power.entry);
  96. list_add(&dev->power.entry, &dpm_off_irq);
  97. error = 0;
  98. }
  99. }
  100. if (error)
  101. printk(KERN_ERR "Could not suspend device %s: "
  102. "error %d\n", kobject_name(&dev->kobj), error);
  103. put_device(dev);
  104. }
  105. up(&dpm_list_sem);
  106. if (error) {
  107. /* we failed... before resuming, bring back devices from
  108. * dpm_off_irq list back to main dpm_off list, we do want
  109. * to call resume() on them, in case they partially suspended
  110. * despite returning -EAGAIN
  111. */
  112. while (!list_empty(&dpm_off_irq)) {
  113. struct list_head * entry = dpm_off_irq.next;
  114. list_del(entry);
  115. list_add(entry, &dpm_off);
  116. }
  117. dpm_resume();
  118. }
  119. up(&dpm_sem);
  120. return error;
  121. }
  122. EXPORT_SYMBOL_GPL(device_suspend);
  123. /**
  124. * device_power_down - Shut down special devices.
  125. * @state: Power state to enter.
  126. *
  127. * Walk the dpm_off_irq list, calling ->power_down() for each device that
  128. * couldn't power down the device with interrupts enabled. When we're
  129. * done, power down system devices.
  130. */
  131. int device_power_down(pm_message_t state)
  132. {
  133. int error = 0;
  134. struct device * dev;
  135. list_for_each_entry_reverse(dev, &dpm_off_irq, power.entry) {
  136. if ((error = suspend_device(dev, state)))
  137. break;
  138. }
  139. if (error)
  140. goto Error;
  141. if ((error = sysdev_suspend(state)))
  142. goto Error;
  143. Done:
  144. return error;
  145. Error:
  146. printk(KERN_ERR "Could not power down device %s: "
  147. "error %d\n", kobject_name(&dev->kobj), error);
  148. dpm_power_up();
  149. goto Done;
  150. }
  151. EXPORT_SYMBOL_GPL(device_power_down);
  152. void __suspend_report_result(const char *function, void *fn, int ret)
  153. {
  154. if (ret) {
  155. printk(KERN_ERR "%s(): ", function);
  156. print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
  157. printk("%d\n", ret);
  158. }
  159. }
  160. EXPORT_SYMBOL_GPL(__suspend_report_result);