main.c 18 KB

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