main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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).
  375. * See Documentation/admin-guide/pm/sleep-states.rst for a description of
  376. * what they mean.
  377. *
  378. * store() accepts one of those strings, translates it into the proper
  379. * enumerated value, and initiates a suspend transition.
  380. */
  381. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
  382. char *buf)
  383. {
  384. char *s = buf;
  385. #ifdef CONFIG_SUSPEND
  386. suspend_state_t i;
  387. for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
  388. if (pm_states[i])
  389. s += sprintf(s,"%s ", pm_states[i]);
  390. #endif
  391. if (hibernation_available())
  392. s += sprintf(s, "disk ");
  393. if (s != buf)
  394. /* convert the last space to a newline */
  395. *(s-1) = '\n';
  396. return (s - buf);
  397. }
  398. static suspend_state_t decode_state(const char *buf, size_t n)
  399. {
  400. #ifdef CONFIG_SUSPEND
  401. suspend_state_t state;
  402. #endif
  403. char *p;
  404. int len;
  405. p = memchr(buf, '\n', n);
  406. len = p ? p - buf : n;
  407. /* Check hibernation first. */
  408. if (len == 4 && !strncmp(buf, "disk", len))
  409. return PM_SUSPEND_MAX;
  410. #ifdef CONFIG_SUSPEND
  411. for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
  412. const char *label = pm_states[state];
  413. if (label && len == strlen(label) && !strncmp(buf, label, len))
  414. return state;
  415. }
  416. #endif
  417. return PM_SUSPEND_ON;
  418. }
  419. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
  420. const char *buf, size_t n)
  421. {
  422. suspend_state_t state;
  423. int error;
  424. error = pm_autosleep_lock();
  425. if (error)
  426. return error;
  427. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  428. error = -EBUSY;
  429. goto out;
  430. }
  431. state = decode_state(buf, n);
  432. if (state < PM_SUSPEND_MAX) {
  433. if (state == PM_SUSPEND_MEM)
  434. state = mem_sleep_current;
  435. error = pm_suspend(state);
  436. } else if (state == PM_SUSPEND_MAX) {
  437. error = hibernate();
  438. } else {
  439. error = -EINVAL;
  440. }
  441. out:
  442. pm_autosleep_unlock();
  443. return error ? error : n;
  444. }
  445. power_attr(state);
  446. #ifdef CONFIG_PM_SLEEP
  447. /*
  448. * The 'wakeup_count' attribute, along with the functions defined in
  449. * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
  450. * handled in a non-racy way.
  451. *
  452. * If a wakeup event occurs when the system is in a sleep state, it simply is
  453. * woken up. In turn, if an event that would wake the system up from a sleep
  454. * state occurs when it is undergoing a transition to that sleep state, the
  455. * transition should be aborted. Moreover, if such an event occurs when the
  456. * system is in the working state, an attempt to start a transition to the
  457. * given sleep state should fail during certain period after the detection of
  458. * the event. Using the 'state' attribute alone is not sufficient to satisfy
  459. * these requirements, because a wakeup event may occur exactly when 'state'
  460. * is being written to and may be delivered to user space right before it is
  461. * frozen, so the event will remain only partially processed until the system is
  462. * woken up by another event. In particular, it won't cause the transition to
  463. * a sleep state to be aborted.
  464. *
  465. * This difficulty may be overcome if user space uses 'wakeup_count' before
  466. * writing to 'state'. It first should read from 'wakeup_count' and store
  467. * the read value. Then, after carrying out its own preparations for the system
  468. * transition to a sleep state, it should write the stored value to
  469. * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
  470. * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
  471. * is allowed to write to 'state', but the transition will be aborted if there
  472. * are any wakeup events detected after 'wakeup_count' was written to.
  473. */
  474. static ssize_t wakeup_count_show(struct kobject *kobj,
  475. struct kobj_attribute *attr,
  476. char *buf)
  477. {
  478. unsigned int val;
  479. return pm_get_wakeup_count(&val, true) ?
  480. sprintf(buf, "%u\n", val) : -EINTR;
  481. }
  482. static ssize_t wakeup_count_store(struct kobject *kobj,
  483. struct kobj_attribute *attr,
  484. const char *buf, size_t n)
  485. {
  486. unsigned int val;
  487. int error;
  488. error = pm_autosleep_lock();
  489. if (error)
  490. return error;
  491. if (pm_autosleep_state() > PM_SUSPEND_ON) {
  492. error = -EBUSY;
  493. goto out;
  494. }
  495. error = -EINVAL;
  496. if (sscanf(buf, "%u", &val) == 1) {
  497. if (pm_save_wakeup_count(val))
  498. error = n;
  499. else
  500. pm_print_active_wakeup_sources();
  501. }
  502. out:
  503. pm_autosleep_unlock();
  504. return error;
  505. }
  506. power_attr(wakeup_count);
  507. #ifdef CONFIG_PM_AUTOSLEEP
  508. static ssize_t autosleep_show(struct kobject *kobj,
  509. struct kobj_attribute *attr,
  510. char *buf)
  511. {
  512. suspend_state_t state = pm_autosleep_state();
  513. if (state == PM_SUSPEND_ON)
  514. return sprintf(buf, "off\n");
  515. #ifdef CONFIG_SUSPEND
  516. if (state < PM_SUSPEND_MAX)
  517. return sprintf(buf, "%s\n", pm_states[state] ?
  518. pm_states[state] : "error");
  519. #endif
  520. #ifdef CONFIG_HIBERNATION
  521. return sprintf(buf, "disk\n");
  522. #else
  523. return sprintf(buf, "error");
  524. #endif
  525. }
  526. static ssize_t autosleep_store(struct kobject *kobj,
  527. struct kobj_attribute *attr,
  528. const char *buf, size_t n)
  529. {
  530. suspend_state_t state = decode_state(buf, n);
  531. int error;
  532. if (state == PM_SUSPEND_ON
  533. && strcmp(buf, "off") && strcmp(buf, "off\n"))
  534. return -EINVAL;
  535. if (state == PM_SUSPEND_MEM)
  536. state = mem_sleep_current;
  537. error = pm_autosleep_set_state(state);
  538. return error ? error : n;
  539. }
  540. power_attr(autosleep);
  541. #endif /* CONFIG_PM_AUTOSLEEP */
  542. #ifdef CONFIG_PM_WAKELOCKS
  543. static ssize_t wake_lock_show(struct kobject *kobj,
  544. struct kobj_attribute *attr,
  545. char *buf)
  546. {
  547. return pm_show_wakelocks(buf, true);
  548. }
  549. static ssize_t wake_lock_store(struct kobject *kobj,
  550. struct kobj_attribute *attr,
  551. const char *buf, size_t n)
  552. {
  553. int error = pm_wake_lock(buf);
  554. return error ? error : n;
  555. }
  556. power_attr(wake_lock);
  557. static ssize_t wake_unlock_show(struct kobject *kobj,
  558. struct kobj_attribute *attr,
  559. char *buf)
  560. {
  561. return pm_show_wakelocks(buf, false);
  562. }
  563. static ssize_t wake_unlock_store(struct kobject *kobj,
  564. struct kobj_attribute *attr,
  565. const char *buf, size_t n)
  566. {
  567. int error = pm_wake_unlock(buf);
  568. return error ? error : n;
  569. }
  570. power_attr(wake_unlock);
  571. #endif /* CONFIG_PM_WAKELOCKS */
  572. #endif /* CONFIG_PM_SLEEP */
  573. #ifdef CONFIG_PM_TRACE
  574. int pm_trace_enabled;
  575. static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
  576. char *buf)
  577. {
  578. return sprintf(buf, "%d\n", pm_trace_enabled);
  579. }
  580. static ssize_t
  581. pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
  582. const char *buf, size_t n)
  583. {
  584. int val;
  585. if (sscanf(buf, "%d", &val) == 1) {
  586. pm_trace_enabled = !!val;
  587. if (pm_trace_enabled) {
  588. pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
  589. "PM: Correct system time has to be restored manually after resume.\n");
  590. }
  591. return n;
  592. }
  593. return -EINVAL;
  594. }
  595. power_attr(pm_trace);
  596. static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
  597. struct kobj_attribute *attr,
  598. char *buf)
  599. {
  600. return show_trace_dev_match(buf, PAGE_SIZE);
  601. }
  602. power_attr_ro(pm_trace_dev_match);
  603. #endif /* CONFIG_PM_TRACE */
  604. #ifdef CONFIG_FREEZER
  605. static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
  606. struct kobj_attribute *attr, char *buf)
  607. {
  608. return sprintf(buf, "%u\n", freeze_timeout_msecs);
  609. }
  610. static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
  611. struct kobj_attribute *attr,
  612. const char *buf, size_t n)
  613. {
  614. unsigned long val;
  615. if (kstrtoul(buf, 10, &val))
  616. return -EINVAL;
  617. freeze_timeout_msecs = val;
  618. return n;
  619. }
  620. power_attr(pm_freeze_timeout);
  621. #endif /* CONFIG_FREEZER*/
  622. static struct attribute * g[] = {
  623. &state_attr.attr,
  624. #ifdef CONFIG_PM_TRACE
  625. &pm_trace_attr.attr,
  626. &pm_trace_dev_match_attr.attr,
  627. #endif
  628. #ifdef CONFIG_PM_SLEEP
  629. &pm_async_attr.attr,
  630. &wakeup_count_attr.attr,
  631. #ifdef CONFIG_SUSPEND
  632. &mem_sleep_attr.attr,
  633. #endif
  634. #ifdef CONFIG_PM_AUTOSLEEP
  635. &autosleep_attr.attr,
  636. #endif
  637. #ifdef CONFIG_PM_WAKELOCKS
  638. &wake_lock_attr.attr,
  639. &wake_unlock_attr.attr,
  640. #endif
  641. #ifdef CONFIG_PM_SLEEP_DEBUG
  642. &pm_test_attr.attr,
  643. &pm_print_times_attr.attr,
  644. &pm_wakeup_irq_attr.attr,
  645. &pm_debug_messages_attr.attr,
  646. #endif
  647. #endif
  648. #ifdef CONFIG_FREEZER
  649. &pm_freeze_timeout_attr.attr,
  650. #endif
  651. NULL,
  652. };
  653. static const struct attribute_group attr_group = {
  654. .attrs = g,
  655. };
  656. struct workqueue_struct *pm_wq;
  657. EXPORT_SYMBOL_GPL(pm_wq);
  658. static int __init pm_start_workqueue(void)
  659. {
  660. pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
  661. return pm_wq ? 0 : -ENOMEM;
  662. }
  663. static int __init pm_init(void)
  664. {
  665. int error = pm_start_workqueue();
  666. if (error)
  667. return error;
  668. hibernate_image_size_init();
  669. hibernate_reserved_size_init();
  670. pm_states_init();
  671. power_kobj = kobject_create_and_add("power", NULL);
  672. if (!power_kobj)
  673. return -ENOMEM;
  674. error = sysfs_create_group(power_kobj, &attr_group);
  675. if (error)
  676. return error;
  677. pm_print_times_init();
  678. return pm_autosleep_init();
  679. }
  680. core_initcall(pm_init);