2 * bios-less APM driver for MIPS 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/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/capability.h>
22 #include <linux/sched.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>
30 #include <asm/apm.h> /* apm_power_info */
31 #include <asm/system.h>
34 * The apm_bios device is one of the misc char devices.
35 * This is its minor number.
37 #define APM_MINOR_DEV 134
40 * See Documentation/Config.help for the configuration options.
42 * Various options can be changed at boot time as follows:
43 * (We allow underscores for compatibility with the modules code)
44 * apm=on/off enable/disable APM
48 * Maximum number of events stored
50 #define APM_MAX_EVENTS 16
53 unsigned int event_head;
54 unsigned int event_tail;
55 apm_event_t events[APM_MAX_EVENTS];
59 * The per-file APM data
62 struct list_head list;
64 unsigned int suser: 1;
65 unsigned int writer: 1;
66 unsigned int reader: 1;
69 unsigned int suspend_state;
70 #define SUSPEND_NONE 0 /* no suspend pending */
71 #define SUSPEND_PENDING 1 /* suspend pending read */
72 #define SUSPEND_READ 2 /* suspend read, pending ack */
73 #define SUSPEND_ACKED 3 /* suspend acked */
74 #define SUSPEND_DONE 4 /* suspend completed */
76 struct apm_queue queue;
82 static int suspends_pending;
83 static int apm_disabled;
84 static int mips_apm_active;
86 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
87 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
90 * This is a list of everyone who has opened /dev/apm_bios
92 static DECLARE_RWSEM(user_list_lock);
93 static LIST_HEAD(apm_user_list);
96 * kapmd info. kapmd provides us a process context to handle
97 * "APM" events within - specifically necessary if we're going
98 * to be suspending the system.
100 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
101 static DECLARE_COMPLETION(kapmd_exit);
102 static DEFINE_SPINLOCK(kapmd_queue_lock);
103 static struct apm_queue kapmd_queue;
106 static const char driver_version[] = "1.13"; /* no spaces */
111 * Compatibility cruft until the IPAQ people move over to the new
114 static void __apm_get_power_status(struct apm_power_info *info)
119 * This allows machines to provide their own "apm get power status" function.
121 void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
122 EXPORT_SYMBOL(apm_get_power_status);
126 * APM event queue management.
128 static inline int queue_empty(struct apm_queue *q)
130 return q->event_head == q->event_tail;
133 static inline apm_event_t queue_get_event(struct apm_queue *q)
135 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
136 return q->events[q->event_tail];
139 static void queue_add_event(struct apm_queue *q, apm_event_t event)
141 q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
142 if (q->event_head == q->event_tail) {
146 printk(KERN_ERR "apm: an event queue overflowed\n");
147 q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
149 q->events[q->event_head] = event;
152 static void queue_event_one_user(struct apm_user *as, apm_event_t event)
154 if (as->suser && as->writer) {
156 case APM_SYS_SUSPEND:
157 case APM_USER_SUSPEND:
159 * If this user already has a suspend pending,
160 * don't queue another one.
162 if (as->suspend_state != SUSPEND_NONE)
165 as->suspend_state = SUSPEND_PENDING;
170 queue_add_event(&as->queue, event);
173 static void queue_event(apm_event_t event, struct apm_user *sender)
177 down_read(&user_list_lock);
178 list_for_each_entry(as, &apm_user_list, list) {
179 if (as != sender && as->reader)
180 queue_event_one_user(as, event);
182 up_read(&user_list_lock);
183 wake_up_interruptible(&apm_waitqueue);
186 static void apm_suspend(void)
189 int err = pm_suspend(PM_SUSPEND_MEM);
192 * Anyone on the APM queues will think we're still suspended.
193 * Send a message so everyone knows we're now awake again.
195 queue_event(APM_NORMAL_RESUME, NULL);
198 * Finally, wake up anyone who is sleeping on the suspend.
200 down_read(&user_list_lock);
201 list_for_each_entry(as, &apm_user_list, list) {
202 as->suspend_result = err;
203 as->suspend_state = SUSPEND_DONE;
205 up_read(&user_list_lock);
207 wake_up(&apm_suspend_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;
214 int i = count, ret = 0;
216 if (count < sizeof(apm_event_t))
219 if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
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);
228 if (copy_to_user(buf, &event, sizeof(event)))
231 if (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND)
232 as->suspend_state = SUSPEND_READ;
234 buf += sizeof(event);
244 static unsigned int apm_poll(struct file *fp, poll_table * wait)
246 struct apm_user *as = fp->private_data;
248 poll_wait(fp, &apm_waitqueue, wait);
249 return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;
253 * apm_ioctl - handle APM ioctl
256 * This IOCTL is overloaded, and performs two functions. It is used to:
257 * - initiate a suspend
258 * - acknowledge a suspend read from /dev/apm_bios.
259 * Only when everyone who has opened /dev/apm_bios with write permission
260 * has acknowledge does the actual suspend happen.
263 apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
265 struct apm_user *as = filp->private_data;
269 if (!as->suser || !as->writer)
273 case APM_IOC_SUSPEND:
274 as->suspend_result = -EINTR;
276 if (as->suspend_state == SUSPEND_READ) {
278 * If we read a suspend command from /dev/apm_bios,
279 * then the corresponding APM_IOC_SUSPEND ioctl is
280 * interpreted as an acknowledge.
282 as->suspend_state = SUSPEND_ACKED;
286 * Otherwise it is a request to suspend the system.
287 * Queue an event for all readers, and expect an
288 * acknowledge from all writers who haven't already
291 queue_event(APM_USER_SUSPEND, as);
295 * If there are no further acknowledges required, suspend
298 if (suspends_pending == 0)
302 * Wait for the suspend/resume to complete. If there are
303 * pending acknowledges, we wait here for them.
305 * Note that we need to ensure that the PM subsystem does
306 * not kick us out of the wait when it suspends the threads.
308 flags = current->flags;
309 current->flags |= PF_NOFREEZE;
312 * Note: do not allow a thread which is acking the suspend
313 * to escape until the resume is complete.
315 if (as->suspend_state == SUSPEND_ACKED)
316 wait_event(apm_suspend_waitqueue,
317 as->suspend_state == SUSPEND_DONE);
319 wait_event_interruptible(apm_suspend_waitqueue,
320 as->suspend_state == SUSPEND_DONE);
322 current->flags = flags;
323 err = as->suspend_result;
324 as->suspend_state = SUSPEND_NONE;
331 static int apm_release(struct inode * inode, struct file * filp)
333 struct apm_user *as = filp->private_data;
334 filp->private_data = NULL;
336 down_write(&user_list_lock);
338 up_write(&user_list_lock);
341 * We are now unhooked from the chain. As far as new
342 * events are concerned, we no longer exist. However, we
343 * need to balance suspends_pending, which means the
344 * possibility of sleeping.
346 if (as->suspend_state != SUSPEND_NONE) {
347 suspends_pending -= 1;
348 if (suspends_pending == 0)
356 static int apm_open(struct inode * inode, struct file * filp)
360 as = (struct apm_user *)kzalloc(sizeof(*as), GFP_KERNEL);
363 * XXX - this is a tiny bit broken, when we consider BSD
364 * process accounting. If the device is opened by root, we
365 * instantly flag that we used superuser privs. Who knows,
366 * we might close the device immediately without doing a
367 * privileged operation -- cevans
369 as->suser = capable(CAP_SYS_ADMIN);
370 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
371 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
373 down_write(&user_list_lock);
374 list_add(&as->list, &apm_user_list);
375 up_write(&user_list_lock);
377 filp->private_data = as;
380 return as ? 0 : -ENOMEM;
383 static struct file_operations apm_bios_fops = {
384 .owner = THIS_MODULE,
389 .release = apm_release,
392 static struct miscdevice apm_device = {
393 .minor = APM_MINOR_DEV,
395 .fops = &apm_bios_fops
399 #ifdef CONFIG_PROC_FS
401 * Arguments, with symbols from linux/apm_bios.h.
403 * 0) Linux driver version (this will change if format changes)
404 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
405 * 2) APM flags from APM Installation Check (0x00):
406 * bit 0: APM_16_BIT_SUPPORT
407 * bit 1: APM_32_BIT_SUPPORT
408 * bit 2: APM_IDLE_SLOWS_CLOCK
409 * bit 3: APM_BIOS_DISABLED
410 * bit 4: APM_BIOS_DISENGAGED
414 * 0x02: On backup power (BIOS >= 1.1 only)
421 * 0x04: Selected battery not present (BIOS >= 1.2 only)
428 * bit 7: No system battery
430 * 6) Remaining battery life (percentage of charge):
433 * 7) Remaining battery life (time units):
434 * Number of remaining minutes or seconds
436 * 8) min = minutes; sec = seconds
438 static int apm_get_info(char *buf, char **start, off_t fpos, int length)
440 struct apm_power_info info;
444 info.ac_line_status = 0xff;
445 info.battery_status = 0xff;
446 info.battery_flag = 0xff;
447 info.battery_life = -1;
451 if (apm_get_power_status)
452 apm_get_power_status(&info);
454 switch (info.units) {
455 default: units = "?"; break;
456 case 0: units = "min"; break;
457 case 1: units = "sec"; break;
460 ret = sprintf(buf, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
461 driver_version, APM_32_BIT_SUPPORT,
462 info.ac_line_status, info.battery_status,
463 info.battery_flag, info.battery_life,
470 static int kapmd(void *arg)
473 current->flags |= PF_NOFREEZE;
478 wait_event_interruptible(kapmd_wait,
479 !queue_empty(&kapmd_queue) || !mips_apm_active);
481 if (!mips_apm_active)
484 spin_lock_irq(&kapmd_queue_lock);
486 if (!queue_empty(&kapmd_queue))
487 event = queue_get_event(&kapmd_queue);
488 spin_unlock_irq(&kapmd_queue_lock);
494 case APM_LOW_BATTERY:
495 case APM_POWER_STATUS_CHANGE:
496 queue_event(event, NULL);
499 case APM_USER_SUSPEND:
500 case APM_SYS_SUSPEND:
501 queue_event(event, NULL);
502 if (suspends_pending == 0)
506 case APM_CRITICAL_SUSPEND:
512 complete_and_exit(&kapmd_exit, 0);
515 static int __init apm_init(void)
520 printk(KERN_NOTICE "apm: disabled on user request.\n");
526 ret = kernel_thread(kapmd, NULL, CLONE_KERNEL);
532 #ifdef CONFIG_PROC_FS
533 create_proc_info_entry("apm", 0, NULL, apm_get_info);
536 ret = misc_register(&apm_device);
538 remove_proc_entry("apm", NULL);
541 wake_up(&kapmd_wait);
542 wait_for_completion(&kapmd_exit);
548 static void __exit apm_exit(void)
550 misc_deregister(&apm_device);
551 remove_proc_entry("apm", NULL);
554 wake_up(&kapmd_wait);
555 wait_for_completion(&kapmd_exit);
558 module_init(apm_init);
559 module_exit(apm_exit);
561 MODULE_AUTHOR("Stephen Rothwell");
562 MODULE_DESCRIPTION("Advanced Power Management");
563 MODULE_LICENSE("GPL");
566 static int __init apm_setup(char *str)
568 while ((str != NULL) && (*str != '\0')) {
569 if (strncmp(str, "off", 3) == 0)
571 if (strncmp(str, "on", 2) == 0)
573 str = strchr(str, ',');
575 str += strspn(str, ", \t");
580 __setup("apm=", apm_setup);
584 * apm_queue_event - queue an APM event for kapmd
587 * Queue an APM event for kapmd to process and ultimately take the
588 * appropriate action. Only a subset of events are handled:
590 * %APM_POWER_STATUS_CHANGE
593 * %APM_CRITICAL_SUSPEND
595 void apm_queue_event(apm_event_t event)
599 spin_lock_irqsave(&kapmd_queue_lock, flags);
600 queue_add_event(&kapmd_queue, event);
601 spin_unlock_irqrestore(&kapmd_queue_lock, flags);
603 wake_up_interruptible(&kapmd_wait);
605 EXPORT_SYMBOL(apm_queue_event);