[MIPS] remove machtype for group Toshiba
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / apm-emulation.c
blobda8a1658a273b6253172b0622631796c4af137f0
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/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
55 struct apm_queue {
56 unsigned int event_head;
57 unsigned int event_tail;
58 apm_event_t events[APM_MAX_EVENTS];
62 * The per-file APM data
64 struct apm_user {
65 struct list_head list;
67 unsigned int suser: 1;
68 unsigned int writer: 1;
69 unsigned int reader: 1;
71 int suspend_result;
72 unsigned int suspend_state;
73 #define SUSPEND_NONE 0 /* no suspend pending */
74 #define SUSPEND_PENDING 1 /* suspend pending read */
75 #define SUSPEND_READ 2 /* suspend read, pending ack */
76 #define SUSPEND_ACKED 3 /* suspend acked */
77 #define SUSPEND_WAIT 4 /* waiting for suspend */
78 #define SUSPEND_DONE 5 /* suspend completed */
80 struct apm_queue queue;
84 * Local variables
86 static int suspends_pending;
87 static int apm_disabled;
88 static struct task_struct *kapmd_tsk;
90 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
91 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
94 * This is a list of everyone who has opened /dev/apm_bios
96 static DECLARE_RWSEM(user_list_lock);
97 static LIST_HEAD(apm_user_list);
100 * kapmd info. kapmd provides us a process context to handle
101 * "APM" events within - specifically necessary if we're going
102 * to be suspending the system.
104 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
105 static DEFINE_SPINLOCK(kapmd_queue_lock);
106 static struct apm_queue kapmd_queue;
108 static DEFINE_MUTEX(state_lock);
110 static const char driver_version[] = "1.13"; /* no spaces */
115 * Compatibility cruft until the IPAQ people move over to the new
116 * interface.
118 static void __apm_get_power_status(struct apm_power_info *info)
123 * This allows machines to provide their own "apm get power status" function.
125 void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
126 EXPORT_SYMBOL(apm_get_power_status);
130 * APM event queue management.
132 static inline int queue_empty(struct apm_queue *q)
134 return q->event_head == q->event_tail;
137 static inline apm_event_t queue_get_event(struct apm_queue *q)
139 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
140 return q->events[q->event_tail];
143 static void queue_add_event(struct apm_queue *q, apm_event_t event)
145 q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
146 if (q->event_head == q->event_tail) {
147 static int notified;
149 if (notified++ == 0)
150 printk(KERN_ERR "apm: an event queue overflowed\n");
151 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
153 q->events[q->event_head] = event;
156 static void queue_event(apm_event_t event)
158 struct apm_user *as;
160 down_read(&user_list_lock);
161 list_for_each_entry(as, &apm_user_list, list) {
162 if (as->reader)
163 queue_add_event(&as->queue, event);
165 up_read(&user_list_lock);
166 wake_up_interruptible(&apm_waitqueue);
170 * queue_suspend_event - queue an APM suspend event.
172 * Check that we're in a state where we can suspend. If not,
173 * return -EBUSY. Otherwise, queue an event to all "writer"
174 * users. If there are no "writer" users, return '1' to
175 * indicate that we can immediately suspend.
177 static int queue_suspend_event(apm_event_t event, struct apm_user *sender)
179 struct apm_user *as;
180 int ret = 1;
182 mutex_lock(&state_lock);
183 down_read(&user_list_lock);
186 * If a thread is still processing, we can't suspend, so reject
187 * the request.
189 list_for_each_entry(as, &apm_user_list, list) {
190 if (as != sender && as->reader && as->writer && as->suser &&
191 as->suspend_state != SUSPEND_NONE) {
192 ret = -EBUSY;
193 goto out;
197 list_for_each_entry(as, &apm_user_list, list) {
198 if (as != sender && as->reader && as->writer && as->suser) {
199 as->suspend_state = SUSPEND_PENDING;
200 suspends_pending++;
201 queue_add_event(&as->queue, event);
202 ret = 0;
205 out:
206 up_read(&user_list_lock);
207 mutex_unlock(&state_lock);
208 wake_up_interruptible(&apm_waitqueue);
209 return ret;
212 static void apm_suspend(void)
214 struct apm_user *as;
215 int err = pm_suspend(PM_SUSPEND_MEM);
218 * Anyone on the APM queues will think we're still suspended.
219 * Send a message so everyone knows we're now awake again.
221 queue_event(APM_NORMAL_RESUME);
224 * Finally, wake up anyone who is sleeping on the suspend.
226 mutex_lock(&state_lock);
227 down_read(&user_list_lock);
228 list_for_each_entry(as, &apm_user_list, list) {
229 if (as->suspend_state == SUSPEND_WAIT ||
230 as->suspend_state == SUSPEND_ACKED) {
231 as->suspend_result = err;
232 as->suspend_state = SUSPEND_DONE;
235 up_read(&user_list_lock);
236 mutex_unlock(&state_lock);
238 wake_up(&apm_suspend_waitqueue);
241 static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
243 struct apm_user *as = fp->private_data;
244 apm_event_t event;
245 int i = count, ret = 0;
247 if (count < sizeof(apm_event_t))
248 return -EINVAL;
250 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
251 return -EAGAIN;
253 wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
255 while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
256 event = queue_get_event(&as->queue);
258 ret = -EFAULT;
259 if (copy_to_user(buf, &event, sizeof(event)))
260 break;
262 mutex_lock(&state_lock);
263 if (as->suspend_state == SUSPEND_PENDING &&
264 (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND))
265 as->suspend_state = SUSPEND_READ;
266 mutex_unlock(&state_lock);
268 buf += sizeof(event);
269 i -= sizeof(event);
272 if (i < count)
273 ret = count - i;
275 return ret;
278 static unsigned int apm_poll(struct file *fp, poll_table * wait)
280 struct apm_user *as = fp->private_data;
282 poll_wait(fp, &apm_waitqueue, wait);
283 return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;
287 * apm_ioctl - handle APM ioctl
289 * APM_IOC_SUSPEND
290 * This IOCTL is overloaded, and performs two functions. It is used to:
291 * - initiate a suspend
292 * - acknowledge a suspend read from /dev/apm_bios.
293 * Only when everyone who has opened /dev/apm_bios with write permission
294 * has acknowledge does the actual suspend happen.
296 static int
297 apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
299 struct apm_user *as = filp->private_data;
300 int err = -EINVAL;
302 if (!as->suser || !as->writer)
303 return -EPERM;
305 switch (cmd) {
306 case APM_IOC_SUSPEND:
307 mutex_lock(&state_lock);
309 as->suspend_result = -EINTR;
311 if (as->suspend_state == SUSPEND_READ) {
312 int pending;
315 * If we read a suspend command from /dev/apm_bios,
316 * then the corresponding APM_IOC_SUSPEND ioctl is
317 * interpreted as an acknowledge.
319 as->suspend_state = SUSPEND_ACKED;
320 suspends_pending--;
321 pending = suspends_pending == 0;
322 mutex_unlock(&state_lock);
325 * If there are no further acknowledges required,
326 * suspend the system.
328 if (pending)
329 apm_suspend();
332 * Wait for the suspend/resume to complete. If there
333 * are pending acknowledges, we wait here for them.
335 freezer_do_not_count();
337 wait_event(apm_suspend_waitqueue,
338 as->suspend_state == SUSPEND_DONE);
341 * Since we are waiting until the suspend is done, the
342 * try_to_freeze() in freezer_count() will not trigger
344 freezer_count();
345 } else {
346 as->suspend_state = SUSPEND_WAIT;
347 mutex_unlock(&state_lock);
350 * Otherwise it is a request to suspend the system.
351 * Queue an event for all readers, and expect an
352 * acknowledge from all writers who haven't already
353 * acknowledged.
355 err = queue_suspend_event(APM_USER_SUSPEND, as);
356 if (err < 0) {
358 * Avoid taking the lock here - this
359 * should be fine.
361 as->suspend_state = SUSPEND_NONE;
362 break;
365 if (err > 0)
366 apm_suspend();
369 * Wait for the suspend/resume to complete. If there
370 * are pending acknowledges, we wait here for them.
372 wait_event_freezable(apm_suspend_waitqueue,
373 as->suspend_state == SUSPEND_DONE);
376 mutex_lock(&state_lock);
377 err = as->suspend_result;
378 as->suspend_state = SUSPEND_NONE;
379 mutex_unlock(&state_lock);
380 break;
383 return err;
386 static int apm_release(struct inode * inode, struct file * filp)
388 struct apm_user *as = filp->private_data;
389 int pending = 0;
391 filp->private_data = NULL;
393 down_write(&user_list_lock);
394 list_del(&as->list);
395 up_write(&user_list_lock);
398 * We are now unhooked from the chain. As far as new
399 * events are concerned, we no longer exist. However, we
400 * need to balance suspends_pending, which means the
401 * possibility of sleeping.
403 mutex_lock(&state_lock);
404 if (as->suspend_state != SUSPEND_NONE) {
405 suspends_pending -= 1;
406 pending = suspends_pending == 0;
408 mutex_unlock(&state_lock);
409 if (pending)
410 apm_suspend();
412 kfree(as);
413 return 0;
416 static int apm_open(struct inode * inode, struct file * filp)
418 struct apm_user *as;
420 lock_kernel();
421 as = kzalloc(sizeof(*as), GFP_KERNEL);
422 if (as) {
424 * XXX - this is a tiny bit broken, when we consider BSD
425 * process accounting. If the device is opened by root, we
426 * instantly flag that we used superuser privs. Who knows,
427 * we might close the device immediately without doing a
428 * privileged operation -- cevans
430 as->suser = capable(CAP_SYS_ADMIN);
431 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
432 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
434 down_write(&user_list_lock);
435 list_add(&as->list, &apm_user_list);
436 up_write(&user_list_lock);
438 filp->private_data = as;
440 unlock_kernel();
442 return as ? 0 : -ENOMEM;
445 static struct file_operations apm_bios_fops = {
446 .owner = THIS_MODULE,
447 .read = apm_read,
448 .poll = apm_poll,
449 .ioctl = apm_ioctl,
450 .open = apm_open,
451 .release = apm_release,
454 static struct miscdevice apm_device = {
455 .minor = APM_MINOR_DEV,
456 .name = "apm_bios",
457 .fops = &apm_bios_fops
461 #ifdef CONFIG_PROC_FS
463 * Arguments, with symbols from linux/apm_bios.h.
465 * 0) Linux driver version (this will change if format changes)
466 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
467 * 2) APM flags from APM Installation Check (0x00):
468 * bit 0: APM_16_BIT_SUPPORT
469 * bit 1: APM_32_BIT_SUPPORT
470 * bit 2: APM_IDLE_SLOWS_CLOCK
471 * bit 3: APM_BIOS_DISABLED
472 * bit 4: APM_BIOS_DISENGAGED
473 * 3) AC line status
474 * 0x00: Off-line
475 * 0x01: On-line
476 * 0x02: On backup power (BIOS >= 1.1 only)
477 * 0xff: Unknown
478 * 4) Battery status
479 * 0x00: High
480 * 0x01: Low
481 * 0x02: Critical
482 * 0x03: Charging
483 * 0x04: Selected battery not present (BIOS >= 1.2 only)
484 * 0xff: Unknown
485 * 5) Battery flag
486 * bit 0: High
487 * bit 1: Low
488 * bit 2: Critical
489 * bit 3: Charging
490 * bit 7: No system battery
491 * 0xff: Unknown
492 * 6) Remaining battery life (percentage of charge):
493 * 0-100: valid
494 * -1: Unknown
495 * 7) Remaining battery life (time units):
496 * Number of remaining minutes or seconds
497 * -1: Unknown
498 * 8) min = minutes; sec = seconds
500 static int proc_apm_show(struct seq_file *m, void *v)
502 struct apm_power_info info;
503 char *units;
505 info.ac_line_status = 0xff;
506 info.battery_status = 0xff;
507 info.battery_flag = 0xff;
508 info.battery_life = -1;
509 info.time = -1;
510 info.units = -1;
512 if (apm_get_power_status)
513 apm_get_power_status(&info);
515 switch (info.units) {
516 default: units = "?"; break;
517 case 0: units = "min"; break;
518 case 1: units = "sec"; break;
521 seq_printf(m, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
522 driver_version, APM_32_BIT_SUPPORT,
523 info.ac_line_status, info.battery_status,
524 info.battery_flag, info.battery_life,
525 info.time, units);
527 return 0;
530 static int proc_apm_open(struct inode *inode, struct file *file)
532 return single_open(file, proc_apm_show, NULL);
535 static const struct file_operations apm_proc_fops = {
536 .owner = THIS_MODULE,
537 .open = proc_apm_open,
538 .read = seq_read,
539 .llseek = seq_lseek,
540 .release = single_release,
542 #endif
544 static int kapmd(void *arg)
546 do {
547 apm_event_t event;
548 int ret;
550 wait_event_interruptible(kapmd_wait,
551 !queue_empty(&kapmd_queue) || kthread_should_stop());
553 if (kthread_should_stop())
554 break;
556 spin_lock_irq(&kapmd_queue_lock);
557 event = 0;
558 if (!queue_empty(&kapmd_queue))
559 event = queue_get_event(&kapmd_queue);
560 spin_unlock_irq(&kapmd_queue_lock);
562 switch (event) {
563 case 0:
564 break;
566 case APM_LOW_BATTERY:
567 case APM_POWER_STATUS_CHANGE:
568 queue_event(event);
569 break;
571 case APM_USER_SUSPEND:
572 case APM_SYS_SUSPEND:
573 ret = queue_suspend_event(event, NULL);
574 if (ret < 0) {
576 * We were busy. Try again in 50ms.
578 queue_add_event(&kapmd_queue, event);
579 msleep(50);
581 if (ret > 0)
582 apm_suspend();
583 break;
585 case APM_CRITICAL_SUSPEND:
586 apm_suspend();
587 break;
589 } while (1);
591 return 0;
594 static int __init apm_init(void)
596 int ret;
598 if (apm_disabled) {
599 printk(KERN_NOTICE "apm: disabled on user request.\n");
600 return -ENODEV;
603 kapmd_tsk = kthread_create(kapmd, NULL, "kapmd");
604 if (IS_ERR(kapmd_tsk)) {
605 ret = PTR_ERR(kapmd_tsk);
606 kapmd_tsk = NULL;
607 return ret;
609 wake_up_process(kapmd_tsk);
611 #ifdef CONFIG_PROC_FS
612 proc_create("apm", 0, NULL, &apm_proc_fops);
613 #endif
615 ret = misc_register(&apm_device);
616 if (ret != 0) {
617 remove_proc_entry("apm", NULL);
618 kthread_stop(kapmd_tsk);
621 return ret;
624 static void __exit apm_exit(void)
626 misc_deregister(&apm_device);
627 remove_proc_entry("apm", NULL);
629 kthread_stop(kapmd_tsk);
632 module_init(apm_init);
633 module_exit(apm_exit);
635 MODULE_AUTHOR("Stephen Rothwell");
636 MODULE_DESCRIPTION("Advanced Power Management");
637 MODULE_LICENSE("GPL");
639 #ifndef MODULE
640 static int __init apm_setup(char *str)
642 while ((str != NULL) && (*str != '\0')) {
643 if (strncmp(str, "off", 3) == 0)
644 apm_disabled = 1;
645 if (strncmp(str, "on", 2) == 0)
646 apm_disabled = 0;
647 str = strchr(str, ',');
648 if (str != NULL)
649 str += strspn(str, ", \t");
651 return 1;
654 __setup("apm=", apm_setup);
655 #endif
658 * apm_queue_event - queue an APM event for kapmd
659 * @event: APM event
661 * Queue an APM event for kapmd to process and ultimately take the
662 * appropriate action. Only a subset of events are handled:
663 * %APM_LOW_BATTERY
664 * %APM_POWER_STATUS_CHANGE
665 * %APM_USER_SUSPEND
666 * %APM_SYS_SUSPEND
667 * %APM_CRITICAL_SUSPEND
669 void apm_queue_event(apm_event_t event)
671 unsigned long flags;
673 spin_lock_irqsave(&kapmd_queue_lock, flags);
674 queue_add_event(&kapmd_queue, event);
675 spin_unlock_irqrestore(&kapmd_queue_lock, flags);
677 wake_up_interruptible(&kapmd_wait);
679 EXPORT_SYMBOL(apm_queue_event);