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 | |
| 33 | /** |
Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 34 | * platform_prepare - prepare the machine for hibernation using the |
| 35 | * platform driver if so configured and return an error code if it fails |
| 36 | */ |
| 37 | |
| 38 | static inline int platform_prepare(void) |
| 39 | { |
| 40 | int error = 0; |
| 41 | |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame^] | 42 | switch (pm_disk_mode) { |
| 43 | case PM_DISK_TEST: |
| 44 | case PM_DISK_TESTPROC: |
| 45 | case PM_DISK_SHUTDOWN: |
| 46 | case PM_DISK_REBOOT: |
| 47 | break; |
| 48 | default: |
Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 49 | if (pm_ops && pm_ops->prepare) |
| 50 | error = pm_ops->prepare(PM_SUSPEND_DISK); |
| 51 | } |
| 52 | return error; |
| 53 | } |
| 54 | |
| 55 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * power_down - Shut machine down for hibernate. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame^] | 58 | * Use the platform driver, if configured so; otherwise try |
| 59 | * to power off or reboot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame^] | 62 | static void power_down(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame^] | 64 | switch (pm_disk_mode) { |
| 65 | case PM_DISK_TEST: |
| 66 | case PM_DISK_TESTPROC: |
| 67 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | case PM_DISK_SHUTDOWN: |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 69 | kernel_power_off(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | break; |
| 71 | case PM_DISK_REBOOT: |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 72 | kernel_restart(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | break; |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame^] | 74 | default: |
| 75 | if (pm_ops && pm_ops->enter) { |
| 76 | kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK); |
| 77 | pm_ops->enter(PM_SUSPEND_DISK); |
| 78 | break; |
| 79 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 81 | kernel_halt(); |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame^] | 82 | /* |
| 83 | * Valid image is on the disk, if we continue we risk serious data |
| 84 | * corruption after resume. |
| 85 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | printk(KERN_CRIT "Please power me down manually\n"); |
| 87 | while(1); |
| 88 | } |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | static inline void platform_finish(void) |
| 91 | { |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame^] | 92 | switch (pm_disk_mode) { |
| 93 | case PM_DISK_TEST: |
| 94 | case PM_DISK_TESTPROC: |
| 95 | case PM_DISK_SHUTDOWN: |
| 96 | case PM_DISK_REBOOT: |
| 97 | break; |
| 98 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (pm_ops && pm_ops->finish) |
| 100 | pm_ops->finish(PM_SUSPEND_DISK); |
| 101 | } |
| 102 | } |
| 103 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 104 | static void unprepare_processes(void) |
| 105 | { |
| 106 | thaw_processes(); |
| 107 | pm_restore_console(); |
| 108 | } |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | static int prepare_processes(void) |
| 111 | { |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 112 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | pm_prepare_console(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (freeze_processes()) { |
| 116 | error = -EBUSY; |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 117 | unprepare_processes(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 119 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /** |
David Brownell | f1cc0a8 | 2006-08-14 23:11:08 -0700 | [diff] [blame] | 123 | * pm_suspend_disk - The granpappy of hibernation power management. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | * |
| 125 | * If we're going through the firmware, then get it over with quickly. |
| 126 | * |
| 127 | * If not, then call swsusp to do its thing, then figure out how |
| 128 | * to power down the system. |
| 129 | */ |
| 130 | |
| 131 | int pm_suspend_disk(void) |
| 132 | { |
| 133 | int error; |
| 134 | |
| 135 | error = prepare_processes(); |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 136 | if (error) |
| 137 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 139 | if (pm_disk_mode == PM_DISK_TESTPROC) { |
| 140 | printk("swsusp debug: Waiting for 5 seconds.\n"); |
| 141 | mdelay(5000); |
| 142 | goto Thaw; |
| 143 | } |
| 144 | /* Free memory before shutting down devices. */ |
| 145 | error = swsusp_shrink_memory(); |
| 146 | if (error) |
| 147 | goto Thaw; |
| 148 | |
| 149 | error = platform_prepare(); |
| 150 | if (error) |
| 151 | goto Thaw; |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 152 | |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 153 | suspend_console(); |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 154 | error = device_suspend(PMSG_FREEZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if (error) { |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 156 | printk(KERN_ERR "PM: Some devices failed to suspend\n"); |
| 157 | goto Resume_devices; |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 158 | } |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 159 | error = disable_nonboot_cpus(); |
| 160 | if (error) |
| 161 | goto Enable_cpus; |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 162 | |
| 163 | if (pm_disk_mode == PM_DISK_TEST) { |
| 164 | printk("swsusp debug: Waiting for 5 seconds.\n"); |
| 165 | mdelay(5000); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 166 | goto Enable_cpus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | pr_debug("PM: snapshotting memory.\n"); |
| 170 | in_suspend = 1; |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 171 | error = swsusp_suspend(); |
| 172 | if (error) |
| 173 | goto Enable_cpus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | if (in_suspend) { |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 176 | enable_nonboot_cpus(); |
| 177 | platform_finish(); |
Rafael J. Wysocki | 0245b3e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 178 | device_resume(); |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 179 | resume_console(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | pr_debug("PM: writing image.\n"); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 181 | error = swsusp_write(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | if (!error) |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame^] | 183 | power_down(); |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 184 | else { |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 185 | swsusp_free(); |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 186 | goto Thaw; |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 187 | } |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 188 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | pr_debug("PM: Image restored successfully.\n"); |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 190 | } |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | swsusp_free(); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 193 | Enable_cpus: |
| 194 | enable_nonboot_cpus(); |
| 195 | Resume_devices: |
| 196 | platform_finish(); |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 197 | device_resume(); |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 198 | resume_console(); |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 199 | Thaw: |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 200 | unprepare_processes(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | return error; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /** |
| 206 | * software_resume - Resume from a saved image. |
| 207 | * |
| 208 | * Called as a late_initcall (so all devices are discovered and |
| 209 | * initialized), we call swsusp to see if we have a saved image or not. |
| 210 | * If so, we quiesce devices, the restore the saved image. We will |
| 211 | * return above (in pm_suspend_disk() ) if everything goes well. |
| 212 | * Otherwise, we fail gracefully and return to the normally |
| 213 | * scheduled program. |
| 214 | * |
| 215 | */ |
| 216 | |
| 217 | static int software_resume(void) |
| 218 | { |
| 219 | int error; |
| 220 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 221 | mutex_lock(&pm_mutex); |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 222 | if (!swsusp_resume_device) { |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 223 | if (!strlen(resume_file)) { |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 224 | mutex_unlock(&pm_mutex); |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 225 | return -ENOENT; |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 226 | } |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 227 | swsusp_resume_device = name_to_dev_t(resume_file); |
| 228 | pr_debug("swsusp: Resume From Partition %s\n", resume_file); |
| 229 | } else { |
| 230 | pr_debug("swsusp: Resume From Partition %d:%d\n", |
| 231 | MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); |
| 232 | } |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | if (noresume) { |
| 235 | /** |
| 236 | * FIXME: If noresume is specified, we need to find the partition |
| 237 | * and reset it back to normal swap space. |
| 238 | */ |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 239 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | pr_debug("PM: Checking swsusp image.\n"); |
| 244 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 245 | error = swsusp_check(); |
| 246 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | goto Done; |
| 248 | |
| 249 | pr_debug("PM: Preparing processes for restore.\n"); |
| 250 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 251 | error = prepare_processes(); |
| 252 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | swsusp_close(); |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 254 | goto Done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | pr_debug("PM: Reading swsusp image.\n"); |
| 258 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 259 | error = swsusp_read(); |
| 260 | if (error) { |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 261 | swsusp_free(); |
| 262 | goto Thaw; |
| 263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | pr_debug("PM: Preparing devices for restore.\n"); |
| 266 | |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 267 | suspend_console(); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 268 | error = device_suspend(PMSG_PRETHAW); |
| 269 | if (error) |
| 270 | goto Free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 272 | error = disable_nonboot_cpus(); |
| 273 | if (!error) |
| 274 | swsusp_resume(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 276 | enable_nonboot_cpus(); |
| 277 | Free: |
| 278 | swsusp_free(); |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 279 | device_resume(); |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 280 | resume_console(); |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 281 | Thaw: |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 282 | printk(KERN_ERR "PM: Restore failed, recovering.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | unprepare_processes(); |
| 284 | Done: |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 285 | /* For success case, the suspend path will release the lock */ |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 286 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | pr_debug("PM: Resume from disk failed.\n"); |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | late_initcall(software_resume); |
| 292 | |
| 293 | |
Andreas Mohr | 3b364b8 | 2006-06-25 05:47:56 -0700 | [diff] [blame] | 294 | static const char * const pm_disk_modes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | [PM_DISK_FIRMWARE] = "firmware", |
| 296 | [PM_DISK_PLATFORM] = "platform", |
| 297 | [PM_DISK_SHUTDOWN] = "shutdown", |
| 298 | [PM_DISK_REBOOT] = "reboot", |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 299 | [PM_DISK_TEST] = "test", |
| 300 | [PM_DISK_TESTPROC] = "testproc", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | /** |
| 304 | * disk - Control suspend-to-disk mode |
| 305 | * |
| 306 | * Suspend-to-disk can be handled in several ways. The greatest |
| 307 | * distinction is who writes memory to disk - the firmware or the OS. |
| 308 | * If the firmware does it, we assume that it also handles suspending |
| 309 | * the system. |
| 310 | * If the OS does it, then we have three options for putting the system |
| 311 | * to sleep - using the platform driver (e.g. ACPI or other PM registers), |
| 312 | * powering off the system or rebooting the system (for testing). |
| 313 | * |
| 314 | * The system will support either 'firmware' or 'platform', and that is |
| 315 | * known a priori (and encoded in pm_ops). But, the user may choose |
| 316 | * 'shutdown' or 'reboot' as alternatives. |
| 317 | * |
| 318 | * show() will display what the mode is currently set to. |
| 319 | * store() will accept one of |
| 320 | * |
| 321 | * 'firmware' |
| 322 | * 'platform' |
| 323 | * 'shutdown' |
| 324 | * 'reboot' |
| 325 | * |
| 326 | * It will only change to 'firmware' or 'platform' if the system |
| 327 | * supports it (as determined from pm_ops->pm_disk_mode). |
| 328 | */ |
| 329 | |
| 330 | static ssize_t disk_show(struct subsystem * subsys, char * buf) |
| 331 | { |
| 332 | return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n) |
| 337 | { |
| 338 | int error = 0; |
| 339 | int i; |
| 340 | int len; |
| 341 | char *p; |
| 342 | suspend_disk_method_t mode = 0; |
| 343 | |
| 344 | p = memchr(buf, '\n', n); |
| 345 | len = p ? p - buf : n; |
| 346 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 347 | mutex_lock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) { |
| 349 | if (!strncmp(buf, pm_disk_modes[i], len)) { |
| 350 | mode = i; |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | if (mode) { |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame^] | 355 | switch (mode) { |
| 356 | case PM_DISK_SHUTDOWN: |
| 357 | case PM_DISK_REBOOT: |
| 358 | case PM_DISK_TEST: |
| 359 | case PM_DISK_TESTPROC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | pm_disk_mode = mode; |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame^] | 361 | break; |
| 362 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | if (pm_ops && pm_ops->enter && |
| 364 | (mode == pm_ops->pm_disk_mode)) |
| 365 | pm_disk_mode = mode; |
| 366 | else |
| 367 | error = -EINVAL; |
| 368 | } |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 369 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | error = -EINVAL; |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | pr_debug("PM: suspend-to-disk mode set to '%s'\n", |
| 374 | pm_disk_modes[mode]); |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 375 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | return error ? error : n; |
| 377 | } |
| 378 | |
| 379 | power_attr(disk); |
| 380 | |
| 381 | static ssize_t resume_show(struct subsystem * subsys, char *buf) |
| 382 | { |
| 383 | return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), |
| 384 | MINOR(swsusp_resume_device)); |
| 385 | } |
| 386 | |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 387 | static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | unsigned int maj, min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | dev_t res; |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 391 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 393 | if (sscanf(buf, "%u:%u", &maj, &min) != 2) |
| 394 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 396 | res = MKDEV(maj,min); |
| 397 | if (maj != MAJOR(res) || min != MINOR(res)) |
| 398 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 400 | mutex_lock(&pm_mutex); |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 401 | swsusp_resume_device = res; |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 402 | mutex_unlock(&pm_mutex); |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 403 | printk("Attempting manual resume\n"); |
| 404 | noresume = 0; |
| 405 | software_resume(); |
| 406 | ret = n; |
Rafael J. Wysocki | 59a49335 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 407 | out: |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 408 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | power_attr(resume); |
| 412 | |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 413 | static ssize_t image_size_show(struct subsystem * subsys, char *buf) |
| 414 | { |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 415 | return sprintf(buf, "%lu\n", image_size); |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n) |
| 419 | { |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 420 | unsigned long size; |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 421 | |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 422 | if (sscanf(buf, "%lu", &size) == 1) { |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 423 | image_size = size; |
| 424 | return n; |
| 425 | } |
| 426 | |
| 427 | return -EINVAL; |
| 428 | } |
| 429 | |
| 430 | power_attr(image_size); |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | static struct attribute * g[] = { |
| 433 | &disk_attr.attr, |
| 434 | &resume_attr.attr, |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 435 | &image_size_attr.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | NULL, |
| 437 | }; |
| 438 | |
| 439 | |
| 440 | static struct attribute_group attr_group = { |
| 441 | .attrs = g, |
| 442 | }; |
| 443 | |
| 444 | |
| 445 | static int __init pm_disk_init(void) |
| 446 | { |
| 447 | return sysfs_create_group(&power_subsys.kset.kobj,&attr_group); |
| 448 | } |
| 449 | |
| 450 | core_initcall(pm_disk_init); |
| 451 | |
| 452 | |
| 453 | static int __init resume_setup(char *str) |
| 454 | { |
| 455 | if (noresume) |
| 456 | return 1; |
| 457 | |
| 458 | strncpy( resume_file, str, 255 ); |
| 459 | return 1; |
| 460 | } |
| 461 | |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 462 | static int __init resume_offset_setup(char *str) |
| 463 | { |
| 464 | unsigned long long offset; |
| 465 | |
| 466 | if (noresume) |
| 467 | return 1; |
| 468 | |
| 469 | if (sscanf(str, "%llu", &offset) == 1) |
| 470 | swsusp_resume_block = offset; |
| 471 | |
| 472 | return 1; |
| 473 | } |
| 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | static int __init noresume_setup(char *str) |
| 476 | { |
| 477 | noresume = 1; |
| 478 | return 1; |
| 479 | } |
| 480 | |
| 481 | __setup("noresume", noresume_setup); |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 482 | __setup("resume_offset=", resume_offset_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | __setup("resume=", resume_setup); |