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/suspend.h>
12 #include <linux/kobject.h>
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
22 /*This is just an arbitrary number */
23 #define FREE_PAGE_NUMBER (100)
25 DECLARE_MUTEX(pm_sem
);
27 struct pm_ops
* pm_ops
= NULL
;
28 suspend_disk_method_t pm_disk_mode
= PM_DISK_SHUTDOWN
;
31 * pm_set_ops - Set the global power method table.
32 * @ops: Pointer to ops structure.
35 void pm_set_ops(struct pm_ops
* ops
)
44 * suspend_prepare - Do prep work before entering low-power state.
45 * @state: State we're entering.
47 * This is common code that is called for each state that we're
48 * entering. Allocate a console, stop all processes, then make sure
49 * the platform can enter the requested state.
52 static int suspend_prepare(suspend_state_t state
)
55 unsigned int free_pages
;
57 if (!pm_ops
|| !pm_ops
->enter
)
62 if (freeze_processes()) {
67 if ((free_pages
= nr_free_pages()) < FREE_PAGE_NUMBER
) {
68 pr_debug("PM: free some memory\n");
69 shrink_all_memory(FREE_PAGE_NUMBER
- free_pages
);
70 if (nr_free_pages() < FREE_PAGE_NUMBER
) {
72 printk(KERN_ERR
"PM: No enough memory\n");
77 if (pm_ops
->prepare
) {
78 if ((error
= pm_ops
->prepare(state
)))
82 if ((error
= device_suspend(PMSG_SUSPEND
))) {
83 printk(KERN_ERR
"Some devices failed to suspend\n");
89 pm_ops
->finish(state
);
97 static int suspend_enter(suspend_state_t state
)
102 local_irq_save(flags
);
104 if ((error
= device_power_down(PMSG_SUSPEND
))) {
105 printk(KERN_ERR
"Some devices failed to power down\n");
108 error
= pm_ops
->enter(state
);
111 local_irq_restore(flags
);
117 * suspend_finish - Do final work before exiting suspend sequence.
118 * @state: State we're coming out of.
120 * Call platform code to clean up, restart processes, and free the
121 * console that we've allocated. This is not called for suspend-to-disk.
124 static void suspend_finish(suspend_state_t state
)
127 if (pm_ops
&& pm_ops
->finish
)
128 pm_ops
->finish(state
);
130 pm_restore_console();
136 static char * pm_states
[] = {
137 [PM_SUSPEND_STANDBY
] = "standby",
138 [PM_SUSPEND_MEM
] = "mem",
139 [PM_SUSPEND_DISK
] = "disk",
145 * enter_state - Do common work of entering low-power state.
146 * @state: pm_state structure for state we're entering.
148 * Make sure we're the only ones trying to enter a sleep state. Fail
149 * if someone has beat us to it, since we don't want anything weird to
150 * happen when we wake up.
151 * Then, do the setup for suspend, enter the state, and cleaup (after
155 static int enter_state(suspend_state_t state
)
159 if (down_trylock(&pm_sem
))
162 if (state
== PM_SUSPEND_DISK
) {
163 error
= pm_suspend_disk();
167 /* Suspend is hard to get right on SMP. */
168 if (num_online_cpus() != 1) {
173 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
174 if ((error
= suspend_prepare(state
)))
177 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
178 error
= suspend_enter(state
);
180 pr_debug("PM: Finishing wakeup.\n");
181 suspend_finish(state
);
188 * This is main interface to the outside world. It needs to be
189 * called from process context.
191 int software_suspend(void)
193 return enter_state(PM_SUSPEND_DISK
);
198 * pm_suspend - Externally visible function for suspending system.
199 * @state: Enumarted value of state to enter.
201 * Determine whether or not value is within range, get state
202 * structure, and enter (above).
205 int pm_suspend(suspend_state_t state
)
207 if (state
> PM_SUSPEND_ON
&& state
<= PM_SUSPEND_MAX
)
208 return enter_state(state
);
214 decl_subsys(power
,NULL
,NULL
);
218 * state - control system power state.
220 * show() returns what states are supported, which is hard-coded to
221 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
222 * 'disk' (Suspend-to-Disk).
224 * store() accepts one of those strings, translates it into the
225 * proper enumerated value, and initiates a suspend transition.
228 static ssize_t
state_show(struct subsystem
* subsys
, char * buf
)
233 for (i
= 0; i
< PM_SUSPEND_MAX
; i
++) {
235 s
+= sprintf(s
,"%s ",pm_states
[i
]);
237 s
+= sprintf(s
,"\n");
241 static ssize_t
state_store(struct subsystem
* subsys
, const char * buf
, size_t n
)
243 suspend_state_t state
= PM_SUSPEND_STANDBY
;
249 p
= memchr(buf
, '\n', n
);
250 len
= p
? p
- buf
: n
;
252 for (s
= &pm_states
[state
]; state
< PM_SUSPEND_MAX
; s
++, state
++) {
253 if (*s
&& !strncmp(buf
, *s
, len
))
257 error
= enter_state(state
);
260 return error
? error
: n
;
265 static struct attribute
* g
[] = {
270 static struct attribute_group attr_group
= {
275 static int __init
pm_init(void)
277 int error
= subsystem_register(&power_subsys
);
279 error
= sysfs_create_group(&power_subsys
.kset
.kobj
,&attr_group
);
283 core_initcall(pm_init
);