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>
36 * The apm_bios device is one of the misc char devices.
37 * This is its minor number.
39 #define APM_MINOR_DEV 134
42 * One option can be changed at boot time as follows:
43 * apm=on/off enable/disable APM
47 * Maximum number of events stored
49 #define APM_MAX_EVENTS 16
52 unsigned int event_head
;
53 unsigned int event_tail
;
54 apm_event_t events
[APM_MAX_EVENTS
];
58 * thread states (for threads using a writable /dev/apm_bios fd):
60 * SUSPEND_NONE: nothing happening
61 * SUSPEND_PENDING: suspend event queued for thread and pending to be read
62 * SUSPEND_READ: suspend event read, pending acknowledgement
63 * SUSPEND_ACKED: acknowledgement received from thread (via ioctl),
65 * SUSPEND_ACKTO: acknowledgement timeout
66 * SUSPEND_DONE: thread had acked suspend and is now notified of
69 * SUSPEND_WAIT: this thread invoked suspend and is waiting for resume
71 * A thread migrates in one of three paths:
72 * NONE -1-> PENDING -2-> READ -3-> ACKED -4-> DONE -5-> NONE
73 * -6-> ACKTO -7-> NONE
74 * NONE -8-> WAIT -9-> NONE
76 * While in PENDING or READ, the thread is accounted for in the
77 * suspend_acks_pending counter.
79 * The transitions are invoked as follows:
80 * 1: suspend event is signalled from the core PM code
81 * 2: the suspend event is read from the fd by the userspace thread
82 * 3: userspace thread issues the APM_IOC_SUSPEND ioctl (as ack)
83 * 4: core PM code signals that we have resumed
84 * 5: APM_IOC_SUSPEND ioctl returns
86 * 6: the notifier invoked from the core PM code timed out waiting
87 * for all relevant threds to enter ACKED state and puts those
88 * that haven't into ACKTO
89 * 7: those threads issue APM_IOC_SUSPEND ioctl too late,
92 * 8: userspace thread issues the APM_IOC_SUSPEND ioctl (to suspend),
93 * ioctl code invokes pm_suspend()
94 * 9: pm_suspend() returns indicating resume
96 enum apm_suspend_state
{
107 * The per-file APM data
110 struct list_head list
;
112 unsigned int suser
: 1;
113 unsigned int writer
: 1;
114 unsigned int reader
: 1;
117 enum apm_suspend_state suspend_state
;
119 struct apm_queue queue
;
125 static atomic_t suspend_acks_pending
= ATOMIC_INIT(0);
126 static atomic_t userspace_notification_inhibit
= ATOMIC_INIT(0);
127 static int apm_disabled
;
128 static struct task_struct
*kapmd_tsk
;
130 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue
);
131 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue
);
134 * This is a list of everyone who has opened /dev/apm_bios
136 static DECLARE_RWSEM(user_list_lock
);
137 static LIST_HEAD(apm_user_list
);
140 * kapmd info. kapmd provides us a process context to handle
141 * "APM" events within - specifically necessary if we're going
142 * to be suspending the system.
144 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait
);
145 static DEFINE_SPINLOCK(kapmd_queue_lock
);
146 static struct apm_queue kapmd_queue
;
148 static DEFINE_MUTEX(state_lock
);
150 static const char driver_version
[] = "1.13"; /* no spaces */
155 * Compatibility cruft until the IPAQ people move over to the new
158 static void __apm_get_power_status(struct apm_power_info
*info
)
163 * This allows machines to provide their own "apm get power status" function.
165 void (*apm_get_power_status
)(struct apm_power_info
*) = __apm_get_power_status
;
166 EXPORT_SYMBOL(apm_get_power_status
);
170 * APM event queue management.
172 static inline int queue_empty(struct apm_queue
*q
)
174 return q
->event_head
== q
->event_tail
;
177 static inline apm_event_t
queue_get_event(struct apm_queue
*q
)
179 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
180 return q
->events
[q
->event_tail
];
183 static void queue_add_event(struct apm_queue
*q
, apm_event_t event
)
185 q
->event_head
= (q
->event_head
+ 1) % APM_MAX_EVENTS
;
186 if (q
->event_head
== q
->event_tail
) {
190 printk(KERN_ERR
"apm: an event queue overflowed\n");
191 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
193 q
->events
[q
->event_head
] = event
;
196 static void queue_event(apm_event_t event
)
200 down_read(&user_list_lock
);
201 list_for_each_entry(as
, &apm_user_list
, list
) {
203 queue_add_event(&as
->queue
, event
);
205 up_read(&user_list_lock
);
206 wake_up_interruptible(&apm_waitqueue
);
209 static ssize_t
apm_read(struct file
*fp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
211 struct apm_user
*as
= fp
->private_data
;
213 int i
= count
, ret
= 0;
215 if (count
< sizeof(apm_event_t
))
218 if (queue_empty(&as
->queue
) && fp
->f_flags
& O_NONBLOCK
)
221 wait_event_interruptible(apm_waitqueue
, !queue_empty(&as
->queue
));
223 while ((i
>= sizeof(event
)) && !queue_empty(&as
->queue
)) {
224 event
= queue_get_event(&as
->queue
);
227 if (copy_to_user(buf
, &event
, sizeof(event
)))
230 mutex_lock(&state_lock
);
231 if (as
->suspend_state
== SUSPEND_PENDING
&&
232 (event
== APM_SYS_SUSPEND
|| event
== APM_USER_SUSPEND
))
233 as
->suspend_state
= SUSPEND_READ
;
234 mutex_unlock(&state_lock
);
236 buf
+= sizeof(event
);
246 static unsigned int apm_poll(struct file
*fp
, poll_table
* wait
)
248 struct apm_user
*as
= fp
->private_data
;
250 poll_wait(fp
, &apm_waitqueue
, wait
);
251 return queue_empty(&as
->queue
) ? 0 : POLLIN
| POLLRDNORM
;
255 * apm_ioctl - handle APM ioctl
258 * This IOCTL is overloaded, and performs two functions. It is used to:
259 * - initiate a suspend
260 * - acknowledge a suspend read from /dev/apm_bios.
261 * Only when everyone who has opened /dev/apm_bios with write permission
262 * has acknowledge does the actual suspend happen.
265 apm_ioctl(struct file
*filp
, u_int cmd
, u_long arg
)
267 struct apm_user
*as
= filp
->private_data
;
270 if (!as
->suser
|| !as
->writer
)
274 case APM_IOC_SUSPEND
:
275 mutex_lock(&state_lock
);
277 as
->suspend_result
= -EINTR
;
279 switch (as
->suspend_state
) {
282 * If we read a suspend command from /dev/apm_bios,
283 * then the corresponding APM_IOC_SUSPEND ioctl is
284 * interpreted as an acknowledge.
286 as
->suspend_state
= SUSPEND_ACKED
;
287 atomic_dec(&suspend_acks_pending
);
288 mutex_unlock(&state_lock
);
291 * suspend_acks_pending changed, the notifier needs to
292 * be woken up for this
294 wake_up(&apm_suspend_waitqueue
);
297 * Wait for the suspend/resume to complete. If there
298 * are pending acknowledges, we wait here for them.
299 * wait_event_freezable() is interruptible and pending
300 * signal can cause busy looping. We aren't doing
301 * anything critical, chill a bit on each iteration.
303 while (wait_event_freezable(apm_suspend_waitqueue
,
304 as
->suspend_state
!= SUSPEND_ACKED
))
308 as
->suspend_result
= -ETIMEDOUT
;
309 mutex_unlock(&state_lock
);
312 as
->suspend_state
= SUSPEND_WAIT
;
313 mutex_unlock(&state_lock
);
316 * Otherwise it is a request to suspend the system.
317 * Just invoke pm_suspend(), we'll handle it from
318 * there via the notifier.
320 as
->suspend_result
= pm_suspend(PM_SUSPEND_MEM
);
323 mutex_lock(&state_lock
);
324 err
= as
->suspend_result
;
325 as
->suspend_state
= SUSPEND_NONE
;
326 mutex_unlock(&state_lock
);
333 static int apm_release(struct inode
* inode
, struct file
* filp
)
335 struct apm_user
*as
= filp
->private_data
;
337 filp
->private_data
= NULL
;
339 down_write(&user_list_lock
);
341 up_write(&user_list_lock
);
344 * We are now unhooked from the chain. As far as new
345 * events are concerned, we no longer exist.
347 mutex_lock(&state_lock
);
348 if (as
->suspend_state
== SUSPEND_PENDING
||
349 as
->suspend_state
== SUSPEND_READ
)
350 atomic_dec(&suspend_acks_pending
);
351 mutex_unlock(&state_lock
);
353 wake_up(&apm_suspend_waitqueue
);
359 static int apm_open(struct inode
* inode
, struct file
* filp
)
363 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
366 * XXX - this is a tiny bit broken, when we consider BSD
367 * process accounting. If the device is opened by root, we
368 * instantly flag that we used superuser privs. Who knows,
369 * we might close the device immediately without doing a
370 * privileged operation -- cevans
372 as
->suser
= capable(CAP_SYS_ADMIN
);
373 as
->writer
= (filp
->f_mode
& FMODE_WRITE
) == FMODE_WRITE
;
374 as
->reader
= (filp
->f_mode
& FMODE_READ
) == FMODE_READ
;
376 down_write(&user_list_lock
);
377 list_add(&as
->list
, &apm_user_list
);
378 up_write(&user_list_lock
);
380 filp
->private_data
= as
;
383 return as
? 0 : -ENOMEM
;
386 static const struct file_operations apm_bios_fops
= {
387 .owner
= THIS_MODULE
,
390 .unlocked_ioctl
= apm_ioctl
,
392 .release
= apm_release
,
393 .llseek
= noop_llseek
,
396 static struct miscdevice apm_device
= {
397 .minor
= APM_MINOR_DEV
,
399 .fops
= &apm_bios_fops
403 #ifdef CONFIG_PROC_FS
405 * Arguments, with symbols from linux/apm_bios.h.
407 * 0) Linux driver version (this will change if format changes)
408 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
409 * 2) APM flags from APM Installation Check (0x00):
410 * bit 0: APM_16_BIT_SUPPORT
411 * bit 1: APM_32_BIT_SUPPORT
412 * bit 2: APM_IDLE_SLOWS_CLOCK
413 * bit 3: APM_BIOS_DISABLED
414 * bit 4: APM_BIOS_DISENGAGED
418 * 0x02: On backup power (BIOS >= 1.1 only)
425 * 0x04: Selected battery not present (BIOS >= 1.2 only)
432 * bit 7: No system battery
434 * 6) Remaining battery life (percentage of charge):
437 * 7) Remaining battery life (time units):
438 * Number of remaining minutes or seconds
440 * 8) min = minutes; sec = seconds
442 static int proc_apm_show(struct seq_file
*m
, void *v
)
444 struct apm_power_info info
;
447 info
.ac_line_status
= 0xff;
448 info
.battery_status
= 0xff;
449 info
.battery_flag
= 0xff;
450 info
.battery_life
= -1;
454 if (apm_get_power_status
)
455 apm_get_power_status(&info
);
457 switch (info
.units
) {
458 default: units
= "?"; break;
459 case 0: units
= "min"; break;
460 case 1: units
= "sec"; break;
463 seq_printf(m
, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
464 driver_version
, APM_32_BIT_SUPPORT
,
465 info
.ac_line_status
, info
.battery_status
,
466 info
.battery_flag
, info
.battery_life
,
472 static int proc_apm_open(struct inode
*inode
, struct file
*file
)
474 return single_open(file
, proc_apm_show
, NULL
);
477 static const struct file_operations apm_proc_fops
= {
478 .owner
= THIS_MODULE
,
479 .open
= proc_apm_open
,
482 .release
= single_release
,
486 static int kapmd(void *arg
)
491 wait_event_interruptible(kapmd_wait
,
492 !queue_empty(&kapmd_queue
) || kthread_should_stop());
494 if (kthread_should_stop())
497 spin_lock_irq(&kapmd_queue_lock
);
499 if (!queue_empty(&kapmd_queue
))
500 event
= queue_get_event(&kapmd_queue
);
501 spin_unlock_irq(&kapmd_queue_lock
);
507 case APM_LOW_BATTERY
:
508 case APM_POWER_STATUS_CHANGE
:
512 case APM_USER_SUSPEND
:
513 case APM_SYS_SUSPEND
:
514 pm_suspend(PM_SUSPEND_MEM
);
517 case APM_CRITICAL_SUSPEND
:
518 atomic_inc(&userspace_notification_inhibit
);
519 pm_suspend(PM_SUSPEND_MEM
);
520 atomic_dec(&userspace_notification_inhibit
);
528 static int apm_suspend_notifier(struct notifier_block
*nb
,
534 unsigned long apm_event
;
536 /* short-cut emergency suspends */
537 if (atomic_read(&userspace_notification_inhibit
))
541 case PM_SUSPEND_PREPARE
:
542 case PM_HIBERNATION_PREPARE
:
543 apm_event
= (event
== PM_SUSPEND_PREPARE
) ?
544 APM_USER_SUSPEND
: APM_USER_HIBERNATION
;
546 * Queue an event to all "writer" users that we want
547 * to suspend and need their ack.
549 mutex_lock(&state_lock
);
550 down_read(&user_list_lock
);
552 list_for_each_entry(as
, &apm_user_list
, list
) {
553 if (as
->suspend_state
!= SUSPEND_WAIT
&& as
->reader
&&
554 as
->writer
&& as
->suser
) {
555 as
->suspend_state
= SUSPEND_PENDING
;
556 atomic_inc(&suspend_acks_pending
);
557 queue_add_event(&as
->queue
, apm_event
);
561 up_read(&user_list_lock
);
562 mutex_unlock(&state_lock
);
563 wake_up_interruptible(&apm_waitqueue
);
566 * Wait for the the suspend_acks_pending variable to drop to
567 * zero, meaning everybody acked the suspend event (or the
568 * process was killed.)
570 * If the app won't answer within a short while we assume it
571 * locked up and ignore it.
573 err
= wait_event_interruptible_timeout(
574 apm_suspend_waitqueue
,
575 atomic_read(&suspend_acks_pending
) == 0,
581 * Move anybody who timed out to "ack timeout" state.
583 * We could time out and the userspace does the ACK
584 * right after we time out but before we enter the
585 * locked section here, but that's fine.
587 mutex_lock(&state_lock
);
588 down_read(&user_list_lock
);
589 list_for_each_entry(as
, &apm_user_list
, list
) {
590 if (as
->suspend_state
== SUSPEND_PENDING
||
591 as
->suspend_state
== SUSPEND_READ
) {
592 as
->suspend_state
= SUSPEND_ACKTO
;
593 atomic_dec(&suspend_acks_pending
);
596 up_read(&user_list_lock
);
597 mutex_unlock(&state_lock
);
600 /* let suspend proceed */
604 /* interrupted by signal */
605 return notifier_from_errno(err
);
607 case PM_POST_SUSPEND
:
608 case PM_POST_HIBERNATION
:
609 apm_event
= (event
== PM_POST_SUSPEND
) ?
610 APM_NORMAL_RESUME
: APM_HIBERNATION_RESUME
;
612 * Anyone on the APM queues will think we're still suspended.
613 * Send a message so everyone knows we're now awake again.
615 queue_event(apm_event
);
618 * Finally, wake up anyone who is sleeping on the suspend.
620 mutex_lock(&state_lock
);
621 down_read(&user_list_lock
);
622 list_for_each_entry(as
, &apm_user_list
, list
) {
623 if (as
->suspend_state
== SUSPEND_ACKED
) {
625 * TODO: maybe grab error code, needs core
626 * changes to push the error to the notifier
627 * chain (could use the second parameter if
630 as
->suspend_result
= 0;
631 as
->suspend_state
= SUSPEND_DONE
;
634 up_read(&user_list_lock
);
635 mutex_unlock(&state_lock
);
637 wake_up(&apm_suspend_waitqueue
);
645 static struct notifier_block apm_notif_block
= {
646 .notifier_call
= apm_suspend_notifier
,
649 static int __init
apm_init(void)
654 printk(KERN_NOTICE
"apm: disabled on user request.\n");
658 kapmd_tsk
= kthread_create(kapmd
, NULL
, "kapmd");
659 if (IS_ERR(kapmd_tsk
)) {
660 ret
= PTR_ERR(kapmd_tsk
);
664 wake_up_process(kapmd_tsk
);
666 #ifdef CONFIG_PROC_FS
667 proc_create("apm", 0, NULL
, &apm_proc_fops
);
670 ret
= misc_register(&apm_device
);
674 ret
= register_pm_notifier(&apm_notif_block
);
681 misc_deregister(&apm_device
);
683 remove_proc_entry("apm", NULL
);
684 kthread_stop(kapmd_tsk
);
689 static void __exit
apm_exit(void)
691 unregister_pm_notifier(&apm_notif_block
);
692 misc_deregister(&apm_device
);
693 remove_proc_entry("apm", NULL
);
695 kthread_stop(kapmd_tsk
);
698 module_init(apm_init
);
699 module_exit(apm_exit
);
701 MODULE_AUTHOR("Stephen Rothwell");
702 MODULE_DESCRIPTION("Advanced Power Management");
703 MODULE_LICENSE("GPL");
706 static int __init
apm_setup(char *str
)
708 while ((str
!= NULL
) && (*str
!= '\0')) {
709 if (strncmp(str
, "off", 3) == 0)
711 if (strncmp(str
, "on", 2) == 0)
713 str
= strchr(str
, ',');
715 str
+= strspn(str
, ", \t");
720 __setup("apm=", apm_setup
);
724 * apm_queue_event - queue an APM event for kapmd
727 * Queue an APM event for kapmd to process and ultimately take the
728 * appropriate action. Only a subset of events are handled:
730 * %APM_POWER_STATUS_CHANGE
733 * %APM_CRITICAL_SUSPEND
735 void apm_queue_event(apm_event_t event
)
739 spin_lock_irqsave(&kapmd_queue_lock
, flags
);
740 queue_add_event(&kapmd_queue
, event
);
741 spin_unlock_irqrestore(&kapmd_queue_lock
, flags
);
743 wake_up_interruptible(&kapmd_wait
);
745 EXPORT_SYMBOL(apm_queue_event
);