disk.c 20 KB

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