blob: eb72255b5c86049d1c31f0c9d6ca09b5b653fdca [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
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070048static struct hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070049
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{
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070057 if (ops && !(ops->prepare && ops->enter && ops->finish
58 && ops->pre_restore && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070059 WARN_ON(1);
60 return;
61 }
62 mutex_lock(&pm_mutex);
63 hibernation_ops = ops;
64 if (ops)
65 hibernation_mode = HIBERNATION_PLATFORM;
66 else if (hibernation_mode == HIBERNATION_PLATFORM)
67 hibernation_mode = HIBERNATION_SHUTDOWN;
68
69 mutex_unlock(&pm_mutex);
70}
71
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/**
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080074 * platform_prepare - prepare the machine for hibernation using the
75 * platform driver if so configured and return an error code if it fails
76 */
77
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070078static int platform_prepare(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080079{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070080 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070081 hibernation_ops->prepare() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080082}
83
84/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070085 * platform_finish - switch the machine to the normal mode of operation
86 * using the platform driver (must be called after platform_prepare())
87 */
88
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070089static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070090{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070091 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070092 hibernation_ops->finish();
93}
94
95/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070096 * platform_pre_restore - prepare the platform for the restoration from a
97 * hibernation image. If the restore fails after this function has been
98 * called, platform_restore_cleanup() must be called.
99 */
100
101static int platform_pre_restore(int platform_mode)
102{
103 return (platform_mode && hibernation_ops) ?
104 hibernation_ops->pre_restore() : 0;
105}
106
107/**
108 * platform_restore_cleanup - switch the platform to the normal mode of
109 * operation after a failing restore. If platform_pre_restore() has been
110 * called before the failing restore, this function must be called too,
111 * regardless of the result of platform_pre_restore().
112 */
113
114static void platform_restore_cleanup(int platform_mode)
115{
116 if (platform_mode && hibernation_ops)
117 hibernation_ops->restore_cleanup();
118}
119
120/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700121 * hibernation_snapshot - quiesce devices and create the hibernation
122 * snapshot image.
123 * @platform_mode - if set, use the platform driver, if available, to
124 * prepare the platform frimware for the power transition.
125 *
126 * Must be called with pm_mutex held
127 */
128
129int hibernation_snapshot(int platform_mode)
130{
131 int error;
132
133 /* Free memory before shutting down devices. */
134 error = swsusp_shrink_memory();
135 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700136 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700137
138 suspend_console();
139 error = device_suspend(PMSG_FREEZE);
140 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700141 goto Resume_console;
142
143 error = platform_prepare(platform_mode);
144 if (error)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700145 goto Resume_devices;
146
147 error = disable_nonboot_cpus();
148 if (!error) {
149 if (hibernation_mode != HIBERNATION_TEST) {
150 in_suspend = 1;
151 error = swsusp_suspend();
152 /* Control returns here after successful restore */
153 } else {
154 printk("swsusp debug: Waiting for 5 seconds.\n");
155 mdelay(5000);
156 }
157 }
158 enable_nonboot_cpus();
159 Resume_devices:
160 platform_finish(platform_mode);
161 device_resume();
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700162 Resume_console:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700163 resume_console();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700164 return error;
165}
166
167/**
168 * hibernation_restore - quiesce devices and restore the hibernation
169 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700170 * @platform_mode - if set, use the platform driver, if available, to
171 * prepare the platform frimware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700172 *
173 * Must be called with pm_mutex held
174 */
175
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700176int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700177{
178 int error;
179
180 pm_prepare_console();
181 suspend_console();
182 error = device_suspend(PMSG_PRETHAW);
183 if (error)
184 goto Finish;
185
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700186 error = platform_pre_restore(platform_mode);
187 if (!error) {
188 error = disable_nonboot_cpus();
189 if (!error)
190 error = swsusp_resume();
191 enable_nonboot_cpus();
192 }
193 platform_restore_cleanup(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700194 device_resume();
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700195 Finish:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700196 resume_console();
197 pm_restore_console();
198 return error;
199}
200
201/**
202 * hibernation_platform_enter - enter the hibernation state using the
203 * platform driver (if available)
204 */
205
206int hibernation_platform_enter(void)
207{
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700208 int error;
209
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700210 if (hibernation_ops) {
211 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700212 /*
213 * We have cancelled the power transition by running
214 * hibernation_ops->finish() before saving the image, so we
215 * should let the firmware know that we're going to enter the
216 * sleep state after all
217 */
218 error = hibernation_ops->prepare();
Rafael J. Wysocki58b3b712007-07-26 16:29:55 +0200219 sysdev_shutdown();
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700220 if (!error)
221 error = hibernation_ops->enter();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700222 } else {
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700223 error = -ENOSYS;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700224 }
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700225 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700226}
227
228/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700229 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *
Johannes Bergfe0c9352007-04-30 15:09:51 -0700231 * Use the platform driver, if configured so; otherwise try
232 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
234
Johannes Bergfe0c9352007-04-30 15:09:51 -0700235static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700237 switch (hibernation_mode) {
238 case HIBERNATION_TEST:
239 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700240 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700241 case HIBERNATION_SHUTDOWN:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600242 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700244 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600245 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700247 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700248 hibernation_platform_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600250 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700251 /*
252 * Valid image is on the disk, if we continue we risk serious data
253 * corruption after resume.
254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 printk(KERN_CRIT "Please power me down manually\n");
256 while(1);
257}
258
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800259static void unprepare_processes(void)
260{
261 thaw_processes();
262 pm_restore_console();
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static int prepare_processes(void)
266{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800267 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 pm_prepare_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (freeze_processes()) {
271 error = -EBUSY;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800272 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700274 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700278 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700281int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 int error;
284
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700285 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700286 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700287 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
288 error = -EBUSY;
289 goto Unlock;
290 }
291
292 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
293 if (error)
294 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700295
296 /* Allocate memory management structures */
297 error = create_basic_memory_bitmaps();
298 if (error)
299 goto Exit;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700302 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700303 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700305 if (hibernation_mode == HIBERNATION_TESTPROC) {
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800306 printk("swsusp debug: Waiting for 5 seconds.\n");
307 mdelay(5000);
308 goto Thaw;
309 }
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700310 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
311 if (in_suspend && !error) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700312 unsigned int flags = 0;
313
314 if (hibernation_mode == HIBERNATION_PLATFORM)
315 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700317 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700318 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700320 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800321 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700323 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800324 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800325 Thaw:
Pavel Machek99dc7d62005-09-03 15:57:05 -0700326 unprepare_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700327 Finish:
328 free_basic_memory_bitmaps();
329 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700330 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700331 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700332 Unlock:
333 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return error;
335}
336
337
338/**
339 * software_resume - Resume from a saved image.
340 *
341 * Called as a late_initcall (so all devices are discovered and
342 * initialized), we call swsusp to see if we have a saved image or not.
343 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700344 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * Otherwise, we fail gracefully and return to the normally
346 * scheduled program.
347 *
348 */
349
350static int software_resume(void)
351{
352 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700353 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800355 mutex_lock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700356 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700357 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800358 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700359 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700360 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700361 swsusp_resume_device = name_to_dev_t(resume_file);
362 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
363 } else {
364 pr_debug("swsusp: Resume From Partition %d:%d\n",
365 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
366 }
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (noresume) {
369 /**
370 * FIXME: If noresume is specified, we need to find the partition
371 * and reset it back to normal swap space.
372 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800373 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375 }
376
377 pr_debug("PM: Checking swsusp image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800378 error = swsusp_check();
379 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700380 goto Unlock;
381
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700382 /* The snapshot device should not be opened while we're running */
383 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
384 error = -EBUSY;
385 goto Unlock;
386 }
387
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700388 error = create_basic_memory_bitmaps();
389 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700390 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800393 error = prepare_processes();
394 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 swsusp_close();
Li Shaohua5a72e042005-06-25 14:55:06 -0700396 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 pr_debug("PM: Reading swsusp image.\n");
400
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700401 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800402 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700403 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800405 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700406 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 unprepare_processes();
408 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700409 free_basic_memory_bitmaps();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700410 Finish:
411 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700412 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700413 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800414 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700416 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419late_initcall(software_resume);
420
421
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700422static const char * const hibernation_modes[] = {
423 [HIBERNATION_PLATFORM] = "platform",
424 [HIBERNATION_SHUTDOWN] = "shutdown",
425 [HIBERNATION_REBOOT] = "reboot",
426 [HIBERNATION_TEST] = "test",
427 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428};
429
430/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700431 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700433 * Suspend-to-disk can be handled in several ways. We have a few options
434 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700435 * or other hibernation_ops), powering off the system or rebooting the
436 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700438 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700439 * encoded by the presence of hibernation_ops). However, the user may
440 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
441 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 *
443 * show() will display what the mode is currently set to.
444 * store() will accept one of
445 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * 'platform'
447 * 'shutdown'
448 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700449 * 'test'
450 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700452 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700453 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 */
455
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700456static ssize_t disk_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700458 int i;
459 char *start = buf;
460
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700461 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
462 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700463 continue;
464 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700465 case HIBERNATION_SHUTDOWN:
466 case HIBERNATION_REBOOT:
467 case HIBERNATION_TEST:
468 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700469 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700470 case HIBERNATION_PLATFORM:
471 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700472 break;
473 /* not a valid mode, continue with loop */
474 continue;
475 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700476 if (i == hibernation_mode)
477 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700478 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700479 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700480 }
481 buf += sprintf(buf, "\n");
482 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700486static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 int error = 0;
489 int i;
490 int len;
491 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700492 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 p = memchr(buf, '\n', n);
495 len = p ? p - buf : n;
496
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800497 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700498 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700499 if (len == strlen(hibernation_modes[i])
500 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 mode = i;
502 break;
503 }
504 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700505 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700506 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700507 case HIBERNATION_SHUTDOWN:
508 case HIBERNATION_REBOOT:
509 case HIBERNATION_TEST:
510 case HIBERNATION_TESTPROC:
511 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700512 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700513 case HIBERNATION_PLATFORM:
514 if (hibernation_ops)
515 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 else
517 error = -EINVAL;
518 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700519 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 error = -EINVAL;
521
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700522 if (!error)
523 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
524 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800525 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return error ? error : n;
527}
528
529power_attr(disk);
530
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700531static ssize_t resume_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
534 MINOR(swsusp_resume_device));
535}
536
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700537static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800541 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Andrew Mortona5762192006-01-06 00:09:50 -0800543 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
544 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Andrew Mortona5762192006-01-06 00:09:50 -0800546 res = MKDEV(maj,min);
547 if (maj != MAJOR(res) || min != MINOR(res))
548 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800550 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800551 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800552 mutex_unlock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800553 printk("Attempting manual resume\n");
554 noresume = 0;
555 software_resume();
556 ret = n;
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800557 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800558 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561power_attr(resume);
562
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700563static ssize_t image_size_show(struct kset *kset, char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800564{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800565 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800566}
567
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700568static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800569{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800570 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800571
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800572 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800573 image_size = size;
574 return n;
575 }
576
577 return -EINVAL;
578}
579
580power_attr(image_size);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static struct attribute * g[] = {
583 &disk_attr.attr,
584 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800585 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 NULL,
587};
588
589
590static struct attribute_group attr_group = {
591 .attrs = g,
592};
593
594
595static int __init pm_disk_init(void)
596{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700597 return sysfs_create_group(&power_subsys.kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600core_initcall(pm_disk_init);
601
602
603static int __init resume_setup(char *str)
604{
605 if (noresume)
606 return 1;
607
608 strncpy( resume_file, str, 255 );
609 return 1;
610}
611
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800612static int __init resume_offset_setup(char *str)
613{
614 unsigned long long offset;
615
616 if (noresume)
617 return 1;
618
619 if (sscanf(str, "%llu", &offset) == 1)
620 swsusp_resume_block = offset;
621
622 return 1;
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static int __init noresume_setup(char *str)
626{
627 noresume = 1;
628 return 1;
629}
630
631__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800632__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633__setup("resume=", resume_setup);