spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / char / apm-emulation.c
blobf4837a893dfad64dcd0f49b5a73fb61f76b948b6
1 /*
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)
6 * APM 1.2 Reference:
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
52 struct apm_queue {
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),
65 * waiting for resume
66 * SUSPEND_ACKTO: acknowledgement timeout
67 * SUSPEND_DONE: thread had acked suspend and is now notified of
68 * resume
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,
91 * get an error
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 {
98 SUSPEND_NONE,
99 SUSPEND_PENDING,
100 SUSPEND_READ,
101 SUSPEND_ACKED,
102 SUSPEND_ACKTO,
103 SUSPEND_WAIT,
104 SUSPEND_DONE,
108 * The per-file APM data
110 struct apm_user {
111 struct list_head list;
113 unsigned int suser: 1;
114 unsigned int writer: 1;
115 unsigned int reader: 1;
117 int suspend_result;
118 enum apm_suspend_state suspend_state;
120 struct apm_queue queue;
124 * Local variables
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
157 * interface.
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) {
188 static int notified;
190 if (notified++ == 0)
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)
199 struct apm_user *as;
201 down_read(&user_list_lock);
202 list_for_each_entry(as, &apm_user_list, list) {
203 if (as->reader)
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;
213 apm_event_t event;
214 int i = count, ret = 0;
216 if (count < sizeof(apm_event_t))
217 return -EINVAL;
219 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
220 return -EAGAIN;
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);
227 ret = -EFAULT;
228 if (copy_to_user(buf, &event, sizeof(event)))
229 break;
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);
238 i -= sizeof(event);
241 if (i < count)
242 ret = count - i;
244 return ret;
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
258 * APM_IOC_SUSPEND
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.
265 static long
266 apm_ioctl(struct file *filp, u_int cmd, u_long arg)
268 struct apm_user *as = filp->private_data;
269 int err = -EINVAL;
271 if (!as->suser || !as->writer)
272 return -EPERM;
274 switch (cmd) {
275 case APM_IOC_SUSPEND:
276 mutex_lock(&state_lock);
278 as->suspend_result = -EINTR;
280 switch (as->suspend_state) {
281 case SUSPEND_READ:
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))
306 msleep(10);
307 break;
308 case SUSPEND_ACKTO:
309 as->suspend_result = -ETIMEDOUT;
310 mutex_unlock(&state_lock);
311 break;
312 default:
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);
328 break;
331 return err;
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);
341 list_del(&as->list);
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);
356 kfree(as);
357 return 0;
360 static int apm_open(struct inode * inode, struct file * filp)
362 struct apm_user *as;
364 as = kzalloc(sizeof(*as), GFP_KERNEL);
365 if (as) {
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,
389 .read = apm_read,
390 .poll = apm_poll,
391 .unlocked_ioctl = apm_ioctl,
392 .open = apm_open,
393 .release = apm_release,
394 .llseek = noop_llseek,
397 static struct miscdevice apm_device = {
398 .minor = APM_MINOR_DEV,
399 .name = "apm_bios",
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
416 * 3) AC line status
417 * 0x00: Off-line
418 * 0x01: On-line
419 * 0x02: On backup power (BIOS >= 1.1 only)
420 * 0xff: Unknown
421 * 4) Battery status
422 * 0x00: High
423 * 0x01: Low
424 * 0x02: Critical
425 * 0x03: Charging
426 * 0x04: Selected battery not present (BIOS >= 1.2 only)
427 * 0xff: Unknown
428 * 5) Battery flag
429 * bit 0: High
430 * bit 1: Low
431 * bit 2: Critical
432 * bit 3: Charging
433 * bit 7: No system battery
434 * 0xff: Unknown
435 * 6) Remaining battery life (percentage of charge):
436 * 0-100: valid
437 * -1: Unknown
438 * 7) Remaining battery life (time units):
439 * Number of remaining minutes or seconds
440 * -1: Unknown
441 * 8) min = minutes; sec = seconds
443 static int proc_apm_show(struct seq_file *m, void *v)
445 struct apm_power_info info;
446 char *units;
448 info.ac_line_status = 0xff;
449 info.battery_status = 0xff;
450 info.battery_flag = 0xff;
451 info.battery_life = -1;
452 info.time = -1;
453 info.units = -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,
468 info.time, units);
470 return 0;
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,
481 .read = seq_read,
482 .llseek = seq_lseek,
483 .release = single_release,
485 #endif
487 static int kapmd(void *arg)
489 do {
490 apm_event_t event;
492 wait_event_interruptible(kapmd_wait,
493 !queue_empty(&kapmd_queue) || kthread_should_stop());
495 if (kthread_should_stop())
496 break;
498 spin_lock_irq(&kapmd_queue_lock);
499 event = 0;
500 if (!queue_empty(&kapmd_queue))
501 event = queue_get_event(&kapmd_queue);
502 spin_unlock_irq(&kapmd_queue_lock);
504 switch (event) {
505 case 0:
506 break;
508 case APM_LOW_BATTERY:
509 case APM_POWER_STATUS_CHANGE:
510 queue_event(event);
511 break;
513 case APM_USER_SUSPEND:
514 case APM_SYS_SUSPEND:
515 pm_suspend(PM_SUSPEND_MEM);
516 break;
518 case APM_CRITICAL_SUSPEND:
519 atomic_inc(&userspace_notification_inhibit);
520 pm_suspend(PM_SUSPEND_MEM);
521 atomic_dec(&userspace_notification_inhibit);
522 break;
524 } while (1);
526 return 0;
529 static int apm_suspend_notifier(struct notifier_block *nb,
530 unsigned long event,
531 void *dummy)
533 struct apm_user *as;
534 int err;
536 /* short-cut emergency suspends */
537 if (atomic_read(&userspace_notification_inhibit))
538 return NOTIFY_DONE;
540 switch (event) {
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,
573 5*HZ);
575 /* timed out */
576 if (err == 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 */
598 if (err >= 0)
599 return NOTIFY_OK;
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
622 * implemented)
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);
632 return NOTIFY_OK;
634 default:
635 return NOTIFY_DONE;
639 static struct notifier_block apm_notif_block = {
640 .notifier_call = apm_suspend_notifier,
643 static int __init apm_init(void)
645 int ret;
647 if (apm_disabled) {
648 printk(KERN_NOTICE "apm: disabled on user request.\n");
649 return -ENODEV;
652 kapmd_tsk = kthread_create(kapmd, NULL, "kapmd");
653 if (IS_ERR(kapmd_tsk)) {
654 ret = PTR_ERR(kapmd_tsk);
655 kapmd_tsk = NULL;
656 goto out;
658 wake_up_process(kapmd_tsk);
660 #ifdef CONFIG_PROC_FS
661 proc_create("apm", 0, NULL, &apm_proc_fops);
662 #endif
664 ret = misc_register(&apm_device);
665 if (ret)
666 goto out_stop;
668 ret = register_pm_notifier(&apm_notif_block);
669 if (ret)
670 goto out_unregister;
672 return 0;
674 out_unregister:
675 misc_deregister(&apm_device);
676 out_stop:
677 remove_proc_entry("apm", NULL);
678 kthread_stop(kapmd_tsk);
679 out:
680 return ret;
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");
699 #ifndef MODULE
700 static int __init apm_setup(char *str)
702 while ((str != NULL) && (*str != '\0')) {
703 if (strncmp(str, "off", 3) == 0)
704 apm_disabled = 1;
705 if (strncmp(str, "on", 2) == 0)
706 apm_disabled = 0;
707 str = strchr(str, ',');
708 if (str != NULL)
709 str += strspn(str, ", \t");
711 return 1;
714 __setup("apm=", apm_setup);
715 #endif
718 * apm_queue_event - queue an APM event for kapmd
719 * @event: APM event
721 * Queue an APM event for kapmd to process and ultimately take the
722 * appropriate action. Only a subset of events are handled:
723 * %APM_LOW_BATTERY
724 * %APM_POWER_STATUS_CHANGE
725 * %APM_USER_SUSPEND
726 * %APM_SYS_SUSPEND
727 * %APM_CRITICAL_SUSPEND
729 void apm_queue_event(apm_event_t event)
731 unsigned long flags;
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);