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> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/fs.h> |
Andrew Morton | d53d9f1 | 2005-07-12 13:58:07 -0700 | [diff] [blame] | 19 | #include <linux/mount.h> |
Eric W. Biederman | 88d10bb | 2005-09-22 21:43:46 -0700 | [diff] [blame] | 20 | #include <linux/pm.h> |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 21 | #include <linux/console.h> |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 22 | #include <linux/cpu.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 23 | #include <linux/freezer.h> |
Andrew Morton | d53d9f1 | 2005-07-12 13:58:07 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "power.h" |
| 26 | |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static int noresume = 0; |
| 29 | char resume_file[256] = CONFIG_PM_STD_PARTITION; |
| 30 | dev_t swsusp_resume_device; |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 31 | sector_t swsusp_resume_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 33 | enum { |
| 34 | HIBERNATION_INVALID, |
| 35 | HIBERNATION_PLATFORM, |
| 36 | HIBERNATION_TEST, |
| 37 | HIBERNATION_TESTPROC, |
| 38 | HIBERNATION_SHUTDOWN, |
| 39 | HIBERNATION_REBOOT, |
| 40 | /* keep last */ |
| 41 | __HIBERNATION_AFTER_LAST |
| 42 | }; |
| 43 | #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1) |
| 44 | #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1) |
| 45 | |
| 46 | static int hibernation_mode = HIBERNATION_SHUTDOWN; |
| 47 | |
Rafael J. Wysocki | b3dac3b | 2007-10-18 03:04:43 -0700 | [diff] [blame] | 48 | static struct platform_hibernation_ops *hibernation_ops; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * hibernation_set_ops - set the global hibernate operations |
| 52 | * @ops: the hibernation operations to use in subsequent hibernation transitions |
| 53 | */ |
| 54 | |
Rafael J. Wysocki | b3dac3b | 2007-10-18 03:04:43 -0700 | [diff] [blame] | 55 | void hibernation_set_ops(struct platform_hibernation_ops *ops) |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 56 | { |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 57 | if (ops && !(ops->start && ops->pre_snapshot && ops->finish |
| 58 | && ops->prepare && ops->enter && ops->pre_restore |
| 59 | && ops->restore_cleanup)) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 60 | WARN_ON(1); |
| 61 | return; |
| 62 | } |
| 63 | mutex_lock(&pm_mutex); |
| 64 | hibernation_ops = ops; |
| 65 | if (ops) |
| 66 | hibernation_mode = HIBERNATION_PLATFORM; |
| 67 | else if (hibernation_mode == HIBERNATION_PLATFORM) |
| 68 | hibernation_mode = HIBERNATION_SHUTDOWN; |
| 69 | |
| 70 | mutex_unlock(&pm_mutex); |
| 71 | } |
| 72 | |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 73 | /** |
| 74 | * platform_start - tell the platform driver that we're starting |
| 75 | * hibernation |
| 76 | */ |
| 77 | |
| 78 | static int platform_start(int platform_mode) |
| 79 | { |
| 80 | return (platform_mode && hibernation_ops) ? |
| 81 | hibernation_ops->start() : 0; |
| 82 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /** |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 85 | * platform_pre_snapshot - prepare the machine for hibernation using the |
Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 86 | * platform driver if so configured and return an error code if it fails |
| 87 | */ |
| 88 | |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 89 | static int platform_pre_snapshot(int platform_mode) |
Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 90 | { |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 91 | return (platform_mode && hibernation_ops) ? |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 92 | hibernation_ops->pre_snapshot() : 0; |
Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame^] | 96 | * platform_leave - prepare the machine for switching to the normal mode |
| 97 | * of operation using the platform driver (called with interrupts disabled) |
| 98 | */ |
| 99 | |
| 100 | static void platform_leave(int platform_mode) |
| 101 | { |
| 102 | if (platform_mode && hibernation_ops) |
| 103 | hibernation_ops->leave(); |
| 104 | } |
| 105 | |
| 106 | /** |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 107 | * platform_finish - switch the machine to the normal mode of operation |
| 108 | * using the platform driver (must be called after platform_prepare()) |
| 109 | */ |
| 110 | |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 111 | static void platform_finish(int platform_mode) |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 112 | { |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 113 | if (platform_mode && hibernation_ops) |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 114 | hibernation_ops->finish(); |
| 115 | } |
| 116 | |
| 117 | /** |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 118 | * platform_pre_restore - prepare the platform for the restoration from a |
| 119 | * hibernation image. If the restore fails after this function has been |
| 120 | * called, platform_restore_cleanup() must be called. |
| 121 | */ |
| 122 | |
| 123 | static int platform_pre_restore(int platform_mode) |
| 124 | { |
| 125 | return (platform_mode && hibernation_ops) ? |
| 126 | hibernation_ops->pre_restore() : 0; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * platform_restore_cleanup - switch the platform to the normal mode of |
| 131 | * operation after a failing restore. If platform_pre_restore() has been |
| 132 | * called before the failing restore, this function must be called too, |
| 133 | * regardless of the result of platform_pre_restore(). |
| 134 | */ |
| 135 | |
| 136 | static void platform_restore_cleanup(int platform_mode) |
| 137 | { |
| 138 | if (platform_mode && hibernation_ops) |
| 139 | hibernation_ops->restore_cleanup(); |
| 140 | } |
| 141 | |
| 142 | /** |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame^] | 143 | * create_image - freeze devices that need to be frozen with interrupts |
| 144 | * off, create the hibernation image and thaw those devices. Control |
| 145 | * reappears in this routine after a restore. |
| 146 | */ |
| 147 | |
| 148 | int create_image(int platform_mode) |
| 149 | { |
| 150 | int error; |
| 151 | |
| 152 | error = arch_prepare_suspend(); |
| 153 | if (error) |
| 154 | return error; |
| 155 | |
| 156 | local_irq_disable(); |
| 157 | /* At this point, device_suspend() has been called, but *not* |
| 158 | * device_power_down(). We *must* call device_power_down() now. |
| 159 | * Otherwise, drivers for some devices (e.g. interrupt controllers) |
| 160 | * become desynchronized with the actual state of the hardware |
| 161 | * at resume time, and evil weirdness ensues. |
| 162 | */ |
| 163 | error = device_power_down(PMSG_FREEZE); |
| 164 | if (error) { |
| 165 | printk(KERN_ERR "Some devices failed to power down, " |
| 166 | KERN_ERR "aborting suspend\n"); |
| 167 | goto Enable_irqs; |
| 168 | } |
| 169 | |
| 170 | save_processor_state(); |
| 171 | error = swsusp_arch_suspend(); |
| 172 | if (error) |
| 173 | printk(KERN_ERR "Error %d while creating the image\n", error); |
| 174 | /* Restore control flow magically appears here */ |
| 175 | restore_processor_state(); |
| 176 | if (!in_suspend) |
| 177 | platform_leave(platform_mode); |
| 178 | /* NOTE: device_power_up() is just a resume() for devices |
| 179 | * that suspended with irqs off ... no overall powerup. |
| 180 | */ |
| 181 | device_power_up(); |
| 182 | Enable_irqs: |
| 183 | local_irq_enable(); |
| 184 | return error; |
| 185 | } |
| 186 | |
| 187 | /** |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 188 | * hibernation_snapshot - quiesce devices and create the hibernation |
| 189 | * snapshot image. |
| 190 | * @platform_mode - if set, use the platform driver, if available, to |
| 191 | * prepare the platform frimware for the power transition. |
| 192 | * |
| 193 | * Must be called with pm_mutex held |
| 194 | */ |
| 195 | |
| 196 | int hibernation_snapshot(int platform_mode) |
| 197 | { |
| 198 | int error; |
| 199 | |
| 200 | /* Free memory before shutting down devices. */ |
| 201 | error = swsusp_shrink_memory(); |
| 202 | if (error) |
Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 203 | return error; |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 204 | |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 205 | error = platform_start(platform_mode); |
| 206 | if (error) |
| 207 | return error; |
| 208 | |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 209 | suspend_console(); |
| 210 | error = device_suspend(PMSG_FREEZE); |
| 211 | if (error) |
Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 212 | goto Resume_console; |
| 213 | |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 214 | error = platform_pre_snapshot(platform_mode); |
Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 215 | if (error) |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 216 | goto Resume_devices; |
| 217 | |
| 218 | error = disable_nonboot_cpus(); |
| 219 | if (!error) { |
| 220 | if (hibernation_mode != HIBERNATION_TEST) { |
| 221 | in_suspend = 1; |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame^] | 222 | error = create_image(platform_mode); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 223 | /* Control returns here after successful restore */ |
| 224 | } else { |
| 225 | printk("swsusp debug: Waiting for 5 seconds.\n"); |
| 226 | mdelay(5000); |
| 227 | } |
| 228 | } |
| 229 | enable_nonboot_cpus(); |
| 230 | Resume_devices: |
| 231 | platform_finish(platform_mode); |
| 232 | device_resume(); |
Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 233 | Resume_console: |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 234 | resume_console(); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 235 | return error; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * hibernation_restore - quiesce devices and restore the hibernation |
| 240 | * snapshot image. If successful, control returns in hibernation_snaphot() |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 241 | * @platform_mode - if set, use the platform driver, if available, to |
| 242 | * prepare the platform frimware for the transition. |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 243 | * |
| 244 | * Must be called with pm_mutex held |
| 245 | */ |
| 246 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 247 | int hibernation_restore(int platform_mode) |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 248 | { |
| 249 | int error; |
| 250 | |
| 251 | pm_prepare_console(); |
| 252 | suspend_console(); |
| 253 | error = device_suspend(PMSG_PRETHAW); |
| 254 | if (error) |
| 255 | goto Finish; |
| 256 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 257 | error = platform_pre_restore(platform_mode); |
| 258 | if (!error) { |
| 259 | error = disable_nonboot_cpus(); |
| 260 | if (!error) |
| 261 | error = swsusp_resume(); |
| 262 | enable_nonboot_cpus(); |
| 263 | } |
| 264 | platform_restore_cleanup(platform_mode); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 265 | device_resume(); |
Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 266 | Finish: |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 267 | resume_console(); |
| 268 | pm_restore_console(); |
| 269 | return error; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * hibernation_platform_enter - enter the hibernation state using the |
| 274 | * platform driver (if available) |
| 275 | */ |
| 276 | |
| 277 | int hibernation_platform_enter(void) |
| 278 | { |
Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 279 | int error; |
| 280 | |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 281 | if (hibernation_ops) { |
| 282 | kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK); |
Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 283 | /* |
| 284 | * We have cancelled the power transition by running |
| 285 | * hibernation_ops->finish() before saving the image, so we |
| 286 | * should let the firmware know that we're going to enter the |
| 287 | * sleep state after all |
| 288 | */ |
| 289 | error = hibernation_ops->prepare(); |
Rafael J. Wysocki | 58b3b71 | 2007-07-26 16:29:55 +0200 | [diff] [blame] | 290 | sysdev_shutdown(); |
Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 291 | if (!error) |
| 292 | error = hibernation_ops->enter(); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 293 | } else { |
Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 294 | error = -ENOSYS; |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 295 | } |
Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 296 | return error; |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 300 | * power_down - Shut the machine down for hibernation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | * |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 302 | * Use the platform driver, if configured so; otherwise try |
| 303 | * to power off or reboot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | */ |
| 305 | |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 306 | static void power_down(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 308 | switch (hibernation_mode) { |
| 309 | case HIBERNATION_TEST: |
| 310 | case HIBERNATION_TESTPROC: |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 311 | break; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 312 | case HIBERNATION_SHUTDOWN: |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 313 | kernel_power_off(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | break; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 315 | case HIBERNATION_REBOOT: |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 316 | kernel_restart(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | break; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 318 | case HIBERNATION_PLATFORM: |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 319 | hibernation_platform_enter(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 321 | kernel_halt(); |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 322 | /* |
| 323 | * Valid image is on the disk, if we continue we risk serious data |
| 324 | * corruption after resume. |
| 325 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | printk(KERN_CRIT "Please power me down manually\n"); |
| 327 | while(1); |
| 328 | } |
| 329 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 330 | static void unprepare_processes(void) |
| 331 | { |
| 332 | thaw_processes(); |
| 333 | pm_restore_console(); |
| 334 | } |
| 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | static int prepare_processes(void) |
| 337 | { |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 338 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
| 340 | pm_prepare_console(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | if (freeze_processes()) { |
| 342 | error = -EBUSY; |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 343 | unprepare_processes(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 345 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | /** |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 349 | * hibernate - The granpappy of the built-in hibernation management |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | */ |
| 351 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 352 | int hibernate(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | int error; |
| 355 | |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 356 | mutex_lock(&pm_mutex); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 357 | /* The snapshot device should not be opened while we're running */ |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 358 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { |
| 359 | error = -EBUSY; |
| 360 | goto Unlock; |
| 361 | } |
| 362 | |
| 363 | error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE); |
| 364 | if (error) |
| 365 | goto Exit; |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 366 | |
| 367 | /* Allocate memory management structures */ |
| 368 | error = create_basic_memory_bitmaps(); |
| 369 | if (error) |
| 370 | goto Exit; |
| 371 | |
Rafael J. Wysocki | 232b143 | 2007-10-18 03:04:44 -0700 | [diff] [blame] | 372 | printk("Syncing filesystems ... "); |
| 373 | sys_sync(); |
| 374 | printk("done.\n"); |
| 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | error = prepare_processes(); |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 377 | if (error) |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 378 | goto Finish; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 380 | if (hibernation_mode == HIBERNATION_TESTPROC) { |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 381 | printk("swsusp debug: Waiting for 5 seconds.\n"); |
| 382 | mdelay(5000); |
| 383 | goto Thaw; |
| 384 | } |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 385 | error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); |
| 386 | if (in_suspend && !error) { |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 387 | unsigned int flags = 0; |
| 388 | |
| 389 | if (hibernation_mode == HIBERNATION_PLATFORM) |
| 390 | flags |= SF_PLATFORM_MODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | pr_debug("PM: writing image.\n"); |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 392 | error = swsusp_write(flags); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 393 | swsusp_free(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | if (!error) |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 395 | power_down(); |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 396 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | pr_debug("PM: Image restored successfully.\n"); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 398 | swsusp_free(); |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 399 | } |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 400 | Thaw: |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 401 | unprepare_processes(); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 402 | Finish: |
| 403 | free_basic_memory_bitmaps(); |
| 404 | Exit: |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 405 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 406 | atomic_inc(&snapshot_device_available); |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 407 | Unlock: |
| 408 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | return error; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | /** |
| 414 | * software_resume - Resume from a saved image. |
| 415 | * |
| 416 | * Called as a late_initcall (so all devices are discovered and |
| 417 | * initialized), we call swsusp to see if we have a saved image or not. |
| 418 | * 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] | 419 | * return above (in hibernate() ) if everything goes well. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | * Otherwise, we fail gracefully and return to the normally |
| 421 | * scheduled program. |
| 422 | * |
| 423 | */ |
| 424 | |
| 425 | static int software_resume(void) |
| 426 | { |
| 427 | int error; |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 428 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 430 | mutex_lock(&pm_mutex); |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 431 | if (!swsusp_resume_device) { |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 432 | if (!strlen(resume_file)) { |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 433 | mutex_unlock(&pm_mutex); |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 434 | return -ENOENT; |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 435 | } |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 436 | swsusp_resume_device = name_to_dev_t(resume_file); |
| 437 | pr_debug("swsusp: Resume From Partition %s\n", resume_file); |
| 438 | } else { |
| 439 | pr_debug("swsusp: Resume From Partition %d:%d\n", |
| 440 | MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); |
| 441 | } |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | if (noresume) { |
| 444 | /** |
| 445 | * FIXME: If noresume is specified, we need to find the partition |
| 446 | * and reset it back to normal swap space. |
| 447 | */ |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 448 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | pr_debug("PM: Checking swsusp image.\n"); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 453 | error = swsusp_check(); |
| 454 | if (error) |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 455 | goto Unlock; |
| 456 | |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 457 | /* The snapshot device should not be opened while we're running */ |
| 458 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { |
| 459 | error = -EBUSY; |
| 460 | goto Unlock; |
| 461 | } |
| 462 | |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 463 | error = create_basic_memory_bitmaps(); |
| 464 | if (error) |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 465 | goto Finish; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | pr_debug("PM: Preparing processes for restore.\n"); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 468 | error = prepare_processes(); |
| 469 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | swsusp_close(); |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 471 | goto Done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | pr_debug("PM: Reading swsusp image.\n"); |
| 475 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 476 | error = swsusp_read(&flags); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 477 | if (!error) |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 478 | hibernation_restore(flags & SF_PLATFORM_MODE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 480 | printk(KERN_ERR "PM: Restore failed, recovering.\n"); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 481 | swsusp_free(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | unprepare_processes(); |
| 483 | Done: |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 484 | free_basic_memory_bitmaps(); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 485 | Finish: |
| 486 | atomic_inc(&snapshot_device_available); |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 487 | /* For success case, the suspend path will release the lock */ |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 488 | Unlock: |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 489 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | pr_debug("PM: Resume from disk failed.\n"); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 491 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | late_initcall(software_resume); |
| 495 | |
| 496 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 497 | static const char * const hibernation_modes[] = { |
| 498 | [HIBERNATION_PLATFORM] = "platform", |
| 499 | [HIBERNATION_SHUTDOWN] = "shutdown", |
| 500 | [HIBERNATION_REBOOT] = "reboot", |
| 501 | [HIBERNATION_TEST] = "test", |
| 502 | [HIBERNATION_TESTPROC] = "testproc", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | }; |
| 504 | |
| 505 | /** |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 506 | * disk - Control hibernation mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | * |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 508 | * Suspend-to-disk can be handled in several ways. We have a few options |
| 509 | * 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] | 510 | * or other hibernation_ops), powering off the system or rebooting the |
| 511 | * system (for testing) as well as the two test modes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | * |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 513 | * 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] | 514 | * encoded by the presence of hibernation_ops). However, the user may |
| 515 | * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the |
| 516 | * test modes, 'test' or 'testproc'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | * |
| 518 | * show() will display what the mode is currently set to. |
| 519 | * store() will accept one of |
| 520 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | * 'platform' |
| 522 | * 'shutdown' |
| 523 | * 'reboot' |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 524 | * 'test' |
| 525 | * 'testproc' |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | * |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 527 | * It will only change to 'platform' if the system |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 528 | * supports it (as determined by having hibernation_ops). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | */ |
| 530 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 531 | static ssize_t disk_show(struct kset *kset, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 533 | int i; |
| 534 | char *start = buf; |
| 535 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 536 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
| 537 | if (!hibernation_modes[i]) |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 538 | continue; |
| 539 | switch (i) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 540 | case HIBERNATION_SHUTDOWN: |
| 541 | case HIBERNATION_REBOOT: |
| 542 | case HIBERNATION_TEST: |
| 543 | case HIBERNATION_TESTPROC: |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 544 | break; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 545 | case HIBERNATION_PLATFORM: |
| 546 | if (hibernation_ops) |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 547 | break; |
| 548 | /* not a valid mode, continue with loop */ |
| 549 | continue; |
| 550 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 551 | if (i == hibernation_mode) |
| 552 | buf += sprintf(buf, "[%s] ", hibernation_modes[i]); |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 553 | else |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 554 | buf += sprintf(buf, "%s ", hibernation_modes[i]); |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 555 | } |
| 556 | buf += sprintf(buf, "\n"); |
| 557 | return buf-start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 561 | static ssize_t disk_store(struct kset *kset, const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
| 563 | int error = 0; |
| 564 | int i; |
| 565 | int len; |
| 566 | char *p; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 567 | int mode = HIBERNATION_INVALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | p = memchr(buf, '\n', n); |
| 570 | len = p ? p - buf : n; |
| 571 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 572 | mutex_lock(&pm_mutex); |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 573 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 574 | if (len == strlen(hibernation_modes[i]) |
| 575 | && !strncmp(buf, hibernation_modes[i], len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | mode = i; |
| 577 | break; |
| 578 | } |
| 579 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 580 | if (mode != HIBERNATION_INVALID) { |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 581 | switch (mode) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 582 | case HIBERNATION_SHUTDOWN: |
| 583 | case HIBERNATION_REBOOT: |
| 584 | case HIBERNATION_TEST: |
| 585 | case HIBERNATION_TESTPROC: |
| 586 | hibernation_mode = mode; |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 587 | break; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 588 | case HIBERNATION_PLATFORM: |
| 589 | if (hibernation_ops) |
| 590 | hibernation_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | else |
| 592 | error = -EINVAL; |
| 593 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 594 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | error = -EINVAL; |
| 596 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 597 | if (!error) |
| 598 | pr_debug("PM: suspend-to-disk mode set to '%s'\n", |
| 599 | hibernation_modes[mode]); |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 600 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | return error ? error : n; |
| 602 | } |
| 603 | |
| 604 | power_attr(disk); |
| 605 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 606 | static ssize_t resume_show(struct kset *kset, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
| 608 | return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), |
| 609 | MINOR(swsusp_resume_device)); |
| 610 | } |
| 611 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 612 | static ssize_t resume_store(struct kset *kset, const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | unsigned int maj, min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | dev_t res; |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 616 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 618 | if (sscanf(buf, "%u:%u", &maj, &min) != 2) |
| 619 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 621 | res = MKDEV(maj,min); |
| 622 | if (maj != MAJOR(res) || min != MINOR(res)) |
| 623 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 625 | mutex_lock(&pm_mutex); |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 626 | swsusp_resume_device = res; |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 627 | mutex_unlock(&pm_mutex); |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 628 | printk("Attempting manual resume\n"); |
| 629 | noresume = 0; |
| 630 | software_resume(); |
| 631 | ret = n; |
Rafael J. Wysocki | 59a49335 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 632 | out: |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 633 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | power_attr(resume); |
| 637 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 638 | static ssize_t image_size_show(struct kset *kset, char *buf) |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 639 | { |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 640 | return sprintf(buf, "%lu\n", image_size); |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 643 | static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n) |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 644 | { |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 645 | unsigned long size; |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 646 | |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 647 | if (sscanf(buf, "%lu", &size) == 1) { |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 648 | image_size = size; |
| 649 | return n; |
| 650 | } |
| 651 | |
| 652 | return -EINVAL; |
| 653 | } |
| 654 | |
| 655 | power_attr(image_size); |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | static struct attribute * g[] = { |
| 658 | &disk_attr.attr, |
| 659 | &resume_attr.attr, |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 660 | &image_size_attr.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | NULL, |
| 662 | }; |
| 663 | |
| 664 | |
| 665 | static struct attribute_group attr_group = { |
| 666 | .attrs = g, |
| 667 | }; |
| 668 | |
| 669 | |
| 670 | static int __init pm_disk_init(void) |
| 671 | { |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 672 | return sysfs_create_group(&power_subsys.kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | core_initcall(pm_disk_init); |
| 676 | |
| 677 | |
| 678 | static int __init resume_setup(char *str) |
| 679 | { |
| 680 | if (noresume) |
| 681 | return 1; |
| 682 | |
| 683 | strncpy( resume_file, str, 255 ); |
| 684 | return 1; |
| 685 | } |
| 686 | |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 687 | static int __init resume_offset_setup(char *str) |
| 688 | { |
| 689 | unsigned long long offset; |
| 690 | |
| 691 | if (noresume) |
| 692 | return 1; |
| 693 | |
| 694 | if (sscanf(str, "%llu", &offset) == 1) |
| 695 | swsusp_resume_block = offset; |
| 696 | |
| 697 | return 1; |
| 698 | } |
| 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | static int __init noresume_setup(char *str) |
| 701 | { |
| 702 | noresume = 1; |
| 703 | return 1; |
| 704 | } |
| 705 | |
| 706 | __setup("noresume", noresume_setup); |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 707 | __setup("resume_offset=", resume_offset_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | __setup("resume=", resume_setup); |