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>
35 * One option can be changed at boot time as follows:
36 * apm=on/off enable/disable APM
40 * Maximum number of events stored
42 #define APM_MAX_EVENTS 16
45 unsigned int event_head
;
46 unsigned int event_tail
;
47 apm_event_t events
[APM_MAX_EVENTS
];
51 * thread states (for threads using a writable /dev/apm_bios fd):
53 * SUSPEND_NONE: nothing happening
54 * SUSPEND_PENDING: suspend event queued for thread and pending to be read
55 * SUSPEND_READ: suspend event read, pending acknowledgement
56 * SUSPEND_ACKED: acknowledgement received from thread (via ioctl),
58 * SUSPEND_ACKTO: acknowledgement timeout
59 * SUSPEND_DONE: thread had acked suspend and is now notified of
62 * SUSPEND_WAIT: this thread invoked suspend and is waiting for resume
64 * A thread migrates in one of three paths:
65 * NONE -1-> PENDING -2-> READ -3-> ACKED -4-> DONE -5-> NONE
66 * -6-> ACKTO -7-> NONE
67 * NONE -8-> WAIT -9-> NONE
69 * While in PENDING or READ, the thread is accounted for in the
70 * suspend_acks_pending counter.
72 * The transitions are invoked as follows:
73 * 1: suspend event is signalled from the core PM code
74 * 2: the suspend event is read from the fd by the userspace thread
75 * 3: userspace thread issues the APM_IOC_SUSPEND ioctl (as ack)
76 * 4: core PM code signals that we have resumed
77 * 5: APM_IOC_SUSPEND ioctl returns
79 * 6: the notifier invoked from the core PM code timed out waiting
80 * for all relevant threds to enter ACKED state and puts those
81 * that haven't into ACKTO
82 * 7: those threads issue APM_IOC_SUSPEND ioctl too late,
85 * 8: userspace thread issues the APM_IOC_SUSPEND ioctl (to suspend),
86 * ioctl code invokes pm_suspend()
87 * 9: pm_suspend() returns indicating resume
89 enum apm_suspend_state
{
100 * The per-file APM data
103 struct list_head list
;
105 unsigned int suser
: 1;
106 unsigned int writer
: 1;
107 unsigned int reader
: 1;
110 enum apm_suspend_state suspend_state
;
112 struct apm_queue queue
;
118 static atomic_t suspend_acks_pending
= ATOMIC_INIT(0);
119 static atomic_t userspace_notification_inhibit
= ATOMIC_INIT(0);
120 static int apm_disabled
;
121 static struct task_struct
*kapmd_tsk
;
123 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue
);
124 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue
);
127 * This is a list of everyone who has opened /dev/apm_bios
129 static DECLARE_RWSEM(user_list_lock
);
130 static LIST_HEAD(apm_user_list
);
133 * kapmd info. kapmd provides us a process context to handle
134 * "APM" events within - specifically necessary if we're going
135 * to be suspending the system.
137 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait
);
138 static DEFINE_SPINLOCK(kapmd_queue_lock
);
139 static struct apm_queue kapmd_queue
;
141 static DEFINE_MUTEX(state_lock
);
143 static const char driver_version
[] = "1.13"; /* no spaces */
148 * Compatibility cruft until the IPAQ people move over to the new
151 static void __apm_get_power_status(struct apm_power_info
*info
)
156 * This allows machines to provide their own "apm get power status" function.
158 void (*apm_get_power_status
)(struct apm_power_info
*) = __apm_get_power_status
;
159 EXPORT_SYMBOL(apm_get_power_status
);
163 * APM event queue management.
165 static inline int queue_empty(struct apm_queue
*q
)
167 return q
->event_head
== q
->event_tail
;
170 static inline apm_event_t
queue_get_event(struct apm_queue
*q
)
172 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
173 return q
->events
[q
->event_tail
];
176 static void queue_add_event(struct apm_queue
*q
, apm_event_t event
)
178 q
->event_head
= (q
->event_head
+ 1) % APM_MAX_EVENTS
;
179 if (q
->event_head
== q
->event_tail
) {
183 printk(KERN_ERR
"apm: an event queue overflowed\n");
184 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
186 q
->events
[q
->event_head
] = event
;
189 static void queue_event(apm_event_t event
)
193 down_read(&user_list_lock
);
194 list_for_each_entry(as
, &apm_user_list
, list
) {
196 queue_add_event(&as
->queue
, event
);
198 up_read(&user_list_lock
);
199 wake_up_interruptible(&apm_waitqueue
);
202 static ssize_t
apm_read(struct file
*fp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
204 struct apm_user
*as
= fp
->private_data
;
206 int i
= count
, ret
= 0;
208 if (count
< sizeof(apm_event_t
))
211 if (queue_empty(&as
->queue
) && fp
->f_flags
& O_NONBLOCK
)
214 wait_event_interruptible(apm_waitqueue
, !queue_empty(&as
->queue
));
216 while ((i
>= sizeof(event
)) && !queue_empty(&as
->queue
)) {
217 event
= queue_get_event(&as
->queue
);
220 if (copy_to_user(buf
, &event
, sizeof(event
)))
223 mutex_lock(&state_lock
);
224 if (as
->suspend_state
== SUSPEND_PENDING
&&
225 (event
== APM_SYS_SUSPEND
|| event
== APM_USER_SUSPEND
))
226 as
->suspend_state
= SUSPEND_READ
;
227 mutex_unlock(&state_lock
);
229 buf
+= sizeof(event
);
239 static __poll_t
apm_poll(struct file
*fp
, poll_table
* wait
)
241 struct apm_user
*as
= fp
->private_data
;
243 poll_wait(fp
, &apm_waitqueue
, wait
);
244 return queue_empty(&as
->queue
) ? 0 : EPOLLIN
| EPOLLRDNORM
;
248 * apm_ioctl - handle APM ioctl
251 * This IOCTL is overloaded, and performs two functions. It is used to:
252 * - initiate a suspend
253 * - acknowledge a suspend read from /dev/apm_bios.
254 * Only when everyone who has opened /dev/apm_bios with write permission
255 * has acknowledge does the actual suspend happen.
258 apm_ioctl(struct file
*filp
, u_int cmd
, u_long arg
)
260 struct apm_user
*as
= filp
->private_data
;
263 if (!as
->suser
|| !as
->writer
)
267 case APM_IOC_SUSPEND
:
268 mutex_lock(&state_lock
);
270 as
->suspend_result
= -EINTR
;
272 switch (as
->suspend_state
) {
275 * If we read a suspend command from /dev/apm_bios,
276 * then the corresponding APM_IOC_SUSPEND ioctl is
277 * interpreted as an acknowledge.
279 as
->suspend_state
= SUSPEND_ACKED
;
280 atomic_dec(&suspend_acks_pending
);
281 mutex_unlock(&state_lock
);
284 * suspend_acks_pending changed, the notifier needs to
285 * be woken up for this
287 wake_up(&apm_suspend_waitqueue
);
290 * Wait for the suspend/resume to complete. If there
291 * are pending acknowledges, we wait here for them.
292 * wait_event_freezable() is interruptible and pending
293 * signal can cause busy looping. We aren't doing
294 * anything critical, chill a bit on each iteration.
296 while (wait_event_freezable(apm_suspend_waitqueue
,
297 as
->suspend_state
!= SUSPEND_ACKED
))
301 as
->suspend_result
= -ETIMEDOUT
;
302 mutex_unlock(&state_lock
);
305 as
->suspend_state
= SUSPEND_WAIT
;
306 mutex_unlock(&state_lock
);
309 * Otherwise it is a request to suspend the system.
310 * Just invoke pm_suspend(), we'll handle it from
311 * there via the notifier.
313 as
->suspend_result
= pm_suspend(PM_SUSPEND_MEM
);
316 mutex_lock(&state_lock
);
317 err
= as
->suspend_result
;
318 as
->suspend_state
= SUSPEND_NONE
;
319 mutex_unlock(&state_lock
);
326 static int apm_release(struct inode
* inode
, struct file
* filp
)
328 struct apm_user
*as
= filp
->private_data
;
330 filp
->private_data
= NULL
;
332 down_write(&user_list_lock
);
334 up_write(&user_list_lock
);
337 * We are now unhooked from the chain. As far as new
338 * events are concerned, we no longer exist.
340 mutex_lock(&state_lock
);
341 if (as
->suspend_state
== SUSPEND_PENDING
||
342 as
->suspend_state
== SUSPEND_READ
)
343 atomic_dec(&suspend_acks_pending
);
344 mutex_unlock(&state_lock
);
346 wake_up(&apm_suspend_waitqueue
);
352 static int apm_open(struct inode
* inode
, struct file
* filp
)
356 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
359 * XXX - this is a tiny bit broken, when we consider BSD
360 * process accounting. If the device is opened by root, we
361 * instantly flag that we used superuser privs. Who knows,
362 * we might close the device immediately without doing a
363 * privileged operation -- cevans
365 as
->suser
= capable(CAP_SYS_ADMIN
);
366 as
->writer
= (filp
->f_mode
& FMODE_WRITE
) == FMODE_WRITE
;
367 as
->reader
= (filp
->f_mode
& FMODE_READ
) == FMODE_READ
;
369 down_write(&user_list_lock
);
370 list_add(&as
->list
, &apm_user_list
);
371 up_write(&user_list_lock
);
373 filp
->private_data
= as
;
376 return as
? 0 : -ENOMEM
;
379 static const struct file_operations apm_bios_fops
= {
380 .owner
= THIS_MODULE
,
383 .unlocked_ioctl
= apm_ioctl
,
385 .release
= apm_release
,
386 .llseek
= noop_llseek
,
389 static struct miscdevice apm_device
= {
390 .minor
= APM_MINOR_DEV
,
392 .fops
= &apm_bios_fops
396 #ifdef CONFIG_PROC_FS
398 * Arguments, with symbols from linux/apm_bios.h.
400 * 0) Linux driver version (this will change if format changes)
401 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
402 * 2) APM flags from APM Installation Check (0x00):
403 * bit 0: APM_16_BIT_SUPPORT
404 * bit 1: APM_32_BIT_SUPPORT
405 * bit 2: APM_IDLE_SLOWS_CLOCK
406 * bit 3: APM_BIOS_DISABLED
407 * bit 4: APM_BIOS_DISENGAGED
411 * 0x02: On backup power (BIOS >= 1.1 only)
418 * 0x04: Selected battery not present (BIOS >= 1.2 only)
425 * bit 7: No system battery
427 * 6) Remaining battery life (percentage of charge):
430 * 7) Remaining battery life (time units):
431 * Number of remaining minutes or seconds
433 * 8) min = minutes; sec = seconds
435 static int proc_apm_show(struct seq_file
*m
, void *v
)
437 struct apm_power_info info
;
440 info
.ac_line_status
= 0xff;
441 info
.battery_status
= 0xff;
442 info
.battery_flag
= 0xff;
443 info
.battery_life
= -1;
447 if (apm_get_power_status
)
448 apm_get_power_status(&info
);
450 switch (info
.units
) {
451 default: units
= "?"; break;
452 case 0: units
= "min"; break;
453 case 1: units
= "sec"; break;
456 seq_printf(m
, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
457 driver_version
, APM_32_BIT_SUPPORT
,
458 info
.ac_line_status
, info
.battery_status
,
459 info
.battery_flag
, info
.battery_life
,
466 static int kapmd(void *arg
)
471 wait_event_interruptible(kapmd_wait
,
472 !queue_empty(&kapmd_queue
) || kthread_should_stop());
474 if (kthread_should_stop())
477 spin_lock_irq(&kapmd_queue_lock
);
479 if (!queue_empty(&kapmd_queue
))
480 event
= queue_get_event(&kapmd_queue
);
481 spin_unlock_irq(&kapmd_queue_lock
);
487 case APM_LOW_BATTERY
:
488 case APM_POWER_STATUS_CHANGE
:
492 case APM_USER_SUSPEND
:
493 case APM_SYS_SUSPEND
:
494 pm_suspend(PM_SUSPEND_MEM
);
497 case APM_CRITICAL_SUSPEND
:
498 atomic_inc(&userspace_notification_inhibit
);
499 pm_suspend(PM_SUSPEND_MEM
);
500 atomic_dec(&userspace_notification_inhibit
);
508 static int apm_suspend_notifier(struct notifier_block
*nb
,
514 unsigned long apm_event
;
516 /* short-cut emergency suspends */
517 if (atomic_read(&userspace_notification_inhibit
))
521 case PM_SUSPEND_PREPARE
:
522 case PM_HIBERNATION_PREPARE
:
523 apm_event
= (event
== PM_SUSPEND_PREPARE
) ?
524 APM_USER_SUSPEND
: APM_USER_HIBERNATION
;
526 * Queue an event to all "writer" users that we want
527 * to suspend and need their ack.
529 mutex_lock(&state_lock
);
530 down_read(&user_list_lock
);
532 list_for_each_entry(as
, &apm_user_list
, list
) {
533 if (as
->suspend_state
!= SUSPEND_WAIT
&& as
->reader
&&
534 as
->writer
&& as
->suser
) {
535 as
->suspend_state
= SUSPEND_PENDING
;
536 atomic_inc(&suspend_acks_pending
);
537 queue_add_event(&as
->queue
, apm_event
);
541 up_read(&user_list_lock
);
542 mutex_unlock(&state_lock
);
543 wake_up_interruptible(&apm_waitqueue
);
546 * Wait for the the suspend_acks_pending variable to drop to
547 * zero, meaning everybody acked the suspend event (or the
548 * process was killed.)
550 * If the app won't answer within a short while we assume it
551 * locked up and ignore it.
553 err
= wait_event_interruptible_timeout(
554 apm_suspend_waitqueue
,
555 atomic_read(&suspend_acks_pending
) == 0,
561 * Move anybody who timed out to "ack timeout" state.
563 * We could time out and the userspace does the ACK
564 * right after we time out but before we enter the
565 * locked section here, but that's fine.
567 mutex_lock(&state_lock
);
568 down_read(&user_list_lock
);
569 list_for_each_entry(as
, &apm_user_list
, list
) {
570 if (as
->suspend_state
== SUSPEND_PENDING
||
571 as
->suspend_state
== SUSPEND_READ
) {
572 as
->suspend_state
= SUSPEND_ACKTO
;
573 atomic_dec(&suspend_acks_pending
);
576 up_read(&user_list_lock
);
577 mutex_unlock(&state_lock
);
580 /* let suspend proceed */
584 /* interrupted by signal */
585 return notifier_from_errno(err
);
587 case PM_POST_SUSPEND
:
588 case PM_POST_HIBERNATION
:
589 apm_event
= (event
== PM_POST_SUSPEND
) ?
590 APM_NORMAL_RESUME
: APM_HIBERNATION_RESUME
;
592 * Anyone on the APM queues will think we're still suspended.
593 * Send a message so everyone knows we're now awake again.
595 queue_event(apm_event
);
598 * Finally, wake up anyone who is sleeping on the suspend.
600 mutex_lock(&state_lock
);
601 down_read(&user_list_lock
);
602 list_for_each_entry(as
, &apm_user_list
, list
) {
603 if (as
->suspend_state
== SUSPEND_ACKED
) {
605 * TODO: maybe grab error code, needs core
606 * changes to push the error to the notifier
607 * chain (could use the second parameter if
610 as
->suspend_result
= 0;
611 as
->suspend_state
= SUSPEND_DONE
;
614 up_read(&user_list_lock
);
615 mutex_unlock(&state_lock
);
617 wake_up(&apm_suspend_waitqueue
);
625 static struct notifier_block apm_notif_block
= {
626 .notifier_call
= apm_suspend_notifier
,
629 static int __init
apm_init(void)
634 printk(KERN_NOTICE
"apm: disabled on user request.\n");
638 kapmd_tsk
= kthread_create(kapmd
, NULL
, "kapmd");
639 if (IS_ERR(kapmd_tsk
)) {
640 ret
= PTR_ERR(kapmd_tsk
);
644 wake_up_process(kapmd_tsk
);
646 #ifdef CONFIG_PROC_FS
647 proc_create_single("apm", 0, NULL
, proc_apm_show
);
650 ret
= misc_register(&apm_device
);
654 ret
= register_pm_notifier(&apm_notif_block
);
661 misc_deregister(&apm_device
);
663 remove_proc_entry("apm", NULL
);
664 kthread_stop(kapmd_tsk
);
669 static void __exit
apm_exit(void)
671 unregister_pm_notifier(&apm_notif_block
);
672 misc_deregister(&apm_device
);
673 remove_proc_entry("apm", NULL
);
675 kthread_stop(kapmd_tsk
);
678 module_init(apm_init
);
679 module_exit(apm_exit
);
681 MODULE_AUTHOR("Stephen Rothwell");
682 MODULE_DESCRIPTION("Advanced Power Management");
683 MODULE_LICENSE("GPL");
686 static int __init
apm_setup(char *str
)
688 while ((str
!= NULL
) && (*str
!= '\0')) {
689 if (strncmp(str
, "off", 3) == 0)
691 if (strncmp(str
, "on", 2) == 0)
693 str
= strchr(str
, ',');
695 str
+= strspn(str
, ", \t");
700 __setup("apm=", apm_setup
);
704 * apm_queue_event - queue an APM event for kapmd
707 * Queue an APM event for kapmd to process and ultimately take the
708 * appropriate action. Only a subset of events are handled:
710 * %APM_POWER_STATUS_CHANGE
713 * %APM_CRITICAL_SUSPEND
715 void apm_queue_event(apm_event_t event
)
719 spin_lock_irqsave(&kapmd_queue_lock
, flags
);
720 queue_add_event(&kapmd_queue
, event
);
721 spin_unlock_irqrestore(&kapmd_queue_lock
, flags
);
723 wake_up_interruptible(&kapmd_wait
);
725 EXPORT_SYMBOL(apm_queue_event
);