| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * kernel/power/disk.c - Suspend-to-disk support. | 
 | 3 |  * | 
 | 4 |  * Copyright (c) 2003 Patrick Mochel | 
 | 5 |  * Copyright (c) 2003 Open Source Development Lab | 
 | 6 |  * Copyright (c) 2004 Pavel Machek <pavel@suse.cz> | 
 | 7 |  * | 
 | 8 |  * This file is released under the GPLv2. | 
 | 9 |  * | 
 | 10 |  */ | 
 | 11 |  | 
 | 12 | #include <linux/suspend.h> | 
 | 13 | #include <linux/syscalls.h> | 
 | 14 | #include <linux/reboot.h> | 
 | 15 | #include <linux/string.h> | 
 | 16 | #include <linux/device.h> | 
| Rafael J. Wysocki | 1bfcf13 | 2008-10-15 22:01:21 -0700 | [diff] [blame] | 17 | #include <linux/kmod.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/delay.h> | 
 | 19 | #include <linux/fs.h> | 
| Andrew Morton | d53d9f1 | 2005-07-12 13:58:07 -0700 | [diff] [blame] | 20 | #include <linux/mount.h> | 
| Eric W. Biederman | 88d10bb | 2005-09-22 21:43:46 -0700 | [diff] [blame] | 21 | #include <linux/pm.h> | 
| Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 22 | #include <linux/console.h> | 
| Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 23 | #include <linux/cpu.h> | 
| Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 24 | #include <linux/freezer.h> | 
| Rafael J. Wysocki | 41108eb | 2008-08-28 14:39:12 +0200 | [diff] [blame] | 25 | #include <linux/ftrace.h> | 
| Andrew Morton | d53d9f1 | 2005-07-12 13:58:07 -0700 | [diff] [blame] | 26 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "power.h" | 
 | 28 |  | 
 | 29 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static int noresume = 0; | 
| Adrian Bunk | 47a460d | 2008-02-04 22:30:06 -0800 | [diff] [blame] | 31 | static char resume_file[256] = CONFIG_PM_STD_PARTITION; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | dev_t swsusp_resume_device; | 
| Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 33 | sector_t swsusp_resume_block; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 35 | enum { | 
 | 36 | 	HIBERNATION_INVALID, | 
 | 37 | 	HIBERNATION_PLATFORM, | 
 | 38 | 	HIBERNATION_TEST, | 
 | 39 | 	HIBERNATION_TESTPROC, | 
 | 40 | 	HIBERNATION_SHUTDOWN, | 
 | 41 | 	HIBERNATION_REBOOT, | 
 | 42 | 	/* keep last */ | 
 | 43 | 	__HIBERNATION_AFTER_LAST | 
 | 44 | }; | 
 | 45 | #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1) | 
 | 46 | #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1) | 
 | 47 |  | 
 | 48 | static int hibernation_mode = HIBERNATION_SHUTDOWN; | 
 | 49 |  | 
| Rafael J. Wysocki | b3dac3b | 2007-10-18 03:04:43 -0700 | [diff] [blame] | 50 | static struct platform_hibernation_ops *hibernation_ops; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 51 |  | 
 | 52 | /** | 
 | 53 |  * hibernation_set_ops - set the global hibernate operations | 
 | 54 |  * @ops: the hibernation operations to use in subsequent hibernation transitions | 
 | 55 |  */ | 
 | 56 |  | 
| Rafael J. Wysocki | b3dac3b | 2007-10-18 03:04:43 -0700 | [diff] [blame] | 57 | void hibernation_set_ops(struct platform_hibernation_ops *ops) | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 58 | { | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 59 | 	if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot | 
 | 60 | 	    && ops->prepare && ops->finish && ops->enter && ops->pre_restore | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 61 | 	    && ops->restore_cleanup)) { | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 62 | 		WARN_ON(1); | 
 | 63 | 		return; | 
 | 64 | 	} | 
 | 65 | 	mutex_lock(&pm_mutex); | 
 | 66 | 	hibernation_ops = ops; | 
 | 67 | 	if (ops) | 
 | 68 | 		hibernation_mode = HIBERNATION_PLATFORM; | 
 | 69 | 	else if (hibernation_mode == HIBERNATION_PLATFORM) | 
 | 70 | 		hibernation_mode = HIBERNATION_SHUTDOWN; | 
 | 71 |  | 
 | 72 | 	mutex_unlock(&pm_mutex); | 
 | 73 | } | 
 | 74 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 75 | #ifdef CONFIG_PM_DEBUG | 
 | 76 | static void hibernation_debug_sleep(void) | 
 | 77 | { | 
 | 78 | 	printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n"); | 
 | 79 | 	mdelay(5000); | 
 | 80 | } | 
 | 81 |  | 
 | 82 | static int hibernation_testmode(int mode) | 
 | 83 | { | 
 | 84 | 	if (hibernation_mode == mode) { | 
 | 85 | 		hibernation_debug_sleep(); | 
 | 86 | 		return 1; | 
 | 87 | 	} | 
 | 88 | 	return 0; | 
 | 89 | } | 
 | 90 |  | 
 | 91 | static int hibernation_test(int level) | 
 | 92 | { | 
 | 93 | 	if (pm_test_level == level) { | 
 | 94 | 		hibernation_debug_sleep(); | 
 | 95 | 		return 1; | 
 | 96 | 	} | 
 | 97 | 	return 0; | 
 | 98 | } | 
 | 99 | #else /* !CONFIG_PM_DEBUG */ | 
 | 100 | static int hibernation_testmode(int mode) { return 0; } | 
 | 101 | static int hibernation_test(int level) { return 0; } | 
 | 102 | #endif /* !CONFIG_PM_DEBUG */ | 
 | 103 |  | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 104 | /** | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 105 |  *	platform_begin - tell the platform driver that we're starting | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 106 |  *	hibernation | 
 | 107 |  */ | 
 | 108 |  | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 109 | static int platform_begin(int platform_mode) | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 110 | { | 
 | 111 | 	return (platform_mode && hibernation_ops) ? | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 112 | 		hibernation_ops->begin() : 0; | 
 | 113 | } | 
 | 114 |  | 
 | 115 | /** | 
 | 116 |  *	platform_end - tell the platform driver that we've entered the | 
 | 117 |  *	working state | 
 | 118 |  */ | 
 | 119 |  | 
 | 120 | static void platform_end(int platform_mode) | 
 | 121 | { | 
 | 122 | 	if (platform_mode && hibernation_ops) | 
 | 123 | 		hibernation_ops->end(); | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 124 | } | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 125 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | /** | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 127 |  *	platform_pre_snapshot - prepare the machine for hibernation using the | 
| Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 128 |  *	platform driver if so configured and return an error code if it fails | 
 | 129 |  */ | 
 | 130 |  | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 131 | static int platform_pre_snapshot(int platform_mode) | 
| Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 132 | { | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 133 | 	return (platform_mode && hibernation_ops) ? | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 134 | 		hibernation_ops->pre_snapshot() : 0; | 
| Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 135 | } | 
 | 136 |  | 
 | 137 | /** | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 138 |  *	platform_leave - prepare the machine for switching to the normal mode | 
 | 139 |  *	of operation using the platform driver (called with interrupts disabled) | 
 | 140 |  */ | 
 | 141 |  | 
 | 142 | static void platform_leave(int platform_mode) | 
 | 143 | { | 
 | 144 | 	if (platform_mode && hibernation_ops) | 
 | 145 | 		hibernation_ops->leave(); | 
 | 146 | } | 
 | 147 |  | 
 | 148 | /** | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 149 |  *	platform_finish - switch the machine to the normal mode of operation | 
 | 150 |  *	using the platform driver (must be called after platform_prepare()) | 
 | 151 |  */ | 
 | 152 |  | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 153 | static void platform_finish(int platform_mode) | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 154 | { | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 155 | 	if (platform_mode && hibernation_ops) | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 156 | 		hibernation_ops->finish(); | 
 | 157 | } | 
 | 158 |  | 
 | 159 | /** | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 160 |  *	platform_pre_restore - prepare the platform for the restoration from a | 
 | 161 |  *	hibernation image.  If the restore fails after this function has been | 
 | 162 |  *	called, platform_restore_cleanup() must be called. | 
 | 163 |  */ | 
 | 164 |  | 
 | 165 | static int platform_pre_restore(int platform_mode) | 
 | 166 | { | 
 | 167 | 	return (platform_mode && hibernation_ops) ? | 
 | 168 | 		hibernation_ops->pre_restore() : 0; | 
 | 169 | } | 
 | 170 |  | 
 | 171 | /** | 
 | 172 |  *	platform_restore_cleanup - switch the platform to the normal mode of | 
 | 173 |  *	operation after a failing restore.  If platform_pre_restore() has been | 
 | 174 |  *	called before the failing restore, this function must be called too, | 
 | 175 |  *	regardless of the result of platform_pre_restore(). | 
 | 176 |  */ | 
 | 177 |  | 
 | 178 | static void platform_restore_cleanup(int platform_mode) | 
 | 179 | { | 
 | 180 | 	if (platform_mode && hibernation_ops) | 
 | 181 | 		hibernation_ops->restore_cleanup(); | 
 | 182 | } | 
 | 183 |  | 
 | 184 | /** | 
| Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 185 |  *	platform_recover - recover the platform from a failure to suspend | 
 | 186 |  *	devices. | 
 | 187 |  */ | 
 | 188 |  | 
 | 189 | static void platform_recover(int platform_mode) | 
 | 190 | { | 
 | 191 | 	if (platform_mode && hibernation_ops && hibernation_ops->recover) | 
 | 192 | 		hibernation_ops->recover(); | 
 | 193 | } | 
 | 194 |  | 
 | 195 | /** | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 196 |  *	create_image - freeze devices that need to be frozen with interrupts | 
 | 197 |  *	off, create the hibernation image and thaw those devices.  Control | 
 | 198 |  *	reappears in this routine after a restore. | 
 | 199 |  */ | 
 | 200 |  | 
