blob: 555c0f0b2f7eaa20110b57064cb5f701b4e764df [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. Wysockib3dac3b2007-10-18 03:04:43 -070048static struct platform_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
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070055void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070056{
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070057 if (ops && !(ops->start && ops->pre_snapshot && ops->finish
58 && ops->prepare && ops->enter && ops->pre_restore
59 && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070060 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. Wysocki74f270a2007-10-18 03:04:42 -070073/**
74 * platform_start - tell the platform driver that we're starting
75 * hibernation
76 */
77
78static int platform_start(int platform_mode)
79{
80 return (platform_mode && hibernation_ops) ?
81 hibernation_ops->start() : 0;
82}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070085 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080086 * platform driver if so configured and return an error code if it fails
87 */
88
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070089static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080090{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070091 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070092 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080093}
94
95/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070096 * platform_finish - switch the machine to the normal mode of operation
97 * using the platform driver (must be called after platform_prepare())
98 */
99
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700100static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700101{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700102 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700103 hibernation_ops->finish();
104}
105
106/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700107 * platform_pre_restore - prepare the platform for the restoration from a
108 * hibernation image. If the restore fails after this function has been
109 * called, platform_restore_cleanup() must be called.
110 */
111
112static int platform_pre_restore(int platform_mode)
113{
114 return (platform_mode && hibernation_ops) ?
115 hibernation_ops->pre_restore() : 0;
116}
117
118/**
119 * platform_restore_cleanup - switch the platform to the normal mode of
120 * operation after a failing restore. If platform_pre_restore() has been
121 * called before the failing restore, this function must be called too,
122 * regardless of the result of platform_pre_restore().
123 */
124
125static void platform_restore_cleanup(int platform_mode)
126{
127 if (platform_mode && hibernation_ops)
128 hibernation_ops->restore_cleanup();
129}
130
131/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700132 * hibernation_snapshot - quiesce devices and create the hibernation
133 * snapshot image.
134 * @platform_mode - if set, use the platform driver, if available, to
135 * prepare the platform frimware for the power transition.
136 *
137 * Must be called with pm_mutex held
138 */
139
140int hibernation_snapshot(int platform_mode)
141{
142 int error;
143
144 /* Free memory before shutting down devices. */
145 error = swsusp_shrink_memory();
146 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700147 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700148
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700149 error = platform_start(platform_mode);
150 if (error)
151 return error;
152
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700153 suspend_console();
154 error = device_suspend(PMSG_FREEZE);
155 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700156 goto Resume_console;
157
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700158 error = platform_pre_snapshot(platform_mode);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700159 if (error)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700160 goto Resume_devices;
161
162 error = disable_nonboot_cpus();
163 if (!error) {
164 if (hibernation_mode != HIBERNATION_TEST) {
165 in_suspend = 1;
166 error = swsusp_suspend();
167 /* Control returns here after successful restore */
168 } else {
169 printk("swsusp debug: Waiting for 5 seconds.\n");
170 mdelay(5000);
171 }
172 }
173 enable_nonboot_cpus();
174 Resume_devices:
175 platform_finish(platform_mode);
176 device_resume();
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700177 Resume_console:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700178 resume_console();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700179 return error;
180}
181
182/**
183 * hibernation_restore - quiesce devices and restore the hibernation
184 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700185 * @platform_mode - if set, use the platform driver, if available, to
186 * prepare the platform frimware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700187 *
188 * Must be called with pm_mutex held
189 */
190
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700191int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700192{
193 int error;
194
195 pm_prepare_console();
196 suspend_console();
197 error = device_suspend(PMSG_PRETHAW);
198 if (error)
199 goto Finish;
200
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700201 error = platform_pre_restore(platform_mode);
202 if (!error) {
203 error = disable_nonboot_cpus();
204 if (!error)
205 error = swsusp_resume();
206 enable_nonboot_cpus();
207 }
208 platform_restore_cleanup(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700209 device_resume();
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700210 Finish:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700211 resume_console();
212 pm_restore_console();
213 return error;
214}
215
216/**
217 * hibernation_platform_enter - enter the hibernation state using the
218 * platform driver (if available)
219 */
220
221int hibernation_platform_enter(void)
222{
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700223 int error;
224
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700225 if (hibernation_ops) {
226 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700227 /*
228 * We have cancelled the power transition by running
229 * hibernation_ops->finish() before saving the image, so we
230 * should let the firmware know that we're going to enter the
231 * sleep state after all
232 */
233 error = hibernation_ops->prepare();
Rafael J. Wysocki58b3b712007-07-26 16:29:55 +0200234 sysdev_shutdown();
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700235 if (!error)
236 error = hibernation_ops->enter();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700237 } else {
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700238 error = -ENOSYS;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700239 }
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700240 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700241}
242
243/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700244 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
Johannes Bergfe0c9352007-04-30 15:09:51 -0700246 * Use the platform driver, if configured so; otherwise try
247 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
249
Johannes Bergfe0c9352007-04-30 15:09:51 -0700250static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700252 switch (hibernation_mode) {
253 case HIBERNATION_TEST:
254 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700255 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700256 case HIBERNATION_SHUTDOWN:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600257 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700259 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600260 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700262 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700263 hibernation_platform_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600265 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700266 /*
267 * Valid image is on the disk, if we continue we risk serious data
268 * corruption after resume.
269 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 printk(KERN_CRIT "Please power me down manually\n");
271 while(1);
272}
273
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800274static void unprepare_processes(void)
275{
276 thaw_processes();
277 pm_restore_console();
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static int prepare_processes(void)
281{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800282 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 pm_prepare_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (freeze_processes()) {
286 error = -EBUSY;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800287 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700289 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700293 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
295
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700296int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 int error;
299
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700300 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700301 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700302 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
303 error = -EBUSY;
304 goto Unlock;
305 }
306
307 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
308 if (error)
309 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700310
311 /* Allocate memory management structures */
312 error = create_basic_memory_bitmaps();
313 if (error)
314 goto Exit;
315
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700316 printk("Syncing filesystems ... ");
317 sys_sync();
318 printk("done.\n");
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700321 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700322 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700324 if (hibernation_mode == HIBERNATION_TESTPROC) {
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800325 printk("swsusp debug: Waiting for 5 seconds.\n");
326 mdelay(5000);
327 goto Thaw;
328 }
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700329 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
330 if (in_suspend && !error) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700331 unsigned int flags = 0;
332
333 if (hibernation_mode == HIBERNATION_PLATFORM)
334 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700336 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700337 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700339 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800340 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700342 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800343 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800344 Thaw:
Pavel Machek99dc7d62005-09-03 15:57:05 -0700345 unprepare_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700346 Finish:
347 free_basic_memory_bitmaps();
348 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700349 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700350 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700351 Unlock:
352 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return error;
354}
355
356
357/**
358 * software_resume - Resume from a saved image.
359 *
360 * Called as a late_initcall (so all devices are discovered and
361 * initialized), we call swsusp to see if we have a saved image or not.
362 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700363 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * Otherwise, we fail gracefully and return to the normally
365 * scheduled program.
366 *
367 */
368
369static int software_resume(void)
370{
371 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700372 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800374 mutex_lock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700375 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700376 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800377 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700378 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700379 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700380 swsusp_resume_device = name_to_dev_t(resume_file);
381 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
382 } else {
383 pr_debug("swsusp: Resume From Partition %d:%d\n",
384 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (noresume) {
388 /**
389 * FIXME: If noresume is specified, we need to find the partition
390 * and reset it back to normal swap space.
391 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800392 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return 0;
394 }
395
396 pr_debug("PM: Checking swsusp image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800397 error = swsusp_check();
398 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700399 goto Unlock;
400
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700401 /* The snapshot device should not be opened while we're running */
402 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
403 error = -EBUSY;
404 goto Unlock;
405 }
406
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700407 error = create_basic_memory_bitmaps();
408 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700409 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800412 error = prepare_processes();
413 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 swsusp_close();
Li Shaohua5a72e042005-06-25 14:55:06 -0700415 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
418 pr_debug("PM: Reading swsusp image.\n");
419
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700420 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800421 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700422 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800424 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700425 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unprepare_processes();
427 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700428 free_basic_memory_bitmaps();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700429 Finish:
430 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700431 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700432 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800433 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700435 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438late_initcall(software_resume);
439
440
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700441static const char * const hibernation_modes[] = {
442 [HIBERNATION_PLATFORM] = "platform",
443 [HIBERNATION_SHUTDOWN] = "shutdown",
444 [HIBERNATION_REBOOT] = "reboot",
445 [HIBERNATION_TEST] = "test",
446 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447};
448
449/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700450 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700452 * Suspend-to-disk can be handled in several ways. We have a few options
453 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700454 * or other hibernation_ops), powering off the system or rebooting the
455 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700457 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700458 * encoded by the presence of hibernation_ops). However, the user may
459 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
460 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *
462 * show() will display what the mode is currently set to.
463 * store() will accept one of
464 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * 'platform'
466 * 'shutdown'
467 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700468 * 'test'
469 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700471 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700472 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
474
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700475static ssize_t disk_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700477 int i;
478 char *start = buf;
479
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700480 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
481 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700482 continue;
483 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700484 case HIBERNATION_SHUTDOWN:
485 case HIBERNATION_REBOOT:
486 case HIBERNATION_TEST:
487 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700488 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700489 case HIBERNATION_PLATFORM:
490 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700491 break;
492 /* not a valid mode, continue with loop */
493 continue;
494 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700495 if (i == hibernation_mode)
496 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700497 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700498 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700499 }
500 buf += sprintf(buf, "\n");
501 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700505static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 int error = 0;
508 int i;
509 int len;
510 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700511 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 p = memchr(buf, '\n', n);
514 len = p ? p - buf : n;
515
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800516 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700517 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700518 if (len == strlen(hibernation_modes[i])
519 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 mode = i;
521 break;
522 }
523 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700524 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700525 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700526 case HIBERNATION_SHUTDOWN:
527 case HIBERNATION_REBOOT:
528 case HIBERNATION_TEST:
529 case HIBERNATION_TESTPROC:
530 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700531 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700532 case HIBERNATION_PLATFORM:
533 if (hibernation_ops)
534 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 else
536 error = -EINVAL;
537 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700538 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 error = -EINVAL;
540
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700541 if (!error)
542 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
543 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800544 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return error ? error : n;
546}
547
548power_attr(disk);
549
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700550static ssize_t resume_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
553 MINOR(swsusp_resume_device));
554}
555
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700556static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800560 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Andrew Mortona5762192006-01-06 00:09:50 -0800562 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
563 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Andrew Mortona5762192006-01-06 00:09:50 -0800565 res = MKDEV(maj,min);
566 if (maj != MAJOR(res) || min != MINOR(res))
567 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800569 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800570 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800571 mutex_unlock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800572 printk("Attempting manual resume\n");
573 noresume = 0;
574 software_resume();
575 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800576 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580power_attr(resume);
581
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700582static ssize_t image_size_show(struct kset *kset, char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800583{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800584 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800585}
586
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700587static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800588{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800589 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800590
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800591 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800592 image_size = size;
593 return n;
594 }
595
596 return -EINVAL;
597}
598
599power_attr(image_size);
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static struct attribute * g[] = {
602 &disk_attr.attr,
603 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800604 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 NULL,
606};
607
608
609static struct attribute_group attr_group = {
610 .attrs = g,
611};
612
613
614static int __init pm_disk_init(void)
615{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700616 return sysfs_create_group(&power_subsys.kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619core_initcall(pm_disk_init);
620
621
622static int __init resume_setup(char *str)
623{
624 if (noresume)
625 return 1;
626
627 strncpy( resume_file, str, 255 );
628 return 1;
629}
630
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800631static int __init resume_offset_setup(char *str)
632{
633 unsigned long long offset;
634
635 if (noresume)
636 return 1;
637
638 if (sscanf(str, "%llu", &offset) == 1)
639 swsusp_resume_block = offset;
640
641 return 1;
642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644static int __init noresume_setup(char *str)
645{
646 noresume = 1;
647 return 1;
648}
649
650__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800651__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652__setup("resume=", resume_setup);