disk.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. 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. 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_SUSPEND);
  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_SUSPEND);
  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 void unprepare_processes(void)
  400. {
  401. thaw_processes();
  402. pm_restore_console();
  403. }
  404. static int prepare_processes(void)
  405. {
  406. int error = 0;
  407. pm_prepare_console();
  408. if (freeze_processes()) {
  409. error = -EBUSY;
  410. unprepare_processes();
  411. }
  412. return error;
  413. }
  414. /**
  415. * hibernate - The granpappy of the built-in hibernation management
  416. */
  417. int hibernate(void)
  418. {
  419. int error;
  420. mutex_lock(&pm_mutex);
  421. /* The snapshot device should not be opened while we're running */
  422. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  423. error = -EBUSY;
  424. goto Unlock;
  425. }
  426. error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
  427. if (error)
  428. goto Exit;
  429. /* Allocate memory management structures */
  430. error = create_basic_memory_bitmaps();
  431. if (error)
  432. goto Exit;
  433. printk(KERN_INFO "PM: Syncing filesystems ... ");
  434. sys_sync();
  435. printk("done.\n");
  436. error = prepare_processes();
  437. if (error)
  438. goto Finish;
  439. if (hibernation_test(TEST_FREEZER))
  440. goto Thaw;
  441. if (hibernation_testmode(HIBERNATION_TESTPROC))
  442. goto Thaw;
  443. error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
  444. if (in_suspend && !error) {
  445. unsigned int flags = 0;
  446. if (hibernation_mode == HIBERNATION_PLATFORM)
  447. flags |= SF_PLATFORM_MODE;
  448. pr_debug("PM: writing image.\n");
  449. error = swsusp_write(flags);
  450. swsusp_free();
  451. if (!error)
  452. power_down();
  453. } else {
  454. pr_debug("PM: Image restored successfully.\n");
  455. swsusp_free();
  456. }
  457. Thaw:
  458. unprepare_processes();
  459. Finish:
  460. free_basic_memory_bitmaps();
  461. Exit:
  462. pm_notifier_call_chain(PM_POST_HIBERNATION);
  463. atomic_inc(&snapshot_device_available);
  464. Unlock:
  465. mutex_unlock(&pm_mutex);
  466. return error;
  467. }
  468. /**
  469. * software_resume - Resume from a saved image.
  470. *
  471. * Called as a late_initcall (so all devices are discovered and
  472. * initialized), we call swsusp to see if we have a saved image or not.
  473. * If so, we quiesce devices, the restore the saved image. We will
  474. * return above (in hibernate() ) if everything goes well.
  475. * Otherwise, we fail gracefully and return to the normally
  476. * scheduled program.
  477. *
  478. */
  479. static int software_resume(void)
  480. {
  481. int error;
  482. unsigned int flags;
  483. /*
  484. * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
  485. * is configured into the kernel. Since the regular hibernate
  486. * trigger path is via sysfs which takes a buffer mutex before
  487. * calling hibernate functions (which take pm_mutex) this can
  488. * cause lockdep to complain about a possible ABBA deadlock
  489. * which cannot happen since we're in the boot code here and
  490. * sysfs can't be invoked yet. Therefore, we use a subclass
  491. * here to avoid lockdep complaining.
  492. */
  493. mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
  494. if (!swsusp_resume_device) {
  495. if (!strlen(resume_file)) {
  496. mutex_unlock(&pm_mutex);
  497. return -ENOENT;
  498. }
  499. swsusp_resume_device = name_to_dev_t(resume_file);
  500. pr_debug("PM: Resume from partition %s\n", resume_file);
  501. } else {
  502. pr_debug("PM: Resume from partition %d:%d\n",
  503. MAJOR(swsusp_resume_device),
  504. MINOR(swsusp_resume_device));
  505. }
  506. if (noresume) {
  507. /**
  508. * FIXME: If noresume is specified, we need to find the
  509. * partition and reset it back to normal swap space.
  510. */
  511. mutex_unlock(&pm_mutex);
  512. return 0;
  513. }
  514. pr_debug("PM: Checking hibernation image.\n");
  515. error = swsusp_check();
  516. if (error)
  517. goto Unlock;
  518. /* The snapshot device should not be opened while we're running */
  519. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  520. error = -EBUSY;
  521. goto Unlock;
  522. }
  523. error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
  524. if (error)
  525. goto Finish;
  526. error = create_basic_memory_bitmaps();
  527. if (error)
  528. goto Finish;
  529. pr_debug("PM: Preparing processes for restore.\n");
  530. error = prepare_processes();
  531. if (error) {
  532. swsusp_close();
  533. goto Done;
  534. }
  535. pr_debug("PM: Reading hibernation image.\n");
  536. error = swsusp_read(&flags);
  537. if (!error)
  538. hibernation_restore(flags & SF_PLATFORM_MODE);
  539. printk(KERN_ERR "PM: Restore failed, recovering.\n");
  540. swsusp_free();
  541. unprepare_processes();
  542. Done:
  543. free_basic_memory_bitmaps();
  544. Finish:
  545. pm_notifier_call_chain(PM_POST_RESTORE);
  546. atomic_inc(&snapshot_device_available);
  547. /* For success case, the suspend path will release the lock */
  548. Unlock:
  549. mutex_unlock(&pm_mutex);
  550. pr_debug("PM: Resume from disk failed.\n");
  551. return error;
  552. }
  553. late_initcall(software_resume);
  554. static const char * const hibernation_modes[] = {
  555. [HIBERNATION_PLATFORM] = "platform",
  556. [HIBERNATION_SHUTDOWN] = "shutdown",
  557. [HIBERNATION_REBOOT] = "reboot",
  558. [HIBERNATION_TEST] = "test",
  559. [HIBERNATION_TESTPROC] = "testproc",
  560. };
  561. /**
  562. * disk - Control hibernation mode
  563. *
  564. * Suspend-to-disk can be handled in several ways. We have a few options
  565. * for putting the system to sleep - using the platform driver (e.g. ACPI
  566. * or other hibernation_ops), powering off the system or rebooting the
  567. * system (for testing) as well as the two test modes.
  568. *
  569. * The system can support 'platform', and that is known a priori (and
  570. * encoded by the presence of hibernation_ops). However, the user may
  571. * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
  572. * test modes, 'test' or 'testproc'.
  573. *
  574. * show() will display what the mode is currently set to.
  575. * store() will accept one of
  576. *
  577. * 'platform'
  578. * 'shutdown'
  579. * 'reboot'
  580. * 'test'
  581. * 'testproc'
  582. *
  583. * It will only change to 'platform' if the system
  584. * supports it (as determined by having hibernation_ops).
  585. */
  586. static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
  587. char *buf)
  588. {
  589. int i;
  590. char *start = buf;
  591. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  592. if (!hibernation_modes[i])
  593. continue;
  594. switch (i) {
  595. case HIBERNATION_SHUTDOWN:
  596. case HIBERNATION_REBOOT:
  597. case HIBERNATION_TEST:
  598. case HIBERNATION_TESTPROC:
  599. break;
  600. case HIBERNATION_PLATFORM:
  601. if (hibernation_ops)
  602. break;
  603. /* not a valid mode, continue with loop */
  604. continue;
  605. }
  606. if (i == hibernation_mode)
  607. buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
  608. else
  609. buf += sprintf(buf, "%s ", hibernation_modes[i]);
  610. }
  611. buf += sprintf(buf, "\n");
  612. return buf-start;
  613. }
  614. static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
  615. const char *buf, size_t n)
  616. {
  617. int error = 0;
  618. int i;
  619. int len;
  620. char *p;
  621. int mode = HIBERNATION_INVALID;
  622. p = memchr(buf, '\n', n);
  623. len = p ? p - buf : n;
  624. mutex_lock(&pm_mutex);
  625. for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
  626. if (len == strlen(hibernation_modes[i])
  627. && !strncmp(buf, hibernation_modes[i], len)) {
  628. mode = i;
  629. break;
  630. }
  631. }
  632. if (mode != HIBERNATION_INVALID) {
  633. switch (mode) {
  634. case HIBERNATION_SHUTDOWN:
  635. case HIBERNATION_REBOOT:
  636. case HIBERNATION_TEST:
  637. case HIBERNATION_TESTPROC:
  638. hibernation_mode = mode;
  639. break;
  640. case HIBERNATION_PLATFORM:
  641. if (hibernation_ops)
  642. hibernation_mode = mode;
  643. else
  644. error = -EINVAL;
  645. }
  646. } else
  647. error = -EINVAL;
  648. if (!error)
  649. pr_debug("PM: Hibernation mode set to '%s'\n",
  650. hibernation_modes[mode]);
  651. mutex_unlock(&pm_mutex);
  652. return error ? error : n;
  653. }
  654. power_attr(disk);
  655. static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
  656. char *buf)
  657. {
  658. return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
  659. MINOR(swsusp_resume_device));
  660. }
  661. static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
  662. const char *buf, size_t n)
  663. {
  664. unsigned int maj, min;
  665. dev_t res;
  666. int ret = -EINVAL;
  667. if (sscanf(buf, "%u:%u", &maj, &min) != 2)
  668. goto out;
  669. res = MKDEV(maj,min);
  670. if (maj != MAJOR(res) || min != MINOR(res))
  671. goto out;
  672. mutex_lock(&pm_mutex);
  673. swsusp_resume_device = res;
  674. mutex_unlock(&pm_mutex);
  675. printk(KERN_INFO "PM: Starting manual resume from disk\n");
  676. noresume = 0;
  677. software_resume();
  678. ret = n;
  679. out:
  680. return ret;
  681. }
  682. power_attr(resume);
  683. static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
  684. char *buf)
  685. {
  686. return sprintf(buf, "%lu\n", image_size);
  687. }
  688. static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
  689. const char *buf, size_t n)
  690. {
  691. unsigned long size;
  692. if (sscanf(buf, "%lu", &size) == 1) {
  693. image_size = size;
  694. return n;
  695. }
  696. return -EINVAL;
  697. }
  698. power_attr(image_size);
  699. static struct attribute * g[] = {
  700. &disk_attr.attr,
  701. &resume_attr.attr,
  702. &image_size_attr.attr,
  703. NULL,
  704. };
  705. static struct attribute_group attr_group = {
  706. .attrs = g,
  707. };
  708. static int __init pm_disk_init(void)
  709. {
  710. return sysfs_create_group(power_kobj, &attr_group);
  711. }
  712. core_initcall(pm_disk_init);
  713. static int __init resume_setup(char *str)
  714. {
  715. if (noresume)
  716. return 1;
  717. strncpy( resume_file, str, 255 );
  718. return 1;
  719. }
  720. static int __init resume_offset_setup(char *str)
  721. {
  722. unsigned long long offset;
  723. if (noresume)
  724. return 1;
  725. if (sscanf(str, "%llu", &offset) == 1)
  726. swsusp_resume_block = offset;
  727. return 1;
  728. }
  729. static int __init noresume_setup(char *str)
  730. {
  731. noresume = 1;
  732. return 1;
  733. }
  734. __setup("noresume", noresume_setup);
  735. __setup("resume_offset=", resume_offset_setup);
  736. __setup("resume=", resume_setup);