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 * See Documentation/Config.help for the configuration options.
45 * Various options can be changed at boot time as follows:
46 * (We allow underscores for compatibility with the modules code)
47 * apm=on/off enable/disable APM
51 * Maximum number of events stored
53 #define APM_MAX_EVENTS 16
56 unsigned int event_head
;
57 unsigned int event_tail
;
58 apm_event_t events
[APM_MAX_EVENTS
];
62 * thread states (for threads using a writable /dev/apm_bios fd):
64 * SUSPEND_NONE: nothing happening
65 * SUSPEND_PENDING: suspend event queued for thread and pending to be read
66 * SUSPEND_READ: suspend event read, pending acknowledgement
67 * SUSPEND_ACKED: acknowledgement received from thread (via ioctl),
69 * SUSPEND_ACKTO: acknowledgement timeout
70 * SUSPEND_DONE: thread had acked suspend and is now notified of
73 * SUSPEND_WAIT: this thread invoked suspend and is waiting for resume
75 * A thread migrates in one of three paths:
76 * NONE -1-> PENDING -2-> READ -3-> ACKED -4-> DONE -5-> NONE
77 * -6-> ACKTO -7-> NONE
78 * NONE -8-> WAIT -9-> NONE
80 * While in PENDING or READ, the thread is accounted for in the
81 * suspend_acks_pending counter.
83 * The transitions are invoked as follows:
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
86 * 3: userspace thread issues the APM_IOC_SUSPEND ioctl (as ack)
87 * 4: core PM code signals that we have resumed
88 * 5: APM_IOC_SUSPEND ioctl returns
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
92 * that haven't into ACKTO
93 * 7: those threads issue APM_IOC_SUSPEND ioctl too late,
96 * 8: userspace thread issues the APM_IOC_SUSPEND ioctl (to suspend),
97 * ioctl code invokes pm_suspend()
98 * 9: pm_suspend() returns indicating resume
100 enum apm_suspend_state
{
111 * The per-file APM data
114 struct list_head list
;
116 unsigned int suser
: 1;
117 unsigned int writer
: 1;
118 unsigned int reader
: 1;
121 enum apm_suspend_state suspend_state
;
123 struct apm_queue queue
;
129 static DEFINE_MUTEX(apm_mutex
);
130 static atomic_t suspend_acks_pending
= ATOMIC_INIT(0);
131 static atomic_t userspace_notification_inhibit
= ATOMIC_INIT(0);
132 static int apm_disabled
;
133 static struct task_struct
*kapmd_tsk
;
135 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue
);
136 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue
);
139 * This is a list of everyone who has opened /dev/apm_bios
141 static DECLARE_RWSEM(user_list_lock
);
142 static LIST_HEAD(apm_user_list
);
145 * kapmd info. kapmd provides us a process context to handle
146 * "APM" events within - specifically necessary if we're going
147 * to be suspending the system.
149 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait
);
150 static DEFINE_SPINLOCK(kapmd_queue_lock
);
151 static struct apm_queue kapmd_queue
;
153 static DEFINE_MUTEX(state_lock
);
155 static const char driver_version
[] = "1.13"; /* no spaces */
160 * Compatibility cruft until the IPAQ people move over to the new
163 static void __apm_get_power_status(struct apm_power_info
*info
)
168 * This allows machines to provide their own "apm get power status" function.
170 void (*apm_get_power_status
)(struct apm_power_info
*) = __apm_get_power_status
;
171 EXPORT_SYMBOL(apm_get_power_status
);
175 * APM event queue management.
177 static inline int queue_empty(struct apm_queue
*q
)
179 return q
->event_head
== q
->event_tail
;
182 static inline apm_event_t
queue_get_event(struct apm_queue
*q
)
184 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
185 return q
->events
[q
->event_tail
];
188 static void queue_add_event(struct apm_queue
*q
, apm_event_t event
)
190 q
->event_head
= (q
->event_head
+ 1) % APM_MAX_EVENTS
;
191 if (q
->event_head
== q
->event_tail
) {
195 printk(KERN_ERR
"apm: an event queue overflowed\n");
196 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
198 q
->events
[q
->event_head
] = event
;
201 static void queue_event(apm_event_t event
)
205 down_read(&user_list_lock
);
206 list_for_each_entry(as
, &apm_user_list
, list
) {
208 queue_add_event(&as
->queue
, event
);
210 up_read(&user_list_lock
);
211 wake_up_interruptible(&apm_waitqueue
);
214 static ssize_t
apm_read(struct file
*fp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
216 struct apm_user
*as
= fp
->private_data
;
218 int i
= count
, ret
= 0;
220 if (count
< sizeof(apm_event_t
))
223 if (queue_empty(&as
->queue
) && fp
->f_flags
& O_NONBLOCK
)
226 wait_event_interruptible(apm_waitqueue
, !queue_empty(&as
->queue
));
228 while ((i
>= sizeof(event
)) && !queue_empty(&as
->queue
)) {
229 event
= queue_get_event(&as
->queue
);
232 if (copy_to_user(buf
, &event
, sizeof(event
)))
235 mutex_lock(&state_lock
);
236 if (as
->suspend_state
== SUSPEND_PENDING
&&
237 (event
== APM_SYS_SUSPEND
|| event
== APM_USER_SUSPEND
))
238 as
->suspend_state
= SUSPEND_READ
;
239 mutex_unlock(&state_lock
);
241 buf
+= sizeof(event
);
251 static unsigned int apm_poll(struct file
*fp
, poll_table
* wait
)
253 struct apm_user
*as
= fp
->private_data
;
255 poll_wait(fp
, &apm_waitqueue
, wait
);
256 return queue_empty(&as
->queue
) ? 0 : POLLIN
| POLLRDNORM
;
260 * apm_ioctl - handle APM ioctl
263 * This IOCTL is overloaded, and performs two functions. It is used to:
264 * - initiate a suspend
265 * - acknowledge a suspend read from /dev/apm_bios.
266 * Only when everyone who has opened /dev/apm_bios with write permission
267 * has acknowledge does the actual suspend happen.
270 apm_ioctl(struct file
*filp
, u_int cmd
, u_long arg
)
272 struct apm_user
*as
= filp
->private_data
;
275 if (!as
->suser
|| !as
->writer
)
278 mutex_lock(&apm_mutex
);
280 case APM_IOC_SUSPEND
:
281 mutex_lock(&state_lock
);
283 as
->suspend_result
= -EINTR
;
285 switch (as
->suspend_state
) {
288 * If we read a suspend command from /dev/apm_bios,
289 * then the corresponding APM_IOC_SUSPEND ioctl is
290 * interpreted as an acknowledge.
292 as
->suspend_state
= SUSPEND_ACKED
;
293 atomic_dec(&suspend_acks_pending
);
294 mutex_unlock(&state_lock
);
297 * suspend_acks_pending changed, the notifier needs to
298 * be woken up for this
300 wake_up(&apm_suspend_waitqueue
);
303 * Wait for the suspend/resume to complete. If there
304 * are pending acknowledges, we wait here for them.
306 freezer_do_not_count();
308 wait_event(apm_suspend_waitqueue
,
309 as
->suspend_state
== SUSPEND_DONE
);
312 * Since we are waiting until the suspend is done, the
313 * try_to_freeze() in freezer_count() will not trigger
318 as
->suspend_result
= -ETIMEDOUT
;
319 mutex_unlock(&state_lock
);
322 as
->suspend_state
= SUSPEND_WAIT
;
323 mutex_unlock(&state_lock
);
326 * Otherwise it is a request to suspend the system.
327 * Just invoke pm_suspend(), we'll handle it from
328 * there via the notifier.
330 as
->suspend_result
= pm_suspend(PM_SUSPEND_MEM
);
333 mutex_lock(&state_lock
);
334 err
= as
->suspend_result
;
335 as
->suspend_state
= SUSPEND_NONE
;
336 mutex_unlock(&state_lock
);
339 mutex_unlock(&apm_mutex
);
344 static int apm_release(struct inode
* inode
, struct file
* filp
)
346 struct apm_user
*as
= filp
->private_data
;
348 filp
->private_data
= NULL
;
350 down_write(&user_list_lock
);
352 up_write(&user_list_lock
);
355 * We are now unhooked from the chain. As far as new
356 * events are concerned, we no longer exist.
358 mutex_lock(&state_lock
);
359 if (as
->suspend_state
== SUSPEND_PENDING
||
360 as
->suspend_state
== SUSPEND_READ
)
361 atomic_dec(&suspend_acks_pending
);
362 mutex_unlock(&state_lock
);
364 wake_up(&apm_suspend_waitqueue
);
370 static int apm_open(struct inode
* inode
, struct file
* filp
)
374 mutex_lock(&apm_mutex
);
375 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
378 * XXX - this is a tiny bit broken, when we consider BSD
379 * process accounting. If the device is opened by root, we
380 * instantly flag that we used superuser privs. Who knows,
381 * we might close the device immediately without doing a
382 * privileged operation -- cevans
384 as
->suser
= capable(CAP_SYS_ADMIN
);
385 as
->writer
= (filp
->f_mode
& FMODE_WRITE
) == FMODE_WRITE
;
386 as
->reader
= (filp
->f_mode
& FMODE_READ
) == FMODE_READ
;
388 down_write(&user_list_lock
);
389 list_add(&as
->list
, &apm_user_list
);
390 up_write(&user_list_lock
);
392 filp
->private_data
= as
;
394 mutex_unlock(&apm_mutex
);
396 return as
? 0 : -ENOMEM
;
399 static const struct file_operations apm_bios_fops
= {
400 .owner
= THIS_MODULE
,
403 .unlocked_ioctl
= apm_ioctl
,
405 .release
= apm_release
,
406 .llseek
= noop_llseek
,
409 static struct miscdevice apm_device
= {
410 .minor
= APM_MINOR_DEV
,
412 .fops
= &apm_bios_fops
416 #ifdef CONFIG_PROC_FS
418 * Arguments, with symbols from linux/apm_bios.h.
420 * 0) Linux driver version (this will change if format changes)
421 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
422 * 2) APM flags from APM Installation Check (0x00):
423 * bit 0: APM_16_BIT_SUPPORT
424 * bit 1: APM_32_BIT_SUPPORT
425 * bit 2: APM_IDLE_SLOWS_CLOCK
426 * bit 3: APM_BIOS_DISABLED
427 * bit 4: APM_BIOS_DISENGAGED
431 * 0x02: On backup power (BIOS >= 1.1 only)
438 * 0x04: Selected battery not present (BIOS >= 1.2 only)
445 * bit 7: No system battery
447 * 6) Remaining battery life (percentage of charge):
450 * 7) Remaining battery life (time units):
451 * Number of remaining minutes or seconds
453 * 8) min = minutes; sec = seconds
455 static int proc_apm_show(struct seq_file
*m
, void *v
)
457 struct apm_power_info info
;
460 info
.ac_line_status
= 0xff;
461 info
.battery_status
= 0xff;
462 info
.battery_flag
= 0xff;
463 info
.battery_life
= -1;
467 if (apm_get_power_status
)
468 apm_get_power_status(&info
);
470 switch (info
.units
) {
471 default: units
= "?"; break;
472 case 0: units
= "min"; break;
473 case 1: units
= "sec"; break;
476 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
,
478 info
.ac_line_status
, info
.battery_status
,
479 info
.battery_flag
, info
.battery_life
,
485 static int proc_apm_open(struct inode
*inode
, struct file
*file
)
487 return single_open(file
, proc_apm_show
, NULL
);
490 static const struct file_operations apm_proc_fops
= {
491 .owner
= THIS_MODULE
,
492 .open
= proc_apm_open
,
495 .release
= single_release
,
499 static int kapmd(void *arg
)
504 wait_event_interruptible(kapmd_wait
,
505 !queue_empty(&kapmd_queue
) || kthread_should_stop());
507 if (kthread_should_stop())
510 spin_lock_irq(&kapmd_queue_lock
);
512 if (!queue_empty(&kapmd_queue
))
513 event
= queue_get_event(&kapmd_queue
);
514 spin_unlock_irq(&kapmd_queue_lock
);
520 case APM_LOW_BATTERY
:
521 case APM_POWER_STATUS_CHANGE
:
525 case APM_USER_SUSPEND
:
526 case APM_SYS_SUSPEND
:
527 pm_suspend(PM_SUSPEND_MEM
);
530 case APM_CRITICAL_SUSPEND
:
531 atomic_inc(&userspace_notification_inhibit
);
532 pm_suspend(PM_SUSPEND_MEM
);
533 atomic_dec(&userspace_notification_inhibit
);
541 static int apm_suspend_notifier(struct notifier_block
*nb
,
548 /* short-cut emergency suspends */
549 if (atomic_read(&userspace_notification_inhibit
))
553 case PM_SUSPEND_PREPARE
:
555 * Queue an event to all "writer" users that we want
556 * to suspend and need their ack.
558 mutex_lock(&state_lock
);
559 down_read(&user_list_lock
);
561 list_for_each_entry(as
, &apm_user_list
, list
) {
562 if (as
->suspend_state
!= SUSPEND_WAIT
&& as
->reader
&&
563 as
->writer
&& as
->suser
) {
564 as
->suspend_state
= SUSPEND_PENDING
;
565 atomic_inc(&suspend_acks_pending
);
566 queue_add_event(&as
->queue
, APM_USER_SUSPEND
);
570 up_read(&user_list_lock
);
571 mutex_unlock(&state_lock
);
572 wake_up_interruptible(&apm_waitqueue
);
575 * Wait for the the suspend_acks_pending variable to drop to
576 * zero, meaning everybody acked the suspend event (or the
577 * process was killed.)
579 * If the app won't answer within a short while we assume it
580 * locked up and ignore it.
582 err
= wait_event_interruptible_timeout(
583 apm_suspend_waitqueue
,
584 atomic_read(&suspend_acks_pending
) == 0,
590 * Move anybody who timed out to "ack timeout" state.
592 * We could time out and the userspace does the ACK
593 * right after we time out but before we enter the
594 * locked section here, but that's fine.
596 mutex_lock(&state_lock
);
597 down_read(&user_list_lock
);
598 list_for_each_entry(as
, &apm_user_list
, list
) {
599 if (as
->suspend_state
== SUSPEND_PENDING
||
600 as
->suspend_state
== SUSPEND_READ
) {
601 as
->suspend_state
= SUSPEND_ACKTO
;
602 atomic_dec(&suspend_acks_pending
);
605 up_read(&user_list_lock
);
606 mutex_unlock(&state_lock
);
609 /* let suspend proceed */
613 /* interrupted by signal */
616 case PM_POST_SUSPEND
:
618 * Anyone on the APM queues will think we're still suspended.
619 * Send a message so everyone knows we're now awake again.
621 queue_event(APM_NORMAL_RESUME
);
624 * Finally, wake up anyone who is sleeping on the suspend.
626 mutex_lock(&state_lock
);
627 down_read(&user_list_lock
);
628 list_for_each_entry(as
, &apm_user_list
, list
) {
629 if (as
->suspend_state
== SUSPEND_ACKED
) {
631 * TODO: maybe grab error code, needs core
632 * changes to push the error to the notifier
633 * chain (could use the second parameter if
636 as
->suspend_result
= 0;
637 as
->suspend_state
= SUSPEND_DONE
;
640 up_read(&user_list_lock
);
641 mutex_unlock(&state_lock
);
643 wake_up(&apm_suspend_waitqueue
);
651 static struct notifier_block apm_notif_block
= {
652 .notifier_call
= apm_suspend_notifier
,
655 static int __init
apm_init(void)
660 printk(KERN_NOTICE
"apm: disabled on user request.\n");
664 kapmd_tsk
= kthread_create(kapmd
, NULL
, "kapmd");
665 if (IS_ERR(kapmd_tsk
)) {
666 ret
= PTR_ERR(kapmd_tsk
);
670 wake_up_process(kapmd_tsk
);
672 #ifdef CONFIG_PROC_FS
673 proc_create("apm", 0, NULL
, &apm_proc_fops
);
676 ret
= misc_register(&apm_device
);
680 ret
= register_pm_notifier(&apm_notif_block
);
687 misc_deregister(&apm_device
);
689 remove_proc_entry("apm", NULL
);
690 kthread_stop(kapmd_tsk
);
695 static void __exit
apm_exit(void)
697 unregister_pm_notifier(&apm_notif_block
);
698 misc_deregister(&apm_device
);
699 remove_proc_entry("apm", NULL
);
701 kthread_stop(kapmd_tsk
);
704 module_init(apm_init
);
705 module_exit(apm_exit
);
707 MODULE_AUTHOR("Stephen Rothwell");
708 MODULE_DESCRIPTION("Advanced Power Management");
709 MODULE_LICENSE("GPL");
712 static int __init
apm_setup(char *str
)
714 while ((str
!= NULL
) && (*str
!= '\0')) {
715 if (strncmp(str
, "off", 3) == 0)
717 if (strncmp(str
, "on", 2) == 0)
719 str
= strchr(str
, ',');
721 str
+= strspn(str
, ", \t");
726 __setup("apm=", apm_setup
);
730 * apm_queue_event - queue an APM event for kapmd
733 * Queue an APM event for kapmd to process and ultimately take the
734 * appropriate action. Only a subset of events are handled:
736 * %APM_POWER_STATUS_CHANGE
739 * %APM_CRITICAL_SUSPEND
741 void apm_queue_event(apm_event_t event
)
745 spin_lock_irqsave(&kapmd_queue_lock
, flags
);
746 queue_add_event(&kapmd_queue
, event
);
747 spin_unlock_irqrestore(&kapmd_queue_lock
, flags
);
749 wake_up_interruptible(&kapmd_wait
);
751 EXPORT_SYMBOL(apm_queue_event
);