Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * This file is released under the GPLv2 |
| 8 | * |
| 9 | */ |
| 10 | |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kobject.h> |
| 13 | #include <linux/string.h> |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 14 | #include <linux/resume-trace.h> |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 15 | #include <linux/workqueue.h> |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 16 | #include <linux/debugfs.h> |
| 17 | #include <linux/seq_file.h> |
| 18 | #include <linux/hrtimer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include "power.h" |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 21 | |
| 22 | #define MAX_BUF 100 |
Amar Singhal | ac2a6d6 | 2012-02-27 17:40:31 -0800 | [diff] [blame] | 23 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 24 | DEFINE_MUTEX(pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Rafael J. Wysocki | cd51e61 | 2011-02-11 00:04:52 +0100 | [diff] [blame] | 26 | #ifdef CONFIG_PM_SLEEP |
| 27 | |
Alan Stern | 8252575 | 2007-11-19 23:49:18 +0100 | [diff] [blame] | 28 | /* Routines for PM-transition notifications */ |
| 29 | |
| 30 | static BLOCKING_NOTIFIER_HEAD(pm_chain_head); |
| 31 | |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 32 | static void touch_event_fn(struct work_struct *work); |
| 33 | static DECLARE_WORK(touch_event_struct, touch_event_fn); |
| 34 | |
| 35 | static struct hrtimer tc_ev_timer; |
| 36 | static int tc_ev_processed; |
| 37 | static ktime_t touch_evt_timer_val; |
| 38 | |
Alan Stern | 8252575 | 2007-11-19 23:49:18 +0100 | [diff] [blame] | 39 | int register_pm_notifier(struct notifier_block *nb) |
| 40 | { |
| 41 | return blocking_notifier_chain_register(&pm_chain_head, nb); |
| 42 | } |
| 43 | EXPORT_SYMBOL_GPL(register_pm_notifier); |
| 44 | |
| 45 | int unregister_pm_notifier(struct notifier_block *nb) |
| 46 | { |
| 47 | return blocking_notifier_chain_unregister(&pm_chain_head, nb); |
| 48 | } |
| 49 | EXPORT_SYMBOL_GPL(unregister_pm_notifier); |
| 50 | |
| 51 | int pm_notifier_call_chain(unsigned long val) |
| 52 | { |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 53 | int ret = blocking_notifier_call_chain(&pm_chain_head, val, NULL); |
| 54 | |
| 55 | return notifier_to_errno(ret); |
Alan Stern | 8252575 | 2007-11-19 23:49:18 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 58 | /* If set, devices may be suspended and resumed asynchronously. */ |
| 59 | int pm_async_enabled = 1; |
| 60 | |
| 61 | static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 62 | char *buf) |
| 63 | { |
| 64 | return sprintf(buf, "%d\n", pm_async_enabled); |
| 65 | } |
| 66 | |
| 67 | static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 68 | const char *buf, size_t n) |
| 69 | { |
| 70 | unsigned long val; |
| 71 | |
| 72 | if (strict_strtoul(buf, 10, &val)) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | if (val > 1) |
| 76 | return -EINVAL; |
| 77 | |
| 78 | pm_async_enabled = val; |
| 79 | return n; |
| 80 | } |
| 81 | |
| 82 | power_attr(pm_async); |
| 83 | |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 84 | static ssize_t |
| 85 | touch_event_show(struct kobject *kobj, |
| 86 | struct kobj_attribute *attr, char *buf) |
| 87 | { |
| 88 | if (tc_ev_processed == 0) |
| 89 | return snprintf(buf, strnlen("touch_event", MAX_BUF) + 1, |
| 90 | "touch_event"); |
| 91 | else |
| 92 | return snprintf(buf, strnlen("null", MAX_BUF) + 1, |
| 93 | "null"); |
| 94 | } |
| 95 | |
| 96 | static ssize_t |
| 97 | touch_event_store(struct kobject *kobj, |
| 98 | struct kobj_attribute *attr, |
| 99 | const char *buf, size_t n) |
| 100 | { |
| 101 | |
| 102 | hrtimer_cancel(&tc_ev_timer); |
| 103 | tc_ev_processed = 0; |
| 104 | |
| 105 | /* set a timer to notify the userspace to stop processing |
| 106 | * touch event |
| 107 | */ |
| 108 | hrtimer_start(&tc_ev_timer, touch_evt_timer_val, HRTIMER_MODE_REL); |
| 109 | |
| 110 | /* wakeup the userspace poll */ |
| 111 | sysfs_notify(kobj, NULL, "touch_event"); |
| 112 | |
| 113 | return n; |
| 114 | } |
| 115 | |
| 116 | power_attr(touch_event); |
| 117 | |
| 118 | static ssize_t |
| 119 | touch_event_timer_show(struct kobject *kobj, |
| 120 | struct kobj_attribute *attr, char *buf) |
| 121 | { |
| 122 | return snprintf(buf, MAX_BUF, "%lld", touch_evt_timer_val.tv64); |
| 123 | } |
| 124 | |
| 125 | static ssize_t |
| 126 | touch_event_timer_store(struct kobject *kobj, |
| 127 | struct kobj_attribute *attr, |
| 128 | const char *buf, size_t n) |
| 129 | { |
| 130 | unsigned long val; |
| 131 | |
| 132 | if (strict_strtoul(buf, 10, &val)) |
| 133 | return -EINVAL; |
| 134 | |
| 135 | touch_evt_timer_val = ktime_set(0, val*1000); |
| 136 | |
| 137 | return n; |
| 138 | } |
| 139 | |
| 140 | power_attr(touch_event_timer); |
| 141 | |
| 142 | static void touch_event_fn(struct work_struct *work) |
| 143 | { |
| 144 | /* wakeup the userspace poll */ |
| 145 | tc_ev_processed = 1; |
| 146 | sysfs_notify(power_kobj, NULL, "touch_event"); |
| 147 | |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | static enum hrtimer_restart tc_ev_stop(struct hrtimer *hrtimer) |
| 152 | { |
| 153 | |
| 154 | schedule_work(&touch_event_struct); |
| 155 | |
| 156 | return HRTIMER_NORESTART; |
| 157 | } |
| 158 | |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 159 | #ifdef CONFIG_PM_DEBUG |
| 160 | int pm_test_level = TEST_NONE; |
| 161 | |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 162 | static const char * const pm_tests[__TEST_AFTER_LAST] = { |
| 163 | [TEST_NONE] = "none", |
| 164 | [TEST_CORE] = "core", |
| 165 | [TEST_CPUS] = "processors", |
| 166 | [TEST_PLATFORM] = "platform", |
| 167 | [TEST_DEVICES] = "devices", |
| 168 | [TEST_FREEZER] = "freezer", |
| 169 | }; |
| 170 | |
Rafael J. Wysocki | 039a75c | 2008-01-29 00:29:06 +0100 | [diff] [blame] | 171 | static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 172 | char *buf) |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 173 | { |
| 174 | char *s = buf; |
| 175 | int level; |
| 176 | |
| 177 | for (level = TEST_FIRST; level <= TEST_MAX; level++) |
| 178 | if (pm_tests[level]) { |
| 179 | if (level == pm_test_level) |
| 180 | s += sprintf(s, "[%s] ", pm_tests[level]); |
| 181 | else |
| 182 | s += sprintf(s, "%s ", pm_tests[level]); |
| 183 | } |
| 184 | |
| 185 | if (s != buf) |
| 186 | /* convert the last space to a newline */ |
| 187 | *(s-1) = '\n'; |
| 188 | |
| 189 | return (s - buf); |
| 190 | } |
| 191 | |
Rafael J. Wysocki | 039a75c | 2008-01-29 00:29:06 +0100 | [diff] [blame] | 192 | static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 193 | const char *buf, size_t n) |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 194 | { |
| 195 | const char * const *s; |
| 196 | int level; |
| 197 | char *p; |
| 198 | int len; |
| 199 | int error = -EINVAL; |
| 200 | |
| 201 | p = memchr(buf, '\n', n); |
| 202 | len = p ? p - buf : n; |
| 203 | |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 204 | lock_system_sleep(); |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 205 | |
| 206 | level = TEST_FIRST; |
| 207 | for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++) |
| 208 | if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) { |
| 209 | pm_test_level = level; |
| 210 | error = 0; |
| 211 | break; |
| 212 | } |
| 213 | |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 214 | unlock_system_sleep(); |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 215 | |
| 216 | return error ? error : n; |
| 217 | } |
| 218 | |
| 219 | power_attr(pm_test); |
Rafael J. Wysocki | 091d71e | 2009-01-17 00:10:45 +0100 | [diff] [blame] | 220 | #endif /* CONFIG_PM_DEBUG */ |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 221 | |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 222 | #ifdef CONFIG_DEBUG_FS |
| 223 | static char *suspend_step_name(enum suspend_stat_step step) |
| 224 | { |
| 225 | switch (step) { |
| 226 | case SUSPEND_FREEZE: |
| 227 | return "freeze"; |
| 228 | case SUSPEND_PREPARE: |
| 229 | return "prepare"; |
| 230 | case SUSPEND_SUSPEND: |
| 231 | return "suspend"; |
| 232 | case SUSPEND_SUSPEND_NOIRQ: |
| 233 | return "suspend_noirq"; |
| 234 | case SUSPEND_RESUME_NOIRQ: |
| 235 | return "resume_noirq"; |
| 236 | case SUSPEND_RESUME: |
| 237 | return "resume"; |
| 238 | default: |
| 239 | return ""; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static int suspend_stats_show(struct seq_file *s, void *unused) |
| 244 | { |
| 245 | int i, index, last_dev, last_errno, last_step; |
| 246 | |
| 247 | last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1; |
| 248 | last_dev %= REC_FAILED_NUM; |
| 249 | last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1; |
| 250 | last_errno %= REC_FAILED_NUM; |
| 251 | last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1; |
| 252 | last_step %= REC_FAILED_NUM; |
| 253 | seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n" |
| 254 | "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n", |
| 255 | "success", suspend_stats.success, |
| 256 | "fail", suspend_stats.fail, |
| 257 | "failed_freeze", suspend_stats.failed_freeze, |
| 258 | "failed_prepare", suspend_stats.failed_prepare, |
| 259 | "failed_suspend", suspend_stats.failed_suspend, |
| 260 | "failed_suspend_late", |
| 261 | suspend_stats.failed_suspend_late, |
| 262 | "failed_suspend_noirq", |
| 263 | suspend_stats.failed_suspend_noirq, |
| 264 | "failed_resume", suspend_stats.failed_resume, |
| 265 | "failed_resume_early", |
| 266 | suspend_stats.failed_resume_early, |
| 267 | "failed_resume_noirq", |
| 268 | suspend_stats.failed_resume_noirq); |
| 269 | seq_printf(s, "failures:\n last_failed_dev:\t%-s\n", |
| 270 | suspend_stats.failed_devs[last_dev]); |
| 271 | for (i = 1; i < REC_FAILED_NUM; i++) { |
| 272 | index = last_dev + REC_FAILED_NUM - i; |
| 273 | index %= REC_FAILED_NUM; |
| 274 | seq_printf(s, "\t\t\t%-s\n", |
| 275 | suspend_stats.failed_devs[index]); |
| 276 | } |
| 277 | seq_printf(s, " last_failed_errno:\t%-d\n", |
| 278 | suspend_stats.errno[last_errno]); |
| 279 | for (i = 1; i < REC_FAILED_NUM; i++) { |
| 280 | index = last_errno + REC_FAILED_NUM - i; |
| 281 | index %= REC_FAILED_NUM; |
| 282 | seq_printf(s, "\t\t\t%-d\n", |
| 283 | suspend_stats.errno[index]); |
| 284 | } |
| 285 | seq_printf(s, " last_failed_step:\t%-s\n", |
| 286 | suspend_step_name( |
| 287 | suspend_stats.failed_steps[last_step])); |
| 288 | for (i = 1; i < REC_FAILED_NUM; i++) { |
| 289 | index = last_step + REC_FAILED_NUM - i; |
| 290 | index %= REC_FAILED_NUM; |
| 291 | seq_printf(s, "\t\t\t%-s\n", |
| 292 | suspend_step_name( |
| 293 | suspend_stats.failed_steps[index])); |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int suspend_stats_open(struct inode *inode, struct file *file) |
| 300 | { |
| 301 | return single_open(file, suspend_stats_show, NULL); |
| 302 | } |
| 303 | |
| 304 | static const struct file_operations suspend_stats_operations = { |
| 305 | .open = suspend_stats_open, |
| 306 | .read = seq_read, |
| 307 | .llseek = seq_lseek, |
| 308 | .release = single_release, |
| 309 | }; |
| 310 | |
| 311 | static int __init pm_debugfs_init(void) |
| 312 | { |
| 313 | debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO, |
| 314 | NULL, NULL, &suspend_stats_operations); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | late_initcall(pm_debugfs_init); |
| 319 | #endif /* CONFIG_DEBUG_FS */ |
| 320 | |
Rafael J. Wysocki | ca12310 | 2011-08-11 22:38:12 +0200 | [diff] [blame] | 321 | #endif /* CONFIG_PM_SLEEP */ |
| 322 | |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 323 | struct kobject *power_kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
| 325 | /** |
| 326 | * state - control system power state. |
| 327 | * |
| 328 | * show() returns what states are supported, which is hard-coded to |
| 329 | * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and |
| 330 | * 'disk' (Suspend-to-Disk). |
| 331 | * |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 332 | * store() accepts one of those strings, translates it into the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | * proper enumerated value, and initiates a suspend transition. |
| 334 | */ |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 335 | static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 336 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 338 | char *s = buf; |
| 339 | #ifdef CONFIG_SUSPEND |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
| 342 | for (i = 0; i < PM_SUSPEND_MAX; i++) { |
Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 343 | if (pm_states[i] && valid_state(i)) |
| 344 | s += sprintf(s,"%s ", pm_states[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 346 | #endif |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 347 | #ifdef CONFIG_HIBERNATION |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 348 | s += sprintf(s, "%s\n", "disk"); |
| 349 | #else |
| 350 | if (s != buf) |
| 351 | /* convert the last space to a newline */ |
| 352 | *(s-1) = '\n'; |
| 353 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return (s - buf); |
| 355 | } |
| 356 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 357 | static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 358 | const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 360 | #ifdef CONFIG_SUSPEND |
Arve Hjønnevåg | b0dc343 | 2008-10-09 19:17:11 -0700 | [diff] [blame] | 361 | #ifdef CONFIG_EARLYSUSPEND |
| 362 | suspend_state_t state = PM_SUSPEND_ON; |
| 363 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | suspend_state_t state = PM_SUSPEND_STANDBY; |
Arve Hjønnevåg | b0dc343 | 2008-10-09 19:17:11 -0700 | [diff] [blame] | 365 | #endif |
Andreas Mohr | 3b364b8 | 2006-06-25 05:47:56 -0700 | [diff] [blame] | 366 | const char * const *s; |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 367 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | int len; |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 370 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | p = memchr(buf, '\n', n); |
| 373 | len = p ? p - buf : n; |
| 374 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 375 | /* First, check if we are requested to hibernate */ |
Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 376 | if (len == 4 && !strncmp(buf, "disk", len)) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 377 | error = hibernate(); |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 378 | goto Exit; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 381 | #ifdef CONFIG_SUSPEND |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 383 | if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) { |
shumash | 17b94be | 2016-01-10 16:44:51 -0700 | [diff] [blame] | 384 | #ifdef CONFIG_EARLYSUSPEND |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 385 | if (state == PM_SUSPEND_ON || valid_state(state)) { |
| 386 | error = 0; |
| 387 | request_suspend_state(state); |
| 388 | break; |
| 389 | } |
shumash | 17b94be | 2016-01-10 16:44:51 -0700 | [diff] [blame] | 390 | #else |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 391 | error = pm_suspend(state); |
shumash | 17b94be | 2016-01-10 16:44:51 -0700 | [diff] [blame] | 392 | #endif |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 393 | } |
| 394 | } |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 395 | #endif |
| 396 | |
| 397 | Exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | return error ? error : n; |
| 399 | } |
| 400 | |
| 401 | power_attr(state); |
| 402 | |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 403 | #ifdef CONFIG_PM_SLEEP |
| 404 | /* |
| 405 | * The 'wakeup_count' attribute, along with the functions defined in |
| 406 | * drivers/base/power/wakeup.c, provides a means by which wakeup events can be |
| 407 | * handled in a non-racy way. |
| 408 | * |
| 409 | * If a wakeup event occurs when the system is in a sleep state, it simply is |
| 410 | * woken up. In turn, if an event that would wake the system up from a sleep |
| 411 | * state occurs when it is undergoing a transition to that sleep state, the |
| 412 | * transition should be aborted. Moreover, if such an event occurs when the |
| 413 | * system is in the working state, an attempt to start a transition to the |
| 414 | * given sleep state should fail during certain period after the detection of |
| 415 | * the event. Using the 'state' attribute alone is not sufficient to satisfy |
| 416 | * these requirements, because a wakeup event may occur exactly when 'state' |
| 417 | * is being written to and may be delivered to user space right before it is |
| 418 | * frozen, so the event will remain only partially processed until the system is |
| 419 | * woken up by another event. In particular, it won't cause the transition to |
| 420 | * a sleep state to be aborted. |
| 421 | * |
| 422 | * This difficulty may be overcome if user space uses 'wakeup_count' before |
| 423 | * writing to 'state'. It first should read from 'wakeup_count' and store |
| 424 | * the read value. Then, after carrying out its own preparations for the system |
| 425 | * transition to a sleep state, it should write the stored value to |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 426 | * 'wakeup_count'. If that fails, at least one wakeup event has occurred since |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 427 | * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it |
| 428 | * is allowed to write to 'state', but the transition will be aborted if there |
| 429 | * are any wakeup events detected after 'wakeup_count' was written to. |
| 430 | */ |
| 431 | |
| 432 | static ssize_t wakeup_count_show(struct kobject *kobj, |
| 433 | struct kobj_attribute *attr, |
| 434 | char *buf) |
| 435 | { |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 436 | unsigned int val; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 437 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 438 | return pm_get_wakeup_count(&val) ? sprintf(buf, "%u\n", val) : -EINTR; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static ssize_t wakeup_count_store(struct kobject *kobj, |
| 442 | struct kobj_attribute *attr, |
| 443 | const char *buf, size_t n) |
| 444 | { |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 445 | unsigned int val; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 446 | |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 447 | if (sscanf(buf, "%u", &val) == 1) { |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 448 | if (pm_save_wakeup_count(val)) |
| 449 | return n; |
| 450 | } |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
| 454 | power_attr(wakeup_count); |
| 455 | #endif /* CONFIG_PM_SLEEP */ |
| 456 | |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 457 | #ifdef CONFIG_PM_TRACE |
| 458 | int pm_trace_enabled; |
| 459 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 460 | static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 461 | char *buf) |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 462 | { |
| 463 | return sprintf(buf, "%d\n", pm_trace_enabled); |
| 464 | } |
| 465 | |
| 466 | static ssize_t |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 467 | pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 468 | const char *buf, size_t n) |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 469 | { |
| 470 | int val; |
| 471 | |
| 472 | if (sscanf(buf, "%d", &val) == 1) { |
| 473 | pm_trace_enabled = !!val; |
| 474 | return n; |
| 475 | } |
| 476 | return -EINVAL; |
| 477 | } |
| 478 | |
| 479 | power_attr(pm_trace); |
James Hogan | d33ac60 | 2010-10-12 00:00:25 +0200 | [diff] [blame] | 480 | |
| 481 | static ssize_t pm_trace_dev_match_show(struct kobject *kobj, |
| 482 | struct kobj_attribute *attr, |
| 483 | char *buf) |
| 484 | { |
| 485 | return show_trace_dev_match(buf, PAGE_SIZE); |
| 486 | } |
| 487 | |
| 488 | static ssize_t |
| 489 | pm_trace_dev_match_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 490 | const char *buf, size_t n) |
| 491 | { |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | |
| 495 | power_attr(pm_trace_dev_match); |
| 496 | |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 497 | #endif /* CONFIG_PM_TRACE */ |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 498 | |
Arve Hjønnevåg | 025ff0a | 2008-10-09 21:01:46 -0700 | [diff] [blame] | 499 | #ifdef CONFIG_USER_WAKELOCK |
| 500 | power_attr(wake_lock); |
| 501 | power_attr(wake_unlock); |
| 502 | #endif |
| 503 | |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 504 | static struct attribute *g[] = { |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 505 | &state_attr.attr, |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 506 | #ifdef CONFIG_PM_TRACE |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 507 | &pm_trace_attr.attr, |
James Hogan | d33ac60 | 2010-10-12 00:00:25 +0200 | [diff] [blame] | 508 | &pm_trace_dev_match_attr.attr, |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 509 | #endif |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 510 | #ifdef CONFIG_PM_SLEEP |
| 511 | &pm_async_attr.attr, |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 512 | &wakeup_count_attr.attr, |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 513 | &touch_event_attr.attr, |
| 514 | &touch_event_timer_attr.attr, |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 515 | #ifdef CONFIG_PM_DEBUG |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 516 | &pm_test_attr.attr, |
| 517 | #endif |
Arve Hjønnevåg | 025ff0a | 2008-10-09 21:01:46 -0700 | [diff] [blame] | 518 | #ifdef CONFIG_USER_WAKELOCK |
| 519 | &wake_lock_attr.attr, |
| 520 | &wake_unlock_attr.attr, |
| 521 | #endif |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 522 | #endif |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 523 | NULL, |
| 524 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
| 526 | static struct attribute_group attr_group = { |
| 527 | .attrs = g, |
| 528 | }; |
| 529 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 530 | #ifdef CONFIG_PM_RUNTIME |
| 531 | struct workqueue_struct *pm_wq; |
Alan Stern | 7b199ca | 2009-12-03 20:22:21 +0100 | [diff] [blame] | 532 | EXPORT_SYMBOL_GPL(pm_wq); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 533 | |
| 534 | static int __init pm_start_workqueue(void) |
| 535 | { |
Tejun Heo | 58a69cb | 2011-02-16 09:25:31 +0100 | [diff] [blame] | 536 | pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 537 | |
| 538 | return pm_wq ? 0 : -ENOMEM; |
| 539 | } |
| 540 | #else |
| 541 | static inline int pm_start_workqueue(void) { return 0; } |
| 542 | #endif |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | static int __init pm_init(void) |
| 545 | { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 546 | int error = pm_start_workqueue(); |
| 547 | if (error) |
| 548 | return error; |
Rafael J. Wysocki | ac5c24ec | 2010-09-20 19:44:56 +0200 | [diff] [blame] | 549 | hibernate_image_size_init(); |
Rafael J. Wysocki | ddeb648 | 2011-05-15 11:38:48 +0200 | [diff] [blame] | 550 | hibernate_reserved_size_init(); |
Nick Reuter | b8678b3 | 2018-08-30 02:53:55 -0500 | [diff] [blame^] | 551 | |
| 552 | touch_evt_timer_val = ktime_set(2, 0); |
| 553 | hrtimer_init(&tc_ev_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 554 | tc_ev_timer.function = &tc_ev_stop; |
| 555 | tc_ev_processed = 1; |
| 556 | |
| 557 | |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 558 | power_kobj = kobject_create_and_add("power", NULL); |
| 559 | if (!power_kobj) |
Greg Kroah-Hartman | 039a5dc | 2007-11-01 10:39:50 -0700 | [diff] [blame] | 560 | return -ENOMEM; |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 561 | return sysfs_create_group(power_kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | core_initcall(pm_init); |