disk.c 20 KB

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