disk.c 20 KB

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