blob: 9b279cc981946dfcca85667e54af39ee96c3e47c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kobject.h>
12#include <linux/string.h>
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -070013#include <linux/resume-trace.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020014#include <linux/workqueue.h>
Amar Singhal97d68a82012-03-20 19:03:04 -070015#include <linux/hrtimer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include "power.h"
18
Amar Singhalac2a6d62012-02-27 17:40:31 -080019#define MAX_BUF 100
20
Stephen Hemmingera6d70982006-12-06 20:34:35 -080021DEFINE_MUTEX(pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Rafael J. Wysockicd51e612011-02-11 00:04:52 +010023#ifdef CONFIG_PM_SLEEP
24
Alan Stern82525752007-11-19 23:49:18 +010025/* Routines for PM-transition notifications */
26
27static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28
Amar Singhal97d68a82012-03-20 19:03:04 -070029static void touch_event_fn(struct work_struct *work);
30static DECLARE_WORK(touch_event_struct, touch_event_fn);
31
32static struct hrtimer tc_ev_timer;
33static int tc_ev_processed;
Amar Singhalac2a6d62012-02-27 17:40:31 -080034static ktime_t touch_evt_timer_val;
35
Alan Stern82525752007-11-19 23:49:18 +010036int register_pm_notifier(struct notifier_block *nb)
37{
38 return blocking_notifier_chain_register(&pm_chain_head, nb);
39}
40EXPORT_SYMBOL_GPL(register_pm_notifier);
41
42int unregister_pm_notifier(struct notifier_block *nb)
43{
44 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
45}
46EXPORT_SYMBOL_GPL(unregister_pm_notifier);
47
48int pm_notifier_call_chain(unsigned long val)
49{
50 return (blocking_notifier_call_chain(&pm_chain_head, val, NULL)
51 == NOTIFY_BAD) ? -EINVAL : 0;
52}
53
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +010054/* If set, devices may be suspended and resumed asynchronously. */
55int pm_async_enabled = 1;
56
57static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
58 char *buf)
59{
60 return sprintf(buf, "%d\n", pm_async_enabled);
61}
62
63static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
64 const char *buf, size_t n)
65{
66 unsigned long val;
67
68 if (strict_strtoul(buf, 10, &val))
69 return -EINVAL;
70
71 if (val > 1)
72 return -EINVAL;
73
74 pm_async_enabled = val;
75 return n;
76}
77
78power_attr(pm_async);
79
Amar Singhalac2a6d62012-02-27 17:40:31 -080080static ssize_t
81touch_event_show(struct kobject *kobj,
82 struct kobj_attribute *attr, char *buf)
83{
Amar Singhal97d68a82012-03-20 19:03:04 -070084 if (tc_ev_processed == 0)
Amar Singhalac2a6d62012-02-27 17:40:31 -080085 return snprintf(buf, strnlen("touch_event", MAX_BUF) + 1,
86 "touch_event");
87 else
88 return snprintf(buf, strnlen("null", MAX_BUF) + 1,
89 "null");
90}
91
92static ssize_t
93touch_event_store(struct kobject *kobj,
94 struct kobj_attribute *attr,
95 const char *buf, size_t n)
96{
97
Amar Singhal97d68a82012-03-20 19:03:04 -070098 hrtimer_cancel(&tc_ev_timer);
99 tc_ev_processed = 0;
Amar Singhalac2a6d62012-02-27 17:40:31 -0800100
101 /* set a timer to notify the userspace to stop processing
102 * touch event
103 */
Amar Singhal97d68a82012-03-20 19:03:04 -0700104 hrtimer_start(&tc_ev_timer, touch_evt_timer_val, HRTIMER_MODE_REL);
Amar Singhalac2a6d62012-02-27 17:40:31 -0800105
106 /* wakeup the userspace poll */
107 sysfs_notify(kobj, NULL, "touch_event");
108
109 return n;
110}
111
112power_attr(touch_event);
113
114static ssize_t
115touch_event_timer_show(struct kobject *kobj,
116 struct kobj_attribute *attr, char *buf)
117{
118 return snprintf(buf, MAX_BUF, "%lld", touch_evt_timer_val.tv64);
119}
120
121static ssize_t
122touch_event_timer_store(struct kobject *kobj,
123 struct kobj_attribute *attr,
124 const char *buf, size_t n)
125{
126 unsigned long val;
127
128 if (strict_strtoul(buf, 10, &val))
129 return -EINVAL;
130
131 touch_evt_timer_val = ktime_set(0, val*1000);
132
133 return n;
134}
135
136power_attr(touch_event_timer);
137
Amar Singhal97d68a82012-03-20 19:03:04 -0700138static void touch_event_fn(struct work_struct *work)
Amar Singhalac2a6d62012-02-27 17:40:31 -0800139{
140 /* wakeup the userspace poll */
Amar Singhal97d68a82012-03-20 19:03:04 -0700141 tc_ev_processed = 1;
Amar Singhalac2a6d62012-02-27 17:40:31 -0800142 sysfs_notify(power_kobj, NULL, "touch_event");
Amar Singhal97d68a82012-03-20 19:03:04 -0700143
144 return;
145}
146
147static enum hrtimer_restart tc_ev_stop(struct hrtimer *hrtimer)
148{
149
150 schedule_work(&touch_event_struct);
151
Amar Singhalac2a6d62012-02-27 17:40:31 -0800152 return HRTIMER_NORESTART;
153}
154
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100155#ifdef CONFIG_PM_DEBUG
156int pm_test_level = TEST_NONE;
157
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100158static const char * const pm_tests[__TEST_AFTER_LAST] = {
159 [TEST_NONE] = "none",
160 [TEST_CORE] = "core",
161 [TEST_CPUS] = "processors",
162 [TEST_PLATFORM] = "platform",
163 [TEST_DEVICES] = "devices",
164 [TEST_FREEZER] = "freezer",
165};
166
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +0100167static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
168 char *buf)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100169{
170 char *s = buf;
171 int level;
172
173 for (level = TEST_FIRST; level <= TEST_MAX; level++)
174 if (pm_tests[level]) {
175 if (level == pm_test_level)
176 s += sprintf(s, "[%s] ", pm_tests[level]);
177 else
178 s += sprintf(s, "%s ", pm_tests[level]);
179 }
180
181 if (s != buf)
182 /* convert the last space to a newline */
183 *(s-1) = '\n';
184
185 return (s - buf);
186}
187
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +0100188static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
189 const char *buf, size_t n)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100190{
191 const char * const *s;
192 int level;
193 char *p;
194 int len;
195 int error = -EINVAL;
196
197 p = memchr(buf, '\n', n);
198 len = p ? p - buf : n;
199
200 mutex_lock(&pm_mutex);
201
202 level = TEST_FIRST;
203 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
204 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
205 pm_test_level = level;
206 error = 0;
207 break;
208 }
209
210 mutex_unlock(&pm_mutex);
211
212 return error ? error : n;
213}
214
215power_attr(pm_test);
Rafael J. Wysocki091d71e2009-01-17 00:10:45 +0100216#endif /* CONFIG_PM_DEBUG */
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100217
Rafael J. Wysocki7671b8a2007-12-14 01:07:13 +0100218#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +0100219
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800220struct kobject *power_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/**
223 * state - control system power state.
224 *
225 * show() returns what states are supported, which is hard-coded to
226 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
227 * 'disk' (Suspend-to-Disk).
228 *
229 * store() accepts one of those strings, translates it into the
230 * proper enumerated value, and initiates a suspend transition.
231 */
Kay Sievers386f2752007-11-02 13:47:53 +0100232static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
233 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200235 char *s = buf;
236#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800240 if (pm_states[i] && valid_state(i))
241 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200243#endif
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200244#ifdef CONFIG_HIBERNATION
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700245 s += sprintf(s, "%s\n", "disk");
246#else
247 if (s != buf)
248 /* convert the last space to a newline */
249 *(s-1) = '\n';
250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return (s - buf);
252}
253
Kay Sievers386f2752007-11-02 13:47:53 +0100254static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
255 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200257#ifdef CONFIG_SUSPEND
Arve Hjønnevågb0dc3432008-10-09 19:17:11 -0700258#ifdef CONFIG_EARLYSUSPEND
259 suspend_state_t state = PM_SUSPEND_ON;
260#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 suspend_state_t state = PM_SUSPEND_STANDBY;
Arve Hjønnevågb0dc3432008-10-09 19:17:11 -0700262#endif
Andreas Mohr3b364b82006-06-25 05:47:56 -0700263 const char * const *s;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 int len;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200267 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 p = memchr(buf, '\n', n);
270 len = p ? p - buf : n;
271
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700272 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700273 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700274 error = hibernate();
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200275 goto Exit;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700276 }
277
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200278#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700280 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282 }
dean gaudet47bb7892006-04-27 18:39:17 -0700283 if (state < PM_SUSPEND_MAX && *s)
Arve Hjønnevågb0dc3432008-10-09 19:17:11 -0700284#ifdef CONFIG_EARLYSUSPEND
285 if (state == PM_SUSPEND_ON || valid_state(state)) {
286 error = 0;
287 request_suspend_state(state);
288 }
289#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 error = enter_state(state);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200291#endif
Arve Hjønnevågb0dc3432008-10-09 19:17:11 -0700292#endif
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200293
294 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return error ? error : n;
296}
297
298power_attr(state);
299
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200300#ifdef CONFIG_PM_SLEEP
301/*
302 * The 'wakeup_count' attribute, along with the functions defined in
303 * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
304 * handled in a non-racy way.
305 *
306 * If a wakeup event occurs when the system is in a sleep state, it simply is
307 * woken up. In turn, if an event that would wake the system up from a sleep
308 * state occurs when it is undergoing a transition to that sleep state, the
309 * transition should be aborted. Moreover, if such an event occurs when the
310 * system is in the working state, an attempt to start a transition to the
311 * given sleep state should fail during certain period after the detection of
312 * the event. Using the 'state' attribute alone is not sufficient to satisfy
313 * these requirements, because a wakeup event may occur exactly when 'state'
314 * is being written to and may be delivered to user space right before it is
315 * frozen, so the event will remain only partially processed until the system is
316 * woken up by another event. In particular, it won't cause the transition to
317 * a sleep state to be aborted.
318 *
319 * This difficulty may be overcome if user space uses 'wakeup_count' before
320 * writing to 'state'. It first should read from 'wakeup_count' and store
321 * the read value. Then, after carrying out its own preparations for the system
322 * transition to a sleep state, it should write the stored value to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300323 * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200324 * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
325 * is allowed to write to 'state', but the transition will be aborted if there
326 * are any wakeup events detected after 'wakeup_count' was written to.
327 */
328
329static ssize_t wakeup_count_show(struct kobject *kobj,
330 struct kobj_attribute *attr,
331 char *buf)
332{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200333 unsigned int val;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200334
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200335 return pm_get_wakeup_count(&val) ? sprintf(buf, "%u\n", val) : -EINTR;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200336}
337
338static ssize_t wakeup_count_store(struct kobject *kobj,
339 struct kobj_attribute *attr,
340 const char *buf, size_t n)
341{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200342 unsigned int val;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200343
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200344 if (sscanf(buf, "%u", &val) == 1) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200345 if (pm_save_wakeup_count(val))
346 return n;
347 }
348 return -EINVAL;
349}
350
351power_attr(wakeup_count);
352#endif /* CONFIG_PM_SLEEP */
353
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700354#ifdef CONFIG_PM_TRACE
355int pm_trace_enabled;
356
Kay Sievers386f2752007-11-02 13:47:53 +0100357static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
358 char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700359{
360 return sprintf(buf, "%d\n", pm_trace_enabled);
361}
362
363static ssize_t
Kay Sievers386f2752007-11-02 13:47:53 +0100364pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
365 const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700366{
367 int val;
368
369 if (sscanf(buf, "%d", &val) == 1) {
370 pm_trace_enabled = !!val;
371 return n;
372 }
373 return -EINVAL;
374}
375
376power_attr(pm_trace);
James Hogand33ac602010-10-12 00:00:25 +0200377
378static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
379 struct kobj_attribute *attr,
380 char *buf)
381{
382 return show_trace_dev_match(buf, PAGE_SIZE);
383}
384
385static ssize_t
386pm_trace_dev_match_store(struct kobject *kobj, struct kobj_attribute *attr,
387 const char *buf, size_t n)
388{
389 return -EINVAL;
390}
391
392power_attr(pm_trace_dev_match);
393
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100394#endif /* CONFIG_PM_TRACE */
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700395
Arve Hjønnevåg47dfb462008-10-09 21:01:46 -0700396#ifdef CONFIG_USER_WAKELOCK
397power_attr(wake_lock);
398power_attr(wake_unlock);
399#endif
400
Amar Singhalac2a6d62012-02-27 17:40:31 -0800401static struct attribute *g[] = {
402 &touch_event_attr.attr,
403 &touch_event_timer_attr.attr,
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700404 &state_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100405#ifdef CONFIG_PM_TRACE
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700406 &pm_trace_attr.attr,
James Hogand33ac602010-10-12 00:00:25 +0200407 &pm_trace_dev_match_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100408#endif
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100409#ifdef CONFIG_PM_SLEEP
410 &pm_async_attr.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200411 &wakeup_count_attr.attr,
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100412#ifdef CONFIG_PM_DEBUG
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100413 &pm_test_attr.attr,
414#endif
Arve Hjønnevåg47dfb462008-10-09 21:01:46 -0700415#ifdef CONFIG_USER_WAKELOCK
416 &wake_lock_attr.attr,
417 &wake_unlock_attr.attr,
418#endif
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100419#endif
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700420 NULL,
421};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423static struct attribute_group attr_group = {
424 .attrs = g,
425};
426
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200427#ifdef CONFIG_PM_RUNTIME
428struct workqueue_struct *pm_wq;
Alan Stern7b199ca2009-12-03 20:22:21 +0100429EXPORT_SYMBOL_GPL(pm_wq);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200430
431static int __init pm_start_workqueue(void)
432{
Tejun Heo58a69cb2011-02-16 09:25:31 +0100433 pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200434
435 return pm_wq ? 0 : -ENOMEM;
436}
437#else
438static inline int pm_start_workqueue(void) { return 0; }
439#endif
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static int __init pm_init(void)
442{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200443 int error = pm_start_workqueue();
444 if (error)
445 return error;
Rafael J. Wysockiac5c24ec2010-09-20 19:44:56 +0200446 hibernate_image_size_init();
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +0200447 hibernate_reserved_size_init();
Amar Singhalac2a6d62012-02-27 17:40:31 -0800448
449 touch_evt_timer_val = ktime_set(2, 0);
Amar Singhal97d68a82012-03-20 19:03:04 -0700450 hrtimer_init(&tc_ev_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
451 tc_ev_timer.function = &tc_ev_stop;
452 tc_ev_processed = 1;
Amar Singhalac2a6d62012-02-27 17:40:31 -0800453
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800454 power_kobj = kobject_create_and_add("power", NULL);
455 if (!power_kobj)
Greg Kroah-Hartman039a5dc2007-11-01 10:39:50 -0700456 return -ENOMEM;
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800457 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460core_initcall(pm_init);