| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/device.h> | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 23 | #include <linux/kallsyms.h> | 
| Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 24 | #include <linux/mutex.h> | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 25 | #include <linux/pm.h> | 
 | 26 | #include <linux/resume-trace.h> | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 27 | #include <linux/rwsem.h> | 
| Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 28 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 29 | #include "../base.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "power.h" | 
 | 31 |  | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | LIST_HEAD(dpm_active); | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 51 | static LIST_HEAD(dpm_off); | 
 | 52 | static LIST_HEAD(dpm_off_irq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 54 | static DEFINE_MUTEX(dpm_list_mtx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 56 | /* 'true' if all devices have been suspended, protected by dpm_list_mtx */ | 
 | 57 | static bool all_sleeping; | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 58 |  | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 59 | /** | 
 | 60 |  *	device_pm_add - add a device to the list of active devices | 
 | 61 |  *	@dev:	Device to be added to the list | 
 | 62 |  */ | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 63 | int device_pm_add(struct device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { | 
| Rafael J. Wysocki | 08119e8 | 2008-04-23 00:48:23 +0200 | [diff] [blame] | 65 | 	int error; | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 66 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | 	pr_debug("PM: Adding info for %s:%s\n", | 
| Dmitry Torokhov | c48ea60 | 2007-04-11 01:37:18 -0400 | [diff] [blame] | 68 | 		 dev->bus ? dev->bus->name : "No Bus", | 
 | 69 | 		 kobject_name(&dev->kobj)); | 
| Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 70 | 	mutex_lock(&dpm_list_mtx); | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 71 | 	if ((dev->parent && dev->parent->power.sleeping) || all_sleeping) { | 
 | 72 | 		if (dev->parent->power.sleeping) | 
| Rafael J. Wysocki | 08119e8 | 2008-04-23 00:48:23 +0200 | [diff] [blame] | 73 | 			dev_warn(dev, "parent %s is sleeping\n", | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 74 | 				dev->parent->bus_id); | 
 | 75 | 		else | 
| Rafael J. Wysocki | 08119e8 | 2008-04-23 00:48:23 +0200 | [diff] [blame] | 76 | 			dev_warn(dev, "all devices are sleeping\n"); | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 77 | 		WARN_ON(true); | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 78 | 	} | 
| Rafael J. Wysocki | 08119e8 | 2008-04-23 00:48:23 +0200 | [diff] [blame] | 79 | 	error = dpm_sysfs_add(dev); | 
 | 80 | 	if (!error) | 
 | 81 | 		list_add_tail(&dev->power.entry, &dpm_active); | 
| Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 82 | 	mutex_unlock(&dpm_list_mtx); | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 83 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } | 
 | 85 |  | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 86 | /** | 
 | 87 |  *	device_pm_remove - remove a device from the list of active devices | 
 | 88 |  *	@dev:	Device to be removed from the list | 
 | 89 |  * | 
 | 90 |  *	This function also removes the device's PM-related sysfs attributes. | 
 | 91 |  */ | 
| Rafael J. Wysocki | 9cddad7 | 2007-06-13 15:53:34 +0200 | [diff] [blame] | 92 | void device_pm_remove(struct device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { | 
 | 94 | 	pr_debug("PM: Removing info for %s:%s\n", | 
| Dmitry Torokhov | c48ea60 | 2007-04-11 01:37:18 -0400 | [diff] [blame] | 95 | 		 dev->bus ? dev->bus->name : "No Bus", | 
 | 96 | 		 kobject_name(&dev->kobj)); | 
| Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 97 | 	mutex_lock(&dpm_list_mtx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | 	dpm_sysfs_remove(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | 	list_del_init(&dev->power.entry); | 
| Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 100 | 	mutex_unlock(&dpm_list_mtx); | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 101 | } | 
 | 102 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 103 | /*------------------------- Resume routines -------------------------*/ | 
 | 104 |  | 
 | 105 | /** | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 106 |  *	resume_device_early - Power on one device (early resume). | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 107 |  *	@dev:	Device. | 
 | 108 |  * | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 109 |  *	Must be called with interrupts disabled. | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 110 |  */ | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 111 | static int resume_device_early(struct device *dev) | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 112 | { | 
 | 113 | 	int error = 0; | 
 | 114 |  | 
 | 115 | 	TRACE_DEVICE(dev); | 
 | 116 | 	TRACE_RESUME(0); | 
 | 117 |  | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 118 | 	if (dev->bus && dev->bus->resume_early) { | 
 | 119 | 		dev_dbg(dev, "EARLY resume\n"); | 
 | 120 | 		error = dev->bus->resume_early(dev); | 
 | 121 | 	} | 
 | 122 |  | 
 | 123 | 	TRACE_RESUME(error); | 
 | 124 | 	return error; | 
 | 125 | } | 
 | 126 |  | 
 | 127 | /** | 
 | 128 |  *	dpm_power_up - Power on all regular (non-sysdev) devices. | 
 | 129 |  * | 
 | 130 |  *	Walk the dpm_off_irq list and power each device up. This | 
 | 131 |  *	is used for devices that required they be powered down with | 
 | 132 |  *	interrupts disabled. As devices are powered on, they are moved | 
 | 133 |  *	to the dpm_off list. | 
 | 134 |  * | 
 | 135 |  *	Must be called with interrupts disabled and only one CPU running. | 
 | 136 |  */ | 
 | 137 | static void dpm_power_up(void) | 
 | 138 | { | 
 | 139 |  | 
 | 140 | 	while (!list_empty(&dpm_off_irq)) { | 
 | 141 | 		struct list_head *entry = dpm_off_irq.next; | 
 | 142 | 		struct device *dev = to_device(entry); | 
 | 143 |  | 
 | 144 | 		list_move_tail(entry, &dpm_off); | 
 | 145 | 		resume_device_early(dev); | 
 | 146 | 	} | 
 | 147 | } | 
 | 148 |  | 
 | 149 | /** | 
 | 150 |  *	device_power_up - Turn on all devices that need special attention. | 
 | 151 |  * | 
 | 152 |  *	Power on system devices, then devices that required we shut them down | 
 | 153 |  *	with interrupts disabled. | 
 | 154 |  * | 
 | 155 |  *	Must be called with interrupts disabled. | 
 | 156 |  */ | 
 | 157 | void device_power_up(void) | 
 | 158 | { | 
 | 159 | 	sysdev_resume(); | 
 | 160 | 	dpm_power_up(); | 
 | 161 | } | 
 | 162 | EXPORT_SYMBOL_GPL(device_power_up); | 
 | 163 |  | 
 | 164 | /** | 
 | 165 |  *	resume_device - Restore state for one device. | 
 | 166 |  *	@dev:	Device. | 
 | 167 |  * | 
 | 168 |  */ | 
 | 169 | static int resume_device(struct device *dev) | 
 | 170 | { | 
 | 171 | 	int error = 0; | 
 | 172 |  | 
 | 173 | 	TRACE_DEVICE(dev); | 
 | 174 | 	TRACE_RESUME(0); | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 175 |  | 
| Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 176 | 	down(&dev->sem); | 
 | 177 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 178 | 	if (dev->bus && dev->bus->resume) { | 
 | 179 | 		dev_dbg(dev,"resuming\n"); | 
 | 180 | 		error = dev->bus->resume(dev); | 
 | 181 | 	} | 
 | 182 |  | 
 | 183 | 	if (!error && dev->type && dev->type->resume) { | 
 | 184 | 		dev_dbg(dev,"resuming\n"); | 
 | 185 | 		error = dev->type->resume(dev); | 
 | 186 | 	} | 
 | 187 |  | 
 | 188 | 	if (!error && dev->class && dev->class->resume) { | 
 | 189 | 		dev_dbg(dev,"class resume\n"); | 
 | 190 | 		error = dev->class->resume(dev); | 
 | 191 | 	} | 
 | 192 |  | 
| Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 193 | 	up(&dev->sem); | 
 | 194 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 195 | 	TRACE_RESUME(error); | 
 | 196 | 	return error; | 
 | 197 | } | 
 | 198 |  | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 199 | /** | 
 | 200 |  *	dpm_resume - Resume every device. | 
 | 201 |  * | 
 | 202 |  *	Resume the devices that have either not gone through | 
 | 203 |  *	the late suspend, or that did go through it but also | 
 | 204 |  *	went through the early resume. | 
 | 205 |  * | 
 | 206 |  *	Take devices from the dpm_off_list, resume them, | 
 | 207 |  *	and put them on the dpm_locked list. | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 208 |  */ | 
 | 209 | static void dpm_resume(void) | 
 | 210 | { | 
 | 211 | 	mutex_lock(&dpm_list_mtx); | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 212 | 	all_sleeping = false; | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 213 | 	while(!list_empty(&dpm_off)) { | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 214 | 		struct list_head *entry = dpm_off.next; | 
 | 215 | 		struct device *dev = to_device(entry); | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 216 |  | 
| Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 217 | 		list_move_tail(entry, &dpm_active); | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 218 | 		dev->power.sleeping = false; | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 219 | 		mutex_unlock(&dpm_list_mtx); | 
 | 220 | 		resume_device(dev); | 
 | 221 | 		mutex_lock(&dpm_list_mtx); | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 222 | 	} | 
 | 223 | 	mutex_unlock(&dpm_list_mtx); | 
 | 224 | } | 
 | 225 |  | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 226 | /** | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 227 |  *	device_resume - Restore state of each device in system. | 
 | 228 |  * | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 229 |  *	Resume all the devices, unlock them all, and allow new | 
 | 230 |  *	devices to be registered once again. | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 231 |  */ | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 232 | void device_resume(void) | 
 | 233 | { | 
 | 234 | 	might_sleep(); | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 235 | 	dpm_resume(); | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 236 | } | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 237 | EXPORT_SYMBOL_GPL(device_resume); | 
 | 238 |  | 
 | 239 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 240 | /*------------------------- Suspend routines -------------------------*/ | 
 | 241 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 242 | static inline char *suspend_verb(u32 event) | 
 | 243 | { | 
 | 244 | 	switch (event) { | 
 | 245 | 	case PM_EVENT_SUSPEND:	return "suspend"; | 
 | 246 | 	case PM_EVENT_FREEZE:	return "freeze"; | 
 | 247 | 	case PM_EVENT_PRETHAW:	return "prethaw"; | 
 | 248 | 	default:		return "(unknown suspend event)"; | 
 | 249 | 	} | 
 | 250 | } | 
 | 251 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 252 | static void | 
 | 253 | suspend_device_dbg(struct device *dev, pm_message_t state, char *info) | 
 | 254 | { | 
 | 255 | 	dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event), | 
 | 256 | 		((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ? | 
 | 257 | 		", may wakeup" : ""); | 
 | 258 | } | 
 | 259 |  | 
 | 260 | /** | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 261 |  *	suspend_device_late - Shut down one device (late suspend). | 
 | 262 |  *	@dev:	Device. | 
 | 263 |  *	@state:	Power state device is entering. | 
 | 264 |  * | 
 | 265 |  *	This is called with interrupts off and only a single CPU running. | 
 | 266 |  */ | 
 | 267 | static int suspend_device_late(struct device *dev, pm_message_t state) | 
 | 268 | { | 
 | 269 | 	int error = 0; | 
 | 270 |  | 
 | 271 | 	if (dev->bus && dev->bus->suspend_late) { | 
 | 272 | 		suspend_device_dbg(dev, state, "LATE "); | 
 | 273 | 		error = dev->bus->suspend_late(dev, state); | 
 | 274 | 		suspend_report_result(dev->bus->suspend_late, error); | 
 | 275 | 	} | 
 | 276 | 	return error; | 
 | 277 | } | 
 | 278 |  | 
 | 279 | /** | 
 | 280 |  *	device_power_down - Shut down special devices. | 
 | 281 |  *	@state:		Power state to enter. | 
 | 282 |  * | 
 | 283 |  *	Power down devices that require interrupts to be disabled | 
 | 284 |  *	and move them from the dpm_off list to the dpm_off_irq list. | 
 | 285 |  *	Then power down system devices. | 
 | 286 |  * | 
 | 287 |  *	Must be called with interrupts disabled and only one CPU running. | 
 | 288 |  */ | 
 | 289 | int device_power_down(pm_message_t state) | 
 | 290 | { | 
 | 291 | 	int error = 0; | 
 | 292 |  | 
 | 293 | 	while (!list_empty(&dpm_off)) { | 
 | 294 | 		struct list_head *entry = dpm_off.prev; | 
 | 295 | 		struct device *dev = to_device(entry); | 
 | 296 |  | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 297 | 		error = suspend_device_late(dev, state); | 
 | 298 | 		if (error) { | 
 | 299 | 			printk(KERN_ERR "Could not power down device %s: " | 
 | 300 | 					"error %d\n", | 
 | 301 | 					kobject_name(&dev->kobj), error); | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 302 | 			break; | 
 | 303 | 		} | 
| Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 304 | 		if (!list_empty(&dev->power.entry)) | 
 | 305 | 			list_move(&dev->power.entry, &dpm_off_irq); | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 306 | 	} | 
 | 307 |  | 
 | 308 | 	if (!error) | 
 | 309 | 		error = sysdev_suspend(state); | 
 | 310 | 	if (error) | 
 | 311 | 		dpm_power_up(); | 
 | 312 | 	return error; | 
 | 313 | } | 
 | 314 | EXPORT_SYMBOL_GPL(device_power_down); | 
 | 315 |  | 
 | 316 | /** | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 317 |  *	suspend_device - Save state of one device. | 
 | 318 |  *	@dev:	Device. | 
 | 319 |  *	@state:	Power state device is entering. | 
 | 320 |  */ | 
| Adrian Bunk | 19e20c9 | 2008-02-03 22:55:18 +0100 | [diff] [blame] | 321 | static int suspend_device(struct device *dev, pm_message_t state) | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 322 | { | 
 | 323 | 	int error = 0; | 
 | 324 |  | 
| Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 325 | 	down(&dev->sem); | 
 | 326 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 327 | 	if (dev->class && dev->class->suspend) { | 
 | 328 | 		suspend_device_dbg(dev, state, "class "); | 
 | 329 | 		error = dev->class->suspend(dev, state); | 
 | 330 | 		suspend_report_result(dev->class->suspend, error); | 
 | 331 | 	} | 
 | 332 |  | 
 | 333 | 	if (!error && dev->type && dev->type->suspend) { | 
 | 334 | 		suspend_device_dbg(dev, state, "type "); | 
 | 335 | 		error = dev->type->suspend(dev, state); | 
 | 336 | 		suspend_report_result(dev->type->suspend, error); | 
 | 337 | 	} | 
 | 338 |  | 
 | 339 | 	if (!error && dev->bus && dev->bus->suspend) { | 
 | 340 | 		suspend_device_dbg(dev, state, ""); | 
 | 341 | 		error = dev->bus->suspend(dev, state); | 
 | 342 | 		suspend_report_result(dev->bus->suspend, error); | 
 | 343 | 	} | 
| Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 344 |  | 
 | 345 | 	up(&dev->sem); | 
 | 346 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 347 | 	return error; | 
 | 348 | } | 
 | 349 |  | 
 | 350 | /** | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 351 |  *	dpm_suspend - Suspend every device. | 
 | 352 |  *	@state:	Power state to put each device in. | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 353 |  * | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 354 |  *	Walk the dpm_locked list.  Suspend each device and move it | 
 | 355 |  *	to the dpm_off list. | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 356 |  * | 
 | 357 |  *	(For historical reasons, if it returns -EAGAIN, that used to mean | 
 | 358 |  *	that the device would be called again with interrupts disabled. | 
 | 359 |  *	These days, we use the "suspend_late()" callback for that, so we | 
 | 360 |  *	print a warning and consider it an error). | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 361 |  */ | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 362 | static int dpm_suspend(pm_message_t state) | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 363 | { | 
 | 364 | 	int error = 0; | 
 | 365 |  | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 366 | 	mutex_lock(&dpm_list_mtx); | 
| Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 367 | 	while (!list_empty(&dpm_active)) { | 
 | 368 | 		struct list_head *entry = dpm_active.prev; | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 369 | 		struct device *dev = to_device(entry); | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 370 |  | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 371 | 		WARN_ON(dev->parent && dev->parent->power.sleeping); | 
 | 372 |  | 
 | 373 | 		dev->power.sleeping = true; | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 374 | 		mutex_unlock(&dpm_list_mtx); | 
 | 375 | 		error = suspend_device(dev, state); | 
| Alan Stern | 1b3cbec | 2008-02-29 11:50:22 -0500 | [diff] [blame] | 376 | 		mutex_lock(&dpm_list_mtx); | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 377 | 		if (error) { | 
 | 378 | 			printk(KERN_ERR "Could not suspend device %s: " | 
 | 379 | 					"error %d%s\n", | 
 | 380 | 					kobject_name(&dev->kobj), | 
 | 381 | 					error, | 
 | 382 | 					(error == -EAGAIN ? | 
 | 383 | 					" (please convert to suspend_late)" : | 
 | 384 | 					"")); | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 385 | 			dev->power.sleeping = false; | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 386 | 			break; | 
 | 387 | 		} | 
| Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 388 | 		if (!list_empty(&dev->power.entry)) | 
 | 389 | 			list_move(&dev->power.entry, &dpm_off); | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 390 | 	} | 
| Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 391 | 	if (!error) | 
 | 392 | 		all_sleeping = true; | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 393 | 	mutex_unlock(&dpm_list_mtx); | 
 | 394 |  | 
 | 395 | 	return error; | 
 | 396 | } | 
 | 397 |  | 
 | 398 | /** | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 399 |  *	device_suspend - Save state and stop all devices in system. | 
| Randy Dunlap | 7199677 | 2008-02-18 13:09:03 -0800 | [diff] [blame] | 400 |  *	@state: new power management state | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 401 |  * | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 402 |  *	Prevent new devices from being registered, then lock all devices | 
 | 403 |  *	and suspend them. | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 404 |  */ | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 405 | int device_suspend(pm_message_t state) | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 406 | { | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 407 | 	int error; | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 408 |  | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 409 | 	might_sleep(); | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 410 | 	error = dpm_suspend(state); | 
 | 411 | 	if (error) | 
 | 412 | 		device_resume(); | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 413 | 	return error; | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 414 | } | 
| Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 415 | EXPORT_SYMBOL_GPL(device_suspend); | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 416 |  | 
 | 417 | void __suspend_report_result(const char *function, void *fn, int ret) | 
 | 418 | { | 
 | 419 | 	if (ret) { | 
 | 420 | 		printk(KERN_ERR "%s(): ", function); | 
| Linus Torvalds | a442ac5 | 2008-05-15 17:50:37 -0700 | [diff] [blame] | 421 | 		print_fn_descriptor_symbol("%s returns ", fn); | 
| Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 422 | 		printk("%d\n", ret); | 
 | 423 | 	} | 
 | 424 | } | 
 | 425 | EXPORT_SYMBOL_GPL(__suspend_report_result); |