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 | |
Ralf Baechle | 4cf3034 | 2006-12-06 20:36:06 -0800 | [diff] [blame] | 11 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/suspend.h> |
| 13 | #include <linux/kobject.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/init.h> |
Andrew Morton | 6cc0719 | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 18 | #include <linux/console.h> |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 19 | #include <linux/cpu.h> |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 20 | #include <linux/resume-trace.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 21 | #include <linux/freezer.h> |
Christoph Lameter | 9617729 | 2007-02-10 01:43:03 -0800 | [diff] [blame] | 22 | #include <linux/vmstat.h> |
Rafael J. Wysocki | 232b143 | 2007-10-18 03:04:44 -0700 | [diff] [blame] | 23 | #include <linux/syscalls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include "power.h" |
| 26 | |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 27 | BLOCKING_NOTIFIER_HEAD(pm_chain_head); |
| 28 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 29 | DEFINE_MUTEX(pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Len Brown | 9f9adec | 2007-12-13 17:38:03 -0500 | [diff] [blame] | 31 | unsigned int pm_flags; |
| 32 | EXPORT_SYMBOL(pm_flags); |
| 33 | |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 34 | #ifdef CONFIG_PM_DEBUG |
| 35 | int pm_test_level = TEST_NONE; |
| 36 | |
| 37 | static int suspend_test(int level) |
| 38 | { |
| 39 | if (pm_test_level == level) { |
| 40 | printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n"); |
| 41 | mdelay(5000); |
| 42 | return 1; |
| 43 | } |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static const char * const pm_tests[__TEST_AFTER_LAST] = { |
| 48 | [TEST_NONE] = "none", |
| 49 | [TEST_CORE] = "core", |
| 50 | [TEST_CPUS] = "processors", |
| 51 | [TEST_PLATFORM] = "platform", |
| 52 | [TEST_DEVICES] = "devices", |
| 53 | [TEST_FREEZER] = "freezer", |
| 54 | }; |
| 55 | |
| 56 | static ssize_t pm_test_show(struct kset *kset, char *buf) |
| 57 | { |
| 58 | char *s = buf; |
| 59 | int level; |
| 60 | |
| 61 | for (level = TEST_FIRST; level <= TEST_MAX; level++) |
| 62 | if (pm_tests[level]) { |
| 63 | if (level == pm_test_level) |
| 64 | s += sprintf(s, "[%s] ", pm_tests[level]); |
| 65 | else |
| 66 | s += sprintf(s, "%s ", pm_tests[level]); |
| 67 | } |
| 68 | |
| 69 | if (s != buf) |
| 70 | /* convert the last space to a newline */ |
| 71 | *(s-1) = '\n'; |
| 72 | |
| 73 | return (s - buf); |
| 74 | } |
| 75 | |
| 76 | static ssize_t pm_test_store(struct kset *kset, const char *buf, size_t n) |
| 77 | { |
| 78 | const char * const *s; |
| 79 | int level; |
| 80 | char *p; |
| 81 | int len; |
| 82 | int error = -EINVAL; |
| 83 | |
| 84 | p = memchr(buf, '\n', n); |
| 85 | len = p ? p - buf : n; |
| 86 | |
| 87 | mutex_lock(&pm_mutex); |
| 88 | |
| 89 | level = TEST_FIRST; |
| 90 | for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++) |
| 91 | if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) { |
| 92 | pm_test_level = level; |
| 93 | error = 0; |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | mutex_unlock(&pm_mutex); |
| 98 | |
| 99 | return error ? error : n; |
| 100 | } |
| 101 | |
| 102 | power_attr(pm_test); |
| 103 | #else /* !CONFIG_PM_DEBUG */ |
| 104 | static inline int suspend_test(int level) { return 0; } |
| 105 | #endif /* !CONFIG_PM_DEBUG */ |
| 106 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 107 | #ifdef CONFIG_SUSPEND |
| 108 | |
| 109 | /* This is just an arbitrary number */ |
| 110 | #define FREE_PAGE_NUMBER (100) |
| 111 | |
Rafael J. Wysocki | f242d91 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 112 | static struct platform_suspend_ops *suspend_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 115 | * suspend_set_ops - Set the global suspend method table. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | * @ops: Pointer to ops structure. |
| 117 | */ |
| 118 | |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 119 | void suspend_set_ops(struct platform_suspend_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 121 | mutex_lock(&pm_mutex); |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 122 | suspend_ops = ops; |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 123 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Johannes Berg | e8c9c50 | 2007-04-30 15:09:54 -0700 | [diff] [blame] | 126 | /** |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 127 | * suspend_valid_only_mem - generic memory-only valid callback |
Johannes Berg | e8c9c50 | 2007-04-30 15:09:54 -0700 | [diff] [blame] | 128 | * |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 129 | * Platform drivers that implement mem suspend only and only need |
Johannes Berg | e8c9c50 | 2007-04-30 15:09:54 -0700 | [diff] [blame] | 130 | * to check for that in their .valid callback can use this instead |
| 131 | * of rolling their own .valid callback. |
| 132 | */ |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 133 | int suspend_valid_only_mem(suspend_state_t state) |
Johannes Berg | e8c9c50 | 2007-04-30 15:09:54 -0700 | [diff] [blame] | 134 | { |
| 135 | return state == PM_SUSPEND_MEM; |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /** |
| 139 | * suspend_prepare - Do prep work before entering low-power state. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | * |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 141 | * This is common code that is called for each state that we're entering. |
| 142 | * Run suspend notifiers, allocate a console and stop all processes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | */ |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 144 | static int suspend_prepare(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 146 | int error; |
David Shaohua Li | 5ae947e | 2005-03-18 16:27:13 -0500 | [diff] [blame] | 147 | unsigned int free_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 149 | if (!suspend_ops || !suspend_ops->enter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return -EPERM; |
| 151 | |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 152 | error = pm_notifier_call_chain(PM_SUSPEND_PREPARE); |
| 153 | if (error) |
| 154 | goto Finish; |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | pm_prepare_console(); |
| 157 | |
| 158 | if (freeze_processes()) { |
| 159 | error = -EAGAIN; |
| 160 | goto Thaw; |
| 161 | } |
| 162 | |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 163 | free_pages = global_page_state(NR_FREE_PAGES); |
| 164 | if (free_pages < FREE_PAGE_NUMBER) { |
David Shaohua Li | 5ae947e | 2005-03-18 16:27:13 -0500 | [diff] [blame] | 165 | pr_debug("PM: free some memory\n"); |
| 166 | shrink_all_memory(FREE_PAGE_NUMBER - free_pages); |
| 167 | if (nr_free_pages() < FREE_PAGE_NUMBER) { |
| 168 | error = -ENOMEM; |
| 169 | printk(KERN_ERR "PM: No enough memory\n"); |
David Shaohua Li | 5ae947e | 2005-03-18 16:27:13 -0500 | [diff] [blame] | 170 | } |
| 171 | } |
Rafael J. Wysocki | e3c7db62 | 2007-02-10 01:43:31 -0800 | [diff] [blame] | 172 | if (!error) |
| 173 | return 0; |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | Thaw: |
| 176 | thaw_processes(); |
| 177 | pm_restore_console(); |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 178 | Finish: |
| 179 | pm_notifier_call_chain(PM_POST_SUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return error; |
| 181 | } |
| 182 | |
Johannes Berg | a53c46d | 2007-04-26 11:43:58 +0200 | [diff] [blame] | 183 | /* default implementation */ |
| 184 | void __attribute__ ((weak)) arch_suspend_disable_irqs(void) |
| 185 | { |
| 186 | local_irq_disable(); |
| 187 | } |
| 188 | |
| 189 | /* default implementation */ |
| 190 | void __attribute__ ((weak)) arch_suspend_enable_irqs(void) |
| 191 | { |
| 192 | local_irq_enable(); |
| 193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 195 | /** |
| 196 | * suspend_enter - enter the desired system sleep state. |
| 197 | * @state: state to enter |
| 198 | * |
| 199 | * This function should be called after devices have been suspended. |
| 200 | */ |
Adrian Bunk | a065c86 | 2007-10-18 03:04:37 -0700 | [diff] [blame] | 201 | static int suspend_enter(suspend_state_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Johannes Berg | a53c46d | 2007-04-26 11:43:58 +0200 | [diff] [blame] | 205 | arch_suspend_disable_irqs(); |
| 206 | BUG_ON(!irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | if ((error = device_power_down(PMSG_SUSPEND))) { |
| 209 | printk(KERN_ERR "Some devices failed to power down\n"); |
| 210 | goto Done; |
| 211 | } |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 212 | |
| 213 | if (!suspend_test(TEST_CORE)) |
| 214 | error = suspend_ops->enter(state); |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | device_power_up(); |
| 217 | Done: |
Johannes Berg | a53c46d | 2007-04-26 11:43:58 +0200 | [diff] [blame] | 218 | arch_suspend_enable_irqs(); |
| 219 | BUG_ON(irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return error; |
| 221 | } |
| 222 | |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 223 | /** |
| 224 | * suspend_devices_and_enter - suspend devices and enter the desired system sleep |
| 225 | * state. |
| 226 | * @state: state to enter |
| 227 | */ |
| 228 | int suspend_devices_and_enter(suspend_state_t state) |
| 229 | { |
| 230 | int error; |
| 231 | |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 232 | if (!suspend_ops) |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 233 | return -ENOSYS; |
| 234 | |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 235 | if (suspend_ops->set_target) { |
| 236 | error = suspend_ops->set_target(state); |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 237 | if (error) |
| 238 | return error; |
| 239 | } |
| 240 | suspend_console(); |
| 241 | error = device_suspend(PMSG_SUSPEND); |
| 242 | if (error) { |
| 243 | printk(KERN_ERR "Some devices failed to suspend\n"); |
| 244 | goto Resume_console; |
| 245 | } |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 246 | |
| 247 | if (suspend_test(TEST_DEVICES)) |
| 248 | goto Resume_devices; |
| 249 | |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 250 | if (suspend_ops->prepare) { |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 251 | error = suspend_ops->prepare(); |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 252 | if (error) |
| 253 | goto Resume_devices; |
| 254 | } |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 255 | |
| 256 | if (suspend_test(TEST_PLATFORM)) |
| 257 | goto Finish; |
| 258 | |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 259 | error = disable_nonboot_cpus(); |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 260 | if (!error && !suspend_test(TEST_CPUS)) |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 261 | suspend_enter(state); |
| 262 | |
| 263 | enable_nonboot_cpus(); |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 264 | Finish: |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 265 | if (suspend_ops->finish) |
| 266 | suspend_ops->finish(); |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 267 | Resume_devices: |
| 268 | device_resume(); |
| 269 | Resume_console: |
| 270 | resume_console(); |
| 271 | return error; |
| 272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * suspend_finish - Do final work before exiting suspend sequence. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | * |
| 277 | * Call platform code to clean up, restart processes, and free the |
| 278 | * console that we've allocated. This is not called for suspend-to-disk. |
| 279 | */ |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 280 | static void suspend_finish(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | thaw_processes(); |
| 283 | pm_restore_console(); |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 284 | pm_notifier_call_chain(PM_POST_SUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
Andreas Mohr | 3b364b8 | 2006-06-25 05:47:56 -0700 | [diff] [blame] | 290 | static const char * const pm_states[PM_SUSPEND_MAX] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | [PM_SUSPEND_STANDBY] = "standby", |
| 292 | [PM_SUSPEND_MEM] = "mem", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 295 | static inline int valid_state(suspend_state_t state) |
| 296 | { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 297 | /* All states need lowlevel support and need to be valid |
| 298 | * to the lowlevel implementation, no valid callback |
Johannes Berg | 9684e51 | 2007-04-30 15:09:55 -0700 | [diff] [blame] | 299 | * implies that none are valid. */ |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 300 | if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state)) |
Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 301 | return 0; |
| 302 | return 1; |
| 303 | } |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | /** |
| 307 | * enter_state - Do common work of entering low-power state. |
| 308 | * @state: pm_state structure for state we're entering. |
| 309 | * |
| 310 | * Make sure we're the only ones trying to enter a sleep state. Fail |
| 311 | * if someone has beat us to it, since we don't want anything weird to |
| 312 | * happen when we wake up. |
| 313 | * Then, do the setup for suspend, enter the state, and cleaup (after |
| 314 | * we've woken up). |
| 315 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | static int enter_state(suspend_state_t state) |
| 317 | { |
| 318 | int error; |
| 319 | |
Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 320 | if (!valid_state(state)) |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 321 | return -ENODEV; |
Rafael J. Wysocki | 232b143 | 2007-10-18 03:04:44 -0700 | [diff] [blame] | 322 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 323 | if (!mutex_trylock(&pm_mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return -EBUSY; |
| 325 | |
Rafael J. Wysocki | 232b143 | 2007-10-18 03:04:44 -0700 | [diff] [blame] | 326 | printk("Syncing filesystems ... "); |
| 327 | sys_sync(); |
| 328 | printk("done.\n"); |
| 329 | |
David Brownell | 82428b6 | 2005-05-09 08:07:00 -0700 | [diff] [blame] | 330 | pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]); |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 331 | error = suspend_prepare(); |
| 332 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | goto Unlock; |
| 334 | |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 335 | if (suspend_test(TEST_FREEZER)) |
| 336 | goto Finish; |
| 337 | |
David Brownell | 82428b6 | 2005-05-09 08:07:00 -0700 | [diff] [blame] | 338 | pr_debug("PM: Entering %s sleep\n", pm_states[state]); |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 339 | error = suspend_devices_and_enter(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 341 | Finish: |
David Brownell | 82428b6 | 2005-05-09 08:07:00 -0700 | [diff] [blame] | 342 | pr_debug("PM: Finishing wakeup.\n"); |
Rafael J. Wysocki | 6c961df | 2007-07-19 01:47:38 -0700 | [diff] [blame] | 343 | suspend_finish(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | Unlock: |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 345 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | return error; |
| 347 | } |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
| 350 | /** |
| 351 | * pm_suspend - Externally visible function for suspending system. |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 352 | * @state: Enumerated value of state to enter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | * |
| 354 | * Determine whether or not value is within range, get state |
| 355 | * structure, and enter (above). |
| 356 | */ |
| 357 | |
| 358 | int pm_suspend(suspend_state_t state) |
| 359 | { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 360 | if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return enter_state(state); |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | |
Ralf Baechle | 4cf3034 | 2006-12-06 20:36:06 -0800 | [diff] [blame] | 365 | EXPORT_SYMBOL(pm_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 367 | #endif /* CONFIG_SUSPEND */ |
| 368 | |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 369 | struct kobject *power_kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | /** |
| 372 | * state - control system power state. |
| 373 | * |
| 374 | * show() returns what states are supported, which is hard-coded to |
| 375 | * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and |
| 376 | * 'disk' (Suspend-to-Disk). |
| 377 | * |
| 378 | * store() accepts one of those strings, translates it into the |
| 379 | * proper enumerated value, and initiates a suspend transition. |
| 380 | */ |
| 381 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 382 | static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 383 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 385 | char *s = buf; |
| 386 | #ifdef CONFIG_SUSPEND |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | for (i = 0; i < PM_SUSPEND_MAX; i++) { |
Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 390 | if (pm_states[i] && valid_state(i)) |
| 391 | s += sprintf(s,"%s ", pm_states[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 393 | #endif |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 394 | #ifdef CONFIG_HIBERNATION |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 395 | s += sprintf(s, "%s\n", "disk"); |
| 396 | #else |
| 397 | if (s != buf) |
| 398 | /* convert the last space to a newline */ |
| 399 | *(s-1) = '\n'; |
| 400 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return (s - buf); |
| 402 | } |
| 403 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 404 | static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 405 | const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 407 | #ifdef CONFIG_SUSPEND |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | suspend_state_t state = PM_SUSPEND_STANDBY; |
Andreas Mohr | 3b364b8 | 2006-06-25 05:47:56 -0700 | [diff] [blame] | 409 | const char * const *s; |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 410 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | int len; |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 413 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | p = memchr(buf, '\n', n); |
| 416 | len = p ? p - buf : n; |
| 417 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 418 | /* First, check if we are requested to hibernate */ |
Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 419 | if (len == 4 && !strncmp(buf, "disk", len)) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 420 | error = hibernate(); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 421 | goto Exit; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 424 | #ifdef CONFIG_SUSPEND |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { |
Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 426 | if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | break; |
| 428 | } |
dean gaudet | 47bb789 | 2006-04-27 18:39:17 -0700 | [diff] [blame] | 429 | if (state < PM_SUSPEND_MAX && *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | error = enter_state(state); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 431 | #endif |
| 432 | |
| 433 | Exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return error ? error : n; |
| 435 | } |
| 436 | |
| 437 | power_attr(state); |
| 438 | |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 439 | #ifdef CONFIG_PM_TRACE |
| 440 | int pm_trace_enabled; |
| 441 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 442 | static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 443 | char *buf) |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 444 | { |
| 445 | return sprintf(buf, "%d\n", pm_trace_enabled); |
| 446 | } |
| 447 | |
| 448 | static ssize_t |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 449 | pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 450 | const char *buf, size_t n) |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 451 | { |
| 452 | int val; |
| 453 | |
| 454 | if (sscanf(buf, "%d", &val) == 1) { |
| 455 | pm_trace_enabled = !!val; |
| 456 | return n; |
| 457 | } |
| 458 | return -EINVAL; |
| 459 | } |
| 460 | |
| 461 | power_attr(pm_trace); |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 462 | #endif /* CONFIG_PM_TRACE */ |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 463 | |
| 464 | static struct attribute * g[] = { |
| 465 | &state_attr.attr, |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 466 | #ifdef CONFIG_PM_TRACE |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 467 | &pm_trace_attr.attr, |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame^] | 468 | #endif |
| 469 | #ifdef CONFIG_PM_DEBUG |
| 470 | &pm_test_attr.attr, |
| 471 | #endif |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 472 | NULL, |
| 473 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | static struct attribute_group attr_group = { |
| 476 | .attrs = g, |
| 477 | }; |
| 478 | |
| 479 | |
| 480 | static int __init pm_init(void) |
| 481 | { |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 482 | power_kobj = kobject_create_and_add("power", NULL); |
| 483 | if (!power_kobj) |
Greg Kroah-Hartman | 039a5dc | 2007-11-01 10:39:50 -0700 | [diff] [blame] | 484 | return -ENOMEM; |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 485 | return sysfs_create_group(power_kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | core_initcall(pm_init); |