[SECURITY] getpeersec: Fix build breakage
[linux-2.6/mini2440.git] / kernel / power / main.c
blob9cb235cba4a9c8136670a74b034fd1738bb615dc
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/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>
17 #include <linux/pm.h>
20 #include "power.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;
28 suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
30 /**
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)
37 down(&pm_sem);
38 pm_ops = ops;
39 up(&pm_sem);
43 /**
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)
54 int error = 0;
55 unsigned int free_pages;
57 if (!pm_ops || !pm_ops->enter)
58 return -EPERM;
60 pm_prepare_console();
62 disable_nonboot_cpus();
64 if (num_online_cpus() != 1) {
65 error = -EPERM;
66 goto Enable_cpu;
69 if (freeze_processes()) {
70 error = -EAGAIN;
71 goto Thaw;
74 if ((free_pages = nr_free_pages()) < FREE_PAGE_NUMBER) {
75 pr_debug("PM: free some memory\n");
76 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
77 if (nr_free_pages() < FREE_PAGE_NUMBER) {
78 error = -ENOMEM;
79 printk(KERN_ERR "PM: No enough memory\n");
80 goto Thaw;
84 if (pm_ops->prepare) {
85 if ((error = pm_ops->prepare(state)))
86 goto Thaw;
89 if ((error = device_suspend(PMSG_SUSPEND))) {
90 printk(KERN_ERR "Some devices failed to suspend\n");
91 goto Finish;
93 return 0;
94 Finish:
95 if (pm_ops->finish)
96 pm_ops->finish(state);
97 Thaw:
98 thaw_processes();
99 Enable_cpu:
100 enable_nonboot_cpus();
101 pm_restore_console();
102 return error;
106 static int suspend_enter(suspend_state_t state)
108 int error = 0;
109 unsigned long flags;
111 local_irq_save(flags);
113 if ((error = device_power_down(PMSG_SUSPEND))) {
114 printk(KERN_ERR "Some devices failed to power down\n");
115 goto Done;
117 error = pm_ops->enter(state);
118 device_power_up();
119 Done:
120 local_irq_restore(flags);
121 return error;
126 * suspend_finish - Do final work before exiting suspend sequence.
127 * @state: State we're coming out of.
129 * Call platform code to clean up, restart processes, and free the
130 * console that we've allocated. This is not called for suspend-to-disk.
133 static void suspend_finish(suspend_state_t state)
135 device_resume();
136 thaw_processes();
137 enable_nonboot_cpus();
138 if (pm_ops && pm_ops->finish)
139 pm_ops->finish(state);
140 pm_restore_console();
146 static char *pm_states[PM_SUSPEND_MAX] = {
147 [PM_SUSPEND_STANDBY] = "standby",
148 [PM_SUSPEND_MEM] = "mem",
149 #ifdef CONFIG_SOFTWARE_SUSPEND
150 [PM_SUSPEND_DISK] = "disk",
151 #endif
154 static inline int valid_state(suspend_state_t state)
156 /* Suspend-to-disk does not really need low-level support.
157 * It can work with reboot if needed. */
158 if (state == PM_SUSPEND_DISK)
159 return 1;
161 if (pm_ops && pm_ops->valid && !pm_ops->valid(state))
162 return 0;
163 return 1;
168 * enter_state - Do common work of entering low-power state.
169 * @state: pm_state structure for state we're entering.
171 * Make sure we're the only ones trying to enter a sleep state. Fail
172 * if someone has beat us to it, since we don't want anything weird to
173 * happen when we wake up.
174 * Then, do the setup for suspend, enter the state, and cleaup (after
175 * we've woken up).
178 static int enter_state(suspend_state_t state)
180 int error;
182 if (!valid_state(state))
183 return -ENODEV;
184 if (down_trylock(&pm_sem))
185 return -EBUSY;
187 if (state == PM_SUSPEND_DISK) {
188 error = pm_suspend_disk();
189 goto Unlock;
192 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
193 if ((error = suspend_prepare(state)))
194 goto Unlock;
196 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
197 error = suspend_enter(state);
199 pr_debug("PM: Finishing wakeup.\n");
200 suspend_finish(state);
201 Unlock:
202 up(&pm_sem);
203 return error;
207 * This is main interface to the outside world. It needs to be
208 * called from process context.
210 int software_suspend(void)
212 return enter_state(PM_SUSPEND_DISK);
217 * pm_suspend - Externally visible function for suspending system.
218 * @state: Enumarted value of state to enter.
220 * Determine whether or not value is within range, get state
221 * structure, and enter (above).
224 int pm_suspend(suspend_state_t state)
226 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
227 return enter_state(state);
228 return -EINVAL;
233 decl_subsys(power,NULL,NULL);
237 * state - control system power state.
239 * show() returns what states are supported, which is hard-coded to
240 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
241 * 'disk' (Suspend-to-Disk).
243 * store() accepts one of those strings, translates it into the
244 * proper enumerated value, and initiates a suspend transition.
247 static ssize_t state_show(struct subsystem * subsys, char * buf)
249 int i;
250 char * s = buf;
252 for (i = 0; i < PM_SUSPEND_MAX; i++) {
253 if (pm_states[i] && valid_state(i))
254 s += sprintf(s,"%s ", pm_states[i]);
256 s += sprintf(s,"\n");
257 return (s - buf);
260 static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
262 suspend_state_t state = PM_SUSPEND_STANDBY;
263 char ** s;
264 char *p;
265 int error;
266 int len;
268 p = memchr(buf, '\n', n);
269 len = p ? p - buf : n;
271 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
272 if (*s && !strncmp(buf, *s, len))
273 break;
275 if (*s)
276 error = enter_state(state);
277 else
278 error = -EINVAL;
279 return error ? error : n;
282 power_attr(state);
284 static struct attribute * g[] = {
285 &state_attr.attr,
286 NULL,
289 static struct attribute_group attr_group = {
290 .attrs = g,
294 static int __init pm_init(void)
296 int error = subsystem_register(&power_subsys);
297 if (!error)
298 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
299 return error;
302 core_initcall(pm_init);