]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/power/disk.c
Merge branch 'suspend' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[net-next-2.6.git] / kernel / power / disk.c
CommitLineData
1da177e4
LT
1/*
2 * kernel/power/disk.c - Suspend-to-disk support.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
7 *
8 * This file is released under the GPLv2.
9 *
10 */
11
12#include <linux/suspend.h>
13#include <linux/syscalls.h>
14#include <linux/reboot.h>
15#include <linux/string.h>
16#include <linux/device.h>
17#include <linux/delay.h>
18#include <linux/fs.h>
d53d9f16 19#include <linux/mount.h>
88d10bba 20#include <linux/pm.h>
97c7801c 21#include <linux/console.h>
e3920fb4 22#include <linux/cpu.h>
7dfb7103 23#include <linux/freezer.h>
d53d9f16 24
1da177e4
LT
25#include "power.h"
26
27
1da177e4 28static int noresume = 0;
47a460d5 29static char resume_file[256] = CONFIG_PM_STD_PARTITION;
1da177e4 30dev_t swsusp_resume_device;
9a154d9d 31sector_t swsusp_resume_block;
1da177e4 32
a3d25c27
RW
33enum {
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
b3dac3b3 48static struct platform_hibernation_ops *hibernation_ops;
a3d25c27
RW
49
50/**
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
53 */
54
b3dac3b3 55void hibernation_set_ops(struct platform_hibernation_ops *ops)
a3d25c27 56{
caea99ef
RW
57 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
58 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
74f270af 59 && ops->restore_cleanup)) {
a3d25c27
RW
60 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
4cc79776
RW
73#ifdef CONFIG_PM_DEBUG
74static void hibernation_debug_sleep(void)
75{
76 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
77 mdelay(5000);
78}
79
80static int hibernation_testmode(int mode)
81{
82 if (hibernation_mode == mode) {
83 hibernation_debug_sleep();
84 return 1;
85 }
86 return 0;
87}
88
89static int hibernation_test(int level)
90{
91 if (pm_test_level == level) {
92 hibernation_debug_sleep();
93 return 1;
94 }
95 return 0;
96}
97#else /* !CONFIG_PM_DEBUG */
98static int hibernation_testmode(int mode) { return 0; }
99static int hibernation_test(int level) { return 0; }
100#endif /* !CONFIG_PM_DEBUG */
101
74f270af 102/**
caea99ef 103 * platform_begin - tell the platform driver that we're starting
74f270af
RW
104 * hibernation
105 */
106
caea99ef 107static int platform_begin(int platform_mode)
74f270af
RW
108{
109 return (platform_mode && hibernation_ops) ?
caea99ef
RW
110 hibernation_ops->begin() : 0;
111}
112
113/**
114 * platform_end - tell the platform driver that we've entered the
115 * working state
116 */
117
118static void platform_end(int platform_mode)
119{
120 if (platform_mode && hibernation_ops)
121 hibernation_ops->end();
74f270af 122}
a3d25c27 123
8a05aac2 124/**
74f270af 125 * platform_pre_snapshot - prepare the machine for hibernation using the
8a05aac2
SS
126 * platform driver if so configured and return an error code if it fails
127 */
128
74f270af 129static int platform_pre_snapshot(int platform_mode)
8a05aac2 130{
7777fab9 131 return (platform_mode && hibernation_ops) ?
74f270af 132 hibernation_ops->pre_snapshot() : 0;
a3d25c27 133}
8a05aac2 134
c7e0831d
RW
135/**
136 * platform_leave - prepare the machine for switching to the normal mode
137 * of operation using the platform driver (called with interrupts disabled)
138 */
139
140static void platform_leave(int platform_mode)
141{
142 if (platform_mode && hibernation_ops)
143 hibernation_ops->leave();
144}
145
a3d25c27
RW
146/**
147 * platform_finish - switch the machine to the normal mode of operation
148 * using the platform driver (must be called after platform_prepare())
149 */
150
7777fab9 151static void platform_finish(int platform_mode)
a3d25c27 152{
7777fab9 153 if (platform_mode && hibernation_ops)
a3d25c27 154 hibernation_ops->finish();
8a05aac2
SS
155}
156
a634cc10
RW
157/**
158 * platform_pre_restore - prepare the platform for the restoration from a
159 * hibernation image. If the restore fails after this function has been
160 * called, platform_restore_cleanup() must be called.
161 */
162
163static int platform_pre_restore(int platform_mode)
164{
165 return (platform_mode && hibernation_ops) ?
166 hibernation_ops->pre_restore() : 0;
167}
168
169/**
170 * platform_restore_cleanup - switch the platform to the normal mode of
171 * operation after a failing restore. If platform_pre_restore() has been
172 * called before the failing restore, this function must be called too,
173 * regardless of the result of platform_pre_restore().
174 */
175
176static void platform_restore_cleanup(int platform_mode)
177{
178 if (platform_mode && hibernation_ops)
179 hibernation_ops->restore_cleanup();
180}
181
c7e0831d
RW
182/**
183 * create_image - freeze devices that need to be frozen with interrupts
184 * off, create the hibernation image and thaw those devices. Control
185 * reappears in this routine after a restore.
186 */
187
47a460d5 188static int create_image(int platform_mode)
c7e0831d
RW
189{
190 int error;
191
192 error = arch_prepare_suspend();
193 if (error)
194 return error;
195
1eede070 196 device_pm_lock();
c7e0831d
RW
197 local_irq_disable();
198 /* At this point, device_suspend() has been called, but *not*
199 * device_power_down(). We *must* call device_power_down() now.
200 * Otherwise, drivers for some devices (e.g. interrupt controllers)
201 * become desynchronized with the actual state of the hardware
202 * at resume time, and evil weirdness ensues.
203 */
204 error = device_power_down(PMSG_FREEZE);
205 if (error) {
23976728
RW
206 printk(KERN_ERR "PM: Some devices failed to power down, "
207 "aborting hibernation\n");
c7e0831d
RW
208 goto Enable_irqs;
209 }
210
4cc79776
RW
211 if (hibernation_test(TEST_CORE))
212 goto Power_up;
213
214 in_suspend = 1;
c7e0831d
RW
215 save_processor_state();
216 error = swsusp_arch_suspend();
217 if (error)
23976728
RW
218 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
219 error);
c7e0831d
RW
220 /* Restore control flow magically appears here */
221 restore_processor_state();
222 if (!in_suspend)
223 platform_leave(platform_mode);
4cc79776 224 Power_up:
c7e0831d
RW
225 /* NOTE: device_power_up() is just a resume() for devices
226 * that suspended with irqs off ... no overall powerup.
227 */
1eede070
RW
228 device_power_up(in_suspend ?
229 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
c7e0831d
RW
230 Enable_irqs:
231 local_irq_enable();
1eede070 232 device_pm_unlock();
c7e0831d
RW
233 return error;
234}
235
7777fab9
RW
236/**
237 * hibernation_snapshot - quiesce devices and create the hibernation
238 * snapshot image.
239 * @platform_mode - if set, use the platform driver, if available, to
240 * prepare the platform frimware for the power transition.
241 *
242 * Must be called with pm_mutex held
243 */
244
245int hibernation_snapshot(int platform_mode)
246{
247 int error;
248
249 /* Free memory before shutting down devices. */
250 error = swsusp_shrink_memory();
251 if (error)
10a1803d 252 return error;
7777fab9 253
caea99ef 254 error = platform_begin(platform_mode);
74f270af 255 if (error)
caea99ef 256 goto Close;
74f270af 257
7777fab9
RW
258 suspend_console();
259 error = device_suspend(PMSG_FREEZE);
10a1803d
RW
260 if (error)
261 goto Resume_console;
262
4cc79776 263 if (hibernation_test(TEST_DEVICES))
7777fab9
RW
264 goto Resume_devices;
265
4cc79776
RW
266 error = platform_pre_snapshot(platform_mode);
267 if (error || hibernation_test(TEST_PLATFORM))
268 goto Finish;
269
7777fab9
RW
270 error = disable_nonboot_cpus();
271 if (!error) {
4cc79776
RW
272 if (hibernation_test(TEST_CPUS))
273 goto Enable_cpus;
274
275 if (hibernation_testmode(HIBERNATION_TEST))
276 goto Enable_cpus;
277
278 error = create_image(platform_mode);
279 /* Control returns here after successful restore */
7777fab9 280 }
4cc79776 281 Enable_cpus:
7777fab9 282 enable_nonboot_cpus();
4cc79776 283 Finish:
7777fab9 284 platform_finish(platform_mode);
4cc79776 285 Resume_devices:
1eede070
RW
286 device_resume(in_suspend ?
287 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
10a1803d 288 Resume_console:
7777fab9 289 resume_console();
caea99ef
RW
290 Close:
291 platform_end(platform_mode);
7777fab9
RW
292 return error;
293}
294
72df68ca
RW
295/**
296 * resume_target_kernel - prepare devices that need to be suspended with
297 * interrupts off, restore the contents of highmem that have not been
298 * restored yet from the image and run the low level code that will restore
299 * the remaining contents of memory and switch to the just restored target
300 * kernel.
301 */
302
303static int resume_target_kernel(void)
304{
305 int error;
306
1eede070 307 device_pm_lock();
72df68ca 308 local_irq_disable();
1eede070 309 error = device_power_down(PMSG_QUIESCE);
72df68ca 310 if (error) {
23976728 311 printk(KERN_ERR "PM: Some devices failed to power down, "
72df68ca
RW
312 "aborting resume\n");
313 goto Enable_irqs;
314 }
315 /* We'll ignore saved state, but this gets preempt count (etc) right */
316 save_processor_state();
317 error = restore_highmem();
318 if (!error) {
319 error = swsusp_arch_resume();
320 /*
321 * The code below is only ever reached in case of a failure.
322 * Otherwise execution continues at place where
323 * swsusp_arch_suspend() was called
324 */
325 BUG_ON(!error);
326 /* This call to restore_highmem() undos the previous one */
327 restore_highmem();
328 }
329 /*
330 * The only reason why swsusp_arch_resume() can fail is memory being
331 * very tight, so we have to free it as soon as we can to avoid
332 * subsequent failures
333 */
334 swsusp_free();
335 restore_processor_state();
336 touch_softlockup_watchdog();
1eede070 337 device_power_up(PMSG_RECOVER);
72df68ca
RW
338 Enable_irqs:
339 local_irq_enable();
1eede070 340 device_pm_unlock();
72df68ca
RW
341 return error;
342}
343
7777fab9
RW
344/**
345 * hibernation_restore - quiesce devices and restore the hibernation
346 * snapshot image. If successful, control returns in hibernation_snaphot()
a634cc10
RW
347 * @platform_mode - if set, use the platform driver, if available, to
348 * prepare the platform frimware for the transition.
7777fab9
RW
349 *
350 * Must be called with pm_mutex held
351 */
352
a634cc10 353int hibernation_restore(int platform_mode)
7777fab9
RW
354{
355 int error;
356
357 pm_prepare_console();
358 suspend_console();
1eede070 359 error = device_suspend(PMSG_QUIESCE);
7777fab9
RW
360 if (error)
361 goto Finish;
362
a634cc10
RW
363 error = platform_pre_restore(platform_mode);
364 if (!error) {
365 error = disable_nonboot_cpus();
366 if (!error)
72df68ca 367 error = resume_target_kernel();
a634cc10
RW
368 enable_nonboot_cpus();
369 }
370 platform_restore_cleanup(platform_mode);
1eede070 371 device_resume(PMSG_RECOVER);
10a1803d 372 Finish:
7777fab9
RW
373 resume_console();
374 pm_restore_console();
375 return error;
376}
377
378/**
379 * hibernation_platform_enter - enter the hibernation state using the
380 * platform driver (if available)
381 */
382
383int hibernation_platform_enter(void)
384{
b1457bcc
RW
385 int error;
386
9cd9a005
RW
387 if (!hibernation_ops)
388 return -ENOSYS;
389
390 /*
391 * We have cancelled the power transition by running
392 * hibernation_ops->finish() before saving the image, so we should let
393 * the firmware know that we're going to enter the sleep state after all
394 */
caea99ef 395 error = hibernation_ops->begin();
9cd9a005 396 if (error)
caea99ef 397 goto Close;
9cd9a005
RW
398
399 suspend_console();
3a2d5b70 400 error = device_suspend(PMSG_HIBERNATE);
9cd9a005
RW
401 if (error)
402 goto Resume_console;
403
404 error = hibernation_ops->prepare();
405 if (error)
406 goto Resume_devices;
407
408 error = disable_nonboot_cpus();
409 if (error)
410 goto Finish;
411
1eede070 412 device_pm_lock();
9cd9a005 413 local_irq_disable();
3a2d5b70 414 error = device_power_down(PMSG_HIBERNATE);
9cd9a005
RW
415 if (!error) {
416 hibernation_ops->enter();
417 /* We should never get here */
418 while (1);
7777fab9 419 }
9cd9a005 420 local_irq_enable();
1eede070 421 device_pm_unlock();
9cd9a005
RW
422
423 /*
424 * We don't need to reenable the nonboot CPUs or resume consoles, since
425 * the system is going to be halted anyway.
426 */
427 Finish:
428 hibernation_ops->finish();
429 Resume_devices:
1eede070 430 device_resume(PMSG_RESTORE);
9cd9a005
RW
431 Resume_console:
432 resume_console();
caea99ef
RW
433 Close:
434 hibernation_ops->end();
b1457bcc 435 return error;
7777fab9
RW
436}
437
1da177e4 438/**
a3d25c27 439 * power_down - Shut the machine down for hibernation.
1da177e4 440 *
fe0c935a
JB
441 * Use the platform driver, if configured so; otherwise try
442 * to power off or reboot.
1da177e4
LT
443 */
444
fe0c935a 445static void power_down(void)
1da177e4 446{
a3d25c27
RW
447 switch (hibernation_mode) {
448 case HIBERNATION_TEST:
449 case HIBERNATION_TESTPROC:
fe0c935a 450 break;
a3d25c27 451 case HIBERNATION_REBOOT:
fdde86ac 452 kernel_restart(NULL);
1da177e4 453 break;
a3d25c27 454 case HIBERNATION_PLATFORM:
7777fab9 455 hibernation_platform_enter();
9cd9a005
RW
456 case HIBERNATION_SHUTDOWN:
457 kernel_power_off();
458 break;
1da177e4 459 }
fdde86ac 460 kernel_halt();
fe0c935a
JB
461 /*
462 * Valid image is on the disk, if we continue we risk serious data
463 * corruption after resume.
464 */
23976728 465 printk(KERN_CRIT "PM: Please power down manually\n");
1da177e4
LT
466 while(1);
467}
468
1da177e4
LT
469static int prepare_processes(void)
470{
b918f6e6 471 int error = 0;
1da177e4 472
1da177e4
LT
473 if (freeze_processes()) {
474 error = -EBUSY;
5a0a2f30 475 thaw_processes();
1da177e4 476 }
5a72e04d 477 return error;
1da177e4
LT
478}
479
1da177e4 480/**
a3d25c27 481 * hibernate - The granpappy of the built-in hibernation management
1da177e4
LT
482 */
483
a3d25c27 484int hibernate(void)
1da177e4
LT
485{
486 int error;
487
b10d9117 488 mutex_lock(&pm_mutex);
0709db60 489 /* The snapshot device should not be opened while we're running */
b10d9117
RW
490 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
491 error = -EBUSY;
492 goto Unlock;
493 }
494
5a0a2f30 495 pm_prepare_console();
b10d9117
RW
496 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
497 if (error)
498 goto Exit;
0709db60
RW
499
500 /* Allocate memory management structures */
501 error = create_basic_memory_bitmaps();
502 if (error)
503 goto Exit;
504
23976728 505 printk(KERN_INFO "PM: Syncing filesystems ... ");
232b1432
RW
506 sys_sync();
507 printk("done.\n");
508
1da177e4 509 error = prepare_processes();
5a72e04d 510 if (error)
0709db60 511 goto Finish;
1da177e4 512
4cc79776 513 if (hibernation_test(TEST_FREEZER))
ed746e3b 514 goto Thaw;
4cc79776
RW
515
516 if (hibernation_testmode(HIBERNATION_TESTPROC))
517 goto Thaw;
518
7777fab9
RW
519 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
520 if (in_suspend && !error) {
a634cc10
RW
521 unsigned int flags = 0;
522
523 if (hibernation_mode == HIBERNATION_PLATFORM)
524 flags |= SF_PLATFORM_MODE;
1da177e4 525 pr_debug("PM: writing image.\n");
a634cc10 526 error = swsusp_write(flags);
7777fab9 527 swsusp_free();
1da177e4 528 if (!error)
fe0c935a 529 power_down();
b918f6e6 530 } else {
1da177e4 531 pr_debug("PM: Image restored successfully.\n");
7777fab9 532 swsusp_free();
b918f6e6 533 }
b918f6e6 534 Thaw:
5a0a2f30 535 thaw_processes();
0709db60
RW
536 Finish:
537 free_basic_memory_bitmaps();
538 Exit:
b10d9117 539 pm_notifier_call_chain(PM_POST_HIBERNATION);
5a0a2f30 540 pm_restore_console();
0709db60 541 atomic_inc(&snapshot_device_available);
b10d9117
RW
542 Unlock:
543 mutex_unlock(&pm_mutex);
1da177e4
LT
544 return error;
545}
546
547
548/**
549 * software_resume - Resume from a saved image.
550 *
551 * Called as a late_initcall (so all devices are discovered and
552 * initialized), we call swsusp to see if we have a saved image or not.
553 * If so, we quiesce devices, the restore the saved image. We will
a3d25c27 554 * return above (in hibernate() ) if everything goes well.
1da177e4
LT
555 * Otherwise, we fail gracefully and return to the normally
556 * scheduled program.
557 *
558 */
559
560static int software_resume(void)
561{
562 int error;
a634cc10 563 unsigned int flags;
1da177e4 564
60a0d233
JB
565 /*
566 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
567 * is configured into the kernel. Since the regular hibernate
568 * trigger path is via sysfs which takes a buffer mutex before
569 * calling hibernate functions (which take pm_mutex) this can
570 * cause lockdep to complain about a possible ABBA deadlock
571 * which cannot happen since we're in the boot code here and
572 * sysfs can't be invoked yet. Therefore, we use a subclass
573 * here to avoid lockdep complaining.
574 */
575 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
3efa147a 576 if (!swsusp_resume_device) {
dd5d666b 577 if (!strlen(resume_file)) {
a6d70980 578 mutex_unlock(&pm_mutex);
3efa147a 579 return -ENOENT;
dd5d666b 580 }
3efa147a 581 swsusp_resume_device = name_to_dev_t(resume_file);
23976728 582 pr_debug("PM: Resume from partition %s\n", resume_file);
3efa147a 583 } else {
23976728
RW
584 pr_debug("PM: Resume from partition %d:%d\n",
585 MAJOR(swsusp_resume_device),
586 MINOR(swsusp_resume_device));
3efa147a
PM
587 }
588
1da177e4
LT
589 if (noresume) {
590 /**
9575809c
RW
591 * FIXME: If noresume is specified, we need to find the
592 * partition and reset it back to normal swap space.
1da177e4 593 */
a6d70980 594 mutex_unlock(&pm_mutex);
1da177e4
LT
595 return 0;
596 }
597
23976728 598 pr_debug("PM: Checking hibernation image.\n");
ed746e3b
RW
599 error = swsusp_check();
600 if (error)
74dfd666 601 goto Unlock;
1da177e4 602
0709db60
RW
603 /* The snapshot device should not be opened while we're running */
604 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
605 error = -EBUSY;
606 goto Unlock;
607 }
608
5a0a2f30 609 pm_prepare_console();
c3e94d89
AS
610 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
611 if (error)
612 goto Finish;
613
74dfd666
RW
614 error = create_basic_memory_bitmaps();
615 if (error)
0709db60 616 goto Finish;
1da177e4 617
74dfd666 618 pr_debug("PM: Preparing processes for restore.\n");
ed746e3b
RW
619 error = prepare_processes();
620 if (error) {
1da177e4 621 swsusp_close();
5a72e04d 622 goto Done;
1da177e4
LT
623 }
624
23976728 625 pr_debug("PM: Reading hibernation image.\n");
1da177e4 626
a634cc10 627 error = swsusp_read(&flags);
ed746e3b 628 if (!error)
a634cc10 629 hibernation_restore(flags & SF_PLATFORM_MODE);
1da177e4 630
ed746e3b 631 printk(KERN_ERR "PM: Restore failed, recovering.\n");
7777fab9 632 swsusp_free();
5a0a2f30 633 thaw_processes();
1da177e4 634 Done:
74dfd666 635 free_basic_memory_bitmaps();
0709db60 636 Finish:
c3e94d89 637 pm_notifier_call_chain(PM_POST_RESTORE);
5a0a2f30 638 pm_restore_console();
0709db60 639 atomic_inc(&snapshot_device_available);
dd5d666b 640 /* For success case, the suspend path will release the lock */
74dfd666 641 Unlock:
a6d70980 642 mutex_unlock(&pm_mutex);
1da177e4 643 pr_debug("PM: Resume from disk failed.\n");
7777fab9 644 return error;
1da177e4
LT
645}
646
647late_initcall(software_resume);
648
649
a3d25c27
RW
650static const char * const hibernation_modes[] = {
651 [HIBERNATION_PLATFORM] = "platform",
652 [HIBERNATION_SHUTDOWN] = "shutdown",
653 [HIBERNATION_REBOOT] = "reboot",
654 [HIBERNATION_TEST] = "test",
655 [HIBERNATION_TESTPROC] = "testproc",
1da177e4
LT
656};
657
658/**
a3d25c27 659 * disk - Control hibernation mode
1da177e4 660 *
11d77d0c
JB
661 * Suspend-to-disk can be handled in several ways. We have a few options
662 * for putting the system to sleep - using the platform driver (e.g. ACPI
a3d25c27
RW
663 * or other hibernation_ops), powering off the system or rebooting the
664 * system (for testing) as well as the two test modes.
1da177e4 665 *
11d77d0c 666 * The system can support 'platform', and that is known a priori (and
a3d25c27
RW
667 * encoded by the presence of hibernation_ops). However, the user may
668 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
669 * test modes, 'test' or 'testproc'.
1da177e4
LT
670 *
671 * show() will display what the mode is currently set to.
672 * store() will accept one of
673 *
1da177e4
LT
674 * 'platform'
675 * 'shutdown'
676 * 'reboot'
11d77d0c
JB
677 * 'test'
678 * 'testproc'
1da177e4 679 *
11d77d0c 680 * It will only change to 'platform' if the system
a3d25c27 681 * supports it (as determined by having hibernation_ops).
1da177e4
LT
682 */
683
386f275f
KS
684static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
685 char *buf)
1da177e4 686{
f0ced9b2
JB
687 int i;
688 char *start = buf;
689
a3d25c27
RW
690 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
691 if (!hibernation_modes[i])
f0ced9b2
JB
692 continue;
693 switch (i) {
a3d25c27
RW
694 case HIBERNATION_SHUTDOWN:
695 case HIBERNATION_REBOOT:
696 case HIBERNATION_TEST:
697 case HIBERNATION_TESTPROC:
f0ced9b2 698 break;
a3d25c27
RW
699 case HIBERNATION_PLATFORM:
700 if (hibernation_ops)
f0ced9b2
JB
701 break;
702 /* not a valid mode, continue with loop */
703 continue;
704 }
a3d25c27
RW
705 if (i == hibernation_mode)
706 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
f0ced9b2 707 else
a3d25c27 708 buf += sprintf(buf, "%s ", hibernation_modes[i]);
f0ced9b2
JB
709 }
710 buf += sprintf(buf, "\n");
711 return buf-start;
1da177e4
LT
712}
713
714
386f275f
KS
715static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
716 const char *buf, size_t n)
1da177e4
LT
717{
718 int error = 0;
719 int i;
720 int len;
721 char *p;
a3d25c27 722 int mode = HIBERNATION_INVALID;
1da177e4
LT
723
724 p = memchr(buf, '\n', n);
725 len = p ? p - buf : n;
726
a6d70980 727 mutex_lock(&pm_mutex);
a3d25c27 728 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
8d98a690
RW
729 if (len == strlen(hibernation_modes[i])
730 && !strncmp(buf, hibernation_modes[i], len)) {
1da177e4
LT
731 mode = i;
732 break;
733 }
734 }
a3d25c27 735 if (mode != HIBERNATION_INVALID) {
fe0c935a 736 switch (mode) {
a3d25c27
RW
737 case HIBERNATION_SHUTDOWN:
738 case HIBERNATION_REBOOT:
739 case HIBERNATION_TEST:
740 case HIBERNATION_TESTPROC:
741 hibernation_mode = mode;
fe0c935a 742 break;
a3d25c27
RW
743 case HIBERNATION_PLATFORM:
744 if (hibernation_ops)
745 hibernation_mode = mode;
1da177e4
LT
746 else
747 error = -EINVAL;
748 }
a3d25c27 749 } else
1da177e4
LT
750 error = -EINVAL;
751
a3d25c27 752 if (!error)
23976728 753 pr_debug("PM: Hibernation mode set to '%s'\n",
a3d25c27 754 hibernation_modes[mode]);
a6d70980 755 mutex_unlock(&pm_mutex);
1da177e4
LT
756 return error ? error : n;
757}
758
759power_attr(disk);
760
386f275f
KS
761static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
762 char *buf)
1da177e4
LT
763{
764 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
765 MINOR(swsusp_resume_device));
766}
767
386f275f
KS
768static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
769 const char *buf, size_t n)
1da177e4 770{
1da177e4 771 unsigned int maj, min;
1da177e4 772 dev_t res;
a576219a 773 int ret = -EINVAL;
1da177e4 774
a576219a
AM
775 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
776 goto out;
1da177e4 777
a576219a
AM
778 res = MKDEV(maj,min);
779 if (maj != MAJOR(res) || min != MINOR(res))
780 goto out;
1da177e4 781
a6d70980 782 mutex_lock(&pm_mutex);
a576219a 783 swsusp_resume_device = res;
a6d70980 784 mutex_unlock(&pm_mutex);
23976728 785 printk(KERN_INFO "PM: Starting manual resume from disk\n");
a576219a
AM
786 noresume = 0;
787 software_resume();
788 ret = n;
59a49335 789 out:
a576219a 790 return ret;
1da177e4
LT
791}
792
793power_attr(resume);
794
386f275f
KS
795static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
796 char *buf)
ca0aec0f 797{
853609b6 798 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
799}
800
386f275f
KS
801static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
802 const char *buf, size_t n)
ca0aec0f 803{
853609b6 804 unsigned long size;
ca0aec0f 805
853609b6 806 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
807 image_size = size;
808 return n;
809 }
810
811 return -EINVAL;
812}
813
814power_attr(image_size);
815
1da177e4
LT
816static struct attribute * g[] = {
817 &disk_attr.attr,
818 &resume_attr.attr,
ca0aec0f 819 &image_size_attr.attr,
1da177e4
LT
820 NULL,
821};
822
823
824static struct attribute_group attr_group = {
825 .attrs = g,
826};
827
828
829static int __init pm_disk_init(void)
830{
d76e15fb 831 return sysfs_create_group(power_kobj, &attr_group);
1da177e4
LT
832}
833
834core_initcall(pm_disk_init);
835
836
837static int __init resume_setup(char *str)
838{
839 if (noresume)
840 return 1;
841
842 strncpy( resume_file, str, 255 );
843 return 1;
844}
845
9a154d9d
RW
846static int __init resume_offset_setup(char *str)
847{
848 unsigned long long offset;
849
850 if (noresume)
851 return 1;
852
853 if (sscanf(str, "%llu", &offset) == 1)
854 swsusp_resume_block = offset;
855
856 return 1;
857}
858
1da177e4
LT
859static int __init noresume_setup(char *str)
860{
861 noresume = 1;
862 return 1;
863}
864
865__setup("noresume", noresume_setup);
9a154d9d 866__setup("resume_offset=", resume_offset_setup);
1da177e4 867__setup("resume=", resume_setup);