disk.c 19 KB

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