main.c 19 KB

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