main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * kernel/power/main.c - PM subsystem core functionality.
  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. #include <linux/suspend.h>
  11. #include <linux/kobject.h>
  12. #include <linux/string.h>
  13. #include <linux/delay.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/pm.h>
  17. #include "power.h"
  18. DECLARE_MUTEX(pm_sem);
  19. struct pm_ops * pm_ops = NULL;
  20. suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
  21. /**
  22. * pm_set_ops - Set the global power method table.
  23. * @ops: Pointer to ops structure.
  24. */
  25. void pm_set_ops(struct pm_ops * ops)
  26. {
  27. down(&pm_sem);
  28. pm_ops = ops;
  29. up(&pm_sem);
  30. }
  31. /**
  32. * suspend_prepare - Do prep work before entering low-power state.
  33. * @state: State we're entering.
  34. *
  35. * This is common code that is called for each state that we're
  36. * entering. Allocate a console, stop all processes, then make sure
  37. * the platform can enter the requested state.
  38. */
  39. static int suspend_prepare(suspend_state_t state)
  40. {
  41. int error = 0;
  42. if (!pm_ops || !pm_ops->enter)
  43. return -EPERM;
  44. pm_prepare_console();
  45. disable_nonboot_cpus();
  46. if (num_online_cpus() != 1) {
  47. error = -EPERM;
  48. goto Enable_cpu;
  49. }
  50. if (freeze_processes()) {
  51. error = -EAGAIN;
  52. goto Thaw;
  53. }
  54. if (pm_ops->prepare) {
  55. if ((error = pm_ops->prepare(state)))
  56. goto Thaw;
  57. }
  58. if ((error = device_suspend(PMSG_SUSPEND))) {
  59. printk(KERN_ERR "Some devices failed to suspend\n");
  60. goto Finish;
  61. }
  62. return 0;
  63. Finish:
  64. if (pm_ops->finish)
  65. pm_ops->finish(state);
  66. Thaw:
  67. thaw_processes();
  68. Enable_cpu:
  69. enable_nonboot_cpus();
  70. pm_restore_console();
  71. return error;
  72. }
  73. static int suspend_enter(suspend_state_t state)
  74. {
  75. int error = 0;
  76. unsigned long flags;
  77. local_irq_save(flags);
  78. if ((error = device_power_down(PMSG_SUSPEND))) {
  79. printk(KERN_ERR "Some devices failed to power down\n");
  80. goto Done;
  81. }
  82. error = pm_ops->enter(state);
  83. device_power_up();
  84. Done:
  85. local_irq_restore(flags);
  86. return error;
  87. }
  88. /**
  89. * suspend_finish - Do final work before exiting suspend sequence.
  90. * @state: State we're coming out of.
  91. *
  92. * Call platform code to clean up, restart processes, and free the
  93. * console that we've allocated. This is not called for suspend-to-disk.
  94. */
  95. static void suspend_finish(suspend_state_t state)
  96. {
  97. device_resume();
  98. if (pm_ops && pm_ops->finish)
  99. pm_ops->finish(state);
  100. thaw_processes();
  101. enable_nonboot_cpus();
  102. pm_restore_console();
  103. }
  104. static char * pm_states[] = {
  105. [PM_SUSPEND_STANDBY] = "standby",
  106. [PM_SUSPEND_MEM] = "mem",
  107. [PM_SUSPEND_DISK] = "disk",
  108. NULL,
  109. };
  110. /**
  111. * enter_state - Do common work of entering low-power state.
  112. * @state: pm_state structure for state we're entering.
  113. *
  114. * Make sure we're the only ones trying to enter a sleep state. Fail
  115. * if someone has beat us to it, since we don't want anything weird to
  116. * happen when we wake up.
  117. * Then, do the setup for suspend, enter the state, and cleaup (after
  118. * we've woken up).
  119. */
  120. static int enter_state(suspend_state_t state)
  121. {
  122. int error;
  123. if (down_trylock(&pm_sem))
  124. return -EBUSY;
  125. if (state == PM_SUSPEND_DISK) {
  126. error = pm_suspend_disk();
  127. goto Unlock;
  128. }
  129. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  130. if ((error = suspend_prepare(state)))
  131. goto Unlock;
  132. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  133. error = suspend_enter(state);
  134. pr_debug("PM: Finishing wakeup.\n");
  135. suspend_finish(state);
  136. Unlock:
  137. up(&pm_sem);
  138. return error;
  139. }
  140. /*
  141. * This is main interface to the outside world. It needs to be
  142. * called from process context.
  143. */
  144. int software_suspend(void)
  145. {
  146. return enter_state(PM_SUSPEND_DISK);
  147. }
  148. /**
  149. * pm_suspend - Externally visible function for suspending system.
  150. * @state: Enumarted value of state to enter.
  151. *
  152. * Determine whether or not value is within range, get state
  153. * structure, and enter (above).
  154. */
  155. int pm_suspend(suspend_state_t state)
  156. {
  157. if (state > PM_SUSPEND_ON && state < PM_SUSPEND_MAX)
  158. return enter_state(state);
  159. return -EINVAL;
  160. }
  161. decl_subsys(power,NULL,NULL);
  162. /**
  163. * state - control system power state.
  164. *
  165. * show() returns what states are supported, which is hard-coded to
  166. * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
  167. * 'disk' (Suspend-to-Disk).
  168. *
  169. * store() accepts one of those strings, translates it into the
  170. * proper enumerated value, and initiates a suspend transition.
  171. */
  172. static ssize_t state_show(struct subsystem * subsys, char * buf)
  173. {
  174. int i;
  175. char * s = buf;
  176. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  177. if (pm_states[i])
  178. s += sprintf(s,"%s ",pm_states[i]);
  179. }
  180. s += sprintf(s,"\n");
  181. return (s - buf);
  182. }
  183. static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
  184. {
  185. suspend_state_t state = PM_SUSPEND_STANDBY;
  186. char ** s;
  187. char *p;
  188. int error;
  189. int len;
  190. p = memchr(buf, '\n', n);
  191. len = p ? p - buf : n;
  192. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
  193. if (*s && !strncmp(buf, *s, len))
  194. break;
  195. }
  196. if (*s)
  197. error = enter_state(state);
  198. else
  199. error = -EINVAL;
  200. return error ? error : n;
  201. }
  202. power_attr(state);
  203. static struct attribute * g[] = {
  204. &state_attr.attr,
  205. NULL,
  206. };
  207. static struct attribute_group attr_group = {
  208. .attrs = g,
  209. };
  210. static int __init pm_init(void)
  211. {
  212. int error = subsystem_register(&power_subsys);
  213. if (!error)
  214. error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
  215. return error;
  216. }
  217. core_initcall(pm_init);