| Adrian Bunk | 47a460d | 2008-02-04 22:30:06 -0800 | [diff] [blame] | 201 | static int create_image(int platform_mode) | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 202 | { | 
 | 203 | 	int error; | 
 | 204 |  | 
 | 205 | 	error = arch_prepare_suspend(); | 
 | 206 | 	if (error) | 
 | 207 | 		return error; | 
 | 208 |  | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 209 | 	device_pm_lock(); | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 210 | 	local_irq_disable(); | 
 | 211 | 	/* At this point, device_suspend() has been called, but *not* | 
 | 212 | 	 * device_power_down(). We *must* call device_power_down() now. | 
 | 213 | 	 * Otherwise, drivers for some devices (e.g. interrupt controllers) | 
 | 214 | 	 * become desynchronized with the actual state of the hardware | 
 | 215 | 	 * at resume time, and evil weirdness ensues. | 
 | 216 | 	 */ | 
 | 217 | 	error = device_power_down(PMSG_FREEZE); | 
 | 218 | 	if (error) { | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 219 | 		printk(KERN_ERR "PM: Some devices failed to power down, " | 
 | 220 | 			"aborting hibernation\n"); | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 221 | 		goto Enable_irqs; | 
 | 222 | 	} | 
 | 223 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 224 | 	if (hibernation_test(TEST_CORE)) | 
 | 225 | 		goto Power_up; | 
 | 226 |  | 
 | 227 | 	in_suspend = 1; | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 228 | 	save_processor_state(); | 
 | 229 | 	error = swsusp_arch_suspend(); | 
 | 230 | 	if (error) | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 231 | 		printk(KERN_ERR "PM: Error %d creating hibernation image\n", | 
 | 232 | 			error); | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 233 | 	/* Restore control flow magically appears here */ | 
 | 234 | 	restore_processor_state(); | 
 | 235 | 	if (!in_suspend) | 
 | 236 | 		platform_leave(platform_mode); | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 237 |  Power_up: | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 238 | 	/* NOTE:  device_power_up() is just a resume() for devices | 
 | 239 | 	 * that suspended with irqs off ... no overall powerup. | 
 | 240 | 	 */ | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 241 | 	device_power_up(in_suspend ? | 
 | 242 | 		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 243 |  Enable_irqs: | 
 | 244 | 	local_irq_enable(); | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 245 | 	device_pm_unlock(); | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 246 | 	return error; | 
 | 247 | } | 
 | 248 |  | 
 | 249 | /** | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 250 |  *	hibernation_snapshot - quiesce devices and create the hibernation | 
 | 251 |  *	snapshot image. | 
 | 252 |  *	@platform_mode - if set, use the platform driver, if available, to | 
 | 253 |  *			 prepare the platform frimware for the power transition. | 
 | 254 |  * | 
 | 255 |  *	Must be called with pm_mutex held | 
 | 256 |  */ | 
 | 257 |  | 
 | 258 | int hibernation_snapshot(int platform_mode) | 
 | 259 | { | 
| Rafael J. Wysocki | 41108eb | 2008-08-28 14:39:12 +0200 | [diff] [blame] | 260 | 	int error, ftrace_save; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 261 |  | 
 | 262 | 	/* Free memory before shutting down devices. */ | 
 | 263 | 	error = swsusp_shrink_memory(); | 
 | 264 | 	if (error) | 
| Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 265 | 		return error; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 266 |  | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 267 | 	error = platform_begin(platform_mode); | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 268 | 	if (error) | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 269 | 		goto Close; | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 270 |  | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 271 | 	suspend_console(); | 
| Rafael J. Wysocki | 41108eb | 2008-08-28 14:39:12 +0200 | [diff] [blame] | 272 | 	ftrace_save = __ftrace_enabled_save(); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 273 | 	error = device_suspend(PMSG_FREEZE); | 
 | 274 | 	if (error) | 
| Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 275 | 		goto Recover_platform; | 
| Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 276 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 277 | 	if (hibernation_test(TEST_DEVICES)) | 
| Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 278 | 		goto Recover_platform; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 279 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 280 | 	error = platform_pre_snapshot(platform_mode); | 
 | 281 | 	if (error || hibernation_test(TEST_PLATFORM)) | 
 | 282 | 		goto Finish; | 
 | 283 |  | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 284 | 	error = disable_nonboot_cpus(); | 
 | 285 | 	if (!error) { | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 286 | 		if (hibernation_test(TEST_CPUS)) | 
 | 287 | 			goto Enable_cpus; | 
 | 288 |  | 
 | 289 | 		if (hibernation_testmode(HIBERNATION_TEST)) | 
 | 290 | 			goto Enable_cpus; | 
 | 291 |  | 
 | 292 | 		error = create_image(platform_mode); | 
 | 293 | 		/* Control returns here after successful restore */ | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 294 | 	} | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 295 |  Enable_cpus: | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 296 | 	enable_nonboot_cpus(); | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 297 |  Finish: | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 298 | 	platform_finish(platform_mode); | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 299 |  Resume_devices: | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 300 | 	device_resume(in_suspend ? | 
 | 301 | 		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); | 
| Rafael J. Wysocki | 41108eb | 2008-08-28 14:39:12 +0200 | [diff] [blame] | 302 | 	__ftrace_enabled_restore(ftrace_save); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 303 | 	resume_console(); | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 304 |  Close: | 
 | 305 | 	platform_end(platform_mode); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 306 | 	return error; | 
| Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 307 |  | 
 | 308 |  Recover_platform: | 
 | 309 | 	platform_recover(platform_mode); | 
 | 310 | 	goto Resume_devices; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 311 | } | 
 | 312 |  | 
 | 313 | /** | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 314 |  *	resume_target_kernel - prepare devices that need to be suspended with | 
 | 315 |  *	interrupts off, restore the contents of highmem that have not been | 
 | 316 |  *	restored yet from the image and run the low level code that will restore | 
 | 317 |  *	the remaining contents of memory and switch to the just restored target | 
 | 318 |  *	kernel. | 
 | 319 |  */ | 
 | 320 |  | 
 | 321 | static int resume_target_kernel(void) | 
 | 322 | { | 
 | 323 | 	int error; | 
 | 324 |  | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 325 | 	device_pm_lock(); | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 326 | 	local_irq_disable(); | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 327 | 	error = device_power_down(PMSG_QUIESCE); | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 328 | 	if (error) { | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 329 | 		printk(KERN_ERR "PM: Some devices failed to power down, " | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 330 | 			"aborting resume\n"); | 
 | 331 | 		goto Enable_irqs; | 
 | 332 | 	} | 
 | 333 | 	/* We'll ignore saved state, but this gets preempt count (etc) right */ | 
 | 334 | 	save_processor_state(); | 
 | 335 | 	error = restore_highmem(); | 
 | 336 | 	if (!error) { | 
 | 337 | 		error = swsusp_arch_resume(); | 
 | 338 | 		/* | 
 | 339 | 		 * The code below is only ever reached in case of a failure. | 
 | 340 | 		 * Otherwise execution continues at place where | 
 | 341 | 		 * swsusp_arch_suspend() was called | 
 | 342 | 		 */ | 
 | 343 | 		BUG_ON(!error); | 
 | 344 | 		/* This call to restore_highmem() undos the previous one */ | 
 | 345 | 		restore_highmem(); | 
 | 346 | 	} | 
 | 347 | 	/* | 
 | 348 | 	 * The only reason why swsusp_arch_resume() can fail is memory being | 
 | 349 | 	 * very tight, so we have to free it as soon as we can to avoid | 
 | 350 | 	 * subsequent failures | 
 | 351 | 	 */ | 
 | 352 | 	swsusp_free(); | 
 | 353 | 	restore_processor_state(); | 
 | 354 | 	touch_softlockup_watchdog(); | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 355 | 	device_power_up(PMSG_RECOVER); | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 356 |  Enable_irqs: | 
 | 357 | 	local_irq_enable(); | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 358 | 	device_pm_unlock(); | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 359 | 	return error; | 
 | 360 | } | 
 | 361 |  | 
 | 362 | /** | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 363 |  *	hibernation_restore - quiesce devices and restore the hibernation | 
 | 364 |  *	snapshot image.  If successful, control returns in hibernation_snaphot() | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 365 |  *	@platform_mode - if set, use the platform driver, if available, to | 
 | 366 |  *			 prepare the platform frimware for the transition. | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 367 |  * | 
 | 368 |  *	Must be called with pm_mutex held | 
 | 369 |  */ | 
 | 370 |  | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 371 | int hibernation_restore(int platform_mode) | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 372 | { | 
| Rafael J. Wysocki | 41108eb | 2008-08-28 14:39:12 +0200 | [diff] [blame] | 373 | 	int error, ftrace_save; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 374 |  | 
 | 375 | 	pm_prepare_console(); | 
 | 376 | 	suspend_console(); | 
| Rafael J. Wysocki | 41108eb | 2008-08-28 14:39:12 +0200 | [diff] [blame] | 377 | 	ftrace_save = __ftrace_enabled_save(); | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 378 | 	error = device_suspend(PMSG_QUIESCE); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 379 | 	if (error) | 
 | 380 | 		goto Finish; | 
 | 381 |  | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 382 | 	error = platform_pre_restore(platform_mode); | 
 | 383 | 	if (!error) { | 
 | 384 | 		error = disable_nonboot_cpus(); | 
 | 385 | 		if (!error) | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 386 | 			error = resume_target_kernel(); | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 387 | 		enable_nonboot_cpus(); | 
 | 388 | 	} | 
 | 389 | 	platform_restore_cleanup(platform_mode); | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 390 | 	device_resume(PMSG_RECOVER); | 
| Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 391 |  Finish: | 
| Rafael J. Wysocki | 41108eb | 2008-08-28 14:39:12 +0200 | [diff] [blame] | 392 | 	__ftrace_enabled_restore(ftrace_save); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 393 | 	resume_console(); | 
 | 394 | 	pm_restore_console(); | 
 | 395 | 	return error; | 
 | 396 | } | 
 | 397 |  | 
 | 398 | /** | 
 | 399 |  *	hibernation_platform_enter - enter the hibernation state using the | 
 | 400 |  *	platform driver (if available) | 
 | 401 |  */ | 
 | 402 |  | 
 | 403 | int hibernation_platform_enter(void) | 
 | 404 | { | 
| Rafael J. Wysocki | 41108eb | 2008-08-28 14:39:12 +0200 | [diff] [blame] | 405 | 	int error, ftrace_save; | 
| Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 406 |  | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 407 | 	if (!hibernation_ops) | 
 | 408 | 		return -ENOSYS; | 
 | 409 |  | 
 | 410 | 	/* | 
 | 411 | 	 * We have cancelled the power transition by running | 
 | 412 | 	 * hibernation_ops->finish() before saving the image, so we should let | 
 | 413 | 	 * the firmware know that we're going to enter the sleep state after all | 
 | 414 | 	 */ | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 415 | 	error = hibernation_ops->begin(); | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 416 | 	if (error) | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 417 | 		goto Close; | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 418 |  | 
 | 419 | 	suspend_console(); | 
| Rafael J. Wysocki | 41108eb | 2008-08-28 14:39:12 +0200 | [diff] [blame] | 420 | 	ftrace_save = __ftrace_enabled_save(); | 
| Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 421 | 	error = device_suspend(PMSG_HIBERNATE); | 
| Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 422 | 	if (error) { | 
 | 423 | 		if (hibernation_ops->recover) | 
 | 424 | 			hibernation_ops->recover(); | 
 | 425 | 		goto Resume_devices; | 
 | 426 | 	} | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 427 |  | 
 | 428 | 	error = hibernation_ops->prepare(); | 
 | 429 | 	if (error) | 
 | 430 | 		goto Resume_devices; | 
 | 431 |  | 
 | 432 | 	error = disable_nonboot_cpus(); | 
 | 433 | 	if (error) | 
 | 434 | 		goto Finish; | 
 | 435 |  | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 436 | 	device_pm_lock(); | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 437 | 	local_irq_disable(); | 
| Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 438 | 	error = device_power_down(PMSG_HIBERNATE); | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 439 | 	if (!error) { | 
 | 440 | 		hibernation_ops->enter(); | 
 | 441 | 		/* We should never get here */ | 
 | 442 | 		while (1); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 443 | 	} | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 444 | 	local_irq_enable(); | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 445 | 	device_pm_unlock(); | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 446 |  | 
 | 447 | 	/* | 
 | 448 | 	 * We don't need to reenable the nonboot CPUs or resume consoles, since | 
 | 449 | 	 * the system is going to be halted anyway. | 
 | 450 | 	 */ | 
 | 451 |  Finish: | 
 | 452 | 	hibernation_ops->finish(); | 
 | 453 |  Resume_devices: | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 454 | 	device_resume(PMSG_RESTORE); | 
| Rafael J. Wysocki | 41108eb | 2008-08-28 14:39:12 +0200 | [diff] [blame] | 455 | 	__ftrace_enabled_restore(ftrace_save); | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 456 | 	resume_console(); | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 457 |  Close: | 
 | 458 | 	hibernation_ops->end(); | 
| Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 459 | 	return error; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 460 | } | 
 | 461 |  | 
 | 462 | /** | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 463 |  *	power_down - Shut the machine down for hibernation. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 |  * | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 465 |  *	Use the platform driver, if configured so; otherwise try | 
 | 466 |  *	to power off or reboot. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 |  */ | 
 | 468 |  | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 469 | static void power_down(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 471 | 	switch (hibernation_mode) { | 
 | 472 | 	case HIBERNATION_TEST: | 
 | 473 | 	case HIBERNATION_TESTPROC: | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 474 | 		break; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 475 | 	case HIBERNATION_REBOOT: | 
| Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 476 | 		kernel_restart(NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | 		break; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 478 | 	case HIBERNATION_PLATFORM: | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 479 | 		hibernation_platform_enter(); | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 480 | 	case HIBERNATION_SHUTDOWN: | 
 | 481 | 		kernel_power_off(); | 
 | 482 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | 	} | 
| Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 484 | 	kernel_halt(); | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 485 | 	/* | 
 | 486 | 	 * Valid image is on the disk, if we continue we risk serious data | 
 | 487 | 	 * corruption after resume. | 
 | 488 | 	 */ | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 489 | 	printk(KERN_CRIT "PM: Please power down manually\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | 	while(1); | 
 | 491 | } | 
 | 492 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | static int prepare_processes(void) | 
 | 494 | { | 
| Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 495 | 	int error = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | 	if (freeze_processes()) { | 
 | 498 | 		error = -EBUSY; | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 499 | 		thaw_processes(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | 	} | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 501 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } | 
 | 503 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | /** | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 505 |  *	hibernate - The granpappy of the built-in hibernation management | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 |  */ | 
 | 507 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 508 | int hibernate(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | { | 
 | 510 | 	int error; | 
 | 511 |  | 
| Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 512 | 	mutex_lock(&pm_mutex); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 513 | 	/* The snapshot device should not be opened while we're running */ | 
| Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 514 | 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { | 
 | 515 | 		error = -EBUSY; | 
 | 516 | 		goto Unlock; | 
 | 517 | 	} | 
 | 518 |  | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 519 | 	pm_prepare_console(); | 
| Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 520 | 	error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE); | 
 | 521 | 	if (error) | 
 | 522 | 		goto Exit; | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 523 |  | 
| Rafael J. Wysocki | 1bfcf13 | 2008-10-15 22:01:21 -0700 | [diff] [blame] | 524 | 	error = usermodehelper_disable(); | 
 | 525 | 	if (error) | 
 | 526 | 		goto Exit; | 
 | 527 |  | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 528 | 	/* Allocate memory management structures */ | 
 | 529 | 	error = create_basic_memory_bitmaps(); | 
 | 530 | 	if (error) | 
 | 531 | 		goto Exit; | 
 | 532 |  | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 533 | 	printk(KERN_INFO "PM: Syncing filesystems ... "); | 
| Rafael J. Wysocki | 232b143 | 2007-10-18 03:04:44 -0700 | [diff] [blame] | 534 | 	sys_sync(); | 
 | 535 | 	printk("done.\n"); | 
 | 536 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | 	error = prepare_processes(); | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 538 | 	if (error) | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 539 | 		goto Finish; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 541 | 	if (hibernation_test(TEST_FREEZER)) | 
| Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 542 | 		goto Thaw; | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 543 |  | 
 | 544 | 	if (hibernation_testmode(HIBERNATION_TESTPROC)) | 
 | 545 | 		goto Thaw; | 
 | 546 |  | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 547 | 	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); | 
 | 548 | 	if (in_suspend && !error) { | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 549 | 		unsigned int flags = 0; | 
 | 550 |  | 
 | 551 | 		if (hibernation_mode == HIBERNATION_PLATFORM) | 
 | 552 | 			flags |= SF_PLATFORM_MODE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | 		pr_debug("PM: writing image.\n"); | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 554 | 		error = swsusp_write(flags); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 555 | 		swsusp_free(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | 		if (!error) | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 557 | 			power_down(); | 
| Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 558 | 	} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | 		pr_debug("PM: Image restored successfully.\n"); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 560 | 		swsusp_free(); | 
| Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 561 | 	} | 
| Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 562 |  Thaw: | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 563 | 	thaw_processes(); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 564 |  Finish: | 
 | 565 | 	free_basic_memory_bitmaps(); | 
| Rafael J. Wysocki | 1bfcf13 | 2008-10-15 22:01:21 -0700 | [diff] [blame] | 566 | 	usermodehelper_enable(); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 567 |  Exit: | 
| Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 568 | 	pm_notifier_call_chain(PM_POST_HIBERNATION); | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 569 | 	pm_restore_console(); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 570 | 	atomic_inc(&snapshot_device_available); | 
| Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 571 |  Unlock: | 
 | 572 | 	mutex_unlock(&pm_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | 	return error; | 
 | 574 | } | 
 | 575 |  | 
 | 576 |  | 
 | 577 | /** | 
 | 578 |  *	software_resume - Resume from a saved image. | 
 | 579 |  * | 
 | 580 |  *	Called as a late_initcall (so all devices are discovered and | 
 | 581 |  *	initialized), we call swsusp to see if we have a saved image or not. | 
 | 582 |  *	If so, we quiesce devices, the restore the saved image. We will | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 583 |  *	return above (in hibernate() ) if everything goes well. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 |  *	Otherwise, we fail gracefully and return to the normally | 
 | 585 |  *	scheduled program. | 
 | 586 |  * | 
 | 587 |  */ | 
 | 588 |  | 
 | 589 | static int software_resume(void) | 
 | 590 | { | 
 | 591 | 	int error; | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 592 | 	unsigned int flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 |  | 
| Johannes Berg | 60a0d23 | 2007-11-14 17:00:16 -0800 | [diff] [blame] | 594 | 	/* | 
 | 595 | 	 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs | 
 | 596 | 	 * is configured into the kernel. Since the regular hibernate | 
 | 597 | 	 * trigger path is via sysfs which takes a buffer mutex before | 
 | 598 | 	 * calling hibernate functions (which take pm_mutex) this can | 
 | 599 | 	 * cause lockdep to complain about a possible ABBA deadlock | 
 | 600 | 	 * which cannot happen since we're in the boot code here and | 
 | 601 | 	 * sysfs can't be invoked yet. Therefore, we use a subclass | 
 | 602 | 	 * here to avoid lockdep complaining. | 
 | 603 | 	 */ | 
 | 604 | 	mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING); | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 605 | 	if (!swsusp_resume_device) { | 
| Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 606 | 		if (!strlen(resume_file)) { | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 607 | 			mutex_unlock(&pm_mutex); | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 608 | 			return -ENOENT; | 
| Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 609 | 		} | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 610 | 		swsusp_resume_device = name_to_dev_t(resume_file); | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 611 | 		pr_debug("PM: Resume from partition %s\n", resume_file); | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 612 | 	} else { | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 613 | 		pr_debug("PM: Resume from partition %d:%d\n", | 
 | 614 | 				MAJOR(swsusp_resume_device), | 
 | 615 | 				MINOR(swsusp_resume_device)); | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 616 | 	} | 
 | 617 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | 	if (noresume) { | 
 | 619 | 		/** | 
| Rafael J. Wysocki | 9575809 | 2007-12-08 02:06:57 +0100 | [diff] [blame] | 620 | 		 * FIXME: If noresume is specified, we need to find the | 
 | 621 | 		 * partition and reset it back to normal swap space. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | 		 */ | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 623 | 		mutex_unlock(&pm_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | 		return 0; | 
 | 625 | 	} | 
 | 626 |  | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 627 | 	pr_debug("PM: Checking hibernation image.\n"); | 
| Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 628 | 	error = swsusp_check(); | 
 | 629 | 	if (error) | 
| Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 630 | 		goto Unlock; | 
 | 631 |  | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 632 | 	/* The snapshot device should not be opened while we're running */ | 
 | 633 | 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { | 
 | 634 | 		error = -EBUSY; | 
 | 635 | 		goto Unlock; | 
 | 636 | 	} | 
 | 637 |  | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 638 | 	pm_prepare_console(); | 
| Alan Stern | c3e94d8 | 2007-11-19 23:38:25 +0100 | [diff] [blame] | 639 | 	error = pm_notifier_call_chain(PM_RESTORE_PREPARE); | 
 | 640 | 	if (error) | 
 | 641 | 		goto Finish; | 
 | 642 |  | 
| Rafael J. Wysocki | 1bfcf13 | 2008-10-15 22:01:21 -0700 | [diff] [blame] | 643 | 	error = usermodehelper_disable(); | 
 | 644 | 	if (error) | 
 | 645 | 		goto Finish; | 
 | 646 |  | 
| Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 647 | 	error = create_basic_memory_bitmaps(); | 
 | 648 | 	if (error) | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 649 | 		goto Finish; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 |  | 
 | 651 | 	pr_debug("PM: Preparing processes for restore.\n"); | 
| Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 652 | 	error = prepare_processes(); | 
 | 653 | 	if (error) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | 		swsusp_close(); | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 655 | 		goto Done; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | 	} | 
 | 657 |  | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 658 | 	pr_debug("PM: Reading hibernation image.\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 |  | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 660 | 	error = swsusp_read(&flags); | 
| Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 661 | 	if (!error) | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 662 | 		hibernation_restore(flags & SF_PLATFORM_MODE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 |  | 
| Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 664 | 	printk(KERN_ERR "PM: Restore failed, recovering.\n"); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 665 | 	swsusp_free(); | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 666 | 	thaw_processes(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 |  Done: | 
| Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 668 | 	free_basic_memory_bitmaps(); | 
| Rafael J. Wysocki | 1bfcf13 | 2008-10-15 22:01:21 -0700 | [diff] [blame] | 669 | 	usermodehelper_enable(); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 670 |  Finish: | 
| Alan Stern | c3e94d8 | 2007-11-19 23:38:25 +0100 | [diff] [blame] | 671 | 	pm_notifier_call_chain(PM_POST_RESTORE); | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 672 | 	pm_restore_console(); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 673 | 	atomic_inc(&snapshot_device_available); | 
| Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 674 | 	/* For success case, the suspend path will release the lock */ | 
| Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 675 |  Unlock: | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 676 | 	mutex_unlock(&pm_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | 	pr_debug("PM: Resume from disk failed.\n"); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 678 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } | 
 | 680 |  | 
 | 681 | late_initcall(software_resume); | 
 | 682 |  | 
 | 683 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 684 | static const char * const hibernation_modes[] = { | 
 | 685 | 	[HIBERNATION_PLATFORM]	= "platform", | 
 | 686 | 	[HIBERNATION_SHUTDOWN]	= "shutdown", | 
 | 687 | 	[HIBERNATION_REBOOT]	= "reboot", | 
 | 688 | 	[HIBERNATION_TEST]	= "test", | 
 | 689 | 	[HIBERNATION_TESTPROC]	= "testproc", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | }; | 
 | 691 |  | 
 | 692 | /** | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 693 |  *	disk - Control hibernation mode | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 |  * | 
| Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 695 |  *	Suspend-to-disk can be handled in several ways. We have a few options | 
 | 696 |  *	for putting the system to sleep - using the platform driver (e.g. ACPI | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 697 |  *	or other hibernation_ops), powering off the system or rebooting the | 
 | 698 |  *	system (for testing) as well as the two test modes. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 |  * | 
| Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 700 |  *	The system can support 'platform', and that is known a priori (and | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 701 |  *	encoded by the presence of hibernation_ops). However, the user may | 
 | 702 |  *	choose 'shutdown' or 'reboot' as alternatives, as well as one fo the | 
 | 703 |  *	test modes, 'test' or 'testproc'. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 |  * | 
 | 705 |  *	show() will display what the mode is currently set to. | 
 | 706 |  *	store() will accept one of | 
 | 707 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 |  *	'platform' | 
 | 709 |  *	'shutdown' | 
 | 710 |  *	'reboot' | 
| Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 711 |  *	'test' | 
 | 712 |  *	'testproc' | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 |  * | 
| Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 714 |  *	It will only change to 'platform' if the system | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 715 |  *	supports it (as determined by having hibernation_ops). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 |  */ | 
 | 717 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 718 | static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 719 | 			 char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | { | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 721 | 	int i; | 
 | 722 | 	char *start = buf; | 
 | 723 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 724 | 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { | 
 | 725 | 		if (!hibernation_modes[i]) | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 726 | 			continue; | 
 | 727 | 		switch (i) { | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 728 | 		case HIBERNATION_SHUTDOWN: | 
 | 729 | 		case HIBERNATION_REBOOT: | 
 | 730 | 		case HIBERNATION_TEST: | 
 | 731 | 		case HIBERNATION_TESTPROC: | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 732 | 			break; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 733 | 		case HIBERNATION_PLATFORM: | 
 | 734 | 			if (hibernation_ops) | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 735 | 				break; | 
 | 736 | 			/* not a valid mode, continue with loop */ | 
 | 737 | 			continue; | 
 | 738 | 		} | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 739 | 		if (i == hibernation_mode) | 
 | 740 | 			buf += sprintf(buf, "[%s] ", hibernation_modes[i]); | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 741 | 		else | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 742 | 			buf += sprintf(buf, "%s ", hibernation_modes[i]); | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 743 | 	} | 
 | 744 | 	buf += sprintf(buf, "\n"); | 
 | 745 | 	return buf-start; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } | 
 | 747 |  | 
 | 748 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 749 | static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 750 | 			  const char *buf, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { | 
 | 752 | 	int error = 0; | 
 | 753 | 	int i; | 
 | 754 | 	int len; | 
 | 755 | 	char *p; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 756 | 	int mode = HIBERNATION_INVALID; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 |  | 
 | 758 | 	p = memchr(buf, '\n', n); | 
 | 759 | 	len = p ? p - buf : n; | 
 | 760 |  | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 761 | 	mutex_lock(&pm_mutex); | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 762 | 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { | 
| Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 763 | 		if (len == strlen(hibernation_modes[i]) | 
 | 764 | 		    && !strncmp(buf, hibernation_modes[i], len)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | 			mode = i; | 
 | 766 | 			break; | 
 | 767 | 		} | 
 | 768 | 	} | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 769 | 	if (mode != HIBERNATION_INVALID) { | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 770 | 		switch (mode) { | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 771 | 		case HIBERNATION_SHUTDOWN: | 
 | 772 | 		case HIBERNATION_REBOOT: | 
 | 773 | 		case HIBERNATION_TEST: | 
 | 774 | 		case HIBERNATION_TESTPROC: | 
 | 775 | 			hibernation_mode = mode; | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 776 | 			break; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 777 | 		case HIBERNATION_PLATFORM: | 
 | 778 | 			if (hibernation_ops) | 
 | 779 | 				hibernation_mode = mode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | 			else | 
 | 781 | 				error = -EINVAL; | 
 | 782 | 		} | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 783 | 	} else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | 		error = -EINVAL; | 
 | 785 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 786 | 	if (!error) | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 787 | 		pr_debug("PM: Hibernation mode set to '%s'\n", | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 788 | 			 hibernation_modes[mode]); | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 789 | 	mutex_unlock(&pm_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | 	return error ? error : n; | 
 | 791 | } | 
 | 792 |  | 
 | 793 | power_attr(disk); | 
 | 794 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 795 | static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 796 | 			   char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | { | 
 | 798 | 	return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), | 
 | 799 | 		       MINOR(swsusp_resume_device)); | 
 | 800 | } | 
 | 801 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 802 | static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 803 | 			    const char *buf, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | 	unsigned int maj, min; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | 	dev_t res; | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 807 | 	int ret = -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 |  | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 809 | 	if (sscanf(buf, "%u:%u", &maj, &min) != 2) | 
 | 810 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 |  | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 812 | 	res = MKDEV(maj,min); | 
 | 813 | 	if (maj != MAJOR(res) || min != MINOR(res)) | 
 | 814 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 |  | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 816 | 	mutex_lock(&pm_mutex); | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 817 | 	swsusp_resume_device = res; | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 818 | 	mutex_unlock(&pm_mutex); | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 819 | 	printk(KERN_INFO "PM: Starting manual resume from disk\n"); | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 820 | 	noresume = 0; | 
 | 821 | 	software_resume(); | 
 | 822 | 	ret = n; | 
| Rafael J. Wysocki | 59a4933 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 823 |  out: | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 824 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } | 
 | 826 |  | 
 | 827 | power_attr(resume); | 
 | 828 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 829 | static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 830 | 			       char *buf) | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 831 | { | 
| Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 832 | 	return sprintf(buf, "%lu\n", image_size); | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 833 | } | 
 | 834 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 835 | static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 836 | 				const char *buf, size_t n) | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 837 | { | 
| Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 838 | 	unsigned long size; | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 839 |  | 
| Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 840 | 	if (sscanf(buf, "%lu", &size) == 1) { | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 841 | 		image_size = size; | 
 | 842 | 		return n; | 
 | 843 | 	} | 
 | 844 |  | 
 | 845 | 	return -EINVAL; | 
 | 846 | } | 
 | 847 |  | 
 | 848 | power_attr(image_size); | 
 | 849 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | static struct attribute * g[] = { | 
 | 851 | 	&disk_attr.attr, | 
 | 852 | 	&resume_attr.attr, | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 853 | 	&image_size_attr.attr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | 	NULL, | 
 | 855 | }; | 
 | 856 |  | 
 | 857 |  | 
 | 858 | static struct attribute_group attr_group = { | 
 | 859 | 	.attrs = g, | 
 | 860 | }; | 
 | 861 |  | 
 | 862 |  | 
 | 863 | static int __init pm_disk_init(void) | 
 | 864 | { | 
| Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 865 | 	return sysfs_create_group(power_kobj, &attr_group); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | } | 
 | 867 |  | 
 | 868 | core_initcall(pm_disk_init); | 
 | 869 |  | 
 | 870 |  | 
 | 871 | static int __init resume_setup(char *str) | 
 | 872 | { | 
 | 873 | 	if (noresume) | 
 | 874 | 		return 1; | 
 | 875 |  | 
 | 876 | 	strncpy( resume_file, str, 255 ); | 
 | 877 | 	return 1; | 
 | 878 | } | 
 | 879 |  | 
| Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 880 | static int __init resume_offset_setup(char *str) | 
 | 881 | { | 
 | 882 | 	unsigned long long offset; | 
 | 883 |  | 
 | 884 | 	if (noresume) | 
 | 885 | 		return 1; | 
 | 886 |  | 
 | 887 | 	if (sscanf(str, "%llu", &offset) == 1) | 
 | 888 | 		swsusp_resume_block = offset; | 
 | 889 |  | 
 | 890 | 	return 1; | 
 | 891 | } | 
 | 892 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | static int __init noresume_setup(char *str) | 
 | 894 | { | 
 | 895 | 	noresume = 1; | 
 | 896 | 	return 1; | 
 | 897 | } | 
 | 898 |  | 
 | 899 | __setup("noresume", noresume_setup); | 
| Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 900 | __setup("resume_offset=", resume_offset_setup); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | __setup("resume=", resume_setup); |