apm-emulation.c

来自「linux 内核源代码」· C语言 代码 · 共 664 行 · 第 1/2 页

C
664
字号
/* * bios-less APM driver for ARM Linux *  Jamey Hicks <jamey@crl.dec.com> *  adapted from the APM BIOS driver for Linux by Stephen Rothwell (sfr@linuxcare.com) * * APM 1.2 Reference: *   Intel Corporation, Microsoft Corporation. Advanced Power Management *   (APM) BIOS Interface Specification, Revision 1.2, February 1996. * * [This document is available from Microsoft at: *    http://www.microsoft.com/hwdev/busbios/amp_12.htm] */#include <linux/module.h>#include <linux/poll.h>#include <linux/slab.h>#include <linux/proc_fs.h>#include <linux/miscdevice.h>#include <linux/apm_bios.h>#include <linux/capability.h>#include <linux/sched.h>#include <linux/suspend.h>#include <linux/apm-emulation.h>#include <linux/freezer.h>#include <linux/device.h>#include <linux/kernel.h>#include <linux/list.h>#include <linux/init.h>#include <linux/completion.h>#include <linux/kthread.h>#include <linux/delay.h>#include <asm/system.h>/* * The apm_bios device is one of the misc char devices. * This is its minor number. */#define APM_MINOR_DEV	134/* * See Documentation/Config.help for the configuration options. * * Various options can be changed at boot time as follows: * (We allow underscores for compatibility with the modules code) *	apm=on/off			enable/disable APM *//* * Maximum number of events stored */#define APM_MAX_EVENTS		16struct apm_queue {	unsigned int		event_head;	unsigned int		event_tail;	apm_event_t		events[APM_MAX_EVENTS];};/* * The per-file APM data */struct apm_user {	struct list_head	list;	unsigned int		suser: 1;	unsigned int		writer: 1;	unsigned int		reader: 1;	int			suspend_result;	unsigned int		suspend_state;#define SUSPEND_NONE	0		/* no suspend pending */#define SUSPEND_PENDING	1		/* suspend pending read */#define SUSPEND_READ	2		/* suspend read, pending ack */#define SUSPEND_ACKED	3		/* suspend acked */#define SUSPEND_WAIT	4		/* waiting for suspend */#define SUSPEND_DONE	5		/* suspend completed */	struct apm_queue	queue;};/* * Local variables */static int suspends_pending;static int apm_disabled;static struct task_struct *kapmd_tsk;static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);/* * This is a list of everyone who has opened /dev/apm_bios */static DECLARE_RWSEM(user_list_lock);static LIST_HEAD(apm_user_list);/* * kapmd info.  kapmd provides us a process context to handle * "APM" events within - specifically necessary if we're going * to be suspending the system. */static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);static DEFINE_SPINLOCK(kapmd_queue_lock);static struct apm_queue kapmd_queue;static DEFINE_MUTEX(state_lock);static const char driver_version[] = "1.13";	/* no spaces *//* * Compatibility cruft until the IPAQ people move over to the new * interface. */static void __apm_get_power_status(struct apm_power_info *info){}/* * This allows machines to provide their own "apm get power status" function. */void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;EXPORT_SYMBOL(apm_get_power_status);/* * APM event queue management. */static inline int queue_empty(struct apm_queue *q){	return q->event_head == q->event_tail;}static inline apm_event_t queue_get_event(struct apm_queue *q){	q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;	return q->events[q->event_tail];}static void queue_add_event(struct apm_queue *q, apm_event_t event){	q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;	if (q->event_head == q->event_tail) {		static int notified;		if (notified++ == 0)		    printk(KERN_ERR "apm: an event queue overflowed\n");		q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;	}	q->events[q->event_head] = event;}static void queue_event(apm_event_t event){	struct apm_user *as;	down_read(&user_list_lock);	list_for_each_entry(as, &apm_user_list, list) {		if (as->reader)			queue_add_event(&as->queue, event);	}	up_read(&user_list_lock);	wake_up_interruptible(&apm_waitqueue);}/* * queue_suspend_event - queue an APM suspend event. * * Check that we're in a state where we can suspend.  If not, * return -EBUSY.  Otherwise, queue an event to all "writer" * users.  If there are no "writer" users, return '1' to * indicate that we can immediately suspend. */static int queue_suspend_event(apm_event_t event, struct apm_user *sender){	struct apm_user *as;	int ret = 1;	mutex_lock(&state_lock);	down_read(&user_list_lock);	/*	 * If a thread is still processing, we can't suspend, so reject	 * the request.	 */	list_for_each_entry(as, &apm_user_list, list) {		if (as != sender && as->reader && as->writer && as->suser &&		    as->suspend_state != SUSPEND_NONE) {			ret = -EBUSY;			goto out;		}	}	list_for_each_entry(as, &apm_user_list, list) {		if (as != sender && as->reader && as->writer && as->suser) {			as->suspend_state = SUSPEND_PENDING;			suspends_pending++;			queue_add_event(&as->queue, event);			ret = 0;		}	} out:	up_read(&user_list_lock);	mutex_unlock(&state_lock);	wake_up_interruptible(&apm_waitqueue);	return ret;}static void apm_suspend(void){	struct apm_user *as;	int err = pm_suspend(PM_SUSPEND_MEM);	/*	 * Anyone on the APM queues will think we're still suspended.	 * Send a message so everyone knows we're now awake again.	 */	queue_event(APM_NORMAL_RESUME);	/*	 * Finally, wake up anyone who is sleeping on the suspend.	 */	mutex_lock(&state_lock);	down_read(&user_list_lock);	list_for_each_entry(as, &apm_user_list, list) {		if (as->suspend_state == SUSPEND_WAIT ||		    as->suspend_state == SUSPEND_ACKED) {			as->suspend_result = err;			as->suspend_state = SUSPEND_DONE;		}	}	up_read(&user_list_lock);	mutex_unlock(&state_lock);	wake_up(&apm_suspend_waitqueue);}static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos){	struct apm_user *as = fp->private_data;	apm_event_t event;	int i = count, ret = 0;	if (count < sizeof(apm_event_t))		return -EINVAL;	if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)		return -EAGAIN;	wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));	while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {		event = queue_get_event(&as->queue);		ret = -EFAULT;		if (copy_to_user(buf, &event, sizeof(event)))			break;		mutex_lock(&state_lock);		if (as->suspend_state == SUSPEND_PENDING &&		    (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND))			as->suspend_state = SUSPEND_READ;		mutex_unlock(&state_lock);		buf += sizeof(event);		i -= sizeof(event);	}	if (i < count)		ret = count - i;	return ret;}static unsigned int apm_poll(struct file *fp, poll_table * wait){	struct apm_user *as = fp->private_data;	poll_wait(fp, &apm_waitqueue, wait);	return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;}/* * apm_ioctl - handle APM ioctl * * APM_IOC_SUSPEND *   This IOCTL is overloaded, and performs two functions.  It is used to: *     - initiate a suspend *     - acknowledge a suspend read from /dev/apm_bios. *   Only when everyone who has opened /dev/apm_bios with write permission *   has acknowledge does the actual suspend happen. */static intapm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg){	struct apm_user *as = filp->private_data;	int err = -EINVAL;	if (!as->suser || !as->writer)		return -EPERM;	switch (cmd) {	case APM_IOC_SUSPEND:		mutex_lock(&state_lock);		as->suspend_result = -EINTR;		if (as->suspend_state == SUSPEND_READ) {			int pending;			/*			 * If we read a suspend command from /dev/apm_bios,			 * then the corresponding APM_IOC_SUSPEND ioctl is			 * interpreted as an acknowledge.			 */			as->suspend_state = SUSPEND_ACKED;			suspends_pending--;			pending = suspends_pending == 0;			mutex_unlock(&state_lock);			/*			 * If there are no further acknowledges required,			 * suspend the system.			 */			if (pending)				apm_suspend();			/*			 * Wait for the suspend/resume to complete.  If there			 * are pending acknowledges, we wait here for them.			 */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?