2 * kernel/power/main.c - PM subsystem core functionality.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
11 #include <linux/kobject.h>
12 #include <linux/string.h>
13 #include <linux/resume-trace.h>
17 DEFINE_MUTEX(pm_mutex
);
19 unsigned int pm_flags
;
20 EXPORT_SYMBOL(pm_flags
);
22 #ifdef CONFIG_PM_SLEEP
24 /* Routines for PM-transition notifications */
26 static BLOCKING_NOTIFIER_HEAD(pm_chain_head
);
28 int register_pm_notifier(struct notifier_block
*nb
)
30 return blocking_notifier_chain_register(&pm_chain_head
, nb
);
32 EXPORT_SYMBOL_GPL(register_pm_notifier
);
34 int unregister_pm_notifier(struct notifier_block
*nb
)
36 return blocking_notifier_chain_unregister(&pm_chain_head
, nb
);
38 EXPORT_SYMBOL_GPL(unregister_pm_notifier
);
40 int pm_notifier_call_chain(unsigned long val
)
42 return (blocking_notifier_call_chain(&pm_chain_head
, val
, NULL
)
43 == NOTIFY_BAD
) ? -EINVAL
: 0;
46 #ifdef CONFIG_PM_DEBUG
47 int pm_test_level
= TEST_NONE
;
49 static const char * const pm_tests
[__TEST_AFTER_LAST
] = {
52 [TEST_CPUS
] = "processors",
53 [TEST_PLATFORM
] = "platform",
54 [TEST_DEVICES
] = "devices",
55 [TEST_FREEZER
] = "freezer",
58 static ssize_t
pm_test_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
64 for (level
= TEST_FIRST
; level
<= TEST_MAX
; level
++)
65 if (pm_tests
[level
]) {
66 if (level
== pm_test_level
)
67 s
+= sprintf(s
, "[%s] ", pm_tests
[level
]);
69 s
+= sprintf(s
, "%s ", pm_tests
[level
]);
73 /* convert the last space to a newline */
79 static ssize_t
pm_test_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
80 const char *buf
, size_t n
)
82 const char * const *s
;
88 p
= memchr(buf
, '\n', n
);
89 len
= p
? p
- buf
: n
;
91 mutex_lock(&pm_mutex
);
94 for (s
= &pm_tests
[level
]; level
<= TEST_MAX
; s
++, level
++)
95 if (*s
&& len
== strlen(*s
) && !strncmp(buf
, *s
, len
)) {
96 pm_test_level
= level
;
101 mutex_unlock(&pm_mutex
);
103 return error
? error
: n
;
107 #endif /* CONFIG_PM_DEBUG */
109 #endif /* CONFIG_PM_SLEEP */
111 struct kobject
*power_kobj
;
114 * state - control system power state.
116 * show() returns what states are supported, which is hard-coded to
117 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
118 * 'disk' (Suspend-to-Disk).
120 * store() accepts one of those strings, translates it into the
121 * proper enumerated value, and initiates a suspend transition.
123 static ssize_t
state_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
127 #ifdef CONFIG_SUSPEND
130 for (i
= 0; i
< PM_SUSPEND_MAX
; i
++) {
131 if (pm_states
[i
] && valid_state(i
))
132 s
+= sprintf(s
,"%s ", pm_states
[i
]);
135 #ifdef CONFIG_HIBERNATION
136 s
+= sprintf(s
, "%s\n", "disk");
139 /* convert the last space to a newline */
145 static ssize_t
state_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
146 const char *buf
, size_t n
)
148 #ifdef CONFIG_SUSPEND
149 suspend_state_t state
= PM_SUSPEND_STANDBY
;
150 const char * const *s
;
156 p
= memchr(buf
, '\n', n
);
157 len
= p
? p
- buf
: n
;
159 /* First, check if we are requested to hibernate */
160 if (len
== 4 && !strncmp(buf
, "disk", len
)) {
165 #ifdef CONFIG_SUSPEND
166 for (s
= &pm_states
[state
]; state
< PM_SUSPEND_MAX
; s
++, state
++) {
167 if (*s
&& len
== strlen(*s
) && !strncmp(buf
, *s
, len
))
170 if (state
< PM_SUSPEND_MAX
&& *s
)
171 error
= enter_state(state
);
175 return error
? error
: n
;
180 #ifdef CONFIG_PM_TRACE
181 int pm_trace_enabled
;
183 static ssize_t
pm_trace_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
186 return sprintf(buf
, "%d\n", pm_trace_enabled
);
190 pm_trace_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
191 const char *buf
, size_t n
)
195 if (sscanf(buf
, "%d", &val
) == 1) {
196 pm_trace_enabled
= !!val
;
202 power_attr(pm_trace
);
203 #endif /* CONFIG_PM_TRACE */
205 static struct attribute
* g
[] = {
207 #ifdef CONFIG_PM_TRACE
210 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
216 static struct attribute_group attr_group
= {
220 static int __init
pm_init(void)
222 power_kobj
= kobject_create_and_add("power", NULL
);
225 return sysfs_create_group(power_kobj
, &attr_group
);
228 core_initcall(pm_init
);