blob: d0cedaa7edf1c519ae32b38faea8b7cb52804134 [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
Ralf Baechle4cf30342006-12-06 20:36:06 -080011#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/suspend.h>
13#include <linux/kobject.h>
14#include <linux/string.h>
15#include <linux/delay.h>
16#include <linux/errno.h>
17#include <linux/init.h>
Andrew Morton6cc07192006-06-22 14:47:18 -070018#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070019#include <linux/cpu.h>
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -070020#include <linux/resume-trace.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080021#include <linux/freezer.h>
Christoph Lameter96177292007-02-10 01:43:03 -080022#include <linux/vmstat.h>
Rafael J. Wysocki232b1432007-10-18 03:04:44 -070023#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "power.h"
26
Stephen Hemmingera6d70982006-12-06 20:34:35 -080027DEFINE_MUTEX(pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Len Brown9f9adec2007-12-13 17:38:03 -050029unsigned int pm_flags;
30EXPORT_SYMBOL(pm_flags);
31
Alan Stern82525752007-11-19 23:49:18 +010032#ifdef CONFIG_PM_SLEEP
33
34/* Routines for PM-transition notifications */
35
36static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
37
38int register_pm_notifier(struct notifier_block *nb)
39{
40 return blocking_notifier_chain_register(&pm_chain_head, nb);
41}
42EXPORT_SYMBOL_GPL(register_pm_notifier);
43
44int unregister_pm_notifier(struct notifier_block *nb)
45{
46 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
47}
48EXPORT_SYMBOL_GPL(unregister_pm_notifier);
49
50int pm_notifier_call_chain(unsigned long val)
51{
52 return (blocking_notifier_call_chain(&pm_chain_head, val, NULL)
53 == NOTIFY_BAD) ? -EINVAL : 0;
54}
55
56#endif /* CONFIG_PM_SLEEP */
57
Rafael J. Wysocki2ed43b62007-12-08 02:03:26 +010058#ifdef CONFIG_SUSPEND
59
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +010060#ifdef CONFIG_PM_DEBUG
61int pm_test_level = TEST_NONE;
62
63static int suspend_test(int level)
64{
65 if (pm_test_level == level) {
66 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
67 mdelay(5000);
68 return 1;
69 }
70 return 0;
71}
72
73static const char * const pm_tests[__TEST_AFTER_LAST] = {
74 [TEST_NONE] = "none",
75 [TEST_CORE] = "core",
76 [TEST_CPUS] = "processors",
77 [TEST_PLATFORM] = "platform",
78 [TEST_DEVICES] = "devices",
79 [TEST_FREEZER] = "freezer",
80};
81
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +010082static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
83 char *buf)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +010084{
85 char *s = buf;
86 int level;
87
88 for (level = TEST_FIRST; level <= TEST_MAX; level++)
89 if (pm_tests[level]) {
90 if (level == pm_test_level)
91 s += sprintf(s, "[%s] ", pm_tests[level]);
92 else
93 s += sprintf(s, "%s ", pm_tests[level]);
94 }
95
96 if (s != buf)
97 /* convert the last space to a newline */
98 *(s-1) = '\n';
99
100 return (s - buf);
101}
102
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +0100103static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
104 const char *buf, size_t n)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100105{
106 const char * const *s;
107 int level;
108 char *p;
109 int len;
110 int error = -EINVAL;
111
112 p = memchr(buf, '\n', n);
113 len = p ? p - buf : n;
114
115 mutex_lock(&pm_mutex);
116
117 level = TEST_FIRST;
118 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
119 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
120 pm_test_level = level;
121 error = 0;
122 break;
123 }
124
125 mutex_unlock(&pm_mutex);
126
127 return error ? error : n;
128}
129
130power_attr(pm_test);
131#else /* !CONFIG_PM_DEBUG */
132static inline int suspend_test(int level) { return 0; }
133#endif /* !CONFIG_PM_DEBUG */
134
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +0100135
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200136#ifdef CONFIG_SUSPEND
137
138/* This is just an arbitrary number */
139#define FREE_PAGE_NUMBER (100)
140
Rafael J. Wysockif242d912007-10-18 03:04:41 -0700141static struct platform_suspend_ops *suspend_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700144 * suspend_set_ops - Set the global suspend method table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * @ops: Pointer to ops structure.
146 */
147
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700148void suspend_set_ops(struct platform_suspend_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800150 mutex_lock(&pm_mutex);
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700151 suspend_ops = ops;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800152 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Johannes Berge8c9c502007-04-30 15:09:54 -0700155/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700156 * suspend_valid_only_mem - generic memory-only valid callback
Johannes Berge8c9c502007-04-30 15:09:54 -0700157 *
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700158 * Platform drivers that implement mem suspend only and only need
Johannes Berge8c9c502007-04-30 15:09:54 -0700159 * to check for that in their .valid callback can use this instead
160 * of rolling their own .valid callback.
161 */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700162int suspend_valid_only_mem(suspend_state_t state)
Johannes Berge8c9c502007-04-30 15:09:54 -0700163{
164 return state == PM_SUSPEND_MEM;
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/**
168 * suspend_prepare - Do prep work before entering low-power state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700170 * This is common code that is called for each state that we're entering.
171 * Run suspend notifiers, allocate a console and stop all processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700173static int suspend_prepare(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700175 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -0500176 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700178 if (!suspend_ops || !suspend_ops->enter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -EPERM;
180
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700181 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
182 if (error)
183 goto Finish;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 pm_prepare_console();
186
187 if (freeze_processes()) {
188 error = -EAGAIN;
189 goto Thaw;
190 }
191
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700192 free_pages = global_page_state(NR_FREE_PAGES);
193 if (free_pages < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -0500194 pr_debug("PM: free some memory\n");
195 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
196 if (nr_free_pages() < FREE_PAGE_NUMBER) {
197 error = -ENOMEM;
198 printk(KERN_ERR "PM: No enough memory\n");
David Shaohua Li5ae947e2005-03-18 16:27:13 -0500199 }
200 }
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800201 if (!error)
202 return 0;
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 Thaw:
205 thaw_processes();
206 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700207 Finish:
208 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return error;
210}
211
Johannes Berga53c46d2007-04-26 11:43:58 +0200212/* default implementation */
213void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
214{
215 local_irq_disable();
216}
217
218/* default implementation */
219void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
220{
221 local_irq_enable();
222}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700224/**
225 * suspend_enter - enter the desired system sleep state.
226 * @state: state to enter
227 *
228 * This function should be called after devices have been suspended.
229 */
Adrian Bunka065c862007-10-18 03:04:37 -0700230static int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Johannes Berga53c46d2007-04-26 11:43:58 +0200234 arch_suspend_disable_irqs();
235 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if ((error = device_power_down(PMSG_SUSPEND))) {
238 printk(KERN_ERR "Some devices failed to power down\n");
239 goto Done;
240 }
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100241
242 if (!suspend_test(TEST_CORE))
243 error = suspend_ops->enter(state);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 device_power_up();
246 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200247 arch_suspend_enable_irqs();
248 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return error;
250}
251
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700252/**
Rafael J. Wysocki9628c0e2007-12-08 02:06:00 +0100253 * suspend_devices_and_enter - suspend devices and enter the desired system
254 * sleep state.
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700255 * @state: state to enter
256 */
257int suspend_devices_and_enter(suspend_state_t state)
258{
259 int error;
260
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700261 if (!suspend_ops)
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700262 return -ENOSYS;
263
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700264 if (suspend_ops->set_target) {
265 error = suspend_ops->set_target(state);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700266 if (error)
267 return error;
268 }
269 suspend_console();
270 error = device_suspend(PMSG_SUSPEND);
271 if (error) {
272 printk(KERN_ERR "Some devices failed to suspend\n");
273 goto Resume_console;
274 }
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100275
276 if (suspend_test(TEST_DEVICES))
277 goto Resume_devices;
278
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700279 if (suspend_ops->prepare) {
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700280 error = suspend_ops->prepare();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700281 if (error)
282 goto Resume_devices;
283 }
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100284
285 if (suspend_test(TEST_PLATFORM))
286 goto Finish;
287
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700288 error = disable_nonboot_cpus();
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100289 if (!error && !suspend_test(TEST_CPUS))
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700290 suspend_enter(state);
291
292 enable_nonboot_cpus();
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100293 Finish:
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700294 if (suspend_ops->finish)
295 suspend_ops->finish();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700296 Resume_devices:
297 device_resume();
298 Resume_console:
299 resume_console();
300 return error;
301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303/**
304 * suspend_finish - Do final work before exiting suspend sequence.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *
306 * Call platform code to clean up, restart processes, and free the
307 * console that we've allocated. This is not called for suspend-to-disk.
308 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700309static void suspend_finish(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 thaw_processes();
312 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700313 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316
317
318
Andreas Mohr3b364b82006-06-25 05:47:56 -0700319static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 [PM_SUSPEND_STANDBY] = "standby",
321 [PM_SUSPEND_MEM] = "mem",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322};
323
Pavel Machek123d3c12005-11-29 19:34:37 -0800324static inline int valid_state(suspend_state_t state)
325{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700326 /* All states need lowlevel support and need to be valid
327 * to the lowlevel implementation, no valid callback
Johannes Berg9684e512007-04-30 15:09:55 -0700328 * implies that none are valid. */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700329 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
Pavel Machek123d3c12005-11-29 19:34:37 -0800330 return 0;
331 return 1;
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335/**
336 * enter_state - Do common work of entering low-power state.
337 * @state: pm_state structure for state we're entering.
338 *
339 * Make sure we're the only ones trying to enter a sleep state. Fail
340 * if someone has beat us to it, since we don't want anything weird to
341 * happen when we wake up.
342 * Then, do the setup for suspend, enter the state, and cleaup (after
343 * we've woken up).
344 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static int enter_state(suspend_state_t state)
346{
347 int error;
348
Pavel Machek123d3c12005-11-29 19:34:37 -0800349 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800350 return -ENODEV;
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700351
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800352 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return -EBUSY;
354
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700355 printk("Syncing filesystems ... ");
356 sys_sync();
357 printk("done.\n");
358
David Brownell82428b62005-05-09 08:07:00 -0700359 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100360 error = suspend_prepare();
361 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto Unlock;
363
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100364 if (suspend_test(TEST_FREEZER))
365 goto Finish;
366
David Brownell82428b62005-05-09 08:07:00 -0700367 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700368 error = suspend_devices_and_enter(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100370 Finish:
David Brownell82428b62005-05-09 08:07:00 -0700371 pr_debug("PM: Finishing wakeup.\n");
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700372 suspend_finish();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800374 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return error;
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379/**
380 * pm_suspend - Externally visible function for suspending system.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700381 * @state: Enumerated value of state to enter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 *
383 * Determine whether or not value is within range, get state
384 * structure, and enter (above).
385 */
386
387int pm_suspend(suspend_state_t state)
388{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500389 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return enter_state(state);
391 return -EINVAL;
392}
393
Ralf Baechle4cf30342006-12-06 20:36:06 -0800394EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200396#endif /* CONFIG_SUSPEND */
397
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800398struct kobject *power_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400/**
401 * state - control system power state.
402 *
403 * show() returns what states are supported, which is hard-coded to
404 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
405 * 'disk' (Suspend-to-Disk).
406 *
407 * store() accepts one of those strings, translates it into the
408 * proper enumerated value, and initiates a suspend transition.
409 */
410
Kay Sievers386f2752007-11-02 13:47:53 +0100411static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
412 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200414 char *s = buf;
415#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800419 if (pm_states[i] && valid_state(i))
420 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200422#endif
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200423#ifdef CONFIG_HIBERNATION
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700424 s += sprintf(s, "%s\n", "disk");
425#else
426 if (s != buf)
427 /* convert the last space to a newline */
428 *(s-1) = '\n';
429#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return (s - buf);
431}
432
Kay Sievers386f2752007-11-02 13:47:53 +0100433static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
434 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200436#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700438 const char * const *s;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int len;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200442 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 p = memchr(buf, '\n', n);
445 len = p ? p - buf : n;
446
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700447 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700448 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700449 error = hibernate();
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200450 goto Exit;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700451 }
452
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200453#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700455 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
457 }
dean gaudet47bb7892006-04-27 18:39:17 -0700458 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 error = enter_state(state);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200460#endif
461
462 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return error ? error : n;
464}
465
466power_attr(state);
467
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700468#ifdef CONFIG_PM_TRACE
469int pm_trace_enabled;
470
Kay Sievers386f2752007-11-02 13:47:53 +0100471static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
472 char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700473{
474 return sprintf(buf, "%d\n", pm_trace_enabled);
475}
476
477static ssize_t
Kay Sievers386f2752007-11-02 13:47:53 +0100478pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
479 const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700480{
481 int val;
482
483 if (sscanf(buf, "%d", &val) == 1) {
484 pm_trace_enabled = !!val;
485 return n;
486 }
487 return -EINVAL;
488}
489
490power_attr(pm_trace);
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100491#endif /* CONFIG_PM_TRACE */
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700492
493static struct attribute * g[] = {
494 &state_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100495#ifdef CONFIG_PM_TRACE
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700496 &pm_trace_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100497#endif
498#ifdef CONFIG_PM_DEBUG
499 &pm_test_attr.attr,
500#endif
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700501 NULL,
502};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504static struct attribute_group attr_group = {
505 .attrs = g,
506};
507
508
509static int __init pm_init(void)
510{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800511 power_kobj = kobject_create_and_add("power", NULL);
512 if (!power_kobj)
Greg Kroah-Hartman039a5dc2007-11-01 10:39:50 -0700513 return -ENOMEM;
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800514 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517core_initcall(pm_init);