1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/device.h>
23#include <linux/kallsyms.h>
24#include <linux/mutex.h>
25#include <linux/pm.h>
26#include <linux/resume-trace.h>
27
28#include "../base.h"
29#include "power.h"
30
31LIST_HEAD(dpm_active);
32static LIST_HEAD(dpm_off);
33static LIST_HEAD(dpm_off_irq);
34
35static DEFINE_MUTEX(dpm_mtx);
36static DEFINE_MUTEX(dpm_list_mtx);
37
38int (*platform_enable_wakeup)(struct device *dev, int is_on);
39
40
41void device_pm_add(struct device *dev)
42{
43 pr_debug("PM: Adding info for %s:%s\n",
44 dev->bus ? dev->bus->name : "No Bus",
45 kobject_name(&dev->kobj));
46 mutex_lock(&dpm_list_mtx);
47 list_add_tail(&dev->power.entry, &dpm_active);
48 mutex_unlock(&dpm_list_mtx);
49}
50
51void device_pm_remove(struct device *dev)
52{
53 pr_debug("PM: Removing info for %s:%s\n",
54 dev->bus ? dev->bus->name : "No Bus",
55 kobject_name(&dev->kobj));
56 mutex_lock(&dpm_list_mtx);
57 dpm_sysfs_remove(dev);
58 list_del_init(&dev->power.entry);
59 mutex_unlock(&dpm_list_mtx);
60}
61
62
63
64
65
66
67
68
69
70
71static int resume_device(struct device * dev)
72{
73 int error = 0;
74
75 TRACE_DEVICE(dev);
76 TRACE_RESUME(0);
77
78 down(&dev->sem);
79
80 if (dev->bus && dev->bus->resume) {
81 dev_dbg(dev,"resuming\n");
82 error = dev->bus->resume(dev);
83 }
84
85 if (!error && dev->type && dev->type->resume) {
86 dev_dbg(dev,"resuming\n");
87 error = dev->type->resume(dev);
88 }
89
90 if (!error && dev->class && dev->class->resume) {
91 dev_dbg(dev,"class resume\n");
92 error = dev->class->resume(dev);
93 }
94
95 up(&dev->sem);
96
97 TRACE_RESUME(error);
98 return error;
99}
100
101
102static int resume_device_early(struct device * dev)
103{
104 int error = 0;
105
106 TRACE_DEVICE(dev);
107 TRACE_RESUME(0);
108 if (dev->bus && dev->bus->resume_early) {
109 dev_dbg(dev,"EARLY resume\n");
110 error = dev->bus->resume_early(dev);
111 }
112 TRACE_RESUME(error);
113 return error;
114}
115
116
117
118
119
120
121static void dpm_resume(void)
122{
123 mutex_lock(&dpm_list_mtx);
124 while(!list_empty(&dpm_off)) {
125 struct list_head * entry = dpm_off.next;
126 struct device * dev = to_device(entry);
127
128 get_device(dev);
129 list_move_tail(entry, &dpm_active);
130
131 mutex_unlock(&dpm_list_mtx);
132 resume_device(dev);
133 mutex_lock(&dpm_list_mtx);
134 put_device(dev);
135 }
136 mutex_unlock(&dpm_list_mtx);
137}
138
139
140
141
142
143
144
145
146
147void device_resume(void)
148{
149 might_sleep();
150 mutex_lock(&dpm_mtx);
151 dpm_resume();
152 mutex_unlock(&dpm_mtx);
153}
154
155EXPORT_SYMBOL_GPL(device_resume);
156
157
158
159
160
161
162
163
164
165
166
167
168
169static void dpm_power_up(void)
170{
171 while(!list_empty(&dpm_off_irq)) {
172 struct list_head * entry = dpm_off_irq.next;
173 struct device * dev = to_device(entry);
174
175 list_move_tail(entry, &dpm_off);
176 resume_device_early(dev);
177 }
178}
179
180
181
182
183
184
185
186
187
188
189void device_power_up(void)
190{
191 sysdev_resume();
192 dpm_power_up();
193}
194
195EXPORT_SYMBOL_GPL(device_power_up);
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215static inline char *suspend_verb(u32 event)
216{
217 switch (event) {
218 case PM_EVENT_SUSPEND: return "suspend";
219 case PM_EVENT_FREEZE: return "freeze";
220 case PM_EVENT_PRETHAW: return "prethaw";
221 default: return "(unknown suspend event)";
222 }
223}
224
225
226static void
227suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
228{
229 dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
230 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
231 ", may wakeup" : "");
232}
233
234
235
236
237
238
239
240static int suspend_device(struct device * dev, pm_message_t state)
241{
242 int error = 0;
243
244 down(&dev->sem);
245 if (dev->power.power_state.event) {
246 dev_dbg(dev, "PM: suspend %d-->%d\n",
247 dev->power.power_state.event, state.event);
248 }
249
250 if (dev->class && dev->class->suspend) {
251 suspend_device_dbg(dev, state, "class ");
252 error = dev->class->suspend(dev, state);
253 suspend_report_result(dev->class->suspend, error);
254 }
255
256 if (!error && dev->type && dev->type->suspend) {
257 suspend_device_dbg(dev, state, "type ");
258 error = dev->type->suspend(dev, state);
259 suspend_report_result(dev->type->suspend, error);
260 }
261
262 if (!error && dev->bus && dev->bus->suspend) {
263 suspend_device_dbg(dev, state, "");
264 error = dev->bus->suspend(dev, state);
265 suspend_report_result(dev->bus->suspend, error);
266 }
267 up(&dev->sem);
268 return error;
269}
270
271
272
273
274
275
276
277static int suspend_device_late(struct device *dev, pm_message_t state)
278{
279 int error = 0;
280
281 if (dev->bus && dev->bus->suspend_late) {
282 suspend_device_dbg(dev, state, "LATE ");
283 error = dev->bus->suspend_late(dev, state);
284 suspend_report_result(dev->bus->suspend_late, error);
285 }
286 return error;
287}
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308int device_suspend(pm_message_t state)
309{
310 int error = 0;
311
312 might_sleep();
313 mutex_lock(&dpm_mtx);
314 mutex_lock(&dpm_list_mtx);
315 while (!list_empty(&dpm_active) && error == 0) {
316 struct list_head * entry = dpm_active.prev;
317 struct device * dev = to_device(entry);
318
319 get_device(dev);
320 mutex_unlock(&dpm_list_mtx);
321
322 error = suspend_device(dev, state);
323
324 mutex_lock(&dpm_list_mtx);
325
326
327 if (!list_empty(&dev->power.entry)) {
328
329 if (!error)
330 list_move(&dev->power.entry, &dpm_off);
331 }
332 if (error)
333 printk(KERN_ERR "Could not suspend device %s: "
334 "error %d%s\n",
335 kobject_name(&dev->kobj), error,
336 error == -EAGAIN ? " (please convert to suspend_late)" : "");
337 put_device(dev);
338 }
339 mutex_unlock(&dpm_list_mtx);
340 if (error)
341 dpm_resume();
342
343 mutex_unlock(&dpm_mtx);
344 return error;
345}
346
347EXPORT_SYMBOL_GPL(device_suspend);
348
349
350
351
352
353
354
355
356
357
358int device_power_down(pm_message_t state)
359{
360 int error = 0;
361 struct device * dev;
362
363 while (!list_empty(&dpm_off)) {
364 struct list_head * entry = dpm_off.prev;
365
366 dev = to_device(entry);
367 error = suspend_device_late(dev, state);
368 if (error)
369 goto Error;
370 list_move(&dev->power.entry, &dpm_off_irq);
371 }
372
373 error = sysdev_suspend(state);
374 Done:
375 return error;
376 Error:
377 printk(KERN_ERR "Could not power down device %s: "
378 "error %d\n", kobject_name(&dev->kobj), error);
379 dpm_power_up();
380 goto Done;
381}
382
383EXPORT_SYMBOL_GPL(device_power_down);
384
385void __suspend_report_result(const char *function, void *fn, int ret)
386{
387 if (ret) {
388 printk(KERN_ERR "%s(): ", function);
389 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
390 printk("%d\n", ret);
391 }
392}
393EXPORT_SYMBOL_GPL(__suspend_report_result);
394