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