disk.c 21 KB

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