main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * kernel/power/main.c - PM subsystem core functionality.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. */
  10. #include <linux/export.h>
  11. #include <linux/kobject.h>
  12. #include <linux/string.h>
  13. #include <linux/pm-trace.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/seq_file.h>
  17. #include "power.h"
  18. DEFINE_MUTEX(pm_mutex);
  19. #ifdef CONFIG_PM_SLEEP
  20. void lock_system_sleep(void)
  21. {
  22. current->flags |= PF_FREEZER_SKIP;
  23. mutex_lock(&pm_mutex);
  24. }
  25. EXPORT_SYMBOL_GPL(lock_system_sleep);
  26. void unlock_system_sleep(void)
  27. {
  28. /*
  29. * Don't use freezer_count() because we don't want the call to
  30. * try_to_freeze() here.
  31. *
  32. * Reason:
  33. * Fundamentally, we just don't need it, because freezing condition
  34. * doesn't come into effect until we release the pm_mutex lock,
  35. * since the freezer always works with pm_mutex held.
  36. *
  37. * More importantly, in the case of hibernation,
  38. * unlock_system_sleep() gets called in snapshot_read() and
  39. * snapshot_write() when the freezing condition is still in effect.
  40. * Which means, if we use try_to_freeze() here, it would make them
  41. * enter the refrigerator, thus causing hibernation to lockup.
  42. */
  43. current->flags &= ~PF_FREEZER_SKIP;
  44. mutex_unlock(&pm_mutex);
  45. }
  46. EXPORT_SYMBOL_GPL(unlock_system_sleep);
  47. /* Routines for PM-transition notifications */
  48. static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
  49. int register_pm_notifier(struct notifier_block *nb)
  50. {
  51. return blocking_notifier_chain_register(&pm_chain_head, nb);
  52. }
  53. EXPORT_SYMBOL_GPL(register_pm_notifier);
  54. int unregister_pm_notifier(struct notifier_block *nb)
  55. {
  56. return blocking_notifier_chain_unregister(&pm_chain_head, nb);
  57. }
  58. EXPORT_SYMBOL_GPL(unregister_pm_notifier);
  59. int __pm_notifier_call_chain(unsigned long val, int nr_to_call, int *nr_calls)
  60. {
  61. int ret;
  62. ret = __blocking_notifier_call_chain(&pm_chain_head, val, NULL,
  63. nr_to_call, nr_calls);
  64. return notifier_to_errno(ret);
  65. }
  66. int pm_notifier_call_chain(unsigned long val)
  67. {
  68. return __pm_notifier_call_chain(val, -1, NULL);
  69. }
  70. /* If set, devices may be suspended and resumed asynchronously. */
  71. int pm_async_enabled = 1;
  72. static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
  73. char *buf)
  74. {
  75. return sprintf(buf, "%d\n", pm_async_enabled);
  76. }
  77. static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
  78. const char *buf, size_t n)
  79. {
  80. unsigned long val;
  81. if (kstrtoul(buf, 10, &val))
  82. return -EINVAL;
  83. if (val > 1)
  84. return -EINVAL;
  85. pm_async_enabled = val;
  86. return n;
  87. }
  88. power_attr(pm_async);
  89. #ifdef CONFIG_SUSPEND
  90. static ssize_t mem_sleep_show(struct kobject *kobj, struct kobj_attribute *attr,
  91. char *buf)
  92. {
  93. char *s = buf;
  94. suspend_state_t i;
  95. for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
  96. if (mem_sleep_states[i]) {
  97. const char *label = mem_sleep_states[i];
  98. if (mem_sleep_current == i)
  99. s += sprintf(s, "[%s] ", label);
  100. else
  101. s += sprintf(s, "%s ", label);
  102. }
  103. /* Convert the last space to a newline if needed. */
  104. if (s != buf)
  105. *(s-1) = '\n';
  106. return (s - buf);
  107. }
  108. static suspend_state_t decode_suspend_state(const char *buf, size_t n)
  109. {
  110. suspend_state_t state;
  111. char *p;
  112. int len;
  113. p = memchr(buf, '\n', n);
  114. len = p ? p - buf : n;
  115. for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
  116. const char *label = mem_sleep_states[state];
  117. if (label && len == strlen(label) && !strncmp(buf, label, len))
  118. return state;
  119. }
  120. return PM_SUSPEND_ON;
  121. }
  122. static ssize_t mem_sleep_store(struct kobject *kobj, struct kobj_attribute *attr,
  123. const char *buf, size_t n)
  124. {
  125. suspend_state_t state;
  126. int error;
  127. error = pm_autosleep_lock();
  128. if (error)
  129. return error;
  130. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  131. error = -EBUSY;
  132. goto out;
  133. }
  134. state = decode_suspend_state(buf, n);
  135. if (state < PM_SUSPEND_MAX && state > PM_SUSPEND_ON)
  136. mem_sleep_current = state;
  137. else
  138. error = -EINVAL;
  139. out:
  140. pm_autosleep_unlock();
  141. return error ? error : n;
  142. }
  143. power_attr(mem_sleep);
  144. #endif /* CONFIG_SUSPEND */
  145. #ifdef CONFIG_PM_SLEEP_DEBUG
  146. int pm_test_level = TEST_NONE;
  147. static const char * const pm_tests[__TEST_AFTER_LAST] = {
  148. [TEST_NONE] = "none",
  149. [TEST_CORE] = "core",
  150. [TEST_CPUS] = "processors",
  151. [TEST_PLATFORM] = "platform",
  152. [TEST_DEVICES] = "devices",
  153. [TEST_FREEZER] = "freezer",
  154. };
  155. static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
  156. char *buf)
  157. {
  158. char *s = buf;
  159. int level;
  160. for (level = TEST_FIRST; level <= TEST_MAX; level++)
  161. if (pm_tests[level]) {
  162. if (level == pm_test_level)
  163. s += sprintf(s, "[%s] ", pm_tests[level]);
  164. else
  165. s += sprintf(s, "%s ", pm_tests[level]);
  166. }
  167. if (s != buf)
  168. /* convert the last space to a newline */
  169. *(s-1) = '\n';
  170. return (s - buf);
  171. }
  172. static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
  173. const char *buf, size_t n)
  174. {
  175. const char * const *s;
  176. int level;
  177. char *p;
  178. int len;
  179. int error = -EINVAL;
  180. p = memchr(buf, '\n', n);
  181. len = p ? p - buf : n;
  182. lock_system_sleep();
  183. level = TEST_FIRST;
  184. for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
  185. if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
  186. pm_test_level = level;
  187. error = 0;
  188. break;
  189. }
  190. unlock_system_sleep();
  191. return error ? error : n;
  192. }
  193. power_attr(pm_test);
  194. #endif /* CONFIG_PM_SLEEP_DEBUG */
  195. #ifdef CONFIG_DEBUG_FS
  196. static char *suspend_step_name(enum suspend_stat_step step)
  197. {
  198. switch (step) {
  199. case SUSPEND_FREEZE:
  200. return "freeze";
  201. case SUSPEND_PREPARE:
  202. return "prepare";
  203. case SUSPEND_SUSPEND:
  204. return "suspend";
  205. case SUSPEND_SUSPEND_NOIRQ:
  206. return "suspend_noirq";
  207. case SUSPEND_RESUME_NOIRQ:
  208. return "resume_noirq";
  209. case SUSPEND_RESUME:
  210. return "resume";
  211. default:
  212. return "";
  213. }
  214. }
  215. static int suspend_stats_show(struct seq_file *s, void *unused)
  216. {
  217. int i, index, last_dev, last_errno, last_step;
  218. last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
  219. last_dev %= REC_FAILED_NUM;
  220. last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
  221. last_errno %= REC_FAILED_NUM;
  222. last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
  223. last_step %= REC_FAILED_NUM;
  224. seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n"
  225. "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n",
  226. "success", suspend_stats.success,
  227. "fail", suspend_stats.fail,
  228. "failed_freeze", suspend_stats.failed_freeze,
  229. "failed_prepare", suspend_stats.failed_prepare,
  230. "failed_suspend", suspend_stats.failed_suspend,
  231. "failed_suspend_late",
  232. suspend_stats.failed_suspend_late,
  233. "failed_suspend_noirq",
  234. suspend_stats.failed_suspend_noirq,
  235. "failed_resume", suspend_stats.failed_resume,
  236. "failed_resume_early",
  237. suspend_stats.failed_resume_early,
  238. "failed_resume_noirq",
  239. suspend_stats.failed_resume_noirq);
  240. seq_printf(s, "failures:\n last_failed_dev:\t%-s\n",
  241. suspend_stats.failed_devs[last_dev]);
  242. for (i = 1; i < REC_FAILED_NUM; i++) {
  243. index = last_dev + REC_FAILED_NUM - i;
  244. index %= REC_FAILED_NUM;
  245. seq_printf(s, "\t\t\t%-s\n",
  246. suspend_stats.failed_devs[index]);
  247. }
  248. seq_printf(s, " last_failed_errno:\t%-d\n",
  249. suspend_stats.errno[last_errno]);
  250. for (i = 1; i < REC_FAILED_NUM; i++) {
  251. index = last_errno + REC_FAILED_NUM - i;
  252. index %= REC_FAILED_NUM;
  253. seq_printf(s, "\t\t\t%-d\n",
  254. suspend_stats.errno[index]);
  255. }
  256. seq_printf(s, " last_failed_step:\t%-s\n",
  257. suspend_step_name(
  258. suspend_stats.failed_steps[last_step]));
  259. for (i = 1; i < REC_FAILED_NUM; i++) {
  260. index = last_step + REC_FAILED_NUM - i;
  261. index %= REC_FAILED_NUM;
  262. seq_printf(s, "\t\t\t%-s\n",
  263. suspend_step_name(
  264. suspend_stats.failed_steps[index]));
  265. }
  266. return 0;
  267. }
  268. static int suspend_stats_open(struct inode *inode, struct file *file)
  269. {
  270. return single_open(file, suspend_stats_show, NULL);
  271. }
  272. static const struct file_operations suspend_stats_operations = {
  273. .open = suspend_stats_open,
  274. .read = seq_read,
  275. .llseek = seq_lseek,
  276. .release = single_release,
  277. };
  278. static int __init pm_debugfs_init(void)
  279. {
  280. debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
  281. NULL, NULL, &suspend_stats_operations);
  282. return 0;
  283. }
  284. late_initcall(pm_debugfs_init);
  285. #endif /* CONFIG_DEBUG_FS */
  286. #endif /* CONFIG_PM_SLEEP */
  287. #ifdef CONFIG_PM_SLEEP_DEBUG
  288. /*
  289. * pm_print_times: print time taken by devices to suspend and resume.
  290. *
  291. * show() returns whether printing of suspend and resume times is enabled.
  292. * store() accepts 0 or 1. 0 disables printing and 1 enables it.
  293. */
  294. bool pm_print_times_enabled;
  295. static ssize_t pm_print_times_show(struct kobject *kobj,
  296. struct kobj_attribute *attr, char *buf)
  297. {
  298. return sprintf(buf, "%d\n", pm_print_times_enabled);
  299. }
  300. static ssize_t pm_print_times_store(struct kobject *kobj,
  301. struct kobj_attribute *attr,
  302. const char *buf, size_t n)
  303. {
  304. unsigned long val;
  305. if (kstrtoul(buf, 10, &val))
  306. return -EINVAL;
  307. if (val > 1)
  308. return -EINVAL;
  309. pm_print_times_enabled = !!val;
  310. return n;
  311. }
  312. power_attr(pm_print_times);
  313. static inline void pm_print_times_init(void)
  314. {
  315. pm_print_times_enabled = !!initcall_debug;
  316. }
  317. static ssize_t pm_wakeup_irq_show(struct kobject *kobj,
  318. struct kobj_attribute *attr,
  319. char *buf)
  320. {
  321. return pm_wakeup_irq ? sprintf(buf, "%u\n", pm_wakeup_irq) : -ENODATA;
  322. }
  323. power_attr_ro(pm_wakeup_irq);
  324. bool pm_debug_messages_on __read_mostly;
  325. static ssize_t pm_debug_messages_show(struct kobject *kobj,
  326. struct kobj_attribute *attr, char *buf)
  327. {
  328. return sprintf(buf, "%d\n", pm_debug_messages_on);
  329. }
  330. static ssize_t pm_debug_messages_store(struct kobject *kobj,
  331. struct kobj_attribute *attr,
  332. const char *buf, size_t n)
  333. {
  334. unsigned long val;
  335. if (kstrtoul(buf, 10, &val))
  336. return -EINVAL;
  337. if (val > 1)
  338. return -EINVAL;
  339. pm_debug_messages_on = !!val;
  340. return n;
  341. }
  342. power_attr(pm_debug_messages);
  343. /**
  344. * __pm_pr_dbg - Print a suspend debug message to the kernel log.
  345. * @defer: Whether or not to use printk_deferred() to print the message.
  346. * @fmt: Message format.
  347. *
  348. * The message will be emitted if enabled through the pm_debug_messages
  349. * sysfs attribute.
  350. */
  351. void __pm_pr_dbg(bool defer, const char *fmt, ...)
  352. {
  353. struct va_format vaf;
  354. va_list args;
  355. if (!pm_debug_messages_on)
  356. return;
  357. va_start(args, fmt);
  358. vaf.fmt = fmt;
  359. vaf.va = &args;
  360. if (defer)
  361. printk_deferred(KERN_DEBUG "PM: %pV", &vaf);
  362. else
  363. printk(KERN_DEBUG "PM: %pV", &vaf);
  364. va_end(args);
  365. }
  366. #else /* !CONFIG_PM_SLEEP_DEBUG */
  367. static inline void pm_print_times_init(void) {}
  368. #endif /* CONFIG_PM_SLEEP_DEBUG */
  369. struct kobject *power_kobj;
  370. /**
  371. * state - control system sleep states.
  372. *
  373. * show() returns available sleep state labels, which may be "mem", "standby",
  374. * "freeze" and "disk" (hibernation). See Documentation/power/states.txt for a
  375. * description of what they mean.
  376. *
  377. * store() accepts one of those strings, translates it into the proper
  378. * enumerated value, and initiates a suspend transition.
  379. */
  380. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
  381. char *buf)
  382. {
  383. char *s = buf;
  384. #ifdef CONFIG_SUSPEND
  385. suspend_state_t i;
  386. for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
  387. if (pm_states[i])
  388. s += sprintf(s,"%s ", pm_states[i]);
  389. #endif
  390. if (hibernation_available())
  391. s += sprintf(s, "disk ");
  392. if (s != buf)
  393. /* convert the last space to a newline */
  394. *(s-1) = '\n';
  395. return (s - buf);
  396. }
  397. static suspend_state_t decode_state(const char *buf, size_t n)
  398. {
  399. #ifdef CONFIG_SUSPEND
  400. suspend_state_t state;
  401. #endif
  402. char *p;
  403. int len;
  404. p = memchr(buf, '\n', n);
  405. len = p ? p - buf : n;
  406. /* Check hibernation first. */
  407. if (len == 4 && !strncmp(buf, "disk", len))
  408. return PM_SUSPEND_MAX;
  409. #ifdef CONFIG_SUSPEND
  410. for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
  411. const char *label = pm_states[state];
  412. if (label && len == strlen(label) && !strncmp(buf, label, len))
  413. return state;
  414. }
  415. #endif
  416. return PM_SUSPEND_ON;
  417. }
  418. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
  419. const char *buf, size_t n)
  420. {
  421. suspend_state_t state;
  422. int error;
  423. error = pm_autosleep_lock();
  424. if (error)
  425. return error;
  426. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  427. error = -EBUSY;
  428. goto out;
  429. }
  430. state = decode_state(buf, n);
  431. if (state < PM_SUSPEND_MAX) {
  432. if (state == PM_SUSPEND_MEM)
  433. state = mem_sleep_current;
  434. error = pm_suspend(state);
  435. } else if (state == PM_SUSPEND_MAX) {
  436. error = hibernate();
  437. } else {
  438. error = -EINVAL;
  439. }
  440. out:
  441. pm_autosleep_unlock();
  442. return error ? error : n;
  443. }
  444. power_attr(state);
  445. #ifdef CONFIG_PM_SLEEP
  446. /*
  447. * The 'wakeup_count' attribute, along with the functions defined in
  448. * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
  449. * handled in a non-racy way.
  450. *
  451. * If a wakeup event occurs when the system is in a sleep state, it simply is
  452. * woken up. In turn, if an event that would wake the system up from a sleep
  453. * state occurs when it is undergoing a transition to that sleep state, the
  454. * transition should be aborted. Moreover, if such an event occurs when the
  455. * system is in the working state, an attempt to start a transition to the
  456. * given sleep state should fail during certain period after the detection of
  457. * the event. Using the 'state' attribute alone is not sufficient to satisfy
  458. * these requirements, because a wakeup event may occur exactly when 'state'
  459. * is being written to and may be delivered to user space right before it is
  460. * frozen, so the event will remain only partially processed until the system is
  461. * woken up by another event. In particular, it won't cause the transition to
  462. * a sleep state to be aborted.
  463. *
  464. * This difficulty may be overcome if user space uses 'wakeup_count' before
  465. * writing to 'state'. It first should read from 'wakeup_count' and store
  466. * the read value. Then, after carrying out its own preparations for the system
  467. * transition to a sleep state, it should write the stored value to
  468. * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
  469. * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
  470. * is allowed to write to 'state', but the transition will be aborted if there
  471. * are any wakeup events detected after 'wakeup_count' was written to.
  472. */
  473. static ssize_t wakeup_count_show(struct kobject *kobj,
  474. struct kobj_attribute *attr,
  475. char *buf)
  476. {
  477. unsigned int val;
  478. return pm_get_wakeup_count(&val, true) ?
  479. sprintf(buf, "%u\n", val) : -EINTR;
  480. }
  481. static ssize_t wakeup_count_store(struct kobject *kobj,
  482. struct kobj_attribute *attr,
  483. const char *buf, size_t n)
  484. {
  485. unsigned int val;
  486. int error;
  487. error = pm_autosleep_lock();
  488. if (error)
  489. return error;
  490. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  491. error = -EBUSY;
  492. goto out;
  493. }
  494. error = -EINVAL;
  495. if (sscanf(buf, "%u", &val) == 1) {
  496. if (pm_save_wakeup_count(val))
  497. error = n;
  498. else
  499. pm_print_active_wakeup_sources();
  500. }
  501. out:
  502. pm_autosleep_unlock();
  503. return error;
  504. }
  505. power_attr(wakeup_count);
  506. #ifdef CONFIG_PM_AUTOSLEEP
  507. static ssize_t autosleep_show(struct kobject *kobj,
  508. struct kobj_attribute *attr,
  509. char *buf)
  510. {
  511. suspend_state_t state = pm_autosleep_state();
  512. if (state == PM_SUSPEND_ON)
  513. return sprintf(buf, "off\n");
  514. #ifdef CONFIG_SUSPEND
  515. if (state < PM_SUSPEND_MAX)
  516. return sprintf(buf, "%s\n", pm_states[state] ?
  517. pm_states[state] : "error");
  518. #endif
  519. #ifdef CONFIG_HIBERNATION
  520. return sprintf(buf, "disk\n");
  521. #else
  522. return sprintf(buf, "error");
  523. #endif
  524. }
  525. static ssize_t autosleep_store(struct kobject *kobj,
  526. struct kobj_attribute *attr,
  527. const char *buf, size_t n)
  528. {
  529. suspend_state_t state = decode_state(buf, n);
  530. int error;
  531. if (state == PM_SUSPEND_ON
  532. && strcmp(buf, "off") && strcmp(buf, "off\n"))
  533. return -EINVAL;
  534. if (state == PM_SUSPEND_MEM)
  535. state = mem_sleep_current;
  536. error = pm_autosleep_set_state(state);
  537. return error ? error : n;
  538. }
  539. power_attr(autosleep);
  540. #endif /* CONFIG_PM_AUTOSLEEP */
  541. #ifdef CONFIG_PM_WAKELOCKS
  542. static ssize_t wake_lock_show(struct kobject *kobj,
  543. struct kobj_attribute *attr,
  544. char *buf)
  545. {
  546. return pm_show_wakelocks(buf, true);
  547. }
  548. static ssize_t wake_lock_store(struct kobject *kobj,
  549. struct kobj_attribute *attr,
  550. const char *buf, size_t n)
  551. {
  552. int error = pm_wake_lock(buf);
  553. return error ? error : n;
  554. }
  555. power_attr(wake_lock);
  556. static ssize_t wake_unlock_show(struct kobject *kobj,
  557. struct kobj_attribute *attr,
  558. char *buf)
  559. {
  560. return pm_show_wakelocks(buf, false);
  561. }
  562. static ssize_t wake_unlock_store(struct kobject *kobj,
  563. struct kobj_attribute *attr,
  564. const char *buf, size_t n)
  565. {
  566. int error = pm_wake_unlock(buf);
  567. return error ? error : n;
  568. }
  569. power_attr(wake_unlock);
  570. #endif /* CONFIG_PM_WAKELOCKS */
  571. #endif /* CONFIG_PM_SLEEP */
  572. #ifdef CONFIG_PM_TRACE
  573. int pm_trace_enabled;
  574. static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
  575. char *buf)
  576. {
  577. return sprintf(buf, "%d\n", pm_trace_enabled);
  578. }
  579. static ssize_t
  580. pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
  581. const char *buf, size_t n)
  582. {
  583. int val;
  584. if (sscanf(buf, "%d", &val) == 1) {
  585. pm_trace_enabled = !!val;
  586. if (pm_trace_enabled) {
  587. pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
  588. "PM: Correct system time has to be restored manually after resume.\n");
  589. }
  590. return n;
  591. }
  592. return -EINVAL;
  593. }
  594. power_attr(pm_trace);
  595. static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
  596. struct kobj_attribute *attr,
  597. char *buf)
  598. {
  599. return show_trace_dev_match(buf, PAGE_SIZE);
  600. }
  601. power_attr_ro(pm_trace_dev_match);
  602. #endif /* CONFIG_PM_TRACE */
  603. #ifdef CONFIG_FREEZER
  604. static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
  605. struct kobj_attribute *attr, char *buf)
  606. {
  607. return sprintf(buf, "%u\n", freeze_timeout_msecs);
  608. }
  609. static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
  610. struct kobj_attribute *attr,
  611. const char *buf, size_t n)
  612. {
  613. unsigned long val;
  614. if (kstrtoul(buf, 10, &val))
  615. return -EINVAL;
  616. freeze_timeout_msecs = val;
  617. return n;
  618. }
  619. power_attr(pm_freeze_timeout);
  620. #endif /* CONFIG_FREEZER*/
  621. static struct attribute * g[] = {
  622. &state_attr.attr,
  623. #ifdef CONFIG_PM_TRACE
  624. &pm_trace_attr.attr,
  625. &pm_trace_dev_match_attr.attr,
  626. #endif
  627. #ifdef CONFIG_PM_SLEEP
  628. &pm_async_attr.attr,
  629. &wakeup_count_attr.attr,
  630. #ifdef CONFIG_SUSPEND
  631. &mem_sleep_attr.attr,
  632. #endif
  633. #ifdef CONFIG_PM_AUTOSLEEP
  634. &autosleep_attr.attr,
  635. #endif
  636. #ifdef CONFIG_PM_WAKELOCKS
  637. &wake_lock_attr.attr,
  638. &wake_unlock_attr.attr,
  639. #endif
  640. #ifdef CONFIG_PM_SLEEP_DEBUG
  641. &pm_test_attr.attr,
  642. &pm_print_times_attr.attr,
  643. &pm_wakeup_irq_attr.attr,
  644. &pm_debug_messages_attr.attr,
  645. #endif
  646. #endif
  647. #ifdef CONFIG_FREEZER
  648. &pm_freeze_timeout_attr.attr,
  649. #endif
  650. NULL,
  651. };
  652. static const struct attribute_group attr_group = {
  653. .attrs = g,
  654. };
  655. struct workqueue_struct *pm_wq;
  656. EXPORT_SYMBOL_GPL(pm_wq);
  657. static int __init pm_start_workqueue(void)
  658. {
  659. pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
  660. return pm_wq ? 0 : -ENOMEM;
  661. }
  662. static int __init pm_init(void)
  663. {
  664. int error = pm_start_workqueue();
  665. if (error)
  666. return error;
  667. hibernate_image_size_init();
  668. hibernate_reserved_size_init();
  669. pm_states_init();
  670. power_kobj = kobject_create_and_add("power", NULL);
  671. if (!power_kobj)
  672. return -ENOMEM;
  673. error = sysfs_create_group(power_kobj, &attr_group);
  674. if (error)
  675. return error;
  676. pm_print_times_init();
  677. return pm_autosleep_init();
  678. }
  679. core_initcall(pm_init);