main.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * drivers/base/power/main.c - Where the driver meets power management.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. *
  10. * The driver model core calls device_pm_add() when a device is registered.
  11. * This will intialize the embedded device_pm_info object in the device
  12. * and add it to the list of power-controlled devices. sysfs entries for
  13. * controlling device power management will also be added.
  14. *
  15. * A different set of lists than the global subsystem list are used to
  16. * keep track of power info because we use different lists to hold
  17. * devices based on what stage of the power management process they
  18. * are in. The power domain dependencies may also differ from the
  19. * ancestral dependencies that the subsystem list maintains.
  20. */
  21. #include <linux/device.h>
  22. #include <linux/kallsyms.h>
  23. #include <linux/mutex.h>
  24. #include <linux/pm.h>
  25. #include <linux/resume-trace.h>
  26. #include "../base.h"
  27. #include "power.h"
  28. LIST_HEAD(dpm_active);
  29. static LIST_HEAD(dpm_off);
  30. static LIST_HEAD(dpm_off_irq);
  31. static DEFINE_MUTEX(dpm_mtx);
  32. static DEFINE_MUTEX(dpm_list_mtx);
  33. int (*platform_enable_wakeup)(struct device *dev, int is_on);
  34. void device_pm_add(struct device *dev)
  35. {
  36. pr_debug("PM: Adding info for %s:%s\n",
  37. dev->bus ? dev->bus->name : "No Bus",
  38. kobject_name(&dev->kobj));
  39. mutex_lock(&dpm_list_mtx);
  40. list_add_tail(&dev->power.entry, &dpm_active);
  41. mutex_unlock(&dpm_list_mtx);
  42. }
  43. void device_pm_remove(struct device *dev)
  44. {
  45. pr_debug("PM: Removing info for %s:%s\n",
  46. dev->bus ? dev->bus->name : "No Bus",
  47. kobject_name(&dev->kobj));
  48. mutex_lock(&dpm_list_mtx);
  49. dpm_sysfs_remove(dev);
  50. list_del_init(&dev->power.entry);
  51. mutex_unlock(&dpm_list_mtx);
  52. }
  53. /*------------------------- Resume routines -------------------------*/
  54. /**
  55. * resume_device - Restore state for one device.
  56. * @dev: Device.
  57. *
  58. */
  59. static int resume_device(struct device * dev)
  60. {
  61. int error = 0;
  62. TRACE_DEVICE(dev);
  63. TRACE_RESUME(0);
  64. down(&dev->sem);
  65. if (dev->bus && dev->bus->resume) {
  66. dev_dbg(dev,"resuming\n");
  67. error = dev->bus->resume(dev);
  68. }
  69. if (!error && dev->type && dev->type->resume) {
  70. dev_dbg(dev,"resuming\n");
  71. error = dev->type->resume(dev);
  72. }
  73. if (!error && dev->class && dev->class->resume) {
  74. dev_dbg(dev,"class resume\n");
  75. error = dev->class->resume(dev);
  76. }
  77. up(&dev->sem);
  78. TRACE_RESUME(error);
  79. return error;
  80. }
  81. static int resume_device_early(struct device * dev)
  82. {
  83. int error = 0;
  84. TRACE_DEVICE(dev);
  85. TRACE_RESUME(0);
  86. if (dev->bus && dev->bus->resume_early) {
  87. dev_dbg(dev,"EARLY resume\n");
  88. error = dev->bus->resume_early(dev);
  89. }
  90. TRACE_RESUME(error);
  91. return error;
  92. }
  93. /*
  94. * Resume the devices that have either not gone through
  95. * the late suspend, or that did go through it but also
  96. * went through the early resume
  97. */
  98. static void dpm_resume(void)
  99. {
  100. mutex_lock(&dpm_list_mtx);
  101. while(!list_empty(&dpm_off)) {
  102. struct list_head * entry = dpm_off.next;
  103. struct device * dev = to_device(entry);
  104. get_device(dev);
  105. list_move_tail(entry, &dpm_active);
  106. mutex_unlock(&dpm_list_mtx);
  107. resume_device(dev);
  108. mutex_lock(&dpm_list_mtx);
  109. put_device(dev);
  110. }
  111. mutex_unlock(&dpm_list_mtx);
  112. }
  113. /**
  114. * device_resume - Restore state of each device in system.
  115. *
  116. * Walk the dpm_off list, remove each entry, resume the device,
  117. * then add it to the dpm_active list.
  118. */
  119. void device_resume(void)
  120. {
  121. might_sleep();
  122. mutex_lock(&dpm_mtx);
  123. dpm_resume();
  124. mutex_unlock(&dpm_mtx);
  125. }
  126. EXPORT_SYMBOL_GPL(device_resume);
  127. /**
  128. * dpm_power_up - Power on some devices.
  129. *
  130. * Walk the dpm_off_irq list and power each device up. This
  131. * is used for devices that required they be powered down with
  132. * interrupts disabled. As devices are powered on, they are moved
  133. * to the dpm_active list.
  134. *
  135. * Interrupts must be disabled when calling this.
  136. */
  137. static void dpm_power_up(void)
  138. {
  139. while(!list_empty(&dpm_off_irq)) {
  140. struct list_head * entry = dpm_off_irq.next;
  141. struct device * dev = to_device(entry);
  142. list_move_tail(entry, &dpm_off);
  143. resume_device_early(dev);
  144. }
  145. }
  146. /**
  147. * device_power_up - Turn on all devices that need special attention.
  148. *
  149. * Power on system devices then devices that required we shut them down
  150. * with interrupts disabled.
  151. * Called with interrupts disabled.
  152. */
  153. void device_power_up(void)
  154. {
  155. sysdev_resume();
  156. dpm_power_up();
  157. }
  158. EXPORT_SYMBOL_GPL(device_power_up);
  159. /*------------------------- Suspend routines -------------------------*/
  160. /*
  161. * The entries in the dpm_active list are in a depth first order, simply
  162. * because children are guaranteed to be discovered after parents, and
  163. * are inserted at the back of the list on discovery.
  164. *
  165. * All list on the suspend path are done in reverse order, so we operate
  166. * on the leaves of the device tree (or forests, depending on how you want
  167. * to look at it ;) first. As nodes are removed from the back of the list,
  168. * they are inserted into the front of their destintation lists.
  169. *
  170. * Things are the reverse on the resume path - iterations are done in
  171. * forward order, and nodes are inserted at the back of their destination
  172. * lists. This way, the ancestors will be accessed before their descendents.
  173. */
  174. static inline char *suspend_verb(u32 event)
  175. {
  176. switch (event) {
  177. case PM_EVENT_SUSPEND: return "suspend";
  178. case PM_EVENT_FREEZE: return "freeze";
  179. case PM_EVENT_PRETHAW: return "prethaw";
  180. default: return "(unknown suspend event)";
  181. }
  182. }
  183. static void
  184. suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
  185. {
  186. dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
  187. ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
  188. ", may wakeup" : "");
  189. }
  190. /**
  191. * suspend_device - Save state of one device.
  192. * @dev: Device.
  193. * @state: Power state device is entering.
  194. */
  195. static int suspend_device(struct device * dev, pm_message_t state)
  196. {
  197. int error = 0;
  198. down(&dev->sem);
  199. if (dev->power.power_state.event) {
  200. dev_dbg(dev, "PM: suspend %d-->%d\n",
  201. dev->power.power_state.event, state.event);
  202. }
  203. if (dev->class && dev->class->suspend) {
  204. suspend_device_dbg(dev, state, "class ");
  205. error = dev->class->suspend(dev, state);
  206. suspend_report_result(dev->class->suspend, error);
  207. }
  208. if (!error && dev->type && dev->type->suspend) {
  209. suspend_device_dbg(dev, state, "type ");
  210. error = dev->type->suspend(dev, state);
  211. suspend_report_result(dev->type->suspend, error);
  212. }
  213. if (!error && dev->bus && dev->bus->suspend) {
  214. suspend_device_dbg(dev, state, "");
  215. error = dev->bus->suspend(dev, state);
  216. suspend_report_result(dev->bus->suspend, error);
  217. }
  218. up(&dev->sem);
  219. return error;
  220. }
  221. /*
  222. * This is called with interrupts off, only a single CPU
  223. * running. We can't acquire a mutex or semaphore (and we don't
  224. * need the protection)
  225. */
  226. static int suspend_device_late(struct device *dev, pm_message_t state)
  227. {
  228. int error = 0;
  229. if (dev->bus && dev->bus->suspend_late) {
  230. suspend_device_dbg(dev, state, "LATE ");
  231. error = dev->bus->suspend_late(dev, state);
  232. suspend_report_result(dev->bus->suspend_late, error);
  233. }
  234. return error;
  235. }
  236. /**
  237. * device_suspend - Save state and stop all devices in system.
  238. * @state: Power state to put each device in.
  239. *
  240. * Walk the dpm_active list, call ->suspend() for each device, and move
  241. * it to the dpm_off list.
  242. *
  243. * (For historical reasons, if it returns -EAGAIN, that used to mean
  244. * that the device would be called again with interrupts disabled.
  245. * These days, we use the "suspend_late()" callback for that, so we
  246. * print a warning and consider it an error).
  247. *
  248. * If we get a different error, try and back out.
  249. *
  250. * If we hit a failure with any of the devices, call device_resume()
  251. * above to bring the suspended devices back to life.
  252. *
  253. */
  254. int device_suspend(pm_message_t state)
  255. {
  256. int error = 0;
  257. might_sleep();
  258. mutex_lock(&dpm_mtx);
  259. mutex_lock(&dpm_list_mtx);
  260. while (!list_empty(&dpm_active) && error == 0) {
  261. struct list_head * entry = dpm_active.prev;
  262. struct device * dev = to_device(entry);
  263. get_device(dev);
  264. mutex_unlock(&dpm_list_mtx);
  265. error = suspend_device(dev, state);
  266. mutex_lock(&dpm_list_mtx);
  267. /* Check if the device got removed */
  268. if (!list_empty(&dev->power.entry)) {
  269. /* Move it to the dpm_off list */
  270. if (!error)
  271. list_move(&dev->power.entry, &dpm_off);
  272. }
  273. if (error)
  274. printk(KERN_ERR "Could not suspend device %s: "
  275. "error %d%s\n",
  276. kobject_name(&dev->kobj), error,
  277. error == -EAGAIN ? " (please convert to suspend_late)" : "");
  278. put_device(dev);
  279. }
  280. mutex_unlock(&dpm_list_mtx);
  281. if (error)
  282. dpm_resume();
  283. mutex_unlock(&dpm_mtx);
  284. return error;
  285. }
  286. EXPORT_SYMBOL_GPL(device_suspend);
  287. /**
  288. * device_power_down - Shut down special devices.
  289. * @state: Power state to enter.
  290. *
  291. * Walk the dpm_off_irq list, calling ->power_down() for each device that
  292. * couldn't power down the device with interrupts enabled. When we're
  293. * done, power down system devices.
  294. */
  295. int device_power_down(pm_message_t state)
  296. {
  297. int error = 0;
  298. struct device * dev;
  299. while (!list_empty(&dpm_off)) {
  300. struct list_head * entry = dpm_off.prev;
  301. dev = to_device(entry);
  302. error = suspend_device_late(dev, state);
  303. if (error)
  304. goto Error;
  305. list_move(&dev->power.entry, &dpm_off_irq);
  306. }
  307. error = sysdev_suspend(state);
  308. Done:
  309. return error;
  310. Error:
  311. printk(KERN_ERR "Could not power down device %s: "
  312. "error %d\n", kobject_name(&dev->kobj), error);
  313. dpm_power_up();
  314. goto Done;
  315. }
  316. EXPORT_SYMBOL_GPL(device_power_down);
  317. void __suspend_report_result(const char *function, void *fn, int ret)
  318. {
  319. if (ret) {
  320. printk(KERN_ERR "%s(): ", function);
  321. print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
  322. printk("%d\n", ret);
  323. }
  324. }
  325. EXPORT_SYMBOL_GPL(__suspend_report_result);