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)
7 * Intel Corporation, Microsoft Corporation. Advanced Power Management
8 * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
10 * This document is available from Microsoft at:
11 * http://www.microsoft.com/whdc/archive/amp_12.mspx
13 #include <linux/module.h>
14 #include <linux/poll.h>
15 #include <linux/slab.h>
16 #include <linux/mutex.h>
17 #include <linux/proc_fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/miscdevice.h>
20 #include <linux/apm_bios.h>
21 #include <linux/capability.h>
22 #include <linux/sched.h>
23 #include <linux/suspend.h>
24 #include <linux/apm-emulation.h>
25 #include <linux/freezer.h>
26 #include <linux/device.h>
27 #include <linux/kernel.h>
28 #include <linux/list.h>
29 #include <linux/init.h>
30 #include <linux/completion.h>
31 #include <linux/kthread.h>
32 #include <linux/delay.h>
34 #include <asm/system.h>
37 * The apm_bios device is one of the misc char devices.
38 * This is its minor number.
40 #define APM_MINOR_DEV 134
43 * One option can be changed at boot time as follows:
44 * apm=on/off enable/disable APM
48 * Maximum number of events stored
50 #define APM_MAX_EVENTS 16
53 unsigned int event_head
;
54 unsigned int event_tail
;
55 apm_event_t events
[APM_MAX_EVENTS
];
59 * thread states (for threads using a writable /dev/apm_bios fd):
61 * SUSPEND_NONE: nothing happening
62 * SUSPEND_PENDING: suspend event queued for thread and pending to be read
63 * SUSPEND_READ: suspend event read, pending acknowledgement
64 * SUSPEND_ACKED: acknowledgement received from thread (via ioctl),
66 * SUSPEND_ACKTO: acknowledgement timeout
67 * SUSPEND_DONE: thread had acked suspend and is now notified of
70 * SUSPEND_WAIT: this thread invoked suspend and is waiting for resume
72 * A thread migrates in one of three paths:
73 * NONE -1-> PENDING -2-> READ -3-> ACKED -4-> DONE -5-> NONE
74 * -6-> ACKTO -7-> NONE
75 * NONE -8-> WAIT -9-> NONE
77 * While in PENDING or READ, the thread is accounted for in the
78 * suspend_acks_pending counter.
80 * The transitions are invoked as follows:
81 * 1: suspend event is signalled from the core PM code
82 * 2: the suspend event is read from the fd by the userspace thread
83 * 3: userspace thread issues the APM_IOC_SUSPEND ioctl (as ack)
84 * 4: core PM code signals that we have resumed
85 * 5: APM_IOC_SUSPEND ioctl returns
87 * 6: the notifier invoked from the core PM code timed out waiting
88 * for all relevant threds to enter ACKED state and puts those
89 * that haven't into ACKTO
90 * 7: those threads issue APM_IOC_SUSPEND ioctl too late,
93 * 8: userspace thread issues the APM_IOC_SUSPEND ioctl (to suspend),
94 * ioctl code invokes pm_suspend()
95 * 9: pm_suspend() returns indicating resume
97 enum apm_suspend_state
{
108 * The per-file APM data
111 struct list_head list
;
113 unsigned int suser
: 1;
114 unsigned int writer
: 1;
115 unsigned int reader
: 1;
118 enum apm_suspend_state suspend_state
;
120 struct apm_queue queue
;
126 static atomic_t suspend_acks_pending
= ATOMIC_INIT(0);
127 static atomic_t userspace_notification_inhibit
= ATOMIC_INIT(0);
128 static int apm_disabled
;
129 static struct task_struct
*kapmd_tsk
;
131 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue
);
132 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue
);
135 * This is a list of everyone who has opened /dev/apm_bios
137 static DECLARE_RWSEM(user_list_lock
);
138 static LIST_HEAD(apm_user_list
);
141 * kapmd info. kapmd provides us a process context to handle
142 * "APM" events within - specifically necessary if we're going
143 * to be suspending the system.
145 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait
);
146 static DEFINE_SPINLOCK(kapmd_queue_lock
);
147 static struct apm_queue kapmd_queue
;
149 static DEFINE_MUTEX(state_lock
);
151 static const char driver_version
[] = "1.13"; /* no spaces */
156 * Compatibility cruft until the IPAQ people move over to the new
159 static void __apm_get_power_status(struct apm_power_info
*info
)
164 * This allows machines to provide their own "apm get power status" function.
166 void (*apm_get_power_status
)(struct apm_power_info
*) = __apm_get_power_status
;
167 EXPORT_SYMBOL(apm_get_power_status
);
171 * APM event queue management.
173 static inline int queue_empty(struct apm_queue
*q
)
175 return q
->event_head
== q
->event_tail
;
178 static inline apm_event_t
queue_get_event(struct apm_queue
*q
)
180 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
181 return q
->events
[q
->event_tail
];
184 static void queue_add_event(struct apm_queue
*q
, apm_event_t event
)
186 q
->event_head
= (q
->event_head
+ 1) % APM_MAX_EVENTS
;
187 if (q
->event_head
== q
->event_tail
) {
191 printk(KERN_ERR
"apm: an event queue overflowed\n");
192 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
194 q
->events
[q
->event_head
] = event
;
197 static void queue_event(apm_event_t event
)
201 down_read(&user_list_lock
);
202 list_for_each_entry(as
, &apm_user_list
, list
) {
204 queue_add_event(&as
->queue
, event
);
206 up_read(&user_list_lock
);
207 wake_up_interruptible(&apm_waitqueue
);
210 static ssize_t
apm_read(struct file
*fp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
212 struct apm_user
*as
= fp
->private_data
;
214 int i
= count
, ret
= 0;
216 if (count
< sizeof(apm_event_t
))
219 if (queue_empty(&as
->queue
) && fp
->f_flags
& O_NONBLOCK
)
222 wait_event_interruptible(apm_waitqueue
, !queue_empty(&as
->queue
));
224 while ((i
>= sizeof(event
)) && !queue_empty(&as
->queue
)) {
225 event
= queue_get_event(&as
->queue
);
228 if (copy_to_user(buf
, &event
, sizeof(event
)))
231 mutex_lock(&state_lock
);
232 if (as
->suspend_state
== SUSPEND_PENDING
&&
233 (event
== APM_SYS_SUSPEND
|| event
== APM_USER_SUSPEND
))
234 as
->suspend_state
= SUSPEND_READ
;
235 mutex_unlock(&state_lock
);
237 buf
+= sizeof(event
);
247 static unsigned int apm_poll(struct file
*fp
, poll_table
* wait
)
249 struct apm_user
*as
= fp
->private_data
;
251 poll_wait(fp
, &apm_waitqueue
, wait
);
252 return queue_empty(&as
->queue
) ? 0 : POLLIN
| POLLRDNORM
;
256 * apm_ioctl - handle APM ioctl
259 * This IOCTL is overloaded, and performs two functions. It is used to:
260 * - initiate a suspend
261 * - acknowledge a suspend read from /dev/apm_bios.
262 * Only when everyone who has opened /dev/apm_bios with write permission
263 * has acknowledge does the actual suspend happen.
266 apm_ioctl(struct file
*filp
, u_int cmd
, u_long arg
)
268 struct apm_user
*as
= filp
->private_data
;
271 if (!as
->suser
|| !as
->writer
)
275 case APM_IOC_SUSPEND
:
276 mutex_lock(&state_lock
);
278 as
->suspend_result
= -EINTR
;
280 switch (as
->suspend_state
) {
283 * If we read a suspend command from /dev/apm_bios,
284 * then the corresponding APM_IOC_SUSPEND ioctl is
285 * interpreted as an acknowledge.
287 as
->suspend_state
= SUSPEND_ACKED
;
288 atomic_dec(&suspend_acks_pending
);
289 mutex_unlock(&state_lock
);
292 * suspend_acks_pending changed, the notifier needs to
293 * be woken up for this
295 wake_up(&apm_suspend_waitqueue
);
298 * Wait for the suspend/resume to complete. If there
299 * are pending acknowledges, we wait here for them.
300 * wait_event_freezable() is interruptible and pending
301 * signal can cause busy looping. We aren't doing
302 * anything critical, chill a bit on each iteration.
304 while (wait_event_freezable(apm_suspend_waitqueue
,
305 as
->suspend_state
== SUSPEND_DONE
))
309 as
->suspend_result
= -ETIMEDOUT
;
310 mutex_unlock(&state_lock
);
313 as
->suspend_state
= SUSPEND_WAIT
;
314 mutex_unlock(&state_lock
);
317 * Otherwise it is a request to suspend the system.
318 * Just invoke pm_suspend(), we'll handle it from
319 * there via the notifier.
321 as
->suspend_result
= pm_suspend(PM_SUSPEND_MEM
);
324 mutex_lock(&state_lock
);
325 err
= as
->suspend_result
;
326 as
->suspend_state
= SUSPEND_NONE
;
327 mutex_unlock(&state_lock
);
334 static int apm_release(struct inode
* inode
, struct file
* filp
)
336 struct apm_user
*as
= filp
->private_data
;
338 filp
->private_data
= NULL
;
340 down_write(&user_list_lock
);
342 up_write(&user_list_lock
);
345 * We are now unhooked from the chain. As far as new
346 * events are concerned, we no longer exist.
348 mutex_lock(&state_lock
);
349 if (as
->suspend_state
== SUSPEND_PENDING
||
350 as
->suspend_state
== SUSPEND_READ
)
351 atomic_dec(&suspend_acks_pending
);
352 mutex_unlock(&state_lock
);
354 wake_up(&apm_suspend_waitqueue
);
360 static int apm_open(struct inode
* inode
, struct file
* filp
)
364 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
367 * XXX - this is a tiny bit broken, when we consider BSD
368 * process accounting. If the device is opened by root, we
369 * instantly flag that we used superuser privs. Who knows,
370 * we might close the device immediately without doing a
371 * privileged operation -- cevans
373 as
->suser
= capable(CAP_SYS_ADMIN
);
374 as
->writer
= (filp
->f_mode
& FMODE_WRITE
) == FMODE_WRITE
;
375 as
->reader
= (filp
->f_mode
& FMODE_READ
) == FMODE_READ
;
377 down_write(&user_list_lock
);
378 list_add(&as
->list
, &apm_user_list
);
379 up_write(&user_list_lock
);
381 filp
->private_data
= as
;
384 return as
? 0 : -ENOMEM
;
387 static const struct file_operations apm_bios_fops
= {
388 .owner
= THIS_MODULE
,
391 .unlocked_ioctl
= apm_ioctl
,
393 .release
= apm_release
,
394 .llseek
= noop_llseek
,
397 static struct miscdevice apm_device
= {
398 .minor
= APM_MINOR_DEV
,
400 .fops
= &apm_bios_fops
404 #ifdef CONFIG_PROC_FS
406 * Arguments, with symbols from linux/apm_bios.h.
408 * 0) Linux driver version (this will change if format changes)
409 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
410 * 2) APM flags from APM Installation Check (0x00):
411 * bit 0: APM_16_BIT_SUPPORT
412 * bit 1: APM_32_BIT_SUPPORT
413 * bit 2: APM_IDLE_SLOWS_CLOCK
414 * bit 3: APM_BIOS_DISABLED
415 * bit 4: APM_BIOS_DISENGAGED
419 * 0x02: On backup power (BIOS >= 1.1 only)
426 * 0x04: Selected battery not present (BIOS >= 1.2 only)
433 * bit 7: No system battery
435 * 6) Remaining battery life (percentage of charge):
438 * 7) Remaining battery life (time units):
439 * Number of remaining minutes or seconds
441 * 8) min = minutes; sec = seconds
443 static int proc_apm_show(struct seq_file
*m
, void *v
)
445 struct apm_power_info info
;
448 info
.ac_line_status
= 0xff;
449 info
.battery_status
= 0xff;
450 info
.battery_flag
= 0xff;
451 info
.battery_life
= -1;
455 if (apm_get_power_status
)
456 apm_get_power_status(&info
);
458 switch (info
.units
) {
459 default: units
= "?"; break;
460 case 0: units
= "min"; break;
461 case 1: units
= "sec"; break;
464 seq_printf(m
, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
465 driver_version
, APM_32_BIT_SUPPORT
,
466 info
.ac_line_status
, info
.battery_status
,
467 info
.battery_flag
, info
.battery_life
,
473 static int proc_apm_open(struct inode
*inode
, struct file
*file
)
475 return single_open(file
, proc_apm_show
, NULL
);
478 static const struct file_operations apm_proc_fops
= {
479 .owner
= THIS_MODULE
,
480 .open
= proc_apm_open
,
483 .release
= single_release
,
487 static int kapmd(void *arg
)
492 wait_event_interruptible(kapmd_wait
,
493 !queue_empty(&kapmd_queue
) || kthread_should_stop());
495 if (kthread_should_stop())
498 spin_lock_irq(&kapmd_queue_lock
);
500 if (!queue_empty(&kapmd_queue
))
501 event
= queue_get_event(&kapmd_queue
);
502 spin_unlock_irq(&kapmd_queue_lock
);
508 case APM_LOW_BATTERY
:
509 case APM_POWER_STATUS_CHANGE
:
513 case APM_USER_SUSPEND
:
514 case APM_SYS_SUSPEND
:
515 pm_suspend(PM_SUSPEND_MEM
);
518 case APM_CRITICAL_SUSPEND
:
519 atomic_inc(&userspace_notification_inhibit
);
520 pm_suspend(PM_SUSPEND_MEM
);
521 atomic_dec(&userspace_notification_inhibit
);
529 static int apm_suspend_notifier(struct notifier_block
*nb
,
536 /* short-cut emergency suspends */
537 if (atomic_read(&userspace_notification_inhibit
))
541 case PM_SUSPEND_PREPARE
:
543 * Queue an event to all "writer" users that we want
544 * to suspend and need their ack.
546 mutex_lock(&state_lock
);
547 down_read(&user_list_lock
);
549 list_for_each_entry(as
, &apm_user_list
, list
) {
550 if (as
->suspend_state
!= SUSPEND_WAIT
&& as
->reader
&&
551 as
->writer
&& as
->suser
) {
552 as
->suspend_state
= SUSPEND_PENDING
;
553 atomic_inc(&suspend_acks_pending
);
554 queue_add_event(&as
->queue
, APM_USER_SUSPEND
);
558 up_read(&user_list_lock
);
559 mutex_unlock(&state_lock
);
560 wake_up_interruptible(&apm_waitqueue
);
563 * Wait for the the suspend_acks_pending variable to drop to
564 * zero, meaning everybody acked the suspend event (or the
565 * process was killed.)
567 * If the app won't answer within a short while we assume it
568 * locked up and ignore it.
570 err
= wait_event_interruptible_timeout(
571 apm_suspend_waitqueue
,
572 atomic_read(&suspend_acks_pending
) == 0,
578 * Move anybody who timed out to "ack timeout" state.
580 * We could time out and the userspace does the ACK
581 * right after we time out but before we enter the
582 * locked section here, but that's fine.
584 mutex_lock(&state_lock
);
585 down_read(&user_list_lock
);
586 list_for_each_entry(as
, &apm_user_list
, list
) {
587 if (as
->suspend_state
== SUSPEND_PENDING
||
588 as
->suspend_state
== SUSPEND_READ
) {
589 as
->suspend_state
= SUSPEND_ACKTO
;
590 atomic_dec(&suspend_acks_pending
);
593 up_read(&user_list_lock
);
594 mutex_unlock(&state_lock
);
597 /* let suspend proceed */
601 /* interrupted by signal */
602 return notifier_from_errno(err
);
604 case PM_POST_SUSPEND
:
606 * Anyone on the APM queues will think we're still suspended.
607 * Send a message so everyone knows we're now awake again.
609 queue_event(APM_NORMAL_RESUME
);
612 * Finally, wake up anyone who is sleeping on the suspend.
614 mutex_lock(&state_lock
);
615 down_read(&user_list_lock
);
616 list_for_each_entry(as
, &apm_user_list
, list
) {
617 if (as
->suspend_state
== SUSPEND_ACKED
) {
619 * TODO: maybe grab error code, needs core
620 * changes to push the error to the notifier
621 * chain (could use the second parameter if
624 as
->suspend_result
= 0;
625 as
->suspend_state
= SUSPEND_DONE
;
628 up_read(&user_list_lock
);
629 mutex_unlock(&state_lock
);
631 wake_up(&apm_suspend_waitqueue
);
639 static struct notifier_block apm_notif_block
= {
640 .notifier_call
= apm_suspend_notifier
,
643 static int __init
apm_init(void)
648 printk(KERN_NOTICE
"apm: disabled on user request.\n");
652 kapmd_tsk
= kthread_create(kapmd
, NULL
, "kapmd");
653 if (IS_ERR(kapmd_tsk
)) {
654 ret
= PTR_ERR(kapmd_tsk
);
658 wake_up_process(kapmd_tsk
);
660 #ifdef CONFIG_PROC_FS
661 proc_create("apm", 0, NULL
, &apm_proc_fops
);
664 ret
= misc_register(&apm_device
);
668 ret
= register_pm_notifier(&apm_notif_block
);
675 misc_deregister(&apm_device
);
677 remove_proc_entry("apm", NULL
);
678 kthread_stop(kapmd_tsk
);
683 static void __exit
apm_exit(void)
685 unregister_pm_notifier(&apm_notif_block
);
686 misc_deregister(&apm_device
);
687 remove_proc_entry("apm", NULL
);
689 kthread_stop(kapmd_tsk
);
692 module_init(apm_init
);
693 module_exit(apm_exit
);
695 MODULE_AUTHOR("Stephen Rothwell");
696 MODULE_DESCRIPTION("Advanced Power Management");
697 MODULE_LICENSE("GPL");
700 static int __init
apm_setup(char *str
)
702 while ((str
!= NULL
) && (*str
!= '\0')) {
703 if (strncmp(str
, "off", 3) == 0)
705 if (strncmp(str
, "on", 2) == 0)
707 str
= strchr(str
, ',');
709 str
+= strspn(str
, ", \t");
714 __setup("apm=", apm_setup
);
718 * apm_queue_event - queue an APM event for kapmd
721 * Queue an APM event for kapmd to process and ultimately take the
722 * appropriate action. Only a subset of events are handled:
724 * %APM_POWER_STATUS_CHANGE
727 * %APM_CRITICAL_SUSPEND
729 void apm_queue_event(apm_event_t event
)
733 spin_lock_irqsave(&kapmd_queue_lock
, flags
);
734 queue_add_event(&kapmd_queue
, event
);
735 spin_unlock_irqrestore(&kapmd_queue_lock
, flags
);
737 wake_up_interruptible(&kapmd_wait
);
739 EXPORT_SYMBOL(apm_queue_event
);