2 * bios-less APM driver for hp680
4 * Copyright 2005 (c) Andriy Skulysh <askulysh@gmail.com>
6 * based on ARM APM driver by
7 * Jamey Hicks <jamey@crl.dec.com>
9 * adapted from the APM BIOS driver for Linux by
10 * Stephen Rothwell (sfr@linuxcare.com)
13 * Intel Corporation, Microsoft Corporation. Advanced Power Management
14 * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
16 * [This document is available from Microsoft at:
17 * http://www.microsoft.com/hwdev/busbios/amp_12.htm]
19 #include <linux/module.h>
20 #include <linux/poll.h>
21 #include <linux/timer.h>
22 #include <linux/slab.h>
23 #include <linux/proc_fs.h>
24 #include <linux/miscdevice.h>
25 #include <linux/apm_bios.h>
27 #include <linux/pm_legacy.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 * Maximum number of events stored
41 #define APM_MAX_EVENTS 16
44 unsigned int event_head
;
45 unsigned int event_tail
;
46 apm_event_t events
[APM_MAX_EVENTS
];
50 * The per-file APM data
53 struct list_head list
;
55 unsigned int suser
: 1;
56 unsigned int writer
: 1;
57 unsigned int reader
: 1;
60 unsigned int suspend_state
;
61 #define SUSPEND_NONE 0 /* no suspend pending */
62 #define SUSPEND_PENDING 1 /* suspend pending read */
63 #define SUSPEND_READ 2 /* suspend read, pending ack */
64 #define SUSPEND_ACKED 3 /* suspend acked */
65 #define SUSPEND_DONE 4 /* suspend completed */
67 struct apm_queue queue
;
73 static int suspends_pending
;
75 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue
);
76 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue
);
79 * This is a list of everyone who has opened /dev/apm_bios
81 static DECLARE_RWSEM(user_list_lock
);
82 static LIST_HEAD(apm_user_list
);
85 * kapmd info. kapmd provides us a process context to handle
86 * "APM" events within - specifically necessary if we're going
87 * to be suspending the system.
89 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait
);
90 static DECLARE_COMPLETION(kapmd_exit
);
91 static DEFINE_SPINLOCK(kapmd_queue_lock
);
92 static struct apm_queue kapmd_queue
;
95 EXPORT_SYMBOL(apm_suspended
);
97 /* Platform-specific apm_read_proc(). */
98 int (*apm_get_info
)(char *buf
, char **start
, off_t fpos
, int length
);
99 EXPORT_SYMBOL(apm_get_info
);
102 * APM event queue management.
104 static inline int queue_empty(struct apm_queue
*q
)
106 return q
->event_head
== q
->event_tail
;
109 static inline apm_event_t
queue_get_event(struct apm_queue
*q
)
111 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
112 return q
->events
[q
->event_tail
];
115 static void queue_add_event(struct apm_queue
*q
, apm_event_t event
)
117 q
->event_head
= (q
->event_head
+ 1) % APM_MAX_EVENTS
;
118 if (q
->event_head
== q
->event_tail
) {
122 printk(KERN_ERR
"apm: an event queue overflowed\n");
124 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
126 q
->events
[q
->event_head
] = event
;
129 static void queue_event_one_user(struct apm_user
*as
, apm_event_t event
)
131 if (as
->suser
&& as
->writer
) {
133 case APM_SYS_SUSPEND
:
134 case APM_USER_SUSPEND
:
136 * If this user already has a suspend pending,
137 * don't queue another one.
139 if (as
->suspend_state
!= SUSPEND_NONE
)
142 as
->suspend_state
= SUSPEND_PENDING
;
147 queue_add_event(&as
->queue
, event
);
150 static void queue_event(apm_event_t event
, struct apm_user
*sender
)
154 down_read(&user_list_lock
);
156 list_for_each_entry(as
, &apm_user_list
, list
)
157 if (as
!= sender
&& as
->reader
)
158 queue_event_one_user(as
, event
);
160 up_read(&user_list_lock
);
161 wake_up_interruptible(&apm_waitqueue
);
165 * apm_queue_event - queue an APM event for kapmd
168 * Queue an APM event for kapmd to process and ultimately take the
169 * appropriate action. Only a subset of events are handled:
171 * %APM_POWER_STATUS_CHANGE
174 * %APM_CRITICAL_SUSPEND
176 void apm_queue_event(apm_event_t event
)
178 spin_lock_irq(&kapmd_queue_lock
);
179 queue_add_event(&kapmd_queue
, event
);
180 spin_unlock_irq(&kapmd_queue_lock
);
182 wake_up_interruptible(&kapmd_wait
);
184 EXPORT_SYMBOL(apm_queue_event
);
186 static void apm_suspend(void)
192 err
= pm_suspend(PM_SUSPEND_MEM
);
195 * Anyone on the APM queues will think we're still suspended.
196 * Send a message so everyone knows we're now awake again.
198 queue_event(APM_NORMAL_RESUME
, NULL
);
201 * Finally, wake up anyone who is sleeping on the suspend.
203 down_read(&user_list_lock
);
204 list_for_each_entry(as
, &apm_user_list
, list
) {
205 as
->suspend_result
= err
;
206 as
->suspend_state
= SUSPEND_DONE
;
208 up_read(&user_list_lock
);
210 wake_up(&apm_suspend_waitqueue
);
214 static ssize_t
apm_read(struct file
*fp
, char __user
*buf
,
215 size_t count
, loff_t
*ppos
)
217 struct apm_user
*as
= fp
->private_data
;
219 int i
= count
, ret
= 0;
221 if (count
< sizeof(apm_event_t
))
224 if (queue_empty(&as
->queue
) && fp
->f_flags
& O_NONBLOCK
)
227 wait_event_interruptible(apm_waitqueue
, !queue_empty(&as
->queue
));
229 while ((i
>= sizeof(event
)) && !queue_empty(&as
->queue
)) {
230 event
= queue_get_event(&as
->queue
);
233 if (copy_to_user(buf
, &event
, sizeof(event
)))
236 if (event
== APM_SYS_SUSPEND
|| event
== APM_USER_SUSPEND
)
237 as
->suspend_state
= SUSPEND_READ
;
239 buf
+= sizeof(event
);
249 static unsigned int apm_poll(struct file
*fp
, poll_table
* wait
)
251 struct apm_user
*as
= fp
->private_data
;
253 poll_wait(fp
, &apm_waitqueue
, wait
);
254 return queue_empty(&as
->queue
) ? 0 : POLLIN
| POLLRDNORM
;
258 * apm_ioctl - handle APM ioctl
261 * This IOCTL is overloaded, and performs two functions. It is used to:
262 * - initiate a suspend
263 * - acknowledge a suspend read from /dev/apm_bios.
264 * Only when everyone who has opened /dev/apm_bios with write permission
265 * has acknowledge does the actual suspend happen.
268 apm_ioctl(struct inode
* inode
, struct file
*filp
, u_int cmd
, u_long arg
)
270 struct apm_user
*as
= filp
->private_data
;
274 if (!as
->suser
|| !as
->writer
)
278 case APM_IOC_SUSPEND
:
279 as
->suspend_result
= -EINTR
;
281 if (as
->suspend_state
== SUSPEND_READ
) {
283 * If we read a suspend command from /dev/apm_bios,
284 * then the corresponding APM_IOC_SUSPEND ioctl is
285 * interpreted as an acknowledge.
287 as
->suspend_state
= SUSPEND_ACKED
;
291 * Otherwise it is a request to suspend the system.
292 * Queue an event for all readers, and expect an
293 * acknowledge from all writers who haven't already
296 queue_event(APM_USER_SUSPEND
, as
);
300 * If there are no further acknowledges required, suspend
303 if (suspends_pending
== 0)
307 * Wait for the suspend/resume to complete. If there are
308 * pending acknowledges, we wait here for them.
310 * Note that we need to ensure that the PM subsystem does
311 * not kick us out of the wait when it suspends the threads.
313 flags
= current
->flags
;
314 current
->flags
|= PF_NOFREEZE
;
317 * Note: do not allow a thread which is acking the suspend
318 * to escape until the resume is complete.
320 if (as
->suspend_state
== SUSPEND_ACKED
)
321 wait_event(apm_suspend_waitqueue
,
322 as
->suspend_state
== SUSPEND_DONE
);
324 wait_event_interruptible(apm_suspend_waitqueue
,
325 as
->suspend_state
== SUSPEND_DONE
);
327 current
->flags
= flags
;
328 err
= as
->suspend_result
;
329 as
->suspend_state
= SUSPEND_NONE
;
336 static int apm_release(struct inode
* inode
, struct file
* filp
)
338 struct apm_user
*as
= filp
->private_data
;
339 filp
->private_data
= NULL
;
341 down_write(&user_list_lock
);
343 up_write(&user_list_lock
);
346 * We are now unhooked from the chain. As far as new
347 * events are concerned, we no longer exist. However, we
348 * need to balance suspends_pending, which means the
349 * possibility of sleeping.
351 if (as
->suspend_state
!= SUSPEND_NONE
) {
352 suspends_pending
-= 1;
353 if (suspends_pending
== 0)
361 static int apm_open(struct inode
* inode
, struct file
* filp
)
365 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
368 * XXX - this is a tiny bit broken, when we consider BSD
369 * process accounting. If the device is opened by root, we
370 * instantly flag that we used superuser privs. Who knows,
371 * we might close the device immediately without doing a
372 * privileged operation -- cevans
374 as
->suser
= capable(CAP_SYS_ADMIN
);
375 as
->writer
= (filp
->f_mode
& FMODE_WRITE
) == FMODE_WRITE
;
376 as
->reader
= (filp
->f_mode
& FMODE_READ
) == FMODE_READ
;
378 down_write(&user_list_lock
);
379 list_add(&as
->list
, &apm_user_list
);
380 up_write(&user_list_lock
);
382 filp
->private_data
= as
;
385 return as
? 0 : -ENOMEM
;
388 static struct file_operations apm_bios_fops
= {
389 .owner
= THIS_MODULE
,
394 .release
= apm_release
,
397 static struct miscdevice apm_device
= {
398 .minor
= APM_MINOR_DEV
,
400 .fops
= &apm_bios_fops
404 #ifdef CONFIG_PROC_FS
406 * Arguments, with symbols from linux/apm_bios.h.
408 * 0) Linux driver version (this will change if format changes)
409 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
410 * 2) APM flags from APM Installation Check (0x00):
411 * bit 0: APM_16_BIT_SUPPORT
412 * bit 1: APM_32_BIT_SUPPORT
413 * bit 2: APM_IDLE_SLOWS_CLOCK
414 * bit 3: APM_BIOS_DISABLED
415 * bit 4: APM_BIOS_DISENGAGED
419 * 0x02: On backup power (BIOS >= 1.1 only)
426 * 0x04: Selected battery not present (BIOS >= 1.2 only)
433 * bit 7: No system battery
435 * 6) Remaining battery life (percentage of charge):
438 * 7) Remaining battery life (time units):
439 * Number of remaining minutes or seconds
441 * 8) min = minutes; sec = seconds
443 static int apm_read_proc(char *buf
, char **start
, off_t fpos
, int length
)
445 if (likely(apm_get_info
))
446 return apm_get_info(buf
, start
, fpos
, length
);
452 static int kapmd(void *arg
)
455 current
->flags
|= PF_NOFREEZE
;
460 wait_event_interruptible(kapmd_wait
,
461 !queue_empty(&kapmd_queue
) || !pm_active
);
466 spin_lock_irq(&kapmd_queue_lock
);
468 if (!queue_empty(&kapmd_queue
))
469 event
= queue_get_event(&kapmd_queue
);
470 spin_unlock_irq(&kapmd_queue_lock
);
476 case APM_LOW_BATTERY
:
477 case APM_POWER_STATUS_CHANGE
:
478 queue_event(event
, NULL
);
481 case APM_USER_SUSPEND
:
482 case APM_SYS_SUSPEND
:
483 queue_event(event
, NULL
);
484 if (suspends_pending
== 0)
488 case APM_CRITICAL_SUSPEND
:
494 complete_and_exit(&kapmd_exit
, 0);
497 static int __init
apm_init(void)
503 ret
= kernel_thread(kapmd
, NULL
, CLONE_KERNEL
);
504 if (unlikely(ret
< 0)) {
509 create_proc_info_entry("apm", 0, NULL
, apm_read_proc
);
511 ret
= misc_register(&apm_device
);
512 if (unlikely(ret
!= 0)) {
513 remove_proc_entry("apm", NULL
);
516 wake_up(&kapmd_wait
);
517 wait_for_completion(&kapmd_exit
);
523 static void __exit
apm_exit(void)
525 misc_deregister(&apm_device
);
526 remove_proc_entry("apm", NULL
);
529 wake_up(&kapmd_wait
);
530 wait_for_completion(&kapmd_exit
);
533 module_init(apm_init
);
534 module_exit(apm_exit
);
536 MODULE_AUTHOR("Stephen Rothwell, Andriy Skulysh");
537 MODULE_DESCRIPTION("Advanced Power Management");
538 MODULE_LICENSE("GPL");