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