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/hwdev/busbios/amp_12.htm]
13 #include <linux/module.h>
14 #include <linux/poll.h>
15 #include <linux/slab.h>
16 #include <linux/smp_lock.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 atomic_t suspend_acks_pending = ATOMIC_INIT(0);
130 static atomic_t userspace_notification_inhibit = ATOMIC_INIT(0);
131 static int apm_disabled;
132 static struct task_struct *kapmd_tsk;
134 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
135 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
138 * This is a list of everyone who has opened /dev/apm_bios
140 static DECLARE_RWSEM(user_list_lock);
141 static LIST_HEAD(apm_user_list);
144 * kapmd info. kapmd provides us a process context to handle
145 * "APM" events within - specifically necessary if we're going
146 * to be suspending the system.
148 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
149 static DEFINE_SPINLOCK(kapmd_queue_lock);
150 static struct apm_queue kapmd_queue;
152 static DEFINE_MUTEX(state_lock);
154 static const char driver_version[] = "1.13"; /* no spaces */
159 * Compatibility cruft until the IPAQ people move over to the new
162 static void __apm_get_power_status(struct apm_power_info *info)
167 * This allows machines to provide their own "apm get power status" function.
169 void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
170 EXPORT_SYMBOL(apm_get_power_status);
174 * APM event queue management.
176 static inline int queue_empty(struct apm_queue *q)
178 return q->event_head == q->event_tail;
181 static inline apm_event_t queue_get_event(struct apm_queue *q)
183 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
184 return q->events[q->event_tail];
187 static void queue_add_event(struct apm_queue *q, apm_event_t event)
189 q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
190 if (q->event_head == q->event_tail) {
194 printk(KERN_ERR "apm: an event queue overflowed\n");
195 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
197 q->events[q->event_head] = event;
200 static void queue_event(apm_event_t event)
204 down_read(&user_list_lock);
205 list_for_each_entry(as, &apm_user_list, list) {
207 queue_add_event(&as->queue, event);
209 up_read(&user_list_lock);
210 wake_up_interruptible(&apm_waitqueue);
213 static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
215 struct apm_user *as = fp->private_data;
217 int i = count, ret = 0;
219 if (count < sizeof(apm_event_t))
222 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
225 wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
227 while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
228 event = queue_get_event(&as->queue);
231 if (copy_to_user(buf, &event, sizeof(event)))
234 mutex_lock(&state_lock);
235 if (as->suspend_state == SUSPEND_PENDING &&
236 (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND))
237 as->suspend_state = SUSPEND_READ;
238 mutex_unlock(&state_lock);
240 buf += sizeof(event);
250 static unsigned int apm_poll(struct file *fp, poll_table * wait)
252 struct apm_user *as = fp->private_data;
254 poll_wait(fp, &apm_waitqueue, wait);
255 return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;
259 * apm_ioctl - handle APM ioctl
262 * This IOCTL is overloaded, and performs two functions. It is used to:
263 * - initiate a suspend
264 * - acknowledge a suspend read from /dev/apm_bios.
265 * Only when everyone who has opened /dev/apm_bios with write permission
266 * has acknowledge does the actual suspend happen.
269 apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
271 struct apm_user *as = filp->private_data;
274 if (!as->suser || !as->writer)
278 case APM_IOC_SUSPEND:
279 mutex_lock(&state_lock);
281 as->suspend_result = -EINTR;
283 switch (as->suspend_state) {
286 * If we read a suspend command from /dev/apm_bios,
287 * then the corresponding APM_IOC_SUSPEND ioctl is
288 * interpreted as an acknowledge.
290 as->suspend_state = SUSPEND_ACKED;
291 atomic_dec(&suspend_acks_pending);
292 mutex_unlock(&state_lock);
295 * suspend_acks_pending changed, the notifier needs to
296 * be woken up for this
298 wake_up(&apm_suspend_waitqueue);
301 * Wait for the suspend/resume to complete. If there
302 * are pending acknowledges, we wait here for them.
304 freezer_do_not_count();
306 wait_event(apm_suspend_waitqueue,
307 as->suspend_state == SUSPEND_DONE);
310 * Since we are waiting until the suspend is done, the
311 * try_to_freeze() in freezer_count() will not trigger
316 as->suspend_result = -ETIMEDOUT;
317 mutex_unlock(&state_lock);
320 as->suspend_state = SUSPEND_WAIT;
321 mutex_unlock(&state_lock);
324 * Otherwise it is a request to suspend the system.
325 * Just invoke pm_suspend(), we'll handle it from
326 * there via the notifier.
328 as->suspend_result = pm_suspend(PM_SUSPEND_MEM);
331 mutex_lock(&state_lock);
332 err = as->suspend_result;
333 as->suspend_state = SUSPEND_NONE;
334 mutex_unlock(&state_lock);
341 static int apm_release(struct inode * inode, struct file * filp)
343 struct apm_user *as = filp->private_data;
345 filp->private_data = NULL;
347 down_write(&user_list_lock);
349 up_write(&user_list_lock);
352 * We are now unhooked from the chain. As far as new
353 * events are concerned, we no longer exist.
355 mutex_lock(&state_lock);
356 if (as->suspend_state == SUSPEND_PENDING ||
357 as->suspend_state == SUSPEND_READ)
358 atomic_dec(&suspend_acks_pending);
359 mutex_unlock(&state_lock);
361 wake_up(&apm_suspend_waitqueue);
367 static int apm_open(struct inode * inode, struct file * filp)
372 as = kzalloc(sizeof(*as), GFP_KERNEL);
375 * XXX - this is a tiny bit broken, when we consider BSD
376 * process accounting. If the device is opened by root, we
377 * instantly flag that we used superuser privs. Who knows,
378 * we might close the device immediately without doing a
379 * privileged operation -- cevans
381 as->suser = capable(CAP_SYS_ADMIN);
382 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
383 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
385 down_write(&user_list_lock);
386 list_add(&as->list, &apm_user_list);
387 up_write(&user_list_lock);
389 filp->private_data = as;
393 return as ? 0 : -ENOMEM;
396 static struct file_operations apm_bios_fops = {
397 .owner = THIS_MODULE,
402 .release = apm_release,
405 static struct miscdevice apm_device = {
406 .minor = APM_MINOR_DEV,
408 .fops = &apm_bios_fops
412 #ifdef CONFIG_PROC_FS
414 * Arguments, with symbols from linux/apm_bios.h.
416 * 0) Linux driver version (this will change if format changes)
417 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
418 * 2) APM flags from APM Installation Check (0x00):
419 * bit 0: APM_16_BIT_SUPPORT
420 * bit 1: APM_32_BIT_SUPPORT
421 * bit 2: APM_IDLE_SLOWS_CLOCK
422 * bit 3: APM_BIOS_DISABLED
423 * bit 4: APM_BIOS_DISENGAGED
427 * 0x02: On backup power (BIOS >= 1.1 only)
434 * 0x04: Selected battery not present (BIOS >= 1.2 only)
441 * bit 7: No system battery
443 * 6) Remaining battery life (percentage of charge):
446 * 7) Remaining battery life (time units):
447 * Number of remaining minutes or seconds
449 * 8) min = minutes; sec = seconds
451 static int proc_apm_show(struct seq_file *m, void *v)
453 struct apm_power_info info;
456 info.ac_line_status = 0xff;
457 info.battery_status = 0xff;
458 info.battery_flag = 0xff;
459 info.battery_life = -1;
463 if (apm_get_power_status)
464 apm_get_power_status(&info);
466 switch (info.units) {
467 default: units = "?"; break;
468 case 0: units = "min"; break;
469 case 1: units = "sec"; break;
472 seq_printf(m, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
473 driver_version, APM_32_BIT_SUPPORT,
474 info.ac_line_status, info.battery_status,
475 info.battery_flag, info.battery_life,
481 static int proc_apm_open(struct inode *inode, struct file *file)
483 return single_open(file, proc_apm_show, NULL);
486 static const struct file_operations apm_proc_fops = {
487 .owner = THIS_MODULE,
488 .open = proc_apm_open,
491 .release = single_release,
495 static int kapmd(void *arg)
500 wait_event_interruptible(kapmd_wait,
501 !queue_empty(&kapmd_queue) || kthread_should_stop());
503 if (kthread_should_stop())
506 spin_lock_irq(&kapmd_queue_lock);
508 if (!queue_empty(&kapmd_queue))
509 event = queue_get_event(&kapmd_queue);
510 spin_unlock_irq(&kapmd_queue_lock);
516 case APM_LOW_BATTERY:
517 case APM_POWER_STATUS_CHANGE:
521 case APM_USER_SUSPEND:
522 case APM_SYS_SUSPEND:
523 pm_suspend(PM_SUSPEND_MEM);
526 case APM_CRITICAL_SUSPEND:
527 atomic_inc(&userspace_notification_inhibit);
528 pm_suspend(PM_SUSPEND_MEM);
529 atomic_dec(&userspace_notification_inhibit);
537 static int apm_suspend_notifier(struct notifier_block *nb,
544 /* short-cut emergency suspends */
545 if (atomic_read(&userspace_notification_inhibit))
549 case PM_SUSPEND_PREPARE:
551 * Queue an event to all "writer" users that we want
552 * to suspend and need their ack.
554 mutex_lock(&state_lock);
555 down_read(&user_list_lock);
557 list_for_each_entry(as, &apm_user_list, list) {
558 if (as->suspend_state != SUSPEND_WAIT && as->reader &&
559 as->writer && as->suser) {
560 as->suspend_state = SUSPEND_PENDING;
561 atomic_inc(&suspend_acks_pending);
562 queue_add_event(&as->queue, APM_USER_SUSPEND);
566 up_read(&user_list_lock);
567 mutex_unlock(&state_lock);
568 wake_up_interruptible(&apm_waitqueue);
571 * Wait for the the suspend_acks_pending variable to drop to
572 * zero, meaning everybody acked the suspend event (or the
573 * process was killed.)
575 * If the app won't answer within a short while we assume it
576 * locked up and ignore it.
578 err = wait_event_interruptible_timeout(
579 apm_suspend_waitqueue,
580 atomic_read(&suspend_acks_pending) == 0,
586 * Move anybody who timed out to "ack timeout" state.
588 * We could time out and the userspace does the ACK
589 * right after we time out but before we enter the
590 * locked section here, but that's fine.
592 mutex_lock(&state_lock);
593 down_read(&user_list_lock);
594 list_for_each_entry(as, &apm_user_list, list) {
595 if (as->suspend_state == SUSPEND_PENDING ||
596 as->suspend_state == SUSPEND_READ) {
597 as->suspend_state = SUSPEND_ACKTO;
598 atomic_dec(&suspend_acks_pending);
601 up_read(&user_list_lock);
602 mutex_unlock(&state_lock);
605 /* let suspend proceed */
609 /* interrupted by signal */
612 case PM_POST_SUSPEND:
614 * Anyone on the APM queues will think we're still suspended.
615 * Send a message so everyone knows we're now awake again.
617 queue_event(APM_NORMAL_RESUME);
620 * Finally, wake up anyone who is sleeping on the suspend.
622 mutex_lock(&state_lock);
623 down_read(&user_list_lock);
624 list_for_each_entry(as, &apm_user_list, list) {
625 if (as->suspend_state == SUSPEND_ACKED) {
627 * TODO: maybe grab error code, needs core
628 * changes to push the error to the notifier
629 * chain (could use the second parameter if
632 as->suspend_result = 0;
633 as->suspend_state = SUSPEND_DONE;
636 up_read(&user_list_lock);
637 mutex_unlock(&state_lock);
639 wake_up(&apm_suspend_waitqueue);
647 static struct notifier_block apm_notif_block = {
648 .notifier_call = apm_suspend_notifier,
651 static int __init apm_init(void)
656 printk(KERN_NOTICE "apm: disabled on user request.\n");
660 kapmd_tsk = kthread_create(kapmd, NULL, "kapmd");
661 if (IS_ERR(kapmd_tsk)) {
662 ret = PTR_ERR(kapmd_tsk);
666 wake_up_process(kapmd_tsk);
668 #ifdef CONFIG_PROC_FS
669 proc_create("apm", 0, NULL, &apm_proc_fops);
672 ret = misc_register(&apm_device);
676 ret = register_pm_notifier(&apm_notif_block);
683 misc_deregister(&apm_device);
685 remove_proc_entry("apm", NULL);
686 kthread_stop(kapmd_tsk);
691 static void __exit apm_exit(void)
693 unregister_pm_notifier(&apm_notif_block);
694 misc_deregister(&apm_device);
695 remove_proc_entry("apm", NULL);
697 kthread_stop(kapmd_tsk);
700 module_init(apm_init);
701 module_exit(apm_exit);
703 MODULE_AUTHOR("Stephen Rothwell");
704 MODULE_DESCRIPTION("Advanced Power Management");
705 MODULE_LICENSE("GPL");
708 static int __init apm_setup(char *str)
710 while ((str != NULL) && (*str != '\0')) {
711 if (strncmp(str, "off", 3) == 0)
713 if (strncmp(str, "on", 2) == 0)
715 str = strchr(str, ',');
717 str += strspn(str, ", \t");
722 __setup("apm=", apm_setup);
726 * apm_queue_event - queue an APM event for kapmd
729 * Queue an APM event for kapmd to process and ultimately take the
730 * appropriate action. Only a subset of events are handled:
732 * %APM_POWER_STATUS_CHANGE
735 * %APM_CRITICAL_SUSPEND
737 void apm_queue_event(apm_event_t event)
741 spin_lock_irqsave(&kapmd_queue_lock, flags);
742 queue_add_event(&kapmd_queue, event);
743 spin_unlock_irqrestore(&kapmd_queue_lock, flags);
745 wake_up_interruptible(&kapmd_wait);
747 EXPORT_SYMBOL(apm_queue_event);