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