disk.c 19 KB

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