Suspend infrastructure cleanup and extension
[powerpc.git] / drivers / base / power / suspend.c
1 /*
2  * suspend.c - Functions for putting devices to sleep.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Labs
6  *
7  * This file is released under the GPLv2
8  *
9  */
10
11 #include <linux/device.h>
12 #include <linux/kallsyms.h>
13 #include <linux/pm.h>
14 #include "../base.h"
15 #include "power.h"
16
17 /*
18  * The entries in the dpm_active list are in a depth first order, simply
19  * because children are guaranteed to be discovered after parents, and
20  * are inserted at the back of the list on discovery.
21  *
22  * All list on the suspend path are done in reverse order, so we operate
23  * on the leaves of the device tree (or forests, depending on how you want
24  * to look at it ;) first. As nodes are removed from the back of the list,
25  * they are inserted into the front of their destintation lists.
26  *
27  * Things are the reverse on the resume path - iterations are done in
28  * forward order, and nodes are inserted at the back of their destination
29  * lists. This way, the ancestors will be accessed before their descendents.
30  */
31
32 static inline char *suspend_verb(u32 event)
33 {
34         switch (event) {
35         case PM_EVENT_SUSPEND:  return "suspend";
36         case PM_EVENT_FREEZE:   return "freeze";
37         default:                return "(unknown suspend event)";
38         }
39 }
40
41
42 /**
43  *      suspend_device - Save state of one device.
44  *      @dev:   Device.
45  *      @state: Power state device is entering.
46  */
47
48 int suspend_device(struct device * dev, pm_message_t state)
49 {
50         int error = 0;
51
52         down(&dev->sem);
53         if (dev->power.power_state.event) {
54                 dev_dbg(dev, "PM: suspend %d-->%d\n",
55                         dev->power.power_state.event, state.event);
56         }
57         if (dev->power.pm_parent
58                         && dev->power.pm_parent->power.power_state.event) {
59                 dev_err(dev,
60                         "PM: suspend %d->%d, parent %s already %d\n",
61                         dev->power.power_state.event, state.event,
62                         dev->power.pm_parent->bus_id,
63                         dev->power.pm_parent->power.power_state.event);
64         }
65
66         dev->power.prev_state = dev->power.power_state;
67
68         if (dev->class && dev->class->suspend && !dev->power.power_state.event) {
69                 dev_dbg(dev, "class %s%s\n",
70                         suspend_verb(state.event),
71                         ((state.event == PM_EVENT_SUSPEND)
72                                         && device_may_wakeup(dev))
73                                 ? ", may wakeup"
74                                 : ""
75                         );
76                 error = dev->class->suspend(dev, state);
77                 suspend_report_result(dev->class->suspend, error);
78         }
79
80         if (!error && dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
81                 dev_dbg(dev, "%s%s\n",
82                         suspend_verb(state.event),
83                         ((state.event == PM_EVENT_SUSPEND)
84                                         && device_may_wakeup(dev))
85                                 ? ", may wakeup"
86                                 : ""
87                         );
88                 error = dev->bus->suspend(dev, state);
89                 suspend_report_result(dev->bus->suspend, error);
90         }
91         up(&dev->sem);
92         return error;
93 }
94
95
96 /*
97  * This is called with interrupts off, only a single CPU
98  * running. We can't do down() on a semaphore (and we don't
99  * need the protection)
100  */
101 static int suspend_device_late(struct device *dev, pm_message_t state)
102 {
103         int error = 0;
104
105         if (dev->power.power_state.event) {
106                 dev_dbg(dev, "PM: suspend_late %d-->%d\n",
107                         dev->power.power_state.event, state.event);
108         }
109
110         if (dev->bus && dev->bus->suspend_late && !dev->power.power_state.event) {
111                 dev_dbg(dev, "LATE %s%s\n",
112                         suspend_verb(state.event),
113                         ((state.event == PM_EVENT_SUSPEND)
114                                         && device_may_wakeup(dev))
115                                 ? ", may wakeup"
116                                 : ""
117                         );
118                 error = dev->bus->suspend_late(dev, state);
119                 suspend_report_result(dev->bus->suspend_late, error);
120         }
121         return error;
122 }
123
124 /**
125  *      device_prepare_suspend - save state and prepare to suspend
126  *
127  *      NOTE! Devices cannot detach at this point - not only do we
128  *      hold the device list semaphores over the whole prepare, but
129  *      the whole point is to do non-invasive preparatory work, not
130  *      the actual suspend.
131  */
132 int device_prepare_suspend(pm_message_t state)
133 {
134         int error = 0;
135         struct device * dev;
136
137         down(&dpm_sem);
138         down(&dpm_list_sem);
139         list_for_each_entry_reverse(dev, &dpm_active, power.entry) {
140                 if (!dev->bus || !dev->bus->suspend_prepare)
141                         continue;
142                 error = dev->bus->suspend_prepare(dev, state);
143                 if (error)
144                         break;
145         }
146         up(&dpm_list_sem);
147         up(&dpm_sem);
148         return error;
149 }
150
151 /**
152  *      device_suspend - Save state and stop all devices in system.
153  *      @state:         Power state to put each device in.
154  *
155  *      Walk the dpm_active list, call ->suspend() for each device, and move
156  *      it to the dpm_off list.
157  *
158  *      (For historical reasons, if it returns -EAGAIN, that used to mean
159  *      that the device would be called again with interrupts disabled.
160  *      These days, we use the "suspend_late()" callback for that, so we
161  *      print a warning and consider it an error).
162  *
163  *      If we get a different error, try and back out.
164  *
165  *      If we hit a failure with any of the devices, call device_resume()
166  *      above to bring the suspended devices back to life.
167  *
168  */
169
170 int device_suspend(pm_message_t state)
171 {
172         int error = 0;
173
174         down(&dpm_sem);
175         down(&dpm_list_sem);
176         while (!list_empty(&dpm_active) && error == 0) {
177                 struct list_head * entry = dpm_active.prev;
178                 struct device * dev = to_device(entry);
179
180                 get_device(dev);
181                 up(&dpm_list_sem);
182
183                 error = suspend_device(dev, state);
184
185                 down(&dpm_list_sem);
186
187                 /* Check if the device got removed */
188                 if (!list_empty(&dev->power.entry)) {
189                         /* Move it to the dpm_off list */
190                         if (!error)
191                                 list_move(&dev->power.entry, &dpm_off);
192                 }
193                 if (error)
194                         printk(KERN_ERR "Could not suspend device %s: "
195                                 "error %d%s\n",
196                                 kobject_name(&dev->kobj), error,
197                                 error == -EAGAIN ? " (please convert to suspend_late)" : "");
198                 put_device(dev);
199         }
200         up(&dpm_list_sem);
201         if (error)
202                 dpm_resume();
203
204         up(&dpm_sem);
205         return error;
206 }
207
208 EXPORT_SYMBOL_GPL(device_suspend);
209
210 /**
211  *      device_power_down - Shut down special devices.
212  *      @state:         Power state to enter.
213  *
214  *      Walk the dpm_off_irq list, calling ->power_down() for each device that
215  *      couldn't power down the device with interrupts enabled. When we're
216  *      done, power down system devices.
217  */
218
219 int device_power_down(pm_message_t state)
220 {
221         int error = 0;
222         struct device * dev;
223
224         while (!list_empty(&dpm_off)) {
225                 struct list_head * entry = dpm_off.prev;
226
227                 dev = to_device(entry);
228                 error = suspend_device_late(dev, state);
229                 if (error)
230                         goto Error;
231                 list_move(&dev->power.entry, &dpm_off_irq);
232         }
233
234         error = sysdev_suspend(state);
235  Done:
236         return error;
237  Error:
238         printk(KERN_ERR "Could not power down device %s: "
239                 "error %d\n", kobject_name(&dev->kobj), error);
240         dpm_power_up();
241         goto Done;
242 }
243
244 EXPORT_SYMBOL_GPL(device_power_down);
245
246 void __suspend_report_result(const char *function, void *fn, int ret)
247 {
248         if (ret) {
249                 printk(KERN_ERR "%s(): ", function);
250                 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
251                 printk("%d\n", ret);
252         }
253 }
254 EXPORT_SYMBOL_GPL(__suspend_report_result);