[PATCH] capable/capability.h (arch/)
[linux-block.git] / arch / arm / kernel / apm.c
CommitLineData
1da177e4
LT
1/*
2 * bios-less APM driver for ARM Linux
3 * Jamey Hicks <jamey@crl.dec.com>
4 * adapted from the APM BIOS driver for Linux by Stephen Rothwell (sfr@linuxcare.com)
5 *
6 * APM 1.2 Reference:
7 * Intel Corporation, Microsoft Corporation. Advanced Power Management
8 * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
9 *
10 * [This document is available from Microsoft at:
11 * http://www.microsoft.com/hwdev/busbios/amp_12.htm]
12 */
13#include <linux/config.h>
14#include <linux/module.h>
15#include <linux/poll.h>
16#include <linux/timer.h>
17#include <linux/slab.h>
18#include <linux/proc_fs.h>
19#include <linux/miscdevice.h>
20#include <linux/apm_bios.h>
a9415644 21#include <linux/capability.h>
1da177e4
LT
22#include <linux/sched.h>
23#include <linux/pm.h>
24#include <linux/device.h>
25#include <linux/kernel.h>
26#include <linux/list.h>
27#include <linux/init.h>
28#include <linux/completion.h>
29
30#include <asm/apm.h> /* apm_power_info */
31#include <asm/system.h>
32
33/*
34 * The apm_bios device is one of the misc char devices.
35 * This is its minor number.
36 */
37#define APM_MINOR_DEV 134
38
39/*
40 * See Documentation/Config.help for the configuration options.
41 *
42 * Various options can be changed at boot time as follows:
43 * (We allow underscores for compatibility with the modules code)
44 * apm=on/off enable/disable APM
45 */
46
47/*
48 * Maximum number of events stored
49 */
50#define APM_MAX_EVENTS 16
51
52struct apm_queue {
53 unsigned int event_head;
54 unsigned int event_tail;
55 apm_event_t events[APM_MAX_EVENTS];
56};
57
58/*
59 * The per-file APM data
60 */
61struct apm_user {
62 struct list_head list;
63
64 unsigned int suser: 1;
65 unsigned int writer: 1;
66 unsigned int reader: 1;
67
68 int suspend_result;
69 unsigned int suspend_state;
70#define SUSPEND_NONE 0 /* no suspend pending */
71#define SUSPEND_PENDING 1 /* suspend pending read */
72#define SUSPEND_READ 2 /* suspend read, pending ack */
73#define SUSPEND_ACKED 3 /* suspend acked */
74#define SUSPEND_DONE 4 /* suspend completed */
75
76 struct apm_queue queue;
77};
78
79/*
80 * Local variables
81 */
82static int suspends_pending;
83static int apm_disabled;
53b7c2b2 84static int arm_apm_active;
1da177e4
LT
85
86static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
87static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
88
89/*
90 * This is a list of everyone who has opened /dev/apm_bios
91 */
92static DECLARE_RWSEM(user_list_lock);
93static LIST_HEAD(apm_user_list);
94
95/*
96 * kapmd info. kapmd provides us a process context to handle
97 * "APM" events within - specifically necessary if we're going
98 * to be suspending the system.
99 */
100static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
101static DECLARE_COMPLETION(kapmd_exit);
102static DEFINE_SPINLOCK(kapmd_queue_lock);
103static struct apm_queue kapmd_queue;
104
105
106static const char driver_version[] = "1.13"; /* no spaces */
107
108
109
110/*
111 * Compatibility cruft until the IPAQ people move over to the new
112 * interface.
113 */
114static void __apm_get_power_status(struct apm_power_info *info)
115{
116}
117
118/*
119 * This allows machines to provide their own "apm get power status" function.
120 */
121void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
122EXPORT_SYMBOL(apm_get_power_status);
123
124
125/*
126 * APM event queue management.
127 */
128static inline int queue_empty(struct apm_queue *q)
129{
130 return q->event_head == q->event_tail;
131}
132
133static inline apm_event_t queue_get_event(struct apm_queue *q)
134{
135 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
136 return q->events[q->event_tail];
137}
138
139static void queue_add_event(struct apm_queue *q, apm_event_t event)
140{
141 q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
142 if (q->event_head == q->event_tail) {
143 static int notified;
144
145 if (notified++ == 0)
146 printk(KERN_ERR "apm: an event queue overflowed\n");
147 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
148 }
149 q->events[q->event_head] = event;
150}
151
152static void queue_event_one_user(struct apm_user *as, apm_event_t event)
153{
154 if (as->suser && as->writer) {
155 switch (event) {
156 case APM_SYS_SUSPEND:
157 case APM_USER_SUSPEND:
158 /*
159 * If this user already has a suspend pending,
160 * don't queue another one.
161 */
162 if (as->suspend_state != SUSPEND_NONE)
163 return;
164
165 as->suspend_state = SUSPEND_PENDING;
166 suspends_pending++;
167 break;
168 }
169 }
170 queue_add_event(&as->queue, event);
171}
172
173static void queue_event(apm_event_t event, struct apm_user *sender)
174{
175 struct apm_user *as;
176
177 down_read(&user_list_lock);
178 list_for_each_entry(as, &apm_user_list, list) {
179 if (as != sender && as->reader)
180 queue_event_one_user(as, event);
181 }
182 up_read(&user_list_lock);
183 wake_up_interruptible(&apm_waitqueue);
184}
185
186static void apm_suspend(void)
187{
188 struct apm_user *as;
189 int err = pm_suspend(PM_SUSPEND_MEM);
190
191 /*
192 * Anyone on the APM queues will think we're still suspended.
193 * Send a message so everyone knows we're now awake again.
194 */
195 queue_event(APM_NORMAL_RESUME, NULL);
196
197 /*
198 * Finally, wake up anyone who is sleeping on the suspend.
199 */
200 down_read(&user_list_lock);
201 list_for_each_entry(as, &apm_user_list, list) {
202 as->suspend_result = err;
203 as->suspend_state = SUSPEND_DONE;
204 }
205 up_read(&user_list_lock);
206
207 wake_up(&apm_suspend_waitqueue);
208}
209
210static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
211{
212 struct apm_user *as = fp->private_data;
213 apm_event_t event;
214 int i = count, ret = 0;
215
216 if (count < sizeof(apm_event_t))
217 return -EINVAL;
218
219 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
220 return -EAGAIN;
221
222 wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
223
224 while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
225 event = queue_get_event(&as->queue);
226
227 ret = -EFAULT;
228 if (copy_to_user(buf, &event, sizeof(event)))
229 break;
230
231 if (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND)
232 as->suspend_state = SUSPEND_READ;
233
234 buf += sizeof(event);
235 i -= sizeof(event);
236 }
237
238 if (i < count)
239 ret = count - i;
240
241 return ret;
242}
243
244static unsigned int apm_poll(struct file *fp, poll_table * wait)
245{
246 struct apm_user *as = fp->private_data;
247
248 poll_wait(fp, &apm_waitqueue, wait);
249 return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;
250}
251
252/*
253 * apm_ioctl - handle APM ioctl
254 *
255 * APM_IOC_SUSPEND
256 * This IOCTL is overloaded, and performs two functions. It is used to:
257 * - initiate a suspend
258 * - acknowledge a suspend read from /dev/apm_bios.
259 * Only when everyone who has opened /dev/apm_bios with write permission
260 * has acknowledge does the actual suspend happen.
261 */
262static int
263apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
264{
265 struct apm_user *as = filp->private_data;
266 unsigned long flags;
267 int err = -EINVAL;
268
269 if (!as->suser || !as->writer)
270 return -EPERM;
271
272 switch (cmd) {
273 case APM_IOC_SUSPEND:
274 as->suspend_result = -EINTR;
275
276 if (as->suspend_state == SUSPEND_READ) {
277 /*
278 * If we read a suspend command from /dev/apm_bios,
279 * then the corresponding APM_IOC_SUSPEND ioctl is
280 * interpreted as an acknowledge.
281 */
282 as->suspend_state = SUSPEND_ACKED;
283 suspends_pending--;
284 } else {
285 /*
286 * Otherwise it is a request to suspend the system.
287 * Queue an event for all readers, and expect an
288 * acknowledge from all writers who haven't already
289 * acknowledged.
290 */
291 queue_event(APM_USER_SUSPEND, as);
292 }
293
294 /*
295 * If there are no further acknowledges required, suspend
296 * the system.
297 */
298 if (suspends_pending == 0)
299 apm_suspend();
300
301 /*
302 * Wait for the suspend/resume to complete. If there are
303 * pending acknowledges, we wait here for them.
304 *
305 * Note that we need to ensure that the PM subsystem does
306 * not kick us out of the wait when it suspends the threads.
307 */
308 flags = current->flags;
309 current->flags |= PF_NOFREEZE;
310
311 /*
312 * Note: do not allow a thread which is acking the suspend
313 * to escape until the resume is complete.
314 */
315 if (as->suspend_state == SUSPEND_ACKED)
316 wait_event(apm_suspend_waitqueue,
317 as->suspend_state == SUSPEND_DONE);
318 else
319 wait_event_interruptible(apm_suspend_waitqueue,
320 as->suspend_state == SUSPEND_DONE);
321
322 current->flags = flags;
323 err = as->suspend_result;
324 as->suspend_state = SUSPEND_NONE;
325 break;
326 }
327
328 return err;
329}
330
331static int apm_release(struct inode * inode, struct file * filp)
332{
333 struct apm_user *as = filp->private_data;
334 filp->private_data = NULL;
335
336 down_write(&user_list_lock);
337 list_del(&as->list);
338 up_write(&user_list_lock);
339
340 /*
341 * We are now unhooked from the chain. As far as new
342 * events are concerned, we no longer exist. However, we
343 * need to balance suspends_pending, which means the
344 * possibility of sleeping.
345 */
346 if (as->suspend_state != SUSPEND_NONE) {
347 suspends_pending -= 1;
348 if (suspends_pending == 0)
349 apm_suspend();
350 }
351
352 kfree(as);
353 return 0;
354}
355
356static int apm_open(struct inode * inode, struct file * filp)
357{
358 struct apm_user *as;
359
360 as = (struct apm_user *)kmalloc(sizeof(*as), GFP_KERNEL);
361 if (as) {
362 memset(as, 0, sizeof(*as));
363
364 /*
365 * XXX - this is a tiny bit broken, when we consider BSD
366 * process accounting. If the device is opened by root, we
367 * instantly flag that we used superuser privs. Who knows,
368 * we might close the device immediately without doing a
369 * privileged operation -- cevans
370 */
371 as->suser = capable(CAP_SYS_ADMIN);
372 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
373 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
374
375 down_write(&user_list_lock);
376 list_add(&as->list, &apm_user_list);
377 up_write(&user_list_lock);
378
379 filp->private_data = as;
380 }
381
382 return as ? 0 : -ENOMEM;
383}
384
385static struct file_operations apm_bios_fops = {
386 .owner = THIS_MODULE,
387 .read = apm_read,
388 .poll = apm_poll,
389 .ioctl = apm_ioctl,
390 .open = apm_open,
391 .release = apm_release,
392};
393
394static struct miscdevice apm_device = {
395 .minor = APM_MINOR_DEV,
396 .name = "apm_bios",
397 .fops = &apm_bios_fops
398};
399
400
401#ifdef CONFIG_PROC_FS
402/*
403 * Arguments, with symbols from linux/apm_bios.h.
404 *
405 * 0) Linux driver version (this will change if format changes)
406 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
407 * 2) APM flags from APM Installation Check (0x00):
408 * bit 0: APM_16_BIT_SUPPORT
409 * bit 1: APM_32_BIT_SUPPORT
410 * bit 2: APM_IDLE_SLOWS_CLOCK
411 * bit 3: APM_BIOS_DISABLED
412 * bit 4: APM_BIOS_DISENGAGED
413 * 3) AC line status
414 * 0x00: Off-line
415 * 0x01: On-line
416 * 0x02: On backup power (BIOS >= 1.1 only)
417 * 0xff: Unknown
418 * 4) Battery status
419 * 0x00: High
420 * 0x01: Low
421 * 0x02: Critical
422 * 0x03: Charging
423 * 0x04: Selected battery not present (BIOS >= 1.2 only)
424 * 0xff: Unknown
425 * 5) Battery flag
426 * bit 0: High
427 * bit 1: Low
428 * bit 2: Critical
429 * bit 3: Charging
430 * bit 7: No system battery
431 * 0xff: Unknown
432 * 6) Remaining battery life (percentage of charge):
433 * 0-100: valid
434 * -1: Unknown
435 * 7) Remaining battery life (time units):
436 * Number of remaining minutes or seconds
437 * -1: Unknown
438 * 8) min = minutes; sec = seconds
439 */
440static int apm_get_info(char *buf, char **start, off_t fpos, int length)
441{
442 struct apm_power_info info;
443 char *units;
444 int ret;
445
446 info.ac_line_status = 0xff;
447 info.battery_status = 0xff;
448 info.battery_flag = 0xff;
449 info.battery_life = -1;
450 info.time = -1;
451 info.units = -1;
452
453 if (apm_get_power_status)
454 apm_get_power_status(&info);
455
456 switch (info.units) {
457 default: units = "?"; break;
458 case 0: units = "min"; break;
459 case 1: units = "sec"; break;
460 }
461
462 ret = sprintf(buf, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
463 driver_version, APM_32_BIT_SUPPORT,
464 info.ac_line_status, info.battery_status,
465 info.battery_flag, info.battery_life,
466 info.time, units);
467
468 return ret;
469}
470#endif
471
472static int kapmd(void *arg)
473{
474 daemonize("kapmd");
475 current->flags |= PF_NOFREEZE;
476
477 do {
478 apm_event_t event;
479
480 wait_event_interruptible(kapmd_wait,
53b7c2b2 481 !queue_empty(&kapmd_queue) || !arm_apm_active);
1da177e4 482
53b7c2b2 483 if (!arm_apm_active)
1da177e4
LT
484 break;
485
486 spin_lock_irq(&kapmd_queue_lock);
487 event = 0;
488 if (!queue_empty(&kapmd_queue))
489 event = queue_get_event(&kapmd_queue);
490 spin_unlock_irq(&kapmd_queue_lock);
491
492 switch (event) {
493 case 0:
494 break;
495
496 case APM_LOW_BATTERY:
497 case APM_POWER_STATUS_CHANGE:
498 queue_event(event, NULL);
499 break;
500
501 case APM_USER_SUSPEND:
502 case APM_SYS_SUSPEND:
503 queue_event(event, NULL);
504 if (suspends_pending == 0)
505 apm_suspend();
506 break;
507
508 case APM_CRITICAL_SUSPEND:
509 apm_suspend();
510 break;
511 }
512 } while (1);
513
514 complete_and_exit(&kapmd_exit, 0);
515}
516
517static int __init apm_init(void)
518{
519 int ret;
520
521 if (apm_disabled) {
522 printk(KERN_NOTICE "apm: disabled on user request.\n");
523 return -ENODEV;
524 }
525
53b7c2b2 526 arm_apm_active = 1;
1da177e4
LT
527
528 ret = kernel_thread(kapmd, NULL, CLONE_KERNEL);
529 if (ret < 0) {
53b7c2b2 530 arm_apm_active = 0;
1da177e4
LT
531 return ret;
532 }
533
534#ifdef CONFIG_PROC_FS
535 create_proc_info_entry("apm", 0, NULL, apm_get_info);
536#endif
537
538 ret = misc_register(&apm_device);
539 if (ret != 0) {
540 remove_proc_entry("apm", NULL);
541
53b7c2b2 542 arm_apm_active = 0;
1da177e4
LT
543 wake_up(&kapmd_wait);
544 wait_for_completion(&kapmd_exit);
545 }
546
547 return ret;
548}
549
550static void __exit apm_exit(void)
551{
552 misc_deregister(&apm_device);
553 remove_proc_entry("apm", NULL);
554
53b7c2b2 555 arm_apm_active = 0;
1da177e4
LT
556 wake_up(&kapmd_wait);
557 wait_for_completion(&kapmd_exit);
558}
559
560module_init(apm_init);
561module_exit(apm_exit);
562
563MODULE_AUTHOR("Stephen Rothwell");
564MODULE_DESCRIPTION("Advanced Power Management");
565MODULE_LICENSE("GPL");
566
567#ifndef MODULE
568static int __init apm_setup(char *str)
569{
570 while ((str != NULL) && (*str != '\0')) {
571 if (strncmp(str, "off", 3) == 0)
572 apm_disabled = 1;
573 if (strncmp(str, "on", 2) == 0)
574 apm_disabled = 0;
575 str = strchr(str, ',');
576 if (str != NULL)
577 str += strspn(str, ", \t");
578 }
579 return 1;
580}
581
582__setup("apm=", apm_setup);
583#endif
584
585/**
586 * apm_queue_event - queue an APM event for kapmd
587 * @event: APM event
588 *
589 * Queue an APM event for kapmd to process and ultimately take the
590 * appropriate action. Only a subset of events are handled:
591 * %APM_LOW_BATTERY
592 * %APM_POWER_STATUS_CHANGE
593 * %APM_USER_SUSPEND
594 * %APM_SYS_SUSPEND
595 * %APM_CRITICAL_SUSPEND
596 */
597void apm_queue_event(apm_event_t event)
598{
599 unsigned long flags;
600
601 spin_lock_irqsave(&kapmd_queue_lock, flags);
602 queue_add_event(&kapmd_queue, event);
603 spin_unlock_irqrestore(&kapmd_queue_lock, flags);
604
605 wake_up_interruptible(&kapmd_wait);
606}
607EXPORT_SYMBOL(apm_queue_event);