PM: Rename struct pm_ops and related things
[linux-2.6/mini2440.git] / kernel / power / main.c
blob854bf0811d40877eee5e3ed0d5161a3374f6c9a4
1 /*
2 * kernel/power/main.c - PM subsystem core functionality.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 *
7 * This file is released under the GPLv2
9 */
11 #include <linux/module.h>
12 #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>
18 #include <linux/console.h>
19 #include <linux/cpu.h>
20 #include <linux/resume-trace.h>
21 #include <linux/freezer.h>
22 #include <linux/vmstat.h>
24 #include "power.h"
26 BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28 DEFINE_MUTEX(pm_mutex);
30 #ifdef CONFIG_SUSPEND
32 /* This is just an arbitrary number */
33 #define FREE_PAGE_NUMBER (100)
35 struct platform_suspend_ops *suspend_ops;
37 /**
38 * suspend_set_ops - Set the global suspend method table.
39 * @ops: Pointer to ops structure.
42 void suspend_set_ops(struct platform_suspend_ops *ops)
44 mutex_lock(&pm_mutex);
45 suspend_ops = ops;
46 mutex_unlock(&pm_mutex);
49 /**
50 * suspend_valid_only_mem - generic memory-only valid callback
52 * Platform drivers that implement mem suspend only and only need
53 * to check for that in their .valid callback can use this instead
54 * of rolling their own .valid callback.
56 int suspend_valid_only_mem(suspend_state_t state)
58 return state == PM_SUSPEND_MEM;
62 static inline void pm_finish(suspend_state_t state)
64 if (suspend_ops->finish)
65 suspend_ops->finish(state);
68 /**
69 * suspend_prepare - Do prep work before entering low-power state.
71 * This is common code that is called for each state that we're entering.
72 * Run suspend notifiers, allocate a console and stop all processes.
74 static int suspend_prepare(void)
76 int error;
77 unsigned int free_pages;
79 if (!suspend_ops || !suspend_ops->enter)
80 return -EPERM;
82 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
83 if (error)
84 goto Finish;
86 pm_prepare_console();
88 if (freeze_processes()) {
89 error = -EAGAIN;
90 goto Thaw;
93 free_pages = global_page_state(NR_FREE_PAGES);
94 if (free_pages < FREE_PAGE_NUMBER) {
95 pr_debug("PM: free some memory\n");
96 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
97 if (nr_free_pages() < FREE_PAGE_NUMBER) {
98 error = -ENOMEM;
99 printk(KERN_ERR "PM: No enough memory\n");
102 if (!error)
103 return 0;
105 Thaw:
106 thaw_processes();
107 pm_restore_console();
108 Finish:
109 pm_notifier_call_chain(PM_POST_SUSPEND);
110 return error;
113 /* default implementation */
114 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
116 local_irq_disable();
119 /* default implementation */
120 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
122 local_irq_enable();
126 * suspend_enter - enter the desired system sleep state.
127 * @state: state to enter
129 * This function should be called after devices have been suspended.
131 static int suspend_enter(suspend_state_t state)
133 int error = 0;
135 arch_suspend_disable_irqs();
136 BUG_ON(!irqs_disabled());
138 if ((error = device_power_down(PMSG_SUSPEND))) {
139 printk(KERN_ERR "Some devices failed to power down\n");
140 goto Done;
142 error = suspend_ops->enter(state);
143 device_power_up();
144 Done:
145 arch_suspend_enable_irqs();
146 BUG_ON(irqs_disabled());
147 return error;
151 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
152 * state.
153 * @state: state to enter
155 int suspend_devices_and_enter(suspend_state_t state)
157 int error;
159 if (!suspend_ops)
160 return -ENOSYS;
162 if (suspend_ops->set_target) {
163 error = suspend_ops->set_target(state);
164 if (error)
165 return error;
167 suspend_console();
168 error = device_suspend(PMSG_SUSPEND);
169 if (error) {
170 printk(KERN_ERR "Some devices failed to suspend\n");
171 goto Resume_console;
173 if (suspend_ops->prepare) {
174 error = suspend_ops->prepare(state);
175 if (error)
176 goto Resume_devices;
178 error = disable_nonboot_cpus();
179 if (!error)
180 suspend_enter(state);
182 enable_nonboot_cpus();
183 pm_finish(state);
184 Resume_devices:
185 device_resume();
186 Resume_console:
187 resume_console();
188 return error;
192 * suspend_finish - Do final work before exiting suspend sequence.
194 * Call platform code to clean up, restart processes, and free the
195 * console that we've allocated. This is not called for suspend-to-disk.
197 static void suspend_finish(void)
199 thaw_processes();
200 pm_restore_console();
201 pm_notifier_call_chain(PM_POST_SUSPEND);
207 static const char * const pm_states[PM_SUSPEND_MAX] = {
208 [PM_SUSPEND_STANDBY] = "standby",
209 [PM_SUSPEND_MEM] = "mem",
212 static inline int valid_state(suspend_state_t state)
214 /* All states need lowlevel support and need to be valid
215 * to the lowlevel implementation, no valid callback
216 * implies that none are valid. */
217 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
218 return 0;
219 return 1;
224 * enter_state - Do common work of entering low-power state.
225 * @state: pm_state structure for state we're entering.
227 * Make sure we're the only ones trying to enter a sleep state. Fail
228 * if someone has beat us to it, since we don't want anything weird to
229 * happen when we wake up.
230 * Then, do the setup for suspend, enter the state, and cleaup (after
231 * we've woken up).
233 static int enter_state(suspend_state_t state)
235 int error;
237 if (!valid_state(state))
238 return -ENODEV;
239 if (!mutex_trylock(&pm_mutex))
240 return -EBUSY;
242 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
243 if ((error = suspend_prepare()))
244 goto Unlock;
246 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
247 error = suspend_devices_and_enter(state);
249 pr_debug("PM: Finishing wakeup.\n");
250 suspend_finish();
251 Unlock:
252 mutex_unlock(&pm_mutex);
253 return error;
258 * pm_suspend - Externally visible function for suspending system.
259 * @state: Enumerated value of state to enter.
261 * Determine whether or not value is within range, get state
262 * structure, and enter (above).
265 int pm_suspend(suspend_state_t state)
267 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
268 return enter_state(state);
269 return -EINVAL;
272 EXPORT_SYMBOL(pm_suspend);
274 #endif /* CONFIG_SUSPEND */
276 decl_subsys(power,NULL,NULL);
280 * state - control system power state.
282 * show() returns what states are supported, which is hard-coded to
283 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
284 * 'disk' (Suspend-to-Disk).
286 * store() accepts one of those strings, translates it into the
287 * proper enumerated value, and initiates a suspend transition.
290 static ssize_t state_show(struct kset *kset, char *buf)
292 char *s = buf;
293 #ifdef CONFIG_SUSPEND
294 int i;
296 for (i = 0; i < PM_SUSPEND_MAX; i++) {
297 if (pm_states[i] && valid_state(i))
298 s += sprintf(s,"%s ", pm_states[i]);
300 #endif
301 #ifdef CONFIG_HIBERNATION
302 s += sprintf(s, "%s\n", "disk");
303 #else
304 if (s != buf)
305 /* convert the last space to a newline */
306 *(s-1) = '\n';
307 #endif
308 return (s - buf);
311 static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
313 #ifdef CONFIG_SUSPEND
314 suspend_state_t state = PM_SUSPEND_STANDBY;
315 const char * const *s;
316 #endif
317 char *p;
318 int len;
319 int error = -EINVAL;
321 p = memchr(buf, '\n', n);
322 len = p ? p - buf : n;
324 /* First, check if we are requested to hibernate */
325 if (len == 4 && !strncmp(buf, "disk", len)) {
326 error = hibernate();
327 goto Exit;
330 #ifdef CONFIG_SUSPEND
331 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
332 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
333 break;
335 if (state < PM_SUSPEND_MAX && *s)
336 error = enter_state(state);
337 #endif
339 Exit:
340 return error ? error : n;
343 power_attr(state);
345 #ifdef CONFIG_PM_TRACE
346 int pm_trace_enabled;
348 static ssize_t pm_trace_show(struct kset *kset, char *buf)
350 return sprintf(buf, "%d\n", pm_trace_enabled);
353 static ssize_t
354 pm_trace_store(struct kset *kset, const char *buf, size_t n)
356 int val;
358 if (sscanf(buf, "%d", &val) == 1) {
359 pm_trace_enabled = !!val;
360 return n;
362 return -EINVAL;
365 power_attr(pm_trace);
367 static struct attribute * g[] = {
368 &state_attr.attr,
369 &pm_trace_attr.attr,
370 NULL,
372 #else
373 static struct attribute * g[] = {
374 &state_attr.attr,
375 NULL,
377 #endif /* CONFIG_PM_TRACE */
379 static struct attribute_group attr_group = {
380 .attrs = g,
384 static int __init pm_init(void)
386 int error = subsystem_register(&power_subsys);
387 if (!error)
388 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
389 return error;
392 core_initcall(pm_init);