]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * pm.c - Power management interface | |
3 | * | |
4 | * Copyright (C) 2000 Andrew Henroid | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
20 | #include <linux/init.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/spinlock.h> | |
23 | #include <linux/mm.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/pm.h> | |
bca73e4b | 26 | #include <linux/pm_legacy.h> |
1da177e4 | 27 | #include <linux/interrupt.h> |
97d1f15b | 28 | #include <linux/mutex.h> |
1da177e4 LT |
29 | |
30 | int pm_active; | |
31 | ||
32 | /* | |
33 | * Locking notes: | |
34 | * pm_devs_lock can be a semaphore providing pm ops are not called | |
35 | * from an interrupt handler (already a bad idea so no change here). Each | |
36 | * change must be protected so that an unlink of an entry doesn't clash | |
37 | * with a pm send - which is permitted to sleep in the current architecture | |
38 | * | |
39 | * Module unloads clashing with pm events now work out safely, the module | |
40 | * unload path will block until the event has been sent. It may well block | |
41 | * until a resume but that will be fine. | |
42 | */ | |
43 | ||
97d1f15b | 44 | static DEFINE_MUTEX(pm_devs_lock); |
1da177e4 LT |
45 | static LIST_HEAD(pm_devs); |
46 | ||
47 | /** | |
48 | * pm_register - register a device with power management | |
49 | * @type: device type | |
50 | * @id: device ID | |
51 | * @callback: callback function | |
52 | * | |
53 | * Add a device to the list of devices that wish to be notified about | |
54 | * power management events. A &pm_dev structure is returned on success, | |
55 | * on failure the return is %NULL. | |
56 | * | |
57 | * The callback function will be called in process context and | |
58 | * it may sleep. | |
59 | */ | |
60 | ||
61 | struct pm_dev *pm_register(pm_dev_t type, | |
62 | unsigned long id, | |
63 | pm_callback callback) | |
64 | { | |
dd392710 | 65 | struct pm_dev *dev = kzalloc(sizeof(struct pm_dev), GFP_KERNEL); |
1da177e4 | 66 | if (dev) { |
1da177e4 LT |
67 | dev->type = type; |
68 | dev->id = id; | |
69 | dev->callback = callback; | |
70 | ||
97d1f15b | 71 | mutex_lock(&pm_devs_lock); |
1da177e4 | 72 | list_add(&dev->entry, &pm_devs); |
97d1f15b | 73 | mutex_unlock(&pm_devs_lock); |
1da177e4 LT |
74 | } |
75 | return dev; | |
76 | } | |
77 | ||
1da177e4 LT |
78 | static void __pm_unregister(struct pm_dev *dev) |
79 | { | |
80 | if (dev) { | |
81 | list_del(&dev->entry); | |
82 | kfree(dev); | |
83 | } | |
84 | } | |
85 | ||
86 | /** | |
87 | * pm_unregister_all - unregister all devices with matching callback | |
88 | * @callback: callback function pointer | |
89 | * | |
90 | * Unregister every device that would call the callback passed. This | |
91 | * is primarily meant as a helper function for loadable modules. It | |
92 | * enables a module to give up all its managed devices without keeping | |
93 | * its own private list. | |
94 | */ | |
95 | ||
96 | void pm_unregister_all(pm_callback callback) | |
97 | { | |
98 | struct list_head *entry; | |
99 | ||
100 | if (!callback) | |
101 | return; | |
102 | ||
97d1f15b | 103 | mutex_lock(&pm_devs_lock); |
1da177e4 LT |
104 | entry = pm_devs.next; |
105 | while (entry != &pm_devs) { | |
106 | struct pm_dev *dev = list_entry(entry, struct pm_dev, entry); | |
107 | entry = entry->next; | |
108 | if (dev->callback == callback) | |
109 | __pm_unregister(dev); | |
110 | } | |
97d1f15b | 111 | mutex_unlock(&pm_devs_lock); |
1da177e4 LT |
112 | } |
113 | ||
114 | /** | |
115 | * pm_send - send request to a single device | |
116 | * @dev: device to send to | |
117 | * @rqst: power management request | |
118 | * @data: data for the callback | |
119 | * | |
120 | * Issue a power management request to a given device. The | |
121 | * %PM_SUSPEND and %PM_RESUME events are handled specially. The | |
122 | * data field must hold the intended next state. No call is made | |
123 | * if the state matches. | |
124 | * | |
125 | * BUGS: what stops two power management requests occurring in parallel | |
126 | * and conflicting. | |
127 | * | |
128 | * WARNING: Calling pm_send directly is not generally recommended, in | |
129 | * particular there is no locking against the pm_dev going away. The | |
130 | * caller must maintain all needed locking or have 'inside knowledge' | |
131 | * on the safety. Also remember that this function is not locked against | |
132 | * pm_unregister. This means that you must handle SMP races on callback | |
133 | * execution and unload yourself. | |
134 | */ | |
135 | ||
136 | static int pm_send(struct pm_dev *dev, pm_request_t rqst, void *data) | |
137 | { | |
138 | int status = 0; | |
139 | unsigned long prev_state, next_state; | |
140 | ||
141 | if (in_interrupt()) | |
142 | BUG(); | |
143 | ||
144 | switch (rqst) { | |
145 | case PM_SUSPEND: | |
146 | case PM_RESUME: | |
147 | prev_state = dev->state; | |
148 | next_state = (unsigned long) data; | |
149 | if (prev_state != next_state) { | |
150 | if (dev->callback) | |
151 | status = (*dev->callback)(dev, rqst, data); | |
152 | if (!status) { | |
153 | dev->state = next_state; | |
154 | dev->prev_state = prev_state; | |
155 | } | |
156 | } | |
157 | else { | |
158 | dev->prev_state = prev_state; | |
159 | } | |
160 | break; | |
161 | default: | |
162 | if (dev->callback) | |
163 | status = (*dev->callback)(dev, rqst, data); | |
164 | break; | |
165 | } | |
166 | return status; | |
167 | } | |
168 | ||
169 | /* | |
170 | * Undo incomplete request | |
171 | */ | |
172 | static void pm_undo_all(struct pm_dev *last) | |
173 | { | |
174 | struct list_head *entry = last->entry.prev; | |
175 | while (entry != &pm_devs) { | |
176 | struct pm_dev *dev = list_entry(entry, struct pm_dev, entry); | |
177 | if (dev->state != dev->prev_state) { | |
178 | /* previous state was zero (running) resume or | |
179 | * previous state was non-zero (suspended) suspend | |
180 | */ | |
181 | pm_request_t undo = (dev->prev_state | |
182 | ? PM_SUSPEND:PM_RESUME); | |
183 | pm_send(dev, undo, (void*) dev->prev_state); | |
184 | } | |
185 | entry = entry->prev; | |
186 | } | |
187 | } | |
188 | ||
189 | /** | |
190 | * pm_send_all - send request to all managed devices | |
191 | * @rqst: power management request | |
192 | * @data: data for the callback | |
193 | * | |
194 | * Issue a power management request to a all devices. The | |
195 | * %PM_SUSPEND events are handled specially. Any device is | |
196 | * permitted to fail a suspend by returning a non zero (error) | |
197 | * value from its callback function. If any device vetoes a | |
198 | * suspend request then all other devices that have suspended | |
199 | * during the processing of this request are restored to their | |
200 | * previous state. | |
201 | * | |
202 | * WARNING: This function takes the pm_devs_lock. The lock is not dropped until | |
203 | * the callbacks have completed. This prevents races against pm locking | |
204 | * functions, races against module unload pm_unregister code. It does | |
205 | * mean however that you must not issue pm_ functions within the callback | |
206 | * or you will deadlock and users will hate you. | |
207 | * | |
208 | * Zero is returned on success. If a suspend fails then the status | |
209 | * from the device that vetoes the suspend is returned. | |
210 | * | |
211 | * BUGS: what stops two power management requests occurring in parallel | |
212 | * and conflicting. | |
213 | */ | |
214 | ||
215 | int pm_send_all(pm_request_t rqst, void *data) | |
216 | { | |
217 | struct list_head *entry; | |
218 | ||
97d1f15b | 219 | mutex_lock(&pm_devs_lock); |
1da177e4 LT |
220 | entry = pm_devs.next; |
221 | while (entry != &pm_devs) { | |
222 | struct pm_dev *dev = list_entry(entry, struct pm_dev, entry); | |
223 | if (dev->callback) { | |
224 | int status = pm_send(dev, rqst, data); | |
225 | if (status) { | |
226 | /* return devices to previous state on | |
227 | * failed suspend request | |
228 | */ | |
229 | if (rqst == PM_SUSPEND) | |
230 | pm_undo_all(dev); | |
97d1f15b | 231 | mutex_unlock(&pm_devs_lock); |
1da177e4 LT |
232 | return status; |
233 | } | |
234 | } | |
235 | entry = entry->next; | |
236 | } | |
97d1f15b | 237 | mutex_unlock(&pm_devs_lock); |
1da177e4 LT |
238 | return 0; |
239 | } | |
240 | ||
241 | EXPORT_SYMBOL(pm_register); | |
1da177e4 LT |
242 | EXPORT_SYMBOL(pm_unregister_all); |
243 | EXPORT_SYMBOL(pm_send_all); | |
244 | EXPORT_SYMBOL(pm_active); | |
245 | ||
246 |