pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / char / apm-emulation.c
blobec116df919d97c043b45ed89637e73624d59810f
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/proc_fs.h>
17 #include <linux/miscdevice.h>
18 #include <linux/apm_bios.h>
19 #include <linux/capability.h>
20 #include <linux/sched.h>
21 #include <linux/pm.h>
22 #include <linux/apm-emulation.h>
23 #include <linux/freezer.h>
24 #include <linux/device.h>
25 #include <linux/kernel.h>
26 #include <linux/list.h>
27 #include <linux/init.h>
28 #include <linux/completion.h>
29 #include <linux/kthread.h>
30 #include <linux/delay.h>
32 #include <asm/system.h>
35 * The apm_bios device is one of the misc char devices.
36 * This is its minor number.
38 #define APM_MINOR_DEV 134
41 * See Documentation/Config.help for the configuration options.
43 * Various options can be changed at boot time as follows:
44 * (We allow underscores for compatibility with the modules code)
45 * apm=on/off enable/disable APM
49 * Maximum number of events stored
51 #define APM_MAX_EVENTS 16
53 struct apm_queue {
54 unsigned int event_head;
55 unsigned int event_tail;
56 apm_event_t events[APM_MAX_EVENTS];
60 * The per-file APM data
62 struct apm_user {
63 struct list_head list;
65 unsigned int suser: 1;
66 unsigned int writer: 1;
67 unsigned int reader: 1;
69 int suspend_result;
70 unsigned int suspend_state;
71 #define SUSPEND_NONE 0 /* no suspend pending */
72 #define SUSPEND_PENDING 1 /* suspend pending read */
73 #define SUSPEND_READ 2 /* suspend read, pending ack */
74 #define SUSPEND_ACKED 3 /* suspend acked */
75 #define SUSPEND_WAIT 4 /* waiting for suspend */
76 #define SUSPEND_DONE 5 /* suspend completed */
78 struct apm_queue queue;
82 * Local variables
84 static int suspends_pending;
85 static int apm_disabled;
86 static struct task_struct *kapmd_tsk;
88 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
89 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
92 * This is a list of everyone who has opened /dev/apm_bios
94 static DECLARE_RWSEM(user_list_lock);
95 static LIST_HEAD(apm_user_list);
98 * kapmd info. kapmd provides us a process context to handle
99 * "APM" events within - specifically necessary if we're going
100 * to be suspending the system.
102 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
103 static DEFINE_SPINLOCK(kapmd_queue_lock);
104 static struct apm_queue kapmd_queue;
106 static DEFINE_MUTEX(state_lock);
108 static const char driver_version[] = "1.13"; /* no spaces */
113 * Compatibility cruft until the IPAQ people move over to the new
114 * interface.
116 static void __apm_get_power_status(struct apm_power_info *info)
121 * This allows machines to provide their own "apm get power status" function.
123 void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
124 EXPORT_SYMBOL(apm_get_power_status);
128 * APM event queue management.
130 static inline int queue_empty(struct apm_queue *q)
132 return q->event_head == q->event_tail;
135 static inline apm_event_t queue_get_event(struct apm_queue *q)
137 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
138 return q->events[q->event_tail];
141 static void queue_add_event(struct apm_queue *q, apm_event_t event)
143 q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
144 if (q->event_head == q->event_tail) {
145 static int notified;
147 if (notified++ == 0)
148 printk(KERN_ERR "apm: an event queue overflowed\n");
149 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
151 q->events[q->event_head] = event;
154 static void queue_event(apm_event_t event)
156 struct apm_user *as;
158 down_read(&user_list_lock);
159 list_for_each_entry(as, &apm_user_list, list) {
160 if (as->reader)
161 queue_add_event(&as->queue, event);
163 up_read(&user_list_lock);
164 wake_up_interruptible(&apm_waitqueue);
168 * queue_suspend_event - queue an APM suspend event.
170 * Check that we're in a state where we can suspend. If not,
171 * return -EBUSY. Otherwise, queue an event to all "writer"
172 * users. If there are no "writer" users, return '1' to
173 * indicate that we can immediately suspend.
175 static int queue_suspend_event(apm_event_t event, struct apm_user *sender)
177 struct apm_user *as;
178 int ret = 1;
180 mutex_lock(&state_lock);
181 down_read(&user_list_lock);
184 * If a thread is still processing, we can't suspend, so reject
185 * the request.
187 list_for_each_entry(as, &apm_user_list, list) {
188 if (as != sender && as->reader && as->writer && as->suser &&
189 as->suspend_state != SUSPEND_NONE) {
190 ret = -EBUSY;
191 goto out;
195 list_for_each_entry(as, &apm_user_list, list) {
196 if (as != sender && as->reader && as->writer && as->suser) {
197 as->suspend_state = SUSPEND_PENDING;
198 suspends_pending++;
199 queue_add_event(&as->queue, event);
200 ret = 0;
203 out:
204 up_read(&user_list_lock);
205 mutex_unlock(&state_lock);
206 wake_up_interruptible(&apm_waitqueue);
207 return ret;
210 static void apm_suspend(void)
212 struct apm_user *as;
213 int err = pm_suspend(PM_SUSPEND_MEM);
216 * Anyone on the APM queues will think we're still suspended.
217 * Send a message so everyone knows we're now awake again.
219 queue_event(APM_NORMAL_RESUME);
222 * Finally, wake up anyone who is sleeping on the suspend.
224 mutex_lock(&state_lock);
225 down_read(&user_list_lock);
226 list_for_each_entry(as, &apm_user_list, list) {
227 if (as->suspend_state == SUSPEND_WAIT ||
228 as->suspend_state == SUSPEND_ACKED) {
229 as->suspend_result = err;
230 as->suspend_state = SUSPEND_DONE;
233 up_read(&user_list_lock);
234 mutex_unlock(&state_lock);
236 wake_up(&apm_suspend_waitqueue);
239 static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
241 struct apm_user *as = fp->private_data;
242 apm_event_t event;
243 int i = count, ret = 0;
245 if (count < sizeof(apm_event_t))
246 return -EINVAL;
248 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
249 return -EAGAIN;
251 wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
253 while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
254 event = queue_get_event(&as->queue);
256 ret = -EFAULT;
257 if (copy_to_user(buf, &event, sizeof(event)))
258 break;
260 mutex_lock(&state_lock);
261 if (as->suspend_state == SUSPEND_PENDING &&
262 (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND))
263 as->suspend_state = SUSPEND_READ;
264 mutex_unlock(&state_lock);
266 buf += sizeof(event);
267 i -= sizeof(event);
270 if (i < count)
271 ret = count - i;
273 return ret;
276 static unsigned int apm_poll(struct file *fp, poll_table * wait)
278 struct apm_user *as = fp->private_data;
280 poll_wait(fp, &apm_waitqueue, wait);
281 return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;
285 * apm_ioctl - handle APM ioctl
287 * APM_IOC_SUSPEND
288 * This IOCTL is overloaded, and performs two functions. It is used to:
289 * - initiate a suspend
290 * - acknowledge a suspend read from /dev/apm_bios.
291 * Only when everyone who has opened /dev/apm_bios with write permission
292 * has acknowledge does the actual suspend happen.
294 static int
295 apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
297 struct apm_user *as = filp->private_data;
298 unsigned long flags;
299 int err = -EINVAL;
301 if (!as->suser || !as->writer)
302 return -EPERM;
304 switch (cmd) {
305 case APM_IOC_SUSPEND:
306 mutex_lock(&state_lock);
308 as->suspend_result = -EINTR;
310 if (as->suspend_state == SUSPEND_READ) {
311 int pending;
314 * If we read a suspend command from /dev/apm_bios,
315 * then the corresponding APM_IOC_SUSPEND ioctl is
316 * interpreted as an acknowledge.
318 as->suspend_state = SUSPEND_ACKED;
319 suspends_pending--;
320 pending = suspends_pending == 0;
321 mutex_unlock(&state_lock);
324 * If there are no further acknowledges required,
325 * suspend the system.
327 if (pending)
328 apm_suspend();
331 * Wait for the suspend/resume to complete. If there
332 * are pending acknowledges, we wait here for them.
334 flags = current->flags;
336 wait_event(apm_suspend_waitqueue,
337 as->suspend_state == SUSPEND_DONE);
338 } else {
339 as->suspend_state = SUSPEND_WAIT;
340 mutex_unlock(&state_lock);
343 * Otherwise it is a request to suspend the system.
344 * Queue an event for all readers, and expect an
345 * acknowledge from all writers who haven't already
346 * acknowledged.
348 err = queue_suspend_event(APM_USER_SUSPEND, as);
349 if (err < 0) {
351 * Avoid taking the lock here - this
352 * should be fine.
354 as->suspend_state = SUSPEND_NONE;
355 break;
358 if (err > 0)
359 apm_suspend();
362 * Wait for the suspend/resume to complete. If there
363 * are pending acknowledges, we wait here for them.
365 flags = current->flags;
367 wait_event_interruptible(apm_suspend_waitqueue,
368 as->suspend_state == SUSPEND_DONE);
371 current->flags = flags;
373 mutex_lock(&state_lock);
374 err = as->suspend_result;
375 as->suspend_state = SUSPEND_NONE;
376 mutex_unlock(&state_lock);
377 break;
380 return err;
383 static int apm_release(struct inode * inode, struct file * filp)
385 struct apm_user *as = filp->private_data;
386 int pending = 0;
388 filp->private_data = NULL;
390 down_write(&user_list_lock);
391 list_del(&as->list);
392 up_write(&user_list_lock);
395 * We are now unhooked from the chain. As far as new
396 * events are concerned, we no longer exist. However, we
397 * need to balance suspends_pending, which means the
398 * possibility of sleeping.
400 mutex_lock(&state_lock);
401 if (as->suspend_state != SUSPEND_NONE) {
402 suspends_pending -= 1;
403 pending = suspends_pending == 0;
405 mutex_unlock(&state_lock);
406 if (pending)
407 apm_suspend();
409 kfree(as);
410 return 0;
413 static int apm_open(struct inode * inode, struct file * filp)
415 struct apm_user *as;
417 as = kzalloc(sizeof(*as), GFP_KERNEL);
418 if (as) {
420 * XXX - this is a tiny bit broken, when we consider BSD
421 * process accounting. If the device is opened by root, we
422 * instantly flag that we used superuser privs. Who knows,
423 * we might close the device immediately without doing a
424 * privileged operation -- cevans
426 as->suser = capable(CAP_SYS_ADMIN);
427 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
428 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
430 down_write(&user_list_lock);
431 list_add(&as->list, &apm_user_list);
432 up_write(&user_list_lock);
434 filp->private_data = as;
437 return as ? 0 : -ENOMEM;
440 static struct file_operations apm_bios_fops = {
441 .owner = THIS_MODULE,
442 .read = apm_read,
443 .poll = apm_poll,
444 .ioctl = apm_ioctl,
445 .open = apm_open,
446 .release = apm_release,
449 static struct miscdevice apm_device = {
450 .minor = APM_MINOR_DEV,
451 .name = "apm_bios",
452 .fops = &apm_bios_fops
456 #ifdef CONFIG_PROC_FS
458 * Arguments, with symbols from linux/apm_bios.h.
460 * 0) Linux driver version (this will change if format changes)
461 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
462 * 2) APM flags from APM Installation Check (0x00):
463 * bit 0: APM_16_BIT_SUPPORT
464 * bit 1: APM_32_BIT_SUPPORT
465 * bit 2: APM_IDLE_SLOWS_CLOCK
466 * bit 3: APM_BIOS_DISABLED
467 * bit 4: APM_BIOS_DISENGAGED
468 * 3) AC line status
469 * 0x00: Off-line
470 * 0x01: On-line
471 * 0x02: On backup power (BIOS >= 1.1 only)
472 * 0xff: Unknown
473 * 4) Battery status
474 * 0x00: High
475 * 0x01: Low
476 * 0x02: Critical
477 * 0x03: Charging
478 * 0x04: Selected battery not present (BIOS >= 1.2 only)
479 * 0xff: Unknown
480 * 5) Battery flag
481 * bit 0: High
482 * bit 1: Low
483 * bit 2: Critical
484 * bit 3: Charging
485 * bit 7: No system battery
486 * 0xff: Unknown
487 * 6) Remaining battery life (percentage of charge):
488 * 0-100: valid
489 * -1: Unknown
490 * 7) Remaining battery life (time units):
491 * Number of remaining minutes or seconds
492 * -1: Unknown
493 * 8) min = minutes; sec = seconds
495 static int apm_get_info(char *buf, char **start, off_t fpos, int length)
497 struct apm_power_info info;
498 char *units;
499 int ret;
501 info.ac_line_status = 0xff;
502 info.battery_status = 0xff;
503 info.battery_flag = 0xff;
504 info.battery_life = -1;
505 info.time = -1;
506 info.units = -1;
508 if (apm_get_power_status)
509 apm_get_power_status(&info);
511 switch (info.units) {
512 default: units = "?"; break;
513 case 0: units = "min"; break;
514 case 1: units = "sec"; break;
517 ret = sprintf(buf, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
518 driver_version, APM_32_BIT_SUPPORT,
519 info.ac_line_status, info.battery_status,
520 info.battery_flag, info.battery_life,
521 info.time, units);
523 return ret;
525 #endif
527 static int kapmd(void *arg)
529 do {
530 apm_event_t event;
531 int ret;
533 wait_event_interruptible(kapmd_wait,
534 !queue_empty(&kapmd_queue) || kthread_should_stop());
536 if (kthread_should_stop())
537 break;
539 spin_lock_irq(&kapmd_queue_lock);
540 event = 0;
541 if (!queue_empty(&kapmd_queue))
542 event = queue_get_event(&kapmd_queue);
543 spin_unlock_irq(&kapmd_queue_lock);
545 switch (event) {
546 case 0:
547 break;
549 case APM_LOW_BATTERY:
550 case APM_POWER_STATUS_CHANGE:
551 queue_event(event);
552 break;
554 case APM_USER_SUSPEND:
555 case APM_SYS_SUSPEND:
556 ret = queue_suspend_event(event, NULL);
557 if (ret < 0) {
559 * We were busy. Try again in 50ms.
561 queue_add_event(&kapmd_queue, event);
562 msleep(50);
564 if (ret > 0)
565 apm_suspend();
566 break;
568 case APM_CRITICAL_SUSPEND:
569 apm_suspend();
570 break;
572 } while (1);
574 return 0;
577 static int __init apm_init(void)
579 int ret;
581 if (apm_disabled) {
582 printk(KERN_NOTICE "apm: disabled on user request.\n");
583 return -ENODEV;
586 kapmd_tsk = kthread_create(kapmd, NULL, "kapmd");
587 if (IS_ERR(kapmd_tsk)) {
588 ret = PTR_ERR(kapmd_tsk);
589 kapmd_tsk = NULL;
590 return ret;
592 wake_up_process(kapmd_tsk);
594 #ifdef CONFIG_PROC_FS
595 create_proc_info_entry("apm", 0, NULL, apm_get_info);
596 #endif
598 ret = misc_register(&apm_device);
599 if (ret != 0) {
600 remove_proc_entry("apm", NULL);
601 kthread_stop(kapmd_tsk);
604 return ret;
607 static void __exit apm_exit(void)
609 misc_deregister(&apm_device);
610 remove_proc_entry("apm", NULL);
612 kthread_stop(kapmd_tsk);
615 module_init(apm_init);
616 module_exit(apm_exit);
618 MODULE_AUTHOR("Stephen Rothwell");
619 MODULE_DESCRIPTION("Advanced Power Management");
620 MODULE_LICENSE("GPL");
622 #ifndef MODULE
623 static int __init apm_setup(char *str)
625 while ((str != NULL) && (*str != '\0')) {
626 if (strncmp(str, "off", 3) == 0)
627 apm_disabled = 1;
628 if (strncmp(str, "on", 2) == 0)
629 apm_disabled = 0;
630 str = strchr(str, ',');
631 if (str != NULL)
632 str += strspn(str, ", \t");
634 return 1;
637 __setup("apm=", apm_setup);
638 #endif
641 * apm_queue_event - queue an APM event for kapmd
642 * @event: APM event
644 * Queue an APM event for kapmd to process and ultimately take the
645 * appropriate action. Only a subset of events are handled:
646 * %APM_LOW_BATTERY
647 * %APM_POWER_STATUS_CHANGE
648 * %APM_USER_SUSPEND
649 * %APM_SYS_SUSPEND
650 * %APM_CRITICAL_SUSPEND
652 void apm_queue_event(apm_event_t event)
654 unsigned long flags;
656 spin_lock_irqsave(&kapmd_queue_lock, flags);
657 queue_add_event(&kapmd_queue, event);
658 spin_unlock_irqrestore(&kapmd_queue_lock, flags);
660 wake_up_interruptible(&kapmd_wait);
662 EXPORT_SYMBOL(apm_queue_event);