main.c 5.3 KB

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