blob: af7f4663deaa40c044a7be4aff7775e9db407559 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sleep.c - ACPI sleep support.
3 *
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -05004 * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
6 * Copyright (c) 2000-2003 Patrick Mochel
7 * Copyright (c) 2003 Open Source Development Lab
8 *
9 * This file is released under the GPLv2.
10 *
11 */
12
13#include <linux/delay.h>
14#include <linux/irq.h>
15#include <linux/dmi.h>
16#include <linux/device.h>
17#include <linux/suspend.h>
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +040018
19#include <asm/io.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <acpi/acpi_bus.h>
22#include <acpi/acpi_drivers.h>
23#include "sleep.h"
24
25u8 sleep_states[ACPI_S_STATE_COUNT];
26
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010027static int acpi_sleep_prepare(u32 acpi_state)
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020028{
29#ifdef CONFIG_ACPI_SLEEP
30 /* do we have a wakeup address for S2 and S3? */
31 if (acpi_state == ACPI_STATE_S3) {
32 if (!acpi_wakeup_address) {
33 return -EFAULT;
34 }
35 acpi_set_firmware_waking_vector((acpi_physical_address)
36 virt_to_phys((void *)
37 acpi_wakeup_address));
38
39 }
40 ACPI_FLUSH_CPU_CACHE();
41 acpi_enable_wakeup_device_prep(acpi_state);
42#endif
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010043 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
44 acpi_state);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020045 acpi_enter_sleep_state_prep(acpi_state);
46 return 0;
47}
48
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +020049#ifdef CONFIG_PM_SLEEP
50static u32 acpi_target_sleep_state = ACPI_STATE_S0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +020052/*
53 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
54 * user to request that behavior by using the 'acpi_old_suspend_ordering'
55 * kernel command line option that causes the following variable to be set.
56 */
57static bool old_suspend_ordering;
58
59void __init acpi_old_suspend_ordering(void)
60{
61 old_suspend_ordering = true;
62}
63
64/**
65 * acpi_pm_disable_gpes - Disable the GPEs.
66 */
67static int acpi_pm_disable_gpes(void)
68{
69 acpi_hw_disable_all_gpes();
70 return 0;
71}
72
73/**
74 * __acpi_pm_prepare - Prepare the platform to enter the target state.
75 *
76 * If necessary, set the firmware waking vector and do arch-specific
77 * nastiness to get the wakeup code to the waking vector.
78 */
79static int __acpi_pm_prepare(void)
80{
81 int error = acpi_sleep_prepare(acpi_target_sleep_state);
82
83 if (error)
84 acpi_target_sleep_state = ACPI_STATE_S0;
85 return error;
86}
87
88/**
89 * acpi_pm_prepare - Prepare the platform to enter the target sleep
90 * state and disable the GPEs.
91 */
92static int acpi_pm_prepare(void)
93{
94 int error = __acpi_pm_prepare();
95
96 if (!error)
97 acpi_hw_disable_all_gpes();
98 return error;
99}
100
101/**
102 * acpi_pm_finish - Instruct the platform to leave a sleep state.
103 *
104 * This is called after we wake back up (or if entering the sleep state
105 * failed).
106 */
107static void acpi_pm_finish(void)
108{
109 u32 acpi_state = acpi_target_sleep_state;
110
111 if (acpi_state == ACPI_STATE_S0)
112 return;
113
114 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
115 acpi_state);
116 acpi_disable_wakeup_device(acpi_state);
117 acpi_leave_sleep_state(acpi_state);
118
119 /* reset firmware waking vector */
120 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
121
122 acpi_target_sleep_state = ACPI_STATE_S0;
123}
124
125/**
126 * acpi_pm_end - Finish up suspend sequence.
127 */
128static void acpi_pm_end(void)
129{
130 /*
131 * This is necessary in case acpi_pm_finish() is not called during a
132 * failing transition to a sleep state.
133 */
134 acpi_target_sleep_state = ACPI_STATE_S0;
135}
136#endif /* CONFIG_PM_SLEEP */
137
138#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139extern void do_suspend_lowlevel(void);
140
141static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500142 [PM_SUSPEND_ON] = ACPI_STATE_S0,
143 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
144 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500145 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200148/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400149 * acpi_suspend_begin - Set the target system sleep state to the state
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200150 * associated with given @pm_state, if supported.
151 */
Len Brown2c6e33c2008-04-23 18:02:52 -0400152static int acpi_suspend_begin(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 u32 acpi_state = acpi_suspend_states[pm_state];
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200155 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200157 if (sleep_states[acpi_state]) {
158 acpi_target_sleep_state = acpi_state;
159 } else {
160 printk(KERN_ERR "ACPI does not support this state: %d\n",
161 pm_state);
162 error = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200164 return error;
165}
166
167/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400168 * acpi_suspend_enter - Actually enter a sleep state.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200169 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200171 * Flush caches and go to sleep. For STR we have to call arch-specific
172 * assembly, which in turn call acpi_enter_sleep_state().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * It's unfortunate, but it works. Please fix if you're feeling frisky.
174 */
Len Brown2c6e33c2008-04-23 18:02:52 -0400175static int acpi_suspend_enter(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 acpi_status status = AE_OK;
178 unsigned long flags = 0;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200179 u32 acpi_state = acpi_target_sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 ACPI_FLUSH_CPU_CACHE();
182
183 /* Do arch specific saving of state. */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200184 if (acpi_state == ACPI_STATE_S3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int error = acpi_save_state_mem();
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200186
Rafael J. Wysocki60417f52008-01-08 00:07:39 +0100187 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return error;
189 }
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 local_irq_save(flags);
192 acpi_enable_wakeup_device(acpi_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200193 switch (acpi_state) {
194 case ACPI_STATE_S1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 barrier();
196 status = acpi_enter_sleep_state(acpi_state);
197 break;
198
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200199 case ACPI_STATE_S3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 do_suspend_lowlevel();
201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400203
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100204 /* Reprogram control registers and execute _BFS */
205 acpi_leave_sleep_state_prep(acpi_state);
206
Pavel Machek23b168d2008-02-05 19:27:12 +0100207 /* ACPI 3.0 specs (P62) says that it's the responsibility
Arnaud Patard872d83d2006-04-27 05:25:00 -0400208 * of the OSPM to clear the status bit [ implying that the
209 * POWER_BUTTON event should not reach userspace ]
210 */
211 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
212 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
213
Shaohua Lia3627f62007-06-20 09:17:58 +0800214 /*
215 * Disable and clear GPE status before interrupt is enabled. Some GPEs
216 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
217 * acpi_leave_sleep_state will reenable specific GPEs later
218 */
219 acpi_hw_disable_all_gpes();
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 local_irq_restore(flags);
222 printk(KERN_DEBUG "Back to C!\n");
223
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200224 /* restore processor state */
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200225 if (acpi_state == ACPI_STATE_S3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 acpi_restore_state_mem();
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
229}
230
Len Brown2c6e33c2008-04-23 18:02:52 -0400231static int acpi_suspend_state_valid(suspend_state_t pm_state)
Shaohua Lieb9289e2005-10-30 15:00:01 -0800232{
Johannes Berge8c9c502007-04-30 15:09:54 -0700233 u32 acpi_state;
Shaohua Lieb9289e2005-10-30 15:00:01 -0800234
Johannes Berge8c9c502007-04-30 15:09:54 -0700235 switch (pm_state) {
236 case PM_SUSPEND_ON:
237 case PM_SUSPEND_STANDBY:
238 case PM_SUSPEND_MEM:
239 acpi_state = acpi_suspend_states[pm_state];
240
241 return sleep_states[acpi_state];
242 default:
243 return 0;
244 }
Shaohua Lieb9289e2005-10-30 15:00:01 -0800245}
246
Len Brown2c6e33c2008-04-23 18:02:52 -0400247static struct platform_suspend_ops acpi_suspend_ops = {
248 .valid = acpi_suspend_state_valid,
249 .begin = acpi_suspend_begin,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200250 .prepare = acpi_pm_prepare,
Len Brown2c6e33c2008-04-23 18:02:52 -0400251 .enter = acpi_suspend_enter,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200252 .finish = acpi_pm_finish,
253 .end = acpi_pm_end,
254};
255
256/**
257 * acpi_suspend_begin_old - Set the target system sleep state to the
258 * state associated with given @pm_state, if supported, and
259 * execute the _PTS control method. This function is used if the
260 * pre-ACPI 2.0 suspend ordering has been requested.
261 */
262static int acpi_suspend_begin_old(suspend_state_t pm_state)
263{
264 int error = acpi_suspend_begin(pm_state);
265
266 if (!error)
267 error = __acpi_pm_prepare();
268 return error;
269}
270
271/*
272 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
273 * been requested.
274 */
275static struct platform_suspend_ops acpi_suspend_ops_old = {
276 .valid = acpi_suspend_state_valid,
277 .begin = acpi_suspend_begin_old,
278 .prepare = acpi_pm_disable_gpes,
279 .enter = acpi_suspend_enter,
280 .finish = acpi_pm_finish,
281 .end = acpi_pm_end,
282 .recover = acpi_pm_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283};
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200284#endif /* CONFIG_SUSPEND */
285
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200286#ifdef CONFIG_HIBERNATION
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100287static int acpi_hibernation_begin(void)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700288{
289 acpi_target_sleep_state = ACPI_STATE_S4;
Rafael J. Wysocki7731ce62008-03-30 02:19:07 +0100290 return 0;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700291}
292
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700293static int acpi_hibernation_enter(void)
294{
295 acpi_status status = AE_OK;
296 unsigned long flags = 0;
297
298 ACPI_FLUSH_CPU_CACHE();
299
300 local_irq_save(flags);
301 acpi_enable_wakeup_device(ACPI_STATE_S4);
302 /* This shouldn't return. If it returns, we have a problem */
303 status = acpi_enter_sleep_state(ACPI_STATE_S4);
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100304 /* Reprogram control registers and execute _BFS */
305 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700306 local_irq_restore(flags);
307
308 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
309}
310
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700311static void acpi_hibernation_leave(void)
312{
313 /*
314 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
315 * enable it here.
316 */
317 acpi_enable();
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100318 /* Reprogram control registers and execute _BFS */
319 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700320}
321
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200322static void acpi_pm_enable_gpes(void)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700323{
324 acpi_hw_enable_all_runtime_gpes();
325}
326
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -0700327static struct platform_hibernation_ops acpi_hibernation_ops = {
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100328 .begin = acpi_hibernation_begin,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200329 .end = acpi_pm_end,
330 .pre_snapshot = acpi_pm_prepare,
331 .finish = acpi_pm_finish,
332 .prepare = acpi_pm_prepare,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700333 .enter = acpi_hibernation_enter,
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700334 .leave = acpi_hibernation_leave,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200335 .pre_restore = acpi_pm_disable_gpes,
336 .restore_cleanup = acpi_pm_enable_gpes,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700337};
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200338
339/**
340 * acpi_hibernation_begin_old - Set the target system sleep state to
341 * ACPI_STATE_S4 and execute the _PTS control method. This
342 * function is used if the pre-ACPI 2.0 suspend ordering has been
343 * requested.
344 */
345static int acpi_hibernation_begin_old(void)
346{
347 int error = acpi_sleep_prepare(ACPI_STATE_S4);
348
349 if (!error)
350 acpi_target_sleep_state = ACPI_STATE_S4;
351 return error;
352}
353
354/*
355 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
356 * been requested.
357 */
358static struct platform_hibernation_ops acpi_hibernation_ops_old = {
359 .begin = acpi_hibernation_begin_old,
360 .end = acpi_pm_end,
361 .pre_snapshot = acpi_pm_disable_gpes,
362 .finish = acpi_pm_finish,
363 .prepare = acpi_pm_disable_gpes,
364 .enter = acpi_hibernation_enter,
365 .leave = acpi_hibernation_leave,
366 .pre_restore = acpi_pm_disable_gpes,
367 .restore_cleanup = acpi_pm_enable_gpes,
368 .recover = acpi_pm_finish,
369};
370#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700371
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200372int acpi_suspend(u32 acpi_state)
373{
374 suspend_state_t states[] = {
375 [1] = PM_SUSPEND_STANDBY,
376 [3] = PM_SUSPEND_MEM,
377 [5] = PM_SUSPEND_MAX
378 };
379
380 if (acpi_state < 6 && states[acpi_state])
381 return pm_suspend(states[acpi_state]);
382 if (acpi_state == 4)
383 return hibernate();
384 return -EINVAL;
385}
386
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400387#ifdef CONFIG_PM_SLEEP
Shaohua Lifd4aff12007-07-17 22:40:25 +0200388/**
389 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
390 * in the system sleep state given by %acpi_target_sleep_state
David Brownell06166782008-06-05 01:15:40 +0200391 * @dev: device to examine; its driver model wakeup flags control
392 * whether it should be able to wake up the system
Shaohua Lifd4aff12007-07-17 22:40:25 +0200393 * @d_min_p: used to store the upper limit of allowed states range
394 * Return value: preferred power state of the device on success, -ENODEV on
395 * failure (ie. if there's no 'struct acpi_device' for @dev)
396 *
397 * Find the lowest power (highest number) ACPI device power state that
398 * device @dev can be in while the system is in the sleep state represented
399 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
400 * able to wake up the system from this sleep state. If @d_min_p is set,
401 * the highest power (lowest number) device power state of @dev allowed
402 * in this system sleep state is stored at the location pointed to by it.
403 *
404 * The caller must ensure that @dev is valid before using this function.
405 * The caller is also responsible for figuring out if the device is
406 * supposed to be able to wake up the system and passing this information
407 * via @wake.
408 */
409
David Brownell06166782008-06-05 01:15:40 +0200410int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
Shaohua Lifd4aff12007-07-17 22:40:25 +0200411{
412 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
413 struct acpi_device *adev;
414 char acpi_method[] = "_SxD";
415 unsigned long d_min, d_max;
416
417 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Shaohua Liead77592007-08-23 15:01:13 +0800418 printk(KERN_DEBUG "ACPI handle has no context!\n");
Shaohua Lifd4aff12007-07-17 22:40:25 +0200419 return -ENODEV;
420 }
421
422 acpi_method[2] = '0' + acpi_target_sleep_state;
423 /*
424 * If the sleep state is S0, we will return D3, but if the device has
425 * _S0W, we will use the value from _S0W
426 */
427 d_min = ACPI_STATE_D0;
428 d_max = ACPI_STATE_D3;
429
430 /*
431 * If present, _SxD methods return the minimum D-state (highest power
432 * state) we can use for the corresponding S-states. Otherwise, the
433 * minimum D-state is D0 (ACPI 3.x).
434 *
435 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
436 * provided -- that's our fault recovery, we ignore retval.
437 */
438 if (acpi_target_sleep_state > ACPI_STATE_S0)
439 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
440
441 /*
442 * If _PRW says we can wake up the system from the target sleep state,
443 * the D-state returned by _SxD is sufficient for that (we assume a
444 * wakeup-aware driver if wake is set). Still, if _SxW exists
445 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
446 * can wake the system. _S0W may be valid, too.
447 */
448 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
David Brownell06166782008-06-05 01:15:40 +0200449 (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
Shaohua Lifd4aff12007-07-17 22:40:25 +0200450 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100451 acpi_status status;
452
Shaohua Lifd4aff12007-07-17 22:40:25 +0200453 acpi_method[3] = 'W';
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100454 status = acpi_evaluate_integer(handle, acpi_method, NULL,
455 &d_max);
456 if (ACPI_FAILURE(status)) {
457 d_max = d_min;
458 } else if (d_max < d_min) {
459 /* Warn the user of the broken DSDT */
460 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
461 acpi_method);
462 /* Sanitize it */
Shaohua Lifd4aff12007-07-17 22:40:25 +0200463 d_min = d_max;
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100464 }
Shaohua Lifd4aff12007-07-17 22:40:25 +0200465 }
466
467 if (d_min_p)
468 *d_min_p = d_min;
469 return d_max;
470}
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200471
472/**
473 * acpi_pm_device_sleep_wake - enable or disable the system wake-up
474 * capability of given device
475 * @dev: device to handle
476 * @enable: 'true' - enable, 'false' - disable the wake-up capability
477 */
478int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
479{
480 acpi_handle handle;
481 struct acpi_device *adev;
482
483 if (!device_may_wakeup(dev))
484 return -EINVAL;
485
486 handle = DEVICE_ACPI_HANDLE(dev);
487 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
488 printk(KERN_DEBUG "ACPI handle has no context!\n");
489 return -ENODEV;
490 }
491
492 return enable ?
493 acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
494 acpi_disable_wakeup_device_power(adev);
495}
Alexey Starikovskiy853298b2007-09-25 18:45:15 +0400496#endif
Shaohua Lifd4aff12007-07-17 22:40:25 +0200497
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400498static void acpi_power_off_prepare(void)
499{
500 /* Prepare to power off the system */
501 acpi_sleep_prepare(ACPI_STATE_S5);
Rafael J. Wysocki3c1d2b62008-01-08 00:06:16 +0100502 acpi_hw_disable_all_gpes();
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400503}
504
505static void acpi_power_off(void)
506{
507 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
Harvey Harrison96b2dd12008-03-05 18:24:51 -0800508 printk("%s called\n", __func__);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400509 local_irq_disable();
Alexey Starikovskiy9c1c6a12007-10-22 14:18:06 +0400510 acpi_enable_wakeup_device(ACPI_STATE_S5);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400511 acpi_enter_sleep_state(ACPI_STATE_S5);
512}
513
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500514int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200516 acpi_status status;
517 u8 type_a, type_b;
518#ifdef CONFIG_SUSPEND
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500519 int i = 0;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200520#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 if (acpi_disabled)
523 return 0;
524
Frans Pop5a50fe72007-09-20 22:27:44 +0200525 sleep_states[ACPI_STATE_S0] = 1;
526 printk(KERN_INFO PREFIX "(supports S0");
527
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200528#ifdef CONFIG_SUSPEND
Frans Pop5a50fe72007-09-20 22:27:44 +0200529 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
531 if (ACPI_SUCCESS(status)) {
532 sleep_states[i] = 1;
533 printk(" S%d", i);
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200537 suspend_set_ops(old_suspend_ordering ?
538 &acpi_suspend_ops_old : &acpi_suspend_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200539#endif
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700540
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200541#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200542 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
543 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200544 hibernation_set_ops(old_suspend_ordering ?
545 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200546 sleep_states[ACPI_STATE_S4] = 1;
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400547 printk(" S4");
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200548 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700549#endif
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400550 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
551 if (ACPI_SUCCESS(status)) {
552 sleep_states[ACPI_STATE_S5] = 1;
553 printk(" S5");
554 pm_power_off_prepare = acpi_power_off_prepare;
555 pm_power_off = acpi_power_off;
556 }
557 printk(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 0;
559}