apm-emulation.c

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

C
664
字号
			freezer_do_not_count();			wait_event(apm_suspend_waitqueue,				   as->suspend_state == SUSPEND_DONE);			/*			 * Since we are waiting until the suspend is done, the			 * try_to_freeze() in freezer_count() will not trigger			 */			freezer_count();		} else {			as->suspend_state = SUSPEND_WAIT;			mutex_unlock(&state_lock);			/*			 * Otherwise it is a request to suspend the system.			 * Queue an event for all readers, and expect an			 * acknowledge from all writers who haven't already			 * acknowledged.			 */			err = queue_suspend_event(APM_USER_SUSPEND, as);			if (err < 0) {				/*				 * Avoid taking the lock here - this				 * should be fine.				 */				as->suspend_state = SUSPEND_NONE;				break;			}			if (err > 0)				apm_suspend();			/*			 * Wait for the suspend/resume to complete.  If there			 * are pending acknowledges, we wait here for them.			 */			wait_event_freezable(apm_suspend_waitqueue,					 as->suspend_state == SUSPEND_DONE);		}		mutex_lock(&state_lock);		err = as->suspend_result;		as->suspend_state = SUSPEND_NONE;		mutex_unlock(&state_lock);		break;	}	return err;}static int apm_release(struct inode * inode, struct file * filp){	struct apm_user *as = filp->private_data;	int pending = 0;	filp->private_data = NULL;	down_write(&user_list_lock);	list_del(&as->list);	up_write(&user_list_lock);	/*	 * We are now unhooked from the chain.  As far as new	 * events are concerned, we no longer exist.  However, we	 * need to balance suspends_pending, which means the	 * possibility of sleeping.	 */	mutex_lock(&state_lock);	if (as->suspend_state != SUSPEND_NONE) {		suspends_pending -= 1;		pending = suspends_pending == 0;	}	mutex_unlock(&state_lock);	if (pending)		apm_suspend();	kfree(as);	return 0;}static int apm_open(struct inode * inode, struct file * filp){	struct apm_user *as;	as = kzalloc(sizeof(*as), GFP_KERNEL);	if (as) {		/*		 * XXX - this is a tiny bit broken, when we consider BSD		 * process accounting. If the device is opened by root, we		 * instantly flag that we used superuser privs. Who knows,		 * we might close the device immediately without doing a		 * privileged operation -- cevans		 */		as->suser = capable(CAP_SYS_ADMIN);		as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;		as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;		down_write(&user_list_lock);		list_add(&as->list, &apm_user_list);		up_write(&user_list_lock);		filp->private_data = as;	}	return as ? 0 : -ENOMEM;}static struct file_operations apm_bios_fops = {	.owner		= THIS_MODULE,	.read		= apm_read,	.poll		= apm_poll,	.ioctl		= apm_ioctl,	.open		= apm_open,	.release	= apm_release,};static struct miscdevice apm_device = {	.minor		= APM_MINOR_DEV,	.name		= "apm_bios",	.fops		= &apm_bios_fops};#ifdef CONFIG_PROC_FS/* * Arguments, with symbols from linux/apm_bios.h. * *   0) Linux driver version (this will change if format changes) *   1) APM BIOS Version.  Usually 1.0, 1.1 or 1.2. *   2) APM flags from APM Installation Check (0x00): *	bit 0: APM_16_BIT_SUPPORT *	bit 1: APM_32_BIT_SUPPORT *	bit 2: APM_IDLE_SLOWS_CLOCK *	bit 3: APM_BIOS_DISABLED *	bit 4: APM_BIOS_DISENGAGED *   3) AC line status *	0x00: Off-line *	0x01: On-line *	0x02: On backup power (BIOS >= 1.1 only) *	0xff: Unknown *   4) Battery status *	0x00: High *	0x01: Low *	0x02: Critical *	0x03: Charging *	0x04: Selected battery not present (BIOS >= 1.2 only) *	0xff: Unknown *   5) Battery flag *	bit 0: High *	bit 1: Low *	bit 2: Critical *	bit 3: Charging *	bit 7: No system battery *	0xff: Unknown *   6) Remaining battery life (percentage of charge): *	0-100: valid *	-1: Unknown *   7) Remaining battery life (time units): *	Number of remaining minutes or seconds *	-1: Unknown *   8) min = minutes; sec = seconds */static int apm_get_info(char *buf, char **start, off_t fpos, int length){	struct apm_power_info info;	char *units;	int ret;	info.ac_line_status = 0xff;	info.battery_status = 0xff;	info.battery_flag   = 0xff;	info.battery_life   = -1;	info.time	    = -1;	info.units	    = -1;	if (apm_get_power_status)		apm_get_power_status(&info);	switch (info.units) {	default:	units = "?";	break;	case 0: 	units = "min";	break;	case 1: 	units = "sec";	break;	}	ret = sprintf(buf, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",		     driver_version, APM_32_BIT_SUPPORT,		     info.ac_line_status, info.battery_status,		     info.battery_flag, info.battery_life,		     info.time, units);	return ret;}#endifstatic int kapmd(void *arg){	do {		apm_event_t event;		int ret;		wait_event_interruptible(kapmd_wait,				!queue_empty(&kapmd_queue) || kthread_should_stop());		if (kthread_should_stop())			break;		spin_lock_irq(&kapmd_queue_lock);		event = 0;		if (!queue_empty(&kapmd_queue))			event = queue_get_event(&kapmd_queue);		spin_unlock_irq(&kapmd_queue_lock);		switch (event) {		case 0:			break;		case APM_LOW_BATTERY:		case APM_POWER_STATUS_CHANGE:			queue_event(event);			break;		case APM_USER_SUSPEND:		case APM_SYS_SUSPEND:			ret = queue_suspend_event(event, NULL);			if (ret < 0) {				/*				 * We were busy.  Try again in 50ms.				 */				queue_add_event(&kapmd_queue, event);				msleep(50);			}			if (ret > 0)				apm_suspend();			break;		case APM_CRITICAL_SUSPEND:			apm_suspend();			break;		}	} while (1);	return 0;}static int __init apm_init(void){	int ret;	if (apm_disabled) {		printk(KERN_NOTICE "apm: disabled on user request.\n");		return -ENODEV;	}	kapmd_tsk = kthread_create(kapmd, NULL, "kapmd");	if (IS_ERR(kapmd_tsk)) {		ret = PTR_ERR(kapmd_tsk);		kapmd_tsk = NULL;		return ret;	}	wake_up_process(kapmd_tsk);#ifdef CONFIG_PROC_FS	create_proc_info_entry("apm", 0, NULL, apm_get_info);#endif	ret = misc_register(&apm_device);	if (ret != 0) {		remove_proc_entry("apm", NULL);		kthread_stop(kapmd_tsk);	}	return ret;}static void __exit apm_exit(void){	misc_deregister(&apm_device);	remove_proc_entry("apm", NULL);	kthread_stop(kapmd_tsk);}module_init(apm_init);module_exit(apm_exit);MODULE_AUTHOR("Stephen Rothwell");MODULE_DESCRIPTION("Advanced Power Management");MODULE_LICENSE("GPL");#ifndef MODULEstatic int __init apm_setup(char *str){	while ((str != NULL) && (*str != '\0')) {		if (strncmp(str, "off", 3) == 0)			apm_disabled = 1;		if (strncmp(str, "on", 2) == 0)			apm_disabled = 0;		str = strchr(str, ',');		if (str != NULL)			str += strspn(str, ", \t");	}	return 1;}__setup("apm=", apm_setup);#endif/** * apm_queue_event - queue an APM event for kapmd * @event: APM event * * Queue an APM event for kapmd to process and ultimately take the * appropriate action.  Only a subset of events are handled: *   %APM_LOW_BATTERY *   %APM_POWER_STATUS_CHANGE *   %APM_USER_SUSPEND *   %APM_SYS_SUSPEND *   %APM_CRITICAL_SUSPEND */void apm_queue_event(apm_event_t event){	unsigned long flags;	spin_lock_irqsave(&kapmd_queue_lock, flags);	queue_add_event(&kapmd_queue, event);	spin_unlock_irqrestore(&kapmd_queue_lock, flags);	wake_up_interruptible(&kapmd_wait);}EXPORT_SYMBOL(apm_queue_event);

⌨️ 快捷键说明

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