1 /* APM emulation layer for PowerMac
3 * Copyright 2001 Benjamin Herrenschmidt (benh@kernel.crashing.org)
5 * Lots of code inherited from apm.c, see appropriate notice in
6 * arch/i386/kernel/apm.c
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
21 #include <linux/module.h>
23 #include <linux/poll.h>
24 #include <linux/types.h>
25 #include <linux/stddef.h>
26 #include <linux/timer.h>
27 #include <linux/fcntl.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
30 #include <linux/proc_fs.h>
31 #include <linux/miscdevice.h>
32 #include <linux/apm_bios.h>
33 #include <linux/init.h>
34 #include <linux/sched.h>
36 #include <linux/kernel.h>
37 #include <linux/smp_lock.h>
39 #include <linux/adb.h>
40 #include <linux/pmu.h>
42 #include <asm/system.h>
43 #include <asm/uaccess.h>
44 #include <asm/machdep.h>
49 #define DBG(args...) printk(KERN_DEBUG args)
50 //#define DBG(args...) xmon_printf(args)
52 #define DBG(args...) do { } while (0)
56 * The apm_bios device is one of the misc char devices.
57 * This is its minor number.
59 #define APM_MINOR_DEV 134
62 * Maximum number of events stored
64 #define APM_MAX_EVENTS 20
66 #define FAKE_APM_BIOS_VERSION 0x0101
68 #define APM_USER_NOTIFY_TIMEOUT (5*HZ)
71 * The per-file APM data
75 struct apm_user * next;
77 int suspend_waiting: 1;
82 apm_event_t events[APM_MAX_EVENTS];
86 * The magic number in apm_user
88 #define APM_BIOS_MAGIC 0x4101
93 static int suspends_pending;
95 static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
96 static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
97 static struct apm_user * user_list;
99 static int apm_notify_sleep(struct pmu_sleep_notifier *self, int when);
100 static struct pmu_sleep_notifier apm_sleep_notifier = {
102 SLEEP_LEVEL_USERLAND,
105 static const char driver_version[] = "0.5"; /* no spaces */
108 static char * apm_event_name[] = {
114 "power status change",
119 "system standby resume",
120 "capabilities change"
122 #define NR_APM_EVENT_NAME \
123 (sizeof(apm_event_name) / sizeof(apm_event_name[0]))
127 static int queue_empty(struct apm_user *as)
129 return as->event_head == as->event_tail;
132 static apm_event_t get_queued_event(struct apm_user *as)
134 as->event_tail = (as->event_tail + 1) % APM_MAX_EVENTS;
135 return as->events[as->event_tail];
138 static void queue_event(apm_event_t event, struct apm_user *sender)
140 struct apm_user * as;
142 DBG("apm_emu: queue_event(%s)\n", apm_event_name[event-1]);
143 if (user_list == NULL)
145 for (as = user_list; as != NULL; as = as->next) {
148 as->event_head = (as->event_head + 1) % APM_MAX_EVENTS;
149 if (as->event_head == as->event_tail) {
153 printk(KERN_ERR "apm_emu: an event queue overflowed\n");
154 as->event_tail = (as->event_tail + 1) % APM_MAX_EVENTS;
156 as->events[as->event_head] = event;
160 case APM_SYS_SUSPEND:
161 case APM_USER_SUSPEND:
162 as->suspends_pending++;
165 case APM_NORMAL_RESUME:
166 as->suspend_waiting = 0;
170 wake_up_interruptible(&apm_waitqueue);
173 static int check_apm_user(struct apm_user *as, const char *func)
175 if ((as == NULL) || (as->magic != APM_BIOS_MAGIC)) {
176 printk(KERN_ERR "apm_emu: %s passed bad filp\n", func);
182 static ssize_t do_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
184 struct apm_user * as;
187 DECLARE_WAITQUEUE(wait, current);
189 as = fp->private_data;
190 if (check_apm_user(as, "read"))
192 if (count < sizeof(apm_event_t))
194 if (queue_empty(as)) {
195 if (fp->f_flags & O_NONBLOCK)
197 add_wait_queue(&apm_waitqueue, &wait);
199 set_current_state(TASK_INTERRUPTIBLE);
200 if (queue_empty(as) && !signal_pending(current)) {
204 set_current_state(TASK_RUNNING);
205 remove_wait_queue(&apm_waitqueue, &wait);
208 while ((i >= sizeof(event)) && !queue_empty(as)) {
209 event = get_queued_event(as);
210 DBG("apm_emu: do_read, returning: %s\n", apm_event_name[event-1]);
211 if (copy_to_user(buf, &event, sizeof(event))) {
217 case APM_SYS_SUSPEND:
218 case APM_USER_SUSPEND:
222 buf += sizeof(event);
227 if (signal_pending(current))
232 static unsigned int do_poll(struct file *fp, poll_table * wait)
234 struct apm_user * as;
236 as = fp->private_data;
237 if (check_apm_user(as, "poll"))
239 poll_wait(fp, &apm_waitqueue, wait);
240 if (!queue_empty(as))
241 return POLLIN | POLLRDNORM;
245 static int do_ioctl(struct inode * inode, struct file *filp,
246 u_int cmd, u_long arg)
248 struct apm_user * as;
249 DECLARE_WAITQUEUE(wait, current);
251 as = filp->private_data;
252 if (check_apm_user(as, "ioctl"))
257 case APM_IOC_SUSPEND:
258 /* If a suspend message was sent to userland, we
259 * consider this as a confirmation message
261 if (as->suspends_read > 0) {
263 as->suspends_pending--;
266 // Route to PMU suspend ?
269 as->suspend_waiting = 1;
270 add_wait_queue(&apm_waitqueue, &wait);
271 DBG("apm_emu: ioctl waking up sleep waiter !\n");
272 wake_up(&apm_suspend_waitqueue);
274 while(as->suspend_waiting && !signal_pending(current)) {
275 set_current_state(TASK_INTERRUPTIBLE);
278 set_current_state(TASK_RUNNING);
279 remove_wait_queue(&apm_waitqueue, &wait);
287 static int do_release(struct inode * inode, struct file * filp)
289 struct apm_user * as;
291 as = filp->private_data;
292 if (check_apm_user(as, "release"))
294 filp->private_data = NULL;
296 if (as->suspends_pending > 0) {
297 suspends_pending -= as->suspends_pending;
298 if (suspends_pending <= 0)
299 wake_up(&apm_suspend_waitqueue);
302 user_list = as->next;
304 struct apm_user * as1;
306 for (as1 = user_list;
307 (as1 != NULL) && (as1->next != as);
311 printk(KERN_ERR "apm: filp not in user list\n");
313 as1->next = as->next;
320 static int do_open(struct inode * inode, struct file * filp)
322 struct apm_user * as;
324 as = kmalloc(sizeof(*as), GFP_KERNEL);
326 printk(KERN_ERR "apm: cannot allocate struct of size %d bytes\n",
330 as->magic = APM_BIOS_MAGIC;
331 as->event_tail = as->event_head = 0;
332 as->suspends_pending = 0;
333 as->suspends_read = 0;
335 * XXX - this is a tiny bit broken, when we consider BSD
336 * process accounting. If the device is opened by root, we
337 * instantly flag that we used superuser privs. Who knows,
338 * we might close the device immediately without doing a
339 * privileged operation -- cevans
341 as->suser = capable(CAP_SYS_ADMIN);
342 as->next = user_list;
344 filp->private_data = as;
346 DBG("apm_emu: opened by %s, suser: %d\n", current->comm, (int)as->suser);
351 /* Wait for all clients to ack the suspend request. APM API
352 * doesn't provide a way to NAK, but this could be added
355 static int wait_all_suspend(void)
357 DECLARE_WAITQUEUE(wait, current);
359 add_wait_queue(&apm_suspend_waitqueue, &wait);
360 DBG("apm_emu: wait_all_suspend(), suspends_pending: %d\n", suspends_pending);
361 while(suspends_pending > 0) {
362 set_current_state(TASK_UNINTERRUPTIBLE);
365 set_current_state(TASK_RUNNING);
366 remove_wait_queue(&apm_suspend_waitqueue, &wait);
368 DBG("apm_emu: wait_all_suspend() - complete !\n");
373 static int apm_notify_sleep(struct pmu_sleep_notifier *self, int when)
376 case PBOOK_SLEEP_REQUEST:
377 queue_event(APM_SYS_SUSPEND, NULL);
378 if (!wait_all_suspend())
379 return PBOOK_SLEEP_REFUSE;
381 case PBOOK_SLEEP_REJECT:
383 queue_event(APM_NORMAL_RESUME, NULL);
386 return PBOOK_SLEEP_OK;
389 #define APM_CRITICAL 10
392 static int apm_emu_get_info(char *buf, char **start, off_t fpos, int length)
394 /* Arguments, with symbols from linux/apm_bios.h. Information is
395 from the Get Power Status (0x0a) call unless otherwise noted.
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 unsigned short ac_line_status;
433 unsigned short battery_status = 0;
434 unsigned short battery_flag = 0xff;
443 unsigned long btype = 0;
445 ac_line_status = ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0);
446 for (i=0; i<pmu_battery_count; i++) {
447 if (pmu_batteries[i].flags & PMU_BATT_PRESENT) {
453 percentage += (pmu_batteries[i].charge * 100) /
454 pmu_batteries[i].max_charge;
455 charge += pmu_batteries[i].charge;
456 amperage += pmu_batteries[i].amperage;
458 btype = (pmu_batteries[i].flags & PMU_BATT_TYPE_MASK);
460 if ((pmu_batteries[i].flags & PMU_BATT_CHARGING))
464 if (0 == battery_status)
466 battery_status = 0xff;
469 if (btype == PMU_BATT_TYPE_SMART)
470 time_units = (charge * 59) / (amperage * -1);
472 time_units = (charge * 16440) / (amperage * -60);
474 percentage /= real_count;
476 battery_status = 0x03;
478 } else if (percentage <= APM_CRITICAL) {
479 battery_status = 0x02;
481 } else if (percentage <= APM_LOW) {
482 battery_status = 0x01;
485 battery_status = 0x00;
489 p += sprintf(p, "%s %d.%d 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
491 (FAKE_APM_BIOS_VERSION >> 8) & 0xff,
492 FAKE_APM_BIOS_VERSION & 0xff,
504 static const struct file_operations apm_bios_fops = {
505 .owner = THIS_MODULE,
510 .release = do_release,
513 static struct miscdevice apm_device = {
519 static int __init apm_emu_init(void)
521 struct proc_dir_entry *apm_proc;
523 if (sys_ctrler != SYS_CTRLER_PMU) {
524 printk(KERN_INFO "apm_emu: Requires a machine with a PMU.\n");
528 apm_proc = create_proc_info_entry("apm", 0, NULL, apm_emu_get_info);
530 apm_proc->owner = THIS_MODULE;
532 if (misc_register(&apm_device) != 0)
533 printk(KERN_INFO "Could not create misc. device for apm\n");
535 pmu_register_sleep_notifier(&apm_sleep_notifier);
537 printk(KERN_INFO "apm_emu: APM Emulation %s initialized.\n", driver_version);
542 static void __exit apm_emu_exit(void)
544 pmu_unregister_sleep_notifier(&apm_sleep_notifier);
545 misc_deregister(&apm_device);
546 remove_proc_entry("apm", NULL);
548 printk(KERN_INFO "apm_emu: APM Emulation removed.\n");
551 module_init(apm_emu_init);
552 module_exit(apm_emu_exit);
554 MODULE_AUTHOR("Benjamin Herrenschmidt");
555 MODULE_DESCRIPTION("APM emulation layer for PowerMac");
556 MODULE_LICENSE("GPL");