[PATCH] relayfs: add Documentation on relay files in other filesystems
[linux-2.6/openmoko-kernel/knife-kernel.git] / arch / arm / kernel / apm.c
blobb9df1b782bb1128b72b30739dbc92671d14c8d2e
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/config.h>
14 #include <linux/module.h>
15 #include <linux/poll.h>
16 #include <linux/timer.h>
17 #include <linux/slab.h>
18 #include <linux/proc_fs.h>
19 #include <linux/miscdevice.h>
20 #include <linux/apm_bios.h>
21 #include <linux/sched.h>
22 #include <linux/pm.h>
23 #include <linux/device.h>
24 #include <linux/kernel.h>
25 #include <linux/list.h>
26 #include <linux/init.h>
27 #include <linux/completion.h>
29 #include <asm/apm.h> /* apm_power_info */
30 #include <asm/system.h>
33 * The apm_bios device is one of the misc char devices.
34 * This is its minor number.
36 #define APM_MINOR_DEV 134
39 * See Documentation/Config.help for the configuration options.
41 * Various options can be changed at boot time as follows:
42 * (We allow underscores for compatibility with the modules code)
43 * apm=on/off enable/disable APM
47 * Maximum number of events stored
49 #define APM_MAX_EVENTS 16
51 struct apm_queue {
52 unsigned int event_head;
53 unsigned int event_tail;
54 apm_event_t events[APM_MAX_EVENTS];
58 * The per-file APM data
60 struct apm_user {
61 struct list_head list;
63 unsigned int suser: 1;
64 unsigned int writer: 1;
65 unsigned int reader: 1;
67 int suspend_result;
68 unsigned int suspend_state;
69 #define SUSPEND_NONE 0 /* no suspend pending */
70 #define SUSPEND_PENDING 1 /* suspend pending read */
71 #define SUSPEND_READ 2 /* suspend read, pending ack */
72 #define SUSPEND_ACKED 3 /* suspend acked */
73 #define SUSPEND_DONE 4 /* suspend completed */
75 struct apm_queue queue;
79 * Local variables
81 static int suspends_pending;
82 static int apm_disabled;
83 static int arm_apm_active;
85 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
86 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
89 * This is a list of everyone who has opened /dev/apm_bios
91 static DECLARE_RWSEM(user_list_lock);
92 static LIST_HEAD(apm_user_list);
95 * kapmd info. kapmd provides us a process context to handle
96 * "APM" events within - specifically necessary if we're going
97 * to be suspending the system.
99 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
100 static DECLARE_COMPLETION(kapmd_exit);
101 static DEFINE_SPINLOCK(kapmd_queue_lock);
102 static struct apm_queue kapmd_queue;
105 static const char driver_version[] = "1.13"; /* no spaces */
110 * Compatibility cruft until the IPAQ people move over to the new
111 * interface.
113 static void __apm_get_power_status(struct apm_power_info *info)
118 * This allows machines to provide their own "apm get power status" function.
120 void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
121 EXPORT_SYMBOL(apm_get_power_status);
125 * APM event queue management.
127 static inline int queue_empty(struct apm_queue *q)
129 return q->event_head == q->event_tail;
132 static inline apm_event_t queue_get_event(struct apm_queue *q)
134 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
135 return q->events[q->event_tail];
138 static void queue_add_event(struct apm_queue *q, apm_event_t event)
140 q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
141 if (q->event_head == q->event_tail) {
142 static int notified;
144 if (notified++ == 0)
145 printk(KERN_ERR "apm: an event queue overflowed\n");
146 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
148 q->events[q->event_head] = event;
151 static void queue_event_one_user(struct apm_user *as, apm_event_t event)
153 if (as->suser && as->writer) {
154 switch (event) {
155 case APM_SYS_SUSPEND:
156 case APM_USER_SUSPEND:
158 * If this user already has a suspend pending,
159 * don't queue another one.
161 if (as->suspend_state != SUSPEND_NONE)
162 return;
164 as->suspend_state = SUSPEND_PENDING;
165 suspends_pending++;
166 break;
169 queue_add_event(&as->queue, event);
172 static void queue_event(apm_event_t event, struct apm_user *sender)
174 struct apm_user *as;
176 down_read(&user_list_lock);
177 list_for_each_entry(as, &apm_user_list, list) {
178 if (as != sender && as->reader)
179 queue_event_one_user(as, event);
181 up_read(&user_list_lock);
182 wake_up_interruptible(&apm_waitqueue);
185 static void apm_suspend(void)
187 struct apm_user *as;
188 int err = pm_suspend(PM_SUSPEND_MEM);
191 * Anyone on the APM queues will think we're still suspended.
192 * Send a message so everyone knows we're now awake again.
194 queue_event(APM_NORMAL_RESUME, NULL);
197 * Finally, wake up anyone who is sleeping on the suspend.
199 down_read(&user_list_lock);
200 list_for_each_entry(as, &apm_user_list, list) {
201 as->suspend_result = err;
202 as->suspend_state = SUSPEND_DONE;
204 up_read(&user_list_lock);
206 wake_up(&apm_suspend_waitqueue);
209 static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
211 struct apm_user *as = fp->private_data;
212 apm_event_t event;
213 int i = count, ret = 0;
215 if (count < sizeof(apm_event_t))
216 return -EINVAL;
218 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
219 return -EAGAIN;
221 wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
223 while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
224 event = queue_get_event(&as->queue);
226 ret = -EFAULT;
227 if (copy_to_user(buf, &event, sizeof(event)))
228 break;
230 if (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND)
231 as->suspend_state = SUSPEND_READ;
233 buf += sizeof(event);
234 i -= sizeof(event);
237 if (i < count)
238 ret = count - i;
240 return ret;
243 static unsigned int apm_poll(struct file *fp, poll_table * wait)
245 struct apm_user *as = fp->private_data;
247 poll_wait(fp, &apm_waitqueue, wait);
248 return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;
252 * apm_ioctl - handle APM ioctl
254 * APM_IOC_SUSPEND
255 * This IOCTL is overloaded, and performs two functions. It is used to:
256 * - initiate a suspend
257 * - acknowledge a suspend read from /dev/apm_bios.
258 * Only when everyone who has opened /dev/apm_bios with write permission
259 * has acknowledge does the actual suspend happen.
261 static int
262 apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
264 struct apm_user *as = filp->private_data;
265 unsigned long flags;
266 int err = -EINVAL;
268 if (!as->suser || !as->writer)
269 return -EPERM;
271 switch (cmd) {
272 case APM_IOC_SUSPEND:
273 as->suspend_result = -EINTR;
275 if (as->suspend_state == SUSPEND_READ) {
277 * If we read a suspend command from /dev/apm_bios,
278 * then the corresponding APM_IOC_SUSPEND ioctl is
279 * interpreted as an acknowledge.
281 as->suspend_state = SUSPEND_ACKED;
282 suspends_pending--;
283 } else {
285 * Otherwise it is a request to suspend the system.
286 * Queue an event for all readers, and expect an
287 * acknowledge from all writers who haven't already
288 * acknowledged.
290 queue_event(APM_USER_SUSPEND, as);
294 * If there are no further acknowledges required, suspend
295 * the system.
297 if (suspends_pending == 0)
298 apm_suspend();
301 * Wait for the suspend/resume to complete. If there are
302 * pending acknowledges, we wait here for them.
304 * Note that we need to ensure that the PM subsystem does
305 * not kick us out of the wait when it suspends the threads.
307 flags = current->flags;
308 current->flags |= PF_NOFREEZE;
311 * Note: do not allow a thread which is acking the suspend
312 * to escape until the resume is complete.
314 if (as->suspend_state == SUSPEND_ACKED)
315 wait_event(apm_suspend_waitqueue,
316 as->suspend_state == SUSPEND_DONE);
317 else
318 wait_event_interruptible(apm_suspend_waitqueue,
319 as->suspend_state == SUSPEND_DONE);
321 current->flags = flags;
322 err = as->suspend_result;
323 as->suspend_state = SUSPEND_NONE;
324 break;
327 return err;
330 static int apm_release(struct inode * inode, struct file * filp)
332 struct apm_user *as = filp->private_data;
333 filp->private_data = NULL;
335 down_write(&user_list_lock);
336 list_del(&as->list);
337 up_write(&user_list_lock);
340 * We are now unhooked from the chain. As far as new
341 * events are concerned, we no longer exist. However, we
342 * need to balance suspends_pending, which means the
343 * possibility of sleeping.
345 if (as->suspend_state != SUSPEND_NONE) {
346 suspends_pending -= 1;
347 if (suspends_pending == 0)
348 apm_suspend();
351 kfree(as);
352 return 0;
355 static int apm_open(struct inode * inode, struct file * filp)
357 struct apm_user *as;
359 as = (struct apm_user *)kmalloc(sizeof(*as), GFP_KERNEL);
360 if (as) {
361 memset(as, 0, sizeof(*as));
364 * XXX - this is a tiny bit broken, when we consider BSD
365 * process accounting. If the device is opened by root, we
366 * instantly flag that we used superuser privs. Who knows,
367 * we might close the device immediately without doing a
368 * privileged operation -- cevans
370 as->suser = capable(CAP_SYS_ADMIN);
371 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
372 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
374 down_write(&user_list_lock);
375 list_add(&as->list, &apm_user_list);
376 up_write(&user_list_lock);
378 filp->private_data = as;
381 return as ? 0 : -ENOMEM;
384 static struct file_operations apm_bios_fops = {
385 .owner = THIS_MODULE,
386 .read = apm_read,
387 .poll = apm_poll,
388 .ioctl = apm_ioctl,
389 .open = apm_open,
390 .release = apm_release,
393 static struct miscdevice apm_device = {
394 .minor = APM_MINOR_DEV,
395 .name = "apm_bios",
396 .fops = &apm_bios_fops
400 #ifdef CONFIG_PROC_FS
402 * Arguments, with symbols from linux/apm_bios.h.
404 * 0) Linux driver version (this will change if format changes)
405 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
406 * 2) APM flags from APM Installation Check (0x00):
407 * bit 0: APM_16_BIT_SUPPORT
408 * bit 1: APM_32_BIT_SUPPORT
409 * bit 2: APM_IDLE_SLOWS_CLOCK
410 * bit 3: APM_BIOS_DISABLED
411 * bit 4: APM_BIOS_DISENGAGED
412 * 3) AC line status
413 * 0x00: Off-line
414 * 0x01: On-line
415 * 0x02: On backup power (BIOS >= 1.1 only)
416 * 0xff: Unknown
417 * 4) Battery status
418 * 0x00: High
419 * 0x01: Low
420 * 0x02: Critical
421 * 0x03: Charging
422 * 0x04: Selected battery not present (BIOS >= 1.2 only)
423 * 0xff: Unknown
424 * 5) Battery flag
425 * bit 0: High
426 * bit 1: Low
427 * bit 2: Critical
428 * bit 3: Charging
429 * bit 7: No system battery
430 * 0xff: Unknown
431 * 6) Remaining battery life (percentage of charge):
432 * 0-100: valid
433 * -1: Unknown
434 * 7) Remaining battery life (time units):
435 * Number of remaining minutes or seconds
436 * -1: Unknown
437 * 8) min = minutes; sec = seconds
439 static int apm_get_info(char *buf, char **start, off_t fpos, int length)
441 struct apm_power_info info;
442 char *units;
443 int ret;
445 info.ac_line_status = 0xff;
446 info.battery_status = 0xff;
447 info.battery_flag = 0xff;
448 info.battery_life = -1;
449 info.time = -1;
450 info.units = -1;
452 if (apm_get_power_status)
453 apm_get_power_status(&info);
455 switch (info.units) {
456 default: units = "?"; break;
457 case 0: units = "min"; break;
458 case 1: units = "sec"; break;
461 ret = sprintf(buf, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
462 driver_version, APM_32_BIT_SUPPORT,
463 info.ac_line_status, info.battery_status,
464 info.battery_flag, info.battery_life,
465 info.time, units);
467 return ret;
469 #endif
471 static int kapmd(void *arg)
473 daemonize("kapmd");
474 current->flags |= PF_NOFREEZE;
476 do {
477 apm_event_t event;
479 wait_event_interruptible(kapmd_wait,
480 !queue_empty(&kapmd_queue) || !arm_apm_active);
482 if (!arm_apm_active)
483 break;
485 spin_lock_irq(&kapmd_queue_lock);
486 event = 0;
487 if (!queue_empty(&kapmd_queue))
488 event = queue_get_event(&kapmd_queue);
489 spin_unlock_irq(&kapmd_queue_lock);
491 switch (event) {
492 case 0:
493 break;
495 case APM_LOW_BATTERY:
496 case APM_POWER_STATUS_CHANGE:
497 queue_event(event, NULL);
498 break;
500 case APM_USER_SUSPEND:
501 case APM_SYS_SUSPEND:
502 queue_event(event, NULL);
503 if (suspends_pending == 0)
504 apm_suspend();
505 break;
507 case APM_CRITICAL_SUSPEND:
508 apm_suspend();
509 break;
511 } while (1);
513 complete_and_exit(&kapmd_exit, 0);
516 static int __init apm_init(void)
518 int ret;
520 if (apm_disabled) {
521 printk(KERN_NOTICE "apm: disabled on user request.\n");
522 return -ENODEV;
525 arm_apm_active = 1;
527 ret = kernel_thread(kapmd, NULL, CLONE_KERNEL);
528 if (ret < 0) {
529 arm_apm_active = 0;
530 return ret;
533 #ifdef CONFIG_PROC_FS
534 create_proc_info_entry("apm", 0, NULL, apm_get_info);
535 #endif
537 ret = misc_register(&apm_device);
538 if (ret != 0) {
539 remove_proc_entry("apm", NULL);
541 arm_apm_active = 0;
542 wake_up(&kapmd_wait);
543 wait_for_completion(&kapmd_exit);
546 return ret;
549 static void __exit apm_exit(void)
551 misc_deregister(&apm_device);
552 remove_proc_entry("apm", NULL);
554 arm_apm_active = 0;
555 wake_up(&kapmd_wait);
556 wait_for_completion(&kapmd_exit);
559 module_init(apm_init);
560 module_exit(apm_exit);
562 MODULE_AUTHOR("Stephen Rothwell");
563 MODULE_DESCRIPTION("Advanced Power Management");
564 MODULE_LICENSE("GPL");
566 #ifndef MODULE
567 static int __init apm_setup(char *str)
569 while ((str != NULL) && (*str != '\0')) {
570 if (strncmp(str, "off", 3) == 0)
571 apm_disabled = 1;
572 if (strncmp(str, "on", 2) == 0)
573 apm_disabled = 0;
574 str = strchr(str, ',');
575 if (str != NULL)
576 str += strspn(str, ", \t");
578 return 1;
581 __setup("apm=", apm_setup);
582 #endif
585 * apm_queue_event - queue an APM event for kapmd
586 * @event: APM event
588 * Queue an APM event for kapmd to process and ultimately take the
589 * appropriate action. Only a subset of events are handled:
590 * %APM_LOW_BATTERY
591 * %APM_POWER_STATUS_CHANGE
592 * %APM_USER_SUSPEND
593 * %APM_SYS_SUSPEND
594 * %APM_CRITICAL_SUSPEND
596 void apm_queue_event(apm_event_t event)
598 unsigned long flags;
600 spin_lock_irqsave(&kapmd_queue_lock, flags);
601 queue_add_event(&kapmd_queue, event);
602 spin_unlock_irqrestore(&kapmd_queue_lock, flags);
604 wake_up_interruptible(&kapmd_wait);
606 EXPORT_SYMBOL(apm_queue_event);