disk.c 21 KB

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