main.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/kobject.h>
  11. #include <linux/string.h>
  12. #include <linux/resume-trace.h>
  13. #include <linux/workqueue.h>
  14. #include "power.h"
  15. DEFINE_MUTEX(pm_mutex);
  16. #ifdef CONFIG_PM_SLEEP
  17. /* Routines for PM-transition notifications */
  18. static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
  19. int register_pm_notifier(struct notifier_block *nb)
  20. {
  21. return blocking_notifier_chain_register(&pm_chain_head, nb);
  22. }
  23. EXPORT_SYMBOL_GPL(register_pm_notifier);
  24. int unregister_pm_notifier(struct notifier_block *nb)
  25. {
  26. return blocking_notifier_chain_unregister(&pm_chain_head, nb);
  27. }
  28. EXPORT_SYMBOL_GPL(unregister_pm_notifier);
  29. int pm_notifier_call_chain(unsigned long val)
  30. {
  31. return (blocking_notifier_call_chain(&pm_chain_head, val, NULL)
  32. == NOTIFY_BAD) ? -EINVAL : 0;
  33. }
  34. /* If set, devices may be suspended and resumed asynchronously. */
  35. int pm_async_enabled = 1;
  36. static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
  37. char *buf)
  38. {
  39. return sprintf(buf, "%d\n", pm_async_enabled);
  40. }
  41. static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
  42. const char *buf, size_t n)
  43. {
  44. unsigned long val;
  45. if (strict_strtoul(buf, 10, &val))
  46. return -EINVAL;
  47. if (val > 1)
  48. return -EINVAL;
  49. pm_async_enabled = val;
  50. return n;
  51. }
  52. power_attr(pm_async);
  53. #ifdef CONFIG_PM_DEBUG
  54. int pm_test_level = TEST_NONE;
  55. static const char * const pm_tests[__TEST_AFTER_LAST] = {
  56. [TEST_NONE] = "none",
  57. [TEST_CORE] = "core",
  58. [TEST_CPUS] = "processors",
  59. [TEST_PLATFORM] = "platform",
  60. [TEST_DEVICES] = "devices",
  61. [TEST_FREEZER] = "freezer",
  62. };
  63. static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
  64. char *buf)
  65. {
  66. char *s = buf;
  67. int level;
  68. for (level = TEST_FIRST; level <= TEST_MAX; level++)
  69. if (pm_tests[level]) {
  70. if (level == pm_test_level)
  71. s += sprintf(s, "[%s] ", pm_tests[level]);
  72. else
  73. s += sprintf(s, "%s ", pm_tests[level]);
  74. }
  75. if (s != buf)
  76. /* convert the last space to a newline */
  77. *(s-1) = '\n';
  78. return (s - buf);
  79. }
  80. static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
  81. const char *buf, size_t n)
  82. {
  83. const char * const *s;
  84. int level;
  85. char *p;
  86. int len;
  87. int error = -EINVAL;
  88. p = memchr(buf, '\n', n);
  89. len = p ? p - buf : n;
  90. mutex_lock(&pm_mutex);
  91. level = TEST_FIRST;
  92. for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
  93. if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
  94. pm_test_level = level;
  95. error = 0;
  96. break;
  97. }
  98. mutex_unlock(&pm_mutex);
  99. return error ? error : n;
  100. }
  101. power_attr(pm_test);
  102. #endif /* CONFIG_PM_DEBUG */
  103. #endif /* CONFIG_PM_SLEEP */
  104. struct kobject *power_kobj;
  105. /**
  106. * state - control system power state.
  107. *
  108. * show() returns what states are supported, which is hard-coded to
  109. * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
  110. * 'disk' (Suspend-to-Disk).
  111. *
  112. * store() accepts one of those strings, translates it into the
  113. * proper enumerated value, and initiates a suspend transition.
  114. */
  115. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
  116. char *buf)
  117. {
  118. char *s = buf;
  119. #ifdef CONFIG_SUSPEND
  120. int i;
  121. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  122. if (pm_states[i] && valid_state(i))
  123. s += sprintf(s,"%s ", pm_states[i]);
  124. }
  125. #endif
  126. #ifdef CONFIG_HIBERNATION
  127. s += sprintf(s, "%s\n", "disk");
  128. #else
  129. if (s != buf)
  130. /* convert the last space to a newline */
  131. *(s-1) = '\n';
  132. #endif
  133. return (s - buf);
  134. }
  135. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
  136. const char *buf, size_t n)
  137. {
  138. #ifdef CONFIG_SUSPEND
  139. #ifdef CONFIG_EARLYSUSPEND
  140. suspend_state_t state = PM_SUSPEND_ON;
  141. #else
  142. suspend_state_t state = PM_SUSPEND_STANDBY;
  143. #endif
  144. const char * const *s;
  145. #endif
  146. char *p;
  147. int len;
  148. int error = -EINVAL;
  149. p = memchr(buf, '\n', n);
  150. len = p ? p - buf : n;
  151. /* First, check if we are requested to hibernate */
  152. if (len == 4 && !strncmp(buf, "disk", len)) {
  153. error = hibernate();
  154. goto Exit;
  155. }
  156. #ifdef CONFIG_SUSPEND
  157. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
  158. if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
  159. break;
  160. }
  161. if (state < PM_SUSPEND_MAX && *s)
  162. #ifdef CONFIG_EARLYSUSPEND
  163. if (state == PM_SUSPEND_ON || valid_state(state)) {
  164. error = 0;
  165. request_suspend_state(state);
  166. }
  167. #else
  168. error = enter_state(state);
  169. #endif
  170. #endif
  171. Exit:
  172. return error ? error : n;
  173. }
  174. power_attr(state);
  175. #ifdef CONFIG_PM_SLEEP
  176. /*
  177. * The 'wakeup_count' attribute, along with the functions defined in
  178. * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
  179. * handled in a non-racy way.
  180. *
  181. * If a wakeup event occurs when the system is in a sleep state, it simply is
  182. * woken up. In turn, if an event that would wake the system up from a sleep
  183. * state occurs when it is undergoing a transition to that sleep state, the
  184. * transition should be aborted. Moreover, if such an event occurs when the
  185. * system is in the working state, an attempt to start a transition to the
  186. * given sleep state should fail during certain period after the detection of
  187. * the event. Using the 'state' attribute alone is not sufficient to satisfy
  188. * these requirements, because a wakeup event may occur exactly when 'state'
  189. * is being written to and may be delivered to user space right before it is
  190. * frozen, so the event will remain only partially processed until the system is
  191. * woken up by another event. In particular, it won't cause the transition to
  192. * a sleep state to be aborted.
  193. *
  194. * This difficulty may be overcome if user space uses 'wakeup_count' before
  195. * writing to 'state'. It first should read from 'wakeup_count' and store
  196. * the read value. Then, after carrying out its own preparations for the system
  197. * transition to a sleep state, it should write the stored value to
  198. * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
  199. * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
  200. * is allowed to write to 'state', but the transition will be aborted if there
  201. * are any wakeup events detected after 'wakeup_count' was written to.
  202. */
  203. static ssize_t wakeup_count_show(struct kobject *kobj,
  204. struct kobj_attribute *attr,
  205. char *buf)
  206. {
  207. unsigned int val;
  208. return pm_get_wakeup_count(&val) ? sprintf(buf, "%u\n", val) : -EINTR;
  209. }
  210. static ssize_t wakeup_count_store(struct kobject *kobj,
  211. struct kobj_attribute *attr,
  212. const char *buf, size_t n)
  213. {
  214. unsigned int val;
  215. if (sscanf(buf, "%u", &val) == 1) {
  216. if (pm_save_wakeup_count(val))
  217. return n;
  218. }
  219. return -EINVAL;
  220. }
  221. power_attr(wakeup_count);
  222. #endif /* CONFIG_PM_SLEEP */
  223. #ifdef CONFIG_PM_TRACE
  224. int pm_trace_enabled;
  225. static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
  226. char *buf)
  227. {
  228. return sprintf(buf, "%d\n", pm_trace_enabled);
  229. }
  230. static ssize_t
  231. pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
  232. const char *buf, size_t n)
  233. {
  234. int val;
  235. if (sscanf(buf, "%d", &val) == 1) {
  236. pm_trace_enabled = !!val;
  237. return n;
  238. }
  239. return -EINVAL;
  240. }
  241. power_attr(pm_trace);
  242. static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
  243. struct kobj_attribute *attr,
  244. char *buf)
  245. {
  246. return show_trace_dev_match(buf, PAGE_SIZE);
  247. }
  248. static ssize_t
  249. pm_trace_dev_match_store(struct kobject *kobj, struct kobj_attribute *attr,
  250. const char *buf, size_t n)
  251. {
  252. return -EINVAL;
  253. }
  254. power_attr(pm_trace_dev_match);
  255. #endif /* CONFIG_PM_TRACE */
  256. #ifdef CONFIG_USER_WAKELOCK
  257. power_attr(wake_lock);
  258. power_attr(wake_unlock);
  259. #endif
  260. static struct attribute * g[] = {
  261. &state_attr.attr,
  262. #ifdef CONFIG_PM_TRACE
  263. &pm_trace_attr.attr,
  264. &pm_trace_dev_match_attr.attr,
  265. #endif
  266. #ifdef CONFIG_PM_SLEEP
  267. &pm_async_attr.attr,
  268. &wakeup_count_attr.attr,
  269. #ifdef CONFIG_PM_DEBUG
  270. &pm_test_attr.attr,
  271. #endif
  272. #ifdef CONFIG_USER_WAKELOCK
  273. &wake_lock_attr.attr,
  274. &wake_unlock_attr.attr,
  275. #endif
  276. #endif
  277. NULL,
  278. };
  279. static struct attribute_group attr_group = {
  280. .attrs = g,
  281. };
  282. #ifdef CONFIG_PM_RUNTIME
  283. struct workqueue_struct *pm_wq;
  284. EXPORT_SYMBOL_GPL(pm_wq);
  285. static int __init pm_start_workqueue(void)
  286. {
  287. pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
  288. return pm_wq ? 0 : -ENOMEM;
  289. }
  290. #else
  291. static inline int pm_start_workqueue(void) { return 0; }
  292. #endif
  293. static int __init pm_init(void)
  294. {
  295. int error = pm_start_workqueue();
  296. if (error)
  297. return error;
  298. hibernate_image_size_init();
  299. hibernate_reserved_size_init();
  300. power_kobj = kobject_create_and_add("power", NULL);
  301. if (!power_kobj)
  302. return -ENOMEM;
  303. return sysfs_create_group(power_kobj, &attr_group);
  304. }
  305. core_initcall(pm_init);