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/module.h>
14 #include <linux/poll.h>
15 #include <linux/timer.h>
16 #include <linux/slab.h>
17 #include <linux/proc_fs.h>
18 #include <linux/miscdevice.h>
19 #include <linux/apm_bios.h>
20 #include <linux/capability.h>
21 #include <linux/sched.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
52 unsigned int event_head
;
53 unsigned int event_tail
;
54 apm_event_t events
[APM_MAX_EVENTS
];
58 * The per-file APM data
61 struct list_head list
;
63 unsigned int suser
: 1;
64 unsigned int writer
: 1;
65 unsigned int reader
: 1;
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
;
81 static int suspends_pending
;
82 static int apm_disabled
;
83 static int mips_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
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
) {
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
) {
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
)
164 as
->suspend_state
= SUSPEND_PENDING
;
169 queue_add_event(&as
->queue
, event
);
172 static void queue_event(apm_event_t event
, struct apm_user
*sender
)
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)
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
;
213 int i
= count
, ret
= 0;
215 if (count
< sizeof(apm_event_t
))
218 if (queue_empty(&as
->queue
) && fp
->f_flags
& O_NONBLOCK
)
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
);
227 if (copy_to_user(buf
, &event
, sizeof(event
)))
230 if (event
== APM_SYS_SUSPEND
|| event
== APM_USER_SUSPEND
)
231 as
->suspend_state
= SUSPEND_READ
;
233 buf
+= sizeof(event
);
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
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.
262 apm_ioctl(struct inode
* inode
, struct file
*filp
, unsigned int cmd
, unsigned long arg
)
264 struct apm_user
*as
= filp
->private_data
;
268 if (!as
->suser
|| !as
->writer
)
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
;
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
290 queue_event(APM_USER_SUSPEND
, as
);
294 * If there are no further acknowledges required, suspend
297 if (suspends_pending
== 0)
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
);
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
;
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
);
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)
355 static int apm_open(struct inode
* inode
, struct file
* filp
)
359 as
= (struct apm_user
*)kzalloc(sizeof(*as
), GFP_KERNEL
);
362 * XXX - this is a tiny bit broken, when we consider BSD
363 * process accounting. If the device is opened by root, we
364 * instantly flag that we used superuser privs. Who knows,
365 * we might close the device immediately without doing a
366 * privileged operation -- cevans
368 as
->suser
= capable(CAP_SYS_ADMIN
);
369 as
->writer
= (filp
->f_mode
& FMODE_WRITE
) == FMODE_WRITE
;
370 as
->reader
= (filp
->f_mode
& FMODE_READ
) == FMODE_READ
;
372 down_write(&user_list_lock
);
373 list_add(&as
->list
, &apm_user_list
);
374 up_write(&user_list_lock
);
376 filp
->private_data
= as
;
379 return as
? 0 : -ENOMEM
;
382 static struct file_operations apm_bios_fops
= {
383 .owner
= THIS_MODULE
,
388 .release
= apm_release
,
391 static struct miscdevice apm_device
= {
392 .minor
= APM_MINOR_DEV
,
394 .fops
= &apm_bios_fops
398 #ifdef CONFIG_PROC_FS
400 * Arguments, with symbols from linux/apm_bios.h.
402 * 0) Linux driver version (this will change if format changes)
403 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
404 * 2) APM flags from APM Installation Check (0x00):
405 * bit 0: APM_16_BIT_SUPPORT
406 * bit 1: APM_32_BIT_SUPPORT
407 * bit 2: APM_IDLE_SLOWS_CLOCK
408 * bit 3: APM_BIOS_DISABLED
409 * bit 4: APM_BIOS_DISENGAGED
413 * 0x02: On backup power (BIOS >= 1.1 only)
420 * 0x04: Selected battery not present (BIOS >= 1.2 only)
427 * bit 7: No system battery
429 * 6) Remaining battery life (percentage of charge):
432 * 7) Remaining battery life (time units):
433 * Number of remaining minutes or seconds
435 * 8) min = minutes; sec = seconds
437 static int apm_get_info(char *buf
, char **start
, off_t fpos
, int length
)
439 struct apm_power_info info
;
443 info
.ac_line_status
= 0xff;
444 info
.battery_status
= 0xff;
445 info
.battery_flag
= 0xff;
446 info
.battery_life
= -1;
450 if (apm_get_power_status
)
451 apm_get_power_status(&info
);
453 switch (info
.units
) {
454 default: units
= "?"; break;
455 case 0: units
= "min"; break;
456 case 1: units
= "sec"; break;
459 ret
= sprintf(buf
, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
460 driver_version
, APM_32_BIT_SUPPORT
,
461 info
.ac_line_status
, info
.battery_status
,
462 info
.battery_flag
, info
.battery_life
,
469 static int kapmd(void *arg
)
472 current
->flags
|= PF_NOFREEZE
;
477 wait_event_interruptible(kapmd_wait
,
478 !queue_empty(&kapmd_queue
) || !mips_apm_active
);
480 if (!mips_apm_active
)
483 spin_lock_irq(&kapmd_queue_lock
);
485 if (!queue_empty(&kapmd_queue
))
486 event
= queue_get_event(&kapmd_queue
);
487 spin_unlock_irq(&kapmd_queue_lock
);
493 case APM_LOW_BATTERY
:
494 case APM_POWER_STATUS_CHANGE
:
495 queue_event(event
, NULL
);
498 case APM_USER_SUSPEND
:
499 case APM_SYS_SUSPEND
:
500 queue_event(event
, NULL
);
501 if (suspends_pending
== 0)
505 case APM_CRITICAL_SUSPEND
:
511 complete_and_exit(&kapmd_exit
, 0);
514 static int __init
apm_init(void)
519 printk(KERN_NOTICE
"apm: disabled on user request.\n");
525 ret
= kernel_thread(kapmd
, NULL
, CLONE_KERNEL
);
531 #ifdef CONFIG_PROC_FS
532 create_proc_info_entry("apm", 0, NULL
, apm_get_info
);
535 ret
= misc_register(&apm_device
);
537 remove_proc_entry("apm", NULL
);
540 wake_up(&kapmd_wait
);
541 wait_for_completion(&kapmd_exit
);
547 static void __exit
apm_exit(void)
549 misc_deregister(&apm_device
);
550 remove_proc_entry("apm", NULL
);
553 wake_up(&kapmd_wait
);
554 wait_for_completion(&kapmd_exit
);
557 module_init(apm_init
);
558 module_exit(apm_exit
);
560 MODULE_AUTHOR("Stephen Rothwell");
561 MODULE_DESCRIPTION("Advanced Power Management");
562 MODULE_LICENSE("GPL");
565 static int __init
apm_setup(char *str
)
567 while ((str
!= NULL
) && (*str
!= '\0')) {
568 if (strncmp(str
, "off", 3) == 0)
570 if (strncmp(str
, "on", 2) == 0)
572 str
= strchr(str
, ',');
574 str
+= strspn(str
, ", \t");
579 __setup("apm=", apm_setup
);
583 * apm_queue_event - queue an APM event for kapmd
586 * Queue an APM event for kapmd to process and ultimately take the
587 * appropriate action. Only a subset of events are handled:
589 * %APM_POWER_STATUS_CHANGE
592 * %APM_CRITICAL_SUSPEND
594 void apm_queue_event(apm_event_t event
)
598 spin_lock_irqsave(&kapmd_queue_lock
, flags
);
599 queue_add_event(&kapmd_queue
, event
);
600 spin_unlock_irqrestore(&kapmd_queue_lock
, flags
);
602 wake_up_interruptible(&kapmd_wait
);
604 EXPORT_SYMBOL(apm_queue_event
);