Merge branch 'for-2.6.30' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6] / drivers / acpi / wakeup.c
1 /*
2  * wakeup.c - support wakeup devices
3  * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com>
4  */
5
6 #include <linux/init.h>
7 #include <linux/acpi.h>
8 #include <acpi/acpi_drivers.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11
12 #include "internal.h"
13 #include "sleep.h"
14
15 #define _COMPONENT              ACPI_SYSTEM_COMPONENT
16 ACPI_MODULE_NAME("wakeup_devices")
17
18 extern struct list_head acpi_wakeup_device_list;
19 extern spinlock_t acpi_device_lock;
20
21 /**
22  * acpi_enable_wakeup_device_prep - prepare wakeup devices
23  *      @sleep_state:   ACPI state
24  * Enable all wakup devices power if the devices' wakeup level
25  * is higher than requested sleep level
26  */
27
28 void acpi_enable_wakeup_device_prep(u8 sleep_state)
29 {
30         struct list_head *node, *next;
31
32         spin_lock(&acpi_device_lock);
33         list_for_each_safe(node, next, &acpi_wakeup_device_list) {
34                 struct acpi_device *dev = container_of(node,
35                                                        struct acpi_device,
36                                                        wakeup_list);
37
38                 if (!dev->wakeup.flags.valid ||
39                     !dev->wakeup.state.enabled ||
40                     (sleep_state > (u32) dev->wakeup.sleep_state))
41                         continue;
42
43                 spin_unlock(&acpi_device_lock);
44                 acpi_enable_wakeup_device_power(dev, sleep_state);
45                 spin_lock(&acpi_device_lock);
46         }
47         spin_unlock(&acpi_device_lock);
48 }
49
50 /**
51  * acpi_enable_wakeup_device - enable wakeup devices
52  *      @sleep_state:   ACPI state
53  * Enable all wakup devices's GPE
54  */
55 void acpi_enable_wakeup_device(u8 sleep_state)
56 {
57         struct list_head *node, *next;
58
59         /* 
60          * Caution: this routine must be invoked when interrupt is disabled 
61          * Refer ACPI2.0: P212
62          */
63         spin_lock(&acpi_device_lock);
64         list_for_each_safe(node, next, &acpi_wakeup_device_list) {
65                 struct acpi_device *dev =
66                         container_of(node, struct acpi_device, wakeup_list);
67
68                 if (!dev->wakeup.flags.valid)
69                         continue;
70
71                 /* If users want to disable run-wake GPE,
72                  * we only disable it for wake and leave it for runtime
73                  */
74                 if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
75                     || sleep_state > (u32) dev->wakeup.sleep_state) {
76                         if (dev->wakeup.flags.run_wake) {
77                                 spin_unlock(&acpi_device_lock);
78                                 /* set_gpe_type will disable GPE, leave it like that */
79                                 acpi_set_gpe_type(dev->wakeup.gpe_device,
80                                                   dev->wakeup.gpe_number,
81                                                   ACPI_GPE_TYPE_RUNTIME);
82                                 spin_lock(&acpi_device_lock);
83                         }
84                         continue;
85                 }
86                 spin_unlock(&acpi_device_lock);
87                 if (!dev->wakeup.flags.run_wake)
88                         acpi_enable_gpe(dev->wakeup.gpe_device,
89                                         dev->wakeup.gpe_number);
90                 spin_lock(&acpi_device_lock);
91         }
92         spin_unlock(&acpi_device_lock);
93 }
94
95 /**
96  * acpi_disable_wakeup_device - disable devices' wakeup capability
97  *      @sleep_state:   ACPI state
98  * Disable all wakup devices's GPE and wakeup capability
99  */
100 void acpi_disable_wakeup_device(u8 sleep_state)
101 {
102         struct list_head *node, *next;
103
104         spin_lock(&acpi_device_lock);
105         list_for_each_safe(node, next, &acpi_wakeup_device_list) {
106                 struct acpi_device *dev =
107                         container_of(node, struct acpi_device, wakeup_list);
108
109                 if (!dev->wakeup.flags.valid)
110                         continue;
111
112                 if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
113                     || sleep_state > (u32) dev->wakeup.sleep_state) {
114                         if (dev->wakeup.flags.run_wake) {
115                                 spin_unlock(&acpi_device_lock);
116                                 acpi_set_gpe_type(dev->wakeup.gpe_device,
117                                                   dev->wakeup.gpe_number,
118                                                   ACPI_GPE_TYPE_WAKE_RUN);
119                                 /* Re-enable it, since set_gpe_type will disable it */
120                                 acpi_enable_gpe(dev->wakeup.gpe_device,
121                                                 dev->wakeup.gpe_number);
122                                 spin_lock(&acpi_device_lock);
123                         }
124                         continue;
125                 }
126
127                 spin_unlock(&acpi_device_lock);
128                 acpi_disable_wakeup_device_power(dev);
129                 /* Never disable run-wake GPE */
130                 if (!dev->wakeup.flags.run_wake) {
131                         acpi_disable_gpe(dev->wakeup.gpe_device,
132                                          dev->wakeup.gpe_number);
133                         acpi_clear_gpe(dev->wakeup.gpe_device,
134                                        dev->wakeup.gpe_number, ACPI_NOT_ISR);
135                 }
136                 spin_lock(&acpi_device_lock);
137         }
138         spin_unlock(&acpi_device_lock);
139 }
140
141 int __init acpi_wakeup_device_init(void)
142 {
143         struct list_head *node, *next;
144
145         spin_lock(&acpi_device_lock);
146         list_for_each_safe(node, next, &acpi_wakeup_device_list) {
147                 struct acpi_device *dev = container_of(node,
148                                                        struct acpi_device,
149                                                        wakeup_list);
150                 /* In case user doesn't load button driver */
151                 if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
152                         continue;
153                 spin_unlock(&acpi_device_lock);
154                 acpi_set_gpe_type(dev->wakeup.gpe_device,
155                                   dev->wakeup.gpe_number,
156                                   ACPI_GPE_TYPE_WAKE_RUN);
157                 acpi_enable_gpe(dev->wakeup.gpe_device,
158                                 dev->wakeup.gpe_number);
159                 dev->wakeup.state.enabled = 1;
160                 spin_lock(&acpi_device_lock);
161         }
162         spin_unlock(&acpi_device_lock);
163         return 0;
164 }