Add linux-next specific files for 20110824
[linux-2.6/next.git] / kernel / power / hibernate.c
blob5ec8277e6fb03535ee834a51377be43c90e7bbaa
1 /*
2 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
7 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
9 * This file is released under the GPLv2.
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/export.h>
18 #include <linux/kmod.h>
19 #include <linux/delay.h>
20 #include <linux/fs.h>
21 #include <linux/mount.h>
22 #include <linux/pm.h>
23 #include <linux/console.h>
24 #include <linux/cpu.h>
25 #include <linux/freezer.h>
26 #include <linux/gfp.h>
27 #include <linux/syscore_ops.h>
28 #include <scsi/scsi_scan.h>
30 #include "power.h"
33 static int nocompress = 0;
34 static int noresume = 0;
35 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
36 dev_t swsusp_resume_device;
37 sector_t swsusp_resume_block;
38 int in_suspend __nosavedata = 0;
40 enum {
41 HIBERNATION_INVALID,
42 HIBERNATION_PLATFORM,
43 HIBERNATION_TEST,
44 HIBERNATION_TESTPROC,
45 HIBERNATION_SHUTDOWN,
46 HIBERNATION_REBOOT,
47 /* keep last */
48 __HIBERNATION_AFTER_LAST
50 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
51 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
53 static int hibernation_mode = HIBERNATION_SHUTDOWN;
55 static const struct platform_hibernation_ops *hibernation_ops;
57 /**
58 * hibernation_set_ops - Set the global hibernate operations.
59 * @ops: Hibernation operations to use in subsequent hibernation transitions.
61 void hibernation_set_ops(const struct platform_hibernation_ops *ops)
63 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
64 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
65 && ops->restore_cleanup && ops->leave)) {
66 WARN_ON(1);
67 return;
69 mutex_lock(&pm_mutex);
70 hibernation_ops = ops;
71 if (ops)
72 hibernation_mode = HIBERNATION_PLATFORM;
73 else if (hibernation_mode == HIBERNATION_PLATFORM)
74 hibernation_mode = HIBERNATION_SHUTDOWN;
76 mutex_unlock(&pm_mutex);
79 static bool entering_platform_hibernation;
81 bool system_entering_hibernation(void)
83 return entering_platform_hibernation;
85 EXPORT_SYMBOL(system_entering_hibernation);
87 #ifdef CONFIG_PM_DEBUG
88 static void hibernation_debug_sleep(void)
90 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
91 mdelay(5000);
94 static int hibernation_testmode(int mode)
96 if (hibernation_mode == mode) {
97 hibernation_debug_sleep();
98 return 1;
100 return 0;
103 static int hibernation_test(int level)
105 if (pm_test_level == level) {
106 hibernation_debug_sleep();
107 return 1;
109 return 0;
111 #else /* !CONFIG_PM_DEBUG */
112 static int hibernation_testmode(int mode) { return 0; }
113 static int hibernation_test(int level) { return 0; }
114 #endif /* !CONFIG_PM_DEBUG */
117 * platform_begin - Call platform to start hibernation.
118 * @platform_mode: Whether or not to use the platform driver.
120 static int platform_begin(int platform_mode)
122 return (platform_mode && hibernation_ops) ?
123 hibernation_ops->begin() : 0;
127 * platform_end - Call platform to finish transition to the working state.
128 * @platform_mode: Whether or not to use the platform driver.
130 static void platform_end(int platform_mode)
132 if (platform_mode && hibernation_ops)
133 hibernation_ops->end();
137 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
138 * @platform_mode: Whether or not to use the platform driver.
140 * Use the platform driver to prepare the system for creating a hibernate image,
141 * if so configured, and return an error code if that fails.
144 static int platform_pre_snapshot(int platform_mode)
146 return (platform_mode && hibernation_ops) ?
147 hibernation_ops->pre_snapshot() : 0;
151 * platform_leave - Call platform to prepare a transition to the working state.
152 * @platform_mode: Whether or not to use the platform driver.
154 * Use the platform driver prepare to prepare the machine for switching to the
155 * normal mode of operation.
157 * This routine is called on one CPU with interrupts disabled.
159 static void platform_leave(int platform_mode)
161 if (platform_mode && hibernation_ops)
162 hibernation_ops->leave();
166 * platform_finish - Call platform to switch the system to the working state.
167 * @platform_mode: Whether or not to use the platform driver.
169 * Use the platform driver to switch the machine to the normal mode of
170 * operation.
172 * This routine must be called after platform_prepare().
174 static void platform_finish(int platform_mode)
176 if (platform_mode && hibernation_ops)
177 hibernation_ops->finish();
181 * platform_pre_restore - Prepare for hibernate image restoration.
182 * @platform_mode: Whether or not to use the platform driver.
184 * Use the platform driver to prepare the system for resume from a hibernation
185 * image.
187 * If the restore fails after this function has been called,
188 * platform_restore_cleanup() must be called.
190 static int platform_pre_restore(int platform_mode)
192 return (platform_mode && hibernation_ops) ?
193 hibernation_ops->pre_restore() : 0;
197 * platform_restore_cleanup - Switch to the working state after failing restore.
198 * @platform_mode: Whether or not to use the platform driver.
200 * Use the platform driver to switch the system to the normal mode of operation
201 * after a failing restore.
203 * If platform_pre_restore() has been called before the failing restore, this
204 * function must be called too, regardless of the result of
205 * platform_pre_restore().
207 static void platform_restore_cleanup(int platform_mode)
209 if (platform_mode && hibernation_ops)
210 hibernation_ops->restore_cleanup();
214 * platform_recover - Recover from a failure to suspend devices.
215 * @platform_mode: Whether or not to use the platform driver.
217 static void platform_recover(int platform_mode)
219 if (platform_mode && hibernation_ops && hibernation_ops->recover)
220 hibernation_ops->recover();
224 * swsusp_show_speed - Print time elapsed between two events during hibernation.
225 * @start: Starting event.
226 * @stop: Final event.
227 * @nr_pages: Number of memory pages processed between @start and @stop.
228 * @msg: Additional diagnostic message to print.
230 void swsusp_show_speed(struct timeval *start, struct timeval *stop,
231 unsigned nr_pages, char *msg)
233 s64 elapsed_centisecs64;
234 int centisecs;
235 int k;
236 int kps;
238 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
239 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
240 centisecs = elapsed_centisecs64;
241 if (centisecs == 0)
242 centisecs = 1; /* avoid div-by-zero */
243 k = nr_pages * (PAGE_SIZE / 1024);
244 kps = (k * 100) / centisecs;
245 printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
246 msg, k,
247 centisecs / 100, centisecs % 100,
248 kps / 1000, (kps % 1000) / 10);
252 * create_image - Create a hibernation image.
253 * @platform_mode: Whether or not to use the platform driver.
255 * Execute device drivers' .freeze_noirq() callbacks, create a hibernation image
256 * and execute the drivers' .thaw_noirq() callbacks.
258 * Control reappears in this routine after the subsequent restore.
260 static int create_image(int platform_mode)
262 int error;
264 error = dpm_suspend_noirq(PMSG_FREEZE);
265 if (error) {
266 printk(KERN_ERR "PM: Some devices failed to power down, "
267 "aborting hibernation\n");
268 return error;
271 error = platform_pre_snapshot(platform_mode);
272 if (error || hibernation_test(TEST_PLATFORM))
273 goto Platform_finish;
275 error = disable_nonboot_cpus();
276 if (error || hibernation_test(TEST_CPUS)
277 || hibernation_testmode(HIBERNATION_TEST))
278 goto Enable_cpus;
280 local_irq_disable();
282 error = syscore_suspend();
283 if (error) {
284 printk(KERN_ERR "PM: Some system devices failed to power down, "
285 "aborting hibernation\n");
286 goto Enable_irqs;
289 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
290 goto Power_up;
292 in_suspend = 1;
293 save_processor_state();
294 error = swsusp_arch_suspend();
295 if (error)
296 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
297 error);
298 /* Restore control flow magically appears here */
299 restore_processor_state();
300 if (!in_suspend) {
301 events_check_enabled = false;
302 platform_leave(platform_mode);
305 Power_up:
306 syscore_resume();
308 Enable_irqs:
309 local_irq_enable();
311 Enable_cpus:
312 enable_nonboot_cpus();
314 Platform_finish:
315 platform_finish(platform_mode);
317 dpm_resume_noirq(in_suspend ?
318 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
320 return error;
324 * hibernation_snapshot - Quiesce devices and create a hibernation image.
325 * @platform_mode: If set, use platform driver to prepare for the transition.
327 * This routine must be called with pm_mutex held.
329 int hibernation_snapshot(int platform_mode)
331 pm_message_t msg = PMSG_RECOVER;
332 int error;
334 error = platform_begin(platform_mode);
335 if (error)
336 goto Close;
338 error = dpm_prepare(PMSG_FREEZE);
339 if (error)
340 goto Complete_devices;
342 /* Preallocate image memory before shutting down devices. */
343 error = hibernate_preallocate_memory();
344 if (error)
345 goto Complete_devices;
347 suspend_console();
348 pm_restrict_gfp_mask();
349 error = dpm_suspend(PMSG_FREEZE);
350 if (error)
351 goto Recover_platform;
353 if (hibernation_test(TEST_DEVICES))
354 goto Recover_platform;
356 error = create_image(platform_mode);
358 * Control returns here (1) after the image has been created or the
359 * image creation has failed and (2) after a successful restore.
362 Resume_devices:
363 /* We may need to release the preallocated image pages here. */
364 if (error || !in_suspend)
365 swsusp_free();
367 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
368 dpm_resume(msg);
370 if (error || !in_suspend)
371 pm_restore_gfp_mask();
373 resume_console();
375 Complete_devices:
376 dpm_complete(msg);
378 Close:
379 platform_end(platform_mode);
380 return error;
382 Recover_platform:
383 platform_recover(platform_mode);
384 goto Resume_devices;
388 * resume_target_kernel - Restore system state from a hibernation image.
389 * @platform_mode: Whether or not to use the platform driver.
391 * Execute device drivers' .freeze_noirq() callbacks, restore the contents of
392 * highmem that have not been restored yet from the image and run the low-level
393 * code that will restore the remaining contents of memory and switch to the
394 * just restored target kernel.
396 static int resume_target_kernel(bool platform_mode)
398 int error;
400 error = dpm_suspend_noirq(PMSG_QUIESCE);
401 if (error) {
402 printk(KERN_ERR "PM: Some devices failed to power down, "
403 "aborting resume\n");
404 return error;
407 error = platform_pre_restore(platform_mode);
408 if (error)
409 goto Cleanup;
411 error = disable_nonboot_cpus();
412 if (error)
413 goto Enable_cpus;
415 local_irq_disable();
417 error = syscore_suspend();
418 if (error)
419 goto Enable_irqs;
421 save_processor_state();
422 error = restore_highmem();
423 if (!error) {
424 error = swsusp_arch_resume();
426 * The code below is only ever reached in case of a failure.
427 * Otherwise, execution continues at the place where
428 * swsusp_arch_suspend() was called.
430 BUG_ON(!error);
432 * This call to restore_highmem() reverts the changes made by
433 * the previous one.
435 restore_highmem();
438 * The only reason why swsusp_arch_resume() can fail is memory being
439 * very tight, so we have to free it as soon as we can to avoid
440 * subsequent failures.
442 swsusp_free();
443 restore_processor_state();
444 touch_softlockup_watchdog();
446 syscore_resume();
448 Enable_irqs:
449 local_irq_enable();
451 Enable_cpus:
452 enable_nonboot_cpus();
454 Cleanup:
455 platform_restore_cleanup(platform_mode);
457 dpm_resume_noirq(PMSG_RECOVER);
459 return error;
463 * hibernation_restore - Quiesce devices and restore from a hibernation image.
464 * @platform_mode: If set, use platform driver to prepare for the transition.
466 * This routine must be called with pm_mutex held. If it is successful, control
467 * reappears in the restored target kernel in hibernation_snaphot().
469 int hibernation_restore(int platform_mode)
471 int error;
473 pm_prepare_console();
474 suspend_console();
475 pm_restrict_gfp_mask();
476 error = dpm_suspend_start(PMSG_QUIESCE);
477 if (!error) {
478 error = resume_target_kernel(platform_mode);
479 dpm_resume_end(PMSG_RECOVER);
481 pm_restore_gfp_mask();
482 resume_console();
483 pm_restore_console();
484 return error;
488 * hibernation_platform_enter - Power off the system using the platform driver.
490 int hibernation_platform_enter(void)
492 int error;
494 if (!hibernation_ops)
495 return -ENOSYS;
498 * We have cancelled the power transition by running
499 * hibernation_ops->finish() before saving the image, so we should let
500 * the firmware know that we're going to enter the sleep state after all
502 error = hibernation_ops->begin();
503 if (error)
504 goto Close;
506 entering_platform_hibernation = true;
507 suspend_console();
508 error = dpm_suspend_start(PMSG_HIBERNATE);
509 if (error) {
510 if (hibernation_ops->recover)
511 hibernation_ops->recover();
512 goto Resume_devices;
515 error = dpm_suspend_noirq(PMSG_HIBERNATE);
516 if (error)
517 goto Resume_devices;
519 error = hibernation_ops->prepare();
520 if (error)
521 goto Platform_finish;
523 error = disable_nonboot_cpus();
524 if (error)
525 goto Platform_finish;
527 local_irq_disable();
528 syscore_suspend();
529 if (pm_wakeup_pending()) {
530 error = -EAGAIN;
531 goto Power_up;
534 hibernation_ops->enter();
535 /* We should never get here */
536 while (1);
538 Power_up:
539 syscore_resume();
540 local_irq_enable();
541 enable_nonboot_cpus();
543 Platform_finish:
544 hibernation_ops->finish();
546 dpm_resume_noirq(PMSG_RESTORE);
548 Resume_devices:
549 entering_platform_hibernation = false;
550 dpm_resume_end(PMSG_RESTORE);
551 resume_console();
553 Close:
554 hibernation_ops->end();
556 return error;
560 * power_down - Shut the machine down for hibernation.
562 * Use the platform driver, if configured, to put the system into the sleep
563 * state corresponding to hibernation, or try to power it off or reboot,
564 * depending on the value of hibernation_mode.
566 static void power_down(void)
568 switch (hibernation_mode) {
569 case HIBERNATION_TEST:
570 case HIBERNATION_TESTPROC:
571 break;
572 case HIBERNATION_REBOOT:
573 kernel_restart(NULL);
574 break;
575 case HIBERNATION_PLATFORM:
576 hibernation_platform_enter();
577 case HIBERNATION_SHUTDOWN:
578 kernel_power_off();
579 break;
581 kernel_halt();
583 * Valid image is on the disk, if we continue we risk serious data
584 * corruption after resume.
586 printk(KERN_CRIT "PM: Please power down manually\n");
587 while(1);
591 * hibernate - Carry out system hibernation, including saving the image.
593 int hibernate(void)
595 int error;
597 mutex_lock(&pm_mutex);
598 /* The snapshot device should not be opened while we're running */
599 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
600 error = -EBUSY;
601 goto Unlock;
604 pm_prepare_console();
605 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
606 if (error)
607 goto Exit;
609 error = usermodehelper_disable();
610 if (error)
611 goto Exit;
613 /* Allocate memory management structures */
614 error = create_basic_memory_bitmaps();
615 if (error)
616 goto Exit;
618 printk(KERN_INFO "PM: Syncing filesystems ... ");
619 sys_sync();
620 printk("done.\n");
622 error = freeze_processes();
623 if (error)
624 goto Finish;
626 if (hibernation_test(TEST_FREEZER))
627 goto Thaw;
629 if (hibernation_testmode(HIBERNATION_TESTPROC))
630 goto Thaw;
632 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
633 if (error)
634 goto Thaw;
636 if (in_suspend) {
637 unsigned int flags = 0;
639 if (hibernation_mode == HIBERNATION_PLATFORM)
640 flags |= SF_PLATFORM_MODE;
641 if (nocompress)
642 flags |= SF_NOCOMPRESS_MODE;
643 pr_debug("PM: writing image.\n");
644 error = swsusp_write(flags);
645 swsusp_free();
646 if (!error)
647 power_down();
648 in_suspend = 0;
649 pm_restore_gfp_mask();
650 } else {
651 pr_debug("PM: Image restored successfully.\n");
654 Thaw:
655 thaw_processes();
656 Finish:
657 free_basic_memory_bitmaps();
658 usermodehelper_enable();
659 Exit:
660 pm_notifier_call_chain(PM_POST_HIBERNATION);
661 pm_restore_console();
662 atomic_inc(&snapshot_device_available);
663 Unlock:
664 mutex_unlock(&pm_mutex);
665 return error;
670 * software_resume - Resume from a saved hibernation image.
672 * This routine is called as a late initcall, when all devices have been
673 * discovered and initialized already.
675 * The image reading code is called to see if there is a hibernation image
676 * available for reading. If that is the case, devices are quiesced and the
677 * contents of memory is restored from the saved image.
679 * If this is successful, control reappears in the restored target kernel in
680 * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
681 * attempts to recover gracefully and make the kernel return to the normal mode
682 * of operation.
684 static int software_resume(void)
686 int error;
687 unsigned int flags;
690 * If the user said "noresume".. bail out early.
692 if (noresume)
693 return 0;
696 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
697 * is configured into the kernel. Since the regular hibernate
698 * trigger path is via sysfs which takes a buffer mutex before
699 * calling hibernate functions (which take pm_mutex) this can
700 * cause lockdep to complain about a possible ABBA deadlock
701 * which cannot happen since we're in the boot code here and
702 * sysfs can't be invoked yet. Therefore, we use a subclass
703 * here to avoid lockdep complaining.
705 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
707 if (swsusp_resume_device)
708 goto Check_image;
710 if (!strlen(resume_file)) {
711 error = -ENOENT;
712 goto Unlock;
715 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
717 /* Check if the device is there */
718 swsusp_resume_device = name_to_dev_t(resume_file);
719 if (!swsusp_resume_device) {
721 * Some device discovery might still be in progress; we need
722 * to wait for this to finish.
724 wait_for_device_probe();
726 * We can't depend on SCSI devices being available after loading
727 * one of their modules until scsi_complete_async_scans() is
728 * called and the resume device usually is a SCSI one.
730 scsi_complete_async_scans();
732 swsusp_resume_device = name_to_dev_t(resume_file);
733 if (!swsusp_resume_device) {
734 error = -ENODEV;
735 goto Unlock;
739 Check_image:
740 pr_debug("PM: Hibernation image partition %d:%d present\n",
741 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
743 pr_debug("PM: Looking for hibernation image.\n");
744 error = swsusp_check();
745 if (error)
746 goto Unlock;
748 /* The snapshot device should not be opened while we're running */
749 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
750 error = -EBUSY;
751 swsusp_close(FMODE_READ);
752 goto Unlock;
755 pm_prepare_console();
756 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
757 if (error)
758 goto close_finish;
760 error = usermodehelper_disable();
761 if (error)
762 goto close_finish;
764 error = create_basic_memory_bitmaps();
765 if (error)
766 goto close_finish;
768 pr_debug("PM: Preparing processes for restore.\n");
769 error = freeze_processes();
770 if (error) {
771 swsusp_close(FMODE_READ);
772 goto Done;
775 pr_debug("PM: Loading hibernation image.\n");
777 error = swsusp_read(&flags);
778 swsusp_close(FMODE_READ);
779 if (!error)
780 hibernation_restore(flags & SF_PLATFORM_MODE);
782 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
783 swsusp_free();
784 thaw_processes();
785 Done:
786 free_basic_memory_bitmaps();
787 usermodehelper_enable();
788 Finish:
789 pm_notifier_call_chain(PM_POST_RESTORE);
790 pm_restore_console();
791 atomic_inc(&snapshot_device_available);
792 /* For success case, the suspend path will release the lock */
793 Unlock:
794 mutex_unlock(&pm_mutex);
795 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
796 return error;
797 close_finish:
798 swsusp_close(FMODE_READ);
799 goto Finish;
802 late_initcall(software_resume);
805 static const char * const hibernation_modes[] = {
806 [HIBERNATION_PLATFORM] = "platform",
807 [HIBERNATION_SHUTDOWN] = "shutdown",
808 [HIBERNATION_REBOOT] = "reboot",
809 [HIBERNATION_TEST] = "test",
810 [HIBERNATION_TESTPROC] = "testproc",
814 * /sys/power/disk - Control hibernation mode.
816 * Hibernation can be handled in several ways. There are a few different ways
817 * to put the system into the sleep state: using the platform driver (e.g. ACPI
818 * or other hibernation_ops), powering it off or rebooting it (for testing
819 * mostly), or using one of the two available test modes.
821 * The sysfs file /sys/power/disk provides an interface for selecting the
822 * hibernation mode to use. Reading from this file causes the available modes
823 * to be printed. There are 5 modes that can be supported:
825 * 'platform'
826 * 'shutdown'
827 * 'reboot'
828 * 'test'
829 * 'testproc'
831 * If a platform hibernation driver is in use, 'platform' will be supported
832 * and will be used by default. Otherwise, 'shutdown' will be used by default.
833 * The selected option (i.e. the one corresponding to the current value of
834 * hibernation_mode) is enclosed by a square bracket.
836 * To select a given hibernation mode it is necessary to write the mode's
837 * string representation (as returned by reading from /sys/power/disk) back
838 * into /sys/power/disk.
841 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
842 char *buf)
844 int i;
845 char *start = buf;
847 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
848 if (!hibernation_modes[i])
849 continue;
850 switch (i) {
851 case HIBERNATION_SHUTDOWN:
852 case HIBERNATION_REBOOT:
853 case HIBERNATION_TEST:
854 case HIBERNATION_TESTPROC:
855 break;
856 case HIBERNATION_PLATFORM:
857 if (hibernation_ops)
858 break;
859 /* not a valid mode, continue with loop */
860 continue;
862 if (i == hibernation_mode)
863 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
864 else
865 buf += sprintf(buf, "%s ", hibernation_modes[i]);
867 buf += sprintf(buf, "\n");
868 return buf-start;
871 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
872 const char *buf, size_t n)
874 int error = 0;
875 int i;
876 int len;
877 char *p;
878 int mode = HIBERNATION_INVALID;
880 p = memchr(buf, '\n', n);
881 len = p ? p - buf : n;
883 mutex_lock(&pm_mutex);
884 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
885 if (len == strlen(hibernation_modes[i])
886 && !strncmp(buf, hibernation_modes[i], len)) {
887 mode = i;
888 break;
891 if (mode != HIBERNATION_INVALID) {
892 switch (mode) {
893 case HIBERNATION_SHUTDOWN:
894 case HIBERNATION_REBOOT:
895 case HIBERNATION_TEST:
896 case HIBERNATION_TESTPROC:
897 hibernation_mode = mode;
898 break;
899 case HIBERNATION_PLATFORM:
900 if (hibernation_ops)
901 hibernation_mode = mode;
902 else
903 error = -EINVAL;
905 } else
906 error = -EINVAL;
908 if (!error)
909 pr_debug("PM: Hibernation mode set to '%s'\n",
910 hibernation_modes[mode]);
911 mutex_unlock(&pm_mutex);
912 return error ? error : n;
915 power_attr(disk);
917 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
918 char *buf)
920 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
921 MINOR(swsusp_resume_device));
924 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
925 const char *buf, size_t n)
927 unsigned int maj, min;
928 dev_t res;
929 int ret = -EINVAL;
931 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
932 goto out;
934 res = MKDEV(maj,min);
935 if (maj != MAJOR(res) || min != MINOR(res))
936 goto out;
938 mutex_lock(&pm_mutex);
939 swsusp_resume_device = res;
940 mutex_unlock(&pm_mutex);
941 printk(KERN_INFO "PM: Starting manual resume from disk\n");
942 noresume = 0;
943 software_resume();
944 ret = n;
945 out:
946 return ret;
949 power_attr(resume);
951 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
952 char *buf)
954 return sprintf(buf, "%lu\n", image_size);
957 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
958 const char *buf, size_t n)
960 unsigned long size;
962 if (sscanf(buf, "%lu", &size) == 1) {
963 image_size = size;
964 return n;
967 return -EINVAL;
970 power_attr(image_size);
972 static ssize_t reserved_size_show(struct kobject *kobj,
973 struct kobj_attribute *attr, char *buf)
975 return sprintf(buf, "%lu\n", reserved_size);
978 static ssize_t reserved_size_store(struct kobject *kobj,
979 struct kobj_attribute *attr,
980 const char *buf, size_t n)
982 unsigned long size;
984 if (sscanf(buf, "%lu", &size) == 1) {
985 reserved_size = size;
986 return n;
989 return -EINVAL;
992 power_attr(reserved_size);
994 static struct attribute * g[] = {
995 &disk_attr.attr,
996 &resume_attr.attr,
997 &image_size_attr.attr,
998 &reserved_size_attr.attr,
999 NULL,
1003 static struct attribute_group attr_group = {
1004 .attrs = g,
1008 static int __init pm_disk_init(void)
1010 return sysfs_create_group(power_kobj, &attr_group);
1013 core_initcall(pm_disk_init);
1016 static int __init resume_setup(char *str)
1018 if (noresume)
1019 return 1;
1021 strncpy( resume_file, str, 255 );
1022 return 1;
1025 static int __init resume_offset_setup(char *str)
1027 unsigned long long offset;
1029 if (noresume)
1030 return 1;
1032 if (sscanf(str, "%llu", &offset) == 1)
1033 swsusp_resume_block = offset;
1035 return 1;
1038 static int __init hibernate_setup(char *str)
1040 if (!strncmp(str, "noresume", 8))
1041 noresume = 1;
1042 else if (!strncmp(str, "nocompress", 10))
1043 nocompress = 1;
1044 return 1;
1047 static int __init noresume_setup(char *str)
1049 noresume = 1;
1050 return 1;
1053 __setup("noresume", noresume_setup);
1054 __setup("resume_offset=", resume_offset_setup);
1055 __setup("resume=", resume_setup);
1056 __setup("hibernate=", hibernate_setup);