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