include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / kernel / power / hibernate.c
blobce21f2580cc339c956d541e54860bc10fe9ba9ac
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);
590 static int prepare_processes(void)
592 int error = 0;
594 if (freeze_processes()) {
595 error = -EBUSY;
596 thaw_processes();
598 return error;
602 * hibernate - Carry out system hibernation, including saving the image.
604 int hibernate(void)
606 int error;
608 mutex_lock(&pm_mutex);
609 /* The snapshot device should not be opened while we're running */
610 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
611 error = -EBUSY;
612 goto Unlock;
615 pm_prepare_console();
616 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
617 if (error)
618 goto Exit;
620 error = usermodehelper_disable();
621 if (error)
622 goto Exit;
624 /* Allocate memory management structures */
625 error = create_basic_memory_bitmaps();
626 if (error)
627 goto Exit;
629 printk(KERN_INFO "PM: Syncing filesystems ... ");
630 sys_sync();
631 printk("done.\n");
633 error = prepare_processes();
634 if (error)
635 goto Finish;
637 if (hibernation_test(TEST_FREEZER))
638 goto Thaw;
640 if (hibernation_testmode(HIBERNATION_TESTPROC))
641 goto Thaw;
643 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
644 if (error)
645 goto Thaw;
647 if (in_suspend) {
648 unsigned int flags = 0;
650 if (hibernation_mode == HIBERNATION_PLATFORM)
651 flags |= SF_PLATFORM_MODE;
652 if (nocompress)
653 flags |= SF_NOCOMPRESS_MODE;
654 pr_debug("PM: writing image.\n");
655 error = swsusp_write(flags);
656 swsusp_free();
657 if (!error)
658 power_down();
659 in_suspend = 0;
660 pm_restore_gfp_mask();
661 } else {
662 pr_debug("PM: Image restored successfully.\n");
665 Thaw:
666 thaw_processes();
667 Finish:
668 free_basic_memory_bitmaps();
669 usermodehelper_enable();
670 Exit:
671 pm_notifier_call_chain(PM_POST_HIBERNATION);
672 pm_restore_console();
673 atomic_inc(&snapshot_device_available);
674 Unlock:
675 mutex_unlock(&pm_mutex);
676 return error;
681 * software_resume - Resume from a saved hibernation image.
683 * This routine is called as a late initcall, when all devices have been
684 * discovered and initialized already.
686 * The image reading code is called to see if there is a hibernation image
687 * available for reading. If that is the case, devices are quiesced and the
688 * contents of memory is restored from the saved image.
690 * If this is successful, control reappears in the restored target kernel in
691 * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
692 * attempts to recover gracefully and make the kernel return to the normal mode
693 * of operation.
695 static int software_resume(void)
697 int error;
698 unsigned int flags;
701 * If the user said "noresume".. bail out early.
703 if (noresume)
704 return 0;
707 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
708 * is configured into the kernel. Since the regular hibernate
709 * trigger path is via sysfs which takes a buffer mutex before
710 * calling hibernate functions (which take pm_mutex) this can
711 * cause lockdep to complain about a possible ABBA deadlock
712 * which cannot happen since we're in the boot code here and
713 * sysfs can't be invoked yet. Therefore, we use a subclass
714 * here to avoid lockdep complaining.
716 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
718 if (swsusp_resume_device)
719 goto Check_image;
721 if (!strlen(resume_file)) {
722 error = -ENOENT;
723 goto Unlock;
726 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
728 /* Check if the device is there */
729 swsusp_resume_device = name_to_dev_t(resume_file);
730 if (!swsusp_resume_device) {
732 * Some device discovery might still be in progress; we need
733 * to wait for this to finish.
735 wait_for_device_probe();
737 * We can't depend on SCSI devices being available after loading
738 * one of their modules until scsi_complete_async_scans() is
739 * called and the resume device usually is a SCSI one.
741 scsi_complete_async_scans();
743 swsusp_resume_device = name_to_dev_t(resume_file);
744 if (!swsusp_resume_device) {
745 error = -ENODEV;
746 goto Unlock;
750 Check_image:
751 pr_debug("PM: Hibernation image partition %d:%d present\n",
752 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
754 pr_debug("PM: Looking for hibernation image.\n");
755 error = swsusp_check();
756 if (error)
757 goto Unlock;
759 /* The snapshot device should not be opened while we're running */
760 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
761 error = -EBUSY;
762 swsusp_close(FMODE_READ);
763 goto Unlock;
766 pm_prepare_console();
767 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
768 if (error)
769 goto close_finish;
771 error = usermodehelper_disable();
772 if (error)
773 goto close_finish;
775 error = create_basic_memory_bitmaps();
776 if (error)
777 goto close_finish;
779 pr_debug("PM: Preparing processes for restore.\n");
780 error = prepare_processes();
781 if (error) {
782 swsusp_close(FMODE_READ);
783 goto Done;
786 pr_debug("PM: Loading hibernation image.\n");
788 error = swsusp_read(&flags);
789 swsusp_close(FMODE_READ);
790 if (!error)
791 hibernation_restore(flags & SF_PLATFORM_MODE);
793 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
794 swsusp_free();
795 thaw_processes();
796 Done:
797 free_basic_memory_bitmaps();
798 usermodehelper_enable();
799 Finish:
800 pm_notifier_call_chain(PM_POST_RESTORE);
801 pm_restore_console();
802 atomic_inc(&snapshot_device_available);
803 /* For success case, the suspend path will release the lock */
804 Unlock:
805 mutex_unlock(&pm_mutex);
806 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
807 return error;
808 close_finish:
809 swsusp_close(FMODE_READ);
810 goto Finish;
813 late_initcall(software_resume);
816 static const char * const hibernation_modes[] = {
817 [HIBERNATION_PLATFORM] = "platform",
818 [HIBERNATION_SHUTDOWN] = "shutdown",
819 [HIBERNATION_REBOOT] = "reboot",
820 [HIBERNATION_TEST] = "test",
821 [HIBERNATION_TESTPROC] = "testproc",
825 * /sys/power/disk - Control hibernation mode.
827 * Hibernation can be handled in several ways. There are a few different ways
828 * to put the system into the sleep state: using the platform driver (e.g. ACPI
829 * or other hibernation_ops), powering it off or rebooting it (for testing
830 * mostly), or using one of the two available test modes.
832 * The sysfs file /sys/power/disk provides an interface for selecting the
833 * hibernation mode to use. Reading from this file causes the available modes
834 * to be printed. There are 5 modes that can be supported:
836 * 'platform'
837 * 'shutdown'
838 * 'reboot'
839 * 'test'
840 * 'testproc'
842 * If a platform hibernation driver is in use, 'platform' will be supported
843 * and will be used by default. Otherwise, 'shutdown' will be used by default.
844 * The selected option (i.e. the one corresponding to the current value of
845 * hibernation_mode) is enclosed by a square bracket.
847 * To select a given hibernation mode it is necessary to write the mode's
848 * string representation (as returned by reading from /sys/power/disk) back
849 * into /sys/power/disk.
852 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
853 char *buf)
855 int i;
856 char *start = buf;
858 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
859 if (!hibernation_modes[i])
860 continue;
861 switch (i) {
862 case HIBERNATION_SHUTDOWN:
863 case HIBERNATION_REBOOT:
864 case HIBERNATION_TEST:
865 case HIBERNATION_TESTPROC:
866 break;
867 case HIBERNATION_PLATFORM:
868 if (hibernation_ops)
869 break;
870 /* not a valid mode, continue with loop */
871 continue;
873 if (i == hibernation_mode)
874 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
875 else
876 buf += sprintf(buf, "%s ", hibernation_modes[i]);
878 buf += sprintf(buf, "\n");
879 return buf-start;
882 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
883 const char *buf, size_t n)
885 int error = 0;
886 int i;
887 int len;
888 char *p;
889 int mode = HIBERNATION_INVALID;
891 p = memchr(buf, '\n', n);
892 len = p ? p - buf : n;
894 mutex_lock(&pm_mutex);
895 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
896 if (len == strlen(hibernation_modes[i])
897 && !strncmp(buf, hibernation_modes[i], len)) {
898 mode = i;
899 break;
902 if (mode != HIBERNATION_INVALID) {
903 switch (mode) {
904 case HIBERNATION_SHUTDOWN:
905 case HIBERNATION_REBOOT:
906 case HIBERNATION_TEST:
907 case HIBERNATION_TESTPROC:
908 hibernation_mode = mode;
909 break;
910 case HIBERNATION_PLATFORM:
911 if (hibernation_ops)
912 hibernation_mode = mode;
913 else
914 error = -EINVAL;
916 } else
917 error = -EINVAL;
919 if (!error)
920 pr_debug("PM: Hibernation mode set to '%s'\n",
921 hibernation_modes[mode]);
922 mutex_unlock(&pm_mutex);
923 return error ? error : n;
926 power_attr(disk);
928 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
929 char *buf)
931 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
932 MINOR(swsusp_resume_device));
935 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
936 const char *buf, size_t n)
938 unsigned int maj, min;
939 dev_t res;
940 int ret = -EINVAL;
942 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
943 goto out;
945 res = MKDEV(maj,min);
946 if (maj != MAJOR(res) || min != MINOR(res))
947 goto out;
949 mutex_lock(&pm_mutex);
950 swsusp_resume_device = res;
951 mutex_unlock(&pm_mutex);
952 printk(KERN_INFO "PM: Starting manual resume from disk\n");
953 noresume = 0;
954 software_resume();
955 ret = n;
956 out:
957 return ret;
960 power_attr(resume);
962 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
963 char *buf)
965 return sprintf(buf, "%lu\n", image_size);
968 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
969 const char *buf, size_t n)
971 unsigned long size;
973 if (sscanf(buf, "%lu", &size) == 1) {
974 image_size = size;
975 return n;
978 return -EINVAL;
981 power_attr(image_size);
983 static ssize_t reserved_size_show(struct kobject *kobj,
984 struct kobj_attribute *attr, char *buf)
986 return sprintf(buf, "%lu\n", reserved_size);
989 static ssize_t reserved_size_store(struct kobject *kobj,
990 struct kobj_attribute *attr,
991 const char *buf, size_t n)
993 unsigned long size;
995 if (sscanf(buf, "%lu", &size) == 1) {
996 reserved_size = size;
997 return n;
1000 return -EINVAL;
1003 power_attr(reserved_size);
1005 static struct attribute * g[] = {
1006 &disk_attr.attr,
1007 &resume_attr.attr,
1008 &image_size_attr.attr,
1009 &reserved_size_attr.attr,
1010 NULL,
1014 static struct attribute_group attr_group = {
1015 .attrs = g,
1019 static int __init pm_disk_init(void)
1021 return sysfs_create_group(power_kobj, &attr_group);
1024 core_initcall(pm_disk_init);
1027 static int __init resume_setup(char *str)
1029 if (noresume)
1030 return 1;
1032 strncpy( resume_file, str, 255 );
1033 return 1;
1036 static int __init resume_offset_setup(char *str)
1038 unsigned long long offset;
1040 if (noresume)
1041 return 1;
1043 if (sscanf(str, "%llu", &offset) == 1)
1044 swsusp_resume_block = offset;
1046 return 1;
1049 static int __init hibernate_setup(char *str)
1051 if (!strncmp(str, "noresume", 8))
1052 noresume = 1;
1053 else if (!strncmp(str, "nocompress", 10))
1054 nocompress = 1;
1055 return 1;
1058 static int __init noresume_setup(char *str)
1060 noresume = 1;
1061 return 1;
1064 __setup("noresume", noresume_setup);
1065 __setup("resume_offset=", resume_offset_setup);
1066 __setup("resume=", resume_setup);
1067 __setup("hibernate=", hibernate_setup);