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)
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>
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 20
52 * The per-file APM data
55 struct list_head list;
65 unsigned int suspends_read;
66 unsigned int standbys_read;
70 apm_event_t events[APM_MAX_EVENTS];
76 static int suspends_pending;
77 static int standbys_pending;
78 static int apm_disabled;
80 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
81 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
84 * This is a list of everyone who has opened /dev/apm_bios
86 static spinlock_t user_list_lock = SPIN_LOCK_UNLOCKED;
87 static LIST_HEAD(apm_user_list);
92 static struct task_struct *kapmd;
93 static DECLARE_COMPLETION(kapmd_exit);
95 static const char driver_version[] = "1.13"; /* no spaces */
100 * Compatibility cruft until the IPAQ people move over to the new
103 static void __apm_get_power_status(struct apm_power_info *info)
105 #if 0 && defined(CONFIG_SA1100_H3600) && defined(CONFIG_TOUCHSCREEN_H3600)
106 extern int h3600_apm_get_power_status(u_char *, u_char *, u_char *,
107 u_char *, u_short *);
109 if (machine_is_h3600()) {
111 h3600_apm_get_power_status(&info->ac_line_status,
112 &info->battery_status, &info->battery_flag,
113 &info->battery_life, &dx);
114 info->time = dx & 0x7fff;
115 info->units = dx & 0x8000 ? 0 : 1;
121 * This allows machines to provide their own "apm get power status" function.
123 void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
124 EXPORT_SYMBOL(apm_get_power_status);
126 static int queue_empty(struct apm_user *as)
128 return as->event_head == as->event_tail;
131 static apm_event_t get_queued_event(struct apm_user *as)
133 as->event_tail = (as->event_tail + 1) % APM_MAX_EVENTS;
134 return as->events[as->event_tail];
137 static void queue_event_one_user(struct apm_user *as, apm_event_t event)
139 as->event_head = (as->event_head + 1) % APM_MAX_EVENTS;
140 if (as->event_head == as->event_tail) {
144 printk(KERN_ERR "apm: an event queue overflowed\n");
145 as->event_tail = (as->event_tail + 1) % APM_MAX_EVENTS;
147 as->events[as->event_head] = event;
149 if (!as->suser || !as->writer)
153 case APM_SYS_SUSPEND:
154 case APM_USER_SUSPEND:
155 as->suspends_pending++;
159 case APM_SYS_STANDBY:
160 case APM_USER_STANDBY:
161 as->standbys_pending++;
167 static void queue_event(apm_event_t event, struct apm_user *sender)
171 spin_lock(&user_list_lock);
172 list_for_each(l, &apm_user_list) {
173 struct apm_user *as = list_entry(l, struct apm_user, list);
175 if (as != sender && as->reader)
176 queue_event_one_user(as, event);
178 spin_unlock(&user_list_lock);
179 wake_up_interruptible(&apm_waitqueue);
182 static int apm_suspend(void)
185 int err = pm_suspend(PM_SUSPEND_MEM);
188 * Anyone on the APM queues will think we're still suspended.
189 * Send a message so everyone knows we're now awake again.
191 queue_event(APM_NORMAL_RESUME, NULL);
194 * Finally, wake up anyone who is sleeping on the suspend.
196 spin_lock(&user_list_lock);
197 list_for_each(l, &apm_user_list) {
198 struct apm_user *as = list_entry(l, struct apm_user, list);
200 as->suspend_result = err;
201 as->suspend_wait = 0;
203 spin_unlock(&user_list_lock);
205 wake_up_interruptible(&apm_suspend_waitqueue);
209 static ssize_t apm_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
211 struct apm_user *as = fp->private_data;
213 int i = count, ret = 0, nonblock = fp->f_flags & O_NONBLOCK;
215 if (count < sizeof(apm_event_t))
218 if (queue_empty(as) && nonblock)
221 wait_event_interruptible(apm_waitqueue, !queue_empty(as));
223 while ((i >= sizeof(event)) && !queue_empty(as)) {
224 event = get_queued_event(as);
225 printk(" apm_read: event=%d\n", event);
228 if (copy_to_user(buf, &event, sizeof(event)))
232 case APM_SYS_SUSPEND:
233 case APM_USER_SUSPEND:
237 case APM_SYS_STANDBY:
238 case APM_USER_STANDBY:
243 buf += sizeof(event);
253 static unsigned int apm_poll(struct file *fp, poll_table * wait)
255 struct apm_user * as = fp->private_data;
257 poll_wait(fp, &apm_waitqueue, wait);
258 return queue_empty(as) ? 0 : POLLIN | POLLRDNORM;
262 * apm_ioctl - handle APM ioctl
265 * This IOCTL is overloaded, and performs two functions. It is used to:
266 * - initiate a suspend
267 * - acknowledge a suspend read from /dev/apm_bios.
268 * Only when everyone who has opened /dev/apm_bios with write permission
269 * has acknowledge does the actual suspend happen.
272 apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
274 struct apm_user *as = filp->private_data;
277 if (!as->suser || !as->writer)
281 case APM_IOC_STANDBY:
284 case APM_IOC_SUSPEND:
286 * If we read a suspend command from /dev/apm_bios,
287 * then the corresponding APM_IOC_SUSPEND ioctl is
288 * interpreted as an acknowledge.
290 if (as->suspends_read > 0) {
292 as->suspends_pending--;
295 queue_event(APM_USER_SUSPEND, as);
299 * If there are outstanding suspend requests for other
300 * people on /dev/apm_bios, we must sleep for them.
301 * Last one to bed turns the lights out.
303 if (suspends_pending > 0) {
304 as->suspend_wait = 1;
305 err = wait_event_interruptible(apm_suspend_waitqueue,
306 as->suspend_wait == 0);
308 err = as->suspend_result;
318 static int apm_release(struct inode * inode, struct file * filp)
320 struct apm_user *as = filp->private_data;
321 filp->private_data = NULL;
323 spin_lock(&user_list_lock);
325 spin_unlock(&user_list_lock);
328 * We are now unhooked from the chain. As far as new
329 * events are concerned, we no longer exist. However, we
330 * need to balance standbys_pending and suspends_pending,
331 * which means the possibility of sleeping.
333 if (as->standbys_pending > 0) {
334 standbys_pending -= as->standbys_pending;
335 // if (standbys_pending <= 0)
338 if (as->suspends_pending > 0) {
339 suspends_pending -= as->suspends_pending;
340 if (suspends_pending <= 0)
348 static int apm_open(struct inode * inode, struct file * filp)
352 as = (struct apm_user *)kmalloc(sizeof(*as), GFP_KERNEL);
354 memset(as, 0, sizeof(*as));
357 * XXX - this is a tiny bit broken, when we consider BSD
358 * process accounting. If the device is opened by root, we
359 * instantly flag that we used superuser privs. Who knows,
360 * we might close the device immediately without doing a
361 * privileged operation -- cevans
363 as->suser = capable(CAP_SYS_ADMIN);
364 as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
365 as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
367 spin_lock(&user_list_lock);
368 list_add(&as->list, &apm_user_list);
369 spin_unlock(&user_list_lock);
371 filp->private_data = as;
374 return as ? 0 : -ENOMEM;
377 static struct file_operations apm_bios_fops = {
378 .owner = THIS_MODULE,
383 .release = apm_release,
386 static struct miscdevice apm_device = {
387 .minor = APM_MINOR_DEV,
389 .fops = &apm_bios_fops
393 #ifdef CONFIG_PROC_FS
395 * Arguments, with symbols from linux/apm_bios.h.
397 * 0) Linux driver version (this will change if format changes)
398 * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
399 * 2) APM flags from APM Installation Check (0x00):
400 * bit 0: APM_16_BIT_SUPPORT
401 * bit 1: APM_32_BIT_SUPPORT
402 * bit 2: APM_IDLE_SLOWS_CLOCK
403 * bit 3: APM_BIOS_DISABLED
404 * bit 4: APM_BIOS_DISENGAGED
408 * 0x02: On backup power (BIOS >= 1.1 only)
415 * 0x04: Selected battery not present (BIOS >= 1.2 only)
422 * bit 7: No system battery
424 * 6) Remaining battery life (percentage of charge):
427 * 7) Remaining battery life (time units):
428 * Number of remaining minutes or seconds
430 * 8) min = minutes; sec = seconds
432 static int apm_get_info(char *buf, char **start, off_t fpos, int length)
434 struct apm_power_info info;
438 info.ac_line_status = 0xff;
439 info.battery_status = 0xff;
440 info.battery_flag = 0xff;
441 info.battery_life = 255;
445 if (apm_get_power_status)
446 apm_get_power_status(&info);
448 switch (info.units) {
449 default: units = "?"; break;
450 case 0: units = "min"; break;
451 case 1: units = "sec"; break;
454 ret = sprintf(buf, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
455 driver_version, APM_32_BIT_SUPPORT,
456 info.ac_line_status, info.battery_status,
457 info.battery_flag, info.battery_life,
465 static int kapmd(void *startup)
467 struct task_struct *tsk = current;
470 strcpy(tsk->comm, "kapmd");
473 spin_lock_irq(&tsk->sigmask_lock);
474 siginitsetinv(&tsk->blocked, sigmask(SIGQUIT));
475 recalc_sigpending(tsk);
476 spin_unlock_irq(&tsk->sigmask_lock);
478 complete((struct completion *)startup);
481 set_task_state(tsk, TASK_INTERRUPTIBLE);
483 } while (!signal_pending(tsk));
485 complete_and_exit(&kapmd_exit, 0);
489 static int __init apm_init(void)
491 // struct completion startup = COMPLETION_INITIALIZER(startup);
495 printk(KERN_NOTICE "apm: disabled on user request.\n");
499 if (PM_IS_ACTIVE()) {
500 printk(KERN_NOTICE "apm: overridden by ACPI.\n");
504 // ret = kernel_thread(kapmd, &startup, CLONE_FS | CLONE_FILES);
507 // wait_for_completion(&startup);
511 #ifdef CONFIG_PROC_FS
512 create_proc_info_entry("apm", 0, NULL, apm_get_info);
515 ret = misc_register(&apm_device);
518 remove_proc_entry("apm", NULL);
519 send_sig(SIGQUIT, kapmd, 1);
520 wait_for_completion(&kapmd_exit);
526 static void __exit apm_exit(void)
528 misc_deregister(&apm_device);
529 remove_proc_entry("apm", NULL);
531 // send_sig(SIGQUIT, kapmd, 1);
532 // wait_for_completion(&kapmd_exit);
535 module_init(apm_init);
536 module_exit(apm_exit);
538 MODULE_AUTHOR("Stephen Rothwell");
539 MODULE_DESCRIPTION("Advanced Power Management");
540 MODULE_LICENSE("GPL");
543 static int __init apm_setup(char *str)
545 while ((str != NULL) && (*str != '\0')) {
546 if (strncmp(str, "off", 3) == 0)
548 if (strncmp(str, "on", 2) == 0)
550 str = strchr(str, ',');
552 str += strspn(str, ", \t");
557 __setup("apm=", apm_setup);