5630af302b2fc154862235ed03d0f24feabb02a2
[safe/jmp/linux-2.6] / drivers / base / power / main.c
1 /*
2  * drivers/base/power/main.c - Where the driver meets power management.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  *
7  * This file is released under the GPLv2
8  *
9  *
10  * The driver model core calls device_pm_add() when a device is registered.
11  * This will intialize the embedded device_pm_info object in the device
12  * and add it to the list of power-controlled devices. sysfs entries for
13  * controlling device power management will also be added.
14  *
15  * A different set of lists than the global subsystem list are used to
16  * keep track of power info because we use different lists to hold
17  * devices based on what stage of the power management process they
18  * are in. The power domain dependencies may also differ from the
19  * ancestral dependencies that the subsystem list maintains.
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 #include <linux/rwsem.h>
28
29 #include "../base.h"
30 #include "power.h"
31
32 /*
33  * The entries in the dpm_active list are in a depth first order, simply
34  * because children are guaranteed to be discovered after parents, and
35  * are inserted at the back of the list on discovery.
36  *
37  * All the other lists are kept in the same order, for consistency.
38  * However the lists aren't always traversed in the same order.
39  * Semaphores must be acquired from the top (i.e., front) down
40  * and released in the opposite order.  Devices must be suspended
41  * from the bottom (i.e., end) up and resumed in the opposite order.
42  * That way no parent will be suspended while it still has an active
43  * child.
44  *
45  * Since device_pm_add() may be called with a device semaphore held,
46  * we must never try to acquire a device semaphore while holding
47  * dpm_list_mutex.
48  */
49
50 LIST_HEAD(dpm_active);
51 static LIST_HEAD(dpm_off);
52 static LIST_HEAD(dpm_off_irq);
53 static LIST_HEAD(dpm_destroy);
54
55 static DEFINE_MUTEX(dpm_list_mtx);
56
57 /* 'true' if all devices have been suspended, protected by dpm_list_mtx */
58 static bool all_sleeping;
59
60 /**
61  *      device_pm_add - add a device to the list of active devices
62  *      @dev:   Device to be added to the list
63  */
64 int device_pm_add(struct device *dev)
65 {
66         int error = 0;
67
68         pr_debug("PM: Adding info for %s:%s\n",
69                  dev->bus ? dev->bus->name : "No Bus",
70                  kobject_name(&dev->kobj));
71         mutex_lock(&dpm_list_mtx);
72         if ((dev->parent && dev->parent->power.sleeping) || all_sleeping) {
73                 if (dev->parent->power.sleeping)
74                         dev_warn(dev,
75                                 "parent %s is sleeping, will not add\n",
76                                 dev->parent->bus_id);
77                 else
78                         dev_warn(dev, "devices are sleeping, will not add\n");
79                 WARN_ON(true);
80                 error = -EBUSY;
81         } else {
82                 error = dpm_sysfs_add(dev);
83                 if (!error)
84                         list_add_tail(&dev->power.entry, &dpm_active);
85         }
86         mutex_unlock(&dpm_list_mtx);
87         return error;
88 }
89
90 /**
91  *      device_pm_remove - remove a device from the list of active devices
92  *      @dev:   Device to be removed from the list
93  *
94  *      This function also removes the device's PM-related sysfs attributes.
95  */
96 void device_pm_remove(struct device *dev)
97 {
98         pr_debug("PM: Removing info for %s:%s\n",
99                  dev->bus ? dev->bus->name : "No Bus",
100                  kobject_name(&dev->kobj));
101         mutex_lock(&dpm_list_mtx);
102         dpm_sysfs_remove(dev);
103         list_del_init(&dev->power.entry);
104         mutex_unlock(&dpm_list_mtx);
105 }
106
107 /**
108  *      device_pm_schedule_removal - schedule the removal of a suspended device
109  *      @dev:   Device to destroy
110  *
111  *      Moves the device to the dpm_destroy list for further processing by
112  *      unregister_dropped_devices().
113  */
114 void device_pm_schedule_removal(struct device *dev)
115 {
116         pr_debug("PM: Preparing for removal: %s:%s\n",
117                 dev->bus ? dev->bus->name : "No Bus",
118                 kobject_name(&dev->kobj));
119         mutex_lock(&dpm_list_mtx);
120         list_move_tail(&dev->power.entry, &dpm_destroy);
121         mutex_unlock(&dpm_list_mtx);
122 }
123 EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
124
125 /*------------------------- Resume routines -------------------------*/
126
127 /**
128  *      resume_device_early - Power on one device (early resume).
129  *      @dev:   Device.
130  *
131  *      Must be called with interrupts disabled.
132  */
133 static int resume_device_early(struct device *dev)
134 {
135         int error = 0;
136
137         TRACE_DEVICE(dev);
138         TRACE_RESUME(0);
139
140         if (dev->bus && dev->bus->resume_early) {
141                 dev_dbg(dev, "EARLY resume\n");
142                 error = dev->bus->resume_early(dev);
143         }
144
145         TRACE_RESUME(error);
146         return error;
147 }
148
149 /**
150  *      dpm_power_up - Power on all regular (non-sysdev) devices.
151  *
152  *      Walk the dpm_off_irq list and power each device up. This
153  *      is used for devices that required they be powered down with
154  *      interrupts disabled. As devices are powered on, they are moved
155  *      to the dpm_off list.
156  *
157  *      Must be called with interrupts disabled and only one CPU running.
158  */
159 static void dpm_power_up(void)
160 {
161
162         while (!list_empty(&dpm_off_irq)) {
163                 struct list_head *entry = dpm_off_irq.next;
164                 struct device *dev = to_device(entry);
165
166                 list_move_tail(entry, &dpm_off);
167                 resume_device_early(dev);
168         }
169 }
170
171 /**
172  *      device_power_up - Turn on all devices that need special attention.
173  *
174  *      Power on system devices, then devices that required we shut them down
175  *      with interrupts disabled.
176  *
177  *      Must be called with interrupts disabled.
178  */
179 void device_power_up(void)
180 {
181         sysdev_resume();
182         dpm_power_up();
183 }
184 EXPORT_SYMBOL_GPL(device_power_up);
185
186 /**
187  *      resume_device - Restore state for one device.
188  *      @dev:   Device.
189  *
190  */
191 static int resume_device(struct device *dev)
192 {
193         int error = 0;
194
195         TRACE_DEVICE(dev);
196         TRACE_RESUME(0);
197
198         down(&dev->sem);
199
200         if (dev->bus && dev->bus->resume) {
201                 dev_dbg(dev,"resuming\n");
202                 error = dev->bus->resume(dev);
203         }
204
205         if (!error && dev->type && dev->type->resume) {
206                 dev_dbg(dev,"resuming\n");
207                 error = dev->type->resume(dev);
208         }
209
210         if (!error && dev->class && dev->class->resume) {
211                 dev_dbg(dev,"class resume\n");
212                 error = dev->class->resume(dev);
213         }
214
215         up(&dev->sem);
216
217         TRACE_RESUME(error);
218         return error;
219 }
220
221 /**
222  *      dpm_resume - Resume every device.
223  *
224  *      Resume the devices that have either not gone through
225  *      the late suspend, or that did go through it but also
226  *      went through the early resume.
227  *
228  *      Take devices from the dpm_off_list, resume them,
229  *      and put them on the dpm_locked list.
230  */
231 static void dpm_resume(void)
232 {
233         mutex_lock(&dpm_list_mtx);
234         all_sleeping = false;
235         while(!list_empty(&dpm_off)) {
236                 struct list_head *entry = dpm_off.next;
237                 struct device *dev = to_device(entry);
238
239                 list_move_tail(entry, &dpm_active);
240                 dev->power.sleeping = false;
241                 mutex_unlock(&dpm_list_mtx);
242                 resume_device(dev);
243                 mutex_lock(&dpm_list_mtx);
244         }
245         mutex_unlock(&dpm_list_mtx);
246 }
247
248 /**
249  *      unregister_dropped_devices - Unregister devices scheduled for removal
250  *
251  *      Unregister all devices on the dpm_destroy list.
252  */
253 static void unregister_dropped_devices(void)
254 {
255         mutex_lock(&dpm_list_mtx);
256         while (!list_empty(&dpm_destroy)) {
257                 struct list_head *entry = dpm_destroy.next;
258                 struct device *dev = to_device(entry);
259
260                 mutex_unlock(&dpm_list_mtx);
261                 /* This also removes the device from the list */
262                 device_unregister(dev);
263                 mutex_lock(&dpm_list_mtx);
264         }
265         mutex_unlock(&dpm_list_mtx);
266 }
267
268 /**
269  *      device_resume - Restore state of each device in system.
270  *
271  *      Resume all the devices, unlock them all, and allow new
272  *      devices to be registered once again.
273  */
274 void device_resume(void)
275 {
276         might_sleep();
277         dpm_resume();
278         unregister_dropped_devices();
279 }
280 EXPORT_SYMBOL_GPL(device_resume);
281
282
283 /*------------------------- Suspend routines -------------------------*/
284
285 static inline char *suspend_verb(u32 event)
286 {
287         switch (event) {
288         case PM_EVENT_SUSPEND:  return "suspend";
289         case PM_EVENT_FREEZE:   return "freeze";
290         case PM_EVENT_PRETHAW:  return "prethaw";
291         default:                return "(unknown suspend event)";
292         }
293 }
294
295 static void
296 suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
297 {
298         dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
299                 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
300                 ", may wakeup" : "");
301 }
302
303 /**
304  *      suspend_device_late - Shut down one device (late suspend).
305  *      @dev:   Device.
306  *      @state: Power state device is entering.
307  *
308  *      This is called with interrupts off and only a single CPU running.
309  */
310 static int suspend_device_late(struct device *dev, pm_message_t state)
311 {
312         int error = 0;
313
314         if (dev->bus && dev->bus->suspend_late) {
315                 suspend_device_dbg(dev, state, "LATE ");
316                 error = dev->bus->suspend_late(dev, state);
317                 suspend_report_result(dev->bus->suspend_late, error);
318         }
319         return error;
320 }
321
322 /**
323  *      device_power_down - Shut down special devices.
324  *      @state:         Power state to enter.
325  *
326  *      Power down devices that require interrupts to be disabled
327  *      and move them from the dpm_off list to the dpm_off_irq list.
328  *      Then power down system devices.
329  *
330  *      Must be called with interrupts disabled and only one CPU running.
331  */
332 int device_power_down(pm_message_t state)
333 {
334         int error = 0;
335
336         while (!list_empty(&dpm_off)) {
337                 struct list_head *entry = dpm_off.prev;
338                 struct device *dev = to_device(entry);
339
340                 error = suspend_device_late(dev, state);
341                 if (error) {
342                         printk(KERN_ERR "Could not power down device %s: "
343                                         "error %d\n",
344                                         kobject_name(&dev->kobj), error);
345                         break;
346                 }
347                 if (!list_empty(&dev->power.entry))
348                         list_move(&dev->power.entry, &dpm_off_irq);
349         }
350
351         if (!error)
352                 error = sysdev_suspend(state);
353         if (error)
354                 dpm_power_up();
355         return error;
356 }
357 EXPORT_SYMBOL_GPL(device_power_down);
358
359 /**
360  *      suspend_device - Save state of one device.
361  *      @dev:   Device.
362  *      @state: Power state device is entering.
363  */
364 static int suspend_device(struct device *dev, pm_message_t state)
365 {
366         int error = 0;
367
368         down(&dev->sem);
369
370         if (dev->class && dev->class->suspend) {
371                 suspend_device_dbg(dev, state, "class ");
372                 error = dev->class->suspend(dev, state);
373                 suspend_report_result(dev->class->suspend, error);
374         }
375
376         if (!error && dev->type && dev->type->suspend) {
377                 suspend_device_dbg(dev, state, "type ");
378                 error = dev->type->suspend(dev, state);
379                 suspend_report_result(dev->type->suspend, error);
380         }
381
382         if (!error && dev->bus && dev->bus->suspend) {
383                 suspend_device_dbg(dev, state, "");
384                 error = dev->bus->suspend(dev, state);
385                 suspend_report_result(dev->bus->suspend, error);
386         }
387
388         up(&dev->sem);
389
390         return error;
391 }
392
393 /**
394  *      dpm_suspend - Suspend every device.
395  *      @state: Power state to put each device in.
396  *
397  *      Walk the dpm_locked list.  Suspend each device and move it
398  *      to the dpm_off list.
399  *
400  *      (For historical reasons, if it returns -EAGAIN, that used to mean
401  *      that the device would be called again with interrupts disabled.
402  *      These days, we use the "suspend_late()" callback for that, so we
403  *      print a warning and consider it an error).
404  */
405 static int dpm_suspend(pm_message_t state)
406 {
407         int error = 0;
408
409         mutex_lock(&dpm_list_mtx);
410         while (!list_empty(&dpm_active)) {
411                 struct list_head *entry = dpm_active.prev;
412                 struct device *dev = to_device(entry);
413
414                 WARN_ON(dev->parent && dev->parent->power.sleeping);
415
416                 dev->power.sleeping = true;
417                 mutex_unlock(&dpm_list_mtx);
418                 error = suspend_device(dev, state);
419                 mutex_lock(&dpm_list_mtx);
420                 if (error) {
421                         printk(KERN_ERR "Could not suspend device %s: "
422                                         "error %d%s\n",
423                                         kobject_name(&dev->kobj),
424                                         error,
425                                         (error == -EAGAIN ?
426                                         " (please convert to suspend_late)" :
427                                         ""));
428                         dev->power.sleeping = false;
429                         break;
430                 }
431                 if (!list_empty(&dev->power.entry))
432                         list_move(&dev->power.entry, &dpm_off);
433         }
434         if (!error)
435                 all_sleeping = true;
436         mutex_unlock(&dpm_list_mtx);
437
438         return error;
439 }
440
441 /**
442  *      device_suspend - Save state and stop all devices in system.
443  *      @state: new power management state
444  *
445  *      Prevent new devices from being registered, then lock all devices
446  *      and suspend them.
447  */
448 int device_suspend(pm_message_t state)
449 {
450         int error;
451
452         might_sleep();
453         error = dpm_suspend(state);
454         if (error)
455                 device_resume();
456         return error;
457 }
458 EXPORT_SYMBOL_GPL(device_suspend);
459
460 void __suspend_report_result(const char *function, void *fn, int ret)
461 {
462         if (ret) {
463                 printk(KERN_ERR "%s(): ", function);
464                 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
465                 printk("%d\n", ret);
466         }
467 }
468 EXPORT_SYMBOL_GPL(__suspend_report_result);