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