main.c 9.6 KB

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