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 |
| 6 | * |
| 7 | * This file is released under the GPLv2 |
| 8 | * |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kobject.h> |
| 12 | #include <linux/string.h> |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 13 | #include <linux/resume-trace.h> |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 14 | #include <linux/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include "power.h" |
| 17 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 18 | DEFINE_MUTEX(pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Len Brown | 9f9adec | 2007-12-13 17:38:03 -0500 | [diff] [blame] | 20 | unsigned int pm_flags; |
| 21 | EXPORT_SYMBOL(pm_flags); |
| 22 | |
Alan Stern | 8252575 | 2007-11-19 23:49:18 +0100 | [diff] [blame] | 23 | #ifdef CONFIG_PM_SLEEP |
| 24 | |
| 25 | /* Routines for PM-transition notifications */ |
| 26 | |
| 27 | static BLOCKING_NOTIFIER_HEAD(pm_chain_head); |
| 28 | |
| 29 | int register_pm_notifier(struct notifier_block *nb) |
| 30 | { |
| 31 | return blocking_notifier_chain_register(&pm_chain_head, nb); |
| 32 | } |
| 33 | EXPORT_SYMBOL_GPL(register_pm_notifier); |
| 34 | |
| 35 | int unregister_pm_notifier(struct notifier_block *nb) |
| 36 | { |
| 37 | return blocking_notifier_chain_unregister(&pm_chain_head, nb); |
| 38 | } |
| 39 | EXPORT_SYMBOL_GPL(unregister_pm_notifier); |
| 40 | |
| 41 | int pm_notifier_call_chain(unsigned long val) |
| 42 | { |
| 43 | return (blocking_notifier_call_chain(&pm_chain_head, val, NULL) |
| 44 | == NOTIFY_BAD) ? -EINVAL : 0; |
| 45 | } |
| 46 | |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 47 | #ifdef CONFIG_PM_DEBUG |
| 48 | int pm_test_level = TEST_NONE; |
| 49 | |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 50 | static const char * const pm_tests[__TEST_AFTER_LAST] = { |
| 51 | [TEST_NONE] = "none", |
| 52 | [TEST_CORE] = "core", |
| 53 | [TEST_CPUS] = "processors", |
| 54 | [TEST_PLATFORM] = "platform", |
| 55 | [TEST_DEVICES] = "devices", |
| 56 | [TEST_FREEZER] = "freezer", |
| 57 | }; |
| 58 | |
Rafael J. Wysocki | 039a75c | 2008-01-29 00:29:06 +0100 | [diff] [blame] | 59 | static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 60 | char *buf) |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 61 | { |
| 62 | char *s = buf; |
| 63 | int level; |
| 64 | |
| 65 | for (level = TEST_FIRST; level <= TEST_MAX; level++) |
| 66 | if (pm_tests[level]) { |
| 67 | if (level == pm_test_level) |
| 68 | s += sprintf(s, "[%s] ", pm_tests[level]); |
| 69 | else |
| 70 | s += sprintf(s, "%s ", pm_tests[level]); |
| 71 | } |
| 72 | |
| 73 | if (s != buf) |
| 74 | /* convert the last space to a newline */ |
| 75 | *(s-1) = '\n'; |
| 76 | |
| 77 | return (s - buf); |
| 78 | } |
| 79 | |
Rafael J. Wysocki | 039a75c | 2008-01-29 00:29:06 +0100 | [diff] [blame] | 80 | static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 81 | const char *buf, size_t n) |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 82 | { |
| 83 | const char * const *s; |
| 84 | int level; |
| 85 | char *p; |
| 86 | int len; |
| 87 | int error = -EINVAL; |
| 88 | |
| 89 | p = memchr(buf, '\n', n); |
| 90 | len = p ? p - buf : n; |
| 91 | |
| 92 | mutex_lock(&pm_mutex); |
| 93 | |
| 94 | level = TEST_FIRST; |
| 95 | for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++) |
| 96 | if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) { |
| 97 | pm_test_level = level; |
| 98 | error = 0; |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | mutex_unlock(&pm_mutex); |
| 103 | |
| 104 | return error ? error : n; |
| 105 | } |
| 106 | |
| 107 | power_attr(pm_test); |
Rafael J. Wysocki | 091d71e | 2009-01-17 00:10:45 +0100 | [diff] [blame] | 108 | #endif /* CONFIG_PM_DEBUG */ |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 109 | |
Rafael J. Wysocki | 7671b8a | 2007-12-14 01:07:13 +0100 | [diff] [blame] | 110 | #endif /* CONFIG_PM_SLEEP */ |
Rafael J. Wysocki | 039a75c | 2008-01-29 00:29:06 +0100 | [diff] [blame] | 111 | |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 112 | struct kobject *power_kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * state - control system power state. |
| 116 | * |
| 117 | * show() returns what states are supported, which is hard-coded to |
| 118 | * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and |
| 119 | * 'disk' (Suspend-to-Disk). |
| 120 | * |
| 121 | * store() accepts one of those strings, translates it into the |
| 122 | * proper enumerated value, and initiates a suspend transition. |
| 123 | */ |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 124 | static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 125 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 127 | char *s = buf; |
| 128 | #ifdef CONFIG_SUSPEND |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | for (i = 0; i < PM_SUSPEND_MAX; i++) { |
Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 132 | if (pm_states[i] && valid_state(i)) |
| 133 | s += sprintf(s,"%s ", pm_states[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 135 | #endif |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 136 | #ifdef CONFIG_HIBERNATION |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 137 | s += sprintf(s, "%s\n", "disk"); |
| 138 | #else |
| 139 | if (s != buf) |
| 140 | /* convert the last space to a newline */ |
| 141 | *(s-1) = '\n'; |
| 142 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return (s - buf); |
| 144 | } |
| 145 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 146 | static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 147 | const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 149 | #ifdef CONFIG_SUSPEND |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | suspend_state_t state = PM_SUSPEND_STANDBY; |
Andreas Mohr | 3b364b8 | 2006-06-25 05:47:56 -0700 | [diff] [blame] | 151 | const char * const *s; |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 152 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | int len; |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 155 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | p = memchr(buf, '\n', n); |
| 158 | len = p ? p - buf : n; |
| 159 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 160 | /* First, check if we are requested to hibernate */ |
Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 161 | if (len == 4 && !strncmp(buf, "disk", len)) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 162 | error = hibernate(); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 163 | goto Exit; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 166 | #ifdef CONFIG_SUSPEND |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { |
Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 168 | if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | break; |
| 170 | } |
dean gaudet | 47bb789 | 2006-04-27 18:39:17 -0700 | [diff] [blame] | 171 | if (state < PM_SUSPEND_MAX && *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | error = enter_state(state); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 173 | #endif |
| 174 | |
| 175 | Exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return error ? error : n; |
| 177 | } |
| 178 | |
| 179 | power_attr(state); |
| 180 | |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 181 | #ifdef CONFIG_PM_TRACE |
| 182 | int pm_trace_enabled; |
| 183 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 184 | static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 185 | char *buf) |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 186 | { |
| 187 | return sprintf(buf, "%d\n", pm_trace_enabled); |
| 188 | } |
| 189 | |
| 190 | static ssize_t |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 191 | pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 192 | const char *buf, size_t n) |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 193 | { |
| 194 | int val; |
| 195 | |
| 196 | if (sscanf(buf, "%d", &val) == 1) { |
| 197 | pm_trace_enabled = !!val; |
| 198 | return n; |
| 199 | } |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | |
| 203 | power_attr(pm_trace); |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 204 | #endif /* CONFIG_PM_TRACE */ |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 205 | |
| 206 | static struct attribute * g[] = { |
| 207 | &state_attr.attr, |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 208 | #ifdef CONFIG_PM_TRACE |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 209 | &pm_trace_attr.attr, |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 210 | #endif |
Rafael J. Wysocki | 7671b8a | 2007-12-14 01:07:13 +0100 | [diff] [blame] | 211 | #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG) |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 212 | &pm_test_attr.attr, |
| 213 | #endif |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 214 | NULL, |
| 215 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | static struct attribute_group attr_group = { |
| 218 | .attrs = g, |
| 219 | }; |
| 220 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 221 | #ifdef CONFIG_PM_RUNTIME |
| 222 | struct workqueue_struct *pm_wq; |
Alan Stern | 7b199ca | 2009-12-03 20:22:21 +0100 | [diff] [blame] | 223 | EXPORT_SYMBOL_GPL(pm_wq); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 224 | |
| 225 | static int __init pm_start_workqueue(void) |
| 226 | { |
| 227 | pm_wq = create_freezeable_workqueue("pm"); |
| 228 | |
| 229 | return pm_wq ? 0 : -ENOMEM; |
| 230 | } |
| 231 | #else |
| 232 | static inline int pm_start_workqueue(void) { return 0; } |
| 233 | #endif |
| 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | static int __init pm_init(void) |
| 236 | { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 237 | int error = pm_start_workqueue(); |
| 238 | if (error) |
| 239 | return error; |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 240 | power_kobj = kobject_create_and_add("power", NULL); |
| 241 | if (!power_kobj) |
Greg Kroah-Hartman | 039a5dc | 2007-11-01 10:39:50 -0700 | [diff] [blame] | 242 | return -ENOMEM; |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 243 | return sysfs_create_group(power_kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | core_initcall(pm_init); |