disk.c 20 KB

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