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/config.h>
20 #include <linux/module.h>
21 #include <linux/poll.h>
22 #include <linux/timer.h>
23 #include <linux/slab.h>
24 #include <linux/proc_fs.h>
25 #include <linux/miscdevice.h>
26 #include <linux/apm_bios.h>
28 #include <linux/pm_legacy.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 * Maximum number of events stored
42 #define APM_MAX_EVENTS 16
45 unsigned int event_head
;
46 unsigned int event_tail
;
47 apm_event_t events
[APM_MAX_EVENTS
];
51 * The per-file APM data
54 struct list_head list
;
56 unsigned int suser
: 1;
57 unsigned int writer
: 1;
58 unsigned int reader
: 1;
61 unsigned int suspend_state
;
62 #define SUSPEND_NONE 0 /* no suspend pending */
63 #define SUSPEND_PENDING 1 /* suspend pending read */
64 #define SUSPEND_READ 2 /* suspend read, pending ack */
65 #define SUSPEND_ACKED 3 /* suspend acked */
66 #define SUSPEND_DONE 4 /* suspend completed */
68 struct apm_queue queue
;
74 static int suspends_pending
;
76 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue
);
77 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue
);
80 * This is a list of everyone who has opened /dev/apm_bios
82 static DECLARE_RWSEM(user_list_lock
);
83 static LIST_HEAD(apm_user_list
);
86 * kapmd info. kapmd provides us a process context to handle
87 * "APM" events within - specifically necessary if we're going
88 * to be suspending the system.
90 static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait
);
91 static DECLARE_COMPLETION(kapmd_exit
);
92 static DEFINE_SPINLOCK(kapmd_queue_lock
);
93 static struct apm_queue kapmd_queue
;
96 EXPORT_SYMBOL(apm_suspended
);
98 /* Platform-specific apm_read_proc(). */
99 int (*apm_get_info
)(char *buf
, char **start
, off_t fpos
, int length
);
100 EXPORT_SYMBOL(apm_get_info
);
103 * APM event queue management.
105 static inline int queue_empty(struct apm_queue
*q
)
107 return q
->event_head
== q
->event_tail
;
110 static inline apm_event_t
queue_get_event(struct apm_queue
*q
)
112 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
113 return q
->events
[q
->event_tail
];
116 static void queue_add_event(struct apm_queue
*q
, apm_event_t event
)
118 q
->event_head
= (q
->event_head
+ 1) % APM_MAX_EVENTS
;
119 if (q
->event_head
== q
->event_tail
) {
123 printk(KERN_ERR
"apm: an event queue overflowed\n");
125 q
->event_tail
= (q
->event_tail
+ 1) % APM_MAX_EVENTS
;
127 q
->events
[q
->event_head
] = event
;
130 static void queue_event_one_user(struct apm_user
*as
, apm_event_t event
)
132 if (as
->suser
&& as
->writer
) {
134 case APM_SYS_SUSPEND
:
135 case APM_USER_SUSPEND
:
137 * If this user already has a suspend pending,
138 * don't queue another one.
140 if (as
->suspend_state
!= SUSPEND_NONE
)
143 as
->suspend_state
= SUSPEND_PENDING
;
148 queue_add_event(&as
->queue
, event
);
151 static void queue_event(apm_event_t event
, struct apm_user
*sender
)
155 down_read(&user_list_lock
);
157 list_for_each_entry(as
, &apm_user_list
, list
)
158 if (as
!= sender
&& as
->reader
)
159 queue_event_one_user(as
, event
);
161 up_read(&user_list_lock
);
162 wake_up_interruptible(&apm_waitqueue
);
166 * apm_queue_event - queue an APM event for kapmd
169 * Queue an APM event for kapmd to process and ultimately take the
170 * appropriate action. Only a subset of events are handled:
172 * %APM_POWER_STATUS_CHANGE
175 * %APM_CRITICAL_SUSPEND
177 void apm_queue_event(apm_event_t event
)
179 spin_lock_irq(&kapmd_queue_lock
);
180 queue_add_event(&kapmd_queue
, event
);
181 spin_unlock_irq(&kapmd_queue_lock
);
183 wake_up_interruptible(&kapmd_wait
);
185 EXPORT_SYMBOL(apm_queue_event
);
187 static void apm_suspend(void)
193 err
= pm_suspend(PM_SUSPEND_MEM
);
196 * Anyone on the APM queues will think we're still suspended.
197 * Send a message so everyone knows we're now awake again.
199 queue_event(APM_NORMAL_RESUME
, NULL
);
202 * Finally, wake up anyone who is sleeping on the suspend.
204 down_read(&user_list_lock
);
205 list_for_each_entry(as
, &apm_user_list
, list
) {
206 as
->suspend_result
= err
;
207 as
->suspend_state
= SUSPEND_DONE
;
209 up_read(&user_list_lock
);
211 wake_up(&apm_suspend_waitqueue
);
215 static ssize_t
apm_read(struct file
*fp
, char __user
*buf
,
216 size_t count
, loff_t
*ppos
)
218 struct apm_user
*as
= fp
->private_data
;
220 int i
= count
, ret
= 0;
222 if (count
< sizeof(apm_event_t
))
225 if (queue_empty(&as
->queue
) && fp
->f_flags
& O_NONBLOCK
)
228 wait_event_interruptible(apm_waitqueue
, !queue_empty(&as
->queue
));
230 while ((i
>= sizeof(event
)) && !queue_empty(&as
->queue
)) {
231 event
= queue_get_event(&as
->queue
);
234 if (copy_to_user(buf
, &event
, sizeof(event
)))
237 if (event
== APM_SYS_SUSPEND
|| event
== APM_USER_SUSPEND
)
238 as
->suspend_state
= SUSPEND_READ
;
240 buf
+= sizeof(event
);
250 static unsigned int apm_poll(struct file
*fp
, poll_table
* wait
)
252 struct apm_user
*as
= fp
->private_data
;
254 poll_wait(fp
, &apm_waitqueue
, wait
);
255 return queue_empty(&as
->queue
) ? 0 : POLLIN
| POLLRDNORM
;
259 * apm_ioctl - handle APM ioctl
262 * This IOCTL is overloaded, and performs two functions. It is used to:
263 * - initiate a suspend
264 * - acknowledge a suspend read from /dev/apm_bios.
265 * Only when everyone who has opened /dev/apm_bios with write permission
266 * has acknowledge does the actual suspend happen.
269 apm_ioctl(struct inode
* inode
, struct file
*filp
, u_int cmd
, u_long arg
)
271 struct apm_user
*as
= filp
->private_data
;
275 if (!as
->suser
|| !as
->writer
)
279 case APM_IOC_SUSPEND
:
280 as
->suspend_result
= -EINTR
;
282 if (as
->suspend_state
== SUSPEND_READ
) {
284 * If we read a suspend command from /dev/apm_bios,
285 * then the corresponding APM_IOC_SUSPEND ioctl is
286 * interpreted as an acknowledge.
288 as
->suspend_state
= SUSPEND_ACKED
;
292 * Otherwise it is a request to suspend the system.
293 * Queue an event for all readers, and expect an
294 * acknowledge from all writers who haven't already
297 queue_event(APM_USER_SUSPEND
, as
);
301 * If there are no further acknowledges required, suspend
304 if (suspends_pending
== 0)
308 * Wait for the suspend/resume to complete. If there are
309 * pending acknowledges, we wait here for them.
311 * Note that we need to ensure that the PM subsystem does
312 * not kick us out of the wait when it suspends the threads.
314 flags
= current
->flags
;
315 current
->flags
|= PF_NOFREEZE
;
318 * Note: do not allow a thread which is acking the suspend
319 * to escape until the resume is complete.
321 if (as
->suspend_state
== SUSPEND_ACKED
)
322 wait_event(apm_suspend_waitqueue
,
323 as
->suspend_state
== SUSPEND_DONE
);
325 wait_event_interruptible(apm_suspend_waitqueue
,
326 as
->suspend_state
== SUSPEND_DONE
);
328 current
->flags
= flags
;
329 err
= as
->suspend_result
;
330 as
->suspend_state
= SUSPEND_NONE
;
337 static int apm_release(struct inode
* inode
, struct file
* filp
)
339 struct apm_user
*as
= filp
->private_data
;
340 filp
->private_data
= NULL
;
342 down_write(&user_list_lock
);
344 up_write(&user_list_lock
);
347 * We are now unhooked from the chain. As far as new
348 * events are concerned, we no longer exist. However, we
349 * need to balance suspends_pending, which means the
350 * possibility of sleeping.
352 if (as
->suspend_state
!= SUSPEND_NONE
) {
353 suspends_pending
-= 1;
354 if (suspends_pending
== 0)
362 static int apm_open(struct inode
* inode
, struct file
* filp
)
366 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
369 * XXX - this is a tiny bit broken, when we consider BSD
370 * process accounting. If the device is opened by root, we
371 * instantly flag that we used superuser privs. Who knows,
372 * we might close the device immediately without doing a
373 * privileged operation -- cevans
375 as
->suser
= capable(CAP_SYS_ADMIN
);
376 as
->writer
= (filp
->f_mode
& FMODE_WRITE
) == FMODE_WRITE
;
377 as
->reader
= (filp
->f_mode
& FMODE_READ
) == FMODE_READ
;
379 down_write(&user_list_lock
);
380 list_add(&as
->list
, &apm_user_list
);
381 up_write(&user_list_lock
);
383 filp
->private_data
= as
;
386 return as
? 0 : -ENOMEM
;
389 static struct file_operations apm_bios_fops
= {
390 .owner
= THIS_MODULE
,
395 .release
= apm_release
,
398 static struct miscdevice apm_device
= {
399 .minor
= APM_MINOR_DEV
,
401 .fops
= &apm_bios_fops
405 #ifdef CONFIG_PROC_FS
407 * Arguments, with symbols from linux/apm_bios.h.
409 * 0) Linux driver version (this will change if format changes)
410 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
411 * 2) APM flags from APM Installation Check (0x00):
412 * bit 0: APM_16_BIT_SUPPORT
413 * bit 1: APM_32_BIT_SUPPORT
414 * bit 2: APM_IDLE_SLOWS_CLOCK
415 * bit 3: APM_BIOS_DISABLED
416 * bit 4: APM_BIOS_DISENGAGED
420 * 0x02: On backup power (BIOS >= 1.1 only)
427 * 0x04: Selected battery not present (BIOS >= 1.2 only)
434 * bit 7: No system battery
436 * 6) Remaining battery life (percentage of charge):
439 * 7) Remaining battery life (time units):
440 * Number of remaining minutes or seconds
442 * 8) min = minutes; sec = seconds
444 static int apm_read_proc(char *buf
, char **start
, off_t fpos
, int length
)
446 if (likely(apm_get_info
))
447 return apm_get_info(buf
, start
, fpos
, length
);
453 static int kapmd(void *arg
)
456 current
->flags
|= PF_NOFREEZE
;
461 wait_event_interruptible(kapmd_wait
,
462 !queue_empty(&kapmd_queue
) || !pm_active
);
467 spin_lock_irq(&kapmd_queue_lock
);
469 if (!queue_empty(&kapmd_queue
))
470 event
= queue_get_event(&kapmd_queue
);
471 spin_unlock_irq(&kapmd_queue_lock
);
477 case APM_LOW_BATTERY
:
478 case APM_POWER_STATUS_CHANGE
:
479 queue_event(event
, NULL
);
482 case APM_USER_SUSPEND
:
483 case APM_SYS_SUSPEND
:
484 queue_event(event
, NULL
);
485 if (suspends_pending
== 0)
489 case APM_CRITICAL_SUSPEND
:
495 complete_and_exit(&kapmd_exit
, 0);
498 static int __init
apm_init(void)
504 ret
= kernel_thread(kapmd
, NULL
, CLONE_KERNEL
);
505 if (unlikely(ret
< 0)) {
510 create_proc_info_entry("apm", 0, NULL
, apm_read_proc
);
512 ret
= misc_register(&apm_device
);
513 if (unlikely(ret
!= 0)) {
514 remove_proc_entry("apm", NULL
);
517 wake_up(&kapmd_wait
);
518 wait_for_completion(&kapmd_exit
);
524 static void __exit
apm_exit(void)
526 misc_deregister(&apm_device
);
527 remove_proc_entry("apm", NULL
);
530 wake_up(&kapmd_wait
);
531 wait_for_completion(&kapmd_exit
);
534 module_init(apm_init
);
535 module_exit(apm_exit
);
537 MODULE_AUTHOR("Stephen Rothwell, Andriy Skulysh");
538 MODULE_DESCRIPTION("Advanced Power Management");
539 MODULE_LICENSE("GPL");