disk.c 20 KB

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