2 * pm.c - Power management interface
4 * Copyright (C) 2000 Andrew Henroid
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.
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.
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
21 #include <linux/module.h>
22 #include <linux/spinlock.h>
23 #include <linux/slab.h>
28 static spinlock_t pm_devs_lock
= SPIN_LOCK_UNLOCKED
;
29 static LIST_HEAD(pm_devs
);
32 * pm_register - register a device with power management
35 * @callback: callback function
37 * Add a device to the list of devices that wish to be notified about
38 * power management events. A &pm_dev structure is returned on success,
39 * on failure the return is %NULL.
42 struct pm_dev
*pm_register(pm_dev_t type
,
46 struct pm_dev
*dev
= kmalloc(sizeof(struct pm_dev
), GFP_KERNEL
);
50 memset(dev
, 0, sizeof(*dev
));
53 dev
->callback
= callback
;
55 spin_lock_irqsave(&pm_devs_lock
, flags
);
56 list_add(&dev
->entry
, &pm_devs
);
57 spin_unlock_irqrestore(&pm_devs_lock
, flags
);
63 * pm_unregister - unregister a device with power management
64 * @dev: device to unregister
66 * Remove a device from the power management notification lists. The
67 * dev passed must be a handle previously returned by pm_register.
70 void pm_unregister(struct pm_dev
*dev
)
75 spin_lock_irqsave(&pm_devs_lock
, flags
);
76 list_del(&dev
->entry
);
77 spin_unlock_irqrestore(&pm_devs_lock
, flags
);
84 * pm_unregister_all - unregister all devices with matching callback
85 * @callback: callback function pointer
87 * Unregister every device that would call the callback passed. This
88 * is primarily meant as a helper function for loadable modules. It
89 * enables a module to give up all its managed devices without keeping
90 * its own private list.
93 void pm_unregister_all(pm_callback callback
)
95 struct list_head
*entry
;
100 entry
= pm_devs
.next
;
101 while (entry
!= &pm_devs
) {
102 struct pm_dev
*dev
= list_entry(entry
, struct pm_dev
, entry
);
104 if (dev
->callback
== callback
)
110 * pm_send - send request to a single device
111 * @dev: device to send to
112 * @rqst: power management request
113 * @data: data for the callback
115 * Issue a power management request to a given device. The
116 * %PM_SUSPEND and %PM_RESUME events are handled specially. The
117 * data field must hold the intended next state. No call is made
118 * if the state matches.
120 * BUGS: what stops two power management requests occuring in parallel
124 int pm_send(struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
127 int prev_state
, next_state
;
131 prev_state
= dev
->state
;
132 next_state
= (int) data
;
133 if (prev_state
!= next_state
) {
135 status
= (*dev
->callback
)(dev
, rqst
, data
);
137 dev
->state
= next_state
;
138 dev
->prev_state
= prev_state
;
142 dev
->prev_state
= prev_state
;
147 status
= (*dev
->callback
)(dev
, rqst
, data
);
154 * Undo incomplete request
156 static void pm_undo_all(struct pm_dev
*last
)
158 struct list_head
*entry
= last
->entry
.prev
;
159 while (entry
!= &pm_devs
) {
160 struct pm_dev
*dev
= list_entry(entry
, struct pm_dev
, entry
);
161 if (dev
->state
!= dev
->prev_state
) {
162 /* previous state was zero (running) resume or
163 * previous state was non-zero (suspended) suspend
165 pm_request_t undo
= (dev
->prev_state
166 ? PM_SUSPEND
:PM_RESUME
);
167 pm_send(dev
, undo
, (void*) dev
->prev_state
);
174 * pm_send_all - send request to all managed devices
175 * @rqst: power management request
176 * @data: data for the callback
178 * Issue a power management request to a all devices. The
179 * %PM_SUSPEND events are handled specially. Any device is
180 * permitted to fail a suspend by returning a non zero (error)
181 * value from its callback function. If any device vetoes a
182 * suspend request then all other devices that have suspended
183 * during the processing of this request are restored to their
186 * Zero is returned on success. If a suspend fails then the status
187 * from the device that vetoes the suspend is returned.
189 * BUGS: what stops two power management requests occuring in parallel
193 int pm_send_all(pm_request_t rqst
, void *data
)
195 struct list_head
*entry
= pm_devs
.next
;
196 while (entry
!= &pm_devs
) {
197 struct pm_dev
*dev
= list_entry(entry
, struct pm_dev
, entry
);
199 int status
= pm_send(dev
, rqst
, data
);
201 /* return devices to previous state on
202 * failed suspend request
204 if (rqst
== PM_SUSPEND
)
215 * pm_find - find a device
216 * @type: type of device
217 * @from: where to start looking
219 * Scan the power management list for devices of a specific type. The
220 * return value for a matching device may be passed to further calls
221 * to this function to find further matches. A %NULL indicates the end
224 * To search from the beginning pass %NULL as the @from value.
227 struct pm_dev
*pm_find(pm_dev_t type
, struct pm_dev
*from
)
229 struct list_head
*entry
= from
? from
->entry
.next
:pm_devs
.next
;
230 while (entry
!= &pm_devs
) {
231 struct pm_dev
*dev
= list_entry(entry
, struct pm_dev
, entry
);
232 if (type
== PM_UNKNOWN_DEV
|| dev
->type
== type
)
239 EXPORT_SYMBOL(pm_register
);
240 EXPORT_SYMBOL(pm_unregister
);
241 EXPORT_SYMBOL(pm_unregister_all
);
242 EXPORT_SYMBOL(pm_send
);
243 EXPORT_SYMBOL(pm_send_all
);
244 EXPORT_SYMBOL(pm_find
);
245 EXPORT_SYMBOL(pm_active
);