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
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/spinlock.h>
24 #include <linux/slab.h>
26 #include <linux/pm_legacy.h>
27 #include <linux/interrupt.h>
28 #include <linux/mutex.h>
32 * pm_devs_lock can be a semaphore providing pm ops are not called
33 * from an interrupt handler (already a bad idea so no change here). Each
34 * change must be protected so that an unlink of an entry doesn't clash
35 * with a pm send - which is permitted to sleep in the current architecture
37 * Module unloads clashing with pm events now work out safely, the module
38 * unload path will block until the event has been sent. It may well block
39 * until a resume but that will be fine.
42 static DEFINE_MUTEX(pm_devs_lock
);
43 static LIST_HEAD(pm_devs
);
46 * pm_register - register a device with power management
49 * @callback: callback function
51 * Add a device to the list of devices that wish to be notified about
52 * power management events. A &pm_dev structure is returned on success,
53 * on failure the return is %NULL.
55 * The callback function will be called in process context and
59 struct pm_dev
*pm_register(pm_dev_t type
,
63 struct pm_dev
*dev
= kzalloc(sizeof(struct pm_dev
), GFP_KERNEL
);
67 dev
->callback
= callback
;
69 mutex_lock(&pm_devs_lock
);
70 list_add(&dev
->entry
, &pm_devs
);
71 mutex_unlock(&pm_devs_lock
);
77 * pm_send - send request to a single device
78 * @dev: device to send to
79 * @rqst: power management request
80 * @data: data for the callback
82 * Issue a power management request to a given device. The
83 * %PM_SUSPEND and %PM_RESUME events are handled specially. The
84 * data field must hold the intended next state. No call is made
85 * if the state matches.
87 * BUGS: what stops two power management requests occurring in parallel
90 * WARNING: Calling pm_send directly is not generally recommended, in
91 * particular there is no locking against the pm_dev going away. The
92 * caller must maintain all needed locking or have 'inside knowledge'
93 * on the safety. Also remember that this function is not locked against
94 * pm_unregister. This means that you must handle SMP races on callback
95 * execution and unload yourself.
98 static int pm_send(struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
101 unsigned long prev_state
, next_state
;
109 prev_state
= dev
->state
;
110 next_state
= (unsigned long) data
;
111 if (prev_state
!= next_state
) {
113 status
= (*dev
->callback
)(dev
, rqst
, data
);
115 dev
->state
= next_state
;
116 dev
->prev_state
= prev_state
;
120 dev
->prev_state
= prev_state
;
125 status
= (*dev
->callback
)(dev
, rqst
, data
);
132 * Undo incomplete request
134 static void pm_undo_all(struct pm_dev
*last
)
136 struct list_head
*entry
= last
->entry
.prev
;
137 while (entry
!= &pm_devs
) {
138 struct pm_dev
*dev
= list_entry(entry
, struct pm_dev
, entry
);
139 if (dev
->state
!= dev
->prev_state
) {
140 /* previous state was zero (running) resume or
141 * previous state was non-zero (suspended) suspend
143 pm_request_t undo
= (dev
->prev_state
144 ? PM_SUSPEND
:PM_RESUME
);
145 pm_send(dev
, undo
, (void*) dev
->prev_state
);
152 * pm_send_all - send request to all managed devices
153 * @rqst: power management request
154 * @data: data for the callback
156 * Issue a power management request to a all devices. The
157 * %PM_SUSPEND events are handled specially. Any device is
158 * permitted to fail a suspend by returning a non zero (error)
159 * value from its callback function. If any device vetoes a
160 * suspend request then all other devices that have suspended
161 * during the processing of this request are restored to their
164 * WARNING: This function takes the pm_devs_lock. The lock is not dropped until
165 * the callbacks have completed. This prevents races against pm locking
166 * functions, races against module unload pm_unregister code. It does
167 * mean however that you must not issue pm_ functions within the callback
168 * or you will deadlock and users will hate you.
170 * Zero is returned on success. If a suspend fails then the status
171 * from the device that vetoes the suspend is returned.
173 * BUGS: what stops two power management requests occurring in parallel
177 int pm_send_all(pm_request_t rqst
, void *data
)
179 struct list_head
*entry
;
181 mutex_lock(&pm_devs_lock
);
182 entry
= pm_devs
.next
;
183 while (entry
!= &pm_devs
) {
184 struct pm_dev
*dev
= list_entry(entry
, struct pm_dev
, entry
);
186 int status
= pm_send(dev
, rqst
, data
);
188 /* return devices to previous state on
189 * failed suspend request
191 if (rqst
== PM_SUSPEND
)
193 mutex_unlock(&pm_devs_lock
);
199 mutex_unlock(&pm_devs_lock
);
203 EXPORT_SYMBOL(pm_register
);
204 EXPORT_SYMBOL(pm_send_all
);