Commit fda5fe19725edd6805f2fd742235d1a0d0ba93f5

Authored by Paul Parsons
Committed by Jiri Kosina
1 parent 81024fc41a

apm-emulation: apm_mutex breaks ACK; remove it

apm_mutex is locked by a process (e.g. apm -s) at the start of apm_ioctl() and
remains locked while pm_suspend() is called. Any subsequent process trying to
ACK the suspend (e.g. apmd) is then blocked at the start of apm_ioctl(),
causing the suspend to be delayed for 5 seconds in apm_suspend_notifier()
while the ACK times out. In short, ACKs don't work.

The driver's data structures are sufficiently protected by assorted locks. And
pm_suspend() has its own mutex to prevent reentrancy. Consequently there is no
obvious requirement for apm_mutex, which evolved from earlier BKL calls. So
let's remove it.

Signed-off-by: Paul Parsons <lost.distance@yahoo.com>
Acked-by: Rafael J. Wysocki <rjw@sisk.pl>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>

Showing 1 changed file with 0 additions and 5 deletions Inline Diff

drivers/char/apm-emulation.c
1 /* 1 /*
2 * bios-less APM driver for ARM Linux 2 * bios-less APM driver for ARM Linux
3 * Jamey Hicks <jamey@crl.dec.com> 3 * Jamey Hicks <jamey@crl.dec.com>
4 * adapted from the APM BIOS driver for Linux by Stephen Rothwell (sfr@linuxcare.com) 4 * adapted from the APM BIOS driver for Linux by Stephen Rothwell (sfr@linuxcare.com)
5 * 5 *
6 * APM 1.2 Reference: 6 * APM 1.2 Reference:
7 * Intel Corporation, Microsoft Corporation. Advanced Power Management 7 * Intel Corporation, Microsoft Corporation. Advanced Power Management
8 * (APM) BIOS Interface Specification, Revision 1.2, February 1996. 8 * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
9 * 9 *
10 * This document is available from Microsoft at: 10 * This document is available from Microsoft at:
11 * http://www.microsoft.com/whdc/archive/amp_12.mspx 11 * http://www.microsoft.com/whdc/archive/amp_12.mspx
12 */ 12 */
13 #include <linux/module.h> 13 #include <linux/module.h>
14 #include <linux/poll.h> 14 #include <linux/poll.h>
15 #include <linux/slab.h> 15 #include <linux/slab.h>
16 #include <linux/mutex.h> 16 #include <linux/mutex.h>
17 #include <linux/proc_fs.h> 17 #include <linux/proc_fs.h>
18 #include <linux/seq_file.h> 18 #include <linux/seq_file.h>
19 #include <linux/miscdevice.h> 19 #include <linux/miscdevice.h>
20 #include <linux/apm_bios.h> 20 #include <linux/apm_bios.h>
21 #include <linux/capability.h> 21 #include <linux/capability.h>
22 #include <linux/sched.h> 22 #include <linux/sched.h>
23 #include <linux/suspend.h> 23 #include <linux/suspend.h>
24 #include <linux/apm-emulation.h> 24 #include <linux/apm-emulation.h>
25 #include <linux/freezer.h> 25 #include <linux/freezer.h>
26 #include <linux/device.h> 26 #include <linux/device.h>
27 #include <linux/kernel.h> 27 #include <linux/kernel.h>
28 #include <linux/list.h> 28 #include <linux/list.h>
29 #include <linux/init.h> 29 #include <linux/init.h>
30 #include <linux/completion.h> 30 #include <linux/completion.h>
31 #include <linux/kthread.h> 31 #include <linux/kthread.h>
32 #include <linux/delay.h> 32 #include <linux/delay.h>
33 33
34 #include <asm/system.h> 34 #include <asm/system.h>
35 35
36 /* 36 /*
37 * The apm_bios device is one of the misc char devices. 37 * The apm_bios device is one of the misc char devices.
38 * This is its minor number. 38 * This is its minor number.
39 */ 39 */
40 #define APM_MINOR_DEV 134 40 #define APM_MINOR_DEV 134
41 41
42 /* 42 /*
43 * See Documentation/Config.help for the configuration options. 43 * See Documentation/Config.help for the configuration options.
44 * 44 *
45 * Various options can be changed at boot time as follows: 45 * Various options can be changed at boot time as follows:
46 * (We allow underscores for compatibility with the modules code) 46 * (We allow underscores for compatibility with the modules code)
47 * apm=on/off enable/disable APM 47 * apm=on/off enable/disable APM
48 */ 48 */
49 49
50 /* 50 /*
51 * Maximum number of events stored 51 * Maximum number of events stored
52 */ 52 */
53 #define APM_MAX_EVENTS 16 53 #define APM_MAX_EVENTS 16
54 54
55 struct apm_queue { 55 struct apm_queue {
56 unsigned int event_head; 56 unsigned int event_head;
57 unsigned int event_tail; 57 unsigned int event_tail;
58 apm_event_t events[APM_MAX_EVENTS]; 58 apm_event_t events[APM_MAX_EVENTS];
59 }; 59 };
60 60
61 /* 61 /*
62 * thread states (for threads using a writable /dev/apm_bios fd): 62 * thread states (for threads using a writable /dev/apm_bios fd):
63 * 63 *
64 * SUSPEND_NONE: nothing happening 64 * SUSPEND_NONE: nothing happening
65 * SUSPEND_PENDING: suspend event queued for thread and pending to be read 65 * SUSPEND_PENDING: suspend event queued for thread and pending to be read
66 * SUSPEND_READ: suspend event read, pending acknowledgement 66 * SUSPEND_READ: suspend event read, pending acknowledgement
67 * SUSPEND_ACKED: acknowledgement received from thread (via ioctl), 67 * SUSPEND_ACKED: acknowledgement received from thread (via ioctl),
68 * waiting for resume 68 * waiting for resume
69 * SUSPEND_ACKTO: acknowledgement timeout 69 * SUSPEND_ACKTO: acknowledgement timeout
70 * SUSPEND_DONE: thread had acked suspend and is now notified of 70 * SUSPEND_DONE: thread had acked suspend and is now notified of
71 * resume 71 * resume
72 * 72 *
73 * SUSPEND_WAIT: this thread invoked suspend and is waiting for resume 73 * SUSPEND_WAIT: this thread invoked suspend and is waiting for resume
74 * 74 *
75 * A thread migrates in one of three paths: 75 * A thread migrates in one of three paths:
76 * NONE -1-> PENDING -2-> READ -3-> ACKED -4-> DONE -5-> NONE 76 * NONE -1-> PENDING -2-> READ -3-> ACKED -4-> DONE -5-> NONE
77 * -6-> ACKTO -7-> NONE 77 * -6-> ACKTO -7-> NONE
78 * NONE -8-> WAIT -9-> NONE 78 * NONE -8-> WAIT -9-> NONE
79 * 79 *
80 * While in PENDING or READ, the thread is accounted for in the 80 * While in PENDING or READ, the thread is accounted for in the
81 * suspend_acks_pending counter. 81 * suspend_acks_pending counter.
82 * 82 *
83 * The transitions are invoked as follows: 83 * The transitions are invoked as follows:
84 * 1: suspend event is signalled from the core PM code 84 * 1: suspend event is signalled from the core PM code
85 * 2: the suspend event is read from the fd by the userspace thread 85 * 2: the suspend event is read from the fd by the userspace thread
86 * 3: userspace thread issues the APM_IOC_SUSPEND ioctl (as ack) 86 * 3: userspace thread issues the APM_IOC_SUSPEND ioctl (as ack)
87 * 4: core PM code signals that we have resumed 87 * 4: core PM code signals that we have resumed
88 * 5: APM_IOC_SUSPEND ioctl returns 88 * 5: APM_IOC_SUSPEND ioctl returns
89 * 89 *
90 * 6: the notifier invoked from the core PM code timed out waiting 90 * 6: the notifier invoked from the core PM code timed out waiting
91 * for all relevant threds to enter ACKED state and puts those 91 * for all relevant threds to enter ACKED state and puts those
92 * that haven't into ACKTO 92 * that haven't into ACKTO
93 * 7: those threads issue APM_IOC_SUSPEND ioctl too late, 93 * 7: those threads issue APM_IOC_SUSPEND ioctl too late,
94 * get an error 94 * get an error
95 * 95 *
96 * 8: userspace thread issues the APM_IOC_SUSPEND ioctl (to suspend), 96 * 8: userspace thread issues the APM_IOC_SUSPEND ioctl (to suspend),
97 * ioctl code invokes pm_suspend() 97 * ioctl code invokes pm_suspend()
98 * 9: pm_suspend() returns indicating resume 98 * 9: pm_suspend() returns indicating resume
99 */ 99 */
100 enum apm_suspend_state { 100 enum apm_suspend_state {
101 SUSPEND_NONE, 101 SUSPEND_NONE,
102 SUSPEND_PENDING, 102 SUSPEND_PENDING,
103 SUSPEND_READ, 103 SUSPEND_READ,
104 SUSPEND_ACKED, 104 SUSPEND_ACKED,
105 SUSPEND_ACKTO, 105 SUSPEND_ACKTO,
106 SUSPEND_WAIT, 106 SUSPEND_WAIT,
107 SUSPEND_DONE, 107 SUSPEND_DONE,
108 }; 108 };
109 109
110 /* 110 /*
111 * The per-file APM data 111 * The per-file APM data
112 */ 112 */
113 struct apm_user { 113 struct apm_user {
114 struct list_head list; 114 struct list_head list;
115 115
116 unsigned int suser: 1; 116 unsigned int suser: 1;
117 unsigned int writer: 1; 117 unsigned int writer: 1;
118 unsigned int reader: 1; 118 unsigned int reader: 1;
119 119
120 int suspend_result; 120 int suspend_result;
121 enum apm_suspend_state suspend_state; 121 enum apm_suspend_state suspend_state;
122 122
123 struct apm_queue queue; 123 struct apm_queue queue;
124 }; 124 };
125 125
126 /* 126 /*
127 * Local variables 127 * Local variables
128 */ 128 */
129 static DEFINE_MUTEX(apm_mutex);
130 static atomic_t suspend_acks_pending = ATOMIC_INIT(0); 129 static atomic_t suspend_acks_pending = ATOMIC_INIT(0);
131 static atomic_t userspace_notification_inhibit = ATOMIC_INIT(0); 130 static atomic_t userspace_notification_inhibit = ATOMIC_INIT(0);
132 static int apm_disabled; 131 static int apm_disabled;
133 static struct task_struct *kapmd_tsk; 132 static struct task_struct *kapmd_tsk;
134 133
135 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue); 134 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
136 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue); 135 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
137 136
138 /* 137 /*
139 * This is a list of everyone who has opened /dev/apm_bios 138 * This is a list of everyone who has opened /dev/apm_bios
140 */ 139 */
141 static DECLARE_RWSEM(user_list_lock); 140 static DECLARE_RWSEM(user_list_lock);
142 static LIST_HEAD(apm_user_list); 141 static LIST_HEAD(apm_user_list);
143 142
144 /* 143 /*
145 * kapmd info. kapmd provides us a process context to handle 144 * kapmd info. kapmd provides us a process context to handle
146 * "APM" events within - specifically necessary if we're going 145 * "APM" events within - specifically necessary if we're going
147 * to be suspending the system. 146 * to be suspending the system.
148 */ 147 */
149 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait); 148 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
150 static DEFINE_SPINLOCK(kapmd_queue_lock); 149 static DEFINE_SPINLOCK(kapmd_queue_lock);
151 static struct apm_queue kapmd_queue; 150 static struct apm_queue kapmd_queue;
152 151
153 static DEFINE_MUTEX(state_lock); 152 static DEFINE_MUTEX(state_lock);
154 153
155 static const char driver_version[] = "1.13"; /* no spaces */ 154 static const char driver_version[] = "1.13"; /* no spaces */
156 155
157 156
158 157
159 /* 158 /*
160 * Compatibility cruft until the IPAQ people move over to the new 159 * Compatibility cruft until the IPAQ people move over to the new
161 * interface. 160 * interface.
162 */ 161 */
163 static void __apm_get_power_status(struct apm_power_info *info) 162 static void __apm_get_power_status(struct apm_power_info *info)
164 { 163 {
165 } 164 }
166 165
167 /* 166 /*
168 * This allows machines to provide their own "apm get power status" function. 167 * This allows machines to provide their own "apm get power status" function.
169 */ 168 */
170 void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status; 169 void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
171 EXPORT_SYMBOL(apm_get_power_status); 170 EXPORT_SYMBOL(apm_get_power_status);
172 171
173 172
174 /* 173 /*
175 * APM event queue management. 174 * APM event queue management.
176 */ 175 */
177 static inline int queue_empty(struct apm_queue *q) 176 static inline int queue_empty(struct apm_queue *q)
178 { 177 {
179 return q->event_head == q->event_tail; 178 return q->event_head == q->event_tail;
180 } 179 }
181 180
182 static inline apm_event_t queue_get_event(struct apm_queue *q) 181 static inline apm_event_t queue_get_event(struct apm_queue *q)
183 { 182 {
184 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS; 183 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
185 return q->events[q->event_tail]; 184 return q->events[q->event_tail];
186 } 185 }
187 186
188 static void queue_add_event(struct apm_queue *q, apm_event_t event) 187 static void queue_add_event(struct apm_queue *q, apm_event_t event)
189 { 188 {
190 q->event_head = (q->event_head + 1) % APM_MAX_EVENTS; 189 q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
191 if (q->event_head == q->event_tail) { 190 if (q->event_head == q->event_tail) {
192 static int notified; 191 static int notified;
193 192
194 if (notified++ == 0) 193 if (notified++ == 0)
195 printk(KERN_ERR "apm: an event queue overflowed\n"); 194 printk(KERN_ERR "apm: an event queue overflowed\n");
196 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS; 195 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
197 } 196 }
198 q->events[q->event_head] = event; 197 q->events[q->event_head] = event;
199 } 198 }
200 199
201 static void queue_event(apm_event_t event) 200 static void queue_event(apm_event_t event)
202 { 201 {
203 struct apm_user *as; 202 struct apm_user *as;
204 203
205 down_read(&user_list_lock); 204 down_read(&user_list_lock);
206 list_for_each_entry(as, &apm_user_list, list) { 205 list_for_each_entry(as, &apm_user_list, list) {
207 if (as->reader) 206 if (as->reader)
208 queue_add_event(&as->queue, event); 207 queue_add_event(&as->queue, event);
209 } 208 }
210 up_read(&user_list_lock); 209 up_read(&user_list_lock);
211 wake_up_interruptible(&apm_waitqueue); 210 wake_up_interruptible(&apm_waitqueue);
212 } 211 }
213 212
214 static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos) 213 static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
215 { 214 {
216 struct apm_user *as = fp->private_data; 215 struct apm_user *as = fp->private_data;
217 apm_event_t event; 216 apm_event_t event;
218 int i = count, ret = 0; 217 int i = count, ret = 0;
219 218
220 if (count < sizeof(apm_event_t)) 219 if (count < sizeof(apm_event_t))
221 return -EINVAL; 220 return -EINVAL;
222 221
223 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK) 222 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
224 return -EAGAIN; 223 return -EAGAIN;
225 224
226 wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue)); 225 wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
227 226
228 while ((i >= sizeof(event)) && !queue_empty(&as->queue)) { 227 while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
229 event = queue_get_event(&as->queue); 228 event = queue_get_event(&as->queue);
230 229
231 ret = -EFAULT; 230 ret = -EFAULT;
232 if (copy_to_user(buf, &event, sizeof(event))) 231 if (copy_to_user(buf, &event, sizeof(event)))
233 break; 232 break;
234 233
235 mutex_lock(&state_lock); 234 mutex_lock(&state_lock);
236 if (as->suspend_state == SUSPEND_PENDING && 235 if (as->suspend_state == SUSPEND_PENDING &&
237 (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND)) 236 (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND))
238 as->suspend_state = SUSPEND_READ; 237 as->suspend_state = SUSPEND_READ;
239 mutex_unlock(&state_lock); 238 mutex_unlock(&state_lock);
240 239
241 buf += sizeof(event); 240 buf += sizeof(event);
242 i -= sizeof(event); 241 i -= sizeof(event);
243 } 242 }
244 243
245 if (i < count) 244 if (i < count)
246 ret = count - i; 245 ret = count - i;
247 246
248 return ret; 247 return ret;
249 } 248 }
250 249
251 static unsigned int apm_poll(struct file *fp, poll_table * wait) 250 static unsigned int apm_poll(struct file *fp, poll_table * wait)
252 { 251 {
253 struct apm_user *as = fp->private_data; 252 struct apm_user *as = fp->private_data;
254 253
255 poll_wait(fp, &apm_waitqueue, wait); 254 poll_wait(fp, &apm_waitqueue, wait);
256 return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM; 255 return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;
257 } 256 }
258 257
259 /* 258 /*
260 * apm_ioctl - handle APM ioctl 259 * apm_ioctl - handle APM ioctl
261 * 260 *
262 * APM_IOC_SUSPEND 261 * APM_IOC_SUSPEND
263 * This IOCTL is overloaded, and performs two functions. It is used to: 262 * This IOCTL is overloaded, and performs two functions. It is used to:
264 * - initiate a suspend 263 * - initiate a suspend
265 * - acknowledge a suspend read from /dev/apm_bios. 264 * - acknowledge a suspend read from /dev/apm_bios.
266 * Only when everyone who has opened /dev/apm_bios with write permission 265 * Only when everyone who has opened /dev/apm_bios with write permission
267 * has acknowledge does the actual suspend happen. 266 * has acknowledge does the actual suspend happen.
268 */ 267 */
269 static long 268 static long
270 apm_ioctl(struct file *filp, u_int cmd, u_long arg) 269 apm_ioctl(struct file *filp, u_int cmd, u_long arg)
271 { 270 {
272 struct apm_user *as = filp->private_data; 271 struct apm_user *as = filp->private_data;
273 int err = -EINVAL; 272 int err = -EINVAL;
274 273
275 if (!as->suser || !as->writer) 274 if (!as->suser || !as->writer)
276 return -EPERM; 275 return -EPERM;
277 276
278 mutex_lock(&apm_mutex);
279 switch (cmd) { 277 switch (cmd) {
280 case APM_IOC_SUSPEND: 278 case APM_IOC_SUSPEND:
281 mutex_lock(&state_lock); 279 mutex_lock(&state_lock);
282 280
283 as->suspend_result = -EINTR; 281 as->suspend_result = -EINTR;
284 282
285 switch (as->suspend_state) { 283 switch (as->suspend_state) {
286 case SUSPEND_READ: 284 case SUSPEND_READ:
287 /* 285 /*
288 * If we read a suspend command from /dev/apm_bios, 286 * If we read a suspend command from /dev/apm_bios,
289 * then the corresponding APM_IOC_SUSPEND ioctl is 287 * then the corresponding APM_IOC_SUSPEND ioctl is
290 * interpreted as an acknowledge. 288 * interpreted as an acknowledge.
291 */ 289 */
292 as->suspend_state = SUSPEND_ACKED; 290 as->suspend_state = SUSPEND_ACKED;
293 atomic_dec(&suspend_acks_pending); 291 atomic_dec(&suspend_acks_pending);
294 mutex_unlock(&state_lock); 292 mutex_unlock(&state_lock);
295 293
296 /* 294 /*
297 * suspend_acks_pending changed, the notifier needs to 295 * suspend_acks_pending changed, the notifier needs to
298 * be woken up for this 296 * be woken up for this
299 */ 297 */
300 wake_up(&apm_suspend_waitqueue); 298 wake_up(&apm_suspend_waitqueue);
301 299
302 /* 300 /*
303 * Wait for the suspend/resume to complete. If there 301 * Wait for the suspend/resume to complete. If there
304 * are pending acknowledges, we wait here for them. 302 * are pending acknowledges, we wait here for them.
305 */ 303 */
306 freezer_do_not_count(); 304 freezer_do_not_count();
307 305
308 wait_event(apm_suspend_waitqueue, 306 wait_event(apm_suspend_waitqueue,
309 as->suspend_state == SUSPEND_DONE); 307 as->suspend_state == SUSPEND_DONE);
310 308
311 /* 309 /*
312 * Since we are waiting until the suspend is done, the 310 * Since we are waiting until the suspend is done, the
313 * try_to_freeze() in freezer_count() will not trigger 311 * try_to_freeze() in freezer_count() will not trigger
314 */ 312 */
315 freezer_count(); 313 freezer_count();
316 break; 314 break;
317 case SUSPEND_ACKTO: 315 case SUSPEND_ACKTO:
318 as->suspend_result = -ETIMEDOUT; 316 as->suspend_result = -ETIMEDOUT;
319 mutex_unlock(&state_lock); 317 mutex_unlock(&state_lock);
320 break; 318 break;
321 default: 319 default:
322 as->suspend_state = SUSPEND_WAIT; 320 as->suspend_state = SUSPEND_WAIT;
323 mutex_unlock(&state_lock); 321 mutex_unlock(&state_lock);
324 322
325 /* 323 /*
326 * Otherwise it is a request to suspend the system. 324 * Otherwise it is a request to suspend the system.
327 * Just invoke pm_suspend(), we'll handle it from 325 * Just invoke pm_suspend(), we'll handle it from
328 * there via the notifier. 326 * there via the notifier.
329 */ 327 */
330 as->suspend_result = pm_suspend(PM_SUSPEND_MEM); 328 as->suspend_result = pm_suspend(PM_SUSPEND_MEM);
331 } 329 }
332 330
333 mutex_lock(&state_lock); 331 mutex_lock(&state_lock);
334 err = as->suspend_result; 332 err = as->suspend_result;
335 as->suspend_state = SUSPEND_NONE; 333 as->suspend_state = SUSPEND_NONE;
336 mutex_unlock(&state_lock); 334 mutex_unlock(&state_lock);
337 break; 335 break;
338 } 336 }
339 mutex_unlock(&apm_mutex);
340 337
341 return err; 338 return err;
342 } 339 }
343 340
344 static int apm_release(struct inode * inode, struct file * filp) 341 static int apm_release(struct inode * inode, struct file * filp)
345 { 342 {
346 struct apm_user *as = filp->private_data; 343 struct apm_user *as = filp->private_data;
347 344
348 filp->private_data = NULL; 345 filp->private_data = NULL;
349 346
350 down_write(&user_list_lock); 347 down_write(&user_list_lock);
351 list_del(&as->list); 348 list_del(&as->list);
352 up_write(&user_list_lock); 349 up_write(&user_list_lock);
353 350
354 /* 351 /*
355 * We are now unhooked from the chain. As far as new 352 * We are now unhooked from the chain. As far as new
356 * events are concerned, we no longer exist. 353 * events are concerned, we no longer exist.
357 */ 354 */
358 mutex_lock(&state_lock); 355 mutex_lock(&state_lock);
359 if (as->suspend_state == SUSPEND_PENDING || 356 if (as->suspend_state == SUSPEND_PENDING ||
360 as->suspend_state == SUSPEND_READ) 357 as->suspend_state == SUSPEND_READ)
361 atomic_dec(&suspend_acks_pending); 358 atomic_dec(&suspend_acks_pending);
362 mutex_unlock(&state_lock); 359 mutex_unlock(&state_lock);
363 360
364 wake_up(&apm_suspend_waitqueue); 361 wake_up(&apm_suspend_waitqueue);
365 362
366 kfree(as); 363 kfree(as);
367 return 0; 364 return 0;
368 } 365 }
369 366
370 static int apm_open(struct inode * inode, struct file * filp) 367 static int apm_open(struct inode * inode, struct file * filp)
371 { 368 {
372 struct apm_user *as; 369 struct apm_user *as;
373 370
374 mutex_lock(&apm_mutex);
375 as = kzalloc(sizeof(*as), GFP_KERNEL); 371 as = kzalloc(sizeof(*as), GFP_KERNEL);
376 if (as) { 372 if (as) {
377 /* 373 /*
378 * XXX - this is a tiny bit broken, when we consider BSD 374 * XXX - this is a tiny bit broken, when we consider BSD
379 * process accounting. If the device is opened by root, we 375 * process accounting. If the device is opened by root, we
380 * instantly flag that we used superuser privs. Who knows, 376 * instantly flag that we used superuser privs. Who knows,
381 * we might close the device immediately without doing a 377 * we might close the device immediately without doing a
382 * privileged operation -- cevans 378 * privileged operation -- cevans
383 */ 379 */
384 as->suser = capable(CAP_SYS_ADMIN); 380 as->suser = capable(CAP_SYS_ADMIN);
385 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE; 381 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
386 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ; 382 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
387 383
388 down_write(&user_list_lock); 384 down_write(&user_list_lock);
389 list_add(&as->list, &apm_user_list); 385 list_add(&as->list, &apm_user_list);
390 up_write(&user_list_lock); 386 up_write(&user_list_lock);
391 387
392 filp->private_data = as; 388 filp->private_data = as;
393 } 389 }
394 mutex_unlock(&apm_mutex);
395 390
396 return as ? 0 : -ENOMEM; 391 return as ? 0 : -ENOMEM;
397 } 392 }
398 393
399 static const struct file_operations apm_bios_fops = { 394 static const struct file_operations apm_bios_fops = {
400 .owner = THIS_MODULE, 395 .owner = THIS_MODULE,
401 .read = apm_read, 396 .read = apm_read,
402 .poll = apm_poll, 397 .poll = apm_poll,
403 .unlocked_ioctl = apm_ioctl, 398 .unlocked_ioctl = apm_ioctl,
404 .open = apm_open, 399 .open = apm_open,
405 .release = apm_release, 400 .release = apm_release,
406 .llseek = noop_llseek, 401 .llseek = noop_llseek,
407 }; 402 };
408 403
409 static struct miscdevice apm_device = { 404 static struct miscdevice apm_device = {
410 .minor = APM_MINOR_DEV, 405 .minor = APM_MINOR_DEV,
411 .name = "apm_bios", 406 .name = "apm_bios",
412 .fops = &apm_bios_fops 407 .fops = &apm_bios_fops
413 }; 408 };
414 409
415 410
416 #ifdef CONFIG_PROC_FS 411 #ifdef CONFIG_PROC_FS
417 /* 412 /*
418 * Arguments, with symbols from linux/apm_bios.h. 413 * Arguments, with symbols from linux/apm_bios.h.
419 * 414 *
420 * 0) Linux driver version (this will change if format changes) 415 * 0) Linux driver version (this will change if format changes)
421 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2. 416 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
422 * 2) APM flags from APM Installation Check (0x00): 417 * 2) APM flags from APM Installation Check (0x00):
423 * bit 0: APM_16_BIT_SUPPORT 418 * bit 0: APM_16_BIT_SUPPORT
424 * bit 1: APM_32_BIT_SUPPORT 419 * bit 1: APM_32_BIT_SUPPORT
425 * bit 2: APM_IDLE_SLOWS_CLOCK 420 * bit 2: APM_IDLE_SLOWS_CLOCK
426 * bit 3: APM_BIOS_DISABLED 421 * bit 3: APM_BIOS_DISABLED
427 * bit 4: APM_BIOS_DISENGAGED 422 * bit 4: APM_BIOS_DISENGAGED
428 * 3) AC line status 423 * 3) AC line status
429 * 0x00: Off-line 424 * 0x00: Off-line
430 * 0x01: On-line 425 * 0x01: On-line
431 * 0x02: On backup power (BIOS >= 1.1 only) 426 * 0x02: On backup power (BIOS >= 1.1 only)
432 * 0xff: Unknown 427 * 0xff: Unknown
433 * 4) Battery status 428 * 4) Battery status
434 * 0x00: High 429 * 0x00: High
435 * 0x01: Low 430 * 0x01: Low
436 * 0x02: Critical 431 * 0x02: Critical
437 * 0x03: Charging 432 * 0x03: Charging
438 * 0x04: Selected battery not present (BIOS >= 1.2 only) 433 * 0x04: Selected battery not present (BIOS >= 1.2 only)
439 * 0xff: Unknown 434 * 0xff: Unknown
440 * 5) Battery flag 435 * 5) Battery flag
441 * bit 0: High 436 * bit 0: High
442 * bit 1: Low 437 * bit 1: Low
443 * bit 2: Critical 438 * bit 2: Critical
444 * bit 3: Charging 439 * bit 3: Charging
445 * bit 7: No system battery 440 * bit 7: No system battery
446 * 0xff: Unknown 441 * 0xff: Unknown
447 * 6) Remaining battery life (percentage of charge): 442 * 6) Remaining battery life (percentage of charge):
448 * 0-100: valid 443 * 0-100: valid
449 * -1: Unknown 444 * -1: Unknown
450 * 7) Remaining battery life (time units): 445 * 7) Remaining battery life (time units):
451 * Number of remaining minutes or seconds 446 * Number of remaining minutes or seconds
452 * -1: Unknown 447 * -1: Unknown
453 * 8) min = minutes; sec = seconds 448 * 8) min = minutes; sec = seconds
454 */ 449 */
455 static int proc_apm_show(struct seq_file *m, void *v) 450 static int proc_apm_show(struct seq_file *m, void *v)
456 { 451 {
457 struct apm_power_info info; 452 struct apm_power_info info;
458 char *units; 453 char *units;
459 454
460 info.ac_line_status = 0xff; 455 info.ac_line_status = 0xff;
461 info.battery_status = 0xff; 456 info.battery_status = 0xff;
462 info.battery_flag = 0xff; 457 info.battery_flag = 0xff;
463 info.battery_life = -1; 458 info.battery_life = -1;
464 info.time = -1; 459 info.time = -1;
465 info.units = -1; 460 info.units = -1;
466 461
467 if (apm_get_power_status) 462 if (apm_get_power_status)
468 apm_get_power_status(&info); 463 apm_get_power_status(&info);
469 464
470 switch (info.units) { 465 switch (info.units) {
471 default: units = "?"; break; 466 default: units = "?"; break;
472 case 0: units = "min"; break; 467 case 0: units = "min"; break;
473 case 1: units = "sec"; break; 468 case 1: units = "sec"; break;
474 } 469 }
475 470
476 seq_printf(m, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n", 471 seq_printf(m, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
477 driver_version, APM_32_BIT_SUPPORT, 472 driver_version, APM_32_BIT_SUPPORT,
478 info.ac_line_status, info.battery_status, 473 info.ac_line_status, info.battery_status,
479 info.battery_flag, info.battery_life, 474 info.battery_flag, info.battery_life,
480 info.time, units); 475 info.time, units);
481 476
482 return 0; 477 return 0;
483 } 478 }
484 479
485 static int proc_apm_open(struct inode *inode, struct file *file) 480 static int proc_apm_open(struct inode *inode, struct file *file)
486 { 481 {
487 return single_open(file, proc_apm_show, NULL); 482 return single_open(file, proc_apm_show, NULL);
488 } 483 }
489 484
490 static const struct file_operations apm_proc_fops = { 485 static const struct file_operations apm_proc_fops = {
491 .owner = THIS_MODULE, 486 .owner = THIS_MODULE,
492 .open = proc_apm_open, 487 .open = proc_apm_open,
493 .read = seq_read, 488 .read = seq_read,
494 .llseek = seq_lseek, 489 .llseek = seq_lseek,
495 .release = single_release, 490 .release = single_release,
496 }; 491 };
497 #endif 492 #endif
498 493
499 static int kapmd(void *arg) 494 static int kapmd(void *arg)
500 { 495 {
501 do { 496 do {
502 apm_event_t event; 497 apm_event_t event;
503 498
504 wait_event_interruptible(kapmd_wait, 499 wait_event_interruptible(kapmd_wait,
505 !queue_empty(&kapmd_queue) || kthread_should_stop()); 500 !queue_empty(&kapmd_queue) || kthread_should_stop());
506 501
507 if (kthread_should_stop()) 502 if (kthread_should_stop())
508 break; 503 break;
509 504
510 spin_lock_irq(&kapmd_queue_lock); 505 spin_lock_irq(&kapmd_queue_lock);
511 event = 0; 506 event = 0;
512 if (!queue_empty(&kapmd_queue)) 507 if (!queue_empty(&kapmd_queue))
513 event = queue_get_event(&kapmd_queue); 508 event = queue_get_event(&kapmd_queue);
514 spin_unlock_irq(&kapmd_queue_lock); 509 spin_unlock_irq(&kapmd_queue_lock);
515 510
516 switch (event) { 511 switch (event) {
517 case 0: 512 case 0:
518 break; 513 break;
519 514
520 case APM_LOW_BATTERY: 515 case APM_LOW_BATTERY:
521 case APM_POWER_STATUS_CHANGE: 516 case APM_POWER_STATUS_CHANGE:
522 queue_event(event); 517 queue_event(event);
523 break; 518 break;
524 519
525 case APM_USER_SUSPEND: 520 case APM_USER_SUSPEND:
526 case APM_SYS_SUSPEND: 521 case APM_SYS_SUSPEND:
527 pm_suspend(PM_SUSPEND_MEM); 522 pm_suspend(PM_SUSPEND_MEM);
528 break; 523 break;
529 524
530 case APM_CRITICAL_SUSPEND: 525 case APM_CRITICAL_SUSPEND:
531 atomic_inc(&userspace_notification_inhibit); 526 atomic_inc(&userspace_notification_inhibit);
532 pm_suspend(PM_SUSPEND_MEM); 527 pm_suspend(PM_SUSPEND_MEM);
533 atomic_dec(&userspace_notification_inhibit); 528 atomic_dec(&userspace_notification_inhibit);
534 break; 529 break;
535 } 530 }
536 } while (1); 531 } while (1);
537 532
538 return 0; 533 return 0;
539 } 534 }
540 535
541 static int apm_suspend_notifier(struct notifier_block *nb, 536 static int apm_suspend_notifier(struct notifier_block *nb,
542 unsigned long event, 537 unsigned long event,
543 void *dummy) 538 void *dummy)
544 { 539 {
545 struct apm_user *as; 540 struct apm_user *as;
546 int err; 541 int err;
547 542
548 /* short-cut emergency suspends */ 543 /* short-cut emergency suspends */
549 if (atomic_read(&userspace_notification_inhibit)) 544 if (atomic_read(&userspace_notification_inhibit))
550 return NOTIFY_DONE; 545 return NOTIFY_DONE;
551 546
552 switch (event) { 547 switch (event) {
553 case PM_SUSPEND_PREPARE: 548 case PM_SUSPEND_PREPARE:
554 /* 549 /*
555 * Queue an event to all "writer" users that we want 550 * Queue an event to all "writer" users that we want
556 * to suspend and need their ack. 551 * to suspend and need their ack.
557 */ 552 */
558 mutex_lock(&state_lock); 553 mutex_lock(&state_lock);
559 down_read(&user_list_lock); 554 down_read(&user_list_lock);
560 555
561 list_for_each_entry(as, &apm_user_list, list) { 556 list_for_each_entry(as, &apm_user_list, list) {
562 if (as->suspend_state != SUSPEND_WAIT && as->reader && 557 if (as->suspend_state != SUSPEND_WAIT && as->reader &&
563 as->writer && as->suser) { 558 as->writer && as->suser) {
564 as->suspend_state = SUSPEND_PENDING; 559 as->suspend_state = SUSPEND_PENDING;
565 atomic_inc(&suspend_acks_pending); 560 atomic_inc(&suspend_acks_pending);
566 queue_add_event(&as->queue, APM_USER_SUSPEND); 561 queue_add_event(&as->queue, APM_USER_SUSPEND);
567 } 562 }
568 } 563 }
569 564
570 up_read(&user_list_lock); 565 up_read(&user_list_lock);
571 mutex_unlock(&state_lock); 566 mutex_unlock(&state_lock);
572 wake_up_interruptible(&apm_waitqueue); 567 wake_up_interruptible(&apm_waitqueue);
573 568
574 /* 569 /*
575 * Wait for the the suspend_acks_pending variable to drop to 570 * Wait for the the suspend_acks_pending variable to drop to
576 * zero, meaning everybody acked the suspend event (or the 571 * zero, meaning everybody acked the suspend event (or the
577 * process was killed.) 572 * process was killed.)
578 * 573 *
579 * If the app won't answer within a short while we assume it 574 * If the app won't answer within a short while we assume it
580 * locked up and ignore it. 575 * locked up and ignore it.
581 */ 576 */
582 err = wait_event_interruptible_timeout( 577 err = wait_event_interruptible_timeout(
583 apm_suspend_waitqueue, 578 apm_suspend_waitqueue,
584 atomic_read(&suspend_acks_pending) == 0, 579 atomic_read(&suspend_acks_pending) == 0,
585 5*HZ); 580 5*HZ);
586 581
587 /* timed out */ 582 /* timed out */
588 if (err == 0) { 583 if (err == 0) {
589 /* 584 /*
590 * Move anybody who timed out to "ack timeout" state. 585 * Move anybody who timed out to "ack timeout" state.
591 * 586 *
592 * We could time out and the userspace does the ACK 587 * We could time out and the userspace does the ACK
593 * right after we time out but before we enter the 588 * right after we time out but before we enter the
594 * locked section here, but that's fine. 589 * locked section here, but that's fine.
595 */ 590 */
596 mutex_lock(&state_lock); 591 mutex_lock(&state_lock);
597 down_read(&user_list_lock); 592 down_read(&user_list_lock);
598 list_for_each_entry(as, &apm_user_list, list) { 593 list_for_each_entry(as, &apm_user_list, list) {
599 if (as->suspend_state == SUSPEND_PENDING || 594 if (as->suspend_state == SUSPEND_PENDING ||
600 as->suspend_state == SUSPEND_READ) { 595 as->suspend_state == SUSPEND_READ) {
601 as->suspend_state = SUSPEND_ACKTO; 596 as->suspend_state = SUSPEND_ACKTO;
602 atomic_dec(&suspend_acks_pending); 597 atomic_dec(&suspend_acks_pending);
603 } 598 }
604 } 599 }
605 up_read(&user_list_lock); 600 up_read(&user_list_lock);
606 mutex_unlock(&state_lock); 601 mutex_unlock(&state_lock);
607 } 602 }
608 603
609 /* let suspend proceed */ 604 /* let suspend proceed */
610 if (err >= 0) 605 if (err >= 0)
611 return NOTIFY_OK; 606 return NOTIFY_OK;
612 607
613 /* interrupted by signal */ 608 /* interrupted by signal */
614 return NOTIFY_BAD; 609 return NOTIFY_BAD;
615 610
616 case PM_POST_SUSPEND: 611 case PM_POST_SUSPEND:
617 /* 612 /*
618 * Anyone on the APM queues will think we're still suspended. 613 * Anyone on the APM queues will think we're still suspended.
619 * Send a message so everyone knows we're now awake again. 614 * Send a message so everyone knows we're now awake again.
620 */ 615 */
621 queue_event(APM_NORMAL_RESUME); 616 queue_event(APM_NORMAL_RESUME);
622 617
623 /* 618 /*
624 * Finally, wake up anyone who is sleeping on the suspend. 619 * Finally, wake up anyone who is sleeping on the suspend.
625 */ 620 */
626 mutex_lock(&state_lock); 621 mutex_lock(&state_lock);
627 down_read(&user_list_lock); 622 down_read(&user_list_lock);
628 list_for_each_entry(as, &apm_user_list, list) { 623 list_for_each_entry(as, &apm_user_list, list) {
629 if (as->suspend_state == SUSPEND_ACKED) { 624 if (as->suspend_state == SUSPEND_ACKED) {
630 /* 625 /*
631 * TODO: maybe grab error code, needs core 626 * TODO: maybe grab error code, needs core
632 * changes to push the error to the notifier 627 * changes to push the error to the notifier
633 * chain (could use the second parameter if 628 * chain (could use the second parameter if
634 * implemented) 629 * implemented)
635 */ 630 */
636 as->suspend_result = 0; 631 as->suspend_result = 0;
637 as->suspend_state = SUSPEND_DONE; 632 as->suspend_state = SUSPEND_DONE;
638 } 633 }
639 } 634 }
640 up_read(&user_list_lock); 635 up_read(&user_list_lock);
641 mutex_unlock(&state_lock); 636 mutex_unlock(&state_lock);
642 637
643 wake_up(&apm_suspend_waitqueue); 638 wake_up(&apm_suspend_waitqueue);
644 return NOTIFY_OK; 639 return NOTIFY_OK;
645 640
646 default: 641 default:
647 return NOTIFY_DONE; 642 return NOTIFY_DONE;
648 } 643 }
649 } 644 }
650 645
651 static struct notifier_block apm_notif_block = { 646 static struct notifier_block apm_notif_block = {
652 .notifier_call = apm_suspend_notifier, 647 .notifier_call = apm_suspend_notifier,
653 }; 648 };
654 649
655 static int __init apm_init(void) 650 static int __init apm_init(void)
656 { 651 {
657 int ret; 652 int ret;
658 653
659 if (apm_disabled) { 654 if (apm_disabled) {
660 printk(KERN_NOTICE "apm: disabled on user request.\n"); 655 printk(KERN_NOTICE "apm: disabled on user request.\n");
661 return -ENODEV; 656 return -ENODEV;
662 } 657 }
663 658
664 kapmd_tsk = kthread_create(kapmd, NULL, "kapmd"); 659 kapmd_tsk = kthread_create(kapmd, NULL, "kapmd");
665 if (IS_ERR(kapmd_tsk)) { 660 if (IS_ERR(kapmd_tsk)) {
666 ret = PTR_ERR(kapmd_tsk); 661 ret = PTR_ERR(kapmd_tsk);
667 kapmd_tsk = NULL; 662 kapmd_tsk = NULL;
668 goto out; 663 goto out;
669 } 664 }
670 wake_up_process(kapmd_tsk); 665 wake_up_process(kapmd_tsk);
671 666
672 #ifdef CONFIG_PROC_FS 667 #ifdef CONFIG_PROC_FS
673 proc_create("apm", 0, NULL, &apm_proc_fops); 668 proc_create("apm", 0, NULL, &apm_proc_fops);
674 #endif 669 #endif
675 670
676 ret = misc_register(&apm_device); 671 ret = misc_register(&apm_device);
677 if (ret) 672 if (ret)
678 goto out_stop; 673 goto out_stop;
679 674
680 ret = register_pm_notifier(&apm_notif_block); 675 ret = register_pm_notifier(&apm_notif_block);
681 if (ret) 676 if (ret)
682 goto out_unregister; 677 goto out_unregister;
683 678
684 return 0; 679 return 0;
685 680
686 out_unregister: 681 out_unregister:
687 misc_deregister(&apm_device); 682 misc_deregister(&apm_device);
688 out_stop: 683 out_stop:
689 remove_proc_entry("apm", NULL); 684 remove_proc_entry("apm", NULL);
690 kthread_stop(kapmd_tsk); 685 kthread_stop(kapmd_tsk);
691 out: 686 out:
692 return ret; 687 return ret;
693 } 688 }
694 689
695 static void __exit apm_exit(void) 690 static void __exit apm_exit(void)
696 { 691 {
697 unregister_pm_notifier(&apm_notif_block); 692 unregister_pm_notifier(&apm_notif_block);
698 misc_deregister(&apm_device); 693 misc_deregister(&apm_device);
699 remove_proc_entry("apm", NULL); 694 remove_proc_entry("apm", NULL);
700 695
701 kthread_stop(kapmd_tsk); 696 kthread_stop(kapmd_tsk);
702 } 697 }
703 698
704 module_init(apm_init); 699 module_init(apm_init);
705 module_exit(apm_exit); 700 module_exit(apm_exit);
706 701
707 MODULE_AUTHOR("Stephen Rothwell"); 702 MODULE_AUTHOR("Stephen Rothwell");
708 MODULE_DESCRIPTION("Advanced Power Management"); 703 MODULE_DESCRIPTION("Advanced Power Management");
709 MODULE_LICENSE("GPL"); 704 MODULE_LICENSE("GPL");
710 705
711 #ifndef MODULE 706 #ifndef MODULE
712 static int __init apm_setup(char *str) 707 static int __init apm_setup(char *str)
713 { 708 {
714 while ((str != NULL) && (*str != '\0')) { 709 while ((str != NULL) && (*str != '\0')) {
715 if (strncmp(str, "off", 3) == 0) 710 if (strncmp(str, "off", 3) == 0)
716 apm_disabled = 1; 711 apm_disabled = 1;
717 if (strncmp(str, "on", 2) == 0) 712 if (strncmp(str, "on", 2) == 0)
718 apm_disabled = 0; 713 apm_disabled = 0;
719 str = strchr(str, ','); 714 str = strchr(str, ',');
720 if (str != NULL) 715 if (str != NULL)
721 str += strspn(str, ", \t"); 716 str += strspn(str, ", \t");
722 } 717 }
723 return 1; 718 return 1;
724 } 719 }
725 720
726 __setup("apm=", apm_setup); 721 __setup("apm=", apm_setup);
727 #endif 722 #endif
728 723
729 /** 724 /**
730 * apm_queue_event - queue an APM event for kapmd 725 * apm_queue_event - queue an APM event for kapmd
731 * @event: APM event 726 * @event: APM event
732 * 727 *
733 * Queue an APM event for kapmd to process and ultimately take the 728 * Queue an APM event for kapmd to process and ultimately take the
734 * appropriate action. Only a subset of events are handled: 729 * appropriate action. Only a subset of events are handled:
735 * %APM_LOW_BATTERY 730 * %APM_LOW_BATTERY
736 * %APM_POWER_STATUS_CHANGE 731 * %APM_POWER_STATUS_CHANGE
737 * %APM_USER_SUSPEND 732 * %APM_USER_SUSPEND
738 * %APM_SYS_SUSPEND 733 * %APM_SYS_SUSPEND
739 * %APM_CRITICAL_SUSPEND 734 * %APM_CRITICAL_SUSPEND
740 */ 735 */
741 void apm_queue_event(apm_event_t event) 736 void apm_queue_event(apm_event_t event)
742 { 737 {
743 unsigned long flags; 738 unsigned long flags;
744 739
745 spin_lock_irqsave(&kapmd_queue_lock, flags); 740 spin_lock_irqsave(&kapmd_queue_lock, flags);
746 queue_add_event(&kapmd_queue, event); 741 queue_add_event(&kapmd_queue, event);
747 spin_unlock_irqrestore(&kapmd_queue_lock, flags); 742 spin_unlock_irqrestore(&kapmd_queue_lock, flags);
748 743
749 wake_up_interruptible(&kapmd_wait); 744 wake_up_interruptible(&kapmd_wait);
750 } 745 }
751 EXPORT_SYMBOL(apm_queue_event); 746 EXPORT_SYMBOL(apm_queue_event);
752 747