disk.c 20 KB

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