]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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/delay.h> | |
18 | #include <linux/fs.h> | |
d53d9f16 | 19 | #include <linux/mount.h> |
88d10bba | 20 | #include <linux/pm.h> |
97c7801c | 21 | #include <linux/console.h> |
e3920fb4 | 22 | #include <linux/cpu.h> |
7dfb7103 | 23 | #include <linux/freezer.h> |
d53d9f16 | 24 | |
1da177e4 LT |
25 | #include "power.h" |
26 | ||
27 | ||
1da177e4 LT |
28 | static int noresume = 0; |
29 | char resume_file[256] = CONFIG_PM_STD_PARTITION; | |
30 | dev_t swsusp_resume_device; | |
9a154d9d | 31 | sector_t swsusp_resume_block; |
1da177e4 | 32 | |
a3d25c27 RW |
33 | enum { |
34 | HIBERNATION_INVALID, | |
35 | HIBERNATION_PLATFORM, | |
36 | HIBERNATION_TEST, | |
37 | HIBERNATION_TESTPROC, | |
38 | HIBERNATION_SHUTDOWN, | |
39 | HIBERNATION_REBOOT, | |
40 | /* keep last */ | |
41 | __HIBERNATION_AFTER_LAST | |
42 | }; | |
43 | #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1) | |
44 | #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1) | |
45 | ||
46 | static int hibernation_mode = HIBERNATION_SHUTDOWN; | |
47 | ||
b3dac3b3 | 48 | static struct platform_hibernation_ops *hibernation_ops; |
a3d25c27 RW |
49 | |
50 | /** | |
51 | * hibernation_set_ops - set the global hibernate operations | |
52 | * @ops: the hibernation operations to use in subsequent hibernation transitions | |
53 | */ | |
54 | ||
b3dac3b3 | 55 | void hibernation_set_ops(struct platform_hibernation_ops *ops) |
a3d25c27 | 56 | { |
74f270af RW |
57 | if (ops && !(ops->start && ops->pre_snapshot && ops->finish |
58 | && ops->prepare && ops->enter && ops->pre_restore | |
59 | && ops->restore_cleanup)) { | |
a3d25c27 RW |
60 | WARN_ON(1); |
61 | return; | |
62 | } | |
63 | mutex_lock(&pm_mutex); | |
64 | hibernation_ops = ops; | |
65 | if (ops) | |
66 | hibernation_mode = HIBERNATION_PLATFORM; | |
67 | else if (hibernation_mode == HIBERNATION_PLATFORM) | |
68 | hibernation_mode = HIBERNATION_SHUTDOWN; | |
69 | ||
70 | mutex_unlock(&pm_mutex); | |
71 | } | |
72 | ||
74f270af RW |
73 | /** |
74 | * platform_start - tell the platform driver that we're starting | |
75 | * hibernation | |
76 | */ | |
77 | ||
78 | static int platform_start(int platform_mode) | |
79 | { | |
80 | return (platform_mode && hibernation_ops) ? | |
81 | hibernation_ops->start() : 0; | |
82 | } | |
a3d25c27 | 83 | |
8a05aac2 | 84 | /** |
74f270af | 85 | * platform_pre_snapshot - prepare the machine for hibernation using the |
8a05aac2 SS |
86 | * platform driver if so configured and return an error code if it fails |
87 | */ | |
88 | ||
74f270af | 89 | static int platform_pre_snapshot(int platform_mode) |
8a05aac2 | 90 | { |
7777fab9 | 91 | return (platform_mode && hibernation_ops) ? |
74f270af | 92 | hibernation_ops->pre_snapshot() : 0; |
a3d25c27 | 93 | } |
8a05aac2 | 94 | |
c7e0831d RW |
95 | /** |
96 | * platform_leave - prepare the machine for switching to the normal mode | |
97 | * of operation using the platform driver (called with interrupts disabled) | |
98 | */ | |
99 | ||
100 | static void platform_leave(int platform_mode) | |
101 | { | |
102 | if (platform_mode && hibernation_ops) | |
103 | hibernation_ops->leave(); | |
104 | } | |
105 | ||
a3d25c27 RW |
106 | /** |
107 | * platform_finish - switch the machine to the normal mode of operation | |
108 | * using the platform driver (must be called after platform_prepare()) | |
109 | */ | |
110 | ||
7777fab9 | 111 | static void platform_finish(int platform_mode) |
a3d25c27 | 112 | { |
7777fab9 | 113 | if (platform_mode && hibernation_ops) |
a3d25c27 | 114 | hibernation_ops->finish(); |
8a05aac2 SS |
115 | } |
116 | ||
a634cc10 RW |
117 | /** |
118 | * platform_pre_restore - prepare the platform for the restoration from a | |
119 | * hibernation image. If the restore fails after this function has been | |
120 | * called, platform_restore_cleanup() must be called. | |
121 | */ | |
122 | ||
123 | static int platform_pre_restore(int platform_mode) | |
124 | { | |
125 | return (platform_mode && hibernation_ops) ? | |
126 | hibernation_ops->pre_restore() : 0; | |
127 | } | |
128 | ||
129 | /** | |
130 | * platform_restore_cleanup - switch the platform to the normal mode of | |
131 | * operation after a failing restore. If platform_pre_restore() has been | |
132 | * called before the failing restore, this function must be called too, | |
133 | * regardless of the result of platform_pre_restore(). | |
134 | */ | |
135 | ||
136 | static void platform_restore_cleanup(int platform_mode) | |
137 | { | |
138 | if (platform_mode && hibernation_ops) | |
139 | hibernation_ops->restore_cleanup(); | |
140 | } | |
141 | ||
c7e0831d RW |
142 | /** |
143 | * create_image - freeze devices that need to be frozen with interrupts | |
144 | * off, create the hibernation image and thaw those devices. Control | |
145 | * reappears in this routine after a restore. | |
146 | */ | |
147 | ||
148 | int create_image(int platform_mode) | |
149 | { | |
150 | int error; | |
151 | ||
152 | error = arch_prepare_suspend(); | |
153 | if (error) | |
154 | return error; | |
155 | ||
156 | local_irq_disable(); | |
157 | /* At this point, device_suspend() has been called, but *not* | |
158 | * device_power_down(). We *must* call device_power_down() now. | |
159 | * Otherwise, drivers for some devices (e.g. interrupt controllers) | |
160 | * become desynchronized with the actual state of the hardware | |
161 | * at resume time, and evil weirdness ensues. | |
162 | */ | |
163 | error = device_power_down(PMSG_FREEZE); | |
164 | if (error) { | |
165 | printk(KERN_ERR "Some devices failed to power down, " | |
166 | KERN_ERR "aborting suspend\n"); | |
167 | goto Enable_irqs; | |
168 | } | |
169 | ||
170 | save_processor_state(); | |
171 | error = swsusp_arch_suspend(); | |
172 | if (error) | |
173 | printk(KERN_ERR "Error %d while creating the image\n", error); | |
174 | /* Restore control flow magically appears here */ | |
175 | restore_processor_state(); | |
176 | if (!in_suspend) | |
177 | platform_leave(platform_mode); | |
178 | /* NOTE: device_power_up() is just a resume() for devices | |
179 | * that suspended with irqs off ... no overall powerup. | |
180 | */ | |
181 | device_power_up(); | |
182 | Enable_irqs: | |
183 | local_irq_enable(); | |
184 | return error; | |
185 | } | |
186 | ||
7777fab9 RW |
187 | /** |
188 | * hibernation_snapshot - quiesce devices and create the hibernation | |
189 | * snapshot image. | |
190 | * @platform_mode - if set, use the platform driver, if available, to | |
191 | * prepare the platform frimware for the power transition. | |
192 | * | |
193 | * Must be called with pm_mutex held | |
194 | */ | |
195 | ||
196 | int hibernation_snapshot(int platform_mode) | |
197 | { | |
198 | int error; | |
199 | ||
200 | /* Free memory before shutting down devices. */ | |
201 | error = swsusp_shrink_memory(); | |
202 | if (error) | |
10a1803d | 203 | return error; |
7777fab9 | 204 | |
74f270af RW |
205 | error = platform_start(platform_mode); |
206 | if (error) | |
207 | return error; | |
208 | ||
7777fab9 RW |
209 | suspend_console(); |
210 | error = device_suspend(PMSG_FREEZE); | |
10a1803d RW |
211 | if (error) |
212 | goto Resume_console; | |
213 | ||
74f270af | 214 | error = platform_pre_snapshot(platform_mode); |
7777fab9 RW |
215 | if (error) |
216 | goto Resume_devices; | |
217 | ||
218 | error = disable_nonboot_cpus(); | |
219 | if (!error) { | |
220 | if (hibernation_mode != HIBERNATION_TEST) { | |
221 | in_suspend = 1; | |
c7e0831d | 222 | error = create_image(platform_mode); |
7777fab9 RW |
223 | /* Control returns here after successful restore */ |
224 | } else { | |
225 | printk("swsusp debug: Waiting for 5 seconds.\n"); | |
226 | mdelay(5000); | |
227 | } | |
228 | } | |
229 | enable_nonboot_cpus(); | |
230 | Resume_devices: | |
231 | platform_finish(platform_mode); | |
232 | device_resume(); | |
10a1803d | 233 | Resume_console: |
7777fab9 | 234 | resume_console(); |
7777fab9 RW |
235 | return error; |
236 | } | |
237 | ||
238 | /** | |
239 | * hibernation_restore - quiesce devices and restore the hibernation | |
240 | * snapshot image. If successful, control returns in hibernation_snaphot() | |
a634cc10 RW |
241 | * @platform_mode - if set, use the platform driver, if available, to |
242 | * prepare the platform frimware for the transition. | |
7777fab9 RW |
243 | * |
244 | * Must be called with pm_mutex held | |
245 | */ | |
246 | ||
a634cc10 | 247 | int hibernation_restore(int platform_mode) |
7777fab9 RW |
248 | { |
249 | int error; | |
250 | ||
251 | pm_prepare_console(); | |
252 | suspend_console(); | |
253 | error = device_suspend(PMSG_PRETHAW); | |
254 | if (error) | |
255 | goto Finish; | |
256 | ||
a634cc10 RW |
257 | error = platform_pre_restore(platform_mode); |
258 | if (!error) { | |
259 | error = disable_nonboot_cpus(); | |
260 | if (!error) | |
261 | error = swsusp_resume(); | |
262 | enable_nonboot_cpus(); | |
263 | } | |
264 | platform_restore_cleanup(platform_mode); | |
7777fab9 | 265 | device_resume(); |
10a1803d | 266 | Finish: |
7777fab9 RW |
267 | resume_console(); |
268 | pm_restore_console(); | |
269 | return error; | |
270 | } | |
271 | ||
272 | /** | |
273 | * hibernation_platform_enter - enter the hibernation state using the | |
274 | * platform driver (if available) | |
275 | */ | |
276 | ||
277 | int hibernation_platform_enter(void) | |
278 | { | |
b1457bcc RW |
279 | int error; |
280 | ||
9cd9a005 RW |
281 | if (!hibernation_ops) |
282 | return -ENOSYS; | |
283 | ||
284 | /* | |
285 | * We have cancelled the power transition by running | |
286 | * hibernation_ops->finish() before saving the image, so we should let | |
287 | * the firmware know that we're going to enter the sleep state after all | |
288 | */ | |
289 | error = hibernation_ops->start(); | |
290 | if (error) | |
291 | return error; | |
292 | ||
293 | suspend_console(); | |
294 | error = device_suspend(PMSG_SUSPEND); | |
295 | if (error) | |
296 | goto Resume_console; | |
297 | ||
298 | error = hibernation_ops->prepare(); | |
299 | if (error) | |
300 | goto Resume_devices; | |
301 | ||
302 | error = disable_nonboot_cpus(); | |
303 | if (error) | |
304 | goto Finish; | |
305 | ||
306 | local_irq_disable(); | |
307 | error = device_power_down(PMSG_SUSPEND); | |
308 | if (!error) { | |
309 | hibernation_ops->enter(); | |
310 | /* We should never get here */ | |
311 | while (1); | |
7777fab9 | 312 | } |
9cd9a005 RW |
313 | local_irq_enable(); |
314 | ||
315 | /* | |
316 | * We don't need to reenable the nonboot CPUs or resume consoles, since | |
317 | * the system is going to be halted anyway. | |
318 | */ | |
319 | Finish: | |
320 | hibernation_ops->finish(); | |
321 | Resume_devices: | |
322 | device_resume(); | |
323 | Resume_console: | |
324 | resume_console(); | |
b1457bcc | 325 | return error; |
7777fab9 RW |
326 | } |
327 | ||
1da177e4 | 328 | /** |
a3d25c27 | 329 | * power_down - Shut the machine down for hibernation. |
1da177e4 | 330 | * |
fe0c935a JB |
331 | * Use the platform driver, if configured so; otherwise try |
332 | * to power off or reboot. | |
1da177e4 LT |
333 | */ |
334 | ||
fe0c935a | 335 | static void power_down(void) |
1da177e4 | 336 | { |
a3d25c27 RW |
337 | switch (hibernation_mode) { |
338 | case HIBERNATION_TEST: | |
339 | case HIBERNATION_TESTPROC: | |
fe0c935a | 340 | break; |
a3d25c27 | 341 | case HIBERNATION_REBOOT: |
fdde86ac | 342 | kernel_restart(NULL); |
1da177e4 | 343 | break; |
a3d25c27 | 344 | case HIBERNATION_PLATFORM: |
7777fab9 | 345 | hibernation_platform_enter(); |
9cd9a005 RW |
346 | case HIBERNATION_SHUTDOWN: |
347 | kernel_power_off(); | |
348 | break; | |
1da177e4 | 349 | } |
fdde86ac | 350 | kernel_halt(); |
fe0c935a JB |
351 | /* |
352 | * Valid image is on the disk, if we continue we risk serious data | |
353 | * corruption after resume. | |
354 | */ | |
1da177e4 LT |
355 | printk(KERN_CRIT "Please power me down manually\n"); |
356 | while(1); | |
357 | } | |
358 | ||
ed746e3b RW |
359 | static void unprepare_processes(void) |
360 | { | |
361 | thaw_processes(); | |
362 | pm_restore_console(); | |
363 | } | |
364 | ||
1da177e4 LT |
365 | static int prepare_processes(void) |
366 | { | |
b918f6e6 | 367 | int error = 0; |
1da177e4 LT |
368 | |
369 | pm_prepare_console(); | |
1da177e4 LT |
370 | if (freeze_processes()) { |
371 | error = -EBUSY; | |
ed746e3b | 372 | unprepare_processes(); |
1da177e4 | 373 | } |
5a72e04d | 374 | return error; |
1da177e4 LT |
375 | } |
376 | ||
1da177e4 | 377 | /** |
a3d25c27 | 378 | * hibernate - The granpappy of the built-in hibernation management |
1da177e4 LT |
379 | */ |
380 | ||
a3d25c27 | 381 | int hibernate(void) |
1da177e4 LT |
382 | { |
383 | int error; | |
384 | ||
b10d9117 | 385 | mutex_lock(&pm_mutex); |
0709db60 | 386 | /* The snapshot device should not be opened while we're running */ |
b10d9117 RW |
387 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { |
388 | error = -EBUSY; | |
389 | goto Unlock; | |
390 | } | |
391 | ||
392 | error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE); | |
393 | if (error) | |
394 | goto Exit; | |
0709db60 RW |
395 | |
396 | /* Allocate memory management structures */ | |
397 | error = create_basic_memory_bitmaps(); | |
398 | if (error) | |
399 | goto Exit; | |
400 | ||
232b1432 RW |
401 | printk("Syncing filesystems ... "); |
402 | sys_sync(); | |
403 | printk("done.\n"); | |
404 | ||
1da177e4 | 405 | error = prepare_processes(); |
5a72e04d | 406 | if (error) |
0709db60 | 407 | goto Finish; |
1da177e4 | 408 | |
a3d25c27 | 409 | if (hibernation_mode == HIBERNATION_TESTPROC) { |
ed746e3b RW |
410 | printk("swsusp debug: Waiting for 5 seconds.\n"); |
411 | mdelay(5000); | |
412 | goto Thaw; | |
413 | } | |
7777fab9 RW |
414 | error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); |
415 | if (in_suspend && !error) { | |
a634cc10 RW |
416 | unsigned int flags = 0; |
417 | ||
418 | if (hibernation_mode == HIBERNATION_PLATFORM) | |
419 | flags |= SF_PLATFORM_MODE; | |
1da177e4 | 420 | pr_debug("PM: writing image.\n"); |
a634cc10 | 421 | error = swsusp_write(flags); |
7777fab9 | 422 | swsusp_free(); |
1da177e4 | 423 | if (!error) |
fe0c935a | 424 | power_down(); |
b918f6e6 | 425 | } else { |
1da177e4 | 426 | pr_debug("PM: Image restored successfully.\n"); |
7777fab9 | 427 | swsusp_free(); |
b918f6e6 | 428 | } |
b918f6e6 | 429 | Thaw: |
99dc7d63 | 430 | unprepare_processes(); |
0709db60 RW |
431 | Finish: |
432 | free_basic_memory_bitmaps(); | |
433 | Exit: | |
b10d9117 | 434 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
0709db60 | 435 | atomic_inc(&snapshot_device_available); |
b10d9117 RW |
436 | Unlock: |
437 | mutex_unlock(&pm_mutex); | |
1da177e4 LT |
438 | return error; |
439 | } | |
440 | ||
441 | ||
442 | /** | |
443 | * software_resume - Resume from a saved image. | |
444 | * | |
445 | * Called as a late_initcall (so all devices are discovered and | |
446 | * initialized), we call swsusp to see if we have a saved image or not. | |
447 | * If so, we quiesce devices, the restore the saved image. We will | |
a3d25c27 | 448 | * return above (in hibernate() ) if everything goes well. |
1da177e4 LT |
449 | * Otherwise, we fail gracefully and return to the normally |
450 | * scheduled program. | |
451 | * | |
452 | */ | |
453 | ||
454 | static int software_resume(void) | |
455 | { | |
456 | int error; | |
a634cc10 | 457 | unsigned int flags; |
1da177e4 | 458 | |
a6d70980 | 459 | mutex_lock(&pm_mutex); |
3efa147a | 460 | if (!swsusp_resume_device) { |
dd5d666b | 461 | if (!strlen(resume_file)) { |
a6d70980 | 462 | mutex_unlock(&pm_mutex); |
3efa147a | 463 | return -ENOENT; |
dd5d666b | 464 | } |
3efa147a PM |
465 | swsusp_resume_device = name_to_dev_t(resume_file); |
466 | pr_debug("swsusp: Resume From Partition %s\n", resume_file); | |
467 | } else { | |
468 | pr_debug("swsusp: Resume From Partition %d:%d\n", | |
469 | MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); | |
470 | } | |
471 | ||
1da177e4 LT |
472 | if (noresume) { |
473 | /** | |
474 | * FIXME: If noresume is specified, we need to find the partition | |
475 | * and reset it back to normal swap space. | |
476 | */ | |
a6d70980 | 477 | mutex_unlock(&pm_mutex); |
1da177e4 LT |
478 | return 0; |
479 | } | |
480 | ||
481 | pr_debug("PM: Checking swsusp image.\n"); | |
ed746e3b RW |
482 | error = swsusp_check(); |
483 | if (error) | |
74dfd666 | 484 | goto Unlock; |
1da177e4 | 485 | |
0709db60 RW |
486 | /* The snapshot device should not be opened while we're running */ |
487 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { | |
488 | error = -EBUSY; | |
489 | goto Unlock; | |
490 | } | |
491 | ||
74dfd666 RW |
492 | error = create_basic_memory_bitmaps(); |
493 | if (error) | |
0709db60 | 494 | goto Finish; |
1da177e4 | 495 | |
74dfd666 | 496 | pr_debug("PM: Preparing processes for restore.\n"); |
ed746e3b RW |
497 | error = prepare_processes(); |
498 | if (error) { | |
1da177e4 | 499 | swsusp_close(); |
5a72e04d | 500 | goto Done; |
1da177e4 LT |
501 | } |
502 | ||
503 | pr_debug("PM: Reading swsusp image.\n"); | |
504 | ||
a634cc10 | 505 | error = swsusp_read(&flags); |
ed746e3b | 506 | if (!error) |
a634cc10 | 507 | hibernation_restore(flags & SF_PLATFORM_MODE); |
1da177e4 | 508 | |
ed746e3b | 509 | printk(KERN_ERR "PM: Restore failed, recovering.\n"); |
7777fab9 | 510 | swsusp_free(); |
1da177e4 LT |
511 | unprepare_processes(); |
512 | Done: | |
74dfd666 | 513 | free_basic_memory_bitmaps(); |
0709db60 RW |
514 | Finish: |
515 | atomic_inc(&snapshot_device_available); | |
dd5d666b | 516 | /* For success case, the suspend path will release the lock */ |
74dfd666 | 517 | Unlock: |
a6d70980 | 518 | mutex_unlock(&pm_mutex); |
1da177e4 | 519 | pr_debug("PM: Resume from disk failed.\n"); |
7777fab9 | 520 | return error; |
1da177e4 LT |
521 | } |
522 | ||
523 | late_initcall(software_resume); | |
524 | ||
525 | ||
a3d25c27 RW |
526 | static const char * const hibernation_modes[] = { |
527 | [HIBERNATION_PLATFORM] = "platform", | |
528 | [HIBERNATION_SHUTDOWN] = "shutdown", | |
529 | [HIBERNATION_REBOOT] = "reboot", | |
530 | [HIBERNATION_TEST] = "test", | |
531 | [HIBERNATION_TESTPROC] = "testproc", | |
1da177e4 LT |
532 | }; |
533 | ||
534 | /** | |
a3d25c27 | 535 | * disk - Control hibernation mode |
1da177e4 | 536 | * |
11d77d0c JB |
537 | * Suspend-to-disk can be handled in several ways. We have a few options |
538 | * for putting the system to sleep - using the platform driver (e.g. ACPI | |
a3d25c27 RW |
539 | * or other hibernation_ops), powering off the system or rebooting the |
540 | * system (for testing) as well as the two test modes. | |
1da177e4 | 541 | * |
11d77d0c | 542 | * The system can support 'platform', and that is known a priori (and |
a3d25c27 RW |
543 | * encoded by the presence of hibernation_ops). However, the user may |
544 | * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the | |
545 | * test modes, 'test' or 'testproc'. | |
1da177e4 LT |
546 | * |
547 | * show() will display what the mode is currently set to. | |
548 | * store() will accept one of | |
549 | * | |
1da177e4 LT |
550 | * 'platform' |
551 | * 'shutdown' | |
552 | * 'reboot' | |
11d77d0c JB |
553 | * 'test' |
554 | * 'testproc' | |
1da177e4 | 555 | * |
11d77d0c | 556 | * It will only change to 'platform' if the system |
a3d25c27 | 557 | * supports it (as determined by having hibernation_ops). |
1da177e4 LT |
558 | */ |
559 | ||
823bccfc | 560 | static ssize_t disk_show(struct kset *kset, char *buf) |
1da177e4 | 561 | { |
f0ced9b2 JB |
562 | int i; |
563 | char *start = buf; | |
564 | ||
a3d25c27 RW |
565 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
566 | if (!hibernation_modes[i]) | |
f0ced9b2 JB |
567 | continue; |
568 | switch (i) { | |
a3d25c27 RW |
569 | case HIBERNATION_SHUTDOWN: |
570 | case HIBERNATION_REBOOT: | |
571 | case HIBERNATION_TEST: | |
572 | case HIBERNATION_TESTPROC: | |
f0ced9b2 | 573 | break; |
a3d25c27 RW |
574 | case HIBERNATION_PLATFORM: |
575 | if (hibernation_ops) | |
f0ced9b2 JB |
576 | break; |
577 | /* not a valid mode, continue with loop */ | |
578 | continue; | |
579 | } | |
a3d25c27 RW |
580 | if (i == hibernation_mode) |
581 | buf += sprintf(buf, "[%s] ", hibernation_modes[i]); | |
f0ced9b2 | 582 | else |
a3d25c27 | 583 | buf += sprintf(buf, "%s ", hibernation_modes[i]); |
f0ced9b2 JB |
584 | } |
585 | buf += sprintf(buf, "\n"); | |
586 | return buf-start; | |
1da177e4 LT |
587 | } |
588 | ||
589 | ||
823bccfc | 590 | static ssize_t disk_store(struct kset *kset, const char *buf, size_t n) |
1da177e4 LT |
591 | { |
592 | int error = 0; | |
593 | int i; | |
594 | int len; | |
595 | char *p; | |
a3d25c27 | 596 | int mode = HIBERNATION_INVALID; |
1da177e4 LT |
597 | |
598 | p = memchr(buf, '\n', n); | |
599 | len = p ? p - buf : n; | |
600 | ||
a6d70980 | 601 | mutex_lock(&pm_mutex); |
a3d25c27 | 602 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
8d98a690 RW |
603 | if (len == strlen(hibernation_modes[i]) |
604 | && !strncmp(buf, hibernation_modes[i], len)) { | |
1da177e4 LT |
605 | mode = i; |
606 | break; | |
607 | } | |
608 | } | |
a3d25c27 | 609 | if (mode != HIBERNATION_INVALID) { |
fe0c935a | 610 | switch (mode) { |
a3d25c27 RW |
611 | case HIBERNATION_SHUTDOWN: |
612 | case HIBERNATION_REBOOT: | |
613 | case HIBERNATION_TEST: | |
614 | case HIBERNATION_TESTPROC: | |
615 | hibernation_mode = mode; | |
fe0c935a | 616 | break; |
a3d25c27 RW |
617 | case HIBERNATION_PLATFORM: |
618 | if (hibernation_ops) | |
619 | hibernation_mode = mode; | |
1da177e4 LT |
620 | else |
621 | error = -EINVAL; | |
622 | } | |
a3d25c27 | 623 | } else |
1da177e4 LT |
624 | error = -EINVAL; |
625 | ||
a3d25c27 RW |
626 | if (!error) |
627 | pr_debug("PM: suspend-to-disk mode set to '%s'\n", | |
628 | hibernation_modes[mode]); | |
a6d70980 | 629 | mutex_unlock(&pm_mutex); |
1da177e4 LT |
630 | return error ? error : n; |
631 | } | |
632 | ||
633 | power_attr(disk); | |
634 | ||
823bccfc | 635 | static ssize_t resume_show(struct kset *kset, char *buf) |
1da177e4 LT |
636 | { |
637 | return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), | |
638 | MINOR(swsusp_resume_device)); | |
639 | } | |
640 | ||
823bccfc | 641 | static ssize_t resume_store(struct kset *kset, const char *buf, size_t n) |
1da177e4 | 642 | { |
1da177e4 | 643 | unsigned int maj, min; |
1da177e4 | 644 | dev_t res; |
a576219a | 645 | int ret = -EINVAL; |
1da177e4 | 646 | |
a576219a AM |
647 | if (sscanf(buf, "%u:%u", &maj, &min) != 2) |
648 | goto out; | |
1da177e4 | 649 | |
a576219a AM |
650 | res = MKDEV(maj,min); |
651 | if (maj != MAJOR(res) || min != MINOR(res)) | |
652 | goto out; | |
1da177e4 | 653 | |
a6d70980 | 654 | mutex_lock(&pm_mutex); |
a576219a | 655 | swsusp_resume_device = res; |
a6d70980 | 656 | mutex_unlock(&pm_mutex); |
a576219a AM |
657 | printk("Attempting manual resume\n"); |
658 | noresume = 0; | |
659 | software_resume(); | |
660 | ret = n; | |
59a49335 | 661 | out: |
a576219a | 662 | return ret; |
1da177e4 LT |
663 | } |
664 | ||
665 | power_attr(resume); | |
666 | ||
823bccfc | 667 | static ssize_t image_size_show(struct kset *kset, char *buf) |
ca0aec0f | 668 | { |
853609b6 | 669 | return sprintf(buf, "%lu\n", image_size); |
ca0aec0f RW |
670 | } |
671 | ||
823bccfc | 672 | static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n) |
ca0aec0f | 673 | { |
853609b6 | 674 | unsigned long size; |
ca0aec0f | 675 | |
853609b6 | 676 | if (sscanf(buf, "%lu", &size) == 1) { |
ca0aec0f RW |
677 | image_size = size; |
678 | return n; | |
679 | } | |
680 | ||
681 | return -EINVAL; | |
682 | } | |
683 | ||
684 | power_attr(image_size); | |
685 | ||
1da177e4 LT |
686 | static struct attribute * g[] = { |
687 | &disk_attr.attr, | |
688 | &resume_attr.attr, | |
ca0aec0f | 689 | &image_size_attr.attr, |
1da177e4 LT |
690 | NULL, |
691 | }; | |
692 | ||
693 | ||
694 | static struct attribute_group attr_group = { | |
695 | .attrs = g, | |
696 | }; | |
697 | ||
698 | ||
699 | static int __init pm_disk_init(void) | |
700 | { | |
823bccfc | 701 | return sysfs_create_group(&power_subsys.kobj, &attr_group); |
1da177e4 LT |
702 | } |
703 | ||
704 | core_initcall(pm_disk_init); | |
705 | ||
706 | ||
707 | static int __init resume_setup(char *str) | |
708 | { | |
709 | if (noresume) | |
710 | return 1; | |
711 | ||
712 | strncpy( resume_file, str, 255 ); | |
713 | return 1; | |
714 | } | |
715 | ||
9a154d9d RW |
716 | static int __init resume_offset_setup(char *str) |
717 | { | |
718 | unsigned long long offset; | |
719 | ||
720 | if (noresume) | |
721 | return 1; | |
722 | ||
723 | if (sscanf(str, "%llu", &offset) == 1) | |
724 | swsusp_resume_block = offset; | |
725 | ||
726 | return 1; | |
727 | } | |
728 | ||
1da177e4 LT |
729 | static int __init noresume_setup(char *str) |
730 | { | |
731 | noresume = 1; | |
732 | return 1; | |
733 | } | |
734 | ||
735 | __setup("noresume", noresume_setup); | |
9a154d9d | 736 | __setup("resume_offset=", resume_offset_setup); |
1da177e4 | 737 | __setup("resume=", resume_setup); |