suspend.c 4.0 KB

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