blob: fc717b836828fdb991532dc8d20c40851535b127 [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
Rafael J. Wysockib10d9112007-07-19 01:47:36 -070027BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28
Stephen Hemmingera6d70982006-12-06 20:34:35 -080029DEFINE_MUTEX(pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Len Brown9f9adec2007-12-13 17:38:03 -050031unsigned int pm_flags;
32EXPORT_SYMBOL(pm_flags);
33
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +010034#ifdef CONFIG_PM_DEBUG
35int pm_test_level = TEST_NONE;
36
37static int suspend_test(int level)
38{
39 if (pm_test_level == level) {
40 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
41 mdelay(5000);
42 return 1;
43 }
44 return 0;
45}
46
47static const char * const pm_tests[__TEST_AFTER_LAST] = {
48 [TEST_NONE] = "none",
49 [TEST_CORE] = "core",
50 [TEST_CPUS] = "processors",
51 [TEST_PLATFORM] = "platform",
52 [TEST_DEVICES] = "devices",
53 [TEST_FREEZER] = "freezer",
54};
55
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +010056static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
57 char *buf)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +010058{
59 char *s = buf;
60 int level;
61
62 for (level = TEST_FIRST; level <= TEST_MAX; level++)
63 if (pm_tests[level]) {
64 if (level == pm_test_level)
65 s += sprintf(s, "[%s] ", pm_tests[level]);
66 else
67 s += sprintf(s, "%s ", pm_tests[level]);
68 }
69
70 if (s != buf)
71 /* convert the last space to a newline */
72 *(s-1) = '\n';
73
74 return (s - buf);
75}
76
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +010077static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
78 const char *buf, size_t n)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +010079{
80 const char * const *s;
81 int level;
82 char *p;
83 int len;
84 int error = -EINVAL;
85
86 p = memchr(buf, '\n', n);
87 len = p ? p - buf : n;
88
89 mutex_lock(&pm_mutex);
90
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
99 mutex_unlock(&pm_mutex);
100
101 return error ? error : n;
102}
103
104power_attr(pm_test);
105#else /* !CONFIG_PM_DEBUG */
106static inline int suspend_test(int level) { return 0; }
107#endif /* !CONFIG_PM_DEBUG */
108
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +0100109
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200110#ifdef CONFIG_SUSPEND
111
112/* This is just an arbitrary number */
113#define FREE_PAGE_NUMBER (100)
114
Rafael J. Wysockif242d912007-10-18 03:04:41 -0700115static struct platform_suspend_ops *suspend_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700118 * suspend_set_ops - Set the global suspend method table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * @ops: Pointer to ops structure.
120 */
121
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700122void suspend_set_ops(struct platform_suspend_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800124 mutex_lock(&pm_mutex);
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700125 suspend_ops = ops;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800126 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Johannes Berge8c9c502007-04-30 15:09:54 -0700129/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700130 * suspend_valid_only_mem - generic memory-only valid callback
Johannes Berge8c9c502007-04-30 15:09:54 -0700131 *
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700132 * Platform drivers that implement mem suspend only and only need
Johannes Berge8c9c502007-04-30 15:09:54 -0700133 * to check for that in their .valid callback can use this instead
134 * of rolling their own .valid callback.
135 */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700136int suspend_valid_only_mem(suspend_state_t state)
Johannes Berge8c9c502007-04-30 15:09:54 -0700137{
138 return state == PM_SUSPEND_MEM;
139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/**
142 * suspend_prepare - Do prep work before entering low-power state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 *
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700144 * This is common code that is called for each state that we're entering.
145 * Run suspend notifiers, allocate a console and stop all processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700147static int suspend_prepare(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700149 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -0500150 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700152 if (!suspend_ops || !suspend_ops->enter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return -EPERM;
154
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700155 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
156 if (error)
157 goto Finish;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 pm_prepare_console();
160
161 if (freeze_processes()) {
162 error = -EAGAIN;
163 goto Thaw;
164 }
165
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700166 free_pages = global_page_state(NR_FREE_PAGES);
167 if (free_pages < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -0500168 pr_debug("PM: free some memory\n");
169 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
170 if (nr_free_pages() < FREE_PAGE_NUMBER) {
171 error = -ENOMEM;
172 printk(KERN_ERR "PM: No enough memory\n");
David Shaohua Li5ae947e2005-03-18 16:27:13 -0500173 }
174 }
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800175 if (!error)
176 return 0;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 Thaw:
179 thaw_processes();
180 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700181 Finish:
182 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return error;
184}
185
Johannes Berga53c46d2007-04-26 11:43:58 +0200186/* default implementation */
187void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
188{
189 local_irq_disable();
190}
191
192/* default implementation */
193void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
194{
195 local_irq_enable();
196}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700198/**
199 * suspend_enter - enter the desired system sleep state.
200 * @state: state to enter
201 *
202 * This function should be called after devices have been suspended.
203 */
Adrian Bunka065c862007-10-18 03:04:37 -0700204static int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Johannes Berga53c46d2007-04-26 11:43:58 +0200208 arch_suspend_disable_irqs();
209 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 if ((error = device_power_down(PMSG_SUSPEND))) {
212 printk(KERN_ERR "Some devices failed to power down\n");
213 goto Done;
214 }
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100215
216 if (!suspend_test(TEST_CORE))
217 error = suspend_ops->enter(state);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 device_power_up();
220 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200221 arch_suspend_enable_irqs();
222 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return error;
224}
225
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700226/**
227 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
228 * state.
229 * @state: state to enter
230 */
231int suspend_devices_and_enter(suspend_state_t state)
232{
233 int error;
234
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700235 if (!suspend_ops)
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700236 return -ENOSYS;
237
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700238 if (suspend_ops->set_target) {
239 error = suspend_ops->set_target(state);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700240 if (error)
241 return error;
242 }
243 suspend_console();
244 error = device_suspend(PMSG_SUSPEND);
245 if (error) {
246 printk(KERN_ERR "Some devices failed to suspend\n");
247 goto Resume_console;
248 }
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100249
250 if (suspend_test(TEST_DEVICES))
251 goto Resume_devices;
252
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700253 if (suspend_ops->prepare) {
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700254 error = suspend_ops->prepare();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700255 if (error)
256 goto Resume_devices;
257 }
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100258
259 if (suspend_test(TEST_PLATFORM))
260 goto Finish;
261
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700262 error = disable_nonboot_cpus();
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100263 if (!error && !suspend_test(TEST_CPUS))
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700264 suspend_enter(state);
265
266 enable_nonboot_cpus();
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100267 Finish:
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700268 if (suspend_ops->finish)
269 suspend_ops->finish();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700270 Resume_devices:
271 device_resume();
272 Resume_console:
273 resume_console();
274 return error;
275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/**
278 * suspend_finish - Do final work before exiting suspend sequence.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *
280 * Call platform code to clean up, restart processes, and free the
281 * console that we've allocated. This is not called for suspend-to-disk.
282 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700283static void suspend_finish(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 thaw_processes();
286 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700287 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290
291
292
Andreas Mohr3b364b82006-06-25 05:47:56 -0700293static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 [PM_SUSPEND_STANDBY] = "standby",
295 [PM_SUSPEND_MEM] = "mem",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296};
297
Pavel Machek123d3c12005-11-29 19:34:37 -0800298static inline int valid_state(suspend_state_t state)
299{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700300 /* All states need lowlevel support and need to be valid
301 * to the lowlevel implementation, no valid callback
Johannes Berg9684e512007-04-30 15:09:55 -0700302 * implies that none are valid. */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700303 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
Pavel Machek123d3c12005-11-29 19:34:37 -0800304 return 0;
305 return 1;
306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/**
310 * enter_state - Do common work of entering low-power state.
311 * @state: pm_state structure for state we're entering.
312 *
313 * Make sure we're the only ones trying to enter a sleep state. Fail
314 * if someone has beat us to it, since we don't want anything weird to
315 * happen when we wake up.
316 * Then, do the setup for suspend, enter the state, and cleaup (after
317 * we've woken up).
318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static int enter_state(suspend_state_t state)
320{
321 int error;
322
Pavel Machek123d3c12005-11-29 19:34:37 -0800323 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800324 return -ENODEV;
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700325
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800326 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return -EBUSY;
328
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700329 printk("Syncing filesystems ... ");
330 sys_sync();
331 printk("done.\n");
332
David Brownell82428b62005-05-09 08:07:00 -0700333 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100334 error = suspend_prepare();
335 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto Unlock;
337
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100338 if (suspend_test(TEST_FREEZER))
339 goto Finish;
340
David Brownell82428b62005-05-09 08:07:00 -0700341 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700342 error = suspend_devices_and_enter(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100344 Finish:
David Brownell82428b62005-05-09 08:07:00 -0700345 pr_debug("PM: Finishing wakeup.\n");
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700346 suspend_finish();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800348 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return error;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353/**
354 * pm_suspend - Externally visible function for suspending system.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700355 * @state: Enumerated value of state to enter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *
357 * Determine whether or not value is within range, get state
358 * structure, and enter (above).
359 */
360
361int pm_suspend(suspend_state_t state)
362{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500363 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return enter_state(state);
365 return -EINVAL;
366}
367
Ralf Baechle4cf30342006-12-06 20:36:06 -0800368EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200370#endif /* CONFIG_SUSPEND */
371
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800372struct kobject *power_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374/**
375 * state - control system power state.
376 *
377 * show() returns what states are supported, which is hard-coded to
378 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
379 * 'disk' (Suspend-to-Disk).
380 *
381 * store() accepts one of those strings, translates it into the
382 * proper enumerated value, and initiates a suspend transition.
383 */
384
Kay Sievers386f2752007-11-02 13:47:53 +0100385static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
386 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200388 char *s = buf;
389#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800393 if (pm_states[i] && valid_state(i))
394 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200396#endif
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200397#ifdef CONFIG_HIBERNATION
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700398 s += sprintf(s, "%s\n", "disk");
399#else
400 if (s != buf)
401 /* convert the last space to a newline */
402 *(s-1) = '\n';
403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return (s - buf);
405}
406
Kay Sievers386f2752007-11-02 13:47:53 +0100407static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
408 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200410#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700412 const char * const *s;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200413#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int len;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200416 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 p = memchr(buf, '\n', n);
419 len = p ? p - buf : n;
420
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700421 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700422 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700423 error = hibernate();
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200424 goto Exit;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700425 }
426
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200427#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700429 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 break;
431 }
dean gaudet47bb7892006-04-27 18:39:17 -0700432 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 error = enter_state(state);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200434#endif
435
436 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return error ? error : n;
438}
439
440power_attr(state);
441
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700442#ifdef CONFIG_PM_TRACE
443int pm_trace_enabled;
444
Kay Sievers386f2752007-11-02 13:47:53 +0100445static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
446 char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700447{
448 return sprintf(buf, "%d\n", pm_trace_enabled);
449}
450
451static ssize_t
Kay Sievers386f2752007-11-02 13:47:53 +0100452pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
453 const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700454{
455 int val;
456
457 if (sscanf(buf, "%d", &val) == 1) {
458 pm_trace_enabled = !!val;
459 return n;
460 }
461 return -EINVAL;
462}
463
464power_attr(pm_trace);
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100465#endif /* CONFIG_PM_TRACE */
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700466
467static struct attribute * g[] = {
468 &state_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100469#ifdef CONFIG_PM_TRACE
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700470 &pm_trace_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100471#endif
472#ifdef CONFIG_PM_DEBUG
473 &pm_test_attr.attr,
474#endif
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700475 NULL,
476};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478static struct attribute_group attr_group = {
479 .attrs = g,
480};
481
482
483static int __init pm_init(void)
484{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800485 power_kobj = kobject_create_and_add("power", NULL);
486 if (!power_kobj)
Greg Kroah-Hartman039a5dc2007-11-01 10:39:50 -0700487 return -ENOMEM;
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800488 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491core_initcall(pm_init);