blob: b5f0543ed84d38bd44ceb96cb5efe3d0330d15e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Mortond53d9f12005-07-12 13:58:07 -070019#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070020#include <linux/pm.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070021#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070022#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080023#include <linux/freezer.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "power.h"
26
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int noresume = 0;
29char resume_file[256] = CONFIG_PM_STD_PARTITION;
30dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080031sector_t swsusp_resume_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070033enum {
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
46static int hibernation_mode = HIBERNATION_SHUTDOWN;
47
48struct hibernation_ops *hibernation_ops;
49
50/**
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
53 */
54
55void hibernation_set_ops(struct hibernation_ops *ops)
56{
57 if (ops && !(ops->prepare && ops->enter && ops->finish)) {
58 WARN_ON(1);
59 return;
60 }
61 mutex_lock(&pm_mutex);
62 hibernation_ops = ops;
63 if (ops)
64 hibernation_mode = HIBERNATION_PLATFORM;
65 else if (hibernation_mode == HIBERNATION_PLATFORM)
66 hibernation_mode = HIBERNATION_SHUTDOWN;
67
68 mutex_unlock(&pm_mutex);
69}
70
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/**
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080073 * platform_prepare - prepare the machine for hibernation using the
74 * platform driver if so configured and return an error code if it fails
75 */
76
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070077static int platform_prepare(void)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080078{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070079 return (hibernation_mode == HIBERNATION_PLATFORM && hibernation_ops) ?
80 hibernation_ops->prepare() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080081}
82
83/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070084 * platform_finish - switch the machine to the normal mode of operation
85 * using the platform driver (must be called after platform_prepare())
86 */
87
88static void platform_finish(void)
89{
90 if (hibernation_mode == HIBERNATION_PLATFORM && hibernation_ops)
91 hibernation_ops->finish();
92}
93
94/**
95 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Johannes Bergfe0c9352007-04-30 15:09:51 -070097 * Use the platform driver, if configured so; otherwise try
98 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
100
Johannes Bergfe0c9352007-04-30 15:09:51 -0700101static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700103 switch (hibernation_mode) {
104 case HIBERNATION_TEST:
105 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700106 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700107 case HIBERNATION_SHUTDOWN:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600108 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700110 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600111 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700113 case HIBERNATION_PLATFORM:
114 if (hibernation_ops) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700115 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700116 hibernation_ops->enter();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700117 break;
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600120 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700121 /*
122 * Valid image is on the disk, if we continue we risk serious data
123 * corruption after resume.
124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 printk(KERN_CRIT "Please power me down manually\n");
126 while(1);
127}
128
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800129static void unprepare_processes(void)
130{
131 thaw_processes();
132 pm_restore_console();
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static int prepare_processes(void)
136{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800137 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 pm_prepare_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (freeze_processes()) {
141 error = -EBUSY;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800142 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700144 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700148 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
150
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700151int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int error;
154
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700155 /* The snapshot device should not be opened while we're running */
156 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
157 return -EBUSY;
158
159 /* Allocate memory management structures */
160 error = create_basic_memory_bitmaps();
161 if (error)
162 goto Exit;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700165 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700166 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700168 mutex_lock(&pm_mutex);
169 if (hibernation_mode == HIBERNATION_TESTPROC) {
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800170 printk("swsusp debug: Waiting for 5 seconds.\n");
171 mdelay(5000);
172 goto Thaw;
173 }
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800174
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700175 /* Free memory before shutting down devices. */
176 error = swsusp_shrink_memory();
177 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700178 goto Thaw;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700179
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800180 error = platform_prepare();
181 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700182 goto Thaw;
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800183
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700184 suspend_console();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700185 error = device_suspend(PMSG_FREEZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (error) {
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800187 printk(KERN_ERR "PM: Some devices failed to suspend\n");
188 goto Resume_devices;
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800189 }
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800190 error = disable_nonboot_cpus();
191 if (error)
192 goto Enable_cpus;
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800193
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700194 if (hibernation_mode == HIBERNATION_TEST) {
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800195 printk("swsusp debug: Waiting for 5 seconds.\n");
196 mdelay(5000);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800197 goto Enable_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 pr_debug("PM: snapshotting memory.\n");
201 in_suspend = 1;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800202 error = swsusp_suspend();
203 if (error)
204 goto Enable_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (in_suspend) {
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800207 enable_nonboot_cpus();
208 platform_finish();
Rafael J. Wysocki0245b3e2005-10-30 15:00:01 -0800209 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700210 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 pr_debug("PM: writing image.\n");
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800212 error = swsusp_write();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700214 power_down();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700215 else {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700216 swsusp_free();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700217 goto Thaw;
Pavel Machek99dc7d62005-09-03 15:57:05 -0700218 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800219 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800221 }
Pavel Machek99dc7d62005-09-03 15:57:05 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 swsusp_free();
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800224 Enable_cpus:
225 enable_nonboot_cpus();
226 Resume_devices:
227 platform_finish();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700228 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700229 resume_console();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800230 Thaw:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700231 mutex_unlock(&pm_mutex);
Pavel Machek99dc7d62005-09-03 15:57:05 -0700232 unprepare_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700233 Finish:
234 free_basic_memory_bitmaps();
235 Exit:
236 atomic_inc(&snapshot_device_available);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return error;
238}
239
240
241/**
242 * software_resume - Resume from a saved image.
243 *
244 * Called as a late_initcall (so all devices are discovered and
245 * initialized), we call swsusp to see if we have a saved image or not.
246 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700247 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * Otherwise, we fail gracefully and return to the normally
249 * scheduled program.
250 *
251 */
252
253static int software_resume(void)
254{
255 int error;
256
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800257 mutex_lock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700258 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700259 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800260 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700261 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700262 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700263 swsusp_resume_device = name_to_dev_t(resume_file);
264 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
265 } else {
266 pr_debug("swsusp: Resume From Partition %d:%d\n",
267 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
268 }
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (noresume) {
271 /**
272 * FIXME: If noresume is specified, we need to find the partition
273 * and reset it back to normal swap space.
274 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800275 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
277 }
278
279 pr_debug("PM: Checking swsusp image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800280 error = swsusp_check();
281 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700282 goto Unlock;
283
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700284 /* The snapshot device should not be opened while we're running */
285 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
286 error = -EBUSY;
287 goto Unlock;
288 }
289
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700290 error = create_basic_memory_bitmaps();
291 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700292 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800295 error = prepare_processes();
296 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 swsusp_close();
Li Shaohua5a72e042005-06-25 14:55:06 -0700298 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 pr_debug("PM: Reading swsusp image.\n");
302
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800303 error = swsusp_read();
304 if (error) {
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800305 swsusp_free();
306 goto Thaw;
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 pr_debug("PM: Preparing devices for restore.\n");
310
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700311 suspend_console();
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800312 error = device_suspend(PMSG_PRETHAW);
313 if (error)
314 goto Free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800316 error = disable_nonboot_cpus();
317 if (!error)
318 swsusp_resume();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800320 enable_nonboot_cpus();
321 Free:
322 swsusp_free();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700323 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700324 resume_console();
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800325 Thaw:
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800326 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unprepare_processes();
328 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700329 free_basic_memory_bitmaps();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700330 Finish:
331 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700332 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700333 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800334 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 pr_debug("PM: Resume from disk failed.\n");
336 return 0;
337}
338
339late_initcall(software_resume);
340
341
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700342static const char * const hibernation_modes[] = {
343 [HIBERNATION_PLATFORM] = "platform",
344 [HIBERNATION_SHUTDOWN] = "shutdown",
345 [HIBERNATION_REBOOT] = "reboot",
346 [HIBERNATION_TEST] = "test",
347 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348};
349
350/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700351 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700353 * Suspend-to-disk can be handled in several ways. We have a few options
354 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700355 * or other hibernation_ops), powering off the system or rebooting the
356 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700358 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700359 * encoded by the presence of hibernation_ops). However, the user may
360 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
361 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 *
363 * show() will display what the mode is currently set to.
364 * store() will accept one of
365 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * 'platform'
367 * 'shutdown'
368 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700369 * 'test'
370 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700372 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700373 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
375
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700376static ssize_t disk_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700378 int i;
379 char *start = buf;
380
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700381 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
382 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700383 continue;
384 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700385 case HIBERNATION_SHUTDOWN:
386 case HIBERNATION_REBOOT:
387 case HIBERNATION_TEST:
388 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700389 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700390 case HIBERNATION_PLATFORM:
391 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700392 break;
393 /* not a valid mode, continue with loop */
394 continue;
395 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700396 if (i == hibernation_mode)
397 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700398 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700399 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700400 }
401 buf += sprintf(buf, "\n");
402 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700406static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 int error = 0;
409 int i;
410 int len;
411 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700412 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 p = memchr(buf, '\n', n);
415 len = p ? p - buf : n;
416
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800417 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700418 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
419 if (!strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 mode = i;
421 break;
422 }
423 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700424 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700425 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700426 case HIBERNATION_SHUTDOWN:
427 case HIBERNATION_REBOOT:
428 case HIBERNATION_TEST:
429 case HIBERNATION_TESTPROC:
430 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700431 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700432 case HIBERNATION_PLATFORM:
433 if (hibernation_ops)
434 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 else
436 error = -EINVAL;
437 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700438 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 error = -EINVAL;
440
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700441 if (!error)
442 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
443 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800444 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return error ? error : n;
446}
447
448power_attr(disk);
449
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700450static ssize_t resume_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
453 MINOR(swsusp_resume_device));
454}
455
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700456static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800460 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Andrew Mortona5762192006-01-06 00:09:50 -0800462 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
463 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Andrew Mortona5762192006-01-06 00:09:50 -0800465 res = MKDEV(maj,min);
466 if (maj != MAJOR(res) || min != MINOR(res))
467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800469 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800470 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800471 mutex_unlock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800472 printk("Attempting manual resume\n");
473 noresume = 0;
474 software_resume();
475 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800476 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800477 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480power_attr(resume);
481
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700482static ssize_t image_size_show(struct kset *kset, char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800483{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800484 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800485}
486
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700487static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800488{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800489 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800490
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800491 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800492 image_size = size;
493 return n;
494 }
495
496 return -EINVAL;
497}
498
499power_attr(image_size);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static struct attribute * g[] = {
502 &disk_attr.attr,
503 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800504 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 NULL,
506};
507
508
509static struct attribute_group attr_group = {
510 .attrs = g,
511};
512
513
514static int __init pm_disk_init(void)
515{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700516 return sysfs_create_group(&power_subsys.kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519core_initcall(pm_disk_init);
520
521
522static int __init resume_setup(char *str)
523{
524 if (noresume)
525 return 1;
526
527 strncpy( resume_file, str, 255 );
528 return 1;
529}
530
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800531static int __init resume_offset_setup(char *str)
532{
533 unsigned long long offset;
534
535 if (noresume)
536 return 1;
537
538 if (sscanf(str, "%llu", &offset) == 1)
539 swsusp_resume_block = offset;
540
541 return 1;
542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544static int __init noresume_setup(char *str)
545{
546 noresume = 1;
547 return 1;
548}
549
550__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800551__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552__setup("resume=", resume_setup);