ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / kernel / power / main.c
1 /*
2  * kernel/power/main.c - PM subsystem core functionality.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  * 
7  * This file is release under the GPLv2
8  *
9  */
10
11 #define DEBUG
12
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>
19 #include <linux/pm.h>
20
21
22 #include "power.h"
23
24 DECLARE_MUTEX(pm_sem);
25
26 struct pm_ops * pm_ops = NULL;
27 u32 pm_disk_mode = PM_DISK_SHUTDOWN;
28
29 /**
30  *      pm_set_ops - Set the global power method table. 
31  *      @ops:   Pointer to ops structure.
32  */
33
34 void pm_set_ops(struct pm_ops * ops)
35 {
36         down(&pm_sem);
37         pm_ops = ops;
38         if (ops->pm_disk_mode && ops->pm_disk_mode < PM_DISK_MAX)
39                 pm_disk_mode = ops->pm_disk_mode;
40         up(&pm_sem);
41 }
42
43
44 /**
45  *      suspend_prepare - Do prep work before entering low-power state.
46  *      @state:         State we're entering.
47  *
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.
51  */
52
53 static int suspend_prepare(u32 state)
54 {
55         int error = 0;
56
57         if (!pm_ops || !pm_ops->enter)
58                 return -EPERM;
59
60         pm_prepare_console();
61
62         if (freeze_processes()) {
63                 error = -EAGAIN;
64                 goto Thaw;
65         }
66
67         if (pm_ops->prepare) {
68                 if ((error = pm_ops->prepare(state)))
69                         goto Thaw;
70         }
71
72         if ((error = device_suspend(state)))
73                 goto Finish;
74         return 0;
75  Finish:
76         if (pm_ops->finish)
77                 pm_ops->finish(state);
78  Thaw:
79         thaw_processes();
80         pm_restore_console();
81         return error;
82 }
83
84
85 static int suspend_enter(u32 state)
86 {
87         int error = 0;
88         unsigned long flags;
89
90         local_irq_save(flags);
91         if ((error = device_power_down(state)))
92                 goto Done;
93         error = pm_ops->enter(state);
94         device_power_up();
95  Done:
96         local_irq_restore(flags);
97         return error;
98 }
99
100
101 /**
102  *      suspend_finish - Do final work before exiting suspend sequence.
103  *      @state:         State we're coming out of.
104  *
105  *      Call platform code to clean up, restart processes, and free the 
106  *      console that we've allocated.
107  */
108
109 static void suspend_finish(u32 state)
110 {
111         device_resume();
112         if (pm_ops && pm_ops->finish)
113                 pm_ops->finish(state);
114         thaw_processes();
115         pm_restore_console();
116 }
117
118
119
120
121 char * pm_states[] = {
122         [PM_SUSPEND_STANDBY]    = "standby",
123         [PM_SUSPEND_MEM]        = "mem",
124         [PM_SUSPEND_DISK]       = "disk",
125         NULL,
126 };
127
128
129 /**
130  *      enter_state - Do common work of entering low-power state.
131  *      @state:         pm_state structure for state we're entering.
132  *
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
137  *      we've woken up).
138  */
139
140 static int enter_state(u32 state)
141 {
142         int error;
143
144         if (down_trylock(&pm_sem))
145                 return -EBUSY;
146
147         /* Suspend is hard to get right on SMP. */
148         if (num_online_cpus() != 1) {
149                 error = -EPERM;
150                 goto Unlock;
151         }
152
153         if (state == PM_SUSPEND_DISK) {
154                 error = pm_suspend_disk();
155                 goto Unlock;
156         }
157
158         pr_debug("PM: Preparing system for suspend\n");
159         if ((error = suspend_prepare(state)))
160                 goto Unlock;
161
162         pr_debug("PM: Entering state.\n");
163         error = suspend_enter(state);
164
165         pr_debug("PM: Finishing up.\n");
166         suspend_finish(state);
167  Unlock:
168         up(&pm_sem);
169         return error;
170 }
171
172
173 /**
174  *      pm_suspend - Externally visible function for suspending system.
175  *      @state:         Enumarted value of state to enter.
176  *
177  *      Determine whether or not value is within range, get state 
178  *      structure, and enter (above).
179  */
180
181 int pm_suspend(u32 state)
182 {
183         if (state > PM_SUSPEND_ON && state < PM_SUSPEND_MAX)
184                 return enter_state(state);
185         return -EINVAL;
186 }
187
188
189
190 decl_subsys(power,NULL,NULL);
191
192
193 /**
194  *      state - control system power state.
195  *
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).
199  *
200  *      store() accepts one of those strings, translates it into the 
201  *      proper enumerated value, and initiates a suspend transition.
202  */
203
204 static ssize_t state_show(struct subsystem * subsys, char * buf)
205 {
206         int i;
207         char * s = buf;
208
209         for (i = 0; i < PM_SUSPEND_MAX; i++) {
210                 if (pm_states[i])
211                         s += sprintf(s,"%s ",pm_states[i]);
212         }
213         s += sprintf(s,"\n");
214         return (s - buf);
215 }
216
217 static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
218 {
219         u32 state = PM_SUSPEND_STANDBY;
220         char ** s;
221         char *p;
222         int error;
223         int len;
224
225         p = memchr(buf, '\n', n);
226         len = p ? p - buf : n;
227
228         for (s = &pm_states[state]; *s; s++, state++) {
229                 if (!strncmp(buf, *s, len))
230                         break;
231         }
232         if (*s)
233                 error = enter_state(state);
234         else
235                 error = -EINVAL;
236         return error ? error : n;
237 }
238
239 power_attr(state);
240
241 static struct attribute * g[] = {
242         &state_attr.attr,
243         NULL,
244 };
245
246 static struct attribute_group attr_group = {
247         .attrs = g,
248 };
249
250
251 static int __init pm_init(void)
252 {
253         int error = subsystem_register(&power_subsys);
254         if (!error)
255                 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
256         return error;
257 }
258
259 core_initcall(pm_init);