blob: 47882bfa610e5b0edb23d7dff3dfb61d9e32f4cc [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{
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. Wysocki7777fab2007-07-19 01:47:29 -070077static int platform_prepare(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080078{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070079 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070080 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
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070088static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070089{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070090 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070091 hibernation_ops->finish();
92}
93
94/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070095 * hibernation_snapshot - quiesce devices and create the hibernation
96 * snapshot image.
97 * @platform_mode - if set, use the platform driver, if available, to
98 * prepare the platform frimware for the power transition.
99 *
100 * Must be called with pm_mutex held
101 */
102
103int hibernation_snapshot(int platform_mode)
104{
105 int error;
106
107 /* Free memory before shutting down devices. */
108 error = swsusp_shrink_memory();
109 if (error)
110 goto Finish;
111
112 error = platform_prepare(platform_mode);
113 if (error)
114 goto Finish;
115
116 suspend_console();
117 error = device_suspend(PMSG_FREEZE);
118 if (error)
119 goto Resume_devices;
120
121 error = disable_nonboot_cpus();
122 if (!error) {
123 if (hibernation_mode != HIBERNATION_TEST) {
124 in_suspend = 1;
125 error = swsusp_suspend();
126 /* Control returns here after successful restore */
127 } else {
128 printk("swsusp debug: Waiting for 5 seconds.\n");
129 mdelay(5000);
130 }
131 }
132 enable_nonboot_cpus();
133 Resume_devices:
134 platform_finish(platform_mode);
135 device_resume();
136 resume_console();
137 Finish:
138 return error;
139}
140
141/**
142 * hibernation_restore - quiesce devices and restore the hibernation
143 * snapshot image. If successful, control returns in hibernation_snaphot()
144 *
145 * Must be called with pm_mutex held
146 */
147
148int hibernation_restore(void)
149{
150 int error;
151
152 pm_prepare_console();
153 suspend_console();
154 error = device_suspend(PMSG_PRETHAW);
155 if (error)
156 goto Finish;
157
158 error = disable_nonboot_cpus();
159 if (!error)
160 error = swsusp_resume();
161
162 enable_nonboot_cpus();
163 Finish:
164 device_resume();
165 resume_console();
166 pm_restore_console();
167 return error;
168}
169
170/**
171 * hibernation_platform_enter - enter the hibernation state using the
172 * platform driver (if available)
173 */
174
175int hibernation_platform_enter(void)
176{
177 if (hibernation_ops) {
178 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
179 return hibernation_ops->enter();
180 } else {
181 return -ENOSYS;
182 }
183}
184
185/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700186 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 *
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700188 * Use the platform driver, if configured so; otherwise try
189 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700192static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700194 switch (hibernation_mode) {
195 case HIBERNATION_TEST:
196 case HIBERNATION_TESTPROC:
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700197 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700198 case HIBERNATION_SHUTDOWN:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600199 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700201 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600202 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700204 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700205 hibernation_platform_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600207 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700208 /*
209 * Valid image is on the disk, if we continue we risk serious data
210 * corruption after resume.
211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 printk(KERN_CRIT "Please power me down manually\n");
213 while(1);
214}
215
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800216static void unprepare_processes(void)
217{
218 thaw_processes();
219 pm_restore_console();
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static int prepare_processes(void)
223{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800224 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 pm_prepare_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (freeze_processes()) {
228 error = -EBUSY;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800229 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700231 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700235 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
237
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700238int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 int error;
241
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700242 /* The snapshot device should not be opened while we're running */
243 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
244 return -EBUSY;
245
246 /* Allocate memory management structures */
247 error = create_basic_memory_bitmaps();
248 if (error)
249 goto Exit;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700252 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700253 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700255 mutex_lock(&pm_mutex);
256 if (hibernation_mode == HIBERNATION_TESTPROC) {
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800257 printk("swsusp debug: Waiting for 5 seconds.\n");
258 mdelay(5000);
259 goto Thaw;
260 }
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700261 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
262 if (in_suspend && !error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 pr_debug("PM: writing image.\n");
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800264 error = swsusp_write();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700265 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!error)
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700267 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800268 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700270 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800271 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800272 Thaw:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700273 mutex_unlock(&pm_mutex);
Pavel Machek99dc7d62005-09-03 15:57:05 -0700274 unprepare_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700275 Finish:
276 free_basic_memory_bitmaps();
277 Exit:
278 atomic_inc(&snapshot_device_available);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return error;
280}
281
282
283/**
284 * software_resume - Resume from a saved image.
285 *
286 * Called as a late_initcall (so all devices are discovered and
287 * initialized), we call swsusp to see if we have a saved image or not.
288 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700289 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * Otherwise, we fail gracefully and return to the normally
291 * scheduled program.
292 *
293 */
294
295static int software_resume(void)
296{
297 int error;
298
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800299 mutex_lock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700300 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700301 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800302 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700303 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700304 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700305 swsusp_resume_device = name_to_dev_t(resume_file);
306 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
307 } else {
308 pr_debug("swsusp: Resume From Partition %d:%d\n",
309 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
310 }
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (noresume) {
313 /**
314 * FIXME: If noresume is specified, we need to find the partition
315 * and reset it back to normal swap space.
316 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800317 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319 }
320
321 pr_debug("PM: Checking swsusp image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800322 error = swsusp_check();
323 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700324 goto Unlock;
325
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700326 /* The snapshot device should not be opened while we're running */
327 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
328 error = -EBUSY;
329 goto Unlock;
330 }
331
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700332 error = create_basic_memory_bitmaps();
333 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700334 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800337 error = prepare_processes();
338 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 swsusp_close();
Li Shaohua5a72e042005-06-25 14:55:06 -0700340 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 pr_debug("PM: Reading swsusp image.\n");
344
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800345 error = swsusp_read();
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800346 if (!error)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700347 hibernation_restore();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800349 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700350 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 unprepare_processes();
352 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700353 free_basic_memory_bitmaps();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700354 Finish:
355 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700356 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700357 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800358 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700360 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363late_initcall(software_resume);
364
365
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700366static const char * const hibernation_modes[] = {
367 [HIBERNATION_PLATFORM] = "platform",
368 [HIBERNATION_SHUTDOWN] = "shutdown",
369 [HIBERNATION_REBOOT] = "reboot",
370 [HIBERNATION_TEST] = "test",
371 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373
374/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700375 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700377 * Suspend-to-disk can be handled in several ways. We have a few options
378 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700379 * or other hibernation_ops), powering off the system or rebooting the
380 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700382 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700383 * encoded by the presence of hibernation_ops). However, the user may
384 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
385 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 *
387 * show() will display what the mode is currently set to.
388 * store() will accept one of
389 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 * 'platform'
391 * 'shutdown'
392 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700393 * 'test'
394 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700396 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700397 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
399
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700400static ssize_t disk_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700402 int i;
403 char *start = buf;
404
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700405 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
406 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700407 continue;
408 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700409 case HIBERNATION_SHUTDOWN:
410 case HIBERNATION_REBOOT:
411 case HIBERNATION_TEST:
412 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700413 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700414 case HIBERNATION_PLATFORM:
415 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700416 break;
417 /* not a valid mode, continue with loop */
418 continue;
419 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700420 if (i == hibernation_mode)
421 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700422 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700423 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700424 }
425 buf += sprintf(buf, "\n");
426 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700430static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 int error = 0;
433 int i;
434 int len;
435 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700436 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 p = memchr(buf, '\n', n);
439 len = p ? p - buf : n;
440
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800441 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700442 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700443 if (len == strlen(hibernation_modes[i])
444 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 mode = i;
446 break;
447 }
448 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700449 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700450 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700451 case HIBERNATION_SHUTDOWN:
452 case HIBERNATION_REBOOT:
453 case HIBERNATION_TEST:
454 case HIBERNATION_TESTPROC:
455 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700456 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700457 case HIBERNATION_PLATFORM:
458 if (hibernation_ops)
459 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 else
461 error = -EINVAL;
462 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700463 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 error = -EINVAL;
465
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700466 if (!error)
467 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
468 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800469 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return error ? error : n;
471}
472
473power_attr(disk);
474
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700475static ssize_t resume_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
478 MINOR(swsusp_resume_device));
479}
480
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700481static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800485 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Andrew Mortona5762192006-01-06 00:09:50 -0800487 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Andrew Mortona5762192006-01-06 00:09:50 -0800490 res = MKDEV(maj,min);
491 if (maj != MAJOR(res) || min != MINOR(res))
492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800494 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800495 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800496 mutex_unlock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800497 printk("Attempting manual resume\n");
498 noresume = 0;
499 software_resume();
500 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800501 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800502 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505power_attr(resume);
506
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700507static ssize_t image_size_show(struct kset *kset, char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800508{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800509 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800510}
511
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700512static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800513{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800514 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800515
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800516 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800517 image_size = size;
518 return n;
519 }
520
521 return -EINVAL;
522}
523
524power_attr(image_size);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526static struct attribute * g[] = {
527 &disk_attr.attr,
528 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800529 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 NULL,
531};
532
533
534static struct attribute_group attr_group = {
535 .attrs = g,
536};
537
538
539static int __init pm_disk_init(void)
540{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700541 return sysfs_create_group(&power_subsys.kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544core_initcall(pm_disk_init);
545
546
547static int __init resume_setup(char *str)
548{
549 if (noresume)
550 return 1;
551
552 strncpy( resume_file, str, 255 );
553 return 1;
554}
555
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800556static int __init resume_offset_setup(char *str)
557{
558 unsigned long long offset;
559
560 if (noresume)
561 return 1;
562
563 if (sscanf(str, "%llu", &offset) == 1)
564 swsusp_resume_block = offset;
565
566 return 1;
567}
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569static int __init noresume_setup(char *str)
570{
571 noresume = 1;
572 return 1;
573}
574
575__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800576__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577__setup("resume=", resume_setup);