disk.c 21 KB

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