suspend.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. dev_dbg(dev, "suspending\n");
  36. dev->power.prev_state = dev->power.power_state;
  37. if (dev->bus && dev->bus->suspend && !dev->power.power_state)
  38. error = dev->bus->suspend(dev, state);
  39. return error;
  40. }
  41. /**
  42. * device_suspend - Save state and stop all devices in system.
  43. * @state: Power state to put each device in.
  44. *
  45. * Walk the dpm_active list, call ->suspend() for each device, and move
  46. * it to dpm_off.
  47. * Check the return value for each. If it returns 0, then we move the
  48. * the device to the dpm_off list. If it returns -EAGAIN, we move it to
  49. * the dpm_off_irq list. If we get a different error, try and back out.
  50. *
  51. * If we hit a failure with any of the devices, call device_resume()
  52. * above to bring the suspended devices back to life.
  53. *
  54. */
  55. int device_suspend(pm_message_t state)
  56. {
  57. int error = 0;
  58. down(&dpm_sem);
  59. down(&dpm_list_sem);
  60. while (!list_empty(&dpm_active) && error == 0) {
  61. struct list_head * entry = dpm_active.prev;
  62. struct device * dev = to_device(entry);
  63. get_device(dev);
  64. up(&dpm_list_sem);
  65. error = suspend_device(dev, state);
  66. down(&dpm_list_sem);
  67. /* Check if the device got removed */
  68. if (!list_empty(&dev->power.entry)) {
  69. /* Move it to the dpm_off or dpm_off_irq list */
  70. if (!error) {
  71. list_del(&dev->power.entry);
  72. list_add(&dev->power.entry, &dpm_off);
  73. } else if (error == -EAGAIN) {
  74. list_del(&dev->power.entry);
  75. list_add(&dev->power.entry, &dpm_off_irq);
  76. error = 0;
  77. }
  78. }
  79. if (error)
  80. printk(KERN_ERR "Could not suspend device %s: "
  81. "error %d\n", kobject_name(&dev->kobj), error);
  82. put_device(dev);
  83. }
  84. up(&dpm_list_sem);
  85. if (error)
  86. dpm_resume();
  87. up(&dpm_sem);
  88. return error;
  89. }
  90. EXPORT_SYMBOL_GPL(device_suspend);
  91. /**
  92. * device_power_down - Shut down special devices.
  93. * @state: Power state to enter.
  94. *
  95. * Walk the dpm_off_irq list, calling ->power_down() for each device that
  96. * couldn't power down the device with interrupts enabled. When we're
  97. * done, power down system devices.
  98. */
  99. int device_power_down(pm_message_t state)
  100. {
  101. int error = 0;
  102. struct device * dev;
  103. list_for_each_entry_reverse(dev, &dpm_off_irq, power.entry) {
  104. if ((error = suspend_device(dev, state)))
  105. break;
  106. }
  107. if (error)
  108. goto Error;
  109. if ((error = sysdev_suspend(state)))
  110. goto Error;
  111. Done:
  112. return error;
  113. Error:
  114. printk(KERN_ERR "Could not power down device %s: "
  115. "error %d\n", kobject_name(&dev->kobj), error);
  116. dpm_power_up();
  117. goto Done;
  118. }
  119. EXPORT_SYMBOL_GPL(device_power_down);