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 release under the GPLv2
13 #include <linux/suspend.h>
14 #include <linux/kobject.h>
15 #include <linux/string.h>
16 #include <linux/delay.h>
17 #include <linux/errno.h>
18 #include <linux/init.h>
24 DECLARE_MUTEX(pm_sem);
26 struct pm_ops * pm_ops = NULL;
27 u32 pm_disk_mode = PM_DISK_SHUTDOWN;
30 * pm_set_ops - Set the global power method table.
31 * @ops: Pointer to ops structure.
34 void pm_set_ops(struct pm_ops * ops)
38 if (ops->pm_disk_mode && ops->pm_disk_mode < PM_DISK_MAX)
39 pm_disk_mode = ops->pm_disk_mode;
45 * suspend_prepare - Do prep work before entering low-power state.
46 * @state: State we're entering.
48 * This is common code that is called for each state that we're
49 * entering. Allocate a console, stop all processes, then make sure
50 * the platform can enter the requested state.
53 static int suspend_prepare(u32 state)
57 if (!pm_ops || !pm_ops->enter)
62 if (freeze_processes()) {
67 if (pm_ops->prepare) {
68 if ((error = pm_ops->prepare(state)))
72 if ((error = device_suspend(state)))
77 pm_ops->finish(state);
85 static int suspend_enter(u32 state)
90 local_irq_save(flags);
91 if ((error = device_power_down(state)))
93 error = pm_ops->enter(state);
96 local_irq_restore(flags);
102 * suspend_finish - Do final work before exiting suspend sequence.
103 * @state: State we're coming out of.
105 * Call platform code to clean up, restart processes, and free the
106 * console that we've allocated.
109 static void suspend_finish(u32 state)
112 if (pm_ops && pm_ops->finish)
113 pm_ops->finish(state);
115 pm_restore_console();
121 char * pm_states[] = {
122 [PM_SUSPEND_STANDBY] = "standby",
123 [PM_SUSPEND_MEM] = "mem",
124 [PM_SUSPEND_DISK] = "disk",
130 * enter_state - Do common work of entering low-power state.
131 * @state: pm_state structure for state we're entering.
133 * Make sure we're the only ones trying to enter a sleep state. Fail
134 * if someone has beat us to it, since we don't want anything weird to
135 * happen when we wake up.
136 * Then, do the setup for suspend, enter the state, and cleaup (after
140 static int enter_state(u32 state)
144 if (down_trylock(&pm_sem))
147 /* Suspend is hard to get right on SMP. */
148 if (num_online_cpus() != 1) {
153 if (state == PM_SUSPEND_DISK) {
154 error = pm_suspend_disk();
158 pr_debug("PM: Preparing system for suspend\n");
159 if ((error = suspend_prepare(state)))
162 pr_debug("PM: Entering state.\n");
163 error = suspend_enter(state);
165 pr_debug("PM: Finishing up.\n");
166 suspend_finish(state);
174 * pm_suspend - Externally visible function for suspending system.
175 * @state: Enumarted value of state to enter.
177 * Determine whether or not value is within range, get state
178 * structure, and enter (above).
181 int pm_suspend(u32 state)
183 if (state > PM_SUSPEND_ON && state < PM_SUSPEND_MAX)
184 return enter_state(state);
190 decl_subsys(power,NULL,NULL);
194 * state - control system power state.
196 * show() returns what states are supported, which is hard-coded to
197 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
198 * 'disk' (Suspend-to-Disk).
200 * store() accepts one of those strings, translates it into the
201 * proper enumerated value, and initiates a suspend transition.
204 static ssize_t state_show(struct subsystem * subsys, char * buf)
209 for (i = 0; i < PM_SUSPEND_MAX; i++) {
211 s += sprintf(s,"%s ",pm_states[i]);
213 s += sprintf(s,"\n");
217 static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
219 u32 state = PM_SUSPEND_STANDBY;
225 p = memchr(buf, '\n', n);
226 len = p ? p - buf : n;
228 for (s = &pm_states[state]; *s; s++, state++) {
229 if (!strncmp(buf, *s, len))
233 error = enter_state(state);
236 return error ? error : n;
241 static struct attribute * g[] = {
246 static struct attribute_group attr_group = {
251 static int __init pm_init(void)
253 int error = subsystem_register(&power_subsys);
255 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
259 core_initcall(pm_init);