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>
26#include <linux/pm_legacy.h>
27#include <linux/interrupt.h>
28#include <linux/mutex.h>
29
30int 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
44static DEFINE_MUTEX(pm_devs_lock);
45static 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
61struct pm_dev *pm_register(pm_dev_t type,
62			   unsigned long id,
63			   pm_callback callback)
64{
65	struct pm_dev *dev = kzalloc(sizeof(struct pm_dev), GFP_KERNEL);
66	if (dev) {
67		dev->type = type;
68		dev->id = id;
69		dev->callback = callback;
70
71		mutex_lock(&pm_devs_lock);
72		list_add(&dev->entry, &pm_devs);
73		mutex_unlock(&pm_devs_lock);
74	}
75	return dev;
76}
77
78/**
79 *	pm_send - send request to a single device
80 *	@dev: device to send to
81 *	@rqst: power management request
82 *	@data: data for the callback
83 *
84 *	Issue a power management request to a given device. The
85 *	%PM_SUSPEND and %PM_RESUME events are handled specially. The
86 *	data field must hold the intended next state. No call is made
87 *	if the state matches.
88 *
89 *	BUGS: what stops two power management requests occurring in parallel
90 *	and conflicting.
91 *
92 *	WARNING: Calling pm_send directly is not generally recommended, in
93 *	particular there is no locking against the pm_dev going away. The
94 *	caller must maintain all needed locking or have 'inside knowledge'
95 *	on the safety. Also remember that this function is not locked against
96 *	pm_unregister. This means that you must handle SMP races on callback
97 *	execution and unload yourself.
98 */
99
100static int pm_send(struct pm_dev *dev, pm_request_t rqst, void *data)
101{
102	int status = 0;
103	unsigned long prev_state, next_state;
104
105	if (in_interrupt())
106		BUG();
107
108	switch (rqst) {
109	case PM_SUSPEND:
110	case PM_RESUME:
111		prev_state = dev->state;
112		next_state = (unsigned long) data;
113		if (prev_state != next_state) {
114			if (dev->callback)
115				status = (*dev->callback)(dev, rqst, data);
116			if (!status) {
117				dev->state = next_state;
118				dev->prev_state = prev_state;
119			}
120		}
121		else {
122			dev->prev_state = prev_state;
123		}
124		break;
125	default:
126		if (dev->callback)
127			status = (*dev->callback)(dev, rqst, data);
128		break;
129	}
130	return status;
131}
132
133/*
134 * Undo incomplete request
135 */
136static void pm_undo_all(struct pm_dev *last)
137{
138	struct list_head *entry = last->entry.prev;
139	while (entry != &pm_devs) {
140		struct pm_dev *dev = list_entry(entry, struct pm_dev, entry);
141		if (dev->state != dev->prev_state) {
142			/* previous state was zero (running) resume or
143			 * previous state was non-zero (suspended) suspend
144			 */
145			pm_request_t undo = (dev->prev_state
146					     ? PM_SUSPEND:PM_RESUME);
147			pm_send(dev, undo, (void*) dev->prev_state);
148		}
149		entry = entry->prev;
150	}
151}
152
153/**
154 *	pm_send_all - send request to all managed devices
155 *	@rqst: power management request
156 *	@data: data for the callback
157 *
158 *	Issue a power management request to a all devices. The
159 *	%PM_SUSPEND events are handled specially. Any device is
160 *	permitted to fail a suspend by returning a non zero (error)
161 *	value from its callback function. If any device vetoes a
162 *	suspend request then all other devices that have suspended
163 *	during the processing of this request are restored to their
164 *	previous state.
165 *
166 *	WARNING:  This function takes the pm_devs_lock. The lock is not dropped until
167 *	the callbacks have completed. This prevents races against pm locking
168 *	functions, races against module unload pm_unregister code. It does
169 *	mean however that you must not issue pm_ functions within the callback
170 *	or you will deadlock and users will hate you.
171 *
172 *	Zero is returned on success. If a suspend fails then the status
173 *	from the device that vetoes the suspend is returned.
174 *
175 *	BUGS: what stops two power management requests occurring in parallel
176 *	and conflicting.
177 */
178
179int pm_send_all(pm_request_t rqst, void *data)
180{
181	struct list_head *entry;
182
183	mutex_lock(&pm_devs_lock);
184	entry = pm_devs.next;
185	while (entry != &pm_devs) {
186		struct pm_dev *dev = list_entry(entry, struct pm_dev, entry);
187		if (dev->callback) {
188			int status = pm_send(dev, rqst, data);
189			if (status) {
190				/* return devices to previous state on
191				 * failed suspend request
192				 */
193				if (rqst == PM_SUSPEND)
194					pm_undo_all(dev);
195				mutex_unlock(&pm_devs_lock);
196				return status;
197			}
198		}
199		entry = entry->next;
200	}
201	mutex_unlock(&pm_devs_lock);
202	return 0;
203}
204
205EXPORT_SYMBOL(pm_register);
206EXPORT_SYMBOL(pm_send_all);
207EXPORT_SYMBOL(pm_active);
208