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